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