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