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