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