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