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