1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "exceptions.h"
60 #include "completer.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
77 typedef struct symbol *symbolp;
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
93 static const struct objfile_data *dwarf2_objfile_data_key;
95 /* The "aclass" indices for various kinds of computed DWARF symbols. */
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
102 struct dwarf2_section_info
107 /* True if we have tried to read this section. */
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
118 DEF_VEC_I (offset_type);
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
127 /* Ensure only legit values are used. */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
135 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
142 /* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
146 /* Index data format version. */
149 /* The total length of the buffer. */
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
161 /* Size in slots, each slot is 2 offset_types. */
162 offset_type symbol_table_slots;
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
171 /* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
174 struct dwarf2_per_objfile
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info macro;
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
184 struct dwarf2_section_info addr;
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
187 struct dwarf2_section_info gdb_index;
189 VEC (dwarf2_section_info_def) *types;
192 struct objfile *objfile;
194 /* Table of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
198 /* The number of compilation units in ALL_COMP_UNITS. */
201 /* The number of .debug_types-related CUs. */
204 /* The .debug_types-related CUs (TUs). */
205 struct signatured_type **all_type_units;
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
222 /* Type unit statistics, to see how well the scaling improvements
226 int nr_uniq_abbrev_tables;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
240 /* Non-zero if we've check for whether there is a DWP file. */
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
250 /* A flag indicating wether this objfile has a section loaded at a
252 int has_section_at_zero;
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
256 unsigned char using_index;
258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
259 struct mapped_index *index_table;
261 /* When using index_table, this keeps track of all quick_file_names entries.
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
268 htab_t quick_file_names_table;
270 /* Set during partial symbol reading, to prevent queueing of full
272 int reading_partial_symbols;
274 /* Table mapping type DIEs to their struct type *.
275 This is NULL if not allocated yet.
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
285 /* Default names of the debugging sections. */
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
308 /* List of DWO/DWP sections. */
310 static const struct dwop_section_names
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
339 /* local data types */
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_offset;
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_offset;
366 /* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368 struct delayed_method_info
370 /* The type to which the method is attached, i.e., its parent class. */
373 /* The index of the method in the type's function fieldlists. */
376 /* The index of the method in the fieldlist. */
379 /* The name of the DIE. */
382 /* The DIE associated with this method. */
383 struct die_info *die;
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
389 /* Internal state when decoding a particular compilation unit. */
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
395 /* The header of the compilation unit. */
396 struct comp_unit_head header;
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
401 /* Non-zero if base_address has been set. */
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
408 const char *producer;
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
443 /* How many compilation units ago was this CU last referenced? */
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
450 /* Full DIEs if read in. */
451 struct die_info *dies;
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
477 struct dwo_unit *dwo_unit;
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base;
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
503 unsigned int has_loclist : 1;
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
511 unsigned int producer_is_gcc_lt_4_3 : 1;
512 unsigned int producer_is_icc : 1;
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
518 unsigned int processing_has_namespace_info : 1;
521 /* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
523 read_symtab_private field of the psymtab. */
525 struct dwarf2_per_cu_data
527 /* The start offset and length of this compilation unit.
528 NOTE: Unlike comp_unit_head.length, this length includes
530 If the DIE refers to a DWO file, this is always of the original die,
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
537 unsigned int queued : 1;
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
545 /* Non-zero if this CU is from .debug_types. */
546 unsigned int is_debug_types : 1;
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
554 struct dwarf2_section_info *section;
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
558 struct dwarf2_cu *cu;
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
563 struct objfile *objfile;
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
569 /* The partial symbol table associated with this compilation unit,
570 or NULL for unread partial units. */
571 struct partial_symtab *psymtab;
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
599 struct type_unit_group *type_unit_group;
602 /* Entry in the signatured_types hash table. */
604 struct signatured_type
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
609 struct dwarf2_per_cu_data per_cu;
611 /* The type's signature. */
614 /* Offset in the TU of the type's DIE, as read from the TU header.
615 If the definition lives in a DWO file, this value is unusable. */
616 cu_offset type_offset_in_tu;
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
626 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627 This includes type_unit_group and quick_file_names. */
629 struct stmt_list_hash
631 /* The DWO unit this table is from or NULL if there is none. */
632 struct dwo_unit *dwo_unit;
634 /* Offset in .debug_line or .debug_line.dwo. */
635 sect_offset line_offset;
638 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639 an object of this type. */
641 struct type_unit_group
643 /* dwarf2read.c's main "handle" on the symtab.
644 To simplify things we create an artificial CU that "includes" all the
645 type units using this stmt_list so that the rest of the code still has
646 a "per_cu" handle on the symtab.
647 This PER_CU is recognized by having no section. */
648 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
649 struct dwarf2_per_cu_data per_cu;
653 /* The TUs that share this DW_AT_stmt_list entry.
654 This is added to while parsing type units to build partial symtabs,
655 and is deleted afterwards and not used again. */
656 VEC (dwarf2_per_cu_ptr) *tus;
658 /* When reading the line table in "quick" functions, we need a real TU.
659 Any will do, we know they all share the same DW_AT_stmt_list entry.
660 For simplicity's sake, we pick the first one. */
661 struct dwarf2_per_cu_data *first_tu;
664 /* The primary symtab.
665 Type units in a group needn't all be defined in the same source file,
666 so we create an essentially anonymous symtab as the primary symtab. */
667 struct symtab *primary_symtab;
669 /* The data used to construct the hash key. */
670 struct stmt_list_hash hash;
672 /* The number of symtabs from the line header.
673 The value here must match line_header.num_file_names. */
674 unsigned int num_symtabs;
676 /* The symbol tables for this TU (obtained from the files listed in
678 WARNING: The order of entries here must match the order of entries
679 in the line header. After the first TU using this type_unit_group, the
680 line header for the subsequent TUs is recreated from this. This is done
681 because we need to use the same symtabs for each TU using the same
682 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
683 there's no guarantee the line header doesn't have duplicate entries. */
684 struct symtab **symtabs;
687 /* These sections are what may appear in a DWO file. */
691 struct dwarf2_section_info abbrev;
692 struct dwarf2_section_info line;
693 struct dwarf2_section_info loc;
694 struct dwarf2_section_info macinfo;
695 struct dwarf2_section_info macro;
696 struct dwarf2_section_info str;
697 struct dwarf2_section_info str_offsets;
698 /* In the case of a virtual DWO file, these two are unused. */
699 struct dwarf2_section_info info;
700 VEC (dwarf2_section_info_def) *types;
703 /* Common bits of DWO CUs/TUs. */
707 /* Backlink to the containing struct dwo_file. */
708 struct dwo_file *dwo_file;
710 /* The "id" that distinguishes this CU/TU.
711 .debug_info calls this "dwo_id", .debug_types calls this "signature".
712 Since signatures came first, we stick with it for consistency. */
715 /* The section this CU/TU lives in, in the DWO file. */
716 struct dwarf2_section_info *section;
718 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
722 /* For types, offset in the type's DIE of the type defined by this TU. */
723 cu_offset type_offset_in_tu;
726 /* Data for one DWO file.
727 This includes virtual DWO files that have been packaged into a
732 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
733 For virtual DWO files the name is constructed from the section offsets
734 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735 from related CU+TUs. */
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
742 /* Section info for this file. */
743 struct dwo_sections sections;
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
754 /* These sections are what may appear in a DWP file. */
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
765 /* These sections are what may appear in a virtual DWO file. */
767 struct virtual_dwo_sections
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
780 /* Contents of DWP hash tables. */
782 struct dwp_hash_table
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
788 /* Data for one DWP file. */
792 /* Name of the file. */
795 /* The bfd, when the file is open. Otherwise this is NULL. */
798 /* Section info for this file. */
799 struct dwp_sections sections;
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
815 /* This represents a '.dwz' file. */
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
825 struct dwarf2_section_info gdb_index;
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
834 struct exists to abstract away the constant parameters of die reading. */
836 struct die_reader_specs
838 /* die_section->asection->owner. */
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
845 struct dwo_file *dwo_file;
847 /* The section the die comes from.
848 This is either .debug_info or .debug_types, or the .dwo variants. */
849 struct dwarf2_section_info *die_section;
851 /* die_section->buffer. */
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
858 /* Type of function passed to init_cutu_and_read_dies, et.al. */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
861 struct die_info *comp_unit_die,
865 /* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
874 unsigned char maximum_ops_per_instruction;
875 unsigned char default_is_stmt;
877 unsigned char line_range;
878 unsigned char opcode_base;
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
890 unsigned int num_include_dirs, include_dirs_size;
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
900 unsigned int dir_index;
901 unsigned int mod_time;
903 int included_p; /* Non-zero if referenced by the Line Number Program. */
904 struct symtab *symtab; /* The associated symbol table, if any. */
907 /* The start and end of the statement program following this
908 header. These point into dwarf2_per_objfile->line_buffer. */
909 gdb_byte *statement_program_start, *statement_program_end;
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info
916 /* Offset of this DIE. */
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
929 unsigned int may_be_inlined : 1;
931 /* Flag set if the SCOPE field of this structure has been
933 unsigned int scope_set : 1;
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
950 /* The name of this DIE. Normally the value of DW_AT_name, but
951 sometimes a default name for unnamed DIEs. */
954 /* The linkage name, if present. */
955 const char *linkage_name;
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977 DW_AT_sibling, if any. */
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
985 sect_offset spec_offset;
987 /* Pointers to this DIE's parent, first child, and next sibling,
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
992 /* This data structure holds the information of an abbrev. */
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
1009 /* Size of abbrev_table.abbrev_hash_table. */
1010 #define ABBREV_HASH_SIZE 121
1012 /* Top level data structure to contain an abbreviation table. */
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1027 struct abbrev_info **abbrevs;
1030 /* Attributes have a name and a value. */
1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1044 struct dwarf_block *blk;
1048 struct signatured_type *signatured_type;
1053 /* This data structure holds a complete die structure. */
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1059 /* Number of attributes */
1060 unsigned char num_attrs;
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
1067 unsigned int abbrev;
1069 /* Offset in .debug_info or .debug_types section. */
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
1075 together via their SIBLING fields. */
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
1086 /* Get at parts of an attribute structure. */
1088 #define DW_STRING(attr) ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr) ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr) ((attr)->u.blk)
1092 #define DW_SND(attr) ((attr)->u.snd)
1093 #define DW_ADDR(attr) ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1096 /* Blocks are a bunch of untyped bytes. */
1101 /* Valid only if SIZE is not zero. */
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1109 /* Allocate fields for structs, unions and enums in this size. */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1117 static int bits_per_byte = 8;
1119 /* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1124 /* List of data member and baseclasses fields. */
1127 struct nextfield *next;
1132 *fields, *baseclasses;
1134 /* Number of fields (including baseclasses). */
1137 /* Number of baseclasses. */
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1159 struct nextfnfield *head;
1163 /* Number of entries in the fnfieldlists array. */
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
1177 /* One item on the queue of compilation units to read in full symbols
1179 struct dwarf2_queue_item
1181 struct dwarf2_per_cu_data *per_cu;
1182 enum language pretend_language;
1183 struct dwarf2_queue_item *next;
1186 /* The current queue. */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1189 /* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194 static int dwarf2_max_cache_age = 5;
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
1205 /* Various complaints about symbol reading that don't abort the process. */
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1210 complaint (&symfile_complaints,
1211 _("statement list doesn't fit in .debug_line section"));
1215 dwarf2_debug_line_missing_file_complaint (void)
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1224 complaint (&symfile_complaints,
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
1230 dwarf2_complex_location_expr_complaint (void)
1232 complaint (&symfile_complaints, _("location expression too complex"));
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1239 complaint (&symfile_complaints,
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1247 complaint (&symfile_complaints,
1248 _("debug info runs off end of %s section"
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1257 complaint (&symfile_complaints,
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1266 complaint (&symfile_complaints,
1267 _("invalid attribute class or form for '%s' in '%s'"),
1271 /* local function prototypes */
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1278 static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1281 static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1286 static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
1288 int, struct dwarf2_cu *);
1290 static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295 int need_pc, struct dwarf2_cu *cu);
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306 int need_pc, struct dwarf2_cu *cu);
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1316 static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1319 static void abbrev_table_free (struct abbrev_table *);
1321 static void abbrev_table_free_cleanup (void *);
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
1326 static void dwarf2_free_abbrev_table (void *);
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1330 static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1342 static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1364 static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1409 static struct die_info *die_specification (struct die_info *die,
1410 struct dwarf2_cu **);
1412 static void free_line_header (struct line_header *lh);
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427 const char *, const char *, CORE_ADDR);
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430 struct dwarf2_cu *);
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436 struct dwarf2_cu *);
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1441 struct obstack *obstack,
1442 struct dwarf2_cu *cu, LONGEST *value,
1444 struct dwarf2_locexpr_baton **baton);
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1448 static int need_gnat_info (struct dwarf2_cu *);
1450 static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1456 static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
1489 static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497 struct dwarf2_cu *);
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500 struct type *, struct dwarf2_cu *);
1502 static void dwarf2_add_member_fn (struct field_info *,
1503 struct die_info *, struct type *,
1504 struct dwarf2_cu *);
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1508 struct dwarf2_cu *);
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1520 static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1523 static const char *namespace_name (struct die_info *die,
1524 int *is_anonymous, struct dwarf2_cu *);
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1533 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1535 gdb_byte **new_info_ptr,
1536 struct die_info *parent);
1538 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 gdb_byte **new_info_ptr,
1541 struct die_info *parent);
1543 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *, int);
1546 static gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, gdb_byte *, int *);
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1556 static const char *dwarf2_full_name (const char *name,
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564 struct dwarf2_cu **);
1566 static const char *dwarf_tag_name (unsigned int);
1568 static const char *dwarf_attr_name (unsigned int);
1570 static const char *dwarf_form_name (unsigned int);
1572 static char *dwarf_bool_name (unsigned int);
1574 static const char *dwarf_type_encoding_name (unsigned int);
1576 static struct die_info *sibling_die (struct die_info *);
1578 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1580 static void dump_die_for_error (struct die_info *);
1582 static void dump_die_1 (struct ui_file *, int level, int max_level,
1585 /*static*/ void dump_die (struct die_info *, int max_level);
1587 static void store_in_ref_table (struct die_info *,
1588 struct dwarf2_cu *);
1590 static int is_ref_attr (struct attribute *);
1592 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1594 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1596 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1598 struct dwarf2_cu **);
1600 static struct die_info *follow_die_ref (struct die_info *,
1602 struct dwarf2_cu **);
1604 static struct die_info *follow_die_sig (struct die_info *,
1606 struct dwarf2_cu **);
1608 static struct signatured_type *lookup_signatured_type_at_offset
1609 (struct objfile *objfile,
1610 struct dwarf2_section_info *section, sect_offset offset);
1612 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1614 static void read_signatured_type (struct signatured_type *);
1616 static struct type_unit_group *get_type_unit_group
1617 (struct dwarf2_cu *, struct attribute *);
1619 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1621 /* memory allocation interface */
1623 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1625 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1627 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1630 static int attr_form_is_block (struct attribute *);
1632 static int attr_form_is_section_offset (struct attribute *);
1634 static int attr_form_is_constant (struct attribute *);
1636 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637 struct dwarf2_loclist_baton *baton,
1638 struct attribute *attr);
1640 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1642 struct dwarf2_cu *cu,
1645 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1647 struct abbrev_info *abbrev);
1649 static void free_stack_comp_unit (void *);
1651 static hashval_t partial_die_hash (const void *item);
1653 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1655 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1656 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1658 static void init_one_comp_unit (struct dwarf2_cu *cu,
1659 struct dwarf2_per_cu_data *per_cu);
1661 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1662 struct die_info *comp_unit_die,
1663 enum language pretend_language);
1665 static void free_heap_comp_unit (void *);
1667 static void free_cached_comp_units (void *);
1669 static void age_cached_comp_units (void);
1671 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1673 static struct type *set_die_type (struct die_info *, struct type *,
1674 struct dwarf2_cu *);
1676 static void create_all_comp_units (struct objfile *);
1678 static int create_all_type_units (struct objfile *);
1680 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1683 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1686 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1689 static void dwarf2_add_dependence (struct dwarf2_cu *,
1690 struct dwarf2_per_cu_data *);
1692 static void dwarf2_mark (struct dwarf2_cu *);
1694 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1696 static struct type *get_die_type_at_offset (sect_offset,
1697 struct dwarf2_per_cu_data *per_cu);
1699 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1701 static void dwarf2_release_queue (void *dummy);
1703 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704 enum language pretend_language);
1706 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707 struct dwarf2_per_cu_data *per_cu,
1708 enum language pretend_language);
1710 static void process_queue (void);
1712 static void find_file_and_directory (struct die_info *die,
1713 struct dwarf2_cu *cu,
1714 const char **name, const char **comp_dir);
1716 static char *file_full_name (int file, struct line_header *lh,
1717 const char *comp_dir);
1719 static gdb_byte *read_and_check_comp_unit_head
1720 (struct comp_unit_head *header,
1721 struct dwarf2_section_info *section,
1722 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723 int is_debug_types_section);
1725 static void init_cutu_and_read_dies
1726 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727 int use_existing_cu, int keep,
1728 die_reader_func_ftype *die_reader_func, void *data);
1730 static void init_cutu_and_read_dies_simple
1731 (struct dwarf2_per_cu_data *this_cu,
1732 die_reader_func_ftype *die_reader_func, void *data);
1734 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1736 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1738 static struct dwo_unit *lookup_dwo_comp_unit
1739 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1741 static struct dwo_unit *lookup_dwo_type_unit
1742 (struct signatured_type *, const char *, const char *);
1744 static void free_dwo_file_cleanup (void *);
1746 static void process_cu_includes (void);
1748 static void check_producer (struct dwarf2_cu *cu);
1752 /* Convert VALUE between big- and little-endian. */
1754 byte_swap (offset_type value)
1758 result = (value & 0xff) << 24;
1759 result |= (value & 0xff00) << 8;
1760 result |= (value & 0xff0000) >> 8;
1761 result |= (value & 0xff000000) >> 24;
1765 #define MAYBE_SWAP(V) byte_swap (V)
1768 #define MAYBE_SWAP(V) (V)
1769 #endif /* WORDS_BIGENDIAN */
1771 /* The suffix for an index file. */
1772 #define INDEX_SUFFIX ".gdb-index"
1774 /* Try to locate the sections we need for DWARF 2 debugging
1775 information and return true if we have enough to do something.
1776 NAMES points to the dwarf2 section names, or is NULL if the standard
1777 ELF names are used. */
1780 dwarf2_has_info (struct objfile *objfile,
1781 const struct dwarf2_debug_sections *names)
1783 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784 if (!dwarf2_per_objfile)
1786 /* Initialize per-objfile state. */
1787 struct dwarf2_per_objfile *data
1788 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1790 memset (data, 0, sizeof (*data));
1791 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792 dwarf2_per_objfile = data;
1794 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1796 dwarf2_per_objfile->objfile = objfile;
1798 return (dwarf2_per_objfile->info.asection != NULL
1799 && dwarf2_per_objfile->abbrev.asection != NULL);
1802 /* When loading sections, we look either for uncompressed section or for
1803 compressed section names. */
1806 section_is_p (const char *section_name,
1807 const struct dwarf2_section_names *names)
1809 if (names->normal != NULL
1810 && strcmp (section_name, names->normal) == 0)
1812 if (names->compressed != NULL
1813 && strcmp (section_name, names->compressed) == 0)
1818 /* This function is mapped across the sections and remembers the
1819 offset and size of each of the debugging sections we are interested
1823 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1825 const struct dwarf2_debug_sections *names;
1826 flagword aflag = bfd_get_section_flags (abfd, sectp);
1829 names = &dwarf2_elf_names;
1831 names = (const struct dwarf2_debug_sections *) vnames;
1833 if ((aflag & SEC_HAS_CONTENTS) == 0)
1836 else if (section_is_p (sectp->name, &names->info))
1838 dwarf2_per_objfile->info.asection = sectp;
1839 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1841 else if (section_is_p (sectp->name, &names->abbrev))
1843 dwarf2_per_objfile->abbrev.asection = sectp;
1844 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1846 else if (section_is_p (sectp->name, &names->line))
1848 dwarf2_per_objfile->line.asection = sectp;
1849 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1851 else if (section_is_p (sectp->name, &names->loc))
1853 dwarf2_per_objfile->loc.asection = sectp;
1854 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1856 else if (section_is_p (sectp->name, &names->macinfo))
1858 dwarf2_per_objfile->macinfo.asection = sectp;
1859 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1861 else if (section_is_p (sectp->name, &names->macro))
1863 dwarf2_per_objfile->macro.asection = sectp;
1864 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1866 else if (section_is_p (sectp->name, &names->str))
1868 dwarf2_per_objfile->str.asection = sectp;
1869 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1871 else if (section_is_p (sectp->name, &names->addr))
1873 dwarf2_per_objfile->addr.asection = sectp;
1874 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1876 else if (section_is_p (sectp->name, &names->frame))
1878 dwarf2_per_objfile->frame.asection = sectp;
1879 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1881 else if (section_is_p (sectp->name, &names->eh_frame))
1883 dwarf2_per_objfile->eh_frame.asection = sectp;
1884 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1886 else if (section_is_p (sectp->name, &names->ranges))
1888 dwarf2_per_objfile->ranges.asection = sectp;
1889 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1891 else if (section_is_p (sectp->name, &names->types))
1893 struct dwarf2_section_info type_section;
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.asection = sectp;
1897 type_section.size = bfd_get_section_size (sectp);
1899 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1902 else if (section_is_p (sectp->name, &names->gdb_index))
1904 dwarf2_per_objfile->gdb_index.asection = sectp;
1905 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1908 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909 && bfd_section_vma (abfd, sectp) == 0)
1910 dwarf2_per_objfile->has_section_at_zero = 1;
1913 /* A helper function that decides whether a section is empty,
1917 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1919 return info->asection == NULL || info->size == 0;
1922 /* Read the contents of the section INFO.
1923 OBJFILE is the main object file, but not necessarily the file where
1924 the section comes from. E.g., for DWO files INFO->asection->owner
1925 is the bfd of the DWO file.
1926 If the section is compressed, uncompress it before returning. */
1929 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1931 asection *sectp = info->asection;
1933 gdb_byte *buf, *retbuf;
1934 unsigned char header[4];
1938 info->buffer = NULL;
1941 if (dwarf2_section_empty_p (info))
1944 abfd = sectp->owner;
1946 /* If the section has relocations, we must read it ourselves.
1947 Otherwise we attach it to the BFD. */
1948 if ((sectp->flags & SEC_RELOC) == 0)
1950 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1952 /* We have to cast away const here for historical reasons.
1953 Fixing dwarf2read to be const-correct would be quite nice. */
1954 info->buffer = (gdb_byte *) bytes;
1958 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1961 /* When debugging .o files, we may need to apply relocations; see
1962 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963 We never compress sections in .o files, so we only need to
1964 try this when the section is not compressed. */
1965 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1968 info->buffer = retbuf;
1972 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973 || bfd_bread (buf, info->size, abfd) != info->size)
1974 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975 bfd_get_filename (abfd));
1978 /* A helper function that returns the size of a section in a safe way.
1979 If you are positive that the section has been read before using the
1980 size, then it is safe to refer to the dwarf2_section_info object's
1981 "size" field directly. In other cases, you must call this
1982 function, because for compressed sections the size field is not set
1983 correctly until the section has been read. */
1985 static bfd_size_type
1986 dwarf2_section_size (struct objfile *objfile,
1987 struct dwarf2_section_info *info)
1990 dwarf2_read_section (objfile, info);
1994 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1998 dwarf2_get_section_info (struct objfile *objfile,
1999 enum dwarf2_section_enum sect,
2000 asection **sectp, gdb_byte **bufp,
2001 bfd_size_type *sizep)
2003 struct dwarf2_per_objfile *data
2004 = objfile_data (objfile, dwarf2_objfile_data_key);
2005 struct dwarf2_section_info *info;
2007 /* We may see an objfile without any DWARF, in which case we just
2018 case DWARF2_DEBUG_FRAME:
2019 info = &data->frame;
2021 case DWARF2_EH_FRAME:
2022 info = &data->eh_frame;
2025 gdb_assert_not_reached ("unexpected section");
2028 dwarf2_read_section (objfile, info);
2030 *sectp = info->asection;
2031 *bufp = info->buffer;
2032 *sizep = info->size;
2035 /* A helper function to find the sections for a .dwz file. */
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2040 struct dwz_file *dwz_file = arg;
2042 /* Note that we only support the standard ELF names, because .dwz
2043 is ELF-only (at the time of writing). */
2044 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2046 dwz_file->abbrev.asection = sectp;
2047 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2051 dwz_file->info.asection = sectp;
2052 dwz_file->info.size = bfd_get_section_size (sectp);
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2056 dwz_file->str.asection = sectp;
2057 dwz_file->str.size = bfd_get_section_size (sectp);
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2061 dwz_file->line.asection = sectp;
2062 dwz_file->line.size = bfd_get_section_size (sectp);
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2066 dwz_file->macro.asection = sectp;
2067 dwz_file->macro.size = bfd_get_section_size (sectp);
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2071 dwz_file->gdb_index.asection = sectp;
2072 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2076 /* Open the separate '.dwz' debug file, if needed. Error if the file
2079 static struct dwz_file *
2080 dwarf2_get_dwz_file (void)
2082 bfd *abfd, *dwz_bfd;
2085 struct cleanup *cleanup;
2086 const char *filename;
2087 struct dwz_file *result;
2089 if (dwarf2_per_objfile->dwz_file != NULL)
2090 return dwarf2_per_objfile->dwz_file;
2092 abfd = dwarf2_per_objfile->objfile->obfd;
2093 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094 if (section == NULL)
2095 error (_("could not find '.gnu_debugaltlink' section"));
2096 if (!bfd_malloc_and_get_section (abfd, section, &data))
2097 error (_("could not read '.gnu_debugaltlink' section: %s"),
2098 bfd_errmsg (bfd_get_error ()));
2099 cleanup = make_cleanup (xfree, data);
2102 if (!IS_ABSOLUTE_PATH (filename))
2104 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2107 make_cleanup (xfree, abs);
2108 abs = ldirname (abs);
2109 make_cleanup (xfree, abs);
2111 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112 make_cleanup (xfree, rel);
2116 /* The format is just a NUL-terminated file name, followed by the
2117 build-id. For now, though, we ignore the build-id. */
2118 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119 if (dwz_bfd == NULL)
2120 error (_("could not read '%s': %s"), filename,
2121 bfd_errmsg (bfd_get_error ()));
2123 if (!bfd_check_format (dwz_bfd, bfd_object))
2125 gdb_bfd_unref (dwz_bfd);
2126 error (_("file '%s' was not usable: %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2130 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2132 result->dwz_bfd = dwz_bfd;
2134 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2136 do_cleanups (cleanup);
2138 dwarf2_per_objfile->dwz_file = result;
2142 /* DWARF quick_symbols_functions support. */
2144 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2145 unique line tables, so we maintain a separate table of all .debug_line
2146 derived entries to support the sharing.
2147 All the quick functions need is the list of file names. We discard the
2148 line_header when we're done and don't need to record it here. */
2149 struct quick_file_names
2151 /* The data used to construct the hash key. */
2152 struct stmt_list_hash hash;
2154 /* The number of entries in file_names, real_names. */
2155 unsigned int num_file_names;
2157 /* The file names from the line table, after being run through
2159 const char **file_names;
2161 /* The file names from the line table after being run through
2162 gdb_realpath. These are computed lazily. */
2163 const char **real_names;
2166 /* When using the index (and thus not using psymtabs), each CU has an
2167 object of this type. This is used to hold information needed by
2168 the various "quick" methods. */
2169 struct dwarf2_per_cu_quick_data
2171 /* The file table. This can be NULL if there was no file table
2172 or it's currently not read in.
2173 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2174 struct quick_file_names *file_names;
2176 /* The corresponding symbol table. This is NULL if symbols for this
2177 CU have not yet been read. */
2178 struct symtab *symtab;
2180 /* A temporary mark bit used when iterating over all CUs in
2181 expand_symtabs_matching. */
2182 unsigned int mark : 1;
2184 /* True if we've tried to read the file table and found there isn't one.
2185 There will be no point in trying to read it again next time. */
2186 unsigned int no_file_data : 1;
2189 /* Utility hash function for a stmt_list_hash. */
2192 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2196 if (stmt_list_hash->dwo_unit != NULL)
2197 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198 v += stmt_list_hash->line_offset.sect_off;
2202 /* Utility equality function for a stmt_list_hash. */
2205 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206 const struct stmt_list_hash *rhs)
2208 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2210 if (lhs->dwo_unit != NULL
2211 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2214 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2217 /* Hash function for a quick_file_names. */
2220 hash_file_name_entry (const void *e)
2222 const struct quick_file_names *file_data = e;
2224 return hash_stmt_list_entry (&file_data->hash);
2227 /* Equality function for a quick_file_names. */
2230 eq_file_name_entry (const void *a, const void *b)
2232 const struct quick_file_names *ea = a;
2233 const struct quick_file_names *eb = b;
2235 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2238 /* Delete function for a quick_file_names. */
2241 delete_file_name_entry (void *e)
2243 struct quick_file_names *file_data = e;
2246 for (i = 0; i < file_data->num_file_names; ++i)
2248 xfree ((void*) file_data->file_names[i]);
2249 if (file_data->real_names)
2250 xfree ((void*) file_data->real_names[i]);
2253 /* The space for the struct itself lives on objfile_obstack,
2254 so we don't free it here. */
2257 /* Create a quick_file_names hash table. */
2260 create_quick_file_names_table (unsigned int nr_initial_entries)
2262 return htab_create_alloc (nr_initial_entries,
2263 hash_file_name_entry, eq_file_name_entry,
2264 delete_file_name_entry, xcalloc, xfree);
2267 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2268 have to be created afterwards. You should call age_cached_comp_units after
2269 processing PER_CU->CU. dw2_setup must have been already called. */
2272 load_cu (struct dwarf2_per_cu_data *per_cu)
2274 if (per_cu->is_debug_types)
2275 load_full_type_unit (per_cu);
2277 load_full_comp_unit (per_cu, language_minimal);
2279 gdb_assert (per_cu->cu != NULL);
2281 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2284 /* Read in the symbols for PER_CU. */
2287 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2289 struct cleanup *back_to;
2291 /* Skip type_unit_groups, reading the type units they contain
2292 is handled elsewhere. */
2293 if (IS_TYPE_UNIT_GROUP (per_cu))
2296 back_to = make_cleanup (dwarf2_release_queue, NULL);
2298 if (dwarf2_per_objfile->using_index
2299 ? per_cu->v.quick->symtab == NULL
2300 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2302 queue_comp_unit (per_cu, language_minimal);
2308 /* Age the cache, releasing compilation units that have not
2309 been used recently. */
2310 age_cached_comp_units ();
2312 do_cleanups (back_to);
2315 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2316 the objfile from which this CU came. Returns the resulting symbol
2319 static struct symtab *
2320 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2322 gdb_assert (dwarf2_per_objfile->using_index);
2323 if (!per_cu->v.quick->symtab)
2325 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326 increment_reading_symtab ();
2327 dw2_do_instantiate_symtab (per_cu);
2328 process_cu_includes ();
2329 do_cleanups (back_to);
2331 return per_cu->v.quick->symtab;
2334 /* Return the CU given its index.
2336 This is intended for loops like:
2338 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339 + dwarf2_per_objfile->n_type_units); ++i)
2341 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2347 static struct dwarf2_per_cu_data *
2348 dw2_get_cu (int index)
2350 if (index >= dwarf2_per_objfile->n_comp_units)
2352 index -= dwarf2_per_objfile->n_comp_units;
2353 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2357 return dwarf2_per_objfile->all_comp_units[index];
2360 /* Return the primary CU given its index.
2361 The difference between this function and dw2_get_cu is in the handling
2362 of type units (TUs). Here we return the type_unit_group object.
2364 This is intended for loops like:
2366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2369 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2375 static struct dwarf2_per_cu_data *
2376 dw2_get_primary_cu (int index)
2378 if (index >= dwarf2_per_objfile->n_comp_units)
2380 index -= dwarf2_per_objfile->n_comp_units;
2381 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2385 return dwarf2_per_objfile->all_comp_units[index];
2388 /* A helper for create_cus_from_index that handles a given list of
2392 create_cus_from_index_list (struct objfile *objfile,
2393 const gdb_byte *cu_list, offset_type n_elements,
2394 struct dwarf2_section_info *section,
2400 for (i = 0; i < n_elements; i += 2)
2402 struct dwarf2_per_cu_data *the_cu;
2403 ULONGEST offset, length;
2405 gdb_static_assert (sizeof (ULONGEST) >= 8);
2406 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2410 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_data);
2412 the_cu->offset.sect_off = offset;
2413 the_cu->length = length;
2414 the_cu->objfile = objfile;
2415 the_cu->section = section;
2416 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_quick_data);
2418 the_cu->is_dwz = is_dwz;
2419 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2423 /* Read the CU list from the mapped index, and use it to create all
2424 the CU objects for this objfile. */
2427 create_cus_from_index (struct objfile *objfile,
2428 const gdb_byte *cu_list, offset_type cu_list_elements,
2429 const gdb_byte *dwz_list, offset_type dwz_elements)
2431 struct dwz_file *dwz;
2433 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434 dwarf2_per_objfile->all_comp_units
2435 = obstack_alloc (&objfile->objfile_obstack,
2436 dwarf2_per_objfile->n_comp_units
2437 * sizeof (struct dwarf2_per_cu_data *));
2439 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440 &dwarf2_per_objfile->info, 0, 0);
2442 if (dwz_elements == 0)
2445 dwz = dwarf2_get_dwz_file ();
2446 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447 cu_list_elements / 2);
2450 /* Create the signatured type hash table from the index. */
2453 create_signatured_type_table_from_index (struct objfile *objfile,
2454 struct dwarf2_section_info *section,
2455 const gdb_byte *bytes,
2456 offset_type elements)
2459 htab_t sig_types_hash;
2461 dwarf2_per_objfile->n_type_units = elements / 3;
2462 dwarf2_per_objfile->all_type_units
2463 = obstack_alloc (&objfile->objfile_obstack,
2464 dwarf2_per_objfile->n_type_units
2465 * sizeof (struct signatured_type *));
2467 sig_types_hash = allocate_signatured_type_table (objfile);
2469 for (i = 0; i < elements; i += 3)
2471 struct signatured_type *sig_type;
2472 ULONGEST offset, type_offset_in_tu, signature;
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2479 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct signatured_type);
2484 sig_type->signature = signature;
2485 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486 sig_type->per_cu.is_debug_types = 1;
2487 sig_type->per_cu.section = section;
2488 sig_type->per_cu.offset.sect_off = offset;
2489 sig_type->per_cu.objfile = objfile;
2490 sig_type->per_cu.v.quick
2491 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492 struct dwarf2_per_cu_quick_data);
2494 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2497 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2500 dwarf2_per_objfile->signatured_types = sig_types_hash;
2503 /* Read the address map data from the mapped index, and use it to
2504 populate the objfile's psymtabs_addrmap. */
2507 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2509 const gdb_byte *iter, *end;
2510 struct obstack temp_obstack;
2511 struct addrmap *mutable_map;
2512 struct cleanup *cleanup;
2515 obstack_init (&temp_obstack);
2516 cleanup = make_cleanup_obstack_free (&temp_obstack);
2517 mutable_map = addrmap_create_mutable (&temp_obstack);
2519 iter = index->address_table;
2520 end = iter + index->address_table_size;
2522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2526 ULONGEST hi, lo, cu_index;
2527 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2529 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2531 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2534 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2535 dw2_get_cu (cu_index));
2538 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2539 &objfile->objfile_obstack);
2540 do_cleanups (cleanup);
2543 /* The hash function for strings in the mapped index. This is the same as
2544 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2545 implementation. This is necessary because the hash function is tied to the
2546 format of the mapped index file. The hash values do not have to match with
2549 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2552 mapped_index_string_hash (int index_version, const void *p)
2554 const unsigned char *str = (const unsigned char *) p;
2558 while ((c = *str++) != 0)
2560 if (index_version >= 5)
2562 r = r * 67 + c - 113;
2568 /* Find a slot in the mapped index INDEX for the object named NAME.
2569 If NAME is found, set *VEC_OUT to point to the CU vector in the
2570 constant pool and return 1. If NAME cannot be found, return 0. */
2573 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2574 offset_type **vec_out)
2576 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2578 offset_type slot, step;
2579 int (*cmp) (const char *, const char *);
2581 if (current_language->la_language == language_cplus
2582 || current_language->la_language == language_java
2583 || current_language->la_language == language_fortran)
2585 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2587 const char *paren = strchr (name, '(');
2593 dup = xmalloc (paren - name + 1);
2594 memcpy (dup, name, paren - name);
2595 dup[paren - name] = 0;
2597 make_cleanup (xfree, dup);
2602 /* Index version 4 did not support case insensitive searches. But the
2603 indices for case insensitive languages are built in lowercase, therefore
2604 simulate our NAME being searched is also lowercased. */
2605 hash = mapped_index_string_hash ((index->version == 4
2606 && case_sensitivity == case_sensitive_off
2607 ? 5 : index->version),
2610 slot = hash & (index->symbol_table_slots - 1);
2611 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2612 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2616 /* Convert a slot number to an offset into the table. */
2617 offset_type i = 2 * slot;
2619 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2621 do_cleanups (back_to);
2625 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2626 if (!cmp (name, str))
2628 *vec_out = (offset_type *) (index->constant_pool
2629 + MAYBE_SWAP (index->symbol_table[i + 1]));
2630 do_cleanups (back_to);
2634 slot = (slot + step) & (index->symbol_table_slots - 1);
2638 /* A helper function that reads the .gdb_index from SECTION and fills
2639 in MAP. FILENAME is the name of the file containing the section;
2640 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2641 ok to use deprecated sections.
2643 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2644 out parameters that are filled in with information about the CU and
2645 TU lists in the section.
2647 Returns 1 if all went well, 0 otherwise. */
2650 read_index_from_section (struct objfile *objfile,
2651 const char *filename,
2653 struct dwarf2_section_info *section,
2654 struct mapped_index *map,
2655 const gdb_byte **cu_list,
2656 offset_type *cu_list_elements,
2657 const gdb_byte **types_list,
2658 offset_type *types_list_elements)
2661 offset_type version;
2662 offset_type *metadata;
2665 if (dwarf2_section_empty_p (section))
2668 /* Older elfutils strip versions could keep the section in the main
2669 executable while splitting it for the separate debug info file. */
2670 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2673 dwarf2_read_section (objfile, section);
2675 addr = section->buffer;
2676 /* Version check. */
2677 version = MAYBE_SWAP (*(offset_type *) addr);
2678 /* Versions earlier than 3 emitted every copy of a psymbol. This
2679 causes the index to behave very poorly for certain requests. Version 3
2680 contained incomplete addrmap. So, it seems better to just ignore such
2684 static int warning_printed = 0;
2685 if (!warning_printed)
2687 warning (_("Skipping obsolete .gdb_index section in %s."),
2689 warning_printed = 1;
2693 /* Index version 4 uses a different hash function than index version
2696 Versions earlier than 6 did not emit psymbols for inlined
2697 functions. Using these files will cause GDB not to be able to
2698 set breakpoints on inlined functions by name, so we ignore these
2699 indices unless the user has done
2700 "set use-deprecated-index-sections on". */
2701 if (version < 6 && !deprecated_ok)
2703 static int warning_printed = 0;
2704 if (!warning_printed)
2707 Skipping deprecated .gdb_index section in %s.\n\
2708 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2709 to use the section anyway."),
2711 warning_printed = 1;
2715 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2716 of the TU (for symbols coming from TUs). It's just a performance bug, and
2717 we can't distinguish gdb-generated indices from gold-generated ones, so
2718 nothing to do here. */
2720 /* Indexes with higher version than the one supported by GDB may be no
2721 longer backward compatible. */
2725 map->version = version;
2726 map->total_size = section->size;
2728 metadata = (offset_type *) (addr + sizeof (offset_type));
2731 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2732 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2736 *types_list = addr + MAYBE_SWAP (metadata[i]);
2737 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]))
2742 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2743 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2744 - MAYBE_SWAP (metadata[i]));
2747 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2748 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2749 - MAYBE_SWAP (metadata[i]))
2750 / (2 * sizeof (offset_type)));
2753 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2759 /* Read the index file. If everything went ok, initialize the "quick"
2760 elements of all the CUs and return 1. Otherwise, return 0. */
2763 dwarf2_read_index (struct objfile *objfile)
2765 struct mapped_index local_map, *map;
2766 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2767 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2769 if (!read_index_from_section (objfile, objfile->name,
2770 use_deprecated_index_sections,
2771 &dwarf2_per_objfile->gdb_index, &local_map,
2772 &cu_list, &cu_list_elements,
2773 &types_list, &types_list_elements))
2776 /* Don't use the index if it's empty. */
2777 if (local_map.symbol_table_slots == 0)
2780 /* If there is a .dwz file, read it so we can get its CU list as
2782 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2784 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2785 struct mapped_index dwz_map;
2786 const gdb_byte *dwz_types_ignore;
2787 offset_type dwz_types_elements_ignore;
2789 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2791 &dwz->gdb_index, &dwz_map,
2792 &dwz_list, &dwz_list_elements,
2794 &dwz_types_elements_ignore))
2796 warning (_("could not read '.gdb_index' section from %s; skipping"),
2797 bfd_get_filename (dwz->dwz_bfd));
2802 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2805 if (types_list_elements)
2807 struct dwarf2_section_info *section;
2809 /* We can only handle a single .debug_types when we have an
2811 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2814 section = VEC_index (dwarf2_section_info_def,
2815 dwarf2_per_objfile->types, 0);
2817 create_signatured_type_table_from_index (objfile, section, types_list,
2818 types_list_elements);
2821 create_addrmap_from_index (objfile, &local_map);
2823 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2826 dwarf2_per_objfile->index_table = map;
2827 dwarf2_per_objfile->using_index = 1;
2828 dwarf2_per_objfile->quick_file_names_table =
2829 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2834 /* A helper for the "quick" functions which sets the global
2835 dwarf2_per_objfile according to OBJFILE. */
2838 dw2_setup (struct objfile *objfile)
2840 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2841 gdb_assert (dwarf2_per_objfile);
2844 /* die_reader_func for dw2_get_file_names. */
2847 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2849 struct die_info *comp_unit_die,
2853 struct dwarf2_cu *cu = reader->cu;
2854 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2855 struct objfile *objfile = dwarf2_per_objfile->objfile;
2856 struct dwarf2_per_cu_data *lh_cu;
2857 struct line_header *lh;
2858 struct attribute *attr;
2860 const char *name, *comp_dir;
2862 struct quick_file_names *qfn;
2863 unsigned int line_offset;
2865 /* Our callers never want to match partial units -- instead they
2866 will match the enclosing full CU. */
2867 if (comp_unit_die->tag == DW_TAG_partial_unit)
2869 this_cu->v.quick->no_file_data = 1;
2873 /* If we're reading the line header for TUs, store it in the "per_cu"
2875 if (this_cu->is_debug_types)
2877 struct type_unit_group *tu_group = data;
2879 gdb_assert (tu_group != NULL);
2880 lh_cu = &tu_group->per_cu;
2889 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2892 struct quick_file_names find_entry;
2894 line_offset = DW_UNSND (attr);
2896 /* We may have already read in this line header (TU line header sharing).
2897 If we have we're done. */
2898 find_entry.hash.dwo_unit = cu->dwo_unit;
2899 find_entry.hash.line_offset.sect_off = line_offset;
2900 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2901 &find_entry, INSERT);
2904 lh_cu->v.quick->file_names = *slot;
2908 lh = dwarf_decode_line_header (line_offset, cu);
2912 lh_cu->v.quick->no_file_data = 1;
2916 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2917 qfn->hash.dwo_unit = cu->dwo_unit;
2918 qfn->hash.line_offset.sect_off = line_offset;
2919 gdb_assert (slot != NULL);
2922 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2924 qfn->num_file_names = lh->num_file_names;
2925 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2926 lh->num_file_names * sizeof (char *));
2927 for (i = 0; i < lh->num_file_names; ++i)
2928 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2929 qfn->real_names = NULL;
2931 free_line_header (lh);
2933 lh_cu->v.quick->file_names = qfn;
2936 /* A helper for the "quick" functions which attempts to read the line
2937 table for THIS_CU. */
2939 static struct quick_file_names *
2940 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2942 /* For TUs this should only be called on the parent group. */
2943 if (this_cu->is_debug_types)
2944 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2946 if (this_cu->v.quick->file_names != NULL)
2947 return this_cu->v.quick->file_names;
2948 /* If we know there is no line data, no point in looking again. */
2949 if (this_cu->v.quick->no_file_data)
2952 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2953 in the stub for CUs, there's is no need to lookup the DWO file.
2954 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2956 if (this_cu->is_debug_types)
2958 struct type_unit_group *tu_group = this_cu->type_unit_group;
2960 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2961 dw2_get_file_names_reader, tu_group);
2964 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2966 if (this_cu->v.quick->no_file_data)
2968 return this_cu->v.quick->file_names;
2971 /* A helper for the "quick" functions which computes and caches the
2972 real path for a given file name from the line table. */
2975 dw2_get_real_path (struct objfile *objfile,
2976 struct quick_file_names *qfn, int index)
2978 if (qfn->real_names == NULL)
2979 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2980 qfn->num_file_names, sizeof (char *));
2982 if (qfn->real_names[index] == NULL)
2983 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2985 return qfn->real_names[index];
2988 static struct symtab *
2989 dw2_find_last_source_symtab (struct objfile *objfile)
2993 dw2_setup (objfile);
2994 index = dwarf2_per_objfile->n_comp_units - 1;
2995 return dw2_instantiate_symtab (dw2_get_cu (index));
2998 /* Traversal function for dw2_forget_cached_source_info. */
3001 dw2_free_cached_file_names (void **slot, void *info)
3003 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3005 if (file_data->real_names)
3009 for (i = 0; i < file_data->num_file_names; ++i)
3011 xfree ((void*) file_data->real_names[i]);
3012 file_data->real_names[i] = NULL;
3020 dw2_forget_cached_source_info (struct objfile *objfile)
3022 dw2_setup (objfile);
3024 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3025 dw2_free_cached_file_names, NULL);
3028 /* Helper function for dw2_map_symtabs_matching_filename that expands
3029 the symtabs and calls the iterator. */
3032 dw2_map_expand_apply (struct objfile *objfile,
3033 struct dwarf2_per_cu_data *per_cu,
3034 const char *name, const char *real_path,
3035 int (*callback) (struct symtab *, void *),
3038 struct symtab *last_made = objfile->symtabs;
3040 /* Don't visit already-expanded CUs. */
3041 if (per_cu->v.quick->symtab)
3044 /* This may expand more than one symtab, and we want to iterate over
3046 dw2_instantiate_symtab (per_cu);
3048 return iterate_over_some_symtabs (name, real_path, callback, data,
3049 objfile->symtabs, last_made);
3052 /* Implementation of the map_symtabs_matching_filename method. */
3055 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3056 const char *real_path,
3057 int (*callback) (struct symtab *, void *),
3061 const char *name_basename = lbasename (name);
3063 dw2_setup (objfile);
3065 /* The rule is CUs specify all the files, including those used by
3066 any TU, so there's no need to scan TUs here. */
3068 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3071 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3072 struct quick_file_names *file_data;
3074 /* We only need to look at symtabs not already expanded. */
3075 if (per_cu->v.quick->symtab)
3078 file_data = dw2_get_file_names (per_cu);
3079 if (file_data == NULL)
3082 for (j = 0; j < file_data->num_file_names; ++j)
3084 const char *this_name = file_data->file_names[j];
3085 const char *this_real_name;
3087 if (compare_filenames_for_search (this_name, name))
3089 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3095 /* Before we invoke realpath, which can get expensive when many
3096 files are involved, do a quick comparison of the basenames. */
3097 if (! basenames_may_differ
3098 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3101 this_real_name = dw2_get_real_path (objfile, file_data, j);
3102 if (compare_filenames_for_search (this_real_name, name))
3104 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3110 if (real_path != NULL)
3112 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3113 gdb_assert (IS_ABSOLUTE_PATH (name));
3114 if (this_real_name != NULL
3115 && FILENAME_CMP (real_path, this_real_name) == 0)
3117 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3129 /* Struct used to manage iterating over all CUs looking for a symbol. */
3131 struct dw2_symtab_iterator
3133 /* The internalized form of .gdb_index. */
3134 struct mapped_index *index;
3135 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3136 int want_specific_block;
3137 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3138 Unused if !WANT_SPECIFIC_BLOCK. */
3140 /* The kind of symbol we're looking for. */
3142 /* The list of CUs from the index entry of the symbol,
3143 or NULL if not found. */
3145 /* The next element in VEC to look at. */
3147 /* The number of elements in VEC, or zero if there is no match. */
3151 /* Initialize the index symtab iterator ITER.
3152 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3153 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3156 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3157 struct mapped_index *index,
3158 int want_specific_block,
3163 iter->index = index;
3164 iter->want_specific_block = want_specific_block;
3165 iter->block_index = block_index;
3166 iter->domain = domain;
3169 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3170 iter->length = MAYBE_SWAP (*iter->vec);
3178 /* Return the next matching CU or NULL if there are no more. */
3180 static struct dwarf2_per_cu_data *
3181 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3183 for ( ; iter->next < iter->length; ++iter->next)
3185 offset_type cu_index_and_attrs =
3186 MAYBE_SWAP (iter->vec[iter->next + 1]);
3187 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3188 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3189 int want_static = iter->block_index != GLOBAL_BLOCK;
3190 /* This value is only valid for index versions >= 7. */
3191 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3192 gdb_index_symbol_kind symbol_kind =
3193 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3194 /* Only check the symbol attributes if they're present.
3195 Indices prior to version 7 don't record them,
3196 and indices >= 7 may elide them for certain symbols
3197 (gold does this). */
3199 (iter->index->version >= 7
3200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3202 /* Skip if already read in. */
3203 if (per_cu->v.quick->symtab)
3207 && iter->want_specific_block
3208 && want_static != is_static)
3211 /* Only check the symbol's kind if it has one. */
3214 switch (iter->domain)
3217 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3218 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3219 /* Some types are also in VAR_DOMAIN. */
3220 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3224 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3228 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3243 static struct symtab *
3244 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3245 const char *name, domain_enum domain)
3247 struct symtab *stab_best = NULL;
3248 struct mapped_index *index;
3250 dw2_setup (objfile);
3252 index = dwarf2_per_objfile->index_table;
3254 /* index is NULL if OBJF_READNOW. */
3257 struct dw2_symtab_iterator iter;
3258 struct dwarf2_per_cu_data *per_cu;
3260 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3262 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3264 struct symbol *sym = NULL;
3265 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3267 /* Some caution must be observed with overloaded functions
3268 and methods, since the index will not contain any overload
3269 information (but NAME might contain it). */
3272 struct blockvector *bv = BLOCKVECTOR (stab);
3273 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3275 sym = lookup_block_symbol (block, name, domain);
3278 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3280 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3286 /* Keep looking through other CUs. */
3294 dw2_print_stats (struct objfile *objfile)
3296 int i, total, count;
3298 dw2_setup (objfile);
3299 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3301 for (i = 0; i < total; ++i)
3303 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3305 if (!per_cu->v.quick->symtab)
3308 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3309 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3313 dw2_dump (struct objfile *objfile)
3315 /* Nothing worth printing. */
3319 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3320 struct section_offsets *delta)
3322 /* There's nothing to relocate here. */
3326 dw2_expand_symtabs_for_function (struct objfile *objfile,
3327 const char *func_name)
3329 struct mapped_index *index;
3331 dw2_setup (objfile);
3333 index = dwarf2_per_objfile->index_table;
3335 /* index is NULL if OBJF_READNOW. */
3338 struct dw2_symtab_iterator iter;
3339 struct dwarf2_per_cu_data *per_cu;
3341 /* Note: It doesn't matter what we pass for block_index here. */
3342 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3345 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3346 dw2_instantiate_symtab (per_cu);
3351 dw2_expand_all_symtabs (struct objfile *objfile)
3355 dw2_setup (objfile);
3357 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3358 + dwarf2_per_objfile->n_type_units); ++i)
3360 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3362 dw2_instantiate_symtab (per_cu);
3367 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3368 const char *fullname)
3372 dw2_setup (objfile);
3374 /* We don't need to consider type units here.
3375 This is only called for examining code, e.g. expand_line_sal.
3376 There can be an order of magnitude (or more) more type units
3377 than comp units, and we avoid them if we can. */
3379 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3382 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3383 struct quick_file_names *file_data;
3385 /* We only need to look at symtabs not already expanded. */
3386 if (per_cu->v.quick->symtab)
3389 file_data = dw2_get_file_names (per_cu);
3390 if (file_data == NULL)
3393 for (j = 0; j < file_data->num_file_names; ++j)
3395 const char *this_fullname = file_data->file_names[j];
3397 if (filename_cmp (this_fullname, fullname) == 0)
3399 dw2_instantiate_symtab (per_cu);
3406 /* A helper function for dw2_find_symbol_file that finds the primary
3407 file name for a given CU. This is a die_reader_func. */
3410 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3412 struct die_info *comp_unit_die,
3416 const char **result_ptr = data;
3417 struct dwarf2_cu *cu = reader->cu;
3418 struct attribute *attr;
3420 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3424 *result_ptr = DW_STRING (attr);
3428 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3430 struct dwarf2_per_cu_data *per_cu;
3432 const char *filename;
3434 dw2_setup (objfile);
3436 /* index_table is NULL if OBJF_READNOW. */
3437 if (!dwarf2_per_objfile->index_table)
3441 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3443 struct blockvector *bv = BLOCKVECTOR (s);
3444 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3445 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3449 /* Only file extension of returned filename is recognized. */
3450 return SYMBOL_SYMTAB (sym)->filename;
3456 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3460 /* Note that this just looks at the very first one named NAME -- but
3461 actually we are looking for a function. find_main_filename
3462 should be rewritten so that it doesn't require a custom hook. It
3463 could just use the ordinary symbol tables. */
3464 /* vec[0] is the length, which must always be >0. */
3465 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3467 if (per_cu->v.quick->symtab != NULL)
3469 /* Only file extension of returned filename is recognized. */
3470 return per_cu->v.quick->symtab->filename;
3473 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3474 dw2_get_primary_filename_reader, &filename);
3476 /* Only file extension of returned filename is recognized. */
3481 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3482 struct objfile *objfile, int global,
3483 int (*callback) (struct block *,
3484 struct symbol *, void *),
3485 void *data, symbol_compare_ftype *match,
3486 symbol_compare_ftype *ordered_compare)
3488 /* Currently unimplemented; used for Ada. The function can be called if the
3489 current language is Ada for a non-Ada objfile using GNU index. As Ada
3490 does not look for non-Ada symbols this function should just return. */
3494 dw2_expand_symtabs_matching
3495 (struct objfile *objfile,
3496 int (*file_matcher) (const char *, void *, int basenames),
3497 int (*name_matcher) (const char *, void *),
3498 enum search_domain kind,
3503 struct mapped_index *index;
3505 dw2_setup (objfile);
3507 /* index_table is NULL if OBJF_READNOW. */
3508 if (!dwarf2_per_objfile->index_table)
3510 index = dwarf2_per_objfile->index_table;
3512 if (file_matcher != NULL)
3514 struct cleanup *cleanup;
3515 htab_t visited_found, visited_not_found;
3517 visited_found = htab_create_alloc (10,
3518 htab_hash_pointer, htab_eq_pointer,
3519 NULL, xcalloc, xfree);
3520 cleanup = make_cleanup_htab_delete (visited_found);
3521 visited_not_found = htab_create_alloc (10,
3522 htab_hash_pointer, htab_eq_pointer,
3523 NULL, xcalloc, xfree);
3524 make_cleanup_htab_delete (visited_not_found);
3526 /* The rule is CUs specify all the files, including those used by
3527 any TU, so there's no need to scan TUs here. */
3529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3532 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3533 struct quick_file_names *file_data;
3536 per_cu->v.quick->mark = 0;
3538 /* We only need to look at symtabs not already expanded. */
3539 if (per_cu->v.quick->symtab)
3542 file_data = dw2_get_file_names (per_cu);
3543 if (file_data == NULL)
3546 if (htab_find (visited_not_found, file_data) != NULL)
3548 else if (htab_find (visited_found, file_data) != NULL)
3550 per_cu->v.quick->mark = 1;
3554 for (j = 0; j < file_data->num_file_names; ++j)
3556 const char *this_real_name;
3558 if (file_matcher (file_data->file_names[j], data, 0))
3560 per_cu->v.quick->mark = 1;
3564 /* Before we invoke realpath, which can get expensive when many
3565 files are involved, do a quick comparison of the basenames. */
3566 if (!basenames_may_differ
3567 && !file_matcher (lbasename (file_data->file_names[j]),
3571 this_real_name = dw2_get_real_path (objfile, file_data, j);
3572 if (file_matcher (this_real_name, data, 0))
3574 per_cu->v.quick->mark = 1;
3579 slot = htab_find_slot (per_cu->v.quick->mark
3581 : visited_not_found,
3586 do_cleanups (cleanup);
3589 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3591 offset_type idx = 2 * iter;
3593 offset_type *vec, vec_len, vec_idx;
3595 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3598 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3600 if (! (*name_matcher) (name, data))
3603 /* The name was matched, now expand corresponding CUs that were
3605 vec = (offset_type *) (index->constant_pool
3606 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3607 vec_len = MAYBE_SWAP (vec[0]);
3608 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3610 struct dwarf2_per_cu_data *per_cu;
3611 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3612 gdb_index_symbol_kind symbol_kind =
3613 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3614 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3616 /* Don't crash on bad data. */
3617 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3618 + dwarf2_per_objfile->n_type_units))
3621 /* Only check the symbol's kind if it has one.
3622 Indices prior to version 7 don't record it. */
3623 if (index->version >= 7)
3627 case VARIABLES_DOMAIN:
3628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3631 case FUNCTIONS_DOMAIN:
3632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3644 per_cu = dw2_get_cu (cu_index);
3645 if (file_matcher == NULL || per_cu->v.quick->mark)
3646 dw2_instantiate_symtab (per_cu);
3651 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3654 static struct symtab *
3655 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3659 if (BLOCKVECTOR (symtab) != NULL
3660 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3663 if (symtab->includes == NULL)
3666 for (i = 0; symtab->includes[i]; ++i)
3668 struct symtab *s = symtab->includes[i];
3670 s = recursively_find_pc_sect_symtab (s, pc);
3678 static struct symtab *
3679 dw2_find_pc_sect_symtab (struct objfile *objfile,
3680 struct minimal_symbol *msymbol,
3682 struct obj_section *section,
3685 struct dwarf2_per_cu_data *data;
3686 struct symtab *result;
3688 dw2_setup (objfile);
3690 if (!objfile->psymtabs_addrmap)
3693 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3697 if (warn_if_readin && data->v.quick->symtab)
3698 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3699 paddress (get_objfile_arch (objfile), pc));
3701 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3702 gdb_assert (result != NULL);
3707 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3708 void *data, int need_fullname)
3711 struct cleanup *cleanup;
3712 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3713 NULL, xcalloc, xfree);
3715 cleanup = make_cleanup_htab_delete (visited);
3716 dw2_setup (objfile);
3718 /* The rule is CUs specify all the files, including those used by
3719 any TU, so there's no need to scan TUs here.
3720 We can ignore file names coming from already-expanded CUs. */
3722 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3724 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3726 if (per_cu->v.quick->symtab)
3728 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3731 *slot = per_cu->v.quick->file_names;
3735 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3738 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3739 struct quick_file_names *file_data;
3742 /* We only need to look at symtabs not already expanded. */
3743 if (per_cu->v.quick->symtab)
3746 file_data = dw2_get_file_names (per_cu);
3747 if (file_data == NULL)
3750 slot = htab_find_slot (visited, file_data, INSERT);
3753 /* Already visited. */
3758 for (j = 0; j < file_data->num_file_names; ++j)
3760 const char *this_real_name;
3763 this_real_name = dw2_get_real_path (objfile, file_data, j);
3765 this_real_name = NULL;
3766 (*fun) (file_data->file_names[j], this_real_name, data);
3770 do_cleanups (cleanup);
3774 dw2_has_symbols (struct objfile *objfile)
3779 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3782 dw2_find_last_source_symtab,
3783 dw2_forget_cached_source_info,
3784 dw2_map_symtabs_matching_filename,
3789 dw2_expand_symtabs_for_function,
3790 dw2_expand_all_symtabs,
3791 dw2_expand_symtabs_with_fullname,
3792 dw2_find_symbol_file,
3793 dw2_map_matching_symbols,
3794 dw2_expand_symtabs_matching,
3795 dw2_find_pc_sect_symtab,
3796 dw2_map_symbol_filenames
3799 /* Initialize for reading DWARF for this objfile. Return 0 if this
3800 file will use psymtabs, or 1 if using the GNU index. */
3803 dwarf2_initialize_objfile (struct objfile *objfile)
3805 /* If we're about to read full symbols, don't bother with the
3806 indices. In this case we also don't care if some other debug
3807 format is making psymtabs, because they are all about to be
3809 if ((objfile->flags & OBJF_READNOW))
3813 dwarf2_per_objfile->using_index = 1;
3814 create_all_comp_units (objfile);
3815 create_all_type_units (objfile);
3816 dwarf2_per_objfile->quick_file_names_table =
3817 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3819 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3820 + dwarf2_per_objfile->n_type_units); ++i)
3822 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3825 struct dwarf2_per_cu_quick_data);
3828 /* Return 1 so that gdb sees the "quick" functions. However,
3829 these functions will be no-ops because we will have expanded
3834 if (dwarf2_read_index (objfile))
3842 /* Build a partial symbol table. */
3845 dwarf2_build_psymtabs (struct objfile *objfile)
3847 volatile struct gdb_exception except;
3849 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3851 init_psymbol_list (objfile, 1024);
3854 TRY_CATCH (except, RETURN_MASK_ERROR)
3856 /* This isn't really ideal: all the data we allocate on the
3857 objfile's obstack is still uselessly kept around. However,
3858 freeing it seems unsafe. */
3859 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3861 dwarf2_build_psymtabs_hard (objfile);
3862 discard_cleanups (cleanups);
3864 if (except.reason < 0)
3865 exception_print (gdb_stderr, except);
3868 /* Return the total length of the CU described by HEADER. */
3871 get_cu_length (const struct comp_unit_head *header)
3873 return header->initial_length_size + header->length;
3876 /* Return TRUE if OFFSET is within CU_HEADER. */
3879 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3881 sect_offset bottom = { cu_header->offset.sect_off };
3882 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3884 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3887 /* Find the base address of the compilation unit for range lists and
3888 location lists. It will normally be specified by DW_AT_low_pc.
3889 In DWARF-3 draft 4, the base address could be overridden by
3890 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3891 compilation units with discontinuous ranges. */
3894 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3896 struct attribute *attr;
3899 cu->base_address = 0;
3901 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3904 cu->base_address = DW_ADDR (attr);
3909 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3912 cu->base_address = DW_ADDR (attr);
3918 /* Read in the comp unit header information from the debug_info at info_ptr.
3919 NOTE: This leaves members offset, first_die_offset to be filled in
3923 read_comp_unit_head (struct comp_unit_head *cu_header,
3924 gdb_byte *info_ptr, bfd *abfd)
3927 unsigned int bytes_read;
3929 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3930 cu_header->initial_length_size = bytes_read;
3931 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3932 info_ptr += bytes_read;
3933 cu_header->version = read_2_bytes (abfd, info_ptr);
3935 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3937 info_ptr += bytes_read;
3938 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3940 signed_addr = bfd_get_sign_extend_vma (abfd);
3941 if (signed_addr < 0)
3942 internal_error (__FILE__, __LINE__,
3943 _("read_comp_unit_head: dwarf from non elf file"));
3944 cu_header->signed_addr_p = signed_addr;
3949 /* Helper function that returns the proper abbrev section for
3952 static struct dwarf2_section_info *
3953 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3955 struct dwarf2_section_info *abbrev;
3957 if (this_cu->is_dwz)
3958 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3960 abbrev = &dwarf2_per_objfile->abbrev;
3965 /* Subroutine of read_and_check_comp_unit_head and
3966 read_and_check_type_unit_head to simplify them.
3967 Perform various error checking on the header. */
3970 error_check_comp_unit_head (struct comp_unit_head *header,
3971 struct dwarf2_section_info *section,
3972 struct dwarf2_section_info *abbrev_section)
3974 bfd *abfd = section->asection->owner;
3975 const char *filename = bfd_get_filename (abfd);
3977 if (header->version != 2 && header->version != 3 && header->version != 4)
3978 error (_("Dwarf Error: wrong version in compilation unit header "
3979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3982 if (header->abbrev_offset.sect_off
3983 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3984 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3985 "(offset 0x%lx + 6) [in module %s]"),
3986 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3989 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3990 avoid potential 32-bit overflow. */
3991 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 0) [in module %s]"),
3995 (long) header->length, (long) header->offset.sect_off,
3999 /* Read in a CU/TU header and perform some basic error checking.
4000 The contents of the header are stored in HEADER.
4001 The result is a pointer to the start of the first DIE. */
4004 read_and_check_comp_unit_head (struct comp_unit_head *header,
4005 struct dwarf2_section_info *section,
4006 struct dwarf2_section_info *abbrev_section,
4008 int is_debug_types_section)
4010 gdb_byte *beg_of_comp_unit = info_ptr;
4011 bfd *abfd = section->asection->owner;
4013 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4015 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4017 /* If we're reading a type unit, skip over the signature and
4018 type_offset fields. */
4019 if (is_debug_types_section)
4020 info_ptr += 8 /*signature*/ + header->offset_size;
4022 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4024 error_check_comp_unit_head (header, section, abbrev_section);
4029 /* Read in the types comp unit header information from .debug_types entry at
4030 types_ptr. The result is a pointer to one past the end of the header. */
4033 read_and_check_type_unit_head (struct comp_unit_head *header,
4034 struct dwarf2_section_info *section,
4035 struct dwarf2_section_info *abbrev_section,
4037 ULONGEST *signature,
4038 cu_offset *type_offset_in_tu)
4040 gdb_byte *beg_of_comp_unit = info_ptr;
4041 bfd *abfd = section->asection->owner;
4043 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4045 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4047 /* If we're reading a type unit, skip over the signature and
4048 type_offset fields. */
4049 if (signature != NULL)
4050 *signature = read_8_bytes (abfd, info_ptr);
4052 if (type_offset_in_tu != NULL)
4053 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4054 header->offset_size);
4055 info_ptr += header->offset_size;
4057 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4059 error_check_comp_unit_head (header, section, abbrev_section);
4064 /* Fetch the abbreviation table offset from a comp or type unit header. */
4067 read_abbrev_offset (struct dwarf2_section_info *section,
4070 bfd *abfd = section->asection->owner;
4072 unsigned int length, initial_length_size, offset_size;
4073 sect_offset abbrev_offset;
4075 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4076 info_ptr = section->buffer + offset.sect_off;
4077 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4078 offset_size = initial_length_size == 4 ? 4 : 8;
4079 info_ptr += initial_length_size + 2 /*version*/;
4080 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4081 return abbrev_offset;
4084 /* Allocate a new partial symtab for file named NAME and mark this new
4085 partial symtab as being an include of PST. */
4088 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4089 struct objfile *objfile)
4091 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4093 if (!IS_ABSOLUTE_PATH (subpst->filename))
4095 /* It shares objfile->objfile_obstack. */
4096 subpst->dirname = pst->dirname;
4099 subpst->section_offsets = pst->section_offsets;
4100 subpst->textlow = 0;
4101 subpst->texthigh = 0;
4103 subpst->dependencies = (struct partial_symtab **)
4104 obstack_alloc (&objfile->objfile_obstack,
4105 sizeof (struct partial_symtab *));
4106 subpst->dependencies[0] = pst;
4107 subpst->number_of_dependencies = 1;
4109 subpst->globals_offset = 0;
4110 subpst->n_global_syms = 0;
4111 subpst->statics_offset = 0;
4112 subpst->n_static_syms = 0;
4113 subpst->symtab = NULL;
4114 subpst->read_symtab = pst->read_symtab;
4117 /* No private part is necessary for include psymtabs. This property
4118 can be used to differentiate between such include psymtabs and
4119 the regular ones. */
4120 subpst->read_symtab_private = NULL;
4123 /* Read the Line Number Program data and extract the list of files
4124 included by the source file represented by PST. Build an include
4125 partial symtab for each of these included files. */
4128 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4129 struct die_info *die,
4130 struct partial_symtab *pst)
4132 struct line_header *lh = NULL;
4133 struct attribute *attr;
4135 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4137 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4139 return; /* No linetable, so no includes. */
4141 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4142 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4144 free_line_header (lh);
4148 hash_signatured_type (const void *item)
4150 const struct signatured_type *sig_type = item;
4152 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4153 return sig_type->signature;
4157 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4159 const struct signatured_type *lhs = item_lhs;
4160 const struct signatured_type *rhs = item_rhs;
4162 return lhs->signature == rhs->signature;
4165 /* Allocate a hash table for signatured types. */
4168 allocate_signatured_type_table (struct objfile *objfile)
4170 return htab_create_alloc_ex (41,
4171 hash_signatured_type,
4174 &objfile->objfile_obstack,
4175 hashtab_obstack_allocate,
4176 dummy_obstack_deallocate);
4179 /* A helper function to add a signatured type CU to a table. */
4182 add_signatured_type_cu_to_table (void **slot, void *datum)
4184 struct signatured_type *sigt = *slot;
4185 struct signatured_type ***datap = datum;
4193 /* Create the hash table of all entries in the .debug_types section.
4194 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4196 Note: This function processes DWO files only, not DWP files.
4197 The result is a pointer to the hash table or NULL if there are
4201 create_debug_types_hash_table (struct dwo_file *dwo_file,
4202 VEC (dwarf2_section_info_def) *types)
4204 struct objfile *objfile = dwarf2_per_objfile->objfile;
4205 htab_t types_htab = NULL;
4207 struct dwarf2_section_info *section;
4208 struct dwarf2_section_info *abbrev_section;
4210 if (VEC_empty (dwarf2_section_info_def, types))
4213 abbrev_section = (dwo_file != NULL
4214 ? &dwo_file->sections.abbrev
4215 : &dwarf2_per_objfile->abbrev);
4217 if (dwarf2_read_debug)
4218 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4219 dwo_file ? ".dwo" : "",
4220 bfd_get_filename (abbrev_section->asection->owner));
4223 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4227 gdb_byte *info_ptr, *end_ptr;
4228 struct dwarf2_section_info *abbrev_section;
4230 dwarf2_read_section (objfile, section);
4231 info_ptr = section->buffer;
4233 if (info_ptr == NULL)
4236 /* We can't set abfd until now because the section may be empty or
4237 not present, in which case section->asection will be NULL. */
4238 abfd = section->asection->owner;
4241 abbrev_section = &dwo_file->sections.abbrev;
4243 abbrev_section = &dwarf2_per_objfile->abbrev;
4245 if (types_htab == NULL)
4248 types_htab = allocate_dwo_unit_table (objfile);
4250 types_htab = allocate_signatured_type_table (objfile);
4253 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4254 because we don't need to read any dies: the signature is in the
4257 end_ptr = info_ptr + section->size;
4258 while (info_ptr < end_ptr)
4261 cu_offset type_offset_in_tu;
4263 struct signatured_type *sig_type;
4264 struct dwo_unit *dwo_tu;
4266 gdb_byte *ptr = info_ptr;
4267 struct comp_unit_head header;
4268 unsigned int length;
4270 offset.sect_off = ptr - section->buffer;
4272 /* We need to read the type's signature in order to build the hash
4273 table, but we don't need anything else just yet. */
4275 ptr = read_and_check_type_unit_head (&header, section,
4276 abbrev_section, ptr,
4277 &signature, &type_offset_in_tu);
4279 length = get_cu_length (&header);
4281 /* Skip dummy type units. */
4282 if (ptr >= info_ptr + length
4283 || peek_abbrev_code (abfd, ptr) == 0)
4292 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4294 dwo_tu->dwo_file = dwo_file;
4295 dwo_tu->signature = signature;
4296 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4297 dwo_tu->section = section;
4298 dwo_tu->offset = offset;
4299 dwo_tu->length = length;
4303 /* N.B.: type_offset is not usable if this type uses a DWO file.
4304 The real type_offset is in the DWO file. */
4306 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4307 struct signatured_type);
4308 sig_type->signature = signature;
4309 sig_type->type_offset_in_tu = type_offset_in_tu;
4310 sig_type->per_cu.objfile = objfile;
4311 sig_type->per_cu.is_debug_types = 1;
4312 sig_type->per_cu.section = section;
4313 sig_type->per_cu.offset = offset;
4314 sig_type->per_cu.length = length;
4317 slot = htab_find_slot (types_htab,
4318 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4320 gdb_assert (slot != NULL);
4323 sect_offset dup_offset;
4327 const struct dwo_unit *dup_tu = *slot;
4329 dup_offset = dup_tu->offset;
4333 const struct signatured_type *dup_tu = *slot;
4335 dup_offset = dup_tu->per_cu.offset;
4338 complaint (&symfile_complaints,
4339 _("debug type entry at offset 0x%x is duplicate to the "
4340 "entry at offset 0x%x, signature 0x%s"),
4341 offset.sect_off, dup_offset.sect_off,
4342 phex (signature, sizeof (signature)));
4344 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4346 if (dwarf2_read_debug)
4347 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4349 phex (signature, sizeof (signature)));
4358 /* Create the hash table of all entries in the .debug_types section,
4359 and initialize all_type_units.
4360 The result is zero if there is an error (e.g. missing .debug_types section),
4361 otherwise non-zero. */
4364 create_all_type_units (struct objfile *objfile)
4367 struct signatured_type **iter;
4369 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4370 if (types_htab == NULL)
4372 dwarf2_per_objfile->signatured_types = NULL;
4376 dwarf2_per_objfile->signatured_types = types_htab;
4378 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4379 dwarf2_per_objfile->all_type_units
4380 = obstack_alloc (&objfile->objfile_obstack,
4381 dwarf2_per_objfile->n_type_units
4382 * sizeof (struct signatured_type *));
4383 iter = &dwarf2_per_objfile->all_type_units[0];
4384 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4385 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4386 == dwarf2_per_objfile->n_type_units);
4391 /* Lookup a signature based type for DW_FORM_ref_sig8.
4392 Returns NULL if signature SIG is not present in the table. */
4394 static struct signatured_type *
4395 lookup_signatured_type (ULONGEST sig)
4397 struct signatured_type find_entry, *entry;
4399 if (dwarf2_per_objfile->signatured_types == NULL)
4401 complaint (&symfile_complaints,
4402 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4406 find_entry.signature = sig;
4407 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4411 /* Low level DIE reading support. */
4413 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4416 init_cu_die_reader (struct die_reader_specs *reader,
4417 struct dwarf2_cu *cu,
4418 struct dwarf2_section_info *section,
4419 struct dwo_file *dwo_file)
4421 gdb_assert (section->readin && section->buffer != NULL);
4422 reader->abfd = section->asection->owner;
4424 reader->dwo_file = dwo_file;
4425 reader->die_section = section;
4426 reader->buffer = section->buffer;
4427 reader->buffer_end = section->buffer + section->size;
4430 /* Initialize a CU (or TU) and read its DIEs.
4431 If the CU defers to a DWO file, read the DWO file as well.
4433 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4434 Otherwise the table specified in the comp unit header is read in and used.
4435 This is an optimization for when we already have the abbrev table.
4437 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4438 Otherwise, a new CU is allocated with xmalloc.
4440 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4441 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4443 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4444 linker) then DIE_READER_FUNC will not get called. */
4447 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4448 struct abbrev_table *abbrev_table,
4449 int use_existing_cu, int keep,
4450 die_reader_func_ftype *die_reader_func,
4453 struct objfile *objfile = dwarf2_per_objfile->objfile;
4454 struct dwarf2_section_info *section = this_cu->section;
4455 bfd *abfd = section->asection->owner;
4456 struct dwarf2_cu *cu;
4457 gdb_byte *begin_info_ptr, *info_ptr;
4458 struct die_reader_specs reader;
4459 struct die_info *comp_unit_die;
4461 struct attribute *attr;
4462 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4463 struct signatured_type *sig_type = NULL;
4464 struct dwarf2_section_info *abbrev_section;
4465 /* Non-zero if CU currently points to a DWO file and we need to
4466 reread it. When this happens we need to reread the skeleton die
4467 before we can reread the DWO file. */
4468 int rereading_dwo_cu = 0;
4470 if (dwarf2_die_debug)
4471 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4472 this_cu->is_debug_types ? "type" : "comp",
4473 this_cu->offset.sect_off);
4475 if (use_existing_cu)
4478 cleanups = make_cleanup (null_cleanup, NULL);
4480 /* This is cheap if the section is already read in. */
4481 dwarf2_read_section (objfile, section);
4483 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4485 abbrev_section = get_abbrev_section_for_cu (this_cu);
4487 if (use_existing_cu && this_cu->cu != NULL)
4491 /* If this CU is from a DWO file we need to start over, we need to
4492 refetch the attributes from the skeleton CU.
4493 This could be optimized by retrieving those attributes from when we
4494 were here the first time: the previous comp_unit_die was stored in
4495 comp_unit_obstack. But there's no data yet that we need this
4497 if (cu->dwo_unit != NULL)
4498 rereading_dwo_cu = 1;
4502 /* If !use_existing_cu, this_cu->cu must be NULL. */
4503 gdb_assert (this_cu->cu == NULL);
4505 cu = xmalloc (sizeof (*cu));
4506 init_one_comp_unit (cu, this_cu);
4508 /* If an error occurs while loading, release our storage. */
4509 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4512 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4514 /* We already have the header, there's no need to read it in again. */
4515 info_ptr += cu->header.first_die_offset.cu_off;
4519 if (this_cu->is_debug_types)
4522 cu_offset type_offset_in_tu;
4524 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4525 abbrev_section, info_ptr,
4527 &type_offset_in_tu);
4529 /* Since per_cu is the first member of struct signatured_type,
4530 we can go from a pointer to one to a pointer to the other. */
4531 sig_type = (struct signatured_type *) this_cu;
4532 gdb_assert (sig_type->signature == signature);
4533 gdb_assert (sig_type->type_offset_in_tu.cu_off
4534 == type_offset_in_tu.cu_off);
4535 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4537 /* LENGTH has not been set yet for type units if we're
4538 using .gdb_index. */
4539 this_cu->length = get_cu_length (&cu->header);
4541 /* Establish the type offset that can be used to lookup the type. */
4542 sig_type->type_offset_in_section.sect_off =
4543 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4547 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4551 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4552 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4556 /* Skip dummy compilation units. */
4557 if (info_ptr >= begin_info_ptr + this_cu->length
4558 || peek_abbrev_code (abfd, info_ptr) == 0)
4560 do_cleanups (cleanups);
4564 /* If we don't have them yet, read the abbrevs for this compilation unit.
4565 And if we need to read them now, make sure they're freed when we're
4566 done. Note that it's important that if the CU had an abbrev table
4567 on entry we don't free it when we're done: Somewhere up the call stack
4568 it may be in use. */
4569 if (abbrev_table != NULL)
4571 gdb_assert (cu->abbrev_table == NULL);
4572 gdb_assert (cu->header.abbrev_offset.sect_off
4573 == abbrev_table->offset.sect_off);
4574 cu->abbrev_table = abbrev_table;
4576 else if (cu->abbrev_table == NULL)
4578 dwarf2_read_abbrevs (cu, abbrev_section);
4579 make_cleanup (dwarf2_free_abbrev_table, cu);
4581 else if (rereading_dwo_cu)
4583 dwarf2_free_abbrev_table (cu);
4584 dwarf2_read_abbrevs (cu, abbrev_section);
4587 /* Read the top level CU/TU die. */
4588 init_cu_die_reader (&reader, cu, section, NULL);
4589 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4591 /* If we have a DWO stub, process it and then read in the DWO file.
4592 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4593 a DWO CU, that this test will fail. */
4594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4597 const char *dwo_name = DW_STRING (attr);
4598 const char *comp_dir_string;
4599 struct dwo_unit *dwo_unit;
4600 ULONGEST signature; /* Or dwo_id. */
4601 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4602 int i,num_extra_attrs;
4603 struct dwarf2_section_info *dwo_abbrev_section;
4606 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4607 " has children (offset 0x%x) [in module %s]"),
4608 this_cu->offset.sect_off, bfd_get_filename (abfd));
4610 /* These attributes aren't processed until later:
4611 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4612 However, the attribute is found in the stub which we won't have later.
4613 In order to not impose this complication on the rest of the code,
4614 we read them here and copy them to the DWO CU/TU die. */
4616 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4619 if (! this_cu->is_debug_types)
4620 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4621 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4622 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4623 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4624 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4626 /* There should be a DW_AT_addr_base attribute here (if needed).
4627 We need the value before we can process DW_FORM_GNU_addr_index. */
4629 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4631 cu->addr_base = DW_UNSND (attr);
4633 /* There should be a DW_AT_ranges_base attribute here (if needed).
4634 We need the value before we can process DW_AT_ranges. */
4635 cu->ranges_base = 0;
4636 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4638 cu->ranges_base = DW_UNSND (attr);
4640 if (this_cu->is_debug_types)
4642 gdb_assert (sig_type != NULL);
4643 signature = sig_type->signature;
4647 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4649 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4651 signature = DW_UNSND (attr);
4654 /* We may need the comp_dir in order to find the DWO file. */
4655 comp_dir_string = NULL;
4657 comp_dir_string = DW_STRING (comp_dir);
4659 if (this_cu->is_debug_types)
4660 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4662 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4665 if (dwo_unit == NULL)
4667 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4668 " with ID %s [in module %s]"),
4669 this_cu->offset.sect_off,
4670 phex (signature, sizeof (signature)),
4674 /* Set up for reading the DWO CU/TU. */
4675 cu->dwo_unit = dwo_unit;
4676 section = dwo_unit->section;
4677 dwarf2_read_section (objfile, section);
4678 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4679 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4680 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4682 if (this_cu->is_debug_types)
4685 cu_offset type_offset_in_tu;
4687 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4691 &type_offset_in_tu);
4692 gdb_assert (sig_type->signature == signature);
4693 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4694 /* For DWOs coming from DWP files, we don't know the CU length
4695 nor the type's offset in the TU until now. */
4696 dwo_unit->length = get_cu_length (&cu->header);
4697 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4699 /* Establish the type offset that can be used to lookup the type.
4700 For DWO files, we don't know it until now. */
4701 sig_type->type_offset_in_section.sect_off =
4702 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4706 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4709 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4710 /* For DWOs coming from DWP files, we don't know the CU length
4712 dwo_unit->length = get_cu_length (&cu->header);
4715 /* Discard the original CU's abbrev table, and read the DWO's. */
4716 if (abbrev_table == NULL)
4718 dwarf2_free_abbrev_table (cu);
4719 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4723 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4724 make_cleanup (dwarf2_free_abbrev_table, cu);
4727 /* Read in the die, but leave space to copy over the attributes
4728 from the stub. This has the benefit of simplifying the rest of
4729 the code - all the real work is done here. */
4730 num_extra_attrs = ((stmt_list != NULL)
4734 + (comp_dir != NULL));
4735 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4736 &has_children, num_extra_attrs);
4738 /* Copy over the attributes from the stub to the DWO die. */
4739 i = comp_unit_die->num_attrs;
4740 if (stmt_list != NULL)
4741 comp_unit_die->attrs[i++] = *stmt_list;
4743 comp_unit_die->attrs[i++] = *low_pc;
4744 if (high_pc != NULL)
4745 comp_unit_die->attrs[i++] = *high_pc;
4747 comp_unit_die->attrs[i++] = *ranges;
4748 if (comp_dir != NULL)
4749 comp_unit_die->attrs[i++] = *comp_dir;
4750 comp_unit_die->num_attrs += num_extra_attrs;
4752 /* Skip dummy compilation units. */
4753 if (info_ptr >= begin_info_ptr + dwo_unit->length
4754 || peek_abbrev_code (abfd, info_ptr) == 0)
4756 do_cleanups (cleanups);
4761 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4763 if (free_cu_cleanup != NULL)
4767 /* We've successfully allocated this compilation unit. Let our
4768 caller clean it up when finished with it. */
4769 discard_cleanups (free_cu_cleanup);
4771 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4772 So we have to manually free the abbrev table. */
4773 dwarf2_free_abbrev_table (cu);
4775 /* Link this CU into read_in_chain. */
4776 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4777 dwarf2_per_objfile->read_in_chain = this_cu;
4780 do_cleanups (free_cu_cleanup);
4783 do_cleanups (cleanups);
4786 /* Read CU/TU THIS_CU in section SECTION,
4787 but do not follow DW_AT_GNU_dwo_name if present.
4788 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4789 to have already done the lookup to find the DWO/DWP file).
4791 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4792 THIS_CU->is_debug_types, but nothing else.
4794 We fill in THIS_CU->length.
4796 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4797 linker) then DIE_READER_FUNC will not get called.
4799 THIS_CU->cu is always freed when done.
4800 This is done in order to not leave THIS_CU->cu in a state where we have
4801 to care whether it refers to the "main" CU or the DWO CU. */
4804 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4805 struct dwarf2_section_info *abbrev_section,
4806 struct dwo_file *dwo_file,
4807 die_reader_func_ftype *die_reader_func,
4810 struct objfile *objfile = dwarf2_per_objfile->objfile;
4811 struct dwarf2_section_info *section = this_cu->section;
4812 bfd *abfd = section->asection->owner;
4813 struct dwarf2_cu cu;
4814 gdb_byte *begin_info_ptr, *info_ptr;
4815 struct die_reader_specs reader;
4816 struct cleanup *cleanups;
4817 struct die_info *comp_unit_die;
4820 if (dwarf2_die_debug)
4821 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4822 this_cu->is_debug_types ? "type" : "comp",
4823 this_cu->offset.sect_off);
4825 gdb_assert (this_cu->cu == NULL);
4827 /* This is cheap if the section is already read in. */
4828 dwarf2_read_section (objfile, section);
4830 init_one_comp_unit (&cu, this_cu);
4832 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4834 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4835 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4836 abbrev_section, info_ptr,
4837 this_cu->is_debug_types);
4839 this_cu->length = get_cu_length (&cu.header);
4841 /* Skip dummy compilation units. */
4842 if (info_ptr >= begin_info_ptr + this_cu->length
4843 || peek_abbrev_code (abfd, info_ptr) == 0)
4845 do_cleanups (cleanups);
4849 dwarf2_read_abbrevs (&cu, abbrev_section);
4850 make_cleanup (dwarf2_free_abbrev_table, &cu);
4852 init_cu_die_reader (&reader, &cu, section, dwo_file);
4853 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4855 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4857 do_cleanups (cleanups);
4860 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4861 does not lookup the specified DWO file.
4862 This cannot be used to read DWO files.
4864 THIS_CU->cu is always freed when done.
4865 This is done in order to not leave THIS_CU->cu in a state where we have
4866 to care whether it refers to the "main" CU or the DWO CU.
4867 We can revisit this if the data shows there's a performance issue. */
4870 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4871 die_reader_func_ftype *die_reader_func,
4874 init_cutu_and_read_dies_no_follow (this_cu,
4875 get_abbrev_section_for_cu (this_cu),
4877 die_reader_func, data);
4880 /* Type Unit Groups.
4882 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4883 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4884 so that all types coming from the same compilation (.o file) are grouped
4885 together. A future step could be to put the types in the same symtab as
4886 the CU the types ultimately came from. */
4889 hash_type_unit_group (const void *item)
4891 const struct type_unit_group *tu_group = item;
4893 return hash_stmt_list_entry (&tu_group->hash);
4897 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4899 const struct type_unit_group *lhs = item_lhs;
4900 const struct type_unit_group *rhs = item_rhs;
4902 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4905 /* Allocate a hash table for type unit groups. */
4908 allocate_type_unit_groups_table (void)
4910 return htab_create_alloc_ex (3,
4911 hash_type_unit_group,
4914 &dwarf2_per_objfile->objfile->objfile_obstack,
4915 hashtab_obstack_allocate,
4916 dummy_obstack_deallocate);
4919 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4920 partial symtabs. We combine several TUs per psymtab to not let the size
4921 of any one psymtab grow too big. */
4922 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4923 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4925 /* Helper routine for get_type_unit_group.
4926 Create the type_unit_group object used to hold one or more TUs. */
4928 static struct type_unit_group *
4929 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4931 struct objfile *objfile = dwarf2_per_objfile->objfile;
4932 struct dwarf2_per_cu_data *per_cu;
4933 struct type_unit_group *tu_group;
4935 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4936 struct type_unit_group);
4937 per_cu = &tu_group->per_cu;
4938 per_cu->objfile = objfile;
4939 per_cu->is_debug_types = 1;
4940 per_cu->type_unit_group = tu_group;
4942 if (dwarf2_per_objfile->using_index)
4944 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4945 struct dwarf2_per_cu_quick_data);
4946 tu_group->t.first_tu = cu->per_cu;
4950 unsigned int line_offset = line_offset_struct.sect_off;
4951 struct partial_symtab *pst;
4954 /* Give the symtab a useful name for debug purposes. */
4955 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4956 name = xstrprintf ("<type_units_%d>",
4957 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4959 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4961 pst = create_partial_symtab (per_cu, name);
4967 tu_group->hash.dwo_unit = cu->dwo_unit;
4968 tu_group->hash.line_offset = line_offset_struct;
4973 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4974 STMT_LIST is a DW_AT_stmt_list attribute. */
4976 static struct type_unit_group *
4977 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4979 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4980 struct type_unit_group *tu_group;
4982 unsigned int line_offset;
4983 struct type_unit_group type_unit_group_for_lookup;
4985 if (dwarf2_per_objfile->type_unit_groups == NULL)
4987 dwarf2_per_objfile->type_unit_groups =
4988 allocate_type_unit_groups_table ();
4991 /* Do we need to create a new group, or can we use an existing one? */
4995 line_offset = DW_UNSND (stmt_list);
4996 ++tu_stats->nr_symtab_sharers;
5000 /* Ugh, no stmt_list. Rare, but we have to handle it.
5001 We can do various things here like create one group per TU or
5002 spread them over multiple groups to split up the expansion work.
5003 To avoid worst case scenarios (too many groups or too large groups)
5004 we, umm, group them in bunches. */
5005 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5006 | (tu_stats->nr_stmt_less_type_units
5007 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5008 ++tu_stats->nr_stmt_less_type_units;
5011 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5012 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5013 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5014 &type_unit_group_for_lookup, INSERT);
5018 gdb_assert (tu_group != NULL);
5022 sect_offset line_offset_struct;
5024 line_offset_struct.sect_off = line_offset;
5025 tu_group = create_type_unit_group (cu, line_offset_struct);
5027 ++tu_stats->nr_symtabs;
5033 /* Struct used to sort TUs by their abbreviation table offset. */
5035 struct tu_abbrev_offset
5037 struct signatured_type *sig_type;
5038 sect_offset abbrev_offset;
5041 /* Helper routine for build_type_unit_groups, passed to qsort. */
5044 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5046 const struct tu_abbrev_offset * const *a = ap;
5047 const struct tu_abbrev_offset * const *b = bp;
5048 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5049 unsigned int boff = (*b)->abbrev_offset.sect_off;
5051 return (aoff > boff) - (aoff < boff);
5054 /* A helper function to add a type_unit_group to a table. */
5057 add_type_unit_group_to_table (void **slot, void *datum)
5059 struct type_unit_group *tu_group = *slot;
5060 struct type_unit_group ***datap = datum;
5068 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5069 each one passing FUNC,DATA.
5071 The efficiency is because we sort TUs by the abbrev table they use and
5072 only read each abbrev table once. In one program there are 200K TUs
5073 sharing 8K abbrev tables.
5075 The main purpose of this function is to support building the
5076 dwarf2_per_objfile->type_unit_groups table.
5077 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5078 can collapse the search space by grouping them by stmt_list.
5079 The savings can be significant, in the same program from above the 200K TUs
5080 share 8K stmt_list tables.
5082 FUNC is expected to call get_type_unit_group, which will create the
5083 struct type_unit_group if necessary and add it to
5084 dwarf2_per_objfile->type_unit_groups. */
5087 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5089 struct objfile *objfile = dwarf2_per_objfile->objfile;
5090 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5091 struct cleanup *cleanups;
5092 struct abbrev_table *abbrev_table;
5093 sect_offset abbrev_offset;
5094 struct tu_abbrev_offset *sorted_by_abbrev;
5095 struct type_unit_group **iter;
5098 /* It's up to the caller to not call us multiple times. */
5099 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5101 if (dwarf2_per_objfile->n_type_units == 0)
5104 /* TUs typically share abbrev tables, and there can be way more TUs than
5105 abbrev tables. Sort by abbrev table to reduce the number of times we
5106 read each abbrev table in.
5107 Alternatives are to punt or to maintain a cache of abbrev tables.
5108 This is simpler and efficient enough for now.
5110 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5111 symtab to use). Typically TUs with the same abbrev offset have the same
5112 stmt_list value too so in practice this should work well.
5114 The basic algorithm here is:
5116 sort TUs by abbrev table
5117 for each TU with same abbrev table:
5118 read abbrev table if first user
5119 read TU top level DIE
5120 [IWBN if DWO skeletons had DW_AT_stmt_list]
5123 if (dwarf2_read_debug)
5124 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5126 /* Sort in a separate table to maintain the order of all_type_units
5127 for .gdb_index: TU indices directly index all_type_units. */
5128 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5129 dwarf2_per_objfile->n_type_units);
5130 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5132 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5134 sorted_by_abbrev[i].sig_type = sig_type;
5135 sorted_by_abbrev[i].abbrev_offset =
5136 read_abbrev_offset (sig_type->per_cu.section,
5137 sig_type->per_cu.offset);
5139 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5140 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5141 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5143 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5144 called any number of times, so we don't reset tu_stats here. */
5146 abbrev_offset.sect_off = ~(unsigned) 0;
5147 abbrev_table = NULL;
5148 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5150 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5152 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5154 /* Switch to the next abbrev table if necessary. */
5155 if (abbrev_table == NULL
5156 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5158 if (abbrev_table != NULL)
5160 abbrev_table_free (abbrev_table);
5161 /* Reset to NULL in case abbrev_table_read_table throws
5162 an error: abbrev_table_free_cleanup will get called. */
5163 abbrev_table = NULL;
5165 abbrev_offset = tu->abbrev_offset;
5167 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5169 ++tu_stats->nr_uniq_abbrev_tables;
5172 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5176 /* Create a vector of pointers to primary type units to make it easy to
5177 iterate over them and CUs. See dw2_get_primary_cu. */
5178 dwarf2_per_objfile->n_type_unit_groups =
5179 htab_elements (dwarf2_per_objfile->type_unit_groups);
5180 dwarf2_per_objfile->all_type_unit_groups =
5181 obstack_alloc (&objfile->objfile_obstack,
5182 dwarf2_per_objfile->n_type_unit_groups
5183 * sizeof (struct type_unit_group *));
5184 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5185 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5186 add_type_unit_group_to_table, &iter);
5187 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5188 == dwarf2_per_objfile->n_type_unit_groups);
5190 do_cleanups (cleanups);
5192 if (dwarf2_read_debug)
5194 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5195 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5196 dwarf2_per_objfile->n_type_units);
5197 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5198 tu_stats->nr_uniq_abbrev_tables);
5199 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5200 tu_stats->nr_symtabs);
5201 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5202 tu_stats->nr_symtab_sharers);
5203 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5204 tu_stats->nr_stmt_less_type_units);
5208 /* Partial symbol tables. */
5210 /* Create a psymtab named NAME and assign it to PER_CU.
5212 The caller must fill in the following details:
5213 dirname, textlow, texthigh. */
5215 static struct partial_symtab *
5216 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5218 struct objfile *objfile = per_cu->objfile;
5219 struct partial_symtab *pst;
5221 pst = start_psymtab_common (objfile, objfile->section_offsets,
5223 objfile->global_psymbols.next,
5224 objfile->static_psymbols.next);
5226 pst->psymtabs_addrmap_supported = 1;
5228 /* This is the glue that links PST into GDB's symbol API. */
5229 pst->read_symtab_private = per_cu;
5230 pst->read_symtab = dwarf2_read_symtab;
5231 per_cu->v.psymtab = pst;
5236 /* die_reader_func for process_psymtab_comp_unit. */
5239 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5241 struct die_info *comp_unit_die,
5245 struct dwarf2_cu *cu = reader->cu;
5246 struct objfile *objfile = cu->objfile;
5247 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5248 struct attribute *attr;
5250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5251 struct partial_symtab *pst;
5253 const char *filename;
5254 int *want_partial_unit_ptr = data;
5256 if (comp_unit_die->tag == DW_TAG_partial_unit
5257 && (want_partial_unit_ptr == NULL
5258 || !*want_partial_unit_ptr))
5261 gdb_assert (! per_cu->is_debug_types);
5263 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5265 cu->list_in_scope = &file_symbols;
5267 /* Allocate a new partial symbol table structure. */
5268 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5269 if (attr == NULL || !DW_STRING (attr))
5272 filename = DW_STRING (attr);
5274 pst = create_partial_symtab (per_cu, filename);
5276 /* This must be done before calling dwarf2_build_include_psymtabs. */
5277 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5279 pst->dirname = DW_STRING (attr);
5281 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5283 dwarf2_find_base_address (comp_unit_die, cu);
5285 /* Possibly set the default values of LOWPC and HIGHPC from
5287 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5288 &best_highpc, cu, pst);
5289 if (has_pc_info == 1 && best_lowpc < best_highpc)
5290 /* Store the contiguous range if it is not empty; it can be empty for
5291 CUs with no code. */
5292 addrmap_set_empty (objfile->psymtabs_addrmap,
5293 best_lowpc + baseaddr,
5294 best_highpc + baseaddr - 1, pst);
5296 /* Check if comp unit has_children.
5297 If so, read the rest of the partial symbols from this comp unit.
5298 If not, there's no more debug_info for this comp unit. */
5301 struct partial_die_info *first_die;
5302 CORE_ADDR lowpc, highpc;
5304 lowpc = ((CORE_ADDR) -1);
5305 highpc = ((CORE_ADDR) 0);
5307 first_die = load_partial_dies (reader, info_ptr, 1);
5309 scan_partial_symbols (first_die, &lowpc, &highpc,
5312 /* If we didn't find a lowpc, set it to highpc to avoid
5313 complaints from `maint check'. */
5314 if (lowpc == ((CORE_ADDR) -1))
5317 /* If the compilation unit didn't have an explicit address range,
5318 then use the information extracted from its child dies. */
5322 best_highpc = highpc;
5325 pst->textlow = best_lowpc + baseaddr;
5326 pst->texthigh = best_highpc + baseaddr;
5328 pst->n_global_syms = objfile->global_psymbols.next -
5329 (objfile->global_psymbols.list + pst->globals_offset);
5330 pst->n_static_syms = objfile->static_psymbols.next -
5331 (objfile->static_psymbols.list + pst->statics_offset);
5332 sort_pst_symbols (objfile, pst);
5334 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5337 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5338 struct dwarf2_per_cu_data *iter;
5340 /* Fill in 'dependencies' here; we fill in 'users' in a
5342 pst->number_of_dependencies = len;
5343 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5344 len * sizeof (struct symtab *));
5346 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5349 pst->dependencies[i] = iter->v.psymtab;
5351 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5354 /* Get the list of files included in the current compilation unit,
5355 and build a psymtab for each of them. */
5356 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5358 if (dwarf2_read_debug)
5360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5362 fprintf_unfiltered (gdb_stdlog,
5363 "Psymtab for %s unit @0x%x: %s - %s"
5364 ", %d global, %d static syms\n",
5365 per_cu->is_debug_types ? "type" : "comp",
5366 per_cu->offset.sect_off,
5367 paddress (gdbarch, pst->textlow),
5368 paddress (gdbarch, pst->texthigh),
5369 pst->n_global_syms, pst->n_static_syms);
5373 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5374 Process compilation unit THIS_CU for a psymtab. */
5377 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5378 int want_partial_unit)
5380 /* If this compilation unit was already read in, free the
5381 cached copy in order to read it in again. This is
5382 necessary because we skipped some symbols when we first
5383 read in the compilation unit (see load_partial_dies).
5384 This problem could be avoided, but the benefit is unclear. */
5385 if (this_cu->cu != NULL)
5386 free_one_cached_comp_unit (this_cu);
5388 gdb_assert (! this_cu->is_debug_types);
5389 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5390 process_psymtab_comp_unit_reader,
5391 &want_partial_unit);
5393 /* Age out any secondary CUs. */
5394 age_cached_comp_units ();
5397 /* Reader function for build_type_psymtabs. */
5400 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5402 struct die_info *type_unit_die,
5406 struct objfile *objfile = dwarf2_per_objfile->objfile;
5407 struct dwarf2_cu *cu = reader->cu;
5408 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5409 struct type_unit_group *tu_group;
5410 struct attribute *attr;
5411 struct partial_die_info *first_die;
5412 CORE_ADDR lowpc, highpc;
5413 struct partial_symtab *pst;
5415 gdb_assert (data == NULL);
5420 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5421 tu_group = get_type_unit_group (cu, attr);
5423 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5425 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5426 cu->list_in_scope = &file_symbols;
5427 pst = create_partial_symtab (per_cu, "");
5430 first_die = load_partial_dies (reader, info_ptr, 1);
5432 lowpc = (CORE_ADDR) -1;
5433 highpc = (CORE_ADDR) 0;
5434 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5436 pst->n_global_syms = objfile->global_psymbols.next -
5437 (objfile->global_psymbols.list + pst->globals_offset);
5438 pst->n_static_syms = objfile->static_psymbols.next -
5439 (objfile->static_psymbols.list + pst->statics_offset);
5440 sort_pst_symbols (objfile, pst);
5443 /* Traversal function for build_type_psymtabs. */
5446 build_type_psymtab_dependencies (void **slot, void *info)
5448 struct objfile *objfile = dwarf2_per_objfile->objfile;
5449 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5450 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5451 struct partial_symtab *pst = per_cu->v.psymtab;
5452 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5453 struct dwarf2_per_cu_data *iter;
5456 gdb_assert (len > 0);
5458 pst->number_of_dependencies = len;
5459 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5460 len * sizeof (struct psymtab *));
5462 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5465 pst->dependencies[i] = iter->v.psymtab;
5466 iter->type_unit_group = tu_group;
5469 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5474 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5475 Build partial symbol tables for the .debug_types comp-units. */
5478 build_type_psymtabs (struct objfile *objfile)
5480 if (! create_all_type_units (objfile))
5483 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5485 /* Now that all TUs have been processed we can fill in the dependencies. */
5486 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5487 build_type_psymtab_dependencies, NULL);
5490 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5493 psymtabs_addrmap_cleanup (void *o)
5495 struct objfile *objfile = o;
5497 objfile->psymtabs_addrmap = NULL;
5500 /* Compute the 'user' field for each psymtab in OBJFILE. */
5503 set_partial_user (struct objfile *objfile)
5507 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5509 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5510 struct partial_symtab *pst = per_cu->v.psymtab;
5516 for (j = 0; j < pst->number_of_dependencies; ++j)
5518 /* Set the 'user' field only if it is not already set. */
5519 if (pst->dependencies[j]->user == NULL)
5520 pst->dependencies[j]->user = pst;
5525 /* Build the partial symbol table by doing a quick pass through the
5526 .debug_info and .debug_abbrev sections. */
5529 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5531 struct cleanup *back_to, *addrmap_cleanup;
5532 struct obstack temp_obstack;
5535 if (dwarf2_read_debug)
5537 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5541 dwarf2_per_objfile->reading_partial_symbols = 1;
5543 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5545 /* Any cached compilation units will be linked by the per-objfile
5546 read_in_chain. Make sure to free them when we're done. */
5547 back_to = make_cleanup (free_cached_comp_units, NULL);
5549 build_type_psymtabs (objfile);
5551 create_all_comp_units (objfile);
5553 /* Create a temporary address map on a temporary obstack. We later
5554 copy this to the final obstack. */
5555 obstack_init (&temp_obstack);
5556 make_cleanup_obstack_free (&temp_obstack);
5557 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5558 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5560 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5562 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5564 process_psymtab_comp_unit (per_cu, 0);
5567 set_partial_user (objfile);
5569 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5570 &objfile->objfile_obstack);
5571 discard_cleanups (addrmap_cleanup);
5573 do_cleanups (back_to);
5575 if (dwarf2_read_debug)
5576 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5580 /* die_reader_func for load_partial_comp_unit. */
5583 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5585 struct die_info *comp_unit_die,
5589 struct dwarf2_cu *cu = reader->cu;
5591 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5593 /* Check if comp unit has_children.
5594 If so, read the rest of the partial symbols from this comp unit.
5595 If not, there's no more debug_info for this comp unit. */
5597 load_partial_dies (reader, info_ptr, 0);
5600 /* Load the partial DIEs for a secondary CU into memory.
5601 This is also used when rereading a primary CU with load_all_dies. */
5604 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5606 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5607 load_partial_comp_unit_reader, NULL);
5611 read_comp_units_from_section (struct objfile *objfile,
5612 struct dwarf2_section_info *section,
5613 unsigned int is_dwz,
5616 struct dwarf2_per_cu_data ***all_comp_units)
5619 bfd *abfd = section->asection->owner;
5621 dwarf2_read_section (objfile, section);
5623 info_ptr = section->buffer;
5625 while (info_ptr < section->buffer + section->size)
5627 unsigned int length, initial_length_size;
5628 struct dwarf2_per_cu_data *this_cu;
5631 offset.sect_off = info_ptr - section->buffer;
5633 /* Read just enough information to find out where the next
5634 compilation unit is. */
5635 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5637 /* Save the compilation unit for later lookup. */
5638 this_cu = obstack_alloc (&objfile->objfile_obstack,
5639 sizeof (struct dwarf2_per_cu_data));
5640 memset (this_cu, 0, sizeof (*this_cu));
5641 this_cu->offset = offset;
5642 this_cu->length = length + initial_length_size;
5643 this_cu->is_dwz = is_dwz;
5644 this_cu->objfile = objfile;
5645 this_cu->section = section;
5647 if (*n_comp_units == *n_allocated)
5650 *all_comp_units = xrealloc (*all_comp_units,
5652 * sizeof (struct dwarf2_per_cu_data *));
5654 (*all_comp_units)[*n_comp_units] = this_cu;
5657 info_ptr = info_ptr + this_cu->length;
5661 /* Create a list of all compilation units in OBJFILE.
5662 This is only done for -readnow and building partial symtabs. */
5665 create_all_comp_units (struct objfile *objfile)
5669 struct dwarf2_per_cu_data **all_comp_units;
5673 all_comp_units = xmalloc (n_allocated
5674 * sizeof (struct dwarf2_per_cu_data *));
5676 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5677 &n_allocated, &n_comp_units, &all_comp_units);
5679 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5681 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5683 read_comp_units_from_section (objfile, &dwz->info, 1,
5684 &n_allocated, &n_comp_units,
5688 dwarf2_per_objfile->all_comp_units
5689 = obstack_alloc (&objfile->objfile_obstack,
5690 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5691 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5692 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5693 xfree (all_comp_units);
5694 dwarf2_per_objfile->n_comp_units = n_comp_units;
5697 /* Process all loaded DIEs for compilation unit CU, starting at
5698 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5699 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5700 DW_AT_ranges). If NEED_PC is set, then this function will set
5701 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5702 and record the covered ranges in the addrmap. */
5705 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5706 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5708 struct partial_die_info *pdi;
5710 /* Now, march along the PDI's, descending into ones which have
5711 interesting children but skipping the children of the other ones,
5712 until we reach the end of the compilation unit. */
5718 fixup_partial_die (pdi, cu);
5720 /* Anonymous namespaces or modules have no name but have interesting
5721 children, so we need to look at them. Ditto for anonymous
5724 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5725 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5726 || pdi->tag == DW_TAG_imported_unit)
5730 case DW_TAG_subprogram:
5731 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5733 case DW_TAG_constant:
5734 case DW_TAG_variable:
5735 case DW_TAG_typedef:
5736 case DW_TAG_union_type:
5737 if (!pdi->is_declaration)
5739 add_partial_symbol (pdi, cu);
5742 case DW_TAG_class_type:
5743 case DW_TAG_interface_type:
5744 case DW_TAG_structure_type:
5745 if (!pdi->is_declaration)
5747 add_partial_symbol (pdi, cu);
5750 case DW_TAG_enumeration_type:
5751 if (!pdi->is_declaration)
5752 add_partial_enumeration (pdi, cu);
5754 case DW_TAG_base_type:
5755 case DW_TAG_subrange_type:
5756 /* File scope base type definitions are added to the partial
5758 add_partial_symbol (pdi, cu);
5760 case DW_TAG_namespace:
5761 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5764 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5766 case DW_TAG_imported_unit:
5768 struct dwarf2_per_cu_data *per_cu;
5770 /* For now we don't handle imported units in type units. */
5771 if (cu->per_cu->is_debug_types)
5773 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5774 " supported in type units [in module %s]"),
5778 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5782 /* Go read the partial unit, if needed. */
5783 if (per_cu->v.psymtab == NULL)
5784 process_psymtab_comp_unit (per_cu, 1);
5786 VEC_safe_push (dwarf2_per_cu_ptr,
5787 cu->per_cu->imported_symtabs, per_cu);
5795 /* If the die has a sibling, skip to the sibling. */
5797 pdi = pdi->die_sibling;
5801 /* Functions used to compute the fully scoped name of a partial DIE.
5803 Normally, this is simple. For C++, the parent DIE's fully scoped
5804 name is concatenated with "::" and the partial DIE's name. For
5805 Java, the same thing occurs except that "." is used instead of "::".
5806 Enumerators are an exception; they use the scope of their parent
5807 enumeration type, i.e. the name of the enumeration type is not
5808 prepended to the enumerator.
5810 There are two complexities. One is DW_AT_specification; in this
5811 case "parent" means the parent of the target of the specification,
5812 instead of the direct parent of the DIE. The other is compilers
5813 which do not emit DW_TAG_namespace; in this case we try to guess
5814 the fully qualified name of structure types from their members'
5815 linkage names. This must be done using the DIE's children rather
5816 than the children of any DW_AT_specification target. We only need
5817 to do this for structures at the top level, i.e. if the target of
5818 any DW_AT_specification (if any; otherwise the DIE itself) does not
5821 /* Compute the scope prefix associated with PDI's parent, in
5822 compilation unit CU. The result will be allocated on CU's
5823 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5824 field. NULL is returned if no prefix is necessary. */
5826 partial_die_parent_scope (struct partial_die_info *pdi,
5827 struct dwarf2_cu *cu)
5829 const char *grandparent_scope;
5830 struct partial_die_info *parent, *real_pdi;
5832 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5833 then this means the parent of the specification DIE. */
5836 while (real_pdi->has_specification)
5837 real_pdi = find_partial_die (real_pdi->spec_offset,
5838 real_pdi->spec_is_dwz, cu);
5840 parent = real_pdi->die_parent;
5844 if (parent->scope_set)
5845 return parent->scope;
5847 fixup_partial_die (parent, cu);
5849 grandparent_scope = partial_die_parent_scope (parent, cu);
5851 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5852 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5853 Work around this problem here. */
5854 if (cu->language == language_cplus
5855 && parent->tag == DW_TAG_namespace
5856 && strcmp (parent->name, "::") == 0
5857 && grandparent_scope == NULL)
5859 parent->scope = NULL;
5860 parent->scope_set = 1;
5864 if (pdi->tag == DW_TAG_enumerator)
5865 /* Enumerators should not get the name of the enumeration as a prefix. */
5866 parent->scope = grandparent_scope;
5867 else if (parent->tag == DW_TAG_namespace
5868 || parent->tag == DW_TAG_module
5869 || parent->tag == DW_TAG_structure_type
5870 || parent->tag == DW_TAG_class_type
5871 || parent->tag == DW_TAG_interface_type
5872 || parent->tag == DW_TAG_union_type
5873 || parent->tag == DW_TAG_enumeration_type)
5875 if (grandparent_scope == NULL)
5876 parent->scope = parent->name;
5878 parent->scope = typename_concat (&cu->comp_unit_obstack,
5880 parent->name, 0, cu);
5884 /* FIXME drow/2004-04-01: What should we be doing with
5885 function-local names? For partial symbols, we should probably be
5887 complaint (&symfile_complaints,
5888 _("unhandled containing DIE tag %d for DIE at %d"),
5889 parent->tag, pdi->offset.sect_off);
5890 parent->scope = grandparent_scope;
5893 parent->scope_set = 1;
5894 return parent->scope;
5897 /* Return the fully scoped name associated with PDI, from compilation unit
5898 CU. The result will be allocated with malloc. */
5901 partial_die_full_name (struct partial_die_info *pdi,
5902 struct dwarf2_cu *cu)
5904 const char *parent_scope;
5906 /* If this is a template instantiation, we can not work out the
5907 template arguments from partial DIEs. So, unfortunately, we have
5908 to go through the full DIEs. At least any work we do building
5909 types here will be reused if full symbols are loaded later. */
5910 if (pdi->has_template_arguments)
5912 fixup_partial_die (pdi, cu);
5914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5916 struct die_info *die;
5917 struct attribute attr;
5918 struct dwarf2_cu *ref_cu = cu;
5920 /* DW_FORM_ref_addr is using section offset. */
5922 attr.form = DW_FORM_ref_addr;
5923 attr.u.unsnd = pdi->offset.sect_off;
5924 die = follow_die_ref (NULL, &attr, &ref_cu);
5926 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5930 parent_scope = partial_die_parent_scope (pdi, cu);
5931 if (parent_scope == NULL)
5934 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5938 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5940 struct objfile *objfile = cu->objfile;
5942 const char *actual_name = NULL;
5944 char *built_actual_name;
5946 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5948 built_actual_name = partial_die_full_name (pdi, cu);
5949 if (built_actual_name != NULL)
5950 actual_name = built_actual_name;
5952 if (actual_name == NULL)
5953 actual_name = pdi->name;
5957 case DW_TAG_subprogram:
5958 if (pdi->is_external || cu->language == language_ada)
5960 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5961 of the global scope. But in Ada, we want to be able to access
5962 nested procedures globally. So all Ada subprograms are stored
5963 in the global scope. */
5964 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5965 mst_text, objfile); */
5966 add_psymbol_to_list (actual_name, strlen (actual_name),
5967 built_actual_name != NULL,
5968 VAR_DOMAIN, LOC_BLOCK,
5969 &objfile->global_psymbols,
5970 0, pdi->lowpc + baseaddr,
5971 cu->language, objfile);
5975 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5976 mst_file_text, objfile); */
5977 add_psymbol_to_list (actual_name, strlen (actual_name),
5978 built_actual_name != NULL,
5979 VAR_DOMAIN, LOC_BLOCK,
5980 &objfile->static_psymbols,
5981 0, pdi->lowpc + baseaddr,
5982 cu->language, objfile);
5985 case DW_TAG_constant:
5987 struct psymbol_allocation_list *list;
5989 if (pdi->is_external)
5990 list = &objfile->global_psymbols;
5992 list = &objfile->static_psymbols;
5993 add_psymbol_to_list (actual_name, strlen (actual_name),
5994 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5995 list, 0, 0, cu->language, objfile);
5998 case DW_TAG_variable:
6000 addr = decode_locdesc (pdi->d.locdesc, cu);
6004 && !dwarf2_per_objfile->has_section_at_zero)
6006 /* A global or static variable may also have been stripped
6007 out by the linker if unused, in which case its address
6008 will be nullified; do not add such variables into partial
6009 symbol table then. */
6011 else if (pdi->is_external)
6014 Don't enter into the minimal symbol tables as there is
6015 a minimal symbol table entry from the ELF symbols already.
6016 Enter into partial symbol table if it has a location
6017 descriptor or a type.
6018 If the location descriptor is missing, new_symbol will create
6019 a LOC_UNRESOLVED symbol, the address of the variable will then
6020 be determined from the minimal symbol table whenever the variable
6022 The address for the partial symbol table entry is not
6023 used by GDB, but it comes in handy for debugging partial symbol
6026 if (pdi->d.locdesc || pdi->has_type)
6027 add_psymbol_to_list (actual_name, strlen (actual_name),
6028 built_actual_name != NULL,
6029 VAR_DOMAIN, LOC_STATIC,
6030 &objfile->global_psymbols,
6032 cu->language, objfile);
6036 /* Static Variable. Skip symbols without location descriptors. */
6037 if (pdi->d.locdesc == NULL)
6039 xfree (built_actual_name);
6042 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6043 mst_file_data, objfile); */
6044 add_psymbol_to_list (actual_name, strlen (actual_name),
6045 built_actual_name != NULL,
6046 VAR_DOMAIN, LOC_STATIC,
6047 &objfile->static_psymbols,
6049 cu->language, objfile);
6052 case DW_TAG_typedef:
6053 case DW_TAG_base_type:
6054 case DW_TAG_subrange_type:
6055 add_psymbol_to_list (actual_name, strlen (actual_name),
6056 built_actual_name != NULL,
6057 VAR_DOMAIN, LOC_TYPEDEF,
6058 &objfile->static_psymbols,
6059 0, (CORE_ADDR) 0, cu->language, objfile);
6061 case DW_TAG_namespace:
6062 add_psymbol_to_list (actual_name, strlen (actual_name),
6063 built_actual_name != NULL,
6064 VAR_DOMAIN, LOC_TYPEDEF,
6065 &objfile->global_psymbols,
6066 0, (CORE_ADDR) 0, cu->language, objfile);
6068 case DW_TAG_class_type:
6069 case DW_TAG_interface_type:
6070 case DW_TAG_structure_type:
6071 case DW_TAG_union_type:
6072 case DW_TAG_enumeration_type:
6073 /* Skip external references. The DWARF standard says in the section
6074 about "Structure, Union, and Class Type Entries": "An incomplete
6075 structure, union or class type is represented by a structure,
6076 union or class entry that does not have a byte size attribute
6077 and that has a DW_AT_declaration attribute." */
6078 if (!pdi->has_byte_size && pdi->is_declaration)
6080 xfree (built_actual_name);
6084 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6085 static vs. global. */
6086 add_psymbol_to_list (actual_name, strlen (actual_name),
6087 built_actual_name != NULL,
6088 STRUCT_DOMAIN, LOC_TYPEDEF,
6089 (cu->language == language_cplus
6090 || cu->language == language_java)
6091 ? &objfile->global_psymbols
6092 : &objfile->static_psymbols,
6093 0, (CORE_ADDR) 0, cu->language, objfile);
6096 case DW_TAG_enumerator:
6097 add_psymbol_to_list (actual_name, strlen (actual_name),
6098 built_actual_name != NULL,
6099 VAR_DOMAIN, LOC_CONST,
6100 (cu->language == language_cplus
6101 || cu->language == language_java)
6102 ? &objfile->global_psymbols
6103 : &objfile->static_psymbols,
6104 0, (CORE_ADDR) 0, cu->language, objfile);
6110 xfree (built_actual_name);
6113 /* Read a partial die corresponding to a namespace; also, add a symbol
6114 corresponding to that namespace to the symbol table. NAMESPACE is
6115 the name of the enclosing namespace. */
6118 add_partial_namespace (struct partial_die_info *pdi,
6119 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6120 int need_pc, struct dwarf2_cu *cu)
6122 /* Add a symbol for the namespace. */
6124 add_partial_symbol (pdi, cu);
6126 /* Now scan partial symbols in that namespace. */
6128 if (pdi->has_children)
6129 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6132 /* Read a partial die corresponding to a Fortran module. */
6135 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6136 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6138 /* Now scan partial symbols in that module. */
6140 if (pdi->has_children)
6141 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6144 /* Read a partial die corresponding to a subprogram and create a partial
6145 symbol for that subprogram. When the CU language allows it, this
6146 routine also defines a partial symbol for each nested subprogram
6147 that this subprogram contains.
6149 DIE my also be a lexical block, in which case we simply search
6150 recursively for suprograms defined inside that lexical block.
6151 Again, this is only performed when the CU language allows this
6152 type of definitions. */
6155 add_partial_subprogram (struct partial_die_info *pdi,
6156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6157 int need_pc, struct dwarf2_cu *cu)
6159 if (pdi->tag == DW_TAG_subprogram)
6161 if (pdi->has_pc_info)
6163 if (pdi->lowpc < *lowpc)
6164 *lowpc = pdi->lowpc;
6165 if (pdi->highpc > *highpc)
6166 *highpc = pdi->highpc;
6170 struct objfile *objfile = cu->objfile;
6172 baseaddr = ANOFFSET (objfile->section_offsets,
6173 SECT_OFF_TEXT (objfile));
6174 addrmap_set_empty (objfile->psymtabs_addrmap,
6175 pdi->lowpc + baseaddr,
6176 pdi->highpc - 1 + baseaddr,
6177 cu->per_cu->v.psymtab);
6181 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6183 if (!pdi->is_declaration)
6184 /* Ignore subprogram DIEs that do not have a name, they are
6185 illegal. Do not emit a complaint at this point, we will
6186 do so when we convert this psymtab into a symtab. */
6188 add_partial_symbol (pdi, cu);
6192 if (! pdi->has_children)
6195 if (cu->language == language_ada)
6197 pdi = pdi->die_child;
6200 fixup_partial_die (pdi, cu);
6201 if (pdi->tag == DW_TAG_subprogram
6202 || pdi->tag == DW_TAG_lexical_block)
6203 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6204 pdi = pdi->die_sibling;
6209 /* Read a partial die corresponding to an enumeration type. */
6212 add_partial_enumeration (struct partial_die_info *enum_pdi,
6213 struct dwarf2_cu *cu)
6215 struct partial_die_info *pdi;
6217 if (enum_pdi->name != NULL)
6218 add_partial_symbol (enum_pdi, cu);
6220 pdi = enum_pdi->die_child;
6223 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6224 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6226 add_partial_symbol (pdi, cu);
6227 pdi = pdi->die_sibling;
6231 /* Return the initial uleb128 in the die at INFO_PTR. */
6234 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6236 unsigned int bytes_read;
6238 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6241 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6242 Return the corresponding abbrev, or NULL if the number is zero (indicating
6243 an empty DIE). In either case *BYTES_READ will be set to the length of
6244 the initial number. */
6246 static struct abbrev_info *
6247 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6248 struct dwarf2_cu *cu)
6250 bfd *abfd = cu->objfile->obfd;
6251 unsigned int abbrev_number;
6252 struct abbrev_info *abbrev;
6254 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6256 if (abbrev_number == 0)
6259 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6262 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6263 abbrev_number, bfd_get_filename (abfd));
6269 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6270 Returns a pointer to the end of a series of DIEs, terminated by an empty
6271 DIE. Any children of the skipped DIEs will also be skipped. */
6274 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6276 struct dwarf2_cu *cu = reader->cu;
6277 struct abbrev_info *abbrev;
6278 unsigned int bytes_read;
6282 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6284 return info_ptr + bytes_read;
6286 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6290 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6291 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6292 abbrev corresponding to that skipped uleb128 should be passed in
6293 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6297 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6298 struct abbrev_info *abbrev)
6300 unsigned int bytes_read;
6301 struct attribute attr;
6302 bfd *abfd = reader->abfd;
6303 struct dwarf2_cu *cu = reader->cu;
6304 gdb_byte *buffer = reader->buffer;
6305 const gdb_byte *buffer_end = reader->buffer_end;
6306 gdb_byte *start_info_ptr = info_ptr;
6307 unsigned int form, i;
6309 for (i = 0; i < abbrev->num_attrs; i++)
6311 /* The only abbrev we care about is DW_AT_sibling. */
6312 if (abbrev->attrs[i].name == DW_AT_sibling)
6314 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6315 if (attr.form == DW_FORM_ref_addr)
6316 complaint (&symfile_complaints,
6317 _("ignoring absolute DW_AT_sibling"));
6319 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6322 /* If it isn't DW_AT_sibling, skip this attribute. */
6323 form = abbrev->attrs[i].form;
6327 case DW_FORM_ref_addr:
6328 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6329 and later it is offset sized. */
6330 if (cu->header.version == 2)
6331 info_ptr += cu->header.addr_size;
6333 info_ptr += cu->header.offset_size;
6335 case DW_FORM_GNU_ref_alt:
6336 info_ptr += cu->header.offset_size;
6339 info_ptr += cu->header.addr_size;
6346 case DW_FORM_flag_present:
6358 case DW_FORM_ref_sig8:
6361 case DW_FORM_string:
6362 read_direct_string (abfd, info_ptr, &bytes_read);
6363 info_ptr += bytes_read;
6365 case DW_FORM_sec_offset:
6367 case DW_FORM_GNU_strp_alt:
6368 info_ptr += cu->header.offset_size;
6370 case DW_FORM_exprloc:
6372 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6373 info_ptr += bytes_read;
6375 case DW_FORM_block1:
6376 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6378 case DW_FORM_block2:
6379 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6381 case DW_FORM_block4:
6382 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6386 case DW_FORM_ref_udata:
6387 case DW_FORM_GNU_addr_index:
6388 case DW_FORM_GNU_str_index:
6389 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6391 case DW_FORM_indirect:
6392 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6393 info_ptr += bytes_read;
6394 /* We need to continue parsing from here, so just go back to
6396 goto skip_attribute;
6399 error (_("Dwarf Error: Cannot handle %s "
6400 "in DWARF reader [in module %s]"),
6401 dwarf_form_name (form),
6402 bfd_get_filename (abfd));
6406 if (abbrev->has_children)
6407 return skip_children (reader, info_ptr);
6412 /* Locate ORIG_PDI's sibling.
6413 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6416 locate_pdi_sibling (const struct die_reader_specs *reader,
6417 struct partial_die_info *orig_pdi,
6420 /* Do we know the sibling already? */
6422 if (orig_pdi->sibling)
6423 return orig_pdi->sibling;
6425 /* Are there any children to deal with? */
6427 if (!orig_pdi->has_children)
6430 /* Skip the children the long way. */
6432 return skip_children (reader, info_ptr);
6435 /* Expand this partial symbol table into a full symbol table. SELF is
6439 dwarf2_read_symtab (struct partial_symtab *self,
6440 struct objfile *objfile)
6444 warning (_("bug: psymtab for %s is already read in."),
6451 printf_filtered (_("Reading in symbols for %s..."),
6453 gdb_flush (gdb_stdout);
6456 /* Restore our global data. */
6457 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6459 /* If this psymtab is constructed from a debug-only objfile, the
6460 has_section_at_zero flag will not necessarily be correct. We
6461 can get the correct value for this flag by looking at the data
6462 associated with the (presumably stripped) associated objfile. */
6463 if (objfile->separate_debug_objfile_backlink)
6465 struct dwarf2_per_objfile *dpo_backlink
6466 = objfile_data (objfile->separate_debug_objfile_backlink,
6467 dwarf2_objfile_data_key);
6469 dwarf2_per_objfile->has_section_at_zero
6470 = dpo_backlink->has_section_at_zero;
6473 dwarf2_per_objfile->reading_partial_symbols = 0;
6475 psymtab_to_symtab_1 (self);
6477 /* Finish up the debug error message. */
6479 printf_filtered (_("done.\n"));
6482 process_cu_includes ();
6485 /* Reading in full CUs. */
6487 /* Add PER_CU to the queue. */
6490 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6491 enum language pretend_language)
6493 struct dwarf2_queue_item *item;
6496 item = xmalloc (sizeof (*item));
6497 item->per_cu = per_cu;
6498 item->pretend_language = pretend_language;
6501 if (dwarf2_queue == NULL)
6502 dwarf2_queue = item;
6504 dwarf2_queue_tail->next = item;
6506 dwarf2_queue_tail = item;
6509 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6510 unit and add it to our queue.
6511 The result is non-zero if PER_CU was queued, otherwise the result is zero
6512 meaning either PER_CU is already queued or it is already loaded. */
6515 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6516 struct dwarf2_per_cu_data *per_cu,
6517 enum language pretend_language)
6519 /* We may arrive here during partial symbol reading, if we need full
6520 DIEs to process an unusual case (e.g. template arguments). Do
6521 not queue PER_CU, just tell our caller to load its DIEs. */
6522 if (dwarf2_per_objfile->reading_partial_symbols)
6524 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6529 /* Mark the dependence relation so that we don't flush PER_CU
6531 dwarf2_add_dependence (this_cu, per_cu);
6533 /* If it's already on the queue, we have nothing to do. */
6537 /* If the compilation unit is already loaded, just mark it as
6539 if (per_cu->cu != NULL)
6541 per_cu->cu->last_used = 0;
6545 /* Add it to the queue. */
6546 queue_comp_unit (per_cu, pretend_language);
6551 /* Process the queue. */
6554 process_queue (void)
6556 struct dwarf2_queue_item *item, *next_item;
6558 if (dwarf2_read_debug)
6560 fprintf_unfiltered (gdb_stdlog,
6561 "Expanding one or more symtabs of objfile %s ...\n",
6562 dwarf2_per_objfile->objfile->name);
6565 /* The queue starts out with one item, but following a DIE reference
6566 may load a new CU, adding it to the end of the queue. */
6567 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6569 if (dwarf2_per_objfile->using_index
6570 ? !item->per_cu->v.quick->symtab
6571 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6573 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6575 if (dwarf2_read_debug)
6577 fprintf_unfiltered (gdb_stdlog,
6578 "Expanding symtab of %s at offset 0x%x\n",
6579 per_cu->is_debug_types ? "TU" : "CU",
6580 per_cu->offset.sect_off);
6583 if (per_cu->is_debug_types)
6584 process_full_type_unit (per_cu, item->pretend_language);
6586 process_full_comp_unit (per_cu, item->pretend_language);
6588 if (dwarf2_read_debug)
6590 fprintf_unfiltered (gdb_stdlog,
6591 "Done expanding %s at offset 0x%x\n",
6592 per_cu->is_debug_types ? "TU" : "CU",
6593 per_cu->offset.sect_off);
6597 item->per_cu->queued = 0;
6598 next_item = item->next;
6602 dwarf2_queue_tail = NULL;
6604 if (dwarf2_read_debug)
6606 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6607 dwarf2_per_objfile->objfile->name);
6611 /* Free all allocated queue entries. This function only releases anything if
6612 an error was thrown; if the queue was processed then it would have been
6613 freed as we went along. */
6616 dwarf2_release_queue (void *dummy)
6618 struct dwarf2_queue_item *item, *last;
6620 item = dwarf2_queue;
6623 /* Anything still marked queued is likely to be in an
6624 inconsistent state, so discard it. */
6625 if (item->per_cu->queued)
6627 if (item->per_cu->cu != NULL)
6628 free_one_cached_comp_unit (item->per_cu);
6629 item->per_cu->queued = 0;
6637 dwarf2_queue = dwarf2_queue_tail = NULL;
6640 /* Read in full symbols for PST, and anything it depends on. */
6643 psymtab_to_symtab_1 (struct partial_symtab *pst)
6645 struct dwarf2_per_cu_data *per_cu;
6651 for (i = 0; i < pst->number_of_dependencies; i++)
6652 if (!pst->dependencies[i]->readin
6653 && pst->dependencies[i]->user == NULL)
6655 /* Inform about additional files that need to be read in. */
6658 /* FIXME: i18n: Need to make this a single string. */
6659 fputs_filtered (" ", gdb_stdout);
6661 fputs_filtered ("and ", gdb_stdout);
6663 printf_filtered ("%s...", pst->dependencies[i]->filename);
6664 wrap_here (""); /* Flush output. */
6665 gdb_flush (gdb_stdout);
6667 psymtab_to_symtab_1 (pst->dependencies[i]);
6670 per_cu = pst->read_symtab_private;
6674 /* It's an include file, no symbols to read for it.
6675 Everything is in the parent symtab. */
6680 dw2_do_instantiate_symtab (per_cu);
6683 /* Trivial hash function for die_info: the hash value of a DIE
6684 is its offset in .debug_info for this objfile. */
6687 die_hash (const void *item)
6689 const struct die_info *die = item;
6691 return die->offset.sect_off;
6694 /* Trivial comparison function for die_info structures: two DIEs
6695 are equal if they have the same offset. */
6698 die_eq (const void *item_lhs, const void *item_rhs)
6700 const struct die_info *die_lhs = item_lhs;
6701 const struct die_info *die_rhs = item_rhs;
6703 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6706 /* die_reader_func for load_full_comp_unit.
6707 This is identical to read_signatured_type_reader,
6708 but is kept separate for now. */
6711 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6713 struct die_info *comp_unit_die,
6717 struct dwarf2_cu *cu = reader->cu;
6718 enum language *language_ptr = data;
6720 gdb_assert (cu->die_hash == NULL);
6722 htab_create_alloc_ex (cu->header.length / 12,
6726 &cu->comp_unit_obstack,
6727 hashtab_obstack_allocate,
6728 dummy_obstack_deallocate);
6731 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6732 &info_ptr, comp_unit_die);
6733 cu->dies = comp_unit_die;
6734 /* comp_unit_die is not stored in die_hash, no need. */
6736 /* We try not to read any attributes in this function, because not
6737 all CUs needed for references have been loaded yet, and symbol
6738 table processing isn't initialized. But we have to set the CU language,
6739 or we won't be able to build types correctly.
6740 Similarly, if we do not read the producer, we can not apply
6741 producer-specific interpretation. */
6742 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6745 /* Load the DIEs associated with PER_CU into memory. */
6748 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6749 enum language pretend_language)
6751 gdb_assert (! this_cu->is_debug_types);
6753 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6754 load_full_comp_unit_reader, &pretend_language);
6757 /* Add a DIE to the delayed physname list. */
6760 add_to_method_list (struct type *type, int fnfield_index, int index,
6761 const char *name, struct die_info *die,
6762 struct dwarf2_cu *cu)
6764 struct delayed_method_info mi;
6766 mi.fnfield_index = fnfield_index;
6770 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6773 /* A cleanup for freeing the delayed method list. */
6776 free_delayed_list (void *ptr)
6778 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6779 if (cu->method_list != NULL)
6781 VEC_free (delayed_method_info, cu->method_list);
6782 cu->method_list = NULL;
6786 /* Compute the physnames of any methods on the CU's method list.
6788 The computation of method physnames is delayed in order to avoid the
6789 (bad) condition that one of the method's formal parameters is of an as yet
6793 compute_delayed_physnames (struct dwarf2_cu *cu)
6796 struct delayed_method_info *mi;
6797 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6799 const char *physname;
6800 struct fn_fieldlist *fn_flp
6801 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6802 physname = dwarf2_physname (mi->name, mi->die, cu);
6803 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6807 /* Go objects should be embedded in a DW_TAG_module DIE,
6808 and it's not clear if/how imported objects will appear.
6809 To keep Go support simple until that's worked out,
6810 go back through what we've read and create something usable.
6811 We could do this while processing each DIE, and feels kinda cleaner,
6812 but that way is more invasive.
6813 This is to, for example, allow the user to type "p var" or "b main"
6814 without having to specify the package name, and allow lookups
6815 of module.object to work in contexts that use the expression
6819 fixup_go_packaging (struct dwarf2_cu *cu)
6821 char *package_name = NULL;
6822 struct pending *list;
6825 for (list = global_symbols; list != NULL; list = list->next)
6827 for (i = 0; i < list->nsyms; ++i)
6829 struct symbol *sym = list->symbol[i];
6831 if (SYMBOL_LANGUAGE (sym) == language_go
6832 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6834 char *this_package_name = go_symbol_package_name (sym);
6836 if (this_package_name == NULL)
6838 if (package_name == NULL)
6839 package_name = this_package_name;
6842 if (strcmp (package_name, this_package_name) != 0)
6843 complaint (&symfile_complaints,
6844 _("Symtab %s has objects from two different Go packages: %s and %s"),
6845 (SYMBOL_SYMTAB (sym)
6846 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6847 : cu->objfile->name),
6848 this_package_name, package_name);
6849 xfree (this_package_name);
6855 if (package_name != NULL)
6857 struct objfile *objfile = cu->objfile;
6858 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6860 strlen (package_name));
6861 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6862 saved_package_name, objfile);
6865 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6867 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6868 SYMBOL_SET_LANGUAGE (sym, language_go);
6869 SYMBOL_SET_NAMES (sym, saved_package_name,
6870 strlen (saved_package_name), 0, objfile);
6871 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6872 e.g., "main" finds the "main" module and not C's main(). */
6873 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6874 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6875 SYMBOL_TYPE (sym) = type;
6877 add_symbol_to_list (sym, &global_symbols);
6879 xfree (package_name);
6883 /* Return the symtab for PER_CU. This works properly regardless of
6884 whether we're using the index or psymtabs. */
6886 static struct symtab *
6887 get_symtab (struct dwarf2_per_cu_data *per_cu)
6889 return (dwarf2_per_objfile->using_index
6890 ? per_cu->v.quick->symtab
6891 : per_cu->v.psymtab->symtab);
6894 /* A helper function for computing the list of all symbol tables
6895 included by PER_CU. */
6898 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6899 htab_t all_children,
6900 struct dwarf2_per_cu_data *per_cu)
6904 struct dwarf2_per_cu_data *iter;
6906 slot = htab_find_slot (all_children, per_cu, INSERT);
6909 /* This inclusion and its children have been processed. */
6914 /* Only add a CU if it has a symbol table. */
6915 if (get_symtab (per_cu) != NULL)
6916 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6919 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6921 recursively_compute_inclusions (result, all_children, iter);
6924 /* Compute the symtab 'includes' fields for the symtab related to
6928 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6930 gdb_assert (! per_cu->is_debug_types);
6932 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6935 struct dwarf2_per_cu_data *iter;
6936 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6937 htab_t all_children;
6938 struct symtab *symtab = get_symtab (per_cu);
6940 /* If we don't have a symtab, we can just skip this case. */
6944 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6945 NULL, xcalloc, xfree);
6948 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6951 recursively_compute_inclusions (&result_children, all_children, iter);
6953 /* Now we have a transitive closure of all the included CUs, and
6954 for .gdb_index version 7 the included TUs, so we can convert it
6955 to a list of symtabs. */
6956 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6958 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6959 (len + 1) * sizeof (struct symtab *));
6961 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6963 symtab->includes[ix] = get_symtab (iter);
6964 symtab->includes[len] = NULL;
6966 VEC_free (dwarf2_per_cu_ptr, result_children);
6967 htab_delete (all_children);
6971 /* Compute the 'includes' field for the symtabs of all the CUs we just
6975 process_cu_includes (void)
6978 struct dwarf2_per_cu_data *iter;
6981 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6985 if (! iter->is_debug_types)
6986 compute_symtab_includes (iter);
6989 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6992 /* Generate full symbol information for PER_CU, whose DIEs have
6993 already been loaded into memory. */
6996 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6997 enum language pretend_language)
6999 struct dwarf2_cu *cu = per_cu->cu;
7000 struct objfile *objfile = per_cu->objfile;
7001 CORE_ADDR lowpc, highpc;
7002 struct symtab *symtab;
7003 struct cleanup *back_to, *delayed_list_cleanup;
7005 struct block *static_block;
7007 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7010 back_to = make_cleanup (really_free_pendings, NULL);
7011 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7013 cu->list_in_scope = &file_symbols;
7015 cu->language = pretend_language;
7016 cu->language_defn = language_def (cu->language);
7018 /* Do line number decoding in read_file_scope () */
7019 process_die (cu->dies, cu);
7021 /* For now fudge the Go package. */
7022 if (cu->language == language_go)
7023 fixup_go_packaging (cu);
7025 /* Now that we have processed all the DIEs in the CU, all the types
7026 should be complete, and it should now be safe to compute all of the
7028 compute_delayed_physnames (cu);
7029 do_cleanups (delayed_list_cleanup);
7031 /* Some compilers don't define a DW_AT_high_pc attribute for the
7032 compilation unit. If the DW_AT_high_pc is missing, synthesize
7033 it, by scanning the DIE's below the compilation unit. */
7034 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7037 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7038 per_cu->imported_symtabs != NULL);
7040 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7041 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7042 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7043 addrmap to help ensure it has an accurate map of pc values belonging to
7045 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7047 symtab = end_symtab_from_static_block (static_block, objfile,
7048 SECT_OFF_TEXT (objfile), 0);
7052 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7054 /* Set symtab language to language from DW_AT_language. If the
7055 compilation is from a C file generated by language preprocessors, do
7056 not set the language if it was already deduced by start_subfile. */
7057 if (!(cu->language == language_c && symtab->language != language_c))
7058 symtab->language = cu->language;
7060 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7061 produce DW_AT_location with location lists but it can be possibly
7062 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7063 there were bugs in prologue debug info, fixed later in GCC-4.5
7064 by "unwind info for epilogues" patch (which is not directly related).
7066 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7067 needed, it would be wrong due to missing DW_AT_producer there.
7069 Still one can confuse GDB by using non-standard GCC compilation
7070 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7072 if (cu->has_loclist && gcc_4_minor >= 5)
7073 symtab->locations_valid = 1;
7075 if (gcc_4_minor >= 5)
7076 symtab->epilogue_unwind_valid = 1;
7078 symtab->call_site_htab = cu->call_site_htab;
7081 if (dwarf2_per_objfile->using_index)
7082 per_cu->v.quick->symtab = symtab;
7085 struct partial_symtab *pst = per_cu->v.psymtab;
7086 pst->symtab = symtab;
7090 /* Push it for inclusion processing later. */
7091 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7093 do_cleanups (back_to);
7096 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7097 already been loaded into memory. */
7100 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7101 enum language pretend_language)
7103 struct dwarf2_cu *cu = per_cu->cu;
7104 struct objfile *objfile = per_cu->objfile;
7105 struct symtab *symtab;
7106 struct cleanup *back_to, *delayed_list_cleanup;
7109 back_to = make_cleanup (really_free_pendings, NULL);
7110 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7112 cu->list_in_scope = &file_symbols;
7114 cu->language = pretend_language;
7115 cu->language_defn = language_def (cu->language);
7117 /* The symbol tables are set up in read_type_unit_scope. */
7118 process_die (cu->dies, cu);
7120 /* For now fudge the Go package. */
7121 if (cu->language == language_go)
7122 fixup_go_packaging (cu);
7124 /* Now that we have processed all the DIEs in the CU, all the types
7125 should be complete, and it should now be safe to compute all of the
7127 compute_delayed_physnames (cu);
7128 do_cleanups (delayed_list_cleanup);
7130 /* TUs share symbol tables.
7131 If this is the first TU to use this symtab, complete the construction
7132 of it with end_expandable_symtab. Otherwise, complete the addition of
7133 this TU's symbols to the existing symtab. */
7134 if (per_cu->type_unit_group->primary_symtab == NULL)
7136 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7137 per_cu->type_unit_group->primary_symtab = symtab;
7141 /* Set symtab language to language from DW_AT_language. If the
7142 compilation is from a C file generated by language preprocessors,
7143 do not set the language if it was already deduced by
7145 if (!(cu->language == language_c && symtab->language != language_c))
7146 symtab->language = cu->language;
7151 augment_type_symtab (objfile,
7152 per_cu->type_unit_group->primary_symtab);
7153 symtab = per_cu->type_unit_group->primary_symtab;
7156 if (dwarf2_per_objfile->using_index)
7157 per_cu->v.quick->symtab = symtab;
7160 struct partial_symtab *pst = per_cu->v.psymtab;
7161 pst->symtab = symtab;
7165 do_cleanups (back_to);
7168 /* Process an imported unit DIE. */
7171 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7173 struct attribute *attr;
7175 /* For now we don't handle imported units in type units. */
7176 if (cu->per_cu->is_debug_types)
7178 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7179 " supported in type units [in module %s]"),
7183 attr = dwarf2_attr (die, DW_AT_import, cu);
7186 struct dwarf2_per_cu_data *per_cu;
7187 struct symtab *imported_symtab;
7191 offset = dwarf2_get_ref_die_offset (attr);
7192 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7193 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7195 /* Queue the unit, if needed. */
7196 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7197 load_full_comp_unit (per_cu, cu->language);
7199 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7204 /* Process a die and its children. */
7207 process_die (struct die_info *die, struct dwarf2_cu *cu)
7211 case DW_TAG_padding:
7213 case DW_TAG_compile_unit:
7214 case DW_TAG_partial_unit:
7215 read_file_scope (die, cu);
7217 case DW_TAG_type_unit:
7218 read_type_unit_scope (die, cu);
7220 case DW_TAG_subprogram:
7221 case DW_TAG_inlined_subroutine:
7222 read_func_scope (die, cu);
7224 case DW_TAG_lexical_block:
7225 case DW_TAG_try_block:
7226 case DW_TAG_catch_block:
7227 read_lexical_block_scope (die, cu);
7229 case DW_TAG_GNU_call_site:
7230 read_call_site_scope (die, cu);
7232 case DW_TAG_class_type:
7233 case DW_TAG_interface_type:
7234 case DW_TAG_structure_type:
7235 case DW_TAG_union_type:
7236 process_structure_scope (die, cu);
7238 case DW_TAG_enumeration_type:
7239 process_enumeration_scope (die, cu);
7242 /* These dies have a type, but processing them does not create
7243 a symbol or recurse to process the children. Therefore we can
7244 read them on-demand through read_type_die. */
7245 case DW_TAG_subroutine_type:
7246 case DW_TAG_set_type:
7247 case DW_TAG_array_type:
7248 case DW_TAG_pointer_type:
7249 case DW_TAG_ptr_to_member_type:
7250 case DW_TAG_reference_type:
7251 case DW_TAG_string_type:
7254 case DW_TAG_base_type:
7255 case DW_TAG_subrange_type:
7256 case DW_TAG_typedef:
7257 /* Add a typedef symbol for the type definition, if it has a
7259 new_symbol (die, read_type_die (die, cu), cu);
7261 case DW_TAG_common_block:
7262 read_common_block (die, cu);
7264 case DW_TAG_common_inclusion:
7266 case DW_TAG_namespace:
7267 cu->processing_has_namespace_info = 1;
7268 read_namespace (die, cu);
7271 cu->processing_has_namespace_info = 1;
7272 read_module (die, cu);
7274 case DW_TAG_imported_declaration:
7275 case DW_TAG_imported_module:
7276 cu->processing_has_namespace_info = 1;
7277 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7278 || cu->language != language_fortran))
7279 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7280 dwarf_tag_name (die->tag));
7281 read_import_statement (die, cu);
7284 case DW_TAG_imported_unit:
7285 process_imported_unit_die (die, cu);
7289 new_symbol (die, NULL, cu);
7294 /* DWARF name computation. */
7296 /* A helper function for dwarf2_compute_name which determines whether DIE
7297 needs to have the name of the scope prepended to the name listed in the
7301 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7303 struct attribute *attr;
7307 case DW_TAG_namespace:
7308 case DW_TAG_typedef:
7309 case DW_TAG_class_type:
7310 case DW_TAG_interface_type:
7311 case DW_TAG_structure_type:
7312 case DW_TAG_union_type:
7313 case DW_TAG_enumeration_type:
7314 case DW_TAG_enumerator:
7315 case DW_TAG_subprogram:
7319 case DW_TAG_variable:
7320 case DW_TAG_constant:
7321 /* We only need to prefix "globally" visible variables. These include
7322 any variable marked with DW_AT_external or any variable that
7323 lives in a namespace. [Variables in anonymous namespaces
7324 require prefixing, but they are not DW_AT_external.] */
7326 if (dwarf2_attr (die, DW_AT_specification, cu))
7328 struct dwarf2_cu *spec_cu = cu;
7330 return die_needs_namespace (die_specification (die, &spec_cu),
7334 attr = dwarf2_attr (die, DW_AT_external, cu);
7335 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7336 && die->parent->tag != DW_TAG_module)
7338 /* A variable in a lexical block of some kind does not need a
7339 namespace, even though in C++ such variables may be external
7340 and have a mangled name. */
7341 if (die->parent->tag == DW_TAG_lexical_block
7342 || die->parent->tag == DW_TAG_try_block
7343 || die->parent->tag == DW_TAG_catch_block
7344 || die->parent->tag == DW_TAG_subprogram)
7353 /* Retrieve the last character from a mem_file. */
7356 do_ui_file_peek_last (void *object, const char *buffer, long length)
7358 char *last_char_p = (char *) object;
7361 *last_char_p = buffer[length - 1];
7364 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7365 compute the physname for the object, which include a method's:
7366 - formal parameters (C++/Java),
7367 - receiver type (Go),
7368 - return type (Java).
7370 The term "physname" is a bit confusing.
7371 For C++, for example, it is the demangled name.
7372 For Go, for example, it's the mangled name.
7374 For Ada, return the DIE's linkage name rather than the fully qualified
7375 name. PHYSNAME is ignored..
7377 The result is allocated on the objfile_obstack and canonicalized. */
7380 dwarf2_compute_name (const char *name,
7381 struct die_info *die, struct dwarf2_cu *cu,
7384 struct objfile *objfile = cu->objfile;
7387 name = dwarf2_name (die, cu);
7389 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7390 compute it by typename_concat inside GDB. */
7391 if (cu->language == language_ada
7392 || (cu->language == language_fortran && physname))
7394 /* For Ada unit, we prefer the linkage name over the name, as
7395 the former contains the exported name, which the user expects
7396 to be able to reference. Ideally, we want the user to be able
7397 to reference this entity using either natural or linkage name,
7398 but we haven't started looking at this enhancement yet. */
7399 struct attribute *attr;
7401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7404 if (attr && DW_STRING (attr))
7405 return DW_STRING (attr);
7408 /* These are the only languages we know how to qualify names in. */
7410 && (cu->language == language_cplus || cu->language == language_java
7411 || cu->language == language_fortran))
7413 if (die_needs_namespace (die, cu))
7417 struct ui_file *buf;
7419 prefix = determine_prefix (die, cu);
7420 buf = mem_fileopen ();
7421 if (*prefix != '\0')
7423 char *prefixed_name = typename_concat (NULL, prefix, name,
7426 fputs_unfiltered (prefixed_name, buf);
7427 xfree (prefixed_name);
7430 fputs_unfiltered (name, buf);
7432 /* Template parameters may be specified in the DIE's DW_AT_name, or
7433 as children with DW_TAG_template_type_param or
7434 DW_TAG_value_type_param. If the latter, add them to the name
7435 here. If the name already has template parameters, then
7436 skip this step; some versions of GCC emit both, and
7437 it is more efficient to use the pre-computed name.
7439 Something to keep in mind about this process: it is very
7440 unlikely, or in some cases downright impossible, to produce
7441 something that will match the mangled name of a function.
7442 If the definition of the function has the same debug info,
7443 we should be able to match up with it anyway. But fallbacks
7444 using the minimal symbol, for instance to find a method
7445 implemented in a stripped copy of libstdc++, will not work.
7446 If we do not have debug info for the definition, we will have to
7447 match them up some other way.
7449 When we do name matching there is a related problem with function
7450 templates; two instantiated function templates are allowed to
7451 differ only by their return types, which we do not add here. */
7453 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7455 struct attribute *attr;
7456 struct die_info *child;
7459 die->building_fullname = 1;
7461 for (child = die->child; child != NULL; child = child->sibling)
7466 struct dwarf2_locexpr_baton *baton;
7469 if (child->tag != DW_TAG_template_type_param
7470 && child->tag != DW_TAG_template_value_param)
7475 fputs_unfiltered ("<", buf);
7479 fputs_unfiltered (", ", buf);
7481 attr = dwarf2_attr (child, DW_AT_type, cu);
7484 complaint (&symfile_complaints,
7485 _("template parameter missing DW_AT_type"));
7486 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7489 type = die_type (child, cu);
7491 if (child->tag == DW_TAG_template_type_param)
7493 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7497 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7500 complaint (&symfile_complaints,
7501 _("template parameter missing "
7502 "DW_AT_const_value"));
7503 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7507 dwarf2_const_value_attr (attr, type, name,
7508 &cu->comp_unit_obstack, cu,
7509 &value, &bytes, &baton);
7511 if (TYPE_NOSIGN (type))
7512 /* GDB prints characters as NUMBER 'CHAR'. If that's
7513 changed, this can use value_print instead. */
7514 c_printchar (value, type, buf);
7517 struct value_print_options opts;
7520 v = dwarf2_evaluate_loc_desc (type, NULL,
7524 else if (bytes != NULL)
7526 v = allocate_value (type);
7527 memcpy (value_contents_writeable (v), bytes,
7528 TYPE_LENGTH (type));
7531 v = value_from_longest (type, value);
7533 /* Specify decimal so that we do not depend on
7535 get_formatted_print_options (&opts, 'd');
7537 value_print (v, buf, &opts);
7543 die->building_fullname = 0;
7547 /* Close the argument list, with a space if necessary
7548 (nested templates). */
7549 char last_char = '\0';
7550 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7551 if (last_char == '>')
7552 fputs_unfiltered (" >", buf);
7554 fputs_unfiltered (">", buf);
7558 /* For Java and C++ methods, append formal parameter type
7559 information, if PHYSNAME. */
7561 if (physname && die->tag == DW_TAG_subprogram
7562 && (cu->language == language_cplus
7563 || cu->language == language_java))
7565 struct type *type = read_type_die (die, cu);
7567 c_type_print_args (type, buf, 1, cu->language,
7568 &type_print_raw_options);
7570 if (cu->language == language_java)
7572 /* For java, we must append the return type to method
7574 if (die->tag == DW_TAG_subprogram)
7575 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7576 0, 0, &type_print_raw_options);
7578 else if (cu->language == language_cplus)
7580 /* Assume that an artificial first parameter is
7581 "this", but do not crash if it is not. RealView
7582 marks unnamed (and thus unused) parameters as
7583 artificial; there is no way to differentiate
7585 if (TYPE_NFIELDS (type) > 0
7586 && TYPE_FIELD_ARTIFICIAL (type, 0)
7587 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7588 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7590 fputs_unfiltered (" const", buf);
7594 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7596 ui_file_delete (buf);
7598 if (cu->language == language_cplus)
7601 = dwarf2_canonicalize_name (name, cu,
7602 &objfile->objfile_obstack);
7613 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7614 If scope qualifiers are appropriate they will be added. The result
7615 will be allocated on the objfile_obstack, or NULL if the DIE does
7616 not have a name. NAME may either be from a previous call to
7617 dwarf2_name or NULL.
7619 The output string will be canonicalized (if C++/Java). */
7622 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7624 return dwarf2_compute_name (name, die, cu, 0);
7627 /* Construct a physname for the given DIE in CU. NAME may either be
7628 from a previous call to dwarf2_name or NULL. The result will be
7629 allocated on the objfile_objstack or NULL if the DIE does not have a
7632 The output string will be canonicalized (if C++/Java). */
7635 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7637 struct objfile *objfile = cu->objfile;
7638 struct attribute *attr;
7639 const char *retval, *mangled = NULL, *canon = NULL;
7640 struct cleanup *back_to;
7643 /* In this case dwarf2_compute_name is just a shortcut not building anything
7645 if (!die_needs_namespace (die, cu))
7646 return dwarf2_compute_name (name, die, cu, 1);
7648 back_to = make_cleanup (null_cleanup, NULL);
7650 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7652 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7654 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7656 if (attr && DW_STRING (attr))
7660 mangled = DW_STRING (attr);
7662 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7663 type. It is easier for GDB users to search for such functions as
7664 `name(params)' than `long name(params)'. In such case the minimal
7665 symbol names do not match the full symbol names but for template
7666 functions there is never a need to look up their definition from their
7667 declaration so the only disadvantage remains the minimal symbol
7668 variant `long name(params)' does not have the proper inferior type.
7671 if (cu->language == language_go)
7673 /* This is a lie, but we already lie to the caller new_symbol_full.
7674 new_symbol_full assumes we return the mangled name.
7675 This just undoes that lie until things are cleaned up. */
7680 demangled = cplus_demangle (mangled,
7681 (DMGL_PARAMS | DMGL_ANSI
7682 | (cu->language == language_java
7683 ? DMGL_JAVA | DMGL_RET_POSTFIX
7688 make_cleanup (xfree, demangled);
7698 if (canon == NULL || check_physname)
7700 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7702 if (canon != NULL && strcmp (physname, canon) != 0)
7704 /* It may not mean a bug in GDB. The compiler could also
7705 compute DW_AT_linkage_name incorrectly. But in such case
7706 GDB would need to be bug-to-bug compatible. */
7708 complaint (&symfile_complaints,
7709 _("Computed physname <%s> does not match demangled <%s> "
7710 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7711 physname, canon, mangled, die->offset.sect_off, objfile->name);
7713 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7714 is available here - over computed PHYSNAME. It is safer
7715 against both buggy GDB and buggy compilers. */
7729 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7731 do_cleanups (back_to);
7735 /* Read the import statement specified by the given die and record it. */
7738 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7740 struct objfile *objfile = cu->objfile;
7741 struct attribute *import_attr;
7742 struct die_info *imported_die, *child_die;
7743 struct dwarf2_cu *imported_cu;
7744 const char *imported_name;
7745 const char *imported_name_prefix;
7746 const char *canonical_name;
7747 const char *import_alias;
7748 const char *imported_declaration = NULL;
7749 const char *import_prefix;
7750 VEC (const_char_ptr) *excludes = NULL;
7751 struct cleanup *cleanups;
7753 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7754 if (import_attr == NULL)
7756 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7757 dwarf_tag_name (die->tag));
7762 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7763 imported_name = dwarf2_name (imported_die, imported_cu);
7764 if (imported_name == NULL)
7766 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7768 The import in the following code:
7782 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7783 <52> DW_AT_decl_file : 1
7784 <53> DW_AT_decl_line : 6
7785 <54> DW_AT_import : <0x75>
7786 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7788 <5b> DW_AT_decl_file : 1
7789 <5c> DW_AT_decl_line : 2
7790 <5d> DW_AT_type : <0x6e>
7792 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7793 <76> DW_AT_byte_size : 4
7794 <77> DW_AT_encoding : 5 (signed)
7796 imports the wrong die ( 0x75 instead of 0x58 ).
7797 This case will be ignored until the gcc bug is fixed. */
7801 /* Figure out the local name after import. */
7802 import_alias = dwarf2_name (die, cu);
7804 /* Figure out where the statement is being imported to. */
7805 import_prefix = determine_prefix (die, cu);
7807 /* Figure out what the scope of the imported die is and prepend it
7808 to the name of the imported die. */
7809 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7811 if (imported_die->tag != DW_TAG_namespace
7812 && imported_die->tag != DW_TAG_module)
7814 imported_declaration = imported_name;
7815 canonical_name = imported_name_prefix;
7817 else if (strlen (imported_name_prefix) > 0)
7818 canonical_name = obconcat (&objfile->objfile_obstack,
7819 imported_name_prefix, "::", imported_name,
7822 canonical_name = imported_name;
7824 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7826 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7827 for (child_die = die->child; child_die && child_die->tag;
7828 child_die = sibling_die (child_die))
7830 /* DWARF-4: A Fortran use statement with a “rename list” may be
7831 represented by an imported module entry with an import attribute
7832 referring to the module and owned entries corresponding to those
7833 entities that are renamed as part of being imported. */
7835 if (child_die->tag != DW_TAG_imported_declaration)
7837 complaint (&symfile_complaints,
7838 _("child DW_TAG_imported_declaration expected "
7839 "- DIE at 0x%x [in module %s]"),
7840 child_die->offset.sect_off, objfile->name);
7844 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7845 if (import_attr == NULL)
7847 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7848 dwarf_tag_name (child_die->tag));
7853 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7855 imported_name = dwarf2_name (imported_die, imported_cu);
7856 if (imported_name == NULL)
7858 complaint (&symfile_complaints,
7859 _("child DW_TAG_imported_declaration has unknown "
7860 "imported name - DIE at 0x%x [in module %s]"),
7861 child_die->offset.sect_off, objfile->name);
7865 VEC_safe_push (const_char_ptr, excludes, imported_name);
7867 process_die (child_die, cu);
7870 cp_add_using_directive (import_prefix,
7873 imported_declaration,
7876 &objfile->objfile_obstack);
7878 do_cleanups (cleanups);
7881 /* Cleanup function for handle_DW_AT_stmt_list. */
7884 free_cu_line_header (void *arg)
7886 struct dwarf2_cu *cu = arg;
7888 free_line_header (cu->line_header);
7889 cu->line_header = NULL;
7892 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7893 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7894 this, it was first present in GCC release 4.3.0. */
7897 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7899 if (!cu->checked_producer)
7900 check_producer (cu);
7902 return cu->producer_is_gcc_lt_4_3;
7906 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7907 const char **name, const char **comp_dir)
7909 struct attribute *attr;
7914 /* Find the filename. Do not use dwarf2_name here, since the filename
7915 is not a source language identifier. */
7916 attr = dwarf2_attr (die, DW_AT_name, cu);
7919 *name = DW_STRING (attr);
7922 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7924 *comp_dir = DW_STRING (attr);
7925 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7926 && IS_ABSOLUTE_PATH (*name))
7928 char *d = ldirname (*name);
7932 make_cleanup (xfree, d);
7934 if (*comp_dir != NULL)
7936 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7937 directory, get rid of it. */
7938 char *cp = strchr (*comp_dir, ':');
7940 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7945 *name = "<unknown>";
7948 /* Handle DW_AT_stmt_list for a compilation unit.
7949 DIE is the DW_TAG_compile_unit die for CU.
7950 COMP_DIR is the compilation directory.
7951 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7954 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7955 const char *comp_dir)
7957 struct attribute *attr;
7959 gdb_assert (! cu->per_cu->is_debug_types);
7961 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7964 unsigned int line_offset = DW_UNSND (attr);
7965 struct line_header *line_header
7966 = dwarf_decode_line_header (line_offset, cu);
7970 cu->line_header = line_header;
7971 make_cleanup (free_cu_line_header, cu);
7972 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7977 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7980 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7982 struct objfile *objfile = dwarf2_per_objfile->objfile;
7983 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7984 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7985 CORE_ADDR highpc = ((CORE_ADDR) 0);
7986 struct attribute *attr;
7987 const char *name = NULL;
7988 const char *comp_dir = NULL;
7989 struct die_info *child_die;
7990 bfd *abfd = objfile->obfd;
7993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7995 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7997 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7998 from finish_block. */
7999 if (lowpc == ((CORE_ADDR) -1))
8004 find_file_and_directory (die, cu, &name, &comp_dir);
8006 prepare_one_comp_unit (cu, die, cu->language);
8008 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8009 standardised yet. As a workaround for the language detection we fall
8010 back to the DW_AT_producer string. */
8011 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8012 cu->language = language_opencl;
8014 /* Similar hack for Go. */
8015 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8016 set_cu_language (DW_LANG_Go, cu);
8018 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8020 /* Decode line number information if present. We do this before
8021 processing child DIEs, so that the line header table is available
8022 for DW_AT_decl_file. */
8023 handle_DW_AT_stmt_list (die, cu, comp_dir);
8025 /* Process all dies in compilation unit. */
8026 if (die->child != NULL)
8028 child_die = die->child;
8029 while (child_die && child_die->tag)
8031 process_die (child_die, cu);
8032 child_die = sibling_die (child_die);
8036 /* Decode macro information, if present. Dwarf 2 macro information
8037 refers to information in the line number info statement program
8038 header, so we can only read it if we've read the header
8040 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8041 if (attr && cu->line_header)
8043 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8044 complaint (&symfile_complaints,
8045 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8047 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8051 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8052 if (attr && cu->line_header)
8054 unsigned int macro_offset = DW_UNSND (attr);
8056 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8060 do_cleanups (back_to);
8063 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8064 Create the set of symtabs used by this TU, or if this TU is sharing
8065 symtabs with another TU and the symtabs have already been created
8066 then restore those symtabs in the line header.
8067 We don't need the pc/line-number mapping for type units. */
8070 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8072 struct objfile *objfile = dwarf2_per_objfile->objfile;
8073 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8074 struct type_unit_group *tu_group;
8076 struct line_header *lh;
8077 struct attribute *attr;
8078 unsigned int i, line_offset;
8080 gdb_assert (per_cu->is_debug_types);
8082 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8084 /* If we're using .gdb_index (includes -readnow) then
8085 per_cu->s.type_unit_group may not have been set up yet. */
8086 if (per_cu->type_unit_group == NULL)
8087 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8088 tu_group = per_cu->type_unit_group;
8090 /* If we've already processed this stmt_list there's no real need to
8091 do it again, we could fake it and just recreate the part we need
8092 (file name,index -> symtab mapping). If data shows this optimization
8093 is useful we can do it then. */
8094 first_time = tu_group->primary_symtab == NULL;
8096 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8101 line_offset = DW_UNSND (attr);
8102 lh = dwarf_decode_line_header (line_offset, cu);
8107 dwarf2_start_symtab (cu, "", NULL, 0);
8110 gdb_assert (tu_group->symtabs == NULL);
8113 /* Note: The primary symtab will get allocated at the end. */
8117 cu->line_header = lh;
8118 make_cleanup (free_cu_line_header, cu);
8122 dwarf2_start_symtab (cu, "", NULL, 0);
8124 tu_group->num_symtabs = lh->num_file_names;
8125 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8127 for (i = 0; i < lh->num_file_names; ++i)
8130 struct file_entry *fe = &lh->file_names[i];
8133 dir = lh->include_dirs[fe->dir_index - 1];
8134 dwarf2_start_subfile (fe->name, dir, NULL);
8136 /* Note: We don't have to watch for the main subfile here, type units
8137 don't have DW_AT_name. */
8139 if (current_subfile->symtab == NULL)
8141 /* NOTE: start_subfile will recognize when it's been passed
8142 a file it has already seen. So we can't assume there's a
8143 simple mapping from lh->file_names to subfiles,
8144 lh->file_names may contain dups. */
8145 current_subfile->symtab = allocate_symtab (current_subfile->name,
8149 fe->symtab = current_subfile->symtab;
8150 tu_group->symtabs[i] = fe->symtab;
8157 for (i = 0; i < lh->num_file_names; ++i)
8159 struct file_entry *fe = &lh->file_names[i];
8161 fe->symtab = tu_group->symtabs[i];
8165 /* The main symtab is allocated last. Type units don't have DW_AT_name
8166 so they don't have a "real" (so to speak) symtab anyway.
8167 There is later code that will assign the main symtab to all symbols
8168 that don't have one. We need to handle the case of a symbol with a
8169 missing symtab (DW_AT_decl_file) anyway. */
8172 /* Process DW_TAG_type_unit.
8173 For TUs we want to skip the first top level sibling if it's not the
8174 actual type being defined by this TU. In this case the first top
8175 level sibling is there to provide context only. */
8178 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8180 struct die_info *child_die;
8182 prepare_one_comp_unit (cu, die, language_minimal);
8184 /* Initialize (or reinitialize) the machinery for building symtabs.
8185 We do this before processing child DIEs, so that the line header table
8186 is available for DW_AT_decl_file. */
8187 setup_type_unit_groups (die, cu);
8189 if (die->child != NULL)
8191 child_die = die->child;
8192 while (child_die && child_die->tag)
8194 process_die (child_die, cu);
8195 child_die = sibling_die (child_die);
8202 http://gcc.gnu.org/wiki/DebugFission
8203 http://gcc.gnu.org/wiki/DebugFissionDWP
8205 To simplify handling of both DWO files ("object" files with the DWARF info)
8206 and DWP files (a file with the DWOs packaged up into one file), we treat
8207 DWP files as having a collection of virtual DWO files. */
8210 hash_dwo_file (const void *item)
8212 const struct dwo_file *dwo_file = item;
8214 return htab_hash_string (dwo_file->name);
8218 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8220 const struct dwo_file *lhs = item_lhs;
8221 const struct dwo_file *rhs = item_rhs;
8223 return strcmp (lhs->name, rhs->name) == 0;
8226 /* Allocate a hash table for DWO files. */
8229 allocate_dwo_file_hash_table (void)
8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
8233 return htab_create_alloc_ex (41,
8237 &objfile->objfile_obstack,
8238 hashtab_obstack_allocate,
8239 dummy_obstack_deallocate);
8242 /* Lookup DWO file DWO_NAME. */
8245 lookup_dwo_file_slot (const char *dwo_name)
8247 struct dwo_file find_entry;
8250 if (dwarf2_per_objfile->dwo_files == NULL)
8251 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8253 memset (&find_entry, 0, sizeof (find_entry));
8254 find_entry.name = dwo_name;
8255 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8261 hash_dwo_unit (const void *item)
8263 const struct dwo_unit *dwo_unit = item;
8265 /* This drops the top 32 bits of the id, but is ok for a hash. */
8266 return dwo_unit->signature;
8270 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8272 const struct dwo_unit *lhs = item_lhs;
8273 const struct dwo_unit *rhs = item_rhs;
8275 /* The signature is assumed to be unique within the DWO file.
8276 So while object file CU dwo_id's always have the value zero,
8277 that's OK, assuming each object file DWO file has only one CU,
8278 and that's the rule for now. */
8279 return lhs->signature == rhs->signature;
8282 /* Allocate a hash table for DWO CUs,TUs.
8283 There is one of these tables for each of CUs,TUs for each DWO file. */
8286 allocate_dwo_unit_table (struct objfile *objfile)
8288 /* Start out with a pretty small number.
8289 Generally DWO files contain only one CU and maybe some TUs. */
8290 return htab_create_alloc_ex (3,
8294 &objfile->objfile_obstack,
8295 hashtab_obstack_allocate,
8296 dummy_obstack_deallocate);
8299 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8301 struct create_dwo_info_table_data
8303 struct dwo_file *dwo_file;
8307 /* die_reader_func for create_dwo_debug_info_hash_table. */
8310 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8312 struct die_info *comp_unit_die,
8316 struct dwarf2_cu *cu = reader->cu;
8317 struct objfile *objfile = dwarf2_per_objfile->objfile;
8318 sect_offset offset = cu->per_cu->offset;
8319 struct dwarf2_section_info *section = cu->per_cu->section;
8320 struct create_dwo_info_table_data *data = datap;
8321 struct dwo_file *dwo_file = data->dwo_file;
8322 htab_t cu_htab = data->cu_htab;
8324 struct attribute *attr;
8325 struct dwo_unit *dwo_unit;
8327 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8330 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8331 " its dwo_id [in module %s]"),
8332 offset.sect_off, dwo_file->name);
8336 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8337 dwo_unit->dwo_file = dwo_file;
8338 dwo_unit->signature = DW_UNSND (attr);
8339 dwo_unit->section = section;
8340 dwo_unit->offset = offset;
8341 dwo_unit->length = cu->per_cu->length;
8343 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8344 gdb_assert (slot != NULL);
8347 const struct dwo_unit *dup_dwo_unit = *slot;
8349 complaint (&symfile_complaints,
8350 _("debug entry at offset 0x%x is duplicate to the entry at"
8351 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8352 offset.sect_off, dup_dwo_unit->offset.sect_off,
8353 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8359 if (dwarf2_read_debug)
8360 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8362 phex (dwo_unit->signature,
8363 sizeof (dwo_unit->signature)));
8366 /* Create a hash table to map DWO IDs to their CU entry in
8367 .debug_info.dwo in DWO_FILE.
8368 Note: This function processes DWO files only, not DWP files. */
8371 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8373 struct objfile *objfile = dwarf2_per_objfile->objfile;
8374 struct dwarf2_section_info *section = &dwo_file->sections.info;
8377 gdb_byte *info_ptr, *end_ptr;
8378 struct create_dwo_info_table_data create_dwo_info_table_data;
8380 dwarf2_read_section (objfile, section);
8381 info_ptr = section->buffer;
8383 if (info_ptr == NULL)
8386 /* We can't set abfd until now because the section may be empty or
8387 not present, in which case section->asection will be NULL. */
8388 abfd = section->asection->owner;
8390 if (dwarf2_read_debug)
8391 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8392 bfd_get_filename (abfd));
8394 cu_htab = allocate_dwo_unit_table (objfile);
8396 create_dwo_info_table_data.dwo_file = dwo_file;
8397 create_dwo_info_table_data.cu_htab = cu_htab;
8399 end_ptr = info_ptr + section->size;
8400 while (info_ptr < end_ptr)
8402 struct dwarf2_per_cu_data per_cu;
8404 memset (&per_cu, 0, sizeof (per_cu));
8405 per_cu.objfile = objfile;
8406 per_cu.is_debug_types = 0;
8407 per_cu.offset.sect_off = info_ptr - section->buffer;
8408 per_cu.section = section;
8410 init_cutu_and_read_dies_no_follow (&per_cu,
8411 &dwo_file->sections.abbrev,
8413 create_dwo_debug_info_hash_table_reader,
8414 &create_dwo_info_table_data);
8416 info_ptr += per_cu.length;
8422 /* DWP file .debug_{cu,tu}_index section format:
8423 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8425 Both index sections have the same format, and serve to map a 64-bit
8426 signature to a set of section numbers. Each section begins with a header,
8427 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8428 indexes, and a pool of 32-bit section numbers. The index sections will be
8429 aligned at 8-byte boundaries in the file.
8431 The index section header contains two unsigned 32-bit values (using the
8432 byte order of the application binary):
8434 N, the number of compilation units or type units in the index
8435 M, the number of slots in the hash table
8437 (We assume that N and M will not exceed 2^32 - 1.)
8439 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8441 The hash table begins at offset 8 in the section, and consists of an array
8442 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8443 order of the application binary). Unused slots in the hash table are 0.
8444 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8446 The parallel table begins immediately after the hash table
8447 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8448 array of 32-bit indexes (using the byte order of the application binary),
8449 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8450 table contains a 32-bit index into the pool of section numbers. For unused
8451 hash table slots, the corresponding entry in the parallel table will be 0.
8453 Given a 64-bit compilation unit signature or a type signature S, an entry
8454 in the hash table is located as follows:
8456 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8457 the low-order k bits all set to 1.
8459 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8461 3) If the hash table entry at index H matches the signature, use that
8462 entry. If the hash table entry at index H is unused (all zeroes),
8463 terminate the search: the signature is not present in the table.
8465 4) Let H = (H + H') modulo M. Repeat at Step 3.
8467 Because M > N and H' and M are relatively prime, the search is guaranteed
8468 to stop at an unused slot or find the match.
8470 The pool of section numbers begins immediately following the hash table
8471 (at offset 8 + 12 * M from the beginning of the section). The pool of
8472 section numbers consists of an array of 32-bit words (using the byte order
8473 of the application binary). Each item in the array is indexed starting
8474 from 0. The hash table entry provides the index of the first section
8475 number in the set. Additional section numbers in the set follow, and the
8476 set is terminated by a 0 entry (section number 0 is not used in ELF).
8478 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8479 section must be the first entry in the set, and the .debug_abbrev.dwo must
8480 be the second entry. Other members of the set may follow in any order. */
8482 /* Create a hash table to map DWO IDs to their CU/TU entry in
8483 .debug_{info,types}.dwo in DWP_FILE.
8484 Returns NULL if there isn't one.
8485 Note: This function processes DWP files only, not DWO files. */
8487 static struct dwp_hash_table *
8488 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8490 struct objfile *objfile = dwarf2_per_objfile->objfile;
8491 bfd *dbfd = dwp_file->dbfd;
8492 char *index_ptr, *index_end;
8493 struct dwarf2_section_info *index;
8494 uint32_t version, nr_units, nr_slots;
8495 struct dwp_hash_table *htab;
8498 index = &dwp_file->sections.tu_index;
8500 index = &dwp_file->sections.cu_index;
8502 if (dwarf2_section_empty_p (index))
8504 dwarf2_read_section (objfile, index);
8506 index_ptr = index->buffer;
8507 index_end = index_ptr + index->size;
8509 version = read_4_bytes (dbfd, index_ptr);
8510 index_ptr += 8; /* Skip the unused word. */
8511 nr_units = read_4_bytes (dbfd, index_ptr);
8513 nr_slots = read_4_bytes (dbfd, index_ptr);
8518 error (_("Dwarf Error: unsupported DWP file version (%u)"
8520 version, dwp_file->name);
8522 if (nr_slots != (nr_slots & -nr_slots))
8524 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8525 " is not power of 2 [in module %s]"),
8526 nr_slots, dwp_file->name);
8529 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8530 htab->nr_units = nr_units;
8531 htab->nr_slots = nr_slots;
8532 htab->hash_table = index_ptr;
8533 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8534 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8539 /* Update SECTIONS with the data from SECTP.
8541 This function is like the other "locate" section routines that are
8542 passed to bfd_map_over_sections, but in this context the sections to
8543 read comes from the DWP hash table, not the full ELF section table.
8545 The result is non-zero for success, or zero if an error was found. */
8548 locate_virtual_dwo_sections (asection *sectp,
8549 struct virtual_dwo_sections *sections)
8551 const struct dwop_section_names *names = &dwop_section_names;
8553 if (section_is_p (sectp->name, &names->abbrev_dwo))
8555 /* There can be only one. */
8556 if (sections->abbrev.asection != NULL)
8558 sections->abbrev.asection = sectp;
8559 sections->abbrev.size = bfd_get_section_size (sectp);
8561 else if (section_is_p (sectp->name, &names->info_dwo)
8562 || section_is_p (sectp->name, &names->types_dwo))
8564 /* There can be only one. */
8565 if (sections->info_or_types.asection != NULL)
8567 sections->info_or_types.asection = sectp;
8568 sections->info_or_types.size = bfd_get_section_size (sectp);
8570 else if (section_is_p (sectp->name, &names->line_dwo))
8572 /* There can be only one. */
8573 if (sections->line.asection != NULL)
8575 sections->line.asection = sectp;
8576 sections->line.size = bfd_get_section_size (sectp);
8578 else if (section_is_p (sectp->name, &names->loc_dwo))
8580 /* There can be only one. */
8581 if (sections->loc.asection != NULL)
8583 sections->loc.asection = sectp;
8584 sections->loc.size = bfd_get_section_size (sectp);
8586 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8588 /* There can be only one. */
8589 if (sections->macinfo.asection != NULL)
8591 sections->macinfo.asection = sectp;
8592 sections->macinfo.size = bfd_get_section_size (sectp);
8594 else if (section_is_p (sectp->name, &names->macro_dwo))
8596 /* There can be only one. */
8597 if (sections->macro.asection != NULL)
8599 sections->macro.asection = sectp;
8600 sections->macro.size = bfd_get_section_size (sectp);
8602 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8604 /* There can be only one. */
8605 if (sections->str_offsets.asection != NULL)
8607 sections->str_offsets.asection = sectp;
8608 sections->str_offsets.size = bfd_get_section_size (sectp);
8612 /* No other kind of section is valid. */
8619 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8620 HTAB is the hash table from the DWP file.
8621 SECTION_INDEX is the index of the DWO in HTAB. */
8623 static struct dwo_unit *
8624 create_dwo_in_dwp (struct dwp_file *dwp_file,
8625 const struct dwp_hash_table *htab,
8626 uint32_t section_index,
8627 ULONGEST signature, int is_debug_types)
8629 struct objfile *objfile = dwarf2_per_objfile->objfile;
8630 bfd *dbfd = dwp_file->dbfd;
8631 const char *kind = is_debug_types ? "TU" : "CU";
8632 struct dwo_file *dwo_file;
8633 struct dwo_unit *dwo_unit;
8634 struct virtual_dwo_sections sections;
8635 void **dwo_file_slot;
8636 char *virtual_dwo_name;
8637 struct dwarf2_section_info *cutu;
8638 struct cleanup *cleanups;
8641 if (dwarf2_read_debug)
8643 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8645 section_index, phex (signature, sizeof (signature)),
8649 /* Fetch the sections of this DWO.
8650 Put a limit on the number of sections we look for so that bad data
8651 doesn't cause us to loop forever. */
8653 #define MAX_NR_DWO_SECTIONS \
8654 (1 /* .debug_info or .debug_types */ \
8655 + 1 /* .debug_abbrev */ \
8656 + 1 /* .debug_line */ \
8657 + 1 /* .debug_loc */ \
8658 + 1 /* .debug_str_offsets */ \
8659 + 1 /* .debug_macro */ \
8660 + 1 /* .debug_macinfo */ \
8661 + 1 /* trailing zero */)
8663 memset (§ions, 0, sizeof (sections));
8664 cleanups = make_cleanup (null_cleanup, 0);
8666 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8669 uint32_t section_nr =
8672 + (section_index + i) * sizeof (uint32_t));
8674 if (section_nr == 0)
8676 if (section_nr >= dwp_file->num_sections)
8678 error (_("Dwarf Error: bad DWP hash table, section number too large"
8683 sectp = dwp_file->elf_sections[section_nr];
8684 if (! locate_virtual_dwo_sections (sectp, §ions))
8686 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8693 || sections.info_or_types.asection == NULL
8694 || sections.abbrev.asection == NULL)
8696 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8700 if (i == MAX_NR_DWO_SECTIONS)
8702 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8707 /* It's easier for the rest of the code if we fake a struct dwo_file and
8708 have dwo_unit "live" in that. At least for now.
8710 The DWP file can be made up of a random collection of CUs and TUs.
8711 However, for each CU + set of TUs that came from the same original DWO
8712 file, we want to combine them back into a virtual DWO file to save space
8713 (fewer struct dwo_file objects to allocated). Remember that for really
8714 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8717 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8718 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8719 sections.line.asection ? sections.line.asection->id : 0,
8720 sections.loc.asection ? sections.loc.asection->id : 0,
8721 (sections.str_offsets.asection
8722 ? sections.str_offsets.asection->id
8724 make_cleanup (xfree, virtual_dwo_name);
8725 /* Can we use an existing virtual DWO file? */
8726 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8727 /* Create one if necessary. */
8728 if (*dwo_file_slot == NULL)
8730 if (dwarf2_read_debug)
8732 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8735 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8736 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8738 strlen (virtual_dwo_name));
8739 dwo_file->sections.abbrev = sections.abbrev;
8740 dwo_file->sections.line = sections.line;
8741 dwo_file->sections.loc = sections.loc;
8742 dwo_file->sections.macinfo = sections.macinfo;
8743 dwo_file->sections.macro = sections.macro;
8744 dwo_file->sections.str_offsets = sections.str_offsets;
8745 /* The "str" section is global to the entire DWP file. */
8746 dwo_file->sections.str = dwp_file->sections.str;
8747 /* The info or types section is assigned later to dwo_unit,
8748 there's no need to record it in dwo_file.
8749 Also, we can't simply record type sections in dwo_file because
8750 we record a pointer into the vector in dwo_unit. As we collect more
8751 types we'll grow the vector and eventually have to reallocate space
8752 for it, invalidating all the pointers into the current copy. */
8753 *dwo_file_slot = dwo_file;
8757 if (dwarf2_read_debug)
8759 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8762 dwo_file = *dwo_file_slot;
8764 do_cleanups (cleanups);
8766 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8767 dwo_unit->dwo_file = dwo_file;
8768 dwo_unit->signature = signature;
8769 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8770 sizeof (struct dwarf2_section_info));
8771 *dwo_unit->section = sections.info_or_types;
8772 /* offset, length, type_offset_in_tu are set later. */
8777 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8779 static struct dwo_unit *
8780 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8781 const struct dwp_hash_table *htab,
8782 ULONGEST signature, int is_debug_types)
8784 bfd *dbfd = dwp_file->dbfd;
8785 uint32_t mask = htab->nr_slots - 1;
8786 uint32_t hash = signature & mask;
8787 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8790 struct dwo_unit find_dwo_cu, *dwo_cu;
8792 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8793 find_dwo_cu.signature = signature;
8794 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8799 /* Use a for loop so that we don't loop forever on bad debug info. */
8800 for (i = 0; i < htab->nr_slots; ++i)
8802 ULONGEST signature_in_table;
8804 signature_in_table =
8805 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8806 if (signature_in_table == signature)
8808 uint32_t section_index =
8809 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8811 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8812 signature, is_debug_types);
8815 if (signature_in_table == 0)
8817 hash = (hash + hash2) & mask;
8820 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8825 /* Subroutine of open_dwop_file to simplify it.
8826 Open the file specified by FILE_NAME and hand it off to BFD for
8827 preliminary analysis. Return a newly initialized bfd *, which
8828 includes a canonicalized copy of FILE_NAME.
8829 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8830 In case of trouble, return NULL.
8831 NOTE: This function is derived from symfile_bfd_open. */
8834 try_open_dwop_file (const char *file_name, int is_dwp)
8838 char *absolute_name;
8840 flags = OPF_TRY_CWD_FIRST;
8842 flags |= OPF_SEARCH_IN_PATH;
8843 desc = openp (debug_file_directory, flags, file_name,
8844 O_RDONLY | O_BINARY, &absolute_name);
8848 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8851 xfree (absolute_name);
8854 xfree (absolute_name);
8855 bfd_set_cacheable (sym_bfd, 1);
8857 if (!bfd_check_format (sym_bfd, bfd_object))
8859 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8866 /* Try to open DWO/DWP file FILE_NAME.
8867 COMP_DIR is the DW_AT_comp_dir attribute.
8868 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8869 The result is the bfd handle of the file.
8870 If there is a problem finding or opening the file, return NULL.
8871 Upon success, the canonicalized path of the file is stored in the bfd,
8872 same as symfile_bfd_open. */
8875 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8879 if (IS_ABSOLUTE_PATH (file_name))
8880 return try_open_dwop_file (file_name, is_dwp);
8882 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8884 if (comp_dir != NULL)
8886 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8888 /* NOTE: If comp_dir is a relative path, this will also try the
8889 search path, which seems useful. */
8890 abfd = try_open_dwop_file (path_to_try, is_dwp);
8891 xfree (path_to_try);
8896 /* That didn't work, try debug-file-directory, which, despite its name,
8897 is a list of paths. */
8899 if (*debug_file_directory == '\0')
8902 return try_open_dwop_file (file_name, is_dwp);
8905 /* This function is mapped across the sections and remembers the offset and
8906 size of each of the DWO debugging sections we are interested in. */
8909 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8911 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8912 const struct dwop_section_names *names = &dwop_section_names;
8914 if (section_is_p (sectp->name, &names->abbrev_dwo))
8916 dwo_sections->abbrev.asection = sectp;
8917 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8919 else if (section_is_p (sectp->name, &names->info_dwo))
8921 dwo_sections->info.asection = sectp;
8922 dwo_sections->info.size = bfd_get_section_size (sectp);
8924 else if (section_is_p (sectp->name, &names->line_dwo))
8926 dwo_sections->line.asection = sectp;
8927 dwo_sections->line.size = bfd_get_section_size (sectp);
8929 else if (section_is_p (sectp->name, &names->loc_dwo))
8931 dwo_sections->loc.asection = sectp;
8932 dwo_sections->loc.size = bfd_get_section_size (sectp);
8934 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8936 dwo_sections->macinfo.asection = sectp;
8937 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8939 else if (section_is_p (sectp->name, &names->macro_dwo))
8941 dwo_sections->macro.asection = sectp;
8942 dwo_sections->macro.size = bfd_get_section_size (sectp);
8944 else if (section_is_p (sectp->name, &names->str_dwo))
8946 dwo_sections->str.asection = sectp;
8947 dwo_sections->str.size = bfd_get_section_size (sectp);
8949 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8951 dwo_sections->str_offsets.asection = sectp;
8952 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8954 else if (section_is_p (sectp->name, &names->types_dwo))
8956 struct dwarf2_section_info type_section;
8958 memset (&type_section, 0, sizeof (type_section));
8959 type_section.asection = sectp;
8960 type_section.size = bfd_get_section_size (sectp);
8961 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8966 /* Initialize the use of the DWO file specified by DWO_NAME.
8967 The result is NULL if DWO_NAME can't be found. */
8969 static struct dwo_file *
8970 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8972 struct objfile *objfile = dwarf2_per_objfile->objfile;
8973 struct dwo_file *dwo_file;
8975 struct cleanup *cleanups;
8977 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8980 if (dwarf2_read_debug)
8981 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8984 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8985 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8986 dwo_name, strlen (dwo_name));
8987 dwo_file->dbfd = dbfd;
8989 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8991 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8993 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8995 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8996 dwo_file->sections.types);
8998 discard_cleanups (cleanups);
9000 if (dwarf2_read_debug)
9001 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9006 /* This function is mapped across the sections and remembers the offset and
9007 size of each of the DWP debugging sections we are interested in. */
9010 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9012 struct dwp_file *dwp_file = dwp_file_ptr;
9013 const struct dwop_section_names *names = &dwop_section_names;
9014 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9016 /* Record the ELF section number for later lookup: this is what the
9017 .debug_cu_index,.debug_tu_index tables use. */
9018 gdb_assert (elf_section_nr < dwp_file->num_sections);
9019 dwp_file->elf_sections[elf_section_nr] = sectp;
9021 /* Look for specific sections that we need. */
9022 if (section_is_p (sectp->name, &names->str_dwo))
9024 dwp_file->sections.str.asection = sectp;
9025 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9027 else if (section_is_p (sectp->name, &names->cu_index))
9029 dwp_file->sections.cu_index.asection = sectp;
9030 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9032 else if (section_is_p (sectp->name, &names->tu_index))
9034 dwp_file->sections.tu_index.asection = sectp;
9035 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9039 /* Hash function for dwp_file loaded CUs/TUs. */
9042 hash_dwp_loaded_cutus (const void *item)
9044 const struct dwo_unit *dwo_unit = item;
9046 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9047 return dwo_unit->signature;
9050 /* Equality function for dwp_file loaded CUs/TUs. */
9053 eq_dwp_loaded_cutus (const void *a, const void *b)
9055 const struct dwo_unit *dua = a;
9056 const struct dwo_unit *dub = b;
9058 return dua->signature == dub->signature;
9061 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9064 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9066 return htab_create_alloc_ex (3,
9067 hash_dwp_loaded_cutus,
9068 eq_dwp_loaded_cutus,
9070 &objfile->objfile_obstack,
9071 hashtab_obstack_allocate,
9072 dummy_obstack_deallocate);
9075 /* Initialize the use of the DWP file for the current objfile.
9076 By convention the name of the DWP file is ${objfile}.dwp.
9077 The result is NULL if it can't be found. */
9079 static struct dwp_file *
9080 open_and_init_dwp_file (const char *comp_dir)
9082 struct objfile *objfile = dwarf2_per_objfile->objfile;
9083 struct dwp_file *dwp_file;
9086 struct cleanup *cleanups;
9088 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9089 cleanups = make_cleanup (xfree, dwp_name);
9091 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9094 if (dwarf2_read_debug)
9095 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9096 do_cleanups (cleanups);
9099 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9100 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9101 dwp_name, strlen (dwp_name));
9102 dwp_file->dbfd = dbfd;
9103 do_cleanups (cleanups);
9105 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9107 /* +1: section 0 is unused */
9108 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9109 dwp_file->elf_sections =
9110 OBSTACK_CALLOC (&objfile->objfile_obstack,
9111 dwp_file->num_sections, asection *);
9113 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9115 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9117 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9119 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9121 discard_cleanups (cleanups);
9123 if (dwarf2_read_debug)
9125 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9126 fprintf_unfiltered (gdb_stdlog,
9127 " %u CUs, %u TUs\n",
9128 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9129 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9135 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9136 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9137 or in the DWP file for the objfile, referenced by THIS_UNIT.
9138 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9139 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9141 This is called, for example, when wanting to read a variable with a
9142 complex location. Therefore we don't want to do file i/o for every call.
9143 Therefore we don't want to look for a DWO file on every call.
9144 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9145 then we check if we've already seen DWO_NAME, and only THEN do we check
9148 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9149 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9151 static struct dwo_unit *
9152 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9153 const char *dwo_name, const char *comp_dir,
9154 ULONGEST signature, int is_debug_types)
9156 struct objfile *objfile = dwarf2_per_objfile->objfile;
9157 const char *kind = is_debug_types ? "TU" : "CU";
9158 void **dwo_file_slot;
9159 struct dwo_file *dwo_file;
9160 struct dwp_file *dwp_file;
9162 /* Have we already read SIGNATURE from a DWP file? */
9164 if (! dwarf2_per_objfile->dwp_checked)
9166 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9167 dwarf2_per_objfile->dwp_checked = 1;
9169 dwp_file = dwarf2_per_objfile->dwp_file;
9171 if (dwp_file != NULL)
9173 const struct dwp_hash_table *dwp_htab =
9174 is_debug_types ? dwp_file->tus : dwp_file->cus;
9176 if (dwp_htab != NULL)
9178 struct dwo_unit *dwo_cutu =
9179 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9181 if (dwo_cutu != NULL)
9183 if (dwarf2_read_debug)
9185 fprintf_unfiltered (gdb_stdlog,
9186 "Virtual DWO %s %s found: @%s\n",
9187 kind, hex_string (signature),
9188 host_address_to_string (dwo_cutu));
9195 /* Have we already seen DWO_NAME? */
9197 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9198 if (*dwo_file_slot == NULL)
9200 /* Read in the file and build a table of the DWOs it contains. */
9201 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9203 /* NOTE: This will be NULL if unable to open the file. */
9204 dwo_file = *dwo_file_slot;
9206 if (dwo_file != NULL)
9208 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9212 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9214 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9215 find_dwo_cutu.signature = signature;
9216 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9218 if (dwo_cutu != NULL)
9220 if (dwarf2_read_debug)
9222 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9223 kind, dwo_name, hex_string (signature),
9224 host_address_to_string (dwo_cutu));
9231 /* We didn't find it. This could mean a dwo_id mismatch, or
9232 someone deleted the DWO/DWP file, or the search path isn't set up
9233 correctly to find the file. */
9235 if (dwarf2_read_debug)
9237 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9238 kind, dwo_name, hex_string (signature));
9241 complaint (&symfile_complaints,
9242 _("Could not find DWO CU referenced by CU at offset 0x%x"
9244 this_unit->offset.sect_off, objfile->name);
9248 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9249 See lookup_dwo_cutu_unit for details. */
9251 static struct dwo_unit *
9252 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9253 const char *dwo_name, const char *comp_dir,
9256 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9259 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9260 See lookup_dwo_cutu_unit for details. */
9262 static struct dwo_unit *
9263 lookup_dwo_type_unit (struct signatured_type *this_tu,
9264 const char *dwo_name, const char *comp_dir)
9266 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9269 /* Free all resources associated with DWO_FILE.
9270 Close the DWO file and munmap the sections.
9271 All memory should be on the objfile obstack. */
9274 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9277 struct dwarf2_section_info *section;
9279 gdb_bfd_unref (dwo_file->dbfd);
9281 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9284 /* Wrapper for free_dwo_file for use in cleanups. */
9287 free_dwo_file_cleanup (void *arg)
9289 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9290 struct objfile *objfile = dwarf2_per_objfile->objfile;
9292 free_dwo_file (dwo_file, objfile);
9295 /* Traversal function for free_dwo_files. */
9298 free_dwo_file_from_slot (void **slot, void *info)
9300 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9301 struct objfile *objfile = (struct objfile *) info;
9303 free_dwo_file (dwo_file, objfile);
9308 /* Free all resources associated with DWO_FILES. */
9311 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9313 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9316 /* Read in various DIEs. */
9318 /* qsort helper for inherit_abstract_dies. */
9321 unsigned_int_compar (const void *ap, const void *bp)
9323 unsigned int a = *(unsigned int *) ap;
9324 unsigned int b = *(unsigned int *) bp;
9326 return (a > b) - (b > a);
9329 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9330 Inherit only the children of the DW_AT_abstract_origin DIE not being
9331 already referenced by DW_AT_abstract_origin from the children of the
9335 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9337 struct die_info *child_die;
9338 unsigned die_children_count;
9339 /* CU offsets which were referenced by children of the current DIE. */
9340 sect_offset *offsets;
9341 sect_offset *offsets_end, *offsetp;
9342 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9343 struct die_info *origin_die;
9344 /* Iterator of the ORIGIN_DIE children. */
9345 struct die_info *origin_child_die;
9346 struct cleanup *cleanups;
9347 struct attribute *attr;
9348 struct dwarf2_cu *origin_cu;
9349 struct pending **origin_previous_list_in_scope;
9351 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9355 /* Note that following die references may follow to a die in a
9359 origin_die = follow_die_ref (die, attr, &origin_cu);
9361 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9363 origin_previous_list_in_scope = origin_cu->list_in_scope;
9364 origin_cu->list_in_scope = cu->list_in_scope;
9366 if (die->tag != origin_die->tag
9367 && !(die->tag == DW_TAG_inlined_subroutine
9368 && origin_die->tag == DW_TAG_subprogram))
9369 complaint (&symfile_complaints,
9370 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9371 die->offset.sect_off, origin_die->offset.sect_off);
9373 child_die = die->child;
9374 die_children_count = 0;
9375 while (child_die && child_die->tag)
9377 child_die = sibling_die (child_die);
9378 die_children_count++;
9380 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9381 cleanups = make_cleanup (xfree, offsets);
9383 offsets_end = offsets;
9384 child_die = die->child;
9385 while (child_die && child_die->tag)
9387 /* For each CHILD_DIE, find the corresponding child of
9388 ORIGIN_DIE. If there is more than one layer of
9389 DW_AT_abstract_origin, follow them all; there shouldn't be,
9390 but GCC versions at least through 4.4 generate this (GCC PR
9392 struct die_info *child_origin_die = child_die;
9393 struct dwarf2_cu *child_origin_cu = cu;
9397 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9401 child_origin_die = follow_die_ref (child_origin_die, attr,
9405 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9406 counterpart may exist. */
9407 if (child_origin_die != child_die)
9409 if (child_die->tag != child_origin_die->tag
9410 && !(child_die->tag == DW_TAG_inlined_subroutine
9411 && child_origin_die->tag == DW_TAG_subprogram))
9412 complaint (&symfile_complaints,
9413 _("Child DIE 0x%x and its abstract origin 0x%x have "
9414 "different tags"), child_die->offset.sect_off,
9415 child_origin_die->offset.sect_off);
9416 if (child_origin_die->parent != origin_die)
9417 complaint (&symfile_complaints,
9418 _("Child DIE 0x%x and its abstract origin 0x%x have "
9419 "different parents"), child_die->offset.sect_off,
9420 child_origin_die->offset.sect_off);
9422 *offsets_end++ = child_origin_die->offset;
9424 child_die = sibling_die (child_die);
9426 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9427 unsigned_int_compar);
9428 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9429 if (offsetp[-1].sect_off == offsetp->sect_off)
9430 complaint (&symfile_complaints,
9431 _("Multiple children of DIE 0x%x refer "
9432 "to DIE 0x%x as their abstract origin"),
9433 die->offset.sect_off, offsetp->sect_off);
9436 origin_child_die = origin_die->child;
9437 while (origin_child_die && origin_child_die->tag)
9439 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9440 while (offsetp < offsets_end
9441 && offsetp->sect_off < origin_child_die->offset.sect_off)
9443 if (offsetp >= offsets_end
9444 || offsetp->sect_off > origin_child_die->offset.sect_off)
9446 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9447 process_die (origin_child_die, origin_cu);
9449 origin_child_die = sibling_die (origin_child_die);
9451 origin_cu->list_in_scope = origin_previous_list_in_scope;
9453 do_cleanups (cleanups);
9457 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9459 struct objfile *objfile = cu->objfile;
9460 struct context_stack *new;
9463 struct die_info *child_die;
9464 struct attribute *attr, *call_line, *call_file;
9467 struct block *block;
9468 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9469 VEC (symbolp) *template_args = NULL;
9470 struct template_symbol *templ_func = NULL;
9474 /* If we do not have call site information, we can't show the
9475 caller of this inlined function. That's too confusing, so
9476 only use the scope for local variables. */
9477 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9478 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9479 if (call_line == NULL || call_file == NULL)
9481 read_lexical_block_scope (die, cu);
9486 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9488 name = dwarf2_name (die, cu);
9490 /* Ignore functions with missing or empty names. These are actually
9491 illegal according to the DWARF standard. */
9494 complaint (&symfile_complaints,
9495 _("missing name for subprogram DIE at %d"),
9496 die->offset.sect_off);
9500 /* Ignore functions with missing or invalid low and high pc attributes. */
9501 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9503 attr = dwarf2_attr (die, DW_AT_external, cu);
9504 if (!attr || !DW_UNSND (attr))
9505 complaint (&symfile_complaints,
9506 _("cannot get low and high bounds "
9507 "for subprogram DIE at %d"),
9508 die->offset.sect_off);
9515 /* If we have any template arguments, then we must allocate a
9516 different sort of symbol. */
9517 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9519 if (child_die->tag == DW_TAG_template_type_param
9520 || child_die->tag == DW_TAG_template_value_param)
9522 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9523 struct template_symbol);
9524 templ_func->base.is_cplus_template_function = 1;
9529 new = push_context (0, lowpc);
9530 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9531 (struct symbol *) templ_func);
9533 /* If there is a location expression for DW_AT_frame_base, record
9535 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9537 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9539 cu->list_in_scope = &local_symbols;
9541 if (die->child != NULL)
9543 child_die = die->child;
9544 while (child_die && child_die->tag)
9546 if (child_die->tag == DW_TAG_template_type_param
9547 || child_die->tag == DW_TAG_template_value_param)
9549 struct symbol *arg = new_symbol (child_die, NULL, cu);
9552 VEC_safe_push (symbolp, template_args, arg);
9555 process_die (child_die, cu);
9556 child_die = sibling_die (child_die);
9560 inherit_abstract_dies (die, cu);
9562 /* If we have a DW_AT_specification, we might need to import using
9563 directives from the context of the specification DIE. See the
9564 comment in determine_prefix. */
9565 if (cu->language == language_cplus
9566 && dwarf2_attr (die, DW_AT_specification, cu))
9568 struct dwarf2_cu *spec_cu = cu;
9569 struct die_info *spec_die = die_specification (die, &spec_cu);
9573 child_die = spec_die->child;
9574 while (child_die && child_die->tag)
9576 if (child_die->tag == DW_TAG_imported_module)
9577 process_die (child_die, spec_cu);
9578 child_die = sibling_die (child_die);
9581 /* In some cases, GCC generates specification DIEs that
9582 themselves contain DW_AT_specification attributes. */
9583 spec_die = die_specification (spec_die, &spec_cu);
9587 new = pop_context ();
9588 /* Make a block for the local symbols within. */
9589 block = finish_block (new->name, &local_symbols, new->old_blocks,
9590 lowpc, highpc, objfile);
9592 /* For C++, set the block's scope. */
9593 if ((cu->language == language_cplus || cu->language == language_fortran)
9594 && cu->processing_has_namespace_info)
9595 block_set_scope (block, determine_prefix (die, cu),
9596 &objfile->objfile_obstack);
9598 /* If we have address ranges, record them. */
9599 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9601 /* Attach template arguments to function. */
9602 if (! VEC_empty (symbolp, template_args))
9604 gdb_assert (templ_func != NULL);
9606 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9607 templ_func->template_arguments
9608 = obstack_alloc (&objfile->objfile_obstack,
9609 (templ_func->n_template_arguments
9610 * sizeof (struct symbol *)));
9611 memcpy (templ_func->template_arguments,
9612 VEC_address (symbolp, template_args),
9613 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9614 VEC_free (symbolp, template_args);
9617 /* In C++, we can have functions nested inside functions (e.g., when
9618 a function declares a class that has methods). This means that
9619 when we finish processing a function scope, we may need to go
9620 back to building a containing block's symbol lists. */
9621 local_symbols = new->locals;
9622 using_directives = new->using_directives;
9624 /* If we've finished processing a top-level function, subsequent
9625 symbols go in the file symbol list. */
9626 if (outermost_context_p ())
9627 cu->list_in_scope = &file_symbols;
9630 /* Process all the DIES contained within a lexical block scope. Start
9631 a new scope, process the dies, and then close the scope. */
9634 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9636 struct objfile *objfile = cu->objfile;
9637 struct context_stack *new;
9638 CORE_ADDR lowpc, highpc;
9639 struct die_info *child_die;
9642 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9644 /* Ignore blocks with missing or invalid low and high pc attributes. */
9645 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9646 as multiple lexical blocks? Handling children in a sane way would
9647 be nasty. Might be easier to properly extend generic blocks to
9649 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9654 push_context (0, lowpc);
9655 if (die->child != NULL)
9657 child_die = die->child;
9658 while (child_die && child_die->tag)
9660 process_die (child_die, cu);
9661 child_die = sibling_die (child_die);
9664 new = pop_context ();
9666 if (local_symbols != NULL || using_directives != NULL)
9669 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9672 /* Note that recording ranges after traversing children, as we
9673 do here, means that recording a parent's ranges entails
9674 walking across all its children's ranges as they appear in
9675 the address map, which is quadratic behavior.
9677 It would be nicer to record the parent's ranges before
9678 traversing its children, simply overriding whatever you find
9679 there. But since we don't even decide whether to create a
9680 block until after we've traversed its children, that's hard
9682 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9684 local_symbols = new->locals;
9685 using_directives = new->using_directives;
9688 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9691 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9693 struct objfile *objfile = cu->objfile;
9694 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9695 CORE_ADDR pc, baseaddr;
9696 struct attribute *attr;
9697 struct call_site *call_site, call_site_local;
9700 struct die_info *child_die;
9702 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9704 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9707 complaint (&symfile_complaints,
9708 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9709 "DIE 0x%x [in module %s]"),
9710 die->offset.sect_off, objfile->name);
9713 pc = DW_ADDR (attr) + baseaddr;
9715 if (cu->call_site_htab == NULL)
9716 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9717 NULL, &objfile->objfile_obstack,
9718 hashtab_obstack_allocate, NULL);
9719 call_site_local.pc = pc;
9720 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9723 complaint (&symfile_complaints,
9724 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9725 "DIE 0x%x [in module %s]"),
9726 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9730 /* Count parameters at the caller. */
9733 for (child_die = die->child; child_die && child_die->tag;
9734 child_die = sibling_die (child_die))
9736 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9738 complaint (&symfile_complaints,
9739 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9740 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9741 child_die->tag, child_die->offset.sect_off, objfile->name);
9748 call_site = obstack_alloc (&objfile->objfile_obstack,
9749 (sizeof (*call_site)
9750 + (sizeof (*call_site->parameter)
9753 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9756 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9758 struct die_info *func_die;
9760 /* Skip also over DW_TAG_inlined_subroutine. */
9761 for (func_die = die->parent;
9762 func_die && func_die->tag != DW_TAG_subprogram
9763 && func_die->tag != DW_TAG_subroutine_type;
9764 func_die = func_die->parent);
9766 /* DW_AT_GNU_all_call_sites is a superset
9767 of DW_AT_GNU_all_tail_call_sites. */
9769 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9770 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9772 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9773 not complete. But keep CALL_SITE for look ups via call_site_htab,
9774 both the initial caller containing the real return address PC and
9775 the final callee containing the current PC of a chain of tail
9776 calls do not need to have the tail call list complete. But any
9777 function candidate for a virtual tail call frame searched via
9778 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9779 determined unambiguously. */
9783 struct type *func_type = NULL;
9786 func_type = get_die_type (func_die, cu);
9787 if (func_type != NULL)
9789 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9791 /* Enlist this call site to the function. */
9792 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9793 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9796 complaint (&symfile_complaints,
9797 _("Cannot find function owning DW_TAG_GNU_call_site "
9798 "DIE 0x%x [in module %s]"),
9799 die->offset.sect_off, objfile->name);
9803 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9805 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9806 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9807 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9808 /* Keep NULL DWARF_BLOCK. */;
9809 else if (attr_form_is_block (attr))
9811 struct dwarf2_locexpr_baton *dlbaton;
9813 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9814 dlbaton->data = DW_BLOCK (attr)->data;
9815 dlbaton->size = DW_BLOCK (attr)->size;
9816 dlbaton->per_cu = cu->per_cu;
9818 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9820 else if (is_ref_attr (attr))
9822 struct dwarf2_cu *target_cu = cu;
9823 struct die_info *target_die;
9825 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9826 gdb_assert (target_cu->objfile == objfile);
9827 if (die_is_declaration (target_die, target_cu))
9829 const char *target_physname = NULL;
9830 struct attribute *target_attr;
9832 /* Prefer the mangled name; otherwise compute the demangled one. */
9833 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9834 if (target_attr == NULL)
9835 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9837 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9838 target_physname = DW_STRING (target_attr);
9840 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9841 if (target_physname == NULL)
9842 complaint (&symfile_complaints,
9843 _("DW_AT_GNU_call_site_target target DIE has invalid "
9844 "physname, for referencing DIE 0x%x [in module %s]"),
9845 die->offset.sect_off, objfile->name);
9847 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9853 /* DW_AT_entry_pc should be preferred. */
9854 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9855 complaint (&symfile_complaints,
9856 _("DW_AT_GNU_call_site_target target DIE has invalid "
9857 "low pc, for referencing DIE 0x%x [in module %s]"),
9858 die->offset.sect_off, objfile->name);
9860 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9864 complaint (&symfile_complaints,
9865 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9866 "block nor reference, for DIE 0x%x [in module %s]"),
9867 die->offset.sect_off, objfile->name);
9869 call_site->per_cu = cu->per_cu;
9871 for (child_die = die->child;
9872 child_die && child_die->tag;
9873 child_die = sibling_die (child_die))
9875 struct call_site_parameter *parameter;
9876 struct attribute *loc, *origin;
9878 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9880 /* Already printed the complaint above. */
9884 gdb_assert (call_site->parameter_count < nparams);
9885 parameter = &call_site->parameter[call_site->parameter_count];
9887 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9888 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9889 register is contained in DW_AT_GNU_call_site_value. */
9891 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9892 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9893 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9897 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9898 offset = dwarf2_get_ref_die_offset (origin);
9899 if (!offset_in_cu_p (&cu->header, offset))
9901 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9902 binding can be done only inside one CU. Such referenced DIE
9903 therefore cannot be even moved to DW_TAG_partial_unit. */
9904 complaint (&symfile_complaints,
9905 _("DW_AT_abstract_origin offset is not in CU for "
9906 "DW_TAG_GNU_call_site child DIE 0x%x "
9908 child_die->offset.sect_off, objfile->name);
9911 parameter->u.param_offset.cu_off = (offset.sect_off
9912 - cu->header.offset.sect_off);
9914 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9916 complaint (&symfile_complaints,
9917 _("No DW_FORM_block* DW_AT_location for "
9918 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9919 child_die->offset.sect_off, objfile->name);
9924 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9925 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9926 if (parameter->u.dwarf_reg != -1)
9927 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9928 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9929 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9930 ¶meter->u.fb_offset))
9931 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9934 complaint (&symfile_complaints,
9935 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9936 "for DW_FORM_block* DW_AT_location is supported for "
9937 "DW_TAG_GNU_call_site child DIE 0x%x "
9939 child_die->offset.sect_off, objfile->name);
9944 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9945 if (!attr_form_is_block (attr))
9947 complaint (&symfile_complaints,
9948 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9949 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9950 child_die->offset.sect_off, objfile->name);
9953 parameter->value = DW_BLOCK (attr)->data;
9954 parameter->value_size = DW_BLOCK (attr)->size;
9956 /* Parameters are not pre-cleared by memset above. */
9957 parameter->data_value = NULL;
9958 parameter->data_value_size = 0;
9959 call_site->parameter_count++;
9961 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9964 if (!attr_form_is_block (attr))
9965 complaint (&symfile_complaints,
9966 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9967 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9968 child_die->offset.sect_off, objfile->name);
9971 parameter->data_value = DW_BLOCK (attr)->data;
9972 parameter->data_value_size = DW_BLOCK (attr)->size;
9978 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9979 Return 1 if the attributes are present and valid, otherwise, return 0.
9980 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9983 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9984 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9985 struct partial_symtab *ranges_pst)
9987 struct objfile *objfile = cu->objfile;
9988 struct comp_unit_head *cu_header = &cu->header;
9989 bfd *obfd = objfile->obfd;
9990 unsigned int addr_size = cu_header->addr_size;
9991 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9992 /* Base address selection entry. */
10000 CORE_ADDR high = 0;
10001 CORE_ADDR baseaddr;
10003 found_base = cu->base_known;
10004 base = cu->base_address;
10006 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10007 if (offset >= dwarf2_per_objfile->ranges.size)
10009 complaint (&symfile_complaints,
10010 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10014 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10016 /* Read in the largest possible address. */
10017 marker = read_address (obfd, buffer, cu, &dummy);
10018 if ((marker & mask) == mask)
10020 /* If we found the largest possible address, then
10021 read the base address. */
10022 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10023 buffer += 2 * addr_size;
10024 offset += 2 * addr_size;
10030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10034 CORE_ADDR range_beginning, range_end;
10036 range_beginning = read_address (obfd, buffer, cu, &dummy);
10037 buffer += addr_size;
10038 range_end = read_address (obfd, buffer, cu, &dummy);
10039 buffer += addr_size;
10040 offset += 2 * addr_size;
10042 /* An end of list marker is a pair of zero addresses. */
10043 if (range_beginning == 0 && range_end == 0)
10044 /* Found the end of list entry. */
10047 /* Each base address selection entry is a pair of 2 values.
10048 The first is the largest possible address, the second is
10049 the base address. Check for a base address here. */
10050 if ((range_beginning & mask) == mask)
10052 /* If we found the largest possible address, then
10053 read the base address. */
10054 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10061 /* We have no valid base address for the ranges
10063 complaint (&symfile_complaints,
10064 _("Invalid .debug_ranges data (no base address)"));
10068 if (range_beginning > range_end)
10070 /* Inverted range entries are invalid. */
10071 complaint (&symfile_complaints,
10072 _("Invalid .debug_ranges data (inverted range)"));
10076 /* Empty range entries have no effect. */
10077 if (range_beginning == range_end)
10080 range_beginning += base;
10083 /* A not-uncommon case of bad debug info.
10084 Don't pollute the addrmap with bad data. */
10085 if (range_beginning + baseaddr == 0
10086 && !dwarf2_per_objfile->has_section_at_zero)
10088 complaint (&symfile_complaints,
10089 _(".debug_ranges entry has start address of zero"
10090 " [in module %s]"), objfile->name);
10094 if (ranges_pst != NULL)
10095 addrmap_set_empty (objfile->psymtabs_addrmap,
10096 range_beginning + baseaddr,
10097 range_end - 1 + baseaddr,
10100 /* FIXME: This is recording everything as a low-high
10101 segment of consecutive addresses. We should have a
10102 data structure for discontiguous block ranges
10106 low = range_beginning;
10112 if (range_beginning < low)
10113 low = range_beginning;
10114 if (range_end > high)
10120 /* If the first entry is an end-of-list marker, the range
10121 describes an empty scope, i.e. no instructions. */
10127 *high_return = high;
10131 /* Get low and high pc attributes from a die. Return 1 if the attributes
10132 are present and valid, otherwise, return 0. Return -1 if the range is
10133 discontinuous, i.e. derived from DW_AT_ranges information. */
10136 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10137 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10138 struct partial_symtab *pst)
10140 struct attribute *attr;
10141 struct attribute *attr_high;
10143 CORE_ADDR high = 0;
10146 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10149 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10152 low = DW_ADDR (attr);
10153 if (attr_high->form == DW_FORM_addr
10154 || attr_high->form == DW_FORM_GNU_addr_index)
10155 high = DW_ADDR (attr_high);
10157 high = low + DW_UNSND (attr_high);
10160 /* Found high w/o low attribute. */
10163 /* Found consecutive range of addresses. */
10168 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10171 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10172 We take advantage of the fact that DW_AT_ranges does not appear
10173 in DW_TAG_compile_unit of DWO files. */
10174 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10175 unsigned int ranges_offset = (DW_UNSND (attr)
10176 + (need_ranges_base
10180 /* Value of the DW_AT_ranges attribute is the offset in the
10181 .debug_ranges section. */
10182 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10184 /* Found discontinuous range of addresses. */
10189 /* read_partial_die has also the strict LOW < HIGH requirement. */
10193 /* When using the GNU linker, .gnu.linkonce. sections are used to
10194 eliminate duplicate copies of functions and vtables and such.
10195 The linker will arbitrarily choose one and discard the others.
10196 The AT_*_pc values for such functions refer to local labels in
10197 these sections. If the section from that file was discarded, the
10198 labels are not in the output, so the relocs get a value of 0.
10199 If this is a discarded function, mark the pc bounds as invalid,
10200 so that GDB will ignore it. */
10201 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10210 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10211 its low and high PC addresses. Do nothing if these addresses could not
10212 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10213 and HIGHPC to the high address if greater than HIGHPC. */
10216 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10218 struct dwarf2_cu *cu)
10220 CORE_ADDR low, high;
10221 struct die_info *child = die->child;
10223 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10225 *lowpc = min (*lowpc, low);
10226 *highpc = max (*highpc, high);
10229 /* If the language does not allow nested subprograms (either inside
10230 subprograms or lexical blocks), we're done. */
10231 if (cu->language != language_ada)
10234 /* Check all the children of the given DIE. If it contains nested
10235 subprograms, then check their pc bounds. Likewise, we need to
10236 check lexical blocks as well, as they may also contain subprogram
10238 while (child && child->tag)
10240 if (child->tag == DW_TAG_subprogram
10241 || child->tag == DW_TAG_lexical_block)
10242 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10243 child = sibling_die (child);
10247 /* Get the low and high pc's represented by the scope DIE, and store
10248 them in *LOWPC and *HIGHPC. If the correct values can't be
10249 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10252 get_scope_pc_bounds (struct die_info *die,
10253 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10254 struct dwarf2_cu *cu)
10256 CORE_ADDR best_low = (CORE_ADDR) -1;
10257 CORE_ADDR best_high = (CORE_ADDR) 0;
10258 CORE_ADDR current_low, current_high;
10260 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
10262 best_low = current_low;
10263 best_high = current_high;
10267 struct die_info *child = die->child;
10269 while (child && child->tag)
10271 switch (child->tag) {
10272 case DW_TAG_subprogram:
10273 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10275 case DW_TAG_namespace:
10276 case DW_TAG_module:
10277 /* FIXME: carlton/2004-01-16: Should we do this for
10278 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10279 that current GCC's always emit the DIEs corresponding
10280 to definitions of methods of classes as children of a
10281 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10282 the DIEs giving the declarations, which could be
10283 anywhere). But I don't see any reason why the
10284 standards says that they have to be there. */
10285 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
10287 if (current_low != ((CORE_ADDR) -1))
10289 best_low = min (best_low, current_low);
10290 best_high = max (best_high, current_high);
10298 child = sibling_die (child);
10303 *highpc = best_high;
10306 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10310 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10311 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10313 struct objfile *objfile = cu->objfile;
10314 struct attribute *attr;
10315 struct attribute *attr_high;
10317 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10320 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10323 CORE_ADDR low = DW_ADDR (attr);
10325 if (attr_high->form == DW_FORM_addr
10326 || attr_high->form == DW_FORM_GNU_addr_index)
10327 high = DW_ADDR (attr_high);
10329 high = low + DW_UNSND (attr_high);
10331 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10335 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10338 bfd *obfd = objfile->obfd;
10339 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10340 We take advantage of the fact that DW_AT_ranges does not appear
10341 in DW_TAG_compile_unit of DWO files. */
10342 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10344 /* The value of the DW_AT_ranges attribute is the offset of the
10345 address range list in the .debug_ranges section. */
10346 unsigned long offset = (DW_UNSND (attr)
10347 + (need_ranges_base ? cu->ranges_base : 0));
10348 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10350 /* For some target architectures, but not others, the
10351 read_address function sign-extends the addresses it returns.
10352 To recognize base address selection entries, we need a
10354 unsigned int addr_size = cu->header.addr_size;
10355 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10357 /* The base address, to which the next pair is relative. Note
10358 that this 'base' is a DWARF concept: most entries in a range
10359 list are relative, to reduce the number of relocs against the
10360 debugging information. This is separate from this function's
10361 'baseaddr' argument, which GDB uses to relocate debugging
10362 information from a shared library based on the address at
10363 which the library was loaded. */
10364 CORE_ADDR base = cu->base_address;
10365 int base_known = cu->base_known;
10367 gdb_assert (dwarf2_per_objfile->ranges.readin);
10368 if (offset >= dwarf2_per_objfile->ranges.size)
10370 complaint (&symfile_complaints,
10371 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10378 unsigned int bytes_read;
10379 CORE_ADDR start, end;
10381 start = read_address (obfd, buffer, cu, &bytes_read);
10382 buffer += bytes_read;
10383 end = read_address (obfd, buffer, cu, &bytes_read);
10384 buffer += bytes_read;
10386 /* Did we find the end of the range list? */
10387 if (start == 0 && end == 0)
10390 /* Did we find a base address selection entry? */
10391 else if ((start & base_select_mask) == base_select_mask)
10397 /* We found an ordinary address range. */
10402 complaint (&symfile_complaints,
10403 _("Invalid .debug_ranges data "
10404 "(no base address)"));
10410 /* Inverted range entries are invalid. */
10411 complaint (&symfile_complaints,
10412 _("Invalid .debug_ranges data "
10413 "(inverted range)"));
10417 /* Empty range entries have no effect. */
10421 start += base + baseaddr;
10422 end += base + baseaddr;
10424 /* A not-uncommon case of bad debug info.
10425 Don't pollute the addrmap with bad data. */
10426 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10428 complaint (&symfile_complaints,
10429 _(".debug_ranges entry has start address of zero"
10430 " [in module %s]"), objfile->name);
10434 record_block_range (block, start, end - 1);
10440 /* Check whether the producer field indicates either of GCC < 4.6, or the
10441 Intel C/C++ compiler, and cache the result in CU. */
10444 check_producer (struct dwarf2_cu *cu)
10447 int major, minor, release;
10449 if (cu->producer == NULL)
10451 /* For unknown compilers expect their behavior is DWARF version
10454 GCC started to support .debug_types sections by -gdwarf-4 since
10455 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10456 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10457 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10458 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10460 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10462 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10464 cs = &cu->producer[strlen ("GNU ")];
10465 while (*cs && !isdigit (*cs))
10467 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10469 /* Not recognized as GCC. */
10473 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10474 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10477 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10478 cu->producer_is_icc = 1;
10481 /* For other non-GCC compilers, expect their behavior is DWARF version
10485 cu->checked_producer = 1;
10488 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10489 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10490 during 4.6.0 experimental. */
10493 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10495 if (!cu->checked_producer)
10496 check_producer (cu);
10498 return cu->producer_is_gxx_lt_4_6;
10501 /* Return the default accessibility type if it is not overriden by
10502 DW_AT_accessibility. */
10504 static enum dwarf_access_attribute
10505 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10507 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10509 /* The default DWARF 2 accessibility for members is public, the default
10510 accessibility for inheritance is private. */
10512 if (die->tag != DW_TAG_inheritance)
10513 return DW_ACCESS_public;
10515 return DW_ACCESS_private;
10519 /* DWARF 3+ defines the default accessibility a different way. The same
10520 rules apply now for DW_TAG_inheritance as for the members and it only
10521 depends on the container kind. */
10523 if (die->parent->tag == DW_TAG_class_type)
10524 return DW_ACCESS_private;
10526 return DW_ACCESS_public;
10530 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10531 offset. If the attribute was not found return 0, otherwise return
10532 1. If it was found but could not properly be handled, set *OFFSET
10536 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10539 struct attribute *attr;
10541 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10546 /* Note that we do not check for a section offset first here.
10547 This is because DW_AT_data_member_location is new in DWARF 4,
10548 so if we see it, we can assume that a constant form is really
10549 a constant and not a section offset. */
10550 if (attr_form_is_constant (attr))
10551 *offset = dwarf2_get_attr_constant_value (attr, 0);
10552 else if (attr_form_is_section_offset (attr))
10553 dwarf2_complex_location_expr_complaint ();
10554 else if (attr_form_is_block (attr))
10555 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10557 dwarf2_complex_location_expr_complaint ();
10565 /* Add an aggregate field to the field list. */
10568 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10569 struct dwarf2_cu *cu)
10571 struct objfile *objfile = cu->objfile;
10572 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10573 struct nextfield *new_field;
10574 struct attribute *attr;
10576 const char *fieldname = "";
10578 /* Allocate a new field list entry and link it in. */
10579 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10580 make_cleanup (xfree, new_field);
10581 memset (new_field, 0, sizeof (struct nextfield));
10583 if (die->tag == DW_TAG_inheritance)
10585 new_field->next = fip->baseclasses;
10586 fip->baseclasses = new_field;
10590 new_field->next = fip->fields;
10591 fip->fields = new_field;
10595 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10597 new_field->accessibility = DW_UNSND (attr);
10599 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10600 if (new_field->accessibility != DW_ACCESS_public)
10601 fip->non_public_fields = 1;
10603 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10605 new_field->virtuality = DW_UNSND (attr);
10607 new_field->virtuality = DW_VIRTUALITY_none;
10609 fp = &new_field->field;
10611 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10615 /* Data member other than a C++ static data member. */
10617 /* Get type of field. */
10618 fp->type = die_type (die, cu);
10620 SET_FIELD_BITPOS (*fp, 0);
10622 /* Get bit size of field (zero if none). */
10623 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10626 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10630 FIELD_BITSIZE (*fp) = 0;
10633 /* Get bit offset of field. */
10634 if (handle_data_member_location (die, cu, &offset))
10635 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10636 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10639 if (gdbarch_bits_big_endian (gdbarch))
10641 /* For big endian bits, the DW_AT_bit_offset gives the
10642 additional bit offset from the MSB of the containing
10643 anonymous object to the MSB of the field. We don't
10644 have to do anything special since we don't need to
10645 know the size of the anonymous object. */
10646 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10650 /* For little endian bits, compute the bit offset to the
10651 MSB of the anonymous object, subtract off the number of
10652 bits from the MSB of the field to the MSB of the
10653 object, and then subtract off the number of bits of
10654 the field itself. The result is the bit offset of
10655 the LSB of the field. */
10656 int anonymous_size;
10657 int bit_offset = DW_UNSND (attr);
10659 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10662 /* The size of the anonymous object containing
10663 the bit field is explicit, so use the
10664 indicated size (in bytes). */
10665 anonymous_size = DW_UNSND (attr);
10669 /* The size of the anonymous object containing
10670 the bit field must be inferred from the type
10671 attribute of the data member containing the
10673 anonymous_size = TYPE_LENGTH (fp->type);
10675 SET_FIELD_BITPOS (*fp,
10676 (FIELD_BITPOS (*fp)
10677 + anonymous_size * bits_per_byte
10678 - bit_offset - FIELD_BITSIZE (*fp)));
10682 /* Get name of field. */
10683 fieldname = dwarf2_name (die, cu);
10684 if (fieldname == NULL)
10687 /* The name is already allocated along with this objfile, so we don't
10688 need to duplicate it for the type. */
10689 fp->name = fieldname;
10691 /* Change accessibility for artificial fields (e.g. virtual table
10692 pointer or virtual base class pointer) to private. */
10693 if (dwarf2_attr (die, DW_AT_artificial, cu))
10695 FIELD_ARTIFICIAL (*fp) = 1;
10696 new_field->accessibility = DW_ACCESS_private;
10697 fip->non_public_fields = 1;
10700 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10702 /* C++ static member. */
10704 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10705 is a declaration, but all versions of G++ as of this writing
10706 (so through at least 3.2.1) incorrectly generate
10707 DW_TAG_variable tags. */
10709 const char *physname;
10711 /* Get name of field. */
10712 fieldname = dwarf2_name (die, cu);
10713 if (fieldname == NULL)
10716 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10718 /* Only create a symbol if this is an external value.
10719 new_symbol checks this and puts the value in the global symbol
10720 table, which we want. If it is not external, new_symbol
10721 will try to put the value in cu->list_in_scope which is wrong. */
10722 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10724 /* A static const member, not much different than an enum as far as
10725 we're concerned, except that we can support more types. */
10726 new_symbol (die, NULL, cu);
10729 /* Get physical name. */
10730 physname = dwarf2_physname (fieldname, die, cu);
10732 /* The name is already allocated along with this objfile, so we don't
10733 need to duplicate it for the type. */
10734 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10735 FIELD_TYPE (*fp) = die_type (die, cu);
10736 FIELD_NAME (*fp) = fieldname;
10738 else if (die->tag == DW_TAG_inheritance)
10742 /* C++ base class field. */
10743 if (handle_data_member_location (die, cu, &offset))
10744 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10745 FIELD_BITSIZE (*fp) = 0;
10746 FIELD_TYPE (*fp) = die_type (die, cu);
10747 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10748 fip->nbaseclasses++;
10752 /* Add a typedef defined in the scope of the FIP's class. */
10755 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10756 struct dwarf2_cu *cu)
10758 struct objfile *objfile = cu->objfile;
10759 struct typedef_field_list *new_field;
10760 struct attribute *attr;
10761 struct typedef_field *fp;
10762 char *fieldname = "";
10764 /* Allocate a new field list entry and link it in. */
10765 new_field = xzalloc (sizeof (*new_field));
10766 make_cleanup (xfree, new_field);
10768 gdb_assert (die->tag == DW_TAG_typedef);
10770 fp = &new_field->field;
10772 /* Get name of field. */
10773 fp->name = dwarf2_name (die, cu);
10774 if (fp->name == NULL)
10777 fp->type = read_type_die (die, cu);
10779 new_field->next = fip->typedef_field_list;
10780 fip->typedef_field_list = new_field;
10781 fip->typedef_field_list_count++;
10784 /* Create the vector of fields, and attach it to the type. */
10787 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10788 struct dwarf2_cu *cu)
10790 int nfields = fip->nfields;
10792 /* Record the field count, allocate space for the array of fields,
10793 and create blank accessibility bitfields if necessary. */
10794 TYPE_NFIELDS (type) = nfields;
10795 TYPE_FIELDS (type) = (struct field *)
10796 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10797 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10799 if (fip->non_public_fields && cu->language != language_ada)
10801 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10803 TYPE_FIELD_PRIVATE_BITS (type) =
10804 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10805 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10807 TYPE_FIELD_PROTECTED_BITS (type) =
10808 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10809 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10811 TYPE_FIELD_IGNORE_BITS (type) =
10812 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10813 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10816 /* If the type has baseclasses, allocate and clear a bit vector for
10817 TYPE_FIELD_VIRTUAL_BITS. */
10818 if (fip->nbaseclasses && cu->language != language_ada)
10820 int num_bytes = B_BYTES (fip->nbaseclasses);
10821 unsigned char *pointer;
10823 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10824 pointer = TYPE_ALLOC (type, num_bytes);
10825 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10826 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10827 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10830 /* Copy the saved-up fields into the field vector. Start from the head of
10831 the list, adding to the tail of the field array, so that they end up in
10832 the same order in the array in which they were added to the list. */
10833 while (nfields-- > 0)
10835 struct nextfield *fieldp;
10839 fieldp = fip->fields;
10840 fip->fields = fieldp->next;
10844 fieldp = fip->baseclasses;
10845 fip->baseclasses = fieldp->next;
10848 TYPE_FIELD (type, nfields) = fieldp->field;
10849 switch (fieldp->accessibility)
10851 case DW_ACCESS_private:
10852 if (cu->language != language_ada)
10853 SET_TYPE_FIELD_PRIVATE (type, nfields);
10856 case DW_ACCESS_protected:
10857 if (cu->language != language_ada)
10858 SET_TYPE_FIELD_PROTECTED (type, nfields);
10861 case DW_ACCESS_public:
10865 /* Unknown accessibility. Complain and treat it as public. */
10867 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10868 fieldp->accessibility);
10872 if (nfields < fip->nbaseclasses)
10874 switch (fieldp->virtuality)
10876 case DW_VIRTUALITY_virtual:
10877 case DW_VIRTUALITY_pure_virtual:
10878 if (cu->language == language_ada)
10879 error (_("unexpected virtuality in component of Ada type"));
10880 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10887 /* Return true if this member function is a constructor, false
10891 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10893 const char *fieldname;
10894 const char *typename;
10897 if (die->parent == NULL)
10900 if (die->parent->tag != DW_TAG_structure_type
10901 && die->parent->tag != DW_TAG_union_type
10902 && die->parent->tag != DW_TAG_class_type)
10905 fieldname = dwarf2_name (die, cu);
10906 typename = dwarf2_name (die->parent, cu);
10907 if (fieldname == NULL || typename == NULL)
10910 len = strlen (fieldname);
10911 return (strncmp (fieldname, typename, len) == 0
10912 && (typename[len] == '\0' || typename[len] == '<'));
10915 /* Add a member function to the proper fieldlist. */
10918 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10919 struct type *type, struct dwarf2_cu *cu)
10921 struct objfile *objfile = cu->objfile;
10922 struct attribute *attr;
10923 struct fnfieldlist *flp;
10925 struct fn_field *fnp;
10926 const char *fieldname;
10927 struct nextfnfield *new_fnfield;
10928 struct type *this_type;
10929 enum dwarf_access_attribute accessibility;
10931 if (cu->language == language_ada)
10932 error (_("unexpected member function in Ada type"));
10934 /* Get name of member function. */
10935 fieldname = dwarf2_name (die, cu);
10936 if (fieldname == NULL)
10939 /* Look up member function name in fieldlist. */
10940 for (i = 0; i < fip->nfnfields; i++)
10942 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10946 /* Create new list element if necessary. */
10947 if (i < fip->nfnfields)
10948 flp = &fip->fnfieldlists[i];
10951 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10953 fip->fnfieldlists = (struct fnfieldlist *)
10954 xrealloc (fip->fnfieldlists,
10955 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10956 * sizeof (struct fnfieldlist));
10957 if (fip->nfnfields == 0)
10958 make_cleanup (free_current_contents, &fip->fnfieldlists);
10960 flp = &fip->fnfieldlists[fip->nfnfields];
10961 flp->name = fieldname;
10964 i = fip->nfnfields++;
10967 /* Create a new member function field and chain it to the field list
10969 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10970 make_cleanup (xfree, new_fnfield);
10971 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10972 new_fnfield->next = flp->head;
10973 flp->head = new_fnfield;
10976 /* Fill in the member function field info. */
10977 fnp = &new_fnfield->fnfield;
10979 /* Delay processing of the physname until later. */
10980 if (cu->language == language_cplus || cu->language == language_java)
10982 add_to_method_list (type, i, flp->length - 1, fieldname,
10987 const char *physname = dwarf2_physname (fieldname, die, cu);
10988 fnp->physname = physname ? physname : "";
10991 fnp->type = alloc_type (objfile);
10992 this_type = read_type_die (die, cu);
10993 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10995 int nparams = TYPE_NFIELDS (this_type);
10997 /* TYPE is the domain of this method, and THIS_TYPE is the type
10998 of the method itself (TYPE_CODE_METHOD). */
10999 smash_to_method_type (fnp->type, type,
11000 TYPE_TARGET_TYPE (this_type),
11001 TYPE_FIELDS (this_type),
11002 TYPE_NFIELDS (this_type),
11003 TYPE_VARARGS (this_type));
11005 /* Handle static member functions.
11006 Dwarf2 has no clean way to discern C++ static and non-static
11007 member functions. G++ helps GDB by marking the first
11008 parameter for non-static member functions (which is the this
11009 pointer) as artificial. We obtain this information from
11010 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11011 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11012 fnp->voffset = VOFFSET_STATIC;
11015 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11016 dwarf2_full_name (fieldname, die, cu));
11018 /* Get fcontext from DW_AT_containing_type if present. */
11019 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11020 fnp->fcontext = die_containing_type (die, cu);
11022 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11023 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11025 /* Get accessibility. */
11026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11028 accessibility = DW_UNSND (attr);
11030 accessibility = dwarf2_default_access_attribute (die, cu);
11031 switch (accessibility)
11033 case DW_ACCESS_private:
11034 fnp->is_private = 1;
11036 case DW_ACCESS_protected:
11037 fnp->is_protected = 1;
11041 /* Check for artificial methods. */
11042 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11043 if (attr && DW_UNSND (attr) != 0)
11044 fnp->is_artificial = 1;
11046 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11048 /* Get index in virtual function table if it is a virtual member
11049 function. For older versions of GCC, this is an offset in the
11050 appropriate virtual table, as specified by DW_AT_containing_type.
11051 For everyone else, it is an expression to be evaluated relative
11052 to the object address. */
11054 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11057 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11059 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11061 /* Old-style GCC. */
11062 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11064 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11065 || (DW_BLOCK (attr)->size > 1
11066 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11067 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11069 struct dwarf_block blk;
11072 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11074 blk.size = DW_BLOCK (attr)->size - offset;
11075 blk.data = DW_BLOCK (attr)->data + offset;
11076 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11077 if ((fnp->voffset % cu->header.addr_size) != 0)
11078 dwarf2_complex_location_expr_complaint ();
11080 fnp->voffset /= cu->header.addr_size;
11084 dwarf2_complex_location_expr_complaint ();
11086 if (!fnp->fcontext)
11087 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11089 else if (attr_form_is_section_offset (attr))
11091 dwarf2_complex_location_expr_complaint ();
11095 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11101 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11102 if (attr && DW_UNSND (attr))
11104 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11105 complaint (&symfile_complaints,
11106 _("Member function \"%s\" (offset %d) is virtual "
11107 "but the vtable offset is not specified"),
11108 fieldname, die->offset.sect_off);
11109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11110 TYPE_CPLUS_DYNAMIC (type) = 1;
11115 /* Create the vector of member function fields, and attach it to the type. */
11118 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11119 struct dwarf2_cu *cu)
11121 struct fnfieldlist *flp;
11124 if (cu->language == language_ada)
11125 error (_("unexpected member functions in Ada type"));
11127 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11128 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11129 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11131 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11133 struct nextfnfield *nfp = flp->head;
11134 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11137 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11138 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11139 fn_flp->fn_fields = (struct fn_field *)
11140 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11141 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11142 fn_flp->fn_fields[k] = nfp->fnfield;
11145 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11148 /* Returns non-zero if NAME is the name of a vtable member in CU's
11149 language, zero otherwise. */
11151 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11153 static const char vptr[] = "_vptr";
11154 static const char vtable[] = "vtable";
11156 /* Look for the C++ and Java forms of the vtable. */
11157 if ((cu->language == language_java
11158 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11159 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11160 && is_cplus_marker (name[sizeof (vptr) - 1])))
11166 /* GCC outputs unnamed structures that are really pointers to member
11167 functions, with the ABI-specified layout. If TYPE describes
11168 such a structure, smash it into a member function type.
11170 GCC shouldn't do this; it should just output pointer to member DIEs.
11171 This is GCC PR debug/28767. */
11174 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11176 struct type *pfn_type, *domain_type, *new_type;
11178 /* Check for a structure with no name and two children. */
11179 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11182 /* Check for __pfn and __delta members. */
11183 if (TYPE_FIELD_NAME (type, 0) == NULL
11184 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11185 || TYPE_FIELD_NAME (type, 1) == NULL
11186 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11189 /* Find the type of the method. */
11190 pfn_type = TYPE_FIELD_TYPE (type, 0);
11191 if (pfn_type == NULL
11192 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11193 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11196 /* Look for the "this" argument. */
11197 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11198 if (TYPE_NFIELDS (pfn_type) == 0
11199 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11200 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11203 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11204 new_type = alloc_type (objfile);
11205 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11206 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11207 TYPE_VARARGS (pfn_type));
11208 smash_to_methodptr_type (type, new_type);
11211 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11215 producer_is_icc (struct dwarf2_cu *cu)
11217 if (!cu->checked_producer)
11218 check_producer (cu);
11220 return cu->producer_is_icc;
11223 /* Called when we find the DIE that starts a structure or union scope
11224 (definition) to create a type for the structure or union. Fill in
11225 the type's name and general properties; the members will not be
11226 processed until process_structure_type.
11228 NOTE: we need to call these functions regardless of whether or not the
11229 DIE has a DW_AT_name attribute, since it might be an anonymous
11230 structure or union. This gets the type entered into our set of
11231 user defined types.
11233 However, if the structure is incomplete (an opaque struct/union)
11234 then suppress creating a symbol table entry for it since gdb only
11235 wants to find the one with the complete definition. Note that if
11236 it is complete, we just call new_symbol, which does it's own
11237 checking about whether the struct/union is anonymous or not (and
11238 suppresses creating a symbol table entry itself). */
11240 static struct type *
11241 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11243 struct objfile *objfile = cu->objfile;
11245 struct attribute *attr;
11248 /* If the definition of this type lives in .debug_types, read that type.
11249 Don't follow DW_AT_specification though, that will take us back up
11250 the chain and we want to go down. */
11251 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11254 struct dwarf2_cu *type_cu = cu;
11255 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11257 /* We could just recurse on read_structure_type, but we need to call
11258 get_die_type to ensure only one type for this DIE is created.
11259 This is important, for example, because for c++ classes we need
11260 TYPE_NAME set which is only done by new_symbol. Blech. */
11261 type = read_type_die (type_die, type_cu);
11263 /* TYPE_CU may not be the same as CU.
11264 Ensure TYPE is recorded in CU's type_hash table. */
11265 return set_die_type (die, type, cu);
11268 type = alloc_type (objfile);
11269 INIT_CPLUS_SPECIFIC (type);
11271 name = dwarf2_name (die, cu);
11274 if (cu->language == language_cplus
11275 || cu->language == language_java)
11277 const char *full_name = dwarf2_full_name (name, die, cu);
11279 /* dwarf2_full_name might have already finished building the DIE's
11280 type. If so, there is no need to continue. */
11281 if (get_die_type (die, cu) != NULL)
11282 return get_die_type (die, cu);
11284 TYPE_TAG_NAME (type) = full_name;
11285 if (die->tag == DW_TAG_structure_type
11286 || die->tag == DW_TAG_class_type)
11287 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11291 /* The name is already allocated along with this objfile, so
11292 we don't need to duplicate it for the type. */
11293 TYPE_TAG_NAME (type) = name;
11294 if (die->tag == DW_TAG_class_type)
11295 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11299 if (die->tag == DW_TAG_structure_type)
11301 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11303 else if (die->tag == DW_TAG_union_type)
11305 TYPE_CODE (type) = TYPE_CODE_UNION;
11309 TYPE_CODE (type) = TYPE_CODE_CLASS;
11312 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11313 TYPE_DECLARED_CLASS (type) = 1;
11315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11318 TYPE_LENGTH (type) = DW_UNSND (attr);
11322 TYPE_LENGTH (type) = 0;
11325 if (producer_is_icc (cu))
11327 /* ICC does not output the required DW_AT_declaration
11328 on incomplete types, but gives them a size of zero. */
11331 TYPE_STUB_SUPPORTED (type) = 1;
11333 if (die_is_declaration (die, cu))
11334 TYPE_STUB (type) = 1;
11335 else if (attr == NULL && die->child == NULL
11336 && producer_is_realview (cu->producer))
11337 /* RealView does not output the required DW_AT_declaration
11338 on incomplete types. */
11339 TYPE_STUB (type) = 1;
11341 /* We need to add the type field to the die immediately so we don't
11342 infinitely recurse when dealing with pointers to the structure
11343 type within the structure itself. */
11344 set_die_type (die, type, cu);
11346 /* set_die_type should be already done. */
11347 set_descriptive_type (type, die, cu);
11352 /* Finish creating a structure or union type, including filling in
11353 its members and creating a symbol for it. */
11356 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11358 struct objfile *objfile = cu->objfile;
11359 struct die_info *child_die = die->child;
11362 type = get_die_type (die, cu);
11364 type = read_structure_type (die, cu);
11366 if (die->child != NULL && ! die_is_declaration (die, cu))
11368 struct field_info fi;
11369 struct die_info *child_die;
11370 VEC (symbolp) *template_args = NULL;
11371 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11373 memset (&fi, 0, sizeof (struct field_info));
11375 child_die = die->child;
11377 while (child_die && child_die->tag)
11379 if (child_die->tag == DW_TAG_member
11380 || child_die->tag == DW_TAG_variable)
11382 /* NOTE: carlton/2002-11-05: A C++ static data member
11383 should be a DW_TAG_member that is a declaration, but
11384 all versions of G++ as of this writing (so through at
11385 least 3.2.1) incorrectly generate DW_TAG_variable
11386 tags for them instead. */
11387 dwarf2_add_field (&fi, child_die, cu);
11389 else if (child_die->tag == DW_TAG_subprogram)
11391 /* C++ member function. */
11392 dwarf2_add_member_fn (&fi, child_die, type, cu);
11394 else if (child_die->tag == DW_TAG_inheritance)
11396 /* C++ base class field. */
11397 dwarf2_add_field (&fi, child_die, cu);
11399 else if (child_die->tag == DW_TAG_typedef)
11400 dwarf2_add_typedef (&fi, child_die, cu);
11401 else if (child_die->tag == DW_TAG_template_type_param
11402 || child_die->tag == DW_TAG_template_value_param)
11404 struct symbol *arg = new_symbol (child_die, NULL, cu);
11407 VEC_safe_push (symbolp, template_args, arg);
11410 child_die = sibling_die (child_die);
11413 /* Attach template arguments to type. */
11414 if (! VEC_empty (symbolp, template_args))
11416 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11417 TYPE_N_TEMPLATE_ARGUMENTS (type)
11418 = VEC_length (symbolp, template_args);
11419 TYPE_TEMPLATE_ARGUMENTS (type)
11420 = obstack_alloc (&objfile->objfile_obstack,
11421 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11422 * sizeof (struct symbol *)));
11423 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11424 VEC_address (symbolp, template_args),
11425 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11426 * sizeof (struct symbol *)));
11427 VEC_free (symbolp, template_args);
11430 /* Attach fields and member functions to the type. */
11432 dwarf2_attach_fields_to_type (&fi, type, cu);
11435 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11437 /* Get the type which refers to the base class (possibly this
11438 class itself) which contains the vtable pointer for the current
11439 class from the DW_AT_containing_type attribute. This use of
11440 DW_AT_containing_type is a GNU extension. */
11442 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11444 struct type *t = die_containing_type (die, cu);
11446 TYPE_VPTR_BASETYPE (type) = t;
11451 /* Our own class provides vtbl ptr. */
11452 for (i = TYPE_NFIELDS (t) - 1;
11453 i >= TYPE_N_BASECLASSES (t);
11456 const char *fieldname = TYPE_FIELD_NAME (t, i);
11458 if (is_vtable_name (fieldname, cu))
11460 TYPE_VPTR_FIELDNO (type) = i;
11465 /* Complain if virtual function table field not found. */
11466 if (i < TYPE_N_BASECLASSES (t))
11467 complaint (&symfile_complaints,
11468 _("virtual function table pointer "
11469 "not found when defining class '%s'"),
11470 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11475 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11478 else if (cu->producer
11479 && strncmp (cu->producer,
11480 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11482 /* The IBM XLC compiler does not provide direct indication
11483 of the containing type, but the vtable pointer is
11484 always named __vfp. */
11488 for (i = TYPE_NFIELDS (type) - 1;
11489 i >= TYPE_N_BASECLASSES (type);
11492 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11494 TYPE_VPTR_FIELDNO (type) = i;
11495 TYPE_VPTR_BASETYPE (type) = type;
11502 /* Copy fi.typedef_field_list linked list elements content into the
11503 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11504 if (fi.typedef_field_list)
11506 int i = fi.typedef_field_list_count;
11508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11509 TYPE_TYPEDEF_FIELD_ARRAY (type)
11510 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11511 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11513 /* Reverse the list order to keep the debug info elements order. */
11516 struct typedef_field *dest, *src;
11518 dest = &TYPE_TYPEDEF_FIELD (type, i);
11519 src = &fi.typedef_field_list->field;
11520 fi.typedef_field_list = fi.typedef_field_list->next;
11525 do_cleanups (back_to);
11527 if (HAVE_CPLUS_STRUCT (type))
11528 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11531 quirk_gcc_member_function_pointer (type, objfile);
11533 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11534 snapshots) has been known to create a die giving a declaration
11535 for a class that has, as a child, a die giving a definition for a
11536 nested class. So we have to process our children even if the
11537 current die is a declaration. Normally, of course, a declaration
11538 won't have any children at all. */
11540 while (child_die != NULL && child_die->tag)
11542 if (child_die->tag == DW_TAG_member
11543 || child_die->tag == DW_TAG_variable
11544 || child_die->tag == DW_TAG_inheritance
11545 || child_die->tag == DW_TAG_template_value_param
11546 || child_die->tag == DW_TAG_template_type_param)
11551 process_die (child_die, cu);
11553 child_die = sibling_die (child_die);
11556 /* Do not consider external references. According to the DWARF standard,
11557 these DIEs are identified by the fact that they have no byte_size
11558 attribute, and a declaration attribute. */
11559 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11560 || !die_is_declaration (die, cu))
11561 new_symbol (die, type, cu);
11564 /* Given a DW_AT_enumeration_type die, set its type. We do not
11565 complete the type's fields yet, or create any symbols. */
11567 static struct type *
11568 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11570 struct objfile *objfile = cu->objfile;
11572 struct attribute *attr;
11575 /* If the definition of this type lives in .debug_types, read that type.
11576 Don't follow DW_AT_specification though, that will take us back up
11577 the chain and we want to go down. */
11578 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11581 struct dwarf2_cu *type_cu = cu;
11582 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11584 type = read_type_die (type_die, type_cu);
11586 /* TYPE_CU may not be the same as CU.
11587 Ensure TYPE is recorded in CU's type_hash table. */
11588 return set_die_type (die, type, cu);
11591 type = alloc_type (objfile);
11593 TYPE_CODE (type) = TYPE_CODE_ENUM;
11594 name = dwarf2_full_name (NULL, die, cu);
11596 TYPE_TAG_NAME (type) = name;
11598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11601 TYPE_LENGTH (type) = DW_UNSND (attr);
11605 TYPE_LENGTH (type) = 0;
11608 /* The enumeration DIE can be incomplete. In Ada, any type can be
11609 declared as private in the package spec, and then defined only
11610 inside the package body. Such types are known as Taft Amendment
11611 Types. When another package uses such a type, an incomplete DIE
11612 may be generated by the compiler. */
11613 if (die_is_declaration (die, cu))
11614 TYPE_STUB (type) = 1;
11616 return set_die_type (die, type, cu);
11619 /* Given a pointer to a die which begins an enumeration, process all
11620 the dies that define the members of the enumeration, and create the
11621 symbol for the enumeration type.
11623 NOTE: We reverse the order of the element list. */
11626 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11628 struct type *this_type;
11630 this_type = get_die_type (die, cu);
11631 if (this_type == NULL)
11632 this_type = read_enumeration_type (die, cu);
11634 if (die->child != NULL)
11636 struct die_info *child_die;
11637 struct symbol *sym;
11638 struct field *fields = NULL;
11639 int num_fields = 0;
11640 int unsigned_enum = 1;
11645 child_die = die->child;
11646 while (child_die && child_die->tag)
11648 if (child_die->tag != DW_TAG_enumerator)
11650 process_die (child_die, cu);
11654 name = dwarf2_name (child_die, cu);
11657 sym = new_symbol (child_die, this_type, cu);
11658 if (SYMBOL_VALUE (sym) < 0)
11663 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11666 mask |= SYMBOL_VALUE (sym);
11668 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11670 fields = (struct field *)
11672 (num_fields + DW_FIELD_ALLOC_CHUNK)
11673 * sizeof (struct field));
11676 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11677 FIELD_TYPE (fields[num_fields]) = NULL;
11678 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11679 FIELD_BITSIZE (fields[num_fields]) = 0;
11685 child_die = sibling_die (child_die);
11690 TYPE_NFIELDS (this_type) = num_fields;
11691 TYPE_FIELDS (this_type) = (struct field *)
11692 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11693 memcpy (TYPE_FIELDS (this_type), fields,
11694 sizeof (struct field) * num_fields);
11698 TYPE_UNSIGNED (this_type) = 1;
11700 TYPE_FLAG_ENUM (this_type) = 1;
11703 /* If we are reading an enum from a .debug_types unit, and the enum
11704 is a declaration, and the enum is not the signatured type in the
11705 unit, then we do not want to add a symbol for it. Adding a
11706 symbol would in some cases obscure the true definition of the
11707 enum, giving users an incomplete type when the definition is
11708 actually available. Note that we do not want to do this for all
11709 enums which are just declarations, because C++0x allows forward
11710 enum declarations. */
11711 if (cu->per_cu->is_debug_types
11712 && die_is_declaration (die, cu))
11714 struct signatured_type *sig_type;
11717 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11718 cu->per_cu->section,
11719 cu->per_cu->offset);
11720 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11721 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11725 new_symbol (die, this_type, cu);
11728 /* Extract all information from a DW_TAG_array_type DIE and put it in
11729 the DIE's type field. For now, this only handles one dimensional
11732 static struct type *
11733 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11735 struct objfile *objfile = cu->objfile;
11736 struct die_info *child_die;
11738 struct type *element_type, *range_type, *index_type;
11739 struct type **range_types = NULL;
11740 struct attribute *attr;
11742 struct cleanup *back_to;
11745 element_type = die_type (die, cu);
11747 /* The die_type call above may have already set the type for this DIE. */
11748 type = get_die_type (die, cu);
11752 /* Irix 6.2 native cc creates array types without children for
11753 arrays with unspecified length. */
11754 if (die->child == NULL)
11756 index_type = objfile_type (objfile)->builtin_int;
11757 range_type = create_range_type (NULL, index_type, 0, -1);
11758 type = create_array_type (NULL, element_type, range_type);
11759 return set_die_type (die, type, cu);
11762 back_to = make_cleanup (null_cleanup, NULL);
11763 child_die = die->child;
11764 while (child_die && child_die->tag)
11766 if (child_die->tag == DW_TAG_subrange_type)
11768 struct type *child_type = read_type_die (child_die, cu);
11770 if (child_type != NULL)
11772 /* The range type was succesfully read. Save it for the
11773 array type creation. */
11774 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11776 range_types = (struct type **)
11777 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11778 * sizeof (struct type *));
11780 make_cleanup (free_current_contents, &range_types);
11782 range_types[ndim++] = child_type;
11785 child_die = sibling_die (child_die);
11788 /* Dwarf2 dimensions are output from left to right, create the
11789 necessary array types in backwards order. */
11791 type = element_type;
11793 if (read_array_order (die, cu) == DW_ORD_col_major)
11798 type = create_array_type (NULL, type, range_types[i++]);
11803 type = create_array_type (NULL, type, range_types[ndim]);
11806 /* Understand Dwarf2 support for vector types (like they occur on
11807 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11808 array type. This is not part of the Dwarf2/3 standard yet, but a
11809 custom vendor extension. The main difference between a regular
11810 array and the vector variant is that vectors are passed by value
11812 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11814 make_vector_type (type);
11816 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11817 implementation may choose to implement triple vectors using this
11819 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11822 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11823 TYPE_LENGTH (type) = DW_UNSND (attr);
11825 complaint (&symfile_complaints,
11826 _("DW_AT_byte_size for array type smaller "
11827 "than the total size of elements"));
11830 name = dwarf2_name (die, cu);
11832 TYPE_NAME (type) = name;
11834 /* Install the type in the die. */
11835 set_die_type (die, type, cu);
11837 /* set_die_type should be already done. */
11838 set_descriptive_type (type, die, cu);
11840 do_cleanups (back_to);
11845 static enum dwarf_array_dim_ordering
11846 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11848 struct attribute *attr;
11850 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11852 if (attr) return DW_SND (attr);
11854 /* GNU F77 is a special case, as at 08/2004 array type info is the
11855 opposite order to the dwarf2 specification, but data is still
11856 laid out as per normal fortran.
11858 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11859 version checking. */
11861 if (cu->language == language_fortran
11862 && cu->producer && strstr (cu->producer, "GNU F77"))
11864 return DW_ORD_row_major;
11867 switch (cu->language_defn->la_array_ordering)
11869 case array_column_major:
11870 return DW_ORD_col_major;
11871 case array_row_major:
11873 return DW_ORD_row_major;
11877 /* Extract all information from a DW_TAG_set_type DIE and put it in
11878 the DIE's type field. */
11880 static struct type *
11881 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11883 struct type *domain_type, *set_type;
11884 struct attribute *attr;
11886 domain_type = die_type (die, cu);
11888 /* The die_type call above may have already set the type for this DIE. */
11889 set_type = get_die_type (die, cu);
11893 set_type = create_set_type (NULL, domain_type);
11895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11897 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11899 return set_die_type (die, set_type, cu);
11902 /* A helper for read_common_block that creates a locexpr baton.
11903 SYM is the symbol which we are marking as computed.
11904 COMMON_DIE is the DIE for the common block.
11905 COMMON_LOC is the location expression attribute for the common
11907 MEMBER_LOC is the location expression attribute for the particular
11908 member of the common block that we are processing.
11909 CU is the CU from which the above come. */
11912 mark_common_block_symbol_computed (struct symbol *sym,
11913 struct die_info *common_die,
11914 struct attribute *common_loc,
11915 struct attribute *member_loc,
11916 struct dwarf2_cu *cu)
11918 struct objfile *objfile = dwarf2_per_objfile->objfile;
11919 struct dwarf2_locexpr_baton *baton;
11921 unsigned int cu_off;
11922 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11923 LONGEST offset = 0;
11925 gdb_assert (common_loc && member_loc);
11926 gdb_assert (attr_form_is_block (common_loc));
11927 gdb_assert (attr_form_is_block (member_loc)
11928 || attr_form_is_constant (member_loc));
11930 baton = obstack_alloc (&objfile->objfile_obstack,
11931 sizeof (struct dwarf2_locexpr_baton));
11932 baton->per_cu = cu->per_cu;
11933 gdb_assert (baton->per_cu);
11935 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11937 if (attr_form_is_constant (member_loc))
11939 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11940 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11943 baton->size += DW_BLOCK (member_loc)->size;
11945 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11948 *ptr++ = DW_OP_call4;
11949 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11950 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11953 if (attr_form_is_constant (member_loc))
11955 *ptr++ = DW_OP_addr;
11956 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11957 ptr += cu->header.addr_size;
11961 /* We have to copy the data here, because DW_OP_call4 will only
11962 use a DW_AT_location attribute. */
11963 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11964 ptr += DW_BLOCK (member_loc)->size;
11967 *ptr++ = DW_OP_plus;
11968 gdb_assert (ptr - baton->data == baton->size);
11970 SYMBOL_LOCATION_BATON (sym) = baton;
11971 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11974 /* Create appropriate locally-scoped variables for all the
11975 DW_TAG_common_block entries. Also create a struct common_block
11976 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11977 is used to sepate the common blocks name namespace from regular
11981 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11983 struct attribute *attr;
11985 attr = dwarf2_attr (die, DW_AT_location, cu);
11988 /* Support the .debug_loc offsets. */
11989 if (attr_form_is_block (attr))
11993 else if (attr_form_is_section_offset (attr))
11995 dwarf2_complex_location_expr_complaint ();
12000 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12001 "common block member");
12006 if (die->child != NULL)
12008 struct objfile *objfile = cu->objfile;
12009 struct die_info *child_die;
12010 size_t n_entries = 0, size;
12011 struct common_block *common_block;
12012 struct symbol *sym;
12014 for (child_die = die->child;
12015 child_die && child_die->tag;
12016 child_die = sibling_die (child_die))
12019 size = (sizeof (struct common_block)
12020 + (n_entries - 1) * sizeof (struct symbol *));
12021 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12022 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12023 common_block->n_entries = 0;
12025 for (child_die = die->child;
12026 child_die && child_die->tag;
12027 child_die = sibling_die (child_die))
12029 /* Create the symbol in the DW_TAG_common_block block in the current
12031 sym = new_symbol (child_die, NULL, cu);
12034 struct attribute *member_loc;
12036 common_block->contents[common_block->n_entries++] = sym;
12038 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12042 /* GDB has handled this for a long time, but it is
12043 not specified by DWARF. It seems to have been
12044 emitted by gfortran at least as recently as:
12045 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12046 complaint (&symfile_complaints,
12047 _("Variable in common block has "
12048 "DW_AT_data_member_location "
12049 "- DIE at 0x%x [in module %s]"),
12050 child_die->offset.sect_off, cu->objfile->name);
12052 if (attr_form_is_section_offset (member_loc))
12053 dwarf2_complex_location_expr_complaint ();
12054 else if (attr_form_is_constant (member_loc)
12055 || attr_form_is_block (member_loc))
12058 mark_common_block_symbol_computed (sym, die, attr,
12062 dwarf2_complex_location_expr_complaint ();
12067 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12068 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12072 /* Create a type for a C++ namespace. */
12074 static struct type *
12075 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12077 struct objfile *objfile = cu->objfile;
12078 const char *previous_prefix, *name;
12082 /* For extensions, reuse the type of the original namespace. */
12083 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12085 struct die_info *ext_die;
12086 struct dwarf2_cu *ext_cu = cu;
12088 ext_die = dwarf2_extension (die, &ext_cu);
12089 type = read_type_die (ext_die, ext_cu);
12091 /* EXT_CU may not be the same as CU.
12092 Ensure TYPE is recorded in CU's type_hash table. */
12093 return set_die_type (die, type, cu);
12096 name = namespace_name (die, &is_anonymous, cu);
12098 /* Now build the name of the current namespace. */
12100 previous_prefix = determine_prefix (die, cu);
12101 if (previous_prefix[0] != '\0')
12102 name = typename_concat (&objfile->objfile_obstack,
12103 previous_prefix, name, 0, cu);
12105 /* Create the type. */
12106 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12108 TYPE_NAME (type) = name;
12109 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12111 return set_die_type (die, type, cu);
12114 /* Read a C++ namespace. */
12117 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12119 struct objfile *objfile = cu->objfile;
12122 /* Add a symbol associated to this if we haven't seen the namespace
12123 before. Also, add a using directive if it's an anonymous
12126 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12130 type = read_type_die (die, cu);
12131 new_symbol (die, type, cu);
12133 namespace_name (die, &is_anonymous, cu);
12136 const char *previous_prefix = determine_prefix (die, cu);
12138 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12139 NULL, NULL, 0, &objfile->objfile_obstack);
12143 if (die->child != NULL)
12145 struct die_info *child_die = die->child;
12147 while (child_die && child_die->tag)
12149 process_die (child_die, cu);
12150 child_die = sibling_die (child_die);
12155 /* Read a Fortran module as type. This DIE can be only a declaration used for
12156 imported module. Still we need that type as local Fortran "use ... only"
12157 declaration imports depend on the created type in determine_prefix. */
12159 static struct type *
12160 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12162 struct objfile *objfile = cu->objfile;
12163 const char *module_name;
12166 module_name = dwarf2_name (die, cu);
12168 complaint (&symfile_complaints,
12169 _("DW_TAG_module has no name, offset 0x%x"),
12170 die->offset.sect_off);
12171 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12173 /* determine_prefix uses TYPE_TAG_NAME. */
12174 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12176 return set_die_type (die, type, cu);
12179 /* Read a Fortran module. */
12182 read_module (struct die_info *die, struct dwarf2_cu *cu)
12184 struct die_info *child_die = die->child;
12186 while (child_die && child_die->tag)
12188 process_die (child_die, cu);
12189 child_die = sibling_die (child_die);
12193 /* Return the name of the namespace represented by DIE. Set
12194 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12197 static const char *
12198 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12200 struct die_info *current_die;
12201 const char *name = NULL;
12203 /* Loop through the extensions until we find a name. */
12205 for (current_die = die;
12206 current_die != NULL;
12207 current_die = dwarf2_extension (die, &cu))
12209 name = dwarf2_name (current_die, cu);
12214 /* Is it an anonymous namespace? */
12216 *is_anonymous = (name == NULL);
12218 name = CP_ANONYMOUS_NAMESPACE_STR;
12223 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12224 the user defined type vector. */
12226 static struct type *
12227 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12229 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12230 struct comp_unit_head *cu_header = &cu->header;
12232 struct attribute *attr_byte_size;
12233 struct attribute *attr_address_class;
12234 int byte_size, addr_class;
12235 struct type *target_type;
12237 target_type = die_type (die, cu);
12239 /* The die_type call above may have already set the type for this DIE. */
12240 type = get_die_type (die, cu);
12244 type = lookup_pointer_type (target_type);
12246 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12247 if (attr_byte_size)
12248 byte_size = DW_UNSND (attr_byte_size);
12250 byte_size = cu_header->addr_size;
12252 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12253 if (attr_address_class)
12254 addr_class = DW_UNSND (attr_address_class);
12256 addr_class = DW_ADDR_none;
12258 /* If the pointer size or address class is different than the
12259 default, create a type variant marked as such and set the
12260 length accordingly. */
12261 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12263 if (gdbarch_address_class_type_flags_p (gdbarch))
12267 type_flags = gdbarch_address_class_type_flags
12268 (gdbarch, byte_size, addr_class);
12269 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12271 type = make_type_with_address_space (type, type_flags);
12273 else if (TYPE_LENGTH (type) != byte_size)
12275 complaint (&symfile_complaints,
12276 _("invalid pointer size %d"), byte_size);
12280 /* Should we also complain about unhandled address classes? */
12284 TYPE_LENGTH (type) = byte_size;
12285 return set_die_type (die, type, cu);
12288 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12289 the user defined type vector. */
12291 static struct type *
12292 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12295 struct type *to_type;
12296 struct type *domain;
12298 to_type = die_type (die, cu);
12299 domain = die_containing_type (die, cu);
12301 /* The calls above may have already set the type for this DIE. */
12302 type = get_die_type (die, cu);
12306 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12307 type = lookup_methodptr_type (to_type);
12308 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12310 struct type *new_type = alloc_type (cu->objfile);
12312 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12313 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12314 TYPE_VARARGS (to_type));
12315 type = lookup_methodptr_type (new_type);
12318 type = lookup_memberptr_type (to_type, domain);
12320 return set_die_type (die, type, cu);
12323 /* Extract all information from a DW_TAG_reference_type DIE and add to
12324 the user defined type vector. */
12326 static struct type *
12327 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12329 struct comp_unit_head *cu_header = &cu->header;
12330 struct type *type, *target_type;
12331 struct attribute *attr;
12333 target_type = die_type (die, cu);
12335 /* The die_type call above may have already set the type for this DIE. */
12336 type = get_die_type (die, cu);
12340 type = lookup_reference_type (target_type);
12341 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12344 TYPE_LENGTH (type) = DW_UNSND (attr);
12348 TYPE_LENGTH (type) = cu_header->addr_size;
12350 return set_die_type (die, type, cu);
12353 static struct type *
12354 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12356 struct type *base_type, *cv_type;
12358 base_type = die_type (die, cu);
12360 /* The die_type call above may have already set the type for this DIE. */
12361 cv_type = get_die_type (die, cu);
12365 /* In case the const qualifier is applied to an array type, the element type
12366 is so qualified, not the array type (section 6.7.3 of C99). */
12367 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12369 struct type *el_type, *inner_array;
12371 base_type = copy_type (base_type);
12372 inner_array = base_type;
12374 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12376 TYPE_TARGET_TYPE (inner_array) =
12377 copy_type (TYPE_TARGET_TYPE (inner_array));
12378 inner_array = TYPE_TARGET_TYPE (inner_array);
12381 el_type = TYPE_TARGET_TYPE (inner_array);
12382 TYPE_TARGET_TYPE (inner_array) =
12383 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12385 return set_die_type (die, base_type, cu);
12388 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12389 return set_die_type (die, cv_type, cu);
12392 static struct type *
12393 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12395 struct type *base_type, *cv_type;
12397 base_type = die_type (die, cu);
12399 /* The die_type call above may have already set the type for this DIE. */
12400 cv_type = get_die_type (die, cu);
12404 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12405 return set_die_type (die, cv_type, cu);
12408 /* Handle DW_TAG_restrict_type. */
12410 static struct type *
12411 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12413 struct type *base_type, *cv_type;
12415 base_type = die_type (die, cu);
12417 /* The die_type call above may have already set the type for this DIE. */
12418 cv_type = get_die_type (die, cu);
12422 cv_type = make_restrict_type (base_type);
12423 return set_die_type (die, cv_type, cu);
12426 /* Extract all information from a DW_TAG_string_type DIE and add to
12427 the user defined type vector. It isn't really a user defined type,
12428 but it behaves like one, with other DIE's using an AT_user_def_type
12429 attribute to reference it. */
12431 static struct type *
12432 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12434 struct objfile *objfile = cu->objfile;
12435 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12436 struct type *type, *range_type, *index_type, *char_type;
12437 struct attribute *attr;
12438 unsigned int length;
12440 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12443 length = DW_UNSND (attr);
12447 /* Check for the DW_AT_byte_size attribute. */
12448 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12451 length = DW_UNSND (attr);
12459 index_type = objfile_type (objfile)->builtin_int;
12460 range_type = create_range_type (NULL, index_type, 1, length);
12461 char_type = language_string_char_type (cu->language_defn, gdbarch);
12462 type = create_string_type (NULL, char_type, range_type);
12464 return set_die_type (die, type, cu);
12467 /* Handle DIES due to C code like:
12471 int (*funcp)(int a, long l);
12475 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12477 static struct type *
12478 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12480 struct objfile *objfile = cu->objfile;
12481 struct type *type; /* Type that this function returns. */
12482 struct type *ftype; /* Function that returns above type. */
12483 struct attribute *attr;
12485 type = die_type (die, cu);
12487 /* The die_type call above may have already set the type for this DIE. */
12488 ftype = get_die_type (die, cu);
12492 ftype = lookup_function_type (type);
12494 /* All functions in C++, Pascal and Java have prototypes. */
12495 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12496 if ((attr && (DW_UNSND (attr) != 0))
12497 || cu->language == language_cplus
12498 || cu->language == language_java
12499 || cu->language == language_pascal)
12500 TYPE_PROTOTYPED (ftype) = 1;
12501 else if (producer_is_realview (cu->producer))
12502 /* RealView does not emit DW_AT_prototyped. We can not
12503 distinguish prototyped and unprototyped functions; default to
12504 prototyped, since that is more common in modern code (and
12505 RealView warns about unprototyped functions). */
12506 TYPE_PROTOTYPED (ftype) = 1;
12508 /* Store the calling convention in the type if it's available in
12509 the subroutine die. Otherwise set the calling convention to
12510 the default value DW_CC_normal. */
12511 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12513 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12514 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12515 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12517 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12519 /* We need to add the subroutine type to the die immediately so
12520 we don't infinitely recurse when dealing with parameters
12521 declared as the same subroutine type. */
12522 set_die_type (die, ftype, cu);
12524 if (die->child != NULL)
12526 struct type *void_type = objfile_type (objfile)->builtin_void;
12527 struct die_info *child_die;
12528 int nparams, iparams;
12530 /* Count the number of parameters.
12531 FIXME: GDB currently ignores vararg functions, but knows about
12532 vararg member functions. */
12534 child_die = die->child;
12535 while (child_die && child_die->tag)
12537 if (child_die->tag == DW_TAG_formal_parameter)
12539 else if (child_die->tag == DW_TAG_unspecified_parameters)
12540 TYPE_VARARGS (ftype) = 1;
12541 child_die = sibling_die (child_die);
12544 /* Allocate storage for parameters and fill them in. */
12545 TYPE_NFIELDS (ftype) = nparams;
12546 TYPE_FIELDS (ftype) = (struct field *)
12547 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12549 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12550 even if we error out during the parameters reading below. */
12551 for (iparams = 0; iparams < nparams; iparams++)
12552 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12555 child_die = die->child;
12556 while (child_die && child_die->tag)
12558 if (child_die->tag == DW_TAG_formal_parameter)
12560 struct type *arg_type;
12562 /* DWARF version 2 has no clean way to discern C++
12563 static and non-static member functions. G++ helps
12564 GDB by marking the first parameter for non-static
12565 member functions (which is the this pointer) as
12566 artificial. We pass this information to
12567 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12569 DWARF version 3 added DW_AT_object_pointer, which GCC
12570 4.5 does not yet generate. */
12571 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12573 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12576 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12578 /* GCC/43521: In java, the formal parameter
12579 "this" is sometimes not marked with DW_AT_artificial. */
12580 if (cu->language == language_java)
12582 const char *name = dwarf2_name (child_die, cu);
12584 if (name && !strcmp (name, "this"))
12585 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12588 arg_type = die_type (child_die, cu);
12590 /* RealView does not mark THIS as const, which the testsuite
12591 expects. GCC marks THIS as const in method definitions,
12592 but not in the class specifications (GCC PR 43053). */
12593 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12594 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12597 struct dwarf2_cu *arg_cu = cu;
12598 const char *name = dwarf2_name (child_die, cu);
12600 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12603 /* If the compiler emits this, use it. */
12604 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12607 else if (name && strcmp (name, "this") == 0)
12608 /* Function definitions will have the argument names. */
12610 else if (name == NULL && iparams == 0)
12611 /* Declarations may not have the names, so like
12612 elsewhere in GDB, assume an artificial first
12613 argument is "this". */
12617 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12621 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12624 child_die = sibling_die (child_die);
12631 static struct type *
12632 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12634 struct objfile *objfile = cu->objfile;
12635 const char *name = NULL;
12636 struct type *this_type, *target_type;
12638 name = dwarf2_full_name (NULL, die, cu);
12639 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12640 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12641 TYPE_NAME (this_type) = name;
12642 set_die_type (die, this_type, cu);
12643 target_type = die_type (die, cu);
12644 if (target_type != this_type)
12645 TYPE_TARGET_TYPE (this_type) = target_type;
12648 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12649 spec and cause infinite loops in GDB. */
12650 complaint (&symfile_complaints,
12651 _("Self-referential DW_TAG_typedef "
12652 "- DIE at 0x%x [in module %s]"),
12653 die->offset.sect_off, objfile->name);
12654 TYPE_TARGET_TYPE (this_type) = NULL;
12659 /* Find a representation of a given base type and install
12660 it in the TYPE field of the die. */
12662 static struct type *
12663 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12665 struct objfile *objfile = cu->objfile;
12667 struct attribute *attr;
12668 int encoding = 0, size = 0;
12670 enum type_code code = TYPE_CODE_INT;
12671 int type_flags = 0;
12672 struct type *target_type = NULL;
12674 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12677 encoding = DW_UNSND (attr);
12679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12682 size = DW_UNSND (attr);
12684 name = dwarf2_name (die, cu);
12687 complaint (&symfile_complaints,
12688 _("DW_AT_name missing from DW_TAG_base_type"));
12693 case DW_ATE_address:
12694 /* Turn DW_ATE_address into a void * pointer. */
12695 code = TYPE_CODE_PTR;
12696 type_flags |= TYPE_FLAG_UNSIGNED;
12697 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12699 case DW_ATE_boolean:
12700 code = TYPE_CODE_BOOL;
12701 type_flags |= TYPE_FLAG_UNSIGNED;
12703 case DW_ATE_complex_float:
12704 code = TYPE_CODE_COMPLEX;
12705 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12707 case DW_ATE_decimal_float:
12708 code = TYPE_CODE_DECFLOAT;
12711 code = TYPE_CODE_FLT;
12713 case DW_ATE_signed:
12715 case DW_ATE_unsigned:
12716 type_flags |= TYPE_FLAG_UNSIGNED;
12717 if (cu->language == language_fortran
12719 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12720 code = TYPE_CODE_CHAR;
12722 case DW_ATE_signed_char:
12723 if (cu->language == language_ada || cu->language == language_m2
12724 || cu->language == language_pascal
12725 || cu->language == language_fortran)
12726 code = TYPE_CODE_CHAR;
12728 case DW_ATE_unsigned_char:
12729 if (cu->language == language_ada || cu->language == language_m2
12730 || cu->language == language_pascal
12731 || cu->language == language_fortran)
12732 code = TYPE_CODE_CHAR;
12733 type_flags |= TYPE_FLAG_UNSIGNED;
12736 /* We just treat this as an integer and then recognize the
12737 type by name elsewhere. */
12741 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12742 dwarf_type_encoding_name (encoding));
12746 type = init_type (code, size, type_flags, NULL, objfile);
12747 TYPE_NAME (type) = name;
12748 TYPE_TARGET_TYPE (type) = target_type;
12750 if (name && strcmp (name, "char") == 0)
12751 TYPE_NOSIGN (type) = 1;
12753 return set_die_type (die, type, cu);
12756 /* Read the given DW_AT_subrange DIE. */
12758 static struct type *
12759 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12761 struct type *base_type, *orig_base_type;
12762 struct type *range_type;
12763 struct attribute *attr;
12765 int low_default_is_valid;
12767 LONGEST negative_mask;
12769 orig_base_type = die_type (die, cu);
12770 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12771 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12772 creating the range type, but we use the result of check_typedef
12773 when examining properties of the type. */
12774 base_type = check_typedef (orig_base_type);
12776 /* The die_type call above may have already set the type for this DIE. */
12777 range_type = get_die_type (die, cu);
12781 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12782 omitting DW_AT_lower_bound. */
12783 switch (cu->language)
12786 case language_cplus:
12788 low_default_is_valid = 1;
12790 case language_fortran:
12792 low_default_is_valid = 1;
12795 case language_java:
12796 case language_objc:
12798 low_default_is_valid = (cu->header.version >= 4);
12802 case language_pascal:
12804 low_default_is_valid = (cu->header.version >= 4);
12808 low_default_is_valid = 0;
12812 /* FIXME: For variable sized arrays either of these could be
12813 a variable rather than a constant value. We'll allow it,
12814 but we don't know how to handle it. */
12815 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12817 low = dwarf2_get_attr_constant_value (attr, low);
12818 else if (!low_default_is_valid)
12819 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12820 "- DIE at 0x%x [in module %s]"),
12821 die->offset.sect_off, cu->objfile->name);
12823 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12826 if (attr_form_is_block (attr) || is_ref_attr (attr))
12828 /* GCC encodes arrays with unspecified or dynamic length
12829 with a DW_FORM_block1 attribute or a reference attribute.
12830 FIXME: GDB does not yet know how to handle dynamic
12831 arrays properly, treat them as arrays with unspecified
12834 FIXME: jimb/2003-09-22: GDB does not really know
12835 how to handle arrays of unspecified length
12836 either; we just represent them as zero-length
12837 arrays. Choose an appropriate upper bound given
12838 the lower bound we've computed above. */
12842 high = dwarf2_get_attr_constant_value (attr, 1);
12846 attr = dwarf2_attr (die, DW_AT_count, cu);
12849 int count = dwarf2_get_attr_constant_value (attr, 1);
12850 high = low + count - 1;
12854 /* Unspecified array length. */
12859 /* Dwarf-2 specifications explicitly allows to create subrange types
12860 without specifying a base type.
12861 In that case, the base type must be set to the type of
12862 the lower bound, upper bound or count, in that order, if any of these
12863 three attributes references an object that has a type.
12864 If no base type is found, the Dwarf-2 specifications say that
12865 a signed integer type of size equal to the size of an address should
12867 For the following C code: `extern char gdb_int [];'
12868 GCC produces an empty range DIE.
12869 FIXME: muller/2010-05-28: Possible references to object for low bound,
12870 high bound or count are not yet handled by this code. */
12871 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12873 struct objfile *objfile = cu->objfile;
12874 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12875 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12876 struct type *int_type = objfile_type (objfile)->builtin_int;
12878 /* Test "int", "long int", and "long long int" objfile types,
12879 and select the first one having a size above or equal to the
12880 architecture address size. */
12881 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12882 base_type = int_type;
12885 int_type = objfile_type (objfile)->builtin_long;
12886 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12887 base_type = int_type;
12890 int_type = objfile_type (objfile)->builtin_long_long;
12891 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12892 base_type = int_type;
12898 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12899 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12900 low |= negative_mask;
12901 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12902 high |= negative_mask;
12904 range_type = create_range_type (NULL, orig_base_type, low, high);
12906 /* Mark arrays with dynamic length at least as an array of unspecified
12907 length. GDB could check the boundary but before it gets implemented at
12908 least allow accessing the array elements. */
12909 if (attr && attr_form_is_block (attr))
12910 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12912 /* Ada expects an empty array on no boundary attributes. */
12913 if (attr == NULL && cu->language != language_ada)
12914 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12916 name = dwarf2_name (die, cu);
12918 TYPE_NAME (range_type) = name;
12920 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12922 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12924 set_die_type (die, range_type, cu);
12926 /* set_die_type should be already done. */
12927 set_descriptive_type (range_type, die, cu);
12932 static struct type *
12933 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12937 /* For now, we only support the C meaning of an unspecified type: void. */
12939 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12940 TYPE_NAME (type) = dwarf2_name (die, cu);
12942 return set_die_type (die, type, cu);
12945 /* Read a single die and all its descendents. Set the die's sibling
12946 field to NULL; set other fields in the die correctly, and set all
12947 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12948 location of the info_ptr after reading all of those dies. PARENT
12949 is the parent of the die in question. */
12951 static struct die_info *
12952 read_die_and_children (const struct die_reader_specs *reader,
12953 gdb_byte *info_ptr,
12954 gdb_byte **new_info_ptr,
12955 struct die_info *parent)
12957 struct die_info *die;
12961 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12964 *new_info_ptr = cur_ptr;
12967 store_in_ref_table (die, reader->cu);
12970 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12974 *new_info_ptr = cur_ptr;
12977 die->sibling = NULL;
12978 die->parent = parent;
12982 /* Read a die, all of its descendents, and all of its siblings; set
12983 all of the fields of all of the dies correctly. Arguments are as
12984 in read_die_and_children. */
12986 static struct die_info *
12987 read_die_and_siblings (const struct die_reader_specs *reader,
12988 gdb_byte *info_ptr,
12989 gdb_byte **new_info_ptr,
12990 struct die_info *parent)
12992 struct die_info *first_die, *last_sibling;
12995 cur_ptr = info_ptr;
12996 first_die = last_sibling = NULL;
13000 struct die_info *die
13001 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13005 *new_info_ptr = cur_ptr;
13012 last_sibling->sibling = die;
13014 last_sibling = die;
13018 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13020 The caller is responsible for filling in the extra attributes
13021 and updating (*DIEP)->num_attrs.
13022 Set DIEP to point to a newly allocated die with its information,
13023 except for its child, sibling, and parent fields.
13024 Set HAS_CHILDREN to tell whether the die has children or not. */
13027 read_full_die_1 (const struct die_reader_specs *reader,
13028 struct die_info **diep, gdb_byte *info_ptr,
13029 int *has_children, int num_extra_attrs)
13031 unsigned int abbrev_number, bytes_read, i;
13032 sect_offset offset;
13033 struct abbrev_info *abbrev;
13034 struct die_info *die;
13035 struct dwarf2_cu *cu = reader->cu;
13036 bfd *abfd = reader->abfd;
13038 offset.sect_off = info_ptr - reader->buffer;
13039 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13040 info_ptr += bytes_read;
13041 if (!abbrev_number)
13048 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13050 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13052 bfd_get_filename (abfd));
13054 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13055 die->offset = offset;
13056 die->tag = abbrev->tag;
13057 die->abbrev = abbrev_number;
13059 /* Make the result usable.
13060 The caller needs to update num_attrs after adding the extra
13062 die->num_attrs = abbrev->num_attrs;
13064 for (i = 0; i < abbrev->num_attrs; ++i)
13065 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13069 *has_children = abbrev->has_children;
13073 /* Read a die and all its attributes.
13074 Set DIEP to point to a newly allocated die with its information,
13075 except for its child, sibling, and parent fields.
13076 Set HAS_CHILDREN to tell whether the die has children or not. */
13079 read_full_die (const struct die_reader_specs *reader,
13080 struct die_info **diep, gdb_byte *info_ptr,
13083 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13086 /* Abbreviation tables.
13088 In DWARF version 2, the description of the debugging information is
13089 stored in a separate .debug_abbrev section. Before we read any
13090 dies from a section we read in all abbreviations and install them
13091 in a hash table. */
13093 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13095 static struct abbrev_info *
13096 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13098 struct abbrev_info *abbrev;
13100 abbrev = (struct abbrev_info *)
13101 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13102 memset (abbrev, 0, sizeof (struct abbrev_info));
13106 /* Add an abbreviation to the table. */
13109 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13110 unsigned int abbrev_number,
13111 struct abbrev_info *abbrev)
13113 unsigned int hash_number;
13115 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13116 abbrev->next = abbrev_table->abbrevs[hash_number];
13117 abbrev_table->abbrevs[hash_number] = abbrev;
13120 /* Look up an abbrev in the table.
13121 Returns NULL if the abbrev is not found. */
13123 static struct abbrev_info *
13124 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13125 unsigned int abbrev_number)
13127 unsigned int hash_number;
13128 struct abbrev_info *abbrev;
13130 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13131 abbrev = abbrev_table->abbrevs[hash_number];
13135 if (abbrev->number == abbrev_number)
13137 abbrev = abbrev->next;
13142 /* Read in an abbrev table. */
13144 static struct abbrev_table *
13145 abbrev_table_read_table (struct dwarf2_section_info *section,
13146 sect_offset offset)
13148 struct objfile *objfile = dwarf2_per_objfile->objfile;
13149 bfd *abfd = section->asection->owner;
13150 struct abbrev_table *abbrev_table;
13151 gdb_byte *abbrev_ptr;
13152 struct abbrev_info *cur_abbrev;
13153 unsigned int abbrev_number, bytes_read, abbrev_name;
13154 unsigned int abbrev_form;
13155 struct attr_abbrev *cur_attrs;
13156 unsigned int allocated_attrs;
13158 abbrev_table = XMALLOC (struct abbrev_table);
13159 abbrev_table->offset = offset;
13160 obstack_init (&abbrev_table->abbrev_obstack);
13161 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13163 * sizeof (struct abbrev_info *)));
13164 memset (abbrev_table->abbrevs, 0,
13165 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13167 dwarf2_read_section (objfile, section);
13168 abbrev_ptr = section->buffer + offset.sect_off;
13169 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13170 abbrev_ptr += bytes_read;
13172 allocated_attrs = ATTR_ALLOC_CHUNK;
13173 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13175 /* Loop until we reach an abbrev number of 0. */
13176 while (abbrev_number)
13178 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13180 /* read in abbrev header */
13181 cur_abbrev->number = abbrev_number;
13182 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13183 abbrev_ptr += bytes_read;
13184 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13187 /* now read in declarations */
13188 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13189 abbrev_ptr += bytes_read;
13190 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13191 abbrev_ptr += bytes_read;
13192 while (abbrev_name)
13194 if (cur_abbrev->num_attrs == allocated_attrs)
13196 allocated_attrs += ATTR_ALLOC_CHUNK;
13198 = xrealloc (cur_attrs, (allocated_attrs
13199 * sizeof (struct attr_abbrev)));
13202 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13203 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13204 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13205 abbrev_ptr += bytes_read;
13206 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13207 abbrev_ptr += bytes_read;
13210 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13211 (cur_abbrev->num_attrs
13212 * sizeof (struct attr_abbrev)));
13213 memcpy (cur_abbrev->attrs, cur_attrs,
13214 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13216 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13218 /* Get next abbreviation.
13219 Under Irix6 the abbreviations for a compilation unit are not
13220 always properly terminated with an abbrev number of 0.
13221 Exit loop if we encounter an abbreviation which we have
13222 already read (which means we are about to read the abbreviations
13223 for the next compile unit) or if the end of the abbreviation
13224 table is reached. */
13225 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13227 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13228 abbrev_ptr += bytes_read;
13229 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13234 return abbrev_table;
13237 /* Free the resources held by ABBREV_TABLE. */
13240 abbrev_table_free (struct abbrev_table *abbrev_table)
13242 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13243 xfree (abbrev_table);
13246 /* Same as abbrev_table_free but as a cleanup.
13247 We pass in a pointer to the pointer to the table so that we can
13248 set the pointer to NULL when we're done. It also simplifies
13249 build_type_unit_groups. */
13252 abbrev_table_free_cleanup (void *table_ptr)
13254 struct abbrev_table **abbrev_table_ptr = table_ptr;
13256 if (*abbrev_table_ptr != NULL)
13257 abbrev_table_free (*abbrev_table_ptr);
13258 *abbrev_table_ptr = NULL;
13261 /* Read the abbrev table for CU from ABBREV_SECTION. */
13264 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13265 struct dwarf2_section_info *abbrev_section)
13268 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13271 /* Release the memory used by the abbrev table for a compilation unit. */
13274 dwarf2_free_abbrev_table (void *ptr_to_cu)
13276 struct dwarf2_cu *cu = ptr_to_cu;
13278 abbrev_table_free (cu->abbrev_table);
13279 /* Set this to NULL so that we SEGV if we try to read it later,
13280 and also because free_comp_unit verifies this is NULL. */
13281 cu->abbrev_table = NULL;
13284 /* Returns nonzero if TAG represents a type that we might generate a partial
13288 is_type_tag_for_partial (int tag)
13293 /* Some types that would be reasonable to generate partial symbols for,
13294 that we don't at present. */
13295 case DW_TAG_array_type:
13296 case DW_TAG_file_type:
13297 case DW_TAG_ptr_to_member_type:
13298 case DW_TAG_set_type:
13299 case DW_TAG_string_type:
13300 case DW_TAG_subroutine_type:
13302 case DW_TAG_base_type:
13303 case DW_TAG_class_type:
13304 case DW_TAG_interface_type:
13305 case DW_TAG_enumeration_type:
13306 case DW_TAG_structure_type:
13307 case DW_TAG_subrange_type:
13308 case DW_TAG_typedef:
13309 case DW_TAG_union_type:
13316 /* Load all DIEs that are interesting for partial symbols into memory. */
13318 static struct partial_die_info *
13319 load_partial_dies (const struct die_reader_specs *reader,
13320 gdb_byte *info_ptr, int building_psymtab)
13322 struct dwarf2_cu *cu = reader->cu;
13323 struct objfile *objfile = cu->objfile;
13324 struct partial_die_info *part_die;
13325 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13326 struct abbrev_info *abbrev;
13327 unsigned int bytes_read;
13328 unsigned int load_all = 0;
13329 int nesting_level = 1;
13334 gdb_assert (cu->per_cu != NULL);
13335 if (cu->per_cu->load_all_dies)
13339 = htab_create_alloc_ex (cu->header.length / 12,
13343 &cu->comp_unit_obstack,
13344 hashtab_obstack_allocate,
13345 dummy_obstack_deallocate);
13347 part_die = obstack_alloc (&cu->comp_unit_obstack,
13348 sizeof (struct partial_die_info));
13352 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13354 /* A NULL abbrev means the end of a series of children. */
13355 if (abbrev == NULL)
13357 if (--nesting_level == 0)
13359 /* PART_DIE was probably the last thing allocated on the
13360 comp_unit_obstack, so we could call obstack_free
13361 here. We don't do that because the waste is small,
13362 and will be cleaned up when we're done with this
13363 compilation unit. This way, we're also more robust
13364 against other users of the comp_unit_obstack. */
13367 info_ptr += bytes_read;
13368 last_die = parent_die;
13369 parent_die = parent_die->die_parent;
13373 /* Check for template arguments. We never save these; if
13374 they're seen, we just mark the parent, and go on our way. */
13375 if (parent_die != NULL
13376 && cu->language == language_cplus
13377 && (abbrev->tag == DW_TAG_template_type_param
13378 || abbrev->tag == DW_TAG_template_value_param))
13380 parent_die->has_template_arguments = 1;
13384 /* We don't need a partial DIE for the template argument. */
13385 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13390 /* We only recurse into c++ subprograms looking for template arguments.
13391 Skip their other children. */
13393 && cu->language == language_cplus
13394 && parent_die != NULL
13395 && parent_die->tag == DW_TAG_subprogram)
13397 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13401 /* Check whether this DIE is interesting enough to save. Normally
13402 we would not be interested in members here, but there may be
13403 later variables referencing them via DW_AT_specification (for
13404 static members). */
13406 && !is_type_tag_for_partial (abbrev->tag)
13407 && abbrev->tag != DW_TAG_constant
13408 && abbrev->tag != DW_TAG_enumerator
13409 && abbrev->tag != DW_TAG_subprogram
13410 && abbrev->tag != DW_TAG_lexical_block
13411 && abbrev->tag != DW_TAG_variable
13412 && abbrev->tag != DW_TAG_namespace
13413 && abbrev->tag != DW_TAG_module
13414 && abbrev->tag != DW_TAG_member
13415 && abbrev->tag != DW_TAG_imported_unit)
13417 /* Otherwise we skip to the next sibling, if any. */
13418 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13422 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13425 /* This two-pass algorithm for processing partial symbols has a
13426 high cost in cache pressure. Thus, handle some simple cases
13427 here which cover the majority of C partial symbols. DIEs
13428 which neither have specification tags in them, nor could have
13429 specification tags elsewhere pointing at them, can simply be
13430 processed and discarded.
13432 This segment is also optional; scan_partial_symbols and
13433 add_partial_symbol will handle these DIEs if we chain
13434 them in normally. When compilers which do not emit large
13435 quantities of duplicate debug information are more common,
13436 this code can probably be removed. */
13438 /* Any complete simple types at the top level (pretty much all
13439 of them, for a language without namespaces), can be processed
13441 if (parent_die == NULL
13442 && part_die->has_specification == 0
13443 && part_die->is_declaration == 0
13444 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13445 || part_die->tag == DW_TAG_base_type
13446 || part_die->tag == DW_TAG_subrange_type))
13448 if (building_psymtab && part_die->name != NULL)
13449 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13450 VAR_DOMAIN, LOC_TYPEDEF,
13451 &objfile->static_psymbols,
13452 0, (CORE_ADDR) 0, cu->language, objfile);
13453 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13457 /* The exception for DW_TAG_typedef with has_children above is
13458 a workaround of GCC PR debug/47510. In the case of this complaint
13459 type_name_no_tag_or_error will error on such types later.
13461 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13462 it could not find the child DIEs referenced later, this is checked
13463 above. In correct DWARF DW_TAG_typedef should have no children. */
13465 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13466 complaint (&symfile_complaints,
13467 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13468 "- DIE at 0x%x [in module %s]"),
13469 part_die->offset.sect_off, objfile->name);
13471 /* If we're at the second level, and we're an enumerator, and
13472 our parent has no specification (meaning possibly lives in a
13473 namespace elsewhere), then we can add the partial symbol now
13474 instead of queueing it. */
13475 if (part_die->tag == DW_TAG_enumerator
13476 && parent_die != NULL
13477 && parent_die->die_parent == NULL
13478 && parent_die->tag == DW_TAG_enumeration_type
13479 && parent_die->has_specification == 0)
13481 if (part_die->name == NULL)
13482 complaint (&symfile_complaints,
13483 _("malformed enumerator DIE ignored"));
13484 else if (building_psymtab)
13485 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13486 VAR_DOMAIN, LOC_CONST,
13487 (cu->language == language_cplus
13488 || cu->language == language_java)
13489 ? &objfile->global_psymbols
13490 : &objfile->static_psymbols,
13491 0, (CORE_ADDR) 0, cu->language, objfile);
13493 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13497 /* We'll save this DIE so link it in. */
13498 part_die->die_parent = parent_die;
13499 part_die->die_sibling = NULL;
13500 part_die->die_child = NULL;
13502 if (last_die && last_die == parent_die)
13503 last_die->die_child = part_die;
13505 last_die->die_sibling = part_die;
13507 last_die = part_die;
13509 if (first_die == NULL)
13510 first_die = part_die;
13512 /* Maybe add the DIE to the hash table. Not all DIEs that we
13513 find interesting need to be in the hash table, because we
13514 also have the parent/sibling/child chains; only those that we
13515 might refer to by offset later during partial symbol reading.
13517 For now this means things that might have be the target of a
13518 DW_AT_specification, DW_AT_abstract_origin, or
13519 DW_AT_extension. DW_AT_extension will refer only to
13520 namespaces; DW_AT_abstract_origin refers to functions (and
13521 many things under the function DIE, but we do not recurse
13522 into function DIEs during partial symbol reading) and
13523 possibly variables as well; DW_AT_specification refers to
13524 declarations. Declarations ought to have the DW_AT_declaration
13525 flag. It happens that GCC forgets to put it in sometimes, but
13526 only for functions, not for types.
13528 Adding more things than necessary to the hash table is harmless
13529 except for the performance cost. Adding too few will result in
13530 wasted time in find_partial_die, when we reread the compilation
13531 unit with load_all_dies set. */
13534 || abbrev->tag == DW_TAG_constant
13535 || abbrev->tag == DW_TAG_subprogram
13536 || abbrev->tag == DW_TAG_variable
13537 || abbrev->tag == DW_TAG_namespace
13538 || part_die->is_declaration)
13542 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13543 part_die->offset.sect_off, INSERT);
13547 part_die = obstack_alloc (&cu->comp_unit_obstack,
13548 sizeof (struct partial_die_info));
13550 /* For some DIEs we want to follow their children (if any). For C
13551 we have no reason to follow the children of structures; for other
13552 languages we have to, so that we can get at method physnames
13553 to infer fully qualified class names, for DW_AT_specification,
13554 and for C++ template arguments. For C++, we also look one level
13555 inside functions to find template arguments (if the name of the
13556 function does not already contain the template arguments).
13558 For Ada, we need to scan the children of subprograms and lexical
13559 blocks as well because Ada allows the definition of nested
13560 entities that could be interesting for the debugger, such as
13561 nested subprograms for instance. */
13562 if (last_die->has_children
13564 || last_die->tag == DW_TAG_namespace
13565 || last_die->tag == DW_TAG_module
13566 || last_die->tag == DW_TAG_enumeration_type
13567 || (cu->language == language_cplus
13568 && last_die->tag == DW_TAG_subprogram
13569 && (last_die->name == NULL
13570 || strchr (last_die->name, '<') == NULL))
13571 || (cu->language != language_c
13572 && (last_die->tag == DW_TAG_class_type
13573 || last_die->tag == DW_TAG_interface_type
13574 || last_die->tag == DW_TAG_structure_type
13575 || last_die->tag == DW_TAG_union_type))
13576 || (cu->language == language_ada
13577 && (last_die->tag == DW_TAG_subprogram
13578 || last_die->tag == DW_TAG_lexical_block))))
13581 parent_die = last_die;
13585 /* Otherwise we skip to the next sibling, if any. */
13586 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13588 /* Back to the top, do it again. */
13592 /* Read a minimal amount of information into the minimal die structure. */
13595 read_partial_die (const struct die_reader_specs *reader,
13596 struct partial_die_info *part_die,
13597 struct abbrev_info *abbrev, unsigned int abbrev_len,
13598 gdb_byte *info_ptr)
13600 struct dwarf2_cu *cu = reader->cu;
13601 struct objfile *objfile = cu->objfile;
13602 gdb_byte *buffer = reader->buffer;
13604 struct attribute attr;
13605 int has_low_pc_attr = 0;
13606 int has_high_pc_attr = 0;
13607 int high_pc_relative = 0;
13609 memset (part_die, 0, sizeof (struct partial_die_info));
13611 part_die->offset.sect_off = info_ptr - buffer;
13613 info_ptr += abbrev_len;
13615 if (abbrev == NULL)
13618 part_die->tag = abbrev->tag;
13619 part_die->has_children = abbrev->has_children;
13621 for (i = 0; i < abbrev->num_attrs; ++i)
13623 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13625 /* Store the data if it is of an attribute we want to keep in a
13626 partial symbol table. */
13630 switch (part_die->tag)
13632 case DW_TAG_compile_unit:
13633 case DW_TAG_partial_unit:
13634 case DW_TAG_type_unit:
13635 /* Compilation units have a DW_AT_name that is a filename, not
13636 a source language identifier. */
13637 case DW_TAG_enumeration_type:
13638 case DW_TAG_enumerator:
13639 /* These tags always have simple identifiers already; no need
13640 to canonicalize them. */
13641 part_die->name = DW_STRING (&attr);
13645 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13646 &objfile->objfile_obstack);
13650 case DW_AT_linkage_name:
13651 case DW_AT_MIPS_linkage_name:
13652 /* Note that both forms of linkage name might appear. We
13653 assume they will be the same, and we only store the last
13655 if (cu->language == language_ada)
13656 part_die->name = DW_STRING (&attr);
13657 part_die->linkage_name = DW_STRING (&attr);
13660 has_low_pc_attr = 1;
13661 part_die->lowpc = DW_ADDR (&attr);
13663 case DW_AT_high_pc:
13664 has_high_pc_attr = 1;
13665 if (attr.form == DW_FORM_addr
13666 || attr.form == DW_FORM_GNU_addr_index)
13667 part_die->highpc = DW_ADDR (&attr);
13670 high_pc_relative = 1;
13671 part_die->highpc = DW_UNSND (&attr);
13674 case DW_AT_location:
13675 /* Support the .debug_loc offsets. */
13676 if (attr_form_is_block (&attr))
13678 part_die->d.locdesc = DW_BLOCK (&attr);
13680 else if (attr_form_is_section_offset (&attr))
13682 dwarf2_complex_location_expr_complaint ();
13686 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13687 "partial symbol information");
13690 case DW_AT_external:
13691 part_die->is_external = DW_UNSND (&attr);
13693 case DW_AT_declaration:
13694 part_die->is_declaration = DW_UNSND (&attr);
13697 part_die->has_type = 1;
13699 case DW_AT_abstract_origin:
13700 case DW_AT_specification:
13701 case DW_AT_extension:
13702 part_die->has_specification = 1;
13703 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13704 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13705 || cu->per_cu->is_dwz);
13707 case DW_AT_sibling:
13708 /* Ignore absolute siblings, they might point outside of
13709 the current compile unit. */
13710 if (attr.form == DW_FORM_ref_addr)
13711 complaint (&symfile_complaints,
13712 _("ignoring absolute DW_AT_sibling"));
13714 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13716 case DW_AT_byte_size:
13717 part_die->has_byte_size = 1;
13719 case DW_AT_calling_convention:
13720 /* DWARF doesn't provide a way to identify a program's source-level
13721 entry point. DW_AT_calling_convention attributes are only meant
13722 to describe functions' calling conventions.
13724 However, because it's a necessary piece of information in
13725 Fortran, and because DW_CC_program is the only piece of debugging
13726 information whose definition refers to a 'main program' at all,
13727 several compilers have begun marking Fortran main programs with
13728 DW_CC_program --- even when those functions use the standard
13729 calling conventions.
13731 So until DWARF specifies a way to provide this information and
13732 compilers pick up the new representation, we'll support this
13734 if (DW_UNSND (&attr) == DW_CC_program
13735 && cu->language == language_fortran)
13737 set_main_name (part_die->name);
13739 /* As this DIE has a static linkage the name would be difficult
13740 to look up later. */
13741 language_of_main = language_fortran;
13745 if (DW_UNSND (&attr) == DW_INL_inlined
13746 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13747 part_die->may_be_inlined = 1;
13751 if (part_die->tag == DW_TAG_imported_unit)
13753 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13754 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13755 || cu->per_cu->is_dwz);
13764 if (high_pc_relative)
13765 part_die->highpc += part_die->lowpc;
13767 if (has_low_pc_attr && has_high_pc_attr)
13769 /* When using the GNU linker, .gnu.linkonce. sections are used to
13770 eliminate duplicate copies of functions and vtables and such.
13771 The linker will arbitrarily choose one and discard the others.
13772 The AT_*_pc values for such functions refer to local labels in
13773 these sections. If the section from that file was discarded, the
13774 labels are not in the output, so the relocs get a value of 0.
13775 If this is a discarded function, mark the pc bounds as invalid,
13776 so that GDB will ignore it. */
13777 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13781 complaint (&symfile_complaints,
13782 _("DW_AT_low_pc %s is zero "
13783 "for DIE at 0x%x [in module %s]"),
13784 paddress (gdbarch, part_die->lowpc),
13785 part_die->offset.sect_off, objfile->name);
13787 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13788 else if (part_die->lowpc >= part_die->highpc)
13790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13792 complaint (&symfile_complaints,
13793 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13794 "for DIE at 0x%x [in module %s]"),
13795 paddress (gdbarch, part_die->lowpc),
13796 paddress (gdbarch, part_die->highpc),
13797 part_die->offset.sect_off, objfile->name);
13800 part_die->has_pc_info = 1;
13806 /* Find a cached partial DIE at OFFSET in CU. */
13808 static struct partial_die_info *
13809 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13811 struct partial_die_info *lookup_die = NULL;
13812 struct partial_die_info part_die;
13814 part_die.offset = offset;
13815 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13821 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13822 except in the case of .debug_types DIEs which do not reference
13823 outside their CU (they do however referencing other types via
13824 DW_FORM_ref_sig8). */
13826 static struct partial_die_info *
13827 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13829 struct objfile *objfile = cu->objfile;
13830 struct dwarf2_per_cu_data *per_cu = NULL;
13831 struct partial_die_info *pd = NULL;
13833 if (offset_in_dwz == cu->per_cu->is_dwz
13834 && offset_in_cu_p (&cu->header, offset))
13836 pd = find_partial_die_in_comp_unit (offset, cu);
13839 /* We missed recording what we needed.
13840 Load all dies and try again. */
13841 per_cu = cu->per_cu;
13845 /* TUs don't reference other CUs/TUs (except via type signatures). */
13846 if (cu->per_cu->is_debug_types)
13848 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13849 " external reference to offset 0x%lx [in module %s].\n"),
13850 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13851 bfd_get_filename (objfile->obfd));
13853 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13856 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13857 load_partial_comp_unit (per_cu);
13859 per_cu->cu->last_used = 0;
13860 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13863 /* If we didn't find it, and not all dies have been loaded,
13864 load them all and try again. */
13866 if (pd == NULL && per_cu->load_all_dies == 0)
13868 per_cu->load_all_dies = 1;
13870 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13871 THIS_CU->cu may already be in use. So we can't just free it and
13872 replace its DIEs with the ones we read in. Instead, we leave those
13873 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13874 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13876 load_partial_comp_unit (per_cu);
13878 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13882 internal_error (__FILE__, __LINE__,
13883 _("could not find partial DIE 0x%x "
13884 "in cache [from module %s]\n"),
13885 offset.sect_off, bfd_get_filename (objfile->obfd));
13889 /* See if we can figure out if the class lives in a namespace. We do
13890 this by looking for a member function; its demangled name will
13891 contain namespace info, if there is any. */
13894 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13895 struct dwarf2_cu *cu)
13897 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13898 what template types look like, because the demangler
13899 frequently doesn't give the same name as the debug info. We
13900 could fix this by only using the demangled name to get the
13901 prefix (but see comment in read_structure_type). */
13903 struct partial_die_info *real_pdi;
13904 struct partial_die_info *child_pdi;
13906 /* If this DIE (this DIE's specification, if any) has a parent, then
13907 we should not do this. We'll prepend the parent's fully qualified
13908 name when we create the partial symbol. */
13910 real_pdi = struct_pdi;
13911 while (real_pdi->has_specification)
13912 real_pdi = find_partial_die (real_pdi->spec_offset,
13913 real_pdi->spec_is_dwz, cu);
13915 if (real_pdi->die_parent != NULL)
13918 for (child_pdi = struct_pdi->die_child;
13920 child_pdi = child_pdi->die_sibling)
13922 if (child_pdi->tag == DW_TAG_subprogram
13923 && child_pdi->linkage_name != NULL)
13925 char *actual_class_name
13926 = language_class_name_from_physname (cu->language_defn,
13927 child_pdi->linkage_name);
13928 if (actual_class_name != NULL)
13931 = obstack_copy0 (&cu->objfile->objfile_obstack,
13933 strlen (actual_class_name));
13934 xfree (actual_class_name);
13941 /* Adjust PART_DIE before generating a symbol for it. This function
13942 may set the is_external flag or change the DIE's name. */
13945 fixup_partial_die (struct partial_die_info *part_die,
13946 struct dwarf2_cu *cu)
13948 /* Once we've fixed up a die, there's no point in doing so again.
13949 This also avoids a memory leak if we were to call
13950 guess_partial_die_structure_name multiple times. */
13951 if (part_die->fixup_called)
13954 /* If we found a reference attribute and the DIE has no name, try
13955 to find a name in the referred to DIE. */
13957 if (part_die->name == NULL && part_die->has_specification)
13959 struct partial_die_info *spec_die;
13961 spec_die = find_partial_die (part_die->spec_offset,
13962 part_die->spec_is_dwz, cu);
13964 fixup_partial_die (spec_die, cu);
13966 if (spec_die->name)
13968 part_die->name = spec_die->name;
13970 /* Copy DW_AT_external attribute if it is set. */
13971 if (spec_die->is_external)
13972 part_die->is_external = spec_die->is_external;
13976 /* Set default names for some unnamed DIEs. */
13978 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13979 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13981 /* If there is no parent die to provide a namespace, and there are
13982 children, see if we can determine the namespace from their linkage
13984 if (cu->language == language_cplus
13985 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13986 && part_die->die_parent == NULL
13987 && part_die->has_children
13988 && (part_die->tag == DW_TAG_class_type
13989 || part_die->tag == DW_TAG_structure_type
13990 || part_die->tag == DW_TAG_union_type))
13991 guess_partial_die_structure_name (part_die, cu);
13993 /* GCC might emit a nameless struct or union that has a linkage
13994 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13995 if (part_die->name == NULL
13996 && (part_die->tag == DW_TAG_class_type
13997 || part_die->tag == DW_TAG_interface_type
13998 || part_die->tag == DW_TAG_structure_type
13999 || part_die->tag == DW_TAG_union_type)
14000 && part_die->linkage_name != NULL)
14004 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14009 /* Strip any leading namespaces/classes, keep only the base name.
14010 DW_AT_name for named DIEs does not contain the prefixes. */
14011 base = strrchr (demangled, ':');
14012 if (base && base > demangled && base[-1] == ':')
14017 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14018 base, strlen (base));
14023 part_die->fixup_called = 1;
14026 /* Read an attribute value described by an attribute form. */
14029 read_attribute_value (const struct die_reader_specs *reader,
14030 struct attribute *attr, unsigned form,
14031 gdb_byte *info_ptr)
14033 struct dwarf2_cu *cu = reader->cu;
14034 bfd *abfd = reader->abfd;
14035 struct comp_unit_head *cu_header = &cu->header;
14036 unsigned int bytes_read;
14037 struct dwarf_block *blk;
14042 case DW_FORM_ref_addr:
14043 if (cu->header.version == 2)
14044 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14046 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14047 &cu->header, &bytes_read);
14048 info_ptr += bytes_read;
14050 case DW_FORM_GNU_ref_alt:
14051 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14052 info_ptr += bytes_read;
14055 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14056 info_ptr += bytes_read;
14058 case DW_FORM_block2:
14059 blk = dwarf_alloc_block (cu);
14060 blk->size = read_2_bytes (abfd, info_ptr);
14062 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14063 info_ptr += blk->size;
14064 DW_BLOCK (attr) = blk;
14066 case DW_FORM_block4:
14067 blk = dwarf_alloc_block (cu);
14068 blk->size = read_4_bytes (abfd, info_ptr);
14070 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14071 info_ptr += blk->size;
14072 DW_BLOCK (attr) = blk;
14074 case DW_FORM_data2:
14075 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14078 case DW_FORM_data4:
14079 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14082 case DW_FORM_data8:
14083 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14086 case DW_FORM_sec_offset:
14087 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14088 info_ptr += bytes_read;
14090 case DW_FORM_string:
14091 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14092 DW_STRING_IS_CANONICAL (attr) = 0;
14093 info_ptr += bytes_read;
14096 if (!cu->per_cu->is_dwz)
14098 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14100 DW_STRING_IS_CANONICAL (attr) = 0;
14101 info_ptr += bytes_read;
14105 case DW_FORM_GNU_strp_alt:
14107 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14108 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14111 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14112 DW_STRING_IS_CANONICAL (attr) = 0;
14113 info_ptr += bytes_read;
14116 case DW_FORM_exprloc:
14117 case DW_FORM_block:
14118 blk = dwarf_alloc_block (cu);
14119 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14120 info_ptr += bytes_read;
14121 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14122 info_ptr += blk->size;
14123 DW_BLOCK (attr) = blk;
14125 case DW_FORM_block1:
14126 blk = dwarf_alloc_block (cu);
14127 blk->size = read_1_byte (abfd, info_ptr);
14129 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14130 info_ptr += blk->size;
14131 DW_BLOCK (attr) = blk;
14133 case DW_FORM_data1:
14134 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14138 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14141 case DW_FORM_flag_present:
14142 DW_UNSND (attr) = 1;
14144 case DW_FORM_sdata:
14145 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14146 info_ptr += bytes_read;
14148 case DW_FORM_udata:
14149 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14150 info_ptr += bytes_read;
14153 DW_UNSND (attr) = (cu->header.offset.sect_off
14154 + read_1_byte (abfd, info_ptr));
14158 DW_UNSND (attr) = (cu->header.offset.sect_off
14159 + read_2_bytes (abfd, info_ptr));
14163 DW_UNSND (attr) = (cu->header.offset.sect_off
14164 + read_4_bytes (abfd, info_ptr));
14168 DW_UNSND (attr) = (cu->header.offset.sect_off
14169 + read_8_bytes (abfd, info_ptr));
14172 case DW_FORM_ref_sig8:
14173 /* Convert the signature to something we can record in DW_UNSND
14175 NOTE: This is NULL if the type wasn't found. */
14176 DW_SIGNATURED_TYPE (attr) =
14177 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14180 case DW_FORM_ref_udata:
14181 DW_UNSND (attr) = (cu->header.offset.sect_off
14182 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14183 info_ptr += bytes_read;
14185 case DW_FORM_indirect:
14186 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14187 info_ptr += bytes_read;
14188 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14190 case DW_FORM_GNU_addr_index:
14191 if (reader->dwo_file == NULL)
14193 /* For now flag a hard error.
14194 Later we can turn this into a complaint. */
14195 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14196 dwarf_form_name (form),
14197 bfd_get_filename (abfd));
14199 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14200 info_ptr += bytes_read;
14202 case DW_FORM_GNU_str_index:
14203 if (reader->dwo_file == NULL)
14205 /* For now flag a hard error.
14206 Later we can turn this into a complaint if warranted. */
14207 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14208 dwarf_form_name (form),
14209 bfd_get_filename (abfd));
14212 ULONGEST str_index =
14213 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14215 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14216 DW_STRING_IS_CANONICAL (attr) = 0;
14217 info_ptr += bytes_read;
14221 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14222 dwarf_form_name (form),
14223 bfd_get_filename (abfd));
14227 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14228 attr->form = DW_FORM_GNU_ref_alt;
14230 /* We have seen instances where the compiler tried to emit a byte
14231 size attribute of -1 which ended up being encoded as an unsigned
14232 0xffffffff. Although 0xffffffff is technically a valid size value,
14233 an object of this size seems pretty unlikely so we can relatively
14234 safely treat these cases as if the size attribute was invalid and
14235 treat them as zero by default. */
14236 if (attr->name == DW_AT_byte_size
14237 && form == DW_FORM_data4
14238 && DW_UNSND (attr) >= 0xffffffff)
14241 (&symfile_complaints,
14242 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14243 hex_string (DW_UNSND (attr)));
14244 DW_UNSND (attr) = 0;
14250 /* Read an attribute described by an abbreviated attribute. */
14253 read_attribute (const struct die_reader_specs *reader,
14254 struct attribute *attr, struct attr_abbrev *abbrev,
14255 gdb_byte *info_ptr)
14257 attr->name = abbrev->name;
14258 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14261 /* Read dwarf information from a buffer. */
14263 static unsigned int
14264 read_1_byte (bfd *abfd, const gdb_byte *buf)
14266 return bfd_get_8 (abfd, buf);
14270 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14272 return bfd_get_signed_8 (abfd, buf);
14275 static unsigned int
14276 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14278 return bfd_get_16 (abfd, buf);
14282 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14284 return bfd_get_signed_16 (abfd, buf);
14287 static unsigned int
14288 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14290 return bfd_get_32 (abfd, buf);
14294 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14296 return bfd_get_signed_32 (abfd, buf);
14300 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14302 return bfd_get_64 (abfd, buf);
14306 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14307 unsigned int *bytes_read)
14309 struct comp_unit_head *cu_header = &cu->header;
14310 CORE_ADDR retval = 0;
14312 if (cu_header->signed_addr_p)
14314 switch (cu_header->addr_size)
14317 retval = bfd_get_signed_16 (abfd, buf);
14320 retval = bfd_get_signed_32 (abfd, buf);
14323 retval = bfd_get_signed_64 (abfd, buf);
14326 internal_error (__FILE__, __LINE__,
14327 _("read_address: bad switch, signed [in module %s]"),
14328 bfd_get_filename (abfd));
14333 switch (cu_header->addr_size)
14336 retval = bfd_get_16 (abfd, buf);
14339 retval = bfd_get_32 (abfd, buf);
14342 retval = bfd_get_64 (abfd, buf);
14345 internal_error (__FILE__, __LINE__,
14346 _("read_address: bad switch, "
14347 "unsigned [in module %s]"),
14348 bfd_get_filename (abfd));
14352 *bytes_read = cu_header->addr_size;
14356 /* Read the initial length from a section. The (draft) DWARF 3
14357 specification allows the initial length to take up either 4 bytes
14358 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14359 bytes describe the length and all offsets will be 8 bytes in length
14362 An older, non-standard 64-bit format is also handled by this
14363 function. The older format in question stores the initial length
14364 as an 8-byte quantity without an escape value. Lengths greater
14365 than 2^32 aren't very common which means that the initial 4 bytes
14366 is almost always zero. Since a length value of zero doesn't make
14367 sense for the 32-bit format, this initial zero can be considered to
14368 be an escape value which indicates the presence of the older 64-bit
14369 format. As written, the code can't detect (old format) lengths
14370 greater than 4GB. If it becomes necessary to handle lengths
14371 somewhat larger than 4GB, we could allow other small values (such
14372 as the non-sensical values of 1, 2, and 3) to also be used as
14373 escape values indicating the presence of the old format.
14375 The value returned via bytes_read should be used to increment the
14376 relevant pointer after calling read_initial_length().
14378 [ Note: read_initial_length() and read_offset() are based on the
14379 document entitled "DWARF Debugging Information Format", revision
14380 3, draft 8, dated November 19, 2001. This document was obtained
14383 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14385 This document is only a draft and is subject to change. (So beware.)
14387 Details regarding the older, non-standard 64-bit format were
14388 determined empirically by examining 64-bit ELF files produced by
14389 the SGI toolchain on an IRIX 6.5 machine.
14391 - Kevin, July 16, 2002
14395 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14397 LONGEST length = bfd_get_32 (abfd, buf);
14399 if (length == 0xffffffff)
14401 length = bfd_get_64 (abfd, buf + 4);
14404 else if (length == 0)
14406 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14407 length = bfd_get_64 (abfd, buf);
14418 /* Cover function for read_initial_length.
14419 Returns the length of the object at BUF, and stores the size of the
14420 initial length in *BYTES_READ and stores the size that offsets will be in
14422 If the initial length size is not equivalent to that specified in
14423 CU_HEADER then issue a complaint.
14424 This is useful when reading non-comp-unit headers. */
14427 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14428 const struct comp_unit_head *cu_header,
14429 unsigned int *bytes_read,
14430 unsigned int *offset_size)
14432 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14434 gdb_assert (cu_header->initial_length_size == 4
14435 || cu_header->initial_length_size == 8
14436 || cu_header->initial_length_size == 12);
14438 if (cu_header->initial_length_size != *bytes_read)
14439 complaint (&symfile_complaints,
14440 _("intermixed 32-bit and 64-bit DWARF sections"));
14442 *offset_size = (*bytes_read == 4) ? 4 : 8;
14446 /* Read an offset from the data stream. The size of the offset is
14447 given by cu_header->offset_size. */
14450 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14451 unsigned int *bytes_read)
14453 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14455 *bytes_read = cu_header->offset_size;
14459 /* Read an offset from the data stream. */
14462 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14464 LONGEST retval = 0;
14466 switch (offset_size)
14469 retval = bfd_get_32 (abfd, buf);
14472 retval = bfd_get_64 (abfd, buf);
14475 internal_error (__FILE__, __LINE__,
14476 _("read_offset_1: bad switch [in module %s]"),
14477 bfd_get_filename (abfd));
14484 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14486 /* If the size of a host char is 8 bits, we can return a pointer
14487 to the buffer, otherwise we have to copy the data to a buffer
14488 allocated on the temporary obstack. */
14489 gdb_assert (HOST_CHAR_BIT == 8);
14494 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14496 /* If the size of a host char is 8 bits, we can return a pointer
14497 to the string, otherwise we have to copy the string to a buffer
14498 allocated on the temporary obstack. */
14499 gdb_assert (HOST_CHAR_BIT == 8);
14502 *bytes_read_ptr = 1;
14505 *bytes_read_ptr = strlen ((char *) buf) + 1;
14506 return (char *) buf;
14510 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14512 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14513 if (dwarf2_per_objfile->str.buffer == NULL)
14514 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14515 bfd_get_filename (abfd));
14516 if (str_offset >= dwarf2_per_objfile->str.size)
14517 error (_("DW_FORM_strp pointing outside of "
14518 ".debug_str section [in module %s]"),
14519 bfd_get_filename (abfd));
14520 gdb_assert (HOST_CHAR_BIT == 8);
14521 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14523 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14526 /* Read a string at offset STR_OFFSET in the .debug_str section from
14527 the .dwz file DWZ. Throw an error if the offset is too large. If
14528 the string consists of a single NUL byte, return NULL; otherwise
14529 return a pointer to the string. */
14532 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14534 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14536 if (dwz->str.buffer == NULL)
14537 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14538 "section [in module %s]"),
14539 bfd_get_filename (dwz->dwz_bfd));
14540 if (str_offset >= dwz->str.size)
14541 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14542 ".debug_str section [in module %s]"),
14543 bfd_get_filename (dwz->dwz_bfd));
14544 gdb_assert (HOST_CHAR_BIT == 8);
14545 if (dwz->str.buffer[str_offset] == '\0')
14547 return (char *) (dwz->str.buffer + str_offset);
14551 read_indirect_string (bfd *abfd, gdb_byte *buf,
14552 const struct comp_unit_head *cu_header,
14553 unsigned int *bytes_read_ptr)
14555 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14557 return read_indirect_string_at_offset (abfd, str_offset);
14561 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14564 unsigned int num_read;
14566 unsigned char byte;
14574 byte = bfd_get_8 (abfd, buf);
14577 result |= ((ULONGEST) (byte & 127) << shift);
14578 if ((byte & 128) == 0)
14584 *bytes_read_ptr = num_read;
14589 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14592 int i, shift, num_read;
14593 unsigned char byte;
14601 byte = bfd_get_8 (abfd, buf);
14604 result |= ((LONGEST) (byte & 127) << shift);
14606 if ((byte & 128) == 0)
14611 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14612 result |= -(((LONGEST) 1) << shift);
14613 *bytes_read_ptr = num_read;
14617 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14618 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14619 ADDR_SIZE is the size of addresses from the CU header. */
14622 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14624 struct objfile *objfile = dwarf2_per_objfile->objfile;
14625 bfd *abfd = objfile->obfd;
14626 const gdb_byte *info_ptr;
14628 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14629 if (dwarf2_per_objfile->addr.buffer == NULL)
14630 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14632 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14633 error (_("DW_FORM_addr_index pointing outside of "
14634 ".debug_addr section [in module %s]"),
14636 info_ptr = (dwarf2_per_objfile->addr.buffer
14637 + addr_base + addr_index * addr_size);
14638 if (addr_size == 4)
14639 return bfd_get_32 (abfd, info_ptr);
14641 return bfd_get_64 (abfd, info_ptr);
14644 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14647 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14649 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14652 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14655 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14656 unsigned int *bytes_read)
14658 bfd *abfd = cu->objfile->obfd;
14659 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14661 return read_addr_index (cu, addr_index);
14664 /* Data structure to pass results from dwarf2_read_addr_index_reader
14665 back to dwarf2_read_addr_index. */
14667 struct dwarf2_read_addr_index_data
14669 ULONGEST addr_base;
14673 /* die_reader_func for dwarf2_read_addr_index. */
14676 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14677 gdb_byte *info_ptr,
14678 struct die_info *comp_unit_die,
14682 struct dwarf2_cu *cu = reader->cu;
14683 struct dwarf2_read_addr_index_data *aidata =
14684 (struct dwarf2_read_addr_index_data *) data;
14686 aidata->addr_base = cu->addr_base;
14687 aidata->addr_size = cu->header.addr_size;
14690 /* Given an index in .debug_addr, fetch the value.
14691 NOTE: This can be called during dwarf expression evaluation,
14692 long after the debug information has been read, and thus per_cu->cu
14693 may no longer exist. */
14696 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14697 unsigned int addr_index)
14699 struct objfile *objfile = per_cu->objfile;
14700 struct dwarf2_cu *cu = per_cu->cu;
14701 ULONGEST addr_base;
14704 /* This is intended to be called from outside this file. */
14705 dw2_setup (objfile);
14707 /* We need addr_base and addr_size.
14708 If we don't have PER_CU->cu, we have to get it.
14709 Nasty, but the alternative is storing the needed info in PER_CU,
14710 which at this point doesn't seem justified: it's not clear how frequently
14711 it would get used and it would increase the size of every PER_CU.
14712 Entry points like dwarf2_per_cu_addr_size do a similar thing
14713 so we're not in uncharted territory here.
14714 Alas we need to be a bit more complicated as addr_base is contained
14717 We don't need to read the entire CU(/TU).
14718 We just need the header and top level die.
14720 IWBN to use the aging mechanism to let us lazily later discard the CU.
14721 For now we skip this optimization. */
14725 addr_base = cu->addr_base;
14726 addr_size = cu->header.addr_size;
14730 struct dwarf2_read_addr_index_data aidata;
14732 /* Note: We can't use init_cutu_and_read_dies_simple here,
14733 we need addr_base. */
14734 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14735 dwarf2_read_addr_index_reader, &aidata);
14736 addr_base = aidata.addr_base;
14737 addr_size = aidata.addr_size;
14740 return read_addr_index_1 (addr_index, addr_base, addr_size);
14743 /* Given a DW_AT_str_index, fetch the string. */
14746 read_str_index (const struct die_reader_specs *reader,
14747 struct dwarf2_cu *cu, ULONGEST str_index)
14749 struct objfile *objfile = dwarf2_per_objfile->objfile;
14750 const char *dwo_name = objfile->name;
14751 bfd *abfd = objfile->obfd;
14752 struct dwo_sections *sections = &reader->dwo_file->sections;
14753 gdb_byte *info_ptr;
14754 ULONGEST str_offset;
14756 dwarf2_read_section (objfile, §ions->str);
14757 dwarf2_read_section (objfile, §ions->str_offsets);
14758 if (sections->str.buffer == NULL)
14759 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14760 " in CU at offset 0x%lx [in module %s]"),
14761 (long) cu->header.offset.sect_off, dwo_name);
14762 if (sections->str_offsets.buffer == NULL)
14763 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14764 " in CU at offset 0x%lx [in module %s]"),
14765 (long) cu->header.offset.sect_off, dwo_name);
14766 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14767 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14768 " section in CU at offset 0x%lx [in module %s]"),
14769 (long) cu->header.offset.sect_off, dwo_name);
14770 info_ptr = (sections->str_offsets.buffer
14771 + str_index * cu->header.offset_size);
14772 if (cu->header.offset_size == 4)
14773 str_offset = bfd_get_32 (abfd, info_ptr);
14775 str_offset = bfd_get_64 (abfd, info_ptr);
14776 if (str_offset >= sections->str.size)
14777 error (_("Offset from DW_FORM_str_index pointing outside of"
14778 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14779 (long) cu->header.offset.sect_off, dwo_name);
14780 return (char *) (sections->str.buffer + str_offset);
14783 /* Return the length of an LEB128 number in BUF. */
14786 leb128_size (const gdb_byte *buf)
14788 const gdb_byte *begin = buf;
14794 if ((byte & 128) == 0)
14795 return buf - begin;
14800 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14807 cu->language = language_c;
14809 case DW_LANG_C_plus_plus:
14810 cu->language = language_cplus;
14813 cu->language = language_d;
14815 case DW_LANG_Fortran77:
14816 case DW_LANG_Fortran90:
14817 case DW_LANG_Fortran95:
14818 cu->language = language_fortran;
14821 cu->language = language_go;
14823 case DW_LANG_Mips_Assembler:
14824 cu->language = language_asm;
14827 cu->language = language_java;
14829 case DW_LANG_Ada83:
14830 case DW_LANG_Ada95:
14831 cu->language = language_ada;
14833 case DW_LANG_Modula2:
14834 cu->language = language_m2;
14836 case DW_LANG_Pascal83:
14837 cu->language = language_pascal;
14840 cu->language = language_objc;
14842 case DW_LANG_Cobol74:
14843 case DW_LANG_Cobol85:
14845 cu->language = language_minimal;
14848 cu->language_defn = language_def (cu->language);
14851 /* Return the named attribute or NULL if not there. */
14853 static struct attribute *
14854 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14859 struct attribute *spec = NULL;
14861 for (i = 0; i < die->num_attrs; ++i)
14863 if (die->attrs[i].name == name)
14864 return &die->attrs[i];
14865 if (die->attrs[i].name == DW_AT_specification
14866 || die->attrs[i].name == DW_AT_abstract_origin)
14867 spec = &die->attrs[i];
14873 die = follow_die_ref (die, spec, &cu);
14879 /* Return the named attribute or NULL if not there,
14880 but do not follow DW_AT_specification, etc.
14881 This is for use in contexts where we're reading .debug_types dies.
14882 Following DW_AT_specification, DW_AT_abstract_origin will take us
14883 back up the chain, and we want to go down. */
14885 static struct attribute *
14886 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14890 for (i = 0; i < die->num_attrs; ++i)
14891 if (die->attrs[i].name == name)
14892 return &die->attrs[i];
14897 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14898 and holds a non-zero value. This function should only be used for
14899 DW_FORM_flag or DW_FORM_flag_present attributes. */
14902 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14904 struct attribute *attr = dwarf2_attr (die, name, cu);
14906 return (attr && DW_UNSND (attr));
14910 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14912 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14913 which value is non-zero. However, we have to be careful with
14914 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14915 (via dwarf2_flag_true_p) follows this attribute. So we may
14916 end up accidently finding a declaration attribute that belongs
14917 to a different DIE referenced by the specification attribute,
14918 even though the given DIE does not have a declaration attribute. */
14919 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14920 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14923 /* Return the die giving the specification for DIE, if there is
14924 one. *SPEC_CU is the CU containing DIE on input, and the CU
14925 containing the return value on output. If there is no
14926 specification, but there is an abstract origin, that is
14929 static struct die_info *
14930 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14932 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14935 if (spec_attr == NULL)
14936 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14938 if (spec_attr == NULL)
14941 return follow_die_ref (die, spec_attr, spec_cu);
14944 /* Free the line_header structure *LH, and any arrays and strings it
14946 NOTE: This is also used as a "cleanup" function. */
14949 free_line_header (struct line_header *lh)
14951 if (lh->standard_opcode_lengths)
14952 xfree (lh->standard_opcode_lengths);
14954 /* Remember that all the lh->file_names[i].name pointers are
14955 pointers into debug_line_buffer, and don't need to be freed. */
14956 if (lh->file_names)
14957 xfree (lh->file_names);
14959 /* Similarly for the include directory names. */
14960 if (lh->include_dirs)
14961 xfree (lh->include_dirs);
14966 /* Add an entry to LH's include directory table. */
14969 add_include_dir (struct line_header *lh, char *include_dir)
14971 /* Grow the array if necessary. */
14972 if (lh->include_dirs_size == 0)
14974 lh->include_dirs_size = 1; /* for testing */
14975 lh->include_dirs = xmalloc (lh->include_dirs_size
14976 * sizeof (*lh->include_dirs));
14978 else if (lh->num_include_dirs >= lh->include_dirs_size)
14980 lh->include_dirs_size *= 2;
14981 lh->include_dirs = xrealloc (lh->include_dirs,
14982 (lh->include_dirs_size
14983 * sizeof (*lh->include_dirs)));
14986 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14989 /* Add an entry to LH's file name table. */
14992 add_file_name (struct line_header *lh,
14994 unsigned int dir_index,
14995 unsigned int mod_time,
14996 unsigned int length)
14998 struct file_entry *fe;
15000 /* Grow the array if necessary. */
15001 if (lh->file_names_size == 0)
15003 lh->file_names_size = 1; /* for testing */
15004 lh->file_names = xmalloc (lh->file_names_size
15005 * sizeof (*lh->file_names));
15007 else if (lh->num_file_names >= lh->file_names_size)
15009 lh->file_names_size *= 2;
15010 lh->file_names = xrealloc (lh->file_names,
15011 (lh->file_names_size
15012 * sizeof (*lh->file_names)));
15015 fe = &lh->file_names[lh->num_file_names++];
15017 fe->dir_index = dir_index;
15018 fe->mod_time = mod_time;
15019 fe->length = length;
15020 fe->included_p = 0;
15024 /* A convenience function to find the proper .debug_line section for a
15027 static struct dwarf2_section_info *
15028 get_debug_line_section (struct dwarf2_cu *cu)
15030 struct dwarf2_section_info *section;
15032 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15034 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15035 section = &cu->dwo_unit->dwo_file->sections.line;
15036 else if (cu->per_cu->is_dwz)
15038 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15040 section = &dwz->line;
15043 section = &dwarf2_per_objfile->line;
15048 /* Read the statement program header starting at OFFSET in
15049 .debug_line, or .debug_line.dwo. Return a pointer
15050 to a struct line_header, allocated using xmalloc.
15052 NOTE: the strings in the include directory and file name tables of
15053 the returned object point into the dwarf line section buffer,
15054 and must not be freed. */
15056 static struct line_header *
15057 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15059 struct cleanup *back_to;
15060 struct line_header *lh;
15061 gdb_byte *line_ptr;
15062 unsigned int bytes_read, offset_size;
15064 char *cur_dir, *cur_file;
15065 struct dwarf2_section_info *section;
15068 section = get_debug_line_section (cu);
15069 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15070 if (section->buffer == NULL)
15072 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15073 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15075 complaint (&symfile_complaints, _("missing .debug_line section"));
15079 /* We can't do this until we know the section is non-empty.
15080 Only then do we know we have such a section. */
15081 abfd = section->asection->owner;
15083 /* Make sure that at least there's room for the total_length field.
15084 That could be 12 bytes long, but we're just going to fudge that. */
15085 if (offset + 4 >= section->size)
15087 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15091 lh = xmalloc (sizeof (*lh));
15092 memset (lh, 0, sizeof (*lh));
15093 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15096 line_ptr = section->buffer + offset;
15098 /* Read in the header. */
15100 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15101 &bytes_read, &offset_size);
15102 line_ptr += bytes_read;
15103 if (line_ptr + lh->total_length > (section->buffer + section->size))
15105 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15108 lh->statement_program_end = line_ptr + lh->total_length;
15109 lh->version = read_2_bytes (abfd, line_ptr);
15111 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15112 line_ptr += offset_size;
15113 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15115 if (lh->version >= 4)
15117 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15121 lh->maximum_ops_per_instruction = 1;
15123 if (lh->maximum_ops_per_instruction == 0)
15125 lh->maximum_ops_per_instruction = 1;
15126 complaint (&symfile_complaints,
15127 _("invalid maximum_ops_per_instruction "
15128 "in `.debug_line' section"));
15131 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15133 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15135 lh->line_range = read_1_byte (abfd, line_ptr);
15137 lh->opcode_base = read_1_byte (abfd, line_ptr);
15139 lh->standard_opcode_lengths
15140 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15142 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15143 for (i = 1; i < lh->opcode_base; ++i)
15145 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15149 /* Read directory table. */
15150 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15152 line_ptr += bytes_read;
15153 add_include_dir (lh, cur_dir);
15155 line_ptr += bytes_read;
15157 /* Read file name table. */
15158 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15160 unsigned int dir_index, mod_time, length;
15162 line_ptr += bytes_read;
15163 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15164 line_ptr += bytes_read;
15165 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15166 line_ptr += bytes_read;
15167 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15168 line_ptr += bytes_read;
15170 add_file_name (lh, cur_file, dir_index, mod_time, length);
15172 line_ptr += bytes_read;
15173 lh->statement_program_start = line_ptr;
15175 if (line_ptr > (section->buffer + section->size))
15176 complaint (&symfile_complaints,
15177 _("line number info header doesn't "
15178 "fit in `.debug_line' section"));
15180 discard_cleanups (back_to);
15184 /* Subroutine of dwarf_decode_lines to simplify it.
15185 Return the file name of the psymtab for included file FILE_INDEX
15186 in line header LH of PST.
15187 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15188 If space for the result is malloc'd, it will be freed by a cleanup.
15189 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15191 The function creates dangling cleanup registration. */
15194 psymtab_include_file_name (const struct line_header *lh, int file_index,
15195 const struct partial_symtab *pst,
15196 const char *comp_dir)
15198 const struct file_entry fe = lh->file_names [file_index];
15199 char *include_name = fe.name;
15200 char *include_name_to_compare = include_name;
15201 char *dir_name = NULL;
15202 const char *pst_filename;
15203 char *copied_name = NULL;
15207 dir_name = lh->include_dirs[fe.dir_index - 1];
15209 if (!IS_ABSOLUTE_PATH (include_name)
15210 && (dir_name != NULL || comp_dir != NULL))
15212 /* Avoid creating a duplicate psymtab for PST.
15213 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15214 Before we do the comparison, however, we need to account
15215 for DIR_NAME and COMP_DIR.
15216 First prepend dir_name (if non-NULL). If we still don't
15217 have an absolute path prepend comp_dir (if non-NULL).
15218 However, the directory we record in the include-file's
15219 psymtab does not contain COMP_DIR (to match the
15220 corresponding symtab(s)).
15225 bash$ gcc -g ./hello.c
15226 include_name = "hello.c"
15228 DW_AT_comp_dir = comp_dir = "/tmp"
15229 DW_AT_name = "./hello.c" */
15231 if (dir_name != NULL)
15233 include_name = concat (dir_name, SLASH_STRING,
15234 include_name, (char *)NULL);
15235 include_name_to_compare = include_name;
15236 make_cleanup (xfree, include_name);
15238 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15240 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15241 include_name, (char *)NULL);
15245 pst_filename = pst->filename;
15246 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15248 copied_name = concat (pst->dirname, SLASH_STRING,
15249 pst_filename, (char *)NULL);
15250 pst_filename = copied_name;
15253 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15255 if (include_name_to_compare != include_name)
15256 xfree (include_name_to_compare);
15257 if (copied_name != NULL)
15258 xfree (copied_name);
15262 return include_name;
15265 /* Ignore this record_line request. */
15268 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15273 /* Subroutine of dwarf_decode_lines to simplify it.
15274 Process the line number information in LH. */
15277 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15278 struct dwarf2_cu *cu, struct partial_symtab *pst)
15280 gdb_byte *line_ptr, *extended_end;
15281 gdb_byte *line_end;
15282 unsigned int bytes_read, extended_len;
15283 unsigned char op_code, extended_op, adj_opcode;
15284 CORE_ADDR baseaddr;
15285 struct objfile *objfile = cu->objfile;
15286 bfd *abfd = objfile->obfd;
15287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15288 const int decode_for_pst_p = (pst != NULL);
15289 struct subfile *last_subfile = NULL;
15290 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15293 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15295 line_ptr = lh->statement_program_start;
15296 line_end = lh->statement_program_end;
15298 /* Read the statement sequences until there's nothing left. */
15299 while (line_ptr < line_end)
15301 /* state machine registers */
15302 CORE_ADDR address = 0;
15303 unsigned int file = 1;
15304 unsigned int line = 1;
15305 unsigned int column = 0;
15306 int is_stmt = lh->default_is_stmt;
15307 int basic_block = 0;
15308 int end_sequence = 0;
15310 unsigned char op_index = 0;
15312 if (!decode_for_pst_p && lh->num_file_names >= file)
15314 /* Start a subfile for the current file of the state machine. */
15315 /* lh->include_dirs and lh->file_names are 0-based, but the
15316 directory and file name numbers in the statement program
15318 struct file_entry *fe = &lh->file_names[file - 1];
15322 dir = lh->include_dirs[fe->dir_index - 1];
15324 dwarf2_start_subfile (fe->name, dir, comp_dir);
15327 /* Decode the table. */
15328 while (!end_sequence)
15330 op_code = read_1_byte (abfd, line_ptr);
15332 if (line_ptr > line_end)
15334 dwarf2_debug_line_missing_end_sequence_complaint ();
15338 if (op_code >= lh->opcode_base)
15340 /* Special operand. */
15341 adj_opcode = op_code - lh->opcode_base;
15342 address += (((op_index + (adj_opcode / lh->line_range))
15343 / lh->maximum_ops_per_instruction)
15344 * lh->minimum_instruction_length);
15345 op_index = ((op_index + (adj_opcode / lh->line_range))
15346 % lh->maximum_ops_per_instruction);
15347 line += lh->line_base + (adj_opcode % lh->line_range);
15348 if (lh->num_file_names < file || file == 0)
15349 dwarf2_debug_line_missing_file_complaint ();
15350 /* For now we ignore lines not starting on an
15351 instruction boundary. */
15352 else if (op_index == 0)
15354 lh->file_names[file - 1].included_p = 1;
15355 if (!decode_for_pst_p && is_stmt)
15357 if (last_subfile != current_subfile)
15359 addr = gdbarch_addr_bits_remove (gdbarch, address);
15361 (*p_record_line) (last_subfile, 0, addr);
15362 last_subfile = current_subfile;
15364 /* Append row to matrix using current values. */
15365 addr = gdbarch_addr_bits_remove (gdbarch, address);
15366 (*p_record_line) (current_subfile, line, addr);
15371 else switch (op_code)
15373 case DW_LNS_extended_op:
15374 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15376 line_ptr += bytes_read;
15377 extended_end = line_ptr + extended_len;
15378 extended_op = read_1_byte (abfd, line_ptr);
15380 switch (extended_op)
15382 case DW_LNE_end_sequence:
15383 p_record_line = record_line;
15386 case DW_LNE_set_address:
15387 address = read_address (abfd, line_ptr, cu, &bytes_read);
15389 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15391 /* This line table is for a function which has been
15392 GCd by the linker. Ignore it. PR gdb/12528 */
15395 = line_ptr - get_debug_line_section (cu)->buffer;
15397 complaint (&symfile_complaints,
15398 _(".debug_line address at offset 0x%lx is 0 "
15400 line_offset, objfile->name);
15401 p_record_line = noop_record_line;
15405 line_ptr += bytes_read;
15406 address += baseaddr;
15408 case DW_LNE_define_file:
15411 unsigned int dir_index, mod_time, length;
15413 cur_file = read_direct_string (abfd, line_ptr,
15415 line_ptr += bytes_read;
15417 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15418 line_ptr += bytes_read;
15420 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15421 line_ptr += bytes_read;
15423 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15424 line_ptr += bytes_read;
15425 add_file_name (lh, cur_file, dir_index, mod_time, length);
15428 case DW_LNE_set_discriminator:
15429 /* The discriminator is not interesting to the debugger;
15431 line_ptr = extended_end;
15434 complaint (&symfile_complaints,
15435 _("mangled .debug_line section"));
15438 /* Make sure that we parsed the extended op correctly. If e.g.
15439 we expected a different address size than the producer used,
15440 we may have read the wrong number of bytes. */
15441 if (line_ptr != extended_end)
15443 complaint (&symfile_complaints,
15444 _("mangled .debug_line section"));
15449 if (lh->num_file_names < file || file == 0)
15450 dwarf2_debug_line_missing_file_complaint ();
15453 lh->file_names[file - 1].included_p = 1;
15454 if (!decode_for_pst_p && is_stmt)
15456 if (last_subfile != current_subfile)
15458 addr = gdbarch_addr_bits_remove (gdbarch, address);
15460 (*p_record_line) (last_subfile, 0, addr);
15461 last_subfile = current_subfile;
15463 addr = gdbarch_addr_bits_remove (gdbarch, address);
15464 (*p_record_line) (current_subfile, line, addr);
15469 case DW_LNS_advance_pc:
15472 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15474 address += (((op_index + adjust)
15475 / lh->maximum_ops_per_instruction)
15476 * lh->minimum_instruction_length);
15477 op_index = ((op_index + adjust)
15478 % lh->maximum_ops_per_instruction);
15479 line_ptr += bytes_read;
15482 case DW_LNS_advance_line:
15483 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15484 line_ptr += bytes_read;
15486 case DW_LNS_set_file:
15488 /* The arrays lh->include_dirs and lh->file_names are
15489 0-based, but the directory and file name numbers in
15490 the statement program are 1-based. */
15491 struct file_entry *fe;
15494 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15495 line_ptr += bytes_read;
15496 if (lh->num_file_names < file || file == 0)
15497 dwarf2_debug_line_missing_file_complaint ();
15500 fe = &lh->file_names[file - 1];
15502 dir = lh->include_dirs[fe->dir_index - 1];
15503 if (!decode_for_pst_p)
15505 last_subfile = current_subfile;
15506 dwarf2_start_subfile (fe->name, dir, comp_dir);
15511 case DW_LNS_set_column:
15512 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15513 line_ptr += bytes_read;
15515 case DW_LNS_negate_stmt:
15516 is_stmt = (!is_stmt);
15518 case DW_LNS_set_basic_block:
15521 /* Add to the address register of the state machine the
15522 address increment value corresponding to special opcode
15523 255. I.e., this value is scaled by the minimum
15524 instruction length since special opcode 255 would have
15525 scaled the increment. */
15526 case DW_LNS_const_add_pc:
15528 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15530 address += (((op_index + adjust)
15531 / lh->maximum_ops_per_instruction)
15532 * lh->minimum_instruction_length);
15533 op_index = ((op_index + adjust)
15534 % lh->maximum_ops_per_instruction);
15537 case DW_LNS_fixed_advance_pc:
15538 address += read_2_bytes (abfd, line_ptr);
15544 /* Unknown standard opcode, ignore it. */
15547 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15549 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15550 line_ptr += bytes_read;
15555 if (lh->num_file_names < file || file == 0)
15556 dwarf2_debug_line_missing_file_complaint ();
15559 lh->file_names[file - 1].included_p = 1;
15560 if (!decode_for_pst_p)
15562 addr = gdbarch_addr_bits_remove (gdbarch, address);
15563 (*p_record_line) (current_subfile, 0, addr);
15569 /* Decode the Line Number Program (LNP) for the given line_header
15570 structure and CU. The actual information extracted and the type
15571 of structures created from the LNP depends on the value of PST.
15573 1. If PST is NULL, then this procedure uses the data from the program
15574 to create all necessary symbol tables, and their linetables.
15576 2. If PST is not NULL, this procedure reads the program to determine
15577 the list of files included by the unit represented by PST, and
15578 builds all the associated partial symbol tables.
15580 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15581 It is used for relative paths in the line table.
15582 NOTE: When processing partial symtabs (pst != NULL),
15583 comp_dir == pst->dirname.
15585 NOTE: It is important that psymtabs have the same file name (via strcmp)
15586 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15587 symtab we don't use it in the name of the psymtabs we create.
15588 E.g. expand_line_sal requires this when finding psymtabs to expand.
15589 A good testcase for this is mb-inline.exp. */
15592 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15593 struct dwarf2_cu *cu, struct partial_symtab *pst,
15594 int want_line_info)
15596 struct objfile *objfile = cu->objfile;
15597 const int decode_for_pst_p = (pst != NULL);
15598 struct subfile *first_subfile = current_subfile;
15600 if (want_line_info)
15601 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15603 if (decode_for_pst_p)
15607 /* Now that we're done scanning the Line Header Program, we can
15608 create the psymtab of each included file. */
15609 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15610 if (lh->file_names[file_index].included_p == 1)
15612 char *include_name =
15613 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15614 if (include_name != NULL)
15615 dwarf2_create_include_psymtab (include_name, pst, objfile);
15620 /* Make sure a symtab is created for every file, even files
15621 which contain only variables (i.e. no code with associated
15625 for (i = 0; i < lh->num_file_names; i++)
15628 struct file_entry *fe;
15630 fe = &lh->file_names[i];
15632 dir = lh->include_dirs[fe->dir_index - 1];
15633 dwarf2_start_subfile (fe->name, dir, comp_dir);
15635 /* Skip the main file; we don't need it, and it must be
15636 allocated last, so that it will show up before the
15637 non-primary symtabs in the objfile's symtab list. */
15638 if (current_subfile == first_subfile)
15641 if (current_subfile->symtab == NULL)
15642 current_subfile->symtab = allocate_symtab (current_subfile->name,
15644 fe->symtab = current_subfile->symtab;
15649 /* Start a subfile for DWARF. FILENAME is the name of the file and
15650 DIRNAME the name of the source directory which contains FILENAME
15651 or NULL if not known. COMP_DIR is the compilation directory for the
15652 linetable's compilation unit or NULL if not known.
15653 This routine tries to keep line numbers from identical absolute and
15654 relative file names in a common subfile.
15656 Using the `list' example from the GDB testsuite, which resides in
15657 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15658 of /srcdir/list0.c yields the following debugging information for list0.c:
15660 DW_AT_name: /srcdir/list0.c
15661 DW_AT_comp_dir: /compdir
15662 files.files[0].name: list0.h
15663 files.files[0].dir: /srcdir
15664 files.files[1].name: list0.c
15665 files.files[1].dir: /srcdir
15667 The line number information for list0.c has to end up in a single
15668 subfile, so that `break /srcdir/list0.c:1' works as expected.
15669 start_subfile will ensure that this happens provided that we pass the
15670 concatenation of files.files[1].dir and files.files[1].name as the
15674 dwarf2_start_subfile (char *filename, const char *dirname,
15675 const char *comp_dir)
15679 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15680 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15681 second argument to start_subfile. To be consistent, we do the
15682 same here. In order not to lose the line information directory,
15683 we concatenate it to the filename when it makes sense.
15684 Note that the Dwarf3 standard says (speaking of filenames in line
15685 information): ``The directory index is ignored for file names
15686 that represent full path names''. Thus ignoring dirname in the
15687 `else' branch below isn't an issue. */
15689 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15690 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15692 fullname = filename;
15694 start_subfile (fullname, comp_dir);
15696 if (fullname != filename)
15700 /* Start a symtab for DWARF.
15701 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15704 dwarf2_start_symtab (struct dwarf2_cu *cu,
15705 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15707 start_symtab (name, comp_dir, low_pc);
15708 record_debugformat ("DWARF 2");
15709 record_producer (cu->producer);
15711 /* We assume that we're processing GCC output. */
15712 processing_gcc_compilation = 2;
15714 cu->processing_has_namespace_info = 0;
15718 var_decode_location (struct attribute *attr, struct symbol *sym,
15719 struct dwarf2_cu *cu)
15721 struct objfile *objfile = cu->objfile;
15722 struct comp_unit_head *cu_header = &cu->header;
15724 /* NOTE drow/2003-01-30: There used to be a comment and some special
15725 code here to turn a symbol with DW_AT_external and a
15726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15728 with some versions of binutils) where shared libraries could have
15729 relocations against symbols in their debug information - the
15730 minimal symbol would have the right address, but the debug info
15731 would not. It's no longer necessary, because we will explicitly
15732 apply relocations when we read in the debug information now. */
15734 /* A DW_AT_location attribute with no contents indicates that a
15735 variable has been optimized away. */
15736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15742 /* Handle one degenerate form of location expression specially, to
15743 preserve GDB's previous behavior when section offsets are
15744 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15745 then mark this symbol as LOC_STATIC. */
15747 if (attr_form_is_block (attr)
15748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15750 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15751 && (DW_BLOCK (attr)->size
15752 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15754 unsigned int dummy;
15756 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15757 SYMBOL_VALUE_ADDRESS (sym) =
15758 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15760 SYMBOL_VALUE_ADDRESS (sym) =
15761 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15762 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15763 fixup_symbol_section (sym, objfile);
15764 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15765 SYMBOL_SECTION (sym));
15769 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15770 expression evaluator, and use LOC_COMPUTED only when necessary
15771 (i.e. when the value of a register or memory location is
15772 referenced, or a thread-local block, etc.). Then again, it might
15773 not be worthwhile. I'm assuming that it isn't unless performance
15774 or memory numbers show me otherwise. */
15776 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15778 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15779 cu->has_loclist = 1;
15782 /* Given a pointer to a DWARF information entry, figure out if we need
15783 to make a symbol table entry for it, and if so, create a new entry
15784 and return a pointer to it.
15785 If TYPE is NULL, determine symbol type from the die, otherwise
15786 used the passed type.
15787 If SPACE is not NULL, use it to hold the new symbol. If it is
15788 NULL, allocate a new symbol on the objfile's obstack. */
15790 static struct symbol *
15791 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15792 struct symbol *space)
15794 struct objfile *objfile = cu->objfile;
15795 struct symbol *sym = NULL;
15797 struct attribute *attr = NULL;
15798 struct attribute *attr2 = NULL;
15799 CORE_ADDR baseaddr;
15800 struct pending **list_to_add = NULL;
15802 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15804 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15806 name = dwarf2_name (die, cu);
15809 const char *linkagename;
15810 int suppress_add = 0;
15815 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15816 OBJSTAT (objfile, n_syms++);
15818 /* Cache this symbol's name and the name's demangled form (if any). */
15819 SYMBOL_SET_LANGUAGE (sym, cu->language);
15820 linkagename = dwarf2_physname (name, die, cu);
15821 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15823 /* Fortran does not have mangling standard and the mangling does differ
15824 between gfortran, iFort etc. */
15825 if (cu->language == language_fortran
15826 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15827 symbol_set_demangled_name (&(sym->ginfo),
15828 dwarf2_full_name (name, die, cu),
15831 /* Default assumptions.
15832 Use the passed type or decode it from the die. */
15833 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15834 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15836 SYMBOL_TYPE (sym) = type;
15838 SYMBOL_TYPE (sym) = die_type (die, cu);
15839 attr = dwarf2_attr (die,
15840 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15844 SYMBOL_LINE (sym) = DW_UNSND (attr);
15847 attr = dwarf2_attr (die,
15848 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15852 int file_index = DW_UNSND (attr);
15854 if (cu->line_header == NULL
15855 || file_index > cu->line_header->num_file_names)
15856 complaint (&symfile_complaints,
15857 _("file index out of range"));
15858 else if (file_index > 0)
15860 struct file_entry *fe;
15862 fe = &cu->line_header->file_names[file_index - 1];
15863 SYMBOL_SYMTAB (sym) = fe->symtab;
15870 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15873 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15875 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15876 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15877 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15878 add_symbol_to_list (sym, cu->list_in_scope);
15880 case DW_TAG_subprogram:
15881 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15883 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15884 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15885 if ((attr2 && (DW_UNSND (attr2) != 0))
15886 || cu->language == language_ada)
15888 /* Subprograms marked external are stored as a global symbol.
15889 Ada subprograms, whether marked external or not, are always
15890 stored as a global symbol, because we want to be able to
15891 access them globally. For instance, we want to be able
15892 to break on a nested subprogram without having to
15893 specify the context. */
15894 list_to_add = &global_symbols;
15898 list_to_add = cu->list_in_scope;
15901 case DW_TAG_inlined_subroutine:
15902 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15904 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15905 SYMBOL_INLINED (sym) = 1;
15906 list_to_add = cu->list_in_scope;
15908 case DW_TAG_template_value_param:
15910 /* Fall through. */
15911 case DW_TAG_constant:
15912 case DW_TAG_variable:
15913 case DW_TAG_member:
15914 /* Compilation with minimal debug info may result in
15915 variables with missing type entries. Change the
15916 misleading `void' type to something sensible. */
15917 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15919 = objfile_type (objfile)->nodebug_data_symbol;
15921 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15922 /* In the case of DW_TAG_member, we should only be called for
15923 static const members. */
15924 if (die->tag == DW_TAG_member)
15926 /* dwarf2_add_field uses die_is_declaration,
15927 so we do the same. */
15928 gdb_assert (die_is_declaration (die, cu));
15933 dwarf2_const_value (attr, sym, cu);
15934 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15937 if (attr2 && (DW_UNSND (attr2) != 0))
15938 list_to_add = &global_symbols;
15940 list_to_add = cu->list_in_scope;
15944 attr = dwarf2_attr (die, DW_AT_location, cu);
15947 var_decode_location (attr, sym, cu);
15948 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15950 /* Fortran explicitly imports any global symbols to the local
15951 scope by DW_TAG_common_block. */
15952 if (cu->language == language_fortran && die->parent
15953 && die->parent->tag == DW_TAG_common_block)
15956 if (SYMBOL_CLASS (sym) == LOC_STATIC
15957 && SYMBOL_VALUE_ADDRESS (sym) == 0
15958 && !dwarf2_per_objfile->has_section_at_zero)
15960 /* When a static variable is eliminated by the linker,
15961 the corresponding debug information is not stripped
15962 out, but the variable address is set to null;
15963 do not add such variables into symbol table. */
15965 else if (attr2 && (DW_UNSND (attr2) != 0))
15967 /* Workaround gfortran PR debug/40040 - it uses
15968 DW_AT_location for variables in -fPIC libraries which may
15969 get overriden by other libraries/executable and get
15970 a different address. Resolve it by the minimal symbol
15971 which may come from inferior's executable using copy
15972 relocation. Make this workaround only for gfortran as for
15973 other compilers GDB cannot guess the minimal symbol
15974 Fortran mangling kind. */
15975 if (cu->language == language_fortran && die->parent
15976 && die->parent->tag == DW_TAG_module
15978 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15979 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15981 /* A variable with DW_AT_external is never static,
15982 but it may be block-scoped. */
15983 list_to_add = (cu->list_in_scope == &file_symbols
15984 ? &global_symbols : cu->list_in_scope);
15987 list_to_add = cu->list_in_scope;
15991 /* We do not know the address of this symbol.
15992 If it is an external symbol and we have type information
15993 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15994 The address of the variable will then be determined from
15995 the minimal symbol table whenever the variable is
15997 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15999 /* Fortran explicitly imports any global symbols to the local
16000 scope by DW_TAG_common_block. */
16001 if (cu->language == language_fortran && die->parent
16002 && die->parent->tag == DW_TAG_common_block)
16004 /* SYMBOL_CLASS doesn't matter here because
16005 read_common_block is going to reset it. */
16007 list_to_add = cu->list_in_scope;
16009 else if (attr2 && (DW_UNSND (attr2) != 0)
16010 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16012 /* A variable with DW_AT_external is never static, but it
16013 may be block-scoped. */
16014 list_to_add = (cu->list_in_scope == &file_symbols
16015 ? &global_symbols : cu->list_in_scope);
16017 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16019 else if (!die_is_declaration (die, cu))
16021 /* Use the default LOC_OPTIMIZED_OUT class. */
16022 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16024 list_to_add = cu->list_in_scope;
16028 case DW_TAG_formal_parameter:
16029 /* If we are inside a function, mark this as an argument. If
16030 not, we might be looking at an argument to an inlined function
16031 when we do not have enough information to show inlined frames;
16032 pretend it's a local variable in that case so that the user can
16034 if (context_stack_depth > 0
16035 && context_stack[context_stack_depth - 1].name != NULL)
16036 SYMBOL_IS_ARGUMENT (sym) = 1;
16037 attr = dwarf2_attr (die, DW_AT_location, cu);
16040 var_decode_location (attr, sym, cu);
16042 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16045 dwarf2_const_value (attr, sym, cu);
16048 list_to_add = cu->list_in_scope;
16050 case DW_TAG_unspecified_parameters:
16051 /* From varargs functions; gdb doesn't seem to have any
16052 interest in this information, so just ignore it for now.
16055 case DW_TAG_template_type_param:
16057 /* Fall through. */
16058 case DW_TAG_class_type:
16059 case DW_TAG_interface_type:
16060 case DW_TAG_structure_type:
16061 case DW_TAG_union_type:
16062 case DW_TAG_set_type:
16063 case DW_TAG_enumeration_type:
16064 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16065 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16068 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16069 really ever be static objects: otherwise, if you try
16070 to, say, break of a class's method and you're in a file
16071 which doesn't mention that class, it won't work unless
16072 the check for all static symbols in lookup_symbol_aux
16073 saves you. See the OtherFileClass tests in
16074 gdb.c++/namespace.exp. */
16078 list_to_add = (cu->list_in_scope == &file_symbols
16079 && (cu->language == language_cplus
16080 || cu->language == language_java)
16081 ? &global_symbols : cu->list_in_scope);
16083 /* The semantics of C++ state that "struct foo {
16084 ... }" also defines a typedef for "foo". A Java
16085 class declaration also defines a typedef for the
16087 if (cu->language == language_cplus
16088 || cu->language == language_java
16089 || cu->language == language_ada)
16091 /* The symbol's name is already allocated along
16092 with this objfile, so we don't need to
16093 duplicate it for the type. */
16094 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16095 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16100 case DW_TAG_typedef:
16101 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16102 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16103 list_to_add = cu->list_in_scope;
16105 case DW_TAG_base_type:
16106 case DW_TAG_subrange_type:
16107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16108 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16109 list_to_add = cu->list_in_scope;
16111 case DW_TAG_enumerator:
16112 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16115 dwarf2_const_value (attr, sym, cu);
16118 /* NOTE: carlton/2003-11-10: See comment above in the
16119 DW_TAG_class_type, etc. block. */
16121 list_to_add = (cu->list_in_scope == &file_symbols
16122 && (cu->language == language_cplus
16123 || cu->language == language_java)
16124 ? &global_symbols : cu->list_in_scope);
16127 case DW_TAG_namespace:
16128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16129 list_to_add = &global_symbols;
16131 case DW_TAG_common_block:
16132 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16133 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16134 add_symbol_to_list (sym, cu->list_in_scope);
16137 /* Not a tag we recognize. Hopefully we aren't processing
16138 trash data, but since we must specifically ignore things
16139 we don't recognize, there is nothing else we should do at
16141 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16142 dwarf_tag_name (die->tag));
16148 sym->hash_next = objfile->template_symbols;
16149 objfile->template_symbols = sym;
16150 list_to_add = NULL;
16153 if (list_to_add != NULL)
16154 add_symbol_to_list (sym, list_to_add);
16156 /* For the benefit of old versions of GCC, check for anonymous
16157 namespaces based on the demangled name. */
16158 if (!cu->processing_has_namespace_info
16159 && cu->language == language_cplus)
16160 cp_scan_for_anonymous_namespaces (sym, objfile);
16165 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16167 static struct symbol *
16168 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16170 return new_symbol_full (die, type, cu, NULL);
16173 /* Given an attr with a DW_FORM_dataN value in host byte order,
16174 zero-extend it as appropriate for the symbol's type. The DWARF
16175 standard (v4) is not entirely clear about the meaning of using
16176 DW_FORM_dataN for a constant with a signed type, where the type is
16177 wider than the data. The conclusion of a discussion on the DWARF
16178 list was that this is unspecified. We choose to always zero-extend
16179 because that is the interpretation long in use by GCC. */
16182 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16183 const char *name, struct obstack *obstack,
16184 struct dwarf2_cu *cu, LONGEST *value, int bits)
16186 struct objfile *objfile = cu->objfile;
16187 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16188 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16189 LONGEST l = DW_UNSND (attr);
16191 if (bits < sizeof (*value) * 8)
16193 l &= ((LONGEST) 1 << bits) - 1;
16196 else if (bits == sizeof (*value) * 8)
16200 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16201 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16208 /* Read a constant value from an attribute. Either set *VALUE, or if
16209 the value does not fit in *VALUE, set *BYTES - either already
16210 allocated on the objfile obstack, or newly allocated on OBSTACK,
16211 or, set *BATON, if we translated the constant to a location
16215 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16216 const char *name, struct obstack *obstack,
16217 struct dwarf2_cu *cu,
16218 LONGEST *value, gdb_byte **bytes,
16219 struct dwarf2_locexpr_baton **baton)
16221 struct objfile *objfile = cu->objfile;
16222 struct comp_unit_head *cu_header = &cu->header;
16223 struct dwarf_block *blk;
16224 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16225 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16231 switch (attr->form)
16234 case DW_FORM_GNU_addr_index:
16238 if (TYPE_LENGTH (type) != cu_header->addr_size)
16239 dwarf2_const_value_length_mismatch_complaint (name,
16240 cu_header->addr_size,
16241 TYPE_LENGTH (type));
16242 /* Symbols of this form are reasonably rare, so we just
16243 piggyback on the existing location code rather than writing
16244 a new implementation of symbol_computed_ops. */
16245 *baton = obstack_alloc (&objfile->objfile_obstack,
16246 sizeof (struct dwarf2_locexpr_baton));
16247 (*baton)->per_cu = cu->per_cu;
16248 gdb_assert ((*baton)->per_cu);
16250 (*baton)->size = 2 + cu_header->addr_size;
16251 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16252 (*baton)->data = data;
16254 data[0] = DW_OP_addr;
16255 store_unsigned_integer (&data[1], cu_header->addr_size,
16256 byte_order, DW_ADDR (attr));
16257 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16260 case DW_FORM_string:
16262 case DW_FORM_GNU_str_index:
16263 case DW_FORM_GNU_strp_alt:
16264 /* DW_STRING is already allocated on the objfile obstack, point
16266 *bytes = (gdb_byte *) DW_STRING (attr);
16268 case DW_FORM_block1:
16269 case DW_FORM_block2:
16270 case DW_FORM_block4:
16271 case DW_FORM_block:
16272 case DW_FORM_exprloc:
16273 blk = DW_BLOCK (attr);
16274 if (TYPE_LENGTH (type) != blk->size)
16275 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16276 TYPE_LENGTH (type));
16277 *bytes = blk->data;
16280 /* The DW_AT_const_value attributes are supposed to carry the
16281 symbol's value "represented as it would be on the target
16282 architecture." By the time we get here, it's already been
16283 converted to host endianness, so we just need to sign- or
16284 zero-extend it as appropriate. */
16285 case DW_FORM_data1:
16286 *bytes = dwarf2_const_value_data (attr, type, name,
16287 obstack, cu, value, 8);
16289 case DW_FORM_data2:
16290 *bytes = dwarf2_const_value_data (attr, type, name,
16291 obstack, cu, value, 16);
16293 case DW_FORM_data4:
16294 *bytes = dwarf2_const_value_data (attr, type, name,
16295 obstack, cu, value, 32);
16297 case DW_FORM_data8:
16298 *bytes = dwarf2_const_value_data (attr, type, name,
16299 obstack, cu, value, 64);
16302 case DW_FORM_sdata:
16303 *value = DW_SND (attr);
16306 case DW_FORM_udata:
16307 *value = DW_UNSND (attr);
16311 complaint (&symfile_complaints,
16312 _("unsupported const value attribute form: '%s'"),
16313 dwarf_form_name (attr->form));
16320 /* Copy constant value from an attribute to a symbol. */
16323 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16324 struct dwarf2_cu *cu)
16326 struct objfile *objfile = cu->objfile;
16327 struct comp_unit_head *cu_header = &cu->header;
16330 struct dwarf2_locexpr_baton *baton;
16332 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16333 SYMBOL_PRINT_NAME (sym),
16334 &objfile->objfile_obstack, cu,
16335 &value, &bytes, &baton);
16339 SYMBOL_LOCATION_BATON (sym) = baton;
16340 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16342 else if (bytes != NULL)
16344 SYMBOL_VALUE_BYTES (sym) = bytes;
16345 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16349 SYMBOL_VALUE (sym) = value;
16350 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16354 /* Return the type of the die in question using its DW_AT_type attribute. */
16356 static struct type *
16357 die_type (struct die_info *die, struct dwarf2_cu *cu)
16359 struct attribute *type_attr;
16361 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16364 /* A missing DW_AT_type represents a void type. */
16365 return objfile_type (cu->objfile)->builtin_void;
16368 return lookup_die_type (die, type_attr, cu);
16371 /* True iff CU's producer generates GNAT Ada auxiliary information
16372 that allows to find parallel types through that information instead
16373 of having to do expensive parallel lookups by type name. */
16376 need_gnat_info (struct dwarf2_cu *cu)
16378 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16379 of GNAT produces this auxiliary information, without any indication
16380 that it is produced. Part of enhancing the FSF version of GNAT
16381 to produce that information will be to put in place an indicator
16382 that we can use in order to determine whether the descriptive type
16383 info is available or not. One suggestion that has been made is
16384 to use a new attribute, attached to the CU die. For now, assume
16385 that the descriptive type info is not available. */
16389 /* Return the auxiliary type of the die in question using its
16390 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16391 attribute is not present. */
16393 static struct type *
16394 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16396 struct attribute *type_attr;
16398 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16402 return lookup_die_type (die, type_attr, cu);
16405 /* If DIE has a descriptive_type attribute, then set the TYPE's
16406 descriptive type accordingly. */
16409 set_descriptive_type (struct type *type, struct die_info *die,
16410 struct dwarf2_cu *cu)
16412 struct type *descriptive_type = die_descriptive_type (die, cu);
16414 if (descriptive_type)
16416 ALLOCATE_GNAT_AUX_TYPE (type);
16417 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16421 /* Return the containing type of the die in question using its
16422 DW_AT_containing_type attribute. */
16424 static struct type *
16425 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16427 struct attribute *type_attr;
16429 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16431 error (_("Dwarf Error: Problem turning containing type into gdb type "
16432 "[in module %s]"), cu->objfile->name);
16434 return lookup_die_type (die, type_attr, cu);
16437 /* Look up the type of DIE in CU using its type attribute ATTR.
16438 If there is no type substitute an error marker. */
16440 static struct type *
16441 lookup_die_type (struct die_info *die, struct attribute *attr,
16442 struct dwarf2_cu *cu)
16444 struct objfile *objfile = cu->objfile;
16445 struct type *this_type;
16447 /* First see if we have it cached. */
16449 if (attr->form == DW_FORM_GNU_ref_alt)
16451 struct dwarf2_per_cu_data *per_cu;
16452 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16454 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16455 this_type = get_die_type_at_offset (offset, per_cu);
16457 else if (is_ref_attr (attr))
16459 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16461 this_type = get_die_type_at_offset (offset, cu->per_cu);
16463 else if (attr->form == DW_FORM_ref_sig8)
16465 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16467 /* sig_type will be NULL if the signatured type is missing from
16469 if (sig_type == NULL)
16470 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16471 "at 0x%x [in module %s]"),
16472 die->offset.sect_off, objfile->name);
16474 gdb_assert (sig_type->per_cu.is_debug_types);
16475 /* If we haven't filled in type_offset_in_section yet, then we
16476 haven't read the type in yet. */
16478 if (sig_type->type_offset_in_section.sect_off != 0)
16481 get_die_type_at_offset (sig_type->type_offset_in_section,
16482 &sig_type->per_cu);
16487 dump_die_for_error (die);
16488 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16489 dwarf_attr_name (attr->name), objfile->name);
16492 /* If not cached we need to read it in. */
16494 if (this_type == NULL)
16496 struct die_info *type_die;
16497 struct dwarf2_cu *type_cu = cu;
16499 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16500 /* If we found the type now, it's probably because the type came
16501 from an inter-CU reference and the type's CU got expanded before
16503 this_type = get_die_type (type_die, type_cu);
16504 if (this_type == NULL)
16505 this_type = read_type_die_1 (type_die, type_cu);
16508 /* If we still don't have a type use an error marker. */
16510 if (this_type == NULL)
16512 char *message, *saved;
16514 /* read_type_die already issued a complaint. */
16515 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16517 cu->header.offset.sect_off,
16518 die->offset.sect_off);
16519 saved = obstack_copy0 (&objfile->objfile_obstack,
16520 message, strlen (message));
16523 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16529 /* Return the type in DIE, CU.
16530 Returns NULL for invalid types.
16532 This first does a lookup in the appropriate type_hash table,
16533 and only reads the die in if necessary.
16535 NOTE: This can be called when reading in partial or full symbols. */
16537 static struct type *
16538 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16540 struct type *this_type;
16542 this_type = get_die_type (die, cu);
16546 return read_type_die_1 (die, cu);
16549 /* Read the type in DIE, CU.
16550 Returns NULL for invalid types. */
16552 static struct type *
16553 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16555 struct type *this_type = NULL;
16559 case DW_TAG_class_type:
16560 case DW_TAG_interface_type:
16561 case DW_TAG_structure_type:
16562 case DW_TAG_union_type:
16563 this_type = read_structure_type (die, cu);
16565 case DW_TAG_enumeration_type:
16566 this_type = read_enumeration_type (die, cu);
16568 case DW_TAG_subprogram:
16569 case DW_TAG_subroutine_type:
16570 case DW_TAG_inlined_subroutine:
16571 this_type = read_subroutine_type (die, cu);
16573 case DW_TAG_array_type:
16574 this_type = read_array_type (die, cu);
16576 case DW_TAG_set_type:
16577 this_type = read_set_type (die, cu);
16579 case DW_TAG_pointer_type:
16580 this_type = read_tag_pointer_type (die, cu);
16582 case DW_TAG_ptr_to_member_type:
16583 this_type = read_tag_ptr_to_member_type (die, cu);
16585 case DW_TAG_reference_type:
16586 this_type = read_tag_reference_type (die, cu);
16588 case DW_TAG_const_type:
16589 this_type = read_tag_const_type (die, cu);
16591 case DW_TAG_volatile_type:
16592 this_type = read_tag_volatile_type (die, cu);
16594 case DW_TAG_restrict_type:
16595 this_type = read_tag_restrict_type (die, cu);
16597 case DW_TAG_string_type:
16598 this_type = read_tag_string_type (die, cu);
16600 case DW_TAG_typedef:
16601 this_type = read_typedef (die, cu);
16603 case DW_TAG_subrange_type:
16604 this_type = read_subrange_type (die, cu);
16606 case DW_TAG_base_type:
16607 this_type = read_base_type (die, cu);
16609 case DW_TAG_unspecified_type:
16610 this_type = read_unspecified_type (die, cu);
16612 case DW_TAG_namespace:
16613 this_type = read_namespace_type (die, cu);
16615 case DW_TAG_module:
16616 this_type = read_module_type (die, cu);
16619 complaint (&symfile_complaints,
16620 _("unexpected tag in read_type_die: '%s'"),
16621 dwarf_tag_name (die->tag));
16628 /* See if we can figure out if the class lives in a namespace. We do
16629 this by looking for a member function; its demangled name will
16630 contain namespace info, if there is any.
16631 Return the computed name or NULL.
16632 Space for the result is allocated on the objfile's obstack.
16633 This is the full-die version of guess_partial_die_structure_name.
16634 In this case we know DIE has no useful parent. */
16637 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16639 struct die_info *spec_die;
16640 struct dwarf2_cu *spec_cu;
16641 struct die_info *child;
16644 spec_die = die_specification (die, &spec_cu);
16645 if (spec_die != NULL)
16651 for (child = die->child;
16653 child = child->sibling)
16655 if (child->tag == DW_TAG_subprogram)
16657 struct attribute *attr;
16659 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16661 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16665 = language_class_name_from_physname (cu->language_defn,
16669 if (actual_name != NULL)
16671 const char *die_name = dwarf2_name (die, cu);
16673 if (die_name != NULL
16674 && strcmp (die_name, actual_name) != 0)
16676 /* Strip off the class name from the full name.
16677 We want the prefix. */
16678 int die_name_len = strlen (die_name);
16679 int actual_name_len = strlen (actual_name);
16681 /* Test for '::' as a sanity check. */
16682 if (actual_name_len > die_name_len + 2
16683 && actual_name[actual_name_len
16684 - die_name_len - 1] == ':')
16686 obstack_copy0 (&cu->objfile->objfile_obstack,
16688 actual_name_len - die_name_len - 2);
16691 xfree (actual_name);
16700 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16701 prefix part in such case. See
16702 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16705 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16707 struct attribute *attr;
16710 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16711 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16714 attr = dwarf2_attr (die, DW_AT_name, cu);
16715 if (attr != NULL && DW_STRING (attr) != NULL)
16718 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16720 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16721 if (attr == NULL || DW_STRING (attr) == NULL)
16724 /* dwarf2_name had to be already called. */
16725 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16727 /* Strip the base name, keep any leading namespaces/classes. */
16728 base = strrchr (DW_STRING (attr), ':');
16729 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16732 return obstack_copy0 (&cu->objfile->objfile_obstack,
16733 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16736 /* Return the name of the namespace/class that DIE is defined within,
16737 or "" if we can't tell. The caller should not xfree the result.
16739 For example, if we're within the method foo() in the following
16749 then determine_prefix on foo's die will return "N::C". */
16751 static const char *
16752 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16754 struct die_info *parent, *spec_die;
16755 struct dwarf2_cu *spec_cu;
16756 struct type *parent_type;
16759 if (cu->language != language_cplus && cu->language != language_java
16760 && cu->language != language_fortran)
16763 retval = anonymous_struct_prefix (die, cu);
16767 /* We have to be careful in the presence of DW_AT_specification.
16768 For example, with GCC 3.4, given the code
16772 // Definition of N::foo.
16776 then we'll have a tree of DIEs like this:
16778 1: DW_TAG_compile_unit
16779 2: DW_TAG_namespace // N
16780 3: DW_TAG_subprogram // declaration of N::foo
16781 4: DW_TAG_subprogram // definition of N::foo
16782 DW_AT_specification // refers to die #3
16784 Thus, when processing die #4, we have to pretend that we're in
16785 the context of its DW_AT_specification, namely the contex of die
16788 spec_die = die_specification (die, &spec_cu);
16789 if (spec_die == NULL)
16790 parent = die->parent;
16793 parent = spec_die->parent;
16797 if (parent == NULL)
16799 else if (parent->building_fullname)
16802 const char *parent_name;
16804 /* It has been seen on RealView 2.2 built binaries,
16805 DW_TAG_template_type_param types actually _defined_ as
16806 children of the parent class:
16809 template class <class Enum> Class{};
16810 Class<enum E> class_e;
16812 1: DW_TAG_class_type (Class)
16813 2: DW_TAG_enumeration_type (E)
16814 3: DW_TAG_enumerator (enum1:0)
16815 3: DW_TAG_enumerator (enum2:1)
16817 2: DW_TAG_template_type_param
16818 DW_AT_type DW_FORM_ref_udata (E)
16820 Besides being broken debug info, it can put GDB into an
16821 infinite loop. Consider:
16823 When we're building the full name for Class<E>, we'll start
16824 at Class, and go look over its template type parameters,
16825 finding E. We'll then try to build the full name of E, and
16826 reach here. We're now trying to build the full name of E,
16827 and look over the parent DIE for containing scope. In the
16828 broken case, if we followed the parent DIE of E, we'd again
16829 find Class, and once again go look at its template type
16830 arguments, etc., etc. Simply don't consider such parent die
16831 as source-level parent of this die (it can't be, the language
16832 doesn't allow it), and break the loop here. */
16833 name = dwarf2_name (die, cu);
16834 parent_name = dwarf2_name (parent, cu);
16835 complaint (&symfile_complaints,
16836 _("template param type '%s' defined within parent '%s'"),
16837 name ? name : "<unknown>",
16838 parent_name ? parent_name : "<unknown>");
16842 switch (parent->tag)
16844 case DW_TAG_namespace:
16845 parent_type = read_type_die (parent, cu);
16846 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16847 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16848 Work around this problem here. */
16849 if (cu->language == language_cplus
16850 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16852 /* We give a name to even anonymous namespaces. */
16853 return TYPE_TAG_NAME (parent_type);
16854 case DW_TAG_class_type:
16855 case DW_TAG_interface_type:
16856 case DW_TAG_structure_type:
16857 case DW_TAG_union_type:
16858 case DW_TAG_module:
16859 parent_type = read_type_die (parent, cu);
16860 if (TYPE_TAG_NAME (parent_type) != NULL)
16861 return TYPE_TAG_NAME (parent_type);
16863 /* An anonymous structure is only allowed non-static data
16864 members; no typedefs, no member functions, et cetera.
16865 So it does not need a prefix. */
16867 case DW_TAG_compile_unit:
16868 case DW_TAG_partial_unit:
16869 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16870 if (cu->language == language_cplus
16871 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16872 && die->child != NULL
16873 && (die->tag == DW_TAG_class_type
16874 || die->tag == DW_TAG_structure_type
16875 || die->tag == DW_TAG_union_type))
16877 char *name = guess_full_die_structure_name (die, cu);
16883 return determine_prefix (parent, cu);
16887 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16888 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16889 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16890 an obconcat, otherwise allocate storage for the result. The CU argument is
16891 used to determine the language and hence, the appropriate separator. */
16893 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16896 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16897 int physname, struct dwarf2_cu *cu)
16899 const char *lead = "";
16902 if (suffix == NULL || suffix[0] == '\0'
16903 || prefix == NULL || prefix[0] == '\0')
16905 else if (cu->language == language_java)
16907 else if (cu->language == language_fortran && physname)
16909 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16910 DW_AT_MIPS_linkage_name is preferred and used instead. */
16918 if (prefix == NULL)
16920 if (suffix == NULL)
16926 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16928 strcpy (retval, lead);
16929 strcat (retval, prefix);
16930 strcat (retval, sep);
16931 strcat (retval, suffix);
16936 /* We have an obstack. */
16937 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16941 /* Return sibling of die, NULL if no sibling. */
16943 static struct die_info *
16944 sibling_die (struct die_info *die)
16946 return die->sibling;
16949 /* Get name of a die, return NULL if not found. */
16951 static const char *
16952 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16953 struct obstack *obstack)
16955 if (name && cu->language == language_cplus)
16957 char *canon_name = cp_canonicalize_string (name);
16959 if (canon_name != NULL)
16961 if (strcmp (canon_name, name) != 0)
16962 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16963 xfree (canon_name);
16970 /* Get name of a die, return NULL if not found. */
16972 static const char *
16973 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16975 struct attribute *attr;
16977 attr = dwarf2_attr (die, DW_AT_name, cu);
16978 if ((!attr || !DW_STRING (attr))
16979 && die->tag != DW_TAG_class_type
16980 && die->tag != DW_TAG_interface_type
16981 && die->tag != DW_TAG_structure_type
16982 && die->tag != DW_TAG_union_type)
16987 case DW_TAG_compile_unit:
16988 case DW_TAG_partial_unit:
16989 /* Compilation units have a DW_AT_name that is a filename, not
16990 a source language identifier. */
16991 case DW_TAG_enumeration_type:
16992 case DW_TAG_enumerator:
16993 /* These tags always have simple identifiers already; no need
16994 to canonicalize them. */
16995 return DW_STRING (attr);
16997 case DW_TAG_subprogram:
16998 /* Java constructors will all be named "<init>", so return
16999 the class name when we see this special case. */
17000 if (cu->language == language_java
17001 && DW_STRING (attr) != NULL
17002 && strcmp (DW_STRING (attr), "<init>") == 0)
17004 struct dwarf2_cu *spec_cu = cu;
17005 struct die_info *spec_die;
17007 /* GCJ will output '<init>' for Java constructor names.
17008 For this special case, return the name of the parent class. */
17010 /* GCJ may output suprogram DIEs with AT_specification set.
17011 If so, use the name of the specified DIE. */
17012 spec_die = die_specification (die, &spec_cu);
17013 if (spec_die != NULL)
17014 return dwarf2_name (spec_die, spec_cu);
17019 if (die->tag == DW_TAG_class_type)
17020 return dwarf2_name (die, cu);
17022 while (die->tag != DW_TAG_compile_unit
17023 && die->tag != DW_TAG_partial_unit);
17027 case DW_TAG_class_type:
17028 case DW_TAG_interface_type:
17029 case DW_TAG_structure_type:
17030 case DW_TAG_union_type:
17031 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17032 structures or unions. These were of the form "._%d" in GCC 4.1,
17033 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17034 and GCC 4.4. We work around this problem by ignoring these. */
17035 if (attr && DW_STRING (attr)
17036 && (strncmp (DW_STRING (attr), "._", 2) == 0
17037 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17040 /* GCC might emit a nameless typedef that has a linkage name. See
17041 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17042 if (!attr || DW_STRING (attr) == NULL)
17044 char *demangled = NULL;
17046 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17048 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17050 if (attr == NULL || DW_STRING (attr) == NULL)
17053 /* Avoid demangling DW_STRING (attr) the second time on a second
17054 call for the same DIE. */
17055 if (!DW_STRING_IS_CANONICAL (attr))
17056 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17062 /* FIXME: we already did this for the partial symbol... */
17063 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17064 demangled, strlen (demangled));
17065 DW_STRING_IS_CANONICAL (attr) = 1;
17068 /* Strip any leading namespaces/classes, keep only the base name.
17069 DW_AT_name for named DIEs does not contain the prefixes. */
17070 base = strrchr (DW_STRING (attr), ':');
17071 if (base && base > DW_STRING (attr) && base[-1] == ':')
17074 return DW_STRING (attr);
17083 if (!DW_STRING_IS_CANONICAL (attr))
17086 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17087 &cu->objfile->objfile_obstack);
17088 DW_STRING_IS_CANONICAL (attr) = 1;
17090 return DW_STRING (attr);
17093 /* Return the die that this die in an extension of, or NULL if there
17094 is none. *EXT_CU is the CU containing DIE on input, and the CU
17095 containing the return value on output. */
17097 static struct die_info *
17098 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17100 struct attribute *attr;
17102 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17106 return follow_die_ref (die, attr, ext_cu);
17109 /* Convert a DIE tag into its string name. */
17111 static const char *
17112 dwarf_tag_name (unsigned tag)
17114 const char *name = get_DW_TAG_name (tag);
17117 return "DW_TAG_<unknown>";
17122 /* Convert a DWARF attribute code into its string name. */
17124 static const char *
17125 dwarf_attr_name (unsigned attr)
17129 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17130 if (attr == DW_AT_MIPS_fde)
17131 return "DW_AT_MIPS_fde";
17133 if (attr == DW_AT_HP_block_index)
17134 return "DW_AT_HP_block_index";
17137 name = get_DW_AT_name (attr);
17140 return "DW_AT_<unknown>";
17145 /* Convert a DWARF value form code into its string name. */
17147 static const char *
17148 dwarf_form_name (unsigned form)
17150 const char *name = get_DW_FORM_name (form);
17153 return "DW_FORM_<unknown>";
17159 dwarf_bool_name (unsigned mybool)
17167 /* Convert a DWARF type code into its string name. */
17169 static const char *
17170 dwarf_type_encoding_name (unsigned enc)
17172 const char *name = get_DW_ATE_name (enc);
17175 return "DW_ATE_<unknown>";
17181 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17185 print_spaces (indent, f);
17186 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17187 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17189 if (die->parent != NULL)
17191 print_spaces (indent, f);
17192 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17193 die->parent->offset.sect_off);
17196 print_spaces (indent, f);
17197 fprintf_unfiltered (f, " has children: %s\n",
17198 dwarf_bool_name (die->child != NULL));
17200 print_spaces (indent, f);
17201 fprintf_unfiltered (f, " attributes:\n");
17203 for (i = 0; i < die->num_attrs; ++i)
17205 print_spaces (indent, f);
17206 fprintf_unfiltered (f, " %s (%s) ",
17207 dwarf_attr_name (die->attrs[i].name),
17208 dwarf_form_name (die->attrs[i].form));
17210 switch (die->attrs[i].form)
17213 case DW_FORM_GNU_addr_index:
17214 fprintf_unfiltered (f, "address: ");
17215 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17217 case DW_FORM_block2:
17218 case DW_FORM_block4:
17219 case DW_FORM_block:
17220 case DW_FORM_block1:
17221 fprintf_unfiltered (f, "block: size %s",
17222 pulongest (DW_BLOCK (&die->attrs[i])->size));
17224 case DW_FORM_exprloc:
17225 fprintf_unfiltered (f, "expression: size %s",
17226 pulongest (DW_BLOCK (&die->attrs[i])->size));
17228 case DW_FORM_ref_addr:
17229 fprintf_unfiltered (f, "ref address: ");
17230 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17232 case DW_FORM_GNU_ref_alt:
17233 fprintf_unfiltered (f, "alt ref address: ");
17234 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17240 case DW_FORM_ref_udata:
17241 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17242 (long) (DW_UNSND (&die->attrs[i])));
17244 case DW_FORM_data1:
17245 case DW_FORM_data2:
17246 case DW_FORM_data4:
17247 case DW_FORM_data8:
17248 case DW_FORM_udata:
17249 case DW_FORM_sdata:
17250 fprintf_unfiltered (f, "constant: %s",
17251 pulongest (DW_UNSND (&die->attrs[i])));
17253 case DW_FORM_sec_offset:
17254 fprintf_unfiltered (f, "section offset: %s",
17255 pulongest (DW_UNSND (&die->attrs[i])));
17257 case DW_FORM_ref_sig8:
17258 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17260 struct signatured_type *sig_type =
17261 DW_SIGNATURED_TYPE (&die->attrs[i]);
17263 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17264 hex_string (sig_type->signature),
17265 sig_type->per_cu.offset.sect_off);
17268 fprintf_unfiltered (f, "signatured type, unknown");
17270 case DW_FORM_string:
17272 case DW_FORM_GNU_str_index:
17273 case DW_FORM_GNU_strp_alt:
17274 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17275 DW_STRING (&die->attrs[i])
17276 ? DW_STRING (&die->attrs[i]) : "",
17277 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17280 if (DW_UNSND (&die->attrs[i]))
17281 fprintf_unfiltered (f, "flag: TRUE");
17283 fprintf_unfiltered (f, "flag: FALSE");
17285 case DW_FORM_flag_present:
17286 fprintf_unfiltered (f, "flag: TRUE");
17288 case DW_FORM_indirect:
17289 /* The reader will have reduced the indirect form to
17290 the "base form" so this form should not occur. */
17291 fprintf_unfiltered (f,
17292 "unexpected attribute form: DW_FORM_indirect");
17295 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17296 die->attrs[i].form);
17299 fprintf_unfiltered (f, "\n");
17304 dump_die_for_error (struct die_info *die)
17306 dump_die_shallow (gdb_stderr, 0, die);
17310 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17312 int indent = level * 4;
17314 gdb_assert (die != NULL);
17316 if (level >= max_level)
17319 dump_die_shallow (f, indent, die);
17321 if (die->child != NULL)
17323 print_spaces (indent, f);
17324 fprintf_unfiltered (f, " Children:");
17325 if (level + 1 < max_level)
17327 fprintf_unfiltered (f, "\n");
17328 dump_die_1 (f, level + 1, max_level, die->child);
17332 fprintf_unfiltered (f,
17333 " [not printed, max nesting level reached]\n");
17337 if (die->sibling != NULL && level > 0)
17339 dump_die_1 (f, level, max_level, die->sibling);
17343 /* This is called from the pdie macro in gdbinit.in.
17344 It's not static so gcc will keep a copy callable from gdb. */
17347 dump_die (struct die_info *die, int max_level)
17349 dump_die_1 (gdb_stdlog, 0, max_level, die);
17353 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17357 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17363 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17364 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17367 is_ref_attr (struct attribute *attr)
17369 switch (attr->form)
17371 case DW_FORM_ref_addr:
17376 case DW_FORM_ref_udata:
17377 case DW_FORM_GNU_ref_alt:
17384 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17388 dwarf2_get_ref_die_offset (struct attribute *attr)
17390 sect_offset retval = { DW_UNSND (attr) };
17392 if (is_ref_attr (attr))
17395 retval.sect_off = 0;
17396 complaint (&symfile_complaints,
17397 _("unsupported die ref attribute form: '%s'"),
17398 dwarf_form_name (attr->form));
17402 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17403 * the value held by the attribute is not constant. */
17406 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17408 if (attr->form == DW_FORM_sdata)
17409 return DW_SND (attr);
17410 else if (attr->form == DW_FORM_udata
17411 || attr->form == DW_FORM_data1
17412 || attr->form == DW_FORM_data2
17413 || attr->form == DW_FORM_data4
17414 || attr->form == DW_FORM_data8)
17415 return DW_UNSND (attr);
17418 complaint (&symfile_complaints,
17419 _("Attribute value is not a constant (%s)"),
17420 dwarf_form_name (attr->form));
17421 return default_value;
17425 /* Follow reference or signature attribute ATTR of SRC_DIE.
17426 On entry *REF_CU is the CU of SRC_DIE.
17427 On exit *REF_CU is the CU of the result. */
17429 static struct die_info *
17430 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17431 struct dwarf2_cu **ref_cu)
17433 struct die_info *die;
17435 if (is_ref_attr (attr))
17436 die = follow_die_ref (src_die, attr, ref_cu);
17437 else if (attr->form == DW_FORM_ref_sig8)
17438 die = follow_die_sig (src_die, attr, ref_cu);
17441 dump_die_for_error (src_die);
17442 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17443 (*ref_cu)->objfile->name);
17449 /* Follow reference OFFSET.
17450 On entry *REF_CU is the CU of the source die referencing OFFSET.
17451 On exit *REF_CU is the CU of the result.
17452 Returns NULL if OFFSET is invalid. */
17454 static struct die_info *
17455 follow_die_offset (sect_offset offset, int offset_in_dwz,
17456 struct dwarf2_cu **ref_cu)
17458 struct die_info temp_die;
17459 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17461 gdb_assert (cu->per_cu != NULL);
17465 if (cu->per_cu->is_debug_types)
17467 /* .debug_types CUs cannot reference anything outside their CU.
17468 If they need to, they have to reference a signatured type via
17469 DW_FORM_ref_sig8. */
17470 if (! offset_in_cu_p (&cu->header, offset))
17473 else if (offset_in_dwz != cu->per_cu->is_dwz
17474 || ! offset_in_cu_p (&cu->header, offset))
17476 struct dwarf2_per_cu_data *per_cu;
17478 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17481 /* If necessary, add it to the queue and load its DIEs. */
17482 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17483 load_full_comp_unit (per_cu, cu->language);
17485 target_cu = per_cu->cu;
17487 else if (cu->dies == NULL)
17489 /* We're loading full DIEs during partial symbol reading. */
17490 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17491 load_full_comp_unit (cu->per_cu, language_minimal);
17494 *ref_cu = target_cu;
17495 temp_die.offset = offset;
17496 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17499 /* Follow reference attribute ATTR of SRC_DIE.
17500 On entry *REF_CU is the CU of SRC_DIE.
17501 On exit *REF_CU is the CU of the result. */
17503 static struct die_info *
17504 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17505 struct dwarf2_cu **ref_cu)
17507 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17508 struct dwarf2_cu *cu = *ref_cu;
17509 struct die_info *die;
17511 die = follow_die_offset (offset,
17512 (attr->form == DW_FORM_GNU_ref_alt
17513 || cu->per_cu->is_dwz),
17516 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17517 "at 0x%x [in module %s]"),
17518 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17523 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17524 Returned value is intended for DW_OP_call*. Returned
17525 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17527 struct dwarf2_locexpr_baton
17528 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17529 struct dwarf2_per_cu_data *per_cu,
17530 CORE_ADDR (*get_frame_pc) (void *baton),
17533 struct dwarf2_cu *cu;
17534 struct die_info *die;
17535 struct attribute *attr;
17536 struct dwarf2_locexpr_baton retval;
17538 dw2_setup (per_cu->objfile);
17540 if (per_cu->cu == NULL)
17544 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17546 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17547 offset.sect_off, per_cu->objfile->name);
17549 attr = dwarf2_attr (die, DW_AT_location, cu);
17552 /* DWARF: "If there is no such attribute, then there is no effect.".
17553 DATA is ignored if SIZE is 0. */
17555 retval.data = NULL;
17558 else if (attr_form_is_section_offset (attr))
17560 struct dwarf2_loclist_baton loclist_baton;
17561 CORE_ADDR pc = (*get_frame_pc) (baton);
17564 fill_in_loclist_baton (cu, &loclist_baton, attr);
17566 retval.data = dwarf2_find_location_expression (&loclist_baton,
17568 retval.size = size;
17572 if (!attr_form_is_block (attr))
17573 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17574 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17575 offset.sect_off, per_cu->objfile->name);
17577 retval.data = DW_BLOCK (attr)->data;
17578 retval.size = DW_BLOCK (attr)->size;
17580 retval.per_cu = cu->per_cu;
17582 age_cached_comp_units ();
17587 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17590 struct dwarf2_locexpr_baton
17591 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17592 struct dwarf2_per_cu_data *per_cu,
17593 CORE_ADDR (*get_frame_pc) (void *baton),
17596 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17598 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17601 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17605 dwarf2_get_die_type (cu_offset die_offset,
17606 struct dwarf2_per_cu_data *per_cu)
17608 sect_offset die_offset_sect;
17610 dw2_setup (per_cu->objfile);
17612 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17613 return get_die_type_at_offset (die_offset_sect, per_cu);
17616 /* Follow the signature attribute ATTR in SRC_DIE.
17617 On entry *REF_CU is the CU of SRC_DIE.
17618 On exit *REF_CU is the CU of the result. */
17620 static struct die_info *
17621 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17622 struct dwarf2_cu **ref_cu)
17624 struct objfile *objfile = (*ref_cu)->objfile;
17625 struct die_info temp_die;
17626 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17627 struct dwarf2_cu *sig_cu;
17628 struct die_info *die;
17630 /* sig_type will be NULL if the signatured type is missing from
17632 if (sig_type == NULL)
17633 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17634 "at 0x%x [in module %s]"),
17635 src_die->offset.sect_off, objfile->name);
17637 /* If necessary, add it to the queue and load its DIEs. */
17639 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17640 read_signatured_type (sig_type);
17642 gdb_assert (sig_type->per_cu.cu != NULL);
17644 sig_cu = sig_type->per_cu.cu;
17645 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17646 temp_die.offset = sig_type->type_offset_in_section;
17647 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17648 temp_die.offset.sect_off);
17651 /* For .gdb_index version 7 keep track of included TUs.
17652 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17653 if (dwarf2_per_objfile->index_table != NULL
17654 && dwarf2_per_objfile->index_table->version <= 7)
17656 VEC_safe_push (dwarf2_per_cu_ptr,
17657 (*ref_cu)->per_cu->imported_symtabs,
17665 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17666 "from DIE at 0x%x [in module %s]"),
17667 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17670 /* Given an offset of a signatured type, return its signatured_type. */
17672 static struct signatured_type *
17673 lookup_signatured_type_at_offset (struct objfile *objfile,
17674 struct dwarf2_section_info *section,
17675 sect_offset offset)
17677 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17678 unsigned int length, initial_length_size;
17679 unsigned int sig_offset;
17680 struct signatured_type find_entry, *sig_type;
17682 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17683 sig_offset = (initial_length_size
17685 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17686 + 1 /*address_size*/);
17687 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17688 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17690 /* This is only used to lookup previously recorded types.
17691 If we didn't find it, it's our bug. */
17692 gdb_assert (sig_type != NULL);
17693 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17698 /* Load the DIEs associated with type unit PER_CU into memory. */
17701 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17703 struct signatured_type *sig_type;
17705 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17706 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17708 /* We have the per_cu, but we need the signatured_type.
17709 Fortunately this is an easy translation. */
17710 gdb_assert (per_cu->is_debug_types);
17711 sig_type = (struct signatured_type *) per_cu;
17713 gdb_assert (per_cu->cu == NULL);
17715 read_signatured_type (sig_type);
17717 gdb_assert (per_cu->cu != NULL);
17720 /* die_reader_func for read_signatured_type.
17721 This is identical to load_full_comp_unit_reader,
17722 but is kept separate for now. */
17725 read_signatured_type_reader (const struct die_reader_specs *reader,
17726 gdb_byte *info_ptr,
17727 struct die_info *comp_unit_die,
17731 struct dwarf2_cu *cu = reader->cu;
17733 gdb_assert (cu->die_hash == NULL);
17735 htab_create_alloc_ex (cu->header.length / 12,
17739 &cu->comp_unit_obstack,
17740 hashtab_obstack_allocate,
17741 dummy_obstack_deallocate);
17744 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17745 &info_ptr, comp_unit_die);
17746 cu->dies = comp_unit_die;
17747 /* comp_unit_die is not stored in die_hash, no need. */
17749 /* We try not to read any attributes in this function, because not
17750 all CUs needed for references have been loaded yet, and symbol
17751 table processing isn't initialized. But we have to set the CU language,
17752 or we won't be able to build types correctly.
17753 Similarly, if we do not read the producer, we can not apply
17754 producer-specific interpretation. */
17755 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17758 /* Read in a signatured type and build its CU and DIEs.
17759 If the type is a stub for the real type in a DWO file,
17760 read in the real type from the DWO file as well. */
17763 read_signatured_type (struct signatured_type *sig_type)
17765 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17767 gdb_assert (per_cu->is_debug_types);
17768 gdb_assert (per_cu->cu == NULL);
17770 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17771 read_signatured_type_reader, NULL);
17774 /* Decode simple location descriptions.
17775 Given a pointer to a dwarf block that defines a location, compute
17776 the location and return the value.
17778 NOTE drow/2003-11-18: This function is called in two situations
17779 now: for the address of static or global variables (partial symbols
17780 only) and for offsets into structures which are expected to be
17781 (more or less) constant. The partial symbol case should go away,
17782 and only the constant case should remain. That will let this
17783 function complain more accurately. A few special modes are allowed
17784 without complaint for global variables (for instance, global
17785 register values and thread-local values).
17787 A location description containing no operations indicates that the
17788 object is optimized out. The return value is 0 for that case.
17789 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17790 callers will only want a very basic result and this can become a
17793 Note that stack[0] is unused except as a default error return. */
17796 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17798 struct objfile *objfile = cu->objfile;
17800 size_t size = blk->size;
17801 gdb_byte *data = blk->data;
17802 CORE_ADDR stack[64];
17804 unsigned int bytes_read, unsnd;
17810 stack[++stacki] = 0;
17849 stack[++stacki] = op - DW_OP_lit0;
17884 stack[++stacki] = op - DW_OP_reg0;
17886 dwarf2_complex_location_expr_complaint ();
17890 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17892 stack[++stacki] = unsnd;
17894 dwarf2_complex_location_expr_complaint ();
17898 stack[++stacki] = read_address (objfile->obfd, &data[i],
17903 case DW_OP_const1u:
17904 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17908 case DW_OP_const1s:
17909 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17913 case DW_OP_const2u:
17914 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17918 case DW_OP_const2s:
17919 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17923 case DW_OP_const4u:
17924 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17928 case DW_OP_const4s:
17929 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17933 case DW_OP_const8u:
17934 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17939 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17945 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17950 stack[stacki + 1] = stack[stacki];
17955 stack[stacki - 1] += stack[stacki];
17959 case DW_OP_plus_uconst:
17960 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17966 stack[stacki - 1] -= stack[stacki];
17971 /* If we're not the last op, then we definitely can't encode
17972 this using GDB's address_class enum. This is valid for partial
17973 global symbols, although the variable's address will be bogus
17976 dwarf2_complex_location_expr_complaint ();
17979 case DW_OP_GNU_push_tls_address:
17980 /* The top of the stack has the offset from the beginning
17981 of the thread control block at which the variable is located. */
17982 /* Nothing should follow this operator, so the top of stack would
17984 /* This is valid for partial global symbols, but the variable's
17985 address will be bogus in the psymtab. Make it always at least
17986 non-zero to not look as a variable garbage collected by linker
17987 which have DW_OP_addr 0. */
17989 dwarf2_complex_location_expr_complaint ();
17993 case DW_OP_GNU_uninit:
17996 case DW_OP_GNU_addr_index:
17997 case DW_OP_GNU_const_index:
17998 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18005 const char *name = get_DW_OP_name (op);
18008 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18011 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18015 return (stack[stacki]);
18018 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18019 outside of the allocated space. Also enforce minimum>0. */
18020 if (stacki >= ARRAY_SIZE (stack) - 1)
18022 complaint (&symfile_complaints,
18023 _("location description stack overflow"));
18029 complaint (&symfile_complaints,
18030 _("location description stack underflow"));
18034 return (stack[stacki]);
18037 /* memory allocation interface */
18039 static struct dwarf_block *
18040 dwarf_alloc_block (struct dwarf2_cu *cu)
18042 struct dwarf_block *blk;
18044 blk = (struct dwarf_block *)
18045 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18049 static struct die_info *
18050 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18052 struct die_info *die;
18053 size_t size = sizeof (struct die_info);
18056 size += (num_attrs - 1) * sizeof (struct attribute);
18058 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18059 memset (die, 0, sizeof (struct die_info));
18064 /* Macro support. */
18066 /* Return file name relative to the compilation directory of file number I in
18067 *LH's file name table. The result is allocated using xmalloc; the caller is
18068 responsible for freeing it. */
18071 file_file_name (int file, struct line_header *lh)
18073 /* Is the file number a valid index into the line header's file name
18074 table? Remember that file numbers start with one, not zero. */
18075 if (1 <= file && file <= lh->num_file_names)
18077 struct file_entry *fe = &lh->file_names[file - 1];
18079 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18080 return xstrdup (fe->name);
18081 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18086 /* The compiler produced a bogus file number. We can at least
18087 record the macro definitions made in the file, even if we
18088 won't be able to find the file by name. */
18089 char fake_name[80];
18091 xsnprintf (fake_name, sizeof (fake_name),
18092 "<bad macro file number %d>", file);
18094 complaint (&symfile_complaints,
18095 _("bad file number in macro information (%d)"),
18098 return xstrdup (fake_name);
18102 /* Return the full name of file number I in *LH's file name table.
18103 Use COMP_DIR as the name of the current directory of the
18104 compilation. The result is allocated using xmalloc; the caller is
18105 responsible for freeing it. */
18107 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18109 /* Is the file number a valid index into the line header's file name
18110 table? Remember that file numbers start with one, not zero. */
18111 if (1 <= file && file <= lh->num_file_names)
18113 char *relative = file_file_name (file, lh);
18115 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18117 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18120 return file_file_name (file, lh);
18124 static struct macro_source_file *
18125 macro_start_file (int file, int line,
18126 struct macro_source_file *current_file,
18127 const char *comp_dir,
18128 struct line_header *lh, struct objfile *objfile)
18130 /* File name relative to the compilation directory of this source file. */
18131 char *file_name = file_file_name (file, lh);
18133 /* We don't create a macro table for this compilation unit
18134 at all until we actually get a filename. */
18135 if (! pending_macros)
18136 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18137 objfile->per_bfd->macro_cache,
18140 if (! current_file)
18142 /* If we have no current file, then this must be the start_file
18143 directive for the compilation unit's main source file. */
18144 current_file = macro_set_main (pending_macros, file_name);
18145 macro_define_special (pending_macros);
18148 current_file = macro_include (current_file, line, file_name);
18152 return current_file;
18156 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18157 followed by a null byte. */
18159 copy_string (const char *buf, int len)
18161 char *s = xmalloc (len + 1);
18163 memcpy (s, buf, len);
18169 static const char *
18170 consume_improper_spaces (const char *p, const char *body)
18174 complaint (&symfile_complaints,
18175 _("macro definition contains spaces "
18176 "in formal argument list:\n`%s'"),
18188 parse_macro_definition (struct macro_source_file *file, int line,
18193 /* The body string takes one of two forms. For object-like macro
18194 definitions, it should be:
18196 <macro name> " " <definition>
18198 For function-like macro definitions, it should be:
18200 <macro name> "() " <definition>
18202 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18204 Spaces may appear only where explicitly indicated, and in the
18207 The Dwarf 2 spec says that an object-like macro's name is always
18208 followed by a space, but versions of GCC around March 2002 omit
18209 the space when the macro's definition is the empty string.
18211 The Dwarf 2 spec says that there should be no spaces between the
18212 formal arguments in a function-like macro's formal argument list,
18213 but versions of GCC around March 2002 include spaces after the
18217 /* Find the extent of the macro name. The macro name is terminated
18218 by either a space or null character (for an object-like macro) or
18219 an opening paren (for a function-like macro). */
18220 for (p = body; *p; p++)
18221 if (*p == ' ' || *p == '(')
18224 if (*p == ' ' || *p == '\0')
18226 /* It's an object-like macro. */
18227 int name_len = p - body;
18228 char *name = copy_string (body, name_len);
18229 const char *replacement;
18232 replacement = body + name_len + 1;
18235 dwarf2_macro_malformed_definition_complaint (body);
18236 replacement = body + name_len;
18239 macro_define_object (file, line, name, replacement);
18243 else if (*p == '(')
18245 /* It's a function-like macro. */
18246 char *name = copy_string (body, p - body);
18249 char **argv = xmalloc (argv_size * sizeof (*argv));
18253 p = consume_improper_spaces (p, body);
18255 /* Parse the formal argument list. */
18256 while (*p && *p != ')')
18258 /* Find the extent of the current argument name. */
18259 const char *arg_start = p;
18261 while (*p && *p != ',' && *p != ')' && *p != ' ')
18264 if (! *p || p == arg_start)
18265 dwarf2_macro_malformed_definition_complaint (body);
18268 /* Make sure argv has room for the new argument. */
18269 if (argc >= argv_size)
18272 argv = xrealloc (argv, argv_size * sizeof (*argv));
18275 argv[argc++] = copy_string (arg_start, p - arg_start);
18278 p = consume_improper_spaces (p, body);
18280 /* Consume the comma, if present. */
18285 p = consume_improper_spaces (p, body);
18294 /* Perfectly formed definition, no complaints. */
18295 macro_define_function (file, line, name,
18296 argc, (const char **) argv,
18298 else if (*p == '\0')
18300 /* Complain, but do define it. */
18301 dwarf2_macro_malformed_definition_complaint (body);
18302 macro_define_function (file, line, name,
18303 argc, (const char **) argv,
18307 /* Just complain. */
18308 dwarf2_macro_malformed_definition_complaint (body);
18311 /* Just complain. */
18312 dwarf2_macro_malformed_definition_complaint (body);
18318 for (i = 0; i < argc; i++)
18324 dwarf2_macro_malformed_definition_complaint (body);
18327 /* Skip some bytes from BYTES according to the form given in FORM.
18328 Returns the new pointer. */
18331 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18332 enum dwarf_form form,
18333 unsigned int offset_size,
18334 struct dwarf2_section_info *section)
18336 unsigned int bytes_read;
18340 case DW_FORM_data1:
18345 case DW_FORM_data2:
18349 case DW_FORM_data4:
18353 case DW_FORM_data8:
18357 case DW_FORM_string:
18358 read_direct_string (abfd, bytes, &bytes_read);
18359 bytes += bytes_read;
18362 case DW_FORM_sec_offset:
18364 case DW_FORM_GNU_strp_alt:
18365 bytes += offset_size;
18368 case DW_FORM_block:
18369 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18370 bytes += bytes_read;
18373 case DW_FORM_block1:
18374 bytes += 1 + read_1_byte (abfd, bytes);
18376 case DW_FORM_block2:
18377 bytes += 2 + read_2_bytes (abfd, bytes);
18379 case DW_FORM_block4:
18380 bytes += 4 + read_4_bytes (abfd, bytes);
18383 case DW_FORM_sdata:
18384 case DW_FORM_udata:
18385 case DW_FORM_GNU_addr_index:
18386 case DW_FORM_GNU_str_index:
18387 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18390 dwarf2_section_buffer_overflow_complaint (section);
18398 complaint (&symfile_complaints,
18399 _("invalid form 0x%x in `%s'"),
18401 section->asection->name);
18409 /* A helper for dwarf_decode_macros that handles skipping an unknown
18410 opcode. Returns an updated pointer to the macro data buffer; or,
18411 on error, issues a complaint and returns NULL. */
18414 skip_unknown_opcode (unsigned int opcode,
18415 gdb_byte **opcode_definitions,
18416 gdb_byte *mac_ptr, gdb_byte *mac_end,
18418 unsigned int offset_size,
18419 struct dwarf2_section_info *section)
18421 unsigned int bytes_read, i;
18425 if (opcode_definitions[opcode] == NULL)
18427 complaint (&symfile_complaints,
18428 _("unrecognized DW_MACFINO opcode 0x%x"),
18433 defn = opcode_definitions[opcode];
18434 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18435 defn += bytes_read;
18437 for (i = 0; i < arg; ++i)
18439 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18441 if (mac_ptr == NULL)
18443 /* skip_form_bytes already issued the complaint. */
18451 /* A helper function which parses the header of a macro section.
18452 If the macro section is the extended (for now called "GNU") type,
18453 then this updates *OFFSET_SIZE. Returns a pointer to just after
18454 the header, or issues a complaint and returns NULL on error. */
18457 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18460 unsigned int *offset_size,
18461 int section_is_gnu)
18463 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18465 if (section_is_gnu)
18467 unsigned int version, flags;
18469 version = read_2_bytes (abfd, mac_ptr);
18472 complaint (&symfile_complaints,
18473 _("unrecognized version `%d' in .debug_macro section"),
18479 flags = read_1_byte (abfd, mac_ptr);
18481 *offset_size = (flags & 1) ? 8 : 4;
18483 if ((flags & 2) != 0)
18484 /* We don't need the line table offset. */
18485 mac_ptr += *offset_size;
18487 /* Vendor opcode descriptions. */
18488 if ((flags & 4) != 0)
18490 unsigned int i, count;
18492 count = read_1_byte (abfd, mac_ptr);
18494 for (i = 0; i < count; ++i)
18496 unsigned int opcode, bytes_read;
18499 opcode = read_1_byte (abfd, mac_ptr);
18501 opcode_definitions[opcode] = mac_ptr;
18502 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18503 mac_ptr += bytes_read;
18512 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18513 including DW_MACRO_GNU_transparent_include. */
18516 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18517 struct macro_source_file *current_file,
18518 struct line_header *lh, const char *comp_dir,
18519 struct dwarf2_section_info *section,
18520 int section_is_gnu, int section_is_dwz,
18521 unsigned int offset_size,
18522 struct objfile *objfile,
18523 htab_t include_hash)
18525 enum dwarf_macro_record_type macinfo_type;
18526 int at_commandline;
18527 gdb_byte *opcode_definitions[256];
18529 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18530 &offset_size, section_is_gnu);
18531 if (mac_ptr == NULL)
18533 /* We already issued a complaint. */
18537 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18538 GDB is still reading the definitions from command line. First
18539 DW_MACINFO_start_file will need to be ignored as it was already executed
18540 to create CURRENT_FILE for the main source holding also the command line
18541 definitions. On first met DW_MACINFO_start_file this flag is reset to
18542 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18544 at_commandline = 1;
18548 /* Do we at least have room for a macinfo type byte? */
18549 if (mac_ptr >= mac_end)
18551 dwarf2_section_buffer_overflow_complaint (section);
18555 macinfo_type = read_1_byte (abfd, mac_ptr);
18558 /* Note that we rely on the fact that the corresponding GNU and
18559 DWARF constants are the same. */
18560 switch (macinfo_type)
18562 /* A zero macinfo type indicates the end of the macro
18567 case DW_MACRO_GNU_define:
18568 case DW_MACRO_GNU_undef:
18569 case DW_MACRO_GNU_define_indirect:
18570 case DW_MACRO_GNU_undef_indirect:
18571 case DW_MACRO_GNU_define_indirect_alt:
18572 case DW_MACRO_GNU_undef_indirect_alt:
18574 unsigned int bytes_read;
18579 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18580 mac_ptr += bytes_read;
18582 if (macinfo_type == DW_MACRO_GNU_define
18583 || macinfo_type == DW_MACRO_GNU_undef)
18585 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18586 mac_ptr += bytes_read;
18590 LONGEST str_offset;
18592 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18593 mac_ptr += offset_size;
18595 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18596 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18599 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18601 body = read_indirect_string_from_dwz (dwz, str_offset);
18604 body = read_indirect_string_at_offset (abfd, str_offset);
18607 is_define = (macinfo_type == DW_MACRO_GNU_define
18608 || macinfo_type == DW_MACRO_GNU_define_indirect
18609 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18610 if (! current_file)
18612 /* DWARF violation as no main source is present. */
18613 complaint (&symfile_complaints,
18614 _("debug info with no main source gives macro %s "
18616 is_define ? _("definition") : _("undefinition"),
18620 if ((line == 0 && !at_commandline)
18621 || (line != 0 && at_commandline))
18622 complaint (&symfile_complaints,
18623 _("debug info gives %s macro %s with %s line %d: %s"),
18624 at_commandline ? _("command-line") : _("in-file"),
18625 is_define ? _("definition") : _("undefinition"),
18626 line == 0 ? _("zero") : _("non-zero"), line, body);
18629 parse_macro_definition (current_file, line, body);
18632 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18633 || macinfo_type == DW_MACRO_GNU_undef_indirect
18634 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18635 macro_undef (current_file, line, body);
18640 case DW_MACRO_GNU_start_file:
18642 unsigned int bytes_read;
18645 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18646 mac_ptr += bytes_read;
18647 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18648 mac_ptr += bytes_read;
18650 if ((line == 0 && !at_commandline)
18651 || (line != 0 && at_commandline))
18652 complaint (&symfile_complaints,
18653 _("debug info gives source %d included "
18654 "from %s at %s line %d"),
18655 file, at_commandline ? _("command-line") : _("file"),
18656 line == 0 ? _("zero") : _("non-zero"), line);
18658 if (at_commandline)
18660 /* This DW_MACRO_GNU_start_file was executed in the
18662 at_commandline = 0;
18665 current_file = macro_start_file (file, line,
18666 current_file, comp_dir,
18671 case DW_MACRO_GNU_end_file:
18672 if (! current_file)
18673 complaint (&symfile_complaints,
18674 _("macro debug info has an unmatched "
18675 "`close_file' directive"));
18678 current_file = current_file->included_by;
18679 if (! current_file)
18681 enum dwarf_macro_record_type next_type;
18683 /* GCC circa March 2002 doesn't produce the zero
18684 type byte marking the end of the compilation
18685 unit. Complain if it's not there, but exit no
18688 /* Do we at least have room for a macinfo type byte? */
18689 if (mac_ptr >= mac_end)
18691 dwarf2_section_buffer_overflow_complaint (section);
18695 /* We don't increment mac_ptr here, so this is just
18697 next_type = read_1_byte (abfd, mac_ptr);
18698 if (next_type != 0)
18699 complaint (&symfile_complaints,
18700 _("no terminating 0-type entry for "
18701 "macros in `.debug_macinfo' section"));
18708 case DW_MACRO_GNU_transparent_include:
18709 case DW_MACRO_GNU_transparent_include_alt:
18713 bfd *include_bfd = abfd;
18714 struct dwarf2_section_info *include_section = section;
18715 struct dwarf2_section_info alt_section;
18716 gdb_byte *include_mac_end = mac_end;
18717 int is_dwz = section_is_dwz;
18718 gdb_byte *new_mac_ptr;
18720 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18721 mac_ptr += offset_size;
18723 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18725 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18727 dwarf2_read_section (dwarf2_per_objfile->objfile,
18730 include_bfd = dwz->macro.asection->owner;
18731 include_section = &dwz->macro;
18732 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18736 new_mac_ptr = include_section->buffer + offset;
18737 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18741 /* This has actually happened; see
18742 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18743 complaint (&symfile_complaints,
18744 _("recursive DW_MACRO_GNU_transparent_include in "
18745 ".debug_macro section"));
18749 *slot = new_mac_ptr;
18751 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18752 include_mac_end, current_file,
18754 section, section_is_gnu, is_dwz,
18755 offset_size, objfile, include_hash);
18757 htab_remove_elt (include_hash, new_mac_ptr);
18762 case DW_MACINFO_vendor_ext:
18763 if (!section_is_gnu)
18765 unsigned int bytes_read;
18768 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18769 mac_ptr += bytes_read;
18770 read_direct_string (abfd, mac_ptr, &bytes_read);
18771 mac_ptr += bytes_read;
18773 /* We don't recognize any vendor extensions. */
18779 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18780 mac_ptr, mac_end, abfd, offset_size,
18782 if (mac_ptr == NULL)
18786 } while (macinfo_type != 0);
18790 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18791 const char *comp_dir, int section_is_gnu)
18793 struct objfile *objfile = dwarf2_per_objfile->objfile;
18794 struct line_header *lh = cu->line_header;
18796 gdb_byte *mac_ptr, *mac_end;
18797 struct macro_source_file *current_file = 0;
18798 enum dwarf_macro_record_type macinfo_type;
18799 unsigned int offset_size = cu->header.offset_size;
18800 gdb_byte *opcode_definitions[256];
18801 struct cleanup *cleanup;
18802 htab_t include_hash;
18804 struct dwarf2_section_info *section;
18805 const char *section_name;
18807 if (cu->dwo_unit != NULL)
18809 if (section_is_gnu)
18811 section = &cu->dwo_unit->dwo_file->sections.macro;
18812 section_name = ".debug_macro.dwo";
18816 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18817 section_name = ".debug_macinfo.dwo";
18822 if (section_is_gnu)
18824 section = &dwarf2_per_objfile->macro;
18825 section_name = ".debug_macro";
18829 section = &dwarf2_per_objfile->macinfo;
18830 section_name = ".debug_macinfo";
18834 dwarf2_read_section (objfile, section);
18835 if (section->buffer == NULL)
18837 complaint (&symfile_complaints, _("missing %s section"), section_name);
18840 abfd = section->asection->owner;
18842 /* First pass: Find the name of the base filename.
18843 This filename is needed in order to process all macros whose definition
18844 (or undefinition) comes from the command line. These macros are defined
18845 before the first DW_MACINFO_start_file entry, and yet still need to be
18846 associated to the base file.
18848 To determine the base file name, we scan the macro definitions until we
18849 reach the first DW_MACINFO_start_file entry. We then initialize
18850 CURRENT_FILE accordingly so that any macro definition found before the
18851 first DW_MACINFO_start_file can still be associated to the base file. */
18853 mac_ptr = section->buffer + offset;
18854 mac_end = section->buffer + section->size;
18856 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18857 &offset_size, section_is_gnu);
18858 if (mac_ptr == NULL)
18860 /* We already issued a complaint. */
18866 /* Do we at least have room for a macinfo type byte? */
18867 if (mac_ptr >= mac_end)
18869 /* Complaint is printed during the second pass as GDB will probably
18870 stop the first pass earlier upon finding
18871 DW_MACINFO_start_file. */
18875 macinfo_type = read_1_byte (abfd, mac_ptr);
18878 /* Note that we rely on the fact that the corresponding GNU and
18879 DWARF constants are the same. */
18880 switch (macinfo_type)
18882 /* A zero macinfo type indicates the end of the macro
18887 case DW_MACRO_GNU_define:
18888 case DW_MACRO_GNU_undef:
18889 /* Only skip the data by MAC_PTR. */
18891 unsigned int bytes_read;
18893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18894 mac_ptr += bytes_read;
18895 read_direct_string (abfd, mac_ptr, &bytes_read);
18896 mac_ptr += bytes_read;
18900 case DW_MACRO_GNU_start_file:
18902 unsigned int bytes_read;
18905 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18906 mac_ptr += bytes_read;
18907 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18908 mac_ptr += bytes_read;
18910 current_file = macro_start_file (file, line, current_file,
18911 comp_dir, lh, objfile);
18915 case DW_MACRO_GNU_end_file:
18916 /* No data to skip by MAC_PTR. */
18919 case DW_MACRO_GNU_define_indirect:
18920 case DW_MACRO_GNU_undef_indirect:
18921 case DW_MACRO_GNU_define_indirect_alt:
18922 case DW_MACRO_GNU_undef_indirect_alt:
18924 unsigned int bytes_read;
18926 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18927 mac_ptr += bytes_read;
18928 mac_ptr += offset_size;
18932 case DW_MACRO_GNU_transparent_include:
18933 case DW_MACRO_GNU_transparent_include_alt:
18934 /* Note that, according to the spec, a transparent include
18935 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18936 skip this opcode. */
18937 mac_ptr += offset_size;
18940 case DW_MACINFO_vendor_ext:
18941 /* Only skip the data by MAC_PTR. */
18942 if (!section_is_gnu)
18944 unsigned int bytes_read;
18946 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18947 mac_ptr += bytes_read;
18948 read_direct_string (abfd, mac_ptr, &bytes_read);
18949 mac_ptr += bytes_read;
18954 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18955 mac_ptr, mac_end, abfd, offset_size,
18957 if (mac_ptr == NULL)
18961 } while (macinfo_type != 0 && current_file == NULL);
18963 /* Second pass: Process all entries.
18965 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18966 command-line macro definitions/undefinitions. This flag is unset when we
18967 reach the first DW_MACINFO_start_file entry. */
18969 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18970 NULL, xcalloc, xfree);
18971 cleanup = make_cleanup_htab_delete (include_hash);
18972 mac_ptr = section->buffer + offset;
18973 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18975 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18976 current_file, lh, comp_dir, section,
18978 offset_size, objfile, include_hash);
18979 do_cleanups (cleanup);
18982 /* Check if the attribute's form is a DW_FORM_block*
18983 if so return true else false. */
18986 attr_form_is_block (struct attribute *attr)
18988 return (attr == NULL ? 0 :
18989 attr->form == DW_FORM_block1
18990 || attr->form == DW_FORM_block2
18991 || attr->form == DW_FORM_block4
18992 || attr->form == DW_FORM_block
18993 || attr->form == DW_FORM_exprloc);
18996 /* Return non-zero if ATTR's value is a section offset --- classes
18997 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18998 You may use DW_UNSND (attr) to retrieve such offsets.
19000 Section 7.5.4, "Attribute Encodings", explains that no attribute
19001 may have a value that belongs to more than one of these classes; it
19002 would be ambiguous if we did, because we use the same forms for all
19006 attr_form_is_section_offset (struct attribute *attr)
19008 return (attr->form == DW_FORM_data4
19009 || attr->form == DW_FORM_data8
19010 || attr->form == DW_FORM_sec_offset);
19013 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19014 zero otherwise. When this function returns true, you can apply
19015 dwarf2_get_attr_constant_value to it.
19017 However, note that for some attributes you must check
19018 attr_form_is_section_offset before using this test. DW_FORM_data4
19019 and DW_FORM_data8 are members of both the constant class, and of
19020 the classes that contain offsets into other debug sections
19021 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19022 that, if an attribute's can be either a constant or one of the
19023 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19024 taken as section offsets, not constants. */
19027 attr_form_is_constant (struct attribute *attr)
19029 switch (attr->form)
19031 case DW_FORM_sdata:
19032 case DW_FORM_udata:
19033 case DW_FORM_data1:
19034 case DW_FORM_data2:
19035 case DW_FORM_data4:
19036 case DW_FORM_data8:
19043 /* Return the .debug_loc section to use for CU.
19044 For DWO files use .debug_loc.dwo. */
19046 static struct dwarf2_section_info *
19047 cu_debug_loc_section (struct dwarf2_cu *cu)
19050 return &cu->dwo_unit->dwo_file->sections.loc;
19051 return &dwarf2_per_objfile->loc;
19054 /* A helper function that fills in a dwarf2_loclist_baton. */
19057 fill_in_loclist_baton (struct dwarf2_cu *cu,
19058 struct dwarf2_loclist_baton *baton,
19059 struct attribute *attr)
19061 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19063 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19065 baton->per_cu = cu->per_cu;
19066 gdb_assert (baton->per_cu);
19067 /* We don't know how long the location list is, but make sure we
19068 don't run off the edge of the section. */
19069 baton->size = section->size - DW_UNSND (attr);
19070 baton->data = section->buffer + DW_UNSND (attr);
19071 baton->base_address = cu->base_address;
19072 baton->from_dwo = cu->dwo_unit != NULL;
19076 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19077 struct dwarf2_cu *cu, int is_block)
19079 struct objfile *objfile = dwarf2_per_objfile->objfile;
19080 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19082 if (attr_form_is_section_offset (attr)
19083 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19084 the section. If so, fall through to the complaint in the
19086 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19088 struct dwarf2_loclist_baton *baton;
19090 baton = obstack_alloc (&objfile->objfile_obstack,
19091 sizeof (struct dwarf2_loclist_baton));
19093 fill_in_loclist_baton (cu, baton, attr);
19095 if (cu->base_known == 0)
19096 complaint (&symfile_complaints,
19097 _("Location list used without "
19098 "specifying the CU base address."));
19100 SYMBOL_ACLASS_INDEX (sym) = (is_block
19101 ? dwarf2_loclist_block_index
19102 : dwarf2_loclist_index);
19103 SYMBOL_LOCATION_BATON (sym) = baton;
19107 struct dwarf2_locexpr_baton *baton;
19109 baton = obstack_alloc (&objfile->objfile_obstack,
19110 sizeof (struct dwarf2_locexpr_baton));
19111 baton->per_cu = cu->per_cu;
19112 gdb_assert (baton->per_cu);
19114 if (attr_form_is_block (attr))
19116 /* Note that we're just copying the block's data pointer
19117 here, not the actual data. We're still pointing into the
19118 info_buffer for SYM's objfile; right now we never release
19119 that buffer, but when we do clean up properly this may
19121 baton->size = DW_BLOCK (attr)->size;
19122 baton->data = DW_BLOCK (attr)->data;
19126 dwarf2_invalid_attrib_class_complaint ("location description",
19127 SYMBOL_NATURAL_NAME (sym));
19131 SYMBOL_ACLASS_INDEX (sym) = (is_block
19132 ? dwarf2_locexpr_block_index
19133 : dwarf2_locexpr_index);
19134 SYMBOL_LOCATION_BATON (sym) = baton;
19138 /* Return the OBJFILE associated with the compilation unit CU. If CU
19139 came from a separate debuginfo file, then the master objfile is
19143 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19145 struct objfile *objfile = per_cu->objfile;
19147 /* Return the master objfile, so that we can report and look up the
19148 correct file containing this variable. */
19149 if (objfile->separate_debug_objfile_backlink)
19150 objfile = objfile->separate_debug_objfile_backlink;
19155 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19156 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19157 CU_HEADERP first. */
19159 static const struct comp_unit_head *
19160 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19161 struct dwarf2_per_cu_data *per_cu)
19163 gdb_byte *info_ptr;
19166 return &per_cu->cu->header;
19168 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19170 memset (cu_headerp, 0, sizeof (*cu_headerp));
19171 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19176 /* Return the address size given in the compilation unit header for CU. */
19179 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19181 struct comp_unit_head cu_header_local;
19182 const struct comp_unit_head *cu_headerp;
19184 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19186 return cu_headerp->addr_size;
19189 /* Return the offset size given in the compilation unit header for CU. */
19192 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19194 struct comp_unit_head cu_header_local;
19195 const struct comp_unit_head *cu_headerp;
19197 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19199 return cu_headerp->offset_size;
19202 /* See its dwarf2loc.h declaration. */
19205 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19207 struct comp_unit_head cu_header_local;
19208 const struct comp_unit_head *cu_headerp;
19210 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19212 if (cu_headerp->version == 2)
19213 return cu_headerp->addr_size;
19215 return cu_headerp->offset_size;
19218 /* Return the text offset of the CU. The returned offset comes from
19219 this CU's objfile. If this objfile came from a separate debuginfo
19220 file, then the offset may be different from the corresponding
19221 offset in the parent objfile. */
19224 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19226 struct objfile *objfile = per_cu->objfile;
19228 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19231 /* Locate the .debug_info compilation unit from CU's objfile which contains
19232 the DIE at OFFSET. Raises an error on failure. */
19234 static struct dwarf2_per_cu_data *
19235 dwarf2_find_containing_comp_unit (sect_offset offset,
19236 unsigned int offset_in_dwz,
19237 struct objfile *objfile)
19239 struct dwarf2_per_cu_data *this_cu;
19241 const sect_offset *cu_off;
19244 high = dwarf2_per_objfile->n_comp_units - 1;
19247 struct dwarf2_per_cu_data *mid_cu;
19248 int mid = low + (high - low) / 2;
19250 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19251 cu_off = &mid_cu->offset;
19252 if (mid_cu->is_dwz > offset_in_dwz
19253 || (mid_cu->is_dwz == offset_in_dwz
19254 && cu_off->sect_off >= offset.sect_off))
19259 gdb_assert (low == high);
19260 this_cu = dwarf2_per_objfile->all_comp_units[low];
19261 cu_off = &this_cu->offset;
19262 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19264 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19265 error (_("Dwarf Error: could not find partial DIE containing "
19266 "offset 0x%lx [in module %s]"),
19267 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19269 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19270 <= offset.sect_off);
19271 return dwarf2_per_objfile->all_comp_units[low-1];
19275 this_cu = dwarf2_per_objfile->all_comp_units[low];
19276 if (low == dwarf2_per_objfile->n_comp_units - 1
19277 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19278 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19279 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19284 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19287 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19289 memset (cu, 0, sizeof (*cu));
19291 cu->per_cu = per_cu;
19292 cu->objfile = per_cu->objfile;
19293 obstack_init (&cu->comp_unit_obstack);
19296 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19299 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19300 enum language pretend_language)
19302 struct attribute *attr;
19304 /* Set the language we're debugging. */
19305 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19307 set_cu_language (DW_UNSND (attr), cu);
19310 cu->language = pretend_language;
19311 cu->language_defn = language_def (cu->language);
19314 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19316 cu->producer = DW_STRING (attr);
19319 /* Release one cached compilation unit, CU. We unlink it from the tree
19320 of compilation units, but we don't remove it from the read_in_chain;
19321 the caller is responsible for that.
19322 NOTE: DATA is a void * because this function is also used as a
19323 cleanup routine. */
19326 free_heap_comp_unit (void *data)
19328 struct dwarf2_cu *cu = data;
19330 gdb_assert (cu->per_cu != NULL);
19331 cu->per_cu->cu = NULL;
19334 obstack_free (&cu->comp_unit_obstack, NULL);
19339 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19340 when we're finished with it. We can't free the pointer itself, but be
19341 sure to unlink it from the cache. Also release any associated storage. */
19344 free_stack_comp_unit (void *data)
19346 struct dwarf2_cu *cu = data;
19348 gdb_assert (cu->per_cu != NULL);
19349 cu->per_cu->cu = NULL;
19352 obstack_free (&cu->comp_unit_obstack, NULL);
19353 cu->partial_dies = NULL;
19356 /* Free all cached compilation units. */
19359 free_cached_comp_units (void *data)
19361 struct dwarf2_per_cu_data *per_cu, **last_chain;
19363 per_cu = dwarf2_per_objfile->read_in_chain;
19364 last_chain = &dwarf2_per_objfile->read_in_chain;
19365 while (per_cu != NULL)
19367 struct dwarf2_per_cu_data *next_cu;
19369 next_cu = per_cu->cu->read_in_chain;
19371 free_heap_comp_unit (per_cu->cu);
19372 *last_chain = next_cu;
19378 /* Increase the age counter on each cached compilation unit, and free
19379 any that are too old. */
19382 age_cached_comp_units (void)
19384 struct dwarf2_per_cu_data *per_cu, **last_chain;
19386 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19387 per_cu = dwarf2_per_objfile->read_in_chain;
19388 while (per_cu != NULL)
19390 per_cu->cu->last_used ++;
19391 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19392 dwarf2_mark (per_cu->cu);
19393 per_cu = per_cu->cu->read_in_chain;
19396 per_cu = dwarf2_per_objfile->read_in_chain;
19397 last_chain = &dwarf2_per_objfile->read_in_chain;
19398 while (per_cu != NULL)
19400 struct dwarf2_per_cu_data *next_cu;
19402 next_cu = per_cu->cu->read_in_chain;
19404 if (!per_cu->cu->mark)
19406 free_heap_comp_unit (per_cu->cu);
19407 *last_chain = next_cu;
19410 last_chain = &per_cu->cu->read_in_chain;
19416 /* Remove a single compilation unit from the cache. */
19419 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19421 struct dwarf2_per_cu_data *per_cu, **last_chain;
19423 per_cu = dwarf2_per_objfile->read_in_chain;
19424 last_chain = &dwarf2_per_objfile->read_in_chain;
19425 while (per_cu != NULL)
19427 struct dwarf2_per_cu_data *next_cu;
19429 next_cu = per_cu->cu->read_in_chain;
19431 if (per_cu == target_per_cu)
19433 free_heap_comp_unit (per_cu->cu);
19435 *last_chain = next_cu;
19439 last_chain = &per_cu->cu->read_in_chain;
19445 /* Release all extra memory associated with OBJFILE. */
19448 dwarf2_free_objfile (struct objfile *objfile)
19450 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19452 if (dwarf2_per_objfile == NULL)
19455 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19456 free_cached_comp_units (NULL);
19458 if (dwarf2_per_objfile->quick_file_names_table)
19459 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19461 /* Everything else should be on the objfile obstack. */
19464 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19465 We store these in a hash table separate from the DIEs, and preserve them
19466 when the DIEs are flushed out of cache.
19468 The CU "per_cu" pointer is needed because offset alone is not enough to
19469 uniquely identify the type. A file may have multiple .debug_types sections,
19470 or the type may come from a DWO file. We have to use something in
19471 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19472 routine, get_die_type_at_offset, from outside this file, and thus won't
19473 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19476 struct dwarf2_per_cu_offset_and_type
19478 const struct dwarf2_per_cu_data *per_cu;
19479 sect_offset offset;
19483 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19486 per_cu_offset_and_type_hash (const void *item)
19488 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19490 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19493 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19496 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19498 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19499 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19501 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19502 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19505 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19506 table if necessary. For convenience, return TYPE.
19508 The DIEs reading must have careful ordering to:
19509 * Not cause infite loops trying to read in DIEs as a prerequisite for
19510 reading current DIE.
19511 * Not trying to dereference contents of still incompletely read in types
19512 while reading in other DIEs.
19513 * Enable referencing still incompletely read in types just by a pointer to
19514 the type without accessing its fields.
19516 Therefore caller should follow these rules:
19517 * Try to fetch any prerequisite types we may need to build this DIE type
19518 before building the type and calling set_die_type.
19519 * After building type call set_die_type for current DIE as soon as
19520 possible before fetching more types to complete the current type.
19521 * Make the type as complete as possible before fetching more types. */
19523 static struct type *
19524 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19526 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19527 struct objfile *objfile = cu->objfile;
19529 /* For Ada types, make sure that the gnat-specific data is always
19530 initialized (if not already set). There are a few types where
19531 we should not be doing so, because the type-specific area is
19532 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19533 where the type-specific area is used to store the floatformat).
19534 But this is not a problem, because the gnat-specific information
19535 is actually not needed for these types. */
19536 if (need_gnat_info (cu)
19537 && TYPE_CODE (type) != TYPE_CODE_FUNC
19538 && TYPE_CODE (type) != TYPE_CODE_FLT
19539 && !HAVE_GNAT_AUX_INFO (type))
19540 INIT_GNAT_SPECIFIC (type);
19542 if (dwarf2_per_objfile->die_type_hash == NULL)
19544 dwarf2_per_objfile->die_type_hash =
19545 htab_create_alloc_ex (127,
19546 per_cu_offset_and_type_hash,
19547 per_cu_offset_and_type_eq,
19549 &objfile->objfile_obstack,
19550 hashtab_obstack_allocate,
19551 dummy_obstack_deallocate);
19554 ofs.per_cu = cu->per_cu;
19555 ofs.offset = die->offset;
19557 slot = (struct dwarf2_per_cu_offset_and_type **)
19558 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19560 complaint (&symfile_complaints,
19561 _("A problem internal to GDB: DIE 0x%x has type already set"),
19562 die->offset.sect_off);
19563 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19568 /* Look up the type for the die at OFFSET in the appropriate type_hash
19569 table, or return NULL if the die does not have a saved type. */
19571 static struct type *
19572 get_die_type_at_offset (sect_offset offset,
19573 struct dwarf2_per_cu_data *per_cu)
19575 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19577 if (dwarf2_per_objfile->die_type_hash == NULL)
19580 ofs.per_cu = per_cu;
19581 ofs.offset = offset;
19582 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19589 /* Look up the type for DIE in the appropriate type_hash table,
19590 or return NULL if DIE does not have a saved type. */
19592 static struct type *
19593 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19595 return get_die_type_at_offset (die->offset, cu->per_cu);
19598 /* Add a dependence relationship from CU to REF_PER_CU. */
19601 dwarf2_add_dependence (struct dwarf2_cu *cu,
19602 struct dwarf2_per_cu_data *ref_per_cu)
19606 if (cu->dependencies == NULL)
19608 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19609 NULL, &cu->comp_unit_obstack,
19610 hashtab_obstack_allocate,
19611 dummy_obstack_deallocate);
19613 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19615 *slot = ref_per_cu;
19618 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19619 Set the mark field in every compilation unit in the
19620 cache that we must keep because we are keeping CU. */
19623 dwarf2_mark_helper (void **slot, void *data)
19625 struct dwarf2_per_cu_data *per_cu;
19627 per_cu = (struct dwarf2_per_cu_data *) *slot;
19629 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19630 reading of the chain. As such dependencies remain valid it is not much
19631 useful to track and undo them during QUIT cleanups. */
19632 if (per_cu->cu == NULL)
19635 if (per_cu->cu->mark)
19637 per_cu->cu->mark = 1;
19639 if (per_cu->cu->dependencies != NULL)
19640 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19645 /* Set the mark field in CU and in every other compilation unit in the
19646 cache that we must keep because we are keeping CU. */
19649 dwarf2_mark (struct dwarf2_cu *cu)
19654 if (cu->dependencies != NULL)
19655 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19659 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19663 per_cu->cu->mark = 0;
19664 per_cu = per_cu->cu->read_in_chain;
19668 /* Trivial hash function for partial_die_info: the hash value of a DIE
19669 is its offset in .debug_info for this objfile. */
19672 partial_die_hash (const void *item)
19674 const struct partial_die_info *part_die = item;
19676 return part_die->offset.sect_off;
19679 /* Trivial comparison function for partial_die_info structures: two DIEs
19680 are equal if they have the same offset. */
19683 partial_die_eq (const void *item_lhs, const void *item_rhs)
19685 const struct partial_die_info *part_die_lhs = item_lhs;
19686 const struct partial_die_info *part_die_rhs = item_rhs;
19688 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19691 static struct cmd_list_element *set_dwarf2_cmdlist;
19692 static struct cmd_list_element *show_dwarf2_cmdlist;
19695 set_dwarf2_cmd (char *args, int from_tty)
19697 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19701 show_dwarf2_cmd (char *args, int from_tty)
19703 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19706 /* Free data associated with OBJFILE, if necessary. */
19709 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19711 struct dwarf2_per_objfile *data = d;
19714 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19715 VEC_free (dwarf2_per_cu_ptr,
19716 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19718 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19719 VEC_free (dwarf2_per_cu_ptr,
19720 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19722 VEC_free (dwarf2_section_info_def, data->types);
19724 if (data->dwo_files)
19725 free_dwo_files (data->dwo_files, objfile);
19727 if (data->dwz_file && data->dwz_file->dwz_bfd)
19728 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19732 /* The "save gdb-index" command. */
19734 /* The contents of the hash table we create when building the string
19736 struct strtab_entry
19738 offset_type offset;
19742 /* Hash function for a strtab_entry.
19744 Function is used only during write_hash_table so no index format backward
19745 compatibility is needed. */
19748 hash_strtab_entry (const void *e)
19750 const struct strtab_entry *entry = e;
19751 return mapped_index_string_hash (INT_MAX, entry->str);
19754 /* Equality function for a strtab_entry. */
19757 eq_strtab_entry (const void *a, const void *b)
19759 const struct strtab_entry *ea = a;
19760 const struct strtab_entry *eb = b;
19761 return !strcmp (ea->str, eb->str);
19764 /* Create a strtab_entry hash table. */
19767 create_strtab (void)
19769 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19770 xfree, xcalloc, xfree);
19773 /* Add a string to the constant pool. Return the string's offset in
19777 add_string (htab_t table, struct obstack *cpool, const char *str)
19780 struct strtab_entry entry;
19781 struct strtab_entry *result;
19784 slot = htab_find_slot (table, &entry, INSERT);
19789 result = XNEW (struct strtab_entry);
19790 result->offset = obstack_object_size (cpool);
19792 obstack_grow_str0 (cpool, str);
19795 return result->offset;
19798 /* An entry in the symbol table. */
19799 struct symtab_index_entry
19801 /* The name of the symbol. */
19803 /* The offset of the name in the constant pool. */
19804 offset_type index_offset;
19805 /* A sorted vector of the indices of all the CUs that hold an object
19807 VEC (offset_type) *cu_indices;
19810 /* The symbol table. This is a power-of-2-sized hash table. */
19811 struct mapped_symtab
19813 offset_type n_elements;
19815 struct symtab_index_entry **data;
19818 /* Hash function for a symtab_index_entry. */
19821 hash_symtab_entry (const void *e)
19823 const struct symtab_index_entry *entry = e;
19824 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19825 sizeof (offset_type) * VEC_length (offset_type,
19826 entry->cu_indices),
19830 /* Equality function for a symtab_index_entry. */
19833 eq_symtab_entry (const void *a, const void *b)
19835 const struct symtab_index_entry *ea = a;
19836 const struct symtab_index_entry *eb = b;
19837 int len = VEC_length (offset_type, ea->cu_indices);
19838 if (len != VEC_length (offset_type, eb->cu_indices))
19840 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19841 VEC_address (offset_type, eb->cu_indices),
19842 sizeof (offset_type) * len);
19845 /* Destroy a symtab_index_entry. */
19848 delete_symtab_entry (void *p)
19850 struct symtab_index_entry *entry = p;
19851 VEC_free (offset_type, entry->cu_indices);
19855 /* Create a hash table holding symtab_index_entry objects. */
19858 create_symbol_hash_table (void)
19860 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19861 delete_symtab_entry, xcalloc, xfree);
19864 /* Create a new mapped symtab object. */
19866 static struct mapped_symtab *
19867 create_mapped_symtab (void)
19869 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19870 symtab->n_elements = 0;
19871 symtab->size = 1024;
19872 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19876 /* Destroy a mapped_symtab. */
19879 cleanup_mapped_symtab (void *p)
19881 struct mapped_symtab *symtab = p;
19882 /* The contents of the array are freed when the other hash table is
19884 xfree (symtab->data);
19888 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19891 Function is used only during write_hash_table so no index format backward
19892 compatibility is needed. */
19894 static struct symtab_index_entry **
19895 find_slot (struct mapped_symtab *symtab, const char *name)
19897 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19899 index = hash & (symtab->size - 1);
19900 step = ((hash * 17) & (symtab->size - 1)) | 1;
19904 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19905 return &symtab->data[index];
19906 index = (index + step) & (symtab->size - 1);
19910 /* Expand SYMTAB's hash table. */
19913 hash_expand (struct mapped_symtab *symtab)
19915 offset_type old_size = symtab->size;
19917 struct symtab_index_entry **old_entries = symtab->data;
19920 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19922 for (i = 0; i < old_size; ++i)
19924 if (old_entries[i])
19926 struct symtab_index_entry **slot = find_slot (symtab,
19927 old_entries[i]->name);
19928 *slot = old_entries[i];
19932 xfree (old_entries);
19935 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19936 CU_INDEX is the index of the CU in which the symbol appears.
19937 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19940 add_index_entry (struct mapped_symtab *symtab, const char *name,
19941 int is_static, gdb_index_symbol_kind kind,
19942 offset_type cu_index)
19944 struct symtab_index_entry **slot;
19945 offset_type cu_index_and_attrs;
19947 ++symtab->n_elements;
19948 if (4 * symtab->n_elements / 3 >= symtab->size)
19949 hash_expand (symtab);
19951 slot = find_slot (symtab, name);
19954 *slot = XNEW (struct symtab_index_entry);
19955 (*slot)->name = name;
19956 /* index_offset is set later. */
19957 (*slot)->cu_indices = NULL;
19960 cu_index_and_attrs = 0;
19961 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19962 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19963 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19965 /* We don't want to record an index value twice as we want to avoid the
19967 We process all global symbols and then all static symbols
19968 (which would allow us to avoid the duplication by only having to check
19969 the last entry pushed), but a symbol could have multiple kinds in one CU.
19970 To keep things simple we don't worry about the duplication here and
19971 sort and uniqufy the list after we've processed all symbols. */
19972 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19975 /* qsort helper routine for uniquify_cu_indices. */
19978 offset_type_compare (const void *ap, const void *bp)
19980 offset_type a = *(offset_type *) ap;
19981 offset_type b = *(offset_type *) bp;
19983 return (a > b) - (b > a);
19986 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19989 uniquify_cu_indices (struct mapped_symtab *symtab)
19993 for (i = 0; i < symtab->size; ++i)
19995 struct symtab_index_entry *entry = symtab->data[i];
19998 && entry->cu_indices != NULL)
20000 unsigned int next_to_insert, next_to_check;
20001 offset_type last_value;
20003 qsort (VEC_address (offset_type, entry->cu_indices),
20004 VEC_length (offset_type, entry->cu_indices),
20005 sizeof (offset_type), offset_type_compare);
20007 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20008 next_to_insert = 1;
20009 for (next_to_check = 1;
20010 next_to_check < VEC_length (offset_type, entry->cu_indices);
20013 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20016 last_value = VEC_index (offset_type, entry->cu_indices,
20018 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20023 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20028 /* Add a vector of indices to the constant pool. */
20031 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20032 struct symtab_index_entry *entry)
20036 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20039 offset_type len = VEC_length (offset_type, entry->cu_indices);
20040 offset_type val = MAYBE_SWAP (len);
20045 entry->index_offset = obstack_object_size (cpool);
20047 obstack_grow (cpool, &val, sizeof (val));
20049 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20052 val = MAYBE_SWAP (iter);
20053 obstack_grow (cpool, &val, sizeof (val));
20058 struct symtab_index_entry *old_entry = *slot;
20059 entry->index_offset = old_entry->index_offset;
20062 return entry->index_offset;
20065 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20066 constant pool entries going into the obstack CPOOL. */
20069 write_hash_table (struct mapped_symtab *symtab,
20070 struct obstack *output, struct obstack *cpool)
20073 htab_t symbol_hash_table;
20076 symbol_hash_table = create_symbol_hash_table ();
20077 str_table = create_strtab ();
20079 /* We add all the index vectors to the constant pool first, to
20080 ensure alignment is ok. */
20081 for (i = 0; i < symtab->size; ++i)
20083 if (symtab->data[i])
20084 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20087 /* Now write out the hash table. */
20088 for (i = 0; i < symtab->size; ++i)
20090 offset_type str_off, vec_off;
20092 if (symtab->data[i])
20094 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20095 vec_off = symtab->data[i]->index_offset;
20099 /* While 0 is a valid constant pool index, it is not valid
20100 to have 0 for both offsets. */
20105 str_off = MAYBE_SWAP (str_off);
20106 vec_off = MAYBE_SWAP (vec_off);
20108 obstack_grow (output, &str_off, sizeof (str_off));
20109 obstack_grow (output, &vec_off, sizeof (vec_off));
20112 htab_delete (str_table);
20113 htab_delete (symbol_hash_table);
20116 /* Struct to map psymtab to CU index in the index file. */
20117 struct psymtab_cu_index_map
20119 struct partial_symtab *psymtab;
20120 unsigned int cu_index;
20124 hash_psymtab_cu_index (const void *item)
20126 const struct psymtab_cu_index_map *map = item;
20128 return htab_hash_pointer (map->psymtab);
20132 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20134 const struct psymtab_cu_index_map *lhs = item_lhs;
20135 const struct psymtab_cu_index_map *rhs = item_rhs;
20137 return lhs->psymtab == rhs->psymtab;
20140 /* Helper struct for building the address table. */
20141 struct addrmap_index_data
20143 struct objfile *objfile;
20144 struct obstack *addr_obstack;
20145 htab_t cu_index_htab;
20147 /* Non-zero if the previous_* fields are valid.
20148 We can't write an entry until we see the next entry (since it is only then
20149 that we know the end of the entry). */
20150 int previous_valid;
20151 /* Index of the CU in the table of all CUs in the index file. */
20152 unsigned int previous_cu_index;
20153 /* Start address of the CU. */
20154 CORE_ADDR previous_cu_start;
20157 /* Write an address entry to OBSTACK. */
20160 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20161 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20163 offset_type cu_index_to_write;
20165 CORE_ADDR baseaddr;
20167 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20169 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20170 obstack_grow (obstack, addr, 8);
20171 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20172 obstack_grow (obstack, addr, 8);
20173 cu_index_to_write = MAYBE_SWAP (cu_index);
20174 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20177 /* Worker function for traversing an addrmap to build the address table. */
20180 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20182 struct addrmap_index_data *data = datap;
20183 struct partial_symtab *pst = obj;
20185 if (data->previous_valid)
20186 add_address_entry (data->objfile, data->addr_obstack,
20187 data->previous_cu_start, start_addr,
20188 data->previous_cu_index);
20190 data->previous_cu_start = start_addr;
20193 struct psymtab_cu_index_map find_map, *map;
20194 find_map.psymtab = pst;
20195 map = htab_find (data->cu_index_htab, &find_map);
20196 gdb_assert (map != NULL);
20197 data->previous_cu_index = map->cu_index;
20198 data->previous_valid = 1;
20201 data->previous_valid = 0;
20206 /* Write OBJFILE's address map to OBSTACK.
20207 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20208 in the index file. */
20211 write_address_map (struct objfile *objfile, struct obstack *obstack,
20212 htab_t cu_index_htab)
20214 struct addrmap_index_data addrmap_index_data;
20216 /* When writing the address table, we have to cope with the fact that
20217 the addrmap iterator only provides the start of a region; we have to
20218 wait until the next invocation to get the start of the next region. */
20220 addrmap_index_data.objfile = objfile;
20221 addrmap_index_data.addr_obstack = obstack;
20222 addrmap_index_data.cu_index_htab = cu_index_htab;
20223 addrmap_index_data.previous_valid = 0;
20225 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20226 &addrmap_index_data);
20228 /* It's highly unlikely the last entry (end address = 0xff...ff)
20229 is valid, but we should still handle it.
20230 The end address is recorded as the start of the next region, but that
20231 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20233 if (addrmap_index_data.previous_valid)
20234 add_address_entry (objfile, obstack,
20235 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20236 addrmap_index_data.previous_cu_index);
20239 /* Return the symbol kind of PSYM. */
20241 static gdb_index_symbol_kind
20242 symbol_kind (struct partial_symbol *psym)
20244 domain_enum domain = PSYMBOL_DOMAIN (psym);
20245 enum address_class aclass = PSYMBOL_CLASS (psym);
20253 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20255 return GDB_INDEX_SYMBOL_KIND_TYPE;
20257 case LOC_CONST_BYTES:
20258 case LOC_OPTIMIZED_OUT:
20260 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20262 /* Note: It's currently impossible to recognize psyms as enum values
20263 short of reading the type info. For now punt. */
20264 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20266 /* There are other LOC_FOO values that one might want to classify
20267 as variables, but dwarf2read.c doesn't currently use them. */
20268 return GDB_INDEX_SYMBOL_KIND_OTHER;
20270 case STRUCT_DOMAIN:
20271 return GDB_INDEX_SYMBOL_KIND_TYPE;
20273 return GDB_INDEX_SYMBOL_KIND_OTHER;
20277 /* Add a list of partial symbols to SYMTAB. */
20280 write_psymbols (struct mapped_symtab *symtab,
20282 struct partial_symbol **psymp,
20284 offset_type cu_index,
20287 for (; count-- > 0; ++psymp)
20289 struct partial_symbol *psym = *psymp;
20292 if (SYMBOL_LANGUAGE (psym) == language_ada)
20293 error (_("Ada is not currently supported by the index"));
20295 /* Only add a given psymbol once. */
20296 slot = htab_find_slot (psyms_seen, psym, INSERT);
20299 gdb_index_symbol_kind kind = symbol_kind (psym);
20302 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20303 is_static, kind, cu_index);
20308 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20309 exception if there is an error. */
20312 write_obstack (FILE *file, struct obstack *obstack)
20314 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20316 != obstack_object_size (obstack))
20317 error (_("couldn't data write to file"));
20320 /* Unlink a file if the argument is not NULL. */
20323 unlink_if_set (void *p)
20325 char **filename = p;
20327 unlink (*filename);
20330 /* A helper struct used when iterating over debug_types. */
20331 struct signatured_type_index_data
20333 struct objfile *objfile;
20334 struct mapped_symtab *symtab;
20335 struct obstack *types_list;
20340 /* A helper function that writes a single signatured_type to an
20344 write_one_signatured_type (void **slot, void *d)
20346 struct signatured_type_index_data *info = d;
20347 struct signatured_type *entry = (struct signatured_type *) *slot;
20348 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20349 struct partial_symtab *psymtab = per_cu->v.psymtab;
20352 write_psymbols (info->symtab,
20354 info->objfile->global_psymbols.list
20355 + psymtab->globals_offset,
20356 psymtab->n_global_syms, info->cu_index,
20358 write_psymbols (info->symtab,
20360 info->objfile->static_psymbols.list
20361 + psymtab->statics_offset,
20362 psymtab->n_static_syms, info->cu_index,
20365 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20366 entry->per_cu.offset.sect_off);
20367 obstack_grow (info->types_list, val, 8);
20368 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20369 entry->type_offset_in_tu.cu_off);
20370 obstack_grow (info->types_list, val, 8);
20371 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20372 obstack_grow (info->types_list, val, 8);
20379 /* Recurse into all "included" dependencies and write their symbols as
20380 if they appeared in this psymtab. */
20383 recursively_write_psymbols (struct objfile *objfile,
20384 struct partial_symtab *psymtab,
20385 struct mapped_symtab *symtab,
20387 offset_type cu_index)
20391 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20392 if (psymtab->dependencies[i]->user != NULL)
20393 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20394 symtab, psyms_seen, cu_index);
20396 write_psymbols (symtab,
20398 objfile->global_psymbols.list + psymtab->globals_offset,
20399 psymtab->n_global_syms, cu_index,
20401 write_psymbols (symtab,
20403 objfile->static_psymbols.list + psymtab->statics_offset,
20404 psymtab->n_static_syms, cu_index,
20408 /* Create an index file for OBJFILE in the directory DIR. */
20411 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20413 struct cleanup *cleanup;
20414 char *filename, *cleanup_filename;
20415 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20416 struct obstack cu_list, types_cu_list;
20419 struct mapped_symtab *symtab;
20420 offset_type val, size_of_contents, total_len;
20423 htab_t cu_index_htab;
20424 struct psymtab_cu_index_map *psymtab_cu_index_map;
20426 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20429 if (dwarf2_per_objfile->using_index)
20430 error (_("Cannot use an index to create the index"));
20432 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20433 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20435 if (stat (objfile->name, &st) < 0)
20436 perror_with_name (objfile->name);
20438 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20439 INDEX_SUFFIX, (char *) NULL);
20440 cleanup = make_cleanup (xfree, filename);
20442 out_file = fopen (filename, "wb");
20444 error (_("Can't open `%s' for writing"), filename);
20446 cleanup_filename = filename;
20447 make_cleanup (unlink_if_set, &cleanup_filename);
20449 symtab = create_mapped_symtab ();
20450 make_cleanup (cleanup_mapped_symtab, symtab);
20452 obstack_init (&addr_obstack);
20453 make_cleanup_obstack_free (&addr_obstack);
20455 obstack_init (&cu_list);
20456 make_cleanup_obstack_free (&cu_list);
20458 obstack_init (&types_cu_list);
20459 make_cleanup_obstack_free (&types_cu_list);
20461 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20462 NULL, xcalloc, xfree);
20463 make_cleanup_htab_delete (psyms_seen);
20465 /* While we're scanning CU's create a table that maps a psymtab pointer
20466 (which is what addrmap records) to its index (which is what is recorded
20467 in the index file). This will later be needed to write the address
20469 cu_index_htab = htab_create_alloc (100,
20470 hash_psymtab_cu_index,
20471 eq_psymtab_cu_index,
20472 NULL, xcalloc, xfree);
20473 make_cleanup_htab_delete (cu_index_htab);
20474 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20475 xmalloc (sizeof (struct psymtab_cu_index_map)
20476 * dwarf2_per_objfile->n_comp_units);
20477 make_cleanup (xfree, psymtab_cu_index_map);
20479 /* The CU list is already sorted, so we don't need to do additional
20480 work here. Also, the debug_types entries do not appear in
20481 all_comp_units, but only in their own hash table. */
20482 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20484 struct dwarf2_per_cu_data *per_cu
20485 = dwarf2_per_objfile->all_comp_units[i];
20486 struct partial_symtab *psymtab = per_cu->v.psymtab;
20488 struct psymtab_cu_index_map *map;
20491 if (psymtab->user == NULL)
20492 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20494 map = &psymtab_cu_index_map[i];
20495 map->psymtab = psymtab;
20497 slot = htab_find_slot (cu_index_htab, map, INSERT);
20498 gdb_assert (slot != NULL);
20499 gdb_assert (*slot == NULL);
20502 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20503 per_cu->offset.sect_off);
20504 obstack_grow (&cu_list, val, 8);
20505 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20506 obstack_grow (&cu_list, val, 8);
20509 /* Dump the address map. */
20510 write_address_map (objfile, &addr_obstack, cu_index_htab);
20512 /* Write out the .debug_type entries, if any. */
20513 if (dwarf2_per_objfile->signatured_types)
20515 struct signatured_type_index_data sig_data;
20517 sig_data.objfile = objfile;
20518 sig_data.symtab = symtab;
20519 sig_data.types_list = &types_cu_list;
20520 sig_data.psyms_seen = psyms_seen;
20521 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20522 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20523 write_one_signatured_type, &sig_data);
20526 /* Now that we've processed all symbols we can shrink their cu_indices
20528 uniquify_cu_indices (symtab);
20530 obstack_init (&constant_pool);
20531 make_cleanup_obstack_free (&constant_pool);
20532 obstack_init (&symtab_obstack);
20533 make_cleanup_obstack_free (&symtab_obstack);
20534 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20536 obstack_init (&contents);
20537 make_cleanup_obstack_free (&contents);
20538 size_of_contents = 6 * sizeof (offset_type);
20539 total_len = size_of_contents;
20541 /* The version number. */
20542 val = MAYBE_SWAP (8);
20543 obstack_grow (&contents, &val, sizeof (val));
20545 /* The offset of the CU list from the start of the file. */
20546 val = MAYBE_SWAP (total_len);
20547 obstack_grow (&contents, &val, sizeof (val));
20548 total_len += obstack_object_size (&cu_list);
20550 /* The offset of the types CU list from the start of the file. */
20551 val = MAYBE_SWAP (total_len);
20552 obstack_grow (&contents, &val, sizeof (val));
20553 total_len += obstack_object_size (&types_cu_list);
20555 /* The offset of the address table from the start of the file. */
20556 val = MAYBE_SWAP (total_len);
20557 obstack_grow (&contents, &val, sizeof (val));
20558 total_len += obstack_object_size (&addr_obstack);
20560 /* The offset of the symbol table from the start of the file. */
20561 val = MAYBE_SWAP (total_len);
20562 obstack_grow (&contents, &val, sizeof (val));
20563 total_len += obstack_object_size (&symtab_obstack);
20565 /* The offset of the constant pool from the start of the file. */
20566 val = MAYBE_SWAP (total_len);
20567 obstack_grow (&contents, &val, sizeof (val));
20568 total_len += obstack_object_size (&constant_pool);
20570 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20572 write_obstack (out_file, &contents);
20573 write_obstack (out_file, &cu_list);
20574 write_obstack (out_file, &types_cu_list);
20575 write_obstack (out_file, &addr_obstack);
20576 write_obstack (out_file, &symtab_obstack);
20577 write_obstack (out_file, &constant_pool);
20581 /* We want to keep the file, so we set cleanup_filename to NULL
20582 here. See unlink_if_set. */
20583 cleanup_filename = NULL;
20585 do_cleanups (cleanup);
20588 /* Implementation of the `save gdb-index' command.
20590 Note that the file format used by this command is documented in the
20591 GDB manual. Any changes here must be documented there. */
20594 save_gdb_index_command (char *arg, int from_tty)
20596 struct objfile *objfile;
20599 error (_("usage: save gdb-index DIRECTORY"));
20601 ALL_OBJFILES (objfile)
20605 /* If the objfile does not correspond to an actual file, skip it. */
20606 if (stat (objfile->name, &st) < 0)
20609 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20610 if (dwarf2_per_objfile)
20612 volatile struct gdb_exception except;
20614 TRY_CATCH (except, RETURN_MASK_ERROR)
20616 write_psymtabs_to_index (objfile, arg);
20618 if (except.reason < 0)
20619 exception_fprintf (gdb_stderr, except,
20620 _("Error while writing index for `%s': "),
20628 int dwarf2_always_disassemble;
20631 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20632 struct cmd_list_element *c, const char *value)
20634 fprintf_filtered (file,
20635 _("Whether to always disassemble "
20636 "DWARF expressions is %s.\n"),
20641 show_check_physname (struct ui_file *file, int from_tty,
20642 struct cmd_list_element *c, const char *value)
20644 fprintf_filtered (file,
20645 _("Whether to check \"physname\" is %s.\n"),
20649 void _initialize_dwarf2_read (void);
20652 _initialize_dwarf2_read (void)
20654 struct cmd_list_element *c;
20656 dwarf2_objfile_data_key
20657 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20659 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20660 Set DWARF 2 specific variables.\n\
20661 Configure DWARF 2 variables such as the cache size"),
20662 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20663 0/*allow-unknown*/, &maintenance_set_cmdlist);
20665 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20666 Show DWARF 2 specific variables\n\
20667 Show DWARF 2 variables such as the cache size"),
20668 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20669 0/*allow-unknown*/, &maintenance_show_cmdlist);
20671 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20672 &dwarf2_max_cache_age, _("\
20673 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20674 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20675 A higher limit means that cached compilation units will be stored\n\
20676 in memory longer, and more total memory will be used. Zero disables\n\
20677 caching, which can slow down startup."),
20679 show_dwarf2_max_cache_age,
20680 &set_dwarf2_cmdlist,
20681 &show_dwarf2_cmdlist);
20683 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20684 &dwarf2_always_disassemble, _("\
20685 Set whether `info address' always disassembles DWARF expressions."), _("\
20686 Show whether `info address' always disassembles DWARF expressions."), _("\
20687 When enabled, DWARF expressions are always printed in an assembly-like\n\
20688 syntax. When disabled, expressions will be printed in a more\n\
20689 conversational style, when possible."),
20691 show_dwarf2_always_disassemble,
20692 &set_dwarf2_cmdlist,
20693 &show_dwarf2_cmdlist);
20695 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20696 Set debugging of the dwarf2 reader."), _("\
20697 Show debugging of the dwarf2 reader."), _("\
20698 When enabled, debugging messages are printed during dwarf2 reading\n\
20699 and symtab expansion."),
20702 &setdebuglist, &showdebuglist);
20704 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20705 Set debugging of the dwarf2 DIE reader."), _("\
20706 Show debugging of the dwarf2 DIE reader."), _("\
20707 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20708 The value is the maximum depth to print."),
20711 &setdebuglist, &showdebuglist);
20713 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20714 Set cross-checking of \"physname\" code against demangler."), _("\
20715 Show cross-checking of \"physname\" code against demangler."), _("\
20716 When enabled, GDB's internal \"physname\" code is checked against\n\
20718 NULL, show_check_physname,
20719 &setdebuglist, &showdebuglist);
20721 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20722 no_class, &use_deprecated_index_sections, _("\
20723 Set whether to use deprecated gdb_index sections."), _("\
20724 Show whether to use deprecated gdb_index sections."), _("\
20725 When enabled, deprecated .gdb_index sections are used anyway.\n\
20726 Normally they are ignored either because of a missing feature or\n\
20727 performance issue.\n\
20728 Warning: This option must be enabled before gdb reads the file."),
20731 &setlist, &showlist);
20733 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20735 Save a gdb-index file.\n\
20736 Usage: save gdb-index DIRECTORY"),
20738 set_cmd_completer (c, filename_completer);
20740 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20741 &dwarf2_locexpr_funcs);
20742 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20743 &dwarf2_loclist_funcs);
20745 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20746 &dwarf2_block_frame_base_locexpr_funcs);
20747 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20748 &dwarf2_block_frame_base_loclist_funcs);