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