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