67c4ef88f181588a2b19760076d9322d83a8f388
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support.
12
13 This file is part of GDB.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27
28 #include "defs.h"
29 #include "bfd.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "objfiles.h"
33 #include "elf/dwarf2.h"
34 #include "buildsym.h"
35 #include "demangle.h"
36 #include "expression.h"
37 #include "filenames.h" /* for DOSish file names */
38 #include "macrotab.h"
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include "dwarf2expr.h"
43 #include "dwarf2loc.h"
44 #include "cp-support.h"
45 #include "hashtab.h"
46 #include "command.h"
47 #include "gdbcmd.h"
48 #include "addrmap.h"
49
50 #include <fcntl.h>
51 #include "gdb_string.h"
52 #include "gdb_assert.h"
53 #include <sys/types.h>
54 #ifdef HAVE_ZLIB_H
55 #include <zlib.h>
56 #endif
57
58 /* A note on memory usage for this file.
59
60 At the present time, this code reads the debug info sections into
61 the objfile's objfile_obstack. A definite improvement for startup
62 time, on platforms which do not emit relocations for debug
63 sections, would be to use mmap instead. The object's complete
64 debug information is loaded into memory, partly to simplify
65 absolute DIE references.
66
67 Whether using obstacks or mmap, the sections should remain loaded
68 until the objfile is released, and pointers into the section data
69 can be used for any other data associated to the objfile (symbol
70 names, type names, location expressions to name a few). */
71
72 #if 0
73 /* .debug_info header for a compilation unit
74 Because of alignment constraints, this structure has padding and cannot
75 be mapped directly onto the beginning of the .debug_info section. */
76 typedef struct comp_unit_header
77 {
78 unsigned int length; /* length of the .debug_info
79 contribution */
80 unsigned short version; /* version number -- 2 for DWARF
81 version 2 */
82 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
83 unsigned char addr_size; /* byte size of an address -- 4 */
84 }
85 _COMP_UNIT_HEADER;
86 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
87 #endif
88
89 /* .debug_pubnames header
90 Because of alignment constraints, this structure has padding and cannot
91 be mapped directly onto the beginning of the .debug_info section. */
92 typedef struct pubnames_header
93 {
94 unsigned int length; /* length of the .debug_pubnames
95 contribution */
96 unsigned char version; /* version number -- 2 for DWARF
97 version 2 */
98 unsigned int info_offset; /* offset into .debug_info section */
99 unsigned int info_size; /* byte size of .debug_info section
100 portion */
101 }
102 _PUBNAMES_HEADER;
103 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
104
105 /* .debug_pubnames header
106 Because of alignment constraints, this structure has padding and cannot
107 be mapped directly onto the beginning of the .debug_info section. */
108 typedef struct aranges_header
109 {
110 unsigned int length; /* byte len of the .debug_aranges
111 contribution */
112 unsigned short version; /* version number -- 2 for DWARF
113 version 2 */
114 unsigned int info_offset; /* offset into .debug_info section */
115 unsigned char addr_size; /* byte size of an address */
116 unsigned char seg_size; /* byte size of segment descriptor */
117 }
118 _ARANGES_HEADER;
119 #define _ACTUAL_ARANGES_HEADER_SIZE 12
120
121 /* .debug_line statement program prologue
122 Because of alignment constraints, this structure has padding and cannot
123 be mapped directly onto the beginning of the .debug_info section. */
124 typedef struct statement_prologue
125 {
126 unsigned int total_length; /* byte length of the statement
127 information */
128 unsigned short version; /* version number -- 2 for DWARF
129 version 2 */
130 unsigned int prologue_length; /* # bytes between prologue &
131 stmt program */
132 unsigned char minimum_instruction_length; /* byte size of
133 smallest instr */
134 unsigned char default_is_stmt; /* initial value of is_stmt
135 register */
136 char line_base;
137 unsigned char line_range;
138 unsigned char opcode_base; /* number assigned to first special
139 opcode */
140 unsigned char *standard_opcode_lengths;
141 }
142 _STATEMENT_PROLOGUE;
143
144 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 if (ranges_pst != NULL)
3230 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3231
3232 while (1)
3233 {
3234 CORE_ADDR range_beginning, range_end;
3235
3236 range_beginning = read_address (obfd, buffer, cu, &dummy);
3237 buffer += addr_size;
3238 range_end = read_address (obfd, buffer, cu, &dummy);
3239 buffer += addr_size;
3240 offset += 2 * addr_size;
3241
3242 /* An end of list marker is a pair of zero addresses. */
3243 if (range_beginning == 0 && range_end == 0)
3244 /* Found the end of list entry. */
3245 break;
3246
3247 /* Each base address selection entry is a pair of 2 values.
3248 The first is the largest possible address, the second is
3249 the base address. Check for a base address here. */
3250 if ((range_beginning & mask) == mask)
3251 {
3252 /* If we found the largest possible address, then
3253 read the base address. */
3254 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3255 found_base = 1;
3256 continue;
3257 }
3258
3259 if (!found_base)
3260 {
3261 /* We have no valid base address for the ranges
3262 data. */
3263 complaint (&symfile_complaints,
3264 _("Invalid .debug_ranges data (no base address)"));
3265 return 0;
3266 }
3267
3268 range_beginning += base;
3269 range_end += base;
3270
3271 if (ranges_pst != NULL && range_beginning < range_end)
3272 addrmap_set_empty (objfile->psymtabs_addrmap,
3273 range_beginning + baseaddr, range_end - 1 + baseaddr,
3274 ranges_pst);
3275
3276 /* FIXME: This is recording everything as a low-high
3277 segment of consecutive addresses. We should have a
3278 data structure for discontiguous block ranges
3279 instead. */
3280 if (! low_set)
3281 {
3282 low = range_beginning;
3283 high = range_end;
3284 low_set = 1;
3285 }
3286 else
3287 {
3288 if (range_beginning < low)
3289 low = range_beginning;
3290 if (range_end > high)
3291 high = range_end;
3292 }
3293 }
3294
3295 if (! low_set)
3296 /* If the first entry is an end-of-list marker, the range
3297 describes an empty scope, i.e. no instructions. */
3298 return 0;
3299
3300 if (low_return)
3301 *low_return = low;
3302 if (high_return)
3303 *high_return = high;
3304 return 1;
3305 }
3306
3307 /* Get low and high pc attributes from a die. Return 1 if the attributes
3308 are present and valid, otherwise, return 0. Return -1 if the range is
3309 discontinuous, i.e. derived from DW_AT_ranges information. */
3310 static int
3311 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
3312 CORE_ADDR *highpc, struct dwarf2_cu *cu)
3313 {
3314 struct attribute *attr;
3315 CORE_ADDR low = 0;
3316 CORE_ADDR high = 0;
3317 int ret = 0;
3318
3319 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3320 if (attr)
3321 {
3322 high = DW_ADDR (attr);
3323 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3324 if (attr)
3325 low = DW_ADDR (attr);
3326 else
3327 /* Found high w/o low attribute. */
3328 return 0;
3329
3330 /* Found consecutive range of addresses. */
3331 ret = 1;
3332 }
3333 else
3334 {
3335 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3336 if (attr != NULL)
3337 {
3338 /* Value of the DW_AT_ranges attribute is the offset in the
3339 .debug_ranges section. */
3340 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
3341 return 0;
3342 /* Found discontinuous range of addresses. */
3343 ret = -1;
3344 }
3345 }
3346
3347 if (high < low)
3348 return 0;
3349
3350 /* When using the GNU linker, .gnu.linkonce. sections are used to
3351 eliminate duplicate copies of functions and vtables and such.
3352 The linker will arbitrarily choose one and discard the others.
3353 The AT_*_pc values for such functions refer to local labels in
3354 these sections. If the section from that file was discarded, the
3355 labels are not in the output, so the relocs get a value of 0.
3356 If this is a discarded function, mark the pc bounds as invalid,
3357 so that GDB will ignore it. */
3358 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
3359 return 0;
3360
3361 *lowpc = low;
3362 *highpc = high;
3363 return ret;
3364 }
3365
3366 /* Get the low and high pc's represented by the scope DIE, and store
3367 them in *LOWPC and *HIGHPC. If the correct values can't be
3368 determined, set *LOWPC to -1 and *HIGHPC to 0. */
3369
3370 static void
3371 get_scope_pc_bounds (struct die_info *die,
3372 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3373 struct dwarf2_cu *cu)
3374 {
3375 CORE_ADDR best_low = (CORE_ADDR) -1;
3376 CORE_ADDR best_high = (CORE_ADDR) 0;
3377 CORE_ADDR current_low, current_high;
3378
3379 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3380 {
3381 best_low = current_low;
3382 best_high = current_high;
3383 }
3384 else
3385 {
3386 struct die_info *child = die->child;
3387
3388 while (child && child->tag)
3389 {
3390 switch (child->tag) {
3391 case DW_TAG_subprogram:
3392 if (dwarf2_get_pc_bounds (child, &current_low, &current_high, cu))
3393 {
3394 best_low = min (best_low, current_low);
3395 best_high = max (best_high, current_high);
3396 }
3397 break;
3398 case DW_TAG_namespace:
3399 /* FIXME: carlton/2004-01-16: Should we do this for
3400 DW_TAG_class_type/DW_TAG_structure_type, too? I think
3401 that current GCC's always emit the DIEs corresponding
3402 to definitions of methods of classes as children of a
3403 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3404 the DIEs giving the declarations, which could be
3405 anywhere). But I don't see any reason why the
3406 standards says that they have to be there. */
3407 get_scope_pc_bounds (child, &current_low, &current_high, cu);
3408
3409 if (current_low != ((CORE_ADDR) -1))
3410 {
3411 best_low = min (best_low, current_low);
3412 best_high = max (best_high, current_high);
3413 }
3414 break;
3415 default:
3416 /* Ignore. */
3417 break;
3418 }
3419
3420 child = sibling_die (child);
3421 }
3422 }
3423
3424 *lowpc = best_low;
3425 *highpc = best_high;
3426 }
3427
3428 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
3429 in DIE. */
3430 static void
3431 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
3432 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
3433 {
3434 struct attribute *attr;
3435
3436 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3437 if (attr)
3438 {
3439 CORE_ADDR high = DW_ADDR (attr);
3440 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3441 if (attr)
3442 {
3443 CORE_ADDR low = DW_ADDR (attr);
3444 record_block_range (block, baseaddr + low, baseaddr + high - 1);
3445 }
3446 }
3447
3448 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3449 if (attr)
3450 {
3451 bfd *obfd = cu->objfile->obfd;
3452
3453 /* The value of the DW_AT_ranges attribute is the offset of the
3454 address range list in the .debug_ranges section. */
3455 unsigned long offset = DW_UNSND (attr);
3456 gdb_byte *buffer = dwarf2_per_objfile->ranges_buffer + offset;
3457
3458 /* For some target architectures, but not others, the
3459 read_address function sign-extends the addresses it returns.
3460 To recognize base address selection entries, we need a
3461 mask. */
3462 unsigned int addr_size = cu->header.addr_size;
3463 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3464
3465 /* The base address, to which the next pair is relative. Note
3466 that this 'base' is a DWARF concept: most entries in a range
3467 list are relative, to reduce the number of relocs against the
3468 debugging information. This is separate from this function's
3469 'baseaddr' argument, which GDB uses to relocate debugging
3470 information from a shared library based on the address at
3471 which the library was loaded. */
3472 CORE_ADDR base = cu->header.base_address;
3473 int base_known = cu->header.base_known;
3474
3475 if (offset >= dwarf2_per_objfile->ranges_size)
3476 {
3477 complaint (&symfile_complaints,
3478 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
3479 offset);
3480 return;
3481 }
3482
3483 for (;;)
3484 {
3485 unsigned int bytes_read;
3486 CORE_ADDR start, end;
3487
3488 start = read_address (obfd, buffer, cu, &bytes_read);
3489 buffer += bytes_read;
3490 end = read_address (obfd, buffer, cu, &bytes_read);
3491 buffer += bytes_read;
3492
3493 /* Did we find the end of the range list? */
3494 if (start == 0 && end == 0)
3495 break;
3496
3497 /* Did we find a base address selection entry? */
3498 else if ((start & base_select_mask) == base_select_mask)
3499 {
3500 base = end;
3501 base_known = 1;
3502 }
3503
3504 /* We found an ordinary address range. */
3505 else
3506 {
3507 if (!base_known)
3508 {
3509 complaint (&symfile_complaints,
3510 _("Invalid .debug_ranges data (no base address)"));
3511 return;
3512 }
3513
3514 record_block_range (block,
3515 baseaddr + base + start,
3516 baseaddr + base + end - 1);
3517 }
3518 }
3519 }
3520 }
3521
3522 /* Add an aggregate field to the field list. */
3523
3524 static void
3525 dwarf2_add_field (struct field_info *fip, struct die_info *die,
3526 struct dwarf2_cu *cu)
3527 {
3528 struct objfile *objfile = cu->objfile;
3529 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3530 struct nextfield *new_field;
3531 struct attribute *attr;
3532 struct field *fp;
3533 char *fieldname = "";
3534
3535 /* Allocate a new field list entry and link it in. */
3536 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3537 make_cleanup (xfree, new_field);
3538 memset (new_field, 0, sizeof (struct nextfield));
3539 new_field->next = fip->fields;
3540 fip->fields = new_field;
3541 fip->nfields++;
3542
3543 /* Handle accessibility and virtuality of field.
3544 The default accessibility for members is public, the default
3545 accessibility for inheritance is private. */
3546 if (die->tag != DW_TAG_inheritance)
3547 new_field->accessibility = DW_ACCESS_public;
3548 else
3549 new_field->accessibility = DW_ACCESS_private;
3550 new_field->virtuality = DW_VIRTUALITY_none;
3551
3552 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3553 if (attr)
3554 new_field->accessibility = DW_UNSND (attr);
3555 if (new_field->accessibility != DW_ACCESS_public)
3556 fip->non_public_fields = 1;
3557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
3558 if (attr)
3559 new_field->virtuality = DW_UNSND (attr);
3560
3561 fp = &new_field->field;
3562
3563 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
3564 {
3565 /* Data member other than a C++ static data member. */
3566
3567 /* Get type of field. */
3568 fp->type = die_type (die, cu);
3569
3570 FIELD_STATIC_KIND (*fp) = 0;
3571
3572 /* Get bit size of field (zero if none). */
3573 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
3574 if (attr)
3575 {
3576 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3577 }
3578 else
3579 {
3580 FIELD_BITSIZE (*fp) = 0;
3581 }
3582
3583 /* Get bit offset of field. */
3584 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3585 if (attr)
3586 {
3587 int byte_offset;
3588
3589 if (attr_form_is_section_offset (attr))
3590 {
3591 dwarf2_complex_location_expr_complaint ();
3592 byte_offset = 0;
3593 }
3594 else if (attr_form_is_constant (attr))
3595 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3596 else
3597 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
3598
3599 FIELD_BITPOS (*fp) = byte_offset * bits_per_byte;
3600 }
3601 else
3602 FIELD_BITPOS (*fp) = 0;
3603 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
3604 if (attr)
3605 {
3606 if (gdbarch_bits_big_endian (gdbarch))
3607 {
3608 /* For big endian bits, the DW_AT_bit_offset gives the
3609 additional bit offset from the MSB of the containing
3610 anonymous object to the MSB of the field. We don't
3611 have to do anything special since we don't need to
3612 know the size of the anonymous object. */
3613 FIELD_BITPOS (*fp) += DW_UNSND (attr);
3614 }
3615 else
3616 {
3617 /* For little endian bits, compute the bit offset to the
3618 MSB of the anonymous object, subtract off the number of
3619 bits from the MSB of the field to the MSB of the
3620 object, and then subtract off the number of bits of
3621 the field itself. The result is the bit offset of
3622 the LSB of the field. */
3623 int anonymous_size;
3624 int bit_offset = DW_UNSND (attr);
3625
3626 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3627 if (attr)
3628 {
3629 /* The size of the anonymous object containing
3630 the bit field is explicit, so use the
3631 indicated size (in bytes). */
3632 anonymous_size = DW_UNSND (attr);
3633 }
3634 else
3635 {
3636 /* The size of the anonymous object containing
3637 the bit field must be inferred from the type
3638 attribute of the data member containing the
3639 bit field. */
3640 anonymous_size = TYPE_LENGTH (fp->type);
3641 }
3642 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3643 - bit_offset - FIELD_BITSIZE (*fp);
3644 }
3645 }
3646
3647 /* Get name of field. */
3648 fieldname = dwarf2_name (die, cu);
3649 if (fieldname == NULL)
3650 fieldname = "";
3651
3652 /* The name is already allocated along with this objfile, so we don't
3653 need to duplicate it for the type. */
3654 fp->name = fieldname;
3655
3656 /* Change accessibility for artificial fields (e.g. virtual table
3657 pointer or virtual base class pointer) to private. */
3658 if (dwarf2_attr (die, DW_AT_artificial, cu))
3659 {
3660 new_field->accessibility = DW_ACCESS_private;
3661 fip->non_public_fields = 1;
3662 }
3663 }
3664 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
3665 {
3666 /* C++ static member. */
3667
3668 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3669 is a declaration, but all versions of G++ as of this writing
3670 (so through at least 3.2.1) incorrectly generate
3671 DW_TAG_variable tags. */
3672
3673 char *physname;
3674
3675 /* Get name of field. */
3676 fieldname = dwarf2_name (die, cu);
3677 if (fieldname == NULL)
3678 return;
3679
3680 /* Get physical name. */
3681 physname = dwarf2_linkage_name (die, cu);
3682
3683 /* The name is already allocated along with this objfile, so we don't
3684 need to duplicate it for the type. */
3685 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
3686 FIELD_TYPE (*fp) = die_type (die, cu);
3687 FIELD_NAME (*fp) = fieldname;
3688 }
3689 else if (die->tag == DW_TAG_inheritance)
3690 {
3691 /* C++ base class field. */
3692 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3693 if (attr)
3694 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
3695 * bits_per_byte);
3696 FIELD_BITSIZE (*fp) = 0;
3697 FIELD_STATIC_KIND (*fp) = 0;
3698 FIELD_TYPE (*fp) = die_type (die, cu);
3699 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3700 fip->nbaseclasses++;
3701 }
3702 }
3703
3704 /* Create the vector of fields, and attach it to the type. */
3705
3706 static void
3707 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
3708 struct dwarf2_cu *cu)
3709 {
3710 int nfields = fip->nfields;
3711
3712 /* Record the field count, allocate space for the array of fields,
3713 and create blank accessibility bitfields if necessary. */
3714 TYPE_NFIELDS (type) = nfields;
3715 TYPE_FIELDS (type) = (struct field *)
3716 TYPE_ALLOC (type, sizeof (struct field) * nfields);
3717 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3718
3719 if (fip->non_public_fields)
3720 {
3721 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3722
3723 TYPE_FIELD_PRIVATE_BITS (type) =
3724 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3725 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3726
3727 TYPE_FIELD_PROTECTED_BITS (type) =
3728 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3729 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3730
3731 TYPE_FIELD_IGNORE_BITS (type) =
3732 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3733 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3734 }
3735
3736 /* If the type has baseclasses, allocate and clear a bit vector for
3737 TYPE_FIELD_VIRTUAL_BITS. */
3738 if (fip->nbaseclasses)
3739 {
3740 int num_bytes = B_BYTES (fip->nbaseclasses);
3741 unsigned char *pointer;
3742
3743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3744 pointer = TYPE_ALLOC (type, num_bytes);
3745 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
3746 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3747 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3748 }
3749
3750 /* Copy the saved-up fields into the field vector. Start from the head
3751 of the list, adding to the tail of the field array, so that they end
3752 up in the same order in the array in which they were added to the list. */
3753 while (nfields-- > 0)
3754 {
3755 TYPE_FIELD (type, nfields) = fip->fields->field;
3756 switch (fip->fields->accessibility)
3757 {
3758 case DW_ACCESS_private:
3759 SET_TYPE_FIELD_PRIVATE (type, nfields);
3760 break;
3761
3762 case DW_ACCESS_protected:
3763 SET_TYPE_FIELD_PROTECTED (type, nfields);
3764 break;
3765
3766 case DW_ACCESS_public:
3767 break;
3768
3769 default:
3770 /* Unknown accessibility. Complain and treat it as public. */
3771 {
3772 complaint (&symfile_complaints, _("unsupported accessibility %d"),
3773 fip->fields->accessibility);
3774 }
3775 break;
3776 }
3777 if (nfields < fip->nbaseclasses)
3778 {
3779 switch (fip->fields->virtuality)
3780 {
3781 case DW_VIRTUALITY_virtual:
3782 case DW_VIRTUALITY_pure_virtual:
3783 SET_TYPE_FIELD_VIRTUAL (type, nfields);
3784 break;
3785 }
3786 }
3787 fip->fields = fip->fields->next;
3788 }
3789 }
3790
3791 /* Add a member function to the proper fieldlist. */
3792
3793 static void
3794 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
3795 struct type *type, struct dwarf2_cu *cu)
3796 {
3797 struct objfile *objfile = cu->objfile;
3798 struct attribute *attr;
3799 struct fnfieldlist *flp;
3800 int i;
3801 struct fn_field *fnp;
3802 char *fieldname;
3803 char *physname;
3804 struct nextfnfield *new_fnfield;
3805
3806 /* Get name of member function. */
3807 fieldname = dwarf2_name (die, cu);
3808 if (fieldname == NULL)
3809 return;
3810
3811 /* Get the mangled name. */
3812 physname = dwarf2_linkage_name (die, cu);
3813
3814 /* Look up member function name in fieldlist. */
3815 for (i = 0; i < fip->nfnfields; i++)
3816 {
3817 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
3818 break;
3819 }
3820
3821 /* Create new list element if necessary. */
3822 if (i < fip->nfnfields)
3823 flp = &fip->fnfieldlists[i];
3824 else
3825 {
3826 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3827 {
3828 fip->fnfieldlists = (struct fnfieldlist *)
3829 xrealloc (fip->fnfieldlists,
3830 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
3831 * sizeof (struct fnfieldlist));
3832 if (fip->nfnfields == 0)
3833 make_cleanup (free_current_contents, &fip->fnfieldlists);
3834 }
3835 flp = &fip->fnfieldlists[fip->nfnfields];
3836 flp->name = fieldname;
3837 flp->length = 0;
3838 flp->head = NULL;
3839 fip->nfnfields++;
3840 }
3841
3842 /* Create a new member function field and chain it to the field list
3843 entry. */
3844 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
3845 make_cleanup (xfree, new_fnfield);
3846 memset (new_fnfield, 0, sizeof (struct nextfnfield));
3847 new_fnfield->next = flp->head;
3848 flp->head = new_fnfield;
3849 flp->length++;
3850
3851 /* Fill in the member function field info. */
3852 fnp = &new_fnfield->fnfield;
3853 /* The name is already allocated along with this objfile, so we don't
3854 need to duplicate it for the type. */
3855 fnp->physname = physname ? physname : "";
3856 fnp->type = alloc_type (objfile);
3857 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
3858 {
3859 int nparams = TYPE_NFIELDS (die->type);
3860
3861 /* TYPE is the domain of this method, and DIE->TYPE is the type
3862 of the method itself (TYPE_CODE_METHOD). */
3863 smash_to_method_type (fnp->type, type,
3864 TYPE_TARGET_TYPE (die->type),
3865 TYPE_FIELDS (die->type),
3866 TYPE_NFIELDS (die->type),
3867 TYPE_VARARGS (die->type));
3868
3869 /* Handle static member functions.
3870 Dwarf2 has no clean way to discern C++ static and non-static
3871 member functions. G++ helps GDB by marking the first
3872 parameter for non-static member functions (which is the
3873 this pointer) as artificial. We obtain this information
3874 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
3875 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
3876 fnp->voffset = VOFFSET_STATIC;
3877 }
3878 else
3879 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3880 physname);
3881
3882 /* Get fcontext from DW_AT_containing_type if present. */
3883 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3884 fnp->fcontext = die_containing_type (die, cu);
3885
3886 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3887 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
3888
3889 /* Get accessibility. */
3890 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3891 if (attr)
3892 {
3893 switch (DW_UNSND (attr))
3894 {
3895 case DW_ACCESS_private:
3896 fnp->is_private = 1;
3897 break;
3898 case DW_ACCESS_protected:
3899 fnp->is_protected = 1;
3900 break;
3901 }
3902 }
3903
3904 /* Check for artificial methods. */
3905 attr = dwarf2_attr (die, DW_AT_artificial, cu);
3906 if (attr && DW_UNSND (attr) != 0)
3907 fnp->is_artificial = 1;
3908
3909 /* Get index in virtual function table if it is a virtual member function. */
3910 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
3911 if (attr)
3912 {
3913 /* Support the .debug_loc offsets */
3914 if (attr_form_is_block (attr))
3915 {
3916 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
3917 }
3918 else if (attr_form_is_section_offset (attr))
3919 {
3920 dwarf2_complex_location_expr_complaint ();
3921 }
3922 else
3923 {
3924 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3925 fieldname);
3926 }
3927 }
3928 }
3929
3930 /* Create the vector of member function fields, and attach it to the type. */
3931
3932 static void
3933 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
3934 struct dwarf2_cu *cu)
3935 {
3936 struct fnfieldlist *flp;
3937 int total_length = 0;
3938 int i;
3939
3940 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3941 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3942 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
3943
3944 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
3945 {
3946 struct nextfnfield *nfp = flp->head;
3947 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
3948 int k;
3949
3950 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
3951 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
3952 fn_flp->fn_fields = (struct fn_field *)
3953 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
3954 for (k = flp->length; (k--, nfp); nfp = nfp->next)
3955 fn_flp->fn_fields[k] = nfp->fnfield;
3956
3957 total_length += flp->length;
3958 }
3959
3960 TYPE_NFN_FIELDS (type) = fip->nfnfields;
3961 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3962 }
3963
3964 /* Returns non-zero if NAME is the name of a vtable member in CU's
3965 language, zero otherwise. */
3966 static int
3967 is_vtable_name (const char *name, struct dwarf2_cu *cu)
3968 {
3969 static const char vptr[] = "_vptr";
3970 static const char vtable[] = "vtable";
3971
3972 /* Look for the C++ and Java forms of the vtable. */
3973 if ((cu->language == language_java
3974 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
3975 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
3976 && is_cplus_marker (name[sizeof (vptr) - 1])))
3977 return 1;
3978
3979 return 0;
3980 }
3981
3982 /* GCC outputs unnamed structures that are really pointers to member
3983 functions, with the ABI-specified layout. If DIE (from CU) describes
3984 such a structure, set its type, and return nonzero. Otherwise return
3985 zero.
3986
3987 GCC shouldn't do this; it should just output pointer to member DIEs.
3988 This is GCC PR debug/28767. */
3989
3990 static int
3991 quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
3992 {
3993 struct objfile *objfile = cu->objfile;
3994 struct type *type;
3995 struct die_info *pfn_die, *delta_die;
3996 struct attribute *pfn_name, *delta_name;
3997 struct type *pfn_type, *domain_type;
3998
3999 /* Check for a structure with no name and two children. */
4000 if (die->tag != DW_TAG_structure_type
4001 || dwarf2_attr (die, DW_AT_name, cu) != NULL
4002 || die->child == NULL
4003 || die->child->sibling == NULL
4004 || (die->child->sibling->sibling != NULL
4005 && die->child->sibling->sibling->tag != DW_TAG_padding))
4006 return 0;
4007
4008 /* Check for __pfn and __delta members. */
4009 pfn_die = die->child;
4010 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
4011 if (pfn_die->tag != DW_TAG_member
4012 || pfn_name == NULL
4013 || DW_STRING (pfn_name) == NULL
4014 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
4015 return 0;
4016
4017 delta_die = pfn_die->sibling;
4018 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
4019 if (delta_die->tag != DW_TAG_member
4020 || delta_name == NULL
4021 || DW_STRING (delta_name) == NULL
4022 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
4023 return 0;
4024
4025 /* Find the type of the method. */
4026 pfn_type = die_type (pfn_die, cu);
4027 if (pfn_type == NULL
4028 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4029 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
4030 return 0;
4031
4032 /* Look for the "this" argument. */
4033 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4034 if (TYPE_NFIELDS (pfn_type) == 0
4035 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
4036 return 0;
4037
4038 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4039 type = alloc_type (objfile);
4040 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4041 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4042 TYPE_VARARGS (pfn_type));
4043 type = lookup_methodptr_type (type);
4044 set_die_type (die, type, cu);
4045
4046 return 1;
4047 }
4048
4049 /* Called when we find the DIE that starts a structure or union scope
4050 (definition) to process all dies that define the members of the
4051 structure or union.
4052
4053 NOTE: we need to call struct_type regardless of whether or not the
4054 DIE has an at_name attribute, since it might be an anonymous
4055 structure or union. This gets the type entered into our set of
4056 user defined types.
4057
4058 However, if the structure is incomplete (an opaque struct/union)
4059 then suppress creating a symbol table entry for it since gdb only
4060 wants to find the one with the complete definition. Note that if
4061 it is complete, we just call new_symbol, which does it's own
4062 checking about whether the struct/union is anonymous or not (and
4063 suppresses creating a symbol table entry itself). */
4064
4065 static void
4066 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
4067 {
4068 struct objfile *objfile = cu->objfile;
4069 struct type *type;
4070 struct attribute *attr;
4071 const char *previous_prefix = processing_current_prefix;
4072 struct cleanup *back_to = NULL;
4073 char *name;
4074
4075 if (die->type)
4076 return;
4077
4078 if (quirk_gcc_member_function_pointer (die, cu))
4079 return;
4080
4081 type = alloc_type (objfile);
4082 INIT_CPLUS_SPECIFIC (type);
4083 name = dwarf2_name (die, cu);
4084 if (name != NULL)
4085 {
4086 if (cu->language == language_cplus
4087 || cu->language == language_java)
4088 {
4089 char *new_prefix = determine_class_name (die, cu);
4090 TYPE_TAG_NAME (type) = obsavestring (new_prefix,
4091 strlen (new_prefix),
4092 &objfile->objfile_obstack);
4093 back_to = make_cleanup (xfree, new_prefix);
4094 processing_current_prefix = new_prefix;
4095 }
4096 else
4097 {
4098 /* The name is already allocated along with this objfile, so
4099 we don't need to duplicate it for the type. */
4100 TYPE_TAG_NAME (type) = name;
4101 }
4102 }
4103
4104 if (die->tag == DW_TAG_structure_type)
4105 {
4106 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4107 }
4108 else if (die->tag == DW_TAG_union_type)
4109 {
4110 TYPE_CODE (type) = TYPE_CODE_UNION;
4111 }
4112 else
4113 {
4114 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
4115 in gdbtypes.h. */
4116 TYPE_CODE (type) = TYPE_CODE_CLASS;
4117 }
4118
4119 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4120 if (attr)
4121 {
4122 TYPE_LENGTH (type) = DW_UNSND (attr);
4123 }
4124 else
4125 {
4126 TYPE_LENGTH (type) = 0;
4127 }
4128
4129 TYPE_FLAGS (type) |= TYPE_FLAG_STUB_SUPPORTED;
4130 if (die_is_declaration (die, cu))
4131 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
4132
4133 /* We need to add the type field to the die immediately so we don't
4134 infinitely recurse when dealing with pointers to the structure
4135 type within the structure itself. */
4136 set_die_type (die, type, cu);
4137
4138 if (die->child != NULL && ! die_is_declaration (die, cu))
4139 {
4140 struct field_info fi;
4141 struct die_info *child_die;
4142 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
4143
4144 memset (&fi, 0, sizeof (struct field_info));
4145
4146 child_die = die->child;
4147
4148 while (child_die && child_die->tag)
4149 {
4150 if (child_die->tag == DW_TAG_member
4151 || child_die->tag == DW_TAG_variable)
4152 {
4153 /* NOTE: carlton/2002-11-05: A C++ static data member
4154 should be a DW_TAG_member that is a declaration, but
4155 all versions of G++ as of this writing (so through at
4156 least 3.2.1) incorrectly generate DW_TAG_variable
4157 tags for them instead. */
4158 dwarf2_add_field (&fi, child_die, cu);
4159 }
4160 else if (child_die->tag == DW_TAG_subprogram)
4161 {
4162 /* C++ member function. */
4163 read_type_die (child_die, cu);
4164 dwarf2_add_member_fn (&fi, child_die, type, cu);
4165 }
4166 else if (child_die->tag == DW_TAG_inheritance)
4167 {
4168 /* C++ base class field. */
4169 dwarf2_add_field (&fi, child_die, cu);
4170 }
4171 child_die = sibling_die (child_die);
4172 }
4173
4174 /* Attach fields and member functions to the type. */
4175 if (fi.nfields)
4176 dwarf2_attach_fields_to_type (&fi, type, cu);
4177 if (fi.nfnfields)
4178 {
4179 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
4180
4181 /* Get the type which refers to the base class (possibly this
4182 class itself) which contains the vtable pointer for the current
4183 class from the DW_AT_containing_type attribute. */
4184
4185 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
4186 {
4187 struct type *t = die_containing_type (die, cu);
4188
4189 TYPE_VPTR_BASETYPE (type) = t;
4190 if (type == t)
4191 {
4192 int i;
4193
4194 /* Our own class provides vtbl ptr. */
4195 for (i = TYPE_NFIELDS (t) - 1;
4196 i >= TYPE_N_BASECLASSES (t);
4197 --i)
4198 {
4199 char *fieldname = TYPE_FIELD_NAME (t, i);
4200
4201 if (is_vtable_name (fieldname, cu))
4202 {
4203 TYPE_VPTR_FIELDNO (type) = i;
4204 break;
4205 }
4206 }
4207
4208 /* Complain if virtual function table field not found. */
4209 if (i < TYPE_N_BASECLASSES (t))
4210 complaint (&symfile_complaints,
4211 _("virtual function table pointer not found when defining class '%s'"),
4212 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
4213 "");
4214 }
4215 else
4216 {
4217 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
4218 }
4219 }
4220 else if (cu->producer
4221 && strncmp (cu->producer,
4222 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
4223 {
4224 /* The IBM XLC compiler does not provide direct indication
4225 of the containing type, but the vtable pointer is
4226 always named __vfp. */
4227
4228 int i;
4229
4230 for (i = TYPE_NFIELDS (type) - 1;
4231 i >= TYPE_N_BASECLASSES (type);
4232 --i)
4233 {
4234 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
4235 {
4236 TYPE_VPTR_FIELDNO (type) = i;
4237 TYPE_VPTR_BASETYPE (type) = type;
4238 break;
4239 }
4240 }
4241 }
4242 }
4243
4244 do_cleanups (back_to);
4245 }
4246
4247 processing_current_prefix = previous_prefix;
4248 if (back_to != NULL)
4249 do_cleanups (back_to);
4250 }
4251
4252 static void
4253 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
4254 {
4255 struct objfile *objfile = cu->objfile;
4256 const char *previous_prefix = processing_current_prefix;
4257 struct die_info *child_die = die->child;
4258
4259 if (TYPE_TAG_NAME (die->type) != NULL)
4260 processing_current_prefix = TYPE_TAG_NAME (die->type);
4261
4262 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
4263 snapshots) has been known to create a die giving a declaration
4264 for a class that has, as a child, a die giving a definition for a
4265 nested class. So we have to process our children even if the
4266 current die is a declaration. Normally, of course, a declaration
4267 won't have any children at all. */
4268
4269 while (child_die != NULL && child_die->tag)
4270 {
4271 if (child_die->tag == DW_TAG_member
4272 || child_die->tag == DW_TAG_variable
4273 || child_die->tag == DW_TAG_inheritance)
4274 {
4275 /* Do nothing. */
4276 }
4277 else
4278 process_die (child_die, cu);
4279
4280 child_die = sibling_die (child_die);
4281 }
4282
4283 /* Do not consider external references. According to the DWARF standard,
4284 these DIEs are identified by the fact that they have no byte_size
4285 attribute, and a declaration attribute. */
4286 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
4287 || !die_is_declaration (die, cu))
4288 new_symbol (die, die->type, cu);
4289
4290 processing_current_prefix = previous_prefix;
4291 }
4292
4293 /* Given a DW_AT_enumeration_type die, set its type. We do not
4294 complete the type's fields yet, or create any symbols. */
4295
4296 static void
4297 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
4298 {
4299 struct objfile *objfile = cu->objfile;
4300 struct type *type;
4301 struct attribute *attr;
4302 char *name;
4303
4304 if (die->type)
4305 return;
4306
4307 type = alloc_type (objfile);
4308
4309 TYPE_CODE (type) = TYPE_CODE_ENUM;
4310 name = dwarf2_name (die, cu);
4311 if (name != NULL)
4312 {
4313 if (processing_has_namespace_info)
4314 {
4315 TYPE_TAG_NAME (type) = typename_concat (&objfile->objfile_obstack,
4316 processing_current_prefix,
4317 name, cu);
4318 }
4319 else
4320 {
4321 /* The name is already allocated along with this objfile, so
4322 we don't need to duplicate it for the type. */
4323 TYPE_TAG_NAME (type) = name;
4324 }
4325 }
4326
4327 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4328 if (attr)
4329 {
4330 TYPE_LENGTH (type) = DW_UNSND (attr);
4331 }
4332 else
4333 {
4334 TYPE_LENGTH (type) = 0;
4335 }
4336
4337 /* The enumeration DIE can be incomplete. In Ada, any type can be
4338 declared as private in the package spec, and then defined only
4339 inside the package body. Such types are known as Taft Amendment
4340 Types. When another package uses such a type, an incomplete DIE
4341 may be generated by the compiler. */
4342 if (die_is_declaration (die, cu))
4343 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
4344
4345 set_die_type (die, type, cu);
4346 }
4347
4348 /* Determine the name of the type represented by DIE, which should be
4349 a named C++ or Java compound type. Return the name in question; the caller
4350 is responsible for xfree()'ing it. */
4351
4352 static char *
4353 determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
4354 {
4355 struct cleanup *back_to = NULL;
4356 struct die_info *spec_die = die_specification (die, cu);
4357 char *new_prefix = NULL;
4358
4359 /* If this is the definition of a class that is declared by another
4360 die, then processing_current_prefix may not be accurate; see
4361 read_func_scope for a similar example. */
4362 if (spec_die != NULL)
4363 {
4364 char *specification_prefix = determine_prefix (spec_die, cu);
4365 processing_current_prefix = specification_prefix;
4366 back_to = make_cleanup (xfree, specification_prefix);
4367 }
4368
4369 /* If we don't have namespace debug info, guess the name by trying
4370 to demangle the names of members, just like we did in
4371 guess_structure_name. */
4372 if (!processing_has_namespace_info)
4373 {
4374 struct die_info *child;
4375
4376 for (child = die->child;
4377 child != NULL && child->tag != 0;
4378 child = sibling_die (child))
4379 {
4380 if (child->tag == DW_TAG_subprogram)
4381 {
4382 new_prefix
4383 = language_class_name_from_physname (cu->language_defn,
4384 dwarf2_linkage_name
4385 (child, cu));
4386
4387 if (new_prefix != NULL)
4388 break;
4389 }
4390 }
4391 }
4392
4393 if (new_prefix == NULL)
4394 {
4395 const char *name = dwarf2_name (die, cu);
4396 new_prefix = typename_concat (NULL, processing_current_prefix,
4397 name ? name : "<<anonymous>>",
4398 cu);
4399 }
4400
4401 if (back_to != NULL)
4402 do_cleanups (back_to);
4403
4404 return new_prefix;
4405 }
4406
4407 /* Given a pointer to a die which begins an enumeration, process all
4408 the dies that define the members of the enumeration, and create the
4409 symbol for the enumeration type.
4410
4411 NOTE: We reverse the order of the element list. */
4412
4413 static void
4414 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4415 {
4416 struct objfile *objfile = cu->objfile;
4417 struct die_info *child_die;
4418 struct field *fields;
4419 struct symbol *sym;
4420 int num_fields;
4421 int unsigned_enum = 1;
4422 char *name;
4423
4424 num_fields = 0;
4425 fields = NULL;
4426 if (die->child != NULL)
4427 {
4428 child_die = die->child;
4429 while (child_die && child_die->tag)
4430 {
4431 if (child_die->tag != DW_TAG_enumerator)
4432 {
4433 process_die (child_die, cu);
4434 }
4435 else
4436 {
4437 name = dwarf2_name (child_die, cu);
4438 if (name)
4439 {
4440 sym = new_symbol (child_die, die->type, cu);
4441 if (SYMBOL_VALUE (sym) < 0)
4442 unsigned_enum = 0;
4443
4444 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4445 {
4446 fields = (struct field *)
4447 xrealloc (fields,
4448 (num_fields + DW_FIELD_ALLOC_CHUNK)
4449 * sizeof (struct field));
4450 }
4451
4452 FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
4453 FIELD_TYPE (fields[num_fields]) = NULL;
4454 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
4455 FIELD_BITSIZE (fields[num_fields]) = 0;
4456 FIELD_STATIC_KIND (fields[num_fields]) = 0;
4457
4458 num_fields++;
4459 }
4460 }
4461
4462 child_die = sibling_die (child_die);
4463 }
4464
4465 if (num_fields)
4466 {
4467 TYPE_NFIELDS (die->type) = num_fields;
4468 TYPE_FIELDS (die->type) = (struct field *)
4469 TYPE_ALLOC (die->type, sizeof (struct field) * num_fields);
4470 memcpy (TYPE_FIELDS (die->type), fields,
4471 sizeof (struct field) * num_fields);
4472 xfree (fields);
4473 }
4474 if (unsigned_enum)
4475 TYPE_FLAGS (die->type) |= TYPE_FLAG_UNSIGNED;
4476 }
4477
4478 new_symbol (die, die->type, cu);
4479 }
4480
4481 /* Extract all information from a DW_TAG_array_type DIE and put it in
4482 the DIE's type field. For now, this only handles one dimensional
4483 arrays. */
4484
4485 static void
4486 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
4487 {
4488 struct objfile *objfile = cu->objfile;
4489 struct die_info *child_die;
4490 struct type *type = NULL;
4491 struct type *element_type, *range_type, *index_type;
4492 struct type **range_types = NULL;
4493 struct attribute *attr;
4494 int ndim = 0;
4495 struct cleanup *back_to;
4496 char *name;
4497
4498 /* Return if we've already decoded this type. */
4499 if (die->type)
4500 {
4501 return;
4502 }
4503
4504 element_type = die_type (die, cu);
4505
4506 /* Irix 6.2 native cc creates array types without children for
4507 arrays with unspecified length. */
4508 if (die->child == NULL)
4509 {
4510 index_type = builtin_type_int32;
4511 range_type = create_range_type (NULL, index_type, 0, -1);
4512 set_die_type (die, create_array_type (NULL, element_type, range_type),
4513 cu);
4514 return;
4515 }
4516
4517 back_to = make_cleanup (null_cleanup, NULL);
4518 child_die = die->child;
4519 while (child_die && child_die->tag)
4520 {
4521 if (child_die->tag == DW_TAG_subrange_type)
4522 {
4523 read_subrange_type (child_die, cu);
4524
4525 if (child_die->type != NULL)
4526 {
4527 /* The range type was succesfully read. Save it for
4528 the array type creation. */
4529 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4530 {
4531 range_types = (struct type **)
4532 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4533 * sizeof (struct type *));
4534 if (ndim == 0)
4535 make_cleanup (free_current_contents, &range_types);
4536 }
4537 range_types[ndim++] = child_die->type;
4538 }
4539 }
4540 child_die = sibling_die (child_die);
4541 }
4542
4543 /* Dwarf2 dimensions are output from left to right, create the
4544 necessary array types in backwards order. */
4545
4546 type = element_type;
4547
4548 if (read_array_order (die, cu) == DW_ORD_col_major)
4549 {
4550 int i = 0;
4551 while (i < ndim)
4552 type = create_array_type (NULL, type, range_types[i++]);
4553 }
4554 else
4555 {
4556 while (ndim-- > 0)
4557 type = create_array_type (NULL, type, range_types[ndim]);
4558 }
4559
4560 /* Understand Dwarf2 support for vector types (like they occur on
4561 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
4562 array type. This is not part of the Dwarf2/3 standard yet, but a
4563 custom vendor extension. The main difference between a regular
4564 array and the vector variant is that vectors are passed by value
4565 to functions. */
4566 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
4567 if (attr)
4568 make_vector_type (type);
4569
4570 name = dwarf2_name (die, cu);
4571 if (name)
4572 TYPE_NAME (type) = name;
4573
4574 do_cleanups (back_to);
4575
4576 /* Install the type in the die. */
4577 set_die_type (die, type, cu);
4578 }
4579
4580 static enum dwarf_array_dim_ordering
4581 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
4582 {
4583 struct attribute *attr;
4584
4585 attr = dwarf2_attr (die, DW_AT_ordering, cu);
4586
4587 if (attr) return DW_SND (attr);
4588
4589 /*
4590 GNU F77 is a special case, as at 08/2004 array type info is the
4591 opposite order to the dwarf2 specification, but data is still
4592 laid out as per normal fortran.
4593
4594 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
4595 version checking.
4596 */
4597
4598 if (cu->language == language_fortran &&
4599 cu->producer && strstr (cu->producer, "GNU F77"))
4600 {
4601 return DW_ORD_row_major;
4602 }
4603
4604 switch (cu->language_defn->la_array_ordering)
4605 {
4606 case array_column_major:
4607 return DW_ORD_col_major;
4608 case array_row_major:
4609 default:
4610 return DW_ORD_row_major;
4611 };
4612 }
4613
4614 /* Extract all information from a DW_TAG_set_type DIE and put it in
4615 the DIE's type field. */
4616
4617 static void
4618 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
4619 {
4620 if (die->type == NULL)
4621 die->type = create_set_type ((struct type *) NULL, die_type (die, cu));
4622 }
4623
4624 /* First cut: install each common block member as a global variable. */
4625
4626 static void
4627 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
4628 {
4629 struct die_info *child_die;
4630 struct attribute *attr;
4631 struct symbol *sym;
4632 CORE_ADDR base = (CORE_ADDR) 0;
4633
4634 attr = dwarf2_attr (die, DW_AT_location, cu);
4635 if (attr)
4636 {
4637 /* Support the .debug_loc offsets */
4638 if (attr_form_is_block (attr))
4639 {
4640 base = decode_locdesc (DW_BLOCK (attr), cu);
4641 }
4642 else if (attr_form_is_section_offset (attr))
4643 {
4644 dwarf2_complex_location_expr_complaint ();
4645 }
4646 else
4647 {
4648 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4649 "common block member");
4650 }
4651 }
4652 if (die->child != NULL)
4653 {
4654 child_die = die->child;
4655 while (child_die && child_die->tag)
4656 {
4657 sym = new_symbol (child_die, NULL, cu);
4658 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
4659 if (attr)
4660 {
4661 SYMBOL_VALUE_ADDRESS (sym) =
4662 base + decode_locdesc (DW_BLOCK (attr), cu);
4663 add_symbol_to_list (sym, &global_symbols);
4664 }
4665 child_die = sibling_die (child_die);
4666 }
4667 }
4668 }
4669
4670 /* Read a C++ namespace. */
4671
4672 static void
4673 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4674 {
4675 struct objfile *objfile = cu->objfile;
4676 const char *previous_prefix = processing_current_prefix;
4677 const char *name;
4678 int is_anonymous;
4679 struct die_info *current_die;
4680 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4681
4682 name = namespace_name (die, &is_anonymous, cu);
4683
4684 /* Now build the name of the current namespace. */
4685
4686 if (previous_prefix[0] == '\0')
4687 {
4688 processing_current_prefix = name;
4689 }
4690 else
4691 {
4692 char *temp_name = typename_concat (NULL, previous_prefix, name, cu);
4693 make_cleanup (xfree, temp_name);
4694 processing_current_prefix = temp_name;
4695 }
4696
4697 /* Add a symbol associated to this if we haven't seen the namespace
4698 before. Also, add a using directive if it's an anonymous
4699 namespace. */
4700
4701 if (dwarf2_extension (die, cu) == NULL)
4702 {
4703 struct type *type;
4704
4705 /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
4706 this cast will hopefully become unnecessary. */
4707 type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
4708 (char *) processing_current_prefix,
4709 objfile);
4710 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4711
4712 new_symbol (die, type, cu);
4713 set_die_type (die, type, cu);
4714
4715 if (is_anonymous)
4716 cp_add_using_directive (processing_current_prefix,
4717 strlen (previous_prefix),
4718 strlen (processing_current_prefix));
4719 }
4720
4721 if (die->child != NULL)
4722 {
4723 struct die_info *child_die = die->child;
4724
4725 while (child_die && child_die->tag)
4726 {
4727 process_die (child_die, cu);
4728 child_die = sibling_die (child_die);
4729 }
4730 }
4731
4732 processing_current_prefix = previous_prefix;
4733 do_cleanups (back_to);
4734 }
4735
4736 /* Return the name of the namespace represented by DIE. Set
4737 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4738 namespace. */
4739
4740 static const char *
4741 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
4742 {
4743 struct die_info *current_die;
4744 const char *name = NULL;
4745
4746 /* Loop through the extensions until we find a name. */
4747
4748 for (current_die = die;
4749 current_die != NULL;
4750 current_die = dwarf2_extension (die, cu))
4751 {
4752 name = dwarf2_name (current_die, cu);
4753 if (name != NULL)
4754 break;
4755 }
4756
4757 /* Is it an anonymous namespace? */
4758
4759 *is_anonymous = (name == NULL);
4760 if (*is_anonymous)
4761 name = "(anonymous namespace)";
4762
4763 return name;
4764 }
4765
4766 /* Extract all information from a DW_TAG_pointer_type DIE and add to
4767 the user defined type vector. */
4768
4769 static void
4770 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
4771 {
4772 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
4773 struct comp_unit_head *cu_header = &cu->header;
4774 struct type *type;
4775 struct attribute *attr_byte_size;
4776 struct attribute *attr_address_class;
4777 int byte_size, addr_class;
4778
4779 if (die->type)
4780 {
4781 return;
4782 }
4783
4784 type = lookup_pointer_type (die_type (die, cu));
4785
4786 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
4787 if (attr_byte_size)
4788 byte_size = DW_UNSND (attr_byte_size);
4789 else
4790 byte_size = cu_header->addr_size;
4791
4792 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
4793 if (attr_address_class)
4794 addr_class = DW_UNSND (attr_address_class);
4795 else
4796 addr_class = DW_ADDR_none;
4797
4798 /* If the pointer size or address class is different than the
4799 default, create a type variant marked as such and set the
4800 length accordingly. */
4801 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
4802 {
4803 if (gdbarch_address_class_type_flags_p (gdbarch))
4804 {
4805 int type_flags;
4806
4807 type_flags = gdbarch_address_class_type_flags
4808 (gdbarch, byte_size, addr_class);
4809 gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
4810 type = make_type_with_address_space (type, type_flags);
4811 }
4812 else if (TYPE_LENGTH (type) != byte_size)
4813 {
4814 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
4815 }
4816 else {
4817 /* Should we also complain about unhandled address classes? */
4818 }
4819 }
4820
4821 TYPE_LENGTH (type) = byte_size;
4822 set_die_type (die, type, cu);
4823 }
4824
4825 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4826 the user defined type vector. */
4827
4828 static void
4829 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
4830 {
4831 struct objfile *objfile = cu->objfile;
4832 struct type *type;
4833 struct type *to_type;
4834 struct type *domain;
4835
4836 if (die->type)
4837 {
4838 return;
4839 }
4840
4841 to_type = die_type (die, cu);
4842 domain = die_containing_type (die, cu);
4843
4844 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
4845 type = lookup_methodptr_type (to_type);
4846 else
4847 type = lookup_memberptr_type (to_type, domain);
4848
4849 set_die_type (die, type, cu);
4850 }
4851
4852 /* Extract all information from a DW_TAG_reference_type DIE and add to
4853 the user defined type vector. */
4854
4855 static void
4856 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
4857 {
4858 struct comp_unit_head *cu_header = &cu->header;
4859 struct type *type;
4860 struct attribute *attr;
4861
4862 if (die->type)
4863 {
4864 return;
4865 }
4866
4867 type = lookup_reference_type (die_type (die, cu));
4868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4869 if (attr)
4870 {
4871 TYPE_LENGTH (type) = DW_UNSND (attr);
4872 }
4873 else
4874 {
4875 TYPE_LENGTH (type) = cu_header->addr_size;
4876 }
4877 set_die_type (die, type, cu);
4878 }
4879
4880 static void
4881 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
4882 {
4883 struct type *base_type;
4884
4885 if (die->type)
4886 {
4887 return;
4888 }
4889
4890 base_type = die_type (die, cu);
4891 set_die_type (die, make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0),
4892 cu);
4893 }
4894
4895 static void
4896 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
4897 {
4898 struct type *base_type;
4899
4900 if (die->type)
4901 {
4902 return;
4903 }
4904
4905 base_type = die_type (die, cu);
4906 set_die_type (die, make_cv_type (TYPE_CONST (base_type), 1, base_type, 0),
4907 cu);
4908 }
4909
4910 /* Extract all information from a DW_TAG_string_type DIE and add to
4911 the user defined type vector. It isn't really a user defined type,
4912 but it behaves like one, with other DIE's using an AT_user_def_type
4913 attribute to reference it. */
4914
4915 static void
4916 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
4917 {
4918 struct objfile *objfile = cu->objfile;
4919 struct type *type, *range_type, *index_type, *char_type;
4920 struct attribute *attr;
4921 unsigned int length;
4922
4923 if (die->type)
4924 {
4925 return;
4926 }
4927
4928 attr = dwarf2_attr (die, DW_AT_string_length, cu);
4929 if (attr)
4930 {
4931 length = DW_UNSND (attr);
4932 }
4933 else
4934 {
4935 /* check for the DW_AT_byte_size attribute */
4936 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4937 if (attr)
4938 {
4939 length = DW_UNSND (attr);
4940 }
4941 else
4942 {
4943 length = 1;
4944 }
4945 }
4946
4947 index_type = builtin_type_int32;
4948 range_type = create_range_type (NULL, index_type, 1, length);
4949 type = create_string_type (NULL, range_type);
4950
4951 set_die_type (die, type, cu);
4952 }
4953
4954 /* Handle DIES due to C code like:
4955
4956 struct foo
4957 {
4958 int (*funcp)(int a, long l);
4959 int b;
4960 };
4961
4962 ('funcp' generates a DW_TAG_subroutine_type DIE)
4963 */
4964
4965 static void
4966 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
4967 {
4968 struct type *type; /* Type that this function returns */
4969 struct type *ftype; /* Function that returns above type */
4970 struct attribute *attr;
4971
4972 /* Decode the type that this subroutine returns */
4973 if (die->type)
4974 {
4975 return;
4976 }
4977 type = die_type (die, cu);
4978 ftype = make_function_type (type, (struct type **) 0);
4979
4980 /* All functions in C++, Pascal and Java have prototypes. */
4981 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
4982 if ((attr && (DW_UNSND (attr) != 0))
4983 || cu->language == language_cplus
4984 || cu->language == language_java
4985 || cu->language == language_pascal)
4986 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
4987
4988 /* Store the calling convention in the type if it's available in
4989 the subroutine die. Otherwise set the calling convention to
4990 the default value DW_CC_normal. */
4991 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
4992 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
4993
4994 if (die->child != NULL)
4995 {
4996 struct die_info *child_die;
4997 int nparams = 0;
4998 int iparams = 0;
4999
5000 /* Count the number of parameters.
5001 FIXME: GDB currently ignores vararg functions, but knows about
5002 vararg member functions. */
5003 child_die = die->child;
5004 while (child_die && child_die->tag)
5005 {
5006 if (child_die->tag == DW_TAG_formal_parameter)
5007 nparams++;
5008 else if (child_die->tag == DW_TAG_unspecified_parameters)
5009 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
5010 child_die = sibling_die (child_die);
5011 }
5012
5013 /* Allocate storage for parameters and fill them in. */
5014 TYPE_NFIELDS (ftype) = nparams;
5015 TYPE_FIELDS (ftype) = (struct field *)
5016 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
5017
5018 child_die = die->child;
5019 while (child_die && child_die->tag)
5020 {
5021 if (child_die->tag == DW_TAG_formal_parameter)
5022 {
5023 /* Dwarf2 has no clean way to discern C++ static and non-static
5024 member functions. G++ helps GDB by marking the first
5025 parameter for non-static member functions (which is the
5026 this pointer) as artificial. We pass this information
5027 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
5028 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
5029 if (attr)
5030 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5031 else
5032 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
5033 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
5034 iparams++;
5035 }
5036 child_die = sibling_die (child_die);
5037 }
5038 }
5039
5040 set_die_type (die, ftype, cu);
5041 }
5042
5043 static void
5044 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
5045 {
5046 struct objfile *objfile = cu->objfile;
5047 struct attribute *attr;
5048 char *name = NULL;
5049
5050 if (!die->type)
5051 {
5052 name = dwarf2_name (die, cu);
5053 set_die_type (die, init_type (TYPE_CODE_TYPEDEF, 0,
5054 TYPE_FLAG_TARGET_STUB, name, objfile),
5055 cu);
5056 TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
5057 }
5058 }
5059
5060 /* Find a representation of a given base type and install
5061 it in the TYPE field of the die. */
5062
5063 static void
5064 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
5065 {
5066 struct objfile *objfile = cu->objfile;
5067 struct type *type;
5068 struct attribute *attr;
5069 int encoding = 0, size = 0;
5070 char *name;
5071 enum type_code code = TYPE_CODE_INT;
5072 int type_flags = 0;
5073 struct type *target_type = NULL;
5074
5075 /* If we've already decoded this die, this is a no-op. */
5076 if (die->type)
5077 {
5078 return;
5079 }
5080
5081 attr = dwarf2_attr (die, DW_AT_encoding, cu);
5082 if (attr)
5083 {
5084 encoding = DW_UNSND (attr);
5085 }
5086 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5087 if (attr)
5088 {
5089 size = DW_UNSND (attr);
5090 }
5091 name = dwarf2_name (die, cu);
5092 if (!name)
5093 {
5094 complaint (&symfile_complaints,
5095 _("DW_AT_name missing from DW_TAG_base_type"));
5096 }
5097
5098 switch (encoding)
5099 {
5100 case DW_ATE_address:
5101 /* Turn DW_ATE_address into a void * pointer. */
5102 code = TYPE_CODE_PTR;
5103 type_flags |= TYPE_FLAG_UNSIGNED;
5104 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
5105 break;
5106 case DW_ATE_boolean:
5107 code = TYPE_CODE_BOOL;
5108 type_flags |= TYPE_FLAG_UNSIGNED;
5109 break;
5110 case DW_ATE_complex_float:
5111 code = TYPE_CODE_COMPLEX;
5112 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5113 break;
5114 case DW_ATE_decimal_float:
5115 code = TYPE_CODE_DECFLOAT;
5116 break;
5117 case DW_ATE_float:
5118 code = TYPE_CODE_FLT;
5119 break;
5120 case DW_ATE_signed:
5121 break;
5122 case DW_ATE_unsigned:
5123 type_flags |= TYPE_FLAG_UNSIGNED;
5124 break;
5125 case DW_ATE_signed_char:
5126 if (cu->language == language_ada || cu->language == language_m2)
5127 code = TYPE_CODE_CHAR;
5128 break;
5129 case DW_ATE_unsigned_char:
5130 if (cu->language == language_ada || cu->language == language_m2)
5131 code = TYPE_CODE_CHAR;
5132 type_flags |= TYPE_FLAG_UNSIGNED;
5133 break;
5134 default:
5135 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5136 dwarf_type_encoding_name (encoding));
5137 break;
5138 }
5139
5140 type = init_type (code, size, type_flags, name, objfile);
5141 TYPE_TARGET_TYPE (type) = target_type;
5142
5143 set_die_type (die, type, cu);
5144 }
5145
5146 /* Read the given DW_AT_subrange DIE. */
5147
5148 static void
5149 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5150 {
5151 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
5152 struct type *base_type;
5153 struct type *range_type;
5154 struct attribute *attr;
5155 int low = 0;
5156 int high = -1;
5157 char *name;
5158
5159 /* If we have already decoded this die, then nothing more to do. */
5160 if (die->type)
5161 return;
5162
5163 base_type = die_type (die, cu);
5164 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
5165 {
5166 complaint (&symfile_complaints,
5167 _("DW_AT_type missing from DW_TAG_subrange_type"));
5168 base_type
5169 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
5170 0, NULL, cu->objfile);
5171 }
5172
5173 if (cu->language == language_fortran)
5174 {
5175 /* FORTRAN implies a lower bound of 1, if not given. */
5176 low = 1;
5177 }
5178
5179 /* FIXME: For variable sized arrays either of these could be
5180 a variable rather than a constant value. We'll allow it,
5181 but we don't know how to handle it. */
5182 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
5183 if (attr)
5184 low = dwarf2_get_attr_constant_value (attr, 0);
5185
5186 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
5187 if (attr)
5188 {
5189 if (attr->form == DW_FORM_block1)
5190 {
5191 /* GCC encodes arrays with unspecified or dynamic length
5192 with a DW_FORM_block1 attribute.
5193 FIXME: GDB does not yet know how to handle dynamic
5194 arrays properly, treat them as arrays with unspecified
5195 length for now.
5196
5197 FIXME: jimb/2003-09-22: GDB does not really know
5198 how to handle arrays of unspecified length
5199 either; we just represent them as zero-length
5200 arrays. Choose an appropriate upper bound given
5201 the lower bound we've computed above. */
5202 high = low - 1;
5203 }
5204 else
5205 high = dwarf2_get_attr_constant_value (attr, 1);
5206 }
5207
5208 range_type = create_range_type (NULL, base_type, low, high);
5209
5210 name = dwarf2_name (die, cu);
5211 if (name)
5212 TYPE_NAME (range_type) = name;
5213
5214 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5215 if (attr)
5216 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5217
5218 set_die_type (die, range_type, cu);
5219 }
5220
5221 static void
5222 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5223 {
5224 struct type *type;
5225
5226 if (die->type)
5227 return;
5228
5229 /* For now, we only support the C meaning of an unspecified type: void. */
5230
5231 type = init_type (TYPE_CODE_VOID, 0, 0, dwarf2_name (die, cu),
5232 cu->objfile);
5233
5234 set_die_type (die, type, cu);
5235 }
5236
5237 /* Read a whole compilation unit into a linked list of dies. */
5238
5239 static struct die_info *
5240 read_comp_unit (gdb_byte *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
5241 {
5242 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
5243 }
5244
5245 /* Read a single die and all its descendents. Set the die's sibling
5246 field to NULL; set other fields in the die correctly, and set all
5247 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
5248 location of the info_ptr after reading all of those dies. PARENT
5249 is the parent of the die in question. */
5250
5251 static struct die_info *
5252 read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
5253 struct dwarf2_cu *cu,
5254 gdb_byte **new_info_ptr,
5255 struct die_info *parent)
5256 {
5257 struct die_info *die;
5258 gdb_byte *cur_ptr;
5259 int has_children;
5260
5261 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
5262 store_in_ref_table (die->offset, die, cu);
5263
5264 if (has_children)
5265 {
5266 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
5267 new_info_ptr, die);
5268 }
5269 else
5270 {
5271 die->child = NULL;
5272 *new_info_ptr = cur_ptr;
5273 }
5274
5275 die->sibling = NULL;
5276 die->parent = parent;
5277 return die;
5278 }
5279
5280 /* Read a die, all of its descendents, and all of its siblings; set
5281 all of the fields of all of the dies correctly. Arguments are as
5282 in read_die_and_children. */
5283
5284 static struct die_info *
5285 read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
5286 struct dwarf2_cu *cu,
5287 gdb_byte **new_info_ptr,
5288 struct die_info *parent)
5289 {
5290 struct die_info *first_die, *last_sibling;
5291 gdb_byte *cur_ptr;
5292
5293 cur_ptr = info_ptr;
5294 first_die = last_sibling = NULL;
5295
5296 while (1)
5297 {
5298 struct die_info *die
5299 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
5300
5301 if (!first_die)
5302 {
5303 first_die = die;
5304 }
5305 else
5306 {
5307 last_sibling->sibling = die;
5308 }
5309
5310 if (die->tag == 0)
5311 {
5312 *new_info_ptr = cur_ptr;
5313 return first_die;
5314 }
5315 else
5316 {
5317 last_sibling = die;
5318 }
5319 }
5320 }
5321
5322 /* Free a linked list of dies. */
5323
5324 static void
5325 free_die_list (struct die_info *dies)
5326 {
5327 struct die_info *die, *next;
5328
5329 die = dies;
5330 while (die)
5331 {
5332 if (die->child != NULL)
5333 free_die_list (die->child);
5334 next = die->sibling;
5335 xfree (die->attrs);
5336 xfree (die);
5337 die = next;
5338 }
5339 }
5340
5341 /* Decompress a section that was compressed using zlib. Store the
5342 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
5343
5344 static void
5345 zlib_decompress_section (struct objfile *objfile, asection *sectp,
5346 gdb_byte **outbuf, bfd_size_type *outsize)
5347 {
5348 bfd *abfd = objfile->obfd;
5349 #ifndef HAVE_ZLIB_H
5350 error (_("Support for zlib-compressed DWARF data (from '%s') "
5351 "is disabled in this copy of GDB"),
5352 bfd_get_filename (abfd));
5353 #else
5354 bfd_size_type compressed_size = bfd_get_section_size (sectp);
5355 gdb_byte *compressed_buffer = xmalloc (compressed_size);
5356 bfd_size_type uncompressed_size;
5357 gdb_byte *uncompressed_buffer;
5358 z_stream strm;
5359 int rc;
5360 int header_size = 12;
5361
5362 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5363 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
5364 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
5365 bfd_get_filename (abfd));
5366
5367 /* Read the zlib header. In this case, it should be "ZLIB" followed
5368 by the uncompressed section size, 8 bytes in big-endian order. */
5369 if (compressed_size < header_size
5370 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
5371 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
5372 bfd_get_filename (abfd));
5373 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
5374 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
5375 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
5376 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
5377 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
5378 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
5379 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
5380 uncompressed_size += compressed_buffer[11];
5381
5382 /* It is possible the section consists of several compressed
5383 buffers concatenated together, so we uncompress in a loop. */
5384 strm.zalloc = NULL;
5385 strm.zfree = NULL;
5386 strm.opaque = NULL;
5387 strm.avail_in = compressed_size - header_size;
5388 strm.next_in = (Bytef*) compressed_buffer + header_size;
5389 strm.avail_out = uncompressed_size;
5390 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
5391 uncompressed_size);
5392 rc = inflateInit (&strm);
5393 while (strm.avail_in > 0)
5394 {
5395 if (rc != Z_OK)
5396 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
5397 bfd_get_filename (abfd), rc);
5398 strm.next_out = ((Bytef*) uncompressed_buffer
5399 + (uncompressed_size - strm.avail_out));
5400 rc = inflate (&strm, Z_FINISH);
5401 if (rc != Z_STREAM_END)
5402 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
5403 bfd_get_filename (abfd), rc);
5404 rc = inflateReset (&strm);
5405 }
5406 rc = inflateEnd (&strm);
5407 if (rc != Z_OK
5408 || strm.avail_out != 0)
5409 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
5410 bfd_get_filename (abfd), rc);
5411
5412 xfree (compressed_buffer);
5413 *outbuf = uncompressed_buffer;
5414 *outsize = uncompressed_size;
5415 #endif
5416 }
5417
5418
5419 /* Read the contents of the section at OFFSET and of size SIZE from the
5420 object file specified by OBJFILE into the objfile_obstack and return it.
5421 If the section is compressed, uncompress it before returning. */
5422
5423 gdb_byte *
5424 dwarf2_read_section (struct objfile *objfile, asection *sectp)
5425 {
5426 bfd *abfd = objfile->obfd;
5427 gdb_byte *buf, *retbuf;
5428 bfd_size_type size = bfd_get_section_size (sectp);
5429 unsigned char header[4];
5430
5431 if (size == 0)
5432 return NULL;
5433
5434 /* Check if the file has a 4-byte header indicating compression. */
5435 if (size > sizeof (header)
5436 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
5437 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
5438 {
5439 /* Upon decompression, update the buffer and its size. */
5440 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
5441 {
5442 zlib_decompress_section (objfile, sectp, &buf, &size);
5443 dwarf2_resize_section (sectp, size);
5444 return buf;
5445 }
5446 }
5447
5448 /* If we get here, we are a normal, not-compressed section. */
5449 buf = obstack_alloc (&objfile->objfile_obstack, size);
5450 /* When debugging .o files, we may need to apply relocations; see
5451 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
5452 We never compress sections in .o files, so we only need to
5453 try this when the section is not compressed. */
5454 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
5455 if (retbuf != NULL)
5456 return retbuf;
5457
5458 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5459 || bfd_bread (buf, size, abfd) != size)
5460 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
5461 bfd_get_filename (abfd));
5462
5463 return buf;
5464 }
5465
5466 /* In DWARF version 2, the description of the debugging information is
5467 stored in a separate .debug_abbrev section. Before we read any
5468 dies from a section we read in all abbreviations and install them
5469 in a hash table. This function also sets flags in CU describing
5470 the data found in the abbrev table. */
5471
5472 static void
5473 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
5474 {
5475 struct comp_unit_head *cu_header = &cu->header;
5476 gdb_byte *abbrev_ptr;
5477 struct abbrev_info *cur_abbrev;
5478 unsigned int abbrev_number, bytes_read, abbrev_name;
5479 unsigned int abbrev_form, hash_number;
5480 struct attr_abbrev *cur_attrs;
5481 unsigned int allocated_attrs;
5482
5483 /* Initialize dwarf2 abbrevs */
5484 obstack_init (&cu->abbrev_obstack);
5485 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
5486 (ABBREV_HASH_SIZE
5487 * sizeof (struct abbrev_info *)));
5488 memset (cu->dwarf2_abbrevs, 0,
5489 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
5490
5491 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
5492 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5493 abbrev_ptr += bytes_read;
5494
5495 allocated_attrs = ATTR_ALLOC_CHUNK;
5496 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
5497
5498 /* loop until we reach an abbrev number of 0 */
5499 while (abbrev_number)
5500 {
5501 cur_abbrev = dwarf_alloc_abbrev (cu);
5502
5503 /* read in abbrev header */
5504 cur_abbrev->number = abbrev_number;
5505 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5506 abbrev_ptr += bytes_read;
5507 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
5508 abbrev_ptr += 1;
5509
5510 if (cur_abbrev->tag == DW_TAG_namespace)
5511 cu->has_namespace_info = 1;
5512
5513 /* now read in declarations */
5514 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5515 abbrev_ptr += bytes_read;
5516 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5517 abbrev_ptr += bytes_read;
5518 while (abbrev_name)
5519 {
5520 if (cur_abbrev->num_attrs == allocated_attrs)
5521 {
5522 allocated_attrs += ATTR_ALLOC_CHUNK;
5523 cur_attrs
5524 = xrealloc (cur_attrs, (allocated_attrs
5525 * sizeof (struct attr_abbrev)));
5526 }
5527
5528 /* Record whether this compilation unit might have
5529 inter-compilation-unit references. If we don't know what form
5530 this attribute will have, then it might potentially be a
5531 DW_FORM_ref_addr, so we conservatively expect inter-CU
5532 references. */
5533
5534 if (abbrev_form == DW_FORM_ref_addr
5535 || abbrev_form == DW_FORM_indirect)
5536 cu->has_form_ref_addr = 1;
5537
5538 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5539 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
5540 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5541 abbrev_ptr += bytes_read;
5542 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5543 abbrev_ptr += bytes_read;
5544 }
5545
5546 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5547 (cur_abbrev->num_attrs
5548 * sizeof (struct attr_abbrev)));
5549 memcpy (cur_abbrev->attrs, cur_attrs,
5550 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5551
5552 hash_number = abbrev_number % ABBREV_HASH_SIZE;
5553 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5554 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
5555
5556 /* Get next abbreviation.
5557 Under Irix6 the abbreviations for a compilation unit are not
5558 always properly terminated with an abbrev number of 0.
5559 Exit loop if we encounter an abbreviation which we have
5560 already read (which means we are about to read the abbreviations
5561 for the next compile unit) or if the end of the abbreviation
5562 table is reached. */
5563 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
5564 >= dwarf2_per_objfile->abbrev_size)
5565 break;
5566 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5567 abbrev_ptr += bytes_read;
5568 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
5569 break;
5570 }
5571
5572 xfree (cur_attrs);
5573 }
5574
5575 /* Release the memory used by the abbrev table for a compilation unit. */
5576
5577 static void
5578 dwarf2_free_abbrev_table (void *ptr_to_cu)
5579 {
5580 struct dwarf2_cu *cu = ptr_to_cu;
5581
5582 obstack_free (&cu->abbrev_obstack, NULL);
5583 cu->dwarf2_abbrevs = NULL;
5584 }
5585
5586 /* Lookup an abbrev_info structure in the abbrev hash table. */
5587
5588 static struct abbrev_info *
5589 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
5590 {
5591 unsigned int hash_number;
5592 struct abbrev_info *abbrev;
5593
5594 hash_number = number % ABBREV_HASH_SIZE;
5595 abbrev = cu->dwarf2_abbrevs[hash_number];
5596
5597 while (abbrev)
5598 {
5599 if (abbrev->number == number)
5600 return abbrev;
5601 else
5602 abbrev = abbrev->next;
5603 }
5604 return NULL;
5605 }
5606
5607 /* Returns nonzero if TAG represents a type that we might generate a partial
5608 symbol for. */
5609
5610 static int
5611 is_type_tag_for_partial (int tag)
5612 {
5613 switch (tag)
5614 {
5615 #if 0
5616 /* Some types that would be reasonable to generate partial symbols for,
5617 that we don't at present. */
5618 case DW_TAG_array_type:
5619 case DW_TAG_file_type:
5620 case DW_TAG_ptr_to_member_type:
5621 case DW_TAG_set_type:
5622 case DW_TAG_string_type:
5623 case DW_TAG_subroutine_type:
5624 #endif
5625 case DW_TAG_base_type:
5626 case DW_TAG_class_type:
5627 case DW_TAG_interface_type:
5628 case DW_TAG_enumeration_type:
5629 case DW_TAG_structure_type:
5630 case DW_TAG_subrange_type:
5631 case DW_TAG_typedef:
5632 case DW_TAG_union_type:
5633 return 1;
5634 default:
5635 return 0;
5636 }
5637 }
5638
5639 /* Load all DIEs that are interesting for partial symbols into memory. */
5640
5641 static struct partial_die_info *
5642 load_partial_dies (bfd *abfd, gdb_byte *info_ptr, int building_psymtab,
5643 struct dwarf2_cu *cu)
5644 {
5645 struct partial_die_info *part_die;
5646 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5647 struct abbrev_info *abbrev;
5648 unsigned int bytes_read;
5649 unsigned int load_all = 0;
5650
5651 int nesting_level = 1;
5652
5653 parent_die = NULL;
5654 last_die = NULL;
5655
5656 if (cu->per_cu && cu->per_cu->load_all_dies)
5657 load_all = 1;
5658
5659 cu->partial_dies
5660 = htab_create_alloc_ex (cu->header.length / 12,
5661 partial_die_hash,
5662 partial_die_eq,
5663 NULL,
5664 &cu->comp_unit_obstack,
5665 hashtab_obstack_allocate,
5666 dummy_obstack_deallocate);
5667
5668 part_die = obstack_alloc (&cu->comp_unit_obstack,
5669 sizeof (struct partial_die_info));
5670
5671 while (1)
5672 {
5673 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5674
5675 /* A NULL abbrev means the end of a series of children. */
5676 if (abbrev == NULL)
5677 {
5678 if (--nesting_level == 0)
5679 {
5680 /* PART_DIE was probably the last thing allocated on the
5681 comp_unit_obstack, so we could call obstack_free
5682 here. We don't do that because the waste is small,
5683 and will be cleaned up when we're done with this
5684 compilation unit. This way, we're also more robust
5685 against other users of the comp_unit_obstack. */
5686 return first_die;
5687 }
5688 info_ptr += bytes_read;
5689 last_die = parent_die;
5690 parent_die = parent_die->die_parent;
5691 continue;
5692 }
5693
5694 /* Check whether this DIE is interesting enough to save. Normally
5695 we would not be interested in members here, but there may be
5696 later variables referencing them via DW_AT_specification (for
5697 static members). */
5698 if (!load_all
5699 && !is_type_tag_for_partial (abbrev->tag)
5700 && abbrev->tag != DW_TAG_enumerator
5701 && abbrev->tag != DW_TAG_subprogram
5702 && abbrev->tag != DW_TAG_variable
5703 && abbrev->tag != DW_TAG_namespace
5704 && abbrev->tag != DW_TAG_member)
5705 {
5706 /* Otherwise we skip to the next sibling, if any. */
5707 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5708 continue;
5709 }
5710
5711 info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5712 abfd, info_ptr, cu);
5713
5714 /* This two-pass algorithm for processing partial symbols has a
5715 high cost in cache pressure. Thus, handle some simple cases
5716 here which cover the majority of C partial symbols. DIEs
5717 which neither have specification tags in them, nor could have
5718 specification tags elsewhere pointing at them, can simply be
5719 processed and discarded.
5720
5721 This segment is also optional; scan_partial_symbols and
5722 add_partial_symbol will handle these DIEs if we chain
5723 them in normally. When compilers which do not emit large
5724 quantities of duplicate debug information are more common,
5725 this code can probably be removed. */
5726
5727 /* Any complete simple types at the top level (pretty much all
5728 of them, for a language without namespaces), can be processed
5729 directly. */
5730 if (parent_die == NULL
5731 && part_die->has_specification == 0
5732 && part_die->is_declaration == 0
5733 && (part_die->tag == DW_TAG_typedef
5734 || part_die->tag == DW_TAG_base_type
5735 || part_die->tag == DW_TAG_subrange_type))
5736 {
5737 if (building_psymtab && part_die->name != NULL)
5738 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5739 VAR_DOMAIN, LOC_TYPEDEF,
5740 &cu->objfile->static_psymbols,
5741 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5742 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5743 continue;
5744 }
5745
5746 /* If we're at the second level, and we're an enumerator, and
5747 our parent has no specification (meaning possibly lives in a
5748 namespace elsewhere), then we can add the partial symbol now
5749 instead of queueing it. */
5750 if (part_die->tag == DW_TAG_enumerator
5751 && parent_die != NULL
5752 && parent_die->die_parent == NULL
5753 && parent_die->tag == DW_TAG_enumeration_type
5754 && parent_die->has_specification == 0)
5755 {
5756 if (part_die->name == NULL)
5757 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
5758 else if (building_psymtab)
5759 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5760 VAR_DOMAIN, LOC_CONST,
5761 (cu->language == language_cplus
5762 || cu->language == language_java)
5763 ? &cu->objfile->global_psymbols
5764 : &cu->objfile->static_psymbols,
5765 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5766
5767 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5768 continue;
5769 }
5770
5771 /* We'll save this DIE so link it in. */
5772 part_die->die_parent = parent_die;
5773 part_die->die_sibling = NULL;
5774 part_die->die_child = NULL;
5775
5776 if (last_die && last_die == parent_die)
5777 last_die->die_child = part_die;
5778 else if (last_die)
5779 last_die->die_sibling = part_die;
5780
5781 last_die = part_die;
5782
5783 if (first_die == NULL)
5784 first_die = part_die;
5785
5786 /* Maybe add the DIE to the hash table. Not all DIEs that we
5787 find interesting need to be in the hash table, because we
5788 also have the parent/sibling/child chains; only those that we
5789 might refer to by offset later during partial symbol reading.
5790
5791 For now this means things that might have be the target of a
5792 DW_AT_specification, DW_AT_abstract_origin, or
5793 DW_AT_extension. DW_AT_extension will refer only to
5794 namespaces; DW_AT_abstract_origin refers to functions (and
5795 many things under the function DIE, but we do not recurse
5796 into function DIEs during partial symbol reading) and
5797 possibly variables as well; DW_AT_specification refers to
5798 declarations. Declarations ought to have the DW_AT_declaration
5799 flag. It happens that GCC forgets to put it in sometimes, but
5800 only for functions, not for types.
5801
5802 Adding more things than necessary to the hash table is harmless
5803 except for the performance cost. Adding too few will result in
5804 wasted time in find_partial_die, when we reread the compilation
5805 unit with load_all_dies set. */
5806
5807 if (load_all
5808 || abbrev->tag == DW_TAG_subprogram
5809 || abbrev->tag == DW_TAG_variable
5810 || abbrev->tag == DW_TAG_namespace
5811 || part_die->is_declaration)
5812 {
5813 void **slot;
5814
5815 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5816 part_die->offset, INSERT);
5817 *slot = part_die;
5818 }
5819
5820 part_die = obstack_alloc (&cu->comp_unit_obstack,
5821 sizeof (struct partial_die_info));
5822
5823 /* For some DIEs we want to follow their children (if any). For C
5824 we have no reason to follow the children of structures; for other
5825 languages we have to, both so that we can get at method physnames
5826 to infer fully qualified class names, and for DW_AT_specification. */
5827 if (last_die->has_children
5828 && (load_all
5829 || last_die->tag == DW_TAG_namespace
5830 || last_die->tag == DW_TAG_enumeration_type
5831 || (cu->language != language_c
5832 && (last_die->tag == DW_TAG_class_type
5833 || last_die->tag == DW_TAG_interface_type
5834 || last_die->tag == DW_TAG_structure_type
5835 || last_die->tag == DW_TAG_union_type))))
5836 {
5837 nesting_level++;
5838 parent_die = last_die;
5839 continue;
5840 }
5841
5842 /* Otherwise we skip to the next sibling, if any. */
5843 info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5844
5845 /* Back to the top, do it again. */
5846 }
5847 }
5848
5849 /* Read a minimal amount of information into the minimal die structure. */
5850
5851 static gdb_byte *
5852 read_partial_die (struct partial_die_info *part_die,
5853 struct abbrev_info *abbrev,
5854 unsigned int abbrev_len, bfd *abfd,
5855 gdb_byte *info_ptr, struct dwarf2_cu *cu)
5856 {
5857 unsigned int bytes_read, i;
5858 struct attribute attr;
5859 int has_low_pc_attr = 0;
5860 int has_high_pc_attr = 0;
5861 CORE_ADDR base_address;
5862 enum
5863 {
5864 base_address_none,
5865 base_address_low_pc,
5866 /* Overrides BASE_ADDRESS_LOW_PC. */
5867 base_address_entry_pc
5868 }
5869 base_address_type = base_address_none;
5870
5871 memset (part_die, 0, sizeof (struct partial_die_info));
5872
5873 part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
5874
5875 info_ptr += abbrev_len;
5876
5877 if (abbrev == NULL)
5878 return info_ptr;
5879
5880 part_die->tag = abbrev->tag;
5881 part_die->has_children = abbrev->has_children;
5882
5883 for (i = 0; i < abbrev->num_attrs; ++i)
5884 {
5885 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
5886
5887 /* Store the data if it is of an attribute we want to keep in a
5888 partial symbol table. */
5889 switch (attr.name)
5890 {
5891 case DW_AT_name:
5892
5893 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
5894 if (part_die->name == NULL)
5895 part_die->name = DW_STRING (&attr);
5896 break;
5897 case DW_AT_comp_dir:
5898 if (part_die->dirname == NULL)
5899 part_die->dirname = DW_STRING (&attr);
5900 break;
5901 case DW_AT_MIPS_linkage_name:
5902 part_die->name = DW_STRING (&attr);
5903 break;
5904 case DW_AT_low_pc:
5905 has_low_pc_attr = 1;
5906 part_die->lowpc = DW_ADDR (&attr);
5907 if (part_die->tag == DW_TAG_compile_unit
5908 && base_address_type < base_address_low_pc)
5909 {
5910 base_address = DW_ADDR (&attr);
5911 base_address_type = base_address_low_pc;
5912 }
5913 break;
5914 case DW_AT_high_pc:
5915 has_high_pc_attr = 1;
5916 part_die->highpc = DW_ADDR (&attr);
5917 break;
5918 case DW_AT_entry_pc:
5919 if (part_die->tag == DW_TAG_compile_unit
5920 && base_address_type < base_address_entry_pc)
5921 {
5922 base_address = DW_ADDR (&attr);
5923 base_address_type = base_address_entry_pc;
5924 }
5925 break;
5926 case DW_AT_ranges:
5927 if (part_die->tag == DW_TAG_compile_unit)
5928 {
5929 cu->ranges_offset = DW_UNSND (&attr);
5930 cu->has_ranges_offset = 1;
5931 }
5932 break;
5933 case DW_AT_location:
5934 /* Support the .debug_loc offsets */
5935 if (attr_form_is_block (&attr))
5936 {
5937 part_die->locdesc = DW_BLOCK (&attr);
5938 }
5939 else if (attr_form_is_section_offset (&attr))
5940 {
5941 dwarf2_complex_location_expr_complaint ();
5942 }
5943 else
5944 {
5945 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5946 "partial symbol information");
5947 }
5948 break;
5949 case DW_AT_language:
5950 part_die->language = DW_UNSND (&attr);
5951 break;
5952 case DW_AT_external:
5953 part_die->is_external = DW_UNSND (&attr);
5954 break;
5955 case DW_AT_declaration:
5956 part_die->is_declaration = DW_UNSND (&attr);
5957 break;
5958 case DW_AT_type:
5959 part_die->has_type = 1;
5960 break;
5961 case DW_AT_abstract_origin:
5962 case DW_AT_specification:
5963 case DW_AT_extension:
5964 part_die->has_specification = 1;
5965 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr, cu);
5966 break;
5967 case DW_AT_sibling:
5968 /* Ignore absolute siblings, they might point outside of
5969 the current compile unit. */
5970 if (attr.form == DW_FORM_ref_addr)
5971 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
5972 else
5973 part_die->sibling = dwarf2_per_objfile->info_buffer
5974 + dwarf2_get_ref_die_offset (&attr, cu);
5975 break;
5976 case DW_AT_stmt_list:
5977 part_die->has_stmt_list = 1;
5978 part_die->line_offset = DW_UNSND (&attr);
5979 break;
5980 case DW_AT_byte_size:
5981 part_die->has_byte_size = 1;
5982 break;
5983 case DW_AT_calling_convention:
5984 /* DWARF doesn't provide a way to identify a program's source-level
5985 entry point. DW_AT_calling_convention attributes are only meant
5986 to describe functions' calling conventions.
5987
5988 However, because it's a necessary piece of information in
5989 Fortran, and because DW_CC_program is the only piece of debugging
5990 information whose definition refers to a 'main program' at all,
5991 several compilers have begun marking Fortran main programs with
5992 DW_CC_program --- even when those functions use the standard
5993 calling conventions.
5994
5995 So until DWARF specifies a way to provide this information and
5996 compilers pick up the new representation, we'll support this
5997 practice. */
5998 if (DW_UNSND (&attr) == DW_CC_program
5999 && cu->language == language_fortran)
6000 set_main_name (part_die->name);
6001 break;
6002 default:
6003 break;
6004 }
6005 }
6006
6007 /* When using the GNU linker, .gnu.linkonce. sections are used to
6008 eliminate duplicate copies of functions and vtables and such.
6009 The linker will arbitrarily choose one and discard the others.
6010 The AT_*_pc values for such functions refer to local labels in
6011 these sections. If the section from that file was discarded, the
6012 labels are not in the output, so the relocs get a value of 0.
6013 If this is a discarded function, mark the pc bounds as invalid,
6014 so that GDB will ignore it. */
6015 if (has_low_pc_attr && has_high_pc_attr
6016 && part_die->lowpc < part_die->highpc
6017 && (part_die->lowpc != 0
6018 || dwarf2_per_objfile->has_section_at_zero))
6019 part_die->has_pc_info = 1;
6020
6021 if (base_address_type != base_address_none && !cu->header.base_known)
6022 {
6023 gdb_assert (part_die->tag == DW_TAG_compile_unit);
6024 cu->header.base_known = 1;
6025 cu->header.base_address = base_address;
6026 }
6027
6028 return info_ptr;
6029 }
6030
6031 /* Find a cached partial DIE at OFFSET in CU. */
6032
6033 static struct partial_die_info *
6034 find_partial_die_in_comp_unit (unsigned long offset, struct dwarf2_cu *cu)
6035 {
6036 struct partial_die_info *lookup_die = NULL;
6037 struct partial_die_info part_die;
6038
6039 part_die.offset = offset;
6040 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6041
6042 return lookup_die;
6043 }
6044
6045 /* Find a partial DIE at OFFSET, which may or may not be in CU. */
6046
6047 static struct partial_die_info *
6048 find_partial_die (unsigned long offset, struct dwarf2_cu *cu)
6049 {
6050 struct dwarf2_per_cu_data *per_cu = NULL;
6051 struct partial_die_info *pd = NULL;
6052
6053 if (offset >= cu->header.offset
6054 && offset < cu->header.offset + cu->header.length)
6055 {
6056 pd = find_partial_die_in_comp_unit (offset, cu);
6057 if (pd != NULL)
6058 return pd;
6059 }
6060
6061 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6062
6063 if (per_cu->cu == NULL)
6064 {
6065 load_comp_unit (per_cu, cu->objfile);
6066 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6067 dwarf2_per_objfile->read_in_chain = per_cu;
6068 }
6069
6070 per_cu->cu->last_used = 0;
6071 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6072
6073 if (pd == NULL && per_cu->load_all_dies == 0)
6074 {
6075 struct cleanup *back_to;
6076 struct partial_die_info comp_unit_die;
6077 struct abbrev_info *abbrev;
6078 unsigned int bytes_read;
6079 char *info_ptr;
6080
6081 per_cu->load_all_dies = 1;
6082
6083 /* Re-read the DIEs. */
6084 back_to = make_cleanup (null_cleanup, 0);
6085 if (per_cu->cu->dwarf2_abbrevs == NULL)
6086 {
6087 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6088 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6089 }
6090 info_ptr = per_cu->cu->header.first_die_ptr;
6091 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6092 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6093 per_cu->cu->objfile->obfd, info_ptr,
6094 per_cu->cu);
6095 if (comp_unit_die.has_children)
6096 load_partial_dies (per_cu->cu->objfile->obfd, info_ptr, 0, per_cu->cu);
6097 do_cleanups (back_to);
6098
6099 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6100 }
6101
6102 if (pd == NULL)
6103 internal_error (__FILE__, __LINE__,
6104 _("could not find partial DIE 0x%lx in cache [from module %s]\n"),
6105 offset, bfd_get_filename (cu->objfile->obfd));
6106 return pd;
6107 }
6108
6109 /* Adjust PART_DIE before generating a symbol for it. This function
6110 may set the is_external flag or change the DIE's name. */
6111
6112 static void
6113 fixup_partial_die (struct partial_die_info *part_die,
6114 struct dwarf2_cu *cu)
6115 {
6116 /* If we found a reference attribute and the DIE has no name, try
6117 to find a name in the referred to DIE. */
6118
6119 if (part_die->name == NULL && part_die->has_specification)
6120 {
6121 struct partial_die_info *spec_die;
6122
6123 spec_die = find_partial_die (part_die->spec_offset, cu);
6124
6125 fixup_partial_die (spec_die, cu);
6126
6127 if (spec_die->name)
6128 {
6129 part_die->name = spec_die->name;
6130
6131 /* Copy DW_AT_external attribute if it is set. */
6132 if (spec_die->is_external)
6133 part_die->is_external = spec_die->is_external;
6134 }
6135 }
6136
6137 /* Set default names for some unnamed DIEs. */
6138 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6139 || part_die->tag == DW_TAG_class_type))
6140 part_die->name = "(anonymous class)";
6141
6142 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6143 part_die->name = "(anonymous namespace)";
6144
6145 if (part_die->tag == DW_TAG_structure_type
6146 || part_die->tag == DW_TAG_class_type
6147 || part_die->tag == DW_TAG_union_type)
6148 guess_structure_name (part_die, cu);
6149 }
6150
6151 /* Read the die from the .debug_info section buffer. Set DIEP to
6152 point to a newly allocated die with its information, except for its
6153 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6154 whether the die has children or not. */
6155
6156 static gdb_byte *
6157 read_full_die (struct die_info **diep, bfd *abfd, gdb_byte *info_ptr,
6158 struct dwarf2_cu *cu, int *has_children)
6159 {
6160 unsigned int abbrev_number, bytes_read, i, offset;
6161 struct abbrev_info *abbrev;
6162 struct die_info *die;
6163
6164 offset = info_ptr - dwarf2_per_objfile->info_buffer;
6165 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6166 info_ptr += bytes_read;
6167 if (!abbrev_number)
6168 {
6169 die = dwarf_alloc_die ();
6170 die->tag = 0;
6171 die->abbrev = abbrev_number;
6172 die->type = NULL;
6173 *diep = die;
6174 *has_children = 0;
6175 return info_ptr;
6176 }
6177
6178 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6179 if (!abbrev)
6180 {
6181 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6182 abbrev_number,
6183 bfd_get_filename (abfd));
6184 }
6185 die = dwarf_alloc_die ();
6186 die->offset = offset;
6187 die->tag = abbrev->tag;
6188 die->abbrev = abbrev_number;
6189 die->type = NULL;
6190
6191 die->num_attrs = abbrev->num_attrs;
6192 die->attrs = (struct attribute *)
6193 xmalloc (die->num_attrs * sizeof (struct attribute));
6194
6195 for (i = 0; i < abbrev->num_attrs; ++i)
6196 {
6197 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6198 abfd, info_ptr, cu);
6199
6200 /* If this attribute is an absolute reference to a different
6201 compilation unit, make sure that compilation unit is loaded
6202 also. */
6203 if (die->attrs[i].form == DW_FORM_ref_addr
6204 && (DW_ADDR (&die->attrs[i]) < cu->header.offset
6205 || (DW_ADDR (&die->attrs[i])
6206 >= cu->header.offset + cu->header.length)))
6207 {
6208 struct dwarf2_per_cu_data *per_cu;
6209 per_cu = dwarf2_find_containing_comp_unit (DW_ADDR (&die->attrs[i]),
6210 cu->objfile);
6211
6212 /* Mark the dependence relation so that we don't flush PER_CU
6213 too early. */
6214 dwarf2_add_dependence (cu, per_cu);
6215
6216 /* If it's already on the queue, we have nothing to do. */
6217 if (per_cu->queued)
6218 continue;
6219
6220 /* If the compilation unit is already loaded, just mark it as
6221 used. */
6222 if (per_cu->cu != NULL)
6223 {
6224 per_cu->cu->last_used = 0;
6225 continue;
6226 }
6227
6228 /* Add it to the queue. */
6229 queue_comp_unit (per_cu);
6230 }
6231 }
6232
6233 *diep = die;
6234 *has_children = abbrev->has_children;
6235 return info_ptr;
6236 }
6237
6238 /* Read an attribute value described by an attribute form. */
6239
6240 static gdb_byte *
6241 read_attribute_value (struct attribute *attr, unsigned form,
6242 bfd *abfd, gdb_byte *info_ptr,
6243 struct dwarf2_cu *cu)
6244 {
6245 struct comp_unit_head *cu_header = &cu->header;
6246 unsigned int bytes_read;
6247 struct dwarf_block *blk;
6248
6249 attr->form = form;
6250 switch (form)
6251 {
6252 case DW_FORM_addr:
6253 case DW_FORM_ref_addr:
6254 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
6255 info_ptr += bytes_read;
6256 break;
6257 case DW_FORM_block2:
6258 blk = dwarf_alloc_block (cu);
6259 blk->size = read_2_bytes (abfd, info_ptr);
6260 info_ptr += 2;
6261 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6262 info_ptr += blk->size;
6263 DW_BLOCK (attr) = blk;
6264 break;
6265 case DW_FORM_block4:
6266 blk = dwarf_alloc_block (cu);
6267 blk->size = read_4_bytes (abfd, info_ptr);
6268 info_ptr += 4;
6269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6270 info_ptr += blk->size;
6271 DW_BLOCK (attr) = blk;
6272 break;
6273 case DW_FORM_data2:
6274 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6275 info_ptr += 2;
6276 break;
6277 case DW_FORM_data4:
6278 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6279 info_ptr += 4;
6280 break;
6281 case DW_FORM_data8:
6282 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6283 info_ptr += 8;
6284 break;
6285 case DW_FORM_string:
6286 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6287 info_ptr += bytes_read;
6288 break;
6289 case DW_FORM_strp:
6290 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6291 &bytes_read);
6292 info_ptr += bytes_read;
6293 break;
6294 case DW_FORM_block:
6295 blk = dwarf_alloc_block (cu);
6296 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6297 info_ptr += bytes_read;
6298 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6299 info_ptr += blk->size;
6300 DW_BLOCK (attr) = blk;
6301 break;
6302 case DW_FORM_block1:
6303 blk = dwarf_alloc_block (cu);
6304 blk->size = read_1_byte (abfd, info_ptr);
6305 info_ptr += 1;
6306 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6307 info_ptr += blk->size;
6308 DW_BLOCK (attr) = blk;
6309 break;
6310 case DW_FORM_data1:
6311 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6312 info_ptr += 1;
6313 break;
6314 case DW_FORM_flag:
6315 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6316 info_ptr += 1;
6317 break;
6318 case DW_FORM_sdata:
6319 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
6320 info_ptr += bytes_read;
6321 break;
6322 case DW_FORM_udata:
6323 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6324 info_ptr += bytes_read;
6325 break;
6326 case DW_FORM_ref1:
6327 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
6328 info_ptr += 1;
6329 break;
6330 case DW_FORM_ref2:
6331 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
6332 info_ptr += 2;
6333 break;
6334 case DW_FORM_ref4:
6335 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
6336 info_ptr += 4;
6337 break;
6338 case DW_FORM_ref8:
6339 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
6340 info_ptr += 8;
6341 break;
6342 case DW_FORM_ref_udata:
6343 DW_ADDR (attr) = (cu->header.offset
6344 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
6345 info_ptr += bytes_read;
6346 break;
6347 case DW_FORM_indirect:
6348 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6349 info_ptr += bytes_read;
6350 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
6351 break;
6352 default:
6353 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
6354 dwarf_form_name (form),
6355 bfd_get_filename (abfd));
6356 }
6357 return info_ptr;
6358 }
6359
6360 /* Read an attribute described by an abbreviated attribute. */
6361
6362 static gdb_byte *
6363 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
6364 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
6365 {
6366 attr->name = abbrev->name;
6367 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
6368 }
6369
6370 /* read dwarf information from a buffer */
6371
6372 static unsigned int
6373 read_1_byte (bfd *abfd, gdb_byte *buf)
6374 {
6375 return bfd_get_8 (abfd, buf);
6376 }
6377
6378 static int
6379 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
6380 {
6381 return bfd_get_signed_8 (abfd, buf);
6382 }
6383
6384 static unsigned int
6385 read_2_bytes (bfd *abfd, gdb_byte *buf)
6386 {
6387 return bfd_get_16 (abfd, buf);
6388 }
6389
6390 static int
6391 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
6392 {
6393 return bfd_get_signed_16 (abfd, buf);
6394 }
6395
6396 static unsigned int
6397 read_4_bytes (bfd *abfd, gdb_byte *buf)
6398 {
6399 return bfd_get_32 (abfd, buf);
6400 }
6401
6402 static int
6403 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
6404 {
6405 return bfd_get_signed_32 (abfd, buf);
6406 }
6407
6408 static unsigned long
6409 read_8_bytes (bfd *abfd, gdb_byte *buf)
6410 {
6411 return bfd_get_64 (abfd, buf);
6412 }
6413
6414 static CORE_ADDR
6415 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
6416 unsigned int *bytes_read)
6417 {
6418 struct comp_unit_head *cu_header = &cu->header;
6419 CORE_ADDR retval = 0;
6420
6421 if (cu_header->signed_addr_p)
6422 {
6423 switch (cu_header->addr_size)
6424 {
6425 case 2:
6426 retval = bfd_get_signed_16 (abfd, buf);
6427 break;
6428 case 4:
6429 retval = bfd_get_signed_32 (abfd, buf);
6430 break;
6431 case 8:
6432 retval = bfd_get_signed_64 (abfd, buf);
6433 break;
6434 default:
6435 internal_error (__FILE__, __LINE__,
6436 _("read_address: bad switch, signed [in module %s]"),
6437 bfd_get_filename (abfd));
6438 }
6439 }
6440 else
6441 {
6442 switch (cu_header->addr_size)
6443 {
6444 case 2:
6445 retval = bfd_get_16 (abfd, buf);
6446 break;
6447 case 4:
6448 retval = bfd_get_32 (abfd, buf);
6449 break;
6450 case 8:
6451 retval = bfd_get_64 (abfd, buf);
6452 break;
6453 default:
6454 internal_error (__FILE__, __LINE__,
6455 _("read_address: bad switch, unsigned [in module %s]"),
6456 bfd_get_filename (abfd));
6457 }
6458 }
6459
6460 *bytes_read = cu_header->addr_size;
6461 return retval;
6462 }
6463
6464 /* Read the initial length from a section. The (draft) DWARF 3
6465 specification allows the initial length to take up either 4 bytes
6466 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
6467 bytes describe the length and all offsets will be 8 bytes in length
6468 instead of 4.
6469
6470 An older, non-standard 64-bit format is also handled by this
6471 function. The older format in question stores the initial length
6472 as an 8-byte quantity without an escape value. Lengths greater
6473 than 2^32 aren't very common which means that the initial 4 bytes
6474 is almost always zero. Since a length value of zero doesn't make
6475 sense for the 32-bit format, this initial zero can be considered to
6476 be an escape value which indicates the presence of the older 64-bit
6477 format. As written, the code can't detect (old format) lengths
6478 greater than 4GB. If it becomes necessary to handle lengths
6479 somewhat larger than 4GB, we could allow other small values (such
6480 as the non-sensical values of 1, 2, and 3) to also be used as
6481 escape values indicating the presence of the old format.
6482
6483 The value returned via bytes_read should be used to increment the
6484 relevant pointer after calling read_initial_length().
6485
6486 As a side effect, this function sets the fields initial_length_size
6487 and offset_size in cu_header to the values appropriate for the
6488 length field. (The format of the initial length field determines
6489 the width of file offsets to be fetched later with read_offset().)
6490
6491 [ Note: read_initial_length() and read_offset() are based on the
6492 document entitled "DWARF Debugging Information Format", revision
6493 3, draft 8, dated November 19, 2001. This document was obtained
6494 from:
6495
6496 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6497
6498 This document is only a draft and is subject to change. (So beware.)
6499
6500 Details regarding the older, non-standard 64-bit format were
6501 determined empirically by examining 64-bit ELF files produced by
6502 the SGI toolchain on an IRIX 6.5 machine.
6503
6504 - Kevin, July 16, 2002
6505 ] */
6506
6507 static LONGEST
6508 read_initial_length (bfd *abfd, gdb_byte *buf, struct comp_unit_head *cu_header,
6509 unsigned int *bytes_read)
6510 {
6511 LONGEST length = bfd_get_32 (abfd, buf);
6512
6513 if (length == 0xffffffff)
6514 {
6515 length = bfd_get_64 (abfd, buf + 4);
6516 *bytes_read = 12;
6517 }
6518 else if (length == 0)
6519 {
6520 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
6521 length = bfd_get_64 (abfd, buf);
6522 *bytes_read = 8;
6523 }
6524 else
6525 {
6526 *bytes_read = 4;
6527 }
6528
6529 if (cu_header)
6530 {
6531 gdb_assert (cu_header->initial_length_size == 0
6532 || cu_header->initial_length_size == 4
6533 || cu_header->initial_length_size == 8
6534 || cu_header->initial_length_size == 12);
6535
6536 if (cu_header->initial_length_size != 0
6537 && cu_header->initial_length_size != *bytes_read)
6538 complaint (&symfile_complaints,
6539 _("intermixed 32-bit and 64-bit DWARF sections"));
6540
6541 cu_header->initial_length_size = *bytes_read;
6542 cu_header->offset_size = (*bytes_read == 4) ? 4 : 8;
6543 }
6544
6545 return length;
6546 }
6547
6548 /* Read an offset from the data stream. The size of the offset is
6549 given by cu_header->offset_size. */
6550
6551 static LONGEST
6552 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
6553 unsigned int *bytes_read)
6554 {
6555 LONGEST retval = 0;
6556
6557 switch (cu_header->offset_size)
6558 {
6559 case 4:
6560 retval = bfd_get_32 (abfd, buf);
6561 *bytes_read = 4;
6562 break;
6563 case 8:
6564 retval = bfd_get_64 (abfd, buf);
6565 *bytes_read = 8;
6566 break;
6567 default:
6568 internal_error (__FILE__, __LINE__,
6569 _("read_offset: bad switch [in module %s]"),
6570 bfd_get_filename (abfd));
6571 }
6572
6573 return retval;
6574 }
6575
6576 static gdb_byte *
6577 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
6578 {
6579 /* If the size of a host char is 8 bits, we can return a pointer
6580 to the buffer, otherwise we have to copy the data to a buffer
6581 allocated on the temporary obstack. */
6582 gdb_assert (HOST_CHAR_BIT == 8);
6583 return buf;
6584 }
6585
6586 static char *
6587 read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
6588 {
6589 /* If the size of a host char is 8 bits, we can return a pointer
6590 to the string, otherwise we have to copy the string to a buffer
6591 allocated on the temporary obstack. */
6592 gdb_assert (HOST_CHAR_BIT == 8);
6593 if (*buf == '\0')
6594 {
6595 *bytes_read_ptr = 1;
6596 return NULL;
6597 }
6598 *bytes_read_ptr = strlen ((char *) buf) + 1;
6599 return (char *) buf;
6600 }
6601
6602 static char *
6603 read_indirect_string (bfd *abfd, gdb_byte *buf,
6604 const struct comp_unit_head *cu_header,
6605 unsigned int *bytes_read_ptr)
6606 {
6607 LONGEST str_offset = read_offset (abfd, buf, cu_header,
6608 bytes_read_ptr);
6609
6610 if (dwarf2_per_objfile->str_buffer == NULL)
6611 {
6612 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
6613 bfd_get_filename (abfd));
6614 return NULL;
6615 }
6616 if (str_offset >= dwarf2_per_objfile->str_size)
6617 {
6618 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
6619 bfd_get_filename (abfd));
6620 return NULL;
6621 }
6622 gdb_assert (HOST_CHAR_BIT == 8);
6623 if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
6624 return NULL;
6625 return (char *) (dwarf2_per_objfile->str_buffer + str_offset);
6626 }
6627
6628 static unsigned long
6629 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
6630 {
6631 unsigned long result;
6632 unsigned int num_read;
6633 int i, shift;
6634 unsigned char byte;
6635
6636 result = 0;
6637 shift = 0;
6638 num_read = 0;
6639 i = 0;
6640 while (1)
6641 {
6642 byte = bfd_get_8 (abfd, buf);
6643 buf++;
6644 num_read++;
6645 result |= ((unsigned long)(byte & 127) << shift);
6646 if ((byte & 128) == 0)
6647 {
6648 break;
6649 }
6650 shift += 7;
6651 }
6652 *bytes_read_ptr = num_read;
6653 return result;
6654 }
6655
6656 static long
6657 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
6658 {
6659 long result;
6660 int i, shift, num_read;
6661 unsigned char byte;
6662
6663 result = 0;
6664 shift = 0;
6665 num_read = 0;
6666 i = 0;
6667 while (1)
6668 {
6669 byte = bfd_get_8 (abfd, buf);
6670 buf++;
6671 num_read++;
6672 result |= ((long)(byte & 127) << shift);
6673 shift += 7;
6674 if ((byte & 128) == 0)
6675 {
6676 break;
6677 }
6678 }
6679 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
6680 result |= -(((long)1) << shift);
6681 *bytes_read_ptr = num_read;
6682 return result;
6683 }
6684
6685 /* Return a pointer to just past the end of an LEB128 number in BUF. */
6686
6687 static gdb_byte *
6688 skip_leb128 (bfd *abfd, gdb_byte *buf)
6689 {
6690 int byte;
6691
6692 while (1)
6693 {
6694 byte = bfd_get_8 (abfd, buf);
6695 buf++;
6696 if ((byte & 128) == 0)
6697 return buf;
6698 }
6699 }
6700
6701 static void
6702 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
6703 {
6704 switch (lang)
6705 {
6706 case DW_LANG_C89:
6707 case DW_LANG_C:
6708 cu->language = language_c;
6709 break;
6710 case DW_LANG_C_plus_plus:
6711 cu->language = language_cplus;
6712 break;
6713 case DW_LANG_Fortran77:
6714 case DW_LANG_Fortran90:
6715 case DW_LANG_Fortran95:
6716 cu->language = language_fortran;
6717 break;
6718 case DW_LANG_Mips_Assembler:
6719 cu->language = language_asm;
6720 break;
6721 case DW_LANG_Java:
6722 cu->language = language_java;
6723 break;
6724 case DW_LANG_Ada83:
6725 case DW_LANG_Ada95:
6726 cu->language = language_ada;
6727 break;
6728 case DW_LANG_Modula2:
6729 cu->language = language_m2;
6730 break;
6731 case DW_LANG_Pascal83:
6732 cu->language = language_pascal;
6733 break;
6734 case DW_LANG_ObjC:
6735 cu->language = language_objc;
6736 break;
6737 case DW_LANG_Cobol74:
6738 case DW_LANG_Cobol85:
6739 default:
6740 cu->language = language_minimal;
6741 break;
6742 }
6743 cu->language_defn = language_def (cu->language);
6744 }
6745
6746 /* Return the named attribute or NULL if not there. */
6747
6748 static struct attribute *
6749 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
6750 {
6751 unsigned int i;
6752 struct attribute *spec = NULL;
6753
6754 for (i = 0; i < die->num_attrs; ++i)
6755 {
6756 if (die->attrs[i].name == name)
6757 return &die->attrs[i];
6758 if (die->attrs[i].name == DW_AT_specification
6759 || die->attrs[i].name == DW_AT_abstract_origin)
6760 spec = &die->attrs[i];
6761 }
6762
6763 if (spec)
6764 return dwarf2_attr (follow_die_ref (die, spec, cu), name, cu);
6765
6766 return NULL;
6767 }
6768
6769 /* Return non-zero iff the attribute NAME is defined for the given DIE,
6770 and holds a non-zero value. This function should only be used for
6771 DW_FORM_flag attributes. */
6772
6773 static int
6774 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
6775 {
6776 struct attribute *attr = dwarf2_attr (die, name, cu);
6777
6778 return (attr && DW_UNSND (attr));
6779 }
6780
6781 static int
6782 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
6783 {
6784 /* A DIE is a declaration if it has a DW_AT_declaration attribute
6785 which value is non-zero. However, we have to be careful with
6786 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
6787 (via dwarf2_flag_true_p) follows this attribute. So we may
6788 end up accidently finding a declaration attribute that belongs
6789 to a different DIE referenced by the specification attribute,
6790 even though the given DIE does not have a declaration attribute. */
6791 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6792 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
6793 }
6794
6795 /* Return the die giving the specification for DIE, if there is
6796 one. */
6797
6798 static struct die_info *
6799 die_specification (struct die_info *die, struct dwarf2_cu *cu)
6800 {
6801 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
6802
6803 if (spec_attr == NULL)
6804 return NULL;
6805 else
6806 return follow_die_ref (die, spec_attr, cu);
6807 }
6808
6809 /* Free the line_header structure *LH, and any arrays and strings it
6810 refers to. */
6811 static void
6812 free_line_header (struct line_header *lh)
6813 {
6814 if (lh->standard_opcode_lengths)
6815 xfree (lh->standard_opcode_lengths);
6816
6817 /* Remember that all the lh->file_names[i].name pointers are
6818 pointers into debug_line_buffer, and don't need to be freed. */
6819 if (lh->file_names)
6820 xfree (lh->file_names);
6821
6822 /* Similarly for the include directory names. */
6823 if (lh->include_dirs)
6824 xfree (lh->include_dirs);
6825
6826 xfree (lh);
6827 }
6828
6829
6830 /* Add an entry to LH's include directory table. */
6831 static void
6832 add_include_dir (struct line_header *lh, char *include_dir)
6833 {
6834 /* Grow the array if necessary. */
6835 if (lh->include_dirs_size == 0)
6836 {
6837 lh->include_dirs_size = 1; /* for testing */
6838 lh->include_dirs = xmalloc (lh->include_dirs_size
6839 * sizeof (*lh->include_dirs));
6840 }
6841 else if (lh->num_include_dirs >= lh->include_dirs_size)
6842 {
6843 lh->include_dirs_size *= 2;
6844 lh->include_dirs = xrealloc (lh->include_dirs,
6845 (lh->include_dirs_size
6846 * sizeof (*lh->include_dirs)));
6847 }
6848
6849 lh->include_dirs[lh->num_include_dirs++] = include_dir;
6850 }
6851
6852
6853 /* Add an entry to LH's file name table. */
6854 static void
6855 add_file_name (struct line_header *lh,
6856 char *name,
6857 unsigned int dir_index,
6858 unsigned int mod_time,
6859 unsigned int length)
6860 {
6861 struct file_entry *fe;
6862
6863 /* Grow the array if necessary. */
6864 if (lh->file_names_size == 0)
6865 {
6866 lh->file_names_size = 1; /* for testing */
6867 lh->file_names = xmalloc (lh->file_names_size
6868 * sizeof (*lh->file_names));
6869 }
6870 else if (lh->num_file_names >= lh->file_names_size)
6871 {
6872 lh->file_names_size *= 2;
6873 lh->file_names = xrealloc (lh->file_names,
6874 (lh->file_names_size
6875 * sizeof (*lh->file_names)));
6876 }
6877
6878 fe = &lh->file_names[lh->num_file_names++];
6879 fe->name = name;
6880 fe->dir_index = dir_index;
6881 fe->mod_time = mod_time;
6882 fe->length = length;
6883 fe->included_p = 0;
6884 fe->symtab = NULL;
6885 }
6886
6887
6888 /* Read the statement program header starting at OFFSET in
6889 .debug_line, according to the endianness of ABFD. Return a pointer
6890 to a struct line_header, allocated using xmalloc.
6891
6892 NOTE: the strings in the include directory and file name tables of
6893 the returned object point into debug_line_buffer, and must not be
6894 freed. */
6895 static struct line_header *
6896 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
6897 struct dwarf2_cu *cu)
6898 {
6899 struct cleanup *back_to;
6900 struct line_header *lh;
6901 gdb_byte *line_ptr;
6902 unsigned int bytes_read;
6903 int i;
6904 char *cur_dir, *cur_file;
6905
6906 if (dwarf2_per_objfile->line_buffer == NULL)
6907 {
6908 complaint (&symfile_complaints, _("missing .debug_line section"));
6909 return 0;
6910 }
6911
6912 /* Make sure that at least there's room for the total_length field.
6913 That could be 12 bytes long, but we're just going to fudge that. */
6914 if (offset + 4 >= dwarf2_per_objfile->line_size)
6915 {
6916 dwarf2_statement_list_fits_in_line_number_section_complaint ();
6917 return 0;
6918 }
6919
6920 lh = xmalloc (sizeof (*lh));
6921 memset (lh, 0, sizeof (*lh));
6922 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6923 (void *) lh);
6924
6925 line_ptr = dwarf2_per_objfile->line_buffer + offset;
6926
6927 /* Read in the header. */
6928 lh->total_length =
6929 read_initial_length (abfd, line_ptr, &cu->header, &bytes_read);
6930 line_ptr += bytes_read;
6931 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6932 + dwarf2_per_objfile->line_size))
6933 {
6934 dwarf2_statement_list_fits_in_line_number_section_complaint ();
6935 return 0;
6936 }
6937 lh->statement_program_end = line_ptr + lh->total_length;
6938 lh->version = read_2_bytes (abfd, line_ptr);
6939 line_ptr += 2;
6940 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
6941 line_ptr += bytes_read;
6942 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6943 line_ptr += 1;
6944 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6945 line_ptr += 1;
6946 lh->line_base = read_1_signed_byte (abfd, line_ptr);
6947 line_ptr += 1;
6948 lh->line_range = read_1_byte (abfd, line_ptr);
6949 line_ptr += 1;
6950 lh->opcode_base = read_1_byte (abfd, line_ptr);
6951 line_ptr += 1;
6952 lh->standard_opcode_lengths
6953 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
6954
6955 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
6956 for (i = 1; i < lh->opcode_base; ++i)
6957 {
6958 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
6959 line_ptr += 1;
6960 }
6961
6962 /* Read directory table. */
6963 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6964 {
6965 line_ptr += bytes_read;
6966 add_include_dir (lh, cur_dir);
6967 }
6968 line_ptr += bytes_read;
6969
6970 /* Read file name table. */
6971 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6972 {
6973 unsigned int dir_index, mod_time, length;
6974
6975 line_ptr += bytes_read;
6976 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6977 line_ptr += bytes_read;
6978 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6979 line_ptr += bytes_read;
6980 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6981 line_ptr += bytes_read;
6982
6983 add_file_name (lh, cur_file, dir_index, mod_time, length);
6984 }
6985 line_ptr += bytes_read;
6986 lh->statement_program_start = line_ptr;
6987
6988 if (line_ptr > (dwarf2_per_objfile->line_buffer
6989 + dwarf2_per_objfile->line_size))
6990 complaint (&symfile_complaints,
6991 _("line number info header doesn't fit in `.debug_line' section"));
6992
6993 discard_cleanups (back_to);
6994 return lh;
6995 }
6996
6997 /* This function exists to work around a bug in certain compilers
6998 (particularly GCC 2.95), in which the first line number marker of a
6999 function does not show up until after the prologue, right before
7000 the second line number marker. This function shifts ADDRESS down
7001 to the beginning of the function if necessary, and is called on
7002 addresses passed to record_line. */
7003
7004 static CORE_ADDR
7005 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
7006 {
7007 struct function_range *fn;
7008
7009 /* Find the function_range containing address. */
7010 if (!cu->first_fn)
7011 return address;
7012
7013 if (!cu->cached_fn)
7014 cu->cached_fn = cu->first_fn;
7015
7016 fn = cu->cached_fn;
7017 while (fn)
7018 if (fn->lowpc <= address && fn->highpc > address)
7019 goto found;
7020 else
7021 fn = fn->next;
7022
7023 fn = cu->first_fn;
7024 while (fn && fn != cu->cached_fn)
7025 if (fn->lowpc <= address && fn->highpc > address)
7026 goto found;
7027 else
7028 fn = fn->next;
7029
7030 return address;
7031
7032 found:
7033 if (fn->seen_line)
7034 return address;
7035 if (address != fn->lowpc)
7036 complaint (&symfile_complaints,
7037 _("misplaced first line number at 0x%lx for '%s'"),
7038 (unsigned long) address, fn->name);
7039 fn->seen_line = 1;
7040 return fn->lowpc;
7041 }
7042
7043 /* Decode the Line Number Program (LNP) for the given line_header
7044 structure and CU. The actual information extracted and the type
7045 of structures created from the LNP depends on the value of PST.
7046
7047 1. If PST is NULL, then this procedure uses the data from the program
7048 to create all necessary symbol tables, and their linetables.
7049 The compilation directory of the file is passed in COMP_DIR,
7050 and must not be NULL.
7051
7052 2. If PST is not NULL, this procedure reads the program to determine
7053 the list of files included by the unit represented by PST, and
7054 builds all the associated partial symbol tables. In this case,
7055 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7056 is not used to compute the full name of the symtab, and therefore
7057 omitting it when building the partial symtab does not introduce
7058 the potential for inconsistency - a partial symtab and its associated
7059 symbtab having a different fullname -). */
7060
7061 static void
7062 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
7063 struct dwarf2_cu *cu, struct partial_symtab *pst)
7064 {
7065 gdb_byte *line_ptr, *extended_end;
7066 gdb_byte *line_end;
7067 unsigned int bytes_read, extended_len;
7068 unsigned char op_code, extended_op, adj_opcode;
7069 CORE_ADDR baseaddr;
7070 struct objfile *objfile = cu->objfile;
7071 const int decode_for_pst_p = (pst != NULL);
7072 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
7073
7074 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7075
7076 line_ptr = lh->statement_program_start;
7077 line_end = lh->statement_program_end;
7078
7079 /* Read the statement sequences until there's nothing left. */
7080 while (line_ptr < line_end)
7081 {
7082 /* state machine registers */
7083 CORE_ADDR address = 0;
7084 unsigned int file = 1;
7085 unsigned int line = 1;
7086 unsigned int column = 0;
7087 int is_stmt = lh->default_is_stmt;
7088 int basic_block = 0;
7089 int end_sequence = 0;
7090
7091 if (!decode_for_pst_p && lh->num_file_names >= file)
7092 {
7093 /* Start a subfile for the current file of the state machine. */
7094 /* lh->include_dirs and lh->file_names are 0-based, but the
7095 directory and file name numbers in the statement program
7096 are 1-based. */
7097 struct file_entry *fe = &lh->file_names[file - 1];
7098 char *dir = NULL;
7099
7100 if (fe->dir_index)
7101 dir = lh->include_dirs[fe->dir_index - 1];
7102
7103 dwarf2_start_subfile (fe->name, dir, comp_dir);
7104 }
7105
7106 /* Decode the table. */
7107 while (!end_sequence)
7108 {
7109 op_code = read_1_byte (abfd, line_ptr);
7110 line_ptr += 1;
7111
7112 if (op_code >= lh->opcode_base)
7113 {
7114 /* Special operand. */
7115 adj_opcode = op_code - lh->opcode_base;
7116 address += (adj_opcode / lh->line_range)
7117 * lh->minimum_instruction_length;
7118 line += lh->line_base + (adj_opcode % lh->line_range);
7119 if (lh->num_file_names < file)
7120 dwarf2_debug_line_missing_file_complaint ();
7121 else
7122 {
7123 lh->file_names[file - 1].included_p = 1;
7124 if (!decode_for_pst_p)
7125 {
7126 if (last_subfile != current_subfile)
7127 {
7128 if (last_subfile)
7129 record_line (last_subfile, 0, address);
7130 last_subfile = current_subfile;
7131 }
7132 /* Append row to matrix using current values. */
7133 record_line (current_subfile, line,
7134 check_cu_functions (address, cu));
7135 }
7136 }
7137 basic_block = 1;
7138 }
7139 else switch (op_code)
7140 {
7141 case DW_LNS_extended_op:
7142 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7143 line_ptr += bytes_read;
7144 extended_end = line_ptr + extended_len;
7145 extended_op = read_1_byte (abfd, line_ptr);
7146 line_ptr += 1;
7147 switch (extended_op)
7148 {
7149 case DW_LNE_end_sequence:
7150 end_sequence = 1;
7151
7152 if (lh->num_file_names < file)
7153 dwarf2_debug_line_missing_file_complaint ();
7154 else
7155 {
7156 lh->file_names[file - 1].included_p = 1;
7157 if (!decode_for_pst_p)
7158 record_line (current_subfile, 0, address);
7159 }
7160 break;
7161 case DW_LNE_set_address:
7162 address = read_address (abfd, line_ptr, cu, &bytes_read);
7163 line_ptr += bytes_read;
7164 address += baseaddr;
7165 break;
7166 case DW_LNE_define_file:
7167 {
7168 char *cur_file;
7169 unsigned int dir_index, mod_time, length;
7170
7171 cur_file = read_string (abfd, line_ptr, &bytes_read);
7172 line_ptr += bytes_read;
7173 dir_index =
7174 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7175 line_ptr += bytes_read;
7176 mod_time =
7177 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7178 line_ptr += bytes_read;
7179 length =
7180 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7181 line_ptr += bytes_read;
7182 add_file_name (lh, cur_file, dir_index, mod_time, length);
7183 }
7184 break;
7185 default:
7186 complaint (&symfile_complaints,
7187 _("mangled .debug_line section"));
7188 return;
7189 }
7190 /* Make sure that we parsed the extended op correctly. If e.g.
7191 we expected a different address size than the producer used,
7192 we may have read the wrong number of bytes. */
7193 if (line_ptr != extended_end)
7194 {
7195 complaint (&symfile_complaints,
7196 _("mangled .debug_line section"));
7197 return;
7198 }
7199 break;
7200 case DW_LNS_copy:
7201 if (lh->num_file_names < file)
7202 dwarf2_debug_line_missing_file_complaint ();
7203 else
7204 {
7205 lh->file_names[file - 1].included_p = 1;
7206 if (!decode_for_pst_p)
7207 {
7208 if (last_subfile != current_subfile)
7209 {
7210 if (last_subfile)
7211 record_line (last_subfile, 0, address);
7212 last_subfile = current_subfile;
7213 }
7214 record_line (current_subfile, line,
7215 check_cu_functions (address, cu));
7216 }
7217 }
7218 basic_block = 0;
7219 break;
7220 case DW_LNS_advance_pc:
7221 address += lh->minimum_instruction_length
7222 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7223 line_ptr += bytes_read;
7224 break;
7225 case DW_LNS_advance_line:
7226 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
7227 line_ptr += bytes_read;
7228 break;
7229 case DW_LNS_set_file:
7230 {
7231 /* The arrays lh->include_dirs and lh->file_names are
7232 0-based, but the directory and file name numbers in
7233 the statement program are 1-based. */
7234 struct file_entry *fe;
7235 char *dir = NULL;
7236
7237 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7238 line_ptr += bytes_read;
7239 if (lh->num_file_names < file)
7240 dwarf2_debug_line_missing_file_complaint ();
7241 else
7242 {
7243 fe = &lh->file_names[file - 1];
7244 if (fe->dir_index)
7245 dir = lh->include_dirs[fe->dir_index - 1];
7246 if (!decode_for_pst_p)
7247 {
7248 last_subfile = current_subfile;
7249 dwarf2_start_subfile (fe->name, dir, comp_dir);
7250 }
7251 }
7252 }
7253 break;
7254 case DW_LNS_set_column:
7255 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7256 line_ptr += bytes_read;
7257 break;
7258 case DW_LNS_negate_stmt:
7259 is_stmt = (!is_stmt);
7260 break;
7261 case DW_LNS_set_basic_block:
7262 basic_block = 1;
7263 break;
7264 /* Add to the address register of the state machine the
7265 address increment value corresponding to special opcode
7266 255. I.e., this value is scaled by the minimum
7267 instruction length since special opcode 255 would have
7268 scaled the the increment. */
7269 case DW_LNS_const_add_pc:
7270 address += (lh->minimum_instruction_length
7271 * ((255 - lh->opcode_base) / lh->line_range));
7272 break;
7273 case DW_LNS_fixed_advance_pc:
7274 address += read_2_bytes (abfd, line_ptr);
7275 line_ptr += 2;
7276 break;
7277 default:
7278 {
7279 /* Unknown standard opcode, ignore it. */
7280 int i;
7281
7282 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
7283 {
7284 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7285 line_ptr += bytes_read;
7286 }
7287 }
7288 }
7289 }
7290 }
7291
7292 if (decode_for_pst_p)
7293 {
7294 int file_index;
7295
7296 /* Now that we're done scanning the Line Header Program, we can
7297 create the psymtab of each included file. */
7298 for (file_index = 0; file_index < lh->num_file_names; file_index++)
7299 if (lh->file_names[file_index].included_p == 1)
7300 {
7301 const struct file_entry fe = lh->file_names [file_index];
7302 char *include_name = fe.name;
7303 char *dir_name = NULL;
7304 char *pst_filename = pst->filename;
7305
7306 if (fe.dir_index)
7307 dir_name = lh->include_dirs[fe.dir_index - 1];
7308
7309 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
7310 {
7311 include_name = concat (dir_name, SLASH_STRING,
7312 include_name, (char *)NULL);
7313 make_cleanup (xfree, include_name);
7314 }
7315
7316 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
7317 {
7318 pst_filename = concat (pst->dirname, SLASH_STRING,
7319 pst_filename, (char *)NULL);
7320 make_cleanup (xfree, pst_filename);
7321 }
7322
7323 if (strcmp (include_name, pst_filename) != 0)
7324 dwarf2_create_include_psymtab (include_name, pst, objfile);
7325 }
7326 }
7327 else
7328 {
7329 /* Make sure a symtab is created for every file, even files
7330 which contain only variables (i.e. no code with associated
7331 line numbers). */
7332
7333 int i;
7334 struct file_entry *fe;
7335
7336 for (i = 0; i < lh->num_file_names; i++)
7337 {
7338 char *dir = NULL;
7339 fe = &lh->file_names[i];
7340 if (fe->dir_index)
7341 dir = lh->include_dirs[fe->dir_index - 1];
7342 dwarf2_start_subfile (fe->name, dir, comp_dir);
7343
7344 /* Skip the main file; we don't need it, and it must be
7345 allocated last, so that it will show up before the
7346 non-primary symtabs in the objfile's symtab list. */
7347 if (current_subfile == first_subfile)
7348 continue;
7349
7350 if (current_subfile->symtab == NULL)
7351 current_subfile->symtab = allocate_symtab (current_subfile->name,
7352 cu->objfile);
7353 fe->symtab = current_subfile->symtab;
7354 }
7355 }
7356 }
7357
7358 /* Start a subfile for DWARF. FILENAME is the name of the file and
7359 DIRNAME the name of the source directory which contains FILENAME
7360 or NULL if not known. COMP_DIR is the compilation directory for the
7361 linetable's compilation unit or NULL if not known.
7362 This routine tries to keep line numbers from identical absolute and
7363 relative file names in a common subfile.
7364
7365 Using the `list' example from the GDB testsuite, which resides in
7366 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
7367 of /srcdir/list0.c yields the following debugging information for list0.c:
7368
7369 DW_AT_name: /srcdir/list0.c
7370 DW_AT_comp_dir: /compdir
7371 files.files[0].name: list0.h
7372 files.files[0].dir: /srcdir
7373 files.files[1].name: list0.c
7374 files.files[1].dir: /srcdir
7375
7376 The line number information for list0.c has to end up in a single
7377 subfile, so that `break /srcdir/list0.c:1' works as expected.
7378 start_subfile will ensure that this happens provided that we pass the
7379 concatenation of files.files[1].dir and files.files[1].name as the
7380 subfile's name. */
7381
7382 static void
7383 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
7384 {
7385 char *fullname;
7386
7387 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
7388 `start_symtab' will always pass the contents of DW_AT_comp_dir as
7389 second argument to start_subfile. To be consistent, we do the
7390 same here. In order not to lose the line information directory,
7391 we concatenate it to the filename when it makes sense.
7392 Note that the Dwarf3 standard says (speaking of filenames in line
7393 information): ``The directory index is ignored for file names
7394 that represent full path names''. Thus ignoring dirname in the
7395 `else' branch below isn't an issue. */
7396
7397 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
7398 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
7399 else
7400 fullname = filename;
7401
7402 start_subfile (fullname, comp_dir);
7403
7404 if (fullname != filename)
7405 xfree (fullname);
7406 }
7407
7408 static void
7409 var_decode_location (struct attribute *attr, struct symbol *sym,
7410 struct dwarf2_cu *cu)
7411 {
7412 struct objfile *objfile = cu->objfile;
7413 struct comp_unit_head *cu_header = &cu->header;
7414
7415 /* NOTE drow/2003-01-30: There used to be a comment and some special
7416 code here to turn a symbol with DW_AT_external and a
7417 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
7418 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
7419 with some versions of binutils) where shared libraries could have
7420 relocations against symbols in their debug information - the
7421 minimal symbol would have the right address, but the debug info
7422 would not. It's no longer necessary, because we will explicitly
7423 apply relocations when we read in the debug information now. */
7424
7425 /* A DW_AT_location attribute with no contents indicates that a
7426 variable has been optimized away. */
7427 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
7428 {
7429 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7430 return;
7431 }
7432
7433 /* Handle one degenerate form of location expression specially, to
7434 preserve GDB's previous behavior when section offsets are
7435 specified. If this is just a DW_OP_addr then mark this symbol
7436 as LOC_STATIC. */
7437
7438 if (attr_form_is_block (attr)
7439 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
7440 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
7441 {
7442 unsigned int dummy;
7443
7444 SYMBOL_VALUE_ADDRESS (sym) =
7445 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
7446 fixup_symbol_section (sym, objfile);
7447 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
7448 SYMBOL_SECTION (sym));
7449 SYMBOL_CLASS (sym) = LOC_STATIC;
7450 return;
7451 }
7452
7453 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
7454 expression evaluator, and use LOC_COMPUTED only when necessary
7455 (i.e. when the value of a register or memory location is
7456 referenced, or a thread-local block, etc.). Then again, it might
7457 not be worthwhile. I'm assuming that it isn't unless performance
7458 or memory numbers show me otherwise. */
7459
7460 dwarf2_symbol_mark_computed (attr, sym, cu);
7461 SYMBOL_CLASS (sym) = LOC_COMPUTED;
7462 }
7463
7464 /* Given a pointer to a DWARF information entry, figure out if we need
7465 to make a symbol table entry for it, and if so, create a new entry
7466 and return a pointer to it.
7467 If TYPE is NULL, determine symbol type from the die, otherwise
7468 used the passed type. */
7469
7470 static struct symbol *
7471 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
7472 {
7473 struct objfile *objfile = cu->objfile;
7474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7475 struct symbol *sym = NULL;
7476 char *name;
7477 struct attribute *attr = NULL;
7478 struct attribute *attr2 = NULL;
7479 CORE_ADDR baseaddr;
7480
7481 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7482
7483 if (die->tag != DW_TAG_namespace)
7484 name = dwarf2_linkage_name (die, cu);
7485 else
7486 name = TYPE_NAME (type);
7487
7488 if (name)
7489 {
7490 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
7491 sizeof (struct symbol));
7492 OBJSTAT (objfile, n_syms++);
7493 memset (sym, 0, sizeof (struct symbol));
7494
7495 /* Cache this symbol's name and the name's demangled form (if any). */
7496 SYMBOL_LANGUAGE (sym) = cu->language;
7497 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
7498
7499 /* Default assumptions.
7500 Use the passed type or decode it from the die. */
7501 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
7502 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7503 if (type != NULL)
7504 SYMBOL_TYPE (sym) = type;
7505 else
7506 SYMBOL_TYPE (sym) = die_type (die, cu);
7507 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
7508 if (attr)
7509 {
7510 SYMBOL_LINE (sym) = DW_UNSND (attr);
7511 }
7512
7513 attr = dwarf2_attr (die, DW_AT_decl_file, cu);
7514 if (attr)
7515 {
7516 int file_index = DW_UNSND (attr);
7517 if (cu->line_header == NULL
7518 || file_index > cu->line_header->num_file_names)
7519 complaint (&symfile_complaints,
7520 _("file index out of range"));
7521 else if (file_index > 0)
7522 {
7523 struct file_entry *fe;
7524 fe = &cu->line_header->file_names[file_index - 1];
7525 SYMBOL_SYMTAB (sym) = fe->symtab;
7526 }
7527 }
7528
7529 switch (die->tag)
7530 {
7531 case DW_TAG_label:
7532 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7533 if (attr)
7534 {
7535 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
7536 }
7537 SYMBOL_CLASS (sym) = LOC_LABEL;
7538 break;
7539 case DW_TAG_subprogram:
7540 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7541 finish_block. */
7542 SYMBOL_CLASS (sym) = LOC_BLOCK;
7543 attr2 = dwarf2_attr (die, DW_AT_external, cu);
7544 if ((attr2 && (DW_UNSND (attr2) != 0))
7545 || cu->language == language_ada)
7546 {
7547 /* Subprograms marked external are stored as a global symbol.
7548 Ada subprograms, whether marked external or not, are always
7549 stored as a global symbol, because we want to be able to
7550 access them globally. For instance, we want to be able
7551 to break on a nested subprogram without having to
7552 specify the context. */
7553 add_symbol_to_list (sym, &global_symbols);
7554 }
7555 else
7556 {
7557 add_symbol_to_list (sym, cu->list_in_scope);
7558 }
7559 break;
7560 case DW_TAG_variable:
7561 /* Compilation with minimal debug info may result in variables
7562 with missing type entries. Change the misleading `void' type
7563 to something sensible. */
7564 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
7565 SYMBOL_TYPE (sym)
7566 = builtin_type (gdbarch)->nodebug_data_symbol;
7567
7568 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7569 if (attr)
7570 {
7571 dwarf2_const_value (attr, sym, cu);
7572 attr2 = dwarf2_attr (die, DW_AT_external, cu);
7573 if (attr2 && (DW_UNSND (attr2) != 0))
7574 add_symbol_to_list (sym, &global_symbols);
7575 else
7576 add_symbol_to_list (sym, cu->list_in_scope);
7577 break;
7578 }
7579 attr = dwarf2_attr (die, DW_AT_location, cu);
7580 if (attr)
7581 {
7582 var_decode_location (attr, sym, cu);
7583 attr2 = dwarf2_attr (die, DW_AT_external, cu);
7584 if (attr2 && (DW_UNSND (attr2) != 0))
7585 add_symbol_to_list (sym, &global_symbols);
7586 else
7587 add_symbol_to_list (sym, cu->list_in_scope);
7588 }
7589 else
7590 {
7591 /* We do not know the address of this symbol.
7592 If it is an external symbol and we have type information
7593 for it, enter the symbol as a LOC_UNRESOLVED symbol.
7594 The address of the variable will then be determined from
7595 the minimal symbol table whenever the variable is
7596 referenced. */
7597 attr2 = dwarf2_attr (die, DW_AT_external, cu);
7598 if (attr2 && (DW_UNSND (attr2) != 0)
7599 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
7600 {
7601 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
7602 add_symbol_to_list (sym, &global_symbols);
7603 }
7604 }
7605 break;
7606 case DW_TAG_formal_parameter:
7607 attr = dwarf2_attr (die, DW_AT_location, cu);
7608 if (attr)
7609 {
7610 var_decode_location (attr, sym, cu);
7611 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
7612 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
7613 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
7614 }
7615 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7616 if (attr)
7617 {
7618 dwarf2_const_value (attr, sym, cu);
7619 }
7620 add_symbol_to_list (sym, cu->list_in_scope);
7621 break;
7622 case DW_TAG_unspecified_parameters:
7623 /* From varargs functions; gdb doesn't seem to have any
7624 interest in this information, so just ignore it for now.
7625 (FIXME?) */
7626 break;
7627 case DW_TAG_class_type:
7628 case DW_TAG_interface_type:
7629 case DW_TAG_structure_type:
7630 case DW_TAG_union_type:
7631 case DW_TAG_set_type:
7632 case DW_TAG_enumeration_type:
7633 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7634 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7635
7636 /* Make sure that the symbol includes appropriate enclosing
7637 classes/namespaces in its name. These are calculated in
7638 read_structure_type, and the correct name is saved in
7639 the type. */
7640
7641 if (cu->language == language_cplus
7642 || cu->language == language_java)
7643 {
7644 struct type *type = SYMBOL_TYPE (sym);
7645
7646 if (TYPE_TAG_NAME (type) != NULL)
7647 {
7648 /* FIXME: carlton/2003-11-10: Should this use
7649 SYMBOL_SET_NAMES instead? (The same problem also
7650 arises further down in this function.) */
7651 /* The type's name is already allocated along with
7652 this objfile, so we don't need to duplicate it
7653 for the symbol. */
7654 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
7655 }
7656 }
7657
7658 {
7659 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
7660 really ever be static objects: otherwise, if you try
7661 to, say, break of a class's method and you're in a file
7662 which doesn't mention that class, it won't work unless
7663 the check for all static symbols in lookup_symbol_aux
7664 saves you. See the OtherFileClass tests in
7665 gdb.c++/namespace.exp. */
7666
7667 struct pending **list_to_add;
7668
7669 list_to_add = (cu->list_in_scope == &file_symbols
7670 && (cu->language == language_cplus
7671 || cu->language == language_java)
7672 ? &global_symbols : cu->list_in_scope);
7673
7674 add_symbol_to_list (sym, list_to_add);
7675
7676 /* The semantics of C++ state that "struct foo { ... }" also
7677 defines a typedef for "foo". A Java class declaration also
7678 defines a typedef for the class. Synthesize a typedef symbol
7679 so that "ptype foo" works as expected. */
7680 if (cu->language == language_cplus
7681 || cu->language == language_java
7682 || cu->language == language_ada)
7683 {
7684 struct symbol *typedef_sym = (struct symbol *)
7685 obstack_alloc (&objfile->objfile_obstack,
7686 sizeof (struct symbol));
7687 *typedef_sym = *sym;
7688 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
7689 /* The symbol's name is already allocated along with
7690 this objfile, so we don't need to duplicate it for
7691 the type. */
7692 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
7693 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
7694 add_symbol_to_list (typedef_sym, list_to_add);
7695 }
7696 }
7697 break;
7698 case DW_TAG_typedef:
7699 if (processing_has_namespace_info
7700 && processing_current_prefix[0] != '\0')
7701 {
7702 SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
7703 processing_current_prefix,
7704 name, cu);
7705 }
7706 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7707 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
7708 add_symbol_to_list (sym, cu->list_in_scope);
7709 break;
7710 case DW_TAG_base_type:
7711 case DW_TAG_subrange_type:
7712 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7713 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
7714 add_symbol_to_list (sym, cu->list_in_scope);
7715 break;
7716 case DW_TAG_enumerator:
7717 if (processing_has_namespace_info
7718 && processing_current_prefix[0] != '\0')
7719 {
7720 SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
7721 processing_current_prefix,
7722 name, cu);
7723 }
7724 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7725 if (attr)
7726 {
7727 dwarf2_const_value (attr, sym, cu);
7728 }
7729 {
7730 /* NOTE: carlton/2003-11-10: See comment above in the
7731 DW_TAG_class_type, etc. block. */
7732
7733 struct pending **list_to_add;
7734
7735 list_to_add = (cu->list_in_scope == &file_symbols
7736 && (cu->language == language_cplus
7737 || cu->language == language_java)
7738 ? &global_symbols : cu->list_in_scope);
7739
7740 add_symbol_to_list (sym, list_to_add);
7741 }
7742 break;
7743 case DW_TAG_namespace:
7744 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7745 add_symbol_to_list (sym, &global_symbols);
7746 break;
7747 default:
7748 /* Not a tag we recognize. Hopefully we aren't processing
7749 trash data, but since we must specifically ignore things
7750 we don't recognize, there is nothing else we should do at
7751 this point. */
7752 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
7753 dwarf_tag_name (die->tag));
7754 break;
7755 }
7756 }
7757 return (sym);
7758 }
7759
7760 /* Copy constant value from an attribute to a symbol. */
7761
7762 static void
7763 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
7764 struct dwarf2_cu *cu)
7765 {
7766 struct objfile *objfile = cu->objfile;
7767 struct comp_unit_head *cu_header = &cu->header;
7768 struct dwarf_block *blk;
7769
7770 switch (attr->form)
7771 {
7772 case DW_FORM_addr:
7773 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
7774 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
7775 cu_header->addr_size,
7776 TYPE_LENGTH (SYMBOL_TYPE
7777 (sym)));
7778 SYMBOL_VALUE_BYTES (sym) =
7779 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
7780 /* NOTE: cagney/2003-05-09: In-lined store_address call with
7781 it's body - store_unsigned_integer. */
7782 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
7783 DW_ADDR (attr));
7784 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7785 break;
7786 case DW_FORM_strp:
7787 /* DW_STRING is already allocated on the obstack, point directly
7788 to it. */
7789 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
7790 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7791 break;
7792 case DW_FORM_block1:
7793 case DW_FORM_block2:
7794 case DW_FORM_block4:
7795 case DW_FORM_block:
7796 blk = DW_BLOCK (attr);
7797 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
7798 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
7799 blk->size,
7800 TYPE_LENGTH (SYMBOL_TYPE
7801 (sym)));
7802 SYMBOL_VALUE_BYTES (sym) =
7803 obstack_alloc (&objfile->objfile_obstack, blk->size);
7804 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
7805 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7806 break;
7807
7808 /* The DW_AT_const_value attributes are supposed to carry the
7809 symbol's value "represented as it would be on the target
7810 architecture." By the time we get here, it's already been
7811 converted to host endianness, so we just need to sign- or
7812 zero-extend it as appropriate. */
7813 case DW_FORM_data1:
7814 dwarf2_const_value_data (attr, sym, 8);
7815 break;
7816 case DW_FORM_data2:
7817 dwarf2_const_value_data (attr, sym, 16);
7818 break;
7819 case DW_FORM_data4:
7820 dwarf2_const_value_data (attr, sym, 32);
7821 break;
7822 case DW_FORM_data8:
7823 dwarf2_const_value_data (attr, sym, 64);
7824 break;
7825
7826 case DW_FORM_sdata:
7827 SYMBOL_VALUE (sym) = DW_SND (attr);
7828 SYMBOL_CLASS (sym) = LOC_CONST;
7829 break;
7830
7831 case DW_FORM_udata:
7832 SYMBOL_VALUE (sym) = DW_UNSND (attr);
7833 SYMBOL_CLASS (sym) = LOC_CONST;
7834 break;
7835
7836 default:
7837 complaint (&symfile_complaints,
7838 _("unsupported const value attribute form: '%s'"),
7839 dwarf_form_name (attr->form));
7840 SYMBOL_VALUE (sym) = 0;
7841 SYMBOL_CLASS (sym) = LOC_CONST;
7842 break;
7843 }
7844 }
7845
7846
7847 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
7848 or zero-extend it as appropriate for the symbol's type. */
7849 static void
7850 dwarf2_const_value_data (struct attribute *attr,
7851 struct symbol *sym,
7852 int bits)
7853 {
7854 LONGEST l = DW_UNSND (attr);
7855
7856 if (bits < sizeof (l) * 8)
7857 {
7858 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
7859 l &= ((LONGEST) 1 << bits) - 1;
7860 else
7861 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
7862 }
7863
7864 SYMBOL_VALUE (sym) = l;
7865 SYMBOL_CLASS (sym) = LOC_CONST;
7866 }
7867
7868
7869 /* Return the type of the die in question using its DW_AT_type attribute. */
7870
7871 static struct type *
7872 die_type (struct die_info *die, struct dwarf2_cu *cu)
7873 {
7874 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7875 struct type *type;
7876 struct attribute *type_attr;
7877 struct die_info *type_die;
7878
7879 type_attr = dwarf2_attr (die, DW_AT_type, cu);
7880 if (!type_attr)
7881 {
7882 /* A missing DW_AT_type represents a void type. */
7883 return builtin_type (gdbarch)->builtin_void;
7884 }
7885 else
7886 type_die = follow_die_ref (die, type_attr, cu);
7887
7888 type = tag_type_to_type (type_die, cu);
7889 if (!type)
7890 {
7891 dump_die (type_die);
7892 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
7893 cu->objfile->name);
7894 }
7895 return type;
7896 }
7897
7898 /* Return the containing type of the die in question using its
7899 DW_AT_containing_type attribute. */
7900
7901 static struct type *
7902 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
7903 {
7904 struct type *type = NULL;
7905 struct attribute *type_attr;
7906 struct die_info *type_die = NULL;
7907
7908 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
7909 if (type_attr)
7910 {
7911 type_die = follow_die_ref (die, type_attr, cu);
7912 type = tag_type_to_type (type_die, cu);
7913 }
7914 if (!type)
7915 {
7916 if (type_die)
7917 dump_die (type_die);
7918 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
7919 cu->objfile->name);
7920 }
7921 return type;
7922 }
7923
7924 static struct type *
7925 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
7926 {
7927 if (die->type)
7928 {
7929 return die->type;
7930 }
7931 else
7932 {
7933 read_type_die (die, cu);
7934 if (!die->type)
7935 {
7936 dump_die (die);
7937 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
7938 cu->objfile->name);
7939 }
7940 return die->type;
7941 }
7942 }
7943
7944 static void
7945 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
7946 {
7947 char *prefix = determine_prefix (die, cu);
7948 const char *old_prefix = processing_current_prefix;
7949 struct cleanup *back_to = make_cleanup (xfree, prefix);
7950 processing_current_prefix = prefix;
7951
7952 switch (die->tag)
7953 {
7954 case DW_TAG_class_type:
7955 case DW_TAG_interface_type:
7956 case DW_TAG_structure_type:
7957 case DW_TAG_union_type:
7958 read_structure_type (die, cu);
7959 break;
7960 case DW_TAG_enumeration_type:
7961 read_enumeration_type (die, cu);
7962 break;
7963 case DW_TAG_subprogram:
7964 case DW_TAG_subroutine_type:
7965 read_subroutine_type (die, cu);
7966 break;
7967 case DW_TAG_array_type:
7968 read_array_type (die, cu);
7969 break;
7970 case DW_TAG_set_type:
7971 read_set_type (die, cu);
7972 break;
7973 case DW_TAG_pointer_type:
7974 read_tag_pointer_type (die, cu);
7975 break;
7976 case DW_TAG_ptr_to_member_type:
7977 read_tag_ptr_to_member_type (die, cu);
7978 break;
7979 case DW_TAG_reference_type:
7980 read_tag_reference_type (die, cu);
7981 break;
7982 case DW_TAG_const_type:
7983 read_tag_const_type (die, cu);
7984 break;
7985 case DW_TAG_volatile_type:
7986 read_tag_volatile_type (die, cu);
7987 break;
7988 case DW_TAG_string_type:
7989 read_tag_string_type (die, cu);
7990 break;
7991 case DW_TAG_typedef:
7992 read_typedef (die, cu);
7993 break;
7994 case DW_TAG_subrange_type:
7995 read_subrange_type (die, cu);
7996 break;
7997 case DW_TAG_base_type:
7998 read_base_type (die, cu);
7999 break;
8000 case DW_TAG_unspecified_type:
8001 read_unspecified_type (die, cu);
8002 break;
8003 default:
8004 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
8005 dwarf_tag_name (die->tag));
8006 break;
8007 }
8008
8009 processing_current_prefix = old_prefix;
8010 do_cleanups (back_to);
8011 }
8012
8013 /* Return the name of the namespace/class that DIE is defined within,
8014 or "" if we can't tell. The caller should xfree the result. */
8015
8016 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
8017 therein) for an example of how to use this function to deal with
8018 DW_AT_specification. */
8019
8020 static char *
8021 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
8022 {
8023 struct die_info *parent;
8024
8025 if (cu->language != language_cplus
8026 && cu->language != language_java)
8027 return NULL;
8028
8029 parent = die->parent;
8030
8031 if (parent == NULL)
8032 {
8033 return xstrdup ("");
8034 }
8035 else
8036 {
8037 switch (parent->tag) {
8038 case DW_TAG_namespace:
8039 {
8040 /* FIXME: carlton/2004-03-05: Should I follow extension dies
8041 before doing this check? */
8042 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
8043 {
8044 return xstrdup (TYPE_TAG_NAME (parent->type));
8045 }
8046 else
8047 {
8048 int dummy;
8049 char *parent_prefix = determine_prefix (parent, cu);
8050 char *retval = typename_concat (NULL, parent_prefix,
8051 namespace_name (parent, &dummy,
8052 cu),
8053 cu);
8054 xfree (parent_prefix);
8055 return retval;
8056 }
8057 }
8058 break;
8059 case DW_TAG_class_type:
8060 case DW_TAG_interface_type:
8061 case DW_TAG_structure_type:
8062 {
8063 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
8064 {
8065 return xstrdup (TYPE_TAG_NAME (parent->type));
8066 }
8067 else
8068 {
8069 const char *old_prefix = processing_current_prefix;
8070 char *new_prefix = determine_prefix (parent, cu);
8071 char *retval;
8072
8073 processing_current_prefix = new_prefix;
8074 retval = determine_class_name (parent, cu);
8075 processing_current_prefix = old_prefix;
8076
8077 xfree (new_prefix);
8078 return retval;
8079 }
8080 }
8081 default:
8082 return determine_prefix (parent, cu);
8083 }
8084 }
8085 }
8086
8087 /* Return a newly-allocated string formed by concatenating PREFIX and
8088 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
8089 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
8090 perform an obconcat, otherwise allocate storage for the result. The CU argument
8091 is used to determine the language and hence, the appropriate separator. */
8092
8093 #define MAX_SEP_LEN 2 /* sizeof ("::") */
8094
8095 static char *
8096 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
8097 struct dwarf2_cu *cu)
8098 {
8099 char *sep;
8100
8101 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
8102 sep = "";
8103 else if (cu->language == language_java)
8104 sep = ".";
8105 else
8106 sep = "::";
8107
8108 if (obs == NULL)
8109 {
8110 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
8111 retval[0] = '\0';
8112
8113 if (prefix)
8114 {
8115 strcpy (retval, prefix);
8116 strcat (retval, sep);
8117 }
8118 if (suffix)
8119 strcat (retval, suffix);
8120
8121 return retval;
8122 }
8123 else
8124 {
8125 /* We have an obstack. */
8126 return obconcat (obs, prefix, sep, suffix);
8127 }
8128 }
8129
8130 #if 0
8131 struct die_info *
8132 copy_die (struct die_info *old_die)
8133 {
8134 struct die_info *new_die;
8135 int i, num_attrs;
8136
8137 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
8138 memset (new_die, 0, sizeof (struct die_info));
8139
8140 new_die->tag = old_die->tag;
8141 new_die->has_children = old_die->has_children;
8142 new_die->abbrev = old_die->abbrev;
8143 new_die->offset = old_die->offset;
8144 new_die->type = NULL;
8145
8146 num_attrs = old_die->num_attrs;
8147 new_die->num_attrs = num_attrs;
8148 new_die->attrs = (struct attribute *)
8149 xmalloc (num_attrs * sizeof (struct attribute));
8150
8151 for (i = 0; i < old_die->num_attrs; ++i)
8152 {
8153 new_die->attrs[i].name = old_die->attrs[i].name;
8154 new_die->attrs[i].form = old_die->attrs[i].form;
8155 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
8156 }
8157
8158 new_die->next = NULL;
8159 return new_die;
8160 }
8161 #endif
8162
8163 /* Return sibling of die, NULL if no sibling. */
8164
8165 static struct die_info *
8166 sibling_die (struct die_info *die)
8167 {
8168 return die->sibling;
8169 }
8170
8171 /* Get linkage name of a die, return NULL if not found. */
8172
8173 static char *
8174 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8175 {
8176 struct attribute *attr;
8177
8178 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8179 if (attr && DW_STRING (attr))
8180 return DW_STRING (attr);
8181 attr = dwarf2_attr (die, DW_AT_name, cu);
8182 if (attr && DW_STRING (attr))
8183 return DW_STRING (attr);
8184 return NULL;
8185 }
8186
8187 /* Get name of a die, return NULL if not found. */
8188
8189 static char *
8190 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
8191 {
8192 struct attribute *attr;
8193
8194 attr = dwarf2_attr (die, DW_AT_name, cu);
8195 if (attr && DW_STRING (attr))
8196 return DW_STRING (attr);
8197 return NULL;
8198 }
8199
8200 /* Return the die that this die in an extension of, or NULL if there
8201 is none. */
8202
8203 static struct die_info *
8204 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
8205 {
8206 struct attribute *attr;
8207
8208 attr = dwarf2_attr (die, DW_AT_extension, cu);
8209 if (attr == NULL)
8210 return NULL;
8211
8212 return follow_die_ref (die, attr, cu);
8213 }
8214
8215 /* Convert a DIE tag into its string name. */
8216
8217 static char *
8218 dwarf_tag_name (unsigned tag)
8219 {
8220 switch (tag)
8221 {
8222 case DW_TAG_padding:
8223 return "DW_TAG_padding";
8224 case DW_TAG_array_type:
8225 return "DW_TAG_array_type";
8226 case DW_TAG_class_type:
8227 return "DW_TAG_class_type";
8228 case DW_TAG_entry_point:
8229 return "DW_TAG_entry_point";
8230 case DW_TAG_enumeration_type:
8231 return "DW_TAG_enumeration_type";
8232 case DW_TAG_formal_parameter:
8233 return "DW_TAG_formal_parameter";
8234 case DW_TAG_imported_declaration:
8235 return "DW_TAG_imported_declaration";
8236 case DW_TAG_label:
8237 return "DW_TAG_label";
8238 case DW_TAG_lexical_block:
8239 return "DW_TAG_lexical_block";
8240 case DW_TAG_member:
8241 return "DW_TAG_member";
8242 case DW_TAG_pointer_type:
8243 return "DW_TAG_pointer_type";
8244 case DW_TAG_reference_type:
8245 return "DW_TAG_reference_type";
8246 case DW_TAG_compile_unit:
8247 return "DW_TAG_compile_unit";
8248 case DW_TAG_string_type:
8249 return "DW_TAG_string_type";
8250 case DW_TAG_structure_type:
8251 return "DW_TAG_structure_type";
8252 case DW_TAG_subroutine_type:
8253 return "DW_TAG_subroutine_type";
8254 case DW_TAG_typedef:
8255 return "DW_TAG_typedef";
8256 case DW_TAG_union_type:
8257 return "DW_TAG_union_type";
8258 case DW_TAG_unspecified_parameters:
8259 return "DW_TAG_unspecified_parameters";
8260 case DW_TAG_variant:
8261 return "DW_TAG_variant";
8262 case DW_TAG_common_block:
8263 return "DW_TAG_common_block";
8264 case DW_TAG_common_inclusion:
8265 return "DW_TAG_common_inclusion";
8266 case DW_TAG_inheritance:
8267 return "DW_TAG_inheritance";
8268 case DW_TAG_inlined_subroutine:
8269 return "DW_TAG_inlined_subroutine";
8270 case DW_TAG_module:
8271 return "DW_TAG_module";
8272 case DW_TAG_ptr_to_member_type:
8273 return "DW_TAG_ptr_to_member_type";
8274 case DW_TAG_set_type:
8275 return "DW_TAG_set_type";
8276 case DW_TAG_subrange_type:
8277 return "DW_TAG_subrange_type";
8278 case DW_TAG_with_stmt:
8279 return "DW_TAG_with_stmt";
8280 case DW_TAG_access_declaration:
8281 return "DW_TAG_access_declaration";
8282 case DW_TAG_base_type:
8283 return "DW_TAG_base_type";
8284 case DW_TAG_catch_block:
8285 return "DW_TAG_catch_block";
8286 case DW_TAG_const_type:
8287 return "DW_TAG_const_type";
8288 case DW_TAG_constant:
8289 return "DW_TAG_constant";
8290 case DW_TAG_enumerator:
8291 return "DW_TAG_enumerator";
8292 case DW_TAG_file_type:
8293 return "DW_TAG_file_type";
8294 case DW_TAG_friend:
8295 return "DW_TAG_friend";
8296 case DW_TAG_namelist:
8297 return "DW_TAG_namelist";
8298 case DW_TAG_namelist_item:
8299 return "DW_TAG_namelist_item";
8300 case DW_TAG_packed_type:
8301 return "DW_TAG_packed_type";
8302 case DW_TAG_subprogram:
8303 return "DW_TAG_subprogram";
8304 case DW_TAG_template_type_param:
8305 return "DW_TAG_template_type_param";
8306 case DW_TAG_template_value_param:
8307 return "DW_TAG_template_value_param";
8308 case DW_TAG_thrown_type:
8309 return "DW_TAG_thrown_type";
8310 case DW_TAG_try_block:
8311 return "DW_TAG_try_block";
8312 case DW_TAG_variant_part:
8313 return "DW_TAG_variant_part";
8314 case DW_TAG_variable:
8315 return "DW_TAG_variable";
8316 case DW_TAG_volatile_type:
8317 return "DW_TAG_volatile_type";
8318 case DW_TAG_dwarf_procedure:
8319 return "DW_TAG_dwarf_procedure";
8320 case DW_TAG_restrict_type:
8321 return "DW_TAG_restrict_type";
8322 case DW_TAG_interface_type:
8323 return "DW_TAG_interface_type";
8324 case DW_TAG_namespace:
8325 return "DW_TAG_namespace";
8326 case DW_TAG_imported_module:
8327 return "DW_TAG_imported_module";
8328 case DW_TAG_unspecified_type:
8329 return "DW_TAG_unspecified_type";
8330 case DW_TAG_partial_unit:
8331 return "DW_TAG_partial_unit";
8332 case DW_TAG_imported_unit:
8333 return "DW_TAG_imported_unit";
8334 case DW_TAG_condition:
8335 return "DW_TAG_condition";
8336 case DW_TAG_shared_type:
8337 return "DW_TAG_shared_type";
8338 case DW_TAG_MIPS_loop:
8339 return "DW_TAG_MIPS_loop";
8340 case DW_TAG_HP_array_descriptor:
8341 return "DW_TAG_HP_array_descriptor";
8342 case DW_TAG_format_label:
8343 return "DW_TAG_format_label";
8344 case DW_TAG_function_template:
8345 return "DW_TAG_function_template";
8346 case DW_TAG_class_template:
8347 return "DW_TAG_class_template";
8348 case DW_TAG_GNU_BINCL:
8349 return "DW_TAG_GNU_BINCL";
8350 case DW_TAG_GNU_EINCL:
8351 return "DW_TAG_GNU_EINCL";
8352 case DW_TAG_upc_shared_type:
8353 return "DW_TAG_upc_shared_type";
8354 case DW_TAG_upc_strict_type:
8355 return "DW_TAG_upc_strict_type";
8356 case DW_TAG_upc_relaxed_type:
8357 return "DW_TAG_upc_relaxed_type";
8358 case DW_TAG_PGI_kanji_type:
8359 return "DW_TAG_PGI_kanji_type";
8360 case DW_TAG_PGI_interface_block:
8361 return "DW_TAG_PGI_interface_block";
8362 default:
8363 return "DW_TAG_<unknown>";
8364 }
8365 }
8366
8367 /* Convert a DWARF attribute code into its string name. */
8368
8369 static char *
8370 dwarf_attr_name (unsigned attr)
8371 {
8372 switch (attr)
8373 {
8374 case DW_AT_sibling:
8375 return "DW_AT_sibling";
8376 case DW_AT_location:
8377 return "DW_AT_location";
8378 case DW_AT_name:
8379 return "DW_AT_name";
8380 case DW_AT_ordering:
8381 return "DW_AT_ordering";
8382 case DW_AT_subscr_data:
8383 return "DW_AT_subscr_data";
8384 case DW_AT_byte_size:
8385 return "DW_AT_byte_size";
8386 case DW_AT_bit_offset:
8387 return "DW_AT_bit_offset";
8388 case DW_AT_bit_size:
8389 return "DW_AT_bit_size";
8390 case DW_AT_element_list:
8391 return "DW_AT_element_list";
8392 case DW_AT_stmt_list:
8393 return "DW_AT_stmt_list";
8394 case DW_AT_low_pc:
8395 return "DW_AT_low_pc";
8396 case DW_AT_high_pc:
8397 return "DW_AT_high_pc";
8398 case DW_AT_language:
8399 return "DW_AT_language";
8400 case DW_AT_member:
8401 return "DW_AT_member";
8402 case DW_AT_discr:
8403 return "DW_AT_discr";
8404 case DW_AT_discr_value:
8405 return "DW_AT_discr_value";
8406 case DW_AT_visibility:
8407 return "DW_AT_visibility";
8408 case DW_AT_import:
8409 return "DW_AT_import";
8410 case DW_AT_string_length:
8411 return "DW_AT_string_length";
8412 case DW_AT_common_reference:
8413 return "DW_AT_common_reference";
8414 case DW_AT_comp_dir:
8415 return "DW_AT_comp_dir";
8416 case DW_AT_const_value:
8417 return "DW_AT_const_value";
8418 case DW_AT_containing_type:
8419 return "DW_AT_containing_type";
8420 case DW_AT_default_value:
8421 return "DW_AT_default_value";
8422 case DW_AT_inline:
8423 return "DW_AT_inline";
8424 case DW_AT_is_optional:
8425 return "DW_AT_is_optional";
8426 case DW_AT_lower_bound:
8427 return "DW_AT_lower_bound";
8428 case DW_AT_producer:
8429 return "DW_AT_producer";
8430 case DW_AT_prototyped:
8431 return "DW_AT_prototyped";
8432 case DW_AT_return_addr:
8433 return "DW_AT_return_addr";
8434 case DW_AT_start_scope:
8435 return "DW_AT_start_scope";
8436 case DW_AT_bit_stride:
8437 return "DW_AT_bit_stride";
8438 case DW_AT_upper_bound:
8439 return "DW_AT_upper_bound";
8440 case DW_AT_abstract_origin:
8441 return "DW_AT_abstract_origin";
8442 case DW_AT_accessibility:
8443 return "DW_AT_accessibility";
8444 case DW_AT_address_class:
8445 return "DW_AT_address_class";
8446 case DW_AT_artificial:
8447 return "DW_AT_artificial";
8448 case DW_AT_base_types:
8449 return "DW_AT_base_types";
8450 case DW_AT_calling_convention:
8451 return "DW_AT_calling_convention";
8452 case DW_AT_count:
8453 return "DW_AT_count";
8454 case DW_AT_data_member_location:
8455 return "DW_AT_data_member_location";
8456 case DW_AT_decl_column:
8457 return "DW_AT_decl_column";
8458 case DW_AT_decl_file:
8459 return "DW_AT_decl_file";
8460 case DW_AT_decl_line:
8461 return "DW_AT_decl_line";
8462 case DW_AT_declaration:
8463 return "DW_AT_declaration";
8464 case DW_AT_discr_list:
8465 return "DW_AT_discr_list";
8466 case DW_AT_encoding:
8467 return "DW_AT_encoding";
8468 case DW_AT_external:
8469 return "DW_AT_external";
8470 case DW_AT_frame_base:
8471 return "DW_AT_frame_base";
8472 case DW_AT_friend:
8473 return "DW_AT_friend";
8474 case DW_AT_identifier_case:
8475 return "DW_AT_identifier_case";
8476 case DW_AT_macro_info:
8477 return "DW_AT_macro_info";
8478 case DW_AT_namelist_items:
8479 return "DW_AT_namelist_items";
8480 case DW_AT_priority:
8481 return "DW_AT_priority";
8482 case DW_AT_segment:
8483 return "DW_AT_segment";
8484 case DW_AT_specification:
8485 return "DW_AT_specification";
8486 case DW_AT_static_link:
8487 return "DW_AT_static_link";
8488 case DW_AT_type:
8489 return "DW_AT_type";
8490 case DW_AT_use_location:
8491 return "DW_AT_use_location";
8492 case DW_AT_variable_parameter:
8493 return "DW_AT_variable_parameter";
8494 case DW_AT_virtuality:
8495 return "DW_AT_virtuality";
8496 case DW_AT_vtable_elem_location:
8497 return "DW_AT_vtable_elem_location";
8498 /* DWARF 3 values. */
8499 case DW_AT_allocated:
8500 return "DW_AT_allocated";
8501 case DW_AT_associated:
8502 return "DW_AT_associated";
8503 case DW_AT_data_location:
8504 return "DW_AT_data_location";
8505 case DW_AT_byte_stride:
8506 return "DW_AT_byte_stride";
8507 case DW_AT_entry_pc:
8508 return "DW_AT_entry_pc";
8509 case DW_AT_use_UTF8:
8510 return "DW_AT_use_UTF8";
8511 case DW_AT_extension:
8512 return "DW_AT_extension";
8513 case DW_AT_ranges:
8514 return "DW_AT_ranges";
8515 case DW_AT_trampoline:
8516 return "DW_AT_trampoline";
8517 case DW_AT_call_column:
8518 return "DW_AT_call_column";
8519 case DW_AT_call_file:
8520 return "DW_AT_call_file";
8521 case DW_AT_call_line:
8522 return "DW_AT_call_line";
8523 case DW_AT_description:
8524 return "DW_AT_description";
8525 case DW_AT_binary_scale:
8526 return "DW_AT_binary_scale";
8527 case DW_AT_decimal_scale:
8528 return "DW_AT_decimal_scale";
8529 case DW_AT_small:
8530 return "DW_AT_small";
8531 case DW_AT_decimal_sign:
8532 return "DW_AT_decimal_sign";
8533 case DW_AT_digit_count:
8534 return "DW_AT_digit_count";
8535 case DW_AT_picture_string:
8536 return "DW_AT_picture_string";
8537 case DW_AT_mutable:
8538 return "DW_AT_mutable";
8539 case DW_AT_threads_scaled:
8540 return "DW_AT_threads_scaled";
8541 case DW_AT_explicit:
8542 return "DW_AT_explicit";
8543 case DW_AT_object_pointer:
8544 return "DW_AT_object_pointer";
8545 case DW_AT_endianity:
8546 return "DW_AT_endianity";
8547 case DW_AT_elemental:
8548 return "DW_AT_elemental";
8549 case DW_AT_pure:
8550 return "DW_AT_pure";
8551 case DW_AT_recursive:
8552 return "DW_AT_recursive";
8553 #ifdef MIPS
8554 /* SGI/MIPS extensions. */
8555 case DW_AT_MIPS_fde:
8556 return "DW_AT_MIPS_fde";
8557 case DW_AT_MIPS_loop_begin:
8558 return "DW_AT_MIPS_loop_begin";
8559 case DW_AT_MIPS_tail_loop_begin:
8560 return "DW_AT_MIPS_tail_loop_begin";
8561 case DW_AT_MIPS_epilog_begin:
8562 return "DW_AT_MIPS_epilog_begin";
8563 case DW_AT_MIPS_loop_unroll_factor:
8564 return "DW_AT_MIPS_loop_unroll_factor";
8565 case DW_AT_MIPS_software_pipeline_depth:
8566 return "DW_AT_MIPS_software_pipeline_depth";
8567 case DW_AT_MIPS_linkage_name:
8568 return "DW_AT_MIPS_linkage_name";
8569 case DW_AT_MIPS_stride:
8570 return "DW_AT_MIPS_stride";
8571 case DW_AT_MIPS_abstract_name:
8572 return "DW_AT_MIPS_abstract_name";
8573 case DW_AT_MIPS_clone_origin:
8574 return "DW_AT_MIPS_clone_origin";
8575 case DW_AT_MIPS_has_inlines:
8576 return "DW_AT_MIPS_has_inlines";
8577 #endif
8578 /* HP extensions. */
8579 case DW_AT_HP_block_index:
8580 return "DW_AT_HP_block_index";
8581 case DW_AT_HP_unmodifiable:
8582 return "DW_AT_HP_unmodifiable";
8583 case DW_AT_HP_actuals_stmt_list:
8584 return "DW_AT_HP_actuals_stmt_list";
8585 case DW_AT_HP_proc_per_section:
8586 return "DW_AT_HP_proc_per_section";
8587 case DW_AT_HP_raw_data_ptr:
8588 return "DW_AT_HP_raw_data_ptr";
8589 case DW_AT_HP_pass_by_reference:
8590 return "DW_AT_HP_pass_by_reference";
8591 case DW_AT_HP_opt_level:
8592 return "DW_AT_HP_opt_level";
8593 case DW_AT_HP_prof_version_id:
8594 return "DW_AT_HP_prof_version_id";
8595 case DW_AT_HP_opt_flags:
8596 return "DW_AT_HP_opt_flags";
8597 case DW_AT_HP_cold_region_low_pc:
8598 return "DW_AT_HP_cold_region_low_pc";
8599 case DW_AT_HP_cold_region_high_pc:
8600 return "DW_AT_HP_cold_region_high_pc";
8601 case DW_AT_HP_all_variables_modifiable:
8602 return "DW_AT_HP_all_variables_modifiable";
8603 case DW_AT_HP_linkage_name:
8604 return "DW_AT_HP_linkage_name";
8605 case DW_AT_HP_prof_flags:
8606 return "DW_AT_HP_prof_flags";
8607 /* GNU extensions. */
8608 case DW_AT_sf_names:
8609 return "DW_AT_sf_names";
8610 case DW_AT_src_info:
8611 return "DW_AT_src_info";
8612 case DW_AT_mac_info:
8613 return "DW_AT_mac_info";
8614 case DW_AT_src_coords:
8615 return "DW_AT_src_coords";
8616 case DW_AT_body_begin:
8617 return "DW_AT_body_begin";
8618 case DW_AT_body_end:
8619 return "DW_AT_body_end";
8620 case DW_AT_GNU_vector:
8621 return "DW_AT_GNU_vector";
8622 /* VMS extensions. */
8623 case DW_AT_VMS_rtnbeg_pd_address:
8624 return "DW_AT_VMS_rtnbeg_pd_address";
8625 /* UPC extension. */
8626 case DW_AT_upc_threads_scaled:
8627 return "DW_AT_upc_threads_scaled";
8628 /* PGI (STMicroelectronics) extensions. */
8629 case DW_AT_PGI_lbase:
8630 return "DW_AT_PGI_lbase";
8631 case DW_AT_PGI_soffset:
8632 return "DW_AT_PGI_soffset";
8633 case DW_AT_PGI_lstride:
8634 return "DW_AT_PGI_lstride";
8635 default:
8636 return "DW_AT_<unknown>";
8637 }
8638 }
8639
8640 /* Convert a DWARF value form code into its string name. */
8641
8642 static char *
8643 dwarf_form_name (unsigned form)
8644 {
8645 switch (form)
8646 {
8647 case DW_FORM_addr:
8648 return "DW_FORM_addr";
8649 case DW_FORM_block2:
8650 return "DW_FORM_block2";
8651 case DW_FORM_block4:
8652 return "DW_FORM_block4";
8653 case DW_FORM_data2:
8654 return "DW_FORM_data2";
8655 case DW_FORM_data4:
8656 return "DW_FORM_data4";
8657 case DW_FORM_data8:
8658 return "DW_FORM_data8";
8659 case DW_FORM_string:
8660 return "DW_FORM_string";
8661 case DW_FORM_block:
8662 return "DW_FORM_block";
8663 case DW_FORM_block1:
8664 return "DW_FORM_block1";
8665 case DW_FORM_data1:
8666 return "DW_FORM_data1";
8667 case DW_FORM_flag:
8668 return "DW_FORM_flag";
8669 case DW_FORM_sdata:
8670 return "DW_FORM_sdata";
8671 case DW_FORM_strp:
8672 return "DW_FORM_strp";
8673 case DW_FORM_udata:
8674 return "DW_FORM_udata";
8675 case DW_FORM_ref_addr:
8676 return "DW_FORM_ref_addr";
8677 case DW_FORM_ref1:
8678 return "DW_FORM_ref1";
8679 case DW_FORM_ref2:
8680 return "DW_FORM_ref2";
8681 case DW_FORM_ref4:
8682 return "DW_FORM_ref4";
8683 case DW_FORM_ref8:
8684 return "DW_FORM_ref8";
8685 case DW_FORM_ref_udata:
8686 return "DW_FORM_ref_udata";
8687 case DW_FORM_indirect:
8688 return "DW_FORM_indirect";
8689 default:
8690 return "DW_FORM_<unknown>";
8691 }
8692 }
8693
8694 /* Convert a DWARF stack opcode into its string name. */
8695
8696 static char *
8697 dwarf_stack_op_name (unsigned op)
8698 {
8699 switch (op)
8700 {
8701 case DW_OP_addr:
8702 return "DW_OP_addr";
8703 case DW_OP_deref:
8704 return "DW_OP_deref";
8705 case DW_OP_const1u:
8706 return "DW_OP_const1u";
8707 case DW_OP_const1s:
8708 return "DW_OP_const1s";
8709 case DW_OP_const2u:
8710 return "DW_OP_const2u";
8711 case DW_OP_const2s:
8712 return "DW_OP_const2s";
8713 case DW_OP_const4u:
8714 return "DW_OP_const4u";
8715 case DW_OP_const4s:
8716 return "DW_OP_const4s";
8717 case DW_OP_const8u:
8718 return "DW_OP_const8u";
8719 case DW_OP_const8s:
8720 return "DW_OP_const8s";
8721 case DW_OP_constu:
8722 return "DW_OP_constu";
8723 case DW_OP_consts:
8724 return "DW_OP_consts";
8725 case DW_OP_dup:
8726 return "DW_OP_dup";
8727 case DW_OP_drop:
8728 return "DW_OP_drop";
8729 case DW_OP_over:
8730 return "DW_OP_over";
8731 case DW_OP_pick:
8732 return "DW_OP_pick";
8733 case DW_OP_swap:
8734 return "DW_OP_swap";
8735 case DW_OP_rot:
8736 return "DW_OP_rot";
8737 case DW_OP_xderef:
8738 return "DW_OP_xderef";
8739 case DW_OP_abs:
8740 return "DW_OP_abs";
8741 case DW_OP_and:
8742 return "DW_OP_and";
8743 case DW_OP_div:
8744 return "DW_OP_div";
8745 case DW_OP_minus:
8746 return "DW_OP_minus";
8747 case DW_OP_mod:
8748 return "DW_OP_mod";
8749 case DW_OP_mul:
8750 return "DW_OP_mul";
8751 case DW_OP_neg:
8752 return "DW_OP_neg";
8753 case DW_OP_not:
8754 return "DW_OP_not";
8755 case DW_OP_or:
8756 return "DW_OP_or";
8757 case DW_OP_plus:
8758 return "DW_OP_plus";
8759 case DW_OP_plus_uconst:
8760 return "DW_OP_plus_uconst";
8761 case DW_OP_shl:
8762 return "DW_OP_shl";
8763 case DW_OP_shr:
8764 return "DW_OP_shr";
8765 case DW_OP_shra:
8766 return "DW_OP_shra";
8767 case DW_OP_xor:
8768 return "DW_OP_xor";
8769 case DW_OP_bra:
8770 return "DW_OP_bra";
8771 case DW_OP_eq:
8772 return "DW_OP_eq";
8773 case DW_OP_ge:
8774 return "DW_OP_ge";
8775 case DW_OP_gt:
8776 return "DW_OP_gt";
8777 case DW_OP_le:
8778 return "DW_OP_le";
8779 case DW_OP_lt:
8780 return "DW_OP_lt";
8781 case DW_OP_ne:
8782 return "DW_OP_ne";
8783 case DW_OP_skip:
8784 return "DW_OP_skip";
8785 case DW_OP_lit0:
8786 return "DW_OP_lit0";
8787 case DW_OP_lit1:
8788 return "DW_OP_lit1";
8789 case DW_OP_lit2:
8790 return "DW_OP_lit2";
8791 case DW_OP_lit3:
8792 return "DW_OP_lit3";
8793 case DW_OP_lit4:
8794 return "DW_OP_lit4";
8795 case DW_OP_lit5:
8796 return "DW_OP_lit5";
8797 case DW_OP_lit6:
8798 return "DW_OP_lit6";
8799 case DW_OP_lit7:
8800 return "DW_OP_lit7";
8801 case DW_OP_lit8:
8802 return "DW_OP_lit8";
8803 case DW_OP_lit9:
8804 return "DW_OP_lit9";
8805 case DW_OP_lit10:
8806 return "DW_OP_lit10";
8807 case DW_OP_lit11:
8808 return "DW_OP_lit11";
8809 case DW_OP_lit12:
8810 return "DW_OP_lit12";
8811 case DW_OP_lit13:
8812 return "DW_OP_lit13";
8813 case DW_OP_lit14:
8814 return "DW_OP_lit14";
8815 case DW_OP_lit15:
8816 return "DW_OP_lit15";
8817 case DW_OP_lit16:
8818 return "DW_OP_lit16";
8819 case DW_OP_lit17:
8820 return "DW_OP_lit17";
8821 case DW_OP_lit18:
8822 return "DW_OP_lit18";
8823 case DW_OP_lit19:
8824 return "DW_OP_lit19";
8825 case DW_OP_lit20:
8826 return "DW_OP_lit20";
8827 case DW_OP_lit21:
8828 return "DW_OP_lit21";
8829 case DW_OP_lit22:
8830 return "DW_OP_lit22";
8831 case DW_OP_lit23:
8832 return "DW_OP_lit23";
8833 case DW_OP_lit24:
8834 return "DW_OP_lit24";
8835 case DW_OP_lit25:
8836 return "DW_OP_lit25";
8837 case DW_OP_lit26:
8838 return "DW_OP_lit26";
8839 case DW_OP_lit27:
8840 return "DW_OP_lit27";
8841 case DW_OP_lit28:
8842 return "DW_OP_lit28";
8843 case DW_OP_lit29:
8844 return "DW_OP_lit29";
8845 case DW_OP_lit30:
8846 return "DW_OP_lit30";
8847 case DW_OP_lit31:
8848 return "DW_OP_lit31";
8849 case DW_OP_reg0:
8850 return "DW_OP_reg0";
8851 case DW_OP_reg1:
8852 return "DW_OP_reg1";
8853 case DW_OP_reg2:
8854 return "DW_OP_reg2";
8855 case DW_OP_reg3:
8856 return "DW_OP_reg3";
8857 case DW_OP_reg4:
8858 return "DW_OP_reg4";
8859 case DW_OP_reg5:
8860 return "DW_OP_reg5";
8861 case DW_OP_reg6:
8862 return "DW_OP_reg6";
8863 case DW_OP_reg7:
8864 return "DW_OP_reg7";
8865 case DW_OP_reg8:
8866 return "DW_OP_reg8";
8867 case DW_OP_reg9:
8868 return "DW_OP_reg9";
8869 case DW_OP_reg10:
8870 return "DW_OP_reg10";
8871 case DW_OP_reg11:
8872 return "DW_OP_reg11";
8873 case DW_OP_reg12:
8874 return "DW_OP_reg12";
8875 case DW_OP_reg13:
8876 return "DW_OP_reg13";
8877 case DW_OP_reg14:
8878 return "DW_OP_reg14";
8879 case DW_OP_reg15:
8880 return "DW_OP_reg15";
8881 case DW_OP_reg16:
8882 return "DW_OP_reg16";
8883 case DW_OP_reg17:
8884 return "DW_OP_reg17";
8885 case DW_OP_reg18:
8886 return "DW_OP_reg18";
8887 case DW_OP_reg19:
8888 return "DW_OP_reg19";
8889 case DW_OP_reg20:
8890 return "DW_OP_reg20";
8891 case DW_OP_reg21:
8892 return "DW_OP_reg21";
8893 case DW_OP_reg22:
8894 return "DW_OP_reg22";
8895 case DW_OP_reg23:
8896 return "DW_OP_reg23";
8897 case DW_OP_reg24:
8898 return "DW_OP_reg24";
8899 case DW_OP_reg25:
8900 return "DW_OP_reg25";
8901 case DW_OP_reg26:
8902 return "DW_OP_reg26";
8903 case DW_OP_reg27:
8904 return "DW_OP_reg27";
8905 case DW_OP_reg28:
8906 return "DW_OP_reg28";
8907 case DW_OP_reg29:
8908 return "DW_OP_reg29";
8909 case DW_OP_reg30:
8910 return "DW_OP_reg30";
8911 case DW_OP_reg31:
8912 return "DW_OP_reg31";
8913 case DW_OP_breg0:
8914 return "DW_OP_breg0";
8915 case DW_OP_breg1:
8916 return "DW_OP_breg1";
8917 case DW_OP_breg2:
8918 return "DW_OP_breg2";
8919 case DW_OP_breg3:
8920 return "DW_OP_breg3";
8921 case DW_OP_breg4:
8922 return "DW_OP_breg4";
8923 case DW_OP_breg5:
8924 return "DW_OP_breg5";
8925 case DW_OP_breg6:
8926 return "DW_OP_breg6";
8927 case DW_OP_breg7:
8928 return "DW_OP_breg7";
8929 case DW_OP_breg8:
8930 return "DW_OP_breg8";
8931 case DW_OP_breg9:
8932 return "DW_OP_breg9";
8933 case DW_OP_breg10:
8934 return "DW_OP_breg10";
8935 case DW_OP_breg11:
8936 return "DW_OP_breg11";
8937 case DW_OP_breg12:
8938 return "DW_OP_breg12";
8939 case DW_OP_breg13:
8940 return "DW_OP_breg13";
8941 case DW_OP_breg14:
8942 return "DW_OP_breg14";
8943 case DW_OP_breg15:
8944 return "DW_OP_breg15";
8945 case DW_OP_breg16:
8946 return "DW_OP_breg16";
8947 case DW_OP_breg17:
8948 return "DW_OP_breg17";
8949 case DW_OP_breg18:
8950 return "DW_OP_breg18";
8951 case DW_OP_breg19:
8952 return "DW_OP_breg19";
8953 case DW_OP_breg20:
8954 return "DW_OP_breg20";
8955 case DW_OP_breg21:
8956 return "DW_OP_breg21";
8957 case DW_OP_breg22:
8958 return "DW_OP_breg22";
8959 case DW_OP_breg23:
8960 return "DW_OP_breg23";
8961 case DW_OP_breg24:
8962 return "DW_OP_breg24";
8963 case DW_OP_breg25:
8964 return "DW_OP_breg25";
8965 case DW_OP_breg26:
8966 return "DW_OP_breg26";
8967 case DW_OP_breg27:
8968 return "DW_OP_breg27";
8969 case DW_OP_breg28:
8970 return "DW_OP_breg28";
8971 case DW_OP_breg29:
8972 return "DW_OP_breg29";
8973 case DW_OP_breg30:
8974 return "DW_OP_breg30";
8975 case DW_OP_breg31:
8976 return "DW_OP_breg31";
8977 case DW_OP_regx:
8978 return "DW_OP_regx";
8979 case DW_OP_fbreg:
8980 return "DW_OP_fbreg";
8981 case DW_OP_bregx:
8982 return "DW_OP_bregx";
8983 case DW_OP_piece:
8984 return "DW_OP_piece";
8985 case DW_OP_deref_size:
8986 return "DW_OP_deref_size";
8987 case DW_OP_xderef_size:
8988 return "DW_OP_xderef_size";
8989 case DW_OP_nop:
8990 return "DW_OP_nop";
8991 /* DWARF 3 extensions. */
8992 case DW_OP_push_object_address:
8993 return "DW_OP_push_object_address";
8994 case DW_OP_call2:
8995 return "DW_OP_call2";
8996 case DW_OP_call4:
8997 return "DW_OP_call4";
8998 case DW_OP_call_ref:
8999 return "DW_OP_call_ref";
9000 /* GNU extensions. */
9001 case DW_OP_form_tls_address:
9002 return "DW_OP_form_tls_address";
9003 case DW_OP_call_frame_cfa:
9004 return "DW_OP_call_frame_cfa";
9005 case DW_OP_bit_piece:
9006 return "DW_OP_bit_piece";
9007 case DW_OP_GNU_push_tls_address:
9008 return "DW_OP_GNU_push_tls_address";
9009 case DW_OP_GNU_uninit:
9010 return "DW_OP_GNU_uninit";
9011 /* HP extensions. */
9012 case DW_OP_HP_is_value:
9013 return "DW_OP_HP_is_value";
9014 case DW_OP_HP_fltconst4:
9015 return "DW_OP_HP_fltconst4";
9016 case DW_OP_HP_fltconst8:
9017 return "DW_OP_HP_fltconst8";
9018 case DW_OP_HP_mod_range:
9019 return "DW_OP_HP_mod_range";
9020 case DW_OP_HP_unmod_range:
9021 return "DW_OP_HP_unmod_range";
9022 case DW_OP_HP_tls:
9023 return "DW_OP_HP_tls";
9024 default:
9025 return "OP_<unknown>";
9026 }
9027 }
9028
9029 static char *
9030 dwarf_bool_name (unsigned mybool)
9031 {
9032 if (mybool)
9033 return "TRUE";
9034 else
9035 return "FALSE";
9036 }
9037
9038 /* Convert a DWARF type code into its string name. */
9039
9040 static char *
9041 dwarf_type_encoding_name (unsigned enc)
9042 {
9043 switch (enc)
9044 {
9045 case DW_ATE_void:
9046 return "DW_ATE_void";
9047 case DW_ATE_address:
9048 return "DW_ATE_address";
9049 case DW_ATE_boolean:
9050 return "DW_ATE_boolean";
9051 case DW_ATE_complex_float:
9052 return "DW_ATE_complex_float";
9053 case DW_ATE_float:
9054 return "DW_ATE_float";
9055 case DW_ATE_signed:
9056 return "DW_ATE_signed";
9057 case DW_ATE_signed_char:
9058 return "DW_ATE_signed_char";
9059 case DW_ATE_unsigned:
9060 return "DW_ATE_unsigned";
9061 case DW_ATE_unsigned_char:
9062 return "DW_ATE_unsigned_char";
9063 /* DWARF 3. */
9064 case DW_ATE_imaginary_float:
9065 return "DW_ATE_imaginary_float";
9066 case DW_ATE_packed_decimal:
9067 return "DW_ATE_packed_decimal";
9068 case DW_ATE_numeric_string:
9069 return "DW_ATE_numeric_string";
9070 case DW_ATE_edited:
9071 return "DW_ATE_edited";
9072 case DW_ATE_signed_fixed:
9073 return "DW_ATE_signed_fixed";
9074 case DW_ATE_unsigned_fixed:
9075 return "DW_ATE_unsigned_fixed";
9076 case DW_ATE_decimal_float:
9077 return "DW_ATE_decimal_float";
9078 /* HP extensions. */
9079 case DW_ATE_HP_float80:
9080 return "DW_ATE_HP_float80";
9081 case DW_ATE_HP_complex_float80:
9082 return "DW_ATE_HP_complex_float80";
9083 case DW_ATE_HP_float128:
9084 return "DW_ATE_HP_float128";
9085 case DW_ATE_HP_complex_float128:
9086 return "DW_ATE_HP_complex_float128";
9087 case DW_ATE_HP_floathpintel:
9088 return "DW_ATE_HP_floathpintel";
9089 case DW_ATE_HP_imaginary_float80:
9090 return "DW_ATE_HP_imaginary_float80";
9091 case DW_ATE_HP_imaginary_float128:
9092 return "DW_ATE_HP_imaginary_float128";
9093 default:
9094 return "DW_ATE_<unknown>";
9095 }
9096 }
9097
9098 /* Convert a DWARF call frame info operation to its string name. */
9099
9100 #if 0
9101 static char *
9102 dwarf_cfi_name (unsigned cfi_opc)
9103 {
9104 switch (cfi_opc)
9105 {
9106 case DW_CFA_advance_loc:
9107 return "DW_CFA_advance_loc";
9108 case DW_CFA_offset:
9109 return "DW_CFA_offset";
9110 case DW_CFA_restore:
9111 return "DW_CFA_restore";
9112 case DW_CFA_nop:
9113 return "DW_CFA_nop";
9114 case DW_CFA_set_loc:
9115 return "DW_CFA_set_loc";
9116 case DW_CFA_advance_loc1:
9117 return "DW_CFA_advance_loc1";
9118 case DW_CFA_advance_loc2:
9119 return "DW_CFA_advance_loc2";
9120 case DW_CFA_advance_loc4:
9121 return "DW_CFA_advance_loc4";
9122 case DW_CFA_offset_extended:
9123 return "DW_CFA_offset_extended";
9124 case DW_CFA_restore_extended:
9125 return "DW_CFA_restore_extended";
9126 case DW_CFA_undefined:
9127 return "DW_CFA_undefined";
9128 case DW_CFA_same_value:
9129 return "DW_CFA_same_value";
9130 case DW_CFA_register:
9131 return "DW_CFA_register";
9132 case DW_CFA_remember_state:
9133 return "DW_CFA_remember_state";
9134 case DW_CFA_restore_state:
9135 return "DW_CFA_restore_state";
9136 case DW_CFA_def_cfa:
9137 return "DW_CFA_def_cfa";
9138 case DW_CFA_def_cfa_register:
9139 return "DW_CFA_def_cfa_register";
9140 case DW_CFA_def_cfa_offset:
9141 return "DW_CFA_def_cfa_offset";
9142 /* DWARF 3. */
9143 case DW_CFA_def_cfa_expression:
9144 return "DW_CFA_def_cfa_expression";
9145 case DW_CFA_expression:
9146 return "DW_CFA_expression";
9147 case DW_CFA_offset_extended_sf:
9148 return "DW_CFA_offset_extended_sf";
9149 case DW_CFA_def_cfa_sf:
9150 return "DW_CFA_def_cfa_sf";
9151 case DW_CFA_def_cfa_offset_sf:
9152 return "DW_CFA_def_cfa_offset_sf";
9153 case DW_CFA_val_offset:
9154 return "DW_CFA_val_offset";
9155 case DW_CFA_val_offset_sf:
9156 return "DW_CFA_val_offset_sf";
9157 case DW_CFA_val_expression:
9158 return "DW_CFA_val_expression";
9159 /* SGI/MIPS specific. */
9160 case DW_CFA_MIPS_advance_loc8:
9161 return "DW_CFA_MIPS_advance_loc8";
9162 /* GNU extensions. */
9163 case DW_CFA_GNU_window_save:
9164 return "DW_CFA_GNU_window_save";
9165 case DW_CFA_GNU_args_size:
9166 return "DW_CFA_GNU_args_size";
9167 case DW_CFA_GNU_negative_offset_extended:
9168 return "DW_CFA_GNU_negative_offset_extended";
9169 default:
9170 return "DW_CFA_<unknown>";
9171 }
9172 }
9173 #endif
9174
9175 static void
9176 dump_die (struct die_info *die)
9177 {
9178 unsigned int i;
9179
9180 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
9181 dwarf_tag_name (die->tag), die->abbrev, die->offset);
9182 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
9183 dwarf_bool_name (die->child != NULL));
9184
9185 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
9186 for (i = 0; i < die->num_attrs; ++i)
9187 {
9188 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
9189 dwarf_attr_name (die->attrs[i].name),
9190 dwarf_form_name (die->attrs[i].form));
9191 switch (die->attrs[i].form)
9192 {
9193 case DW_FORM_ref_addr:
9194 case DW_FORM_addr:
9195 fprintf_unfiltered (gdb_stderr, "address: ");
9196 fputs_filtered (paddress (DW_ADDR (&die->attrs[i])), gdb_stderr);
9197 break;
9198 case DW_FORM_block2:
9199 case DW_FORM_block4:
9200 case DW_FORM_block:
9201 case DW_FORM_block1:
9202 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
9203 break;
9204 case DW_FORM_ref1:
9205 case DW_FORM_ref2:
9206 case DW_FORM_ref4:
9207 fprintf_unfiltered (gdb_stderr, "constant ref: %ld (adjusted)",
9208 (long) (DW_ADDR (&die->attrs[i])));
9209 break;
9210 case DW_FORM_data1:
9211 case DW_FORM_data2:
9212 case DW_FORM_data4:
9213 case DW_FORM_data8:
9214 case DW_FORM_udata:
9215 case DW_FORM_sdata:
9216 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
9217 break;
9218 case DW_FORM_string:
9219 case DW_FORM_strp:
9220 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
9221 DW_STRING (&die->attrs[i])
9222 ? DW_STRING (&die->attrs[i]) : "");
9223 break;
9224 case DW_FORM_flag:
9225 if (DW_UNSND (&die->attrs[i]))
9226 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
9227 else
9228 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
9229 break;
9230 case DW_FORM_indirect:
9231 /* the reader will have reduced the indirect form to
9232 the "base form" so this form should not occur */
9233 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
9234 break;
9235 default:
9236 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
9237 die->attrs[i].form);
9238 }
9239 fprintf_unfiltered (gdb_stderr, "\n");
9240 }
9241 }
9242
9243 static void
9244 dump_die_list (struct die_info *die)
9245 {
9246 while (die)
9247 {
9248 dump_die (die);
9249 if (die->child != NULL)
9250 dump_die_list (die->child);
9251 if (die->sibling != NULL)
9252 dump_die_list (die->sibling);
9253 }
9254 }
9255
9256 static void
9257 store_in_ref_table (unsigned int offset, struct die_info *die,
9258 struct dwarf2_cu *cu)
9259 {
9260 int h;
9261 struct die_info *old;
9262
9263 h = (offset % REF_HASH_SIZE);
9264 old = cu->die_ref_table[h];
9265 die->next_ref = old;
9266 cu->die_ref_table[h] = die;
9267 }
9268
9269 static unsigned int
9270 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
9271 {
9272 unsigned int result = 0;
9273
9274 switch (attr->form)
9275 {
9276 case DW_FORM_ref_addr:
9277 case DW_FORM_ref1:
9278 case DW_FORM_ref2:
9279 case DW_FORM_ref4:
9280 case DW_FORM_ref8:
9281 case DW_FORM_ref_udata:
9282 result = DW_ADDR (attr);
9283 break;
9284 default:
9285 complaint (&symfile_complaints,
9286 _("unsupported die ref attribute form: '%s'"),
9287 dwarf_form_name (attr->form));
9288 }
9289 return result;
9290 }
9291
9292 /* Return the constant value held by the given attribute. Return -1
9293 if the value held by the attribute is not constant. */
9294
9295 static int
9296 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
9297 {
9298 if (attr->form == DW_FORM_sdata)
9299 return DW_SND (attr);
9300 else if (attr->form == DW_FORM_udata
9301 || attr->form == DW_FORM_data1
9302 || attr->form == DW_FORM_data2
9303 || attr->form == DW_FORM_data4
9304 || attr->form == DW_FORM_data8)
9305 return DW_UNSND (attr);
9306 else
9307 {
9308 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
9309 dwarf_form_name (attr->form));
9310 return default_value;
9311 }
9312 }
9313
9314 static struct die_info *
9315 follow_die_ref (struct die_info *src_die, struct attribute *attr,
9316 struct dwarf2_cu *cu)
9317 {
9318 struct die_info *die;
9319 unsigned int offset;
9320 int h;
9321 struct die_info temp_die;
9322 struct dwarf2_cu *target_cu;
9323
9324 offset = dwarf2_get_ref_die_offset (attr, cu);
9325
9326 if (DW_ADDR (attr) < cu->header.offset
9327 || DW_ADDR (attr) >= cu->header.offset + cu->header.length)
9328 {
9329 struct dwarf2_per_cu_data *per_cu;
9330 per_cu = dwarf2_find_containing_comp_unit (DW_ADDR (attr),
9331 cu->objfile);
9332 target_cu = per_cu->cu;
9333 }
9334 else
9335 target_cu = cu;
9336
9337 h = (offset % REF_HASH_SIZE);
9338 die = target_cu->die_ref_table[h];
9339 while (die)
9340 {
9341 if (die->offset == offset)
9342 return die;
9343 die = die->next_ref;
9344 }
9345
9346 error (_("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
9347 "at 0x%lx [in module %s]"),
9348 (long) src_die->offset, (long) offset, cu->objfile->name);
9349
9350 return NULL;
9351 }
9352
9353 /* Decode simple location descriptions.
9354 Given a pointer to a dwarf block that defines a location, compute
9355 the location and return the value.
9356
9357 NOTE drow/2003-11-18: This function is called in two situations
9358 now: for the address of static or global variables (partial symbols
9359 only) and for offsets into structures which are expected to be
9360 (more or less) constant. The partial symbol case should go away,
9361 and only the constant case should remain. That will let this
9362 function complain more accurately. A few special modes are allowed
9363 without complaint for global variables (for instance, global
9364 register values and thread-local values).
9365
9366 A location description containing no operations indicates that the
9367 object is optimized out. The return value is 0 for that case.
9368 FIXME drow/2003-11-16: No callers check for this case any more; soon all
9369 callers will only want a very basic result and this can become a
9370 complaint.
9371
9372 Note that stack[0] is unused except as a default error return.
9373 Note that stack overflow is not yet handled. */
9374
9375 static CORE_ADDR
9376 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
9377 {
9378 struct objfile *objfile = cu->objfile;
9379 struct comp_unit_head *cu_header = &cu->header;
9380 int i;
9381 int size = blk->size;
9382 gdb_byte *data = blk->data;
9383 CORE_ADDR stack[64];
9384 int stacki;
9385 unsigned int bytes_read, unsnd;
9386 gdb_byte op;
9387
9388 i = 0;
9389 stacki = 0;
9390 stack[stacki] = 0;
9391
9392 while (i < size)
9393 {
9394 op = data[i++];
9395 switch (op)
9396 {
9397 case DW_OP_lit0:
9398 case DW_OP_lit1:
9399 case DW_OP_lit2:
9400 case DW_OP_lit3:
9401 case DW_OP_lit4:
9402 case DW_OP_lit5:
9403 case DW_OP_lit6:
9404 case DW_OP_lit7:
9405 case DW_OP_lit8:
9406 case DW_OP_lit9:
9407 case DW_OP_lit10:
9408 case DW_OP_lit11:
9409 case DW_OP_lit12:
9410 case DW_OP_lit13:
9411 case DW_OP_lit14:
9412 case DW_OP_lit15:
9413 case DW_OP_lit16:
9414 case DW_OP_lit17:
9415 case DW_OP_lit18:
9416 case DW_OP_lit19:
9417 case DW_OP_lit20:
9418 case DW_OP_lit21:
9419 case DW_OP_lit22:
9420 case DW_OP_lit23:
9421 case DW_OP_lit24:
9422 case DW_OP_lit25:
9423 case DW_OP_lit26:
9424 case DW_OP_lit27:
9425 case DW_OP_lit28:
9426 case DW_OP_lit29:
9427 case DW_OP_lit30:
9428 case DW_OP_lit31:
9429 stack[++stacki] = op - DW_OP_lit0;
9430 break;
9431
9432 case DW_OP_reg0:
9433 case DW_OP_reg1:
9434 case DW_OP_reg2:
9435 case DW_OP_reg3:
9436 case DW_OP_reg4:
9437 case DW_OP_reg5:
9438 case DW_OP_reg6:
9439 case DW_OP_reg7:
9440 case DW_OP_reg8:
9441 case DW_OP_reg9:
9442 case DW_OP_reg10:
9443 case DW_OP_reg11:
9444 case DW_OP_reg12:
9445 case DW_OP_reg13:
9446 case DW_OP_reg14:
9447 case DW_OP_reg15:
9448 case DW_OP_reg16:
9449 case DW_OP_reg17:
9450 case DW_OP_reg18:
9451 case DW_OP_reg19:
9452 case DW_OP_reg20:
9453 case DW_OP_reg21:
9454 case DW_OP_reg22:
9455 case DW_OP_reg23:
9456 case DW_OP_reg24:
9457 case DW_OP_reg25:
9458 case DW_OP_reg26:
9459 case DW_OP_reg27:
9460 case DW_OP_reg28:
9461 case DW_OP_reg29:
9462 case DW_OP_reg30:
9463 case DW_OP_reg31:
9464 stack[++stacki] = op - DW_OP_reg0;
9465 if (i < size)
9466 dwarf2_complex_location_expr_complaint ();
9467 break;
9468
9469 case DW_OP_regx:
9470 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9471 i += bytes_read;
9472 stack[++stacki] = unsnd;
9473 if (i < size)
9474 dwarf2_complex_location_expr_complaint ();
9475 break;
9476
9477 case DW_OP_addr:
9478 stack[++stacki] = read_address (objfile->obfd, &data[i],
9479 cu, &bytes_read);
9480 i += bytes_read;
9481 break;
9482
9483 case DW_OP_const1u:
9484 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
9485 i += 1;
9486 break;
9487
9488 case DW_OP_const1s:
9489 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
9490 i += 1;
9491 break;
9492
9493 case DW_OP_const2u:
9494 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
9495 i += 2;
9496 break;
9497
9498 case DW_OP_const2s:
9499 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
9500 i += 2;
9501 break;
9502
9503 case DW_OP_const4u:
9504 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
9505 i += 4;
9506 break;
9507
9508 case DW_OP_const4s:
9509 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
9510 i += 4;
9511 break;
9512
9513 case DW_OP_constu:
9514 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
9515 &bytes_read);
9516 i += bytes_read;
9517 break;
9518
9519 case DW_OP_consts:
9520 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
9521 i += bytes_read;
9522 break;
9523
9524 case DW_OP_dup:
9525 stack[stacki + 1] = stack[stacki];
9526 stacki++;
9527 break;
9528
9529 case DW_OP_plus:
9530 stack[stacki - 1] += stack[stacki];
9531 stacki--;
9532 break;
9533
9534 case DW_OP_plus_uconst:
9535 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9536 i += bytes_read;
9537 break;
9538
9539 case DW_OP_minus:
9540 stack[stacki - 1] -= stack[stacki];
9541 stacki--;
9542 break;
9543
9544 case DW_OP_deref:
9545 /* If we're not the last op, then we definitely can't encode
9546 this using GDB's address_class enum. This is valid for partial
9547 global symbols, although the variable's address will be bogus
9548 in the psymtab. */
9549 if (i < size)
9550 dwarf2_complex_location_expr_complaint ();
9551 break;
9552
9553 case DW_OP_GNU_push_tls_address:
9554 /* The top of the stack has the offset from the beginning
9555 of the thread control block at which the variable is located. */
9556 /* Nothing should follow this operator, so the top of stack would
9557 be returned. */
9558 /* This is valid for partial global symbols, but the variable's
9559 address will be bogus in the psymtab. */
9560 if (i < size)
9561 dwarf2_complex_location_expr_complaint ();
9562 break;
9563
9564 case DW_OP_GNU_uninit:
9565 break;
9566
9567 default:
9568 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9569 dwarf_stack_op_name (op));
9570 return (stack[stacki]);
9571 }
9572 }
9573 return (stack[stacki]);
9574 }
9575
9576 /* memory allocation interface */
9577
9578 static struct dwarf_block *
9579 dwarf_alloc_block (struct dwarf2_cu *cu)
9580 {
9581 struct dwarf_block *blk;
9582
9583 blk = (struct dwarf_block *)
9584 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
9585 return (blk);
9586 }
9587
9588 static struct abbrev_info *
9589 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
9590 {
9591 struct abbrev_info *abbrev;
9592
9593 abbrev = (struct abbrev_info *)
9594 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
9595 memset (abbrev, 0, sizeof (struct abbrev_info));
9596 return (abbrev);
9597 }
9598
9599 static struct die_info *
9600 dwarf_alloc_die (void)
9601 {
9602 struct die_info *die;
9603
9604 die = (struct die_info *) xmalloc (sizeof (struct die_info));
9605 memset (die, 0, sizeof (struct die_info));
9606 return (die);
9607 }
9608
9609 \f
9610 /* Macro support. */
9611
9612
9613 /* Return the full name of file number I in *LH's file name table.
9614 Use COMP_DIR as the name of the current directory of the
9615 compilation. The result is allocated using xmalloc; the caller is
9616 responsible for freeing it. */
9617 static char *
9618 file_full_name (int file, struct line_header *lh, const char *comp_dir)
9619 {
9620 /* Is the file number a valid index into the line header's file name
9621 table? Remember that file numbers start with one, not zero. */
9622 if (1 <= file && file <= lh->num_file_names)
9623 {
9624 struct file_entry *fe = &lh->file_names[file - 1];
9625
9626 if (IS_ABSOLUTE_PATH (fe->name))
9627 return xstrdup (fe->name);
9628 else
9629 {
9630 const char *dir;
9631 int dir_len;
9632 char *full_name;
9633
9634 if (fe->dir_index)
9635 dir = lh->include_dirs[fe->dir_index - 1];
9636 else
9637 dir = comp_dir;
9638
9639 if (dir)
9640 {
9641 dir_len = strlen (dir);
9642 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
9643 strcpy (full_name, dir);
9644 full_name[dir_len] = '/';
9645 strcpy (full_name + dir_len + 1, fe->name);
9646 return full_name;
9647 }
9648 else
9649 return xstrdup (fe->name);
9650 }
9651 }
9652 else
9653 {
9654 /* The compiler produced a bogus file number. We can at least
9655 record the macro definitions made in the file, even if we
9656 won't be able to find the file by name. */
9657 char fake_name[80];
9658 sprintf (fake_name, "<bad macro file number %d>", file);
9659
9660 complaint (&symfile_complaints,
9661 _("bad file number in macro information (%d)"),
9662 file);
9663
9664 return xstrdup (fake_name);
9665 }
9666 }
9667
9668
9669 static struct macro_source_file *
9670 macro_start_file (int file, int line,
9671 struct macro_source_file *current_file,
9672 const char *comp_dir,
9673 struct line_header *lh, struct objfile *objfile)
9674 {
9675 /* The full name of this source file. */
9676 char *full_name = file_full_name (file, lh, comp_dir);
9677
9678 /* We don't create a macro table for this compilation unit
9679 at all until we actually get a filename. */
9680 if (! pending_macros)
9681 pending_macros = new_macro_table (&objfile->objfile_obstack,
9682 objfile->macro_cache);
9683
9684 if (! current_file)
9685 /* If we have no current file, then this must be the start_file
9686 directive for the compilation unit's main source file. */
9687 current_file = macro_set_main (pending_macros, full_name);
9688 else
9689 current_file = macro_include (current_file, line, full_name);
9690
9691 xfree (full_name);
9692
9693 return current_file;
9694 }
9695
9696
9697 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
9698 followed by a null byte. */
9699 static char *
9700 copy_string (const char *buf, int len)
9701 {
9702 char *s = xmalloc (len + 1);
9703 memcpy (s, buf, len);
9704 s[len] = '\0';
9705
9706 return s;
9707 }
9708
9709
9710 static const char *
9711 consume_improper_spaces (const char *p, const char *body)
9712 {
9713 if (*p == ' ')
9714 {
9715 complaint (&symfile_complaints,
9716 _("macro definition contains spaces in formal argument list:\n`%s'"),
9717 body);
9718
9719 while (*p == ' ')
9720 p++;
9721 }
9722
9723 return p;
9724 }
9725
9726
9727 static void
9728 parse_macro_definition (struct macro_source_file *file, int line,
9729 const char *body)
9730 {
9731 const char *p;
9732
9733 /* The body string takes one of two forms. For object-like macro
9734 definitions, it should be:
9735
9736 <macro name> " " <definition>
9737
9738 For function-like macro definitions, it should be:
9739
9740 <macro name> "() " <definition>
9741 or
9742 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
9743
9744 Spaces may appear only where explicitly indicated, and in the
9745 <definition>.
9746
9747 The Dwarf 2 spec says that an object-like macro's name is always
9748 followed by a space, but versions of GCC around March 2002 omit
9749 the space when the macro's definition is the empty string.
9750
9751 The Dwarf 2 spec says that there should be no spaces between the
9752 formal arguments in a function-like macro's formal argument list,
9753 but versions of GCC around March 2002 include spaces after the
9754 commas. */
9755
9756
9757 /* Find the extent of the macro name. The macro name is terminated
9758 by either a space or null character (for an object-like macro) or
9759 an opening paren (for a function-like macro). */
9760 for (p = body; *p; p++)
9761 if (*p == ' ' || *p == '(')
9762 break;
9763
9764 if (*p == ' ' || *p == '\0')
9765 {
9766 /* It's an object-like macro. */
9767 int name_len = p - body;
9768 char *name = copy_string (body, name_len);
9769 const char *replacement;
9770
9771 if (*p == ' ')
9772 replacement = body + name_len + 1;
9773 else
9774 {
9775 dwarf2_macro_malformed_definition_complaint (body);
9776 replacement = body + name_len;
9777 }
9778
9779 macro_define_object (file, line, name, replacement);
9780
9781 xfree (name);
9782 }
9783 else if (*p == '(')
9784 {
9785 /* It's a function-like macro. */
9786 char *name = copy_string (body, p - body);
9787 int argc = 0;
9788 int argv_size = 1;
9789 char **argv = xmalloc (argv_size * sizeof (*argv));
9790
9791 p++;
9792
9793 p = consume_improper_spaces (p, body);
9794
9795 /* Parse the formal argument list. */
9796 while (*p && *p != ')')
9797 {
9798 /* Find the extent of the current argument name. */
9799 const char *arg_start = p;
9800
9801 while (*p && *p != ',' && *p != ')' && *p != ' ')
9802 p++;
9803
9804 if (! *p || p == arg_start)
9805 dwarf2_macro_malformed_definition_complaint (body);
9806 else
9807 {
9808 /* Make sure argv has room for the new argument. */
9809 if (argc >= argv_size)
9810 {
9811 argv_size *= 2;
9812 argv = xrealloc (argv, argv_size * sizeof (*argv));
9813 }
9814
9815 argv[argc++] = copy_string (arg_start, p - arg_start);
9816 }
9817
9818 p = consume_improper_spaces (p, body);
9819
9820 /* Consume the comma, if present. */
9821 if (*p == ',')
9822 {
9823 p++;
9824
9825 p = consume_improper_spaces (p, body);
9826 }
9827 }
9828
9829 if (*p == ')')
9830 {
9831 p++;
9832
9833 if (*p == ' ')
9834 /* Perfectly formed definition, no complaints. */
9835 macro_define_function (file, line, name,
9836 argc, (const char **) argv,
9837 p + 1);
9838 else if (*p == '\0')
9839 {
9840 /* Complain, but do define it. */
9841 dwarf2_macro_malformed_definition_complaint (body);
9842 macro_define_function (file, line, name,
9843 argc, (const char **) argv,
9844 p);
9845 }
9846 else
9847 /* Just complain. */
9848 dwarf2_macro_malformed_definition_complaint (body);
9849 }
9850 else
9851 /* Just complain. */
9852 dwarf2_macro_malformed_definition_complaint (body);
9853
9854 xfree (name);
9855 {
9856 int i;
9857
9858 for (i = 0; i < argc; i++)
9859 xfree (argv[i]);
9860 }
9861 xfree (argv);
9862 }
9863 else
9864 dwarf2_macro_malformed_definition_complaint (body);
9865 }
9866
9867
9868 static void
9869 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
9870 char *comp_dir, bfd *abfd,
9871 struct dwarf2_cu *cu)
9872 {
9873 gdb_byte *mac_ptr, *mac_end;
9874 struct macro_source_file *current_file = 0;
9875
9876 if (dwarf2_per_objfile->macinfo_buffer == NULL)
9877 {
9878 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
9879 return;
9880 }
9881
9882 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
9883 mac_end = dwarf2_per_objfile->macinfo_buffer
9884 + dwarf2_per_objfile->macinfo_size;
9885
9886 for (;;)
9887 {
9888 enum dwarf_macinfo_record_type macinfo_type;
9889
9890 /* Do we at least have room for a macinfo type byte? */
9891 if (mac_ptr >= mac_end)
9892 {
9893 dwarf2_macros_too_long_complaint ();
9894 return;
9895 }
9896
9897 macinfo_type = read_1_byte (abfd, mac_ptr);
9898 mac_ptr++;
9899
9900 switch (macinfo_type)
9901 {
9902 /* A zero macinfo type indicates the end of the macro
9903 information. */
9904 case 0:
9905 return;
9906
9907 case DW_MACINFO_define:
9908 case DW_MACINFO_undef:
9909 {
9910 unsigned int bytes_read;
9911 int line;
9912 char *body;
9913
9914 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9915 mac_ptr += bytes_read;
9916 body = read_string (abfd, mac_ptr, &bytes_read);
9917 mac_ptr += bytes_read;
9918
9919 if (! current_file)
9920 complaint (&symfile_complaints,
9921 _("debug info gives macro %s outside of any file: %s"),
9922 macinfo_type ==
9923 DW_MACINFO_define ? "definition" : macinfo_type ==
9924 DW_MACINFO_undef ? "undefinition" :
9925 "something-or-other", body);
9926 else
9927 {
9928 if (macinfo_type == DW_MACINFO_define)
9929 parse_macro_definition (current_file, line, body);
9930 else if (macinfo_type == DW_MACINFO_undef)
9931 macro_undef (current_file, line, body);
9932 }
9933 }
9934 break;
9935
9936 case DW_MACINFO_start_file:
9937 {
9938 unsigned int bytes_read;
9939 int line, file;
9940
9941 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9942 mac_ptr += bytes_read;
9943 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9944 mac_ptr += bytes_read;
9945
9946 current_file = macro_start_file (file, line,
9947 current_file, comp_dir,
9948 lh, cu->objfile);
9949 }
9950 break;
9951
9952 case DW_MACINFO_end_file:
9953 if (! current_file)
9954 complaint (&symfile_complaints,
9955 _("macro debug info has an unmatched `close_file' directive"));
9956 else
9957 {
9958 current_file = current_file->included_by;
9959 if (! current_file)
9960 {
9961 enum dwarf_macinfo_record_type next_type;
9962
9963 /* GCC circa March 2002 doesn't produce the zero
9964 type byte marking the end of the compilation
9965 unit. Complain if it's not there, but exit no
9966 matter what. */
9967
9968 /* Do we at least have room for a macinfo type byte? */
9969 if (mac_ptr >= mac_end)
9970 {
9971 dwarf2_macros_too_long_complaint ();
9972 return;
9973 }
9974
9975 /* We don't increment mac_ptr here, so this is just
9976 a look-ahead. */
9977 next_type = read_1_byte (abfd, mac_ptr);
9978 if (next_type != 0)
9979 complaint (&symfile_complaints,
9980 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
9981
9982 return;
9983 }
9984 }
9985 break;
9986
9987 case DW_MACINFO_vendor_ext:
9988 {
9989 unsigned int bytes_read;
9990 int constant;
9991 char *string;
9992
9993 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9994 mac_ptr += bytes_read;
9995 string = read_string (abfd, mac_ptr, &bytes_read);
9996 mac_ptr += bytes_read;
9997
9998 /* We don't recognize any vendor extensions. */
9999 }
10000 break;
10001 }
10002 }
10003 }
10004
10005 /* Check if the attribute's form is a DW_FORM_block*
10006 if so return true else false. */
10007 static int
10008 attr_form_is_block (struct attribute *attr)
10009 {
10010 return (attr == NULL ? 0 :
10011 attr->form == DW_FORM_block1
10012 || attr->form == DW_FORM_block2
10013 || attr->form == DW_FORM_block4
10014 || attr->form == DW_FORM_block);
10015 }
10016
10017 /* Return non-zero if ATTR's value is a section offset --- classes
10018 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
10019 You may use DW_UNSND (attr) to retrieve such offsets.
10020
10021 Section 7.5.4, "Attribute Encodings", explains that no attribute
10022 may have a value that belongs to more than one of these classes; it
10023 would be ambiguous if we did, because we use the same forms for all
10024 of them. */
10025 static int
10026 attr_form_is_section_offset (struct attribute *attr)
10027 {
10028 return (attr->form == DW_FORM_data4
10029 || attr->form == DW_FORM_data8);
10030 }
10031
10032
10033 /* Return non-zero if ATTR's value falls in the 'constant' class, or
10034 zero otherwise. When this function returns true, you can apply
10035 dwarf2_get_attr_constant_value to it.
10036
10037 However, note that for some attributes you must check
10038 attr_form_is_section_offset before using this test. DW_FORM_data4
10039 and DW_FORM_data8 are members of both the constant class, and of
10040 the classes that contain offsets into other debug sections
10041 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
10042 that, if an attribute's can be either a constant or one of the
10043 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
10044 taken as section offsets, not constants. */
10045 static int
10046 attr_form_is_constant (struct attribute *attr)
10047 {
10048 switch (attr->form)
10049 {
10050 case DW_FORM_sdata:
10051 case DW_FORM_udata:
10052 case DW_FORM_data1:
10053 case DW_FORM_data2:
10054 case DW_FORM_data4:
10055 case DW_FORM_data8:
10056 return 1;
10057 default:
10058 return 0;
10059 }
10060 }
10061
10062 static void
10063 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
10064 struct dwarf2_cu *cu)
10065 {
10066 if (attr_form_is_section_offset (attr)
10067 /* ".debug_loc" may not exist at all, or the offset may be outside
10068 the section. If so, fall through to the complaint in the
10069 other branch. */
10070 && DW_UNSND (attr) < dwarf2_per_objfile->loc_size)
10071 {
10072 struct dwarf2_loclist_baton *baton;
10073
10074 baton = obstack_alloc (&cu->objfile->objfile_obstack,
10075 sizeof (struct dwarf2_loclist_baton));
10076 baton->per_cu = cu->per_cu;
10077 gdb_assert (baton->per_cu);
10078
10079 /* We don't know how long the location list is, but make sure we
10080 don't run off the edge of the section. */
10081 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
10082 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
10083 baton->base_address = cu->header.base_address;
10084 if (cu->header.base_known == 0)
10085 complaint (&symfile_complaints,
10086 _("Location list used without specifying the CU base address."));
10087
10088 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
10089 SYMBOL_LOCATION_BATON (sym) = baton;
10090 }
10091 else
10092 {
10093 struct dwarf2_locexpr_baton *baton;
10094
10095 baton = obstack_alloc (&cu->objfile->objfile_obstack,
10096 sizeof (struct dwarf2_locexpr_baton));
10097 baton->per_cu = cu->per_cu;
10098 gdb_assert (baton->per_cu);
10099
10100 if (attr_form_is_block (attr))
10101 {
10102 /* Note that we're just copying the block's data pointer
10103 here, not the actual data. We're still pointing into the
10104 info_buffer for SYM's objfile; right now we never release
10105 that buffer, but when we do clean up properly this may
10106 need to change. */
10107 baton->size = DW_BLOCK (attr)->size;
10108 baton->data = DW_BLOCK (attr)->data;
10109 }
10110 else
10111 {
10112 dwarf2_invalid_attrib_class_complaint ("location description",
10113 SYMBOL_NATURAL_NAME (sym));
10114 baton->size = 0;
10115 baton->data = NULL;
10116 }
10117
10118 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
10119 SYMBOL_LOCATION_BATON (sym) = baton;
10120 }
10121 }
10122
10123 /* Return the OBJFILE associated with the compilation unit CU. */
10124
10125 struct objfile *
10126 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
10127 {
10128 struct objfile *objfile = per_cu->psymtab->objfile;
10129
10130 /* Return the master objfile, so that we can report and look up the
10131 correct file containing this variable. */
10132 if (objfile->separate_debug_objfile_backlink)
10133 objfile = objfile->separate_debug_objfile_backlink;
10134
10135 return objfile;
10136 }
10137
10138 /* Return the address size given in the compilation unit header for CU. */
10139
10140 CORE_ADDR
10141 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
10142 {
10143 if (per_cu->cu)
10144 return per_cu->cu->header.addr_size;
10145 else
10146 {
10147 /* If the CU is not currently read in, we re-read its header. */
10148 struct objfile *objfile = per_cu->psymtab->objfile;
10149 struct dwarf2_per_objfile *per_objfile
10150 = objfile_data (objfile, dwarf2_objfile_data_key);
10151 gdb_byte *info_ptr = per_objfile->info_buffer + per_cu->offset;
10152
10153 struct comp_unit_head cu_header;
10154 memset (&cu_header, 0, sizeof cu_header);
10155 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
10156 return cu_header.addr_size;
10157 }
10158 }
10159
10160 /* Locate the compilation unit from CU's objfile which contains the
10161 DIE at OFFSET. Raises an error on failure. */
10162
10163 static struct dwarf2_per_cu_data *
10164 dwarf2_find_containing_comp_unit (unsigned long offset,
10165 struct objfile *objfile)
10166 {
10167 struct dwarf2_per_cu_data *this_cu;
10168 int low, high;
10169
10170 low = 0;
10171 high = dwarf2_per_objfile->n_comp_units - 1;
10172 while (high > low)
10173 {
10174 int mid = low + (high - low) / 2;
10175 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
10176 high = mid;
10177 else
10178 low = mid + 1;
10179 }
10180 gdb_assert (low == high);
10181 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
10182 {
10183 if (low == 0)
10184 error (_("Dwarf Error: could not find partial DIE containing "
10185 "offset 0x%lx [in module %s]"),
10186 (long) offset, bfd_get_filename (objfile->obfd));
10187
10188 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
10189 return dwarf2_per_objfile->all_comp_units[low-1];
10190 }
10191 else
10192 {
10193 this_cu = dwarf2_per_objfile->all_comp_units[low];
10194 if (low == dwarf2_per_objfile->n_comp_units - 1
10195 && offset >= this_cu->offset + this_cu->length)
10196 error (_("invalid dwarf2 offset %ld"), offset);
10197 gdb_assert (offset < this_cu->offset + this_cu->length);
10198 return this_cu;
10199 }
10200 }
10201
10202 /* Locate the compilation unit from OBJFILE which is located at exactly
10203 OFFSET. Raises an error on failure. */
10204
10205 static struct dwarf2_per_cu_data *
10206 dwarf2_find_comp_unit (unsigned long offset, struct objfile *objfile)
10207 {
10208 struct dwarf2_per_cu_data *this_cu;
10209 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10210 if (this_cu->offset != offset)
10211 error (_("no compilation unit with offset %ld."), offset);
10212 return this_cu;
10213 }
10214
10215 /* Release one cached compilation unit, CU. We unlink it from the tree
10216 of compilation units, but we don't remove it from the read_in_chain;
10217 the caller is responsible for that. */
10218
10219 static void
10220 free_one_comp_unit (void *data)
10221 {
10222 struct dwarf2_cu *cu = data;
10223
10224 if (cu->per_cu != NULL)
10225 cu->per_cu->cu = NULL;
10226 cu->per_cu = NULL;
10227
10228 obstack_free (&cu->comp_unit_obstack, NULL);
10229 if (cu->dies)
10230 free_die_list (cu->dies);
10231
10232 xfree (cu);
10233 }
10234
10235 /* This cleanup function is passed the address of a dwarf2_cu on the stack
10236 when we're finished with it. We can't free the pointer itself, but be
10237 sure to unlink it from the cache. Also release any associated storage
10238 and perform cache maintenance.
10239
10240 Only used during partial symbol parsing. */
10241
10242 static void
10243 free_stack_comp_unit (void *data)
10244 {
10245 struct dwarf2_cu *cu = data;
10246
10247 obstack_free (&cu->comp_unit_obstack, NULL);
10248 cu->partial_dies = NULL;
10249
10250 if (cu->per_cu != NULL)
10251 {
10252 /* This compilation unit is on the stack in our caller, so we
10253 should not xfree it. Just unlink it. */
10254 cu->per_cu->cu = NULL;
10255 cu->per_cu = NULL;
10256
10257 /* If we had a per-cu pointer, then we may have other compilation
10258 units loaded, so age them now. */
10259 age_cached_comp_units ();
10260 }
10261 }
10262
10263 /* Free all cached compilation units. */
10264
10265 static void
10266 free_cached_comp_units (void *data)
10267 {
10268 struct dwarf2_per_cu_data *per_cu, **last_chain;
10269
10270 per_cu = dwarf2_per_objfile->read_in_chain;
10271 last_chain = &dwarf2_per_objfile->read_in_chain;
10272 while (per_cu != NULL)
10273 {
10274 struct dwarf2_per_cu_data *next_cu;
10275
10276 next_cu = per_cu->cu->read_in_chain;
10277
10278 free_one_comp_unit (per_cu->cu);
10279 *last_chain = next_cu;
10280
10281 per_cu = next_cu;
10282 }
10283 }
10284
10285 /* Increase the age counter on each cached compilation unit, and free
10286 any that are too old. */
10287
10288 static void
10289 age_cached_comp_units (void)
10290 {
10291 struct dwarf2_per_cu_data *per_cu, **last_chain;
10292
10293 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
10294 per_cu = dwarf2_per_objfile->read_in_chain;
10295 while (per_cu != NULL)
10296 {
10297 per_cu->cu->last_used ++;
10298 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
10299 dwarf2_mark (per_cu->cu);
10300 per_cu = per_cu->cu->read_in_chain;
10301 }
10302
10303 per_cu = dwarf2_per_objfile->read_in_chain;
10304 last_chain = &dwarf2_per_objfile->read_in_chain;
10305 while (per_cu != NULL)
10306 {
10307 struct dwarf2_per_cu_data *next_cu;
10308
10309 next_cu = per_cu->cu->read_in_chain;
10310
10311 if (!per_cu->cu->mark)
10312 {
10313 free_one_comp_unit (per_cu->cu);
10314 *last_chain = next_cu;
10315 }
10316 else
10317 last_chain = &per_cu->cu->read_in_chain;
10318
10319 per_cu = next_cu;
10320 }
10321 }
10322
10323 /* Remove a single compilation unit from the cache. */
10324
10325 static void
10326 free_one_cached_comp_unit (void *target_cu)
10327 {
10328 struct dwarf2_per_cu_data *per_cu, **last_chain;
10329
10330 per_cu = dwarf2_per_objfile->read_in_chain;
10331 last_chain = &dwarf2_per_objfile->read_in_chain;
10332 while (per_cu != NULL)
10333 {
10334 struct dwarf2_per_cu_data *next_cu;
10335
10336 next_cu = per_cu->cu->read_in_chain;
10337
10338 if (per_cu->cu == target_cu)
10339 {
10340 free_one_comp_unit (per_cu->cu);
10341 *last_chain = next_cu;
10342 break;
10343 }
10344 else
10345 last_chain = &per_cu->cu->read_in_chain;
10346
10347 per_cu = next_cu;
10348 }
10349 }
10350
10351 /* Release all extra memory associated with OBJFILE. */
10352
10353 void
10354 dwarf2_free_objfile (struct objfile *objfile)
10355 {
10356 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10357
10358 if (dwarf2_per_objfile == NULL)
10359 return;
10360
10361 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
10362 free_cached_comp_units (NULL);
10363
10364 /* Everything else should be on the objfile obstack. */
10365 }
10366
10367 /* A pair of DIE offset and GDB type pointer. We store these
10368 in a hash table separate from the DIEs, and preserve them
10369 when the DIEs are flushed out of cache. */
10370
10371 struct dwarf2_offset_and_type
10372 {
10373 unsigned int offset;
10374 struct type *type;
10375 };
10376
10377 /* Hash function for a dwarf2_offset_and_type. */
10378
10379 static hashval_t
10380 offset_and_type_hash (const void *item)
10381 {
10382 const struct dwarf2_offset_and_type *ofs = item;
10383 return ofs->offset;
10384 }
10385
10386 /* Equality function for a dwarf2_offset_and_type. */
10387
10388 static int
10389 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
10390 {
10391 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
10392 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
10393 return ofs_lhs->offset == ofs_rhs->offset;
10394 }
10395
10396 /* Set the type associated with DIE to TYPE. Save it in CU's hash
10397 table if necessary. */
10398
10399 static void
10400 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10401 {
10402 struct dwarf2_offset_and_type **slot, ofs;
10403
10404 die->type = type;
10405
10406 if (cu->per_cu == NULL)
10407 return;
10408
10409 if (cu->per_cu->type_hash == NULL)
10410 cu->per_cu->type_hash
10411 = htab_create_alloc_ex (cu->header.length / 24,
10412 offset_and_type_hash,
10413 offset_and_type_eq,
10414 NULL,
10415 &cu->objfile->objfile_obstack,
10416 hashtab_obstack_allocate,
10417 dummy_obstack_deallocate);
10418
10419 ofs.offset = die->offset;
10420 ofs.type = type;
10421 slot = (struct dwarf2_offset_and_type **)
10422 htab_find_slot_with_hash (cu->per_cu->type_hash, &ofs, ofs.offset, INSERT);
10423 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
10424 **slot = ofs;
10425 }
10426
10427 /* Find the type for DIE in TYPE_HASH, or return NULL if DIE does not
10428 have a saved type. */
10429
10430 static struct type *
10431 get_die_type (struct die_info *die, htab_t type_hash)
10432 {
10433 struct dwarf2_offset_and_type *slot, ofs;
10434
10435 ofs.offset = die->offset;
10436 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
10437 if (slot)
10438 return slot->type;
10439 else
10440 return NULL;
10441 }
10442
10443 /* Restore the types of the DIE tree starting at START_DIE from the hash
10444 table saved in CU. */
10445
10446 static void
10447 reset_die_and_siblings_types (struct die_info *start_die, struct dwarf2_cu *cu)
10448 {
10449 struct die_info *die;
10450
10451 if (cu->per_cu->type_hash == NULL)
10452 return;
10453
10454 for (die = start_die; die != NULL; die = die->sibling)
10455 {
10456 die->type = get_die_type (die, cu->per_cu->type_hash);
10457 if (die->child != NULL)
10458 reset_die_and_siblings_types (die->child, cu);
10459 }
10460 }
10461
10462 /* Set the mark field in CU and in every other compilation unit in the
10463 cache that we must keep because we are keeping CU. */
10464
10465 /* Add a dependence relationship from CU to REF_PER_CU. */
10466
10467 static void
10468 dwarf2_add_dependence (struct dwarf2_cu *cu,
10469 struct dwarf2_per_cu_data *ref_per_cu)
10470 {
10471 void **slot;
10472
10473 if (cu->dependencies == NULL)
10474 cu->dependencies
10475 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
10476 NULL, &cu->comp_unit_obstack,
10477 hashtab_obstack_allocate,
10478 dummy_obstack_deallocate);
10479
10480 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
10481 if (*slot == NULL)
10482 *slot = ref_per_cu;
10483 }
10484
10485 /* Set the mark field in CU and in every other compilation unit in the
10486 cache that we must keep because we are keeping CU. */
10487
10488 static int
10489 dwarf2_mark_helper (void **slot, void *data)
10490 {
10491 struct dwarf2_per_cu_data *per_cu;
10492
10493 per_cu = (struct dwarf2_per_cu_data *) *slot;
10494 if (per_cu->cu->mark)
10495 return 1;
10496 per_cu->cu->mark = 1;
10497
10498 if (per_cu->cu->dependencies != NULL)
10499 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
10500
10501 return 1;
10502 }
10503
10504 static void
10505 dwarf2_mark (struct dwarf2_cu *cu)
10506 {
10507 if (cu->mark)
10508 return;
10509 cu->mark = 1;
10510 if (cu->dependencies != NULL)
10511 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
10512 }
10513
10514 static void
10515 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
10516 {
10517 while (per_cu)
10518 {
10519 per_cu->cu->mark = 0;
10520 per_cu = per_cu->cu->read_in_chain;
10521 }
10522 }
10523
10524 /* Trivial hash function for partial_die_info: the hash value of a DIE
10525 is its offset in .debug_info for this objfile. */
10526
10527 static hashval_t
10528 partial_die_hash (const void *item)
10529 {
10530 const struct partial_die_info *part_die = item;
10531 return part_die->offset;
10532 }
10533
10534 /* Trivial comparison function for partial_die_info structures: two DIEs
10535 are equal if they have the same offset. */
10536
10537 static int
10538 partial_die_eq (const void *item_lhs, const void *item_rhs)
10539 {
10540 const struct partial_die_info *part_die_lhs = item_lhs;
10541 const struct partial_die_info *part_die_rhs = item_rhs;
10542 return part_die_lhs->offset == part_die_rhs->offset;
10543 }
10544
10545 static struct cmd_list_element *set_dwarf2_cmdlist;
10546 static struct cmd_list_element *show_dwarf2_cmdlist;
10547
10548 static void
10549 set_dwarf2_cmd (char *args, int from_tty)
10550 {
10551 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
10552 }
10553
10554 static void
10555 show_dwarf2_cmd (char *args, int from_tty)
10556 {
10557 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
10558 }
10559
10560 void _initialize_dwarf2_read (void);
10561
10562 void
10563 _initialize_dwarf2_read (void)
10564 {
10565 dwarf2_objfile_data_key = register_objfile_data ();
10566
10567 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
10568 Set DWARF 2 specific variables.\n\
10569 Configure DWARF 2 variables such as the cache size"),
10570 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
10571 0/*allow-unknown*/, &maintenance_set_cmdlist);
10572
10573 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
10574 Show DWARF 2 specific variables\n\
10575 Show DWARF 2 variables such as the cache size"),
10576 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
10577 0/*allow-unknown*/, &maintenance_show_cmdlist);
10578
10579 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
10580 &dwarf2_max_cache_age, _("\
10581 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
10582 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
10583 A higher limit means that cached compilation units will be stored\n\
10584 in memory longer, and more total memory will be used. Zero disables\n\
10585 caching, which can slow down startup."),
10586 NULL,
10587 show_dwarf2_max_cache_age,
10588 &set_dwarf2_cmdlist,
10589 &show_dwarf2_cmdlist);
10590 }
This page took 0.265353 seconds and 4 git commands to generate.