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