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