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