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