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