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