* doc/gdbint.texinfo (Host Definition): Remove documentation
[deliverable/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 lh->file_names[file - 1].included_p = 1;
5981 if (!decode_for_pst_p)
5982 {
5983 /* append row to matrix using current values */
5984 record_line (current_subfile, line,
5985 check_cu_functions (address, cu));
5986 }
5987 basic_block = 1;
5988 }
5989 else switch (op_code)
5990 {
5991 case DW_LNS_extended_op:
5992 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5993 line_ptr += bytes_read;
5994 extended_op = read_1_byte (abfd, line_ptr);
5995 line_ptr += 1;
5996 switch (extended_op)
5997 {
5998 case DW_LNE_end_sequence:
5999 end_sequence = 1;
6000 lh->file_names[file - 1].included_p = 1;
6001 if (!decode_for_pst_p)
6002 record_line (current_subfile, 0, address);
6003 break;
6004 case DW_LNE_set_address:
6005 address = read_address (abfd, line_ptr, cu, &bytes_read);
6006 line_ptr += bytes_read;
6007 address += baseaddr;
6008 break;
6009 case DW_LNE_define_file:
6010 {
6011 char *cur_file;
6012 unsigned int dir_index, mod_time, length;
6013
6014 cur_file = read_string (abfd, line_ptr, &bytes_read);
6015 line_ptr += bytes_read;
6016 dir_index =
6017 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6018 line_ptr += bytes_read;
6019 mod_time =
6020 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6021 line_ptr += bytes_read;
6022 length =
6023 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6024 line_ptr += bytes_read;
6025 add_file_name (lh, cur_file, dir_index, mod_time, length);
6026 }
6027 break;
6028 default:
6029 complaint (&symfile_complaints,
6030 "mangled .debug_line section");
6031 return;
6032 }
6033 break;
6034 case DW_LNS_copy:
6035 lh->file_names[file - 1].included_p = 1;
6036 if (!decode_for_pst_p)
6037 record_line (current_subfile, line,
6038 check_cu_functions (address, cu));
6039 basic_block = 0;
6040 break;
6041 case DW_LNS_advance_pc:
6042 address += lh->minimum_instruction_length
6043 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6044 line_ptr += bytes_read;
6045 break;
6046 case DW_LNS_advance_line:
6047 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
6048 line_ptr += bytes_read;
6049 break;
6050 case DW_LNS_set_file:
6051 {
6052 /* lh->include_dirs and lh->file_names are 0-based,
6053 but the directory and file name numbers in the
6054 statement program are 1-based. */
6055 struct file_entry *fe;
6056 char *dir;
6057 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6058 line_ptr += bytes_read;
6059 fe = &lh->file_names[file - 1];
6060 if (fe->dir_index)
6061 dir = lh->include_dirs[fe->dir_index - 1];
6062 else
6063 dir = comp_dir;
6064 if (!decode_for_pst_p)
6065 dwarf2_start_subfile (fe->name, dir);
6066 }
6067 break;
6068 case DW_LNS_set_column:
6069 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6070 line_ptr += bytes_read;
6071 break;
6072 case DW_LNS_negate_stmt:
6073 is_stmt = (!is_stmt);
6074 break;
6075 case DW_LNS_set_basic_block:
6076 basic_block = 1;
6077 break;
6078 /* Add to the address register of the state machine the
6079 address increment value corresponding to special opcode
6080 255. Ie, this value is scaled by the minimum instruction
6081 length since special opcode 255 would have scaled the
6082 the increment. */
6083 case DW_LNS_const_add_pc:
6084 address += (lh->minimum_instruction_length
6085 * ((255 - lh->opcode_base) / lh->line_range));
6086 break;
6087 case DW_LNS_fixed_advance_pc:
6088 address += read_2_bytes (abfd, line_ptr);
6089 line_ptr += 2;
6090 break;
6091 default:
6092 { /* Unknown standard opcode, ignore it. */
6093 int i;
6094 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
6095 {
6096 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6097 line_ptr += bytes_read;
6098 }
6099 }
6100 }
6101 }
6102 }
6103
6104 if (decode_for_pst_p)
6105 {
6106 int file_index;
6107
6108 /* Now that we're done scanning the Line Header Program, we can
6109 create the psymtab of each included file. */
6110 for (file_index = 0; file_index < lh->num_file_names; file_index++)
6111 if (lh->file_names[file_index].included_p == 1)
6112 {
6113 char *include_name = lh->file_names [file_index].name;
6114
6115 if (strcmp (include_name, pst->filename) != 0)
6116 dwarf2_create_include_psymtab (include_name, pst, objfile);
6117 }
6118 }
6119 }
6120
6121 /* Start a subfile for DWARF. FILENAME is the name of the file and
6122 DIRNAME the name of the source directory which contains FILENAME
6123 or NULL if not known.
6124 This routine tries to keep line numbers from identical absolute and
6125 relative file names in a common subfile.
6126
6127 Using the `list' example from the GDB testsuite, which resides in
6128 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
6129 of /srcdir/list0.c yields the following debugging information for list0.c:
6130
6131 DW_AT_name: /srcdir/list0.c
6132 DW_AT_comp_dir: /compdir
6133 files.files[0].name: list0.h
6134 files.files[0].dir: /srcdir
6135 files.files[1].name: list0.c
6136 files.files[1].dir: /srcdir
6137
6138 The line number information for list0.c has to end up in a single
6139 subfile, so that `break /srcdir/list0.c:1' works as expected. */
6140
6141 static void
6142 dwarf2_start_subfile (char *filename, char *dirname)
6143 {
6144 /* If the filename isn't absolute, try to match an existing subfile
6145 with the full pathname. */
6146
6147 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
6148 {
6149 struct subfile *subfile;
6150 char *fullname = concat (dirname, "/", filename, NULL);
6151
6152 for (subfile = subfiles; subfile; subfile = subfile->next)
6153 {
6154 if (FILENAME_CMP (subfile->name, fullname) == 0)
6155 {
6156 current_subfile = subfile;
6157 xfree (fullname);
6158 return;
6159 }
6160 }
6161 xfree (fullname);
6162 }
6163 start_subfile (filename, dirname);
6164 }
6165
6166 static void
6167 var_decode_location (struct attribute *attr, struct symbol *sym,
6168 struct dwarf2_cu *cu)
6169 {
6170 struct objfile *objfile = cu->objfile;
6171 struct comp_unit_head *cu_header = &cu->header;
6172
6173 /* NOTE drow/2003-01-30: There used to be a comment and some special
6174 code here to turn a symbol with DW_AT_external and a
6175 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
6176 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
6177 with some versions of binutils) where shared libraries could have
6178 relocations against symbols in their debug information - the
6179 minimal symbol would have the right address, but the debug info
6180 would not. It's no longer necessary, because we will explicitly
6181 apply relocations when we read in the debug information now. */
6182
6183 /* A DW_AT_location attribute with no contents indicates that a
6184 variable has been optimized away. */
6185 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
6186 {
6187 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
6188 return;
6189 }
6190
6191 /* Handle one degenerate form of location expression specially, to
6192 preserve GDB's previous behavior when section offsets are
6193 specified. If this is just a DW_OP_addr then mark this symbol
6194 as LOC_STATIC. */
6195
6196 if (attr_form_is_block (attr)
6197 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
6198 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
6199 {
6200 int dummy;
6201
6202 SYMBOL_VALUE_ADDRESS (sym) =
6203 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
6204 fixup_symbol_section (sym, objfile);
6205 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
6206 SYMBOL_SECTION (sym));
6207 SYMBOL_CLASS (sym) = LOC_STATIC;
6208 return;
6209 }
6210
6211 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
6212 expression evaluator, and use LOC_COMPUTED only when necessary
6213 (i.e. when the value of a register or memory location is
6214 referenced, or a thread-local block, etc.). Then again, it might
6215 not be worthwhile. I'm assuming that it isn't unless performance
6216 or memory numbers show me otherwise. */
6217
6218 dwarf2_symbol_mark_computed (attr, sym, cu);
6219 SYMBOL_CLASS (sym) = LOC_COMPUTED;
6220 }
6221
6222 /* Given a pointer to a DWARF information entry, figure out if we need
6223 to make a symbol table entry for it, and if so, create a new entry
6224 and return a pointer to it.
6225 If TYPE is NULL, determine symbol type from the die, otherwise
6226 used the passed type. */
6227
6228 static struct symbol *
6229 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
6230 {
6231 struct objfile *objfile = cu->objfile;
6232 struct symbol *sym = NULL;
6233 char *name;
6234 struct attribute *attr = NULL;
6235 struct attribute *attr2 = NULL;
6236 CORE_ADDR baseaddr;
6237
6238 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6239
6240 if (die->tag != DW_TAG_namespace)
6241 name = dwarf2_linkage_name (die, cu);
6242 else
6243 name = TYPE_NAME (type);
6244
6245 if (name)
6246 {
6247 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
6248 sizeof (struct symbol));
6249 OBJSTAT (objfile, n_syms++);
6250 memset (sym, 0, sizeof (struct symbol));
6251
6252 /* Cache this symbol's name and the name's demangled form (if any). */
6253 SYMBOL_LANGUAGE (sym) = cu->language;
6254 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
6255
6256 /* Default assumptions.
6257 Use the passed type or decode it from the die. */
6258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6259 SYMBOL_CLASS (sym) = LOC_STATIC;
6260 if (type != NULL)
6261 SYMBOL_TYPE (sym) = type;
6262 else
6263 SYMBOL_TYPE (sym) = die_type (die, cu);
6264 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
6265 if (attr)
6266 {
6267 SYMBOL_LINE (sym) = DW_UNSND (attr);
6268 }
6269 switch (die->tag)
6270 {
6271 case DW_TAG_label:
6272 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6273 if (attr)
6274 {
6275 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
6276 }
6277 SYMBOL_CLASS (sym) = LOC_LABEL;
6278 break;
6279 case DW_TAG_subprogram:
6280 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
6281 finish_block. */
6282 SYMBOL_CLASS (sym) = LOC_BLOCK;
6283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
6284 if (attr2 && (DW_UNSND (attr2) != 0))
6285 {
6286 add_symbol_to_list (sym, &global_symbols);
6287 }
6288 else
6289 {
6290 add_symbol_to_list (sym, cu->list_in_scope);
6291 }
6292 break;
6293 case DW_TAG_variable:
6294 /* Compilation with minimal debug info may result in variables
6295 with missing type entries. Change the misleading `void' type
6296 to something sensible. */
6297 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
6298 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
6299 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
6300 "<variable, no debug info>",
6301 objfile);
6302 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6303 if (attr)
6304 {
6305 dwarf2_const_value (attr, sym, cu);
6306 attr2 = dwarf2_attr (die, DW_AT_external, cu);
6307 if (attr2 && (DW_UNSND (attr2) != 0))
6308 add_symbol_to_list (sym, &global_symbols);
6309 else
6310 add_symbol_to_list (sym, cu->list_in_scope);
6311 break;
6312 }
6313 attr = dwarf2_attr (die, DW_AT_location, cu);
6314 if (attr)
6315 {
6316 var_decode_location (attr, sym, cu);
6317 attr2 = dwarf2_attr (die, DW_AT_external, cu);
6318 if (attr2 && (DW_UNSND (attr2) != 0))
6319 add_symbol_to_list (sym, &global_symbols);
6320 else
6321 add_symbol_to_list (sym, cu->list_in_scope);
6322 }
6323 else
6324 {
6325 /* We do not know the address of this symbol.
6326 If it is an external symbol and we have type information
6327 for it, enter the symbol as a LOC_UNRESOLVED symbol.
6328 The address of the variable will then be determined from
6329 the minimal symbol table whenever the variable is
6330 referenced. */
6331 attr2 = dwarf2_attr (die, DW_AT_external, cu);
6332 if (attr2 && (DW_UNSND (attr2) != 0)
6333 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
6334 {
6335 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
6336 add_symbol_to_list (sym, &global_symbols);
6337 }
6338 }
6339 break;
6340 case DW_TAG_formal_parameter:
6341 attr = dwarf2_attr (die, DW_AT_location, cu);
6342 if (attr)
6343 {
6344 var_decode_location (attr, sym, cu);
6345 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
6346 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
6347 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
6348 }
6349 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6350 if (attr)
6351 {
6352 dwarf2_const_value (attr, sym, cu);
6353 }
6354 add_symbol_to_list (sym, cu->list_in_scope);
6355 break;
6356 case DW_TAG_unspecified_parameters:
6357 /* From varargs functions; gdb doesn't seem to have any
6358 interest in this information, so just ignore it for now.
6359 (FIXME?) */
6360 break;
6361 case DW_TAG_class_type:
6362 case DW_TAG_structure_type:
6363 case DW_TAG_union_type:
6364 case DW_TAG_enumeration_type:
6365 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6366 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6367
6368 /* Make sure that the symbol includes appropriate enclosing
6369 classes/namespaces in its name. These are calculated in
6370 read_structure_type, and the correct name is saved in
6371 the type. */
6372
6373 if (cu->language == language_cplus)
6374 {
6375 struct type *type = SYMBOL_TYPE (sym);
6376
6377 if (TYPE_TAG_NAME (type) != NULL)
6378 {
6379 /* FIXME: carlton/2003-11-10: Should this use
6380 SYMBOL_SET_NAMES instead? (The same problem also
6381 arises further down in this function.) */
6382 /* The type's name is already allocated along with
6383 this objfile, so we don't need to duplicate it
6384 for the symbol. */
6385 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
6386 }
6387 }
6388
6389 {
6390 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
6391 really ever be static objects: otherwise, if you try
6392 to, say, break of a class's method and you're in a file
6393 which doesn't mention that class, it won't work unless
6394 the check for all static symbols in lookup_symbol_aux
6395 saves you. See the OtherFileClass tests in
6396 gdb.c++/namespace.exp. */
6397
6398 struct pending **list_to_add;
6399
6400 list_to_add = (cu->list_in_scope == &file_symbols
6401 && cu->language == language_cplus
6402 ? &global_symbols : cu->list_in_scope);
6403
6404 add_symbol_to_list (sym, list_to_add);
6405
6406 /* The semantics of C++ state that "struct foo { ... }" also
6407 defines a typedef for "foo". Synthesize a typedef symbol so
6408 that "ptype foo" works as expected. */
6409 if (cu->language == language_cplus)
6410 {
6411 struct symbol *typedef_sym = (struct symbol *)
6412 obstack_alloc (&objfile->objfile_obstack,
6413 sizeof (struct symbol));
6414 *typedef_sym = *sym;
6415 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
6416 /* The symbol's name is already allocated along with
6417 this objfile, so we don't need to duplicate it for
6418 the type. */
6419 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
6420 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
6421 add_symbol_to_list (typedef_sym, list_to_add);
6422 }
6423 }
6424 break;
6425 case DW_TAG_typedef:
6426 if (processing_has_namespace_info
6427 && processing_current_prefix[0] != '\0')
6428 {
6429 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
6430 processing_current_prefix,
6431 "::",
6432 name);
6433 }
6434 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6435 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6436 add_symbol_to_list (sym, cu->list_in_scope);
6437 break;
6438 case DW_TAG_base_type:
6439 case DW_TAG_subrange_type:
6440 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6441 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6442 add_symbol_to_list (sym, cu->list_in_scope);
6443 break;
6444 case DW_TAG_enumerator:
6445 if (processing_has_namespace_info
6446 && processing_current_prefix[0] != '\0')
6447 {
6448 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
6449 processing_current_prefix,
6450 "::",
6451 name);
6452 }
6453 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6454 if (attr)
6455 {
6456 dwarf2_const_value (attr, sym, cu);
6457 }
6458 {
6459 /* NOTE: carlton/2003-11-10: See comment above in the
6460 DW_TAG_class_type, etc. block. */
6461
6462 struct pending **list_to_add;
6463
6464 list_to_add = (cu->list_in_scope == &file_symbols
6465 && cu->language == language_cplus
6466 ? &global_symbols : cu->list_in_scope);
6467
6468 add_symbol_to_list (sym, list_to_add);
6469 }
6470 break;
6471 case DW_TAG_namespace:
6472 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6473 add_symbol_to_list (sym, &global_symbols);
6474 break;
6475 default:
6476 /* Not a tag we recognize. Hopefully we aren't processing
6477 trash data, but since we must specifically ignore things
6478 we don't recognize, there is nothing else we should do at
6479 this point. */
6480 complaint (&symfile_complaints, "unsupported tag: '%s'",
6481 dwarf_tag_name (die->tag));
6482 break;
6483 }
6484 }
6485 return (sym);
6486 }
6487
6488 /* Copy constant value from an attribute to a symbol. */
6489
6490 static void
6491 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
6492 struct dwarf2_cu *cu)
6493 {
6494 struct objfile *objfile = cu->objfile;
6495 struct comp_unit_head *cu_header = &cu->header;
6496 struct dwarf_block *blk;
6497
6498 switch (attr->form)
6499 {
6500 case DW_FORM_addr:
6501 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
6502 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6503 cu_header->addr_size,
6504 TYPE_LENGTH (SYMBOL_TYPE
6505 (sym)));
6506 SYMBOL_VALUE_BYTES (sym) = (char *)
6507 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
6508 /* NOTE: cagney/2003-05-09: In-lined store_address call with
6509 it's body - store_unsigned_integer. */
6510 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
6511 DW_ADDR (attr));
6512 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6513 break;
6514 case DW_FORM_block1:
6515 case DW_FORM_block2:
6516 case DW_FORM_block4:
6517 case DW_FORM_block:
6518 blk = DW_BLOCK (attr);
6519 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
6520 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6521 blk->size,
6522 TYPE_LENGTH (SYMBOL_TYPE
6523 (sym)));
6524 SYMBOL_VALUE_BYTES (sym) = (char *)
6525 obstack_alloc (&objfile->objfile_obstack, blk->size);
6526 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
6527 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6528 break;
6529
6530 /* The DW_AT_const_value attributes are supposed to carry the
6531 symbol's value "represented as it would be on the target
6532 architecture." By the time we get here, it's already been
6533 converted to host endianness, so we just need to sign- or
6534 zero-extend it as appropriate. */
6535 case DW_FORM_data1:
6536 dwarf2_const_value_data (attr, sym, 8);
6537 break;
6538 case DW_FORM_data2:
6539 dwarf2_const_value_data (attr, sym, 16);
6540 break;
6541 case DW_FORM_data4:
6542 dwarf2_const_value_data (attr, sym, 32);
6543 break;
6544 case DW_FORM_data8:
6545 dwarf2_const_value_data (attr, sym, 64);
6546 break;
6547
6548 case DW_FORM_sdata:
6549 SYMBOL_VALUE (sym) = DW_SND (attr);
6550 SYMBOL_CLASS (sym) = LOC_CONST;
6551 break;
6552
6553 case DW_FORM_udata:
6554 SYMBOL_VALUE (sym) = DW_UNSND (attr);
6555 SYMBOL_CLASS (sym) = LOC_CONST;
6556 break;
6557
6558 default:
6559 complaint (&symfile_complaints,
6560 "unsupported const value attribute form: '%s'",
6561 dwarf_form_name (attr->form));
6562 SYMBOL_VALUE (sym) = 0;
6563 SYMBOL_CLASS (sym) = LOC_CONST;
6564 break;
6565 }
6566 }
6567
6568
6569 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
6570 or zero-extend it as appropriate for the symbol's type. */
6571 static void
6572 dwarf2_const_value_data (struct attribute *attr,
6573 struct symbol *sym,
6574 int bits)
6575 {
6576 LONGEST l = DW_UNSND (attr);
6577
6578 if (bits < sizeof (l) * 8)
6579 {
6580 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
6581 l &= ((LONGEST) 1 << bits) - 1;
6582 else
6583 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
6584 }
6585
6586 SYMBOL_VALUE (sym) = l;
6587 SYMBOL_CLASS (sym) = LOC_CONST;
6588 }
6589
6590
6591 /* Return the type of the die in question using its DW_AT_type attribute. */
6592
6593 static struct type *
6594 die_type (struct die_info *die, struct dwarf2_cu *cu)
6595 {
6596 struct type *type;
6597 struct attribute *type_attr;
6598 struct die_info *type_die;
6599 unsigned int ref;
6600
6601 type_attr = dwarf2_attr (die, DW_AT_type, cu);
6602 if (!type_attr)
6603 {
6604 /* A missing DW_AT_type represents a void type. */
6605 return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
6606 }
6607 else
6608 {
6609 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6610 type_die = follow_die_ref (ref);
6611 if (!type_die)
6612 {
6613 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]",
6614 ref, cu->objfile->name);
6615 return NULL;
6616 }
6617 }
6618 type = tag_type_to_type (type_die, cu);
6619 if (!type)
6620 {
6621 dump_die (type_die);
6622 error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
6623 cu->objfile->name);
6624 }
6625 return type;
6626 }
6627
6628 /* Return the containing type of the die in question using its
6629 DW_AT_containing_type attribute. */
6630
6631 static struct type *
6632 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
6633 {
6634 struct type *type = NULL;
6635 struct attribute *type_attr;
6636 struct die_info *type_die = NULL;
6637 unsigned int ref;
6638
6639 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
6640 if (type_attr)
6641 {
6642 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6643 type_die = follow_die_ref (ref);
6644 if (!type_die)
6645 {
6646 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref,
6647 cu->objfile->name);
6648 return NULL;
6649 }
6650 type = tag_type_to_type (type_die, cu);
6651 }
6652 if (!type)
6653 {
6654 if (type_die)
6655 dump_die (type_die);
6656 error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]",
6657 cu->objfile->name);
6658 }
6659 return type;
6660 }
6661
6662 #if 0
6663 static struct type *
6664 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
6665 {
6666 struct die_info *die;
6667 struct type *type;
6668
6669 die = follow_die_ref (offset);
6670 if (!die)
6671 {
6672 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
6673 return NULL;
6674 }
6675 type = tag_type_to_type (die, cu);
6676 return type;
6677 }
6678 #endif
6679
6680 static struct type *
6681 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
6682 {
6683 if (die->type)
6684 {
6685 return die->type;
6686 }
6687 else
6688 {
6689 read_type_die (die, cu);
6690 if (!die->type)
6691 {
6692 dump_die (die);
6693 error ("Dwarf Error: Cannot find type of die [in module %s]",
6694 cu->objfile->name);
6695 }
6696 return die->type;
6697 }
6698 }
6699
6700 static void
6701 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
6702 {
6703 char *prefix = determine_prefix (die, cu);
6704 const char *old_prefix = processing_current_prefix;
6705 struct cleanup *back_to = make_cleanup (xfree, prefix);
6706 processing_current_prefix = prefix;
6707
6708 switch (die->tag)
6709 {
6710 case DW_TAG_class_type:
6711 case DW_TAG_structure_type:
6712 case DW_TAG_union_type:
6713 read_structure_type (die, cu);
6714 break;
6715 case DW_TAG_enumeration_type:
6716 read_enumeration_type (die, cu);
6717 break;
6718 case DW_TAG_subprogram:
6719 case DW_TAG_subroutine_type:
6720 read_subroutine_type (die, cu);
6721 break;
6722 case DW_TAG_array_type:
6723 read_array_type (die, cu);
6724 break;
6725 case DW_TAG_pointer_type:
6726 read_tag_pointer_type (die, cu);
6727 break;
6728 case DW_TAG_ptr_to_member_type:
6729 read_tag_ptr_to_member_type (die, cu);
6730 break;
6731 case DW_TAG_reference_type:
6732 read_tag_reference_type (die, cu);
6733 break;
6734 case DW_TAG_const_type:
6735 read_tag_const_type (die, cu);
6736 break;
6737 case DW_TAG_volatile_type:
6738 read_tag_volatile_type (die, cu);
6739 break;
6740 case DW_TAG_string_type:
6741 read_tag_string_type (die, cu);
6742 break;
6743 case DW_TAG_typedef:
6744 read_typedef (die, cu);
6745 break;
6746 case DW_TAG_subrange_type:
6747 read_subrange_type (die, cu);
6748 break;
6749 case DW_TAG_base_type:
6750 read_base_type (die, cu);
6751 break;
6752 default:
6753 complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
6754 dwarf_tag_name (die->tag));
6755 break;
6756 }
6757
6758 processing_current_prefix = old_prefix;
6759 do_cleanups (back_to);
6760 }
6761
6762 /* Return the name of the namespace/class that DIE is defined within,
6763 or "" if we can't tell. The caller should xfree the result. */
6764
6765 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
6766 therein) for an example of how to use this function to deal with
6767 DW_AT_specification. */
6768
6769 static char *
6770 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
6771 {
6772 struct die_info *parent;
6773
6774 if (cu->language != language_cplus)
6775 return NULL;
6776
6777 parent = die->parent;
6778
6779 if (parent == NULL)
6780 {
6781 return xstrdup ("");
6782 }
6783 else
6784 {
6785 switch (parent->tag) {
6786 case DW_TAG_namespace:
6787 {
6788 /* FIXME: carlton/2004-03-05: Should I follow extension dies
6789 before doing this check? */
6790 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6791 {
6792 return xstrdup (TYPE_TAG_NAME (parent->type));
6793 }
6794 else
6795 {
6796 int dummy;
6797 char *parent_prefix = determine_prefix (parent, cu);
6798 char *retval = typename_concat (parent_prefix,
6799 namespace_name (parent, &dummy,
6800 cu));
6801 xfree (parent_prefix);
6802 return retval;
6803 }
6804 }
6805 break;
6806 case DW_TAG_class_type:
6807 case DW_TAG_structure_type:
6808 {
6809 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6810 {
6811 return xstrdup (TYPE_TAG_NAME (parent->type));
6812 }
6813 else
6814 {
6815 const char *old_prefix = processing_current_prefix;
6816 char *new_prefix = determine_prefix (parent, cu);
6817 char *retval;
6818
6819 processing_current_prefix = new_prefix;
6820 retval = determine_class_name (parent, cu);
6821 processing_current_prefix = old_prefix;
6822
6823 xfree (new_prefix);
6824 return retval;
6825 }
6826 }
6827 default:
6828 return determine_prefix (parent, cu);
6829 }
6830 }
6831 }
6832
6833 /* Return a newly-allocated string formed by concatenating PREFIX,
6834 "::", and SUFFIX, except that if PREFIX is NULL or the empty
6835 string, just return a copy of SUFFIX. */
6836
6837 static char *
6838 typename_concat (const char *prefix, const char *suffix)
6839 {
6840 if (prefix == NULL || prefix[0] == '\0')
6841 return xstrdup (suffix);
6842 else
6843 {
6844 char *retval = xmalloc (strlen (prefix) + 2 + strlen (suffix) + 1);
6845
6846 strcpy (retval, prefix);
6847 strcat (retval, "::");
6848 strcat (retval, suffix);
6849
6850 return retval;
6851 }
6852 }
6853
6854 static struct type *
6855 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
6856 {
6857 struct objfile *objfile = cu->objfile;
6858
6859 /* FIXME - this should not produce a new (struct type *)
6860 every time. It should cache base types. */
6861 struct type *type;
6862 switch (encoding)
6863 {
6864 case DW_ATE_address:
6865 type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
6866 return type;
6867 case DW_ATE_boolean:
6868 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
6869 return type;
6870 case DW_ATE_complex_float:
6871 if (size == 16)
6872 {
6873 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
6874 }
6875 else
6876 {
6877 type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
6878 }
6879 return type;
6880 case DW_ATE_float:
6881 if (size == 8)
6882 {
6883 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
6884 }
6885 else
6886 {
6887 type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
6888 }
6889 return type;
6890 case DW_ATE_signed:
6891 switch (size)
6892 {
6893 case 1:
6894 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6895 break;
6896 case 2:
6897 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
6898 break;
6899 default:
6900 case 4:
6901 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6902 break;
6903 }
6904 return type;
6905 case DW_ATE_signed_char:
6906 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6907 return type;
6908 case DW_ATE_unsigned:
6909 switch (size)
6910 {
6911 case 1:
6912 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6913 break;
6914 case 2:
6915 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
6916 break;
6917 default:
6918 case 4:
6919 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
6920 break;
6921 }
6922 return type;
6923 case DW_ATE_unsigned_char:
6924 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6925 return type;
6926 default:
6927 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6928 return type;
6929 }
6930 }
6931
6932 #if 0
6933 struct die_info *
6934 copy_die (struct die_info *old_die)
6935 {
6936 struct die_info *new_die;
6937 int i, num_attrs;
6938
6939 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
6940 memset (new_die, 0, sizeof (struct die_info));
6941
6942 new_die->tag = old_die->tag;
6943 new_die->has_children = old_die->has_children;
6944 new_die->abbrev = old_die->abbrev;
6945 new_die->offset = old_die->offset;
6946 new_die->type = NULL;
6947
6948 num_attrs = old_die->num_attrs;
6949 new_die->num_attrs = num_attrs;
6950 new_die->attrs = (struct attribute *)
6951 xmalloc (num_attrs * sizeof (struct attribute));
6952
6953 for (i = 0; i < old_die->num_attrs; ++i)
6954 {
6955 new_die->attrs[i].name = old_die->attrs[i].name;
6956 new_die->attrs[i].form = old_die->attrs[i].form;
6957 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
6958 }
6959
6960 new_die->next = NULL;
6961 return new_die;
6962 }
6963 #endif
6964
6965 /* Return sibling of die, NULL if no sibling. */
6966
6967 static struct die_info *
6968 sibling_die (struct die_info *die)
6969 {
6970 return die->sibling;
6971 }
6972
6973 /* Get linkage name of a die, return NULL if not found. */
6974
6975 static char *
6976 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6977 {
6978 struct attribute *attr;
6979
6980 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6981 if (attr && DW_STRING (attr))
6982 return DW_STRING (attr);
6983 attr = dwarf2_attr (die, DW_AT_name, cu);
6984 if (attr && DW_STRING (attr))
6985 return DW_STRING (attr);
6986 return NULL;
6987 }
6988
6989 /* Get name of a die, return NULL if not found. */
6990
6991 static char *
6992 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
6993 {
6994 struct attribute *attr;
6995
6996 attr = dwarf2_attr (die, DW_AT_name, cu);
6997 if (attr && DW_STRING (attr))
6998 return DW_STRING (attr);
6999 return NULL;
7000 }
7001
7002 /* Return the die that this die in an extension of, or NULL if there
7003 is none. */
7004
7005 static struct die_info *
7006 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
7007 {
7008 struct attribute *attr;
7009 struct die_info *extension_die;
7010 unsigned int ref;
7011
7012 attr = dwarf2_attr (die, DW_AT_extension, cu);
7013 if (attr == NULL)
7014 return NULL;
7015
7016 ref = dwarf2_get_ref_die_offset (attr, cu);
7017 extension_die = follow_die_ref (ref);
7018 if (!extension_die)
7019 {
7020 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
7021 }
7022
7023 return extension_die;
7024 }
7025
7026 /* Convert a DIE tag into its string name. */
7027
7028 static char *
7029 dwarf_tag_name (unsigned tag)
7030 {
7031 switch (tag)
7032 {
7033 case DW_TAG_padding:
7034 return "DW_TAG_padding";
7035 case DW_TAG_array_type:
7036 return "DW_TAG_array_type";
7037 case DW_TAG_class_type:
7038 return "DW_TAG_class_type";
7039 case DW_TAG_entry_point:
7040 return "DW_TAG_entry_point";
7041 case DW_TAG_enumeration_type:
7042 return "DW_TAG_enumeration_type";
7043 case DW_TAG_formal_parameter:
7044 return "DW_TAG_formal_parameter";
7045 case DW_TAG_imported_declaration:
7046 return "DW_TAG_imported_declaration";
7047 case DW_TAG_label:
7048 return "DW_TAG_label";
7049 case DW_TAG_lexical_block:
7050 return "DW_TAG_lexical_block";
7051 case DW_TAG_member:
7052 return "DW_TAG_member";
7053 case DW_TAG_pointer_type:
7054 return "DW_TAG_pointer_type";
7055 case DW_TAG_reference_type:
7056 return "DW_TAG_reference_type";
7057 case DW_TAG_compile_unit:
7058 return "DW_TAG_compile_unit";
7059 case DW_TAG_string_type:
7060 return "DW_TAG_string_type";
7061 case DW_TAG_structure_type:
7062 return "DW_TAG_structure_type";
7063 case DW_TAG_subroutine_type:
7064 return "DW_TAG_subroutine_type";
7065 case DW_TAG_typedef:
7066 return "DW_TAG_typedef";
7067 case DW_TAG_union_type:
7068 return "DW_TAG_union_type";
7069 case DW_TAG_unspecified_parameters:
7070 return "DW_TAG_unspecified_parameters";
7071 case DW_TAG_variant:
7072 return "DW_TAG_variant";
7073 case DW_TAG_common_block:
7074 return "DW_TAG_common_block";
7075 case DW_TAG_common_inclusion:
7076 return "DW_TAG_common_inclusion";
7077 case DW_TAG_inheritance:
7078 return "DW_TAG_inheritance";
7079 case DW_TAG_inlined_subroutine:
7080 return "DW_TAG_inlined_subroutine";
7081 case DW_TAG_module:
7082 return "DW_TAG_module";
7083 case DW_TAG_ptr_to_member_type:
7084 return "DW_TAG_ptr_to_member_type";
7085 case DW_TAG_set_type:
7086 return "DW_TAG_set_type";
7087 case DW_TAG_subrange_type:
7088 return "DW_TAG_subrange_type";
7089 case DW_TAG_with_stmt:
7090 return "DW_TAG_with_stmt";
7091 case DW_TAG_access_declaration:
7092 return "DW_TAG_access_declaration";
7093 case DW_TAG_base_type:
7094 return "DW_TAG_base_type";
7095 case DW_TAG_catch_block:
7096 return "DW_TAG_catch_block";
7097 case DW_TAG_const_type:
7098 return "DW_TAG_const_type";
7099 case DW_TAG_constant:
7100 return "DW_TAG_constant";
7101 case DW_TAG_enumerator:
7102 return "DW_TAG_enumerator";
7103 case DW_TAG_file_type:
7104 return "DW_TAG_file_type";
7105 case DW_TAG_friend:
7106 return "DW_TAG_friend";
7107 case DW_TAG_namelist:
7108 return "DW_TAG_namelist";
7109 case DW_TAG_namelist_item:
7110 return "DW_TAG_namelist_item";
7111 case DW_TAG_packed_type:
7112 return "DW_TAG_packed_type";
7113 case DW_TAG_subprogram:
7114 return "DW_TAG_subprogram";
7115 case DW_TAG_template_type_param:
7116 return "DW_TAG_template_type_param";
7117 case DW_TAG_template_value_param:
7118 return "DW_TAG_template_value_param";
7119 case DW_TAG_thrown_type:
7120 return "DW_TAG_thrown_type";
7121 case DW_TAG_try_block:
7122 return "DW_TAG_try_block";
7123 case DW_TAG_variant_part:
7124 return "DW_TAG_variant_part";
7125 case DW_TAG_variable:
7126 return "DW_TAG_variable";
7127 case DW_TAG_volatile_type:
7128 return "DW_TAG_volatile_type";
7129 case DW_TAG_dwarf_procedure:
7130 return "DW_TAG_dwarf_procedure";
7131 case DW_TAG_restrict_type:
7132 return "DW_TAG_restrict_type";
7133 case DW_TAG_interface_type:
7134 return "DW_TAG_interface_type";
7135 case DW_TAG_namespace:
7136 return "DW_TAG_namespace";
7137 case DW_TAG_imported_module:
7138 return "DW_TAG_imported_module";
7139 case DW_TAG_unspecified_type:
7140 return "DW_TAG_unspecified_type";
7141 case DW_TAG_partial_unit:
7142 return "DW_TAG_partial_unit";
7143 case DW_TAG_imported_unit:
7144 return "DW_TAG_imported_unit";
7145 case DW_TAG_MIPS_loop:
7146 return "DW_TAG_MIPS_loop";
7147 case DW_TAG_format_label:
7148 return "DW_TAG_format_label";
7149 case DW_TAG_function_template:
7150 return "DW_TAG_function_template";
7151 case DW_TAG_class_template:
7152 return "DW_TAG_class_template";
7153 default:
7154 return "DW_TAG_<unknown>";
7155 }
7156 }
7157
7158 /* Convert a DWARF attribute code into its string name. */
7159
7160 static char *
7161 dwarf_attr_name (unsigned attr)
7162 {
7163 switch (attr)
7164 {
7165 case DW_AT_sibling:
7166 return "DW_AT_sibling";
7167 case DW_AT_location:
7168 return "DW_AT_location";
7169 case DW_AT_name:
7170 return "DW_AT_name";
7171 case DW_AT_ordering:
7172 return "DW_AT_ordering";
7173 case DW_AT_subscr_data:
7174 return "DW_AT_subscr_data";
7175 case DW_AT_byte_size:
7176 return "DW_AT_byte_size";
7177 case DW_AT_bit_offset:
7178 return "DW_AT_bit_offset";
7179 case DW_AT_bit_size:
7180 return "DW_AT_bit_size";
7181 case DW_AT_element_list:
7182 return "DW_AT_element_list";
7183 case DW_AT_stmt_list:
7184 return "DW_AT_stmt_list";
7185 case DW_AT_low_pc:
7186 return "DW_AT_low_pc";
7187 case DW_AT_high_pc:
7188 return "DW_AT_high_pc";
7189 case DW_AT_language:
7190 return "DW_AT_language";
7191 case DW_AT_member:
7192 return "DW_AT_member";
7193 case DW_AT_discr:
7194 return "DW_AT_discr";
7195 case DW_AT_discr_value:
7196 return "DW_AT_discr_value";
7197 case DW_AT_visibility:
7198 return "DW_AT_visibility";
7199 case DW_AT_import:
7200 return "DW_AT_import";
7201 case DW_AT_string_length:
7202 return "DW_AT_string_length";
7203 case DW_AT_common_reference:
7204 return "DW_AT_common_reference";
7205 case DW_AT_comp_dir:
7206 return "DW_AT_comp_dir";
7207 case DW_AT_const_value:
7208 return "DW_AT_const_value";
7209 case DW_AT_containing_type:
7210 return "DW_AT_containing_type";
7211 case DW_AT_default_value:
7212 return "DW_AT_default_value";
7213 case DW_AT_inline:
7214 return "DW_AT_inline";
7215 case DW_AT_is_optional:
7216 return "DW_AT_is_optional";
7217 case DW_AT_lower_bound:
7218 return "DW_AT_lower_bound";
7219 case DW_AT_producer:
7220 return "DW_AT_producer";
7221 case DW_AT_prototyped:
7222 return "DW_AT_prototyped";
7223 case DW_AT_return_addr:
7224 return "DW_AT_return_addr";
7225 case DW_AT_start_scope:
7226 return "DW_AT_start_scope";
7227 case DW_AT_stride_size:
7228 return "DW_AT_stride_size";
7229 case DW_AT_upper_bound:
7230 return "DW_AT_upper_bound";
7231 case DW_AT_abstract_origin:
7232 return "DW_AT_abstract_origin";
7233 case DW_AT_accessibility:
7234 return "DW_AT_accessibility";
7235 case DW_AT_address_class:
7236 return "DW_AT_address_class";
7237 case DW_AT_artificial:
7238 return "DW_AT_artificial";
7239 case DW_AT_base_types:
7240 return "DW_AT_base_types";
7241 case DW_AT_calling_convention:
7242 return "DW_AT_calling_convention";
7243 case DW_AT_count:
7244 return "DW_AT_count";
7245 case DW_AT_data_member_location:
7246 return "DW_AT_data_member_location";
7247 case DW_AT_decl_column:
7248 return "DW_AT_decl_column";
7249 case DW_AT_decl_file:
7250 return "DW_AT_decl_file";
7251 case DW_AT_decl_line:
7252 return "DW_AT_decl_line";
7253 case DW_AT_declaration:
7254 return "DW_AT_declaration";
7255 case DW_AT_discr_list:
7256 return "DW_AT_discr_list";
7257 case DW_AT_encoding:
7258 return "DW_AT_encoding";
7259 case DW_AT_external:
7260 return "DW_AT_external";
7261 case DW_AT_frame_base:
7262 return "DW_AT_frame_base";
7263 case DW_AT_friend:
7264 return "DW_AT_friend";
7265 case DW_AT_identifier_case:
7266 return "DW_AT_identifier_case";
7267 case DW_AT_macro_info:
7268 return "DW_AT_macro_info";
7269 case DW_AT_namelist_items:
7270 return "DW_AT_namelist_items";
7271 case DW_AT_priority:
7272 return "DW_AT_priority";
7273 case DW_AT_segment:
7274 return "DW_AT_segment";
7275 case DW_AT_specification:
7276 return "DW_AT_specification";
7277 case DW_AT_static_link:
7278 return "DW_AT_static_link";
7279 case DW_AT_type:
7280 return "DW_AT_type";
7281 case DW_AT_use_location:
7282 return "DW_AT_use_location";
7283 case DW_AT_variable_parameter:
7284 return "DW_AT_variable_parameter";
7285 case DW_AT_virtuality:
7286 return "DW_AT_virtuality";
7287 case DW_AT_vtable_elem_location:
7288 return "DW_AT_vtable_elem_location";
7289 case DW_AT_allocated:
7290 return "DW_AT_allocated";
7291 case DW_AT_associated:
7292 return "DW_AT_associated";
7293 case DW_AT_data_location:
7294 return "DW_AT_data_location";
7295 case DW_AT_stride:
7296 return "DW_AT_stride";
7297 case DW_AT_entry_pc:
7298 return "DW_AT_entry_pc";
7299 case DW_AT_use_UTF8:
7300 return "DW_AT_use_UTF8";
7301 case DW_AT_extension:
7302 return "DW_AT_extension";
7303 case DW_AT_ranges:
7304 return "DW_AT_ranges";
7305 case DW_AT_trampoline:
7306 return "DW_AT_trampoline";
7307 case DW_AT_call_column:
7308 return "DW_AT_call_column";
7309 case DW_AT_call_file:
7310 return "DW_AT_call_file";
7311 case DW_AT_call_line:
7312 return "DW_AT_call_line";
7313 #ifdef MIPS
7314 case DW_AT_MIPS_fde:
7315 return "DW_AT_MIPS_fde";
7316 case DW_AT_MIPS_loop_begin:
7317 return "DW_AT_MIPS_loop_begin";
7318 case DW_AT_MIPS_tail_loop_begin:
7319 return "DW_AT_MIPS_tail_loop_begin";
7320 case DW_AT_MIPS_epilog_begin:
7321 return "DW_AT_MIPS_epilog_begin";
7322 case DW_AT_MIPS_loop_unroll_factor:
7323 return "DW_AT_MIPS_loop_unroll_factor";
7324 case DW_AT_MIPS_software_pipeline_depth:
7325 return "DW_AT_MIPS_software_pipeline_depth";
7326 #endif
7327 case DW_AT_MIPS_linkage_name:
7328 return "DW_AT_MIPS_linkage_name";
7329
7330 case DW_AT_sf_names:
7331 return "DW_AT_sf_names";
7332 case DW_AT_src_info:
7333 return "DW_AT_src_info";
7334 case DW_AT_mac_info:
7335 return "DW_AT_mac_info";
7336 case DW_AT_src_coords:
7337 return "DW_AT_src_coords";
7338 case DW_AT_body_begin:
7339 return "DW_AT_body_begin";
7340 case DW_AT_body_end:
7341 return "DW_AT_body_end";
7342 case DW_AT_GNU_vector:
7343 return "DW_AT_GNU_vector";
7344 default:
7345 return "DW_AT_<unknown>";
7346 }
7347 }
7348
7349 /* Convert a DWARF value form code into its string name. */
7350
7351 static char *
7352 dwarf_form_name (unsigned form)
7353 {
7354 switch (form)
7355 {
7356 case DW_FORM_addr:
7357 return "DW_FORM_addr";
7358 case DW_FORM_block2:
7359 return "DW_FORM_block2";
7360 case DW_FORM_block4:
7361 return "DW_FORM_block4";
7362 case DW_FORM_data2:
7363 return "DW_FORM_data2";
7364 case DW_FORM_data4:
7365 return "DW_FORM_data4";
7366 case DW_FORM_data8:
7367 return "DW_FORM_data8";
7368 case DW_FORM_string:
7369 return "DW_FORM_string";
7370 case DW_FORM_block:
7371 return "DW_FORM_block";
7372 case DW_FORM_block1:
7373 return "DW_FORM_block1";
7374 case DW_FORM_data1:
7375 return "DW_FORM_data1";
7376 case DW_FORM_flag:
7377 return "DW_FORM_flag";
7378 case DW_FORM_sdata:
7379 return "DW_FORM_sdata";
7380 case DW_FORM_strp:
7381 return "DW_FORM_strp";
7382 case DW_FORM_udata:
7383 return "DW_FORM_udata";
7384 case DW_FORM_ref_addr:
7385 return "DW_FORM_ref_addr";
7386 case DW_FORM_ref1:
7387 return "DW_FORM_ref1";
7388 case DW_FORM_ref2:
7389 return "DW_FORM_ref2";
7390 case DW_FORM_ref4:
7391 return "DW_FORM_ref4";
7392 case DW_FORM_ref8:
7393 return "DW_FORM_ref8";
7394 case DW_FORM_ref_udata:
7395 return "DW_FORM_ref_udata";
7396 case DW_FORM_indirect:
7397 return "DW_FORM_indirect";
7398 default:
7399 return "DW_FORM_<unknown>";
7400 }
7401 }
7402
7403 /* Convert a DWARF stack opcode into its string name. */
7404
7405 static char *
7406 dwarf_stack_op_name (unsigned op)
7407 {
7408 switch (op)
7409 {
7410 case DW_OP_addr:
7411 return "DW_OP_addr";
7412 case DW_OP_deref:
7413 return "DW_OP_deref";
7414 case DW_OP_const1u:
7415 return "DW_OP_const1u";
7416 case DW_OP_const1s:
7417 return "DW_OP_const1s";
7418 case DW_OP_const2u:
7419 return "DW_OP_const2u";
7420 case DW_OP_const2s:
7421 return "DW_OP_const2s";
7422 case DW_OP_const4u:
7423 return "DW_OP_const4u";
7424 case DW_OP_const4s:
7425 return "DW_OP_const4s";
7426 case DW_OP_const8u:
7427 return "DW_OP_const8u";
7428 case DW_OP_const8s:
7429 return "DW_OP_const8s";
7430 case DW_OP_constu:
7431 return "DW_OP_constu";
7432 case DW_OP_consts:
7433 return "DW_OP_consts";
7434 case DW_OP_dup:
7435 return "DW_OP_dup";
7436 case DW_OP_drop:
7437 return "DW_OP_drop";
7438 case DW_OP_over:
7439 return "DW_OP_over";
7440 case DW_OP_pick:
7441 return "DW_OP_pick";
7442 case DW_OP_swap:
7443 return "DW_OP_swap";
7444 case DW_OP_rot:
7445 return "DW_OP_rot";
7446 case DW_OP_xderef:
7447 return "DW_OP_xderef";
7448 case DW_OP_abs:
7449 return "DW_OP_abs";
7450 case DW_OP_and:
7451 return "DW_OP_and";
7452 case DW_OP_div:
7453 return "DW_OP_div";
7454 case DW_OP_minus:
7455 return "DW_OP_minus";
7456 case DW_OP_mod:
7457 return "DW_OP_mod";
7458 case DW_OP_mul:
7459 return "DW_OP_mul";
7460 case DW_OP_neg:
7461 return "DW_OP_neg";
7462 case DW_OP_not:
7463 return "DW_OP_not";
7464 case DW_OP_or:
7465 return "DW_OP_or";
7466 case DW_OP_plus:
7467 return "DW_OP_plus";
7468 case DW_OP_plus_uconst:
7469 return "DW_OP_plus_uconst";
7470 case DW_OP_shl:
7471 return "DW_OP_shl";
7472 case DW_OP_shr:
7473 return "DW_OP_shr";
7474 case DW_OP_shra:
7475 return "DW_OP_shra";
7476 case DW_OP_xor:
7477 return "DW_OP_xor";
7478 case DW_OP_bra:
7479 return "DW_OP_bra";
7480 case DW_OP_eq:
7481 return "DW_OP_eq";
7482 case DW_OP_ge:
7483 return "DW_OP_ge";
7484 case DW_OP_gt:
7485 return "DW_OP_gt";
7486 case DW_OP_le:
7487 return "DW_OP_le";
7488 case DW_OP_lt:
7489 return "DW_OP_lt";
7490 case DW_OP_ne:
7491 return "DW_OP_ne";
7492 case DW_OP_skip:
7493 return "DW_OP_skip";
7494 case DW_OP_lit0:
7495 return "DW_OP_lit0";
7496 case DW_OP_lit1:
7497 return "DW_OP_lit1";
7498 case DW_OP_lit2:
7499 return "DW_OP_lit2";
7500 case DW_OP_lit3:
7501 return "DW_OP_lit3";
7502 case DW_OP_lit4:
7503 return "DW_OP_lit4";
7504 case DW_OP_lit5:
7505 return "DW_OP_lit5";
7506 case DW_OP_lit6:
7507 return "DW_OP_lit6";
7508 case DW_OP_lit7:
7509 return "DW_OP_lit7";
7510 case DW_OP_lit8:
7511 return "DW_OP_lit8";
7512 case DW_OP_lit9:
7513 return "DW_OP_lit9";
7514 case DW_OP_lit10:
7515 return "DW_OP_lit10";
7516 case DW_OP_lit11:
7517 return "DW_OP_lit11";
7518 case DW_OP_lit12:
7519 return "DW_OP_lit12";
7520 case DW_OP_lit13:
7521 return "DW_OP_lit13";
7522 case DW_OP_lit14:
7523 return "DW_OP_lit14";
7524 case DW_OP_lit15:
7525 return "DW_OP_lit15";
7526 case DW_OP_lit16:
7527 return "DW_OP_lit16";
7528 case DW_OP_lit17:
7529 return "DW_OP_lit17";
7530 case DW_OP_lit18:
7531 return "DW_OP_lit18";
7532 case DW_OP_lit19:
7533 return "DW_OP_lit19";
7534 case DW_OP_lit20:
7535 return "DW_OP_lit20";
7536 case DW_OP_lit21:
7537 return "DW_OP_lit21";
7538 case DW_OP_lit22:
7539 return "DW_OP_lit22";
7540 case DW_OP_lit23:
7541 return "DW_OP_lit23";
7542 case DW_OP_lit24:
7543 return "DW_OP_lit24";
7544 case DW_OP_lit25:
7545 return "DW_OP_lit25";
7546 case DW_OP_lit26:
7547 return "DW_OP_lit26";
7548 case DW_OP_lit27:
7549 return "DW_OP_lit27";
7550 case DW_OP_lit28:
7551 return "DW_OP_lit28";
7552 case DW_OP_lit29:
7553 return "DW_OP_lit29";
7554 case DW_OP_lit30:
7555 return "DW_OP_lit30";
7556 case DW_OP_lit31:
7557 return "DW_OP_lit31";
7558 case DW_OP_reg0:
7559 return "DW_OP_reg0";
7560 case DW_OP_reg1:
7561 return "DW_OP_reg1";
7562 case DW_OP_reg2:
7563 return "DW_OP_reg2";
7564 case DW_OP_reg3:
7565 return "DW_OP_reg3";
7566 case DW_OP_reg4:
7567 return "DW_OP_reg4";
7568 case DW_OP_reg5:
7569 return "DW_OP_reg5";
7570 case DW_OP_reg6:
7571 return "DW_OP_reg6";
7572 case DW_OP_reg7:
7573 return "DW_OP_reg7";
7574 case DW_OP_reg8:
7575 return "DW_OP_reg8";
7576 case DW_OP_reg9:
7577 return "DW_OP_reg9";
7578 case DW_OP_reg10:
7579 return "DW_OP_reg10";
7580 case DW_OP_reg11:
7581 return "DW_OP_reg11";
7582 case DW_OP_reg12:
7583 return "DW_OP_reg12";
7584 case DW_OP_reg13:
7585 return "DW_OP_reg13";
7586 case DW_OP_reg14:
7587 return "DW_OP_reg14";
7588 case DW_OP_reg15:
7589 return "DW_OP_reg15";
7590 case DW_OP_reg16:
7591 return "DW_OP_reg16";
7592 case DW_OP_reg17:
7593 return "DW_OP_reg17";
7594 case DW_OP_reg18:
7595 return "DW_OP_reg18";
7596 case DW_OP_reg19:
7597 return "DW_OP_reg19";
7598 case DW_OP_reg20:
7599 return "DW_OP_reg20";
7600 case DW_OP_reg21:
7601 return "DW_OP_reg21";
7602 case DW_OP_reg22:
7603 return "DW_OP_reg22";
7604 case DW_OP_reg23:
7605 return "DW_OP_reg23";
7606 case DW_OP_reg24:
7607 return "DW_OP_reg24";
7608 case DW_OP_reg25:
7609 return "DW_OP_reg25";
7610 case DW_OP_reg26:
7611 return "DW_OP_reg26";
7612 case DW_OP_reg27:
7613 return "DW_OP_reg27";
7614 case DW_OP_reg28:
7615 return "DW_OP_reg28";
7616 case DW_OP_reg29:
7617 return "DW_OP_reg29";
7618 case DW_OP_reg30:
7619 return "DW_OP_reg30";
7620 case DW_OP_reg31:
7621 return "DW_OP_reg31";
7622 case DW_OP_breg0:
7623 return "DW_OP_breg0";
7624 case DW_OP_breg1:
7625 return "DW_OP_breg1";
7626 case DW_OP_breg2:
7627 return "DW_OP_breg2";
7628 case DW_OP_breg3:
7629 return "DW_OP_breg3";
7630 case DW_OP_breg4:
7631 return "DW_OP_breg4";
7632 case DW_OP_breg5:
7633 return "DW_OP_breg5";
7634 case DW_OP_breg6:
7635 return "DW_OP_breg6";
7636 case DW_OP_breg7:
7637 return "DW_OP_breg7";
7638 case DW_OP_breg8:
7639 return "DW_OP_breg8";
7640 case DW_OP_breg9:
7641 return "DW_OP_breg9";
7642 case DW_OP_breg10:
7643 return "DW_OP_breg10";
7644 case DW_OP_breg11:
7645 return "DW_OP_breg11";
7646 case DW_OP_breg12:
7647 return "DW_OP_breg12";
7648 case DW_OP_breg13:
7649 return "DW_OP_breg13";
7650 case DW_OP_breg14:
7651 return "DW_OP_breg14";
7652 case DW_OP_breg15:
7653 return "DW_OP_breg15";
7654 case DW_OP_breg16:
7655 return "DW_OP_breg16";
7656 case DW_OP_breg17:
7657 return "DW_OP_breg17";
7658 case DW_OP_breg18:
7659 return "DW_OP_breg18";
7660 case DW_OP_breg19:
7661 return "DW_OP_breg19";
7662 case DW_OP_breg20:
7663 return "DW_OP_breg20";
7664 case DW_OP_breg21:
7665 return "DW_OP_breg21";
7666 case DW_OP_breg22:
7667 return "DW_OP_breg22";
7668 case DW_OP_breg23:
7669 return "DW_OP_breg23";
7670 case DW_OP_breg24:
7671 return "DW_OP_breg24";
7672 case DW_OP_breg25:
7673 return "DW_OP_breg25";
7674 case DW_OP_breg26:
7675 return "DW_OP_breg26";
7676 case DW_OP_breg27:
7677 return "DW_OP_breg27";
7678 case DW_OP_breg28:
7679 return "DW_OP_breg28";
7680 case DW_OP_breg29:
7681 return "DW_OP_breg29";
7682 case DW_OP_breg30:
7683 return "DW_OP_breg30";
7684 case DW_OP_breg31:
7685 return "DW_OP_breg31";
7686 case DW_OP_regx:
7687 return "DW_OP_regx";
7688 case DW_OP_fbreg:
7689 return "DW_OP_fbreg";
7690 case DW_OP_bregx:
7691 return "DW_OP_bregx";
7692 case DW_OP_piece:
7693 return "DW_OP_piece";
7694 case DW_OP_deref_size:
7695 return "DW_OP_deref_size";
7696 case DW_OP_xderef_size:
7697 return "DW_OP_xderef_size";
7698 case DW_OP_nop:
7699 return "DW_OP_nop";
7700 /* DWARF 3 extensions. */
7701 case DW_OP_push_object_address:
7702 return "DW_OP_push_object_address";
7703 case DW_OP_call2:
7704 return "DW_OP_call2";
7705 case DW_OP_call4:
7706 return "DW_OP_call4";
7707 case DW_OP_call_ref:
7708 return "DW_OP_call_ref";
7709 /* GNU extensions. */
7710 case DW_OP_GNU_push_tls_address:
7711 return "DW_OP_GNU_push_tls_address";
7712 default:
7713 return "OP_<unknown>";
7714 }
7715 }
7716
7717 static char *
7718 dwarf_bool_name (unsigned mybool)
7719 {
7720 if (mybool)
7721 return "TRUE";
7722 else
7723 return "FALSE";
7724 }
7725
7726 /* Convert a DWARF type code into its string name. */
7727
7728 static char *
7729 dwarf_type_encoding_name (unsigned enc)
7730 {
7731 switch (enc)
7732 {
7733 case DW_ATE_address:
7734 return "DW_ATE_address";
7735 case DW_ATE_boolean:
7736 return "DW_ATE_boolean";
7737 case DW_ATE_complex_float:
7738 return "DW_ATE_complex_float";
7739 case DW_ATE_float:
7740 return "DW_ATE_float";
7741 case DW_ATE_signed:
7742 return "DW_ATE_signed";
7743 case DW_ATE_signed_char:
7744 return "DW_ATE_signed_char";
7745 case DW_ATE_unsigned:
7746 return "DW_ATE_unsigned";
7747 case DW_ATE_unsigned_char:
7748 return "DW_ATE_unsigned_char";
7749 case DW_ATE_imaginary_float:
7750 return "DW_ATE_imaginary_float";
7751 default:
7752 return "DW_ATE_<unknown>";
7753 }
7754 }
7755
7756 /* Convert a DWARF call frame info operation to its string name. */
7757
7758 #if 0
7759 static char *
7760 dwarf_cfi_name (unsigned cfi_opc)
7761 {
7762 switch (cfi_opc)
7763 {
7764 case DW_CFA_advance_loc:
7765 return "DW_CFA_advance_loc";
7766 case DW_CFA_offset:
7767 return "DW_CFA_offset";
7768 case DW_CFA_restore:
7769 return "DW_CFA_restore";
7770 case DW_CFA_nop:
7771 return "DW_CFA_nop";
7772 case DW_CFA_set_loc:
7773 return "DW_CFA_set_loc";
7774 case DW_CFA_advance_loc1:
7775 return "DW_CFA_advance_loc1";
7776 case DW_CFA_advance_loc2:
7777 return "DW_CFA_advance_loc2";
7778 case DW_CFA_advance_loc4:
7779 return "DW_CFA_advance_loc4";
7780 case DW_CFA_offset_extended:
7781 return "DW_CFA_offset_extended";
7782 case DW_CFA_restore_extended:
7783 return "DW_CFA_restore_extended";
7784 case DW_CFA_undefined:
7785 return "DW_CFA_undefined";
7786 case DW_CFA_same_value:
7787 return "DW_CFA_same_value";
7788 case DW_CFA_register:
7789 return "DW_CFA_register";
7790 case DW_CFA_remember_state:
7791 return "DW_CFA_remember_state";
7792 case DW_CFA_restore_state:
7793 return "DW_CFA_restore_state";
7794 case DW_CFA_def_cfa:
7795 return "DW_CFA_def_cfa";
7796 case DW_CFA_def_cfa_register:
7797 return "DW_CFA_def_cfa_register";
7798 case DW_CFA_def_cfa_offset:
7799 return "DW_CFA_def_cfa_offset";
7800
7801 /* DWARF 3 */
7802 case DW_CFA_def_cfa_expression:
7803 return "DW_CFA_def_cfa_expression";
7804 case DW_CFA_expression:
7805 return "DW_CFA_expression";
7806 case DW_CFA_offset_extended_sf:
7807 return "DW_CFA_offset_extended_sf";
7808 case DW_CFA_def_cfa_sf:
7809 return "DW_CFA_def_cfa_sf";
7810 case DW_CFA_def_cfa_offset_sf:
7811 return "DW_CFA_def_cfa_offset_sf";
7812
7813 /* SGI/MIPS specific */
7814 case DW_CFA_MIPS_advance_loc8:
7815 return "DW_CFA_MIPS_advance_loc8";
7816
7817 /* GNU extensions */
7818 case DW_CFA_GNU_window_save:
7819 return "DW_CFA_GNU_window_save";
7820 case DW_CFA_GNU_args_size:
7821 return "DW_CFA_GNU_args_size";
7822 case DW_CFA_GNU_negative_offset_extended:
7823 return "DW_CFA_GNU_negative_offset_extended";
7824
7825 default:
7826 return "DW_CFA_<unknown>";
7827 }
7828 }
7829 #endif
7830
7831 static void
7832 dump_die (struct die_info *die)
7833 {
7834 unsigned int i;
7835
7836 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
7837 dwarf_tag_name (die->tag), die->abbrev, die->offset);
7838 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
7839 dwarf_bool_name (die->child != NULL));
7840
7841 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
7842 for (i = 0; i < die->num_attrs; ++i)
7843 {
7844 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
7845 dwarf_attr_name (die->attrs[i].name),
7846 dwarf_form_name (die->attrs[i].form));
7847 switch (die->attrs[i].form)
7848 {
7849 case DW_FORM_ref_addr:
7850 case DW_FORM_addr:
7851 fprintf_unfiltered (gdb_stderr, "address: ");
7852 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
7853 break;
7854 case DW_FORM_block2:
7855 case DW_FORM_block4:
7856 case DW_FORM_block:
7857 case DW_FORM_block1:
7858 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
7859 break;
7860 case DW_FORM_data1:
7861 case DW_FORM_data2:
7862 case DW_FORM_data4:
7863 case DW_FORM_data8:
7864 case DW_FORM_ref1:
7865 case DW_FORM_ref2:
7866 case DW_FORM_ref4:
7867 case DW_FORM_udata:
7868 case DW_FORM_sdata:
7869 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
7870 break;
7871 case DW_FORM_string:
7872 case DW_FORM_strp:
7873 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
7874 DW_STRING (&die->attrs[i])
7875 ? DW_STRING (&die->attrs[i]) : "");
7876 break;
7877 case DW_FORM_flag:
7878 if (DW_UNSND (&die->attrs[i]))
7879 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
7880 else
7881 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
7882 break;
7883 case DW_FORM_indirect:
7884 /* the reader will have reduced the indirect form to
7885 the "base form" so this form should not occur */
7886 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
7887 break;
7888 default:
7889 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
7890 die->attrs[i].form);
7891 }
7892 fprintf_unfiltered (gdb_stderr, "\n");
7893 }
7894 }
7895
7896 static void
7897 dump_die_list (struct die_info *die)
7898 {
7899 while (die)
7900 {
7901 dump_die (die);
7902 if (die->child != NULL)
7903 dump_die_list (die->child);
7904 if (die->sibling != NULL)
7905 dump_die_list (die->sibling);
7906 }
7907 }
7908
7909 static void
7910 store_in_ref_table (unsigned int offset, struct die_info *die)
7911 {
7912 int h;
7913 struct die_info *old;
7914
7915 h = (offset % REF_HASH_SIZE);
7916 old = die_ref_table[h];
7917 die->next_ref = old;
7918 die_ref_table[h] = die;
7919 }
7920
7921
7922 static void
7923 dwarf2_empty_hash_tables (void)
7924 {
7925 memset (die_ref_table, 0, sizeof (die_ref_table));
7926 }
7927
7928 static unsigned int
7929 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
7930 {
7931 unsigned int result = 0;
7932
7933 switch (attr->form)
7934 {
7935 case DW_FORM_ref_addr:
7936 result = DW_ADDR (attr);
7937 break;
7938 case DW_FORM_ref1:
7939 case DW_FORM_ref2:
7940 case DW_FORM_ref4:
7941 case DW_FORM_ref8:
7942 case DW_FORM_ref_udata:
7943 result = cu->header.offset + DW_UNSND (attr);
7944 break;
7945 default:
7946 complaint (&symfile_complaints,
7947 "unsupported die ref attribute form: '%s'",
7948 dwarf_form_name (attr->form));
7949 }
7950 return result;
7951 }
7952
7953 /* Return the constant value held by the given attribute. Return -1
7954 if the value held by the attribute is not constant. */
7955
7956 static int
7957 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
7958 {
7959 if (attr->form == DW_FORM_sdata)
7960 return DW_SND (attr);
7961 else if (attr->form == DW_FORM_udata
7962 || attr->form == DW_FORM_data1
7963 || attr->form == DW_FORM_data2
7964 || attr->form == DW_FORM_data4
7965 || attr->form == DW_FORM_data8)
7966 return DW_UNSND (attr);
7967 else
7968 {
7969 complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
7970 dwarf_form_name (attr->form));
7971 return default_value;
7972 }
7973 }
7974
7975 static struct die_info *
7976 follow_die_ref (unsigned int offset)
7977 {
7978 struct die_info *die;
7979 int h;
7980
7981 h = (offset % REF_HASH_SIZE);
7982 die = die_ref_table[h];
7983 while (die)
7984 {
7985 if (die->offset == offset)
7986 {
7987 return die;
7988 }
7989 die = die->next_ref;
7990 }
7991 return NULL;
7992 }
7993
7994 static struct type *
7995 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
7996 struct dwarf2_cu *cu)
7997 {
7998 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
7999 {
8000 error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
8001 typeid, objfile->name);
8002 }
8003
8004 /* Look for this particular type in the fundamental type vector. If
8005 one is not found, create and install one appropriate for the
8006 current language and the current target machine. */
8007
8008 if (cu->ftypes[typeid] == NULL)
8009 {
8010 cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
8011 }
8012
8013 return (cu->ftypes[typeid]);
8014 }
8015
8016 /* Decode simple location descriptions.
8017 Given a pointer to a dwarf block that defines a location, compute
8018 the location and return the value.
8019
8020 NOTE drow/2003-11-18: This function is called in two situations
8021 now: for the address of static or global variables (partial symbols
8022 only) and for offsets into structures which are expected to be
8023 (more or less) constant. The partial symbol case should go away,
8024 and only the constant case should remain. That will let this
8025 function complain more accurately. A few special modes are allowed
8026 without complaint for global variables (for instance, global
8027 register values and thread-local values).
8028
8029 A location description containing no operations indicates that the
8030 object is optimized out. The return value is 0 for that case.
8031 FIXME drow/2003-11-16: No callers check for this case any more; soon all
8032 callers will only want a very basic result and this can become a
8033 complaint.
8034
8035 When the result is a register number, the global isreg flag is set,
8036 otherwise it is cleared.
8037
8038 Note that stack[0] is unused except as a default error return.
8039 Note that stack overflow is not yet handled. */
8040
8041 static CORE_ADDR
8042 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
8043 {
8044 struct objfile *objfile = cu->objfile;
8045 struct comp_unit_head *cu_header = &cu->header;
8046 int i;
8047 int size = blk->size;
8048 char *data = blk->data;
8049 CORE_ADDR stack[64];
8050 int stacki;
8051 unsigned int bytes_read, unsnd;
8052 unsigned char op;
8053
8054 i = 0;
8055 stacki = 0;
8056 stack[stacki] = 0;
8057 isreg = 0;
8058
8059 while (i < size)
8060 {
8061 op = data[i++];
8062 switch (op)
8063 {
8064 case DW_OP_lit0:
8065 case DW_OP_lit1:
8066 case DW_OP_lit2:
8067 case DW_OP_lit3:
8068 case DW_OP_lit4:
8069 case DW_OP_lit5:
8070 case DW_OP_lit6:
8071 case DW_OP_lit7:
8072 case DW_OP_lit8:
8073 case DW_OP_lit9:
8074 case DW_OP_lit10:
8075 case DW_OP_lit11:
8076 case DW_OP_lit12:
8077 case DW_OP_lit13:
8078 case DW_OP_lit14:
8079 case DW_OP_lit15:
8080 case DW_OP_lit16:
8081 case DW_OP_lit17:
8082 case DW_OP_lit18:
8083 case DW_OP_lit19:
8084 case DW_OP_lit20:
8085 case DW_OP_lit21:
8086 case DW_OP_lit22:
8087 case DW_OP_lit23:
8088 case DW_OP_lit24:
8089 case DW_OP_lit25:
8090 case DW_OP_lit26:
8091 case DW_OP_lit27:
8092 case DW_OP_lit28:
8093 case DW_OP_lit29:
8094 case DW_OP_lit30:
8095 case DW_OP_lit31:
8096 stack[++stacki] = op - DW_OP_lit0;
8097 break;
8098
8099 case DW_OP_reg0:
8100 case DW_OP_reg1:
8101 case DW_OP_reg2:
8102 case DW_OP_reg3:
8103 case DW_OP_reg4:
8104 case DW_OP_reg5:
8105 case DW_OP_reg6:
8106 case DW_OP_reg7:
8107 case DW_OP_reg8:
8108 case DW_OP_reg9:
8109 case DW_OP_reg10:
8110 case DW_OP_reg11:
8111 case DW_OP_reg12:
8112 case DW_OP_reg13:
8113 case DW_OP_reg14:
8114 case DW_OP_reg15:
8115 case DW_OP_reg16:
8116 case DW_OP_reg17:
8117 case DW_OP_reg18:
8118 case DW_OP_reg19:
8119 case DW_OP_reg20:
8120 case DW_OP_reg21:
8121 case DW_OP_reg22:
8122 case DW_OP_reg23:
8123 case DW_OP_reg24:
8124 case DW_OP_reg25:
8125 case DW_OP_reg26:
8126 case DW_OP_reg27:
8127 case DW_OP_reg28:
8128 case DW_OP_reg29:
8129 case DW_OP_reg30:
8130 case DW_OP_reg31:
8131 isreg = 1;
8132 stack[++stacki] = op - DW_OP_reg0;
8133 if (i < size)
8134 dwarf2_complex_location_expr_complaint ();
8135 break;
8136
8137 case DW_OP_regx:
8138 isreg = 1;
8139 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8140 i += bytes_read;
8141 stack[++stacki] = unsnd;
8142 if (i < size)
8143 dwarf2_complex_location_expr_complaint ();
8144 break;
8145
8146 case DW_OP_addr:
8147 stack[++stacki] = read_address (objfile->obfd, &data[i],
8148 cu, &bytes_read);
8149 i += bytes_read;
8150 break;
8151
8152 case DW_OP_const1u:
8153 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
8154 i += 1;
8155 break;
8156
8157 case DW_OP_const1s:
8158 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
8159 i += 1;
8160 break;
8161
8162 case DW_OP_const2u:
8163 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
8164 i += 2;
8165 break;
8166
8167 case DW_OP_const2s:
8168 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
8169 i += 2;
8170 break;
8171
8172 case DW_OP_const4u:
8173 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
8174 i += 4;
8175 break;
8176
8177 case DW_OP_const4s:
8178 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
8179 i += 4;
8180 break;
8181
8182 case DW_OP_constu:
8183 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
8184 &bytes_read);
8185 i += bytes_read;
8186 break;
8187
8188 case DW_OP_consts:
8189 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
8190 i += bytes_read;
8191 break;
8192
8193 case DW_OP_dup:
8194 stack[stacki + 1] = stack[stacki];
8195 stacki++;
8196 break;
8197
8198 case DW_OP_plus:
8199 stack[stacki - 1] += stack[stacki];
8200 stacki--;
8201 break;
8202
8203 case DW_OP_plus_uconst:
8204 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8205 i += bytes_read;
8206 break;
8207
8208 case DW_OP_minus:
8209 stack[stacki - 1] -= stack[stacki];
8210 stacki--;
8211 break;
8212
8213 case DW_OP_deref:
8214 /* If we're not the last op, then we definitely can't encode
8215 this using GDB's address_class enum. This is valid for partial
8216 global symbols, although the variable's address will be bogus
8217 in the psymtab. */
8218 if (i < size)
8219 dwarf2_complex_location_expr_complaint ();
8220 break;
8221
8222 case DW_OP_GNU_push_tls_address:
8223 /* The top of the stack has the offset from the beginning
8224 of the thread control block at which the variable is located. */
8225 /* Nothing should follow this operator, so the top of stack would
8226 be returned. */
8227 /* This is valid for partial global symbols, but the variable's
8228 address will be bogus in the psymtab. */
8229 if (i < size)
8230 dwarf2_complex_location_expr_complaint ();
8231 break;
8232
8233 default:
8234 complaint (&symfile_complaints, "unsupported stack op: '%s'",
8235 dwarf_stack_op_name (op));
8236 return (stack[stacki]);
8237 }
8238 }
8239 return (stack[stacki]);
8240 }
8241
8242 /* memory allocation interface */
8243
8244 static struct dwarf_block *
8245 dwarf_alloc_block (struct dwarf2_cu *cu)
8246 {
8247 struct dwarf_block *blk;
8248
8249 blk = (struct dwarf_block *)
8250 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
8251 return (blk);
8252 }
8253
8254 static struct abbrev_info *
8255 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
8256 {
8257 struct abbrev_info *abbrev;
8258
8259 abbrev = (struct abbrev_info *)
8260 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
8261 memset (abbrev, 0, sizeof (struct abbrev_info));
8262 return (abbrev);
8263 }
8264
8265 static struct die_info *
8266 dwarf_alloc_die (void)
8267 {
8268 struct die_info *die;
8269
8270 die = (struct die_info *) xmalloc (sizeof (struct die_info));
8271 memset (die, 0, sizeof (struct die_info));
8272 return (die);
8273 }
8274
8275 \f
8276 /* Macro support. */
8277
8278
8279 /* Return the full name of file number I in *LH's file name table.
8280 Use COMP_DIR as the name of the current directory of the
8281 compilation. The result is allocated using xmalloc; the caller is
8282 responsible for freeing it. */
8283 static char *
8284 file_full_name (int file, struct line_header *lh, const char *comp_dir)
8285 {
8286 struct file_entry *fe = &lh->file_names[file - 1];
8287
8288 if (IS_ABSOLUTE_PATH (fe->name))
8289 return xstrdup (fe->name);
8290 else
8291 {
8292 const char *dir;
8293 int dir_len;
8294 char *full_name;
8295
8296 if (fe->dir_index)
8297 dir = lh->include_dirs[fe->dir_index - 1];
8298 else
8299 dir = comp_dir;
8300
8301 if (dir)
8302 {
8303 dir_len = strlen (dir);
8304 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
8305 strcpy (full_name, dir);
8306 full_name[dir_len] = '/';
8307 strcpy (full_name + dir_len + 1, fe->name);
8308 return full_name;
8309 }
8310 else
8311 return xstrdup (fe->name);
8312 }
8313 }
8314
8315
8316 static struct macro_source_file *
8317 macro_start_file (int file, int line,
8318 struct macro_source_file *current_file,
8319 const char *comp_dir,
8320 struct line_header *lh, struct objfile *objfile)
8321 {
8322 /* The full name of this source file. */
8323 char *full_name = file_full_name (file, lh, comp_dir);
8324
8325 /* We don't create a macro table for this compilation unit
8326 at all until we actually get a filename. */
8327 if (! pending_macros)
8328 pending_macros = new_macro_table (&objfile->objfile_obstack,
8329 objfile->macro_cache);
8330
8331 if (! current_file)
8332 /* If we have no current file, then this must be the start_file
8333 directive for the compilation unit's main source file. */
8334 current_file = macro_set_main (pending_macros, full_name);
8335 else
8336 current_file = macro_include (current_file, line, full_name);
8337
8338 xfree (full_name);
8339
8340 return current_file;
8341 }
8342
8343
8344 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
8345 followed by a null byte. */
8346 static char *
8347 copy_string (const char *buf, int len)
8348 {
8349 char *s = xmalloc (len + 1);
8350 memcpy (s, buf, len);
8351 s[len] = '\0';
8352
8353 return s;
8354 }
8355
8356
8357 static const char *
8358 consume_improper_spaces (const char *p, const char *body)
8359 {
8360 if (*p == ' ')
8361 {
8362 complaint (&symfile_complaints,
8363 "macro definition contains spaces in formal argument list:\n`%s'",
8364 body);
8365
8366 while (*p == ' ')
8367 p++;
8368 }
8369
8370 return p;
8371 }
8372
8373
8374 static void
8375 parse_macro_definition (struct macro_source_file *file, int line,
8376 const char *body)
8377 {
8378 const char *p;
8379
8380 /* The body string takes one of two forms. For object-like macro
8381 definitions, it should be:
8382
8383 <macro name> " " <definition>
8384
8385 For function-like macro definitions, it should be:
8386
8387 <macro name> "() " <definition>
8388 or
8389 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
8390
8391 Spaces may appear only where explicitly indicated, and in the
8392 <definition>.
8393
8394 The Dwarf 2 spec says that an object-like macro's name is always
8395 followed by a space, but versions of GCC around March 2002 omit
8396 the space when the macro's definition is the empty string.
8397
8398 The Dwarf 2 spec says that there should be no spaces between the
8399 formal arguments in a function-like macro's formal argument list,
8400 but versions of GCC around March 2002 include spaces after the
8401 commas. */
8402
8403
8404 /* Find the extent of the macro name. The macro name is terminated
8405 by either a space or null character (for an object-like macro) or
8406 an opening paren (for a function-like macro). */
8407 for (p = body; *p; p++)
8408 if (*p == ' ' || *p == '(')
8409 break;
8410
8411 if (*p == ' ' || *p == '\0')
8412 {
8413 /* It's an object-like macro. */
8414 int name_len = p - body;
8415 char *name = copy_string (body, name_len);
8416 const char *replacement;
8417
8418 if (*p == ' ')
8419 replacement = body + name_len + 1;
8420 else
8421 {
8422 dwarf2_macro_malformed_definition_complaint (body);
8423 replacement = body + name_len;
8424 }
8425
8426 macro_define_object (file, line, name, replacement);
8427
8428 xfree (name);
8429 }
8430 else if (*p == '(')
8431 {
8432 /* It's a function-like macro. */
8433 char *name = copy_string (body, p - body);
8434 int argc = 0;
8435 int argv_size = 1;
8436 char **argv = xmalloc (argv_size * sizeof (*argv));
8437
8438 p++;
8439
8440 p = consume_improper_spaces (p, body);
8441
8442 /* Parse the formal argument list. */
8443 while (*p && *p != ')')
8444 {
8445 /* Find the extent of the current argument name. */
8446 const char *arg_start = p;
8447
8448 while (*p && *p != ',' && *p != ')' && *p != ' ')
8449 p++;
8450
8451 if (! *p || p == arg_start)
8452 dwarf2_macro_malformed_definition_complaint (body);
8453 else
8454 {
8455 /* Make sure argv has room for the new argument. */
8456 if (argc >= argv_size)
8457 {
8458 argv_size *= 2;
8459 argv = xrealloc (argv, argv_size * sizeof (*argv));
8460 }
8461
8462 argv[argc++] = copy_string (arg_start, p - arg_start);
8463 }
8464
8465 p = consume_improper_spaces (p, body);
8466
8467 /* Consume the comma, if present. */
8468 if (*p == ',')
8469 {
8470 p++;
8471
8472 p = consume_improper_spaces (p, body);
8473 }
8474 }
8475
8476 if (*p == ')')
8477 {
8478 p++;
8479
8480 if (*p == ' ')
8481 /* Perfectly formed definition, no complaints. */
8482 macro_define_function (file, line, name,
8483 argc, (const char **) argv,
8484 p + 1);
8485 else if (*p == '\0')
8486 {
8487 /* Complain, but do define it. */
8488 dwarf2_macro_malformed_definition_complaint (body);
8489 macro_define_function (file, line, name,
8490 argc, (const char **) argv,
8491 p);
8492 }
8493 else
8494 /* Just complain. */
8495 dwarf2_macro_malformed_definition_complaint (body);
8496 }
8497 else
8498 /* Just complain. */
8499 dwarf2_macro_malformed_definition_complaint (body);
8500
8501 xfree (name);
8502 {
8503 int i;
8504
8505 for (i = 0; i < argc; i++)
8506 xfree (argv[i]);
8507 }
8508 xfree (argv);
8509 }
8510 else
8511 dwarf2_macro_malformed_definition_complaint (body);
8512 }
8513
8514
8515 static void
8516 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
8517 char *comp_dir, bfd *abfd,
8518 struct dwarf2_cu *cu)
8519 {
8520 char *mac_ptr, *mac_end;
8521 struct macro_source_file *current_file = 0;
8522
8523 if (dwarf2_per_objfile->macinfo_buffer == NULL)
8524 {
8525 complaint (&symfile_complaints, "missing .debug_macinfo section");
8526 return;
8527 }
8528
8529 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
8530 mac_end = dwarf2_per_objfile->macinfo_buffer
8531 + dwarf2_per_objfile->macinfo_size;
8532
8533 for (;;)
8534 {
8535 enum dwarf_macinfo_record_type macinfo_type;
8536
8537 /* Do we at least have room for a macinfo type byte? */
8538 if (mac_ptr >= mac_end)
8539 {
8540 dwarf2_macros_too_long_complaint ();
8541 return;
8542 }
8543
8544 macinfo_type = read_1_byte (abfd, mac_ptr);
8545 mac_ptr++;
8546
8547 switch (macinfo_type)
8548 {
8549 /* A zero macinfo type indicates the end of the macro
8550 information. */
8551 case 0:
8552 return;
8553
8554 case DW_MACINFO_define:
8555 case DW_MACINFO_undef:
8556 {
8557 int bytes_read;
8558 int line;
8559 char *body;
8560
8561 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8562 mac_ptr += bytes_read;
8563 body = read_string (abfd, mac_ptr, &bytes_read);
8564 mac_ptr += bytes_read;
8565
8566 if (! current_file)
8567 complaint (&symfile_complaints,
8568 "debug info gives macro %s outside of any file: %s",
8569 macinfo_type ==
8570 DW_MACINFO_define ? "definition" : macinfo_type ==
8571 DW_MACINFO_undef ? "undefinition" :
8572 "something-or-other", body);
8573 else
8574 {
8575 if (macinfo_type == DW_MACINFO_define)
8576 parse_macro_definition (current_file, line, body);
8577 else if (macinfo_type == DW_MACINFO_undef)
8578 macro_undef (current_file, line, body);
8579 }
8580 }
8581 break;
8582
8583 case DW_MACINFO_start_file:
8584 {
8585 int bytes_read;
8586 int line, file;
8587
8588 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8589 mac_ptr += bytes_read;
8590 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8591 mac_ptr += bytes_read;
8592
8593 current_file = macro_start_file (file, line,
8594 current_file, comp_dir,
8595 lh, cu->objfile);
8596 }
8597 break;
8598
8599 case DW_MACINFO_end_file:
8600 if (! current_file)
8601 complaint (&symfile_complaints,
8602 "macro debug info has an unmatched `close_file' directive");
8603 else
8604 {
8605 current_file = current_file->included_by;
8606 if (! current_file)
8607 {
8608 enum dwarf_macinfo_record_type next_type;
8609
8610 /* GCC circa March 2002 doesn't produce the zero
8611 type byte marking the end of the compilation
8612 unit. Complain if it's not there, but exit no
8613 matter what. */
8614
8615 /* Do we at least have room for a macinfo type byte? */
8616 if (mac_ptr >= mac_end)
8617 {
8618 dwarf2_macros_too_long_complaint ();
8619 return;
8620 }
8621
8622 /* We don't increment mac_ptr here, so this is just
8623 a look-ahead. */
8624 next_type = read_1_byte (abfd, mac_ptr);
8625 if (next_type != 0)
8626 complaint (&symfile_complaints,
8627 "no terminating 0-type entry for macros in `.debug_macinfo' section");
8628
8629 return;
8630 }
8631 }
8632 break;
8633
8634 case DW_MACINFO_vendor_ext:
8635 {
8636 int bytes_read;
8637 int constant;
8638 char *string;
8639
8640 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8641 mac_ptr += bytes_read;
8642 string = read_string (abfd, mac_ptr, &bytes_read);
8643 mac_ptr += bytes_read;
8644
8645 /* We don't recognize any vendor extensions. */
8646 }
8647 break;
8648 }
8649 }
8650 }
8651
8652 /* Check if the attribute's form is a DW_FORM_block*
8653 if so return true else false. */
8654 static int
8655 attr_form_is_block (struct attribute *attr)
8656 {
8657 return (attr == NULL ? 0 :
8658 attr->form == DW_FORM_block1
8659 || attr->form == DW_FORM_block2
8660 || attr->form == DW_FORM_block4
8661 || attr->form == DW_FORM_block);
8662 }
8663
8664 static void
8665 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
8666 struct dwarf2_cu *cu)
8667 {
8668 if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
8669 {
8670 struct dwarf2_loclist_baton *baton;
8671
8672 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8673 sizeof (struct dwarf2_loclist_baton));
8674 baton->objfile = cu->objfile;
8675
8676 /* We don't know how long the location list is, but make sure we
8677 don't run off the edge of the section. */
8678 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
8679 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
8680 baton->base_address = cu->header.base_address;
8681 if (cu->header.base_known == 0)
8682 complaint (&symfile_complaints,
8683 "Location list used without specifying the CU base address.");
8684
8685 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
8686 SYMBOL_LOCATION_BATON (sym) = baton;
8687 }
8688 else
8689 {
8690 struct dwarf2_locexpr_baton *baton;
8691
8692 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8693 sizeof (struct dwarf2_locexpr_baton));
8694 baton->objfile = cu->objfile;
8695
8696 if (attr_form_is_block (attr))
8697 {
8698 /* Note that we're just copying the block's data pointer
8699 here, not the actual data. We're still pointing into the
8700 info_buffer for SYM's objfile; right now we never release
8701 that buffer, but when we do clean up properly this may
8702 need to change. */
8703 baton->size = DW_BLOCK (attr)->size;
8704 baton->data = DW_BLOCK (attr)->data;
8705 }
8706 else
8707 {
8708 dwarf2_invalid_attrib_class_complaint ("location description",
8709 SYMBOL_NATURAL_NAME (sym));
8710 baton->size = 0;
8711 baton->data = NULL;
8712 }
8713
8714 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
8715 SYMBOL_LOCATION_BATON (sym) = baton;
8716 }
8717 }
8718
8719 /* This cleanup function is passed the address of a dwarf2_cu on the stack
8720 when we're finished with it. We can't free the pointer itself, but
8721 release any associated storage.
8722
8723 Only used during partial symbol parsing. */
8724
8725 static void
8726 free_stack_comp_unit (void *data)
8727 {
8728 struct dwarf2_cu *cu = data;
8729
8730 obstack_free (&cu->comp_unit_obstack, NULL);
8731 cu->partial_dies = NULL;
8732 }
8733
8734 /* Allocation function for the libiberty hash table which uses an
8735 obstack. */
8736
8737 static void *
8738 hashtab_obstack_allocate (void *data, size_t size, size_t count)
8739 {
8740 unsigned int total = size * count;
8741 void *ptr = obstack_alloc ((struct obstack *) data, total);
8742 memset (ptr, 0, total);
8743 return ptr;
8744 }
8745
8746 /* Trivial deallocation function for the libiberty splay tree and hash
8747 table - don't deallocate anything. Rely on later deletion of the
8748 obstack. */
8749
8750 static void
8751 dummy_obstack_deallocate (void *object, void *data)
8752 {
8753 return;
8754 }
8755
8756 /* Trivial hash function for partial_die_info: the hash value of a DIE
8757 is its offset in .debug_info for this objfile. */
8758
8759 static hashval_t
8760 partial_die_hash (const void *item)
8761 {
8762 const struct partial_die_info *part_die = item;
8763 return part_die->offset;
8764 }
8765
8766 /* Trivial comparison function for partial_die_info structures: two DIEs
8767 are equal if they have the same offset. */
8768
8769 static int
8770 partial_die_eq (const void *item_lhs, const void *item_rhs)
8771 {
8772 const struct partial_die_info *part_die_lhs = item_lhs;
8773 const struct partial_die_info *part_die_rhs = item_rhs;
8774 return part_die_lhs->offset == part_die_rhs->offset;
8775 }
8776
8777 void _initialize_dwarf2_read (void);
8778
8779 void
8780 _initialize_dwarf2_read (void)
8781 {
8782 dwarf2_objfile_data_key = register_objfile_data ();
8783 }
This page took 0.212169 seconds and 4 git commands to generate.