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