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