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