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