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