c1727525c9ebf23cc4a43697249435edfe83ea8c
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 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     case DW_TAG_const_type:
3224     case DW_TAG_volatile_type:
3225       /* Add a typedef symbol for the type definition, if it has a
3226          DW_AT_name.  */
3227       new_symbol (die, read_type_die (die, cu), cu);
3228       break;
3229     case DW_TAG_common_block:
3230       read_common_block (die, cu);
3231       break;
3232     case DW_TAG_common_inclusion:
3233       break;
3234     case DW_TAG_namespace:
3235       processing_has_namespace_info = 1;
3236       read_namespace (die, cu);
3237       break;
3238     case DW_TAG_module:
3239       processing_has_namespace_info = 1;
3240       read_module (die, cu);
3241       break;
3242     case DW_TAG_imported_declaration:
3243     case DW_TAG_imported_module:
3244       processing_has_namespace_info = 1;
3245       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3246                                  || cu->language != language_fortran))
3247         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
3248                    dwarf_tag_name (die->tag));
3249       read_import_statement (die, cu);
3250       break;
3251     default:
3252       new_symbol (die, NULL, cu);
3253       break;
3254     }
3255 }
3256
3257 /* A helper function for dwarf2_compute_name which determines whether DIE
3258    needs to have the name of the scope prepended to the name listed in the
3259    die.  */
3260
3261 static int
3262 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
3263 {
3264   struct attribute *attr;
3265
3266   switch (die->tag)
3267     {
3268     case DW_TAG_namespace:
3269     case DW_TAG_typedef:
3270     case DW_TAG_class_type:
3271     case DW_TAG_interface_type:
3272     case DW_TAG_structure_type:
3273     case DW_TAG_union_type:
3274     case DW_TAG_enumeration_type:
3275     case DW_TAG_enumerator:
3276     case DW_TAG_subprogram:
3277     case DW_TAG_member:
3278       return 1;
3279
3280     case DW_TAG_variable:
3281       /* We only need to prefix "globally" visible variables.  These include
3282          any variable marked with DW_AT_external or any variable that
3283          lives in a namespace.  [Variables in anonymous namespaces
3284          require prefixing, but they are not DW_AT_external.]  */
3285
3286       if (dwarf2_attr (die, DW_AT_specification, cu))
3287         {
3288           struct dwarf2_cu *spec_cu = cu;
3289
3290           return die_needs_namespace (die_specification (die, &spec_cu),
3291                                       spec_cu);
3292         }
3293
3294       attr = dwarf2_attr (die, DW_AT_external, cu);
3295       if (attr == NULL && die->parent->tag != DW_TAG_namespace
3296           && die->parent->tag != DW_TAG_module)
3297         return 0;
3298       /* A variable in a lexical block of some kind does not need a
3299          namespace, even though in C++ such variables may be external
3300          and have a mangled name.  */
3301       if (die->parent->tag ==  DW_TAG_lexical_block
3302           || die->parent->tag ==  DW_TAG_try_block
3303           || die->parent->tag ==  DW_TAG_catch_block
3304           || die->parent->tag == DW_TAG_subprogram)
3305         return 0;
3306       return 1;
3307
3308     default:
3309       return 0;
3310     }
3311 }
3312
3313 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
3314    compute the physname for the object, which include a method's
3315    formal parameters (C++/Java) and return type (Java).
3316
3317    For Ada, return the DIE's linkage name rather than the fully qualified
3318    name.  PHYSNAME is ignored..
3319
3320    The result is allocated on the objfile_obstack and canonicalized.  */
3321
3322 static const char *
3323 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
3324                      int physname)
3325 {
3326   if (name == NULL)
3327     name = dwarf2_name (die, cu);
3328
3329   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
3330      compute it by typename_concat inside GDB.  */
3331   if (cu->language == language_ada
3332       || (cu->language == language_fortran && physname))
3333     {
3334       /* For Ada unit, we prefer the linkage name over the name, as
3335          the former contains the exported name, which the user expects
3336          to be able to reference.  Ideally, we want the user to be able
3337          to reference this entity using either natural or linkage name,
3338          but we haven't started looking at this enhancement yet.  */
3339       struct attribute *attr;
3340
3341       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
3342       if (attr == NULL)
3343         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
3344       if (attr && DW_STRING (attr))
3345         return DW_STRING (attr);
3346     }
3347
3348   /* These are the only languages we know how to qualify names in.  */
3349   if (name != NULL
3350       && (cu->language == language_cplus || cu->language == language_java
3351           || cu->language == language_fortran))
3352     {
3353       if (die_needs_namespace (die, cu))
3354         {
3355           long length;
3356           char *prefix;
3357           struct ui_file *buf;
3358
3359           prefix = determine_prefix (die, cu);
3360           buf = mem_fileopen ();
3361           if (*prefix != '\0')
3362             {
3363               char *prefixed_name = typename_concat (NULL, prefix, name,
3364                                                      physname, cu);
3365
3366               fputs_unfiltered (prefixed_name, buf);
3367               xfree (prefixed_name);
3368             }
3369           else
3370             fputs_unfiltered (name ? name : "", buf);
3371
3372           /* For Java and C++ methods, append formal parameter type
3373              information, if PHYSNAME.  */
3374
3375           if (physname && die->tag == DW_TAG_subprogram
3376               && (cu->language == language_cplus
3377                   || cu->language == language_java))
3378             {
3379               struct type *type = read_type_die (die, cu);
3380
3381               c_type_print_args (type, buf, 0, cu->language);
3382
3383               if (cu->language == language_java)
3384                 {
3385                   /* For java, we must append the return type to method
3386                      names. */
3387                   if (die->tag == DW_TAG_subprogram)
3388                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
3389                                      0, 0);
3390                 }
3391               else if (cu->language == language_cplus)
3392                 {
3393                   if (TYPE_NFIELDS (type) > 0
3394                       && TYPE_FIELD_ARTIFICIAL (type, 0)
3395                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
3396                     fputs_unfiltered (" const", buf);
3397                 }
3398             }
3399
3400           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
3401                                        &length);
3402           ui_file_delete (buf);
3403
3404           if (cu->language == language_cplus)
3405             {
3406               char *cname
3407                 = dwarf2_canonicalize_name (name, cu,
3408                                             &cu->objfile->objfile_obstack);
3409
3410               if (cname != NULL)
3411                 name = cname;
3412             }
3413         }
3414     }
3415
3416   return name;
3417 }
3418
3419 /* Return the fully qualified name of DIE, based on its DW_AT_name.
3420    If scope qualifiers are appropriate they will be added.  The result
3421    will be allocated on the objfile_obstack, or NULL if the DIE does
3422    not have a name.  NAME may either be from a previous call to
3423    dwarf2_name or NULL.
3424
3425    The output string will be canonicalized (if C++/Java). */
3426
3427 static const char *
3428 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
3429 {
3430   return dwarf2_compute_name (name, die, cu, 0);
3431 }
3432
3433 /* Construct a physname for the given DIE in CU.  NAME may either be
3434    from a previous call to dwarf2_name or NULL.  The result will be
3435    allocated on the objfile_objstack or NULL if the DIE does not have a
3436    name.
3437
3438    The output string will be canonicalized (if C++/Java).  */
3439
3440 static const char *
3441 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
3442 {
3443   return dwarf2_compute_name (name, die, cu, 1);
3444 }
3445
3446 /* Read the import statement specified by the given die and record it.  */
3447
3448 static void
3449 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3450 {
3451   struct attribute *import_attr;
3452   struct die_info *imported_die;
3453   struct dwarf2_cu *imported_cu;
3454   const char *imported_name;
3455   const char *imported_name_prefix;
3456   const char *canonical_name;
3457   const char *import_alias;
3458   const char *imported_declaration = NULL;
3459   const char *import_prefix;
3460
3461   char *temp;
3462
3463   import_attr = dwarf2_attr (die, DW_AT_import, cu);
3464   if (import_attr == NULL)
3465     {
3466       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3467                  dwarf_tag_name (die->tag));
3468       return;
3469     }
3470
3471   imported_cu = cu;
3472   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
3473   imported_name = dwarf2_name (imported_die, imported_cu);
3474   if (imported_name == NULL)
3475     {
3476       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3477
3478         The import in the following code:
3479         namespace A
3480           {
3481             typedef int B;
3482           }
3483
3484         int main ()
3485           {
3486             using A::B;
3487             B b;
3488             return b;
3489           }
3490
3491         ...
3492          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3493             <52>   DW_AT_decl_file   : 1
3494             <53>   DW_AT_decl_line   : 6
3495             <54>   DW_AT_import      : <0x75>
3496          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3497             <59>   DW_AT_name        : B
3498             <5b>   DW_AT_decl_file   : 1
3499             <5c>   DW_AT_decl_line   : 2
3500             <5d>   DW_AT_type        : <0x6e>
3501         ...
3502          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3503             <76>   DW_AT_byte_size   : 4
3504             <77>   DW_AT_encoding    : 5        (signed)
3505
3506         imports the wrong die ( 0x75 instead of 0x58 ).
3507         This case will be ignored until the gcc bug is fixed.  */
3508       return;
3509     }
3510
3511   /* Figure out the local name after import.  */
3512   import_alias = dwarf2_name (die, cu);
3513
3514   /* Figure out where the statement is being imported to.  */
3515   import_prefix = determine_prefix (die, cu);
3516
3517   /* Figure out what the scope of the imported die is and prepend it
3518      to the name of the imported die.  */
3519   imported_name_prefix = determine_prefix (imported_die, imported_cu);
3520
3521   if (imported_die->tag != DW_TAG_namespace
3522       && imported_die->tag != DW_TAG_module)
3523     {
3524       imported_declaration = imported_name;
3525       canonical_name = imported_name_prefix;
3526     }
3527   else if (strlen (imported_name_prefix) > 0)
3528     {
3529       temp = alloca (strlen (imported_name_prefix)
3530                      + 2 + strlen (imported_name) + 1);
3531       strcpy (temp, imported_name_prefix);
3532       strcat (temp, "::");
3533       strcat (temp, imported_name);
3534       canonical_name = temp;
3535     }
3536   else
3537     canonical_name = imported_name;
3538
3539   cp_add_using_directive (import_prefix,
3540                           canonical_name,
3541                           import_alias,
3542                           imported_declaration,
3543                           &cu->objfile->objfile_obstack);
3544 }
3545
3546 static void
3547 initialize_cu_func_list (struct dwarf2_cu *cu)
3548 {
3549   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
3550 }
3551
3552 static void
3553 free_cu_line_header (void *arg)
3554 {
3555   struct dwarf2_cu *cu = arg;
3556
3557   free_line_header (cu->line_header);
3558   cu->line_header = NULL;
3559 }
3560
3561 static void
3562 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
3563 {
3564   struct objfile *objfile = cu->objfile;
3565   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3566   CORE_ADDR lowpc = ((CORE_ADDR) -1);
3567   CORE_ADDR highpc = ((CORE_ADDR) 0);
3568   struct attribute *attr;
3569   char *name = NULL;
3570   char *comp_dir = NULL;
3571   struct die_info *child_die;
3572   bfd *abfd = objfile->obfd;
3573   struct line_header *line_header = 0;
3574   CORE_ADDR baseaddr;
3575
3576   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3577
3578   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
3579
3580   /* If we didn't find a lowpc, set it to highpc to avoid complaints
3581      from finish_block.  */
3582   if (lowpc == ((CORE_ADDR) -1))
3583     lowpc = highpc;
3584   lowpc += baseaddr;
3585   highpc += baseaddr;
3586
3587   /* Find the filename.  Do not use dwarf2_name here, since the filename
3588      is not a source language identifier.  */
3589   attr = dwarf2_attr (die, DW_AT_name, cu);
3590   if (attr)
3591     {
3592       name = DW_STRING (attr);
3593     }
3594
3595   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3596   if (attr)
3597     comp_dir = DW_STRING (attr);
3598   else if (name != NULL && IS_ABSOLUTE_PATH (name))
3599     {
3600       comp_dir = ldirname (name);
3601       if (comp_dir != NULL)
3602         make_cleanup (xfree, comp_dir);
3603     }
3604   if (comp_dir != NULL)
3605     {
3606       /* Irix 6.2 native cc prepends <machine>.: to the compilation
3607          directory, get rid of it.  */
3608       char *cp = strchr (comp_dir, ':');
3609
3610       if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3611         comp_dir = cp + 1;
3612     }
3613
3614   if (name == NULL)
3615     name = "<unknown>";
3616
3617   attr = dwarf2_attr (die, DW_AT_language, cu);
3618   if (attr)
3619     {
3620       set_cu_language (DW_UNSND (attr), cu);
3621     }
3622
3623   attr = dwarf2_attr (die, DW_AT_producer, cu);
3624   if (attr)
3625     cu->producer = DW_STRING (attr);
3626
3627   /* We assume that we're processing GCC output. */
3628   processing_gcc_compilation = 2;
3629
3630   processing_has_namespace_info = 0;
3631
3632   start_symtab (name, comp_dir, lowpc);
3633   record_debugformat ("DWARF 2");
3634   record_producer (cu->producer);
3635
3636   initialize_cu_func_list (cu);
3637
3638   /* Decode line number information if present.  We do this before
3639      processing child DIEs, so that the line header table is available
3640      for DW_AT_decl_file.  */
3641   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3642   if (attr)
3643     {
3644       unsigned int line_offset = DW_UNSND (attr);
3645       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
3646       if (line_header)
3647         {
3648           cu->line_header = line_header;
3649           make_cleanup (free_cu_line_header, cu);
3650           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
3651         }
3652     }
3653
3654   /* Process all dies in compilation unit.  */
3655   if (die->child != NULL)
3656     {
3657       child_die = die->child;
3658       while (child_die && child_die->tag)
3659         {
3660           process_die (child_die, cu);
3661           child_die = sibling_die (child_die);
3662         }
3663     }
3664
3665   /* Decode macro information, if present.  Dwarf 2 macro information
3666      refers to information in the line number info statement program
3667      header, so we can only read it if we've read the header
3668      successfully.  */
3669   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
3670   if (attr && line_header)
3671     {
3672       unsigned int macro_offset = DW_UNSND (attr);
3673
3674       dwarf_decode_macros (line_header, macro_offset,
3675                            comp_dir, abfd, cu);
3676     }
3677   do_cleanups (back_to);
3678 }
3679
3680 /* For TUs we want to skip the first top level sibling if it's not the
3681    actual type being defined by this TU.  In this case the first top
3682    level sibling is there to provide context only.  */
3683
3684 static void
3685 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
3686 {
3687   struct objfile *objfile = cu->objfile;
3688   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3689   CORE_ADDR lowpc;
3690   struct attribute *attr;
3691   char *name = NULL;
3692   char *comp_dir = NULL;
3693   struct die_info *child_die;
3694   bfd *abfd = objfile->obfd;
3695
3696   /* start_symtab needs a low pc, but we don't really have one.
3697      Do what read_file_scope would do in the absence of such info.  */
3698   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3699
3700   /* Find the filename.  Do not use dwarf2_name here, since the filename
3701      is not a source language identifier.  */
3702   attr = dwarf2_attr (die, DW_AT_name, cu);
3703   if (attr)
3704     name = DW_STRING (attr);
3705
3706   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3707   if (attr)
3708     comp_dir = DW_STRING (attr);
3709   else if (name != NULL && IS_ABSOLUTE_PATH (name))
3710     {
3711       comp_dir = ldirname (name);
3712       if (comp_dir != NULL)
3713         make_cleanup (xfree, comp_dir);
3714     }
3715
3716   if (name == NULL)
3717     name = "<unknown>";
3718
3719   attr = dwarf2_attr (die, DW_AT_language, cu);
3720   if (attr)
3721     set_cu_language (DW_UNSND (attr), cu);
3722
3723   /* This isn't technically needed today.  It is done for symmetry
3724      with read_file_scope.  */
3725   attr = dwarf2_attr (die, DW_AT_producer, cu);
3726   if (attr)
3727     cu->producer = DW_STRING (attr);
3728
3729   /* We assume that we're processing GCC output. */
3730   processing_gcc_compilation = 2;
3731
3732   processing_has_namespace_info = 0;
3733
3734   start_symtab (name, comp_dir, lowpc);
3735   record_debugformat ("DWARF 2");
3736   record_producer (cu->producer);
3737
3738   /* Process the dies in the type unit.  */
3739   if (die->child == NULL)
3740     {
3741       dump_die_for_error (die);
3742       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
3743              bfd_get_filename (abfd));
3744     }
3745
3746   child_die = die->child;
3747
3748   while (child_die && child_die->tag)
3749     {
3750       process_die (child_die, cu);
3751
3752       child_die = sibling_die (child_die);
3753     }
3754
3755   do_cleanups (back_to);
3756 }
3757
3758 static void
3759 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3760                      struct dwarf2_cu *cu)
3761 {
3762   struct function_range *thisfn;
3763
3764   thisfn = (struct function_range *)
3765     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
3766   thisfn->name = name;
3767   thisfn->lowpc = lowpc;
3768   thisfn->highpc = highpc;
3769   thisfn->seen_line = 0;
3770   thisfn->next = NULL;
3771
3772   if (cu->last_fn == NULL)
3773       cu->first_fn = thisfn;
3774   else
3775       cu->last_fn->next = thisfn;
3776
3777   cu->last_fn = thisfn;
3778 }
3779
3780 /* qsort helper for inherit_abstract_dies.  */
3781
3782 static int
3783 unsigned_int_compar (const void *ap, const void *bp)
3784 {
3785   unsigned int a = *(unsigned int *) ap;
3786   unsigned int b = *(unsigned int *) bp;
3787
3788   return (a > b) - (b > a);
3789 }
3790
3791 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3792    Inherit only the children of the DW_AT_abstract_origin DIE not being already
3793    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
3794
3795 static void
3796 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3797 {
3798   struct die_info *child_die;
3799   unsigned die_children_count;
3800   /* CU offsets which were referenced by children of the current DIE.  */
3801   unsigned *offsets;
3802   unsigned *offsets_end, *offsetp;
3803   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
3804   struct die_info *origin_die;
3805   /* Iterator of the ORIGIN_DIE children.  */
3806   struct die_info *origin_child_die;
3807   struct cleanup *cleanups;
3808   struct attribute *attr;
3809
3810   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3811   if (!attr)
3812     return;
3813
3814   origin_die = follow_die_ref (die, attr, &cu);
3815   if (die->tag != origin_die->tag
3816       && !(die->tag == DW_TAG_inlined_subroutine
3817            && origin_die->tag == DW_TAG_subprogram))
3818     complaint (&symfile_complaints,
3819                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3820                die->offset, origin_die->offset);
3821
3822   child_die = die->child;
3823   die_children_count = 0;
3824   while (child_die && child_die->tag)
3825     {
3826       child_die = sibling_die (child_die);
3827       die_children_count++;
3828     }
3829   offsets = xmalloc (sizeof (*offsets) * die_children_count);
3830   cleanups = make_cleanup (xfree, offsets);
3831
3832   offsets_end = offsets;
3833   child_die = die->child;
3834   while (child_die && child_die->tag)
3835     {
3836       /* For each CHILD_DIE, find the corresponding child of
3837          ORIGIN_DIE.  If there is more than one layer of
3838          DW_AT_abstract_origin, follow them all; there shouldn't be,
3839          but GCC versions at least through 4.4 generate this (GCC PR
3840          40573).  */
3841       struct die_info *child_origin_die = child_die;
3842
3843       while (1)
3844         {
3845           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
3846           if (attr == NULL)
3847             break;
3848           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
3849         }
3850
3851       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3852          counterpart may exist.  */
3853       if (child_origin_die != child_die)
3854         {
3855           if (child_die->tag != child_origin_die->tag
3856               && !(child_die->tag == DW_TAG_inlined_subroutine
3857                    && child_origin_die->tag == DW_TAG_subprogram))
3858             complaint (&symfile_complaints,
3859                        _("Child DIE 0x%x and its abstract origin 0x%x have "
3860                          "different tags"), child_die->offset,
3861                        child_origin_die->offset);
3862           if (child_origin_die->parent != origin_die)
3863             complaint (&symfile_complaints,
3864                        _("Child DIE 0x%x and its abstract origin 0x%x have "
3865                          "different parents"), child_die->offset,
3866                        child_origin_die->offset);
3867           else
3868             *offsets_end++ = child_origin_die->offset;
3869         }
3870       child_die = sibling_die (child_die);
3871     }
3872   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3873          unsigned_int_compar);
3874   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3875     if (offsetp[-1] == *offsetp)
3876       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3877                                         "to DIE 0x%x as their abstract origin"),
3878                  die->offset, *offsetp);
3879
3880   offsetp = offsets;
3881   origin_child_die = origin_die->child;
3882   while (origin_child_die && origin_child_die->tag)
3883     {
3884       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
3885       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3886         offsetp++;
3887       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3888         {
3889           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
3890           process_die (origin_child_die, cu);
3891         }
3892       origin_child_die = sibling_die (origin_child_die);
3893     }
3894
3895   do_cleanups (cleanups);
3896 }
3897
3898 static void
3899 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
3900 {
3901   struct objfile *objfile = cu->objfile;
3902   struct context_stack *new;
3903   CORE_ADDR lowpc;
3904   CORE_ADDR highpc;
3905   struct die_info *child_die;
3906   struct attribute *attr, *call_line, *call_file;
3907   char *name;
3908   CORE_ADDR baseaddr;
3909   struct block *block;
3910   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3911
3912   if (inlined_func)
3913     {
3914       /* If we do not have call site information, we can't show the
3915          caller of this inlined function.  That's too confusing, so
3916          only use the scope for local variables.  */
3917       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3918       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3919       if (call_line == NULL || call_file == NULL)
3920         {
3921           read_lexical_block_scope (die, cu);
3922           return;
3923         }
3924     }
3925
3926   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3927
3928   name = dwarf2_name (die, cu);
3929
3930   /* Ignore functions with missing or empty names.  These are actually
3931      illegal according to the DWARF standard.  */
3932   if (name == NULL)
3933     {
3934       complaint (&symfile_complaints,
3935                  _("missing name for subprogram DIE at %d"), die->offset);
3936       return;
3937     }
3938
3939   /* Ignore functions with missing or invalid low and high pc attributes.  */
3940   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
3941     {
3942       attr = dwarf2_attr (die, DW_AT_external, cu);
3943       if (!attr || !DW_UNSND (attr))
3944         complaint (&symfile_complaints,
3945                    _("cannot get low and high bounds for subprogram DIE at %d"),
3946                    die->offset);
3947       return;
3948     }
3949
3950   lowpc += baseaddr;
3951   highpc += baseaddr;
3952
3953   /* Record the function range for dwarf_decode_lines.  */
3954   add_to_cu_func_list (name, lowpc, highpc, cu);
3955
3956   new = push_context (0, lowpc);
3957   new->name = new_symbol (die, read_type_die (die, cu), cu);
3958
3959   /* If there is a location expression for DW_AT_frame_base, record
3960      it.  */
3961   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
3962   if (attr)
3963     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3964        expression is being recorded directly in the function's symbol
3965        and not in a separate frame-base object.  I guess this hack is
3966        to avoid adding some sort of frame-base adjunct/annex to the
3967        function's symbol :-(.  The problem with doing this is that it
3968        results in a function symbol with a location expression that
3969        has nothing to do with the location of the function, ouch!  The
3970        relationship should be: a function's symbol has-a frame base; a
3971        frame-base has-a location expression.  */
3972     dwarf2_symbol_mark_computed (attr, new->name, cu);
3973
3974   cu->list_in_scope = &local_symbols;
3975
3976   if (die->child != NULL)
3977     {
3978       child_die = die->child;
3979       while (child_die && child_die->tag)
3980         {
3981           process_die (child_die, cu);
3982           child_die = sibling_die (child_die);
3983         }
3984     }
3985
3986   inherit_abstract_dies (die, cu);
3987
3988   /* If we have a DW_AT_specification, we might need to import using
3989      directives from the context of the specification DIE.  See the
3990      comment in determine_prefix.  */
3991   if (cu->language == language_cplus
3992       && dwarf2_attr (die, DW_AT_specification, cu))
3993     {
3994       struct dwarf2_cu *spec_cu = cu;
3995       struct die_info *spec_die = die_specification (die, &spec_cu);
3996
3997       while (spec_die)
3998         {
3999           child_die = spec_die->child;
4000           while (child_die && child_die->tag)
4001             {
4002               if (child_die->tag == DW_TAG_imported_module)
4003                 process_die (child_die, spec_cu);
4004               child_die = sibling_die (child_die);
4005             }
4006
4007           /* In some cases, GCC generates specification DIEs that
4008              themselves contain DW_AT_specification attributes.  */
4009           spec_die = die_specification (spec_die, &spec_cu);
4010         }
4011     }
4012
4013   new = pop_context ();
4014   /* Make a block for the local symbols within.  */
4015   block = finish_block (new->name, &local_symbols, new->old_blocks,
4016                         lowpc, highpc, objfile);
4017
4018   /* For C++, set the block's scope.  */
4019   if (cu->language == language_cplus || cu->language == language_fortran)
4020     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
4021                         determine_prefix (die, cu),
4022                         processing_has_namespace_info);
4023
4024   /* If we have address ranges, record them.  */
4025   dwarf2_record_block_ranges (die, block, baseaddr, cu);
4026
4027   /* In C++, we can have functions nested inside functions (e.g., when
4028      a function declares a class that has methods).  This means that
4029      when we finish processing a function scope, we may need to go
4030      back to building a containing block's symbol lists.  */
4031   local_symbols = new->locals;
4032   param_symbols = new->params;
4033   using_directives = new->using_directives;
4034
4035   /* If we've finished processing a top-level function, subsequent
4036      symbols go in the file symbol list.  */
4037   if (outermost_context_p ())
4038     cu->list_in_scope = &file_symbols;
4039 }
4040
4041 /* Process all the DIES contained within a lexical block scope.  Start
4042    a new scope, process the dies, and then close the scope.  */
4043
4044 static void
4045 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
4046 {
4047   struct objfile *objfile = cu->objfile;
4048   struct context_stack *new;
4049   CORE_ADDR lowpc, highpc;
4050   struct die_info *child_die;
4051   CORE_ADDR baseaddr;
4052
4053   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4054
4055   /* Ignore blocks with missing or invalid low and high pc attributes.  */
4056   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
4057      as multiple lexical blocks?  Handling children in a sane way would
4058      be nasty.  Might be easier to properly extend generic blocks to
4059      describe ranges.  */
4060   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4061     return;
4062   lowpc += baseaddr;
4063   highpc += baseaddr;
4064
4065   push_context (0, lowpc);
4066   if (die->child != NULL)
4067     {
4068       child_die = die->child;
4069       while (child_die && child_die->tag)
4070         {
4071           process_die (child_die, cu);
4072           child_die = sibling_die (child_die);
4073         }
4074     }
4075   new = pop_context ();
4076
4077   if (local_symbols != NULL || using_directives != NULL)
4078     {
4079       struct block *block
4080         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
4081                         highpc, objfile);
4082
4083       /* Note that recording ranges after traversing children, as we
4084          do here, means that recording a parent's ranges entails
4085          walking across all its children's ranges as they appear in
4086          the address map, which is quadratic behavior.
4087
4088          It would be nicer to record the parent's ranges before
4089          traversing its children, simply overriding whatever you find
4090          there.  But since we don't even decide whether to create a
4091          block until after we've traversed its children, that's hard
4092          to do.  */
4093       dwarf2_record_block_ranges (die, block, baseaddr, cu);
4094     }
4095   local_symbols = new->locals;
4096   using_directives = new->using_directives;
4097 }
4098
4099 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
4100    Return 1 if the attributes are present and valid, otherwise, return 0.
4101    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
4102
4103 static int
4104 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
4105                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
4106                     struct partial_symtab *ranges_pst)
4107 {
4108   struct objfile *objfile = cu->objfile;
4109   struct comp_unit_head *cu_header = &cu->header;
4110   bfd *obfd = objfile->obfd;
4111   unsigned int addr_size = cu_header->addr_size;
4112   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4113   /* Base address selection entry.  */
4114   CORE_ADDR base;
4115   int found_base;
4116   unsigned int dummy;
4117   gdb_byte *buffer;
4118   CORE_ADDR marker;
4119   int low_set;
4120   CORE_ADDR low = 0;
4121   CORE_ADDR high = 0;
4122   CORE_ADDR baseaddr;
4123
4124   found_base = cu->base_known;
4125   base = cu->base_address;
4126
4127   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
4128   if (offset >= dwarf2_per_objfile->ranges.size)
4129     {
4130       complaint (&symfile_complaints,
4131                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
4132                  offset);
4133       return 0;
4134     }
4135   buffer = dwarf2_per_objfile->ranges.buffer + offset;
4136
4137   /* Read in the largest possible address.  */
4138   marker = read_address (obfd, buffer, cu, &dummy);
4139   if ((marker & mask) == mask)
4140     {
4141       /* If we found the largest possible address, then
4142          read the base address.  */
4143       base = read_address (obfd, buffer + addr_size, cu, &dummy);
4144       buffer += 2 * addr_size;
4145       offset += 2 * addr_size;
4146       found_base = 1;
4147     }
4148
4149   low_set = 0;
4150
4151   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4152
4153   while (1)
4154     {
4155       CORE_ADDR range_beginning, range_end;
4156
4157       range_beginning = read_address (obfd, buffer, cu, &dummy);
4158       buffer += addr_size;
4159       range_end = read_address (obfd, buffer, cu, &dummy);
4160       buffer += addr_size;
4161       offset += 2 * addr_size;
4162
4163       /* An end of list marker is a pair of zero addresses.  */
4164       if (range_beginning == 0 && range_end == 0)
4165         /* Found the end of list entry.  */
4166         break;
4167
4168       /* Each base address selection entry is a pair of 2 values.
4169          The first is the largest possible address, the second is
4170          the base address.  Check for a base address here.  */
4171       if ((range_beginning & mask) == mask)
4172         {
4173           /* If we found the largest possible address, then
4174              read the base address.  */
4175           base = read_address (obfd, buffer + addr_size, cu, &dummy);
4176           found_base = 1;
4177           continue;
4178         }
4179
4180       if (!found_base)
4181         {
4182           /* We have no valid base address for the ranges
4183              data.  */
4184           complaint (&symfile_complaints,
4185                      _("Invalid .debug_ranges data (no base address)"));
4186           return 0;
4187         }
4188
4189       range_beginning += base;
4190       range_end += base;
4191
4192       if (ranges_pst != NULL && range_beginning < range_end)
4193         addrmap_set_empty (objfile->psymtabs_addrmap,
4194                            range_beginning + baseaddr, range_end - 1 + baseaddr,
4195                            ranges_pst);
4196
4197       /* FIXME: This is recording everything as a low-high
4198          segment of consecutive addresses.  We should have a
4199          data structure for discontiguous block ranges
4200          instead.  */
4201       if (! low_set)
4202         {
4203           low = range_beginning;
4204           high = range_end;
4205           low_set = 1;
4206         }
4207       else
4208         {
4209           if (range_beginning < low)
4210             low = range_beginning;
4211           if (range_end > high)
4212             high = range_end;
4213         }
4214     }
4215
4216   if (! low_set)
4217     /* If the first entry is an end-of-list marker, the range
4218        describes an empty scope, i.e. no instructions.  */
4219     return 0;
4220
4221   if (low_return)
4222     *low_return = low;
4223   if (high_return)
4224     *high_return = high;
4225   return 1;
4226 }
4227
4228 /* Get low and high pc attributes from a die.  Return 1 if the attributes
4229    are present and valid, otherwise, return 0.  Return -1 if the range is
4230    discontinuous, i.e. derived from DW_AT_ranges information.  */
4231 static int
4232 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
4233                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
4234                       struct partial_symtab *pst)
4235 {
4236   struct attribute *attr;
4237   CORE_ADDR low = 0;
4238   CORE_ADDR high = 0;
4239   int ret = 0;
4240
4241   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4242   if (attr)
4243     {
4244       high = DW_ADDR (attr);
4245       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4246       if (attr)
4247         low = DW_ADDR (attr);
4248       else
4249         /* Found high w/o low attribute.  */
4250         return 0;
4251
4252       /* Found consecutive range of addresses.  */
4253       ret = 1;
4254     }
4255   else
4256     {
4257       attr = dwarf2_attr (die, DW_AT_ranges, cu);
4258       if (attr != NULL)
4259         {
4260           /* Value of the DW_AT_ranges attribute is the offset in the
4261              .debug_ranges section.  */
4262           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
4263             return 0;
4264           /* Found discontinuous range of addresses.  */
4265           ret = -1;
4266         }
4267     }
4268
4269   if (high < low)
4270     return 0;
4271
4272   /* When using the GNU linker, .gnu.linkonce. sections are used to
4273      eliminate duplicate copies of functions and vtables and such.
4274      The linker will arbitrarily choose one and discard the others.
4275      The AT_*_pc values for such functions refer to local labels in
4276      these sections.  If the section from that file was discarded, the
4277      labels are not in the output, so the relocs get a value of 0.
4278      If this is a discarded function, mark the pc bounds as invalid,
4279      so that GDB will ignore it.  */
4280   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
4281     return 0;
4282
4283   *lowpc = low;
4284   *highpc = high;
4285   return ret;
4286 }
4287
4288 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
4289    its low and high PC addresses.  Do nothing if these addresses could not
4290    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
4291    and HIGHPC to the high address if greater than HIGHPC.  */
4292
4293 static void
4294 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
4295                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
4296                                  struct dwarf2_cu *cu)
4297 {
4298   CORE_ADDR low, high;
4299   struct die_info *child = die->child;
4300
4301   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
4302     {
4303       *lowpc = min (*lowpc, low);
4304       *highpc = max (*highpc, high);
4305     }
4306
4307   /* If the language does not allow nested subprograms (either inside
4308      subprograms or lexical blocks), we're done.  */
4309   if (cu->language != language_ada)
4310     return;
4311
4312   /* Check all the children of the given DIE.  If it contains nested
4313      subprograms, then check their pc bounds.  Likewise, we need to
4314      check lexical blocks as well, as they may also contain subprogram
4315      definitions.  */
4316   while (child && child->tag)
4317     {
4318       if (child->tag == DW_TAG_subprogram
4319           || child->tag == DW_TAG_lexical_block)
4320         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
4321       child = sibling_die (child);
4322     }
4323 }
4324
4325 /* Get the low and high pc's represented by the scope DIE, and store
4326    them in *LOWPC and *HIGHPC.  If the correct values can't be
4327    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
4328
4329 static void
4330 get_scope_pc_bounds (struct die_info *die,
4331                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
4332                      struct dwarf2_cu *cu)
4333 {
4334   CORE_ADDR best_low = (CORE_ADDR) -1;
4335   CORE_ADDR best_high = (CORE_ADDR) 0;
4336   CORE_ADDR current_low, current_high;
4337
4338   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
4339     {
4340       best_low = current_low;
4341       best_high = current_high;
4342     }
4343   else
4344     {
4345       struct die_info *child = die->child;
4346
4347       while (child && child->tag)
4348         {
4349           switch (child->tag) {
4350           case DW_TAG_subprogram:
4351             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
4352             break;
4353           case DW_TAG_namespace:
4354           case DW_TAG_module:
4355             /* FIXME: carlton/2004-01-16: Should we do this for
4356                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
4357                that current GCC's always emit the DIEs corresponding
4358                to definitions of methods of classes as children of a
4359                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
4360                the DIEs giving the declarations, which could be
4361                anywhere).  But I don't see any reason why the
4362                standards says that they have to be there.  */
4363             get_scope_pc_bounds (child, &current_low, &current_high, cu);
4364
4365             if (current_low != ((CORE_ADDR) -1))
4366               {
4367                 best_low = min (best_low, current_low);
4368                 best_high = max (best_high, current_high);
4369               }
4370             break;
4371           default:
4372             /* Ignore. */
4373             break;
4374           }
4375
4376           child = sibling_die (child);
4377         }
4378     }
4379
4380   *lowpc = best_low;
4381   *highpc = best_high;
4382 }
4383
4384 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
4385    in DIE.  */
4386 static void
4387 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
4388                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
4389 {
4390   struct attribute *attr;
4391
4392   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4393   if (attr)
4394     {
4395       CORE_ADDR high = DW_ADDR (attr);
4396
4397       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4398       if (attr)
4399         {
4400           CORE_ADDR low = DW_ADDR (attr);
4401
4402           record_block_range (block, baseaddr + low, baseaddr + high - 1);
4403         }
4404     }
4405
4406   attr = dwarf2_attr (die, DW_AT_ranges, cu);
4407   if (attr)
4408     {
4409       bfd *obfd = cu->objfile->obfd;
4410
4411       /* The value of the DW_AT_ranges attribute is the offset of the
4412          address range list in the .debug_ranges section.  */
4413       unsigned long offset = DW_UNSND (attr);
4414       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
4415
4416       /* For some target architectures, but not others, the
4417          read_address function sign-extends the addresses it returns.
4418          To recognize base address selection entries, we need a
4419          mask.  */
4420       unsigned int addr_size = cu->header.addr_size;
4421       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4422
4423       /* The base address, to which the next pair is relative.  Note
4424          that this 'base' is a DWARF concept: most entries in a range
4425          list are relative, to reduce the number of relocs against the
4426          debugging information.  This is separate from this function's
4427          'baseaddr' argument, which GDB uses to relocate debugging
4428          information from a shared library based on the address at
4429          which the library was loaded.  */
4430       CORE_ADDR base = cu->base_address;
4431       int base_known = cu->base_known;
4432
4433       gdb_assert (dwarf2_per_objfile->ranges.readin);
4434       if (offset >= dwarf2_per_objfile->ranges.size)
4435         {
4436           complaint (&symfile_complaints,
4437                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
4438                      offset);
4439           return;
4440         }
4441
4442       for (;;)
4443         {
4444           unsigned int bytes_read;
4445           CORE_ADDR start, end;
4446
4447           start = read_address (obfd, buffer, cu, &bytes_read);
4448           buffer += bytes_read;
4449           end = read_address (obfd, buffer, cu, &bytes_read);
4450           buffer += bytes_read;
4451
4452           /* Did we find the end of the range list?  */
4453           if (start == 0 && end == 0)
4454             break;
4455
4456           /* Did we find a base address selection entry?  */
4457           else if ((start & base_select_mask) == base_select_mask)
4458             {
4459               base = end;
4460               base_known = 1;
4461             }
4462
4463           /* We found an ordinary address range.  */
4464           else
4465             {
4466               if (!base_known)
4467                 {
4468                   complaint (&symfile_complaints,
4469                              _("Invalid .debug_ranges data (no base address)"));
4470                   return;
4471                 }
4472
4473               record_block_range (block,
4474                                   baseaddr + base + start,
4475                                   baseaddr + base + end - 1);
4476             }
4477         }
4478     }
4479 }
4480
4481 /* Add an aggregate field to the field list.  */
4482
4483 static void
4484 dwarf2_add_field (struct field_info *fip, struct die_info *die,
4485                   struct dwarf2_cu *cu)
4486 {
4487   struct objfile *objfile = cu->objfile;
4488   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4489   struct nextfield *new_field;
4490   struct attribute *attr;
4491   struct field *fp;
4492   char *fieldname = "";
4493
4494   /* Allocate a new field list entry and link it in.  */
4495   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
4496   make_cleanup (xfree, new_field);
4497   memset (new_field, 0, sizeof (struct nextfield));
4498
4499   if (die->tag == DW_TAG_inheritance)
4500     {
4501       new_field->next = fip->baseclasses;
4502       fip->baseclasses = new_field;
4503     }
4504   else
4505     {
4506       new_field->next = fip->fields;
4507       fip->fields = new_field;
4508     }
4509   fip->nfields++;
4510
4511   /* Handle accessibility and virtuality of field.
4512      The default accessibility for members is public, the default
4513      accessibility for inheritance is private.  */
4514   if (die->tag != DW_TAG_inheritance)
4515     new_field->accessibility = DW_ACCESS_public;
4516   else
4517     new_field->accessibility = DW_ACCESS_private;
4518   new_field->virtuality = DW_VIRTUALITY_none;
4519
4520   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4521   if (attr)
4522     new_field->accessibility = DW_UNSND (attr);
4523   if (new_field->accessibility != DW_ACCESS_public)
4524     fip->non_public_fields = 1;
4525   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4526   if (attr)
4527     new_field->virtuality = DW_UNSND (attr);
4528
4529   fp = &new_field->field;
4530
4531   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
4532     {
4533       /* Data member other than a C++ static data member.  */
4534
4535       /* Get type of field.  */
4536       fp->type = die_type (die, cu);
4537
4538       SET_FIELD_BITPOS (*fp, 0);
4539
4540       /* Get bit size of field (zero if none).  */
4541       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
4542       if (attr)
4543         {
4544           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
4545         }
4546       else
4547         {
4548           FIELD_BITSIZE (*fp) = 0;
4549         }
4550
4551       /* Get bit offset of field.  */
4552       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4553       if (attr)
4554         {
4555           int byte_offset = 0;
4556
4557           if (attr_form_is_section_offset (attr))
4558             dwarf2_complex_location_expr_complaint ();
4559           else if (attr_form_is_constant (attr))
4560             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4561           else if (attr_form_is_block (attr))
4562             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4563           else
4564             dwarf2_complex_location_expr_complaint ();
4565
4566           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4567         }
4568       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
4569       if (attr)
4570         {
4571           if (gdbarch_bits_big_endian (gdbarch))
4572             {
4573               /* For big endian bits, the DW_AT_bit_offset gives the
4574                  additional bit offset from the MSB of the containing
4575                  anonymous object to the MSB of the field.  We don't
4576                  have to do anything special since we don't need to
4577                  know the size of the anonymous object.  */
4578               FIELD_BITPOS (*fp) += DW_UNSND (attr);
4579             }
4580           else
4581             {
4582               /* For little endian bits, compute the bit offset to the
4583                  MSB of the anonymous object, subtract off the number of
4584                  bits from the MSB of the field to the MSB of the
4585                  object, and then subtract off the number of bits of
4586                  the field itself.  The result is the bit offset of
4587                  the LSB of the field.  */
4588               int anonymous_size;
4589               int bit_offset = DW_UNSND (attr);
4590
4591               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4592               if (attr)
4593                 {
4594                   /* The size of the anonymous object containing
4595                      the bit field is explicit, so use the
4596                      indicated size (in bytes).  */
4597                   anonymous_size = DW_UNSND (attr);
4598                 }
4599               else
4600                 {
4601                   /* The size of the anonymous object containing
4602                      the bit field must be inferred from the type
4603                      attribute of the data member containing the
4604                      bit field.  */
4605                   anonymous_size = TYPE_LENGTH (fp->type);
4606                 }
4607               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4608                 - bit_offset - FIELD_BITSIZE (*fp);
4609             }
4610         }
4611
4612       /* Get name of field.  */
4613       fieldname = dwarf2_name (die, cu);
4614       if (fieldname == NULL)
4615         fieldname = "";
4616
4617       /* The name is already allocated along with this objfile, so we don't
4618          need to duplicate it for the type.  */
4619       fp->name = fieldname;
4620
4621       /* Change accessibility for artificial fields (e.g. virtual table
4622          pointer or virtual base class pointer) to private.  */
4623       if (dwarf2_attr (die, DW_AT_artificial, cu))
4624         {
4625           FIELD_ARTIFICIAL (*fp) = 1;
4626           new_field->accessibility = DW_ACCESS_private;
4627           fip->non_public_fields = 1;
4628         }
4629     }
4630   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
4631     {
4632       /* C++ static member.  */
4633
4634       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4635          is a declaration, but all versions of G++ as of this writing
4636          (so through at least 3.2.1) incorrectly generate
4637          DW_TAG_variable tags.  */
4638
4639       char *physname;
4640
4641       /* Get name of field.  */
4642       fieldname = dwarf2_name (die, cu);
4643       if (fieldname == NULL)
4644         return;
4645
4646       /* Get physical name.  */
4647       physname = (char *) dwarf2_physname (fieldname, die, cu);
4648
4649       /* The name is already allocated along with this objfile, so we don't
4650          need to duplicate it for the type.  */
4651       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
4652       FIELD_TYPE (*fp) = die_type (die, cu);
4653       FIELD_NAME (*fp) = fieldname;
4654     }
4655   else if (die->tag == DW_TAG_inheritance)
4656     {
4657       /* C++ base class field.  */
4658       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4659       if (attr)
4660         {
4661           int byte_offset = 0;
4662
4663           if (attr_form_is_section_offset (attr))
4664             dwarf2_complex_location_expr_complaint ();
4665           else if (attr_form_is_constant (attr))
4666             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4667           else if (attr_form_is_block (attr))
4668             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4669           else
4670             dwarf2_complex_location_expr_complaint ();
4671
4672           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4673         }
4674       FIELD_BITSIZE (*fp) = 0;
4675       FIELD_TYPE (*fp) = die_type (die, cu);
4676       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4677       fip->nbaseclasses++;
4678     }
4679 }
4680
4681 /* Add a typedef defined in the scope of the FIP's class.  */
4682
4683 static void
4684 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
4685                     struct dwarf2_cu *cu)
4686 {
4687   struct objfile *objfile = cu->objfile;
4688   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4689   struct typedef_field_list *new_field;
4690   struct attribute *attr;
4691   struct typedef_field *fp;
4692   char *fieldname = "";
4693
4694   /* Allocate a new field list entry and link it in.  */
4695   new_field = xzalloc (sizeof (*new_field));
4696   make_cleanup (xfree, new_field);
4697
4698   gdb_assert (die->tag == DW_TAG_typedef);
4699
4700   fp = &new_field->field;
4701
4702   /* Get name of field.  */
4703   fp->name = dwarf2_name (die, cu);
4704   if (fp->name == NULL)
4705     return;
4706
4707   fp->type = read_type_die (die, cu);
4708
4709   new_field->next = fip->typedef_field_list;
4710   fip->typedef_field_list = new_field;
4711   fip->typedef_field_list_count++;
4712 }
4713
4714 /* Create the vector of fields, and attach it to the type.  */
4715
4716 static void
4717 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
4718                               struct dwarf2_cu *cu)
4719 {
4720   int nfields = fip->nfields;
4721
4722   /* Record the field count, allocate space for the array of fields,
4723      and create blank accessibility bitfields if necessary.  */
4724   TYPE_NFIELDS (type) = nfields;
4725   TYPE_FIELDS (type) = (struct field *)
4726     TYPE_ALLOC (type, sizeof (struct field) * nfields);
4727   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4728
4729   if (fip->non_public_fields && cu->language != language_ada)
4730     {
4731       ALLOCATE_CPLUS_STRUCT_TYPE (type);
4732
4733       TYPE_FIELD_PRIVATE_BITS (type) =
4734         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4735       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4736
4737       TYPE_FIELD_PROTECTED_BITS (type) =
4738         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4739       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4740
4741       TYPE_FIELD_IGNORE_BITS (type) =
4742         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4743       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4744     }
4745
4746   /* If the type has baseclasses, allocate and clear a bit vector for
4747      TYPE_FIELD_VIRTUAL_BITS.  */
4748   if (fip->nbaseclasses && cu->language != language_ada)
4749     {
4750       int num_bytes = B_BYTES (fip->nbaseclasses);
4751       unsigned char *pointer;
4752
4753       ALLOCATE_CPLUS_STRUCT_TYPE (type);
4754       pointer = TYPE_ALLOC (type, num_bytes);
4755       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
4756       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4757       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4758     }
4759
4760   /* Copy the saved-up fields into the field vector.  Start from the head
4761      of the list, adding to the tail of the field array, so that they end
4762      up in the same order in the array in which they were added to the list.  */
4763   while (nfields-- > 0)
4764     {
4765       struct nextfield *fieldp;
4766
4767       if (fip->fields)
4768         {
4769           fieldp = fip->fields;
4770           fip->fields = fieldp->next;
4771         }
4772       else
4773         {
4774           fieldp = fip->baseclasses;
4775           fip->baseclasses = fieldp->next;
4776         }
4777
4778       TYPE_FIELD (type, nfields) = fieldp->field;
4779       switch (fieldp->accessibility)
4780         {
4781         case DW_ACCESS_private:
4782           if (cu->language != language_ada)
4783             SET_TYPE_FIELD_PRIVATE (type, nfields);
4784           break;
4785
4786         case DW_ACCESS_protected:
4787           if (cu->language != language_ada)
4788             SET_TYPE_FIELD_PROTECTED (type, nfields);
4789           break;
4790
4791         case DW_ACCESS_public:
4792           break;
4793
4794         default:
4795           /* Unknown accessibility.  Complain and treat it as public.  */
4796           {
4797             complaint (&symfile_complaints, _("unsupported accessibility %d"),
4798                        fieldp->accessibility);
4799           }
4800           break;
4801         }
4802       if (nfields < fip->nbaseclasses)
4803         {
4804           switch (fieldp->virtuality)
4805             {
4806             case DW_VIRTUALITY_virtual:
4807             case DW_VIRTUALITY_pure_virtual:
4808               if (cu->language == language_ada)
4809                 error ("unexpected virtuality in component of Ada type");
4810               SET_TYPE_FIELD_VIRTUAL (type, nfields);
4811               break;
4812             }
4813         }
4814     }
4815 }
4816
4817 /* Add a member function to the proper fieldlist.  */
4818
4819 static void
4820 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
4821                       struct type *type, struct dwarf2_cu *cu)
4822 {
4823   struct objfile *objfile = cu->objfile;
4824   struct attribute *attr;
4825   struct fnfieldlist *flp;
4826   int i;
4827   struct fn_field *fnp;
4828   char *fieldname;
4829   char *physname;
4830   struct nextfnfield *new_fnfield;
4831   struct type *this_type;
4832
4833   if (cu->language == language_ada)
4834     error ("unexpected member function in Ada type");
4835
4836   /* Get name of member function.  */
4837   fieldname = dwarf2_name (die, cu);
4838   if (fieldname == NULL)
4839     return;
4840
4841   /* Get the mangled name.  */
4842   physname = (char *) dwarf2_physname (fieldname, die, cu);
4843
4844   /* Look up member function name in fieldlist.  */
4845   for (i = 0; i < fip->nfnfields; i++)
4846     {
4847       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
4848         break;
4849     }
4850
4851   /* Create new list element if necessary.  */
4852   if (i < fip->nfnfields)
4853     flp = &fip->fnfieldlists[i];
4854   else
4855     {
4856       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4857         {
4858           fip->fnfieldlists = (struct fnfieldlist *)
4859             xrealloc (fip->fnfieldlists,
4860                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
4861                       * sizeof (struct fnfieldlist));
4862           if (fip->nfnfields == 0)
4863             make_cleanup (free_current_contents, &fip->fnfieldlists);
4864         }
4865       flp = &fip->fnfieldlists[fip->nfnfields];
4866       flp->name = fieldname;
4867       flp->length = 0;
4868       flp->head = NULL;
4869       fip->nfnfields++;
4870     }
4871
4872   /* Create a new member function field and chain it to the field list
4873      entry. */
4874   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
4875   make_cleanup (xfree, new_fnfield);
4876   memset (new_fnfield, 0, sizeof (struct nextfnfield));
4877   new_fnfield->next = flp->head;
4878   flp->head = new_fnfield;
4879   flp->length++;
4880
4881   /* Fill in the member function field info.  */
4882   fnp = &new_fnfield->fnfield;
4883   /* The name is already allocated along with this objfile, so we don't
4884      need to duplicate it for the type.  */
4885   fnp->physname = physname ? physname : "";
4886   fnp->type = alloc_type (objfile);
4887   this_type = read_type_die (die, cu);
4888   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
4889     {
4890       int nparams = TYPE_NFIELDS (this_type);
4891
4892       /* TYPE is the domain of this method, and THIS_TYPE is the type
4893            of the method itself (TYPE_CODE_METHOD).  */
4894       smash_to_method_type (fnp->type, type,
4895                             TYPE_TARGET_TYPE (this_type),
4896                             TYPE_FIELDS (this_type),
4897                             TYPE_NFIELDS (this_type),
4898                             TYPE_VARARGS (this_type));
4899
4900       /* Handle static member functions.
4901          Dwarf2 has no clean way to discern C++ static and non-static
4902          member functions. G++ helps GDB by marking the first
4903          parameter for non-static member functions (which is the
4904          this pointer) as artificial. We obtain this information
4905          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
4906       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
4907         fnp->voffset = VOFFSET_STATIC;
4908     }
4909   else
4910     complaint (&symfile_complaints, _("member function type missing for '%s'"),
4911                physname);
4912
4913   /* Get fcontext from DW_AT_containing_type if present.  */
4914   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
4915     fnp->fcontext = die_containing_type (die, cu);
4916
4917   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4918      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
4919
4920   /* Get accessibility.  */
4921   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4922   if (attr)
4923     {
4924       switch (DW_UNSND (attr))
4925         {
4926         case DW_ACCESS_private:
4927           fnp->is_private = 1;
4928           break;
4929         case DW_ACCESS_protected:
4930           fnp->is_protected = 1;
4931           break;
4932         }
4933     }
4934
4935   /* Check for artificial methods.  */
4936   attr = dwarf2_attr (die, DW_AT_artificial, cu);
4937   if (attr && DW_UNSND (attr) != 0)
4938     fnp->is_artificial = 1;
4939
4940   /* Get index in virtual function table if it is a virtual member
4941      function.  For older versions of GCC, this is an offset in the
4942      appropriate virtual table, as specified by DW_AT_containing_type.
4943      For everyone else, it is an expression to be evaluated relative
4944      to the object address.  */
4945
4946   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
4947   if (attr)
4948     {
4949       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
4950         {
4951           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
4952             {
4953               /* Old-style GCC.  */
4954               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
4955             }
4956           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
4957                    || (DW_BLOCK (attr)->size > 1
4958                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
4959                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
4960             {
4961               struct dwarf_block blk;
4962               int offset;
4963
4964               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
4965                         ? 1 : 2);
4966               blk.size = DW_BLOCK (attr)->size - offset;
4967               blk.data = DW_BLOCK (attr)->data + offset;
4968               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
4969               if ((fnp->voffset % cu->header.addr_size) != 0)
4970                 dwarf2_complex_location_expr_complaint ();
4971               else
4972                 fnp->voffset /= cu->header.addr_size;
4973               fnp->voffset += 2;
4974             }
4975           else
4976             dwarf2_complex_location_expr_complaint ();
4977
4978           if (!fnp->fcontext)
4979             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
4980         }
4981       else if (attr_form_is_section_offset (attr))
4982         {
4983           dwarf2_complex_location_expr_complaint ();
4984         }
4985       else
4986         {
4987           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4988                                                  fieldname);
4989         }
4990     }
4991   else
4992     {
4993       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4994       if (attr && DW_UNSND (attr))
4995         {
4996           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
4997           complaint (&symfile_complaints,
4998                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
4999                      fieldname, die->offset);
5000           ALLOCATE_CPLUS_STRUCT_TYPE (type);
5001           TYPE_CPLUS_DYNAMIC (type) = 1;
5002         }
5003     }
5004 }
5005
5006 /* Create the vector of member function fields, and attach it to the type.  */
5007
5008 static void
5009 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
5010                                  struct dwarf2_cu *cu)
5011 {
5012   struct fnfieldlist *flp;
5013   int total_length = 0;
5014   int i;
5015
5016   if (cu->language == language_ada)
5017     error ("unexpected member functions in Ada type");
5018
5019   ALLOCATE_CPLUS_STRUCT_TYPE (type);
5020   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
5021     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
5022
5023   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
5024     {
5025       struct nextfnfield *nfp = flp->head;
5026       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
5027       int k;
5028
5029       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
5030       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
5031       fn_flp->fn_fields = (struct fn_field *)
5032         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
5033       for (k = flp->length; (k--, nfp); nfp = nfp->next)
5034         fn_flp->fn_fields[k] = nfp->fnfield;
5035
5036       total_length += flp->length;
5037     }
5038
5039   TYPE_NFN_FIELDS (type) = fip->nfnfields;
5040   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
5041 }
5042
5043 /* Returns non-zero if NAME is the name of a vtable member in CU's
5044    language, zero otherwise.  */
5045 static int
5046 is_vtable_name (const char *name, struct dwarf2_cu *cu)
5047 {
5048   static const char vptr[] = "_vptr";
5049   static const char vtable[] = "vtable";
5050
5051   /* Look for the C++ and Java forms of the vtable.  */
5052   if ((cu->language == language_java
5053        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
5054        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
5055        && is_cplus_marker (name[sizeof (vptr) - 1])))
5056     return 1;
5057
5058   return 0;
5059 }
5060
5061 /* GCC outputs unnamed structures that are really pointers to member
5062    functions, with the ABI-specified layout.  If TYPE describes
5063    such a structure, smash it into a member function type.
5064
5065    GCC shouldn't do this; it should just output pointer to member DIEs.
5066    This is GCC PR debug/28767.  */
5067
5068 static void
5069 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
5070 {
5071   struct type *pfn_type, *domain_type, *new_type;
5072
5073   /* Check for a structure with no name and two children.  */
5074   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
5075     return;
5076
5077   /* Check for __pfn and __delta members.  */
5078   if (TYPE_FIELD_NAME (type, 0) == NULL
5079       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
5080       || TYPE_FIELD_NAME (type, 1) == NULL
5081       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
5082     return;
5083
5084   /* Find the type of the method.  */
5085   pfn_type = TYPE_FIELD_TYPE (type, 0);
5086   if (pfn_type == NULL
5087       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
5088       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
5089     return;
5090
5091   /* Look for the "this" argument.  */
5092   pfn_type = TYPE_TARGET_TYPE (pfn_type);
5093   if (TYPE_NFIELDS (pfn_type) == 0
5094       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
5095       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
5096     return;
5097
5098   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
5099   new_type = alloc_type (objfile);
5100   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
5101                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
5102                         TYPE_VARARGS (pfn_type));
5103   smash_to_methodptr_type (type, new_type);
5104 }
5105
5106 /* Called when we find the DIE that starts a structure or union scope
5107    (definition) to process all dies that define the members of the
5108    structure or union.
5109
5110    NOTE: we need to call struct_type regardless of whether or not the
5111    DIE has an at_name attribute, since it might be an anonymous
5112    structure or union.  This gets the type entered into our set of
5113    user defined types.
5114
5115    However, if the structure is incomplete (an opaque struct/union)
5116    then suppress creating a symbol table entry for it since gdb only
5117    wants to find the one with the complete definition.  Note that if
5118    it is complete, we just call new_symbol, which does it's own
5119    checking about whether the struct/union is anonymous or not (and
5120    suppresses creating a symbol table entry itself).  */
5121
5122 static struct type *
5123 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
5124 {
5125   struct objfile *objfile = cu->objfile;
5126   struct type *type;
5127   struct attribute *attr;
5128   char *name;
5129   struct cleanup *back_to;
5130
5131   /* If the definition of this type lives in .debug_types, read that type.
5132      Don't follow DW_AT_specification though, that will take us back up
5133      the chain and we want to go down.  */
5134   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5135   if (attr)
5136     {
5137       struct dwarf2_cu *type_cu = cu;
5138       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5139
5140       /* We could just recurse on read_structure_type, but we need to call
5141          get_die_type to ensure only one type for this DIE is created.
5142          This is important, for example, because for c++ classes we need
5143          TYPE_NAME set which is only done by new_symbol.  Blech.  */
5144       type = read_type_die (type_die, type_cu);
5145       return set_die_type (die, type, cu);
5146     }
5147
5148   back_to = make_cleanup (null_cleanup, 0);
5149
5150   type = alloc_type (objfile);
5151   INIT_CPLUS_SPECIFIC (type);
5152
5153   name = dwarf2_name (die, cu);
5154   if (name != NULL)
5155     {
5156       if (cu->language == language_cplus
5157           || cu->language == language_java)
5158         {
5159           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
5160           if (die->tag == DW_TAG_structure_type
5161               || die->tag == DW_TAG_class_type)
5162             TYPE_NAME (type) = TYPE_TAG_NAME (type);
5163         }
5164       else
5165         {
5166           /* The name is already allocated along with this objfile, so
5167              we don't need to duplicate it for the type.  */
5168           TYPE_TAG_NAME (type) = (char *) name;
5169           if (die->tag == DW_TAG_class_type)
5170             TYPE_NAME (type) = TYPE_TAG_NAME (type);
5171         }
5172     }
5173
5174   if (die->tag == DW_TAG_structure_type)
5175     {
5176       TYPE_CODE (type) = TYPE_CODE_STRUCT;
5177     }
5178   else if (die->tag == DW_TAG_union_type)
5179     {
5180       TYPE_CODE (type) = TYPE_CODE_UNION;
5181     }
5182   else
5183     {
5184       TYPE_CODE (type) = TYPE_CODE_CLASS;
5185     }
5186
5187   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
5188     TYPE_DECLARED_CLASS (type) = 1;
5189
5190   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5191   if (attr)
5192     {
5193       TYPE_LENGTH (type) = DW_UNSND (attr);
5194     }
5195   else
5196     {
5197       TYPE_LENGTH (type) = 0;
5198     }
5199
5200   TYPE_STUB_SUPPORTED (type) = 1;
5201   if (die_is_declaration (die, cu))
5202     TYPE_STUB (type) = 1;
5203   else if (attr == NULL && die->child == NULL
5204            && producer_is_realview (cu->producer))
5205     /* RealView does not output the required DW_AT_declaration
5206        on incomplete types.  */
5207     TYPE_STUB (type) = 1;
5208
5209   /* We need to add the type field to the die immediately so we don't
5210      infinitely recurse when dealing with pointers to the structure
5211      type within the structure itself. */
5212   set_die_type (die, type, cu);
5213
5214   /* set_die_type should be already done.  */
5215   set_descriptive_type (type, die, cu);
5216
5217   if (die->child != NULL && ! die_is_declaration (die, cu))
5218     {
5219       struct field_info fi;
5220       struct die_info *child_die;
5221
5222       memset (&fi, 0, sizeof (struct field_info));
5223
5224       child_die = die->child;
5225
5226       while (child_die && child_die->tag)
5227         {
5228           if (child_die->tag == DW_TAG_member
5229               || child_die->tag == DW_TAG_variable)
5230             {
5231               /* NOTE: carlton/2002-11-05: A C++ static data member
5232                  should be a DW_TAG_member that is a declaration, but
5233                  all versions of G++ as of this writing (so through at
5234                  least 3.2.1) incorrectly generate DW_TAG_variable
5235                  tags for them instead.  */
5236               dwarf2_add_field (&fi, child_die, cu);
5237             }
5238           else if (child_die->tag == DW_TAG_subprogram)
5239             {
5240               /* C++ member function. */
5241               dwarf2_add_member_fn (&fi, child_die, type, cu);
5242             }
5243           else if (child_die->tag == DW_TAG_inheritance)
5244             {
5245               /* C++ base class field.  */
5246               dwarf2_add_field (&fi, child_die, cu);
5247             }
5248           else if (child_die->tag == DW_TAG_typedef)
5249             dwarf2_add_typedef (&fi, child_die, cu);
5250           child_die = sibling_die (child_die);
5251         }
5252
5253       /* Attach fields and member functions to the type.  */
5254       if (fi.nfields)
5255         dwarf2_attach_fields_to_type (&fi, type, cu);
5256       if (fi.nfnfields)
5257         {
5258           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
5259
5260           /* Get the type which refers to the base class (possibly this
5261              class itself) which contains the vtable pointer for the current
5262              class from the DW_AT_containing_type attribute.  This use of
5263              DW_AT_containing_type is a GNU extension.  */
5264
5265           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
5266             {
5267               struct type *t = die_containing_type (die, cu);
5268
5269               TYPE_VPTR_BASETYPE (type) = t;
5270               if (type == t)
5271                 {
5272                   int i;
5273
5274                   /* Our own class provides vtbl ptr.  */
5275                   for (i = TYPE_NFIELDS (t) - 1;
5276                        i >= TYPE_N_BASECLASSES (t);
5277                        --i)
5278                     {
5279                       char *fieldname = TYPE_FIELD_NAME (t, i);
5280
5281                       if (is_vtable_name (fieldname, cu))
5282                         {
5283                           TYPE_VPTR_FIELDNO (type) = i;
5284                           break;
5285                         }
5286                     }
5287
5288                   /* Complain if virtual function table field not found.  */
5289                   if (i < TYPE_N_BASECLASSES (t))
5290                     complaint (&symfile_complaints,
5291                                _("virtual function table pointer not found when defining class '%s'"),
5292                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
5293                                "");
5294                 }
5295               else
5296                 {
5297                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
5298                 }
5299             }
5300           else if (cu->producer
5301                    && strncmp (cu->producer,
5302                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
5303             {
5304               /* The IBM XLC compiler does not provide direct indication
5305                  of the containing type, but the vtable pointer is
5306                  always named __vfp.  */
5307
5308               int i;
5309
5310               for (i = TYPE_NFIELDS (type) - 1;
5311                    i >= TYPE_N_BASECLASSES (type);
5312                    --i)
5313                 {
5314                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
5315                     {
5316                       TYPE_VPTR_FIELDNO (type) = i;
5317                       TYPE_VPTR_BASETYPE (type) = type;
5318                       break;
5319                     }
5320                 }
5321             }
5322         }
5323
5324       /* Copy fi.typedef_field_list linked list elements content into the
5325          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
5326       if (fi.typedef_field_list)
5327         {
5328           int i = fi.typedef_field_list_count;
5329
5330           ALLOCATE_CPLUS_STRUCT_TYPE (type);
5331           TYPE_TYPEDEF_FIELD_ARRAY (type)
5332             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
5333           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
5334
5335           /* Reverse the list order to keep the debug info elements order.  */
5336           while (--i >= 0)
5337             {
5338               struct typedef_field *dest, *src;
5339
5340               dest = &TYPE_TYPEDEF_FIELD (type, i);
5341               src = &fi.typedef_field_list->field;
5342               fi.typedef_field_list = fi.typedef_field_list->next;
5343               *dest = *src;
5344             }
5345         }
5346     }
5347
5348   quirk_gcc_member_function_pointer (type, cu->objfile);
5349
5350   do_cleanups (back_to);
5351   return type;
5352 }
5353
5354 static void
5355 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
5356 {
5357   struct die_info *child_die = die->child;
5358   struct type *this_type;
5359
5360   this_type = get_die_type (die, cu);
5361   if (this_type == NULL)
5362     this_type = read_structure_type (die, cu);
5363
5364   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
5365      snapshots) has been known to create a die giving a declaration
5366      for a class that has, as a child, a die giving a definition for a
5367      nested class.  So we have to process our children even if the
5368      current die is a declaration.  Normally, of course, a declaration
5369      won't have any children at all.  */
5370
5371   while (child_die != NULL && child_die->tag)
5372     {
5373       if (child_die->tag == DW_TAG_member
5374           || child_die->tag == DW_TAG_variable
5375           || child_die->tag == DW_TAG_inheritance)
5376         {
5377           /* Do nothing.  */
5378         }
5379       else
5380         process_die (child_die, cu);
5381
5382       child_die = sibling_die (child_die);
5383     }
5384
5385   /* Do not consider external references.  According to the DWARF standard,
5386      these DIEs are identified by the fact that they have no byte_size
5387      attribute, and a declaration attribute.  */
5388   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
5389       || !die_is_declaration (die, cu))
5390     new_symbol (die, this_type, cu);
5391 }
5392
5393 /* Given a DW_AT_enumeration_type die, set its type.  We do not
5394    complete the type's fields yet, or create any symbols.  */
5395
5396 static struct type *
5397 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
5398 {
5399   struct objfile *objfile = cu->objfile;
5400   struct type *type;
5401   struct attribute *attr;
5402   const char *name;
5403
5404   /* If the definition of this type lives in .debug_types, read that type.
5405      Don't follow DW_AT_specification though, that will take us back up
5406      the chain and we want to go down.  */
5407   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5408   if (attr)
5409     {
5410       struct dwarf2_cu *type_cu = cu;
5411       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5412
5413       type = read_type_die (type_die, type_cu);
5414       return set_die_type (die, type, cu);
5415     }
5416
5417   type = alloc_type (objfile);
5418
5419   TYPE_CODE (type) = TYPE_CODE_ENUM;
5420   name = dwarf2_full_name (NULL, die, cu);
5421   if (name != NULL)
5422     TYPE_TAG_NAME (type) = (char *) name;
5423
5424   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5425   if (attr)
5426     {
5427       TYPE_LENGTH (type) = DW_UNSND (attr);
5428     }
5429   else
5430     {
5431       TYPE_LENGTH (type) = 0;
5432     }
5433
5434   /* The enumeration DIE can be incomplete.  In Ada, any type can be
5435      declared as private in the package spec, and then defined only
5436      inside the package body.  Such types are known as Taft Amendment
5437      Types.  When another package uses such a type, an incomplete DIE
5438      may be generated by the compiler.  */
5439   if (die_is_declaration (die, cu))
5440     TYPE_STUB (type) = 1;
5441
5442   return set_die_type (die, type, cu);
5443 }
5444
5445 /* Given a pointer to a die which begins an enumeration, process all
5446    the dies that define the members of the enumeration, and create the
5447    symbol for the enumeration type.
5448
5449    NOTE: We reverse the order of the element list.  */
5450
5451 static void
5452 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
5453 {
5454   struct die_info *child_die;
5455   struct field *fields;
5456   struct symbol *sym;
5457   int num_fields;
5458   int unsigned_enum = 1;
5459   char *name;
5460   struct type *this_type;
5461
5462   num_fields = 0;
5463   fields = NULL;
5464   this_type = get_die_type (die, cu);
5465   if (this_type == NULL)
5466     this_type = read_enumeration_type (die, cu);
5467   if (die->child != NULL)
5468     {
5469       child_die = die->child;
5470       while (child_die && child_die->tag)
5471         {
5472           if (child_die->tag != DW_TAG_enumerator)
5473             {
5474               process_die (child_die, cu);
5475             }
5476           else
5477             {
5478               name = dwarf2_name (child_die, cu);
5479               if (name)
5480                 {
5481                   sym = new_symbol (child_die, this_type, cu);
5482                   if (SYMBOL_VALUE (sym) < 0)
5483                     unsigned_enum = 0;
5484
5485                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
5486                     {
5487                       fields = (struct field *)
5488                         xrealloc (fields,
5489                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
5490                                   * sizeof (struct field));
5491                     }
5492
5493                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
5494                   FIELD_TYPE (fields[num_fields]) = NULL;
5495                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
5496                   FIELD_BITSIZE (fields[num_fields]) = 0;
5497
5498                   num_fields++;
5499                 }
5500             }
5501
5502           child_die = sibling_die (child_die);
5503         }
5504
5505       if (num_fields)
5506         {
5507           TYPE_NFIELDS (this_type) = num_fields;
5508           TYPE_FIELDS (this_type) = (struct field *)
5509             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
5510           memcpy (TYPE_FIELDS (this_type), fields,
5511                   sizeof (struct field) * num_fields);
5512           xfree (fields);
5513         }
5514       if (unsigned_enum)
5515         TYPE_UNSIGNED (this_type) = 1;
5516     }
5517
5518   new_symbol (die, this_type, cu);
5519 }
5520
5521 /* Extract all information from a DW_TAG_array_type DIE and put it in
5522    the DIE's type field.  For now, this only handles one dimensional
5523    arrays.  */
5524
5525 static struct type *
5526 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
5527 {
5528   struct objfile *objfile = cu->objfile;
5529   struct die_info *child_die;
5530   struct type *type;
5531   struct type *element_type, *range_type, *index_type;
5532   struct type **range_types = NULL;
5533   struct attribute *attr;
5534   int ndim = 0;
5535   struct cleanup *back_to;
5536   char *name;
5537
5538   element_type = die_type (die, cu);
5539
5540   /* The die_type call above may have already set the type for this DIE.  */
5541   type = get_die_type (die, cu);
5542   if (type)
5543     return type;
5544
5545   /* Irix 6.2 native cc creates array types without children for
5546      arrays with unspecified length.  */
5547   if (die->child == NULL)
5548     {
5549       index_type = objfile_type (objfile)->builtin_int;
5550       range_type = create_range_type (NULL, index_type, 0, -1);
5551       type = create_array_type (NULL, element_type, range_type);
5552       return set_die_type (die, type, cu);
5553     }
5554
5555   back_to = make_cleanup (null_cleanup, NULL);
5556   child_die = die->child;
5557   while (child_die && child_die->tag)
5558     {
5559       if (child_die->tag == DW_TAG_subrange_type)
5560         {
5561           struct type *child_type = read_type_die (child_die, cu);
5562
5563           if (child_type != NULL)
5564             {
5565               /* The range type was succesfully read. Save it for
5566                  the array type creation.  */
5567               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
5568                 {
5569                   range_types = (struct type **)
5570                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
5571                               * sizeof (struct type *));
5572                   if (ndim == 0)
5573                     make_cleanup (free_current_contents, &range_types);
5574                 }
5575               range_types[ndim++] = child_type;
5576             }
5577         }
5578       child_die = sibling_die (child_die);
5579     }
5580
5581   /* Dwarf2 dimensions are output from left to right, create the
5582      necessary array types in backwards order.  */
5583
5584   type = element_type;
5585
5586   if (read_array_order (die, cu) == DW_ORD_col_major)
5587     {
5588       int i = 0;
5589
5590       while (i < ndim)
5591         type = create_array_type (NULL, type, range_types[i++]);
5592     }
5593   else
5594     {
5595       while (ndim-- > 0)
5596         type = create_array_type (NULL, type, range_types[ndim]);
5597     }
5598
5599   /* Understand Dwarf2 support for vector types (like they occur on
5600      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
5601      array type.  This is not part of the Dwarf2/3 standard yet, but a
5602      custom vendor extension.  The main difference between a regular
5603      array and the vector variant is that vectors are passed by value
5604      to functions.  */
5605   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
5606   if (attr)
5607     make_vector_type (type);
5608
5609   name = dwarf2_name (die, cu);
5610   if (name)
5611     TYPE_NAME (type) = name;
5612
5613   /* Install the type in the die. */
5614   set_die_type (die, type, cu);
5615
5616   /* set_die_type should be already done.  */
5617   set_descriptive_type (type, die, cu);
5618
5619   do_cleanups (back_to);
5620
5621   return type;
5622 }
5623
5624 static enum dwarf_array_dim_ordering
5625 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
5626 {
5627   struct attribute *attr;
5628
5629   attr = dwarf2_attr (die, DW_AT_ordering, cu);
5630
5631   if (attr) return DW_SND (attr);
5632
5633   /*
5634     GNU F77 is a special case, as at 08/2004 array type info is the
5635     opposite order to the dwarf2 specification, but data is still
5636     laid out as per normal fortran.
5637
5638     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
5639     version checking.
5640   */
5641
5642   if (cu->language == language_fortran
5643       && cu->producer && strstr (cu->producer, "GNU F77"))
5644     {
5645       return DW_ORD_row_major;
5646     }
5647
5648   switch (cu->language_defn->la_array_ordering)
5649     {
5650     case array_column_major:
5651       return DW_ORD_col_major;
5652     case array_row_major:
5653     default:
5654       return DW_ORD_row_major;
5655     };
5656 }
5657
5658 /* Extract all information from a DW_TAG_set_type DIE and put it in
5659    the DIE's type field. */
5660
5661 static struct type *
5662 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
5663 {
5664   struct type *domain_type, *set_type;
5665   struct attribute *attr;
5666
5667   domain_type = die_type (die, cu);
5668
5669   /* The die_type call above may have already set the type for this DIE.  */
5670   set_type = get_die_type (die, cu);
5671   if (set_type)
5672     return set_type;
5673
5674   set_type = create_set_type (NULL, domain_type);
5675
5676   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5677   if (attr)
5678     TYPE_LENGTH (set_type) = DW_UNSND (attr);
5679
5680   return set_die_type (die, set_type, cu);
5681 }
5682
5683 /* First cut: install each common block member as a global variable.  */
5684
5685 static void
5686 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
5687 {
5688   struct die_info *child_die;
5689   struct attribute *attr;
5690   struct symbol *sym;
5691   CORE_ADDR base = (CORE_ADDR) 0;
5692
5693   attr = dwarf2_attr (die, DW_AT_location, cu);
5694   if (attr)
5695     {
5696       /* Support the .debug_loc offsets */
5697       if (attr_form_is_block (attr))
5698         {
5699           base = decode_locdesc (DW_BLOCK (attr), cu);
5700         }
5701       else if (attr_form_is_section_offset (attr))
5702         {
5703           dwarf2_complex_location_expr_complaint ();
5704         }
5705       else
5706         {
5707           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5708                                                  "common block member");
5709         }
5710     }
5711   if (die->child != NULL)
5712     {
5713       child_die = die->child;
5714       while (child_die && child_die->tag)
5715         {
5716           sym = new_symbol (child_die, NULL, cu);
5717           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
5718           if (attr)
5719             {
5720               CORE_ADDR byte_offset = 0;
5721
5722               if (attr_form_is_section_offset (attr))
5723                 dwarf2_complex_location_expr_complaint ();
5724               else if (attr_form_is_constant (attr))
5725                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5726               else if (attr_form_is_block (attr))
5727                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5728               else
5729                 dwarf2_complex_location_expr_complaint ();
5730
5731               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
5732               add_symbol_to_list (sym, &global_symbols);
5733             }
5734           child_die = sibling_die (child_die);
5735         }
5736     }
5737 }
5738
5739 /* Create a type for a C++ namespace.  */
5740
5741 static struct type *
5742 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
5743 {
5744   struct objfile *objfile = cu->objfile;
5745   const char *previous_prefix, *name;
5746   int is_anonymous;
5747   struct type *type;
5748
5749   /* For extensions, reuse the type of the original namespace.  */
5750   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5751     {
5752       struct die_info *ext_die;
5753       struct dwarf2_cu *ext_cu = cu;
5754
5755       ext_die = dwarf2_extension (die, &ext_cu);
5756       type = read_type_die (ext_die, ext_cu);
5757       return set_die_type (die, type, cu);
5758     }
5759
5760   name = namespace_name (die, &is_anonymous, cu);
5761
5762   /* Now build the name of the current namespace.  */
5763
5764   previous_prefix = determine_prefix (die, cu);
5765   if (previous_prefix[0] != '\0')
5766     name = typename_concat (&objfile->objfile_obstack,
5767                             previous_prefix, name, 0, cu);
5768
5769   /* Create the type.  */
5770   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5771                     objfile);
5772   TYPE_NAME (type) = (char *) name;
5773   TYPE_TAG_NAME (type) = TYPE_NAME (type);
5774
5775   return set_die_type (die, type, cu);
5776 }
5777
5778 /* Read a C++ namespace.  */
5779
5780 static void
5781 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5782 {
5783   struct objfile *objfile = cu->objfile;
5784   const char *name;
5785   int is_anonymous;
5786
5787   /* Add a symbol associated to this if we haven't seen the namespace
5788      before.  Also, add a using directive if it's an anonymous
5789      namespace.  */
5790
5791   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5792     {
5793       struct type *type;
5794
5795       type = read_type_die (die, cu);
5796       new_symbol (die, type, cu);
5797
5798       name = namespace_name (die, &is_anonymous, cu);
5799       if (is_anonymous)
5800         {
5801           const char *previous_prefix = determine_prefix (die, cu);
5802
5803           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
5804                                   NULL, &objfile->objfile_obstack);
5805         }
5806     }
5807
5808   if (die->child != NULL)
5809     {
5810       struct die_info *child_die = die->child;
5811
5812       while (child_die && child_die->tag)
5813         {
5814           process_die (child_die, cu);
5815           child_die = sibling_die (child_die);
5816         }
5817     }
5818 }
5819
5820 /* Read a Fortran module as type.  This DIE can be only a declaration used for
5821    imported module.  Still we need that type as local Fortran "use ... only"
5822    declaration imports depend on the created type in determine_prefix.  */
5823
5824 static struct type *
5825 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
5826 {
5827   struct objfile *objfile = cu->objfile;
5828   char *module_name;
5829   struct type *type;
5830
5831   module_name = dwarf2_name (die, cu);
5832   if (!module_name)
5833     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
5834                die->offset);
5835   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
5836
5837   /* determine_prefix uses TYPE_TAG_NAME.  */
5838   TYPE_TAG_NAME (type) = TYPE_NAME (type);
5839
5840   return set_die_type (die, type, cu);
5841 }
5842
5843 /* Read a Fortran module.  */
5844
5845 static void
5846 read_module (struct die_info *die, struct dwarf2_cu *cu)
5847 {
5848   struct die_info *child_die = die->child;
5849
5850   while (child_die && child_die->tag)
5851     {
5852       process_die (child_die, cu);
5853       child_die = sibling_die (child_die);
5854     }
5855 }
5856
5857 /* Return the name of the namespace represented by DIE.  Set
5858    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5859    namespace.  */
5860
5861 static const char *
5862 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
5863 {
5864   struct die_info *current_die;
5865   const char *name = NULL;
5866
5867   /* Loop through the extensions until we find a name.  */
5868
5869   for (current_die = die;
5870        current_die != NULL;
5871        current_die = dwarf2_extension (die, &cu))
5872     {
5873       name = dwarf2_name (current_die, cu);
5874       if (name != NULL)
5875         break;
5876     }
5877
5878   /* Is it an anonymous namespace?  */
5879
5880   *is_anonymous = (name == NULL);
5881   if (*is_anonymous)
5882     name = "(anonymous namespace)";
5883
5884   return name;
5885 }
5886
5887 /* Extract all information from a DW_TAG_pointer_type DIE and add to
5888    the user defined type vector.  */
5889
5890 static struct type *
5891 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
5892 {
5893   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
5894   struct comp_unit_head *cu_header = &cu->header;
5895   struct type *type;
5896   struct attribute *attr_byte_size;
5897   struct attribute *attr_address_class;
5898   int byte_size, addr_class;
5899   struct type *target_type;
5900
5901   target_type = die_type (die, cu);
5902
5903   /* The die_type call above may have already set the type for this DIE.  */
5904   type = get_die_type (die, cu);
5905   if (type)
5906     return type;
5907
5908   type = lookup_pointer_type (target_type);
5909
5910   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
5911   if (attr_byte_size)
5912     byte_size = DW_UNSND (attr_byte_size);
5913   else
5914     byte_size = cu_header->addr_size;
5915
5916   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
5917   if (attr_address_class)
5918     addr_class = DW_UNSND (attr_address_class);
5919   else
5920     addr_class = DW_ADDR_none;
5921
5922   /* If the pointer size or address class is different than the
5923      default, create a type variant marked as such and set the
5924      length accordingly.  */
5925   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
5926     {
5927       if (gdbarch_address_class_type_flags_p (gdbarch))
5928         {
5929           int type_flags;
5930
5931           type_flags = gdbarch_address_class_type_flags
5932                          (gdbarch, byte_size, addr_class);
5933           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5934                       == 0);
5935           type = make_type_with_address_space (type, type_flags);
5936         }
5937       else if (TYPE_LENGTH (type) != byte_size)
5938         {
5939           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
5940         }
5941       else
5942         {
5943           /* Should we also complain about unhandled address classes?  */
5944         }
5945     }
5946
5947   TYPE_LENGTH (type) = byte_size;
5948   return set_die_type (die, type, cu);
5949 }
5950
5951 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5952    the user defined type vector.  */
5953
5954 static struct type *
5955 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
5956 {
5957   struct type *type;
5958   struct type *to_type;
5959   struct type *domain;
5960
5961   to_type = die_type (die, cu);
5962   domain = die_containing_type (die, cu);
5963
5964   /* The calls above may have already set the type for this DIE.  */
5965   type = get_die_type (die, cu);
5966   if (type)
5967     return type;
5968
5969   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5970     type = lookup_methodptr_type (to_type);
5971   else
5972     type = lookup_memberptr_type (to_type, domain);
5973
5974   return set_die_type (die, type, cu);
5975 }
5976
5977 /* Extract all information from a DW_TAG_reference_type DIE and add to
5978    the user defined type vector.  */
5979
5980 static struct type *
5981 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
5982 {
5983   struct comp_unit_head *cu_header = &cu->header;
5984   struct type *type, *target_type;
5985   struct attribute *attr;
5986
5987   target_type = die_type (die, cu);
5988
5989   /* The die_type call above may have already set the type for this DIE.  */
5990   type = get_die_type (die, cu);
5991   if (type)
5992     return type;
5993
5994   type = lookup_reference_type (target_type);
5995   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5996   if (attr)
5997     {
5998       TYPE_LENGTH (type) = DW_UNSND (attr);
5999     }
6000   else
6001     {
6002       TYPE_LENGTH (type) = cu_header->addr_size;
6003     }
6004   return set_die_type (die, type, cu);
6005 }
6006
6007 static struct type *
6008 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
6009 {
6010   struct type *base_type, *cv_type;
6011
6012   base_type = die_type (die, cu);
6013
6014   /* The die_type call above may have already set the type for this DIE.  */
6015   cv_type = get_die_type (die, cu);
6016   if (cv_type)
6017     return cv_type;
6018
6019   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
6020   return set_die_type (die, cv_type, cu);
6021 }
6022
6023 static struct type *
6024 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
6025 {
6026   struct type *base_type, *cv_type;
6027
6028   base_type = die_type (die, cu);
6029
6030   /* The die_type call above may have already set the type for this DIE.  */
6031   cv_type = get_die_type (die, cu);
6032   if (cv_type)
6033     return cv_type;
6034
6035   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
6036   return set_die_type (die, cv_type, cu);
6037 }
6038
6039 /* Extract all information from a DW_TAG_string_type DIE and add to
6040    the user defined type vector.  It isn't really a user defined type,
6041    but it behaves like one, with other DIE's using an AT_user_def_type
6042    attribute to reference it.  */
6043
6044 static struct type *
6045 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
6046 {
6047   struct objfile *objfile = cu->objfile;
6048   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6049   struct type *type, *range_type, *index_type, *char_type;
6050   struct attribute *attr;
6051   unsigned int length;
6052
6053   attr = dwarf2_attr (die, DW_AT_string_length, cu);
6054   if (attr)
6055     {
6056       length = DW_UNSND (attr);
6057     }
6058   else
6059     {
6060       /* check for the DW_AT_byte_size attribute */
6061       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6062       if (attr)
6063         {
6064           length = DW_UNSND (attr);
6065         }
6066       else
6067         {
6068           length = 1;
6069         }
6070     }
6071
6072   index_type = objfile_type (objfile)->builtin_int;
6073   range_type = create_range_type (NULL, index_type, 1, length);
6074   char_type = language_string_char_type (cu->language_defn, gdbarch);
6075   type = create_string_type (NULL, char_type, range_type);
6076
6077   return set_die_type (die, type, cu);
6078 }
6079
6080 /* Handle DIES due to C code like:
6081
6082    struct foo
6083    {
6084    int (*funcp)(int a, long l);
6085    int b;
6086    };
6087
6088    ('funcp' generates a DW_TAG_subroutine_type DIE)
6089  */
6090
6091 static struct type *
6092 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
6093 {
6094   struct type *type;            /* Type that this function returns */
6095   struct type *ftype;           /* Function that returns above type */
6096   struct attribute *attr;
6097
6098   type = die_type (die, cu);
6099
6100   /* The die_type call above may have already set the type for this DIE.  */
6101   ftype = get_die_type (die, cu);
6102   if (ftype)
6103     return ftype;
6104
6105   ftype = lookup_function_type (type);
6106
6107   /* All functions in C++, Pascal and Java have prototypes.  */
6108   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
6109   if ((attr && (DW_UNSND (attr) != 0))
6110       || cu->language == language_cplus
6111       || cu->language == language_java
6112       || cu->language == language_pascal)
6113     TYPE_PROTOTYPED (ftype) = 1;
6114   else if (producer_is_realview (cu->producer))
6115     /* RealView does not emit DW_AT_prototyped.  We can not
6116        distinguish prototyped and unprototyped functions; default to
6117        prototyped, since that is more common in modern code (and
6118        RealView warns about unprototyped functions).  */
6119     TYPE_PROTOTYPED (ftype) = 1;
6120
6121   /* Store the calling convention in the type if it's available in
6122      the subroutine die.  Otherwise set the calling convention to
6123      the default value DW_CC_normal.  */
6124   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
6125   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
6126
6127   /* We need to add the subroutine type to the die immediately so
6128      we don't infinitely recurse when dealing with parameters
6129      declared as the same subroutine type. */
6130   set_die_type (die, ftype, cu);
6131
6132   if (die->child != NULL)
6133     {
6134       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
6135       struct die_info *child_die;
6136       int nparams, iparams;
6137
6138       /* Count the number of parameters.
6139          FIXME: GDB currently ignores vararg functions, but knows about
6140          vararg member functions.  */
6141       nparams = 0;
6142       child_die = die->child;
6143       while (child_die && child_die->tag)
6144         {
6145           if (child_die->tag == DW_TAG_formal_parameter)
6146             nparams++;
6147           else if (child_die->tag == DW_TAG_unspecified_parameters)
6148             TYPE_VARARGS (ftype) = 1;
6149           child_die = sibling_die (child_die);
6150         }
6151
6152       /* Allocate storage for parameters and fill them in.  */
6153       TYPE_NFIELDS (ftype) = nparams;
6154       TYPE_FIELDS (ftype) = (struct field *)
6155         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
6156
6157       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
6158          even if we error out during the parameters reading below.  */
6159       for (iparams = 0; iparams < nparams; iparams++)
6160         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
6161
6162       iparams = 0;
6163       child_die = die->child;
6164       while (child_die && child_die->tag)
6165         {
6166           if (child_die->tag == DW_TAG_formal_parameter)
6167             {
6168               /* Dwarf2 has no clean way to discern C++ static and non-static
6169                  member functions. G++ helps GDB by marking the first
6170                  parameter for non-static member functions (which is the
6171                  this pointer) as artificial. We pass this information
6172                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
6173               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
6174               if (attr)
6175                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
6176               else
6177                 {
6178                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
6179
6180                   /* GCC/43521: In java, the formal parameter
6181                      "this" is sometimes not marked with DW_AT_artificial.  */
6182                   if (cu->language == language_java)
6183                     {
6184                       const char *name = dwarf2_name (child_die, cu);
6185
6186                       if (name && !strcmp (name, "this"))
6187                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
6188                     }
6189                 }
6190               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
6191               iparams++;
6192             }
6193           child_die = sibling_die (child_die);
6194         }
6195     }
6196
6197   return ftype;
6198 }
6199
6200 static struct type *
6201 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
6202 {
6203   struct objfile *objfile = cu->objfile;
6204   const char *name = NULL;
6205   struct type *this_type;
6206
6207   name = dwarf2_full_name (NULL, die, cu);
6208   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
6209                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
6210   TYPE_NAME (this_type) = (char *) name;
6211   set_die_type (die, this_type, cu);
6212   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
6213   return this_type;
6214 }
6215
6216 /* Find a representation of a given base type and install
6217    it in the TYPE field of the die.  */
6218
6219 static struct type *
6220 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
6221 {
6222   struct objfile *objfile = cu->objfile;
6223   struct type *type;
6224   struct attribute *attr;
6225   int encoding = 0, size = 0;
6226   char *name;
6227   enum type_code code = TYPE_CODE_INT;
6228   int type_flags = 0;
6229   struct type *target_type = NULL;
6230
6231   attr = dwarf2_attr (die, DW_AT_encoding, cu);
6232   if (attr)
6233     {
6234       encoding = DW_UNSND (attr);
6235     }
6236   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6237   if (attr)
6238     {
6239       size = DW_UNSND (attr);
6240     }
6241   name = dwarf2_name (die, cu);
6242   if (!name)
6243     {
6244       complaint (&symfile_complaints,
6245                  _("DW_AT_name missing from DW_TAG_base_type"));
6246     }
6247
6248   switch (encoding)
6249     {
6250       case DW_ATE_address:
6251         /* Turn DW_ATE_address into a void * pointer.  */
6252         code = TYPE_CODE_PTR;
6253         type_flags |= TYPE_FLAG_UNSIGNED;
6254         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
6255         break;
6256       case DW_ATE_boolean:
6257         code = TYPE_CODE_BOOL;
6258         type_flags |= TYPE_FLAG_UNSIGNED;
6259         break;
6260       case DW_ATE_complex_float:
6261         code = TYPE_CODE_COMPLEX;
6262         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
6263         break;
6264       case DW_ATE_decimal_float:
6265         code = TYPE_CODE_DECFLOAT;
6266         break;
6267       case DW_ATE_float:
6268         code = TYPE_CODE_FLT;
6269         break;
6270       case DW_ATE_signed:
6271         break;
6272       case DW_ATE_unsigned:
6273         type_flags |= TYPE_FLAG_UNSIGNED;
6274         break;
6275       case DW_ATE_signed_char:
6276         if (cu->language == language_ada || cu->language == language_m2
6277             || cu->language == language_pascal)
6278           code = TYPE_CODE_CHAR;
6279         break;
6280       case DW_ATE_unsigned_char:
6281         if (cu->language == language_ada || cu->language == language_m2
6282             || cu->language == language_pascal)
6283           code = TYPE_CODE_CHAR;
6284         type_flags |= TYPE_FLAG_UNSIGNED;
6285         break;
6286       case DW_ATE_UTF:
6287         /* We just treat this as an integer and then recognize the
6288            type by name elsewhere.  */
6289         break;
6290
6291       default:
6292         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
6293                    dwarf_type_encoding_name (encoding));
6294         break;
6295     }
6296
6297   type = init_type (code, size, type_flags, NULL, objfile);
6298   TYPE_NAME (type) = name;
6299   TYPE_TARGET_TYPE (type) = target_type;
6300
6301   if (name && strcmp (name, "char") == 0)
6302     TYPE_NOSIGN (type) = 1;
6303
6304   return set_die_type (die, type, cu);
6305 }
6306
6307 /* Read the given DW_AT_subrange DIE.  */
6308
6309 static struct type *
6310 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
6311 {
6312   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
6313   struct type *base_type;
6314   struct type *range_type;
6315   struct attribute *attr;
6316   LONGEST low = 0;
6317   LONGEST high = -1;
6318   char *name;
6319   LONGEST negative_mask;
6320
6321   base_type = die_type (die, cu);
6322
6323   /* The die_type call above may have already set the type for this DIE.  */
6324   range_type = get_die_type (die, cu);
6325   if (range_type)
6326     return range_type;
6327
6328   if (cu->language == language_fortran)
6329     {
6330       /* FORTRAN implies a lower bound of 1, if not given.  */
6331       low = 1;
6332     }
6333
6334   /* FIXME: For variable sized arrays either of these could be
6335      a variable rather than a constant value.  We'll allow it,
6336      but we don't know how to handle it.  */
6337   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
6338   if (attr)
6339     low = dwarf2_get_attr_constant_value (attr, 0);
6340
6341   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
6342   if (attr)
6343     {
6344       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
6345         {
6346           /* GCC encodes arrays with unspecified or dynamic length
6347              with a DW_FORM_block1 attribute or a reference attribute.
6348              FIXME: GDB does not yet know how to handle dynamic
6349              arrays properly, treat them as arrays with unspecified
6350              length for now.
6351
6352              FIXME: jimb/2003-09-22: GDB does not really know
6353              how to handle arrays of unspecified length
6354              either; we just represent them as zero-length
6355              arrays.  Choose an appropriate upper bound given
6356              the lower bound we've computed above.  */
6357           high = low - 1;
6358         }
6359       else
6360         high = dwarf2_get_attr_constant_value (attr, 1);
6361     }
6362   else
6363     {
6364       attr = dwarf2_attr (die, DW_AT_count, cu);
6365       if (attr)
6366         {
6367           int count = dwarf2_get_attr_constant_value (attr, 1);
6368           high = low + count - 1;
6369         }
6370     }
6371
6372   /* Dwarf-2 specifications explicitly allows to create subrange types
6373      without specifying a base type.
6374      In that case, the base type must be set to the type of
6375      the lower bound, upper bound or count, in that order, if any of these
6376      three attributes references an object that has a type.
6377      If no base type is found, the Dwarf-2 specifications say that
6378      a signed integer type of size equal to the size of an address should
6379      be used.
6380      For the following C code: `extern char gdb_int [];'
6381      GCC produces an empty range DIE.
6382      FIXME: muller/2010-05-28: Possible references to object for low bound,
6383      high bound or count are not yet handled by this code.
6384   */
6385   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
6386     {
6387       struct objfile *objfile = cu->objfile;
6388       struct gdbarch *gdbarch = get_objfile_arch (objfile);
6389       int addr_size = gdbarch_addr_bit (gdbarch) /8;
6390       struct type *int_type = objfile_type (objfile)->builtin_int;
6391
6392       /* Test "int", "long int", and "long long int" objfile types,
6393          and select the first one having a size above or equal to the
6394          architecture address size.  */
6395       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
6396         base_type = int_type;
6397       else
6398         {
6399           int_type = objfile_type (objfile)->builtin_long;
6400           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
6401             base_type = int_type;
6402           else
6403             {
6404               int_type = objfile_type (objfile)->builtin_long_long;
6405               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
6406                 base_type = int_type;
6407             }
6408         }
6409     }
6410
6411   negative_mask =
6412     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
6413   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
6414     low |= negative_mask;
6415   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
6416     high |= negative_mask;
6417
6418   range_type = create_range_type (NULL, base_type, low, high);
6419
6420   /* Mark arrays with dynamic length at least as an array of unspecified
6421      length.  GDB could check the boundary but before it gets implemented at
6422      least allow accessing the array elements.  */
6423   if (attr && attr->form == DW_FORM_block1)
6424     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
6425
6426   name = dwarf2_name (die, cu);
6427   if (name)
6428     TYPE_NAME (range_type) = name;
6429
6430   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6431   if (attr)
6432     TYPE_LENGTH (range_type) = DW_UNSND (attr);
6433
6434   set_die_type (die, range_type, cu);
6435
6436   /* set_die_type should be already done.  */
6437   set_descriptive_type (range_type, die, cu);
6438
6439   return range_type;
6440 }
6441
6442 static struct type *
6443 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
6444 {
6445   struct type *type;
6446
6447   /* For now, we only support the C meaning of an unspecified type: void.  */
6448
6449   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6450   TYPE_NAME (type) = dwarf2_name (die, cu);
6451
6452   return set_die_type (die, type, cu);
6453 }
6454
6455 /* Trivial hash function for die_info: the hash value of a DIE
6456    is its offset in .debug_info for this objfile.  */
6457
6458 static hashval_t
6459 die_hash (const void *item)
6460 {
6461   const struct die_info *die = item;
6462
6463   return die->offset;
6464 }
6465
6466 /* Trivial comparison function for die_info structures: two DIEs
6467    are equal if they have the same offset.  */
6468
6469 static int
6470 die_eq (const void *item_lhs, const void *item_rhs)
6471 {
6472   const struct die_info *die_lhs = item_lhs;
6473   const struct die_info *die_rhs = item_rhs;
6474
6475   return die_lhs->offset == die_rhs->offset;
6476 }
6477
6478 /* Read a whole compilation unit into a linked list of dies.  */
6479
6480 static struct die_info *
6481 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
6482 {
6483   struct die_reader_specs reader_specs;
6484
6485   gdb_assert (cu->die_hash == NULL);
6486   cu->die_hash
6487     = htab_create_alloc_ex (cu->header.length / 12,
6488                             die_hash,
6489                             die_eq,
6490                             NULL,
6491                             &cu->comp_unit_obstack,
6492                             hashtab_obstack_allocate,
6493                             dummy_obstack_deallocate);
6494
6495   init_cu_die_reader (&reader_specs, cu);
6496
6497   return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
6498 }
6499
6500 /* Main entry point for reading a DIE and all children.
6501    Read the DIE and dump it if requested.  */
6502
6503 static struct die_info *
6504 read_die_and_children (const struct die_reader_specs *reader,
6505                        gdb_byte *info_ptr,
6506                        gdb_byte **new_info_ptr,
6507                        struct die_info *parent)
6508 {
6509   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
6510                                                      new_info_ptr, parent);
6511
6512   if (dwarf2_die_debug)
6513     {
6514       fprintf_unfiltered (gdb_stdlog,
6515                           "\nRead die from %s of %s:\n",
6516                           reader->buffer == dwarf2_per_objfile->info.buffer
6517                           ? ".debug_info"
6518                           : reader->buffer == dwarf2_per_objfile->types.buffer
6519                           ? ".debug_types"
6520                           : "unknown section",
6521                           reader->abfd->filename);
6522       dump_die (result, dwarf2_die_debug);
6523     }
6524
6525   return result;
6526 }
6527
6528 /* Read a single die and all its descendents.  Set the die's sibling
6529    field to NULL; set other fields in the die correctly, and set all
6530    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
6531    location of the info_ptr after reading all of those dies.  PARENT
6532    is the parent of the die in question.  */
6533
6534 static struct die_info *
6535 read_die_and_children_1 (const struct die_reader_specs *reader,
6536                          gdb_byte *info_ptr,
6537                          gdb_byte **new_info_ptr,
6538                          struct die_info *parent)
6539 {
6540   struct die_info *die;
6541   gdb_byte *cur_ptr;
6542   int has_children;
6543
6544   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
6545   if (die == NULL)
6546     {
6547       *new_info_ptr = cur_ptr;
6548       return NULL;
6549     }
6550   store_in_ref_table (die, reader->cu);
6551
6552   if (has_children)
6553     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
6554   else
6555     {
6556       die->child = NULL;
6557       *new_info_ptr = cur_ptr;
6558     }
6559
6560   die->sibling = NULL;
6561   die->parent = parent;
6562   return die;
6563 }
6564
6565 /* Read a die, all of its descendents, and all of its siblings; set
6566    all of the fields of all of the dies correctly.  Arguments are as
6567    in read_die_and_children.  */
6568
6569 static struct die_info *
6570 read_die_and_siblings (const struct die_reader_specs *reader,
6571                        gdb_byte *info_ptr,
6572                        gdb_byte **new_info_ptr,
6573                        struct die_info *parent)
6574 {
6575   struct die_info *first_die, *last_sibling;
6576   gdb_byte *cur_ptr;
6577
6578   cur_ptr = info_ptr;
6579   first_die = last_sibling = NULL;
6580
6581   while (1)
6582     {
6583       struct die_info *die
6584         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
6585
6586       if (die == NULL)
6587         {
6588           *new_info_ptr = cur_ptr;
6589           return first_die;
6590         }
6591
6592       if (!first_die)
6593         first_die = die;
6594       else
6595         last_sibling->sibling = die;
6596
6597       last_sibling = die;
6598     }
6599 }
6600
6601 /* Read the die from the .debug_info section buffer.  Set DIEP to
6602    point to a newly allocated die with its information, except for its
6603    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
6604    whether the die has children or not.  */
6605
6606 static gdb_byte *
6607 read_full_die (const struct die_reader_specs *reader,
6608                struct die_info **diep, gdb_byte *info_ptr,
6609                int *has_children)
6610 {
6611   unsigned int abbrev_number, bytes_read, i, offset;
6612   struct abbrev_info *abbrev;
6613   struct die_info *die;
6614   struct dwarf2_cu *cu = reader->cu;
6615   bfd *abfd = reader->abfd;
6616
6617   offset = info_ptr - reader->buffer;
6618   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6619   info_ptr += bytes_read;
6620   if (!abbrev_number)
6621     {
6622       *diep = NULL;
6623       *has_children = 0;
6624       return info_ptr;
6625     }
6626
6627   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6628   if (!abbrev)
6629     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6630            abbrev_number,
6631            bfd_get_filename (abfd));
6632
6633   die = dwarf_alloc_die (cu, abbrev->num_attrs);
6634   die->offset = offset;
6635   die->tag = abbrev->tag;
6636   die->abbrev = abbrev_number;
6637
6638   die->num_attrs = abbrev->num_attrs;
6639
6640   for (i = 0; i < abbrev->num_attrs; ++i)
6641     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6642                                abfd, info_ptr, cu);
6643
6644   *diep = die;
6645   *has_children = abbrev->has_children;
6646   return info_ptr;
6647 }
6648
6649 /* In DWARF version 2, the description of the debugging information is
6650    stored in a separate .debug_abbrev section.  Before we read any
6651    dies from a section we read in all abbreviations and install them
6652    in a hash table.  This function also sets flags in CU describing
6653    the data found in the abbrev table.  */
6654
6655 static void
6656 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
6657 {
6658   struct comp_unit_head *cu_header = &cu->header;
6659   gdb_byte *abbrev_ptr;
6660   struct abbrev_info *cur_abbrev;
6661   unsigned int abbrev_number, bytes_read, abbrev_name;
6662   unsigned int abbrev_form, hash_number;
6663   struct attr_abbrev *cur_attrs;
6664   unsigned int allocated_attrs;
6665
6666   /* Initialize dwarf2 abbrevs */
6667   obstack_init (&cu->abbrev_obstack);
6668   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6669                                       (ABBREV_HASH_SIZE
6670                                        * sizeof (struct abbrev_info *)));
6671   memset (cu->dwarf2_abbrevs, 0,
6672           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
6673
6674   dwarf2_read_section (dwarf2_per_objfile->objfile,
6675                        &dwarf2_per_objfile->abbrev);
6676   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
6677   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6678   abbrev_ptr += bytes_read;
6679
6680   allocated_attrs = ATTR_ALLOC_CHUNK;
6681   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6682
6683   /* loop until we reach an abbrev number of 0 */
6684   while (abbrev_number)
6685     {
6686       cur_abbrev = dwarf_alloc_abbrev (cu);
6687
6688       /* read in abbrev header */
6689       cur_abbrev->number = abbrev_number;
6690       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6691       abbrev_ptr += bytes_read;
6692       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6693       abbrev_ptr += 1;
6694
6695       if (cur_abbrev->tag == DW_TAG_namespace)
6696         cu->has_namespace_info = 1;
6697
6698       /* now read in declarations */
6699       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6700       abbrev_ptr += bytes_read;
6701       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6702       abbrev_ptr += bytes_read;
6703       while (abbrev_name)
6704         {
6705           if (cur_abbrev->num_attrs == allocated_attrs)
6706             {
6707               allocated_attrs += ATTR_ALLOC_CHUNK;
6708               cur_attrs
6709                 = xrealloc (cur_attrs, (allocated_attrs
6710                                         * sizeof (struct attr_abbrev)));
6711             }
6712
6713           /* Record whether this compilation unit might have
6714              inter-compilation-unit references.  If we don't know what form
6715              this attribute will have, then it might potentially be a
6716              DW_FORM_ref_addr, so we conservatively expect inter-CU
6717              references.  */
6718
6719           if (abbrev_form == DW_FORM_ref_addr
6720               || abbrev_form == DW_FORM_indirect)
6721             cu->has_form_ref_addr = 1;
6722
6723           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6724           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
6725           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6726           abbrev_ptr += bytes_read;
6727           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6728           abbrev_ptr += bytes_read;
6729         }
6730
6731       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6732                                          (cur_abbrev->num_attrs
6733                                           * sizeof (struct attr_abbrev)));
6734       memcpy (cur_abbrev->attrs, cur_attrs,
6735               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6736
6737       hash_number = abbrev_number % ABBREV_HASH_SIZE;
6738       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6739       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
6740
6741       /* Get next abbreviation.
6742          Under Irix6 the abbreviations for a compilation unit are not
6743          always properly terminated with an abbrev number of 0.
6744          Exit loop if we encounter an abbreviation which we have
6745          already read (which means we are about to read the abbreviations
6746          for the next compile unit) or if the end of the abbreviation
6747          table is reached.  */
6748       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6749           >= dwarf2_per_objfile->abbrev.size)
6750         break;
6751       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6752       abbrev_ptr += bytes_read;
6753       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
6754         break;
6755     }
6756
6757   xfree (cur_attrs);
6758 }
6759
6760 /* Release the memory used by the abbrev table for a compilation unit.  */
6761
6762 static void
6763 dwarf2_free_abbrev_table (void *ptr_to_cu)
6764 {
6765   struct dwarf2_cu *cu = ptr_to_cu;
6766
6767   obstack_free (&cu->abbrev_obstack, NULL);
6768   cu->dwarf2_abbrevs = NULL;
6769 }
6770
6771 /* Lookup an abbrev_info structure in the abbrev hash table.  */
6772
6773 static struct abbrev_info *
6774 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
6775 {
6776   unsigned int hash_number;
6777   struct abbrev_info *abbrev;
6778
6779   hash_number = number % ABBREV_HASH_SIZE;
6780   abbrev = cu->dwarf2_abbrevs[hash_number];
6781
6782   while (abbrev)
6783     {
6784       if (abbrev->number == number)
6785         return abbrev;
6786       else
6787         abbrev = abbrev->next;
6788     }
6789   return NULL;
6790 }
6791
6792 /* Returns nonzero if TAG represents a type that we might generate a partial
6793    symbol for.  */
6794
6795 static int
6796 is_type_tag_for_partial (int tag)
6797 {
6798   switch (tag)
6799     {
6800 #if 0
6801     /* Some types that would be reasonable to generate partial symbols for,
6802        that we don't at present.  */
6803     case DW_TAG_array_type:
6804     case DW_TAG_file_type:
6805     case DW_TAG_ptr_to_member_type:
6806     case DW_TAG_set_type:
6807     case DW_TAG_string_type:
6808     case DW_TAG_subroutine_type:
6809 #endif
6810     case DW_TAG_base_type:
6811     case DW_TAG_class_type:
6812     case DW_TAG_interface_type:
6813     case DW_TAG_enumeration_type:
6814     case DW_TAG_structure_type:
6815     case DW_TAG_subrange_type:
6816     case DW_TAG_typedef:
6817     case DW_TAG_union_type:
6818       return 1;
6819     default:
6820       return 0;
6821     }
6822 }
6823
6824 /* Load all DIEs that are interesting for partial symbols into memory.  */
6825
6826 static struct partial_die_info *
6827 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6828                    int building_psymtab, struct dwarf2_cu *cu)
6829 {
6830   struct partial_die_info *part_die;
6831   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6832   struct abbrev_info *abbrev;
6833   unsigned int bytes_read;
6834   unsigned int load_all = 0;
6835
6836   int nesting_level = 1;
6837
6838   parent_die = NULL;
6839   last_die = NULL;
6840
6841   if (cu->per_cu && cu->per_cu->load_all_dies)
6842     load_all = 1;
6843
6844   cu->partial_dies
6845     = htab_create_alloc_ex (cu->header.length / 12,
6846                             partial_die_hash,
6847                             partial_die_eq,
6848                             NULL,
6849                             &cu->comp_unit_obstack,
6850                             hashtab_obstack_allocate,
6851                             dummy_obstack_deallocate);
6852
6853   part_die = obstack_alloc (&cu->comp_unit_obstack,
6854                             sizeof (struct partial_die_info));
6855
6856   while (1)
6857     {
6858       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6859
6860       /* A NULL abbrev means the end of a series of children.  */
6861       if (abbrev == NULL)
6862         {
6863           if (--nesting_level == 0)
6864             {
6865               /* PART_DIE was probably the last thing allocated on the
6866                  comp_unit_obstack, so we could call obstack_free
6867                  here.  We don't do that because the waste is small,
6868                  and will be cleaned up when we're done with this
6869                  compilation unit.  This way, we're also more robust
6870                  against other users of the comp_unit_obstack.  */
6871               return first_die;
6872             }
6873           info_ptr += bytes_read;
6874           last_die = parent_die;
6875           parent_die = parent_die->die_parent;
6876           continue;
6877         }
6878
6879       /* Check whether this DIE is interesting enough to save.  Normally
6880          we would not be interested in members here, but there may be
6881          later variables referencing them via DW_AT_specification (for
6882          static members).  */
6883       if (!load_all
6884           && !is_type_tag_for_partial (abbrev->tag)
6885           && abbrev->tag != DW_TAG_enumerator
6886           && abbrev->tag != DW_TAG_subprogram
6887           && abbrev->tag != DW_TAG_lexical_block
6888           && abbrev->tag != DW_TAG_variable
6889           && abbrev->tag != DW_TAG_namespace
6890           && abbrev->tag != DW_TAG_module
6891           && abbrev->tag != DW_TAG_member)
6892         {
6893           /* Otherwise we skip to the next sibling, if any.  */
6894           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
6895           continue;
6896         }
6897
6898       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6899                                    buffer, info_ptr, cu);
6900
6901       /* This two-pass algorithm for processing partial symbols has a
6902          high cost in cache pressure.  Thus, handle some simple cases
6903          here which cover the majority of C partial symbols.  DIEs
6904          which neither have specification tags in them, nor could have
6905          specification tags elsewhere pointing at them, can simply be
6906          processed and discarded.
6907
6908          This segment is also optional; scan_partial_symbols and
6909          add_partial_symbol will handle these DIEs if we chain
6910          them in normally.  When compilers which do not emit large
6911          quantities of duplicate debug information are more common,
6912          this code can probably be removed.  */
6913
6914       /* Any complete simple types at the top level (pretty much all
6915          of them, for a language without namespaces), can be processed
6916          directly.  */
6917       if (parent_die == NULL
6918           && part_die->has_specification == 0
6919           && part_die->is_declaration == 0
6920           && (part_die->tag == DW_TAG_typedef
6921               || part_die->tag == DW_TAG_base_type
6922               || part_die->tag == DW_TAG_subrange_type))
6923         {
6924           if (building_psymtab && part_die->name != NULL)
6925             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6926                                  VAR_DOMAIN, LOC_TYPEDEF,
6927                                  &cu->objfile->static_psymbols,
6928                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
6929           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6930           continue;
6931         }
6932
6933       /* If we're at the second level, and we're an enumerator, and
6934          our parent has no specification (meaning possibly lives in a
6935          namespace elsewhere), then we can add the partial symbol now
6936          instead of queueing it.  */
6937       if (part_die->tag == DW_TAG_enumerator
6938           && parent_die != NULL
6939           && parent_die->die_parent == NULL
6940           && parent_die->tag == DW_TAG_enumeration_type
6941           && parent_die->has_specification == 0)
6942         {
6943           if (part_die->name == NULL)
6944             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6945           else if (building_psymtab)
6946             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6947                                  VAR_DOMAIN, LOC_CONST,
6948                                  (cu->language == language_cplus
6949                                   || cu->language == language_java)
6950                                  ? &cu->objfile->global_psymbols
6951                                  : &cu->objfile->static_psymbols,
6952                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
6953
6954           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6955           continue;
6956         }
6957
6958       /* We'll save this DIE so link it in.  */
6959       part_die->die_parent = parent_die;
6960       part_die->die_sibling = NULL;
6961       part_die->die_child = NULL;
6962
6963       if (last_die && last_die == parent_die)
6964         last_die->die_child = part_die;
6965       else if (last_die)
6966         last_die->die_sibling = part_die;
6967
6968       last_die = part_die;
6969
6970       if (first_die == NULL)
6971         first_die = part_die;
6972
6973       /* Maybe add the DIE to the hash table.  Not all DIEs that we
6974          find interesting need to be in the hash table, because we
6975          also have the parent/sibling/child chains; only those that we
6976          might refer to by offset later during partial symbol reading.
6977
6978          For now this means things that might have be the target of a
6979          DW_AT_specification, DW_AT_abstract_origin, or
6980          DW_AT_extension.  DW_AT_extension will refer only to
6981          namespaces; DW_AT_abstract_origin refers to functions (and
6982          many things under the function DIE, but we do not recurse
6983          into function DIEs during partial symbol reading) and
6984          possibly variables as well; DW_AT_specification refers to
6985          declarations.  Declarations ought to have the DW_AT_declaration
6986          flag.  It happens that GCC forgets to put it in sometimes, but
6987          only for functions, not for types.
6988
6989          Adding more things than necessary to the hash table is harmless
6990          except for the performance cost.  Adding too few will result in
6991          wasted time in find_partial_die, when we reread the compilation
6992          unit with load_all_dies set.  */
6993
6994       if (load_all
6995           || abbrev->tag == DW_TAG_subprogram
6996           || abbrev->tag == DW_TAG_variable
6997           || abbrev->tag == DW_TAG_namespace
6998           || part_die->is_declaration)
6999         {
7000           void **slot;
7001
7002           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
7003                                            part_die->offset, INSERT);
7004           *slot = part_die;
7005         }
7006
7007       part_die = obstack_alloc (&cu->comp_unit_obstack,
7008                                 sizeof (struct partial_die_info));
7009
7010       /* For some DIEs we want to follow their children (if any).  For C
7011          we have no reason to follow the children of structures; for other
7012          languages we have to, both so that we can get at method physnames
7013          to infer fully qualified class names, and for DW_AT_specification.
7014
7015          For Ada, we need to scan the children of subprograms and lexical
7016          blocks as well because Ada allows the definition of nested
7017          entities that could be interesting for the debugger, such as
7018          nested subprograms for instance.  */
7019       if (last_die->has_children
7020           && (load_all
7021               || last_die->tag == DW_TAG_namespace
7022               || last_die->tag == DW_TAG_module
7023               || last_die->tag == DW_TAG_enumeration_type
7024               || (cu->language != language_c
7025                   && (last_die->tag == DW_TAG_class_type
7026                       || last_die->tag == DW_TAG_interface_type
7027                       || last_die->tag == DW_TAG_structure_type
7028                       || last_die->tag == DW_TAG_union_type))
7029               || (cu->language == language_ada
7030                   && (last_die->tag == DW_TAG_subprogram
7031                       || last_die->tag == DW_TAG_lexical_block))))
7032         {
7033           nesting_level++;
7034           parent_die = last_die;
7035           continue;
7036         }
7037
7038       /* Otherwise we skip to the next sibling, if any.  */
7039       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
7040
7041       /* Back to the top, do it again.  */
7042     }
7043 }
7044
7045 /* Read a minimal amount of information into the minimal die structure.  */
7046
7047 static gdb_byte *
7048 read_partial_die (struct partial_die_info *part_die,
7049                   struct abbrev_info *abbrev,
7050                   unsigned int abbrev_len, bfd *abfd,
7051                   gdb_byte *buffer, gdb_byte *info_ptr,
7052                   struct dwarf2_cu *cu)
7053 {
7054   unsigned int i;
7055   struct attribute attr;
7056   int has_low_pc_attr = 0;
7057   int has_high_pc_attr = 0;
7058
7059   memset (part_die, 0, sizeof (struct partial_die_info));
7060
7061   part_die->offset = info_ptr - buffer;
7062
7063   info_ptr += abbrev_len;
7064
7065   if (abbrev == NULL)
7066     return info_ptr;
7067
7068   part_die->tag = abbrev->tag;
7069   part_die->has_children = abbrev->has_children;
7070
7071   for (i = 0; i < abbrev->num_attrs; ++i)
7072     {
7073       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
7074
7075       /* Store the data if it is of an attribute we want to keep in a
7076          partial symbol table.  */
7077       switch (attr.name)
7078         {
7079         case DW_AT_name:
7080           switch (part_die->tag)
7081             {
7082             case DW_TAG_compile_unit:
7083             case DW_TAG_type_unit:
7084               /* Compilation units have a DW_AT_name that is a filename, not
7085                  a source language identifier.  */
7086             case DW_TAG_enumeration_type:
7087             case DW_TAG_enumerator:
7088               /* These tags always have simple identifiers already; no need
7089                  to canonicalize them.  */
7090               part_die->name = DW_STRING (&attr);
7091               break;
7092             default:
7093               part_die->name
7094                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
7095                                             &cu->objfile->objfile_obstack);
7096               break;
7097             }
7098           break;
7099         case DW_AT_linkage_name:
7100         case DW_AT_MIPS_linkage_name:
7101           /* Note that both forms of linkage name might appear.  We
7102              assume they will be the same, and we only store the last
7103              one we see.  */
7104           if (cu->language == language_ada)
7105             part_die->name = DW_STRING (&attr);
7106           break;
7107         case DW_AT_low_pc:
7108           has_low_pc_attr = 1;
7109           part_die->lowpc = DW_ADDR (&attr);
7110           break;
7111         case DW_AT_high_pc:
7112           has_high_pc_attr = 1;
7113           part_die->highpc = DW_ADDR (&attr);
7114           break;
7115         case DW_AT_location:
7116           /* Support the .debug_loc offsets */
7117           if (attr_form_is_block (&attr))
7118             {
7119                part_die->locdesc = DW_BLOCK (&attr);
7120             }
7121           else if (attr_form_is_section_offset (&attr))
7122             {
7123               dwarf2_complex_location_expr_complaint ();
7124             }
7125           else
7126             {
7127               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7128                                                      "partial symbol information");
7129             }
7130           break;
7131         case DW_AT_external:
7132           part_die->is_external = DW_UNSND (&attr);
7133           break;
7134         case DW_AT_declaration:
7135           part_die->is_declaration = DW_UNSND (&attr);
7136           break;
7137         case DW_AT_type:
7138           part_die->has_type = 1;
7139           break;
7140         case DW_AT_abstract_origin:
7141         case DW_AT_specification:
7142         case DW_AT_extension:
7143           part_die->has_specification = 1;
7144           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
7145           break;
7146         case DW_AT_sibling:
7147           /* Ignore absolute siblings, they might point outside of
7148              the current compile unit.  */
7149           if (attr.form == DW_FORM_ref_addr)
7150             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
7151           else
7152             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
7153           break;
7154         case DW_AT_byte_size:
7155           part_die->has_byte_size = 1;
7156           break;
7157         case DW_AT_calling_convention:
7158           /* DWARF doesn't provide a way to identify a program's source-level
7159              entry point.  DW_AT_calling_convention attributes are only meant
7160              to describe functions' calling conventions.
7161
7162              However, because it's a necessary piece of information in
7163              Fortran, and because DW_CC_program is the only piece of debugging
7164              information whose definition refers to a 'main program' at all,
7165              several compilers have begun marking Fortran main programs with
7166              DW_CC_program --- even when those functions use the standard
7167              calling conventions.
7168
7169              So until DWARF specifies a way to provide this information and
7170              compilers pick up the new representation, we'll support this
7171              practice.  */
7172           if (DW_UNSND (&attr) == DW_CC_program
7173               && cu->language == language_fortran)
7174             set_main_name (part_die->name);
7175           break;
7176         default:
7177           break;
7178         }
7179     }
7180
7181   /* When using the GNU linker, .gnu.linkonce. sections are used to
7182      eliminate duplicate copies of functions and vtables and such.
7183      The linker will arbitrarily choose one and discard the others.
7184      The AT_*_pc values for such functions refer to local labels in
7185      these sections.  If the section from that file was discarded, the
7186      labels are not in the output, so the relocs get a value of 0.
7187      If this is a discarded function, mark the pc bounds as invalid,
7188      so that GDB will ignore it.  */
7189   if (has_low_pc_attr && has_high_pc_attr
7190       && part_die->lowpc < part_die->highpc
7191       && (part_die->lowpc != 0
7192           || dwarf2_per_objfile->has_section_at_zero))
7193     part_die->has_pc_info = 1;
7194
7195   return info_ptr;
7196 }
7197
7198 /* Find a cached partial DIE at OFFSET in CU.  */
7199
7200 static struct partial_die_info *
7201 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
7202 {
7203   struct partial_die_info *lookup_die = NULL;
7204   struct partial_die_info part_die;
7205
7206   part_die.offset = offset;
7207   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
7208
7209   return lookup_die;
7210 }
7211
7212 /* Find a partial DIE at OFFSET, which may or may not be in CU,
7213    except in the case of .debug_types DIEs which do not reference
7214    outside their CU (they do however referencing other types via
7215    DW_FORM_sig8).  */
7216
7217 static struct partial_die_info *
7218 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
7219 {
7220   struct dwarf2_per_cu_data *per_cu = NULL;
7221   struct partial_die_info *pd = NULL;
7222
7223   if (cu->per_cu->from_debug_types)
7224     {
7225       pd = find_partial_die_in_comp_unit (offset, cu);
7226       if (pd != NULL)
7227         return pd;
7228       goto not_found;
7229     }
7230
7231   if (offset_in_cu_p (&cu->header, offset))
7232     {
7233       pd = find_partial_die_in_comp_unit (offset, cu);
7234       if (pd != NULL)
7235         return pd;
7236     }
7237
7238   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
7239
7240   if (per_cu->cu == NULL)
7241     {
7242       load_partial_comp_unit (per_cu, cu->objfile);
7243       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7244       dwarf2_per_objfile->read_in_chain = per_cu;
7245     }
7246
7247   per_cu->cu->last_used = 0;
7248   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
7249
7250   if (pd == NULL && per_cu->load_all_dies == 0)
7251     {
7252       struct cleanup *back_to;
7253       struct partial_die_info comp_unit_die;
7254       struct abbrev_info *abbrev;
7255       unsigned int bytes_read;
7256       char *info_ptr;
7257
7258       per_cu->load_all_dies = 1;
7259
7260       /* Re-read the DIEs.  */
7261       back_to = make_cleanup (null_cleanup, 0);
7262       if (per_cu->cu->dwarf2_abbrevs == NULL)
7263         {
7264           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
7265           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
7266         }
7267       info_ptr = (dwarf2_per_objfile->info.buffer
7268                   + per_cu->cu->header.offset
7269                   + per_cu->cu->header.first_die_offset);
7270       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
7271       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
7272                                    per_cu->cu->objfile->obfd,
7273                                    dwarf2_per_objfile->info.buffer, info_ptr,
7274                                    per_cu->cu);
7275       if (comp_unit_die.has_children)
7276         load_partial_dies (per_cu->cu->objfile->obfd,
7277                            dwarf2_per_objfile->info.buffer, info_ptr,
7278                            0, per_cu->cu);
7279       do_cleanups (back_to);
7280
7281       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
7282     }
7283
7284  not_found:
7285
7286   if (pd == NULL)
7287     internal_error (__FILE__, __LINE__,
7288                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
7289                     offset, bfd_get_filename (cu->objfile->obfd));
7290   return pd;
7291 }
7292
7293 /* Adjust PART_DIE before generating a symbol for it.  This function
7294    may set the is_external flag or change the DIE's name.  */
7295
7296 static void
7297 fixup_partial_die (struct partial_die_info *part_die,
7298                    struct dwarf2_cu *cu)
7299 {
7300   /* If we found a reference attribute and the DIE has no name, try
7301      to find a name in the referred to DIE.  */
7302
7303   if (part_die->name == NULL && part_die->has_specification)
7304     {
7305       struct partial_die_info *spec_die;
7306
7307       spec_die = find_partial_die (part_die->spec_offset, cu);
7308
7309       fixup_partial_die (spec_die, cu);
7310
7311       if (spec_die->name)
7312         {
7313           part_die->name = spec_die->name;
7314
7315           /* Copy DW_AT_external attribute if it is set.  */
7316           if (spec_die->is_external)
7317             part_die->is_external = spec_die->is_external;
7318         }
7319     }
7320
7321   /* Set default names for some unnamed DIEs.  */
7322   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
7323                                  || part_die->tag == DW_TAG_class_type))
7324     part_die->name = "(anonymous class)";
7325
7326   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
7327     part_die->name = "(anonymous namespace)";
7328
7329   if (part_die->tag == DW_TAG_structure_type
7330       || part_die->tag == DW_TAG_class_type
7331       || part_die->tag == DW_TAG_union_type)
7332     guess_structure_name (part_die, cu);
7333 }
7334
7335 /* Read an attribute value described by an attribute form.  */
7336
7337 static gdb_byte *
7338 read_attribute_value (struct attribute *attr, unsigned form,
7339                       bfd *abfd, gdb_byte *info_ptr,
7340                       struct dwarf2_cu *cu)
7341 {
7342   struct comp_unit_head *cu_header = &cu->header;
7343   unsigned int bytes_read;
7344   struct dwarf_block *blk;
7345
7346   attr->form = form;
7347   switch (form)
7348     {
7349     case DW_FORM_ref_addr:
7350       if (cu->header.version == 2)
7351         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7352       else
7353         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7354       info_ptr += bytes_read;
7355       break;
7356     case DW_FORM_addr:
7357       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7358       info_ptr += bytes_read;
7359       break;
7360     case DW_FORM_block2:
7361       blk = dwarf_alloc_block (cu);
7362       blk->size = read_2_bytes (abfd, info_ptr);
7363       info_ptr += 2;
7364       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7365       info_ptr += blk->size;
7366       DW_BLOCK (attr) = blk;
7367       break;
7368     case DW_FORM_block4:
7369       blk = dwarf_alloc_block (cu);
7370       blk->size = read_4_bytes (abfd, info_ptr);
7371       info_ptr += 4;
7372       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7373       info_ptr += blk->size;
7374       DW_BLOCK (attr) = blk;
7375       break;
7376     case DW_FORM_data2:
7377       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
7378       info_ptr += 2;
7379       break;
7380     case DW_FORM_data4:
7381       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
7382       info_ptr += 4;
7383       break;
7384     case DW_FORM_data8:
7385       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
7386       info_ptr += 8;
7387       break;
7388     case DW_FORM_sec_offset:
7389       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7390       info_ptr += bytes_read;
7391       break;
7392     case DW_FORM_string:
7393       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
7394       DW_STRING_IS_CANONICAL (attr) = 0;
7395       info_ptr += bytes_read;
7396       break;
7397     case DW_FORM_strp:
7398       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
7399                                                &bytes_read);
7400       DW_STRING_IS_CANONICAL (attr) = 0;
7401       info_ptr += bytes_read;
7402       break;
7403     case DW_FORM_exprloc:
7404     case DW_FORM_block:
7405       blk = dwarf_alloc_block (cu);
7406       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7407       info_ptr += bytes_read;
7408       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7409       info_ptr += blk->size;
7410       DW_BLOCK (attr) = blk;
7411       break;
7412     case DW_FORM_block1:
7413       blk = dwarf_alloc_block (cu);
7414       blk->size = read_1_byte (abfd, info_ptr);
7415       info_ptr += 1;
7416       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7417       info_ptr += blk->size;
7418       DW_BLOCK (attr) = blk;
7419       break;
7420     case DW_FORM_data1:
7421       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7422       info_ptr += 1;
7423       break;
7424     case DW_FORM_flag:
7425       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7426       info_ptr += 1;
7427       break;
7428     case DW_FORM_flag_present:
7429       DW_UNSND (attr) = 1;
7430       break;
7431     case DW_FORM_sdata:
7432       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7433       info_ptr += bytes_read;
7434       break;
7435     case DW_FORM_udata:
7436       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7437       info_ptr += bytes_read;
7438       break;
7439     case DW_FORM_ref1:
7440       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
7441       info_ptr += 1;
7442       break;
7443     case DW_FORM_ref2:
7444       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
7445       info_ptr += 2;
7446       break;
7447     case DW_FORM_ref4:
7448       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
7449       info_ptr += 4;
7450       break;
7451     case DW_FORM_ref8:
7452       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
7453       info_ptr += 8;
7454       break;
7455     case DW_FORM_sig8:
7456       /* Convert the signature to something we can record in DW_UNSND
7457          for later lookup.
7458          NOTE: This is NULL if the type wasn't found.  */
7459       DW_SIGNATURED_TYPE (attr) =
7460         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7461       info_ptr += 8;
7462       break;
7463     case DW_FORM_ref_udata:
7464       DW_ADDR (attr) = (cu->header.offset
7465                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
7466       info_ptr += bytes_read;
7467       break;
7468     case DW_FORM_indirect:
7469       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7470       info_ptr += bytes_read;
7471       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
7472       break;
7473     default:
7474       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
7475              dwarf_form_name (form),
7476              bfd_get_filename (abfd));
7477     }
7478
7479   /* We have seen instances where the compiler tried to emit a byte
7480      size attribute of -1 which ended up being encoded as an unsigned
7481      0xffffffff.  Although 0xffffffff is technically a valid size value,
7482      an object of this size seems pretty unlikely so we can relatively
7483      safely treat these cases as if the size attribute was invalid and
7484      treat them as zero by default.  */
7485   if (attr->name == DW_AT_byte_size
7486       && form == DW_FORM_data4
7487       && DW_UNSND (attr) >= 0xffffffff)
7488     {
7489       complaint
7490         (&symfile_complaints,
7491          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
7492          hex_string (DW_UNSND (attr)));
7493       DW_UNSND (attr) = 0;
7494     }
7495
7496   return info_ptr;
7497 }
7498
7499 /* Read an attribute described by an abbreviated attribute.  */
7500
7501 static gdb_byte *
7502 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
7503                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
7504 {
7505   attr->name = abbrev->name;
7506   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
7507 }
7508
7509 /* read dwarf information from a buffer */
7510
7511 static unsigned int
7512 read_1_byte (bfd *abfd, gdb_byte *buf)
7513 {
7514   return bfd_get_8 (abfd, buf);
7515 }
7516
7517 static int
7518 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
7519 {
7520   return bfd_get_signed_8 (abfd, buf);
7521 }
7522
7523 static unsigned int
7524 read_2_bytes (bfd *abfd, gdb_byte *buf)
7525 {
7526   return bfd_get_16 (abfd, buf);
7527 }
7528
7529 static int
7530 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
7531 {
7532   return bfd_get_signed_16 (abfd, buf);
7533 }
7534
7535 static unsigned int
7536 read_4_bytes (bfd *abfd, gdb_byte *buf)
7537 {
7538   return bfd_get_32 (abfd, buf);
7539 }
7540
7541 static int
7542 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
7543 {
7544   return bfd_get_signed_32 (abfd, buf);
7545 }
7546
7547 static ULONGEST
7548 read_8_bytes (bfd *abfd, gdb_byte *buf)
7549 {
7550   return bfd_get_64 (abfd, buf);
7551 }
7552
7553 static CORE_ADDR
7554 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
7555               unsigned int *bytes_read)
7556 {
7557   struct comp_unit_head *cu_header = &cu->header;
7558   CORE_ADDR retval = 0;
7559
7560   if (cu_header->signed_addr_p)
7561     {
7562       switch (cu_header->addr_size)
7563         {
7564         case 2:
7565           retval = bfd_get_signed_16 (abfd, buf);
7566           break;
7567         case 4:
7568           retval = bfd_get_signed_32 (abfd, buf);
7569           break;
7570         case 8:
7571           retval = bfd_get_signed_64 (abfd, buf);
7572           break;
7573         default:
7574           internal_error (__FILE__, __LINE__,
7575                           _("read_address: bad switch, signed [in module %s]"),
7576                           bfd_get_filename (abfd));
7577         }
7578     }
7579   else
7580     {
7581       switch (cu_header->addr_size)
7582         {
7583         case 2:
7584           retval = bfd_get_16 (abfd, buf);
7585           break;
7586         case 4:
7587           retval = bfd_get_32 (abfd, buf);
7588           break;
7589         case 8:
7590           retval = bfd_get_64 (abfd, buf);
7591           break;
7592         default:
7593           internal_error (__FILE__, __LINE__,
7594                           _("read_address: bad switch, unsigned [in module %s]"),
7595                           bfd_get_filename (abfd));
7596         }
7597     }
7598
7599   *bytes_read = cu_header->addr_size;
7600   return retval;
7601 }
7602
7603 /* Read the initial length from a section.  The (draft) DWARF 3
7604    specification allows the initial length to take up either 4 bytes
7605    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
7606    bytes describe the length and all offsets will be 8 bytes in length
7607    instead of 4.
7608
7609    An older, non-standard 64-bit format is also handled by this
7610    function.  The older format in question stores the initial length
7611    as an 8-byte quantity without an escape value.  Lengths greater
7612    than 2^32 aren't very common which means that the initial 4 bytes
7613    is almost always zero.  Since a length value of zero doesn't make
7614    sense for the 32-bit format, this initial zero can be considered to
7615    be an escape value which indicates the presence of the older 64-bit
7616    format.  As written, the code can't detect (old format) lengths
7617    greater than 4GB.  If it becomes necessary to handle lengths
7618    somewhat larger than 4GB, we could allow other small values (such
7619    as the non-sensical values of 1, 2, and 3) to also be used as
7620    escape values indicating the presence of the old format.
7621
7622    The value returned via bytes_read should be used to increment the
7623    relevant pointer after calling read_initial_length().
7624
7625    [ Note:  read_initial_length() and read_offset() are based on the
7626      document entitled "DWARF Debugging Information Format", revision
7627      3, draft 8, dated November 19, 2001.  This document was obtained
7628      from:
7629
7630         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
7631
7632      This document is only a draft and is subject to change.  (So beware.)
7633
7634      Details regarding the older, non-standard 64-bit format were
7635      determined empirically by examining 64-bit ELF files produced by
7636      the SGI toolchain on an IRIX 6.5 machine.
7637
7638      - Kevin, July 16, 2002
7639    ] */
7640
7641 static LONGEST
7642 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
7643 {
7644   LONGEST length = bfd_get_32 (abfd, buf);
7645
7646   if (length == 0xffffffff)
7647     {
7648       length = bfd_get_64 (abfd, buf + 4);
7649       *bytes_read = 12;
7650     }
7651   else if (length == 0)
7652     {
7653       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
7654       length = bfd_get_64 (abfd, buf);
7655       *bytes_read = 8;
7656     }
7657   else
7658     {
7659       *bytes_read = 4;
7660     }
7661
7662   return length;
7663 }
7664
7665 /* Cover function for read_initial_length.
7666    Returns the length of the object at BUF, and stores the size of the
7667    initial length in *BYTES_READ and stores the size that offsets will be in
7668    *OFFSET_SIZE.
7669    If the initial length size is not equivalent to that specified in
7670    CU_HEADER then issue a complaint.
7671    This is useful when reading non-comp-unit headers.  */
7672
7673 static LONGEST
7674 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7675                                         const struct comp_unit_head *cu_header,
7676                                         unsigned int *bytes_read,
7677                                         unsigned int *offset_size)
7678 {
7679   LONGEST length = read_initial_length (abfd, buf, bytes_read);
7680
7681   gdb_assert (cu_header->initial_length_size == 4
7682               || cu_header->initial_length_size == 8
7683               || cu_header->initial_length_size == 12);
7684
7685   if (cu_header->initial_length_size != *bytes_read)
7686     complaint (&symfile_complaints,
7687                _("intermixed 32-bit and 64-bit DWARF sections"));
7688
7689   *offset_size = (*bytes_read == 4) ? 4 : 8;
7690   return length;
7691 }
7692
7693 /* Read an offset from the data stream.  The size of the offset is
7694    given by cu_header->offset_size.  */
7695
7696 static LONGEST
7697 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
7698              unsigned int *bytes_read)
7699 {
7700   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7701
7702   *bytes_read = cu_header->offset_size;
7703   return offset;
7704 }
7705
7706 /* Read an offset from the data stream.  */
7707
7708 static LONGEST
7709 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
7710 {
7711   LONGEST retval = 0;
7712
7713   switch (offset_size)
7714     {
7715     case 4:
7716       retval = bfd_get_32 (abfd, buf);
7717       break;
7718     case 8:
7719       retval = bfd_get_64 (abfd, buf);
7720       break;
7721     default:
7722       internal_error (__FILE__, __LINE__,
7723                       _("read_offset_1: bad switch [in module %s]"),
7724                       bfd_get_filename (abfd));
7725     }
7726
7727   return retval;
7728 }
7729
7730 static gdb_byte *
7731 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
7732 {
7733   /* If the size of a host char is 8 bits, we can return a pointer
7734      to the buffer, otherwise we have to copy the data to a buffer
7735      allocated on the temporary obstack.  */
7736   gdb_assert (HOST_CHAR_BIT == 8);
7737   return buf;
7738 }
7739
7740 static char *
7741 read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7742 {
7743   /* If the size of a host char is 8 bits, we can return a pointer
7744      to the string, otherwise we have to copy the string to a buffer
7745      allocated on the temporary obstack.  */
7746   gdb_assert (HOST_CHAR_BIT == 8);
7747   if (*buf == '\0')
7748     {
7749       *bytes_read_ptr = 1;
7750       return NULL;
7751     }
7752   *bytes_read_ptr = strlen ((char *) buf) + 1;
7753   return (char *) buf;
7754 }
7755
7756 static char *
7757 read_indirect_string (bfd *abfd, gdb_byte *buf,
7758                       const struct comp_unit_head *cu_header,
7759                       unsigned int *bytes_read_ptr)
7760 {
7761   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
7762
7763   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
7764   if (dwarf2_per_objfile->str.buffer == NULL)
7765     {
7766       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
7767                       bfd_get_filename (abfd));
7768       return NULL;
7769     }
7770   if (str_offset >= dwarf2_per_objfile->str.size)
7771     {
7772       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
7773                       bfd_get_filename (abfd));
7774       return NULL;
7775     }
7776   gdb_assert (HOST_CHAR_BIT == 8);
7777   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
7778     return NULL;
7779   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
7780 }
7781
7782 static unsigned long
7783 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7784 {
7785   unsigned long result;
7786   unsigned int num_read;
7787   int i, shift;
7788   unsigned char byte;
7789
7790   result = 0;
7791   shift = 0;
7792   num_read = 0;
7793   i = 0;
7794   while (1)
7795     {
7796       byte = bfd_get_8 (abfd, buf);
7797       buf++;
7798       num_read++;
7799       result |= ((unsigned long)(byte & 127) << shift);
7800       if ((byte & 128) == 0)
7801         {
7802           break;
7803         }
7804       shift += 7;
7805     }
7806   *bytes_read_ptr = num_read;
7807   return result;
7808 }
7809
7810 static long
7811 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7812 {
7813   long result;
7814   int i, shift, num_read;
7815   unsigned char byte;
7816
7817   result = 0;
7818   shift = 0;
7819   num_read = 0;
7820   i = 0;
7821   while (1)
7822     {
7823       byte = bfd_get_8 (abfd, buf);
7824       buf++;
7825       num_read++;
7826       result |= ((long)(byte & 127) << shift);
7827       shift += 7;
7828       if ((byte & 128) == 0)
7829         {
7830           break;
7831         }
7832     }
7833   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7834     result |= -(((long)1) << shift);
7835   *bytes_read_ptr = num_read;
7836   return result;
7837 }
7838
7839 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
7840
7841 static gdb_byte *
7842 skip_leb128 (bfd *abfd, gdb_byte *buf)
7843 {
7844   int byte;
7845
7846   while (1)
7847     {
7848       byte = bfd_get_8 (abfd, buf);
7849       buf++;
7850       if ((byte & 128) == 0)
7851         return buf;
7852     }
7853 }
7854
7855 static void
7856 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
7857 {
7858   switch (lang)
7859     {
7860     case DW_LANG_C89:
7861     case DW_LANG_C99:
7862     case DW_LANG_C:
7863       cu->language = language_c;
7864       break;
7865     case DW_LANG_C_plus_plus:
7866       cu->language = language_cplus;
7867       break;
7868     case DW_LANG_D:
7869       cu->language = language_d;
7870       break;
7871     case DW_LANG_Fortran77:
7872     case DW_LANG_Fortran90:
7873     case DW_LANG_Fortran95:
7874       cu->language = language_fortran;
7875       break;
7876     case DW_LANG_Mips_Assembler:
7877       cu->language = language_asm;
7878       break;
7879     case DW_LANG_Java:
7880       cu->language = language_java;
7881       break;
7882     case DW_LANG_Ada83:
7883     case DW_LANG_Ada95:
7884       cu->language = language_ada;
7885       break;
7886     case DW_LANG_Modula2:
7887       cu->language = language_m2;
7888       break;
7889     case DW_LANG_Pascal83:
7890       cu->language = language_pascal;
7891       break;
7892     case DW_LANG_ObjC:
7893       cu->language = language_objc;
7894       break;
7895     case DW_LANG_Cobol74:
7896     case DW_LANG_Cobol85:
7897     default:
7898       cu->language = language_minimal;
7899       break;
7900     }
7901   cu->language_defn = language_def (cu->language);
7902 }
7903
7904 /* Return the named attribute or NULL if not there.  */
7905
7906 static struct attribute *
7907 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
7908 {
7909   unsigned int i;
7910   struct attribute *spec = NULL;
7911
7912   for (i = 0; i < die->num_attrs; ++i)
7913     {
7914       if (die->attrs[i].name == name)
7915         return &die->attrs[i];
7916       if (die->attrs[i].name == DW_AT_specification
7917           || die->attrs[i].name == DW_AT_abstract_origin)
7918         spec = &die->attrs[i];
7919     }
7920
7921   if (spec)
7922     {
7923       die = follow_die_ref (die, spec, &cu);
7924       return dwarf2_attr (die, name, cu);
7925     }
7926
7927   return NULL;
7928 }
7929
7930 /* Return the named attribute or NULL if not there,
7931    but do not follow DW_AT_specification, etc.
7932    This is for use in contexts where we're reading .debug_types dies.
7933    Following DW_AT_specification, DW_AT_abstract_origin will take us
7934    back up the chain, and we want to go down.  */
7935
7936 static struct attribute *
7937 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7938                        struct dwarf2_cu *cu)
7939 {
7940   unsigned int i;
7941
7942   for (i = 0; i < die->num_attrs; ++i)
7943     if (die->attrs[i].name == name)
7944       return &die->attrs[i];
7945
7946   return NULL;
7947 }
7948
7949 /* Return non-zero iff the attribute NAME is defined for the given DIE,
7950    and holds a non-zero value.  This function should only be used for
7951    DW_FORM_flag or DW_FORM_flag_present attributes.  */
7952
7953 static int
7954 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7955 {
7956   struct attribute *attr = dwarf2_attr (die, name, cu);
7957
7958   return (attr && DW_UNSND (attr));
7959 }
7960
7961 static int
7962 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
7963 {
7964   /* A DIE is a declaration if it has a DW_AT_declaration attribute
7965      which value is non-zero.  However, we have to be careful with
7966      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7967      (via dwarf2_flag_true_p) follows this attribute.  So we may
7968      end up accidently finding a declaration attribute that belongs
7969      to a different DIE referenced by the specification attribute,
7970      even though the given DIE does not have a declaration attribute.  */
7971   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7972           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
7973 }
7974
7975 /* Return the die giving the specification for DIE, if there is
7976    one.  *SPEC_CU is the CU containing DIE on input, and the CU
7977    containing the return value on output.  If there is no
7978    specification, but there is an abstract origin, that is
7979    returned.  */
7980
7981 static struct die_info *
7982 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
7983 {
7984   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7985                                              *spec_cu);
7986
7987   if (spec_attr == NULL)
7988     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7989
7990   if (spec_attr == NULL)
7991     return NULL;
7992   else
7993     return follow_die_ref (die, spec_attr, spec_cu);
7994 }
7995
7996 /* Free the line_header structure *LH, and any arrays and strings it
7997    refers to.  */
7998 static void
7999 free_line_header (struct line_header *lh)
8000 {
8001   if (lh->standard_opcode_lengths)
8002     xfree (lh->standard_opcode_lengths);
8003
8004   /* Remember that all the lh->file_names[i].name pointers are
8005      pointers into debug_line_buffer, and don't need to be freed.  */
8006   if (lh->file_names)
8007     xfree (lh->file_names);
8008
8009   /* Similarly for the include directory names.  */
8010   if (lh->include_dirs)
8011     xfree (lh->include_dirs);
8012
8013   xfree (lh);
8014 }
8015
8016
8017 /* Add an entry to LH's include directory table.  */
8018 static void
8019 add_include_dir (struct line_header *lh, char *include_dir)
8020 {
8021   /* Grow the array if necessary.  */
8022   if (lh->include_dirs_size == 0)
8023     {
8024       lh->include_dirs_size = 1; /* for testing */
8025       lh->include_dirs = xmalloc (lh->include_dirs_size
8026                                   * sizeof (*lh->include_dirs));
8027     }
8028   else if (lh->num_include_dirs >= lh->include_dirs_size)
8029     {
8030       lh->include_dirs_size *= 2;
8031       lh->include_dirs = xrealloc (lh->include_dirs,
8032                                    (lh->include_dirs_size
8033                                     * sizeof (*lh->include_dirs)));
8034     }
8035
8036   lh->include_dirs[lh->num_include_dirs++] = include_dir;
8037 }
8038
8039
8040 /* Add an entry to LH's file name table.  */
8041 static void
8042 add_file_name (struct line_header *lh,
8043                char *name,
8044                unsigned int dir_index,
8045                unsigned int mod_time,
8046                unsigned int length)
8047 {
8048   struct file_entry *fe;
8049
8050   /* Grow the array if necessary.  */
8051   if (lh->file_names_size == 0)
8052     {
8053       lh->file_names_size = 1; /* for testing */
8054       lh->file_names = xmalloc (lh->file_names_size
8055                                 * sizeof (*lh->file_names));
8056     }
8057   else if (lh->num_file_names >= lh->file_names_size)
8058     {
8059       lh->file_names_size *= 2;
8060       lh->file_names = xrealloc (lh->file_names,
8061                                  (lh->file_names_size
8062                                   * sizeof (*lh->file_names)));
8063     }
8064
8065   fe = &lh->file_names[lh->num_file_names++];
8066   fe->name = name;
8067   fe->dir_index = dir_index;
8068   fe->mod_time = mod_time;
8069   fe->length = length;
8070   fe->included_p = 0;
8071   fe->symtab = NULL;
8072 }
8073
8074
8075 /* Read the statement program header starting at OFFSET in
8076    .debug_line, according to the endianness of ABFD.  Return a pointer
8077    to a struct line_header, allocated using xmalloc.
8078
8079    NOTE: the strings in the include directory and file name tables of
8080    the returned object point into debug_line_buffer, and must not be
8081    freed.  */
8082 static struct line_header *
8083 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
8084                           struct dwarf2_cu *cu)
8085 {
8086   struct cleanup *back_to;
8087   struct line_header *lh;
8088   gdb_byte *line_ptr;
8089   unsigned int bytes_read, offset_size;
8090   int i;
8091   char *cur_dir, *cur_file;
8092
8093   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
8094   if (dwarf2_per_objfile->line.buffer == NULL)
8095     {
8096       complaint (&symfile_complaints, _("missing .debug_line section"));
8097       return 0;
8098     }
8099
8100   /* Make sure that at least there's room for the total_length field.
8101      That could be 12 bytes long, but we're just going to fudge that.  */
8102   if (offset + 4 >= dwarf2_per_objfile->line.size)
8103     {
8104       dwarf2_statement_list_fits_in_line_number_section_complaint ();
8105       return 0;
8106     }
8107
8108   lh = xmalloc (sizeof (*lh));
8109   memset (lh, 0, sizeof (*lh));
8110   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
8111                           (void *) lh);
8112
8113   line_ptr = dwarf2_per_objfile->line.buffer + offset;
8114
8115   /* Read in the header.  */
8116   lh->total_length =
8117     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
8118                                             &bytes_read, &offset_size);
8119   line_ptr += bytes_read;
8120   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
8121                                      + dwarf2_per_objfile->line.size))
8122     {
8123       dwarf2_statement_list_fits_in_line_number_section_complaint ();
8124       return 0;
8125     }
8126   lh->statement_program_end = line_ptr + lh->total_length;
8127   lh->version = read_2_bytes (abfd, line_ptr);
8128   line_ptr += 2;
8129   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
8130   line_ptr += offset_size;
8131   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
8132   line_ptr += 1;
8133   if (lh->version >= 4)
8134     {
8135       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
8136       line_ptr += 1;
8137     }
8138   else
8139     lh->maximum_ops_per_instruction = 1;
8140
8141   if (lh->maximum_ops_per_instruction == 0)
8142     {
8143       lh->maximum_ops_per_instruction = 1;
8144       complaint (&symfile_complaints,
8145                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
8146     }
8147
8148   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
8149   line_ptr += 1;
8150   lh->line_base = read_1_signed_byte (abfd, line_ptr);
8151   line_ptr += 1;
8152   lh->line_range = read_1_byte (abfd, line_ptr);
8153   line_ptr += 1;
8154   lh->opcode_base = read_1_byte (abfd, line_ptr);
8155   line_ptr += 1;
8156   lh->standard_opcode_lengths
8157     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
8158
8159   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
8160   for (i = 1; i < lh->opcode_base; ++i)
8161     {
8162       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
8163       line_ptr += 1;
8164     }
8165
8166   /* Read directory table.  */
8167   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
8168     {
8169       line_ptr += bytes_read;
8170       add_include_dir (lh, cur_dir);
8171     }
8172   line_ptr += bytes_read;
8173
8174   /* Read file name table.  */
8175   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
8176     {
8177       unsigned int dir_index, mod_time, length;
8178
8179       line_ptr += bytes_read;
8180       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8181       line_ptr += bytes_read;
8182       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8183       line_ptr += bytes_read;
8184       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8185       line_ptr += bytes_read;
8186
8187       add_file_name (lh, cur_file, dir_index, mod_time, length);
8188     }
8189   line_ptr += bytes_read;
8190   lh->statement_program_start = line_ptr;
8191
8192   if (line_ptr > (dwarf2_per_objfile->line.buffer
8193                   + dwarf2_per_objfile->line.size))
8194     complaint (&symfile_complaints,
8195                _("line number info header doesn't fit in `.debug_line' section"));
8196
8197   discard_cleanups (back_to);
8198   return lh;
8199 }
8200
8201 /* This function exists to work around a bug in certain compilers
8202    (particularly GCC 2.95), in which the first line number marker of a
8203    function does not show up until after the prologue, right before
8204    the second line number marker.  This function shifts ADDRESS down
8205    to the beginning of the function if necessary, and is called on
8206    addresses passed to record_line.  */
8207
8208 static CORE_ADDR
8209 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
8210 {
8211   struct function_range *fn;
8212
8213   /* Find the function_range containing address.  */
8214   if (!cu->first_fn)
8215     return address;
8216
8217   if (!cu->cached_fn)
8218     cu->cached_fn = cu->first_fn;
8219
8220   fn = cu->cached_fn;
8221   while (fn)
8222     if (fn->lowpc <= address && fn->highpc > address)
8223       goto found;
8224     else
8225       fn = fn->next;
8226
8227   fn = cu->first_fn;
8228   while (fn && fn != cu->cached_fn)
8229     if (fn->lowpc <= address && fn->highpc > address)
8230       goto found;
8231     else
8232       fn = fn->next;
8233
8234   return address;
8235
8236  found:
8237   if (fn->seen_line)
8238     return address;
8239   if (address != fn->lowpc)
8240     complaint (&symfile_complaints,
8241                _("misplaced first line number at 0x%lx for '%s'"),
8242                (unsigned long) address, fn->name);
8243   fn->seen_line = 1;
8244   return fn->lowpc;
8245 }
8246
8247 /* Decode the Line Number Program (LNP) for the given line_header
8248    structure and CU.  The actual information extracted and the type
8249    of structures created from the LNP depends on the value of PST.
8250
8251    1. If PST is NULL, then this procedure uses the data from the program
8252       to create all necessary symbol tables, and their linetables.
8253       The compilation directory of the file is passed in COMP_DIR,
8254       and must not be NULL.
8255
8256    2. If PST is not NULL, this procedure reads the program to determine
8257       the list of files included by the unit represented by PST, and
8258       builds all the associated partial symbol tables.  In this case,
8259       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
8260       is not used to compute the full name of the symtab, and therefore
8261       omitting it when building the partial symtab does not introduce
8262       the potential for inconsistency - a partial symtab and its associated
8263       symbtab having a different fullname -).  */
8264
8265 static void
8266 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
8267                     struct dwarf2_cu *cu, struct partial_symtab *pst)
8268 {
8269   gdb_byte *line_ptr, *extended_end;
8270   gdb_byte *line_end;
8271   unsigned int bytes_read, extended_len;
8272   unsigned char op_code, extended_op, adj_opcode;
8273   CORE_ADDR baseaddr;
8274   struct objfile *objfile = cu->objfile;
8275   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8276   const int decode_for_pst_p = (pst != NULL);
8277   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
8278
8279   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8280
8281   line_ptr = lh->statement_program_start;
8282   line_end = lh->statement_program_end;
8283
8284   /* Read the statement sequences until there's nothing left.  */
8285   while (line_ptr < line_end)
8286     {
8287       /* state machine registers  */
8288       CORE_ADDR address = 0;
8289       unsigned int file = 1;
8290       unsigned int line = 1;
8291       unsigned int column = 0;
8292       int is_stmt = lh->default_is_stmt;
8293       int basic_block = 0;
8294       int end_sequence = 0;
8295       CORE_ADDR addr;
8296       unsigned char op_index = 0;
8297
8298       if (!decode_for_pst_p && lh->num_file_names >= file)
8299         {
8300           /* Start a subfile for the current file of the state machine.  */
8301           /* lh->include_dirs and lh->file_names are 0-based, but the
8302              directory and file name numbers in the statement program
8303              are 1-based.  */
8304           struct file_entry *fe = &lh->file_names[file - 1];
8305           char *dir = NULL;
8306
8307           if (fe->dir_index)
8308             dir = lh->include_dirs[fe->dir_index - 1];
8309
8310           dwarf2_start_subfile (fe->name, dir, comp_dir);
8311         }
8312
8313       /* Decode the table.  */
8314       while (!end_sequence)
8315         {
8316           op_code = read_1_byte (abfd, line_ptr);
8317           line_ptr += 1;
8318           if (line_ptr > line_end)
8319             {
8320               dwarf2_debug_line_missing_end_sequence_complaint ();
8321               break;
8322             }
8323
8324           if (op_code >= lh->opcode_base)
8325             {
8326               /* Special operand.  */
8327               adj_opcode = op_code - lh->opcode_base;
8328               address += (((op_index + (adj_opcode / lh->line_range))
8329                            / lh->maximum_ops_per_instruction)
8330                           * lh->minimum_instruction_length);
8331               op_index = ((op_index + (adj_opcode / lh->line_range))
8332                           % lh->maximum_ops_per_instruction);
8333               line += lh->line_base + (adj_opcode % lh->line_range);
8334               if (lh->num_file_names < file || file == 0)
8335                 dwarf2_debug_line_missing_file_complaint ();
8336               /* For now we ignore lines not starting on an
8337                  instruction boundary.  */
8338               else if (op_index == 0)
8339                 {
8340                   lh->file_names[file - 1].included_p = 1;
8341                   if (!decode_for_pst_p && is_stmt)
8342                     {
8343                       if (last_subfile != current_subfile)
8344                         {
8345                           addr = gdbarch_addr_bits_remove (gdbarch, address);
8346                           if (last_subfile)
8347                             record_line (last_subfile, 0, addr);
8348                           last_subfile = current_subfile;
8349                         }
8350                       /* Append row to matrix using current values.  */
8351                       addr = check_cu_functions (address, cu);
8352                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
8353                       record_line (current_subfile, line, addr);
8354                     }
8355                 }
8356               basic_block = 0;
8357             }
8358           else switch (op_code)
8359             {
8360             case DW_LNS_extended_op:
8361               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8362               line_ptr += bytes_read;
8363               extended_end = line_ptr + extended_len;
8364               extended_op = read_1_byte (abfd, line_ptr);
8365               line_ptr += 1;
8366               switch (extended_op)
8367                 {
8368                 case DW_LNE_end_sequence:
8369                   end_sequence = 1;
8370                   break;
8371                 case DW_LNE_set_address:
8372                   address = read_address (abfd, line_ptr, cu, &bytes_read);
8373                   op_index = 0;
8374                   line_ptr += bytes_read;
8375                   address += baseaddr;
8376                   break;
8377                 case DW_LNE_define_file:
8378                   {
8379                     char *cur_file;
8380                     unsigned int dir_index, mod_time, length;
8381
8382                     cur_file = read_string (abfd, line_ptr, &bytes_read);
8383                     line_ptr += bytes_read;
8384                     dir_index =
8385                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8386                     line_ptr += bytes_read;
8387                     mod_time =
8388                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8389                     line_ptr += bytes_read;
8390                     length =
8391                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8392                     line_ptr += bytes_read;
8393                     add_file_name (lh, cur_file, dir_index, mod_time, length);
8394                   }
8395                   break;
8396                 case DW_LNE_set_discriminator:
8397                   /* The discriminator is not interesting to the debugger;
8398                      just ignore it.  */
8399                   line_ptr = extended_end;
8400                   break;
8401                 default:
8402                   complaint (&symfile_complaints,
8403                              _("mangled .debug_line section"));
8404                   return;
8405                 }
8406               /* Make sure that we parsed the extended op correctly.  If e.g.
8407                  we expected a different address size than the producer used,
8408                  we may have read the wrong number of bytes.  */
8409               if (line_ptr != extended_end)
8410                 {
8411                   complaint (&symfile_complaints,
8412                              _("mangled .debug_line section"));
8413                   return;
8414                 }
8415               break;
8416             case DW_LNS_copy:
8417               if (lh->num_file_names < file || file == 0)
8418                 dwarf2_debug_line_missing_file_complaint ();
8419               else
8420                 {
8421                   lh->file_names[file - 1].included_p = 1;
8422                   if (!decode_for_pst_p && is_stmt)
8423                     {
8424                       if (last_subfile != current_subfile)
8425                         {
8426                           addr = gdbarch_addr_bits_remove (gdbarch, address);
8427                           if (last_subfile)
8428                             record_line (last_subfile, 0, addr);
8429                           last_subfile = current_subfile;
8430                         }
8431                       addr = check_cu_functions (address, cu);
8432                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
8433                       record_line (current_subfile, line, addr);
8434                     }
8435                 }
8436               basic_block = 0;
8437               break;
8438             case DW_LNS_advance_pc:
8439               {
8440                 CORE_ADDR adjust
8441                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8442
8443                 address += (((op_index + adjust)
8444                              / lh->maximum_ops_per_instruction)
8445                             * lh->minimum_instruction_length);
8446                 op_index = ((op_index + adjust)
8447                             % lh->maximum_ops_per_instruction);
8448                 line_ptr += bytes_read;
8449               }
8450               break;
8451             case DW_LNS_advance_line:
8452               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8453               line_ptr += bytes_read;
8454               break;
8455             case DW_LNS_set_file:
8456               {
8457                 /* The arrays lh->include_dirs and lh->file_names are
8458                    0-based, but the directory and file name numbers in
8459                    the statement program are 1-based.  */
8460                 struct file_entry *fe;
8461                 char *dir = NULL;
8462
8463                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8464                 line_ptr += bytes_read;
8465                 if (lh->num_file_names < file || file == 0)
8466                   dwarf2_debug_line_missing_file_complaint ();
8467                 else
8468                   {
8469                     fe = &lh->file_names[file - 1];
8470                     if (fe->dir_index)
8471                       dir = lh->include_dirs[fe->dir_index - 1];
8472                     if (!decode_for_pst_p)
8473                       {
8474                         last_subfile = current_subfile;
8475                         dwarf2_start_subfile (fe->name, dir, comp_dir);
8476                       }
8477                   }
8478               }
8479               break;
8480             case DW_LNS_set_column:
8481               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8482               line_ptr += bytes_read;
8483               break;
8484             case DW_LNS_negate_stmt:
8485               is_stmt = (!is_stmt);
8486               break;
8487             case DW_LNS_set_basic_block:
8488               basic_block = 1;
8489               break;
8490             /* Add to the address register of the state machine the
8491                address increment value corresponding to special opcode
8492                255.  I.e., this value is scaled by the minimum
8493                instruction length since special opcode 255 would have
8494                scaled the the increment.  */
8495             case DW_LNS_const_add_pc:
8496               {
8497                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
8498
8499                 address += (((op_index + adjust)
8500                              / lh->maximum_ops_per_instruction)
8501                             * lh->minimum_instruction_length);
8502                 op_index = ((op_index + adjust)
8503                             % lh->maximum_ops_per_instruction);
8504               }
8505               break;
8506             case DW_LNS_fixed_advance_pc:
8507               address += read_2_bytes (abfd, line_ptr);
8508               op_index = 0;
8509               line_ptr += 2;
8510               break;
8511             default:
8512               {
8513                 /* Unknown standard opcode, ignore it.  */
8514                 int i;
8515
8516                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
8517                   {
8518                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8519                     line_ptr += bytes_read;
8520                   }
8521               }
8522             }
8523         }
8524       if (lh->num_file_names < file || file == 0)
8525         dwarf2_debug_line_missing_file_complaint ();
8526       else
8527         {
8528           lh->file_names[file - 1].included_p = 1;
8529           if (!decode_for_pst_p)
8530             {
8531               addr = gdbarch_addr_bits_remove (gdbarch, address);
8532               record_line (current_subfile, 0, addr);
8533             }
8534         }
8535     }
8536
8537   if (decode_for_pst_p)
8538     {
8539       int file_index;
8540
8541       /* Now that we're done scanning the Line Header Program, we can
8542          create the psymtab of each included file.  */
8543       for (file_index = 0; file_index < lh->num_file_names; file_index++)
8544         if (lh->file_names[file_index].included_p == 1)
8545           {
8546             const struct file_entry fe = lh->file_names [file_index];
8547             char *include_name = fe.name;
8548             char *dir_name = NULL;
8549             char *pst_filename = pst->filename;
8550
8551             if (fe.dir_index)
8552               dir_name = lh->include_dirs[fe.dir_index - 1];
8553
8554             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8555               {
8556                 include_name = concat (dir_name, SLASH_STRING,
8557                                        include_name, (char *)NULL);
8558                 make_cleanup (xfree, include_name);
8559               }
8560
8561             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8562               {
8563                 pst_filename = concat (pst->dirname, SLASH_STRING,
8564                                        pst_filename, (char *)NULL);
8565                 make_cleanup (xfree, pst_filename);
8566               }
8567
8568             if (strcmp (include_name, pst_filename) != 0)
8569               dwarf2_create_include_psymtab (include_name, pst, objfile);
8570           }
8571     }
8572   else
8573     {
8574       /* Make sure a symtab is created for every file, even files
8575          which contain only variables (i.e. no code with associated
8576          line numbers).  */
8577
8578       int i;
8579       struct file_entry *fe;
8580
8581       for (i = 0; i < lh->num_file_names; i++)
8582         {
8583           char *dir = NULL;
8584
8585           fe = &lh->file_names[i];
8586           if (fe->dir_index)
8587             dir = lh->include_dirs[fe->dir_index - 1];
8588           dwarf2_start_subfile (fe->name, dir, comp_dir);
8589
8590           /* Skip the main file; we don't need it, and it must be
8591              allocated last, so that it will show up before the
8592              non-primary symtabs in the objfile's symtab list.  */
8593           if (current_subfile == first_subfile)
8594             continue;
8595
8596           if (current_subfile->symtab == NULL)
8597             current_subfile->symtab = allocate_symtab (current_subfile->name,
8598                                                        cu->objfile);
8599           fe->symtab = current_subfile->symtab;
8600         }
8601     }
8602 }
8603
8604 /* Start a subfile for DWARF.  FILENAME is the name of the file and
8605    DIRNAME the name of the source directory which contains FILENAME
8606    or NULL if not known.  COMP_DIR is the compilation directory for the
8607    linetable's compilation unit or NULL if not known.
8608    This routine tries to keep line numbers from identical absolute and
8609    relative file names in a common subfile.
8610
8611    Using the `list' example from the GDB testsuite, which resides in
8612    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8613    of /srcdir/list0.c yields the following debugging information for list0.c:
8614
8615    DW_AT_name:          /srcdir/list0.c
8616    DW_AT_comp_dir:              /compdir
8617    files.files[0].name: list0.h
8618    files.files[0].dir:  /srcdir
8619    files.files[1].name: list0.c
8620    files.files[1].dir:  /srcdir
8621
8622    The line number information for list0.c has to end up in a single
8623    subfile, so that `break /srcdir/list0.c:1' works as expected.
8624    start_subfile will ensure that this happens provided that we pass the
8625    concatenation of files.files[1].dir and files.files[1].name as the
8626    subfile's name.  */
8627
8628 static void
8629 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
8630 {
8631   char *fullname;
8632
8633   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8634      `start_symtab' will always pass the contents of DW_AT_comp_dir as
8635      second argument to start_subfile.  To be consistent, we do the
8636      same here.  In order not to lose the line information directory,
8637      we concatenate it to the filename when it makes sense.
8638      Note that the Dwarf3 standard says (speaking of filenames in line
8639      information): ``The directory index is ignored for file names
8640      that represent full path names''.  Thus ignoring dirname in the
8641      `else' branch below isn't an issue.  */
8642
8643   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
8644     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8645   else
8646     fullname = filename;
8647
8648   start_subfile (fullname, comp_dir);
8649
8650   if (fullname != filename)
8651     xfree (fullname);
8652 }
8653
8654 static void
8655 var_decode_location (struct attribute *attr, struct symbol *sym,
8656                      struct dwarf2_cu *cu)
8657 {
8658   struct objfile *objfile = cu->objfile;
8659   struct comp_unit_head *cu_header = &cu->header;
8660
8661   /* NOTE drow/2003-01-30: There used to be a comment and some special
8662      code here to turn a symbol with DW_AT_external and a
8663      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
8664      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8665      with some versions of binutils) where shared libraries could have
8666      relocations against symbols in their debug information - the
8667      minimal symbol would have the right address, but the debug info
8668      would not.  It's no longer necessary, because we will explicitly
8669      apply relocations when we read in the debug information now.  */
8670
8671   /* A DW_AT_location attribute with no contents indicates that a
8672      variable has been optimized away.  */
8673   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8674     {
8675       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8676       return;
8677     }
8678
8679   /* Handle one degenerate form of location expression specially, to
8680      preserve GDB's previous behavior when section offsets are
8681      specified.  If this is just a DW_OP_addr then mark this symbol
8682      as LOC_STATIC.  */
8683
8684   if (attr_form_is_block (attr)
8685       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8686       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8687     {
8688       unsigned int dummy;
8689
8690       SYMBOL_VALUE_ADDRESS (sym) =
8691         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
8692       SYMBOL_CLASS (sym) = LOC_STATIC;
8693       fixup_symbol_section (sym, objfile);
8694       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8695                                               SYMBOL_SECTION (sym));
8696       return;
8697     }
8698
8699   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8700      expression evaluator, and use LOC_COMPUTED only when necessary
8701      (i.e. when the value of a register or memory location is
8702      referenced, or a thread-local block, etc.).  Then again, it might
8703      not be worthwhile.  I'm assuming that it isn't unless performance
8704      or memory numbers show me otherwise.  */
8705
8706   dwarf2_symbol_mark_computed (attr, sym, cu);
8707   SYMBOL_CLASS (sym) = LOC_COMPUTED;
8708 }
8709
8710 /* Given a pointer to a DWARF information entry, figure out if we need
8711    to make a symbol table entry for it, and if so, create a new entry
8712    and return a pointer to it.
8713    If TYPE is NULL, determine symbol type from the die, otherwise
8714    used the passed type.  */
8715
8716 static struct symbol *
8717 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
8718 {
8719   struct objfile *objfile = cu->objfile;
8720   struct symbol *sym = NULL;
8721   char *name;
8722   struct attribute *attr = NULL;
8723   struct attribute *attr2 = NULL;
8724   CORE_ADDR baseaddr;
8725   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8726
8727   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8728
8729   name = dwarf2_name (die, cu);
8730   if (name)
8731     {
8732       const char *linkagename;
8733
8734       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
8735                                              sizeof (struct symbol));
8736       OBJSTAT (objfile, n_syms++);
8737       memset (sym, 0, sizeof (struct symbol));
8738
8739       /* Cache this symbol's name and the name's demangled form (if any).  */
8740       SYMBOL_LANGUAGE (sym) = cu->language;
8741       linkagename = dwarf2_physname (name, die, cu);
8742       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
8743
8744       /* Fortran does not have mangling standard and the mangling does differ
8745          between gfortran, iFort etc.  */
8746       if (cu->language == language_fortran
8747           && sym->ginfo.language_specific.cplus_specific.demangled_name == NULL)
8748         sym->ginfo.language_specific.cplus_specific.demangled_name
8749           = (char *) dwarf2_full_name (name, die, cu);
8750
8751       /* Default assumptions.
8752          Use the passed type or decode it from the die.  */
8753       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8754       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8755       if (type != NULL)
8756         SYMBOL_TYPE (sym) = type;
8757       else
8758         SYMBOL_TYPE (sym) = die_type (die, cu);
8759       attr = dwarf2_attr (die,
8760                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8761                           cu);
8762       if (attr)
8763         {
8764           SYMBOL_LINE (sym) = DW_UNSND (attr);
8765         }
8766
8767       attr = dwarf2_attr (die,
8768                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8769                           cu);
8770       if (attr)
8771         {
8772           int file_index = DW_UNSND (attr);
8773
8774           if (cu->line_header == NULL
8775               || file_index > cu->line_header->num_file_names)
8776             complaint (&symfile_complaints,
8777                        _("file index out of range"));
8778           else if (file_index > 0)
8779             {
8780               struct file_entry *fe;
8781
8782               fe = &cu->line_header->file_names[file_index - 1];
8783               SYMBOL_SYMTAB (sym) = fe->symtab;
8784             }
8785         }
8786
8787       switch (die->tag)
8788         {
8789         case DW_TAG_label:
8790           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8791           if (attr)
8792             {
8793               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8794             }
8795           SYMBOL_CLASS (sym) = LOC_LABEL;
8796           break;
8797         case DW_TAG_subprogram:
8798           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8799              finish_block.  */
8800           SYMBOL_CLASS (sym) = LOC_BLOCK;
8801           attr2 = dwarf2_attr (die, DW_AT_external, cu);
8802           if ((attr2 && (DW_UNSND (attr2) != 0))
8803               || cu->language == language_ada)
8804             {
8805               /* Subprograms marked external are stored as a global symbol.
8806                  Ada subprograms, whether marked external or not, are always
8807                  stored as a global symbol, because we want to be able to
8808                  access them globally.  For instance, we want to be able
8809                  to break on a nested subprogram without having to
8810                  specify the context.  */
8811               add_symbol_to_list (sym, &global_symbols);
8812             }
8813           else
8814             {
8815               add_symbol_to_list (sym, cu->list_in_scope);
8816             }
8817           break;
8818         case DW_TAG_inlined_subroutine:
8819           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8820              finish_block.  */
8821           SYMBOL_CLASS (sym) = LOC_BLOCK;
8822           SYMBOL_INLINED (sym) = 1;
8823           /* Do not add the symbol to any lists.  It will be found via
8824              BLOCK_FUNCTION from the blockvector.  */
8825           break;
8826         case DW_TAG_variable:
8827           /* Compilation with minimal debug info may result in variables
8828              with missing type entries. Change the misleading `void' type
8829              to something sensible.  */
8830           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
8831             SYMBOL_TYPE (sym)
8832               = objfile_type (objfile)->nodebug_data_symbol;
8833
8834           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8835           if (attr)
8836             {
8837               dwarf2_const_value (attr, sym, cu);
8838               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8839               if (attr2 && (DW_UNSND (attr2) != 0))
8840                 add_symbol_to_list (sym, &global_symbols);
8841               else
8842                 add_symbol_to_list (sym, cu->list_in_scope);
8843               break;
8844             }
8845           attr = dwarf2_attr (die, DW_AT_location, cu);
8846           if (attr)
8847             {
8848               var_decode_location (attr, sym, cu);
8849               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8850               if (attr2 && (DW_UNSND (attr2) != 0))
8851                 {
8852                   struct pending **list_to_add;
8853
8854                   /* Workaround gfortran PR debug/40040 - it uses
8855                      DW_AT_location for variables in -fPIC libraries which may
8856                      get overriden by other libraries/executable and get
8857                      a different address.  Resolve it by the minimal symbol
8858                      which may come from inferior's executable using copy
8859                      relocation.  Make this workaround only for gfortran as for
8860                      other compilers GDB cannot guess the minimal symbol
8861                      Fortran mangling kind.  */
8862                   if (cu->language == language_fortran && die->parent
8863                       && die->parent->tag == DW_TAG_module
8864                       && cu->producer
8865                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
8866                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
8867
8868                   /* A variable with DW_AT_external is never static,
8869                      but it may be block-scoped.  */
8870                   list_to_add = (cu->list_in_scope == &file_symbols
8871                                  ? &global_symbols : cu->list_in_scope);
8872                   add_symbol_to_list (sym, list_to_add);
8873                 }
8874               else
8875                 add_symbol_to_list (sym, cu->list_in_scope);
8876             }
8877           else
8878             {
8879               /* We do not know the address of this symbol.
8880                  If it is an external symbol and we have type information
8881                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
8882                  The address of the variable will then be determined from
8883                  the minimal symbol table whenever the variable is
8884                  referenced.  */
8885               attr2 = dwarf2_attr (die, DW_AT_external, cu);
8886               if (attr2 && (DW_UNSND (attr2) != 0)
8887                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
8888                 {
8889                   struct pending **list_to_add;
8890
8891                   /* A variable with DW_AT_external is never static, but it
8892                      may be block-scoped.  */
8893                   list_to_add = (cu->list_in_scope == &file_symbols
8894                                  ? &global_symbols : cu->list_in_scope);
8895
8896                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
8897                   add_symbol_to_list (sym, list_to_add);
8898                 }
8899               else if (!die_is_declaration (die, cu))
8900                 {
8901                   /* Use the default LOC_OPTIMIZED_OUT class.  */
8902                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8903                   add_symbol_to_list (sym, cu->list_in_scope);
8904                 }
8905             }
8906           break;
8907         case DW_TAG_formal_parameter:
8908           /* If we are inside a function, mark this as an argument.  If
8909              not, we might be looking at an argument to an inlined function
8910              when we do not have enough information to show inlined frames;
8911              pretend it's a local variable in that case so that the user can
8912              still see it.  */
8913           if (context_stack_depth > 0
8914               && context_stack[context_stack_depth - 1].name != NULL)
8915             SYMBOL_IS_ARGUMENT (sym) = 1;
8916           attr = dwarf2_attr (die, DW_AT_location, cu);
8917           if (attr)
8918             {
8919               var_decode_location (attr, sym, cu);
8920             }
8921           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8922           if (attr)
8923             {
8924               dwarf2_const_value (attr, sym, cu);
8925             }
8926           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
8927           if (attr && DW_UNSND (attr))
8928             {
8929               struct type *ref_type;
8930
8931               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
8932               SYMBOL_TYPE (sym) = ref_type;
8933             }
8934
8935           add_symbol_to_list (sym, cu->list_in_scope);
8936           break;
8937         case DW_TAG_unspecified_parameters:
8938           /* From varargs functions; gdb doesn't seem to have any
8939              interest in this information, so just ignore it for now.
8940              (FIXME?) */
8941           break;
8942         case DW_TAG_class_type:
8943         case DW_TAG_interface_type:
8944         case DW_TAG_structure_type:
8945         case DW_TAG_union_type:
8946         case DW_TAG_set_type:
8947         case DW_TAG_enumeration_type:
8948           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8949           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8950
8951           {
8952             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
8953                really ever be static objects: otherwise, if you try
8954                to, say, break of a class's method and you're in a file
8955                which doesn't mention that class, it won't work unless
8956                the check for all static symbols in lookup_symbol_aux
8957                saves you.  See the OtherFileClass tests in
8958                gdb.c++/namespace.exp.  */
8959
8960             struct pending **list_to_add;
8961
8962             list_to_add = (cu->list_in_scope == &file_symbols
8963                            && (cu->language == language_cplus
8964                                || cu->language == language_java)
8965                            ? &global_symbols : cu->list_in_scope);
8966
8967             add_symbol_to_list (sym, list_to_add);
8968
8969             /* The semantics of C++ state that "struct foo { ... }" also
8970                defines a typedef for "foo".  A Java class declaration also
8971                defines a typedef for the class.  */
8972             if (cu->language == language_cplus
8973                 || cu->language == language_java
8974                 || cu->language == language_ada)
8975               {
8976                 /* The symbol's name is already allocated along with
8977                    this objfile, so we don't need to duplicate it for
8978                    the type.  */
8979                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
8980                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
8981               }
8982           }
8983           break;
8984         case DW_TAG_typedef:
8985           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8986           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8987           add_symbol_to_list (sym, cu->list_in_scope);
8988           break;
8989         case DW_TAG_base_type:
8990         case DW_TAG_subrange_type:
8991         case DW_TAG_const_type:
8992         case DW_TAG_volatile_type:
8993           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8994           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8995           add_symbol_to_list (sym, cu->list_in_scope);
8996           break;
8997         case DW_TAG_enumerator:
8998           attr = dwarf2_attr (die, DW_AT_const_value, cu);
8999           if (attr)
9000             {
9001               dwarf2_const_value (attr, sym, cu);
9002             }
9003           {
9004             /* NOTE: carlton/2003-11-10: See comment above in the
9005                DW_TAG_class_type, etc. block.  */
9006
9007             struct pending **list_to_add;
9008
9009             list_to_add = (cu->list_in_scope == &file_symbols
9010                            && (cu->language == language_cplus
9011                                || cu->language == language_java)
9012                            ? &global_symbols : cu->list_in_scope);
9013
9014             add_symbol_to_list (sym, list_to_add);
9015           }
9016           break;
9017         case DW_TAG_namespace:
9018           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
9019           add_symbol_to_list (sym, &global_symbols);
9020           break;
9021         default:
9022           /* Not a tag we recognize.  Hopefully we aren't processing
9023              trash data, but since we must specifically ignore things
9024              we don't recognize, there is nothing else we should do at
9025              this point. */
9026           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
9027                      dwarf_tag_name (die->tag));
9028           break;
9029         }
9030
9031       /* For the benefit of old versions of GCC, check for anonymous
9032          namespaces based on the demangled name.  */
9033       if (!processing_has_namespace_info
9034           && cu->language == language_cplus)
9035         cp_scan_for_anonymous_namespaces (sym);
9036     }
9037   return (sym);
9038 }
9039
9040 /* Copy constant value from an attribute to a symbol.  */
9041
9042 static void
9043 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
9044                     struct dwarf2_cu *cu)
9045 {
9046   struct objfile *objfile = cu->objfile;
9047   struct comp_unit_head *cu_header = &cu->header;
9048   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
9049                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
9050   struct dwarf_block *blk;
9051
9052   switch (attr->form)
9053     {
9054     case DW_FORM_addr:
9055       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
9056         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
9057                                                       cu_header->addr_size,
9058                                                       TYPE_LENGTH (SYMBOL_TYPE
9059                                                                    (sym)));
9060       SYMBOL_VALUE_BYTES (sym) =
9061         obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
9062       /* NOTE: cagney/2003-05-09: In-lined store_address call with
9063          it's body - store_unsigned_integer.  */
9064       store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
9065                               byte_order, DW_ADDR (attr));
9066       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
9067       break;
9068     case DW_FORM_string:
9069     case DW_FORM_strp:
9070       /* DW_STRING is already allocated on the obstack, point directly
9071          to it.  */
9072       SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
9073       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
9074       break;
9075     case DW_FORM_block1:
9076     case DW_FORM_block2:
9077     case DW_FORM_block4:
9078     case DW_FORM_block:
9079     case DW_FORM_exprloc:
9080       blk = DW_BLOCK (attr);
9081       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
9082         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
9083                                                       blk->size,
9084                                                       TYPE_LENGTH (SYMBOL_TYPE
9085                                                                    (sym)));
9086       SYMBOL_VALUE_BYTES (sym) =
9087         obstack_alloc (&objfile->objfile_obstack, blk->size);
9088       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
9089       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
9090       break;
9091
9092       /* The DW_AT_const_value attributes are supposed to carry the
9093          symbol's value "represented as it would be on the target
9094          architecture."  By the time we get here, it's already been
9095          converted to host endianness, so we just need to sign- or
9096          zero-extend it as appropriate.  */
9097     case DW_FORM_data1:
9098       dwarf2_const_value_data (attr, sym, 8);
9099       break;
9100     case DW_FORM_data2:
9101       dwarf2_const_value_data (attr, sym, 16);
9102       break;
9103     case DW_FORM_data4:
9104       dwarf2_const_value_data (attr, sym, 32);
9105       break;
9106     case DW_FORM_data8:
9107       dwarf2_const_value_data (attr, sym, 64);
9108       break;
9109
9110     case DW_FORM_sdata:
9111       SYMBOL_VALUE (sym) = DW_SND (attr);
9112       SYMBOL_CLASS (sym) = LOC_CONST;
9113       break;
9114
9115     case DW_FORM_udata:
9116       SYMBOL_VALUE (sym) = DW_UNSND (attr);
9117       SYMBOL_CLASS (sym) = LOC_CONST;
9118       break;
9119
9120     default:
9121       complaint (&symfile_complaints,
9122                  _("unsupported const value attribute form: '%s'"),
9123                  dwarf_form_name (attr->form));
9124       SYMBOL_VALUE (sym) = 0;
9125       SYMBOL_CLASS (sym) = LOC_CONST;
9126       break;
9127     }
9128 }
9129
9130
9131 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
9132    or zero-extend it as appropriate for the symbol's type.  */
9133 static void
9134 dwarf2_const_value_data (struct attribute *attr,
9135                          struct symbol *sym,
9136                          int bits)
9137 {
9138   LONGEST l = DW_UNSND (attr);
9139
9140   if (bits < sizeof (l) * 8)
9141     {
9142       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
9143         l &= ((LONGEST) 1 << bits) - 1;
9144       else
9145         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
9146     }
9147
9148   SYMBOL_VALUE (sym) = l;
9149   SYMBOL_CLASS (sym) = LOC_CONST;
9150 }
9151
9152
9153 /* Return the type of the die in question using its DW_AT_type attribute.  */
9154
9155 static struct type *
9156 die_type (struct die_info *die, struct dwarf2_cu *cu)
9157 {
9158   struct attribute *type_attr;
9159   struct die_info *type_die;
9160
9161   type_attr = dwarf2_attr (die, DW_AT_type, cu);
9162   if (!type_attr)
9163     {
9164       /* A missing DW_AT_type represents a void type.  */
9165       return objfile_type (cu->objfile)->builtin_void;
9166     }
9167
9168   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
9169
9170   return tag_type_to_type (type_die, cu);
9171 }
9172
9173 /* True iff CU's producer generates GNAT Ada auxiliary information
9174    that allows to find parallel types through that information instead
9175    of having to do expensive parallel lookups by type name.  */
9176
9177 static int
9178 need_gnat_info (struct dwarf2_cu *cu)
9179 {
9180   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
9181      of GNAT produces this auxiliary information, without any indication
9182      that it is produced.  Part of enhancing the FSF version of GNAT
9183      to produce that information will be to put in place an indicator
9184      that we can use in order to determine whether the descriptive type
9185      info is available or not.  One suggestion that has been made is
9186      to use a new attribute, attached to the CU die.  For now, assume
9187      that the descriptive type info is not available.  */
9188   return 0;
9189 }
9190
9191
9192 /* Return the auxiliary type of the die in question using its
9193    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
9194    attribute is not present.  */
9195
9196 static struct type *
9197 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
9198 {
9199   struct attribute *type_attr;
9200   struct die_info *type_die;
9201
9202   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
9203   if (!type_attr)
9204     return NULL;
9205
9206   type_die = follow_die_ref (die, type_attr, &cu);
9207   return tag_type_to_type (type_die, cu);
9208 }
9209
9210 /* If DIE has a descriptive_type attribute, then set the TYPE's
9211    descriptive type accordingly.  */
9212
9213 static void
9214 set_descriptive_type (struct type *type, struct die_info *die,
9215                       struct dwarf2_cu *cu)
9216 {
9217   struct type *descriptive_type = die_descriptive_type (die, cu);
9218
9219   if (descriptive_type)
9220     {
9221       ALLOCATE_GNAT_AUX_TYPE (type);
9222       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
9223     }
9224 }
9225
9226 /* Return the containing type of the die in question using its
9227    DW_AT_containing_type attribute.  */
9228
9229 static struct type *
9230 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
9231 {
9232   struct attribute *type_attr;
9233   struct die_info *type_die;
9234
9235   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
9236   if (!type_attr)
9237     error (_("Dwarf Error: Problem turning containing type into gdb type "
9238              "[in module %s]"), cu->objfile->name);
9239
9240   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
9241   return tag_type_to_type (type_die, cu);
9242 }
9243
9244 static struct type *
9245 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
9246 {
9247   struct type *this_type;
9248
9249   this_type = read_type_die (die, cu);
9250   if (!this_type)
9251     {
9252       char *message, *saved;
9253
9254       /* read_type_die already issued a complaint.  */
9255       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
9256                             cu->objfile->name,
9257                             cu->header.offset,
9258                             die->offset);
9259       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
9260                              message, strlen (message));
9261       xfree (message);
9262
9263       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
9264     }
9265   return this_type;
9266 }
9267
9268 static struct type *
9269 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
9270 {
9271   struct type *this_type;
9272
9273   this_type = get_die_type (die, cu);
9274   if (this_type)
9275     return this_type;
9276
9277   switch (die->tag)
9278     {
9279     case DW_TAG_class_type:
9280     case DW_TAG_interface_type:
9281     case DW_TAG_structure_type:
9282     case DW_TAG_union_type:
9283       this_type = read_structure_type (die, cu);
9284       break;
9285     case DW_TAG_enumeration_type:
9286       this_type = read_enumeration_type (die, cu);
9287       break;
9288     case DW_TAG_subprogram:
9289     case DW_TAG_subroutine_type:
9290     case DW_TAG_inlined_subroutine:
9291       this_type = read_subroutine_type (die, cu);
9292       break;
9293     case DW_TAG_array_type:
9294       this_type = read_array_type (die, cu);
9295       break;
9296     case DW_TAG_set_type:
9297       this_type = read_set_type (die, cu);
9298       break;
9299     case DW_TAG_pointer_type:
9300       this_type = read_tag_pointer_type (die, cu);
9301       break;
9302     case DW_TAG_ptr_to_member_type:
9303       this_type = read_tag_ptr_to_member_type (die, cu);
9304       break;
9305     case DW_TAG_reference_type:
9306       this_type = read_tag_reference_type (die, cu);
9307       break;
9308     case DW_TAG_const_type:
9309       this_type = read_tag_const_type (die, cu);
9310       break;
9311     case DW_TAG_volatile_type:
9312       this_type = read_tag_volatile_type (die, cu);
9313       break;
9314     case DW_TAG_string_type:
9315       this_type = read_tag_string_type (die, cu);
9316       break;
9317     case DW_TAG_typedef:
9318       this_type = read_typedef (die, cu);
9319       break;
9320     case DW_TAG_subrange_type:
9321       this_type = read_subrange_type (die, cu);
9322       break;
9323     case DW_TAG_base_type:
9324       this_type = read_base_type (die, cu);
9325       break;
9326     case DW_TAG_unspecified_type:
9327       this_type = read_unspecified_type (die, cu);
9328       break;
9329     case DW_TAG_namespace:
9330       this_type = read_namespace_type (die, cu);
9331       break;
9332     case DW_TAG_module:
9333       this_type = read_module_type (die, cu);
9334       break;
9335     default:
9336       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
9337                  dwarf_tag_name (die->tag));
9338       break;
9339     }
9340
9341   return this_type;
9342 }
9343
9344 /* Return the name of the namespace/class that DIE is defined within,
9345    or "" if we can't tell.  The caller should not xfree the result.
9346
9347    For example, if we're within the method foo() in the following
9348    code:
9349
9350    namespace N {
9351      class C {
9352        void foo () {
9353        }
9354      };
9355    }
9356
9357    then determine_prefix on foo's die will return "N::C".  */
9358
9359 static char *
9360 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
9361 {
9362   struct die_info *parent, *spec_die;
9363   struct dwarf2_cu *spec_cu;
9364   struct type *parent_type;
9365
9366   if (cu->language != language_cplus && cu->language != language_java
9367       && cu->language != language_fortran)
9368     return "";
9369
9370   /* We have to be careful in the presence of DW_AT_specification.
9371      For example, with GCC 3.4, given the code
9372
9373      namespace N {
9374        void foo() {
9375          // Definition of N::foo.
9376        }
9377      }
9378
9379      then we'll have a tree of DIEs like this:
9380
9381      1: DW_TAG_compile_unit
9382        2: DW_TAG_namespace        // N
9383          3: DW_TAG_subprogram     // declaration of N::foo
9384        4: DW_TAG_subprogram       // definition of N::foo
9385             DW_AT_specification   // refers to die #3
9386
9387      Thus, when processing die #4, we have to pretend that we're in
9388      the context of its DW_AT_specification, namely the contex of die
9389      #3.  */
9390   spec_cu = cu;
9391   spec_die = die_specification (die, &spec_cu);
9392   if (spec_die == NULL)
9393     parent = die->parent;
9394   else
9395     {
9396       parent = spec_die->parent;
9397       cu = spec_cu;
9398     }
9399
9400   if (parent == NULL)
9401     return "";
9402   else
9403     switch (parent->tag)
9404       {
9405       case DW_TAG_namespace:
9406         parent_type = read_type_die (parent, cu);
9407         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9408            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9409            Work around this problem here.  */
9410         if (cu->language == language_cplus
9411             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
9412           return "";
9413         /* We give a name to even anonymous namespaces.  */
9414         return TYPE_TAG_NAME (parent_type);
9415       case DW_TAG_class_type:
9416       case DW_TAG_interface_type:
9417       case DW_TAG_structure_type:
9418       case DW_TAG_union_type:
9419       case DW_TAG_module:
9420         parent_type = read_type_die (parent, cu);
9421         if (TYPE_TAG_NAME (parent_type) != NULL)
9422           return TYPE_TAG_NAME (parent_type);
9423         else
9424           /* An anonymous structure is only allowed non-static data
9425              members; no typedefs, no member functions, et cetera.
9426              So it does not need a prefix.  */
9427           return "";
9428       default:
9429         return determine_prefix (parent, cu);
9430       }
9431 }
9432
9433 /* Return a newly-allocated string formed by concatenating PREFIX and
9434    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
9435    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
9436    perform an obconcat, otherwise allocate storage for the result.  The CU argument
9437    is used to determine the language and hence, the appropriate separator.  */
9438
9439 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
9440
9441 static char *
9442 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
9443                  int physname, struct dwarf2_cu *cu)
9444 {
9445   const char *lead = "";
9446   const char *sep;
9447
9448   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
9449     sep = "";
9450   else if (cu->language == language_java)
9451     sep = ".";
9452   else if (cu->language == language_fortran && physname)
9453     {
9454       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
9455          DW_AT_MIPS_linkage_name is preferred and used instead.  */
9456
9457       lead = "__";
9458       sep = "_MOD_";
9459     }
9460   else
9461     sep = "::";
9462
9463   if (prefix == NULL)
9464     prefix = "";
9465   if (suffix == NULL)
9466     suffix = "";
9467
9468   if (obs == NULL)
9469     {
9470       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9471
9472       strcpy (retval, lead);
9473       strcat (retval, prefix);
9474       strcat (retval, sep);
9475       strcat (retval, suffix);
9476       return retval;
9477     }
9478   else
9479     {
9480       /* We have an obstack.  */
9481       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
9482     }
9483 }
9484
9485 /* Return sibling of die, NULL if no sibling.  */
9486
9487 static struct die_info *
9488 sibling_die (struct die_info *die)
9489 {
9490   return die->sibling;
9491 }
9492
9493 /* Get name of a die, return NULL if not found.  */
9494
9495 static char *
9496 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
9497                           struct obstack *obstack)
9498 {
9499   if (name && cu->language == language_cplus)
9500     {
9501       char *canon_name = cp_canonicalize_string (name);
9502
9503       if (canon_name != NULL)
9504         {
9505           if (strcmp (canon_name, name) != 0)
9506             name = obsavestring (canon_name, strlen (canon_name),
9507                                  obstack);
9508           xfree (canon_name);
9509         }
9510     }
9511
9512   return name;
9513 }
9514
9515 /* Get name of a die, return NULL if not found.  */
9516
9517 static char *
9518 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9519 {
9520   struct attribute *attr;
9521
9522   attr = dwarf2_attr (die, DW_AT_name, cu);
9523   if (!attr || !DW_STRING (attr))
9524     return NULL;
9525
9526   switch (die->tag)
9527     {
9528     case DW_TAG_compile_unit:
9529       /* Compilation units have a DW_AT_name that is a filename, not
9530          a source language identifier.  */
9531     case DW_TAG_enumeration_type:
9532     case DW_TAG_enumerator:
9533       /* These tags always have simple identifiers already; no need
9534          to canonicalize them.  */
9535       return DW_STRING (attr);
9536
9537     case DW_TAG_subprogram:
9538       /* Java constructors will all be named "<init>", so return
9539          the class name when we see this special case.  */
9540       if (cu->language == language_java
9541           && DW_STRING (attr) != NULL
9542           && strcmp (DW_STRING (attr), "<init>") == 0)
9543         {
9544           struct dwarf2_cu *spec_cu = cu;
9545           struct die_info *spec_die;
9546
9547           /* GCJ will output '<init>' for Java constructor names.
9548              For this special case, return the name of the parent class.  */
9549
9550           /* GCJ may output suprogram DIEs with AT_specification set.
9551              If so, use the name of the specified DIE.  */
9552           spec_die = die_specification (die, &spec_cu);
9553           if (spec_die != NULL)
9554             return dwarf2_name (spec_die, spec_cu);
9555
9556           do
9557             {
9558               die = die->parent;
9559               if (die->tag == DW_TAG_class_type)
9560                 return dwarf2_name (die, cu);
9561             }
9562           while (die->tag != DW_TAG_compile_unit);
9563         }
9564       break;
9565
9566     case DW_TAG_class_type:
9567     case DW_TAG_interface_type:
9568     case DW_TAG_structure_type:
9569     case DW_TAG_union_type:
9570       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
9571          structures or unions.  These were of the form "._%d" in GCC 4.1,
9572          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
9573          and GCC 4.4.  We work around this problem by ignoring these.  */
9574       if (strncmp (DW_STRING (attr), "._", 2) == 0
9575           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
9576         return NULL;
9577       break;
9578
9579     default:
9580       break;
9581     }
9582
9583   if (!DW_STRING_IS_CANONICAL (attr))
9584     {
9585       DW_STRING (attr)
9586         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9587                                     &cu->objfile->objfile_obstack);
9588       DW_STRING_IS_CANONICAL (attr) = 1;
9589     }
9590   return DW_STRING (attr);
9591 }
9592
9593 /* Return the die that this die in an extension of, or NULL if there
9594    is none.  *EXT_CU is the CU containing DIE on input, and the CU
9595    containing the return value on output.  */
9596
9597 static struct die_info *
9598 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9599 {
9600   struct attribute *attr;
9601
9602   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9603   if (attr == NULL)
9604     return NULL;
9605
9606   return follow_die_ref (die, attr, ext_cu);
9607 }
9608
9609 /* Convert a DIE tag into its string name.  */
9610
9611 static char *
9612 dwarf_tag_name (unsigned tag)
9613 {
9614   switch (tag)
9615     {
9616     case DW_TAG_padding:
9617       return "DW_TAG_padding";
9618     case DW_TAG_array_type:
9619       return "DW_TAG_array_type";
9620     case DW_TAG_class_type:
9621       return "DW_TAG_class_type";
9622     case DW_TAG_entry_point:
9623       return "DW_TAG_entry_point";
9624     case DW_TAG_enumeration_type:
9625       return "DW_TAG_enumeration_type";
9626     case DW_TAG_formal_parameter:
9627       return "DW_TAG_formal_parameter";
9628     case DW_TAG_imported_declaration:
9629       return "DW_TAG_imported_declaration";
9630     case DW_TAG_label:
9631       return "DW_TAG_label";
9632     case DW_TAG_lexical_block:
9633       return "DW_TAG_lexical_block";
9634     case DW_TAG_member:
9635       return "DW_TAG_member";
9636     case DW_TAG_pointer_type:
9637       return "DW_TAG_pointer_type";
9638     case DW_TAG_reference_type:
9639       return "DW_TAG_reference_type";
9640     case DW_TAG_compile_unit:
9641       return "DW_TAG_compile_unit";
9642     case DW_TAG_string_type:
9643       return "DW_TAG_string_type";
9644     case DW_TAG_structure_type:
9645       return "DW_TAG_structure_type";
9646     case DW_TAG_subroutine_type:
9647       return "DW_TAG_subroutine_type";
9648     case DW_TAG_typedef:
9649       return "DW_TAG_typedef";
9650     case DW_TAG_union_type:
9651       return "DW_TAG_union_type";
9652     case DW_TAG_unspecified_parameters:
9653       return "DW_TAG_unspecified_parameters";
9654     case DW_TAG_variant:
9655       return "DW_TAG_variant";
9656     case DW_TAG_common_block:
9657       return "DW_TAG_common_block";
9658     case DW_TAG_common_inclusion:
9659       return "DW_TAG_common_inclusion";
9660     case DW_TAG_inheritance:
9661       return "DW_TAG_inheritance";
9662     case DW_TAG_inlined_subroutine:
9663       return "DW_TAG_inlined_subroutine";
9664     case DW_TAG_module:
9665       return "DW_TAG_module";
9666     case DW_TAG_ptr_to_member_type:
9667       return "DW_TAG_ptr_to_member_type";
9668     case DW_TAG_set_type:
9669       return "DW_TAG_set_type";
9670     case DW_TAG_subrange_type:
9671       return "DW_TAG_subrange_type";
9672     case DW_TAG_with_stmt:
9673       return "DW_TAG_with_stmt";
9674     case DW_TAG_access_declaration:
9675       return "DW_TAG_access_declaration";
9676     case DW_TAG_base_type:
9677       return "DW_TAG_base_type";
9678     case DW_TAG_catch_block:
9679       return "DW_TAG_catch_block";
9680     case DW_TAG_const_type:
9681       return "DW_TAG_const_type";
9682     case DW_TAG_constant:
9683       return "DW_TAG_constant";
9684     case DW_TAG_enumerator:
9685       return "DW_TAG_enumerator";
9686     case DW_TAG_file_type:
9687       return "DW_TAG_file_type";
9688     case DW_TAG_friend:
9689       return "DW_TAG_friend";
9690     case DW_TAG_namelist:
9691       return "DW_TAG_namelist";
9692     case DW_TAG_namelist_item:
9693       return "DW_TAG_namelist_item";
9694     case DW_TAG_packed_type:
9695       return "DW_TAG_packed_type";
9696     case DW_TAG_subprogram:
9697       return "DW_TAG_subprogram";
9698     case DW_TAG_template_type_param:
9699       return "DW_TAG_template_type_param";
9700     case DW_TAG_template_value_param:
9701       return "DW_TAG_template_value_param";
9702     case DW_TAG_thrown_type:
9703       return "DW_TAG_thrown_type";
9704     case DW_TAG_try_block:
9705       return "DW_TAG_try_block";
9706     case DW_TAG_variant_part:
9707       return "DW_TAG_variant_part";
9708     case DW_TAG_variable:
9709       return "DW_TAG_variable";
9710     case DW_TAG_volatile_type:
9711       return "DW_TAG_volatile_type";
9712     case DW_TAG_dwarf_procedure:
9713       return "DW_TAG_dwarf_procedure";
9714     case DW_TAG_restrict_type:
9715       return "DW_TAG_restrict_type";
9716     case DW_TAG_interface_type:
9717       return "DW_TAG_interface_type";
9718     case DW_TAG_namespace:
9719       return "DW_TAG_namespace";
9720     case DW_TAG_imported_module:
9721       return "DW_TAG_imported_module";
9722     case DW_TAG_unspecified_type:
9723       return "DW_TAG_unspecified_type";
9724     case DW_TAG_partial_unit:
9725       return "DW_TAG_partial_unit";
9726     case DW_TAG_imported_unit:
9727       return "DW_TAG_imported_unit";
9728     case DW_TAG_condition:
9729       return "DW_TAG_condition";
9730     case DW_TAG_shared_type:
9731       return "DW_TAG_shared_type";
9732     case DW_TAG_type_unit:
9733       return "DW_TAG_type_unit";
9734     case DW_TAG_MIPS_loop:
9735       return "DW_TAG_MIPS_loop";
9736     case DW_TAG_HP_array_descriptor:
9737       return "DW_TAG_HP_array_descriptor";
9738     case DW_TAG_format_label:
9739       return "DW_TAG_format_label";
9740     case DW_TAG_function_template:
9741       return "DW_TAG_function_template";
9742     case DW_TAG_class_template:
9743       return "DW_TAG_class_template";
9744     case DW_TAG_GNU_BINCL:
9745       return "DW_TAG_GNU_BINCL";
9746     case DW_TAG_GNU_EINCL:
9747       return "DW_TAG_GNU_EINCL";
9748     case DW_TAG_upc_shared_type:
9749       return "DW_TAG_upc_shared_type";
9750     case DW_TAG_upc_strict_type:
9751       return "DW_TAG_upc_strict_type";
9752     case DW_TAG_upc_relaxed_type:
9753       return "DW_TAG_upc_relaxed_type";
9754     case DW_TAG_PGI_kanji_type:
9755       return "DW_TAG_PGI_kanji_type";
9756     case DW_TAG_PGI_interface_block:
9757       return "DW_TAG_PGI_interface_block";
9758     default:
9759       return "DW_TAG_<unknown>";
9760     }
9761 }
9762
9763 /* Convert a DWARF attribute code into its string name.  */
9764
9765 static char *
9766 dwarf_attr_name (unsigned attr)
9767 {
9768   switch (attr)
9769     {
9770     case DW_AT_sibling:
9771       return "DW_AT_sibling";
9772     case DW_AT_location:
9773       return "DW_AT_location";
9774     case DW_AT_name:
9775       return "DW_AT_name";
9776     case DW_AT_ordering:
9777       return "DW_AT_ordering";
9778     case DW_AT_subscr_data:
9779       return "DW_AT_subscr_data";
9780     case DW_AT_byte_size:
9781       return "DW_AT_byte_size";
9782     case DW_AT_bit_offset:
9783       return "DW_AT_bit_offset";
9784     case DW_AT_bit_size:
9785       return "DW_AT_bit_size";
9786     case DW_AT_element_list:
9787       return "DW_AT_element_list";
9788     case DW_AT_stmt_list:
9789       return "DW_AT_stmt_list";
9790     case DW_AT_low_pc:
9791       return "DW_AT_low_pc";
9792     case DW_AT_high_pc:
9793       return "DW_AT_high_pc";
9794     case DW_AT_language:
9795       return "DW_AT_language";
9796     case DW_AT_member:
9797       return "DW_AT_member";
9798     case DW_AT_discr:
9799       return "DW_AT_discr";
9800     case DW_AT_discr_value:
9801       return "DW_AT_discr_value";
9802     case DW_AT_visibility:
9803       return "DW_AT_visibility";
9804     case DW_AT_import:
9805       return "DW_AT_import";
9806     case DW_AT_string_length:
9807       return "DW_AT_string_length";
9808     case DW_AT_common_reference:
9809       return "DW_AT_common_reference";
9810     case DW_AT_comp_dir:
9811       return "DW_AT_comp_dir";
9812     case DW_AT_const_value:
9813       return "DW_AT_const_value";
9814     case DW_AT_containing_type:
9815       return "DW_AT_containing_type";
9816     case DW_AT_default_value:
9817       return "DW_AT_default_value";
9818     case DW_AT_inline:
9819       return "DW_AT_inline";
9820     case DW_AT_is_optional:
9821       return "DW_AT_is_optional";
9822     case DW_AT_lower_bound:
9823       return "DW_AT_lower_bound";
9824     case DW_AT_producer:
9825       return "DW_AT_producer";
9826     case DW_AT_prototyped:
9827       return "DW_AT_prototyped";
9828     case DW_AT_return_addr:
9829       return "DW_AT_return_addr";
9830     case DW_AT_start_scope:
9831       return "DW_AT_start_scope";
9832     case DW_AT_bit_stride:
9833       return "DW_AT_bit_stride";
9834     case DW_AT_upper_bound:
9835       return "DW_AT_upper_bound";
9836     case DW_AT_abstract_origin:
9837       return "DW_AT_abstract_origin";
9838     case DW_AT_accessibility:
9839       return "DW_AT_accessibility";
9840     case DW_AT_address_class:
9841       return "DW_AT_address_class";
9842     case DW_AT_artificial:
9843       return "DW_AT_artificial";
9844     case DW_AT_base_types:
9845       return "DW_AT_base_types";
9846     case DW_AT_calling_convention:
9847       return "DW_AT_calling_convention";
9848     case DW_AT_count:
9849       return "DW_AT_count";
9850     case DW_AT_data_member_location:
9851       return "DW_AT_data_member_location";
9852     case DW_AT_decl_column:
9853       return "DW_AT_decl_column";
9854     case DW_AT_decl_file:
9855       return "DW_AT_decl_file";
9856     case DW_AT_decl_line:
9857       return "DW_AT_decl_line";
9858     case DW_AT_declaration:
9859       return "DW_AT_declaration";
9860     case DW_AT_discr_list:
9861       return "DW_AT_discr_list";
9862     case DW_AT_encoding:
9863       return "DW_AT_encoding";
9864     case DW_AT_external:
9865       return "DW_AT_external";
9866     case DW_AT_frame_base:
9867       return "DW_AT_frame_base";
9868     case DW_AT_friend:
9869       return "DW_AT_friend";
9870     case DW_AT_identifier_case:
9871       return "DW_AT_identifier_case";
9872     case DW_AT_macro_info:
9873       return "DW_AT_macro_info";
9874     case DW_AT_namelist_items:
9875       return "DW_AT_namelist_items";
9876     case DW_AT_priority:
9877       return "DW_AT_priority";
9878     case DW_AT_segment:
9879       return "DW_AT_segment";
9880     case DW_AT_specification:
9881       return "DW_AT_specification";
9882     case DW_AT_static_link:
9883       return "DW_AT_static_link";
9884     case DW_AT_type:
9885       return "DW_AT_type";
9886     case DW_AT_use_location:
9887       return "DW_AT_use_location";
9888     case DW_AT_variable_parameter:
9889       return "DW_AT_variable_parameter";
9890     case DW_AT_virtuality:
9891       return "DW_AT_virtuality";
9892     case DW_AT_vtable_elem_location:
9893       return "DW_AT_vtable_elem_location";
9894     /* DWARF 3 values.  */
9895     case DW_AT_allocated:
9896       return "DW_AT_allocated";
9897     case DW_AT_associated:
9898       return "DW_AT_associated";
9899     case DW_AT_data_location:
9900       return "DW_AT_data_location";
9901     case DW_AT_byte_stride:
9902       return "DW_AT_byte_stride";
9903     case DW_AT_entry_pc:
9904       return "DW_AT_entry_pc";
9905     case DW_AT_use_UTF8:
9906       return "DW_AT_use_UTF8";
9907     case DW_AT_extension:
9908       return "DW_AT_extension";
9909     case DW_AT_ranges:
9910       return "DW_AT_ranges";
9911     case DW_AT_trampoline:
9912       return "DW_AT_trampoline";
9913     case DW_AT_call_column:
9914       return "DW_AT_call_column";
9915     case DW_AT_call_file:
9916       return "DW_AT_call_file";
9917     case DW_AT_call_line:
9918       return "DW_AT_call_line";
9919     case DW_AT_description:
9920       return "DW_AT_description";
9921     case DW_AT_binary_scale:
9922       return "DW_AT_binary_scale";
9923     case DW_AT_decimal_scale:
9924       return "DW_AT_decimal_scale";
9925     case DW_AT_small:
9926       return "DW_AT_small";
9927     case DW_AT_decimal_sign:
9928       return "DW_AT_decimal_sign";
9929     case DW_AT_digit_count:
9930       return "DW_AT_digit_count";
9931     case DW_AT_picture_string:
9932       return "DW_AT_picture_string";
9933     case DW_AT_mutable:
9934       return "DW_AT_mutable";
9935     case DW_AT_threads_scaled:
9936       return "DW_AT_threads_scaled";
9937     case DW_AT_explicit:
9938       return "DW_AT_explicit";
9939     case DW_AT_object_pointer:
9940       return "DW_AT_object_pointer";
9941     case DW_AT_endianity:
9942       return "DW_AT_endianity";
9943     case DW_AT_elemental:
9944       return "DW_AT_elemental";
9945     case DW_AT_pure:
9946       return "DW_AT_pure";
9947     case DW_AT_recursive:
9948       return "DW_AT_recursive";
9949     /* DWARF 4 values.  */
9950     case DW_AT_signature:
9951       return "DW_AT_signature";
9952     case DW_AT_linkage_name:
9953       return "DW_AT_linkage_name";
9954     /* SGI/MIPS extensions.  */
9955 #ifdef MIPS /* collides with DW_AT_HP_block_index */
9956     case DW_AT_MIPS_fde:
9957       return "DW_AT_MIPS_fde";
9958 #endif
9959     case DW_AT_MIPS_loop_begin:
9960       return "DW_AT_MIPS_loop_begin";
9961     case DW_AT_MIPS_tail_loop_begin:
9962       return "DW_AT_MIPS_tail_loop_begin";
9963     case DW_AT_MIPS_epilog_begin:
9964       return "DW_AT_MIPS_epilog_begin";
9965     case DW_AT_MIPS_loop_unroll_factor:
9966       return "DW_AT_MIPS_loop_unroll_factor";
9967     case DW_AT_MIPS_software_pipeline_depth:
9968       return "DW_AT_MIPS_software_pipeline_depth";
9969     case DW_AT_MIPS_linkage_name:
9970       return "DW_AT_MIPS_linkage_name";
9971     case DW_AT_MIPS_stride:
9972       return "DW_AT_MIPS_stride";
9973     case DW_AT_MIPS_abstract_name:
9974       return "DW_AT_MIPS_abstract_name";
9975     case DW_AT_MIPS_clone_origin:
9976       return "DW_AT_MIPS_clone_origin";
9977     case DW_AT_MIPS_has_inlines:
9978       return "DW_AT_MIPS_has_inlines";
9979     /* HP extensions.  */
9980 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
9981     case DW_AT_HP_block_index:
9982       return "DW_AT_HP_block_index";
9983 #endif
9984     case DW_AT_HP_unmodifiable:
9985       return "DW_AT_HP_unmodifiable";
9986     case DW_AT_HP_actuals_stmt_list:
9987       return "DW_AT_HP_actuals_stmt_list";
9988     case DW_AT_HP_proc_per_section:
9989       return "DW_AT_HP_proc_per_section";
9990     case DW_AT_HP_raw_data_ptr:
9991       return "DW_AT_HP_raw_data_ptr";
9992     case DW_AT_HP_pass_by_reference:
9993       return "DW_AT_HP_pass_by_reference";
9994     case DW_AT_HP_opt_level:
9995       return "DW_AT_HP_opt_level";
9996     case DW_AT_HP_prof_version_id:
9997       return "DW_AT_HP_prof_version_id";
9998     case DW_AT_HP_opt_flags:
9999       return "DW_AT_HP_opt_flags";
10000     case DW_AT_HP_cold_region_low_pc:
10001       return "DW_AT_HP_cold_region_low_pc";
10002     case DW_AT_HP_cold_region_high_pc:
10003       return "DW_AT_HP_cold_region_high_pc";
10004     case DW_AT_HP_all_variables_modifiable:
10005       return "DW_AT_HP_all_variables_modifiable";
10006     case DW_AT_HP_linkage_name:
10007       return "DW_AT_HP_linkage_name";
10008     case DW_AT_HP_prof_flags:
10009       return "DW_AT_HP_prof_flags";
10010     /* GNU extensions.  */
10011     case DW_AT_sf_names:
10012       return "DW_AT_sf_names";
10013     case DW_AT_src_info:
10014       return "DW_AT_src_info";
10015     case DW_AT_mac_info:
10016       return "DW_AT_mac_info";
10017     case DW_AT_src_coords:
10018       return "DW_AT_src_coords";
10019     case DW_AT_body_begin:
10020       return "DW_AT_body_begin";
10021     case DW_AT_body_end:
10022       return "DW_AT_body_end";
10023     case DW_AT_GNU_vector:
10024       return "DW_AT_GNU_vector";
10025     /* VMS extensions.  */
10026     case DW_AT_VMS_rtnbeg_pd_address:
10027       return "DW_AT_VMS_rtnbeg_pd_address";
10028     /* UPC extension.  */
10029     case DW_AT_upc_threads_scaled:
10030       return "DW_AT_upc_threads_scaled";
10031     /* PGI (STMicroelectronics) extensions.  */
10032     case DW_AT_PGI_lbase:
10033       return "DW_AT_PGI_lbase";
10034     case DW_AT_PGI_soffset:
10035       return "DW_AT_PGI_soffset";
10036     case DW_AT_PGI_lstride:
10037       return "DW_AT_PGI_lstride";
10038     default:
10039       return "DW_AT_<unknown>";
10040     }
10041 }
10042
10043 /* Convert a DWARF value form code into its string name.  */
10044
10045 static char *
10046 dwarf_form_name (unsigned form)
10047 {
10048   switch (form)
10049     {
10050     case DW_FORM_addr:
10051       return "DW_FORM_addr";
10052     case DW_FORM_block2:
10053       return "DW_FORM_block2";
10054     case DW_FORM_block4:
10055       return "DW_FORM_block4";
10056     case DW_FORM_data2:
10057       return "DW_FORM_data2";
10058     case DW_FORM_data4:
10059       return "DW_FORM_data4";
10060     case DW_FORM_data8:
10061       return "DW_FORM_data8";
10062     case DW_FORM_string:
10063       return "DW_FORM_string";
10064     case DW_FORM_block:
10065       return "DW_FORM_block";
10066     case DW_FORM_block1:
10067       return "DW_FORM_block1";
10068     case DW_FORM_data1:
10069       return "DW_FORM_data1";
10070     case DW_FORM_flag:
10071       return "DW_FORM_flag";
10072     case DW_FORM_sdata:
10073       return "DW_FORM_sdata";
10074     case DW_FORM_strp:
10075       return "DW_FORM_strp";
10076     case DW_FORM_udata:
10077       return "DW_FORM_udata";
10078     case DW_FORM_ref_addr:
10079       return "DW_FORM_ref_addr";
10080     case DW_FORM_ref1:
10081       return "DW_FORM_ref1";
10082     case DW_FORM_ref2:
10083       return "DW_FORM_ref2";
10084     case DW_FORM_ref4:
10085       return "DW_FORM_ref4";
10086     case DW_FORM_ref8:
10087       return "DW_FORM_ref8";
10088     case DW_FORM_ref_udata:
10089       return "DW_FORM_ref_udata";
10090     case DW_FORM_indirect:
10091       return "DW_FORM_indirect";
10092     case DW_FORM_sec_offset:
10093       return "DW_FORM_sec_offset";
10094     case DW_FORM_exprloc:
10095       return "DW_FORM_exprloc";
10096     case DW_FORM_flag_present:
10097       return "DW_FORM_flag_present";
10098     case DW_FORM_sig8:
10099       return "DW_FORM_sig8";
10100     default:
10101       return "DW_FORM_<unknown>";
10102     }
10103 }
10104
10105 /* Convert a DWARF stack opcode into its string name.  */
10106
10107 const char *
10108 dwarf_stack_op_name (unsigned op, int def)
10109 {
10110   switch (op)
10111     {
10112     case DW_OP_addr:
10113       return "DW_OP_addr";
10114     case DW_OP_deref:
10115       return "DW_OP_deref";
10116     case DW_OP_const1u:
10117       return "DW_OP_const1u";
10118     case DW_OP_const1s:
10119       return "DW_OP_const1s";
10120     case DW_OP_const2u:
10121       return "DW_OP_const2u";
10122     case DW_OP_const2s:
10123       return "DW_OP_const2s";
10124     case DW_OP_const4u:
10125       return "DW_OP_const4u";
10126     case DW_OP_const4s:
10127       return "DW_OP_const4s";
10128     case DW_OP_const8u:
10129       return "DW_OP_const8u";
10130     case DW_OP_const8s:
10131       return "DW_OP_const8s";
10132     case DW_OP_constu:
10133       return "DW_OP_constu";
10134     case DW_OP_consts:
10135       return "DW_OP_consts";
10136     case DW_OP_dup:
10137       return "DW_OP_dup";
10138     case DW_OP_drop:
10139       return "DW_OP_drop";
10140     case DW_OP_over:
10141       return "DW_OP_over";
10142     case DW_OP_pick:
10143       return "DW_OP_pick";
10144     case DW_OP_swap:
10145       return "DW_OP_swap";
10146     case DW_OP_rot:
10147       return "DW_OP_rot";
10148     case DW_OP_xderef:
10149       return "DW_OP_xderef";
10150     case DW_OP_abs:
10151       return "DW_OP_abs";
10152     case DW_OP_and:
10153       return "DW_OP_and";
10154     case DW_OP_div:
10155       return "DW_OP_div";
10156     case DW_OP_minus:
10157       return "DW_OP_minus";
10158     case DW_OP_mod:
10159       return "DW_OP_mod";
10160     case DW_OP_mul:
10161       return "DW_OP_mul";
10162     case DW_OP_neg:
10163       return "DW_OP_neg";
10164     case DW_OP_not:
10165       return "DW_OP_not";
10166     case DW_OP_or:
10167       return "DW_OP_or";
10168     case DW_OP_plus:
10169       return "DW_OP_plus";
10170     case DW_OP_plus_uconst:
10171       return "DW_OP_plus_uconst";
10172     case DW_OP_shl:
10173       return "DW_OP_shl";
10174     case DW_OP_shr:
10175       return "DW_OP_shr";
10176     case DW_OP_shra:
10177       return "DW_OP_shra";
10178     case DW_OP_xor:
10179       return "DW_OP_xor";
10180     case DW_OP_bra:
10181       return "DW_OP_bra";
10182     case DW_OP_eq:
10183       return "DW_OP_eq";
10184     case DW_OP_ge:
10185       return "DW_OP_ge";
10186     case DW_OP_gt:
10187       return "DW_OP_gt";
10188     case DW_OP_le:
10189       return "DW_OP_le";
10190     case DW_OP_lt:
10191       return "DW_OP_lt";
10192     case DW_OP_ne:
10193       return "DW_OP_ne";
10194     case DW_OP_skip:
10195       return "DW_OP_skip";
10196     case DW_OP_lit0:
10197       return "DW_OP_lit0";
10198     case DW_OP_lit1:
10199       return "DW_OP_lit1";
10200     case DW_OP_lit2:
10201       return "DW_OP_lit2";
10202     case DW_OP_lit3:
10203       return "DW_OP_lit3";
10204     case DW_OP_lit4:
10205       return "DW_OP_lit4";
10206     case DW_OP_lit5:
10207       return "DW_OP_lit5";
10208     case DW_OP_lit6:
10209       return "DW_OP_lit6";
10210     case DW_OP_lit7:
10211       return "DW_OP_lit7";
10212     case DW_OP_lit8:
10213       return "DW_OP_lit8";
10214     case DW_OP_lit9:
10215       return "DW_OP_lit9";
10216     case DW_OP_lit10:
10217       return "DW_OP_lit10";
10218     case DW_OP_lit11:
10219       return "DW_OP_lit11";
10220     case DW_OP_lit12:
10221       return "DW_OP_lit12";
10222     case DW_OP_lit13:
10223       return "DW_OP_lit13";
10224     case DW_OP_lit14:
10225       return "DW_OP_lit14";
10226     case DW_OP_lit15:
10227       return "DW_OP_lit15";
10228     case DW_OP_lit16:
10229       return "DW_OP_lit16";
10230     case DW_OP_lit17:
10231       return "DW_OP_lit17";
10232     case DW_OP_lit18:
10233       return "DW_OP_lit18";
10234     case DW_OP_lit19:
10235       return "DW_OP_lit19";
10236     case DW_OP_lit20:
10237       return "DW_OP_lit20";
10238     case DW_OP_lit21:
10239       return "DW_OP_lit21";
10240     case DW_OP_lit22:
10241       return "DW_OP_lit22";
10242     case DW_OP_lit23:
10243       return "DW_OP_lit23";
10244     case DW_OP_lit24:
10245       return "DW_OP_lit24";
10246     case DW_OP_lit25:
10247       return "DW_OP_lit25";
10248     case DW_OP_lit26:
10249       return "DW_OP_lit26";
10250     case DW_OP_lit27:
10251       return "DW_OP_lit27";
10252     case DW_OP_lit28:
10253       return "DW_OP_lit28";
10254     case DW_OP_lit29:
10255       return "DW_OP_lit29";
10256     case DW_OP_lit30:
10257       return "DW_OP_lit30";
10258     case DW_OP_lit31:
10259       return "DW_OP_lit31";
10260     case DW_OP_reg0:
10261       return "DW_OP_reg0";
10262     case DW_OP_reg1:
10263       return "DW_OP_reg1";
10264     case DW_OP_reg2:
10265       return "DW_OP_reg2";
10266     case DW_OP_reg3:
10267       return "DW_OP_reg3";
10268     case DW_OP_reg4:
10269       return "DW_OP_reg4";
10270     case DW_OP_reg5:
10271       return "DW_OP_reg5";
10272     case DW_OP_reg6:
10273       return "DW_OP_reg6";
10274     case DW_OP_reg7:
10275       return "DW_OP_reg7";
10276     case DW_OP_reg8:
10277       return "DW_OP_reg8";
10278     case DW_OP_reg9:
10279       return "DW_OP_reg9";
10280     case DW_OP_reg10:
10281       return "DW_OP_reg10";
10282     case DW_OP_reg11:
10283       return "DW_OP_reg11";
10284     case DW_OP_reg12:
10285       return "DW_OP_reg12";
10286     case DW_OP_reg13:
10287       return "DW_OP_reg13";
10288     case DW_OP_reg14:
10289       return "DW_OP_reg14";
10290     case DW_OP_reg15:
10291       return "DW_OP_reg15";
10292     case DW_OP_reg16:
10293       return "DW_OP_reg16";
10294     case DW_OP_reg17:
10295       return "DW_OP_reg17";
10296     case DW_OP_reg18:
10297       return "DW_OP_reg18";
10298     case DW_OP_reg19:
10299       return "DW_OP_reg19";
10300     case DW_OP_reg20:
10301       return "DW_OP_reg20";
10302     case DW_OP_reg21:
10303       return "DW_OP_reg21";
10304     case DW_OP_reg22:
10305       return "DW_OP_reg22";
10306     case DW_OP_reg23:
10307       return "DW_OP_reg23";
10308     case DW_OP_reg24:
10309       return "DW_OP_reg24";
10310     case DW_OP_reg25:
10311       return "DW_OP_reg25";
10312     case DW_OP_reg26:
10313       return "DW_OP_reg26";
10314     case DW_OP_reg27:
10315       return "DW_OP_reg27";
10316     case DW_OP_reg28:
10317       return "DW_OP_reg28";
10318     case DW_OP_reg29:
10319       return "DW_OP_reg29";
10320     case DW_OP_reg30:
10321       return "DW_OP_reg30";
10322     case DW_OP_reg31:
10323       return "DW_OP_reg31";
10324     case DW_OP_breg0:
10325       return "DW_OP_breg0";
10326     case DW_OP_breg1:
10327       return "DW_OP_breg1";
10328     case DW_OP_breg2:
10329       return "DW_OP_breg2";
10330     case DW_OP_breg3:
10331       return "DW_OP_breg3";
10332     case DW_OP_breg4:
10333       return "DW_OP_breg4";
10334     case DW_OP_breg5:
10335       return "DW_OP_breg5";
10336     case DW_OP_breg6:
10337       return "DW_OP_breg6";
10338     case DW_OP_breg7:
10339       return "DW_OP_breg7";
10340     case DW_OP_breg8:
10341       return "DW_OP_breg8";
10342     case DW_OP_breg9:
10343       return "DW_OP_breg9";
10344     case DW_OP_breg10:
10345       return "DW_OP_breg10";
10346     case DW_OP_breg11:
10347       return "DW_OP_breg11";
10348     case DW_OP_breg12:
10349       return "DW_OP_breg12";
10350     case DW_OP_breg13:
10351       return "DW_OP_breg13";
10352     case DW_OP_breg14:
10353       return "DW_OP_breg14";
10354     case DW_OP_breg15:
10355       return "DW_OP_breg15";
10356     case DW_OP_breg16:
10357       return "DW_OP_breg16";
10358     case DW_OP_breg17:
10359       return "DW_OP_breg17";
10360     case DW_OP_breg18:
10361       return "DW_OP_breg18";
10362     case DW_OP_breg19:
10363       return "DW_OP_breg19";
10364     case DW_OP_breg20:
10365       return "DW_OP_breg20";
10366     case DW_OP_breg21:
10367       return "DW_OP_breg21";
10368     case DW_OP_breg22:
10369       return "DW_OP_breg22";
10370     case DW_OP_breg23:
10371       return "DW_OP_breg23";
10372     case DW_OP_breg24:
10373       return "DW_OP_breg24";
10374     case DW_OP_breg25:
10375       return "DW_OP_breg25";
10376     case DW_OP_breg26:
10377       return "DW_OP_breg26";
10378     case DW_OP_breg27:
10379       return "DW_OP_breg27";
10380     case DW_OP_breg28:
10381       return "DW_OP_breg28";
10382     case DW_OP_breg29:
10383       return "DW_OP_breg29";
10384     case DW_OP_breg30:
10385       return "DW_OP_breg30";
10386     case DW_OP_breg31:
10387       return "DW_OP_breg31";
10388     case DW_OP_regx:
10389       return "DW_OP_regx";
10390     case DW_OP_fbreg:
10391       return "DW_OP_fbreg";
10392     case DW_OP_bregx:
10393       return "DW_OP_bregx";
10394     case DW_OP_piece:
10395       return "DW_OP_piece";
10396     case DW_OP_deref_size:
10397       return "DW_OP_deref_size";
10398     case DW_OP_xderef_size:
10399       return "DW_OP_xderef_size";
10400     case DW_OP_nop:
10401       return "DW_OP_nop";
10402     /* DWARF 3 extensions.  */
10403     case DW_OP_push_object_address:
10404       return "DW_OP_push_object_address";
10405     case DW_OP_call2:
10406       return "DW_OP_call2";
10407     case DW_OP_call4:
10408       return "DW_OP_call4";
10409     case DW_OP_call_ref:
10410       return "DW_OP_call_ref";
10411     case DW_OP_form_tls_address:
10412       return "DW_OP_form_tls_address";
10413     case DW_OP_call_frame_cfa:
10414       return "DW_OP_call_frame_cfa";
10415     case DW_OP_bit_piece:
10416       return "DW_OP_bit_piece";
10417     /* DWARF 4 extensions.  */
10418     case DW_OP_implicit_value:
10419       return "DW_OP_implicit_value";
10420     case DW_OP_stack_value:
10421       return "DW_OP_stack_value";
10422     /* GNU extensions.  */
10423     case DW_OP_GNU_push_tls_address:
10424       return "DW_OP_GNU_push_tls_address";
10425     case DW_OP_GNU_uninit:
10426       return "DW_OP_GNU_uninit";
10427     default:
10428       return def ? "OP_<unknown>" : NULL;
10429     }
10430 }
10431
10432 static char *
10433 dwarf_bool_name (unsigned mybool)
10434 {
10435   if (mybool)
10436     return "TRUE";
10437   else
10438     return "FALSE";
10439 }
10440
10441 /* Convert a DWARF type code into its string name.  */
10442
10443 static char *
10444 dwarf_type_encoding_name (unsigned enc)
10445 {
10446   switch (enc)
10447     {
10448     case DW_ATE_void:
10449       return "DW_ATE_void";
10450     case DW_ATE_address:
10451       return "DW_ATE_address";
10452     case DW_ATE_boolean:
10453       return "DW_ATE_boolean";
10454     case DW_ATE_complex_float:
10455       return "DW_ATE_complex_float";
10456     case DW_ATE_float:
10457       return "DW_ATE_float";
10458     case DW_ATE_signed:
10459       return "DW_ATE_signed";
10460     case DW_ATE_signed_char:
10461       return "DW_ATE_signed_char";
10462     case DW_ATE_unsigned:
10463       return "DW_ATE_unsigned";
10464     case DW_ATE_unsigned_char:
10465       return "DW_ATE_unsigned_char";
10466     /* DWARF 3.  */
10467     case DW_ATE_imaginary_float:
10468       return "DW_ATE_imaginary_float";
10469     case DW_ATE_packed_decimal:
10470       return "DW_ATE_packed_decimal";
10471     case DW_ATE_numeric_string:
10472       return "DW_ATE_numeric_string";
10473     case DW_ATE_edited:
10474       return "DW_ATE_edited";
10475     case DW_ATE_signed_fixed:
10476       return "DW_ATE_signed_fixed";
10477     case DW_ATE_unsigned_fixed:
10478       return "DW_ATE_unsigned_fixed";
10479     case DW_ATE_decimal_float:
10480       return "DW_ATE_decimal_float";
10481     /* DWARF 4.  */
10482     case DW_ATE_UTF:
10483       return "DW_ATE_UTF";
10484     /* HP extensions.  */
10485     case DW_ATE_HP_float80:
10486       return "DW_ATE_HP_float80";
10487     case DW_ATE_HP_complex_float80:
10488       return "DW_ATE_HP_complex_float80";
10489     case DW_ATE_HP_float128:
10490       return "DW_ATE_HP_float128";
10491     case DW_ATE_HP_complex_float128:
10492       return "DW_ATE_HP_complex_float128";
10493     case DW_ATE_HP_floathpintel:
10494       return "DW_ATE_HP_floathpintel";
10495     case DW_ATE_HP_imaginary_float80:
10496       return "DW_ATE_HP_imaginary_float80";
10497     case DW_ATE_HP_imaginary_float128:
10498       return "DW_ATE_HP_imaginary_float128";
10499     default:
10500       return "DW_ATE_<unknown>";
10501     }
10502 }
10503
10504 /* Convert a DWARF call frame info operation to its string name. */
10505
10506 #if 0
10507 static char *
10508 dwarf_cfi_name (unsigned cfi_opc)
10509 {
10510   switch (cfi_opc)
10511     {
10512     case DW_CFA_advance_loc:
10513       return "DW_CFA_advance_loc";
10514     case DW_CFA_offset:
10515       return "DW_CFA_offset";
10516     case DW_CFA_restore:
10517       return "DW_CFA_restore";
10518     case DW_CFA_nop:
10519       return "DW_CFA_nop";
10520     case DW_CFA_set_loc:
10521       return "DW_CFA_set_loc";
10522     case DW_CFA_advance_loc1:
10523       return "DW_CFA_advance_loc1";
10524     case DW_CFA_advance_loc2:
10525       return "DW_CFA_advance_loc2";
10526     case DW_CFA_advance_loc4:
10527       return "DW_CFA_advance_loc4";
10528     case DW_CFA_offset_extended:
10529       return "DW_CFA_offset_extended";
10530     case DW_CFA_restore_extended:
10531       return "DW_CFA_restore_extended";
10532     case DW_CFA_undefined:
10533       return "DW_CFA_undefined";
10534     case DW_CFA_same_value:
10535       return "DW_CFA_same_value";
10536     case DW_CFA_register:
10537       return "DW_CFA_register";
10538     case DW_CFA_remember_state:
10539       return "DW_CFA_remember_state";
10540     case DW_CFA_restore_state:
10541       return "DW_CFA_restore_state";
10542     case DW_CFA_def_cfa:
10543       return "DW_CFA_def_cfa";
10544     case DW_CFA_def_cfa_register:
10545       return "DW_CFA_def_cfa_register";
10546     case DW_CFA_def_cfa_offset:
10547       return "DW_CFA_def_cfa_offset";
10548     /* DWARF 3.  */
10549     case DW_CFA_def_cfa_expression:
10550       return "DW_CFA_def_cfa_expression";
10551     case DW_CFA_expression:
10552       return "DW_CFA_expression";
10553     case DW_CFA_offset_extended_sf:
10554       return "DW_CFA_offset_extended_sf";
10555     case DW_CFA_def_cfa_sf:
10556       return "DW_CFA_def_cfa_sf";
10557     case DW_CFA_def_cfa_offset_sf:
10558       return "DW_CFA_def_cfa_offset_sf";
10559     case DW_CFA_val_offset:
10560       return "DW_CFA_val_offset";
10561     case DW_CFA_val_offset_sf:
10562       return "DW_CFA_val_offset_sf";
10563     case DW_CFA_val_expression:
10564       return "DW_CFA_val_expression";
10565     /* SGI/MIPS specific.  */
10566     case DW_CFA_MIPS_advance_loc8:
10567       return "DW_CFA_MIPS_advance_loc8";
10568     /* GNU extensions.  */
10569     case DW_CFA_GNU_window_save:
10570       return "DW_CFA_GNU_window_save";
10571     case DW_CFA_GNU_args_size:
10572       return "DW_CFA_GNU_args_size";
10573     case DW_CFA_GNU_negative_offset_extended:
10574       return "DW_CFA_GNU_negative_offset_extended";
10575     default:
10576       return "DW_CFA_<unknown>";
10577     }
10578 }
10579 #endif
10580
10581 static void
10582 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
10583 {
10584   unsigned int i;
10585
10586   print_spaces (indent, f);
10587   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
10588            dwarf_tag_name (die->tag), die->abbrev, die->offset);
10589
10590   if (die->parent != NULL)
10591     {
10592       print_spaces (indent, f);
10593       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
10594                           die->parent->offset);
10595     }
10596
10597   print_spaces (indent, f);
10598   fprintf_unfiltered (f, "  has children: %s\n",
10599            dwarf_bool_name (die->child != NULL));
10600
10601   print_spaces (indent, f);
10602   fprintf_unfiltered (f, "  attributes:\n");
10603
10604   for (i = 0; i < die->num_attrs; ++i)
10605     {
10606       print_spaces (indent, f);
10607       fprintf_unfiltered (f, "    %s (%s) ",
10608                dwarf_attr_name (die->attrs[i].name),
10609                dwarf_form_name (die->attrs[i].form));
10610
10611       switch (die->attrs[i].form)
10612         {
10613         case DW_FORM_ref_addr:
10614         case DW_FORM_addr:
10615           fprintf_unfiltered (f, "address: ");
10616           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
10617           break;
10618         case DW_FORM_block2:
10619         case DW_FORM_block4:
10620         case DW_FORM_block:
10621         case DW_FORM_block1:
10622           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
10623           break;
10624         case DW_FORM_exprloc:
10625           fprintf_unfiltered (f, "expression: size %u",
10626                               DW_BLOCK (&die->attrs[i])->size);
10627           break;
10628         case DW_FORM_ref1:
10629         case DW_FORM_ref2:
10630         case DW_FORM_ref4:
10631           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10632                               (long) (DW_ADDR (&die->attrs[i])));
10633           break;
10634         case DW_FORM_data1:
10635         case DW_FORM_data2:
10636         case DW_FORM_data4:
10637         case DW_FORM_data8:
10638         case DW_FORM_udata:
10639         case DW_FORM_sdata:
10640           fprintf_unfiltered (f, "constant: %s",
10641                               pulongest (DW_UNSND (&die->attrs[i])));
10642           break;
10643         case DW_FORM_sec_offset:
10644           fprintf_unfiltered (f, "section offset: %s",
10645                               pulongest (DW_UNSND (&die->attrs[i])));
10646           break;
10647         case DW_FORM_sig8:
10648           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10649             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10650                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10651           else
10652             fprintf_unfiltered (f, "signatured type, offset: unknown");
10653           break;
10654         case DW_FORM_string:
10655         case DW_FORM_strp:
10656           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
10657                    DW_STRING (&die->attrs[i])
10658                    ? DW_STRING (&die->attrs[i]) : "",
10659                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
10660           break;
10661         case DW_FORM_flag:
10662           if (DW_UNSND (&die->attrs[i]))
10663             fprintf_unfiltered (f, "flag: TRUE");
10664           else
10665             fprintf_unfiltered (f, "flag: FALSE");
10666           break;
10667         case DW_FORM_flag_present:
10668           fprintf_unfiltered (f, "flag: TRUE");
10669           break;
10670         case DW_FORM_indirect:
10671           /* the reader will have reduced the indirect form to
10672              the "base form" so this form should not occur */
10673           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
10674           break;
10675         default:
10676           fprintf_unfiltered (f, "unsupported attribute form: %d.",
10677                    die->attrs[i].form);
10678           break;
10679         }
10680       fprintf_unfiltered (f, "\n");
10681     }
10682 }
10683
10684 static void
10685 dump_die_for_error (struct die_info *die)
10686 {
10687   dump_die_shallow (gdb_stderr, 0, die);
10688 }
10689
10690 static void
10691 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10692 {
10693   int indent = level * 4;
10694
10695   gdb_assert (die != NULL);
10696
10697   if (level >= max_level)
10698     return;
10699
10700   dump_die_shallow (f, indent, die);
10701
10702   if (die->child != NULL)
10703     {
10704       print_spaces (indent, f);
10705       fprintf_unfiltered (f, "  Children:");
10706       if (level + 1 < max_level)
10707         {
10708           fprintf_unfiltered (f, "\n");
10709           dump_die_1 (f, level + 1, max_level, die->child);
10710         }
10711       else
10712         {
10713           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10714         }
10715     }
10716
10717   if (die->sibling != NULL && level > 0)
10718     {
10719       dump_die_1 (f, level, max_level, die->sibling);
10720     }
10721 }
10722
10723 /* This is called from the pdie macro in gdbinit.in.
10724    It's not static so gcc will keep a copy callable from gdb.  */
10725
10726 void
10727 dump_die (struct die_info *die, int max_level)
10728 {
10729   dump_die_1 (gdb_stdlog, 0, max_level, die);
10730 }
10731
10732 static void
10733 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735   void **slot;
10736
10737   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10738
10739   *slot = die;
10740 }
10741
10742 static int
10743 is_ref_attr (struct attribute *attr)
10744 {
10745   switch (attr->form)
10746     {
10747     case DW_FORM_ref_addr:
10748     case DW_FORM_ref1:
10749     case DW_FORM_ref2:
10750     case DW_FORM_ref4:
10751     case DW_FORM_ref8:
10752     case DW_FORM_ref_udata:
10753       return 1;
10754     default:
10755       return 0;
10756     }
10757 }
10758
10759 static unsigned int
10760 dwarf2_get_ref_die_offset (struct attribute *attr)
10761 {
10762   if (is_ref_attr (attr))
10763     return DW_ADDR (attr);
10764
10765   complaint (&symfile_complaints,
10766              _("unsupported die ref attribute form: '%s'"),
10767              dwarf_form_name (attr->form));
10768   return 0;
10769 }
10770
10771 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
10772  * the value held by the attribute is not constant.  */
10773
10774 static LONGEST
10775 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10776 {
10777   if (attr->form == DW_FORM_sdata)
10778     return DW_SND (attr);
10779   else if (attr->form == DW_FORM_udata
10780            || attr->form == DW_FORM_data1
10781            || attr->form == DW_FORM_data2
10782            || attr->form == DW_FORM_data4
10783            || attr->form == DW_FORM_data8)
10784     return DW_UNSND (attr);
10785   else
10786     {
10787       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
10788                  dwarf_form_name (attr->form));
10789       return default_value;
10790     }
10791 }
10792
10793 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
10794    unit and add it to our queue.
10795    The result is non-zero if PER_CU was queued, otherwise the result is zero
10796    meaning either PER_CU is already queued or it is already loaded.  */
10797
10798 static int
10799 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10800                        struct dwarf2_per_cu_data *per_cu)
10801 {
10802   /* Mark the dependence relation so that we don't flush PER_CU
10803      too early.  */
10804   dwarf2_add_dependence (this_cu, per_cu);
10805
10806   /* If it's already on the queue, we have nothing to do.  */
10807   if (per_cu->queued)
10808     return 0;
10809
10810   /* If the compilation unit is already loaded, just mark it as
10811      used.  */
10812   if (per_cu->cu != NULL)
10813     {
10814       per_cu->cu->last_used = 0;
10815       return 0;
10816     }
10817
10818   /* Add it to the queue.  */
10819   queue_comp_unit (per_cu, this_cu->objfile);
10820
10821   return 1;
10822 }
10823
10824 /* Follow reference or signature attribute ATTR of SRC_DIE.
10825    On entry *REF_CU is the CU of SRC_DIE.
10826    On exit *REF_CU is the CU of the result.  */
10827
10828 static struct die_info *
10829 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10830                        struct dwarf2_cu **ref_cu)
10831 {
10832   struct die_info *die;
10833
10834   if (is_ref_attr (attr))
10835     die = follow_die_ref (src_die, attr, ref_cu);
10836   else if (attr->form == DW_FORM_sig8)
10837     die = follow_die_sig (src_die, attr, ref_cu);
10838   else
10839     {
10840       dump_die_for_error (src_die);
10841       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10842              (*ref_cu)->objfile->name);
10843     }
10844
10845   return die;
10846 }
10847
10848 /* Follow reference OFFSET.
10849    On entry *REF_CU is the CU of source DIE referencing OFFSET.
10850    On exit *REF_CU is the CU of the result.  */
10851
10852 static struct die_info *
10853 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
10854 {
10855   struct die_info temp_die;
10856   struct dwarf2_cu *target_cu, *cu = *ref_cu;
10857
10858   gdb_assert (cu->per_cu != NULL);
10859
10860   if (cu->per_cu->from_debug_types)
10861     {
10862       /* .debug_types CUs cannot reference anything outside their CU.
10863          If they need to, they have to reference a signatured type via
10864          DW_FORM_sig8.  */
10865       if (! offset_in_cu_p (&cu->header, offset))
10866         return NULL;
10867       target_cu = cu;
10868     }
10869   else if (! offset_in_cu_p (&cu->header, offset))
10870     {
10871       struct dwarf2_per_cu_data *per_cu;
10872
10873       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10874
10875       /* If necessary, add it to the queue and load its DIEs.  */
10876       if (maybe_queue_comp_unit (cu, per_cu))
10877         load_full_comp_unit (per_cu, cu->objfile);
10878
10879       target_cu = per_cu->cu;
10880     }
10881   else
10882     target_cu = cu;
10883
10884   *ref_cu = target_cu;
10885   temp_die.offset = offset;
10886   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10887 }
10888
10889 /* Follow reference attribute ATTR of SRC_DIE.
10890    On entry *REF_CU is the CU of SRC_DIE.
10891    On exit *REF_CU is the CU of the result.  */
10892
10893 static struct die_info *
10894 follow_die_ref (struct die_info *src_die, struct attribute *attr,
10895                 struct dwarf2_cu **ref_cu)
10896 {
10897   unsigned int offset = dwarf2_get_ref_die_offset (attr);
10898   struct dwarf2_cu *cu = *ref_cu;
10899   struct die_info *die;
10900
10901   die = follow_die_offset (offset, ref_cu);
10902   if (!die)
10903     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10904            "at 0x%x [in module %s]"),
10905            offset, src_die->offset, cu->objfile->name);
10906
10907   return die;
10908 }
10909
10910 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
10911    value is intended for DW_OP_call*.  */
10912
10913 struct dwarf2_locexpr_baton
10914 dwarf2_fetch_die_location_block (unsigned int offset,
10915                                  struct dwarf2_per_cu_data *per_cu)
10916 {
10917   struct dwarf2_cu *cu = per_cu->cu;
10918   struct die_info *die;
10919   struct attribute *attr;
10920   struct dwarf2_locexpr_baton retval;
10921
10922   die = follow_die_offset (offset, &cu);
10923   if (!die)
10924     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
10925            offset, per_cu->cu->objfile->name);
10926
10927   attr = dwarf2_attr (die, DW_AT_location, cu);
10928   if (!attr)
10929     {
10930       /* DWARF: "If there is no such attribute, then there is no effect.".  */
10931
10932       retval.data = NULL;
10933       retval.size = 0;
10934     }
10935   else
10936     {
10937       if (!attr_form_is_block (attr))
10938         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
10939                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
10940                offset, per_cu->cu->objfile->name);
10941
10942       retval.data = DW_BLOCK (attr)->data;
10943       retval.size = DW_BLOCK (attr)->size;
10944     }
10945   retval.per_cu = cu->per_cu;
10946   return retval;
10947 }
10948
10949 /* Follow the signature attribute ATTR in SRC_DIE.
10950    On entry *REF_CU is the CU of SRC_DIE.
10951    On exit *REF_CU is the CU of the result.  */
10952
10953 static struct die_info *
10954 follow_die_sig (struct die_info *src_die, struct attribute *attr,
10955                 struct dwarf2_cu **ref_cu)
10956 {
10957   struct objfile *objfile = (*ref_cu)->objfile;
10958   struct die_info temp_die;
10959   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10960   struct dwarf2_cu *sig_cu;
10961   struct die_info *die;
10962
10963   /* sig_type will be NULL if the signatured type is missing from
10964      the debug info.  */
10965   if (sig_type == NULL)
10966     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10967              "at 0x%x [in module %s]"),
10968            src_die->offset, objfile->name);
10969
10970   /* If necessary, add it to the queue and load its DIEs.  */
10971
10972   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10973     read_signatured_type (objfile, sig_type);
10974
10975   gdb_assert (sig_type->per_cu.cu != NULL);
10976
10977   sig_cu = sig_type->per_cu.cu;
10978   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10979   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10980   if (die)
10981     {
10982       *ref_cu = sig_cu;
10983       return die;
10984     }
10985
10986   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10987          "at 0x%x [in module %s]"),
10988          sig_type->type_offset, src_die->offset, objfile->name);
10989 }
10990
10991 /* Given an offset of a signatured type, return its signatured_type.  */
10992
10993 static struct signatured_type *
10994 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10995 {
10996   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10997   unsigned int length, initial_length_size;
10998   unsigned int sig_offset;
10999   struct signatured_type find_entry, *type_sig;
11000
11001   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
11002   sig_offset = (initial_length_size
11003                 + 2 /*version*/
11004                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
11005                 + 1 /*address_size*/);
11006   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
11007   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
11008
11009   /* This is only used to lookup previously recorded types.
11010      If we didn't find it, it's our bug.  */
11011   gdb_assert (type_sig != NULL);
11012   gdb_assert (offset == type_sig->offset);
11013
11014   return type_sig;
11015 }
11016
11017 /* Read in signatured type at OFFSET and build its CU and die(s).  */
11018
11019 static void
11020 read_signatured_type_at_offset (struct objfile *objfile,
11021                                 unsigned int offset)
11022 {
11023   struct signatured_type *type_sig;
11024
11025   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
11026
11027   /* We have the section offset, but we need the signature to do the
11028      hash table lookup.  */
11029   type_sig = lookup_signatured_type_at_offset (objfile, offset);
11030
11031   gdb_assert (type_sig->per_cu.cu == NULL);
11032
11033   read_signatured_type (objfile, type_sig);
11034
11035   gdb_assert (type_sig->per_cu.cu != NULL);
11036 }
11037
11038 /* Read in a signatured type and build its CU and DIEs.  */
11039
11040 static void
11041 read_signatured_type (struct objfile *objfile,
11042                       struct signatured_type *type_sig)
11043 {
11044   gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
11045   struct die_reader_specs reader_specs;
11046   struct dwarf2_cu *cu;
11047   ULONGEST signature;
11048   struct cleanup *back_to, *free_cu_cleanup;
11049   struct attribute *attr;
11050
11051   gdb_assert (type_sig->per_cu.cu == NULL);
11052
11053   cu = xmalloc (sizeof (struct dwarf2_cu));
11054   memset (cu, 0, sizeof (struct dwarf2_cu));
11055   obstack_init (&cu->comp_unit_obstack);
11056   cu->objfile = objfile;
11057   type_sig->per_cu.cu = cu;
11058   cu->per_cu = &type_sig->per_cu;
11059
11060   /* If an error occurs while loading, release our storage.  */
11061   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
11062
11063   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
11064                                         types_ptr, objfile->obfd);
11065   gdb_assert (signature == type_sig->signature);
11066
11067   cu->die_hash
11068     = htab_create_alloc_ex (cu->header.length / 12,
11069                             die_hash,
11070                             die_eq,
11071                             NULL,
11072                             &cu->comp_unit_obstack,
11073                             hashtab_obstack_allocate,
11074                             dummy_obstack_deallocate);
11075
11076   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
11077   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
11078
11079   init_cu_die_reader (&reader_specs, cu);
11080
11081   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
11082                                     NULL /*parent*/);
11083
11084   /* We try not to read any attributes in this function, because not
11085      all objfiles needed for references have been loaded yet, and symbol
11086      table processing isn't initialized.  But we have to set the CU language,
11087      or we won't be able to build types correctly.  */
11088   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
11089   if (attr)
11090     set_cu_language (DW_UNSND (attr), cu);
11091   else
11092     set_cu_language (language_minimal, cu);
11093
11094   do_cleanups (back_to);
11095
11096   /* We've successfully allocated this compilation unit.  Let our caller
11097      clean it up when finished with it.  */
11098   discard_cleanups (free_cu_cleanup);
11099
11100   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
11101   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
11102 }
11103
11104 /* Decode simple location descriptions.
11105    Given a pointer to a dwarf block that defines a location, compute
11106    the location and return the value.
11107
11108    NOTE drow/2003-11-18: This function is called in two situations
11109    now: for the address of static or global variables (partial symbols
11110    only) and for offsets into structures which are expected to be
11111    (more or less) constant.  The partial symbol case should go away,
11112    and only the constant case should remain.  That will let this
11113    function complain more accurately.  A few special modes are allowed
11114    without complaint for global variables (for instance, global
11115    register values and thread-local values).
11116
11117    A location description containing no operations indicates that the
11118    object is optimized out.  The return value is 0 for that case.
11119    FIXME drow/2003-11-16: No callers check for this case any more; soon all
11120    callers will only want a very basic result and this can become a
11121    complaint.
11122
11123    Note that stack[0] is unused except as a default error return.
11124    Note that stack overflow is not yet handled.  */
11125
11126 static CORE_ADDR
11127 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
11128 {
11129   struct objfile *objfile = cu->objfile;
11130   int i;
11131   int size = blk->size;
11132   gdb_byte *data = blk->data;
11133   CORE_ADDR stack[64];
11134   int stacki;
11135   unsigned int bytes_read, unsnd;
11136   gdb_byte op;
11137
11138   i = 0;
11139   stacki = 0;
11140   stack[stacki] = 0;
11141
11142   while (i < size)
11143     {
11144       op = data[i++];
11145       switch (op)
11146         {
11147         case DW_OP_lit0:
11148         case DW_OP_lit1:
11149         case DW_OP_lit2:
11150         case DW_OP_lit3:
11151         case DW_OP_lit4:
11152         case DW_OP_lit5:
11153         case DW_OP_lit6:
11154         case DW_OP_lit7:
11155         case DW_OP_lit8:
11156         case DW_OP_lit9:
11157         case DW_OP_lit10:
11158         case DW_OP_lit11:
11159         case DW_OP_lit12:
11160         case DW_OP_lit13:
11161         case DW_OP_lit14:
11162         case DW_OP_lit15:
11163         case DW_OP_lit16:
11164         case DW_OP_lit17:
11165         case DW_OP_lit18:
11166         case DW_OP_lit19:
11167         case DW_OP_lit20:
11168         case DW_OP_lit21:
11169         case DW_OP_lit22:
11170         case DW_OP_lit23:
11171         case DW_OP_lit24:
11172         case DW_OP_lit25:
11173         case DW_OP_lit26:
11174         case DW_OP_lit27:
11175         case DW_OP_lit28:
11176         case DW_OP_lit29:
11177         case DW_OP_lit30:
11178         case DW_OP_lit31:
11179           stack[++stacki] = op - DW_OP_lit0;
11180           break;
11181
11182         case DW_OP_reg0:
11183         case DW_OP_reg1:
11184         case DW_OP_reg2:
11185         case DW_OP_reg3:
11186         case DW_OP_reg4:
11187         case DW_OP_reg5:
11188         case DW_OP_reg6:
11189         case DW_OP_reg7:
11190         case DW_OP_reg8:
11191         case DW_OP_reg9:
11192         case DW_OP_reg10:
11193         case DW_OP_reg11:
11194         case DW_OP_reg12:
11195         case DW_OP_reg13:
11196         case DW_OP_reg14:
11197         case DW_OP_reg15:
11198         case DW_OP_reg16:
11199         case DW_OP_reg17:
11200         case DW_OP_reg18:
11201         case DW_OP_reg19:
11202         case DW_OP_reg20:
11203         case DW_OP_reg21:
11204         case DW_OP_reg22:
11205         case DW_OP_reg23:
11206         case DW_OP_reg24:
11207         case DW_OP_reg25:
11208         case DW_OP_reg26:
11209         case DW_OP_reg27:
11210         case DW_OP_reg28:
11211         case DW_OP_reg29:
11212         case DW_OP_reg30:
11213         case DW_OP_reg31:
11214           stack[++stacki] = op - DW_OP_reg0;
11215           if (i < size)
11216             dwarf2_complex_location_expr_complaint ();
11217           break;
11218
11219         case DW_OP_regx:
11220           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
11221           i += bytes_read;
11222           stack[++stacki] = unsnd;
11223           if (i < size)
11224             dwarf2_complex_location_expr_complaint ();
11225           break;
11226
11227         case DW_OP_addr:
11228           stack[++stacki] = read_address (objfile->obfd, &data[i],
11229                                           cu, &bytes_read);
11230           i += bytes_read;
11231           break;
11232
11233         case DW_OP_const1u:
11234           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
11235           i += 1;
11236           break;
11237
11238         case DW_OP_const1s:
11239           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
11240           i += 1;
11241           break;
11242
11243         case DW_OP_const2u:
11244           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
11245           i += 2;
11246           break;
11247
11248         case DW_OP_const2s:
11249           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
11250           i += 2;
11251           break;
11252
11253         case DW_OP_const4u:
11254           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
11255           i += 4;
11256           break;
11257
11258         case DW_OP_const4s:
11259           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
11260           i += 4;
11261           break;
11262
11263         case DW_OP_constu:
11264           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
11265                                                   &bytes_read);
11266           i += bytes_read;
11267           break;
11268
11269         case DW_OP_consts:
11270           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
11271           i += bytes_read;
11272           break;
11273
11274         case DW_OP_dup:
11275           stack[stacki + 1] = stack[stacki];
11276           stacki++;
11277           break;
11278
11279         case DW_OP_plus:
11280           stack[stacki - 1] += stack[stacki];
11281           stacki--;
11282           break;
11283
11284         case DW_OP_plus_uconst:
11285           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
11286           i += bytes_read;
11287           break;
11288
11289         case DW_OP_minus:
11290           stack[stacki - 1] -= stack[stacki];
11291           stacki--;
11292           break;
11293
11294         case DW_OP_deref:
11295           /* If we're not the last op, then we definitely can't encode
11296              this using GDB's address_class enum.  This is valid for partial
11297              global symbols, although the variable's address will be bogus
11298              in the psymtab.  */
11299           if (i < size)
11300             dwarf2_complex_location_expr_complaint ();
11301           break;
11302
11303         case DW_OP_GNU_push_tls_address:
11304           /* The top of the stack has the offset from the beginning
11305              of the thread control block at which the variable is located.  */
11306           /* Nothing should follow this operator, so the top of stack would
11307              be returned.  */
11308           /* This is valid for partial global symbols, but the variable's
11309              address will be bogus in the psymtab.  */
11310           if (i < size)
11311             dwarf2_complex_location_expr_complaint ();
11312           break;
11313
11314         case DW_OP_GNU_uninit:
11315           break;
11316
11317         default:
11318           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
11319                      dwarf_stack_op_name (op, 1));
11320           return (stack[stacki]);
11321         }
11322     }
11323   return (stack[stacki]);
11324 }
11325
11326 /* memory allocation interface */
11327
11328 static struct dwarf_block *
11329 dwarf_alloc_block (struct dwarf2_cu *cu)
11330 {
11331   struct dwarf_block *blk;
11332
11333   blk = (struct dwarf_block *)
11334     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
11335   return (blk);
11336 }
11337
11338 static struct abbrev_info *
11339 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
11340 {
11341   struct abbrev_info *abbrev;
11342
11343   abbrev = (struct abbrev_info *)
11344     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
11345   memset (abbrev, 0, sizeof (struct abbrev_info));
11346   return (abbrev);
11347 }
11348
11349 static struct die_info *
11350 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
11351 {
11352   struct die_info *die;
11353   size_t size = sizeof (struct die_info);
11354
11355   if (num_attrs > 1)
11356     size += (num_attrs - 1) * sizeof (struct attribute);
11357
11358   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
11359   memset (die, 0, sizeof (struct die_info));
11360   return (die);
11361 }
11362
11363 \f
11364 /* Macro support.  */
11365
11366
11367 /* Return the full name of file number I in *LH's file name table.
11368    Use COMP_DIR as the name of the current directory of the
11369    compilation.  The result is allocated using xmalloc; the caller is
11370    responsible for freeing it.  */
11371 static char *
11372 file_full_name (int file, struct line_header *lh, const char *comp_dir)
11373 {
11374   /* Is the file number a valid index into the line header's file name
11375      table?  Remember that file numbers start with one, not zero.  */
11376   if (1 <= file && file <= lh->num_file_names)
11377     {
11378       struct file_entry *fe = &lh->file_names[file - 1];
11379
11380       if (IS_ABSOLUTE_PATH (fe->name))
11381         return xstrdup (fe->name);
11382       else
11383         {
11384           const char *dir;
11385           int dir_len;
11386           char *full_name;
11387
11388           if (fe->dir_index)
11389             dir = lh->include_dirs[fe->dir_index - 1];
11390           else
11391             dir = comp_dir;
11392
11393           if (dir)
11394             {
11395               dir_len = strlen (dir);
11396               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
11397               strcpy (full_name, dir);
11398               full_name[dir_len] = '/';
11399               strcpy (full_name + dir_len + 1, fe->name);
11400               return full_name;
11401             }
11402           else
11403             return xstrdup (fe->name);
11404         }
11405     }
11406   else
11407     {
11408       /* The compiler produced a bogus file number.  We can at least
11409          record the macro definitions made in the file, even if we
11410          won't be able to find the file by name.  */
11411       char fake_name[80];
11412
11413       sprintf (fake_name, "<bad macro file number %d>", file);
11414
11415       complaint (&symfile_complaints,
11416                  _("bad file number in macro information (%d)"),
11417                  file);
11418
11419       return xstrdup (fake_name);
11420     }
11421 }
11422
11423
11424 static struct macro_source_file *
11425 macro_start_file (int file, int line,
11426                   struct macro_source_file *current_file,
11427                   const char *comp_dir,
11428                   struct line_header *lh, struct objfile *objfile)
11429 {
11430   /* The full name of this source file.  */
11431   char *full_name = file_full_name (file, lh, comp_dir);
11432
11433   /* We don't create a macro table for this compilation unit
11434      at all until we actually get a filename.  */
11435   if (! pending_macros)
11436     pending_macros = new_macro_table (&objfile->objfile_obstack,
11437                                       objfile->macro_cache);
11438
11439   if (! current_file)
11440     /* If we have no current file, then this must be the start_file
11441        directive for the compilation unit's main source file.  */
11442     current_file = macro_set_main (pending_macros, full_name);
11443   else
11444     current_file = macro_include (current_file, line, full_name);
11445
11446   xfree (full_name);
11447
11448   return current_file;
11449 }
11450
11451
11452 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
11453    followed by a null byte.  */
11454 static char *
11455 copy_string (const char *buf, int len)
11456 {
11457   char *s = xmalloc (len + 1);
11458
11459   memcpy (s, buf, len);
11460   s[len] = '\0';
11461   return s;
11462 }
11463
11464
11465 static const char *
11466 consume_improper_spaces (const char *p, const char *body)
11467 {
11468   if (*p == ' ')
11469     {
11470       complaint (&symfile_complaints,
11471                  _("macro definition contains spaces in formal argument list:\n`%s'"),
11472                  body);
11473
11474       while (*p == ' ')
11475         p++;
11476     }
11477
11478   return p;
11479 }
11480
11481
11482 static void
11483 parse_macro_definition (struct macro_source_file *file, int line,
11484                         const char *body)
11485 {
11486   const char *p;
11487
11488   /* The body string takes one of two forms.  For object-like macro
11489      definitions, it should be:
11490
11491         <macro name> " " <definition>
11492
11493      For function-like macro definitions, it should be:
11494
11495         <macro name> "() " <definition>
11496      or
11497         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
11498
11499      Spaces may appear only where explicitly indicated, and in the
11500      <definition>.
11501
11502      The Dwarf 2 spec says that an object-like macro's name is always
11503      followed by a space, but versions of GCC around March 2002 omit
11504      the space when the macro's definition is the empty string.
11505
11506      The Dwarf 2 spec says that there should be no spaces between the
11507      formal arguments in a function-like macro's formal argument list,
11508      but versions of GCC around March 2002 include spaces after the
11509      commas.  */
11510
11511
11512   /* Find the extent of the macro name.  The macro name is terminated
11513      by either a space or null character (for an object-like macro) or
11514      an opening paren (for a function-like macro).  */
11515   for (p = body; *p; p++)
11516     if (*p == ' ' || *p == '(')
11517       break;
11518
11519   if (*p == ' ' || *p == '\0')
11520     {
11521       /* It's an object-like macro.  */
11522       int name_len = p - body;
11523       char *name = copy_string (body, name_len);
11524       const char *replacement;
11525
11526       if (*p == ' ')
11527         replacement = body + name_len + 1;
11528       else
11529         {
11530           dwarf2_macro_malformed_definition_complaint (body);
11531           replacement = body + name_len;
11532         }
11533
11534       macro_define_object (file, line, name, replacement);
11535
11536       xfree (name);
11537     }
11538   else if (*p == '(')
11539     {
11540       /* It's a function-like macro.  */
11541       char *name = copy_string (body, p - body);
11542       int argc = 0;
11543       int argv_size = 1;
11544       char **argv = xmalloc (argv_size * sizeof (*argv));
11545
11546       p++;
11547
11548       p = consume_improper_spaces (p, body);
11549
11550       /* Parse the formal argument list.  */
11551       while (*p && *p != ')')
11552         {
11553           /* Find the extent of the current argument name.  */
11554           const char *arg_start = p;
11555
11556           while (*p && *p != ',' && *p != ')' && *p != ' ')
11557             p++;
11558
11559           if (! *p || p == arg_start)
11560             dwarf2_macro_malformed_definition_complaint (body);
11561           else
11562             {
11563               /* Make sure argv has room for the new argument.  */
11564               if (argc >= argv_size)
11565                 {
11566                   argv_size *= 2;
11567                   argv = xrealloc (argv, argv_size * sizeof (*argv));
11568                 }
11569
11570               argv[argc++] = copy_string (arg_start, p - arg_start);
11571             }
11572
11573           p = consume_improper_spaces (p, body);
11574
11575           /* Consume the comma, if present.  */
11576           if (*p == ',')
11577             {
11578               p++;
11579
11580               p = consume_improper_spaces (p, body);
11581             }
11582         }
11583
11584       if (*p == ')')
11585         {
11586           p++;
11587
11588           if (*p == ' ')
11589             /* Perfectly formed definition, no complaints.  */
11590             macro_define_function (file, line, name,
11591                                    argc, (const char **) argv,
11592                                    p + 1);
11593           else if (*p == '\0')
11594             {
11595               /* Complain, but do define it.  */
11596               dwarf2_macro_malformed_definition_complaint (body);
11597               macro_define_function (file, line, name,
11598                                      argc, (const char **) argv,
11599                                      p);
11600             }
11601           else
11602             /* Just complain.  */
11603             dwarf2_macro_malformed_definition_complaint (body);
11604         }
11605       else
11606         /* Just complain.  */
11607         dwarf2_macro_malformed_definition_complaint (body);
11608
11609       xfree (name);
11610       {
11611         int i;
11612
11613         for (i = 0; i < argc; i++)
11614           xfree (argv[i]);
11615       }
11616       xfree (argv);
11617     }
11618   else
11619     dwarf2_macro_malformed_definition_complaint (body);
11620 }
11621
11622
11623 static void
11624 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
11625                      char *comp_dir, bfd *abfd,
11626                      struct dwarf2_cu *cu)
11627 {
11628   gdb_byte *mac_ptr, *mac_end;
11629   struct macro_source_file *current_file = 0;
11630   enum dwarf_macinfo_record_type macinfo_type;
11631   int at_commandline;
11632
11633   dwarf2_read_section (dwarf2_per_objfile->objfile,
11634                        &dwarf2_per_objfile->macinfo);
11635   if (dwarf2_per_objfile->macinfo.buffer == NULL)
11636     {
11637       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
11638       return;
11639     }
11640
11641   /* First pass: Find the name of the base filename.
11642      This filename is needed in order to process all macros whose definition
11643      (or undefinition) comes from the command line.  These macros are defined
11644      before the first DW_MACINFO_start_file entry, and yet still need to be
11645      associated to the base file.
11646
11647      To determine the base file name, we scan the macro definitions until we
11648      reach the first DW_MACINFO_start_file entry.  We then initialize
11649      CURRENT_FILE accordingly so that any macro definition found before the
11650      first DW_MACINFO_start_file can still be associated to the base file.  */
11651
11652   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11653   mac_end = dwarf2_per_objfile->macinfo.buffer
11654     + dwarf2_per_objfile->macinfo.size;
11655
11656   do
11657     {
11658       /* Do we at least have room for a macinfo type byte?  */
11659       if (mac_ptr >= mac_end)
11660         {
11661           /* Complaint is printed during the second pass as GDB will probably
11662              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
11663           break;
11664         }
11665
11666       macinfo_type = read_1_byte (abfd, mac_ptr);
11667       mac_ptr++;
11668
11669       switch (macinfo_type)
11670         {
11671           /* A zero macinfo type indicates the end of the macro
11672              information.  */
11673         case 0:
11674           break;
11675
11676         case DW_MACINFO_define:
11677         case DW_MACINFO_undef:
11678           /* Only skip the data by MAC_PTR.  */
11679           {
11680             unsigned int bytes_read;
11681
11682             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11683             mac_ptr += bytes_read;
11684             read_string (abfd, mac_ptr, &bytes_read);
11685             mac_ptr += bytes_read;
11686           }
11687           break;
11688
11689         case DW_MACINFO_start_file:
11690           {
11691             unsigned int bytes_read;
11692             int line, file;
11693
11694             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11695             mac_ptr += bytes_read;
11696             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11697             mac_ptr += bytes_read;
11698
11699             current_file = macro_start_file (file, line, current_file, comp_dir,
11700                                              lh, cu->objfile);
11701           }
11702           break;
11703
11704         case DW_MACINFO_end_file:
11705           /* No data to skip by MAC_PTR.  */
11706           break;
11707
11708         case DW_MACINFO_vendor_ext:
11709           /* Only skip the data by MAC_PTR.  */
11710           {
11711             unsigned int bytes_read;
11712
11713             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11714             mac_ptr += bytes_read;
11715             read_string (abfd, mac_ptr, &bytes_read);
11716             mac_ptr += bytes_read;
11717           }
11718           break;
11719
11720         default:
11721           break;
11722         }
11723     } while (macinfo_type != 0 && current_file == NULL);
11724
11725   /* Second pass: Process all entries.
11726
11727      Use the AT_COMMAND_LINE flag to determine whether we are still processing
11728      command-line macro definitions/undefinitions.  This flag is unset when we
11729      reach the first DW_MACINFO_start_file entry.  */
11730
11731   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11732
11733   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
11734      GDB is still reading the definitions from command line.  First
11735      DW_MACINFO_start_file will need to be ignored as it was already executed
11736      to create CURRENT_FILE for the main source holding also the command line
11737      definitions.  On first met DW_MACINFO_start_file this flag is reset to
11738      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
11739
11740   at_commandline = 1;
11741
11742   do
11743     {
11744       /* Do we at least have room for a macinfo type byte?  */
11745       if (mac_ptr >= mac_end)
11746         {
11747           dwarf2_macros_too_long_complaint ();
11748           break;
11749         }
11750
11751       macinfo_type = read_1_byte (abfd, mac_ptr);
11752       mac_ptr++;
11753
11754       switch (macinfo_type)
11755         {
11756           /* A zero macinfo type indicates the end of the macro
11757              information.  */
11758         case 0:
11759           break;
11760
11761         case DW_MACINFO_define:
11762         case DW_MACINFO_undef:
11763           {
11764             unsigned int bytes_read;
11765             int line;
11766             char *body;
11767
11768             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11769             mac_ptr += bytes_read;
11770             body = read_string (abfd, mac_ptr, &bytes_read);
11771             mac_ptr += bytes_read;
11772
11773             if (! current_file)
11774               {
11775                 /* DWARF violation as no main source is present.  */
11776                 complaint (&symfile_complaints,
11777                            _("debug info with no main source gives macro %s "
11778                              "on line %d: %s"),
11779                            macinfo_type == DW_MACINFO_define ?
11780                              _("definition") :
11781                                macinfo_type == DW_MACINFO_undef ?
11782                                  _("undefinition") :
11783                                  _("something-or-other"), line, body);
11784                 break;
11785               }
11786             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11787               complaint (&symfile_complaints,
11788                          _("debug info gives %s macro %s with %s line %d: %s"),
11789                          at_commandline ? _("command-line") : _("in-file"),
11790                          macinfo_type == DW_MACINFO_define ?
11791                            _("definition") :
11792                              macinfo_type == DW_MACINFO_undef ?
11793                                _("undefinition") :
11794                                _("something-or-other"),
11795                          line == 0 ? _("zero") : _("non-zero"), line, body);
11796
11797             if (macinfo_type == DW_MACINFO_define)
11798               parse_macro_definition (current_file, line, body);
11799             else if (macinfo_type == DW_MACINFO_undef)
11800               macro_undef (current_file, line, body);
11801           }
11802           break;
11803
11804         case DW_MACINFO_start_file:
11805           {
11806             unsigned int bytes_read;
11807             int line, file;
11808
11809             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11810             mac_ptr += bytes_read;
11811             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11812             mac_ptr += bytes_read;
11813
11814             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11815               complaint (&symfile_complaints,
11816                          _("debug info gives source %d included "
11817                            "from %s at %s line %d"),
11818                          file, at_commandline ? _("command-line") : _("file"),
11819                          line == 0 ? _("zero") : _("non-zero"), line);
11820
11821             if (at_commandline)
11822               {
11823                 /* This DW_MACINFO_start_file was executed in the pass one.  */
11824                 at_commandline = 0;
11825               }
11826             else
11827               current_file = macro_start_file (file, line,
11828                                                current_file, comp_dir,
11829                                                lh, cu->objfile);
11830           }
11831           break;
11832
11833         case DW_MACINFO_end_file:
11834           if (! current_file)
11835             complaint (&symfile_complaints,
11836                        _("macro debug info has an unmatched `close_file' directive"));
11837           else
11838             {
11839               current_file = current_file->included_by;
11840               if (! current_file)
11841                 {
11842                   enum dwarf_macinfo_record_type next_type;
11843
11844                   /* GCC circa March 2002 doesn't produce the zero
11845                      type byte marking the end of the compilation
11846                      unit.  Complain if it's not there, but exit no
11847                      matter what.  */
11848
11849                   /* Do we at least have room for a macinfo type byte?  */
11850                   if (mac_ptr >= mac_end)
11851                     {
11852                       dwarf2_macros_too_long_complaint ();
11853                       return;
11854                     }
11855
11856                   /* We don't increment mac_ptr here, so this is just
11857                      a look-ahead.  */
11858                   next_type = read_1_byte (abfd, mac_ptr);
11859                   if (next_type != 0)
11860                     complaint (&symfile_complaints,
11861                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
11862
11863                   return;
11864                 }
11865             }
11866           break;
11867
11868         case DW_MACINFO_vendor_ext:
11869           {
11870             unsigned int bytes_read;
11871             int constant;
11872             char *string;
11873
11874             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11875             mac_ptr += bytes_read;
11876             string = read_string (abfd, mac_ptr, &bytes_read);
11877             mac_ptr += bytes_read;
11878
11879             /* We don't recognize any vendor extensions.  */
11880           }
11881           break;
11882         }
11883     } while (macinfo_type != 0);
11884 }
11885
11886 /* Check if the attribute's form is a DW_FORM_block*
11887    if so return true else false. */
11888 static int
11889 attr_form_is_block (struct attribute *attr)
11890 {
11891   return (attr == NULL ? 0 :
11892       attr->form == DW_FORM_block1
11893       || attr->form == DW_FORM_block2
11894       || attr->form == DW_FORM_block4
11895       || attr->form == DW_FORM_block
11896       || attr->form == DW_FORM_exprloc);
11897 }
11898
11899 /* Return non-zero if ATTR's value is a section offset --- classes
11900    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11901    You may use DW_UNSND (attr) to retrieve such offsets.
11902
11903    Section 7.5.4, "Attribute Encodings", explains that no attribute
11904    may have a value that belongs to more than one of these classes; it
11905    would be ambiguous if we did, because we use the same forms for all
11906    of them.  */
11907 static int
11908 attr_form_is_section_offset (struct attribute *attr)
11909 {
11910   return (attr->form == DW_FORM_data4
11911           || attr->form == DW_FORM_data8
11912           || attr->form == DW_FORM_sec_offset);
11913 }
11914
11915
11916 /* Return non-zero if ATTR's value falls in the 'constant' class, or
11917    zero otherwise.  When this function returns true, you can apply
11918    dwarf2_get_attr_constant_value to it.
11919
11920    However, note that for some attributes you must check
11921    attr_form_is_section_offset before using this test.  DW_FORM_data4
11922    and DW_FORM_data8 are members of both the constant class, and of
11923    the classes that contain offsets into other debug sections
11924    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
11925    that, if an attribute's can be either a constant or one of the
11926    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11927    taken as section offsets, not constants.  */
11928 static int
11929 attr_form_is_constant (struct attribute *attr)
11930 {
11931   switch (attr->form)
11932     {
11933     case DW_FORM_sdata:
11934     case DW_FORM_udata:
11935     case DW_FORM_data1:
11936     case DW_FORM_data2:
11937     case DW_FORM_data4:
11938     case DW_FORM_data8:
11939       return 1;
11940     default:
11941       return 0;
11942     }
11943 }
11944
11945 static void
11946 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
11947                              struct dwarf2_cu *cu)
11948 {
11949   if (attr_form_is_section_offset (attr)
11950       /* ".debug_loc" may not exist at all, or the offset may be outside
11951          the section.  If so, fall through to the complaint in the
11952          other branch.  */
11953       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
11954     {
11955       struct dwarf2_loclist_baton *baton;
11956
11957       baton = obstack_alloc (&cu->objfile->objfile_obstack,
11958                              sizeof (struct dwarf2_loclist_baton));
11959       baton->per_cu = cu->per_cu;
11960       gdb_assert (baton->per_cu);
11961
11962       dwarf2_read_section (dwarf2_per_objfile->objfile,
11963                            &dwarf2_per_objfile->loc);
11964
11965       /* We don't know how long the location list is, but make sure we
11966          don't run off the edge of the section.  */
11967       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11968       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
11969       baton->base_address = cu->base_address;
11970       if (cu->base_known == 0)
11971         complaint (&symfile_complaints,
11972                    _("Location list used without specifying the CU base address."));
11973
11974       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
11975       SYMBOL_LOCATION_BATON (sym) = baton;
11976     }
11977   else
11978     {
11979       struct dwarf2_locexpr_baton *baton;
11980
11981       baton = obstack_alloc (&cu->objfile->objfile_obstack,
11982                              sizeof (struct dwarf2_locexpr_baton));
11983       baton->per_cu = cu->per_cu;
11984       gdb_assert (baton->per_cu);
11985
11986       if (attr_form_is_block (attr))
11987         {
11988           /* Note that we're just copying the block's data pointer
11989              here, not the actual data.  We're still pointing into the
11990              info_buffer for SYM's objfile; right now we never release
11991              that buffer, but when we do clean up properly this may
11992              need to change.  */
11993           baton->size = DW_BLOCK (attr)->size;
11994           baton->data = DW_BLOCK (attr)->data;
11995         }
11996       else
11997         {
11998           dwarf2_invalid_attrib_class_complaint ("location description",
11999                                                  SYMBOL_NATURAL_NAME (sym));
12000           baton->size = 0;
12001           baton->data = NULL;
12002         }
12003
12004       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12005       SYMBOL_LOCATION_BATON (sym) = baton;
12006     }
12007 }
12008
12009 /* Return the OBJFILE associated with the compilation unit CU.  */
12010
12011 struct objfile *
12012 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
12013 {
12014   struct objfile *objfile = per_cu->psymtab->objfile;
12015
12016   /* Return the master objfile, so that we can report and look up the
12017      correct file containing this variable.  */
12018   if (objfile->separate_debug_objfile_backlink)
12019     objfile = objfile->separate_debug_objfile_backlink;
12020
12021   return objfile;
12022 }
12023
12024 /* Return the address size given in the compilation unit header for CU.  */
12025
12026 CORE_ADDR
12027 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
12028 {
12029   if (per_cu->cu)
12030     return per_cu->cu->header.addr_size;
12031   else
12032     {
12033       /* If the CU is not currently read in, we re-read its header.  */
12034       struct objfile *objfile = per_cu->psymtab->objfile;
12035       struct dwarf2_per_objfile *per_objfile
12036         = objfile_data (objfile, dwarf2_objfile_data_key);
12037       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
12038       struct comp_unit_head cu_header;
12039
12040       memset (&cu_header, 0, sizeof cu_header);
12041       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
12042       return cu_header.addr_size;
12043     }
12044 }
12045
12046 /* Return the offset size given in the compilation unit header for CU.  */
12047
12048 int
12049 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
12050 {
12051   if (per_cu->cu)
12052     return per_cu->cu->header.offset_size;
12053   else
12054     {
12055       /* If the CU is not currently read in, we re-read its header.  */
12056       struct objfile *objfile = per_cu->psymtab->objfile;
12057       struct dwarf2_per_objfile *per_objfile
12058         = objfile_data (objfile, dwarf2_objfile_data_key);
12059       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
12060       struct comp_unit_head cu_header;
12061
12062       memset (&cu_header, 0, sizeof cu_header);
12063       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
12064       return cu_header.offset_size;
12065     }
12066 }
12067
12068 /* Locate the .debug_info compilation unit from CU's objfile which contains
12069    the DIE at OFFSET.  Raises an error on failure.  */
12070
12071 static struct dwarf2_per_cu_data *
12072 dwarf2_find_containing_comp_unit (unsigned int offset,
12073                                   struct objfile *objfile)
12074 {
12075   struct dwarf2_per_cu_data *this_cu;
12076   int low, high;
12077
12078   low = 0;
12079   high = dwarf2_per_objfile->n_comp_units - 1;
12080   while (high > low)
12081     {
12082       int mid = low + (high - low) / 2;
12083
12084       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
12085         high = mid;
12086       else
12087         low = mid + 1;
12088     }
12089   gdb_assert (low == high);
12090   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
12091     {
12092       if (low == 0)
12093         error (_("Dwarf Error: could not find partial DIE containing "
12094                "offset 0x%lx [in module %s]"),
12095                (long) offset, bfd_get_filename (objfile->obfd));
12096
12097       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
12098       return dwarf2_per_objfile->all_comp_units[low-1];
12099     }
12100   else
12101     {
12102       this_cu = dwarf2_per_objfile->all_comp_units[low];
12103       if (low == dwarf2_per_objfile->n_comp_units - 1
12104           && offset >= this_cu->offset + this_cu->length)
12105         error (_("invalid dwarf2 offset %u"), offset);
12106       gdb_assert (offset < this_cu->offset + this_cu->length);
12107       return this_cu;
12108     }
12109 }
12110
12111 /* Locate the compilation unit from OBJFILE which is located at exactly
12112    OFFSET.  Raises an error on failure.  */
12113
12114 static struct dwarf2_per_cu_data *
12115 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
12116 {
12117   struct dwarf2_per_cu_data *this_cu;
12118
12119   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
12120   if (this_cu->offset != offset)
12121     error (_("no compilation unit with offset %u."), offset);
12122   return this_cu;
12123 }
12124
12125 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
12126
12127 static struct dwarf2_cu *
12128 alloc_one_comp_unit (struct objfile *objfile)
12129 {
12130   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
12131   cu->objfile = objfile;
12132   obstack_init (&cu->comp_unit_obstack);
12133   return cu;
12134 }
12135
12136 /* Release one cached compilation unit, CU.  We unlink it from the tree
12137    of compilation units, but we don't remove it from the read_in_chain;
12138    the caller is responsible for that.
12139    NOTE: DATA is a void * because this function is also used as a
12140    cleanup routine.  */
12141
12142 static void
12143 free_one_comp_unit (void *data)
12144 {
12145   struct dwarf2_cu *cu = data;
12146
12147   if (cu->per_cu != NULL)
12148     cu->per_cu->cu = NULL;
12149   cu->per_cu = NULL;
12150
12151   obstack_free (&cu->comp_unit_obstack, NULL);
12152
12153   xfree (cu);
12154 }
12155
12156 /* This cleanup function is passed the address of a dwarf2_cu on the stack
12157    when we're finished with it.  We can't free the pointer itself, but be
12158    sure to unlink it from the cache.  Also release any associated storage
12159    and perform cache maintenance.
12160
12161    Only used during partial symbol parsing.  */
12162
12163 static void
12164 free_stack_comp_unit (void *data)
12165 {
12166   struct dwarf2_cu *cu = data;
12167
12168   obstack_free (&cu->comp_unit_obstack, NULL);
12169   cu->partial_dies = NULL;
12170
12171   if (cu->per_cu != NULL)
12172     {
12173       /* This compilation unit is on the stack in our caller, so we
12174          should not xfree it.  Just unlink it.  */
12175       cu->per_cu->cu = NULL;
12176       cu->per_cu = NULL;
12177
12178       /* If we had a per-cu pointer, then we may have other compilation
12179          units loaded, so age them now.  */
12180       age_cached_comp_units ();
12181     }
12182 }
12183
12184 /* Free all cached compilation units.  */
12185
12186 static void
12187 free_cached_comp_units (void *data)
12188 {
12189   struct dwarf2_per_cu_data *per_cu, **last_chain;
12190
12191   per_cu = dwarf2_per_objfile->read_in_chain;
12192   last_chain = &dwarf2_per_objfile->read_in_chain;
12193   while (per_cu != NULL)
12194     {
12195       struct dwarf2_per_cu_data *next_cu;
12196
12197       next_cu = per_cu->cu->read_in_chain;
12198
12199       free_one_comp_unit (per_cu->cu);
12200       *last_chain = next_cu;
12201
12202       per_cu = next_cu;
12203     }
12204 }
12205
12206 /* Increase the age counter on each cached compilation unit, and free
12207    any that are too old.  */
12208
12209 static void
12210 age_cached_comp_units (void)
12211 {
12212   struct dwarf2_per_cu_data *per_cu, **last_chain;
12213
12214   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
12215   per_cu = dwarf2_per_objfile->read_in_chain;
12216   while (per_cu != NULL)
12217     {
12218       per_cu->cu->last_used ++;
12219       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
12220         dwarf2_mark (per_cu->cu);
12221       per_cu = per_cu->cu->read_in_chain;
12222     }
12223
12224   per_cu = dwarf2_per_objfile->read_in_chain;
12225   last_chain = &dwarf2_per_objfile->read_in_chain;
12226   while (per_cu != NULL)
12227     {
12228       struct dwarf2_per_cu_data *next_cu;
12229
12230       next_cu = per_cu->cu->read_in_chain;
12231
12232       if (!per_cu->cu->mark)
12233         {
12234           free_one_comp_unit (per_cu->cu);
12235           *last_chain = next_cu;
12236         }
12237       else
12238         last_chain = &per_cu->cu->read_in_chain;
12239
12240       per_cu = next_cu;
12241     }
12242 }
12243
12244 /* Remove a single compilation unit from the cache.  */
12245
12246 static void
12247 free_one_cached_comp_unit (void *target_cu)
12248 {
12249   struct dwarf2_per_cu_data *per_cu, **last_chain;
12250
12251   per_cu = dwarf2_per_objfile->read_in_chain;
12252   last_chain = &dwarf2_per_objfile->read_in_chain;
12253   while (per_cu != NULL)
12254     {
12255       struct dwarf2_per_cu_data *next_cu;
12256
12257       next_cu = per_cu->cu->read_in_chain;
12258
12259       if (per_cu->cu == target_cu)
12260         {
12261           free_one_comp_unit (per_cu->cu);
12262           *last_chain = next_cu;
12263           break;
12264         }
12265       else
12266         last_chain = &per_cu->cu->read_in_chain;
12267
12268       per_cu = next_cu;
12269     }
12270 }
12271
12272 /* Release all extra memory associated with OBJFILE.  */
12273
12274 void
12275 dwarf2_free_objfile (struct objfile *objfile)
12276 {
12277   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
12278
12279   if (dwarf2_per_objfile == NULL)
12280     return;
12281
12282   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
12283   free_cached_comp_units (NULL);
12284
12285   /* Everything else should be on the objfile obstack.  */
12286 }
12287
12288 /* A pair of DIE offset and GDB type pointer.  We store these
12289    in a hash table separate from the DIEs, and preserve them
12290    when the DIEs are flushed out of cache.  */
12291
12292 struct dwarf2_offset_and_type
12293 {
12294   unsigned int offset;
12295   struct type *type;
12296 };
12297
12298 /* Hash function for a dwarf2_offset_and_type.  */
12299
12300 static hashval_t
12301 offset_and_type_hash (const void *item)
12302 {
12303   const struct dwarf2_offset_and_type *ofs = item;
12304
12305   return ofs->offset;
12306 }
12307
12308 /* Equality function for a dwarf2_offset_and_type.  */
12309
12310 static int
12311 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
12312 {
12313   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
12314   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
12315
12316   return ofs_lhs->offset == ofs_rhs->offset;
12317 }
12318
12319 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
12320    table if necessary.  For convenience, return TYPE.
12321
12322    The DIEs reading must have careful ordering to:
12323     * Not cause infite loops trying to read in DIEs as a prerequisite for
12324       reading current DIE.
12325     * Not trying to dereference contents of still incompletely read in types
12326       while reading in other DIEs.
12327     * Enable referencing still incompletely read in types just by a pointer to
12328       the type without accessing its fields.
12329
12330    Therefore caller should follow these rules:
12331      * Try to fetch any prerequisite types we may need to build this DIE type
12332        before building the type and calling set_die_type.
12333      * After building typer call set_die_type for current DIE as soon as
12334        possible before fetching more types to complete the current type.
12335      * Make the type as complete as possible before fetching more types.  */
12336
12337 static struct type *
12338 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12339 {
12340   struct dwarf2_offset_and_type **slot, ofs;
12341
12342   /* For Ada types, make sure that the gnat-specific data is always
12343      initialized (if not already set).  There are a few types where
12344      we should not be doing so, because the type-specific area is
12345      already used to hold some other piece of info (eg: TYPE_CODE_FLT
12346      where the type-specific area is used to store the floatformat).
12347      But this is not a problem, because the gnat-specific information
12348      is actually not needed for these types.  */
12349   if (need_gnat_info (cu)
12350       && TYPE_CODE (type) != TYPE_CODE_FUNC
12351       && TYPE_CODE (type) != TYPE_CODE_FLT
12352       && !HAVE_GNAT_AUX_INFO (type))
12353     INIT_GNAT_SPECIFIC (type);
12354
12355   if (cu->type_hash == NULL)
12356     {
12357       gdb_assert (cu->per_cu != NULL);
12358       cu->per_cu->type_hash
12359         = htab_create_alloc_ex (cu->header.length / 24,
12360                                 offset_and_type_hash,
12361                                 offset_and_type_eq,
12362                                 NULL,
12363                                 &cu->objfile->objfile_obstack,
12364                                 hashtab_obstack_allocate,
12365                                 dummy_obstack_deallocate);
12366       cu->type_hash = cu->per_cu->type_hash;
12367     }
12368
12369   ofs.offset = die->offset;
12370   ofs.type = type;
12371   slot = (struct dwarf2_offset_and_type **)
12372     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
12373   if (*slot)
12374     complaint (&symfile_complaints,
12375                _("A problem internal to GDB: DIE 0x%x has type already set"),
12376                die->offset);
12377   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
12378   **slot = ofs;
12379   return type;
12380 }
12381
12382 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
12383    not have a saved type.  */
12384
12385 static struct type *
12386 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
12387 {
12388   struct dwarf2_offset_and_type *slot, ofs;
12389   htab_t type_hash = cu->type_hash;
12390
12391   if (type_hash == NULL)
12392     return NULL;
12393
12394   ofs.offset = die->offset;
12395   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
12396   if (slot)
12397     return slot->type;
12398   else
12399     return NULL;
12400 }
12401
12402 /* Add a dependence relationship from CU to REF_PER_CU.  */
12403
12404 static void
12405 dwarf2_add_dependence (struct dwarf2_cu *cu,
12406                        struct dwarf2_per_cu_data *ref_per_cu)
12407 {
12408   void **slot;
12409
12410   if (cu->dependencies == NULL)
12411     cu->dependencies
12412       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
12413                               NULL, &cu->comp_unit_obstack,
12414                               hashtab_obstack_allocate,
12415                               dummy_obstack_deallocate);
12416
12417   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
12418   if (*slot == NULL)
12419     *slot = ref_per_cu;
12420 }
12421
12422 /* Subroutine of dwarf2_mark to pass to htab_traverse.
12423    Set the mark field in every compilation unit in the
12424    cache that we must keep because we are keeping CU.  */
12425
12426 static int
12427 dwarf2_mark_helper (void **slot, void *data)
12428 {
12429   struct dwarf2_per_cu_data *per_cu;
12430
12431   per_cu = (struct dwarf2_per_cu_data *) *slot;
12432   if (per_cu->cu->mark)
12433     return 1;
12434   per_cu->cu->mark = 1;
12435
12436   if (per_cu->cu->dependencies != NULL)
12437     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
12438
12439   return 1;
12440 }
12441
12442 /* Set the mark field in CU and in every other compilation unit in the
12443    cache that we must keep because we are keeping CU.  */
12444
12445 static void
12446 dwarf2_mark (struct dwarf2_cu *cu)
12447 {
12448   if (cu->mark)
12449     return;
12450   cu->mark = 1;
12451   if (cu->dependencies != NULL)
12452     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
12453 }
12454
12455 static void
12456 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
12457 {
12458   while (per_cu)
12459     {
12460       per_cu->cu->mark = 0;
12461       per_cu = per_cu->cu->read_in_chain;
12462     }
12463 }
12464
12465 /* Trivial hash function for partial_die_info: the hash value of a DIE
12466    is its offset in .debug_info for this objfile.  */
12467
12468 static hashval_t
12469 partial_die_hash (const void *item)
12470 {
12471   const struct partial_die_info *part_die = item;
12472
12473   return part_die->offset;
12474 }
12475
12476 /* Trivial comparison function for partial_die_info structures: two DIEs
12477    are equal if they have the same offset.  */
12478
12479 static int
12480 partial_die_eq (const void *item_lhs, const void *item_rhs)
12481 {
12482   const struct partial_die_info *part_die_lhs = item_lhs;
12483   const struct partial_die_info *part_die_rhs = item_rhs;
12484
12485   return part_die_lhs->offset == part_die_rhs->offset;
12486 }
12487
12488 static struct cmd_list_element *set_dwarf2_cmdlist;
12489 static struct cmd_list_element *show_dwarf2_cmdlist;
12490
12491 static void
12492 set_dwarf2_cmd (char *args, int from_tty)
12493 {
12494   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
12495 }
12496
12497 static void
12498 show_dwarf2_cmd (char *args, int from_tty)
12499 {
12500   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
12501 }
12502
12503 /* If section described by INFO was mmapped, munmap it now.  */
12504
12505 static void
12506 munmap_section_buffer (struct dwarf2_section_info *info)
12507 {
12508   if (info->was_mmapped)
12509     {
12510 #ifdef HAVE_MMAP
12511       intptr_t begin = (intptr_t) info->buffer;
12512       intptr_t map_begin = begin & ~(pagesize - 1);
12513       size_t map_length = info->size + begin - map_begin;
12514
12515       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
12516 #else
12517       /* Without HAVE_MMAP, we should never be here to begin with.  */
12518       gdb_assert (0);
12519 #endif
12520     }
12521 }
12522
12523 /* munmap debug sections for OBJFILE, if necessary.  */
12524
12525 static void
12526 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
12527 {
12528   struct dwarf2_per_objfile *data = d;
12529
12530   munmap_section_buffer (&data->info);
12531   munmap_section_buffer (&data->abbrev);
12532   munmap_section_buffer (&data->line);
12533   munmap_section_buffer (&data->str);
12534   munmap_section_buffer (&data->macinfo);
12535   munmap_section_buffer (&data->ranges);
12536   munmap_section_buffer (&data->loc);
12537   munmap_section_buffer (&data->frame);
12538   munmap_section_buffer (&data->eh_frame);
12539 }
12540
12541 int dwarf2_always_disassemble;
12542
12543 static void
12544 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
12545                                 struct cmd_list_element *c, const char *value)
12546 {
12547   fprintf_filtered (file, _("\
12548 Whether to always disassemble DWARF expressions is %s.\n"),
12549                     value);
12550 }
12551
12552 void _initialize_dwarf2_read (void);
12553
12554 void
12555 _initialize_dwarf2_read (void)
12556 {
12557   dwarf2_objfile_data_key
12558     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
12559
12560   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
12561 Set DWARF 2 specific variables.\n\
12562 Configure DWARF 2 variables such as the cache size"),
12563                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
12564                   0/*allow-unknown*/, &maintenance_set_cmdlist);
12565
12566   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
12567 Show DWARF 2 specific variables\n\
12568 Show DWARF 2 variables such as the cache size"),
12569                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
12570                   0/*allow-unknown*/, &maintenance_show_cmdlist);
12571
12572   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
12573                             &dwarf2_max_cache_age, _("\
12574 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
12575 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
12576 A higher limit means that cached compilation units will be stored\n\
12577 in memory longer, and more total memory will be used.  Zero disables\n\
12578 caching, which can slow down startup."),
12579                             NULL,
12580                             show_dwarf2_max_cache_age,
12581                             &set_dwarf2_cmdlist,
12582                             &show_dwarf2_cmdlist);
12583
12584   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
12585                            &dwarf2_always_disassemble, _("\
12586 Set whether `info address' always disassembles DWARF expressions."), _("\
12587 Show whether `info address' always disassembles DWARF expressions."), _("\
12588 When enabled, DWARF expressions are always printed in an assembly-like\n\
12589 syntax.  When disabled, expressions will be printed in a more\n\
12590 conversational style, when possible."),
12591                            NULL,
12592                            show_dwarf2_always_disassemble,
12593                            &set_dwarf2_cmdlist,
12594                            &show_dwarf2_cmdlist);
12595
12596   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
12597 Set debugging of the dwarf2 DIE reader."), _("\
12598 Show debugging of the dwarf2 DIE reader."), _("\
12599 When enabled (non-zero), DIEs are dumped after they are read in.\n\
12600 The value is the maximum depth to print."),
12601                             NULL,
12602                             NULL,
12603                             &setdebuglist, &showdebuglist);
12604 }