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