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