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