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