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