* dwarf2read.c (dwarf2_per_cu_addr_size): Make result type an int.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "symtab.h"
30 #include "gdbtypes.h"
31 #include "objfiles.h"
32 #include "dwarf2.h"
33 #include "buildsym.h"
34 #include "demangle.h"
35 #include "gdb-demangle.h"
36 #include "expression.h"
37 #include "filenames.h" /* for DOSish file names */
38 #include "macrotab.h"
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include "dwarf2expr.h"
43 #include "dwarf2loc.h"
44 #include "cp-support.h"
45 #include "hashtab.h"
46 #include "command.h"
47 #include "gdbcmd.h"
48 #include "block.h"
49 #include "addrmap.h"
50 #include "typeprint.h"
51 #include "jv-lang.h"
52 #include "psympriv.h"
53 #include "exceptions.h"
54 #include "gdb_stat.h"
55 #include "completer.h"
56 #include "vec.h"
57 #include "c-lang.h"
58 #include "valprint.h"
59 #include <ctype.h>
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 /* .debug_line statement program prologue
79 Because of alignment constraints, this structure has padding and cannot
80 be mapped directly onto the beginning of the .debug_info section. */
81 typedef struct statement_prologue
82 {
83 unsigned int total_length; /* byte length of the statement
84 information */
85 unsigned short version; /* version number -- 2 for DWARF
86 version 2 */
87 unsigned int prologue_length; /* # bytes between prologue &
88 stmt program */
89 unsigned char minimum_instruction_length; /* byte size of
90 smallest instr */
91 unsigned char default_is_stmt; /* initial value of is_stmt
92 register */
93 char line_base;
94 unsigned char line_range;
95 unsigned char opcode_base; /* number assigned to first special
96 opcode */
97 unsigned char *standard_opcode_lengths;
98 }
99 _STATEMENT_PROLOGUE;
100
101 /* When non-zero, dump DIEs after they are read in. */
102 static int dwarf2_die_debug = 0;
103
104 /* When non-zero, cross-check physname against demangler. */
105 static int check_physname = 0;
106
107 static int pagesize;
108
109 /* When set, the file that we're processing is known to have debugging
110 info for C++ namespaces. GCC 3.3.x did not produce this information,
111 but later versions do. */
112
113 static int processing_has_namespace_info;
114
115 static const struct objfile_data *dwarf2_objfile_data_key;
116
117 struct dwarf2_section_info
118 {
119 asection *asection;
120 gdb_byte *buffer;
121 bfd_size_type size;
122 /* Not NULL if the section was actually mmapped. */
123 void *map_addr;
124 /* Page aligned size of mmapped area. */
125 bfd_size_type map_len;
126 /* True if we have tried to read this section. */
127 int readin;
128 };
129
130 typedef struct dwarf2_section_info dwarf2_section_info_def;
131 DEF_VEC_O (dwarf2_section_info_def);
132
133 /* All offsets in the index are of this type. It must be
134 architecture-independent. */
135 typedef uint32_t offset_type;
136
137 DEF_VEC_I (offset_type);
138
139 /* A description of the mapped index. The file format is described in
140 a comment by the code that writes the index. */
141 struct mapped_index
142 {
143 /* Index data format version. */
144 int version;
145
146 /* The total length of the buffer. */
147 off_t total_size;
148
149 /* A pointer to the address table data. */
150 const gdb_byte *address_table;
151
152 /* Size of the address table data in bytes. */
153 offset_type address_table_size;
154
155 /* The symbol table, implemented as a hash table. */
156 const offset_type *symbol_table;
157
158 /* Size in slots, each slot is 2 offset_types. */
159 offset_type symbol_table_slots;
160
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 /* Collection of data recorded per objfile.
166 This hangs off of dwarf2_objfile_data_key. */
167
168 struct dwarf2_per_objfile
169 {
170 struct dwarf2_section_info info;
171 struct dwarf2_section_info abbrev;
172 struct dwarf2_section_info line;
173 struct dwarf2_section_info loc;
174 struct dwarf2_section_info macinfo;
175 struct dwarf2_section_info macro;
176 struct dwarf2_section_info str;
177 struct dwarf2_section_info ranges;
178 struct dwarf2_section_info frame;
179 struct dwarf2_section_info eh_frame;
180 struct dwarf2_section_info gdb_index;
181
182 VEC (dwarf2_section_info_def) *types;
183
184 /* Back link. */
185 struct objfile *objfile;
186
187 /* A list of all the compilation units. This is used to locate
188 the target compilation unit of a particular reference. */
189 struct dwarf2_per_cu_data **all_comp_units;
190
191 /* The number of compilation units in ALL_COMP_UNITS. */
192 int n_comp_units;
193
194 /* The number of .debug_types-related CUs. */
195 int n_type_comp_units;
196
197 /* The .debug_types-related CUs. */
198 struct dwarf2_per_cu_data **type_comp_units;
199
200 /* A chain of compilation units that are currently read in, so that
201 they can be freed later. */
202 struct dwarf2_per_cu_data *read_in_chain;
203
204 /* A table mapping .debug_types signatures to its signatured_type entry.
205 This is NULL if the .debug_types section hasn't been read in yet. */
206 htab_t signatured_types;
207
208 /* A flag indicating wether this objfile has a section loaded at a
209 VMA of 0. */
210 int has_section_at_zero;
211
212 /* True if we are using the mapped index,
213 or we are faking it for OBJF_READNOW's sake. */
214 unsigned char using_index;
215
216 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
217 struct mapped_index *index_table;
218
219 /* When using index_table, this keeps track of all quick_file_names entries.
220 TUs can share line table entries with CUs or other TUs, and there can be
221 a lot more TUs than unique line tables, so we maintain a separate table
222 of all line table entries to support the sharing. */
223 htab_t quick_file_names_table;
224
225 /* Set during partial symbol reading, to prevent queueing of full
226 symbols. */
227 int reading_partial_symbols;
228
229 /* Table mapping type .debug_info DIE offsets to types.
230 This is NULL if not allocated yet.
231 It (currently) makes sense to allocate debug_types_type_hash lazily.
232 To keep things simple we allocate both lazily. */
233 htab_t debug_info_type_hash;
234
235 /* Table mapping type .debug_types DIE offsets to types.
236 This is NULL if not allocated yet. */
237 htab_t debug_types_type_hash;
238 };
239
240 static struct dwarf2_per_objfile *dwarf2_per_objfile;
241
242 /* Default names of the debugging sections. */
243
244 /* Note that if the debugging section has been compressed, it might
245 have a name like .zdebug_info. */
246
247 static const struct dwarf2_debug_sections dwarf2_elf_names =
248 {
249 { ".debug_info", ".zdebug_info" },
250 { ".debug_abbrev", ".zdebug_abbrev" },
251 { ".debug_line", ".zdebug_line" },
252 { ".debug_loc", ".zdebug_loc" },
253 { ".debug_macinfo", ".zdebug_macinfo" },
254 { ".debug_macro", ".zdebug_macro" },
255 { ".debug_str", ".zdebug_str" },
256 { ".debug_ranges", ".zdebug_ranges" },
257 { ".debug_types", ".zdebug_types" },
258 { ".debug_frame", ".zdebug_frame" },
259 { ".eh_frame", NULL },
260 { ".gdb_index", ".zgdb_index" },
261 23
262 };
263
264 /* local data types */
265
266 /* We hold several abbreviation tables in memory at the same time. */
267 #ifndef ABBREV_HASH_SIZE
268 #define ABBREV_HASH_SIZE 121
269 #endif
270
271 /* The data in a compilation unit header, after target2host
272 translation, looks like this. */
273 struct comp_unit_head
274 {
275 unsigned int length;
276 short version;
277 unsigned char addr_size;
278 unsigned char signed_addr_p;
279 unsigned int abbrev_offset;
280
281 /* Size of file offsets; either 4 or 8. */
282 unsigned int offset_size;
283
284 /* Size of the length field; either 4 or 12. */
285 unsigned int initial_length_size;
286
287 /* Offset to the first byte of this compilation unit header in the
288 .debug_info section, for resolving relative reference dies. */
289 unsigned int offset;
290
291 /* Offset to first die in this cu from the start of the cu.
292 This will be the first byte following the compilation unit header. */
293 unsigned int first_die_offset;
294 };
295
296 /* Type used for delaying computation of method physnames.
297 See comments for compute_delayed_physnames. */
298 struct delayed_method_info
299 {
300 /* The type to which the method is attached, i.e., its parent class. */
301 struct type *type;
302
303 /* The index of the method in the type's function fieldlists. */
304 int fnfield_index;
305
306 /* The index of the method in the fieldlist. */
307 int index;
308
309 /* The name of the DIE. */
310 const char *name;
311
312 /* The DIE associated with this method. */
313 struct die_info *die;
314 };
315
316 typedef struct delayed_method_info delayed_method_info;
317 DEF_VEC_O (delayed_method_info);
318
319 /* Internal state when decoding a particular compilation unit. */
320 struct dwarf2_cu
321 {
322 /* The objfile containing this compilation unit. */
323 struct objfile *objfile;
324
325 /* The header of the compilation unit. */
326 struct comp_unit_head header;
327
328 /* Base address of this compilation unit. */
329 CORE_ADDR base_address;
330
331 /* Non-zero if base_address has been set. */
332 int base_known;
333
334 /* The language we are debugging. */
335 enum language language;
336 const struct language_defn *language_defn;
337
338 const char *producer;
339
340 /* The generic symbol table building routines have separate lists for
341 file scope symbols and all all other scopes (local scopes). So
342 we need to select the right one to pass to add_symbol_to_list().
343 We do it by keeping a pointer to the correct list in list_in_scope.
344
345 FIXME: The original dwarf code just treated the file scope as the
346 first local scope, and all other local scopes as nested local
347 scopes, and worked fine. Check to see if we really need to
348 distinguish these in buildsym.c. */
349 struct pending **list_in_scope;
350
351 /* DWARF abbreviation table associated with this compilation unit. */
352 struct abbrev_info **dwarf2_abbrevs;
353
354 /* Storage for the abbrev table. */
355 struct obstack abbrev_obstack;
356
357 /* Hash table holding all the loaded partial DIEs. */
358 htab_t partial_dies;
359
360 /* Storage for things with the same lifetime as this read-in compilation
361 unit, including partial DIEs. */
362 struct obstack comp_unit_obstack;
363
364 /* When multiple dwarf2_cu structures are living in memory, this field
365 chains them all together, so that they can be released efficiently.
366 We will probably also want a generation counter so that most-recently-used
367 compilation units are cached... */
368 struct dwarf2_per_cu_data *read_in_chain;
369
370 /* Backchain to our per_cu entry if the tree has been built. */
371 struct dwarf2_per_cu_data *per_cu;
372
373 /* How many compilation units ago was this CU last referenced? */
374 int last_used;
375
376 /* A hash table of die offsets for following references. */
377 htab_t die_hash;
378
379 /* Full DIEs if read in. */
380 struct die_info *dies;
381
382 /* A set of pointers to dwarf2_per_cu_data objects for compilation
383 units referenced by this one. Only set during full symbol processing;
384 partial symbol tables do not have dependencies. */
385 htab_t dependencies;
386
387 /* Header data from the line table, during full symbol processing. */
388 struct line_header *line_header;
389
390 /* A list of methods which need to have physnames computed
391 after all type information has been read. */
392 VEC (delayed_method_info) *method_list;
393
394 /* To be copied to symtab->call_site_htab. */
395 htab_t call_site_htab;
396
397 /* Mark used when releasing cached dies. */
398 unsigned int mark : 1;
399
400 /* This flag will be set if this compilation unit might include
401 inter-compilation-unit references. */
402 unsigned int has_form_ref_addr : 1;
403
404 /* This flag will be set if this compilation unit includes any
405 DW_TAG_namespace DIEs. If we know that there are explicit
406 DIEs for namespaces, we don't need to try to infer them
407 from mangled names. */
408 unsigned int has_namespace_info : 1;
409
410 /* This CU references .debug_loc. See the symtab->locations_valid field.
411 This test is imperfect as there may exist optimized debug code not using
412 any location list and still facing inlining issues if handled as
413 unoptimized code. For a future better test see GCC PR other/32998. */
414 unsigned int has_loclist : 1;
415 };
416
417 /* Persistent data held for a compilation unit, even when not
418 processing it. We put a pointer to this structure in the
419 read_symtab_private field of the psymtab. */
420
421 struct dwarf2_per_cu_data
422 {
423 /* The start offset and length of this compilation unit. 2**29-1
424 bytes should suffice to store the length of any compilation unit
425 - if it doesn't, GDB will fall over anyway.
426 NOTE: Unlike comp_unit_head.length, this length includes
427 initial_length_size. */
428 unsigned int offset;
429 unsigned int length : 29;
430
431 /* Flag indicating this compilation unit will be read in before
432 any of the current compilation units are processed. */
433 unsigned int queued : 1;
434
435 /* This flag will be set if we need to load absolutely all DIEs
436 for this compilation unit, instead of just the ones we think
437 are interesting. It gets set if we look for a DIE in the
438 hash table and don't find it. */
439 unsigned int load_all_dies : 1;
440
441 /* Non-null if this CU is from .debug_types; in which case it points
442 to the section. Otherwise it's from .debug_info. */
443 struct dwarf2_section_info *debug_types_section;
444
445 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
446 of the CU cache it gets reset to NULL again. */
447 struct dwarf2_cu *cu;
448
449 /* The corresponding objfile.
450 Normally we can get the objfile from dwarf2_per_objfile.
451 However we can enter this file with just a "per_cu" handle. */
452 struct objfile *objfile;
453
454 /* When using partial symbol tables, the 'psymtab' field is active.
455 Otherwise the 'quick' field is active. */
456 union
457 {
458 /* The partial symbol table associated with this compilation unit,
459 or NULL for partial units (which do not have an associated
460 symtab). */
461 struct partial_symtab *psymtab;
462
463 /* Data needed by the "quick" functions. */
464 struct dwarf2_per_cu_quick_data *quick;
465 } v;
466 };
467
468 /* Entry in the signatured_types hash table. */
469
470 struct signatured_type
471 {
472 ULONGEST signature;
473
474 /* Offset in .debug_types of the type defined by this TU. */
475 unsigned int type_offset;
476
477 /* The CU(/TU) of this type. */
478 struct dwarf2_per_cu_data per_cu;
479 };
480
481 /* Struct used to pass misc. parameters to read_die_and_children, et
482 al. which are used for both .debug_info and .debug_types dies.
483 All parameters here are unchanging for the life of the call. This
484 struct exists to abstract away the constant parameters of die
485 reading. */
486
487 struct die_reader_specs
488 {
489 /* The bfd of this objfile. */
490 bfd* abfd;
491
492 /* The CU of the DIE we are parsing. */
493 struct dwarf2_cu *cu;
494
495 /* Pointer to start of section buffer.
496 This is either the start of .debug_info or .debug_types. */
497 const gdb_byte *buffer;
498 };
499
500 /* The line number information for a compilation unit (found in the
501 .debug_line section) begins with a "statement program header",
502 which contains the following information. */
503 struct line_header
504 {
505 unsigned int total_length;
506 unsigned short version;
507 unsigned int header_length;
508 unsigned char minimum_instruction_length;
509 unsigned char maximum_ops_per_instruction;
510 unsigned char default_is_stmt;
511 int line_base;
512 unsigned char line_range;
513 unsigned char opcode_base;
514
515 /* standard_opcode_lengths[i] is the number of operands for the
516 standard opcode whose value is i. This means that
517 standard_opcode_lengths[0] is unused, and the last meaningful
518 element is standard_opcode_lengths[opcode_base - 1]. */
519 unsigned char *standard_opcode_lengths;
520
521 /* The include_directories table. NOTE! These strings are not
522 allocated with xmalloc; instead, they are pointers into
523 debug_line_buffer. If you try to free them, `free' will get
524 indigestion. */
525 unsigned int num_include_dirs, include_dirs_size;
526 char **include_dirs;
527
528 /* The file_names table. NOTE! These strings are not allocated
529 with xmalloc; instead, they are pointers into debug_line_buffer.
530 Don't try to free them directly. */
531 unsigned int num_file_names, file_names_size;
532 struct file_entry
533 {
534 char *name;
535 unsigned int dir_index;
536 unsigned int mod_time;
537 unsigned int length;
538 int included_p; /* Non-zero if referenced by the Line Number Program. */
539 struct symtab *symtab; /* The associated symbol table, if any. */
540 } *file_names;
541
542 /* The start and end of the statement program following this
543 header. These point into dwarf2_per_objfile->line_buffer. */
544 gdb_byte *statement_program_start, *statement_program_end;
545 };
546
547 /* When we construct a partial symbol table entry we only
548 need this much information. */
549 struct partial_die_info
550 {
551 /* Offset of this DIE. */
552 unsigned int offset;
553
554 /* DWARF-2 tag for this DIE. */
555 ENUM_BITFIELD(dwarf_tag) tag : 16;
556
557 /* Assorted flags describing the data found in this DIE. */
558 unsigned int has_children : 1;
559 unsigned int is_external : 1;
560 unsigned int is_declaration : 1;
561 unsigned int has_type : 1;
562 unsigned int has_specification : 1;
563 unsigned int has_pc_info : 1;
564
565 /* Flag set if the SCOPE field of this structure has been
566 computed. */
567 unsigned int scope_set : 1;
568
569 /* Flag set if the DIE has a byte_size attribute. */
570 unsigned int has_byte_size : 1;
571
572 /* Flag set if any of the DIE's children are template arguments. */
573 unsigned int has_template_arguments : 1;
574
575 /* Flag set if fixup_partial_die has been called on this die. */
576 unsigned int fixup_called : 1;
577
578 /* The name of this DIE. Normally the value of DW_AT_name, but
579 sometimes a default name for unnamed DIEs. */
580 char *name;
581
582 /* The linkage name, if present. */
583 const char *linkage_name;
584
585 /* The scope to prepend to our children. This is generally
586 allocated on the comp_unit_obstack, so will disappear
587 when this compilation unit leaves the cache. */
588 char *scope;
589
590 /* The location description associated with this DIE, if any. */
591 struct dwarf_block *locdesc;
592
593 /* If HAS_PC_INFO, the PC range associated with this DIE. */
594 CORE_ADDR lowpc;
595 CORE_ADDR highpc;
596
597 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
598 DW_AT_sibling, if any. */
599 /* NOTE: This member isn't strictly necessary, read_partial_die could
600 return DW_AT_sibling values to its caller load_partial_dies. */
601 gdb_byte *sibling;
602
603 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
604 DW_AT_specification (or DW_AT_abstract_origin or
605 DW_AT_extension). */
606 unsigned int spec_offset;
607
608 /* Pointers to this DIE's parent, first child, and next sibling,
609 if any. */
610 struct partial_die_info *die_parent, *die_child, *die_sibling;
611 };
612
613 /* This data structure holds the information of an abbrev. */
614 struct abbrev_info
615 {
616 unsigned int number; /* number identifying abbrev */
617 enum dwarf_tag tag; /* dwarf tag */
618 unsigned short has_children; /* boolean */
619 unsigned short num_attrs; /* number of attributes */
620 struct attr_abbrev *attrs; /* an array of attribute descriptions */
621 struct abbrev_info *next; /* next in chain */
622 };
623
624 struct attr_abbrev
625 {
626 ENUM_BITFIELD(dwarf_attribute) name : 16;
627 ENUM_BITFIELD(dwarf_form) form : 16;
628 };
629
630 /* Attributes have a name and a value. */
631 struct attribute
632 {
633 ENUM_BITFIELD(dwarf_attribute) name : 16;
634 ENUM_BITFIELD(dwarf_form) form : 15;
635
636 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
637 field should be in u.str (existing only for DW_STRING) but it is kept
638 here for better struct attribute alignment. */
639 unsigned int string_is_canonical : 1;
640
641 union
642 {
643 char *str;
644 struct dwarf_block *blk;
645 ULONGEST unsnd;
646 LONGEST snd;
647 CORE_ADDR addr;
648 struct signatured_type *signatured_type;
649 }
650 u;
651 };
652
653 /* This data structure holds a complete die structure. */
654 struct die_info
655 {
656 /* DWARF-2 tag for this DIE. */
657 ENUM_BITFIELD(dwarf_tag) tag : 16;
658
659 /* Number of attributes */
660 unsigned char num_attrs;
661
662 /* True if we're presently building the full type name for the
663 type derived from this DIE. */
664 unsigned char building_fullname : 1;
665
666 /* Abbrev number */
667 unsigned int abbrev;
668
669 /* Offset in .debug_info or .debug_types section. */
670 unsigned int offset;
671
672 /* The dies in a compilation unit form an n-ary tree. PARENT
673 points to this die's parent; CHILD points to the first child of
674 this node; and all the children of a given node are chained
675 together via their SIBLING fields. */
676 struct die_info *child; /* Its first child, if any. */
677 struct die_info *sibling; /* Its next sibling, if any. */
678 struct die_info *parent; /* Its parent, if any. */
679
680 /* An array of attributes, with NUM_ATTRS elements. There may be
681 zero, but it's not common and zero-sized arrays are not
682 sufficiently portable C. */
683 struct attribute attrs[1];
684 };
685
686 /* Get at parts of an attribute structure. */
687
688 #define DW_STRING(attr) ((attr)->u.str)
689 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
690 #define DW_UNSND(attr) ((attr)->u.unsnd)
691 #define DW_BLOCK(attr) ((attr)->u.blk)
692 #define DW_SND(attr) ((attr)->u.snd)
693 #define DW_ADDR(attr) ((attr)->u.addr)
694 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
695
696 /* Blocks are a bunch of untyped bytes. */
697 struct dwarf_block
698 {
699 unsigned int size;
700
701 /* Valid only if SIZE is not zero. */
702 gdb_byte *data;
703 };
704
705 #ifndef ATTR_ALLOC_CHUNK
706 #define ATTR_ALLOC_CHUNK 4
707 #endif
708
709 /* Allocate fields for structs, unions and enums in this size. */
710 #ifndef DW_FIELD_ALLOC_CHUNK
711 #define DW_FIELD_ALLOC_CHUNK 4
712 #endif
713
714 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
715 but this would require a corresponding change in unpack_field_as_long
716 and friends. */
717 static int bits_per_byte = 8;
718
719 /* The routines that read and process dies for a C struct or C++ class
720 pass lists of data member fields and lists of member function fields
721 in an instance of a field_info structure, as defined below. */
722 struct field_info
723 {
724 /* List of data member and baseclasses fields. */
725 struct nextfield
726 {
727 struct nextfield *next;
728 int accessibility;
729 int virtuality;
730 struct field field;
731 }
732 *fields, *baseclasses;
733
734 /* Number of fields (including baseclasses). */
735 int nfields;
736
737 /* Number of baseclasses. */
738 int nbaseclasses;
739
740 /* Set if the accesibility of one of the fields is not public. */
741 int non_public_fields;
742
743 /* Member function fields array, entries are allocated in the order they
744 are encountered in the object file. */
745 struct nextfnfield
746 {
747 struct nextfnfield *next;
748 struct fn_field fnfield;
749 }
750 *fnfields;
751
752 /* Member function fieldlist array, contains name of possibly overloaded
753 member function, number of overloaded member functions and a pointer
754 to the head of the member function field chain. */
755 struct fnfieldlist
756 {
757 char *name;
758 int length;
759 struct nextfnfield *head;
760 }
761 *fnfieldlists;
762
763 /* Number of entries in the fnfieldlists array. */
764 int nfnfields;
765
766 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
767 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
768 struct typedef_field_list
769 {
770 struct typedef_field field;
771 struct typedef_field_list *next;
772 }
773 *typedef_field_list;
774 unsigned typedef_field_list_count;
775 };
776
777 /* One item on the queue of compilation units to read in full symbols
778 for. */
779 struct dwarf2_queue_item
780 {
781 struct dwarf2_per_cu_data *per_cu;
782 struct dwarf2_queue_item *next;
783 };
784
785 /* The current queue. */
786 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
787
788 /* Loaded secondary compilation units are kept in memory until they
789 have not been referenced for the processing of this many
790 compilation units. Set this to zero to disable caching. Cache
791 sizes of up to at least twenty will improve startup time for
792 typical inter-CU-reference binaries, at an obvious memory cost. */
793 static int dwarf2_max_cache_age = 5;
794 static void
795 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
796 struct cmd_list_element *c, const char *value)
797 {
798 fprintf_filtered (file, _("The upper bound on the age of cached "
799 "dwarf2 compilation units is %s.\n"),
800 value);
801 }
802
803
804 /* Various complaints about symbol reading that don't abort the process. */
805
806 static void
807 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
808 {
809 complaint (&symfile_complaints,
810 _("statement list doesn't fit in .debug_line section"));
811 }
812
813 static void
814 dwarf2_debug_line_missing_file_complaint (void)
815 {
816 complaint (&symfile_complaints,
817 _(".debug_line section has line data without a file"));
818 }
819
820 static void
821 dwarf2_debug_line_missing_end_sequence_complaint (void)
822 {
823 complaint (&symfile_complaints,
824 _(".debug_line section has line "
825 "program sequence without an end"));
826 }
827
828 static void
829 dwarf2_complex_location_expr_complaint (void)
830 {
831 complaint (&symfile_complaints, _("location expression too complex"));
832 }
833
834 static void
835 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
836 int arg3)
837 {
838 complaint (&symfile_complaints,
839 _("const value length mismatch for '%s', got %d, expected %d"),
840 arg1, arg2, arg3);
841 }
842
843 static void
844 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
845 {
846 complaint (&symfile_complaints,
847 _("macro info runs off end of `%s' section"),
848 section->asection->name);
849 }
850
851 static void
852 dwarf2_macro_malformed_definition_complaint (const char *arg1)
853 {
854 complaint (&symfile_complaints,
855 _("macro debug info contains a "
856 "malformed macro definition:\n`%s'"),
857 arg1);
858 }
859
860 static void
861 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
862 {
863 complaint (&symfile_complaints,
864 _("invalid attribute class or form for '%s' in '%s'"),
865 arg1, arg2);
866 }
867
868 /* local function prototypes */
869
870 static void dwarf2_locate_sections (bfd *, asection *, void *);
871
872 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
875 static void dwarf2_find_base_address (struct die_info *die,
876 struct dwarf2_cu *cu);
877
878 static void dwarf2_build_psymtabs_hard (struct objfile *);
879
880 static void scan_partial_symbols (struct partial_die_info *,
881 CORE_ADDR *, CORE_ADDR *,
882 int, struct dwarf2_cu *);
883
884 static void add_partial_symbol (struct partial_die_info *,
885 struct dwarf2_cu *);
886
887 static void add_partial_namespace (struct partial_die_info *pdi,
888 CORE_ADDR *lowpc, CORE_ADDR *highpc,
889 int need_pc, struct dwarf2_cu *cu);
890
891 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
892 CORE_ADDR *highpc, int need_pc,
893 struct dwarf2_cu *cu);
894
895 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
896 struct dwarf2_cu *cu);
897
898 static void add_partial_subprogram (struct partial_die_info *pdi,
899 CORE_ADDR *lowpc, CORE_ADDR *highpc,
900 int need_pc, struct dwarf2_cu *cu);
901
902 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
903 gdb_byte *buffer, gdb_byte *info_ptr,
904 bfd *abfd, struct dwarf2_cu *cu);
905
906 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
907
908 static void psymtab_to_symtab_1 (struct partial_symtab *);
909
910 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
911
912 static void dwarf2_free_abbrev_table (void *);
913
914 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
915
916 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
917 struct dwarf2_cu *);
918
919 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
920 struct dwarf2_cu *);
921
922 static struct partial_die_info *load_partial_dies (bfd *,
923 gdb_byte *, gdb_byte *,
924 int, struct dwarf2_cu *);
925
926 static gdb_byte *read_partial_die (struct partial_die_info *,
927 struct abbrev_info *abbrev,
928 unsigned int, bfd *,
929 gdb_byte *, gdb_byte *,
930 struct dwarf2_cu *);
931
932 static struct partial_die_info *find_partial_die (unsigned int,
933 struct dwarf2_cu *);
934
935 static void fixup_partial_die (struct partial_die_info *,
936 struct dwarf2_cu *);
937
938 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
939 bfd *, gdb_byte *, struct dwarf2_cu *);
940
941 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
943
944 static unsigned int read_1_byte (bfd *, gdb_byte *);
945
946 static int read_1_signed_byte (bfd *, gdb_byte *);
947
948 static unsigned int read_2_bytes (bfd *, gdb_byte *);
949
950 static unsigned int read_4_bytes (bfd *, gdb_byte *);
951
952 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
953
954 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
955 unsigned int *);
956
957 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
958
959 static LONGEST read_checked_initial_length_and_offset
960 (bfd *, gdb_byte *, const struct comp_unit_head *,
961 unsigned int *, unsigned int *);
962
963 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
964 unsigned int *);
965
966 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
967
968 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
969
970 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
971
972 static char *read_indirect_string (bfd *, gdb_byte *,
973 const struct comp_unit_head *,
974 unsigned int *);
975
976 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
977
978 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
979
980 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
981
982 static void set_cu_language (unsigned int, struct dwarf2_cu *);
983
984 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
985 struct dwarf2_cu *);
986
987 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
988 unsigned int,
989 struct dwarf2_cu *);
990
991 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
992 struct dwarf2_cu *cu);
993
994 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
995
996 static struct die_info *die_specification (struct die_info *die,
997 struct dwarf2_cu **);
998
999 static void free_line_header (struct line_header *lh);
1000
1001 static void add_file_name (struct line_header *, char *, unsigned int,
1002 unsigned int, unsigned int);
1003
1004 static struct line_header *(dwarf_decode_line_header
1005 (unsigned int offset,
1006 bfd *abfd, struct dwarf2_cu *cu));
1007
1008 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1009 struct dwarf2_cu *, struct partial_symtab *);
1010
1011 static void dwarf2_start_subfile (char *, const char *, const char *);
1012
1013 static struct symbol *new_symbol (struct die_info *, struct type *,
1014 struct dwarf2_cu *);
1015
1016 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1017 struct dwarf2_cu *, struct symbol *);
1018
1019 static void dwarf2_const_value (struct attribute *, struct symbol *,
1020 struct dwarf2_cu *);
1021
1022 static void dwarf2_const_value_attr (struct attribute *attr,
1023 struct type *type,
1024 const char *name,
1025 struct obstack *obstack,
1026 struct dwarf2_cu *cu, long *value,
1027 gdb_byte **bytes,
1028 struct dwarf2_locexpr_baton **baton);
1029
1030 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1031
1032 static int need_gnat_info (struct dwarf2_cu *);
1033
1034 static struct type *die_descriptive_type (struct die_info *,
1035 struct dwarf2_cu *);
1036
1037 static void set_descriptive_type (struct type *, struct die_info *,
1038 struct dwarf2_cu *);
1039
1040 static struct type *die_containing_type (struct die_info *,
1041 struct dwarf2_cu *);
1042
1043 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1044 struct dwarf2_cu *);
1045
1046 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1047
1048 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1049
1050 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1051
1052 static char *typename_concat (struct obstack *obs, const char *prefix,
1053 const char *suffix, int physname,
1054 struct dwarf2_cu *cu);
1055
1056 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1057
1058 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1059
1060 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1061
1062 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1065
1066 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *, struct partial_symtab *);
1068
1069 static int dwarf2_get_pc_bounds (struct die_info *,
1070 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071 struct partial_symtab *);
1072
1073 static void get_scope_pc_bounds (struct die_info *,
1074 CORE_ADDR *, CORE_ADDR *,
1075 struct dwarf2_cu *);
1076
1077 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078 CORE_ADDR, struct dwarf2_cu *);
1079
1080 static void dwarf2_add_field (struct field_info *, struct die_info *,
1081 struct dwarf2_cu *);
1082
1083 static void dwarf2_attach_fields_to_type (struct field_info *,
1084 struct type *, struct dwarf2_cu *);
1085
1086 static void dwarf2_add_member_fn (struct field_info *,
1087 struct die_info *, struct type *,
1088 struct dwarf2_cu *);
1089
1090 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1091 struct type *,
1092 struct dwarf2_cu *);
1093
1094 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1095
1096 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1097
1098 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1099
1100 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1101
1102 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1103
1104 static struct type *read_module_type (struct die_info *die,
1105 struct dwarf2_cu *cu);
1106
1107 static const char *namespace_name (struct die_info *die,
1108 int *is_anonymous, struct dwarf2_cu *);
1109
1110 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1111
1112 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1113
1114 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1115 struct dwarf2_cu *);
1116
1117 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1118
1119 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
1121 gdb_byte **new_info_ptr,
1122 struct die_info *parent);
1123
1124 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
1126 gdb_byte **new_info_ptr,
1127 struct die_info *parent);
1128
1129 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1130 gdb_byte *info_ptr,
1131 gdb_byte **new_info_ptr,
1132 struct die_info *parent);
1133
1134 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1135 struct die_info **, gdb_byte *,
1136 int *);
1137
1138 static void process_die (struct die_info *, struct dwarf2_cu *);
1139
1140 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1141 struct obstack *);
1142
1143 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1144
1145 static const char *dwarf2_full_name (char *name,
1146 struct die_info *die,
1147 struct dwarf2_cu *cu);
1148
1149 static struct die_info *dwarf2_extension (struct die_info *die,
1150 struct dwarf2_cu **);
1151
1152 static char *dwarf_tag_name (unsigned int);
1153
1154 static char *dwarf_attr_name (unsigned int);
1155
1156 static char *dwarf_form_name (unsigned int);
1157
1158 static char *dwarf_bool_name (unsigned int);
1159
1160 static char *dwarf_type_encoding_name (unsigned int);
1161
1162 #if 0
1163 static char *dwarf_cfi_name (unsigned int);
1164 #endif
1165
1166 static struct die_info *sibling_die (struct die_info *);
1167
1168 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1169
1170 static void dump_die_for_error (struct die_info *);
1171
1172 static void dump_die_1 (struct ui_file *, int level, int max_level,
1173 struct die_info *);
1174
1175 /*static*/ void dump_die (struct die_info *, int max_level);
1176
1177 static void store_in_ref_table (struct die_info *,
1178 struct dwarf2_cu *);
1179
1180 static int is_ref_attr (struct attribute *);
1181
1182 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1183
1184 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1185
1186 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1187 struct attribute *,
1188 struct dwarf2_cu **);
1189
1190 static struct die_info *follow_die_ref (struct die_info *,
1191 struct attribute *,
1192 struct dwarf2_cu **);
1193
1194 static struct die_info *follow_die_sig (struct die_info *,
1195 struct attribute *,
1196 struct dwarf2_cu **);
1197
1198 static struct signatured_type *lookup_signatured_type_at_offset
1199 (struct objfile *objfile,
1200 struct dwarf2_section_info *section,
1201 unsigned int offset);
1202
1203 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1204
1205 static void read_signatured_type (struct signatured_type *type_sig);
1206
1207 /* memory allocation interface */
1208
1209 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1210
1211 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1212
1213 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1214
1215 static void dwarf_decode_macros (struct line_header *, unsigned int,
1216 char *, bfd *, struct dwarf2_cu *,
1217 struct dwarf2_section_info *,
1218 int);
1219
1220 static int attr_form_is_block (struct attribute *);
1221
1222 static int attr_form_is_section_offset (struct attribute *);
1223
1224 static int attr_form_is_constant (struct attribute *);
1225
1226 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1227 struct dwarf2_loclist_baton *baton,
1228 struct attribute *attr);
1229
1230 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1231 struct symbol *sym,
1232 struct dwarf2_cu *cu);
1233
1234 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1235 struct abbrev_info *abbrev,
1236 struct dwarf2_cu *cu);
1237
1238 static void free_stack_comp_unit (void *);
1239
1240 static hashval_t partial_die_hash (const void *item);
1241
1242 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1243
1244 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1245 (unsigned int offset, struct objfile *objfile);
1246
1247 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1248 (unsigned int offset, struct objfile *objfile);
1249
1250 static void init_one_comp_unit (struct dwarf2_cu *cu,
1251 struct dwarf2_per_cu_data *per_cu);
1252
1253 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1254 struct die_info *comp_unit_die);
1255
1256 static void free_heap_comp_unit (void *);
1257
1258 static void free_cached_comp_units (void *);
1259
1260 static void age_cached_comp_units (void);
1261
1262 static void free_one_cached_comp_unit (void *);
1263
1264 static struct type *set_die_type (struct die_info *, struct type *,
1265 struct dwarf2_cu *);
1266
1267 static void create_all_comp_units (struct objfile *);
1268
1269 static int create_debug_types_hash_table (struct objfile *objfile);
1270
1271 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1272
1273 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275 static void dwarf2_add_dependence (struct dwarf2_cu *,
1276 struct dwarf2_per_cu_data *);
1277
1278 static void dwarf2_mark (struct dwarf2_cu *);
1279
1280 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
1282 static struct type *get_die_type_at_offset (unsigned int,
1283 struct dwarf2_per_cu_data *per_cu);
1284
1285 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static void dwarf2_release_queue (void *dummy);
1288
1289 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1290
1291 static void process_queue (void);
1292
1293 static void find_file_and_directory (struct die_info *die,
1294 struct dwarf2_cu *cu,
1295 char **name, char **comp_dir);
1296
1297 static char *file_full_name (int file, struct line_header *lh,
1298 const char *comp_dir);
1299
1300 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1301 gdb_byte *info_ptr,
1302 gdb_byte *buffer,
1303 unsigned int buffer_size,
1304 bfd *abfd,
1305 int is_debug_types_section);
1306
1307 static void init_cu_die_reader (struct die_reader_specs *reader,
1308 struct dwarf2_cu *cu);
1309
1310 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1311
1312 #if WORDS_BIGENDIAN
1313
1314 /* Convert VALUE between big- and little-endian. */
1315 static offset_type
1316 byte_swap (offset_type value)
1317 {
1318 offset_type result;
1319
1320 result = (value & 0xff) << 24;
1321 result |= (value & 0xff00) << 8;
1322 result |= (value & 0xff0000) >> 8;
1323 result |= (value & 0xff000000) >> 24;
1324 return result;
1325 }
1326
1327 #define MAYBE_SWAP(V) byte_swap (V)
1328
1329 #else
1330 #define MAYBE_SWAP(V) (V)
1331 #endif /* WORDS_BIGENDIAN */
1332
1333 /* The suffix for an index file. */
1334 #define INDEX_SUFFIX ".gdb-index"
1335
1336 static const char *dwarf2_physname (char *name, struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
1339 /* Try to locate the sections we need for DWARF 2 debugging
1340 information and return true if we have enough to do something.
1341 NAMES points to the dwarf2 section names, or is NULL if the standard
1342 ELF names are used. */
1343
1344 int
1345 dwarf2_has_info (struct objfile *objfile,
1346 const struct dwarf2_debug_sections *names)
1347 {
1348 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1349 if (!dwarf2_per_objfile)
1350 {
1351 /* Initialize per-objfile state. */
1352 struct dwarf2_per_objfile *data
1353 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1354
1355 memset (data, 0, sizeof (*data));
1356 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1357 dwarf2_per_objfile = data;
1358
1359 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1360 (void *) names);
1361 dwarf2_per_objfile->objfile = objfile;
1362 }
1363 return (dwarf2_per_objfile->info.asection != NULL
1364 && dwarf2_per_objfile->abbrev.asection != NULL);
1365 }
1366
1367 /* When loading sections, we look either for uncompressed section or for
1368 compressed section names. */
1369
1370 static int
1371 section_is_p (const char *section_name,
1372 const struct dwarf2_section_names *names)
1373 {
1374 if (names->normal != NULL
1375 && strcmp (section_name, names->normal) == 0)
1376 return 1;
1377 if (names->compressed != NULL
1378 && strcmp (section_name, names->compressed) == 0)
1379 return 1;
1380 return 0;
1381 }
1382
1383 /* This function is mapped across the sections and remembers the
1384 offset and size of each of the debugging sections we are interested
1385 in. */
1386
1387 static void
1388 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1389 {
1390 const struct dwarf2_debug_sections *names;
1391
1392 if (vnames == NULL)
1393 names = &dwarf2_elf_names;
1394 else
1395 names = (const struct dwarf2_debug_sections *) vnames;
1396
1397 if (section_is_p (sectp->name, &names->info))
1398 {
1399 dwarf2_per_objfile->info.asection = sectp;
1400 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1401 }
1402 else if (section_is_p (sectp->name, &names->abbrev))
1403 {
1404 dwarf2_per_objfile->abbrev.asection = sectp;
1405 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1406 }
1407 else if (section_is_p (sectp->name, &names->line))
1408 {
1409 dwarf2_per_objfile->line.asection = sectp;
1410 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1411 }
1412 else if (section_is_p (sectp->name, &names->loc))
1413 {
1414 dwarf2_per_objfile->loc.asection = sectp;
1415 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1416 }
1417 else if (section_is_p (sectp->name, &names->macinfo))
1418 {
1419 dwarf2_per_objfile->macinfo.asection = sectp;
1420 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1421 }
1422 else if (section_is_p (sectp->name, &names->macro))
1423 {
1424 dwarf2_per_objfile->macro.asection = sectp;
1425 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1426 }
1427 else if (section_is_p (sectp->name, &names->str))
1428 {
1429 dwarf2_per_objfile->str.asection = sectp;
1430 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1431 }
1432 else if (section_is_p (sectp->name, &names->frame))
1433 {
1434 dwarf2_per_objfile->frame.asection = sectp;
1435 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1436 }
1437 else if (section_is_p (sectp->name, &names->eh_frame))
1438 {
1439 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1440
1441 if (aflag & SEC_HAS_CONTENTS)
1442 {
1443 dwarf2_per_objfile->eh_frame.asection = sectp;
1444 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1445 }
1446 }
1447 else if (section_is_p (sectp->name, &names->ranges))
1448 {
1449 dwarf2_per_objfile->ranges.asection = sectp;
1450 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1451 }
1452 else if (section_is_p (sectp->name, &names->types))
1453 {
1454 struct dwarf2_section_info type_section;
1455
1456 memset (&type_section, 0, sizeof (type_section));
1457 type_section.asection = sectp;
1458 type_section.size = bfd_get_section_size (sectp);
1459
1460 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1461 &type_section);
1462 }
1463 else if (section_is_p (sectp->name, &names->gdb_index))
1464 {
1465 dwarf2_per_objfile->gdb_index.asection = sectp;
1466 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1467 }
1468
1469 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1470 && bfd_section_vma (abfd, sectp) == 0)
1471 dwarf2_per_objfile->has_section_at_zero = 1;
1472 }
1473
1474 /* Decompress a section that was compressed using zlib. Store the
1475 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1476
1477 static void
1478 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1479 gdb_byte **outbuf, bfd_size_type *outsize)
1480 {
1481 bfd *abfd = objfile->obfd;
1482 #ifndef HAVE_ZLIB_H
1483 error (_("Support for zlib-compressed DWARF data (from '%s') "
1484 "is disabled in this copy of GDB"),
1485 bfd_get_filename (abfd));
1486 #else
1487 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1488 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1489 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1490 bfd_size_type uncompressed_size;
1491 gdb_byte *uncompressed_buffer;
1492 z_stream strm;
1493 int rc;
1494 int header_size = 12;
1495
1496 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1497 || bfd_bread (compressed_buffer,
1498 compressed_size, abfd) != compressed_size)
1499 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1500 bfd_get_filename (abfd));
1501
1502 /* Read the zlib header. In this case, it should be "ZLIB" followed
1503 by the uncompressed section size, 8 bytes in big-endian order. */
1504 if (compressed_size < header_size
1505 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1506 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1507 bfd_get_filename (abfd));
1508 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1509 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1510 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1511 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1512 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1513 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1514 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1515 uncompressed_size += compressed_buffer[11];
1516
1517 /* It is possible the section consists of several compressed
1518 buffers concatenated together, so we uncompress in a loop. */
1519 strm.zalloc = NULL;
1520 strm.zfree = NULL;
1521 strm.opaque = NULL;
1522 strm.avail_in = compressed_size - header_size;
1523 strm.next_in = (Bytef*) compressed_buffer + header_size;
1524 strm.avail_out = uncompressed_size;
1525 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1526 uncompressed_size);
1527 rc = inflateInit (&strm);
1528 while (strm.avail_in > 0)
1529 {
1530 if (rc != Z_OK)
1531 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1532 bfd_get_filename (abfd), rc);
1533 strm.next_out = ((Bytef*) uncompressed_buffer
1534 + (uncompressed_size - strm.avail_out));
1535 rc = inflate (&strm, Z_FINISH);
1536 if (rc != Z_STREAM_END)
1537 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1538 bfd_get_filename (abfd), rc);
1539 rc = inflateReset (&strm);
1540 }
1541 rc = inflateEnd (&strm);
1542 if (rc != Z_OK
1543 || strm.avail_out != 0)
1544 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1545 bfd_get_filename (abfd), rc);
1546
1547 do_cleanups (cleanup);
1548 *outbuf = uncompressed_buffer;
1549 *outsize = uncompressed_size;
1550 #endif
1551 }
1552
1553 /* A helper function that decides whether a section is empty. */
1554
1555 static int
1556 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1557 {
1558 return info->asection == NULL || info->size == 0;
1559 }
1560
1561 /* Read the contents of the section INFO from object file specified by
1562 OBJFILE, store info about the section into INFO.
1563 If the section is compressed, uncompress it before returning. */
1564
1565 static void
1566 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1567 {
1568 bfd *abfd = objfile->obfd;
1569 asection *sectp = info->asection;
1570 gdb_byte *buf, *retbuf;
1571 unsigned char header[4];
1572
1573 if (info->readin)
1574 return;
1575 info->buffer = NULL;
1576 info->map_addr = NULL;
1577 info->readin = 1;
1578
1579 if (dwarf2_section_empty_p (info))
1580 return;
1581
1582 /* Check if the file has a 4-byte header indicating compression. */
1583 if (info->size > sizeof (header)
1584 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1585 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1586 {
1587 /* Upon decompression, update the buffer and its size. */
1588 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1589 {
1590 zlib_decompress_section (objfile, sectp, &info->buffer,
1591 &info->size);
1592 return;
1593 }
1594 }
1595
1596 #ifdef HAVE_MMAP
1597 if (pagesize == 0)
1598 pagesize = getpagesize ();
1599
1600 /* Only try to mmap sections which are large enough: we don't want to
1601 waste space due to fragmentation. Also, only try mmap for sections
1602 without relocations. */
1603
1604 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1605 {
1606 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1607 MAP_PRIVATE, sectp->filepos,
1608 &info->map_addr, &info->map_len);
1609
1610 if ((caddr_t)info->buffer != MAP_FAILED)
1611 {
1612 #if HAVE_POSIX_MADVISE
1613 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1614 #endif
1615 return;
1616 }
1617 }
1618 #endif
1619
1620 /* If we get here, we are a normal, not-compressed section. */
1621 info->buffer = buf
1622 = obstack_alloc (&objfile->objfile_obstack, info->size);
1623
1624 /* When debugging .o files, we may need to apply relocations; see
1625 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1626 We never compress sections in .o files, so we only need to
1627 try this when the section is not compressed. */
1628 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1629 if (retbuf != NULL)
1630 {
1631 info->buffer = retbuf;
1632 return;
1633 }
1634
1635 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1636 || bfd_bread (buf, info->size, abfd) != info->size)
1637 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1638 bfd_get_filename (abfd));
1639 }
1640
1641 /* A helper function that returns the size of a section in a safe way.
1642 If you are positive that the section has been read before using the
1643 size, then it is safe to refer to the dwarf2_section_info object's
1644 "size" field directly. In other cases, you must call this
1645 function, because for compressed sections the size field is not set
1646 correctly until the section has been read. */
1647
1648 static bfd_size_type
1649 dwarf2_section_size (struct objfile *objfile,
1650 struct dwarf2_section_info *info)
1651 {
1652 if (!info->readin)
1653 dwarf2_read_section (objfile, info);
1654 return info->size;
1655 }
1656
1657 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1658 SECTION_NAME. */
1659
1660 void
1661 dwarf2_get_section_info (struct objfile *objfile,
1662 enum dwarf2_section_enum sect,
1663 asection **sectp, gdb_byte **bufp,
1664 bfd_size_type *sizep)
1665 {
1666 struct dwarf2_per_objfile *data
1667 = objfile_data (objfile, dwarf2_objfile_data_key);
1668 struct dwarf2_section_info *info;
1669
1670 /* We may see an objfile without any DWARF, in which case we just
1671 return nothing. */
1672 if (data == NULL)
1673 {
1674 *sectp = NULL;
1675 *bufp = NULL;
1676 *sizep = 0;
1677 return;
1678 }
1679 switch (sect)
1680 {
1681 case DWARF2_DEBUG_FRAME:
1682 info = &data->frame;
1683 break;
1684 case DWARF2_EH_FRAME:
1685 info = &data->eh_frame;
1686 break;
1687 default:
1688 gdb_assert_not_reached ("unexpected section");
1689 }
1690
1691 dwarf2_read_section (objfile, info);
1692
1693 *sectp = info->asection;
1694 *bufp = info->buffer;
1695 *sizep = info->size;
1696 }
1697
1698 \f
1699 /* DWARF quick_symbols_functions support. */
1700
1701 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1702 unique line tables, so we maintain a separate table of all .debug_line
1703 derived entries to support the sharing.
1704 All the quick functions need is the list of file names. We discard the
1705 line_header when we're done and don't need to record it here. */
1706 struct quick_file_names
1707 {
1708 /* The offset in .debug_line of the line table. We hash on this. */
1709 unsigned int offset;
1710
1711 /* The number of entries in file_names, real_names. */
1712 unsigned int num_file_names;
1713
1714 /* The file names from the line table, after being run through
1715 file_full_name. */
1716 const char **file_names;
1717
1718 /* The file names from the line table after being run through
1719 gdb_realpath. These are computed lazily. */
1720 const char **real_names;
1721 };
1722
1723 /* When using the index (and thus not using psymtabs), each CU has an
1724 object of this type. This is used to hold information needed by
1725 the various "quick" methods. */
1726 struct dwarf2_per_cu_quick_data
1727 {
1728 /* The file table. This can be NULL if there was no file table
1729 or it's currently not read in.
1730 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1731 struct quick_file_names *file_names;
1732
1733 /* The corresponding symbol table. This is NULL if symbols for this
1734 CU have not yet been read. */
1735 struct symtab *symtab;
1736
1737 /* A temporary mark bit used when iterating over all CUs in
1738 expand_symtabs_matching. */
1739 unsigned int mark : 1;
1740
1741 /* True if we've tried to read the file table and found there isn't one.
1742 There will be no point in trying to read it again next time. */
1743 unsigned int no_file_data : 1;
1744 };
1745
1746 /* Hash function for a quick_file_names. */
1747
1748 static hashval_t
1749 hash_file_name_entry (const void *e)
1750 {
1751 const struct quick_file_names *file_data = e;
1752
1753 return file_data->offset;
1754 }
1755
1756 /* Equality function for a quick_file_names. */
1757
1758 static int
1759 eq_file_name_entry (const void *a, const void *b)
1760 {
1761 const struct quick_file_names *ea = a;
1762 const struct quick_file_names *eb = b;
1763
1764 return ea->offset == eb->offset;
1765 }
1766
1767 /* Delete function for a quick_file_names. */
1768
1769 static void
1770 delete_file_name_entry (void *e)
1771 {
1772 struct quick_file_names *file_data = e;
1773 int i;
1774
1775 for (i = 0; i < file_data->num_file_names; ++i)
1776 {
1777 xfree ((void*) file_data->file_names[i]);
1778 if (file_data->real_names)
1779 xfree ((void*) file_data->real_names[i]);
1780 }
1781
1782 /* The space for the struct itself lives on objfile_obstack,
1783 so we don't free it here. */
1784 }
1785
1786 /* Create a quick_file_names hash table. */
1787
1788 static htab_t
1789 create_quick_file_names_table (unsigned int nr_initial_entries)
1790 {
1791 return htab_create_alloc (nr_initial_entries,
1792 hash_file_name_entry, eq_file_name_entry,
1793 delete_file_name_entry, xcalloc, xfree);
1794 }
1795
1796 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1797 have to be created afterwards. You should call age_cached_comp_units after
1798 processing PER_CU->CU. dw2_setup must have been already called. */
1799
1800 static void
1801 load_cu (struct dwarf2_per_cu_data *per_cu)
1802 {
1803 if (per_cu->debug_types_section)
1804 load_full_type_unit (per_cu);
1805 else
1806 load_full_comp_unit (per_cu);
1807
1808 gdb_assert (per_cu->cu != NULL);
1809
1810 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1811 }
1812
1813 /* Read in the symbols for PER_CU. */
1814
1815 static void
1816 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1817 {
1818 struct cleanup *back_to;
1819
1820 back_to = make_cleanup (dwarf2_release_queue, NULL);
1821
1822 queue_comp_unit (per_cu);
1823
1824 load_cu (per_cu);
1825
1826 process_queue ();
1827
1828 /* Age the cache, releasing compilation units that have not
1829 been used recently. */
1830 age_cached_comp_units ();
1831
1832 do_cleanups (back_to);
1833 }
1834
1835 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1836 the objfile from which this CU came. Returns the resulting symbol
1837 table. */
1838
1839 static struct symtab *
1840 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1841 {
1842 if (!per_cu->v.quick->symtab)
1843 {
1844 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1845 increment_reading_symtab ();
1846 dw2_do_instantiate_symtab (per_cu);
1847 do_cleanups (back_to);
1848 }
1849 return per_cu->v.quick->symtab;
1850 }
1851
1852 /* Return the CU given its index. */
1853
1854 static struct dwarf2_per_cu_data *
1855 dw2_get_cu (int index)
1856 {
1857 if (index >= dwarf2_per_objfile->n_comp_units)
1858 {
1859 index -= dwarf2_per_objfile->n_comp_units;
1860 return dwarf2_per_objfile->type_comp_units[index];
1861 }
1862 return dwarf2_per_objfile->all_comp_units[index];
1863 }
1864
1865 /* A helper function that knows how to read a 64-bit value in a way
1866 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1867 otherwise. */
1868
1869 static int
1870 extract_cu_value (const char *bytes, ULONGEST *result)
1871 {
1872 if (sizeof (ULONGEST) < 8)
1873 {
1874 int i;
1875
1876 /* Ignore the upper 4 bytes if they are all zero. */
1877 for (i = 0; i < 4; ++i)
1878 if (bytes[i + 4] != 0)
1879 return 0;
1880
1881 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1882 }
1883 else
1884 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1885 return 1;
1886 }
1887
1888 /* Read the CU list from the mapped index, and use it to create all
1889 the CU objects for this objfile. Return 0 if something went wrong,
1890 1 if everything went ok. */
1891
1892 static int
1893 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1894 offset_type cu_list_elements)
1895 {
1896 offset_type i;
1897
1898 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1899 dwarf2_per_objfile->all_comp_units
1900 = obstack_alloc (&objfile->objfile_obstack,
1901 dwarf2_per_objfile->n_comp_units
1902 * sizeof (struct dwarf2_per_cu_data *));
1903
1904 for (i = 0; i < cu_list_elements; i += 2)
1905 {
1906 struct dwarf2_per_cu_data *the_cu;
1907 ULONGEST offset, length;
1908
1909 if (!extract_cu_value (cu_list, &offset)
1910 || !extract_cu_value (cu_list + 8, &length))
1911 return 0;
1912 cu_list += 2 * 8;
1913
1914 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1915 struct dwarf2_per_cu_data);
1916 the_cu->offset = offset;
1917 the_cu->length = length;
1918 the_cu->objfile = objfile;
1919 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1920 struct dwarf2_per_cu_quick_data);
1921 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1922 }
1923
1924 return 1;
1925 }
1926
1927 /* Create the signatured type hash table from the index. */
1928
1929 static int
1930 create_signatured_type_table_from_index (struct objfile *objfile,
1931 struct dwarf2_section_info *section,
1932 const gdb_byte *bytes,
1933 offset_type elements)
1934 {
1935 offset_type i;
1936 htab_t sig_types_hash;
1937
1938 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1939 dwarf2_per_objfile->type_comp_units
1940 = obstack_alloc (&objfile->objfile_obstack,
1941 dwarf2_per_objfile->n_type_comp_units
1942 * sizeof (struct dwarf2_per_cu_data *));
1943
1944 sig_types_hash = allocate_signatured_type_table (objfile);
1945
1946 for (i = 0; i < elements; i += 3)
1947 {
1948 struct signatured_type *type_sig;
1949 ULONGEST offset, type_offset, signature;
1950 void **slot;
1951
1952 if (!extract_cu_value (bytes, &offset)
1953 || !extract_cu_value (bytes + 8, &type_offset))
1954 return 0;
1955 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1956 bytes += 3 * 8;
1957
1958 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1959 struct signatured_type);
1960 type_sig->signature = signature;
1961 type_sig->type_offset = type_offset;
1962 type_sig->per_cu.debug_types_section = section;
1963 type_sig->per_cu.offset = offset;
1964 type_sig->per_cu.objfile = objfile;
1965 type_sig->per_cu.v.quick
1966 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1967 struct dwarf2_per_cu_quick_data);
1968
1969 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1970 *slot = type_sig;
1971
1972 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1973 }
1974
1975 dwarf2_per_objfile->signatured_types = sig_types_hash;
1976
1977 return 1;
1978 }
1979
1980 /* Read the address map data from the mapped index, and use it to
1981 populate the objfile's psymtabs_addrmap. */
1982
1983 static void
1984 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1985 {
1986 const gdb_byte *iter, *end;
1987 struct obstack temp_obstack;
1988 struct addrmap *mutable_map;
1989 struct cleanup *cleanup;
1990 CORE_ADDR baseaddr;
1991
1992 obstack_init (&temp_obstack);
1993 cleanup = make_cleanup_obstack_free (&temp_obstack);
1994 mutable_map = addrmap_create_mutable (&temp_obstack);
1995
1996 iter = index->address_table;
1997 end = iter + index->address_table_size;
1998
1999 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2000
2001 while (iter < end)
2002 {
2003 ULONGEST hi, lo, cu_index;
2004 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2005 iter += 8;
2006 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2007 iter += 8;
2008 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2009 iter += 4;
2010
2011 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2012 dw2_get_cu (cu_index));
2013 }
2014
2015 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2016 &objfile->objfile_obstack);
2017 do_cleanups (cleanup);
2018 }
2019
2020 /* The hash function for strings in the mapped index. This is the same as
2021 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2022 implementation. This is necessary because the hash function is tied to the
2023 format of the mapped index file. The hash values do not have to match with
2024 SYMBOL_HASH_NEXT.
2025
2026 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2027
2028 static hashval_t
2029 mapped_index_string_hash (int index_version, const void *p)
2030 {
2031 const unsigned char *str = (const unsigned char *) p;
2032 hashval_t r = 0;
2033 unsigned char c;
2034
2035 while ((c = *str++) != 0)
2036 {
2037 if (index_version >= 5)
2038 c = tolower (c);
2039 r = r * 67 + c - 113;
2040 }
2041
2042 return r;
2043 }
2044
2045 /* Find a slot in the mapped index INDEX for the object named NAME.
2046 If NAME is found, set *VEC_OUT to point to the CU vector in the
2047 constant pool and return 1. If NAME cannot be found, return 0. */
2048
2049 static int
2050 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2051 offset_type **vec_out)
2052 {
2053 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2054 offset_type hash;
2055 offset_type slot, step;
2056 int (*cmp) (const char *, const char *);
2057
2058 if (current_language->la_language == language_cplus
2059 || current_language->la_language == language_java
2060 || current_language->la_language == language_fortran)
2061 {
2062 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2063 not contain any. */
2064 const char *paren = strchr (name, '(');
2065
2066 if (paren)
2067 {
2068 char *dup;
2069
2070 dup = xmalloc (paren - name + 1);
2071 memcpy (dup, name, paren - name);
2072 dup[paren - name] = 0;
2073
2074 make_cleanup (xfree, dup);
2075 name = dup;
2076 }
2077 }
2078
2079 /* Index version 4 did not support case insensitive searches. But the
2080 indexes for case insensitive languages are built in lowercase, therefore
2081 simulate our NAME being searched is also lowercased. */
2082 hash = mapped_index_string_hash ((index->version == 4
2083 && case_sensitivity == case_sensitive_off
2084 ? 5 : index->version),
2085 name);
2086
2087 slot = hash & (index->symbol_table_slots - 1);
2088 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2089 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2090
2091 for (;;)
2092 {
2093 /* Convert a slot number to an offset into the table. */
2094 offset_type i = 2 * slot;
2095 const char *str;
2096 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2097 {
2098 do_cleanups (back_to);
2099 return 0;
2100 }
2101
2102 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2103 if (!cmp (name, str))
2104 {
2105 *vec_out = (offset_type *) (index->constant_pool
2106 + MAYBE_SWAP (index->symbol_table[i + 1]));
2107 do_cleanups (back_to);
2108 return 1;
2109 }
2110
2111 slot = (slot + step) & (index->symbol_table_slots - 1);
2112 }
2113 }
2114
2115 /* Read the index file. If everything went ok, initialize the "quick"
2116 elements of all the CUs and return 1. Otherwise, return 0. */
2117
2118 static int
2119 dwarf2_read_index (struct objfile *objfile)
2120 {
2121 char *addr;
2122 struct mapped_index *map;
2123 offset_type *metadata;
2124 const gdb_byte *cu_list;
2125 const gdb_byte *types_list = NULL;
2126 offset_type version, cu_list_elements;
2127 offset_type types_list_elements = 0;
2128 int i;
2129
2130 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2131 return 0;
2132
2133 /* Older elfutils strip versions could keep the section in the main
2134 executable while splitting it for the separate debug info file. */
2135 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2136 & SEC_HAS_CONTENTS) == 0)
2137 return 0;
2138
2139 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2140
2141 addr = dwarf2_per_objfile->gdb_index.buffer;
2142 /* Version check. */
2143 version = MAYBE_SWAP (*(offset_type *) addr);
2144 /* Versions earlier than 3 emitted every copy of a psymbol. This
2145 causes the index to behave very poorly for certain requests. Version 3
2146 contained incomplete addrmap. So, it seems better to just ignore such
2147 indices. Index version 4 uses a different hash function than index
2148 version 5 and later. */
2149 if (version < 4)
2150 return 0;
2151 /* Indexes with higher version than the one supported by GDB may be no
2152 longer backward compatible. */
2153 if (version > 5)
2154 return 0;
2155
2156 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2157 map->version = version;
2158 map->total_size = dwarf2_per_objfile->gdb_index.size;
2159
2160 metadata = (offset_type *) (addr + sizeof (offset_type));
2161
2162 i = 0;
2163 cu_list = addr + MAYBE_SWAP (metadata[i]);
2164 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2165 / 8);
2166 ++i;
2167
2168 types_list = addr + MAYBE_SWAP (metadata[i]);
2169 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2170 - MAYBE_SWAP (metadata[i]))
2171 / 8);
2172 ++i;
2173
2174 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2175 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2176 - MAYBE_SWAP (metadata[i]));
2177 ++i;
2178
2179 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2180 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2181 - MAYBE_SWAP (metadata[i]))
2182 / (2 * sizeof (offset_type)));
2183 ++i;
2184
2185 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2186
2187 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2188 return 0;
2189
2190 if (types_list_elements)
2191 {
2192 struct dwarf2_section_info *section;
2193
2194 /* We can only handle a single .debug_types when we have an
2195 index. */
2196 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2197 return 0;
2198
2199 section = VEC_index (dwarf2_section_info_def,
2200 dwarf2_per_objfile->types, 0);
2201
2202 if (!create_signatured_type_table_from_index (objfile, section,
2203 types_list,
2204 types_list_elements))
2205 return 0;
2206 }
2207
2208 create_addrmap_from_index (objfile, map);
2209
2210 dwarf2_per_objfile->index_table = map;
2211 dwarf2_per_objfile->using_index = 1;
2212 dwarf2_per_objfile->quick_file_names_table =
2213 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2214
2215 return 1;
2216 }
2217
2218 /* A helper for the "quick" functions which sets the global
2219 dwarf2_per_objfile according to OBJFILE. */
2220
2221 static void
2222 dw2_setup (struct objfile *objfile)
2223 {
2224 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2225 gdb_assert (dwarf2_per_objfile);
2226 }
2227
2228 /* A helper for the "quick" functions which attempts to read the line
2229 table for THIS_CU. */
2230
2231 static struct quick_file_names *
2232 dw2_get_file_names (struct objfile *objfile,
2233 struct dwarf2_per_cu_data *this_cu)
2234 {
2235 bfd *abfd = objfile->obfd;
2236 struct line_header *lh;
2237 struct attribute *attr;
2238 struct cleanup *cleanups;
2239 struct die_info *comp_unit_die;
2240 struct dwarf2_section_info* sec;
2241 gdb_byte *info_ptr, *buffer;
2242 int has_children, i;
2243 struct dwarf2_cu cu;
2244 unsigned int bytes_read, buffer_size;
2245 struct die_reader_specs reader_specs;
2246 char *name, *comp_dir;
2247 void **slot;
2248 struct quick_file_names *qfn;
2249 unsigned int line_offset;
2250
2251 if (this_cu->v.quick->file_names != NULL)
2252 return this_cu->v.quick->file_names;
2253 /* If we know there is no line data, no point in looking again. */
2254 if (this_cu->v.quick->no_file_data)
2255 return NULL;
2256
2257 init_one_comp_unit (&cu, this_cu);
2258 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2259
2260 if (this_cu->debug_types_section)
2261 sec = this_cu->debug_types_section;
2262 else
2263 sec = &dwarf2_per_objfile->info;
2264 dwarf2_read_section (objfile, sec);
2265 buffer_size = sec->size;
2266 buffer = sec->buffer;
2267 info_ptr = buffer + this_cu->offset;
2268
2269 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2270 buffer, buffer_size,
2271 abfd,
2272 this_cu->debug_types_section != NULL);
2273
2274 /* Skip dummy compilation units. */
2275 if (info_ptr >= buffer + buffer_size
2276 || peek_abbrev_code (abfd, info_ptr) == 0)
2277 {
2278 do_cleanups (cleanups);
2279 return NULL;
2280 }
2281
2282 dwarf2_read_abbrevs (&cu);
2283 make_cleanup (dwarf2_free_abbrev_table, &cu);
2284
2285 init_cu_die_reader (&reader_specs, &cu);
2286 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2287 &has_children);
2288
2289 lh = NULL;
2290 slot = NULL;
2291 line_offset = 0;
2292 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2293 if (attr)
2294 {
2295 struct quick_file_names find_entry;
2296
2297 line_offset = DW_UNSND (attr);
2298
2299 /* We may have already read in this line header (TU line header sharing).
2300 If we have we're done. */
2301 find_entry.offset = line_offset;
2302 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2303 &find_entry, INSERT);
2304 if (*slot != NULL)
2305 {
2306 do_cleanups (cleanups);
2307 this_cu->v.quick->file_names = *slot;
2308 return *slot;
2309 }
2310
2311 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2312 }
2313 if (lh == NULL)
2314 {
2315 do_cleanups (cleanups);
2316 this_cu->v.quick->no_file_data = 1;
2317 return NULL;
2318 }
2319
2320 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2321 qfn->offset = line_offset;
2322 gdb_assert (slot != NULL);
2323 *slot = qfn;
2324
2325 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2326
2327 qfn->num_file_names = lh->num_file_names;
2328 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2329 lh->num_file_names * sizeof (char *));
2330 for (i = 0; i < lh->num_file_names; ++i)
2331 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2332 qfn->real_names = NULL;
2333
2334 free_line_header (lh);
2335 do_cleanups (cleanups);
2336
2337 this_cu->v.quick->file_names = qfn;
2338 return qfn;
2339 }
2340
2341 /* A helper for the "quick" functions which computes and caches the
2342 real path for a given file name from the line table. */
2343
2344 static const char *
2345 dw2_get_real_path (struct objfile *objfile,
2346 struct quick_file_names *qfn, int index)
2347 {
2348 if (qfn->real_names == NULL)
2349 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2350 qfn->num_file_names, sizeof (char *));
2351
2352 if (qfn->real_names[index] == NULL)
2353 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2354
2355 return qfn->real_names[index];
2356 }
2357
2358 static struct symtab *
2359 dw2_find_last_source_symtab (struct objfile *objfile)
2360 {
2361 int index;
2362
2363 dw2_setup (objfile);
2364 index = dwarf2_per_objfile->n_comp_units - 1;
2365 return dw2_instantiate_symtab (dw2_get_cu (index));
2366 }
2367
2368 /* Traversal function for dw2_forget_cached_source_info. */
2369
2370 static int
2371 dw2_free_cached_file_names (void **slot, void *info)
2372 {
2373 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2374
2375 if (file_data->real_names)
2376 {
2377 int i;
2378
2379 for (i = 0; i < file_data->num_file_names; ++i)
2380 {
2381 xfree ((void*) file_data->real_names[i]);
2382 file_data->real_names[i] = NULL;
2383 }
2384 }
2385
2386 return 1;
2387 }
2388
2389 static void
2390 dw2_forget_cached_source_info (struct objfile *objfile)
2391 {
2392 dw2_setup (objfile);
2393
2394 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2395 dw2_free_cached_file_names, NULL);
2396 }
2397
2398 /* Helper function for dw2_map_symtabs_matching_filename that expands
2399 the symtabs and calls the iterator. */
2400
2401 static int
2402 dw2_map_expand_apply (struct objfile *objfile,
2403 struct dwarf2_per_cu_data *per_cu,
2404 const char *name,
2405 const char *full_path, const char *real_path,
2406 int (*callback) (struct symtab *, void *),
2407 void *data)
2408 {
2409 struct symtab *last_made = objfile->symtabs;
2410
2411 /* Don't visit already-expanded CUs. */
2412 if (per_cu->v.quick->symtab)
2413 return 0;
2414
2415 /* This may expand more than one symtab, and we want to iterate over
2416 all of them. */
2417 dw2_instantiate_symtab (per_cu);
2418
2419 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2420 objfile->symtabs, last_made);
2421 }
2422
2423 /* Implementation of the map_symtabs_matching_filename method. */
2424
2425 static int
2426 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2427 const char *full_path, const char *real_path,
2428 int (*callback) (struct symtab *, void *),
2429 void *data)
2430 {
2431 int i;
2432 const char *name_basename = lbasename (name);
2433 int check_basename = name_basename == name;
2434 struct dwarf2_per_cu_data *base_cu = NULL;
2435
2436 dw2_setup (objfile);
2437
2438 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2439 + dwarf2_per_objfile->n_type_comp_units); ++i)
2440 {
2441 int j;
2442 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2443 struct quick_file_names *file_data;
2444
2445 /* We only need to look at symtabs not already expanded. */
2446 if (per_cu->v.quick->symtab)
2447 continue;
2448
2449 file_data = dw2_get_file_names (objfile, per_cu);
2450 if (file_data == NULL)
2451 continue;
2452
2453 for (j = 0; j < file_data->num_file_names; ++j)
2454 {
2455 const char *this_name = file_data->file_names[j];
2456
2457 if (FILENAME_CMP (name, this_name) == 0)
2458 {
2459 if (dw2_map_expand_apply (objfile, per_cu,
2460 name, full_path, real_path,
2461 callback, data))
2462 return 1;
2463 }
2464
2465 if (check_basename && ! base_cu
2466 && FILENAME_CMP (lbasename (this_name), name) == 0)
2467 base_cu = per_cu;
2468
2469 /* Before we invoke realpath, which can get expensive when many
2470 files are involved, do a quick comparison of the basenames. */
2471 if (! basenames_may_differ
2472 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2473 continue;
2474
2475 if (full_path != NULL)
2476 {
2477 const char *this_real_name = dw2_get_real_path (objfile,
2478 file_data, j);
2479
2480 if (this_real_name != NULL
2481 && FILENAME_CMP (full_path, this_real_name) == 0)
2482 {
2483 if (dw2_map_expand_apply (objfile, per_cu,
2484 name, full_path, real_path,
2485 callback, data))
2486 return 1;
2487 }
2488 }
2489
2490 if (real_path != NULL)
2491 {
2492 const char *this_real_name = dw2_get_real_path (objfile,
2493 file_data, j);
2494
2495 if (this_real_name != NULL
2496 && FILENAME_CMP (real_path, this_real_name) == 0)
2497 {
2498 if (dw2_map_expand_apply (objfile, per_cu,
2499 name, full_path, real_path,
2500 callback, data))
2501 return 1;
2502 }
2503 }
2504 }
2505 }
2506
2507 if (base_cu)
2508 {
2509 if (dw2_map_expand_apply (objfile, base_cu,
2510 name, full_path, real_path,
2511 callback, data))
2512 return 1;
2513 }
2514
2515 return 0;
2516 }
2517
2518 static struct symtab *
2519 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2520 const char *name, domain_enum domain)
2521 {
2522 /* We do all the work in the pre_expand_symtabs_matching hook
2523 instead. */
2524 return NULL;
2525 }
2526
2527 /* A helper function that expands all symtabs that hold an object
2528 named NAME. */
2529
2530 static void
2531 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2532 {
2533 dw2_setup (objfile);
2534
2535 /* index_table is NULL if OBJF_READNOW. */
2536 if (dwarf2_per_objfile->index_table)
2537 {
2538 offset_type *vec;
2539
2540 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2541 name, &vec))
2542 {
2543 offset_type i, len = MAYBE_SWAP (*vec);
2544 for (i = 0; i < len; ++i)
2545 {
2546 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2548
2549 dw2_instantiate_symtab (per_cu);
2550 }
2551 }
2552 }
2553 }
2554
2555 static void
2556 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2557 enum block_enum block_kind, const char *name,
2558 domain_enum domain)
2559 {
2560 dw2_do_expand_symtabs_matching (objfile, name);
2561 }
2562
2563 static void
2564 dw2_print_stats (struct objfile *objfile)
2565 {
2566 int i, count;
2567
2568 dw2_setup (objfile);
2569 count = 0;
2570 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2571 + dwarf2_per_objfile->n_type_comp_units); ++i)
2572 {
2573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2574
2575 if (!per_cu->v.quick->symtab)
2576 ++count;
2577 }
2578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2579 }
2580
2581 static void
2582 dw2_dump (struct objfile *objfile)
2583 {
2584 /* Nothing worth printing. */
2585 }
2586
2587 static void
2588 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2589 struct section_offsets *delta)
2590 {
2591 /* There's nothing to relocate here. */
2592 }
2593
2594 static void
2595 dw2_expand_symtabs_for_function (struct objfile *objfile,
2596 const char *func_name)
2597 {
2598 dw2_do_expand_symtabs_matching (objfile, func_name);
2599 }
2600
2601 static void
2602 dw2_expand_all_symtabs (struct objfile *objfile)
2603 {
2604 int i;
2605
2606 dw2_setup (objfile);
2607
2608 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2609 + dwarf2_per_objfile->n_type_comp_units); ++i)
2610 {
2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2612
2613 dw2_instantiate_symtab (per_cu);
2614 }
2615 }
2616
2617 static void
2618 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2619 const char *filename)
2620 {
2621 int i;
2622
2623 dw2_setup (objfile);
2624
2625 /* We don't need to consider type units here.
2626 This is only called for examining code, e.g. expand_line_sal.
2627 There can be an order of magnitude (or more) more type units
2628 than comp units, and we avoid them if we can. */
2629
2630 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2631 {
2632 int j;
2633 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2634 struct quick_file_names *file_data;
2635
2636 /* We only need to look at symtabs not already expanded. */
2637 if (per_cu->v.quick->symtab)
2638 continue;
2639
2640 file_data = dw2_get_file_names (objfile, per_cu);
2641 if (file_data == NULL)
2642 continue;
2643
2644 for (j = 0; j < file_data->num_file_names; ++j)
2645 {
2646 const char *this_name = file_data->file_names[j];
2647 if (FILENAME_CMP (this_name, filename) == 0)
2648 {
2649 dw2_instantiate_symtab (per_cu);
2650 break;
2651 }
2652 }
2653 }
2654 }
2655
2656 static const char *
2657 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2658 {
2659 struct dwarf2_per_cu_data *per_cu;
2660 offset_type *vec;
2661 struct quick_file_names *file_data;
2662
2663 dw2_setup (objfile);
2664
2665 /* index_table is NULL if OBJF_READNOW. */
2666 if (!dwarf2_per_objfile->index_table)
2667 {
2668 struct symtab *s;
2669
2670 ALL_OBJFILE_SYMTABS (objfile, s)
2671 if (s->primary)
2672 {
2673 struct blockvector *bv = BLOCKVECTOR (s);
2674 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2675 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2676
2677 if (sym)
2678 return sym->symtab->filename;
2679 }
2680 return NULL;
2681 }
2682
2683 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2684 name, &vec))
2685 return NULL;
2686
2687 /* Note that this just looks at the very first one named NAME -- but
2688 actually we are looking for a function. find_main_filename
2689 should be rewritten so that it doesn't require a custom hook. It
2690 could just use the ordinary symbol tables. */
2691 /* vec[0] is the length, which must always be >0. */
2692 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2693
2694 file_data = dw2_get_file_names (objfile, per_cu);
2695 if (file_data == NULL)
2696 return NULL;
2697
2698 return file_data->file_names[file_data->num_file_names - 1];
2699 }
2700
2701 static void
2702 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2703 struct objfile *objfile, int global,
2704 int (*callback) (struct block *,
2705 struct symbol *, void *),
2706 void *data, symbol_compare_ftype *match,
2707 symbol_compare_ftype *ordered_compare)
2708 {
2709 /* Currently unimplemented; used for Ada. The function can be called if the
2710 current language is Ada for a non-Ada objfile using GNU index. As Ada
2711 does not look for non-Ada symbols this function should just return. */
2712 }
2713
2714 static void
2715 dw2_expand_symtabs_matching
2716 (struct objfile *objfile,
2717 int (*file_matcher) (const char *, void *),
2718 int (*name_matcher) (const struct language_defn *, const char *, void *),
2719 enum search_domain kind,
2720 void *data)
2721 {
2722 int i;
2723 offset_type iter;
2724 struct mapped_index *index;
2725
2726 dw2_setup (objfile);
2727
2728 /* index_table is NULL if OBJF_READNOW. */
2729 if (!dwarf2_per_objfile->index_table)
2730 return;
2731 index = dwarf2_per_objfile->index_table;
2732
2733 if (file_matcher != NULL)
2734 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2735 + dwarf2_per_objfile->n_type_comp_units); ++i)
2736 {
2737 int j;
2738 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2739 struct quick_file_names *file_data;
2740
2741 per_cu->v.quick->mark = 0;
2742
2743 /* We only need to look at symtabs not already expanded. */
2744 if (per_cu->v.quick->symtab)
2745 continue;
2746
2747 file_data = dw2_get_file_names (objfile, per_cu);
2748 if (file_data == NULL)
2749 continue;
2750
2751 for (j = 0; j < file_data->num_file_names; ++j)
2752 {
2753 if (file_matcher (file_data->file_names[j], data))
2754 {
2755 per_cu->v.quick->mark = 1;
2756 break;
2757 }
2758 }
2759 }
2760
2761 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2762 {
2763 offset_type idx = 2 * iter;
2764 const char *name;
2765 offset_type *vec, vec_len, vec_idx;
2766
2767 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2768 continue;
2769
2770 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2771
2772 if (! (*name_matcher) (current_language, name, data))
2773 continue;
2774
2775 /* The name was matched, now expand corresponding CUs that were
2776 marked. */
2777 vec = (offset_type *) (index->constant_pool
2778 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2779 vec_len = MAYBE_SWAP (vec[0]);
2780 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2781 {
2782 struct dwarf2_per_cu_data *per_cu;
2783
2784 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2785 if (file_matcher == NULL || per_cu->v.quick->mark)
2786 dw2_instantiate_symtab (per_cu);
2787 }
2788 }
2789 }
2790
2791 static struct symtab *
2792 dw2_find_pc_sect_symtab (struct objfile *objfile,
2793 struct minimal_symbol *msymbol,
2794 CORE_ADDR pc,
2795 struct obj_section *section,
2796 int warn_if_readin)
2797 {
2798 struct dwarf2_per_cu_data *data;
2799
2800 dw2_setup (objfile);
2801
2802 if (!objfile->psymtabs_addrmap)
2803 return NULL;
2804
2805 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2806 if (!data)
2807 return NULL;
2808
2809 if (warn_if_readin && data->v.quick->symtab)
2810 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2811 paddress (get_objfile_arch (objfile), pc));
2812
2813 return dw2_instantiate_symtab (data);
2814 }
2815
2816 static void
2817 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2818 void *data, int need_fullname)
2819 {
2820 int i;
2821
2822 dw2_setup (objfile);
2823
2824 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2825 + dwarf2_per_objfile->n_type_comp_units); ++i)
2826 {
2827 int j;
2828 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2829 struct quick_file_names *file_data;
2830
2831 /* We only need to look at symtabs not already expanded. */
2832 if (per_cu->v.quick->symtab)
2833 continue;
2834
2835 file_data = dw2_get_file_names (objfile, per_cu);
2836 if (file_data == NULL)
2837 continue;
2838
2839 for (j = 0; j < file_data->num_file_names; ++j)
2840 {
2841 const char *this_real_name;
2842
2843 if (need_fullname)
2844 this_real_name = dw2_get_real_path (objfile, file_data, j);
2845 else
2846 this_real_name = NULL;
2847 (*fun) (file_data->file_names[j], this_real_name, data);
2848 }
2849 }
2850 }
2851
2852 static int
2853 dw2_has_symbols (struct objfile *objfile)
2854 {
2855 return 1;
2856 }
2857
2858 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2859 {
2860 dw2_has_symbols,
2861 dw2_find_last_source_symtab,
2862 dw2_forget_cached_source_info,
2863 dw2_map_symtabs_matching_filename,
2864 dw2_lookup_symbol,
2865 dw2_pre_expand_symtabs_matching,
2866 dw2_print_stats,
2867 dw2_dump,
2868 dw2_relocate,
2869 dw2_expand_symtabs_for_function,
2870 dw2_expand_all_symtabs,
2871 dw2_expand_symtabs_with_filename,
2872 dw2_find_symbol_file,
2873 dw2_map_matching_symbols,
2874 dw2_expand_symtabs_matching,
2875 dw2_find_pc_sect_symtab,
2876 dw2_map_symbol_filenames
2877 };
2878
2879 /* Initialize for reading DWARF for this objfile. Return 0 if this
2880 file will use psymtabs, or 1 if using the GNU index. */
2881
2882 int
2883 dwarf2_initialize_objfile (struct objfile *objfile)
2884 {
2885 /* If we're about to read full symbols, don't bother with the
2886 indices. In this case we also don't care if some other debug
2887 format is making psymtabs, because they are all about to be
2888 expanded anyway. */
2889 if ((objfile->flags & OBJF_READNOW))
2890 {
2891 int i;
2892
2893 dwarf2_per_objfile->using_index = 1;
2894 create_all_comp_units (objfile);
2895 create_debug_types_hash_table (objfile);
2896 dwarf2_per_objfile->quick_file_names_table =
2897 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2898
2899 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2900 + dwarf2_per_objfile->n_type_comp_units); ++i)
2901 {
2902 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2903
2904 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2905 struct dwarf2_per_cu_quick_data);
2906 }
2907
2908 /* Return 1 so that gdb sees the "quick" functions. However,
2909 these functions will be no-ops because we will have expanded
2910 all symtabs. */
2911 return 1;
2912 }
2913
2914 if (dwarf2_read_index (objfile))
2915 return 1;
2916
2917 return 0;
2918 }
2919
2920 \f
2921
2922 /* Build a partial symbol table. */
2923
2924 void
2925 dwarf2_build_psymtabs (struct objfile *objfile)
2926 {
2927 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2928 {
2929 init_psymbol_list (objfile, 1024);
2930 }
2931
2932 dwarf2_build_psymtabs_hard (objfile);
2933 }
2934
2935 /* Return TRUE if OFFSET is within CU_HEADER. */
2936
2937 static inline int
2938 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2939 {
2940 unsigned int bottom = cu_header->offset;
2941 unsigned int top = (cu_header->offset
2942 + cu_header->length
2943 + cu_header->initial_length_size);
2944
2945 return (offset >= bottom && offset < top);
2946 }
2947
2948 /* Read in the comp unit header information from the debug_info at info_ptr.
2949 NOTE: This leaves members offset, first_die_offset to be filled in
2950 by the caller. */
2951
2952 static gdb_byte *
2953 read_comp_unit_head (struct comp_unit_head *cu_header,
2954 gdb_byte *info_ptr, bfd *abfd)
2955 {
2956 int signed_addr;
2957 unsigned int bytes_read;
2958
2959 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2960 cu_header->initial_length_size = bytes_read;
2961 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2962 info_ptr += bytes_read;
2963 cu_header->version = read_2_bytes (abfd, info_ptr);
2964 info_ptr += 2;
2965 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2966 &bytes_read);
2967 info_ptr += bytes_read;
2968 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2969 info_ptr += 1;
2970 signed_addr = bfd_get_sign_extend_vma (abfd);
2971 if (signed_addr < 0)
2972 internal_error (__FILE__, __LINE__,
2973 _("read_comp_unit_head: dwarf from non elf file"));
2974 cu_header->signed_addr_p = signed_addr;
2975
2976 return info_ptr;
2977 }
2978
2979 /* Read in a CU header and perform some basic error checking. */
2980
2981 static gdb_byte *
2982 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2983 gdb_byte *buffer, unsigned int buffer_size,
2984 bfd *abfd, int is_debug_types_section)
2985 {
2986 gdb_byte *beg_of_comp_unit = info_ptr;
2987
2988 header->offset = beg_of_comp_unit - buffer;
2989
2990 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2991
2992 /* If we're reading a type unit, skip over the signature and
2993 type_offset fields. */
2994 if (is_debug_types_section)
2995 info_ptr += 8 /*signature*/ + header->offset_size;
2996
2997 header->first_die_offset = info_ptr - beg_of_comp_unit;
2998
2999 if (header->version != 2 && header->version != 3 && header->version != 4)
3000 error (_("Dwarf Error: wrong version in compilation unit header "
3001 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3002 bfd_get_filename (abfd));
3003
3004 if (header->abbrev_offset
3005 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3006 &dwarf2_per_objfile->abbrev))
3007 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3008 "(offset 0x%lx + 6) [in module %s]"),
3009 (long) header->abbrev_offset,
3010 (long) (beg_of_comp_unit - buffer),
3011 bfd_get_filename (abfd));
3012
3013 if (beg_of_comp_unit + header->length + header->initial_length_size
3014 > buffer + buffer_size)
3015 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3016 "(offset 0x%lx + 0) [in module %s]"),
3017 (long) header->length,
3018 (long) (beg_of_comp_unit - buffer),
3019 bfd_get_filename (abfd));
3020
3021 return info_ptr;
3022 }
3023
3024 /* Read in the types comp unit header information from .debug_types entry at
3025 types_ptr. The result is a pointer to one past the end of the header. */
3026
3027 static gdb_byte *
3028 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3029 struct dwarf2_section_info *section,
3030 ULONGEST *signature,
3031 gdb_byte *types_ptr, bfd *abfd)
3032 {
3033 gdb_byte *initial_types_ptr = types_ptr;
3034
3035 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3036 cu_header->offset = types_ptr - section->buffer;
3037
3038 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3039
3040 *signature = read_8_bytes (abfd, types_ptr);
3041 types_ptr += 8;
3042 types_ptr += cu_header->offset_size;
3043 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3044
3045 return types_ptr;
3046 }
3047
3048 /* Allocate a new partial symtab for file named NAME and mark this new
3049 partial symtab as being an include of PST. */
3050
3051 static void
3052 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3053 struct objfile *objfile)
3054 {
3055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3056
3057 subpst->section_offsets = pst->section_offsets;
3058 subpst->textlow = 0;
3059 subpst->texthigh = 0;
3060
3061 subpst->dependencies = (struct partial_symtab **)
3062 obstack_alloc (&objfile->objfile_obstack,
3063 sizeof (struct partial_symtab *));
3064 subpst->dependencies[0] = pst;
3065 subpst->number_of_dependencies = 1;
3066
3067 subpst->globals_offset = 0;
3068 subpst->n_global_syms = 0;
3069 subpst->statics_offset = 0;
3070 subpst->n_static_syms = 0;
3071 subpst->symtab = NULL;
3072 subpst->read_symtab = pst->read_symtab;
3073 subpst->readin = 0;
3074
3075 /* No private part is necessary for include psymtabs. This property
3076 can be used to differentiate between such include psymtabs and
3077 the regular ones. */
3078 subpst->read_symtab_private = NULL;
3079 }
3080
3081 /* Read the Line Number Program data and extract the list of files
3082 included by the source file represented by PST. Build an include
3083 partial symtab for each of these included files. */
3084
3085 static void
3086 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3087 struct die_info *die,
3088 struct partial_symtab *pst)
3089 {
3090 struct objfile *objfile = cu->objfile;
3091 bfd *abfd = objfile->obfd;
3092 struct line_header *lh = NULL;
3093 struct attribute *attr;
3094
3095 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3096 if (attr)
3097 {
3098 unsigned int line_offset = DW_UNSND (attr);
3099
3100 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3101 }
3102 if (lh == NULL)
3103 return; /* No linetable, so no includes. */
3104
3105 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3106 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3107
3108 free_line_header (lh);
3109 }
3110
3111 static hashval_t
3112 hash_type_signature (const void *item)
3113 {
3114 const struct signatured_type *type_sig = item;
3115
3116 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3117 return type_sig->signature;
3118 }
3119
3120 static int
3121 eq_type_signature (const void *item_lhs, const void *item_rhs)
3122 {
3123 const struct signatured_type *lhs = item_lhs;
3124 const struct signatured_type *rhs = item_rhs;
3125
3126 return lhs->signature == rhs->signature;
3127 }
3128
3129 /* Allocate a hash table for signatured types. */
3130
3131 static htab_t
3132 allocate_signatured_type_table (struct objfile *objfile)
3133 {
3134 return htab_create_alloc_ex (41,
3135 hash_type_signature,
3136 eq_type_signature,
3137 NULL,
3138 &objfile->objfile_obstack,
3139 hashtab_obstack_allocate,
3140 dummy_obstack_deallocate);
3141 }
3142
3143 /* A helper function to add a signatured type CU to a list. */
3144
3145 static int
3146 add_signatured_type_cu_to_list (void **slot, void *datum)
3147 {
3148 struct signatured_type *sigt = *slot;
3149 struct dwarf2_per_cu_data ***datap = datum;
3150
3151 **datap = &sigt->per_cu;
3152 ++*datap;
3153
3154 return 1;
3155 }
3156
3157 /* Create the hash table of all entries in the .debug_types section.
3158 The result is zero if there is an error (e.g. missing .debug_types section),
3159 otherwise non-zero. */
3160
3161 static int
3162 create_debug_types_hash_table (struct objfile *objfile)
3163 {
3164 htab_t types_htab = NULL;
3165 struct dwarf2_per_cu_data **iter;
3166 int ix;
3167 struct dwarf2_section_info *section;
3168
3169 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3170 {
3171 dwarf2_per_objfile->signatured_types = NULL;
3172 return 0;
3173 }
3174
3175 for (ix = 0;
3176 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3177 ix, section);
3178 ++ix)
3179 {
3180 gdb_byte *info_ptr, *end_ptr;
3181
3182 dwarf2_read_section (objfile, section);
3183 info_ptr = section->buffer;
3184
3185 if (info_ptr == NULL)
3186 continue;
3187
3188 if (types_htab == NULL)
3189 types_htab = allocate_signatured_type_table (objfile);
3190
3191 if (dwarf2_die_debug)
3192 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3193
3194 end_ptr = info_ptr + section->size;
3195 while (info_ptr < end_ptr)
3196 {
3197 unsigned int offset;
3198 unsigned int offset_size;
3199 unsigned int type_offset;
3200 unsigned int length, initial_length_size;
3201 unsigned short version;
3202 ULONGEST signature;
3203 struct signatured_type *type_sig;
3204 void **slot;
3205 gdb_byte *ptr = info_ptr;
3206
3207 offset = ptr - section->buffer;
3208
3209 /* We need to read the type's signature in order to build the hash
3210 table, but we don't need to read anything else just yet. */
3211
3212 /* Sanity check to ensure entire cu is present. */
3213 length = read_initial_length (objfile->obfd, ptr,
3214 &initial_length_size);
3215 if (ptr + length + initial_length_size > end_ptr)
3216 {
3217 complaint (&symfile_complaints,
3218 _("debug type entry runs off end "
3219 "of `.debug_types' section, ignored"));
3220 break;
3221 }
3222
3223 offset_size = initial_length_size == 4 ? 4 : 8;
3224 ptr += initial_length_size;
3225 version = bfd_get_16 (objfile->obfd, ptr);
3226 ptr += 2;
3227 ptr += offset_size; /* abbrev offset */
3228 ptr += 1; /* address size */
3229 signature = bfd_get_64 (objfile->obfd, ptr);
3230 ptr += 8;
3231 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3232 ptr += offset_size;
3233
3234 /* Skip dummy type units. */
3235 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3236 {
3237 info_ptr = info_ptr + initial_length_size + length;
3238 continue;
3239 }
3240
3241 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3242 memset (type_sig, 0, sizeof (*type_sig));
3243 type_sig->signature = signature;
3244 type_sig->type_offset = type_offset;
3245 type_sig->per_cu.objfile = objfile;
3246 type_sig->per_cu.debug_types_section = section;
3247 type_sig->per_cu.offset = offset;
3248
3249 slot = htab_find_slot (types_htab, type_sig, INSERT);
3250 gdb_assert (slot != NULL);
3251 if (*slot != NULL)
3252 {
3253 const struct signatured_type *dup_sig = *slot;
3254
3255 complaint (&symfile_complaints,
3256 _("debug type entry at offset 0x%x is duplicate to the "
3257 "entry at offset 0x%x, signature 0x%s"),
3258 offset, dup_sig->per_cu.offset,
3259 phex (signature, sizeof (signature)));
3260 gdb_assert (signature == dup_sig->signature);
3261 }
3262 *slot = type_sig;
3263
3264 if (dwarf2_die_debug)
3265 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3266 offset, phex (signature, sizeof (signature)));
3267
3268 info_ptr = info_ptr + initial_length_size + length;
3269 }
3270 }
3271
3272 dwarf2_per_objfile->signatured_types = types_htab;
3273
3274 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3275 dwarf2_per_objfile->type_comp_units
3276 = obstack_alloc (&objfile->objfile_obstack,
3277 dwarf2_per_objfile->n_type_comp_units
3278 * sizeof (struct dwarf2_per_cu_data *));
3279 iter = &dwarf2_per_objfile->type_comp_units[0];
3280 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3281 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3282 == dwarf2_per_objfile->n_type_comp_units);
3283
3284 return 1;
3285 }
3286
3287 /* Lookup a signature based type.
3288 Returns NULL if SIG is not present in the table. */
3289
3290 static struct signatured_type *
3291 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3292 {
3293 struct signatured_type find_entry, *entry;
3294
3295 if (dwarf2_per_objfile->signatured_types == NULL)
3296 {
3297 complaint (&symfile_complaints,
3298 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3299 return 0;
3300 }
3301
3302 find_entry.signature = sig;
3303 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3304 return entry;
3305 }
3306
3307 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3308
3309 static void
3310 init_cu_die_reader (struct die_reader_specs *reader,
3311 struct dwarf2_cu *cu)
3312 {
3313 reader->abfd = cu->objfile->obfd;
3314 reader->cu = cu;
3315 if (cu->per_cu->debug_types_section)
3316 {
3317 gdb_assert (cu->per_cu->debug_types_section->readin);
3318 reader->buffer = cu->per_cu->debug_types_section->buffer;
3319 }
3320 else
3321 {
3322 gdb_assert (dwarf2_per_objfile->info.readin);
3323 reader->buffer = dwarf2_per_objfile->info.buffer;
3324 }
3325 }
3326
3327 /* Find the base address of the compilation unit for range lists and
3328 location lists. It will normally be specified by DW_AT_low_pc.
3329 In DWARF-3 draft 4, the base address could be overridden by
3330 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3331 compilation units with discontinuous ranges. */
3332
3333 static void
3334 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3335 {
3336 struct attribute *attr;
3337
3338 cu->base_known = 0;
3339 cu->base_address = 0;
3340
3341 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3342 if (attr)
3343 {
3344 cu->base_address = DW_ADDR (attr);
3345 cu->base_known = 1;
3346 }
3347 else
3348 {
3349 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3350 if (attr)
3351 {
3352 cu->base_address = DW_ADDR (attr);
3353 cu->base_known = 1;
3354 }
3355 }
3356 }
3357
3358 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3359 to combine the common parts.
3360 Process a compilation unit for a psymtab.
3361 BUFFER is a pointer to the beginning of the dwarf section buffer,
3362 either .debug_info or debug_types.
3363 INFO_PTR is a pointer to the start of the CU.
3364 Returns a pointer to the next CU. */
3365
3366 static gdb_byte *
3367 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3368 gdb_byte *buffer, gdb_byte *info_ptr,
3369 unsigned int buffer_size)
3370 {
3371 struct objfile *objfile = this_cu->objfile;
3372 bfd *abfd = objfile->obfd;
3373 gdb_byte *beg_of_comp_unit = info_ptr;
3374 struct die_info *comp_unit_die;
3375 struct partial_symtab *pst;
3376 CORE_ADDR baseaddr;
3377 struct cleanup *back_to_inner;
3378 struct dwarf2_cu cu;
3379 int has_children, has_pc_info;
3380 struct attribute *attr;
3381 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3382 struct die_reader_specs reader_specs;
3383 const char *filename;
3384
3385 /* If this compilation unit was already read in, free the
3386 cached copy in order to read it in again. This is
3387 necessary because we skipped some symbols when we first
3388 read in the compilation unit (see load_partial_dies).
3389 This problem could be avoided, but the benefit is
3390 unclear. */
3391 if (this_cu->cu != NULL)
3392 free_one_cached_comp_unit (this_cu->cu);
3393
3394 /* Note that this is a pointer to our stack frame, being
3395 added to a global data structure. It will be cleaned up
3396 in free_stack_comp_unit when we finish with this
3397 compilation unit. */
3398 init_one_comp_unit (&cu, this_cu);
3399 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3400
3401 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3402 buffer, buffer_size,
3403 abfd,
3404 this_cu->debug_types_section != NULL);
3405
3406 /* Skip dummy compilation units. */
3407 if (info_ptr >= buffer + buffer_size
3408 || peek_abbrev_code (abfd, info_ptr) == 0)
3409 {
3410 info_ptr = (beg_of_comp_unit + cu.header.length
3411 + cu.header.initial_length_size);
3412 do_cleanups (back_to_inner);
3413 return info_ptr;
3414 }
3415
3416 cu.list_in_scope = &file_symbols;
3417
3418 /* Read the abbrevs for this compilation unit into a table. */
3419 dwarf2_read_abbrevs (&cu);
3420 make_cleanup (dwarf2_free_abbrev_table, &cu);
3421
3422 /* Read the compilation unit die. */
3423 init_cu_die_reader (&reader_specs, &cu);
3424 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3425 &has_children);
3426
3427 if (this_cu->debug_types_section)
3428 {
3429 /* LENGTH has not been set yet for type units. */
3430 gdb_assert (this_cu->offset == cu.header.offset);
3431 this_cu->length = cu.header.length + cu.header.initial_length_size;
3432 }
3433 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3434 {
3435 info_ptr = (beg_of_comp_unit + cu.header.length
3436 + cu.header.initial_length_size);
3437 do_cleanups (back_to_inner);
3438 return info_ptr;
3439 }
3440
3441 prepare_one_comp_unit (&cu, comp_unit_die);
3442
3443 /* Allocate a new partial symbol table structure. */
3444 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3445 if (attr == NULL || !DW_STRING (attr))
3446 filename = "";
3447 else
3448 filename = DW_STRING (attr);
3449 pst = start_psymtab_common (objfile, objfile->section_offsets,
3450 filename,
3451 /* TEXTLOW and TEXTHIGH are set below. */
3452 0,
3453 objfile->global_psymbols.next,
3454 objfile->static_psymbols.next);
3455 pst->psymtabs_addrmap_supported = 1;
3456
3457 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3458 if (attr != NULL)
3459 pst->dirname = DW_STRING (attr);
3460
3461 pst->read_symtab_private = this_cu;
3462
3463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3464
3465 /* Store the function that reads in the rest of the symbol table. */
3466 pst->read_symtab = dwarf2_psymtab_to_symtab;
3467
3468 this_cu->v.psymtab = pst;
3469
3470 dwarf2_find_base_address (comp_unit_die, &cu);
3471
3472 /* Possibly set the default values of LOWPC and HIGHPC from
3473 `DW_AT_ranges'. */
3474 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3475 &best_highpc, &cu, pst);
3476 if (has_pc_info == 1 && best_lowpc < best_highpc)
3477 /* Store the contiguous range if it is not empty; it can be empty for
3478 CUs with no code. */
3479 addrmap_set_empty (objfile->psymtabs_addrmap,
3480 best_lowpc + baseaddr,
3481 best_highpc + baseaddr - 1, pst);
3482
3483 /* Check if comp unit has_children.
3484 If so, read the rest of the partial symbols from this comp unit.
3485 If not, there's no more debug_info for this comp unit. */
3486 if (has_children)
3487 {
3488 struct partial_die_info *first_die;
3489 CORE_ADDR lowpc, highpc;
3490
3491 lowpc = ((CORE_ADDR) -1);
3492 highpc = ((CORE_ADDR) 0);
3493
3494 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3495
3496 scan_partial_symbols (first_die, &lowpc, &highpc,
3497 ! has_pc_info, &cu);
3498
3499 /* If we didn't find a lowpc, set it to highpc to avoid
3500 complaints from `maint check'. */
3501 if (lowpc == ((CORE_ADDR) -1))
3502 lowpc = highpc;
3503
3504 /* If the compilation unit didn't have an explicit address range,
3505 then use the information extracted from its child dies. */
3506 if (! has_pc_info)
3507 {
3508 best_lowpc = lowpc;
3509 best_highpc = highpc;
3510 }
3511 }
3512 pst->textlow = best_lowpc + baseaddr;
3513 pst->texthigh = best_highpc + baseaddr;
3514
3515 pst->n_global_syms = objfile->global_psymbols.next -
3516 (objfile->global_psymbols.list + pst->globals_offset);
3517 pst->n_static_syms = objfile->static_psymbols.next -
3518 (objfile->static_psymbols.list + pst->statics_offset);
3519 sort_pst_symbols (pst);
3520
3521 info_ptr = (beg_of_comp_unit + cu.header.length
3522 + cu.header.initial_length_size);
3523
3524 if (this_cu->debug_types_section)
3525 {
3526 /* It's not clear we want to do anything with stmt lists here.
3527 Waiting to see what gcc ultimately does. */
3528 }
3529 else
3530 {
3531 /* Get the list of files included in the current compilation unit,
3532 and build a psymtab for each of them. */
3533 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3534 }
3535
3536 do_cleanups (back_to_inner);
3537
3538 return info_ptr;
3539 }
3540
3541 /* Traversal function for htab_traverse_noresize.
3542 Process one .debug_types comp-unit. */
3543
3544 static int
3545 process_type_comp_unit (void **slot, void *info)
3546 {
3547 struct signatured_type *entry = (struct signatured_type *) *slot;
3548 struct dwarf2_per_cu_data *this_cu;
3549
3550 gdb_assert (info == NULL);
3551 this_cu = &entry->per_cu;
3552
3553 gdb_assert (this_cu->debug_types_section->readin);
3554 process_psymtab_comp_unit (this_cu,
3555 this_cu->debug_types_section->buffer,
3556 (this_cu->debug_types_section->buffer
3557 + this_cu->offset),
3558 this_cu->debug_types_section->size);
3559
3560 return 1;
3561 }
3562
3563 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3564 Build partial symbol tables for the .debug_types comp-units. */
3565
3566 static void
3567 build_type_psymtabs (struct objfile *objfile)
3568 {
3569 if (! create_debug_types_hash_table (objfile))
3570 return;
3571
3572 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3573 process_type_comp_unit, NULL);
3574 }
3575
3576 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3577
3578 static void
3579 psymtabs_addrmap_cleanup (void *o)
3580 {
3581 struct objfile *objfile = o;
3582
3583 objfile->psymtabs_addrmap = NULL;
3584 }
3585
3586 /* Build the partial symbol table by doing a quick pass through the
3587 .debug_info and .debug_abbrev sections. */
3588
3589 static void
3590 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3591 {
3592 gdb_byte *info_ptr;
3593 struct cleanup *back_to, *addrmap_cleanup;
3594 struct obstack temp_obstack;
3595
3596 dwarf2_per_objfile->reading_partial_symbols = 1;
3597
3598 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3599 info_ptr = dwarf2_per_objfile->info.buffer;
3600
3601 /* Any cached compilation units will be linked by the per-objfile
3602 read_in_chain. Make sure to free them when we're done. */
3603 back_to = make_cleanup (free_cached_comp_units, NULL);
3604
3605 build_type_psymtabs (objfile);
3606
3607 create_all_comp_units (objfile);
3608
3609 /* Create a temporary address map on a temporary obstack. We later
3610 copy this to the final obstack. */
3611 obstack_init (&temp_obstack);
3612 make_cleanup_obstack_free (&temp_obstack);
3613 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3614 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3615
3616 /* Since the objects we're extracting from .debug_info vary in
3617 length, only the individual functions to extract them (like
3618 read_comp_unit_head and load_partial_die) can really know whether
3619 the buffer is large enough to hold another complete object.
3620
3621 At the moment, they don't actually check that. If .debug_info
3622 holds just one extra byte after the last compilation unit's dies,
3623 then read_comp_unit_head will happily read off the end of the
3624 buffer. read_partial_die is similarly casual. Those functions
3625 should be fixed.
3626
3627 For this loop condition, simply checking whether there's any data
3628 left at all should be sufficient. */
3629
3630 while (info_ptr < (dwarf2_per_objfile->info.buffer
3631 + dwarf2_per_objfile->info.size))
3632 {
3633 struct dwarf2_per_cu_data *this_cu;
3634
3635 this_cu = dwarf2_find_comp_unit (info_ptr
3636 - dwarf2_per_objfile->info.buffer,
3637 objfile);
3638
3639 info_ptr = process_psymtab_comp_unit (this_cu,
3640 dwarf2_per_objfile->info.buffer,
3641 info_ptr,
3642 dwarf2_per_objfile->info.size);
3643 }
3644
3645 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3646 &objfile->objfile_obstack);
3647 discard_cleanups (addrmap_cleanup);
3648
3649 do_cleanups (back_to);
3650 }
3651
3652 /* Load the partial DIEs for a secondary CU into memory. */
3653
3654 static void
3655 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3656 {
3657 struct objfile *objfile = this_cu->objfile;
3658 bfd *abfd = objfile->obfd;
3659 gdb_byte *info_ptr;
3660 struct die_info *comp_unit_die;
3661 struct dwarf2_cu *cu;
3662 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3663 int has_children;
3664 struct die_reader_specs reader_specs;
3665 int read_cu = 0;
3666
3667 gdb_assert (! this_cu->debug_types_section);
3668
3669 gdb_assert (dwarf2_per_objfile->info.readin);
3670 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3671
3672 if (this_cu->cu == NULL)
3673 {
3674 cu = xmalloc (sizeof (*cu));
3675 init_one_comp_unit (cu, this_cu);
3676
3677 read_cu = 1;
3678
3679 /* If an error occurs while loading, release our storage. */
3680 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3681
3682 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3683 dwarf2_per_objfile->info.buffer,
3684 dwarf2_per_objfile->info.size,
3685 abfd, 0);
3686
3687 /* Skip dummy compilation units. */
3688 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3689 + dwarf2_per_objfile->info.size)
3690 || peek_abbrev_code (abfd, info_ptr) == 0)
3691 {
3692 do_cleanups (free_cu_cleanup);
3693 return;
3694 }
3695
3696 /* Link this CU into read_in_chain. */
3697 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3698 dwarf2_per_objfile->read_in_chain = this_cu;
3699 }
3700 else
3701 {
3702 cu = this_cu->cu;
3703 info_ptr += cu->header.first_die_offset;
3704 }
3705
3706 /* Read the abbrevs for this compilation unit into a table. */
3707 gdb_assert (cu->dwarf2_abbrevs == NULL);
3708 dwarf2_read_abbrevs (cu);
3709 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3710
3711 /* Read the compilation unit die. */
3712 init_cu_die_reader (&reader_specs, cu);
3713 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3714 &has_children);
3715
3716 prepare_one_comp_unit (cu, comp_unit_die);
3717
3718 /* Check if comp unit has_children.
3719 If so, read the rest of the partial symbols from this comp unit.
3720 If not, there's no more debug_info for this comp unit. */
3721 if (has_children)
3722 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3723
3724 do_cleanups (free_abbrevs_cleanup);
3725
3726 if (read_cu)
3727 {
3728 /* We've successfully allocated this compilation unit. Let our
3729 caller clean it up when finished with it. */
3730 discard_cleanups (free_cu_cleanup);
3731 }
3732 }
3733
3734 /* Create a list of all compilation units in OBJFILE.
3735 This is only done for -readnow and building partial symtabs. */
3736
3737 static void
3738 create_all_comp_units (struct objfile *objfile)
3739 {
3740 int n_allocated;
3741 int n_comp_units;
3742 struct dwarf2_per_cu_data **all_comp_units;
3743 gdb_byte *info_ptr;
3744
3745 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3746 info_ptr = dwarf2_per_objfile->info.buffer;
3747
3748 n_comp_units = 0;
3749 n_allocated = 10;
3750 all_comp_units = xmalloc (n_allocated
3751 * sizeof (struct dwarf2_per_cu_data *));
3752
3753 while (info_ptr < dwarf2_per_objfile->info.buffer
3754 + dwarf2_per_objfile->info.size)
3755 {
3756 unsigned int length, initial_length_size;
3757 struct dwarf2_per_cu_data *this_cu;
3758 unsigned int offset;
3759
3760 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3761
3762 /* Read just enough information to find out where the next
3763 compilation unit is. */
3764 length = read_initial_length (objfile->obfd, info_ptr,
3765 &initial_length_size);
3766
3767 /* Save the compilation unit for later lookup. */
3768 this_cu = obstack_alloc (&objfile->objfile_obstack,
3769 sizeof (struct dwarf2_per_cu_data));
3770 memset (this_cu, 0, sizeof (*this_cu));
3771 this_cu->offset = offset;
3772 this_cu->length = length + initial_length_size;
3773 this_cu->objfile = objfile;
3774
3775 if (n_comp_units == n_allocated)
3776 {
3777 n_allocated *= 2;
3778 all_comp_units = xrealloc (all_comp_units,
3779 n_allocated
3780 * sizeof (struct dwarf2_per_cu_data *));
3781 }
3782 all_comp_units[n_comp_units++] = this_cu;
3783
3784 info_ptr = info_ptr + this_cu->length;
3785 }
3786
3787 dwarf2_per_objfile->all_comp_units
3788 = obstack_alloc (&objfile->objfile_obstack,
3789 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3790 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3791 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3792 xfree (all_comp_units);
3793 dwarf2_per_objfile->n_comp_units = n_comp_units;
3794 }
3795
3796 /* Process all loaded DIEs for compilation unit CU, starting at
3797 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3798 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3799 DW_AT_ranges). If NEED_PC is set, then this function will set
3800 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3801 and record the covered ranges in the addrmap. */
3802
3803 static void
3804 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3805 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3806 {
3807 struct partial_die_info *pdi;
3808
3809 /* Now, march along the PDI's, descending into ones which have
3810 interesting children but skipping the children of the other ones,
3811 until we reach the end of the compilation unit. */
3812
3813 pdi = first_die;
3814
3815 while (pdi != NULL)
3816 {
3817 fixup_partial_die (pdi, cu);
3818
3819 /* Anonymous namespaces or modules have no name but have interesting
3820 children, so we need to look at them. Ditto for anonymous
3821 enums. */
3822
3823 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3824 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3825 {
3826 switch (pdi->tag)
3827 {
3828 case DW_TAG_subprogram:
3829 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3830 break;
3831 case DW_TAG_constant:
3832 case DW_TAG_variable:
3833 case DW_TAG_typedef:
3834 case DW_TAG_union_type:
3835 if (!pdi->is_declaration)
3836 {
3837 add_partial_symbol (pdi, cu);
3838 }
3839 break;
3840 case DW_TAG_class_type:
3841 case DW_TAG_interface_type:
3842 case DW_TAG_structure_type:
3843 if (!pdi->is_declaration)
3844 {
3845 add_partial_symbol (pdi, cu);
3846 }
3847 break;
3848 case DW_TAG_enumeration_type:
3849 if (!pdi->is_declaration)
3850 add_partial_enumeration (pdi, cu);
3851 break;
3852 case DW_TAG_base_type:
3853 case DW_TAG_subrange_type:
3854 /* File scope base type definitions are added to the partial
3855 symbol table. */
3856 add_partial_symbol (pdi, cu);
3857 break;
3858 case DW_TAG_namespace:
3859 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3860 break;
3861 case DW_TAG_module:
3862 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3863 break;
3864 default:
3865 break;
3866 }
3867 }
3868
3869 /* If the die has a sibling, skip to the sibling. */
3870
3871 pdi = pdi->die_sibling;
3872 }
3873 }
3874
3875 /* Functions used to compute the fully scoped name of a partial DIE.
3876
3877 Normally, this is simple. For C++, the parent DIE's fully scoped
3878 name is concatenated with "::" and the partial DIE's name. For
3879 Java, the same thing occurs except that "." is used instead of "::".
3880 Enumerators are an exception; they use the scope of their parent
3881 enumeration type, i.e. the name of the enumeration type is not
3882 prepended to the enumerator.
3883
3884 There are two complexities. One is DW_AT_specification; in this
3885 case "parent" means the parent of the target of the specification,
3886 instead of the direct parent of the DIE. The other is compilers
3887 which do not emit DW_TAG_namespace; in this case we try to guess
3888 the fully qualified name of structure types from their members'
3889 linkage names. This must be done using the DIE's children rather
3890 than the children of any DW_AT_specification target. We only need
3891 to do this for structures at the top level, i.e. if the target of
3892 any DW_AT_specification (if any; otherwise the DIE itself) does not
3893 have a parent. */
3894
3895 /* Compute the scope prefix associated with PDI's parent, in
3896 compilation unit CU. The result will be allocated on CU's
3897 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3898 field. NULL is returned if no prefix is necessary. */
3899 static char *
3900 partial_die_parent_scope (struct partial_die_info *pdi,
3901 struct dwarf2_cu *cu)
3902 {
3903 char *grandparent_scope;
3904 struct partial_die_info *parent, *real_pdi;
3905
3906 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3907 then this means the parent of the specification DIE. */
3908
3909 real_pdi = pdi;
3910 while (real_pdi->has_specification)
3911 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3912
3913 parent = real_pdi->die_parent;
3914 if (parent == NULL)
3915 return NULL;
3916
3917 if (parent->scope_set)
3918 return parent->scope;
3919
3920 fixup_partial_die (parent, cu);
3921
3922 grandparent_scope = partial_die_parent_scope (parent, cu);
3923
3924 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3925 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3926 Work around this problem here. */
3927 if (cu->language == language_cplus
3928 && parent->tag == DW_TAG_namespace
3929 && strcmp (parent->name, "::") == 0
3930 && grandparent_scope == NULL)
3931 {
3932 parent->scope = NULL;
3933 parent->scope_set = 1;
3934 return NULL;
3935 }
3936
3937 if (pdi->tag == DW_TAG_enumerator)
3938 /* Enumerators should not get the name of the enumeration as a prefix. */
3939 parent->scope = grandparent_scope;
3940 else if (parent->tag == DW_TAG_namespace
3941 || parent->tag == DW_TAG_module
3942 || parent->tag == DW_TAG_structure_type
3943 || parent->tag == DW_TAG_class_type
3944 || parent->tag == DW_TAG_interface_type
3945 || parent->tag == DW_TAG_union_type
3946 || parent->tag == DW_TAG_enumeration_type)
3947 {
3948 if (grandparent_scope == NULL)
3949 parent->scope = parent->name;
3950 else
3951 parent->scope = typename_concat (&cu->comp_unit_obstack,
3952 grandparent_scope,
3953 parent->name, 0, cu);
3954 }
3955 else
3956 {
3957 /* FIXME drow/2004-04-01: What should we be doing with
3958 function-local names? For partial symbols, we should probably be
3959 ignoring them. */
3960 complaint (&symfile_complaints,
3961 _("unhandled containing DIE tag %d for DIE at %d"),
3962 parent->tag, pdi->offset);
3963 parent->scope = grandparent_scope;
3964 }
3965
3966 parent->scope_set = 1;
3967 return parent->scope;
3968 }
3969
3970 /* Return the fully scoped name associated with PDI, from compilation unit
3971 CU. The result will be allocated with malloc. */
3972 static char *
3973 partial_die_full_name (struct partial_die_info *pdi,
3974 struct dwarf2_cu *cu)
3975 {
3976 char *parent_scope;
3977
3978 /* If this is a template instantiation, we can not work out the
3979 template arguments from partial DIEs. So, unfortunately, we have
3980 to go through the full DIEs. At least any work we do building
3981 types here will be reused if full symbols are loaded later. */
3982 if (pdi->has_template_arguments)
3983 {
3984 fixup_partial_die (pdi, cu);
3985
3986 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3987 {
3988 struct die_info *die;
3989 struct attribute attr;
3990 struct dwarf2_cu *ref_cu = cu;
3991
3992 attr.name = 0;
3993 attr.form = DW_FORM_ref_addr;
3994 attr.u.addr = pdi->offset;
3995 die = follow_die_ref (NULL, &attr, &ref_cu);
3996
3997 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3998 }
3999 }
4000
4001 parent_scope = partial_die_parent_scope (pdi, cu);
4002 if (parent_scope == NULL)
4003 return NULL;
4004 else
4005 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4006 }
4007
4008 static void
4009 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4010 {
4011 struct objfile *objfile = cu->objfile;
4012 CORE_ADDR addr = 0;
4013 char *actual_name = NULL;
4014 const struct partial_symbol *psym = NULL;
4015 CORE_ADDR baseaddr;
4016 int built_actual_name = 0;
4017
4018 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4019
4020 actual_name = partial_die_full_name (pdi, cu);
4021 if (actual_name)
4022 built_actual_name = 1;
4023
4024 if (actual_name == NULL)
4025 actual_name = pdi->name;
4026
4027 switch (pdi->tag)
4028 {
4029 case DW_TAG_subprogram:
4030 if (pdi->is_external || cu->language == language_ada)
4031 {
4032 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4033 of the global scope. But in Ada, we want to be able to access
4034 nested procedures globally. So all Ada subprograms are stored
4035 in the global scope. */
4036 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4037 mst_text, objfile); */
4038 add_psymbol_to_list (actual_name, strlen (actual_name),
4039 built_actual_name,
4040 VAR_DOMAIN, LOC_BLOCK,
4041 &objfile->global_psymbols,
4042 0, pdi->lowpc + baseaddr,
4043 cu->language, objfile);
4044 }
4045 else
4046 {
4047 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4048 mst_file_text, objfile); */
4049 add_psymbol_to_list (actual_name, strlen (actual_name),
4050 built_actual_name,
4051 VAR_DOMAIN, LOC_BLOCK,
4052 &objfile->static_psymbols,
4053 0, pdi->lowpc + baseaddr,
4054 cu->language, objfile);
4055 }
4056 break;
4057 case DW_TAG_constant:
4058 {
4059 struct psymbol_allocation_list *list;
4060
4061 if (pdi->is_external)
4062 list = &objfile->global_psymbols;
4063 else
4064 list = &objfile->static_psymbols;
4065 add_psymbol_to_list (actual_name, strlen (actual_name),
4066 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4067 list, 0, 0, cu->language, objfile);
4068 }
4069 break;
4070 case DW_TAG_variable:
4071 if (pdi->locdesc)
4072 addr = decode_locdesc (pdi->locdesc, cu);
4073
4074 if (pdi->locdesc
4075 && addr == 0
4076 && !dwarf2_per_objfile->has_section_at_zero)
4077 {
4078 /* A global or static variable may also have been stripped
4079 out by the linker if unused, in which case its address
4080 will be nullified; do not add such variables into partial
4081 symbol table then. */
4082 }
4083 else if (pdi->is_external)
4084 {
4085 /* Global Variable.
4086 Don't enter into the minimal symbol tables as there is
4087 a minimal symbol table entry from the ELF symbols already.
4088 Enter into partial symbol table if it has a location
4089 descriptor or a type.
4090 If the location descriptor is missing, new_symbol will create
4091 a LOC_UNRESOLVED symbol, the address of the variable will then
4092 be determined from the minimal symbol table whenever the variable
4093 is referenced.
4094 The address for the partial symbol table entry is not
4095 used by GDB, but it comes in handy for debugging partial symbol
4096 table building. */
4097
4098 if (pdi->locdesc || pdi->has_type)
4099 add_psymbol_to_list (actual_name, strlen (actual_name),
4100 built_actual_name,
4101 VAR_DOMAIN, LOC_STATIC,
4102 &objfile->global_psymbols,
4103 0, addr + baseaddr,
4104 cu->language, objfile);
4105 }
4106 else
4107 {
4108 /* Static Variable. Skip symbols without location descriptors. */
4109 if (pdi->locdesc == NULL)
4110 {
4111 if (built_actual_name)
4112 xfree (actual_name);
4113 return;
4114 }
4115 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4116 mst_file_data, objfile); */
4117 add_psymbol_to_list (actual_name, strlen (actual_name),
4118 built_actual_name,
4119 VAR_DOMAIN, LOC_STATIC,
4120 &objfile->static_psymbols,
4121 0, addr + baseaddr,
4122 cu->language, objfile);
4123 }
4124 break;
4125 case DW_TAG_typedef:
4126 case DW_TAG_base_type:
4127 case DW_TAG_subrange_type:
4128 add_psymbol_to_list (actual_name, strlen (actual_name),
4129 built_actual_name,
4130 VAR_DOMAIN, LOC_TYPEDEF,
4131 &objfile->static_psymbols,
4132 0, (CORE_ADDR) 0, cu->language, objfile);
4133 break;
4134 case DW_TAG_namespace:
4135 add_psymbol_to_list (actual_name, strlen (actual_name),
4136 built_actual_name,
4137 VAR_DOMAIN, LOC_TYPEDEF,
4138 &objfile->global_psymbols,
4139 0, (CORE_ADDR) 0, cu->language, objfile);
4140 break;
4141 case DW_TAG_class_type:
4142 case DW_TAG_interface_type:
4143 case DW_TAG_structure_type:
4144 case DW_TAG_union_type:
4145 case DW_TAG_enumeration_type:
4146 /* Skip external references. The DWARF standard says in the section
4147 about "Structure, Union, and Class Type Entries": "An incomplete
4148 structure, union or class type is represented by a structure,
4149 union or class entry that does not have a byte size attribute
4150 and that has a DW_AT_declaration attribute." */
4151 if (!pdi->has_byte_size && pdi->is_declaration)
4152 {
4153 if (built_actual_name)
4154 xfree (actual_name);
4155 return;
4156 }
4157
4158 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4159 static vs. global. */
4160 add_psymbol_to_list (actual_name, strlen (actual_name),
4161 built_actual_name,
4162 STRUCT_DOMAIN, LOC_TYPEDEF,
4163 (cu->language == language_cplus
4164 || cu->language == language_java)
4165 ? &objfile->global_psymbols
4166 : &objfile->static_psymbols,
4167 0, (CORE_ADDR) 0, cu->language, objfile);
4168
4169 break;
4170 case DW_TAG_enumerator:
4171 add_psymbol_to_list (actual_name, strlen (actual_name),
4172 built_actual_name,
4173 VAR_DOMAIN, LOC_CONST,
4174 (cu->language == language_cplus
4175 || cu->language == language_java)
4176 ? &objfile->global_psymbols
4177 : &objfile->static_psymbols,
4178 0, (CORE_ADDR) 0, cu->language, objfile);
4179 break;
4180 default:
4181 break;
4182 }
4183
4184 if (built_actual_name)
4185 xfree (actual_name);
4186 }
4187
4188 /* Read a partial die corresponding to a namespace; also, add a symbol
4189 corresponding to that namespace to the symbol table. NAMESPACE is
4190 the name of the enclosing namespace. */
4191
4192 static void
4193 add_partial_namespace (struct partial_die_info *pdi,
4194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4195 int need_pc, struct dwarf2_cu *cu)
4196 {
4197 /* Add a symbol for the namespace. */
4198
4199 add_partial_symbol (pdi, cu);
4200
4201 /* Now scan partial symbols in that namespace. */
4202
4203 if (pdi->has_children)
4204 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4205 }
4206
4207 /* Read a partial die corresponding to a Fortran module. */
4208
4209 static void
4210 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4211 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4212 {
4213 /* Now scan partial symbols in that module. */
4214
4215 if (pdi->has_children)
4216 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4217 }
4218
4219 /* Read a partial die corresponding to a subprogram and create a partial
4220 symbol for that subprogram. When the CU language allows it, this
4221 routine also defines a partial symbol for each nested subprogram
4222 that this subprogram contains.
4223
4224 DIE my also be a lexical block, in which case we simply search
4225 recursively for suprograms defined inside that lexical block.
4226 Again, this is only performed when the CU language allows this
4227 type of definitions. */
4228
4229 static void
4230 add_partial_subprogram (struct partial_die_info *pdi,
4231 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4232 int need_pc, struct dwarf2_cu *cu)
4233 {
4234 if (pdi->tag == DW_TAG_subprogram)
4235 {
4236 if (pdi->has_pc_info)
4237 {
4238 if (pdi->lowpc < *lowpc)
4239 *lowpc = pdi->lowpc;
4240 if (pdi->highpc > *highpc)
4241 *highpc = pdi->highpc;
4242 if (need_pc)
4243 {
4244 CORE_ADDR baseaddr;
4245 struct objfile *objfile = cu->objfile;
4246
4247 baseaddr = ANOFFSET (objfile->section_offsets,
4248 SECT_OFF_TEXT (objfile));
4249 addrmap_set_empty (objfile->psymtabs_addrmap,
4250 pdi->lowpc + baseaddr,
4251 pdi->highpc - 1 + baseaddr,
4252 cu->per_cu->v.psymtab);
4253 }
4254 if (!pdi->is_declaration)
4255 /* Ignore subprogram DIEs that do not have a name, they are
4256 illegal. Do not emit a complaint at this point, we will
4257 do so when we convert this psymtab into a symtab. */
4258 if (pdi->name)
4259 add_partial_symbol (pdi, cu);
4260 }
4261 }
4262
4263 if (! pdi->has_children)
4264 return;
4265
4266 if (cu->language == language_ada)
4267 {
4268 pdi = pdi->die_child;
4269 while (pdi != NULL)
4270 {
4271 fixup_partial_die (pdi, cu);
4272 if (pdi->tag == DW_TAG_subprogram
4273 || pdi->tag == DW_TAG_lexical_block)
4274 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4275 pdi = pdi->die_sibling;
4276 }
4277 }
4278 }
4279
4280 /* Read a partial die corresponding to an enumeration type. */
4281
4282 static void
4283 add_partial_enumeration (struct partial_die_info *enum_pdi,
4284 struct dwarf2_cu *cu)
4285 {
4286 struct partial_die_info *pdi;
4287
4288 if (enum_pdi->name != NULL)
4289 add_partial_symbol (enum_pdi, cu);
4290
4291 pdi = enum_pdi->die_child;
4292 while (pdi)
4293 {
4294 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4295 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4296 else
4297 add_partial_symbol (pdi, cu);
4298 pdi = pdi->die_sibling;
4299 }
4300 }
4301
4302 /* Return the initial uleb128 in the die at INFO_PTR. */
4303
4304 static unsigned int
4305 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4306 {
4307 unsigned int bytes_read;
4308
4309 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4310 }
4311
4312 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4313 Return the corresponding abbrev, or NULL if the number is zero (indicating
4314 an empty DIE). In either case *BYTES_READ will be set to the length of
4315 the initial number. */
4316
4317 static struct abbrev_info *
4318 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4319 struct dwarf2_cu *cu)
4320 {
4321 bfd *abfd = cu->objfile->obfd;
4322 unsigned int abbrev_number;
4323 struct abbrev_info *abbrev;
4324
4325 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4326
4327 if (abbrev_number == 0)
4328 return NULL;
4329
4330 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4331 if (!abbrev)
4332 {
4333 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4334 abbrev_number, bfd_get_filename (abfd));
4335 }
4336
4337 return abbrev;
4338 }
4339
4340 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4341 Returns a pointer to the end of a series of DIEs, terminated by an empty
4342 DIE. Any children of the skipped DIEs will also be skipped. */
4343
4344 static gdb_byte *
4345 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4346 {
4347 struct abbrev_info *abbrev;
4348 unsigned int bytes_read;
4349
4350 while (1)
4351 {
4352 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4353 if (abbrev == NULL)
4354 return info_ptr + bytes_read;
4355 else
4356 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4357 }
4358 }
4359
4360 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4361 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4362 abbrev corresponding to that skipped uleb128 should be passed in
4363 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4364 children. */
4365
4366 static gdb_byte *
4367 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4368 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4369 {
4370 unsigned int bytes_read;
4371 struct attribute attr;
4372 bfd *abfd = cu->objfile->obfd;
4373 unsigned int form, i;
4374
4375 for (i = 0; i < abbrev->num_attrs; i++)
4376 {
4377 /* The only abbrev we care about is DW_AT_sibling. */
4378 if (abbrev->attrs[i].name == DW_AT_sibling)
4379 {
4380 read_attribute (&attr, &abbrev->attrs[i],
4381 abfd, info_ptr, cu);
4382 if (attr.form == DW_FORM_ref_addr)
4383 complaint (&symfile_complaints,
4384 _("ignoring absolute DW_AT_sibling"));
4385 else
4386 return buffer + dwarf2_get_ref_die_offset (&attr);
4387 }
4388
4389 /* If it isn't DW_AT_sibling, skip this attribute. */
4390 form = abbrev->attrs[i].form;
4391 skip_attribute:
4392 switch (form)
4393 {
4394 case DW_FORM_ref_addr:
4395 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4396 and later it is offset sized. */
4397 if (cu->header.version == 2)
4398 info_ptr += cu->header.addr_size;
4399 else
4400 info_ptr += cu->header.offset_size;
4401 break;
4402 case DW_FORM_addr:
4403 info_ptr += cu->header.addr_size;
4404 break;
4405 case DW_FORM_data1:
4406 case DW_FORM_ref1:
4407 case DW_FORM_flag:
4408 info_ptr += 1;
4409 break;
4410 case DW_FORM_flag_present:
4411 break;
4412 case DW_FORM_data2:
4413 case DW_FORM_ref2:
4414 info_ptr += 2;
4415 break;
4416 case DW_FORM_data4:
4417 case DW_FORM_ref4:
4418 info_ptr += 4;
4419 break;
4420 case DW_FORM_data8:
4421 case DW_FORM_ref8:
4422 case DW_FORM_ref_sig8:
4423 info_ptr += 8;
4424 break;
4425 case DW_FORM_string:
4426 read_direct_string (abfd, info_ptr, &bytes_read);
4427 info_ptr += bytes_read;
4428 break;
4429 case DW_FORM_sec_offset:
4430 case DW_FORM_strp:
4431 info_ptr += cu->header.offset_size;
4432 break;
4433 case DW_FORM_exprloc:
4434 case DW_FORM_block:
4435 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4436 info_ptr += bytes_read;
4437 break;
4438 case DW_FORM_block1:
4439 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4440 break;
4441 case DW_FORM_block2:
4442 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4443 break;
4444 case DW_FORM_block4:
4445 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4446 break;
4447 case DW_FORM_sdata:
4448 case DW_FORM_udata:
4449 case DW_FORM_ref_udata:
4450 info_ptr = skip_leb128 (abfd, info_ptr);
4451 break;
4452 case DW_FORM_indirect:
4453 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4454 info_ptr += bytes_read;
4455 /* We need to continue parsing from here, so just go back to
4456 the top. */
4457 goto skip_attribute;
4458
4459 default:
4460 error (_("Dwarf Error: Cannot handle %s "
4461 "in DWARF reader [in module %s]"),
4462 dwarf_form_name (form),
4463 bfd_get_filename (abfd));
4464 }
4465 }
4466
4467 if (abbrev->has_children)
4468 return skip_children (buffer, info_ptr, cu);
4469 else
4470 return info_ptr;
4471 }
4472
4473 /* Locate ORIG_PDI's sibling.
4474 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4475 in BUFFER. */
4476
4477 static gdb_byte *
4478 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4479 gdb_byte *buffer, gdb_byte *info_ptr,
4480 bfd *abfd, struct dwarf2_cu *cu)
4481 {
4482 /* Do we know the sibling already? */
4483
4484 if (orig_pdi->sibling)
4485 return orig_pdi->sibling;
4486
4487 /* Are there any children to deal with? */
4488
4489 if (!orig_pdi->has_children)
4490 return info_ptr;
4491
4492 /* Skip the children the long way. */
4493
4494 return skip_children (buffer, info_ptr, cu);
4495 }
4496
4497 /* Expand this partial symbol table into a full symbol table. */
4498
4499 static void
4500 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4501 {
4502 if (pst != NULL)
4503 {
4504 if (pst->readin)
4505 {
4506 warning (_("bug: psymtab for %s is already read in."),
4507 pst->filename);
4508 }
4509 else
4510 {
4511 if (info_verbose)
4512 {
4513 printf_filtered (_("Reading in symbols for %s..."),
4514 pst->filename);
4515 gdb_flush (gdb_stdout);
4516 }
4517
4518 /* Restore our global data. */
4519 dwarf2_per_objfile = objfile_data (pst->objfile,
4520 dwarf2_objfile_data_key);
4521
4522 /* If this psymtab is constructed from a debug-only objfile, the
4523 has_section_at_zero flag will not necessarily be correct. We
4524 can get the correct value for this flag by looking at the data
4525 associated with the (presumably stripped) associated objfile. */
4526 if (pst->objfile->separate_debug_objfile_backlink)
4527 {
4528 struct dwarf2_per_objfile *dpo_backlink
4529 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4530 dwarf2_objfile_data_key);
4531
4532 dwarf2_per_objfile->has_section_at_zero
4533 = dpo_backlink->has_section_at_zero;
4534 }
4535
4536 dwarf2_per_objfile->reading_partial_symbols = 0;
4537
4538 psymtab_to_symtab_1 (pst);
4539
4540 /* Finish up the debug error message. */
4541 if (info_verbose)
4542 printf_filtered (_("done.\n"));
4543 }
4544 }
4545 }
4546 \f
4547 /* Reading in full CUs. */
4548
4549 /* Add PER_CU to the queue. */
4550
4551 static void
4552 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4553 {
4554 struct dwarf2_queue_item *item;
4555
4556 per_cu->queued = 1;
4557 item = xmalloc (sizeof (*item));
4558 item->per_cu = per_cu;
4559 item->next = NULL;
4560
4561 if (dwarf2_queue == NULL)
4562 dwarf2_queue = item;
4563 else
4564 dwarf2_queue_tail->next = item;
4565
4566 dwarf2_queue_tail = item;
4567 }
4568
4569 /* Process the queue. */
4570
4571 static void
4572 process_queue (void)
4573 {
4574 struct dwarf2_queue_item *item, *next_item;
4575
4576 /* The queue starts out with one item, but following a DIE reference
4577 may load a new CU, adding it to the end of the queue. */
4578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4579 {
4580 if (dwarf2_per_objfile->using_index
4581 ? !item->per_cu->v.quick->symtab
4582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4583 process_full_comp_unit (item->per_cu);
4584
4585 item->per_cu->queued = 0;
4586 next_item = item->next;
4587 xfree (item);
4588 }
4589
4590 dwarf2_queue_tail = NULL;
4591 }
4592
4593 /* Free all allocated queue entries. This function only releases anything if
4594 an error was thrown; if the queue was processed then it would have been
4595 freed as we went along. */
4596
4597 static void
4598 dwarf2_release_queue (void *dummy)
4599 {
4600 struct dwarf2_queue_item *item, *last;
4601
4602 item = dwarf2_queue;
4603 while (item)
4604 {
4605 /* Anything still marked queued is likely to be in an
4606 inconsistent state, so discard it. */
4607 if (item->per_cu->queued)
4608 {
4609 if (item->per_cu->cu != NULL)
4610 free_one_cached_comp_unit (item->per_cu->cu);
4611 item->per_cu->queued = 0;
4612 }
4613
4614 last = item;
4615 item = item->next;
4616 xfree (last);
4617 }
4618
4619 dwarf2_queue = dwarf2_queue_tail = NULL;
4620 }
4621
4622 /* Read in full symbols for PST, and anything it depends on. */
4623
4624 static void
4625 psymtab_to_symtab_1 (struct partial_symtab *pst)
4626 {
4627 struct dwarf2_per_cu_data *per_cu;
4628 struct cleanup *back_to;
4629 int i;
4630
4631 for (i = 0; i < pst->number_of_dependencies; i++)
4632 if (!pst->dependencies[i]->readin)
4633 {
4634 /* Inform about additional files that need to be read in. */
4635 if (info_verbose)
4636 {
4637 /* FIXME: i18n: Need to make this a single string. */
4638 fputs_filtered (" ", gdb_stdout);
4639 wrap_here ("");
4640 fputs_filtered ("and ", gdb_stdout);
4641 wrap_here ("");
4642 printf_filtered ("%s...", pst->dependencies[i]->filename);
4643 wrap_here (""); /* Flush output. */
4644 gdb_flush (gdb_stdout);
4645 }
4646 psymtab_to_symtab_1 (pst->dependencies[i]);
4647 }
4648
4649 per_cu = pst->read_symtab_private;
4650
4651 if (per_cu == NULL)
4652 {
4653 /* It's an include file, no symbols to read for it.
4654 Everything is in the parent symtab. */
4655 pst->readin = 1;
4656 return;
4657 }
4658
4659 dw2_do_instantiate_symtab (per_cu);
4660 }
4661
4662 /* Load the DIEs associated with PER_CU into memory. */
4663
4664 static void
4665 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4666 {
4667 struct objfile *objfile = per_cu->objfile;
4668 bfd *abfd = objfile->obfd;
4669 struct dwarf2_cu *cu;
4670 unsigned int offset;
4671 gdb_byte *info_ptr, *beg_of_comp_unit;
4672 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4673 struct attribute *attr;
4674 int read_cu = 0;
4675
4676 gdb_assert (! per_cu->debug_types_section);
4677
4678 /* Set local variables from the partial symbol table info. */
4679 offset = per_cu->offset;
4680
4681 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4682 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4683 beg_of_comp_unit = info_ptr;
4684
4685 if (per_cu->cu == NULL)
4686 {
4687 cu = xmalloc (sizeof (*cu));
4688 init_one_comp_unit (cu, per_cu);
4689
4690 read_cu = 1;
4691
4692 /* If an error occurs while loading, release our storage. */
4693 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4694
4695 /* Read in the comp_unit header. */
4696 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4697
4698 /* Skip dummy compilation units. */
4699 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4700 + dwarf2_per_objfile->info.size)
4701 || peek_abbrev_code (abfd, info_ptr) == 0)
4702 {
4703 do_cleanups (free_cu_cleanup);
4704 return;
4705 }
4706
4707 /* Complete the cu_header. */
4708 cu->header.offset = offset;
4709 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4710
4711 /* Read the abbrevs for this compilation unit. */
4712 dwarf2_read_abbrevs (cu);
4713 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4714
4715 /* Link this CU into read_in_chain. */
4716 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4717 dwarf2_per_objfile->read_in_chain = per_cu;
4718 }
4719 else
4720 {
4721 cu = per_cu->cu;
4722 info_ptr += cu->header.first_die_offset;
4723 }
4724
4725 cu->dies = read_comp_unit (info_ptr, cu);
4726
4727 /* We try not to read any attributes in this function, because not
4728 all CUs needed for references have been loaded yet, and symbol
4729 table processing isn't initialized. But we have to set the CU language,
4730 or we won't be able to build types correctly. */
4731 prepare_one_comp_unit (cu, cu->dies);
4732
4733 /* Similarly, if we do not read the producer, we can not apply
4734 producer-specific interpretation. */
4735 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4736 if (attr)
4737 cu->producer = DW_STRING (attr);
4738
4739 if (read_cu)
4740 {
4741 do_cleanups (free_abbrevs_cleanup);
4742
4743 /* We've successfully allocated this compilation unit. Let our
4744 caller clean it up when finished with it. */
4745 discard_cleanups (free_cu_cleanup);
4746 }
4747 }
4748
4749 /* Add a DIE to the delayed physname list. */
4750
4751 static void
4752 add_to_method_list (struct type *type, int fnfield_index, int index,
4753 const char *name, struct die_info *die,
4754 struct dwarf2_cu *cu)
4755 {
4756 struct delayed_method_info mi;
4757 mi.type = type;
4758 mi.fnfield_index = fnfield_index;
4759 mi.index = index;
4760 mi.name = name;
4761 mi.die = die;
4762 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4763 }
4764
4765 /* A cleanup for freeing the delayed method list. */
4766
4767 static void
4768 free_delayed_list (void *ptr)
4769 {
4770 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4771 if (cu->method_list != NULL)
4772 {
4773 VEC_free (delayed_method_info, cu->method_list);
4774 cu->method_list = NULL;
4775 }
4776 }
4777
4778 /* Compute the physnames of any methods on the CU's method list.
4779
4780 The computation of method physnames is delayed in order to avoid the
4781 (bad) condition that one of the method's formal parameters is of an as yet
4782 incomplete type. */
4783
4784 static void
4785 compute_delayed_physnames (struct dwarf2_cu *cu)
4786 {
4787 int i;
4788 struct delayed_method_info *mi;
4789 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4790 {
4791 const char *physname;
4792 struct fn_fieldlist *fn_flp
4793 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4794 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4795 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4796 }
4797 }
4798
4799 /* Generate full symbol information for PER_CU, whose DIEs have
4800 already been loaded into memory. */
4801
4802 static void
4803 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4804 {
4805 struct dwarf2_cu *cu = per_cu->cu;
4806 struct objfile *objfile = per_cu->objfile;
4807 CORE_ADDR lowpc, highpc;
4808 struct symtab *symtab;
4809 struct cleanup *back_to, *delayed_list_cleanup;
4810 CORE_ADDR baseaddr;
4811
4812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4813
4814 buildsym_init ();
4815 back_to = make_cleanup (really_free_pendings, NULL);
4816 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4817
4818 cu->list_in_scope = &file_symbols;
4819
4820 /* Do line number decoding in read_file_scope () */
4821 process_die (cu->dies, cu);
4822
4823 /* Now that we have processed all the DIEs in the CU, all the types
4824 should be complete, and it should now be safe to compute all of the
4825 physnames. */
4826 compute_delayed_physnames (cu);
4827 do_cleanups (delayed_list_cleanup);
4828
4829 /* Some compilers don't define a DW_AT_high_pc attribute for the
4830 compilation unit. If the DW_AT_high_pc is missing, synthesize
4831 it, by scanning the DIE's below the compilation unit. */
4832 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4833
4834 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4835
4836 if (symtab != NULL)
4837 {
4838 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4839
4840 /* Set symtab language to language from DW_AT_language. If the
4841 compilation is from a C file generated by language preprocessors, do
4842 not set the language if it was already deduced by start_subfile. */
4843 if (!(cu->language == language_c && symtab->language != language_c))
4844 symtab->language = cu->language;
4845
4846 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4847 produce DW_AT_location with location lists but it can be possibly
4848 invalid without -fvar-tracking.
4849
4850 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4851 needed, it would be wrong due to missing DW_AT_producer there.
4852
4853 Still one can confuse GDB by using non-standard GCC compilation
4854 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4855 */
4856 if (cu->has_loclist && gcc_4_minor >= 0)
4857 symtab->locations_valid = 1;
4858
4859 if (gcc_4_minor >= 5)
4860 symtab->epilogue_unwind_valid = 1;
4861
4862 symtab->call_site_htab = cu->call_site_htab;
4863 }
4864
4865 if (dwarf2_per_objfile->using_index)
4866 per_cu->v.quick->symtab = symtab;
4867 else
4868 {
4869 struct partial_symtab *pst = per_cu->v.psymtab;
4870 pst->symtab = symtab;
4871 pst->readin = 1;
4872 }
4873
4874 do_cleanups (back_to);
4875 }
4876
4877 /* Process a die and its children. */
4878
4879 static void
4880 process_die (struct die_info *die, struct dwarf2_cu *cu)
4881 {
4882 switch (die->tag)
4883 {
4884 case DW_TAG_padding:
4885 break;
4886 case DW_TAG_compile_unit:
4887 read_file_scope (die, cu);
4888 break;
4889 case DW_TAG_type_unit:
4890 read_type_unit_scope (die, cu);
4891 break;
4892 case DW_TAG_subprogram:
4893 case DW_TAG_inlined_subroutine:
4894 read_func_scope (die, cu);
4895 break;
4896 case DW_TAG_lexical_block:
4897 case DW_TAG_try_block:
4898 case DW_TAG_catch_block:
4899 read_lexical_block_scope (die, cu);
4900 break;
4901 case DW_TAG_GNU_call_site:
4902 read_call_site_scope (die, cu);
4903 break;
4904 case DW_TAG_class_type:
4905 case DW_TAG_interface_type:
4906 case DW_TAG_structure_type:
4907 case DW_TAG_union_type:
4908 process_structure_scope (die, cu);
4909 break;
4910 case DW_TAG_enumeration_type:
4911 process_enumeration_scope (die, cu);
4912 break;
4913
4914 /* These dies have a type, but processing them does not create
4915 a symbol or recurse to process the children. Therefore we can
4916 read them on-demand through read_type_die. */
4917 case DW_TAG_subroutine_type:
4918 case DW_TAG_set_type:
4919 case DW_TAG_array_type:
4920 case DW_TAG_pointer_type:
4921 case DW_TAG_ptr_to_member_type:
4922 case DW_TAG_reference_type:
4923 case DW_TAG_string_type:
4924 break;
4925
4926 case DW_TAG_base_type:
4927 case DW_TAG_subrange_type:
4928 case DW_TAG_typedef:
4929 /* Add a typedef symbol for the type definition, if it has a
4930 DW_AT_name. */
4931 new_symbol (die, read_type_die (die, cu), cu);
4932 break;
4933 case DW_TAG_common_block:
4934 read_common_block (die, cu);
4935 break;
4936 case DW_TAG_common_inclusion:
4937 break;
4938 case DW_TAG_namespace:
4939 processing_has_namespace_info = 1;
4940 read_namespace (die, cu);
4941 break;
4942 case DW_TAG_module:
4943 processing_has_namespace_info = 1;
4944 read_module (die, cu);
4945 break;
4946 case DW_TAG_imported_declaration:
4947 case DW_TAG_imported_module:
4948 processing_has_namespace_info = 1;
4949 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4950 || cu->language != language_fortran))
4951 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4952 dwarf_tag_name (die->tag));
4953 read_import_statement (die, cu);
4954 break;
4955 default:
4956 new_symbol (die, NULL, cu);
4957 break;
4958 }
4959 }
4960
4961 /* A helper function for dwarf2_compute_name which determines whether DIE
4962 needs to have the name of the scope prepended to the name listed in the
4963 die. */
4964
4965 static int
4966 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4967 {
4968 struct attribute *attr;
4969
4970 switch (die->tag)
4971 {
4972 case DW_TAG_namespace:
4973 case DW_TAG_typedef:
4974 case DW_TAG_class_type:
4975 case DW_TAG_interface_type:
4976 case DW_TAG_structure_type:
4977 case DW_TAG_union_type:
4978 case DW_TAG_enumeration_type:
4979 case DW_TAG_enumerator:
4980 case DW_TAG_subprogram:
4981 case DW_TAG_member:
4982 return 1;
4983
4984 case DW_TAG_variable:
4985 case DW_TAG_constant:
4986 /* We only need to prefix "globally" visible variables. These include
4987 any variable marked with DW_AT_external or any variable that
4988 lives in a namespace. [Variables in anonymous namespaces
4989 require prefixing, but they are not DW_AT_external.] */
4990
4991 if (dwarf2_attr (die, DW_AT_specification, cu))
4992 {
4993 struct dwarf2_cu *spec_cu = cu;
4994
4995 return die_needs_namespace (die_specification (die, &spec_cu),
4996 spec_cu);
4997 }
4998
4999 attr = dwarf2_attr (die, DW_AT_external, cu);
5000 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5001 && die->parent->tag != DW_TAG_module)
5002 return 0;
5003 /* A variable in a lexical block of some kind does not need a
5004 namespace, even though in C++ such variables may be external
5005 and have a mangled name. */
5006 if (die->parent->tag == DW_TAG_lexical_block
5007 || die->parent->tag == DW_TAG_try_block
5008 || die->parent->tag == DW_TAG_catch_block
5009 || die->parent->tag == DW_TAG_subprogram)
5010 return 0;
5011 return 1;
5012
5013 default:
5014 return 0;
5015 }
5016 }
5017
5018 /* Retrieve the last character from a mem_file. */
5019
5020 static void
5021 do_ui_file_peek_last (void *object, const char *buffer, long length)
5022 {
5023 char *last_char_p = (char *) object;
5024
5025 if (length > 0)
5026 *last_char_p = buffer[length - 1];
5027 }
5028
5029 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5030 compute the physname for the object, which include a method's
5031 formal parameters (C++/Java) and return type (Java).
5032
5033 For Ada, return the DIE's linkage name rather than the fully qualified
5034 name. PHYSNAME is ignored..
5035
5036 The result is allocated on the objfile_obstack and canonicalized. */
5037
5038 static const char *
5039 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5040 int physname)
5041 {
5042 struct objfile *objfile = cu->objfile;
5043
5044 if (name == NULL)
5045 name = dwarf2_name (die, cu);
5046
5047 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5048 compute it by typename_concat inside GDB. */
5049 if (cu->language == language_ada
5050 || (cu->language == language_fortran && physname))
5051 {
5052 /* For Ada unit, we prefer the linkage name over the name, as
5053 the former contains the exported name, which the user expects
5054 to be able to reference. Ideally, we want the user to be able
5055 to reference this entity using either natural or linkage name,
5056 but we haven't started looking at this enhancement yet. */
5057 struct attribute *attr;
5058
5059 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5060 if (attr == NULL)
5061 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5062 if (attr && DW_STRING (attr))
5063 return DW_STRING (attr);
5064 }
5065
5066 /* These are the only languages we know how to qualify names in. */
5067 if (name != NULL
5068 && (cu->language == language_cplus || cu->language == language_java
5069 || cu->language == language_fortran))
5070 {
5071 if (die_needs_namespace (die, cu))
5072 {
5073 long length;
5074 char *prefix;
5075 struct ui_file *buf;
5076
5077 prefix = determine_prefix (die, cu);
5078 buf = mem_fileopen ();
5079 if (*prefix != '\0')
5080 {
5081 char *prefixed_name = typename_concat (NULL, prefix, name,
5082 physname, cu);
5083
5084 fputs_unfiltered (prefixed_name, buf);
5085 xfree (prefixed_name);
5086 }
5087 else
5088 fputs_unfiltered (name, buf);
5089
5090 /* Template parameters may be specified in the DIE's DW_AT_name, or
5091 as children with DW_TAG_template_type_param or
5092 DW_TAG_value_type_param. If the latter, add them to the name
5093 here. If the name already has template parameters, then
5094 skip this step; some versions of GCC emit both, and
5095 it is more efficient to use the pre-computed name.
5096
5097 Something to keep in mind about this process: it is very
5098 unlikely, or in some cases downright impossible, to produce
5099 something that will match the mangled name of a function.
5100 If the definition of the function has the same debug info,
5101 we should be able to match up with it anyway. But fallbacks
5102 using the minimal symbol, for instance to find a method
5103 implemented in a stripped copy of libstdc++, will not work.
5104 If we do not have debug info for the definition, we will have to
5105 match them up some other way.
5106
5107 When we do name matching there is a related problem with function
5108 templates; two instantiated function templates are allowed to
5109 differ only by their return types, which we do not add here. */
5110
5111 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5112 {
5113 struct attribute *attr;
5114 struct die_info *child;
5115 int first = 1;
5116
5117 die->building_fullname = 1;
5118
5119 for (child = die->child; child != NULL; child = child->sibling)
5120 {
5121 struct type *type;
5122 long value;
5123 gdb_byte *bytes;
5124 struct dwarf2_locexpr_baton *baton;
5125 struct value *v;
5126
5127 if (child->tag != DW_TAG_template_type_param
5128 && child->tag != DW_TAG_template_value_param)
5129 continue;
5130
5131 if (first)
5132 {
5133 fputs_unfiltered ("<", buf);
5134 first = 0;
5135 }
5136 else
5137 fputs_unfiltered (", ", buf);
5138
5139 attr = dwarf2_attr (child, DW_AT_type, cu);
5140 if (attr == NULL)
5141 {
5142 complaint (&symfile_complaints,
5143 _("template parameter missing DW_AT_type"));
5144 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5145 continue;
5146 }
5147 type = die_type (child, cu);
5148
5149 if (child->tag == DW_TAG_template_type_param)
5150 {
5151 c_print_type (type, "", buf, -1, 0);
5152 continue;
5153 }
5154
5155 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5156 if (attr == NULL)
5157 {
5158 complaint (&symfile_complaints,
5159 _("template parameter missing "
5160 "DW_AT_const_value"));
5161 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5162 continue;
5163 }
5164
5165 dwarf2_const_value_attr (attr, type, name,
5166 &cu->comp_unit_obstack, cu,
5167 &value, &bytes, &baton);
5168
5169 if (TYPE_NOSIGN (type))
5170 /* GDB prints characters as NUMBER 'CHAR'. If that's
5171 changed, this can use value_print instead. */
5172 c_printchar (value, type, buf);
5173 else
5174 {
5175 struct value_print_options opts;
5176
5177 if (baton != NULL)
5178 v = dwarf2_evaluate_loc_desc (type, NULL,
5179 baton->data,
5180 baton->size,
5181 baton->per_cu);
5182 else if (bytes != NULL)
5183 {
5184 v = allocate_value (type);
5185 memcpy (value_contents_writeable (v), bytes,
5186 TYPE_LENGTH (type));
5187 }
5188 else
5189 v = value_from_longest (type, value);
5190
5191 /* Specify decimal so that we do not depend on
5192 the radix. */
5193 get_formatted_print_options (&opts, 'd');
5194 opts.raw = 1;
5195 value_print (v, buf, &opts);
5196 release_value (v);
5197 value_free (v);
5198 }
5199 }
5200
5201 die->building_fullname = 0;
5202
5203 if (!first)
5204 {
5205 /* Close the argument list, with a space if necessary
5206 (nested templates). */
5207 char last_char = '\0';
5208 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5209 if (last_char == '>')
5210 fputs_unfiltered (" >", buf);
5211 else
5212 fputs_unfiltered (">", buf);
5213 }
5214 }
5215
5216 /* For Java and C++ methods, append formal parameter type
5217 information, if PHYSNAME. */
5218
5219 if (physname && die->tag == DW_TAG_subprogram
5220 && (cu->language == language_cplus
5221 || cu->language == language_java))
5222 {
5223 struct type *type = read_type_die (die, cu);
5224
5225 c_type_print_args (type, buf, 1, cu->language);
5226
5227 if (cu->language == language_java)
5228 {
5229 /* For java, we must append the return type to method
5230 names. */
5231 if (die->tag == DW_TAG_subprogram)
5232 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5233 0, 0);
5234 }
5235 else if (cu->language == language_cplus)
5236 {
5237 /* Assume that an artificial first parameter is
5238 "this", but do not crash if it is not. RealView
5239 marks unnamed (and thus unused) parameters as
5240 artificial; there is no way to differentiate
5241 the two cases. */
5242 if (TYPE_NFIELDS (type) > 0
5243 && TYPE_FIELD_ARTIFICIAL (type, 0)
5244 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5245 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5246 0))))
5247 fputs_unfiltered (" const", buf);
5248 }
5249 }
5250
5251 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5252 &length);
5253 ui_file_delete (buf);
5254
5255 if (cu->language == language_cplus)
5256 {
5257 char *cname
5258 = dwarf2_canonicalize_name (name, cu,
5259 &objfile->objfile_obstack);
5260
5261 if (cname != NULL)
5262 name = cname;
5263 }
5264 }
5265 }
5266
5267 return name;
5268 }
5269
5270 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5271 If scope qualifiers are appropriate they will be added. The result
5272 will be allocated on the objfile_obstack, or NULL if the DIE does
5273 not have a name. NAME may either be from a previous call to
5274 dwarf2_name or NULL.
5275
5276 The output string will be canonicalized (if C++/Java). */
5277
5278 static const char *
5279 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5280 {
5281 return dwarf2_compute_name (name, die, cu, 0);
5282 }
5283
5284 /* Construct a physname for the given DIE in CU. NAME may either be
5285 from a previous call to dwarf2_name or NULL. The result will be
5286 allocated on the objfile_objstack or NULL if the DIE does not have a
5287 name.
5288
5289 The output string will be canonicalized (if C++/Java). */
5290
5291 static const char *
5292 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5293 {
5294 struct objfile *objfile = cu->objfile;
5295 struct attribute *attr;
5296 const char *retval, *mangled = NULL, *canon = NULL;
5297 struct cleanup *back_to;
5298 int need_copy = 1;
5299
5300 /* In this case dwarf2_compute_name is just a shortcut not building anything
5301 on its own. */
5302 if (!die_needs_namespace (die, cu))
5303 return dwarf2_compute_name (name, die, cu, 1);
5304
5305 back_to = make_cleanup (null_cleanup, NULL);
5306
5307 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5308 if (!attr)
5309 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5310
5311 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5312 has computed. */
5313 if (attr && DW_STRING (attr))
5314 {
5315 char *demangled;
5316
5317 mangled = DW_STRING (attr);
5318
5319 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5320 type. It is easier for GDB users to search for such functions as
5321 `name(params)' than `long name(params)'. In such case the minimal
5322 symbol names do not match the full symbol names but for template
5323 functions there is never a need to look up their definition from their
5324 declaration so the only disadvantage remains the minimal symbol
5325 variant `long name(params)' does not have the proper inferior type.
5326 */
5327
5328 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5329 | (cu->language == language_java
5330 ? DMGL_JAVA | DMGL_RET_POSTFIX
5331 : DMGL_RET_DROP)));
5332 if (demangled)
5333 {
5334 make_cleanup (xfree, demangled);
5335 canon = demangled;
5336 }
5337 else
5338 {
5339 canon = mangled;
5340 need_copy = 0;
5341 }
5342 }
5343
5344 if (canon == NULL || check_physname)
5345 {
5346 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5347
5348 if (canon != NULL && strcmp (physname, canon) != 0)
5349 {
5350 /* It may not mean a bug in GDB. The compiler could also
5351 compute DW_AT_linkage_name incorrectly. But in such case
5352 GDB would need to be bug-to-bug compatible. */
5353
5354 complaint (&symfile_complaints,
5355 _("Computed physname <%s> does not match demangled <%s> "
5356 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5357 physname, canon, mangled, die->offset, objfile->name);
5358
5359 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5360 is available here - over computed PHYSNAME. It is safer
5361 against both buggy GDB and buggy compilers. */
5362
5363 retval = canon;
5364 }
5365 else
5366 {
5367 retval = physname;
5368 need_copy = 0;
5369 }
5370 }
5371 else
5372 retval = canon;
5373
5374 if (need_copy)
5375 retval = obsavestring (retval, strlen (retval),
5376 &objfile->objfile_obstack);
5377
5378 do_cleanups (back_to);
5379 return retval;
5380 }
5381
5382 /* Read the import statement specified by the given die and record it. */
5383
5384 static void
5385 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5386 {
5387 struct objfile *objfile = cu->objfile;
5388 struct attribute *import_attr;
5389 struct die_info *imported_die, *child_die;
5390 struct dwarf2_cu *imported_cu;
5391 const char *imported_name;
5392 const char *imported_name_prefix;
5393 const char *canonical_name;
5394 const char *import_alias;
5395 const char *imported_declaration = NULL;
5396 const char *import_prefix;
5397 VEC (const_char_ptr) *excludes = NULL;
5398 struct cleanup *cleanups;
5399
5400 char *temp;
5401
5402 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5403 if (import_attr == NULL)
5404 {
5405 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5406 dwarf_tag_name (die->tag));
5407 return;
5408 }
5409
5410 imported_cu = cu;
5411 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5412 imported_name = dwarf2_name (imported_die, imported_cu);
5413 if (imported_name == NULL)
5414 {
5415 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5416
5417 The import in the following code:
5418 namespace A
5419 {
5420 typedef int B;
5421 }
5422
5423 int main ()
5424 {
5425 using A::B;
5426 B b;
5427 return b;
5428 }
5429
5430 ...
5431 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5432 <52> DW_AT_decl_file : 1
5433 <53> DW_AT_decl_line : 6
5434 <54> DW_AT_import : <0x75>
5435 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5436 <59> DW_AT_name : B
5437 <5b> DW_AT_decl_file : 1
5438 <5c> DW_AT_decl_line : 2
5439 <5d> DW_AT_type : <0x6e>
5440 ...
5441 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5442 <76> DW_AT_byte_size : 4
5443 <77> DW_AT_encoding : 5 (signed)
5444
5445 imports the wrong die ( 0x75 instead of 0x58 ).
5446 This case will be ignored until the gcc bug is fixed. */
5447 return;
5448 }
5449
5450 /* Figure out the local name after import. */
5451 import_alias = dwarf2_name (die, cu);
5452
5453 /* Figure out where the statement is being imported to. */
5454 import_prefix = determine_prefix (die, cu);
5455
5456 /* Figure out what the scope of the imported die is and prepend it
5457 to the name of the imported die. */
5458 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5459
5460 if (imported_die->tag != DW_TAG_namespace
5461 && imported_die->tag != DW_TAG_module)
5462 {
5463 imported_declaration = imported_name;
5464 canonical_name = imported_name_prefix;
5465 }
5466 else if (strlen (imported_name_prefix) > 0)
5467 {
5468 temp = alloca (strlen (imported_name_prefix)
5469 + 2 + strlen (imported_name) + 1);
5470 strcpy (temp, imported_name_prefix);
5471 strcat (temp, "::");
5472 strcat (temp, imported_name);
5473 canonical_name = temp;
5474 }
5475 else
5476 canonical_name = imported_name;
5477
5478 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5479
5480 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5481 for (child_die = die->child; child_die && child_die->tag;
5482 child_die = sibling_die (child_die))
5483 {
5484 /* DWARF-4: A Fortran use statement with a “rename list” may be
5485 represented by an imported module entry with an import attribute
5486 referring to the module and owned entries corresponding to those
5487 entities that are renamed as part of being imported. */
5488
5489 if (child_die->tag != DW_TAG_imported_declaration)
5490 {
5491 complaint (&symfile_complaints,
5492 _("child DW_TAG_imported_declaration expected "
5493 "- DIE at 0x%x [in module %s]"),
5494 child_die->offset, objfile->name);
5495 continue;
5496 }
5497
5498 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5499 if (import_attr == NULL)
5500 {
5501 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5502 dwarf_tag_name (child_die->tag));
5503 continue;
5504 }
5505
5506 imported_cu = cu;
5507 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5508 &imported_cu);
5509 imported_name = dwarf2_name (imported_die, imported_cu);
5510 if (imported_name == NULL)
5511 {
5512 complaint (&symfile_complaints,
5513 _("child DW_TAG_imported_declaration has unknown "
5514 "imported name - DIE at 0x%x [in module %s]"),
5515 child_die->offset, objfile->name);
5516 continue;
5517 }
5518
5519 VEC_safe_push (const_char_ptr, excludes, imported_name);
5520
5521 process_die (child_die, cu);
5522 }
5523
5524 cp_add_using_directive (import_prefix,
5525 canonical_name,
5526 import_alias,
5527 imported_declaration,
5528 excludes,
5529 &objfile->objfile_obstack);
5530
5531 do_cleanups (cleanups);
5532 }
5533
5534 /* Cleanup function for read_file_scope. */
5535
5536 static void
5537 free_cu_line_header (void *arg)
5538 {
5539 struct dwarf2_cu *cu = arg;
5540
5541 free_line_header (cu->line_header);
5542 cu->line_header = NULL;
5543 }
5544
5545 static void
5546 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5547 char **name, char **comp_dir)
5548 {
5549 struct attribute *attr;
5550
5551 *name = NULL;
5552 *comp_dir = NULL;
5553
5554 /* Find the filename. Do not use dwarf2_name here, since the filename
5555 is not a source language identifier. */
5556 attr = dwarf2_attr (die, DW_AT_name, cu);
5557 if (attr)
5558 {
5559 *name = DW_STRING (attr);
5560 }
5561
5562 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5563 if (attr)
5564 *comp_dir = DW_STRING (attr);
5565 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5566 {
5567 *comp_dir = ldirname (*name);
5568 if (*comp_dir != NULL)
5569 make_cleanup (xfree, *comp_dir);
5570 }
5571 if (*comp_dir != NULL)
5572 {
5573 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5574 directory, get rid of it. */
5575 char *cp = strchr (*comp_dir, ':');
5576
5577 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5578 *comp_dir = cp + 1;
5579 }
5580
5581 if (*name == NULL)
5582 *name = "<unknown>";
5583 }
5584
5585 /* Handle DW_AT_stmt_list for a compilation unit. */
5586
5587 static void
5588 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5589 const char *comp_dir)
5590 {
5591 struct attribute *attr;
5592 struct objfile *objfile = cu->objfile;
5593 bfd *abfd = objfile->obfd;
5594
5595 /* Decode line number information if present. We do this before
5596 processing child DIEs, so that the line header table is available
5597 for DW_AT_decl_file. */
5598 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5599 if (attr)
5600 {
5601 unsigned int line_offset = DW_UNSND (attr);
5602 struct line_header *line_header
5603 = dwarf_decode_line_header (line_offset, abfd, cu);
5604
5605 if (line_header)
5606 {
5607 cu->line_header = line_header;
5608 make_cleanup (free_cu_line_header, cu);
5609 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5610 }
5611 }
5612 }
5613
5614 /* Process DW_TAG_compile_unit. */
5615
5616 static void
5617 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5618 {
5619 struct objfile *objfile = cu->objfile;
5620 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5621 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5622 CORE_ADDR highpc = ((CORE_ADDR) 0);
5623 struct attribute *attr;
5624 char *name = NULL;
5625 char *comp_dir = NULL;
5626 struct die_info *child_die;
5627 bfd *abfd = objfile->obfd;
5628 CORE_ADDR baseaddr;
5629
5630 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5631
5632 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5633
5634 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5635 from finish_block. */
5636 if (lowpc == ((CORE_ADDR) -1))
5637 lowpc = highpc;
5638 lowpc += baseaddr;
5639 highpc += baseaddr;
5640
5641 find_file_and_directory (die, cu, &name, &comp_dir);
5642
5643 attr = dwarf2_attr (die, DW_AT_language, cu);
5644 if (attr)
5645 {
5646 set_cu_language (DW_UNSND (attr), cu);
5647 }
5648
5649 attr = dwarf2_attr (die, DW_AT_producer, cu);
5650 if (attr)
5651 cu->producer = DW_STRING (attr);
5652
5653 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5654 standardised yet. As a workaround for the language detection we fall
5655 back to the DW_AT_producer string. */
5656 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5657 cu->language = language_opencl;
5658
5659 /* We assume that we're processing GCC output. */
5660 processing_gcc_compilation = 2;
5661
5662 processing_has_namespace_info = 0;
5663
5664 start_symtab (name, comp_dir, lowpc);
5665 record_debugformat ("DWARF 2");
5666 record_producer (cu->producer);
5667
5668 handle_DW_AT_stmt_list (die, cu, comp_dir);
5669
5670 /* Process all dies in compilation unit. */
5671 if (die->child != NULL)
5672 {
5673 child_die = die->child;
5674 while (child_die && child_die->tag)
5675 {
5676 process_die (child_die, cu);
5677 child_die = sibling_die (child_die);
5678 }
5679 }
5680
5681 /* Decode macro information, if present. Dwarf 2 macro information
5682 refers to information in the line number info statement program
5683 header, so we can only read it if we've read the header
5684 successfully. */
5685 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5686 if (attr && cu->line_header)
5687 {
5688 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5689 complaint (&symfile_complaints,
5690 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5691
5692 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5693 comp_dir, abfd, cu,
5694 &dwarf2_per_objfile->macro, 1);
5695 }
5696 else
5697 {
5698 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5699 if (attr && cu->line_header)
5700 {
5701 unsigned int macro_offset = DW_UNSND (attr);
5702
5703 dwarf_decode_macros (cu->line_header, macro_offset,
5704 comp_dir, abfd, cu,
5705 &dwarf2_per_objfile->macinfo, 0);
5706 }
5707 }
5708
5709 do_cleanups (back_to);
5710 }
5711
5712 /* Process DW_TAG_type_unit.
5713 For TUs we want to skip the first top level sibling if it's not the
5714 actual type being defined by this TU. In this case the first top
5715 level sibling is there to provide context only. */
5716
5717 static void
5718 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5719 {
5720 struct objfile *objfile = cu->objfile;
5721 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5722 CORE_ADDR lowpc;
5723 struct attribute *attr;
5724 char *name = NULL;
5725 char *comp_dir = NULL;
5726 struct die_info *child_die;
5727 bfd *abfd = objfile->obfd;
5728
5729 /* start_symtab needs a low pc, but we don't really have one.
5730 Do what read_file_scope would do in the absence of such info. */
5731 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5732
5733 /* Find the filename. Do not use dwarf2_name here, since the filename
5734 is not a source language identifier. */
5735 attr = dwarf2_attr (die, DW_AT_name, cu);
5736 if (attr)
5737 name = DW_STRING (attr);
5738
5739 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5740 if (attr)
5741 comp_dir = DW_STRING (attr);
5742 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5743 {
5744 comp_dir = ldirname (name);
5745 if (comp_dir != NULL)
5746 make_cleanup (xfree, comp_dir);
5747 }
5748
5749 if (name == NULL)
5750 name = "<unknown>";
5751
5752 attr = dwarf2_attr (die, DW_AT_language, cu);
5753 if (attr)
5754 set_cu_language (DW_UNSND (attr), cu);
5755
5756 /* This isn't technically needed today. It is done for symmetry
5757 with read_file_scope. */
5758 attr = dwarf2_attr (die, DW_AT_producer, cu);
5759 if (attr)
5760 cu->producer = DW_STRING (attr);
5761
5762 /* We assume that we're processing GCC output. */
5763 processing_gcc_compilation = 2;
5764
5765 processing_has_namespace_info = 0;
5766
5767 start_symtab (name, comp_dir, lowpc);
5768 record_debugformat ("DWARF 2");
5769 record_producer (cu->producer);
5770
5771 handle_DW_AT_stmt_list (die, cu, comp_dir);
5772
5773 /* Process the dies in the type unit. */
5774 if (die->child == NULL)
5775 {
5776 dump_die_for_error (die);
5777 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5778 bfd_get_filename (abfd));
5779 }
5780
5781 child_die = die->child;
5782
5783 while (child_die && child_die->tag)
5784 {
5785 process_die (child_die, cu);
5786
5787 child_die = sibling_die (child_die);
5788 }
5789
5790 do_cleanups (back_to);
5791 }
5792
5793 /* qsort helper for inherit_abstract_dies. */
5794
5795 static int
5796 unsigned_int_compar (const void *ap, const void *bp)
5797 {
5798 unsigned int a = *(unsigned int *) ap;
5799 unsigned int b = *(unsigned int *) bp;
5800
5801 return (a > b) - (b > a);
5802 }
5803
5804 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5805 Inherit only the children of the DW_AT_abstract_origin DIE not being
5806 already referenced by DW_AT_abstract_origin from the children of the
5807 current DIE. */
5808
5809 static void
5810 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5811 {
5812 struct die_info *child_die;
5813 unsigned die_children_count;
5814 /* CU offsets which were referenced by children of the current DIE. */
5815 unsigned *offsets;
5816 unsigned *offsets_end, *offsetp;
5817 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5818 struct die_info *origin_die;
5819 /* Iterator of the ORIGIN_DIE children. */
5820 struct die_info *origin_child_die;
5821 struct cleanup *cleanups;
5822 struct attribute *attr;
5823 struct dwarf2_cu *origin_cu;
5824 struct pending **origin_previous_list_in_scope;
5825
5826 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5827 if (!attr)
5828 return;
5829
5830 /* Note that following die references may follow to a die in a
5831 different cu. */
5832
5833 origin_cu = cu;
5834 origin_die = follow_die_ref (die, attr, &origin_cu);
5835
5836 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5837 symbols in. */
5838 origin_previous_list_in_scope = origin_cu->list_in_scope;
5839 origin_cu->list_in_scope = cu->list_in_scope;
5840
5841 if (die->tag != origin_die->tag
5842 && !(die->tag == DW_TAG_inlined_subroutine
5843 && origin_die->tag == DW_TAG_subprogram))
5844 complaint (&symfile_complaints,
5845 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5846 die->offset, origin_die->offset);
5847
5848 child_die = die->child;
5849 die_children_count = 0;
5850 while (child_die && child_die->tag)
5851 {
5852 child_die = sibling_die (child_die);
5853 die_children_count++;
5854 }
5855 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5856 cleanups = make_cleanup (xfree, offsets);
5857
5858 offsets_end = offsets;
5859 child_die = die->child;
5860 while (child_die && child_die->tag)
5861 {
5862 /* For each CHILD_DIE, find the corresponding child of
5863 ORIGIN_DIE. If there is more than one layer of
5864 DW_AT_abstract_origin, follow them all; there shouldn't be,
5865 but GCC versions at least through 4.4 generate this (GCC PR
5866 40573). */
5867 struct die_info *child_origin_die = child_die;
5868 struct dwarf2_cu *child_origin_cu = cu;
5869
5870 while (1)
5871 {
5872 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5873 child_origin_cu);
5874 if (attr == NULL)
5875 break;
5876 child_origin_die = follow_die_ref (child_origin_die, attr,
5877 &child_origin_cu);
5878 }
5879
5880 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5881 counterpart may exist. */
5882 if (child_origin_die != child_die)
5883 {
5884 if (child_die->tag != child_origin_die->tag
5885 && !(child_die->tag == DW_TAG_inlined_subroutine
5886 && child_origin_die->tag == DW_TAG_subprogram))
5887 complaint (&symfile_complaints,
5888 _("Child DIE 0x%x and its abstract origin 0x%x have "
5889 "different tags"), child_die->offset,
5890 child_origin_die->offset);
5891 if (child_origin_die->parent != origin_die)
5892 complaint (&symfile_complaints,
5893 _("Child DIE 0x%x and its abstract origin 0x%x have "
5894 "different parents"), child_die->offset,
5895 child_origin_die->offset);
5896 else
5897 *offsets_end++ = child_origin_die->offset;
5898 }
5899 child_die = sibling_die (child_die);
5900 }
5901 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5902 unsigned_int_compar);
5903 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5904 if (offsetp[-1] == *offsetp)
5905 complaint (&symfile_complaints,
5906 _("Multiple children of DIE 0x%x refer "
5907 "to DIE 0x%x as their abstract origin"),
5908 die->offset, *offsetp);
5909
5910 offsetp = offsets;
5911 origin_child_die = origin_die->child;
5912 while (origin_child_die && origin_child_die->tag)
5913 {
5914 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5915 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5916 offsetp++;
5917 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5918 {
5919 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5920 process_die (origin_child_die, origin_cu);
5921 }
5922 origin_child_die = sibling_die (origin_child_die);
5923 }
5924 origin_cu->list_in_scope = origin_previous_list_in_scope;
5925
5926 do_cleanups (cleanups);
5927 }
5928
5929 static void
5930 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5931 {
5932 struct objfile *objfile = cu->objfile;
5933 struct context_stack *new;
5934 CORE_ADDR lowpc;
5935 CORE_ADDR highpc;
5936 struct die_info *child_die;
5937 struct attribute *attr, *call_line, *call_file;
5938 char *name;
5939 CORE_ADDR baseaddr;
5940 struct block *block;
5941 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5942 VEC (symbolp) *template_args = NULL;
5943 struct template_symbol *templ_func = NULL;
5944
5945 if (inlined_func)
5946 {
5947 /* If we do not have call site information, we can't show the
5948 caller of this inlined function. That's too confusing, so
5949 only use the scope for local variables. */
5950 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5951 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5952 if (call_line == NULL || call_file == NULL)
5953 {
5954 read_lexical_block_scope (die, cu);
5955 return;
5956 }
5957 }
5958
5959 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5960
5961 name = dwarf2_name (die, cu);
5962
5963 /* Ignore functions with missing or empty names. These are actually
5964 illegal according to the DWARF standard. */
5965 if (name == NULL)
5966 {
5967 complaint (&symfile_complaints,
5968 _("missing name for subprogram DIE at %d"), die->offset);
5969 return;
5970 }
5971
5972 /* Ignore functions with missing or invalid low and high pc attributes. */
5973 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5974 {
5975 attr = dwarf2_attr (die, DW_AT_external, cu);
5976 if (!attr || !DW_UNSND (attr))
5977 complaint (&symfile_complaints,
5978 _("cannot get low and high bounds "
5979 "for subprogram DIE at %d"),
5980 die->offset);
5981 return;
5982 }
5983
5984 lowpc += baseaddr;
5985 highpc += baseaddr;
5986
5987 /* If we have any template arguments, then we must allocate a
5988 different sort of symbol. */
5989 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5990 {
5991 if (child_die->tag == DW_TAG_template_type_param
5992 || child_die->tag == DW_TAG_template_value_param)
5993 {
5994 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5995 struct template_symbol);
5996 templ_func->base.is_cplus_template_function = 1;
5997 break;
5998 }
5999 }
6000
6001 new = push_context (0, lowpc);
6002 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6003 (struct symbol *) templ_func);
6004
6005 /* If there is a location expression for DW_AT_frame_base, record
6006 it. */
6007 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6008 if (attr)
6009 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6010 expression is being recorded directly in the function's symbol
6011 and not in a separate frame-base object. I guess this hack is
6012 to avoid adding some sort of frame-base adjunct/annex to the
6013 function's symbol :-(. The problem with doing this is that it
6014 results in a function symbol with a location expression that
6015 has nothing to do with the location of the function, ouch! The
6016 relationship should be: a function's symbol has-a frame base; a
6017 frame-base has-a location expression. */
6018 dwarf2_symbol_mark_computed (attr, new->name, cu);
6019
6020 cu->list_in_scope = &local_symbols;
6021
6022 if (die->child != NULL)
6023 {
6024 child_die = die->child;
6025 while (child_die && child_die->tag)
6026 {
6027 if (child_die->tag == DW_TAG_template_type_param
6028 || child_die->tag == DW_TAG_template_value_param)
6029 {
6030 struct symbol *arg = new_symbol (child_die, NULL, cu);
6031
6032 if (arg != NULL)
6033 VEC_safe_push (symbolp, template_args, arg);
6034 }
6035 else
6036 process_die (child_die, cu);
6037 child_die = sibling_die (child_die);
6038 }
6039 }
6040
6041 inherit_abstract_dies (die, cu);
6042
6043 /* If we have a DW_AT_specification, we might need to import using
6044 directives from the context of the specification DIE. See the
6045 comment in determine_prefix. */
6046 if (cu->language == language_cplus
6047 && dwarf2_attr (die, DW_AT_specification, cu))
6048 {
6049 struct dwarf2_cu *spec_cu = cu;
6050 struct die_info *spec_die = die_specification (die, &spec_cu);
6051
6052 while (spec_die)
6053 {
6054 child_die = spec_die->child;
6055 while (child_die && child_die->tag)
6056 {
6057 if (child_die->tag == DW_TAG_imported_module)
6058 process_die (child_die, spec_cu);
6059 child_die = sibling_die (child_die);
6060 }
6061
6062 /* In some cases, GCC generates specification DIEs that
6063 themselves contain DW_AT_specification attributes. */
6064 spec_die = die_specification (spec_die, &spec_cu);
6065 }
6066 }
6067
6068 new = pop_context ();
6069 /* Make a block for the local symbols within. */
6070 block = finish_block (new->name, &local_symbols, new->old_blocks,
6071 lowpc, highpc, objfile);
6072
6073 /* For C++, set the block's scope. */
6074 if (cu->language == language_cplus || cu->language == language_fortran)
6075 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6076 determine_prefix (die, cu),
6077 processing_has_namespace_info);
6078
6079 /* If we have address ranges, record them. */
6080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6081
6082 /* Attach template arguments to function. */
6083 if (! VEC_empty (symbolp, template_args))
6084 {
6085 gdb_assert (templ_func != NULL);
6086
6087 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6088 templ_func->template_arguments
6089 = obstack_alloc (&objfile->objfile_obstack,
6090 (templ_func->n_template_arguments
6091 * sizeof (struct symbol *)));
6092 memcpy (templ_func->template_arguments,
6093 VEC_address (symbolp, template_args),
6094 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6095 VEC_free (symbolp, template_args);
6096 }
6097
6098 /* In C++, we can have functions nested inside functions (e.g., when
6099 a function declares a class that has methods). This means that
6100 when we finish processing a function scope, we may need to go
6101 back to building a containing block's symbol lists. */
6102 local_symbols = new->locals;
6103 param_symbols = new->params;
6104 using_directives = new->using_directives;
6105
6106 /* If we've finished processing a top-level function, subsequent
6107 symbols go in the file symbol list. */
6108 if (outermost_context_p ())
6109 cu->list_in_scope = &file_symbols;
6110 }
6111
6112 /* Process all the DIES contained within a lexical block scope. Start
6113 a new scope, process the dies, and then close the scope. */
6114
6115 static void
6116 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6117 {
6118 struct objfile *objfile = cu->objfile;
6119 struct context_stack *new;
6120 CORE_ADDR lowpc, highpc;
6121 struct die_info *child_die;
6122 CORE_ADDR baseaddr;
6123
6124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6125
6126 /* Ignore blocks with missing or invalid low and high pc attributes. */
6127 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6128 as multiple lexical blocks? Handling children in a sane way would
6129 be nasty. Might be easier to properly extend generic blocks to
6130 describe ranges. */
6131 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6132 return;
6133 lowpc += baseaddr;
6134 highpc += baseaddr;
6135
6136 push_context (0, lowpc);
6137 if (die->child != NULL)
6138 {
6139 child_die = die->child;
6140 while (child_die && child_die->tag)
6141 {
6142 process_die (child_die, cu);
6143 child_die = sibling_die (child_die);
6144 }
6145 }
6146 new = pop_context ();
6147
6148 if (local_symbols != NULL || using_directives != NULL)
6149 {
6150 struct block *block
6151 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6152 highpc, objfile);
6153
6154 /* Note that recording ranges after traversing children, as we
6155 do here, means that recording a parent's ranges entails
6156 walking across all its children's ranges as they appear in
6157 the address map, which is quadratic behavior.
6158
6159 It would be nicer to record the parent's ranges before
6160 traversing its children, simply overriding whatever you find
6161 there. But since we don't even decide whether to create a
6162 block until after we've traversed its children, that's hard
6163 to do. */
6164 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6165 }
6166 local_symbols = new->locals;
6167 using_directives = new->using_directives;
6168 }
6169
6170 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6171
6172 static void
6173 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6174 {
6175 struct objfile *objfile = cu->objfile;
6176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6177 CORE_ADDR pc, baseaddr;
6178 struct attribute *attr;
6179 struct call_site *call_site, call_site_local;
6180 void **slot;
6181 int nparams;
6182 struct die_info *child_die;
6183
6184 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6185
6186 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6187 if (!attr)
6188 {
6189 complaint (&symfile_complaints,
6190 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6191 "DIE 0x%x [in module %s]"),
6192 die->offset, objfile->name);
6193 return;
6194 }
6195 pc = DW_ADDR (attr) + baseaddr;
6196
6197 if (cu->call_site_htab == NULL)
6198 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6199 NULL, &objfile->objfile_obstack,
6200 hashtab_obstack_allocate, NULL);
6201 call_site_local.pc = pc;
6202 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6203 if (*slot != NULL)
6204 {
6205 complaint (&symfile_complaints,
6206 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6207 "DIE 0x%x [in module %s]"),
6208 paddress (gdbarch, pc), die->offset, objfile->name);
6209 return;
6210 }
6211
6212 /* Count parameters at the caller. */
6213
6214 nparams = 0;
6215 for (child_die = die->child; child_die && child_die->tag;
6216 child_die = sibling_die (child_die))
6217 {
6218 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6219 {
6220 complaint (&symfile_complaints,
6221 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6222 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6223 child_die->tag, child_die->offset, objfile->name);
6224 continue;
6225 }
6226
6227 nparams++;
6228 }
6229
6230 call_site = obstack_alloc (&objfile->objfile_obstack,
6231 (sizeof (*call_site)
6232 + (sizeof (*call_site->parameter)
6233 * (nparams - 1))));
6234 *slot = call_site;
6235 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6236 call_site->pc = pc;
6237
6238 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6239 {
6240 struct die_info *func_die;
6241
6242 /* Skip also over DW_TAG_inlined_subroutine. */
6243 for (func_die = die->parent;
6244 func_die && func_die->tag != DW_TAG_subprogram
6245 && func_die->tag != DW_TAG_subroutine_type;
6246 func_die = func_die->parent);
6247
6248 /* DW_AT_GNU_all_call_sites is a superset
6249 of DW_AT_GNU_all_tail_call_sites. */
6250 if (func_die
6251 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6252 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6253 {
6254 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6255 not complete. But keep CALL_SITE for look ups via call_site_htab,
6256 both the initial caller containing the real return address PC and
6257 the final callee containing the current PC of a chain of tail
6258 calls do not need to have the tail call list complete. But any
6259 function candidate for a virtual tail call frame searched via
6260 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6261 determined unambiguously. */
6262 }
6263 else
6264 {
6265 struct type *func_type = NULL;
6266
6267 if (func_die)
6268 func_type = get_die_type (func_die, cu);
6269 if (func_type != NULL)
6270 {
6271 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6272
6273 /* Enlist this call site to the function. */
6274 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6275 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6276 }
6277 else
6278 complaint (&symfile_complaints,
6279 _("Cannot find function owning DW_TAG_GNU_call_site "
6280 "DIE 0x%x [in module %s]"),
6281 die->offset, objfile->name);
6282 }
6283 }
6284
6285 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6286 if (attr == NULL)
6287 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6288 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6289 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6290 /* Keep NULL DWARF_BLOCK. */;
6291 else if (attr_form_is_block (attr))
6292 {
6293 struct dwarf2_locexpr_baton *dlbaton;
6294
6295 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6296 dlbaton->data = DW_BLOCK (attr)->data;
6297 dlbaton->size = DW_BLOCK (attr)->size;
6298 dlbaton->per_cu = cu->per_cu;
6299
6300 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6301 }
6302 else if (is_ref_attr (attr))
6303 {
6304 struct dwarf2_cu *target_cu = cu;
6305 struct die_info *target_die;
6306
6307 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6308 gdb_assert (target_cu->objfile == objfile);
6309 if (die_is_declaration (target_die, target_cu))
6310 {
6311 const char *target_physname;
6312
6313 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6314 if (target_physname == NULL)
6315 complaint (&symfile_complaints,
6316 _("DW_AT_GNU_call_site_target target DIE has invalid "
6317 "physname, for referencing DIE 0x%x [in module %s]"),
6318 die->offset, objfile->name);
6319 else
6320 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6321 }
6322 else
6323 {
6324 CORE_ADDR lowpc;
6325
6326 /* DW_AT_entry_pc should be preferred. */
6327 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6328 complaint (&symfile_complaints,
6329 _("DW_AT_GNU_call_site_target target DIE has invalid "
6330 "low pc, for referencing DIE 0x%x [in module %s]"),
6331 die->offset, objfile->name);
6332 else
6333 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6334 }
6335 }
6336 else
6337 complaint (&symfile_complaints,
6338 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6339 "block nor reference, for DIE 0x%x [in module %s]"),
6340 die->offset, objfile->name);
6341
6342 call_site->per_cu = cu->per_cu;
6343
6344 for (child_die = die->child;
6345 child_die && child_die->tag;
6346 child_die = sibling_die (child_die))
6347 {
6348 struct dwarf2_locexpr_baton *dlbaton;
6349 struct call_site_parameter *parameter;
6350
6351 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6352 {
6353 /* Already printed the complaint above. */
6354 continue;
6355 }
6356
6357 gdb_assert (call_site->parameter_count < nparams);
6358 parameter = &call_site->parameter[call_site->parameter_count];
6359
6360 /* DW_AT_location specifies the register number. Value of the data
6361 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6362
6363 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6364 if (!attr || !attr_form_is_block (attr))
6365 {
6366 complaint (&symfile_complaints,
6367 _("No DW_FORM_block* DW_AT_location for "
6368 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6369 child_die->offset, objfile->name);
6370 continue;
6371 }
6372 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6373 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6374 if (parameter->dwarf_reg == -1
6375 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6376 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6377 &parameter->fb_offset))
6378 {
6379 complaint (&symfile_complaints,
6380 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6381 "for DW_FORM_block* DW_AT_location for "
6382 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6383 child_die->offset, objfile->name);
6384 continue;
6385 }
6386
6387 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6388 if (!attr_form_is_block (attr))
6389 {
6390 complaint (&symfile_complaints,
6391 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6392 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6393 child_die->offset, objfile->name);
6394 continue;
6395 }
6396 parameter->value = DW_BLOCK (attr)->data;
6397 parameter->value_size = DW_BLOCK (attr)->size;
6398
6399 /* Parameters are not pre-cleared by memset above. */
6400 parameter->data_value = NULL;
6401 parameter->data_value_size = 0;
6402 call_site->parameter_count++;
6403
6404 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6405 if (attr)
6406 {
6407 if (!attr_form_is_block (attr))
6408 complaint (&symfile_complaints,
6409 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6410 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6411 child_die->offset, objfile->name);
6412 else
6413 {
6414 parameter->data_value = DW_BLOCK (attr)->data;
6415 parameter->data_value_size = DW_BLOCK (attr)->size;
6416 }
6417 }
6418 }
6419 }
6420
6421 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6422 Return 1 if the attributes are present and valid, otherwise, return 0.
6423 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6424
6425 static int
6426 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6427 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6428 struct partial_symtab *ranges_pst)
6429 {
6430 struct objfile *objfile = cu->objfile;
6431 struct comp_unit_head *cu_header = &cu->header;
6432 bfd *obfd = objfile->obfd;
6433 unsigned int addr_size = cu_header->addr_size;
6434 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6435 /* Base address selection entry. */
6436 CORE_ADDR base;
6437 int found_base;
6438 unsigned int dummy;
6439 gdb_byte *buffer;
6440 CORE_ADDR marker;
6441 int low_set;
6442 CORE_ADDR low = 0;
6443 CORE_ADDR high = 0;
6444 CORE_ADDR baseaddr;
6445
6446 found_base = cu->base_known;
6447 base = cu->base_address;
6448
6449 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6450 if (offset >= dwarf2_per_objfile->ranges.size)
6451 {
6452 complaint (&symfile_complaints,
6453 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6454 offset);
6455 return 0;
6456 }
6457 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6458
6459 /* Read in the largest possible address. */
6460 marker = read_address (obfd, buffer, cu, &dummy);
6461 if ((marker & mask) == mask)
6462 {
6463 /* If we found the largest possible address, then
6464 read the base address. */
6465 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6466 buffer += 2 * addr_size;
6467 offset += 2 * addr_size;
6468 found_base = 1;
6469 }
6470
6471 low_set = 0;
6472
6473 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6474
6475 while (1)
6476 {
6477 CORE_ADDR range_beginning, range_end;
6478
6479 range_beginning = read_address (obfd, buffer, cu, &dummy);
6480 buffer += addr_size;
6481 range_end = read_address (obfd, buffer, cu, &dummy);
6482 buffer += addr_size;
6483 offset += 2 * addr_size;
6484
6485 /* An end of list marker is a pair of zero addresses. */
6486 if (range_beginning == 0 && range_end == 0)
6487 /* Found the end of list entry. */
6488 break;
6489
6490 /* Each base address selection entry is a pair of 2 values.
6491 The first is the largest possible address, the second is
6492 the base address. Check for a base address here. */
6493 if ((range_beginning & mask) == mask)
6494 {
6495 /* If we found the largest possible address, then
6496 read the base address. */
6497 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6498 found_base = 1;
6499 continue;
6500 }
6501
6502 if (!found_base)
6503 {
6504 /* We have no valid base address for the ranges
6505 data. */
6506 complaint (&symfile_complaints,
6507 _("Invalid .debug_ranges data (no base address)"));
6508 return 0;
6509 }
6510
6511 if (range_beginning > range_end)
6512 {
6513 /* Inverted range entries are invalid. */
6514 complaint (&symfile_complaints,
6515 _("Invalid .debug_ranges data (inverted range)"));
6516 return 0;
6517 }
6518
6519 /* Empty range entries have no effect. */
6520 if (range_beginning == range_end)
6521 continue;
6522
6523 range_beginning += base;
6524 range_end += base;
6525
6526 if (ranges_pst != NULL)
6527 addrmap_set_empty (objfile->psymtabs_addrmap,
6528 range_beginning + baseaddr,
6529 range_end - 1 + baseaddr,
6530 ranges_pst);
6531
6532 /* FIXME: This is recording everything as a low-high
6533 segment of consecutive addresses. We should have a
6534 data structure for discontiguous block ranges
6535 instead. */
6536 if (! low_set)
6537 {
6538 low = range_beginning;
6539 high = range_end;
6540 low_set = 1;
6541 }
6542 else
6543 {
6544 if (range_beginning < low)
6545 low = range_beginning;
6546 if (range_end > high)
6547 high = range_end;
6548 }
6549 }
6550
6551 if (! low_set)
6552 /* If the first entry is an end-of-list marker, the range
6553 describes an empty scope, i.e. no instructions. */
6554 return 0;
6555
6556 if (low_return)
6557 *low_return = low;
6558 if (high_return)
6559 *high_return = high;
6560 return 1;
6561 }
6562
6563 /* Get low and high pc attributes from a die. Return 1 if the attributes
6564 are present and valid, otherwise, return 0. Return -1 if the range is
6565 discontinuous, i.e. derived from DW_AT_ranges information. */
6566 static int
6567 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6568 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6569 struct partial_symtab *pst)
6570 {
6571 struct attribute *attr;
6572 CORE_ADDR low = 0;
6573 CORE_ADDR high = 0;
6574 int ret = 0;
6575
6576 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6577 if (attr)
6578 {
6579 high = DW_ADDR (attr);
6580 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6581 if (attr)
6582 low = DW_ADDR (attr);
6583 else
6584 /* Found high w/o low attribute. */
6585 return 0;
6586
6587 /* Found consecutive range of addresses. */
6588 ret = 1;
6589 }
6590 else
6591 {
6592 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6593 if (attr != NULL)
6594 {
6595 /* Value of the DW_AT_ranges attribute is the offset in the
6596 .debug_ranges section. */
6597 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6598 return 0;
6599 /* Found discontinuous range of addresses. */
6600 ret = -1;
6601 }
6602 }
6603
6604 /* read_partial_die has also the strict LOW < HIGH requirement. */
6605 if (high <= low)
6606 return 0;
6607
6608 /* When using the GNU linker, .gnu.linkonce. sections are used to
6609 eliminate duplicate copies of functions and vtables and such.
6610 The linker will arbitrarily choose one and discard the others.
6611 The AT_*_pc values for such functions refer to local labels in
6612 these sections. If the section from that file was discarded, the
6613 labels are not in the output, so the relocs get a value of 0.
6614 If this is a discarded function, mark the pc bounds as invalid,
6615 so that GDB will ignore it. */
6616 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6617 return 0;
6618
6619 *lowpc = low;
6620 if (highpc)
6621 *highpc = high;
6622 return ret;
6623 }
6624
6625 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6626 its low and high PC addresses. Do nothing if these addresses could not
6627 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6628 and HIGHPC to the high address if greater than HIGHPC. */
6629
6630 static void
6631 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6632 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6633 struct dwarf2_cu *cu)
6634 {
6635 CORE_ADDR low, high;
6636 struct die_info *child = die->child;
6637
6638 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6639 {
6640 *lowpc = min (*lowpc, low);
6641 *highpc = max (*highpc, high);
6642 }
6643
6644 /* If the language does not allow nested subprograms (either inside
6645 subprograms or lexical blocks), we're done. */
6646 if (cu->language != language_ada)
6647 return;
6648
6649 /* Check all the children of the given DIE. If it contains nested
6650 subprograms, then check their pc bounds. Likewise, we need to
6651 check lexical blocks as well, as they may also contain subprogram
6652 definitions. */
6653 while (child && child->tag)
6654 {
6655 if (child->tag == DW_TAG_subprogram
6656 || child->tag == DW_TAG_lexical_block)
6657 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6658 child = sibling_die (child);
6659 }
6660 }
6661
6662 /* Get the low and high pc's represented by the scope DIE, and store
6663 them in *LOWPC and *HIGHPC. If the correct values can't be
6664 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6665
6666 static void
6667 get_scope_pc_bounds (struct die_info *die,
6668 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6669 struct dwarf2_cu *cu)
6670 {
6671 CORE_ADDR best_low = (CORE_ADDR) -1;
6672 CORE_ADDR best_high = (CORE_ADDR) 0;
6673 CORE_ADDR current_low, current_high;
6674
6675 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6676 {
6677 best_low = current_low;
6678 best_high = current_high;
6679 }
6680 else
6681 {
6682 struct die_info *child = die->child;
6683
6684 while (child && child->tag)
6685 {
6686 switch (child->tag) {
6687 case DW_TAG_subprogram:
6688 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6689 break;
6690 case DW_TAG_namespace:
6691 case DW_TAG_module:
6692 /* FIXME: carlton/2004-01-16: Should we do this for
6693 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6694 that current GCC's always emit the DIEs corresponding
6695 to definitions of methods of classes as children of a
6696 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6697 the DIEs giving the declarations, which could be
6698 anywhere). But I don't see any reason why the
6699 standards says that they have to be there. */
6700 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6701
6702 if (current_low != ((CORE_ADDR) -1))
6703 {
6704 best_low = min (best_low, current_low);
6705 best_high = max (best_high, current_high);
6706 }
6707 break;
6708 default:
6709 /* Ignore. */
6710 break;
6711 }
6712
6713 child = sibling_die (child);
6714 }
6715 }
6716
6717 *lowpc = best_low;
6718 *highpc = best_high;
6719 }
6720
6721 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6722 in DIE. */
6723 static void
6724 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6725 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6726 {
6727 struct objfile *objfile = cu->objfile;
6728 struct attribute *attr;
6729
6730 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6731 if (attr)
6732 {
6733 CORE_ADDR high = DW_ADDR (attr);
6734
6735 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6736 if (attr)
6737 {
6738 CORE_ADDR low = DW_ADDR (attr);
6739
6740 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6741 }
6742 }
6743
6744 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6745 if (attr)
6746 {
6747 bfd *obfd = objfile->obfd;
6748
6749 /* The value of the DW_AT_ranges attribute is the offset of the
6750 address range list in the .debug_ranges section. */
6751 unsigned long offset = DW_UNSND (attr);
6752 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6753
6754 /* For some target architectures, but not others, the
6755 read_address function sign-extends the addresses it returns.
6756 To recognize base address selection entries, we need a
6757 mask. */
6758 unsigned int addr_size = cu->header.addr_size;
6759 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6760
6761 /* The base address, to which the next pair is relative. Note
6762 that this 'base' is a DWARF concept: most entries in a range
6763 list are relative, to reduce the number of relocs against the
6764 debugging information. This is separate from this function's
6765 'baseaddr' argument, which GDB uses to relocate debugging
6766 information from a shared library based on the address at
6767 which the library was loaded. */
6768 CORE_ADDR base = cu->base_address;
6769 int base_known = cu->base_known;
6770
6771 gdb_assert (dwarf2_per_objfile->ranges.readin);
6772 if (offset >= dwarf2_per_objfile->ranges.size)
6773 {
6774 complaint (&symfile_complaints,
6775 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6776 offset);
6777 return;
6778 }
6779
6780 for (;;)
6781 {
6782 unsigned int bytes_read;
6783 CORE_ADDR start, end;
6784
6785 start = read_address (obfd, buffer, cu, &bytes_read);
6786 buffer += bytes_read;
6787 end = read_address (obfd, buffer, cu, &bytes_read);
6788 buffer += bytes_read;
6789
6790 /* Did we find the end of the range list? */
6791 if (start == 0 && end == 0)
6792 break;
6793
6794 /* Did we find a base address selection entry? */
6795 else if ((start & base_select_mask) == base_select_mask)
6796 {
6797 base = end;
6798 base_known = 1;
6799 }
6800
6801 /* We found an ordinary address range. */
6802 else
6803 {
6804 if (!base_known)
6805 {
6806 complaint (&symfile_complaints,
6807 _("Invalid .debug_ranges data "
6808 "(no base address)"));
6809 return;
6810 }
6811
6812 if (start > end)
6813 {
6814 /* Inverted range entries are invalid. */
6815 complaint (&symfile_complaints,
6816 _("Invalid .debug_ranges data "
6817 "(inverted range)"));
6818 return;
6819 }
6820
6821 /* Empty range entries have no effect. */
6822 if (start == end)
6823 continue;
6824
6825 record_block_range (block,
6826 baseaddr + base + start,
6827 baseaddr + base + end - 1);
6828 }
6829 }
6830 }
6831 }
6832
6833 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6834 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6835 during 4.6.0 experimental. */
6836
6837 static int
6838 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6839 {
6840 const char *cs;
6841 int major, minor, release;
6842
6843 if (cu->producer == NULL)
6844 {
6845 /* For unknown compilers expect their behavior is DWARF version
6846 compliant.
6847
6848 GCC started to support .debug_types sections by -gdwarf-4 since
6849 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6850 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6851 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6852 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6853
6854 return 0;
6855 }
6856
6857 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6858
6859 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6860 {
6861 /* For non-GCC compilers expect their behavior is DWARF version
6862 compliant. */
6863
6864 return 0;
6865 }
6866 cs = &cu->producer[strlen ("GNU ")];
6867 while (*cs && !isdigit (*cs))
6868 cs++;
6869 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6870 {
6871 /* Not recognized as GCC. */
6872
6873 return 0;
6874 }
6875
6876 return major < 4 || (major == 4 && minor < 6);
6877 }
6878
6879 /* Return the default accessibility type if it is not overriden by
6880 DW_AT_accessibility. */
6881
6882 static enum dwarf_access_attribute
6883 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6884 {
6885 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6886 {
6887 /* The default DWARF 2 accessibility for members is public, the default
6888 accessibility for inheritance is private. */
6889
6890 if (die->tag != DW_TAG_inheritance)
6891 return DW_ACCESS_public;
6892 else
6893 return DW_ACCESS_private;
6894 }
6895 else
6896 {
6897 /* DWARF 3+ defines the default accessibility a different way. The same
6898 rules apply now for DW_TAG_inheritance as for the members and it only
6899 depends on the container kind. */
6900
6901 if (die->parent->tag == DW_TAG_class_type)
6902 return DW_ACCESS_private;
6903 else
6904 return DW_ACCESS_public;
6905 }
6906 }
6907
6908 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6909 offset. If the attribute was not found return 0, otherwise return
6910 1. If it was found but could not properly be handled, set *OFFSET
6911 to 0. */
6912
6913 static int
6914 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6915 LONGEST *offset)
6916 {
6917 struct attribute *attr;
6918
6919 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6920 if (attr != NULL)
6921 {
6922 *offset = 0;
6923
6924 /* Note that we do not check for a section offset first here.
6925 This is because DW_AT_data_member_location is new in DWARF 4,
6926 so if we see it, we can assume that a constant form is really
6927 a constant and not a section offset. */
6928 if (attr_form_is_constant (attr))
6929 *offset = dwarf2_get_attr_constant_value (attr, 0);
6930 else if (attr_form_is_section_offset (attr))
6931 dwarf2_complex_location_expr_complaint ();
6932 else if (attr_form_is_block (attr))
6933 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6934 else
6935 dwarf2_complex_location_expr_complaint ();
6936
6937 return 1;
6938 }
6939
6940 return 0;
6941 }
6942
6943 /* Add an aggregate field to the field list. */
6944
6945 static void
6946 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6947 struct dwarf2_cu *cu)
6948 {
6949 struct objfile *objfile = cu->objfile;
6950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6951 struct nextfield *new_field;
6952 struct attribute *attr;
6953 struct field *fp;
6954 char *fieldname = "";
6955
6956 /* Allocate a new field list entry and link it in. */
6957 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6958 make_cleanup (xfree, new_field);
6959 memset (new_field, 0, sizeof (struct nextfield));
6960
6961 if (die->tag == DW_TAG_inheritance)
6962 {
6963 new_field->next = fip->baseclasses;
6964 fip->baseclasses = new_field;
6965 }
6966 else
6967 {
6968 new_field->next = fip->fields;
6969 fip->fields = new_field;
6970 }
6971 fip->nfields++;
6972
6973 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6974 if (attr)
6975 new_field->accessibility = DW_UNSND (attr);
6976 else
6977 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6978 if (new_field->accessibility != DW_ACCESS_public)
6979 fip->non_public_fields = 1;
6980
6981 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6982 if (attr)
6983 new_field->virtuality = DW_UNSND (attr);
6984 else
6985 new_field->virtuality = DW_VIRTUALITY_none;
6986
6987 fp = &new_field->field;
6988
6989 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6990 {
6991 LONGEST offset;
6992
6993 /* Data member other than a C++ static data member. */
6994
6995 /* Get type of field. */
6996 fp->type = die_type (die, cu);
6997
6998 SET_FIELD_BITPOS (*fp, 0);
6999
7000 /* Get bit size of field (zero if none). */
7001 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7002 if (attr)
7003 {
7004 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7005 }
7006 else
7007 {
7008 FIELD_BITSIZE (*fp) = 0;
7009 }
7010
7011 /* Get bit offset of field. */
7012 if (handle_data_member_location (die, cu, &offset))
7013 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7014 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7015 if (attr)
7016 {
7017 if (gdbarch_bits_big_endian (gdbarch))
7018 {
7019 /* For big endian bits, the DW_AT_bit_offset gives the
7020 additional bit offset from the MSB of the containing
7021 anonymous object to the MSB of the field. We don't
7022 have to do anything special since we don't need to
7023 know the size of the anonymous object. */
7024 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7025 }
7026 else
7027 {
7028 /* For little endian bits, compute the bit offset to the
7029 MSB of the anonymous object, subtract off the number of
7030 bits from the MSB of the field to the MSB of the
7031 object, and then subtract off the number of bits of
7032 the field itself. The result is the bit offset of
7033 the LSB of the field. */
7034 int anonymous_size;
7035 int bit_offset = DW_UNSND (attr);
7036
7037 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7038 if (attr)
7039 {
7040 /* The size of the anonymous object containing
7041 the bit field is explicit, so use the
7042 indicated size (in bytes). */
7043 anonymous_size = DW_UNSND (attr);
7044 }
7045 else
7046 {
7047 /* The size of the anonymous object containing
7048 the bit field must be inferred from the type
7049 attribute of the data member containing the
7050 bit field. */
7051 anonymous_size = TYPE_LENGTH (fp->type);
7052 }
7053 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7054 - bit_offset - FIELD_BITSIZE (*fp);
7055 }
7056 }
7057
7058 /* Get name of field. */
7059 fieldname = dwarf2_name (die, cu);
7060 if (fieldname == NULL)
7061 fieldname = "";
7062
7063 /* The name is already allocated along with this objfile, so we don't
7064 need to duplicate it for the type. */
7065 fp->name = fieldname;
7066
7067 /* Change accessibility for artificial fields (e.g. virtual table
7068 pointer or virtual base class pointer) to private. */
7069 if (dwarf2_attr (die, DW_AT_artificial, cu))
7070 {
7071 FIELD_ARTIFICIAL (*fp) = 1;
7072 new_field->accessibility = DW_ACCESS_private;
7073 fip->non_public_fields = 1;
7074 }
7075 }
7076 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7077 {
7078 /* C++ static member. */
7079
7080 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7081 is a declaration, but all versions of G++ as of this writing
7082 (so through at least 3.2.1) incorrectly generate
7083 DW_TAG_variable tags. */
7084
7085 const char *physname;
7086
7087 /* Get name of field. */
7088 fieldname = dwarf2_name (die, cu);
7089 if (fieldname == NULL)
7090 return;
7091
7092 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7093 if (attr
7094 /* Only create a symbol if this is an external value.
7095 new_symbol checks this and puts the value in the global symbol
7096 table, which we want. If it is not external, new_symbol
7097 will try to put the value in cu->list_in_scope which is wrong. */
7098 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7099 {
7100 /* A static const member, not much different than an enum as far as
7101 we're concerned, except that we can support more types. */
7102 new_symbol (die, NULL, cu);
7103 }
7104
7105 /* Get physical name. */
7106 physname = dwarf2_physname (fieldname, die, cu);
7107
7108 /* The name is already allocated along with this objfile, so we don't
7109 need to duplicate it for the type. */
7110 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7111 FIELD_TYPE (*fp) = die_type (die, cu);
7112 FIELD_NAME (*fp) = fieldname;
7113 }
7114 else if (die->tag == DW_TAG_inheritance)
7115 {
7116 LONGEST offset;
7117
7118 /* C++ base class field. */
7119 if (handle_data_member_location (die, cu, &offset))
7120 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7121 FIELD_BITSIZE (*fp) = 0;
7122 FIELD_TYPE (*fp) = die_type (die, cu);
7123 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7124 fip->nbaseclasses++;
7125 }
7126 }
7127
7128 /* Add a typedef defined in the scope of the FIP's class. */
7129
7130 static void
7131 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7132 struct dwarf2_cu *cu)
7133 {
7134 struct objfile *objfile = cu->objfile;
7135 struct typedef_field_list *new_field;
7136 struct attribute *attr;
7137 struct typedef_field *fp;
7138 char *fieldname = "";
7139
7140 /* Allocate a new field list entry and link it in. */
7141 new_field = xzalloc (sizeof (*new_field));
7142 make_cleanup (xfree, new_field);
7143
7144 gdb_assert (die->tag == DW_TAG_typedef);
7145
7146 fp = &new_field->field;
7147
7148 /* Get name of field. */
7149 fp->name = dwarf2_name (die, cu);
7150 if (fp->name == NULL)
7151 return;
7152
7153 fp->type = read_type_die (die, cu);
7154
7155 new_field->next = fip->typedef_field_list;
7156 fip->typedef_field_list = new_field;
7157 fip->typedef_field_list_count++;
7158 }
7159
7160 /* Create the vector of fields, and attach it to the type. */
7161
7162 static void
7163 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7164 struct dwarf2_cu *cu)
7165 {
7166 int nfields = fip->nfields;
7167
7168 /* Record the field count, allocate space for the array of fields,
7169 and create blank accessibility bitfields if necessary. */
7170 TYPE_NFIELDS (type) = nfields;
7171 TYPE_FIELDS (type) = (struct field *)
7172 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7173 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7174
7175 if (fip->non_public_fields && cu->language != language_ada)
7176 {
7177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7178
7179 TYPE_FIELD_PRIVATE_BITS (type) =
7180 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7181 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7182
7183 TYPE_FIELD_PROTECTED_BITS (type) =
7184 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7185 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7186
7187 TYPE_FIELD_IGNORE_BITS (type) =
7188 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7189 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7190 }
7191
7192 /* If the type has baseclasses, allocate and clear a bit vector for
7193 TYPE_FIELD_VIRTUAL_BITS. */
7194 if (fip->nbaseclasses && cu->language != language_ada)
7195 {
7196 int num_bytes = B_BYTES (fip->nbaseclasses);
7197 unsigned char *pointer;
7198
7199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7200 pointer = TYPE_ALLOC (type, num_bytes);
7201 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7202 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7203 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7204 }
7205
7206 /* Copy the saved-up fields into the field vector. Start from the head of
7207 the list, adding to the tail of the field array, so that they end up in
7208 the same order in the array in which they were added to the list. */
7209 while (nfields-- > 0)
7210 {
7211 struct nextfield *fieldp;
7212
7213 if (fip->fields)
7214 {
7215 fieldp = fip->fields;
7216 fip->fields = fieldp->next;
7217 }
7218 else
7219 {
7220 fieldp = fip->baseclasses;
7221 fip->baseclasses = fieldp->next;
7222 }
7223
7224 TYPE_FIELD (type, nfields) = fieldp->field;
7225 switch (fieldp->accessibility)
7226 {
7227 case DW_ACCESS_private:
7228 if (cu->language != language_ada)
7229 SET_TYPE_FIELD_PRIVATE (type, nfields);
7230 break;
7231
7232 case DW_ACCESS_protected:
7233 if (cu->language != language_ada)
7234 SET_TYPE_FIELD_PROTECTED (type, nfields);
7235 break;
7236
7237 case DW_ACCESS_public:
7238 break;
7239
7240 default:
7241 /* Unknown accessibility. Complain and treat it as public. */
7242 {
7243 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7244 fieldp->accessibility);
7245 }
7246 break;
7247 }
7248 if (nfields < fip->nbaseclasses)
7249 {
7250 switch (fieldp->virtuality)
7251 {
7252 case DW_VIRTUALITY_virtual:
7253 case DW_VIRTUALITY_pure_virtual:
7254 if (cu->language == language_ada)
7255 error (_("unexpected virtuality in component of Ada type"));
7256 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7257 break;
7258 }
7259 }
7260 }
7261 }
7262
7263 /* Add a member function to the proper fieldlist. */
7264
7265 static void
7266 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7267 struct type *type, struct dwarf2_cu *cu)
7268 {
7269 struct objfile *objfile = cu->objfile;
7270 struct attribute *attr;
7271 struct fnfieldlist *flp;
7272 int i;
7273 struct fn_field *fnp;
7274 char *fieldname;
7275 struct nextfnfield *new_fnfield;
7276 struct type *this_type;
7277 enum dwarf_access_attribute accessibility;
7278
7279 if (cu->language == language_ada)
7280 error (_("unexpected member function in Ada type"));
7281
7282 /* Get name of member function. */
7283 fieldname = dwarf2_name (die, cu);
7284 if (fieldname == NULL)
7285 return;
7286
7287 /* Look up member function name in fieldlist. */
7288 for (i = 0; i < fip->nfnfields; i++)
7289 {
7290 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7291 break;
7292 }
7293
7294 /* Create new list element if necessary. */
7295 if (i < fip->nfnfields)
7296 flp = &fip->fnfieldlists[i];
7297 else
7298 {
7299 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7300 {
7301 fip->fnfieldlists = (struct fnfieldlist *)
7302 xrealloc (fip->fnfieldlists,
7303 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7304 * sizeof (struct fnfieldlist));
7305 if (fip->nfnfields == 0)
7306 make_cleanup (free_current_contents, &fip->fnfieldlists);
7307 }
7308 flp = &fip->fnfieldlists[fip->nfnfields];
7309 flp->name = fieldname;
7310 flp->length = 0;
7311 flp->head = NULL;
7312 i = fip->nfnfields++;
7313 }
7314
7315 /* Create a new member function field and chain it to the field list
7316 entry. */
7317 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7318 make_cleanup (xfree, new_fnfield);
7319 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7320 new_fnfield->next = flp->head;
7321 flp->head = new_fnfield;
7322 flp->length++;
7323
7324 /* Fill in the member function field info. */
7325 fnp = &new_fnfield->fnfield;
7326
7327 /* Delay processing of the physname until later. */
7328 if (cu->language == language_cplus || cu->language == language_java)
7329 {
7330 add_to_method_list (type, i, flp->length - 1, fieldname,
7331 die, cu);
7332 }
7333 else
7334 {
7335 const char *physname = dwarf2_physname (fieldname, die, cu);
7336 fnp->physname = physname ? physname : "";
7337 }
7338
7339 fnp->type = alloc_type (objfile);
7340 this_type = read_type_die (die, cu);
7341 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7342 {
7343 int nparams = TYPE_NFIELDS (this_type);
7344
7345 /* TYPE is the domain of this method, and THIS_TYPE is the type
7346 of the method itself (TYPE_CODE_METHOD). */
7347 smash_to_method_type (fnp->type, type,
7348 TYPE_TARGET_TYPE (this_type),
7349 TYPE_FIELDS (this_type),
7350 TYPE_NFIELDS (this_type),
7351 TYPE_VARARGS (this_type));
7352
7353 /* Handle static member functions.
7354 Dwarf2 has no clean way to discern C++ static and non-static
7355 member functions. G++ helps GDB by marking the first
7356 parameter for non-static member functions (which is the this
7357 pointer) as artificial. We obtain this information from
7358 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7359 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7360 fnp->voffset = VOFFSET_STATIC;
7361 }
7362 else
7363 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7364 dwarf2_full_name (fieldname, die, cu));
7365
7366 /* Get fcontext from DW_AT_containing_type if present. */
7367 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7368 fnp->fcontext = die_containing_type (die, cu);
7369
7370 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7371 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7372
7373 /* Get accessibility. */
7374 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7375 if (attr)
7376 accessibility = DW_UNSND (attr);
7377 else
7378 accessibility = dwarf2_default_access_attribute (die, cu);
7379 switch (accessibility)
7380 {
7381 case DW_ACCESS_private:
7382 fnp->is_private = 1;
7383 break;
7384 case DW_ACCESS_protected:
7385 fnp->is_protected = 1;
7386 break;
7387 }
7388
7389 /* Check for artificial methods. */
7390 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7391 if (attr && DW_UNSND (attr) != 0)
7392 fnp->is_artificial = 1;
7393
7394 /* Get index in virtual function table if it is a virtual member
7395 function. For older versions of GCC, this is an offset in the
7396 appropriate virtual table, as specified by DW_AT_containing_type.
7397 For everyone else, it is an expression to be evaluated relative
7398 to the object address. */
7399
7400 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7401 if (attr)
7402 {
7403 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7404 {
7405 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7406 {
7407 /* Old-style GCC. */
7408 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7409 }
7410 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7411 || (DW_BLOCK (attr)->size > 1
7412 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7413 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7414 {
7415 struct dwarf_block blk;
7416 int offset;
7417
7418 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7419 ? 1 : 2);
7420 blk.size = DW_BLOCK (attr)->size - offset;
7421 blk.data = DW_BLOCK (attr)->data + offset;
7422 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7423 if ((fnp->voffset % cu->header.addr_size) != 0)
7424 dwarf2_complex_location_expr_complaint ();
7425 else
7426 fnp->voffset /= cu->header.addr_size;
7427 fnp->voffset += 2;
7428 }
7429 else
7430 dwarf2_complex_location_expr_complaint ();
7431
7432 if (!fnp->fcontext)
7433 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7434 }
7435 else if (attr_form_is_section_offset (attr))
7436 {
7437 dwarf2_complex_location_expr_complaint ();
7438 }
7439 else
7440 {
7441 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7442 fieldname);
7443 }
7444 }
7445 else
7446 {
7447 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7448 if (attr && DW_UNSND (attr))
7449 {
7450 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7451 complaint (&symfile_complaints,
7452 _("Member function \"%s\" (offset %d) is virtual "
7453 "but the vtable offset is not specified"),
7454 fieldname, die->offset);
7455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7456 TYPE_CPLUS_DYNAMIC (type) = 1;
7457 }
7458 }
7459 }
7460
7461 /* Create the vector of member function fields, and attach it to the type. */
7462
7463 static void
7464 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7465 struct dwarf2_cu *cu)
7466 {
7467 struct fnfieldlist *flp;
7468 int total_length = 0;
7469 int i;
7470
7471 if (cu->language == language_ada)
7472 error (_("unexpected member functions in Ada type"));
7473
7474 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7475 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7476 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7477
7478 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7479 {
7480 struct nextfnfield *nfp = flp->head;
7481 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7482 int k;
7483
7484 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7485 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7486 fn_flp->fn_fields = (struct fn_field *)
7487 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7488 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7489 fn_flp->fn_fields[k] = nfp->fnfield;
7490
7491 total_length += flp->length;
7492 }
7493
7494 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7495 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7496 }
7497
7498 /* Returns non-zero if NAME is the name of a vtable member in CU's
7499 language, zero otherwise. */
7500 static int
7501 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7502 {
7503 static const char vptr[] = "_vptr";
7504 static const char vtable[] = "vtable";
7505
7506 /* Look for the C++ and Java forms of the vtable. */
7507 if ((cu->language == language_java
7508 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7509 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7510 && is_cplus_marker (name[sizeof (vptr) - 1])))
7511 return 1;
7512
7513 return 0;
7514 }
7515
7516 /* GCC outputs unnamed structures that are really pointers to member
7517 functions, with the ABI-specified layout. If TYPE describes
7518 such a structure, smash it into a member function type.
7519
7520 GCC shouldn't do this; it should just output pointer to member DIEs.
7521 This is GCC PR debug/28767. */
7522
7523 static void
7524 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7525 {
7526 struct type *pfn_type, *domain_type, *new_type;
7527
7528 /* Check for a structure with no name and two children. */
7529 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7530 return;
7531
7532 /* Check for __pfn and __delta members. */
7533 if (TYPE_FIELD_NAME (type, 0) == NULL
7534 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7535 || TYPE_FIELD_NAME (type, 1) == NULL
7536 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7537 return;
7538
7539 /* Find the type of the method. */
7540 pfn_type = TYPE_FIELD_TYPE (type, 0);
7541 if (pfn_type == NULL
7542 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7543 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7544 return;
7545
7546 /* Look for the "this" argument. */
7547 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7548 if (TYPE_NFIELDS (pfn_type) == 0
7549 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7550 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7551 return;
7552
7553 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7554 new_type = alloc_type (objfile);
7555 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7556 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7557 TYPE_VARARGS (pfn_type));
7558 smash_to_methodptr_type (type, new_type);
7559 }
7560
7561 /* Called when we find the DIE that starts a structure or union scope
7562 (definition) to create a type for the structure or union. Fill in
7563 the type's name and general properties; the members will not be
7564 processed until process_structure_type.
7565
7566 NOTE: we need to call these functions regardless of whether or not the
7567 DIE has a DW_AT_name attribute, since it might be an anonymous
7568 structure or union. This gets the type entered into our set of
7569 user defined types.
7570
7571 However, if the structure is incomplete (an opaque struct/union)
7572 then suppress creating a symbol table entry for it since gdb only
7573 wants to find the one with the complete definition. Note that if
7574 it is complete, we just call new_symbol, which does it's own
7575 checking about whether the struct/union is anonymous or not (and
7576 suppresses creating a symbol table entry itself). */
7577
7578 static struct type *
7579 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7580 {
7581 struct objfile *objfile = cu->objfile;
7582 struct type *type;
7583 struct attribute *attr;
7584 char *name;
7585
7586 /* If the definition of this type lives in .debug_types, read that type.
7587 Don't follow DW_AT_specification though, that will take us back up
7588 the chain and we want to go down. */
7589 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7590 if (attr)
7591 {
7592 struct dwarf2_cu *type_cu = cu;
7593 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7594
7595 /* We could just recurse on read_structure_type, but we need to call
7596 get_die_type to ensure only one type for this DIE is created.
7597 This is important, for example, because for c++ classes we need
7598 TYPE_NAME set which is only done by new_symbol. Blech. */
7599 type = read_type_die (type_die, type_cu);
7600
7601 /* TYPE_CU may not be the same as CU.
7602 Ensure TYPE is recorded in CU's type_hash table. */
7603 return set_die_type (die, type, cu);
7604 }
7605
7606 type = alloc_type (objfile);
7607 INIT_CPLUS_SPECIFIC (type);
7608
7609 name = dwarf2_name (die, cu);
7610 if (name != NULL)
7611 {
7612 if (cu->language == language_cplus
7613 || cu->language == language_java)
7614 {
7615 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7616
7617 /* dwarf2_full_name might have already finished building the DIE's
7618 type. If so, there is no need to continue. */
7619 if (get_die_type (die, cu) != NULL)
7620 return get_die_type (die, cu);
7621
7622 TYPE_TAG_NAME (type) = full_name;
7623 if (die->tag == DW_TAG_structure_type
7624 || die->tag == DW_TAG_class_type)
7625 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7626 }
7627 else
7628 {
7629 /* The name is already allocated along with this objfile, so
7630 we don't need to duplicate it for the type. */
7631 TYPE_TAG_NAME (type) = (char *) name;
7632 if (die->tag == DW_TAG_class_type)
7633 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7634 }
7635 }
7636
7637 if (die->tag == DW_TAG_structure_type)
7638 {
7639 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7640 }
7641 else if (die->tag == DW_TAG_union_type)
7642 {
7643 TYPE_CODE (type) = TYPE_CODE_UNION;
7644 }
7645 else
7646 {
7647 TYPE_CODE (type) = TYPE_CODE_CLASS;
7648 }
7649
7650 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7651 TYPE_DECLARED_CLASS (type) = 1;
7652
7653 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7654 if (attr)
7655 {
7656 TYPE_LENGTH (type) = DW_UNSND (attr);
7657 }
7658 else
7659 {
7660 TYPE_LENGTH (type) = 0;
7661 }
7662
7663 TYPE_STUB_SUPPORTED (type) = 1;
7664 if (die_is_declaration (die, cu))
7665 TYPE_STUB (type) = 1;
7666 else if (attr == NULL && die->child == NULL
7667 && producer_is_realview (cu->producer))
7668 /* RealView does not output the required DW_AT_declaration
7669 on incomplete types. */
7670 TYPE_STUB (type) = 1;
7671
7672 /* We need to add the type field to the die immediately so we don't
7673 infinitely recurse when dealing with pointers to the structure
7674 type within the structure itself. */
7675 set_die_type (die, type, cu);
7676
7677 /* set_die_type should be already done. */
7678 set_descriptive_type (type, die, cu);
7679
7680 return type;
7681 }
7682
7683 /* Finish creating a structure or union type, including filling in
7684 its members and creating a symbol for it. */
7685
7686 static void
7687 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7688 {
7689 struct objfile *objfile = cu->objfile;
7690 struct die_info *child_die = die->child;
7691 struct type *type;
7692
7693 type = get_die_type (die, cu);
7694 if (type == NULL)
7695 type = read_structure_type (die, cu);
7696
7697 if (die->child != NULL && ! die_is_declaration (die, cu))
7698 {
7699 struct field_info fi;
7700 struct die_info *child_die;
7701 VEC (symbolp) *template_args = NULL;
7702 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7703
7704 memset (&fi, 0, sizeof (struct field_info));
7705
7706 child_die = die->child;
7707
7708 while (child_die && child_die->tag)
7709 {
7710 if (child_die->tag == DW_TAG_member
7711 || child_die->tag == DW_TAG_variable)
7712 {
7713 /* NOTE: carlton/2002-11-05: A C++ static data member
7714 should be a DW_TAG_member that is a declaration, but
7715 all versions of G++ as of this writing (so through at
7716 least 3.2.1) incorrectly generate DW_TAG_variable
7717 tags for them instead. */
7718 dwarf2_add_field (&fi, child_die, cu);
7719 }
7720 else if (child_die->tag == DW_TAG_subprogram)
7721 {
7722 /* C++ member function. */
7723 dwarf2_add_member_fn (&fi, child_die, type, cu);
7724 }
7725 else if (child_die->tag == DW_TAG_inheritance)
7726 {
7727 /* C++ base class field. */
7728 dwarf2_add_field (&fi, child_die, cu);
7729 }
7730 else if (child_die->tag == DW_TAG_typedef)
7731 dwarf2_add_typedef (&fi, child_die, cu);
7732 else if (child_die->tag == DW_TAG_template_type_param
7733 || child_die->tag == DW_TAG_template_value_param)
7734 {
7735 struct symbol *arg = new_symbol (child_die, NULL, cu);
7736
7737 if (arg != NULL)
7738 VEC_safe_push (symbolp, template_args, arg);
7739 }
7740
7741 child_die = sibling_die (child_die);
7742 }
7743
7744 /* Attach template arguments to type. */
7745 if (! VEC_empty (symbolp, template_args))
7746 {
7747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7748 TYPE_N_TEMPLATE_ARGUMENTS (type)
7749 = VEC_length (symbolp, template_args);
7750 TYPE_TEMPLATE_ARGUMENTS (type)
7751 = obstack_alloc (&objfile->objfile_obstack,
7752 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7753 * sizeof (struct symbol *)));
7754 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7755 VEC_address (symbolp, template_args),
7756 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7757 * sizeof (struct symbol *)));
7758 VEC_free (symbolp, template_args);
7759 }
7760
7761 /* Attach fields and member functions to the type. */
7762 if (fi.nfields)
7763 dwarf2_attach_fields_to_type (&fi, type, cu);
7764 if (fi.nfnfields)
7765 {
7766 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7767
7768 /* Get the type which refers to the base class (possibly this
7769 class itself) which contains the vtable pointer for the current
7770 class from the DW_AT_containing_type attribute. This use of
7771 DW_AT_containing_type is a GNU extension. */
7772
7773 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7774 {
7775 struct type *t = die_containing_type (die, cu);
7776
7777 TYPE_VPTR_BASETYPE (type) = t;
7778 if (type == t)
7779 {
7780 int i;
7781
7782 /* Our own class provides vtbl ptr. */
7783 for (i = TYPE_NFIELDS (t) - 1;
7784 i >= TYPE_N_BASECLASSES (t);
7785 --i)
7786 {
7787 char *fieldname = TYPE_FIELD_NAME (t, i);
7788
7789 if (is_vtable_name (fieldname, cu))
7790 {
7791 TYPE_VPTR_FIELDNO (type) = i;
7792 break;
7793 }
7794 }
7795
7796 /* Complain if virtual function table field not found. */
7797 if (i < TYPE_N_BASECLASSES (t))
7798 complaint (&symfile_complaints,
7799 _("virtual function table pointer "
7800 "not found when defining class '%s'"),
7801 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7802 "");
7803 }
7804 else
7805 {
7806 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7807 }
7808 }
7809 else if (cu->producer
7810 && strncmp (cu->producer,
7811 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7812 {
7813 /* The IBM XLC compiler does not provide direct indication
7814 of the containing type, but the vtable pointer is
7815 always named __vfp. */
7816
7817 int i;
7818
7819 for (i = TYPE_NFIELDS (type) - 1;
7820 i >= TYPE_N_BASECLASSES (type);
7821 --i)
7822 {
7823 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7824 {
7825 TYPE_VPTR_FIELDNO (type) = i;
7826 TYPE_VPTR_BASETYPE (type) = type;
7827 break;
7828 }
7829 }
7830 }
7831 }
7832
7833 /* Copy fi.typedef_field_list linked list elements content into the
7834 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7835 if (fi.typedef_field_list)
7836 {
7837 int i = fi.typedef_field_list_count;
7838
7839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7840 TYPE_TYPEDEF_FIELD_ARRAY (type)
7841 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7842 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7843
7844 /* Reverse the list order to keep the debug info elements order. */
7845 while (--i >= 0)
7846 {
7847 struct typedef_field *dest, *src;
7848
7849 dest = &TYPE_TYPEDEF_FIELD (type, i);
7850 src = &fi.typedef_field_list->field;
7851 fi.typedef_field_list = fi.typedef_field_list->next;
7852 *dest = *src;
7853 }
7854 }
7855
7856 do_cleanups (back_to);
7857
7858 if (HAVE_CPLUS_STRUCT (type))
7859 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7860 }
7861
7862 quirk_gcc_member_function_pointer (type, objfile);
7863
7864 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7865 snapshots) has been known to create a die giving a declaration
7866 for a class that has, as a child, a die giving a definition for a
7867 nested class. So we have to process our children even if the
7868 current die is a declaration. Normally, of course, a declaration
7869 won't have any children at all. */
7870
7871 while (child_die != NULL && child_die->tag)
7872 {
7873 if (child_die->tag == DW_TAG_member
7874 || child_die->tag == DW_TAG_variable
7875 || child_die->tag == DW_TAG_inheritance
7876 || child_die->tag == DW_TAG_template_value_param
7877 || child_die->tag == DW_TAG_template_type_param)
7878 {
7879 /* Do nothing. */
7880 }
7881 else
7882 process_die (child_die, cu);
7883
7884 child_die = sibling_die (child_die);
7885 }
7886
7887 /* Do not consider external references. According to the DWARF standard,
7888 these DIEs are identified by the fact that they have no byte_size
7889 attribute, and a declaration attribute. */
7890 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7891 || !die_is_declaration (die, cu))
7892 new_symbol (die, type, cu);
7893 }
7894
7895 /* Given a DW_AT_enumeration_type die, set its type. We do not
7896 complete the type's fields yet, or create any symbols. */
7897
7898 static struct type *
7899 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7900 {
7901 struct objfile *objfile = cu->objfile;
7902 struct type *type;
7903 struct attribute *attr;
7904 const char *name;
7905
7906 /* If the definition of this type lives in .debug_types, read that type.
7907 Don't follow DW_AT_specification though, that will take us back up
7908 the chain and we want to go down. */
7909 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7910 if (attr)
7911 {
7912 struct dwarf2_cu *type_cu = cu;
7913 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7914
7915 type = read_type_die (type_die, type_cu);
7916
7917 /* TYPE_CU may not be the same as CU.
7918 Ensure TYPE is recorded in CU's type_hash table. */
7919 return set_die_type (die, type, cu);
7920 }
7921
7922 type = alloc_type (objfile);
7923
7924 TYPE_CODE (type) = TYPE_CODE_ENUM;
7925 name = dwarf2_full_name (NULL, die, cu);
7926 if (name != NULL)
7927 TYPE_TAG_NAME (type) = (char *) name;
7928
7929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7930 if (attr)
7931 {
7932 TYPE_LENGTH (type) = DW_UNSND (attr);
7933 }
7934 else
7935 {
7936 TYPE_LENGTH (type) = 0;
7937 }
7938
7939 /* The enumeration DIE can be incomplete. In Ada, any type can be
7940 declared as private in the package spec, and then defined only
7941 inside the package body. Such types are known as Taft Amendment
7942 Types. When another package uses such a type, an incomplete DIE
7943 may be generated by the compiler. */
7944 if (die_is_declaration (die, cu))
7945 TYPE_STUB (type) = 1;
7946
7947 return set_die_type (die, type, cu);
7948 }
7949
7950 /* Given a pointer to a die which begins an enumeration, process all
7951 the dies that define the members of the enumeration, and create the
7952 symbol for the enumeration type.
7953
7954 NOTE: We reverse the order of the element list. */
7955
7956 static void
7957 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7958 {
7959 struct type *this_type;
7960
7961 this_type = get_die_type (die, cu);
7962 if (this_type == NULL)
7963 this_type = read_enumeration_type (die, cu);
7964
7965 if (die->child != NULL)
7966 {
7967 struct die_info *child_die;
7968 struct symbol *sym;
7969 struct field *fields = NULL;
7970 int num_fields = 0;
7971 int unsigned_enum = 1;
7972 char *name;
7973
7974 child_die = die->child;
7975 while (child_die && child_die->tag)
7976 {
7977 if (child_die->tag != DW_TAG_enumerator)
7978 {
7979 process_die (child_die, cu);
7980 }
7981 else
7982 {
7983 name = dwarf2_name (child_die, cu);
7984 if (name)
7985 {
7986 sym = new_symbol (child_die, this_type, cu);
7987 if (SYMBOL_VALUE (sym) < 0)
7988 unsigned_enum = 0;
7989
7990 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7991 {
7992 fields = (struct field *)
7993 xrealloc (fields,
7994 (num_fields + DW_FIELD_ALLOC_CHUNK)
7995 * sizeof (struct field));
7996 }
7997
7998 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7999 FIELD_TYPE (fields[num_fields]) = NULL;
8000 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8001 FIELD_BITSIZE (fields[num_fields]) = 0;
8002
8003 num_fields++;
8004 }
8005 }
8006
8007 child_die = sibling_die (child_die);
8008 }
8009
8010 if (num_fields)
8011 {
8012 TYPE_NFIELDS (this_type) = num_fields;
8013 TYPE_FIELDS (this_type) = (struct field *)
8014 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8015 memcpy (TYPE_FIELDS (this_type), fields,
8016 sizeof (struct field) * num_fields);
8017 xfree (fields);
8018 }
8019 if (unsigned_enum)
8020 TYPE_UNSIGNED (this_type) = 1;
8021 }
8022
8023 /* If we are reading an enum from a .debug_types unit, and the enum
8024 is a declaration, and the enum is not the signatured type in the
8025 unit, then we do not want to add a symbol for it. Adding a
8026 symbol would in some cases obscure the true definition of the
8027 enum, giving users an incomplete type when the definition is
8028 actually available. Note that we do not want to do this for all
8029 enums which are just declarations, because C++0x allows forward
8030 enum declarations. */
8031 if (cu->per_cu->debug_types_section
8032 && die_is_declaration (die, cu))
8033 {
8034 struct signatured_type *type_sig;
8035
8036 type_sig
8037 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8038 cu->per_cu->debug_types_section,
8039 cu->per_cu->offset);
8040 if (type_sig->type_offset != die->offset)
8041 return;
8042 }
8043
8044 new_symbol (die, this_type, cu);
8045 }
8046
8047 /* Extract all information from a DW_TAG_array_type DIE and put it in
8048 the DIE's type field. For now, this only handles one dimensional
8049 arrays. */
8050
8051 static struct type *
8052 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8053 {
8054 struct objfile *objfile = cu->objfile;
8055 struct die_info *child_die;
8056 struct type *type;
8057 struct type *element_type, *range_type, *index_type;
8058 struct type **range_types = NULL;
8059 struct attribute *attr;
8060 int ndim = 0;
8061 struct cleanup *back_to;
8062 char *name;
8063
8064 element_type = die_type (die, cu);
8065
8066 /* The die_type call above may have already set the type for this DIE. */
8067 type = get_die_type (die, cu);
8068 if (type)
8069 return type;
8070
8071 /* Irix 6.2 native cc creates array types without children for
8072 arrays with unspecified length. */
8073 if (die->child == NULL)
8074 {
8075 index_type = objfile_type (objfile)->builtin_int;
8076 range_type = create_range_type (NULL, index_type, 0, -1);
8077 type = create_array_type (NULL, element_type, range_type);
8078 return set_die_type (die, type, cu);
8079 }
8080
8081 back_to = make_cleanup (null_cleanup, NULL);
8082 child_die = die->child;
8083 while (child_die && child_die->tag)
8084 {
8085 if (child_die->tag == DW_TAG_subrange_type)
8086 {
8087 struct type *child_type = read_type_die (child_die, cu);
8088
8089 if (child_type != NULL)
8090 {
8091 /* The range type was succesfully read. Save it for the
8092 array type creation. */
8093 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8094 {
8095 range_types = (struct type **)
8096 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8097 * sizeof (struct type *));
8098 if (ndim == 0)
8099 make_cleanup (free_current_contents, &range_types);
8100 }
8101 range_types[ndim++] = child_type;
8102 }
8103 }
8104 child_die = sibling_die (child_die);
8105 }
8106
8107 /* Dwarf2 dimensions are output from left to right, create the
8108 necessary array types in backwards order. */
8109
8110 type = element_type;
8111
8112 if (read_array_order (die, cu) == DW_ORD_col_major)
8113 {
8114 int i = 0;
8115
8116 while (i < ndim)
8117 type = create_array_type (NULL, type, range_types[i++]);
8118 }
8119 else
8120 {
8121 while (ndim-- > 0)
8122 type = create_array_type (NULL, type, range_types[ndim]);
8123 }
8124
8125 /* Understand Dwarf2 support for vector types (like they occur on
8126 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8127 array type. This is not part of the Dwarf2/3 standard yet, but a
8128 custom vendor extension. The main difference between a regular
8129 array and the vector variant is that vectors are passed by value
8130 to functions. */
8131 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8132 if (attr)
8133 make_vector_type (type);
8134
8135 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8136 implementation may choose to implement triple vectors using this
8137 attribute. */
8138 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8139 if (attr)
8140 {
8141 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8142 TYPE_LENGTH (type) = DW_UNSND (attr);
8143 else
8144 complaint (&symfile_complaints,
8145 _("DW_AT_byte_size for array type smaller "
8146 "than the total size of elements"));
8147 }
8148
8149 name = dwarf2_name (die, cu);
8150 if (name)
8151 TYPE_NAME (type) = name;
8152
8153 /* Install the type in the die. */
8154 set_die_type (die, type, cu);
8155
8156 /* set_die_type should be already done. */
8157 set_descriptive_type (type, die, cu);
8158
8159 do_cleanups (back_to);
8160
8161 return type;
8162 }
8163
8164 static enum dwarf_array_dim_ordering
8165 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8166 {
8167 struct attribute *attr;
8168
8169 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8170
8171 if (attr) return DW_SND (attr);
8172
8173 /* GNU F77 is a special case, as at 08/2004 array type info is the
8174 opposite order to the dwarf2 specification, but data is still
8175 laid out as per normal fortran.
8176
8177 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8178 version checking. */
8179
8180 if (cu->language == language_fortran
8181 && cu->producer && strstr (cu->producer, "GNU F77"))
8182 {
8183 return DW_ORD_row_major;
8184 }
8185
8186 switch (cu->language_defn->la_array_ordering)
8187 {
8188 case array_column_major:
8189 return DW_ORD_col_major;
8190 case array_row_major:
8191 default:
8192 return DW_ORD_row_major;
8193 };
8194 }
8195
8196 /* Extract all information from a DW_TAG_set_type DIE and put it in
8197 the DIE's type field. */
8198
8199 static struct type *
8200 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8201 {
8202 struct type *domain_type, *set_type;
8203 struct attribute *attr;
8204
8205 domain_type = die_type (die, cu);
8206
8207 /* The die_type call above may have already set the type for this DIE. */
8208 set_type = get_die_type (die, cu);
8209 if (set_type)
8210 return set_type;
8211
8212 set_type = create_set_type (NULL, domain_type);
8213
8214 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8215 if (attr)
8216 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8217
8218 return set_die_type (die, set_type, cu);
8219 }
8220
8221 /* First cut: install each common block member as a global variable. */
8222
8223 static void
8224 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8225 {
8226 struct die_info *child_die;
8227 struct attribute *attr;
8228 struct symbol *sym;
8229 CORE_ADDR base = (CORE_ADDR) 0;
8230
8231 attr = dwarf2_attr (die, DW_AT_location, cu);
8232 if (attr)
8233 {
8234 /* Support the .debug_loc offsets. */
8235 if (attr_form_is_block (attr))
8236 {
8237 base = decode_locdesc (DW_BLOCK (attr), cu);
8238 }
8239 else if (attr_form_is_section_offset (attr))
8240 {
8241 dwarf2_complex_location_expr_complaint ();
8242 }
8243 else
8244 {
8245 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8246 "common block member");
8247 }
8248 }
8249 if (die->child != NULL)
8250 {
8251 child_die = die->child;
8252 while (child_die && child_die->tag)
8253 {
8254 LONGEST offset;
8255
8256 sym = new_symbol (child_die, NULL, cu);
8257 if (sym != NULL
8258 && handle_data_member_location (child_die, cu, &offset))
8259 {
8260 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8261 add_symbol_to_list (sym, &global_symbols);
8262 }
8263 child_die = sibling_die (child_die);
8264 }
8265 }
8266 }
8267
8268 /* Create a type for a C++ namespace. */
8269
8270 static struct type *
8271 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8272 {
8273 struct objfile *objfile = cu->objfile;
8274 const char *previous_prefix, *name;
8275 int is_anonymous;
8276 struct type *type;
8277
8278 /* For extensions, reuse the type of the original namespace. */
8279 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8280 {
8281 struct die_info *ext_die;
8282 struct dwarf2_cu *ext_cu = cu;
8283
8284 ext_die = dwarf2_extension (die, &ext_cu);
8285 type = read_type_die (ext_die, ext_cu);
8286
8287 /* EXT_CU may not be the same as CU.
8288 Ensure TYPE is recorded in CU's type_hash table. */
8289 return set_die_type (die, type, cu);
8290 }
8291
8292 name = namespace_name (die, &is_anonymous, cu);
8293
8294 /* Now build the name of the current namespace. */
8295
8296 previous_prefix = determine_prefix (die, cu);
8297 if (previous_prefix[0] != '\0')
8298 name = typename_concat (&objfile->objfile_obstack,
8299 previous_prefix, name, 0, cu);
8300
8301 /* Create the type. */
8302 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8303 objfile);
8304 TYPE_NAME (type) = (char *) name;
8305 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8306
8307 return set_die_type (die, type, cu);
8308 }
8309
8310 /* Read a C++ namespace. */
8311
8312 static void
8313 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8314 {
8315 struct objfile *objfile = cu->objfile;
8316 int is_anonymous;
8317
8318 /* Add a symbol associated to this if we haven't seen the namespace
8319 before. Also, add a using directive if it's an anonymous
8320 namespace. */
8321
8322 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8323 {
8324 struct type *type;
8325
8326 type = read_type_die (die, cu);
8327 new_symbol (die, type, cu);
8328
8329 namespace_name (die, &is_anonymous, cu);
8330 if (is_anonymous)
8331 {
8332 const char *previous_prefix = determine_prefix (die, cu);
8333
8334 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8335 NULL, NULL, &objfile->objfile_obstack);
8336 }
8337 }
8338
8339 if (die->child != NULL)
8340 {
8341 struct die_info *child_die = die->child;
8342
8343 while (child_die && child_die->tag)
8344 {
8345 process_die (child_die, cu);
8346 child_die = sibling_die (child_die);
8347 }
8348 }
8349 }
8350
8351 /* Read a Fortran module as type. This DIE can be only a declaration used for
8352 imported module. Still we need that type as local Fortran "use ... only"
8353 declaration imports depend on the created type in determine_prefix. */
8354
8355 static struct type *
8356 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8357 {
8358 struct objfile *objfile = cu->objfile;
8359 char *module_name;
8360 struct type *type;
8361
8362 module_name = dwarf2_name (die, cu);
8363 if (!module_name)
8364 complaint (&symfile_complaints,
8365 _("DW_TAG_module has no name, offset 0x%x"),
8366 die->offset);
8367 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8368
8369 /* determine_prefix uses TYPE_TAG_NAME. */
8370 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8371
8372 return set_die_type (die, type, cu);
8373 }
8374
8375 /* Read a Fortran module. */
8376
8377 static void
8378 read_module (struct die_info *die, struct dwarf2_cu *cu)
8379 {
8380 struct die_info *child_die = die->child;
8381
8382 while (child_die && child_die->tag)
8383 {
8384 process_die (child_die, cu);
8385 child_die = sibling_die (child_die);
8386 }
8387 }
8388
8389 /* Return the name of the namespace represented by DIE. Set
8390 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8391 namespace. */
8392
8393 static const char *
8394 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8395 {
8396 struct die_info *current_die;
8397 const char *name = NULL;
8398
8399 /* Loop through the extensions until we find a name. */
8400
8401 for (current_die = die;
8402 current_die != NULL;
8403 current_die = dwarf2_extension (die, &cu))
8404 {
8405 name = dwarf2_name (current_die, cu);
8406 if (name != NULL)
8407 break;
8408 }
8409
8410 /* Is it an anonymous namespace? */
8411
8412 *is_anonymous = (name == NULL);
8413 if (*is_anonymous)
8414 name = CP_ANONYMOUS_NAMESPACE_STR;
8415
8416 return name;
8417 }
8418
8419 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8420 the user defined type vector. */
8421
8422 static struct type *
8423 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8424 {
8425 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8426 struct comp_unit_head *cu_header = &cu->header;
8427 struct type *type;
8428 struct attribute *attr_byte_size;
8429 struct attribute *attr_address_class;
8430 int byte_size, addr_class;
8431 struct type *target_type;
8432
8433 target_type = die_type (die, cu);
8434
8435 /* The die_type call above may have already set the type for this DIE. */
8436 type = get_die_type (die, cu);
8437 if (type)
8438 return type;
8439
8440 type = lookup_pointer_type (target_type);
8441
8442 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8443 if (attr_byte_size)
8444 byte_size = DW_UNSND (attr_byte_size);
8445 else
8446 byte_size = cu_header->addr_size;
8447
8448 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8449 if (attr_address_class)
8450 addr_class = DW_UNSND (attr_address_class);
8451 else
8452 addr_class = DW_ADDR_none;
8453
8454 /* If the pointer size or address class is different than the
8455 default, create a type variant marked as such and set the
8456 length accordingly. */
8457 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8458 {
8459 if (gdbarch_address_class_type_flags_p (gdbarch))
8460 {
8461 int type_flags;
8462
8463 type_flags = gdbarch_address_class_type_flags
8464 (gdbarch, byte_size, addr_class);
8465 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8466 == 0);
8467 type = make_type_with_address_space (type, type_flags);
8468 }
8469 else if (TYPE_LENGTH (type) != byte_size)
8470 {
8471 complaint (&symfile_complaints,
8472 _("invalid pointer size %d"), byte_size);
8473 }
8474 else
8475 {
8476 /* Should we also complain about unhandled address classes? */
8477 }
8478 }
8479
8480 TYPE_LENGTH (type) = byte_size;
8481 return set_die_type (die, type, cu);
8482 }
8483
8484 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8485 the user defined type vector. */
8486
8487 static struct type *
8488 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8489 {
8490 struct type *type;
8491 struct type *to_type;
8492 struct type *domain;
8493
8494 to_type = die_type (die, cu);
8495 domain = die_containing_type (die, cu);
8496
8497 /* The calls above may have already set the type for this DIE. */
8498 type = get_die_type (die, cu);
8499 if (type)
8500 return type;
8501
8502 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8503 type = lookup_methodptr_type (to_type);
8504 else
8505 type = lookup_memberptr_type (to_type, domain);
8506
8507 return set_die_type (die, type, cu);
8508 }
8509
8510 /* Extract all information from a DW_TAG_reference_type DIE and add to
8511 the user defined type vector. */
8512
8513 static struct type *
8514 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8515 {
8516 struct comp_unit_head *cu_header = &cu->header;
8517 struct type *type, *target_type;
8518 struct attribute *attr;
8519
8520 target_type = die_type (die, cu);
8521
8522 /* The die_type call above may have already set the type for this DIE. */
8523 type = get_die_type (die, cu);
8524 if (type)
8525 return type;
8526
8527 type = lookup_reference_type (target_type);
8528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8529 if (attr)
8530 {
8531 TYPE_LENGTH (type) = DW_UNSND (attr);
8532 }
8533 else
8534 {
8535 TYPE_LENGTH (type) = cu_header->addr_size;
8536 }
8537 return set_die_type (die, type, cu);
8538 }
8539
8540 static struct type *
8541 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8542 {
8543 struct type *base_type, *cv_type;
8544
8545 base_type = die_type (die, cu);
8546
8547 /* The die_type call above may have already set the type for this DIE. */
8548 cv_type = get_die_type (die, cu);
8549 if (cv_type)
8550 return cv_type;
8551
8552 /* In case the const qualifier is applied to an array type, the element type
8553 is so qualified, not the array type (section 6.7.3 of C99). */
8554 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8555 {
8556 struct type *el_type, *inner_array;
8557
8558 base_type = copy_type (base_type);
8559 inner_array = base_type;
8560
8561 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8562 {
8563 TYPE_TARGET_TYPE (inner_array) =
8564 copy_type (TYPE_TARGET_TYPE (inner_array));
8565 inner_array = TYPE_TARGET_TYPE (inner_array);
8566 }
8567
8568 el_type = TYPE_TARGET_TYPE (inner_array);
8569 TYPE_TARGET_TYPE (inner_array) =
8570 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8571
8572 return set_die_type (die, base_type, cu);
8573 }
8574
8575 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8576 return set_die_type (die, cv_type, cu);
8577 }
8578
8579 static struct type *
8580 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8581 {
8582 struct type *base_type, *cv_type;
8583
8584 base_type = die_type (die, cu);
8585
8586 /* The die_type call above may have already set the type for this DIE. */
8587 cv_type = get_die_type (die, cu);
8588 if (cv_type)
8589 return cv_type;
8590
8591 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8592 return set_die_type (die, cv_type, cu);
8593 }
8594
8595 /* Extract all information from a DW_TAG_string_type DIE and add to
8596 the user defined type vector. It isn't really a user defined type,
8597 but it behaves like one, with other DIE's using an AT_user_def_type
8598 attribute to reference it. */
8599
8600 static struct type *
8601 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8602 {
8603 struct objfile *objfile = cu->objfile;
8604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8605 struct type *type, *range_type, *index_type, *char_type;
8606 struct attribute *attr;
8607 unsigned int length;
8608
8609 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8610 if (attr)
8611 {
8612 length = DW_UNSND (attr);
8613 }
8614 else
8615 {
8616 /* Check for the DW_AT_byte_size attribute. */
8617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8618 if (attr)
8619 {
8620 length = DW_UNSND (attr);
8621 }
8622 else
8623 {
8624 length = 1;
8625 }
8626 }
8627
8628 index_type = objfile_type (objfile)->builtin_int;
8629 range_type = create_range_type (NULL, index_type, 1, length);
8630 char_type = language_string_char_type (cu->language_defn, gdbarch);
8631 type = create_string_type (NULL, char_type, range_type);
8632
8633 return set_die_type (die, type, cu);
8634 }
8635
8636 /* Handle DIES due to C code like:
8637
8638 struct foo
8639 {
8640 int (*funcp)(int a, long l);
8641 int b;
8642 };
8643
8644 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8645
8646 static struct type *
8647 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8648 {
8649 struct objfile *objfile = cu->objfile;
8650 struct type *type; /* Type that this function returns. */
8651 struct type *ftype; /* Function that returns above type. */
8652 struct attribute *attr;
8653
8654 type = die_type (die, cu);
8655
8656 /* The die_type call above may have already set the type for this DIE. */
8657 ftype = get_die_type (die, cu);
8658 if (ftype)
8659 return ftype;
8660
8661 ftype = lookup_function_type (type);
8662
8663 /* All functions in C++, Pascal and Java have prototypes. */
8664 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8665 if ((attr && (DW_UNSND (attr) != 0))
8666 || cu->language == language_cplus
8667 || cu->language == language_java
8668 || cu->language == language_pascal)
8669 TYPE_PROTOTYPED (ftype) = 1;
8670 else if (producer_is_realview (cu->producer))
8671 /* RealView does not emit DW_AT_prototyped. We can not
8672 distinguish prototyped and unprototyped functions; default to
8673 prototyped, since that is more common in modern code (and
8674 RealView warns about unprototyped functions). */
8675 TYPE_PROTOTYPED (ftype) = 1;
8676
8677 /* Store the calling convention in the type if it's available in
8678 the subroutine die. Otherwise set the calling convention to
8679 the default value DW_CC_normal. */
8680 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8681 if (attr)
8682 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8685 else
8686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8687
8688 /* We need to add the subroutine type to the die immediately so
8689 we don't infinitely recurse when dealing with parameters
8690 declared as the same subroutine type. */
8691 set_die_type (die, ftype, cu);
8692
8693 if (die->child != NULL)
8694 {
8695 struct type *void_type = objfile_type (objfile)->builtin_void;
8696 struct die_info *child_die;
8697 int nparams, iparams;
8698
8699 /* Count the number of parameters.
8700 FIXME: GDB currently ignores vararg functions, but knows about
8701 vararg member functions. */
8702 nparams = 0;
8703 child_die = die->child;
8704 while (child_die && child_die->tag)
8705 {
8706 if (child_die->tag == DW_TAG_formal_parameter)
8707 nparams++;
8708 else if (child_die->tag == DW_TAG_unspecified_parameters)
8709 TYPE_VARARGS (ftype) = 1;
8710 child_die = sibling_die (child_die);
8711 }
8712
8713 /* Allocate storage for parameters and fill them in. */
8714 TYPE_NFIELDS (ftype) = nparams;
8715 TYPE_FIELDS (ftype) = (struct field *)
8716 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8717
8718 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8719 even if we error out during the parameters reading below. */
8720 for (iparams = 0; iparams < nparams; iparams++)
8721 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8722
8723 iparams = 0;
8724 child_die = die->child;
8725 while (child_die && child_die->tag)
8726 {
8727 if (child_die->tag == DW_TAG_formal_parameter)
8728 {
8729 struct type *arg_type;
8730
8731 /* DWARF version 2 has no clean way to discern C++
8732 static and non-static member functions. G++ helps
8733 GDB by marking the first parameter for non-static
8734 member functions (which is the this pointer) as
8735 artificial. We pass this information to
8736 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8737
8738 DWARF version 3 added DW_AT_object_pointer, which GCC
8739 4.5 does not yet generate. */
8740 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8741 if (attr)
8742 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8743 else
8744 {
8745 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8746
8747 /* GCC/43521: In java, the formal parameter
8748 "this" is sometimes not marked with DW_AT_artificial. */
8749 if (cu->language == language_java)
8750 {
8751 const char *name = dwarf2_name (child_die, cu);
8752
8753 if (name && !strcmp (name, "this"))
8754 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8755 }
8756 }
8757 arg_type = die_type (child_die, cu);
8758
8759 /* RealView does not mark THIS as const, which the testsuite
8760 expects. GCC marks THIS as const in method definitions,
8761 but not in the class specifications (GCC PR 43053). */
8762 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8763 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8764 {
8765 int is_this = 0;
8766 struct dwarf2_cu *arg_cu = cu;
8767 const char *name = dwarf2_name (child_die, cu);
8768
8769 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8770 if (attr)
8771 {
8772 /* If the compiler emits this, use it. */
8773 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8774 is_this = 1;
8775 }
8776 else if (name && strcmp (name, "this") == 0)
8777 /* Function definitions will have the argument names. */
8778 is_this = 1;
8779 else if (name == NULL && iparams == 0)
8780 /* Declarations may not have the names, so like
8781 elsewhere in GDB, assume an artificial first
8782 argument is "this". */
8783 is_this = 1;
8784
8785 if (is_this)
8786 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8787 arg_type, 0);
8788 }
8789
8790 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8791 iparams++;
8792 }
8793 child_die = sibling_die (child_die);
8794 }
8795 }
8796
8797 return ftype;
8798 }
8799
8800 static struct type *
8801 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8802 {
8803 struct objfile *objfile = cu->objfile;
8804 const char *name = NULL;
8805 struct type *this_type, *target_type;
8806
8807 name = dwarf2_full_name (NULL, die, cu);
8808 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8809 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8810 TYPE_NAME (this_type) = (char *) name;
8811 set_die_type (die, this_type, cu);
8812 target_type = die_type (die, cu);
8813 if (target_type != this_type)
8814 TYPE_TARGET_TYPE (this_type) = target_type;
8815 else
8816 {
8817 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8818 spec and cause infinite loops in GDB. */
8819 complaint (&symfile_complaints,
8820 _("Self-referential DW_TAG_typedef "
8821 "- DIE at 0x%x [in module %s]"),
8822 die->offset, objfile->name);
8823 TYPE_TARGET_TYPE (this_type) = NULL;
8824 }
8825 return this_type;
8826 }
8827
8828 /* Find a representation of a given base type and install
8829 it in the TYPE field of the die. */
8830
8831 static struct type *
8832 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8833 {
8834 struct objfile *objfile = cu->objfile;
8835 struct type *type;
8836 struct attribute *attr;
8837 int encoding = 0, size = 0;
8838 char *name;
8839 enum type_code code = TYPE_CODE_INT;
8840 int type_flags = 0;
8841 struct type *target_type = NULL;
8842
8843 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8844 if (attr)
8845 {
8846 encoding = DW_UNSND (attr);
8847 }
8848 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8849 if (attr)
8850 {
8851 size = DW_UNSND (attr);
8852 }
8853 name = dwarf2_name (die, cu);
8854 if (!name)
8855 {
8856 complaint (&symfile_complaints,
8857 _("DW_AT_name missing from DW_TAG_base_type"));
8858 }
8859
8860 switch (encoding)
8861 {
8862 case DW_ATE_address:
8863 /* Turn DW_ATE_address into a void * pointer. */
8864 code = TYPE_CODE_PTR;
8865 type_flags |= TYPE_FLAG_UNSIGNED;
8866 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8867 break;
8868 case DW_ATE_boolean:
8869 code = TYPE_CODE_BOOL;
8870 type_flags |= TYPE_FLAG_UNSIGNED;
8871 break;
8872 case DW_ATE_complex_float:
8873 code = TYPE_CODE_COMPLEX;
8874 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8875 break;
8876 case DW_ATE_decimal_float:
8877 code = TYPE_CODE_DECFLOAT;
8878 break;
8879 case DW_ATE_float:
8880 code = TYPE_CODE_FLT;
8881 break;
8882 case DW_ATE_signed:
8883 break;
8884 case DW_ATE_unsigned:
8885 type_flags |= TYPE_FLAG_UNSIGNED;
8886 if (cu->language == language_fortran
8887 && name
8888 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8889 code = TYPE_CODE_CHAR;
8890 break;
8891 case DW_ATE_signed_char:
8892 if (cu->language == language_ada || cu->language == language_m2
8893 || cu->language == language_pascal
8894 || cu->language == language_fortran)
8895 code = TYPE_CODE_CHAR;
8896 break;
8897 case DW_ATE_unsigned_char:
8898 if (cu->language == language_ada || cu->language == language_m2
8899 || cu->language == language_pascal
8900 || cu->language == language_fortran)
8901 code = TYPE_CODE_CHAR;
8902 type_flags |= TYPE_FLAG_UNSIGNED;
8903 break;
8904 case DW_ATE_UTF:
8905 /* We just treat this as an integer and then recognize the
8906 type by name elsewhere. */
8907 break;
8908
8909 default:
8910 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8911 dwarf_type_encoding_name (encoding));
8912 break;
8913 }
8914
8915 type = init_type (code, size, type_flags, NULL, objfile);
8916 TYPE_NAME (type) = name;
8917 TYPE_TARGET_TYPE (type) = target_type;
8918
8919 if (name && strcmp (name, "char") == 0)
8920 TYPE_NOSIGN (type) = 1;
8921
8922 return set_die_type (die, type, cu);
8923 }
8924
8925 /* Read the given DW_AT_subrange DIE. */
8926
8927 static struct type *
8928 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8929 {
8930 struct type *base_type;
8931 struct type *range_type;
8932 struct attribute *attr;
8933 LONGEST low = 0;
8934 LONGEST high = -1;
8935 char *name;
8936 LONGEST negative_mask;
8937
8938 base_type = die_type (die, cu);
8939 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8940 check_typedef (base_type);
8941
8942 /* The die_type call above may have already set the type for this DIE. */
8943 range_type = get_die_type (die, cu);
8944 if (range_type)
8945 return range_type;
8946
8947 if (cu->language == language_fortran)
8948 {
8949 /* FORTRAN implies a lower bound of 1, if not given. */
8950 low = 1;
8951 }
8952
8953 /* FIXME: For variable sized arrays either of these could be
8954 a variable rather than a constant value. We'll allow it,
8955 but we don't know how to handle it. */
8956 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8957 if (attr)
8958 low = dwarf2_get_attr_constant_value (attr, 0);
8959
8960 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8961 if (attr)
8962 {
8963 if (attr_form_is_block (attr) || is_ref_attr (attr))
8964 {
8965 /* GCC encodes arrays with unspecified or dynamic length
8966 with a DW_FORM_block1 attribute or a reference attribute.
8967 FIXME: GDB does not yet know how to handle dynamic
8968 arrays properly, treat them as arrays with unspecified
8969 length for now.
8970
8971 FIXME: jimb/2003-09-22: GDB does not really know
8972 how to handle arrays of unspecified length
8973 either; we just represent them as zero-length
8974 arrays. Choose an appropriate upper bound given
8975 the lower bound we've computed above. */
8976 high = low - 1;
8977 }
8978 else
8979 high = dwarf2_get_attr_constant_value (attr, 1);
8980 }
8981 else
8982 {
8983 attr = dwarf2_attr (die, DW_AT_count, cu);
8984 if (attr)
8985 {
8986 int count = dwarf2_get_attr_constant_value (attr, 1);
8987 high = low + count - 1;
8988 }
8989 else
8990 {
8991 /* Unspecified array length. */
8992 high = low - 1;
8993 }
8994 }
8995
8996 /* Dwarf-2 specifications explicitly allows to create subrange types
8997 without specifying a base type.
8998 In that case, the base type must be set to the type of
8999 the lower bound, upper bound or count, in that order, if any of these
9000 three attributes references an object that has a type.
9001 If no base type is found, the Dwarf-2 specifications say that
9002 a signed integer type of size equal to the size of an address should
9003 be used.
9004 For the following C code: `extern char gdb_int [];'
9005 GCC produces an empty range DIE.
9006 FIXME: muller/2010-05-28: Possible references to object for low bound,
9007 high bound or count are not yet handled by this code. */
9008 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9009 {
9010 struct objfile *objfile = cu->objfile;
9011 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9012 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9013 struct type *int_type = objfile_type (objfile)->builtin_int;
9014
9015 /* Test "int", "long int", and "long long int" objfile types,
9016 and select the first one having a size above or equal to the
9017 architecture address size. */
9018 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9019 base_type = int_type;
9020 else
9021 {
9022 int_type = objfile_type (objfile)->builtin_long;
9023 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9024 base_type = int_type;
9025 else
9026 {
9027 int_type = objfile_type (objfile)->builtin_long_long;
9028 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9029 base_type = int_type;
9030 }
9031 }
9032 }
9033
9034 negative_mask =
9035 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9036 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9037 low |= negative_mask;
9038 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9039 high |= negative_mask;
9040
9041 range_type = create_range_type (NULL, base_type, low, high);
9042
9043 /* Mark arrays with dynamic length at least as an array of unspecified
9044 length. GDB could check the boundary but before it gets implemented at
9045 least allow accessing the array elements. */
9046 if (attr && attr_form_is_block (attr))
9047 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9048
9049 /* Ada expects an empty array on no boundary attributes. */
9050 if (attr == NULL && cu->language != language_ada)
9051 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9052
9053 name = dwarf2_name (die, cu);
9054 if (name)
9055 TYPE_NAME (range_type) = name;
9056
9057 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9058 if (attr)
9059 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9060
9061 set_die_type (die, range_type, cu);
9062
9063 /* set_die_type should be already done. */
9064 set_descriptive_type (range_type, die, cu);
9065
9066 return range_type;
9067 }
9068
9069 static struct type *
9070 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9071 {
9072 struct type *type;
9073
9074 /* For now, we only support the C meaning of an unspecified type: void. */
9075
9076 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9077 TYPE_NAME (type) = dwarf2_name (die, cu);
9078
9079 return set_die_type (die, type, cu);
9080 }
9081
9082 /* Trivial hash function for die_info: the hash value of a DIE
9083 is its offset in .debug_info for this objfile. */
9084
9085 static hashval_t
9086 die_hash (const void *item)
9087 {
9088 const struct die_info *die = item;
9089
9090 return die->offset;
9091 }
9092
9093 /* Trivial comparison function for die_info structures: two DIEs
9094 are equal if they have the same offset. */
9095
9096 static int
9097 die_eq (const void *item_lhs, const void *item_rhs)
9098 {
9099 const struct die_info *die_lhs = item_lhs;
9100 const struct die_info *die_rhs = item_rhs;
9101
9102 return die_lhs->offset == die_rhs->offset;
9103 }
9104
9105 /* Read a whole compilation unit into a linked list of dies. */
9106
9107 static struct die_info *
9108 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9109 {
9110 struct die_reader_specs reader_specs;
9111 int read_abbrevs = 0;
9112 struct cleanup *back_to = NULL;
9113 struct die_info *die;
9114
9115 if (cu->dwarf2_abbrevs == NULL)
9116 {
9117 dwarf2_read_abbrevs (cu);
9118 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9119 read_abbrevs = 1;
9120 }
9121
9122 gdb_assert (cu->die_hash == NULL);
9123 cu->die_hash
9124 = htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
9131
9132 init_cu_die_reader (&reader_specs, cu);
9133
9134 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9135
9136 if (read_abbrevs)
9137 do_cleanups (back_to);
9138
9139 return die;
9140 }
9141
9142 /* Main entry point for reading a DIE and all children.
9143 Read the DIE and dump it if requested. */
9144
9145 static struct die_info *
9146 read_die_and_children (const struct die_reader_specs *reader,
9147 gdb_byte *info_ptr,
9148 gdb_byte **new_info_ptr,
9149 struct die_info *parent)
9150 {
9151 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9152 new_info_ptr, parent);
9153
9154 if (dwarf2_die_debug)
9155 {
9156 fprintf_unfiltered (gdb_stdlog,
9157 "\nRead die from %s of %s:\n",
9158 (reader->cu->per_cu->debug_types_section
9159 ? ".debug_types"
9160 : ".debug_info"),
9161 reader->abfd->filename);
9162 dump_die (result, dwarf2_die_debug);
9163 }
9164
9165 return result;
9166 }
9167
9168 /* Read a single die and all its descendents. Set the die's sibling
9169 field to NULL; set other fields in the die correctly, and set all
9170 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9171 location of the info_ptr after reading all of those dies. PARENT
9172 is the parent of the die in question. */
9173
9174 static struct die_info *
9175 read_die_and_children_1 (const struct die_reader_specs *reader,
9176 gdb_byte *info_ptr,
9177 gdb_byte **new_info_ptr,
9178 struct die_info *parent)
9179 {
9180 struct die_info *die;
9181 gdb_byte *cur_ptr;
9182 int has_children;
9183
9184 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9185 if (die == NULL)
9186 {
9187 *new_info_ptr = cur_ptr;
9188 return NULL;
9189 }
9190 store_in_ref_table (die, reader->cu);
9191
9192 if (has_children)
9193 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9194 else
9195 {
9196 die->child = NULL;
9197 *new_info_ptr = cur_ptr;
9198 }
9199
9200 die->sibling = NULL;
9201 die->parent = parent;
9202 return die;
9203 }
9204
9205 /* Read a die, all of its descendents, and all of its siblings; set
9206 all of the fields of all of the dies correctly. Arguments are as
9207 in read_die_and_children. */
9208
9209 static struct die_info *
9210 read_die_and_siblings (const struct die_reader_specs *reader,
9211 gdb_byte *info_ptr,
9212 gdb_byte **new_info_ptr,
9213 struct die_info *parent)
9214 {
9215 struct die_info *first_die, *last_sibling;
9216 gdb_byte *cur_ptr;
9217
9218 cur_ptr = info_ptr;
9219 first_die = last_sibling = NULL;
9220
9221 while (1)
9222 {
9223 struct die_info *die
9224 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9225
9226 if (die == NULL)
9227 {
9228 *new_info_ptr = cur_ptr;
9229 return first_die;
9230 }
9231
9232 if (!first_die)
9233 first_die = die;
9234 else
9235 last_sibling->sibling = die;
9236
9237 last_sibling = die;
9238 }
9239 }
9240
9241 /* Read the die from the .debug_info section buffer. Set DIEP to
9242 point to a newly allocated die with its information, except for its
9243 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9244 whether the die has children or not. */
9245
9246 static gdb_byte *
9247 read_full_die (const struct die_reader_specs *reader,
9248 struct die_info **diep, gdb_byte *info_ptr,
9249 int *has_children)
9250 {
9251 unsigned int abbrev_number, bytes_read, i, offset;
9252 struct abbrev_info *abbrev;
9253 struct die_info *die;
9254 struct dwarf2_cu *cu = reader->cu;
9255 bfd *abfd = reader->abfd;
9256
9257 offset = info_ptr - reader->buffer;
9258 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9259 info_ptr += bytes_read;
9260 if (!abbrev_number)
9261 {
9262 *diep = NULL;
9263 *has_children = 0;
9264 return info_ptr;
9265 }
9266
9267 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9268 if (!abbrev)
9269 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9270 abbrev_number,
9271 bfd_get_filename (abfd));
9272
9273 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9274 die->offset = offset;
9275 die->tag = abbrev->tag;
9276 die->abbrev = abbrev_number;
9277
9278 die->num_attrs = abbrev->num_attrs;
9279
9280 for (i = 0; i < abbrev->num_attrs; ++i)
9281 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9282 abfd, info_ptr, cu);
9283
9284 *diep = die;
9285 *has_children = abbrev->has_children;
9286 return info_ptr;
9287 }
9288
9289 /* In DWARF version 2, the description of the debugging information is
9290 stored in a separate .debug_abbrev section. Before we read any
9291 dies from a section we read in all abbreviations and install them
9292 in a hash table. This function also sets flags in CU describing
9293 the data found in the abbrev table. */
9294
9295 static void
9296 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9297 {
9298 bfd *abfd = cu->objfile->obfd;
9299 struct comp_unit_head *cu_header = &cu->header;
9300 gdb_byte *abbrev_ptr;
9301 struct abbrev_info *cur_abbrev;
9302 unsigned int abbrev_number, bytes_read, abbrev_name;
9303 unsigned int abbrev_form, hash_number;
9304 struct attr_abbrev *cur_attrs;
9305 unsigned int allocated_attrs;
9306
9307 /* Initialize dwarf2 abbrevs. */
9308 obstack_init (&cu->abbrev_obstack);
9309 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9310 (ABBREV_HASH_SIZE
9311 * sizeof (struct abbrev_info *)));
9312 memset (cu->dwarf2_abbrevs, 0,
9313 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9314
9315 dwarf2_read_section (dwarf2_per_objfile->objfile,
9316 &dwarf2_per_objfile->abbrev);
9317 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9318 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9319 abbrev_ptr += bytes_read;
9320
9321 allocated_attrs = ATTR_ALLOC_CHUNK;
9322 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9323
9324 /* Loop until we reach an abbrev number of 0. */
9325 while (abbrev_number)
9326 {
9327 cur_abbrev = dwarf_alloc_abbrev (cu);
9328
9329 /* read in abbrev header */
9330 cur_abbrev->number = abbrev_number;
9331 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9332 abbrev_ptr += bytes_read;
9333 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9334 abbrev_ptr += 1;
9335
9336 if (cur_abbrev->tag == DW_TAG_namespace)
9337 cu->has_namespace_info = 1;
9338
9339 /* now read in declarations */
9340 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9341 abbrev_ptr += bytes_read;
9342 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9343 abbrev_ptr += bytes_read;
9344 while (abbrev_name)
9345 {
9346 if (cur_abbrev->num_attrs == allocated_attrs)
9347 {
9348 allocated_attrs += ATTR_ALLOC_CHUNK;
9349 cur_attrs
9350 = xrealloc (cur_attrs, (allocated_attrs
9351 * sizeof (struct attr_abbrev)));
9352 }
9353
9354 /* Record whether this compilation unit might have
9355 inter-compilation-unit references. If we don't know what form
9356 this attribute will have, then it might potentially be a
9357 DW_FORM_ref_addr, so we conservatively expect inter-CU
9358 references. */
9359
9360 if (abbrev_form == DW_FORM_ref_addr
9361 || abbrev_form == DW_FORM_indirect)
9362 cu->has_form_ref_addr = 1;
9363
9364 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9365 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9366 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9367 abbrev_ptr += bytes_read;
9368 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9369 abbrev_ptr += bytes_read;
9370 }
9371
9372 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9373 (cur_abbrev->num_attrs
9374 * sizeof (struct attr_abbrev)));
9375 memcpy (cur_abbrev->attrs, cur_attrs,
9376 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9377
9378 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9379 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9380 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9381
9382 /* Get next abbreviation.
9383 Under Irix6 the abbreviations for a compilation unit are not
9384 always properly terminated with an abbrev number of 0.
9385 Exit loop if we encounter an abbreviation which we have
9386 already read (which means we are about to read the abbreviations
9387 for the next compile unit) or if the end of the abbreviation
9388 table is reached. */
9389 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9390 >= dwarf2_per_objfile->abbrev.size)
9391 break;
9392 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9393 abbrev_ptr += bytes_read;
9394 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9395 break;
9396 }
9397
9398 xfree (cur_attrs);
9399 }
9400
9401 /* Release the memory used by the abbrev table for a compilation unit. */
9402
9403 static void
9404 dwarf2_free_abbrev_table (void *ptr_to_cu)
9405 {
9406 struct dwarf2_cu *cu = ptr_to_cu;
9407
9408 obstack_free (&cu->abbrev_obstack, NULL);
9409 cu->dwarf2_abbrevs = NULL;
9410 }
9411
9412 /* Lookup an abbrev_info structure in the abbrev hash table. */
9413
9414 static struct abbrev_info *
9415 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9416 {
9417 unsigned int hash_number;
9418 struct abbrev_info *abbrev;
9419
9420 hash_number = number % ABBREV_HASH_SIZE;
9421 abbrev = cu->dwarf2_abbrevs[hash_number];
9422
9423 while (abbrev)
9424 {
9425 if (abbrev->number == number)
9426 return abbrev;
9427 else
9428 abbrev = abbrev->next;
9429 }
9430 return NULL;
9431 }
9432
9433 /* Returns nonzero if TAG represents a type that we might generate a partial
9434 symbol for. */
9435
9436 static int
9437 is_type_tag_for_partial (int tag)
9438 {
9439 switch (tag)
9440 {
9441 #if 0
9442 /* Some types that would be reasonable to generate partial symbols for,
9443 that we don't at present. */
9444 case DW_TAG_array_type:
9445 case DW_TAG_file_type:
9446 case DW_TAG_ptr_to_member_type:
9447 case DW_TAG_set_type:
9448 case DW_TAG_string_type:
9449 case DW_TAG_subroutine_type:
9450 #endif
9451 case DW_TAG_base_type:
9452 case DW_TAG_class_type:
9453 case DW_TAG_interface_type:
9454 case DW_TAG_enumeration_type:
9455 case DW_TAG_structure_type:
9456 case DW_TAG_subrange_type:
9457 case DW_TAG_typedef:
9458 case DW_TAG_union_type:
9459 return 1;
9460 default:
9461 return 0;
9462 }
9463 }
9464
9465 /* Load all DIEs that are interesting for partial symbols into memory. */
9466
9467 static struct partial_die_info *
9468 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9469 int building_psymtab, struct dwarf2_cu *cu)
9470 {
9471 struct objfile *objfile = cu->objfile;
9472 struct partial_die_info *part_die;
9473 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9474 struct abbrev_info *abbrev;
9475 unsigned int bytes_read;
9476 unsigned int load_all = 0;
9477
9478 int nesting_level = 1;
9479
9480 parent_die = NULL;
9481 last_die = NULL;
9482
9483 if (cu->per_cu && cu->per_cu->load_all_dies)
9484 load_all = 1;
9485
9486 cu->partial_dies
9487 = htab_create_alloc_ex (cu->header.length / 12,
9488 partial_die_hash,
9489 partial_die_eq,
9490 NULL,
9491 &cu->comp_unit_obstack,
9492 hashtab_obstack_allocate,
9493 dummy_obstack_deallocate);
9494
9495 part_die = obstack_alloc (&cu->comp_unit_obstack,
9496 sizeof (struct partial_die_info));
9497
9498 while (1)
9499 {
9500 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9501
9502 /* A NULL abbrev means the end of a series of children. */
9503 if (abbrev == NULL)
9504 {
9505 if (--nesting_level == 0)
9506 {
9507 /* PART_DIE was probably the last thing allocated on the
9508 comp_unit_obstack, so we could call obstack_free
9509 here. We don't do that because the waste is small,
9510 and will be cleaned up when we're done with this
9511 compilation unit. This way, we're also more robust
9512 against other users of the comp_unit_obstack. */
9513 return first_die;
9514 }
9515 info_ptr += bytes_read;
9516 last_die = parent_die;
9517 parent_die = parent_die->die_parent;
9518 continue;
9519 }
9520
9521 /* Check for template arguments. We never save these; if
9522 they're seen, we just mark the parent, and go on our way. */
9523 if (parent_die != NULL
9524 && cu->language == language_cplus
9525 && (abbrev->tag == DW_TAG_template_type_param
9526 || abbrev->tag == DW_TAG_template_value_param))
9527 {
9528 parent_die->has_template_arguments = 1;
9529
9530 if (!load_all)
9531 {
9532 /* We don't need a partial DIE for the template argument. */
9533 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9534 cu);
9535 continue;
9536 }
9537 }
9538
9539 /* We only recurse into subprograms looking for template arguments.
9540 Skip their other children. */
9541 if (!load_all
9542 && cu->language == language_cplus
9543 && parent_die != NULL
9544 && parent_die->tag == DW_TAG_subprogram)
9545 {
9546 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9547 continue;
9548 }
9549
9550 /* Check whether this DIE is interesting enough to save. Normally
9551 we would not be interested in members here, but there may be
9552 later variables referencing them via DW_AT_specification (for
9553 static members). */
9554 if (!load_all
9555 && !is_type_tag_for_partial (abbrev->tag)
9556 && abbrev->tag != DW_TAG_constant
9557 && abbrev->tag != DW_TAG_enumerator
9558 && abbrev->tag != DW_TAG_subprogram
9559 && abbrev->tag != DW_TAG_lexical_block
9560 && abbrev->tag != DW_TAG_variable
9561 && abbrev->tag != DW_TAG_namespace
9562 && abbrev->tag != DW_TAG_module
9563 && abbrev->tag != DW_TAG_member)
9564 {
9565 /* Otherwise we skip to the next sibling, if any. */
9566 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9567 continue;
9568 }
9569
9570 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9571 buffer, info_ptr, cu);
9572
9573 /* This two-pass algorithm for processing partial symbols has a
9574 high cost in cache pressure. Thus, handle some simple cases
9575 here which cover the majority of C partial symbols. DIEs
9576 which neither have specification tags in them, nor could have
9577 specification tags elsewhere pointing at them, can simply be
9578 processed and discarded.
9579
9580 This segment is also optional; scan_partial_symbols and
9581 add_partial_symbol will handle these DIEs if we chain
9582 them in normally. When compilers which do not emit large
9583 quantities of duplicate debug information are more common,
9584 this code can probably be removed. */
9585
9586 /* Any complete simple types at the top level (pretty much all
9587 of them, for a language without namespaces), can be processed
9588 directly. */
9589 if (parent_die == NULL
9590 && part_die->has_specification == 0
9591 && part_die->is_declaration == 0
9592 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9593 || part_die->tag == DW_TAG_base_type
9594 || part_die->tag == DW_TAG_subrange_type))
9595 {
9596 if (building_psymtab && part_die->name != NULL)
9597 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9598 VAR_DOMAIN, LOC_TYPEDEF,
9599 &objfile->static_psymbols,
9600 0, (CORE_ADDR) 0, cu->language, objfile);
9601 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9602 continue;
9603 }
9604
9605 /* The exception for DW_TAG_typedef with has_children above is
9606 a workaround of GCC PR debug/47510. In the case of this complaint
9607 type_name_no_tag_or_error will error on such types later.
9608
9609 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9610 it could not find the child DIEs referenced later, this is checked
9611 above. In correct DWARF DW_TAG_typedef should have no children. */
9612
9613 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9614 complaint (&symfile_complaints,
9615 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9616 "- DIE at 0x%x [in module %s]"),
9617 part_die->offset, objfile->name);
9618
9619 /* If we're at the second level, and we're an enumerator, and
9620 our parent has no specification (meaning possibly lives in a
9621 namespace elsewhere), then we can add the partial symbol now
9622 instead of queueing it. */
9623 if (part_die->tag == DW_TAG_enumerator
9624 && parent_die != NULL
9625 && parent_die->die_parent == NULL
9626 && parent_die->tag == DW_TAG_enumeration_type
9627 && parent_die->has_specification == 0)
9628 {
9629 if (part_die->name == NULL)
9630 complaint (&symfile_complaints,
9631 _("malformed enumerator DIE ignored"));
9632 else if (building_psymtab)
9633 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9634 VAR_DOMAIN, LOC_CONST,
9635 (cu->language == language_cplus
9636 || cu->language == language_java)
9637 ? &objfile->global_psymbols
9638 : &objfile->static_psymbols,
9639 0, (CORE_ADDR) 0, cu->language, objfile);
9640
9641 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9642 continue;
9643 }
9644
9645 /* We'll save this DIE so link it in. */
9646 part_die->die_parent = parent_die;
9647 part_die->die_sibling = NULL;
9648 part_die->die_child = NULL;
9649
9650 if (last_die && last_die == parent_die)
9651 last_die->die_child = part_die;
9652 else if (last_die)
9653 last_die->die_sibling = part_die;
9654
9655 last_die = part_die;
9656
9657 if (first_die == NULL)
9658 first_die = part_die;
9659
9660 /* Maybe add the DIE to the hash table. Not all DIEs that we
9661 find interesting need to be in the hash table, because we
9662 also have the parent/sibling/child chains; only those that we
9663 might refer to by offset later during partial symbol reading.
9664
9665 For now this means things that might have be the target of a
9666 DW_AT_specification, DW_AT_abstract_origin, or
9667 DW_AT_extension. DW_AT_extension will refer only to
9668 namespaces; DW_AT_abstract_origin refers to functions (and
9669 many things under the function DIE, but we do not recurse
9670 into function DIEs during partial symbol reading) and
9671 possibly variables as well; DW_AT_specification refers to
9672 declarations. Declarations ought to have the DW_AT_declaration
9673 flag. It happens that GCC forgets to put it in sometimes, but
9674 only for functions, not for types.
9675
9676 Adding more things than necessary to the hash table is harmless
9677 except for the performance cost. Adding too few will result in
9678 wasted time in find_partial_die, when we reread the compilation
9679 unit with load_all_dies set. */
9680
9681 if (load_all
9682 || abbrev->tag == DW_TAG_constant
9683 || abbrev->tag == DW_TAG_subprogram
9684 || abbrev->tag == DW_TAG_variable
9685 || abbrev->tag == DW_TAG_namespace
9686 || part_die->is_declaration)
9687 {
9688 void **slot;
9689
9690 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9691 part_die->offset, INSERT);
9692 *slot = part_die;
9693 }
9694
9695 part_die = obstack_alloc (&cu->comp_unit_obstack,
9696 sizeof (struct partial_die_info));
9697
9698 /* For some DIEs we want to follow their children (if any). For C
9699 we have no reason to follow the children of structures; for other
9700 languages we have to, so that we can get at method physnames
9701 to infer fully qualified class names, for DW_AT_specification,
9702 and for C++ template arguments. For C++, we also look one level
9703 inside functions to find template arguments (if the name of the
9704 function does not already contain the template arguments).
9705
9706 For Ada, we need to scan the children of subprograms and lexical
9707 blocks as well because Ada allows the definition of nested
9708 entities that could be interesting for the debugger, such as
9709 nested subprograms for instance. */
9710 if (last_die->has_children
9711 && (load_all
9712 || last_die->tag == DW_TAG_namespace
9713 || last_die->tag == DW_TAG_module
9714 || last_die->tag == DW_TAG_enumeration_type
9715 || (cu->language == language_cplus
9716 && last_die->tag == DW_TAG_subprogram
9717 && (last_die->name == NULL
9718 || strchr (last_die->name, '<') == NULL))
9719 || (cu->language != language_c
9720 && (last_die->tag == DW_TAG_class_type
9721 || last_die->tag == DW_TAG_interface_type
9722 || last_die->tag == DW_TAG_structure_type
9723 || last_die->tag == DW_TAG_union_type))
9724 || (cu->language == language_ada
9725 && (last_die->tag == DW_TAG_subprogram
9726 || last_die->tag == DW_TAG_lexical_block))))
9727 {
9728 nesting_level++;
9729 parent_die = last_die;
9730 continue;
9731 }
9732
9733 /* Otherwise we skip to the next sibling, if any. */
9734 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9735
9736 /* Back to the top, do it again. */
9737 }
9738 }
9739
9740 /* Read a minimal amount of information into the minimal die structure. */
9741
9742 static gdb_byte *
9743 read_partial_die (struct partial_die_info *part_die,
9744 struct abbrev_info *abbrev,
9745 unsigned int abbrev_len, bfd *abfd,
9746 gdb_byte *buffer, gdb_byte *info_ptr,
9747 struct dwarf2_cu *cu)
9748 {
9749 struct objfile *objfile = cu->objfile;
9750 unsigned int i;
9751 struct attribute attr;
9752 int has_low_pc_attr = 0;
9753 int has_high_pc_attr = 0;
9754
9755 memset (part_die, 0, sizeof (struct partial_die_info));
9756
9757 part_die->offset = info_ptr - buffer;
9758
9759 info_ptr += abbrev_len;
9760
9761 if (abbrev == NULL)
9762 return info_ptr;
9763
9764 part_die->tag = abbrev->tag;
9765 part_die->has_children = abbrev->has_children;
9766
9767 for (i = 0; i < abbrev->num_attrs; ++i)
9768 {
9769 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9770
9771 /* Store the data if it is of an attribute we want to keep in a
9772 partial symbol table. */
9773 switch (attr.name)
9774 {
9775 case DW_AT_name:
9776 switch (part_die->tag)
9777 {
9778 case DW_TAG_compile_unit:
9779 case DW_TAG_type_unit:
9780 /* Compilation units have a DW_AT_name that is a filename, not
9781 a source language identifier. */
9782 case DW_TAG_enumeration_type:
9783 case DW_TAG_enumerator:
9784 /* These tags always have simple identifiers already; no need
9785 to canonicalize them. */
9786 part_die->name = DW_STRING (&attr);
9787 break;
9788 default:
9789 part_die->name
9790 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9791 &objfile->objfile_obstack);
9792 break;
9793 }
9794 break;
9795 case DW_AT_linkage_name:
9796 case DW_AT_MIPS_linkage_name:
9797 /* Note that both forms of linkage name might appear. We
9798 assume they will be the same, and we only store the last
9799 one we see. */
9800 if (cu->language == language_ada)
9801 part_die->name = DW_STRING (&attr);
9802 part_die->linkage_name = DW_STRING (&attr);
9803 break;
9804 case DW_AT_low_pc:
9805 has_low_pc_attr = 1;
9806 part_die->lowpc = DW_ADDR (&attr);
9807 break;
9808 case DW_AT_high_pc:
9809 has_high_pc_attr = 1;
9810 part_die->highpc = DW_ADDR (&attr);
9811 break;
9812 case DW_AT_location:
9813 /* Support the .debug_loc offsets. */
9814 if (attr_form_is_block (&attr))
9815 {
9816 part_die->locdesc = DW_BLOCK (&attr);
9817 }
9818 else if (attr_form_is_section_offset (&attr))
9819 {
9820 dwarf2_complex_location_expr_complaint ();
9821 }
9822 else
9823 {
9824 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9825 "partial symbol information");
9826 }
9827 break;
9828 case DW_AT_external:
9829 part_die->is_external = DW_UNSND (&attr);
9830 break;
9831 case DW_AT_declaration:
9832 part_die->is_declaration = DW_UNSND (&attr);
9833 break;
9834 case DW_AT_type:
9835 part_die->has_type = 1;
9836 break;
9837 case DW_AT_abstract_origin:
9838 case DW_AT_specification:
9839 case DW_AT_extension:
9840 part_die->has_specification = 1;
9841 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9842 break;
9843 case DW_AT_sibling:
9844 /* Ignore absolute siblings, they might point outside of
9845 the current compile unit. */
9846 if (attr.form == DW_FORM_ref_addr)
9847 complaint (&symfile_complaints,
9848 _("ignoring absolute DW_AT_sibling"));
9849 else
9850 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9851 break;
9852 case DW_AT_byte_size:
9853 part_die->has_byte_size = 1;
9854 break;
9855 case DW_AT_calling_convention:
9856 /* DWARF doesn't provide a way to identify a program's source-level
9857 entry point. DW_AT_calling_convention attributes are only meant
9858 to describe functions' calling conventions.
9859
9860 However, because it's a necessary piece of information in
9861 Fortran, and because DW_CC_program is the only piece of debugging
9862 information whose definition refers to a 'main program' at all,
9863 several compilers have begun marking Fortran main programs with
9864 DW_CC_program --- even when those functions use the standard
9865 calling conventions.
9866
9867 So until DWARF specifies a way to provide this information and
9868 compilers pick up the new representation, we'll support this
9869 practice. */
9870 if (DW_UNSND (&attr) == DW_CC_program
9871 && cu->language == language_fortran)
9872 {
9873 set_main_name (part_die->name);
9874
9875 /* As this DIE has a static linkage the name would be difficult
9876 to look up later. */
9877 language_of_main = language_fortran;
9878 }
9879 break;
9880 default:
9881 break;
9882 }
9883 }
9884
9885 if (has_low_pc_attr && has_high_pc_attr)
9886 {
9887 /* When using the GNU linker, .gnu.linkonce. sections are used to
9888 eliminate duplicate copies of functions and vtables and such.
9889 The linker will arbitrarily choose one and discard the others.
9890 The AT_*_pc values for such functions refer to local labels in
9891 these sections. If the section from that file was discarded, the
9892 labels are not in the output, so the relocs get a value of 0.
9893 If this is a discarded function, mark the pc bounds as invalid,
9894 so that GDB will ignore it. */
9895 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9896 {
9897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9898
9899 complaint (&symfile_complaints,
9900 _("DW_AT_low_pc %s is zero "
9901 "for DIE at 0x%x [in module %s]"),
9902 paddress (gdbarch, part_die->lowpc),
9903 part_die->offset, objfile->name);
9904 }
9905 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9906 else if (part_die->lowpc >= part_die->highpc)
9907 {
9908 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9909
9910 complaint (&symfile_complaints,
9911 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9912 "for DIE at 0x%x [in module %s]"),
9913 paddress (gdbarch, part_die->lowpc),
9914 paddress (gdbarch, part_die->highpc),
9915 part_die->offset, objfile->name);
9916 }
9917 else
9918 part_die->has_pc_info = 1;
9919 }
9920
9921 return info_ptr;
9922 }
9923
9924 /* Find a cached partial DIE at OFFSET in CU. */
9925
9926 static struct partial_die_info *
9927 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9928 {
9929 struct partial_die_info *lookup_die = NULL;
9930 struct partial_die_info part_die;
9931
9932 part_die.offset = offset;
9933 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9934
9935 return lookup_die;
9936 }
9937
9938 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9939 except in the case of .debug_types DIEs which do not reference
9940 outside their CU (they do however referencing other types via
9941 DW_FORM_ref_sig8). */
9942
9943 static struct partial_die_info *
9944 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9945 {
9946 struct objfile *objfile = cu->objfile;
9947 struct dwarf2_per_cu_data *per_cu = NULL;
9948 struct partial_die_info *pd = NULL;
9949
9950 if (cu->per_cu->debug_types_section)
9951 {
9952 pd = find_partial_die_in_comp_unit (offset, cu);
9953 if (pd != NULL)
9954 return pd;
9955 goto not_found;
9956 }
9957
9958 if (offset_in_cu_p (&cu->header, offset))
9959 {
9960 pd = find_partial_die_in_comp_unit (offset, cu);
9961 if (pd != NULL)
9962 return pd;
9963 }
9964
9965 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9966
9967 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9968 load_partial_comp_unit (per_cu);
9969
9970 per_cu->cu->last_used = 0;
9971 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9972
9973 if (pd == NULL && per_cu->load_all_dies == 0)
9974 {
9975 struct cleanup *back_to;
9976 struct partial_die_info comp_unit_die;
9977 struct abbrev_info *abbrev;
9978 unsigned int bytes_read;
9979 char *info_ptr;
9980
9981 per_cu->load_all_dies = 1;
9982
9983 /* Re-read the DIEs. */
9984 back_to = make_cleanup (null_cleanup, 0);
9985 if (per_cu->cu->dwarf2_abbrevs == NULL)
9986 {
9987 dwarf2_read_abbrevs (per_cu->cu);
9988 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9989 }
9990 info_ptr = (dwarf2_per_objfile->info.buffer
9991 + per_cu->cu->header.offset
9992 + per_cu->cu->header.first_die_offset);
9993 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9994 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9995 objfile->obfd,
9996 dwarf2_per_objfile->info.buffer, info_ptr,
9997 per_cu->cu);
9998 if (comp_unit_die.has_children)
9999 load_partial_dies (objfile->obfd,
10000 dwarf2_per_objfile->info.buffer, info_ptr,
10001 0, per_cu->cu);
10002 do_cleanups (back_to);
10003
10004 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10005 }
10006
10007 not_found:
10008
10009 if (pd == NULL)
10010 internal_error (__FILE__, __LINE__,
10011 _("could not find partial DIE 0x%x "
10012 "in cache [from module %s]\n"),
10013 offset, bfd_get_filename (objfile->obfd));
10014 return pd;
10015 }
10016
10017 /* See if we can figure out if the class lives in a namespace. We do
10018 this by looking for a member function; its demangled name will
10019 contain namespace info, if there is any. */
10020
10021 static void
10022 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10023 struct dwarf2_cu *cu)
10024 {
10025 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10026 what template types look like, because the demangler
10027 frequently doesn't give the same name as the debug info. We
10028 could fix this by only using the demangled name to get the
10029 prefix (but see comment in read_structure_type). */
10030
10031 struct partial_die_info *real_pdi;
10032 struct partial_die_info *child_pdi;
10033
10034 /* If this DIE (this DIE's specification, if any) has a parent, then
10035 we should not do this. We'll prepend the parent's fully qualified
10036 name when we create the partial symbol. */
10037
10038 real_pdi = struct_pdi;
10039 while (real_pdi->has_specification)
10040 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10041
10042 if (real_pdi->die_parent != NULL)
10043 return;
10044
10045 for (child_pdi = struct_pdi->die_child;
10046 child_pdi != NULL;
10047 child_pdi = child_pdi->die_sibling)
10048 {
10049 if (child_pdi->tag == DW_TAG_subprogram
10050 && child_pdi->linkage_name != NULL)
10051 {
10052 char *actual_class_name
10053 = language_class_name_from_physname (cu->language_defn,
10054 child_pdi->linkage_name);
10055 if (actual_class_name != NULL)
10056 {
10057 struct_pdi->name
10058 = obsavestring (actual_class_name,
10059 strlen (actual_class_name),
10060 &cu->objfile->objfile_obstack);
10061 xfree (actual_class_name);
10062 }
10063 break;
10064 }
10065 }
10066 }
10067
10068 /* Adjust PART_DIE before generating a symbol for it. This function
10069 may set the is_external flag or change the DIE's name. */
10070
10071 static void
10072 fixup_partial_die (struct partial_die_info *part_die,
10073 struct dwarf2_cu *cu)
10074 {
10075 /* Once we've fixed up a die, there's no point in doing so again.
10076 This also avoids a memory leak if we were to call
10077 guess_partial_die_structure_name multiple times. */
10078 if (part_die->fixup_called)
10079 return;
10080
10081 /* If we found a reference attribute and the DIE has no name, try
10082 to find a name in the referred to DIE. */
10083
10084 if (part_die->name == NULL && part_die->has_specification)
10085 {
10086 struct partial_die_info *spec_die;
10087
10088 spec_die = find_partial_die (part_die->spec_offset, cu);
10089
10090 fixup_partial_die (spec_die, cu);
10091
10092 if (spec_die->name)
10093 {
10094 part_die->name = spec_die->name;
10095
10096 /* Copy DW_AT_external attribute if it is set. */
10097 if (spec_die->is_external)
10098 part_die->is_external = spec_die->is_external;
10099 }
10100 }
10101
10102 /* Set default names for some unnamed DIEs. */
10103
10104 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10105 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10106
10107 /* If there is no parent die to provide a namespace, and there are
10108 children, see if we can determine the namespace from their linkage
10109 name.
10110 NOTE: We need to do this even if cu->has_namespace_info != 0.
10111 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10112 if (cu->language == language_cplus
10113 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10114 && part_die->die_parent == NULL
10115 && part_die->has_children
10116 && (part_die->tag == DW_TAG_class_type
10117 || part_die->tag == DW_TAG_structure_type
10118 || part_die->tag == DW_TAG_union_type))
10119 guess_partial_die_structure_name (part_die, cu);
10120
10121 /* GCC might emit a nameless struct or union that has a linkage
10122 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10123 if (part_die->name == NULL
10124 && (part_die->tag == DW_TAG_class_type
10125 || part_die->tag == DW_TAG_interface_type
10126 || part_die->tag == DW_TAG_structure_type
10127 || part_die->tag == DW_TAG_union_type)
10128 && part_die->linkage_name != NULL)
10129 {
10130 char *demangled;
10131
10132 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10133 if (demangled)
10134 {
10135 const char *base;
10136
10137 /* Strip any leading namespaces/classes, keep only the base name.
10138 DW_AT_name for named DIEs does not contain the prefixes. */
10139 base = strrchr (demangled, ':');
10140 if (base && base > demangled && base[-1] == ':')
10141 base++;
10142 else
10143 base = demangled;
10144
10145 part_die->name = obsavestring (base, strlen (base),
10146 &cu->objfile->objfile_obstack);
10147 xfree (demangled);
10148 }
10149 }
10150
10151 part_die->fixup_called = 1;
10152 }
10153
10154 /* Read an attribute value described by an attribute form. */
10155
10156 static gdb_byte *
10157 read_attribute_value (struct attribute *attr, unsigned form,
10158 bfd *abfd, gdb_byte *info_ptr,
10159 struct dwarf2_cu *cu)
10160 {
10161 struct comp_unit_head *cu_header = &cu->header;
10162 unsigned int bytes_read;
10163 struct dwarf_block *blk;
10164
10165 attr->form = form;
10166 switch (form)
10167 {
10168 case DW_FORM_ref_addr:
10169 if (cu->header.version == 2)
10170 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10171 else
10172 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10173 &cu->header, &bytes_read);
10174 info_ptr += bytes_read;
10175 break;
10176 case DW_FORM_addr:
10177 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10178 info_ptr += bytes_read;
10179 break;
10180 case DW_FORM_block2:
10181 blk = dwarf_alloc_block (cu);
10182 blk->size = read_2_bytes (abfd, info_ptr);
10183 info_ptr += 2;
10184 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10185 info_ptr += blk->size;
10186 DW_BLOCK (attr) = blk;
10187 break;
10188 case DW_FORM_block4:
10189 blk = dwarf_alloc_block (cu);
10190 blk->size = read_4_bytes (abfd, info_ptr);
10191 info_ptr += 4;
10192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10193 info_ptr += blk->size;
10194 DW_BLOCK (attr) = blk;
10195 break;
10196 case DW_FORM_data2:
10197 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10198 info_ptr += 2;
10199 break;
10200 case DW_FORM_data4:
10201 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10202 info_ptr += 4;
10203 break;
10204 case DW_FORM_data8:
10205 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10206 info_ptr += 8;
10207 break;
10208 case DW_FORM_sec_offset:
10209 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_string:
10213 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10214 DW_STRING_IS_CANONICAL (attr) = 0;
10215 info_ptr += bytes_read;
10216 break;
10217 case DW_FORM_strp:
10218 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10219 &bytes_read);
10220 DW_STRING_IS_CANONICAL (attr) = 0;
10221 info_ptr += bytes_read;
10222 break;
10223 case DW_FORM_exprloc:
10224 case DW_FORM_block:
10225 blk = dwarf_alloc_block (cu);
10226 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10227 info_ptr += bytes_read;
10228 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10229 info_ptr += blk->size;
10230 DW_BLOCK (attr) = blk;
10231 break;
10232 case DW_FORM_block1:
10233 blk = dwarf_alloc_block (cu);
10234 blk->size = read_1_byte (abfd, info_ptr);
10235 info_ptr += 1;
10236 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10237 info_ptr += blk->size;
10238 DW_BLOCK (attr) = blk;
10239 break;
10240 case DW_FORM_data1:
10241 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10242 info_ptr += 1;
10243 break;
10244 case DW_FORM_flag:
10245 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10246 info_ptr += 1;
10247 break;
10248 case DW_FORM_flag_present:
10249 DW_UNSND (attr) = 1;
10250 break;
10251 case DW_FORM_sdata:
10252 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10253 info_ptr += bytes_read;
10254 break;
10255 case DW_FORM_udata:
10256 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10257 info_ptr += bytes_read;
10258 break;
10259 case DW_FORM_ref1:
10260 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10261 info_ptr += 1;
10262 break;
10263 case DW_FORM_ref2:
10264 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10265 info_ptr += 2;
10266 break;
10267 case DW_FORM_ref4:
10268 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10269 info_ptr += 4;
10270 break;
10271 case DW_FORM_ref8:
10272 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10273 info_ptr += 8;
10274 break;
10275 case DW_FORM_ref_sig8:
10276 /* Convert the signature to something we can record in DW_UNSND
10277 for later lookup.
10278 NOTE: This is NULL if the type wasn't found. */
10279 DW_SIGNATURED_TYPE (attr) =
10280 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10281 info_ptr += 8;
10282 break;
10283 case DW_FORM_ref_udata:
10284 DW_ADDR (attr) = (cu->header.offset
10285 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10286 info_ptr += bytes_read;
10287 break;
10288 case DW_FORM_indirect:
10289 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10290 info_ptr += bytes_read;
10291 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10292 break;
10293 default:
10294 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10295 dwarf_form_name (form),
10296 bfd_get_filename (abfd));
10297 }
10298
10299 /* We have seen instances where the compiler tried to emit a byte
10300 size attribute of -1 which ended up being encoded as an unsigned
10301 0xffffffff. Although 0xffffffff is technically a valid size value,
10302 an object of this size seems pretty unlikely so we can relatively
10303 safely treat these cases as if the size attribute was invalid and
10304 treat them as zero by default. */
10305 if (attr->name == DW_AT_byte_size
10306 && form == DW_FORM_data4
10307 && DW_UNSND (attr) >= 0xffffffff)
10308 {
10309 complaint
10310 (&symfile_complaints,
10311 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10312 hex_string (DW_UNSND (attr)));
10313 DW_UNSND (attr) = 0;
10314 }
10315
10316 return info_ptr;
10317 }
10318
10319 /* Read an attribute described by an abbreviated attribute. */
10320
10321 static gdb_byte *
10322 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10323 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10324 {
10325 attr->name = abbrev->name;
10326 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10327 }
10328
10329 /* Read dwarf information from a buffer. */
10330
10331 static unsigned int
10332 read_1_byte (bfd *abfd, gdb_byte *buf)
10333 {
10334 return bfd_get_8 (abfd, buf);
10335 }
10336
10337 static int
10338 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10339 {
10340 return bfd_get_signed_8 (abfd, buf);
10341 }
10342
10343 static unsigned int
10344 read_2_bytes (bfd *abfd, gdb_byte *buf)
10345 {
10346 return bfd_get_16 (abfd, buf);
10347 }
10348
10349 static int
10350 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10351 {
10352 return bfd_get_signed_16 (abfd, buf);
10353 }
10354
10355 static unsigned int
10356 read_4_bytes (bfd *abfd, gdb_byte *buf)
10357 {
10358 return bfd_get_32 (abfd, buf);
10359 }
10360
10361 static int
10362 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10363 {
10364 return bfd_get_signed_32 (abfd, buf);
10365 }
10366
10367 static ULONGEST
10368 read_8_bytes (bfd *abfd, gdb_byte *buf)
10369 {
10370 return bfd_get_64 (abfd, buf);
10371 }
10372
10373 static CORE_ADDR
10374 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10375 unsigned int *bytes_read)
10376 {
10377 struct comp_unit_head *cu_header = &cu->header;
10378 CORE_ADDR retval = 0;
10379
10380 if (cu_header->signed_addr_p)
10381 {
10382 switch (cu_header->addr_size)
10383 {
10384 case 2:
10385 retval = bfd_get_signed_16 (abfd, buf);
10386 break;
10387 case 4:
10388 retval = bfd_get_signed_32 (abfd, buf);
10389 break;
10390 case 8:
10391 retval = bfd_get_signed_64 (abfd, buf);
10392 break;
10393 default:
10394 internal_error (__FILE__, __LINE__,
10395 _("read_address: bad switch, signed [in module %s]"),
10396 bfd_get_filename (abfd));
10397 }
10398 }
10399 else
10400 {
10401 switch (cu_header->addr_size)
10402 {
10403 case 2:
10404 retval = bfd_get_16 (abfd, buf);
10405 break;
10406 case 4:
10407 retval = bfd_get_32 (abfd, buf);
10408 break;
10409 case 8:
10410 retval = bfd_get_64 (abfd, buf);
10411 break;
10412 default:
10413 internal_error (__FILE__, __LINE__,
10414 _("read_address: bad switch, "
10415 "unsigned [in module %s]"),
10416 bfd_get_filename (abfd));
10417 }
10418 }
10419
10420 *bytes_read = cu_header->addr_size;
10421 return retval;
10422 }
10423
10424 /* Read the initial length from a section. The (draft) DWARF 3
10425 specification allows the initial length to take up either 4 bytes
10426 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10427 bytes describe the length and all offsets will be 8 bytes in length
10428 instead of 4.
10429
10430 An older, non-standard 64-bit format is also handled by this
10431 function. The older format in question stores the initial length
10432 as an 8-byte quantity without an escape value. Lengths greater
10433 than 2^32 aren't very common which means that the initial 4 bytes
10434 is almost always zero. Since a length value of zero doesn't make
10435 sense for the 32-bit format, this initial zero can be considered to
10436 be an escape value which indicates the presence of the older 64-bit
10437 format. As written, the code can't detect (old format) lengths
10438 greater than 4GB. If it becomes necessary to handle lengths
10439 somewhat larger than 4GB, we could allow other small values (such
10440 as the non-sensical values of 1, 2, and 3) to also be used as
10441 escape values indicating the presence of the old format.
10442
10443 The value returned via bytes_read should be used to increment the
10444 relevant pointer after calling read_initial_length().
10445
10446 [ Note: read_initial_length() and read_offset() are based on the
10447 document entitled "DWARF Debugging Information Format", revision
10448 3, draft 8, dated November 19, 2001. This document was obtained
10449 from:
10450
10451 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10452
10453 This document is only a draft and is subject to change. (So beware.)
10454
10455 Details regarding the older, non-standard 64-bit format were
10456 determined empirically by examining 64-bit ELF files produced by
10457 the SGI toolchain on an IRIX 6.5 machine.
10458
10459 - Kevin, July 16, 2002
10460 ] */
10461
10462 static LONGEST
10463 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10464 {
10465 LONGEST length = bfd_get_32 (abfd, buf);
10466
10467 if (length == 0xffffffff)
10468 {
10469 length = bfd_get_64 (abfd, buf + 4);
10470 *bytes_read = 12;
10471 }
10472 else if (length == 0)
10473 {
10474 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10475 length = bfd_get_64 (abfd, buf);
10476 *bytes_read = 8;
10477 }
10478 else
10479 {
10480 *bytes_read = 4;
10481 }
10482
10483 return length;
10484 }
10485
10486 /* Cover function for read_initial_length.
10487 Returns the length of the object at BUF, and stores the size of the
10488 initial length in *BYTES_READ and stores the size that offsets will be in
10489 *OFFSET_SIZE.
10490 If the initial length size is not equivalent to that specified in
10491 CU_HEADER then issue a complaint.
10492 This is useful when reading non-comp-unit headers. */
10493
10494 static LONGEST
10495 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10496 const struct comp_unit_head *cu_header,
10497 unsigned int *bytes_read,
10498 unsigned int *offset_size)
10499 {
10500 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10501
10502 gdb_assert (cu_header->initial_length_size == 4
10503 || cu_header->initial_length_size == 8
10504 || cu_header->initial_length_size == 12);
10505
10506 if (cu_header->initial_length_size != *bytes_read)
10507 complaint (&symfile_complaints,
10508 _("intermixed 32-bit and 64-bit DWARF sections"));
10509
10510 *offset_size = (*bytes_read == 4) ? 4 : 8;
10511 return length;
10512 }
10513
10514 /* Read an offset from the data stream. The size of the offset is
10515 given by cu_header->offset_size. */
10516
10517 static LONGEST
10518 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10519 unsigned int *bytes_read)
10520 {
10521 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10522
10523 *bytes_read = cu_header->offset_size;
10524 return offset;
10525 }
10526
10527 /* Read an offset from the data stream. */
10528
10529 static LONGEST
10530 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10531 {
10532 LONGEST retval = 0;
10533
10534 switch (offset_size)
10535 {
10536 case 4:
10537 retval = bfd_get_32 (abfd, buf);
10538 break;
10539 case 8:
10540 retval = bfd_get_64 (abfd, buf);
10541 break;
10542 default:
10543 internal_error (__FILE__, __LINE__,
10544 _("read_offset_1: bad switch [in module %s]"),
10545 bfd_get_filename (abfd));
10546 }
10547
10548 return retval;
10549 }
10550
10551 static gdb_byte *
10552 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10553 {
10554 /* If the size of a host char is 8 bits, we can return a pointer
10555 to the buffer, otherwise we have to copy the data to a buffer
10556 allocated on the temporary obstack. */
10557 gdb_assert (HOST_CHAR_BIT == 8);
10558 return buf;
10559 }
10560
10561 static char *
10562 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10563 {
10564 /* If the size of a host char is 8 bits, we can return a pointer
10565 to the string, otherwise we have to copy the string to a buffer
10566 allocated on the temporary obstack. */
10567 gdb_assert (HOST_CHAR_BIT == 8);
10568 if (*buf == '\0')
10569 {
10570 *bytes_read_ptr = 1;
10571 return NULL;
10572 }
10573 *bytes_read_ptr = strlen ((char *) buf) + 1;
10574 return (char *) buf;
10575 }
10576
10577 static char *
10578 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10579 {
10580 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10581 if (dwarf2_per_objfile->str.buffer == NULL)
10582 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10583 bfd_get_filename (abfd));
10584 if (str_offset >= dwarf2_per_objfile->str.size)
10585 error (_("DW_FORM_strp pointing outside of "
10586 ".debug_str section [in module %s]"),
10587 bfd_get_filename (abfd));
10588 gdb_assert (HOST_CHAR_BIT == 8);
10589 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10590 return NULL;
10591 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10592 }
10593
10594 static char *
10595 read_indirect_string (bfd *abfd, gdb_byte *buf,
10596 const struct comp_unit_head *cu_header,
10597 unsigned int *bytes_read_ptr)
10598 {
10599 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10600
10601 return read_indirect_string_at_offset (abfd, str_offset);
10602 }
10603
10604 static unsigned long
10605 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10606 {
10607 unsigned long result;
10608 unsigned int num_read;
10609 int i, shift;
10610 unsigned char byte;
10611
10612 result = 0;
10613 shift = 0;
10614 num_read = 0;
10615 i = 0;
10616 while (1)
10617 {
10618 byte = bfd_get_8 (abfd, buf);
10619 buf++;
10620 num_read++;
10621 result |= ((unsigned long)(byte & 127) << shift);
10622 if ((byte & 128) == 0)
10623 {
10624 break;
10625 }
10626 shift += 7;
10627 }
10628 *bytes_read_ptr = num_read;
10629 return result;
10630 }
10631
10632 static long
10633 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10634 {
10635 long result;
10636 int i, shift, num_read;
10637 unsigned char byte;
10638
10639 result = 0;
10640 shift = 0;
10641 num_read = 0;
10642 i = 0;
10643 while (1)
10644 {
10645 byte = bfd_get_8 (abfd, buf);
10646 buf++;
10647 num_read++;
10648 result |= ((long)(byte & 127) << shift);
10649 shift += 7;
10650 if ((byte & 128) == 0)
10651 {
10652 break;
10653 }
10654 }
10655 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10656 result |= -(((long)1) << shift);
10657 *bytes_read_ptr = num_read;
10658 return result;
10659 }
10660
10661 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10662
10663 static gdb_byte *
10664 skip_leb128 (bfd *abfd, gdb_byte *buf)
10665 {
10666 int byte;
10667
10668 while (1)
10669 {
10670 byte = bfd_get_8 (abfd, buf);
10671 buf++;
10672 if ((byte & 128) == 0)
10673 return buf;
10674 }
10675 }
10676
10677 static void
10678 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10679 {
10680 switch (lang)
10681 {
10682 case DW_LANG_C89:
10683 case DW_LANG_C99:
10684 case DW_LANG_C:
10685 cu->language = language_c;
10686 break;
10687 case DW_LANG_C_plus_plus:
10688 cu->language = language_cplus;
10689 break;
10690 case DW_LANG_D:
10691 cu->language = language_d;
10692 break;
10693 case DW_LANG_Fortran77:
10694 case DW_LANG_Fortran90:
10695 case DW_LANG_Fortran95:
10696 cu->language = language_fortran;
10697 break;
10698 case DW_LANG_Mips_Assembler:
10699 cu->language = language_asm;
10700 break;
10701 case DW_LANG_Java:
10702 cu->language = language_java;
10703 break;
10704 case DW_LANG_Ada83:
10705 case DW_LANG_Ada95:
10706 cu->language = language_ada;
10707 break;
10708 case DW_LANG_Modula2:
10709 cu->language = language_m2;
10710 break;
10711 case DW_LANG_Pascal83:
10712 cu->language = language_pascal;
10713 break;
10714 case DW_LANG_ObjC:
10715 cu->language = language_objc;
10716 break;
10717 case DW_LANG_Cobol74:
10718 case DW_LANG_Cobol85:
10719 default:
10720 cu->language = language_minimal;
10721 break;
10722 }
10723 cu->language_defn = language_def (cu->language);
10724 }
10725
10726 /* Return the named attribute or NULL if not there. */
10727
10728 static struct attribute *
10729 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10730 {
10731 unsigned int i;
10732 struct attribute *spec = NULL;
10733
10734 for (i = 0; i < die->num_attrs; ++i)
10735 {
10736 if (die->attrs[i].name == name)
10737 return &die->attrs[i];
10738 if (die->attrs[i].name == DW_AT_specification
10739 || die->attrs[i].name == DW_AT_abstract_origin)
10740 spec = &die->attrs[i];
10741 }
10742
10743 if (spec)
10744 {
10745 die = follow_die_ref (die, spec, &cu);
10746 return dwarf2_attr (die, name, cu);
10747 }
10748
10749 return NULL;
10750 }
10751
10752 /* Return the named attribute or NULL if not there,
10753 but do not follow DW_AT_specification, etc.
10754 This is for use in contexts where we're reading .debug_types dies.
10755 Following DW_AT_specification, DW_AT_abstract_origin will take us
10756 back up the chain, and we want to go down. */
10757
10758 static struct attribute *
10759 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10760 struct dwarf2_cu *cu)
10761 {
10762 unsigned int i;
10763
10764 for (i = 0; i < die->num_attrs; ++i)
10765 if (die->attrs[i].name == name)
10766 return &die->attrs[i];
10767
10768 return NULL;
10769 }
10770
10771 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10772 and holds a non-zero value. This function should only be used for
10773 DW_FORM_flag or DW_FORM_flag_present attributes. */
10774
10775 static int
10776 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10777 {
10778 struct attribute *attr = dwarf2_attr (die, name, cu);
10779
10780 return (attr && DW_UNSND (attr));
10781 }
10782
10783 static int
10784 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10785 {
10786 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10787 which value is non-zero. However, we have to be careful with
10788 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10789 (via dwarf2_flag_true_p) follows this attribute. So we may
10790 end up accidently finding a declaration attribute that belongs
10791 to a different DIE referenced by the specification attribute,
10792 even though the given DIE does not have a declaration attribute. */
10793 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10794 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10795 }
10796
10797 /* Return the die giving the specification for DIE, if there is
10798 one. *SPEC_CU is the CU containing DIE on input, and the CU
10799 containing the return value on output. If there is no
10800 specification, but there is an abstract origin, that is
10801 returned. */
10802
10803 static struct die_info *
10804 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10805 {
10806 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10807 *spec_cu);
10808
10809 if (spec_attr == NULL)
10810 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10811
10812 if (spec_attr == NULL)
10813 return NULL;
10814 else
10815 return follow_die_ref (die, spec_attr, spec_cu);
10816 }
10817
10818 /* Free the line_header structure *LH, and any arrays and strings it
10819 refers to.
10820 NOTE: This is also used as a "cleanup" function. */
10821
10822 static void
10823 free_line_header (struct line_header *lh)
10824 {
10825 if (lh->standard_opcode_lengths)
10826 xfree (lh->standard_opcode_lengths);
10827
10828 /* Remember that all the lh->file_names[i].name pointers are
10829 pointers into debug_line_buffer, and don't need to be freed. */
10830 if (lh->file_names)
10831 xfree (lh->file_names);
10832
10833 /* Similarly for the include directory names. */
10834 if (lh->include_dirs)
10835 xfree (lh->include_dirs);
10836
10837 xfree (lh);
10838 }
10839
10840 /* Add an entry to LH's include directory table. */
10841
10842 static void
10843 add_include_dir (struct line_header *lh, char *include_dir)
10844 {
10845 /* Grow the array if necessary. */
10846 if (lh->include_dirs_size == 0)
10847 {
10848 lh->include_dirs_size = 1; /* for testing */
10849 lh->include_dirs = xmalloc (lh->include_dirs_size
10850 * sizeof (*lh->include_dirs));
10851 }
10852 else if (lh->num_include_dirs >= lh->include_dirs_size)
10853 {
10854 lh->include_dirs_size *= 2;
10855 lh->include_dirs = xrealloc (lh->include_dirs,
10856 (lh->include_dirs_size
10857 * sizeof (*lh->include_dirs)));
10858 }
10859
10860 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10861 }
10862
10863 /* Add an entry to LH's file name table. */
10864
10865 static void
10866 add_file_name (struct line_header *lh,
10867 char *name,
10868 unsigned int dir_index,
10869 unsigned int mod_time,
10870 unsigned int length)
10871 {
10872 struct file_entry *fe;
10873
10874 /* Grow the array if necessary. */
10875 if (lh->file_names_size == 0)
10876 {
10877 lh->file_names_size = 1; /* for testing */
10878 lh->file_names = xmalloc (lh->file_names_size
10879 * sizeof (*lh->file_names));
10880 }
10881 else if (lh->num_file_names >= lh->file_names_size)
10882 {
10883 lh->file_names_size *= 2;
10884 lh->file_names = xrealloc (lh->file_names,
10885 (lh->file_names_size
10886 * sizeof (*lh->file_names)));
10887 }
10888
10889 fe = &lh->file_names[lh->num_file_names++];
10890 fe->name = name;
10891 fe->dir_index = dir_index;
10892 fe->mod_time = mod_time;
10893 fe->length = length;
10894 fe->included_p = 0;
10895 fe->symtab = NULL;
10896 }
10897
10898 /* Read the statement program header starting at OFFSET in
10899 .debug_line, according to the endianness of ABFD. Return a pointer
10900 to a struct line_header, allocated using xmalloc.
10901
10902 NOTE: the strings in the include directory and file name tables of
10903 the returned object point into debug_line_buffer, and must not be
10904 freed. */
10905
10906 static struct line_header *
10907 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10908 struct dwarf2_cu *cu)
10909 {
10910 struct cleanup *back_to;
10911 struct line_header *lh;
10912 gdb_byte *line_ptr;
10913 unsigned int bytes_read, offset_size;
10914 int i;
10915 char *cur_dir, *cur_file;
10916
10917 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10918 if (dwarf2_per_objfile->line.buffer == NULL)
10919 {
10920 complaint (&symfile_complaints, _("missing .debug_line section"));
10921 return 0;
10922 }
10923
10924 /* Make sure that at least there's room for the total_length field.
10925 That could be 12 bytes long, but we're just going to fudge that. */
10926 if (offset + 4 >= dwarf2_per_objfile->line.size)
10927 {
10928 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10929 return 0;
10930 }
10931
10932 lh = xmalloc (sizeof (*lh));
10933 memset (lh, 0, sizeof (*lh));
10934 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10935 (void *) lh);
10936
10937 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10938
10939 /* Read in the header. */
10940 lh->total_length =
10941 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10942 &bytes_read, &offset_size);
10943 line_ptr += bytes_read;
10944 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10945 + dwarf2_per_objfile->line.size))
10946 {
10947 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10948 return 0;
10949 }
10950 lh->statement_program_end = line_ptr + lh->total_length;
10951 lh->version = read_2_bytes (abfd, line_ptr);
10952 line_ptr += 2;
10953 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10954 line_ptr += offset_size;
10955 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10956 line_ptr += 1;
10957 if (lh->version >= 4)
10958 {
10959 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10960 line_ptr += 1;
10961 }
10962 else
10963 lh->maximum_ops_per_instruction = 1;
10964
10965 if (lh->maximum_ops_per_instruction == 0)
10966 {
10967 lh->maximum_ops_per_instruction = 1;
10968 complaint (&symfile_complaints,
10969 _("invalid maximum_ops_per_instruction "
10970 "in `.debug_line' section"));
10971 }
10972
10973 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10974 line_ptr += 1;
10975 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10976 line_ptr += 1;
10977 lh->line_range = read_1_byte (abfd, line_ptr);
10978 line_ptr += 1;
10979 lh->opcode_base = read_1_byte (abfd, line_ptr);
10980 line_ptr += 1;
10981 lh->standard_opcode_lengths
10982 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10983
10984 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10985 for (i = 1; i < lh->opcode_base; ++i)
10986 {
10987 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10988 line_ptr += 1;
10989 }
10990
10991 /* Read directory table. */
10992 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10993 {
10994 line_ptr += bytes_read;
10995 add_include_dir (lh, cur_dir);
10996 }
10997 line_ptr += bytes_read;
10998
10999 /* Read file name table. */
11000 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11001 {
11002 unsigned int dir_index, mod_time, length;
11003
11004 line_ptr += bytes_read;
11005 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11006 line_ptr += bytes_read;
11007 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11008 line_ptr += bytes_read;
11009 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11010 line_ptr += bytes_read;
11011
11012 add_file_name (lh, cur_file, dir_index, mod_time, length);
11013 }
11014 line_ptr += bytes_read;
11015 lh->statement_program_start = line_ptr;
11016
11017 if (line_ptr > (dwarf2_per_objfile->line.buffer
11018 + dwarf2_per_objfile->line.size))
11019 complaint (&symfile_complaints,
11020 _("line number info header doesn't "
11021 "fit in `.debug_line' section"));
11022
11023 discard_cleanups (back_to);
11024 return lh;
11025 }
11026
11027 /* Subroutine of dwarf_decode_lines to simplify it.
11028 Return the file name of the psymtab for included file FILE_INDEX
11029 in line header LH of PST.
11030 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11031 If space for the result is malloc'd, it will be freed by a cleanup.
11032 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11033
11034 static char *
11035 psymtab_include_file_name (const struct line_header *lh, int file_index,
11036 const struct partial_symtab *pst,
11037 const char *comp_dir)
11038 {
11039 const struct file_entry fe = lh->file_names [file_index];
11040 char *include_name = fe.name;
11041 char *include_name_to_compare = include_name;
11042 char *dir_name = NULL;
11043 const char *pst_filename;
11044 char *copied_name = NULL;
11045 int file_is_pst;
11046
11047 if (fe.dir_index)
11048 dir_name = lh->include_dirs[fe.dir_index - 1];
11049
11050 if (!IS_ABSOLUTE_PATH (include_name)
11051 && (dir_name != NULL || comp_dir != NULL))
11052 {
11053 /* Avoid creating a duplicate psymtab for PST.
11054 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11055 Before we do the comparison, however, we need to account
11056 for DIR_NAME and COMP_DIR.
11057 First prepend dir_name (if non-NULL). If we still don't
11058 have an absolute path prepend comp_dir (if non-NULL).
11059 However, the directory we record in the include-file's
11060 psymtab does not contain COMP_DIR (to match the
11061 corresponding symtab(s)).
11062
11063 Example:
11064
11065 bash$ cd /tmp
11066 bash$ gcc -g ./hello.c
11067 include_name = "hello.c"
11068 dir_name = "."
11069 DW_AT_comp_dir = comp_dir = "/tmp"
11070 DW_AT_name = "./hello.c" */
11071
11072 if (dir_name != NULL)
11073 {
11074 include_name = concat (dir_name, SLASH_STRING,
11075 include_name, (char *)NULL);
11076 include_name_to_compare = include_name;
11077 make_cleanup (xfree, include_name);
11078 }
11079 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11080 {
11081 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11082 include_name, (char *)NULL);
11083 }
11084 }
11085
11086 pst_filename = pst->filename;
11087 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11088 {
11089 copied_name = concat (pst->dirname, SLASH_STRING,
11090 pst_filename, (char *)NULL);
11091 pst_filename = copied_name;
11092 }
11093
11094 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11095
11096 if (include_name_to_compare != include_name)
11097 xfree (include_name_to_compare);
11098 if (copied_name != NULL)
11099 xfree (copied_name);
11100
11101 if (file_is_pst)
11102 return NULL;
11103 return include_name;
11104 }
11105
11106 /* Ignore this record_line request. */
11107
11108 static void
11109 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11110 {
11111 return;
11112 }
11113
11114 /* Decode the Line Number Program (LNP) for the given line_header
11115 structure and CU. The actual information extracted and the type
11116 of structures created from the LNP depends on the value of PST.
11117
11118 1. If PST is NULL, then this procedure uses the data from the program
11119 to create all necessary symbol tables, and their linetables.
11120
11121 2. If PST is not NULL, this procedure reads the program to determine
11122 the list of files included by the unit represented by PST, and
11123 builds all the associated partial symbol tables.
11124
11125 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11126 It is used for relative paths in the line table.
11127 NOTE: When processing partial symtabs (pst != NULL),
11128 comp_dir == pst->dirname.
11129
11130 NOTE: It is important that psymtabs have the same file name (via strcmp)
11131 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11132 symtab we don't use it in the name of the psymtabs we create.
11133 E.g. expand_line_sal requires this when finding psymtabs to expand.
11134 A good testcase for this is mb-inline.exp. */
11135
11136 static void
11137 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11138 struct dwarf2_cu *cu, struct partial_symtab *pst)
11139 {
11140 gdb_byte *line_ptr, *extended_end;
11141 gdb_byte *line_end;
11142 unsigned int bytes_read, extended_len;
11143 unsigned char op_code, extended_op, adj_opcode;
11144 CORE_ADDR baseaddr;
11145 struct objfile *objfile = cu->objfile;
11146 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11147 const int decode_for_pst_p = (pst != NULL);
11148 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11149 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11150 = record_line;
11151
11152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11153
11154 line_ptr = lh->statement_program_start;
11155 line_end = lh->statement_program_end;
11156
11157 /* Read the statement sequences until there's nothing left. */
11158 while (line_ptr < line_end)
11159 {
11160 /* state machine registers */
11161 CORE_ADDR address = 0;
11162 unsigned int file = 1;
11163 unsigned int line = 1;
11164 unsigned int column = 0;
11165 int is_stmt = lh->default_is_stmt;
11166 int basic_block = 0;
11167 int end_sequence = 0;
11168 CORE_ADDR addr;
11169 unsigned char op_index = 0;
11170
11171 if (!decode_for_pst_p && lh->num_file_names >= file)
11172 {
11173 /* Start a subfile for the current file of the state machine. */
11174 /* lh->include_dirs and lh->file_names are 0-based, but the
11175 directory and file name numbers in the statement program
11176 are 1-based. */
11177 struct file_entry *fe = &lh->file_names[file - 1];
11178 char *dir = NULL;
11179
11180 if (fe->dir_index)
11181 dir = lh->include_dirs[fe->dir_index - 1];
11182
11183 dwarf2_start_subfile (fe->name, dir, comp_dir);
11184 }
11185
11186 /* Decode the table. */
11187 while (!end_sequence)
11188 {
11189 op_code = read_1_byte (abfd, line_ptr);
11190 line_ptr += 1;
11191 if (line_ptr > line_end)
11192 {
11193 dwarf2_debug_line_missing_end_sequence_complaint ();
11194 break;
11195 }
11196
11197 if (op_code >= lh->opcode_base)
11198 {
11199 /* Special operand. */
11200 adj_opcode = op_code - lh->opcode_base;
11201 address += (((op_index + (adj_opcode / lh->line_range))
11202 / lh->maximum_ops_per_instruction)
11203 * lh->minimum_instruction_length);
11204 op_index = ((op_index + (adj_opcode / lh->line_range))
11205 % lh->maximum_ops_per_instruction);
11206 line += lh->line_base + (adj_opcode % lh->line_range);
11207 if (lh->num_file_names < file || file == 0)
11208 dwarf2_debug_line_missing_file_complaint ();
11209 /* For now we ignore lines not starting on an
11210 instruction boundary. */
11211 else if (op_index == 0)
11212 {
11213 lh->file_names[file - 1].included_p = 1;
11214 if (!decode_for_pst_p && is_stmt)
11215 {
11216 if (last_subfile != current_subfile)
11217 {
11218 addr = gdbarch_addr_bits_remove (gdbarch, address);
11219 if (last_subfile)
11220 (*p_record_line) (last_subfile, 0, addr);
11221 last_subfile = current_subfile;
11222 }
11223 /* Append row to matrix using current values. */
11224 addr = gdbarch_addr_bits_remove (gdbarch, address);
11225 (*p_record_line) (current_subfile, line, addr);
11226 }
11227 }
11228 basic_block = 0;
11229 }
11230 else switch (op_code)
11231 {
11232 case DW_LNS_extended_op:
11233 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11234 &bytes_read);
11235 line_ptr += bytes_read;
11236 extended_end = line_ptr + extended_len;
11237 extended_op = read_1_byte (abfd, line_ptr);
11238 line_ptr += 1;
11239 switch (extended_op)
11240 {
11241 case DW_LNE_end_sequence:
11242 p_record_line = record_line;
11243 end_sequence = 1;
11244 break;
11245 case DW_LNE_set_address:
11246 address = read_address (abfd, line_ptr, cu, &bytes_read);
11247
11248 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11249 {
11250 /* This line table is for a function which has been
11251 GCd by the linker. Ignore it. PR gdb/12528 */
11252
11253 long line_offset
11254 = line_ptr - dwarf2_per_objfile->line.buffer;
11255
11256 complaint (&symfile_complaints,
11257 _(".debug_line address at offset 0x%lx is 0 "
11258 "[in module %s]"),
11259 line_offset, objfile->name);
11260 p_record_line = noop_record_line;
11261 }
11262
11263 op_index = 0;
11264 line_ptr += bytes_read;
11265 address += baseaddr;
11266 break;
11267 case DW_LNE_define_file:
11268 {
11269 char *cur_file;
11270 unsigned int dir_index, mod_time, length;
11271
11272 cur_file = read_direct_string (abfd, line_ptr,
11273 &bytes_read);
11274 line_ptr += bytes_read;
11275 dir_index =
11276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11277 line_ptr += bytes_read;
11278 mod_time =
11279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11280 line_ptr += bytes_read;
11281 length =
11282 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11283 line_ptr += bytes_read;
11284 add_file_name (lh, cur_file, dir_index, mod_time, length);
11285 }
11286 break;
11287 case DW_LNE_set_discriminator:
11288 /* The discriminator is not interesting to the debugger;
11289 just ignore it. */
11290 line_ptr = extended_end;
11291 break;
11292 default:
11293 complaint (&symfile_complaints,
11294 _("mangled .debug_line section"));
11295 return;
11296 }
11297 /* Make sure that we parsed the extended op correctly. If e.g.
11298 we expected a different address size than the producer used,
11299 we may have read the wrong number of bytes. */
11300 if (line_ptr != extended_end)
11301 {
11302 complaint (&symfile_complaints,
11303 _("mangled .debug_line section"));
11304 return;
11305 }
11306 break;
11307 case DW_LNS_copy:
11308 if (lh->num_file_names < file || file == 0)
11309 dwarf2_debug_line_missing_file_complaint ();
11310 else
11311 {
11312 lh->file_names[file - 1].included_p = 1;
11313 if (!decode_for_pst_p && is_stmt)
11314 {
11315 if (last_subfile != current_subfile)
11316 {
11317 addr = gdbarch_addr_bits_remove (gdbarch, address);
11318 if (last_subfile)
11319 (*p_record_line) (last_subfile, 0, addr);
11320 last_subfile = current_subfile;
11321 }
11322 addr = gdbarch_addr_bits_remove (gdbarch, address);
11323 (*p_record_line) (current_subfile, line, addr);
11324 }
11325 }
11326 basic_block = 0;
11327 break;
11328 case DW_LNS_advance_pc:
11329 {
11330 CORE_ADDR adjust
11331 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11332
11333 address += (((op_index + adjust)
11334 / lh->maximum_ops_per_instruction)
11335 * lh->minimum_instruction_length);
11336 op_index = ((op_index + adjust)
11337 % lh->maximum_ops_per_instruction);
11338 line_ptr += bytes_read;
11339 }
11340 break;
11341 case DW_LNS_advance_line:
11342 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11343 line_ptr += bytes_read;
11344 break;
11345 case DW_LNS_set_file:
11346 {
11347 /* The arrays lh->include_dirs and lh->file_names are
11348 0-based, but the directory and file name numbers in
11349 the statement program are 1-based. */
11350 struct file_entry *fe;
11351 char *dir = NULL;
11352
11353 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11354 line_ptr += bytes_read;
11355 if (lh->num_file_names < file || file == 0)
11356 dwarf2_debug_line_missing_file_complaint ();
11357 else
11358 {
11359 fe = &lh->file_names[file - 1];
11360 if (fe->dir_index)
11361 dir = lh->include_dirs[fe->dir_index - 1];
11362 if (!decode_for_pst_p)
11363 {
11364 last_subfile = current_subfile;
11365 dwarf2_start_subfile (fe->name, dir, comp_dir);
11366 }
11367 }
11368 }
11369 break;
11370 case DW_LNS_set_column:
11371 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11372 line_ptr += bytes_read;
11373 break;
11374 case DW_LNS_negate_stmt:
11375 is_stmt = (!is_stmt);
11376 break;
11377 case DW_LNS_set_basic_block:
11378 basic_block = 1;
11379 break;
11380 /* Add to the address register of the state machine the
11381 address increment value corresponding to special opcode
11382 255. I.e., this value is scaled by the minimum
11383 instruction length since special opcode 255 would have
11384 scaled the increment. */
11385 case DW_LNS_const_add_pc:
11386 {
11387 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11388
11389 address += (((op_index + adjust)
11390 / lh->maximum_ops_per_instruction)
11391 * lh->minimum_instruction_length);
11392 op_index = ((op_index + adjust)
11393 % lh->maximum_ops_per_instruction);
11394 }
11395 break;
11396 case DW_LNS_fixed_advance_pc:
11397 address += read_2_bytes (abfd, line_ptr);
11398 op_index = 0;
11399 line_ptr += 2;
11400 break;
11401 default:
11402 {
11403 /* Unknown standard opcode, ignore it. */
11404 int i;
11405
11406 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11407 {
11408 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11409 line_ptr += bytes_read;
11410 }
11411 }
11412 }
11413 }
11414 if (lh->num_file_names < file || file == 0)
11415 dwarf2_debug_line_missing_file_complaint ();
11416 else
11417 {
11418 lh->file_names[file - 1].included_p = 1;
11419 if (!decode_for_pst_p)
11420 {
11421 addr = gdbarch_addr_bits_remove (gdbarch, address);
11422 (*p_record_line) (current_subfile, 0, addr);
11423 }
11424 }
11425 }
11426
11427 if (decode_for_pst_p)
11428 {
11429 int file_index;
11430
11431 /* Now that we're done scanning the Line Header Program, we can
11432 create the psymtab of each included file. */
11433 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11434 if (lh->file_names[file_index].included_p == 1)
11435 {
11436 char *include_name =
11437 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11438 if (include_name != NULL)
11439 dwarf2_create_include_psymtab (include_name, pst, objfile);
11440 }
11441 }
11442 else
11443 {
11444 /* Make sure a symtab is created for every file, even files
11445 which contain only variables (i.e. no code with associated
11446 line numbers). */
11447
11448 int i;
11449 struct file_entry *fe;
11450
11451 for (i = 0; i < lh->num_file_names; i++)
11452 {
11453 char *dir = NULL;
11454
11455 fe = &lh->file_names[i];
11456 if (fe->dir_index)
11457 dir = lh->include_dirs[fe->dir_index - 1];
11458 dwarf2_start_subfile (fe->name, dir, comp_dir);
11459
11460 /* Skip the main file; we don't need it, and it must be
11461 allocated last, so that it will show up before the
11462 non-primary symtabs in the objfile's symtab list. */
11463 if (current_subfile == first_subfile)
11464 continue;
11465
11466 if (current_subfile->symtab == NULL)
11467 current_subfile->symtab = allocate_symtab (current_subfile->name,
11468 objfile);
11469 fe->symtab = current_subfile->symtab;
11470 }
11471 }
11472 }
11473
11474 /* Start a subfile for DWARF. FILENAME is the name of the file and
11475 DIRNAME the name of the source directory which contains FILENAME
11476 or NULL if not known. COMP_DIR is the compilation directory for the
11477 linetable's compilation unit or NULL if not known.
11478 This routine tries to keep line numbers from identical absolute and
11479 relative file names in a common subfile.
11480
11481 Using the `list' example from the GDB testsuite, which resides in
11482 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11483 of /srcdir/list0.c yields the following debugging information for list0.c:
11484
11485 DW_AT_name: /srcdir/list0.c
11486 DW_AT_comp_dir: /compdir
11487 files.files[0].name: list0.h
11488 files.files[0].dir: /srcdir
11489 files.files[1].name: list0.c
11490 files.files[1].dir: /srcdir
11491
11492 The line number information for list0.c has to end up in a single
11493 subfile, so that `break /srcdir/list0.c:1' works as expected.
11494 start_subfile will ensure that this happens provided that we pass the
11495 concatenation of files.files[1].dir and files.files[1].name as the
11496 subfile's name. */
11497
11498 static void
11499 dwarf2_start_subfile (char *filename, const char *dirname,
11500 const char *comp_dir)
11501 {
11502 char *fullname;
11503
11504 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11505 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11506 second argument to start_subfile. To be consistent, we do the
11507 same here. In order not to lose the line information directory,
11508 we concatenate it to the filename when it makes sense.
11509 Note that the Dwarf3 standard says (speaking of filenames in line
11510 information): ``The directory index is ignored for file names
11511 that represent full path names''. Thus ignoring dirname in the
11512 `else' branch below isn't an issue. */
11513
11514 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11515 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11516 else
11517 fullname = filename;
11518
11519 start_subfile (fullname, comp_dir);
11520
11521 if (fullname != filename)
11522 xfree (fullname);
11523 }
11524
11525 static void
11526 var_decode_location (struct attribute *attr, struct symbol *sym,
11527 struct dwarf2_cu *cu)
11528 {
11529 struct objfile *objfile = cu->objfile;
11530 struct comp_unit_head *cu_header = &cu->header;
11531
11532 /* NOTE drow/2003-01-30: There used to be a comment and some special
11533 code here to turn a symbol with DW_AT_external and a
11534 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11535 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11536 with some versions of binutils) where shared libraries could have
11537 relocations against symbols in their debug information - the
11538 minimal symbol would have the right address, but the debug info
11539 would not. It's no longer necessary, because we will explicitly
11540 apply relocations when we read in the debug information now. */
11541
11542 /* A DW_AT_location attribute with no contents indicates that a
11543 variable has been optimized away. */
11544 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11545 {
11546 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11547 return;
11548 }
11549
11550 /* Handle one degenerate form of location expression specially, to
11551 preserve GDB's previous behavior when section offsets are
11552 specified. If this is just a DW_OP_addr then mark this symbol
11553 as LOC_STATIC. */
11554
11555 if (attr_form_is_block (attr)
11556 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11557 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11558 {
11559 unsigned int dummy;
11560
11561 SYMBOL_VALUE_ADDRESS (sym) =
11562 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11563 SYMBOL_CLASS (sym) = LOC_STATIC;
11564 fixup_symbol_section (sym, objfile);
11565 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11566 SYMBOL_SECTION (sym));
11567 return;
11568 }
11569
11570 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11571 expression evaluator, and use LOC_COMPUTED only when necessary
11572 (i.e. when the value of a register or memory location is
11573 referenced, or a thread-local block, etc.). Then again, it might
11574 not be worthwhile. I'm assuming that it isn't unless performance
11575 or memory numbers show me otherwise. */
11576
11577 dwarf2_symbol_mark_computed (attr, sym, cu);
11578 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11579
11580 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11581 cu->has_loclist = 1;
11582 }
11583
11584 /* Given a pointer to a DWARF information entry, figure out if we need
11585 to make a symbol table entry for it, and if so, create a new entry
11586 and return a pointer to it.
11587 If TYPE is NULL, determine symbol type from the die, otherwise
11588 used the passed type.
11589 If SPACE is not NULL, use it to hold the new symbol. If it is
11590 NULL, allocate a new symbol on the objfile's obstack. */
11591
11592 static struct symbol *
11593 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11594 struct symbol *space)
11595 {
11596 struct objfile *objfile = cu->objfile;
11597 struct symbol *sym = NULL;
11598 char *name;
11599 struct attribute *attr = NULL;
11600 struct attribute *attr2 = NULL;
11601 CORE_ADDR baseaddr;
11602 struct pending **list_to_add = NULL;
11603
11604 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11605
11606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11607
11608 name = dwarf2_name (die, cu);
11609 if (name)
11610 {
11611 const char *linkagename;
11612 int suppress_add = 0;
11613
11614 if (space)
11615 sym = space;
11616 else
11617 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11618 OBJSTAT (objfile, n_syms++);
11619
11620 /* Cache this symbol's name and the name's demangled form (if any). */
11621 SYMBOL_SET_LANGUAGE (sym, cu->language);
11622 linkagename = dwarf2_physname (name, die, cu);
11623 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11624
11625 /* Fortran does not have mangling standard and the mangling does differ
11626 between gfortran, iFort etc. */
11627 if (cu->language == language_fortran
11628 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11629 symbol_set_demangled_name (&(sym->ginfo),
11630 (char *) dwarf2_full_name (name, die, cu),
11631 NULL);
11632
11633 /* Default assumptions.
11634 Use the passed type or decode it from the die. */
11635 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11636 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11637 if (type != NULL)
11638 SYMBOL_TYPE (sym) = type;
11639 else
11640 SYMBOL_TYPE (sym) = die_type (die, cu);
11641 attr = dwarf2_attr (die,
11642 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11643 cu);
11644 if (attr)
11645 {
11646 SYMBOL_LINE (sym) = DW_UNSND (attr);
11647 }
11648
11649 attr = dwarf2_attr (die,
11650 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11651 cu);
11652 if (attr)
11653 {
11654 int file_index = DW_UNSND (attr);
11655
11656 if (cu->line_header == NULL
11657 || file_index > cu->line_header->num_file_names)
11658 complaint (&symfile_complaints,
11659 _("file index out of range"));
11660 else if (file_index > 0)
11661 {
11662 struct file_entry *fe;
11663
11664 fe = &cu->line_header->file_names[file_index - 1];
11665 SYMBOL_SYMTAB (sym) = fe->symtab;
11666 }
11667 }
11668
11669 switch (die->tag)
11670 {
11671 case DW_TAG_label:
11672 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11673 if (attr)
11674 {
11675 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11676 }
11677 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11678 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11679 SYMBOL_CLASS (sym) = LOC_LABEL;
11680 add_symbol_to_list (sym, cu->list_in_scope);
11681 break;
11682 case DW_TAG_subprogram:
11683 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11684 finish_block. */
11685 SYMBOL_CLASS (sym) = LOC_BLOCK;
11686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11687 if ((attr2 && (DW_UNSND (attr2) != 0))
11688 || cu->language == language_ada)
11689 {
11690 /* Subprograms marked external are stored as a global symbol.
11691 Ada subprograms, whether marked external or not, are always
11692 stored as a global symbol, because we want to be able to
11693 access them globally. For instance, we want to be able
11694 to break on a nested subprogram without having to
11695 specify the context. */
11696 list_to_add = &global_symbols;
11697 }
11698 else
11699 {
11700 list_to_add = cu->list_in_scope;
11701 }
11702 break;
11703 case DW_TAG_inlined_subroutine:
11704 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11705 finish_block. */
11706 SYMBOL_CLASS (sym) = LOC_BLOCK;
11707 SYMBOL_INLINED (sym) = 1;
11708 /* Do not add the symbol to any lists. It will be found via
11709 BLOCK_FUNCTION from the blockvector. */
11710 break;
11711 case DW_TAG_template_value_param:
11712 suppress_add = 1;
11713 /* Fall through. */
11714 case DW_TAG_constant:
11715 case DW_TAG_variable:
11716 case DW_TAG_member:
11717 /* Compilation with minimal debug info may result in
11718 variables with missing type entries. Change the
11719 misleading `void' type to something sensible. */
11720 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11721 SYMBOL_TYPE (sym)
11722 = objfile_type (objfile)->nodebug_data_symbol;
11723
11724 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11725 /* In the case of DW_TAG_member, we should only be called for
11726 static const members. */
11727 if (die->tag == DW_TAG_member)
11728 {
11729 /* dwarf2_add_field uses die_is_declaration,
11730 so we do the same. */
11731 gdb_assert (die_is_declaration (die, cu));
11732 gdb_assert (attr);
11733 }
11734 if (attr)
11735 {
11736 dwarf2_const_value (attr, sym, cu);
11737 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11738 if (!suppress_add)
11739 {
11740 if (attr2 && (DW_UNSND (attr2) != 0))
11741 list_to_add = &global_symbols;
11742 else
11743 list_to_add = cu->list_in_scope;
11744 }
11745 break;
11746 }
11747 attr = dwarf2_attr (die, DW_AT_location, cu);
11748 if (attr)
11749 {
11750 var_decode_location (attr, sym, cu);
11751 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11752 if (SYMBOL_CLASS (sym) == LOC_STATIC
11753 && SYMBOL_VALUE_ADDRESS (sym) == 0
11754 && !dwarf2_per_objfile->has_section_at_zero)
11755 {
11756 /* When a static variable is eliminated by the linker,
11757 the corresponding debug information is not stripped
11758 out, but the variable address is set to null;
11759 do not add such variables into symbol table. */
11760 }
11761 else if (attr2 && (DW_UNSND (attr2) != 0))
11762 {
11763 /* Workaround gfortran PR debug/40040 - it uses
11764 DW_AT_location for variables in -fPIC libraries which may
11765 get overriden by other libraries/executable and get
11766 a different address. Resolve it by the minimal symbol
11767 which may come from inferior's executable using copy
11768 relocation. Make this workaround only for gfortran as for
11769 other compilers GDB cannot guess the minimal symbol
11770 Fortran mangling kind. */
11771 if (cu->language == language_fortran && die->parent
11772 && die->parent->tag == DW_TAG_module
11773 && cu->producer
11774 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11775 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11776
11777 /* A variable with DW_AT_external is never static,
11778 but it may be block-scoped. */
11779 list_to_add = (cu->list_in_scope == &file_symbols
11780 ? &global_symbols : cu->list_in_scope);
11781 }
11782 else
11783 list_to_add = cu->list_in_scope;
11784 }
11785 else
11786 {
11787 /* We do not know the address of this symbol.
11788 If it is an external symbol and we have type information
11789 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11790 The address of the variable will then be determined from
11791 the minimal symbol table whenever the variable is
11792 referenced. */
11793 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11794 if (attr2 && (DW_UNSND (attr2) != 0)
11795 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11796 {
11797 /* A variable with DW_AT_external is never static, but it
11798 may be block-scoped. */
11799 list_to_add = (cu->list_in_scope == &file_symbols
11800 ? &global_symbols : cu->list_in_scope);
11801
11802 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11803 }
11804 else if (!die_is_declaration (die, cu))
11805 {
11806 /* Use the default LOC_OPTIMIZED_OUT class. */
11807 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11808 if (!suppress_add)
11809 list_to_add = cu->list_in_scope;
11810 }
11811 }
11812 break;
11813 case DW_TAG_formal_parameter:
11814 /* If we are inside a function, mark this as an argument. If
11815 not, we might be looking at an argument to an inlined function
11816 when we do not have enough information to show inlined frames;
11817 pretend it's a local variable in that case so that the user can
11818 still see it. */
11819 if (context_stack_depth > 0
11820 && context_stack[context_stack_depth - 1].name != NULL)
11821 SYMBOL_IS_ARGUMENT (sym) = 1;
11822 attr = dwarf2_attr (die, DW_AT_location, cu);
11823 if (attr)
11824 {
11825 var_decode_location (attr, sym, cu);
11826 }
11827 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11828 if (attr)
11829 {
11830 dwarf2_const_value (attr, sym, cu);
11831 }
11832
11833 list_to_add = cu->list_in_scope;
11834 break;
11835 case DW_TAG_unspecified_parameters:
11836 /* From varargs functions; gdb doesn't seem to have any
11837 interest in this information, so just ignore it for now.
11838 (FIXME?) */
11839 break;
11840 case DW_TAG_template_type_param:
11841 suppress_add = 1;
11842 /* Fall through. */
11843 case DW_TAG_class_type:
11844 case DW_TAG_interface_type:
11845 case DW_TAG_structure_type:
11846 case DW_TAG_union_type:
11847 case DW_TAG_set_type:
11848 case DW_TAG_enumeration_type:
11849 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11850 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11851
11852 {
11853 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11854 really ever be static objects: otherwise, if you try
11855 to, say, break of a class's method and you're in a file
11856 which doesn't mention that class, it won't work unless
11857 the check for all static symbols in lookup_symbol_aux
11858 saves you. See the OtherFileClass tests in
11859 gdb.c++/namespace.exp. */
11860
11861 if (!suppress_add)
11862 {
11863 list_to_add = (cu->list_in_scope == &file_symbols
11864 && (cu->language == language_cplus
11865 || cu->language == language_java)
11866 ? &global_symbols : cu->list_in_scope);
11867
11868 /* The semantics of C++ state that "struct foo {
11869 ... }" also defines a typedef for "foo". A Java
11870 class declaration also defines a typedef for the
11871 class. */
11872 if (cu->language == language_cplus
11873 || cu->language == language_java
11874 || cu->language == language_ada)
11875 {
11876 /* The symbol's name is already allocated along
11877 with this objfile, so we don't need to
11878 duplicate it for the type. */
11879 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11880 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11881 }
11882 }
11883 }
11884 break;
11885 case DW_TAG_typedef:
11886 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11887 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11888 list_to_add = cu->list_in_scope;
11889 break;
11890 case DW_TAG_base_type:
11891 case DW_TAG_subrange_type:
11892 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11893 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11894 list_to_add = cu->list_in_scope;
11895 break;
11896 case DW_TAG_enumerator:
11897 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11898 if (attr)
11899 {
11900 dwarf2_const_value (attr, sym, cu);
11901 }
11902 {
11903 /* NOTE: carlton/2003-11-10: See comment above in the
11904 DW_TAG_class_type, etc. block. */
11905
11906 list_to_add = (cu->list_in_scope == &file_symbols
11907 && (cu->language == language_cplus
11908 || cu->language == language_java)
11909 ? &global_symbols : cu->list_in_scope);
11910 }
11911 break;
11912 case DW_TAG_namespace:
11913 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11914 list_to_add = &global_symbols;
11915 break;
11916 default:
11917 /* Not a tag we recognize. Hopefully we aren't processing
11918 trash data, but since we must specifically ignore things
11919 we don't recognize, there is nothing else we should do at
11920 this point. */
11921 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11922 dwarf_tag_name (die->tag));
11923 break;
11924 }
11925
11926 if (suppress_add)
11927 {
11928 sym->hash_next = objfile->template_symbols;
11929 objfile->template_symbols = sym;
11930 list_to_add = NULL;
11931 }
11932
11933 if (list_to_add != NULL)
11934 add_symbol_to_list (sym, list_to_add);
11935
11936 /* For the benefit of old versions of GCC, check for anonymous
11937 namespaces based on the demangled name. */
11938 if (!processing_has_namespace_info
11939 && cu->language == language_cplus)
11940 cp_scan_for_anonymous_namespaces (sym, objfile);
11941 }
11942 return (sym);
11943 }
11944
11945 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11946
11947 static struct symbol *
11948 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11949 {
11950 return new_symbol_full (die, type, cu, NULL);
11951 }
11952
11953 /* Given an attr with a DW_FORM_dataN value in host byte order,
11954 zero-extend it as appropriate for the symbol's type. The DWARF
11955 standard (v4) is not entirely clear about the meaning of using
11956 DW_FORM_dataN for a constant with a signed type, where the type is
11957 wider than the data. The conclusion of a discussion on the DWARF
11958 list was that this is unspecified. We choose to always zero-extend
11959 because that is the interpretation long in use by GCC. */
11960
11961 static gdb_byte *
11962 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11963 const char *name, struct obstack *obstack,
11964 struct dwarf2_cu *cu, long *value, int bits)
11965 {
11966 struct objfile *objfile = cu->objfile;
11967 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11968 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11969 LONGEST l = DW_UNSND (attr);
11970
11971 if (bits < sizeof (*value) * 8)
11972 {
11973 l &= ((LONGEST) 1 << bits) - 1;
11974 *value = l;
11975 }
11976 else if (bits == sizeof (*value) * 8)
11977 *value = l;
11978 else
11979 {
11980 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11981 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11982 return bytes;
11983 }
11984
11985 return NULL;
11986 }
11987
11988 /* Read a constant value from an attribute. Either set *VALUE, or if
11989 the value does not fit in *VALUE, set *BYTES - either already
11990 allocated on the objfile obstack, or newly allocated on OBSTACK,
11991 or, set *BATON, if we translated the constant to a location
11992 expression. */
11993
11994 static void
11995 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11996 const char *name, struct obstack *obstack,
11997 struct dwarf2_cu *cu,
11998 long *value, gdb_byte **bytes,
11999 struct dwarf2_locexpr_baton **baton)
12000 {
12001 struct objfile *objfile = cu->objfile;
12002 struct comp_unit_head *cu_header = &cu->header;
12003 struct dwarf_block *blk;
12004 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12005 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12006
12007 *value = 0;
12008 *bytes = NULL;
12009 *baton = NULL;
12010
12011 switch (attr->form)
12012 {
12013 case DW_FORM_addr:
12014 {
12015 gdb_byte *data;
12016
12017 if (TYPE_LENGTH (type) != cu_header->addr_size)
12018 dwarf2_const_value_length_mismatch_complaint (name,
12019 cu_header->addr_size,
12020 TYPE_LENGTH (type));
12021 /* Symbols of this form are reasonably rare, so we just
12022 piggyback on the existing location code rather than writing
12023 a new implementation of symbol_computed_ops. */
12024 *baton = obstack_alloc (&objfile->objfile_obstack,
12025 sizeof (struct dwarf2_locexpr_baton));
12026 (*baton)->per_cu = cu->per_cu;
12027 gdb_assert ((*baton)->per_cu);
12028
12029 (*baton)->size = 2 + cu_header->addr_size;
12030 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12031 (*baton)->data = data;
12032
12033 data[0] = DW_OP_addr;
12034 store_unsigned_integer (&data[1], cu_header->addr_size,
12035 byte_order, DW_ADDR (attr));
12036 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12037 }
12038 break;
12039 case DW_FORM_string:
12040 case DW_FORM_strp:
12041 /* DW_STRING is already allocated on the objfile obstack, point
12042 directly to it. */
12043 *bytes = (gdb_byte *) DW_STRING (attr);
12044 break;
12045 case DW_FORM_block1:
12046 case DW_FORM_block2:
12047 case DW_FORM_block4:
12048 case DW_FORM_block:
12049 case DW_FORM_exprloc:
12050 blk = DW_BLOCK (attr);
12051 if (TYPE_LENGTH (type) != blk->size)
12052 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12053 TYPE_LENGTH (type));
12054 *bytes = blk->data;
12055 break;
12056
12057 /* The DW_AT_const_value attributes are supposed to carry the
12058 symbol's value "represented as it would be on the target
12059 architecture." By the time we get here, it's already been
12060 converted to host endianness, so we just need to sign- or
12061 zero-extend it as appropriate. */
12062 case DW_FORM_data1:
12063 *bytes = dwarf2_const_value_data (attr, type, name,
12064 obstack, cu, value, 8);
12065 break;
12066 case DW_FORM_data2:
12067 *bytes = dwarf2_const_value_data (attr, type, name,
12068 obstack, cu, value, 16);
12069 break;
12070 case DW_FORM_data4:
12071 *bytes = dwarf2_const_value_data (attr, type, name,
12072 obstack, cu, value, 32);
12073 break;
12074 case DW_FORM_data8:
12075 *bytes = dwarf2_const_value_data (attr, type, name,
12076 obstack, cu, value, 64);
12077 break;
12078
12079 case DW_FORM_sdata:
12080 *value = DW_SND (attr);
12081 break;
12082
12083 case DW_FORM_udata:
12084 *value = DW_UNSND (attr);
12085 break;
12086
12087 default:
12088 complaint (&symfile_complaints,
12089 _("unsupported const value attribute form: '%s'"),
12090 dwarf_form_name (attr->form));
12091 *value = 0;
12092 break;
12093 }
12094 }
12095
12096
12097 /* Copy constant value from an attribute to a symbol. */
12098
12099 static void
12100 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12101 struct dwarf2_cu *cu)
12102 {
12103 struct objfile *objfile = cu->objfile;
12104 struct comp_unit_head *cu_header = &cu->header;
12105 long value;
12106 gdb_byte *bytes;
12107 struct dwarf2_locexpr_baton *baton;
12108
12109 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12110 SYMBOL_PRINT_NAME (sym),
12111 &objfile->objfile_obstack, cu,
12112 &value, &bytes, &baton);
12113
12114 if (baton != NULL)
12115 {
12116 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12117 SYMBOL_LOCATION_BATON (sym) = baton;
12118 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12119 }
12120 else if (bytes != NULL)
12121 {
12122 SYMBOL_VALUE_BYTES (sym) = bytes;
12123 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12124 }
12125 else
12126 {
12127 SYMBOL_VALUE (sym) = value;
12128 SYMBOL_CLASS (sym) = LOC_CONST;
12129 }
12130 }
12131
12132 /* Return the type of the die in question using its DW_AT_type attribute. */
12133
12134 static struct type *
12135 die_type (struct die_info *die, struct dwarf2_cu *cu)
12136 {
12137 struct attribute *type_attr;
12138
12139 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12140 if (!type_attr)
12141 {
12142 /* A missing DW_AT_type represents a void type. */
12143 return objfile_type (cu->objfile)->builtin_void;
12144 }
12145
12146 return lookup_die_type (die, type_attr, cu);
12147 }
12148
12149 /* True iff CU's producer generates GNAT Ada auxiliary information
12150 that allows to find parallel types through that information instead
12151 of having to do expensive parallel lookups by type name. */
12152
12153 static int
12154 need_gnat_info (struct dwarf2_cu *cu)
12155 {
12156 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12157 of GNAT produces this auxiliary information, without any indication
12158 that it is produced. Part of enhancing the FSF version of GNAT
12159 to produce that information will be to put in place an indicator
12160 that we can use in order to determine whether the descriptive type
12161 info is available or not. One suggestion that has been made is
12162 to use a new attribute, attached to the CU die. For now, assume
12163 that the descriptive type info is not available. */
12164 return 0;
12165 }
12166
12167 /* Return the auxiliary type of the die in question using its
12168 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12169 attribute is not present. */
12170
12171 static struct type *
12172 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12173 {
12174 struct attribute *type_attr;
12175
12176 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12177 if (!type_attr)
12178 return NULL;
12179
12180 return lookup_die_type (die, type_attr, cu);
12181 }
12182
12183 /* If DIE has a descriptive_type attribute, then set the TYPE's
12184 descriptive type accordingly. */
12185
12186 static void
12187 set_descriptive_type (struct type *type, struct die_info *die,
12188 struct dwarf2_cu *cu)
12189 {
12190 struct type *descriptive_type = die_descriptive_type (die, cu);
12191
12192 if (descriptive_type)
12193 {
12194 ALLOCATE_GNAT_AUX_TYPE (type);
12195 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12196 }
12197 }
12198
12199 /* Return the containing type of the die in question using its
12200 DW_AT_containing_type attribute. */
12201
12202 static struct type *
12203 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12204 {
12205 struct attribute *type_attr;
12206
12207 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12208 if (!type_attr)
12209 error (_("Dwarf Error: Problem turning containing type into gdb type "
12210 "[in module %s]"), cu->objfile->name);
12211
12212 return lookup_die_type (die, type_attr, cu);
12213 }
12214
12215 /* Look up the type of DIE in CU using its type attribute ATTR.
12216 If there is no type substitute an error marker. */
12217
12218 static struct type *
12219 lookup_die_type (struct die_info *die, struct attribute *attr,
12220 struct dwarf2_cu *cu)
12221 {
12222 struct objfile *objfile = cu->objfile;
12223 struct type *this_type;
12224
12225 /* First see if we have it cached. */
12226
12227 if (is_ref_attr (attr))
12228 {
12229 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12230
12231 this_type = get_die_type_at_offset (offset, cu->per_cu);
12232 }
12233 else if (attr->form == DW_FORM_ref_sig8)
12234 {
12235 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12236 struct dwarf2_cu *sig_cu;
12237 unsigned int offset;
12238
12239 /* sig_type will be NULL if the signatured type is missing from
12240 the debug info. */
12241 if (sig_type == NULL)
12242 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12243 "at 0x%x [in module %s]"),
12244 die->offset, objfile->name);
12245
12246 gdb_assert (sig_type->per_cu.debug_types_section);
12247 offset = sig_type->per_cu.offset + sig_type->type_offset;
12248 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12249 }
12250 else
12251 {
12252 dump_die_for_error (die);
12253 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12254 dwarf_attr_name (attr->name), objfile->name);
12255 }
12256
12257 /* If not cached we need to read it in. */
12258
12259 if (this_type == NULL)
12260 {
12261 struct die_info *type_die;
12262 struct dwarf2_cu *type_cu = cu;
12263
12264 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12265 /* If the type is cached, we should have found it above. */
12266 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12267 this_type = read_type_die_1 (type_die, type_cu);
12268 }
12269
12270 /* If we still don't have a type use an error marker. */
12271
12272 if (this_type == NULL)
12273 {
12274 char *message, *saved;
12275
12276 /* read_type_die already issued a complaint. */
12277 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12278 objfile->name,
12279 cu->header.offset,
12280 die->offset);
12281 saved = obstack_copy0 (&objfile->objfile_obstack,
12282 message, strlen (message));
12283 xfree (message);
12284
12285 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12286 }
12287
12288 return this_type;
12289 }
12290
12291 /* Return the type in DIE, CU.
12292 Returns NULL for invalid types.
12293
12294 This first does a lookup in the appropriate type_hash table,
12295 and only reads the die in if necessary.
12296
12297 NOTE: This can be called when reading in partial or full symbols. */
12298
12299 static struct type *
12300 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12301 {
12302 struct type *this_type;
12303
12304 this_type = get_die_type (die, cu);
12305 if (this_type)
12306 return this_type;
12307
12308 return read_type_die_1 (die, cu);
12309 }
12310
12311 /* Read the type in DIE, CU.
12312 Returns NULL for invalid types. */
12313
12314 static struct type *
12315 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12316 {
12317 struct type *this_type = NULL;
12318
12319 switch (die->tag)
12320 {
12321 case DW_TAG_class_type:
12322 case DW_TAG_interface_type:
12323 case DW_TAG_structure_type:
12324 case DW_TAG_union_type:
12325 this_type = read_structure_type (die, cu);
12326 break;
12327 case DW_TAG_enumeration_type:
12328 this_type = read_enumeration_type (die, cu);
12329 break;
12330 case DW_TAG_subprogram:
12331 case DW_TAG_subroutine_type:
12332 case DW_TAG_inlined_subroutine:
12333 this_type = read_subroutine_type (die, cu);
12334 break;
12335 case DW_TAG_array_type:
12336 this_type = read_array_type (die, cu);
12337 break;
12338 case DW_TAG_set_type:
12339 this_type = read_set_type (die, cu);
12340 break;
12341 case DW_TAG_pointer_type:
12342 this_type = read_tag_pointer_type (die, cu);
12343 break;
12344 case DW_TAG_ptr_to_member_type:
12345 this_type = read_tag_ptr_to_member_type (die, cu);
12346 break;
12347 case DW_TAG_reference_type:
12348 this_type = read_tag_reference_type (die, cu);
12349 break;
12350 case DW_TAG_const_type:
12351 this_type = read_tag_const_type (die, cu);
12352 break;
12353 case DW_TAG_volatile_type:
12354 this_type = read_tag_volatile_type (die, cu);
12355 break;
12356 case DW_TAG_string_type:
12357 this_type = read_tag_string_type (die, cu);
12358 break;
12359 case DW_TAG_typedef:
12360 this_type = read_typedef (die, cu);
12361 break;
12362 case DW_TAG_subrange_type:
12363 this_type = read_subrange_type (die, cu);
12364 break;
12365 case DW_TAG_base_type:
12366 this_type = read_base_type (die, cu);
12367 break;
12368 case DW_TAG_unspecified_type:
12369 this_type = read_unspecified_type (die, cu);
12370 break;
12371 case DW_TAG_namespace:
12372 this_type = read_namespace_type (die, cu);
12373 break;
12374 case DW_TAG_module:
12375 this_type = read_module_type (die, cu);
12376 break;
12377 default:
12378 complaint (&symfile_complaints,
12379 _("unexpected tag in read_type_die: '%s'"),
12380 dwarf_tag_name (die->tag));
12381 break;
12382 }
12383
12384 return this_type;
12385 }
12386
12387 /* See if we can figure out if the class lives in a namespace. We do
12388 this by looking for a member function; its demangled name will
12389 contain namespace info, if there is any.
12390 Return the computed name or NULL.
12391 Space for the result is allocated on the objfile's obstack.
12392 This is the full-die version of guess_partial_die_structure_name.
12393 In this case we know DIE has no useful parent. */
12394
12395 static char *
12396 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12397 {
12398 struct die_info *spec_die;
12399 struct dwarf2_cu *spec_cu;
12400 struct die_info *child;
12401
12402 spec_cu = cu;
12403 spec_die = die_specification (die, &spec_cu);
12404 if (spec_die != NULL)
12405 {
12406 die = spec_die;
12407 cu = spec_cu;
12408 }
12409
12410 for (child = die->child;
12411 child != NULL;
12412 child = child->sibling)
12413 {
12414 if (child->tag == DW_TAG_subprogram)
12415 {
12416 struct attribute *attr;
12417
12418 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12419 if (attr == NULL)
12420 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12421 if (attr != NULL)
12422 {
12423 char *actual_name
12424 = language_class_name_from_physname (cu->language_defn,
12425 DW_STRING (attr));
12426 char *name = NULL;
12427
12428 if (actual_name != NULL)
12429 {
12430 char *die_name = dwarf2_name (die, cu);
12431
12432 if (die_name != NULL
12433 && strcmp (die_name, actual_name) != 0)
12434 {
12435 /* Strip off the class name from the full name.
12436 We want the prefix. */
12437 int die_name_len = strlen (die_name);
12438 int actual_name_len = strlen (actual_name);
12439
12440 /* Test for '::' as a sanity check. */
12441 if (actual_name_len > die_name_len + 2
12442 && actual_name[actual_name_len
12443 - die_name_len - 1] == ':')
12444 name =
12445 obsavestring (actual_name,
12446 actual_name_len - die_name_len - 2,
12447 &cu->objfile->objfile_obstack);
12448 }
12449 }
12450 xfree (actual_name);
12451 return name;
12452 }
12453 }
12454 }
12455
12456 return NULL;
12457 }
12458
12459 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12460 prefix part in such case. See
12461 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12462
12463 static char *
12464 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12465 {
12466 struct attribute *attr;
12467 char *base;
12468
12469 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12470 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12471 return NULL;
12472
12473 attr = dwarf2_attr (die, DW_AT_name, cu);
12474 if (attr != NULL && DW_STRING (attr) != NULL)
12475 return NULL;
12476
12477 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12478 if (attr == NULL)
12479 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12480 if (attr == NULL || DW_STRING (attr) == NULL)
12481 return NULL;
12482
12483 /* dwarf2_name had to be already called. */
12484 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12485
12486 /* Strip the base name, keep any leading namespaces/classes. */
12487 base = strrchr (DW_STRING (attr), ':');
12488 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12489 return "";
12490
12491 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12492 &cu->objfile->objfile_obstack);
12493 }
12494
12495 /* Return the name of the namespace/class that DIE is defined within,
12496 or "" if we can't tell. The caller should not xfree the result.
12497
12498 For example, if we're within the method foo() in the following
12499 code:
12500
12501 namespace N {
12502 class C {
12503 void foo () {
12504 }
12505 };
12506 }
12507
12508 then determine_prefix on foo's die will return "N::C". */
12509
12510 static char *
12511 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12512 {
12513 struct die_info *parent, *spec_die;
12514 struct dwarf2_cu *spec_cu;
12515 struct type *parent_type;
12516 char *retval;
12517
12518 if (cu->language != language_cplus && cu->language != language_java
12519 && cu->language != language_fortran)
12520 return "";
12521
12522 retval = anonymous_struct_prefix (die, cu);
12523 if (retval)
12524 return retval;
12525
12526 /* We have to be careful in the presence of DW_AT_specification.
12527 For example, with GCC 3.4, given the code
12528
12529 namespace N {
12530 void foo() {
12531 // Definition of N::foo.
12532 }
12533 }
12534
12535 then we'll have a tree of DIEs like this:
12536
12537 1: DW_TAG_compile_unit
12538 2: DW_TAG_namespace // N
12539 3: DW_TAG_subprogram // declaration of N::foo
12540 4: DW_TAG_subprogram // definition of N::foo
12541 DW_AT_specification // refers to die #3
12542
12543 Thus, when processing die #4, we have to pretend that we're in
12544 the context of its DW_AT_specification, namely the contex of die
12545 #3. */
12546 spec_cu = cu;
12547 spec_die = die_specification (die, &spec_cu);
12548 if (spec_die == NULL)
12549 parent = die->parent;
12550 else
12551 {
12552 parent = spec_die->parent;
12553 cu = spec_cu;
12554 }
12555
12556 if (parent == NULL)
12557 return "";
12558 else if (parent->building_fullname)
12559 {
12560 const char *name;
12561 const char *parent_name;
12562
12563 /* It has been seen on RealView 2.2 built binaries,
12564 DW_TAG_template_type_param types actually _defined_ as
12565 children of the parent class:
12566
12567 enum E {};
12568 template class <class Enum> Class{};
12569 Class<enum E> class_e;
12570
12571 1: DW_TAG_class_type (Class)
12572 2: DW_TAG_enumeration_type (E)
12573 3: DW_TAG_enumerator (enum1:0)
12574 3: DW_TAG_enumerator (enum2:1)
12575 ...
12576 2: DW_TAG_template_type_param
12577 DW_AT_type DW_FORM_ref_udata (E)
12578
12579 Besides being broken debug info, it can put GDB into an
12580 infinite loop. Consider:
12581
12582 When we're building the full name for Class<E>, we'll start
12583 at Class, and go look over its template type parameters,
12584 finding E. We'll then try to build the full name of E, and
12585 reach here. We're now trying to build the full name of E,
12586 and look over the parent DIE for containing scope. In the
12587 broken case, if we followed the parent DIE of E, we'd again
12588 find Class, and once again go look at its template type
12589 arguments, etc., etc. Simply don't consider such parent die
12590 as source-level parent of this die (it can't be, the language
12591 doesn't allow it), and break the loop here. */
12592 name = dwarf2_name (die, cu);
12593 parent_name = dwarf2_name (parent, cu);
12594 complaint (&symfile_complaints,
12595 _("template param type '%s' defined within parent '%s'"),
12596 name ? name : "<unknown>",
12597 parent_name ? parent_name : "<unknown>");
12598 return "";
12599 }
12600 else
12601 switch (parent->tag)
12602 {
12603 case DW_TAG_namespace:
12604 parent_type = read_type_die (parent, cu);
12605 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12606 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12607 Work around this problem here. */
12608 if (cu->language == language_cplus
12609 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12610 return "";
12611 /* We give a name to even anonymous namespaces. */
12612 return TYPE_TAG_NAME (parent_type);
12613 case DW_TAG_class_type:
12614 case DW_TAG_interface_type:
12615 case DW_TAG_structure_type:
12616 case DW_TAG_union_type:
12617 case DW_TAG_module:
12618 parent_type = read_type_die (parent, cu);
12619 if (TYPE_TAG_NAME (parent_type) != NULL)
12620 return TYPE_TAG_NAME (parent_type);
12621 else
12622 /* An anonymous structure is only allowed non-static data
12623 members; no typedefs, no member functions, et cetera.
12624 So it does not need a prefix. */
12625 return "";
12626 case DW_TAG_compile_unit:
12627 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12628 if (cu->language == language_cplus
12629 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12630 && die->child != NULL
12631 && (die->tag == DW_TAG_class_type
12632 || die->tag == DW_TAG_structure_type
12633 || die->tag == DW_TAG_union_type))
12634 {
12635 char *name = guess_full_die_structure_name (die, cu);
12636 if (name != NULL)
12637 return name;
12638 }
12639 return "";
12640 default:
12641 return determine_prefix (parent, cu);
12642 }
12643 }
12644
12645 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12646 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12647 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12648 an obconcat, otherwise allocate storage for the result. The CU argument is
12649 used to determine the language and hence, the appropriate separator. */
12650
12651 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12652
12653 static char *
12654 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12655 int physname, struct dwarf2_cu *cu)
12656 {
12657 const char *lead = "";
12658 const char *sep;
12659
12660 if (suffix == NULL || suffix[0] == '\0'
12661 || prefix == NULL || prefix[0] == '\0')
12662 sep = "";
12663 else if (cu->language == language_java)
12664 sep = ".";
12665 else if (cu->language == language_fortran && physname)
12666 {
12667 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12668 DW_AT_MIPS_linkage_name is preferred and used instead. */
12669
12670 lead = "__";
12671 sep = "_MOD_";
12672 }
12673 else
12674 sep = "::";
12675
12676 if (prefix == NULL)
12677 prefix = "";
12678 if (suffix == NULL)
12679 suffix = "";
12680
12681 if (obs == NULL)
12682 {
12683 char *retval
12684 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12685
12686 strcpy (retval, lead);
12687 strcat (retval, prefix);
12688 strcat (retval, sep);
12689 strcat (retval, suffix);
12690 return retval;
12691 }
12692 else
12693 {
12694 /* We have an obstack. */
12695 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12696 }
12697 }
12698
12699 /* Return sibling of die, NULL if no sibling. */
12700
12701 static struct die_info *
12702 sibling_die (struct die_info *die)
12703 {
12704 return die->sibling;
12705 }
12706
12707 /* Get name of a die, return NULL if not found. */
12708
12709 static char *
12710 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12711 struct obstack *obstack)
12712 {
12713 if (name && cu->language == language_cplus)
12714 {
12715 char *canon_name = cp_canonicalize_string (name);
12716
12717 if (canon_name != NULL)
12718 {
12719 if (strcmp (canon_name, name) != 0)
12720 name = obsavestring (canon_name, strlen (canon_name),
12721 obstack);
12722 xfree (canon_name);
12723 }
12724 }
12725
12726 return name;
12727 }
12728
12729 /* Get name of a die, return NULL if not found. */
12730
12731 static char *
12732 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12733 {
12734 struct attribute *attr;
12735
12736 attr = dwarf2_attr (die, DW_AT_name, cu);
12737 if ((!attr || !DW_STRING (attr))
12738 && die->tag != DW_TAG_class_type
12739 && die->tag != DW_TAG_interface_type
12740 && die->tag != DW_TAG_structure_type
12741 && die->tag != DW_TAG_union_type)
12742 return NULL;
12743
12744 switch (die->tag)
12745 {
12746 case DW_TAG_compile_unit:
12747 /* Compilation units have a DW_AT_name that is a filename, not
12748 a source language identifier. */
12749 case DW_TAG_enumeration_type:
12750 case DW_TAG_enumerator:
12751 /* These tags always have simple identifiers already; no need
12752 to canonicalize them. */
12753 return DW_STRING (attr);
12754
12755 case DW_TAG_subprogram:
12756 /* Java constructors will all be named "<init>", so return
12757 the class name when we see this special case. */
12758 if (cu->language == language_java
12759 && DW_STRING (attr) != NULL
12760 && strcmp (DW_STRING (attr), "<init>") == 0)
12761 {
12762 struct dwarf2_cu *spec_cu = cu;
12763 struct die_info *spec_die;
12764
12765 /* GCJ will output '<init>' for Java constructor names.
12766 For this special case, return the name of the parent class. */
12767
12768 /* GCJ may output suprogram DIEs with AT_specification set.
12769 If so, use the name of the specified DIE. */
12770 spec_die = die_specification (die, &spec_cu);
12771 if (spec_die != NULL)
12772 return dwarf2_name (spec_die, spec_cu);
12773
12774 do
12775 {
12776 die = die->parent;
12777 if (die->tag == DW_TAG_class_type)
12778 return dwarf2_name (die, cu);
12779 }
12780 while (die->tag != DW_TAG_compile_unit);
12781 }
12782 break;
12783
12784 case DW_TAG_class_type:
12785 case DW_TAG_interface_type:
12786 case DW_TAG_structure_type:
12787 case DW_TAG_union_type:
12788 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12789 structures or unions. These were of the form "._%d" in GCC 4.1,
12790 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12791 and GCC 4.4. We work around this problem by ignoring these. */
12792 if (attr && DW_STRING (attr)
12793 && (strncmp (DW_STRING (attr), "._", 2) == 0
12794 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12795 return NULL;
12796
12797 /* GCC might emit a nameless typedef that has a linkage name. See
12798 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12799 if (!attr || DW_STRING (attr) == NULL)
12800 {
12801 char *demangled = NULL;
12802
12803 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12804 if (attr == NULL)
12805 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12806
12807 if (attr == NULL || DW_STRING (attr) == NULL)
12808 return NULL;
12809
12810 /* Avoid demangling DW_STRING (attr) the second time on a second
12811 call for the same DIE. */
12812 if (!DW_STRING_IS_CANONICAL (attr))
12813 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12814
12815 if (demangled)
12816 {
12817 char *base;
12818
12819 /* FIXME: we already did this for the partial symbol... */
12820 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12821 &cu->objfile->objfile_obstack);
12822 DW_STRING_IS_CANONICAL (attr) = 1;
12823 xfree (demangled);
12824
12825 /* Strip any leading namespaces/classes, keep only the base name.
12826 DW_AT_name for named DIEs does not contain the prefixes. */
12827 base = strrchr (DW_STRING (attr), ':');
12828 if (base && base > DW_STRING (attr) && base[-1] == ':')
12829 return &base[1];
12830 else
12831 return DW_STRING (attr);
12832 }
12833 }
12834 break;
12835
12836 default:
12837 break;
12838 }
12839
12840 if (!DW_STRING_IS_CANONICAL (attr))
12841 {
12842 DW_STRING (attr)
12843 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12844 &cu->objfile->objfile_obstack);
12845 DW_STRING_IS_CANONICAL (attr) = 1;
12846 }
12847 return DW_STRING (attr);
12848 }
12849
12850 /* Return the die that this die in an extension of, or NULL if there
12851 is none. *EXT_CU is the CU containing DIE on input, and the CU
12852 containing the return value on output. */
12853
12854 static struct die_info *
12855 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12856 {
12857 struct attribute *attr;
12858
12859 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12860 if (attr == NULL)
12861 return NULL;
12862
12863 return follow_die_ref (die, attr, ext_cu);
12864 }
12865
12866 /* Convert a DIE tag into its string name. */
12867
12868 static char *
12869 dwarf_tag_name (unsigned tag)
12870 {
12871 switch (tag)
12872 {
12873 case DW_TAG_padding:
12874 return "DW_TAG_padding";
12875 case DW_TAG_array_type:
12876 return "DW_TAG_array_type";
12877 case DW_TAG_class_type:
12878 return "DW_TAG_class_type";
12879 case DW_TAG_entry_point:
12880 return "DW_TAG_entry_point";
12881 case DW_TAG_enumeration_type:
12882 return "DW_TAG_enumeration_type";
12883 case DW_TAG_formal_parameter:
12884 return "DW_TAG_formal_parameter";
12885 case DW_TAG_imported_declaration:
12886 return "DW_TAG_imported_declaration";
12887 case DW_TAG_label:
12888 return "DW_TAG_label";
12889 case DW_TAG_lexical_block:
12890 return "DW_TAG_lexical_block";
12891 case DW_TAG_member:
12892 return "DW_TAG_member";
12893 case DW_TAG_pointer_type:
12894 return "DW_TAG_pointer_type";
12895 case DW_TAG_reference_type:
12896 return "DW_TAG_reference_type";
12897 case DW_TAG_compile_unit:
12898 return "DW_TAG_compile_unit";
12899 case DW_TAG_string_type:
12900 return "DW_TAG_string_type";
12901 case DW_TAG_structure_type:
12902 return "DW_TAG_structure_type";
12903 case DW_TAG_subroutine_type:
12904 return "DW_TAG_subroutine_type";
12905 case DW_TAG_typedef:
12906 return "DW_TAG_typedef";
12907 case DW_TAG_union_type:
12908 return "DW_TAG_union_type";
12909 case DW_TAG_unspecified_parameters:
12910 return "DW_TAG_unspecified_parameters";
12911 case DW_TAG_variant:
12912 return "DW_TAG_variant";
12913 case DW_TAG_common_block:
12914 return "DW_TAG_common_block";
12915 case DW_TAG_common_inclusion:
12916 return "DW_TAG_common_inclusion";
12917 case DW_TAG_inheritance:
12918 return "DW_TAG_inheritance";
12919 case DW_TAG_inlined_subroutine:
12920 return "DW_TAG_inlined_subroutine";
12921 case DW_TAG_module:
12922 return "DW_TAG_module";
12923 case DW_TAG_ptr_to_member_type:
12924 return "DW_TAG_ptr_to_member_type";
12925 case DW_TAG_set_type:
12926 return "DW_TAG_set_type";
12927 case DW_TAG_subrange_type:
12928 return "DW_TAG_subrange_type";
12929 case DW_TAG_with_stmt:
12930 return "DW_TAG_with_stmt";
12931 case DW_TAG_access_declaration:
12932 return "DW_TAG_access_declaration";
12933 case DW_TAG_base_type:
12934 return "DW_TAG_base_type";
12935 case DW_TAG_catch_block:
12936 return "DW_TAG_catch_block";
12937 case DW_TAG_const_type:
12938 return "DW_TAG_const_type";
12939 case DW_TAG_constant:
12940 return "DW_TAG_constant";
12941 case DW_TAG_enumerator:
12942 return "DW_TAG_enumerator";
12943 case DW_TAG_file_type:
12944 return "DW_TAG_file_type";
12945 case DW_TAG_friend:
12946 return "DW_TAG_friend";
12947 case DW_TAG_namelist:
12948 return "DW_TAG_namelist";
12949 case DW_TAG_namelist_item:
12950 return "DW_TAG_namelist_item";
12951 case DW_TAG_packed_type:
12952 return "DW_TAG_packed_type";
12953 case DW_TAG_subprogram:
12954 return "DW_TAG_subprogram";
12955 case DW_TAG_template_type_param:
12956 return "DW_TAG_template_type_param";
12957 case DW_TAG_template_value_param:
12958 return "DW_TAG_template_value_param";
12959 case DW_TAG_thrown_type:
12960 return "DW_TAG_thrown_type";
12961 case DW_TAG_try_block:
12962 return "DW_TAG_try_block";
12963 case DW_TAG_variant_part:
12964 return "DW_TAG_variant_part";
12965 case DW_TAG_variable:
12966 return "DW_TAG_variable";
12967 case DW_TAG_volatile_type:
12968 return "DW_TAG_volatile_type";
12969 case DW_TAG_dwarf_procedure:
12970 return "DW_TAG_dwarf_procedure";
12971 case DW_TAG_restrict_type:
12972 return "DW_TAG_restrict_type";
12973 case DW_TAG_interface_type:
12974 return "DW_TAG_interface_type";
12975 case DW_TAG_namespace:
12976 return "DW_TAG_namespace";
12977 case DW_TAG_imported_module:
12978 return "DW_TAG_imported_module";
12979 case DW_TAG_unspecified_type:
12980 return "DW_TAG_unspecified_type";
12981 case DW_TAG_partial_unit:
12982 return "DW_TAG_partial_unit";
12983 case DW_TAG_imported_unit:
12984 return "DW_TAG_imported_unit";
12985 case DW_TAG_condition:
12986 return "DW_TAG_condition";
12987 case DW_TAG_shared_type:
12988 return "DW_TAG_shared_type";
12989 case DW_TAG_type_unit:
12990 return "DW_TAG_type_unit";
12991 case DW_TAG_MIPS_loop:
12992 return "DW_TAG_MIPS_loop";
12993 case DW_TAG_HP_array_descriptor:
12994 return "DW_TAG_HP_array_descriptor";
12995 case DW_TAG_format_label:
12996 return "DW_TAG_format_label";
12997 case DW_TAG_function_template:
12998 return "DW_TAG_function_template";
12999 case DW_TAG_class_template:
13000 return "DW_TAG_class_template";
13001 case DW_TAG_GNU_BINCL:
13002 return "DW_TAG_GNU_BINCL";
13003 case DW_TAG_GNU_EINCL:
13004 return "DW_TAG_GNU_EINCL";
13005 case DW_TAG_upc_shared_type:
13006 return "DW_TAG_upc_shared_type";
13007 case DW_TAG_upc_strict_type:
13008 return "DW_TAG_upc_strict_type";
13009 case DW_TAG_upc_relaxed_type:
13010 return "DW_TAG_upc_relaxed_type";
13011 case DW_TAG_PGI_kanji_type:
13012 return "DW_TAG_PGI_kanji_type";
13013 case DW_TAG_PGI_interface_block:
13014 return "DW_TAG_PGI_interface_block";
13015 case DW_TAG_GNU_call_site:
13016 return "DW_TAG_GNU_call_site";
13017 default:
13018 return "DW_TAG_<unknown>";
13019 }
13020 }
13021
13022 /* Convert a DWARF attribute code into its string name. */
13023
13024 static char *
13025 dwarf_attr_name (unsigned attr)
13026 {
13027 switch (attr)
13028 {
13029 case DW_AT_sibling:
13030 return "DW_AT_sibling";
13031 case DW_AT_location:
13032 return "DW_AT_location";
13033 case DW_AT_name:
13034 return "DW_AT_name";
13035 case DW_AT_ordering:
13036 return "DW_AT_ordering";
13037 case DW_AT_subscr_data:
13038 return "DW_AT_subscr_data";
13039 case DW_AT_byte_size:
13040 return "DW_AT_byte_size";
13041 case DW_AT_bit_offset:
13042 return "DW_AT_bit_offset";
13043 case DW_AT_bit_size:
13044 return "DW_AT_bit_size";
13045 case DW_AT_element_list:
13046 return "DW_AT_element_list";
13047 case DW_AT_stmt_list:
13048 return "DW_AT_stmt_list";
13049 case DW_AT_low_pc:
13050 return "DW_AT_low_pc";
13051 case DW_AT_high_pc:
13052 return "DW_AT_high_pc";
13053 case DW_AT_language:
13054 return "DW_AT_language";
13055 case DW_AT_member:
13056 return "DW_AT_member";
13057 case DW_AT_discr:
13058 return "DW_AT_discr";
13059 case DW_AT_discr_value:
13060 return "DW_AT_discr_value";
13061 case DW_AT_visibility:
13062 return "DW_AT_visibility";
13063 case DW_AT_import:
13064 return "DW_AT_import";
13065 case DW_AT_string_length:
13066 return "DW_AT_string_length";
13067 case DW_AT_common_reference:
13068 return "DW_AT_common_reference";
13069 case DW_AT_comp_dir:
13070 return "DW_AT_comp_dir";
13071 case DW_AT_const_value:
13072 return "DW_AT_const_value";
13073 case DW_AT_containing_type:
13074 return "DW_AT_containing_type";
13075 case DW_AT_default_value:
13076 return "DW_AT_default_value";
13077 case DW_AT_inline:
13078 return "DW_AT_inline";
13079 case DW_AT_is_optional:
13080 return "DW_AT_is_optional";
13081 case DW_AT_lower_bound:
13082 return "DW_AT_lower_bound";
13083 case DW_AT_producer:
13084 return "DW_AT_producer";
13085 case DW_AT_prototyped:
13086 return "DW_AT_prototyped";
13087 case DW_AT_return_addr:
13088 return "DW_AT_return_addr";
13089 case DW_AT_start_scope:
13090 return "DW_AT_start_scope";
13091 case DW_AT_bit_stride:
13092 return "DW_AT_bit_stride";
13093 case DW_AT_upper_bound:
13094 return "DW_AT_upper_bound";
13095 case DW_AT_abstract_origin:
13096 return "DW_AT_abstract_origin";
13097 case DW_AT_accessibility:
13098 return "DW_AT_accessibility";
13099 case DW_AT_address_class:
13100 return "DW_AT_address_class";
13101 case DW_AT_artificial:
13102 return "DW_AT_artificial";
13103 case DW_AT_base_types:
13104 return "DW_AT_base_types";
13105 case DW_AT_calling_convention:
13106 return "DW_AT_calling_convention";
13107 case DW_AT_count:
13108 return "DW_AT_count";
13109 case DW_AT_data_member_location:
13110 return "DW_AT_data_member_location";
13111 case DW_AT_decl_column:
13112 return "DW_AT_decl_column";
13113 case DW_AT_decl_file:
13114 return "DW_AT_decl_file";
13115 case DW_AT_decl_line:
13116 return "DW_AT_decl_line";
13117 case DW_AT_declaration:
13118 return "DW_AT_declaration";
13119 case DW_AT_discr_list:
13120 return "DW_AT_discr_list";
13121 case DW_AT_encoding:
13122 return "DW_AT_encoding";
13123 case DW_AT_external:
13124 return "DW_AT_external";
13125 case DW_AT_frame_base:
13126 return "DW_AT_frame_base";
13127 case DW_AT_friend:
13128 return "DW_AT_friend";
13129 case DW_AT_identifier_case:
13130 return "DW_AT_identifier_case";
13131 case DW_AT_macro_info:
13132 return "DW_AT_macro_info";
13133 case DW_AT_namelist_items:
13134 return "DW_AT_namelist_items";
13135 case DW_AT_priority:
13136 return "DW_AT_priority";
13137 case DW_AT_segment:
13138 return "DW_AT_segment";
13139 case DW_AT_specification:
13140 return "DW_AT_specification";
13141 case DW_AT_static_link:
13142 return "DW_AT_static_link";
13143 case DW_AT_type:
13144 return "DW_AT_type";
13145 case DW_AT_use_location:
13146 return "DW_AT_use_location";
13147 case DW_AT_variable_parameter:
13148 return "DW_AT_variable_parameter";
13149 case DW_AT_virtuality:
13150 return "DW_AT_virtuality";
13151 case DW_AT_vtable_elem_location:
13152 return "DW_AT_vtable_elem_location";
13153 /* DWARF 3 values. */
13154 case DW_AT_allocated:
13155 return "DW_AT_allocated";
13156 case DW_AT_associated:
13157 return "DW_AT_associated";
13158 case DW_AT_data_location:
13159 return "DW_AT_data_location";
13160 case DW_AT_byte_stride:
13161 return "DW_AT_byte_stride";
13162 case DW_AT_entry_pc:
13163 return "DW_AT_entry_pc";
13164 case DW_AT_use_UTF8:
13165 return "DW_AT_use_UTF8";
13166 case DW_AT_extension:
13167 return "DW_AT_extension";
13168 case DW_AT_ranges:
13169 return "DW_AT_ranges";
13170 case DW_AT_trampoline:
13171 return "DW_AT_trampoline";
13172 case DW_AT_call_column:
13173 return "DW_AT_call_column";
13174 case DW_AT_call_file:
13175 return "DW_AT_call_file";
13176 case DW_AT_call_line:
13177 return "DW_AT_call_line";
13178 case DW_AT_description:
13179 return "DW_AT_description";
13180 case DW_AT_binary_scale:
13181 return "DW_AT_binary_scale";
13182 case DW_AT_decimal_scale:
13183 return "DW_AT_decimal_scale";
13184 case DW_AT_small:
13185 return "DW_AT_small";
13186 case DW_AT_decimal_sign:
13187 return "DW_AT_decimal_sign";
13188 case DW_AT_digit_count:
13189 return "DW_AT_digit_count";
13190 case DW_AT_picture_string:
13191 return "DW_AT_picture_string";
13192 case DW_AT_mutable:
13193 return "DW_AT_mutable";
13194 case DW_AT_threads_scaled:
13195 return "DW_AT_threads_scaled";
13196 case DW_AT_explicit:
13197 return "DW_AT_explicit";
13198 case DW_AT_object_pointer:
13199 return "DW_AT_object_pointer";
13200 case DW_AT_endianity:
13201 return "DW_AT_endianity";
13202 case DW_AT_elemental:
13203 return "DW_AT_elemental";
13204 case DW_AT_pure:
13205 return "DW_AT_pure";
13206 case DW_AT_recursive:
13207 return "DW_AT_recursive";
13208 /* DWARF 4 values. */
13209 case DW_AT_signature:
13210 return "DW_AT_signature";
13211 case DW_AT_linkage_name:
13212 return "DW_AT_linkage_name";
13213 /* SGI/MIPS extensions. */
13214 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13215 case DW_AT_MIPS_fde:
13216 return "DW_AT_MIPS_fde";
13217 #endif
13218 case DW_AT_MIPS_loop_begin:
13219 return "DW_AT_MIPS_loop_begin";
13220 case DW_AT_MIPS_tail_loop_begin:
13221 return "DW_AT_MIPS_tail_loop_begin";
13222 case DW_AT_MIPS_epilog_begin:
13223 return "DW_AT_MIPS_epilog_begin";
13224 case DW_AT_MIPS_loop_unroll_factor:
13225 return "DW_AT_MIPS_loop_unroll_factor";
13226 case DW_AT_MIPS_software_pipeline_depth:
13227 return "DW_AT_MIPS_software_pipeline_depth";
13228 case DW_AT_MIPS_linkage_name:
13229 return "DW_AT_MIPS_linkage_name";
13230 case DW_AT_MIPS_stride:
13231 return "DW_AT_MIPS_stride";
13232 case DW_AT_MIPS_abstract_name:
13233 return "DW_AT_MIPS_abstract_name";
13234 case DW_AT_MIPS_clone_origin:
13235 return "DW_AT_MIPS_clone_origin";
13236 case DW_AT_MIPS_has_inlines:
13237 return "DW_AT_MIPS_has_inlines";
13238 /* HP extensions. */
13239 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13240 case DW_AT_HP_block_index:
13241 return "DW_AT_HP_block_index";
13242 #endif
13243 case DW_AT_HP_unmodifiable:
13244 return "DW_AT_HP_unmodifiable";
13245 case DW_AT_HP_actuals_stmt_list:
13246 return "DW_AT_HP_actuals_stmt_list";
13247 case DW_AT_HP_proc_per_section:
13248 return "DW_AT_HP_proc_per_section";
13249 case DW_AT_HP_raw_data_ptr:
13250 return "DW_AT_HP_raw_data_ptr";
13251 case DW_AT_HP_pass_by_reference:
13252 return "DW_AT_HP_pass_by_reference";
13253 case DW_AT_HP_opt_level:
13254 return "DW_AT_HP_opt_level";
13255 case DW_AT_HP_prof_version_id:
13256 return "DW_AT_HP_prof_version_id";
13257 case DW_AT_HP_opt_flags:
13258 return "DW_AT_HP_opt_flags";
13259 case DW_AT_HP_cold_region_low_pc:
13260 return "DW_AT_HP_cold_region_low_pc";
13261 case DW_AT_HP_cold_region_high_pc:
13262 return "DW_AT_HP_cold_region_high_pc";
13263 case DW_AT_HP_all_variables_modifiable:
13264 return "DW_AT_HP_all_variables_modifiable";
13265 case DW_AT_HP_linkage_name:
13266 return "DW_AT_HP_linkage_name";
13267 case DW_AT_HP_prof_flags:
13268 return "DW_AT_HP_prof_flags";
13269 /* GNU extensions. */
13270 case DW_AT_sf_names:
13271 return "DW_AT_sf_names";
13272 case DW_AT_src_info:
13273 return "DW_AT_src_info";
13274 case DW_AT_mac_info:
13275 return "DW_AT_mac_info";
13276 case DW_AT_src_coords:
13277 return "DW_AT_src_coords";
13278 case DW_AT_body_begin:
13279 return "DW_AT_body_begin";
13280 case DW_AT_body_end:
13281 return "DW_AT_body_end";
13282 case DW_AT_GNU_vector:
13283 return "DW_AT_GNU_vector";
13284 case DW_AT_GNU_odr_signature:
13285 return "DW_AT_GNU_odr_signature";
13286 /* VMS extensions. */
13287 case DW_AT_VMS_rtnbeg_pd_address:
13288 return "DW_AT_VMS_rtnbeg_pd_address";
13289 /* UPC extension. */
13290 case DW_AT_upc_threads_scaled:
13291 return "DW_AT_upc_threads_scaled";
13292 /* PGI (STMicroelectronics) extensions. */
13293 case DW_AT_PGI_lbase:
13294 return "DW_AT_PGI_lbase";
13295 case DW_AT_PGI_soffset:
13296 return "DW_AT_PGI_soffset";
13297 case DW_AT_PGI_lstride:
13298 return "DW_AT_PGI_lstride";
13299 default:
13300 return "DW_AT_<unknown>";
13301 }
13302 }
13303
13304 /* Convert a DWARF value form code into its string name. */
13305
13306 static char *
13307 dwarf_form_name (unsigned form)
13308 {
13309 switch (form)
13310 {
13311 case DW_FORM_addr:
13312 return "DW_FORM_addr";
13313 case DW_FORM_block2:
13314 return "DW_FORM_block2";
13315 case DW_FORM_block4:
13316 return "DW_FORM_block4";
13317 case DW_FORM_data2:
13318 return "DW_FORM_data2";
13319 case DW_FORM_data4:
13320 return "DW_FORM_data4";
13321 case DW_FORM_data8:
13322 return "DW_FORM_data8";
13323 case DW_FORM_string:
13324 return "DW_FORM_string";
13325 case DW_FORM_block:
13326 return "DW_FORM_block";
13327 case DW_FORM_block1:
13328 return "DW_FORM_block1";
13329 case DW_FORM_data1:
13330 return "DW_FORM_data1";
13331 case DW_FORM_flag:
13332 return "DW_FORM_flag";
13333 case DW_FORM_sdata:
13334 return "DW_FORM_sdata";
13335 case DW_FORM_strp:
13336 return "DW_FORM_strp";
13337 case DW_FORM_udata:
13338 return "DW_FORM_udata";
13339 case DW_FORM_ref_addr:
13340 return "DW_FORM_ref_addr";
13341 case DW_FORM_ref1:
13342 return "DW_FORM_ref1";
13343 case DW_FORM_ref2:
13344 return "DW_FORM_ref2";
13345 case DW_FORM_ref4:
13346 return "DW_FORM_ref4";
13347 case DW_FORM_ref8:
13348 return "DW_FORM_ref8";
13349 case DW_FORM_ref_udata:
13350 return "DW_FORM_ref_udata";
13351 case DW_FORM_indirect:
13352 return "DW_FORM_indirect";
13353 case DW_FORM_sec_offset:
13354 return "DW_FORM_sec_offset";
13355 case DW_FORM_exprloc:
13356 return "DW_FORM_exprloc";
13357 case DW_FORM_flag_present:
13358 return "DW_FORM_flag_present";
13359 case DW_FORM_ref_sig8:
13360 return "DW_FORM_ref_sig8";
13361 default:
13362 return "DW_FORM_<unknown>";
13363 }
13364 }
13365
13366 /* Convert a DWARF stack opcode into its string name. */
13367
13368 const char *
13369 dwarf_stack_op_name (unsigned op)
13370 {
13371 switch (op)
13372 {
13373 case DW_OP_addr:
13374 return "DW_OP_addr";
13375 case DW_OP_deref:
13376 return "DW_OP_deref";
13377 case DW_OP_const1u:
13378 return "DW_OP_const1u";
13379 case DW_OP_const1s:
13380 return "DW_OP_const1s";
13381 case DW_OP_const2u:
13382 return "DW_OP_const2u";
13383 case DW_OP_const2s:
13384 return "DW_OP_const2s";
13385 case DW_OP_const4u:
13386 return "DW_OP_const4u";
13387 case DW_OP_const4s:
13388 return "DW_OP_const4s";
13389 case DW_OP_const8u:
13390 return "DW_OP_const8u";
13391 case DW_OP_const8s:
13392 return "DW_OP_const8s";
13393 case DW_OP_constu:
13394 return "DW_OP_constu";
13395 case DW_OP_consts:
13396 return "DW_OP_consts";
13397 case DW_OP_dup:
13398 return "DW_OP_dup";
13399 case DW_OP_drop:
13400 return "DW_OP_drop";
13401 case DW_OP_over:
13402 return "DW_OP_over";
13403 case DW_OP_pick:
13404 return "DW_OP_pick";
13405 case DW_OP_swap:
13406 return "DW_OP_swap";
13407 case DW_OP_rot:
13408 return "DW_OP_rot";
13409 case DW_OP_xderef:
13410 return "DW_OP_xderef";
13411 case DW_OP_abs:
13412 return "DW_OP_abs";
13413 case DW_OP_and:
13414 return "DW_OP_and";
13415 case DW_OP_div:
13416 return "DW_OP_div";
13417 case DW_OP_minus:
13418 return "DW_OP_minus";
13419 case DW_OP_mod:
13420 return "DW_OP_mod";
13421 case DW_OP_mul:
13422 return "DW_OP_mul";
13423 case DW_OP_neg:
13424 return "DW_OP_neg";
13425 case DW_OP_not:
13426 return "DW_OP_not";
13427 case DW_OP_or:
13428 return "DW_OP_or";
13429 case DW_OP_plus:
13430 return "DW_OP_plus";
13431 case DW_OP_plus_uconst:
13432 return "DW_OP_plus_uconst";
13433 case DW_OP_shl:
13434 return "DW_OP_shl";
13435 case DW_OP_shr:
13436 return "DW_OP_shr";
13437 case DW_OP_shra:
13438 return "DW_OP_shra";
13439 case DW_OP_xor:
13440 return "DW_OP_xor";
13441 case DW_OP_bra:
13442 return "DW_OP_bra";
13443 case DW_OP_eq:
13444 return "DW_OP_eq";
13445 case DW_OP_ge:
13446 return "DW_OP_ge";
13447 case DW_OP_gt:
13448 return "DW_OP_gt";
13449 case DW_OP_le:
13450 return "DW_OP_le";
13451 case DW_OP_lt:
13452 return "DW_OP_lt";
13453 case DW_OP_ne:
13454 return "DW_OP_ne";
13455 case DW_OP_skip:
13456 return "DW_OP_skip";
13457 case DW_OP_lit0:
13458 return "DW_OP_lit0";
13459 case DW_OP_lit1:
13460 return "DW_OP_lit1";
13461 case DW_OP_lit2:
13462 return "DW_OP_lit2";
13463 case DW_OP_lit3:
13464 return "DW_OP_lit3";
13465 case DW_OP_lit4:
13466 return "DW_OP_lit4";
13467 case DW_OP_lit5:
13468 return "DW_OP_lit5";
13469 case DW_OP_lit6:
13470 return "DW_OP_lit6";
13471 case DW_OP_lit7:
13472 return "DW_OP_lit7";
13473 case DW_OP_lit8:
13474 return "DW_OP_lit8";
13475 case DW_OP_lit9:
13476 return "DW_OP_lit9";
13477 case DW_OP_lit10:
13478 return "DW_OP_lit10";
13479 case DW_OP_lit11:
13480 return "DW_OP_lit11";
13481 case DW_OP_lit12:
13482 return "DW_OP_lit12";
13483 case DW_OP_lit13:
13484 return "DW_OP_lit13";
13485 case DW_OP_lit14:
13486 return "DW_OP_lit14";
13487 case DW_OP_lit15:
13488 return "DW_OP_lit15";
13489 case DW_OP_lit16:
13490 return "DW_OP_lit16";
13491 case DW_OP_lit17:
13492 return "DW_OP_lit17";
13493 case DW_OP_lit18:
13494 return "DW_OP_lit18";
13495 case DW_OP_lit19:
13496 return "DW_OP_lit19";
13497 case DW_OP_lit20:
13498 return "DW_OP_lit20";
13499 case DW_OP_lit21:
13500 return "DW_OP_lit21";
13501 case DW_OP_lit22:
13502 return "DW_OP_lit22";
13503 case DW_OP_lit23:
13504 return "DW_OP_lit23";
13505 case DW_OP_lit24:
13506 return "DW_OP_lit24";
13507 case DW_OP_lit25:
13508 return "DW_OP_lit25";
13509 case DW_OP_lit26:
13510 return "DW_OP_lit26";
13511 case DW_OP_lit27:
13512 return "DW_OP_lit27";
13513 case DW_OP_lit28:
13514 return "DW_OP_lit28";
13515 case DW_OP_lit29:
13516 return "DW_OP_lit29";
13517 case DW_OP_lit30:
13518 return "DW_OP_lit30";
13519 case DW_OP_lit31:
13520 return "DW_OP_lit31";
13521 case DW_OP_reg0:
13522 return "DW_OP_reg0";
13523 case DW_OP_reg1:
13524 return "DW_OP_reg1";
13525 case DW_OP_reg2:
13526 return "DW_OP_reg2";
13527 case DW_OP_reg3:
13528 return "DW_OP_reg3";
13529 case DW_OP_reg4:
13530 return "DW_OP_reg4";
13531 case DW_OP_reg5:
13532 return "DW_OP_reg5";
13533 case DW_OP_reg6:
13534 return "DW_OP_reg6";
13535 case DW_OP_reg7:
13536 return "DW_OP_reg7";
13537 case DW_OP_reg8:
13538 return "DW_OP_reg8";
13539 case DW_OP_reg9:
13540 return "DW_OP_reg9";
13541 case DW_OP_reg10:
13542 return "DW_OP_reg10";
13543 case DW_OP_reg11:
13544 return "DW_OP_reg11";
13545 case DW_OP_reg12:
13546 return "DW_OP_reg12";
13547 case DW_OP_reg13:
13548 return "DW_OP_reg13";
13549 case DW_OP_reg14:
13550 return "DW_OP_reg14";
13551 case DW_OP_reg15:
13552 return "DW_OP_reg15";
13553 case DW_OP_reg16:
13554 return "DW_OP_reg16";
13555 case DW_OP_reg17:
13556 return "DW_OP_reg17";
13557 case DW_OP_reg18:
13558 return "DW_OP_reg18";
13559 case DW_OP_reg19:
13560 return "DW_OP_reg19";
13561 case DW_OP_reg20:
13562 return "DW_OP_reg20";
13563 case DW_OP_reg21:
13564 return "DW_OP_reg21";
13565 case DW_OP_reg22:
13566 return "DW_OP_reg22";
13567 case DW_OP_reg23:
13568 return "DW_OP_reg23";
13569 case DW_OP_reg24:
13570 return "DW_OP_reg24";
13571 case DW_OP_reg25:
13572 return "DW_OP_reg25";
13573 case DW_OP_reg26:
13574 return "DW_OP_reg26";
13575 case DW_OP_reg27:
13576 return "DW_OP_reg27";
13577 case DW_OP_reg28:
13578 return "DW_OP_reg28";
13579 case DW_OP_reg29:
13580 return "DW_OP_reg29";
13581 case DW_OP_reg30:
13582 return "DW_OP_reg30";
13583 case DW_OP_reg31:
13584 return "DW_OP_reg31";
13585 case DW_OP_breg0:
13586 return "DW_OP_breg0";
13587 case DW_OP_breg1:
13588 return "DW_OP_breg1";
13589 case DW_OP_breg2:
13590 return "DW_OP_breg2";
13591 case DW_OP_breg3:
13592 return "DW_OP_breg3";
13593 case DW_OP_breg4:
13594 return "DW_OP_breg4";
13595 case DW_OP_breg5:
13596 return "DW_OP_breg5";
13597 case DW_OP_breg6:
13598 return "DW_OP_breg6";
13599 case DW_OP_breg7:
13600 return "DW_OP_breg7";
13601 case DW_OP_breg8:
13602 return "DW_OP_breg8";
13603 case DW_OP_breg9:
13604 return "DW_OP_breg9";
13605 case DW_OP_breg10:
13606 return "DW_OP_breg10";
13607 case DW_OP_breg11:
13608 return "DW_OP_breg11";
13609 case DW_OP_breg12:
13610 return "DW_OP_breg12";
13611 case DW_OP_breg13:
13612 return "DW_OP_breg13";
13613 case DW_OP_breg14:
13614 return "DW_OP_breg14";
13615 case DW_OP_breg15:
13616 return "DW_OP_breg15";
13617 case DW_OP_breg16:
13618 return "DW_OP_breg16";
13619 case DW_OP_breg17:
13620 return "DW_OP_breg17";
13621 case DW_OP_breg18:
13622 return "DW_OP_breg18";
13623 case DW_OP_breg19:
13624 return "DW_OP_breg19";
13625 case DW_OP_breg20:
13626 return "DW_OP_breg20";
13627 case DW_OP_breg21:
13628 return "DW_OP_breg21";
13629 case DW_OP_breg22:
13630 return "DW_OP_breg22";
13631 case DW_OP_breg23:
13632 return "DW_OP_breg23";
13633 case DW_OP_breg24:
13634 return "DW_OP_breg24";
13635 case DW_OP_breg25:
13636 return "DW_OP_breg25";
13637 case DW_OP_breg26:
13638 return "DW_OP_breg26";
13639 case DW_OP_breg27:
13640 return "DW_OP_breg27";
13641 case DW_OP_breg28:
13642 return "DW_OP_breg28";
13643 case DW_OP_breg29:
13644 return "DW_OP_breg29";
13645 case DW_OP_breg30:
13646 return "DW_OP_breg30";
13647 case DW_OP_breg31:
13648 return "DW_OP_breg31";
13649 case DW_OP_regx:
13650 return "DW_OP_regx";
13651 case DW_OP_fbreg:
13652 return "DW_OP_fbreg";
13653 case DW_OP_bregx:
13654 return "DW_OP_bregx";
13655 case DW_OP_piece:
13656 return "DW_OP_piece";
13657 case DW_OP_deref_size:
13658 return "DW_OP_deref_size";
13659 case DW_OP_xderef_size:
13660 return "DW_OP_xderef_size";
13661 case DW_OP_nop:
13662 return "DW_OP_nop";
13663 /* DWARF 3 extensions. */
13664 case DW_OP_push_object_address:
13665 return "DW_OP_push_object_address";
13666 case DW_OP_call2:
13667 return "DW_OP_call2";
13668 case DW_OP_call4:
13669 return "DW_OP_call4";
13670 case DW_OP_call_ref:
13671 return "DW_OP_call_ref";
13672 case DW_OP_form_tls_address:
13673 return "DW_OP_form_tls_address";
13674 case DW_OP_call_frame_cfa:
13675 return "DW_OP_call_frame_cfa";
13676 case DW_OP_bit_piece:
13677 return "DW_OP_bit_piece";
13678 /* DWARF 4 extensions. */
13679 case DW_OP_implicit_value:
13680 return "DW_OP_implicit_value";
13681 case DW_OP_stack_value:
13682 return "DW_OP_stack_value";
13683 /* GNU extensions. */
13684 case DW_OP_GNU_push_tls_address:
13685 return "DW_OP_GNU_push_tls_address";
13686 case DW_OP_GNU_uninit:
13687 return "DW_OP_GNU_uninit";
13688 case DW_OP_GNU_implicit_pointer:
13689 return "DW_OP_GNU_implicit_pointer";
13690 case DW_OP_GNU_entry_value:
13691 return "DW_OP_GNU_entry_value";
13692 case DW_OP_GNU_const_type:
13693 return "DW_OP_GNU_const_type";
13694 case DW_OP_GNU_regval_type:
13695 return "DW_OP_GNU_regval_type";
13696 case DW_OP_GNU_deref_type:
13697 return "DW_OP_GNU_deref_type";
13698 case DW_OP_GNU_convert:
13699 return "DW_OP_GNU_convert";
13700 case DW_OP_GNU_reinterpret:
13701 return "DW_OP_GNU_reinterpret";
13702 default:
13703 return NULL;
13704 }
13705 }
13706
13707 static char *
13708 dwarf_bool_name (unsigned mybool)
13709 {
13710 if (mybool)
13711 return "TRUE";
13712 else
13713 return "FALSE";
13714 }
13715
13716 /* Convert a DWARF type code into its string name. */
13717
13718 static char *
13719 dwarf_type_encoding_name (unsigned enc)
13720 {
13721 switch (enc)
13722 {
13723 case DW_ATE_void:
13724 return "DW_ATE_void";
13725 case DW_ATE_address:
13726 return "DW_ATE_address";
13727 case DW_ATE_boolean:
13728 return "DW_ATE_boolean";
13729 case DW_ATE_complex_float:
13730 return "DW_ATE_complex_float";
13731 case DW_ATE_float:
13732 return "DW_ATE_float";
13733 case DW_ATE_signed:
13734 return "DW_ATE_signed";
13735 case DW_ATE_signed_char:
13736 return "DW_ATE_signed_char";
13737 case DW_ATE_unsigned:
13738 return "DW_ATE_unsigned";
13739 case DW_ATE_unsigned_char:
13740 return "DW_ATE_unsigned_char";
13741 /* DWARF 3. */
13742 case DW_ATE_imaginary_float:
13743 return "DW_ATE_imaginary_float";
13744 case DW_ATE_packed_decimal:
13745 return "DW_ATE_packed_decimal";
13746 case DW_ATE_numeric_string:
13747 return "DW_ATE_numeric_string";
13748 case DW_ATE_edited:
13749 return "DW_ATE_edited";
13750 case DW_ATE_signed_fixed:
13751 return "DW_ATE_signed_fixed";
13752 case DW_ATE_unsigned_fixed:
13753 return "DW_ATE_unsigned_fixed";
13754 case DW_ATE_decimal_float:
13755 return "DW_ATE_decimal_float";
13756 /* DWARF 4. */
13757 case DW_ATE_UTF:
13758 return "DW_ATE_UTF";
13759 /* HP extensions. */
13760 case DW_ATE_HP_float80:
13761 return "DW_ATE_HP_float80";
13762 case DW_ATE_HP_complex_float80:
13763 return "DW_ATE_HP_complex_float80";
13764 case DW_ATE_HP_float128:
13765 return "DW_ATE_HP_float128";
13766 case DW_ATE_HP_complex_float128:
13767 return "DW_ATE_HP_complex_float128";
13768 case DW_ATE_HP_floathpintel:
13769 return "DW_ATE_HP_floathpintel";
13770 case DW_ATE_HP_imaginary_float80:
13771 return "DW_ATE_HP_imaginary_float80";
13772 case DW_ATE_HP_imaginary_float128:
13773 return "DW_ATE_HP_imaginary_float128";
13774 default:
13775 return "DW_ATE_<unknown>";
13776 }
13777 }
13778
13779 /* Convert a DWARF call frame info operation to its string name. */
13780
13781 #if 0
13782 static char *
13783 dwarf_cfi_name (unsigned cfi_opc)
13784 {
13785 switch (cfi_opc)
13786 {
13787 case DW_CFA_advance_loc:
13788 return "DW_CFA_advance_loc";
13789 case DW_CFA_offset:
13790 return "DW_CFA_offset";
13791 case DW_CFA_restore:
13792 return "DW_CFA_restore";
13793 case DW_CFA_nop:
13794 return "DW_CFA_nop";
13795 case DW_CFA_set_loc:
13796 return "DW_CFA_set_loc";
13797 case DW_CFA_advance_loc1:
13798 return "DW_CFA_advance_loc1";
13799 case DW_CFA_advance_loc2:
13800 return "DW_CFA_advance_loc2";
13801 case DW_CFA_advance_loc4:
13802 return "DW_CFA_advance_loc4";
13803 case DW_CFA_offset_extended:
13804 return "DW_CFA_offset_extended";
13805 case DW_CFA_restore_extended:
13806 return "DW_CFA_restore_extended";
13807 case DW_CFA_undefined:
13808 return "DW_CFA_undefined";
13809 case DW_CFA_same_value:
13810 return "DW_CFA_same_value";
13811 case DW_CFA_register:
13812 return "DW_CFA_register";
13813 case DW_CFA_remember_state:
13814 return "DW_CFA_remember_state";
13815 case DW_CFA_restore_state:
13816 return "DW_CFA_restore_state";
13817 case DW_CFA_def_cfa:
13818 return "DW_CFA_def_cfa";
13819 case DW_CFA_def_cfa_register:
13820 return "DW_CFA_def_cfa_register";
13821 case DW_CFA_def_cfa_offset:
13822 return "DW_CFA_def_cfa_offset";
13823 /* DWARF 3. */
13824 case DW_CFA_def_cfa_expression:
13825 return "DW_CFA_def_cfa_expression";
13826 case DW_CFA_expression:
13827 return "DW_CFA_expression";
13828 case DW_CFA_offset_extended_sf:
13829 return "DW_CFA_offset_extended_sf";
13830 case DW_CFA_def_cfa_sf:
13831 return "DW_CFA_def_cfa_sf";
13832 case DW_CFA_def_cfa_offset_sf:
13833 return "DW_CFA_def_cfa_offset_sf";
13834 case DW_CFA_val_offset:
13835 return "DW_CFA_val_offset";
13836 case DW_CFA_val_offset_sf:
13837 return "DW_CFA_val_offset_sf";
13838 case DW_CFA_val_expression:
13839 return "DW_CFA_val_expression";
13840 /* SGI/MIPS specific. */
13841 case DW_CFA_MIPS_advance_loc8:
13842 return "DW_CFA_MIPS_advance_loc8";
13843 /* GNU extensions. */
13844 case DW_CFA_GNU_window_save:
13845 return "DW_CFA_GNU_window_save";
13846 case DW_CFA_GNU_args_size:
13847 return "DW_CFA_GNU_args_size";
13848 case DW_CFA_GNU_negative_offset_extended:
13849 return "DW_CFA_GNU_negative_offset_extended";
13850 default:
13851 return "DW_CFA_<unknown>";
13852 }
13853 }
13854 #endif
13855
13856 static void
13857 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13858 {
13859 unsigned int i;
13860
13861 print_spaces (indent, f);
13862 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13863 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13864
13865 if (die->parent != NULL)
13866 {
13867 print_spaces (indent, f);
13868 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13869 die->parent->offset);
13870 }
13871
13872 print_spaces (indent, f);
13873 fprintf_unfiltered (f, " has children: %s\n",
13874 dwarf_bool_name (die->child != NULL));
13875
13876 print_spaces (indent, f);
13877 fprintf_unfiltered (f, " attributes:\n");
13878
13879 for (i = 0; i < die->num_attrs; ++i)
13880 {
13881 print_spaces (indent, f);
13882 fprintf_unfiltered (f, " %s (%s) ",
13883 dwarf_attr_name (die->attrs[i].name),
13884 dwarf_form_name (die->attrs[i].form));
13885
13886 switch (die->attrs[i].form)
13887 {
13888 case DW_FORM_ref_addr:
13889 case DW_FORM_addr:
13890 fprintf_unfiltered (f, "address: ");
13891 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13892 break;
13893 case DW_FORM_block2:
13894 case DW_FORM_block4:
13895 case DW_FORM_block:
13896 case DW_FORM_block1:
13897 fprintf_unfiltered (f, "block: size %d",
13898 DW_BLOCK (&die->attrs[i])->size);
13899 break;
13900 case DW_FORM_exprloc:
13901 fprintf_unfiltered (f, "expression: size %u",
13902 DW_BLOCK (&die->attrs[i])->size);
13903 break;
13904 case DW_FORM_ref1:
13905 case DW_FORM_ref2:
13906 case DW_FORM_ref4:
13907 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13908 (long) (DW_ADDR (&die->attrs[i])));
13909 break;
13910 case DW_FORM_data1:
13911 case DW_FORM_data2:
13912 case DW_FORM_data4:
13913 case DW_FORM_data8:
13914 case DW_FORM_udata:
13915 case DW_FORM_sdata:
13916 fprintf_unfiltered (f, "constant: %s",
13917 pulongest (DW_UNSND (&die->attrs[i])));
13918 break;
13919 case DW_FORM_sec_offset:
13920 fprintf_unfiltered (f, "section offset: %s",
13921 pulongest (DW_UNSND (&die->attrs[i])));
13922 break;
13923 case DW_FORM_ref_sig8:
13924 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13925 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13926 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13927 else
13928 fprintf_unfiltered (f, "signatured type, offset: unknown");
13929 break;
13930 case DW_FORM_string:
13931 case DW_FORM_strp:
13932 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13933 DW_STRING (&die->attrs[i])
13934 ? DW_STRING (&die->attrs[i]) : "",
13935 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13936 break;
13937 case DW_FORM_flag:
13938 if (DW_UNSND (&die->attrs[i]))
13939 fprintf_unfiltered (f, "flag: TRUE");
13940 else
13941 fprintf_unfiltered (f, "flag: FALSE");
13942 break;
13943 case DW_FORM_flag_present:
13944 fprintf_unfiltered (f, "flag: TRUE");
13945 break;
13946 case DW_FORM_indirect:
13947 /* The reader will have reduced the indirect form to
13948 the "base form" so this form should not occur. */
13949 fprintf_unfiltered (f,
13950 "unexpected attribute form: DW_FORM_indirect");
13951 break;
13952 default:
13953 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13954 die->attrs[i].form);
13955 break;
13956 }
13957 fprintf_unfiltered (f, "\n");
13958 }
13959 }
13960
13961 static void
13962 dump_die_for_error (struct die_info *die)
13963 {
13964 dump_die_shallow (gdb_stderr, 0, die);
13965 }
13966
13967 static void
13968 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13969 {
13970 int indent = level * 4;
13971
13972 gdb_assert (die != NULL);
13973
13974 if (level >= max_level)
13975 return;
13976
13977 dump_die_shallow (f, indent, die);
13978
13979 if (die->child != NULL)
13980 {
13981 print_spaces (indent, f);
13982 fprintf_unfiltered (f, " Children:");
13983 if (level + 1 < max_level)
13984 {
13985 fprintf_unfiltered (f, "\n");
13986 dump_die_1 (f, level + 1, max_level, die->child);
13987 }
13988 else
13989 {
13990 fprintf_unfiltered (f,
13991 " [not printed, max nesting level reached]\n");
13992 }
13993 }
13994
13995 if (die->sibling != NULL && level > 0)
13996 {
13997 dump_die_1 (f, level, max_level, die->sibling);
13998 }
13999 }
14000
14001 /* This is called from the pdie macro in gdbinit.in.
14002 It's not static so gcc will keep a copy callable from gdb. */
14003
14004 void
14005 dump_die (struct die_info *die, int max_level)
14006 {
14007 dump_die_1 (gdb_stdlog, 0, max_level, die);
14008 }
14009
14010 static void
14011 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14012 {
14013 void **slot;
14014
14015 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14016
14017 *slot = die;
14018 }
14019
14020 static int
14021 is_ref_attr (struct attribute *attr)
14022 {
14023 switch (attr->form)
14024 {
14025 case DW_FORM_ref_addr:
14026 case DW_FORM_ref1:
14027 case DW_FORM_ref2:
14028 case DW_FORM_ref4:
14029 case DW_FORM_ref8:
14030 case DW_FORM_ref_udata:
14031 return 1;
14032 default:
14033 return 0;
14034 }
14035 }
14036
14037 static unsigned int
14038 dwarf2_get_ref_die_offset (struct attribute *attr)
14039 {
14040 if (is_ref_attr (attr))
14041 return DW_ADDR (attr);
14042
14043 complaint (&symfile_complaints,
14044 _("unsupported die ref attribute form: '%s'"),
14045 dwarf_form_name (attr->form));
14046 return 0;
14047 }
14048
14049 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14050 * the value held by the attribute is not constant. */
14051
14052 static LONGEST
14053 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14054 {
14055 if (attr->form == DW_FORM_sdata)
14056 return DW_SND (attr);
14057 else if (attr->form == DW_FORM_udata
14058 || attr->form == DW_FORM_data1
14059 || attr->form == DW_FORM_data2
14060 || attr->form == DW_FORM_data4
14061 || attr->form == DW_FORM_data8)
14062 return DW_UNSND (attr);
14063 else
14064 {
14065 complaint (&symfile_complaints,
14066 _("Attribute value is not a constant (%s)"),
14067 dwarf_form_name (attr->form));
14068 return default_value;
14069 }
14070 }
14071
14072 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14073 unit and add it to our queue.
14074 The result is non-zero if PER_CU was queued, otherwise the result is zero
14075 meaning either PER_CU is already queued or it is already loaded. */
14076
14077 static int
14078 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14079 struct dwarf2_per_cu_data *per_cu)
14080 {
14081 /* We may arrive here during partial symbol reading, if we need full
14082 DIEs to process an unusual case (e.g. template arguments). Do
14083 not queue PER_CU, just tell our caller to load its DIEs. */
14084 if (dwarf2_per_objfile->reading_partial_symbols)
14085 {
14086 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14087 return 1;
14088 return 0;
14089 }
14090
14091 /* Mark the dependence relation so that we don't flush PER_CU
14092 too early. */
14093 dwarf2_add_dependence (this_cu, per_cu);
14094
14095 /* If it's already on the queue, we have nothing to do. */
14096 if (per_cu->queued)
14097 return 0;
14098
14099 /* If the compilation unit is already loaded, just mark it as
14100 used. */
14101 if (per_cu->cu != NULL)
14102 {
14103 per_cu->cu->last_used = 0;
14104 return 0;
14105 }
14106
14107 /* Add it to the queue. */
14108 queue_comp_unit (per_cu);
14109
14110 return 1;
14111 }
14112
14113 /* Follow reference or signature attribute ATTR of SRC_DIE.
14114 On entry *REF_CU is the CU of SRC_DIE.
14115 On exit *REF_CU is the CU of the result. */
14116
14117 static struct die_info *
14118 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14119 struct dwarf2_cu **ref_cu)
14120 {
14121 struct die_info *die;
14122
14123 if (is_ref_attr (attr))
14124 die = follow_die_ref (src_die, attr, ref_cu);
14125 else if (attr->form == DW_FORM_ref_sig8)
14126 die = follow_die_sig (src_die, attr, ref_cu);
14127 else
14128 {
14129 dump_die_for_error (src_die);
14130 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14131 (*ref_cu)->objfile->name);
14132 }
14133
14134 return die;
14135 }
14136
14137 /* Follow reference OFFSET.
14138 On entry *REF_CU is the CU of the source die referencing OFFSET.
14139 On exit *REF_CU is the CU of the result.
14140 Returns NULL if OFFSET is invalid. */
14141
14142 static struct die_info *
14143 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14144 {
14145 struct die_info temp_die;
14146 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14147
14148 gdb_assert (cu->per_cu != NULL);
14149
14150 target_cu = cu;
14151
14152 if (cu->per_cu->debug_types_section)
14153 {
14154 /* .debug_types CUs cannot reference anything outside their CU.
14155 If they need to, they have to reference a signatured type via
14156 DW_FORM_ref_sig8. */
14157 if (! offset_in_cu_p (&cu->header, offset))
14158 return NULL;
14159 }
14160 else if (! offset_in_cu_p (&cu->header, offset))
14161 {
14162 struct dwarf2_per_cu_data *per_cu;
14163
14164 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14165
14166 /* If necessary, add it to the queue and load its DIEs. */
14167 if (maybe_queue_comp_unit (cu, per_cu))
14168 load_full_comp_unit (per_cu);
14169
14170 target_cu = per_cu->cu;
14171 }
14172 else if (cu->dies == NULL)
14173 {
14174 /* We're loading full DIEs during partial symbol reading. */
14175 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14176 load_full_comp_unit (cu->per_cu);
14177 }
14178
14179 *ref_cu = target_cu;
14180 temp_die.offset = offset;
14181 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14182 }
14183
14184 /* Follow reference attribute ATTR of SRC_DIE.
14185 On entry *REF_CU is the CU of SRC_DIE.
14186 On exit *REF_CU is the CU of the result. */
14187
14188 static struct die_info *
14189 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14190 struct dwarf2_cu **ref_cu)
14191 {
14192 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14193 struct dwarf2_cu *cu = *ref_cu;
14194 struct die_info *die;
14195
14196 die = follow_die_offset (offset, ref_cu);
14197 if (!die)
14198 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14199 "at 0x%x [in module %s]"),
14200 offset, src_die->offset, cu->objfile->name);
14201
14202 return die;
14203 }
14204
14205 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14206 Returned value is intended for DW_OP_call*. Returned
14207 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14208
14209 struct dwarf2_locexpr_baton
14210 dwarf2_fetch_die_location_block (unsigned int offset,
14211 struct dwarf2_per_cu_data *per_cu,
14212 CORE_ADDR (*get_frame_pc) (void *baton),
14213 void *baton)
14214 {
14215 struct dwarf2_cu *cu;
14216 struct die_info *die;
14217 struct attribute *attr;
14218 struct dwarf2_locexpr_baton retval;
14219
14220 dw2_setup (per_cu->objfile);
14221
14222 if (per_cu->cu == NULL)
14223 load_cu (per_cu);
14224 cu = per_cu->cu;
14225
14226 die = follow_die_offset (offset, &cu);
14227 if (!die)
14228 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14229 offset, per_cu->objfile->name);
14230
14231 attr = dwarf2_attr (die, DW_AT_location, cu);
14232 if (!attr)
14233 {
14234 /* DWARF: "If there is no such attribute, then there is no effect.".
14235 DATA is ignored if SIZE is 0. */
14236
14237 retval.data = NULL;
14238 retval.size = 0;
14239 }
14240 else if (attr_form_is_section_offset (attr))
14241 {
14242 struct dwarf2_loclist_baton loclist_baton;
14243 CORE_ADDR pc = (*get_frame_pc) (baton);
14244 size_t size;
14245
14246 fill_in_loclist_baton (cu, &loclist_baton, attr);
14247
14248 retval.data = dwarf2_find_location_expression (&loclist_baton,
14249 &size, pc);
14250 retval.size = size;
14251 }
14252 else
14253 {
14254 if (!attr_form_is_block (attr))
14255 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14256 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14257 offset, per_cu->objfile->name);
14258
14259 retval.data = DW_BLOCK (attr)->data;
14260 retval.size = DW_BLOCK (attr)->size;
14261 }
14262 retval.per_cu = cu->per_cu;
14263
14264 age_cached_comp_units ();
14265
14266 return retval;
14267 }
14268
14269 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14270 PER_CU. */
14271
14272 struct type *
14273 dwarf2_get_die_type (unsigned int die_offset,
14274 struct dwarf2_per_cu_data *per_cu)
14275 {
14276 dw2_setup (per_cu->objfile);
14277 return get_die_type_at_offset (die_offset, per_cu);
14278 }
14279
14280 /* Follow the signature attribute ATTR in SRC_DIE.
14281 On entry *REF_CU is the CU of SRC_DIE.
14282 On exit *REF_CU is the CU of the result. */
14283
14284 static struct die_info *
14285 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14286 struct dwarf2_cu **ref_cu)
14287 {
14288 struct objfile *objfile = (*ref_cu)->objfile;
14289 struct die_info temp_die;
14290 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14291 struct dwarf2_cu *sig_cu;
14292 struct die_info *die;
14293
14294 /* sig_type will be NULL if the signatured type is missing from
14295 the debug info. */
14296 if (sig_type == NULL)
14297 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14298 "at 0x%x [in module %s]"),
14299 src_die->offset, objfile->name);
14300
14301 /* If necessary, add it to the queue and load its DIEs. */
14302
14303 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14304 read_signatured_type (sig_type);
14305
14306 gdb_assert (sig_type->per_cu.cu != NULL);
14307
14308 sig_cu = sig_type->per_cu.cu;
14309 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14310 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14311 if (die)
14312 {
14313 *ref_cu = sig_cu;
14314 return die;
14315 }
14316
14317 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14318 "from DIE at 0x%x [in module %s]"),
14319 sig_type->type_offset, src_die->offset, objfile->name);
14320 }
14321
14322 /* Given an offset of a signatured type, return its signatured_type. */
14323
14324 static struct signatured_type *
14325 lookup_signatured_type_at_offset (struct objfile *objfile,
14326 struct dwarf2_section_info *section,
14327 unsigned int offset)
14328 {
14329 gdb_byte *info_ptr = section->buffer + offset;
14330 unsigned int length, initial_length_size;
14331 unsigned int sig_offset;
14332 struct signatured_type find_entry, *type_sig;
14333
14334 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14335 sig_offset = (initial_length_size
14336 + 2 /*version*/
14337 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14338 + 1 /*address_size*/);
14339 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14340 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14341
14342 /* This is only used to lookup previously recorded types.
14343 If we didn't find it, it's our bug. */
14344 gdb_assert (type_sig != NULL);
14345 gdb_assert (offset == type_sig->per_cu.offset);
14346
14347 return type_sig;
14348 }
14349
14350 /* Load the DIEs associated with type unit PER_CU into memory. */
14351
14352 static void
14353 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14354 {
14355 struct objfile *objfile = per_cu->objfile;
14356 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14357 unsigned int offset = per_cu->offset;
14358 struct signatured_type *type_sig;
14359
14360 dwarf2_read_section (objfile, sect);
14361
14362 /* We have the section offset, but we need the signature to do the
14363 hash table lookup. */
14364 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14365 the signature to assert we found the right one.
14366 Ok, but it's a lot of work. We should simplify things so any needed
14367 assert doesn't require all this clumsiness. */
14368 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14369
14370 gdb_assert (type_sig->per_cu.cu == NULL);
14371
14372 read_signatured_type (type_sig);
14373
14374 gdb_assert (type_sig->per_cu.cu != NULL);
14375 }
14376
14377 /* Read in a signatured type and build its CU and DIEs. */
14378
14379 static void
14380 read_signatured_type (struct signatured_type *type_sig)
14381 {
14382 struct objfile *objfile = type_sig->per_cu.objfile;
14383 gdb_byte *types_ptr;
14384 struct die_reader_specs reader_specs;
14385 struct dwarf2_cu *cu;
14386 ULONGEST signature;
14387 struct cleanup *back_to, *free_cu_cleanup;
14388 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14389
14390 dwarf2_read_section (objfile, section);
14391 types_ptr = section->buffer + type_sig->per_cu.offset;
14392
14393 gdb_assert (type_sig->per_cu.cu == NULL);
14394
14395 cu = xmalloc (sizeof (*cu));
14396 init_one_comp_unit (cu, &type_sig->per_cu);
14397
14398 /* If an error occurs while loading, release our storage. */
14399 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14400
14401 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14402 types_ptr, objfile->obfd);
14403 gdb_assert (signature == type_sig->signature);
14404
14405 cu->die_hash
14406 = htab_create_alloc_ex (cu->header.length / 12,
14407 die_hash,
14408 die_eq,
14409 NULL,
14410 &cu->comp_unit_obstack,
14411 hashtab_obstack_allocate,
14412 dummy_obstack_deallocate);
14413
14414 dwarf2_read_abbrevs (cu);
14415 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14416
14417 init_cu_die_reader (&reader_specs, cu);
14418
14419 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14420 NULL /*parent*/);
14421
14422 /* We try not to read any attributes in this function, because not
14423 all CUs needed for references have been loaded yet, and symbol
14424 table processing isn't initialized. But we have to set the CU language,
14425 or we won't be able to build types correctly. */
14426 prepare_one_comp_unit (cu, cu->dies);
14427
14428 do_cleanups (back_to);
14429
14430 /* We've successfully allocated this compilation unit. Let our caller
14431 clean it up when finished with it. */
14432 discard_cleanups (free_cu_cleanup);
14433
14434 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14435 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14436 }
14437
14438 /* Decode simple location descriptions.
14439 Given a pointer to a dwarf block that defines a location, compute
14440 the location and return the value.
14441
14442 NOTE drow/2003-11-18: This function is called in two situations
14443 now: for the address of static or global variables (partial symbols
14444 only) and for offsets into structures which are expected to be
14445 (more or less) constant. The partial symbol case should go away,
14446 and only the constant case should remain. That will let this
14447 function complain more accurately. A few special modes are allowed
14448 without complaint for global variables (for instance, global
14449 register values and thread-local values).
14450
14451 A location description containing no operations indicates that the
14452 object is optimized out. The return value is 0 for that case.
14453 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14454 callers will only want a very basic result and this can become a
14455 complaint.
14456
14457 Note that stack[0] is unused except as a default error return. */
14458
14459 static CORE_ADDR
14460 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14461 {
14462 struct objfile *objfile = cu->objfile;
14463 int i;
14464 int size = blk->size;
14465 gdb_byte *data = blk->data;
14466 CORE_ADDR stack[64];
14467 int stacki;
14468 unsigned int bytes_read, unsnd;
14469 gdb_byte op;
14470
14471 i = 0;
14472 stacki = 0;
14473 stack[stacki] = 0;
14474 stack[++stacki] = 0;
14475
14476 while (i < size)
14477 {
14478 op = data[i++];
14479 switch (op)
14480 {
14481 case DW_OP_lit0:
14482 case DW_OP_lit1:
14483 case DW_OP_lit2:
14484 case DW_OP_lit3:
14485 case DW_OP_lit4:
14486 case DW_OP_lit5:
14487 case DW_OP_lit6:
14488 case DW_OP_lit7:
14489 case DW_OP_lit8:
14490 case DW_OP_lit9:
14491 case DW_OP_lit10:
14492 case DW_OP_lit11:
14493 case DW_OP_lit12:
14494 case DW_OP_lit13:
14495 case DW_OP_lit14:
14496 case DW_OP_lit15:
14497 case DW_OP_lit16:
14498 case DW_OP_lit17:
14499 case DW_OP_lit18:
14500 case DW_OP_lit19:
14501 case DW_OP_lit20:
14502 case DW_OP_lit21:
14503 case DW_OP_lit22:
14504 case DW_OP_lit23:
14505 case DW_OP_lit24:
14506 case DW_OP_lit25:
14507 case DW_OP_lit26:
14508 case DW_OP_lit27:
14509 case DW_OP_lit28:
14510 case DW_OP_lit29:
14511 case DW_OP_lit30:
14512 case DW_OP_lit31:
14513 stack[++stacki] = op - DW_OP_lit0;
14514 break;
14515
14516 case DW_OP_reg0:
14517 case DW_OP_reg1:
14518 case DW_OP_reg2:
14519 case DW_OP_reg3:
14520 case DW_OP_reg4:
14521 case DW_OP_reg5:
14522 case DW_OP_reg6:
14523 case DW_OP_reg7:
14524 case DW_OP_reg8:
14525 case DW_OP_reg9:
14526 case DW_OP_reg10:
14527 case DW_OP_reg11:
14528 case DW_OP_reg12:
14529 case DW_OP_reg13:
14530 case DW_OP_reg14:
14531 case DW_OP_reg15:
14532 case DW_OP_reg16:
14533 case DW_OP_reg17:
14534 case DW_OP_reg18:
14535 case DW_OP_reg19:
14536 case DW_OP_reg20:
14537 case DW_OP_reg21:
14538 case DW_OP_reg22:
14539 case DW_OP_reg23:
14540 case DW_OP_reg24:
14541 case DW_OP_reg25:
14542 case DW_OP_reg26:
14543 case DW_OP_reg27:
14544 case DW_OP_reg28:
14545 case DW_OP_reg29:
14546 case DW_OP_reg30:
14547 case DW_OP_reg31:
14548 stack[++stacki] = op - DW_OP_reg0;
14549 if (i < size)
14550 dwarf2_complex_location_expr_complaint ();
14551 break;
14552
14553 case DW_OP_regx:
14554 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14555 i += bytes_read;
14556 stack[++stacki] = unsnd;
14557 if (i < size)
14558 dwarf2_complex_location_expr_complaint ();
14559 break;
14560
14561 case DW_OP_addr:
14562 stack[++stacki] = read_address (objfile->obfd, &data[i],
14563 cu, &bytes_read);
14564 i += bytes_read;
14565 break;
14566
14567 case DW_OP_const1u:
14568 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14569 i += 1;
14570 break;
14571
14572 case DW_OP_const1s:
14573 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14574 i += 1;
14575 break;
14576
14577 case DW_OP_const2u:
14578 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14579 i += 2;
14580 break;
14581
14582 case DW_OP_const2s:
14583 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14584 i += 2;
14585 break;
14586
14587 case DW_OP_const4u:
14588 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14589 i += 4;
14590 break;
14591
14592 case DW_OP_const4s:
14593 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14594 i += 4;
14595 break;
14596
14597 case DW_OP_const8u:
14598 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14599 i += 8;
14600 break;
14601
14602 case DW_OP_constu:
14603 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14604 &bytes_read);
14605 i += bytes_read;
14606 break;
14607
14608 case DW_OP_consts:
14609 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14610 i += bytes_read;
14611 break;
14612
14613 case DW_OP_dup:
14614 stack[stacki + 1] = stack[stacki];
14615 stacki++;
14616 break;
14617
14618 case DW_OP_plus:
14619 stack[stacki - 1] += stack[stacki];
14620 stacki--;
14621 break;
14622
14623 case DW_OP_plus_uconst:
14624 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14625 &bytes_read);
14626 i += bytes_read;
14627 break;
14628
14629 case DW_OP_minus:
14630 stack[stacki - 1] -= stack[stacki];
14631 stacki--;
14632 break;
14633
14634 case DW_OP_deref:
14635 /* If we're not the last op, then we definitely can't encode
14636 this using GDB's address_class enum. This is valid for partial
14637 global symbols, although the variable's address will be bogus
14638 in the psymtab. */
14639 if (i < size)
14640 dwarf2_complex_location_expr_complaint ();
14641 break;
14642
14643 case DW_OP_GNU_push_tls_address:
14644 /* The top of the stack has the offset from the beginning
14645 of the thread control block at which the variable is located. */
14646 /* Nothing should follow this operator, so the top of stack would
14647 be returned. */
14648 /* This is valid for partial global symbols, but the variable's
14649 address will be bogus in the psymtab. Make it always at least
14650 non-zero to not look as a variable garbage collected by linker
14651 which have DW_OP_addr 0. */
14652 if (i < size)
14653 dwarf2_complex_location_expr_complaint ();
14654 stack[stacki]++;
14655 break;
14656
14657 case DW_OP_GNU_uninit:
14658 break;
14659
14660 default:
14661 {
14662 const char *name = dwarf_stack_op_name (op);
14663
14664 if (name)
14665 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14666 name);
14667 else
14668 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14669 op);
14670 }
14671
14672 return (stack[stacki]);
14673 }
14674
14675 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14676 outside of the allocated space. Also enforce minimum>0. */
14677 if (stacki >= ARRAY_SIZE (stack) - 1)
14678 {
14679 complaint (&symfile_complaints,
14680 _("location description stack overflow"));
14681 return 0;
14682 }
14683
14684 if (stacki <= 0)
14685 {
14686 complaint (&symfile_complaints,
14687 _("location description stack underflow"));
14688 return 0;
14689 }
14690 }
14691 return (stack[stacki]);
14692 }
14693
14694 /* memory allocation interface */
14695
14696 static struct dwarf_block *
14697 dwarf_alloc_block (struct dwarf2_cu *cu)
14698 {
14699 struct dwarf_block *blk;
14700
14701 blk = (struct dwarf_block *)
14702 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14703 return (blk);
14704 }
14705
14706 static struct abbrev_info *
14707 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14708 {
14709 struct abbrev_info *abbrev;
14710
14711 abbrev = (struct abbrev_info *)
14712 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14713 memset (abbrev, 0, sizeof (struct abbrev_info));
14714 return (abbrev);
14715 }
14716
14717 static struct die_info *
14718 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14719 {
14720 struct die_info *die;
14721 size_t size = sizeof (struct die_info);
14722
14723 if (num_attrs > 1)
14724 size += (num_attrs - 1) * sizeof (struct attribute);
14725
14726 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14727 memset (die, 0, sizeof (struct die_info));
14728 return (die);
14729 }
14730
14731 \f
14732 /* Macro support. */
14733
14734 /* Return the full name of file number I in *LH's file name table.
14735 Use COMP_DIR as the name of the current directory of the
14736 compilation. The result is allocated using xmalloc; the caller is
14737 responsible for freeing it. */
14738 static char *
14739 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14740 {
14741 /* Is the file number a valid index into the line header's file name
14742 table? Remember that file numbers start with one, not zero. */
14743 if (1 <= file && file <= lh->num_file_names)
14744 {
14745 struct file_entry *fe = &lh->file_names[file - 1];
14746
14747 if (IS_ABSOLUTE_PATH (fe->name))
14748 return xstrdup (fe->name);
14749 else
14750 {
14751 const char *dir;
14752 int dir_len;
14753 char *full_name;
14754
14755 if (fe->dir_index)
14756 dir = lh->include_dirs[fe->dir_index - 1];
14757 else
14758 dir = comp_dir;
14759
14760 if (dir)
14761 {
14762 dir_len = strlen (dir);
14763 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14764 strcpy (full_name, dir);
14765 full_name[dir_len] = '/';
14766 strcpy (full_name + dir_len + 1, fe->name);
14767 return full_name;
14768 }
14769 else
14770 return xstrdup (fe->name);
14771 }
14772 }
14773 else
14774 {
14775 /* The compiler produced a bogus file number. We can at least
14776 record the macro definitions made in the file, even if we
14777 won't be able to find the file by name. */
14778 char fake_name[80];
14779
14780 sprintf (fake_name, "<bad macro file number %d>", file);
14781
14782 complaint (&symfile_complaints,
14783 _("bad file number in macro information (%d)"),
14784 file);
14785
14786 return xstrdup (fake_name);
14787 }
14788 }
14789
14790
14791 static struct macro_source_file *
14792 macro_start_file (int file, int line,
14793 struct macro_source_file *current_file,
14794 const char *comp_dir,
14795 struct line_header *lh, struct objfile *objfile)
14796 {
14797 /* The full name of this source file. */
14798 char *full_name = file_full_name (file, lh, comp_dir);
14799
14800 /* We don't create a macro table for this compilation unit
14801 at all until we actually get a filename. */
14802 if (! pending_macros)
14803 pending_macros = new_macro_table (&objfile->objfile_obstack,
14804 objfile->macro_cache);
14805
14806 if (! current_file)
14807 /* If we have no current file, then this must be the start_file
14808 directive for the compilation unit's main source file. */
14809 current_file = macro_set_main (pending_macros, full_name);
14810 else
14811 current_file = macro_include (current_file, line, full_name);
14812
14813 xfree (full_name);
14814
14815 return current_file;
14816 }
14817
14818
14819 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14820 followed by a null byte. */
14821 static char *
14822 copy_string (const char *buf, int len)
14823 {
14824 char *s = xmalloc (len + 1);
14825
14826 memcpy (s, buf, len);
14827 s[len] = '\0';
14828 return s;
14829 }
14830
14831
14832 static const char *
14833 consume_improper_spaces (const char *p, const char *body)
14834 {
14835 if (*p == ' ')
14836 {
14837 complaint (&symfile_complaints,
14838 _("macro definition contains spaces "
14839 "in formal argument list:\n`%s'"),
14840 body);
14841
14842 while (*p == ' ')
14843 p++;
14844 }
14845
14846 return p;
14847 }
14848
14849
14850 static void
14851 parse_macro_definition (struct macro_source_file *file, int line,
14852 const char *body)
14853 {
14854 const char *p;
14855
14856 /* The body string takes one of two forms. For object-like macro
14857 definitions, it should be:
14858
14859 <macro name> " " <definition>
14860
14861 For function-like macro definitions, it should be:
14862
14863 <macro name> "() " <definition>
14864 or
14865 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14866
14867 Spaces may appear only where explicitly indicated, and in the
14868 <definition>.
14869
14870 The Dwarf 2 spec says that an object-like macro's name is always
14871 followed by a space, but versions of GCC around March 2002 omit
14872 the space when the macro's definition is the empty string.
14873
14874 The Dwarf 2 spec says that there should be no spaces between the
14875 formal arguments in a function-like macro's formal argument list,
14876 but versions of GCC around March 2002 include spaces after the
14877 commas. */
14878
14879
14880 /* Find the extent of the macro name. The macro name is terminated
14881 by either a space or null character (for an object-like macro) or
14882 an opening paren (for a function-like macro). */
14883 for (p = body; *p; p++)
14884 if (*p == ' ' || *p == '(')
14885 break;
14886
14887 if (*p == ' ' || *p == '\0')
14888 {
14889 /* It's an object-like macro. */
14890 int name_len = p - body;
14891 char *name = copy_string (body, name_len);
14892 const char *replacement;
14893
14894 if (*p == ' ')
14895 replacement = body + name_len + 1;
14896 else
14897 {
14898 dwarf2_macro_malformed_definition_complaint (body);
14899 replacement = body + name_len;
14900 }
14901
14902 macro_define_object (file, line, name, replacement);
14903
14904 xfree (name);
14905 }
14906 else if (*p == '(')
14907 {
14908 /* It's a function-like macro. */
14909 char *name = copy_string (body, p - body);
14910 int argc = 0;
14911 int argv_size = 1;
14912 char **argv = xmalloc (argv_size * sizeof (*argv));
14913
14914 p++;
14915
14916 p = consume_improper_spaces (p, body);
14917
14918 /* Parse the formal argument list. */
14919 while (*p && *p != ')')
14920 {
14921 /* Find the extent of the current argument name. */
14922 const char *arg_start = p;
14923
14924 while (*p && *p != ',' && *p != ')' && *p != ' ')
14925 p++;
14926
14927 if (! *p || p == arg_start)
14928 dwarf2_macro_malformed_definition_complaint (body);
14929 else
14930 {
14931 /* Make sure argv has room for the new argument. */
14932 if (argc >= argv_size)
14933 {
14934 argv_size *= 2;
14935 argv = xrealloc (argv, argv_size * sizeof (*argv));
14936 }
14937
14938 argv[argc++] = copy_string (arg_start, p - arg_start);
14939 }
14940
14941 p = consume_improper_spaces (p, body);
14942
14943 /* Consume the comma, if present. */
14944 if (*p == ',')
14945 {
14946 p++;
14947
14948 p = consume_improper_spaces (p, body);
14949 }
14950 }
14951
14952 if (*p == ')')
14953 {
14954 p++;
14955
14956 if (*p == ' ')
14957 /* Perfectly formed definition, no complaints. */
14958 macro_define_function (file, line, name,
14959 argc, (const char **) argv,
14960 p + 1);
14961 else if (*p == '\0')
14962 {
14963 /* Complain, but do define it. */
14964 dwarf2_macro_malformed_definition_complaint (body);
14965 macro_define_function (file, line, name,
14966 argc, (const char **) argv,
14967 p);
14968 }
14969 else
14970 /* Just complain. */
14971 dwarf2_macro_malformed_definition_complaint (body);
14972 }
14973 else
14974 /* Just complain. */
14975 dwarf2_macro_malformed_definition_complaint (body);
14976
14977 xfree (name);
14978 {
14979 int i;
14980
14981 for (i = 0; i < argc; i++)
14982 xfree (argv[i]);
14983 }
14984 xfree (argv);
14985 }
14986 else
14987 dwarf2_macro_malformed_definition_complaint (body);
14988 }
14989
14990 /* Skip some bytes from BYTES according to the form given in FORM.
14991 Returns the new pointer. */
14992
14993 static gdb_byte *
14994 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14995 enum dwarf_form form,
14996 unsigned int offset_size,
14997 struct dwarf2_section_info *section)
14998 {
14999 unsigned int bytes_read;
15000
15001 switch (form)
15002 {
15003 case DW_FORM_data1:
15004 case DW_FORM_flag:
15005 ++bytes;
15006 break;
15007
15008 case DW_FORM_data2:
15009 bytes += 2;
15010 break;
15011
15012 case DW_FORM_data4:
15013 bytes += 4;
15014 break;
15015
15016 case DW_FORM_data8:
15017 bytes += 8;
15018 break;
15019
15020 case DW_FORM_string:
15021 read_direct_string (abfd, bytes, &bytes_read);
15022 bytes += bytes_read;
15023 break;
15024
15025 case DW_FORM_sec_offset:
15026 case DW_FORM_strp:
15027 bytes += offset_size;
15028 break;
15029
15030 case DW_FORM_block:
15031 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15032 bytes += bytes_read;
15033 break;
15034
15035 case DW_FORM_block1:
15036 bytes += 1 + read_1_byte (abfd, bytes);
15037 break;
15038 case DW_FORM_block2:
15039 bytes += 2 + read_2_bytes (abfd, bytes);
15040 break;
15041 case DW_FORM_block4:
15042 bytes += 4 + read_4_bytes (abfd, bytes);
15043 break;
15044
15045 case DW_FORM_sdata:
15046 case DW_FORM_udata:
15047 bytes = skip_leb128 (abfd, bytes);
15048 break;
15049
15050 default:
15051 {
15052 complain:
15053 complaint (&symfile_complaints,
15054 _("invalid form 0x%x in `%s'"),
15055 form,
15056 section->asection->name);
15057 return NULL;
15058 }
15059 }
15060
15061 return bytes;
15062 }
15063
15064 /* A helper for dwarf_decode_macros that handles skipping an unknown
15065 opcode. Returns an updated pointer to the macro data buffer; or,
15066 on error, issues a complaint and returns NULL. */
15067
15068 static gdb_byte *
15069 skip_unknown_opcode (unsigned int opcode,
15070 gdb_byte **opcode_definitions,
15071 gdb_byte *mac_ptr,
15072 bfd *abfd,
15073 unsigned int offset_size,
15074 struct dwarf2_section_info *section)
15075 {
15076 unsigned int bytes_read, i;
15077 unsigned long arg;
15078 gdb_byte *defn;
15079
15080 if (opcode_definitions[opcode] == NULL)
15081 {
15082 complaint (&symfile_complaints,
15083 _("unrecognized DW_MACFINO opcode 0x%x"),
15084 opcode);
15085 return NULL;
15086 }
15087
15088 defn = opcode_definitions[opcode];
15089 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15090 defn += bytes_read;
15091
15092 for (i = 0; i < arg; ++i)
15093 {
15094 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15095 if (mac_ptr == NULL)
15096 {
15097 /* skip_form_bytes already issued the complaint. */
15098 return NULL;
15099 }
15100 }
15101
15102 return mac_ptr;
15103 }
15104
15105 /* A helper function which parses the header of a macro section.
15106 If the macro section is the extended (for now called "GNU") type,
15107 then this updates *OFFSET_SIZE. Returns a pointer to just after
15108 the header, or issues a complaint and returns NULL on error. */
15109
15110 static gdb_byte *
15111 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15112 bfd *abfd,
15113 gdb_byte *mac_ptr,
15114 unsigned int *offset_size,
15115 int section_is_gnu)
15116 {
15117 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15118
15119 if (section_is_gnu)
15120 {
15121 unsigned int version, flags;
15122
15123 version = read_2_bytes (abfd, mac_ptr);
15124 if (version != 4)
15125 {
15126 complaint (&symfile_complaints,
15127 _("unrecognized version `%d' in .debug_macro section"),
15128 version);
15129 return NULL;
15130 }
15131 mac_ptr += 2;
15132
15133 flags = read_1_byte (abfd, mac_ptr);
15134 ++mac_ptr;
15135 *offset_size = (flags & 1) ? 8 : 4;
15136
15137 if ((flags & 2) != 0)
15138 /* We don't need the line table offset. */
15139 mac_ptr += *offset_size;
15140
15141 /* Vendor opcode descriptions. */
15142 if ((flags & 4) != 0)
15143 {
15144 unsigned int i, count;
15145
15146 count = read_1_byte (abfd, mac_ptr);
15147 ++mac_ptr;
15148 for (i = 0; i < count; ++i)
15149 {
15150 unsigned int opcode, bytes_read;
15151 unsigned long arg;
15152
15153 opcode = read_1_byte (abfd, mac_ptr);
15154 ++mac_ptr;
15155 opcode_definitions[opcode] = mac_ptr;
15156 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15157 mac_ptr += bytes_read;
15158 mac_ptr += arg;
15159 }
15160 }
15161 }
15162
15163 return mac_ptr;
15164 }
15165
15166 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15167 including DW_GNU_MACINFO_transparent_include. */
15168
15169 static void
15170 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15171 struct macro_source_file *current_file,
15172 struct line_header *lh, char *comp_dir,
15173 struct dwarf2_section_info *section,
15174 int section_is_gnu,
15175 unsigned int offset_size,
15176 struct objfile *objfile)
15177 {
15178 enum dwarf_macro_record_type macinfo_type;
15179 int at_commandline;
15180 gdb_byte *opcode_definitions[256];
15181
15182 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15183 &offset_size, section_is_gnu);
15184 if (mac_ptr == NULL)
15185 {
15186 /* We already issued a complaint. */
15187 return;
15188 }
15189
15190 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15191 GDB is still reading the definitions from command line. First
15192 DW_MACINFO_start_file will need to be ignored as it was already executed
15193 to create CURRENT_FILE for the main source holding also the command line
15194 definitions. On first met DW_MACINFO_start_file this flag is reset to
15195 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15196
15197 at_commandline = 1;
15198
15199 do
15200 {
15201 /* Do we at least have room for a macinfo type byte? */
15202 if (mac_ptr >= mac_end)
15203 {
15204 dwarf2_macros_too_long_complaint (section);
15205 break;
15206 }
15207
15208 macinfo_type = read_1_byte (abfd, mac_ptr);
15209 mac_ptr++;
15210
15211 /* Note that we rely on the fact that the corresponding GNU and
15212 DWARF constants are the same. */
15213 switch (macinfo_type)
15214 {
15215 /* A zero macinfo type indicates the end of the macro
15216 information. */
15217 case 0:
15218 break;
15219
15220 case DW_MACRO_GNU_define:
15221 case DW_MACRO_GNU_undef:
15222 case DW_MACRO_GNU_define_indirect:
15223 case DW_MACRO_GNU_undef_indirect:
15224 {
15225 unsigned int bytes_read;
15226 int line;
15227 char *body;
15228 int is_define;
15229
15230 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15231 mac_ptr += bytes_read;
15232
15233 if (macinfo_type == DW_MACRO_GNU_define
15234 || macinfo_type == DW_MACRO_GNU_undef)
15235 {
15236 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15237 mac_ptr += bytes_read;
15238 }
15239 else
15240 {
15241 LONGEST str_offset;
15242
15243 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15244 mac_ptr += offset_size;
15245
15246 body = read_indirect_string_at_offset (abfd, str_offset);
15247 }
15248
15249 is_define = (macinfo_type == DW_MACRO_GNU_define
15250 || macinfo_type == DW_MACRO_GNU_define_indirect);
15251 if (! current_file)
15252 {
15253 /* DWARF violation as no main source is present. */
15254 complaint (&symfile_complaints,
15255 _("debug info with no main source gives macro %s "
15256 "on line %d: %s"),
15257 is_define ? _("definition") : _("undefinition"),
15258 line, body);
15259 break;
15260 }
15261 if ((line == 0 && !at_commandline)
15262 || (line != 0 && at_commandline))
15263 complaint (&symfile_complaints,
15264 _("debug info gives %s macro %s with %s line %d: %s"),
15265 at_commandline ? _("command-line") : _("in-file"),
15266 is_define ? _("definition") : _("undefinition"),
15267 line == 0 ? _("zero") : _("non-zero"), line, body);
15268
15269 if (is_define)
15270 parse_macro_definition (current_file, line, body);
15271 else
15272 {
15273 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15274 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15275 macro_undef (current_file, line, body);
15276 }
15277 }
15278 break;
15279
15280 case DW_MACRO_GNU_start_file:
15281 {
15282 unsigned int bytes_read;
15283 int line, file;
15284
15285 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15286 mac_ptr += bytes_read;
15287 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15288 mac_ptr += bytes_read;
15289
15290 if ((line == 0 && !at_commandline)
15291 || (line != 0 && at_commandline))
15292 complaint (&symfile_complaints,
15293 _("debug info gives source %d included "
15294 "from %s at %s line %d"),
15295 file, at_commandline ? _("command-line") : _("file"),
15296 line == 0 ? _("zero") : _("non-zero"), line);
15297
15298 if (at_commandline)
15299 {
15300 /* This DW_MACRO_GNU_start_file was executed in the
15301 pass one. */
15302 at_commandline = 0;
15303 }
15304 else
15305 current_file = macro_start_file (file, line,
15306 current_file, comp_dir,
15307 lh, objfile);
15308 }
15309 break;
15310
15311 case DW_MACRO_GNU_end_file:
15312 if (! current_file)
15313 complaint (&symfile_complaints,
15314 _("macro debug info has an unmatched "
15315 "`close_file' directive"));
15316 else
15317 {
15318 current_file = current_file->included_by;
15319 if (! current_file)
15320 {
15321 enum dwarf_macro_record_type next_type;
15322
15323 /* GCC circa March 2002 doesn't produce the zero
15324 type byte marking the end of the compilation
15325 unit. Complain if it's not there, but exit no
15326 matter what. */
15327
15328 /* Do we at least have room for a macinfo type byte? */
15329 if (mac_ptr >= mac_end)
15330 {
15331 dwarf2_macros_too_long_complaint (section);
15332 return;
15333 }
15334
15335 /* We don't increment mac_ptr here, so this is just
15336 a look-ahead. */
15337 next_type = read_1_byte (abfd, mac_ptr);
15338 if (next_type != 0)
15339 complaint (&symfile_complaints,
15340 _("no terminating 0-type entry for "
15341 "macros in `.debug_macinfo' section"));
15342
15343 return;
15344 }
15345 }
15346 break;
15347
15348 case DW_MACRO_GNU_transparent_include:
15349 {
15350 LONGEST offset;
15351
15352 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15353 mac_ptr += offset_size;
15354
15355 dwarf_decode_macro_bytes (abfd,
15356 section->buffer + offset,
15357 mac_end, current_file,
15358 lh, comp_dir,
15359 section, section_is_gnu,
15360 offset_size, objfile);
15361 }
15362 break;
15363
15364 case DW_MACINFO_vendor_ext:
15365 if (!section_is_gnu)
15366 {
15367 unsigned int bytes_read;
15368 int constant;
15369
15370 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15371 mac_ptr += bytes_read;
15372 read_direct_string (abfd, mac_ptr, &bytes_read);
15373 mac_ptr += bytes_read;
15374
15375 /* We don't recognize any vendor extensions. */
15376 break;
15377 }
15378 /* FALLTHROUGH */
15379
15380 default:
15381 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15382 mac_ptr, abfd, offset_size,
15383 section);
15384 if (mac_ptr == NULL)
15385 return;
15386 break;
15387 }
15388 } while (macinfo_type != 0);
15389 }
15390
15391 static void
15392 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15393 char *comp_dir, bfd *abfd,
15394 struct dwarf2_cu *cu,
15395 struct dwarf2_section_info *section,
15396 int section_is_gnu)
15397 {
15398 struct objfile *objfile = dwarf2_per_objfile->objfile;
15399 gdb_byte *mac_ptr, *mac_end;
15400 struct macro_source_file *current_file = 0;
15401 enum dwarf_macro_record_type macinfo_type;
15402 unsigned int offset_size = cu->header.offset_size;
15403 gdb_byte *opcode_definitions[256];
15404
15405 dwarf2_read_section (objfile, section);
15406 if (section->buffer == NULL)
15407 {
15408 complaint (&symfile_complaints, _("missing %s section"),
15409 section->asection->name);
15410 return;
15411 }
15412
15413 /* First pass: Find the name of the base filename.
15414 This filename is needed in order to process all macros whose definition
15415 (or undefinition) comes from the command line. These macros are defined
15416 before the first DW_MACINFO_start_file entry, and yet still need to be
15417 associated to the base file.
15418
15419 To determine the base file name, we scan the macro definitions until we
15420 reach the first DW_MACINFO_start_file entry. We then initialize
15421 CURRENT_FILE accordingly so that any macro definition found before the
15422 first DW_MACINFO_start_file can still be associated to the base file. */
15423
15424 mac_ptr = section->buffer + offset;
15425 mac_end = section->buffer + section->size;
15426
15427 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15428 &offset_size, section_is_gnu);
15429 if (mac_ptr == NULL)
15430 {
15431 /* We already issued a complaint. */
15432 return;
15433 }
15434
15435 do
15436 {
15437 /* Do we at least have room for a macinfo type byte? */
15438 if (mac_ptr >= mac_end)
15439 {
15440 /* Complaint is printed during the second pass as GDB will probably
15441 stop the first pass earlier upon finding
15442 DW_MACINFO_start_file. */
15443 break;
15444 }
15445
15446 macinfo_type = read_1_byte (abfd, mac_ptr);
15447 mac_ptr++;
15448
15449 /* Note that we rely on the fact that the corresponding GNU and
15450 DWARF constants are the same. */
15451 switch (macinfo_type)
15452 {
15453 /* A zero macinfo type indicates the end of the macro
15454 information. */
15455 case 0:
15456 break;
15457
15458 case DW_MACRO_GNU_define:
15459 case DW_MACRO_GNU_undef:
15460 /* Only skip the data by MAC_PTR. */
15461 {
15462 unsigned int bytes_read;
15463
15464 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15465 mac_ptr += bytes_read;
15466 read_direct_string (abfd, mac_ptr, &bytes_read);
15467 mac_ptr += bytes_read;
15468 }
15469 break;
15470
15471 case DW_MACRO_GNU_start_file:
15472 {
15473 unsigned int bytes_read;
15474 int line, file;
15475
15476 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15477 mac_ptr += bytes_read;
15478 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15479 mac_ptr += bytes_read;
15480
15481 current_file = macro_start_file (file, line, current_file,
15482 comp_dir, lh, objfile);
15483 }
15484 break;
15485
15486 case DW_MACRO_GNU_end_file:
15487 /* No data to skip by MAC_PTR. */
15488 break;
15489
15490 case DW_MACRO_GNU_define_indirect:
15491 case DW_MACRO_GNU_undef_indirect:
15492 {
15493 unsigned int bytes_read;
15494
15495 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15496 mac_ptr += bytes_read;
15497 mac_ptr += offset_size;
15498 }
15499 break;
15500
15501 case DW_MACRO_GNU_transparent_include:
15502 /* Note that, according to the spec, a transparent include
15503 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15504 skip this opcode. */
15505 mac_ptr += offset_size;
15506 break;
15507
15508 case DW_MACINFO_vendor_ext:
15509 /* Only skip the data by MAC_PTR. */
15510 if (!section_is_gnu)
15511 {
15512 unsigned int bytes_read;
15513
15514 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15515 mac_ptr += bytes_read;
15516 read_direct_string (abfd, mac_ptr, &bytes_read);
15517 mac_ptr += bytes_read;
15518 }
15519 /* FALLTHROUGH */
15520
15521 default:
15522 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15523 mac_ptr, abfd, offset_size,
15524 section);
15525 if (mac_ptr == NULL)
15526 return;
15527 break;
15528 }
15529 } while (macinfo_type != 0 && current_file == NULL);
15530
15531 /* Second pass: Process all entries.
15532
15533 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15534 command-line macro definitions/undefinitions. This flag is unset when we
15535 reach the first DW_MACINFO_start_file entry. */
15536
15537 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15538 current_file, lh, comp_dir, section, section_is_gnu,
15539 offset_size, objfile);
15540 }
15541
15542 /* Check if the attribute's form is a DW_FORM_block*
15543 if so return true else false. */
15544 static int
15545 attr_form_is_block (struct attribute *attr)
15546 {
15547 return (attr == NULL ? 0 :
15548 attr->form == DW_FORM_block1
15549 || attr->form == DW_FORM_block2
15550 || attr->form == DW_FORM_block4
15551 || attr->form == DW_FORM_block
15552 || attr->form == DW_FORM_exprloc);
15553 }
15554
15555 /* Return non-zero if ATTR's value is a section offset --- classes
15556 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15557 You may use DW_UNSND (attr) to retrieve such offsets.
15558
15559 Section 7.5.4, "Attribute Encodings", explains that no attribute
15560 may have a value that belongs to more than one of these classes; it
15561 would be ambiguous if we did, because we use the same forms for all
15562 of them. */
15563 static int
15564 attr_form_is_section_offset (struct attribute *attr)
15565 {
15566 return (attr->form == DW_FORM_data4
15567 || attr->form == DW_FORM_data8
15568 || attr->form == DW_FORM_sec_offset);
15569 }
15570
15571
15572 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15573 zero otherwise. When this function returns true, you can apply
15574 dwarf2_get_attr_constant_value to it.
15575
15576 However, note that for some attributes you must check
15577 attr_form_is_section_offset before using this test. DW_FORM_data4
15578 and DW_FORM_data8 are members of both the constant class, and of
15579 the classes that contain offsets into other debug sections
15580 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15581 that, if an attribute's can be either a constant or one of the
15582 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15583 taken as section offsets, not constants. */
15584 static int
15585 attr_form_is_constant (struct attribute *attr)
15586 {
15587 switch (attr->form)
15588 {
15589 case DW_FORM_sdata:
15590 case DW_FORM_udata:
15591 case DW_FORM_data1:
15592 case DW_FORM_data2:
15593 case DW_FORM_data4:
15594 case DW_FORM_data8:
15595 return 1;
15596 default:
15597 return 0;
15598 }
15599 }
15600
15601 /* A helper function that fills in a dwarf2_loclist_baton. */
15602
15603 static void
15604 fill_in_loclist_baton (struct dwarf2_cu *cu,
15605 struct dwarf2_loclist_baton *baton,
15606 struct attribute *attr)
15607 {
15608 dwarf2_read_section (dwarf2_per_objfile->objfile,
15609 &dwarf2_per_objfile->loc);
15610
15611 baton->per_cu = cu->per_cu;
15612 gdb_assert (baton->per_cu);
15613 /* We don't know how long the location list is, but make sure we
15614 don't run off the edge of the section. */
15615 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15616 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15617 baton->base_address = cu->base_address;
15618 }
15619
15620 static void
15621 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15622 struct dwarf2_cu *cu)
15623 {
15624 struct objfile *objfile = dwarf2_per_objfile->objfile;
15625
15626 if (attr_form_is_section_offset (attr)
15627 /* ".debug_loc" may not exist at all, or the offset may be outside
15628 the section. If so, fall through to the complaint in the
15629 other branch. */
15630 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15631 &dwarf2_per_objfile->loc))
15632 {
15633 struct dwarf2_loclist_baton *baton;
15634
15635 baton = obstack_alloc (&objfile->objfile_obstack,
15636 sizeof (struct dwarf2_loclist_baton));
15637
15638 fill_in_loclist_baton (cu, baton, attr);
15639
15640 if (cu->base_known == 0)
15641 complaint (&symfile_complaints,
15642 _("Location list used without "
15643 "specifying the CU base address."));
15644
15645 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15646 SYMBOL_LOCATION_BATON (sym) = baton;
15647 }
15648 else
15649 {
15650 struct dwarf2_locexpr_baton *baton;
15651
15652 baton = obstack_alloc (&objfile->objfile_obstack,
15653 sizeof (struct dwarf2_locexpr_baton));
15654 baton->per_cu = cu->per_cu;
15655 gdb_assert (baton->per_cu);
15656
15657 if (attr_form_is_block (attr))
15658 {
15659 /* Note that we're just copying the block's data pointer
15660 here, not the actual data. We're still pointing into the
15661 info_buffer for SYM's objfile; right now we never release
15662 that buffer, but when we do clean up properly this may
15663 need to change. */
15664 baton->size = DW_BLOCK (attr)->size;
15665 baton->data = DW_BLOCK (attr)->data;
15666 }
15667 else
15668 {
15669 dwarf2_invalid_attrib_class_complaint ("location description",
15670 SYMBOL_NATURAL_NAME (sym));
15671 baton->size = 0;
15672 }
15673
15674 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15675 SYMBOL_LOCATION_BATON (sym) = baton;
15676 }
15677 }
15678
15679 /* Return the OBJFILE associated with the compilation unit CU. If CU
15680 came from a separate debuginfo file, then the master objfile is
15681 returned. */
15682
15683 struct objfile *
15684 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15685 {
15686 struct objfile *objfile = per_cu->objfile;
15687
15688 /* Return the master objfile, so that we can report and look up the
15689 correct file containing this variable. */
15690 if (objfile->separate_debug_objfile_backlink)
15691 objfile = objfile->separate_debug_objfile_backlink;
15692
15693 return objfile;
15694 }
15695
15696 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15697 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15698 CU_HEADERP first. */
15699
15700 static const struct comp_unit_head *
15701 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15702 struct dwarf2_per_cu_data *per_cu)
15703 {
15704 struct objfile *objfile;
15705 struct dwarf2_per_objfile *per_objfile;
15706 gdb_byte *info_ptr;
15707
15708 if (per_cu->cu)
15709 return &per_cu->cu->header;
15710
15711 objfile = per_cu->objfile;
15712 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15713 info_ptr = per_objfile->info.buffer + per_cu->offset;
15714
15715 memset (cu_headerp, 0, sizeof (*cu_headerp));
15716 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15717
15718 return cu_headerp;
15719 }
15720
15721 /* Return the address size given in the compilation unit header for CU. */
15722
15723 int
15724 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15725 {
15726 struct comp_unit_head cu_header_local;
15727 const struct comp_unit_head *cu_headerp;
15728
15729 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15730
15731 return cu_headerp->addr_size;
15732 }
15733
15734 /* Return the offset size given in the compilation unit header for CU. */
15735
15736 int
15737 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15738 {
15739 struct comp_unit_head cu_header_local;
15740 const struct comp_unit_head *cu_headerp;
15741
15742 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15743
15744 return cu_headerp->offset_size;
15745 }
15746
15747 /* See its dwarf2loc.h declaration. */
15748
15749 int
15750 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15751 {
15752 struct comp_unit_head cu_header_local;
15753 const struct comp_unit_head *cu_headerp;
15754
15755 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15756
15757 if (cu_headerp->version == 2)
15758 return cu_headerp->addr_size;
15759 else
15760 return cu_headerp->offset_size;
15761 }
15762
15763 /* Return the text offset of the CU. The returned offset comes from
15764 this CU's objfile. If this objfile came from a separate debuginfo
15765 file, then the offset may be different from the corresponding
15766 offset in the parent objfile. */
15767
15768 CORE_ADDR
15769 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15770 {
15771 struct objfile *objfile = per_cu->objfile;
15772
15773 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15774 }
15775
15776 /* Locate the .debug_info compilation unit from CU's objfile which contains
15777 the DIE at OFFSET. Raises an error on failure. */
15778
15779 static struct dwarf2_per_cu_data *
15780 dwarf2_find_containing_comp_unit (unsigned int offset,
15781 struct objfile *objfile)
15782 {
15783 struct dwarf2_per_cu_data *this_cu;
15784 int low, high;
15785
15786 low = 0;
15787 high = dwarf2_per_objfile->n_comp_units - 1;
15788 while (high > low)
15789 {
15790 int mid = low + (high - low) / 2;
15791
15792 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15793 high = mid;
15794 else
15795 low = mid + 1;
15796 }
15797 gdb_assert (low == high);
15798 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15799 {
15800 if (low == 0)
15801 error (_("Dwarf Error: could not find partial DIE containing "
15802 "offset 0x%lx [in module %s]"),
15803 (long) offset, bfd_get_filename (objfile->obfd));
15804
15805 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15806 return dwarf2_per_objfile->all_comp_units[low-1];
15807 }
15808 else
15809 {
15810 this_cu = dwarf2_per_objfile->all_comp_units[low];
15811 if (low == dwarf2_per_objfile->n_comp_units - 1
15812 && offset >= this_cu->offset + this_cu->length)
15813 error (_("invalid dwarf2 offset %u"), offset);
15814 gdb_assert (offset < this_cu->offset + this_cu->length);
15815 return this_cu;
15816 }
15817 }
15818
15819 /* Locate the compilation unit from OBJFILE which is located at exactly
15820 OFFSET. Raises an error on failure. */
15821
15822 static struct dwarf2_per_cu_data *
15823 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15824 {
15825 struct dwarf2_per_cu_data *this_cu;
15826
15827 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15828 if (this_cu->offset != offset)
15829 error (_("no compilation unit with offset %u."), offset);
15830 return this_cu;
15831 }
15832
15833 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15834
15835 static void
15836 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15837 {
15838 memset (cu, 0, sizeof (*cu));
15839 per_cu->cu = cu;
15840 cu->per_cu = per_cu;
15841 cu->objfile = per_cu->objfile;
15842 obstack_init (&cu->comp_unit_obstack);
15843 }
15844
15845 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15846
15847 static void
15848 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15849 {
15850 struct attribute *attr;
15851
15852 /* Set the language we're debugging. */
15853 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15854 if (attr)
15855 set_cu_language (DW_UNSND (attr), cu);
15856 else
15857 {
15858 cu->language = language_minimal;
15859 cu->language_defn = language_def (cu->language);
15860 }
15861 }
15862
15863 /* Release one cached compilation unit, CU. We unlink it from the tree
15864 of compilation units, but we don't remove it from the read_in_chain;
15865 the caller is responsible for that.
15866 NOTE: DATA is a void * because this function is also used as a
15867 cleanup routine. */
15868
15869 static void
15870 free_heap_comp_unit (void *data)
15871 {
15872 struct dwarf2_cu *cu = data;
15873
15874 gdb_assert (cu->per_cu != NULL);
15875 cu->per_cu->cu = NULL;
15876 cu->per_cu = NULL;
15877
15878 obstack_free (&cu->comp_unit_obstack, NULL);
15879
15880 xfree (cu);
15881 }
15882
15883 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15884 when we're finished with it. We can't free the pointer itself, but be
15885 sure to unlink it from the cache. Also release any associated storage
15886 and perform cache maintenance.
15887
15888 Only used during partial symbol parsing. */
15889
15890 static void
15891 free_stack_comp_unit (void *data)
15892 {
15893 struct dwarf2_cu *cu = data;
15894
15895 gdb_assert (cu->per_cu != NULL);
15896 cu->per_cu->cu = NULL;
15897 cu->per_cu = NULL;
15898
15899 obstack_free (&cu->comp_unit_obstack, NULL);
15900 cu->partial_dies = NULL;
15901
15902 /* The previous code only did this if per_cu != NULL.
15903 But that would always succeed, so now we just unconditionally do
15904 the aging. This seems like the wrong place to do such aging,
15905 but cleaning that up is left for later. */
15906 age_cached_comp_units ();
15907 }
15908
15909 /* Free all cached compilation units. */
15910
15911 static void
15912 free_cached_comp_units (void *data)
15913 {
15914 struct dwarf2_per_cu_data *per_cu, **last_chain;
15915
15916 per_cu = dwarf2_per_objfile->read_in_chain;
15917 last_chain = &dwarf2_per_objfile->read_in_chain;
15918 while (per_cu != NULL)
15919 {
15920 struct dwarf2_per_cu_data *next_cu;
15921
15922 next_cu = per_cu->cu->read_in_chain;
15923
15924 free_heap_comp_unit (per_cu->cu);
15925 *last_chain = next_cu;
15926
15927 per_cu = next_cu;
15928 }
15929 }
15930
15931 /* Increase the age counter on each cached compilation unit, and free
15932 any that are too old. */
15933
15934 static void
15935 age_cached_comp_units (void)
15936 {
15937 struct dwarf2_per_cu_data *per_cu, **last_chain;
15938
15939 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15940 per_cu = dwarf2_per_objfile->read_in_chain;
15941 while (per_cu != NULL)
15942 {
15943 per_cu->cu->last_used ++;
15944 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15945 dwarf2_mark (per_cu->cu);
15946 per_cu = per_cu->cu->read_in_chain;
15947 }
15948
15949 per_cu = dwarf2_per_objfile->read_in_chain;
15950 last_chain = &dwarf2_per_objfile->read_in_chain;
15951 while (per_cu != NULL)
15952 {
15953 struct dwarf2_per_cu_data *next_cu;
15954
15955 next_cu = per_cu->cu->read_in_chain;
15956
15957 if (!per_cu->cu->mark)
15958 {
15959 free_heap_comp_unit (per_cu->cu);
15960 *last_chain = next_cu;
15961 }
15962 else
15963 last_chain = &per_cu->cu->read_in_chain;
15964
15965 per_cu = next_cu;
15966 }
15967 }
15968
15969 /* Remove a single compilation unit from the cache. */
15970
15971 static void
15972 free_one_cached_comp_unit (void *target_cu)
15973 {
15974 struct dwarf2_per_cu_data *per_cu, **last_chain;
15975
15976 per_cu = dwarf2_per_objfile->read_in_chain;
15977 last_chain = &dwarf2_per_objfile->read_in_chain;
15978 while (per_cu != NULL)
15979 {
15980 struct dwarf2_per_cu_data *next_cu;
15981
15982 next_cu = per_cu->cu->read_in_chain;
15983
15984 if (per_cu->cu == target_cu)
15985 {
15986 free_heap_comp_unit (per_cu->cu);
15987 *last_chain = next_cu;
15988 break;
15989 }
15990 else
15991 last_chain = &per_cu->cu->read_in_chain;
15992
15993 per_cu = next_cu;
15994 }
15995 }
15996
15997 /* Release all extra memory associated with OBJFILE. */
15998
15999 void
16000 dwarf2_free_objfile (struct objfile *objfile)
16001 {
16002 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16003
16004 if (dwarf2_per_objfile == NULL)
16005 return;
16006
16007 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16008 free_cached_comp_units (NULL);
16009
16010 if (dwarf2_per_objfile->quick_file_names_table)
16011 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16012
16013 /* Everything else should be on the objfile obstack. */
16014 }
16015
16016 /* A pair of DIE offset and GDB type pointer. We store these
16017 in a hash table separate from the DIEs, and preserve them
16018 when the DIEs are flushed out of cache. */
16019
16020 struct dwarf2_offset_and_type
16021 {
16022 unsigned int offset;
16023 struct type *type;
16024 };
16025
16026 /* Hash function for a dwarf2_offset_and_type. */
16027
16028 static hashval_t
16029 offset_and_type_hash (const void *item)
16030 {
16031 const struct dwarf2_offset_and_type *ofs = item;
16032
16033 return ofs->offset;
16034 }
16035
16036 /* Equality function for a dwarf2_offset_and_type. */
16037
16038 static int
16039 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16040 {
16041 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16042 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16043
16044 return ofs_lhs->offset == ofs_rhs->offset;
16045 }
16046
16047 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16048 table if necessary. For convenience, return TYPE.
16049
16050 The DIEs reading must have careful ordering to:
16051 * Not cause infite loops trying to read in DIEs as a prerequisite for
16052 reading current DIE.
16053 * Not trying to dereference contents of still incompletely read in types
16054 while reading in other DIEs.
16055 * Enable referencing still incompletely read in types just by a pointer to
16056 the type without accessing its fields.
16057
16058 Therefore caller should follow these rules:
16059 * Try to fetch any prerequisite types we may need to build this DIE type
16060 before building the type and calling set_die_type.
16061 * After building type call set_die_type for current DIE as soon as
16062 possible before fetching more types to complete the current type.
16063 * Make the type as complete as possible before fetching more types. */
16064
16065 static struct type *
16066 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16067 {
16068 struct dwarf2_offset_and_type **slot, ofs;
16069 struct objfile *objfile = cu->objfile;
16070 htab_t *type_hash_ptr;
16071
16072 /* For Ada types, make sure that the gnat-specific data is always
16073 initialized (if not already set). There are a few types where
16074 we should not be doing so, because the type-specific area is
16075 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16076 where the type-specific area is used to store the floatformat).
16077 But this is not a problem, because the gnat-specific information
16078 is actually not needed for these types. */
16079 if (need_gnat_info (cu)
16080 && TYPE_CODE (type) != TYPE_CODE_FUNC
16081 && TYPE_CODE (type) != TYPE_CODE_FLT
16082 && !HAVE_GNAT_AUX_INFO (type))
16083 INIT_GNAT_SPECIFIC (type);
16084
16085 if (cu->per_cu->debug_types_section)
16086 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16087 else
16088 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16089
16090 if (*type_hash_ptr == NULL)
16091 {
16092 *type_hash_ptr
16093 = htab_create_alloc_ex (127,
16094 offset_and_type_hash,
16095 offset_and_type_eq,
16096 NULL,
16097 &objfile->objfile_obstack,
16098 hashtab_obstack_allocate,
16099 dummy_obstack_deallocate);
16100 }
16101
16102 ofs.offset = die->offset;
16103 ofs.type = type;
16104 slot = (struct dwarf2_offset_and_type **)
16105 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16106 if (*slot)
16107 complaint (&symfile_complaints,
16108 _("A problem internal to GDB: DIE 0x%x has type already set"),
16109 die->offset);
16110 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16111 **slot = ofs;
16112 return type;
16113 }
16114
16115 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16116 table, or return NULL if the die does not have a saved type. */
16117
16118 static struct type *
16119 get_die_type_at_offset (unsigned int offset,
16120 struct dwarf2_per_cu_data *per_cu)
16121 {
16122 struct dwarf2_offset_and_type *slot, ofs;
16123 htab_t type_hash;
16124
16125 if (per_cu->debug_types_section)
16126 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16127 else
16128 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16129 if (type_hash == NULL)
16130 return NULL;
16131
16132 ofs.offset = offset;
16133 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16134 if (slot)
16135 return slot->type;
16136 else
16137 return NULL;
16138 }
16139
16140 /* Look up the type for DIE in the appropriate type_hash table,
16141 or return NULL if DIE does not have a saved type. */
16142
16143 static struct type *
16144 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16145 {
16146 return get_die_type_at_offset (die->offset, cu->per_cu);
16147 }
16148
16149 /* Add a dependence relationship from CU to REF_PER_CU. */
16150
16151 static void
16152 dwarf2_add_dependence (struct dwarf2_cu *cu,
16153 struct dwarf2_per_cu_data *ref_per_cu)
16154 {
16155 void **slot;
16156
16157 if (cu->dependencies == NULL)
16158 cu->dependencies
16159 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16160 NULL, &cu->comp_unit_obstack,
16161 hashtab_obstack_allocate,
16162 dummy_obstack_deallocate);
16163
16164 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16165 if (*slot == NULL)
16166 *slot = ref_per_cu;
16167 }
16168
16169 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16170 Set the mark field in every compilation unit in the
16171 cache that we must keep because we are keeping CU. */
16172
16173 static int
16174 dwarf2_mark_helper (void **slot, void *data)
16175 {
16176 struct dwarf2_per_cu_data *per_cu;
16177
16178 per_cu = (struct dwarf2_per_cu_data *) *slot;
16179
16180 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16181 reading of the chain. As such dependencies remain valid it is not much
16182 useful to track and undo them during QUIT cleanups. */
16183 if (per_cu->cu == NULL)
16184 return 1;
16185
16186 if (per_cu->cu->mark)
16187 return 1;
16188 per_cu->cu->mark = 1;
16189
16190 if (per_cu->cu->dependencies != NULL)
16191 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16192
16193 return 1;
16194 }
16195
16196 /* Set the mark field in CU and in every other compilation unit in the
16197 cache that we must keep because we are keeping CU. */
16198
16199 static void
16200 dwarf2_mark (struct dwarf2_cu *cu)
16201 {
16202 if (cu->mark)
16203 return;
16204 cu->mark = 1;
16205 if (cu->dependencies != NULL)
16206 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16207 }
16208
16209 static void
16210 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16211 {
16212 while (per_cu)
16213 {
16214 per_cu->cu->mark = 0;
16215 per_cu = per_cu->cu->read_in_chain;
16216 }
16217 }
16218
16219 /* Trivial hash function for partial_die_info: the hash value of a DIE
16220 is its offset in .debug_info for this objfile. */
16221
16222 static hashval_t
16223 partial_die_hash (const void *item)
16224 {
16225 const struct partial_die_info *part_die = item;
16226
16227 return part_die->offset;
16228 }
16229
16230 /* Trivial comparison function for partial_die_info structures: two DIEs
16231 are equal if they have the same offset. */
16232
16233 static int
16234 partial_die_eq (const void *item_lhs, const void *item_rhs)
16235 {
16236 const struct partial_die_info *part_die_lhs = item_lhs;
16237 const struct partial_die_info *part_die_rhs = item_rhs;
16238
16239 return part_die_lhs->offset == part_die_rhs->offset;
16240 }
16241
16242 static struct cmd_list_element *set_dwarf2_cmdlist;
16243 static struct cmd_list_element *show_dwarf2_cmdlist;
16244
16245 static void
16246 set_dwarf2_cmd (char *args, int from_tty)
16247 {
16248 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16249 }
16250
16251 static void
16252 show_dwarf2_cmd (char *args, int from_tty)
16253 {
16254 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16255 }
16256
16257 /* If section described by INFO was mmapped, munmap it now. */
16258
16259 static void
16260 munmap_section_buffer (struct dwarf2_section_info *info)
16261 {
16262 if (info->map_addr != NULL)
16263 {
16264 #ifdef HAVE_MMAP
16265 int res;
16266
16267 res = munmap (info->map_addr, info->map_len);
16268 gdb_assert (res == 0);
16269 #else
16270 /* Without HAVE_MMAP, we should never be here to begin with. */
16271 gdb_assert_not_reached ("no mmap support");
16272 #endif
16273 }
16274 }
16275
16276 /* munmap debug sections for OBJFILE, if necessary. */
16277
16278 static void
16279 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16280 {
16281 struct dwarf2_per_objfile *data = d;
16282 int ix;
16283 struct dwarf2_section_info *section;
16284
16285 /* This is sorted according to the order they're defined in to make it easier
16286 to keep in sync. */
16287 munmap_section_buffer (&data->info);
16288 munmap_section_buffer (&data->abbrev);
16289 munmap_section_buffer (&data->line);
16290 munmap_section_buffer (&data->loc);
16291 munmap_section_buffer (&data->macinfo);
16292 munmap_section_buffer (&data->macro);
16293 munmap_section_buffer (&data->str);
16294 munmap_section_buffer (&data->ranges);
16295 munmap_section_buffer (&data->frame);
16296 munmap_section_buffer (&data->eh_frame);
16297 munmap_section_buffer (&data->gdb_index);
16298
16299 for (ix = 0;
16300 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16301 ++ix)
16302 munmap_section_buffer (section);
16303
16304 VEC_free (dwarf2_section_info_def, data->types);
16305 }
16306
16307 \f
16308 /* The "save gdb-index" command. */
16309
16310 /* The contents of the hash table we create when building the string
16311 table. */
16312 struct strtab_entry
16313 {
16314 offset_type offset;
16315 const char *str;
16316 };
16317
16318 /* Hash function for a strtab_entry.
16319
16320 Function is used only during write_hash_table so no index format backward
16321 compatibility is needed. */
16322
16323 static hashval_t
16324 hash_strtab_entry (const void *e)
16325 {
16326 const struct strtab_entry *entry = e;
16327 return mapped_index_string_hash (INT_MAX, entry->str);
16328 }
16329
16330 /* Equality function for a strtab_entry. */
16331
16332 static int
16333 eq_strtab_entry (const void *a, const void *b)
16334 {
16335 const struct strtab_entry *ea = a;
16336 const struct strtab_entry *eb = b;
16337 return !strcmp (ea->str, eb->str);
16338 }
16339
16340 /* Create a strtab_entry hash table. */
16341
16342 static htab_t
16343 create_strtab (void)
16344 {
16345 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16346 xfree, xcalloc, xfree);
16347 }
16348
16349 /* Add a string to the constant pool. Return the string's offset in
16350 host order. */
16351
16352 static offset_type
16353 add_string (htab_t table, struct obstack *cpool, const char *str)
16354 {
16355 void **slot;
16356 struct strtab_entry entry;
16357 struct strtab_entry *result;
16358
16359 entry.str = str;
16360 slot = htab_find_slot (table, &entry, INSERT);
16361 if (*slot)
16362 result = *slot;
16363 else
16364 {
16365 result = XNEW (struct strtab_entry);
16366 result->offset = obstack_object_size (cpool);
16367 result->str = str;
16368 obstack_grow_str0 (cpool, str);
16369 *slot = result;
16370 }
16371 return result->offset;
16372 }
16373
16374 /* An entry in the symbol table. */
16375 struct symtab_index_entry
16376 {
16377 /* The name of the symbol. */
16378 const char *name;
16379 /* The offset of the name in the constant pool. */
16380 offset_type index_offset;
16381 /* A sorted vector of the indices of all the CUs that hold an object
16382 of this name. */
16383 VEC (offset_type) *cu_indices;
16384 };
16385
16386 /* The symbol table. This is a power-of-2-sized hash table. */
16387 struct mapped_symtab
16388 {
16389 offset_type n_elements;
16390 offset_type size;
16391 struct symtab_index_entry **data;
16392 };
16393
16394 /* Hash function for a symtab_index_entry. */
16395
16396 static hashval_t
16397 hash_symtab_entry (const void *e)
16398 {
16399 const struct symtab_index_entry *entry = e;
16400 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16401 sizeof (offset_type) * VEC_length (offset_type,
16402 entry->cu_indices),
16403 0);
16404 }
16405
16406 /* Equality function for a symtab_index_entry. */
16407
16408 static int
16409 eq_symtab_entry (const void *a, const void *b)
16410 {
16411 const struct symtab_index_entry *ea = a;
16412 const struct symtab_index_entry *eb = b;
16413 int len = VEC_length (offset_type, ea->cu_indices);
16414 if (len != VEC_length (offset_type, eb->cu_indices))
16415 return 0;
16416 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16417 VEC_address (offset_type, eb->cu_indices),
16418 sizeof (offset_type) * len);
16419 }
16420
16421 /* Destroy a symtab_index_entry. */
16422
16423 static void
16424 delete_symtab_entry (void *p)
16425 {
16426 struct symtab_index_entry *entry = p;
16427 VEC_free (offset_type, entry->cu_indices);
16428 xfree (entry);
16429 }
16430
16431 /* Create a hash table holding symtab_index_entry objects. */
16432
16433 static htab_t
16434 create_symbol_hash_table (void)
16435 {
16436 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16437 delete_symtab_entry, xcalloc, xfree);
16438 }
16439
16440 /* Create a new mapped symtab object. */
16441
16442 static struct mapped_symtab *
16443 create_mapped_symtab (void)
16444 {
16445 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16446 symtab->n_elements = 0;
16447 symtab->size = 1024;
16448 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16449 return symtab;
16450 }
16451
16452 /* Destroy a mapped_symtab. */
16453
16454 static void
16455 cleanup_mapped_symtab (void *p)
16456 {
16457 struct mapped_symtab *symtab = p;
16458 /* The contents of the array are freed when the other hash table is
16459 destroyed. */
16460 xfree (symtab->data);
16461 xfree (symtab);
16462 }
16463
16464 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16465 the slot.
16466
16467 Function is used only during write_hash_table so no index format backward
16468 compatibility is needed. */
16469
16470 static struct symtab_index_entry **
16471 find_slot (struct mapped_symtab *symtab, const char *name)
16472 {
16473 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16474
16475 index = hash & (symtab->size - 1);
16476 step = ((hash * 17) & (symtab->size - 1)) | 1;
16477
16478 for (;;)
16479 {
16480 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16481 return &symtab->data[index];
16482 index = (index + step) & (symtab->size - 1);
16483 }
16484 }
16485
16486 /* Expand SYMTAB's hash table. */
16487
16488 static void
16489 hash_expand (struct mapped_symtab *symtab)
16490 {
16491 offset_type old_size = symtab->size;
16492 offset_type i;
16493 struct symtab_index_entry **old_entries = symtab->data;
16494
16495 symtab->size *= 2;
16496 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16497
16498 for (i = 0; i < old_size; ++i)
16499 {
16500 if (old_entries[i])
16501 {
16502 struct symtab_index_entry **slot = find_slot (symtab,
16503 old_entries[i]->name);
16504 *slot = old_entries[i];
16505 }
16506 }
16507
16508 xfree (old_entries);
16509 }
16510
16511 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16512 is the index of the CU in which the symbol appears. */
16513
16514 static void
16515 add_index_entry (struct mapped_symtab *symtab, const char *name,
16516 offset_type cu_index)
16517 {
16518 struct symtab_index_entry **slot;
16519
16520 ++symtab->n_elements;
16521 if (4 * symtab->n_elements / 3 >= symtab->size)
16522 hash_expand (symtab);
16523
16524 slot = find_slot (symtab, name);
16525 if (!*slot)
16526 {
16527 *slot = XNEW (struct symtab_index_entry);
16528 (*slot)->name = name;
16529 (*slot)->cu_indices = NULL;
16530 }
16531 /* Don't push an index twice. Due to how we add entries we only
16532 have to check the last one. */
16533 if (VEC_empty (offset_type, (*slot)->cu_indices)
16534 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16535 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16536 }
16537
16538 /* Add a vector of indices to the constant pool. */
16539
16540 static offset_type
16541 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16542 struct symtab_index_entry *entry)
16543 {
16544 void **slot;
16545
16546 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16547 if (!*slot)
16548 {
16549 offset_type len = VEC_length (offset_type, entry->cu_indices);
16550 offset_type val = MAYBE_SWAP (len);
16551 offset_type iter;
16552 int i;
16553
16554 *slot = entry;
16555 entry->index_offset = obstack_object_size (cpool);
16556
16557 obstack_grow (cpool, &val, sizeof (val));
16558 for (i = 0;
16559 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16560 ++i)
16561 {
16562 val = MAYBE_SWAP (iter);
16563 obstack_grow (cpool, &val, sizeof (val));
16564 }
16565 }
16566 else
16567 {
16568 struct symtab_index_entry *old_entry = *slot;
16569 entry->index_offset = old_entry->index_offset;
16570 entry = old_entry;
16571 }
16572 return entry->index_offset;
16573 }
16574
16575 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16576 constant pool entries going into the obstack CPOOL. */
16577
16578 static void
16579 write_hash_table (struct mapped_symtab *symtab,
16580 struct obstack *output, struct obstack *cpool)
16581 {
16582 offset_type i;
16583 htab_t symbol_hash_table;
16584 htab_t str_table;
16585
16586 symbol_hash_table = create_symbol_hash_table ();
16587 str_table = create_strtab ();
16588
16589 /* We add all the index vectors to the constant pool first, to
16590 ensure alignment is ok. */
16591 for (i = 0; i < symtab->size; ++i)
16592 {
16593 if (symtab->data[i])
16594 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16595 }
16596
16597 /* Now write out the hash table. */
16598 for (i = 0; i < symtab->size; ++i)
16599 {
16600 offset_type str_off, vec_off;
16601
16602 if (symtab->data[i])
16603 {
16604 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16605 vec_off = symtab->data[i]->index_offset;
16606 }
16607 else
16608 {
16609 /* While 0 is a valid constant pool index, it is not valid
16610 to have 0 for both offsets. */
16611 str_off = 0;
16612 vec_off = 0;
16613 }
16614
16615 str_off = MAYBE_SWAP (str_off);
16616 vec_off = MAYBE_SWAP (vec_off);
16617
16618 obstack_grow (output, &str_off, sizeof (str_off));
16619 obstack_grow (output, &vec_off, sizeof (vec_off));
16620 }
16621
16622 htab_delete (str_table);
16623 htab_delete (symbol_hash_table);
16624 }
16625
16626 /* Struct to map psymtab to CU index in the index file. */
16627 struct psymtab_cu_index_map
16628 {
16629 struct partial_symtab *psymtab;
16630 unsigned int cu_index;
16631 };
16632
16633 static hashval_t
16634 hash_psymtab_cu_index (const void *item)
16635 {
16636 const struct psymtab_cu_index_map *map = item;
16637
16638 return htab_hash_pointer (map->psymtab);
16639 }
16640
16641 static int
16642 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16643 {
16644 const struct psymtab_cu_index_map *lhs = item_lhs;
16645 const struct psymtab_cu_index_map *rhs = item_rhs;
16646
16647 return lhs->psymtab == rhs->psymtab;
16648 }
16649
16650 /* Helper struct for building the address table. */
16651 struct addrmap_index_data
16652 {
16653 struct objfile *objfile;
16654 struct obstack *addr_obstack;
16655 htab_t cu_index_htab;
16656
16657 /* Non-zero if the previous_* fields are valid.
16658 We can't write an entry until we see the next entry (since it is only then
16659 that we know the end of the entry). */
16660 int previous_valid;
16661 /* Index of the CU in the table of all CUs in the index file. */
16662 unsigned int previous_cu_index;
16663 /* Start address of the CU. */
16664 CORE_ADDR previous_cu_start;
16665 };
16666
16667 /* Write an address entry to OBSTACK. */
16668
16669 static void
16670 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16671 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16672 {
16673 offset_type cu_index_to_write;
16674 char addr[8];
16675 CORE_ADDR baseaddr;
16676
16677 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16678
16679 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16680 obstack_grow (obstack, addr, 8);
16681 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16682 obstack_grow (obstack, addr, 8);
16683 cu_index_to_write = MAYBE_SWAP (cu_index);
16684 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16685 }
16686
16687 /* Worker function for traversing an addrmap to build the address table. */
16688
16689 static int
16690 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16691 {
16692 struct addrmap_index_data *data = datap;
16693 struct partial_symtab *pst = obj;
16694 offset_type cu_index;
16695 void **slot;
16696
16697 if (data->previous_valid)
16698 add_address_entry (data->objfile, data->addr_obstack,
16699 data->previous_cu_start, start_addr,
16700 data->previous_cu_index);
16701
16702 data->previous_cu_start = start_addr;
16703 if (pst != NULL)
16704 {
16705 struct psymtab_cu_index_map find_map, *map;
16706 find_map.psymtab = pst;
16707 map = htab_find (data->cu_index_htab, &find_map);
16708 gdb_assert (map != NULL);
16709 data->previous_cu_index = map->cu_index;
16710 data->previous_valid = 1;
16711 }
16712 else
16713 data->previous_valid = 0;
16714
16715 return 0;
16716 }
16717
16718 /* Write OBJFILE's address map to OBSTACK.
16719 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16720 in the index file. */
16721
16722 static void
16723 write_address_map (struct objfile *objfile, struct obstack *obstack,
16724 htab_t cu_index_htab)
16725 {
16726 struct addrmap_index_data addrmap_index_data;
16727
16728 /* When writing the address table, we have to cope with the fact that
16729 the addrmap iterator only provides the start of a region; we have to
16730 wait until the next invocation to get the start of the next region. */
16731
16732 addrmap_index_data.objfile = objfile;
16733 addrmap_index_data.addr_obstack = obstack;
16734 addrmap_index_data.cu_index_htab = cu_index_htab;
16735 addrmap_index_data.previous_valid = 0;
16736
16737 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16738 &addrmap_index_data);
16739
16740 /* It's highly unlikely the last entry (end address = 0xff...ff)
16741 is valid, but we should still handle it.
16742 The end address is recorded as the start of the next region, but that
16743 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16744 anyway. */
16745 if (addrmap_index_data.previous_valid)
16746 add_address_entry (objfile, obstack,
16747 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16748 addrmap_index_data.previous_cu_index);
16749 }
16750
16751 /* Add a list of partial symbols to SYMTAB. */
16752
16753 static void
16754 write_psymbols (struct mapped_symtab *symtab,
16755 htab_t psyms_seen,
16756 struct partial_symbol **psymp,
16757 int count,
16758 offset_type cu_index,
16759 int is_static)
16760 {
16761 for (; count-- > 0; ++psymp)
16762 {
16763 void **slot, *lookup;
16764
16765 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16766 error (_("Ada is not currently supported by the index"));
16767
16768 /* We only want to add a given psymbol once. However, we also
16769 want to account for whether it is global or static. So, we
16770 may add it twice, using slightly different values. */
16771 if (is_static)
16772 {
16773 uintptr_t val = 1 | (uintptr_t) *psymp;
16774
16775 lookup = (void *) val;
16776 }
16777 else
16778 lookup = *psymp;
16779
16780 /* Only add a given psymbol once. */
16781 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16782 if (!*slot)
16783 {
16784 *slot = lookup;
16785 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16786 }
16787 }
16788 }
16789
16790 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16791 exception if there is an error. */
16792
16793 static void
16794 write_obstack (FILE *file, struct obstack *obstack)
16795 {
16796 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16797 file)
16798 != obstack_object_size (obstack))
16799 error (_("couldn't data write to file"));
16800 }
16801
16802 /* Unlink a file if the argument is not NULL. */
16803
16804 static void
16805 unlink_if_set (void *p)
16806 {
16807 char **filename = p;
16808 if (*filename)
16809 unlink (*filename);
16810 }
16811
16812 /* A helper struct used when iterating over debug_types. */
16813 struct signatured_type_index_data
16814 {
16815 struct objfile *objfile;
16816 struct mapped_symtab *symtab;
16817 struct obstack *types_list;
16818 htab_t psyms_seen;
16819 int cu_index;
16820 };
16821
16822 /* A helper function that writes a single signatured_type to an
16823 obstack. */
16824
16825 static int
16826 write_one_signatured_type (void **slot, void *d)
16827 {
16828 struct signatured_type_index_data *info = d;
16829 struct signatured_type *entry = (struct signatured_type *) *slot;
16830 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16831 struct partial_symtab *psymtab = per_cu->v.psymtab;
16832 gdb_byte val[8];
16833
16834 write_psymbols (info->symtab,
16835 info->psyms_seen,
16836 info->objfile->global_psymbols.list
16837 + psymtab->globals_offset,
16838 psymtab->n_global_syms, info->cu_index,
16839 0);
16840 write_psymbols (info->symtab,
16841 info->psyms_seen,
16842 info->objfile->static_psymbols.list
16843 + psymtab->statics_offset,
16844 psymtab->n_static_syms, info->cu_index,
16845 1);
16846
16847 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16848 obstack_grow (info->types_list, val, 8);
16849 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16850 obstack_grow (info->types_list, val, 8);
16851 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16852 obstack_grow (info->types_list, val, 8);
16853
16854 ++info->cu_index;
16855
16856 return 1;
16857 }
16858
16859 /* Create an index file for OBJFILE in the directory DIR. */
16860
16861 static void
16862 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16863 {
16864 struct cleanup *cleanup;
16865 char *filename, *cleanup_filename;
16866 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16867 struct obstack cu_list, types_cu_list;
16868 int i;
16869 FILE *out_file;
16870 struct mapped_symtab *symtab;
16871 offset_type val, size_of_contents, total_len;
16872 struct stat st;
16873 char buf[8];
16874 htab_t psyms_seen;
16875 htab_t cu_index_htab;
16876 struct psymtab_cu_index_map *psymtab_cu_index_map;
16877
16878 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16879 return;
16880
16881 if (dwarf2_per_objfile->using_index)
16882 error (_("Cannot use an index to create the index"));
16883
16884 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16885 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16886
16887 if (stat (objfile->name, &st) < 0)
16888 perror_with_name (objfile->name);
16889
16890 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16891 INDEX_SUFFIX, (char *) NULL);
16892 cleanup = make_cleanup (xfree, filename);
16893
16894 out_file = fopen (filename, "wb");
16895 if (!out_file)
16896 error (_("Can't open `%s' for writing"), filename);
16897
16898 cleanup_filename = filename;
16899 make_cleanup (unlink_if_set, &cleanup_filename);
16900
16901 symtab = create_mapped_symtab ();
16902 make_cleanup (cleanup_mapped_symtab, symtab);
16903
16904 obstack_init (&addr_obstack);
16905 make_cleanup_obstack_free (&addr_obstack);
16906
16907 obstack_init (&cu_list);
16908 make_cleanup_obstack_free (&cu_list);
16909
16910 obstack_init (&types_cu_list);
16911 make_cleanup_obstack_free (&types_cu_list);
16912
16913 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16914 NULL, xcalloc, xfree);
16915 make_cleanup_htab_delete (psyms_seen);
16916
16917 /* While we're scanning CU's create a table that maps a psymtab pointer
16918 (which is what addrmap records) to its index (which is what is recorded
16919 in the index file). This will later be needed to write the address
16920 table. */
16921 cu_index_htab = htab_create_alloc (100,
16922 hash_psymtab_cu_index,
16923 eq_psymtab_cu_index,
16924 NULL, xcalloc, xfree);
16925 make_cleanup_htab_delete (cu_index_htab);
16926 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16927 xmalloc (sizeof (struct psymtab_cu_index_map)
16928 * dwarf2_per_objfile->n_comp_units);
16929 make_cleanup (xfree, psymtab_cu_index_map);
16930
16931 /* The CU list is already sorted, so we don't need to do additional
16932 work here. Also, the debug_types entries do not appear in
16933 all_comp_units, but only in their own hash table. */
16934 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16935 {
16936 struct dwarf2_per_cu_data *per_cu
16937 = dwarf2_per_objfile->all_comp_units[i];
16938 struct partial_symtab *psymtab = per_cu->v.psymtab;
16939 gdb_byte val[8];
16940 struct psymtab_cu_index_map *map;
16941 void **slot;
16942
16943 write_psymbols (symtab,
16944 psyms_seen,
16945 objfile->global_psymbols.list + psymtab->globals_offset,
16946 psymtab->n_global_syms, i,
16947 0);
16948 write_psymbols (symtab,
16949 psyms_seen,
16950 objfile->static_psymbols.list + psymtab->statics_offset,
16951 psymtab->n_static_syms, i,
16952 1);
16953
16954 map = &psymtab_cu_index_map[i];
16955 map->psymtab = psymtab;
16956 map->cu_index = i;
16957 slot = htab_find_slot (cu_index_htab, map, INSERT);
16958 gdb_assert (slot != NULL);
16959 gdb_assert (*slot == NULL);
16960 *slot = map;
16961
16962 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16963 obstack_grow (&cu_list, val, 8);
16964 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16965 obstack_grow (&cu_list, val, 8);
16966 }
16967
16968 /* Dump the address map. */
16969 write_address_map (objfile, &addr_obstack, cu_index_htab);
16970
16971 /* Write out the .debug_type entries, if any. */
16972 if (dwarf2_per_objfile->signatured_types)
16973 {
16974 struct signatured_type_index_data sig_data;
16975
16976 sig_data.objfile = objfile;
16977 sig_data.symtab = symtab;
16978 sig_data.types_list = &types_cu_list;
16979 sig_data.psyms_seen = psyms_seen;
16980 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16981 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16982 write_one_signatured_type, &sig_data);
16983 }
16984
16985 obstack_init (&constant_pool);
16986 make_cleanup_obstack_free (&constant_pool);
16987 obstack_init (&symtab_obstack);
16988 make_cleanup_obstack_free (&symtab_obstack);
16989 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16990
16991 obstack_init (&contents);
16992 make_cleanup_obstack_free (&contents);
16993 size_of_contents = 6 * sizeof (offset_type);
16994 total_len = size_of_contents;
16995
16996 /* The version number. */
16997 val = MAYBE_SWAP (5);
16998 obstack_grow (&contents, &val, sizeof (val));
16999
17000 /* The offset of the CU list from the start of the file. */
17001 val = MAYBE_SWAP (total_len);
17002 obstack_grow (&contents, &val, sizeof (val));
17003 total_len += obstack_object_size (&cu_list);
17004
17005 /* The offset of the types CU list from the start of the file. */
17006 val = MAYBE_SWAP (total_len);
17007 obstack_grow (&contents, &val, sizeof (val));
17008 total_len += obstack_object_size (&types_cu_list);
17009
17010 /* The offset of the address table from the start of the file. */
17011 val = MAYBE_SWAP (total_len);
17012 obstack_grow (&contents, &val, sizeof (val));
17013 total_len += obstack_object_size (&addr_obstack);
17014
17015 /* The offset of the symbol table from the start of the file. */
17016 val = MAYBE_SWAP (total_len);
17017 obstack_grow (&contents, &val, sizeof (val));
17018 total_len += obstack_object_size (&symtab_obstack);
17019
17020 /* The offset of the constant pool from the start of the file. */
17021 val = MAYBE_SWAP (total_len);
17022 obstack_grow (&contents, &val, sizeof (val));
17023 total_len += obstack_object_size (&constant_pool);
17024
17025 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17026
17027 write_obstack (out_file, &contents);
17028 write_obstack (out_file, &cu_list);
17029 write_obstack (out_file, &types_cu_list);
17030 write_obstack (out_file, &addr_obstack);
17031 write_obstack (out_file, &symtab_obstack);
17032 write_obstack (out_file, &constant_pool);
17033
17034 fclose (out_file);
17035
17036 /* We want to keep the file, so we set cleanup_filename to NULL
17037 here. See unlink_if_set. */
17038 cleanup_filename = NULL;
17039
17040 do_cleanups (cleanup);
17041 }
17042
17043 /* Implementation of the `save gdb-index' command.
17044
17045 Note that the file format used by this command is documented in the
17046 GDB manual. Any changes here must be documented there. */
17047
17048 static void
17049 save_gdb_index_command (char *arg, int from_tty)
17050 {
17051 struct objfile *objfile;
17052
17053 if (!arg || !*arg)
17054 error (_("usage: save gdb-index DIRECTORY"));
17055
17056 ALL_OBJFILES (objfile)
17057 {
17058 struct stat st;
17059
17060 /* If the objfile does not correspond to an actual file, skip it. */
17061 if (stat (objfile->name, &st) < 0)
17062 continue;
17063
17064 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17065 if (dwarf2_per_objfile)
17066 {
17067 volatile struct gdb_exception except;
17068
17069 TRY_CATCH (except, RETURN_MASK_ERROR)
17070 {
17071 write_psymtabs_to_index (objfile, arg);
17072 }
17073 if (except.reason < 0)
17074 exception_fprintf (gdb_stderr, except,
17075 _("Error while writing index for `%s': "),
17076 objfile->name);
17077 }
17078 }
17079 }
17080
17081 \f
17082
17083 int dwarf2_always_disassemble;
17084
17085 static void
17086 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17087 struct cmd_list_element *c, const char *value)
17088 {
17089 fprintf_filtered (file,
17090 _("Whether to always disassemble "
17091 "DWARF expressions is %s.\n"),
17092 value);
17093 }
17094
17095 static void
17096 show_check_physname (struct ui_file *file, int from_tty,
17097 struct cmd_list_element *c, const char *value)
17098 {
17099 fprintf_filtered (file,
17100 _("Whether to check \"physname\" is %s.\n"),
17101 value);
17102 }
17103
17104 void _initialize_dwarf2_read (void);
17105
17106 void
17107 _initialize_dwarf2_read (void)
17108 {
17109 struct cmd_list_element *c;
17110
17111 dwarf2_objfile_data_key
17112 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17113
17114 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17115 Set DWARF 2 specific variables.\n\
17116 Configure DWARF 2 variables such as the cache size"),
17117 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17118 0/*allow-unknown*/, &maintenance_set_cmdlist);
17119
17120 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17121 Show DWARF 2 specific variables\n\
17122 Show DWARF 2 variables such as the cache size"),
17123 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17124 0/*allow-unknown*/, &maintenance_show_cmdlist);
17125
17126 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17127 &dwarf2_max_cache_age, _("\
17128 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17129 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17130 A higher limit means that cached compilation units will be stored\n\
17131 in memory longer, and more total memory will be used. Zero disables\n\
17132 caching, which can slow down startup."),
17133 NULL,
17134 show_dwarf2_max_cache_age,
17135 &set_dwarf2_cmdlist,
17136 &show_dwarf2_cmdlist);
17137
17138 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17139 &dwarf2_always_disassemble, _("\
17140 Set whether `info address' always disassembles DWARF expressions."), _("\
17141 Show whether `info address' always disassembles DWARF expressions."), _("\
17142 When enabled, DWARF expressions are always printed in an assembly-like\n\
17143 syntax. When disabled, expressions will be printed in a more\n\
17144 conversational style, when possible."),
17145 NULL,
17146 show_dwarf2_always_disassemble,
17147 &set_dwarf2_cmdlist,
17148 &show_dwarf2_cmdlist);
17149
17150 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17151 Set debugging of the dwarf2 DIE reader."), _("\
17152 Show debugging of the dwarf2 DIE reader."), _("\
17153 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17154 The value is the maximum depth to print."),
17155 NULL,
17156 NULL,
17157 &setdebuglist, &showdebuglist);
17158
17159 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17160 Set cross-checking of \"physname\" code against demangler."), _("\
17161 Show cross-checking of \"physname\" code against demangler."), _("\
17162 When enabled, GDB's internal \"physname\" code is checked against\n\
17163 the demangler."),
17164 NULL, show_check_physname,
17165 &setdebuglist, &showdebuglist);
17166
17167 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17168 _("\
17169 Save a gdb-index file.\n\
17170 Usage: save gdb-index DIRECTORY"),
17171 &save_cmdlist);
17172 set_cmd_completer (c, filename_completer);
17173 }
This page took 0.526959 seconds and 4 git commands to generate.