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