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