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