* dwarf2read.c (dwarf2_per_objfile): New member quick_file_names_table.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.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 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
137 /* True if we have tried to read this section. */
138 int readin;
139 };
140
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149 struct mapped_index
150 {
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
178
179 /* Back link. */
180 struct objfile *objfile;
181
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
198
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
206
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
209 unsigned char using_index;
210
211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
212 struct mapped_index *index_table;
213
214 /* When using index_table, this keeps track of all quick_file_names entries.
215 TUs can share line table entries with CUs or other TUs, and there can be
216 a lot more TUs than unique line tables, so we maintain a separate table
217 of all line table entries to support the sharing. */
218 htab_t quick_file_names_table;
219
220 /* Set during partial symbol reading, to prevent queueing of full
221 symbols. */
222 int reading_partial_symbols;
223
224 /* Table mapping type .debug_info DIE offsets to types.
225 This is NULL if not allocated yet.
226 It (currently) makes sense to allocate debug_types_type_hash lazily.
227 To keep things simple we allocate both lazily. */
228 htab_t debug_info_type_hash;
229
230 /* Table mapping type .debug_types DIE offsets to types.
231 This is NULL if not allocated yet. */
232 htab_t debug_types_type_hash;
233 };
234
235 static struct dwarf2_per_objfile *dwarf2_per_objfile;
236
237 /* names of the debugging sections */
238
239 /* Note that if the debugging section has been compressed, it might
240 have a name like .zdebug_info. */
241
242 #define INFO_SECTION "debug_info"
243 #define ABBREV_SECTION "debug_abbrev"
244 #define LINE_SECTION "debug_line"
245 #define LOC_SECTION "debug_loc"
246 #define MACINFO_SECTION "debug_macinfo"
247 #define STR_SECTION "debug_str"
248 #define RANGES_SECTION "debug_ranges"
249 #define TYPES_SECTION "debug_types"
250 #define FRAME_SECTION "debug_frame"
251 #define EH_FRAME_SECTION "eh_frame"
252 #define GDB_INDEX_SECTION "gdb_index"
253
254 /* local data types */
255
256 /* We hold several abbreviation tables in memory at the same time. */
257 #ifndef ABBREV_HASH_SIZE
258 #define ABBREV_HASH_SIZE 121
259 #endif
260
261 /* The data in a compilation unit header, after target2host
262 translation, looks like this. */
263 struct comp_unit_head
264 {
265 unsigned int length;
266 short version;
267 unsigned char addr_size;
268 unsigned char signed_addr_p;
269 unsigned int abbrev_offset;
270
271 /* Size of file offsets; either 4 or 8. */
272 unsigned int offset_size;
273
274 /* Size of the length field; either 4 or 12. */
275 unsigned int initial_length_size;
276
277 /* Offset to the first byte of this compilation unit header in the
278 .debug_info section, for resolving relative reference dies. */
279 unsigned int offset;
280
281 /* Offset to first die in this cu from the start of the cu.
282 This will be the first byte following the compilation unit header. */
283 unsigned int first_die_offset;
284 };
285
286 /* Type used for delaying computation of method physnames.
287 See comments for compute_delayed_physnames. */
288 struct delayed_method_info
289 {
290 /* The type to which the method is attached, i.e., its parent class. */
291 struct type *type;
292
293 /* The index of the method in the type's function fieldlists. */
294 int fnfield_index;
295
296 /* The index of the method in the fieldlist. */
297 int index;
298
299 /* The name of the DIE. */
300 const char *name;
301
302 /* The DIE associated with this method. */
303 struct die_info *die;
304 };
305
306 typedef struct delayed_method_info delayed_method_info;
307 DEF_VEC_O (delayed_method_info);
308
309 /* Internal state when decoding a particular compilation unit. */
310 struct dwarf2_cu
311 {
312 /* The objfile containing this compilation unit. */
313 struct objfile *objfile;
314
315 /* The header of the compilation unit. */
316 struct comp_unit_head header;
317
318 /* Base address of this compilation unit. */
319 CORE_ADDR base_address;
320
321 /* Non-zero if base_address has been set. */
322 int base_known;
323
324 struct function_range *first_fn, *last_fn, *cached_fn;
325
326 /* The language we are debugging. */
327 enum language language;
328 const struct language_defn *language_defn;
329
330 const char *producer;
331
332 /* The generic symbol table building routines have separate lists for
333 file scope symbols and all all other scopes (local scopes). So
334 we need to select the right one to pass to add_symbol_to_list().
335 We do it by keeping a pointer to the correct list in list_in_scope.
336
337 FIXME: The original dwarf code just treated the file scope as the
338 first local scope, and all other local scopes as nested local
339 scopes, and worked fine. Check to see if we really need to
340 distinguish these in buildsym.c. */
341 struct pending **list_in_scope;
342
343 /* DWARF abbreviation table associated with this compilation unit. */
344 struct abbrev_info **dwarf2_abbrevs;
345
346 /* Storage for the abbrev table. */
347 struct obstack abbrev_obstack;
348
349 /* Hash table holding all the loaded partial DIEs. */
350 htab_t partial_dies;
351
352 /* Storage for things with the same lifetime as this read-in compilation
353 unit, including partial DIEs. */
354 struct obstack comp_unit_obstack;
355
356 /* When multiple dwarf2_cu structures are living in memory, this field
357 chains them all together, so that they can be released efficiently.
358 We will probably also want a generation counter so that most-recently-used
359 compilation units are cached... */
360 struct dwarf2_per_cu_data *read_in_chain;
361
362 /* Backchain to our per_cu entry if the tree has been built. */
363 struct dwarf2_per_cu_data *per_cu;
364
365 /* How many compilation units ago was this CU last referenced? */
366 int last_used;
367
368 /* A hash table of die offsets for following references. */
369 htab_t die_hash;
370
371 /* Full DIEs if read in. */
372 struct die_info *dies;
373
374 /* A set of pointers to dwarf2_per_cu_data objects for compilation
375 units referenced by this one. Only set during full symbol processing;
376 partial symbol tables do not have dependencies. */
377 htab_t dependencies;
378
379 /* Header data from the line table, during full symbol processing. */
380 struct line_header *line_header;
381
382 /* A list of methods which need to have physnames computed
383 after all type information has been read. */
384 VEC (delayed_method_info) *method_list;
385
386 /* Mark used when releasing cached dies. */
387 unsigned int mark : 1;
388
389 /* This flag will be set if this compilation unit might include
390 inter-compilation-unit references. */
391 unsigned int has_form_ref_addr : 1;
392
393 /* This flag will be set if this compilation unit includes any
394 DW_TAG_namespace DIEs. If we know that there are explicit
395 DIEs for namespaces, we don't need to try to infer them
396 from mangled names. */
397 unsigned int has_namespace_info : 1;
398 };
399
400 /* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
402 read_symtab_private field of the psymtab. If we encounter
403 inter-compilation-unit references, we also maintain a sorted
404 list of all compilation units. */
405
406 struct dwarf2_per_cu_data
407 {
408 /* The start offset and length of this compilation unit. 2**29-1
409 bytes should suffice to store the length of any compilation unit
410 - if it doesn't, GDB will fall over anyway.
411 NOTE: Unlike comp_unit_head.length, this length includes
412 initial_length_size. */
413 unsigned int offset;
414 unsigned int length : 29;
415
416 /* Flag indicating this compilation unit will be read in before
417 any of the current compilation units are processed. */
418 unsigned int queued : 1;
419
420 /* This flag will be set if we need to load absolutely all DIEs
421 for this compilation unit, instead of just the ones we think
422 are interesting. It gets set if we look for a DIE in the
423 hash table and don't find it. */
424 unsigned int load_all_dies : 1;
425
426 /* Non-zero if this CU is from .debug_types.
427 Otherwise it's from .debug_info. */
428 unsigned int from_debug_types : 1;
429
430 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
431 of the CU cache it gets reset to NULL again. */
432 struct dwarf2_cu *cu;
433
434 /* The corresponding objfile. */
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
449 };
450
451 /* Entry in the signatured_types hash table. */
452
453 struct signatured_type
454 {
455 ULONGEST signature;
456
457 /* Offset in .debug_types of the TU (type_unit) for this type. */
458 unsigned int offset;
459
460 /* Offset in .debug_types of the type defined by this TU. */
461 unsigned int type_offset;
462
463 /* The CU(/TU) of this type. */
464 struct dwarf2_per_cu_data per_cu;
465 };
466
467 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
468 which are used for both .debug_info and .debug_types dies.
469 All parameters here are unchanging for the life of the call.
470 This struct exists to abstract away the constant parameters of
471 die reading. */
472
473 struct die_reader_specs
474 {
475 /* The bfd of this objfile. */
476 bfd* abfd;
477
478 /* The CU of the DIE we are parsing. */
479 struct dwarf2_cu *cu;
480
481 /* Pointer to start of section buffer.
482 This is either the start of .debug_info or .debug_types. */
483 const gdb_byte *buffer;
484 };
485
486 /* The line number information for a compilation unit (found in the
487 .debug_line section) begins with a "statement program header",
488 which contains the following information. */
489 struct line_header
490 {
491 unsigned int total_length;
492 unsigned short version;
493 unsigned int header_length;
494 unsigned char minimum_instruction_length;
495 unsigned char maximum_ops_per_instruction;
496 unsigned char default_is_stmt;
497 int line_base;
498 unsigned char line_range;
499 unsigned char opcode_base;
500
501 /* standard_opcode_lengths[i] is the number of operands for the
502 standard opcode whose value is i. This means that
503 standard_opcode_lengths[0] is unused, and the last meaningful
504 element is standard_opcode_lengths[opcode_base - 1]. */
505 unsigned char *standard_opcode_lengths;
506
507 /* The include_directories table. NOTE! These strings are not
508 allocated with xmalloc; instead, they are pointers into
509 debug_line_buffer. If you try to free them, `free' will get
510 indigestion. */
511 unsigned int num_include_dirs, include_dirs_size;
512 char **include_dirs;
513
514 /* The file_names table. NOTE! These strings are not allocated
515 with xmalloc; instead, they are pointers into debug_line_buffer.
516 Don't try to free them directly. */
517 unsigned int num_file_names, file_names_size;
518 struct file_entry
519 {
520 char *name;
521 unsigned int dir_index;
522 unsigned int mod_time;
523 unsigned int length;
524 int included_p; /* Non-zero if referenced by the Line Number Program. */
525 struct symtab *symtab; /* The associated symbol table, if any. */
526 } *file_names;
527
528 /* The start and end of the statement program following this
529 header. These point into dwarf2_per_objfile->line_buffer. */
530 gdb_byte *statement_program_start, *statement_program_end;
531 };
532
533 /* When we construct a partial symbol table entry we only
534 need this much information. */
535 struct partial_die_info
536 {
537 /* Offset of this DIE. */
538 unsigned int offset;
539
540 /* DWARF-2 tag for this DIE. */
541 ENUM_BITFIELD(dwarf_tag) tag : 16;
542
543 /* Assorted flags describing the data found in this DIE. */
544 unsigned int has_children : 1;
545 unsigned int is_external : 1;
546 unsigned int is_declaration : 1;
547 unsigned int has_type : 1;
548 unsigned int has_specification : 1;
549 unsigned int has_pc_info : 1;
550
551 /* Flag set if the SCOPE field of this structure has been
552 computed. */
553 unsigned int scope_set : 1;
554
555 /* Flag set if the DIE has a byte_size attribute. */
556 unsigned int has_byte_size : 1;
557
558 /* Flag set if any of the DIE's children are template arguments. */
559 unsigned int has_template_arguments : 1;
560
561 /* Flag set if fixup_partial_die has been called on this die. */
562 unsigned int fixup_called : 1;
563
564 /* The name of this DIE. Normally the value of DW_AT_name, but
565 sometimes a default name for unnamed DIEs. */
566 char *name;
567
568 /* The linkage name, if present. */
569 const char *linkage_name;
570
571 /* The scope to prepend to our children. This is generally
572 allocated on the comp_unit_obstack, so will disappear
573 when this compilation unit leaves the cache. */
574 char *scope;
575
576 /* The location description associated with this DIE, if any. */
577 struct dwarf_block *locdesc;
578
579 /* If HAS_PC_INFO, the PC range associated with this DIE. */
580 CORE_ADDR lowpc;
581 CORE_ADDR highpc;
582
583 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
584 DW_AT_sibling, if any. */
585 /* NOTE: This member isn't strictly necessary, read_partial_die could
586 return DW_AT_sibling values to its caller load_partial_dies. */
587 gdb_byte *sibling;
588
589 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590 DW_AT_specification (or DW_AT_abstract_origin or
591 DW_AT_extension). */
592 unsigned int spec_offset;
593
594 /* Pointers to this DIE's parent, first child, and next sibling,
595 if any. */
596 struct partial_die_info *die_parent, *die_child, *die_sibling;
597 };
598
599 /* This data structure holds the information of an abbrev. */
600 struct abbrev_info
601 {
602 unsigned int number; /* number identifying abbrev */
603 enum dwarf_tag tag; /* dwarf tag */
604 unsigned short has_children; /* boolean */
605 unsigned short num_attrs; /* number of attributes */
606 struct attr_abbrev *attrs; /* an array of attribute descriptions */
607 struct abbrev_info *next; /* next in chain */
608 };
609
610 struct attr_abbrev
611 {
612 ENUM_BITFIELD(dwarf_attribute) name : 16;
613 ENUM_BITFIELD(dwarf_form) form : 16;
614 };
615
616 /* Attributes have a name and a value */
617 struct attribute
618 {
619 ENUM_BITFIELD(dwarf_attribute) name : 16;
620 ENUM_BITFIELD(dwarf_form) form : 15;
621
622 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
623 field should be in u.str (existing only for DW_STRING) but it is kept
624 here for better struct attribute alignment. */
625 unsigned int string_is_canonical : 1;
626
627 union
628 {
629 char *str;
630 struct dwarf_block *blk;
631 ULONGEST unsnd;
632 LONGEST snd;
633 CORE_ADDR addr;
634 struct signatured_type *signatured_type;
635 }
636 u;
637 };
638
639 /* This data structure holds a complete die structure. */
640 struct die_info
641 {
642 /* DWARF-2 tag for this DIE. */
643 ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645 /* Number of attributes */
646 unsigned char num_attrs;
647
648 /* True if we're presently building the full type name for the
649 type derived from this DIE. */
650 unsigned char building_fullname : 1;
651
652 /* Abbrev number */
653 unsigned int abbrev;
654
655 /* Offset in .debug_info or .debug_types section. */
656 unsigned int offset;
657
658 /* The dies in a compilation unit form an n-ary tree. PARENT
659 points to this die's parent; CHILD points to the first child of
660 this node; and all the children of a given node are chained
661 together via their SIBLING fields. */
662 struct die_info *child; /* Its first child, if any. */
663 struct die_info *sibling; /* Its next sibling, if any. */
664 struct die_info *parent; /* Its parent, if any. */
665
666 /* An array of attributes, with NUM_ATTRS elements. There may be
667 zero, but it's not common and zero-sized arrays are not
668 sufficiently portable C. */
669 struct attribute attrs[1];
670 };
671
672 struct function_range
673 {
674 const char *name;
675 CORE_ADDR lowpc, highpc;
676 int seen_line;
677 struct function_range *next;
678 };
679
680 /* Get at parts of an attribute structure */
681
682 #define DW_STRING(attr) ((attr)->u.str)
683 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
684 #define DW_UNSND(attr) ((attr)->u.unsnd)
685 #define DW_BLOCK(attr) ((attr)->u.blk)
686 #define DW_SND(attr) ((attr)->u.snd)
687 #define DW_ADDR(attr) ((attr)->u.addr)
688 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
689
690 /* Blocks are a bunch of untyped bytes. */
691 struct dwarf_block
692 {
693 unsigned int size;
694 gdb_byte *data;
695 };
696
697 #ifndef ATTR_ALLOC_CHUNK
698 #define ATTR_ALLOC_CHUNK 4
699 #endif
700
701 /* Allocate fields for structs, unions and enums in this size. */
702 #ifndef DW_FIELD_ALLOC_CHUNK
703 #define DW_FIELD_ALLOC_CHUNK 4
704 #endif
705
706 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707 but this would require a corresponding change in unpack_field_as_long
708 and friends. */
709 static int bits_per_byte = 8;
710
711 /* The routines that read and process dies for a C struct or C++ class
712 pass lists of data member fields and lists of member function fields
713 in an instance of a field_info structure, as defined below. */
714 struct field_info
715 {
716 /* List of data member and baseclasses fields. */
717 struct nextfield
718 {
719 struct nextfield *next;
720 int accessibility;
721 int virtuality;
722 struct field field;
723 }
724 *fields, *baseclasses;
725
726 /* Number of fields (including baseclasses). */
727 int nfields;
728
729 /* Number of baseclasses. */
730 int nbaseclasses;
731
732 /* Set if the accesibility of one of the fields is not public. */
733 int non_public_fields;
734
735 /* Member function fields array, entries are allocated in the order they
736 are encountered in the object file. */
737 struct nextfnfield
738 {
739 struct nextfnfield *next;
740 struct fn_field fnfield;
741 }
742 *fnfields;
743
744 /* Member function fieldlist array, contains name of possibly overloaded
745 member function, number of overloaded member functions and a pointer
746 to the head of the member function field chain. */
747 struct fnfieldlist
748 {
749 char *name;
750 int length;
751 struct nextfnfield *head;
752 }
753 *fnfieldlists;
754
755 /* Number of entries in the fnfieldlists array. */
756 int nfnfields;
757
758 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
759 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
760 struct typedef_field_list
761 {
762 struct typedef_field field;
763 struct typedef_field_list *next;
764 }
765 *typedef_field_list;
766 unsigned typedef_field_list_count;
767 };
768
769 /* One item on the queue of compilation units to read in full symbols
770 for. */
771 struct dwarf2_queue_item
772 {
773 struct dwarf2_per_cu_data *per_cu;
774 struct dwarf2_queue_item *next;
775 };
776
777 /* The current queue. */
778 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
780 /* Loaded secondary compilation units are kept in memory until they
781 have not been referenced for the processing of this many
782 compilation units. Set this to zero to disable caching. Cache
783 sizes of up to at least twenty will improve startup time for
784 typical inter-CU-reference binaries, at an obvious memory cost. */
785 static int dwarf2_max_cache_age = 5;
786 static void
787 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789 {
790 fprintf_filtered (file, _("\
791 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
792 value);
793 }
794
795
796 /* Various complaints about symbol reading that don't abort the process */
797
798 static void
799 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
800 {
801 complaint (&symfile_complaints,
802 _("statement list doesn't fit in .debug_line section"));
803 }
804
805 static void
806 dwarf2_debug_line_missing_file_complaint (void)
807 {
808 complaint (&symfile_complaints,
809 _(".debug_line section has line data without a file"));
810 }
811
812 static void
813 dwarf2_debug_line_missing_end_sequence_complaint (void)
814 {
815 complaint (&symfile_complaints,
816 _(".debug_line section has line program sequence without an end"));
817 }
818
819 static void
820 dwarf2_complex_location_expr_complaint (void)
821 {
822 complaint (&symfile_complaints, _("location expression too complex"));
823 }
824
825 static void
826 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
827 int arg3)
828 {
829 complaint (&symfile_complaints,
830 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
831 arg2, arg3);
832 }
833
834 static void
835 dwarf2_macros_too_long_complaint (void)
836 {
837 complaint (&symfile_complaints,
838 _("macro info runs off end of `.debug_macinfo' section"));
839 }
840
841 static void
842 dwarf2_macro_malformed_definition_complaint (const char *arg1)
843 {
844 complaint (&symfile_complaints,
845 _("macro debug info contains a malformed macro definition:\n`%s'"),
846 arg1);
847 }
848
849 static void
850 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
851 {
852 complaint (&symfile_complaints,
853 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
854 }
855
856 /* local function prototypes */
857
858 static void dwarf2_locate_sections (bfd *, asection *, void *);
859
860 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
861 struct objfile *);
862
863 static void dwarf2_build_psymtabs_hard (struct objfile *);
864
865 static void scan_partial_symbols (struct partial_die_info *,
866 CORE_ADDR *, CORE_ADDR *,
867 int, struct dwarf2_cu *);
868
869 static void add_partial_symbol (struct partial_die_info *,
870 struct dwarf2_cu *);
871
872 static void add_partial_namespace (struct partial_die_info *pdi,
873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
874 int need_pc, struct dwarf2_cu *cu);
875
876 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
877 CORE_ADDR *highpc, int need_pc,
878 struct dwarf2_cu *cu);
879
880 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
881 struct dwarf2_cu *cu);
882
883 static void add_partial_subprogram (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
885 int need_pc, struct dwarf2_cu *cu);
886
887 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
888 gdb_byte *buffer, gdb_byte *info_ptr,
889 bfd *abfd, struct dwarf2_cu *cu);
890
891 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
892
893 static void psymtab_to_symtab_1 (struct partial_symtab *);
894
895 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
896
897 static void dwarf2_free_abbrev_table (void *);
898
899 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
900 struct dwarf2_cu *);
901
902 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
903 struct dwarf2_cu *);
904
905 static struct partial_die_info *load_partial_dies (bfd *,
906 gdb_byte *, gdb_byte *,
907 int, struct dwarf2_cu *);
908
909 static gdb_byte *read_partial_die (struct partial_die_info *,
910 struct abbrev_info *abbrev,
911 unsigned int, bfd *,
912 gdb_byte *, gdb_byte *,
913 struct dwarf2_cu *);
914
915 static struct partial_die_info *find_partial_die (unsigned int,
916 struct dwarf2_cu *);
917
918 static void fixup_partial_die (struct partial_die_info *,
919 struct dwarf2_cu *);
920
921 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
922 bfd *, gdb_byte *, struct dwarf2_cu *);
923
924 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
925 bfd *, gdb_byte *, struct dwarf2_cu *);
926
927 static unsigned int read_1_byte (bfd *, gdb_byte *);
928
929 static int read_1_signed_byte (bfd *, gdb_byte *);
930
931 static unsigned int read_2_bytes (bfd *, gdb_byte *);
932
933 static unsigned int read_4_bytes (bfd *, gdb_byte *);
934
935 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
936
937 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
938 unsigned int *);
939
940 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
941
942 static LONGEST read_checked_initial_length_and_offset
943 (bfd *, gdb_byte *, const struct comp_unit_head *,
944 unsigned int *, unsigned int *);
945
946 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
947 unsigned int *);
948
949 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
950
951 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
952
953 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
954
955 static char *read_indirect_string (bfd *, gdb_byte *,
956 const struct comp_unit_head *,
957 unsigned int *);
958
959 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
960
961 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
962
963 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
964
965 static void set_cu_language (unsigned int, struct dwarf2_cu *);
966
967 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
968 struct dwarf2_cu *);
969
970 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
971 unsigned int,
972 struct dwarf2_cu *);
973
974 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
975 struct dwarf2_cu *cu);
976
977 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
978
979 static struct die_info *die_specification (struct die_info *die,
980 struct dwarf2_cu **);
981
982 static void free_line_header (struct line_header *lh);
983
984 static void add_file_name (struct line_header *, char *, unsigned int,
985 unsigned int, unsigned int);
986
987 static struct line_header *(dwarf_decode_line_header
988 (unsigned int offset,
989 bfd *abfd, struct dwarf2_cu *cu));
990
991 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
992 struct dwarf2_cu *, struct partial_symtab *);
993
994 static void dwarf2_start_subfile (char *, const char *, const char *);
995
996 static struct symbol *new_symbol (struct die_info *, struct type *,
997 struct dwarf2_cu *);
998
999 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1000 struct dwarf2_cu *, struct symbol *);
1001
1002 static void dwarf2_const_value (struct attribute *, struct symbol *,
1003 struct dwarf2_cu *);
1004
1005 static void dwarf2_const_value_attr (struct attribute *attr,
1006 struct type *type,
1007 const char *name,
1008 struct obstack *obstack,
1009 struct dwarf2_cu *cu, long *value,
1010 gdb_byte **bytes,
1011 struct dwarf2_locexpr_baton **baton);
1012
1013 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1014
1015 static int need_gnat_info (struct dwarf2_cu *);
1016
1017 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1018
1019 static void set_descriptive_type (struct type *, struct die_info *,
1020 struct dwarf2_cu *);
1021
1022 static struct type *die_containing_type (struct die_info *,
1023 struct dwarf2_cu *);
1024
1025 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026 struct dwarf2_cu *);
1027
1028 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1029
1030 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
1032 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1033
1034 static char *typename_concat (struct obstack *obs, const char *prefix,
1035 const char *suffix, int physname,
1036 struct dwarf2_cu *cu);
1037
1038 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1039
1040 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
1042 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1047 struct dwarf2_cu *, struct partial_symtab *);
1048
1049 static int dwarf2_get_pc_bounds (struct die_info *,
1050 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1051 struct partial_symtab *);
1052
1053 static void get_scope_pc_bounds (struct die_info *,
1054 CORE_ADDR *, CORE_ADDR *,
1055 struct dwarf2_cu *);
1056
1057 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1058 CORE_ADDR, struct dwarf2_cu *);
1059
1060 static void dwarf2_add_field (struct field_info *, struct die_info *,
1061 struct dwarf2_cu *);
1062
1063 static void dwarf2_attach_fields_to_type (struct field_info *,
1064 struct type *, struct dwarf2_cu *);
1065
1066 static void dwarf2_add_member_fn (struct field_info *,
1067 struct die_info *, struct type *,
1068 struct dwarf2_cu *);
1069
1070 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1071 struct type *, struct dwarf2_cu *);
1072
1073 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1074
1075 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1078
1079 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1080
1081 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1082
1083 static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
1086 static const char *namespace_name (struct die_info *die,
1087 int *is_anonymous, struct dwarf2_cu *);
1088
1089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1090
1091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1092
1093 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1094 struct dwarf2_cu *);
1095
1096 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1097
1098 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1099 gdb_byte *info_ptr,
1100 gdb_byte **new_info_ptr,
1101 struct die_info *parent);
1102
1103 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
1105 gdb_byte **new_info_ptr,
1106 struct die_info *parent);
1107
1108 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
1110 gdb_byte **new_info_ptr,
1111 struct die_info *parent);
1112
1113 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1114 struct die_info **, gdb_byte *,
1115 int *);
1116
1117 static void process_die (struct die_info *, struct dwarf2_cu *);
1118
1119 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1120 struct obstack *);
1121
1122 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1123
1124 static const char *dwarf2_full_name (char *name,
1125 struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
1128 static struct die_info *dwarf2_extension (struct die_info *die,
1129 struct dwarf2_cu **);
1130
1131 static char *dwarf_tag_name (unsigned int);
1132
1133 static char *dwarf_attr_name (unsigned int);
1134
1135 static char *dwarf_form_name (unsigned int);
1136
1137 static char *dwarf_bool_name (unsigned int);
1138
1139 static char *dwarf_type_encoding_name (unsigned int);
1140
1141 #if 0
1142 static char *dwarf_cfi_name (unsigned int);
1143 #endif
1144
1145 static struct die_info *sibling_die (struct die_info *);
1146
1147 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1148
1149 static void dump_die_for_error (struct die_info *);
1150
1151 static void dump_die_1 (struct ui_file *, int level, int max_level,
1152 struct die_info *);
1153
1154 /*static*/ void dump_die (struct die_info *, int max_level);
1155
1156 static void store_in_ref_table (struct die_info *,
1157 struct dwarf2_cu *);
1158
1159 static int is_ref_attr (struct attribute *);
1160
1161 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1162
1163 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1164
1165 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1166 struct attribute *,
1167 struct dwarf2_cu **);
1168
1169 static struct die_info *follow_die_ref (struct die_info *,
1170 struct attribute *,
1171 struct dwarf2_cu **);
1172
1173 static struct die_info *follow_die_sig (struct die_info *,
1174 struct attribute *,
1175 struct dwarf2_cu **);
1176
1177 static void read_signatured_type_at_offset (struct objfile *objfile,
1178 unsigned int offset);
1179
1180 static void read_signatured_type (struct objfile *,
1181 struct signatured_type *type_sig);
1182
1183 /* memory allocation interface */
1184
1185 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1186
1187 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1188
1189 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1190
1191 static void initialize_cu_func_list (struct dwarf2_cu *);
1192
1193 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1194 struct dwarf2_cu *);
1195
1196 static void dwarf_decode_macros (struct line_header *, unsigned int,
1197 char *, bfd *, struct dwarf2_cu *);
1198
1199 static int attr_form_is_block (struct attribute *);
1200
1201 static int attr_form_is_section_offset (struct attribute *);
1202
1203 static int attr_form_is_constant (struct attribute *);
1204
1205 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1206 struct dwarf2_loclist_baton *baton,
1207 struct attribute *attr);
1208
1209 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1210 struct symbol *sym,
1211 struct dwarf2_cu *cu);
1212
1213 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1214 struct abbrev_info *abbrev,
1215 struct dwarf2_cu *cu);
1216
1217 static void free_stack_comp_unit (void *);
1218
1219 static hashval_t partial_die_hash (const void *item);
1220
1221 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1222
1223 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1224 (unsigned int offset, struct objfile *objfile);
1225
1226 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1227 (unsigned int offset, struct objfile *objfile);
1228
1229 static void init_one_comp_unit (struct dwarf2_cu *cu,
1230 struct objfile *objfile);
1231
1232 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233 struct die_info *comp_unit_die);
1234
1235 static void free_one_comp_unit (void *);
1236
1237 static void free_cached_comp_units (void *);
1238
1239 static void age_cached_comp_units (void);
1240
1241 static void free_one_cached_comp_unit (void *);
1242
1243 static struct type *set_die_type (struct die_info *, struct type *,
1244 struct dwarf2_cu *);
1245
1246 static void create_all_comp_units (struct objfile *);
1247
1248 static int create_debug_types_hash_table (struct objfile *objfile);
1249
1250 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1251 struct objfile *);
1252
1253 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1254
1255 static void dwarf2_add_dependence (struct dwarf2_cu *,
1256 struct dwarf2_per_cu_data *);
1257
1258 static void dwarf2_mark (struct dwarf2_cu *);
1259
1260 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1261
1262 static struct type *get_die_type_at_offset (unsigned int,
1263 struct dwarf2_per_cu_data *per_cu);
1264
1265 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1266
1267 static void dwarf2_release_queue (void *dummy);
1268
1269 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1270 struct objfile *objfile);
1271
1272 static void process_queue (struct objfile *objfile);
1273
1274 static void find_file_and_directory (struct die_info *die,
1275 struct dwarf2_cu *cu,
1276 char **name, char **comp_dir);
1277
1278 static char *file_full_name (int file, struct line_header *lh,
1279 const char *comp_dir);
1280
1281 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1282 gdb_byte *info_ptr,
1283 gdb_byte *buffer,
1284 unsigned int buffer_size,
1285 bfd *abfd);
1286
1287 static void init_cu_die_reader (struct die_reader_specs *reader,
1288 struct dwarf2_cu *cu);
1289
1290 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1291
1292 #if WORDS_BIGENDIAN
1293
1294 /* Convert VALUE between big- and little-endian. */
1295 static offset_type
1296 byte_swap (offset_type value)
1297 {
1298 offset_type result;
1299
1300 result = (value & 0xff) << 24;
1301 result |= (value & 0xff00) << 8;
1302 result |= (value & 0xff0000) >> 8;
1303 result |= (value & 0xff000000) >> 24;
1304 return result;
1305 }
1306
1307 #define MAYBE_SWAP(V) byte_swap (V)
1308
1309 #else
1310 #define MAYBE_SWAP(V) (V)
1311 #endif /* WORDS_BIGENDIAN */
1312
1313 /* The suffix for an index file. */
1314 #define INDEX_SUFFIX ".gdb-index"
1315
1316 static const char *dwarf2_physname (char *name, struct die_info *die,
1317 struct dwarf2_cu *cu);
1318
1319 /* Try to locate the sections we need for DWARF 2 debugging
1320 information and return true if we have enough to do something. */
1321
1322 int
1323 dwarf2_has_info (struct objfile *objfile)
1324 {
1325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326 if (!dwarf2_per_objfile)
1327 {
1328 /* Initialize per-objfile state. */
1329 struct dwarf2_per_objfile *data
1330 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1331
1332 memset (data, 0, sizeof (*data));
1333 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334 dwarf2_per_objfile = data;
1335
1336 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1337 dwarf2_per_objfile->objfile = objfile;
1338 }
1339 return (dwarf2_per_objfile->info.asection != NULL
1340 && dwarf2_per_objfile->abbrev.asection != NULL);
1341 }
1342
1343 /* When loading sections, we can either look for ".<name>", or for
1344 * ".z<name>", which indicates a compressed section. */
1345
1346 static int
1347 section_is_p (const char *section_name, const char *name)
1348 {
1349 return (section_name[0] == '.'
1350 && (strcmp (section_name + 1, name) == 0
1351 || (section_name[1] == 'z'
1352 && strcmp (section_name + 2, name) == 0)));
1353 }
1354
1355 /* This function is mapped across the sections and remembers the
1356 offset and size of each of the debugging sections we are interested
1357 in. */
1358
1359 static void
1360 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1361 {
1362 if (section_is_p (sectp->name, INFO_SECTION))
1363 {
1364 dwarf2_per_objfile->info.asection = sectp;
1365 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1366 }
1367 else if (section_is_p (sectp->name, ABBREV_SECTION))
1368 {
1369 dwarf2_per_objfile->abbrev.asection = sectp;
1370 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1371 }
1372 else if (section_is_p (sectp->name, LINE_SECTION))
1373 {
1374 dwarf2_per_objfile->line.asection = sectp;
1375 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1376 }
1377 else if (section_is_p (sectp->name, LOC_SECTION))
1378 {
1379 dwarf2_per_objfile->loc.asection = sectp;
1380 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1381 }
1382 else if (section_is_p (sectp->name, MACINFO_SECTION))
1383 {
1384 dwarf2_per_objfile->macinfo.asection = sectp;
1385 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1386 }
1387 else if (section_is_p (sectp->name, STR_SECTION))
1388 {
1389 dwarf2_per_objfile->str.asection = sectp;
1390 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1391 }
1392 else if (section_is_p (sectp->name, FRAME_SECTION))
1393 {
1394 dwarf2_per_objfile->frame.asection = sectp;
1395 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1396 }
1397 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1398 {
1399 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1400
1401 if (aflag & SEC_HAS_CONTENTS)
1402 {
1403 dwarf2_per_objfile->eh_frame.asection = sectp;
1404 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1405 }
1406 }
1407 else if (section_is_p (sectp->name, RANGES_SECTION))
1408 {
1409 dwarf2_per_objfile->ranges.asection = sectp;
1410 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1411 }
1412 else if (section_is_p (sectp->name, TYPES_SECTION))
1413 {
1414 dwarf2_per_objfile->types.asection = sectp;
1415 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1416 }
1417 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1418 {
1419 dwarf2_per_objfile->gdb_index.asection = sectp;
1420 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1421 }
1422
1423 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1424 && bfd_section_vma (abfd, sectp) == 0)
1425 dwarf2_per_objfile->has_section_at_zero = 1;
1426 }
1427
1428 /* Decompress a section that was compressed using zlib. Store the
1429 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1430
1431 static void
1432 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1433 gdb_byte **outbuf, bfd_size_type *outsize)
1434 {
1435 bfd *abfd = objfile->obfd;
1436 #ifndef HAVE_ZLIB_H
1437 error (_("Support for zlib-compressed DWARF data (from '%s') "
1438 "is disabled in this copy of GDB"),
1439 bfd_get_filename (abfd));
1440 #else
1441 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1442 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1443 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1444 bfd_size_type uncompressed_size;
1445 gdb_byte *uncompressed_buffer;
1446 z_stream strm;
1447 int rc;
1448 int header_size = 12;
1449
1450 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1451 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1452 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1453 bfd_get_filename (abfd));
1454
1455 /* Read the zlib header. In this case, it should be "ZLIB" followed
1456 by the uncompressed section size, 8 bytes in big-endian order. */
1457 if (compressed_size < header_size
1458 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1459 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1460 bfd_get_filename (abfd));
1461 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1462 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1463 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1464 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[11];
1469
1470 /* It is possible the section consists of several compressed
1471 buffers concatenated together, so we uncompress in a loop. */
1472 strm.zalloc = NULL;
1473 strm.zfree = NULL;
1474 strm.opaque = NULL;
1475 strm.avail_in = compressed_size - header_size;
1476 strm.next_in = (Bytef*) compressed_buffer + header_size;
1477 strm.avail_out = uncompressed_size;
1478 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1479 uncompressed_size);
1480 rc = inflateInit (&strm);
1481 while (strm.avail_in > 0)
1482 {
1483 if (rc != Z_OK)
1484 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1485 bfd_get_filename (abfd), rc);
1486 strm.next_out = ((Bytef*) uncompressed_buffer
1487 + (uncompressed_size - strm.avail_out));
1488 rc = inflate (&strm, Z_FINISH);
1489 if (rc != Z_STREAM_END)
1490 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1491 bfd_get_filename (abfd), rc);
1492 rc = inflateReset (&strm);
1493 }
1494 rc = inflateEnd (&strm);
1495 if (rc != Z_OK
1496 || strm.avail_out != 0)
1497 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1498 bfd_get_filename (abfd), rc);
1499
1500 do_cleanups (cleanup);
1501 *outbuf = uncompressed_buffer;
1502 *outsize = uncompressed_size;
1503 #endif
1504 }
1505
1506 /* Read the contents of the section SECTP from object file specified by
1507 OBJFILE, store info about the section into INFO.
1508 If the section is compressed, uncompress it before returning. */
1509
1510 static void
1511 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1512 {
1513 bfd *abfd = objfile->obfd;
1514 asection *sectp = info->asection;
1515 gdb_byte *buf, *retbuf;
1516 unsigned char header[4];
1517
1518 if (info->readin)
1519 return;
1520 info->buffer = NULL;
1521 info->was_mmapped = 0;
1522 info->readin = 1;
1523
1524 if (info->asection == NULL || info->size == 0)
1525 return;
1526
1527 /* Check if the file has a 4-byte header indicating compression. */
1528 if (info->size > sizeof (header)
1529 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1530 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1531 {
1532 /* Upon decompression, update the buffer and its size. */
1533 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1534 {
1535 zlib_decompress_section (objfile, sectp, &info->buffer,
1536 &info->size);
1537 return;
1538 }
1539 }
1540
1541 #ifdef HAVE_MMAP
1542 if (pagesize == 0)
1543 pagesize = getpagesize ();
1544
1545 /* Only try to mmap sections which are large enough: we don't want to
1546 waste space due to fragmentation. Also, only try mmap for sections
1547 without relocations. */
1548
1549 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1550 {
1551 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1552 size_t map_length = info->size + sectp->filepos - pg_offset;
1553 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1554 MAP_PRIVATE, pg_offset);
1555
1556 if (retbuf != MAP_FAILED)
1557 {
1558 info->was_mmapped = 1;
1559 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1560 #if HAVE_POSIX_MADVISE
1561 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1562 #endif
1563 return;
1564 }
1565 }
1566 #endif
1567
1568 /* If we get here, we are a normal, not-compressed section. */
1569 info->buffer = buf
1570 = obstack_alloc (&objfile->objfile_obstack, info->size);
1571
1572 /* When debugging .o files, we may need to apply relocations; see
1573 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1574 We never compress sections in .o files, so we only need to
1575 try this when the section is not compressed. */
1576 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1577 if (retbuf != NULL)
1578 {
1579 info->buffer = retbuf;
1580 return;
1581 }
1582
1583 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1584 || bfd_bread (buf, info->size, abfd) != info->size)
1585 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1586 bfd_get_filename (abfd));
1587 }
1588
1589 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1590 SECTION_NAME. */
1591
1592 void
1593 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1594 asection **sectp, gdb_byte **bufp,
1595 bfd_size_type *sizep)
1596 {
1597 struct dwarf2_per_objfile *data
1598 = objfile_data (objfile, dwarf2_objfile_data_key);
1599 struct dwarf2_section_info *info;
1600
1601 /* We may see an objfile without any DWARF, in which case we just
1602 return nothing. */
1603 if (data == NULL)
1604 {
1605 *sectp = NULL;
1606 *bufp = NULL;
1607 *sizep = 0;
1608 return;
1609 }
1610 if (section_is_p (section_name, EH_FRAME_SECTION))
1611 info = &data->eh_frame;
1612 else if (section_is_p (section_name, FRAME_SECTION))
1613 info = &data->frame;
1614 else
1615 gdb_assert_not_reached ("unexpected section");
1616
1617 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1618 /* We haven't read this section in yet. Do it now. */
1619 dwarf2_read_section (objfile, info);
1620
1621 *sectp = info->asection;
1622 *bufp = info->buffer;
1623 *sizep = info->size;
1624 }
1625
1626 \f
1627 /* DWARF quick_symbols_functions support. */
1628
1629 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1630 unique line tables, so we maintain a separate table of all .debug_line
1631 derived entries to support the sharing.
1632 All the quick functions need is the list of file names. We discard the
1633 line_header when we're done and don't need to record it here. */
1634 struct quick_file_names
1635 {
1636 /* The offset in .debug_line of the line table. We hash on this. */
1637 unsigned int offset;
1638
1639 /* The number of entries in file_names, real_names. */
1640 unsigned int num_file_names;
1641
1642 /* The file names from the line table, after being run through
1643 file_full_name. */
1644 const char **file_names;
1645
1646 /* The file names from the line table after being run through
1647 gdb_realpath. These are computed lazily. */
1648 const char **real_names;
1649 };
1650
1651 /* When using the index (and thus not using psymtabs), each CU has an
1652 object of this type. This is used to hold information needed by
1653 the various "quick" methods. */
1654 struct dwarf2_per_cu_quick_data
1655 {
1656 /* The file table. This can be NULL if there was no file table
1657 or it's currently not read in.
1658 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1659 struct quick_file_names *file_names;
1660
1661 /* The corresponding symbol table. This is NULL if symbols for this
1662 CU have not yet been read. */
1663 struct symtab *symtab;
1664
1665 /* A temporary mark bit used when iterating over all CUs in
1666 expand_symtabs_matching. */
1667 unsigned int mark : 1;
1668
1669 /* True if we've tried to read the file table and found there isn't one.
1670 There will be no point in trying to read it again next time. */
1671 unsigned int no_file_data : 1;
1672 };
1673
1674 /* Hash function for a quick_file_names. */
1675
1676 static hashval_t
1677 hash_file_name_entry (const void *e)
1678 {
1679 const struct quick_file_names *file_data = e;
1680
1681 return file_data->offset;
1682 }
1683
1684 /* Equality function for a quick_file_names. */
1685
1686 static int
1687 eq_file_name_entry (const void *a, const void *b)
1688 {
1689 const struct quick_file_names *ea = a;
1690 const struct quick_file_names *eb = b;
1691
1692 return ea->offset == eb->offset;
1693 }
1694
1695 /* Delete function for a quick_file_names. */
1696
1697 static void
1698 delete_file_name_entry (void *e)
1699 {
1700 struct quick_file_names *file_data = e;
1701 int i;
1702
1703 for (i = 0; i < file_data->num_file_names; ++i)
1704 {
1705 xfree ((void*) file_data->file_names[i]);
1706 if (file_data->real_names)
1707 xfree ((void*) file_data->real_names[i]);
1708 }
1709
1710 /* The space for the struct itself lives on objfile_obstack,
1711 so we don't free it here. */
1712 }
1713
1714 /* Create a quick_file_names hash table. */
1715
1716 static htab_t
1717 create_quick_file_names_table (unsigned int nr_initial_entries)
1718 {
1719 return htab_create_alloc (nr_initial_entries,
1720 hash_file_name_entry, eq_file_name_entry,
1721 delete_file_name_entry, xcalloc, xfree);
1722 }
1723
1724 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1725 this CU came. */
1726
1727 static void
1728 dw2_do_instantiate_symtab (struct objfile *objfile,
1729 struct dwarf2_per_cu_data *per_cu)
1730 {
1731 struct cleanup *back_to;
1732
1733 back_to = make_cleanup (dwarf2_release_queue, NULL);
1734
1735 queue_comp_unit (per_cu, objfile);
1736
1737 if (per_cu->from_debug_types)
1738 read_signatured_type_at_offset (objfile, per_cu->offset);
1739 else
1740 load_full_comp_unit (per_cu, objfile);
1741
1742 process_queue (objfile);
1743
1744 /* Age the cache, releasing compilation units that have not
1745 been used recently. */
1746 age_cached_comp_units ();
1747
1748 do_cleanups (back_to);
1749 }
1750
1751 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1752 the objfile from which this CU came. Returns the resulting symbol
1753 table. */
1754
1755 static struct symtab *
1756 dw2_instantiate_symtab (struct objfile *objfile,
1757 struct dwarf2_per_cu_data *per_cu)
1758 {
1759 if (!per_cu->v.quick->symtab)
1760 {
1761 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1762 increment_reading_symtab ();
1763 dw2_do_instantiate_symtab (objfile, per_cu);
1764 do_cleanups (back_to);
1765 }
1766 return per_cu->v.quick->symtab;
1767 }
1768
1769 /* Return the CU given its index. */
1770
1771 static struct dwarf2_per_cu_data *
1772 dw2_get_cu (int index)
1773 {
1774 if (index >= dwarf2_per_objfile->n_comp_units)
1775 {
1776 index -= dwarf2_per_objfile->n_comp_units;
1777 return dwarf2_per_objfile->type_comp_units[index];
1778 }
1779 return dwarf2_per_objfile->all_comp_units[index];
1780 }
1781
1782 /* A helper function that knows how to read a 64-bit value in a way
1783 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1784 otherwise. */
1785
1786 static int
1787 extract_cu_value (const char *bytes, ULONGEST *result)
1788 {
1789 if (sizeof (ULONGEST) < 8)
1790 {
1791 int i;
1792
1793 /* Ignore the upper 4 bytes if they are all zero. */
1794 for (i = 0; i < 4; ++i)
1795 if (bytes[i + 4] != 0)
1796 return 0;
1797
1798 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1799 }
1800 else
1801 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1802 return 1;
1803 }
1804
1805 /* Read the CU list from the mapped index, and use it to create all
1806 the CU objects for this objfile. Return 0 if something went wrong,
1807 1 if everything went ok. */
1808
1809 static int
1810 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1811 offset_type cu_list_elements)
1812 {
1813 offset_type i;
1814
1815 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1816 dwarf2_per_objfile->all_comp_units
1817 = obstack_alloc (&objfile->objfile_obstack,
1818 dwarf2_per_objfile->n_comp_units
1819 * sizeof (struct dwarf2_per_cu_data *));
1820
1821 for (i = 0; i < cu_list_elements; i += 2)
1822 {
1823 struct dwarf2_per_cu_data *the_cu;
1824 ULONGEST offset, length;
1825
1826 if (!extract_cu_value (cu_list, &offset)
1827 || !extract_cu_value (cu_list + 8, &length))
1828 return 0;
1829 cu_list += 2 * 8;
1830
1831 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1832 struct dwarf2_per_cu_data);
1833 the_cu->offset = offset;
1834 the_cu->length = length;
1835 the_cu->objfile = objfile;
1836 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1837 struct dwarf2_per_cu_quick_data);
1838 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1839 }
1840
1841 return 1;
1842 }
1843
1844 /* Create the signatured type hash table from the index. */
1845
1846 static int
1847 create_signatured_type_table_from_index (struct objfile *objfile,
1848 const gdb_byte *bytes,
1849 offset_type elements)
1850 {
1851 offset_type i;
1852 htab_t sig_types_hash;
1853
1854 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1855 dwarf2_per_objfile->type_comp_units
1856 = obstack_alloc (&objfile->objfile_obstack,
1857 dwarf2_per_objfile->n_type_comp_units
1858 * sizeof (struct dwarf2_per_cu_data *));
1859
1860 sig_types_hash = allocate_signatured_type_table (objfile);
1861
1862 for (i = 0; i < elements; i += 3)
1863 {
1864 struct signatured_type *type_sig;
1865 ULONGEST offset, type_offset, signature;
1866 void **slot;
1867
1868 if (!extract_cu_value (bytes, &offset)
1869 || !extract_cu_value (bytes + 8, &type_offset))
1870 return 0;
1871 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1872 bytes += 3 * 8;
1873
1874 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1875 struct signatured_type);
1876 type_sig->signature = signature;
1877 type_sig->offset = offset;
1878 type_sig->type_offset = type_offset;
1879 type_sig->per_cu.from_debug_types = 1;
1880 type_sig->per_cu.offset = offset;
1881 type_sig->per_cu.objfile = objfile;
1882 type_sig->per_cu.v.quick
1883 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1884 struct dwarf2_per_cu_quick_data);
1885
1886 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1887 *slot = type_sig;
1888
1889 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1890 }
1891
1892 dwarf2_per_objfile->signatured_types = sig_types_hash;
1893
1894 return 1;
1895 }
1896
1897 /* Read the address map data from the mapped index, and use it to
1898 populate the objfile's psymtabs_addrmap. */
1899
1900 static void
1901 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1902 {
1903 const gdb_byte *iter, *end;
1904 struct obstack temp_obstack;
1905 struct addrmap *mutable_map;
1906 struct cleanup *cleanup;
1907 CORE_ADDR baseaddr;
1908
1909 obstack_init (&temp_obstack);
1910 cleanup = make_cleanup_obstack_free (&temp_obstack);
1911 mutable_map = addrmap_create_mutable (&temp_obstack);
1912
1913 iter = index->address_table;
1914 end = iter + index->address_table_size;
1915
1916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1917
1918 while (iter < end)
1919 {
1920 ULONGEST hi, lo, cu_index;
1921 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1922 iter += 8;
1923 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1924 iter += 8;
1925 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1926 iter += 4;
1927
1928 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1929 dw2_get_cu (cu_index));
1930 }
1931
1932 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1933 &objfile->objfile_obstack);
1934 do_cleanups (cleanup);
1935 }
1936
1937 /* The hash function for strings in the mapped index. This is the
1938 same as the hashtab.c hash function, but we keep a separate copy to
1939 maintain control over the implementation. This is necessary
1940 because the hash function is tied to the format of the mapped index
1941 file. */
1942
1943 static hashval_t
1944 mapped_index_string_hash (const void *p)
1945 {
1946 const unsigned char *str = (const unsigned char *) p;
1947 hashval_t r = 0;
1948 unsigned char c;
1949
1950 while ((c = *str++) != 0)
1951 r = r * 67 + c - 113;
1952
1953 return r;
1954 }
1955
1956 /* Find a slot in the mapped index INDEX for the object named NAME.
1957 If NAME is found, set *VEC_OUT to point to the CU vector in the
1958 constant pool and return 1. If NAME cannot be found, return 0. */
1959
1960 static int
1961 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1962 offset_type **vec_out)
1963 {
1964 offset_type hash = mapped_index_string_hash (name);
1965 offset_type slot, step;
1966
1967 slot = hash & (index->symbol_table_slots - 1);
1968 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
1969
1970 for (;;)
1971 {
1972 /* Convert a slot number to an offset into the table. */
1973 offset_type i = 2 * slot;
1974 const char *str;
1975 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
1976 return 0;
1977
1978 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
1979 if (!strcmp (name, str))
1980 {
1981 *vec_out = (offset_type *) (index->constant_pool
1982 + MAYBE_SWAP (index->symbol_table[i + 1]));
1983 return 1;
1984 }
1985
1986 slot = (slot + step) & (index->symbol_table_slots - 1);
1987 }
1988 }
1989
1990 /* Read the index file. If everything went ok, initialize the "quick"
1991 elements of all the CUs and return 1. Otherwise, return 0. */
1992
1993 static int
1994 dwarf2_read_index (struct objfile *objfile)
1995 {
1996 char *addr;
1997 struct mapped_index *map;
1998 offset_type *metadata;
1999 const gdb_byte *cu_list;
2000 const gdb_byte *types_list = NULL;
2001 offset_type version, cu_list_elements;
2002 offset_type types_list_elements = 0;
2003 int i;
2004
2005 if (dwarf2_per_objfile->gdb_index.asection == NULL
2006 || dwarf2_per_objfile->gdb_index.size == 0)
2007 return 0;
2008
2009 /* Older elfutils strip versions could keep the section in the main
2010 executable while splitting it for the separate debug info file. */
2011 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2012 & SEC_HAS_CONTENTS) == 0)
2013 return 0;
2014
2015 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2016
2017 addr = dwarf2_per_objfile->gdb_index.buffer;
2018 /* Version check. */
2019 version = MAYBE_SWAP (*(offset_type *) addr);
2020 /* Versions earlier than 3 emitted every copy of a psymbol. This
2021 causes the index to behave very poorly for certain requests. So,
2022 it seems better to just ignore such indices. */
2023 if (version < 3)
2024 return 0;
2025 /* Indexes with higher version than the one supported by GDB may be no
2026 longer backward compatible. */
2027 if (version > 3)
2028 return 0;
2029
2030 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2031 map->total_size = dwarf2_per_objfile->gdb_index.size;
2032
2033 metadata = (offset_type *) (addr + sizeof (offset_type));
2034
2035 i = 0;
2036 cu_list = addr + MAYBE_SWAP (metadata[i]);
2037 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2038 / 8);
2039 ++i;
2040
2041 types_list = addr + MAYBE_SWAP (metadata[i]);
2042 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2043 - MAYBE_SWAP (metadata[i]))
2044 / 8);
2045 ++i;
2046
2047 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2048 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2049 - MAYBE_SWAP (metadata[i]));
2050 ++i;
2051
2052 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2053 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2054 - MAYBE_SWAP (metadata[i]))
2055 / (2 * sizeof (offset_type)));
2056 ++i;
2057
2058 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2059
2060 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2061 return 0;
2062
2063 if (types_list_elements
2064 && !create_signatured_type_table_from_index (objfile, types_list,
2065 types_list_elements))
2066 return 0;
2067
2068 create_addrmap_from_index (objfile, map);
2069
2070 dwarf2_per_objfile->index_table = map;
2071 dwarf2_per_objfile->using_index = 1;
2072 dwarf2_per_objfile->quick_file_names_table =
2073 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2074
2075 return 1;
2076 }
2077
2078 /* A helper for the "quick" functions which sets the global
2079 dwarf2_per_objfile according to OBJFILE. */
2080
2081 static void
2082 dw2_setup (struct objfile *objfile)
2083 {
2084 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2085 gdb_assert (dwarf2_per_objfile);
2086 }
2087
2088 /* A helper for the "quick" functions which attempts to read the line
2089 table for THIS_CU. */
2090
2091 static struct quick_file_names *
2092 dw2_get_file_names (struct objfile *objfile,
2093 struct dwarf2_per_cu_data *this_cu)
2094 {
2095 bfd *abfd = objfile->obfd;
2096 struct line_header *lh;
2097 struct attribute *attr;
2098 struct cleanup *cleanups;
2099 struct die_info *comp_unit_die;
2100 struct dwarf2_section_info* sec;
2101 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2102 int has_children, i;
2103 struct dwarf2_cu cu;
2104 unsigned int bytes_read, buffer_size;
2105 struct die_reader_specs reader_specs;
2106 char *name, *comp_dir;
2107 void **slot;
2108 struct quick_file_names *qfn;
2109 unsigned int line_offset;
2110
2111 if (this_cu->v.quick->file_names != NULL)
2112 return this_cu->v.quick->file_names;
2113 /* If we know there is no line data, no point in looking again. */
2114 if (this_cu->v.quick->no_file_data)
2115 return NULL;
2116
2117 init_one_comp_unit (&cu, objfile);
2118 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2119
2120 if (this_cu->from_debug_types)
2121 sec = &dwarf2_per_objfile->types;
2122 else
2123 sec = &dwarf2_per_objfile->info;
2124 dwarf2_read_section (objfile, sec);
2125 buffer_size = sec->size;
2126 buffer = sec->buffer;
2127 info_ptr = buffer + this_cu->offset;
2128 beg_of_comp_unit = info_ptr;
2129
2130 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2131 buffer, buffer_size,
2132 abfd);
2133
2134 /* Complete the cu_header. */
2135 cu.header.offset = beg_of_comp_unit - buffer;
2136 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2137
2138 this_cu->cu = &cu;
2139 cu.per_cu = this_cu;
2140
2141 dwarf2_read_abbrevs (abfd, &cu);
2142 make_cleanup (dwarf2_free_abbrev_table, &cu);
2143
2144 if (this_cu->from_debug_types)
2145 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2146 init_cu_die_reader (&reader_specs, &cu);
2147 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2148 &has_children);
2149
2150 lh = NULL;
2151 slot = NULL;
2152 line_offset = 0;
2153 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2154 if (attr)
2155 {
2156 struct quick_file_names find_entry;
2157
2158 line_offset = DW_UNSND (attr);
2159
2160 /* We may have already read in this line header (TU line header sharing).
2161 If we have we're done. */
2162 find_entry.offset = line_offset;
2163 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2164 &find_entry, INSERT);
2165 if (*slot != NULL)
2166 {
2167 do_cleanups (cleanups);
2168 this_cu->v.quick->file_names = *slot;
2169 return *slot;
2170 }
2171
2172 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2173 }
2174 if (lh == NULL)
2175 {
2176 do_cleanups (cleanups);
2177 this_cu->v.quick->no_file_data = 1;
2178 return NULL;
2179 }
2180
2181 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2182 qfn->offset = line_offset;
2183 gdb_assert (slot != NULL);
2184 *slot = qfn;
2185
2186 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2187
2188 qfn->num_file_names = lh->num_file_names;
2189 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2190 lh->num_file_names * sizeof (char *));
2191 for (i = 0; i < lh->num_file_names; ++i)
2192 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2193 qfn->real_names = NULL;
2194
2195 free_line_header (lh);
2196 do_cleanups (cleanups);
2197
2198 this_cu->v.quick->file_names = qfn;
2199 return qfn;
2200 }
2201
2202 /* A helper for the "quick" functions which computes and caches the
2203 real path for a given file name from the line table. */
2204
2205 static const char *
2206 dw2_get_real_path (struct objfile *objfile,
2207 struct quick_file_names *qfn, int index)
2208 {
2209 if (qfn->real_names == NULL)
2210 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2211 qfn->num_file_names, sizeof (char *));
2212
2213 if (qfn->real_names[index] == NULL)
2214 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2215
2216 return qfn->real_names[index];
2217 }
2218
2219 static struct symtab *
2220 dw2_find_last_source_symtab (struct objfile *objfile)
2221 {
2222 int index;
2223
2224 dw2_setup (objfile);
2225 index = dwarf2_per_objfile->n_comp_units - 1;
2226 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2227 }
2228
2229 /* Traversal function for dw2_forget_cached_source_info. */
2230
2231 static int
2232 dw2_free_cached_file_names (void **slot, void *info)
2233 {
2234 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2235
2236 if (file_data->real_names)
2237 {
2238 int i;
2239
2240 for (i = 0; i < file_data->num_file_names; ++i)
2241 {
2242 xfree ((void*) file_data->real_names[i]);
2243 file_data->real_names[i] = NULL;
2244 }
2245 }
2246
2247 return 1;
2248 }
2249
2250 static void
2251 dw2_forget_cached_source_info (struct objfile *objfile)
2252 {
2253 dw2_setup (objfile);
2254
2255 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2256 dw2_free_cached_file_names, NULL);
2257 }
2258
2259 static int
2260 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2261 const char *full_path, const char *real_path,
2262 struct symtab **result)
2263 {
2264 int i;
2265 int check_basename = lbasename (name) == name;
2266 struct dwarf2_per_cu_data *base_cu = NULL;
2267
2268 dw2_setup (objfile);
2269
2270 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2271 + dwarf2_per_objfile->n_type_comp_units); ++i)
2272 {
2273 int j;
2274 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2275 struct quick_file_names *file_data;
2276
2277 if (per_cu->v.quick->symtab)
2278 continue;
2279
2280 file_data = dw2_get_file_names (objfile, per_cu);
2281 if (file_data == NULL)
2282 continue;
2283
2284 for (j = 0; j < file_data->num_file_names; ++j)
2285 {
2286 const char *this_name = file_data->file_names[j];
2287
2288 if (FILENAME_CMP (name, this_name) == 0)
2289 {
2290 *result = dw2_instantiate_symtab (objfile, per_cu);
2291 return 1;
2292 }
2293
2294 if (check_basename && ! base_cu
2295 && FILENAME_CMP (lbasename (this_name), name) == 0)
2296 base_cu = per_cu;
2297
2298 if (full_path != NULL)
2299 {
2300 const char *this_real_name = dw2_get_real_path (objfile,
2301 file_data, j);
2302
2303 if (this_real_name != NULL
2304 && FILENAME_CMP (full_path, this_real_name) == 0)
2305 {
2306 *result = dw2_instantiate_symtab (objfile, per_cu);
2307 return 1;
2308 }
2309 }
2310
2311 if (real_path != NULL)
2312 {
2313 const char *this_real_name = dw2_get_real_path (objfile,
2314 file_data, j);
2315
2316 if (this_real_name != NULL
2317 && FILENAME_CMP (real_path, this_real_name) == 0)
2318 {
2319 *result = dw2_instantiate_symtab (objfile, per_cu);
2320 return 1;
2321 }
2322 }
2323 }
2324 }
2325
2326 if (base_cu)
2327 {
2328 *result = dw2_instantiate_symtab (objfile, base_cu);
2329 return 1;
2330 }
2331
2332 return 0;
2333 }
2334
2335 static struct symtab *
2336 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2337 const char *name, domain_enum domain)
2338 {
2339 /* We do all the work in the pre_expand_symtabs_matching hook
2340 instead. */
2341 return NULL;
2342 }
2343
2344 /* A helper function that expands all symtabs that hold an object
2345 named NAME. */
2346
2347 static void
2348 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2349 {
2350 dw2_setup (objfile);
2351
2352 /* index_table is NULL if OBJF_READNOW. */
2353 if (dwarf2_per_objfile->index_table)
2354 {
2355 offset_type *vec;
2356
2357 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2358 name, &vec))
2359 {
2360 offset_type i, len = MAYBE_SWAP (*vec);
2361 for (i = 0; i < len; ++i)
2362 {
2363 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2364 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2365
2366 dw2_instantiate_symtab (objfile, per_cu);
2367 }
2368 }
2369 }
2370 }
2371
2372 static void
2373 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2374 int kind, const char *name,
2375 domain_enum domain)
2376 {
2377 dw2_do_expand_symtabs_matching (objfile, name);
2378 }
2379
2380 static void
2381 dw2_print_stats (struct objfile *objfile)
2382 {
2383 int i, count;
2384
2385 dw2_setup (objfile);
2386 count = 0;
2387 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2388 + dwarf2_per_objfile->n_type_comp_units); ++i)
2389 {
2390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2391
2392 if (!per_cu->v.quick->symtab)
2393 ++count;
2394 }
2395 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2396 }
2397
2398 static void
2399 dw2_dump (struct objfile *objfile)
2400 {
2401 /* Nothing worth printing. */
2402 }
2403
2404 static void
2405 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2406 struct section_offsets *delta)
2407 {
2408 /* There's nothing to relocate here. */
2409 }
2410
2411 static void
2412 dw2_expand_symtabs_for_function (struct objfile *objfile,
2413 const char *func_name)
2414 {
2415 dw2_do_expand_symtabs_matching (objfile, func_name);
2416 }
2417
2418 static void
2419 dw2_expand_all_symtabs (struct objfile *objfile)
2420 {
2421 int i;
2422
2423 dw2_setup (objfile);
2424
2425 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2426 + dwarf2_per_objfile->n_type_comp_units); ++i)
2427 {
2428 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2429
2430 dw2_instantiate_symtab (objfile, per_cu);
2431 }
2432 }
2433
2434 static void
2435 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2436 const char *filename)
2437 {
2438 int i;
2439
2440 dw2_setup (objfile);
2441
2442 /* We don't need to consider type units here.
2443 This is only called for examining code, e.g. expand_line_sal.
2444 There can be an order of magnitude (or more) more type units
2445 than comp units, and we avoid them if we can. */
2446
2447 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2448 {
2449 int j;
2450 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2451 struct quick_file_names *file_data;
2452
2453 if (per_cu->v.quick->symtab)
2454 continue;
2455
2456 file_data = dw2_get_file_names (objfile, per_cu);
2457 if (file_data == NULL)
2458 continue;
2459
2460 for (j = 0; j < file_data->num_file_names; ++j)
2461 {
2462 const char *this_name = file_data->file_names[j];
2463 if (FILENAME_CMP (this_name, filename) == 0)
2464 {
2465 dw2_instantiate_symtab (objfile, per_cu);
2466 break;
2467 }
2468 }
2469 }
2470 }
2471
2472 static const char *
2473 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2474 {
2475 struct dwarf2_per_cu_data *per_cu;
2476 offset_type *vec;
2477 struct quick_file_names *file_data;
2478
2479 dw2_setup (objfile);
2480
2481 /* index_table is NULL if OBJF_READNOW. */
2482 if (!dwarf2_per_objfile->index_table)
2483 return NULL;
2484
2485 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2486 name, &vec))
2487 return NULL;
2488
2489 /* Note that this just looks at the very first one named NAME -- but
2490 actually we are looking for a function. find_main_filename
2491 should be rewritten so that it doesn't require a custom hook. It
2492 could just use the ordinary symbol tables. */
2493 /* vec[0] is the length, which must always be >0. */
2494 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2495
2496 file_data = dw2_get_file_names (objfile, per_cu);
2497 if (file_data == NULL)
2498 return NULL;
2499
2500 return file_data->file_names[file_data->num_file_names - 1];
2501 }
2502
2503 static void
2504 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2505 struct objfile *objfile, int global,
2506 int (*callback) (struct block *,
2507 struct symbol *, void *),
2508 void *data, symbol_compare_ftype *match,
2509 symbol_compare_ftype *ordered_compare)
2510 {
2511 /* Currently unimplemented; used for Ada. The function can be called if the
2512 current language is Ada for a non-Ada objfile using GNU index. As Ada
2513 does not look for non-Ada symbols this function should just return. */
2514 }
2515
2516 static void
2517 dw2_expand_symtabs_matching (struct objfile *objfile,
2518 int (*file_matcher) (const char *, void *),
2519 int (*name_matcher) (const char *, void *),
2520 domain_enum kind,
2521 void *data)
2522 {
2523 int i;
2524 offset_type iter;
2525 struct mapped_index *index;
2526
2527 dw2_setup (objfile);
2528
2529 /* index_table is NULL if OBJF_READNOW. */
2530 if (!dwarf2_per_objfile->index_table)
2531 return;
2532 index = dwarf2_per_objfile->index_table;
2533
2534 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2535 + dwarf2_per_objfile->n_type_comp_units); ++i)
2536 {
2537 int j;
2538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2539 struct quick_file_names *file_data;
2540
2541 per_cu->v.quick->mark = 0;
2542 if (per_cu->v.quick->symtab)
2543 continue;
2544
2545 file_data = dw2_get_file_names (objfile, per_cu);
2546 if (file_data == NULL)
2547 continue;
2548
2549 for (j = 0; j < file_data->num_file_names; ++j)
2550 {
2551 if (file_matcher (file_data->file_names[j], data))
2552 {
2553 per_cu->v.quick->mark = 1;
2554 break;
2555 }
2556 }
2557 }
2558
2559 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2560 {
2561 offset_type idx = 2 * iter;
2562 const char *name;
2563 offset_type *vec, vec_len, vec_idx;
2564
2565 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2566 continue;
2567
2568 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2569
2570 if (! (*name_matcher) (name, data))
2571 continue;
2572
2573 /* The name was matched, now expand corresponding CUs that were
2574 marked. */
2575 vec = (offset_type *) (index->constant_pool
2576 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2577 vec_len = MAYBE_SWAP (vec[0]);
2578 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2579 {
2580 struct dwarf2_per_cu_data *per_cu;
2581
2582 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2583 if (per_cu->v.quick->mark)
2584 dw2_instantiate_symtab (objfile, per_cu);
2585 }
2586 }
2587 }
2588
2589 static struct symtab *
2590 dw2_find_pc_sect_symtab (struct objfile *objfile,
2591 struct minimal_symbol *msymbol,
2592 CORE_ADDR pc,
2593 struct obj_section *section,
2594 int warn_if_readin)
2595 {
2596 struct dwarf2_per_cu_data *data;
2597
2598 dw2_setup (objfile);
2599
2600 if (!objfile->psymtabs_addrmap)
2601 return NULL;
2602
2603 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2604 if (!data)
2605 return NULL;
2606
2607 if (warn_if_readin && data->v.quick->symtab)
2608 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2609 paddress (get_objfile_arch (objfile), pc));
2610
2611 return dw2_instantiate_symtab (objfile, data);
2612 }
2613
2614 static void
2615 dw2_map_symbol_names (struct objfile *objfile,
2616 void (*fun) (const char *, void *),
2617 void *data)
2618 {
2619 offset_type iter;
2620 struct mapped_index *index;
2621
2622 dw2_setup (objfile);
2623
2624 /* index_table is NULL if OBJF_READNOW. */
2625 if (!dwarf2_per_objfile->index_table)
2626 return;
2627 index = dwarf2_per_objfile->index_table;
2628
2629 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2630 {
2631 offset_type idx = 2 * iter;
2632 const char *name;
2633 offset_type *vec, vec_len, vec_idx;
2634
2635 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2636 continue;
2637
2638 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2639
2640 (*fun) (name, data);
2641 }
2642 }
2643
2644 static void
2645 dw2_map_symbol_filenames (struct objfile *objfile,
2646 void (*fun) (const char *, const char *, void *),
2647 void *data)
2648 {
2649 int i;
2650
2651 dw2_setup (objfile);
2652
2653 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2654 + dwarf2_per_objfile->n_type_comp_units); ++i)
2655 {
2656 int j;
2657 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2658 struct quick_file_names *file_data;
2659
2660 if (per_cu->v.quick->symtab)
2661 continue;
2662
2663 file_data = dw2_get_file_names (objfile, per_cu);
2664 if (file_data == NULL)
2665 continue;
2666
2667 for (j = 0; j < file_data->num_file_names; ++j)
2668 {
2669 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2670 j);
2671 (*fun) (file_data->file_names[j], this_real_name, data);
2672 }
2673 }
2674 }
2675
2676 static int
2677 dw2_has_symbols (struct objfile *objfile)
2678 {
2679 return 1;
2680 }
2681
2682 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2683 {
2684 dw2_has_symbols,
2685 dw2_find_last_source_symtab,
2686 dw2_forget_cached_source_info,
2687 dw2_lookup_symtab,
2688 dw2_lookup_symbol,
2689 dw2_pre_expand_symtabs_matching,
2690 dw2_print_stats,
2691 dw2_dump,
2692 dw2_relocate,
2693 dw2_expand_symtabs_for_function,
2694 dw2_expand_all_symtabs,
2695 dw2_expand_symtabs_with_filename,
2696 dw2_find_symbol_file,
2697 dw2_map_matching_symbols,
2698 dw2_expand_symtabs_matching,
2699 dw2_find_pc_sect_symtab,
2700 dw2_map_symbol_names,
2701 dw2_map_symbol_filenames
2702 };
2703
2704 /* Initialize for reading DWARF for this objfile. Return 0 if this
2705 file will use psymtabs, or 1 if using the GNU index. */
2706
2707 int
2708 dwarf2_initialize_objfile (struct objfile *objfile)
2709 {
2710 /* If we're about to read full symbols, don't bother with the
2711 indices. In this case we also don't care if some other debug
2712 format is making psymtabs, because they are all about to be
2713 expanded anyway. */
2714 if ((objfile->flags & OBJF_READNOW))
2715 {
2716 int i;
2717
2718 dwarf2_per_objfile->using_index = 1;
2719 create_all_comp_units (objfile);
2720 create_debug_types_hash_table (objfile);
2721 dwarf2_per_objfile->quick_file_names_table =
2722 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2723
2724 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2725 + dwarf2_per_objfile->n_type_comp_units); ++i)
2726 {
2727 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2728
2729 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2730 struct dwarf2_per_cu_quick_data);
2731 }
2732
2733 /* Return 1 so that gdb sees the "quick" functions. However,
2734 these functions will be no-ops because we will have expanded
2735 all symtabs. */
2736 return 1;
2737 }
2738
2739 if (dwarf2_read_index (objfile))
2740 return 1;
2741
2742 dwarf2_build_psymtabs (objfile);
2743 return 0;
2744 }
2745
2746 \f
2747
2748 /* Build a partial symbol table. */
2749
2750 void
2751 dwarf2_build_psymtabs (struct objfile *objfile)
2752 {
2753 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2754 {
2755 init_psymbol_list (objfile, 1024);
2756 }
2757
2758 dwarf2_build_psymtabs_hard (objfile);
2759 }
2760
2761 /* Return TRUE if OFFSET is within CU_HEADER. */
2762
2763 static inline int
2764 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2765 {
2766 unsigned int bottom = cu_header->offset;
2767 unsigned int top = (cu_header->offset
2768 + cu_header->length
2769 + cu_header->initial_length_size);
2770
2771 return (offset >= bottom && offset < top);
2772 }
2773
2774 /* Read in the comp unit header information from the debug_info at info_ptr.
2775 NOTE: This leaves members offset, first_die_offset to be filled in
2776 by the caller. */
2777
2778 static gdb_byte *
2779 read_comp_unit_head (struct comp_unit_head *cu_header,
2780 gdb_byte *info_ptr, bfd *abfd)
2781 {
2782 int signed_addr;
2783 unsigned int bytes_read;
2784
2785 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2786 cu_header->initial_length_size = bytes_read;
2787 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2788 info_ptr += bytes_read;
2789 cu_header->version = read_2_bytes (abfd, info_ptr);
2790 info_ptr += 2;
2791 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2792 &bytes_read);
2793 info_ptr += bytes_read;
2794 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2795 info_ptr += 1;
2796 signed_addr = bfd_get_sign_extend_vma (abfd);
2797 if (signed_addr < 0)
2798 internal_error (__FILE__, __LINE__,
2799 _("read_comp_unit_head: dwarf from non elf file"));
2800 cu_header->signed_addr_p = signed_addr;
2801
2802 return info_ptr;
2803 }
2804
2805 static gdb_byte *
2806 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2807 gdb_byte *buffer, unsigned int buffer_size,
2808 bfd *abfd)
2809 {
2810 gdb_byte *beg_of_comp_unit = info_ptr;
2811
2812 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2813
2814 if (header->version != 2 && header->version != 3 && header->version != 4)
2815 error (_("Dwarf Error: wrong version in compilation unit header "
2816 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2817 bfd_get_filename (abfd));
2818
2819 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2820 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2821 "(offset 0x%lx + 6) [in module %s]"),
2822 (long) header->abbrev_offset,
2823 (long) (beg_of_comp_unit - buffer),
2824 bfd_get_filename (abfd));
2825
2826 if (beg_of_comp_unit + header->length + header->initial_length_size
2827 > buffer + buffer_size)
2828 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2829 "(offset 0x%lx + 0) [in module %s]"),
2830 (long) header->length,
2831 (long) (beg_of_comp_unit - buffer),
2832 bfd_get_filename (abfd));
2833
2834 return info_ptr;
2835 }
2836
2837 /* Read in the types comp unit header information from .debug_types entry at
2838 types_ptr. The result is a pointer to one past the end of the header. */
2839
2840 static gdb_byte *
2841 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2842 ULONGEST *signature,
2843 gdb_byte *types_ptr, bfd *abfd)
2844 {
2845 gdb_byte *initial_types_ptr = types_ptr;
2846
2847 dwarf2_read_section (dwarf2_per_objfile->objfile,
2848 &dwarf2_per_objfile->types);
2849 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2850
2851 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2852
2853 *signature = read_8_bytes (abfd, types_ptr);
2854 types_ptr += 8;
2855 types_ptr += cu_header->offset_size;
2856 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2857
2858 return types_ptr;
2859 }
2860
2861 /* Allocate a new partial symtab for file named NAME and mark this new
2862 partial symtab as being an include of PST. */
2863
2864 static void
2865 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2866 struct objfile *objfile)
2867 {
2868 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2869
2870 subpst->section_offsets = pst->section_offsets;
2871 subpst->textlow = 0;
2872 subpst->texthigh = 0;
2873
2874 subpst->dependencies = (struct partial_symtab **)
2875 obstack_alloc (&objfile->objfile_obstack,
2876 sizeof (struct partial_symtab *));
2877 subpst->dependencies[0] = pst;
2878 subpst->number_of_dependencies = 1;
2879
2880 subpst->globals_offset = 0;
2881 subpst->n_global_syms = 0;
2882 subpst->statics_offset = 0;
2883 subpst->n_static_syms = 0;
2884 subpst->symtab = NULL;
2885 subpst->read_symtab = pst->read_symtab;
2886 subpst->readin = 0;
2887
2888 /* No private part is necessary for include psymtabs. This property
2889 can be used to differentiate between such include psymtabs and
2890 the regular ones. */
2891 subpst->read_symtab_private = NULL;
2892 }
2893
2894 /* Read the Line Number Program data and extract the list of files
2895 included by the source file represented by PST. Build an include
2896 partial symtab for each of these included files. */
2897
2898 static void
2899 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2900 struct die_info *die,
2901 struct partial_symtab *pst)
2902 {
2903 struct objfile *objfile = cu->objfile;
2904 bfd *abfd = objfile->obfd;
2905 struct line_header *lh = NULL;
2906 struct attribute *attr;
2907
2908 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2909 if (attr)
2910 {
2911 unsigned int line_offset = DW_UNSND (attr);
2912
2913 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2914 }
2915 if (lh == NULL)
2916 return; /* No linetable, so no includes. */
2917
2918 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2919 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2920
2921 free_line_header (lh);
2922 }
2923
2924 static hashval_t
2925 hash_type_signature (const void *item)
2926 {
2927 const struct signatured_type *type_sig = item;
2928
2929 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2930 return type_sig->signature;
2931 }
2932
2933 static int
2934 eq_type_signature (const void *item_lhs, const void *item_rhs)
2935 {
2936 const struct signatured_type *lhs = item_lhs;
2937 const struct signatured_type *rhs = item_rhs;
2938
2939 return lhs->signature == rhs->signature;
2940 }
2941
2942 /* Allocate a hash table for signatured types. */
2943
2944 static htab_t
2945 allocate_signatured_type_table (struct objfile *objfile)
2946 {
2947 return htab_create_alloc_ex (41,
2948 hash_type_signature,
2949 eq_type_signature,
2950 NULL,
2951 &objfile->objfile_obstack,
2952 hashtab_obstack_allocate,
2953 dummy_obstack_deallocate);
2954 }
2955
2956 /* A helper function to add a signatured type CU to a list. */
2957
2958 static int
2959 add_signatured_type_cu_to_list (void **slot, void *datum)
2960 {
2961 struct signatured_type *sigt = *slot;
2962 struct dwarf2_per_cu_data ***datap = datum;
2963
2964 **datap = &sigt->per_cu;
2965 ++*datap;
2966
2967 return 1;
2968 }
2969
2970 /* Create the hash table of all entries in the .debug_types section.
2971 The result is zero if there is an error (e.g. missing .debug_types section),
2972 otherwise non-zero. */
2973
2974 static int
2975 create_debug_types_hash_table (struct objfile *objfile)
2976 {
2977 gdb_byte *info_ptr;
2978 htab_t types_htab;
2979 struct dwarf2_per_cu_data **iter;
2980
2981 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2982 info_ptr = dwarf2_per_objfile->types.buffer;
2983
2984 if (info_ptr == NULL)
2985 {
2986 dwarf2_per_objfile->signatured_types = NULL;
2987 return 0;
2988 }
2989
2990 types_htab = allocate_signatured_type_table (objfile);
2991
2992 if (dwarf2_die_debug)
2993 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2994
2995 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2996 {
2997 unsigned int offset;
2998 unsigned int offset_size;
2999 unsigned int type_offset;
3000 unsigned int length, initial_length_size;
3001 unsigned short version;
3002 ULONGEST signature;
3003 struct signatured_type *type_sig;
3004 void **slot;
3005 gdb_byte *ptr = info_ptr;
3006
3007 offset = ptr - dwarf2_per_objfile->types.buffer;
3008
3009 /* We need to read the type's signature in order to build the hash
3010 table, but we don't need to read anything else just yet. */
3011
3012 /* Sanity check to ensure entire cu is present. */
3013 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3014 if (ptr + length + initial_length_size
3015 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3016 {
3017 complaint (&symfile_complaints,
3018 _("debug type entry runs off end of `.debug_types' section, ignored"));
3019 break;
3020 }
3021
3022 offset_size = initial_length_size == 4 ? 4 : 8;
3023 ptr += initial_length_size;
3024 version = bfd_get_16 (objfile->obfd, ptr);
3025 ptr += 2;
3026 ptr += offset_size; /* abbrev offset */
3027 ptr += 1; /* address size */
3028 signature = bfd_get_64 (objfile->obfd, ptr);
3029 ptr += 8;
3030 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3031
3032 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3033 memset (type_sig, 0, sizeof (*type_sig));
3034 type_sig->signature = signature;
3035 type_sig->offset = offset;
3036 type_sig->type_offset = type_offset;
3037 type_sig->per_cu.objfile = objfile;
3038 type_sig->per_cu.from_debug_types = 1;
3039
3040 slot = htab_find_slot (types_htab, type_sig, INSERT);
3041 gdb_assert (slot != NULL);
3042 *slot = type_sig;
3043
3044 if (dwarf2_die_debug)
3045 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3046 offset, phex (signature, sizeof (signature)));
3047
3048 info_ptr = info_ptr + initial_length_size + length;
3049 }
3050
3051 dwarf2_per_objfile->signatured_types = types_htab;
3052
3053 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3054 dwarf2_per_objfile->type_comp_units
3055 = obstack_alloc (&objfile->objfile_obstack,
3056 dwarf2_per_objfile->n_type_comp_units
3057 * sizeof (struct dwarf2_per_cu_data *));
3058 iter = &dwarf2_per_objfile->type_comp_units[0];
3059 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3060 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3061 == dwarf2_per_objfile->n_type_comp_units);
3062
3063 return 1;
3064 }
3065
3066 /* Lookup a signature based type.
3067 Returns NULL if SIG is not present in the table. */
3068
3069 static struct signatured_type *
3070 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3071 {
3072 struct signatured_type find_entry, *entry;
3073
3074 if (dwarf2_per_objfile->signatured_types == NULL)
3075 {
3076 complaint (&symfile_complaints,
3077 _("missing `.debug_types' section for DW_FORM_sig8 die"));
3078 return 0;
3079 }
3080
3081 find_entry.signature = sig;
3082 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3083 return entry;
3084 }
3085
3086 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3087
3088 static void
3089 init_cu_die_reader (struct die_reader_specs *reader,
3090 struct dwarf2_cu *cu)
3091 {
3092 reader->abfd = cu->objfile->obfd;
3093 reader->cu = cu;
3094 if (cu->per_cu->from_debug_types)
3095 {
3096 gdb_assert (dwarf2_per_objfile->types.readin);
3097 reader->buffer = dwarf2_per_objfile->types.buffer;
3098 }
3099 else
3100 {
3101 gdb_assert (dwarf2_per_objfile->info.readin);
3102 reader->buffer = dwarf2_per_objfile->info.buffer;
3103 }
3104 }
3105
3106 /* Find the base address of the compilation unit for range lists and
3107 location lists. It will normally be specified by DW_AT_low_pc.
3108 In DWARF-3 draft 4, the base address could be overridden by
3109 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3110 compilation units with discontinuous ranges. */
3111
3112 static void
3113 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3114 {
3115 struct attribute *attr;
3116
3117 cu->base_known = 0;
3118 cu->base_address = 0;
3119
3120 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3121 if (attr)
3122 {
3123 cu->base_address = DW_ADDR (attr);
3124 cu->base_known = 1;
3125 }
3126 else
3127 {
3128 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3129 if (attr)
3130 {
3131 cu->base_address = DW_ADDR (attr);
3132 cu->base_known = 1;
3133 }
3134 }
3135 }
3136
3137 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3138 to combine the common parts.
3139 Process a compilation unit for a psymtab.
3140 BUFFER is a pointer to the beginning of the dwarf section buffer,
3141 either .debug_info or debug_types.
3142 INFO_PTR is a pointer to the start of the CU.
3143 Returns a pointer to the next CU. */
3144
3145 static gdb_byte *
3146 process_psymtab_comp_unit (struct objfile *objfile,
3147 struct dwarf2_per_cu_data *this_cu,
3148 gdb_byte *buffer, gdb_byte *info_ptr,
3149 unsigned int buffer_size)
3150 {
3151 bfd *abfd = objfile->obfd;
3152 gdb_byte *beg_of_comp_unit = info_ptr;
3153 struct die_info *comp_unit_die;
3154 struct partial_symtab *pst;
3155 CORE_ADDR baseaddr;
3156 struct cleanup *back_to_inner;
3157 struct dwarf2_cu cu;
3158 int has_children, has_pc_info;
3159 struct attribute *attr;
3160 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3161 struct die_reader_specs reader_specs;
3162
3163 init_one_comp_unit (&cu, objfile);
3164 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3165
3166 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3167 buffer, buffer_size,
3168 abfd);
3169
3170 /* Complete the cu_header. */
3171 cu.header.offset = beg_of_comp_unit - buffer;
3172 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3173
3174 cu.list_in_scope = &file_symbols;
3175
3176 /* If this compilation unit was already read in, free the
3177 cached copy in order to read it in again. This is
3178 necessary because we skipped some symbols when we first
3179 read in the compilation unit (see load_partial_dies).
3180 This problem could be avoided, but the benefit is
3181 unclear. */
3182 if (this_cu->cu != NULL)
3183 free_one_cached_comp_unit (this_cu->cu);
3184
3185 /* Note that this is a pointer to our stack frame, being
3186 added to a global data structure. It will be cleaned up
3187 in free_stack_comp_unit when we finish with this
3188 compilation unit. */
3189 this_cu->cu = &cu;
3190 cu.per_cu = this_cu;
3191
3192 /* Read the abbrevs for this compilation unit into a table. */
3193 dwarf2_read_abbrevs (abfd, &cu);
3194 make_cleanup (dwarf2_free_abbrev_table, &cu);
3195
3196 /* Read the compilation unit die. */
3197 if (this_cu->from_debug_types)
3198 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3199 init_cu_die_reader (&reader_specs, &cu);
3200 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3201 &has_children);
3202
3203 if (this_cu->from_debug_types)
3204 {
3205 /* offset,length haven't been set yet for type units. */
3206 this_cu->offset = cu.header.offset;
3207 this_cu->length = cu.header.length + cu.header.initial_length_size;
3208 }
3209 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3210 {
3211 info_ptr = (beg_of_comp_unit + cu.header.length
3212 + cu.header.initial_length_size);
3213 do_cleanups (back_to_inner);
3214 return info_ptr;
3215 }
3216
3217 prepare_one_comp_unit (&cu, comp_unit_die);
3218
3219 /* Allocate a new partial symbol table structure. */
3220 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3221 pst = start_psymtab_common (objfile, objfile->section_offsets,
3222 (attr != NULL) ? DW_STRING (attr) : "",
3223 /* TEXTLOW and TEXTHIGH are set below. */
3224 0,
3225 objfile->global_psymbols.next,
3226 objfile->static_psymbols.next);
3227
3228 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3229 if (attr != NULL)
3230 pst->dirname = DW_STRING (attr);
3231
3232 pst->read_symtab_private = this_cu;
3233
3234 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3235
3236 /* Store the function that reads in the rest of the symbol table */
3237 pst->read_symtab = dwarf2_psymtab_to_symtab;
3238
3239 this_cu->v.psymtab = pst;
3240
3241 dwarf2_find_base_address (comp_unit_die, &cu);
3242
3243 /* Possibly set the default values of LOWPC and HIGHPC from
3244 `DW_AT_ranges'. */
3245 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3246 &best_highpc, &cu, pst);
3247 if (has_pc_info == 1 && best_lowpc < best_highpc)
3248 /* Store the contiguous range if it is not empty; it can be empty for
3249 CUs with no code. */
3250 addrmap_set_empty (objfile->psymtabs_addrmap,
3251 best_lowpc + baseaddr,
3252 best_highpc + baseaddr - 1, pst);
3253
3254 /* Check if comp unit has_children.
3255 If so, read the rest of the partial symbols from this comp unit.
3256 If not, there's no more debug_info for this comp unit. */
3257 if (has_children)
3258 {
3259 struct partial_die_info *first_die;
3260 CORE_ADDR lowpc, highpc;
3261
3262 lowpc = ((CORE_ADDR) -1);
3263 highpc = ((CORE_ADDR) 0);
3264
3265 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3266
3267 scan_partial_symbols (first_die, &lowpc, &highpc,
3268 ! has_pc_info, &cu);
3269
3270 /* If we didn't find a lowpc, set it to highpc to avoid
3271 complaints from `maint check'. */
3272 if (lowpc == ((CORE_ADDR) -1))
3273 lowpc = highpc;
3274
3275 /* If the compilation unit didn't have an explicit address range,
3276 then use the information extracted from its child dies. */
3277 if (! has_pc_info)
3278 {
3279 best_lowpc = lowpc;
3280 best_highpc = highpc;
3281 }
3282 }
3283 pst->textlow = best_lowpc + baseaddr;
3284 pst->texthigh = best_highpc + baseaddr;
3285
3286 pst->n_global_syms = objfile->global_psymbols.next -
3287 (objfile->global_psymbols.list + pst->globals_offset);
3288 pst->n_static_syms = objfile->static_psymbols.next -
3289 (objfile->static_psymbols.list + pst->statics_offset);
3290 sort_pst_symbols (pst);
3291
3292 info_ptr = (beg_of_comp_unit + cu.header.length
3293 + cu.header.initial_length_size);
3294
3295 if (this_cu->from_debug_types)
3296 {
3297 /* It's not clear we want to do anything with stmt lists here.
3298 Waiting to see what gcc ultimately does. */
3299 }
3300 else
3301 {
3302 /* Get the list of files included in the current compilation unit,
3303 and build a psymtab for each of them. */
3304 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3305 }
3306
3307 do_cleanups (back_to_inner);
3308
3309 return info_ptr;
3310 }
3311
3312 /* Traversal function for htab_traverse_noresize.
3313 Process one .debug_types comp-unit. */
3314
3315 static int
3316 process_type_comp_unit (void **slot, void *info)
3317 {
3318 struct signatured_type *entry = (struct signatured_type *) *slot;
3319 struct objfile *objfile = (struct objfile *) info;
3320 struct dwarf2_per_cu_data *this_cu;
3321
3322 this_cu = &entry->per_cu;
3323
3324 gdb_assert (dwarf2_per_objfile->types.readin);
3325 process_psymtab_comp_unit (objfile, this_cu,
3326 dwarf2_per_objfile->types.buffer,
3327 dwarf2_per_objfile->types.buffer + entry->offset,
3328 dwarf2_per_objfile->types.size);
3329
3330 return 1;
3331 }
3332
3333 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3334 Build partial symbol tables for the .debug_types comp-units. */
3335
3336 static void
3337 build_type_psymtabs (struct objfile *objfile)
3338 {
3339 if (! create_debug_types_hash_table (objfile))
3340 return;
3341
3342 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3343 process_type_comp_unit, objfile);
3344 }
3345
3346 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3347
3348 static void
3349 psymtabs_addrmap_cleanup (void *o)
3350 {
3351 struct objfile *objfile = o;
3352
3353 objfile->psymtabs_addrmap = NULL;
3354 }
3355
3356 /* Build the partial symbol table by doing a quick pass through the
3357 .debug_info and .debug_abbrev sections. */
3358
3359 static void
3360 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3361 {
3362 gdb_byte *info_ptr;
3363 struct cleanup *back_to, *addrmap_cleanup;
3364 struct obstack temp_obstack;
3365
3366 dwarf2_per_objfile->reading_partial_symbols = 1;
3367
3368 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3369 info_ptr = dwarf2_per_objfile->info.buffer;
3370
3371 /* Any cached compilation units will be linked by the per-objfile
3372 read_in_chain. Make sure to free them when we're done. */
3373 back_to = make_cleanup (free_cached_comp_units, NULL);
3374
3375 build_type_psymtabs (objfile);
3376
3377 create_all_comp_units (objfile);
3378
3379 /* Create a temporary address map on a temporary obstack. We later
3380 copy this to the final obstack. */
3381 obstack_init (&temp_obstack);
3382 make_cleanup_obstack_free (&temp_obstack);
3383 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3384 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3385
3386 /* Since the objects we're extracting from .debug_info vary in
3387 length, only the individual functions to extract them (like
3388 read_comp_unit_head and load_partial_die) can really know whether
3389 the buffer is large enough to hold another complete object.
3390
3391 At the moment, they don't actually check that. If .debug_info
3392 holds just one extra byte after the last compilation unit's dies,
3393 then read_comp_unit_head will happily read off the end of the
3394 buffer. read_partial_die is similarly casual. Those functions
3395 should be fixed.
3396
3397 For this loop condition, simply checking whether there's any data
3398 left at all should be sufficient. */
3399
3400 while (info_ptr < (dwarf2_per_objfile->info.buffer
3401 + dwarf2_per_objfile->info.size))
3402 {
3403 struct dwarf2_per_cu_data *this_cu;
3404
3405 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3406 objfile);
3407
3408 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3409 dwarf2_per_objfile->info.buffer,
3410 info_ptr,
3411 dwarf2_per_objfile->info.size);
3412 }
3413
3414 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3415 &objfile->objfile_obstack);
3416 discard_cleanups (addrmap_cleanup);
3417
3418 do_cleanups (back_to);
3419 }
3420
3421 /* Load the partial DIEs for a secondary CU into memory. */
3422
3423 static void
3424 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3425 struct objfile *objfile)
3426 {
3427 bfd *abfd = objfile->obfd;
3428 gdb_byte *info_ptr, *beg_of_comp_unit;
3429 struct die_info *comp_unit_die;
3430 struct dwarf2_cu *cu;
3431 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3432 int has_children;
3433 struct die_reader_specs reader_specs;
3434 int read_cu = 0;
3435
3436 gdb_assert (! this_cu->from_debug_types);
3437
3438 gdb_assert (dwarf2_per_objfile->info.readin);
3439 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3440 beg_of_comp_unit = info_ptr;
3441
3442 if (this_cu->cu == NULL)
3443 {
3444 cu = xmalloc (sizeof (*cu));
3445 init_one_comp_unit (cu, objfile);
3446
3447 read_cu = 1;
3448
3449 /* If an error occurs while loading, release our storage. */
3450 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3451
3452 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3453 dwarf2_per_objfile->info.buffer,
3454 dwarf2_per_objfile->info.size,
3455 abfd);
3456
3457 /* Complete the cu_header. */
3458 cu->header.offset = this_cu->offset;
3459 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3460
3461 /* Link this compilation unit into the compilation unit tree. */
3462 this_cu->cu = cu;
3463 cu->per_cu = this_cu;
3464
3465 /* Link this CU into read_in_chain. */
3466 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3467 dwarf2_per_objfile->read_in_chain = this_cu;
3468 }
3469 else
3470 {
3471 cu = this_cu->cu;
3472 info_ptr += cu->header.first_die_offset;
3473 }
3474
3475 /* Read the abbrevs for this compilation unit into a table. */
3476 gdb_assert (cu->dwarf2_abbrevs == NULL);
3477 dwarf2_read_abbrevs (abfd, cu);
3478 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3479
3480 /* Read the compilation unit die. */
3481 init_cu_die_reader (&reader_specs, cu);
3482 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3483 &has_children);
3484
3485 prepare_one_comp_unit (cu, comp_unit_die);
3486
3487 /* Check if comp unit has_children.
3488 If so, read the rest of the partial symbols from this comp unit.
3489 If not, there's no more debug_info for this comp unit. */
3490 if (has_children)
3491 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3492
3493 do_cleanups (free_abbrevs_cleanup);
3494
3495 if (read_cu)
3496 {
3497 /* We've successfully allocated this compilation unit. Let our
3498 caller clean it up when finished with it. */
3499 discard_cleanups (free_cu_cleanup);
3500 }
3501 }
3502
3503 /* Create a list of all compilation units in OBJFILE. We do this only
3504 if an inter-comp-unit reference is found; presumably if there is one,
3505 there will be many, and one will occur early in the .debug_info section.
3506 So there's no point in building this list incrementally. */
3507
3508 static void
3509 create_all_comp_units (struct objfile *objfile)
3510 {
3511 int n_allocated;
3512 int n_comp_units;
3513 struct dwarf2_per_cu_data **all_comp_units;
3514 gdb_byte *info_ptr;
3515
3516 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3517 info_ptr = dwarf2_per_objfile->info.buffer;
3518
3519 n_comp_units = 0;
3520 n_allocated = 10;
3521 all_comp_units = xmalloc (n_allocated
3522 * sizeof (struct dwarf2_per_cu_data *));
3523
3524 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3525 {
3526 unsigned int length, initial_length_size;
3527 struct dwarf2_per_cu_data *this_cu;
3528 unsigned int offset;
3529
3530 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3531
3532 /* Read just enough information to find out where the next
3533 compilation unit is. */
3534 length = read_initial_length (objfile->obfd, info_ptr,
3535 &initial_length_size);
3536
3537 /* Save the compilation unit for later lookup. */
3538 this_cu = obstack_alloc (&objfile->objfile_obstack,
3539 sizeof (struct dwarf2_per_cu_data));
3540 memset (this_cu, 0, sizeof (*this_cu));
3541 this_cu->offset = offset;
3542 this_cu->length = length + initial_length_size;
3543 this_cu->objfile = objfile;
3544
3545 if (n_comp_units == n_allocated)
3546 {
3547 n_allocated *= 2;
3548 all_comp_units = xrealloc (all_comp_units,
3549 n_allocated
3550 * sizeof (struct dwarf2_per_cu_data *));
3551 }
3552 all_comp_units[n_comp_units++] = this_cu;
3553
3554 info_ptr = info_ptr + this_cu->length;
3555 }
3556
3557 dwarf2_per_objfile->all_comp_units
3558 = obstack_alloc (&objfile->objfile_obstack,
3559 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3560 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3561 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3562 xfree (all_comp_units);
3563 dwarf2_per_objfile->n_comp_units = n_comp_units;
3564 }
3565
3566 /* Process all loaded DIEs for compilation unit CU, starting at
3567 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3568 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3569 DW_AT_ranges). If NEED_PC is set, then this function will set
3570 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3571 and record the covered ranges in the addrmap. */
3572
3573 static void
3574 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3575 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3576 {
3577 struct partial_die_info *pdi;
3578
3579 /* Now, march along the PDI's, descending into ones which have
3580 interesting children but skipping the children of the other ones,
3581 until we reach the end of the compilation unit. */
3582
3583 pdi = first_die;
3584
3585 while (pdi != NULL)
3586 {
3587 fixup_partial_die (pdi, cu);
3588
3589 /* Anonymous namespaces or modules have no name but have interesting
3590 children, so we need to look at them. Ditto for anonymous
3591 enums. */
3592
3593 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3594 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3595 {
3596 switch (pdi->tag)
3597 {
3598 case DW_TAG_subprogram:
3599 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3600 break;
3601 case DW_TAG_constant:
3602 case DW_TAG_variable:
3603 case DW_TAG_typedef:
3604 case DW_TAG_union_type:
3605 if (!pdi->is_declaration)
3606 {
3607 add_partial_symbol (pdi, cu);
3608 }
3609 break;
3610 case DW_TAG_class_type:
3611 case DW_TAG_interface_type:
3612 case DW_TAG_structure_type:
3613 if (!pdi->is_declaration)
3614 {
3615 add_partial_symbol (pdi, cu);
3616 }
3617 break;
3618 case DW_TAG_enumeration_type:
3619 if (!pdi->is_declaration)
3620 add_partial_enumeration (pdi, cu);
3621 break;
3622 case DW_TAG_base_type:
3623 case DW_TAG_subrange_type:
3624 /* File scope base type definitions are added to the partial
3625 symbol table. */
3626 add_partial_symbol (pdi, cu);
3627 break;
3628 case DW_TAG_namespace:
3629 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3630 break;
3631 case DW_TAG_module:
3632 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3633 break;
3634 default:
3635 break;
3636 }
3637 }
3638
3639 /* If the die has a sibling, skip to the sibling. */
3640
3641 pdi = pdi->die_sibling;
3642 }
3643 }
3644
3645 /* Functions used to compute the fully scoped name of a partial DIE.
3646
3647 Normally, this is simple. For C++, the parent DIE's fully scoped
3648 name is concatenated with "::" and the partial DIE's name. For
3649 Java, the same thing occurs except that "." is used instead of "::".
3650 Enumerators are an exception; they use the scope of their parent
3651 enumeration type, i.e. the name of the enumeration type is not
3652 prepended to the enumerator.
3653
3654 There are two complexities. One is DW_AT_specification; in this
3655 case "parent" means the parent of the target of the specification,
3656 instead of the direct parent of the DIE. The other is compilers
3657 which do not emit DW_TAG_namespace; in this case we try to guess
3658 the fully qualified name of structure types from their members'
3659 linkage names. This must be done using the DIE's children rather
3660 than the children of any DW_AT_specification target. We only need
3661 to do this for structures at the top level, i.e. if the target of
3662 any DW_AT_specification (if any; otherwise the DIE itself) does not
3663 have a parent. */
3664
3665 /* Compute the scope prefix associated with PDI's parent, in
3666 compilation unit CU. The result will be allocated on CU's
3667 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3668 field. NULL is returned if no prefix is necessary. */
3669 static char *
3670 partial_die_parent_scope (struct partial_die_info *pdi,
3671 struct dwarf2_cu *cu)
3672 {
3673 char *grandparent_scope;
3674 struct partial_die_info *parent, *real_pdi;
3675
3676 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3677 then this means the parent of the specification DIE. */
3678
3679 real_pdi = pdi;
3680 while (real_pdi->has_specification)
3681 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3682
3683 parent = real_pdi->die_parent;
3684 if (parent == NULL)
3685 return NULL;
3686
3687 if (parent->scope_set)
3688 return parent->scope;
3689
3690 fixup_partial_die (parent, cu);
3691
3692 grandparent_scope = partial_die_parent_scope (parent, cu);
3693
3694 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3695 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3696 Work around this problem here. */
3697 if (cu->language == language_cplus
3698 && parent->tag == DW_TAG_namespace
3699 && strcmp (parent->name, "::") == 0
3700 && grandparent_scope == NULL)
3701 {
3702 parent->scope = NULL;
3703 parent->scope_set = 1;
3704 return NULL;
3705 }
3706
3707 if (parent->tag == DW_TAG_namespace
3708 || parent->tag == DW_TAG_module
3709 || parent->tag == DW_TAG_structure_type
3710 || parent->tag == DW_TAG_class_type
3711 || parent->tag == DW_TAG_interface_type
3712 || parent->tag == DW_TAG_union_type
3713 || parent->tag == DW_TAG_enumeration_type)
3714 {
3715 if (grandparent_scope == NULL)
3716 parent->scope = parent->name;
3717 else
3718 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3719 parent->name, 0, cu);
3720 }
3721 else if (parent->tag == DW_TAG_enumerator)
3722 /* Enumerators should not get the name of the enumeration as a prefix. */
3723 parent->scope = grandparent_scope;
3724 else
3725 {
3726 /* FIXME drow/2004-04-01: What should we be doing with
3727 function-local names? For partial symbols, we should probably be
3728 ignoring them. */
3729 complaint (&symfile_complaints,
3730 _("unhandled containing DIE tag %d for DIE at %d"),
3731 parent->tag, pdi->offset);
3732 parent->scope = grandparent_scope;
3733 }
3734
3735 parent->scope_set = 1;
3736 return parent->scope;
3737 }
3738
3739 /* Return the fully scoped name associated with PDI, from compilation unit
3740 CU. The result will be allocated with malloc. */
3741 static char *
3742 partial_die_full_name (struct partial_die_info *pdi,
3743 struct dwarf2_cu *cu)
3744 {
3745 char *parent_scope;
3746
3747 /* If this is a template instantiation, we can not work out the
3748 template arguments from partial DIEs. So, unfortunately, we have
3749 to go through the full DIEs. At least any work we do building
3750 types here will be reused if full symbols are loaded later. */
3751 if (pdi->has_template_arguments)
3752 {
3753 fixup_partial_die (pdi, cu);
3754
3755 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3756 {
3757 struct die_info *die;
3758 struct attribute attr;
3759 struct dwarf2_cu *ref_cu = cu;
3760
3761 attr.name = 0;
3762 attr.form = DW_FORM_ref_addr;
3763 attr.u.addr = pdi->offset;
3764 die = follow_die_ref (NULL, &attr, &ref_cu);
3765
3766 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3767 }
3768 }
3769
3770 parent_scope = partial_die_parent_scope (pdi, cu);
3771 if (parent_scope == NULL)
3772 return NULL;
3773 else
3774 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3775 }
3776
3777 static void
3778 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3779 {
3780 struct objfile *objfile = cu->objfile;
3781 CORE_ADDR addr = 0;
3782 char *actual_name = NULL;
3783 const struct partial_symbol *psym = NULL;
3784 CORE_ADDR baseaddr;
3785 int built_actual_name = 0;
3786
3787 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3788
3789 actual_name = partial_die_full_name (pdi, cu);
3790 if (actual_name)
3791 built_actual_name = 1;
3792
3793 if (actual_name == NULL)
3794 actual_name = pdi->name;
3795
3796 switch (pdi->tag)
3797 {
3798 case DW_TAG_subprogram:
3799 if (pdi->is_external || cu->language == language_ada)
3800 {
3801 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3802 of the global scope. But in Ada, we want to be able to access
3803 nested procedures globally. So all Ada subprograms are stored
3804 in the global scope. */
3805 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3806 mst_text, objfile); */
3807 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3808 built_actual_name,
3809 VAR_DOMAIN, LOC_BLOCK,
3810 &objfile->global_psymbols,
3811 0, pdi->lowpc + baseaddr,
3812 cu->language, objfile);
3813 }
3814 else
3815 {
3816 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3817 mst_file_text, objfile); */
3818 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3819 built_actual_name,
3820 VAR_DOMAIN, LOC_BLOCK,
3821 &objfile->static_psymbols,
3822 0, pdi->lowpc + baseaddr,
3823 cu->language, objfile);
3824 }
3825 break;
3826 case DW_TAG_constant:
3827 {
3828 struct psymbol_allocation_list *list;
3829
3830 if (pdi->is_external)
3831 list = &objfile->global_psymbols;
3832 else
3833 list = &objfile->static_psymbols;
3834 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3835 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3836 list, 0, 0, cu->language, objfile);
3837
3838 }
3839 break;
3840 case DW_TAG_variable:
3841 if (pdi->locdesc)
3842 addr = decode_locdesc (pdi->locdesc, cu);
3843
3844 if (pdi->locdesc
3845 && addr == 0
3846 && !dwarf2_per_objfile->has_section_at_zero)
3847 {
3848 /* A global or static variable may also have been stripped
3849 out by the linker if unused, in which case its address
3850 will be nullified; do not add such variables into partial
3851 symbol table then. */
3852 }
3853 else if (pdi->is_external)
3854 {
3855 /* Global Variable.
3856 Don't enter into the minimal symbol tables as there is
3857 a minimal symbol table entry from the ELF symbols already.
3858 Enter into partial symbol table if it has a location
3859 descriptor or a type.
3860 If the location descriptor is missing, new_symbol will create
3861 a LOC_UNRESOLVED symbol, the address of the variable will then
3862 be determined from the minimal symbol table whenever the variable
3863 is referenced.
3864 The address for the partial symbol table entry is not
3865 used by GDB, but it comes in handy for debugging partial symbol
3866 table building. */
3867
3868 if (pdi->locdesc || pdi->has_type)
3869 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3870 built_actual_name,
3871 VAR_DOMAIN, LOC_STATIC,
3872 &objfile->global_psymbols,
3873 0, addr + baseaddr,
3874 cu->language, objfile);
3875 }
3876 else
3877 {
3878 /* Static Variable. Skip symbols without location descriptors. */
3879 if (pdi->locdesc == NULL)
3880 {
3881 if (built_actual_name)
3882 xfree (actual_name);
3883 return;
3884 }
3885 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3886 mst_file_data, objfile); */
3887 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3888 built_actual_name,
3889 VAR_DOMAIN, LOC_STATIC,
3890 &objfile->static_psymbols,
3891 0, addr + baseaddr,
3892 cu->language, objfile);
3893 }
3894 break;
3895 case DW_TAG_typedef:
3896 case DW_TAG_base_type:
3897 case DW_TAG_subrange_type:
3898 add_psymbol_to_list (actual_name, strlen (actual_name),
3899 built_actual_name,
3900 VAR_DOMAIN, LOC_TYPEDEF,
3901 &objfile->static_psymbols,
3902 0, (CORE_ADDR) 0, cu->language, objfile);
3903 break;
3904 case DW_TAG_namespace:
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
3906 built_actual_name,
3907 VAR_DOMAIN, LOC_TYPEDEF,
3908 &objfile->global_psymbols,
3909 0, (CORE_ADDR) 0, cu->language, objfile);
3910 break;
3911 case DW_TAG_class_type:
3912 case DW_TAG_interface_type:
3913 case DW_TAG_structure_type:
3914 case DW_TAG_union_type:
3915 case DW_TAG_enumeration_type:
3916 /* Skip external references. The DWARF standard says in the section
3917 about "Structure, Union, and Class Type Entries": "An incomplete
3918 structure, union or class type is represented by a structure,
3919 union or class entry that does not have a byte size attribute
3920 and that has a DW_AT_declaration attribute." */
3921 if (!pdi->has_byte_size && pdi->is_declaration)
3922 {
3923 if (built_actual_name)
3924 xfree (actual_name);
3925 return;
3926 }
3927
3928 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3929 static vs. global. */
3930 add_psymbol_to_list (actual_name, strlen (actual_name),
3931 built_actual_name,
3932 STRUCT_DOMAIN, LOC_TYPEDEF,
3933 (cu->language == language_cplus
3934 || cu->language == language_java)
3935 ? &objfile->global_psymbols
3936 : &objfile->static_psymbols,
3937 0, (CORE_ADDR) 0, cu->language, objfile);
3938
3939 break;
3940 case DW_TAG_enumerator:
3941 add_psymbol_to_list (actual_name, strlen (actual_name),
3942 built_actual_name,
3943 VAR_DOMAIN, LOC_CONST,
3944 (cu->language == language_cplus
3945 || cu->language == language_java)
3946 ? &objfile->global_psymbols
3947 : &objfile->static_psymbols,
3948 0, (CORE_ADDR) 0, cu->language, objfile);
3949 break;
3950 default:
3951 break;
3952 }
3953
3954 if (built_actual_name)
3955 xfree (actual_name);
3956 }
3957
3958 /* Read a partial die corresponding to a namespace; also, add a symbol
3959 corresponding to that namespace to the symbol table. NAMESPACE is
3960 the name of the enclosing namespace. */
3961
3962 static void
3963 add_partial_namespace (struct partial_die_info *pdi,
3964 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3965 int need_pc, struct dwarf2_cu *cu)
3966 {
3967 /* Add a symbol for the namespace. */
3968
3969 add_partial_symbol (pdi, cu);
3970
3971 /* Now scan partial symbols in that namespace. */
3972
3973 if (pdi->has_children)
3974 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3975 }
3976
3977 /* Read a partial die corresponding to a Fortran module. */
3978
3979 static void
3980 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3981 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3982 {
3983 /* Now scan partial symbols in that module. */
3984
3985 if (pdi->has_children)
3986 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3987 }
3988
3989 /* Read a partial die corresponding to a subprogram and create a partial
3990 symbol for that subprogram. When the CU language allows it, this
3991 routine also defines a partial symbol for each nested subprogram
3992 that this subprogram contains.
3993
3994 DIE my also be a lexical block, in which case we simply search
3995 recursively for suprograms defined inside that lexical block.
3996 Again, this is only performed when the CU language allows this
3997 type of definitions. */
3998
3999 static void
4000 add_partial_subprogram (struct partial_die_info *pdi,
4001 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4002 int need_pc, struct dwarf2_cu *cu)
4003 {
4004 if (pdi->tag == DW_TAG_subprogram)
4005 {
4006 if (pdi->has_pc_info)
4007 {
4008 if (pdi->lowpc < *lowpc)
4009 *lowpc = pdi->lowpc;
4010 if (pdi->highpc > *highpc)
4011 *highpc = pdi->highpc;
4012 if (need_pc)
4013 {
4014 CORE_ADDR baseaddr;
4015 struct objfile *objfile = cu->objfile;
4016
4017 baseaddr = ANOFFSET (objfile->section_offsets,
4018 SECT_OFF_TEXT (objfile));
4019 addrmap_set_empty (objfile->psymtabs_addrmap,
4020 pdi->lowpc + baseaddr,
4021 pdi->highpc - 1 + baseaddr,
4022 cu->per_cu->v.psymtab);
4023 }
4024 if (!pdi->is_declaration)
4025 /* Ignore subprogram DIEs that do not have a name, they are
4026 illegal. Do not emit a complaint at this point, we will
4027 do so when we convert this psymtab into a symtab. */
4028 if (pdi->name)
4029 add_partial_symbol (pdi, cu);
4030 }
4031 }
4032
4033 if (! pdi->has_children)
4034 return;
4035
4036 if (cu->language == language_ada)
4037 {
4038 pdi = pdi->die_child;
4039 while (pdi != NULL)
4040 {
4041 fixup_partial_die (pdi, cu);
4042 if (pdi->tag == DW_TAG_subprogram
4043 || pdi->tag == DW_TAG_lexical_block)
4044 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4045 pdi = pdi->die_sibling;
4046 }
4047 }
4048 }
4049
4050 /* Read a partial die corresponding to an enumeration type. */
4051
4052 static void
4053 add_partial_enumeration (struct partial_die_info *enum_pdi,
4054 struct dwarf2_cu *cu)
4055 {
4056 struct partial_die_info *pdi;
4057
4058 if (enum_pdi->name != NULL)
4059 add_partial_symbol (enum_pdi, cu);
4060
4061 pdi = enum_pdi->die_child;
4062 while (pdi)
4063 {
4064 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4065 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4066 else
4067 add_partial_symbol (pdi, cu);
4068 pdi = pdi->die_sibling;
4069 }
4070 }
4071
4072 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4073 Return the corresponding abbrev, or NULL if the number is zero (indicating
4074 an empty DIE). In either case *BYTES_READ will be set to the length of
4075 the initial number. */
4076
4077 static struct abbrev_info *
4078 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4079 struct dwarf2_cu *cu)
4080 {
4081 bfd *abfd = cu->objfile->obfd;
4082 unsigned int abbrev_number;
4083 struct abbrev_info *abbrev;
4084
4085 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4086
4087 if (abbrev_number == 0)
4088 return NULL;
4089
4090 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4091 if (!abbrev)
4092 {
4093 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4094 bfd_get_filename (abfd));
4095 }
4096
4097 return abbrev;
4098 }
4099
4100 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4101 Returns a pointer to the end of a series of DIEs, terminated by an empty
4102 DIE. Any children of the skipped DIEs will also be skipped. */
4103
4104 static gdb_byte *
4105 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4106 {
4107 struct abbrev_info *abbrev;
4108 unsigned int bytes_read;
4109
4110 while (1)
4111 {
4112 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4113 if (abbrev == NULL)
4114 return info_ptr + bytes_read;
4115 else
4116 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4117 }
4118 }
4119
4120 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4121 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4122 abbrev corresponding to that skipped uleb128 should be passed in
4123 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4124 children. */
4125
4126 static gdb_byte *
4127 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4128 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4129 {
4130 unsigned int bytes_read;
4131 struct attribute attr;
4132 bfd *abfd = cu->objfile->obfd;
4133 unsigned int form, i;
4134
4135 for (i = 0; i < abbrev->num_attrs; i++)
4136 {
4137 /* The only abbrev we care about is DW_AT_sibling. */
4138 if (abbrev->attrs[i].name == DW_AT_sibling)
4139 {
4140 read_attribute (&attr, &abbrev->attrs[i],
4141 abfd, info_ptr, cu);
4142 if (attr.form == DW_FORM_ref_addr)
4143 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4144 else
4145 return buffer + dwarf2_get_ref_die_offset (&attr);
4146 }
4147
4148 /* If it isn't DW_AT_sibling, skip this attribute. */
4149 form = abbrev->attrs[i].form;
4150 skip_attribute:
4151 switch (form)
4152 {
4153 case DW_FORM_ref_addr:
4154 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4155 and later it is offset sized. */
4156 if (cu->header.version == 2)
4157 info_ptr += cu->header.addr_size;
4158 else
4159 info_ptr += cu->header.offset_size;
4160 break;
4161 case DW_FORM_addr:
4162 info_ptr += cu->header.addr_size;
4163 break;
4164 case DW_FORM_data1:
4165 case DW_FORM_ref1:
4166 case DW_FORM_flag:
4167 info_ptr += 1;
4168 break;
4169 case DW_FORM_flag_present:
4170 break;
4171 case DW_FORM_data2:
4172 case DW_FORM_ref2:
4173 info_ptr += 2;
4174 break;
4175 case DW_FORM_data4:
4176 case DW_FORM_ref4:
4177 info_ptr += 4;
4178 break;
4179 case DW_FORM_data8:
4180 case DW_FORM_ref8:
4181 case DW_FORM_sig8:
4182 info_ptr += 8;
4183 break;
4184 case DW_FORM_string:
4185 read_direct_string (abfd, info_ptr, &bytes_read);
4186 info_ptr += bytes_read;
4187 break;
4188 case DW_FORM_sec_offset:
4189 case DW_FORM_strp:
4190 info_ptr += cu->header.offset_size;
4191 break;
4192 case DW_FORM_exprloc:
4193 case DW_FORM_block:
4194 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4195 info_ptr += bytes_read;
4196 break;
4197 case DW_FORM_block1:
4198 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4199 break;
4200 case DW_FORM_block2:
4201 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4202 break;
4203 case DW_FORM_block4:
4204 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4205 break;
4206 case DW_FORM_sdata:
4207 case DW_FORM_udata:
4208 case DW_FORM_ref_udata:
4209 info_ptr = skip_leb128 (abfd, info_ptr);
4210 break;
4211 case DW_FORM_indirect:
4212 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4213 info_ptr += bytes_read;
4214 /* We need to continue parsing from here, so just go back to
4215 the top. */
4216 goto skip_attribute;
4217
4218 default:
4219 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4220 dwarf_form_name (form),
4221 bfd_get_filename (abfd));
4222 }
4223 }
4224
4225 if (abbrev->has_children)
4226 return skip_children (buffer, info_ptr, cu);
4227 else
4228 return info_ptr;
4229 }
4230
4231 /* Locate ORIG_PDI's sibling.
4232 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4233 in BUFFER. */
4234
4235 static gdb_byte *
4236 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4237 gdb_byte *buffer, gdb_byte *info_ptr,
4238 bfd *abfd, struct dwarf2_cu *cu)
4239 {
4240 /* Do we know the sibling already? */
4241
4242 if (orig_pdi->sibling)
4243 return orig_pdi->sibling;
4244
4245 /* Are there any children to deal with? */
4246
4247 if (!orig_pdi->has_children)
4248 return info_ptr;
4249
4250 /* Skip the children the long way. */
4251
4252 return skip_children (buffer, info_ptr, cu);
4253 }
4254
4255 /* Expand this partial symbol table into a full symbol table. */
4256
4257 static void
4258 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4259 {
4260 if (pst != NULL)
4261 {
4262 if (pst->readin)
4263 {
4264 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4265 }
4266 else
4267 {
4268 if (info_verbose)
4269 {
4270 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4271 gdb_flush (gdb_stdout);
4272 }
4273
4274 /* Restore our global data. */
4275 dwarf2_per_objfile = objfile_data (pst->objfile,
4276 dwarf2_objfile_data_key);
4277
4278 /* If this psymtab is constructed from a debug-only objfile, the
4279 has_section_at_zero flag will not necessarily be correct. We
4280 can get the correct value for this flag by looking at the data
4281 associated with the (presumably stripped) associated objfile. */
4282 if (pst->objfile->separate_debug_objfile_backlink)
4283 {
4284 struct dwarf2_per_objfile *dpo_backlink
4285 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4286 dwarf2_objfile_data_key);
4287
4288 dwarf2_per_objfile->has_section_at_zero
4289 = dpo_backlink->has_section_at_zero;
4290 }
4291
4292 dwarf2_per_objfile->reading_partial_symbols = 0;
4293
4294 psymtab_to_symtab_1 (pst);
4295
4296 /* Finish up the debug error message. */
4297 if (info_verbose)
4298 printf_filtered (_("done.\n"));
4299 }
4300 }
4301 }
4302
4303 /* Add PER_CU to the queue. */
4304
4305 static void
4306 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4307 {
4308 struct dwarf2_queue_item *item;
4309
4310 per_cu->queued = 1;
4311 item = xmalloc (sizeof (*item));
4312 item->per_cu = per_cu;
4313 item->next = NULL;
4314
4315 if (dwarf2_queue == NULL)
4316 dwarf2_queue = item;
4317 else
4318 dwarf2_queue_tail->next = item;
4319
4320 dwarf2_queue_tail = item;
4321 }
4322
4323 /* Process the queue. */
4324
4325 static void
4326 process_queue (struct objfile *objfile)
4327 {
4328 struct dwarf2_queue_item *item, *next_item;
4329
4330 /* The queue starts out with one item, but following a DIE reference
4331 may load a new CU, adding it to the end of the queue. */
4332 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4333 {
4334 if (dwarf2_per_objfile->using_index
4335 ? !item->per_cu->v.quick->symtab
4336 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4337 process_full_comp_unit (item->per_cu);
4338
4339 item->per_cu->queued = 0;
4340 next_item = item->next;
4341 xfree (item);
4342 }
4343
4344 dwarf2_queue_tail = NULL;
4345 }
4346
4347 /* Free all allocated queue entries. This function only releases anything if
4348 an error was thrown; if the queue was processed then it would have been
4349 freed as we went along. */
4350
4351 static void
4352 dwarf2_release_queue (void *dummy)
4353 {
4354 struct dwarf2_queue_item *item, *last;
4355
4356 item = dwarf2_queue;
4357 while (item)
4358 {
4359 /* Anything still marked queued is likely to be in an
4360 inconsistent state, so discard it. */
4361 if (item->per_cu->queued)
4362 {
4363 if (item->per_cu->cu != NULL)
4364 free_one_cached_comp_unit (item->per_cu->cu);
4365 item->per_cu->queued = 0;
4366 }
4367
4368 last = item;
4369 item = item->next;
4370 xfree (last);
4371 }
4372
4373 dwarf2_queue = dwarf2_queue_tail = NULL;
4374 }
4375
4376 /* Read in full symbols for PST, and anything it depends on. */
4377
4378 static void
4379 psymtab_to_symtab_1 (struct partial_symtab *pst)
4380 {
4381 struct dwarf2_per_cu_data *per_cu;
4382 struct cleanup *back_to;
4383 int i;
4384
4385 for (i = 0; i < pst->number_of_dependencies; i++)
4386 if (!pst->dependencies[i]->readin)
4387 {
4388 /* Inform about additional files that need to be read in. */
4389 if (info_verbose)
4390 {
4391 /* FIXME: i18n: Need to make this a single string. */
4392 fputs_filtered (" ", gdb_stdout);
4393 wrap_here ("");
4394 fputs_filtered ("and ", gdb_stdout);
4395 wrap_here ("");
4396 printf_filtered ("%s...", pst->dependencies[i]->filename);
4397 wrap_here (""); /* Flush output */
4398 gdb_flush (gdb_stdout);
4399 }
4400 psymtab_to_symtab_1 (pst->dependencies[i]);
4401 }
4402
4403 per_cu = pst->read_symtab_private;
4404
4405 if (per_cu == NULL)
4406 {
4407 /* It's an include file, no symbols to read for it.
4408 Everything is in the parent symtab. */
4409 pst->readin = 1;
4410 return;
4411 }
4412
4413 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4414 }
4415
4416 /* Load the DIEs associated with PER_CU into memory. */
4417
4418 static void
4419 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4420 {
4421 bfd *abfd = objfile->obfd;
4422 struct dwarf2_cu *cu;
4423 unsigned int offset;
4424 gdb_byte *info_ptr, *beg_of_comp_unit;
4425 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4426 struct attribute *attr;
4427 int read_cu = 0;
4428
4429 gdb_assert (! per_cu->from_debug_types);
4430
4431 /* Set local variables from the partial symbol table info. */
4432 offset = per_cu->offset;
4433
4434 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4435 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4436 beg_of_comp_unit = info_ptr;
4437
4438 if (per_cu->cu == NULL)
4439 {
4440 cu = xmalloc (sizeof (*cu));
4441 init_one_comp_unit (cu, objfile);
4442
4443 read_cu = 1;
4444
4445 /* If an error occurs while loading, release our storage. */
4446 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4447
4448 /* Read in the comp_unit header. */
4449 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4450
4451 /* Complete the cu_header. */
4452 cu->header.offset = offset;
4453 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4454
4455 /* Read the abbrevs for this compilation unit. */
4456 dwarf2_read_abbrevs (abfd, cu);
4457 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4458
4459 /* Link this compilation unit into the compilation unit tree. */
4460 per_cu->cu = cu;
4461 cu->per_cu = per_cu;
4462
4463 /* Link this CU into read_in_chain. */
4464 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4465 dwarf2_per_objfile->read_in_chain = per_cu;
4466 }
4467 else
4468 {
4469 cu = per_cu->cu;
4470 info_ptr += cu->header.first_die_offset;
4471 }
4472
4473 cu->dies = read_comp_unit (info_ptr, cu);
4474
4475 /* We try not to read any attributes in this function, because not
4476 all objfiles needed for references have been loaded yet, and symbol
4477 table processing isn't initialized. But we have to set the CU language,
4478 or we won't be able to build types correctly. */
4479 prepare_one_comp_unit (cu, cu->dies);
4480
4481 /* Similarly, if we do not read the producer, we can not apply
4482 producer-specific interpretation. */
4483 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4484 if (attr)
4485 cu->producer = DW_STRING (attr);
4486
4487 if (read_cu)
4488 {
4489 do_cleanups (free_abbrevs_cleanup);
4490
4491 /* We've successfully allocated this compilation unit. Let our
4492 caller clean it up when finished with it. */
4493 discard_cleanups (free_cu_cleanup);
4494 }
4495 }
4496
4497 /* Add a DIE to the delayed physname list. */
4498
4499 static void
4500 add_to_method_list (struct type *type, int fnfield_index, int index,
4501 const char *name, struct die_info *die,
4502 struct dwarf2_cu *cu)
4503 {
4504 struct delayed_method_info mi;
4505 mi.type = type;
4506 mi.fnfield_index = fnfield_index;
4507 mi.index = index;
4508 mi.name = name;
4509 mi.die = die;
4510 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4511 }
4512
4513 /* A cleanup for freeing the delayed method list. */
4514
4515 static void
4516 free_delayed_list (void *ptr)
4517 {
4518 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4519 if (cu->method_list != NULL)
4520 {
4521 VEC_free (delayed_method_info, cu->method_list);
4522 cu->method_list = NULL;
4523 }
4524 }
4525
4526 /* Compute the physnames of any methods on the CU's method list.
4527
4528 The computation of method physnames is delayed in order to avoid the
4529 (bad) condition that one of the method's formal parameters is of an as yet
4530 incomplete type. */
4531
4532 static void
4533 compute_delayed_physnames (struct dwarf2_cu *cu)
4534 {
4535 int i;
4536 struct delayed_method_info *mi;
4537 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4538 {
4539 char *physname;
4540 struct fn_fieldlist *fn_flp
4541 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4542 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4543 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4544 }
4545 }
4546
4547 /* Generate full symbol information for PST and CU, whose DIEs have
4548 already been loaded into memory. */
4549
4550 static void
4551 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4552 {
4553 struct dwarf2_cu *cu = per_cu->cu;
4554 struct objfile *objfile = per_cu->objfile;
4555 CORE_ADDR lowpc, highpc;
4556 struct symtab *symtab;
4557 struct cleanup *back_to, *delayed_list_cleanup;
4558 CORE_ADDR baseaddr;
4559
4560 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4561
4562 buildsym_init ();
4563 back_to = make_cleanup (really_free_pendings, NULL);
4564 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4565
4566 cu->list_in_scope = &file_symbols;
4567
4568 dwarf2_find_base_address (cu->dies, cu);
4569
4570 /* Do line number decoding in read_file_scope () */
4571 process_die (cu->dies, cu);
4572
4573 /* Now that we have processed all the DIEs in the CU, all the types
4574 should be complete, and it should now be safe to compute all of the
4575 physnames. */
4576 compute_delayed_physnames (cu);
4577 do_cleanups (delayed_list_cleanup);
4578
4579 /* Some compilers don't define a DW_AT_high_pc attribute for the
4580 compilation unit. If the DW_AT_high_pc is missing, synthesize
4581 it, by scanning the DIE's below the compilation unit. */
4582 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4583
4584 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4585
4586 /* Set symtab language to language from DW_AT_language.
4587 If the compilation is from a C file generated by language preprocessors,
4588 do not set the language if it was already deduced by start_subfile. */
4589 if (symtab != NULL
4590 && !(cu->language == language_c && symtab->language != language_c))
4591 {
4592 symtab->language = cu->language;
4593 }
4594
4595 if (dwarf2_per_objfile->using_index)
4596 per_cu->v.quick->symtab = symtab;
4597 else
4598 {
4599 struct partial_symtab *pst = per_cu->v.psymtab;
4600 pst->symtab = symtab;
4601 pst->readin = 1;
4602 }
4603
4604 do_cleanups (back_to);
4605 }
4606
4607 /* Process a die and its children. */
4608
4609 static void
4610 process_die (struct die_info *die, struct dwarf2_cu *cu)
4611 {
4612 switch (die->tag)
4613 {
4614 case DW_TAG_padding:
4615 break;
4616 case DW_TAG_compile_unit:
4617 read_file_scope (die, cu);
4618 break;
4619 case DW_TAG_type_unit:
4620 read_type_unit_scope (die, cu);
4621 break;
4622 case DW_TAG_subprogram:
4623 case DW_TAG_inlined_subroutine:
4624 read_func_scope (die, cu);
4625 break;
4626 case DW_TAG_lexical_block:
4627 case DW_TAG_try_block:
4628 case DW_TAG_catch_block:
4629 read_lexical_block_scope (die, cu);
4630 break;
4631 case DW_TAG_class_type:
4632 case DW_TAG_interface_type:
4633 case DW_TAG_structure_type:
4634 case DW_TAG_union_type:
4635 process_structure_scope (die, cu);
4636 break;
4637 case DW_TAG_enumeration_type:
4638 process_enumeration_scope (die, cu);
4639 break;
4640
4641 /* These dies have a type, but processing them does not create
4642 a symbol or recurse to process the children. Therefore we can
4643 read them on-demand through read_type_die. */
4644 case DW_TAG_subroutine_type:
4645 case DW_TAG_set_type:
4646 case DW_TAG_array_type:
4647 case DW_TAG_pointer_type:
4648 case DW_TAG_ptr_to_member_type:
4649 case DW_TAG_reference_type:
4650 case DW_TAG_string_type:
4651 break;
4652
4653 case DW_TAG_base_type:
4654 case DW_TAG_subrange_type:
4655 case DW_TAG_typedef:
4656 /* Add a typedef symbol for the type definition, if it has a
4657 DW_AT_name. */
4658 new_symbol (die, read_type_die (die, cu), cu);
4659 break;
4660 case DW_TAG_common_block:
4661 read_common_block (die, cu);
4662 break;
4663 case DW_TAG_common_inclusion:
4664 break;
4665 case DW_TAG_namespace:
4666 processing_has_namespace_info = 1;
4667 read_namespace (die, cu);
4668 break;
4669 case DW_TAG_module:
4670 processing_has_namespace_info = 1;
4671 read_module (die, cu);
4672 break;
4673 case DW_TAG_imported_declaration:
4674 case DW_TAG_imported_module:
4675 processing_has_namespace_info = 1;
4676 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4677 || cu->language != language_fortran))
4678 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4679 dwarf_tag_name (die->tag));
4680 read_import_statement (die, cu);
4681 break;
4682 default:
4683 new_symbol (die, NULL, cu);
4684 break;
4685 }
4686 }
4687
4688 /* A helper function for dwarf2_compute_name which determines whether DIE
4689 needs to have the name of the scope prepended to the name listed in the
4690 die. */
4691
4692 static int
4693 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4694 {
4695 struct attribute *attr;
4696
4697 switch (die->tag)
4698 {
4699 case DW_TAG_namespace:
4700 case DW_TAG_typedef:
4701 case DW_TAG_class_type:
4702 case DW_TAG_interface_type:
4703 case DW_TAG_structure_type:
4704 case DW_TAG_union_type:
4705 case DW_TAG_enumeration_type:
4706 case DW_TAG_enumerator:
4707 case DW_TAG_subprogram:
4708 case DW_TAG_member:
4709 return 1;
4710
4711 case DW_TAG_variable:
4712 case DW_TAG_constant:
4713 /* We only need to prefix "globally" visible variables. These include
4714 any variable marked with DW_AT_external or any variable that
4715 lives in a namespace. [Variables in anonymous namespaces
4716 require prefixing, but they are not DW_AT_external.] */
4717
4718 if (dwarf2_attr (die, DW_AT_specification, cu))
4719 {
4720 struct dwarf2_cu *spec_cu = cu;
4721
4722 return die_needs_namespace (die_specification (die, &spec_cu),
4723 spec_cu);
4724 }
4725
4726 attr = dwarf2_attr (die, DW_AT_external, cu);
4727 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4728 && die->parent->tag != DW_TAG_module)
4729 return 0;
4730 /* A variable in a lexical block of some kind does not need a
4731 namespace, even though in C++ such variables may be external
4732 and have a mangled name. */
4733 if (die->parent->tag == DW_TAG_lexical_block
4734 || die->parent->tag == DW_TAG_try_block
4735 || die->parent->tag == DW_TAG_catch_block
4736 || die->parent->tag == DW_TAG_subprogram)
4737 return 0;
4738 return 1;
4739
4740 default:
4741 return 0;
4742 }
4743 }
4744
4745 /* Retrieve the last character from a mem_file. */
4746
4747 static void
4748 do_ui_file_peek_last (void *object, const char *buffer, long length)
4749 {
4750 char *last_char_p = (char *) object;
4751
4752 if (length > 0)
4753 *last_char_p = buffer[length - 1];
4754 }
4755
4756 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4757 compute the physname for the object, which include a method's
4758 formal parameters (C++/Java) and return type (Java).
4759
4760 For Ada, return the DIE's linkage name rather than the fully qualified
4761 name. PHYSNAME is ignored..
4762
4763 The result is allocated on the objfile_obstack and canonicalized. */
4764
4765 static const char *
4766 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4767 int physname)
4768 {
4769 if (name == NULL)
4770 name = dwarf2_name (die, cu);
4771
4772 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4773 compute it by typename_concat inside GDB. */
4774 if (cu->language == language_ada
4775 || (cu->language == language_fortran && physname))
4776 {
4777 /* For Ada unit, we prefer the linkage name over the name, as
4778 the former contains the exported name, which the user expects
4779 to be able to reference. Ideally, we want the user to be able
4780 to reference this entity using either natural or linkage name,
4781 but we haven't started looking at this enhancement yet. */
4782 struct attribute *attr;
4783
4784 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4785 if (attr == NULL)
4786 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4787 if (attr && DW_STRING (attr))
4788 return DW_STRING (attr);
4789 }
4790
4791 /* These are the only languages we know how to qualify names in. */
4792 if (name != NULL
4793 && (cu->language == language_cplus || cu->language == language_java
4794 || cu->language == language_fortran))
4795 {
4796 if (die_needs_namespace (die, cu))
4797 {
4798 long length;
4799 char *prefix;
4800 struct ui_file *buf;
4801
4802 prefix = determine_prefix (die, cu);
4803 buf = mem_fileopen ();
4804 if (*prefix != '\0')
4805 {
4806 char *prefixed_name = typename_concat (NULL, prefix, name,
4807 physname, cu);
4808
4809 fputs_unfiltered (prefixed_name, buf);
4810 xfree (prefixed_name);
4811 }
4812 else
4813 fputs_unfiltered (name ? name : "", buf);
4814
4815 /* Template parameters may be specified in the DIE's DW_AT_name, or
4816 as children with DW_TAG_template_type_param or
4817 DW_TAG_value_type_param. If the latter, add them to the name
4818 here. If the name already has template parameters, then
4819 skip this step; some versions of GCC emit both, and
4820 it is more efficient to use the pre-computed name.
4821
4822 Something to keep in mind about this process: it is very
4823 unlikely, or in some cases downright impossible, to produce
4824 something that will match the mangled name of a function.
4825 If the definition of the function has the same debug info,
4826 we should be able to match up with it anyway. But fallbacks
4827 using the minimal symbol, for instance to find a method
4828 implemented in a stripped copy of libstdc++, will not work.
4829 If we do not have debug info for the definition, we will have to
4830 match them up some other way.
4831
4832 When we do name matching there is a related problem with function
4833 templates; two instantiated function templates are allowed to
4834 differ only by their return types, which we do not add here. */
4835
4836 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4837 {
4838 struct attribute *attr;
4839 struct die_info *child;
4840 int first = 1;
4841
4842 die->building_fullname = 1;
4843
4844 for (child = die->child; child != NULL; child = child->sibling)
4845 {
4846 struct type *type;
4847 long value;
4848 gdb_byte *bytes;
4849 struct dwarf2_locexpr_baton *baton;
4850 struct value *v;
4851
4852 if (child->tag != DW_TAG_template_type_param
4853 && child->tag != DW_TAG_template_value_param)
4854 continue;
4855
4856 if (first)
4857 {
4858 fputs_unfiltered ("<", buf);
4859 first = 0;
4860 }
4861 else
4862 fputs_unfiltered (", ", buf);
4863
4864 attr = dwarf2_attr (child, DW_AT_type, cu);
4865 if (attr == NULL)
4866 {
4867 complaint (&symfile_complaints,
4868 _("template parameter missing DW_AT_type"));
4869 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4870 continue;
4871 }
4872 type = die_type (child, cu);
4873
4874 if (child->tag == DW_TAG_template_type_param)
4875 {
4876 c_print_type (type, "", buf, -1, 0);
4877 continue;
4878 }
4879
4880 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4881 if (attr == NULL)
4882 {
4883 complaint (&symfile_complaints,
4884 _("template parameter missing DW_AT_const_value"));
4885 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4886 continue;
4887 }
4888
4889 dwarf2_const_value_attr (attr, type, name,
4890 &cu->comp_unit_obstack, cu,
4891 &value, &bytes, &baton);
4892
4893 if (TYPE_NOSIGN (type))
4894 /* GDB prints characters as NUMBER 'CHAR'. If that's
4895 changed, this can use value_print instead. */
4896 c_printchar (value, type, buf);
4897 else
4898 {
4899 struct value_print_options opts;
4900
4901 if (baton != NULL)
4902 v = dwarf2_evaluate_loc_desc (type, NULL,
4903 baton->data,
4904 baton->size,
4905 baton->per_cu);
4906 else if (bytes != NULL)
4907 {
4908 v = allocate_value (type);
4909 memcpy (value_contents_writeable (v), bytes,
4910 TYPE_LENGTH (type));
4911 }
4912 else
4913 v = value_from_longest (type, value);
4914
4915 /* Specify decimal so that we do not depend on the radix. */
4916 get_formatted_print_options (&opts, 'd');
4917 opts.raw = 1;
4918 value_print (v, buf, &opts);
4919 release_value (v);
4920 value_free (v);
4921 }
4922 }
4923
4924 die->building_fullname = 0;
4925
4926 if (!first)
4927 {
4928 /* Close the argument list, with a space if necessary
4929 (nested templates). */
4930 char last_char = '\0';
4931 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4932 if (last_char == '>')
4933 fputs_unfiltered (" >", buf);
4934 else
4935 fputs_unfiltered (">", buf);
4936 }
4937 }
4938
4939 /* For Java and C++ methods, append formal parameter type
4940 information, if PHYSNAME. */
4941
4942 if (physname && die->tag == DW_TAG_subprogram
4943 && (cu->language == language_cplus
4944 || cu->language == language_java))
4945 {
4946 struct type *type = read_type_die (die, cu);
4947
4948 c_type_print_args (type, buf, 0, cu->language);
4949
4950 if (cu->language == language_java)
4951 {
4952 /* For java, we must append the return type to method
4953 names. */
4954 if (die->tag == DW_TAG_subprogram)
4955 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4956 0, 0);
4957 }
4958 else if (cu->language == language_cplus)
4959 {
4960 /* Assume that an artificial first parameter is
4961 "this", but do not crash if it is not. RealView
4962 marks unnamed (and thus unused) parameters as
4963 artificial; there is no way to differentiate
4964 the two cases. */
4965 if (TYPE_NFIELDS (type) > 0
4966 && TYPE_FIELD_ARTIFICIAL (type, 0)
4967 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4968 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4969 fputs_unfiltered (" const", buf);
4970 }
4971 }
4972
4973 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4974 &length);
4975 ui_file_delete (buf);
4976
4977 if (cu->language == language_cplus)
4978 {
4979 char *cname
4980 = dwarf2_canonicalize_name (name, cu,
4981 &cu->objfile->objfile_obstack);
4982
4983 if (cname != NULL)
4984 name = cname;
4985 }
4986 }
4987 }
4988
4989 return name;
4990 }
4991
4992 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4993 If scope qualifiers are appropriate they will be added. The result
4994 will be allocated on the objfile_obstack, or NULL if the DIE does
4995 not have a name. NAME may either be from a previous call to
4996 dwarf2_name or NULL.
4997
4998 The output string will be canonicalized (if C++/Java). */
4999
5000 static const char *
5001 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5002 {
5003 return dwarf2_compute_name (name, die, cu, 0);
5004 }
5005
5006 /* Construct a physname for the given DIE in CU. NAME may either be
5007 from a previous call to dwarf2_name or NULL. The result will be
5008 allocated on the objfile_objstack or NULL if the DIE does not have a
5009 name.
5010
5011 The output string will be canonicalized (if C++/Java). */
5012
5013 static const char *
5014 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5015 {
5016 return dwarf2_compute_name (name, die, cu, 1);
5017 }
5018
5019 /* Read the import statement specified by the given die and record it. */
5020
5021 static void
5022 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5023 {
5024 struct attribute *import_attr;
5025 struct die_info *imported_die;
5026 struct dwarf2_cu *imported_cu;
5027 const char *imported_name;
5028 const char *imported_name_prefix;
5029 const char *canonical_name;
5030 const char *import_alias;
5031 const char *imported_declaration = NULL;
5032 const char *import_prefix;
5033
5034 char *temp;
5035
5036 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5037 if (import_attr == NULL)
5038 {
5039 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5040 dwarf_tag_name (die->tag));
5041 return;
5042 }
5043
5044 imported_cu = cu;
5045 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5046 imported_name = dwarf2_name (imported_die, imported_cu);
5047 if (imported_name == NULL)
5048 {
5049 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5050
5051 The import in the following code:
5052 namespace A
5053 {
5054 typedef int B;
5055 }
5056
5057 int main ()
5058 {
5059 using A::B;
5060 B b;
5061 return b;
5062 }
5063
5064 ...
5065 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5066 <52> DW_AT_decl_file : 1
5067 <53> DW_AT_decl_line : 6
5068 <54> DW_AT_import : <0x75>
5069 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5070 <59> DW_AT_name : B
5071 <5b> DW_AT_decl_file : 1
5072 <5c> DW_AT_decl_line : 2
5073 <5d> DW_AT_type : <0x6e>
5074 ...
5075 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5076 <76> DW_AT_byte_size : 4
5077 <77> DW_AT_encoding : 5 (signed)
5078
5079 imports the wrong die ( 0x75 instead of 0x58 ).
5080 This case will be ignored until the gcc bug is fixed. */
5081 return;
5082 }
5083
5084 /* Figure out the local name after import. */
5085 import_alias = dwarf2_name (die, cu);
5086
5087 /* Figure out where the statement is being imported to. */
5088 import_prefix = determine_prefix (die, cu);
5089
5090 /* Figure out what the scope of the imported die is and prepend it
5091 to the name of the imported die. */
5092 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5093
5094 if (imported_die->tag != DW_TAG_namespace
5095 && imported_die->tag != DW_TAG_module)
5096 {
5097 imported_declaration = imported_name;
5098 canonical_name = imported_name_prefix;
5099 }
5100 else if (strlen (imported_name_prefix) > 0)
5101 {
5102 temp = alloca (strlen (imported_name_prefix)
5103 + 2 + strlen (imported_name) + 1);
5104 strcpy (temp, imported_name_prefix);
5105 strcat (temp, "::");
5106 strcat (temp, imported_name);
5107 canonical_name = temp;
5108 }
5109 else
5110 canonical_name = imported_name;
5111
5112 cp_add_using_directive (import_prefix,
5113 canonical_name,
5114 import_alias,
5115 imported_declaration,
5116 &cu->objfile->objfile_obstack);
5117 }
5118
5119 static void
5120 initialize_cu_func_list (struct dwarf2_cu *cu)
5121 {
5122 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5123 }
5124
5125 /* Cleanup function for read_file_scope. */
5126
5127 static void
5128 free_cu_line_header (void *arg)
5129 {
5130 struct dwarf2_cu *cu = arg;
5131
5132 free_line_header (cu->line_header);
5133 cu->line_header = NULL;
5134 }
5135
5136 static void
5137 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5138 char **name, char **comp_dir)
5139 {
5140 struct attribute *attr;
5141
5142 *name = NULL;
5143 *comp_dir = NULL;
5144
5145 /* Find the filename. Do not use dwarf2_name here, since the filename
5146 is not a source language identifier. */
5147 attr = dwarf2_attr (die, DW_AT_name, cu);
5148 if (attr)
5149 {
5150 *name = DW_STRING (attr);
5151 }
5152
5153 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5154 if (attr)
5155 *comp_dir = DW_STRING (attr);
5156 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5157 {
5158 *comp_dir = ldirname (*name);
5159 if (*comp_dir != NULL)
5160 make_cleanup (xfree, *comp_dir);
5161 }
5162 if (*comp_dir != NULL)
5163 {
5164 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5165 directory, get rid of it. */
5166 char *cp = strchr (*comp_dir, ':');
5167
5168 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5169 *comp_dir = cp + 1;
5170 }
5171
5172 if (*name == NULL)
5173 *name = "<unknown>";
5174 }
5175
5176 /* Process DW_TAG_compile_unit. */
5177
5178 static void
5179 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5180 {
5181 struct objfile *objfile = cu->objfile;
5182 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5183 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5184 CORE_ADDR highpc = ((CORE_ADDR) 0);
5185 struct attribute *attr;
5186 char *name = NULL;
5187 char *comp_dir = NULL;
5188 struct die_info *child_die;
5189 bfd *abfd = objfile->obfd;
5190 struct line_header *line_header = 0;
5191 CORE_ADDR baseaddr;
5192
5193 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5194
5195 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5196
5197 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5198 from finish_block. */
5199 if (lowpc == ((CORE_ADDR) -1))
5200 lowpc = highpc;
5201 lowpc += baseaddr;
5202 highpc += baseaddr;
5203
5204 find_file_and_directory (die, cu, &name, &comp_dir);
5205
5206 attr = dwarf2_attr (die, DW_AT_language, cu);
5207 if (attr)
5208 {
5209 set_cu_language (DW_UNSND (attr), cu);
5210 }
5211
5212 attr = dwarf2_attr (die, DW_AT_producer, cu);
5213 if (attr)
5214 cu->producer = DW_STRING (attr);
5215
5216 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5217 standardised yet. As a workaround for the language detection we fall
5218 back to the DW_AT_producer string. */
5219 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5220 cu->language = language_opencl;
5221
5222 /* We assume that we're processing GCC output. */
5223 processing_gcc_compilation = 2;
5224
5225 processing_has_namespace_info = 0;
5226
5227 start_symtab (name, comp_dir, lowpc);
5228 record_debugformat ("DWARF 2");
5229 record_producer (cu->producer);
5230
5231 initialize_cu_func_list (cu);
5232
5233 /* Decode line number information if present. We do this before
5234 processing child DIEs, so that the line header table is available
5235 for DW_AT_decl_file. */
5236 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5237 if (attr)
5238 {
5239 unsigned int line_offset = DW_UNSND (attr);
5240 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5241 if (line_header)
5242 {
5243 cu->line_header = line_header;
5244 make_cleanup (free_cu_line_header, cu);
5245 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5246 }
5247 }
5248
5249 /* Process all dies in compilation unit. */
5250 if (die->child != NULL)
5251 {
5252 child_die = die->child;
5253 while (child_die && child_die->tag)
5254 {
5255 process_die (child_die, cu);
5256 child_die = sibling_die (child_die);
5257 }
5258 }
5259
5260 /* Decode macro information, if present. Dwarf 2 macro information
5261 refers to information in the line number info statement program
5262 header, so we can only read it if we've read the header
5263 successfully. */
5264 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5265 if (attr && line_header)
5266 {
5267 unsigned int macro_offset = DW_UNSND (attr);
5268
5269 dwarf_decode_macros (line_header, macro_offset,
5270 comp_dir, abfd, cu);
5271 }
5272 do_cleanups (back_to);
5273 }
5274
5275 /* Process DW_TAG_type_unit.
5276 For TUs we want to skip the first top level sibling if it's not the
5277 actual type being defined by this TU. In this case the first top
5278 level sibling is there to provide context only. */
5279
5280 static void
5281 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5282 {
5283 struct objfile *objfile = cu->objfile;
5284 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5285 CORE_ADDR lowpc;
5286 struct attribute *attr;
5287 char *name = NULL;
5288 char *comp_dir = NULL;
5289 struct die_info *child_die;
5290 bfd *abfd = objfile->obfd;
5291
5292 /* start_symtab needs a low pc, but we don't really have one.
5293 Do what read_file_scope would do in the absence of such info. */
5294 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5295
5296 /* Find the filename. Do not use dwarf2_name here, since the filename
5297 is not a source language identifier. */
5298 attr = dwarf2_attr (die, DW_AT_name, cu);
5299 if (attr)
5300 name = DW_STRING (attr);
5301
5302 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5303 if (attr)
5304 comp_dir = DW_STRING (attr);
5305 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5306 {
5307 comp_dir = ldirname (name);
5308 if (comp_dir != NULL)
5309 make_cleanup (xfree, comp_dir);
5310 }
5311
5312 if (name == NULL)
5313 name = "<unknown>";
5314
5315 attr = dwarf2_attr (die, DW_AT_language, cu);
5316 if (attr)
5317 set_cu_language (DW_UNSND (attr), cu);
5318
5319 /* This isn't technically needed today. It is done for symmetry
5320 with read_file_scope. */
5321 attr = dwarf2_attr (die, DW_AT_producer, cu);
5322 if (attr)
5323 cu->producer = DW_STRING (attr);
5324
5325 /* We assume that we're processing GCC output. */
5326 processing_gcc_compilation = 2;
5327
5328 processing_has_namespace_info = 0;
5329
5330 start_symtab (name, comp_dir, lowpc);
5331 record_debugformat ("DWARF 2");
5332 record_producer (cu->producer);
5333
5334 /* Process the dies in the type unit. */
5335 if (die->child == NULL)
5336 {
5337 dump_die_for_error (die);
5338 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5339 bfd_get_filename (abfd));
5340 }
5341
5342 child_die = die->child;
5343
5344 while (child_die && child_die->tag)
5345 {
5346 process_die (child_die, cu);
5347
5348 child_die = sibling_die (child_die);
5349 }
5350
5351 do_cleanups (back_to);
5352 }
5353
5354 static void
5355 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5356 struct dwarf2_cu *cu)
5357 {
5358 struct function_range *thisfn;
5359
5360 thisfn = (struct function_range *)
5361 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5362 thisfn->name = name;
5363 thisfn->lowpc = lowpc;
5364 thisfn->highpc = highpc;
5365 thisfn->seen_line = 0;
5366 thisfn->next = NULL;
5367
5368 if (cu->last_fn == NULL)
5369 cu->first_fn = thisfn;
5370 else
5371 cu->last_fn->next = thisfn;
5372
5373 cu->last_fn = thisfn;
5374 }
5375
5376 /* qsort helper for inherit_abstract_dies. */
5377
5378 static int
5379 unsigned_int_compar (const void *ap, const void *bp)
5380 {
5381 unsigned int a = *(unsigned int *) ap;
5382 unsigned int b = *(unsigned int *) bp;
5383
5384 return (a > b) - (b > a);
5385 }
5386
5387 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5388 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5389 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5390
5391 static void
5392 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5393 {
5394 struct die_info *child_die;
5395 unsigned die_children_count;
5396 /* CU offsets which were referenced by children of the current DIE. */
5397 unsigned *offsets;
5398 unsigned *offsets_end, *offsetp;
5399 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5400 struct die_info *origin_die;
5401 /* Iterator of the ORIGIN_DIE children. */
5402 struct die_info *origin_child_die;
5403 struct cleanup *cleanups;
5404 struct attribute *attr;
5405 struct dwarf2_cu *origin_cu;
5406 struct pending **origin_previous_list_in_scope;
5407
5408 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5409 if (!attr)
5410 return;
5411
5412 /* Note that following die references may follow to a die in a
5413 different cu. */
5414
5415 origin_cu = cu;
5416 origin_die = follow_die_ref (die, attr, &origin_cu);
5417
5418 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5419 symbols in. */
5420 origin_previous_list_in_scope = origin_cu->list_in_scope;
5421 origin_cu->list_in_scope = cu->list_in_scope;
5422
5423 if (die->tag != origin_die->tag
5424 && !(die->tag == DW_TAG_inlined_subroutine
5425 && origin_die->tag == DW_TAG_subprogram))
5426 complaint (&symfile_complaints,
5427 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5428 die->offset, origin_die->offset);
5429
5430 child_die = die->child;
5431 die_children_count = 0;
5432 while (child_die && child_die->tag)
5433 {
5434 child_die = sibling_die (child_die);
5435 die_children_count++;
5436 }
5437 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5438 cleanups = make_cleanup (xfree, offsets);
5439
5440 offsets_end = offsets;
5441 child_die = die->child;
5442 while (child_die && child_die->tag)
5443 {
5444 /* For each CHILD_DIE, find the corresponding child of
5445 ORIGIN_DIE. If there is more than one layer of
5446 DW_AT_abstract_origin, follow them all; there shouldn't be,
5447 but GCC versions at least through 4.4 generate this (GCC PR
5448 40573). */
5449 struct die_info *child_origin_die = child_die;
5450 struct dwarf2_cu *child_origin_cu = cu;
5451
5452 while (1)
5453 {
5454 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5455 child_origin_cu);
5456 if (attr == NULL)
5457 break;
5458 child_origin_die = follow_die_ref (child_origin_die, attr,
5459 &child_origin_cu);
5460 }
5461
5462 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5463 counterpart may exist. */
5464 if (child_origin_die != child_die)
5465 {
5466 if (child_die->tag != child_origin_die->tag
5467 && !(child_die->tag == DW_TAG_inlined_subroutine
5468 && child_origin_die->tag == DW_TAG_subprogram))
5469 complaint (&symfile_complaints,
5470 _("Child DIE 0x%x and its abstract origin 0x%x have "
5471 "different tags"), child_die->offset,
5472 child_origin_die->offset);
5473 if (child_origin_die->parent != origin_die)
5474 complaint (&symfile_complaints,
5475 _("Child DIE 0x%x and its abstract origin 0x%x have "
5476 "different parents"), child_die->offset,
5477 child_origin_die->offset);
5478 else
5479 *offsets_end++ = child_origin_die->offset;
5480 }
5481 child_die = sibling_die (child_die);
5482 }
5483 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5484 unsigned_int_compar);
5485 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5486 if (offsetp[-1] == *offsetp)
5487 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5488 "to DIE 0x%x as their abstract origin"),
5489 die->offset, *offsetp);
5490
5491 offsetp = offsets;
5492 origin_child_die = origin_die->child;
5493 while (origin_child_die && origin_child_die->tag)
5494 {
5495 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5496 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5497 offsetp++;
5498 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5499 {
5500 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5501 process_die (origin_child_die, origin_cu);
5502 }
5503 origin_child_die = sibling_die (origin_child_die);
5504 }
5505 origin_cu->list_in_scope = origin_previous_list_in_scope;
5506
5507 do_cleanups (cleanups);
5508 }
5509
5510 static void
5511 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5512 {
5513 struct objfile *objfile = cu->objfile;
5514 struct context_stack *new;
5515 CORE_ADDR lowpc;
5516 CORE_ADDR highpc;
5517 struct die_info *child_die;
5518 struct attribute *attr, *call_line, *call_file;
5519 char *name;
5520 CORE_ADDR baseaddr;
5521 struct block *block;
5522 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5523 VEC (symbolp) *template_args = NULL;
5524 struct template_symbol *templ_func = NULL;
5525
5526 if (inlined_func)
5527 {
5528 /* If we do not have call site information, we can't show the
5529 caller of this inlined function. That's too confusing, so
5530 only use the scope for local variables. */
5531 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5532 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5533 if (call_line == NULL || call_file == NULL)
5534 {
5535 read_lexical_block_scope (die, cu);
5536 return;
5537 }
5538 }
5539
5540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5541
5542 name = dwarf2_name (die, cu);
5543
5544 /* Ignore functions with missing or empty names. These are actually
5545 illegal according to the DWARF standard. */
5546 if (name == NULL)
5547 {
5548 complaint (&symfile_complaints,
5549 _("missing name for subprogram DIE at %d"), die->offset);
5550 return;
5551 }
5552
5553 /* Ignore functions with missing or invalid low and high pc attributes. */
5554 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5555 {
5556 attr = dwarf2_attr (die, DW_AT_external, cu);
5557 if (!attr || !DW_UNSND (attr))
5558 complaint (&symfile_complaints,
5559 _("cannot get low and high bounds for subprogram DIE at %d"),
5560 die->offset);
5561 return;
5562 }
5563
5564 lowpc += baseaddr;
5565 highpc += baseaddr;
5566
5567 /* Record the function range for dwarf_decode_lines. */
5568 add_to_cu_func_list (name, lowpc, highpc, cu);
5569
5570 /* If we have any template arguments, then we must allocate a
5571 different sort of symbol. */
5572 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5573 {
5574 if (child_die->tag == DW_TAG_template_type_param
5575 || child_die->tag == DW_TAG_template_value_param)
5576 {
5577 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5578 struct template_symbol);
5579 templ_func->base.is_cplus_template_function = 1;
5580 break;
5581 }
5582 }
5583
5584 new = push_context (0, lowpc);
5585 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5586 (struct symbol *) templ_func);
5587
5588 /* If there is a location expression for DW_AT_frame_base, record
5589 it. */
5590 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5591 if (attr)
5592 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5593 expression is being recorded directly in the function's symbol
5594 and not in a separate frame-base object. I guess this hack is
5595 to avoid adding some sort of frame-base adjunct/annex to the
5596 function's symbol :-(. The problem with doing this is that it
5597 results in a function symbol with a location expression that
5598 has nothing to do with the location of the function, ouch! The
5599 relationship should be: a function's symbol has-a frame base; a
5600 frame-base has-a location expression. */
5601 dwarf2_symbol_mark_computed (attr, new->name, cu);
5602
5603 cu->list_in_scope = &local_symbols;
5604
5605 if (die->child != NULL)
5606 {
5607 child_die = die->child;
5608 while (child_die && child_die->tag)
5609 {
5610 if (child_die->tag == DW_TAG_template_type_param
5611 || child_die->tag == DW_TAG_template_value_param)
5612 {
5613 struct symbol *arg = new_symbol (child_die, NULL, cu);
5614
5615 if (arg != NULL)
5616 VEC_safe_push (symbolp, template_args, arg);
5617 }
5618 else
5619 process_die (child_die, cu);
5620 child_die = sibling_die (child_die);
5621 }
5622 }
5623
5624 inherit_abstract_dies (die, cu);
5625
5626 /* If we have a DW_AT_specification, we might need to import using
5627 directives from the context of the specification DIE. See the
5628 comment in determine_prefix. */
5629 if (cu->language == language_cplus
5630 && dwarf2_attr (die, DW_AT_specification, cu))
5631 {
5632 struct dwarf2_cu *spec_cu = cu;
5633 struct die_info *spec_die = die_specification (die, &spec_cu);
5634
5635 while (spec_die)
5636 {
5637 child_die = spec_die->child;
5638 while (child_die && child_die->tag)
5639 {
5640 if (child_die->tag == DW_TAG_imported_module)
5641 process_die (child_die, spec_cu);
5642 child_die = sibling_die (child_die);
5643 }
5644
5645 /* In some cases, GCC generates specification DIEs that
5646 themselves contain DW_AT_specification attributes. */
5647 spec_die = die_specification (spec_die, &spec_cu);
5648 }
5649 }
5650
5651 new = pop_context ();
5652 /* Make a block for the local symbols within. */
5653 block = finish_block (new->name, &local_symbols, new->old_blocks,
5654 lowpc, highpc, objfile);
5655
5656 /* For C++, set the block's scope. */
5657 if (cu->language == language_cplus || cu->language == language_fortran)
5658 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5659 determine_prefix (die, cu),
5660 processing_has_namespace_info);
5661
5662 /* If we have address ranges, record them. */
5663 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5664
5665 /* Attach template arguments to function. */
5666 if (! VEC_empty (symbolp, template_args))
5667 {
5668 gdb_assert (templ_func != NULL);
5669
5670 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5671 templ_func->template_arguments
5672 = obstack_alloc (&objfile->objfile_obstack,
5673 (templ_func->n_template_arguments
5674 * sizeof (struct symbol *)));
5675 memcpy (templ_func->template_arguments,
5676 VEC_address (symbolp, template_args),
5677 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5678 VEC_free (symbolp, template_args);
5679 }
5680
5681 /* In C++, we can have functions nested inside functions (e.g., when
5682 a function declares a class that has methods). This means that
5683 when we finish processing a function scope, we may need to go
5684 back to building a containing block's symbol lists. */
5685 local_symbols = new->locals;
5686 param_symbols = new->params;
5687 using_directives = new->using_directives;
5688
5689 /* If we've finished processing a top-level function, subsequent
5690 symbols go in the file symbol list. */
5691 if (outermost_context_p ())
5692 cu->list_in_scope = &file_symbols;
5693 }
5694
5695 /* Process all the DIES contained within a lexical block scope. Start
5696 a new scope, process the dies, and then close the scope. */
5697
5698 static void
5699 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5700 {
5701 struct objfile *objfile = cu->objfile;
5702 struct context_stack *new;
5703 CORE_ADDR lowpc, highpc;
5704 struct die_info *child_die;
5705 CORE_ADDR baseaddr;
5706
5707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5708
5709 /* Ignore blocks with missing or invalid low and high pc attributes. */
5710 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5711 as multiple lexical blocks? Handling children in a sane way would
5712 be nasty. Might be easier to properly extend generic blocks to
5713 describe ranges. */
5714 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5715 return;
5716 lowpc += baseaddr;
5717 highpc += baseaddr;
5718
5719 push_context (0, lowpc);
5720 if (die->child != NULL)
5721 {
5722 child_die = die->child;
5723 while (child_die && child_die->tag)
5724 {
5725 process_die (child_die, cu);
5726 child_die = sibling_die (child_die);
5727 }
5728 }
5729 new = pop_context ();
5730
5731 if (local_symbols != NULL || using_directives != NULL)
5732 {
5733 struct block *block
5734 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5735 highpc, objfile);
5736
5737 /* Note that recording ranges after traversing children, as we
5738 do here, means that recording a parent's ranges entails
5739 walking across all its children's ranges as they appear in
5740 the address map, which is quadratic behavior.
5741
5742 It would be nicer to record the parent's ranges before
5743 traversing its children, simply overriding whatever you find
5744 there. But since we don't even decide whether to create a
5745 block until after we've traversed its children, that's hard
5746 to do. */
5747 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5748 }
5749 local_symbols = new->locals;
5750 using_directives = new->using_directives;
5751 }
5752
5753 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5754 Return 1 if the attributes are present and valid, otherwise, return 0.
5755 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5756
5757 static int
5758 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5759 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5760 struct partial_symtab *ranges_pst)
5761 {
5762 struct objfile *objfile = cu->objfile;
5763 struct comp_unit_head *cu_header = &cu->header;
5764 bfd *obfd = objfile->obfd;
5765 unsigned int addr_size = cu_header->addr_size;
5766 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5767 /* Base address selection entry. */
5768 CORE_ADDR base;
5769 int found_base;
5770 unsigned int dummy;
5771 gdb_byte *buffer;
5772 CORE_ADDR marker;
5773 int low_set;
5774 CORE_ADDR low = 0;
5775 CORE_ADDR high = 0;
5776 CORE_ADDR baseaddr;
5777
5778 found_base = cu->base_known;
5779 base = cu->base_address;
5780
5781 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5782 if (offset >= dwarf2_per_objfile->ranges.size)
5783 {
5784 complaint (&symfile_complaints,
5785 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5786 offset);
5787 return 0;
5788 }
5789 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5790
5791 /* Read in the largest possible address. */
5792 marker = read_address (obfd, buffer, cu, &dummy);
5793 if ((marker & mask) == mask)
5794 {
5795 /* If we found the largest possible address, then
5796 read the base address. */
5797 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5798 buffer += 2 * addr_size;
5799 offset += 2 * addr_size;
5800 found_base = 1;
5801 }
5802
5803 low_set = 0;
5804
5805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5806
5807 while (1)
5808 {
5809 CORE_ADDR range_beginning, range_end;
5810
5811 range_beginning = read_address (obfd, buffer, cu, &dummy);
5812 buffer += addr_size;
5813 range_end = read_address (obfd, buffer, cu, &dummy);
5814 buffer += addr_size;
5815 offset += 2 * addr_size;
5816
5817 /* An end of list marker is a pair of zero addresses. */
5818 if (range_beginning == 0 && range_end == 0)
5819 /* Found the end of list entry. */
5820 break;
5821
5822 /* Each base address selection entry is a pair of 2 values.
5823 The first is the largest possible address, the second is
5824 the base address. Check for a base address here. */
5825 if ((range_beginning & mask) == mask)
5826 {
5827 /* If we found the largest possible address, then
5828 read the base address. */
5829 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5830 found_base = 1;
5831 continue;
5832 }
5833
5834 if (!found_base)
5835 {
5836 /* We have no valid base address for the ranges
5837 data. */
5838 complaint (&symfile_complaints,
5839 _("Invalid .debug_ranges data (no base address)"));
5840 return 0;
5841 }
5842
5843 range_beginning += base;
5844 range_end += base;
5845
5846 if (ranges_pst != NULL && range_beginning < range_end)
5847 addrmap_set_empty (objfile->psymtabs_addrmap,
5848 range_beginning + baseaddr, range_end - 1 + baseaddr,
5849 ranges_pst);
5850
5851 /* FIXME: This is recording everything as a low-high
5852 segment of consecutive addresses. We should have a
5853 data structure for discontiguous block ranges
5854 instead. */
5855 if (! low_set)
5856 {
5857 low = range_beginning;
5858 high = range_end;
5859 low_set = 1;
5860 }
5861 else
5862 {
5863 if (range_beginning < low)
5864 low = range_beginning;
5865 if (range_end > high)
5866 high = range_end;
5867 }
5868 }
5869
5870 if (! low_set)
5871 /* If the first entry is an end-of-list marker, the range
5872 describes an empty scope, i.e. no instructions. */
5873 return 0;
5874
5875 if (low_return)
5876 *low_return = low;
5877 if (high_return)
5878 *high_return = high;
5879 return 1;
5880 }
5881
5882 /* Get low and high pc attributes from a die. Return 1 if the attributes
5883 are present and valid, otherwise, return 0. Return -1 if the range is
5884 discontinuous, i.e. derived from DW_AT_ranges information. */
5885 static int
5886 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5887 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5888 struct partial_symtab *pst)
5889 {
5890 struct attribute *attr;
5891 CORE_ADDR low = 0;
5892 CORE_ADDR high = 0;
5893 int ret = 0;
5894
5895 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5896 if (attr)
5897 {
5898 high = DW_ADDR (attr);
5899 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5900 if (attr)
5901 low = DW_ADDR (attr);
5902 else
5903 /* Found high w/o low attribute. */
5904 return 0;
5905
5906 /* Found consecutive range of addresses. */
5907 ret = 1;
5908 }
5909 else
5910 {
5911 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5912 if (attr != NULL)
5913 {
5914 /* Value of the DW_AT_ranges attribute is the offset in the
5915 .debug_ranges section. */
5916 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5917 return 0;
5918 /* Found discontinuous range of addresses. */
5919 ret = -1;
5920 }
5921 }
5922
5923 if (high < low)
5924 return 0;
5925
5926 /* When using the GNU linker, .gnu.linkonce. sections are used to
5927 eliminate duplicate copies of functions and vtables and such.
5928 The linker will arbitrarily choose one and discard the others.
5929 The AT_*_pc values for such functions refer to local labels in
5930 these sections. If the section from that file was discarded, the
5931 labels are not in the output, so the relocs get a value of 0.
5932 If this is a discarded function, mark the pc bounds as invalid,
5933 so that GDB will ignore it. */
5934 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5935 return 0;
5936
5937 *lowpc = low;
5938 *highpc = high;
5939 return ret;
5940 }
5941
5942 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5943 its low and high PC addresses. Do nothing if these addresses could not
5944 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5945 and HIGHPC to the high address if greater than HIGHPC. */
5946
5947 static void
5948 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5949 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5950 struct dwarf2_cu *cu)
5951 {
5952 CORE_ADDR low, high;
5953 struct die_info *child = die->child;
5954
5955 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5956 {
5957 *lowpc = min (*lowpc, low);
5958 *highpc = max (*highpc, high);
5959 }
5960
5961 /* If the language does not allow nested subprograms (either inside
5962 subprograms or lexical blocks), we're done. */
5963 if (cu->language != language_ada)
5964 return;
5965
5966 /* Check all the children of the given DIE. If it contains nested
5967 subprograms, then check their pc bounds. Likewise, we need to
5968 check lexical blocks as well, as they may also contain subprogram
5969 definitions. */
5970 while (child && child->tag)
5971 {
5972 if (child->tag == DW_TAG_subprogram
5973 || child->tag == DW_TAG_lexical_block)
5974 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5975 child = sibling_die (child);
5976 }
5977 }
5978
5979 /* Get the low and high pc's represented by the scope DIE, and store
5980 them in *LOWPC and *HIGHPC. If the correct values can't be
5981 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5982
5983 static void
5984 get_scope_pc_bounds (struct die_info *die,
5985 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5986 struct dwarf2_cu *cu)
5987 {
5988 CORE_ADDR best_low = (CORE_ADDR) -1;
5989 CORE_ADDR best_high = (CORE_ADDR) 0;
5990 CORE_ADDR current_low, current_high;
5991
5992 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5993 {
5994 best_low = current_low;
5995 best_high = current_high;
5996 }
5997 else
5998 {
5999 struct die_info *child = die->child;
6000
6001 while (child && child->tag)
6002 {
6003 switch (child->tag) {
6004 case DW_TAG_subprogram:
6005 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6006 break;
6007 case DW_TAG_namespace:
6008 case DW_TAG_module:
6009 /* FIXME: carlton/2004-01-16: Should we do this for
6010 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6011 that current GCC's always emit the DIEs corresponding
6012 to definitions of methods of classes as children of a
6013 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6014 the DIEs giving the declarations, which could be
6015 anywhere). But I don't see any reason why the
6016 standards says that they have to be there. */
6017 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6018
6019 if (current_low != ((CORE_ADDR) -1))
6020 {
6021 best_low = min (best_low, current_low);
6022 best_high = max (best_high, current_high);
6023 }
6024 break;
6025 default:
6026 /* Ignore. */
6027 break;
6028 }
6029
6030 child = sibling_die (child);
6031 }
6032 }
6033
6034 *lowpc = best_low;
6035 *highpc = best_high;
6036 }
6037
6038 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6039 in DIE. */
6040 static void
6041 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6042 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6043 {
6044 struct attribute *attr;
6045
6046 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6047 if (attr)
6048 {
6049 CORE_ADDR high = DW_ADDR (attr);
6050
6051 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6052 if (attr)
6053 {
6054 CORE_ADDR low = DW_ADDR (attr);
6055
6056 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6057 }
6058 }
6059
6060 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6061 if (attr)
6062 {
6063 bfd *obfd = cu->objfile->obfd;
6064
6065 /* The value of the DW_AT_ranges attribute is the offset of the
6066 address range list in the .debug_ranges section. */
6067 unsigned long offset = DW_UNSND (attr);
6068 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6069
6070 /* For some target architectures, but not others, the
6071 read_address function sign-extends the addresses it returns.
6072 To recognize base address selection entries, we need a
6073 mask. */
6074 unsigned int addr_size = cu->header.addr_size;
6075 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6076
6077 /* The base address, to which the next pair is relative. Note
6078 that this 'base' is a DWARF concept: most entries in a range
6079 list are relative, to reduce the number of relocs against the
6080 debugging information. This is separate from this function's
6081 'baseaddr' argument, which GDB uses to relocate debugging
6082 information from a shared library based on the address at
6083 which the library was loaded. */
6084 CORE_ADDR base = cu->base_address;
6085 int base_known = cu->base_known;
6086
6087 gdb_assert (dwarf2_per_objfile->ranges.readin);
6088 if (offset >= dwarf2_per_objfile->ranges.size)
6089 {
6090 complaint (&symfile_complaints,
6091 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6092 offset);
6093 return;
6094 }
6095
6096 for (;;)
6097 {
6098 unsigned int bytes_read;
6099 CORE_ADDR start, end;
6100
6101 start = read_address (obfd, buffer, cu, &bytes_read);
6102 buffer += bytes_read;
6103 end = read_address (obfd, buffer, cu, &bytes_read);
6104 buffer += bytes_read;
6105
6106 /* Did we find the end of the range list? */
6107 if (start == 0 && end == 0)
6108 break;
6109
6110 /* Did we find a base address selection entry? */
6111 else if ((start & base_select_mask) == base_select_mask)
6112 {
6113 base = end;
6114 base_known = 1;
6115 }
6116
6117 /* We found an ordinary address range. */
6118 else
6119 {
6120 if (!base_known)
6121 {
6122 complaint (&symfile_complaints,
6123 _("Invalid .debug_ranges data (no base address)"));
6124 return;
6125 }
6126
6127 record_block_range (block,
6128 baseaddr + base + start,
6129 baseaddr + base + end - 1);
6130 }
6131 }
6132 }
6133 }
6134
6135 /* Add an aggregate field to the field list. */
6136
6137 static void
6138 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6139 struct dwarf2_cu *cu)
6140 {
6141 struct objfile *objfile = cu->objfile;
6142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6143 struct nextfield *new_field;
6144 struct attribute *attr;
6145 struct field *fp;
6146 char *fieldname = "";
6147
6148 /* Allocate a new field list entry and link it in. */
6149 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6150 make_cleanup (xfree, new_field);
6151 memset (new_field, 0, sizeof (struct nextfield));
6152
6153 if (die->tag == DW_TAG_inheritance)
6154 {
6155 new_field->next = fip->baseclasses;
6156 fip->baseclasses = new_field;
6157 }
6158 else
6159 {
6160 new_field->next = fip->fields;
6161 fip->fields = new_field;
6162 }
6163 fip->nfields++;
6164
6165 /* Handle accessibility and virtuality of field.
6166 The default accessibility for members is public, the default
6167 accessibility for inheritance is private. */
6168 if (die->tag != DW_TAG_inheritance)
6169 new_field->accessibility = DW_ACCESS_public;
6170 else
6171 new_field->accessibility = DW_ACCESS_private;
6172 new_field->virtuality = DW_VIRTUALITY_none;
6173
6174 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6175 if (attr)
6176 new_field->accessibility = DW_UNSND (attr);
6177 if (new_field->accessibility != DW_ACCESS_public)
6178 fip->non_public_fields = 1;
6179 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6180 if (attr)
6181 new_field->virtuality = DW_UNSND (attr);
6182
6183 fp = &new_field->field;
6184
6185 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6186 {
6187 /* Data member other than a C++ static data member. */
6188
6189 /* Get type of field. */
6190 fp->type = die_type (die, cu);
6191
6192 SET_FIELD_BITPOS (*fp, 0);
6193
6194 /* Get bit size of field (zero if none). */
6195 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6196 if (attr)
6197 {
6198 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6199 }
6200 else
6201 {
6202 FIELD_BITSIZE (*fp) = 0;
6203 }
6204
6205 /* Get bit offset of field. */
6206 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6207 if (attr)
6208 {
6209 int byte_offset = 0;
6210
6211 if (attr_form_is_section_offset (attr))
6212 dwarf2_complex_location_expr_complaint ();
6213 else if (attr_form_is_constant (attr))
6214 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6215 else if (attr_form_is_block (attr))
6216 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6217 else
6218 dwarf2_complex_location_expr_complaint ();
6219
6220 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6221 }
6222 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6223 if (attr)
6224 {
6225 if (gdbarch_bits_big_endian (gdbarch))
6226 {
6227 /* For big endian bits, the DW_AT_bit_offset gives the
6228 additional bit offset from the MSB of the containing
6229 anonymous object to the MSB of the field. We don't
6230 have to do anything special since we don't need to
6231 know the size of the anonymous object. */
6232 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6233 }
6234 else
6235 {
6236 /* For little endian bits, compute the bit offset to the
6237 MSB of the anonymous object, subtract off the number of
6238 bits from the MSB of the field to the MSB of the
6239 object, and then subtract off the number of bits of
6240 the field itself. The result is the bit offset of
6241 the LSB of the field. */
6242 int anonymous_size;
6243 int bit_offset = DW_UNSND (attr);
6244
6245 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6246 if (attr)
6247 {
6248 /* The size of the anonymous object containing
6249 the bit field is explicit, so use the
6250 indicated size (in bytes). */
6251 anonymous_size = DW_UNSND (attr);
6252 }
6253 else
6254 {
6255 /* The size of the anonymous object containing
6256 the bit field must be inferred from the type
6257 attribute of the data member containing the
6258 bit field. */
6259 anonymous_size = TYPE_LENGTH (fp->type);
6260 }
6261 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6262 - bit_offset - FIELD_BITSIZE (*fp);
6263 }
6264 }
6265
6266 /* Get name of field. */
6267 fieldname = dwarf2_name (die, cu);
6268 if (fieldname == NULL)
6269 fieldname = "";
6270
6271 /* The name is already allocated along with this objfile, so we don't
6272 need to duplicate it for the type. */
6273 fp->name = fieldname;
6274
6275 /* Change accessibility for artificial fields (e.g. virtual table
6276 pointer or virtual base class pointer) to private. */
6277 if (dwarf2_attr (die, DW_AT_artificial, cu))
6278 {
6279 FIELD_ARTIFICIAL (*fp) = 1;
6280 new_field->accessibility = DW_ACCESS_private;
6281 fip->non_public_fields = 1;
6282 }
6283 }
6284 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6285 {
6286 /* C++ static member. */
6287
6288 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6289 is a declaration, but all versions of G++ as of this writing
6290 (so through at least 3.2.1) incorrectly generate
6291 DW_TAG_variable tags. */
6292
6293 char *physname;
6294
6295 /* Get name of field. */
6296 fieldname = dwarf2_name (die, cu);
6297 if (fieldname == NULL)
6298 return;
6299
6300 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6301 if (attr
6302 /* Only create a symbol if this is an external value.
6303 new_symbol checks this and puts the value in the global symbol
6304 table, which we want. If it is not external, new_symbol
6305 will try to put the value in cu->list_in_scope which is wrong. */
6306 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6307 {
6308 /* A static const member, not much different than an enum as far as
6309 we're concerned, except that we can support more types. */
6310 new_symbol (die, NULL, cu);
6311 }
6312
6313 /* Get physical name. */
6314 physname = (char *) dwarf2_physname (fieldname, die, cu);
6315
6316 /* The name is already allocated along with this objfile, so we don't
6317 need to duplicate it for the type. */
6318 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6319 FIELD_TYPE (*fp) = die_type (die, cu);
6320 FIELD_NAME (*fp) = fieldname;
6321 }
6322 else if (die->tag == DW_TAG_inheritance)
6323 {
6324 /* C++ base class field. */
6325 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6326 if (attr)
6327 {
6328 int byte_offset = 0;
6329
6330 if (attr_form_is_section_offset (attr))
6331 dwarf2_complex_location_expr_complaint ();
6332 else if (attr_form_is_constant (attr))
6333 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6334 else if (attr_form_is_block (attr))
6335 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6336 else
6337 dwarf2_complex_location_expr_complaint ();
6338
6339 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6340 }
6341 FIELD_BITSIZE (*fp) = 0;
6342 FIELD_TYPE (*fp) = die_type (die, cu);
6343 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6344 fip->nbaseclasses++;
6345 }
6346 }
6347
6348 /* Add a typedef defined in the scope of the FIP's class. */
6349
6350 static void
6351 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6352 struct dwarf2_cu *cu)
6353 {
6354 struct objfile *objfile = cu->objfile;
6355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6356 struct typedef_field_list *new_field;
6357 struct attribute *attr;
6358 struct typedef_field *fp;
6359 char *fieldname = "";
6360
6361 /* Allocate a new field list entry and link it in. */
6362 new_field = xzalloc (sizeof (*new_field));
6363 make_cleanup (xfree, new_field);
6364
6365 gdb_assert (die->tag == DW_TAG_typedef);
6366
6367 fp = &new_field->field;
6368
6369 /* Get name of field. */
6370 fp->name = dwarf2_name (die, cu);
6371 if (fp->name == NULL)
6372 return;
6373
6374 fp->type = read_type_die (die, cu);
6375
6376 new_field->next = fip->typedef_field_list;
6377 fip->typedef_field_list = new_field;
6378 fip->typedef_field_list_count++;
6379 }
6380
6381 /* Create the vector of fields, and attach it to the type. */
6382
6383 static void
6384 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6385 struct dwarf2_cu *cu)
6386 {
6387 int nfields = fip->nfields;
6388
6389 /* Record the field count, allocate space for the array of fields,
6390 and create blank accessibility bitfields if necessary. */
6391 TYPE_NFIELDS (type) = nfields;
6392 TYPE_FIELDS (type) = (struct field *)
6393 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6394 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6395
6396 if (fip->non_public_fields && cu->language != language_ada)
6397 {
6398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6399
6400 TYPE_FIELD_PRIVATE_BITS (type) =
6401 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6402 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6403
6404 TYPE_FIELD_PROTECTED_BITS (type) =
6405 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6406 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6407
6408 TYPE_FIELD_IGNORE_BITS (type) =
6409 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6410 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6411 }
6412
6413 /* If the type has baseclasses, allocate and clear a bit vector for
6414 TYPE_FIELD_VIRTUAL_BITS. */
6415 if (fip->nbaseclasses && cu->language != language_ada)
6416 {
6417 int num_bytes = B_BYTES (fip->nbaseclasses);
6418 unsigned char *pointer;
6419
6420 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6421 pointer = TYPE_ALLOC (type, num_bytes);
6422 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6423 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6424 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6425 }
6426
6427 /* Copy the saved-up fields into the field vector. Start from the head
6428 of the list, adding to the tail of the field array, so that they end
6429 up in the same order in the array in which they were added to the list. */
6430 while (nfields-- > 0)
6431 {
6432 struct nextfield *fieldp;
6433
6434 if (fip->fields)
6435 {
6436 fieldp = fip->fields;
6437 fip->fields = fieldp->next;
6438 }
6439 else
6440 {
6441 fieldp = fip->baseclasses;
6442 fip->baseclasses = fieldp->next;
6443 }
6444
6445 TYPE_FIELD (type, nfields) = fieldp->field;
6446 switch (fieldp->accessibility)
6447 {
6448 case DW_ACCESS_private:
6449 if (cu->language != language_ada)
6450 SET_TYPE_FIELD_PRIVATE (type, nfields);
6451 break;
6452
6453 case DW_ACCESS_protected:
6454 if (cu->language != language_ada)
6455 SET_TYPE_FIELD_PROTECTED (type, nfields);
6456 break;
6457
6458 case DW_ACCESS_public:
6459 break;
6460
6461 default:
6462 /* Unknown accessibility. Complain and treat it as public. */
6463 {
6464 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6465 fieldp->accessibility);
6466 }
6467 break;
6468 }
6469 if (nfields < fip->nbaseclasses)
6470 {
6471 switch (fieldp->virtuality)
6472 {
6473 case DW_VIRTUALITY_virtual:
6474 case DW_VIRTUALITY_pure_virtual:
6475 if (cu->language == language_ada)
6476 error ("unexpected virtuality in component of Ada type");
6477 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6478 break;
6479 }
6480 }
6481 }
6482 }
6483
6484 /* Add a member function to the proper fieldlist. */
6485
6486 static void
6487 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6488 struct type *type, struct dwarf2_cu *cu)
6489 {
6490 struct objfile *objfile = cu->objfile;
6491 struct attribute *attr;
6492 struct fnfieldlist *flp;
6493 int i;
6494 struct fn_field *fnp;
6495 char *fieldname;
6496 struct nextfnfield *new_fnfield;
6497 struct type *this_type;
6498
6499 if (cu->language == language_ada)
6500 error ("unexpected member function in Ada type");
6501
6502 /* Get name of member function. */
6503 fieldname = dwarf2_name (die, cu);
6504 if (fieldname == NULL)
6505 return;
6506
6507 /* Look up member function name in fieldlist. */
6508 for (i = 0; i < fip->nfnfields; i++)
6509 {
6510 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6511 break;
6512 }
6513
6514 /* Create new list element if necessary. */
6515 if (i < fip->nfnfields)
6516 flp = &fip->fnfieldlists[i];
6517 else
6518 {
6519 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6520 {
6521 fip->fnfieldlists = (struct fnfieldlist *)
6522 xrealloc (fip->fnfieldlists,
6523 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6524 * sizeof (struct fnfieldlist));
6525 if (fip->nfnfields == 0)
6526 make_cleanup (free_current_contents, &fip->fnfieldlists);
6527 }
6528 flp = &fip->fnfieldlists[fip->nfnfields];
6529 flp->name = fieldname;
6530 flp->length = 0;
6531 flp->head = NULL;
6532 i = fip->nfnfields++;
6533 }
6534
6535 /* Create a new member function field and chain it to the field list
6536 entry. */
6537 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6538 make_cleanup (xfree, new_fnfield);
6539 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6540 new_fnfield->next = flp->head;
6541 flp->head = new_fnfield;
6542 flp->length++;
6543
6544 /* Fill in the member function field info. */
6545 fnp = &new_fnfield->fnfield;
6546
6547 /* Delay processing of the physname until later. */
6548 if (cu->language == language_cplus || cu->language == language_java)
6549 {
6550 add_to_method_list (type, i, flp->length - 1, fieldname,
6551 die, cu);
6552 }
6553 else
6554 {
6555 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6556 fnp->physname = physname ? physname : "";
6557 }
6558
6559 fnp->type = alloc_type (objfile);
6560 this_type = read_type_die (die, cu);
6561 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6562 {
6563 int nparams = TYPE_NFIELDS (this_type);
6564
6565 /* TYPE is the domain of this method, and THIS_TYPE is the type
6566 of the method itself (TYPE_CODE_METHOD). */
6567 smash_to_method_type (fnp->type, type,
6568 TYPE_TARGET_TYPE (this_type),
6569 TYPE_FIELDS (this_type),
6570 TYPE_NFIELDS (this_type),
6571 TYPE_VARARGS (this_type));
6572
6573 /* Handle static member functions.
6574 Dwarf2 has no clean way to discern C++ static and non-static
6575 member functions. G++ helps GDB by marking the first
6576 parameter for non-static member functions (which is the
6577 this pointer) as artificial. We obtain this information
6578 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6579 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6580 fnp->voffset = VOFFSET_STATIC;
6581 }
6582 else
6583 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6584 dwarf2_full_name (fieldname, die, cu));
6585
6586 /* Get fcontext from DW_AT_containing_type if present. */
6587 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6588 fnp->fcontext = die_containing_type (die, cu);
6589
6590 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6591 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6592
6593 /* Get accessibility. */
6594 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6595 if (attr)
6596 {
6597 switch (DW_UNSND (attr))
6598 {
6599 case DW_ACCESS_private:
6600 fnp->is_private = 1;
6601 break;
6602 case DW_ACCESS_protected:
6603 fnp->is_protected = 1;
6604 break;
6605 }
6606 }
6607
6608 /* Check for artificial methods. */
6609 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6610 if (attr && DW_UNSND (attr) != 0)
6611 fnp->is_artificial = 1;
6612
6613 /* Get index in virtual function table if it is a virtual member
6614 function. For older versions of GCC, this is an offset in the
6615 appropriate virtual table, as specified by DW_AT_containing_type.
6616 For everyone else, it is an expression to be evaluated relative
6617 to the object address. */
6618
6619 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6620 if (attr)
6621 {
6622 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6623 {
6624 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6625 {
6626 /* Old-style GCC. */
6627 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6628 }
6629 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6630 || (DW_BLOCK (attr)->size > 1
6631 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6632 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6633 {
6634 struct dwarf_block blk;
6635 int offset;
6636
6637 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6638 ? 1 : 2);
6639 blk.size = DW_BLOCK (attr)->size - offset;
6640 blk.data = DW_BLOCK (attr)->data + offset;
6641 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6642 if ((fnp->voffset % cu->header.addr_size) != 0)
6643 dwarf2_complex_location_expr_complaint ();
6644 else
6645 fnp->voffset /= cu->header.addr_size;
6646 fnp->voffset += 2;
6647 }
6648 else
6649 dwarf2_complex_location_expr_complaint ();
6650
6651 if (!fnp->fcontext)
6652 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6653 }
6654 else if (attr_form_is_section_offset (attr))
6655 {
6656 dwarf2_complex_location_expr_complaint ();
6657 }
6658 else
6659 {
6660 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6661 fieldname);
6662 }
6663 }
6664 else
6665 {
6666 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6667 if (attr && DW_UNSND (attr))
6668 {
6669 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6670 complaint (&symfile_complaints,
6671 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6672 fieldname, die->offset);
6673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6674 TYPE_CPLUS_DYNAMIC (type) = 1;
6675 }
6676 }
6677 }
6678
6679 /* Create the vector of member function fields, and attach it to the type. */
6680
6681 static void
6682 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6683 struct dwarf2_cu *cu)
6684 {
6685 struct fnfieldlist *flp;
6686 int total_length = 0;
6687 int i;
6688
6689 if (cu->language == language_ada)
6690 error ("unexpected member functions in Ada type");
6691
6692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6693 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6694 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6695
6696 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6697 {
6698 struct nextfnfield *nfp = flp->head;
6699 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6700 int k;
6701
6702 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6703 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6704 fn_flp->fn_fields = (struct fn_field *)
6705 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6706 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6707 fn_flp->fn_fields[k] = nfp->fnfield;
6708
6709 total_length += flp->length;
6710 }
6711
6712 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6713 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6714 }
6715
6716 /* Returns non-zero if NAME is the name of a vtable member in CU's
6717 language, zero otherwise. */
6718 static int
6719 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6720 {
6721 static const char vptr[] = "_vptr";
6722 static const char vtable[] = "vtable";
6723
6724 /* Look for the C++ and Java forms of the vtable. */
6725 if ((cu->language == language_java
6726 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6727 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6728 && is_cplus_marker (name[sizeof (vptr) - 1])))
6729 return 1;
6730
6731 return 0;
6732 }
6733
6734 /* GCC outputs unnamed structures that are really pointers to member
6735 functions, with the ABI-specified layout. If TYPE describes
6736 such a structure, smash it into a member function type.
6737
6738 GCC shouldn't do this; it should just output pointer to member DIEs.
6739 This is GCC PR debug/28767. */
6740
6741 static void
6742 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6743 {
6744 struct type *pfn_type, *domain_type, *new_type;
6745
6746 /* Check for a structure with no name and two children. */
6747 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6748 return;
6749
6750 /* Check for __pfn and __delta members. */
6751 if (TYPE_FIELD_NAME (type, 0) == NULL
6752 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6753 || TYPE_FIELD_NAME (type, 1) == NULL
6754 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6755 return;
6756
6757 /* Find the type of the method. */
6758 pfn_type = TYPE_FIELD_TYPE (type, 0);
6759 if (pfn_type == NULL
6760 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6761 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6762 return;
6763
6764 /* Look for the "this" argument. */
6765 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6766 if (TYPE_NFIELDS (pfn_type) == 0
6767 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6768 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6769 return;
6770
6771 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6772 new_type = alloc_type (objfile);
6773 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6774 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6775 TYPE_VARARGS (pfn_type));
6776 smash_to_methodptr_type (type, new_type);
6777 }
6778
6779 /* Called when we find the DIE that starts a structure or union scope
6780 (definition) to create a type for the structure or union. Fill in
6781 the type's name and general properties; the members will not be
6782 processed until process_structure_type.
6783
6784 NOTE: we need to call these functions regardless of whether or not the
6785 DIE has a DW_AT_name attribute, since it might be an anonymous
6786 structure or union. This gets the type entered into our set of
6787 user defined types.
6788
6789 However, if the structure is incomplete (an opaque struct/union)
6790 then suppress creating a symbol table entry for it since gdb only
6791 wants to find the one with the complete definition. Note that if
6792 it is complete, we just call new_symbol, which does it's own
6793 checking about whether the struct/union is anonymous or not (and
6794 suppresses creating a symbol table entry itself). */
6795
6796 static struct type *
6797 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6798 {
6799 struct objfile *objfile = cu->objfile;
6800 struct type *type;
6801 struct attribute *attr;
6802 char *name;
6803
6804 /* If the definition of this type lives in .debug_types, read that type.
6805 Don't follow DW_AT_specification though, that will take us back up
6806 the chain and we want to go down. */
6807 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6808 if (attr)
6809 {
6810 struct dwarf2_cu *type_cu = cu;
6811 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6812
6813 /* We could just recurse on read_structure_type, but we need to call
6814 get_die_type to ensure only one type for this DIE is created.
6815 This is important, for example, because for c++ classes we need
6816 TYPE_NAME set which is only done by new_symbol. Blech. */
6817 type = read_type_die (type_die, type_cu);
6818
6819 /* TYPE_CU may not be the same as CU.
6820 Ensure TYPE is recorded in CU's type_hash table. */
6821 return set_die_type (die, type, cu);
6822 }
6823
6824 type = alloc_type (objfile);
6825 INIT_CPLUS_SPECIFIC (type);
6826
6827 name = dwarf2_name (die, cu);
6828 if (name != NULL)
6829 {
6830 if (cu->language == language_cplus
6831 || cu->language == language_java)
6832 {
6833 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6834
6835 /* dwarf2_full_name might have already finished building the DIE's
6836 type. If so, there is no need to continue. */
6837 if (get_die_type (die, cu) != NULL)
6838 return get_die_type (die, cu);
6839
6840 TYPE_TAG_NAME (type) = full_name;
6841 if (die->tag == DW_TAG_structure_type
6842 || die->tag == DW_TAG_class_type)
6843 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6844 }
6845 else
6846 {
6847 /* The name is already allocated along with this objfile, so
6848 we don't need to duplicate it for the type. */
6849 TYPE_TAG_NAME (type) = (char *) name;
6850 if (die->tag == DW_TAG_class_type)
6851 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6852 }
6853 }
6854
6855 if (die->tag == DW_TAG_structure_type)
6856 {
6857 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6858 }
6859 else if (die->tag == DW_TAG_union_type)
6860 {
6861 TYPE_CODE (type) = TYPE_CODE_UNION;
6862 }
6863 else
6864 {
6865 TYPE_CODE (type) = TYPE_CODE_CLASS;
6866 }
6867
6868 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6869 TYPE_DECLARED_CLASS (type) = 1;
6870
6871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6872 if (attr)
6873 {
6874 TYPE_LENGTH (type) = DW_UNSND (attr);
6875 }
6876 else
6877 {
6878 TYPE_LENGTH (type) = 0;
6879 }
6880
6881 TYPE_STUB_SUPPORTED (type) = 1;
6882 if (die_is_declaration (die, cu))
6883 TYPE_STUB (type) = 1;
6884 else if (attr == NULL && die->child == NULL
6885 && producer_is_realview (cu->producer))
6886 /* RealView does not output the required DW_AT_declaration
6887 on incomplete types. */
6888 TYPE_STUB (type) = 1;
6889
6890 /* We need to add the type field to the die immediately so we don't
6891 infinitely recurse when dealing with pointers to the structure
6892 type within the structure itself. */
6893 set_die_type (die, type, cu);
6894
6895 /* set_die_type should be already done. */
6896 set_descriptive_type (type, die, cu);
6897
6898 return type;
6899 }
6900
6901 /* Finish creating a structure or union type, including filling in
6902 its members and creating a symbol for it. */
6903
6904 static void
6905 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6906 {
6907 struct objfile *objfile = cu->objfile;
6908 struct die_info *child_die = die->child;
6909 struct type *type;
6910
6911 type = get_die_type (die, cu);
6912 if (type == NULL)
6913 type = read_structure_type (die, cu);
6914
6915 if (die->child != NULL && ! die_is_declaration (die, cu))
6916 {
6917 struct field_info fi;
6918 struct die_info *child_die;
6919 VEC (symbolp) *template_args = NULL;
6920 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6921
6922 memset (&fi, 0, sizeof (struct field_info));
6923
6924 child_die = die->child;
6925
6926 while (child_die && child_die->tag)
6927 {
6928 if (child_die->tag == DW_TAG_member
6929 || child_die->tag == DW_TAG_variable)
6930 {
6931 /* NOTE: carlton/2002-11-05: A C++ static data member
6932 should be a DW_TAG_member that is a declaration, but
6933 all versions of G++ as of this writing (so through at
6934 least 3.2.1) incorrectly generate DW_TAG_variable
6935 tags for them instead. */
6936 dwarf2_add_field (&fi, child_die, cu);
6937 }
6938 else if (child_die->tag == DW_TAG_subprogram)
6939 {
6940 /* C++ member function. */
6941 dwarf2_add_member_fn (&fi, child_die, type, cu);
6942 }
6943 else if (child_die->tag == DW_TAG_inheritance)
6944 {
6945 /* C++ base class field. */
6946 dwarf2_add_field (&fi, child_die, cu);
6947 }
6948 else if (child_die->tag == DW_TAG_typedef)
6949 dwarf2_add_typedef (&fi, child_die, cu);
6950 else if (child_die->tag == DW_TAG_template_type_param
6951 || child_die->tag == DW_TAG_template_value_param)
6952 {
6953 struct symbol *arg = new_symbol (child_die, NULL, cu);
6954
6955 if (arg != NULL)
6956 VEC_safe_push (symbolp, template_args, arg);
6957 }
6958
6959 child_die = sibling_die (child_die);
6960 }
6961
6962 /* Attach template arguments to type. */
6963 if (! VEC_empty (symbolp, template_args))
6964 {
6965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6966 TYPE_N_TEMPLATE_ARGUMENTS (type)
6967 = VEC_length (symbolp, template_args);
6968 TYPE_TEMPLATE_ARGUMENTS (type)
6969 = obstack_alloc (&objfile->objfile_obstack,
6970 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6971 * sizeof (struct symbol *)));
6972 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6973 VEC_address (symbolp, template_args),
6974 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6975 * sizeof (struct symbol *)));
6976 VEC_free (symbolp, template_args);
6977 }
6978
6979 /* Attach fields and member functions to the type. */
6980 if (fi.nfields)
6981 dwarf2_attach_fields_to_type (&fi, type, cu);
6982 if (fi.nfnfields)
6983 {
6984 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6985
6986 /* Get the type which refers to the base class (possibly this
6987 class itself) which contains the vtable pointer for the current
6988 class from the DW_AT_containing_type attribute. This use of
6989 DW_AT_containing_type is a GNU extension. */
6990
6991 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6992 {
6993 struct type *t = die_containing_type (die, cu);
6994
6995 TYPE_VPTR_BASETYPE (type) = t;
6996 if (type == t)
6997 {
6998 int i;
6999
7000 /* Our own class provides vtbl ptr. */
7001 for (i = TYPE_NFIELDS (t) - 1;
7002 i >= TYPE_N_BASECLASSES (t);
7003 --i)
7004 {
7005 char *fieldname = TYPE_FIELD_NAME (t, i);
7006
7007 if (is_vtable_name (fieldname, cu))
7008 {
7009 TYPE_VPTR_FIELDNO (type) = i;
7010 break;
7011 }
7012 }
7013
7014 /* Complain if virtual function table field not found. */
7015 if (i < TYPE_N_BASECLASSES (t))
7016 complaint (&symfile_complaints,
7017 _("virtual function table pointer not found when defining class '%s'"),
7018 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7019 "");
7020 }
7021 else
7022 {
7023 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7024 }
7025 }
7026 else if (cu->producer
7027 && strncmp (cu->producer,
7028 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7029 {
7030 /* The IBM XLC compiler does not provide direct indication
7031 of the containing type, but the vtable pointer is
7032 always named __vfp. */
7033
7034 int i;
7035
7036 for (i = TYPE_NFIELDS (type) - 1;
7037 i >= TYPE_N_BASECLASSES (type);
7038 --i)
7039 {
7040 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7041 {
7042 TYPE_VPTR_FIELDNO (type) = i;
7043 TYPE_VPTR_BASETYPE (type) = type;
7044 break;
7045 }
7046 }
7047 }
7048 }
7049
7050 /* Copy fi.typedef_field_list linked list elements content into the
7051 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7052 if (fi.typedef_field_list)
7053 {
7054 int i = fi.typedef_field_list_count;
7055
7056 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7057 TYPE_TYPEDEF_FIELD_ARRAY (type)
7058 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7059 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7060
7061 /* Reverse the list order to keep the debug info elements order. */
7062 while (--i >= 0)
7063 {
7064 struct typedef_field *dest, *src;
7065
7066 dest = &TYPE_TYPEDEF_FIELD (type, i);
7067 src = &fi.typedef_field_list->field;
7068 fi.typedef_field_list = fi.typedef_field_list->next;
7069 *dest = *src;
7070 }
7071 }
7072
7073 do_cleanups (back_to);
7074 }
7075
7076 quirk_gcc_member_function_pointer (type, cu->objfile);
7077
7078 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7079 snapshots) has been known to create a die giving a declaration
7080 for a class that has, as a child, a die giving a definition for a
7081 nested class. So we have to process our children even if the
7082 current die is a declaration. Normally, of course, a declaration
7083 won't have any children at all. */
7084
7085 while (child_die != NULL && child_die->tag)
7086 {
7087 if (child_die->tag == DW_TAG_member
7088 || child_die->tag == DW_TAG_variable
7089 || child_die->tag == DW_TAG_inheritance
7090 || child_die->tag == DW_TAG_template_value_param
7091 || child_die->tag == DW_TAG_template_type_param)
7092 {
7093 /* Do nothing. */
7094 }
7095 else
7096 process_die (child_die, cu);
7097
7098 child_die = sibling_die (child_die);
7099 }
7100
7101 /* Do not consider external references. According to the DWARF standard,
7102 these DIEs are identified by the fact that they have no byte_size
7103 attribute, and a declaration attribute. */
7104 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7105 || !die_is_declaration (die, cu))
7106 new_symbol (die, type, cu);
7107 }
7108
7109 /* Given a DW_AT_enumeration_type die, set its type. We do not
7110 complete the type's fields yet, or create any symbols. */
7111
7112 static struct type *
7113 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7114 {
7115 struct objfile *objfile = cu->objfile;
7116 struct type *type;
7117 struct attribute *attr;
7118 const char *name;
7119
7120 /* If the definition of this type lives in .debug_types, read that type.
7121 Don't follow DW_AT_specification though, that will take us back up
7122 the chain and we want to go down. */
7123 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7124 if (attr)
7125 {
7126 struct dwarf2_cu *type_cu = cu;
7127 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7128
7129 type = read_type_die (type_die, type_cu);
7130
7131 /* TYPE_CU may not be the same as CU.
7132 Ensure TYPE is recorded in CU's type_hash table. */
7133 return set_die_type (die, type, cu);
7134 }
7135
7136 type = alloc_type (objfile);
7137
7138 TYPE_CODE (type) = TYPE_CODE_ENUM;
7139 name = dwarf2_full_name (NULL, die, cu);
7140 if (name != NULL)
7141 TYPE_TAG_NAME (type) = (char *) name;
7142
7143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7144 if (attr)
7145 {
7146 TYPE_LENGTH (type) = DW_UNSND (attr);
7147 }
7148 else
7149 {
7150 TYPE_LENGTH (type) = 0;
7151 }
7152
7153 /* The enumeration DIE can be incomplete. In Ada, any type can be
7154 declared as private in the package spec, and then defined only
7155 inside the package body. Such types are known as Taft Amendment
7156 Types. When another package uses such a type, an incomplete DIE
7157 may be generated by the compiler. */
7158 if (die_is_declaration (die, cu))
7159 TYPE_STUB (type) = 1;
7160
7161 return set_die_type (die, type, cu);
7162 }
7163
7164 /* Given a pointer to a die which begins an enumeration, process all
7165 the dies that define the members of the enumeration, and create the
7166 symbol for the enumeration type.
7167
7168 NOTE: We reverse the order of the element list. */
7169
7170 static void
7171 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7172 {
7173 struct type *this_type;
7174
7175 this_type = get_die_type (die, cu);
7176 if (this_type == NULL)
7177 this_type = read_enumeration_type (die, cu);
7178
7179 if (die->child != NULL)
7180 {
7181 struct die_info *child_die;
7182 struct symbol *sym;
7183 struct field *fields = NULL;
7184 int num_fields = 0;
7185 int unsigned_enum = 1;
7186 char *name;
7187
7188 child_die = die->child;
7189 while (child_die && child_die->tag)
7190 {
7191 if (child_die->tag != DW_TAG_enumerator)
7192 {
7193 process_die (child_die, cu);
7194 }
7195 else
7196 {
7197 name = dwarf2_name (child_die, cu);
7198 if (name)
7199 {
7200 sym = new_symbol (child_die, this_type, cu);
7201 if (SYMBOL_VALUE (sym) < 0)
7202 unsigned_enum = 0;
7203
7204 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7205 {
7206 fields = (struct field *)
7207 xrealloc (fields,
7208 (num_fields + DW_FIELD_ALLOC_CHUNK)
7209 * sizeof (struct field));
7210 }
7211
7212 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7213 FIELD_TYPE (fields[num_fields]) = NULL;
7214 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7215 FIELD_BITSIZE (fields[num_fields]) = 0;
7216
7217 num_fields++;
7218 }
7219 }
7220
7221 child_die = sibling_die (child_die);
7222 }
7223
7224 if (num_fields)
7225 {
7226 TYPE_NFIELDS (this_type) = num_fields;
7227 TYPE_FIELDS (this_type) = (struct field *)
7228 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7229 memcpy (TYPE_FIELDS (this_type), fields,
7230 sizeof (struct field) * num_fields);
7231 xfree (fields);
7232 }
7233 if (unsigned_enum)
7234 TYPE_UNSIGNED (this_type) = 1;
7235 }
7236
7237 new_symbol (die, this_type, cu);
7238 }
7239
7240 /* Extract all information from a DW_TAG_array_type DIE and put it in
7241 the DIE's type field. For now, this only handles one dimensional
7242 arrays. */
7243
7244 static struct type *
7245 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7246 {
7247 struct objfile *objfile = cu->objfile;
7248 struct die_info *child_die;
7249 struct type *type;
7250 struct type *element_type, *range_type, *index_type;
7251 struct type **range_types = NULL;
7252 struct attribute *attr;
7253 int ndim = 0;
7254 struct cleanup *back_to;
7255 char *name;
7256
7257 element_type = die_type (die, cu);
7258
7259 /* The die_type call above may have already set the type for this DIE. */
7260 type = get_die_type (die, cu);
7261 if (type)
7262 return type;
7263
7264 /* Irix 6.2 native cc creates array types without children for
7265 arrays with unspecified length. */
7266 if (die->child == NULL)
7267 {
7268 index_type = objfile_type (objfile)->builtin_int;
7269 range_type = create_range_type (NULL, index_type, 0, -1);
7270 type = create_array_type (NULL, element_type, range_type);
7271 return set_die_type (die, type, cu);
7272 }
7273
7274 back_to = make_cleanup (null_cleanup, NULL);
7275 child_die = die->child;
7276 while (child_die && child_die->tag)
7277 {
7278 if (child_die->tag == DW_TAG_subrange_type)
7279 {
7280 struct type *child_type = read_type_die (child_die, cu);
7281
7282 if (child_type != NULL)
7283 {
7284 /* The range type was succesfully read. Save it for
7285 the array type creation. */
7286 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7287 {
7288 range_types = (struct type **)
7289 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7290 * sizeof (struct type *));
7291 if (ndim == 0)
7292 make_cleanup (free_current_contents, &range_types);
7293 }
7294 range_types[ndim++] = child_type;
7295 }
7296 }
7297 child_die = sibling_die (child_die);
7298 }
7299
7300 /* Dwarf2 dimensions are output from left to right, create the
7301 necessary array types in backwards order. */
7302
7303 type = element_type;
7304
7305 if (read_array_order (die, cu) == DW_ORD_col_major)
7306 {
7307 int i = 0;
7308
7309 while (i < ndim)
7310 type = create_array_type (NULL, type, range_types[i++]);
7311 }
7312 else
7313 {
7314 while (ndim-- > 0)
7315 type = create_array_type (NULL, type, range_types[ndim]);
7316 }
7317
7318 /* Understand Dwarf2 support for vector types (like they occur on
7319 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7320 array type. This is not part of the Dwarf2/3 standard yet, but a
7321 custom vendor extension. The main difference between a regular
7322 array and the vector variant is that vectors are passed by value
7323 to functions. */
7324 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7325 if (attr)
7326 make_vector_type (type);
7327
7328 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7329 implementation may choose to implement triple vectors using this
7330 attribute. */
7331 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7332 if (attr)
7333 {
7334 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7335 TYPE_LENGTH (type) = DW_UNSND (attr);
7336 else
7337 complaint (&symfile_complaints, _("\
7338 DW_AT_byte_size for array type smaller than the total size of elements"));
7339 }
7340
7341 name = dwarf2_name (die, cu);
7342 if (name)
7343 TYPE_NAME (type) = name;
7344
7345 /* Install the type in the die. */
7346 set_die_type (die, type, cu);
7347
7348 /* set_die_type should be already done. */
7349 set_descriptive_type (type, die, cu);
7350
7351 do_cleanups (back_to);
7352
7353 return type;
7354 }
7355
7356 static enum dwarf_array_dim_ordering
7357 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7358 {
7359 struct attribute *attr;
7360
7361 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7362
7363 if (attr) return DW_SND (attr);
7364
7365 /*
7366 GNU F77 is a special case, as at 08/2004 array type info is the
7367 opposite order to the dwarf2 specification, but data is still
7368 laid out as per normal fortran.
7369
7370 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7371 version checking.
7372 */
7373
7374 if (cu->language == language_fortran
7375 && cu->producer && strstr (cu->producer, "GNU F77"))
7376 {
7377 return DW_ORD_row_major;
7378 }
7379
7380 switch (cu->language_defn->la_array_ordering)
7381 {
7382 case array_column_major:
7383 return DW_ORD_col_major;
7384 case array_row_major:
7385 default:
7386 return DW_ORD_row_major;
7387 };
7388 }
7389
7390 /* Extract all information from a DW_TAG_set_type DIE and put it in
7391 the DIE's type field. */
7392
7393 static struct type *
7394 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7395 {
7396 struct type *domain_type, *set_type;
7397 struct attribute *attr;
7398
7399 domain_type = die_type (die, cu);
7400
7401 /* The die_type call above may have already set the type for this DIE. */
7402 set_type = get_die_type (die, cu);
7403 if (set_type)
7404 return set_type;
7405
7406 set_type = create_set_type (NULL, domain_type);
7407
7408 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7409 if (attr)
7410 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7411
7412 return set_die_type (die, set_type, cu);
7413 }
7414
7415 /* First cut: install each common block member as a global variable. */
7416
7417 static void
7418 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7419 {
7420 struct die_info *child_die;
7421 struct attribute *attr;
7422 struct symbol *sym;
7423 CORE_ADDR base = (CORE_ADDR) 0;
7424
7425 attr = dwarf2_attr (die, DW_AT_location, cu);
7426 if (attr)
7427 {
7428 /* Support the .debug_loc offsets */
7429 if (attr_form_is_block (attr))
7430 {
7431 base = decode_locdesc (DW_BLOCK (attr), cu);
7432 }
7433 else if (attr_form_is_section_offset (attr))
7434 {
7435 dwarf2_complex_location_expr_complaint ();
7436 }
7437 else
7438 {
7439 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7440 "common block member");
7441 }
7442 }
7443 if (die->child != NULL)
7444 {
7445 child_die = die->child;
7446 while (child_die && child_die->tag)
7447 {
7448 sym = new_symbol (child_die, NULL, cu);
7449 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7450 if (sym != NULL && attr != NULL)
7451 {
7452 CORE_ADDR byte_offset = 0;
7453
7454 if (attr_form_is_section_offset (attr))
7455 dwarf2_complex_location_expr_complaint ();
7456 else if (attr_form_is_constant (attr))
7457 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7458 else if (attr_form_is_block (attr))
7459 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7460 else
7461 dwarf2_complex_location_expr_complaint ();
7462
7463 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7464 add_symbol_to_list (sym, &global_symbols);
7465 }
7466 child_die = sibling_die (child_die);
7467 }
7468 }
7469 }
7470
7471 /* Create a type for a C++ namespace. */
7472
7473 static struct type *
7474 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7475 {
7476 struct objfile *objfile = cu->objfile;
7477 const char *previous_prefix, *name;
7478 int is_anonymous;
7479 struct type *type;
7480
7481 /* For extensions, reuse the type of the original namespace. */
7482 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7483 {
7484 struct die_info *ext_die;
7485 struct dwarf2_cu *ext_cu = cu;
7486
7487 ext_die = dwarf2_extension (die, &ext_cu);
7488 type = read_type_die (ext_die, ext_cu);
7489
7490 /* EXT_CU may not be the same as CU.
7491 Ensure TYPE is recorded in CU's type_hash table. */
7492 return set_die_type (die, type, cu);
7493 }
7494
7495 name = namespace_name (die, &is_anonymous, cu);
7496
7497 /* Now build the name of the current namespace. */
7498
7499 previous_prefix = determine_prefix (die, cu);
7500 if (previous_prefix[0] != '\0')
7501 name = typename_concat (&objfile->objfile_obstack,
7502 previous_prefix, name, 0, cu);
7503
7504 /* Create the type. */
7505 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7506 objfile);
7507 TYPE_NAME (type) = (char *) name;
7508 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7509
7510 return set_die_type (die, type, cu);
7511 }
7512
7513 /* Read a C++ namespace. */
7514
7515 static void
7516 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7517 {
7518 struct objfile *objfile = cu->objfile;
7519 const char *name;
7520 int is_anonymous;
7521
7522 /* Add a symbol associated to this if we haven't seen the namespace
7523 before. Also, add a using directive if it's an anonymous
7524 namespace. */
7525
7526 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7527 {
7528 struct type *type;
7529
7530 type = read_type_die (die, cu);
7531 new_symbol (die, type, cu);
7532
7533 name = namespace_name (die, &is_anonymous, cu);
7534 if (is_anonymous)
7535 {
7536 const char *previous_prefix = determine_prefix (die, cu);
7537
7538 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7539 NULL, &objfile->objfile_obstack);
7540 }
7541 }
7542
7543 if (die->child != NULL)
7544 {
7545 struct die_info *child_die = die->child;
7546
7547 while (child_die && child_die->tag)
7548 {
7549 process_die (child_die, cu);
7550 child_die = sibling_die (child_die);
7551 }
7552 }
7553 }
7554
7555 /* Read a Fortran module as type. This DIE can be only a declaration used for
7556 imported module. Still we need that type as local Fortran "use ... only"
7557 declaration imports depend on the created type in determine_prefix. */
7558
7559 static struct type *
7560 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7561 {
7562 struct objfile *objfile = cu->objfile;
7563 char *module_name;
7564 struct type *type;
7565
7566 module_name = dwarf2_name (die, cu);
7567 if (!module_name)
7568 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7569 die->offset);
7570 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7571
7572 /* determine_prefix uses TYPE_TAG_NAME. */
7573 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7574
7575 return set_die_type (die, type, cu);
7576 }
7577
7578 /* Read a Fortran module. */
7579
7580 static void
7581 read_module (struct die_info *die, struct dwarf2_cu *cu)
7582 {
7583 struct die_info *child_die = die->child;
7584
7585 while (child_die && child_die->tag)
7586 {
7587 process_die (child_die, cu);
7588 child_die = sibling_die (child_die);
7589 }
7590 }
7591
7592 /* Return the name of the namespace represented by DIE. Set
7593 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7594 namespace. */
7595
7596 static const char *
7597 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7598 {
7599 struct die_info *current_die;
7600 const char *name = NULL;
7601
7602 /* Loop through the extensions until we find a name. */
7603
7604 for (current_die = die;
7605 current_die != NULL;
7606 current_die = dwarf2_extension (die, &cu))
7607 {
7608 name = dwarf2_name (current_die, cu);
7609 if (name != NULL)
7610 break;
7611 }
7612
7613 /* Is it an anonymous namespace? */
7614
7615 *is_anonymous = (name == NULL);
7616 if (*is_anonymous)
7617 name = "(anonymous namespace)";
7618
7619 return name;
7620 }
7621
7622 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7623 the user defined type vector. */
7624
7625 static struct type *
7626 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7627 {
7628 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7629 struct comp_unit_head *cu_header = &cu->header;
7630 struct type *type;
7631 struct attribute *attr_byte_size;
7632 struct attribute *attr_address_class;
7633 int byte_size, addr_class;
7634 struct type *target_type;
7635
7636 target_type = die_type (die, cu);
7637
7638 /* The die_type call above may have already set the type for this DIE. */
7639 type = get_die_type (die, cu);
7640 if (type)
7641 return type;
7642
7643 type = lookup_pointer_type (target_type);
7644
7645 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7646 if (attr_byte_size)
7647 byte_size = DW_UNSND (attr_byte_size);
7648 else
7649 byte_size = cu_header->addr_size;
7650
7651 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7652 if (attr_address_class)
7653 addr_class = DW_UNSND (attr_address_class);
7654 else
7655 addr_class = DW_ADDR_none;
7656
7657 /* If the pointer size or address class is different than the
7658 default, create a type variant marked as such and set the
7659 length accordingly. */
7660 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7661 {
7662 if (gdbarch_address_class_type_flags_p (gdbarch))
7663 {
7664 int type_flags;
7665
7666 type_flags = gdbarch_address_class_type_flags
7667 (gdbarch, byte_size, addr_class);
7668 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7669 == 0);
7670 type = make_type_with_address_space (type, type_flags);
7671 }
7672 else if (TYPE_LENGTH (type) != byte_size)
7673 {
7674 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7675 }
7676 else
7677 {
7678 /* Should we also complain about unhandled address classes? */
7679 }
7680 }
7681
7682 TYPE_LENGTH (type) = byte_size;
7683 return set_die_type (die, type, cu);
7684 }
7685
7686 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7687 the user defined type vector. */
7688
7689 static struct type *
7690 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7691 {
7692 struct type *type;
7693 struct type *to_type;
7694 struct type *domain;
7695
7696 to_type = die_type (die, cu);
7697 domain = die_containing_type (die, cu);
7698
7699 /* The calls above may have already set the type for this DIE. */
7700 type = get_die_type (die, cu);
7701 if (type)
7702 return type;
7703
7704 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7705 type = lookup_methodptr_type (to_type);
7706 else
7707 type = lookup_memberptr_type (to_type, domain);
7708
7709 return set_die_type (die, type, cu);
7710 }
7711
7712 /* Extract all information from a DW_TAG_reference_type DIE and add to
7713 the user defined type vector. */
7714
7715 static struct type *
7716 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7717 {
7718 struct comp_unit_head *cu_header = &cu->header;
7719 struct type *type, *target_type;
7720 struct attribute *attr;
7721
7722 target_type = die_type (die, cu);
7723
7724 /* The die_type call above may have already set the type for this DIE. */
7725 type = get_die_type (die, cu);
7726 if (type)
7727 return type;
7728
7729 type = lookup_reference_type (target_type);
7730 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7731 if (attr)
7732 {
7733 TYPE_LENGTH (type) = DW_UNSND (attr);
7734 }
7735 else
7736 {
7737 TYPE_LENGTH (type) = cu_header->addr_size;
7738 }
7739 return set_die_type (die, type, cu);
7740 }
7741
7742 static struct type *
7743 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7744 {
7745 struct type *base_type, *cv_type;
7746
7747 base_type = die_type (die, cu);
7748
7749 /* The die_type call above may have already set the type for this DIE. */
7750 cv_type = get_die_type (die, cu);
7751 if (cv_type)
7752 return cv_type;
7753
7754 /* In case the const qualifier is applied to an array type, the element type
7755 is so qualified, not the array type (section 6.7.3 of C99). */
7756 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7757 {
7758 struct type *el_type, *inner_array;
7759
7760 base_type = copy_type (base_type);
7761 inner_array = base_type;
7762
7763 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7764 {
7765 TYPE_TARGET_TYPE (inner_array) =
7766 copy_type (TYPE_TARGET_TYPE (inner_array));
7767 inner_array = TYPE_TARGET_TYPE (inner_array);
7768 }
7769
7770 el_type = TYPE_TARGET_TYPE (inner_array);
7771 TYPE_TARGET_TYPE (inner_array) =
7772 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7773
7774 return set_die_type (die, base_type, cu);
7775 }
7776
7777 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7778 return set_die_type (die, cv_type, cu);
7779 }
7780
7781 static struct type *
7782 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7783 {
7784 struct type *base_type, *cv_type;
7785
7786 base_type = die_type (die, cu);
7787
7788 /* The die_type call above may have already set the type for this DIE. */
7789 cv_type = get_die_type (die, cu);
7790 if (cv_type)
7791 return cv_type;
7792
7793 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7794 return set_die_type (die, cv_type, cu);
7795 }
7796
7797 /* Extract all information from a DW_TAG_string_type DIE and add to
7798 the user defined type vector. It isn't really a user defined type,
7799 but it behaves like one, with other DIE's using an AT_user_def_type
7800 attribute to reference it. */
7801
7802 static struct type *
7803 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7804 {
7805 struct objfile *objfile = cu->objfile;
7806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7807 struct type *type, *range_type, *index_type, *char_type;
7808 struct attribute *attr;
7809 unsigned int length;
7810
7811 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7812 if (attr)
7813 {
7814 length = DW_UNSND (attr);
7815 }
7816 else
7817 {
7818 /* check for the DW_AT_byte_size attribute */
7819 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7820 if (attr)
7821 {
7822 length = DW_UNSND (attr);
7823 }
7824 else
7825 {
7826 length = 1;
7827 }
7828 }
7829
7830 index_type = objfile_type (objfile)->builtin_int;
7831 range_type = create_range_type (NULL, index_type, 1, length);
7832 char_type = language_string_char_type (cu->language_defn, gdbarch);
7833 type = create_string_type (NULL, char_type, range_type);
7834
7835 return set_die_type (die, type, cu);
7836 }
7837
7838 /* Handle DIES due to C code like:
7839
7840 struct foo
7841 {
7842 int (*funcp)(int a, long l);
7843 int b;
7844 };
7845
7846 ('funcp' generates a DW_TAG_subroutine_type DIE)
7847 */
7848
7849 static struct type *
7850 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7851 {
7852 struct type *type; /* Type that this function returns */
7853 struct type *ftype; /* Function that returns above type */
7854 struct attribute *attr;
7855
7856 type = die_type (die, cu);
7857
7858 /* The die_type call above may have already set the type for this DIE. */
7859 ftype = get_die_type (die, cu);
7860 if (ftype)
7861 return ftype;
7862
7863 ftype = lookup_function_type (type);
7864
7865 /* All functions in C++, Pascal and Java have prototypes. */
7866 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7867 if ((attr && (DW_UNSND (attr) != 0))
7868 || cu->language == language_cplus
7869 || cu->language == language_java
7870 || cu->language == language_pascal)
7871 TYPE_PROTOTYPED (ftype) = 1;
7872 else if (producer_is_realview (cu->producer))
7873 /* RealView does not emit DW_AT_prototyped. We can not
7874 distinguish prototyped and unprototyped functions; default to
7875 prototyped, since that is more common in modern code (and
7876 RealView warns about unprototyped functions). */
7877 TYPE_PROTOTYPED (ftype) = 1;
7878
7879 /* Store the calling convention in the type if it's available in
7880 the subroutine die. Otherwise set the calling convention to
7881 the default value DW_CC_normal. */
7882 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7883 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7884
7885 /* We need to add the subroutine type to the die immediately so
7886 we don't infinitely recurse when dealing with parameters
7887 declared as the same subroutine type. */
7888 set_die_type (die, ftype, cu);
7889
7890 if (die->child != NULL)
7891 {
7892 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7893 struct die_info *child_die;
7894 int nparams, iparams;
7895
7896 /* Count the number of parameters.
7897 FIXME: GDB currently ignores vararg functions, but knows about
7898 vararg member functions. */
7899 nparams = 0;
7900 child_die = die->child;
7901 while (child_die && child_die->tag)
7902 {
7903 if (child_die->tag == DW_TAG_formal_parameter)
7904 nparams++;
7905 else if (child_die->tag == DW_TAG_unspecified_parameters)
7906 TYPE_VARARGS (ftype) = 1;
7907 child_die = sibling_die (child_die);
7908 }
7909
7910 /* Allocate storage for parameters and fill them in. */
7911 TYPE_NFIELDS (ftype) = nparams;
7912 TYPE_FIELDS (ftype) = (struct field *)
7913 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7914
7915 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7916 even if we error out during the parameters reading below. */
7917 for (iparams = 0; iparams < nparams; iparams++)
7918 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7919
7920 iparams = 0;
7921 child_die = die->child;
7922 while (child_die && child_die->tag)
7923 {
7924 if (child_die->tag == DW_TAG_formal_parameter)
7925 {
7926 struct type *arg_type;
7927
7928 /* DWARF version 2 has no clean way to discern C++
7929 static and non-static member functions. G++ helps
7930 GDB by marking the first parameter for non-static
7931 member functions (which is the this pointer) as
7932 artificial. We pass this information to
7933 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7934
7935 DWARF version 3 added DW_AT_object_pointer, which GCC
7936 4.5 does not yet generate. */
7937 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7938 if (attr)
7939 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7940 else
7941 {
7942 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7943
7944 /* GCC/43521: In java, the formal parameter
7945 "this" is sometimes not marked with DW_AT_artificial. */
7946 if (cu->language == language_java)
7947 {
7948 const char *name = dwarf2_name (child_die, cu);
7949
7950 if (name && !strcmp (name, "this"))
7951 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7952 }
7953 }
7954 arg_type = die_type (child_die, cu);
7955
7956 /* RealView does not mark THIS as const, which the testsuite
7957 expects. GCC marks THIS as const in method definitions,
7958 but not in the class specifications (GCC PR 43053). */
7959 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7960 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7961 {
7962 int is_this = 0;
7963 struct dwarf2_cu *arg_cu = cu;
7964 const char *name = dwarf2_name (child_die, cu);
7965
7966 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7967 if (attr)
7968 {
7969 /* If the compiler emits this, use it. */
7970 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7971 is_this = 1;
7972 }
7973 else if (name && strcmp (name, "this") == 0)
7974 /* Function definitions will have the argument names. */
7975 is_this = 1;
7976 else if (name == NULL && iparams == 0)
7977 /* Declarations may not have the names, so like
7978 elsewhere in GDB, assume an artificial first
7979 argument is "this". */
7980 is_this = 1;
7981
7982 if (is_this)
7983 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7984 arg_type, 0);
7985 }
7986
7987 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7988 iparams++;
7989 }
7990 child_die = sibling_die (child_die);
7991 }
7992 }
7993
7994 return ftype;
7995 }
7996
7997 static struct type *
7998 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7999 {
8000 struct objfile *objfile = cu->objfile;
8001 const char *name = NULL;
8002 struct type *this_type;
8003
8004 name = dwarf2_full_name (NULL, die, cu);
8005 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8006 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8007 TYPE_NAME (this_type) = (char *) name;
8008 set_die_type (die, this_type, cu);
8009 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8010 return this_type;
8011 }
8012
8013 /* Find a representation of a given base type and install
8014 it in the TYPE field of the die. */
8015
8016 static struct type *
8017 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8018 {
8019 struct objfile *objfile = cu->objfile;
8020 struct type *type;
8021 struct attribute *attr;
8022 int encoding = 0, size = 0;
8023 char *name;
8024 enum type_code code = TYPE_CODE_INT;
8025 int type_flags = 0;
8026 struct type *target_type = NULL;
8027
8028 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8029 if (attr)
8030 {
8031 encoding = DW_UNSND (attr);
8032 }
8033 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8034 if (attr)
8035 {
8036 size = DW_UNSND (attr);
8037 }
8038 name = dwarf2_name (die, cu);
8039 if (!name)
8040 {
8041 complaint (&symfile_complaints,
8042 _("DW_AT_name missing from DW_TAG_base_type"));
8043 }
8044
8045 switch (encoding)
8046 {
8047 case DW_ATE_address:
8048 /* Turn DW_ATE_address into a void * pointer. */
8049 code = TYPE_CODE_PTR;
8050 type_flags |= TYPE_FLAG_UNSIGNED;
8051 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8052 break;
8053 case DW_ATE_boolean:
8054 code = TYPE_CODE_BOOL;
8055 type_flags |= TYPE_FLAG_UNSIGNED;
8056 break;
8057 case DW_ATE_complex_float:
8058 code = TYPE_CODE_COMPLEX;
8059 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8060 break;
8061 case DW_ATE_decimal_float:
8062 code = TYPE_CODE_DECFLOAT;
8063 break;
8064 case DW_ATE_float:
8065 code = TYPE_CODE_FLT;
8066 break;
8067 case DW_ATE_signed:
8068 break;
8069 case DW_ATE_unsigned:
8070 type_flags |= TYPE_FLAG_UNSIGNED;
8071 break;
8072 case DW_ATE_signed_char:
8073 if (cu->language == language_ada || cu->language == language_m2
8074 || cu->language == language_pascal)
8075 code = TYPE_CODE_CHAR;
8076 break;
8077 case DW_ATE_unsigned_char:
8078 if (cu->language == language_ada || cu->language == language_m2
8079 || cu->language == language_pascal)
8080 code = TYPE_CODE_CHAR;
8081 type_flags |= TYPE_FLAG_UNSIGNED;
8082 break;
8083 case DW_ATE_UTF:
8084 /* We just treat this as an integer and then recognize the
8085 type by name elsewhere. */
8086 break;
8087
8088 default:
8089 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8090 dwarf_type_encoding_name (encoding));
8091 break;
8092 }
8093
8094 type = init_type (code, size, type_flags, NULL, objfile);
8095 TYPE_NAME (type) = name;
8096 TYPE_TARGET_TYPE (type) = target_type;
8097
8098 if (name && strcmp (name, "char") == 0)
8099 TYPE_NOSIGN (type) = 1;
8100
8101 return set_die_type (die, type, cu);
8102 }
8103
8104 /* Read the given DW_AT_subrange DIE. */
8105
8106 static struct type *
8107 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8108 {
8109 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8110 struct type *base_type;
8111 struct type *range_type;
8112 struct attribute *attr;
8113 LONGEST low = 0;
8114 LONGEST high = -1;
8115 char *name;
8116 LONGEST negative_mask;
8117
8118 base_type = die_type (die, cu);
8119 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8120 check_typedef (base_type);
8121
8122 /* The die_type call above may have already set the type for this DIE. */
8123 range_type = get_die_type (die, cu);
8124 if (range_type)
8125 return range_type;
8126
8127 if (cu->language == language_fortran)
8128 {
8129 /* FORTRAN implies a lower bound of 1, if not given. */
8130 low = 1;
8131 }
8132
8133 /* FIXME: For variable sized arrays either of these could be
8134 a variable rather than a constant value. We'll allow it,
8135 but we don't know how to handle it. */
8136 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8137 if (attr)
8138 low = dwarf2_get_attr_constant_value (attr, 0);
8139
8140 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8141 if (attr)
8142 {
8143 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8144 {
8145 /* GCC encodes arrays with unspecified or dynamic length
8146 with a DW_FORM_block1 attribute or a reference attribute.
8147 FIXME: GDB does not yet know how to handle dynamic
8148 arrays properly, treat them as arrays with unspecified
8149 length for now.
8150
8151 FIXME: jimb/2003-09-22: GDB does not really know
8152 how to handle arrays of unspecified length
8153 either; we just represent them as zero-length
8154 arrays. Choose an appropriate upper bound given
8155 the lower bound we've computed above. */
8156 high = low - 1;
8157 }
8158 else
8159 high = dwarf2_get_attr_constant_value (attr, 1);
8160 }
8161 else
8162 {
8163 attr = dwarf2_attr (die, DW_AT_count, cu);
8164 if (attr)
8165 {
8166 int count = dwarf2_get_attr_constant_value (attr, 1);
8167 high = low + count - 1;
8168 }
8169 }
8170
8171 /* Dwarf-2 specifications explicitly allows to create subrange types
8172 without specifying a base type.
8173 In that case, the base type must be set to the type of
8174 the lower bound, upper bound or count, in that order, if any of these
8175 three attributes references an object that has a type.
8176 If no base type is found, the Dwarf-2 specifications say that
8177 a signed integer type of size equal to the size of an address should
8178 be used.
8179 For the following C code: `extern char gdb_int [];'
8180 GCC produces an empty range DIE.
8181 FIXME: muller/2010-05-28: Possible references to object for low bound,
8182 high bound or count are not yet handled by this code.
8183 */
8184 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8185 {
8186 struct objfile *objfile = cu->objfile;
8187 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8188 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8189 struct type *int_type = objfile_type (objfile)->builtin_int;
8190
8191 /* Test "int", "long int", and "long long int" objfile types,
8192 and select the first one having a size above or equal to the
8193 architecture address size. */
8194 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8195 base_type = int_type;
8196 else
8197 {
8198 int_type = objfile_type (objfile)->builtin_long;
8199 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8200 base_type = int_type;
8201 else
8202 {
8203 int_type = objfile_type (objfile)->builtin_long_long;
8204 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8205 base_type = int_type;
8206 }
8207 }
8208 }
8209
8210 negative_mask =
8211 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8212 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8213 low |= negative_mask;
8214 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8215 high |= negative_mask;
8216
8217 range_type = create_range_type (NULL, base_type, low, high);
8218
8219 /* Mark arrays with dynamic length at least as an array of unspecified
8220 length. GDB could check the boundary but before it gets implemented at
8221 least allow accessing the array elements. */
8222 if (attr && attr->form == DW_FORM_block1)
8223 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8224
8225 name = dwarf2_name (die, cu);
8226 if (name)
8227 TYPE_NAME (range_type) = name;
8228
8229 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8230 if (attr)
8231 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8232
8233 set_die_type (die, range_type, cu);
8234
8235 /* set_die_type should be already done. */
8236 set_descriptive_type (range_type, die, cu);
8237
8238 return range_type;
8239 }
8240
8241 static struct type *
8242 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8243 {
8244 struct type *type;
8245
8246 /* For now, we only support the C meaning of an unspecified type: void. */
8247
8248 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8249 TYPE_NAME (type) = dwarf2_name (die, cu);
8250
8251 return set_die_type (die, type, cu);
8252 }
8253
8254 /* Trivial hash function for die_info: the hash value of a DIE
8255 is its offset in .debug_info for this objfile. */
8256
8257 static hashval_t
8258 die_hash (const void *item)
8259 {
8260 const struct die_info *die = item;
8261
8262 return die->offset;
8263 }
8264
8265 /* Trivial comparison function for die_info structures: two DIEs
8266 are equal if they have the same offset. */
8267
8268 static int
8269 die_eq (const void *item_lhs, const void *item_rhs)
8270 {
8271 const struct die_info *die_lhs = item_lhs;
8272 const struct die_info *die_rhs = item_rhs;
8273
8274 return die_lhs->offset == die_rhs->offset;
8275 }
8276
8277 /* Read a whole compilation unit into a linked list of dies. */
8278
8279 static struct die_info *
8280 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8281 {
8282 struct die_reader_specs reader_specs;
8283 int read_abbrevs = 0;
8284 struct cleanup *back_to = NULL;
8285 struct die_info *die;
8286
8287 if (cu->dwarf2_abbrevs == NULL)
8288 {
8289 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8290 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8291 read_abbrevs = 1;
8292 }
8293
8294 gdb_assert (cu->die_hash == NULL);
8295 cu->die_hash
8296 = htab_create_alloc_ex (cu->header.length / 12,
8297 die_hash,
8298 die_eq,
8299 NULL,
8300 &cu->comp_unit_obstack,
8301 hashtab_obstack_allocate,
8302 dummy_obstack_deallocate);
8303
8304 init_cu_die_reader (&reader_specs, cu);
8305
8306 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8307
8308 if (read_abbrevs)
8309 do_cleanups (back_to);
8310
8311 return die;
8312 }
8313
8314 /* Main entry point for reading a DIE and all children.
8315 Read the DIE and dump it if requested. */
8316
8317 static struct die_info *
8318 read_die_and_children (const struct die_reader_specs *reader,
8319 gdb_byte *info_ptr,
8320 gdb_byte **new_info_ptr,
8321 struct die_info *parent)
8322 {
8323 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8324 new_info_ptr, parent);
8325
8326 if (dwarf2_die_debug)
8327 {
8328 fprintf_unfiltered (gdb_stdlog,
8329 "\nRead die from %s of %s:\n",
8330 reader->buffer == dwarf2_per_objfile->info.buffer
8331 ? ".debug_info"
8332 : reader->buffer == dwarf2_per_objfile->types.buffer
8333 ? ".debug_types"
8334 : "unknown section",
8335 reader->abfd->filename);
8336 dump_die (result, dwarf2_die_debug);
8337 }
8338
8339 return result;
8340 }
8341
8342 /* Read a single die and all its descendents. Set the die's sibling
8343 field to NULL; set other fields in the die correctly, and set all
8344 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8345 location of the info_ptr after reading all of those dies. PARENT
8346 is the parent of the die in question. */
8347
8348 static struct die_info *
8349 read_die_and_children_1 (const struct die_reader_specs *reader,
8350 gdb_byte *info_ptr,
8351 gdb_byte **new_info_ptr,
8352 struct die_info *parent)
8353 {
8354 struct die_info *die;
8355 gdb_byte *cur_ptr;
8356 int has_children;
8357
8358 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8359 if (die == NULL)
8360 {
8361 *new_info_ptr = cur_ptr;
8362 return NULL;
8363 }
8364 store_in_ref_table (die, reader->cu);
8365
8366 if (has_children)
8367 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8368 else
8369 {
8370 die->child = NULL;
8371 *new_info_ptr = cur_ptr;
8372 }
8373
8374 die->sibling = NULL;
8375 die->parent = parent;
8376 return die;
8377 }
8378
8379 /* Read a die, all of its descendents, and all of its siblings; set
8380 all of the fields of all of the dies correctly. Arguments are as
8381 in read_die_and_children. */
8382
8383 static struct die_info *
8384 read_die_and_siblings (const struct die_reader_specs *reader,
8385 gdb_byte *info_ptr,
8386 gdb_byte **new_info_ptr,
8387 struct die_info *parent)
8388 {
8389 struct die_info *first_die, *last_sibling;
8390 gdb_byte *cur_ptr;
8391
8392 cur_ptr = info_ptr;
8393 first_die = last_sibling = NULL;
8394
8395 while (1)
8396 {
8397 struct die_info *die
8398 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8399
8400 if (die == NULL)
8401 {
8402 *new_info_ptr = cur_ptr;
8403 return first_die;
8404 }
8405
8406 if (!first_die)
8407 first_die = die;
8408 else
8409 last_sibling->sibling = die;
8410
8411 last_sibling = die;
8412 }
8413 }
8414
8415 /* Read the die from the .debug_info section buffer. Set DIEP to
8416 point to a newly allocated die with its information, except for its
8417 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8418 whether the die has children or not. */
8419
8420 static gdb_byte *
8421 read_full_die (const struct die_reader_specs *reader,
8422 struct die_info **diep, gdb_byte *info_ptr,
8423 int *has_children)
8424 {
8425 unsigned int abbrev_number, bytes_read, i, offset;
8426 struct abbrev_info *abbrev;
8427 struct die_info *die;
8428 struct dwarf2_cu *cu = reader->cu;
8429 bfd *abfd = reader->abfd;
8430
8431 offset = info_ptr - reader->buffer;
8432 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8433 info_ptr += bytes_read;
8434 if (!abbrev_number)
8435 {
8436 *diep = NULL;
8437 *has_children = 0;
8438 return info_ptr;
8439 }
8440
8441 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8442 if (!abbrev)
8443 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8444 abbrev_number,
8445 bfd_get_filename (abfd));
8446
8447 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8448 die->offset = offset;
8449 die->tag = abbrev->tag;
8450 die->abbrev = abbrev_number;
8451
8452 die->num_attrs = abbrev->num_attrs;
8453
8454 for (i = 0; i < abbrev->num_attrs; ++i)
8455 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8456 abfd, info_ptr, cu);
8457
8458 *diep = die;
8459 *has_children = abbrev->has_children;
8460 return info_ptr;
8461 }
8462
8463 /* In DWARF version 2, the description of the debugging information is
8464 stored in a separate .debug_abbrev section. Before we read any
8465 dies from a section we read in all abbreviations and install them
8466 in a hash table. This function also sets flags in CU describing
8467 the data found in the abbrev table. */
8468
8469 static void
8470 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8471 {
8472 struct comp_unit_head *cu_header = &cu->header;
8473 gdb_byte *abbrev_ptr;
8474 struct abbrev_info *cur_abbrev;
8475 unsigned int abbrev_number, bytes_read, abbrev_name;
8476 unsigned int abbrev_form, hash_number;
8477 struct attr_abbrev *cur_attrs;
8478 unsigned int allocated_attrs;
8479
8480 /* Initialize dwarf2 abbrevs */
8481 obstack_init (&cu->abbrev_obstack);
8482 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8483 (ABBREV_HASH_SIZE
8484 * sizeof (struct abbrev_info *)));
8485 memset (cu->dwarf2_abbrevs, 0,
8486 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8487
8488 dwarf2_read_section (dwarf2_per_objfile->objfile,
8489 &dwarf2_per_objfile->abbrev);
8490 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8491 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8492 abbrev_ptr += bytes_read;
8493
8494 allocated_attrs = ATTR_ALLOC_CHUNK;
8495 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8496
8497 /* loop until we reach an abbrev number of 0 */
8498 while (abbrev_number)
8499 {
8500 cur_abbrev = dwarf_alloc_abbrev (cu);
8501
8502 /* read in abbrev header */
8503 cur_abbrev->number = abbrev_number;
8504 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8505 abbrev_ptr += bytes_read;
8506 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8507 abbrev_ptr += 1;
8508
8509 if (cur_abbrev->tag == DW_TAG_namespace)
8510 cu->has_namespace_info = 1;
8511
8512 /* now read in declarations */
8513 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8514 abbrev_ptr += bytes_read;
8515 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8516 abbrev_ptr += bytes_read;
8517 while (abbrev_name)
8518 {
8519 if (cur_abbrev->num_attrs == allocated_attrs)
8520 {
8521 allocated_attrs += ATTR_ALLOC_CHUNK;
8522 cur_attrs
8523 = xrealloc (cur_attrs, (allocated_attrs
8524 * sizeof (struct attr_abbrev)));
8525 }
8526
8527 /* Record whether this compilation unit might have
8528 inter-compilation-unit references. If we don't know what form
8529 this attribute will have, then it might potentially be a
8530 DW_FORM_ref_addr, so we conservatively expect inter-CU
8531 references. */
8532
8533 if (abbrev_form == DW_FORM_ref_addr
8534 || abbrev_form == DW_FORM_indirect)
8535 cu->has_form_ref_addr = 1;
8536
8537 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8538 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8539 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8540 abbrev_ptr += bytes_read;
8541 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8542 abbrev_ptr += bytes_read;
8543 }
8544
8545 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8546 (cur_abbrev->num_attrs
8547 * sizeof (struct attr_abbrev)));
8548 memcpy (cur_abbrev->attrs, cur_attrs,
8549 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8550
8551 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8552 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8553 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8554
8555 /* Get next abbreviation.
8556 Under Irix6 the abbreviations for a compilation unit are not
8557 always properly terminated with an abbrev number of 0.
8558 Exit loop if we encounter an abbreviation which we have
8559 already read (which means we are about to read the abbreviations
8560 for the next compile unit) or if the end of the abbreviation
8561 table is reached. */
8562 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8563 >= dwarf2_per_objfile->abbrev.size)
8564 break;
8565 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8566 abbrev_ptr += bytes_read;
8567 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8568 break;
8569 }
8570
8571 xfree (cur_attrs);
8572 }
8573
8574 /* Release the memory used by the abbrev table for a compilation unit. */
8575
8576 static void
8577 dwarf2_free_abbrev_table (void *ptr_to_cu)
8578 {
8579 struct dwarf2_cu *cu = ptr_to_cu;
8580
8581 obstack_free (&cu->abbrev_obstack, NULL);
8582 cu->dwarf2_abbrevs = NULL;
8583 }
8584
8585 /* Lookup an abbrev_info structure in the abbrev hash table. */
8586
8587 static struct abbrev_info *
8588 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8589 {
8590 unsigned int hash_number;
8591 struct abbrev_info *abbrev;
8592
8593 hash_number = number % ABBREV_HASH_SIZE;
8594 abbrev = cu->dwarf2_abbrevs[hash_number];
8595
8596 while (abbrev)
8597 {
8598 if (abbrev->number == number)
8599 return abbrev;
8600 else
8601 abbrev = abbrev->next;
8602 }
8603 return NULL;
8604 }
8605
8606 /* Returns nonzero if TAG represents a type that we might generate a partial
8607 symbol for. */
8608
8609 static int
8610 is_type_tag_for_partial (int tag)
8611 {
8612 switch (tag)
8613 {
8614 #if 0
8615 /* Some types that would be reasonable to generate partial symbols for,
8616 that we don't at present. */
8617 case DW_TAG_array_type:
8618 case DW_TAG_file_type:
8619 case DW_TAG_ptr_to_member_type:
8620 case DW_TAG_set_type:
8621 case DW_TAG_string_type:
8622 case DW_TAG_subroutine_type:
8623 #endif
8624 case DW_TAG_base_type:
8625 case DW_TAG_class_type:
8626 case DW_TAG_interface_type:
8627 case DW_TAG_enumeration_type:
8628 case DW_TAG_structure_type:
8629 case DW_TAG_subrange_type:
8630 case DW_TAG_typedef:
8631 case DW_TAG_union_type:
8632 return 1;
8633 default:
8634 return 0;
8635 }
8636 }
8637
8638 /* Load all DIEs that are interesting for partial symbols into memory. */
8639
8640 static struct partial_die_info *
8641 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8642 int building_psymtab, struct dwarf2_cu *cu)
8643 {
8644 struct partial_die_info *part_die;
8645 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8646 struct abbrev_info *abbrev;
8647 unsigned int bytes_read;
8648 unsigned int load_all = 0;
8649
8650 int nesting_level = 1;
8651
8652 parent_die = NULL;
8653 last_die = NULL;
8654
8655 if (cu->per_cu && cu->per_cu->load_all_dies)
8656 load_all = 1;
8657
8658 cu->partial_dies
8659 = htab_create_alloc_ex (cu->header.length / 12,
8660 partial_die_hash,
8661 partial_die_eq,
8662 NULL,
8663 &cu->comp_unit_obstack,
8664 hashtab_obstack_allocate,
8665 dummy_obstack_deallocate);
8666
8667 part_die = obstack_alloc (&cu->comp_unit_obstack,
8668 sizeof (struct partial_die_info));
8669
8670 while (1)
8671 {
8672 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8673
8674 /* A NULL abbrev means the end of a series of children. */
8675 if (abbrev == NULL)
8676 {
8677 if (--nesting_level == 0)
8678 {
8679 /* PART_DIE was probably the last thing allocated on the
8680 comp_unit_obstack, so we could call obstack_free
8681 here. We don't do that because the waste is small,
8682 and will be cleaned up when we're done with this
8683 compilation unit. This way, we're also more robust
8684 against other users of the comp_unit_obstack. */
8685 return first_die;
8686 }
8687 info_ptr += bytes_read;
8688 last_die = parent_die;
8689 parent_die = parent_die->die_parent;
8690 continue;
8691 }
8692
8693 /* Check for template arguments. We never save these; if
8694 they're seen, we just mark the parent, and go on our way. */
8695 if (parent_die != NULL
8696 && cu->language == language_cplus
8697 && (abbrev->tag == DW_TAG_template_type_param
8698 || abbrev->tag == DW_TAG_template_value_param))
8699 {
8700 parent_die->has_template_arguments = 1;
8701
8702 if (!load_all)
8703 {
8704 /* We don't need a partial DIE for the template argument. */
8705 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8706 cu);
8707 continue;
8708 }
8709 }
8710
8711 /* We only recurse into subprograms looking for template arguments.
8712 Skip their other children. */
8713 if (!load_all
8714 && cu->language == language_cplus
8715 && parent_die != NULL
8716 && parent_die->tag == DW_TAG_subprogram)
8717 {
8718 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8719 continue;
8720 }
8721
8722 /* Check whether this DIE is interesting enough to save. Normally
8723 we would not be interested in members here, but there may be
8724 later variables referencing them via DW_AT_specification (for
8725 static members). */
8726 if (!load_all
8727 && !is_type_tag_for_partial (abbrev->tag)
8728 && abbrev->tag != DW_TAG_constant
8729 && abbrev->tag != DW_TAG_enumerator
8730 && abbrev->tag != DW_TAG_subprogram
8731 && abbrev->tag != DW_TAG_lexical_block
8732 && abbrev->tag != DW_TAG_variable
8733 && abbrev->tag != DW_TAG_namespace
8734 && abbrev->tag != DW_TAG_module
8735 && abbrev->tag != DW_TAG_member)
8736 {
8737 /* Otherwise we skip to the next sibling, if any. */
8738 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8739 continue;
8740 }
8741
8742 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8743 buffer, info_ptr, cu);
8744
8745 /* This two-pass algorithm for processing partial symbols has a
8746 high cost in cache pressure. Thus, handle some simple cases
8747 here which cover the majority of C partial symbols. DIEs
8748 which neither have specification tags in them, nor could have
8749 specification tags elsewhere pointing at them, can simply be
8750 processed and discarded.
8751
8752 This segment is also optional; scan_partial_symbols and
8753 add_partial_symbol will handle these DIEs if we chain
8754 them in normally. When compilers which do not emit large
8755 quantities of duplicate debug information are more common,
8756 this code can probably be removed. */
8757
8758 /* Any complete simple types at the top level (pretty much all
8759 of them, for a language without namespaces), can be processed
8760 directly. */
8761 if (parent_die == NULL
8762 && part_die->has_specification == 0
8763 && part_die->is_declaration == 0
8764 && (part_die->tag == DW_TAG_typedef
8765 || part_die->tag == DW_TAG_base_type
8766 || part_die->tag == DW_TAG_subrange_type))
8767 {
8768 if (building_psymtab && part_die->name != NULL)
8769 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8770 VAR_DOMAIN, LOC_TYPEDEF,
8771 &cu->objfile->static_psymbols,
8772 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8773 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8774 continue;
8775 }
8776
8777 /* If we're at the second level, and we're an enumerator, and
8778 our parent has no specification (meaning possibly lives in a
8779 namespace elsewhere), then we can add the partial symbol now
8780 instead of queueing it. */
8781 if (part_die->tag == DW_TAG_enumerator
8782 && parent_die != NULL
8783 && parent_die->die_parent == NULL
8784 && parent_die->tag == DW_TAG_enumeration_type
8785 && parent_die->has_specification == 0)
8786 {
8787 if (part_die->name == NULL)
8788 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8789 else if (building_psymtab)
8790 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8791 VAR_DOMAIN, LOC_CONST,
8792 (cu->language == language_cplus
8793 || cu->language == language_java)
8794 ? &cu->objfile->global_psymbols
8795 : &cu->objfile->static_psymbols,
8796 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8797
8798 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8799 continue;
8800 }
8801
8802 /* We'll save this DIE so link it in. */
8803 part_die->die_parent = parent_die;
8804 part_die->die_sibling = NULL;
8805 part_die->die_child = NULL;
8806
8807 if (last_die && last_die == parent_die)
8808 last_die->die_child = part_die;
8809 else if (last_die)
8810 last_die->die_sibling = part_die;
8811
8812 last_die = part_die;
8813
8814 if (first_die == NULL)
8815 first_die = part_die;
8816
8817 /* Maybe add the DIE to the hash table. Not all DIEs that we
8818 find interesting need to be in the hash table, because we
8819 also have the parent/sibling/child chains; only those that we
8820 might refer to by offset later during partial symbol reading.
8821
8822 For now this means things that might have be the target of a
8823 DW_AT_specification, DW_AT_abstract_origin, or
8824 DW_AT_extension. DW_AT_extension will refer only to
8825 namespaces; DW_AT_abstract_origin refers to functions (and
8826 many things under the function DIE, but we do not recurse
8827 into function DIEs during partial symbol reading) and
8828 possibly variables as well; DW_AT_specification refers to
8829 declarations. Declarations ought to have the DW_AT_declaration
8830 flag. It happens that GCC forgets to put it in sometimes, but
8831 only for functions, not for types.
8832
8833 Adding more things than necessary to the hash table is harmless
8834 except for the performance cost. Adding too few will result in
8835 wasted time in find_partial_die, when we reread the compilation
8836 unit with load_all_dies set. */
8837
8838 if (load_all
8839 || abbrev->tag == DW_TAG_constant
8840 || abbrev->tag == DW_TAG_subprogram
8841 || abbrev->tag == DW_TAG_variable
8842 || abbrev->tag == DW_TAG_namespace
8843 || part_die->is_declaration)
8844 {
8845 void **slot;
8846
8847 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8848 part_die->offset, INSERT);
8849 *slot = part_die;
8850 }
8851
8852 part_die = obstack_alloc (&cu->comp_unit_obstack,
8853 sizeof (struct partial_die_info));
8854
8855 /* For some DIEs we want to follow their children (if any). For C
8856 we have no reason to follow the children of structures; for other
8857 languages we have to, so that we can get at method physnames
8858 to infer fully qualified class names, for DW_AT_specification,
8859 and for C++ template arguments. For C++, we also look one level
8860 inside functions to find template arguments (if the name of the
8861 function does not already contain the template arguments).
8862
8863 For Ada, we need to scan the children of subprograms and lexical
8864 blocks as well because Ada allows the definition of nested
8865 entities that could be interesting for the debugger, such as
8866 nested subprograms for instance. */
8867 if (last_die->has_children
8868 && (load_all
8869 || last_die->tag == DW_TAG_namespace
8870 || last_die->tag == DW_TAG_module
8871 || last_die->tag == DW_TAG_enumeration_type
8872 || (cu->language == language_cplus
8873 && last_die->tag == DW_TAG_subprogram
8874 && (last_die->name == NULL
8875 || strchr (last_die->name, '<') == NULL))
8876 || (cu->language != language_c
8877 && (last_die->tag == DW_TAG_class_type
8878 || last_die->tag == DW_TAG_interface_type
8879 || last_die->tag == DW_TAG_structure_type
8880 || last_die->tag == DW_TAG_union_type))
8881 || (cu->language == language_ada
8882 && (last_die->tag == DW_TAG_subprogram
8883 || last_die->tag == DW_TAG_lexical_block))))
8884 {
8885 nesting_level++;
8886 parent_die = last_die;
8887 continue;
8888 }
8889
8890 /* Otherwise we skip to the next sibling, if any. */
8891 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8892
8893 /* Back to the top, do it again. */
8894 }
8895 }
8896
8897 /* Read a minimal amount of information into the minimal die structure. */
8898
8899 static gdb_byte *
8900 read_partial_die (struct partial_die_info *part_die,
8901 struct abbrev_info *abbrev,
8902 unsigned int abbrev_len, bfd *abfd,
8903 gdb_byte *buffer, gdb_byte *info_ptr,
8904 struct dwarf2_cu *cu)
8905 {
8906 unsigned int i;
8907 struct attribute attr;
8908 int has_low_pc_attr = 0;
8909 int has_high_pc_attr = 0;
8910
8911 memset (part_die, 0, sizeof (struct partial_die_info));
8912
8913 part_die->offset = info_ptr - buffer;
8914
8915 info_ptr += abbrev_len;
8916
8917 if (abbrev == NULL)
8918 return info_ptr;
8919
8920 part_die->tag = abbrev->tag;
8921 part_die->has_children = abbrev->has_children;
8922
8923 for (i = 0; i < abbrev->num_attrs; ++i)
8924 {
8925 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8926
8927 /* Store the data if it is of an attribute we want to keep in a
8928 partial symbol table. */
8929 switch (attr.name)
8930 {
8931 case DW_AT_name:
8932 switch (part_die->tag)
8933 {
8934 case DW_TAG_compile_unit:
8935 case DW_TAG_type_unit:
8936 /* Compilation units have a DW_AT_name that is a filename, not
8937 a source language identifier. */
8938 case DW_TAG_enumeration_type:
8939 case DW_TAG_enumerator:
8940 /* These tags always have simple identifiers already; no need
8941 to canonicalize them. */
8942 part_die->name = DW_STRING (&attr);
8943 break;
8944 default:
8945 part_die->name
8946 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8947 &cu->objfile->objfile_obstack);
8948 break;
8949 }
8950 break;
8951 case DW_AT_linkage_name:
8952 case DW_AT_MIPS_linkage_name:
8953 /* Note that both forms of linkage name might appear. We
8954 assume they will be the same, and we only store the last
8955 one we see. */
8956 if (cu->language == language_ada)
8957 part_die->name = DW_STRING (&attr);
8958 part_die->linkage_name = DW_STRING (&attr);
8959 break;
8960 case DW_AT_low_pc:
8961 has_low_pc_attr = 1;
8962 part_die->lowpc = DW_ADDR (&attr);
8963 break;
8964 case DW_AT_high_pc:
8965 has_high_pc_attr = 1;
8966 part_die->highpc = DW_ADDR (&attr);
8967 break;
8968 case DW_AT_location:
8969 /* Support the .debug_loc offsets */
8970 if (attr_form_is_block (&attr))
8971 {
8972 part_die->locdesc = DW_BLOCK (&attr);
8973 }
8974 else if (attr_form_is_section_offset (&attr))
8975 {
8976 dwarf2_complex_location_expr_complaint ();
8977 }
8978 else
8979 {
8980 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8981 "partial symbol information");
8982 }
8983 break;
8984 case DW_AT_external:
8985 part_die->is_external = DW_UNSND (&attr);
8986 break;
8987 case DW_AT_declaration:
8988 part_die->is_declaration = DW_UNSND (&attr);
8989 break;
8990 case DW_AT_type:
8991 part_die->has_type = 1;
8992 break;
8993 case DW_AT_abstract_origin:
8994 case DW_AT_specification:
8995 case DW_AT_extension:
8996 part_die->has_specification = 1;
8997 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8998 break;
8999 case DW_AT_sibling:
9000 /* Ignore absolute siblings, they might point outside of
9001 the current compile unit. */
9002 if (attr.form == DW_FORM_ref_addr)
9003 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
9004 else
9005 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9006 break;
9007 case DW_AT_byte_size:
9008 part_die->has_byte_size = 1;
9009 break;
9010 case DW_AT_calling_convention:
9011 /* DWARF doesn't provide a way to identify a program's source-level
9012 entry point. DW_AT_calling_convention attributes are only meant
9013 to describe functions' calling conventions.
9014
9015 However, because it's a necessary piece of information in
9016 Fortran, and because DW_CC_program is the only piece of debugging
9017 information whose definition refers to a 'main program' at all,
9018 several compilers have begun marking Fortran main programs with
9019 DW_CC_program --- even when those functions use the standard
9020 calling conventions.
9021
9022 So until DWARF specifies a way to provide this information and
9023 compilers pick up the new representation, we'll support this
9024 practice. */
9025 if (DW_UNSND (&attr) == DW_CC_program
9026 && cu->language == language_fortran)
9027 {
9028 set_main_name (part_die->name);
9029
9030 /* As this DIE has a static linkage the name would be difficult
9031 to look up later. */
9032 language_of_main = language_fortran;
9033 }
9034 break;
9035 default:
9036 break;
9037 }
9038 }
9039
9040 /* When using the GNU linker, .gnu.linkonce. sections are used to
9041 eliminate duplicate copies of functions and vtables and such.
9042 The linker will arbitrarily choose one and discard the others.
9043 The AT_*_pc values for such functions refer to local labels in
9044 these sections. If the section from that file was discarded, the
9045 labels are not in the output, so the relocs get a value of 0.
9046 If this is a discarded function, mark the pc bounds as invalid,
9047 so that GDB will ignore it. */
9048 if (has_low_pc_attr && has_high_pc_attr
9049 && part_die->lowpc < part_die->highpc
9050 && (part_die->lowpc != 0
9051 || dwarf2_per_objfile->has_section_at_zero))
9052 part_die->has_pc_info = 1;
9053
9054 return info_ptr;
9055 }
9056
9057 /* Find a cached partial DIE at OFFSET in CU. */
9058
9059 static struct partial_die_info *
9060 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9061 {
9062 struct partial_die_info *lookup_die = NULL;
9063 struct partial_die_info part_die;
9064
9065 part_die.offset = offset;
9066 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9067
9068 return lookup_die;
9069 }
9070
9071 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9072 except in the case of .debug_types DIEs which do not reference
9073 outside their CU (they do however referencing other types via
9074 DW_FORM_sig8). */
9075
9076 static struct partial_die_info *
9077 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9078 {
9079 struct dwarf2_per_cu_data *per_cu = NULL;
9080 struct partial_die_info *pd = NULL;
9081
9082 if (cu->per_cu->from_debug_types)
9083 {
9084 pd = find_partial_die_in_comp_unit (offset, cu);
9085 if (pd != NULL)
9086 return pd;
9087 goto not_found;
9088 }
9089
9090 if (offset_in_cu_p (&cu->header, offset))
9091 {
9092 pd = find_partial_die_in_comp_unit (offset, cu);
9093 if (pd != NULL)
9094 return pd;
9095 }
9096
9097 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9098
9099 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9100 load_partial_comp_unit (per_cu, cu->objfile);
9101
9102 per_cu->cu->last_used = 0;
9103 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9104
9105 if (pd == NULL && per_cu->load_all_dies == 0)
9106 {
9107 struct cleanup *back_to;
9108 struct partial_die_info comp_unit_die;
9109 struct abbrev_info *abbrev;
9110 unsigned int bytes_read;
9111 char *info_ptr;
9112
9113 per_cu->load_all_dies = 1;
9114
9115 /* Re-read the DIEs. */
9116 back_to = make_cleanup (null_cleanup, 0);
9117 if (per_cu->cu->dwarf2_abbrevs == NULL)
9118 {
9119 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9120 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9121 }
9122 info_ptr = (dwarf2_per_objfile->info.buffer
9123 + per_cu->cu->header.offset
9124 + per_cu->cu->header.first_die_offset);
9125 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9126 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9127 per_cu->cu->objfile->obfd,
9128 dwarf2_per_objfile->info.buffer, info_ptr,
9129 per_cu->cu);
9130 if (comp_unit_die.has_children)
9131 load_partial_dies (per_cu->cu->objfile->obfd,
9132 dwarf2_per_objfile->info.buffer, info_ptr,
9133 0, per_cu->cu);
9134 do_cleanups (back_to);
9135
9136 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9137 }
9138
9139 not_found:
9140
9141 if (pd == NULL)
9142 internal_error (__FILE__, __LINE__,
9143 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
9144 offset, bfd_get_filename (cu->objfile->obfd));
9145 return pd;
9146 }
9147
9148 /* See if we can figure out if the class lives in a namespace. We do
9149 this by looking for a member function; its demangled name will
9150 contain namespace info, if there is any. */
9151
9152 static void
9153 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9154 struct dwarf2_cu *cu)
9155 {
9156 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9157 what template types look like, because the demangler
9158 frequently doesn't give the same name as the debug info. We
9159 could fix this by only using the demangled name to get the
9160 prefix (but see comment in read_structure_type). */
9161
9162 struct partial_die_info *real_pdi;
9163 struct partial_die_info *child_pdi;
9164
9165 /* If this DIE (this DIE's specification, if any) has a parent, then
9166 we should not do this. We'll prepend the parent's fully qualified
9167 name when we create the partial symbol. */
9168
9169 real_pdi = struct_pdi;
9170 while (real_pdi->has_specification)
9171 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9172
9173 if (real_pdi->die_parent != NULL)
9174 return;
9175
9176 for (child_pdi = struct_pdi->die_child;
9177 child_pdi != NULL;
9178 child_pdi = child_pdi->die_sibling)
9179 {
9180 if (child_pdi->tag == DW_TAG_subprogram
9181 && child_pdi->linkage_name != NULL)
9182 {
9183 char *actual_class_name
9184 = language_class_name_from_physname (cu->language_defn,
9185 child_pdi->linkage_name);
9186 if (actual_class_name != NULL)
9187 {
9188 struct_pdi->name
9189 = obsavestring (actual_class_name,
9190 strlen (actual_class_name),
9191 &cu->objfile->objfile_obstack);
9192 xfree (actual_class_name);
9193 }
9194 break;
9195 }
9196 }
9197 }
9198
9199 /* Adjust PART_DIE before generating a symbol for it. This function
9200 may set the is_external flag or change the DIE's name. */
9201
9202 static void
9203 fixup_partial_die (struct partial_die_info *part_die,
9204 struct dwarf2_cu *cu)
9205 {
9206 /* Once we've fixed up a die, there's no point in doing so again.
9207 This also avoids a memory leak if we were to call
9208 guess_partial_die_structure_name multiple times. */
9209 if (part_die->fixup_called)
9210 return;
9211
9212 /* If we found a reference attribute and the DIE has no name, try
9213 to find a name in the referred to DIE. */
9214
9215 if (part_die->name == NULL && part_die->has_specification)
9216 {
9217 struct partial_die_info *spec_die;
9218
9219 spec_die = find_partial_die (part_die->spec_offset, cu);
9220
9221 fixup_partial_die (spec_die, cu);
9222
9223 if (spec_die->name)
9224 {
9225 part_die->name = spec_die->name;
9226
9227 /* Copy DW_AT_external attribute if it is set. */
9228 if (spec_die->is_external)
9229 part_die->is_external = spec_die->is_external;
9230 }
9231 }
9232
9233 /* Set default names for some unnamed DIEs. */
9234
9235 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9236 part_die->name = "(anonymous namespace)";
9237
9238 /* If there is no parent die to provide a namespace, and there are
9239 children, see if we can determine the namespace from their linkage
9240 name.
9241 NOTE: We need to do this even if cu->has_namespace_info != 0.
9242 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9243 if (cu->language == language_cplus
9244 && dwarf2_per_objfile->types.asection != NULL
9245 && part_die->die_parent == NULL
9246 && part_die->has_children
9247 && (part_die->tag == DW_TAG_class_type
9248 || part_die->tag == DW_TAG_structure_type
9249 || part_die->tag == DW_TAG_union_type))
9250 guess_partial_die_structure_name (part_die, cu);
9251
9252 part_die->fixup_called = 1;
9253 }
9254
9255 /* Read an attribute value described by an attribute form. */
9256
9257 static gdb_byte *
9258 read_attribute_value (struct attribute *attr, unsigned form,
9259 bfd *abfd, gdb_byte *info_ptr,
9260 struct dwarf2_cu *cu)
9261 {
9262 struct comp_unit_head *cu_header = &cu->header;
9263 unsigned int bytes_read;
9264 struct dwarf_block *blk;
9265
9266 attr->form = form;
9267 switch (form)
9268 {
9269 case DW_FORM_ref_addr:
9270 if (cu->header.version == 2)
9271 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9272 else
9273 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9274 info_ptr += bytes_read;
9275 break;
9276 case DW_FORM_addr:
9277 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9278 info_ptr += bytes_read;
9279 break;
9280 case DW_FORM_block2:
9281 blk = dwarf_alloc_block (cu);
9282 blk->size = read_2_bytes (abfd, info_ptr);
9283 info_ptr += 2;
9284 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9285 info_ptr += blk->size;
9286 DW_BLOCK (attr) = blk;
9287 break;
9288 case DW_FORM_block4:
9289 blk = dwarf_alloc_block (cu);
9290 blk->size = read_4_bytes (abfd, info_ptr);
9291 info_ptr += 4;
9292 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9293 info_ptr += blk->size;
9294 DW_BLOCK (attr) = blk;
9295 break;
9296 case DW_FORM_data2:
9297 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9298 info_ptr += 2;
9299 break;
9300 case DW_FORM_data4:
9301 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9302 info_ptr += 4;
9303 break;
9304 case DW_FORM_data8:
9305 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9306 info_ptr += 8;
9307 break;
9308 case DW_FORM_sec_offset:
9309 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9310 info_ptr += bytes_read;
9311 break;
9312 case DW_FORM_string:
9313 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9314 DW_STRING_IS_CANONICAL (attr) = 0;
9315 info_ptr += bytes_read;
9316 break;
9317 case DW_FORM_strp:
9318 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9319 &bytes_read);
9320 DW_STRING_IS_CANONICAL (attr) = 0;
9321 info_ptr += bytes_read;
9322 break;
9323 case DW_FORM_exprloc:
9324 case DW_FORM_block:
9325 blk = dwarf_alloc_block (cu);
9326 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9327 info_ptr += bytes_read;
9328 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9329 info_ptr += blk->size;
9330 DW_BLOCK (attr) = blk;
9331 break;
9332 case DW_FORM_block1:
9333 blk = dwarf_alloc_block (cu);
9334 blk->size = read_1_byte (abfd, info_ptr);
9335 info_ptr += 1;
9336 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9337 info_ptr += blk->size;
9338 DW_BLOCK (attr) = blk;
9339 break;
9340 case DW_FORM_data1:
9341 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9342 info_ptr += 1;
9343 break;
9344 case DW_FORM_flag:
9345 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9346 info_ptr += 1;
9347 break;
9348 case DW_FORM_flag_present:
9349 DW_UNSND (attr) = 1;
9350 break;
9351 case DW_FORM_sdata:
9352 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9353 info_ptr += bytes_read;
9354 break;
9355 case DW_FORM_udata:
9356 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9357 info_ptr += bytes_read;
9358 break;
9359 case DW_FORM_ref1:
9360 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9361 info_ptr += 1;
9362 break;
9363 case DW_FORM_ref2:
9364 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9365 info_ptr += 2;
9366 break;
9367 case DW_FORM_ref4:
9368 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9369 info_ptr += 4;
9370 break;
9371 case DW_FORM_ref8:
9372 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9373 info_ptr += 8;
9374 break;
9375 case DW_FORM_sig8:
9376 /* Convert the signature to something we can record in DW_UNSND
9377 for later lookup.
9378 NOTE: This is NULL if the type wasn't found. */
9379 DW_SIGNATURED_TYPE (attr) =
9380 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9381 info_ptr += 8;
9382 break;
9383 case DW_FORM_ref_udata:
9384 DW_ADDR (attr) = (cu->header.offset
9385 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9386 info_ptr += bytes_read;
9387 break;
9388 case DW_FORM_indirect:
9389 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9390 info_ptr += bytes_read;
9391 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9392 break;
9393 default:
9394 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9395 dwarf_form_name (form),
9396 bfd_get_filename (abfd));
9397 }
9398
9399 /* We have seen instances where the compiler tried to emit a byte
9400 size attribute of -1 which ended up being encoded as an unsigned
9401 0xffffffff. Although 0xffffffff is technically a valid size value,
9402 an object of this size seems pretty unlikely so we can relatively
9403 safely treat these cases as if the size attribute was invalid and
9404 treat them as zero by default. */
9405 if (attr->name == DW_AT_byte_size
9406 && form == DW_FORM_data4
9407 && DW_UNSND (attr) >= 0xffffffff)
9408 {
9409 complaint
9410 (&symfile_complaints,
9411 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9412 hex_string (DW_UNSND (attr)));
9413 DW_UNSND (attr) = 0;
9414 }
9415
9416 return info_ptr;
9417 }
9418
9419 /* Read an attribute described by an abbreviated attribute. */
9420
9421 static gdb_byte *
9422 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9423 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9424 {
9425 attr->name = abbrev->name;
9426 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9427 }
9428
9429 /* read dwarf information from a buffer */
9430
9431 static unsigned int
9432 read_1_byte (bfd *abfd, gdb_byte *buf)
9433 {
9434 return bfd_get_8 (abfd, buf);
9435 }
9436
9437 static int
9438 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9439 {
9440 return bfd_get_signed_8 (abfd, buf);
9441 }
9442
9443 static unsigned int
9444 read_2_bytes (bfd *abfd, gdb_byte *buf)
9445 {
9446 return bfd_get_16 (abfd, buf);
9447 }
9448
9449 static int
9450 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9451 {
9452 return bfd_get_signed_16 (abfd, buf);
9453 }
9454
9455 static unsigned int
9456 read_4_bytes (bfd *abfd, gdb_byte *buf)
9457 {
9458 return bfd_get_32 (abfd, buf);
9459 }
9460
9461 static int
9462 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9463 {
9464 return bfd_get_signed_32 (abfd, buf);
9465 }
9466
9467 static ULONGEST
9468 read_8_bytes (bfd *abfd, gdb_byte *buf)
9469 {
9470 return bfd_get_64 (abfd, buf);
9471 }
9472
9473 static CORE_ADDR
9474 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9475 unsigned int *bytes_read)
9476 {
9477 struct comp_unit_head *cu_header = &cu->header;
9478 CORE_ADDR retval = 0;
9479
9480 if (cu_header->signed_addr_p)
9481 {
9482 switch (cu_header->addr_size)
9483 {
9484 case 2:
9485 retval = bfd_get_signed_16 (abfd, buf);
9486 break;
9487 case 4:
9488 retval = bfd_get_signed_32 (abfd, buf);
9489 break;
9490 case 8:
9491 retval = bfd_get_signed_64 (abfd, buf);
9492 break;
9493 default:
9494 internal_error (__FILE__, __LINE__,
9495 _("read_address: bad switch, signed [in module %s]"),
9496 bfd_get_filename (abfd));
9497 }
9498 }
9499 else
9500 {
9501 switch (cu_header->addr_size)
9502 {
9503 case 2:
9504 retval = bfd_get_16 (abfd, buf);
9505 break;
9506 case 4:
9507 retval = bfd_get_32 (abfd, buf);
9508 break;
9509 case 8:
9510 retval = bfd_get_64 (abfd, buf);
9511 break;
9512 default:
9513 internal_error (__FILE__, __LINE__,
9514 _("read_address: bad switch, unsigned [in module %s]"),
9515 bfd_get_filename (abfd));
9516 }
9517 }
9518
9519 *bytes_read = cu_header->addr_size;
9520 return retval;
9521 }
9522
9523 /* Read the initial length from a section. The (draft) DWARF 3
9524 specification allows the initial length to take up either 4 bytes
9525 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9526 bytes describe the length and all offsets will be 8 bytes in length
9527 instead of 4.
9528
9529 An older, non-standard 64-bit format is also handled by this
9530 function. The older format in question stores the initial length
9531 as an 8-byte quantity without an escape value. Lengths greater
9532 than 2^32 aren't very common which means that the initial 4 bytes
9533 is almost always zero. Since a length value of zero doesn't make
9534 sense for the 32-bit format, this initial zero can be considered to
9535 be an escape value which indicates the presence of the older 64-bit
9536 format. As written, the code can't detect (old format) lengths
9537 greater than 4GB. If it becomes necessary to handle lengths
9538 somewhat larger than 4GB, we could allow other small values (such
9539 as the non-sensical values of 1, 2, and 3) to also be used as
9540 escape values indicating the presence of the old format.
9541
9542 The value returned via bytes_read should be used to increment the
9543 relevant pointer after calling read_initial_length().
9544
9545 [ Note: read_initial_length() and read_offset() are based on the
9546 document entitled "DWARF Debugging Information Format", revision
9547 3, draft 8, dated November 19, 2001. This document was obtained
9548 from:
9549
9550 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9551
9552 This document is only a draft and is subject to change. (So beware.)
9553
9554 Details regarding the older, non-standard 64-bit format were
9555 determined empirically by examining 64-bit ELF files produced by
9556 the SGI toolchain on an IRIX 6.5 machine.
9557
9558 - Kevin, July 16, 2002
9559 ] */
9560
9561 static LONGEST
9562 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9563 {
9564 LONGEST length = bfd_get_32 (abfd, buf);
9565
9566 if (length == 0xffffffff)
9567 {
9568 length = bfd_get_64 (abfd, buf + 4);
9569 *bytes_read = 12;
9570 }
9571 else if (length == 0)
9572 {
9573 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9574 length = bfd_get_64 (abfd, buf);
9575 *bytes_read = 8;
9576 }
9577 else
9578 {
9579 *bytes_read = 4;
9580 }
9581
9582 return length;
9583 }
9584
9585 /* Cover function for read_initial_length.
9586 Returns the length of the object at BUF, and stores the size of the
9587 initial length in *BYTES_READ and stores the size that offsets will be in
9588 *OFFSET_SIZE.
9589 If the initial length size is not equivalent to that specified in
9590 CU_HEADER then issue a complaint.
9591 This is useful when reading non-comp-unit headers. */
9592
9593 static LONGEST
9594 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9595 const struct comp_unit_head *cu_header,
9596 unsigned int *bytes_read,
9597 unsigned int *offset_size)
9598 {
9599 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9600
9601 gdb_assert (cu_header->initial_length_size == 4
9602 || cu_header->initial_length_size == 8
9603 || cu_header->initial_length_size == 12);
9604
9605 if (cu_header->initial_length_size != *bytes_read)
9606 complaint (&symfile_complaints,
9607 _("intermixed 32-bit and 64-bit DWARF sections"));
9608
9609 *offset_size = (*bytes_read == 4) ? 4 : 8;
9610 return length;
9611 }
9612
9613 /* Read an offset from the data stream. The size of the offset is
9614 given by cu_header->offset_size. */
9615
9616 static LONGEST
9617 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9618 unsigned int *bytes_read)
9619 {
9620 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9621
9622 *bytes_read = cu_header->offset_size;
9623 return offset;
9624 }
9625
9626 /* Read an offset from the data stream. */
9627
9628 static LONGEST
9629 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9630 {
9631 LONGEST retval = 0;
9632
9633 switch (offset_size)
9634 {
9635 case 4:
9636 retval = bfd_get_32 (abfd, buf);
9637 break;
9638 case 8:
9639 retval = bfd_get_64 (abfd, buf);
9640 break;
9641 default:
9642 internal_error (__FILE__, __LINE__,
9643 _("read_offset_1: bad switch [in module %s]"),
9644 bfd_get_filename (abfd));
9645 }
9646
9647 return retval;
9648 }
9649
9650 static gdb_byte *
9651 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9652 {
9653 /* If the size of a host char is 8 bits, we can return a pointer
9654 to the buffer, otherwise we have to copy the data to a buffer
9655 allocated on the temporary obstack. */
9656 gdb_assert (HOST_CHAR_BIT == 8);
9657 return buf;
9658 }
9659
9660 static char *
9661 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9662 {
9663 /* If the size of a host char is 8 bits, we can return a pointer
9664 to the string, otherwise we have to copy the string to a buffer
9665 allocated on the temporary obstack. */
9666 gdb_assert (HOST_CHAR_BIT == 8);
9667 if (*buf == '\0')
9668 {
9669 *bytes_read_ptr = 1;
9670 return NULL;
9671 }
9672 *bytes_read_ptr = strlen ((char *) buf) + 1;
9673 return (char *) buf;
9674 }
9675
9676 static char *
9677 read_indirect_string (bfd *abfd, gdb_byte *buf,
9678 const struct comp_unit_head *cu_header,
9679 unsigned int *bytes_read_ptr)
9680 {
9681 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9682
9683 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9684 if (dwarf2_per_objfile->str.buffer == NULL)
9685 {
9686 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9687 bfd_get_filename (abfd));
9688 return NULL;
9689 }
9690 if (str_offset >= dwarf2_per_objfile->str.size)
9691 {
9692 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9693 bfd_get_filename (abfd));
9694 return NULL;
9695 }
9696 gdb_assert (HOST_CHAR_BIT == 8);
9697 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9698 return NULL;
9699 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9700 }
9701
9702 static unsigned long
9703 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9704 {
9705 unsigned long result;
9706 unsigned int num_read;
9707 int i, shift;
9708 unsigned char byte;
9709
9710 result = 0;
9711 shift = 0;
9712 num_read = 0;
9713 i = 0;
9714 while (1)
9715 {
9716 byte = bfd_get_8 (abfd, buf);
9717 buf++;
9718 num_read++;
9719 result |= ((unsigned long)(byte & 127) << shift);
9720 if ((byte & 128) == 0)
9721 {
9722 break;
9723 }
9724 shift += 7;
9725 }
9726 *bytes_read_ptr = num_read;
9727 return result;
9728 }
9729
9730 static long
9731 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9732 {
9733 long result;
9734 int i, shift, num_read;
9735 unsigned char byte;
9736
9737 result = 0;
9738 shift = 0;
9739 num_read = 0;
9740 i = 0;
9741 while (1)
9742 {
9743 byte = bfd_get_8 (abfd, buf);
9744 buf++;
9745 num_read++;
9746 result |= ((long)(byte & 127) << shift);
9747 shift += 7;
9748 if ((byte & 128) == 0)
9749 {
9750 break;
9751 }
9752 }
9753 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9754 result |= -(((long)1) << shift);
9755 *bytes_read_ptr = num_read;
9756 return result;
9757 }
9758
9759 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9760
9761 static gdb_byte *
9762 skip_leb128 (bfd *abfd, gdb_byte *buf)
9763 {
9764 int byte;
9765
9766 while (1)
9767 {
9768 byte = bfd_get_8 (abfd, buf);
9769 buf++;
9770 if ((byte & 128) == 0)
9771 return buf;
9772 }
9773 }
9774
9775 static void
9776 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9777 {
9778 switch (lang)
9779 {
9780 case DW_LANG_C89:
9781 case DW_LANG_C99:
9782 case DW_LANG_C:
9783 cu->language = language_c;
9784 break;
9785 case DW_LANG_C_plus_plus:
9786 cu->language = language_cplus;
9787 break;
9788 case DW_LANG_D:
9789 cu->language = language_d;
9790 break;
9791 case DW_LANG_Fortran77:
9792 case DW_LANG_Fortran90:
9793 case DW_LANG_Fortran95:
9794 cu->language = language_fortran;
9795 break;
9796 case DW_LANG_Mips_Assembler:
9797 cu->language = language_asm;
9798 break;
9799 case DW_LANG_Java:
9800 cu->language = language_java;
9801 break;
9802 case DW_LANG_Ada83:
9803 case DW_LANG_Ada95:
9804 cu->language = language_ada;
9805 break;
9806 case DW_LANG_Modula2:
9807 cu->language = language_m2;
9808 break;
9809 case DW_LANG_Pascal83:
9810 cu->language = language_pascal;
9811 break;
9812 case DW_LANG_ObjC:
9813 cu->language = language_objc;
9814 break;
9815 case DW_LANG_Cobol74:
9816 case DW_LANG_Cobol85:
9817 default:
9818 cu->language = language_minimal;
9819 break;
9820 }
9821 cu->language_defn = language_def (cu->language);
9822 }
9823
9824 /* Return the named attribute or NULL if not there. */
9825
9826 static struct attribute *
9827 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9828 {
9829 unsigned int i;
9830 struct attribute *spec = NULL;
9831
9832 for (i = 0; i < die->num_attrs; ++i)
9833 {
9834 if (die->attrs[i].name == name)
9835 return &die->attrs[i];
9836 if (die->attrs[i].name == DW_AT_specification
9837 || die->attrs[i].name == DW_AT_abstract_origin)
9838 spec = &die->attrs[i];
9839 }
9840
9841 if (spec)
9842 {
9843 die = follow_die_ref (die, spec, &cu);
9844 return dwarf2_attr (die, name, cu);
9845 }
9846
9847 return NULL;
9848 }
9849
9850 /* Return the named attribute or NULL if not there,
9851 but do not follow DW_AT_specification, etc.
9852 This is for use in contexts where we're reading .debug_types dies.
9853 Following DW_AT_specification, DW_AT_abstract_origin will take us
9854 back up the chain, and we want to go down. */
9855
9856 static struct attribute *
9857 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9858 struct dwarf2_cu *cu)
9859 {
9860 unsigned int i;
9861
9862 for (i = 0; i < die->num_attrs; ++i)
9863 if (die->attrs[i].name == name)
9864 return &die->attrs[i];
9865
9866 return NULL;
9867 }
9868
9869 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9870 and holds a non-zero value. This function should only be used for
9871 DW_FORM_flag or DW_FORM_flag_present attributes. */
9872
9873 static int
9874 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9875 {
9876 struct attribute *attr = dwarf2_attr (die, name, cu);
9877
9878 return (attr && DW_UNSND (attr));
9879 }
9880
9881 static int
9882 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9883 {
9884 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9885 which value is non-zero. However, we have to be careful with
9886 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9887 (via dwarf2_flag_true_p) follows this attribute. So we may
9888 end up accidently finding a declaration attribute that belongs
9889 to a different DIE referenced by the specification attribute,
9890 even though the given DIE does not have a declaration attribute. */
9891 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9892 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9893 }
9894
9895 /* Return the die giving the specification for DIE, if there is
9896 one. *SPEC_CU is the CU containing DIE on input, and the CU
9897 containing the return value on output. If there is no
9898 specification, but there is an abstract origin, that is
9899 returned. */
9900
9901 static struct die_info *
9902 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9903 {
9904 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9905 *spec_cu);
9906
9907 if (spec_attr == NULL)
9908 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9909
9910 if (spec_attr == NULL)
9911 return NULL;
9912 else
9913 return follow_die_ref (die, spec_attr, spec_cu);
9914 }
9915
9916 /* Free the line_header structure *LH, and any arrays and strings it
9917 refers to.
9918 NOTE: This is also used as a "cleanup" function. */
9919
9920 static void
9921 free_line_header (struct line_header *lh)
9922 {
9923 if (lh->standard_opcode_lengths)
9924 xfree (lh->standard_opcode_lengths);
9925
9926 /* Remember that all the lh->file_names[i].name pointers are
9927 pointers into debug_line_buffer, and don't need to be freed. */
9928 if (lh->file_names)
9929 xfree (lh->file_names);
9930
9931 /* Similarly for the include directory names. */
9932 if (lh->include_dirs)
9933 xfree (lh->include_dirs);
9934
9935 xfree (lh);
9936 }
9937
9938 /* Add an entry to LH's include directory table. */
9939
9940 static void
9941 add_include_dir (struct line_header *lh, char *include_dir)
9942 {
9943 /* Grow the array if necessary. */
9944 if (lh->include_dirs_size == 0)
9945 {
9946 lh->include_dirs_size = 1; /* for testing */
9947 lh->include_dirs = xmalloc (lh->include_dirs_size
9948 * sizeof (*lh->include_dirs));
9949 }
9950 else if (lh->num_include_dirs >= lh->include_dirs_size)
9951 {
9952 lh->include_dirs_size *= 2;
9953 lh->include_dirs = xrealloc (lh->include_dirs,
9954 (lh->include_dirs_size
9955 * sizeof (*lh->include_dirs)));
9956 }
9957
9958 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9959 }
9960
9961 /* Add an entry to LH's file name table. */
9962
9963 static void
9964 add_file_name (struct line_header *lh,
9965 char *name,
9966 unsigned int dir_index,
9967 unsigned int mod_time,
9968 unsigned int length)
9969 {
9970 struct file_entry *fe;
9971
9972 /* Grow the array if necessary. */
9973 if (lh->file_names_size == 0)
9974 {
9975 lh->file_names_size = 1; /* for testing */
9976 lh->file_names = xmalloc (lh->file_names_size
9977 * sizeof (*lh->file_names));
9978 }
9979 else if (lh->num_file_names >= lh->file_names_size)
9980 {
9981 lh->file_names_size *= 2;
9982 lh->file_names = xrealloc (lh->file_names,
9983 (lh->file_names_size
9984 * sizeof (*lh->file_names)));
9985 }
9986
9987 fe = &lh->file_names[lh->num_file_names++];
9988 fe->name = name;
9989 fe->dir_index = dir_index;
9990 fe->mod_time = mod_time;
9991 fe->length = length;
9992 fe->included_p = 0;
9993 fe->symtab = NULL;
9994 }
9995
9996 /* Read the statement program header starting at OFFSET in
9997 .debug_line, according to the endianness of ABFD. Return a pointer
9998 to a struct line_header, allocated using xmalloc.
9999
10000 NOTE: the strings in the include directory and file name tables of
10001 the returned object point into debug_line_buffer, and must not be
10002 freed. */
10003
10004 static struct line_header *
10005 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10006 struct dwarf2_cu *cu)
10007 {
10008 struct cleanup *back_to;
10009 struct line_header *lh;
10010 gdb_byte *line_ptr;
10011 unsigned int bytes_read, offset_size;
10012 int i;
10013 char *cur_dir, *cur_file;
10014
10015 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10016 if (dwarf2_per_objfile->line.buffer == NULL)
10017 {
10018 complaint (&symfile_complaints, _("missing .debug_line section"));
10019 return 0;
10020 }
10021
10022 /* Make sure that at least there's room for the total_length field.
10023 That could be 12 bytes long, but we're just going to fudge that. */
10024 if (offset + 4 >= dwarf2_per_objfile->line.size)
10025 {
10026 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10027 return 0;
10028 }
10029
10030 lh = xmalloc (sizeof (*lh));
10031 memset (lh, 0, sizeof (*lh));
10032 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10033 (void *) lh);
10034
10035 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10036
10037 /* Read in the header. */
10038 lh->total_length =
10039 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10040 &bytes_read, &offset_size);
10041 line_ptr += bytes_read;
10042 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10043 + dwarf2_per_objfile->line.size))
10044 {
10045 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10046 return 0;
10047 }
10048 lh->statement_program_end = line_ptr + lh->total_length;
10049 lh->version = read_2_bytes (abfd, line_ptr);
10050 line_ptr += 2;
10051 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10052 line_ptr += offset_size;
10053 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10054 line_ptr += 1;
10055 if (lh->version >= 4)
10056 {
10057 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10058 line_ptr += 1;
10059 }
10060 else
10061 lh->maximum_ops_per_instruction = 1;
10062
10063 if (lh->maximum_ops_per_instruction == 0)
10064 {
10065 lh->maximum_ops_per_instruction = 1;
10066 complaint (&symfile_complaints,
10067 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
10068 }
10069
10070 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10071 line_ptr += 1;
10072 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10073 line_ptr += 1;
10074 lh->line_range = read_1_byte (abfd, line_ptr);
10075 line_ptr += 1;
10076 lh->opcode_base = read_1_byte (abfd, line_ptr);
10077 line_ptr += 1;
10078 lh->standard_opcode_lengths
10079 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10080
10081 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10082 for (i = 1; i < lh->opcode_base; ++i)
10083 {
10084 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10085 line_ptr += 1;
10086 }
10087
10088 /* Read directory table. */
10089 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10090 {
10091 line_ptr += bytes_read;
10092 add_include_dir (lh, cur_dir);
10093 }
10094 line_ptr += bytes_read;
10095
10096 /* Read file name table. */
10097 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10098 {
10099 unsigned int dir_index, mod_time, length;
10100
10101 line_ptr += bytes_read;
10102 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10103 line_ptr += bytes_read;
10104 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10105 line_ptr += bytes_read;
10106 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10107 line_ptr += bytes_read;
10108
10109 add_file_name (lh, cur_file, dir_index, mod_time, length);
10110 }
10111 line_ptr += bytes_read;
10112 lh->statement_program_start = line_ptr;
10113
10114 if (line_ptr > (dwarf2_per_objfile->line.buffer
10115 + dwarf2_per_objfile->line.size))
10116 complaint (&symfile_complaints,
10117 _("line number info header doesn't fit in `.debug_line' section"));
10118
10119 discard_cleanups (back_to);
10120 return lh;
10121 }
10122
10123 /* This function exists to work around a bug in certain compilers
10124 (particularly GCC 2.95), in which the first line number marker of a
10125 function does not show up until after the prologue, right before
10126 the second line number marker. This function shifts ADDRESS down
10127 to the beginning of the function if necessary, and is called on
10128 addresses passed to record_line. */
10129
10130 static CORE_ADDR
10131 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10132 {
10133 struct function_range *fn;
10134
10135 /* Find the function_range containing address. */
10136 if (!cu->first_fn)
10137 return address;
10138
10139 if (!cu->cached_fn)
10140 cu->cached_fn = cu->first_fn;
10141
10142 fn = cu->cached_fn;
10143 while (fn)
10144 if (fn->lowpc <= address && fn->highpc > address)
10145 goto found;
10146 else
10147 fn = fn->next;
10148
10149 fn = cu->first_fn;
10150 while (fn && fn != cu->cached_fn)
10151 if (fn->lowpc <= address && fn->highpc > address)
10152 goto found;
10153 else
10154 fn = fn->next;
10155
10156 return address;
10157
10158 found:
10159 if (fn->seen_line)
10160 return address;
10161 if (address != fn->lowpc)
10162 complaint (&symfile_complaints,
10163 _("misplaced first line number at 0x%lx for '%s'"),
10164 (unsigned long) address, fn->name);
10165 fn->seen_line = 1;
10166 return fn->lowpc;
10167 }
10168
10169 /* Subroutine of dwarf_decode_lines to simplify it.
10170 Return the file name of the psymtab for included file FILE_INDEX
10171 in line header LH of PST.
10172 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10173 If space for the result is malloc'd, it will be freed by a cleanup.
10174 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10175
10176 static char *
10177 psymtab_include_file_name (const struct line_header *lh, int file_index,
10178 const struct partial_symtab *pst,
10179 const char *comp_dir)
10180 {
10181 const struct file_entry fe = lh->file_names [file_index];
10182 char *include_name = fe.name;
10183 char *include_name_to_compare = include_name;
10184 char *dir_name = NULL;
10185 const char *pst_filename;
10186 char *copied_name = NULL;
10187 int file_is_pst;
10188
10189 if (fe.dir_index)
10190 dir_name = lh->include_dirs[fe.dir_index - 1];
10191
10192 if (!IS_ABSOLUTE_PATH (include_name)
10193 && (dir_name != NULL || comp_dir != NULL))
10194 {
10195 /* Avoid creating a duplicate psymtab for PST.
10196 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10197 Before we do the comparison, however, we need to account
10198 for DIR_NAME and COMP_DIR.
10199 First prepend dir_name (if non-NULL). If we still don't
10200 have an absolute path prepend comp_dir (if non-NULL).
10201 However, the directory we record in the include-file's
10202 psymtab does not contain COMP_DIR (to match the
10203 corresponding symtab(s)).
10204
10205 Example:
10206
10207 bash$ cd /tmp
10208 bash$ gcc -g ./hello.c
10209 include_name = "hello.c"
10210 dir_name = "."
10211 DW_AT_comp_dir = comp_dir = "/tmp"
10212 DW_AT_name = "./hello.c" */
10213
10214 if (dir_name != NULL)
10215 {
10216 include_name = concat (dir_name, SLASH_STRING,
10217 include_name, (char *)NULL);
10218 include_name_to_compare = include_name;
10219 make_cleanup (xfree, include_name);
10220 }
10221 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10222 {
10223 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10224 include_name, (char *)NULL);
10225 }
10226 }
10227
10228 pst_filename = pst->filename;
10229 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10230 {
10231 copied_name = concat (pst->dirname, SLASH_STRING,
10232 pst_filename, (char *)NULL);
10233 pst_filename = copied_name;
10234 }
10235
10236 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10237
10238 if (include_name_to_compare != include_name)
10239 xfree (include_name_to_compare);
10240 if (copied_name != NULL)
10241 xfree (copied_name);
10242
10243 if (file_is_pst)
10244 return NULL;
10245 return include_name;
10246 }
10247
10248 /* Decode the Line Number Program (LNP) for the given line_header
10249 structure and CU. The actual information extracted and the type
10250 of structures created from the LNP depends on the value of PST.
10251
10252 1. If PST is NULL, then this procedure uses the data from the program
10253 to create all necessary symbol tables, and their linetables.
10254
10255 2. If PST is not NULL, this procedure reads the program to determine
10256 the list of files included by the unit represented by PST, and
10257 builds all the associated partial symbol tables.
10258
10259 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10260 It is used for relative paths in the line table.
10261 NOTE: When processing partial symtabs (pst != NULL),
10262 comp_dir == pst->dirname.
10263
10264 NOTE: It is important that psymtabs have the same file name (via strcmp)
10265 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10266 symtab we don't use it in the name of the psymtabs we create.
10267 E.g. expand_line_sal requires this when finding psymtabs to expand.
10268 A good testcase for this is mb-inline.exp. */
10269
10270 static void
10271 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10272 struct dwarf2_cu *cu, struct partial_symtab *pst)
10273 {
10274 gdb_byte *line_ptr, *extended_end;
10275 gdb_byte *line_end;
10276 unsigned int bytes_read, extended_len;
10277 unsigned char op_code, extended_op, adj_opcode;
10278 CORE_ADDR baseaddr;
10279 struct objfile *objfile = cu->objfile;
10280 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10281 const int decode_for_pst_p = (pst != NULL);
10282 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10283
10284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10285
10286 line_ptr = lh->statement_program_start;
10287 line_end = lh->statement_program_end;
10288
10289 /* Read the statement sequences until there's nothing left. */
10290 while (line_ptr < line_end)
10291 {
10292 /* state machine registers */
10293 CORE_ADDR address = 0;
10294 unsigned int file = 1;
10295 unsigned int line = 1;
10296 unsigned int column = 0;
10297 int is_stmt = lh->default_is_stmt;
10298 int basic_block = 0;
10299 int end_sequence = 0;
10300 CORE_ADDR addr;
10301 unsigned char op_index = 0;
10302
10303 if (!decode_for_pst_p && lh->num_file_names >= file)
10304 {
10305 /* Start a subfile for the current file of the state machine. */
10306 /* lh->include_dirs and lh->file_names are 0-based, but the
10307 directory and file name numbers in the statement program
10308 are 1-based. */
10309 struct file_entry *fe = &lh->file_names[file - 1];
10310 char *dir = NULL;
10311
10312 if (fe->dir_index)
10313 dir = lh->include_dirs[fe->dir_index - 1];
10314
10315 dwarf2_start_subfile (fe->name, dir, comp_dir);
10316 }
10317
10318 /* Decode the table. */
10319 while (!end_sequence)
10320 {
10321 op_code = read_1_byte (abfd, line_ptr);
10322 line_ptr += 1;
10323 if (line_ptr > line_end)
10324 {
10325 dwarf2_debug_line_missing_end_sequence_complaint ();
10326 break;
10327 }
10328
10329 if (op_code >= lh->opcode_base)
10330 {
10331 /* Special operand. */
10332 adj_opcode = op_code - lh->opcode_base;
10333 address += (((op_index + (adj_opcode / lh->line_range))
10334 / lh->maximum_ops_per_instruction)
10335 * lh->minimum_instruction_length);
10336 op_index = ((op_index + (adj_opcode / lh->line_range))
10337 % lh->maximum_ops_per_instruction);
10338 line += lh->line_base + (adj_opcode % lh->line_range);
10339 if (lh->num_file_names < file || file == 0)
10340 dwarf2_debug_line_missing_file_complaint ();
10341 /* For now we ignore lines not starting on an
10342 instruction boundary. */
10343 else if (op_index == 0)
10344 {
10345 lh->file_names[file - 1].included_p = 1;
10346 if (!decode_for_pst_p && is_stmt)
10347 {
10348 if (last_subfile != current_subfile)
10349 {
10350 addr = gdbarch_addr_bits_remove (gdbarch, address);
10351 if (last_subfile)
10352 record_line (last_subfile, 0, addr);
10353 last_subfile = current_subfile;
10354 }
10355 /* Append row to matrix using current values. */
10356 addr = check_cu_functions (address, cu);
10357 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10358 record_line (current_subfile, line, addr);
10359 }
10360 }
10361 basic_block = 0;
10362 }
10363 else switch (op_code)
10364 {
10365 case DW_LNS_extended_op:
10366 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10367 line_ptr += bytes_read;
10368 extended_end = line_ptr + extended_len;
10369 extended_op = read_1_byte (abfd, line_ptr);
10370 line_ptr += 1;
10371 switch (extended_op)
10372 {
10373 case DW_LNE_end_sequence:
10374 end_sequence = 1;
10375 break;
10376 case DW_LNE_set_address:
10377 address = read_address (abfd, line_ptr, cu, &bytes_read);
10378 op_index = 0;
10379 line_ptr += bytes_read;
10380 address += baseaddr;
10381 break;
10382 case DW_LNE_define_file:
10383 {
10384 char *cur_file;
10385 unsigned int dir_index, mod_time, length;
10386
10387 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10388 line_ptr += bytes_read;
10389 dir_index =
10390 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10391 line_ptr += bytes_read;
10392 mod_time =
10393 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10394 line_ptr += bytes_read;
10395 length =
10396 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10397 line_ptr += bytes_read;
10398 add_file_name (lh, cur_file, dir_index, mod_time, length);
10399 }
10400 break;
10401 case DW_LNE_set_discriminator:
10402 /* The discriminator is not interesting to the debugger;
10403 just ignore it. */
10404 line_ptr = extended_end;
10405 break;
10406 default:
10407 complaint (&symfile_complaints,
10408 _("mangled .debug_line section"));
10409 return;
10410 }
10411 /* Make sure that we parsed the extended op correctly. If e.g.
10412 we expected a different address size than the producer used,
10413 we may have read the wrong number of bytes. */
10414 if (line_ptr != extended_end)
10415 {
10416 complaint (&symfile_complaints,
10417 _("mangled .debug_line section"));
10418 return;
10419 }
10420 break;
10421 case DW_LNS_copy:
10422 if (lh->num_file_names < file || file == 0)
10423 dwarf2_debug_line_missing_file_complaint ();
10424 else
10425 {
10426 lh->file_names[file - 1].included_p = 1;
10427 if (!decode_for_pst_p && is_stmt)
10428 {
10429 if (last_subfile != current_subfile)
10430 {
10431 addr = gdbarch_addr_bits_remove (gdbarch, address);
10432 if (last_subfile)
10433 record_line (last_subfile, 0, addr);
10434 last_subfile = current_subfile;
10435 }
10436 addr = check_cu_functions (address, cu);
10437 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10438 record_line (current_subfile, line, addr);
10439 }
10440 }
10441 basic_block = 0;
10442 break;
10443 case DW_LNS_advance_pc:
10444 {
10445 CORE_ADDR adjust
10446 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10447
10448 address += (((op_index + adjust)
10449 / lh->maximum_ops_per_instruction)
10450 * lh->minimum_instruction_length);
10451 op_index = ((op_index + adjust)
10452 % lh->maximum_ops_per_instruction);
10453 line_ptr += bytes_read;
10454 }
10455 break;
10456 case DW_LNS_advance_line:
10457 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10458 line_ptr += bytes_read;
10459 break;
10460 case DW_LNS_set_file:
10461 {
10462 /* The arrays lh->include_dirs and lh->file_names are
10463 0-based, but the directory and file name numbers in
10464 the statement program are 1-based. */
10465 struct file_entry *fe;
10466 char *dir = NULL;
10467
10468 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10469 line_ptr += bytes_read;
10470 if (lh->num_file_names < file || file == 0)
10471 dwarf2_debug_line_missing_file_complaint ();
10472 else
10473 {
10474 fe = &lh->file_names[file - 1];
10475 if (fe->dir_index)
10476 dir = lh->include_dirs[fe->dir_index - 1];
10477 if (!decode_for_pst_p)
10478 {
10479 last_subfile = current_subfile;
10480 dwarf2_start_subfile (fe->name, dir, comp_dir);
10481 }
10482 }
10483 }
10484 break;
10485 case DW_LNS_set_column:
10486 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10487 line_ptr += bytes_read;
10488 break;
10489 case DW_LNS_negate_stmt:
10490 is_stmt = (!is_stmt);
10491 break;
10492 case DW_LNS_set_basic_block:
10493 basic_block = 1;
10494 break;
10495 /* Add to the address register of the state machine the
10496 address increment value corresponding to special opcode
10497 255. I.e., this value is scaled by the minimum
10498 instruction length since special opcode 255 would have
10499 scaled the the increment. */
10500 case DW_LNS_const_add_pc:
10501 {
10502 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10503
10504 address += (((op_index + adjust)
10505 / lh->maximum_ops_per_instruction)
10506 * lh->minimum_instruction_length);
10507 op_index = ((op_index + adjust)
10508 % lh->maximum_ops_per_instruction);
10509 }
10510 break;
10511 case DW_LNS_fixed_advance_pc:
10512 address += read_2_bytes (abfd, line_ptr);
10513 op_index = 0;
10514 line_ptr += 2;
10515 break;
10516 default:
10517 {
10518 /* Unknown standard opcode, ignore it. */
10519 int i;
10520
10521 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10522 {
10523 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10524 line_ptr += bytes_read;
10525 }
10526 }
10527 }
10528 }
10529 if (lh->num_file_names < file || file == 0)
10530 dwarf2_debug_line_missing_file_complaint ();
10531 else
10532 {
10533 lh->file_names[file - 1].included_p = 1;
10534 if (!decode_for_pst_p)
10535 {
10536 addr = gdbarch_addr_bits_remove (gdbarch, address);
10537 record_line (current_subfile, 0, addr);
10538 }
10539 }
10540 }
10541
10542 if (decode_for_pst_p)
10543 {
10544 int file_index;
10545
10546 /* Now that we're done scanning the Line Header Program, we can
10547 create the psymtab of each included file. */
10548 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10549 if (lh->file_names[file_index].included_p == 1)
10550 {
10551 char *include_name =
10552 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10553 if (include_name != NULL)
10554 dwarf2_create_include_psymtab (include_name, pst, objfile);
10555 }
10556 }
10557 else
10558 {
10559 /* Make sure a symtab is created for every file, even files
10560 which contain only variables (i.e. no code with associated
10561 line numbers). */
10562
10563 int i;
10564 struct file_entry *fe;
10565
10566 for (i = 0; i < lh->num_file_names; i++)
10567 {
10568 char *dir = NULL;
10569
10570 fe = &lh->file_names[i];
10571 if (fe->dir_index)
10572 dir = lh->include_dirs[fe->dir_index - 1];
10573 dwarf2_start_subfile (fe->name, dir, comp_dir);
10574
10575 /* Skip the main file; we don't need it, and it must be
10576 allocated last, so that it will show up before the
10577 non-primary symtabs in the objfile's symtab list. */
10578 if (current_subfile == first_subfile)
10579 continue;
10580
10581 if (current_subfile->symtab == NULL)
10582 current_subfile->symtab = allocate_symtab (current_subfile->name,
10583 cu->objfile);
10584 fe->symtab = current_subfile->symtab;
10585 }
10586 }
10587 }
10588
10589 /* Start a subfile for DWARF. FILENAME is the name of the file and
10590 DIRNAME the name of the source directory which contains FILENAME
10591 or NULL if not known. COMP_DIR is the compilation directory for the
10592 linetable's compilation unit or NULL if not known.
10593 This routine tries to keep line numbers from identical absolute and
10594 relative file names in a common subfile.
10595
10596 Using the `list' example from the GDB testsuite, which resides in
10597 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10598 of /srcdir/list0.c yields the following debugging information for list0.c:
10599
10600 DW_AT_name: /srcdir/list0.c
10601 DW_AT_comp_dir: /compdir
10602 files.files[0].name: list0.h
10603 files.files[0].dir: /srcdir
10604 files.files[1].name: list0.c
10605 files.files[1].dir: /srcdir
10606
10607 The line number information for list0.c has to end up in a single
10608 subfile, so that `break /srcdir/list0.c:1' works as expected.
10609 start_subfile will ensure that this happens provided that we pass the
10610 concatenation of files.files[1].dir and files.files[1].name as the
10611 subfile's name. */
10612
10613 static void
10614 dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
10615 {
10616 char *fullname;
10617
10618 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10619 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10620 second argument to start_subfile. To be consistent, we do the
10621 same here. In order not to lose the line information directory,
10622 we concatenate it to the filename when it makes sense.
10623 Note that the Dwarf3 standard says (speaking of filenames in line
10624 information): ``The directory index is ignored for file names
10625 that represent full path names''. Thus ignoring dirname in the
10626 `else' branch below isn't an issue. */
10627
10628 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10629 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10630 else
10631 fullname = filename;
10632
10633 start_subfile (fullname, comp_dir);
10634
10635 if (fullname != filename)
10636 xfree (fullname);
10637 }
10638
10639 static void
10640 var_decode_location (struct attribute *attr, struct symbol *sym,
10641 struct dwarf2_cu *cu)
10642 {
10643 struct objfile *objfile = cu->objfile;
10644 struct comp_unit_head *cu_header = &cu->header;
10645
10646 /* NOTE drow/2003-01-30: There used to be a comment and some special
10647 code here to turn a symbol with DW_AT_external and a
10648 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10649 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10650 with some versions of binutils) where shared libraries could have
10651 relocations against symbols in their debug information - the
10652 minimal symbol would have the right address, but the debug info
10653 would not. It's no longer necessary, because we will explicitly
10654 apply relocations when we read in the debug information now. */
10655
10656 /* A DW_AT_location attribute with no contents indicates that a
10657 variable has been optimized away. */
10658 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10659 {
10660 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10661 return;
10662 }
10663
10664 /* Handle one degenerate form of location expression specially, to
10665 preserve GDB's previous behavior when section offsets are
10666 specified. If this is just a DW_OP_addr then mark this symbol
10667 as LOC_STATIC. */
10668
10669 if (attr_form_is_block (attr)
10670 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10671 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10672 {
10673 unsigned int dummy;
10674
10675 SYMBOL_VALUE_ADDRESS (sym) =
10676 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10677 SYMBOL_CLASS (sym) = LOC_STATIC;
10678 fixup_symbol_section (sym, objfile);
10679 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10680 SYMBOL_SECTION (sym));
10681 return;
10682 }
10683
10684 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10685 expression evaluator, and use LOC_COMPUTED only when necessary
10686 (i.e. when the value of a register or memory location is
10687 referenced, or a thread-local block, etc.). Then again, it might
10688 not be worthwhile. I'm assuming that it isn't unless performance
10689 or memory numbers show me otherwise. */
10690
10691 dwarf2_symbol_mark_computed (attr, sym, cu);
10692 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10693 }
10694
10695 /* Given a pointer to a DWARF information entry, figure out if we need
10696 to make a symbol table entry for it, and if so, create a new entry
10697 and return a pointer to it.
10698 If TYPE is NULL, determine symbol type from the die, otherwise
10699 used the passed type.
10700 If SPACE is not NULL, use it to hold the new symbol. If it is
10701 NULL, allocate a new symbol on the objfile's obstack. */
10702
10703 static struct symbol *
10704 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10705 struct symbol *space)
10706 {
10707 struct objfile *objfile = cu->objfile;
10708 struct symbol *sym = NULL;
10709 char *name;
10710 struct attribute *attr = NULL;
10711 struct attribute *attr2 = NULL;
10712 CORE_ADDR baseaddr;
10713 struct pending **list_to_add = NULL;
10714
10715 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10716
10717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10718
10719 name = dwarf2_name (die, cu);
10720 if (name)
10721 {
10722 const char *linkagename;
10723 int suppress_add = 0;
10724
10725 if (space)
10726 sym = space;
10727 else
10728 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10729 OBJSTAT (objfile, n_syms++);
10730
10731 /* Cache this symbol's name and the name's demangled form (if any). */
10732 SYMBOL_SET_LANGUAGE (sym, cu->language);
10733 linkagename = dwarf2_physname (name, die, cu);
10734 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10735
10736 /* Fortran does not have mangling standard and the mangling does differ
10737 between gfortran, iFort etc. */
10738 if (cu->language == language_fortran
10739 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10740 symbol_set_demangled_name (&(sym->ginfo),
10741 (char *) dwarf2_full_name (name, die, cu),
10742 NULL);
10743
10744 /* Default assumptions.
10745 Use the passed type or decode it from the die. */
10746 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10747 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10748 if (type != NULL)
10749 SYMBOL_TYPE (sym) = type;
10750 else
10751 SYMBOL_TYPE (sym) = die_type (die, cu);
10752 attr = dwarf2_attr (die,
10753 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10754 cu);
10755 if (attr)
10756 {
10757 SYMBOL_LINE (sym) = DW_UNSND (attr);
10758 }
10759
10760 attr = dwarf2_attr (die,
10761 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10762 cu);
10763 if (attr)
10764 {
10765 int file_index = DW_UNSND (attr);
10766
10767 if (cu->line_header == NULL
10768 || file_index > cu->line_header->num_file_names)
10769 complaint (&symfile_complaints,
10770 _("file index out of range"));
10771 else if (file_index > 0)
10772 {
10773 struct file_entry *fe;
10774
10775 fe = &cu->line_header->file_names[file_index - 1];
10776 SYMBOL_SYMTAB (sym) = fe->symtab;
10777 }
10778 }
10779
10780 switch (die->tag)
10781 {
10782 case DW_TAG_label:
10783 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10784 if (attr)
10785 {
10786 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10787 }
10788 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10789 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10790 SYMBOL_CLASS (sym) = LOC_LABEL;
10791 add_symbol_to_list (sym, cu->list_in_scope);
10792 break;
10793 case DW_TAG_subprogram:
10794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10795 finish_block. */
10796 SYMBOL_CLASS (sym) = LOC_BLOCK;
10797 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10798 if ((attr2 && (DW_UNSND (attr2) != 0))
10799 || cu->language == language_ada)
10800 {
10801 /* Subprograms marked external are stored as a global symbol.
10802 Ada subprograms, whether marked external or not, are always
10803 stored as a global symbol, because we want to be able to
10804 access them globally. For instance, we want to be able
10805 to break on a nested subprogram without having to
10806 specify the context. */
10807 list_to_add = &global_symbols;
10808 }
10809 else
10810 {
10811 list_to_add = cu->list_in_scope;
10812 }
10813 break;
10814 case DW_TAG_inlined_subroutine:
10815 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10816 finish_block. */
10817 SYMBOL_CLASS (sym) = LOC_BLOCK;
10818 SYMBOL_INLINED (sym) = 1;
10819 /* Do not add the symbol to any lists. It will be found via
10820 BLOCK_FUNCTION from the blockvector. */
10821 break;
10822 case DW_TAG_template_value_param:
10823 suppress_add = 1;
10824 /* Fall through. */
10825 case DW_TAG_constant:
10826 case DW_TAG_variable:
10827 case DW_TAG_member:
10828 /* Compilation with minimal debug info may result in variables
10829 with missing type entries. Change the misleading `void' type
10830 to something sensible. */
10831 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10832 SYMBOL_TYPE (sym)
10833 = objfile_type (objfile)->nodebug_data_symbol;
10834
10835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10836 /* In the case of DW_TAG_member, we should only be called for
10837 static const members. */
10838 if (die->tag == DW_TAG_member)
10839 {
10840 /* dwarf2_add_field uses die_is_declaration,
10841 so we do the same. */
10842 gdb_assert (die_is_declaration (die, cu));
10843 gdb_assert (attr);
10844 }
10845 if (attr)
10846 {
10847 dwarf2_const_value (attr, sym, cu);
10848 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10849 if (!suppress_add)
10850 {
10851 if (attr2 && (DW_UNSND (attr2) != 0))
10852 list_to_add = &global_symbols;
10853 else
10854 list_to_add = cu->list_in_scope;
10855 }
10856 break;
10857 }
10858 attr = dwarf2_attr (die, DW_AT_location, cu);
10859 if (attr)
10860 {
10861 var_decode_location (attr, sym, cu);
10862 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10863 if (SYMBOL_CLASS (sym) == LOC_STATIC
10864 && SYMBOL_VALUE_ADDRESS (sym) == 0
10865 && !dwarf2_per_objfile->has_section_at_zero)
10866 {
10867 /* When a static variable is eliminated by the linker,
10868 the corresponding debug information is not stripped
10869 out, but the variable address is set to null;
10870 do not add such variables into symbol table. */
10871 }
10872 else if (attr2 && (DW_UNSND (attr2) != 0))
10873 {
10874 /* Workaround gfortran PR debug/40040 - it uses
10875 DW_AT_location for variables in -fPIC libraries which may
10876 get overriden by other libraries/executable and get
10877 a different address. Resolve it by the minimal symbol
10878 which may come from inferior's executable using copy
10879 relocation. Make this workaround only for gfortran as for
10880 other compilers GDB cannot guess the minimal symbol
10881 Fortran mangling kind. */
10882 if (cu->language == language_fortran && die->parent
10883 && die->parent->tag == DW_TAG_module
10884 && cu->producer
10885 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10886 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10887
10888 /* A variable with DW_AT_external is never static,
10889 but it may be block-scoped. */
10890 list_to_add = (cu->list_in_scope == &file_symbols
10891 ? &global_symbols : cu->list_in_scope);
10892 }
10893 else
10894 list_to_add = cu->list_in_scope;
10895 }
10896 else
10897 {
10898 /* We do not know the address of this symbol.
10899 If it is an external symbol and we have type information
10900 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10901 The address of the variable will then be determined from
10902 the minimal symbol table whenever the variable is
10903 referenced. */
10904 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10905 if (attr2 && (DW_UNSND (attr2) != 0)
10906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10907 {
10908 /* A variable with DW_AT_external is never static, but it
10909 may be block-scoped. */
10910 list_to_add = (cu->list_in_scope == &file_symbols
10911 ? &global_symbols : cu->list_in_scope);
10912
10913 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10914 }
10915 else if (!die_is_declaration (die, cu))
10916 {
10917 /* Use the default LOC_OPTIMIZED_OUT class. */
10918 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10919 if (!suppress_add)
10920 list_to_add = cu->list_in_scope;
10921 }
10922 }
10923 break;
10924 case DW_TAG_formal_parameter:
10925 /* If we are inside a function, mark this as an argument. If
10926 not, we might be looking at an argument to an inlined function
10927 when we do not have enough information to show inlined frames;
10928 pretend it's a local variable in that case so that the user can
10929 still see it. */
10930 if (context_stack_depth > 0
10931 && context_stack[context_stack_depth - 1].name != NULL)
10932 SYMBOL_IS_ARGUMENT (sym) = 1;
10933 attr = dwarf2_attr (die, DW_AT_location, cu);
10934 if (attr)
10935 {
10936 var_decode_location (attr, sym, cu);
10937 }
10938 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10939 if (attr)
10940 {
10941 dwarf2_const_value (attr, sym, cu);
10942 }
10943 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10944 if (attr && DW_UNSND (attr))
10945 {
10946 struct type *ref_type;
10947
10948 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10949 SYMBOL_TYPE (sym) = ref_type;
10950 }
10951
10952 list_to_add = cu->list_in_scope;
10953 break;
10954 case DW_TAG_unspecified_parameters:
10955 /* From varargs functions; gdb doesn't seem to have any
10956 interest in this information, so just ignore it for now.
10957 (FIXME?) */
10958 break;
10959 case DW_TAG_template_type_param:
10960 suppress_add = 1;
10961 /* Fall through. */
10962 case DW_TAG_class_type:
10963 case DW_TAG_interface_type:
10964 case DW_TAG_structure_type:
10965 case DW_TAG_union_type:
10966 case DW_TAG_set_type:
10967 case DW_TAG_enumeration_type:
10968 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10969 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10970
10971 {
10972 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10973 really ever be static objects: otherwise, if you try
10974 to, say, break of a class's method and you're in a file
10975 which doesn't mention that class, it won't work unless
10976 the check for all static symbols in lookup_symbol_aux
10977 saves you. See the OtherFileClass tests in
10978 gdb.c++/namespace.exp. */
10979
10980 if (!suppress_add)
10981 {
10982 list_to_add = (cu->list_in_scope == &file_symbols
10983 && (cu->language == language_cplus
10984 || cu->language == language_java)
10985 ? &global_symbols : cu->list_in_scope);
10986
10987 /* The semantics of C++ state that "struct foo {
10988 ... }" also defines a typedef for "foo". A Java
10989 class declaration also defines a typedef for the
10990 class. */
10991 if (cu->language == language_cplus
10992 || cu->language == language_java
10993 || cu->language == language_ada)
10994 {
10995 /* The symbol's name is already allocated along
10996 with this objfile, so we don't need to
10997 duplicate it for the type. */
10998 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10999 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11000 }
11001 }
11002 }
11003 break;
11004 case DW_TAG_typedef:
11005 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11006 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11007 list_to_add = cu->list_in_scope;
11008 break;
11009 case DW_TAG_base_type:
11010 case DW_TAG_subrange_type:
11011 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11012 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11013 list_to_add = cu->list_in_scope;
11014 break;
11015 case DW_TAG_enumerator:
11016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11017 if (attr)
11018 {
11019 dwarf2_const_value (attr, sym, cu);
11020 }
11021 {
11022 /* NOTE: carlton/2003-11-10: See comment above in the
11023 DW_TAG_class_type, etc. block. */
11024
11025 list_to_add = (cu->list_in_scope == &file_symbols
11026 && (cu->language == language_cplus
11027 || cu->language == language_java)
11028 ? &global_symbols : cu->list_in_scope);
11029 }
11030 break;
11031 case DW_TAG_namespace:
11032 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11033 list_to_add = &global_symbols;
11034 break;
11035 default:
11036 /* Not a tag we recognize. Hopefully we aren't processing
11037 trash data, but since we must specifically ignore things
11038 we don't recognize, there is nothing else we should do at
11039 this point. */
11040 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11041 dwarf_tag_name (die->tag));
11042 break;
11043 }
11044
11045 if (suppress_add)
11046 {
11047 sym->hash_next = objfile->template_symbols;
11048 objfile->template_symbols = sym;
11049 list_to_add = NULL;
11050 }
11051
11052 if (list_to_add != NULL)
11053 add_symbol_to_list (sym, list_to_add);
11054
11055 /* For the benefit of old versions of GCC, check for anonymous
11056 namespaces based on the demangled name. */
11057 if (!processing_has_namespace_info
11058 && cu->language == language_cplus)
11059 cp_scan_for_anonymous_namespaces (sym);
11060 }
11061 return (sym);
11062 }
11063
11064 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11065
11066 static struct symbol *
11067 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11068 {
11069 return new_symbol_full (die, type, cu, NULL);
11070 }
11071
11072 /* Given an attr with a DW_FORM_dataN value in host byte order,
11073 zero-extend it as appropriate for the symbol's type. The DWARF
11074 standard (v4) is not entirely clear about the meaning of using
11075 DW_FORM_dataN for a constant with a signed type, where the type is
11076 wider than the data. The conclusion of a discussion on the DWARF
11077 list was that this is unspecified. We choose to always zero-extend
11078 because that is the interpretation long in use by GCC. */
11079
11080 static gdb_byte *
11081 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11082 const char *name, struct obstack *obstack,
11083 struct dwarf2_cu *cu, long *value, int bits)
11084 {
11085 struct objfile *objfile = cu->objfile;
11086 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11088 LONGEST l = DW_UNSND (attr);
11089
11090 if (bits < sizeof (*value) * 8)
11091 {
11092 l &= ((LONGEST) 1 << bits) - 1;
11093 *value = l;
11094 }
11095 else if (bits == sizeof (*value) * 8)
11096 *value = l;
11097 else
11098 {
11099 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11100 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11101 return bytes;
11102 }
11103
11104 return NULL;
11105 }
11106
11107 /* Read a constant value from an attribute. Either set *VALUE, or if
11108 the value does not fit in *VALUE, set *BYTES - either already
11109 allocated on the objfile obstack, or newly allocated on OBSTACK,
11110 or, set *BATON, if we translated the constant to a location
11111 expression. */
11112
11113 static void
11114 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11115 const char *name, struct obstack *obstack,
11116 struct dwarf2_cu *cu,
11117 long *value, gdb_byte **bytes,
11118 struct dwarf2_locexpr_baton **baton)
11119 {
11120 struct objfile *objfile = cu->objfile;
11121 struct comp_unit_head *cu_header = &cu->header;
11122 struct dwarf_block *blk;
11123 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11124 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11125
11126 *value = 0;
11127 *bytes = NULL;
11128 *baton = NULL;
11129
11130 switch (attr->form)
11131 {
11132 case DW_FORM_addr:
11133 {
11134 gdb_byte *data;
11135
11136 if (TYPE_LENGTH (type) != cu_header->addr_size)
11137 dwarf2_const_value_length_mismatch_complaint (name,
11138 cu_header->addr_size,
11139 TYPE_LENGTH (type));
11140 /* Symbols of this form are reasonably rare, so we just
11141 piggyback on the existing location code rather than writing
11142 a new implementation of symbol_computed_ops. */
11143 *baton = obstack_alloc (&objfile->objfile_obstack,
11144 sizeof (struct dwarf2_locexpr_baton));
11145 (*baton)->per_cu = cu->per_cu;
11146 gdb_assert ((*baton)->per_cu);
11147
11148 (*baton)->size = 2 + cu_header->addr_size;
11149 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11150 (*baton)->data = data;
11151
11152 data[0] = DW_OP_addr;
11153 store_unsigned_integer (&data[1], cu_header->addr_size,
11154 byte_order, DW_ADDR (attr));
11155 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11156 }
11157 break;
11158 case DW_FORM_string:
11159 case DW_FORM_strp:
11160 /* DW_STRING is already allocated on the objfile obstack, point
11161 directly to it. */
11162 *bytes = (gdb_byte *) DW_STRING (attr);
11163 break;
11164 case DW_FORM_block1:
11165 case DW_FORM_block2:
11166 case DW_FORM_block4:
11167 case DW_FORM_block:
11168 case DW_FORM_exprloc:
11169 blk = DW_BLOCK (attr);
11170 if (TYPE_LENGTH (type) != blk->size)
11171 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11172 TYPE_LENGTH (type));
11173 *bytes = blk->data;
11174 break;
11175
11176 /* The DW_AT_const_value attributes are supposed to carry the
11177 symbol's value "represented as it would be on the target
11178 architecture." By the time we get here, it's already been
11179 converted to host endianness, so we just need to sign- or
11180 zero-extend it as appropriate. */
11181 case DW_FORM_data1:
11182 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
11183 break;
11184 case DW_FORM_data2:
11185 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
11186 break;
11187 case DW_FORM_data4:
11188 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
11189 break;
11190 case DW_FORM_data8:
11191 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
11192 break;
11193
11194 case DW_FORM_sdata:
11195 *value = DW_SND (attr);
11196 break;
11197
11198 case DW_FORM_udata:
11199 *value = DW_UNSND (attr);
11200 break;
11201
11202 default:
11203 complaint (&symfile_complaints,
11204 _("unsupported const value attribute form: '%s'"),
11205 dwarf_form_name (attr->form));
11206 *value = 0;
11207 break;
11208 }
11209 }
11210
11211
11212 /* Copy constant value from an attribute to a symbol. */
11213
11214 static void
11215 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11216 struct dwarf2_cu *cu)
11217 {
11218 struct objfile *objfile = cu->objfile;
11219 struct comp_unit_head *cu_header = &cu->header;
11220 long value;
11221 gdb_byte *bytes;
11222 struct dwarf2_locexpr_baton *baton;
11223
11224 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11225 SYMBOL_PRINT_NAME (sym),
11226 &objfile->objfile_obstack, cu,
11227 &value, &bytes, &baton);
11228
11229 if (baton != NULL)
11230 {
11231 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11232 SYMBOL_LOCATION_BATON (sym) = baton;
11233 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11234 }
11235 else if (bytes != NULL)
11236 {
11237 SYMBOL_VALUE_BYTES (sym) = bytes;
11238 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11239 }
11240 else
11241 {
11242 SYMBOL_VALUE (sym) = value;
11243 SYMBOL_CLASS (sym) = LOC_CONST;
11244 }
11245 }
11246
11247 /* Return the type of the die in question using its DW_AT_type attribute. */
11248
11249 static struct type *
11250 die_type (struct die_info *die, struct dwarf2_cu *cu)
11251 {
11252 struct attribute *type_attr;
11253
11254 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11255 if (!type_attr)
11256 {
11257 /* A missing DW_AT_type represents a void type. */
11258 return objfile_type (cu->objfile)->builtin_void;
11259 }
11260
11261 return lookup_die_type (die, type_attr, cu);
11262 }
11263
11264 /* True iff CU's producer generates GNAT Ada auxiliary information
11265 that allows to find parallel types through that information instead
11266 of having to do expensive parallel lookups by type name. */
11267
11268 static int
11269 need_gnat_info (struct dwarf2_cu *cu)
11270 {
11271 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11272 of GNAT produces this auxiliary information, without any indication
11273 that it is produced. Part of enhancing the FSF version of GNAT
11274 to produce that information will be to put in place an indicator
11275 that we can use in order to determine whether the descriptive type
11276 info is available or not. One suggestion that has been made is
11277 to use a new attribute, attached to the CU die. For now, assume
11278 that the descriptive type info is not available. */
11279 return 0;
11280 }
11281
11282 /* Return the auxiliary type of the die in question using its
11283 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11284 attribute is not present. */
11285
11286 static struct type *
11287 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11288 {
11289 struct attribute *type_attr;
11290
11291 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11292 if (!type_attr)
11293 return NULL;
11294
11295 return lookup_die_type (die, type_attr, cu);
11296 }
11297
11298 /* If DIE has a descriptive_type attribute, then set the TYPE's
11299 descriptive type accordingly. */
11300
11301 static void
11302 set_descriptive_type (struct type *type, struct die_info *die,
11303 struct dwarf2_cu *cu)
11304 {
11305 struct type *descriptive_type = die_descriptive_type (die, cu);
11306
11307 if (descriptive_type)
11308 {
11309 ALLOCATE_GNAT_AUX_TYPE (type);
11310 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11311 }
11312 }
11313
11314 /* Return the containing type of the die in question using its
11315 DW_AT_containing_type attribute. */
11316
11317 static struct type *
11318 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11319 {
11320 struct attribute *type_attr;
11321
11322 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11323 if (!type_attr)
11324 error (_("Dwarf Error: Problem turning containing type into gdb type "
11325 "[in module %s]"), cu->objfile->name);
11326
11327 return lookup_die_type (die, type_attr, cu);
11328 }
11329
11330 /* Look up the type of DIE in CU using its type attribute ATTR.
11331 If there is no type substitute an error marker. */
11332
11333 static struct type *
11334 lookup_die_type (struct die_info *die, struct attribute *attr,
11335 struct dwarf2_cu *cu)
11336 {
11337 struct type *this_type;
11338
11339 /* First see if we have it cached. */
11340
11341 if (is_ref_attr (attr))
11342 {
11343 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11344
11345 this_type = get_die_type_at_offset (offset, cu->per_cu);
11346 }
11347 else if (attr->form == DW_FORM_sig8)
11348 {
11349 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11350 struct dwarf2_cu *sig_cu;
11351 unsigned int offset;
11352
11353 /* sig_type will be NULL if the signatured type is missing from
11354 the debug info. */
11355 if (sig_type == NULL)
11356 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11357 "at 0x%x [in module %s]"),
11358 die->offset, cu->objfile->name);
11359
11360 gdb_assert (sig_type->per_cu.from_debug_types);
11361 offset = sig_type->offset + sig_type->type_offset;
11362 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11363 }
11364 else
11365 {
11366 dump_die_for_error (die);
11367 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11368 dwarf_attr_name (attr->name), cu->objfile->name);
11369 }
11370
11371 /* If not cached we need to read it in. */
11372
11373 if (this_type == NULL)
11374 {
11375 struct die_info *type_die;
11376 struct dwarf2_cu *type_cu = cu;
11377
11378 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11379 /* If the type is cached, we should have found it above. */
11380 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11381 this_type = read_type_die_1 (type_die, type_cu);
11382 }
11383
11384 /* If we still don't have a type use an error marker. */
11385
11386 if (this_type == NULL)
11387 {
11388 char *message, *saved;
11389
11390 /* read_type_die already issued a complaint. */
11391 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11392 cu->objfile->name,
11393 cu->header.offset,
11394 die->offset);
11395 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11396 message, strlen (message));
11397 xfree (message);
11398
11399 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11400 }
11401
11402 return this_type;
11403 }
11404
11405 /* Return the type in DIE, CU.
11406 Returns NULL for invalid types.
11407
11408 This first does a lookup in the appropriate type_hash table,
11409 and only reads the die in if necessary.
11410
11411 NOTE: This can be called when reading in partial or full symbols. */
11412
11413 static struct type *
11414 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11415 {
11416 struct type *this_type;
11417
11418 this_type = get_die_type (die, cu);
11419 if (this_type)
11420 return this_type;
11421
11422 return read_type_die_1 (die, cu);
11423 }
11424
11425 /* Read the type in DIE, CU.
11426 Returns NULL for invalid types. */
11427
11428 static struct type *
11429 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11430 {
11431 struct type *this_type = NULL;
11432
11433 switch (die->tag)
11434 {
11435 case DW_TAG_class_type:
11436 case DW_TAG_interface_type:
11437 case DW_TAG_structure_type:
11438 case DW_TAG_union_type:
11439 this_type = read_structure_type (die, cu);
11440 break;
11441 case DW_TAG_enumeration_type:
11442 this_type = read_enumeration_type (die, cu);
11443 break;
11444 case DW_TAG_subprogram:
11445 case DW_TAG_subroutine_type:
11446 case DW_TAG_inlined_subroutine:
11447 this_type = read_subroutine_type (die, cu);
11448 break;
11449 case DW_TAG_array_type:
11450 this_type = read_array_type (die, cu);
11451 break;
11452 case DW_TAG_set_type:
11453 this_type = read_set_type (die, cu);
11454 break;
11455 case DW_TAG_pointer_type:
11456 this_type = read_tag_pointer_type (die, cu);
11457 break;
11458 case DW_TAG_ptr_to_member_type:
11459 this_type = read_tag_ptr_to_member_type (die, cu);
11460 break;
11461 case DW_TAG_reference_type:
11462 this_type = read_tag_reference_type (die, cu);
11463 break;
11464 case DW_TAG_const_type:
11465 this_type = read_tag_const_type (die, cu);
11466 break;
11467 case DW_TAG_volatile_type:
11468 this_type = read_tag_volatile_type (die, cu);
11469 break;
11470 case DW_TAG_string_type:
11471 this_type = read_tag_string_type (die, cu);
11472 break;
11473 case DW_TAG_typedef:
11474 this_type = read_typedef (die, cu);
11475 break;
11476 case DW_TAG_subrange_type:
11477 this_type = read_subrange_type (die, cu);
11478 break;
11479 case DW_TAG_base_type:
11480 this_type = read_base_type (die, cu);
11481 break;
11482 case DW_TAG_unspecified_type:
11483 this_type = read_unspecified_type (die, cu);
11484 break;
11485 case DW_TAG_namespace:
11486 this_type = read_namespace_type (die, cu);
11487 break;
11488 case DW_TAG_module:
11489 this_type = read_module_type (die, cu);
11490 break;
11491 default:
11492 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11493 dwarf_tag_name (die->tag));
11494 break;
11495 }
11496
11497 return this_type;
11498 }
11499
11500 /* See if we can figure out if the class lives in a namespace. We do
11501 this by looking for a member function; its demangled name will
11502 contain namespace info, if there is any.
11503 Return the computed name or NULL.
11504 Space for the result is allocated on the objfile's obstack.
11505 This is the full-die version of guess_partial_die_structure_name.
11506 In this case we know DIE has no useful parent. */
11507
11508 static char *
11509 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11510 {
11511 struct die_info *spec_die;
11512 struct dwarf2_cu *spec_cu;
11513 struct die_info *child;
11514
11515 spec_cu = cu;
11516 spec_die = die_specification (die, &spec_cu);
11517 if (spec_die != NULL)
11518 {
11519 die = spec_die;
11520 cu = spec_cu;
11521 }
11522
11523 for (child = die->child;
11524 child != NULL;
11525 child = child->sibling)
11526 {
11527 if (child->tag == DW_TAG_subprogram)
11528 {
11529 struct attribute *attr;
11530
11531 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11532 if (attr == NULL)
11533 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11534 if (attr != NULL)
11535 {
11536 char *actual_name
11537 = language_class_name_from_physname (cu->language_defn,
11538 DW_STRING (attr));
11539 char *name = NULL;
11540
11541 if (actual_name != NULL)
11542 {
11543 char *die_name = dwarf2_name (die, cu);
11544
11545 if (die_name != NULL
11546 && strcmp (die_name, actual_name) != 0)
11547 {
11548 /* Strip off the class name from the full name.
11549 We want the prefix. */
11550 int die_name_len = strlen (die_name);
11551 int actual_name_len = strlen (actual_name);
11552
11553 /* Test for '::' as a sanity check. */
11554 if (actual_name_len > die_name_len + 2
11555 && actual_name[actual_name_len - die_name_len - 1] == ':')
11556 name =
11557 obsavestring (actual_name,
11558 actual_name_len - die_name_len - 2,
11559 &cu->objfile->objfile_obstack);
11560 }
11561 }
11562 xfree (actual_name);
11563 return name;
11564 }
11565 }
11566 }
11567
11568 return NULL;
11569 }
11570
11571 /* Return the name of the namespace/class that DIE is defined within,
11572 or "" if we can't tell. The caller should not xfree the result.
11573
11574 For example, if we're within the method foo() in the following
11575 code:
11576
11577 namespace N {
11578 class C {
11579 void foo () {
11580 }
11581 };
11582 }
11583
11584 then determine_prefix on foo's die will return "N::C". */
11585
11586 static char *
11587 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11588 {
11589 struct die_info *parent, *spec_die;
11590 struct dwarf2_cu *spec_cu;
11591 struct type *parent_type;
11592
11593 if (cu->language != language_cplus && cu->language != language_java
11594 && cu->language != language_fortran)
11595 return "";
11596
11597 /* We have to be careful in the presence of DW_AT_specification.
11598 For example, with GCC 3.4, given the code
11599
11600 namespace N {
11601 void foo() {
11602 // Definition of N::foo.
11603 }
11604 }
11605
11606 then we'll have a tree of DIEs like this:
11607
11608 1: DW_TAG_compile_unit
11609 2: DW_TAG_namespace // N
11610 3: DW_TAG_subprogram // declaration of N::foo
11611 4: DW_TAG_subprogram // definition of N::foo
11612 DW_AT_specification // refers to die #3
11613
11614 Thus, when processing die #4, we have to pretend that we're in
11615 the context of its DW_AT_specification, namely the contex of die
11616 #3. */
11617 spec_cu = cu;
11618 spec_die = die_specification (die, &spec_cu);
11619 if (spec_die == NULL)
11620 parent = die->parent;
11621 else
11622 {
11623 parent = spec_die->parent;
11624 cu = spec_cu;
11625 }
11626
11627 if (parent == NULL)
11628 return "";
11629 else if (parent->building_fullname)
11630 {
11631 const char *name;
11632 const char *parent_name;
11633
11634 /* It has been seen on RealView 2.2 built binaries,
11635 DW_TAG_template_type_param types actually _defined_ as
11636 children of the parent class:
11637
11638 enum E {};
11639 template class <class Enum> Class{};
11640 Class<enum E> class_e;
11641
11642 1: DW_TAG_class_type (Class)
11643 2: DW_TAG_enumeration_type (E)
11644 3: DW_TAG_enumerator (enum1:0)
11645 3: DW_TAG_enumerator (enum2:1)
11646 ...
11647 2: DW_TAG_template_type_param
11648 DW_AT_type DW_FORM_ref_udata (E)
11649
11650 Besides being broken debug info, it can put GDB into an
11651 infinite loop. Consider:
11652
11653 When we're building the full name for Class<E>, we'll start
11654 at Class, and go look over its template type parameters,
11655 finding E. We'll then try to build the full name of E, and
11656 reach here. We're now trying to build the full name of E,
11657 and look over the parent DIE for containing scope. In the
11658 broken case, if we followed the parent DIE of E, we'd again
11659 find Class, and once again go look at its template type
11660 arguments, etc., etc. Simply don't consider such parent die
11661 as source-level parent of this die (it can't be, the language
11662 doesn't allow it), and break the loop here. */
11663 name = dwarf2_name (die, cu);
11664 parent_name = dwarf2_name (parent, cu);
11665 complaint (&symfile_complaints,
11666 _("template param type '%s' defined within parent '%s'"),
11667 name ? name : "<unknown>",
11668 parent_name ? parent_name : "<unknown>");
11669 return "";
11670 }
11671 else
11672 switch (parent->tag)
11673 {
11674 case DW_TAG_namespace:
11675 parent_type = read_type_die (parent, cu);
11676 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11677 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11678 Work around this problem here. */
11679 if (cu->language == language_cplus
11680 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11681 return "";
11682 /* We give a name to even anonymous namespaces. */
11683 return TYPE_TAG_NAME (parent_type);
11684 case DW_TAG_class_type:
11685 case DW_TAG_interface_type:
11686 case DW_TAG_structure_type:
11687 case DW_TAG_union_type:
11688 case DW_TAG_module:
11689 parent_type = read_type_die (parent, cu);
11690 if (TYPE_TAG_NAME (parent_type) != NULL)
11691 return TYPE_TAG_NAME (parent_type);
11692 else
11693 /* An anonymous structure is only allowed non-static data
11694 members; no typedefs, no member functions, et cetera.
11695 So it does not need a prefix. */
11696 return "";
11697 case DW_TAG_compile_unit:
11698 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11699 if (cu->language == language_cplus
11700 && dwarf2_per_objfile->types.asection != NULL
11701 && die->child != NULL
11702 && (die->tag == DW_TAG_class_type
11703 || die->tag == DW_TAG_structure_type
11704 || die->tag == DW_TAG_union_type))
11705 {
11706 char *name = guess_full_die_structure_name (die, cu);
11707 if (name != NULL)
11708 return name;
11709 }
11710 return "";
11711 default:
11712 return determine_prefix (parent, cu);
11713 }
11714 }
11715
11716 /* Return a newly-allocated string formed by concatenating PREFIX and
11717 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11718 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11719 perform an obconcat, otherwise allocate storage for the result. The CU argument
11720 is used to determine the language and hence, the appropriate separator. */
11721
11722 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11723
11724 static char *
11725 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11726 int physname, struct dwarf2_cu *cu)
11727 {
11728 const char *lead = "";
11729 const char *sep;
11730
11731 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11732 sep = "";
11733 else if (cu->language == language_java)
11734 sep = ".";
11735 else if (cu->language == language_fortran && physname)
11736 {
11737 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11738 DW_AT_MIPS_linkage_name is preferred and used instead. */
11739
11740 lead = "__";
11741 sep = "_MOD_";
11742 }
11743 else
11744 sep = "::";
11745
11746 if (prefix == NULL)
11747 prefix = "";
11748 if (suffix == NULL)
11749 suffix = "";
11750
11751 if (obs == NULL)
11752 {
11753 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11754
11755 strcpy (retval, lead);
11756 strcat (retval, prefix);
11757 strcat (retval, sep);
11758 strcat (retval, suffix);
11759 return retval;
11760 }
11761 else
11762 {
11763 /* We have an obstack. */
11764 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11765 }
11766 }
11767
11768 /* Return sibling of die, NULL if no sibling. */
11769
11770 static struct die_info *
11771 sibling_die (struct die_info *die)
11772 {
11773 return die->sibling;
11774 }
11775
11776 /* Get name of a die, return NULL if not found. */
11777
11778 static char *
11779 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11780 struct obstack *obstack)
11781 {
11782 if (name && cu->language == language_cplus)
11783 {
11784 char *canon_name = cp_canonicalize_string (name);
11785
11786 if (canon_name != NULL)
11787 {
11788 if (strcmp (canon_name, name) != 0)
11789 name = obsavestring (canon_name, strlen (canon_name),
11790 obstack);
11791 xfree (canon_name);
11792 }
11793 }
11794
11795 return name;
11796 }
11797
11798 /* Get name of a die, return NULL if not found. */
11799
11800 static char *
11801 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11802 {
11803 struct attribute *attr;
11804
11805 attr = dwarf2_attr (die, DW_AT_name, cu);
11806 if (!attr || !DW_STRING (attr))
11807 return NULL;
11808
11809 switch (die->tag)
11810 {
11811 case DW_TAG_compile_unit:
11812 /* Compilation units have a DW_AT_name that is a filename, not
11813 a source language identifier. */
11814 case DW_TAG_enumeration_type:
11815 case DW_TAG_enumerator:
11816 /* These tags always have simple identifiers already; no need
11817 to canonicalize them. */
11818 return DW_STRING (attr);
11819
11820 case DW_TAG_subprogram:
11821 /* Java constructors will all be named "<init>", so return
11822 the class name when we see this special case. */
11823 if (cu->language == language_java
11824 && DW_STRING (attr) != NULL
11825 && strcmp (DW_STRING (attr), "<init>") == 0)
11826 {
11827 struct dwarf2_cu *spec_cu = cu;
11828 struct die_info *spec_die;
11829
11830 /* GCJ will output '<init>' for Java constructor names.
11831 For this special case, return the name of the parent class. */
11832
11833 /* GCJ may output suprogram DIEs with AT_specification set.
11834 If so, use the name of the specified DIE. */
11835 spec_die = die_specification (die, &spec_cu);
11836 if (spec_die != NULL)
11837 return dwarf2_name (spec_die, spec_cu);
11838
11839 do
11840 {
11841 die = die->parent;
11842 if (die->tag == DW_TAG_class_type)
11843 return dwarf2_name (die, cu);
11844 }
11845 while (die->tag != DW_TAG_compile_unit);
11846 }
11847 break;
11848
11849 case DW_TAG_class_type:
11850 case DW_TAG_interface_type:
11851 case DW_TAG_structure_type:
11852 case DW_TAG_union_type:
11853 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11854 structures or unions. These were of the form "._%d" in GCC 4.1,
11855 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11856 and GCC 4.4. We work around this problem by ignoring these. */
11857 if (strncmp (DW_STRING (attr), "._", 2) == 0
11858 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11859 return NULL;
11860 break;
11861
11862 default:
11863 break;
11864 }
11865
11866 if (!DW_STRING_IS_CANONICAL (attr))
11867 {
11868 DW_STRING (attr)
11869 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11870 &cu->objfile->objfile_obstack);
11871 DW_STRING_IS_CANONICAL (attr) = 1;
11872 }
11873 return DW_STRING (attr);
11874 }
11875
11876 /* Return the die that this die in an extension of, or NULL if there
11877 is none. *EXT_CU is the CU containing DIE on input, and the CU
11878 containing the return value on output. */
11879
11880 static struct die_info *
11881 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11882 {
11883 struct attribute *attr;
11884
11885 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11886 if (attr == NULL)
11887 return NULL;
11888
11889 return follow_die_ref (die, attr, ext_cu);
11890 }
11891
11892 /* Convert a DIE tag into its string name. */
11893
11894 static char *
11895 dwarf_tag_name (unsigned tag)
11896 {
11897 switch (tag)
11898 {
11899 case DW_TAG_padding:
11900 return "DW_TAG_padding";
11901 case DW_TAG_array_type:
11902 return "DW_TAG_array_type";
11903 case DW_TAG_class_type:
11904 return "DW_TAG_class_type";
11905 case DW_TAG_entry_point:
11906 return "DW_TAG_entry_point";
11907 case DW_TAG_enumeration_type:
11908 return "DW_TAG_enumeration_type";
11909 case DW_TAG_formal_parameter:
11910 return "DW_TAG_formal_parameter";
11911 case DW_TAG_imported_declaration:
11912 return "DW_TAG_imported_declaration";
11913 case DW_TAG_label:
11914 return "DW_TAG_label";
11915 case DW_TAG_lexical_block:
11916 return "DW_TAG_lexical_block";
11917 case DW_TAG_member:
11918 return "DW_TAG_member";
11919 case DW_TAG_pointer_type:
11920 return "DW_TAG_pointer_type";
11921 case DW_TAG_reference_type:
11922 return "DW_TAG_reference_type";
11923 case DW_TAG_compile_unit:
11924 return "DW_TAG_compile_unit";
11925 case DW_TAG_string_type:
11926 return "DW_TAG_string_type";
11927 case DW_TAG_structure_type:
11928 return "DW_TAG_structure_type";
11929 case DW_TAG_subroutine_type:
11930 return "DW_TAG_subroutine_type";
11931 case DW_TAG_typedef:
11932 return "DW_TAG_typedef";
11933 case DW_TAG_union_type:
11934 return "DW_TAG_union_type";
11935 case DW_TAG_unspecified_parameters:
11936 return "DW_TAG_unspecified_parameters";
11937 case DW_TAG_variant:
11938 return "DW_TAG_variant";
11939 case DW_TAG_common_block:
11940 return "DW_TAG_common_block";
11941 case DW_TAG_common_inclusion:
11942 return "DW_TAG_common_inclusion";
11943 case DW_TAG_inheritance:
11944 return "DW_TAG_inheritance";
11945 case DW_TAG_inlined_subroutine:
11946 return "DW_TAG_inlined_subroutine";
11947 case DW_TAG_module:
11948 return "DW_TAG_module";
11949 case DW_TAG_ptr_to_member_type:
11950 return "DW_TAG_ptr_to_member_type";
11951 case DW_TAG_set_type:
11952 return "DW_TAG_set_type";
11953 case DW_TAG_subrange_type:
11954 return "DW_TAG_subrange_type";
11955 case DW_TAG_with_stmt:
11956 return "DW_TAG_with_stmt";
11957 case DW_TAG_access_declaration:
11958 return "DW_TAG_access_declaration";
11959 case DW_TAG_base_type:
11960 return "DW_TAG_base_type";
11961 case DW_TAG_catch_block:
11962 return "DW_TAG_catch_block";
11963 case DW_TAG_const_type:
11964 return "DW_TAG_const_type";
11965 case DW_TAG_constant:
11966 return "DW_TAG_constant";
11967 case DW_TAG_enumerator:
11968 return "DW_TAG_enumerator";
11969 case DW_TAG_file_type:
11970 return "DW_TAG_file_type";
11971 case DW_TAG_friend:
11972 return "DW_TAG_friend";
11973 case DW_TAG_namelist:
11974 return "DW_TAG_namelist";
11975 case DW_TAG_namelist_item:
11976 return "DW_TAG_namelist_item";
11977 case DW_TAG_packed_type:
11978 return "DW_TAG_packed_type";
11979 case DW_TAG_subprogram:
11980 return "DW_TAG_subprogram";
11981 case DW_TAG_template_type_param:
11982 return "DW_TAG_template_type_param";
11983 case DW_TAG_template_value_param:
11984 return "DW_TAG_template_value_param";
11985 case DW_TAG_thrown_type:
11986 return "DW_TAG_thrown_type";
11987 case DW_TAG_try_block:
11988 return "DW_TAG_try_block";
11989 case DW_TAG_variant_part:
11990 return "DW_TAG_variant_part";
11991 case DW_TAG_variable:
11992 return "DW_TAG_variable";
11993 case DW_TAG_volatile_type:
11994 return "DW_TAG_volatile_type";
11995 case DW_TAG_dwarf_procedure:
11996 return "DW_TAG_dwarf_procedure";
11997 case DW_TAG_restrict_type:
11998 return "DW_TAG_restrict_type";
11999 case DW_TAG_interface_type:
12000 return "DW_TAG_interface_type";
12001 case DW_TAG_namespace:
12002 return "DW_TAG_namespace";
12003 case DW_TAG_imported_module:
12004 return "DW_TAG_imported_module";
12005 case DW_TAG_unspecified_type:
12006 return "DW_TAG_unspecified_type";
12007 case DW_TAG_partial_unit:
12008 return "DW_TAG_partial_unit";
12009 case DW_TAG_imported_unit:
12010 return "DW_TAG_imported_unit";
12011 case DW_TAG_condition:
12012 return "DW_TAG_condition";
12013 case DW_TAG_shared_type:
12014 return "DW_TAG_shared_type";
12015 case DW_TAG_type_unit:
12016 return "DW_TAG_type_unit";
12017 case DW_TAG_MIPS_loop:
12018 return "DW_TAG_MIPS_loop";
12019 case DW_TAG_HP_array_descriptor:
12020 return "DW_TAG_HP_array_descriptor";
12021 case DW_TAG_format_label:
12022 return "DW_TAG_format_label";
12023 case DW_TAG_function_template:
12024 return "DW_TAG_function_template";
12025 case DW_TAG_class_template:
12026 return "DW_TAG_class_template";
12027 case DW_TAG_GNU_BINCL:
12028 return "DW_TAG_GNU_BINCL";
12029 case DW_TAG_GNU_EINCL:
12030 return "DW_TAG_GNU_EINCL";
12031 case DW_TAG_upc_shared_type:
12032 return "DW_TAG_upc_shared_type";
12033 case DW_TAG_upc_strict_type:
12034 return "DW_TAG_upc_strict_type";
12035 case DW_TAG_upc_relaxed_type:
12036 return "DW_TAG_upc_relaxed_type";
12037 case DW_TAG_PGI_kanji_type:
12038 return "DW_TAG_PGI_kanji_type";
12039 case DW_TAG_PGI_interface_block:
12040 return "DW_TAG_PGI_interface_block";
12041 default:
12042 return "DW_TAG_<unknown>";
12043 }
12044 }
12045
12046 /* Convert a DWARF attribute code into its string name. */
12047
12048 static char *
12049 dwarf_attr_name (unsigned attr)
12050 {
12051 switch (attr)
12052 {
12053 case DW_AT_sibling:
12054 return "DW_AT_sibling";
12055 case DW_AT_location:
12056 return "DW_AT_location";
12057 case DW_AT_name:
12058 return "DW_AT_name";
12059 case DW_AT_ordering:
12060 return "DW_AT_ordering";
12061 case DW_AT_subscr_data:
12062 return "DW_AT_subscr_data";
12063 case DW_AT_byte_size:
12064 return "DW_AT_byte_size";
12065 case DW_AT_bit_offset:
12066 return "DW_AT_bit_offset";
12067 case DW_AT_bit_size:
12068 return "DW_AT_bit_size";
12069 case DW_AT_element_list:
12070 return "DW_AT_element_list";
12071 case DW_AT_stmt_list:
12072 return "DW_AT_stmt_list";
12073 case DW_AT_low_pc:
12074 return "DW_AT_low_pc";
12075 case DW_AT_high_pc:
12076 return "DW_AT_high_pc";
12077 case DW_AT_language:
12078 return "DW_AT_language";
12079 case DW_AT_member:
12080 return "DW_AT_member";
12081 case DW_AT_discr:
12082 return "DW_AT_discr";
12083 case DW_AT_discr_value:
12084 return "DW_AT_discr_value";
12085 case DW_AT_visibility:
12086 return "DW_AT_visibility";
12087 case DW_AT_import:
12088 return "DW_AT_import";
12089 case DW_AT_string_length:
12090 return "DW_AT_string_length";
12091 case DW_AT_common_reference:
12092 return "DW_AT_common_reference";
12093 case DW_AT_comp_dir:
12094 return "DW_AT_comp_dir";
12095 case DW_AT_const_value:
12096 return "DW_AT_const_value";
12097 case DW_AT_containing_type:
12098 return "DW_AT_containing_type";
12099 case DW_AT_default_value:
12100 return "DW_AT_default_value";
12101 case DW_AT_inline:
12102 return "DW_AT_inline";
12103 case DW_AT_is_optional:
12104 return "DW_AT_is_optional";
12105 case DW_AT_lower_bound:
12106 return "DW_AT_lower_bound";
12107 case DW_AT_producer:
12108 return "DW_AT_producer";
12109 case DW_AT_prototyped:
12110 return "DW_AT_prototyped";
12111 case DW_AT_return_addr:
12112 return "DW_AT_return_addr";
12113 case DW_AT_start_scope:
12114 return "DW_AT_start_scope";
12115 case DW_AT_bit_stride:
12116 return "DW_AT_bit_stride";
12117 case DW_AT_upper_bound:
12118 return "DW_AT_upper_bound";
12119 case DW_AT_abstract_origin:
12120 return "DW_AT_abstract_origin";
12121 case DW_AT_accessibility:
12122 return "DW_AT_accessibility";
12123 case DW_AT_address_class:
12124 return "DW_AT_address_class";
12125 case DW_AT_artificial:
12126 return "DW_AT_artificial";
12127 case DW_AT_base_types:
12128 return "DW_AT_base_types";
12129 case DW_AT_calling_convention:
12130 return "DW_AT_calling_convention";
12131 case DW_AT_count:
12132 return "DW_AT_count";
12133 case DW_AT_data_member_location:
12134 return "DW_AT_data_member_location";
12135 case DW_AT_decl_column:
12136 return "DW_AT_decl_column";
12137 case DW_AT_decl_file:
12138 return "DW_AT_decl_file";
12139 case DW_AT_decl_line:
12140 return "DW_AT_decl_line";
12141 case DW_AT_declaration:
12142 return "DW_AT_declaration";
12143 case DW_AT_discr_list:
12144 return "DW_AT_discr_list";
12145 case DW_AT_encoding:
12146 return "DW_AT_encoding";
12147 case DW_AT_external:
12148 return "DW_AT_external";
12149 case DW_AT_frame_base:
12150 return "DW_AT_frame_base";
12151 case DW_AT_friend:
12152 return "DW_AT_friend";
12153 case DW_AT_identifier_case:
12154 return "DW_AT_identifier_case";
12155 case DW_AT_macro_info:
12156 return "DW_AT_macro_info";
12157 case DW_AT_namelist_items:
12158 return "DW_AT_namelist_items";
12159 case DW_AT_priority:
12160 return "DW_AT_priority";
12161 case DW_AT_segment:
12162 return "DW_AT_segment";
12163 case DW_AT_specification:
12164 return "DW_AT_specification";
12165 case DW_AT_static_link:
12166 return "DW_AT_static_link";
12167 case DW_AT_type:
12168 return "DW_AT_type";
12169 case DW_AT_use_location:
12170 return "DW_AT_use_location";
12171 case DW_AT_variable_parameter:
12172 return "DW_AT_variable_parameter";
12173 case DW_AT_virtuality:
12174 return "DW_AT_virtuality";
12175 case DW_AT_vtable_elem_location:
12176 return "DW_AT_vtable_elem_location";
12177 /* DWARF 3 values. */
12178 case DW_AT_allocated:
12179 return "DW_AT_allocated";
12180 case DW_AT_associated:
12181 return "DW_AT_associated";
12182 case DW_AT_data_location:
12183 return "DW_AT_data_location";
12184 case DW_AT_byte_stride:
12185 return "DW_AT_byte_stride";
12186 case DW_AT_entry_pc:
12187 return "DW_AT_entry_pc";
12188 case DW_AT_use_UTF8:
12189 return "DW_AT_use_UTF8";
12190 case DW_AT_extension:
12191 return "DW_AT_extension";
12192 case DW_AT_ranges:
12193 return "DW_AT_ranges";
12194 case DW_AT_trampoline:
12195 return "DW_AT_trampoline";
12196 case DW_AT_call_column:
12197 return "DW_AT_call_column";
12198 case DW_AT_call_file:
12199 return "DW_AT_call_file";
12200 case DW_AT_call_line:
12201 return "DW_AT_call_line";
12202 case DW_AT_description:
12203 return "DW_AT_description";
12204 case DW_AT_binary_scale:
12205 return "DW_AT_binary_scale";
12206 case DW_AT_decimal_scale:
12207 return "DW_AT_decimal_scale";
12208 case DW_AT_small:
12209 return "DW_AT_small";
12210 case DW_AT_decimal_sign:
12211 return "DW_AT_decimal_sign";
12212 case DW_AT_digit_count:
12213 return "DW_AT_digit_count";
12214 case DW_AT_picture_string:
12215 return "DW_AT_picture_string";
12216 case DW_AT_mutable:
12217 return "DW_AT_mutable";
12218 case DW_AT_threads_scaled:
12219 return "DW_AT_threads_scaled";
12220 case DW_AT_explicit:
12221 return "DW_AT_explicit";
12222 case DW_AT_object_pointer:
12223 return "DW_AT_object_pointer";
12224 case DW_AT_endianity:
12225 return "DW_AT_endianity";
12226 case DW_AT_elemental:
12227 return "DW_AT_elemental";
12228 case DW_AT_pure:
12229 return "DW_AT_pure";
12230 case DW_AT_recursive:
12231 return "DW_AT_recursive";
12232 /* DWARF 4 values. */
12233 case DW_AT_signature:
12234 return "DW_AT_signature";
12235 case DW_AT_linkage_name:
12236 return "DW_AT_linkage_name";
12237 /* SGI/MIPS extensions. */
12238 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12239 case DW_AT_MIPS_fde:
12240 return "DW_AT_MIPS_fde";
12241 #endif
12242 case DW_AT_MIPS_loop_begin:
12243 return "DW_AT_MIPS_loop_begin";
12244 case DW_AT_MIPS_tail_loop_begin:
12245 return "DW_AT_MIPS_tail_loop_begin";
12246 case DW_AT_MIPS_epilog_begin:
12247 return "DW_AT_MIPS_epilog_begin";
12248 case DW_AT_MIPS_loop_unroll_factor:
12249 return "DW_AT_MIPS_loop_unroll_factor";
12250 case DW_AT_MIPS_software_pipeline_depth:
12251 return "DW_AT_MIPS_software_pipeline_depth";
12252 case DW_AT_MIPS_linkage_name:
12253 return "DW_AT_MIPS_linkage_name";
12254 case DW_AT_MIPS_stride:
12255 return "DW_AT_MIPS_stride";
12256 case DW_AT_MIPS_abstract_name:
12257 return "DW_AT_MIPS_abstract_name";
12258 case DW_AT_MIPS_clone_origin:
12259 return "DW_AT_MIPS_clone_origin";
12260 case DW_AT_MIPS_has_inlines:
12261 return "DW_AT_MIPS_has_inlines";
12262 /* HP extensions. */
12263 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12264 case DW_AT_HP_block_index:
12265 return "DW_AT_HP_block_index";
12266 #endif
12267 case DW_AT_HP_unmodifiable:
12268 return "DW_AT_HP_unmodifiable";
12269 case DW_AT_HP_actuals_stmt_list:
12270 return "DW_AT_HP_actuals_stmt_list";
12271 case DW_AT_HP_proc_per_section:
12272 return "DW_AT_HP_proc_per_section";
12273 case DW_AT_HP_raw_data_ptr:
12274 return "DW_AT_HP_raw_data_ptr";
12275 case DW_AT_HP_pass_by_reference:
12276 return "DW_AT_HP_pass_by_reference";
12277 case DW_AT_HP_opt_level:
12278 return "DW_AT_HP_opt_level";
12279 case DW_AT_HP_prof_version_id:
12280 return "DW_AT_HP_prof_version_id";
12281 case DW_AT_HP_opt_flags:
12282 return "DW_AT_HP_opt_flags";
12283 case DW_AT_HP_cold_region_low_pc:
12284 return "DW_AT_HP_cold_region_low_pc";
12285 case DW_AT_HP_cold_region_high_pc:
12286 return "DW_AT_HP_cold_region_high_pc";
12287 case DW_AT_HP_all_variables_modifiable:
12288 return "DW_AT_HP_all_variables_modifiable";
12289 case DW_AT_HP_linkage_name:
12290 return "DW_AT_HP_linkage_name";
12291 case DW_AT_HP_prof_flags:
12292 return "DW_AT_HP_prof_flags";
12293 /* GNU extensions. */
12294 case DW_AT_sf_names:
12295 return "DW_AT_sf_names";
12296 case DW_AT_src_info:
12297 return "DW_AT_src_info";
12298 case DW_AT_mac_info:
12299 return "DW_AT_mac_info";
12300 case DW_AT_src_coords:
12301 return "DW_AT_src_coords";
12302 case DW_AT_body_begin:
12303 return "DW_AT_body_begin";
12304 case DW_AT_body_end:
12305 return "DW_AT_body_end";
12306 case DW_AT_GNU_vector:
12307 return "DW_AT_GNU_vector";
12308 case DW_AT_GNU_odr_signature:
12309 return "DW_AT_GNU_odr_signature";
12310 /* VMS extensions. */
12311 case DW_AT_VMS_rtnbeg_pd_address:
12312 return "DW_AT_VMS_rtnbeg_pd_address";
12313 /* UPC extension. */
12314 case DW_AT_upc_threads_scaled:
12315 return "DW_AT_upc_threads_scaled";
12316 /* PGI (STMicroelectronics) extensions. */
12317 case DW_AT_PGI_lbase:
12318 return "DW_AT_PGI_lbase";
12319 case DW_AT_PGI_soffset:
12320 return "DW_AT_PGI_soffset";
12321 case DW_AT_PGI_lstride:
12322 return "DW_AT_PGI_lstride";
12323 default:
12324 return "DW_AT_<unknown>";
12325 }
12326 }
12327
12328 /* Convert a DWARF value form code into its string name. */
12329
12330 static char *
12331 dwarf_form_name (unsigned form)
12332 {
12333 switch (form)
12334 {
12335 case DW_FORM_addr:
12336 return "DW_FORM_addr";
12337 case DW_FORM_block2:
12338 return "DW_FORM_block2";
12339 case DW_FORM_block4:
12340 return "DW_FORM_block4";
12341 case DW_FORM_data2:
12342 return "DW_FORM_data2";
12343 case DW_FORM_data4:
12344 return "DW_FORM_data4";
12345 case DW_FORM_data8:
12346 return "DW_FORM_data8";
12347 case DW_FORM_string:
12348 return "DW_FORM_string";
12349 case DW_FORM_block:
12350 return "DW_FORM_block";
12351 case DW_FORM_block1:
12352 return "DW_FORM_block1";
12353 case DW_FORM_data1:
12354 return "DW_FORM_data1";
12355 case DW_FORM_flag:
12356 return "DW_FORM_flag";
12357 case DW_FORM_sdata:
12358 return "DW_FORM_sdata";
12359 case DW_FORM_strp:
12360 return "DW_FORM_strp";
12361 case DW_FORM_udata:
12362 return "DW_FORM_udata";
12363 case DW_FORM_ref_addr:
12364 return "DW_FORM_ref_addr";
12365 case DW_FORM_ref1:
12366 return "DW_FORM_ref1";
12367 case DW_FORM_ref2:
12368 return "DW_FORM_ref2";
12369 case DW_FORM_ref4:
12370 return "DW_FORM_ref4";
12371 case DW_FORM_ref8:
12372 return "DW_FORM_ref8";
12373 case DW_FORM_ref_udata:
12374 return "DW_FORM_ref_udata";
12375 case DW_FORM_indirect:
12376 return "DW_FORM_indirect";
12377 case DW_FORM_sec_offset:
12378 return "DW_FORM_sec_offset";
12379 case DW_FORM_exprloc:
12380 return "DW_FORM_exprloc";
12381 case DW_FORM_flag_present:
12382 return "DW_FORM_flag_present";
12383 case DW_FORM_sig8:
12384 return "DW_FORM_sig8";
12385 default:
12386 return "DW_FORM_<unknown>";
12387 }
12388 }
12389
12390 /* Convert a DWARF stack opcode into its string name. */
12391
12392 const char *
12393 dwarf_stack_op_name (unsigned op, int def)
12394 {
12395 switch (op)
12396 {
12397 case DW_OP_addr:
12398 return "DW_OP_addr";
12399 case DW_OP_deref:
12400 return "DW_OP_deref";
12401 case DW_OP_const1u:
12402 return "DW_OP_const1u";
12403 case DW_OP_const1s:
12404 return "DW_OP_const1s";
12405 case DW_OP_const2u:
12406 return "DW_OP_const2u";
12407 case DW_OP_const2s:
12408 return "DW_OP_const2s";
12409 case DW_OP_const4u:
12410 return "DW_OP_const4u";
12411 case DW_OP_const4s:
12412 return "DW_OP_const4s";
12413 case DW_OP_const8u:
12414 return "DW_OP_const8u";
12415 case DW_OP_const8s:
12416 return "DW_OP_const8s";
12417 case DW_OP_constu:
12418 return "DW_OP_constu";
12419 case DW_OP_consts:
12420 return "DW_OP_consts";
12421 case DW_OP_dup:
12422 return "DW_OP_dup";
12423 case DW_OP_drop:
12424 return "DW_OP_drop";
12425 case DW_OP_over:
12426 return "DW_OP_over";
12427 case DW_OP_pick:
12428 return "DW_OP_pick";
12429 case DW_OP_swap:
12430 return "DW_OP_swap";
12431 case DW_OP_rot:
12432 return "DW_OP_rot";
12433 case DW_OP_xderef:
12434 return "DW_OP_xderef";
12435 case DW_OP_abs:
12436 return "DW_OP_abs";
12437 case DW_OP_and:
12438 return "DW_OP_and";
12439 case DW_OP_div:
12440 return "DW_OP_div";
12441 case DW_OP_minus:
12442 return "DW_OP_minus";
12443 case DW_OP_mod:
12444 return "DW_OP_mod";
12445 case DW_OP_mul:
12446 return "DW_OP_mul";
12447 case DW_OP_neg:
12448 return "DW_OP_neg";
12449 case DW_OP_not:
12450 return "DW_OP_not";
12451 case DW_OP_or:
12452 return "DW_OP_or";
12453 case DW_OP_plus:
12454 return "DW_OP_plus";
12455 case DW_OP_plus_uconst:
12456 return "DW_OP_plus_uconst";
12457 case DW_OP_shl:
12458 return "DW_OP_shl";
12459 case DW_OP_shr:
12460 return "DW_OP_shr";
12461 case DW_OP_shra:
12462 return "DW_OP_shra";
12463 case DW_OP_xor:
12464 return "DW_OP_xor";
12465 case DW_OP_bra:
12466 return "DW_OP_bra";
12467 case DW_OP_eq:
12468 return "DW_OP_eq";
12469 case DW_OP_ge:
12470 return "DW_OP_ge";
12471 case DW_OP_gt:
12472 return "DW_OP_gt";
12473 case DW_OP_le:
12474 return "DW_OP_le";
12475 case DW_OP_lt:
12476 return "DW_OP_lt";
12477 case DW_OP_ne:
12478 return "DW_OP_ne";
12479 case DW_OP_skip:
12480 return "DW_OP_skip";
12481 case DW_OP_lit0:
12482 return "DW_OP_lit0";
12483 case DW_OP_lit1:
12484 return "DW_OP_lit1";
12485 case DW_OP_lit2:
12486 return "DW_OP_lit2";
12487 case DW_OP_lit3:
12488 return "DW_OP_lit3";
12489 case DW_OP_lit4:
12490 return "DW_OP_lit4";
12491 case DW_OP_lit5:
12492 return "DW_OP_lit5";
12493 case DW_OP_lit6:
12494 return "DW_OP_lit6";
12495 case DW_OP_lit7:
12496 return "DW_OP_lit7";
12497 case DW_OP_lit8:
12498 return "DW_OP_lit8";
12499 case DW_OP_lit9:
12500 return "DW_OP_lit9";
12501 case DW_OP_lit10:
12502 return "DW_OP_lit10";
12503 case DW_OP_lit11:
12504 return "DW_OP_lit11";
12505 case DW_OP_lit12:
12506 return "DW_OP_lit12";
12507 case DW_OP_lit13:
12508 return "DW_OP_lit13";
12509 case DW_OP_lit14:
12510 return "DW_OP_lit14";
12511 case DW_OP_lit15:
12512 return "DW_OP_lit15";
12513 case DW_OP_lit16:
12514 return "DW_OP_lit16";
12515 case DW_OP_lit17:
12516 return "DW_OP_lit17";
12517 case DW_OP_lit18:
12518 return "DW_OP_lit18";
12519 case DW_OP_lit19:
12520 return "DW_OP_lit19";
12521 case DW_OP_lit20:
12522 return "DW_OP_lit20";
12523 case DW_OP_lit21:
12524 return "DW_OP_lit21";
12525 case DW_OP_lit22:
12526 return "DW_OP_lit22";
12527 case DW_OP_lit23:
12528 return "DW_OP_lit23";
12529 case DW_OP_lit24:
12530 return "DW_OP_lit24";
12531 case DW_OP_lit25:
12532 return "DW_OP_lit25";
12533 case DW_OP_lit26:
12534 return "DW_OP_lit26";
12535 case DW_OP_lit27:
12536 return "DW_OP_lit27";
12537 case DW_OP_lit28:
12538 return "DW_OP_lit28";
12539 case DW_OP_lit29:
12540 return "DW_OP_lit29";
12541 case DW_OP_lit30:
12542 return "DW_OP_lit30";
12543 case DW_OP_lit31:
12544 return "DW_OP_lit31";
12545 case DW_OP_reg0:
12546 return "DW_OP_reg0";
12547 case DW_OP_reg1:
12548 return "DW_OP_reg1";
12549 case DW_OP_reg2:
12550 return "DW_OP_reg2";
12551 case DW_OP_reg3:
12552 return "DW_OP_reg3";
12553 case DW_OP_reg4:
12554 return "DW_OP_reg4";
12555 case DW_OP_reg5:
12556 return "DW_OP_reg5";
12557 case DW_OP_reg6:
12558 return "DW_OP_reg6";
12559 case DW_OP_reg7:
12560 return "DW_OP_reg7";
12561 case DW_OP_reg8:
12562 return "DW_OP_reg8";
12563 case DW_OP_reg9:
12564 return "DW_OP_reg9";
12565 case DW_OP_reg10:
12566 return "DW_OP_reg10";
12567 case DW_OP_reg11:
12568 return "DW_OP_reg11";
12569 case DW_OP_reg12:
12570 return "DW_OP_reg12";
12571 case DW_OP_reg13:
12572 return "DW_OP_reg13";
12573 case DW_OP_reg14:
12574 return "DW_OP_reg14";
12575 case DW_OP_reg15:
12576 return "DW_OP_reg15";
12577 case DW_OP_reg16:
12578 return "DW_OP_reg16";
12579 case DW_OP_reg17:
12580 return "DW_OP_reg17";
12581 case DW_OP_reg18:
12582 return "DW_OP_reg18";
12583 case DW_OP_reg19:
12584 return "DW_OP_reg19";
12585 case DW_OP_reg20:
12586 return "DW_OP_reg20";
12587 case DW_OP_reg21:
12588 return "DW_OP_reg21";
12589 case DW_OP_reg22:
12590 return "DW_OP_reg22";
12591 case DW_OP_reg23:
12592 return "DW_OP_reg23";
12593 case DW_OP_reg24:
12594 return "DW_OP_reg24";
12595 case DW_OP_reg25:
12596 return "DW_OP_reg25";
12597 case DW_OP_reg26:
12598 return "DW_OP_reg26";
12599 case DW_OP_reg27:
12600 return "DW_OP_reg27";
12601 case DW_OP_reg28:
12602 return "DW_OP_reg28";
12603 case DW_OP_reg29:
12604 return "DW_OP_reg29";
12605 case DW_OP_reg30:
12606 return "DW_OP_reg30";
12607 case DW_OP_reg31:
12608 return "DW_OP_reg31";
12609 case DW_OP_breg0:
12610 return "DW_OP_breg0";
12611 case DW_OP_breg1:
12612 return "DW_OP_breg1";
12613 case DW_OP_breg2:
12614 return "DW_OP_breg2";
12615 case DW_OP_breg3:
12616 return "DW_OP_breg3";
12617 case DW_OP_breg4:
12618 return "DW_OP_breg4";
12619 case DW_OP_breg5:
12620 return "DW_OP_breg5";
12621 case DW_OP_breg6:
12622 return "DW_OP_breg6";
12623 case DW_OP_breg7:
12624 return "DW_OP_breg7";
12625 case DW_OP_breg8:
12626 return "DW_OP_breg8";
12627 case DW_OP_breg9:
12628 return "DW_OP_breg9";
12629 case DW_OP_breg10:
12630 return "DW_OP_breg10";
12631 case DW_OP_breg11:
12632 return "DW_OP_breg11";
12633 case DW_OP_breg12:
12634 return "DW_OP_breg12";
12635 case DW_OP_breg13:
12636 return "DW_OP_breg13";
12637 case DW_OP_breg14:
12638 return "DW_OP_breg14";
12639 case DW_OP_breg15:
12640 return "DW_OP_breg15";
12641 case DW_OP_breg16:
12642 return "DW_OP_breg16";
12643 case DW_OP_breg17:
12644 return "DW_OP_breg17";
12645 case DW_OP_breg18:
12646 return "DW_OP_breg18";
12647 case DW_OP_breg19:
12648 return "DW_OP_breg19";
12649 case DW_OP_breg20:
12650 return "DW_OP_breg20";
12651 case DW_OP_breg21:
12652 return "DW_OP_breg21";
12653 case DW_OP_breg22:
12654 return "DW_OP_breg22";
12655 case DW_OP_breg23:
12656 return "DW_OP_breg23";
12657 case DW_OP_breg24:
12658 return "DW_OP_breg24";
12659 case DW_OP_breg25:
12660 return "DW_OP_breg25";
12661 case DW_OP_breg26:
12662 return "DW_OP_breg26";
12663 case DW_OP_breg27:
12664 return "DW_OP_breg27";
12665 case DW_OP_breg28:
12666 return "DW_OP_breg28";
12667 case DW_OP_breg29:
12668 return "DW_OP_breg29";
12669 case DW_OP_breg30:
12670 return "DW_OP_breg30";
12671 case DW_OP_breg31:
12672 return "DW_OP_breg31";
12673 case DW_OP_regx:
12674 return "DW_OP_regx";
12675 case DW_OP_fbreg:
12676 return "DW_OP_fbreg";
12677 case DW_OP_bregx:
12678 return "DW_OP_bregx";
12679 case DW_OP_piece:
12680 return "DW_OP_piece";
12681 case DW_OP_deref_size:
12682 return "DW_OP_deref_size";
12683 case DW_OP_xderef_size:
12684 return "DW_OP_xderef_size";
12685 case DW_OP_nop:
12686 return "DW_OP_nop";
12687 /* DWARF 3 extensions. */
12688 case DW_OP_push_object_address:
12689 return "DW_OP_push_object_address";
12690 case DW_OP_call2:
12691 return "DW_OP_call2";
12692 case DW_OP_call4:
12693 return "DW_OP_call4";
12694 case DW_OP_call_ref:
12695 return "DW_OP_call_ref";
12696 case DW_OP_form_tls_address:
12697 return "DW_OP_form_tls_address";
12698 case DW_OP_call_frame_cfa:
12699 return "DW_OP_call_frame_cfa";
12700 case DW_OP_bit_piece:
12701 return "DW_OP_bit_piece";
12702 /* DWARF 4 extensions. */
12703 case DW_OP_implicit_value:
12704 return "DW_OP_implicit_value";
12705 case DW_OP_stack_value:
12706 return "DW_OP_stack_value";
12707 /* GNU extensions. */
12708 case DW_OP_GNU_push_tls_address:
12709 return "DW_OP_GNU_push_tls_address";
12710 case DW_OP_GNU_uninit:
12711 return "DW_OP_GNU_uninit";
12712 case DW_OP_GNU_implicit_pointer:
12713 return "DW_OP_GNU_implicit_pointer";
12714 default:
12715 return def ? "OP_<unknown>" : NULL;
12716 }
12717 }
12718
12719 static char *
12720 dwarf_bool_name (unsigned mybool)
12721 {
12722 if (mybool)
12723 return "TRUE";
12724 else
12725 return "FALSE";
12726 }
12727
12728 /* Convert a DWARF type code into its string name. */
12729
12730 static char *
12731 dwarf_type_encoding_name (unsigned enc)
12732 {
12733 switch (enc)
12734 {
12735 case DW_ATE_void:
12736 return "DW_ATE_void";
12737 case DW_ATE_address:
12738 return "DW_ATE_address";
12739 case DW_ATE_boolean:
12740 return "DW_ATE_boolean";
12741 case DW_ATE_complex_float:
12742 return "DW_ATE_complex_float";
12743 case DW_ATE_float:
12744 return "DW_ATE_float";
12745 case DW_ATE_signed:
12746 return "DW_ATE_signed";
12747 case DW_ATE_signed_char:
12748 return "DW_ATE_signed_char";
12749 case DW_ATE_unsigned:
12750 return "DW_ATE_unsigned";
12751 case DW_ATE_unsigned_char:
12752 return "DW_ATE_unsigned_char";
12753 /* DWARF 3. */
12754 case DW_ATE_imaginary_float:
12755 return "DW_ATE_imaginary_float";
12756 case DW_ATE_packed_decimal:
12757 return "DW_ATE_packed_decimal";
12758 case DW_ATE_numeric_string:
12759 return "DW_ATE_numeric_string";
12760 case DW_ATE_edited:
12761 return "DW_ATE_edited";
12762 case DW_ATE_signed_fixed:
12763 return "DW_ATE_signed_fixed";
12764 case DW_ATE_unsigned_fixed:
12765 return "DW_ATE_unsigned_fixed";
12766 case DW_ATE_decimal_float:
12767 return "DW_ATE_decimal_float";
12768 /* DWARF 4. */
12769 case DW_ATE_UTF:
12770 return "DW_ATE_UTF";
12771 /* HP extensions. */
12772 case DW_ATE_HP_float80:
12773 return "DW_ATE_HP_float80";
12774 case DW_ATE_HP_complex_float80:
12775 return "DW_ATE_HP_complex_float80";
12776 case DW_ATE_HP_float128:
12777 return "DW_ATE_HP_float128";
12778 case DW_ATE_HP_complex_float128:
12779 return "DW_ATE_HP_complex_float128";
12780 case DW_ATE_HP_floathpintel:
12781 return "DW_ATE_HP_floathpintel";
12782 case DW_ATE_HP_imaginary_float80:
12783 return "DW_ATE_HP_imaginary_float80";
12784 case DW_ATE_HP_imaginary_float128:
12785 return "DW_ATE_HP_imaginary_float128";
12786 default:
12787 return "DW_ATE_<unknown>";
12788 }
12789 }
12790
12791 /* Convert a DWARF call frame info operation to its string name. */
12792
12793 #if 0
12794 static char *
12795 dwarf_cfi_name (unsigned cfi_opc)
12796 {
12797 switch (cfi_opc)
12798 {
12799 case DW_CFA_advance_loc:
12800 return "DW_CFA_advance_loc";
12801 case DW_CFA_offset:
12802 return "DW_CFA_offset";
12803 case DW_CFA_restore:
12804 return "DW_CFA_restore";
12805 case DW_CFA_nop:
12806 return "DW_CFA_nop";
12807 case DW_CFA_set_loc:
12808 return "DW_CFA_set_loc";
12809 case DW_CFA_advance_loc1:
12810 return "DW_CFA_advance_loc1";
12811 case DW_CFA_advance_loc2:
12812 return "DW_CFA_advance_loc2";
12813 case DW_CFA_advance_loc4:
12814 return "DW_CFA_advance_loc4";
12815 case DW_CFA_offset_extended:
12816 return "DW_CFA_offset_extended";
12817 case DW_CFA_restore_extended:
12818 return "DW_CFA_restore_extended";
12819 case DW_CFA_undefined:
12820 return "DW_CFA_undefined";
12821 case DW_CFA_same_value:
12822 return "DW_CFA_same_value";
12823 case DW_CFA_register:
12824 return "DW_CFA_register";
12825 case DW_CFA_remember_state:
12826 return "DW_CFA_remember_state";
12827 case DW_CFA_restore_state:
12828 return "DW_CFA_restore_state";
12829 case DW_CFA_def_cfa:
12830 return "DW_CFA_def_cfa";
12831 case DW_CFA_def_cfa_register:
12832 return "DW_CFA_def_cfa_register";
12833 case DW_CFA_def_cfa_offset:
12834 return "DW_CFA_def_cfa_offset";
12835 /* DWARF 3. */
12836 case DW_CFA_def_cfa_expression:
12837 return "DW_CFA_def_cfa_expression";
12838 case DW_CFA_expression:
12839 return "DW_CFA_expression";
12840 case DW_CFA_offset_extended_sf:
12841 return "DW_CFA_offset_extended_sf";
12842 case DW_CFA_def_cfa_sf:
12843 return "DW_CFA_def_cfa_sf";
12844 case DW_CFA_def_cfa_offset_sf:
12845 return "DW_CFA_def_cfa_offset_sf";
12846 case DW_CFA_val_offset:
12847 return "DW_CFA_val_offset";
12848 case DW_CFA_val_offset_sf:
12849 return "DW_CFA_val_offset_sf";
12850 case DW_CFA_val_expression:
12851 return "DW_CFA_val_expression";
12852 /* SGI/MIPS specific. */
12853 case DW_CFA_MIPS_advance_loc8:
12854 return "DW_CFA_MIPS_advance_loc8";
12855 /* GNU extensions. */
12856 case DW_CFA_GNU_window_save:
12857 return "DW_CFA_GNU_window_save";
12858 case DW_CFA_GNU_args_size:
12859 return "DW_CFA_GNU_args_size";
12860 case DW_CFA_GNU_negative_offset_extended:
12861 return "DW_CFA_GNU_negative_offset_extended";
12862 default:
12863 return "DW_CFA_<unknown>";
12864 }
12865 }
12866 #endif
12867
12868 static void
12869 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12870 {
12871 unsigned int i;
12872
12873 print_spaces (indent, f);
12874 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12875 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12876
12877 if (die->parent != NULL)
12878 {
12879 print_spaces (indent, f);
12880 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12881 die->parent->offset);
12882 }
12883
12884 print_spaces (indent, f);
12885 fprintf_unfiltered (f, " has children: %s\n",
12886 dwarf_bool_name (die->child != NULL));
12887
12888 print_spaces (indent, f);
12889 fprintf_unfiltered (f, " attributes:\n");
12890
12891 for (i = 0; i < die->num_attrs; ++i)
12892 {
12893 print_spaces (indent, f);
12894 fprintf_unfiltered (f, " %s (%s) ",
12895 dwarf_attr_name (die->attrs[i].name),
12896 dwarf_form_name (die->attrs[i].form));
12897
12898 switch (die->attrs[i].form)
12899 {
12900 case DW_FORM_ref_addr:
12901 case DW_FORM_addr:
12902 fprintf_unfiltered (f, "address: ");
12903 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12904 break;
12905 case DW_FORM_block2:
12906 case DW_FORM_block4:
12907 case DW_FORM_block:
12908 case DW_FORM_block1:
12909 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12910 break;
12911 case DW_FORM_exprloc:
12912 fprintf_unfiltered (f, "expression: size %u",
12913 DW_BLOCK (&die->attrs[i])->size);
12914 break;
12915 case DW_FORM_ref1:
12916 case DW_FORM_ref2:
12917 case DW_FORM_ref4:
12918 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12919 (long) (DW_ADDR (&die->attrs[i])));
12920 break;
12921 case DW_FORM_data1:
12922 case DW_FORM_data2:
12923 case DW_FORM_data4:
12924 case DW_FORM_data8:
12925 case DW_FORM_udata:
12926 case DW_FORM_sdata:
12927 fprintf_unfiltered (f, "constant: %s",
12928 pulongest (DW_UNSND (&die->attrs[i])));
12929 break;
12930 case DW_FORM_sec_offset:
12931 fprintf_unfiltered (f, "section offset: %s",
12932 pulongest (DW_UNSND (&die->attrs[i])));
12933 break;
12934 case DW_FORM_sig8:
12935 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12936 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12937 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12938 else
12939 fprintf_unfiltered (f, "signatured type, offset: unknown");
12940 break;
12941 case DW_FORM_string:
12942 case DW_FORM_strp:
12943 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12944 DW_STRING (&die->attrs[i])
12945 ? DW_STRING (&die->attrs[i]) : "",
12946 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12947 break;
12948 case DW_FORM_flag:
12949 if (DW_UNSND (&die->attrs[i]))
12950 fprintf_unfiltered (f, "flag: TRUE");
12951 else
12952 fprintf_unfiltered (f, "flag: FALSE");
12953 break;
12954 case DW_FORM_flag_present:
12955 fprintf_unfiltered (f, "flag: TRUE");
12956 break;
12957 case DW_FORM_indirect:
12958 /* the reader will have reduced the indirect form to
12959 the "base form" so this form should not occur */
12960 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12961 break;
12962 default:
12963 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12964 die->attrs[i].form);
12965 break;
12966 }
12967 fprintf_unfiltered (f, "\n");
12968 }
12969 }
12970
12971 static void
12972 dump_die_for_error (struct die_info *die)
12973 {
12974 dump_die_shallow (gdb_stderr, 0, die);
12975 }
12976
12977 static void
12978 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12979 {
12980 int indent = level * 4;
12981
12982 gdb_assert (die != NULL);
12983
12984 if (level >= max_level)
12985 return;
12986
12987 dump_die_shallow (f, indent, die);
12988
12989 if (die->child != NULL)
12990 {
12991 print_spaces (indent, f);
12992 fprintf_unfiltered (f, " Children:");
12993 if (level + 1 < max_level)
12994 {
12995 fprintf_unfiltered (f, "\n");
12996 dump_die_1 (f, level + 1, max_level, die->child);
12997 }
12998 else
12999 {
13000 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
13001 }
13002 }
13003
13004 if (die->sibling != NULL && level > 0)
13005 {
13006 dump_die_1 (f, level, max_level, die->sibling);
13007 }
13008 }
13009
13010 /* This is called from the pdie macro in gdbinit.in.
13011 It's not static so gcc will keep a copy callable from gdb. */
13012
13013 void
13014 dump_die (struct die_info *die, int max_level)
13015 {
13016 dump_die_1 (gdb_stdlog, 0, max_level, die);
13017 }
13018
13019 static void
13020 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13021 {
13022 void **slot;
13023
13024 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13025
13026 *slot = die;
13027 }
13028
13029 static int
13030 is_ref_attr (struct attribute *attr)
13031 {
13032 switch (attr->form)
13033 {
13034 case DW_FORM_ref_addr:
13035 case DW_FORM_ref1:
13036 case DW_FORM_ref2:
13037 case DW_FORM_ref4:
13038 case DW_FORM_ref8:
13039 case DW_FORM_ref_udata:
13040 return 1;
13041 default:
13042 return 0;
13043 }
13044 }
13045
13046 static unsigned int
13047 dwarf2_get_ref_die_offset (struct attribute *attr)
13048 {
13049 if (is_ref_attr (attr))
13050 return DW_ADDR (attr);
13051
13052 complaint (&symfile_complaints,
13053 _("unsupported die ref attribute form: '%s'"),
13054 dwarf_form_name (attr->form));
13055 return 0;
13056 }
13057
13058 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13059 * the value held by the attribute is not constant. */
13060
13061 static LONGEST
13062 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13063 {
13064 if (attr->form == DW_FORM_sdata)
13065 return DW_SND (attr);
13066 else if (attr->form == DW_FORM_udata
13067 || attr->form == DW_FORM_data1
13068 || attr->form == DW_FORM_data2
13069 || attr->form == DW_FORM_data4
13070 || attr->form == DW_FORM_data8)
13071 return DW_UNSND (attr);
13072 else
13073 {
13074 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
13075 dwarf_form_name (attr->form));
13076 return default_value;
13077 }
13078 }
13079
13080 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13081 unit and add it to our queue.
13082 The result is non-zero if PER_CU was queued, otherwise the result is zero
13083 meaning either PER_CU is already queued or it is already loaded. */
13084
13085 static int
13086 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13087 struct dwarf2_per_cu_data *per_cu)
13088 {
13089 /* We may arrive here during partial symbol reading, if we need full
13090 DIEs to process an unusual case (e.g. template arguments). Do
13091 not queue PER_CU, just tell our caller to load its DIEs. */
13092 if (dwarf2_per_objfile->reading_partial_symbols)
13093 {
13094 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13095 return 1;
13096 return 0;
13097 }
13098
13099 /* Mark the dependence relation so that we don't flush PER_CU
13100 too early. */
13101 dwarf2_add_dependence (this_cu, per_cu);
13102
13103 /* If it's already on the queue, we have nothing to do. */
13104 if (per_cu->queued)
13105 return 0;
13106
13107 /* If the compilation unit is already loaded, just mark it as
13108 used. */
13109 if (per_cu->cu != NULL)
13110 {
13111 per_cu->cu->last_used = 0;
13112 return 0;
13113 }
13114
13115 /* Add it to the queue. */
13116 queue_comp_unit (per_cu, this_cu->objfile);
13117
13118 return 1;
13119 }
13120
13121 /* Follow reference or signature attribute ATTR of SRC_DIE.
13122 On entry *REF_CU is the CU of SRC_DIE.
13123 On exit *REF_CU is the CU of the result. */
13124
13125 static struct die_info *
13126 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13127 struct dwarf2_cu **ref_cu)
13128 {
13129 struct die_info *die;
13130
13131 if (is_ref_attr (attr))
13132 die = follow_die_ref (src_die, attr, ref_cu);
13133 else if (attr->form == DW_FORM_sig8)
13134 die = follow_die_sig (src_die, attr, ref_cu);
13135 else
13136 {
13137 dump_die_for_error (src_die);
13138 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13139 (*ref_cu)->objfile->name);
13140 }
13141
13142 return die;
13143 }
13144
13145 /* Follow reference OFFSET.
13146 On entry *REF_CU is the CU of the source die referencing OFFSET.
13147 On exit *REF_CU is the CU of the result.
13148 Returns NULL if OFFSET is invalid. */
13149
13150 static struct die_info *
13151 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13152 {
13153 struct die_info temp_die;
13154 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13155
13156 gdb_assert (cu->per_cu != NULL);
13157
13158 target_cu = cu;
13159
13160 if (cu->per_cu->from_debug_types)
13161 {
13162 /* .debug_types CUs cannot reference anything outside their CU.
13163 If they need to, they have to reference a signatured type via
13164 DW_FORM_sig8. */
13165 if (! offset_in_cu_p (&cu->header, offset))
13166 return NULL;
13167 }
13168 else if (! offset_in_cu_p (&cu->header, offset))
13169 {
13170 struct dwarf2_per_cu_data *per_cu;
13171
13172 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13173
13174 /* If necessary, add it to the queue and load its DIEs. */
13175 if (maybe_queue_comp_unit (cu, per_cu))
13176 load_full_comp_unit (per_cu, cu->objfile);
13177
13178 target_cu = per_cu->cu;
13179 }
13180 else if (cu->dies == NULL)
13181 {
13182 /* We're loading full DIEs during partial symbol reading. */
13183 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13184 load_full_comp_unit (cu->per_cu, cu->objfile);
13185 }
13186
13187 *ref_cu = target_cu;
13188 temp_die.offset = offset;
13189 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13190 }
13191
13192 /* Follow reference attribute ATTR of SRC_DIE.
13193 On entry *REF_CU is the CU of SRC_DIE.
13194 On exit *REF_CU is the CU of the result. */
13195
13196 static struct die_info *
13197 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13198 struct dwarf2_cu **ref_cu)
13199 {
13200 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13201 struct dwarf2_cu *cu = *ref_cu;
13202 struct die_info *die;
13203
13204 die = follow_die_offset (offset, ref_cu);
13205 if (!die)
13206 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13207 "at 0x%x [in module %s]"),
13208 offset, src_die->offset, cu->objfile->name);
13209
13210 return die;
13211 }
13212
13213 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13214 value is intended for DW_OP_call*. */
13215
13216 struct dwarf2_locexpr_baton
13217 dwarf2_fetch_die_location_block (unsigned int offset,
13218 struct dwarf2_per_cu_data *per_cu,
13219 CORE_ADDR (*get_frame_pc) (void *baton),
13220 void *baton)
13221 {
13222 struct dwarf2_cu *cu = per_cu->cu;
13223 struct die_info *die;
13224 struct attribute *attr;
13225 struct dwarf2_locexpr_baton retval;
13226
13227 dw2_setup (per_cu->objfile);
13228
13229 die = follow_die_offset (offset, &cu);
13230 if (!die)
13231 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13232 offset, per_cu->cu->objfile->name);
13233
13234 attr = dwarf2_attr (die, DW_AT_location, cu);
13235 if (!attr)
13236 {
13237 /* DWARF: "If there is no such attribute, then there is no effect.". */
13238
13239 retval.data = NULL;
13240 retval.size = 0;
13241 }
13242 else if (attr_form_is_section_offset (attr))
13243 {
13244 struct dwarf2_loclist_baton loclist_baton;
13245 CORE_ADDR pc = (*get_frame_pc) (baton);
13246 size_t size;
13247
13248 fill_in_loclist_baton (cu, &loclist_baton, attr);
13249
13250 retval.data = dwarf2_find_location_expression (&loclist_baton,
13251 &size, pc);
13252 retval.size = size;
13253 }
13254 else
13255 {
13256 if (!attr_form_is_block (attr))
13257 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13258 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13259 offset, per_cu->cu->objfile->name);
13260
13261 retval.data = DW_BLOCK (attr)->data;
13262 retval.size = DW_BLOCK (attr)->size;
13263 }
13264 retval.per_cu = cu->per_cu;
13265 return retval;
13266 }
13267
13268 /* Follow the signature attribute ATTR in SRC_DIE.
13269 On entry *REF_CU is the CU of SRC_DIE.
13270 On exit *REF_CU is the CU of the result. */
13271
13272 static struct die_info *
13273 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13274 struct dwarf2_cu **ref_cu)
13275 {
13276 struct objfile *objfile = (*ref_cu)->objfile;
13277 struct die_info temp_die;
13278 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13279 struct dwarf2_cu *sig_cu;
13280 struct die_info *die;
13281
13282 /* sig_type will be NULL if the signatured type is missing from
13283 the debug info. */
13284 if (sig_type == NULL)
13285 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13286 "at 0x%x [in module %s]"),
13287 src_die->offset, objfile->name);
13288
13289 /* If necessary, add it to the queue and load its DIEs. */
13290
13291 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13292 read_signatured_type (objfile, sig_type);
13293
13294 gdb_assert (sig_type->per_cu.cu != NULL);
13295
13296 sig_cu = sig_type->per_cu.cu;
13297 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13298 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13299 if (die)
13300 {
13301 *ref_cu = sig_cu;
13302 return die;
13303 }
13304
13305 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13306 "at 0x%x [in module %s]"),
13307 sig_type->type_offset, src_die->offset, objfile->name);
13308 }
13309
13310 /* Given an offset of a signatured type, return its signatured_type. */
13311
13312 static struct signatured_type *
13313 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13314 {
13315 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13316 unsigned int length, initial_length_size;
13317 unsigned int sig_offset;
13318 struct signatured_type find_entry, *type_sig;
13319
13320 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13321 sig_offset = (initial_length_size
13322 + 2 /*version*/
13323 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13324 + 1 /*address_size*/);
13325 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13326 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13327
13328 /* This is only used to lookup previously recorded types.
13329 If we didn't find it, it's our bug. */
13330 gdb_assert (type_sig != NULL);
13331 gdb_assert (offset == type_sig->offset);
13332
13333 return type_sig;
13334 }
13335
13336 /* Read in signatured type at OFFSET and build its CU and die(s). */
13337
13338 static void
13339 read_signatured_type_at_offset (struct objfile *objfile,
13340 unsigned int offset)
13341 {
13342 struct signatured_type *type_sig;
13343
13344 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13345
13346 /* We have the section offset, but we need the signature to do the
13347 hash table lookup. */
13348 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13349
13350 gdb_assert (type_sig->per_cu.cu == NULL);
13351
13352 read_signatured_type (objfile, type_sig);
13353
13354 gdb_assert (type_sig->per_cu.cu != NULL);
13355 }
13356
13357 /* Read in a signatured type and build its CU and DIEs. */
13358
13359 static void
13360 read_signatured_type (struct objfile *objfile,
13361 struct signatured_type *type_sig)
13362 {
13363 gdb_byte *types_ptr;
13364 struct die_reader_specs reader_specs;
13365 struct dwarf2_cu *cu;
13366 ULONGEST signature;
13367 struct cleanup *back_to, *free_cu_cleanup;
13368
13369 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13370 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13371
13372 gdb_assert (type_sig->per_cu.cu == NULL);
13373
13374 cu = xmalloc (sizeof (*cu));
13375 init_one_comp_unit (cu, objfile);
13376
13377 type_sig->per_cu.cu = cu;
13378 cu->per_cu = &type_sig->per_cu;
13379
13380 /* If an error occurs while loading, release our storage. */
13381 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13382
13383 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13384 types_ptr, objfile->obfd);
13385 gdb_assert (signature == type_sig->signature);
13386
13387 cu->die_hash
13388 = htab_create_alloc_ex (cu->header.length / 12,
13389 die_hash,
13390 die_eq,
13391 NULL,
13392 &cu->comp_unit_obstack,
13393 hashtab_obstack_allocate,
13394 dummy_obstack_deallocate);
13395
13396 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13397 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13398
13399 init_cu_die_reader (&reader_specs, cu);
13400
13401 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13402 NULL /*parent*/);
13403
13404 /* We try not to read any attributes in this function, because not
13405 all objfiles needed for references have been loaded yet, and symbol
13406 table processing isn't initialized. But we have to set the CU language,
13407 or we won't be able to build types correctly. */
13408 prepare_one_comp_unit (cu, cu->dies);
13409
13410 do_cleanups (back_to);
13411
13412 /* We've successfully allocated this compilation unit. Let our caller
13413 clean it up when finished with it. */
13414 discard_cleanups (free_cu_cleanup);
13415
13416 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13417 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13418 }
13419
13420 /* Decode simple location descriptions.
13421 Given a pointer to a dwarf block that defines a location, compute
13422 the location and return the value.
13423
13424 NOTE drow/2003-11-18: This function is called in two situations
13425 now: for the address of static or global variables (partial symbols
13426 only) and for offsets into structures which are expected to be
13427 (more or less) constant. The partial symbol case should go away,
13428 and only the constant case should remain. That will let this
13429 function complain more accurately. A few special modes are allowed
13430 without complaint for global variables (for instance, global
13431 register values and thread-local values).
13432
13433 A location description containing no operations indicates that the
13434 object is optimized out. The return value is 0 for that case.
13435 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13436 callers will only want a very basic result and this can become a
13437 complaint.
13438
13439 Note that stack[0] is unused except as a default error return. */
13440
13441 static CORE_ADDR
13442 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13443 {
13444 struct objfile *objfile = cu->objfile;
13445 int i;
13446 int size = blk->size;
13447 gdb_byte *data = blk->data;
13448 CORE_ADDR stack[64];
13449 int stacki;
13450 unsigned int bytes_read, unsnd;
13451 gdb_byte op;
13452
13453 i = 0;
13454 stacki = 0;
13455 stack[stacki] = 0;
13456 stack[++stacki] = 0;
13457
13458 while (i < size)
13459 {
13460 op = data[i++];
13461 switch (op)
13462 {
13463 case DW_OP_lit0:
13464 case DW_OP_lit1:
13465 case DW_OP_lit2:
13466 case DW_OP_lit3:
13467 case DW_OP_lit4:
13468 case DW_OP_lit5:
13469 case DW_OP_lit6:
13470 case DW_OP_lit7:
13471 case DW_OP_lit8:
13472 case DW_OP_lit9:
13473 case DW_OP_lit10:
13474 case DW_OP_lit11:
13475 case DW_OP_lit12:
13476 case DW_OP_lit13:
13477 case DW_OP_lit14:
13478 case DW_OP_lit15:
13479 case DW_OP_lit16:
13480 case DW_OP_lit17:
13481 case DW_OP_lit18:
13482 case DW_OP_lit19:
13483 case DW_OP_lit20:
13484 case DW_OP_lit21:
13485 case DW_OP_lit22:
13486 case DW_OP_lit23:
13487 case DW_OP_lit24:
13488 case DW_OP_lit25:
13489 case DW_OP_lit26:
13490 case DW_OP_lit27:
13491 case DW_OP_lit28:
13492 case DW_OP_lit29:
13493 case DW_OP_lit30:
13494 case DW_OP_lit31:
13495 stack[++stacki] = op - DW_OP_lit0;
13496 break;
13497
13498 case DW_OP_reg0:
13499 case DW_OP_reg1:
13500 case DW_OP_reg2:
13501 case DW_OP_reg3:
13502 case DW_OP_reg4:
13503 case DW_OP_reg5:
13504 case DW_OP_reg6:
13505 case DW_OP_reg7:
13506 case DW_OP_reg8:
13507 case DW_OP_reg9:
13508 case DW_OP_reg10:
13509 case DW_OP_reg11:
13510 case DW_OP_reg12:
13511 case DW_OP_reg13:
13512 case DW_OP_reg14:
13513 case DW_OP_reg15:
13514 case DW_OP_reg16:
13515 case DW_OP_reg17:
13516 case DW_OP_reg18:
13517 case DW_OP_reg19:
13518 case DW_OP_reg20:
13519 case DW_OP_reg21:
13520 case DW_OP_reg22:
13521 case DW_OP_reg23:
13522 case DW_OP_reg24:
13523 case DW_OP_reg25:
13524 case DW_OP_reg26:
13525 case DW_OP_reg27:
13526 case DW_OP_reg28:
13527 case DW_OP_reg29:
13528 case DW_OP_reg30:
13529 case DW_OP_reg31:
13530 stack[++stacki] = op - DW_OP_reg0;
13531 if (i < size)
13532 dwarf2_complex_location_expr_complaint ();
13533 break;
13534
13535 case DW_OP_regx:
13536 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13537 i += bytes_read;
13538 stack[++stacki] = unsnd;
13539 if (i < size)
13540 dwarf2_complex_location_expr_complaint ();
13541 break;
13542
13543 case DW_OP_addr:
13544 stack[++stacki] = read_address (objfile->obfd, &data[i],
13545 cu, &bytes_read);
13546 i += bytes_read;
13547 break;
13548
13549 case DW_OP_const1u:
13550 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13551 i += 1;
13552 break;
13553
13554 case DW_OP_const1s:
13555 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13556 i += 1;
13557 break;
13558
13559 case DW_OP_const2u:
13560 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13561 i += 2;
13562 break;
13563
13564 case DW_OP_const2s:
13565 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13566 i += 2;
13567 break;
13568
13569 case DW_OP_const4u:
13570 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13571 i += 4;
13572 break;
13573
13574 case DW_OP_const4s:
13575 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13576 i += 4;
13577 break;
13578
13579 case DW_OP_constu:
13580 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13581 &bytes_read);
13582 i += bytes_read;
13583 break;
13584
13585 case DW_OP_consts:
13586 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13587 i += bytes_read;
13588 break;
13589
13590 case DW_OP_dup:
13591 stack[stacki + 1] = stack[stacki];
13592 stacki++;
13593 break;
13594
13595 case DW_OP_plus:
13596 stack[stacki - 1] += stack[stacki];
13597 stacki--;
13598 break;
13599
13600 case DW_OP_plus_uconst:
13601 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13602 i += bytes_read;
13603 break;
13604
13605 case DW_OP_minus:
13606 stack[stacki - 1] -= stack[stacki];
13607 stacki--;
13608 break;
13609
13610 case DW_OP_deref:
13611 /* If we're not the last op, then we definitely can't encode
13612 this using GDB's address_class enum. This is valid for partial
13613 global symbols, although the variable's address will be bogus
13614 in the psymtab. */
13615 if (i < size)
13616 dwarf2_complex_location_expr_complaint ();
13617 break;
13618
13619 case DW_OP_GNU_push_tls_address:
13620 /* The top of the stack has the offset from the beginning
13621 of the thread control block at which the variable is located. */
13622 /* Nothing should follow this operator, so the top of stack would
13623 be returned. */
13624 /* This is valid for partial global symbols, but the variable's
13625 address will be bogus in the psymtab. */
13626 if (i < size)
13627 dwarf2_complex_location_expr_complaint ();
13628 break;
13629
13630 case DW_OP_GNU_uninit:
13631 break;
13632
13633 default:
13634 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13635 dwarf_stack_op_name (op, 1));
13636 return (stack[stacki]);
13637 }
13638
13639 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13640 outside of the allocated space. Also enforce minimum>0. */
13641 if (stacki >= ARRAY_SIZE (stack) - 1)
13642 {
13643 complaint (&symfile_complaints,
13644 _("location description stack overflow"));
13645 return 0;
13646 }
13647
13648 if (stacki <= 0)
13649 {
13650 complaint (&symfile_complaints,
13651 _("location description stack underflow"));
13652 return 0;
13653 }
13654 }
13655 return (stack[stacki]);
13656 }
13657
13658 /* memory allocation interface */
13659
13660 static struct dwarf_block *
13661 dwarf_alloc_block (struct dwarf2_cu *cu)
13662 {
13663 struct dwarf_block *blk;
13664
13665 blk = (struct dwarf_block *)
13666 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13667 return (blk);
13668 }
13669
13670 static struct abbrev_info *
13671 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13672 {
13673 struct abbrev_info *abbrev;
13674
13675 abbrev = (struct abbrev_info *)
13676 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13677 memset (abbrev, 0, sizeof (struct abbrev_info));
13678 return (abbrev);
13679 }
13680
13681 static struct die_info *
13682 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13683 {
13684 struct die_info *die;
13685 size_t size = sizeof (struct die_info);
13686
13687 if (num_attrs > 1)
13688 size += (num_attrs - 1) * sizeof (struct attribute);
13689
13690 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13691 memset (die, 0, sizeof (struct die_info));
13692 return (die);
13693 }
13694
13695 \f
13696 /* Macro support. */
13697
13698 /* Return the full name of file number I in *LH's file name table.
13699 Use COMP_DIR as the name of the current directory of the
13700 compilation. The result is allocated using xmalloc; the caller is
13701 responsible for freeing it. */
13702 static char *
13703 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13704 {
13705 /* Is the file number a valid index into the line header's file name
13706 table? Remember that file numbers start with one, not zero. */
13707 if (1 <= file && file <= lh->num_file_names)
13708 {
13709 struct file_entry *fe = &lh->file_names[file - 1];
13710
13711 if (IS_ABSOLUTE_PATH (fe->name))
13712 return xstrdup (fe->name);
13713 else
13714 {
13715 const char *dir;
13716 int dir_len;
13717 char *full_name;
13718
13719 if (fe->dir_index)
13720 dir = lh->include_dirs[fe->dir_index - 1];
13721 else
13722 dir = comp_dir;
13723
13724 if (dir)
13725 {
13726 dir_len = strlen (dir);
13727 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13728 strcpy (full_name, dir);
13729 full_name[dir_len] = '/';
13730 strcpy (full_name + dir_len + 1, fe->name);
13731 return full_name;
13732 }
13733 else
13734 return xstrdup (fe->name);
13735 }
13736 }
13737 else
13738 {
13739 /* The compiler produced a bogus file number. We can at least
13740 record the macro definitions made in the file, even if we
13741 won't be able to find the file by name. */
13742 char fake_name[80];
13743
13744 sprintf (fake_name, "<bad macro file number %d>", file);
13745
13746 complaint (&symfile_complaints,
13747 _("bad file number in macro information (%d)"),
13748 file);
13749
13750 return xstrdup (fake_name);
13751 }
13752 }
13753
13754
13755 static struct macro_source_file *
13756 macro_start_file (int file, int line,
13757 struct macro_source_file *current_file,
13758 const char *comp_dir,
13759 struct line_header *lh, struct objfile *objfile)
13760 {
13761 /* The full name of this source file. */
13762 char *full_name = file_full_name (file, lh, comp_dir);
13763
13764 /* We don't create a macro table for this compilation unit
13765 at all until we actually get a filename. */
13766 if (! pending_macros)
13767 pending_macros = new_macro_table (&objfile->objfile_obstack,
13768 objfile->macro_cache);
13769
13770 if (! current_file)
13771 /* If we have no current file, then this must be the start_file
13772 directive for the compilation unit's main source file. */
13773 current_file = macro_set_main (pending_macros, full_name);
13774 else
13775 current_file = macro_include (current_file, line, full_name);
13776
13777 xfree (full_name);
13778
13779 return current_file;
13780 }
13781
13782
13783 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13784 followed by a null byte. */
13785 static char *
13786 copy_string (const char *buf, int len)
13787 {
13788 char *s = xmalloc (len + 1);
13789
13790 memcpy (s, buf, len);
13791 s[len] = '\0';
13792 return s;
13793 }
13794
13795
13796 static const char *
13797 consume_improper_spaces (const char *p, const char *body)
13798 {
13799 if (*p == ' ')
13800 {
13801 complaint (&symfile_complaints,
13802 _("macro definition contains spaces in formal argument list:\n`%s'"),
13803 body);
13804
13805 while (*p == ' ')
13806 p++;
13807 }
13808
13809 return p;
13810 }
13811
13812
13813 static void
13814 parse_macro_definition (struct macro_source_file *file, int line,
13815 const char *body)
13816 {
13817 const char *p;
13818
13819 /* The body string takes one of two forms. For object-like macro
13820 definitions, it should be:
13821
13822 <macro name> " " <definition>
13823
13824 For function-like macro definitions, it should be:
13825
13826 <macro name> "() " <definition>
13827 or
13828 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13829
13830 Spaces may appear only where explicitly indicated, and in the
13831 <definition>.
13832
13833 The Dwarf 2 spec says that an object-like macro's name is always
13834 followed by a space, but versions of GCC around March 2002 omit
13835 the space when the macro's definition is the empty string.
13836
13837 The Dwarf 2 spec says that there should be no spaces between the
13838 formal arguments in a function-like macro's formal argument list,
13839 but versions of GCC around March 2002 include spaces after the
13840 commas. */
13841
13842
13843 /* Find the extent of the macro name. The macro name is terminated
13844 by either a space or null character (for an object-like macro) or
13845 an opening paren (for a function-like macro). */
13846 for (p = body; *p; p++)
13847 if (*p == ' ' || *p == '(')
13848 break;
13849
13850 if (*p == ' ' || *p == '\0')
13851 {
13852 /* It's an object-like macro. */
13853 int name_len = p - body;
13854 char *name = copy_string (body, name_len);
13855 const char *replacement;
13856
13857 if (*p == ' ')
13858 replacement = body + name_len + 1;
13859 else
13860 {
13861 dwarf2_macro_malformed_definition_complaint (body);
13862 replacement = body + name_len;
13863 }
13864
13865 macro_define_object (file, line, name, replacement);
13866
13867 xfree (name);
13868 }
13869 else if (*p == '(')
13870 {
13871 /* It's a function-like macro. */
13872 char *name = copy_string (body, p - body);
13873 int argc = 0;
13874 int argv_size = 1;
13875 char **argv = xmalloc (argv_size * sizeof (*argv));
13876
13877 p++;
13878
13879 p = consume_improper_spaces (p, body);
13880
13881 /* Parse the formal argument list. */
13882 while (*p && *p != ')')
13883 {
13884 /* Find the extent of the current argument name. */
13885 const char *arg_start = p;
13886
13887 while (*p && *p != ',' && *p != ')' && *p != ' ')
13888 p++;
13889
13890 if (! *p || p == arg_start)
13891 dwarf2_macro_malformed_definition_complaint (body);
13892 else
13893 {
13894 /* Make sure argv has room for the new argument. */
13895 if (argc >= argv_size)
13896 {
13897 argv_size *= 2;
13898 argv = xrealloc (argv, argv_size * sizeof (*argv));
13899 }
13900
13901 argv[argc++] = copy_string (arg_start, p - arg_start);
13902 }
13903
13904 p = consume_improper_spaces (p, body);
13905
13906 /* Consume the comma, if present. */
13907 if (*p == ',')
13908 {
13909 p++;
13910
13911 p = consume_improper_spaces (p, body);
13912 }
13913 }
13914
13915 if (*p == ')')
13916 {
13917 p++;
13918
13919 if (*p == ' ')
13920 /* Perfectly formed definition, no complaints. */
13921 macro_define_function (file, line, name,
13922 argc, (const char **) argv,
13923 p + 1);
13924 else if (*p == '\0')
13925 {
13926 /* Complain, but do define it. */
13927 dwarf2_macro_malformed_definition_complaint (body);
13928 macro_define_function (file, line, name,
13929 argc, (const char **) argv,
13930 p);
13931 }
13932 else
13933 /* Just complain. */
13934 dwarf2_macro_malformed_definition_complaint (body);
13935 }
13936 else
13937 /* Just complain. */
13938 dwarf2_macro_malformed_definition_complaint (body);
13939
13940 xfree (name);
13941 {
13942 int i;
13943
13944 for (i = 0; i < argc; i++)
13945 xfree (argv[i]);
13946 }
13947 xfree (argv);
13948 }
13949 else
13950 dwarf2_macro_malformed_definition_complaint (body);
13951 }
13952
13953
13954 static void
13955 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13956 char *comp_dir, bfd *abfd,
13957 struct dwarf2_cu *cu)
13958 {
13959 gdb_byte *mac_ptr, *mac_end;
13960 struct macro_source_file *current_file = 0;
13961 enum dwarf_macinfo_record_type macinfo_type;
13962 int at_commandline;
13963
13964 dwarf2_read_section (dwarf2_per_objfile->objfile,
13965 &dwarf2_per_objfile->macinfo);
13966 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13967 {
13968 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13969 return;
13970 }
13971
13972 /* First pass: Find the name of the base filename.
13973 This filename is needed in order to process all macros whose definition
13974 (or undefinition) comes from the command line. These macros are defined
13975 before the first DW_MACINFO_start_file entry, and yet still need to be
13976 associated to the base file.
13977
13978 To determine the base file name, we scan the macro definitions until we
13979 reach the first DW_MACINFO_start_file entry. We then initialize
13980 CURRENT_FILE accordingly so that any macro definition found before the
13981 first DW_MACINFO_start_file can still be associated to the base file. */
13982
13983 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13984 mac_end = dwarf2_per_objfile->macinfo.buffer
13985 + dwarf2_per_objfile->macinfo.size;
13986
13987 do
13988 {
13989 /* Do we at least have room for a macinfo type byte? */
13990 if (mac_ptr >= mac_end)
13991 {
13992 /* Complaint is printed during the second pass as GDB will probably
13993 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13994 break;
13995 }
13996
13997 macinfo_type = read_1_byte (abfd, mac_ptr);
13998 mac_ptr++;
13999
14000 switch (macinfo_type)
14001 {
14002 /* A zero macinfo type indicates the end of the macro
14003 information. */
14004 case 0:
14005 break;
14006
14007 case DW_MACINFO_define:
14008 case DW_MACINFO_undef:
14009 /* Only skip the data by MAC_PTR. */
14010 {
14011 unsigned int bytes_read;
14012
14013 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14014 mac_ptr += bytes_read;
14015 read_direct_string (abfd, mac_ptr, &bytes_read);
14016 mac_ptr += bytes_read;
14017 }
14018 break;
14019
14020 case DW_MACINFO_start_file:
14021 {
14022 unsigned int bytes_read;
14023 int line, file;
14024
14025 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14026 mac_ptr += bytes_read;
14027 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14028 mac_ptr += bytes_read;
14029
14030 current_file = macro_start_file (file, line, current_file, comp_dir,
14031 lh, cu->objfile);
14032 }
14033 break;
14034
14035 case DW_MACINFO_end_file:
14036 /* No data to skip by MAC_PTR. */
14037 break;
14038
14039 case DW_MACINFO_vendor_ext:
14040 /* Only skip the data by MAC_PTR. */
14041 {
14042 unsigned int bytes_read;
14043
14044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14045 mac_ptr += bytes_read;
14046 read_direct_string (abfd, mac_ptr, &bytes_read);
14047 mac_ptr += bytes_read;
14048 }
14049 break;
14050
14051 default:
14052 break;
14053 }
14054 } while (macinfo_type != 0 && current_file == NULL);
14055
14056 /* Second pass: Process all entries.
14057
14058 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14059 command-line macro definitions/undefinitions. This flag is unset when we
14060 reach the first DW_MACINFO_start_file entry. */
14061
14062 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14063
14064 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14065 GDB is still reading the definitions from command line. First
14066 DW_MACINFO_start_file will need to be ignored as it was already executed
14067 to create CURRENT_FILE for the main source holding also the command line
14068 definitions. On first met DW_MACINFO_start_file this flag is reset to
14069 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14070
14071 at_commandline = 1;
14072
14073 do
14074 {
14075 /* Do we at least have room for a macinfo type byte? */
14076 if (mac_ptr >= mac_end)
14077 {
14078 dwarf2_macros_too_long_complaint ();
14079 break;
14080 }
14081
14082 macinfo_type = read_1_byte (abfd, mac_ptr);
14083 mac_ptr++;
14084
14085 switch (macinfo_type)
14086 {
14087 /* A zero macinfo type indicates the end of the macro
14088 information. */
14089 case 0:
14090 break;
14091
14092 case DW_MACINFO_define:
14093 case DW_MACINFO_undef:
14094 {
14095 unsigned int bytes_read;
14096 int line;
14097 char *body;
14098
14099 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14100 mac_ptr += bytes_read;
14101 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14102 mac_ptr += bytes_read;
14103
14104 if (! current_file)
14105 {
14106 /* DWARF violation as no main source is present. */
14107 complaint (&symfile_complaints,
14108 _("debug info with no main source gives macro %s "
14109 "on line %d: %s"),
14110 macinfo_type == DW_MACINFO_define ?
14111 _("definition") :
14112 macinfo_type == DW_MACINFO_undef ?
14113 _("undefinition") :
14114 _("something-or-other"), line, body);
14115 break;
14116 }
14117 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14118 complaint (&symfile_complaints,
14119 _("debug info gives %s macro %s with %s line %d: %s"),
14120 at_commandline ? _("command-line") : _("in-file"),
14121 macinfo_type == DW_MACINFO_define ?
14122 _("definition") :
14123 macinfo_type == DW_MACINFO_undef ?
14124 _("undefinition") :
14125 _("something-or-other"),
14126 line == 0 ? _("zero") : _("non-zero"), line, body);
14127
14128 if (macinfo_type == DW_MACINFO_define)
14129 parse_macro_definition (current_file, line, body);
14130 else if (macinfo_type == DW_MACINFO_undef)
14131 macro_undef (current_file, line, body);
14132 }
14133 break;
14134
14135 case DW_MACINFO_start_file:
14136 {
14137 unsigned int bytes_read;
14138 int line, file;
14139
14140 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14141 mac_ptr += bytes_read;
14142 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14143 mac_ptr += bytes_read;
14144
14145 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14146 complaint (&symfile_complaints,
14147 _("debug info gives source %d included "
14148 "from %s at %s line %d"),
14149 file, at_commandline ? _("command-line") : _("file"),
14150 line == 0 ? _("zero") : _("non-zero"), line);
14151
14152 if (at_commandline)
14153 {
14154 /* This DW_MACINFO_start_file was executed in the pass one. */
14155 at_commandline = 0;
14156 }
14157 else
14158 current_file = macro_start_file (file, line,
14159 current_file, comp_dir,
14160 lh, cu->objfile);
14161 }
14162 break;
14163
14164 case DW_MACINFO_end_file:
14165 if (! current_file)
14166 complaint (&symfile_complaints,
14167 _("macro debug info has an unmatched `close_file' directive"));
14168 else
14169 {
14170 current_file = current_file->included_by;
14171 if (! current_file)
14172 {
14173 enum dwarf_macinfo_record_type next_type;
14174
14175 /* GCC circa March 2002 doesn't produce the zero
14176 type byte marking the end of the compilation
14177 unit. Complain if it's not there, but exit no
14178 matter what. */
14179
14180 /* Do we at least have room for a macinfo type byte? */
14181 if (mac_ptr >= mac_end)
14182 {
14183 dwarf2_macros_too_long_complaint ();
14184 return;
14185 }
14186
14187 /* We don't increment mac_ptr here, so this is just
14188 a look-ahead. */
14189 next_type = read_1_byte (abfd, mac_ptr);
14190 if (next_type != 0)
14191 complaint (&symfile_complaints,
14192 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
14193
14194 return;
14195 }
14196 }
14197 break;
14198
14199 case DW_MACINFO_vendor_ext:
14200 {
14201 unsigned int bytes_read;
14202 int constant;
14203 char *string;
14204
14205 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14206 mac_ptr += bytes_read;
14207 string = read_direct_string (abfd, mac_ptr, &bytes_read);
14208 mac_ptr += bytes_read;
14209
14210 /* We don't recognize any vendor extensions. */
14211 }
14212 break;
14213 }
14214 } while (macinfo_type != 0);
14215 }
14216
14217 /* Check if the attribute's form is a DW_FORM_block*
14218 if so return true else false. */
14219 static int
14220 attr_form_is_block (struct attribute *attr)
14221 {
14222 return (attr == NULL ? 0 :
14223 attr->form == DW_FORM_block1
14224 || attr->form == DW_FORM_block2
14225 || attr->form == DW_FORM_block4
14226 || attr->form == DW_FORM_block
14227 || attr->form == DW_FORM_exprloc);
14228 }
14229
14230 /* Return non-zero if ATTR's value is a section offset --- classes
14231 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14232 You may use DW_UNSND (attr) to retrieve such offsets.
14233
14234 Section 7.5.4, "Attribute Encodings", explains that no attribute
14235 may have a value that belongs to more than one of these classes; it
14236 would be ambiguous if we did, because we use the same forms for all
14237 of them. */
14238 static int
14239 attr_form_is_section_offset (struct attribute *attr)
14240 {
14241 return (attr->form == DW_FORM_data4
14242 || attr->form == DW_FORM_data8
14243 || attr->form == DW_FORM_sec_offset);
14244 }
14245
14246
14247 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14248 zero otherwise. When this function returns true, you can apply
14249 dwarf2_get_attr_constant_value to it.
14250
14251 However, note that for some attributes you must check
14252 attr_form_is_section_offset before using this test. DW_FORM_data4
14253 and DW_FORM_data8 are members of both the constant class, and of
14254 the classes that contain offsets into other debug sections
14255 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14256 that, if an attribute's can be either a constant or one of the
14257 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14258 taken as section offsets, not constants. */
14259 static int
14260 attr_form_is_constant (struct attribute *attr)
14261 {
14262 switch (attr->form)
14263 {
14264 case DW_FORM_sdata:
14265 case DW_FORM_udata:
14266 case DW_FORM_data1:
14267 case DW_FORM_data2:
14268 case DW_FORM_data4:
14269 case DW_FORM_data8:
14270 return 1;
14271 default:
14272 return 0;
14273 }
14274 }
14275
14276 /* A helper function that fills in a dwarf2_loclist_baton. */
14277
14278 static void
14279 fill_in_loclist_baton (struct dwarf2_cu *cu,
14280 struct dwarf2_loclist_baton *baton,
14281 struct attribute *attr)
14282 {
14283 dwarf2_read_section (dwarf2_per_objfile->objfile,
14284 &dwarf2_per_objfile->loc);
14285
14286 baton->per_cu = cu->per_cu;
14287 gdb_assert (baton->per_cu);
14288 /* We don't know how long the location list is, but make sure we
14289 don't run off the edge of the section. */
14290 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14291 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14292 baton->base_address = cu->base_address;
14293 }
14294
14295 static void
14296 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14297 struct dwarf2_cu *cu)
14298 {
14299 if (attr_form_is_section_offset (attr)
14300 /* ".debug_loc" may not exist at all, or the offset may be outside
14301 the section. If so, fall through to the complaint in the
14302 other branch. */
14303 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
14304 {
14305 struct dwarf2_loclist_baton *baton;
14306
14307 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14308 sizeof (struct dwarf2_loclist_baton));
14309
14310 fill_in_loclist_baton (cu, baton, attr);
14311
14312 if (cu->base_known == 0)
14313 complaint (&symfile_complaints,
14314 _("Location list used without specifying the CU base address."));
14315
14316 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14317 SYMBOL_LOCATION_BATON (sym) = baton;
14318 }
14319 else
14320 {
14321 struct dwarf2_locexpr_baton *baton;
14322
14323 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14324 sizeof (struct dwarf2_locexpr_baton));
14325 baton->per_cu = cu->per_cu;
14326 gdb_assert (baton->per_cu);
14327
14328 if (attr_form_is_block (attr))
14329 {
14330 /* Note that we're just copying the block's data pointer
14331 here, not the actual data. We're still pointing into the
14332 info_buffer for SYM's objfile; right now we never release
14333 that buffer, but when we do clean up properly this may
14334 need to change. */
14335 baton->size = DW_BLOCK (attr)->size;
14336 baton->data = DW_BLOCK (attr)->data;
14337 }
14338 else
14339 {
14340 dwarf2_invalid_attrib_class_complaint ("location description",
14341 SYMBOL_NATURAL_NAME (sym));
14342 baton->size = 0;
14343 baton->data = NULL;
14344 }
14345
14346 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14347 SYMBOL_LOCATION_BATON (sym) = baton;
14348 }
14349 }
14350
14351 /* Return the OBJFILE associated with the compilation unit CU. If CU
14352 came from a separate debuginfo file, then the master objfile is
14353 returned. */
14354
14355 struct objfile *
14356 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14357 {
14358 struct objfile *objfile = per_cu->objfile;
14359
14360 /* Return the master objfile, so that we can report and look up the
14361 correct file containing this variable. */
14362 if (objfile->separate_debug_objfile_backlink)
14363 objfile = objfile->separate_debug_objfile_backlink;
14364
14365 return objfile;
14366 }
14367
14368 /* Return the address size given in the compilation unit header for CU. */
14369
14370 CORE_ADDR
14371 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14372 {
14373 if (per_cu->cu)
14374 return per_cu->cu->header.addr_size;
14375 else
14376 {
14377 /* If the CU is not currently read in, we re-read its header. */
14378 struct objfile *objfile = per_cu->objfile;
14379 struct dwarf2_per_objfile *per_objfile
14380 = objfile_data (objfile, dwarf2_objfile_data_key);
14381 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14382 struct comp_unit_head cu_header;
14383
14384 memset (&cu_header, 0, sizeof cu_header);
14385 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14386 return cu_header.addr_size;
14387 }
14388 }
14389
14390 /* Return the offset size given in the compilation unit header for CU. */
14391
14392 int
14393 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14394 {
14395 if (per_cu->cu)
14396 return per_cu->cu->header.offset_size;
14397 else
14398 {
14399 /* If the CU is not currently read in, we re-read its header. */
14400 struct objfile *objfile = per_cu->objfile;
14401 struct dwarf2_per_objfile *per_objfile
14402 = objfile_data (objfile, dwarf2_objfile_data_key);
14403 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14404 struct comp_unit_head cu_header;
14405
14406 memset (&cu_header, 0, sizeof cu_header);
14407 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14408 return cu_header.offset_size;
14409 }
14410 }
14411
14412 /* Return the text offset of the CU. The returned offset comes from
14413 this CU's objfile. If this objfile came from a separate debuginfo
14414 file, then the offset may be different from the corresponding
14415 offset in the parent objfile. */
14416
14417 CORE_ADDR
14418 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14419 {
14420 struct objfile *objfile = per_cu->objfile;
14421
14422 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14423 }
14424
14425 /* Locate the .debug_info compilation unit from CU's objfile which contains
14426 the DIE at OFFSET. Raises an error on failure. */
14427
14428 static struct dwarf2_per_cu_data *
14429 dwarf2_find_containing_comp_unit (unsigned int offset,
14430 struct objfile *objfile)
14431 {
14432 struct dwarf2_per_cu_data *this_cu;
14433 int low, high;
14434
14435 low = 0;
14436 high = dwarf2_per_objfile->n_comp_units - 1;
14437 while (high > low)
14438 {
14439 int mid = low + (high - low) / 2;
14440
14441 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14442 high = mid;
14443 else
14444 low = mid + 1;
14445 }
14446 gdb_assert (low == high);
14447 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14448 {
14449 if (low == 0)
14450 error (_("Dwarf Error: could not find partial DIE containing "
14451 "offset 0x%lx [in module %s]"),
14452 (long) offset, bfd_get_filename (objfile->obfd));
14453
14454 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14455 return dwarf2_per_objfile->all_comp_units[low-1];
14456 }
14457 else
14458 {
14459 this_cu = dwarf2_per_objfile->all_comp_units[low];
14460 if (low == dwarf2_per_objfile->n_comp_units - 1
14461 && offset >= this_cu->offset + this_cu->length)
14462 error (_("invalid dwarf2 offset %u"), offset);
14463 gdb_assert (offset < this_cu->offset + this_cu->length);
14464 return this_cu;
14465 }
14466 }
14467
14468 /* Locate the compilation unit from OBJFILE which is located at exactly
14469 OFFSET. Raises an error on failure. */
14470
14471 static struct dwarf2_per_cu_data *
14472 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14473 {
14474 struct dwarf2_per_cu_data *this_cu;
14475
14476 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14477 if (this_cu->offset != offset)
14478 error (_("no compilation unit with offset %u."), offset);
14479 return this_cu;
14480 }
14481
14482 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
14483
14484 static void
14485 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14486 {
14487 memset (cu, 0, sizeof (*cu));
14488 cu->objfile = objfile;
14489 obstack_init (&cu->comp_unit_obstack);
14490 }
14491
14492 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14493
14494 static void
14495 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14496 {
14497 struct attribute *attr;
14498
14499 /* Set the language we're debugging. */
14500 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14501 if (attr)
14502 set_cu_language (DW_UNSND (attr), cu);
14503 else
14504 set_cu_language (language_minimal, cu);
14505 }
14506
14507 /* Release one cached compilation unit, CU. We unlink it from the tree
14508 of compilation units, but we don't remove it from the read_in_chain;
14509 the caller is responsible for that.
14510 NOTE: DATA is a void * because this function is also used as a
14511 cleanup routine. */
14512
14513 static void
14514 free_one_comp_unit (void *data)
14515 {
14516 struct dwarf2_cu *cu = data;
14517
14518 if (cu->per_cu != NULL)
14519 cu->per_cu->cu = NULL;
14520 cu->per_cu = NULL;
14521
14522 obstack_free (&cu->comp_unit_obstack, NULL);
14523
14524 xfree (cu);
14525 }
14526
14527 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14528 when we're finished with it. We can't free the pointer itself, but be
14529 sure to unlink it from the cache. Also release any associated storage
14530 and perform cache maintenance.
14531
14532 Only used during partial symbol parsing. */
14533
14534 static void
14535 free_stack_comp_unit (void *data)
14536 {
14537 struct dwarf2_cu *cu = data;
14538
14539 obstack_free (&cu->comp_unit_obstack, NULL);
14540 cu->partial_dies = NULL;
14541
14542 if (cu->per_cu != NULL)
14543 {
14544 /* This compilation unit is on the stack in our caller, so we
14545 should not xfree it. Just unlink it. */
14546 cu->per_cu->cu = NULL;
14547 cu->per_cu = NULL;
14548
14549 /* If we had a per-cu pointer, then we may have other compilation
14550 units loaded, so age them now. */
14551 age_cached_comp_units ();
14552 }
14553 }
14554
14555 /* Free all cached compilation units. */
14556
14557 static void
14558 free_cached_comp_units (void *data)
14559 {
14560 struct dwarf2_per_cu_data *per_cu, **last_chain;
14561
14562 per_cu = dwarf2_per_objfile->read_in_chain;
14563 last_chain = &dwarf2_per_objfile->read_in_chain;
14564 while (per_cu != NULL)
14565 {
14566 struct dwarf2_per_cu_data *next_cu;
14567
14568 next_cu = per_cu->cu->read_in_chain;
14569
14570 free_one_comp_unit (per_cu->cu);
14571 *last_chain = next_cu;
14572
14573 per_cu = next_cu;
14574 }
14575 }
14576
14577 /* Increase the age counter on each cached compilation unit, and free
14578 any that are too old. */
14579
14580 static void
14581 age_cached_comp_units (void)
14582 {
14583 struct dwarf2_per_cu_data *per_cu, **last_chain;
14584
14585 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14586 per_cu = dwarf2_per_objfile->read_in_chain;
14587 while (per_cu != NULL)
14588 {
14589 per_cu->cu->last_used ++;
14590 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14591 dwarf2_mark (per_cu->cu);
14592 per_cu = per_cu->cu->read_in_chain;
14593 }
14594
14595 per_cu = dwarf2_per_objfile->read_in_chain;
14596 last_chain = &dwarf2_per_objfile->read_in_chain;
14597 while (per_cu != NULL)
14598 {
14599 struct dwarf2_per_cu_data *next_cu;
14600
14601 next_cu = per_cu->cu->read_in_chain;
14602
14603 if (!per_cu->cu->mark)
14604 {
14605 free_one_comp_unit (per_cu->cu);
14606 *last_chain = next_cu;
14607 }
14608 else
14609 last_chain = &per_cu->cu->read_in_chain;
14610
14611 per_cu = next_cu;
14612 }
14613 }
14614
14615 /* Remove a single compilation unit from the cache. */
14616
14617 static void
14618 free_one_cached_comp_unit (void *target_cu)
14619 {
14620 struct dwarf2_per_cu_data *per_cu, **last_chain;
14621
14622 per_cu = dwarf2_per_objfile->read_in_chain;
14623 last_chain = &dwarf2_per_objfile->read_in_chain;
14624 while (per_cu != NULL)
14625 {
14626 struct dwarf2_per_cu_data *next_cu;
14627
14628 next_cu = per_cu->cu->read_in_chain;
14629
14630 if (per_cu->cu == target_cu)
14631 {
14632 free_one_comp_unit (per_cu->cu);
14633 *last_chain = next_cu;
14634 break;
14635 }
14636 else
14637 last_chain = &per_cu->cu->read_in_chain;
14638
14639 per_cu = next_cu;
14640 }
14641 }
14642
14643 /* Release all extra memory associated with OBJFILE. */
14644
14645 void
14646 dwarf2_free_objfile (struct objfile *objfile)
14647 {
14648 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14649
14650 if (dwarf2_per_objfile == NULL)
14651 return;
14652
14653 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14654 free_cached_comp_units (NULL);
14655
14656 if (dwarf2_per_objfile->quick_file_names_table)
14657 htab_delete (dwarf2_per_objfile->quick_file_names_table);
14658
14659 /* Everything else should be on the objfile obstack. */
14660 }
14661
14662 /* A pair of DIE offset and GDB type pointer. We store these
14663 in a hash table separate from the DIEs, and preserve them
14664 when the DIEs are flushed out of cache. */
14665
14666 struct dwarf2_offset_and_type
14667 {
14668 unsigned int offset;
14669 struct type *type;
14670 };
14671
14672 /* Hash function for a dwarf2_offset_and_type. */
14673
14674 static hashval_t
14675 offset_and_type_hash (const void *item)
14676 {
14677 const struct dwarf2_offset_and_type *ofs = item;
14678
14679 return ofs->offset;
14680 }
14681
14682 /* Equality function for a dwarf2_offset_and_type. */
14683
14684 static int
14685 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14686 {
14687 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14688 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14689
14690 return ofs_lhs->offset == ofs_rhs->offset;
14691 }
14692
14693 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14694 table if necessary. For convenience, return TYPE.
14695
14696 The DIEs reading must have careful ordering to:
14697 * Not cause infite loops trying to read in DIEs as a prerequisite for
14698 reading current DIE.
14699 * Not trying to dereference contents of still incompletely read in types
14700 while reading in other DIEs.
14701 * Enable referencing still incompletely read in types just by a pointer to
14702 the type without accessing its fields.
14703
14704 Therefore caller should follow these rules:
14705 * Try to fetch any prerequisite types we may need to build this DIE type
14706 before building the type and calling set_die_type.
14707 * After building type call set_die_type for current DIE as soon as
14708 possible before fetching more types to complete the current type.
14709 * Make the type as complete as possible before fetching more types. */
14710
14711 static struct type *
14712 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14713 {
14714 struct dwarf2_offset_and_type **slot, ofs;
14715 struct objfile *objfile = cu->objfile;
14716 htab_t *type_hash_ptr;
14717
14718 /* For Ada types, make sure that the gnat-specific data is always
14719 initialized (if not already set). There are a few types where
14720 we should not be doing so, because the type-specific area is
14721 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14722 where the type-specific area is used to store the floatformat).
14723 But this is not a problem, because the gnat-specific information
14724 is actually not needed for these types. */
14725 if (need_gnat_info (cu)
14726 && TYPE_CODE (type) != TYPE_CODE_FUNC
14727 && TYPE_CODE (type) != TYPE_CODE_FLT
14728 && !HAVE_GNAT_AUX_INFO (type))
14729 INIT_GNAT_SPECIFIC (type);
14730
14731 if (cu->per_cu->from_debug_types)
14732 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14733 else
14734 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14735
14736 if (*type_hash_ptr == NULL)
14737 {
14738 *type_hash_ptr
14739 = htab_create_alloc_ex (127,
14740 offset_and_type_hash,
14741 offset_and_type_eq,
14742 NULL,
14743 &objfile->objfile_obstack,
14744 hashtab_obstack_allocate,
14745 dummy_obstack_deallocate);
14746 }
14747
14748 ofs.offset = die->offset;
14749 ofs.type = type;
14750 slot = (struct dwarf2_offset_and_type **)
14751 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14752 if (*slot)
14753 complaint (&symfile_complaints,
14754 _("A problem internal to GDB: DIE 0x%x has type already set"),
14755 die->offset);
14756 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14757 **slot = ofs;
14758 return type;
14759 }
14760
14761 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14762 table, or return NULL if the die does not have a saved type. */
14763
14764 static struct type *
14765 get_die_type_at_offset (unsigned int offset,
14766 struct dwarf2_per_cu_data *per_cu)
14767 {
14768 struct dwarf2_offset_and_type *slot, ofs;
14769 htab_t type_hash;
14770
14771 if (per_cu->from_debug_types)
14772 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14773 else
14774 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14775 if (type_hash == NULL)
14776 return NULL;
14777
14778 ofs.offset = offset;
14779 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14780 if (slot)
14781 return slot->type;
14782 else
14783 return NULL;
14784 }
14785
14786 /* Look up the type for DIE in the appropriate type_hash table,
14787 or return NULL if DIE does not have a saved type. */
14788
14789 static struct type *
14790 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14791 {
14792 return get_die_type_at_offset (die->offset, cu->per_cu);
14793 }
14794
14795 /* Add a dependence relationship from CU to REF_PER_CU. */
14796
14797 static void
14798 dwarf2_add_dependence (struct dwarf2_cu *cu,
14799 struct dwarf2_per_cu_data *ref_per_cu)
14800 {
14801 void **slot;
14802
14803 if (cu->dependencies == NULL)
14804 cu->dependencies
14805 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14806 NULL, &cu->comp_unit_obstack,
14807 hashtab_obstack_allocate,
14808 dummy_obstack_deallocate);
14809
14810 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14811 if (*slot == NULL)
14812 *slot = ref_per_cu;
14813 }
14814
14815 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14816 Set the mark field in every compilation unit in the
14817 cache that we must keep because we are keeping CU. */
14818
14819 static int
14820 dwarf2_mark_helper (void **slot, void *data)
14821 {
14822 struct dwarf2_per_cu_data *per_cu;
14823
14824 per_cu = (struct dwarf2_per_cu_data *) *slot;
14825 if (per_cu->cu->mark)
14826 return 1;
14827 per_cu->cu->mark = 1;
14828
14829 if (per_cu->cu->dependencies != NULL)
14830 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14831
14832 return 1;
14833 }
14834
14835 /* Set the mark field in CU and in every other compilation unit in the
14836 cache that we must keep because we are keeping CU. */
14837
14838 static void
14839 dwarf2_mark (struct dwarf2_cu *cu)
14840 {
14841 if (cu->mark)
14842 return;
14843 cu->mark = 1;
14844 if (cu->dependencies != NULL)
14845 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14846 }
14847
14848 static void
14849 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14850 {
14851 while (per_cu)
14852 {
14853 per_cu->cu->mark = 0;
14854 per_cu = per_cu->cu->read_in_chain;
14855 }
14856 }
14857
14858 /* Trivial hash function for partial_die_info: the hash value of a DIE
14859 is its offset in .debug_info for this objfile. */
14860
14861 static hashval_t
14862 partial_die_hash (const void *item)
14863 {
14864 const struct partial_die_info *part_die = item;
14865
14866 return part_die->offset;
14867 }
14868
14869 /* Trivial comparison function for partial_die_info structures: two DIEs
14870 are equal if they have the same offset. */
14871
14872 static int
14873 partial_die_eq (const void *item_lhs, const void *item_rhs)
14874 {
14875 const struct partial_die_info *part_die_lhs = item_lhs;
14876 const struct partial_die_info *part_die_rhs = item_rhs;
14877
14878 return part_die_lhs->offset == part_die_rhs->offset;
14879 }
14880
14881 static struct cmd_list_element *set_dwarf2_cmdlist;
14882 static struct cmd_list_element *show_dwarf2_cmdlist;
14883
14884 static void
14885 set_dwarf2_cmd (char *args, int from_tty)
14886 {
14887 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14888 }
14889
14890 static void
14891 show_dwarf2_cmd (char *args, int from_tty)
14892 {
14893 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14894 }
14895
14896 /* If section described by INFO was mmapped, munmap it now. */
14897
14898 static void
14899 munmap_section_buffer (struct dwarf2_section_info *info)
14900 {
14901 if (info->was_mmapped)
14902 {
14903 #ifdef HAVE_MMAP
14904 intptr_t begin = (intptr_t) info->buffer;
14905 intptr_t map_begin = begin & ~(pagesize - 1);
14906 size_t map_length = info->size + begin - map_begin;
14907
14908 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14909 #else
14910 /* Without HAVE_MMAP, we should never be here to begin with. */
14911 gdb_assert_not_reached ("no mmap support");
14912 #endif
14913 }
14914 }
14915
14916 /* munmap debug sections for OBJFILE, if necessary. */
14917
14918 static void
14919 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14920 {
14921 struct dwarf2_per_objfile *data = d;
14922
14923 /* This is sorted according to the order they're defined in to make it easier
14924 to keep in sync. */
14925 munmap_section_buffer (&data->info);
14926 munmap_section_buffer (&data->abbrev);
14927 munmap_section_buffer (&data->line);
14928 munmap_section_buffer (&data->loc);
14929 munmap_section_buffer (&data->macinfo);
14930 munmap_section_buffer (&data->str);
14931 munmap_section_buffer (&data->ranges);
14932 munmap_section_buffer (&data->types);
14933 munmap_section_buffer (&data->frame);
14934 munmap_section_buffer (&data->eh_frame);
14935 munmap_section_buffer (&data->gdb_index);
14936 }
14937
14938 \f
14939 /* The "save gdb-index" command. */
14940
14941 /* The contents of the hash table we create when building the string
14942 table. */
14943 struct strtab_entry
14944 {
14945 offset_type offset;
14946 const char *str;
14947 };
14948
14949 /* Hash function for a strtab_entry. */
14950
14951 static hashval_t
14952 hash_strtab_entry (const void *e)
14953 {
14954 const struct strtab_entry *entry = e;
14955 return mapped_index_string_hash (entry->str);
14956 }
14957
14958 /* Equality function for a strtab_entry. */
14959
14960 static int
14961 eq_strtab_entry (const void *a, const void *b)
14962 {
14963 const struct strtab_entry *ea = a;
14964 const struct strtab_entry *eb = b;
14965 return !strcmp (ea->str, eb->str);
14966 }
14967
14968 /* Create a strtab_entry hash table. */
14969
14970 static htab_t
14971 create_strtab (void)
14972 {
14973 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14974 xfree, xcalloc, xfree);
14975 }
14976
14977 /* Add a string to the constant pool. Return the string's offset in
14978 host order. */
14979
14980 static offset_type
14981 add_string (htab_t table, struct obstack *cpool, const char *str)
14982 {
14983 void **slot;
14984 struct strtab_entry entry;
14985 struct strtab_entry *result;
14986
14987 entry.str = str;
14988 slot = htab_find_slot (table, &entry, INSERT);
14989 if (*slot)
14990 result = *slot;
14991 else
14992 {
14993 result = XNEW (struct strtab_entry);
14994 result->offset = obstack_object_size (cpool);
14995 result->str = str;
14996 obstack_grow_str0 (cpool, str);
14997 *slot = result;
14998 }
14999 return result->offset;
15000 }
15001
15002 /* An entry in the symbol table. */
15003 struct symtab_index_entry
15004 {
15005 /* The name of the symbol. */
15006 const char *name;
15007 /* The offset of the name in the constant pool. */
15008 offset_type index_offset;
15009 /* A sorted vector of the indices of all the CUs that hold an object
15010 of this name. */
15011 VEC (offset_type) *cu_indices;
15012 };
15013
15014 /* The symbol table. This is a power-of-2-sized hash table. */
15015 struct mapped_symtab
15016 {
15017 offset_type n_elements;
15018 offset_type size;
15019 struct symtab_index_entry **data;
15020 };
15021
15022 /* Hash function for a symtab_index_entry. */
15023
15024 static hashval_t
15025 hash_symtab_entry (const void *e)
15026 {
15027 const struct symtab_index_entry *entry = e;
15028 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15029 sizeof (offset_type) * VEC_length (offset_type,
15030 entry->cu_indices),
15031 0);
15032 }
15033
15034 /* Equality function for a symtab_index_entry. */
15035
15036 static int
15037 eq_symtab_entry (const void *a, const void *b)
15038 {
15039 const struct symtab_index_entry *ea = a;
15040 const struct symtab_index_entry *eb = b;
15041 int len = VEC_length (offset_type, ea->cu_indices);
15042 if (len != VEC_length (offset_type, eb->cu_indices))
15043 return 0;
15044 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15045 VEC_address (offset_type, eb->cu_indices),
15046 sizeof (offset_type) * len);
15047 }
15048
15049 /* Destroy a symtab_index_entry. */
15050
15051 static void
15052 delete_symtab_entry (void *p)
15053 {
15054 struct symtab_index_entry *entry = p;
15055 VEC_free (offset_type, entry->cu_indices);
15056 xfree (entry);
15057 }
15058
15059 /* Create a hash table holding symtab_index_entry objects. */
15060
15061 static htab_t
15062 create_symbol_hash_table (void)
15063 {
15064 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15065 delete_symtab_entry, xcalloc, xfree);
15066 }
15067
15068 /* Create a new mapped symtab object. */
15069
15070 static struct mapped_symtab *
15071 create_mapped_symtab (void)
15072 {
15073 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15074 symtab->n_elements = 0;
15075 symtab->size = 1024;
15076 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15077 return symtab;
15078 }
15079
15080 /* Destroy a mapped_symtab. */
15081
15082 static void
15083 cleanup_mapped_symtab (void *p)
15084 {
15085 struct mapped_symtab *symtab = p;
15086 /* The contents of the array are freed when the other hash table is
15087 destroyed. */
15088 xfree (symtab->data);
15089 xfree (symtab);
15090 }
15091
15092 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15093 the slot. */
15094
15095 static struct symtab_index_entry **
15096 find_slot (struct mapped_symtab *symtab, const char *name)
15097 {
15098 offset_type index, step, hash = mapped_index_string_hash (name);
15099
15100 index = hash & (symtab->size - 1);
15101 step = ((hash * 17) & (symtab->size - 1)) | 1;
15102
15103 for (;;)
15104 {
15105 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15106 return &symtab->data[index];
15107 index = (index + step) & (symtab->size - 1);
15108 }
15109 }
15110
15111 /* Expand SYMTAB's hash table. */
15112
15113 static void
15114 hash_expand (struct mapped_symtab *symtab)
15115 {
15116 offset_type old_size = symtab->size;
15117 offset_type i;
15118 struct symtab_index_entry **old_entries = symtab->data;
15119
15120 symtab->size *= 2;
15121 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15122
15123 for (i = 0; i < old_size; ++i)
15124 {
15125 if (old_entries[i])
15126 {
15127 struct symtab_index_entry **slot = find_slot (symtab,
15128 old_entries[i]->name);
15129 *slot = old_entries[i];
15130 }
15131 }
15132
15133 xfree (old_entries);
15134 }
15135
15136 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15137 is the index of the CU in which the symbol appears. */
15138
15139 static void
15140 add_index_entry (struct mapped_symtab *symtab, const char *name,
15141 offset_type cu_index)
15142 {
15143 struct symtab_index_entry **slot;
15144
15145 ++symtab->n_elements;
15146 if (4 * symtab->n_elements / 3 >= symtab->size)
15147 hash_expand (symtab);
15148
15149 slot = find_slot (symtab, name);
15150 if (!*slot)
15151 {
15152 *slot = XNEW (struct symtab_index_entry);
15153 (*slot)->name = name;
15154 (*slot)->cu_indices = NULL;
15155 }
15156 /* Don't push an index twice. Due to how we add entries we only
15157 have to check the last one. */
15158 if (VEC_empty (offset_type, (*slot)->cu_indices)
15159 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15160 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15161 }
15162
15163 /* Add a vector of indices to the constant pool. */
15164
15165 static offset_type
15166 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15167 struct symtab_index_entry *entry)
15168 {
15169 void **slot;
15170
15171 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15172 if (!*slot)
15173 {
15174 offset_type len = VEC_length (offset_type, entry->cu_indices);
15175 offset_type val = MAYBE_SWAP (len);
15176 offset_type iter;
15177 int i;
15178
15179 *slot = entry;
15180 entry->index_offset = obstack_object_size (cpool);
15181
15182 obstack_grow (cpool, &val, sizeof (val));
15183 for (i = 0;
15184 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15185 ++i)
15186 {
15187 val = MAYBE_SWAP (iter);
15188 obstack_grow (cpool, &val, sizeof (val));
15189 }
15190 }
15191 else
15192 {
15193 struct symtab_index_entry *old_entry = *slot;
15194 entry->index_offset = old_entry->index_offset;
15195 entry = old_entry;
15196 }
15197 return entry->index_offset;
15198 }
15199
15200 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15201 constant pool entries going into the obstack CPOOL. */
15202
15203 static void
15204 write_hash_table (struct mapped_symtab *symtab,
15205 struct obstack *output, struct obstack *cpool)
15206 {
15207 offset_type i;
15208 htab_t symbol_hash_table;
15209 htab_t str_table;
15210
15211 symbol_hash_table = create_symbol_hash_table ();
15212 str_table = create_strtab ();
15213
15214 /* We add all the index vectors to the constant pool first, to
15215 ensure alignment is ok. */
15216 for (i = 0; i < symtab->size; ++i)
15217 {
15218 if (symtab->data[i])
15219 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15220 }
15221
15222 /* Now write out the hash table. */
15223 for (i = 0; i < symtab->size; ++i)
15224 {
15225 offset_type str_off, vec_off;
15226
15227 if (symtab->data[i])
15228 {
15229 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15230 vec_off = symtab->data[i]->index_offset;
15231 }
15232 else
15233 {
15234 /* While 0 is a valid constant pool index, it is not valid
15235 to have 0 for both offsets. */
15236 str_off = 0;
15237 vec_off = 0;
15238 }
15239
15240 str_off = MAYBE_SWAP (str_off);
15241 vec_off = MAYBE_SWAP (vec_off);
15242
15243 obstack_grow (output, &str_off, sizeof (str_off));
15244 obstack_grow (output, &vec_off, sizeof (vec_off));
15245 }
15246
15247 htab_delete (str_table);
15248 htab_delete (symbol_hash_table);
15249 }
15250
15251 /* Struct to map psymtab to CU index in the index file. */
15252 struct psymtab_cu_index_map
15253 {
15254 struct partial_symtab *psymtab;
15255 unsigned int cu_index;
15256 };
15257
15258 static hashval_t
15259 hash_psymtab_cu_index (const void *item)
15260 {
15261 const struct psymtab_cu_index_map *map = item;
15262
15263 return htab_hash_pointer (map->psymtab);
15264 }
15265
15266 static int
15267 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15268 {
15269 const struct psymtab_cu_index_map *lhs = item_lhs;
15270 const struct psymtab_cu_index_map *rhs = item_rhs;
15271
15272 return lhs->psymtab == rhs->psymtab;
15273 }
15274
15275 /* Helper struct for building the address table. */
15276 struct addrmap_index_data
15277 {
15278 struct objfile *objfile;
15279 struct obstack *addr_obstack;
15280 htab_t cu_index_htab;
15281
15282 /* Non-zero if the previous_* fields are valid.
15283 We can't write an entry until we see the next entry (since it is only then
15284 that we know the end of the entry). */
15285 int previous_valid;
15286 /* Index of the CU in the table of all CUs in the index file. */
15287 unsigned int previous_cu_index;
15288 /* Start address of the CU. */
15289 CORE_ADDR previous_cu_start;
15290 };
15291
15292 /* Write an address entry to OBSTACK. */
15293
15294 static void
15295 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15296 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15297 {
15298 offset_type cu_index_to_write;
15299 char addr[8];
15300 CORE_ADDR baseaddr;
15301
15302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15303
15304 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15305 obstack_grow (obstack, addr, 8);
15306 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15307 obstack_grow (obstack, addr, 8);
15308 cu_index_to_write = MAYBE_SWAP (cu_index);
15309 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15310 }
15311
15312 /* Worker function for traversing an addrmap to build the address table. */
15313
15314 static int
15315 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15316 {
15317 struct addrmap_index_data *data = datap;
15318 struct partial_symtab *pst = obj;
15319 offset_type cu_index;
15320 void **slot;
15321
15322 if (data->previous_valid)
15323 add_address_entry (data->objfile, data->addr_obstack,
15324 data->previous_cu_start, start_addr,
15325 data->previous_cu_index);
15326
15327 data->previous_cu_start = start_addr;
15328 if (pst != NULL)
15329 {
15330 struct psymtab_cu_index_map find_map, *map;
15331 find_map.psymtab = pst;
15332 map = htab_find (data->cu_index_htab, &find_map);
15333 gdb_assert (map != NULL);
15334 data->previous_cu_index = map->cu_index;
15335 data->previous_valid = 1;
15336 }
15337 else
15338 data->previous_valid = 0;
15339
15340 return 0;
15341 }
15342
15343 /* Write OBJFILE's address map to OBSTACK.
15344 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15345 in the index file. */
15346
15347 static void
15348 write_address_map (struct objfile *objfile, struct obstack *obstack,
15349 htab_t cu_index_htab)
15350 {
15351 struct addrmap_index_data addrmap_index_data;
15352
15353 /* When writing the address table, we have to cope with the fact that
15354 the addrmap iterator only provides the start of a region; we have to
15355 wait until the next invocation to get the start of the next region. */
15356
15357 addrmap_index_data.objfile = objfile;
15358 addrmap_index_data.addr_obstack = obstack;
15359 addrmap_index_data.cu_index_htab = cu_index_htab;
15360 addrmap_index_data.previous_valid = 0;
15361
15362 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15363 &addrmap_index_data);
15364
15365 /* It's highly unlikely the last entry (end address = 0xff...ff)
15366 is valid, but we should still handle it.
15367 The end address is recorded as the start of the next region, but that
15368 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15369 anyway. */
15370 if (addrmap_index_data.previous_valid)
15371 add_address_entry (objfile, obstack,
15372 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15373 addrmap_index_data.previous_cu_index);
15374 }
15375
15376 /* Add a list of partial symbols to SYMTAB. */
15377
15378 static void
15379 write_psymbols (struct mapped_symtab *symtab,
15380 htab_t psyms_seen,
15381 struct partial_symbol **psymp,
15382 int count,
15383 offset_type cu_index,
15384 int is_static)
15385 {
15386 for (; count-- > 0; ++psymp)
15387 {
15388 void **slot, *lookup;
15389
15390 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15391 error (_("Ada is not currently supported by the index"));
15392
15393 /* We only want to add a given psymbol once. However, we also
15394 want to account for whether it is global or static. So, we
15395 may add it twice, using slightly different values. */
15396 if (is_static)
15397 {
15398 uintptr_t val = 1 | (uintptr_t) *psymp;
15399
15400 lookup = (void *) val;
15401 }
15402 else
15403 lookup = *psymp;
15404
15405 /* Only add a given psymbol once. */
15406 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15407 if (!*slot)
15408 {
15409 *slot = lookup;
15410 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15411 }
15412 }
15413 }
15414
15415 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15416 exception if there is an error. */
15417
15418 static void
15419 write_obstack (FILE *file, struct obstack *obstack)
15420 {
15421 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15422 file)
15423 != obstack_object_size (obstack))
15424 error (_("couldn't data write to file"));
15425 }
15426
15427 /* Unlink a file if the argument is not NULL. */
15428
15429 static void
15430 unlink_if_set (void *p)
15431 {
15432 char **filename = p;
15433 if (*filename)
15434 unlink (*filename);
15435 }
15436
15437 /* A helper struct used when iterating over debug_types. */
15438 struct signatured_type_index_data
15439 {
15440 struct objfile *objfile;
15441 struct mapped_symtab *symtab;
15442 struct obstack *types_list;
15443 htab_t psyms_seen;
15444 int cu_index;
15445 };
15446
15447 /* A helper function that writes a single signatured_type to an
15448 obstack. */
15449
15450 static int
15451 write_one_signatured_type (void **slot, void *d)
15452 {
15453 struct signatured_type_index_data *info = d;
15454 struct signatured_type *entry = (struct signatured_type *) *slot;
15455 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15456 struct partial_symtab *psymtab = per_cu->v.psymtab;
15457 gdb_byte val[8];
15458
15459 write_psymbols (info->symtab,
15460 info->psyms_seen,
15461 info->objfile->global_psymbols.list + psymtab->globals_offset,
15462 psymtab->n_global_syms, info->cu_index,
15463 0);
15464 write_psymbols (info->symtab,
15465 info->psyms_seen,
15466 info->objfile->static_psymbols.list + psymtab->statics_offset,
15467 psymtab->n_static_syms, info->cu_index,
15468 1);
15469
15470 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15471 obstack_grow (info->types_list, val, 8);
15472 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15473 obstack_grow (info->types_list, val, 8);
15474 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15475 obstack_grow (info->types_list, val, 8);
15476
15477 ++info->cu_index;
15478
15479 return 1;
15480 }
15481
15482 /* A cleanup function for an htab_t. */
15483
15484 static void
15485 cleanup_htab (void *arg)
15486 {
15487 htab_delete (arg);
15488 }
15489
15490 /* Create an index file for OBJFILE in the directory DIR. */
15491
15492 static void
15493 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15494 {
15495 struct cleanup *cleanup;
15496 char *filename, *cleanup_filename;
15497 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15498 struct obstack cu_list, types_cu_list;
15499 int i;
15500 FILE *out_file;
15501 struct mapped_symtab *symtab;
15502 offset_type val, size_of_contents, total_len;
15503 struct stat st;
15504 char buf[8];
15505 htab_t psyms_seen;
15506 htab_t cu_index_htab;
15507 struct psymtab_cu_index_map *psymtab_cu_index_map;
15508
15509 if (!objfile->psymtabs)
15510 return;
15511 if (dwarf2_per_objfile->using_index)
15512 error (_("Cannot use an index to create the index"));
15513
15514 if (stat (objfile->name, &st) < 0)
15515 perror_with_name (objfile->name);
15516
15517 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15518 INDEX_SUFFIX, (char *) NULL);
15519 cleanup = make_cleanup (xfree, filename);
15520
15521 out_file = fopen (filename, "wb");
15522 if (!out_file)
15523 error (_("Can't open `%s' for writing"), filename);
15524
15525 cleanup_filename = filename;
15526 make_cleanup (unlink_if_set, &cleanup_filename);
15527
15528 symtab = create_mapped_symtab ();
15529 make_cleanup (cleanup_mapped_symtab, symtab);
15530
15531 obstack_init (&addr_obstack);
15532 make_cleanup_obstack_free (&addr_obstack);
15533
15534 obstack_init (&cu_list);
15535 make_cleanup_obstack_free (&cu_list);
15536
15537 obstack_init (&types_cu_list);
15538 make_cleanup_obstack_free (&types_cu_list);
15539
15540 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15541 NULL, xcalloc, xfree);
15542 make_cleanup (cleanup_htab, psyms_seen);
15543
15544 /* While we're scanning CU's create a table that maps a psymtab pointer
15545 (which is what addrmap records) to its index (which is what is recorded
15546 in the index file). This will later be needed to write the address
15547 table. */
15548 cu_index_htab = htab_create_alloc (100,
15549 hash_psymtab_cu_index,
15550 eq_psymtab_cu_index,
15551 NULL, xcalloc, xfree);
15552 make_cleanup (cleanup_htab, cu_index_htab);
15553 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15554 xmalloc (sizeof (struct psymtab_cu_index_map)
15555 * dwarf2_per_objfile->n_comp_units);
15556 make_cleanup (xfree, psymtab_cu_index_map);
15557
15558 /* The CU list is already sorted, so we don't need to do additional
15559 work here. Also, the debug_types entries do not appear in
15560 all_comp_units, but only in their own hash table. */
15561 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15562 {
15563 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15564 struct partial_symtab *psymtab = per_cu->v.psymtab;
15565 gdb_byte val[8];
15566 struct psymtab_cu_index_map *map;
15567 void **slot;
15568
15569 write_psymbols (symtab,
15570 psyms_seen,
15571 objfile->global_psymbols.list + psymtab->globals_offset,
15572 psymtab->n_global_syms, i,
15573 0);
15574 write_psymbols (symtab,
15575 psyms_seen,
15576 objfile->static_psymbols.list + psymtab->statics_offset,
15577 psymtab->n_static_syms, i,
15578 1);
15579
15580 map = &psymtab_cu_index_map[i];
15581 map->psymtab = psymtab;
15582 map->cu_index = i;
15583 slot = htab_find_slot (cu_index_htab, map, INSERT);
15584 gdb_assert (slot != NULL);
15585 gdb_assert (*slot == NULL);
15586 *slot = map;
15587
15588 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15589 obstack_grow (&cu_list, val, 8);
15590 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15591 obstack_grow (&cu_list, val, 8);
15592 }
15593
15594 /* Dump the address map. */
15595 write_address_map (objfile, &addr_obstack, cu_index_htab);
15596
15597 /* Write out the .debug_type entries, if any. */
15598 if (dwarf2_per_objfile->signatured_types)
15599 {
15600 struct signatured_type_index_data sig_data;
15601
15602 sig_data.objfile = objfile;
15603 sig_data.symtab = symtab;
15604 sig_data.types_list = &types_cu_list;
15605 sig_data.psyms_seen = psyms_seen;
15606 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15607 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15608 write_one_signatured_type, &sig_data);
15609 }
15610
15611 obstack_init (&constant_pool);
15612 make_cleanup_obstack_free (&constant_pool);
15613 obstack_init (&symtab_obstack);
15614 make_cleanup_obstack_free (&symtab_obstack);
15615 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15616
15617 obstack_init (&contents);
15618 make_cleanup_obstack_free (&contents);
15619 size_of_contents = 6 * sizeof (offset_type);
15620 total_len = size_of_contents;
15621
15622 /* The version number. */
15623 val = MAYBE_SWAP (3);
15624 obstack_grow (&contents, &val, sizeof (val));
15625
15626 /* The offset of the CU list from the start of the file. */
15627 val = MAYBE_SWAP (total_len);
15628 obstack_grow (&contents, &val, sizeof (val));
15629 total_len += obstack_object_size (&cu_list);
15630
15631 /* The offset of the types CU list from the start of the file. */
15632 val = MAYBE_SWAP (total_len);
15633 obstack_grow (&contents, &val, sizeof (val));
15634 total_len += obstack_object_size (&types_cu_list);
15635
15636 /* The offset of the address table from the start of the file. */
15637 val = MAYBE_SWAP (total_len);
15638 obstack_grow (&contents, &val, sizeof (val));
15639 total_len += obstack_object_size (&addr_obstack);
15640
15641 /* The offset of the symbol table from the start of the file. */
15642 val = MAYBE_SWAP (total_len);
15643 obstack_grow (&contents, &val, sizeof (val));
15644 total_len += obstack_object_size (&symtab_obstack);
15645
15646 /* The offset of the constant pool from the start of the file. */
15647 val = MAYBE_SWAP (total_len);
15648 obstack_grow (&contents, &val, sizeof (val));
15649 total_len += obstack_object_size (&constant_pool);
15650
15651 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15652
15653 write_obstack (out_file, &contents);
15654 write_obstack (out_file, &cu_list);
15655 write_obstack (out_file, &types_cu_list);
15656 write_obstack (out_file, &addr_obstack);
15657 write_obstack (out_file, &symtab_obstack);
15658 write_obstack (out_file, &constant_pool);
15659
15660 fclose (out_file);
15661
15662 /* We want to keep the file, so we set cleanup_filename to NULL
15663 here. See unlink_if_set. */
15664 cleanup_filename = NULL;
15665
15666 do_cleanups (cleanup);
15667 }
15668
15669 /* The mapped index file format is designed to be directly mmap()able
15670 on any architecture. In most cases, a datum is represented using a
15671 little-endian 32-bit integer value, called an offset_type. Big
15672 endian machines must byte-swap the values before using them.
15673 Exceptions to this rule are noted. The data is laid out such that
15674 alignment is always respected.
15675
15676 A mapped index consists of several sections.
15677
15678 1. The file header. This is a sequence of values, of offset_type
15679 unless otherwise noted:
15680
15681 [0] The version number, currently 3. Versions 1 and 2 are
15682 obsolete.
15683 [1] The offset, from the start of the file, of the CU list.
15684 [2] The offset, from the start of the file, of the types CU list.
15685 Note that this section can be empty, in which case this offset will
15686 be equal to the next offset.
15687 [3] The offset, from the start of the file, of the address section.
15688 [4] The offset, from the start of the file, of the symbol table.
15689 [5] The offset, from the start of the file, of the constant pool.
15690
15691 2. The CU list. This is a sequence of pairs of 64-bit
15692 little-endian values, sorted by the CU offset. The first element
15693 in each pair is the offset of a CU in the .debug_info section. The
15694 second element in each pair is the length of that CU. References
15695 to a CU elsewhere in the map are done using a CU index, which is
15696 just the 0-based index into this table. Note that if there are
15697 type CUs, then conceptually CUs and type CUs form a single list for
15698 the purposes of CU indices.
15699
15700 3. The types CU list. This is a sequence of triplets of 64-bit
15701 little-endian values. In a triplet, the first value is the CU
15702 offset, the second value is the type offset in the CU, and the
15703 third value is the type signature. The types CU list is not
15704 sorted.
15705
15706 4. The address section. The address section consists of a sequence
15707 of address entries. Each address entry has three elements.
15708 [0] The low address. This is a 64-bit little-endian value.
15709 [1] The high address. This is a 64-bit little-endian value.
15710 Like DW_AT_high_pc, the value is one byte beyond the end.
15711 [2] The CU index. This is an offset_type value.
15712
15713 5. The symbol table. This is a hash table. The size of the hash
15714 table is always a power of 2. The initial hash and the step are
15715 currently defined by the `find_slot' function.
15716
15717 Each slot in the hash table consists of a pair of offset_type
15718 values. The first value is the offset of the symbol's name in the
15719 constant pool. The second value is the offset of the CU vector in
15720 the constant pool.
15721
15722 If both values are 0, then this slot in the hash table is empty.
15723 This is ok because while 0 is a valid constant pool index, it
15724 cannot be a valid index for both a string and a CU vector.
15725
15726 A string in the constant pool is stored as a \0-terminated string,
15727 as you'd expect.
15728
15729 A CU vector in the constant pool is a sequence of offset_type
15730 values. The first value is the number of CU indices in the vector.
15731 Each subsequent value is the index of a CU in the CU list. This
15732 element in the hash table is used to indicate which CUs define the
15733 symbol.
15734
15735 6. The constant pool. This is simply a bunch of bytes. It is
15736 organized so that alignment is correct: CU vectors are stored
15737 first, followed by strings. */
15738
15739 static void
15740 save_gdb_index_command (char *arg, int from_tty)
15741 {
15742 struct objfile *objfile;
15743
15744 if (!arg || !*arg)
15745 error (_("usage: save gdb-index DIRECTORY"));
15746
15747 ALL_OBJFILES (objfile)
15748 {
15749 struct stat st;
15750
15751 /* If the objfile does not correspond to an actual file, skip it. */
15752 if (stat (objfile->name, &st) < 0)
15753 continue;
15754
15755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15756 if (dwarf2_per_objfile)
15757 {
15758 volatile struct gdb_exception except;
15759
15760 TRY_CATCH (except, RETURN_MASK_ERROR)
15761 {
15762 write_psymtabs_to_index (objfile, arg);
15763 }
15764 if (except.reason < 0)
15765 exception_fprintf (gdb_stderr, except,
15766 _("Error while writing index for `%s': "),
15767 objfile->name);
15768 }
15769 }
15770 }
15771
15772 \f
15773
15774 int dwarf2_always_disassemble;
15775
15776 static void
15777 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15778 struct cmd_list_element *c, const char *value)
15779 {
15780 fprintf_filtered (file, _("\
15781 Whether to always disassemble DWARF expressions is %s.\n"),
15782 value);
15783 }
15784
15785 void _initialize_dwarf2_read (void);
15786
15787 void
15788 _initialize_dwarf2_read (void)
15789 {
15790 struct cmd_list_element *c;
15791
15792 dwarf2_objfile_data_key
15793 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15794
15795 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15796 Set DWARF 2 specific variables.\n\
15797 Configure DWARF 2 variables such as the cache size"),
15798 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15799 0/*allow-unknown*/, &maintenance_set_cmdlist);
15800
15801 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15802 Show DWARF 2 specific variables\n\
15803 Show DWARF 2 variables such as the cache size"),
15804 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15805 0/*allow-unknown*/, &maintenance_show_cmdlist);
15806
15807 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15808 &dwarf2_max_cache_age, _("\
15809 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15810 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15811 A higher limit means that cached compilation units will be stored\n\
15812 in memory longer, and more total memory will be used. Zero disables\n\
15813 caching, which can slow down startup."),
15814 NULL,
15815 show_dwarf2_max_cache_age,
15816 &set_dwarf2_cmdlist,
15817 &show_dwarf2_cmdlist);
15818
15819 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15820 &dwarf2_always_disassemble, _("\
15821 Set whether `info address' always disassembles DWARF expressions."), _("\
15822 Show whether `info address' always disassembles DWARF expressions."), _("\
15823 When enabled, DWARF expressions are always printed in an assembly-like\n\
15824 syntax. When disabled, expressions will be printed in a more\n\
15825 conversational style, when possible."),
15826 NULL,
15827 show_dwarf2_always_disassemble,
15828 &set_dwarf2_cmdlist,
15829 &show_dwarf2_cmdlist);
15830
15831 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15832 Set debugging of the dwarf2 DIE reader."), _("\
15833 Show debugging of the dwarf2 DIE reader."), _("\
15834 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15835 The value is the maximum depth to print."),
15836 NULL,
15837 NULL,
15838 &setdebuglist, &showdebuglist);
15839
15840 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15841 _("\
15842 Save a .gdb-index file.\n\
15843 Usage: save gdb-index DIRECTORY"),
15844 &save_cmdlist);
15845 set_cmd_completer (c, filename_completer);
15846 }
This page took 0.351915 seconds and 5 git commands to generate.