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