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