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