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