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