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