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