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