Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / dwarf2 / read.c
blob851852404ebe56fa759295e28d9f32906d97c7a8
1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2023 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
10 support.
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. */
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
126 /* This is used to store the data that is always per objfile. */
127 static const registry<objfile>::key<dwarf2_per_objfile>
128 dwarf2_objfile_data_key;
130 /* These are used to store the dwarf2_per_bfd objects.
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
137 version. */
138 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
139 static const registry<objfile>::key<dwarf2_per_bfd>
140 dwarf2_per_bfd_objfile_data_key;
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
144 static int dwarf2_locexpr_index;
145 static int dwarf2_loclist_index;
146 static int dwarf2_locexpr_block_index;
147 static int dwarf2_loclist_block_index;
149 /* Size of .debug_loclists section header for 32-bit DWARF format. */
150 #define LOCLIST_HEADER_SIZE32 12
152 /* Size of .debug_loclists section header for 64-bit DWARF format. */
153 #define LOCLIST_HEADER_SIZE64 20
155 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
156 #define RNGLIST_HEADER_SIZE32 12
158 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
159 #define RNGLIST_HEADER_SIZE64 20
161 /* This is a view into the index that converts from bytes to an
162 offset_type, and allows indexing. Unaligned bytes are specifically
163 allowed here, and handled via unpacking. */
165 class offset_view
167 public:
168 offset_view () = default;
170 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
171 : m_bytes (bytes)
175 /* Extract the INDEXth offset_type from the array. */
176 offset_type operator[] (size_t index) const
178 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
179 return (offset_type) extract_unsigned_integer (bytes,
180 sizeof (offset_type),
181 BFD_ENDIAN_LITTLE);
184 /* Return the number of offset_types in this array. */
185 size_t size () const
187 return m_bytes.size () / sizeof (offset_type);
190 /* Return true if this view is empty. */
191 bool empty () const
193 return m_bytes.empty ();
196 private:
197 /* The underlying bytes. */
198 gdb::array_view<const gdb_byte> m_bytes;
201 /* A description of the mapped index. The file format is described in
202 a comment by the code that writes the index. */
203 struct mapped_index final : public mapped_index_base
205 /* Index data format version. */
206 int version = 0;
208 /* The address table data. */
209 gdb::array_view<const gdb_byte> address_table;
211 /* The symbol table, implemented as a hash table. */
212 offset_view symbol_table;
214 /* A pointer to the constant pool. */
215 gdb::array_view<const gdb_byte> constant_pool;
217 /* Return the index into the constant pool of the name of the IDXth
218 symbol in the symbol table. */
219 offset_type symbol_name_index (offset_type idx) const
221 return symbol_table[2 * idx];
224 /* Return the index into the constant pool of the CU vector of the
225 IDXth symbol in the symbol table. */
226 offset_type symbol_vec_index (offset_type idx) const
228 return symbol_table[2 * idx + 1];
231 bool symbol_name_slot_invalid (offset_type idx) const override
233 return (symbol_name_index (idx) == 0
234 && symbol_vec_index (idx) == 0);
237 /* Convenience method to get at the name of the symbol at IDX in the
238 symbol table. */
239 const char *symbol_name_at
240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
242 return (const char *) (this->constant_pool.data ()
243 + symbol_name_index (idx));
246 size_t symbol_name_count () const override
247 { return this->symbol_table.size () / 2; }
249 quick_symbol_functions_up make_quick_functions () const override;
251 bool version_check () const override
253 return version >= 8;
257 /* A description of the mapped .debug_names.
258 Uninitialized map has CU_COUNT 0. */
259 struct mapped_debug_names final : public mapped_index_base
261 bfd_endian dwarf5_byte_order;
262 bool dwarf5_is_dwarf64;
263 bool augmentation_is_gdb;
264 uint8_t offset_size;
265 uint32_t cu_count = 0;
266 uint32_t tu_count, bucket_count, name_count;
267 const gdb_byte *cu_table_reordered, *tu_table_reordered;
268 const uint32_t *bucket_table_reordered, *hash_table_reordered;
269 const gdb_byte *name_table_string_offs_reordered;
270 const gdb_byte *name_table_entry_offs_reordered;
271 const gdb_byte *entry_pool;
273 struct index_val
275 ULONGEST dwarf_tag;
276 struct attr
278 /* Attribute name DW_IDX_*. */
279 ULONGEST dw_idx;
281 /* Attribute form DW_FORM_*. */
282 ULONGEST form;
284 /* Value if FORM is DW_FORM_implicit_const. */
285 LONGEST implicit_const;
287 std::vector<attr> attr_vec;
290 std::unordered_map<ULONGEST, index_val> abbrev_map;
292 const char *namei_to_name
293 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
295 /* Implementation of the mapped_index_base virtual interface, for
296 the name_components cache. */
298 const char *symbol_name_at
299 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
300 { return namei_to_name (idx, per_objfile); }
302 size_t symbol_name_count () const override
303 { return this->name_count; }
305 quick_symbol_functions_up make_quick_functions () const override;
308 /* See dwarf2/read.h. */
310 dwarf2_per_objfile *
311 get_dwarf2_per_objfile (struct objfile *objfile)
313 return dwarf2_objfile_data_key.get (objfile);
316 /* Default names of the debugging sections. */
318 /* Note that if the debugging section has been compressed, it might
319 have a name like .zdebug_info. */
321 const struct dwarf2_debug_sections dwarf2_elf_names =
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_line", ".zdebug_line" },
326 { ".debug_loc", ".zdebug_loc" },
327 { ".debug_loclists", ".zdebug_loclists" },
328 { ".debug_macinfo", ".zdebug_macinfo" },
329 { ".debug_macro", ".zdebug_macro" },
330 { ".debug_str", ".zdebug_str" },
331 { ".debug_str_offsets", ".zdebug_str_offsets" },
332 { ".debug_line_str", ".zdebug_line_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_rnglists", ".zdebug_rnglists" },
335 { ".debug_types", ".zdebug_types" },
336 { ".debug_addr", ".zdebug_addr" },
337 { ".debug_frame", ".zdebug_frame" },
338 { ".eh_frame", NULL },
339 { ".gdb_index", ".zgdb_index" },
340 { ".debug_names", ".zdebug_names" },
341 { ".debug_aranges", ".zdebug_aranges" },
345 /* List of DWO/DWP sections. */
347 static const struct dwop_section_names
349 struct dwarf2_section_names abbrev_dwo;
350 struct dwarf2_section_names info_dwo;
351 struct dwarf2_section_names line_dwo;
352 struct dwarf2_section_names loc_dwo;
353 struct dwarf2_section_names loclists_dwo;
354 struct dwarf2_section_names macinfo_dwo;
355 struct dwarf2_section_names macro_dwo;
356 struct dwarf2_section_names rnglists_dwo;
357 struct dwarf2_section_names str_dwo;
358 struct dwarf2_section_names str_offsets_dwo;
359 struct dwarf2_section_names types_dwo;
360 struct dwarf2_section_names cu_index;
361 struct dwarf2_section_names tu_index;
363 dwop_section_names =
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
369 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
372 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
373 { ".debug_str.dwo", ".zdebug_str.dwo" },
374 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
375 { ".debug_types.dwo", ".zdebug_types.dwo" },
376 { ".debug_cu_index", ".zdebug_cu_index" },
377 { ".debug_tu_index", ".zdebug_tu_index" },
380 /* local data types */
382 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
383 begin with a header, which contains the following information. */
384 struct loclists_rnglists_header
386 /* A 4-byte or 12-byte length containing the length of the
387 set of entries for this compilation unit, not including the
388 length field itself. */
389 unsigned int length;
391 /* A 2-byte version identifier. */
392 short version;
394 /* A 1-byte unsigned integer containing the size in bytes of an address on
395 the target system. */
396 unsigned char addr_size;
398 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
399 on the target system. */
400 unsigned char segment_collector_size;
402 /* A 4-byte count of the number of offsets that follow the header. */
403 unsigned int offset_entry_count;
406 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
407 This includes type_unit_group and quick_file_names. */
409 struct stmt_list_hash
411 /* The DWO unit this table is from or NULL if there is none. */
412 struct dwo_unit *dwo_unit;
414 /* Offset in .debug_line or .debug_line.dwo. */
415 sect_offset line_sect_off;
418 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
419 an object of this type. This contains elements of type unit groups
420 that can be shared across objfiles. The non-shareable parts are in
421 type_unit_group_unshareable. */
423 struct type_unit_group
425 /* The data used to construct the hash key. */
426 struct stmt_list_hash hash {};
429 /* These sections are what may appear in a (real or virtual) DWO file. */
431 struct dwo_sections
433 struct dwarf2_section_info abbrev;
434 struct dwarf2_section_info line;
435 struct dwarf2_section_info loc;
436 struct dwarf2_section_info loclists;
437 struct dwarf2_section_info macinfo;
438 struct dwarf2_section_info macro;
439 struct dwarf2_section_info rnglists;
440 struct dwarf2_section_info str;
441 struct dwarf2_section_info str_offsets;
442 /* In the case of a virtual DWO file, these two are unused. */
443 struct dwarf2_section_info info;
444 std::vector<dwarf2_section_info> types;
447 /* CUs/TUs in DWP/DWO files. */
449 struct dwo_unit
451 /* Backlink to the containing struct dwo_file. */
452 struct dwo_file *dwo_file;
454 /* The "id" that distinguishes this CU/TU.
455 .debug_info calls this "dwo_id", .debug_types calls this "signature".
456 Since signatures came first, we stick with it for consistency. */
457 ULONGEST signature;
459 /* The section this CU/TU lives in, in the DWO file. */
460 struct dwarf2_section_info *section;
462 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
463 sect_offset sect_off;
464 unsigned int length;
466 /* For types, offset in the type's DIE of the type defined by this TU. */
467 cu_offset type_offset_in_tu;
470 /* include/dwarf2.h defines the DWP section codes.
471 It defines a max value but it doesn't define a min value, which we
472 use for error checking, so provide one. */
474 enum dwp_v2_section_ids
476 DW_SECT_MIN = 1
479 /* Data for one DWO file.
481 This includes virtual DWO files (a virtual DWO file is a DWO file as it
482 appears in a DWP file). DWP files don't really have DWO files per se -
483 comdat folding of types "loses" the DWO file they came from, and from
484 a high level view DWP files appear to contain a mass of random types.
485 However, to maintain consistency with the non-DWP case we pretend DWP
486 files contain virtual DWO files, and we assign each TU with one virtual
487 DWO file (generally based on the line and abbrev section offsets -
488 a heuristic that seems to work in practice). */
490 struct dwo_file
492 dwo_file () = default;
493 DISABLE_COPY_AND_ASSIGN (dwo_file);
495 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
496 For virtual DWO files the name is constructed from the section offsets
497 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
498 from related CU+TUs. */
499 const char *dwo_name = nullptr;
501 /* The DW_AT_comp_dir attribute. */
502 const char *comp_dir = nullptr;
504 /* The bfd, when the file is open. Otherwise this is NULL.
505 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
506 gdb_bfd_ref_ptr dbfd;
508 /* The sections that make up this DWO file.
509 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
510 sections (for lack of a better name). */
511 struct dwo_sections sections {};
513 /* The CUs in the file.
514 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
515 an extension to handle LLVM's Link Time Optimization output (where
516 multiple source files may be compiled into a single object/dwo pair). */
517 htab_up cus;
519 /* Table of TUs in the file.
520 Each element is a struct dwo_unit. */
521 htab_up tus;
524 /* These sections are what may appear in a DWP file. */
526 struct dwp_sections
528 /* These are used by all DWP versions (1, 2 and 5). */
529 struct dwarf2_section_info str;
530 struct dwarf2_section_info cu_index;
531 struct dwarf2_section_info tu_index;
533 /* These are only used by DWP version 2 and version 5 files.
534 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
535 sections are referenced by section number, and are not recorded here.
536 In DWP version 2 or 5 there is at most one copy of all these sections,
537 each section being (effectively) comprised of the concatenation of all of
538 the individual sections that exist in the version 1 format.
539 To keep the code simple we treat each of these concatenated pieces as a
540 section itself (a virtual section?). */
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info loclists;
546 struct dwarf2_section_info macinfo;
547 struct dwarf2_section_info macro;
548 struct dwarf2_section_info rnglists;
549 struct dwarf2_section_info str_offsets;
550 struct dwarf2_section_info types;
553 /* These sections are what may appear in a virtual DWO file in DWP version 1.
554 A virtual DWO file is a DWO file as it appears in a DWP file. */
556 struct virtual_v1_dwo_sections
558 struct dwarf2_section_info abbrev;
559 struct dwarf2_section_info line;
560 struct dwarf2_section_info loc;
561 struct dwarf2_section_info macinfo;
562 struct dwarf2_section_info macro;
563 struct dwarf2_section_info str_offsets;
564 /* Each DWP hash table entry records one CU or one TU.
565 That is recorded here, and copied to dwo_unit.section. */
566 struct dwarf2_section_info info_or_types;
569 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
570 In version 2, the sections of the DWO files are concatenated together
571 and stored in one section of that name. Thus each ELF section contains
572 several "virtual" sections. */
574 struct virtual_v2_or_v5_dwo_sections
576 bfd_size_type abbrev_offset;
577 bfd_size_type abbrev_size;
579 bfd_size_type line_offset;
580 bfd_size_type line_size;
582 bfd_size_type loc_offset;
583 bfd_size_type loc_size;
585 bfd_size_type loclists_offset;
586 bfd_size_type loclists_size;
588 bfd_size_type macinfo_offset;
589 bfd_size_type macinfo_size;
591 bfd_size_type macro_offset;
592 bfd_size_type macro_size;
594 bfd_size_type rnglists_offset;
595 bfd_size_type rnglists_size;
597 bfd_size_type str_offsets_offset;
598 bfd_size_type str_offsets_size;
600 /* Each DWP hash table entry records one CU or one TU.
601 That is recorded here, and copied to dwo_unit.section. */
602 bfd_size_type info_or_types_offset;
603 bfd_size_type info_or_types_size;
606 /* Contents of DWP hash tables. */
608 struct dwp_hash_table
610 uint32_t version, nr_columns;
611 uint32_t nr_units, nr_slots;
612 const gdb_byte *hash_table, *unit_table;
613 union
615 struct
617 const gdb_byte *indices;
618 } v1;
619 struct
621 /* This is indexed by column number and gives the id of the section
622 in that column. */
623 #define MAX_NR_V2_DWO_SECTIONS \
624 (1 /* .debug_info or .debug_types */ \
625 + 1 /* .debug_abbrev */ \
626 + 1 /* .debug_line */ \
627 + 1 /* .debug_loc */ \
628 + 1 /* .debug_str_offsets */ \
629 + 1 /* .debug_macro or .debug_macinfo */)
630 int section_ids[MAX_NR_V2_DWO_SECTIONS];
631 const gdb_byte *offsets;
632 const gdb_byte *sizes;
633 } v2;
634 struct
636 /* This is indexed by column number and gives the id of the section
637 in that column. */
638 #define MAX_NR_V5_DWO_SECTIONS \
639 (1 /* .debug_info */ \
640 + 1 /* .debug_abbrev */ \
641 + 1 /* .debug_line */ \
642 + 1 /* .debug_loclists */ \
643 + 1 /* .debug_str_offsets */ \
644 + 1 /* .debug_macro */ \
645 + 1 /* .debug_rnglists */)
646 int section_ids[MAX_NR_V5_DWO_SECTIONS];
647 const gdb_byte *offsets;
648 const gdb_byte *sizes;
649 } v5;
650 } section_pool;
653 /* Data for one DWP file. */
655 struct dwp_file
657 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
658 : name (name_),
659 dbfd (std::move (abfd))
663 /* Name of the file. */
664 const char *name;
666 /* File format version. */
667 int version = 0;
669 /* The bfd. */
670 gdb_bfd_ref_ptr dbfd;
672 /* Section info for this file. */
673 struct dwp_sections sections {};
675 /* Table of CUs in the file. */
676 const struct dwp_hash_table *cus = nullptr;
678 /* Table of TUs in the file. */
679 const struct dwp_hash_table *tus = nullptr;
681 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
682 htab_up loaded_cus;
683 htab_up loaded_tus;
685 /* Table to map ELF section numbers to their sections.
686 This is only needed for the DWP V1 file format. */
687 unsigned int num_sections = 0;
688 asection **elf_sections = nullptr;
691 /* Struct used to pass misc. parameters to read_die_and_children, et
692 al. which are used for both .debug_info and .debug_types dies.
693 All parameters here are unchanging for the life of the call. This
694 struct exists to abstract away the constant parameters of die reading. */
696 struct die_reader_specs
698 /* The bfd of die_section. */
699 bfd *abfd;
701 /* The CU of the DIE we are parsing. */
702 struct dwarf2_cu *cu;
704 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
705 struct dwo_file *dwo_file;
707 /* The section the die comes from.
708 This is either .debug_info or .debug_types, or the .dwo variants. */
709 struct dwarf2_section_info *die_section;
711 /* die_section->buffer. */
712 const gdb_byte *buffer;
714 /* The end of the buffer. */
715 const gdb_byte *buffer_end;
717 /* The abbreviation table to use when reading the DIEs. */
718 struct abbrev_table *abbrev_table;
721 /* A subclass of die_reader_specs that holds storage and has complex
722 constructor and destructor behavior. */
724 class cutu_reader : public die_reader_specs
726 public:
728 cutu_reader (dwarf2_per_cu_data *this_cu,
729 dwarf2_per_objfile *per_objfile,
730 struct abbrev_table *abbrev_table,
731 dwarf2_cu *existing_cu,
732 bool skip_partial,
733 abbrev_cache *cache = nullptr);
735 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
736 dwarf2_per_objfile *per_objfile,
737 struct dwarf2_cu *parent_cu = nullptr,
738 struct dwo_file *dwo_file = nullptr);
740 DISABLE_COPY_AND_ASSIGN (cutu_reader);
742 cutu_reader (cutu_reader &&) = default;
744 const gdb_byte *info_ptr = nullptr;
745 struct die_info *comp_unit_die = nullptr;
746 bool dummy_p = false;
748 /* Release the new CU, putting it on the chain. This cannot be done
749 for dummy CUs. */
750 void keep ();
752 /* Release the abbrev table, transferring ownership to the
753 caller. */
754 abbrev_table_up release_abbrev_table ()
756 return std::move (m_abbrev_table_holder);
759 private:
760 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
761 dwarf2_per_objfile *per_objfile,
762 dwarf2_cu *existing_cu);
764 struct dwarf2_per_cu_data *m_this_cu;
765 std::unique_ptr<dwarf2_cu> m_new_cu;
767 /* The ordinary abbreviation table. */
768 abbrev_table_up m_abbrev_table_holder;
770 /* The DWO abbreviation table. */
771 abbrev_table_up m_dwo_abbrev_table;
774 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
775 but this would require a corresponding change in unpack_field_as_long
776 and friends. */
777 static int bits_per_byte = 8;
779 struct variant_part_builder;
781 /* When reading a variant, we track a bit more information about the
782 field, and store it in an object of this type. */
784 struct variant_field
786 int first_field = -1;
787 int last_field = -1;
789 /* A variant can contain other variant parts. */
790 std::vector<variant_part_builder> variant_parts;
792 /* If we see a DW_TAG_variant, then this will be set if this is the
793 default branch. */
794 bool default_branch = false;
795 /* If we see a DW_AT_discr_value, then this will be the discriminant
796 value. */
797 ULONGEST discriminant_value = 0;
798 /* If we see a DW_AT_discr_list, then this is a pointer to the list
799 data. */
800 struct dwarf_block *discr_list_data = nullptr;
803 /* This represents a DW_TAG_variant_part. */
805 struct variant_part_builder
807 /* The offset of the discriminant field. */
808 sect_offset discriminant_offset {};
810 /* Variants that are direct children of this variant part. */
811 std::vector<variant_field> variants;
813 /* True if we're currently reading a variant. */
814 bool processing_variant = false;
817 struct nextfield
819 int accessibility = 0;
820 int virtuality = 0;
821 /* Variant parts need to find the discriminant, which is a DIE
822 reference. We track the section offset of each field to make
823 this link. */
824 sect_offset offset;
825 struct field field {};
828 struct fnfieldlist
830 const char *name = nullptr;
831 std::vector<struct fn_field> fnfields;
834 /* The routines that read and process dies for a C struct or C++ class
835 pass lists of data member fields and lists of member function fields
836 in an instance of a field_info structure, as defined below. */
837 struct field_info
839 /* List of data member and baseclasses fields. */
840 std::vector<struct nextfield> fields;
841 std::vector<struct nextfield> baseclasses;
843 /* Set if the accessibility of one of the fields is not public. */
844 bool non_public_fields = false;
846 /* Member function fieldlist array, contains name of possibly overloaded
847 member function, number of overloaded member functions and a pointer
848 to the head of the member function field chain. */
849 std::vector<struct fnfieldlist> fnfieldlists;
851 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
852 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
853 std::vector<struct decl_field> typedef_field_list;
855 /* Nested types defined by this class and the number of elements in this
856 list. */
857 std::vector<struct decl_field> nested_types_list;
859 /* If non-null, this is the variant part we are currently
860 reading. */
861 variant_part_builder *current_variant_part = nullptr;
862 /* This holds all the top-level variant parts attached to the type
863 we're reading. */
864 std::vector<variant_part_builder> variant_parts;
866 /* Return the total number of fields (including baseclasses). */
867 int nfields () const
869 return fields.size () + baseclasses.size ();
873 /* Loaded secondary compilation units are kept in memory until they
874 have not been referenced for the processing of this many
875 compilation units. Set this to zero to disable caching. Cache
876 sizes of up to at least twenty will improve startup time for
877 typical inter-CU-reference binaries, at an obvious memory cost. */
878 static int dwarf_max_cache_age = 5;
879 static void
880 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
881 struct cmd_list_element *c, const char *value)
883 gdb_printf (file, _("The upper bound on the age of cached "
884 "DWARF compilation units is %s.\n"),
885 value);
888 /* local function prototypes */
890 static void dwarf2_find_base_address (struct die_info *die,
891 struct dwarf2_cu *cu);
893 class cooked_index_storage;
894 static void build_type_psymtabs_reader (cutu_reader *reader,
895 cooked_index_storage *storage);
897 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
899 static void var_decode_location (struct attribute *attr,
900 struct symbol *sym,
901 struct dwarf2_cu *cu);
903 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
905 static const gdb_byte *read_attribute (const struct die_reader_specs *,
906 struct attribute *,
907 const struct attr_abbrev *,
908 const gdb_byte *);
910 static void read_attribute_reprocess (const struct die_reader_specs *reader,
911 struct attribute *attr, dwarf_tag tag);
913 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
915 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
916 dwarf2_section_info *, sect_offset);
918 static const char *read_indirect_string
919 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
920 const struct comp_unit_head *, unsigned int *);
922 static const char *read_indirect_string_at_offset
923 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
925 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
926 const gdb_byte *,
927 unsigned int *);
929 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
930 ULONGEST str_index);
932 static const char *read_stub_str_index (struct dwarf2_cu *cu,
933 ULONGEST str_index);
935 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
936 struct dwarf2_cu *);
938 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
939 struct dwarf2_cu *cu);
941 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
943 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944 struct dwarf2_cu *cu);
946 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
948 static struct die_info *die_specification (struct die_info *die,
949 struct dwarf2_cu **);
951 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
952 struct dwarf2_cu *cu,
953 const char *comp_dir);
955 static void dwarf_decode_lines (struct line_header *,
956 struct dwarf2_cu *,
957 CORE_ADDR, int decode_mapping);
959 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
960 const line_header &lh);
962 static struct symbol *new_symbol (struct die_info *, struct type *,
963 struct dwarf2_cu *, struct symbol * = NULL);
965 static void dwarf2_const_value (const struct attribute *, struct symbol *,
966 struct dwarf2_cu *);
968 static void dwarf2_const_value_attr (const struct attribute *attr,
969 struct type *type,
970 const char *name,
971 struct obstack *obstack,
972 struct dwarf2_cu *cu, LONGEST *value,
973 const gdb_byte **bytes,
974 struct dwarf2_locexpr_baton **baton);
976 static struct type *read_subrange_index_type (struct die_info *die,
977 struct dwarf2_cu *cu);
979 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
981 static int need_gnat_info (struct dwarf2_cu *);
983 static struct type *die_descriptive_type (struct die_info *,
984 struct dwarf2_cu *);
986 static void set_descriptive_type (struct type *, struct die_info *,
987 struct dwarf2_cu *);
989 static struct type *die_containing_type (struct die_info *,
990 struct dwarf2_cu *);
992 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
993 struct dwarf2_cu *);
995 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
997 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
999 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1001 static char *typename_concat (struct obstack *obs, const char *prefix,
1002 const char *suffix, int physname,
1003 struct dwarf2_cu *cu);
1005 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1007 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1009 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1011 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1013 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1015 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1017 /* Return the .debug_loclists section to use for cu. */
1018 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1020 /* Return the .debug_rnglists section to use for cu. */
1021 static struct dwarf2_section_info *cu_debug_rnglists_section
1022 (struct dwarf2_cu *cu, dwarf_tag tag);
1024 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1025 values. Keep the items ordered with increasing constraints compliance. */
1026 enum pc_bounds_kind
1028 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1029 PC_BOUNDS_NOT_PRESENT,
1031 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1032 were present but they do not form a valid range of PC addresses. */
1033 PC_BOUNDS_INVALID,
1035 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1036 PC_BOUNDS_RANGES,
1038 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1039 PC_BOUNDS_HIGH_LOW,
1042 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1043 CORE_ADDR *, CORE_ADDR *,
1044 struct dwarf2_cu *,
1045 addrmap *,
1046 void *);
1048 static void get_scope_pc_bounds (struct die_info *,
1049 CORE_ADDR *, CORE_ADDR *,
1050 struct dwarf2_cu *);
1052 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1053 CORE_ADDR, struct dwarf2_cu *);
1055 static void dwarf2_add_field (struct field_info *, struct die_info *,
1056 struct dwarf2_cu *);
1058 static void dwarf2_attach_fields_to_type (struct field_info *,
1059 struct type *, struct dwarf2_cu *);
1061 static void dwarf2_add_member_fn (struct field_info *,
1062 struct die_info *, struct type *,
1063 struct dwarf2_cu *);
1065 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1066 struct type *,
1067 struct dwarf2_cu *);
1069 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1071 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1073 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1075 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1077 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1079 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1081 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
1083 static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1086 static const char *namespace_name (struct die_info *die,
1087 int *is_anonymous, struct dwarf2_cu *);
1089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1092 bool * = nullptr);
1094 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1095 struct dwarf2_cu *);
1097 static struct die_info *read_die_and_siblings_1
1098 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1099 struct die_info *);
1101 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1102 const gdb_byte *info_ptr,
1103 const gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1106 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1107 struct die_info **, const gdb_byte *,
1108 int);
1110 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1111 struct die_info **, const gdb_byte *);
1113 static void process_die (struct die_info *, struct dwarf2_cu *);
1115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1116 struct objfile *);
1118 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1120 static const char *dwarf2_full_name (const char *name,
1121 struct die_info *die,
1122 struct dwarf2_cu *cu);
1124 static const char *dwarf2_physname (const char *name, struct die_info *die,
1125 struct dwarf2_cu *cu);
1127 static struct die_info *dwarf2_extension (struct die_info *die,
1128 struct dwarf2_cu **);
1130 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1132 static void dump_die_for_error (struct die_info *);
1134 static void dump_die_1 (struct ui_file *, int level, int max_level,
1135 struct die_info *);
1137 /*static*/ void dump_die (struct die_info *, int max_level);
1139 static void store_in_ref_table (struct die_info *,
1140 struct dwarf2_cu *);
1142 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1143 const struct attribute *,
1144 struct dwarf2_cu **);
1146 static struct die_info *follow_die_ref (struct die_info *,
1147 const struct attribute *,
1148 struct dwarf2_cu **);
1150 static struct die_info *follow_die_sig (struct die_info *,
1151 const struct attribute *,
1152 struct dwarf2_cu **);
1154 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1155 struct dwarf2_cu *);
1157 static struct type *get_DW_AT_signature_type (struct die_info *,
1158 const struct attribute *,
1159 struct dwarf2_cu *);
1161 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1162 dwarf2_per_objfile *per_objfile);
1164 static void read_signatured_type (signatured_type *sig_type,
1165 dwarf2_per_objfile *per_objfile);
1167 static int attr_to_dynamic_prop (const struct attribute *attr,
1168 struct die_info *die, struct dwarf2_cu *cu,
1169 struct dynamic_prop *prop, struct type *type);
1171 /* memory allocation interface */
1173 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1175 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1177 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1179 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1180 struct dwarf2_loclist_baton *baton,
1181 const struct attribute *attr);
1183 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1184 struct symbol *sym,
1185 struct dwarf2_cu *cu,
1186 int is_block);
1188 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1189 const gdb_byte *info_ptr,
1190 const struct abbrev_info *abbrev,
1191 bool do_skip_children = true);
1193 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1194 (sect_offset sect_off, unsigned int offset_in_dwz,
1195 dwarf2_per_bfd *per_bfd);
1197 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1198 struct die_info *comp_unit_die,
1199 enum language pretend_language);
1201 static struct type *set_die_type (struct die_info *, struct type *,
1202 struct dwarf2_cu *, bool = false);
1204 static void create_all_units (dwarf2_per_objfile *per_objfile);
1206 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1207 dwarf2_per_objfile *per_objfile,
1208 dwarf2_cu *existing_cu,
1209 bool skip_partial,
1210 enum language pretend_language);
1212 static void process_full_comp_unit (dwarf2_cu *cu,
1213 enum language pretend_language);
1215 static void process_full_type_unit (dwarf2_cu *cu,
1216 enum language pretend_language);
1218 static struct type *get_die_type_at_offset (sect_offset,
1219 dwarf2_per_cu_data *per_cu,
1220 dwarf2_per_objfile *per_objfile);
1222 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1224 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1225 dwarf2_per_objfile *per_objfile,
1226 enum language pretend_language);
1228 static void process_queue (dwarf2_per_objfile *per_objfile);
1230 /* Class, the destructor of which frees all allocated queue entries. This
1231 will only have work to do if an error was thrown while processing the
1232 dwarf. If no error was thrown then the queue entries should have all
1233 been processed, and freed, as we went along. */
1235 class dwarf2_queue_guard
1237 public:
1238 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1239 : m_per_objfile (per_objfile)
1241 gdb_assert (!m_per_objfile->queue.has_value ());
1243 m_per_objfile->queue.emplace ();
1246 /* Free any entries remaining on the queue. There should only be
1247 entries left if we hit an error while processing the dwarf. */
1248 ~dwarf2_queue_guard ()
1250 gdb_assert (m_per_objfile->queue.has_value ());
1252 m_per_objfile->queue.reset ();
1255 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1257 private:
1258 dwarf2_per_objfile *m_per_objfile;
1261 dwarf2_queue_item::~dwarf2_queue_item ()
1263 /* Anything still marked queued is likely to be in an
1264 inconsistent state, so discard it. */
1265 if (per_cu->queued)
1267 per_objfile->remove_cu (per_cu);
1268 per_cu->queued = 0;
1272 /* See dwarf2/read.h. */
1274 void
1275 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1277 if (data->is_debug_types)
1278 delete static_cast<signatured_type *> (data);
1279 else
1280 delete data;
1283 static file_and_directory &find_file_and_directory
1284 (struct die_info *die, struct dwarf2_cu *cu);
1286 static const char *compute_include_file_name
1287 (const struct line_header *lh,
1288 const file_entry &fe,
1289 const file_and_directory &cu_info,
1290 std::string &name_holder);
1292 static htab_up allocate_signatured_type_table ();
1294 static htab_up allocate_dwo_unit_table ();
1296 static struct dwo_unit *lookup_dwo_unit_in_dwp
1297 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1298 const char *comp_dir, ULONGEST signature, int is_debug_types);
1300 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1302 static struct dwo_unit *lookup_dwo_comp_unit
1303 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1304 ULONGEST signature);
1306 static struct dwo_unit *lookup_dwo_type_unit
1307 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1309 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1311 /* A unique pointer to a dwo_file. */
1313 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1315 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1317 static void check_producer (struct dwarf2_cu *cu);
1319 /* Various complaints about symbol reading that don't abort the process. */
1321 static void
1322 dwarf2_debug_line_missing_file_complaint (void)
1324 complaint (_(".debug_line section has line data without a file"));
1327 static void
1328 dwarf2_debug_line_missing_end_sequence_complaint (void)
1330 complaint (_(".debug_line section has line "
1331 "program sequence without an end"));
1334 static void
1335 dwarf2_complex_location_expr_complaint (void)
1337 complaint (_("location expression too complex"));
1340 static void
1341 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1342 int arg3)
1344 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1345 arg1, arg2, arg3);
1348 static void
1349 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1351 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1352 arg1, arg2);
1355 /* Hash function for line_header_hash. */
1357 static hashval_t
1358 line_header_hash (const struct line_header *ofs)
1360 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1363 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1365 static hashval_t
1366 line_header_hash_voidp (const void *item)
1368 const struct line_header *ofs = (const struct line_header *) item;
1370 return line_header_hash (ofs);
1373 /* Equality function for line_header_hash. */
1375 static int
1376 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1378 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1379 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1381 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1382 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1387 /* An iterator for all_units that is based on index. This
1388 approach makes it possible to iterate over all_units safely,
1389 when some caller in the loop may add new units. */
1391 class all_units_iterator
1393 public:
1395 all_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1396 : m_per_bfd (per_bfd),
1397 m_index (start ? 0 : per_bfd->all_units.size ())
1401 all_units_iterator &operator++ ()
1403 ++m_index;
1404 return *this;
1407 dwarf2_per_cu_data *operator* () const
1409 return m_per_bfd->get_cu (m_index);
1412 bool operator== (const all_units_iterator &other) const
1414 return m_index == other.m_index;
1418 bool operator!= (const all_units_iterator &other) const
1420 return m_index != other.m_index;
1423 private:
1425 dwarf2_per_bfd *m_per_bfd;
1426 size_t m_index;
1429 /* A range adapter for the all_units_iterator. */
1430 class all_units_range
1432 public:
1434 all_units_range (dwarf2_per_bfd *per_bfd)
1435 : m_per_bfd (per_bfd)
1439 all_units_iterator begin ()
1441 return all_units_iterator (m_per_bfd, true);
1444 all_units_iterator end ()
1446 return all_units_iterator (m_per_bfd, false);
1449 private:
1451 dwarf2_per_bfd *m_per_bfd;
1454 /* See declaration. */
1456 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1457 bool can_copy_)
1458 : obfd (obfd),
1459 can_copy (can_copy_)
1461 if (names == NULL)
1462 names = &dwarf2_elf_names;
1464 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1465 locate_sections (obfd, sec, *names);
1468 dwarf2_per_bfd::~dwarf2_per_bfd ()
1470 for (auto &per_cu : all_units)
1472 per_cu->imported_symtabs_free ();
1473 per_cu->free_cached_file_names ();
1476 /* Everything else should be on this->obstack. */
1479 /* See read.h. */
1481 void
1482 dwarf2_per_objfile::remove_all_cus ()
1484 gdb_assert (!queue.has_value ());
1486 m_dwarf2_cus.clear ();
1489 /* A helper class that calls free_cached_comp_units on
1490 destruction. */
1492 class free_cached_comp_units
1494 public:
1496 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1497 : m_per_objfile (per_objfile)
1501 ~free_cached_comp_units ()
1503 m_per_objfile->remove_all_cus ();
1506 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1508 private:
1510 dwarf2_per_objfile *m_per_objfile;
1513 /* See read.h. */
1515 bool
1516 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1518 if (per_cu->index < this->m_symtabs.size ())
1519 return this->m_symtabs[per_cu->index] != nullptr;
1520 return false;
1523 /* See read.h. */
1525 compunit_symtab *
1526 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1528 if (per_cu->index < this->m_symtabs.size ())
1529 return this->m_symtabs[per_cu->index];
1530 return nullptr;
1533 /* See read.h. */
1535 void
1536 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1537 compunit_symtab *symtab)
1539 if (per_cu->index >= this->m_symtabs.size ())
1540 this->m_symtabs.resize (per_cu->index + 1);
1541 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1542 this->m_symtabs[per_cu->index] = symtab;
1545 /* Try to locate the sections we need for DWARF 2 debugging
1546 information and return true if we have enough to do something.
1547 NAMES points to the dwarf2 section names, or is NULL if the standard
1548 ELF names are used. CAN_COPY is true for formats where symbol
1549 interposition is possible and so symbol values must follow copy
1550 relocation rules. */
1552 bool
1553 dwarf2_has_info (struct objfile *objfile,
1554 const struct dwarf2_debug_sections *names,
1555 bool can_copy)
1557 if (objfile->flags & OBJF_READNEVER)
1558 return false;
1560 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1562 if (per_objfile == NULL)
1564 dwarf2_per_bfd *per_bfd;
1566 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1567 BFD doesn't require relocations.
1569 We don't share with objfiles for which -readnow was requested,
1570 because it would complicate things when loading the same BFD with
1571 -readnow and then without -readnow. */
1572 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1573 && (objfile->flags & OBJF_READNOW) == 0)
1575 /* See if one has been created for this BFD yet. */
1576 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1578 if (per_bfd == nullptr)
1580 /* No, create it now. */
1581 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1582 can_copy);
1583 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1586 else
1588 /* No sharing possible, create one specifically for this objfile. */
1589 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1590 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1593 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1596 return (!per_objfile->per_bfd->info.is_virtual
1597 && per_objfile->per_bfd->info.s.section != NULL
1598 && !per_objfile->per_bfd->abbrev.is_virtual
1599 && per_objfile->per_bfd->abbrev.s.section != NULL);
1602 /* See declaration. */
1604 void
1605 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1606 const dwarf2_debug_sections &names)
1608 flagword aflag = bfd_section_flags (sectp);
1610 if ((aflag & SEC_HAS_CONTENTS) == 0)
1613 else if (elf_section_data (sectp)->this_hdr.sh_size
1614 > bfd_get_file_size (abfd))
1616 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1617 warning (_("Discarding section %s which has a section size (%s"
1618 ") larger than the file size [in module %s]"),
1619 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1620 bfd_get_filename (abfd));
1622 else if (names.info.matches (sectp->name))
1624 this->info.s.section = sectp;
1625 this->info.size = bfd_section_size (sectp);
1627 else if (names.abbrev.matches (sectp->name))
1629 this->abbrev.s.section = sectp;
1630 this->abbrev.size = bfd_section_size (sectp);
1632 else if (names.line.matches (sectp->name))
1634 this->line.s.section = sectp;
1635 this->line.size = bfd_section_size (sectp);
1637 else if (names.loc.matches (sectp->name))
1639 this->loc.s.section = sectp;
1640 this->loc.size = bfd_section_size (sectp);
1642 else if (names.loclists.matches (sectp->name))
1644 this->loclists.s.section = sectp;
1645 this->loclists.size = bfd_section_size (sectp);
1647 else if (names.macinfo.matches (sectp->name))
1649 this->macinfo.s.section = sectp;
1650 this->macinfo.size = bfd_section_size (sectp);
1652 else if (names.macro.matches (sectp->name))
1654 this->macro.s.section = sectp;
1655 this->macro.size = bfd_section_size (sectp);
1657 else if (names.str.matches (sectp->name))
1659 this->str.s.section = sectp;
1660 this->str.size = bfd_section_size (sectp);
1662 else if (names.str_offsets.matches (sectp->name))
1664 this->str_offsets.s.section = sectp;
1665 this->str_offsets.size = bfd_section_size (sectp);
1667 else if (names.line_str.matches (sectp->name))
1669 this->line_str.s.section = sectp;
1670 this->line_str.size = bfd_section_size (sectp);
1672 else if (names.addr.matches (sectp->name))
1674 this->addr.s.section = sectp;
1675 this->addr.size = bfd_section_size (sectp);
1677 else if (names.frame.matches (sectp->name))
1679 this->frame.s.section = sectp;
1680 this->frame.size = bfd_section_size (sectp);
1682 else if (names.eh_frame.matches (sectp->name))
1684 this->eh_frame.s.section = sectp;
1685 this->eh_frame.size = bfd_section_size (sectp);
1687 else if (names.ranges.matches (sectp->name))
1689 this->ranges.s.section = sectp;
1690 this->ranges.size = bfd_section_size (sectp);
1692 else if (names.rnglists.matches (sectp->name))
1694 this->rnglists.s.section = sectp;
1695 this->rnglists.size = bfd_section_size (sectp);
1697 else if (names.types.matches (sectp->name))
1699 struct dwarf2_section_info type_section;
1701 memset (&type_section, 0, sizeof (type_section));
1702 type_section.s.section = sectp;
1703 type_section.size = bfd_section_size (sectp);
1705 this->types.push_back (type_section);
1707 else if (names.gdb_index.matches (sectp->name))
1709 this->gdb_index.s.section = sectp;
1710 this->gdb_index.size = bfd_section_size (sectp);
1712 else if (names.debug_names.matches (sectp->name))
1714 this->debug_names.s.section = sectp;
1715 this->debug_names.size = bfd_section_size (sectp);
1717 else if (names.debug_aranges.matches (sectp->name))
1719 this->debug_aranges.s.section = sectp;
1720 this->debug_aranges.size = bfd_section_size (sectp);
1723 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1724 && bfd_section_vma (sectp) == 0)
1725 this->has_section_at_zero = true;
1728 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1729 SECTION_NAME. */
1731 void
1732 dwarf2_get_section_info (struct objfile *objfile,
1733 enum dwarf2_section_enum sect,
1734 asection **sectp, const gdb_byte **bufp,
1735 bfd_size_type *sizep)
1737 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1738 struct dwarf2_section_info *info;
1740 /* We may see an objfile without any DWARF, in which case we just
1741 return nothing. */
1742 if (per_objfile == NULL)
1744 *sectp = NULL;
1745 *bufp = NULL;
1746 *sizep = 0;
1747 return;
1749 switch (sect)
1751 case DWARF2_DEBUG_FRAME:
1752 info = &per_objfile->per_bfd->frame;
1753 break;
1754 case DWARF2_EH_FRAME:
1755 info = &per_objfile->per_bfd->eh_frame;
1756 break;
1757 default:
1758 gdb_assert_not_reached ("unexpected section");
1761 info->read (objfile);
1763 *sectp = info->get_bfd_section ();
1764 *bufp = info->buffer;
1765 *sizep = info->size;
1768 /* See dwarf2/read.h. */
1770 void
1771 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1773 info.read (objfile);
1774 abbrev.read (objfile);
1775 line.read (objfile);
1776 str.read (objfile);
1777 str_offsets.read (objfile);
1778 line_str.read (objfile);
1779 ranges.read (objfile);
1780 rnglists.read (objfile);
1781 addr.read (objfile);
1783 for (auto &section : types)
1784 section.read (objfile);
1788 /* DWARF quick_symbol_functions support. */
1790 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1791 unique line tables, so we maintain a separate table of all .debug_line
1792 derived entries to support the sharing.
1793 All the quick functions need is the list of file names. We discard the
1794 line_header when we're done and don't need to record it here. */
1795 struct quick_file_names
1797 /* The data used to construct the hash key. */
1798 struct stmt_list_hash hash;
1800 /* The number of entries in file_names, real_names. */
1801 unsigned int num_file_names;
1803 /* The CU directory, as given by DW_AT_comp_dir. May be
1804 nullptr. */
1805 const char *comp_dir;
1807 /* The file names from the line table, after being run through
1808 file_full_name. */
1809 const char **file_names;
1811 /* The file names from the line table after being run through
1812 gdb_realpath. These are computed lazily. */
1813 const char **real_names;
1816 struct dwarf2_base_index_functions : public quick_symbol_functions
1818 bool has_symbols (struct objfile *objfile) override;
1820 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1822 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1824 void forget_cached_source_info (struct objfile *objfile) override;
1826 enum language lookup_global_symbol_language (struct objfile *objfile,
1827 const char *name,
1828 domain_enum domain,
1829 bool *symbol_found_p) override
1831 *symbol_found_p = false;
1832 return language_unknown;
1835 void print_stats (struct objfile *objfile, bool print_bcache) override;
1837 void expand_all_symtabs (struct objfile *objfile) override;
1839 /* A helper function that finds the per-cu object from an "adjusted"
1840 PC -- a PC with the base text offset removed. */
1841 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
1842 CORE_ADDR adjusted_pc);
1844 struct compunit_symtab *find_pc_sect_compunit_symtab
1845 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1846 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
1847 override final;
1849 struct compunit_symtab *find_compunit_symtab_by_address
1850 (struct objfile *objfile, CORE_ADDR address) override
1852 return nullptr;
1855 void map_symbol_filenames (struct objfile *objfile,
1856 gdb::function_view<symbol_filename_ftype> fun,
1857 bool need_fullname) override;
1860 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1861 It's handy in this case to have an empty implementation of the
1862 quick symbol functions, to avoid special cases in the rest of the
1863 code. */
1865 struct readnow_functions : public dwarf2_base_index_functions
1867 void dump (struct objfile *objfile) override
1871 void expand_matching_symbols
1872 (struct objfile *,
1873 const lookup_name_info &lookup_name,
1874 domain_enum domain,
1875 int global,
1876 symbol_compare_ftype *ordered_compare) override
1880 bool expand_symtabs_matching
1881 (struct objfile *objfile,
1882 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1883 const lookup_name_info *lookup_name,
1884 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1885 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1886 block_search_flags search_flags,
1887 domain_enum domain,
1888 enum search_domain kind) override
1890 return true;
1894 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1896 void dump (struct objfile *objfile) override;
1898 void expand_matching_symbols
1899 (struct objfile *,
1900 const lookup_name_info &lookup_name,
1901 domain_enum domain,
1902 int global,
1903 symbol_compare_ftype *ordered_compare) override;
1905 bool expand_symtabs_matching
1906 (struct objfile *objfile,
1907 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1908 const lookup_name_info *lookup_name,
1909 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1910 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1911 block_search_flags search_flags,
1912 domain_enum domain,
1913 enum search_domain kind) override;
1916 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1918 void dump (struct objfile *objfile) override;
1920 void expand_matching_symbols
1921 (struct objfile *,
1922 const lookup_name_info &lookup_name,
1923 domain_enum domain,
1924 int global,
1925 symbol_compare_ftype *ordered_compare) override;
1927 bool expand_symtabs_matching
1928 (struct objfile *objfile,
1929 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1930 const lookup_name_info *lookup_name,
1931 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1932 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1933 block_search_flags search_flags,
1934 domain_enum domain,
1935 enum search_domain kind) override;
1938 quick_symbol_functions_up
1939 mapped_index::make_quick_functions () const
1941 return quick_symbol_functions_up (new dwarf2_gdb_index);
1944 quick_symbol_functions_up
1945 mapped_debug_names::make_quick_functions () const
1947 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1950 /* Utility hash function for a stmt_list_hash. */
1952 static hashval_t
1953 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1955 hashval_t v = 0;
1957 if (stmt_list_hash->dwo_unit != NULL)
1958 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1959 v += to_underlying (stmt_list_hash->line_sect_off);
1960 return v;
1963 /* Utility equality function for a stmt_list_hash. */
1965 static int
1966 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1967 const struct stmt_list_hash *rhs)
1969 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1970 return 0;
1971 if (lhs->dwo_unit != NULL
1972 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1973 return 0;
1975 return lhs->line_sect_off == rhs->line_sect_off;
1978 /* Hash function for a quick_file_names. */
1980 static hashval_t
1981 hash_file_name_entry (const void *e)
1983 const struct quick_file_names *file_data
1984 = (const struct quick_file_names *) e;
1986 return hash_stmt_list_entry (&file_data->hash);
1989 /* Equality function for a quick_file_names. */
1991 static int
1992 eq_file_name_entry (const void *a, const void *b)
1994 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1995 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1997 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2000 /* Create a quick_file_names hash table. */
2002 static htab_up
2003 create_quick_file_names_table (unsigned int nr_initial_entries)
2005 return htab_up (htab_create_alloc (nr_initial_entries,
2006 hash_file_name_entry, eq_file_name_entry,
2007 nullptr, xcalloc, xfree));
2010 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2011 function is unrelated to symtabs, symtab would have to be created afterwards.
2012 You should call age_cached_comp_units after processing the CU. */
2014 static dwarf2_cu *
2015 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2016 bool skip_partial)
2018 if (per_cu->is_debug_types)
2019 load_full_type_unit (per_cu, per_objfile);
2020 else
2021 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2022 skip_partial, language_minimal);
2024 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2025 if (cu == nullptr)
2026 return nullptr; /* Dummy CU. */
2028 dwarf2_find_base_address (cu->dies, cu);
2030 return cu;
2033 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2035 static void
2036 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2037 dwarf2_per_objfile *per_objfile, bool skip_partial)
2040 /* The destructor of dwarf2_queue_guard frees any entries left on
2041 the queue. After this point we're guaranteed to leave this function
2042 with the dwarf queue empty. */
2043 dwarf2_queue_guard q_guard (per_objfile);
2045 if (!per_objfile->symtab_set_p (per_cu))
2047 queue_comp_unit (per_cu, per_objfile, language_minimal);
2048 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2050 /* If we just loaded a CU from a DWO, and we're working with an index
2051 that may badly handle TUs, load all the TUs in that DWO as well.
2052 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2053 if (!per_cu->is_debug_types
2054 && cu != NULL
2055 && cu->dwo_unit != NULL
2056 && per_objfile->per_bfd->index_table != NULL
2057 && !per_objfile->per_bfd->index_table->version_check ()
2058 /* DWP files aren't supported yet. */
2059 && get_dwp_file (per_objfile) == NULL)
2060 queue_and_load_all_dwo_tus (cu);
2063 process_queue (per_objfile);
2066 /* Age the cache, releasing compilation units that have not
2067 been used recently. */
2068 per_objfile->age_comp_units ();
2071 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2072 the per-objfile for which this symtab is instantiated.
2074 Returns the resulting symbol table. */
2076 static struct compunit_symtab *
2077 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2078 dwarf2_per_objfile *per_objfile,
2079 bool skip_partial)
2081 if (!per_objfile->symtab_set_p (per_cu))
2083 free_cached_comp_units freer (per_objfile);
2084 scoped_restore decrementer = increment_reading_symtab ();
2085 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2086 process_cu_includes (per_objfile);
2089 return per_objfile->get_symtab (per_cu);
2092 /* See read.h. */
2094 dwarf2_per_cu_data_up
2095 dwarf2_per_bfd::allocate_per_cu ()
2097 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2098 result->per_bfd = this;
2099 result->index = all_units.size ();
2100 return result;
2103 /* See read.h. */
2105 signatured_type_up
2106 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2108 signatured_type_up result (new signatured_type (signature));
2109 result->per_bfd = this;
2110 result->index = all_units.size ();
2111 result->is_debug_types = true;
2112 tu_stats.nr_tus++;
2113 return result;
2116 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2117 obstack, and constructed with the specified field values. */
2119 static dwarf2_per_cu_data_up
2120 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2121 struct dwarf2_section_info *section,
2122 int is_dwz,
2123 sect_offset sect_off, ULONGEST length)
2125 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2126 the_cu->sect_off = sect_off;
2127 the_cu->set_length (length);
2128 the_cu->section = section;
2129 the_cu->is_dwz = is_dwz;
2130 return the_cu;
2133 /* A helper for create_cus_from_index that handles a given list of
2134 CUs. */
2136 static void
2137 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2138 const gdb_byte *cu_list, offset_type n_elements,
2139 struct dwarf2_section_info *section,
2140 int is_dwz)
2142 for (offset_type i = 0; i < n_elements; i += 2)
2144 gdb_static_assert (sizeof (ULONGEST) >= 8);
2146 sect_offset sect_off
2147 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2148 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2149 cu_list += 2 * 8;
2151 dwarf2_per_cu_data_up per_cu
2152 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2153 length);
2154 per_bfd->all_units.push_back (std::move (per_cu));
2158 /* Read the CU list from the mapped index, and use it to create all
2159 the CU objects for PER_BFD. */
2161 static void
2162 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2163 const gdb_byte *cu_list, offset_type cu_list_elements,
2164 const gdb_byte *dwz_list, offset_type dwz_elements)
2166 gdb_assert (per_bfd->all_units.empty ());
2167 per_bfd->all_units.reserve ((cu_list_elements + dwz_elements) / 2);
2169 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2170 &per_bfd->info, 0);
2172 if (dwz_elements == 0)
2173 return;
2175 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2176 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2177 &dwz->info, 1);
2180 /* Create the signatured type hash table from the index. */
2182 static void
2183 create_signatured_type_table_from_index
2184 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2185 const gdb_byte *bytes, offset_type elements)
2187 htab_up sig_types_hash = allocate_signatured_type_table ();
2189 for (offset_type i = 0; i < elements; i += 3)
2191 signatured_type_up sig_type;
2192 ULONGEST signature;
2193 void **slot;
2194 cu_offset type_offset_in_tu;
2196 gdb_static_assert (sizeof (ULONGEST) >= 8);
2197 sect_offset sect_off
2198 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2199 type_offset_in_tu
2200 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2201 BFD_ENDIAN_LITTLE);
2202 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2203 bytes += 3 * 8;
2205 sig_type = per_bfd->allocate_signatured_type (signature);
2206 sig_type->type_offset_in_tu = type_offset_in_tu;
2207 sig_type->section = section;
2208 sig_type->sect_off = sect_off;
2210 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2211 *slot = sig_type.get ();
2213 per_bfd->all_units.emplace_back (sig_type.release ());
2216 per_bfd->signatured_types = std::move (sig_types_hash);
2219 /* Create the signatured type hash table from .debug_names. */
2221 static void
2222 create_signatured_type_table_from_debug_names
2223 (dwarf2_per_objfile *per_objfile,
2224 const mapped_debug_names &map,
2225 struct dwarf2_section_info *section,
2226 struct dwarf2_section_info *abbrev_section)
2228 struct objfile *objfile = per_objfile->objfile;
2230 section->read (objfile);
2231 abbrev_section->read (objfile);
2233 htab_up sig_types_hash = allocate_signatured_type_table ();
2235 for (uint32_t i = 0; i < map.tu_count; ++i)
2237 signatured_type_up sig_type;
2238 void **slot;
2240 sect_offset sect_off
2241 = (sect_offset) (extract_unsigned_integer
2242 (map.tu_table_reordered + i * map.offset_size,
2243 map.offset_size,
2244 map.dwarf5_byte_order));
2246 comp_unit_head cu_header;
2247 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2248 abbrev_section,
2249 section->buffer + to_underlying (sect_off),
2250 rcuh_kind::TYPE);
2252 sig_type = per_objfile->per_bfd->allocate_signatured_type
2253 (cu_header.signature);
2254 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2255 sig_type->section = section;
2256 sig_type->sect_off = sect_off;
2258 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2259 *slot = sig_type.get ();
2261 per_objfile->per_bfd->all_units.emplace_back (sig_type.release ());
2264 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2267 /* Read the address map data from the mapped index, and use it to
2268 populate the index_addrmap. */
2270 static void
2271 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2272 struct mapped_index *index)
2274 struct objfile *objfile = per_objfile->objfile;
2275 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2276 struct gdbarch *gdbarch = objfile->arch ();
2277 const gdb_byte *iter, *end;
2278 CORE_ADDR baseaddr;
2280 addrmap_mutable mutable_map;
2282 iter = index->address_table.data ();
2283 end = iter + index->address_table.size ();
2285 baseaddr = objfile->text_section_offset ();
2287 while (iter < end)
2289 ULONGEST hi, lo, cu_index;
2290 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2291 iter += 8;
2292 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2293 iter += 8;
2294 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2295 iter += 4;
2297 if (lo > hi)
2299 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2300 hex_string (lo), hex_string (hi));
2301 continue;
2304 if (cu_index >= per_bfd->all_units.size ())
2306 complaint (_(".gdb_index address table has invalid CU number %u"),
2307 (unsigned) cu_index);
2308 continue;
2311 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2312 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2313 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
2316 per_bfd->index_addrmap
2317 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
2320 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2321 to populate given addrmap. Returns true on success, false on
2322 failure. */
2324 static bool
2325 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2326 struct dwarf2_section_info *section,
2327 addrmap *mutable_map)
2329 struct objfile *objfile = per_objfile->objfile;
2330 bfd *abfd = objfile->obfd.get ();
2331 struct gdbarch *gdbarch = objfile->arch ();
2332 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2333 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2335 std::unordered_map<sect_offset,
2336 dwarf2_per_cu_data *,
2337 gdb::hash_enum<sect_offset>>
2338 debug_info_offset_to_per_cu;
2339 for (const auto &per_cu : per_bfd->all_units)
2341 /* A TU will not need aranges, and skipping them here is an easy
2342 way of ignoring .debug_types -- and possibly seeing a
2343 duplicate section offset -- entirely. The same applies to
2344 units coming from a dwz file. */
2345 if (per_cu->is_debug_types || per_cu->is_dwz)
2346 continue;
2348 const auto insertpair
2349 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2350 per_cu.get ());
2352 /* Assume no duplicate offsets in all_units. */
2353 gdb_assert (insertpair.second);
2356 std::set<sect_offset> debug_info_offset_seen;
2358 section->read (objfile);
2360 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2362 const gdb_byte *addr = section->buffer;
2364 while (addr < section->buffer + section->size)
2366 const gdb_byte *const entry_addr = addr;
2367 unsigned int bytes_read;
2369 const LONGEST entry_length = read_initial_length (abfd, addr,
2370 &bytes_read);
2371 addr += bytes_read;
2373 const gdb_byte *const entry_end = addr + entry_length;
2374 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2375 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2376 if (addr + entry_length > section->buffer + section->size)
2378 warning (_("Section .debug_aranges in %s entry at offset %s "
2379 "length %s exceeds section length %s, "
2380 "ignoring .debug_aranges."),
2381 objfile_name (objfile),
2382 plongest (entry_addr - section->buffer),
2383 plongest (bytes_read + entry_length),
2384 pulongest (section->size));
2385 return false;
2388 /* The version number. */
2389 const uint16_t version = read_2_bytes (abfd, addr);
2390 addr += 2;
2391 if (version != 2)
2393 warning (_("Section .debug_aranges in %s entry at offset %s "
2394 "has unsupported version %d, ignoring .debug_aranges."),
2395 objfile_name (objfile),
2396 plongest (entry_addr - section->buffer), version);
2397 return false;
2400 const uint64_t debug_info_offset
2401 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2402 addr += offset_size;
2403 const auto per_cu_it
2404 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2405 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2407 warning (_("Section .debug_aranges in %s entry at offset %s "
2408 "debug_info_offset %s does not exists, "
2409 "ignoring .debug_aranges."),
2410 objfile_name (objfile),
2411 plongest (entry_addr - section->buffer),
2412 pulongest (debug_info_offset));
2413 return false;
2415 const auto insertpair
2416 = debug_info_offset_seen.insert (sect_offset (debug_info_offset));
2417 if (!insertpair.second)
2419 warning (_("Section .debug_aranges in %s has duplicate "
2420 "debug_info_offset %s, ignoring .debug_aranges."),
2421 objfile_name (objfile),
2422 sect_offset_str (sect_offset (debug_info_offset)));
2423 return false;
2425 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2427 const uint8_t address_size = *addr++;
2428 if (address_size < 1 || address_size > 8)
2430 warning (_("Section .debug_aranges in %s entry at offset %s "
2431 "address_size %u is invalid, ignoring .debug_aranges."),
2432 objfile_name (objfile),
2433 plongest (entry_addr - section->buffer), address_size);
2434 return false;
2437 const uint8_t segment_selector_size = *addr++;
2438 if (segment_selector_size != 0)
2440 warning (_("Section .debug_aranges in %s entry at offset %s "
2441 "segment_selector_size %u is not supported, "
2442 "ignoring .debug_aranges."),
2443 objfile_name (objfile),
2444 plongest (entry_addr - section->buffer),
2445 segment_selector_size);
2446 return false;
2449 /* Must pad to an alignment boundary that is twice the address
2450 size. It is undocumented by the DWARF standard but GCC does
2451 use it. However, not every compiler does this. We can see
2452 whether it has happened by looking at the total length of the
2453 contents of the aranges for this CU -- it if isn't a multiple
2454 of twice the address size, then we skip any leftover
2455 bytes. */
2456 addr += (entry_end - addr) % (2 * address_size);
2458 while (addr < entry_end)
2460 if (addr + 2 * address_size > entry_end)
2462 warning (_("Section .debug_aranges in %s entry at offset %s "
2463 "address list is not properly terminated, "
2464 "ignoring .debug_aranges."),
2465 objfile_name (objfile),
2466 plongest (entry_addr - section->buffer));
2467 return false;
2469 ULONGEST start = extract_unsigned_integer (addr, address_size,
2470 dwarf5_byte_order);
2471 addr += address_size;
2472 ULONGEST length = extract_unsigned_integer (addr, address_size,
2473 dwarf5_byte_order);
2474 addr += address_size;
2475 if (start == 0 && length == 0)
2477 /* This can happen on some targets with --gc-sections.
2478 This pair of values is also used to mark the end of
2479 the entries for a given CU, but we ignore it and
2480 instead handle termination using the check at the top
2481 of the loop. */
2482 continue;
2484 if (start == 0 && !per_bfd->has_section_at_zero)
2486 /* Symbol was eliminated due to a COMDAT group. */
2487 continue;
2489 ULONGEST end = start + length;
2490 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2491 - baseaddr);
2492 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2493 - baseaddr);
2494 mutable_map->set_empty (start, end - 1, per_cu);
2497 per_cu->addresses_seen = true;
2500 return true;
2503 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2504 populate the index_addrmap. */
2506 static void
2507 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2508 struct dwarf2_section_info *section)
2510 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2512 addrmap_mutable mutable_map;
2514 if (read_addrmap_from_aranges (per_objfile, section, &mutable_map))
2515 per_bfd->index_addrmap
2516 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack,
2517 &mutable_map);
2520 /* A helper function that reads the .gdb_index from BUFFER and fills
2521 in MAP. FILENAME is the name of the file containing the data;
2522 it is used for error reporting. DEPRECATED_OK is true if it is
2523 ok to use deprecated sections.
2525 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2526 out parameters that are filled in with information about the CU and
2527 TU lists in the section.
2529 Returns true if all went well, false otherwise. */
2531 static bool
2532 read_gdb_index_from_buffer (const char *filename,
2533 bool deprecated_ok,
2534 gdb::array_view<const gdb_byte> buffer,
2535 struct mapped_index *map,
2536 const gdb_byte **cu_list,
2537 offset_type *cu_list_elements,
2538 const gdb_byte **types_list,
2539 offset_type *types_list_elements)
2541 const gdb_byte *addr = &buffer[0];
2542 offset_view metadata (buffer);
2544 /* Version check. */
2545 offset_type version = metadata[0];
2546 /* Versions earlier than 3 emitted every copy of a psymbol. This
2547 causes the index to behave very poorly for certain requests. Version 3
2548 contained incomplete addrmap. So, it seems better to just ignore such
2549 indices. */
2550 if (version < 4)
2552 static int warning_printed = 0;
2553 if (!warning_printed)
2555 warning (_("Skipping obsolete .gdb_index section in %s."),
2556 filename);
2557 warning_printed = 1;
2559 return 0;
2561 /* Index version 4 uses a different hash function than index version
2562 5 and later.
2564 Versions earlier than 6 did not emit psymbols for inlined
2565 functions. Using these files will cause GDB not to be able to
2566 set breakpoints on inlined functions by name, so we ignore these
2567 indices unless the user has done
2568 "set use-deprecated-index-sections on". */
2569 if (version < 6 && !deprecated_ok)
2571 static int warning_printed = 0;
2572 if (!warning_printed)
2574 warning (_("\
2575 Skipping deprecated .gdb_index section in %s.\n\
2576 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2577 to use the section anyway."),
2578 filename);
2579 warning_printed = 1;
2581 return 0;
2583 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2584 of the TU (for symbols coming from TUs),
2585 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2586 Plus gold-generated indices can have duplicate entries for global symbols,
2587 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2588 These are just performance bugs, and we can't distinguish gdb-generated
2589 indices from gold-generated ones, so issue no warning here. */
2591 /* Indexes with higher version than the one supported by GDB may be no
2592 longer backward compatible. */
2593 if (version > 8)
2594 return 0;
2596 map->version = version;
2598 int i = 1;
2599 *cu_list = addr + metadata[i];
2600 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2601 ++i;
2603 *types_list = addr + metadata[i];
2604 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2605 ++i;
2607 const gdb_byte *address_table = addr + metadata[i];
2608 const gdb_byte *address_table_end = addr + metadata[i + 1];
2609 map->address_table
2610 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2611 ++i;
2613 const gdb_byte *symbol_table = addr + metadata[i];
2614 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2615 map->symbol_table
2616 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2617 symbol_table_end));
2619 ++i;
2620 map->constant_pool = buffer.slice (metadata[i]);
2622 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2624 /* An empty constant pool implies that all symbol table entries are
2625 empty. Make map->symbol_table.empty () == true. */
2626 map->symbol_table
2627 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2628 symbol_table));
2631 return 1;
2634 static void finalize_all_units (dwarf2_per_bfd *per_bfd);
2636 /* Callback types for dwarf2_read_gdb_index. */
2638 typedef gdb::function_view
2639 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2640 get_gdb_index_contents_ftype;
2641 typedef gdb::function_view
2642 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2643 get_gdb_index_contents_dwz_ftype;
2645 /* Read .gdb_index. If everything went ok, initialize the "quick"
2646 elements of all the CUs and return 1. Otherwise, return 0. */
2648 static int
2649 dwarf2_read_gdb_index
2650 (dwarf2_per_objfile *per_objfile,
2651 get_gdb_index_contents_ftype get_gdb_index_contents,
2652 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2654 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2655 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2656 struct dwz_file *dwz;
2657 struct objfile *objfile = per_objfile->objfile;
2658 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2660 gdb::array_view<const gdb_byte> main_index_contents
2661 = get_gdb_index_contents (objfile, per_bfd);
2663 if (main_index_contents.empty ())
2664 return 0;
2666 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2667 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2668 use_deprecated_index_sections,
2669 main_index_contents, map.get (), &cu_list,
2670 &cu_list_elements, &types_list,
2671 &types_list_elements))
2672 return 0;
2674 /* Don't use the index if it's empty. */
2675 if (map->symbol_table.empty ())
2676 return 0;
2678 /* If there is a .dwz file, read it so we can get its CU list as
2679 well. */
2680 dwz = dwarf2_get_dwz_file (per_bfd);
2681 if (dwz != NULL)
2683 struct mapped_index dwz_map;
2684 const gdb_byte *dwz_types_ignore;
2685 offset_type dwz_types_elements_ignore;
2687 gdb::array_view<const gdb_byte> dwz_index_content
2688 = get_gdb_index_contents_dwz (objfile, dwz);
2690 if (dwz_index_content.empty ())
2691 return 0;
2693 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2694 1, dwz_index_content, &dwz_map,
2695 &dwz_list, &dwz_list_elements,
2696 &dwz_types_ignore,
2697 &dwz_types_elements_ignore))
2699 warning (_("could not read '.gdb_index' section from %s; skipping"),
2700 bfd_get_filename (dwz->dwz_bfd.get ()));
2701 return 0;
2705 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2706 dwz_list_elements);
2708 if (types_list_elements)
2710 /* We can only handle a single .debug_types when we have an
2711 index. */
2712 if (per_bfd->types.size () > 1)
2714 per_bfd->all_units.clear ();
2715 return 0;
2718 dwarf2_section_info *section
2719 = (per_bfd->types.size () == 1
2720 ? &per_bfd->types[0]
2721 : &per_bfd->info);
2723 create_signatured_type_table_from_index (per_bfd, section, types_list,
2724 types_list_elements);
2727 finalize_all_units (per_bfd);
2729 create_addrmap_from_index (per_objfile, map.get ());
2731 per_bfd->index_table = std::move (map);
2732 per_bfd->quick_file_names_table =
2733 create_quick_file_names_table (per_bfd->all_units.size ());
2735 return 1;
2738 /* die_reader_func for dw2_get_file_names. */
2740 static void
2741 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2742 struct die_info *comp_unit_die)
2744 struct dwarf2_cu *cu = reader->cu;
2745 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2747 struct dwarf2_per_cu_data *lh_cu;
2748 struct attribute *attr;
2749 void **slot;
2750 struct quick_file_names *qfn;
2752 gdb_assert (! this_cu->is_debug_types);
2754 this_cu->files_read = true;
2755 /* Our callers never want to match partial units -- instead they
2756 will match the enclosing full CU. */
2757 if (comp_unit_die->tag == DW_TAG_partial_unit)
2758 return;
2760 lh_cu = this_cu;
2761 slot = NULL;
2763 line_header_up lh;
2764 sect_offset line_offset {};
2766 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2768 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2769 if (attr != nullptr && attr->form_is_unsigned ())
2771 struct quick_file_names find_entry;
2773 line_offset = (sect_offset) attr->as_unsigned ();
2775 /* We may have already read in this line header (TU line header sharing).
2776 If we have we're done. */
2777 find_entry.hash.dwo_unit = cu->dwo_unit;
2778 find_entry.hash.line_sect_off = line_offset;
2779 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2780 &find_entry, INSERT);
2781 if (*slot != NULL)
2783 lh_cu->file_names = (struct quick_file_names *) *slot;
2784 return;
2787 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
2790 int offset = 0;
2791 if (!fnd.is_unknown ())
2792 ++offset;
2793 else if (lh == nullptr)
2794 return;
2796 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2797 qfn->hash.dwo_unit = cu->dwo_unit;
2798 qfn->hash.line_sect_off = line_offset;
2799 /* There may not be a DW_AT_stmt_list. */
2800 if (slot != nullptr)
2801 *slot = qfn;
2803 std::vector<const char *> include_names;
2804 if (lh != nullptr)
2806 for (const auto &entry : lh->file_names ())
2808 std::string name_holder;
2809 const char *include_name =
2810 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2811 if (include_name != nullptr)
2813 include_name = per_objfile->objfile->intern (include_name);
2814 include_names.push_back (include_name);
2819 qfn->num_file_names = offset + include_names.size ();
2820 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2821 qfn->file_names =
2822 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2823 qfn->num_file_names);
2824 if (offset != 0)
2825 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
2827 if (!include_names.empty ())
2828 memcpy (&qfn->file_names[offset], include_names.data (),
2829 include_names.size () * sizeof (const char *));
2831 qfn->real_names = NULL;
2833 lh_cu->file_names = qfn;
2836 /* A helper for the "quick" functions which attempts to read the line
2837 table for THIS_CU. */
2839 static struct quick_file_names *
2840 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2841 dwarf2_per_objfile *per_objfile)
2843 /* This should never be called for TUs. */
2844 gdb_assert (! this_cu->is_debug_types);
2846 if (this_cu->files_read)
2847 return this_cu->file_names;
2849 cutu_reader reader (this_cu, per_objfile);
2850 if (!reader.dummy_p)
2851 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2853 return this_cu->file_names;
2856 /* A helper for the "quick" functions which computes and caches the
2857 real path for a given file name from the line table. */
2859 static const char *
2860 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2861 struct quick_file_names *qfn, int index)
2863 if (qfn->real_names == NULL)
2864 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2865 qfn->num_file_names, const char *);
2867 if (qfn->real_names[index] == NULL)
2869 const char *dirname = nullptr;
2871 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2872 dirname = qfn->comp_dir;
2874 gdb::unique_xmalloc_ptr<char> fullname;
2875 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2877 qfn->real_names[index] = fullname.release ();
2880 return qfn->real_names[index];
2883 struct symtab *
2884 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2886 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2887 dwarf2_per_cu_data *dwarf_cu
2888 = per_objfile->per_bfd->all_units.back ().get ();
2889 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2891 if (cust == NULL)
2892 return NULL;
2894 return cust->primary_filetab ();
2897 /* See read.h. */
2899 void
2900 dwarf2_per_cu_data::free_cached_file_names ()
2902 if (fnd != nullptr)
2903 fnd->forget_fullname ();
2905 if (per_bfd == nullptr)
2906 return;
2908 struct quick_file_names *file_data = file_names;
2909 if (file_data != nullptr && file_data->real_names != nullptr)
2911 for (int i = 0; i < file_data->num_file_names; ++i)
2913 xfree ((void *) file_data->real_names[i]);
2914 file_data->real_names[i] = nullptr;
2919 void
2920 dwarf2_base_index_functions::forget_cached_source_info
2921 (struct objfile *objfile)
2923 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2925 for (auto &per_cu : per_objfile->per_bfd->all_units)
2926 per_cu->free_cached_file_names ();
2929 /* Struct used to manage iterating over all CUs looking for a symbol. */
2931 struct dw2_symtab_iterator
2933 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2934 dwarf2_per_objfile *per_objfile;
2935 /* If set, only look for symbols that match that block. Valid values are
2936 GLOBAL_BLOCK and STATIC_BLOCK. */
2937 gdb::optional<block_enum> block_index;
2938 /* The kind of symbol we're looking for. */
2939 domain_enum domain;
2940 /* The list of CUs from the index entry of the symbol,
2941 or NULL if not found. */
2942 offset_view vec;
2943 /* The next element in VEC to look at. */
2944 int next;
2945 /* The number of elements in VEC, or zero if there is no match. */
2946 int length;
2947 /* Have we seen a global version of the symbol?
2948 If so we can ignore all further global instances.
2949 This is to work around gold/15646, inefficient gold-generated
2950 indices. */
2951 int global_seen;
2954 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2956 static void
2957 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2958 dwarf2_per_objfile *per_objfile,
2959 gdb::optional<block_enum> block_index,
2960 domain_enum domain, offset_type namei,
2961 mapped_index &index)
2963 iter->per_objfile = per_objfile;
2964 iter->block_index = block_index;
2965 iter->domain = domain;
2966 iter->next = 0;
2967 iter->global_seen = 0;
2968 iter->vec = {};
2969 iter->length = 0;
2971 gdb_assert (!index.symbol_name_slot_invalid (namei));
2972 offset_type vec_idx = index.symbol_vec_index (namei);
2974 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2975 iter->length = iter->vec[0];
2978 /* Return the next matching CU or NULL if there are no more. */
2980 static struct dwarf2_per_cu_data *
2981 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2982 mapped_index &index)
2984 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2986 for ( ; iter->next < iter->length; ++iter->next)
2988 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2989 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2990 gdb_index_symbol_kind symbol_kind =
2991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2992 /* Only check the symbol attributes if they're present.
2993 Indices prior to version 7 don't record them,
2994 and indices >= 7 may elide them for certain symbols
2995 (gold does this). */
2996 int attrs_valid = (index.version >= 7
2997 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2999 /* Don't crash on bad data. */
3000 if (cu_index >= per_objfile->per_bfd->all_units.size ())
3002 complaint (_(".gdb_index entry has bad CU index"
3003 " [in module %s]"), objfile_name (per_objfile->objfile));
3004 continue;
3007 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3009 /* Skip if already read in. */
3010 if (per_objfile->symtab_set_p (per_cu))
3011 continue;
3013 /* Check static vs global. */
3014 if (attrs_valid)
3016 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3018 if (iter->block_index.has_value ())
3020 bool want_static = *iter->block_index == STATIC_BLOCK;
3022 if (is_static != want_static)
3023 continue;
3026 /* Work around gold/15646. */
3027 if (!is_static
3028 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3030 if (iter->global_seen)
3031 continue;
3033 iter->global_seen = 1;
3037 /* Only check the symbol's kind if it has one. */
3038 if (attrs_valid)
3040 switch (iter->domain)
3042 case VAR_DOMAIN:
3043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3044 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3045 /* Some types are also in VAR_DOMAIN. */
3046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3047 continue;
3048 break;
3049 case STRUCT_DOMAIN:
3050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3051 continue;
3052 break;
3053 case LABEL_DOMAIN:
3054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3055 continue;
3056 break;
3057 case MODULE_DOMAIN:
3058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3059 continue;
3060 break;
3061 default:
3062 break;
3066 ++iter->next;
3067 return per_cu;
3070 return NULL;
3073 void
3074 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3075 bool print_bcache)
3077 if (print_bcache)
3078 return;
3080 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3081 int total = per_objfile->per_bfd->all_units.size ();
3082 int count = 0;
3084 for (int i = 0; i < total; ++i)
3086 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3088 if (!per_objfile->symtab_set_p (per_cu))
3089 ++count;
3091 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3092 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3095 /* This dumps minimal information about the index.
3096 It is called via "mt print objfiles".
3097 One use is to verify .gdb_index has been loaded by the
3098 gdb.dwarf2/gdb-index.exp testcase. */
3100 void
3101 dwarf2_gdb_index::dump (struct objfile *objfile)
3103 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3105 mapped_index *index = (gdb::checked_static_cast<mapped_index *>
3106 (per_objfile->per_bfd->index_table.get ()));
3107 gdb_printf (".gdb_index: version %d\n", index->version);
3108 gdb_printf ("\n");
3111 void
3112 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3114 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3115 int total_units = per_objfile->per_bfd->all_units.size ();
3117 for (int i = 0; i < total_units; ++i)
3119 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3121 /* We don't want to directly expand a partial CU, because if we
3122 read it with the wrong language, then assertion failures can
3123 be triggered later on. See PR symtab/23010. So, tell
3124 dw2_instantiate_symtab to skip partial CUs -- any important
3125 partial CU will be read via DW_TAG_imported_unit anyway. */
3126 dw2_instantiate_symtab (per_cu, per_objfile, true);
3130 static bool
3131 dw2_expand_symtabs_matching_symbol
3132 (mapped_index_base &index,
3133 const lookup_name_info &lookup_name_in,
3134 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3135 gdb::function_view<bool (offset_type)> match_callback,
3136 dwarf2_per_objfile *per_objfile);
3138 static bool
3139 dw2_expand_symtabs_matching_one
3140 (dwarf2_per_cu_data *per_cu,
3141 dwarf2_per_objfile *per_objfile,
3142 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3143 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3145 void
3146 dwarf2_gdb_index::expand_matching_symbols
3147 (struct objfile *objfile,
3148 const lookup_name_info &name, domain_enum domain,
3149 int global,
3150 symbol_compare_ftype *ordered_compare)
3152 /* Used for Ada. */
3153 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3155 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3157 mapped_index &index
3158 = *(gdb::checked_static_cast<mapped_index *>
3159 (per_objfile->per_bfd->index_table.get ()));
3161 const char *match_name = name.ada ().lookup_name ().c_str ();
3162 auto matcher = [&] (const char *symname)
3164 if (ordered_compare == nullptr)
3165 return true;
3166 return ordered_compare (symname, match_name) == 0;
3169 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3170 [&] (offset_type namei)
3172 struct dw2_symtab_iterator iter;
3173 struct dwarf2_per_cu_data *per_cu;
3175 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3176 index);
3177 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3178 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3179 nullptr);
3180 return true;
3181 }, per_objfile);
3184 /* Starting from a search name, return the string that finds the upper
3185 bound of all strings that start with SEARCH_NAME in a sorted name
3186 list. Returns the empty string to indicate that the upper bound is
3187 the end of the list. */
3189 static std::string
3190 make_sort_after_prefix_name (const char *search_name)
3192 /* When looking to complete "func", we find the upper bound of all
3193 symbols that start with "func" by looking for where we'd insert
3194 the closest string that would follow "func" in lexicographical
3195 order. Usually, that's "func"-with-last-character-incremented,
3196 i.e. "fund". Mind non-ASCII characters, though. Usually those
3197 will be UTF-8 multi-byte sequences, but we can't be certain.
3198 Especially mind the 0xff character, which is a valid character in
3199 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3200 rule out compilers allowing it in identifiers. Note that
3201 conveniently, strcmp/strcasecmp are specified to compare
3202 characters interpreted as unsigned char. So what we do is treat
3203 the whole string as a base 256 number composed of a sequence of
3204 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3205 to 0, and carries 1 to the following more-significant position.
3206 If the very first character in SEARCH_NAME ends up incremented
3207 and carries/overflows, then the upper bound is the end of the
3208 list. The string after the empty string is also the empty
3209 string.
3211 Some examples of this operation:
3213 SEARCH_NAME => "+1" RESULT
3215 "abc" => "abd"
3216 "ab\xff" => "ac"
3217 "\xff" "a" "\xff" => "\xff" "b"
3218 "\xff" => ""
3219 "\xff\xff" => ""
3220 "" => ""
3222 Then, with these symbols for example:
3224 func
3225 func1
3226 fund
3228 completing "func" looks for symbols between "func" and
3229 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3230 which finds "func" and "func1", but not "fund".
3232 And with:
3234 funcÿ (Latin1 'ÿ' [0xff])
3235 funcÿ1
3236 fund
3238 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3239 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3241 And with:
3243 ÿÿ (Latin1 'ÿ' [0xff])
3244 ÿÿ1
3246 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3247 the end of the list.
3249 std::string after = search_name;
3250 while (!after.empty () && (unsigned char) after.back () == 0xff)
3251 after.pop_back ();
3252 if (!after.empty ())
3253 after.back () = (unsigned char) after.back () + 1;
3254 return after;
3257 /* See declaration. */
3259 std::pair<std::vector<name_component>::const_iterator,
3260 std::vector<name_component>::const_iterator>
3261 mapped_index_base::find_name_components_bounds
3262 (const lookup_name_info &lookup_name_without_params, language lang,
3263 dwarf2_per_objfile *per_objfile) const
3265 auto *name_cmp
3266 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3268 const char *lang_name
3269 = lookup_name_without_params.language_lookup_name (lang);
3271 /* Comparison function object for lower_bound that matches against a
3272 given symbol name. */
3273 auto lookup_compare_lower = [&] (const name_component &elem,
3274 const char *name)
3276 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3277 const char *elem_name = elem_qualified + elem.name_offset;
3278 return name_cmp (elem_name, name) < 0;
3281 /* Comparison function object for upper_bound that matches against a
3282 given symbol name. */
3283 auto lookup_compare_upper = [&] (const char *name,
3284 const name_component &elem)
3286 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3287 const char *elem_name = elem_qualified + elem.name_offset;
3288 return name_cmp (name, elem_name) < 0;
3291 auto begin = this->name_components.begin ();
3292 auto end = this->name_components.end ();
3294 /* Find the lower bound. */
3295 auto lower = [&] ()
3297 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3298 return begin;
3299 else
3300 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3301 } ();
3303 /* Find the upper bound. */
3304 auto upper = [&] ()
3306 if (lookup_name_without_params.completion_mode ())
3308 /* In completion mode, we want UPPER to point past all
3309 symbols names that have the same prefix. I.e., with
3310 these symbols, and completing "func":
3312 function << lower bound
3313 function1
3314 other_function << upper bound
3316 We find the upper bound by looking for the insertion
3317 point of "func"-with-last-character-incremented,
3318 i.e. "fund". */
3319 std::string after = make_sort_after_prefix_name (lang_name);
3320 if (after.empty ())
3321 return end;
3322 return std::lower_bound (lower, end, after.c_str (),
3323 lookup_compare_lower);
3325 else
3326 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3327 } ();
3329 return {lower, upper};
3332 /* See declaration. */
3334 void
3335 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3337 if (!this->name_components.empty ())
3338 return;
3340 this->name_components_casing = case_sensitivity;
3341 auto *name_cmp
3342 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3344 /* The code below only knows how to break apart components of C++
3345 symbol names (and other languages that use '::' as
3346 namespace/module separator) and Ada symbol names. */
3347 auto count = this->symbol_name_count ();
3348 for (offset_type idx = 0; idx < count; idx++)
3350 if (this->symbol_name_slot_invalid (idx))
3351 continue;
3353 const char *name = this->symbol_name_at (idx, per_objfile);
3355 /* Add each name component to the name component table. */
3356 unsigned int previous_len = 0;
3358 if (strstr (name, "::") != nullptr)
3360 for (unsigned int current_len = cp_find_first_component (name);
3361 name[current_len] != '\0';
3362 current_len += cp_find_first_component (name + current_len))
3364 gdb_assert (name[current_len] == ':');
3365 this->name_components.push_back ({previous_len, idx});
3366 /* Skip the '::'. */
3367 current_len += 2;
3368 previous_len = current_len;
3371 else
3373 /* Handle the Ada encoded (aka mangled) form here. */
3374 for (const char *iter = strstr (name, "__");
3375 iter != nullptr;
3376 iter = strstr (iter, "__"))
3378 this->name_components.push_back ({previous_len, idx});
3379 iter += 2;
3380 previous_len = iter - name;
3384 this->name_components.push_back ({previous_len, idx});
3387 /* Sort name_components elements by name. */
3388 auto name_comp_compare = [&] (const name_component &left,
3389 const name_component &right)
3391 const char *left_qualified
3392 = this->symbol_name_at (left.idx, per_objfile);
3393 const char *right_qualified
3394 = this->symbol_name_at (right.idx, per_objfile);
3396 const char *left_name = left_qualified + left.name_offset;
3397 const char *right_name = right_qualified + right.name_offset;
3399 return name_cmp (left_name, right_name) < 0;
3402 std::sort (this->name_components.begin (),
3403 this->name_components.end (),
3404 name_comp_compare);
3407 /* Helper for dw2_expand_symtabs_matching that works with a
3408 mapped_index_base instead of the containing objfile. This is split
3409 to a separate function in order to be able to unit test the
3410 name_components matching using a mock mapped_index_base. For each
3411 symbol name that matches, calls MATCH_CALLBACK, passing it the
3412 symbol's index in the mapped_index_base symbol table. */
3414 static bool
3415 dw2_expand_symtabs_matching_symbol
3416 (mapped_index_base &index,
3417 const lookup_name_info &lookup_name_in,
3418 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3419 gdb::function_view<bool (offset_type)> match_callback,
3420 dwarf2_per_objfile *per_objfile)
3422 lookup_name_info lookup_name_without_params
3423 = lookup_name_in.make_ignore_params ();
3425 /* Build the symbol name component sorted vector, if we haven't
3426 yet. */
3427 index.build_name_components (per_objfile);
3429 /* The same symbol may appear more than once in the range though.
3430 E.g., if we're looking for symbols that complete "w", and we have
3431 a symbol named "w1::w2", we'll find the two name components for
3432 that same symbol in the range. To be sure we only call the
3433 callback once per symbol, we first collect the symbol name
3434 indexes that matched in a temporary vector and ignore
3435 duplicates. */
3436 std::vector<offset_type> matches;
3438 struct name_and_matcher
3440 symbol_name_matcher_ftype *matcher;
3441 const char *name;
3443 bool operator== (const name_and_matcher &other) const
3445 return matcher == other.matcher && strcmp (name, other.name) == 0;
3449 /* A vector holding all the different symbol name matchers, for all
3450 languages. */
3451 std::vector<name_and_matcher> matchers;
3453 for (int i = 0; i < nr_languages; i++)
3455 enum language lang_e = (enum language) i;
3457 const language_defn *lang = language_def (lang_e);
3458 symbol_name_matcher_ftype *name_matcher
3459 = lang->get_symbol_name_matcher (lookup_name_without_params);
3461 name_and_matcher key {
3462 name_matcher,
3463 lookup_name_without_params.language_lookup_name (lang_e)
3466 /* Don't insert the same comparison routine more than once.
3467 Note that we do this linear walk. This is not a problem in
3468 practice because the number of supported languages is
3469 low. */
3470 if (std::find (matchers.begin (), matchers.end (), key)
3471 != matchers.end ())
3472 continue;
3473 matchers.push_back (std::move (key));
3475 auto bounds
3476 = index.find_name_components_bounds (lookup_name_without_params,
3477 lang_e, per_objfile);
3479 /* Now for each symbol name in range, check to see if we have a name
3480 match, and if so, call the MATCH_CALLBACK callback. */
3482 for (; bounds.first != bounds.second; ++bounds.first)
3484 const char *qualified
3485 = index.symbol_name_at (bounds.first->idx, per_objfile);
3487 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3488 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3489 continue;
3491 matches.push_back (bounds.first->idx);
3495 std::sort (matches.begin (), matches.end ());
3497 /* Finally call the callback, once per match. */
3498 ULONGEST prev = -1;
3499 bool result = true;
3500 for (offset_type idx : matches)
3502 if (prev != idx)
3504 if (!match_callback (idx))
3506 result = false;
3507 break;
3509 prev = idx;
3513 /* Above we use a type wider than idx's for 'prev', since 0 and
3514 (offset_type)-1 are both possible values. */
3515 static_assert (sizeof (prev) > sizeof (offset_type), "");
3517 return result;
3520 #if GDB_SELF_TEST
3522 namespace selftests { namespace dw2_expand_symtabs_matching {
3524 /* A mock .gdb_index/.debug_names-like name index table, enough to
3525 exercise dw2_expand_symtabs_matching_symbol, which works with the
3526 mapped_index_base interface. Builds an index from the symbol list
3527 passed as parameter to the constructor. */
3528 class mock_mapped_index : public mapped_index_base
3530 public:
3531 mock_mapped_index (gdb::array_view<const char *> symbols)
3532 : m_symbol_table (symbols)
3535 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3537 /* Return the number of names in the symbol table. */
3538 size_t symbol_name_count () const override
3540 return m_symbol_table.size ();
3543 /* Get the name of the symbol at IDX in the symbol table. */
3544 const char *symbol_name_at
3545 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3547 return m_symbol_table[idx];
3550 quick_symbol_functions_up make_quick_functions () const override
3552 return nullptr;
3555 private:
3556 gdb::array_view<const char *> m_symbol_table;
3559 /* Convenience function that converts a NULL pointer to a "<null>"
3560 string, to pass to print routines. */
3562 static const char *
3563 string_or_null (const char *str)
3565 return str != NULL ? str : "<null>";
3568 /* Check if a lookup_name_info built from
3569 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3570 index. EXPECTED_LIST is the list of expected matches, in expected
3571 matching order. If no match expected, then an empty list is
3572 specified. Returns true on success. On failure prints a warning
3573 indicating the file:line that failed, and returns false. */
3575 static bool
3576 check_match (const char *file, int line,
3577 mock_mapped_index &mock_index,
3578 const char *name, symbol_name_match_type match_type,
3579 bool completion_mode,
3580 std::initializer_list<const char *> expected_list,
3581 dwarf2_per_objfile *per_objfile)
3583 lookup_name_info lookup_name (name, match_type, completion_mode);
3585 bool matched = true;
3587 auto mismatch = [&] (const char *expected_str,
3588 const char *got)
3590 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3591 "expected=\"%s\", got=\"%s\"\n"),
3592 file, line,
3593 (match_type == symbol_name_match_type::FULL
3594 ? "FULL" : "WILD"),
3595 name, string_or_null (expected_str), string_or_null (got));
3596 matched = false;
3599 auto expected_it = expected_list.begin ();
3600 auto expected_end = expected_list.end ();
3602 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3603 nullptr,
3604 [&] (offset_type idx)
3606 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3607 const char *expected_str
3608 = expected_it == expected_end ? NULL : *expected_it++;
3610 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3611 mismatch (expected_str, matched_name);
3612 return true;
3613 }, per_objfile);
3615 const char *expected_str
3616 = expected_it == expected_end ? NULL : *expected_it++;
3617 if (expected_str != NULL)
3618 mismatch (expected_str, NULL);
3620 return matched;
3623 /* The symbols added to the mock mapped_index for testing (in
3624 canonical form). */
3625 static const char *test_symbols[] = {
3626 "function",
3627 "std::bar",
3628 "std::zfunction",
3629 "std::zfunction2",
3630 "w1::w2",
3631 "ns::foo<char*>",
3632 "ns::foo<int>",
3633 "ns::foo<long>",
3634 "ns2::tmpl<int>::foo2",
3635 "(anonymous namespace)::A::B::C",
3637 /* These are used to check that the increment-last-char in the
3638 matching algorithm for completion doesn't match "t1_fund" when
3639 completing "t1_func". */
3640 "t1_func",
3641 "t1_func1",
3642 "t1_fund",
3643 "t1_fund1",
3645 /* A UTF-8 name with multi-byte sequences to make sure that
3646 cp-name-parser understands this as a single identifier ("função"
3647 is "function" in PT). */
3648 u8"u8função",
3650 /* Test a symbol name that ends with a 0xff character, which is a
3651 valid character in non-UTF-8 source character sets (e.g. Latin1
3652 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3653 We test this because the completion algorithm finds the upper
3654 bound of symbols by looking for the insertion point of
3655 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3656 to 0xff should wraparound and carry to the previous character.
3657 See comments in make_sort_after_prefix_name. */
3658 "yfunc\377",
3660 /* Some more symbols with \377 (0xff). See above. */
3661 "\377",
3662 "\377\377123",
3664 /* A name with all sorts of complications. Starts with "z" to make
3665 it easier for the completion tests below. */
3666 #define Z_SYM_NAME \
3667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3668 "::tuple<(anonymous namespace)::ui*, " \
3669 "std::default_delete<(anonymous namespace)::ui>, void>"
3671 Z_SYM_NAME
3674 /* Returns true if the mapped_index_base::find_name_component_bounds
3675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3676 in completion mode. */
3678 static bool
3679 check_find_bounds_finds (mapped_index_base &index,
3680 const char *search_name,
3681 gdb::array_view<const char *> expected_syms,
3682 dwarf2_per_objfile *per_objfile)
3684 lookup_name_info lookup_name (search_name,
3685 symbol_name_match_type::FULL, true);
3687 auto bounds = index.find_name_components_bounds (lookup_name,
3688 language_cplus,
3689 per_objfile);
3691 size_t distance = std::distance (bounds.first, bounds.second);
3692 if (distance != expected_syms.size ())
3693 return false;
3695 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3697 auto nc_elem = bounds.first + exp_elem;
3698 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3699 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3700 return false;
3703 return true;
3706 /* Test the lower-level mapped_index::find_name_component_bounds
3707 method. */
3709 static void
3710 test_mapped_index_find_name_component_bounds ()
3712 mock_mapped_index mock_index (test_symbols);
3714 mock_index.build_name_components (NULL /* per_objfile */);
3716 /* Test the lower-level mapped_index::find_name_component_bounds
3717 method in completion mode. */
3719 static const char *expected_syms[] = {
3720 "t1_func",
3721 "t1_func1",
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index, "t1_func", expected_syms,
3726 NULL /* per_objfile */));
3729 /* Check that the increment-last-char in the name matching algorithm
3730 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3731 make_sort_after_prefix_name. */
3733 static const char *expected_syms1[] = {
3734 "\377",
3735 "\377\377123",
3737 SELF_CHECK (check_find_bounds_finds
3738 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3740 static const char *expected_syms2[] = {
3741 "\377\377123",
3743 SELF_CHECK (check_find_bounds_finds
3744 (mock_index, "\377\377", expected_syms2,
3745 NULL /* per_objfile */));
3749 /* Test dw2_expand_symtabs_matching_symbol. */
3751 static void
3752 test_dw2_expand_symtabs_matching_symbol ()
3754 mock_mapped_index mock_index (test_symbols);
3756 /* We let all tests run until the end even if some fails, for debug
3757 convenience. */
3758 bool any_mismatch = false;
3760 /* Create the expected symbols list (an initializer_list). Needed
3761 because lists have commas, and we need to pass them to CHECK,
3762 which is a macro. */
3763 #define EXPECT(...) { __VA_ARGS__ }
3765 /* Wrapper for check_match that passes down the current
3766 __FILE__/__LINE__. */
3767 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3768 any_mismatch |= !check_match (__FILE__, __LINE__, \
3769 mock_index, \
3770 NAME, MATCH_TYPE, COMPLETION_MODE, \
3771 EXPECTED_LIST, NULL)
3773 /* Identity checks. */
3774 for (const char *sym : test_symbols)
3776 /* Should be able to match all existing symbols. */
3777 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3778 EXPECT (sym));
3780 /* Should be able to match all existing symbols with
3781 parameters. */
3782 std::string with_params = std::string (sym) + "(int)";
3783 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3784 EXPECT (sym));
3786 /* Should be able to match all existing symbols with
3787 parameters and qualifiers. */
3788 with_params = std::string (sym) + " ( int ) const";
3789 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3790 EXPECT (sym));
3792 /* This should really find sym, but cp-name-parser.y doesn't
3793 know about lvalue/rvalue qualifiers yet. */
3794 with_params = std::string (sym) + " ( int ) &&";
3795 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3796 {});
3799 /* Check that the name matching algorithm for completion doesn't get
3800 confused with Latin1 'ÿ' / 0xff. See
3801 make_sort_after_prefix_name. */
3803 static const char str[] = "\377";
3804 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3805 EXPECT ("\377", "\377\377123"));
3808 /* Check that the increment-last-char in the matching algorithm for
3809 completion doesn't match "t1_fund" when completing "t1_func". */
3811 static const char str[] = "t1_func";
3812 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3813 EXPECT ("t1_func", "t1_func1"));
3816 /* Check that completion mode works at each prefix of the expected
3817 symbol name. */
3819 static const char str[] = "function(int)";
3820 size_t len = strlen (str);
3821 std::string lookup;
3823 for (size_t i = 1; i < len; i++)
3825 lookup.assign (str, i);
3826 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3827 EXPECT ("function"));
3831 /* While "w" is a prefix of both components, the match function
3832 should still only be called once. */
3834 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3835 EXPECT ("w1::w2"));
3836 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3837 EXPECT ("w1::w2"));
3840 /* Same, with a "complicated" symbol. */
3842 static const char str[] = Z_SYM_NAME;
3843 size_t len = strlen (str);
3844 std::string lookup;
3846 for (size_t i = 1; i < len; i++)
3848 lookup.assign (str, i);
3849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3850 EXPECT (Z_SYM_NAME));
3854 /* In FULL mode, an incomplete symbol doesn't match. */
3856 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3857 {});
3860 /* A complete symbol with parameters matches any overload, since the
3861 index has no overload info. */
3863 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3864 EXPECT ("std::zfunction", "std::zfunction2"));
3865 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3866 EXPECT ("std::zfunction", "std::zfunction2"));
3867 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3868 EXPECT ("std::zfunction", "std::zfunction2"));
3871 /* Check that whitespace is ignored appropriately. A symbol with a
3872 template argument list. */
3874 static const char expected[] = "ns::foo<int>";
3875 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3876 EXPECT (expected));
3877 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3878 EXPECT (expected));
3881 /* Check that whitespace is ignored appropriately. A symbol with a
3882 template argument list that includes a pointer. */
3884 static const char expected[] = "ns::foo<char*>";
3885 /* Try both completion and non-completion modes. */
3886 static const bool completion_mode[2] = {false, true};
3887 for (size_t i = 0; i < 2; i++)
3889 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3890 completion_mode[i], EXPECT (expected));
3891 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3892 completion_mode[i], EXPECT (expected));
3894 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3895 completion_mode[i], EXPECT (expected));
3896 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3897 completion_mode[i], EXPECT (expected));
3902 /* Check method qualifiers are ignored. */
3903 static const char expected[] = "ns::foo<char*>";
3904 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3905 symbol_name_match_type::FULL, true, EXPECT (expected));
3906 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3907 symbol_name_match_type::FULL, true, EXPECT (expected));
3908 CHECK_MATCH ("foo < char * > ( int ) const",
3909 symbol_name_match_type::WILD, true, EXPECT (expected));
3910 CHECK_MATCH ("foo < char * > ( int ) &&",
3911 symbol_name_match_type::WILD, true, EXPECT (expected));
3914 /* Test lookup names that don't match anything. */
3916 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3917 {});
3919 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3920 {});
3923 /* Some wild matching tests, exercising "(anonymous namespace)",
3924 which should not be confused with a parameter list. */
3926 static const char *syms[] = {
3927 "A::B::C",
3928 "B::C",
3929 "C",
3930 "A :: B :: C ( int )",
3931 "B :: C ( int )",
3932 "C ( int )",
3935 for (const char *s : syms)
3937 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3938 EXPECT ("(anonymous namespace)::A::B::C"));
3943 static const char expected[] = "ns2::tmpl<int>::foo2";
3944 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3945 EXPECT (expected));
3946 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3947 EXPECT (expected));
3950 SELF_CHECK (!any_mismatch);
3952 #undef EXPECT
3953 #undef CHECK_MATCH
3956 static void
3957 run_test ()
3959 test_mapped_index_find_name_component_bounds ();
3960 test_dw2_expand_symtabs_matching_symbol ();
3963 }} // namespace selftests::dw2_expand_symtabs_matching
3965 #endif /* GDB_SELF_TEST */
3967 /* If FILE_MATCHER is NULL or if PER_CU has
3968 dwarf2_per_cu_quick_data::MARK set (see
3969 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3970 EXPANSION_NOTIFY on it. */
3972 static bool
3973 dw2_expand_symtabs_matching_one
3974 (dwarf2_per_cu_data *per_cu,
3975 dwarf2_per_objfile *per_objfile,
3976 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3977 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3979 if (file_matcher == NULL || per_cu->mark)
3981 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3983 compunit_symtab *symtab
3984 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3985 gdb_assert (symtab != nullptr);
3987 if (expansion_notify != NULL && symtab_was_null)
3988 return expansion_notify (symtab);
3990 return true;
3993 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3994 matched, to expand corresponding CUs that were marked. IDX is the
3995 index of the symbol name that matched. */
3997 static bool
3998 dw2_expand_marked_cus
3999 (dwarf2_per_objfile *per_objfile, offset_type idx,
4000 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4001 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4002 block_search_flags search_flags,
4003 search_domain kind)
4005 offset_type vec_len, vec_idx;
4006 bool global_seen = false;
4007 mapped_index &index
4008 = *(gdb::checked_static_cast<mapped_index *>
4009 (per_objfile->per_bfd->index_table.get ()));
4011 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4012 vec_len = vec[0];
4013 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4015 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4016 /* This value is only valid for index versions >= 7. */
4017 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4018 gdb_index_symbol_kind symbol_kind =
4019 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4020 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4021 /* Only check the symbol attributes if they're present.
4022 Indices prior to version 7 don't record them,
4023 and indices >= 7 may elide them for certain symbols
4024 (gold does this). */
4025 int attrs_valid =
4026 (index.version >= 7
4027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4029 /* Work around gold/15646. */
4030 if (attrs_valid
4031 && !is_static
4032 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4034 if (global_seen)
4035 continue;
4037 global_seen = true;
4040 /* Only check the symbol's kind if it has one. */
4041 if (attrs_valid)
4043 if (is_static)
4045 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4046 continue;
4048 else
4050 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4051 continue;
4054 switch (kind)
4056 case VARIABLES_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4058 continue;
4059 break;
4060 case FUNCTIONS_DOMAIN:
4061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4062 continue;
4063 break;
4064 case TYPES_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4066 continue;
4067 break;
4068 case MODULES_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4070 continue;
4071 break;
4072 default:
4073 break;
4077 /* Don't crash on bad data. */
4078 if (cu_index >= per_objfile->per_bfd->all_units.size ())
4080 complaint (_(".gdb_index entry has bad CU index"
4081 " [in module %s]"), objfile_name (per_objfile->objfile));
4082 continue;
4085 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4086 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4087 expansion_notify))
4088 return false;
4091 return true;
4094 /* If FILE_MATCHER is non-NULL, set all the
4095 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4096 that match FILE_MATCHER. */
4098 static void
4099 dw_expand_symtabs_matching_file_matcher
4100 (dwarf2_per_objfile *per_objfile,
4101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4103 if (file_matcher == NULL)
4104 return;
4106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4107 htab_eq_pointer,
4108 NULL, xcalloc, xfree));
4109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4110 htab_eq_pointer,
4111 NULL, xcalloc, xfree));
4113 /* The rule is CUs specify all the files, including those used by
4114 any TU, so there's no need to scan TUs here. */
4116 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4118 QUIT;
4120 if (per_cu->is_debug_types)
4121 continue;
4122 per_cu->mark = 0;
4124 /* We only need to look at symtabs not already expanded. */
4125 if (per_objfile->symtab_set_p (per_cu.get ()))
4126 continue;
4128 if (per_cu->fnd != nullptr)
4130 file_and_directory *fnd = per_cu->fnd.get ();
4132 if (file_matcher (fnd->get_name (), false))
4134 per_cu->mark = 1;
4135 continue;
4138 /* Before we invoke realpath, which can get expensive when many
4139 files are involved, do a quick comparison of the basenames. */
4140 if ((basenames_may_differ
4141 || file_matcher (lbasename (fnd->get_name ()), true))
4142 && file_matcher (fnd->get_fullname (), false))
4144 per_cu->mark = 1;
4145 continue;
4149 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4150 per_objfile);
4151 if (file_data == NULL)
4152 continue;
4154 if (htab_find (visited_not_found.get (), file_data) != NULL)
4155 continue;
4156 else if (htab_find (visited_found.get (), file_data) != NULL)
4158 per_cu->mark = 1;
4159 continue;
4162 for (int j = 0; j < file_data->num_file_names; ++j)
4164 const char *this_real_name;
4166 if (file_matcher (file_data->file_names[j], false))
4168 per_cu->mark = 1;
4169 break;
4172 /* Before we invoke realpath, which can get expensive when many
4173 files are involved, do a quick comparison of the basenames. */
4174 if (!basenames_may_differ
4175 && !file_matcher (lbasename (file_data->file_names[j]),
4176 true))
4177 continue;
4179 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4180 if (file_matcher (this_real_name, false))
4182 per_cu->mark = 1;
4183 break;
4187 void **slot = htab_find_slot (per_cu->mark
4188 ? visited_found.get ()
4189 : visited_not_found.get (),
4190 file_data, INSERT);
4191 *slot = file_data;
4195 bool
4196 dwarf2_gdb_index::expand_symtabs_matching
4197 (struct objfile *objfile,
4198 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4199 const lookup_name_info *lookup_name,
4200 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4201 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4202 block_search_flags search_flags,
4203 domain_enum domain,
4204 enum search_domain kind)
4206 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4208 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4210 /* This invariant is documented in quick-functions.h. */
4211 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4212 if (lookup_name == nullptr)
4214 for (dwarf2_per_cu_data *per_cu
4215 : all_units_range (per_objfile->per_bfd))
4217 QUIT;
4219 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4220 file_matcher,
4221 expansion_notify))
4222 return false;
4224 return true;
4227 mapped_index &index
4228 = *(gdb::checked_static_cast<mapped_index *>
4229 (per_objfile->per_bfd->index_table.get ()));
4231 bool result
4232 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4233 symbol_matcher,
4234 [&] (offset_type idx)
4236 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4237 expansion_notify, search_flags, kind))
4238 return false;
4239 return true;
4240 }, per_objfile);
4242 return result;
4245 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4246 symtab. */
4248 static struct compunit_symtab *
4249 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4250 CORE_ADDR pc)
4252 int i;
4254 if (cust->blockvector () != nullptr
4255 && blockvector_contains_pc (cust->blockvector (), pc))
4256 return cust;
4258 if (cust->includes == NULL)
4259 return NULL;
4261 for (i = 0; cust->includes[i]; ++i)
4263 struct compunit_symtab *s = cust->includes[i];
4265 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4266 if (s != NULL)
4267 return s;
4270 return NULL;
4273 dwarf2_per_cu_data *
4274 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
4275 CORE_ADDR adjusted_pc)
4277 if (per_bfd->index_addrmap == nullptr)
4278 return nullptr;
4279 return ((struct dwarf2_per_cu_data *)
4280 per_bfd->index_addrmap->find (adjusted_pc));
4283 struct compunit_symtab *
4284 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4285 (struct objfile *objfile,
4286 struct bound_minimal_symbol msymbol,
4287 CORE_ADDR pc,
4288 struct obj_section *section,
4289 int warn_if_readin)
4291 struct compunit_symtab *result;
4293 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4295 CORE_ADDR baseaddr = objfile->text_section_offset ();
4296 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
4297 pc - baseaddr);
4298 if (data == nullptr)
4299 return nullptr;
4301 if (warn_if_readin && per_objfile->symtab_set_p (data))
4302 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4303 paddress (objfile->arch (), pc));
4305 result = recursively_find_pc_sect_compunit_symtab
4306 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4308 if (warn_if_readin && result == nullptr)
4309 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4310 paddress (objfile->arch (), pc));
4312 return result;
4315 void
4316 dwarf2_base_index_functions::map_symbol_filenames
4317 (struct objfile *objfile,
4318 gdb::function_view<symbol_filename_ftype> fun,
4319 bool need_fullname)
4321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4323 /* Use caches to ensure we only call FUN once for each filename. */
4324 filename_seen_cache filenames_cache;
4325 std::unordered_set<quick_file_names *> qfn_cache;
4327 /* The rule is CUs specify all the files, including those used by any TU,
4328 so there's no need to scan TUs here. We can ignore file names coming
4329 from already-expanded CUs. It is possible that an expanded CU might
4330 reuse the file names data from a currently unexpanded CU, in this
4331 case we don't want to report the files from the unexpanded CU. */
4333 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4335 if (!per_cu->is_debug_types
4336 && per_objfile->symtab_set_p (per_cu.get ()))
4338 if (per_cu->file_names != nullptr)
4339 qfn_cache.insert (per_cu->file_names);
4343 for (dwarf2_per_cu_data *per_cu
4344 : all_units_range (per_objfile->per_bfd))
4346 /* We only need to look at symtabs not already expanded. */
4347 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4348 continue;
4350 if (per_cu->fnd != nullptr)
4352 file_and_directory *fnd = per_cu->fnd.get ();
4354 const char *filename = fnd->get_name ();
4355 const char *key = filename;
4356 const char *fullname = nullptr;
4358 if (need_fullname)
4360 fullname = fnd->get_fullname ();
4361 key = fullname;
4364 if (!filenames_cache.seen (key))
4365 fun (filename, fullname);
4368 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4369 if (file_data == nullptr
4370 || qfn_cache.find (file_data) != qfn_cache.end ())
4371 continue;
4373 for (int j = 0; j < file_data->num_file_names; ++j)
4375 const char *filename = file_data->file_names[j];
4376 const char *key = filename;
4377 const char *fullname = nullptr;
4379 if (need_fullname)
4381 fullname = dw2_get_real_path (per_objfile, file_data, j);
4382 key = fullname;
4385 if (!filenames_cache.seen (key))
4386 fun (filename, fullname);
4391 bool
4392 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4394 return true;
4397 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4399 bool
4400 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4402 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4404 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4406 /* Is this already expanded? */
4407 if (per_objfile->symtab_set_p (per_cu.get ()))
4408 continue;
4410 /* It has not yet been expanded. */
4411 return true;
4414 return false;
4417 /* DWARF-5 debug_names reader. */
4419 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4420 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4422 /* A helper function that reads the .debug_names section in SECTION
4423 and fills in MAP. FILENAME is the name of the file containing the
4424 section; it is used for error reporting.
4426 Returns true if all went well, false otherwise. */
4428 static bool
4429 read_debug_names_from_section (struct objfile *objfile,
4430 const char *filename,
4431 struct dwarf2_section_info *section,
4432 mapped_debug_names &map)
4434 if (section->empty ())
4435 return false;
4437 /* Older elfutils strip versions could keep the section in the main
4438 executable while splitting it for the separate debug info file. */
4439 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4440 return false;
4442 section->read (objfile);
4444 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4446 const gdb_byte *addr = section->buffer;
4448 bfd *const abfd = section->get_bfd_owner ();
4450 unsigned int bytes_read;
4451 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4452 addr += bytes_read;
4454 map.dwarf5_is_dwarf64 = bytes_read != 4;
4455 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4456 if (bytes_read + length != section->size)
4458 /* There may be multiple per-CU indices. */
4459 warning (_("Section .debug_names in %s length %s does not match "
4460 "section length %s, ignoring .debug_names."),
4461 filename, plongest (bytes_read + length),
4462 pulongest (section->size));
4463 return false;
4466 /* The version number. */
4467 uint16_t version = read_2_bytes (abfd, addr);
4468 addr += 2;
4469 if (version != 5)
4471 warning (_("Section .debug_names in %s has unsupported version %d, "
4472 "ignoring .debug_names."),
4473 filename, version);
4474 return false;
4477 /* Padding. */
4478 uint16_t padding = read_2_bytes (abfd, addr);
4479 addr += 2;
4480 if (padding != 0)
4482 warning (_("Section .debug_names in %s has unsupported padding %d, "
4483 "ignoring .debug_names."),
4484 filename, padding);
4485 return false;
4488 /* comp_unit_count - The number of CUs in the CU list. */
4489 map.cu_count = read_4_bytes (abfd, addr);
4490 addr += 4;
4492 /* local_type_unit_count - The number of TUs in the local TU
4493 list. */
4494 map.tu_count = read_4_bytes (abfd, addr);
4495 addr += 4;
4497 /* foreign_type_unit_count - The number of TUs in the foreign TU
4498 list. */
4499 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4500 addr += 4;
4501 if (foreign_tu_count != 0)
4503 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4504 "ignoring .debug_names."),
4505 filename, static_cast<unsigned long> (foreign_tu_count));
4506 return false;
4509 /* bucket_count - The number of hash buckets in the hash lookup
4510 table. */
4511 map.bucket_count = read_4_bytes (abfd, addr);
4512 addr += 4;
4514 /* name_count - The number of unique names in the index. */
4515 map.name_count = read_4_bytes (abfd, addr);
4516 addr += 4;
4518 /* abbrev_table_size - The size in bytes of the abbreviations
4519 table. */
4520 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4521 addr += 4;
4523 /* augmentation_string_size - The size in bytes of the augmentation
4524 string. This value is rounded up to a multiple of 4. */
4525 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4526 addr += 4;
4527 map.augmentation_is_gdb = ((augmentation_string_size
4528 == sizeof (dwarf5_augmentation))
4529 && memcmp (addr, dwarf5_augmentation,
4530 sizeof (dwarf5_augmentation)) == 0);
4531 augmentation_string_size += (-augmentation_string_size) & 3;
4532 addr += augmentation_string_size;
4534 /* List of CUs */
4535 map.cu_table_reordered = addr;
4536 addr += map.cu_count * map.offset_size;
4538 /* List of Local TUs */
4539 map.tu_table_reordered = addr;
4540 addr += map.tu_count * map.offset_size;
4542 /* Hash Lookup Table */
4543 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4544 addr += map.bucket_count * 4;
4545 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4546 addr += map.name_count * 4;
4548 /* Name Table */
4549 map.name_table_string_offs_reordered = addr;
4550 addr += map.name_count * map.offset_size;
4551 map.name_table_entry_offs_reordered = addr;
4552 addr += map.name_count * map.offset_size;
4554 const gdb_byte *abbrev_table_start = addr;
4555 for (;;)
4557 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4558 addr += bytes_read;
4559 if (index_num == 0)
4560 break;
4562 const auto insertpair
4563 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4564 if (!insertpair.second)
4566 warning (_("Section .debug_names in %s has duplicate index %s, "
4567 "ignoring .debug_names."),
4568 filename, pulongest (index_num));
4569 return false;
4571 mapped_debug_names::index_val &indexval = insertpair.first->second;
4572 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4573 addr += bytes_read;
4575 for (;;)
4577 mapped_debug_names::index_val::attr attr;
4578 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4579 addr += bytes_read;
4580 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4581 addr += bytes_read;
4582 if (attr.form == DW_FORM_implicit_const)
4584 attr.implicit_const = read_signed_leb128 (abfd, addr,
4585 &bytes_read);
4586 addr += bytes_read;
4588 if (attr.dw_idx == 0 && attr.form == 0)
4589 break;
4590 indexval.attr_vec.push_back (std::move (attr));
4593 if (addr != abbrev_table_start + abbrev_table_size)
4595 warning (_("Section .debug_names in %s has abbreviation_table "
4596 "of size %s vs. written as %u, ignoring .debug_names."),
4597 filename, plongest (addr - abbrev_table_start),
4598 abbrev_table_size);
4599 return false;
4601 map.entry_pool = addr;
4603 return true;
4606 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4607 list. */
4609 static bool
4610 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4611 const mapped_debug_names &map,
4612 dwarf2_section_info &section,
4613 bool is_dwz)
4615 if (!map.augmentation_is_gdb)
4617 for (uint32_t i = 0; i < map.cu_count; ++i)
4619 sect_offset sect_off
4620 = (sect_offset) (extract_unsigned_integer
4621 (map.cu_table_reordered + i * map.offset_size,
4622 map.offset_size,
4623 map.dwarf5_byte_order));
4624 /* We don't know the length of the CU, because the CU list in a
4625 .debug_names index can be incomplete, so we can't use the start
4626 of the next CU as end of this CU. We create the CUs here with
4627 length 0, and in cutu_reader::cutu_reader we'll fill in the
4628 actual length. */
4629 dwarf2_per_cu_data_up per_cu
4630 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4631 sect_off, 0);
4632 per_bfd->all_units.push_back (std::move (per_cu));
4634 return true;
4637 sect_offset sect_off_prev;
4638 for (uint32_t i = 0; i <= map.cu_count; ++i)
4640 sect_offset sect_off_next;
4641 if (i < map.cu_count)
4643 sect_off_next
4644 = (sect_offset) (extract_unsigned_integer
4645 (map.cu_table_reordered + i * map.offset_size,
4646 map.offset_size,
4647 map.dwarf5_byte_order));
4649 else
4650 sect_off_next = (sect_offset) section.size;
4651 if (i >= 1)
4653 if (sect_off_next == sect_off_prev)
4655 warning (_("Section .debug_names has duplicate entry in CU table,"
4656 " ignoring .debug_names."));
4657 return false;
4659 if (sect_off_next < sect_off_prev)
4661 warning (_("Section .debug_names has non-ascending CU table,"
4662 " ignoring .debug_names."));
4663 return false;
4665 /* Note: we're not using length = sect_off_next - sect_off_prev,
4666 to gracefully handle an incomplete CU list. */
4667 const ULONGEST length = 0;
4668 dwarf2_per_cu_data_up per_cu
4669 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4670 sect_off_prev, length);
4671 per_bfd->all_units.push_back (std::move (per_cu));
4673 sect_off_prev = sect_off_next;
4676 return true;
4679 /* Read the CU list from the mapped index, and use it to create all
4680 the CU objects for this dwarf2_per_objfile. */
4682 static bool
4683 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4684 const mapped_debug_names &map,
4685 const mapped_debug_names &dwz_map)
4687 gdb_assert (per_bfd->all_units.empty ());
4688 per_bfd->all_units.reserve (map.cu_count + dwz_map.cu_count);
4690 if (!create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4691 false /* is_dwz */))
4692 return false;
4694 if (dwz_map.cu_count == 0)
4695 return true;
4697 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4698 return create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4699 true /* is_dwz */);
4702 /* Read .debug_names. If everything went ok, initialize the "quick"
4703 elements of all the CUs and return true. Otherwise, return false. */
4705 static bool
4706 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4708 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4709 mapped_debug_names dwz_map;
4710 struct objfile *objfile = per_objfile->objfile;
4711 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4713 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4714 &per_bfd->debug_names, *map))
4715 return false;
4717 /* Don't use the index if it's empty. */
4718 if (map->name_count == 0)
4719 return false;
4721 /* If there is a .dwz file, read it so we can get its CU list as
4722 well. */
4723 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4724 if (dwz != NULL)
4726 if (!read_debug_names_from_section (objfile,
4727 bfd_get_filename (dwz->dwz_bfd.get ()),
4728 &dwz->debug_names, dwz_map))
4730 warning (_("could not read '.debug_names' section from %s; skipping"),
4731 bfd_get_filename (dwz->dwz_bfd.get ()));
4732 return false;
4736 if (!create_cus_from_debug_names (per_bfd, *map, dwz_map))
4738 per_bfd->all_units.clear ();
4739 return false;
4742 if (map->tu_count != 0)
4744 /* We can only handle a single .debug_types when we have an
4745 index. */
4746 if (per_bfd->types.size () > 1)
4748 per_bfd->all_units.clear ();
4749 return false;
4752 dwarf2_section_info *section
4753 = (per_bfd->types.size () == 1
4754 ? &per_bfd->types[0]
4755 : &per_bfd->info);
4757 create_signatured_type_table_from_debug_names
4758 (per_objfile, *map, section, &per_bfd->abbrev);
4761 finalize_all_units (per_bfd);
4763 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4765 per_bfd->index_table = std::move (map);
4766 per_bfd->quick_file_names_table =
4767 create_quick_file_names_table (per_bfd->all_units.size ());
4769 return true;
4772 /* Type used to manage iterating over all CUs looking for a symbol for
4773 .debug_names. */
4775 class dw2_debug_names_iterator
4777 public:
4778 dw2_debug_names_iterator (const mapped_debug_names &map,
4779 block_search_flags block_index,
4780 domain_enum domain,
4781 const char *name, dwarf2_per_objfile *per_objfile)
4782 : m_map (map), m_block_index (block_index), m_domain (domain),
4783 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4784 m_per_objfile (per_objfile)
4787 dw2_debug_names_iterator (const mapped_debug_names &map,
4788 search_domain search, uint32_t namei,
4789 dwarf2_per_objfile *per_objfile,
4790 domain_enum domain = UNDEF_DOMAIN)
4791 : m_map (map),
4792 m_domain (domain),
4793 m_search (search),
4794 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4795 m_per_objfile (per_objfile)
4798 dw2_debug_names_iterator (const mapped_debug_names &map,
4799 block_search_flags block_index, domain_enum domain,
4800 uint32_t namei, dwarf2_per_objfile *per_objfile)
4801 : m_map (map), m_block_index (block_index), m_domain (domain),
4802 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4803 m_per_objfile (per_objfile)
4806 /* Return the next matching CU or NULL if there are no more. */
4807 dwarf2_per_cu_data *next ();
4809 private:
4810 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4811 const char *name,
4812 dwarf2_per_objfile *per_objfile);
4813 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4814 uint32_t namei,
4815 dwarf2_per_objfile *per_objfile);
4817 /* The internalized form of .debug_names. */
4818 const mapped_debug_names &m_map;
4820 /* Restrict the search to these blocks. */
4821 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4822 | SEARCH_STATIC_BLOCK);
4824 /* The kind of symbol we're looking for. */
4825 const domain_enum m_domain = UNDEF_DOMAIN;
4826 const search_domain m_search = ALL_DOMAIN;
4828 /* The list of CUs from the index entry of the symbol, or NULL if
4829 not found. */
4830 const gdb_byte *m_addr;
4832 dwarf2_per_objfile *m_per_objfile;
4835 const char *
4836 mapped_debug_names::namei_to_name
4837 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4839 const ULONGEST namei_string_offs
4840 = extract_unsigned_integer ((name_table_string_offs_reordered
4841 + namei * offset_size),
4842 offset_size,
4843 dwarf5_byte_order);
4844 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4847 /* Find a slot in .debug_names for the object named NAME. If NAME is
4848 found, return pointer to its pool data. If NAME cannot be found,
4849 return NULL. */
4851 const gdb_byte *
4852 dw2_debug_names_iterator::find_vec_in_debug_names
4853 (const mapped_debug_names &map, const char *name,
4854 dwarf2_per_objfile *per_objfile)
4856 int (*cmp) (const char *, const char *);
4858 gdb::unique_xmalloc_ptr<char> without_params;
4859 if (current_language->la_language == language_cplus
4860 || current_language->la_language == language_fortran
4861 || current_language->la_language == language_d)
4863 /* NAME is already canonical. Drop any qualifiers as
4864 .debug_names does not contain any. */
4866 if (strchr (name, '(') != NULL)
4868 without_params = cp_remove_params (name);
4869 if (without_params != NULL)
4870 name = without_params.get ();
4874 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4876 const uint32_t full_hash = dwarf5_djb_hash (name);
4877 uint32_t namei
4878 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4879 (map.bucket_table_reordered
4880 + (full_hash % map.bucket_count)), 4,
4881 map.dwarf5_byte_order);
4882 if (namei == 0)
4883 return NULL;
4884 --namei;
4885 if (namei >= map.name_count)
4887 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4888 "[in module %s]"),
4889 namei, map.name_count,
4890 objfile_name (per_objfile->objfile));
4891 return NULL;
4894 for (;;)
4896 const uint32_t namei_full_hash
4897 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4898 (map.hash_table_reordered + namei), 4,
4899 map.dwarf5_byte_order);
4900 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4901 return NULL;
4903 if (full_hash == namei_full_hash)
4905 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4907 #if 0 /* An expensive sanity check. */
4908 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4910 complaint (_("Wrong .debug_names hash for string at index %u "
4911 "[in module %s]"),
4912 namei, objfile_name (dwarf2_per_objfile->objfile));
4913 return NULL;
4915 #endif
4917 if (cmp (namei_string, name) == 0)
4919 const ULONGEST namei_entry_offs
4920 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4921 + namei * map.offset_size),
4922 map.offset_size, map.dwarf5_byte_order);
4923 return map.entry_pool + namei_entry_offs;
4927 ++namei;
4928 if (namei >= map.name_count)
4929 return NULL;
4933 const gdb_byte *
4934 dw2_debug_names_iterator::find_vec_in_debug_names
4935 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4937 if (namei >= map.name_count)
4939 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4940 "[in module %s]"),
4941 namei, map.name_count,
4942 objfile_name (per_objfile->objfile));
4943 return NULL;
4946 const ULONGEST namei_entry_offs
4947 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4948 + namei * map.offset_size),
4949 map.offset_size, map.dwarf5_byte_order);
4950 return map.entry_pool + namei_entry_offs;
4953 /* See dw2_debug_names_iterator. */
4955 dwarf2_per_cu_data *
4956 dw2_debug_names_iterator::next ()
4958 if (m_addr == NULL)
4959 return NULL;
4961 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4962 struct objfile *objfile = m_per_objfile->objfile;
4963 bfd *const abfd = objfile->obfd.get ();
4965 again:
4967 unsigned int bytes_read;
4968 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4969 m_addr += bytes_read;
4970 if (abbrev == 0)
4971 return NULL;
4973 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4974 if (indexval_it == m_map.abbrev_map.cend ())
4976 complaint (_("Wrong .debug_names undefined abbrev code %s "
4977 "[in module %s]"),
4978 pulongest (abbrev), objfile_name (objfile));
4979 return NULL;
4981 const mapped_debug_names::index_val &indexval = indexval_it->second;
4982 enum class symbol_linkage {
4983 unknown,
4984 static_,
4985 extern_,
4986 } symbol_linkage_ = symbol_linkage::unknown;
4987 dwarf2_per_cu_data *per_cu = NULL;
4988 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4990 ULONGEST ull;
4991 switch (attr.form)
4993 case DW_FORM_implicit_const:
4994 ull = attr.implicit_const;
4995 break;
4996 case DW_FORM_flag_present:
4997 ull = 1;
4998 break;
4999 case DW_FORM_udata:
5000 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5001 m_addr += bytes_read;
5002 break;
5003 case DW_FORM_ref4:
5004 ull = read_4_bytes (abfd, m_addr);
5005 m_addr += 4;
5006 break;
5007 case DW_FORM_ref8:
5008 ull = read_8_bytes (abfd, m_addr);
5009 m_addr += 8;
5010 break;
5011 case DW_FORM_ref_sig8:
5012 ull = read_8_bytes (abfd, m_addr);
5013 m_addr += 8;
5014 break;
5015 default:
5016 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5017 dwarf_form_name (attr.form),
5018 objfile_name (objfile));
5019 return NULL;
5021 switch (attr.dw_idx)
5023 case DW_IDX_compile_unit:
5025 /* Don't crash on bad data. */
5026 if (ull >= per_bfd->all_comp_units.size ())
5028 complaint (_(".debug_names entry has bad CU index %s"
5029 " [in module %s]"),
5030 pulongest (ull),
5031 objfile_name (objfile));
5032 continue;
5035 per_cu = per_bfd->get_cu (ull);
5036 break;
5037 case DW_IDX_type_unit:
5038 /* Don't crash on bad data. */
5039 if (ull >= per_bfd->all_type_units.size ())
5041 complaint (_(".debug_names entry has bad TU index %s"
5042 " [in module %s]"),
5043 pulongest (ull),
5044 objfile_name (objfile));
5045 continue;
5048 int nr_cus = per_bfd->all_comp_units.size ();
5049 per_cu = per_bfd->get_cu (nr_cus + ull);
5051 break;
5052 case DW_IDX_die_offset:
5053 /* In a per-CU index (as opposed to a per-module index), index
5054 entries without CU attribute implicitly refer to the single CU. */
5055 if (per_cu == NULL)
5056 per_cu = per_bfd->get_cu (0);
5057 break;
5058 case DW_IDX_GNU_internal:
5059 if (!m_map.augmentation_is_gdb)
5060 break;
5061 symbol_linkage_ = symbol_linkage::static_;
5062 break;
5063 case DW_IDX_GNU_external:
5064 if (!m_map.augmentation_is_gdb)
5065 break;
5066 symbol_linkage_ = symbol_linkage::extern_;
5067 break;
5071 /* Skip if we couldn't find a valid CU/TU index. */
5072 if (per_cu == nullptr)
5073 goto again;
5075 /* Skip if already read in. */
5076 if (m_per_objfile->symtab_set_p (per_cu))
5077 goto again;
5079 /* Check static vs global. */
5080 if (symbol_linkage_ != symbol_linkage::unknown)
5082 if (symbol_linkage_ == symbol_linkage::static_)
5084 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5085 goto again;
5087 else
5089 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5090 goto again;
5094 /* Match dw2_symtab_iter_next, symbol_kind
5095 and debug_names::psymbol_tag. */
5096 switch (m_domain)
5098 case VAR_DOMAIN:
5099 switch (indexval.dwarf_tag)
5101 case DW_TAG_variable:
5102 case DW_TAG_subprogram:
5103 /* Some types are also in VAR_DOMAIN. */
5104 case DW_TAG_typedef:
5105 case DW_TAG_structure_type:
5106 break;
5107 default:
5108 goto again;
5110 break;
5111 case STRUCT_DOMAIN:
5112 switch (indexval.dwarf_tag)
5114 case DW_TAG_typedef:
5115 case DW_TAG_structure_type:
5116 break;
5117 default:
5118 goto again;
5120 break;
5121 case LABEL_DOMAIN:
5122 switch (indexval.dwarf_tag)
5124 case 0:
5125 case DW_TAG_variable:
5126 break;
5127 default:
5128 goto again;
5130 break;
5131 case MODULE_DOMAIN:
5132 switch (indexval.dwarf_tag)
5134 case DW_TAG_module:
5135 break;
5136 default:
5137 goto again;
5139 break;
5140 default:
5141 break;
5144 /* Match dw2_expand_symtabs_matching, symbol_kind and
5145 debug_names::psymbol_tag. */
5146 switch (m_search)
5148 case VARIABLES_DOMAIN:
5149 switch (indexval.dwarf_tag)
5151 case DW_TAG_variable:
5152 break;
5153 default:
5154 goto again;
5156 break;
5157 case FUNCTIONS_DOMAIN:
5158 switch (indexval.dwarf_tag)
5160 case DW_TAG_subprogram:
5161 break;
5162 default:
5163 goto again;
5165 break;
5166 case TYPES_DOMAIN:
5167 switch (indexval.dwarf_tag)
5169 case DW_TAG_typedef:
5170 case DW_TAG_structure_type:
5171 break;
5172 default:
5173 goto again;
5175 break;
5176 case MODULES_DOMAIN:
5177 switch (indexval.dwarf_tag)
5179 case DW_TAG_module:
5180 break;
5181 default:
5182 goto again;
5184 default:
5185 break;
5188 return per_cu;
5191 /* This dumps minimal information about .debug_names. It is called
5192 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5193 uses this to verify that .debug_names has been loaded. */
5195 void
5196 dwarf2_debug_names_index::dump (struct objfile *objfile)
5198 gdb_printf (".debug_names: exists\n");
5201 void
5202 dwarf2_debug_names_index::expand_matching_symbols
5203 (struct objfile *objfile,
5204 const lookup_name_info &name, domain_enum domain,
5205 int global,
5206 symbol_compare_ftype *ordered_compare)
5208 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5210 mapped_debug_names &map
5211 = *(gdb::checked_static_cast<mapped_debug_names *>
5212 (per_objfile->per_bfd->index_table.get ()));
5213 const block_search_flags block_flags
5214 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5216 const char *match_name = name.ada ().lookup_name ().c_str ();
5217 auto matcher = [&] (const char *symname)
5219 if (ordered_compare == nullptr)
5220 return true;
5221 return ordered_compare (symname, match_name) == 0;
5224 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5225 [&] (offset_type namei)
5227 /* The name was matched, now expand corresponding CUs that were
5228 marked. */
5229 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5230 per_objfile);
5232 struct dwarf2_per_cu_data *per_cu;
5233 while ((per_cu = iter.next ()) != NULL)
5234 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5235 nullptr);
5236 return true;
5237 }, per_objfile);
5240 bool
5241 dwarf2_debug_names_index::expand_symtabs_matching
5242 (struct objfile *objfile,
5243 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5244 const lookup_name_info *lookup_name,
5245 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5246 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5247 block_search_flags search_flags,
5248 domain_enum domain,
5249 enum search_domain kind)
5251 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5253 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5255 /* This invariant is documented in quick-functions.h. */
5256 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5257 if (lookup_name == nullptr)
5259 for (dwarf2_per_cu_data *per_cu
5260 : all_units_range (per_objfile->per_bfd))
5262 QUIT;
5264 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5265 file_matcher,
5266 expansion_notify))
5267 return false;
5269 return true;
5272 mapped_debug_names &map
5273 = *(gdb::checked_static_cast<mapped_debug_names *>
5274 (per_objfile->per_bfd->index_table.get ()));
5276 bool result
5277 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5278 symbol_matcher,
5279 [&] (offset_type namei)
5281 /* The name was matched, now expand corresponding CUs that were
5282 marked. */
5283 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5285 struct dwarf2_per_cu_data *per_cu;
5286 while ((per_cu = iter.next ()) != NULL)
5287 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5288 file_matcher,
5289 expansion_notify))
5290 return false;
5291 return true;
5292 }, per_objfile);
5294 return result;
5297 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5298 to either a dwarf2_per_bfd or dwz_file object. */
5300 template <typename T>
5301 static gdb::array_view<const gdb_byte>
5302 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5304 dwarf2_section_info *section = &section_owner->gdb_index;
5306 if (section->empty ())
5307 return {};
5309 /* Older elfutils strip versions could keep the section in the main
5310 executable while splitting it for the separate debug info file. */
5311 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5312 return {};
5314 section->read (obj);
5316 /* dwarf2_section_info::size is a bfd_size_type, while
5317 gdb::array_view works with size_t. On 32-bit hosts, with
5318 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5319 is 32-bit. So we need an explicit narrowing conversion here.
5320 This is fine, because it's impossible to allocate or mmap an
5321 array/buffer larger than what size_t can represent. */
5322 return gdb::make_array_view (section->buffer, section->size);
5325 /* Lookup the index cache for the contents of the index associated to
5326 DWARF2_OBJ. */
5328 static gdb::array_view<const gdb_byte>
5329 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5331 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
5332 if (build_id == nullptr)
5333 return {};
5335 return global_index_cache.lookup_gdb_index (build_id,
5336 &dwarf2_per_bfd->index_cache_res);
5339 /* Same as the above, but for DWZ. */
5341 static gdb::array_view<const gdb_byte>
5342 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5344 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5345 if (build_id == nullptr)
5346 return {};
5348 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5351 static quick_symbol_functions_up make_cooked_index_funcs ();
5353 /* See dwarf2/public.h. */
5355 void
5356 dwarf2_initialize_objfile (struct objfile *objfile)
5358 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5359 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5361 dwarf_read_debug_printf ("called");
5363 /* If we're about to read full symbols, don't bother with the
5364 indices. In this case we also don't care if some other debug
5365 format is making psymtabs, because they are all about to be
5366 expanded anyway. */
5367 if ((objfile->flags & OBJF_READNOW))
5369 dwarf_read_debug_printf ("readnow requested");
5371 create_all_units (per_objfile);
5372 per_bfd->quick_file_names_table
5373 = create_quick_file_names_table (per_bfd->all_units.size ());
5375 objfile->qf.emplace_front (new readnow_functions);
5376 return;
5379 /* Was a GDB index already read when we processed an objfile sharing
5380 PER_BFD? */
5381 if (per_bfd->index_table != nullptr)
5383 dwarf_read_debug_printf ("re-using symbols");
5384 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5385 return;
5388 if (dwarf2_read_debug_names (per_objfile))
5390 dwarf_read_debug_printf ("found debug names");
5391 objfile->qf.push_front
5392 (per_bfd->index_table->make_quick_functions ());
5393 return;
5396 if (dwarf2_read_gdb_index (per_objfile,
5397 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5398 get_gdb_index_contents_from_section<dwz_file>))
5400 dwarf_read_debug_printf ("found gdb index from file");
5401 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5402 return;
5405 /* ... otherwise, try to find the index in the index cache. */
5406 if (dwarf2_read_gdb_index (per_objfile,
5407 get_gdb_index_contents_from_cache,
5408 get_gdb_index_contents_from_cache_dwz))
5410 dwarf_read_debug_printf ("found gdb index from cache");
5411 global_index_cache.hit ();
5412 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5413 return;
5416 global_index_cache.miss ();
5417 objfile->qf.push_front (make_cooked_index_funcs ());
5422 /* Build a partial symbol table. */
5424 static void
5425 dwarf2_build_psymtabs (struct objfile *objfile)
5427 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5429 if (per_objfile->per_bfd->index_table != nullptr)
5430 return;
5434 dwarf2_build_psymtabs_hard (per_objfile);
5436 /* (maybe) store an index in the cache. */
5437 global_index_cache.store (per_objfile);
5439 catch (const gdb_exception_error &except)
5441 exception_print (gdb_stderr, except);
5445 /* Find the base address of the compilation unit for range lists and
5446 location lists. It will normally be specified by DW_AT_low_pc.
5447 In DWARF-3 draft 4, the base address could be overridden by
5448 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5449 compilation units with discontinuous ranges. */
5451 static void
5452 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5454 struct attribute *attr;
5456 cu->base_address.reset ();
5458 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5459 if (attr != nullptr)
5460 cu->base_address = attr->as_address ();
5461 else
5463 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5464 if (attr != nullptr)
5465 cu->base_address = attr->as_address ();
5469 /* Helper function that returns the proper abbrev section for
5470 THIS_CU. */
5472 static struct dwarf2_section_info *
5473 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5475 struct dwarf2_section_info *abbrev;
5476 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5478 if (this_cu->is_dwz)
5479 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5480 else
5481 abbrev = &per_bfd->abbrev;
5483 return abbrev;
5486 /* Fetch the abbreviation table offset from a comp or type unit header. */
5488 static sect_offset
5489 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5490 struct dwarf2_section_info *section,
5491 sect_offset sect_off)
5493 bfd *abfd = section->get_bfd_owner ();
5494 const gdb_byte *info_ptr;
5495 unsigned int initial_length_size, offset_size;
5496 uint16_t version;
5498 section->read (per_objfile->objfile);
5499 info_ptr = section->buffer + to_underlying (sect_off);
5500 read_initial_length (abfd, info_ptr, &initial_length_size);
5501 offset_size = initial_length_size == 4 ? 4 : 8;
5502 info_ptr += initial_length_size;
5504 version = read_2_bytes (abfd, info_ptr);
5505 info_ptr += 2;
5506 if (version >= 5)
5508 /* Skip unit type and address size. */
5509 info_ptr += 2;
5512 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5515 static hashval_t
5516 hash_signatured_type (const void *item)
5518 const struct signatured_type *sig_type
5519 = (const struct signatured_type *) item;
5521 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5522 return sig_type->signature;
5525 static int
5526 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5528 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5529 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5531 return lhs->signature == rhs->signature;
5534 /* Allocate a hash table for signatured types. */
5536 static htab_up
5537 allocate_signatured_type_table ()
5539 return htab_up (htab_create_alloc (41,
5540 hash_signatured_type,
5541 eq_signatured_type,
5542 NULL, xcalloc, xfree));
5545 /* A helper for create_debug_types_hash_table. Read types from SECTION
5546 and fill them into TYPES_HTAB. It will process only type units,
5547 therefore DW_UT_type. */
5549 static void
5550 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5551 struct dwo_file *dwo_file,
5552 dwarf2_section_info *section, htab_up &types_htab,
5553 rcuh_kind section_kind)
5555 struct objfile *objfile = per_objfile->objfile;
5556 struct dwarf2_section_info *abbrev_section;
5557 bfd *abfd;
5558 const gdb_byte *info_ptr, *end_ptr;
5560 abbrev_section = &dwo_file->sections.abbrev;
5562 dwarf_read_debug_printf ("Reading %s for %s",
5563 section->get_name (),
5564 abbrev_section->get_file_name ());
5566 section->read (objfile);
5567 info_ptr = section->buffer;
5569 if (info_ptr == NULL)
5570 return;
5572 /* We can't set abfd until now because the section may be empty or
5573 not present, in which case the bfd is unknown. */
5574 abfd = section->get_bfd_owner ();
5576 /* We don't use cutu_reader here because we don't need to read
5577 any dies: the signature is in the header. */
5579 end_ptr = info_ptr + section->size;
5580 while (info_ptr < end_ptr)
5582 signatured_type_up sig_type;
5583 struct dwo_unit *dwo_tu;
5584 void **slot;
5585 const gdb_byte *ptr = info_ptr;
5586 struct comp_unit_head header;
5587 unsigned int length;
5589 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5591 /* Initialize it due to a false compiler warning. */
5592 header.signature = -1;
5593 header.type_cu_offset_in_tu = (cu_offset) -1;
5595 /* We need to read the type's signature in order to build the hash
5596 table, but we don't need anything else just yet. */
5598 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5599 abbrev_section, ptr, section_kind);
5601 length = header.get_length_with_initial ();
5603 /* Skip dummy type units. */
5604 if (ptr >= info_ptr + length
5605 || peek_abbrev_code (abfd, ptr) == 0
5606 || (header.unit_type != DW_UT_type
5607 && header.unit_type != DW_UT_split_type))
5609 info_ptr += length;
5610 continue;
5613 if (types_htab == NULL)
5614 types_htab = allocate_dwo_unit_table ();
5616 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5617 dwo_tu->dwo_file = dwo_file;
5618 dwo_tu->signature = header.signature;
5619 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5620 dwo_tu->section = section;
5621 dwo_tu->sect_off = sect_off;
5622 dwo_tu->length = length;
5624 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5625 gdb_assert (slot != NULL);
5626 if (*slot != NULL)
5627 complaint (_("debug type entry at offset %s is duplicate to"
5628 " the entry at offset %s, signature %s"),
5629 sect_offset_str (sect_off),
5630 sect_offset_str (dwo_tu->sect_off),
5631 hex_string (header.signature));
5632 *slot = dwo_tu;
5634 dwarf_read_debug_printf_v (" offset %s, signature %s",
5635 sect_offset_str (sect_off),
5636 hex_string (header.signature));
5638 info_ptr += length;
5642 /* Create the hash table of all entries in the .debug_types
5643 (or .debug_types.dwo) section(s).
5644 DWO_FILE is a pointer to the DWO file object.
5646 The result is a pointer to the hash table or NULL if there are no types.
5648 Note: This function processes DWO files only, not DWP files. */
5650 static void
5651 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5652 struct dwo_file *dwo_file,
5653 gdb::array_view<dwarf2_section_info> type_sections,
5654 htab_up &types_htab)
5656 for (dwarf2_section_info &section : type_sections)
5657 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5658 rcuh_kind::TYPE);
5661 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5662 If SLOT is non-NULL, it is the entry to use in the hash table.
5663 Otherwise we find one. */
5665 static struct signatured_type *
5666 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5668 if (per_objfile->per_bfd->all_units.size ()
5669 == per_objfile->per_bfd->all_units.capacity ())
5670 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5672 signatured_type_up sig_type_holder
5673 = per_objfile->per_bfd->allocate_signatured_type (sig);
5674 signatured_type *sig_type = sig_type_holder.get ();
5676 per_objfile->per_bfd->all_units.emplace_back
5677 (sig_type_holder.release ());
5679 if (slot == NULL)
5681 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5682 sig_type, INSERT);
5684 gdb_assert (*slot == NULL);
5685 *slot = sig_type;
5686 /* The rest of sig_type must be filled in by the caller. */
5687 return sig_type;
5690 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5691 Fill in SIG_ENTRY with DWO_ENTRY. */
5693 static void
5694 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5695 struct signatured_type *sig_entry,
5696 struct dwo_unit *dwo_entry)
5698 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5700 /* Make sure we're not clobbering something we don't expect to. */
5701 gdb_assert (! sig_entry->queued);
5702 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5703 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5704 gdb_assert (sig_entry->signature == dwo_entry->signature);
5705 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5706 || (to_underlying (sig_entry->type_offset_in_section)
5707 == to_underlying (dwo_entry->type_offset_in_tu)));
5708 gdb_assert (sig_entry->type_unit_group == NULL);
5709 gdb_assert (sig_entry->dwo_unit == NULL
5710 || sig_entry->dwo_unit == dwo_entry);
5712 sig_entry->section = dwo_entry->section;
5713 sig_entry->sect_off = dwo_entry->sect_off;
5714 sig_entry->set_length (dwo_entry->length, false);
5715 sig_entry->reading_dwo_directly = 1;
5716 sig_entry->per_bfd = per_bfd;
5717 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5718 sig_entry->dwo_unit = dwo_entry;
5721 /* Subroutine of lookup_signatured_type.
5722 If we haven't read the TU yet, create the signatured_type data structure
5723 for a TU to be read in directly from a DWO file, bypassing the stub.
5724 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5725 using .gdb_index, then when reading a CU we want to stay in the DWO file
5726 containing that CU. Otherwise we could end up reading several other DWO
5727 files (due to comdat folding) to process the transitive closure of all the
5728 mentioned TUs, and that can be slow. The current DWO file will have every
5729 type signature that it needs.
5730 We only do this for .gdb_index because in the psymtab case we already have
5731 to read all the DWOs to build the type unit groups. */
5733 static struct signatured_type *
5734 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5737 struct dwo_file *dwo_file;
5738 struct dwo_unit find_dwo_entry, *dwo_entry;
5739 void **slot;
5741 gdb_assert (cu->dwo_unit);
5743 /* If TU skeletons have been removed then we may not have read in any
5744 TUs yet. */
5745 if (per_objfile->per_bfd->signatured_types == NULL)
5746 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5748 /* We only ever need to read in one copy of a signatured type.
5749 Use the global signatured_types array to do our own comdat-folding
5750 of types. If this is the first time we're reading this TU, and
5751 the TU has an entry in .gdb_index, replace the recorded data from
5752 .gdb_index with this TU. */
5754 signatured_type find_sig_entry (sig);
5755 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5756 &find_sig_entry, INSERT);
5757 signatured_type *sig_entry = (struct signatured_type *) *slot;
5759 /* We can get here with the TU already read, *or* in the process of being
5760 read. Don't reassign the global entry to point to this DWO if that's
5761 the case. Also note that if the TU is already being read, it may not
5762 have come from a DWO, the program may be a mix of Fission-compiled
5763 code and non-Fission-compiled code. */
5765 /* Have we already tried to read this TU?
5766 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5767 needn't exist in the global table yet). */
5768 if (sig_entry != NULL && sig_entry->tu_read)
5769 return sig_entry;
5771 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5772 dwo_unit of the TU itself. */
5773 dwo_file = cu->dwo_unit->dwo_file;
5775 /* Ok, this is the first time we're reading this TU. */
5776 if (dwo_file->tus == NULL)
5777 return NULL;
5778 find_dwo_entry.signature = sig;
5779 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5780 &find_dwo_entry);
5781 if (dwo_entry == NULL)
5782 return NULL;
5784 /* If the global table doesn't have an entry for this TU, add one. */
5785 if (sig_entry == NULL)
5786 sig_entry = add_type_unit (per_objfile, sig, slot);
5788 if (sig_entry->dwo_unit == nullptr)
5789 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5790 sig_entry->tu_read = 1;
5791 return sig_entry;
5794 /* Subroutine of lookup_signatured_type.
5795 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5796 then try the DWP file. If the TU stub (skeleton) has been removed then
5797 it won't be in .gdb_index. */
5799 static struct signatured_type *
5800 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5803 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5804 struct dwo_unit *dwo_entry;
5805 void **slot;
5807 gdb_assert (cu->dwo_unit);
5808 gdb_assert (dwp_file != NULL);
5810 /* If TU skeletons have been removed then we may not have read in any
5811 TUs yet. */
5812 if (per_objfile->per_bfd->signatured_types == NULL)
5813 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5815 signatured_type find_sig_entry (sig);
5816 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5817 &find_sig_entry, INSERT);
5818 signatured_type *sig_entry = (struct signatured_type *) *slot;
5820 /* Have we already tried to read this TU?
5821 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5822 needn't exist in the global table yet). */
5823 if (sig_entry != NULL)
5824 return sig_entry;
5826 if (dwp_file->tus == NULL)
5827 return NULL;
5828 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5829 1 /* is_debug_types */);
5830 if (dwo_entry == NULL)
5831 return NULL;
5833 sig_entry = add_type_unit (per_objfile, sig, slot);
5834 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5836 return sig_entry;
5839 /* Lookup a signature based type for DW_FORM_ref_sig8.
5840 Returns NULL if signature SIG is not present in the table.
5841 It is up to the caller to complain about this. */
5843 static struct signatured_type *
5844 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5848 if (cu->dwo_unit)
5850 /* We're in a DWO/DWP file, and we're using .gdb_index.
5851 These cases require special processing. */
5852 if (get_dwp_file (per_objfile) == NULL)
5853 return lookup_dwo_signatured_type (cu, sig);
5854 else
5855 return lookup_dwp_signatured_type (cu, sig);
5857 else
5859 if (per_objfile->per_bfd->signatured_types == NULL)
5860 return NULL;
5861 signatured_type find_entry (sig);
5862 return ((struct signatured_type *)
5863 htab_find (per_objfile->per_bfd->signatured_types.get (),
5864 &find_entry));
5868 /* Low level DIE reading support. */
5870 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5872 static void
5873 init_cu_die_reader (struct die_reader_specs *reader,
5874 struct dwarf2_cu *cu,
5875 struct dwarf2_section_info *section,
5876 struct dwo_file *dwo_file,
5877 struct abbrev_table *abbrev_table)
5879 gdb_assert (section->readin && section->buffer != NULL);
5880 reader->abfd = section->get_bfd_owner ();
5881 reader->cu = cu;
5882 reader->dwo_file = dwo_file;
5883 reader->die_section = section;
5884 reader->buffer = section->buffer;
5885 reader->buffer_end = section->buffer + section->size;
5886 reader->abbrev_table = abbrev_table;
5889 /* Subroutine of cutu_reader to simplify it.
5890 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5891 There's just a lot of work to do, and cutu_reader is big enough
5892 already.
5894 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5895 from it to the DIE in the DWO. If NULL we are skipping the stub.
5896 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5897 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5898 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5899 STUB_COMP_DIR may be non-NULL.
5900 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5901 are filled in with the info of the DIE from the DWO file.
5902 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5903 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5904 kept around for at least as long as *RESULT_READER.
5906 The result is non-zero if a valid (non-dummy) DIE was found. */
5908 static int
5909 read_cutu_die_from_dwo (dwarf2_cu *cu,
5910 struct dwo_unit *dwo_unit,
5911 struct die_info *stub_comp_unit_die,
5912 const char *stub_comp_dir,
5913 struct die_reader_specs *result_reader,
5914 const gdb_byte **result_info_ptr,
5915 struct die_info **result_comp_unit_die,
5916 abbrev_table_up *result_dwo_abbrev_table)
5918 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5919 dwarf2_per_cu_data *per_cu = cu->per_cu;
5920 struct objfile *objfile = per_objfile->objfile;
5921 bfd *abfd;
5922 const gdb_byte *begin_info_ptr, *info_ptr;
5923 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5924 int i,num_extra_attrs;
5925 struct dwarf2_section_info *dwo_abbrev_section;
5926 struct die_info *comp_unit_die;
5928 /* At most one of these may be provided. */
5929 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5931 /* These attributes aren't processed until later:
5932 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5933 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5934 referenced later. However, these attributes are found in the stub
5935 which we won't have later. In order to not impose this complication
5936 on the rest of the code, we read them here and copy them to the
5937 DWO CU/TU die. */
5939 stmt_list = NULL;
5940 low_pc = NULL;
5941 high_pc = NULL;
5942 ranges = NULL;
5943 comp_dir = NULL;
5945 if (stub_comp_unit_die != NULL)
5947 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5948 DWO file. */
5949 if (!per_cu->is_debug_types)
5950 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5951 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5952 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5953 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5954 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5956 cu->addr_base = stub_comp_unit_die->addr_base ();
5958 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5959 We need the value before we can process DW_AT_ranges values from the
5960 DWO. */
5961 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5963 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5964 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5965 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5966 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5967 section. */
5968 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5970 else if (stub_comp_dir != NULL)
5972 /* Reconstruct the comp_dir attribute to simplify the code below. */
5973 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5974 comp_dir->name = DW_AT_comp_dir;
5975 comp_dir->form = DW_FORM_string;
5976 comp_dir->set_string_noncanonical (stub_comp_dir);
5979 /* Set up for reading the DWO CU/TU. */
5980 cu->dwo_unit = dwo_unit;
5981 dwarf2_section_info *section = dwo_unit->section;
5982 section->read (objfile);
5983 abfd = section->get_bfd_owner ();
5984 begin_info_ptr = info_ptr = (section->buffer
5985 + to_underlying (dwo_unit->sect_off));
5986 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5988 if (per_cu->is_debug_types)
5990 signatured_type *sig_type = (struct signatured_type *) per_cu;
5992 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5993 section, dwo_abbrev_section,
5994 info_ptr, rcuh_kind::TYPE);
5995 /* This is not an assert because it can be caused by bad debug info. */
5996 if (sig_type->signature != cu->header.signature)
5998 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5999 " TU at offset %s [in module %s]"),
6000 hex_string (sig_type->signature),
6001 hex_string (cu->header.signature),
6002 sect_offset_str (dwo_unit->sect_off),
6003 bfd_get_filename (abfd));
6005 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6006 /* For DWOs coming from DWP files, we don't know the CU length
6007 nor the type's offset in the TU until now. */
6008 dwo_unit->length = cu->header.get_length_with_initial ();
6009 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6011 /* Establish the type offset that can be used to lookup the type.
6012 For DWO files, we don't know it until now. */
6013 sig_type->type_offset_in_section
6014 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6016 else
6018 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6019 section, dwo_abbrev_section,
6020 info_ptr, rcuh_kind::COMPILE);
6021 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6022 /* For DWOs coming from DWP files, we don't know the CU length
6023 until now. */
6024 dwo_unit->length = cu->header.get_length_with_initial ();
6027 dwo_abbrev_section->read (objfile);
6028 *result_dwo_abbrev_table
6029 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6030 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6031 result_dwo_abbrev_table->get ());
6033 /* Read in the die, but leave space to copy over the attributes
6034 from the stub. This has the benefit of simplifying the rest of
6035 the code - all the work to maintain the illusion of a single
6036 DW_TAG_{compile,type}_unit DIE is done here. */
6037 num_extra_attrs = ((stmt_list != NULL)
6038 + (low_pc != NULL)
6039 + (high_pc != NULL)
6040 + (ranges != NULL)
6041 + (comp_dir != NULL));
6042 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6043 num_extra_attrs);
6045 /* Copy over the attributes from the stub to the DIE we just read in. */
6046 comp_unit_die = *result_comp_unit_die;
6047 i = comp_unit_die->num_attrs;
6048 if (stmt_list != NULL)
6049 comp_unit_die->attrs[i++] = *stmt_list;
6050 if (low_pc != NULL)
6051 comp_unit_die->attrs[i++] = *low_pc;
6052 if (high_pc != NULL)
6053 comp_unit_die->attrs[i++] = *high_pc;
6054 if (ranges != NULL)
6055 comp_unit_die->attrs[i++] = *ranges;
6056 if (comp_dir != NULL)
6057 comp_unit_die->attrs[i++] = *comp_dir;
6058 comp_unit_die->num_attrs += num_extra_attrs;
6060 if (dwarf_die_debug)
6062 gdb_printf (gdb_stdlog,
6063 "Read die from %s@0x%x of %s:\n",
6064 section->get_name (),
6065 (unsigned) (begin_info_ptr - section->buffer),
6066 bfd_get_filename (abfd));
6067 dump_die (comp_unit_die, dwarf_die_debug);
6070 /* Skip dummy compilation units. */
6071 if (info_ptr >= begin_info_ptr + dwo_unit->length
6072 || peek_abbrev_code (abfd, info_ptr) == 0)
6073 return 0;
6075 *result_info_ptr = info_ptr;
6076 return 1;
6079 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6080 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6081 signature is part of the header. */
6082 static gdb::optional<ULONGEST>
6083 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6085 if (cu->header.version >= 5)
6086 return cu->header.signature;
6087 struct attribute *attr;
6088 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6089 if (attr == nullptr || !attr->form_is_unsigned ())
6090 return gdb::optional<ULONGEST> ();
6091 return attr->as_unsigned ();
6094 /* Subroutine of cutu_reader to simplify it.
6095 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6096 Returns NULL if the specified DWO unit cannot be found. */
6098 static struct dwo_unit *
6099 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6101 #if CXX_STD_THREAD
6102 /* We need a lock here both to handle the DWO hash table, and BFD,
6103 which is not thread-safe. */
6104 static std::mutex dwo_lock;
6106 std::lock_guard<std::mutex> guard (dwo_lock);
6107 #endif
6109 dwarf2_per_cu_data *per_cu = cu->per_cu;
6110 struct dwo_unit *dwo_unit;
6111 const char *comp_dir;
6113 gdb_assert (cu != NULL);
6115 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6116 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6117 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6119 if (per_cu->is_debug_types)
6120 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6121 else
6123 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6125 if (!signature.has_value ())
6126 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6127 " [in module %s]"),
6128 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6130 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6133 return dwo_unit;
6136 /* Subroutine of cutu_reader to simplify it.
6137 See it for a description of the parameters.
6138 Read a TU directly from a DWO file, bypassing the stub. */
6140 void
6141 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6142 dwarf2_per_objfile *per_objfile,
6143 dwarf2_cu *existing_cu)
6145 struct signatured_type *sig_type;
6147 /* Verify we can do the following downcast, and that we have the
6148 data we need. */
6149 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6150 sig_type = (struct signatured_type *) this_cu;
6151 gdb_assert (sig_type->dwo_unit != NULL);
6153 dwarf2_cu *cu;
6155 if (existing_cu != nullptr)
6157 cu = existing_cu;
6158 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6159 /* There's no need to do the rereading_dwo_cu handling that
6160 cutu_reader does since we don't read the stub. */
6162 else
6164 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6165 in per_objfile yet. */
6166 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6167 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6168 cu = m_new_cu.get ();
6171 /* A future optimization, if needed, would be to use an existing
6172 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6173 could share abbrev tables. */
6175 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6176 NULL /* stub_comp_unit_die */,
6177 sig_type->dwo_unit->dwo_file->comp_dir,
6178 this, &info_ptr,
6179 &comp_unit_die,
6180 &m_dwo_abbrev_table) == 0)
6182 /* Dummy die. */
6183 dummy_p = true;
6187 /* Initialize a CU (or TU) and read its DIEs.
6188 If the CU defers to a DWO file, read the DWO file as well.
6190 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6191 Otherwise the table specified in the comp unit header is read in and used.
6192 This is an optimization for when we already have the abbrev table.
6194 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6195 allocated. */
6197 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6198 dwarf2_per_objfile *per_objfile,
6199 struct abbrev_table *abbrev_table,
6200 dwarf2_cu *existing_cu,
6201 bool skip_partial,
6202 abbrev_cache *cache)
6203 : die_reader_specs {},
6204 m_this_cu (this_cu)
6206 struct objfile *objfile = per_objfile->objfile;
6207 struct dwarf2_section_info *section = this_cu->section;
6208 bfd *abfd = section->get_bfd_owner ();
6209 const gdb_byte *begin_info_ptr;
6210 struct signatured_type *sig_type = NULL;
6211 struct dwarf2_section_info *abbrev_section;
6212 /* Non-zero if CU currently points to a DWO file and we need to
6213 reread it. When this happens we need to reread the skeleton die
6214 before we can reread the DWO file (this only applies to CUs, not TUs). */
6215 int rereading_dwo_cu = 0;
6217 if (dwarf_die_debug)
6218 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6219 this_cu->is_debug_types ? "type" : "comp",
6220 sect_offset_str (this_cu->sect_off));
6222 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6223 file (instead of going through the stub), short-circuit all of this. */
6224 if (this_cu->reading_dwo_directly)
6226 /* Narrow down the scope of possibilities to have to understand. */
6227 gdb_assert (this_cu->is_debug_types);
6228 gdb_assert (abbrev_table == NULL);
6229 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6230 return;
6233 /* This is cheap if the section is already read in. */
6234 section->read (objfile);
6236 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6238 abbrev_section = get_abbrev_section_for_cu (this_cu);
6240 dwarf2_cu *cu;
6242 if (existing_cu != nullptr)
6244 cu = existing_cu;
6245 /* If this CU is from a DWO file we need to start over, we need to
6246 refetch the attributes from the skeleton CU.
6247 This could be optimized by retrieving those attributes from when we
6248 were here the first time: the previous comp_unit_die was stored in
6249 comp_unit_obstack. But there's no data yet that we need this
6250 optimization. */
6251 if (cu->dwo_unit != NULL)
6252 rereading_dwo_cu = 1;
6254 else
6256 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6257 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6258 let us know that the CU is being scanned using the parallel
6259 indexer. This assert is avoided in this case because (1) it
6260 is irrelevant, and (2) the get_cu method is not
6261 thread-safe. */
6262 gdb_assert (cache != nullptr
6263 || per_objfile->get_cu (this_cu) == nullptr);
6264 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6265 cu = m_new_cu.get ();
6268 /* Get the header. */
6269 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6271 /* We already have the header, there's no need to read it in again. */
6272 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6274 else
6276 if (this_cu->is_debug_types)
6278 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6279 section, abbrev_section,
6280 info_ptr, rcuh_kind::TYPE);
6282 /* Since per_cu is the first member of struct signatured_type,
6283 we can go from a pointer to one to a pointer to the other. */
6284 sig_type = (struct signatured_type *) this_cu;
6285 gdb_assert (sig_type->signature == cu->header.signature);
6286 gdb_assert (sig_type->type_offset_in_tu
6287 == cu->header.type_cu_offset_in_tu);
6288 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6290 /* LENGTH has not been set yet for type units if we're
6291 using .gdb_index. */
6292 this_cu->set_length (cu->header.get_length_with_initial ());
6294 /* Establish the type offset that can be used to lookup the type. */
6295 sig_type->type_offset_in_section =
6296 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6298 this_cu->set_version (cu->header.version);
6300 else
6302 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6303 section, abbrev_section,
6304 info_ptr,
6305 rcuh_kind::COMPILE);
6307 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6308 this_cu->set_length (cu->header.get_length_with_initial ());
6309 this_cu->set_version (cu->header.version);
6313 /* Skip dummy compilation units. */
6314 if (info_ptr >= begin_info_ptr + this_cu->length ()
6315 || peek_abbrev_code (abfd, info_ptr) == 0)
6317 dummy_p = true;
6318 return;
6321 /* If we don't have them yet, read the abbrevs for this compilation unit.
6322 And if we need to read them now, make sure they're freed when we're
6323 done. */
6324 if (abbrev_table != NULL)
6325 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6326 else
6328 if (cache != nullptr)
6329 abbrev_table = cache->find (abbrev_section,
6330 cu->header.abbrev_sect_off);
6331 if (abbrev_table == nullptr)
6333 abbrev_section->read (objfile);
6334 m_abbrev_table_holder
6335 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6336 abbrev_table = m_abbrev_table_holder.get ();
6340 /* Read the top level CU/TU die. */
6341 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6342 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6344 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6346 dummy_p = true;
6347 return;
6350 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6351 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6352 table from the DWO file and pass the ownership over to us. It will be
6353 referenced from READER, so we must make sure to free it after we're done
6354 with READER.
6356 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6357 DWO CU, that this test will fail (the attribute will not be present). */
6358 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6359 if (dwo_name != nullptr)
6361 struct dwo_unit *dwo_unit;
6362 struct die_info *dwo_comp_unit_die;
6364 if (comp_unit_die->has_children)
6366 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6367 " has children (offset %s) [in module %s]"),
6368 sect_offset_str (this_cu->sect_off),
6369 bfd_get_filename (abfd));
6371 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6372 if (dwo_unit != NULL)
6374 if (read_cutu_die_from_dwo (cu, dwo_unit,
6375 comp_unit_die, NULL,
6376 this, &info_ptr,
6377 &dwo_comp_unit_die,
6378 &m_dwo_abbrev_table) == 0)
6380 /* Dummy die. */
6381 dummy_p = true;
6382 return;
6384 comp_unit_die = dwo_comp_unit_die;
6386 else
6388 /* Yikes, we couldn't find the rest of the DIE, we only have
6389 the stub. A complaint has already been logged. There's
6390 not much more we can do except pass on the stub DIE to
6391 die_reader_func. We don't want to throw an error on bad
6392 debug info. */
6397 void
6398 cutu_reader::keep ()
6400 /* Done, clean up. */
6401 gdb_assert (!dummy_p);
6402 if (m_new_cu != NULL)
6404 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6405 now. */
6406 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6407 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
6411 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6412 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6413 assumed to have already done the lookup to find the DWO file).
6415 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6416 THIS_CU->is_debug_types, but nothing else.
6418 We fill in THIS_CU->length.
6420 THIS_CU->cu is always freed when done.
6421 This is done in order to not leave THIS_CU->cu in a state where we have
6422 to care whether it refers to the "main" CU or the DWO CU.
6424 When parent_cu is passed, it is used to provide a default value for
6425 str_offsets_base and addr_base from the parent. */
6427 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6428 dwarf2_per_objfile *per_objfile,
6429 struct dwarf2_cu *parent_cu,
6430 struct dwo_file *dwo_file)
6431 : die_reader_specs {},
6432 m_this_cu (this_cu)
6434 struct objfile *objfile = per_objfile->objfile;
6435 struct dwarf2_section_info *section = this_cu->section;
6436 bfd *abfd = section->get_bfd_owner ();
6437 struct dwarf2_section_info *abbrev_section;
6438 const gdb_byte *begin_info_ptr, *info_ptr;
6440 if (dwarf_die_debug)
6441 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6442 this_cu->is_debug_types ? "type" : "comp",
6443 sect_offset_str (this_cu->sect_off));
6445 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6447 abbrev_section = (dwo_file != NULL
6448 ? &dwo_file->sections.abbrev
6449 : get_abbrev_section_for_cu (this_cu));
6451 /* This is cheap if the section is already read in. */
6452 section->read (objfile);
6454 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6456 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6457 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6458 section, abbrev_section, info_ptr,
6459 (this_cu->is_debug_types
6460 ? rcuh_kind::TYPE
6461 : rcuh_kind::COMPILE));
6463 if (parent_cu != nullptr)
6465 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6466 m_new_cu->addr_base = parent_cu->addr_base;
6468 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
6470 /* Skip dummy compilation units. */
6471 if (info_ptr >= begin_info_ptr + this_cu->length ()
6472 || peek_abbrev_code (abfd, info_ptr) == 0)
6474 dummy_p = true;
6475 return;
6478 abbrev_section->read (objfile);
6479 m_abbrev_table_holder
6480 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6482 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6483 m_abbrev_table_holder.get ());
6484 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6488 /* Type Unit Groups.
6490 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6491 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6492 so that all types coming from the same compilation (.o file) are grouped
6493 together. A future step could be to put the types in the same symtab as
6494 the CU the types ultimately came from. */
6496 static hashval_t
6497 hash_type_unit_group (const void *item)
6499 const struct type_unit_group *tu_group
6500 = (const struct type_unit_group *) item;
6502 return hash_stmt_list_entry (&tu_group->hash);
6505 static int
6506 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6508 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6509 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6511 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6514 /* Allocate a hash table for type unit groups. */
6516 static htab_up
6517 allocate_type_unit_groups_table ()
6519 return htab_up (htab_create_alloc (3,
6520 hash_type_unit_group,
6521 eq_type_unit_group,
6522 htab_delete_entry<type_unit_group>,
6523 xcalloc, xfree));
6526 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6527 partial symtabs. We combine several TUs per psymtab to not let the size
6528 of any one psymtab grow too big. */
6529 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6530 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6532 /* Helper routine for get_type_unit_group.
6533 Create the type_unit_group object used to hold one or more TUs. */
6535 static std::unique_ptr<type_unit_group>
6536 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6538 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6540 tu_group->hash.dwo_unit = cu->dwo_unit;
6541 tu_group->hash.line_sect_off = line_offset_struct;
6543 return tu_group;
6546 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6547 STMT_LIST is a DW_AT_stmt_list attribute. */
6549 static struct type_unit_group *
6550 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6552 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6553 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6554 struct type_unit_group *tu_group;
6555 void **slot;
6556 unsigned int line_offset;
6557 struct type_unit_group type_unit_group_for_lookup;
6559 if (per_objfile->per_bfd->type_unit_groups == NULL)
6560 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6562 /* Do we need to create a new group, or can we use an existing one? */
6564 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6566 line_offset = stmt_list->as_unsigned ();
6567 ++tu_stats->nr_symtab_sharers;
6569 else
6571 /* Ugh, no stmt_list. Rare, but we have to handle it.
6572 We can do various things here like create one group per TU or
6573 spread them over multiple groups to split up the expansion work.
6574 To avoid worst case scenarios (too many groups or too large groups)
6575 we, umm, group them in bunches. */
6576 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6577 | (tu_stats->nr_stmt_less_type_units
6578 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6579 ++tu_stats->nr_stmt_less_type_units;
6582 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6583 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6584 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6585 &type_unit_group_for_lookup, INSERT);
6586 if (*slot == nullptr)
6588 sect_offset line_offset_struct = (sect_offset) line_offset;
6589 std::unique_ptr<type_unit_group> grp
6590 = create_type_unit_group (cu, line_offset_struct);
6591 *slot = grp.release ();
6592 ++tu_stats->nr_symtabs;
6595 tu_group = (struct type_unit_group *) *slot;
6596 gdb_assert (tu_group != nullptr);
6597 return tu_group;
6601 /* An instance of this is created when scanning DWARF to create a
6602 cooked index. */
6604 class cooked_index_storage
6606 public:
6608 cooked_index_storage ()
6609 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6610 eq_cutu_reader,
6611 htab_delete_entry<cutu_reader>,
6612 xcalloc, xfree)),
6613 m_index (new cooked_index)
6617 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6619 /* Return the current abbrev cache. */
6620 abbrev_cache *get_abbrev_cache ()
6622 return &m_abbrev_cache;
6625 /* Return the DIE reader corresponding to PER_CU. If no such reader
6626 has been registered, return NULL. */
6627 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6629 int index = per_cu->index;
6630 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6631 &index, index);
6634 /* Preserve READER by storing it in the local hash table. */
6635 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6637 m_abbrev_cache.add (reader->release_abbrev_table ());
6639 int index = reader->cu->per_cu->index;
6640 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6641 index, INSERT);
6642 gdb_assert (*slot == nullptr);
6643 cutu_reader *result = reader.get ();
6644 *slot = reader.release ();
6645 return result;
6648 /* Add an entry to the index. The arguments describe the entry; see
6649 cooked-index.h. The new entry is returned. */
6650 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6651 cooked_index_flag flags,
6652 const char *name,
6653 const cooked_index_entry *parent_entry,
6654 dwarf2_per_cu_data *per_cu)
6656 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6659 /* Install the current addrmap into the index being constructed,
6660 then transfer ownership of the index to the caller. */
6661 std::unique_ptr<cooked_index> release ()
6663 m_index->install_addrmap (&m_addrmap);
6664 return std::move (m_index);
6667 /* Return the mutable addrmap that is currently being created. */
6668 addrmap_mutable *get_addrmap ()
6670 return &m_addrmap;
6673 private:
6675 /* Hash function for a cutu_reader. */
6676 static hashval_t hash_cutu_reader (const void *a)
6678 const cutu_reader *reader = (const cutu_reader *) a;
6679 return reader->cu->per_cu->index;
6682 /* Equality function for cutu_reader. */
6683 static int eq_cutu_reader (const void *a, const void *b)
6685 const cutu_reader *ra = (const cutu_reader *) a;
6686 const int *rb = (const int *) b;
6687 return ra->cu->per_cu->index == *rb;
6690 /* The abbrev cache used by this indexer. */
6691 abbrev_cache m_abbrev_cache;
6692 /* A hash table of cutu_reader objects. */
6693 htab_up m_reader_hash;
6694 /* The index that is being constructed. */
6695 std::unique_ptr<cooked_index> m_index;
6697 /* A writeable addrmap being constructed by this scanner. */
6698 addrmap_mutable m_addrmap;
6701 /* An instance of this is created to index a CU. */
6703 class cooked_indexer
6705 public:
6707 cooked_indexer (cooked_index_storage *storage,
6708 dwarf2_per_cu_data *per_cu,
6709 enum language language)
6710 : m_index_storage (storage),
6711 m_per_cu (per_cu),
6712 m_language (language)
6716 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6718 /* Index the given CU. */
6719 void make_index (cutu_reader *reader);
6721 private:
6723 /* A helper function to turn a section offset into an address that
6724 can be used in an addrmap. */
6725 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6727 CORE_ADDR value = to_underlying (offset);
6728 if (is_dwz)
6729 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6730 return value;
6733 /* A helper function to scan the PC bounds of READER and record them
6734 in the storage's addrmap. */
6735 void check_bounds (cutu_reader *reader);
6737 /* Ensure that the indicated CU exists. The cutu_reader for it is
6738 returned. FOR_SCANNING is true if the caller intends to scan all
6739 the DIEs in the CU; when false, this use is assumed to be to look
6740 up just a single DIE. */
6741 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6742 dwarf2_per_objfile *per_objfile,
6743 sect_offset sect_off,
6744 bool is_dwz,
6745 bool for_scanning);
6747 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6748 the entry for the enclosing scope (nullptr at top level). FULLY
6749 is true when a full scan must be done -- in some languages,
6750 function scopes must be fully explored in order to find nested
6751 functions. This returns a pointer to just after the spot where
6752 reading stopped. */
6753 const gdb_byte *index_dies (cutu_reader *reader,
6754 const gdb_byte *info_ptr,
6755 const cooked_index_entry *parent_entry,
6756 bool fully);
6758 /* Scan the attributes for a given DIE and update the out
6759 parameters. Returns a pointer to the byte after the DIE. */
6760 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6761 cutu_reader *reader,
6762 const gdb_byte *watermark_ptr,
6763 const gdb_byte *info_ptr,
6764 const abbrev_info *abbrev,
6765 const char **name,
6766 const char **linkage_name,
6767 cooked_index_flag *flags,
6768 sect_offset *sibling_offset,
6769 const cooked_index_entry **parent_entry,
6770 CORE_ADDR *maybe_defer,
6771 bool for_specification);
6773 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6774 DW_AT_import, and then scanning the referenced CU. Returns a
6775 pointer to the byte after the DIE. */
6776 const gdb_byte *index_imported_unit (cutu_reader *reader,
6777 const gdb_byte *info_ptr,
6778 const abbrev_info *abbrev);
6780 /* Recursively read DIEs, recording the section offsets in
6781 m_die_range_map and then calling index_dies. */
6782 const gdb_byte *recurse (cutu_reader *reader,
6783 const gdb_byte *info_ptr,
6784 const cooked_index_entry *parent_entry,
6785 bool fully);
6787 /* The storage object, where the results are kept. */
6788 cooked_index_storage *m_index_storage;
6789 /* The CU that we are reading on behalf of. This object might be
6790 asked to index one CU but to treat the results as if they come
6791 from some including CU; in this case the including CU would be
6792 recorded here. */
6793 dwarf2_per_cu_data *m_per_cu;
6794 /* The language that we're assuming when reading. */
6795 enum language m_language;
6797 /* An addrmap that maps from section offsets (see the form_addr
6798 method) to newly-created entries. See m_deferred_entries to
6799 understand this. */
6800 addrmap_mutable m_die_range_map;
6802 /* A single deferred entry. */
6803 struct deferred_entry
6805 sect_offset die_offset;
6806 const char *name;
6807 CORE_ADDR spec_offset;
6808 dwarf_tag tag;
6809 cooked_index_flag flags;
6812 /* The generated DWARF can sometimes have the declaration for a
6813 method in a class (or perhaps namespace) scope, with the
6814 definition appearing outside this scope... just one of the many
6815 bad things about DWARF. In order to handle this situation, we
6816 defer certain entries until the end of scanning, at which point
6817 we'll know the containing context of all the DIEs that we might
6818 have scanned. This vector stores these deferred entries. */
6819 std::vector<deferred_entry> m_deferred_entries;
6822 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6823 Process compilation unit THIS_CU for a psymtab. */
6825 static void
6826 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6827 dwarf2_per_objfile *per_objfile,
6828 cooked_index_storage *storage)
6830 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6831 storage->get_abbrev_cache ());
6833 if (reader.comp_unit_die == nullptr)
6834 return;
6836 if (reader.dummy_p)
6838 /* Nothing. */
6840 else if (this_cu->is_debug_types)
6841 build_type_psymtabs_reader (&reader, storage);
6842 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6844 bool nope = false;
6845 if (this_cu->scanned.compare_exchange_strong (nope, true))
6847 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6848 language_minimal);
6849 gdb_assert (storage != nullptr);
6850 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
6851 indexer.make_index (&reader);
6856 /* Reader function for build_type_psymtabs. */
6858 static void
6859 build_type_psymtabs_reader (cutu_reader *reader,
6860 cooked_index_storage *storage)
6862 struct dwarf2_cu *cu = reader->cu;
6863 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6864 struct die_info *type_unit_die = reader->comp_unit_die;
6866 gdb_assert (per_cu->is_debug_types);
6868 if (! type_unit_die->has_children)
6869 return;
6871 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6873 gdb_assert (storage != nullptr);
6874 cooked_indexer indexer (storage, per_cu, cu->lang ());
6875 indexer.make_index (reader);
6878 /* Struct used to sort TUs by their abbreviation table offset. */
6880 struct tu_abbrev_offset
6882 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6883 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6886 /* This is used when sorting. */
6887 bool operator< (const tu_abbrev_offset &other) const
6889 return abbrev_offset < other.abbrev_offset;
6892 signatured_type *sig_type;
6893 sect_offset abbrev_offset;
6896 /* Efficiently read all the type units.
6898 The efficiency is because we sort TUs by the abbrev table they use and
6899 only read each abbrev table once. In one program there are 200K TUs
6900 sharing 8K abbrev tables.
6902 The main purpose of this function is to support building the
6903 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6904 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6905 can collapse the search space by grouping them by stmt_list.
6906 The savings can be significant, in the same program from above the 200K TUs
6907 share 8K stmt_list tables.
6909 FUNC is expected to call get_type_unit_group, which will create the
6910 struct type_unit_group if necessary and add it to
6911 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6913 static void
6914 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6915 cooked_index_storage *storage)
6917 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6918 abbrev_table_up abbrev_table;
6919 sect_offset abbrev_offset;
6921 /* It's up to the caller to not call us multiple times. */
6922 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6924 if (per_objfile->per_bfd->all_type_units.size () == 0)
6925 return;
6927 /* TUs typically share abbrev tables, and there can be way more TUs than
6928 abbrev tables. Sort by abbrev table to reduce the number of times we
6929 read each abbrev table in.
6930 Alternatives are to punt or to maintain a cache of abbrev tables.
6931 This is simpler and efficient enough for now.
6933 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6934 symtab to use). Typically TUs with the same abbrev offset have the same
6935 stmt_list value too so in practice this should work well.
6937 The basic algorithm here is:
6939 sort TUs by abbrev table
6940 for each TU with same abbrev table:
6941 read abbrev table if first user
6942 read TU top level DIE
6943 [IWBN if DWO skeletons had DW_AT_stmt_list]
6944 call FUNC */
6946 dwarf_read_debug_printf ("Building type unit groups ...");
6948 /* Sort in a separate table to maintain the order of all_units
6949 for .gdb_index: TU indices directly index all_type_units. */
6950 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6951 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
6953 for (const auto &cu : per_objfile->per_bfd->all_units)
6955 if (cu->is_debug_types)
6957 auto sig_type = static_cast<signatured_type *> (cu.get ());
6958 sorted_by_abbrev.emplace_back
6959 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6960 sig_type->sect_off));
6964 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6966 abbrev_offset = (sect_offset) ~(unsigned) 0;
6968 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6970 /* Switch to the next abbrev table if necessary. */
6971 if (abbrev_table == NULL
6972 || tu.abbrev_offset != abbrev_offset)
6974 abbrev_offset = tu.abbrev_offset;
6975 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6976 abbrev_table =
6977 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6978 ++tu_stats->nr_uniq_abbrev_tables;
6981 cutu_reader reader (tu.sig_type, per_objfile,
6982 abbrev_table.get (), nullptr, false);
6983 if (!reader.dummy_p)
6984 build_type_psymtabs_reader (&reader, storage);
6988 /* Print collected type unit statistics. */
6990 static void
6991 print_tu_stats (dwarf2_per_objfile *per_objfile)
6993 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6995 dwarf_read_debug_printf ("Type unit statistics:");
6996 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6997 dwarf_read_debug_printf (" %d uniq abbrev tables",
6998 tu_stats->nr_uniq_abbrev_tables);
6999 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7000 tu_stats->nr_symtabs);
7001 dwarf_read_debug_printf (" %d symtab sharers",
7002 tu_stats->nr_symtab_sharers);
7003 dwarf_read_debug_printf (" %d type units without a stmt_list",
7004 tu_stats->nr_stmt_less_type_units);
7005 dwarf_read_debug_printf (" %d all_type_units reallocs",
7006 tu_stats->nr_all_type_units_reallocs);
7009 struct skeleton_data
7011 dwarf2_per_objfile *per_objfile;
7012 cooked_index_storage *storage;
7015 /* Traversal function for process_skeletonless_type_unit.
7016 Read a TU in a DWO file and build partial symbols for it. */
7018 static int
7019 process_skeletonless_type_unit (void **slot, void *info)
7021 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7022 skeleton_data *data = (skeleton_data *) info;
7024 /* If this TU doesn't exist in the global table, add it and read it in. */
7026 if (data->per_objfile->per_bfd->signatured_types == NULL)
7027 data->per_objfile->per_bfd->signatured_types
7028 = allocate_signatured_type_table ();
7030 signatured_type find_entry (dwo_unit->signature);
7031 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
7032 &find_entry, INSERT);
7033 /* If we've already seen this type there's nothing to do. What's happening
7034 is we're doing our own version of comdat-folding here. */
7035 if (*slot != NULL)
7036 return 1;
7038 /* This does the job that create_all_units would have done for
7039 this TU. */
7040 signatured_type *entry
7041 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7042 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7043 *slot = entry;
7045 /* This does the job that build_type_psymtabs would have done. */
7046 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7047 if (!reader.dummy_p)
7048 build_type_psymtabs_reader (&reader, data->storage);
7050 return 1;
7053 /* Traversal function for process_skeletonless_type_units. */
7055 static int
7056 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7058 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7060 if (dwo_file->tus != NULL)
7061 htab_traverse_noresize (dwo_file->tus.get (),
7062 process_skeletonless_type_unit, info);
7064 return 1;
7067 /* Scan all TUs of DWO files, verifying we've processed them.
7068 This is needed in case a TU was emitted without its skeleton.
7069 Note: This can't be done until we know what all the DWO files are. */
7071 static void
7072 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7073 cooked_index_storage *storage)
7075 skeleton_data data { per_objfile, storage };
7077 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7078 if (get_dwp_file (per_objfile) == NULL
7079 && per_objfile->per_bfd->dwo_files != NULL)
7081 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7082 process_dwo_file_for_skeletonless_type_units,
7083 &data);
7087 /* Build the partial symbol table by doing a quick pass through the
7088 .debug_info and .debug_abbrev sections. */
7090 static void
7091 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7093 struct objfile *objfile = per_objfile->objfile;
7094 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7096 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7097 objfile_name (objfile));
7099 per_bfd->map_info_sections (objfile);
7101 cooked_index_storage index_storage;
7102 create_all_units (per_objfile);
7103 build_type_psymtabs (per_objfile, &index_storage);
7104 std::vector<std::unique_ptr<cooked_index>> indexes;
7106 per_bfd->quick_file_names_table
7107 = create_quick_file_names_table (per_bfd->all_units.size ());
7108 if (!per_bfd->debug_aranges.empty ())
7109 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7110 index_storage.get_addrmap ());
7113 /* Ensure that complaints are handled correctly. */
7114 complaint_interceptor complaint_handler;
7116 using iter_type = decltype (per_bfd->all_units.begin ());
7118 auto task_size_ = [] (iter_type iter)
7120 dwarf2_per_cu_data *per_cu = iter->get ();
7121 return (size_t)per_cu->length ();
7123 auto task_size = gdb::make_function_view (task_size_);
7125 /* Each thread returns a pair holding a cooked index, and a vector
7126 of errors that should be printed. The latter is done because
7127 GDB's I/O system is not thread-safe. run_on_main_thread could be
7128 used, but that would mean the messages are printed after the
7129 prompt, which looks weird. */
7130 using result_type = std::pair<std::unique_ptr<cooked_index>,
7131 std::vector<gdb_exception>>;
7132 std::vector<result_type> results
7133 = gdb::parallel_for_each (1, per_bfd->all_units.begin (),
7134 per_bfd->all_units.end (),
7135 [=] (iter_type iter, iter_type end)
7137 std::vector<gdb_exception> errors;
7138 cooked_index_storage thread_storage;
7139 for (; iter != end; ++iter)
7141 dwarf2_per_cu_data *per_cu = iter->get ();
7144 process_psymtab_comp_unit (per_cu, per_objfile,
7145 &thread_storage);
7147 catch (gdb_exception &except)
7149 errors.push_back (std::move (except));
7152 return result_type (thread_storage.release (), std::move (errors));
7153 }, task_size);
7155 /* Only show a given exception a single time. */
7156 std::unordered_set<gdb_exception> seen_exceptions;
7157 for (auto &one_result : results)
7159 indexes.push_back (std::move (one_result.first));
7160 for (auto &one_exc : one_result.second)
7161 if (seen_exceptions.insert (one_exc).second)
7162 exception_print (gdb_stderr, one_exc);
7166 /* This has to wait until we read the CUs, we need the list of DWOs. */
7167 process_skeletonless_type_units (per_objfile, &index_storage);
7169 if (dwarf_read_debug > 0)
7170 print_tu_stats (per_objfile);
7172 indexes.push_back (index_storage.release ());
7173 /* Remove any NULL entries. This might happen if parallel-for
7174 decides to throttle the number of threads that were used. */
7175 indexes.erase
7176 (std::remove_if (indexes.begin (),
7177 indexes.end (),
7178 [] (const std::unique_ptr<cooked_index> &entry)
7180 return entry == nullptr;
7182 indexes.end ());
7183 indexes.shrink_to_fit ();
7185 cooked_index_vector *vec = new cooked_index_vector (std::move (indexes));
7186 per_bfd->index_table.reset (vec);
7188 const cooked_index_entry *main_entry = vec->get_main ();
7189 if (main_entry != nullptr)
7190 set_objfile_main_name (objfile, main_entry->name,
7191 main_entry->per_cu->lang ());
7193 dwarf_read_debug_printf ("Done building psymtabs of %s",
7194 objfile_name (objfile));
7197 static void
7198 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7199 struct dwarf2_section_info *section,
7200 struct dwarf2_section_info *abbrev_section,
7201 unsigned int is_dwz,
7202 htab_up &types_htab,
7203 rcuh_kind section_kind)
7205 const gdb_byte *info_ptr;
7206 struct objfile *objfile = per_objfile->objfile;
7208 dwarf_read_debug_printf ("Reading %s for %s",
7209 section->get_name (),
7210 section->get_file_name ());
7212 section->read (objfile);
7214 info_ptr = section->buffer;
7216 while (info_ptr < section->buffer + section->size)
7218 dwarf2_per_cu_data_up this_cu;
7220 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7222 comp_unit_head cu_header;
7223 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7224 abbrev_section, info_ptr,
7225 section_kind);
7227 /* Save the compilation unit for later lookup. */
7228 if (cu_header.unit_type != DW_UT_type)
7229 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7230 else
7232 if (types_htab == nullptr)
7233 types_htab = allocate_signatured_type_table ();
7235 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7236 (cu_header.signature);
7237 signatured_type *sig_ptr = sig_type.get ();
7238 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7239 this_cu.reset (sig_type.release ());
7241 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7242 gdb_assert (slot != nullptr);
7243 if (*slot != nullptr)
7244 complaint (_("debug type entry at offset %s is duplicate to"
7245 " the entry at offset %s, signature %s"),
7246 sect_offset_str (sect_off),
7247 sect_offset_str (sig_ptr->sect_off),
7248 hex_string (sig_ptr->signature));
7249 *slot = sig_ptr;
7251 this_cu->sect_off = sect_off;
7252 this_cu->set_length (cu_header.get_length_with_initial ());
7253 this_cu->is_dwz = is_dwz;
7254 this_cu->section = section;
7255 /* Init this asap, to avoid a data race in the set_version in
7256 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7257 index case). */
7258 this_cu->set_version (cu_header.version);
7260 info_ptr = info_ptr + this_cu->length ();
7261 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
7265 /* Initialize the views on all_units. */
7267 static void
7268 finalize_all_units (dwarf2_per_bfd *per_bfd)
7270 size_t nr_tus = per_bfd->tu_stats.nr_tus;
7271 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
7272 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
7273 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
7274 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
7277 /* Create a list of all compilation units in OBJFILE.
7278 This is only done for -readnow and building partial symtabs. */
7280 static void
7281 create_all_units (dwarf2_per_objfile *per_objfile)
7283 htab_up types_htab;
7284 gdb_assert (per_objfile->per_bfd->all_units.empty ());
7286 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7287 &per_objfile->per_bfd->abbrev, 0,
7288 types_htab, rcuh_kind::COMPILE);
7289 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7290 read_comp_units_from_section (per_objfile, &section,
7291 &per_objfile->per_bfd->abbrev, 0,
7292 types_htab, rcuh_kind::TYPE);
7294 dwz_file *dwz;
7297 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7299 catch (const gdb_exception_error &)
7301 per_objfile->per_bfd->all_units.clear ();
7302 throw;
7304 if (dwz != NULL)
7306 /* Pre-read the sections we'll need to construct an index. */
7307 struct objfile *objfile = per_objfile->objfile;
7308 dwz->abbrev.read (objfile);
7309 dwz->info.read (objfile);
7310 dwz->str.read (objfile);
7311 dwz->line.read (objfile);
7312 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7313 types_htab, rcuh_kind::COMPILE);
7316 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7318 finalize_all_units (per_objfile->per_bfd);
7321 /* Return the initial uleb128 in the die at INFO_PTR. */
7323 static unsigned int
7324 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7326 unsigned int bytes_read;
7328 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7331 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7332 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7334 Return the corresponding abbrev, or NULL if the number is zero (indicating
7335 an empty DIE). In either case *BYTES_READ will be set to the length of
7336 the initial number. */
7338 static const struct abbrev_info *
7339 peek_die_abbrev (const die_reader_specs &reader,
7340 const gdb_byte *info_ptr, unsigned int *bytes_read)
7342 dwarf2_cu *cu = reader.cu;
7343 bfd *abfd = reader.abfd;
7344 unsigned int abbrev_number
7345 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7347 if (abbrev_number == 0)
7348 return NULL;
7350 const abbrev_info *abbrev
7351 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7352 if (!abbrev)
7354 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7355 " at offset %s [in module %s]"),
7356 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7357 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7360 return abbrev;
7363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7364 Returns a pointer to the end of a series of DIEs, terminated by an empty
7365 DIE. Any children of the skipped DIEs will also be skipped. */
7367 static const gdb_byte *
7368 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7370 while (1)
7372 unsigned int bytes_read;
7373 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7374 &bytes_read);
7376 if (abbrev == NULL)
7377 return info_ptr + bytes_read;
7378 else
7379 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7383 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7384 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7385 abbrev corresponding to that skipped uleb128 should be passed in
7386 ABBREV.
7388 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7389 returns a pointer to this DIE's sibling, skipping any children.
7390 Otherwise, returns a pointer to the DIE's first child. */
7392 static const gdb_byte *
7393 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7394 const struct abbrev_info *abbrev, bool do_skip_children)
7396 unsigned int bytes_read;
7397 struct attribute attr;
7398 bfd *abfd = reader->abfd;
7399 struct dwarf2_cu *cu = reader->cu;
7400 const gdb_byte *buffer = reader->buffer;
7401 const gdb_byte *buffer_end = reader->buffer_end;
7402 unsigned int form, i;
7404 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7406 /* We only handle DW_FORM_ref4 here. */
7407 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7408 unsigned int offset = read_4_bytes (abfd, sibling_data);
7409 const gdb_byte *sibling_ptr
7410 = buffer + to_underlying (cu->header.sect_off) + offset;
7411 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7412 return sibling_ptr;
7413 /* Fall through to the slow way. */
7415 else if (abbrev->size_if_constant != 0)
7417 info_ptr += abbrev->size_if_constant;
7418 if (do_skip_children && abbrev->has_children)
7419 return skip_children (reader, info_ptr);
7420 return info_ptr;
7423 for (i = 0; i < abbrev->num_attrs; i++)
7425 /* The only abbrev we care about is DW_AT_sibling. */
7426 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7428 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7429 if (attr.form == DW_FORM_ref_addr)
7430 complaint (_("ignoring absolute DW_AT_sibling"));
7431 else
7433 sect_offset off = attr.get_ref_die_offset ();
7434 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7436 if (sibling_ptr < info_ptr)
7437 complaint (_("DW_AT_sibling points backwards"));
7438 else if (sibling_ptr > reader->buffer_end)
7439 reader->die_section->overflow_complaint ();
7440 else
7441 return sibling_ptr;
7445 /* If it isn't DW_AT_sibling, skip this attribute. */
7446 form = abbrev->attrs[i].form;
7447 skip_attribute:
7448 switch (form)
7450 case DW_FORM_ref_addr:
7451 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7452 and later it is offset sized. */
7453 if (cu->header.version == 2)
7454 info_ptr += cu->header.addr_size;
7455 else
7456 info_ptr += cu->header.offset_size;
7457 break;
7458 case DW_FORM_GNU_ref_alt:
7459 info_ptr += cu->header.offset_size;
7460 break;
7461 case DW_FORM_addr:
7462 info_ptr += cu->header.addr_size;
7463 break;
7464 case DW_FORM_data1:
7465 case DW_FORM_ref1:
7466 case DW_FORM_flag:
7467 case DW_FORM_strx1:
7468 info_ptr += 1;
7469 break;
7470 case DW_FORM_flag_present:
7471 case DW_FORM_implicit_const:
7472 break;
7473 case DW_FORM_data2:
7474 case DW_FORM_ref2:
7475 case DW_FORM_strx2:
7476 info_ptr += 2;
7477 break;
7478 case DW_FORM_strx3:
7479 info_ptr += 3;
7480 break;
7481 case DW_FORM_data4:
7482 case DW_FORM_ref4:
7483 case DW_FORM_strx4:
7484 info_ptr += 4;
7485 break;
7486 case DW_FORM_data8:
7487 case DW_FORM_ref8:
7488 case DW_FORM_ref_sig8:
7489 info_ptr += 8;
7490 break;
7491 case DW_FORM_data16:
7492 info_ptr += 16;
7493 break;
7494 case DW_FORM_string:
7495 read_direct_string (abfd, info_ptr, &bytes_read);
7496 info_ptr += bytes_read;
7497 break;
7498 case DW_FORM_sec_offset:
7499 case DW_FORM_strp:
7500 case DW_FORM_GNU_strp_alt:
7501 info_ptr += cu->header.offset_size;
7502 break;
7503 case DW_FORM_exprloc:
7504 case DW_FORM_block:
7505 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7506 info_ptr += bytes_read;
7507 break;
7508 case DW_FORM_block1:
7509 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7510 break;
7511 case DW_FORM_block2:
7512 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7513 break;
7514 case DW_FORM_block4:
7515 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7516 break;
7517 case DW_FORM_addrx:
7518 case DW_FORM_strx:
7519 case DW_FORM_sdata:
7520 case DW_FORM_udata:
7521 case DW_FORM_ref_udata:
7522 case DW_FORM_GNU_addr_index:
7523 case DW_FORM_GNU_str_index:
7524 case DW_FORM_rnglistx:
7525 case DW_FORM_loclistx:
7526 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7527 break;
7528 case DW_FORM_indirect:
7529 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7530 info_ptr += bytes_read;
7531 /* We need to continue parsing from here, so just go back to
7532 the top. */
7533 goto skip_attribute;
7535 default:
7536 error (_("Dwarf Error: Cannot handle %s "
7537 "in DWARF reader [in module %s]"),
7538 dwarf_form_name (form),
7539 bfd_get_filename (abfd));
7543 if (do_skip_children && abbrev->has_children)
7544 return skip_children (reader, info_ptr);
7545 else
7546 return info_ptr;
7549 /* Reading in full CUs. */
7551 /* Add PER_CU to the queue. */
7553 static void
7554 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7555 dwarf2_per_objfile *per_objfile,
7556 enum language pretend_language)
7558 per_cu->queued = 1;
7560 gdb_assert (per_objfile->queue.has_value ());
7561 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
7564 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7566 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7567 dependency.
7569 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7570 DIEs, false otherwise.
7572 Explanation: there is an invariant that if a CU is queued for expansion
7573 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7574 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7575 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7576 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7577 invariant is respected.
7579 The caller is therefore not required to load the CU's DIEs (we return false)
7582 - the CU is already expanded, and therefore does not get enqueued
7583 - the CU gets enqueued for expansion, but its DIEs are already loaded
7585 Note that the caller should not use this function's return value as an
7586 indicator of whether the CU's DIEs are loaded right now, it should check
7587 that by calling `dwarf2_per_objfile::get_cu` instead. */
7589 static int
7590 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7591 dwarf2_per_cu_data *per_cu,
7592 dwarf2_per_objfile *per_objfile,
7593 enum language pretend_language)
7595 /* Mark the dependence relation so that we don't flush PER_CU
7596 too early. */
7597 if (dependent_cu != NULL)
7598 dependent_cu->add_dependence (per_cu);
7600 /* If it's already on the queue, we have nothing to do. */
7601 if (per_cu->queued)
7603 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7604 loaded. */
7605 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7607 /* If the CU is queued for expansion, it should not already be
7608 expanded. */
7609 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7611 /* The DIEs are already loaded, the caller doesn't need to do it. */
7612 return 0;
7615 bool queued = false;
7616 if (!per_objfile->symtab_set_p (per_cu))
7618 /* Add it to the queue. */
7619 queue_comp_unit (per_cu, per_objfile, pretend_language);
7620 queued = true;
7623 /* If the compilation unit is already loaded, just mark it as
7624 used. */
7625 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7626 if (cu != nullptr)
7627 cu->last_used = 0;
7629 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7630 and the DIEs are not already loaded. */
7631 return queued && cu == nullptr;
7634 /* Process the queue. */
7636 static void
7637 process_queue (dwarf2_per_objfile *per_objfile)
7639 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7640 objfile_name (per_objfile->objfile));
7642 /* The queue starts out with one item, but following a DIE reference
7643 may load a new CU, adding it to the end of the queue. */
7644 while (!per_objfile->queue->empty ())
7646 dwarf2_queue_item &item = per_objfile->queue->front ();
7647 dwarf2_per_cu_data *per_cu = item.per_cu;
7649 if (!per_objfile->symtab_set_p (per_cu))
7651 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7653 /* Skip dummy CUs. */
7654 if (cu != nullptr)
7656 unsigned int debug_print_threshold;
7657 char buf[100];
7659 if (per_cu->is_debug_types)
7661 struct signatured_type *sig_type =
7662 (struct signatured_type *) per_cu;
7664 sprintf (buf, "TU %s at offset %s",
7665 hex_string (sig_type->signature),
7666 sect_offset_str (per_cu->sect_off));
7667 /* There can be 100s of TUs.
7668 Only print them in verbose mode. */
7669 debug_print_threshold = 2;
7671 else
7673 sprintf (buf, "CU at offset %s",
7674 sect_offset_str (per_cu->sect_off));
7675 debug_print_threshold = 1;
7678 if (dwarf_read_debug >= debug_print_threshold)
7679 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7681 if (per_cu->is_debug_types)
7682 process_full_type_unit (cu, item.pretend_language);
7683 else
7684 process_full_comp_unit (cu, item.pretend_language);
7686 if (dwarf_read_debug >= debug_print_threshold)
7687 dwarf_read_debug_printf ("Done expanding %s", buf);
7691 per_cu->queued = 0;
7692 per_objfile->queue->pop ();
7695 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7696 objfile_name (per_objfile->objfile));
7699 /* Trivial hash function for die_info: the hash value of a DIE
7700 is its offset in .debug_info for this objfile. */
7702 static hashval_t
7703 die_hash (const void *item)
7705 const struct die_info *die = (const struct die_info *) item;
7707 return to_underlying (die->sect_off);
7710 /* Trivial comparison function for die_info structures: two DIEs
7711 are equal if they have the same offset. */
7713 static int
7714 die_eq (const void *item_lhs, const void *item_rhs)
7716 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7717 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7719 return die_lhs->sect_off == die_rhs->sect_off;
7722 /* Load the DIEs associated with PER_CU into memory.
7724 In some cases, the caller, while reading partial symbols, will need to load
7725 the full symbols for the CU for some reason. It will already have a
7726 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7727 rather than creating a new one. */
7729 static void
7730 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7731 dwarf2_per_objfile *per_objfile,
7732 dwarf2_cu *existing_cu,
7733 bool skip_partial,
7734 enum language pretend_language)
7736 gdb_assert (! this_cu->is_debug_types);
7738 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7739 if (reader.dummy_p)
7740 return;
7742 struct dwarf2_cu *cu = reader.cu;
7743 const gdb_byte *info_ptr = reader.info_ptr;
7745 gdb_assert (cu->die_hash == NULL);
7746 cu->die_hash =
7747 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
7748 die_hash,
7749 die_eq,
7750 NULL,
7751 &cu->comp_unit_obstack,
7752 hashtab_obstack_allocate,
7753 dummy_obstack_deallocate);
7755 if (reader.comp_unit_die->has_children)
7756 reader.comp_unit_die->child
7757 = read_die_and_siblings (&reader, reader.info_ptr,
7758 &info_ptr, reader.comp_unit_die);
7759 cu->dies = reader.comp_unit_die;
7760 /* comp_unit_die is not stored in die_hash, no need. */
7762 /* We try not to read any attributes in this function, because not
7763 all CUs needed for references have been loaded yet, and symbol
7764 table processing isn't initialized. But we have to set the CU language,
7765 or we won't be able to build types correctly.
7766 Similarly, if we do not read the producer, we can not apply
7767 producer-specific interpretation. */
7768 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7770 reader.keep ();
7773 /* Add a DIE to the delayed physname list. */
7775 static void
7776 add_to_method_list (struct type *type, int fnfield_index, int index,
7777 const char *name, struct die_info *die,
7778 struct dwarf2_cu *cu)
7780 struct delayed_method_info mi;
7781 mi.type = type;
7782 mi.fnfield_index = fnfield_index;
7783 mi.index = index;
7784 mi.name = name;
7785 mi.die = die;
7786 cu->method_list.push_back (mi);
7789 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7790 "const" / "volatile". If so, decrements LEN by the length of the
7791 modifier and return true. Otherwise return false. */
7793 template<size_t N>
7794 static bool
7795 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7797 size_t mod_len = sizeof (mod) - 1;
7798 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7800 len -= mod_len;
7801 return true;
7803 return false;
7806 /* Compute the physnames of any methods on the CU's method list.
7808 The computation of method physnames is delayed in order to avoid the
7809 (bad) condition that one of the method's formal parameters is of an as yet
7810 incomplete type. */
7812 static void
7813 compute_delayed_physnames (struct dwarf2_cu *cu)
7815 /* Only C++ delays computing physnames. */
7816 if (cu->method_list.empty ())
7817 return;
7818 gdb_assert (cu->lang () == language_cplus);
7820 for (const delayed_method_info &mi : cu->method_list)
7822 const char *physname;
7823 struct fn_fieldlist *fn_flp
7824 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7825 physname = dwarf2_physname (mi.name, mi.die, cu);
7826 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7827 = physname ? physname : "";
7829 /* Since there's no tag to indicate whether a method is a
7830 const/volatile overload, extract that information out of the
7831 demangled name. */
7832 if (physname != NULL)
7834 size_t len = strlen (physname);
7836 while (1)
7838 if (physname[len] == ')') /* shortcut */
7839 break;
7840 else if (check_modifier (physname, len, " const"))
7841 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7842 else if (check_modifier (physname, len, " volatile"))
7843 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7844 else
7845 break;
7850 /* The list is no longer needed. */
7851 cu->method_list.clear ();
7854 /* Go objects should be embedded in a DW_TAG_module DIE,
7855 and it's not clear if/how imported objects will appear.
7856 To keep Go support simple until that's worked out,
7857 go back through what we've read and create something usable.
7858 We could do this while processing each DIE, and feels kinda cleaner,
7859 but that way is more invasive.
7860 This is to, for example, allow the user to type "p var" or "b main"
7861 without having to specify the package name, and allow lookups
7862 of module.object to work in contexts that use the expression
7863 parser. */
7865 static void
7866 fixup_go_packaging (struct dwarf2_cu *cu)
7868 gdb::unique_xmalloc_ptr<char> package_name;
7869 struct pending *list;
7870 int i;
7872 for (list = *cu->get_builder ()->get_global_symbols ();
7873 list != NULL;
7874 list = list->next)
7876 for (i = 0; i < list->nsyms; ++i)
7878 struct symbol *sym = list->symbol[i];
7880 if (sym->language () == language_go
7881 && sym->aclass () == LOC_BLOCK)
7883 gdb::unique_xmalloc_ptr<char> this_package_name
7884 (go_symbol_package_name (sym));
7886 if (this_package_name == NULL)
7887 continue;
7888 if (package_name == NULL)
7889 package_name = std::move (this_package_name);
7890 else
7892 struct objfile *objfile = cu->per_objfile->objfile;
7893 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7894 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7895 (sym->symtab () != NULL
7896 ? symtab_to_filename_for_display
7897 (sym->symtab ())
7898 : objfile_name (objfile)),
7899 this_package_name.get (), package_name.get ());
7905 if (package_name != NULL)
7907 struct objfile *objfile = cu->per_objfile->objfile;
7908 const char *saved_package_name = objfile->intern (package_name.get ());
7909 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7910 saved_package_name);
7911 struct symbol *sym;
7913 sym = new (&objfile->objfile_obstack) symbol;
7914 sym->set_language (language_go, &objfile->objfile_obstack);
7915 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7916 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7917 e.g., "main" finds the "main" module and not C's main(). */
7918 sym->set_domain (STRUCT_DOMAIN);
7919 sym->set_aclass_index (LOC_TYPEDEF);
7920 sym->set_type (type);
7922 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7926 /* Allocate a fully-qualified name consisting of the two parts on the
7927 obstack. */
7929 static const char *
7930 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7932 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7935 /* A helper that allocates a variant part to attach to a Rust enum
7936 type. OBSTACK is where the results should be allocated. TYPE is
7937 the type we're processing. DISCRIMINANT_INDEX is the index of the
7938 discriminant. It must be the index of one of the fields of TYPE,
7939 or -1 to mean there is no discriminant (univariant enum).
7940 DEFAULT_INDEX is the index of the default field; or -1 if there is
7941 no default. RANGES is indexed by "effective" field number (the
7942 field index, but omitting the discriminant and default fields) and
7943 must hold the discriminant values used by the variants. Note that
7944 RANGES must have a lifetime at least as long as OBSTACK -- either
7945 already allocated on it, or static. */
7947 static void
7948 alloc_rust_variant (struct obstack *obstack, struct type *type,
7949 int discriminant_index, int default_index,
7950 gdb::array_view<discriminant_range> ranges)
7952 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7953 gdb_assert (discriminant_index == -1
7954 || (discriminant_index >= 0
7955 && discriminant_index < type->num_fields ()));
7956 gdb_assert (default_index == -1
7957 || (default_index >= 0 && default_index < type->num_fields ()));
7959 /* We have one variant for each non-discriminant field. */
7960 int n_variants = type->num_fields ();
7961 if (discriminant_index != -1)
7962 --n_variants;
7964 variant *variants = new (obstack) variant[n_variants];
7965 int var_idx = 0;
7966 int range_idx = 0;
7967 for (int i = 0; i < type->num_fields (); ++i)
7969 if (i == discriminant_index)
7970 continue;
7972 variants[var_idx].first_field = i;
7973 variants[var_idx].last_field = i + 1;
7975 /* The default field does not need a range, but other fields do.
7976 We skipped the discriminant above. */
7977 if (i != default_index)
7979 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7980 ++range_idx;
7983 ++var_idx;
7986 gdb_assert (range_idx == ranges.size ());
7987 gdb_assert (var_idx == n_variants);
7989 variant_part *part = new (obstack) variant_part;
7990 part->discriminant_index = discriminant_index;
7991 /* If there is no discriminant, then whether it is signed is of no
7992 consequence. */
7993 part->is_unsigned
7994 = (discriminant_index == -1
7995 ? false
7996 : type->field (discriminant_index).type ()->is_unsigned ());
7997 part->variants = gdb::array_view<variant> (variants, n_variants);
7999 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8000 gdb::array_view<variant_part> *prop_value
8001 = new (storage) gdb::array_view<variant_part> (part, 1);
8003 struct dynamic_prop prop;
8004 prop.set_variant_parts (prop_value);
8006 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
8009 /* Some versions of rustc emitted enums in an unusual way.
8011 Ordinary enums were emitted as unions. The first element of each
8012 structure in the union was named "RUST$ENUM$DISR". This element
8013 held the discriminant.
8015 These versions of Rust also implemented the "non-zero"
8016 optimization. When the enum had two values, and one is empty and
8017 the other holds a pointer that cannot be zero, the pointer is used
8018 as the discriminant, with a zero value meaning the empty variant.
8019 Here, the union's first member is of the form
8020 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8021 where the fieldnos are the indices of the fields that should be
8022 traversed in order to find the field (which may be several fields deep)
8023 and the variantname is the name of the variant of the case when the
8024 field is zero.
8026 This function recognizes whether TYPE is of one of these forms,
8027 and, if so, smashes it to be a variant type. */
8029 static void
8030 quirk_rust_enum (struct type *type, struct objfile *objfile)
8032 gdb_assert (type->code () == TYPE_CODE_UNION);
8034 /* We don't need to deal with empty enums. */
8035 if (type->num_fields () == 0)
8036 return;
8038 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8039 if (type->num_fields () == 1
8040 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
8042 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
8044 /* Decode the field name to find the offset of the
8045 discriminant. */
8046 ULONGEST bit_offset = 0;
8047 struct type *field_type = type->field (0).type ();
8048 while (name[0] >= '0' && name[0] <= '9')
8050 char *tail;
8051 unsigned long index = strtoul (name, &tail, 10);
8052 name = tail;
8053 if (*name != '$'
8054 || index >= field_type->num_fields ()
8055 || (field_type->field (index).loc_kind ()
8056 != FIELD_LOC_KIND_BITPOS))
8058 complaint (_("Could not parse Rust enum encoding string \"%s\""
8059 "[in module %s]"),
8060 type->field (0).name (),
8061 objfile_name (objfile));
8062 return;
8064 ++name;
8066 bit_offset += field_type->field (index).loc_bitpos ();
8067 field_type = field_type->field (index).type ();
8070 /* Smash this type to be a structure type. We have to do this
8071 because the type has already been recorded. */
8072 type->set_code (TYPE_CODE_STRUCT);
8073 type->set_num_fields (3);
8074 /* Save the field we care about. */
8075 struct field saved_field = type->field (0);
8076 type->set_fields
8077 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8079 /* Put the discriminant at index 0. */
8080 type->field (0).set_type (field_type);
8081 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8082 type->field (0).set_name ("<<discriminant>>");
8083 type->field (0).set_loc_bitpos (bit_offset);
8085 /* The order of fields doesn't really matter, so put the real
8086 field at index 1 and the data-less field at index 2. */
8087 type->field (1) = saved_field;
8088 type->field (1).set_name
8089 (rust_last_path_segment (type->field (1).type ()->name ()));
8090 type->field (1).type ()->set_name
8091 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8092 type->field (1).name ()));
8094 const char *dataless_name
8095 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8096 name);
8097 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8098 dataless_name);
8099 type->field (2).set_type (dataless_type);
8100 /* NAME points into the original discriminant name, which
8101 already has the correct lifetime. */
8102 type->field (2).set_name (name);
8103 type->field (2).set_loc_bitpos (0);
8105 /* Indicate that this is a variant type. */
8106 static discriminant_range ranges[1] = { { 0, 0 } };
8107 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8109 /* A union with a single anonymous field is probably an old-style
8110 univariant enum. */
8111 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8113 /* Smash this type to be a structure type. We have to do this
8114 because the type has already been recorded. */
8115 type->set_code (TYPE_CODE_STRUCT);
8117 struct type *field_type = type->field (0).type ();
8118 const char *variant_name
8119 = rust_last_path_segment (field_type->name ());
8120 type->field (0).set_name (variant_name);
8121 field_type->set_name
8122 (rust_fully_qualify (&objfile->objfile_obstack,
8123 type->name (), variant_name));
8125 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8127 else
8129 struct type *disr_type = nullptr;
8130 for (int i = 0; i < type->num_fields (); ++i)
8132 disr_type = type->field (i).type ();
8134 if (disr_type->code () != TYPE_CODE_STRUCT)
8136 /* All fields of a true enum will be structs. */
8137 return;
8139 else if (disr_type->num_fields () == 0)
8141 /* Could be data-less variant, so keep going. */
8142 disr_type = nullptr;
8144 else if (strcmp (disr_type->field (0).name (),
8145 "RUST$ENUM$DISR") != 0)
8147 /* Not a Rust enum. */
8148 return;
8150 else
8152 /* Found one. */
8153 break;
8157 /* If we got here without a discriminant, then it's probably
8158 just a union. */
8159 if (disr_type == nullptr)
8160 return;
8162 /* Smash this type to be a structure type. We have to do this
8163 because the type has already been recorded. */
8164 type->set_code (TYPE_CODE_STRUCT);
8166 /* Make space for the discriminant field. */
8167 struct field *disr_field = &disr_type->field (0);
8168 field *new_fields
8169 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8170 * sizeof (struct field)));
8171 memcpy (new_fields + 1, type->fields (),
8172 type->num_fields () * sizeof (struct field));
8173 type->set_fields (new_fields);
8174 type->set_num_fields (type->num_fields () + 1);
8176 /* Install the discriminant at index 0 in the union. */
8177 type->field (0) = *disr_field;
8178 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8179 type->field (0).set_name ("<<discriminant>>");
8181 /* We need a way to find the correct discriminant given a
8182 variant name. For convenience we build a map here. */
8183 struct type *enum_type = disr_field->type ();
8184 std::unordered_map<std::string, ULONGEST> discriminant_map;
8185 for (int i = 0; i < enum_type->num_fields (); ++i)
8187 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8189 const char *name
8190 = rust_last_path_segment (enum_type->field (i).name ());
8191 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8195 int n_fields = type->num_fields ();
8196 /* We don't need a range entry for the discriminant, but we do
8197 need one for every other field, as there is no default
8198 variant. */
8199 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8200 discriminant_range,
8201 n_fields - 1);
8202 /* Skip the discriminant here. */
8203 for (int i = 1; i < n_fields; ++i)
8205 /* Find the final word in the name of this variant's type.
8206 That name can be used to look up the correct
8207 discriminant. */
8208 const char *variant_name
8209 = rust_last_path_segment (type->field (i).type ()->name ());
8211 auto iter = discriminant_map.find (variant_name);
8212 if (iter != discriminant_map.end ())
8214 ranges[i - 1].low = iter->second;
8215 ranges[i - 1].high = iter->second;
8218 /* In Rust, each element should have the size of the
8219 enclosing enum. */
8220 type->field (i).type ()->set_length (type->length ());
8222 /* Remove the discriminant field, if it exists. */
8223 struct type *sub_type = type->field (i).type ();
8224 if (sub_type->num_fields () > 0)
8226 sub_type->set_num_fields (sub_type->num_fields () - 1);
8227 sub_type->set_fields (sub_type->fields () + 1);
8229 type->field (i).set_name (variant_name);
8230 sub_type->set_name
8231 (rust_fully_qualify (&objfile->objfile_obstack,
8232 type->name (), variant_name));
8235 /* Indicate that this is a variant type. */
8236 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8237 gdb::array_view<discriminant_range> (ranges,
8238 n_fields - 1));
8242 /* Rewrite some Rust unions to be structures with variants parts. */
8244 static void
8245 rust_union_quirks (struct dwarf2_cu *cu)
8247 gdb_assert (cu->lang () == language_rust);
8248 for (type *type_ : cu->rust_unions)
8249 quirk_rust_enum (type_, cu->per_objfile->objfile);
8250 /* We don't need this any more. */
8251 cu->rust_unions.clear ();
8254 /* See read.h. */
8256 type_unit_group_unshareable *
8257 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8259 auto iter = this->m_type_units.find (tu_group);
8260 if (iter != this->m_type_units.end ())
8261 return iter->second.get ();
8263 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8264 type_unit_group_unshareable *result = uniq.get ();
8265 this->m_type_units[tu_group] = std::move (uniq);
8266 return result;
8269 struct type *
8270 dwarf2_per_objfile::get_type_for_signatured_type
8271 (signatured_type *sig_type) const
8273 auto iter = this->m_type_map.find (sig_type);
8274 if (iter == this->m_type_map.end ())
8275 return nullptr;
8277 return iter->second;
8280 void dwarf2_per_objfile::set_type_for_signatured_type
8281 (signatured_type *sig_type, struct type *type)
8283 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8285 this->m_type_map[sig_type] = type;
8288 /* A helper function for computing the list of all symbol tables
8289 included by PER_CU. */
8291 static void
8292 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8293 htab_t all_children, htab_t all_type_symtabs,
8294 dwarf2_per_cu_data *per_cu,
8295 dwarf2_per_objfile *per_objfile,
8296 struct compunit_symtab *immediate_parent)
8298 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8299 if (*slot != NULL)
8301 /* This inclusion and its children have been processed. */
8302 return;
8305 *slot = per_cu;
8307 /* Only add a CU if it has a symbol table. */
8308 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8309 if (cust != NULL)
8311 /* If this is a type unit only add its symbol table if we haven't
8312 seen it yet (type unit per_cu's can share symtabs). */
8313 if (per_cu->is_debug_types)
8315 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8316 if (*slot == NULL)
8318 *slot = cust;
8319 result->push_back (cust);
8320 if (cust->user == NULL)
8321 cust->user = immediate_parent;
8324 else
8326 result->push_back (cust);
8327 if (cust->user == NULL)
8328 cust->user = immediate_parent;
8332 if (!per_cu->imported_symtabs_empty ())
8333 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8335 recursively_compute_inclusions (result, all_children,
8336 all_type_symtabs, ptr, per_objfile,
8337 cust);
8341 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8342 PER_CU. */
8344 static void
8345 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8346 dwarf2_per_objfile *per_objfile)
8348 gdb_assert (! per_cu->is_debug_types);
8350 if (!per_cu->imported_symtabs_empty ())
8352 int len;
8353 std::vector<compunit_symtab *> result_symtabs;
8354 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8356 /* If we don't have a symtab, we can just skip this case. */
8357 if (cust == NULL)
8358 return;
8360 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8361 htab_eq_pointer,
8362 NULL, xcalloc, xfree));
8363 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8364 htab_eq_pointer,
8365 NULL, xcalloc, xfree));
8367 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8369 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8370 all_type_symtabs.get (), ptr,
8371 per_objfile, cust);
8374 /* Now we have a transitive closure of all the included symtabs. */
8375 len = result_symtabs.size ();
8376 cust->includes
8377 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8378 struct compunit_symtab *, len + 1);
8379 memcpy (cust->includes, result_symtabs.data (),
8380 len * sizeof (compunit_symtab *));
8381 cust->includes[len] = NULL;
8385 /* Compute the 'includes' field for the symtabs of all the CUs we just
8386 read. */
8388 static void
8389 process_cu_includes (dwarf2_per_objfile *per_objfile)
8391 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8393 if (! iter->is_debug_types)
8394 compute_compunit_symtab_includes (iter, per_objfile);
8397 per_objfile->per_bfd->just_read_cus.clear ();
8400 /* Generate full symbol information for CU, whose DIEs have
8401 already been loaded into memory. */
8403 static void
8404 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8406 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8407 struct objfile *objfile = per_objfile->objfile;
8408 struct gdbarch *gdbarch = objfile->arch ();
8409 CORE_ADDR lowpc, highpc;
8410 struct compunit_symtab *cust;
8411 CORE_ADDR baseaddr;
8412 struct block *static_block;
8413 CORE_ADDR addr;
8415 baseaddr = objfile->text_section_offset ();
8417 /* Clear the list here in case something was left over. */
8418 cu->method_list.clear ();
8420 dwarf2_find_base_address (cu->dies, cu);
8422 /* Before we start reading the top-level DIE, ensure it has a valid tag
8423 type. */
8424 switch (cu->dies->tag)
8426 case DW_TAG_compile_unit:
8427 case DW_TAG_partial_unit:
8428 case DW_TAG_type_unit:
8429 break;
8430 default:
8431 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8432 dwarf_tag_name (cu->dies->tag),
8433 sect_offset_str (cu->per_cu->sect_off),
8434 objfile_name (per_objfile->objfile));
8437 /* Do line number decoding in read_file_scope () */
8438 process_die (cu->dies, cu);
8440 /* For now fudge the Go package. */
8441 if (cu->lang () == language_go)
8442 fixup_go_packaging (cu);
8444 /* Now that we have processed all the DIEs in the CU, all the types
8445 should be complete, and it should now be safe to compute all of the
8446 physnames. */
8447 compute_delayed_physnames (cu);
8449 if (cu->lang () == language_rust)
8450 rust_union_quirks (cu);
8452 /* Some compilers don't define a DW_AT_high_pc attribute for the
8453 compilation unit. If the DW_AT_high_pc is missing, synthesize
8454 it, by scanning the DIE's below the compilation unit. */
8455 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8457 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8458 static_block
8459 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8461 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8462 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8463 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8464 addrmap to help ensure it has an accurate map of pc values belonging to
8465 this comp unit. */
8466 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8468 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8469 (static_block, SECT_OFF_TEXT (objfile), 0);
8471 if (cust != NULL)
8473 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8475 /* Set symtab language to language from DW_AT_language. If the
8476 compilation is from a C file generated by language preprocessors, do
8477 not set the language if it was already deduced by start_subfile. */
8478 if (!(cu->lang () == language_c
8479 && cust->primary_filetab ()->language () != language_unknown))
8480 cust->primary_filetab ()->set_language (cu->lang ());
8482 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8483 produce DW_AT_location with location lists but it can be possibly
8484 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8485 there were bugs in prologue debug info, fixed later in GCC-4.5
8486 by "unwind info for epilogues" patch (which is not directly related).
8488 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8489 needed, it would be wrong due to missing DW_AT_producer there.
8491 Still one can confuse GDB by using non-standard GCC compilation
8492 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8494 if (cu->has_loclist && gcc_4_minor >= 5)
8495 cust->set_locations_valid (true);
8497 if (gcc_4_minor >= 5)
8498 cust->set_epilogue_unwind_valid (true);
8500 cust->set_call_site_htab (cu->call_site_htab);
8503 per_objfile->set_symtab (cu->per_cu, cust);
8505 /* Push it for inclusion processing later. */
8506 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8508 /* Not needed any more. */
8509 cu->reset_builder ();
8512 /* Generate full symbol information for type unit CU, whose DIEs have
8513 already been loaded into memory. */
8515 static void
8516 process_full_type_unit (dwarf2_cu *cu,
8517 enum language pretend_language)
8519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8520 struct objfile *objfile = per_objfile->objfile;
8521 struct compunit_symtab *cust;
8522 struct signatured_type *sig_type;
8524 gdb_assert (cu->per_cu->is_debug_types);
8525 sig_type = (struct signatured_type *) cu->per_cu;
8527 /* Clear the list here in case something was left over. */
8528 cu->method_list.clear ();
8530 /* The symbol tables are set up in read_type_unit_scope. */
8531 process_die (cu->dies, cu);
8533 /* For now fudge the Go package. */
8534 if (cu->lang () == language_go)
8535 fixup_go_packaging (cu);
8537 /* Now that we have processed all the DIEs in the CU, all the types
8538 should be complete, and it should now be safe to compute all of the
8539 physnames. */
8540 compute_delayed_physnames (cu);
8542 if (cu->lang () == language_rust)
8543 rust_union_quirks (cu);
8545 /* TUs share symbol tables.
8546 If this is the first TU to use this symtab, complete the construction
8547 of it with end_expandable_symtab. Otherwise, complete the addition of
8548 this TU's symbols to the existing symtab. */
8549 type_unit_group_unshareable *tug_unshare =
8550 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8551 if (tug_unshare->compunit_symtab == NULL)
8553 buildsym_compunit *builder = cu->get_builder ();
8554 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8555 tug_unshare->compunit_symtab = cust;
8557 if (cust != NULL)
8559 /* Set symtab language to language from DW_AT_language. If the
8560 compilation is from a C file generated by language preprocessors,
8561 do not set the language if it was already deduced by
8562 start_subfile. */
8563 if (!(cu->lang () == language_c
8564 && cust->primary_filetab ()->language () != language_c))
8565 cust->primary_filetab ()->set_language (cu->lang ());
8568 else
8570 cu->get_builder ()->augment_type_symtab ();
8571 cust = tug_unshare->compunit_symtab;
8574 per_objfile->set_symtab (cu->per_cu, cust);
8576 /* Not needed any more. */
8577 cu->reset_builder ();
8580 /* Process an imported unit DIE. */
8582 static void
8583 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8585 struct attribute *attr;
8587 /* For now we don't handle imported units in type units. */
8588 if (cu->per_cu->is_debug_types)
8590 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8591 " supported in type units [in module %s]"),
8592 objfile_name (cu->per_objfile->objfile));
8595 attr = dwarf2_attr (die, DW_AT_import, cu);
8596 if (attr != NULL)
8598 sect_offset sect_off = attr->get_ref_die_offset ();
8599 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8600 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8601 dwarf2_per_cu_data *per_cu
8602 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8603 per_objfile->per_bfd);
8605 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8606 into another compilation unit, at root level. Regard this as a hint,
8607 and ignore it. This is a best effort, it only works if unit_type and
8608 lang are already set. */
8609 if (die->parent && die->parent->parent == NULL
8610 && per_cu->unit_type (false) == DW_UT_compile
8611 && per_cu->lang (false) == language_cplus)
8612 return;
8614 /* If necessary, add it to the queue and load its DIEs. */
8615 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8616 cu->lang ()))
8617 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8618 false, cu->lang ());
8620 cu->per_cu->imported_symtabs_push (per_cu);
8624 /* RAII object that represents a process_die scope: i.e.,
8625 starts/finishes processing a DIE. */
8626 class process_die_scope
8628 public:
8629 process_die_scope (die_info *die, dwarf2_cu *cu)
8630 : m_die (die), m_cu (cu)
8632 /* We should only be processing DIEs not already in process. */
8633 gdb_assert (!m_die->in_process);
8634 m_die->in_process = true;
8637 ~process_die_scope ()
8639 m_die->in_process = false;
8641 /* If we're done processing the DIE for the CU that owns the line
8642 header, we don't need the line header anymore. */
8643 if (m_cu->line_header_die_owner == m_die)
8645 delete m_cu->line_header;
8646 m_cu->line_header = NULL;
8647 m_cu->line_header_die_owner = NULL;
8651 private:
8652 die_info *m_die;
8653 dwarf2_cu *m_cu;
8656 /* Process a die and its children. */
8658 static void
8659 process_die (struct die_info *die, struct dwarf2_cu *cu)
8661 process_die_scope scope (die, cu);
8663 switch (die->tag)
8665 case DW_TAG_padding:
8666 break;
8667 case DW_TAG_compile_unit:
8668 case DW_TAG_partial_unit:
8669 read_file_scope (die, cu);
8670 break;
8671 case DW_TAG_type_unit:
8672 read_type_unit_scope (die, cu);
8673 break;
8674 case DW_TAG_subprogram:
8675 /* Nested subprograms in Fortran get a prefix. */
8676 if (cu->lang () == language_fortran
8677 && die->parent != NULL
8678 && die->parent->tag == DW_TAG_subprogram)
8679 cu->processing_has_namespace_info = true;
8680 /* Fall through. */
8681 case DW_TAG_inlined_subroutine:
8682 read_func_scope (die, cu);
8683 break;
8684 case DW_TAG_lexical_block:
8685 case DW_TAG_try_block:
8686 case DW_TAG_catch_block:
8687 read_lexical_block_scope (die, cu);
8688 break;
8689 case DW_TAG_call_site:
8690 case DW_TAG_GNU_call_site:
8691 read_call_site_scope (die, cu);
8692 break;
8693 case DW_TAG_class_type:
8694 case DW_TAG_interface_type:
8695 case DW_TAG_structure_type:
8696 case DW_TAG_union_type:
8697 case DW_TAG_namelist:
8698 process_structure_scope (die, cu);
8699 break;
8700 case DW_TAG_enumeration_type:
8701 process_enumeration_scope (die, cu);
8702 break;
8704 /* These dies have a type, but processing them does not create
8705 a symbol or recurse to process the children. Therefore we can
8706 read them on-demand through read_type_die. */
8707 case DW_TAG_subroutine_type:
8708 case DW_TAG_set_type:
8709 case DW_TAG_pointer_type:
8710 case DW_TAG_ptr_to_member_type:
8711 case DW_TAG_reference_type:
8712 case DW_TAG_rvalue_reference_type:
8713 case DW_TAG_string_type:
8714 break;
8716 case DW_TAG_array_type:
8717 /* We only need to handle this case for Ada -- in other
8718 languages, it's normal for the compiler to emit a typedef
8719 instead. */
8720 if (cu->lang () != language_ada)
8721 break;
8722 /* FALLTHROUGH */
8723 case DW_TAG_base_type:
8724 case DW_TAG_subrange_type:
8725 case DW_TAG_generic_subrange:
8726 case DW_TAG_typedef:
8727 /* Add a typedef symbol for the type definition, if it has a
8728 DW_AT_name. */
8729 new_symbol (die, read_type_die (die, cu), cu);
8730 break;
8731 case DW_TAG_common_block:
8732 read_common_block (die, cu);
8733 break;
8734 case DW_TAG_common_inclusion:
8735 break;
8736 case DW_TAG_namespace:
8737 cu->processing_has_namespace_info = true;
8738 read_namespace (die, cu);
8739 break;
8740 case DW_TAG_module:
8741 cu->processing_has_namespace_info = true;
8742 read_module (die, cu);
8743 break;
8744 case DW_TAG_imported_declaration:
8745 cu->processing_has_namespace_info = true;
8746 if (read_alias (die, cu))
8747 break;
8748 /* The declaration is neither a global namespace nor a variable
8749 alias. */
8750 /* Fall through. */
8751 case DW_TAG_imported_module:
8752 cu->processing_has_namespace_info = true;
8753 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8754 || cu->lang () != language_fortran))
8755 complaint (_("Tag '%s' has unexpected children"),
8756 dwarf_tag_name (die->tag));
8757 read_import_statement (die, cu);
8758 break;
8760 case DW_TAG_imported_unit:
8761 process_imported_unit_die (die, cu);
8762 break;
8764 case DW_TAG_variable:
8765 read_variable (die, cu);
8766 break;
8768 default:
8769 new_symbol (die, NULL, cu);
8770 break;
8774 /* DWARF name computation. */
8776 /* A helper function for dwarf2_compute_name which determines whether DIE
8777 needs to have the name of the scope prepended to the name listed in the
8778 die. */
8780 static int
8781 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8783 struct attribute *attr;
8785 switch (die->tag)
8787 case DW_TAG_namespace:
8788 case DW_TAG_typedef:
8789 case DW_TAG_class_type:
8790 case DW_TAG_interface_type:
8791 case DW_TAG_structure_type:
8792 case DW_TAG_union_type:
8793 case DW_TAG_enumeration_type:
8794 case DW_TAG_enumerator:
8795 case DW_TAG_subprogram:
8796 case DW_TAG_inlined_subroutine:
8797 case DW_TAG_member:
8798 case DW_TAG_imported_declaration:
8799 return 1;
8801 case DW_TAG_variable:
8802 case DW_TAG_constant:
8803 /* We only need to prefix "globally" visible variables. These include
8804 any variable marked with DW_AT_external or any variable that
8805 lives in a namespace. [Variables in anonymous namespaces
8806 require prefixing, but they are not DW_AT_external.] */
8808 if (dwarf2_attr (die, DW_AT_specification, cu))
8810 struct dwarf2_cu *spec_cu = cu;
8812 return die_needs_namespace (die_specification (die, &spec_cu),
8813 spec_cu);
8816 attr = dwarf2_attr (die, DW_AT_external, cu);
8817 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8818 && die->parent->tag != DW_TAG_module)
8819 return 0;
8820 /* A variable in a lexical block of some kind does not need a
8821 namespace, even though in C++ such variables may be external
8822 and have a mangled name. */
8823 if (die->parent->tag == DW_TAG_lexical_block
8824 || die->parent->tag == DW_TAG_try_block
8825 || die->parent->tag == DW_TAG_catch_block
8826 || die->parent->tag == DW_TAG_subprogram)
8827 return 0;
8828 return 1;
8830 default:
8831 return 0;
8835 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8836 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8837 defined for the given DIE. */
8839 static struct attribute *
8840 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8842 struct attribute *attr;
8844 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8845 if (attr == NULL)
8846 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8848 return attr;
8851 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8852 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8853 defined for the given DIE. */
8855 static const char *
8856 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8858 const char *linkage_name;
8860 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8861 if (linkage_name == NULL)
8862 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8864 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8865 See https://github.com/rust-lang/rust/issues/32925. */
8866 if (cu->lang () == language_rust && linkage_name != NULL
8867 && strchr (linkage_name, '{') != NULL)
8868 linkage_name = NULL;
8870 return linkage_name;
8873 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8874 compute the physname for the object, which include a method's:
8875 - formal parameters (C++),
8876 - receiver type (Go),
8878 The term "physname" is a bit confusing.
8879 For C++, for example, it is the demangled name.
8880 For Go, for example, it's the mangled name.
8882 For Ada, return the DIE's linkage name rather than the fully qualified
8883 name. PHYSNAME is ignored..
8885 The result is allocated on the objfile->per_bfd's obstack and
8886 canonicalized. */
8888 static const char *
8889 dwarf2_compute_name (const char *name,
8890 struct die_info *die, struct dwarf2_cu *cu,
8891 int physname)
8893 struct objfile *objfile = cu->per_objfile->objfile;
8895 if (name == NULL)
8896 name = dwarf2_name (die, cu);
8898 enum language lang = cu->lang ();
8900 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8901 but otherwise compute it by typename_concat inside GDB.
8902 FIXME: Actually this is not really true, or at least not always true.
8903 It's all very confusing. compute_and_set_names doesn't try to demangle
8904 Fortran names because there is no mangling standard. So new_symbol
8905 will set the demangled name to the result of dwarf2_full_name, and it is
8906 the demangled name that GDB uses if it exists. */
8907 if (lang == language_ada
8908 || (lang == language_fortran && physname))
8910 /* For Ada unit, we prefer the linkage name over the name, as
8911 the former contains the exported name, which the user expects
8912 to be able to reference. Ideally, we want the user to be able
8913 to reference this entity using either natural or linkage name,
8914 but we haven't started looking at this enhancement yet. */
8915 const char *linkage_name = dw2_linkage_name (die, cu);
8917 if (linkage_name != NULL)
8918 return linkage_name;
8921 /* These are the only languages we know how to qualify names in. */
8922 if (name != NULL
8923 && (lang == language_cplus
8924 || lang == language_fortran || lang == language_d
8925 || lang == language_rust))
8927 if (die_needs_namespace (die, cu))
8929 const char *prefix;
8931 string_file buf;
8933 prefix = determine_prefix (die, cu);
8934 if (*prefix != '\0')
8936 gdb::unique_xmalloc_ptr<char> prefixed_name
8937 (typename_concat (NULL, prefix, name, physname, cu));
8939 buf.puts (prefixed_name.get ());
8941 else
8942 buf.puts (name);
8944 /* Template parameters may be specified in the DIE's DW_AT_name, or
8945 as children with DW_TAG_template_type_param or
8946 DW_TAG_value_type_param. If the latter, add them to the name
8947 here. If the name already has template parameters, then
8948 skip this step; some versions of GCC emit both, and
8949 it is more efficient to use the pre-computed name.
8951 Something to keep in mind about this process: it is very
8952 unlikely, or in some cases downright impossible, to produce
8953 something that will match the mangled name of a function.
8954 If the definition of the function has the same debug info,
8955 we should be able to match up with it anyway. But fallbacks
8956 using the minimal symbol, for instance to find a method
8957 implemented in a stripped copy of libstdc++, will not work.
8958 If we do not have debug info for the definition, we will have to
8959 match them up some other way.
8961 When we do name matching there is a related problem with function
8962 templates; two instantiated function templates are allowed to
8963 differ only by their return types, which we do not add here. */
8965 if (lang == language_cplus && strchr (name, '<') == NULL)
8967 struct attribute *attr;
8968 struct die_info *child;
8969 int first = 1;
8971 die->building_fullname = 1;
8973 for (child = die->child; child != NULL; child = child->sibling)
8975 struct type *type;
8976 LONGEST value;
8977 const gdb_byte *bytes;
8978 struct dwarf2_locexpr_baton *baton;
8979 struct value *v;
8981 if (child->tag != DW_TAG_template_type_param
8982 && child->tag != DW_TAG_template_value_param)
8983 continue;
8985 if (first)
8987 buf.puts ("<");
8988 first = 0;
8990 else
8991 buf.puts (", ");
8993 attr = dwarf2_attr (child, DW_AT_type, cu);
8994 if (attr == NULL)
8996 complaint (_("template parameter missing DW_AT_type"));
8997 buf.puts ("UNKNOWN_TYPE");
8998 continue;
9000 type = die_type (child, cu);
9002 if (child->tag == DW_TAG_template_type_param)
9004 cu->language_defn->print_type (type, "", &buf, -1, 0,
9005 &type_print_raw_options);
9006 continue;
9009 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9010 if (attr == NULL)
9012 complaint (_("template parameter missing "
9013 "DW_AT_const_value"));
9014 buf.puts ("UNKNOWN_VALUE");
9015 continue;
9018 dwarf2_const_value_attr (attr, type, name,
9019 &cu->comp_unit_obstack, cu,
9020 &value, &bytes, &baton);
9022 if (type->has_no_signedness ())
9023 /* GDB prints characters as NUMBER 'CHAR'. If that's
9024 changed, this can use value_print instead. */
9025 cu->language_defn->printchar (value, type, &buf);
9026 else
9028 struct value_print_options opts;
9030 if (baton != NULL)
9031 v = dwarf2_evaluate_loc_desc (type, NULL,
9032 baton->data,
9033 baton->size,
9034 baton->per_cu,
9035 baton->per_objfile);
9036 else if (bytes != NULL)
9038 v = allocate_value (type);
9039 memcpy (value_contents_writeable (v).data (), bytes,
9040 type->length ());
9042 else
9043 v = value_from_longest (type, value);
9045 /* Specify decimal so that we do not depend on
9046 the radix. */
9047 get_formatted_print_options (&opts, 'd');
9048 opts.raw = true;
9049 value_print (v, &buf, &opts);
9050 release_value (v);
9054 die->building_fullname = 0;
9056 if (!first)
9058 /* Close the argument list, with a space if necessary
9059 (nested templates). */
9060 if (!buf.empty () && buf.string ().back () == '>')
9061 buf.puts (" >");
9062 else
9063 buf.puts (">");
9067 /* For C++ methods, append formal parameter type
9068 information, if PHYSNAME. */
9070 if (physname && die->tag == DW_TAG_subprogram
9071 && lang == language_cplus)
9073 struct type *type = read_type_die (die, cu);
9075 c_type_print_args (type, &buf, 1, lang,
9076 &type_print_raw_options);
9078 if (lang == language_cplus)
9080 /* Assume that an artificial first parameter is
9081 "this", but do not crash if it is not. RealView
9082 marks unnamed (and thus unused) parameters as
9083 artificial; there is no way to differentiate
9084 the two cases. */
9085 if (type->num_fields () > 0
9086 && TYPE_FIELD_ARTIFICIAL (type, 0)
9087 && type->field (0).type ()->code () == TYPE_CODE_PTR
9088 && TYPE_CONST (type->field (0).type ()->target_type ()))
9089 buf.puts (" const");
9093 const std::string &intermediate_name = buf.string ();
9095 const char *canonical_name
9096 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9097 objfile);
9099 /* If we only computed INTERMEDIATE_NAME, or if
9100 INTERMEDIATE_NAME is already canonical, then we need to
9101 intern it. */
9102 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9103 name = objfile->intern (intermediate_name);
9104 else
9105 name = canonical_name;
9109 return name;
9112 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9113 If scope qualifiers are appropriate they will be added. The result
9114 will be allocated on the storage_obstack, or NULL if the DIE does
9115 not have a name. NAME may either be from a previous call to
9116 dwarf2_name or NULL.
9118 The output string will be canonicalized (if C++). */
9120 static const char *
9121 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9123 return dwarf2_compute_name (name, die, cu, 0);
9126 /* Construct a physname for the given DIE in CU. NAME may either be
9127 from a previous call to dwarf2_name or NULL. The result will be
9128 allocated on the objfile_objstack or NULL if the DIE does not have a
9129 name.
9131 The output string will be canonicalized (if C++). */
9133 static const char *
9134 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9136 struct objfile *objfile = cu->per_objfile->objfile;
9137 const char *retval, *mangled = NULL, *canon = NULL;
9138 int need_copy = 1;
9140 /* In this case dwarf2_compute_name is just a shortcut not building anything
9141 on its own. */
9142 if (!die_needs_namespace (die, cu))
9143 return dwarf2_compute_name (name, die, cu, 1);
9145 if (cu->lang () != language_rust)
9146 mangled = dw2_linkage_name (die, cu);
9148 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9149 has computed. */
9150 gdb::unique_xmalloc_ptr<char> demangled;
9151 if (mangled != NULL)
9153 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9155 /* Do nothing (do not demangle the symbol name). */
9157 else
9159 /* Use DMGL_RET_DROP for C++ template functions to suppress
9160 their return type. It is easier for GDB users to search
9161 for such functions as `name(params)' than `long name(params)'.
9162 In such case the minimal symbol names do not match the full
9163 symbol names but for template functions there is never a need
9164 to look up their definition from their declaration so
9165 the only disadvantage remains the minimal symbol variant
9166 `long name(params)' does not have the proper inferior type. */
9167 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9168 | DMGL_RET_DROP));
9170 if (demangled)
9171 canon = demangled.get ();
9172 else
9174 canon = mangled;
9175 need_copy = 0;
9179 if (canon == NULL || check_physname)
9181 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9183 if (canon != NULL && strcmp (physname, canon) != 0)
9185 /* It may not mean a bug in GDB. The compiler could also
9186 compute DW_AT_linkage_name incorrectly. But in such case
9187 GDB would need to be bug-to-bug compatible. */
9189 complaint (_("Computed physname <%s> does not match demangled <%s> "
9190 "(from linkage <%s>) - DIE at %s [in module %s]"),
9191 physname, canon, mangled, sect_offset_str (die->sect_off),
9192 objfile_name (objfile));
9194 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9195 is available here - over computed PHYSNAME. It is safer
9196 against both buggy GDB and buggy compilers. */
9198 retval = canon;
9200 else
9202 retval = physname;
9203 need_copy = 0;
9206 else
9207 retval = canon;
9209 if (need_copy)
9210 retval = objfile->intern (retval);
9212 return retval;
9215 /* Inspect DIE in CU for a namespace alias or a variable with alias
9216 attribute. If one exists, record a new symbol for it.
9218 Returns true if an alias was recorded, false otherwise. */
9220 static bool
9221 read_alias (struct die_info *die, struct dwarf2_cu *cu)
9223 struct attribute *attr;
9225 /* If the die does not have a name, this is neither a namespace
9226 alias nor a variable alias. */
9227 attr = dwarf2_attr (die, DW_AT_name, cu);
9228 if (attr != NULL)
9230 int num;
9231 struct die_info *d = die;
9232 struct dwarf2_cu *imported_cu = cu;
9234 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9235 keep inspecting DIEs until we hit the underlying import. */
9236 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9237 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9239 attr = dwarf2_attr (d, DW_AT_import, cu);
9240 if (attr == NULL)
9241 break;
9243 d = follow_die_ref (d, attr, &imported_cu);
9244 if (d->tag != DW_TAG_imported_declaration)
9245 break;
9248 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9250 complaint (_("DIE at %s has too many recursively imported "
9251 "declarations"), sect_offset_str (d->sect_off));
9252 return false;
9255 if (attr != NULL)
9257 struct type *type;
9258 if (d->tag == DW_TAG_variable)
9260 /* This declaration is a C/C++ global variable alias.
9261 Add a symbol for it whose type is the same as the
9262 aliased variable's. */
9263 type = die_type (d, imported_cu);
9264 struct symbol *sym = new_symbol (die, type, cu);
9265 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
9266 sym->set_aclass_index (LOC_UNRESOLVED);
9267 if (attr != nullptr)
9268 var_decode_location (attr, sym, cu);
9269 return true;
9271 else
9273 sect_offset sect_off = attr->get_ref_die_offset ();
9274 type = get_die_type_at_offset (sect_off, cu->per_cu,
9275 cu->per_objfile);
9276 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
9278 /* This declaration is a global namespace alias. Add
9279 a symbol for it whose type is the aliased
9280 namespace. */
9281 new_symbol (die, type, cu);
9282 return true;
9287 return false;
9290 /* Return the using directives repository (global or local?) to use in the
9291 current context for CU.
9293 For Ada, imported declarations can materialize renamings, which *may* be
9294 global. However it is impossible (for now?) in DWARF to distinguish
9295 "external" imported declarations and "static" ones. As all imported
9296 declarations seem to be static in all other languages, make them all CU-wide
9297 global only in Ada. */
9299 static struct using_direct **
9300 using_directives (struct dwarf2_cu *cu)
9302 if (cu->lang () == language_ada
9303 && cu->get_builder ()->outermost_context_p ())
9304 return cu->get_builder ()->get_global_using_directives ();
9305 else
9306 return cu->get_builder ()->get_local_using_directives ();
9309 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
9310 given CU. If the format is not recognized or the attribute is
9311 not present, set it to 0. */
9313 static unsigned int
9314 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
9316 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
9317 if (decl_line == nullptr)
9318 return 0;
9319 if (decl_line->form_is_constant ())
9321 LONGEST val = decl_line->constant_value (0);
9322 if (0 <= val && val <= UINT_MAX)
9323 return (unsigned int) val;
9325 complaint (_("Declared line for using directive is too large"));
9326 return 0;
9329 complaint (_("Declared line for using directive is of incorrect format"));
9330 return 0;
9333 /* Read the import statement specified by the given die and record it. */
9335 static void
9336 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9338 struct objfile *objfile = cu->per_objfile->objfile;
9339 struct attribute *import_attr;
9340 struct die_info *imported_die, *child_die;
9341 struct dwarf2_cu *imported_cu;
9342 const char *imported_name;
9343 const char *imported_name_prefix;
9344 const char *canonical_name;
9345 const char *import_alias;
9346 const char *imported_declaration = NULL;
9347 const char *import_prefix;
9348 std::vector<const char *> excludes;
9350 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9351 if (import_attr == NULL)
9353 complaint (_("Tag '%s' has no DW_AT_import"),
9354 dwarf_tag_name (die->tag));
9355 return;
9358 imported_cu = cu;
9359 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9360 imported_name = dwarf2_name (imported_die, imported_cu);
9361 if (imported_name == NULL)
9363 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9365 The import in the following code:
9366 namespace A
9368 typedef int B;
9371 int main ()
9373 using A::B;
9374 B b;
9375 return b;
9379 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9380 <52> DW_AT_decl_file : 1
9381 <53> DW_AT_decl_line : 6
9382 <54> DW_AT_import : <0x75>
9383 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9384 <59> DW_AT_name : B
9385 <5b> DW_AT_decl_file : 1
9386 <5c> DW_AT_decl_line : 2
9387 <5d> DW_AT_type : <0x6e>
9389 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9390 <76> DW_AT_byte_size : 4
9391 <77> DW_AT_encoding : 5 (signed)
9393 imports the wrong die ( 0x75 instead of 0x58 ).
9394 This case will be ignored until the gcc bug is fixed. */
9395 return;
9398 /* Figure out the local name after import. */
9399 import_alias = dwarf2_name (die, cu);
9401 /* Figure out where the statement is being imported to. */
9402 import_prefix = determine_prefix (die, cu);
9404 /* Figure out what the scope of the imported die is and prepend it
9405 to the name of the imported die. */
9406 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9408 if (imported_die->tag != DW_TAG_namespace
9409 && imported_die->tag != DW_TAG_module)
9411 imported_declaration = imported_name;
9412 canonical_name = imported_name_prefix;
9414 else if (strlen (imported_name_prefix) > 0)
9415 canonical_name = obconcat (&objfile->objfile_obstack,
9416 imported_name_prefix,
9417 (cu->lang () == language_d
9418 ? "."
9419 : "::"),
9420 imported_name, (char *) NULL);
9421 else
9422 canonical_name = imported_name;
9424 if (die->tag == DW_TAG_imported_module
9425 && cu->lang () == language_fortran)
9426 for (child_die = die->child; child_die && child_die->tag;
9427 child_die = child_die->sibling)
9429 /* DWARF-4: A Fortran use statement with a “rename list” may be
9430 represented by an imported module entry with an import attribute
9431 referring to the module and owned entries corresponding to those
9432 entities that are renamed as part of being imported. */
9434 if (child_die->tag != DW_TAG_imported_declaration)
9436 complaint (_("child DW_TAG_imported_declaration expected "
9437 "- DIE at %s [in module %s]"),
9438 sect_offset_str (child_die->sect_off),
9439 objfile_name (objfile));
9440 continue;
9443 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9444 if (import_attr == NULL)
9446 complaint (_("Tag '%s' has no DW_AT_import"),
9447 dwarf_tag_name (child_die->tag));
9448 continue;
9451 imported_cu = cu;
9452 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9453 &imported_cu);
9454 imported_name = dwarf2_name (imported_die, imported_cu);
9455 if (imported_name == NULL)
9457 complaint (_("child DW_TAG_imported_declaration has unknown "
9458 "imported name - DIE at %s [in module %s]"),
9459 sect_offset_str (child_die->sect_off),
9460 objfile_name (objfile));
9461 continue;
9464 excludes.push_back (imported_name);
9466 process_die (child_die, cu);
9469 add_using_directive (using_directives (cu),
9470 import_prefix,
9471 canonical_name,
9472 import_alias,
9473 imported_declaration,
9474 excludes,
9475 read_decl_line (die, cu),
9477 &objfile->objfile_obstack);
9480 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9481 types, but gives them a size of zero. Starting with version 14,
9482 ICC is compatible with GCC. */
9484 static bool
9485 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9487 if (!cu->checked_producer)
9488 check_producer (cu);
9490 return cu->producer_is_icc_lt_14;
9493 /* ICC generates a DW_AT_type for C void functions. This was observed on
9494 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9495 which says that void functions should not have a DW_AT_type. */
9497 static bool
9498 producer_is_icc (struct dwarf2_cu *cu)
9500 if (!cu->checked_producer)
9501 check_producer (cu);
9503 return cu->producer_is_icc;
9506 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9507 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9508 this, it was first present in GCC release 4.3.0. */
9510 static bool
9511 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9513 if (!cu->checked_producer)
9514 check_producer (cu);
9516 return cu->producer_is_gcc_lt_4_3;
9519 /* See dwarf2/read.h. */
9520 bool
9521 producer_is_clang (struct dwarf2_cu *cu)
9523 if (!cu->checked_producer)
9524 check_producer (cu);
9526 return cu->producer_is_clang;
9529 static file_and_directory &
9530 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9532 if (cu->per_cu->fnd != nullptr)
9533 return *cu->per_cu->fnd;
9535 /* Find the filename. Do not use dwarf2_name here, since the filename
9536 is not a source language identifier. */
9537 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9538 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9540 if (res.get_comp_dir () == nullptr
9541 && producer_is_gcc_lt_4_3 (cu)
9542 && res.get_name () != nullptr
9543 && IS_ABSOLUTE_PATH (res.get_name ()))
9545 res.set_comp_dir (ldirname (res.get_name ()));
9546 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
9549 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9550 return *cu->per_cu->fnd;
9553 /* Handle DW_AT_stmt_list for a compilation unit.
9554 DIE is the DW_TAG_compile_unit die for CU.
9555 COMP_DIR is the compilation directory. LOWPC is passed to
9556 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9558 static void
9559 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9560 const file_and_directory &fnd, CORE_ADDR lowpc,
9561 bool have_code) /* ARI: editCase function */
9563 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9564 struct attribute *attr;
9565 hashval_t line_header_local_hash;
9566 void **slot;
9567 int decode_mapping;
9569 gdb_assert (! cu->per_cu->is_debug_types);
9571 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9572 if (attr == NULL || !attr->form_is_unsigned ())
9573 return;
9575 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9577 /* The line header hash table is only created if needed (it exists to
9578 prevent redundant reading of the line table for partial_units).
9579 If we're given a partial_unit, we'll need it. If we're given a
9580 compile_unit, then use the line header hash table if it's already
9581 created, but don't create one just yet. */
9583 if (per_objfile->line_header_hash == NULL
9584 && die->tag == DW_TAG_partial_unit)
9586 per_objfile->line_header_hash
9587 .reset (htab_create_alloc (127, line_header_hash_voidp,
9588 line_header_eq_voidp,
9589 htab_delete_entry<line_header>,
9590 xcalloc, xfree));
9593 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
9594 line_header_local_hash = line_header_hash (&line_header_local);
9595 if (per_objfile->line_header_hash != NULL)
9597 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9598 &line_header_local,
9599 line_header_local_hash, NO_INSERT);
9601 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9602 is not present in *SLOT (since if there is something in *SLOT then
9603 it will be for a partial_unit). */
9604 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9606 gdb_assert (*slot != NULL);
9607 cu->line_header = (struct line_header *) *slot;
9608 return;
9612 /* dwarf_decode_line_header does not yet provide sufficient information.
9613 We always have to call also dwarf_decode_lines for it. */
9614 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
9615 fnd.get_comp_dir ());
9616 if (lh == NULL)
9617 return;
9619 cu->line_header = lh.release ();
9620 cu->line_header_die_owner = die;
9622 if (per_objfile->line_header_hash == NULL)
9623 slot = NULL;
9624 else
9626 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9627 &line_header_local,
9628 line_header_local_hash, INSERT);
9629 gdb_assert (slot != NULL);
9631 if (slot != NULL && *slot == NULL)
9633 /* This newly decoded line number information unit will be owned
9634 by line_header_hash hash table. */
9635 *slot = cu->line_header;
9636 cu->line_header_die_owner = NULL;
9638 else
9640 /* We cannot free any current entry in (*slot) as that struct line_header
9641 may be already used by multiple CUs. Create only temporary decoded
9642 line_header for this CU - it may happen at most once for each line
9643 number information unit. And if we're not using line_header_hash
9644 then this is what we want as well. */
9645 gdb_assert (die->tag != DW_TAG_partial_unit);
9647 decode_mapping = (die->tag != DW_TAG_partial_unit);
9648 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
9649 then there won't be any interesting code in the CU, but a check later on
9650 (in lnp_state_machine::check_line_address) will fail to properly exclude
9651 an entry that was removed via --gc-sections. */
9652 if (have_code)
9653 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9656 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9658 static void
9659 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9661 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9662 struct objfile *objfile = per_objfile->objfile;
9663 struct gdbarch *gdbarch = objfile->arch ();
9664 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9665 CORE_ADDR highpc = ((CORE_ADDR) 0);
9666 struct attribute *attr;
9667 struct die_info *child_die;
9668 CORE_ADDR baseaddr;
9670 prepare_one_comp_unit (cu, die, cu->lang ());
9671 baseaddr = objfile->text_section_offset ();
9673 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9675 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9676 from finish_block. */
9677 if (lowpc == ((CORE_ADDR) -1))
9678 lowpc = highpc;
9679 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9681 file_and_directory &fnd = find_file_and_directory (die, cu);
9683 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9684 lowpc);
9686 gdb_assert (per_objfile->sym_cu == nullptr);
9687 scoped_restore restore_sym_cu
9688 = make_scoped_restore (&per_objfile->sym_cu, cu);
9690 /* Decode line number information if present. We do this before
9691 processing child DIEs, so that the line header table is available
9692 for DW_AT_decl_file. */
9693 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
9695 /* Process all dies in compilation unit. */
9696 if (die->child != NULL)
9698 child_die = die->child;
9699 while (child_die && child_die->tag)
9701 process_die (child_die, cu);
9702 child_die = child_die->sibling;
9705 per_objfile->sym_cu = nullptr;
9707 /* Decode macro information, if present. Dwarf 2 macro information
9708 refers to information in the line number info statement program
9709 header, so we can only read it if we've read the header
9710 successfully. */
9711 attr = dwarf2_attr (die, DW_AT_macros, cu);
9712 if (attr == NULL)
9713 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9714 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9716 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9717 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9719 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9721 else
9723 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9724 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9726 unsigned int macro_offset = attr->as_unsigned ();
9728 dwarf_decode_macros (cu, macro_offset, 0);
9733 void
9734 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9736 struct type_unit_group *tu_group;
9737 int first_time;
9738 struct attribute *attr;
9739 unsigned int i;
9740 struct signatured_type *sig_type;
9742 gdb_assert (per_cu->is_debug_types);
9743 sig_type = (struct signatured_type *) per_cu;
9745 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9747 /* If we're using .gdb_index (includes -readnow) then
9748 per_cu->type_unit_group may not have been set up yet. */
9749 if (sig_type->type_unit_group == NULL)
9750 sig_type->type_unit_group = get_type_unit_group (this, attr);
9751 tu_group = sig_type->type_unit_group;
9753 /* If we've already processed this stmt_list there's no real need to
9754 do it again, we could fake it and just recreate the part we need
9755 (file name,index -> symtab mapping). If data shows this optimization
9756 is useful we can do it then. */
9757 type_unit_group_unshareable *tug_unshare
9758 = per_objfile->get_type_unit_group_unshareable (tu_group);
9759 first_time = tug_unshare->compunit_symtab == NULL;
9761 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9762 debug info. */
9763 line_header_up lh;
9764 if (attr != NULL && attr->form_is_unsigned ())
9766 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9767 lh = dwarf_decode_line_header (line_offset, this, nullptr);
9769 if (lh == NULL)
9771 if (first_time)
9772 start_compunit_symtab ("", NULL, 0);
9773 else
9775 gdb_assert (tug_unshare->symtabs == NULL);
9776 gdb_assert (m_builder == nullptr);
9777 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9778 m_builder.reset (new struct buildsym_compunit
9779 (cust->objfile (), "",
9780 cust->dirname (),
9781 cust->language (),
9782 0, cust));
9783 list_in_scope = get_builder ()->get_file_symbols ();
9785 return;
9788 line_header = lh.release ();
9789 line_header_die_owner = die;
9791 if (first_time)
9793 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9795 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9796 still initializing it, and our caller (a few levels up)
9797 process_full_type_unit still needs to know if this is the first
9798 time. */
9800 tug_unshare->symtabs
9801 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9802 struct symtab *, line_header->file_names_size ());
9804 auto &file_names = line_header->file_names ();
9805 for (i = 0; i < file_names.size (); ++i)
9807 file_entry &fe = file_names[i];
9808 dwarf2_start_subfile (this, fe, *line_header);
9809 buildsym_compunit *b = get_builder ();
9810 subfile *sf = b->get_current_subfile ();
9812 if (sf->symtab == nullptr)
9814 /* NOTE: start_subfile will recognize when it's been
9815 passed a file it has already seen. So we can't
9816 assume there's a simple mapping from
9817 cu->line_header->file_names to subfiles, plus
9818 cu->line_header->file_names may contain dups. */
9819 const char *name = sf->name.c_str ();
9820 const char *name_for_id = sf->name_for_id.c_str ();
9821 sf->symtab = allocate_symtab (cust, name, name_for_id);
9824 fe.symtab = b->get_current_subfile ()->symtab;
9825 tug_unshare->symtabs[i] = fe.symtab;
9828 else
9830 gdb_assert (m_builder == nullptr);
9831 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9832 m_builder.reset (new struct buildsym_compunit
9833 (cust->objfile (), "",
9834 cust->dirname (),
9835 cust->language (),
9836 0, cust));
9837 list_in_scope = get_builder ()->get_file_symbols ();
9839 auto &file_names = line_header->file_names ();
9840 for (i = 0; i < file_names.size (); ++i)
9842 file_entry &fe = file_names[i];
9843 fe.symtab = tug_unshare->symtabs[i];
9847 /* The main symtab is allocated last. Type units don't have DW_AT_name
9848 so they don't have a "real" (so to speak) symtab anyway.
9849 There is later code that will assign the main symtab to all symbols
9850 that don't have one. We need to handle the case of a symbol with a
9851 missing symtab (DW_AT_decl_file) anyway. */
9854 /* Process DW_TAG_type_unit.
9855 For TUs we want to skip the first top level sibling if it's not the
9856 actual type being defined by this TU. In this case the first top
9857 level sibling is there to provide context only. */
9859 static void
9860 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9862 struct die_info *child_die;
9864 prepare_one_comp_unit (cu, die, language_minimal);
9866 /* Initialize (or reinitialize) the machinery for building symtabs.
9867 We do this before processing child DIEs, so that the line header table
9868 is available for DW_AT_decl_file. */
9869 cu->setup_type_unit_groups (die);
9871 if (die->child != NULL)
9873 child_die = die->child;
9874 while (child_die && child_die->tag)
9876 process_die (child_die, cu);
9877 child_die = child_die->sibling;
9882 /* DWO/DWP files.
9884 http://gcc.gnu.org/wiki/DebugFission
9885 http://gcc.gnu.org/wiki/DebugFissionDWP
9887 To simplify handling of both DWO files ("object" files with the DWARF info)
9888 and DWP files (a file with the DWOs packaged up into one file), we treat
9889 DWP files as having a collection of virtual DWO files. */
9891 static hashval_t
9892 hash_dwo_file (const void *item)
9894 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9895 hashval_t hash;
9897 hash = htab_hash_string (dwo_file->dwo_name);
9898 if (dwo_file->comp_dir != NULL)
9899 hash += htab_hash_string (dwo_file->comp_dir);
9900 return hash;
9903 static int
9904 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9906 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9907 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9909 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9910 return 0;
9911 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9912 return lhs->comp_dir == rhs->comp_dir;
9913 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9916 /* Allocate a hash table for DWO files. */
9918 static htab_up
9919 allocate_dwo_file_hash_table ()
9921 return htab_up (htab_create_alloc (41,
9922 hash_dwo_file,
9923 eq_dwo_file,
9924 htab_delete_entry<dwo_file>,
9925 xcalloc, xfree));
9928 /* Lookup DWO file DWO_NAME. */
9930 static void **
9931 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9932 const char *dwo_name,
9933 const char *comp_dir)
9935 struct dwo_file find_entry;
9936 void **slot;
9938 if (per_objfile->per_bfd->dwo_files == NULL)
9939 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9941 find_entry.dwo_name = dwo_name;
9942 find_entry.comp_dir = comp_dir;
9943 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9944 INSERT);
9946 return slot;
9949 static hashval_t
9950 hash_dwo_unit (const void *item)
9952 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9954 /* This drops the top 32 bits of the id, but is ok for a hash. */
9955 return dwo_unit->signature;
9958 static int
9959 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9961 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9962 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9964 /* The signature is assumed to be unique within the DWO file.
9965 So while object file CU dwo_id's always have the value zero,
9966 that's OK, assuming each object file DWO file has only one CU,
9967 and that's the rule for now. */
9968 return lhs->signature == rhs->signature;
9971 /* Allocate a hash table for DWO CUs,TUs.
9972 There is one of these tables for each of CUs,TUs for each DWO file. */
9974 static htab_up
9975 allocate_dwo_unit_table ()
9977 /* Start out with a pretty small number.
9978 Generally DWO files contain only one CU and maybe some TUs. */
9979 return htab_up (htab_create_alloc (3,
9980 hash_dwo_unit,
9981 eq_dwo_unit,
9982 NULL, xcalloc, xfree));
9985 /* die_reader_func for create_dwo_cu. */
9987 static void
9988 create_dwo_cu_reader (const struct die_reader_specs *reader,
9989 const gdb_byte *info_ptr,
9990 struct die_info *comp_unit_die,
9991 struct dwo_file *dwo_file,
9992 struct dwo_unit *dwo_unit)
9994 struct dwarf2_cu *cu = reader->cu;
9995 sect_offset sect_off = cu->per_cu->sect_off;
9996 struct dwarf2_section_info *section = cu->per_cu->section;
9998 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9999 if (!signature.has_value ())
10001 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10002 " its dwo_id [in module %s]"),
10003 sect_offset_str (sect_off), dwo_file->dwo_name);
10004 return;
10007 dwo_unit->dwo_file = dwo_file;
10008 dwo_unit->signature = *signature;
10009 dwo_unit->section = section;
10010 dwo_unit->sect_off = sect_off;
10011 dwo_unit->length = cu->per_cu->length ();
10013 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10014 sect_offset_str (sect_off),
10015 hex_string (dwo_unit->signature));
10018 /* Create the dwo_units for the CUs in a DWO_FILE.
10019 Note: This function processes DWO files only, not DWP files. */
10021 static void
10022 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10023 dwarf2_cu *cu, struct dwo_file &dwo_file,
10024 dwarf2_section_info &section, htab_up &cus_htab)
10026 struct objfile *objfile = per_objfile->objfile;
10027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10028 const gdb_byte *info_ptr, *end_ptr;
10030 section.read (objfile);
10031 info_ptr = section.buffer;
10033 if (info_ptr == NULL)
10034 return;
10036 dwarf_read_debug_printf ("Reading %s for %s:",
10037 section.get_name (),
10038 section.get_file_name ());
10040 end_ptr = info_ptr + section.size;
10041 while (info_ptr < end_ptr)
10043 struct dwarf2_per_cu_data per_cu;
10044 struct dwo_unit read_unit {};
10045 struct dwo_unit *dwo_unit;
10046 void **slot;
10047 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10049 per_cu.per_bfd = per_bfd;
10050 per_cu.is_debug_types = 0;
10051 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10052 per_cu.section = &section;
10054 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10055 if (!reader.dummy_p)
10056 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10057 &dwo_file, &read_unit);
10058 info_ptr += per_cu.length ();
10060 // If the unit could not be parsed, skip it.
10061 if (read_unit.dwo_file == NULL)
10062 continue;
10064 if (cus_htab == NULL)
10065 cus_htab = allocate_dwo_unit_table ();
10067 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10068 struct dwo_unit);
10069 *dwo_unit = read_unit;
10070 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10071 gdb_assert (slot != NULL);
10072 if (*slot != NULL)
10074 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10075 sect_offset dup_sect_off = dup_cu->sect_off;
10077 complaint (_("debug cu entry at offset %s is duplicate to"
10078 " the entry at offset %s, signature %s"),
10079 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10080 hex_string (dwo_unit->signature));
10082 *slot = (void *)dwo_unit;
10086 /* DWP file .debug_{cu,tu}_index section format:
10087 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10088 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10090 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10091 officially standard DWP format was published with DWARF v5 and is called
10092 Version 5. There are no versions 3 or 4.
10094 DWP Version 1:
10096 Both index sections have the same format, and serve to map a 64-bit
10097 signature to a set of section numbers. Each section begins with a header,
10098 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10099 indexes, and a pool of 32-bit section numbers. The index sections will be
10100 aligned at 8-byte boundaries in the file.
10102 The index section header consists of:
10104 V, 32 bit version number
10105 -, 32 bits unused
10106 N, 32 bit number of compilation units or type units in the index
10107 M, 32 bit number of slots in the hash table
10109 Numbers are recorded using the byte order of the application binary.
10111 The hash table begins at offset 16 in the section, and consists of an array
10112 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10113 order of the application binary). Unused slots in the hash table are 0.
10114 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10116 The parallel table begins immediately after the hash table
10117 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10118 array of 32-bit indexes (using the byte order of the application binary),
10119 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10120 table contains a 32-bit index into the pool of section numbers. For unused
10121 hash table slots, the corresponding entry in the parallel table will be 0.
10123 The pool of section numbers begins immediately following the hash table
10124 (at offset 16 + 12 * M from the beginning of the section). The pool of
10125 section numbers consists of an array of 32-bit words (using the byte order
10126 of the application binary). Each item in the array is indexed starting
10127 from 0. The hash table entry provides the index of the first section
10128 number in the set. Additional section numbers in the set follow, and the
10129 set is terminated by a 0 entry (section number 0 is not used in ELF).
10131 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10132 section must be the first entry in the set, and the .debug_abbrev.dwo must
10133 be the second entry. Other members of the set may follow in any order.
10137 DWP Versions 2 and 5:
10139 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10140 and the entries in the index tables are now offsets into these sections.
10141 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10142 section.
10144 Index Section Contents:
10145 Header
10146 Hash Table of Signatures dwp_hash_table.hash_table
10147 Parallel Table of Indices dwp_hash_table.unit_table
10148 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10149 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10151 The index section header consists of:
10153 V, 32 bit version number
10154 L, 32 bit number of columns in the table of section offsets
10155 N, 32 bit number of compilation units or type units in the index
10156 M, 32 bit number of slots in the hash table
10158 Numbers are recorded using the byte order of the application binary.
10160 The hash table has the same format as version 1.
10161 The parallel table of indices has the same format as version 1,
10162 except that the entries are origin-1 indices into the table of sections
10163 offsets and the table of section sizes.
10165 The table of offsets begins immediately following the parallel table
10166 (at offset 16 + 12 * M from the beginning of the section). The table is
10167 a two-dimensional array of 32-bit words (using the byte order of the
10168 application binary), with L columns and N+1 rows, in row-major order.
10169 Each row in the array is indexed starting from 0. The first row provides
10170 a key to the remaining rows: each column in this row provides an identifier
10171 for a debug section, and the offsets in the same column of subsequent rows
10172 refer to that section. The section identifiers for Version 2 are:
10174 DW_SECT_INFO 1 .debug_info.dwo
10175 DW_SECT_TYPES 2 .debug_types.dwo
10176 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10177 DW_SECT_LINE 4 .debug_line.dwo
10178 DW_SECT_LOC 5 .debug_loc.dwo
10179 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10180 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10181 DW_SECT_MACRO 8 .debug_macro.dwo
10183 The section identifiers for Version 5 are:
10185 DW_SECT_INFO_V5 1 .debug_info.dwo
10186 DW_SECT_RESERVED_V5 2 --
10187 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10188 DW_SECT_LINE_V5 4 .debug_line.dwo
10189 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10190 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10191 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10192 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10194 The offsets provided by the CU and TU index sections are the base offsets
10195 for the contributions made by each CU or TU to the corresponding section
10196 in the package file. Each CU and TU header contains an abbrev_offset
10197 field, used to find the abbreviations table for that CU or TU within the
10198 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10199 be interpreted as relative to the base offset given in the index section.
10200 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10201 should be interpreted as relative to the base offset for .debug_line.dwo,
10202 and offsets into other debug sections obtained from DWARF attributes should
10203 also be interpreted as relative to the corresponding base offset.
10205 The table of sizes begins immediately following the table of offsets.
10206 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10207 with L columns and N rows, in row-major order. Each row in the array is
10208 indexed starting from 1 (row 0 is shared by the two tables).
10212 Hash table lookup is handled the same in version 1 and 2:
10214 We assume that N and M will not exceed 2^32 - 1.
10215 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10217 Given a 64-bit compilation unit signature or a type signature S, an entry
10218 in the hash table is located as follows:
10220 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10221 the low-order k bits all set to 1.
10223 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10225 3) If the hash table entry at index H matches the signature, use that
10226 entry. If the hash table entry at index H is unused (all zeroes),
10227 terminate the search: the signature is not present in the table.
10229 4) Let H = (H + H') modulo M. Repeat at Step 3.
10231 Because M > N and H' and M are relatively prime, the search is guaranteed
10232 to stop at an unused slot or find the match. */
10234 /* Create a hash table to map DWO IDs to their CU/TU entry in
10235 .debug_{info,types}.dwo in DWP_FILE.
10236 Returns NULL if there isn't one.
10237 Note: This function processes DWP files only, not DWO files. */
10239 static struct dwp_hash_table *
10240 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10241 struct dwp_file *dwp_file, int is_debug_types)
10243 struct objfile *objfile = per_objfile->objfile;
10244 bfd *dbfd = dwp_file->dbfd.get ();
10245 const gdb_byte *index_ptr, *index_end;
10246 struct dwarf2_section_info *index;
10247 uint32_t version, nr_columns, nr_units, nr_slots;
10248 struct dwp_hash_table *htab;
10250 if (is_debug_types)
10251 index = &dwp_file->sections.tu_index;
10252 else
10253 index = &dwp_file->sections.cu_index;
10255 if (index->empty ())
10256 return NULL;
10257 index->read (objfile);
10259 index_ptr = index->buffer;
10260 index_end = index_ptr + index->size;
10262 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10263 For now it's safe to just read 4 bytes (particularly as it's difficult to
10264 tell if you're dealing with Version 5 before you've read the version). */
10265 version = read_4_bytes (dbfd, index_ptr);
10266 index_ptr += 4;
10267 if (version == 2 || version == 5)
10268 nr_columns = read_4_bytes (dbfd, index_ptr);
10269 else
10270 nr_columns = 0;
10271 index_ptr += 4;
10272 nr_units = read_4_bytes (dbfd, index_ptr);
10273 index_ptr += 4;
10274 nr_slots = read_4_bytes (dbfd, index_ptr);
10275 index_ptr += 4;
10277 if (version != 1 && version != 2 && version != 5)
10279 error (_("Dwarf Error: unsupported DWP file version (%s)"
10280 " [in module %s]"),
10281 pulongest (version), dwp_file->name);
10283 if (nr_slots != (nr_slots & -nr_slots))
10285 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10286 " is not power of 2 [in module %s]"),
10287 pulongest (nr_slots), dwp_file->name);
10290 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10291 htab->version = version;
10292 htab->nr_columns = nr_columns;
10293 htab->nr_units = nr_units;
10294 htab->nr_slots = nr_slots;
10295 htab->hash_table = index_ptr;
10296 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10298 /* Exit early if the table is empty. */
10299 if (nr_slots == 0 || nr_units == 0
10300 || (version == 2 && nr_columns == 0)
10301 || (version == 5 && nr_columns == 0))
10303 /* All must be zero. */
10304 if (nr_slots != 0 || nr_units != 0
10305 || (version == 2 && nr_columns != 0)
10306 || (version == 5 && nr_columns != 0))
10308 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10309 " all zero [in modules %s]"),
10310 dwp_file->name);
10312 return htab;
10315 if (version == 1)
10317 htab->section_pool.v1.indices =
10318 htab->unit_table + sizeof (uint32_t) * nr_slots;
10319 /* It's harder to decide whether the section is too small in v1.
10320 V1 is deprecated anyway so we punt. */
10322 else if (version == 2)
10324 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10325 int *ids = htab->section_pool.v2.section_ids;
10326 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10327 /* Reverse map for error checking. */
10328 int ids_seen[DW_SECT_MAX + 1];
10329 int i;
10331 if (nr_columns < 2)
10333 error (_("Dwarf Error: bad DWP hash table, too few columns"
10334 " in section table [in module %s]"),
10335 dwp_file->name);
10337 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10339 error (_("Dwarf Error: bad DWP hash table, too many columns"
10340 " in section table [in module %s]"),
10341 dwp_file->name);
10343 memset (ids, 255, sizeof_ids);
10344 memset (ids_seen, 255, sizeof (ids_seen));
10345 for (i = 0; i < nr_columns; ++i)
10347 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10349 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10351 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10352 " in section table [in module %s]"),
10353 id, dwp_file->name);
10355 if (ids_seen[id] != -1)
10357 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10358 " id %d in section table [in module %s]"),
10359 id, dwp_file->name);
10361 ids_seen[id] = i;
10362 ids[i] = id;
10364 /* Must have exactly one info or types section. */
10365 if (((ids_seen[DW_SECT_INFO] != -1)
10366 + (ids_seen[DW_SECT_TYPES] != -1))
10367 != 1)
10369 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10370 " DWO info/types section [in module %s]"),
10371 dwp_file->name);
10373 /* Must have an abbrev section. */
10374 if (ids_seen[DW_SECT_ABBREV] == -1)
10376 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10377 " section [in module %s]"),
10378 dwp_file->name);
10380 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10381 htab->section_pool.v2.sizes =
10382 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10383 * nr_units * nr_columns);
10384 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10385 * nr_units * nr_columns))
10386 > index_end)
10388 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10389 " [in module %s]"),
10390 dwp_file->name);
10393 else /* version == 5 */
10395 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10396 int *ids = htab->section_pool.v5.section_ids;
10397 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10398 /* Reverse map for error checking. */
10399 int ids_seen[DW_SECT_MAX_V5 + 1];
10401 if (nr_columns < 2)
10403 error (_("Dwarf Error: bad DWP hash table, too few columns"
10404 " in section table [in module %s]"),
10405 dwp_file->name);
10407 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10409 error (_("Dwarf Error: bad DWP hash table, too many columns"
10410 " in section table [in module %s]"),
10411 dwp_file->name);
10413 memset (ids, 255, sizeof_ids);
10414 memset (ids_seen, 255, sizeof (ids_seen));
10415 for (int i = 0; i < nr_columns; ++i)
10417 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10419 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10421 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10422 " in section table [in module %s]"),
10423 id, dwp_file->name);
10425 if (ids_seen[id] != -1)
10427 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10428 " id %d in section table [in module %s]"),
10429 id, dwp_file->name);
10431 ids_seen[id] = i;
10432 ids[i] = id;
10434 /* Must have seen an info section. */
10435 if (ids_seen[DW_SECT_INFO_V5] == -1)
10437 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10438 " DWO info/types section [in module %s]"),
10439 dwp_file->name);
10441 /* Must have an abbrev section. */
10442 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10444 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10445 " section [in module %s]"),
10446 dwp_file->name);
10448 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10449 htab->section_pool.v5.sizes
10450 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10451 * nr_units * nr_columns);
10452 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10453 * nr_units * nr_columns))
10454 > index_end)
10456 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10457 " [in module %s]"),
10458 dwp_file->name);
10462 return htab;
10465 /* Update SECTIONS with the data from SECTP.
10467 This function is like the other "locate" section routines, but in
10468 this context the sections to read comes from the DWP V1 hash table,
10469 not the full ELF section table.
10471 The result is non-zero for success, or zero if an error was found. */
10473 static int
10474 locate_v1_virtual_dwo_sections (asection *sectp,
10475 struct virtual_v1_dwo_sections *sections)
10477 const struct dwop_section_names *names = &dwop_section_names;
10479 if (names->abbrev_dwo.matches (sectp->name))
10481 /* There can be only one. */
10482 if (sections->abbrev.s.section != NULL)
10483 return 0;
10484 sections->abbrev.s.section = sectp;
10485 sections->abbrev.size = bfd_section_size (sectp);
10487 else if (names->info_dwo.matches (sectp->name)
10488 || names->types_dwo.matches (sectp->name))
10490 /* There can be only one. */
10491 if (sections->info_or_types.s.section != NULL)
10492 return 0;
10493 sections->info_or_types.s.section = sectp;
10494 sections->info_or_types.size = bfd_section_size (sectp);
10496 else if (names->line_dwo.matches (sectp->name))
10498 /* There can be only one. */
10499 if (sections->line.s.section != NULL)
10500 return 0;
10501 sections->line.s.section = sectp;
10502 sections->line.size = bfd_section_size (sectp);
10504 else if (names->loc_dwo.matches (sectp->name))
10506 /* There can be only one. */
10507 if (sections->loc.s.section != NULL)
10508 return 0;
10509 sections->loc.s.section = sectp;
10510 sections->loc.size = bfd_section_size (sectp);
10512 else if (names->macinfo_dwo.matches (sectp->name))
10514 /* There can be only one. */
10515 if (sections->macinfo.s.section != NULL)
10516 return 0;
10517 sections->macinfo.s.section = sectp;
10518 sections->macinfo.size = bfd_section_size (sectp);
10520 else if (names->macro_dwo.matches (sectp->name))
10522 /* There can be only one. */
10523 if (sections->macro.s.section != NULL)
10524 return 0;
10525 sections->macro.s.section = sectp;
10526 sections->macro.size = bfd_section_size (sectp);
10528 else if (names->str_offsets_dwo.matches (sectp->name))
10530 /* There can be only one. */
10531 if (sections->str_offsets.s.section != NULL)
10532 return 0;
10533 sections->str_offsets.s.section = sectp;
10534 sections->str_offsets.size = bfd_section_size (sectp);
10536 else
10538 /* No other kind of section is valid. */
10539 return 0;
10542 return 1;
10545 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10546 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10547 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10548 This is for DWP version 1 files. */
10550 static struct dwo_unit *
10551 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10552 struct dwp_file *dwp_file,
10553 uint32_t unit_index,
10554 const char *comp_dir,
10555 ULONGEST signature, int is_debug_types)
10557 const struct dwp_hash_table *dwp_htab =
10558 is_debug_types ? dwp_file->tus : dwp_file->cus;
10559 bfd *dbfd = dwp_file->dbfd.get ();
10560 const char *kind = is_debug_types ? "TU" : "CU";
10561 struct dwo_file *dwo_file;
10562 struct dwo_unit *dwo_unit;
10563 struct virtual_v1_dwo_sections sections;
10564 void **dwo_file_slot;
10565 int i;
10567 gdb_assert (dwp_file->version == 1);
10569 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10570 kind, pulongest (unit_index), hex_string (signature),
10571 dwp_file->name);
10573 /* Fetch the sections of this DWO unit.
10574 Put a limit on the number of sections we look for so that bad data
10575 doesn't cause us to loop forever. */
10577 #define MAX_NR_V1_DWO_SECTIONS \
10578 (1 /* .debug_info or .debug_types */ \
10579 + 1 /* .debug_abbrev */ \
10580 + 1 /* .debug_line */ \
10581 + 1 /* .debug_loc */ \
10582 + 1 /* .debug_str_offsets */ \
10583 + 1 /* .debug_macro or .debug_macinfo */ \
10584 + 1 /* trailing zero */)
10586 memset (&sections, 0, sizeof (sections));
10588 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10590 asection *sectp;
10591 uint32_t section_nr =
10592 read_4_bytes (dbfd,
10593 dwp_htab->section_pool.v1.indices
10594 + (unit_index + i) * sizeof (uint32_t));
10596 if (section_nr == 0)
10597 break;
10598 if (section_nr >= dwp_file->num_sections)
10600 error (_("Dwarf Error: bad DWP hash table, section number too large"
10601 " [in module %s]"),
10602 dwp_file->name);
10605 sectp = dwp_file->elf_sections[section_nr];
10606 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10608 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10609 " [in module %s]"),
10610 dwp_file->name);
10614 if (i < 2
10615 || sections.info_or_types.empty ()
10616 || sections.abbrev.empty ())
10618 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10619 " [in module %s]"),
10620 dwp_file->name);
10622 if (i == MAX_NR_V1_DWO_SECTIONS)
10624 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10625 " [in module %s]"),
10626 dwp_file->name);
10629 /* It's easier for the rest of the code if we fake a struct dwo_file and
10630 have dwo_unit "live" in that. At least for now.
10632 The DWP file can be made up of a random collection of CUs and TUs.
10633 However, for each CU + set of TUs that came from the same original DWO
10634 file, we can combine them back into a virtual DWO file to save space
10635 (fewer struct dwo_file objects to allocate). Remember that for really
10636 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10638 std::string virtual_dwo_name =
10639 string_printf ("virtual-dwo/%d-%d-%d-%d",
10640 sections.abbrev.get_id (),
10641 sections.line.get_id (),
10642 sections.loc.get_id (),
10643 sections.str_offsets.get_id ());
10644 /* Can we use an existing virtual DWO file? */
10645 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10646 comp_dir);
10647 /* Create one if necessary. */
10648 if (*dwo_file_slot == NULL)
10650 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10651 virtual_dwo_name.c_str ());
10653 dwo_file = new struct dwo_file;
10654 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10655 dwo_file->comp_dir = comp_dir;
10656 dwo_file->sections.abbrev = sections.abbrev;
10657 dwo_file->sections.line = sections.line;
10658 dwo_file->sections.loc = sections.loc;
10659 dwo_file->sections.macinfo = sections.macinfo;
10660 dwo_file->sections.macro = sections.macro;
10661 dwo_file->sections.str_offsets = sections.str_offsets;
10662 /* The "str" section is global to the entire DWP file. */
10663 dwo_file->sections.str = dwp_file->sections.str;
10664 /* The info or types section is assigned below to dwo_unit,
10665 there's no need to record it in dwo_file.
10666 Also, we can't simply record type sections in dwo_file because
10667 we record a pointer into the vector in dwo_unit. As we collect more
10668 types we'll grow the vector and eventually have to reallocate space
10669 for it, invalidating all copies of pointers into the previous
10670 contents. */
10671 *dwo_file_slot = dwo_file;
10673 else
10675 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10676 virtual_dwo_name.c_str ());
10678 dwo_file = (struct dwo_file *) *dwo_file_slot;
10681 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10682 dwo_unit->dwo_file = dwo_file;
10683 dwo_unit->signature = signature;
10684 dwo_unit->section =
10685 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10686 *dwo_unit->section = sections.info_or_types;
10687 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10689 return dwo_unit;
10692 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10693 simplify them. Given a pointer to the containing section SECTION, and
10694 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10695 virtual section of just that piece. */
10697 static struct dwarf2_section_info
10698 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10699 struct dwarf2_section_info *section,
10700 bfd_size_type offset, bfd_size_type size)
10702 struct dwarf2_section_info result;
10703 asection *sectp;
10705 gdb_assert (section != NULL);
10706 gdb_assert (!section->is_virtual);
10708 memset (&result, 0, sizeof (result));
10709 result.s.containing_section = section;
10710 result.is_virtual = true;
10712 if (size == 0)
10713 return result;
10715 sectp = section->get_bfd_section ();
10717 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10718 bounds of the real section. This is a pretty-rare event, so just
10719 flag an error (easier) instead of a warning and trying to cope. */
10720 if (sectp == NULL
10721 || offset + size > bfd_section_size (sectp))
10723 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10724 " in section %s [in module %s]"),
10725 sectp ? bfd_section_name (sectp) : "<unknown>",
10726 objfile_name (per_objfile->objfile));
10729 result.virtual_offset = offset;
10730 result.size = size;
10731 return result;
10734 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10735 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10736 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10737 This is for DWP version 2 files. */
10739 static struct dwo_unit *
10740 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10741 struct dwp_file *dwp_file,
10742 uint32_t unit_index,
10743 const char *comp_dir,
10744 ULONGEST signature, int is_debug_types)
10746 const struct dwp_hash_table *dwp_htab =
10747 is_debug_types ? dwp_file->tus : dwp_file->cus;
10748 bfd *dbfd = dwp_file->dbfd.get ();
10749 const char *kind = is_debug_types ? "TU" : "CU";
10750 struct dwo_file *dwo_file;
10751 struct dwo_unit *dwo_unit;
10752 struct virtual_v2_or_v5_dwo_sections sections;
10753 void **dwo_file_slot;
10754 int i;
10756 gdb_assert (dwp_file->version == 2);
10758 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10759 kind, pulongest (unit_index), hex_string (signature),
10760 dwp_file->name);
10762 /* Fetch the section offsets of this DWO unit. */
10764 memset (&sections, 0, sizeof (sections));
10766 for (i = 0; i < dwp_htab->nr_columns; ++i)
10768 uint32_t offset = read_4_bytes (dbfd,
10769 dwp_htab->section_pool.v2.offsets
10770 + (((unit_index - 1) * dwp_htab->nr_columns
10771 + i)
10772 * sizeof (uint32_t)));
10773 uint32_t size = read_4_bytes (dbfd,
10774 dwp_htab->section_pool.v2.sizes
10775 + (((unit_index - 1) * dwp_htab->nr_columns
10776 + i)
10777 * sizeof (uint32_t)));
10779 switch (dwp_htab->section_pool.v2.section_ids[i])
10781 case DW_SECT_INFO:
10782 case DW_SECT_TYPES:
10783 sections.info_or_types_offset = offset;
10784 sections.info_or_types_size = size;
10785 break;
10786 case DW_SECT_ABBREV:
10787 sections.abbrev_offset = offset;
10788 sections.abbrev_size = size;
10789 break;
10790 case DW_SECT_LINE:
10791 sections.line_offset = offset;
10792 sections.line_size = size;
10793 break;
10794 case DW_SECT_LOC:
10795 sections.loc_offset = offset;
10796 sections.loc_size = size;
10797 break;
10798 case DW_SECT_STR_OFFSETS:
10799 sections.str_offsets_offset = offset;
10800 sections.str_offsets_size = size;
10801 break;
10802 case DW_SECT_MACINFO:
10803 sections.macinfo_offset = offset;
10804 sections.macinfo_size = size;
10805 break;
10806 case DW_SECT_MACRO:
10807 sections.macro_offset = offset;
10808 sections.macro_size = size;
10809 break;
10813 /* It's easier for the rest of the code if we fake a struct dwo_file and
10814 have dwo_unit "live" in that. At least for now.
10816 The DWP file can be made up of a random collection of CUs and TUs.
10817 However, for each CU + set of TUs that came from the same original DWO
10818 file, we can combine them back into a virtual DWO file to save space
10819 (fewer struct dwo_file objects to allocate). Remember that for really
10820 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10822 std::string virtual_dwo_name =
10823 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10824 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10825 (long) (sections.line_size ? sections.line_offset : 0),
10826 (long) (sections.loc_size ? sections.loc_offset : 0),
10827 (long) (sections.str_offsets_size
10828 ? sections.str_offsets_offset : 0));
10829 /* Can we use an existing virtual DWO file? */
10830 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10831 comp_dir);
10832 /* Create one if necessary. */
10833 if (*dwo_file_slot == NULL)
10835 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10836 virtual_dwo_name.c_str ());
10838 dwo_file = new struct dwo_file;
10839 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10840 dwo_file->comp_dir = comp_dir;
10841 dwo_file->sections.abbrev =
10842 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10843 sections.abbrev_offset,
10844 sections.abbrev_size);
10845 dwo_file->sections.line =
10846 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10847 sections.line_offset,
10848 sections.line_size);
10849 dwo_file->sections.loc =
10850 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10851 sections.loc_offset, sections.loc_size);
10852 dwo_file->sections.macinfo =
10853 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10854 sections.macinfo_offset,
10855 sections.macinfo_size);
10856 dwo_file->sections.macro =
10857 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10858 sections.macro_offset,
10859 sections.macro_size);
10860 dwo_file->sections.str_offsets =
10861 create_dwp_v2_or_v5_section (per_objfile,
10862 &dwp_file->sections.str_offsets,
10863 sections.str_offsets_offset,
10864 sections.str_offsets_size);
10865 /* The "str" section is global to the entire DWP file. */
10866 dwo_file->sections.str = dwp_file->sections.str;
10867 /* The info or types section is assigned below to dwo_unit,
10868 there's no need to record it in dwo_file.
10869 Also, we can't simply record type sections in dwo_file because
10870 we record a pointer into the vector in dwo_unit. As we collect more
10871 types we'll grow the vector and eventually have to reallocate space
10872 for it, invalidating all copies of pointers into the previous
10873 contents. */
10874 *dwo_file_slot = dwo_file;
10876 else
10878 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10879 virtual_dwo_name.c_str ());
10881 dwo_file = (struct dwo_file *) *dwo_file_slot;
10884 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10885 dwo_unit->dwo_file = dwo_file;
10886 dwo_unit->signature = signature;
10887 dwo_unit->section =
10888 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10889 *dwo_unit->section = create_dwp_v2_or_v5_section
10890 (per_objfile,
10891 is_debug_types
10892 ? &dwp_file->sections.types
10893 : &dwp_file->sections.info,
10894 sections.info_or_types_offset,
10895 sections.info_or_types_size);
10896 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10898 return dwo_unit;
10901 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10902 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10903 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10904 This is for DWP version 5 files. */
10906 static struct dwo_unit *
10907 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10908 struct dwp_file *dwp_file,
10909 uint32_t unit_index,
10910 const char *comp_dir,
10911 ULONGEST signature, int is_debug_types)
10913 const struct dwp_hash_table *dwp_htab
10914 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10915 bfd *dbfd = dwp_file->dbfd.get ();
10916 const char *kind = is_debug_types ? "TU" : "CU";
10917 struct dwo_file *dwo_file;
10918 struct dwo_unit *dwo_unit;
10919 struct virtual_v2_or_v5_dwo_sections sections {};
10920 void **dwo_file_slot;
10922 gdb_assert (dwp_file->version == 5);
10924 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10925 kind, pulongest (unit_index), hex_string (signature),
10926 dwp_file->name);
10928 /* Fetch the section offsets of this DWO unit. */
10930 /* memset (&sections, 0, sizeof (sections)); */
10932 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10934 uint32_t offset = read_4_bytes (dbfd,
10935 dwp_htab->section_pool.v5.offsets
10936 + (((unit_index - 1)
10937 * dwp_htab->nr_columns
10938 + i)
10939 * sizeof (uint32_t)));
10940 uint32_t size = read_4_bytes (dbfd,
10941 dwp_htab->section_pool.v5.sizes
10942 + (((unit_index - 1) * dwp_htab->nr_columns
10943 + i)
10944 * sizeof (uint32_t)));
10946 switch (dwp_htab->section_pool.v5.section_ids[i])
10948 case DW_SECT_ABBREV_V5:
10949 sections.abbrev_offset = offset;
10950 sections.abbrev_size = size;
10951 break;
10952 case DW_SECT_INFO_V5:
10953 sections.info_or_types_offset = offset;
10954 sections.info_or_types_size = size;
10955 break;
10956 case DW_SECT_LINE_V5:
10957 sections.line_offset = offset;
10958 sections.line_size = size;
10959 break;
10960 case DW_SECT_LOCLISTS_V5:
10961 sections.loclists_offset = offset;
10962 sections.loclists_size = size;
10963 break;
10964 case DW_SECT_MACRO_V5:
10965 sections.macro_offset = offset;
10966 sections.macro_size = size;
10967 break;
10968 case DW_SECT_RNGLISTS_V5:
10969 sections.rnglists_offset = offset;
10970 sections.rnglists_size = size;
10971 break;
10972 case DW_SECT_STR_OFFSETS_V5:
10973 sections.str_offsets_offset = offset;
10974 sections.str_offsets_size = size;
10975 break;
10976 case DW_SECT_RESERVED_V5:
10977 default:
10978 break;
10982 /* It's easier for the rest of the code if we fake a struct dwo_file and
10983 have dwo_unit "live" in that. At least for now.
10985 The DWP file can be made up of a random collection of CUs and TUs.
10986 However, for each CU + set of TUs that came from the same original DWO
10987 file, we can combine them back into a virtual DWO file to save space
10988 (fewer struct dwo_file objects to allocate). Remember that for really
10989 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10991 std::string virtual_dwo_name =
10992 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10993 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10994 (long) (sections.line_size ? sections.line_offset : 0),
10995 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10996 (long) (sections.str_offsets_size
10997 ? sections.str_offsets_offset : 0),
10998 (long) (sections.macro_size ? sections.macro_offset : 0),
10999 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11000 /* Can we use an existing virtual DWO file? */
11001 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11002 virtual_dwo_name.c_str (),
11003 comp_dir);
11004 /* Create one if necessary. */
11005 if (*dwo_file_slot == NULL)
11007 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11008 virtual_dwo_name.c_str ());
11010 dwo_file = new struct dwo_file;
11011 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11012 dwo_file->comp_dir = comp_dir;
11013 dwo_file->sections.abbrev =
11014 create_dwp_v2_or_v5_section (per_objfile,
11015 &dwp_file->sections.abbrev,
11016 sections.abbrev_offset,
11017 sections.abbrev_size);
11018 dwo_file->sections.line =
11019 create_dwp_v2_or_v5_section (per_objfile,
11020 &dwp_file->sections.line,
11021 sections.line_offset, sections.line_size);
11022 dwo_file->sections.macro =
11023 create_dwp_v2_or_v5_section (per_objfile,
11024 &dwp_file->sections.macro,
11025 sections.macro_offset,
11026 sections.macro_size);
11027 dwo_file->sections.loclists =
11028 create_dwp_v2_or_v5_section (per_objfile,
11029 &dwp_file->sections.loclists,
11030 sections.loclists_offset,
11031 sections.loclists_size);
11032 dwo_file->sections.rnglists =
11033 create_dwp_v2_or_v5_section (per_objfile,
11034 &dwp_file->sections.rnglists,
11035 sections.rnglists_offset,
11036 sections.rnglists_size);
11037 dwo_file->sections.str_offsets =
11038 create_dwp_v2_or_v5_section (per_objfile,
11039 &dwp_file->sections.str_offsets,
11040 sections.str_offsets_offset,
11041 sections.str_offsets_size);
11042 /* The "str" section is global to the entire DWP file. */
11043 dwo_file->sections.str = dwp_file->sections.str;
11044 /* The info or types section is assigned below to dwo_unit,
11045 there's no need to record it in dwo_file.
11046 Also, we can't simply record type sections in dwo_file because
11047 we record a pointer into the vector in dwo_unit. As we collect more
11048 types we'll grow the vector and eventually have to reallocate space
11049 for it, invalidating all copies of pointers into the previous
11050 contents. */
11051 *dwo_file_slot = dwo_file;
11053 else
11055 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11056 virtual_dwo_name.c_str ());
11058 dwo_file = (struct dwo_file *) *dwo_file_slot;
11061 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11062 dwo_unit->dwo_file = dwo_file;
11063 dwo_unit->signature = signature;
11064 dwo_unit->section
11065 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11066 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11067 &dwp_file->sections.info,
11068 sections.info_or_types_offset,
11069 sections.info_or_types_size);
11070 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11072 return dwo_unit;
11075 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11076 Returns NULL if the signature isn't found. */
11078 static struct dwo_unit *
11079 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11080 struct dwp_file *dwp_file, const char *comp_dir,
11081 ULONGEST signature, int is_debug_types)
11083 const struct dwp_hash_table *dwp_htab =
11084 is_debug_types ? dwp_file->tus : dwp_file->cus;
11085 bfd *dbfd = dwp_file->dbfd.get ();
11086 uint32_t mask = dwp_htab->nr_slots - 1;
11087 uint32_t hash = signature & mask;
11088 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11089 unsigned int i;
11090 void **slot;
11091 struct dwo_unit find_dwo_cu;
11093 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11094 find_dwo_cu.signature = signature;
11095 slot = htab_find_slot (is_debug_types
11096 ? dwp_file->loaded_tus.get ()
11097 : dwp_file->loaded_cus.get (),
11098 &find_dwo_cu, INSERT);
11100 if (*slot != NULL)
11101 return (struct dwo_unit *) *slot;
11103 /* Use a for loop so that we don't loop forever on bad debug info. */
11104 for (i = 0; i < dwp_htab->nr_slots; ++i)
11106 ULONGEST signature_in_table;
11108 signature_in_table =
11109 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11110 if (signature_in_table == signature)
11112 uint32_t unit_index =
11113 read_4_bytes (dbfd,
11114 dwp_htab->unit_table + hash * sizeof (uint32_t));
11116 if (dwp_file->version == 1)
11118 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11119 unit_index, comp_dir,
11120 signature, is_debug_types);
11122 else if (dwp_file->version == 2)
11124 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11125 unit_index, comp_dir,
11126 signature, is_debug_types);
11128 else /* version == 5 */
11130 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11131 unit_index, comp_dir,
11132 signature, is_debug_types);
11134 return (struct dwo_unit *) *slot;
11136 if (signature_in_table == 0)
11137 return NULL;
11138 hash = (hash + hash2) & mask;
11141 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11142 " [in module %s]"),
11143 dwp_file->name);
11146 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11147 Open the file specified by FILE_NAME and hand it off to BFD for
11148 preliminary analysis. Return a newly initialized bfd *, which
11149 includes a canonicalized copy of FILE_NAME.
11150 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11151 SEARCH_CWD is true if the current directory is to be searched.
11152 It will be searched before debug-file-directory.
11153 If successful, the file is added to the bfd include table of the
11154 objfile's bfd (see gdb_bfd_record_inclusion).
11155 If unable to find/open the file, return NULL.
11156 NOTE: This function is derived from symfile_bfd_open. */
11158 static gdb_bfd_ref_ptr
11159 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11160 const char *file_name, int is_dwp, int search_cwd)
11162 int desc;
11163 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11164 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11165 to debug_file_directory. */
11166 const char *search_path;
11167 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11169 gdb::unique_xmalloc_ptr<char> search_path_holder;
11170 if (search_cwd)
11172 if (!debug_file_directory.empty ())
11174 search_path_holder.reset (concat (".", dirname_separator_string,
11175 debug_file_directory.c_str (),
11176 (char *) NULL));
11177 search_path = search_path_holder.get ();
11179 else
11180 search_path = ".";
11182 else
11183 search_path = debug_file_directory.c_str ();
11185 /* Add the path for the executable binary to the list of search paths. */
11186 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11187 search_path_holder.reset (concat (objfile_dir.c_str (),
11188 dirname_separator_string,
11189 search_path, nullptr));
11190 search_path = search_path_holder.get ();
11192 openp_flags flags = OPF_RETURN_REALPATH;
11193 if (is_dwp)
11194 flags |= OPF_SEARCH_IN_PATH;
11196 gdb::unique_xmalloc_ptr<char> absolute_name;
11197 desc = openp (search_path, flags, file_name,
11198 O_RDONLY | O_BINARY, &absolute_name);
11199 if (desc < 0)
11200 return NULL;
11202 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11203 gnutarget, desc));
11204 if (sym_bfd == NULL)
11205 return NULL;
11206 bfd_set_cacheable (sym_bfd.get (), 1);
11208 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11209 return NULL;
11211 /* Success. Record the bfd as having been included by the objfile's bfd.
11212 This is important because things like demangled_names_hash lives in the
11213 objfile's per_bfd space and may have references to things like symbol
11214 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11215 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
11217 return sym_bfd;
11220 /* Try to open DWO file FILE_NAME.
11221 COMP_DIR is the DW_AT_comp_dir attribute.
11222 The result is the bfd handle of the file.
11223 If there is a problem finding or opening the file, return NULL.
11224 Upon success, the canonicalized path of the file is stored in the bfd,
11225 same as symfile_bfd_open. */
11227 static gdb_bfd_ref_ptr
11228 open_dwo_file (dwarf2_per_objfile *per_objfile,
11229 const char *file_name, const char *comp_dir)
11231 if (IS_ABSOLUTE_PATH (file_name))
11232 return try_open_dwop_file (per_objfile, file_name,
11233 0 /*is_dwp*/, 0 /*search_cwd*/);
11235 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11237 if (comp_dir != NULL)
11239 std::string path_to_try = path_join (comp_dir, file_name);
11241 /* NOTE: If comp_dir is a relative path, this will also try the
11242 search path, which seems useful. */
11243 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11244 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11246 if (abfd != NULL)
11247 return abfd;
11250 /* That didn't work, try debug-file-directory, which, despite its name,
11251 is a list of paths. */
11253 if (debug_file_directory.empty ())
11254 return NULL;
11256 return try_open_dwop_file (per_objfile, file_name,
11257 0 /*is_dwp*/, 1 /*search_cwd*/);
11260 /* This function is mapped across the sections and remembers the offset and
11261 size of each of the DWO debugging sections we are interested in. */
11263 static void
11264 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11265 asection *sectp, dwo_sections *dwo_sections)
11267 const struct dwop_section_names *names = &dwop_section_names;
11269 struct dwarf2_section_info *dw_sect = nullptr;
11271 if (names->abbrev_dwo.matches (sectp->name))
11272 dw_sect = &dwo_sections->abbrev;
11273 else if (names->info_dwo.matches (sectp->name))
11274 dw_sect = &dwo_sections->info;
11275 else if (names->line_dwo.matches (sectp->name))
11276 dw_sect = &dwo_sections->line;
11277 else if (names->loc_dwo.matches (sectp->name))
11278 dw_sect = &dwo_sections->loc;
11279 else if (names->loclists_dwo.matches (sectp->name))
11280 dw_sect = &dwo_sections->loclists;
11281 else if (names->macinfo_dwo.matches (sectp->name))
11282 dw_sect = &dwo_sections->macinfo;
11283 else if (names->macro_dwo.matches (sectp->name))
11284 dw_sect = &dwo_sections->macro;
11285 else if (names->rnglists_dwo.matches (sectp->name))
11286 dw_sect = &dwo_sections->rnglists;
11287 else if (names->str_dwo.matches (sectp->name))
11288 dw_sect = &dwo_sections->str;
11289 else if (names->str_offsets_dwo.matches (sectp->name))
11290 dw_sect = &dwo_sections->str_offsets;
11291 else if (names->types_dwo.matches (sectp->name))
11293 struct dwarf2_section_info type_section;
11295 memset (&type_section, 0, sizeof (type_section));
11296 dwo_sections->types.push_back (type_section);
11297 dw_sect = &dwo_sections->types.back ();
11300 if (dw_sect != nullptr)
11302 dw_sect->s.section = sectp;
11303 dw_sect->size = bfd_section_size (sectp);
11304 dw_sect->read (objfile);
11308 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11309 by PER_CU. This is for the non-DWP case.
11310 The result is NULL if DWO_NAME can't be found. */
11312 static struct dwo_file *
11313 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11314 const char *comp_dir)
11316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11318 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11319 if (dbfd == NULL)
11321 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11323 return NULL;
11326 dwo_file_up dwo_file (new struct dwo_file);
11327 dwo_file->dwo_name = dwo_name;
11328 dwo_file->comp_dir = comp_dir;
11329 dwo_file->dbfd = std::move (dbfd);
11331 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11332 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11333 sec, &dwo_file->sections);
11335 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11336 dwo_file->cus);
11338 if (cu->per_cu->version () < 5)
11340 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11341 dwo_file->sections.types, dwo_file->tus);
11343 else
11345 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11346 &dwo_file->sections.info, dwo_file->tus,
11347 rcuh_kind::COMPILE);
11350 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11352 return dwo_file.release ();
11355 /* This function is mapped across the sections and remembers the offset and
11356 size of each of the DWP debugging sections common to version 1 and 2 that
11357 we are interested in. */
11359 static void
11360 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11361 asection *sectp, dwp_file *dwp_file)
11363 const struct dwop_section_names *names = &dwop_section_names;
11364 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11366 /* Record the ELF section number for later lookup: this is what the
11367 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11368 gdb_assert (elf_section_nr < dwp_file->num_sections);
11369 dwp_file->elf_sections[elf_section_nr] = sectp;
11371 /* Look for specific sections that we need. */
11372 struct dwarf2_section_info *dw_sect = nullptr;
11373 if (names->str_dwo.matches (sectp->name))
11374 dw_sect = &dwp_file->sections.str;
11375 else if (names->cu_index.matches (sectp->name))
11376 dw_sect = &dwp_file->sections.cu_index;
11377 else if (names->tu_index.matches (sectp->name))
11378 dw_sect = &dwp_file->sections.tu_index;
11380 if (dw_sect != nullptr)
11382 dw_sect->s.section = sectp;
11383 dw_sect->size = bfd_section_size (sectp);
11384 dw_sect->read (objfile);
11388 /* This function is mapped across the sections and remembers the offset and
11389 size of each of the DWP version 2 debugging sections that we are interested
11390 in. This is split into a separate function because we don't know if we
11391 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11393 static void
11394 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11395 asection *sectp, void *dwp_file_ptr)
11397 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11398 const struct dwop_section_names *names = &dwop_section_names;
11399 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11401 /* Record the ELF section number for later lookup: this is what the
11402 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11403 gdb_assert (elf_section_nr < dwp_file->num_sections);
11404 dwp_file->elf_sections[elf_section_nr] = sectp;
11406 /* Look for specific sections that we need. */
11407 struct dwarf2_section_info *dw_sect = nullptr;
11408 if (names->abbrev_dwo.matches (sectp->name))
11409 dw_sect = &dwp_file->sections.abbrev;
11410 else if (names->info_dwo.matches (sectp->name))
11411 dw_sect = &dwp_file->sections.info;
11412 else if (names->line_dwo.matches (sectp->name))
11413 dw_sect = &dwp_file->sections.line;
11414 else if (names->loc_dwo.matches (sectp->name))
11415 dw_sect = &dwp_file->sections.loc;
11416 else if (names->macinfo_dwo.matches (sectp->name))
11417 dw_sect = &dwp_file->sections.macinfo;
11418 else if (names->macro_dwo.matches (sectp->name))
11419 dw_sect = &dwp_file->sections.macro;
11420 else if (names->str_offsets_dwo.matches (sectp->name))
11421 dw_sect = &dwp_file->sections.str_offsets;
11422 else if (names->types_dwo.matches (sectp->name))
11423 dw_sect = &dwp_file->sections.types;
11425 if (dw_sect != nullptr)
11427 dw_sect->s.section = sectp;
11428 dw_sect->size = bfd_section_size (sectp);
11429 dw_sect->read (objfile);
11433 /* This function is mapped across the sections and remembers the offset and
11434 size of each of the DWP version 5 debugging sections that we are interested
11435 in. This is split into a separate function because we don't know if we
11436 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11438 static void
11439 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11440 asection *sectp, void *dwp_file_ptr)
11442 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11443 const struct dwop_section_names *names = &dwop_section_names;
11444 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11446 /* Record the ELF section number for later lookup: this is what the
11447 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11448 gdb_assert (elf_section_nr < dwp_file->num_sections);
11449 dwp_file->elf_sections[elf_section_nr] = sectp;
11451 /* Look for specific sections that we need. */
11452 struct dwarf2_section_info *dw_sect = nullptr;
11453 if (names->abbrev_dwo.matches (sectp->name))
11454 dw_sect = &dwp_file->sections.abbrev;
11455 else if (names->info_dwo.matches (sectp->name))
11456 dw_sect = &dwp_file->sections.info;
11457 else if (names->line_dwo.matches (sectp->name))
11458 dw_sect = &dwp_file->sections.line;
11459 else if (names->loclists_dwo.matches (sectp->name))
11460 dw_sect = &dwp_file->sections.loclists;
11461 else if (names->macro_dwo.matches (sectp->name))
11462 dw_sect = &dwp_file->sections.macro;
11463 else if (names->rnglists_dwo.matches (sectp->name))
11464 dw_sect = &dwp_file->sections.rnglists;
11465 else if (names->str_offsets_dwo.matches (sectp->name))
11466 dw_sect = &dwp_file->sections.str_offsets;
11468 if (dw_sect != nullptr)
11470 dw_sect->s.section = sectp;
11471 dw_sect->size = bfd_section_size (sectp);
11472 dw_sect->read (objfile);
11476 /* Hash function for dwp_file loaded CUs/TUs. */
11478 static hashval_t
11479 hash_dwp_loaded_cutus (const void *item)
11481 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11483 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11484 return dwo_unit->signature;
11487 /* Equality function for dwp_file loaded CUs/TUs. */
11489 static int
11490 eq_dwp_loaded_cutus (const void *a, const void *b)
11492 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11493 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11495 return dua->signature == dub->signature;
11498 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11500 static htab_up
11501 allocate_dwp_loaded_cutus_table ()
11503 return htab_up (htab_create_alloc (3,
11504 hash_dwp_loaded_cutus,
11505 eq_dwp_loaded_cutus,
11506 NULL, xcalloc, xfree));
11509 /* Try to open DWP file FILE_NAME.
11510 The result is the bfd handle of the file.
11511 If there is a problem finding or opening the file, return NULL.
11512 Upon success, the canonicalized path of the file is stored in the bfd,
11513 same as symfile_bfd_open. */
11515 static gdb_bfd_ref_ptr
11516 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11518 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11519 1 /*is_dwp*/,
11520 1 /*search_cwd*/));
11521 if (abfd != NULL)
11522 return abfd;
11524 /* Work around upstream bug 15652.
11525 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11526 [Whether that's a "bug" is debatable, but it is getting in our way.]
11527 We have no real idea where the dwp file is, because gdb's realpath-ing
11528 of the executable's path may have discarded the needed info.
11529 [IWBN if the dwp file name was recorded in the executable, akin to
11530 .gnu_debuglink, but that doesn't exist yet.]
11531 Strip the directory from FILE_NAME and search again. */
11532 if (!debug_file_directory.empty ())
11534 /* Don't implicitly search the current directory here.
11535 If the user wants to search "." to handle this case,
11536 it must be added to debug-file-directory. */
11537 return try_open_dwop_file (per_objfile, lbasename (file_name),
11538 1 /*is_dwp*/,
11539 0 /*search_cwd*/);
11542 return NULL;
11545 /* Initialize the use of the DWP file for the current objfile.
11546 By convention the name of the DWP file is ${objfile}.dwp.
11547 The result is NULL if it can't be found. */
11549 static std::unique_ptr<struct dwp_file>
11550 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11552 struct objfile *objfile = per_objfile->objfile;
11554 /* Try to find first .dwp for the binary file before any symbolic links
11555 resolving. */
11557 /* If the objfile is a debug file, find the name of the real binary
11558 file and get the name of dwp file from there. */
11559 std::string dwp_name;
11560 if (objfile->separate_debug_objfile_backlink != NULL)
11562 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11563 const char *backlink_basename = lbasename (backlink->original_name);
11565 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11567 else
11568 dwp_name = objfile->original_name;
11570 dwp_name += ".dwp";
11572 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11573 if (dbfd == NULL
11574 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11576 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11577 dwp_name = objfile_name (objfile);
11578 dwp_name += ".dwp";
11579 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11582 if (dbfd == NULL)
11584 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11586 return std::unique_ptr<dwp_file> ();
11589 const char *name = bfd_get_filename (dbfd.get ());
11590 std::unique_ptr<struct dwp_file> dwp_file
11591 (new struct dwp_file (name, std::move (dbfd)));
11593 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11594 dwp_file->elf_sections =
11595 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11596 dwp_file->num_sections, asection *);
11598 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11599 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11600 dwp_file.get ());
11602 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11604 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11606 /* The DWP file version is stored in the hash table. Oh well. */
11607 if (dwp_file->cus && dwp_file->tus
11608 && dwp_file->cus->version != dwp_file->tus->version)
11610 /* Technically speaking, we should try to limp along, but this is
11611 pretty bizarre. We use pulongest here because that's the established
11612 portability solution (e.g, we cannot use %u for uint32_t). */
11613 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11614 " TU version %s [in DWP file %s]"),
11615 pulongest (dwp_file->cus->version),
11616 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11619 if (dwp_file->cus)
11620 dwp_file->version = dwp_file->cus->version;
11621 else if (dwp_file->tus)
11622 dwp_file->version = dwp_file->tus->version;
11623 else
11624 dwp_file->version = 2;
11626 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11628 if (dwp_file->version == 2)
11629 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11630 dwp_file.get ());
11631 else
11632 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11633 dwp_file.get ());
11636 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11637 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11639 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11640 dwarf_read_debug_printf (" %s CUs, %s TUs",
11641 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11642 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11644 return dwp_file;
11647 /* Wrapper around open_and_init_dwp_file, only open it once. */
11649 static struct dwp_file *
11650 get_dwp_file (dwarf2_per_objfile *per_objfile)
11652 if (!per_objfile->per_bfd->dwp_checked)
11654 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11655 per_objfile->per_bfd->dwp_checked = 1;
11657 return per_objfile->per_bfd->dwp_file.get ();
11660 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11661 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11662 or in the DWP file for the objfile, referenced by THIS_UNIT.
11663 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11664 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11666 This is called, for example, when wanting to read a variable with a
11667 complex location. Therefore we don't want to do file i/o for every call.
11668 Therefore we don't want to look for a DWO file on every call.
11669 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11670 then we check if we've already seen DWO_NAME, and only THEN do we check
11671 for a DWO file.
11673 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11674 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11676 static struct dwo_unit *
11677 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11678 ULONGEST signature, int is_debug_types)
11680 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11681 struct objfile *objfile = per_objfile->objfile;
11682 const char *kind = is_debug_types ? "TU" : "CU";
11683 void **dwo_file_slot;
11684 struct dwo_file *dwo_file;
11685 struct dwp_file *dwp_file;
11687 /* First see if there's a DWP file.
11688 If we have a DWP file but didn't find the DWO inside it, don't
11689 look for the original DWO file. It makes gdb behave differently
11690 depending on whether one is debugging in the build tree. */
11692 dwp_file = get_dwp_file (per_objfile);
11693 if (dwp_file != NULL)
11695 const struct dwp_hash_table *dwp_htab =
11696 is_debug_types ? dwp_file->tus : dwp_file->cus;
11698 if (dwp_htab != NULL)
11700 struct dwo_unit *dwo_cutu =
11701 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11702 is_debug_types);
11704 if (dwo_cutu != NULL)
11706 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11707 kind, hex_string (signature),
11708 host_address_to_string (dwo_cutu));
11710 return dwo_cutu;
11714 else
11716 /* No DWP file, look for the DWO file. */
11718 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11719 if (*dwo_file_slot == NULL)
11721 /* Read in the file and build a table of the CUs/TUs it contains. */
11722 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11724 /* NOTE: This will be NULL if unable to open the file. */
11725 dwo_file = (struct dwo_file *) *dwo_file_slot;
11727 if (dwo_file != NULL)
11729 struct dwo_unit *dwo_cutu = NULL;
11731 if (is_debug_types && dwo_file->tus)
11733 struct dwo_unit find_dwo_cutu;
11735 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11736 find_dwo_cutu.signature = signature;
11737 dwo_cutu
11738 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11739 &find_dwo_cutu);
11741 else if (!is_debug_types && dwo_file->cus)
11743 struct dwo_unit find_dwo_cutu;
11745 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11746 find_dwo_cutu.signature = signature;
11747 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11748 &find_dwo_cutu);
11751 if (dwo_cutu != NULL)
11753 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11754 kind, dwo_name, hex_string (signature),
11755 host_address_to_string (dwo_cutu));
11757 return dwo_cutu;
11762 /* We didn't find it. This could mean a dwo_id mismatch, or
11763 someone deleted the DWO/DWP file, or the search path isn't set up
11764 correctly to find the file. */
11766 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11767 kind, dwo_name, hex_string (signature));
11769 /* This is a warning and not a complaint because it can be caused by
11770 pilot error (e.g., user accidentally deleting the DWO). */
11772 /* Print the name of the DWP file if we looked there, helps the user
11773 better diagnose the problem. */
11774 std::string dwp_text;
11776 if (dwp_file != NULL)
11777 dwp_text = string_printf (" [in DWP file %s]",
11778 lbasename (dwp_file->name));
11780 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11781 " [in module %s]"),
11782 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11783 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11785 return NULL;
11788 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11789 See lookup_dwo_cutu_unit for details. */
11791 static struct dwo_unit *
11792 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11793 ULONGEST signature)
11795 gdb_assert (!cu->per_cu->is_debug_types);
11797 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11800 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11801 See lookup_dwo_cutu_unit for details. */
11803 static struct dwo_unit *
11804 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11806 gdb_assert (cu->per_cu->is_debug_types);
11808 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11810 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11813 /* Traversal function for queue_and_load_all_dwo_tus. */
11815 static int
11816 queue_and_load_dwo_tu (void **slot, void *info)
11818 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11819 dwarf2_cu *cu = (dwarf2_cu *) info;
11820 ULONGEST signature = dwo_unit->signature;
11821 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11823 if (sig_type != NULL)
11825 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11826 a real dependency of PER_CU on SIG_TYPE. That is detected later
11827 while processing PER_CU. */
11828 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11829 cu->lang ()))
11830 load_full_type_unit (sig_type, cu->per_objfile);
11831 cu->per_cu->imported_symtabs_push (sig_type);
11834 return 1;
11837 /* Queue all TUs contained in the DWO of CU to be read in.
11838 The DWO may have the only definition of the type, though it may not be
11839 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11840 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11842 static void
11843 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11845 struct dwo_unit *dwo_unit;
11846 struct dwo_file *dwo_file;
11848 gdb_assert (cu != nullptr);
11849 gdb_assert (!cu->per_cu->is_debug_types);
11850 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11852 dwo_unit = cu->dwo_unit;
11853 gdb_assert (dwo_unit != NULL);
11855 dwo_file = dwo_unit->dwo_file;
11856 if (dwo_file->tus != NULL)
11857 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11860 /* Read in various DIEs. */
11862 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11863 Inherit only the children of the DW_AT_abstract_origin DIE not being
11864 already referenced by DW_AT_abstract_origin from the children of the
11865 current DIE. */
11867 static void
11868 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11870 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11871 if (attr == nullptr)
11872 return;
11874 /* Note that following die references may follow to a die in a
11875 different CU. */
11876 dwarf2_cu *origin_cu = cu;
11878 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11879 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
11881 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11882 symbols in. */
11883 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
11884 origin_cu->list_in_scope = cu->list_in_scope;
11886 if (die->tag != origin_die->tag
11887 && !(die->tag == DW_TAG_inlined_subroutine
11888 && origin_die->tag == DW_TAG_subprogram))
11889 complaint (_("DIE %s and its abstract origin %s have different tags"),
11890 sect_offset_str (die->sect_off),
11891 sect_offset_str (origin_die->sect_off));
11893 /* Find if the concrete and abstract trees are structurally the
11894 same. This is a shallow traversal and it is not bullet-proof;
11895 the compiler can trick the debugger into believing that the trees
11896 are isomorphic, whereas they actually are not. However, the
11897 likelyhood of this happening is pretty low, and a full-fledged
11898 check would be an overkill. */
11899 bool are_isomorphic = true;
11900 die_info *concrete_child = die->child;
11901 die_info *abstract_child = origin_die->child;
11902 while (concrete_child != nullptr || abstract_child != nullptr)
11904 if (concrete_child == nullptr
11905 || abstract_child == nullptr
11906 || concrete_child->tag != abstract_child->tag)
11908 are_isomorphic = false;
11909 break;
11912 concrete_child = concrete_child->sibling;
11913 abstract_child = abstract_child->sibling;
11916 /* Walk the origin's children in parallel to the concrete children.
11917 This helps match an origin child in case the debug info misses
11918 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11919 origin tree may not have the same tree structure as the concrete
11920 DIE, though. */
11921 die_info *corresponding_abstract_child
11922 = are_isomorphic ? origin_die->child : nullptr;
11924 std::vector<sect_offset> offsets;
11926 for (die_info *child_die = die->child;
11927 child_die && child_die->tag;
11928 child_die = child_die->sibling)
11930 /* We are trying to process concrete instance entries:
11931 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11932 it's not relevant to our analysis here. i.e. detecting DIEs that are
11933 present in the abstract instance but not referenced in the concrete
11934 one. */
11935 if (child_die->tag == DW_TAG_call_site
11936 || child_die->tag == DW_TAG_GNU_call_site)
11938 if (are_isomorphic)
11939 corresponding_abstract_child
11940 = corresponding_abstract_child->sibling;
11941 continue;
11944 /* For each CHILD_DIE, find the corresponding child of
11945 ORIGIN_DIE. If there is more than one layer of
11946 DW_AT_abstract_origin, follow them all; there shouldn't be,
11947 but GCC versions at least through 4.4 generate this (GCC PR
11948 40573). */
11949 die_info *child_origin_die = child_die;
11950 dwarf2_cu *child_origin_cu = cu;
11951 while (true)
11953 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11954 child_origin_cu);
11955 if (attr == nullptr)
11956 break;
11958 child_origin_die = follow_die_ref (child_origin_die, attr,
11959 &child_origin_cu);
11962 /* If missing DW_AT_abstract_origin, try the corresponding child
11963 of the origin. Clang emits such lexical scopes. */
11964 if (child_origin_die == child_die
11965 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11966 && are_isomorphic
11967 && child_die->tag == DW_TAG_lexical_block)
11968 child_origin_die = corresponding_abstract_child;
11970 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11971 counterpart may exist. */
11972 if (child_origin_die != child_die)
11974 if (child_die->tag != child_origin_die->tag
11975 && !(child_die->tag == DW_TAG_inlined_subroutine
11976 && child_origin_die->tag == DW_TAG_subprogram))
11977 complaint (_("Child DIE %s and its abstract origin %s have "
11978 "different tags"),
11979 sect_offset_str (child_die->sect_off),
11980 sect_offset_str (child_origin_die->sect_off));
11981 if (child_origin_die->parent != origin_die)
11982 complaint (_("Child DIE %s and its abstract origin %s have "
11983 "different parents"),
11984 sect_offset_str (child_die->sect_off),
11985 sect_offset_str (child_origin_die->sect_off));
11986 else
11987 offsets.push_back (child_origin_die->sect_off);
11990 if (are_isomorphic)
11991 corresponding_abstract_child = corresponding_abstract_child->sibling;
11994 if (!offsets.empty ())
11996 std::sort (offsets.begin (), offsets.end ());
11998 for (auto offsets_it = offsets.begin () + 1;
11999 offsets_it < offsets.end ();
12000 ++offsets_it)
12001 if (*(offsets_it - 1) == *offsets_it)
12002 complaint (_("Multiple children of DIE %s refer "
12003 "to DIE %s as their abstract origin"),
12004 sect_offset_str (die->sect_off),
12005 sect_offset_str (*offsets_it));
12008 auto offsets_it = offsets.begin ();
12009 die_info *origin_child_die = origin_die->child;
12010 while (origin_child_die != nullptr && origin_child_die->tag != 0)
12012 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12013 while (offsets_it < offsets.end ()
12014 && *offsets_it < origin_child_die->sect_off)
12015 ++offsets_it;
12017 if (offsets_it == offsets.end ()
12018 || *offsets_it > origin_child_die->sect_off)
12020 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12021 Check whether we're already processing ORIGIN_CHILD_DIE.
12022 This can happen with mutually referenced abstract_origins.
12023 PR 16581. */
12024 if (!origin_child_die->in_process)
12025 process_die (origin_child_die, origin_cu);
12028 origin_child_die = origin_child_die->sibling;
12031 origin_cu->list_in_scope = origin_previous_list_in_scope;
12033 if (cu != origin_cu)
12034 compute_delayed_physnames (origin_cu);
12037 static void
12038 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12040 struct objfile *objfile = cu->per_objfile->objfile;
12041 struct gdbarch *gdbarch = objfile->arch ();
12042 struct context_stack *newobj;
12043 CORE_ADDR lowpc;
12044 CORE_ADDR highpc;
12045 struct die_info *child_die;
12046 struct attribute *attr, *call_line, *call_file;
12047 const char *name;
12048 CORE_ADDR baseaddr;
12049 struct block *block;
12050 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12051 std::vector<struct symbol *> template_args;
12052 struct template_symbol *templ_func = NULL;
12054 if (inlined_func)
12056 /* If we do not have call site information, we can't show the
12057 caller of this inlined function. That's too confusing, so
12058 only use the scope for local variables. */
12059 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12060 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12061 if (call_line == NULL || call_file == NULL)
12063 read_lexical_block_scope (die, cu);
12064 return;
12068 baseaddr = objfile->text_section_offset ();
12070 name = dwarf2_name (die, cu);
12071 if (name == nullptr)
12072 name = dw2_linkage_name (die, cu);
12074 /* Ignore functions with missing or empty names. These are actually
12075 illegal according to the DWARF standard. */
12076 if (name == NULL)
12078 complaint (_("missing name for subprogram DIE at %s"),
12079 sect_offset_str (die->sect_off));
12080 return;
12083 /* Ignore functions with missing or invalid low and high pc attributes. */
12084 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
12085 <= PC_BOUNDS_INVALID)
12087 if (have_complaint ())
12089 attr = dwarf2_attr (die, DW_AT_external, cu);
12090 bool external_p = attr != nullptr && attr->as_boolean ();
12091 attr = dwarf2_attr (die, DW_AT_inline, cu);
12092 bool inlined_p
12093 = (attr != nullptr
12094 && attr->is_nonnegative ()
12095 && (attr->as_nonnegative () == DW_INL_inlined
12096 || attr->as_nonnegative () == DW_INL_declared_inlined));
12097 attr = dwarf2_attr (die, DW_AT_declaration, cu);
12098 bool decl_p = attr != nullptr && attr->as_boolean ();
12099 if (!external_p && !inlined_p && !decl_p)
12100 complaint (_("cannot get low and high bounds "
12101 "for subprogram DIE at %s"),
12102 sect_offset_str (die->sect_off));
12104 return;
12107 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12108 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12110 /* If we have any template arguments, then we must allocate a
12111 different sort of symbol. */
12112 for (child_die = die->child; child_die; child_die = child_die->sibling)
12114 if (child_die->tag == DW_TAG_template_type_param
12115 || child_die->tag == DW_TAG_template_value_param)
12117 templ_func = new (&objfile->objfile_obstack) template_symbol;
12118 templ_func->subclass = SYMBOL_TEMPLATE;
12119 break;
12123 gdb_assert (cu->get_builder () != nullptr);
12124 newobj = cu->get_builder ()->push_context (0, lowpc);
12125 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12126 (struct symbol *) templ_func);
12128 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12129 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12130 cu->lang ());
12132 /* If there is a location expression for DW_AT_frame_base, record
12133 it. */
12134 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12135 if (attr != nullptr)
12136 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12138 /* If there is a location for the static link, record it. */
12139 newobj->static_link = NULL;
12140 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12141 if (attr != nullptr)
12143 newobj->static_link
12144 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12145 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12146 cu->addr_type ());
12149 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12151 if (die->child != NULL)
12153 child_die = die->child;
12154 while (child_die && child_die->tag)
12156 if (child_die->tag == DW_TAG_template_type_param
12157 || child_die->tag == DW_TAG_template_value_param)
12159 struct symbol *arg = new_symbol (child_die, NULL, cu);
12161 if (arg != NULL)
12162 template_args.push_back (arg);
12164 else
12165 process_die (child_die, cu);
12166 child_die = child_die->sibling;
12170 inherit_abstract_dies (die, cu);
12172 /* If we have a DW_AT_specification, we might need to import using
12173 directives from the context of the specification DIE. See the
12174 comment in determine_prefix. */
12175 if (cu->lang () == language_cplus
12176 && dwarf2_attr (die, DW_AT_specification, cu))
12178 struct dwarf2_cu *spec_cu = cu;
12179 struct die_info *spec_die = die_specification (die, &spec_cu);
12181 while (spec_die)
12183 child_die = spec_die->child;
12184 while (child_die && child_die->tag)
12186 if (child_die->tag == DW_TAG_imported_module)
12187 process_die (child_die, spec_cu);
12188 child_die = child_die->sibling;
12191 /* In some cases, GCC generates specification DIEs that
12192 themselves contain DW_AT_specification attributes. */
12193 spec_die = die_specification (spec_die, &spec_cu);
12197 struct context_stack cstk = cu->get_builder ()->pop_context ();
12198 /* Make a block for the local symbols within. */
12199 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12200 cstk.static_link, lowpc, highpc);
12202 /* For C++, set the block's scope. */
12203 if ((cu->lang () == language_cplus
12204 || cu->lang () == language_fortran
12205 || cu->lang () == language_d
12206 || cu->lang () == language_rust)
12207 && cu->processing_has_namespace_info)
12208 block_set_scope (block, determine_prefix (die, cu),
12209 &objfile->objfile_obstack);
12211 /* If we have address ranges, record them. */
12212 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12214 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12216 /* Attach template arguments to function. */
12217 if (!template_args.empty ())
12219 gdb_assert (templ_func != NULL);
12221 templ_func->n_template_arguments = template_args.size ();
12222 templ_func->template_arguments
12223 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12224 templ_func->n_template_arguments);
12225 memcpy (templ_func->template_arguments,
12226 template_args.data (),
12227 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12229 /* Make sure that the symtab is set on the new symbols. Even
12230 though they don't appear in this symtab directly, other parts
12231 of gdb assume that symbols do, and this is reasonably
12232 true. */
12233 for (symbol *sym : template_args)
12234 sym->set_symtab (templ_func->symtab ());
12237 /* In C++, we can have functions nested inside functions (e.g., when
12238 a function declares a class that has methods). This means that
12239 when we finish processing a function scope, we may need to go
12240 back to building a containing block's symbol lists. */
12241 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12242 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12244 /* If we've finished processing a top-level function, subsequent
12245 symbols go in the file symbol list. */
12246 if (cu->get_builder ()->outermost_context_p ())
12247 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12250 /* Process all the DIES contained within a lexical block scope. Start
12251 a new scope, process the dies, and then close the scope. */
12253 static void
12254 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12256 struct objfile *objfile = cu->per_objfile->objfile;
12257 struct gdbarch *gdbarch = objfile->arch ();
12258 CORE_ADDR lowpc, highpc;
12259 struct die_info *child_die;
12260 CORE_ADDR baseaddr;
12262 baseaddr = objfile->text_section_offset ();
12264 /* Ignore blocks with missing or invalid low and high pc attributes. */
12265 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12266 as multiple lexical blocks? Handling children in a sane way would
12267 be nasty. Might be easier to properly extend generic blocks to
12268 describe ranges. */
12269 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12271 case PC_BOUNDS_NOT_PRESENT:
12272 /* DW_TAG_lexical_block has no attributes, process its children as if
12273 there was no wrapping by that DW_TAG_lexical_block.
12274 GCC does no longer produces such DWARF since GCC r224161. */
12275 for (child_die = die->child;
12276 child_die != NULL && child_die->tag;
12277 child_die = child_die->sibling)
12279 /* We might already be processing this DIE. This can happen
12280 in an unusual circumstance -- where a subroutine A
12281 appears lexically in another subroutine B, but A actually
12282 inlines B. The recursion is broken here, rather than in
12283 inherit_abstract_dies, because it seems better to simply
12284 drop concrete children here. */
12285 if (!child_die->in_process)
12286 process_die (child_die, cu);
12288 return;
12289 case PC_BOUNDS_INVALID:
12290 return;
12292 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12293 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12295 cu->get_builder ()->push_context (0, lowpc);
12296 if (die->child != NULL)
12298 child_die = die->child;
12299 while (child_die && child_die->tag)
12301 process_die (child_die, cu);
12302 child_die = child_die->sibling;
12305 inherit_abstract_dies (die, cu);
12306 struct context_stack cstk = cu->get_builder ()->pop_context ();
12308 if (*cu->get_builder ()->get_local_symbols () != NULL
12309 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12311 struct block *block
12312 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12313 cstk.start_addr, highpc);
12315 /* Note that recording ranges after traversing children, as we
12316 do here, means that recording a parent's ranges entails
12317 walking across all its children's ranges as they appear in
12318 the address map, which is quadratic behavior.
12320 It would be nicer to record the parent's ranges before
12321 traversing its children, simply overriding whatever you find
12322 there. But since we don't even decide whether to create a
12323 block until after we've traversed its children, that's hard
12324 to do. */
12325 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12327 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12328 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12331 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12332 struct dwarf2_cu *cu,
12333 dwarf_tag tag,
12334 std::vector<CORE_ADDR> &result);
12336 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12338 static void
12339 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12342 struct objfile *objfile = per_objfile->objfile;
12343 struct gdbarch *gdbarch = objfile->arch ();
12344 CORE_ADDR pc, baseaddr;
12345 struct attribute *attr;
12346 void **slot;
12347 int nparams;
12348 struct die_info *child_die;
12350 baseaddr = objfile->text_section_offset ();
12352 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12353 if (attr == NULL)
12355 /* This was a pre-DWARF-5 GNU extension alias
12356 for DW_AT_call_return_pc. */
12357 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12359 if (!attr)
12361 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12362 "DIE %s [in module %s]"),
12363 sect_offset_str (die->sect_off), objfile_name (objfile));
12364 return;
12366 pc = attr->as_address () + baseaddr;
12367 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12368 pc -= baseaddr;
12370 if (cu->call_site_htab == NULL)
12371 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12372 call_site::eq, NULL,
12373 &objfile->objfile_obstack,
12374 hashtab_obstack_allocate, NULL);
12375 struct call_site call_site_local (pc, nullptr, nullptr);
12376 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12377 if (*slot != NULL)
12379 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12380 "DIE %s [in module %s]"),
12381 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12382 objfile_name (objfile));
12383 return;
12386 /* Count parameters at the caller. */
12388 nparams = 0;
12389 for (child_die = die->child; child_die && child_die->tag;
12390 child_die = child_die->sibling)
12392 if (child_die->tag != DW_TAG_call_site_parameter
12393 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12395 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12396 "DW_TAG_call_site child DIE %s [in module %s]"),
12397 child_die->tag, sect_offset_str (child_die->sect_off),
12398 objfile_name (objfile));
12399 continue;
12402 nparams++;
12405 struct call_site *call_site
12406 = new (XOBNEWVAR (&objfile->objfile_obstack,
12407 struct call_site,
12408 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12409 struct call_site (pc, cu->per_cu, per_objfile);
12410 *slot = call_site;
12412 /* We never call the destructor of call_site, so we must ensure it is
12413 trivially destructible. */
12414 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12416 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12417 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12419 struct die_info *func_die;
12421 /* Skip also over DW_TAG_inlined_subroutine. */
12422 for (func_die = die->parent;
12423 func_die && func_die->tag != DW_TAG_subprogram
12424 && func_die->tag != DW_TAG_subroutine_type;
12425 func_die = func_die->parent);
12427 /* DW_AT_call_all_calls is a superset
12428 of DW_AT_call_all_tail_calls. */
12429 if (func_die
12430 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12431 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12432 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12433 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12435 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12436 not complete. But keep CALL_SITE for look ups via call_site_htab,
12437 both the initial caller containing the real return address PC and
12438 the final callee containing the current PC of a chain of tail
12439 calls do not need to have the tail call list complete. But any
12440 function candidate for a virtual tail call frame searched via
12441 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12442 determined unambiguously. */
12444 else
12446 struct type *func_type = NULL;
12448 if (func_die)
12449 func_type = get_die_type (func_die, cu);
12450 if (func_type != NULL)
12452 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12454 /* Enlist this call site to the function. */
12455 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12456 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12458 else
12459 complaint (_("Cannot find function owning DW_TAG_call_site "
12460 "DIE %s [in module %s]"),
12461 sect_offset_str (die->sect_off), objfile_name (objfile));
12465 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12466 if (attr == NULL)
12467 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12468 if (attr == NULL)
12469 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12470 if (attr == NULL)
12472 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12473 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12476 call_site->target.set_loc_dwarf_block (nullptr);
12477 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12478 /* Keep NULL DWARF_BLOCK. */;
12479 else if (attr->form_is_block ())
12481 struct dwarf2_locexpr_baton *dlbaton;
12482 struct dwarf_block *block = attr->as_block ();
12484 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12485 dlbaton->data = block->data;
12486 dlbaton->size = block->size;
12487 dlbaton->per_objfile = per_objfile;
12488 dlbaton->per_cu = cu->per_cu;
12490 call_site->target.set_loc_dwarf_block (dlbaton);
12492 else if (attr->form_is_ref ())
12494 struct dwarf2_cu *target_cu = cu;
12495 struct die_info *target_die;
12497 target_die = follow_die_ref (die, attr, &target_cu);
12498 gdb_assert (target_cu->per_objfile->objfile == objfile);
12500 struct attribute *ranges_attr
12501 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12503 if (die_is_declaration (target_die, target_cu))
12505 const char *target_physname;
12507 /* Prefer the mangled name; otherwise compute the demangled one. */
12508 target_physname = dw2_linkage_name (target_die, target_cu);
12509 if (target_physname == NULL)
12510 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12511 if (target_physname == NULL)
12512 complaint (_("DW_AT_call_target target DIE has invalid "
12513 "physname, for referencing DIE %s [in module %s]"),
12514 sect_offset_str (die->sect_off), objfile_name (objfile));
12515 else
12516 call_site->target.set_loc_physname (target_physname);
12518 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12520 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12521 + target_cu->gnu_ranges_base);
12522 std::vector<CORE_ADDR> addresses;
12523 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12524 target_die->tag, addresses);
12525 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12526 addresses.size ());
12527 std::copy (addresses.begin (), addresses.end (), saved);
12528 call_site->target.set_loc_array (addresses.size (), saved);
12530 else
12532 CORE_ADDR lowpc;
12534 /* DW_AT_entry_pc should be preferred. */
12535 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12536 nullptr, nullptr)
12537 <= PC_BOUNDS_INVALID)
12538 complaint (_("DW_AT_call_target target DIE has invalid "
12539 "low pc, for referencing DIE %s [in module %s]"),
12540 sect_offset_str (die->sect_off), objfile_name (objfile));
12541 else
12543 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12544 - baseaddr);
12545 call_site->target.set_loc_physaddr (lowpc);
12549 else
12550 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12551 "block nor reference, for DIE %s [in module %s]"),
12552 sect_offset_str (die->sect_off), objfile_name (objfile));
12554 for (child_die = die->child;
12555 child_die && child_die->tag;
12556 child_die = child_die->sibling)
12558 struct call_site_parameter *parameter;
12559 struct attribute *loc, *origin;
12561 if (child_die->tag != DW_TAG_call_site_parameter
12562 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12564 /* Already printed the complaint above. */
12565 continue;
12568 gdb_assert (call_site->parameter_count < nparams);
12569 parameter = &call_site->parameter[call_site->parameter_count];
12571 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12572 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12573 register is contained in DW_AT_call_value. */
12575 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12576 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12577 if (origin == NULL)
12579 /* This was a pre-DWARF-5 GNU extension alias
12580 for DW_AT_call_parameter. */
12581 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12583 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12585 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12587 sect_offset sect_off = origin->get_ref_die_offset ();
12588 if (!cu->header.offset_in_cu_p (sect_off))
12590 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12591 binding can be done only inside one CU. Such referenced DIE
12592 therefore cannot be even moved to DW_TAG_partial_unit. */
12593 complaint (_("DW_AT_call_parameter offset is not in CU for "
12594 "DW_TAG_call_site child DIE %s [in module %s]"),
12595 sect_offset_str (child_die->sect_off),
12596 objfile_name (objfile));
12597 continue;
12599 parameter->u.param_cu_off
12600 = (cu_offset) (sect_off - cu->header.sect_off);
12602 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12604 complaint (_("No DW_FORM_block* DW_AT_location for "
12605 "DW_TAG_call_site child DIE %s [in module %s]"),
12606 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12607 continue;
12609 else
12611 struct dwarf_block *block = loc->as_block ();
12613 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12614 (block->data, &block->data[block->size]);
12615 if (parameter->u.dwarf_reg != -1)
12616 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12617 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12618 &block->data[block->size],
12619 &parameter->u.fb_offset))
12620 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12621 else
12623 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12624 "for DW_FORM_block* DW_AT_location is supported for "
12625 "DW_TAG_call_site child DIE %s "
12626 "[in module %s]"),
12627 sect_offset_str (child_die->sect_off),
12628 objfile_name (objfile));
12629 continue;
12633 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12634 if (attr == NULL)
12635 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12636 if (attr == NULL || !attr->form_is_block ())
12638 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12639 "DW_TAG_call_site child DIE %s [in module %s]"),
12640 sect_offset_str (child_die->sect_off),
12641 objfile_name (objfile));
12642 continue;
12645 struct dwarf_block *block = attr->as_block ();
12646 parameter->value = block->data;
12647 parameter->value_size = block->size;
12649 /* Parameters are not pre-cleared by memset above. */
12650 parameter->data_value = NULL;
12651 parameter->data_value_size = 0;
12652 call_site->parameter_count++;
12654 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12655 if (attr == NULL)
12656 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12657 if (attr != nullptr)
12659 if (!attr->form_is_block ())
12660 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12661 "DW_TAG_call_site child DIE %s [in module %s]"),
12662 sect_offset_str (child_die->sect_off),
12663 objfile_name (objfile));
12664 else
12666 block = attr->as_block ();
12667 parameter->data_value = block->data;
12668 parameter->data_value_size = block->size;
12674 /* Helper function for read_variable. If DIE represents a virtual
12675 table, then return the type of the concrete object that is
12676 associated with the virtual table. Otherwise, return NULL. */
12678 static struct type *
12679 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12681 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12682 if (attr == NULL)
12683 return NULL;
12685 /* Find the type DIE. */
12686 struct die_info *type_die = NULL;
12687 struct dwarf2_cu *type_cu = cu;
12689 if (attr->form_is_ref ())
12690 type_die = follow_die_ref (die, attr, &type_cu);
12691 if (type_die == NULL)
12692 return NULL;
12694 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12695 return NULL;
12696 return die_containing_type (type_die, type_cu);
12699 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12701 static void
12702 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12704 struct rust_vtable_symbol *storage = NULL;
12706 if (cu->lang () == language_rust)
12708 struct type *containing_type = rust_containing_type (die, cu);
12710 if (containing_type != NULL)
12712 struct objfile *objfile = cu->per_objfile->objfile;
12714 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12715 storage->concrete_type = containing_type;
12716 storage->subclass = SYMBOL_RUST_VTABLE;
12720 struct symbol *res = new_symbol (die, NULL, cu, storage);
12721 struct attribute *abstract_origin
12722 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12723 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12724 if (res == NULL && loc && abstract_origin)
12726 /* We have a variable without a name, but with a location and an abstract
12727 origin. This may be a concrete instance of an abstract variable
12728 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12729 later. */
12730 struct dwarf2_cu *origin_cu = cu;
12731 struct die_info *origin_die
12732 = follow_die_ref (die, abstract_origin, &origin_cu);
12733 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12734 per_objfile->per_bfd->abstract_to_concrete
12735 [origin_die->sect_off].push_back (die->sect_off);
12739 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12740 reading .debug_rnglists.
12741 Callback's type should be:
12742 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12743 Return true if the attributes are present and valid, otherwise,
12744 return false. */
12746 template <typename Callback>
12747 static bool
12748 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12749 dwarf_tag tag, Callback &&callback)
12751 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12752 struct objfile *objfile = per_objfile->objfile;
12753 bfd *obfd = objfile->obfd.get ();
12754 /* Base address selection entry. */
12755 gdb::optional<CORE_ADDR> base;
12756 const gdb_byte *buffer;
12757 bool overflow = false;
12758 ULONGEST addr_index;
12759 struct dwarf2_section_info *rnglists_section;
12761 base = cu->base_address;
12762 rnglists_section = cu_debug_rnglists_section (cu, tag);
12763 rnglists_section->read (objfile);
12765 if (offset >= rnglists_section->size)
12767 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12768 offset);
12769 return false;
12771 buffer = rnglists_section->buffer + offset;
12773 while (1)
12775 /* Initialize it due to a false compiler warning. */
12776 CORE_ADDR range_beginning = 0, range_end = 0;
12777 const gdb_byte *buf_end = (rnglists_section->buffer
12778 + rnglists_section->size);
12779 unsigned int bytes_read;
12781 if (buffer == buf_end)
12783 overflow = true;
12784 break;
12786 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12787 switch (rlet)
12789 case DW_RLE_end_of_list:
12790 break;
12791 case DW_RLE_base_address:
12792 if (buffer + cu->header.addr_size > buf_end)
12794 overflow = true;
12795 break;
12797 base = cu->header.read_address (obfd, buffer, &bytes_read);
12798 buffer += bytes_read;
12799 break;
12800 case DW_RLE_base_addressx:
12801 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12802 buffer += bytes_read;
12803 base = read_addr_index (cu, addr_index);
12804 break;
12805 case DW_RLE_start_length:
12806 if (buffer + cu->header.addr_size > buf_end)
12808 overflow = true;
12809 break;
12811 range_beginning = cu->header.read_address (obfd, buffer,
12812 &bytes_read);
12813 buffer += bytes_read;
12814 range_end = (range_beginning
12815 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12816 buffer += bytes_read;
12817 if (buffer > buf_end)
12819 overflow = true;
12820 break;
12822 break;
12823 case DW_RLE_startx_length:
12824 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12825 buffer += bytes_read;
12826 range_beginning = read_addr_index (cu, addr_index);
12827 if (buffer > buf_end)
12829 overflow = true;
12830 break;
12832 range_end = (range_beginning
12833 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12834 buffer += bytes_read;
12835 break;
12836 case DW_RLE_offset_pair:
12837 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12838 buffer += bytes_read;
12839 if (buffer > buf_end)
12841 overflow = true;
12842 break;
12844 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12845 buffer += bytes_read;
12846 if (buffer > buf_end)
12848 overflow = true;
12849 break;
12851 break;
12852 case DW_RLE_start_end:
12853 if (buffer + 2 * cu->header.addr_size > buf_end)
12855 overflow = true;
12856 break;
12858 range_beginning = cu->header.read_address (obfd, buffer,
12859 &bytes_read);
12860 buffer += bytes_read;
12861 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12862 buffer += bytes_read;
12863 break;
12864 case DW_RLE_startx_endx:
12865 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12866 buffer += bytes_read;
12867 range_beginning = read_addr_index (cu, addr_index);
12868 if (buffer > buf_end)
12870 overflow = true;
12871 break;
12873 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12874 buffer += bytes_read;
12875 range_end = read_addr_index (cu, addr_index);
12876 break;
12877 default:
12878 complaint (_("Invalid .debug_rnglists data (no base address)"));
12879 return false;
12881 if (rlet == DW_RLE_end_of_list || overflow)
12882 break;
12883 if (rlet == DW_RLE_base_address)
12884 continue;
12886 if (range_beginning > range_end)
12888 /* Inverted range entries are invalid. */
12889 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12890 return false;
12893 /* Empty range entries have no effect. */
12894 if (range_beginning == range_end)
12895 continue;
12897 /* Only DW_RLE_offset_pair needs the base address added. */
12898 if (rlet == DW_RLE_offset_pair)
12900 if (!base.has_value ())
12902 /* We have no valid base address for the DW_RLE_offset_pair. */
12903 complaint (_("Invalid .debug_rnglists data (no base address for "
12904 "DW_RLE_offset_pair)"));
12905 return false;
12908 range_beginning += *base;
12909 range_end += *base;
12912 /* A not-uncommon case of bad debug info.
12913 Don't pollute the addrmap with bad data. */
12914 if (range_beginning == 0
12915 && !per_objfile->per_bfd->has_section_at_zero)
12917 complaint (_(".debug_rnglists entry has start address of zero"
12918 " [in module %s]"), objfile_name (objfile));
12919 continue;
12922 callback (range_beginning, range_end);
12925 if (overflow)
12927 complaint (_("Offset %d is not terminated "
12928 "for DW_AT_ranges attribute"),
12929 offset);
12930 return false;
12933 return true;
12936 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12937 Callback's type should be:
12938 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12939 Return 1 if the attributes are present and valid, otherwise, return 0. */
12941 template <typename Callback>
12942 static int
12943 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12944 Callback &&callback)
12946 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12947 struct objfile *objfile = per_objfile->objfile;
12948 struct comp_unit_head *cu_header = &cu->header;
12949 bfd *obfd = objfile->obfd.get ();
12950 unsigned int addr_size = cu_header->addr_size;
12951 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12952 /* Base address selection entry. */
12953 gdb::optional<CORE_ADDR> base;
12954 unsigned int dummy;
12955 const gdb_byte *buffer;
12957 if (cu_header->version >= 5)
12958 return dwarf2_rnglists_process (offset, cu, tag, callback);
12960 base = cu->base_address;
12962 per_objfile->per_bfd->ranges.read (objfile);
12963 if (offset >= per_objfile->per_bfd->ranges.size)
12965 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12966 offset);
12967 return 0;
12969 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12971 while (1)
12973 CORE_ADDR range_beginning, range_end;
12975 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12976 buffer += addr_size;
12977 range_end = cu->header.read_address (obfd, buffer, &dummy);
12978 buffer += addr_size;
12979 offset += 2 * addr_size;
12981 /* An end of list marker is a pair of zero addresses. */
12982 if (range_beginning == 0 && range_end == 0)
12983 /* Found the end of list entry. */
12984 break;
12986 /* Each base address selection entry is a pair of 2 values.
12987 The first is the largest possible address, the second is
12988 the base address. Check for a base address here. */
12989 if ((range_beginning & mask) == mask)
12991 /* If we found the largest possible address, then we already
12992 have the base address in range_end. */
12993 base = range_end;
12994 continue;
12997 if (!base.has_value ())
12999 /* We have no valid base address for the ranges
13000 data. */
13001 complaint (_("Invalid .debug_ranges data (no base address)"));
13002 return 0;
13005 if (range_beginning > range_end)
13007 /* Inverted range entries are invalid. */
13008 complaint (_("Invalid .debug_ranges data (inverted range)"));
13009 return 0;
13012 /* Empty range entries have no effect. */
13013 if (range_beginning == range_end)
13014 continue;
13016 range_beginning += *base;
13017 range_end += *base;
13019 /* A not-uncommon case of bad debug info.
13020 Don't pollute the addrmap with bad data. */
13021 if (range_beginning == 0
13022 && !per_objfile->per_bfd->has_section_at_zero)
13024 complaint (_(".debug_ranges entry has start address of zero"
13025 " [in module %s]"), objfile_name (objfile));
13026 continue;
13029 callback (range_beginning, range_end);
13032 return 1;
13035 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13036 Return 1 if the attributes are present and valid, otherwise, return 0.
13037 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
13038 ranges in MAP are set, using DATUM as the value. */
13040 static int
13041 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13042 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13043 addrmap *map, void *datum, dwarf_tag tag)
13045 struct objfile *objfile = cu->per_objfile->objfile;
13046 struct gdbarch *gdbarch = objfile->arch ();
13047 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13048 int low_set = 0;
13049 CORE_ADDR low = 0;
13050 CORE_ADDR high = 0;
13051 int retval;
13053 retval = dwarf2_ranges_process (offset, cu, tag,
13054 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13056 if (map != nullptr)
13058 CORE_ADDR lowpc;
13059 CORE_ADDR highpc;
13061 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13062 range_beginning + baseaddr)
13063 - baseaddr);
13064 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13065 range_end + baseaddr)
13066 - baseaddr);
13067 map->set_empty (lowpc, highpc - 1, datum);
13070 /* FIXME: This is recording everything as a low-high
13071 segment of consecutive addresses. We should have a
13072 data structure for discontiguous block ranges
13073 instead. */
13074 if (! low_set)
13076 low = range_beginning;
13077 high = range_end;
13078 low_set = 1;
13080 else
13082 if (range_beginning < low)
13083 low = range_beginning;
13084 if (range_end > high)
13085 high = range_end;
13088 if (!retval)
13089 return 0;
13091 if (! low_set)
13092 /* If the first entry is an end-of-list marker, the range
13093 describes an empty scope, i.e. no instructions. */
13094 return 0;
13096 if (low_return)
13097 *low_return = low;
13098 if (high_return)
13099 *high_return = high;
13100 return 1;
13103 /* Process ranges and fill in a vector of the low PC values only. */
13105 static void
13106 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
13107 dwarf_tag tag,
13108 std::vector<CORE_ADDR> &result)
13110 dwarf2_ranges_process (offset, cu, tag,
13111 [&] (CORE_ADDR start, CORE_ADDR end)
13113 result.push_back (start);
13117 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13118 definition for the return value. *LOWPC and *HIGHPC are set iff
13119 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13121 static enum pc_bounds_kind
13122 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13123 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13124 addrmap *map, void *datum)
13126 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13127 struct attribute *attr;
13128 struct attribute *attr_high;
13129 CORE_ADDR low = 0;
13130 CORE_ADDR high = 0;
13131 enum pc_bounds_kind ret;
13133 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13134 if (attr_high)
13136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13137 if (attr != nullptr)
13139 low = attr->as_address ();
13140 high = attr_high->as_address ();
13141 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13142 high += low;
13144 else
13145 /* Found high w/o low attribute. */
13146 return PC_BOUNDS_INVALID;
13148 /* Found consecutive range of addresses. */
13149 ret = PC_BOUNDS_HIGH_LOW;
13151 else
13153 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13154 if (attr != nullptr && attr->form_is_unsigned ())
13156 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13157 on DWARF version). */
13158 ULONGEST ranges_offset = attr->as_unsigned ();
13160 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13161 this value. */
13162 if (die->tag != DW_TAG_compile_unit)
13163 ranges_offset += cu->gnu_ranges_base;
13165 /* Value of the DW_AT_ranges attribute is the offset in the
13166 .debug_ranges section. */
13167 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13168 map, datum, die->tag))
13169 return PC_BOUNDS_INVALID;
13170 /* Found discontinuous range of addresses. */
13171 ret = PC_BOUNDS_RANGES;
13173 else
13174 return PC_BOUNDS_NOT_PRESENT;
13177 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13178 if (high <= low)
13179 return PC_BOUNDS_INVALID;
13181 /* When using the GNU linker, .gnu.linkonce. sections are used to
13182 eliminate duplicate copies of functions and vtables and such.
13183 The linker will arbitrarily choose one and discard the others.
13184 The AT_*_pc values for such functions refer to local labels in
13185 these sections. If the section from that file was discarded, the
13186 labels are not in the output, so the relocs get a value of 0.
13187 If this is a discarded function, mark the pc bounds as invalid,
13188 so that GDB will ignore it. */
13189 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13190 return PC_BOUNDS_INVALID;
13192 *lowpc = low;
13193 if (highpc)
13194 *highpc = high;
13195 return ret;
13198 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13199 its low and high PC addresses. Do nothing if these addresses could not
13200 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13201 and HIGHPC to the high address if greater than HIGHPC. */
13203 static void
13204 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13206 struct dwarf2_cu *cu)
13208 CORE_ADDR low, high;
13209 struct die_info *child = die->child;
13211 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13212 >= PC_BOUNDS_RANGES)
13214 *lowpc = std::min (*lowpc, low);
13215 *highpc = std::max (*highpc, high);
13218 /* If the language does not allow nested subprograms (either inside
13219 subprograms or lexical blocks), we're done. */
13220 if (cu->lang () != language_ada)
13221 return;
13223 /* Check all the children of the given DIE. If it contains nested
13224 subprograms, then check their pc bounds. Likewise, we need to
13225 check lexical blocks as well, as they may also contain subprogram
13226 definitions. */
13227 while (child && child->tag)
13229 if (child->tag == DW_TAG_subprogram
13230 || child->tag == DW_TAG_lexical_block)
13231 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13232 child = child->sibling;
13236 /* Get the low and high pc's represented by the scope DIE, and store
13237 them in *LOWPC and *HIGHPC. If the correct values can't be
13238 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13240 static void
13241 get_scope_pc_bounds (struct die_info *die,
13242 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13243 struct dwarf2_cu *cu)
13245 CORE_ADDR best_low = (CORE_ADDR) -1;
13246 CORE_ADDR best_high = (CORE_ADDR) 0;
13247 CORE_ADDR current_low, current_high;
13249 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13250 nullptr, nullptr)
13251 >= PC_BOUNDS_RANGES)
13253 best_low = current_low;
13254 best_high = current_high;
13256 else
13258 struct die_info *child = die->child;
13260 while (child && child->tag)
13262 switch (child->tag) {
13263 case DW_TAG_subprogram:
13264 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13265 break;
13266 case DW_TAG_namespace:
13267 case DW_TAG_module:
13268 /* FIXME: carlton/2004-01-16: Should we do this for
13269 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13270 that current GCC's always emit the DIEs corresponding
13271 to definitions of methods of classes as children of a
13272 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13273 the DIEs giving the declarations, which could be
13274 anywhere). But I don't see any reason why the
13275 standards says that they have to be there. */
13276 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13278 if (current_low != ((CORE_ADDR) -1))
13280 best_low = std::min (best_low, current_low);
13281 best_high = std::max (best_high, current_high);
13283 break;
13284 default:
13285 /* Ignore. */
13286 break;
13289 child = child->sibling;
13293 *lowpc = best_low;
13294 *highpc = best_high;
13297 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13298 in DIE. */
13300 static void
13301 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13302 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13304 struct objfile *objfile = cu->per_objfile->objfile;
13305 struct gdbarch *gdbarch = objfile->arch ();
13306 struct attribute *attr;
13307 struct attribute *attr_high;
13309 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13310 if (attr_high)
13312 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13313 if (attr != nullptr)
13315 CORE_ADDR low = attr->as_address ();
13316 CORE_ADDR high = attr_high->as_address ();
13318 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13319 high += low;
13321 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13322 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13323 cu->get_builder ()->record_block_range (block, low, high - 1);
13327 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13328 if (attr != nullptr && attr->form_is_unsigned ())
13330 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13331 on DWARF version). */
13332 ULONGEST ranges_offset = attr->as_unsigned ();
13334 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13335 this value. */
13336 if (die->tag != DW_TAG_compile_unit)
13337 ranges_offset += cu->gnu_ranges_base;
13339 std::vector<blockrange> blockvec;
13340 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13341 [&] (CORE_ADDR start, CORE_ADDR end)
13343 start += baseaddr;
13344 end += baseaddr;
13345 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13346 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13347 cu->get_builder ()->record_block_range (block, start, end - 1);
13348 blockvec.emplace_back (start, end);
13351 block->set_ranges (make_blockranges (objfile, blockvec));
13355 /* Check whether the producer field indicates either of GCC < 4.6, or the
13356 Intel C/C++ compiler, and cache the result in CU. */
13358 static void
13359 check_producer (struct dwarf2_cu *cu)
13361 int major, minor;
13363 if (cu->producer == NULL)
13365 /* For unknown compilers expect their behavior is DWARF version
13366 compliant.
13368 GCC started to support .debug_types sections by -gdwarf-4 since
13369 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13370 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13371 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13372 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13374 else if (producer_is_gcc (cu->producer, &major, &minor))
13376 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13377 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13378 cu->producer_is_gcc_11 = major == 11;
13380 else if (producer_is_icc (cu->producer, &major, &minor))
13382 cu->producer_is_icc = true;
13383 cu->producer_is_icc_lt_14 = major < 14;
13385 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13386 cu->producer_is_codewarrior = true;
13387 else if (producer_is_clang (cu->producer, &major, &minor))
13388 cu->producer_is_clang = true;
13389 else
13391 /* For other non-GCC compilers, expect their behavior is DWARF version
13392 compliant. */
13395 cu->checked_producer = true;
13398 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13399 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13400 during 4.6.0 experimental. */
13402 static bool
13403 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13405 if (!cu->checked_producer)
13406 check_producer (cu);
13408 return cu->producer_is_gxx_lt_4_6;
13412 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13413 with incorrect is_stmt attributes. */
13415 static bool
13416 producer_is_codewarrior (struct dwarf2_cu *cu)
13418 if (!cu->checked_producer)
13419 check_producer (cu);
13421 return cu->producer_is_codewarrior;
13424 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13425 If that attribute is not available, return the appropriate
13426 default. */
13428 static enum dwarf_access_attribute
13429 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13431 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13432 if (attr != nullptr)
13434 LONGEST value = attr->constant_value (-1);
13435 if (value == DW_ACCESS_public
13436 || value == DW_ACCESS_protected
13437 || value == DW_ACCESS_private)
13438 return (dwarf_access_attribute) value;
13439 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13440 plongest (value));
13443 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13445 /* The default DWARF 2 accessibility for members is public, the default
13446 accessibility for inheritance is private. */
13448 if (die->tag != DW_TAG_inheritance)
13449 return DW_ACCESS_public;
13450 else
13451 return DW_ACCESS_private;
13453 else
13455 /* DWARF 3+ defines the default accessibility a different way. The same
13456 rules apply now for DW_TAG_inheritance as for the members and it only
13457 depends on the container kind. */
13459 if (die->parent->tag == DW_TAG_class_type)
13460 return DW_ACCESS_private;
13461 else
13462 return DW_ACCESS_public;
13466 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13467 *OFFSET to the byte offset. If the attribute was not found return
13468 0, otherwise return 1. If it was found but could not properly be
13469 handled, set *OFFSET to 0. */
13471 static int
13472 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13473 LONGEST *offset)
13475 struct attribute *attr;
13477 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13478 if (attr != NULL)
13480 *offset = 0;
13482 /* Note that we do not check for a section offset first here.
13483 This is because DW_AT_data_member_location is new in DWARF 4,
13484 so if we see it, we can assume that a constant form is really
13485 a constant and not a section offset. */
13486 if (attr->form_is_constant ())
13487 *offset = attr->constant_value (0);
13488 else if (attr->form_is_section_offset ())
13489 dwarf2_complex_location_expr_complaint ();
13490 else if (attr->form_is_block ())
13491 *offset = decode_locdesc (attr->as_block (), cu);
13492 else
13493 dwarf2_complex_location_expr_complaint ();
13495 return 1;
13497 else
13499 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13500 if (attr != nullptr)
13502 *offset = attr->constant_value (0);
13503 return 1;
13507 return 0;
13510 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13511 store the results in FIELD. */
13513 static void
13514 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13515 struct field *field)
13517 struct attribute *attr;
13519 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13520 if (attr != NULL)
13522 if (attr->form_is_constant ())
13524 LONGEST offset = attr->constant_value (0);
13526 /* Work around this GCC 11 bug, where it would erroneously use -1
13527 data member locations, instead of 0:
13529 Negative DW_AT_data_member_location
13530 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13532 if (offset == -1 && cu->producer_is_gcc_11)
13534 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13535 offset = 0;
13538 field->set_loc_bitpos (offset * bits_per_byte);
13540 else if (attr->form_is_section_offset ())
13541 dwarf2_complex_location_expr_complaint ();
13542 else if (attr->form_is_block ())
13544 bool handled;
13545 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13546 if (handled)
13547 field->set_loc_bitpos (offset * bits_per_byte);
13548 else
13550 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13551 struct objfile *objfile = per_objfile->objfile;
13552 struct dwarf2_locexpr_baton *dlbaton
13553 = XOBNEW (&objfile->objfile_obstack,
13554 struct dwarf2_locexpr_baton);
13555 dlbaton->data = attr->as_block ()->data;
13556 dlbaton->size = attr->as_block ()->size;
13557 /* When using this baton, we want to compute the address
13558 of the field, not the value. This is why
13559 is_reference is set to false here. */
13560 dlbaton->is_reference = false;
13561 dlbaton->per_objfile = per_objfile;
13562 dlbaton->per_cu = cu->per_cu;
13564 field->set_loc_dwarf_block (dlbaton);
13567 else
13568 dwarf2_complex_location_expr_complaint ();
13570 else
13572 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13573 if (attr != nullptr)
13574 field->set_loc_bitpos (attr->constant_value (0));
13578 /* Add an aggregate field to the field list. */
13580 static void
13581 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13582 struct dwarf2_cu *cu)
13584 struct objfile *objfile = cu->per_objfile->objfile;
13585 struct gdbarch *gdbarch = objfile->arch ();
13586 struct nextfield *new_field;
13587 struct attribute *attr;
13588 struct field *fp;
13589 const char *fieldname = "";
13591 if (die->tag == DW_TAG_inheritance)
13593 fip->baseclasses.emplace_back ();
13594 new_field = &fip->baseclasses.back ();
13596 else
13598 fip->fields.emplace_back ();
13599 new_field = &fip->fields.back ();
13602 new_field->offset = die->sect_off;
13604 new_field->accessibility = dwarf2_access_attribute (die, cu);
13605 if (new_field->accessibility != DW_ACCESS_public)
13606 fip->non_public_fields = true;
13608 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13609 if (attr != nullptr)
13610 new_field->virtuality = attr->as_virtuality ();
13611 else
13612 new_field->virtuality = DW_VIRTUALITY_none;
13614 fp = &new_field->field;
13616 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13617 && !die_is_declaration (die, cu))
13619 if (die->tag == DW_TAG_namelist_item)
13621 /* Typically, DW_TAG_namelist_item are references to namelist items.
13622 If so, follow that reference. */
13623 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13624 struct die_info *item_die = nullptr;
13625 struct dwarf2_cu *item_cu = cu;
13626 if (attr1->form_is_ref ())
13627 item_die = follow_die_ref (die, attr1, &item_cu);
13628 if (item_die != nullptr)
13629 die = item_die;
13631 /* Data member other than a C++ static data member. */
13633 /* Get type of field. */
13634 fp->set_type (die_type (die, cu));
13636 fp->set_loc_bitpos (0);
13638 /* Get bit size of field (zero if none). */
13639 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13640 if (attr != nullptr)
13642 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13644 else
13646 FIELD_BITSIZE (*fp) = 0;
13649 /* Get bit offset of field. */
13650 handle_member_location (die, cu, fp);
13651 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13652 if (attr != nullptr && attr->form_is_constant ())
13654 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13656 /* For big endian bits, the DW_AT_bit_offset gives the
13657 additional bit offset from the MSB of the containing
13658 anonymous object to the MSB of the field. We don't
13659 have to do anything special since we don't need to
13660 know the size of the anonymous object. */
13661 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13663 else
13665 /* For little endian bits, compute the bit offset to the
13666 MSB of the anonymous object, subtract off the number of
13667 bits from the MSB of the field to the MSB of the
13668 object, and then subtract off the number of bits of
13669 the field itself. The result is the bit offset of
13670 the LSB of the field. */
13671 int anonymous_size;
13672 int bit_offset = attr->constant_value (0);
13674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13675 if (attr != nullptr && attr->form_is_constant ())
13677 /* The size of the anonymous object containing
13678 the bit field is explicit, so use the
13679 indicated size (in bytes). */
13680 anonymous_size = attr->constant_value (0);
13682 else
13684 /* The size of the anonymous object containing
13685 the bit field must be inferred from the type
13686 attribute of the data member containing the
13687 bit field. */
13688 anonymous_size = fp->type ()->length ();
13690 fp->set_loc_bitpos (fp->loc_bitpos ()
13691 + anonymous_size * bits_per_byte
13692 - bit_offset - FIELD_BITSIZE (*fp));
13696 /* Get name of field. */
13697 fieldname = dwarf2_name (die, cu);
13698 if (fieldname == NULL)
13699 fieldname = "";
13701 /* The name is already allocated along with this objfile, so we don't
13702 need to duplicate it for the type. */
13703 fp->set_name (fieldname);
13705 /* Change accessibility for artificial fields (e.g. virtual table
13706 pointer or virtual base class pointer) to private. */
13707 if (dwarf2_attr (die, DW_AT_artificial, cu))
13709 FIELD_ARTIFICIAL (*fp) = 1;
13710 new_field->accessibility = DW_ACCESS_private;
13711 fip->non_public_fields = true;
13714 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13716 /* C++ static member. */
13718 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13719 is a declaration, but all versions of G++ as of this writing
13720 (so through at least 3.2.1) incorrectly generate
13721 DW_TAG_variable tags. */
13723 const char *physname;
13725 /* Get name of field. */
13726 fieldname = dwarf2_name (die, cu);
13727 if (fieldname == NULL)
13728 return;
13730 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13731 if (attr
13732 /* Only create a symbol if this is an external value.
13733 new_symbol checks this and puts the value in the global symbol
13734 table, which we want. If it is not external, new_symbol
13735 will try to put the value in cu->list_in_scope which is wrong. */
13736 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13738 /* A static const member, not much different than an enum as far as
13739 we're concerned, except that we can support more types. */
13740 new_symbol (die, NULL, cu);
13743 /* Get physical name. */
13744 physname = dwarf2_physname (fieldname, die, cu);
13746 /* The name is already allocated along with this objfile, so we don't
13747 need to duplicate it for the type. */
13748 fp->set_loc_physname (physname ? physname : "");
13749 fp->set_type (die_type (die, cu));
13750 fp->set_name (fieldname);
13752 else if (die->tag == DW_TAG_inheritance)
13754 /* C++ base class field. */
13755 handle_member_location (die, cu, fp);
13756 FIELD_BITSIZE (*fp) = 0;
13757 fp->set_type (die_type (die, cu));
13758 fp->set_name (fp->type ()->name ());
13760 else
13761 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13764 /* Can the type given by DIE define another type? */
13766 static bool
13767 type_can_define_types (const struct die_info *die)
13769 switch (die->tag)
13771 case DW_TAG_typedef:
13772 case DW_TAG_class_type:
13773 case DW_TAG_structure_type:
13774 case DW_TAG_union_type:
13775 case DW_TAG_enumeration_type:
13776 return true;
13778 default:
13779 return false;
13783 /* Add a type definition defined in the scope of the FIP's class. */
13785 static void
13786 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13787 struct dwarf2_cu *cu)
13789 struct decl_field fp;
13790 memset (&fp, 0, sizeof (fp));
13792 gdb_assert (type_can_define_types (die));
13794 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13795 fp.name = dwarf2_name (die, cu);
13796 fp.type = read_type_die (die, cu);
13798 /* Save accessibility. */
13799 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13800 switch (accessibility)
13802 case DW_ACCESS_public:
13803 /* The assumed value if neither private nor protected. */
13804 break;
13805 case DW_ACCESS_private:
13806 fp.is_private = 1;
13807 break;
13808 case DW_ACCESS_protected:
13809 fp.is_protected = 1;
13810 break;
13813 if (die->tag == DW_TAG_typedef)
13814 fip->typedef_field_list.push_back (fp);
13815 else
13816 fip->nested_types_list.push_back (fp);
13819 /* A convenience typedef that's used when finding the discriminant
13820 field for a variant part. */
13821 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13822 offset_map_type;
13824 /* Compute the discriminant range for a given variant. OBSTACK is
13825 where the results will be stored. VARIANT is the variant to
13826 process. IS_UNSIGNED indicates whether the discriminant is signed
13827 or unsigned. */
13829 static const gdb::array_view<discriminant_range>
13830 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13831 bool is_unsigned)
13833 std::vector<discriminant_range> ranges;
13835 if (variant.default_branch)
13836 return {};
13838 if (variant.discr_list_data == nullptr)
13840 discriminant_range r
13841 = {variant.discriminant_value, variant.discriminant_value};
13842 ranges.push_back (r);
13844 else
13846 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13847 variant.discr_list_data->size);
13848 while (!data.empty ())
13850 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13852 complaint (_("invalid discriminant marker: %d"), data[0]);
13853 break;
13855 bool is_range = data[0] == DW_DSC_range;
13856 data = data.slice (1);
13858 ULONGEST low, high;
13859 unsigned int bytes_read;
13861 if (data.empty ())
13863 complaint (_("DW_AT_discr_list missing low value"));
13864 break;
13866 if (is_unsigned)
13867 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13868 else
13869 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13870 &bytes_read);
13871 data = data.slice (bytes_read);
13873 if (is_range)
13875 if (data.empty ())
13877 complaint (_("DW_AT_discr_list missing high value"));
13878 break;
13880 if (is_unsigned)
13881 high = read_unsigned_leb128 (nullptr, data.data (),
13882 &bytes_read);
13883 else
13884 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13885 &bytes_read);
13886 data = data.slice (bytes_read);
13888 else
13889 high = low;
13891 ranges.push_back ({ low, high });
13895 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13896 ranges.size ());
13897 std::copy (ranges.begin (), ranges.end (), result);
13898 return gdb::array_view<discriminant_range> (result, ranges.size ());
13901 static const gdb::array_view<variant_part> create_variant_parts
13902 (struct obstack *obstack,
13903 const offset_map_type &offset_map,
13904 struct field_info *fi,
13905 const std::vector<variant_part_builder> &variant_parts);
13907 /* Fill in a "struct variant" for a given variant field. RESULT is
13908 the variant to fill in. OBSTACK is where any needed allocations
13909 will be done. OFFSET_MAP holds the mapping from section offsets to
13910 fields for the type. FI describes the fields of the type we're
13911 processing. FIELD is the variant field we're converting. */
13913 static void
13914 create_one_variant (variant &result, struct obstack *obstack,
13915 const offset_map_type &offset_map,
13916 struct field_info *fi, const variant_field &field)
13918 result.discriminants = convert_variant_range (obstack, field, false);
13919 result.first_field = field.first_field + fi->baseclasses.size ();
13920 result.last_field = field.last_field + fi->baseclasses.size ();
13921 result.parts = create_variant_parts (obstack, offset_map, fi,
13922 field.variant_parts);
13925 /* Fill in a "struct variant_part" for a given variant part. RESULT
13926 is the variant part to fill in. OBSTACK is where any needed
13927 allocations will be done. OFFSET_MAP holds the mapping from
13928 section offsets to fields for the type. FI describes the fields of
13929 the type we're processing. BUILDER is the variant part to be
13930 converted. */
13932 static void
13933 create_one_variant_part (variant_part &result,
13934 struct obstack *obstack,
13935 const offset_map_type &offset_map,
13936 struct field_info *fi,
13937 const variant_part_builder &builder)
13939 auto iter = offset_map.find (builder.discriminant_offset);
13940 if (iter == offset_map.end ())
13942 result.discriminant_index = -1;
13943 /* Doesn't matter. */
13944 result.is_unsigned = false;
13946 else
13948 result.discriminant_index = iter->second;
13949 result.is_unsigned
13950 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13953 size_t n = builder.variants.size ();
13954 variant *output = new (obstack) variant[n];
13955 for (size_t i = 0; i < n; ++i)
13956 create_one_variant (output[i], obstack, offset_map, fi,
13957 builder.variants[i]);
13959 result.variants = gdb::array_view<variant> (output, n);
13962 /* Create a vector of variant parts that can be attached to a type.
13963 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13964 holds the mapping from section offsets to fields for the type. FI
13965 describes the fields of the type we're processing. VARIANT_PARTS
13966 is the vector to convert. */
13968 static const gdb::array_view<variant_part>
13969 create_variant_parts (struct obstack *obstack,
13970 const offset_map_type &offset_map,
13971 struct field_info *fi,
13972 const std::vector<variant_part_builder> &variant_parts)
13974 if (variant_parts.empty ())
13975 return {};
13977 size_t n = variant_parts.size ();
13978 variant_part *result = new (obstack) variant_part[n];
13979 for (size_t i = 0; i < n; ++i)
13980 create_one_variant_part (result[i], obstack, offset_map, fi,
13981 variant_parts[i]);
13983 return gdb::array_view<variant_part> (result, n);
13986 /* Compute the variant part vector for FIP, attaching it to TYPE when
13987 done. */
13989 static void
13990 add_variant_property (struct field_info *fip, struct type *type,
13991 struct dwarf2_cu *cu)
13993 /* Map section offsets of fields to their field index. Note the
13994 field index here does not take the number of baseclasses into
13995 account. */
13996 offset_map_type offset_map;
13997 for (int i = 0; i < fip->fields.size (); ++i)
13998 offset_map[fip->fields[i].offset] = i;
14000 struct objfile *objfile = cu->per_objfile->objfile;
14001 gdb::array_view<const variant_part> parts
14002 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14003 fip->variant_parts);
14005 struct dynamic_prop prop;
14006 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14007 obstack_copy (&objfile->objfile_obstack, &parts,
14008 sizeof (parts)));
14010 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14013 /* Create the vector of fields, and attach it to the type. */
14015 static void
14016 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14017 struct dwarf2_cu *cu)
14019 int nfields = fip->nfields ();
14021 /* Record the field count, allocate space for the array of fields,
14022 and create blank accessibility bitfields if necessary. */
14023 type->set_num_fields (nfields);
14024 type->set_fields
14025 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14027 if (fip->non_public_fields && cu->lang () != language_ada)
14029 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14031 TYPE_FIELD_PRIVATE_BITS (type) =
14032 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14033 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14035 TYPE_FIELD_PROTECTED_BITS (type) =
14036 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14037 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14039 TYPE_FIELD_IGNORE_BITS (type) =
14040 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14041 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14044 /* If the type has baseclasses, allocate and clear a bit vector for
14045 TYPE_FIELD_VIRTUAL_BITS. */
14046 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
14048 int num_bytes = B_BYTES (fip->baseclasses.size ());
14049 unsigned char *pointer;
14051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14052 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14053 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14054 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14055 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14058 if (!fip->variant_parts.empty ())
14059 add_variant_property (fip, type, cu);
14061 /* Copy the saved-up fields into the field vector. */
14062 for (int i = 0; i < nfields; ++i)
14064 struct nextfield &field
14065 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14066 : fip->fields[i - fip->baseclasses.size ()]);
14068 type->field (i) = field.field;
14069 switch (field.accessibility)
14071 case DW_ACCESS_private:
14072 if (cu->lang () != language_ada)
14073 SET_TYPE_FIELD_PRIVATE (type, i);
14074 break;
14076 case DW_ACCESS_protected:
14077 if (cu->lang () != language_ada)
14078 SET_TYPE_FIELD_PROTECTED (type, i);
14079 break;
14081 case DW_ACCESS_public:
14082 break;
14084 default:
14085 /* Unknown accessibility. Complain and treat it as public. */
14087 complaint (_("unsupported accessibility %d"),
14088 field.accessibility);
14090 break;
14092 if (i < fip->baseclasses.size ())
14094 switch (field.virtuality)
14096 case DW_VIRTUALITY_virtual:
14097 case DW_VIRTUALITY_pure_virtual:
14098 if (cu->lang () == language_ada)
14099 error (_("unexpected virtuality in component of Ada type"));
14100 SET_TYPE_FIELD_VIRTUAL (type, i);
14101 break;
14107 /* Return true if this member function is a constructor, false
14108 otherwise. */
14110 static int
14111 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14113 const char *fieldname;
14114 const char *type_name;
14115 int len;
14117 if (die->parent == NULL)
14118 return 0;
14120 if (die->parent->tag != DW_TAG_structure_type
14121 && die->parent->tag != DW_TAG_union_type
14122 && die->parent->tag != DW_TAG_class_type)
14123 return 0;
14125 fieldname = dwarf2_name (die, cu);
14126 type_name = dwarf2_name (die->parent, cu);
14127 if (fieldname == NULL || type_name == NULL)
14128 return 0;
14130 len = strlen (fieldname);
14131 return (strncmp (fieldname, type_name, len) == 0
14132 && (type_name[len] == '\0' || type_name[len] == '<'));
14135 /* Add a member function to the proper fieldlist. */
14137 static void
14138 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14139 struct type *type, struct dwarf2_cu *cu)
14141 struct objfile *objfile = cu->per_objfile->objfile;
14142 struct attribute *attr;
14143 int i;
14144 struct fnfieldlist *flp = nullptr;
14145 struct fn_field *fnp;
14146 const char *fieldname;
14147 struct type *this_type;
14149 if (cu->lang () == language_ada)
14150 error (_("unexpected member function in Ada type"));
14152 /* Get name of member function. */
14153 fieldname = dwarf2_name (die, cu);
14154 if (fieldname == NULL)
14155 return;
14157 /* Look up member function name in fieldlist. */
14158 for (i = 0; i < fip->fnfieldlists.size (); i++)
14160 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14162 flp = &fip->fnfieldlists[i];
14163 break;
14167 /* Create a new fnfieldlist if necessary. */
14168 if (flp == nullptr)
14170 fip->fnfieldlists.emplace_back ();
14171 flp = &fip->fnfieldlists.back ();
14172 flp->name = fieldname;
14173 i = fip->fnfieldlists.size () - 1;
14176 /* Create a new member function field and add it to the vector of
14177 fnfieldlists. */
14178 flp->fnfields.emplace_back ();
14179 fnp = &flp->fnfields.back ();
14181 /* Delay processing of the physname until later. */
14182 if (cu->lang () == language_cplus)
14183 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14184 die, cu);
14185 else
14187 const char *physname = dwarf2_physname (fieldname, die, cu);
14188 fnp->physname = physname ? physname : "";
14191 fnp->type = alloc_type (objfile);
14192 this_type = read_type_die (die, cu);
14193 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14195 int nparams = this_type->num_fields ();
14197 /* TYPE is the domain of this method, and THIS_TYPE is the type
14198 of the method itself (TYPE_CODE_METHOD). */
14199 smash_to_method_type (fnp->type, type,
14200 this_type->target_type (),
14201 this_type->fields (),
14202 this_type->num_fields (),
14203 this_type->has_varargs ());
14205 /* Handle static member functions.
14206 Dwarf2 has no clean way to discern C++ static and non-static
14207 member functions. G++ helps GDB by marking the first
14208 parameter for non-static member functions (which is the this
14209 pointer) as artificial. We obtain this information from
14210 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14211 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14212 fnp->voffset = VOFFSET_STATIC;
14214 else
14215 complaint (_("member function type missing for '%s'"),
14216 dwarf2_full_name (fieldname, die, cu));
14218 /* Get fcontext from DW_AT_containing_type if present. */
14219 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14220 fnp->fcontext = die_containing_type (die, cu);
14222 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14223 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14225 /* Get accessibility. */
14226 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14227 switch (accessibility)
14229 case DW_ACCESS_private:
14230 fnp->is_private = 1;
14231 break;
14232 case DW_ACCESS_protected:
14233 fnp->is_protected = 1;
14234 break;
14237 /* Check for artificial methods. */
14238 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14239 if (attr && attr->as_boolean ())
14240 fnp->is_artificial = 1;
14242 /* Check for defaulted methods. */
14243 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14244 if (attr != nullptr)
14245 fnp->defaulted = attr->defaulted ();
14247 /* Check for deleted methods. */
14248 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14249 if (attr != nullptr && attr->as_boolean ())
14250 fnp->is_deleted = 1;
14252 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14254 /* Get index in virtual function table if it is a virtual member
14255 function. For older versions of GCC, this is an offset in the
14256 appropriate virtual table, as specified by DW_AT_containing_type.
14257 For everyone else, it is an expression to be evaluated relative
14258 to the object address. */
14260 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14261 if (attr != nullptr)
14263 if (attr->form_is_block () && attr->as_block ()->size > 0)
14265 struct dwarf_block *block = attr->as_block ();
14267 if (block->data[0] == DW_OP_constu)
14269 /* Old-style GCC. */
14270 fnp->voffset = decode_locdesc (block, cu) + 2;
14272 else if (block->data[0] == DW_OP_deref
14273 || (block->size > 1
14274 && block->data[0] == DW_OP_deref_size
14275 && block->data[1] == cu->header.addr_size))
14277 fnp->voffset = decode_locdesc (block, cu);
14278 if ((fnp->voffset % cu->header.addr_size) != 0)
14279 dwarf2_complex_location_expr_complaint ();
14280 else
14281 fnp->voffset /= cu->header.addr_size;
14282 fnp->voffset += 2;
14284 else
14285 dwarf2_complex_location_expr_complaint ();
14287 if (!fnp->fcontext)
14289 /* If there is no `this' field and no DW_AT_containing_type,
14290 we cannot actually find a base class context for the
14291 vtable! */
14292 if (this_type->num_fields () == 0
14293 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14295 complaint (_("cannot determine context for virtual member "
14296 "function \"%s\" (offset %s)"),
14297 fieldname, sect_offset_str (die->sect_off));
14299 else
14301 fnp->fcontext = this_type->field (0).type ()->target_type ();
14305 else if (attr->form_is_section_offset ())
14307 dwarf2_complex_location_expr_complaint ();
14309 else
14311 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14312 fieldname);
14315 else
14317 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14318 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14320 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14321 complaint (_("Member function \"%s\" (offset %s) is virtual "
14322 "but the vtable offset is not specified"),
14323 fieldname, sect_offset_str (die->sect_off));
14324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14325 TYPE_CPLUS_DYNAMIC (type) = 1;
14330 /* Create the vector of member function fields, and attach it to the type. */
14332 static void
14333 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14334 struct dwarf2_cu *cu)
14336 if (cu->lang () == language_ada)
14337 error (_("unexpected member functions in Ada type"));
14339 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14340 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14341 TYPE_ALLOC (type,
14342 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14344 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14346 struct fnfieldlist &nf = fip->fnfieldlists[i];
14347 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14349 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14350 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14351 fn_flp->fn_fields = (struct fn_field *)
14352 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14354 for (int k = 0; k < nf.fnfields.size (); ++k)
14355 fn_flp->fn_fields[k] = nf.fnfields[k];
14358 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14361 /* Returns non-zero if NAME is the name of a vtable member in CU's
14362 language, zero otherwise. */
14363 static int
14364 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14366 static const char vptr[] = "_vptr";
14368 /* Look for the C++ form of the vtable. */
14369 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14370 return 1;
14372 return 0;
14375 /* GCC outputs unnamed structures that are really pointers to member
14376 functions, with the ABI-specified layout. If TYPE describes
14377 such a structure, smash it into a member function type.
14379 GCC shouldn't do this; it should just output pointer to member DIEs.
14380 This is GCC PR debug/28767. */
14382 static void
14383 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14385 struct type *pfn_type, *self_type, *new_type;
14387 /* Check for a structure with no name and two children. */
14388 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14389 return;
14391 /* Check for __pfn and __delta members. */
14392 if (type->field (0).name () == NULL
14393 || strcmp (type->field (0).name (), "__pfn") != 0
14394 || type->field (1).name () == NULL
14395 || strcmp (type->field (1).name (), "__delta") != 0)
14396 return;
14398 /* Find the type of the method. */
14399 pfn_type = type->field (0).type ();
14400 if (pfn_type == NULL
14401 || pfn_type->code () != TYPE_CODE_PTR
14402 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
14403 return;
14405 /* Look for the "this" argument. */
14406 pfn_type = pfn_type->target_type ();
14407 if (pfn_type->num_fields () == 0
14408 /* || pfn_type->field (0).type () == NULL */
14409 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14410 return;
14412 self_type = pfn_type->field (0).type ()->target_type ();
14413 new_type = alloc_type (objfile);
14414 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
14415 pfn_type->fields (), pfn_type->num_fields (),
14416 pfn_type->has_varargs ());
14417 smash_to_methodptr_type (type, new_type);
14420 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14421 requires rewriting, then copy it and return the updated copy.
14422 Otherwise return nullptr. */
14424 static struct type *
14425 rewrite_array_type (struct type *type)
14427 if (type->code () != TYPE_CODE_ARRAY)
14428 return nullptr;
14430 struct type *index_type = type->index_type ();
14431 range_bounds *current_bounds = index_type->bounds ();
14433 /* Handle multi-dimensional arrays. */
14434 struct type *new_target = rewrite_array_type (type->target_type ());
14435 if (new_target == nullptr)
14437 /* Maybe we don't need to rewrite this array. */
14438 if (current_bounds->low.kind () == PROP_CONST
14439 && current_bounds->high.kind () == PROP_CONST)
14440 return nullptr;
14443 /* Either the target type was rewritten, or the bounds have to be
14444 updated. Either way we want to copy the type and update
14445 everything. */
14446 struct type *copy = copy_type (type);
14447 int nfields = copy->num_fields ();
14448 field *new_fields
14449 = ((struct field *) TYPE_ZALLOC (copy,
14450 nfields * sizeof (struct field)));
14451 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14452 copy->set_fields (new_fields);
14453 if (new_target != nullptr)
14454 copy->set_target_type (new_target);
14456 struct type *index_copy = copy_type (index_type);
14457 range_bounds *bounds
14458 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14459 sizeof (range_bounds));
14460 *bounds = *current_bounds;
14461 bounds->low.set_const_val (1);
14462 bounds->high.set_const_val (0);
14463 index_copy->set_bounds (bounds);
14464 copy->set_index_type (index_copy);
14466 return copy;
14469 /* While some versions of GCC will generate complicated DWARF for an
14470 array (see quirk_ada_thick_pointer), more recent versions were
14471 modified to emit an explicit thick pointer structure. However, in
14472 this case, the array still has DWARF expressions for its ranges,
14473 and these must be ignored. */
14475 static void
14476 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14477 struct type *type)
14479 gdb_assert (cu->lang () == language_ada);
14481 /* Check for a structure with two children. */
14482 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14483 return;
14485 /* Check for P_ARRAY and P_BOUNDS members. */
14486 if (type->field (0).name () == NULL
14487 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14488 || type->field (1).name () == NULL
14489 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14490 return;
14492 /* Make sure we're looking at a pointer to an array. */
14493 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14494 return;
14496 /* The Ada code already knows how to handle these types, so all that
14497 we need to do is turn the bounds into static bounds. However, we
14498 don't want to rewrite existing array or index types in-place,
14499 because those may be referenced in other contexts where this
14500 rewriting is undesirable. */
14501 struct type *new_ary_type
14502 = rewrite_array_type (type->field (0).type ()->target_type ());
14503 if (new_ary_type != nullptr)
14504 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14507 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14508 appropriate error checking and issuing complaints if there is a
14509 problem. */
14511 static ULONGEST
14512 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14514 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14516 if (attr == nullptr)
14517 return 0;
14519 if (!attr->form_is_constant ())
14521 complaint (_("DW_AT_alignment must have constant form"
14522 " - DIE at %s [in module %s]"),
14523 sect_offset_str (die->sect_off),
14524 objfile_name (cu->per_objfile->objfile));
14525 return 0;
14528 LONGEST val = attr->constant_value (0);
14529 if (val < 0)
14531 complaint (_("DW_AT_alignment value must not be negative"
14532 " - DIE at %s [in module %s]"),
14533 sect_offset_str (die->sect_off),
14534 objfile_name (cu->per_objfile->objfile));
14535 return 0;
14537 ULONGEST align = val;
14539 if (align == 0)
14541 complaint (_("DW_AT_alignment value must not be zero"
14542 " - DIE at %s [in module %s]"),
14543 sect_offset_str (die->sect_off),
14544 objfile_name (cu->per_objfile->objfile));
14545 return 0;
14547 if ((align & (align - 1)) != 0)
14549 complaint (_("DW_AT_alignment value must be a power of 2"
14550 " - DIE at %s [in module %s]"),
14551 sect_offset_str (die->sect_off),
14552 objfile_name (cu->per_objfile->objfile));
14553 return 0;
14556 return align;
14559 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14560 the alignment for TYPE. */
14562 static void
14563 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14564 struct type *type)
14566 if (!set_type_align (type, get_alignment (cu, die)))
14567 complaint (_("DW_AT_alignment value too large"
14568 " - DIE at %s [in module %s]"),
14569 sect_offset_str (die->sect_off),
14570 objfile_name (cu->per_objfile->objfile));
14573 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14574 constant for a type, according to DWARF5 spec, Table 5.5. */
14576 static bool
14577 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14579 switch (value)
14581 case DW_CC_normal:
14582 case DW_CC_pass_by_reference:
14583 case DW_CC_pass_by_value:
14584 return true;
14586 default:
14587 complaint (_("unrecognized DW_AT_calling_convention value "
14588 "(%s) for a type"), pulongest (value));
14589 return false;
14593 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14594 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14595 also according to GNU-specific values (see include/dwarf2.h). */
14597 static bool
14598 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14600 switch (value)
14602 case DW_CC_normal:
14603 case DW_CC_program:
14604 case DW_CC_nocall:
14605 return true;
14607 case DW_CC_GNU_renesas_sh:
14608 case DW_CC_GNU_borland_fastcall_i386:
14609 case DW_CC_GDB_IBM_OpenCL:
14610 return true;
14612 default:
14613 complaint (_("unrecognized DW_AT_calling_convention value "
14614 "(%s) for a subroutine"), pulongest (value));
14615 return false;
14619 /* Called when we find the DIE that starts a structure or union scope
14620 (definition) to create a type for the structure or union. Fill in
14621 the type's name and general properties; the members will not be
14622 processed until process_structure_scope. A symbol table entry for
14623 the type will also not be done until process_structure_scope (assuming
14624 the type has a name).
14626 NOTE: we need to call these functions regardless of whether or not the
14627 DIE has a DW_AT_name attribute, since it might be an anonymous
14628 structure or union. This gets the type entered into our set of
14629 user defined types. */
14631 static struct type *
14632 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14634 struct objfile *objfile = cu->per_objfile->objfile;
14635 struct type *type;
14636 struct attribute *attr;
14637 const char *name;
14639 /* If the definition of this type lives in .debug_types, read that type.
14640 Don't follow DW_AT_specification though, that will take us back up
14641 the chain and we want to go down. */
14642 attr = die->attr (DW_AT_signature);
14643 if (attr != nullptr)
14645 type = get_DW_AT_signature_type (die, attr, cu);
14647 /* The type's CU may not be the same as CU.
14648 Ensure TYPE is recorded with CU in die_type_hash. */
14649 return set_die_type (die, type, cu);
14652 type = alloc_type (objfile);
14653 INIT_CPLUS_SPECIFIC (type);
14655 name = dwarf2_name (die, cu);
14656 if (name != NULL)
14658 if (cu->lang () == language_cplus
14659 || cu->lang () == language_d
14660 || cu->lang () == language_rust)
14662 const char *full_name = dwarf2_full_name (name, die, cu);
14664 /* dwarf2_full_name might have already finished building the DIE's
14665 type. If so, there is no need to continue. */
14666 if (get_die_type (die, cu) != NULL)
14667 return get_die_type (die, cu);
14669 type->set_name (full_name);
14671 else
14673 /* The name is already allocated along with this objfile, so
14674 we don't need to duplicate it for the type. */
14675 type->set_name (name);
14679 if (die->tag == DW_TAG_structure_type)
14681 type->set_code (TYPE_CODE_STRUCT);
14683 else if (die->tag == DW_TAG_union_type)
14685 type->set_code (TYPE_CODE_UNION);
14687 else if (die->tag == DW_TAG_namelist)
14689 type->set_code (TYPE_CODE_NAMELIST);
14691 else
14693 type->set_code (TYPE_CODE_STRUCT);
14696 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
14697 type->set_is_declared_class (true);
14699 /* Store the calling convention in the type if it's available in
14700 the die. Otherwise the calling convention remains set to
14701 the default value DW_CC_normal. */
14702 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14703 if (attr != nullptr
14704 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14706 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14707 TYPE_CPLUS_CALLING_CONVENTION (type)
14708 = (enum dwarf_calling_convention) (attr->constant_value (0));
14711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14712 if (attr != nullptr)
14714 if (attr->form_is_constant ())
14715 type->set_length (attr->constant_value (0));
14716 else
14718 struct dynamic_prop prop;
14719 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14720 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14722 type->set_length (0);
14725 else
14726 type->set_length (0);
14728 maybe_set_alignment (cu, die, type);
14730 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
14732 /* ICC<14 does not output the required DW_AT_declaration on
14733 incomplete types, but gives them a size of zero. */
14734 type->set_is_stub (true);
14736 else
14737 type->set_stub_is_supported (true);
14739 if (die_is_declaration (die, cu))
14740 type->set_is_stub (true);
14741 else if (attr == NULL && die->child == NULL
14742 && producer_is_realview (cu->producer))
14743 /* RealView does not output the required DW_AT_declaration
14744 on incomplete types. */
14745 type->set_is_stub (true);
14747 /* We need to add the type field to the die immediately so we don't
14748 infinitely recurse when dealing with pointers to the structure
14749 type within the structure itself. */
14750 set_die_type (die, type, cu);
14752 /* set_die_type should be already done. */
14753 set_descriptive_type (type, die, cu);
14755 return type;
14758 static void handle_struct_member_die
14759 (struct die_info *child_die,
14760 struct type *type,
14761 struct field_info *fi,
14762 std::vector<struct symbol *> *template_args,
14763 struct dwarf2_cu *cu);
14765 /* A helper for handle_struct_member_die that handles
14766 DW_TAG_variant_part. */
14768 static void
14769 handle_variant_part (struct die_info *die, struct type *type,
14770 struct field_info *fi,
14771 std::vector<struct symbol *> *template_args,
14772 struct dwarf2_cu *cu)
14774 variant_part_builder *new_part;
14775 if (fi->current_variant_part == nullptr)
14777 fi->variant_parts.emplace_back ();
14778 new_part = &fi->variant_parts.back ();
14780 else if (!fi->current_variant_part->processing_variant)
14782 complaint (_("nested DW_TAG_variant_part seen "
14783 "- DIE at %s [in module %s]"),
14784 sect_offset_str (die->sect_off),
14785 objfile_name (cu->per_objfile->objfile));
14786 return;
14788 else
14790 variant_field &current = fi->current_variant_part->variants.back ();
14791 current.variant_parts.emplace_back ();
14792 new_part = &current.variant_parts.back ();
14795 /* When we recurse, we want callees to add to this new variant
14796 part. */
14797 scoped_restore save_current_variant_part
14798 = make_scoped_restore (&fi->current_variant_part, new_part);
14800 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14801 if (discr == NULL)
14803 /* It's a univariant form, an extension we support. */
14805 else if (discr->form_is_ref ())
14807 struct dwarf2_cu *target_cu = cu;
14808 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14810 new_part->discriminant_offset = target_die->sect_off;
14812 else
14814 complaint (_("DW_AT_discr does not have DIE reference form"
14815 " - DIE at %s [in module %s]"),
14816 sect_offset_str (die->sect_off),
14817 objfile_name (cu->per_objfile->objfile));
14820 for (die_info *child_die = die->child;
14821 child_die != NULL;
14822 child_die = child_die->sibling)
14823 handle_struct_member_die (child_die, type, fi, template_args, cu);
14826 /* A helper for handle_struct_member_die that handles
14827 DW_TAG_variant. */
14829 static void
14830 handle_variant (struct die_info *die, struct type *type,
14831 struct field_info *fi,
14832 std::vector<struct symbol *> *template_args,
14833 struct dwarf2_cu *cu)
14835 if (fi->current_variant_part == nullptr)
14837 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14838 "- DIE at %s [in module %s]"),
14839 sect_offset_str (die->sect_off),
14840 objfile_name (cu->per_objfile->objfile));
14841 return;
14843 if (fi->current_variant_part->processing_variant)
14845 complaint (_("nested DW_TAG_variant seen "
14846 "- DIE at %s [in module %s]"),
14847 sect_offset_str (die->sect_off),
14848 objfile_name (cu->per_objfile->objfile));
14849 return;
14852 scoped_restore save_processing_variant
14853 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14854 true);
14856 fi->current_variant_part->variants.emplace_back ();
14857 variant_field &variant = fi->current_variant_part->variants.back ();
14858 variant.first_field = fi->fields.size ();
14860 /* In a variant we want to get the discriminant and also add a
14861 field for our sole member child. */
14862 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14863 if (discr == nullptr || !discr->form_is_constant ())
14865 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14866 if (discr == nullptr || discr->as_block ()->size == 0)
14867 variant.default_branch = true;
14868 else
14869 variant.discr_list_data = discr->as_block ();
14871 else
14872 variant.discriminant_value = discr->constant_value (0);
14874 for (die_info *variant_child = die->child;
14875 variant_child != NULL;
14876 variant_child = variant_child->sibling)
14877 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14879 variant.last_field = fi->fields.size ();
14882 /* A helper for process_structure_scope that handles a single member
14883 DIE. */
14885 static void
14886 handle_struct_member_die (struct die_info *child_die, struct type *type,
14887 struct field_info *fi,
14888 std::vector<struct symbol *> *template_args,
14889 struct dwarf2_cu *cu)
14891 if (child_die->tag == DW_TAG_member
14892 || child_die->tag == DW_TAG_variable
14893 || child_die->tag == DW_TAG_namelist_item)
14895 /* NOTE: carlton/2002-11-05: A C++ static data member
14896 should be a DW_TAG_member that is a declaration, but
14897 all versions of G++ as of this writing (so through at
14898 least 3.2.1) incorrectly generate DW_TAG_variable
14899 tags for them instead. */
14900 dwarf2_add_field (fi, child_die, cu);
14902 else if (child_die->tag == DW_TAG_subprogram)
14904 /* Rust doesn't have member functions in the C++ sense.
14905 However, it does emit ordinary functions as children
14906 of a struct DIE. */
14907 if (cu->lang () == language_rust)
14908 read_func_scope (child_die, cu);
14909 else
14911 /* C++ member function. */
14912 dwarf2_add_member_fn (fi, child_die, type, cu);
14915 else if (child_die->tag == DW_TAG_inheritance)
14917 /* C++ base class field. */
14918 dwarf2_add_field (fi, child_die, cu);
14920 else if (type_can_define_types (child_die))
14921 dwarf2_add_type_defn (fi, child_die, cu);
14922 else if (child_die->tag == DW_TAG_template_type_param
14923 || child_die->tag == DW_TAG_template_value_param)
14925 struct symbol *arg = new_symbol (child_die, NULL, cu);
14927 if (arg != NULL)
14928 template_args->push_back (arg);
14930 else if (child_die->tag == DW_TAG_variant_part)
14931 handle_variant_part (child_die, type, fi, template_args, cu);
14932 else if (child_die->tag == DW_TAG_variant)
14933 handle_variant (child_die, type, fi, template_args, cu);
14936 /* Finish creating a structure or union type, including filling in its
14937 members and creating a symbol for it. This function also handles Fortran
14938 namelist variables, their items or members and creating a symbol for
14939 them. */
14941 static void
14942 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14944 struct objfile *objfile = cu->per_objfile->objfile;
14945 struct die_info *child_die;
14946 struct type *type;
14948 type = get_die_type (die, cu);
14949 if (type == NULL)
14950 type = read_structure_type (die, cu);
14952 bool has_template_parameters = false;
14953 if (die->child != NULL && ! die_is_declaration (die, cu))
14955 struct field_info fi;
14956 std::vector<struct symbol *> template_args;
14958 child_die = die->child;
14960 while (child_die && child_die->tag)
14962 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14963 child_die = child_die->sibling;
14966 /* Attach template arguments to type. */
14967 if (!template_args.empty ())
14969 has_template_parameters = true;
14970 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14971 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14972 TYPE_TEMPLATE_ARGUMENTS (type)
14973 = XOBNEWVEC (&objfile->objfile_obstack,
14974 struct symbol *,
14975 TYPE_N_TEMPLATE_ARGUMENTS (type));
14976 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14977 template_args.data (),
14978 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14979 * sizeof (struct symbol *)));
14982 /* Attach fields and member functions to the type. */
14983 if (fi.nfields () > 0)
14984 dwarf2_attach_fields_to_type (&fi, type, cu);
14985 if (!fi.fnfieldlists.empty ())
14987 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14989 /* Get the type which refers to the base class (possibly this
14990 class itself) which contains the vtable pointer for the current
14991 class from the DW_AT_containing_type attribute. This use of
14992 DW_AT_containing_type is a GNU extension. */
14994 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14996 struct type *t = die_containing_type (die, cu);
14998 set_type_vptr_basetype (type, t);
14999 if (type == t)
15001 int i;
15003 /* Our own class provides vtbl ptr. */
15004 for (i = t->num_fields () - 1;
15005 i >= TYPE_N_BASECLASSES (t);
15006 --i)
15008 const char *fieldname = t->field (i).name ();
15010 if (is_vtable_name (fieldname, cu))
15012 set_type_vptr_fieldno (type, i);
15013 break;
15017 /* Complain if virtual function table field not found. */
15018 if (i < TYPE_N_BASECLASSES (t))
15019 complaint (_("virtual function table pointer "
15020 "not found when defining class '%s'"),
15021 type->name () ? type->name () : "");
15023 else
15025 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15028 else if (cu->producer
15029 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15031 /* The IBM XLC compiler does not provide direct indication
15032 of the containing type, but the vtable pointer is
15033 always named __vfp. */
15035 int i;
15037 for (i = type->num_fields () - 1;
15038 i >= TYPE_N_BASECLASSES (type);
15039 --i)
15041 if (strcmp (type->field (i).name (), "__vfp") == 0)
15043 set_type_vptr_fieldno (type, i);
15044 set_type_vptr_basetype (type, type);
15045 break;
15051 /* Copy fi.typedef_field_list linked list elements content into the
15052 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15053 if (!fi.typedef_field_list.empty ())
15055 int count = fi.typedef_field_list.size ();
15057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15058 TYPE_TYPEDEF_FIELD_ARRAY (type)
15059 = ((struct decl_field *)
15060 TYPE_ALLOC (type,
15061 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15062 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15064 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15065 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15068 /* Copy fi.nested_types_list linked list elements content into the
15069 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15070 if (!fi.nested_types_list.empty ()
15071 && cu->lang () != language_ada)
15073 int count = fi.nested_types_list.size ();
15075 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15076 TYPE_NESTED_TYPES_ARRAY (type)
15077 = ((struct decl_field *)
15078 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15079 TYPE_NESTED_TYPES_COUNT (type) = count;
15081 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15082 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15086 quirk_gcc_member_function_pointer (type, objfile);
15087 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
15088 cu->rust_unions.push_back (type);
15089 else if (cu->lang () == language_ada)
15090 quirk_ada_thick_pointer_struct (die, cu, type);
15092 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15093 snapshots) has been known to create a die giving a declaration
15094 for a class that has, as a child, a die giving a definition for a
15095 nested class. So we have to process our children even if the
15096 current die is a declaration. Normally, of course, a declaration
15097 won't have any children at all. */
15099 child_die = die->child;
15101 while (child_die != NULL && child_die->tag)
15103 if (child_die->tag == DW_TAG_member
15104 || child_die->tag == DW_TAG_variable
15105 || child_die->tag == DW_TAG_inheritance
15106 || child_die->tag == DW_TAG_template_value_param
15107 || child_die->tag == DW_TAG_template_type_param)
15109 /* Do nothing. */
15111 else
15112 process_die (child_die, cu);
15114 child_die = child_die->sibling;
15117 /* Do not consider external references. According to the DWARF standard,
15118 these DIEs are identified by the fact that they have no byte_size
15119 attribute, and a declaration attribute. */
15120 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15121 || !die_is_declaration (die, cu)
15122 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15124 struct symbol *sym = new_symbol (die, type, cu);
15126 if (has_template_parameters)
15128 struct symtab *symtab;
15129 if (sym != nullptr)
15130 symtab = sym->symtab ();
15131 else if (cu->line_header != nullptr)
15133 /* Any related symtab will do. */
15134 symtab
15135 = cu->line_header->file_names ()[0].symtab;
15137 else
15139 symtab = nullptr;
15140 complaint (_("could not find suitable "
15141 "symtab for template parameter"
15142 " - DIE at %s [in module %s]"),
15143 sect_offset_str (die->sect_off),
15144 objfile_name (objfile));
15147 if (symtab != nullptr)
15149 /* Make sure that the symtab is set on the new symbols.
15150 Even though they don't appear in this symtab directly,
15151 other parts of gdb assume that symbols do, and this is
15152 reasonably true. */
15153 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15154 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15160 /* Assuming DIE is an enumeration type, and TYPE is its associated
15161 type, update TYPE using some information only available in DIE's
15162 children. In particular, the fields are computed. */
15164 static void
15165 update_enumeration_type_from_children (struct die_info *die,
15166 struct type *type,
15167 struct dwarf2_cu *cu)
15169 struct die_info *child_die;
15170 int unsigned_enum = 1;
15171 int flag_enum = 1;
15173 auto_obstack obstack;
15174 std::vector<struct field> fields;
15176 for (child_die = die->child;
15177 child_die != NULL && child_die->tag;
15178 child_die = child_die->sibling)
15180 struct attribute *attr;
15181 LONGEST value;
15182 const gdb_byte *bytes;
15183 struct dwarf2_locexpr_baton *baton;
15184 const char *name;
15186 if (child_die->tag != DW_TAG_enumerator)
15187 continue;
15189 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15190 if (attr == NULL)
15191 continue;
15193 name = dwarf2_name (child_die, cu);
15194 if (name == NULL)
15195 name = "<anonymous enumerator>";
15197 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15198 &value, &bytes, &baton);
15199 if (value < 0)
15201 unsigned_enum = 0;
15202 flag_enum = 0;
15204 else
15206 if (count_one_bits_ll (value) >= 2)
15207 flag_enum = 0;
15210 fields.emplace_back ();
15211 struct field &field = fields.back ();
15212 field.set_name (dwarf2_physname (name, child_die, cu));
15213 field.set_loc_enumval (value);
15216 if (!fields.empty ())
15218 type->set_num_fields (fields.size ());
15219 type->set_fields
15220 ((struct field *)
15221 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15222 memcpy (type->fields (), fields.data (),
15223 sizeof (struct field) * fields.size ());
15226 if (unsigned_enum)
15227 type->set_is_unsigned (true);
15229 if (flag_enum)
15230 type->set_is_flag_enum (true);
15233 /* Given a DW_AT_enumeration_type die, set its type. We do not
15234 complete the type's fields yet, or create any symbols. */
15236 static struct type *
15237 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15239 struct objfile *objfile = cu->per_objfile->objfile;
15240 struct type *type;
15241 struct attribute *attr;
15242 const char *name;
15244 /* If the definition of this type lives in .debug_types, read that type.
15245 Don't follow DW_AT_specification though, that will take us back up
15246 the chain and we want to go down. */
15247 attr = die->attr (DW_AT_signature);
15248 if (attr != nullptr)
15250 type = get_DW_AT_signature_type (die, attr, cu);
15252 /* The type's CU may not be the same as CU.
15253 Ensure TYPE is recorded with CU in die_type_hash. */
15254 return set_die_type (die, type, cu);
15257 type = alloc_type (objfile);
15259 type->set_code (TYPE_CODE_ENUM);
15260 name = dwarf2_full_name (NULL, die, cu);
15261 if (name != NULL)
15262 type->set_name (name);
15264 attr = dwarf2_attr (die, DW_AT_type, cu);
15265 if (attr != NULL)
15267 struct type *underlying_type = die_type (die, cu);
15269 type->set_target_type (underlying_type);
15272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15273 if (attr != nullptr)
15274 type->set_length (attr->constant_value (0));
15275 else
15276 type->set_length (0);
15278 maybe_set_alignment (cu, die, type);
15280 /* The enumeration DIE can be incomplete. In Ada, any type can be
15281 declared as private in the package spec, and then defined only
15282 inside the package body. Such types are known as Taft Amendment
15283 Types. When another package uses such a type, an incomplete DIE
15284 may be generated by the compiler. */
15285 if (die_is_declaration (die, cu))
15286 type->set_is_stub (true);
15288 /* If this type has an underlying type that is not a stub, then we
15289 may use its attributes. We always use the "unsigned" attribute
15290 in this situation, because ordinarily we guess whether the type
15291 is unsigned -- but the guess can be wrong and the underlying type
15292 can tell us the reality. However, we defer to a local size
15293 attribute if one exists, because this lets the compiler override
15294 the underlying type if needed. */
15295 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
15297 struct type *underlying_type = type->target_type ();
15298 underlying_type = check_typedef (underlying_type);
15300 type->set_is_unsigned (underlying_type->is_unsigned ());
15302 if (type->length () == 0)
15303 type->set_length (underlying_type->length ());
15305 if (TYPE_RAW_ALIGN (type) == 0
15306 && TYPE_RAW_ALIGN (underlying_type) != 0)
15307 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15310 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15312 set_die_type (die, type, cu);
15314 /* Finish the creation of this type by using the enum's children.
15315 Note that, as usual, this must come after set_die_type to avoid
15316 infinite recursion when trying to compute the names of the
15317 enumerators. */
15318 update_enumeration_type_from_children (die, type, cu);
15320 return type;
15323 /* Given a pointer to a die which begins an enumeration, process all
15324 the dies that define the members of the enumeration, and create the
15325 symbol for the enumeration type.
15327 NOTE: We reverse the order of the element list. */
15329 static void
15330 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15332 struct type *this_type;
15334 this_type = get_die_type (die, cu);
15335 if (this_type == NULL)
15336 this_type = read_enumeration_type (die, cu);
15338 if (die->child != NULL)
15340 struct die_info *child_die;
15341 const char *name;
15343 child_die = die->child;
15344 while (child_die && child_die->tag)
15346 if (child_die->tag != DW_TAG_enumerator)
15348 process_die (child_die, cu);
15350 else
15352 name = dwarf2_name (child_die, cu);
15353 if (name)
15354 new_symbol (child_die, this_type, cu);
15357 child_die = child_die->sibling;
15361 /* If we are reading an enum from a .debug_types unit, and the enum
15362 is a declaration, and the enum is not the signatured type in the
15363 unit, then we do not want to add a symbol for it. Adding a
15364 symbol would in some cases obscure the true definition of the
15365 enum, giving users an incomplete type when the definition is
15366 actually available. Note that we do not want to do this for all
15367 enums which are just declarations, because C++0x allows forward
15368 enum declarations. */
15369 if (cu->per_cu->is_debug_types
15370 && die_is_declaration (die, cu))
15372 struct signatured_type *sig_type;
15374 sig_type = (struct signatured_type *) cu->per_cu;
15375 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15376 if (sig_type->type_offset_in_section != die->sect_off)
15377 return;
15380 new_symbol (die, this_type, cu);
15383 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15384 expression for an index type and finds the corresponding field
15385 offset in the hidden "P_BOUNDS" structure. Returns true on success
15386 and updates *FIELD, false if it fails to recognize an
15387 expression. */
15389 static bool
15390 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15391 int *bounds_offset, struct field *field,
15392 struct dwarf2_cu *cu)
15394 struct attribute *attr = dwarf2_attr (die, name, cu);
15395 if (attr == nullptr || !attr->form_is_block ())
15396 return false;
15398 const struct dwarf_block *block = attr->as_block ();
15399 const gdb_byte *start = block->data;
15400 const gdb_byte *end = block->data + block->size;
15402 /* The expression to recognize generally looks like:
15404 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15405 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15407 However, the second "plus_uconst" may be missing:
15409 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15410 DW_OP_deref_size: 4)
15412 This happens when the field is at the start of the structure.
15414 Also, the final deref may not be sized:
15416 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15417 DW_OP_deref)
15419 This happens when the size of the index type happens to be the
15420 same as the architecture's word size. This can occur with or
15421 without the second plus_uconst. */
15423 if (end - start < 2)
15424 return false;
15425 if (*start++ != DW_OP_push_object_address)
15426 return false;
15427 if (*start++ != DW_OP_plus_uconst)
15428 return false;
15430 uint64_t this_bound_off;
15431 start = gdb_read_uleb128 (start, end, &this_bound_off);
15432 if (start == nullptr || (int) this_bound_off != this_bound_off)
15433 return false;
15434 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15435 is consistent among all bounds. */
15436 if (*bounds_offset == -1)
15437 *bounds_offset = this_bound_off;
15438 else if (*bounds_offset != this_bound_off)
15439 return false;
15441 if (start == end || *start++ != DW_OP_deref)
15442 return false;
15444 int offset = 0;
15445 if (start ==end)
15446 return false;
15447 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15449 /* This means an offset of 0. */
15451 else if (*start++ != DW_OP_plus_uconst)
15452 return false;
15453 else
15455 /* The size is the parameter to DW_OP_plus_uconst. */
15456 uint64_t val;
15457 start = gdb_read_uleb128 (start, end, &val);
15458 if (start == nullptr)
15459 return false;
15460 if ((int) val != val)
15461 return false;
15462 offset = val;
15465 if (start == end)
15466 return false;
15468 uint64_t size;
15469 if (*start == DW_OP_deref_size)
15471 start = gdb_read_uleb128 (start + 1, end, &size);
15472 if (start == nullptr)
15473 return false;
15475 else if (*start == DW_OP_deref)
15477 size = cu->header.addr_size;
15478 ++start;
15480 else
15481 return false;
15483 field->set_loc_bitpos (8 * offset);
15484 if (size != field->type ()->length ())
15485 FIELD_BITSIZE (*field) = 8 * size;
15487 return true;
15490 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15491 some kinds of Ada arrays:
15493 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15494 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15495 <11e0> DW_AT_data_location: 2 byte block: 97 6
15496 (DW_OP_push_object_address; DW_OP_deref)
15497 <11e3> DW_AT_type : <0x1173>
15498 <11e7> DW_AT_sibling : <0x1201>
15499 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15500 <11ec> DW_AT_type : <0x1206>
15501 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15502 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15503 DW_OP_deref_size: 4)
15504 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15505 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15506 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15508 This actually represents a "thick pointer", which is a structure
15509 with two elements: one that is a pointer to the array data, and one
15510 that is a pointer to another structure; this second structure holds
15511 the array bounds.
15513 This returns a new type on success, or nullptr if this didn't
15514 recognize the type. */
15516 static struct type *
15517 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15518 struct type *type)
15520 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15521 /* So far we've only seen this with block form. */
15522 if (attr == nullptr || !attr->form_is_block ())
15523 return nullptr;
15525 /* Note that this will fail if the structure layout is changed by
15526 the compiler. However, we have no good way to recognize some
15527 other layout, because we don't know what expression the compiler
15528 might choose to emit should this happen. */
15529 struct dwarf_block *blk = attr->as_block ();
15530 if (blk->size != 2
15531 || blk->data[0] != DW_OP_push_object_address
15532 || blk->data[1] != DW_OP_deref)
15533 return nullptr;
15535 int bounds_offset = -1;
15536 int max_align = -1;
15537 std::vector<struct field> range_fields;
15538 for (struct die_info *child_die = die->child;
15539 child_die;
15540 child_die = child_die->sibling)
15542 if (child_die->tag == DW_TAG_subrange_type)
15544 struct type *underlying = read_subrange_index_type (child_die, cu);
15546 int this_align = type_align (underlying);
15547 if (this_align > max_align)
15548 max_align = this_align;
15550 range_fields.emplace_back ();
15551 range_fields.emplace_back ();
15553 struct field &lower = range_fields[range_fields.size () - 2];
15554 struct field &upper = range_fields[range_fields.size () - 1];
15556 lower.set_type (underlying);
15557 FIELD_ARTIFICIAL (lower) = 1;
15559 upper.set_type (underlying);
15560 FIELD_ARTIFICIAL (upper) = 1;
15562 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15563 &bounds_offset, &lower, cu)
15564 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15565 &bounds_offset, &upper, cu))
15566 return nullptr;
15570 /* This shouldn't really happen, but double-check that we found
15571 where the bounds are stored. */
15572 if (bounds_offset == -1)
15573 return nullptr;
15575 struct objfile *objfile = cu->per_objfile->objfile;
15576 for (int i = 0; i < range_fields.size (); i += 2)
15578 char name[20];
15580 /* Set the name of each field in the bounds. */
15581 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15582 range_fields[i].set_name (objfile->intern (name));
15583 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15584 range_fields[i + 1].set_name (objfile->intern (name));
15587 struct type *bounds = alloc_type (objfile);
15588 bounds->set_code (TYPE_CODE_STRUCT);
15590 bounds->set_num_fields (range_fields.size ());
15591 bounds->set_fields
15592 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15593 * sizeof (struct field))));
15594 memcpy (bounds->fields (), range_fields.data (),
15595 bounds->num_fields () * sizeof (struct field));
15597 int last_fieldno = range_fields.size () - 1;
15598 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15599 + bounds->field (last_fieldno).type ()->length ());
15600 bounds->set_length (align_up (bounds_size, max_align));
15602 /* Rewrite the existing array type in place. Specifically, we
15603 remove any dynamic properties we might have read, and we replace
15604 the index types. */
15605 struct type *iter = type;
15606 for (int i = 0; i < range_fields.size (); i += 2)
15608 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15609 iter->main_type->dyn_prop_list = nullptr;
15610 iter->set_index_type
15611 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15612 iter = iter->target_type ();
15615 struct type *result = alloc_type (objfile);
15616 result->set_code (TYPE_CODE_STRUCT);
15618 result->set_num_fields (2);
15619 result->set_fields
15620 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15621 * sizeof (struct field))));
15623 /* The names are chosen to coincide with what the compiler does with
15624 -fgnat-encodings=all, which the Ada code in gdb already
15625 understands. */
15626 result->field (0).set_name ("P_ARRAY");
15627 result->field (0).set_type (lookup_pointer_type (type));
15629 result->field (1).set_name ("P_BOUNDS");
15630 result->field (1).set_type (lookup_pointer_type (bounds));
15631 result->field (1).set_loc_bitpos (8 * bounds_offset);
15633 result->set_name (type->name ());
15634 result->set_length (result->field (0).type ()->length ()
15635 + result->field (1).type ()->length ());
15637 return result;
15640 /* Extract all information from a DW_TAG_array_type DIE and put it in
15641 the DIE's type field. For now, this only handles one dimensional
15642 arrays. */
15644 static struct type *
15645 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15647 struct objfile *objfile = cu->per_objfile->objfile;
15648 struct die_info *child_die;
15649 struct type *type;
15650 struct type *element_type, *range_type, *index_type;
15651 struct attribute *attr;
15652 const char *name;
15653 struct dynamic_prop *byte_stride_prop = NULL;
15654 unsigned int bit_stride = 0;
15656 element_type = die_type (die, cu);
15658 /* The die_type call above may have already set the type for this DIE. */
15659 type = get_die_type (die, cu);
15660 if (type)
15661 return type;
15663 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15664 if (attr != NULL)
15666 int stride_ok;
15667 struct type *prop_type = cu->addr_sized_int_type (false);
15669 byte_stride_prop
15670 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15671 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15672 prop_type);
15673 if (!stride_ok)
15675 complaint (_("unable to read array DW_AT_byte_stride "
15676 " - DIE at %s [in module %s]"),
15677 sect_offset_str (die->sect_off),
15678 objfile_name (cu->per_objfile->objfile));
15679 /* Ignore this attribute. We will likely not be able to print
15680 arrays of this type correctly, but there is little we can do
15681 to help if we cannot read the attribute's value. */
15682 byte_stride_prop = NULL;
15686 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15687 if (attr != NULL)
15688 bit_stride = attr->constant_value (0);
15690 /* Irix 6.2 native cc creates array types without children for
15691 arrays with unspecified length. */
15692 if (die->child == NULL)
15694 index_type = objfile_type (objfile)->builtin_int;
15695 range_type = create_static_range_type (NULL, index_type, 0, -1);
15696 type = create_array_type_with_stride (NULL, element_type, range_type,
15697 byte_stride_prop, bit_stride);
15698 return set_die_type (die, type, cu);
15701 std::vector<struct type *> range_types;
15702 child_die = die->child;
15703 while (child_die && child_die->tag)
15705 if (child_die->tag == DW_TAG_subrange_type
15706 || child_die->tag == DW_TAG_generic_subrange)
15708 struct type *child_type = read_type_die (child_die, cu);
15710 if (child_type != NULL)
15712 /* The range type was succesfully read. Save it for the
15713 array type creation. */
15714 range_types.push_back (child_type);
15717 child_die = child_die->sibling;
15720 if (range_types.empty ())
15722 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15723 sect_offset_str (die->sect_off),
15724 objfile_name (cu->per_objfile->objfile));
15725 return NULL;
15728 /* Dwarf2 dimensions are output from left to right, create the
15729 necessary array types in backwards order. */
15731 type = element_type;
15733 if (read_array_order (die, cu) == DW_ORD_col_major)
15735 int i = 0;
15737 while (i < range_types.size ())
15739 type = create_array_type_with_stride (NULL, type, range_types[i++],
15740 byte_stride_prop, bit_stride);
15741 type->set_is_multi_dimensional (true);
15742 bit_stride = 0;
15743 byte_stride_prop = nullptr;
15746 else
15748 size_t ndim = range_types.size ();
15749 while (ndim-- > 0)
15751 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15752 byte_stride_prop, bit_stride);
15753 type->set_is_multi_dimensional (true);
15754 bit_stride = 0;
15755 byte_stride_prop = nullptr;
15759 /* Clear the flag on the outermost array type. */
15760 type->set_is_multi_dimensional (false);
15761 gdb_assert (type != element_type);
15763 /* Understand Dwarf2 support for vector types (like they occur on
15764 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15765 array type. This is not part of the Dwarf2/3 standard yet, but a
15766 custom vendor extension. The main difference between a regular
15767 array and the vector variant is that vectors are passed by value
15768 to functions. */
15769 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15770 if (attr != nullptr)
15771 make_vector_type (type);
15773 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15774 implementation may choose to implement triple vectors using this
15775 attribute. */
15776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15777 if (attr != nullptr && attr->form_is_unsigned ())
15779 if (attr->as_unsigned () >= type->length ())
15780 type->set_length (attr->as_unsigned ());
15781 else
15782 complaint (_("DW_AT_byte_size for array type smaller "
15783 "than the total size of elements"));
15786 name = dwarf2_name (die, cu);
15787 if (name)
15788 type->set_name (name);
15790 maybe_set_alignment (cu, die, type);
15792 struct type *replacement_type = nullptr;
15793 if (cu->lang () == language_ada)
15795 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15796 if (replacement_type != nullptr)
15797 type = replacement_type;
15800 /* Install the type in the die. */
15801 set_die_type (die, type, cu, replacement_type != nullptr);
15803 /* set_die_type should be already done. */
15804 set_descriptive_type (type, die, cu);
15806 return type;
15809 static enum dwarf_array_dim_ordering
15810 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15812 struct attribute *attr;
15814 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15816 if (attr != nullptr)
15818 LONGEST val = attr->constant_value (-1);
15819 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15820 return (enum dwarf_array_dim_ordering) val;
15823 /* GNU F77 is a special case, as at 08/2004 array type info is the
15824 opposite order to the dwarf2 specification, but data is still
15825 laid out as per normal fortran.
15827 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15828 version checking. */
15830 if (cu->lang () == language_fortran
15831 && cu->producer && strstr (cu->producer, "GNU F77"))
15833 return DW_ORD_row_major;
15836 switch (cu->language_defn->array_ordering ())
15838 case array_column_major:
15839 return DW_ORD_col_major;
15840 case array_row_major:
15841 default:
15842 return DW_ORD_row_major;
15846 /* Extract all information from a DW_TAG_set_type DIE and put it in
15847 the DIE's type field. */
15849 static struct type *
15850 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15852 struct type *domain_type, *set_type;
15853 struct attribute *attr;
15855 domain_type = die_type (die, cu);
15857 /* The die_type call above may have already set the type for this DIE. */
15858 set_type = get_die_type (die, cu);
15859 if (set_type)
15860 return set_type;
15862 set_type = create_set_type (NULL, domain_type);
15864 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15865 if (attr != nullptr && attr->form_is_unsigned ())
15866 set_type->set_length (attr->as_unsigned ());
15868 maybe_set_alignment (cu, die, set_type);
15870 return set_die_type (die, set_type, cu);
15873 /* A helper for read_common_block that creates a locexpr baton.
15874 SYM is the symbol which we are marking as computed.
15875 COMMON_DIE is the DIE for the common block.
15876 COMMON_LOC is the location expression attribute for the common
15877 block itself.
15878 MEMBER_LOC is the location expression attribute for the particular
15879 member of the common block that we are processing.
15880 CU is the CU from which the above come. */
15882 static void
15883 mark_common_block_symbol_computed (struct symbol *sym,
15884 struct die_info *common_die,
15885 struct attribute *common_loc,
15886 struct attribute *member_loc,
15887 struct dwarf2_cu *cu)
15889 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15890 struct objfile *objfile = per_objfile->objfile;
15891 struct dwarf2_locexpr_baton *baton;
15892 gdb_byte *ptr;
15893 unsigned int cu_off;
15894 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15895 LONGEST offset = 0;
15897 gdb_assert (common_loc && member_loc);
15898 gdb_assert (common_loc->form_is_block ());
15899 gdb_assert (member_loc->form_is_block ()
15900 || member_loc->form_is_constant ());
15902 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15903 baton->per_objfile = per_objfile;
15904 baton->per_cu = cu->per_cu;
15905 gdb_assert (baton->per_cu);
15907 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15909 if (member_loc->form_is_constant ())
15911 offset = member_loc->constant_value (0);
15912 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15914 else
15915 baton->size += member_loc->as_block ()->size;
15917 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15918 baton->data = ptr;
15920 *ptr++ = DW_OP_call4;
15921 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15922 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15923 ptr += 4;
15925 if (member_loc->form_is_constant ())
15927 *ptr++ = DW_OP_addr;
15928 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15929 ptr += cu->header.addr_size;
15931 else
15933 /* We have to copy the data here, because DW_OP_call4 will only
15934 use a DW_AT_location attribute. */
15935 struct dwarf_block *block = member_loc->as_block ();
15936 memcpy (ptr, block->data, block->size);
15937 ptr += block->size;
15940 *ptr++ = DW_OP_plus;
15941 gdb_assert (ptr - baton->data == baton->size);
15943 SYMBOL_LOCATION_BATON (sym) = baton;
15944 sym->set_aclass_index (dwarf2_locexpr_index);
15947 /* Create appropriate locally-scoped variables for all the
15948 DW_TAG_common_block entries. Also create a struct common_block
15949 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15950 is used to separate the common blocks name namespace from regular
15951 variable names. */
15953 static void
15954 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15956 struct attribute *attr;
15958 attr = dwarf2_attr (die, DW_AT_location, cu);
15959 if (attr != nullptr)
15961 /* Support the .debug_loc offsets. */
15962 if (attr->form_is_block ())
15964 /* Ok. */
15966 else if (attr->form_is_section_offset ())
15968 dwarf2_complex_location_expr_complaint ();
15969 attr = NULL;
15971 else
15973 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15974 "common block member");
15975 attr = NULL;
15979 if (die->child != NULL)
15981 struct objfile *objfile = cu->per_objfile->objfile;
15982 struct die_info *child_die;
15983 size_t n_entries = 0, size;
15984 struct common_block *common_block;
15985 struct symbol *sym;
15987 for (child_die = die->child;
15988 child_die && child_die->tag;
15989 child_die = child_die->sibling)
15990 ++n_entries;
15992 size = (sizeof (struct common_block)
15993 + (n_entries - 1) * sizeof (struct symbol *));
15994 common_block
15995 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15996 size);
15997 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15998 common_block->n_entries = 0;
16000 for (child_die = die->child;
16001 child_die && child_die->tag;
16002 child_die = child_die->sibling)
16004 /* Create the symbol in the DW_TAG_common_block block in the current
16005 symbol scope. */
16006 sym = new_symbol (child_die, NULL, cu);
16007 if (sym != NULL)
16009 struct attribute *member_loc;
16011 common_block->contents[common_block->n_entries++] = sym;
16013 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16014 cu);
16015 if (member_loc)
16017 /* GDB has handled this for a long time, but it is
16018 not specified by DWARF. It seems to have been
16019 emitted by gfortran at least as recently as:
16020 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16021 complaint (_("Variable in common block has "
16022 "DW_AT_data_member_location "
16023 "- DIE at %s [in module %s]"),
16024 sect_offset_str (child_die->sect_off),
16025 objfile_name (objfile));
16027 if (member_loc->form_is_section_offset ())
16028 dwarf2_complex_location_expr_complaint ();
16029 else if (member_loc->form_is_constant ()
16030 || member_loc->form_is_block ())
16032 if (attr != nullptr)
16033 mark_common_block_symbol_computed (sym, die, attr,
16034 member_loc, cu);
16036 else
16037 dwarf2_complex_location_expr_complaint ();
16042 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16043 sym->set_value_common_block (common_block);
16047 /* Create a type for a C++ namespace. */
16049 static struct type *
16050 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16052 struct objfile *objfile = cu->per_objfile->objfile;
16053 const char *previous_prefix, *name;
16054 int is_anonymous;
16055 struct type *type;
16057 /* For extensions, reuse the type of the original namespace. */
16058 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16060 struct die_info *ext_die;
16061 struct dwarf2_cu *ext_cu = cu;
16063 ext_die = dwarf2_extension (die, &ext_cu);
16064 type = read_type_die (ext_die, ext_cu);
16066 /* EXT_CU may not be the same as CU.
16067 Ensure TYPE is recorded with CU in die_type_hash. */
16068 return set_die_type (die, type, cu);
16071 name = namespace_name (die, &is_anonymous, cu);
16073 /* Now build the name of the current namespace. */
16075 previous_prefix = determine_prefix (die, cu);
16076 if (previous_prefix[0] != '\0')
16077 name = typename_concat (&objfile->objfile_obstack,
16078 previous_prefix, name, 0, cu);
16080 /* Create the type. */
16081 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16083 return set_die_type (die, type, cu);
16086 /* Read a namespace scope. */
16088 static void
16089 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16091 struct objfile *objfile = cu->per_objfile->objfile;
16092 int is_anonymous;
16094 /* Add a symbol associated to this if we haven't seen the namespace
16095 before. Also, add a using directive if it's an anonymous
16096 namespace. */
16098 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16100 struct type *type;
16102 type = read_type_die (die, cu);
16103 new_symbol (die, type, cu);
16105 namespace_name (die, &is_anonymous, cu);
16106 if (is_anonymous)
16108 const char *previous_prefix = determine_prefix (die, cu);
16110 std::vector<const char *> excludes;
16111 add_using_directive (using_directives (cu),
16112 previous_prefix, type->name (), NULL,
16113 NULL, excludes,
16114 read_decl_line (die, cu),
16115 0, &objfile->objfile_obstack);
16119 if (die->child != NULL)
16121 struct die_info *child_die = die->child;
16123 while (child_die && child_die->tag)
16125 process_die (child_die, cu);
16126 child_die = child_die->sibling;
16131 /* Read a Fortran module as type. This DIE can be only a declaration used for
16132 imported module. Still we need that type as local Fortran "use ... only"
16133 declaration imports depend on the created type in determine_prefix. */
16135 static struct type *
16136 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16138 struct objfile *objfile = cu->per_objfile->objfile;
16139 const char *module_name;
16140 struct type *type;
16142 module_name = dwarf2_name (die, cu);
16143 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16145 return set_die_type (die, type, cu);
16148 /* Read a Fortran module. */
16150 static void
16151 read_module (struct die_info *die, struct dwarf2_cu *cu)
16153 struct die_info *child_die = die->child;
16154 struct type *type;
16156 type = read_type_die (die, cu);
16157 new_symbol (die, type, cu);
16159 while (child_die && child_die->tag)
16161 process_die (child_die, cu);
16162 child_die = child_die->sibling;
16166 /* Return the name of the namespace represented by DIE. Set
16167 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16168 namespace. */
16170 static const char *
16171 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16173 struct die_info *current_die;
16174 const char *name = NULL;
16176 /* Loop through the extensions until we find a name. */
16178 for (current_die = die;
16179 current_die != NULL;
16180 current_die = dwarf2_extension (die, &cu))
16182 /* We don't use dwarf2_name here so that we can detect the absence
16183 of a name -> anonymous namespace. */
16184 name = dwarf2_string_attr (die, DW_AT_name, cu);
16186 if (name != NULL)
16187 break;
16190 /* Is it an anonymous namespace? */
16192 *is_anonymous = (name == NULL);
16193 if (*is_anonymous)
16194 name = CP_ANONYMOUS_NAMESPACE_STR;
16196 return name;
16199 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16200 the user defined type vector. */
16202 static struct type *
16203 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16205 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16206 struct comp_unit_head *cu_header = &cu->header;
16207 struct type *type;
16208 struct attribute *attr_byte_size;
16209 struct attribute *attr_address_class;
16210 int byte_size, addr_class;
16211 struct type *target_type;
16213 target_type = die_type (die, cu);
16215 /* The die_type call above may have already set the type for this DIE. */
16216 type = get_die_type (die, cu);
16217 if (type)
16218 return type;
16220 type = lookup_pointer_type (target_type);
16222 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16223 if (attr_byte_size)
16224 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16225 else
16226 byte_size = cu_header->addr_size;
16228 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16229 if (attr_address_class)
16230 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16231 else
16232 addr_class = DW_ADDR_none;
16234 ULONGEST alignment = get_alignment (cu, die);
16236 /* If the pointer size, alignment, or address class is different
16237 than the default, create a type variant marked as such and set
16238 the length accordingly. */
16239 if (type->length () != byte_size
16240 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16241 && alignment != TYPE_RAW_ALIGN (type))
16242 || addr_class != DW_ADDR_none)
16244 if (gdbarch_address_class_type_flags_p (gdbarch))
16246 type_instance_flags type_flags
16247 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16248 addr_class);
16249 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16250 == 0);
16251 type = make_type_with_address_space (type, type_flags);
16253 else if (type->length () != byte_size)
16255 complaint (_("invalid pointer size %d"), byte_size);
16257 else if (TYPE_RAW_ALIGN (type) != alignment)
16259 complaint (_("Invalid DW_AT_alignment"
16260 " - DIE at %s [in module %s]"),
16261 sect_offset_str (die->sect_off),
16262 objfile_name (cu->per_objfile->objfile));
16264 else
16266 /* Should we also complain about unhandled address classes? */
16270 type->set_length (byte_size);
16271 set_type_align (type, alignment);
16272 return set_die_type (die, type, cu);
16275 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16276 the user defined type vector. */
16278 static struct type *
16279 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16281 struct type *type;
16282 struct type *to_type;
16283 struct type *domain;
16285 to_type = die_type (die, cu);
16286 domain = die_containing_type (die, cu);
16288 /* The calls above may have already set the type for this DIE. */
16289 type = get_die_type (die, cu);
16290 if (type)
16291 return type;
16293 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16294 type = lookup_methodptr_type (to_type);
16295 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16297 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16299 smash_to_method_type (new_type, domain, to_type->target_type (),
16300 to_type->fields (), to_type->num_fields (),
16301 to_type->has_varargs ());
16302 type = lookup_methodptr_type (new_type);
16304 else
16305 type = lookup_memberptr_type (to_type, domain);
16307 return set_die_type (die, type, cu);
16310 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16311 the user defined type vector. */
16313 static struct type *
16314 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16315 enum type_code refcode)
16317 struct comp_unit_head *cu_header = &cu->header;
16318 struct type *type, *target_type;
16319 struct attribute *attr;
16321 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16323 target_type = die_type (die, cu);
16325 /* The die_type call above may have already set the type for this DIE. */
16326 type = get_die_type (die, cu);
16327 if (type)
16328 return type;
16330 type = lookup_reference_type (target_type, refcode);
16331 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16332 if (attr != nullptr)
16333 type->set_length (attr->constant_value (cu_header->addr_size));
16334 else
16335 type->set_length (cu_header->addr_size);
16337 maybe_set_alignment (cu, die, type);
16338 return set_die_type (die, type, cu);
16341 /* Add the given cv-qualifiers to the element type of the array. GCC
16342 outputs DWARF type qualifiers that apply to an array, not the
16343 element type. But GDB relies on the array element type to carry
16344 the cv-qualifiers. This mimics section 6.7.3 of the C99
16345 specification. */
16347 static struct type *
16348 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16349 struct type *base_type, int cnst, int voltl)
16351 struct type *el_type, *inner_array;
16353 base_type = copy_type (base_type);
16354 inner_array = base_type;
16356 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
16358 inner_array->set_target_type (copy_type (inner_array->target_type ()));
16359 inner_array = inner_array->target_type ();
16362 el_type = inner_array->target_type ();
16363 cnst |= TYPE_CONST (el_type);
16364 voltl |= TYPE_VOLATILE (el_type);
16365 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
16367 return set_die_type (die, base_type, cu);
16370 static struct type *
16371 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16373 struct type *base_type, *cv_type;
16375 base_type = die_type (die, cu);
16377 /* The die_type call above may have already set the type for this DIE. */
16378 cv_type = get_die_type (die, cu);
16379 if (cv_type)
16380 return cv_type;
16382 /* In case the const qualifier is applied to an array type, the element type
16383 is so qualified, not the array type (section 6.7.3 of C99). */
16384 if (base_type->code () == TYPE_CODE_ARRAY)
16385 return add_array_cv_type (die, cu, base_type, 1, 0);
16387 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16388 return set_die_type (die, cv_type, cu);
16391 static struct type *
16392 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16394 struct type *base_type, *cv_type;
16396 base_type = die_type (die, cu);
16398 /* The die_type call above may have already set the type for this DIE. */
16399 cv_type = get_die_type (die, cu);
16400 if (cv_type)
16401 return cv_type;
16403 /* In case the volatile qualifier is applied to an array type, the
16404 element type is so qualified, not the array type (section 6.7.3
16405 of C99). */
16406 if (base_type->code () == TYPE_CODE_ARRAY)
16407 return add_array_cv_type (die, cu, base_type, 0, 1);
16409 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16410 return set_die_type (die, cv_type, cu);
16413 /* Handle DW_TAG_restrict_type. */
16415 static struct type *
16416 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16418 struct type *base_type, *cv_type;
16420 base_type = die_type (die, cu);
16422 /* The die_type call above may have already set the type for this DIE. */
16423 cv_type = get_die_type (die, cu);
16424 if (cv_type)
16425 return cv_type;
16427 cv_type = make_restrict_type (base_type);
16428 return set_die_type (die, cv_type, cu);
16431 /* Handle DW_TAG_atomic_type. */
16433 static struct type *
16434 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16436 struct type *base_type, *cv_type;
16438 base_type = die_type (die, cu);
16440 /* The die_type call above may have already set the type for this DIE. */
16441 cv_type = get_die_type (die, cu);
16442 if (cv_type)
16443 return cv_type;
16445 cv_type = make_atomic_type (base_type);
16446 return set_die_type (die, cv_type, cu);
16449 /* Extract all information from a DW_TAG_string_type DIE and add to
16450 the user defined type vector. It isn't really a user defined type,
16451 but it behaves like one, with other DIE's using an AT_user_def_type
16452 attribute to reference it. */
16454 static struct type *
16455 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16457 struct objfile *objfile = cu->per_objfile->objfile;
16458 struct gdbarch *gdbarch = objfile->arch ();
16459 struct type *type, *range_type, *index_type, *char_type;
16460 struct attribute *attr;
16461 struct dynamic_prop prop;
16462 bool length_is_constant = true;
16463 LONGEST length;
16465 /* There are a couple of places where bit sizes might be made use of
16466 when parsing a DW_TAG_string_type, however, no producer that we know
16467 of make use of these. Handling bit sizes that are a multiple of the
16468 byte size is easy enough, but what about other bit sizes? Lets deal
16469 with that problem when we have to. Warn about these attributes being
16470 unsupported, then parse the type and ignore them like we always
16471 have. */
16472 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16473 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16475 static bool warning_printed = false;
16476 if (!warning_printed)
16478 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16479 "currently supported on DW_TAG_string_type."));
16480 warning_printed = true;
16484 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16485 if (attr != nullptr && !attr->form_is_constant ())
16487 /* The string length describes the location at which the length of
16488 the string can be found. The size of the length field can be
16489 specified with one of the attributes below. */
16490 struct type *prop_type;
16491 struct attribute *len
16492 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16493 if (len == nullptr)
16494 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16495 if (len != nullptr && len->form_is_constant ())
16497 /* Pass 0 as the default as we know this attribute is constant
16498 and the default value will not be returned. */
16499 LONGEST sz = len->constant_value (0);
16500 prop_type = objfile_int_type (objfile, sz, true);
16502 else
16504 /* If the size is not specified then we assume it is the size of
16505 an address on this target. */
16506 prop_type = cu->addr_sized_int_type (true);
16509 /* Convert the attribute into a dynamic property. */
16510 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16511 length = 1;
16512 else
16513 length_is_constant = false;
16515 else if (attr != nullptr)
16517 /* This DW_AT_string_length just contains the length with no
16518 indirection. There's no need to create a dynamic property in this
16519 case. Pass 0 for the default value as we know it will not be
16520 returned in this case. */
16521 length = attr->constant_value (0);
16523 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16525 /* We don't currently support non-constant byte sizes for strings. */
16526 length = attr->constant_value (1);
16528 else
16530 /* Use 1 as a fallback length if we have nothing else. */
16531 length = 1;
16534 index_type = objfile_type (objfile)->builtin_int;
16535 if (length_is_constant)
16536 range_type = create_static_range_type (NULL, index_type, 1, length);
16537 else
16539 struct dynamic_prop low_bound;
16541 low_bound.set_const_val (1);
16542 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16544 char_type = language_string_char_type (cu->language_defn, gdbarch);
16545 type = create_string_type (NULL, char_type, range_type);
16547 return set_die_type (die, type, cu);
16550 /* Assuming that DIE corresponds to a function, returns nonzero
16551 if the function is prototyped. */
16553 static int
16554 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16556 struct attribute *attr;
16558 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16559 if (attr && attr->as_boolean ())
16560 return 1;
16562 /* The DWARF standard implies that the DW_AT_prototyped attribute
16563 is only meaningful for C, but the concept also extends to other
16564 languages that allow unprototyped functions (Eg: Objective C).
16565 For all other languages, assume that functions are always
16566 prototyped. */
16567 if (cu->lang () != language_c
16568 && cu->lang () != language_objc
16569 && cu->lang () != language_opencl)
16570 return 1;
16572 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16573 prototyped and unprototyped functions; default to prototyped,
16574 since that is more common in modern code (and RealView warns
16575 about unprototyped functions). */
16576 if (producer_is_realview (cu->producer))
16577 return 1;
16579 return 0;
16582 /* Handle DIES due to C code like:
16584 struct foo
16586 int (*funcp)(int a, long l);
16587 int b;
16590 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16592 static struct type *
16593 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16595 struct objfile *objfile = cu->per_objfile->objfile;
16596 struct type *type; /* Type that this function returns. */
16597 struct type *ftype; /* Function that returns above type. */
16598 struct attribute *attr;
16600 type = die_type (die, cu);
16602 /* The die_type call above may have already set the type for this DIE. */
16603 ftype = get_die_type (die, cu);
16604 if (ftype)
16605 return ftype;
16607 ftype = lookup_function_type (type);
16609 if (prototyped_function_p (die, cu))
16610 ftype->set_is_prototyped (true);
16612 /* Store the calling convention in the type if it's available in
16613 the subroutine die. Otherwise set the calling convention to
16614 the default value DW_CC_normal. */
16615 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16616 if (attr != nullptr
16617 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16618 TYPE_CALLING_CONVENTION (ftype)
16619 = (enum dwarf_calling_convention) attr->constant_value (0);
16620 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16621 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16622 else
16623 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16625 /* Record whether the function returns normally to its caller or not
16626 if the DWARF producer set that information. */
16627 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16628 if (attr && attr->as_boolean ())
16629 TYPE_NO_RETURN (ftype) = 1;
16631 /* We need to add the subroutine type to the die immediately so
16632 we don't infinitely recurse when dealing with parameters
16633 declared as the same subroutine type. */
16634 set_die_type (die, ftype, cu);
16636 if (die->child != NULL)
16638 struct type *void_type = objfile_type (objfile)->builtin_void;
16639 struct die_info *child_die;
16640 int nparams, iparams;
16642 /* Count the number of parameters.
16643 FIXME: GDB currently ignores vararg functions, but knows about
16644 vararg member functions. */
16645 nparams = 0;
16646 child_die = die->child;
16647 while (child_die && child_die->tag)
16649 if (child_die->tag == DW_TAG_formal_parameter)
16650 nparams++;
16651 else if (child_die->tag == DW_TAG_unspecified_parameters)
16652 ftype->set_has_varargs (true);
16654 child_die = child_die->sibling;
16657 /* Allocate storage for parameters and fill them in. */
16658 ftype->set_num_fields (nparams);
16659 ftype->set_fields
16660 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16662 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16663 even if we error out during the parameters reading below. */
16664 for (iparams = 0; iparams < nparams; iparams++)
16665 ftype->field (iparams).set_type (void_type);
16667 iparams = 0;
16668 child_die = die->child;
16669 while (child_die && child_die->tag)
16671 if (child_die->tag == DW_TAG_formal_parameter)
16673 struct type *arg_type;
16675 /* DWARF version 2 has no clean way to discern C++
16676 static and non-static member functions. G++ helps
16677 GDB by marking the first parameter for non-static
16678 member functions (which is the this pointer) as
16679 artificial. We pass this information to
16680 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16682 DWARF version 3 added DW_AT_object_pointer, which GCC
16683 4.5 does not yet generate. */
16684 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16685 if (attr != nullptr)
16686 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16687 else
16688 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16689 arg_type = die_type (child_die, cu);
16691 /* RealView does not mark THIS as const, which the testsuite
16692 expects. GCC marks THIS as const in method definitions,
16693 but not in the class specifications (GCC PR 43053). */
16694 if (cu->lang () == language_cplus
16695 && !TYPE_CONST (arg_type)
16696 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16698 int is_this = 0;
16699 struct dwarf2_cu *arg_cu = cu;
16700 const char *name = dwarf2_name (child_die, cu);
16702 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16703 if (attr != nullptr)
16705 /* If the compiler emits this, use it. */
16706 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16707 is_this = 1;
16709 else if (name && strcmp (name, "this") == 0)
16710 /* Function definitions will have the argument names. */
16711 is_this = 1;
16712 else if (name == NULL && iparams == 0)
16713 /* Declarations may not have the names, so like
16714 elsewhere in GDB, assume an artificial first
16715 argument is "this". */
16716 is_this = 1;
16718 if (is_this)
16719 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16720 arg_type, 0);
16723 ftype->field (iparams).set_type (arg_type);
16724 iparams++;
16726 child_die = child_die->sibling;
16730 return ftype;
16733 static struct type *
16734 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16736 struct objfile *objfile = cu->per_objfile->objfile;
16737 const char *name = NULL;
16738 struct type *this_type, *target_type;
16740 name = dwarf2_full_name (NULL, die, cu);
16741 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16742 this_type->set_target_is_stub (true);
16743 set_die_type (die, this_type, cu);
16744 target_type = die_type (die, cu);
16745 if (target_type != this_type)
16746 this_type->set_target_type (target_type);
16747 else
16749 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16750 spec and cause infinite loops in GDB. */
16751 complaint (_("Self-referential DW_TAG_typedef "
16752 "- DIE at %s [in module %s]"),
16753 sect_offset_str (die->sect_off), objfile_name (objfile));
16754 this_type->set_target_type (nullptr);
16756 if (name == NULL)
16758 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16759 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16760 Handle these by just returning the target type, rather than
16761 constructing an anonymous typedef type and trying to handle this
16762 elsewhere. */
16763 set_die_type (die, target_type, cu);
16764 return target_type;
16766 return this_type;
16769 /* Helper for get_dwarf2_rational_constant that computes the value of
16770 a given gmp_mpz given an attribute. */
16772 static void
16773 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16775 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16776 location expression that pushes an implicit value. */
16777 if (attr->form == DW_FORM_exprloc)
16779 dwarf_block *blk = attr->as_block ();
16780 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16782 uint64_t len;
16783 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16784 blk->data + blk->size,
16785 &len);
16786 if (ptr - blk->data + len <= blk->size)
16788 mpz_import (value->val, len,
16789 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16790 ? 1 : -1,
16791 1, 0, 0, ptr);
16792 return;
16796 /* On failure set it to 1. */
16797 *value = gdb_mpz (1);
16799 else if (attr->form_is_block ())
16801 dwarf_block *blk = attr->as_block ();
16802 mpz_import (value->val, blk->size,
16803 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16804 ? 1 : -1,
16805 1, 0, 0, blk->data);
16807 else
16808 *value = gdb_mpz (attr->constant_value (1));
16811 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16812 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16814 If the numerator and/or numerator attribute is missing,
16815 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16816 untouched. */
16818 static void
16819 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16820 gdb_mpz *numerator, gdb_mpz *denominator)
16822 struct attribute *num_attr, *denom_attr;
16824 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16825 if (num_attr == nullptr)
16826 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16827 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16829 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16830 if (denom_attr == nullptr)
16831 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16832 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16834 if (num_attr == nullptr || denom_attr == nullptr)
16835 return;
16837 get_mpz (cu, numerator, num_attr);
16838 get_mpz (cu, denominator, denom_attr);
16841 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16842 rational constant, rather than a signed one.
16844 If the rational constant has a negative value, a complaint
16845 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16847 static void
16848 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16849 struct dwarf2_cu *cu,
16850 gdb_mpz *numerator,
16851 gdb_mpz *denominator)
16853 gdb_mpz num (1);
16854 gdb_mpz denom (1);
16856 get_dwarf2_rational_constant (die, cu, &num, &denom);
16857 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16859 mpz_neg (num.val, num.val);
16860 mpz_neg (denom.val, denom.val);
16862 else if (mpz_sgn (num.val) == -1)
16864 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16865 " in DIE at %s"),
16866 sect_offset_str (die->sect_off));
16867 return;
16869 else if (mpz_sgn (denom.val) == -1)
16871 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16872 " in DIE at %s"),
16873 sect_offset_str (die->sect_off));
16874 return;
16877 *numerator = std::move (num);
16878 *denominator = std::move (denom);
16881 /* Assuming that ENCODING is a string whose contents starting at the
16882 K'th character is "_nn" where "nn" is a decimal number, scan that
16883 number and set RESULT to the value. K is updated to point to the
16884 character immediately following the number.
16886 If the string does not conform to the format described above, false
16887 is returned, and K may or may not be changed. */
16889 static bool
16890 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16892 /* The next character should be an underscore ('_') followed
16893 by a digit. */
16894 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16895 return false;
16897 /* Skip the underscore. */
16898 k++;
16899 int start = k;
16901 /* Determine the number of digits for our number. */
16902 while (isdigit (encoding[k]))
16903 k++;
16904 if (k == start)
16905 return false;
16907 std::string copy (&encoding[start], k - start);
16908 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16909 return false;
16911 return true;
16914 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16915 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16916 DENOM, update OFFSET, and return true on success. Return false on
16917 failure. */
16919 static bool
16920 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16921 gdb_mpz *num, gdb_mpz *denom)
16923 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16924 return false;
16925 return ada_get_gnat_encoded_number (encoding, offset, denom);
16928 /* Assuming DIE corresponds to a fixed point type, finish the creation
16929 of the corresponding TYPE by setting its type-specific data. CU is
16930 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16931 encodings. It is nullptr if the GNAT encoding should be
16932 ignored. */
16934 static void
16935 finish_fixed_point_type (struct type *type, const char *suffix,
16936 struct die_info *die, struct dwarf2_cu *cu)
16938 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16939 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16941 /* If GNAT encodings are preferred, don't examine the
16942 attributes. */
16943 struct attribute *attr = nullptr;
16944 if (suffix == nullptr)
16946 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16947 if (attr == nullptr)
16948 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16949 if (attr == nullptr)
16950 attr = dwarf2_attr (die, DW_AT_small, cu);
16953 /* Numerator and denominator of our fixed-point type's scaling factor.
16954 The default is a scaling factor of 1, which we use as a fallback
16955 when we are not able to decode it (problem with the debugging info,
16956 unsupported forms, bug in GDB, etc...). Using that as the default
16957 allows us to at least print the unscaled value, which might still
16958 be useful to a user. */
16959 gdb_mpz scale_num (1);
16960 gdb_mpz scale_denom (1);
16962 if (attr == nullptr)
16964 int offset = 0;
16965 if (suffix != nullptr
16966 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16967 &scale_denom)
16968 /* The number might be encoded as _nn_dd_nn_dd, where the
16969 second ratio is the 'small value. In this situation, we
16970 want the second value. */
16971 && (suffix[offset] != '_'
16972 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16973 &scale_denom)))
16975 /* Found it. */
16977 else
16979 /* Scaling factor not found. Assume a scaling factor of 1,
16980 and hope for the best. At least the user will be able to
16981 see the encoded value. */
16982 scale_num = 1;
16983 scale_denom = 1;
16984 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16985 sect_offset_str (die->sect_off));
16988 else if (attr->name == DW_AT_binary_scale)
16990 LONGEST scale_exp = attr->constant_value (0);
16991 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16993 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16995 else if (attr->name == DW_AT_decimal_scale)
16997 LONGEST scale_exp = attr->constant_value (0);
16998 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17000 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17002 else if (attr->name == DW_AT_small)
17004 struct die_info *scale_die;
17005 struct dwarf2_cu *scale_cu = cu;
17007 scale_die = follow_die_ref (die, attr, &scale_cu);
17008 if (scale_die->tag == DW_TAG_constant)
17009 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17010 &scale_num, &scale_denom);
17011 else
17012 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17013 " (DIE at %s)"),
17014 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17016 else
17018 complaint (_("unsupported scale attribute %s for fixed-point type"
17019 " (DIE at %s)"),
17020 dwarf_attr_name (attr->name),
17021 sect_offset_str (die->sect_off));
17024 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17025 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17026 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17027 mpq_canonicalize (scaling_factor.val);
17030 /* The gnat-encoding suffix for fixed point. */
17032 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17034 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17035 "XF" suffix of the name. The text after this is what encodes the
17036 'small and 'delta information. Otherwise, return nullptr. */
17038 static const char *
17039 gnat_encoded_fixed_point_type_info (const char *name)
17041 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17044 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17045 (which may be different from NAME) to the architecture back-end to allow
17046 it to guess the correct format if necessary. */
17048 static struct type *
17049 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17050 const char *name_hint, enum bfd_endian byte_order)
17052 struct gdbarch *gdbarch = objfile->arch ();
17053 const struct floatformat **format;
17054 struct type *type;
17056 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17057 if (format)
17058 type = init_float_type (objfile, bits, name, format, byte_order);
17059 else
17060 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17062 return type;
17065 /* Allocate an integer type of size BITS and name NAME. */
17067 static struct type *
17068 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17069 int bits, int unsigned_p, const char *name)
17071 struct type *type;
17073 /* Versions of Intel's C Compiler generate an integer type called "void"
17074 instead of using DW_TAG_unspecified_type. This has been seen on
17075 at least versions 14, 17, and 18. */
17076 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17077 && strcmp (name, "void") == 0)
17078 type = objfile_type (objfile)->builtin_void;
17079 else
17080 type = init_integer_type (objfile, bits, unsigned_p, name);
17082 return type;
17085 /* Return true if DIE has a DW_AT_small attribute whose value is
17086 a constant rational, where both the numerator and denominator
17087 are equal to zero.
17089 CU is the DIE's Compilation Unit. */
17091 static bool
17092 has_zero_over_zero_small_attribute (struct die_info *die,
17093 struct dwarf2_cu *cu)
17095 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17096 if (attr == nullptr)
17097 return false;
17099 struct dwarf2_cu *scale_cu = cu;
17100 struct die_info *scale_die
17101 = follow_die_ref (die, attr, &scale_cu);
17103 if (scale_die->tag != DW_TAG_constant)
17104 return false;
17106 gdb_mpz num (1), denom (1);
17107 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17108 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17111 /* Initialise and return a floating point type of size BITS suitable for
17112 use as a component of a complex number. The NAME_HINT is passed through
17113 when initialising the floating point type and is the name of the complex
17114 type.
17116 As DWARF doesn't currently provide an explicit name for the components
17117 of a complex number, but it can be helpful to have these components
17118 named, we try to select a suitable name based on the size of the
17119 component. */
17120 static struct type *
17121 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17122 struct objfile *objfile,
17123 int bits, const char *name_hint,
17124 enum bfd_endian byte_order)
17126 gdbarch *gdbarch = objfile->arch ();
17127 struct type *tt = nullptr;
17129 /* Try to find a suitable floating point builtin type of size BITS.
17130 We're going to use the name of this type as the name for the complex
17131 target type that we are about to create. */
17132 switch (cu->lang ())
17134 case language_fortran:
17135 switch (bits)
17137 case 32:
17138 tt = builtin_f_type (gdbarch)->builtin_real;
17139 break;
17140 case 64:
17141 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17142 break;
17143 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17144 case 128:
17145 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17146 break;
17148 break;
17149 default:
17150 switch (bits)
17152 case 32:
17153 tt = builtin_type (gdbarch)->builtin_float;
17154 break;
17155 case 64:
17156 tt = builtin_type (gdbarch)->builtin_double;
17157 break;
17158 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17159 case 128:
17160 tt = builtin_type (gdbarch)->builtin_long_double;
17161 break;
17163 break;
17166 /* If the type we found doesn't match the size we were looking for, then
17167 pretend we didn't find a type at all, the complex target type we
17168 create will then be nameless. */
17169 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
17170 tt = nullptr;
17172 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17173 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17176 /* Find a representation of a given base type and install
17177 it in the TYPE field of the die. */
17179 static struct type *
17180 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17182 struct objfile *objfile = cu->per_objfile->objfile;
17183 struct type *type;
17184 struct attribute *attr;
17185 int encoding = 0, bits = 0;
17186 const char *name;
17187 gdbarch *arch;
17189 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17190 if (attr != nullptr && attr->form_is_constant ())
17191 encoding = attr->constant_value (0);
17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17193 if (attr != nullptr)
17194 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17195 name = dwarf2_name (die, cu);
17196 if (!name)
17197 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17199 arch = objfile->arch ();
17200 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17202 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17203 if (attr != nullptr && attr->form_is_constant ())
17205 int endianity = attr->constant_value (0);
17207 switch (endianity)
17209 case DW_END_big:
17210 byte_order = BFD_ENDIAN_BIG;
17211 break;
17212 case DW_END_little:
17213 byte_order = BFD_ENDIAN_LITTLE;
17214 break;
17215 default:
17216 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17217 break;
17221 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17222 && cu->lang () == language_ada
17223 && has_zero_over_zero_small_attribute (die, cu))
17225 /* brobecker/2018-02-24: This is a fixed point type for which
17226 the scaling factor is represented as fraction whose value
17227 does not make sense (zero divided by zero), so we should
17228 normally never see these. However, there is a small category
17229 of fixed point types for which GNAT is unable to provide
17230 the scaling factor via the standard DWARF mechanisms, and
17231 for which the info is provided via the GNAT encodings instead.
17232 This is likely what this DIE is about. */
17233 encoding = (encoding == DW_ATE_signed_fixed
17234 ? DW_ATE_signed
17235 : DW_ATE_unsigned);
17238 /* With GNAT encodings, fixed-point information will be encoded in
17239 the type name. Note that this can also occur with the above
17240 zero-over-zero case, which is why this is a separate "if" rather
17241 than an "else if". */
17242 const char *gnat_encoding_suffix = nullptr;
17243 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17244 && cu->lang () == language_ada
17245 && name != nullptr)
17247 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17248 if (gnat_encoding_suffix != nullptr)
17250 gdb_assert (startswith (gnat_encoding_suffix,
17251 GNAT_FIXED_POINT_SUFFIX));
17252 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17253 name, gnat_encoding_suffix - name);
17254 /* Use -1 here so that SUFFIX points at the "_" after the
17255 "XF". */
17256 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17258 encoding = (encoding == DW_ATE_signed
17259 ? DW_ATE_signed_fixed
17260 : DW_ATE_unsigned_fixed);
17264 switch (encoding)
17266 case DW_ATE_address:
17267 /* Turn DW_ATE_address into a void * pointer. */
17268 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17269 type = init_pointer_type (objfile, bits, name, type);
17270 break;
17271 case DW_ATE_boolean:
17272 type = init_boolean_type (objfile, bits, 1, name);
17273 break;
17274 case DW_ATE_complex_float:
17275 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17276 byte_order);
17277 if (type->code () == TYPE_CODE_ERROR)
17279 if (name == nullptr)
17281 struct obstack *obstack
17282 = &cu->per_objfile->objfile->objfile_obstack;
17283 name = obconcat (obstack, "_Complex ", type->name (),
17284 nullptr);
17286 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17288 else
17289 type = init_complex_type (name, type);
17290 break;
17291 case DW_ATE_decimal_float:
17292 type = init_decfloat_type (objfile, bits, name);
17293 break;
17294 case DW_ATE_float:
17295 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17296 break;
17297 case DW_ATE_signed:
17298 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17299 break;
17300 case DW_ATE_unsigned:
17301 if (cu->lang () == language_fortran
17302 && name
17303 && startswith (name, "character("))
17304 type = init_character_type (objfile, bits, 1, name);
17305 else
17306 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17307 break;
17308 case DW_ATE_signed_char:
17309 if (cu->lang () == language_ada
17310 || cu->lang () == language_m2
17311 || cu->lang () == language_pascal
17312 || cu->lang () == language_fortran)
17313 type = init_character_type (objfile, bits, 0, name);
17314 else
17315 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17316 break;
17317 case DW_ATE_unsigned_char:
17318 if (cu->lang () == language_ada
17319 || cu->lang () == language_m2
17320 || cu->lang () == language_pascal
17321 || cu->lang () == language_fortran
17322 || cu->lang () == language_rust)
17323 type = init_character_type (objfile, bits, 1, name);
17324 else
17325 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17326 break;
17327 case DW_ATE_UTF:
17329 type = init_character_type (objfile, bits, 1, name);
17330 return set_die_type (die, type, cu);
17332 break;
17333 case DW_ATE_signed_fixed:
17334 type = init_fixed_point_type (objfile, bits, 0, name);
17335 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17336 break;
17337 case DW_ATE_unsigned_fixed:
17338 type = init_fixed_point_type (objfile, bits, 1, name);
17339 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17340 break;
17342 default:
17343 complaint (_("unsupported DW_AT_encoding: '%s'"),
17344 dwarf_type_encoding_name (encoding));
17345 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17346 break;
17349 if (type->code () == TYPE_CODE_INT
17350 && name != nullptr
17351 && strcmp (name, "char") == 0)
17352 type->set_has_no_signedness (true);
17354 maybe_set_alignment (cu, die, type);
17356 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17358 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17360 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17361 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
17363 unsigned real_bit_size = attr->as_unsigned ();
17364 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17365 /* Only use the attributes if they make sense together. */
17366 if (attr == nullptr
17367 || (attr->as_unsigned () + real_bit_size
17368 <= 8 * type->length ()))
17370 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17371 = real_bit_size;
17372 if (attr != nullptr)
17373 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17374 = attr->as_unsigned ();
17379 return set_die_type (die, type, cu);
17382 /* A helper function that returns the name of DIE, if it refers to a
17383 variable declaration. */
17385 static const char *
17386 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17388 if (die->tag != DW_TAG_variable)
17389 return nullptr;
17391 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17392 if (attr == nullptr || !attr->as_boolean ())
17393 return nullptr;
17395 attr = dwarf2_attr (die, DW_AT_name, cu);
17396 if (attr == nullptr)
17397 return nullptr;
17398 return attr->as_string ();
17401 /* Parse dwarf attribute if it's a block, reference or constant and put the
17402 resulting value of the attribute into struct bound_prop.
17403 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17405 static int
17406 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17407 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17408 struct type *default_type)
17410 struct dwarf2_property_baton *baton;
17411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17412 struct objfile *objfile = per_objfile->objfile;
17413 struct obstack *obstack = &objfile->objfile_obstack;
17415 gdb_assert (default_type != NULL);
17417 if (attr == NULL || prop == NULL)
17418 return 0;
17420 if (attr->form_is_block ())
17422 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17423 baton->property_type = default_type;
17424 baton->locexpr.per_cu = cu->per_cu;
17425 baton->locexpr.per_objfile = per_objfile;
17427 struct dwarf_block *block;
17428 if (attr->form == DW_FORM_data16)
17430 size_t data_size = 16;
17431 block = XOBNEW (obstack, struct dwarf_block);
17432 block->size = (data_size
17433 + 2 /* Extra bytes for DW_OP and arg. */);
17434 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17435 data[0] = DW_OP_implicit_value;
17436 data[1] = data_size;
17437 memcpy (&data[2], attr->as_block ()->data, data_size);
17438 block->data = data;
17440 else
17441 block = attr->as_block ();
17443 baton->locexpr.size = block->size;
17444 baton->locexpr.data = block->data;
17445 switch (attr->name)
17447 case DW_AT_string_length:
17448 baton->locexpr.is_reference = true;
17449 break;
17450 default:
17451 baton->locexpr.is_reference = false;
17452 break;
17455 prop->set_locexpr (baton);
17456 gdb_assert (prop->baton () != NULL);
17458 else if (attr->form_is_ref ())
17460 struct dwarf2_cu *target_cu = cu;
17461 struct die_info *target_die;
17462 struct attribute *target_attr;
17464 target_die = follow_die_ref (die, attr, &target_cu);
17465 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17466 if (target_attr == NULL)
17467 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17468 target_cu);
17469 if (target_attr == nullptr)
17470 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17471 target_cu);
17472 if (target_attr == NULL)
17474 const char *name = var_decl_name (target_die, target_cu);
17475 if (name != nullptr)
17477 prop->set_variable_name (name);
17478 return 1;
17480 return 0;
17483 switch (target_attr->name)
17485 case DW_AT_location:
17486 if (target_attr->form_is_section_offset ())
17488 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17489 baton->property_type = die_type (target_die, target_cu);
17490 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17491 prop->set_loclist (baton);
17492 gdb_assert (prop->baton () != NULL);
17494 else if (target_attr->form_is_block ())
17496 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17497 baton->property_type = die_type (target_die, target_cu);
17498 baton->locexpr.per_cu = cu->per_cu;
17499 baton->locexpr.per_objfile = per_objfile;
17500 struct dwarf_block *block = target_attr->as_block ();
17501 baton->locexpr.size = block->size;
17502 baton->locexpr.data = block->data;
17503 baton->locexpr.is_reference = true;
17504 prop->set_locexpr (baton);
17505 gdb_assert (prop->baton () != NULL);
17507 else
17509 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17510 "dynamic property");
17511 return 0;
17513 break;
17514 case DW_AT_data_member_location:
17515 case DW_AT_data_bit_offset:
17517 LONGEST offset;
17519 if (!handle_member_location (target_die, target_cu, &offset))
17520 return 0;
17522 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17523 baton->property_type = read_type_die (target_die->parent,
17524 target_cu);
17525 baton->offset_info.offset = offset;
17526 baton->offset_info.type = die_type (target_die, target_cu);
17527 prop->set_addr_offset (baton);
17528 break;
17532 else if (attr->form_is_constant ())
17533 prop->set_const_val (attr->constant_value (0));
17534 else if (attr->form_is_section_offset ())
17536 switch (attr->name)
17538 case DW_AT_string_length:
17539 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17540 baton->property_type = default_type;
17541 fill_in_loclist_baton (cu, &baton->loclist, attr);
17542 prop->set_loclist (baton);
17543 gdb_assert (prop->baton () != NULL);
17544 break;
17545 default:
17546 goto invalid;
17549 else
17550 goto invalid;
17552 return 1;
17554 invalid:
17555 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17556 dwarf2_name (die, cu));
17557 return 0;
17560 /* See read.h. */
17562 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17563 present (which is valid) then compute the default type based on the
17564 compilation units address size. */
17566 static struct type *
17567 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17569 struct type *index_type = die_type (die, cu);
17571 /* Dwarf-2 specifications explicitly allows to create subrange types
17572 without specifying a base type.
17573 In that case, the base type must be set to the type of
17574 the lower bound, upper bound or count, in that order, if any of these
17575 three attributes references an object that has a type.
17576 If no base type is found, the Dwarf-2 specifications say that
17577 a signed integer type of size equal to the size of an address should
17578 be used.
17579 For the following C code: `extern char gdb_int [];'
17580 GCC produces an empty range DIE.
17581 FIXME: muller/2010-05-28: Possible references to object for low bound,
17582 high bound or count are not yet handled by this code. */
17583 if (index_type->code () == TYPE_CODE_VOID)
17584 index_type = cu->addr_sized_int_type (false);
17586 return index_type;
17589 /* Read the given DW_AT_subrange DIE. */
17591 static struct type *
17592 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17594 struct type *base_type, *orig_base_type;
17595 struct type *range_type;
17596 struct attribute *attr;
17597 struct dynamic_prop low, high;
17598 int low_default_is_valid;
17599 int high_bound_is_count = 0;
17600 const char *name;
17601 ULONGEST negative_mask;
17603 orig_base_type = read_subrange_index_type (die, cu);
17605 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17606 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17607 creating the range type, but we use the result of check_typedef
17608 when examining properties of the type. */
17609 base_type = check_typedef (orig_base_type);
17611 /* The die_type call above may have already set the type for this DIE. */
17612 range_type = get_die_type (die, cu);
17613 if (range_type)
17614 return range_type;
17616 high.set_const_val (0);
17618 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17619 omitting DW_AT_lower_bound. */
17620 switch (cu->lang ())
17622 case language_c:
17623 case language_cplus:
17624 low.set_const_val (0);
17625 low_default_is_valid = 1;
17626 break;
17627 case language_fortran:
17628 low.set_const_val (1);
17629 low_default_is_valid = 1;
17630 break;
17631 case language_d:
17632 case language_objc:
17633 case language_rust:
17634 low.set_const_val (0);
17635 low_default_is_valid = (cu->header.version >= 4);
17636 break;
17637 case language_ada:
17638 case language_m2:
17639 case language_pascal:
17640 low.set_const_val (1);
17641 low_default_is_valid = (cu->header.version >= 4);
17642 break;
17643 default:
17644 low.set_const_val (0);
17645 low_default_is_valid = 0;
17646 break;
17649 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17650 if (attr != nullptr)
17651 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17652 else if (!low_default_is_valid)
17653 complaint (_("Missing DW_AT_lower_bound "
17654 "- DIE at %s [in module %s]"),
17655 sect_offset_str (die->sect_off),
17656 objfile_name (cu->per_objfile->objfile));
17658 struct attribute *attr_ub, *attr_count;
17659 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17660 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17662 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17663 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17665 /* If bounds are constant do the final calculation here. */
17666 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17667 high.set_const_val (low.const_val () + high.const_val () - 1);
17668 else
17669 high_bound_is_count = 1;
17671 else
17673 if (attr_ub != NULL)
17674 complaint (_("Unresolved DW_AT_upper_bound "
17675 "- DIE at %s [in module %s]"),
17676 sect_offset_str (die->sect_off),
17677 objfile_name (cu->per_objfile->objfile));
17678 if (attr_count != NULL)
17679 complaint (_("Unresolved DW_AT_count "
17680 "- DIE at %s [in module %s]"),
17681 sect_offset_str (die->sect_off),
17682 objfile_name (cu->per_objfile->objfile));
17686 LONGEST bias = 0;
17687 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17688 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17689 bias = bias_attr->constant_value (0);
17691 /* Normally, the DWARF producers are expected to use a signed
17692 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17693 But this is unfortunately not always the case, as witnessed
17694 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17695 is used instead. To work around that ambiguity, we treat
17696 the bounds as signed, and thus sign-extend their values, when
17697 the base type is signed. */
17698 negative_mask =
17699 -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
17700 if (low.kind () == PROP_CONST
17701 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17702 low.set_const_val (low.const_val () | negative_mask);
17703 if (high.kind () == PROP_CONST
17704 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17705 high.set_const_val (high.const_val () | negative_mask);
17707 /* Check for bit and byte strides. */
17708 struct dynamic_prop byte_stride_prop;
17709 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17710 if (attr_byte_stride != nullptr)
17712 struct type *prop_type = cu->addr_sized_int_type (false);
17713 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17714 prop_type);
17717 struct dynamic_prop bit_stride_prop;
17718 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17719 if (attr_bit_stride != nullptr)
17721 /* It only makes sense to have either a bit or byte stride. */
17722 if (attr_byte_stride != nullptr)
17724 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17725 "- DIE at %s [in module %s]"),
17726 sect_offset_str (die->sect_off),
17727 objfile_name (cu->per_objfile->objfile));
17728 attr_bit_stride = nullptr;
17730 else
17732 struct type *prop_type = cu->addr_sized_int_type (false);
17733 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17734 prop_type);
17738 if (attr_byte_stride != nullptr
17739 || attr_bit_stride != nullptr)
17741 bool byte_stride_p = (attr_byte_stride != nullptr);
17742 struct dynamic_prop *stride
17743 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17745 range_type
17746 = create_range_type_with_stride (NULL, orig_base_type, &low,
17747 &high, bias, stride, byte_stride_p);
17749 else
17750 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17752 if (high_bound_is_count)
17753 range_type->bounds ()->flag_upper_bound_is_count = 1;
17755 /* Ada expects an empty array on no boundary attributes. */
17756 if (attr == NULL && cu->lang () != language_ada)
17757 range_type->bounds ()->high.set_undefined ();
17759 name = dwarf2_name (die, cu);
17760 if (name)
17761 range_type->set_name (name);
17763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17764 if (attr != nullptr)
17765 range_type->set_length (attr->constant_value (0));
17767 maybe_set_alignment (cu, die, range_type);
17769 set_die_type (die, range_type, cu);
17771 /* set_die_type should be already done. */
17772 set_descriptive_type (range_type, die, cu);
17774 return range_type;
17777 static struct type *
17778 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17780 struct type *type;
17782 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17783 type->set_name (dwarf2_name (die, cu));
17785 /* In Ada, an unspecified type is typically used when the description
17786 of the type is deferred to a different unit. When encountering
17787 such a type, we treat it as a stub, and try to resolve it later on,
17788 when needed.
17789 Mark this as a stub type for all languages though. */
17790 type->set_is_stub (true);
17792 return set_die_type (die, type, cu);
17795 /* Read a single die and all its descendents. Set the die's sibling
17796 field to NULL; set other fields in the die correctly, and set all
17797 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17798 location of the info_ptr after reading all of those dies. PARENT
17799 is the parent of the die in question. */
17801 static struct die_info *
17802 read_die_and_children (const struct die_reader_specs *reader,
17803 const gdb_byte *info_ptr,
17804 const gdb_byte **new_info_ptr,
17805 struct die_info *parent)
17807 struct die_info *die;
17808 const gdb_byte *cur_ptr;
17810 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17811 if (die == NULL)
17813 *new_info_ptr = cur_ptr;
17814 return NULL;
17816 store_in_ref_table (die, reader->cu);
17818 if (die->has_children)
17819 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17820 else
17822 die->child = NULL;
17823 *new_info_ptr = cur_ptr;
17826 die->sibling = NULL;
17827 die->parent = parent;
17828 return die;
17831 /* Read a die, all of its descendents, and all of its siblings; set
17832 all of the fields of all of the dies correctly. Arguments are as
17833 in read_die_and_children. */
17835 static struct die_info *
17836 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17837 const gdb_byte *info_ptr,
17838 const gdb_byte **new_info_ptr,
17839 struct die_info *parent)
17841 struct die_info *first_die, *last_sibling;
17842 const gdb_byte *cur_ptr;
17844 cur_ptr = info_ptr;
17845 first_die = last_sibling = NULL;
17847 while (1)
17849 struct die_info *die
17850 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17852 if (die == NULL)
17854 *new_info_ptr = cur_ptr;
17855 return first_die;
17858 if (!first_die)
17859 first_die = die;
17860 else
17861 last_sibling->sibling = die;
17863 last_sibling = die;
17867 /* Read a die, all of its descendents, and all of its siblings; set
17868 all of the fields of all of the dies correctly. Arguments are as
17869 in read_die_and_children.
17870 This the main entry point for reading a DIE and all its children. */
17872 static struct die_info *
17873 read_die_and_siblings (const struct die_reader_specs *reader,
17874 const gdb_byte *info_ptr,
17875 const gdb_byte **new_info_ptr,
17876 struct die_info *parent)
17878 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17879 new_info_ptr, parent);
17881 if (dwarf_die_debug)
17883 gdb_printf (gdb_stdlog,
17884 "Read die from %s@0x%x of %s:\n",
17885 reader->die_section->get_name (),
17886 (unsigned) (info_ptr - reader->die_section->buffer),
17887 bfd_get_filename (reader->abfd));
17888 dump_die (die, dwarf_die_debug);
17891 return die;
17894 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17895 attributes.
17896 The caller is responsible for filling in the extra attributes
17897 and updating (*DIEP)->num_attrs.
17898 Set DIEP to point to a newly allocated die with its information,
17899 except for its child, sibling, and parent fields. */
17901 static const gdb_byte *
17902 read_full_die_1 (const struct die_reader_specs *reader,
17903 struct die_info **diep, const gdb_byte *info_ptr,
17904 int num_extra_attrs)
17906 unsigned int abbrev_number, bytes_read, i;
17907 const struct abbrev_info *abbrev;
17908 struct die_info *die;
17909 struct dwarf2_cu *cu = reader->cu;
17910 bfd *abfd = reader->abfd;
17912 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17913 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17914 info_ptr += bytes_read;
17915 if (!abbrev_number)
17917 *diep = NULL;
17918 return info_ptr;
17921 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17922 if (!abbrev)
17923 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17924 abbrev_number,
17925 bfd_get_filename (abfd));
17927 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17928 die->sect_off = sect_off;
17929 die->tag = abbrev->tag;
17930 die->abbrev = abbrev_number;
17931 die->has_children = abbrev->has_children;
17933 /* Make the result usable.
17934 The caller needs to update num_attrs after adding the extra
17935 attributes. */
17936 die->num_attrs = abbrev->num_attrs;
17938 bool any_need_reprocess = false;
17939 for (i = 0; i < abbrev->num_attrs; ++i)
17941 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17942 info_ptr);
17943 if (die->attrs[i].requires_reprocessing_p ())
17944 any_need_reprocess = true;
17947 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17948 if (attr != nullptr && attr->form_is_unsigned ())
17949 cu->str_offsets_base = attr->as_unsigned ();
17951 attr = die->attr (DW_AT_loclists_base);
17952 if (attr != nullptr)
17953 cu->loclist_base = attr->as_unsigned ();
17955 auto maybe_addr_base = die->addr_base ();
17956 if (maybe_addr_base.has_value ())
17957 cu->addr_base = *maybe_addr_base;
17959 attr = die->attr (DW_AT_rnglists_base);
17960 if (attr != nullptr)
17961 cu->rnglists_base = attr->as_unsigned ();
17963 if (any_need_reprocess)
17965 for (i = 0; i < abbrev->num_attrs; ++i)
17967 if (die->attrs[i].requires_reprocessing_p ())
17968 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17971 *diep = die;
17972 return info_ptr;
17975 /* Read a die and all its attributes.
17976 Set DIEP to point to a newly allocated die with its information,
17977 except for its child, sibling, and parent fields. */
17979 static const gdb_byte *
17980 read_full_die (const struct die_reader_specs *reader,
17981 struct die_info **diep, const gdb_byte *info_ptr)
17983 const gdb_byte *result;
17985 result = read_full_die_1 (reader, diep, info_ptr, 0);
17987 if (dwarf_die_debug)
17989 gdb_printf (gdb_stdlog,
17990 "Read die from %s@0x%x of %s:\n",
17991 reader->die_section->get_name (),
17992 (unsigned) (info_ptr - reader->die_section->buffer),
17993 bfd_get_filename (reader->abfd));
17994 dump_die (*diep, dwarf_die_debug);
17997 return result;
18001 void
18002 cooked_indexer::check_bounds (cutu_reader *reader)
18004 if (reader->cu->per_cu->addresses_seen)
18005 return;
18007 dwarf2_cu *cu = reader->cu;
18009 CORE_ADDR best_lowpc = 0, best_highpc = 0;
18010 /* Possibly set the default values of LOWPC and HIGHPC from
18011 `DW_AT_ranges'. */
18012 dwarf2_find_base_address (reader->comp_unit_die, cu);
18013 enum pc_bounds_kind cu_bounds_kind
18014 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
18015 cu, m_index_storage->get_addrmap (), cu->per_cu);
18016 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
18018 struct objfile *objfile = cu->per_objfile->objfile;
18019 CORE_ADDR baseaddr = objfile->text_section_offset ();
18020 struct gdbarch *gdbarch = objfile->arch ();
18021 CORE_ADDR low
18022 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
18023 - baseaddr);
18024 CORE_ADDR high
18025 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
18026 - baseaddr - 1);
18027 /* Store the contiguous range if it is not empty; it can be
18028 empty for CUs with no code. */
18029 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
18031 cu->per_cu->addresses_seen = true;
18035 /* Helper function that returns true if TAG can have a linkage
18036 name. */
18038 static bool
18039 tag_can_have_linkage_name (enum dwarf_tag tag)
18041 switch (tag)
18043 case DW_TAG_variable:
18044 case DW_TAG_subprogram:
18045 return true;
18047 default:
18048 return false;
18052 cutu_reader *
18053 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
18054 dwarf2_per_objfile *per_objfile,
18055 sect_offset sect_off, bool is_dwz,
18056 bool for_scanning)
18058 /* Lookups for type unit references are always in the CU, and
18059 cross-CU references will crash. */
18060 if (reader->cu->per_cu->is_dwz == is_dwz
18061 && reader->cu->header.offset_in_cu_p (sect_off))
18062 return reader;
18064 dwarf2_per_cu_data *per_cu
18065 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
18066 per_objfile->per_bfd);
18068 /* When scanning, we only want to visit a given CU a single time.
18069 Doing this check here avoids self-imports as well. */
18070 if (for_scanning)
18072 bool nope = false;
18073 if (!per_cu->scanned.compare_exchange_strong (nope, true))
18074 return nullptr;
18076 if (per_cu == m_per_cu)
18077 return reader;
18079 cutu_reader *result = m_index_storage->get_reader (per_cu);
18080 if (result == nullptr)
18082 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
18083 m_index_storage->get_abbrev_cache ());
18085 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
18086 language_minimal);
18087 std::unique_ptr<cutu_reader> copy
18088 (new cutu_reader (std::move (new_reader)));
18089 result = m_index_storage->preserve (std::move (copy));
18092 if (result->dummy_p || !result->comp_unit_die->has_children)
18093 return nullptr;
18095 if (for_scanning)
18096 check_bounds (result);
18098 return result;
18101 const gdb_byte *
18102 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
18103 cutu_reader *reader,
18104 const gdb_byte *watermark_ptr,
18105 const gdb_byte *info_ptr,
18106 const abbrev_info *abbrev,
18107 const char **name,
18108 const char **linkage_name,
18109 cooked_index_flag *flags,
18110 sect_offset *sibling_offset,
18111 const cooked_index_entry **parent_entry,
18112 CORE_ADDR *maybe_defer,
18113 bool for_specification)
18115 bool origin_is_dwz = false;
18116 bool is_declaration = false;
18117 sect_offset origin_offset {};
18119 gdb::optional<CORE_ADDR> low_pc;
18120 gdb::optional<CORE_ADDR> high_pc;
18121 bool high_pc_relative = false;
18123 for (int i = 0; i < abbrev->num_attrs; ++i)
18125 attribute attr;
18126 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18127 if (attr.requires_reprocessing_p ())
18128 read_attribute_reprocess (reader, &attr, abbrev->tag);
18130 /* Store the data if it is of an attribute we want to keep in a
18131 partial symbol table. */
18132 switch (attr.name)
18134 case DW_AT_name:
18135 switch (abbrev->tag)
18137 case DW_TAG_compile_unit:
18138 case DW_TAG_partial_unit:
18139 case DW_TAG_type_unit:
18140 /* Compilation units have a DW_AT_name that is a filename, not
18141 a source language identifier. */
18142 break;
18144 default:
18145 if (*name == nullptr)
18146 *name = attr.as_string ();
18147 break;
18149 break;
18151 case DW_AT_linkage_name:
18152 case DW_AT_MIPS_linkage_name:
18153 /* Note that both forms of linkage name might appear. We
18154 assume they will be the same, and we only store the last
18155 one we see. */
18156 if (*linkage_name == nullptr)
18157 *linkage_name = attr.as_string ();
18158 break;
18160 case DW_AT_main_subprogram:
18161 if (attr.as_boolean ())
18162 *flags |= IS_MAIN;
18163 break;
18165 case DW_AT_declaration:
18166 is_declaration = attr.as_boolean ();
18167 break;
18169 case DW_AT_sibling:
18170 if (sibling_offset != nullptr)
18171 *sibling_offset = attr.get_ref_die_offset ();
18172 break;
18174 case DW_AT_specification:
18175 case DW_AT_abstract_origin:
18176 case DW_AT_extension:
18177 origin_offset = attr.get_ref_die_offset ();
18178 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18179 break;
18181 case DW_AT_external:
18182 if (attr.as_boolean ())
18183 *flags &= ~IS_STATIC;
18184 break;
18186 case DW_AT_enum_class:
18187 if (attr.as_boolean ())
18188 *flags |= IS_ENUM_CLASS;
18189 break;
18191 case DW_AT_low_pc:
18192 low_pc = attr.as_address ();
18193 break;
18195 case DW_AT_high_pc:
18196 high_pc = attr.as_address ();
18197 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18198 high_pc_relative = true;
18199 break;
18201 case DW_AT_location:
18202 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18204 struct dwarf_block *locdesc = attr.as_block ();
18205 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18206 if (addr != 0
18207 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18209 low_pc = addr;
18210 /* For variables, we don't want to try decoding the
18211 type just to find the size -- for gdb's purposes
18212 we only need the address of a variable. */
18213 high_pc = addr + 1;
18214 high_pc_relative = false;
18217 break;
18219 case DW_AT_ranges:
18220 if (!scanning_per_cu->addresses_seen)
18222 /* Offset in the .debug_ranges or .debug_rnglist section
18223 (depending on DWARF version). */
18224 ULONGEST ranges_offset = attr.as_unsigned ();
18226 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18227 want to add this value. */
18228 ranges_offset += reader->cu->gnu_ranges_base;
18230 CORE_ADDR lowpc, highpc;
18231 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18232 m_index_storage->get_addrmap (),
18233 scanning_per_cu, abbrev->tag);
18235 break;
18239 /* We don't want to examine declarations, but if we found a
18240 declaration when handling DW_AT_specification or the like, then
18241 that is ok. Similarly, we allow an external variable without a
18242 location; those are resolved via minimal symbols. */
18243 if (is_declaration && !for_specification
18244 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
18246 /* We always want to recurse into some types, but we may not
18247 want to treat them as definitions. */
18248 if ((abbrev->tag == DW_TAG_class_type
18249 || abbrev->tag == DW_TAG_structure_type
18250 || abbrev->tag == DW_TAG_union_type)
18251 && abbrev->has_children)
18252 *flags |= IS_TYPE_DECLARATION;
18253 else
18255 *linkage_name = nullptr;
18256 *name = nullptr;
18259 else if ((*name == nullptr
18260 || (*linkage_name == nullptr
18261 && tag_can_have_linkage_name (abbrev->tag))
18262 || (*parent_entry == nullptr && m_language != language_c))
18263 && origin_offset != sect_offset (0))
18265 cutu_reader *new_reader
18266 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18267 origin_is_dwz, false);
18268 if (new_reader != nullptr)
18270 const gdb_byte *new_info_ptr = (new_reader->buffer
18271 + to_underlying (origin_offset));
18273 if (new_reader->cu == reader->cu
18274 && new_info_ptr > watermark_ptr
18275 && *parent_entry == nullptr)
18276 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18277 else if (*parent_entry == nullptr)
18279 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18280 *parent_entry
18281 = (cooked_index_entry *) m_die_range_map.find (lookup);
18284 unsigned int bytes_read;
18285 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18286 new_info_ptr,
18287 &bytes_read);
18288 new_info_ptr += bytes_read;
18289 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18290 new_abbrev, name, linkage_name, flags, nullptr,
18291 parent_entry, maybe_defer, true);
18295 if (!for_specification)
18297 if (m_language == language_ada
18298 && *linkage_name == nullptr)
18299 *linkage_name = *name;
18301 if (!scanning_per_cu->addresses_seen
18302 && low_pc.has_value ()
18303 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18304 || *low_pc != 0)
18305 && high_pc.has_value ())
18307 if (high_pc_relative)
18308 high_pc = *high_pc + *low_pc;
18310 if (*high_pc > *low_pc)
18312 struct objfile *objfile = reader->cu->per_objfile->objfile;
18313 CORE_ADDR baseaddr = objfile->text_section_offset ();
18314 struct gdbarch *gdbarch = objfile->arch ();
18315 CORE_ADDR lo
18316 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18317 - baseaddr);
18318 CORE_ADDR hi
18319 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18320 - baseaddr);
18321 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
18322 scanning_per_cu);
18326 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18327 *flags &= ~IS_STATIC;
18329 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18330 *name = "(anonymous namespace)";
18332 if (m_language == language_cplus
18333 && (abbrev->tag == DW_TAG_class_type
18334 || abbrev->tag == DW_TAG_interface_type
18335 || abbrev->tag == DW_TAG_structure_type
18336 || abbrev->tag == DW_TAG_union_type
18337 || abbrev->tag == DW_TAG_enumeration_type
18338 || abbrev->tag == DW_TAG_enumerator))
18339 *flags &= ~IS_STATIC;
18342 return info_ptr;
18345 const gdb_byte *
18346 cooked_indexer::index_imported_unit (cutu_reader *reader,
18347 const gdb_byte *info_ptr,
18348 const abbrev_info *abbrev)
18350 sect_offset sect_off {};
18351 bool is_dwz = false;
18353 for (int i = 0; i < abbrev->num_attrs; ++i)
18355 /* Note that we never need to reprocess attributes here. */
18356 attribute attr;
18357 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18359 if (attr.name == DW_AT_import)
18361 sect_off = attr.get_ref_die_offset ();
18362 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18363 || reader->cu->per_cu->is_dwz);
18367 /* Did not find DW_AT_import. */
18368 if (sect_off == sect_offset (0))
18369 return info_ptr;
18371 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18372 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18373 is_dwz, true);
18374 if (new_reader != nullptr)
18376 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18378 reader->cu->add_dependence (new_reader->cu->per_cu);
18381 return info_ptr;
18384 const gdb_byte *
18385 cooked_indexer::recurse (cutu_reader *reader,
18386 const gdb_byte *info_ptr,
18387 const cooked_index_entry *parent_entry,
18388 bool fully)
18390 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18392 if (parent_entry != nullptr)
18394 CORE_ADDR start = form_addr (parent_entry->die_offset,
18395 reader->cu->per_cu->is_dwz);
18396 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18397 reader->cu->per_cu->is_dwz);
18398 m_die_range_map.set_empty (start, end, (void *) parent_entry);
18401 return info_ptr;
18404 const gdb_byte *
18405 cooked_indexer::index_dies (cutu_reader *reader,
18406 const gdb_byte *info_ptr,
18407 const cooked_index_entry *parent_entry,
18408 bool fully)
18410 const gdb_byte *end_ptr = (reader->buffer
18411 + to_underlying (reader->cu->header.sect_off)
18412 + reader->cu->header.get_length_with_initial ());
18414 while (info_ptr < end_ptr)
18416 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18417 unsigned int bytes_read;
18418 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18419 &bytes_read);
18420 info_ptr += bytes_read;
18421 if (abbrev == nullptr)
18422 break;
18424 if (abbrev->tag == DW_TAG_imported_unit)
18426 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18427 continue;
18430 if (!abbrev->interesting)
18432 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18433 if (fully && abbrev->has_children)
18434 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18435 continue;
18438 const char *name = nullptr;
18439 const char *linkage_name = nullptr;
18440 CORE_ADDR defer = 0;
18441 cooked_index_flag flags = IS_STATIC;
18442 sect_offset sibling {};
18443 const cooked_index_entry *this_parent_entry = parent_entry;
18444 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18445 info_ptr, abbrev, &name, &linkage_name,
18446 &flags, &sibling, &this_parent_entry,
18447 &defer, false);
18449 if (abbrev->tag == DW_TAG_namespace
18450 && m_language == language_cplus
18451 && strcmp (name, "::") == 0)
18453 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18454 generated bogus DW_TAG_namespace DIEs with a name of "::"
18455 for the global namespace. Work around this problem
18456 here. */
18457 name = nullptr;
18460 const cooked_index_entry *this_entry = nullptr;
18461 if (name != nullptr)
18463 if (defer != 0)
18464 m_deferred_entries.push_back ({
18465 this_die, name, defer, abbrev->tag, flags
18467 else
18468 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18469 name, this_parent_entry,
18470 m_per_cu);
18473 if (linkage_name != nullptr)
18475 /* We only want this to be "main" if it has a linkage name
18476 but not an ordinary name. */
18477 if (name != nullptr)
18478 flags = flags & ~IS_MAIN;
18479 /* Set the IS_LINKAGE on for everything except when functions
18480 have linkage name present but name is absent. */
18481 if (name != nullptr
18482 || (abbrev->tag != DW_TAG_subprogram
18483 && abbrev->tag != DW_TAG_inlined_subroutine
18484 && abbrev->tag != DW_TAG_entry_point))
18485 flags = flags | IS_LINKAGE;
18486 m_index_storage->add (this_die, abbrev->tag, flags,
18487 linkage_name, nullptr, m_per_cu);
18490 if (abbrev->has_children)
18492 switch (abbrev->tag)
18494 case DW_TAG_class_type:
18495 case DW_TAG_interface_type:
18496 case DW_TAG_structure_type:
18497 case DW_TAG_union_type:
18498 if (m_language != language_c && this_entry != nullptr)
18500 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18501 continue;
18503 break;
18505 case DW_TAG_enumeration_type:
18506 /* We need to recurse even for an anonymous enumeration.
18507 Which scope we record as the parent scope depends on
18508 whether we're reading an "enum class". If so, we use
18509 the enum itself as the parent, yielding names like
18510 "enum_class::enumerator"; otherwise we inject the
18511 names into our own parent scope. */
18512 info_ptr = recurse (reader, info_ptr,
18513 ((flags & IS_ENUM_CLASS) == 0)
18514 ? parent_entry
18515 : this_entry,
18516 fully);
18517 continue;
18519 case DW_TAG_module:
18520 if (this_entry == nullptr)
18521 break;
18522 /* FALLTHROUGH */
18523 case DW_TAG_namespace:
18524 /* We don't check THIS_ENTRY for a namespace, to handle
18525 the ancient G++ workaround pointed out above. */
18526 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18527 continue;
18529 case DW_TAG_subprogram:
18530 if ((m_language == language_fortran
18531 || m_language == language_ada)
18532 && this_entry != nullptr)
18534 info_ptr = recurse (reader, info_ptr, this_entry, true);
18535 continue;
18537 break;
18540 if (sibling != sect_offset (0))
18542 const gdb_byte *sibling_ptr
18543 = reader->buffer + to_underlying (sibling);
18545 if (sibling_ptr < info_ptr)
18546 complaint (_("DW_AT_sibling points backwards"));
18547 else if (sibling_ptr > reader->buffer_end)
18548 reader->die_section->overflow_complaint ();
18549 else
18550 info_ptr = sibling_ptr;
18552 else
18553 info_ptr = skip_children (reader, info_ptr);
18557 return info_ptr;
18560 void
18561 cooked_indexer::make_index (cutu_reader *reader)
18563 check_bounds (reader);
18564 find_file_and_directory (reader->comp_unit_die, reader->cu);
18565 if (!reader->comp_unit_die->has_children)
18566 return;
18567 index_dies (reader, reader->info_ptr, nullptr, false);
18569 for (const auto &entry : m_deferred_entries)
18571 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18572 cooked_index_entry *parent
18573 = (cooked_index_entry *) m_die_range_map.find (key);
18574 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18575 entry.name, parent, m_per_cu);
18579 /* An implementation of quick_symbol_functions for the cooked DWARF
18580 index. */
18582 struct cooked_index_functions : public dwarf2_base_index_functions
18584 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
18585 CORE_ADDR adjusted_pc) override;
18587 struct compunit_symtab *find_compunit_symtab_by_address
18588 (struct objfile *objfile, CORE_ADDR address) override;
18590 void dump (struct objfile *objfile) override
18592 gdb_printf ("Cooked index in use\n");
18595 void expand_matching_symbols
18596 (struct objfile *,
18597 const lookup_name_info &lookup_name,
18598 domain_enum domain,
18599 int global,
18600 symbol_compare_ftype *ordered_compare) override;
18602 bool expand_symtabs_matching
18603 (struct objfile *objfile,
18604 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18605 const lookup_name_info *lookup_name,
18606 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18607 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18608 block_search_flags search_flags,
18609 domain_enum domain,
18610 enum search_domain kind) override;
18612 bool can_lazily_read_symbols () override
18614 return true;
18617 void read_partial_symbols (struct objfile *objfile) override
18619 if (dwarf2_has_info (objfile, nullptr))
18620 dwarf2_build_psymtabs (objfile);
18624 dwarf2_per_cu_data *
18625 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
18626 CORE_ADDR adjusted_pc)
18628 if (per_bfd->index_table == nullptr)
18629 return nullptr;
18630 cooked_index_vector *table
18631 = (gdb::checked_static_cast<cooked_index_vector *>
18632 (per_bfd->index_table.get ()));
18633 return table->lookup (adjusted_pc);
18636 struct compunit_symtab *
18637 cooked_index_functions::find_compunit_symtab_by_address
18638 (struct objfile *objfile, CORE_ADDR address)
18640 if (objfile->sect_index_data == -1)
18641 return nullptr;
18643 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18644 if (per_objfile->per_bfd->index_table == nullptr)
18645 return nullptr;
18647 CORE_ADDR baseaddr = objfile->data_section_offset ();
18648 cooked_index_vector *table
18649 = (gdb::checked_static_cast<cooked_index_vector *>
18650 (per_objfile->per_bfd->index_table.get ()));
18651 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18652 if (per_cu == nullptr)
18653 return nullptr;
18655 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18658 void
18659 cooked_index_functions::expand_matching_symbols
18660 (struct objfile *objfile,
18661 const lookup_name_info &lookup_name,
18662 domain_enum domain,
18663 int global,
18664 symbol_compare_ftype *ordered_compare)
18666 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18667 if (per_objfile->per_bfd->index_table == nullptr)
18668 return;
18669 const block_search_flags search_flags = (global
18670 ? SEARCH_GLOBAL_BLOCK
18671 : SEARCH_STATIC_BLOCK);
18672 const language_defn *lang = language_def (language_ada);
18673 symbol_name_matcher_ftype *name_match
18674 = lang->get_symbol_name_matcher (lookup_name);
18676 cooked_index_vector *table
18677 = (gdb::checked_static_cast<cooked_index_vector *>
18678 (per_objfile->per_bfd->index_table.get ()));
18679 for (const cooked_index_entry *entry : table->all_entries ())
18681 QUIT;
18683 if (entry->parent_entry != nullptr)
18684 continue;
18686 if (!entry->matches (search_flags)
18687 || !entry->matches (domain))
18688 continue;
18690 if (name_match (entry->canonical, lookup_name, nullptr))
18691 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18695 bool
18696 cooked_index_functions::expand_symtabs_matching
18697 (struct objfile *objfile,
18698 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18699 const lookup_name_info *lookup_name,
18700 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18701 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18702 block_search_flags search_flags,
18703 domain_enum domain,
18704 enum search_domain kind)
18706 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18707 if (per_objfile->per_bfd->index_table == nullptr)
18708 return true;
18710 cooked_index_vector *table
18711 = (gdb::checked_static_cast<cooked_index_vector *>
18712 (per_objfile->per_bfd->index_table.get ()));
18713 table->wait ();
18715 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18717 /* This invariant is documented in quick-functions.h. */
18718 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18719 if (lookup_name == nullptr)
18721 for (dwarf2_per_cu_data *per_cu
18722 : all_units_range (per_objfile->per_bfd))
18724 QUIT;
18726 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18727 file_matcher,
18728 expansion_notify))
18729 return false;
18731 return true;
18734 lookup_name_info lookup_name_without_params
18735 = lookup_name->make_ignore_params ();
18736 bool completing = lookup_name->completion_mode ();
18738 /* Unique styles of language splitting. */
18739 static const enum language unique_styles[] =
18741 /* No splitting is also a style. */
18742 language_c,
18743 /* This includes Rust. */
18744 language_cplus,
18745 /* This includes Go. */
18746 language_d,
18747 language_ada
18750 for (enum language lang : unique_styles)
18752 std::vector<gdb::string_view> name_vec
18753 = lookup_name_without_params.split_name (lang);
18755 for (const cooked_index_entry *entry : table->find (name_vec.back (),
18756 completing))
18758 QUIT;
18760 /* No need to consider symbols from expanded CUs. */
18761 if (per_objfile->symtab_set_p (entry->per_cu))
18762 continue;
18764 /* If file-matching was done, we don't need to consider
18765 symbols from unmarked CUs. */
18766 if (file_matcher != nullptr && !entry->per_cu->mark)
18767 continue;
18769 /* See if the symbol matches the type filter. */
18770 if (!entry->matches (search_flags)
18771 || !entry->matches (domain)
18772 || !entry->matches (kind))
18773 continue;
18775 /* We've found the base name of the symbol; now walk its
18776 parentage chain, ensuring that each component
18777 matches. */
18778 bool found = true;
18780 const cooked_index_entry *parent = entry->parent_entry;
18781 for (int i = name_vec.size () - 1; i > 0; --i)
18783 /* If we ran out of entries, or if this segment doesn't
18784 match, this did not match. */
18785 if (parent == nullptr
18786 || strncmp (parent->name, name_vec[i - 1].data (),
18787 name_vec[i - 1].length ()) != 0)
18789 found = false;
18790 break;
18793 parent = parent->parent_entry;
18796 if (!found)
18797 continue;
18799 /* Might have been looking for "a::b" and found
18800 "x::a::b". */
18801 if (symbol_matcher == nullptr)
18803 symbol_name_match_type match_type
18804 = lookup_name_without_params.match_type ();
18805 if ((match_type == symbol_name_match_type::FULL
18806 || (lang != language_ada
18807 && match_type == symbol_name_match_type::EXPRESSION))
18808 && parent != nullptr)
18809 continue;
18811 else
18813 auto_obstack temp_storage;
18814 const char *full_name = entry->full_name (&temp_storage);
18815 if (!symbol_matcher (full_name))
18816 continue;
18819 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18820 file_matcher,
18821 expansion_notify))
18822 return false;
18826 return true;
18829 /* Return a new cooked_index_functions object. */
18831 static quick_symbol_functions_up
18832 make_cooked_index_funcs ()
18834 return quick_symbol_functions_up (new cooked_index_functions);
18837 quick_symbol_functions_up
18838 cooked_index_vector::make_quick_functions () const
18840 return make_cooked_index_funcs ();
18845 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18846 contents from the given SECTION in the HEADER.
18848 HEADER_OFFSET is the offset of the header in the section. */
18849 static void
18850 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18851 struct dwarf2_section_info *section,
18852 sect_offset header_offset)
18854 unsigned int bytes_read;
18855 bfd *abfd = section->get_bfd_owner ();
18856 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18858 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18859 info_ptr += bytes_read;
18861 header->version = read_2_bytes (abfd, info_ptr);
18862 info_ptr += 2;
18864 header->addr_size = read_1_byte (abfd, info_ptr);
18865 info_ptr += 1;
18867 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18868 info_ptr += 1;
18870 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18873 /* Return the DW_AT_loclists_base value for the CU. */
18874 static ULONGEST
18875 lookup_loclist_base (struct dwarf2_cu *cu)
18877 /* For the .dwo unit, the loclist_base points to the first offset following
18878 the header. The header consists of the following entities-
18879 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18880 bit format)
18881 2. version (2 bytes)
18882 3. address size (1 byte)
18883 4. segment selector size (1 byte)
18884 5. offset entry count (4 bytes)
18885 These sizes are derived as per the DWARFv5 standard. */
18886 if (cu->dwo_unit != nullptr)
18888 if (cu->header.initial_length_size == 4)
18889 return LOCLIST_HEADER_SIZE32;
18890 return LOCLIST_HEADER_SIZE64;
18892 return cu->loclist_base;
18895 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18896 array of offsets in the .debug_loclists section. */
18898 static sect_offset
18899 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18901 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18902 struct objfile *objfile = per_objfile->objfile;
18903 bfd *abfd = objfile->obfd.get ();
18904 ULONGEST loclist_header_size =
18905 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18906 : LOCLIST_HEADER_SIZE64);
18907 ULONGEST loclist_base = lookup_loclist_base (cu);
18909 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18910 ULONGEST start_offset =
18911 loclist_base + loclist_index * cu->header.offset_size;
18913 /* Get loclists section. */
18914 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18916 /* Read the loclists section content. */
18917 section->read (objfile);
18918 if (section->buffer == NULL)
18919 error (_("DW_FORM_loclistx used without .debug_loclists "
18920 "section [in module %s]"), objfile_name (objfile));
18922 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18923 so if loclist_base is smaller than the header size, we have a problem. */
18924 if (loclist_base < loclist_header_size)
18925 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18926 objfile_name (objfile));
18928 /* Read the header of the loclists contribution. */
18929 struct loclists_rnglists_header header;
18930 read_loclists_rnglists_header (&header, section,
18931 (sect_offset) (loclist_base - loclist_header_size));
18933 /* Verify the loclist index is valid. */
18934 if (loclist_index >= header.offset_entry_count)
18935 error (_("DW_FORM_loclistx pointing outside of "
18936 ".debug_loclists offset array [in module %s]"),
18937 objfile_name (objfile));
18939 /* Validate that reading won't go beyond the end of the section. */
18940 if (start_offset + cu->header.offset_size > section->size)
18941 error (_("Reading DW_FORM_loclistx index beyond end of"
18942 ".debug_loclists section [in module %s]"),
18943 objfile_name (objfile));
18945 const gdb_byte *info_ptr = section->buffer + start_offset;
18947 if (cu->header.offset_size == 4)
18948 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18949 else
18950 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18953 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18954 array of offsets in the .debug_rnglists section. */
18956 static sect_offset
18957 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18958 dwarf_tag tag)
18960 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18961 struct objfile *objfile = dwarf2_per_objfile->objfile;
18962 bfd *abfd = objfile->obfd.get ();
18963 ULONGEST rnglist_header_size =
18964 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18965 : RNGLIST_HEADER_SIZE64);
18967 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18968 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18969 doesn't apply. */
18970 ULONGEST rnglist_base =
18971 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18973 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18974 ULONGEST start_offset =
18975 rnglist_base + rnglist_index * cu->header.offset_size;
18977 /* Get rnglists section. */
18978 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18980 /* Read the rnglists section content. */
18981 section->read (objfile);
18982 if (section->buffer == nullptr)
18983 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18984 "[in module %s]"),
18985 objfile_name (objfile));
18987 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18988 so if rnglist_base is smaller than the header size, we have a problem. */
18989 if (rnglist_base < rnglist_header_size)
18990 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18991 objfile_name (objfile));
18993 /* Read the header of the rnglists contribution. */
18994 struct loclists_rnglists_header header;
18995 read_loclists_rnglists_header (&header, section,
18996 (sect_offset) (rnglist_base - rnglist_header_size));
18998 /* Verify the rnglist index is valid. */
18999 if (rnglist_index >= header.offset_entry_count)
19000 error (_("DW_FORM_rnglistx index pointing outside of "
19001 ".debug_rnglists offset array [in module %s]"),
19002 objfile_name (objfile));
19004 /* Validate that reading won't go beyond the end of the section. */
19005 if (start_offset + cu->header.offset_size > section->size)
19006 error (_("Reading DW_FORM_rnglistx index beyond end of"
19007 ".debug_rnglists section [in module %s]"),
19008 objfile_name (objfile));
19010 const gdb_byte *info_ptr = section->buffer + start_offset;
19012 if (cu->header.offset_size == 4)
19013 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19014 else
19015 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19018 /* Process the attributes that had to be skipped in the first round. These
19019 attributes are the ones that need str_offsets_base or addr_base attributes.
19020 They could not have been processed in the first round, because at the time
19021 the values of str_offsets_base or addr_base may not have been known. */
19022 static void
19023 read_attribute_reprocess (const struct die_reader_specs *reader,
19024 struct attribute *attr, dwarf_tag tag)
19026 struct dwarf2_cu *cu = reader->cu;
19027 switch (attr->form)
19029 case DW_FORM_addrx:
19030 case DW_FORM_GNU_addr_index:
19031 attr->set_address (read_addr_index (cu,
19032 attr->as_unsigned_reprocess ()));
19033 break;
19034 case DW_FORM_loclistx:
19036 sect_offset loclists_sect_off
19037 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19039 attr->set_unsigned (to_underlying (loclists_sect_off));
19041 break;
19042 case DW_FORM_rnglistx:
19044 sect_offset rnglists_sect_off
19045 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19047 attr->set_unsigned (to_underlying (rnglists_sect_off));
19049 break;
19050 case DW_FORM_strx:
19051 case DW_FORM_strx1:
19052 case DW_FORM_strx2:
19053 case DW_FORM_strx3:
19054 case DW_FORM_strx4:
19055 case DW_FORM_GNU_str_index:
19057 unsigned int str_index = attr->as_unsigned_reprocess ();
19058 gdb_assert (!attr->canonical_string_p ());
19059 if (reader->dwo_file != NULL)
19060 attr->set_string_noncanonical (read_dwo_str_index (reader,
19061 str_index));
19062 else
19063 attr->set_string_noncanonical (read_stub_str_index (cu,
19064 str_index));
19065 break;
19067 default:
19068 gdb_assert_not_reached ("Unexpected DWARF form.");
19072 /* Read an attribute value described by an attribute form. */
19074 static const gdb_byte *
19075 read_attribute_value (const struct die_reader_specs *reader,
19076 struct attribute *attr, unsigned form,
19077 LONGEST implicit_const, const gdb_byte *info_ptr)
19079 struct dwarf2_cu *cu = reader->cu;
19080 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19081 struct objfile *objfile = per_objfile->objfile;
19082 bfd *abfd = reader->abfd;
19083 struct comp_unit_head *cu_header = &cu->header;
19084 unsigned int bytes_read;
19085 struct dwarf_block *blk;
19087 attr->form = (enum dwarf_form) form;
19088 switch (form)
19090 case DW_FORM_ref_addr:
19091 if (cu_header->version == 2)
19092 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19093 &bytes_read));
19094 else
19095 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19096 &bytes_read));
19097 info_ptr += bytes_read;
19098 break;
19099 case DW_FORM_GNU_ref_alt:
19100 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19101 &bytes_read));
19102 info_ptr += bytes_read;
19103 break;
19104 case DW_FORM_addr:
19106 struct gdbarch *gdbarch = objfile->arch ();
19107 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19108 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19109 attr->set_address (addr);
19110 info_ptr += bytes_read;
19112 break;
19113 case DW_FORM_block2:
19114 blk = dwarf_alloc_block (cu);
19115 blk->size = read_2_bytes (abfd, info_ptr);
19116 info_ptr += 2;
19117 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19118 info_ptr += blk->size;
19119 attr->set_block (blk);
19120 break;
19121 case DW_FORM_block4:
19122 blk = dwarf_alloc_block (cu);
19123 blk->size = read_4_bytes (abfd, info_ptr);
19124 info_ptr += 4;
19125 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19126 info_ptr += blk->size;
19127 attr->set_block (blk);
19128 break;
19129 case DW_FORM_data2:
19130 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19131 info_ptr += 2;
19132 break;
19133 case DW_FORM_data4:
19134 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19135 info_ptr += 4;
19136 break;
19137 case DW_FORM_data8:
19138 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19139 info_ptr += 8;
19140 break;
19141 case DW_FORM_data16:
19142 blk = dwarf_alloc_block (cu);
19143 blk->size = 16;
19144 blk->data = read_n_bytes (abfd, info_ptr, 16);
19145 info_ptr += 16;
19146 attr->set_block (blk);
19147 break;
19148 case DW_FORM_sec_offset:
19149 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19150 &bytes_read));
19151 info_ptr += bytes_read;
19152 break;
19153 case DW_FORM_loclistx:
19155 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19156 &bytes_read));
19157 info_ptr += bytes_read;
19159 break;
19160 case DW_FORM_string:
19161 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19162 &bytes_read));
19163 info_ptr += bytes_read;
19164 break;
19165 case DW_FORM_strp:
19166 if (!cu->per_cu->is_dwz)
19168 attr->set_string_noncanonical
19169 (read_indirect_string (per_objfile,
19170 abfd, info_ptr, cu_header,
19171 &bytes_read));
19172 info_ptr += bytes_read;
19173 break;
19175 /* FALLTHROUGH */
19176 case DW_FORM_line_strp:
19177 if (!cu->per_cu->is_dwz)
19179 attr->set_string_noncanonical
19180 (per_objfile->read_line_string (info_ptr, cu_header,
19181 &bytes_read));
19182 info_ptr += bytes_read;
19183 break;
19185 /* FALLTHROUGH */
19186 case DW_FORM_GNU_strp_alt:
19188 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19189 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19190 &bytes_read);
19192 attr->set_string_noncanonical
19193 (dwz->read_string (objfile, str_offset));
19194 info_ptr += bytes_read;
19196 break;
19197 case DW_FORM_exprloc:
19198 case DW_FORM_block:
19199 blk = dwarf_alloc_block (cu);
19200 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19201 info_ptr += bytes_read;
19202 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19203 info_ptr += blk->size;
19204 attr->set_block (blk);
19205 break;
19206 case DW_FORM_block1:
19207 blk = dwarf_alloc_block (cu);
19208 blk->size = read_1_byte (abfd, info_ptr);
19209 info_ptr += 1;
19210 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19211 info_ptr += blk->size;
19212 attr->set_block (blk);
19213 break;
19214 case DW_FORM_data1:
19215 case DW_FORM_flag:
19216 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19217 info_ptr += 1;
19218 break;
19219 case DW_FORM_flag_present:
19220 attr->set_unsigned (1);
19221 break;
19222 case DW_FORM_sdata:
19223 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19224 info_ptr += bytes_read;
19225 break;
19226 case DW_FORM_rnglistx:
19228 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19229 &bytes_read));
19230 info_ptr += bytes_read;
19232 break;
19233 case DW_FORM_udata:
19234 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19235 info_ptr += bytes_read;
19236 break;
19237 case DW_FORM_ref1:
19238 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19239 + read_1_byte (abfd, info_ptr)));
19240 info_ptr += 1;
19241 break;
19242 case DW_FORM_ref2:
19243 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19244 + read_2_bytes (abfd, info_ptr)));
19245 info_ptr += 2;
19246 break;
19247 case DW_FORM_ref4:
19248 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19249 + read_4_bytes (abfd, info_ptr)));
19250 info_ptr += 4;
19251 break;
19252 case DW_FORM_ref8:
19253 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19254 + read_8_bytes (abfd, info_ptr)));
19255 info_ptr += 8;
19256 break;
19257 case DW_FORM_ref_sig8:
19258 attr->set_signature (read_8_bytes (abfd, info_ptr));
19259 info_ptr += 8;
19260 break;
19261 case DW_FORM_ref_udata:
19262 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19263 + read_unsigned_leb128 (abfd, info_ptr,
19264 &bytes_read)));
19265 info_ptr += bytes_read;
19266 break;
19267 case DW_FORM_indirect:
19268 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19269 info_ptr += bytes_read;
19270 if (form == DW_FORM_implicit_const)
19272 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19273 info_ptr += bytes_read;
19275 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19276 info_ptr);
19277 break;
19278 case DW_FORM_implicit_const:
19279 attr->set_signed (implicit_const);
19280 break;
19281 case DW_FORM_addrx:
19282 case DW_FORM_GNU_addr_index:
19283 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19284 &bytes_read));
19285 info_ptr += bytes_read;
19286 break;
19287 case DW_FORM_strx:
19288 case DW_FORM_strx1:
19289 case DW_FORM_strx2:
19290 case DW_FORM_strx3:
19291 case DW_FORM_strx4:
19292 case DW_FORM_GNU_str_index:
19294 ULONGEST str_index;
19295 if (form == DW_FORM_strx1)
19297 str_index = read_1_byte (abfd, info_ptr);
19298 info_ptr += 1;
19300 else if (form == DW_FORM_strx2)
19302 str_index = read_2_bytes (abfd, info_ptr);
19303 info_ptr += 2;
19305 else if (form == DW_FORM_strx3)
19307 str_index = read_3_bytes (abfd, info_ptr);
19308 info_ptr += 3;
19310 else if (form == DW_FORM_strx4)
19312 str_index = read_4_bytes (abfd, info_ptr);
19313 info_ptr += 4;
19315 else
19317 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19318 info_ptr += bytes_read;
19320 attr->set_unsigned_reprocess (str_index);
19322 break;
19323 default:
19324 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19325 dwarf_form_name (form),
19326 bfd_get_filename (abfd));
19329 /* Super hack. */
19330 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19331 attr->form = DW_FORM_GNU_ref_alt;
19333 /* We have seen instances where the compiler tried to emit a byte
19334 size attribute of -1 which ended up being encoded as an unsigned
19335 0xffffffff. Although 0xffffffff is technically a valid size value,
19336 an object of this size seems pretty unlikely so we can relatively
19337 safely treat these cases as if the size attribute was invalid and
19338 treat them as zero by default. */
19339 if (attr->name == DW_AT_byte_size
19340 && form == DW_FORM_data4
19341 && attr->as_unsigned () >= 0xffffffff)
19343 complaint
19344 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19345 hex_string (attr->as_unsigned ()));
19346 attr->set_unsigned (0);
19349 return info_ptr;
19352 /* Read an attribute described by an abbreviated attribute. */
19354 static const gdb_byte *
19355 read_attribute (const struct die_reader_specs *reader,
19356 struct attribute *attr, const struct attr_abbrev *abbrev,
19357 const gdb_byte *info_ptr)
19359 attr->name = abbrev->name;
19360 attr->string_is_canonical = 0;
19361 attr->requires_reprocessing = 0;
19362 return read_attribute_value (reader, attr, abbrev->form,
19363 abbrev->implicit_const, info_ptr);
19366 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19368 static const char *
19369 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19370 LONGEST str_offset)
19372 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19373 str_offset, "DW_FORM_strp");
19376 /* Return pointer to string at .debug_str offset as read from BUF.
19377 BUF is assumed to be in a compilation unit described by CU_HEADER.
19378 Return *BYTES_READ_PTR count of bytes read from BUF. */
19380 static const char *
19381 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19382 const gdb_byte *buf,
19383 const struct comp_unit_head *cu_header,
19384 unsigned int *bytes_read_ptr)
19386 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19388 return read_indirect_string_at_offset (per_objfile, str_offset);
19391 /* See read.h. */
19393 const char *
19394 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19395 unsigned int offset_size)
19397 bfd *abfd = objfile->obfd.get ();
19398 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19400 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19403 /* See read.h. */
19405 const char *
19406 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19407 const struct comp_unit_head *cu_header,
19408 unsigned int *bytes_read_ptr)
19410 bfd *abfd = objfile->obfd.get ();
19411 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19413 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19416 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19417 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19418 ADDR_SIZE is the size of addresses from the CU header. */
19420 static CORE_ADDR
19421 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19422 gdb::optional<ULONGEST> addr_base, int addr_size)
19424 struct objfile *objfile = per_objfile->objfile;
19425 bfd *abfd = objfile->obfd.get ();
19426 const gdb_byte *info_ptr;
19427 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19429 per_objfile->per_bfd->addr.read (objfile);
19430 if (per_objfile->per_bfd->addr.buffer == NULL)
19431 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19432 objfile_name (objfile));
19433 if (addr_base_or_zero + addr_index * addr_size
19434 >= per_objfile->per_bfd->addr.size)
19435 error (_("DW_FORM_addr_index pointing outside of "
19436 ".debug_addr section [in module %s]"),
19437 objfile_name (objfile));
19438 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19439 + addr_index * addr_size);
19440 if (addr_size == 4)
19441 return bfd_get_32 (abfd, info_ptr);
19442 else
19443 return bfd_get_64 (abfd, info_ptr);
19446 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19448 static CORE_ADDR
19449 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19451 return read_addr_index_1 (cu->per_objfile, addr_index,
19452 cu->addr_base, cu->header.addr_size);
19455 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19457 static CORE_ADDR
19458 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19459 unsigned int *bytes_read)
19461 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
19462 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19464 return read_addr_index (cu, addr_index);
19467 /* See read.h. */
19469 CORE_ADDR
19470 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19471 dwarf2_per_objfile *per_objfile,
19472 unsigned int addr_index)
19474 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19475 gdb::optional<ULONGEST> addr_base;
19476 int addr_size;
19478 /* We need addr_base and addr_size.
19479 If we don't have PER_CU->cu, we have to get it.
19480 Nasty, but the alternative is storing the needed info in PER_CU,
19481 which at this point doesn't seem justified: it's not clear how frequently
19482 it would get used and it would increase the size of every PER_CU.
19483 Entry points like dwarf2_per_cu_addr_size do a similar thing
19484 so we're not in uncharted territory here.
19485 Alas we need to be a bit more complicated as addr_base is contained
19486 in the DIE.
19488 We don't need to read the entire CU(/TU).
19489 We just need the header and top level die.
19491 IWBN to use the aging mechanism to let us lazily later discard the CU.
19492 For now we skip this optimization. */
19494 if (cu != NULL)
19496 addr_base = cu->addr_base;
19497 addr_size = cu->header.addr_size;
19499 else
19501 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19502 addr_base = reader.cu->addr_base;
19503 addr_size = reader.cu->header.addr_size;
19506 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19509 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19510 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19511 DWO file. */
19513 static const char *
19514 read_str_index (struct dwarf2_cu *cu,
19515 struct dwarf2_section_info *str_section,
19516 struct dwarf2_section_info *str_offsets_section,
19517 ULONGEST str_offsets_base, ULONGEST str_index,
19518 unsigned offset_size)
19520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19521 struct objfile *objfile = per_objfile->objfile;
19522 const char *objf_name = objfile_name (objfile);
19523 bfd *abfd = objfile->obfd.get ();
19524 const gdb_byte *info_ptr;
19525 ULONGEST str_offset;
19526 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19528 str_section->read (objfile);
19529 str_offsets_section->read (objfile);
19530 if (str_section->buffer == NULL)
19531 error (_("%s used without %s section"
19532 " in CU at offset %s [in module %s]"),
19533 form_name, str_section->get_name (),
19534 sect_offset_str (cu->header.sect_off), objf_name);
19535 if (str_offsets_section->buffer == NULL)
19536 error (_("%s used without %s section"
19537 " in CU at offset %s [in module %s]"),
19538 form_name, str_section->get_name (),
19539 sect_offset_str (cu->header.sect_off), objf_name);
19540 info_ptr = (str_offsets_section->buffer
19541 + str_offsets_base
19542 + str_index * offset_size);
19543 if (offset_size == 4)
19544 str_offset = bfd_get_32 (abfd, info_ptr);
19545 else
19546 str_offset = bfd_get_64 (abfd, info_ptr);
19547 if (str_offset >= str_section->size)
19548 error (_("Offset from %s pointing outside of"
19549 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19550 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19551 return (const char *) (str_section->buffer + str_offset);
19554 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19556 static const char *
19557 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19559 unsigned offset_size;
19560 ULONGEST str_offsets_base;
19561 if (reader->cu->header.version >= 5)
19563 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19564 so assume the .debug_str_offsets section is DWARF5 as well, and
19565 parse the header. FIXME: Parse the header only once. */
19566 unsigned int bytes_read = 0;
19567 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
19568 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
19570 /* Header: Initial length. */
19571 read_initial_length (abfd, p + bytes_read, &bytes_read);
19573 /* Determine offset_size based on the .debug_str_offsets header. */
19574 const bool dwarf5_is_dwarf64 = bytes_read != 4;
19575 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
19577 /* Header: Version. */
19578 unsigned version = read_2_bytes (abfd, p + bytes_read);
19579 bytes_read += 2;
19581 if (version <= 4)
19583 /* We'd like one warning here about ignoring the section, but
19584 because we parse the header more than once (see FIXME above)
19585 we'd have many warnings, so use a complaint instead, which at
19586 least has a limit. */
19587 complaint (_("Section .debug_str_offsets in %s has unsupported"
19588 " version %d, use empty string."),
19589 reader->dwo_file->dwo_name, version);
19590 return "";
19593 /* Header: Padding. */
19594 bytes_read += 2;
19596 str_offsets_base = bytes_read;
19598 else
19600 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19601 section, assume the .debug_str_offsets section is pre-DWARF5 as
19602 well, which doesn't have a header. */
19603 str_offsets_base = 0;
19605 /* Determine offset_size based on the .debug_info header. */
19606 offset_size = reader->cu->header.offset_size;
19609 return read_str_index (reader->cu,
19610 &reader->dwo_file->sections.str,
19611 &reader->dwo_file->sections.str_offsets,
19612 str_offsets_base, str_index, offset_size);
19615 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19617 static const char *
19618 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19620 struct objfile *objfile = cu->per_objfile->objfile;
19621 const char *objf_name = objfile_name (objfile);
19622 static const char form_name[] = "DW_FORM_GNU_str_index";
19623 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19625 if (!cu->str_offsets_base.has_value ())
19626 error (_("%s used in Fission stub without %s"
19627 " in CU at offset 0x%lx [in module %s]"),
19628 form_name, str_offsets_attr_name,
19629 (long) cu->header.offset_size, objf_name);
19631 return read_str_index (cu,
19632 &cu->per_objfile->per_bfd->str,
19633 &cu->per_objfile->per_bfd->str_offsets,
19634 *cu->str_offsets_base, str_index,
19635 cu->header.offset_size);
19638 /* Return the length of an LEB128 number in BUF. */
19640 static int
19641 leb128_size (const gdb_byte *buf)
19643 const gdb_byte *begin = buf;
19644 gdb_byte byte;
19646 while (1)
19648 byte = *buf++;
19649 if ((byte & 128) == 0)
19650 return buf - begin;
19654 static enum language
19655 dwarf_lang_to_enum_language (unsigned int lang)
19657 enum language language;
19659 switch (lang)
19661 case DW_LANG_C89:
19662 case DW_LANG_C99:
19663 case DW_LANG_C11:
19664 case DW_LANG_C:
19665 case DW_LANG_UPC:
19666 language = language_c;
19667 break;
19668 case DW_LANG_Java:
19669 case DW_LANG_C_plus_plus:
19670 case DW_LANG_C_plus_plus_11:
19671 case DW_LANG_C_plus_plus_14:
19672 language = language_cplus;
19673 break;
19674 case DW_LANG_D:
19675 language = language_d;
19676 break;
19677 case DW_LANG_Fortran77:
19678 case DW_LANG_Fortran90:
19679 case DW_LANG_Fortran95:
19680 case DW_LANG_Fortran03:
19681 case DW_LANG_Fortran08:
19682 language = language_fortran;
19683 break;
19684 case DW_LANG_Go:
19685 language = language_go;
19686 break;
19687 case DW_LANG_Mips_Assembler:
19688 language = language_asm;
19689 break;
19690 case DW_LANG_Ada83:
19691 case DW_LANG_Ada95:
19692 language = language_ada;
19693 break;
19694 case DW_LANG_Modula2:
19695 language = language_m2;
19696 break;
19697 case DW_LANG_Pascal83:
19698 language = language_pascal;
19699 break;
19700 case DW_LANG_ObjC:
19701 language = language_objc;
19702 break;
19703 case DW_LANG_Rust:
19704 case DW_LANG_Rust_old:
19705 language = language_rust;
19706 break;
19707 case DW_LANG_OpenCL:
19708 language = language_opencl;
19709 break;
19710 case DW_LANG_Cobol74:
19711 case DW_LANG_Cobol85:
19712 default:
19713 language = language_minimal;
19714 break;
19717 return language;
19720 /* Return the named attribute or NULL if not there. */
19722 static struct attribute *
19723 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19725 for (;;)
19727 unsigned int i;
19728 struct attribute *spec = NULL;
19730 for (i = 0; i < die->num_attrs; ++i)
19732 if (die->attrs[i].name == name)
19733 return &die->attrs[i];
19734 if (die->attrs[i].name == DW_AT_specification
19735 || die->attrs[i].name == DW_AT_abstract_origin)
19736 spec = &die->attrs[i];
19739 if (!spec)
19740 break;
19742 die = follow_die_ref (die, spec, &cu);
19745 return NULL;
19748 /* Return the string associated with a string-typed attribute, or NULL if it
19749 is either not found or is of an incorrect type. */
19751 static const char *
19752 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19754 struct attribute *attr;
19755 const char *str = NULL;
19757 attr = dwarf2_attr (die, name, cu);
19759 if (attr != NULL)
19761 str = attr->as_string ();
19762 if (str == nullptr)
19763 complaint (_("string type expected for attribute %s for "
19764 "DIE at %s in module %s"),
19765 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19766 objfile_name (cu->per_objfile->objfile));
19769 return str;
19772 /* Return the dwo name or NULL if not present. If present, it is in either
19773 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19774 static const char *
19775 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19777 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19778 if (dwo_name == nullptr)
19779 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19780 return dwo_name;
19783 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19784 and holds a non-zero value. This function should only be used for
19785 DW_FORM_flag or DW_FORM_flag_present attributes. */
19787 static int
19788 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19790 struct attribute *attr = dwarf2_attr (die, name, cu);
19792 return attr != nullptr && attr->as_boolean ();
19795 static int
19796 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19798 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19799 which value is non-zero. However, we have to be careful with
19800 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19801 (via dwarf2_flag_true_p) follows this attribute. So we may
19802 end up accidently finding a declaration attribute that belongs
19803 to a different DIE referenced by the specification attribute,
19804 even though the given DIE does not have a declaration attribute. */
19805 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19806 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19809 /* Return the die giving the specification for DIE, if there is
19810 one. *SPEC_CU is the CU containing DIE on input, and the CU
19811 containing the return value on output. If there is no
19812 specification, but there is an abstract origin, that is
19813 returned. */
19815 static struct die_info *
19816 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19818 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19819 *spec_cu);
19821 if (spec_attr == NULL)
19822 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19824 if (spec_attr == NULL)
19825 return NULL;
19826 else
19827 return follow_die_ref (die, spec_attr, spec_cu);
19830 /* A convenience function to find the proper .debug_line section for a CU. */
19832 static struct dwarf2_section_info *
19833 get_debug_line_section (struct dwarf2_cu *cu)
19835 struct dwarf2_section_info *section;
19836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19838 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19839 DWO file. */
19840 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19841 section = &cu->dwo_unit->dwo_file->sections.line;
19842 else if (cu->per_cu->is_dwz)
19844 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19846 section = &dwz->line;
19848 else
19849 section = &per_objfile->per_bfd->line;
19851 return section;
19854 /* Read the statement program header starting at OFFSET in
19855 .debug_line, or .debug_line.dwo. Return a pointer
19856 to a struct line_header, allocated using xmalloc.
19857 Returns NULL if there is a problem reading the header, e.g., if it
19858 has a version we don't understand.
19860 NOTE: the strings in the include directory and file name tables of
19861 the returned object point into the dwarf line section buffer,
19862 and must not be freed. */
19864 static line_header_up
19865 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
19866 const char *comp_dir)
19868 struct dwarf2_section_info *section;
19869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19871 section = get_debug_line_section (cu);
19872 section->read (per_objfile->objfile);
19873 if (section->buffer == NULL)
19875 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19876 complaint (_("missing .debug_line.dwo section"));
19877 else
19878 complaint (_("missing .debug_line section"));
19879 return 0;
19882 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19883 per_objfile, section, &cu->header,
19884 comp_dir);
19887 /* Subroutine of dwarf_decode_lines to simplify it.
19888 Return the file name for the given file_entry.
19889 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19890 If space for the result is malloc'd, *NAME_HOLDER will be set.
19891 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19892 equivalent to CU_INFO. */
19894 static const char *
19895 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19896 const file_and_directory &cu_info,
19897 std::string &name_holder)
19899 const char *include_name = fe.name;
19900 const char *include_name_to_compare = include_name;
19902 const char *dir_name = fe.include_dir (lh);
19904 std::string hold_compare;
19905 if (!IS_ABSOLUTE_PATH (include_name)
19906 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19908 /* Avoid creating a duplicate name for CU_INFO.
19909 We do this by comparing INCLUDE_NAME and CU_INFO.
19910 Before we do the comparison, however, we need to account
19911 for DIR_NAME and COMP_DIR.
19912 First prepend dir_name (if non-NULL). If we still don't
19913 have an absolute path prepend comp_dir (if non-NULL).
19914 However, the directory we record in the include-file's
19915 psymtab does not contain COMP_DIR (to match the
19916 corresponding symtab(s)).
19918 Example:
19920 bash$ cd /tmp
19921 bash$ gcc -g ./hello.c
19922 include_name = "hello.c"
19923 dir_name = "."
19924 DW_AT_comp_dir = comp_dir = "/tmp"
19925 DW_AT_name = "./hello.c"
19929 if (dir_name != NULL)
19931 name_holder = path_join (dir_name, include_name);
19932 include_name = name_holder.c_str ();
19933 include_name_to_compare = include_name;
19935 if (!IS_ABSOLUTE_PATH (include_name)
19936 && cu_info.get_comp_dir () != nullptr)
19938 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19939 include_name_to_compare = hold_compare.c_str ();
19943 std::string copied_name;
19944 const char *cu_filename = cu_info.get_name ();
19945 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19947 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19948 cu_filename = copied_name.c_str ();
19951 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19952 return nullptr;
19953 return include_name;
19956 /* State machine to track the state of the line number program. */
19958 class lnp_state_machine
19960 public:
19961 /* Initialize a machine state for the start of a line number
19962 program. */
19963 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19965 file_entry *current_file ()
19967 /* lh->file_names is 0-based, but the file name numbers in the
19968 statement program are 1-based. */
19969 return m_line_header->file_name_at (m_file);
19972 /* Record the line in the state machine. END_SEQUENCE is true if
19973 we're processing the end of a sequence. */
19974 void record_line (bool end_sequence);
19976 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19977 nop-out rest of the lines in this sequence. */
19978 void check_line_address (struct dwarf2_cu *cu,
19979 const gdb_byte *line_ptr,
19980 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19982 void handle_set_discriminator (unsigned int discriminator)
19984 m_discriminator = discriminator;
19985 m_line_has_non_zero_discriminator |= discriminator != 0;
19988 /* Handle DW_LNE_set_address. */
19989 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19991 m_op_index = 0;
19992 address += baseaddr;
19993 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19996 /* Handle DW_LNS_advance_pc. */
19997 void handle_advance_pc (CORE_ADDR adjust);
19999 /* Handle a special opcode. */
20000 void handle_special_opcode (unsigned char op_code);
20002 /* Handle DW_LNS_advance_line. */
20003 void handle_advance_line (int line_delta)
20005 advance_line (line_delta);
20008 /* Handle DW_LNS_set_file. */
20009 void handle_set_file (file_name_index file);
20011 /* Handle DW_LNS_negate_stmt. */
20012 void handle_negate_stmt ()
20014 m_flags ^= LEF_IS_STMT;
20017 /* Handle DW_LNS_const_add_pc. */
20018 void handle_const_add_pc ();
20020 /* Handle DW_LNS_fixed_advance_pc. */
20021 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20023 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20024 m_op_index = 0;
20027 /* Handle DW_LNS_copy. */
20028 void handle_copy ()
20030 record_line (false);
20031 m_discriminator = 0;
20032 m_flags &= ~LEF_PROLOGUE_END;
20035 /* Handle DW_LNE_end_sequence. */
20036 void handle_end_sequence ()
20038 m_currently_recording_lines = true;
20041 /* Handle DW_LNS_set_prologue_end. */
20042 void handle_set_prologue_end ()
20044 m_flags |= LEF_PROLOGUE_END;
20047 private:
20048 /* Advance the line by LINE_DELTA. */
20049 void advance_line (int line_delta)
20051 m_line += line_delta;
20053 if (line_delta != 0)
20054 m_line_has_non_zero_discriminator = m_discriminator != 0;
20057 struct dwarf2_cu *m_cu;
20059 gdbarch *m_gdbarch;
20061 /* The line number header. */
20062 line_header *m_line_header;
20064 /* These are part of the standard DWARF line number state machine,
20065 and initialized according to the DWARF spec. */
20067 unsigned char m_op_index = 0;
20068 /* The line table index of the current file. */
20069 file_name_index m_file = 1;
20070 unsigned int m_line = 1;
20072 /* These are initialized in the constructor. */
20074 CORE_ADDR m_address;
20075 linetable_entry_flags m_flags;
20076 unsigned int m_discriminator;
20078 /* Additional bits of state we need to track. */
20080 /* The last file that we called dwarf2_start_subfile for.
20081 This is only used for TLLs. */
20082 unsigned int m_last_file = 0;
20083 /* The last file a line number was recorded for. */
20084 struct subfile *m_last_subfile = NULL;
20086 /* The address of the last line entry. */
20087 CORE_ADDR m_last_address;
20089 /* Set to true when a previous line at the same address (using
20090 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
20091 when a line entry at a new address (m_address different to
20092 m_last_address) is processed. */
20093 bool m_stmt_at_address = false;
20095 /* When true, record the lines we decode. */
20096 bool m_currently_recording_lines = false;
20098 /* The last line number that was recorded, used to coalesce
20099 consecutive entries for the same line. This can happen, for
20100 example, when discriminators are present. PR 17276. */
20101 unsigned int m_last_line = 0;
20102 bool m_line_has_non_zero_discriminator = false;
20105 void
20106 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20108 CORE_ADDR addr_adj = (((m_op_index + adjust)
20109 / m_line_header->maximum_ops_per_instruction)
20110 * m_line_header->minimum_instruction_length);
20111 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20112 m_op_index = ((m_op_index + adjust)
20113 % m_line_header->maximum_ops_per_instruction);
20116 void
20117 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20119 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20120 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20121 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20122 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20123 / m_line_header->maximum_ops_per_instruction)
20124 * m_line_header->minimum_instruction_length);
20125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20126 m_op_index = ((m_op_index + adj_opcode_d)
20127 % m_line_header->maximum_ops_per_instruction);
20129 int line_delta = m_line_header->line_base + adj_opcode_r;
20130 advance_line (line_delta);
20131 record_line (false);
20132 m_discriminator = 0;
20133 m_flags &= ~LEF_PROLOGUE_END;
20136 void
20137 lnp_state_machine::handle_set_file (file_name_index file)
20139 m_file = file;
20141 const file_entry *fe = current_file ();
20142 if (fe == NULL)
20143 dwarf2_debug_line_missing_file_complaint ();
20144 else
20146 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20147 m_line_has_non_zero_discriminator = m_discriminator != 0;
20148 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
20152 void
20153 lnp_state_machine::handle_const_add_pc ()
20155 CORE_ADDR adjust
20156 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20158 CORE_ADDR addr_adj
20159 = (((m_op_index + adjust)
20160 / m_line_header->maximum_ops_per_instruction)
20161 * m_line_header->minimum_instruction_length);
20163 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20164 m_op_index = ((m_op_index + adjust)
20165 % m_line_header->maximum_ops_per_instruction);
20168 /* Return non-zero if we should add LINE to the line number table.
20169 LINE is the line to add, LAST_LINE is the last line that was added,
20170 LAST_SUBFILE is the subfile for LAST_LINE.
20171 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20172 had a non-zero discriminator.
20174 We have to be careful in the presence of discriminators.
20175 E.g., for this line:
20177 for (i = 0; i < 100000; i++);
20179 clang can emit four line number entries for that one line,
20180 each with a different discriminator.
20181 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20183 However, we want gdb to coalesce all four entries into one.
20184 Otherwise the user could stepi into the middle of the line and
20185 gdb would get confused about whether the pc really was in the
20186 middle of the line.
20188 Things are further complicated by the fact that two consecutive
20189 line number entries for the same line is a heuristic used by gcc
20190 to denote the end of the prologue. So we can't just discard duplicate
20191 entries, we have to be selective about it. The heuristic we use is
20192 that we only collapse consecutive entries for the same line if at least
20193 one of those entries has a non-zero discriminator. PR 17276.
20195 Note: Addresses in the line number state machine can never go backwards
20196 within one sequence, thus this coalescing is ok. */
20198 static int
20199 dwarf_record_line_p (struct dwarf2_cu *cu,
20200 unsigned int line, unsigned int last_line,
20201 int line_has_non_zero_discriminator,
20202 struct subfile *last_subfile)
20204 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20205 return 1;
20206 if (line != last_line)
20207 return 1;
20208 /* Same line for the same file that we've seen already.
20209 As a last check, for pr 17276, only record the line if the line
20210 has never had a non-zero discriminator. */
20211 if (!line_has_non_zero_discriminator)
20212 return 1;
20213 return 0;
20216 /* Use the CU's builder to record line number LINE beginning at
20217 address ADDRESS in the line table of subfile SUBFILE. */
20219 static void
20220 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20221 unsigned int line, CORE_ADDR address,
20222 linetable_entry_flags flags,
20223 struct dwarf2_cu *cu)
20225 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20227 if (dwarf_line_debug)
20229 gdb_printf (gdb_stdlog,
20230 "Recording line %u, file %s, address %s\n",
20231 line, lbasename (subfile->name.c_str ()),
20232 paddress (gdbarch, address));
20235 if (cu != nullptr)
20236 cu->get_builder ()->record_line (subfile, line, addr, flags);
20239 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20240 Mark the end of a set of line number records.
20241 The arguments are the same as for dwarf_record_line_1.
20242 If SUBFILE is NULL the request is ignored. */
20244 static void
20245 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20246 CORE_ADDR address, struct dwarf2_cu *cu)
20248 if (subfile == NULL)
20249 return;
20251 if (dwarf_line_debug)
20253 gdb_printf (gdb_stdlog,
20254 "Finishing current line, file %s, address %s\n",
20255 lbasename (subfile->name.c_str ()),
20256 paddress (gdbarch, address));
20259 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20262 void
20263 lnp_state_machine::record_line (bool end_sequence)
20265 if (dwarf_line_debug)
20267 gdb_printf (gdb_stdlog,
20268 "Processing actual line %u: file %u,"
20269 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20270 m_line, m_file,
20271 paddress (m_gdbarch, m_address),
20272 (m_flags & LEF_IS_STMT) != 0,
20273 (m_flags & LEF_PROLOGUE_END) != 0,
20274 m_discriminator,
20275 (end_sequence ? "\t(end sequence)" : ""));
20278 file_entry *fe = current_file ();
20280 if (fe == NULL)
20281 dwarf2_debug_line_missing_file_complaint ();
20282 /* For now we ignore lines not starting on an instruction boundary.
20283 But not when processing end_sequence for compatibility with the
20284 previous version of the code. */
20285 else if (m_op_index == 0 || end_sequence)
20287 /* When we switch files we insert an end maker in the first file,
20288 switch to the second file and add a new line entry. The
20289 problem is that the end marker inserted in the first file will
20290 discard any previous line entries at the same address. If the
20291 line entries in the first file are marked as is-stmt, while
20292 the new line in the second file is non-stmt, then this means
20293 the end marker will discard is-stmt lines so we can have a
20294 non-stmt line. This means that there are less addresses at
20295 which the user can insert a breakpoint.
20297 To improve this we track the last address in m_last_address,
20298 and whether we have seen an is-stmt at this address. Then
20299 when switching files, if we have seen a stmt at the current
20300 address, and we are switching to create a non-stmt line, then
20301 discard the new line. */
20302 bool file_changed
20303 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20304 bool ignore_this_line
20305 = ((file_changed && !end_sequence && m_last_address == m_address
20306 && ((m_flags & LEF_IS_STMT) == 0)
20307 && m_stmt_at_address)
20308 || (!end_sequence && m_line == 0));
20310 if ((file_changed && !ignore_this_line) || end_sequence)
20312 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20313 m_currently_recording_lines ? m_cu : nullptr);
20316 if (!end_sequence && !ignore_this_line)
20318 linetable_entry_flags lte_flags = m_flags;
20319 if (producer_is_codewarrior (m_cu))
20320 lte_flags |= LEF_IS_STMT;
20322 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20323 m_line_has_non_zero_discriminator,
20324 m_last_subfile))
20326 buildsym_compunit *builder = m_cu->get_builder ();
20327 dwarf_record_line_1 (m_gdbarch,
20328 builder->get_current_subfile (),
20329 m_line, m_address, lte_flags,
20330 m_currently_recording_lines ? m_cu : nullptr);
20332 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20333 m_last_line = m_line;
20337 /* Track whether we have seen any IS_STMT true at m_address in case we
20338 have multiple line table entries all at m_address. */
20339 if (m_last_address != m_address)
20341 m_stmt_at_address = false;
20342 m_last_address = m_address;
20344 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20347 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20348 line_header *lh)
20350 m_cu = cu;
20351 m_gdbarch = arch;
20352 m_line_header = lh;
20354 m_currently_recording_lines = true;
20356 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20357 was a line entry for it so that the backend has a chance to adjust it
20358 and also record it in case it needs it. This is currently used by MIPS
20359 code, cf. `mips_adjust_dwarf2_line'. */
20360 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20361 m_flags = 0;
20362 if (lh->default_is_stmt)
20363 m_flags |= LEF_IS_STMT;
20364 m_discriminator = 0;
20366 m_last_address = m_address;
20367 m_stmt_at_address = false;
20370 void
20371 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20372 const gdb_byte *line_ptr,
20373 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20375 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20376 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20377 located at 0x0. In this case, additionally check that if
20378 ADDRESS < UNRELOCATED_LOWPC. */
20380 if ((address == 0 && address < unrelocated_lowpc)
20381 || address == (CORE_ADDR) -1)
20383 /* This line table is for a function which has been
20384 GCd by the linker. Ignore it. PR gdb/12528 */
20386 struct objfile *objfile = cu->per_objfile->objfile;
20387 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20389 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20390 line_offset, objfile_name (objfile));
20391 m_currently_recording_lines = false;
20392 /* Note: m_currently_recording_lines is left as false until we see
20393 DW_LNE_end_sequence. */
20397 /* Subroutine of dwarf_decode_lines to simplify it.
20398 Process the line number information in LH. */
20400 static void
20401 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20402 CORE_ADDR lowpc)
20404 const gdb_byte *line_ptr, *extended_end;
20405 const gdb_byte *line_end;
20406 unsigned int bytes_read, extended_len;
20407 unsigned char op_code, extended_op;
20408 CORE_ADDR baseaddr;
20409 struct objfile *objfile = cu->per_objfile->objfile;
20410 bfd *abfd = objfile->obfd.get ();
20411 struct gdbarch *gdbarch = objfile->arch ();
20413 baseaddr = objfile->text_section_offset ();
20415 line_ptr = lh->statement_program_start;
20416 line_end = lh->statement_program_end;
20418 /* Read the statement sequences until there's nothing left. */
20419 while (line_ptr < line_end)
20421 /* The DWARF line number program state machine. Reset the state
20422 machine at the start of each sequence. */
20423 lnp_state_machine state_machine (cu, gdbarch, lh);
20424 bool end_sequence = false;
20426 /* Start a subfile for the current file of the state
20427 machine. */
20428 const file_entry *fe = state_machine.current_file ();
20430 if (fe != NULL)
20431 dwarf2_start_subfile (cu, *fe, *lh);
20433 /* Decode the table. */
20434 while (line_ptr < line_end && !end_sequence)
20436 op_code = read_1_byte (abfd, line_ptr);
20437 line_ptr += 1;
20439 if (op_code >= lh->opcode_base)
20441 /* Special opcode. */
20442 state_machine.handle_special_opcode (op_code);
20444 else switch (op_code)
20446 case DW_LNS_extended_op:
20447 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20448 &bytes_read);
20449 line_ptr += bytes_read;
20450 extended_end = line_ptr + extended_len;
20451 extended_op = read_1_byte (abfd, line_ptr);
20452 line_ptr += 1;
20453 if (DW_LNE_lo_user <= extended_op
20454 && extended_op <= DW_LNE_hi_user)
20456 /* Vendor extension, ignore. */
20457 line_ptr = extended_end;
20458 break;
20460 switch (extended_op)
20462 case DW_LNE_end_sequence:
20463 state_machine.handle_end_sequence ();
20464 end_sequence = true;
20465 break;
20466 case DW_LNE_set_address:
20468 CORE_ADDR address
20469 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20470 line_ptr += bytes_read;
20472 state_machine.check_line_address (cu, line_ptr,
20473 lowpc - baseaddr, address);
20474 state_machine.handle_set_address (baseaddr, address);
20476 break;
20477 case DW_LNE_define_file:
20479 const char *cur_file;
20480 unsigned int mod_time, length;
20481 dir_index dindex;
20483 cur_file = read_direct_string (abfd, line_ptr,
20484 &bytes_read);
20485 line_ptr += bytes_read;
20486 dindex = (dir_index)
20487 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20488 line_ptr += bytes_read;
20489 mod_time =
20490 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20491 line_ptr += bytes_read;
20492 length =
20493 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20494 line_ptr += bytes_read;
20495 lh->add_file_name (cur_file, dindex, mod_time, length);
20497 break;
20498 case DW_LNE_set_discriminator:
20500 /* The discriminator is not interesting to the
20501 debugger; just ignore it. We still need to
20502 check its value though:
20503 if there are consecutive entries for the same
20504 (non-prologue) line we want to coalesce them.
20505 PR 17276. */
20506 unsigned int discr
20507 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20508 line_ptr += bytes_read;
20510 state_machine.handle_set_discriminator (discr);
20512 break;
20513 default:
20514 complaint (_("mangled .debug_line section"));
20515 return;
20517 /* Make sure that we parsed the extended op correctly. If e.g.
20518 we expected a different address size than the producer used,
20519 we may have read the wrong number of bytes. */
20520 if (line_ptr != extended_end)
20522 complaint (_("mangled .debug_line section"));
20523 return;
20525 break;
20526 case DW_LNS_copy:
20527 state_machine.handle_copy ();
20528 break;
20529 case DW_LNS_advance_pc:
20531 CORE_ADDR adjust
20532 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20533 line_ptr += bytes_read;
20535 state_machine.handle_advance_pc (adjust);
20537 break;
20538 case DW_LNS_advance_line:
20540 int line_delta
20541 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20542 line_ptr += bytes_read;
20544 state_machine.handle_advance_line (line_delta);
20546 break;
20547 case DW_LNS_set_file:
20549 file_name_index file
20550 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20551 &bytes_read);
20552 line_ptr += bytes_read;
20554 state_machine.handle_set_file (file);
20556 break;
20557 case DW_LNS_set_column:
20558 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20559 line_ptr += bytes_read;
20560 break;
20561 case DW_LNS_negate_stmt:
20562 state_machine.handle_negate_stmt ();
20563 break;
20564 case DW_LNS_set_basic_block:
20565 break;
20566 /* Add to the address register of the state machine the
20567 address increment value corresponding to special opcode
20568 255. I.e., this value is scaled by the minimum
20569 instruction length since special opcode 255 would have
20570 scaled the increment. */
20571 case DW_LNS_const_add_pc:
20572 state_machine.handle_const_add_pc ();
20573 break;
20574 case DW_LNS_fixed_advance_pc:
20576 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20577 line_ptr += 2;
20579 state_machine.handle_fixed_advance_pc (addr_adj);
20581 break;
20582 case DW_LNS_set_prologue_end:
20583 state_machine.handle_set_prologue_end ();
20584 break;
20585 default:
20587 /* Unknown standard opcode, ignore it. */
20588 int i;
20590 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20592 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20593 line_ptr += bytes_read;
20599 if (!end_sequence)
20600 dwarf2_debug_line_missing_end_sequence_complaint ();
20602 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20603 in which case we still finish recording the last line). */
20604 state_machine.record_line (true);
20608 /* Decode the Line Number Program (LNP) for the given line_header
20609 structure and CU. The actual information extracted and the type
20610 of structures created from the LNP depends on the value of PST.
20612 FND holds the CU file name and directory, if known.
20613 It is used for relative paths in the line table.
20615 NOTE: It is important that psymtabs have the same file name (via
20616 strcmp) as the corresponding symtab. Since the directory is not
20617 used in the name of the symtab we don't use it in the name of the
20618 psymtabs we create. E.g. expand_line_sal requires this when
20619 finding psymtabs to expand. A good testcase for this is
20620 mb-inline.exp.
20622 LOWPC is the lowest address in CU (or 0 if not known).
20624 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20625 for its PC<->lines mapping information. Otherwise only the filename
20626 table is read in. */
20628 static void
20629 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20630 CORE_ADDR lowpc, int decode_mapping)
20632 if (decode_mapping)
20633 dwarf_decode_lines_1 (lh, cu, lowpc);
20635 /* Make sure a symtab is created for every file, even files
20636 which contain only variables (i.e. no code with associated
20637 line numbers). */
20638 buildsym_compunit *builder = cu->get_builder ();
20639 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20641 for (auto &fe : lh->file_names ())
20643 dwarf2_start_subfile (cu, fe, *lh);
20644 subfile *sf = builder->get_current_subfile ();
20646 if (sf->symtab == nullptr)
20647 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
20648 sf->name_for_id.c_str ());
20650 fe.symtab = sf->symtab;
20654 /* Start a subfile for DWARF. FILENAME is the name of the file and
20655 DIRNAME the name of the source directory which contains FILENAME
20656 or NULL if not known.
20657 This routine tries to keep line numbers from identical absolute and
20658 relative file names in a common subfile.
20660 Using the `list' example from the GDB testsuite, which resides in
20661 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20662 of /srcdir/list0.c yields the following debugging information for list0.c:
20664 DW_AT_name: /srcdir/list0.c
20665 DW_AT_comp_dir: /compdir
20666 files.files[0].name: list0.h
20667 files.files[0].dir: /srcdir
20668 files.files[1].name: list0.c
20669 files.files[1].dir: /srcdir
20671 The line number information for list0.c has to end up in a single
20672 subfile, so that `break /srcdir/list0.c:1' works as expected.
20673 start_subfile will ensure that this happens provided that we pass the
20674 concatenation of files.files[1].dir and files.files[1].name as the
20675 subfile's name. */
20677 static void
20678 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
20679 const line_header &lh)
20681 std::string filename_holder;
20682 const char *filename = fe.name;
20683 const char *dirname = lh.include_dir_at (fe.d_index);
20685 /* In order not to lose the line information directory,
20686 we concatenate it to the filename when it makes sense.
20687 Note that the Dwarf3 standard says (speaking of filenames in line
20688 information): ``The directory index is ignored for file names
20689 that represent full path names''. Thus ignoring dirname in the
20690 `else' branch below isn't an issue. */
20692 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20694 filename_holder = path_join (dirname, filename);
20695 filename = filename_holder.c_str ();
20698 std::string filename_for_id = lh.file_file_name (fe);
20699 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
20702 static void
20703 var_decode_location (struct attribute *attr, struct symbol *sym,
20704 struct dwarf2_cu *cu)
20706 struct objfile *objfile = cu->per_objfile->objfile;
20707 struct comp_unit_head *cu_header = &cu->header;
20709 /* NOTE drow/2003-01-30: There used to be a comment and some special
20710 code here to turn a symbol with DW_AT_external and a
20711 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20712 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20713 with some versions of binutils) where shared libraries could have
20714 relocations against symbols in their debug information - the
20715 minimal symbol would have the right address, but the debug info
20716 would not. It's no longer necessary, because we will explicitly
20717 apply relocations when we read in the debug information now. */
20719 /* A DW_AT_location attribute with no contents indicates that a
20720 variable has been optimized away. */
20721 if (attr->form_is_block () && attr->as_block ()->size == 0)
20723 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20724 return;
20727 /* Handle one degenerate form of location expression specially, to
20728 preserve GDB's previous behavior when section offsets are
20729 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20730 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20732 if (attr->form_is_block ())
20734 struct dwarf_block *block = attr->as_block ();
20736 if ((block->data[0] == DW_OP_addr
20737 && block->size == 1 + cu_header->addr_size)
20738 || ((block->data[0] == DW_OP_GNU_addr_index
20739 || block->data[0] == DW_OP_addrx)
20740 && (block->size
20741 == 1 + leb128_size (&block->data[1]))))
20743 unsigned int dummy;
20745 if (block->data[0] == DW_OP_addr)
20746 sym->set_value_address
20747 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
20748 &dummy));
20749 else
20750 sym->set_value_address
20751 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20752 sym->set_aclass_index (LOC_STATIC);
20753 fixup_symbol_section (sym, objfile);
20754 sym->set_value_address
20755 (sym->value_address ()
20756 + objfile->section_offsets[sym->section_index ()]);
20757 return;
20761 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20762 expression evaluator, and use LOC_COMPUTED only when necessary
20763 (i.e. when the value of a register or memory location is
20764 referenced, or a thread-local block, etc.). Then again, it might
20765 not be worthwhile. I'm assuming that it isn't unless performance
20766 or memory numbers show me otherwise. */
20768 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20770 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20771 cu->has_loclist = true;
20774 /* Given a pointer to a DWARF information entry, figure out if we need
20775 to make a symbol table entry for it, and if so, create a new entry
20776 and return a pointer to it.
20777 If TYPE is NULL, determine symbol type from the die, otherwise
20778 used the passed type.
20779 If SPACE is not NULL, use it to hold the new symbol. If it is
20780 NULL, allocate a new symbol on the objfile's obstack. */
20782 static struct symbol *
20783 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20784 struct symbol *space)
20786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20787 struct objfile *objfile = per_objfile->objfile;
20788 struct gdbarch *gdbarch = objfile->arch ();
20789 struct symbol *sym = NULL;
20790 const char *name;
20791 struct attribute *attr = NULL;
20792 struct attribute *attr2 = NULL;
20793 CORE_ADDR baseaddr;
20794 struct pending **list_to_add = NULL;
20796 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20798 baseaddr = objfile->text_section_offset ();
20800 name = dwarf2_name (die, cu);
20801 if (name == nullptr && (die->tag == DW_TAG_subprogram
20802 || die->tag == DW_TAG_inlined_subroutine
20803 || die->tag == DW_TAG_entry_point))
20804 name = dw2_linkage_name (die, cu);
20806 if (name)
20808 int suppress_add = 0;
20810 if (space)
20811 sym = space;
20812 else
20813 sym = new (&objfile->objfile_obstack) symbol;
20814 OBJSTAT (objfile, n_syms++);
20816 /* Cache this symbol's name and the name's demangled form (if any). */
20817 sym->set_language (cu->lang (), &objfile->objfile_obstack);
20818 /* Fortran does not have mangling standard and the mangling does differ
20819 between gfortran, iFort etc. */
20820 const char *physname
20821 = (cu->lang () == language_fortran
20822 ? dwarf2_full_name (name, die, cu)
20823 : dwarf2_physname (name, die, cu));
20824 const char *linkagename = dw2_linkage_name (die, cu);
20826 if (linkagename == nullptr || cu->lang () == language_ada)
20827 sym->set_linkage_name (physname);
20828 else
20830 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20831 sym->set_linkage_name (linkagename);
20834 /* Handle DW_AT_artificial. */
20835 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20836 if (attr != nullptr)
20837 sym->set_is_artificial (attr->as_boolean ());
20839 /* Default assumptions.
20840 Use the passed type or decode it from the die. */
20841 sym->set_domain (VAR_DOMAIN);
20842 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20843 if (type != NULL)
20844 sym->set_type (type);
20845 else
20846 sym->set_type (die_type (die, cu));
20847 attr = dwarf2_attr (die,
20848 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20849 cu);
20850 if (attr != nullptr)
20851 sym->set_line (attr->constant_value (0));
20853 attr = dwarf2_attr (die,
20854 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20855 cu);
20856 if (attr != nullptr && attr->is_nonnegative ())
20858 file_name_index file_index
20859 = (file_name_index) attr->as_nonnegative ();
20860 struct file_entry *fe;
20862 if (cu->line_header != NULL)
20863 fe = cu->line_header->file_name_at (file_index);
20864 else
20865 fe = NULL;
20867 if (fe == NULL)
20868 complaint (_("file index out of range"));
20869 else
20870 sym->set_symtab (fe->symtab);
20873 switch (die->tag)
20875 case DW_TAG_label:
20876 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20877 if (attr != nullptr)
20879 CORE_ADDR addr;
20881 addr = attr->as_address ();
20882 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20883 sym->set_value_address (addr);
20884 sym->set_aclass_index (LOC_LABEL);
20886 else
20887 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20888 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20889 sym->set_domain (LABEL_DOMAIN);
20890 add_symbol_to_list (sym, cu->list_in_scope);
20891 break;
20892 case DW_TAG_subprogram:
20893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20894 finish_block. */
20895 sym->set_aclass_index (LOC_BLOCK);
20896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20897 if ((attr2 != nullptr && attr2->as_boolean ())
20898 || cu->lang () == language_ada
20899 || cu->lang () == language_fortran)
20901 /* Subprograms marked external are stored as a global symbol.
20902 Ada and Fortran subprograms, whether marked external or
20903 not, are always stored as a global symbol, because we want
20904 to be able to access them globally. For instance, we want
20905 to be able to break on a nested subprogram without having
20906 to specify the context. */
20907 list_to_add = cu->get_builder ()->get_global_symbols ();
20909 else
20911 list_to_add = cu->list_in_scope;
20913 break;
20914 case DW_TAG_inlined_subroutine:
20915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20916 finish_block. */
20917 sym->set_aclass_index (LOC_BLOCK);
20918 sym->set_is_inlined (1);
20919 list_to_add = cu->list_in_scope;
20920 break;
20921 case DW_TAG_template_value_param:
20922 suppress_add = 1;
20923 /* Fall through. */
20924 case DW_TAG_constant:
20925 case DW_TAG_variable:
20926 case DW_TAG_member:
20927 /* Compilation with minimal debug info may result in
20928 variables with missing type entries. Change the
20929 misleading `void' type to something sensible. */
20930 if (sym->type ()->code () == TYPE_CODE_VOID)
20931 sym->set_type (objfile_type (objfile)->builtin_int);
20933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20934 /* In the case of DW_TAG_member, we should only be called for
20935 static const members. */
20936 if (die->tag == DW_TAG_member)
20938 /* dwarf2_add_field uses die_is_declaration,
20939 so we do the same. */
20940 gdb_assert (die_is_declaration (die, cu));
20941 gdb_assert (attr);
20943 if (attr != nullptr)
20945 dwarf2_const_value (attr, sym, cu);
20946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20947 if (!suppress_add)
20949 if (attr2 != nullptr && attr2->as_boolean ())
20950 list_to_add = cu->get_builder ()->get_global_symbols ();
20951 else
20952 list_to_add = cu->list_in_scope;
20954 break;
20956 attr = dwarf2_attr (die, DW_AT_location, cu);
20957 if (attr != nullptr)
20959 var_decode_location (attr, sym, cu);
20960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20962 /* Fortran explicitly imports any global symbols to the local
20963 scope by DW_TAG_common_block. */
20964 if (cu->lang () == language_fortran && die->parent
20965 && die->parent->tag == DW_TAG_common_block)
20966 attr2 = NULL;
20968 if (sym->aclass () == LOC_STATIC
20969 && sym->value_address () == 0
20970 && !per_objfile->per_bfd->has_section_at_zero)
20972 /* When a static variable is eliminated by the linker,
20973 the corresponding debug information is not stripped
20974 out, but the variable address is set to null;
20975 do not add such variables into symbol table. */
20977 else if (attr2 != nullptr && attr2->as_boolean ())
20979 if (sym->aclass () == LOC_STATIC
20980 && (objfile->flags & OBJF_MAINLINE) == 0
20981 && per_objfile->per_bfd->can_copy)
20983 /* A global static variable might be subject to
20984 copy relocation. We first check for a local
20985 minsym, though, because maybe the symbol was
20986 marked hidden, in which case this would not
20987 apply. */
20988 bound_minimal_symbol found
20989 = (lookup_minimal_symbol_linkage
20990 (sym->linkage_name (), objfile));
20991 if (found.minsym != nullptr)
20992 sym->maybe_copied = 1;
20995 /* A variable with DW_AT_external is never static,
20996 but it may be block-scoped. */
20997 list_to_add
20998 = ((cu->list_in_scope
20999 == cu->get_builder ()->get_file_symbols ())
21000 ? cu->get_builder ()->get_global_symbols ()
21001 : cu->list_in_scope);
21003 else
21004 list_to_add = cu->list_in_scope;
21006 else
21008 /* We do not know the address of this symbol.
21009 If it is an external symbol and we have type information
21010 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21011 The address of the variable will then be determined from
21012 the minimal symbol table whenever the variable is
21013 referenced. */
21014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21016 /* Fortran explicitly imports any global symbols to the local
21017 scope by DW_TAG_common_block. */
21018 if (cu->lang () == language_fortran && die->parent
21019 && die->parent->tag == DW_TAG_common_block)
21021 /* SYMBOL_CLASS doesn't matter here because
21022 read_common_block is going to reset it. */
21023 if (!suppress_add)
21024 list_to_add = cu->list_in_scope;
21026 else if (attr2 != nullptr && attr2->as_boolean ()
21027 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21029 /* A variable with DW_AT_external is never static, but it
21030 may be block-scoped. */
21031 list_to_add
21032 = ((cu->list_in_scope
21033 == cu->get_builder ()->get_file_symbols ())
21034 ? cu->get_builder ()->get_global_symbols ()
21035 : cu->list_in_scope);
21037 sym->set_aclass_index (LOC_UNRESOLVED);
21039 else if (!die_is_declaration (die, cu))
21041 /* Use the default LOC_OPTIMIZED_OUT class. */
21042 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
21043 if (!suppress_add)
21044 list_to_add = cu->list_in_scope;
21047 break;
21048 case DW_TAG_formal_parameter:
21050 /* If we are inside a function, mark this as an argument. If
21051 not, we might be looking at an argument to an inlined function
21052 when we do not have enough information to show inlined frames;
21053 pretend it's a local variable in that case so that the user can
21054 still see it. */
21055 struct context_stack *curr
21056 = cu->get_builder ()->get_current_context_stack ();
21057 if (curr != nullptr && curr->name != nullptr)
21058 sym->set_is_argument (1);
21059 attr = dwarf2_attr (die, DW_AT_location, cu);
21060 if (attr != nullptr)
21062 var_decode_location (attr, sym, cu);
21064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21065 if (attr != nullptr)
21067 dwarf2_const_value (attr, sym, cu);
21070 list_to_add = cu->list_in_scope;
21072 break;
21073 case DW_TAG_unspecified_parameters:
21074 /* From varargs functions; gdb doesn't seem to have any
21075 interest in this information, so just ignore it for now.
21076 (FIXME?) */
21077 break;
21078 case DW_TAG_template_type_param:
21079 suppress_add = 1;
21080 /* Fall through. */
21081 case DW_TAG_class_type:
21082 case DW_TAG_interface_type:
21083 case DW_TAG_structure_type:
21084 case DW_TAG_union_type:
21085 case DW_TAG_set_type:
21086 case DW_TAG_enumeration_type:
21087 case DW_TAG_namelist:
21088 if (die->tag == DW_TAG_namelist)
21090 sym->set_aclass_index (LOC_STATIC);
21091 sym->set_domain (VAR_DOMAIN);
21093 else
21095 sym->set_aclass_index (LOC_TYPEDEF);
21096 sym->set_domain (STRUCT_DOMAIN);
21099 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21100 really ever be static objects: otherwise, if you try
21101 to, say, break of a class's method and you're in a file
21102 which doesn't mention that class, it won't work unless
21103 the check for all static symbols in lookup_symbol_aux
21104 saves you. See the OtherFileClass tests in
21105 gdb.c++/namespace.exp. */
21107 if (!suppress_add)
21109 buildsym_compunit *builder = cu->get_builder ();
21110 list_to_add
21111 = (cu->list_in_scope == builder->get_file_symbols ()
21112 && cu->lang () == language_cplus
21113 ? builder->get_global_symbols ()
21114 : cu->list_in_scope);
21116 /* The semantics of C++ state that "struct foo {
21117 ... }" also defines a typedef for "foo". */
21118 if (cu->lang () == language_cplus
21119 || cu->lang () == language_ada
21120 || cu->lang () == language_d
21121 || cu->lang () == language_rust)
21123 /* The symbol's name is already allocated along
21124 with this objfile, so we don't need to
21125 duplicate it for the type. */
21126 if (sym->type ()->name () == 0)
21127 sym->type ()->set_name (sym->search_name ());
21131 break;
21132 case DW_TAG_typedef:
21133 sym->set_aclass_index (LOC_TYPEDEF);
21134 sym->set_domain (VAR_DOMAIN);
21135 list_to_add = cu->list_in_scope;
21136 break;
21137 case DW_TAG_array_type:
21138 case DW_TAG_base_type:
21139 case DW_TAG_subrange_type:
21140 case DW_TAG_generic_subrange:
21141 case DW_TAG_unspecified_type:
21142 sym->set_aclass_index (LOC_TYPEDEF);
21143 sym->set_domain (VAR_DOMAIN);
21144 list_to_add = cu->list_in_scope;
21145 break;
21146 case DW_TAG_enumerator:
21147 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21148 if (attr != nullptr)
21150 dwarf2_const_value (attr, sym, cu);
21153 /* NOTE: carlton/2003-11-10: See comment above in the
21154 DW_TAG_class_type, etc. block. */
21156 list_to_add
21157 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21158 && cu->lang () == language_cplus
21159 ? cu->get_builder ()->get_global_symbols ()
21160 : cu->list_in_scope);
21162 break;
21163 case DW_TAG_imported_declaration:
21164 case DW_TAG_namespace:
21165 sym->set_aclass_index (LOC_TYPEDEF);
21166 list_to_add = cu->get_builder ()->get_global_symbols ();
21167 break;
21168 case DW_TAG_module:
21169 sym->set_aclass_index (LOC_TYPEDEF);
21170 sym->set_domain (MODULE_DOMAIN);
21171 list_to_add = cu->get_builder ()->get_global_symbols ();
21172 break;
21173 case DW_TAG_common_block:
21174 sym->set_aclass_index (LOC_COMMON_BLOCK);
21175 sym->set_domain (COMMON_BLOCK_DOMAIN);
21176 add_symbol_to_list (sym, cu->list_in_scope);
21177 break;
21178 default:
21179 /* Not a tag we recognize. Hopefully we aren't processing
21180 trash data, but since we must specifically ignore things
21181 we don't recognize, there is nothing else we should do at
21182 this point. */
21183 complaint (_("unsupported tag: '%s'"),
21184 dwarf_tag_name (die->tag));
21185 break;
21188 if (suppress_add)
21190 sym->hash_next = objfile->template_symbols;
21191 objfile->template_symbols = sym;
21192 list_to_add = NULL;
21195 if (list_to_add != NULL)
21196 add_symbol_to_list (sym, list_to_add);
21198 /* For the benefit of old versions of GCC, check for anonymous
21199 namespaces based on the demangled name. */
21200 if (!cu->processing_has_namespace_info
21201 && cu->lang () == language_cplus)
21202 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21204 return (sym);
21207 /* Given an attr with a DW_FORM_dataN value in host byte order,
21208 zero-extend it as appropriate for the symbol's type. The DWARF
21209 standard (v4) is not entirely clear about the meaning of using
21210 DW_FORM_dataN for a constant with a signed type, where the type is
21211 wider than the data. The conclusion of a discussion on the DWARF
21212 list was that this is unspecified. We choose to always zero-extend
21213 because that is the interpretation long in use by GCC. */
21215 static gdb_byte *
21216 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21217 struct dwarf2_cu *cu, LONGEST *value, int bits)
21219 struct objfile *objfile = cu->per_objfile->objfile;
21220 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
21221 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21222 LONGEST l = attr->constant_value (0);
21224 if (bits < sizeof (*value) * 8)
21226 l &= ((LONGEST) 1 << bits) - 1;
21227 *value = l;
21229 else if (bits == sizeof (*value) * 8)
21230 *value = l;
21231 else
21233 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21234 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21235 return bytes;
21238 return NULL;
21241 /* Read a constant value from an attribute. Either set *VALUE, or if
21242 the value does not fit in *VALUE, set *BYTES - either already
21243 allocated on the objfile obstack, or newly allocated on OBSTACK,
21244 or, set *BATON, if we translated the constant to a location
21245 expression. */
21247 static void
21248 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21249 const char *name, struct obstack *obstack,
21250 struct dwarf2_cu *cu,
21251 LONGEST *value, const gdb_byte **bytes,
21252 struct dwarf2_locexpr_baton **baton)
21254 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21255 struct objfile *objfile = per_objfile->objfile;
21256 struct comp_unit_head *cu_header = &cu->header;
21257 struct dwarf_block *blk;
21258 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
21259 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21261 *value = 0;
21262 *bytes = NULL;
21263 *baton = NULL;
21265 switch (attr->form)
21267 case DW_FORM_addr:
21268 case DW_FORM_addrx:
21269 case DW_FORM_GNU_addr_index:
21271 gdb_byte *data;
21273 if (type->length () != cu_header->addr_size)
21274 dwarf2_const_value_length_mismatch_complaint (name,
21275 cu_header->addr_size,
21276 type->length ());
21277 /* Symbols of this form are reasonably rare, so we just
21278 piggyback on the existing location code rather than writing
21279 a new implementation of symbol_computed_ops. */
21280 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21281 (*baton)->per_objfile = per_objfile;
21282 (*baton)->per_cu = cu->per_cu;
21283 gdb_assert ((*baton)->per_cu);
21285 (*baton)->size = 2 + cu_header->addr_size;
21286 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21287 (*baton)->data = data;
21289 data[0] = DW_OP_addr;
21290 store_unsigned_integer (&data[1], cu_header->addr_size,
21291 byte_order, attr->as_address ());
21292 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21294 break;
21295 case DW_FORM_string:
21296 case DW_FORM_strp:
21297 case DW_FORM_strx:
21298 case DW_FORM_GNU_str_index:
21299 case DW_FORM_GNU_strp_alt:
21300 /* The string is already allocated on the objfile obstack, point
21301 directly to it. */
21302 *bytes = (const gdb_byte *) attr->as_string ();
21303 break;
21304 case DW_FORM_block1:
21305 case DW_FORM_block2:
21306 case DW_FORM_block4:
21307 case DW_FORM_block:
21308 case DW_FORM_exprloc:
21309 case DW_FORM_data16:
21310 blk = attr->as_block ();
21311 if (type->length () != blk->size)
21312 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21313 type->length ());
21314 *bytes = blk->data;
21315 break;
21317 /* The DW_AT_const_value attributes are supposed to carry the
21318 symbol's value "represented as it would be on the target
21319 architecture." By the time we get here, it's already been
21320 converted to host endianness, so we just need to sign- or
21321 zero-extend it as appropriate. */
21322 case DW_FORM_data1:
21323 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21324 break;
21325 case DW_FORM_data2:
21326 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21327 break;
21328 case DW_FORM_data4:
21329 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21330 break;
21331 case DW_FORM_data8:
21332 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21333 break;
21335 case DW_FORM_sdata:
21336 case DW_FORM_implicit_const:
21337 *value = attr->as_signed ();
21338 break;
21340 case DW_FORM_udata:
21341 *value = attr->as_unsigned ();
21342 break;
21344 default:
21345 complaint (_("unsupported const value attribute form: '%s'"),
21346 dwarf_form_name (attr->form));
21347 *value = 0;
21348 break;
21353 /* Copy constant value from an attribute to a symbol. */
21355 static void
21356 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21357 struct dwarf2_cu *cu)
21359 struct objfile *objfile = cu->per_objfile->objfile;
21360 LONGEST value;
21361 const gdb_byte *bytes;
21362 struct dwarf2_locexpr_baton *baton;
21364 dwarf2_const_value_attr (attr, sym->type (),
21365 sym->print_name (),
21366 &objfile->objfile_obstack, cu,
21367 &value, &bytes, &baton);
21369 if (baton != NULL)
21371 SYMBOL_LOCATION_BATON (sym) = baton;
21372 sym->set_aclass_index (dwarf2_locexpr_index);
21374 else if (bytes != NULL)
21376 sym->set_value_bytes (bytes);
21377 sym->set_aclass_index (LOC_CONST_BYTES);
21379 else
21381 sym->set_value_longest (value);
21382 sym->set_aclass_index (LOC_CONST);
21386 /* Return the type of the die in question using its DW_AT_type attribute. */
21388 static struct type *
21389 die_type (struct die_info *die, struct dwarf2_cu *cu)
21391 struct attribute *type_attr;
21393 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21394 if (!type_attr)
21396 struct objfile *objfile = cu->per_objfile->objfile;
21397 /* A missing DW_AT_type represents a void type. */
21398 return objfile_type (objfile)->builtin_void;
21401 return lookup_die_type (die, type_attr, cu);
21404 /* True iff CU's producer generates GNAT Ada auxiliary information
21405 that allows to find parallel types through that information instead
21406 of having to do expensive parallel lookups by type name. */
21408 static int
21409 need_gnat_info (struct dwarf2_cu *cu)
21411 /* Assume that the Ada compiler was GNAT, which always produces
21412 the auxiliary information. */
21413 return (cu->lang () == language_ada);
21416 /* Return the auxiliary type of the die in question using its
21417 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21418 attribute is not present. */
21420 static struct type *
21421 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21423 struct attribute *type_attr;
21425 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21426 if (!type_attr)
21427 return NULL;
21429 return lookup_die_type (die, type_attr, cu);
21432 /* If DIE has a descriptive_type attribute, then set the TYPE's
21433 descriptive type accordingly. */
21435 static void
21436 set_descriptive_type (struct type *type, struct die_info *die,
21437 struct dwarf2_cu *cu)
21439 struct type *descriptive_type = die_descriptive_type (die, cu);
21441 if (descriptive_type)
21443 ALLOCATE_GNAT_AUX_TYPE (type);
21444 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21448 /* Return the containing type of the die in question using its
21449 DW_AT_containing_type attribute. */
21451 static struct type *
21452 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21454 struct attribute *type_attr;
21455 struct objfile *objfile = cu->per_objfile->objfile;
21457 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21458 if (!type_attr)
21459 error (_("Dwarf Error: Problem turning containing type into gdb type "
21460 "[in module %s]"), objfile_name (objfile));
21462 return lookup_die_type (die, type_attr, cu);
21465 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21467 static struct type *
21468 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21470 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21471 struct objfile *objfile = per_objfile->objfile;
21472 char *saved;
21474 std::string message
21475 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21476 objfile_name (objfile),
21477 sect_offset_str (cu->header.sect_off),
21478 sect_offset_str (die->sect_off));
21479 saved = obstack_strdup (&objfile->objfile_obstack, message);
21481 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21484 /* Look up the type of DIE in CU using its type attribute ATTR.
21485 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21486 DW_AT_containing_type.
21487 If there is no type substitute an error marker. */
21489 static struct type *
21490 lookup_die_type (struct die_info *die, const struct attribute *attr,
21491 struct dwarf2_cu *cu)
21493 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21494 struct objfile *objfile = per_objfile->objfile;
21495 struct type *this_type;
21497 gdb_assert (attr->name == DW_AT_type
21498 || attr->name == DW_AT_GNAT_descriptive_type
21499 || attr->name == DW_AT_containing_type);
21501 /* First see if we have it cached. */
21503 if (attr->form == DW_FORM_GNU_ref_alt)
21505 struct dwarf2_per_cu_data *per_cu;
21506 sect_offset sect_off = attr->get_ref_die_offset ();
21508 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21509 per_objfile->per_bfd);
21510 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21512 else if (attr->form_is_ref ())
21514 sect_offset sect_off = attr->get_ref_die_offset ();
21516 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21518 else if (attr->form == DW_FORM_ref_sig8)
21520 ULONGEST signature = attr->as_signature ();
21522 return get_signatured_type (die, signature, cu);
21524 else
21526 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21527 " at %s [in module %s]"),
21528 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21529 objfile_name (objfile));
21530 return build_error_marker_type (cu, die);
21533 /* If not cached we need to read it in. */
21535 if (this_type == NULL)
21537 struct die_info *type_die = NULL;
21538 struct dwarf2_cu *type_cu = cu;
21540 if (attr->form_is_ref ())
21541 type_die = follow_die_ref (die, attr, &type_cu);
21542 if (type_die == NULL)
21543 return build_error_marker_type (cu, die);
21544 /* If we find the type now, it's probably because the type came
21545 from an inter-CU reference and the type's CU got expanded before
21546 ours. */
21547 this_type = read_type_die (type_die, type_cu);
21550 /* If we still don't have a type use an error marker. */
21552 if (this_type == NULL)
21553 return build_error_marker_type (cu, die);
21555 return this_type;
21558 /* Return the type in DIE, CU.
21559 Returns NULL for invalid types.
21561 This first does a lookup in die_type_hash,
21562 and only reads the die in if necessary.
21564 NOTE: This can be called when reading in partial or full symbols. */
21566 static struct type *
21567 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21569 struct type *this_type;
21571 this_type = get_die_type (die, cu);
21572 if (this_type)
21573 return this_type;
21575 return read_type_die_1 (die, cu);
21578 /* Read the type in DIE, CU.
21579 Returns NULL for invalid types. */
21581 static struct type *
21582 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21584 struct type *this_type = NULL;
21586 switch (die->tag)
21588 case DW_TAG_class_type:
21589 case DW_TAG_interface_type:
21590 case DW_TAG_structure_type:
21591 case DW_TAG_union_type:
21592 this_type = read_structure_type (die, cu);
21593 break;
21594 case DW_TAG_enumeration_type:
21595 this_type = read_enumeration_type (die, cu);
21596 break;
21597 case DW_TAG_subprogram:
21598 case DW_TAG_subroutine_type:
21599 case DW_TAG_inlined_subroutine:
21600 this_type = read_subroutine_type (die, cu);
21601 break;
21602 case DW_TAG_array_type:
21603 this_type = read_array_type (die, cu);
21604 break;
21605 case DW_TAG_set_type:
21606 this_type = read_set_type (die, cu);
21607 break;
21608 case DW_TAG_pointer_type:
21609 this_type = read_tag_pointer_type (die, cu);
21610 break;
21611 case DW_TAG_ptr_to_member_type:
21612 this_type = read_tag_ptr_to_member_type (die, cu);
21613 break;
21614 case DW_TAG_reference_type:
21615 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21616 break;
21617 case DW_TAG_rvalue_reference_type:
21618 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21619 break;
21620 case DW_TAG_const_type:
21621 this_type = read_tag_const_type (die, cu);
21622 break;
21623 case DW_TAG_volatile_type:
21624 this_type = read_tag_volatile_type (die, cu);
21625 break;
21626 case DW_TAG_restrict_type:
21627 this_type = read_tag_restrict_type (die, cu);
21628 break;
21629 case DW_TAG_string_type:
21630 this_type = read_tag_string_type (die, cu);
21631 break;
21632 case DW_TAG_typedef:
21633 this_type = read_typedef (die, cu);
21634 break;
21635 case DW_TAG_generic_subrange:
21636 case DW_TAG_subrange_type:
21637 this_type = read_subrange_type (die, cu);
21638 break;
21639 case DW_TAG_base_type:
21640 this_type = read_base_type (die, cu);
21641 break;
21642 case DW_TAG_unspecified_type:
21643 this_type = read_unspecified_type (die, cu);
21644 break;
21645 case DW_TAG_namespace:
21646 this_type = read_namespace_type (die, cu);
21647 break;
21648 case DW_TAG_module:
21649 this_type = read_module_type (die, cu);
21650 break;
21651 case DW_TAG_atomic_type:
21652 this_type = read_tag_atomic_type (die, cu);
21653 break;
21654 default:
21655 complaint (_("unexpected tag in read_type_die: '%s'"),
21656 dwarf_tag_name (die->tag));
21657 break;
21660 return this_type;
21663 /* See if we can figure out if the class lives in a namespace. We do
21664 this by looking for a member function; its demangled name will
21665 contain namespace info, if there is any.
21666 Return the computed name or NULL.
21667 Space for the result is allocated on the objfile's obstack.
21668 This is the full-die version of guess_partial_die_structure_name.
21669 In this case we know DIE has no useful parent. */
21671 static const char *
21672 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21674 struct die_info *spec_die;
21675 struct dwarf2_cu *spec_cu;
21676 struct die_info *child;
21677 struct objfile *objfile = cu->per_objfile->objfile;
21679 spec_cu = cu;
21680 spec_die = die_specification (die, &spec_cu);
21681 if (spec_die != NULL)
21683 die = spec_die;
21684 cu = spec_cu;
21687 for (child = die->child;
21688 child != NULL;
21689 child = child->sibling)
21691 if (child->tag == DW_TAG_subprogram)
21693 const char *linkage_name = dw2_linkage_name (child, cu);
21695 if (linkage_name != NULL)
21697 gdb::unique_xmalloc_ptr<char> actual_name
21698 (cu->language_defn->class_name_from_physname (linkage_name));
21699 const char *name = NULL;
21701 if (actual_name != NULL)
21703 const char *die_name = dwarf2_name (die, cu);
21705 if (die_name != NULL
21706 && strcmp (die_name, actual_name.get ()) != 0)
21708 /* Strip off the class name from the full name.
21709 We want the prefix. */
21710 int die_name_len = strlen (die_name);
21711 int actual_name_len = strlen (actual_name.get ());
21712 const char *ptr = actual_name.get ();
21714 /* Test for '::' as a sanity check. */
21715 if (actual_name_len > die_name_len + 2
21716 && ptr[actual_name_len - die_name_len - 1] == ':')
21717 name = obstack_strndup (
21718 &objfile->per_bfd->storage_obstack,
21719 ptr, actual_name_len - die_name_len - 2);
21722 return name;
21727 return NULL;
21730 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21731 prefix part in such case. See
21732 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21734 static const char *
21735 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21737 struct attribute *attr;
21738 const char *base;
21740 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21741 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21742 return NULL;
21744 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21745 return NULL;
21747 attr = dw2_linkage_name_attr (die, cu);
21748 const char *attr_name = attr->as_string ();
21749 if (attr == NULL || attr_name == NULL)
21750 return NULL;
21752 /* dwarf2_name had to be already called. */
21753 gdb_assert (attr->canonical_string_p ());
21755 /* Strip the base name, keep any leading namespaces/classes. */
21756 base = strrchr (attr_name, ':');
21757 if (base == NULL || base == attr_name || base[-1] != ':')
21758 return "";
21760 struct objfile *objfile = cu->per_objfile->objfile;
21761 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21762 attr_name,
21763 &base[-1] - attr_name);
21766 /* Return the name of the namespace/class that DIE is defined within,
21767 or "" if we can't tell. The caller should not xfree the result.
21769 For example, if we're within the method foo() in the following
21770 code:
21772 namespace N {
21773 class C {
21774 void foo () {
21779 then determine_prefix on foo's die will return "N::C". */
21781 static const char *
21782 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21784 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21785 struct die_info *parent, *spec_die;
21786 struct dwarf2_cu *spec_cu;
21787 struct type *parent_type;
21788 const char *retval;
21790 if (cu->lang () != language_cplus
21791 && cu->lang () != language_fortran
21792 && cu->lang () != language_d
21793 && cu->lang () != language_rust)
21794 return "";
21796 retval = anonymous_struct_prefix (die, cu);
21797 if (retval)
21798 return retval;
21800 /* We have to be careful in the presence of DW_AT_specification.
21801 For example, with GCC 3.4, given the code
21803 namespace N {
21804 void foo() {
21805 // Definition of N::foo.
21809 then we'll have a tree of DIEs like this:
21811 1: DW_TAG_compile_unit
21812 2: DW_TAG_namespace // N
21813 3: DW_TAG_subprogram // declaration of N::foo
21814 4: DW_TAG_subprogram // definition of N::foo
21815 DW_AT_specification // refers to die #3
21817 Thus, when processing die #4, we have to pretend that we're in
21818 the context of its DW_AT_specification, namely the contex of die
21819 #3. */
21820 spec_cu = cu;
21821 spec_die = die_specification (die, &spec_cu);
21822 if (spec_die == NULL)
21823 parent = die->parent;
21824 else
21826 parent = spec_die->parent;
21827 cu = spec_cu;
21830 if (parent == NULL)
21831 return "";
21832 else if (parent->building_fullname)
21834 const char *name;
21835 const char *parent_name;
21837 /* It has been seen on RealView 2.2 built binaries,
21838 DW_TAG_template_type_param types actually _defined_ as
21839 children of the parent class:
21841 enum E {};
21842 template class <class Enum> Class{};
21843 Class<enum E> class_e;
21845 1: DW_TAG_class_type (Class)
21846 2: DW_TAG_enumeration_type (E)
21847 3: DW_TAG_enumerator (enum1:0)
21848 3: DW_TAG_enumerator (enum2:1)
21850 2: DW_TAG_template_type_param
21851 DW_AT_type DW_FORM_ref_udata (E)
21853 Besides being broken debug info, it can put GDB into an
21854 infinite loop. Consider:
21856 When we're building the full name for Class<E>, we'll start
21857 at Class, and go look over its template type parameters,
21858 finding E. We'll then try to build the full name of E, and
21859 reach here. We're now trying to build the full name of E,
21860 and look over the parent DIE for containing scope. In the
21861 broken case, if we followed the parent DIE of E, we'd again
21862 find Class, and once again go look at its template type
21863 arguments, etc., etc. Simply don't consider such parent die
21864 as source-level parent of this die (it can't be, the language
21865 doesn't allow it), and break the loop here. */
21866 name = dwarf2_name (die, cu);
21867 parent_name = dwarf2_name (parent, cu);
21868 complaint (_("template param type '%s' defined within parent '%s'"),
21869 name ? name : "<unknown>",
21870 parent_name ? parent_name : "<unknown>");
21871 return "";
21873 else
21874 switch (parent->tag)
21876 case DW_TAG_namespace:
21877 parent_type = read_type_die (parent, cu);
21878 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21879 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21880 Work around this problem here. */
21881 if (cu->lang () == language_cplus
21882 && strcmp (parent_type->name (), "::") == 0)
21883 return "";
21884 /* We give a name to even anonymous namespaces. */
21885 return parent_type->name ();
21886 case DW_TAG_class_type:
21887 case DW_TAG_interface_type:
21888 case DW_TAG_structure_type:
21889 case DW_TAG_union_type:
21890 case DW_TAG_module:
21891 parent_type = read_type_die (parent, cu);
21892 if (parent_type->name () != NULL)
21893 return parent_type->name ();
21894 else
21895 /* An anonymous structure is only allowed non-static data
21896 members; no typedefs, no member functions, et cetera.
21897 So it does not need a prefix. */
21898 return "";
21899 case DW_TAG_compile_unit:
21900 case DW_TAG_partial_unit:
21901 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21902 if (cu->lang () == language_cplus
21903 && !per_objfile->per_bfd->types.empty ()
21904 && die->child != NULL
21905 && (die->tag == DW_TAG_class_type
21906 || die->tag == DW_TAG_structure_type
21907 || die->tag == DW_TAG_union_type))
21909 const char *name = guess_full_die_structure_name (die, cu);
21910 if (name != NULL)
21911 return name;
21913 return "";
21914 case DW_TAG_subprogram:
21915 /* Nested subroutines in Fortran get a prefix with the name
21916 of the parent's subroutine. */
21917 if (cu->lang () == language_fortran)
21919 if ((die->tag == DW_TAG_subprogram)
21920 && (dwarf2_name (parent, cu) != NULL))
21921 return dwarf2_name (parent, cu);
21923 return "";
21924 case DW_TAG_enumeration_type:
21925 parent_type = read_type_die (parent, cu);
21926 if (parent_type->is_declared_class ())
21928 if (parent_type->name () != NULL)
21929 return parent_type->name ();
21930 return "";
21932 /* Fall through. */
21933 default:
21934 return determine_prefix (parent, cu);
21938 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21939 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21940 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21941 an obconcat, otherwise allocate storage for the result. The CU argument is
21942 used to determine the language and hence, the appropriate separator. */
21944 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21946 static char *
21947 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21948 int physname, struct dwarf2_cu *cu)
21950 const char *lead = "";
21951 const char *sep;
21953 if (suffix == NULL || suffix[0] == '\0'
21954 || prefix == NULL || prefix[0] == '\0')
21955 sep = "";
21956 else if (cu->lang () == language_d)
21958 /* For D, the 'main' function could be defined in any module, but it
21959 should never be prefixed. */
21960 if (strcmp (suffix, "D main") == 0)
21962 prefix = "";
21963 sep = "";
21965 else
21966 sep = ".";
21968 else if (cu->lang () == language_fortran && physname)
21970 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21971 DW_AT_MIPS_linkage_name is preferred and used instead. */
21973 lead = "__";
21974 sep = "_MOD_";
21976 else
21977 sep = "::";
21979 if (prefix == NULL)
21980 prefix = "";
21981 if (suffix == NULL)
21982 suffix = "";
21984 if (obs == NULL)
21986 char *retval
21987 = ((char *)
21988 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21990 strcpy (retval, lead);
21991 strcat (retval, prefix);
21992 strcat (retval, sep);
21993 strcat (retval, suffix);
21994 return retval;
21996 else
21998 /* We have an obstack. */
21999 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22003 /* Return a generic name for a DW_TAG_template_type_param or
22004 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
22005 per parent, so each function/class/struct template will have their own set
22006 of template parameters named <unnnamed0>, <unnamed1>, ... where the
22007 enumeration starts at 0 and represents the position of the template tag in
22008 the list of unnamed template tags for this parent, counting both, type and
22009 value tags. */
22011 static const char *
22012 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
22014 if (die->parent == nullptr)
22015 return nullptr;
22017 /* Count the parent types unnamed template type and value children until, we
22018 arrive at our entry. */
22019 size_t nth_unnamed = 0;
22021 die_info *child = die->parent->child;
22022 while (child != die)
22024 gdb_assert (child != nullptr);
22025 if (child->tag == DW_TAG_template_type_param
22026 || child->tag == DW_TAG_template_value_param)
22028 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
22029 ++nth_unnamed;
22031 child = child->sibling;
22034 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
22035 return cu->per_objfile->objfile->intern (name_str.c_str ());
22038 /* Get name of a die, return NULL if not found. */
22040 static const char *
22041 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22042 struct objfile *objfile)
22044 if (name == nullptr)
22045 return name;
22047 if (cu->lang () == language_cplus)
22049 gdb::unique_xmalloc_ptr<char> canon_name
22050 = cp_canonicalize_string (name);
22052 if (canon_name != nullptr)
22053 name = objfile->intern (canon_name.get ());
22055 else if (cu->lang () == language_c)
22057 gdb::unique_xmalloc_ptr<char> canon_name
22058 = c_canonicalize_name (name);
22060 if (canon_name != nullptr)
22061 name = objfile->intern (canon_name.get ());
22064 return name;
22067 /* Get name of a die, return NULL if not found.
22068 Anonymous namespaces are converted to their magic string. */
22070 static const char *
22071 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22073 struct attribute *attr;
22074 struct objfile *objfile = cu->per_objfile->objfile;
22076 attr = dwarf2_attr (die, DW_AT_name, cu);
22077 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22078 if (attr_name == nullptr
22079 && die->tag != DW_TAG_namespace
22080 && die->tag != DW_TAG_class_type
22081 && die->tag != DW_TAG_interface_type
22082 && die->tag != DW_TAG_structure_type
22083 && die->tag != DW_TAG_namelist
22084 && die->tag != DW_TAG_union_type
22085 && die->tag != DW_TAG_template_type_param
22086 && die->tag != DW_TAG_template_value_param)
22087 return NULL;
22089 switch (die->tag)
22091 /* A member's name should not be canonicalized. This is a bit
22092 of a hack, in that normally it should not be possible to run
22093 into this situation; however, the dw2-unusual-field-names.exp
22094 test creates custom DWARF that does. */
22095 case DW_TAG_member:
22096 case DW_TAG_compile_unit:
22097 case DW_TAG_partial_unit:
22098 /* Compilation units have a DW_AT_name that is a filename, not
22099 a source language identifier. */
22100 case DW_TAG_enumeration_type:
22101 case DW_TAG_enumerator:
22102 /* These tags always have simple identifiers already; no need
22103 to canonicalize them. */
22104 return attr_name;
22106 case DW_TAG_namespace:
22107 if (attr_name != nullptr)
22108 return attr_name;
22109 return CP_ANONYMOUS_NAMESPACE_STR;
22111 /* DWARF does not actually require template tags to have a name. */
22112 case DW_TAG_template_type_param:
22113 case DW_TAG_template_value_param:
22114 if (attr_name == nullptr)
22115 return unnamed_template_tag_name (die, cu);
22116 /* FALLTHROUGH. */
22117 case DW_TAG_class_type:
22118 case DW_TAG_interface_type:
22119 case DW_TAG_structure_type:
22120 case DW_TAG_union_type:
22121 case DW_TAG_namelist:
22122 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22123 structures or unions. These were of the form "._%d" in GCC 4.1,
22124 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22125 and GCC 4.4. We work around this problem by ignoring these. */
22126 if (attr_name != nullptr
22127 && (startswith (attr_name, "._")
22128 || startswith (attr_name, "<anonymous")))
22129 return NULL;
22131 /* GCC might emit a nameless typedef that has a linkage name. See
22132 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22133 if (!attr || attr_name == NULL)
22135 attr = dw2_linkage_name_attr (die, cu);
22136 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22137 if (attr == NULL || attr_name == NULL)
22138 return NULL;
22140 /* Avoid demangling attr_name the second time on a second
22141 call for the same DIE. */
22142 if (!attr->canonical_string_p ())
22144 gdb::unique_xmalloc_ptr<char> demangled
22145 (gdb_demangle (attr_name, DMGL_TYPES));
22146 if (demangled == nullptr)
22147 return nullptr;
22149 attr->set_string_canonical (objfile->intern (demangled.get ()));
22150 attr_name = attr->as_string ();
22153 /* Strip any leading namespaces/classes, keep only the
22154 base name. DW_AT_name for named DIEs does not
22155 contain the prefixes. */
22156 const char *base = strrchr (attr_name, ':');
22157 if (base && base > attr_name && base[-1] == ':')
22158 return &base[1];
22159 else
22160 return attr_name;
22162 break;
22164 default:
22165 break;
22168 if (!attr->canonical_string_p ())
22169 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22170 objfile));
22171 return attr->as_string ();
22174 /* Return the die that this die in an extension of, or NULL if there
22175 is none. *EXT_CU is the CU containing DIE on input, and the CU
22176 containing the return value on output. */
22178 static struct die_info *
22179 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22181 struct attribute *attr;
22183 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22184 if (attr == NULL)
22185 return NULL;
22187 return follow_die_ref (die, attr, ext_cu);
22190 static void
22191 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22193 unsigned int i;
22195 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
22196 indent, "",
22197 dwarf_tag_name (die->tag), die->abbrev,
22198 sect_offset_str (die->sect_off));
22200 if (die->parent != NULL)
22201 gdb_printf (f, "%*s parent at offset: %s\n",
22202 indent, "",
22203 sect_offset_str (die->parent->sect_off));
22205 gdb_printf (f, "%*s has children: %s\n",
22206 indent, "",
22207 dwarf_bool_name (die->child != NULL));
22209 gdb_printf (f, "%*s attributes:\n", indent, "");
22211 for (i = 0; i < die->num_attrs; ++i)
22213 gdb_printf (f, "%*s %s (%s) ",
22214 indent, "",
22215 dwarf_attr_name (die->attrs[i].name),
22216 dwarf_form_name (die->attrs[i].form));
22218 switch (die->attrs[i].form)
22220 case DW_FORM_addr:
22221 case DW_FORM_addrx:
22222 case DW_FORM_GNU_addr_index:
22223 gdb_printf (f, "address: ");
22224 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
22225 break;
22226 case DW_FORM_block2:
22227 case DW_FORM_block4:
22228 case DW_FORM_block:
22229 case DW_FORM_block1:
22230 gdb_printf (f, "block: size %s",
22231 pulongest (die->attrs[i].as_block ()->size));
22232 break;
22233 case DW_FORM_exprloc:
22234 gdb_printf (f, "expression: size %s",
22235 pulongest (die->attrs[i].as_block ()->size));
22236 break;
22237 case DW_FORM_data16:
22238 gdb_printf (f, "constant of 16 bytes");
22239 break;
22240 case DW_FORM_ref_addr:
22241 gdb_printf (f, "ref address: ");
22242 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22243 break;
22244 case DW_FORM_GNU_ref_alt:
22245 gdb_printf (f, "alt ref address: ");
22246 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22247 break;
22248 case DW_FORM_ref1:
22249 case DW_FORM_ref2:
22250 case DW_FORM_ref4:
22251 case DW_FORM_ref8:
22252 case DW_FORM_ref_udata:
22253 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
22254 (long) (die->attrs[i].as_unsigned ()));
22255 break;
22256 case DW_FORM_data1:
22257 case DW_FORM_data2:
22258 case DW_FORM_data4:
22259 case DW_FORM_data8:
22260 case DW_FORM_udata:
22261 gdb_printf (f, "constant: %s",
22262 pulongest (die->attrs[i].as_unsigned ()));
22263 break;
22264 case DW_FORM_sec_offset:
22265 gdb_printf (f, "section offset: %s",
22266 pulongest (die->attrs[i].as_unsigned ()));
22267 break;
22268 case DW_FORM_ref_sig8:
22269 gdb_printf (f, "signature: %s",
22270 hex_string (die->attrs[i].as_signature ()));
22271 break;
22272 case DW_FORM_string:
22273 case DW_FORM_strp:
22274 case DW_FORM_line_strp:
22275 case DW_FORM_strx:
22276 case DW_FORM_GNU_str_index:
22277 case DW_FORM_GNU_strp_alt:
22278 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22279 die->attrs[i].as_string ()
22280 ? die->attrs[i].as_string () : "",
22281 die->attrs[i].canonical_string_p () ? "is" : "not");
22282 break;
22283 case DW_FORM_flag:
22284 if (die->attrs[i].as_boolean ())
22285 gdb_printf (f, "flag: TRUE");
22286 else
22287 gdb_printf (f, "flag: FALSE");
22288 break;
22289 case DW_FORM_flag_present:
22290 gdb_printf (f, "flag: TRUE");
22291 break;
22292 case DW_FORM_indirect:
22293 /* The reader will have reduced the indirect form to
22294 the "base form" so this form should not occur. */
22295 gdb_printf (f,
22296 "unexpected attribute form: DW_FORM_indirect");
22297 break;
22298 case DW_FORM_sdata:
22299 case DW_FORM_implicit_const:
22300 gdb_printf (f, "constant: %s",
22301 plongest (die->attrs[i].as_signed ()));
22302 break;
22303 default:
22304 gdb_printf (f, "unsupported attribute form: %d.",
22305 die->attrs[i].form);
22306 break;
22308 gdb_printf (f, "\n");
22312 static void
22313 dump_die_for_error (struct die_info *die)
22315 dump_die_shallow (gdb_stderr, 0, die);
22318 static void
22319 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22321 int indent = level * 4;
22323 gdb_assert (die != NULL);
22325 if (level >= max_level)
22326 return;
22328 dump_die_shallow (f, indent, die);
22330 if (die->child != NULL)
22332 gdb_printf (f, "%*s Children:", indent, "");
22333 if (level + 1 < max_level)
22335 gdb_printf (f, "\n");
22336 dump_die_1 (f, level + 1, max_level, die->child);
22338 else
22340 gdb_printf (f,
22341 " [not printed, max nesting level reached]\n");
22345 if (die->sibling != NULL && level > 0)
22347 dump_die_1 (f, level, max_level, die->sibling);
22351 /* This is called from the pdie macro in gdbinit.in.
22352 It's not static so gcc will keep a copy callable from gdb. */
22354 void
22355 dump_die (struct die_info *die, int max_level)
22357 dump_die_1 (gdb_stdlog, 0, max_level, die);
22360 static void
22361 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22363 void **slot;
22365 slot = htab_find_slot_with_hash (cu->die_hash, die,
22366 to_underlying (die->sect_off),
22367 INSERT);
22369 *slot = die;
22372 /* Follow reference or signature attribute ATTR of SRC_DIE.
22373 On entry *REF_CU is the CU of SRC_DIE.
22374 On exit *REF_CU is the CU of the result. */
22376 static struct die_info *
22377 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22378 struct dwarf2_cu **ref_cu)
22380 struct die_info *die;
22382 if (attr->form_is_ref ())
22383 die = follow_die_ref (src_die, attr, ref_cu);
22384 else if (attr->form == DW_FORM_ref_sig8)
22385 die = follow_die_sig (src_die, attr, ref_cu);
22386 else
22388 dump_die_for_error (src_die);
22389 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22390 objfile_name ((*ref_cu)->per_objfile->objfile));
22393 return die;
22396 /* Follow reference OFFSET.
22397 On entry *REF_CU is the CU of the source die referencing OFFSET.
22398 On exit *REF_CU is the CU of the result.
22399 Returns NULL if OFFSET is invalid. */
22401 static struct die_info *
22402 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22403 struct dwarf2_cu **ref_cu)
22405 struct die_info temp_die;
22406 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22409 gdb_assert (cu->per_cu != NULL);
22411 target_cu = cu;
22413 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22414 "source CU contains target offset: %d",
22415 sect_offset_str (cu->per_cu->sect_off),
22416 sect_offset_str (sect_off),
22417 cu->header.offset_in_cu_p (sect_off));
22419 if (cu->per_cu->is_debug_types)
22421 /* .debug_types CUs cannot reference anything outside their CU.
22422 If they need to, they have to reference a signatured type via
22423 DW_FORM_ref_sig8. */
22424 if (!cu->header.offset_in_cu_p (sect_off))
22425 return NULL;
22427 else if (offset_in_dwz != cu->per_cu->is_dwz
22428 || !cu->header.offset_in_cu_p (sect_off))
22430 struct dwarf2_per_cu_data *per_cu;
22432 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22433 per_objfile->per_bfd);
22435 dwarf_read_debug_printf_v ("target CU offset: %s, "
22436 "target CU DIEs loaded: %d",
22437 sect_offset_str (per_cu->sect_off),
22438 per_objfile->get_cu (per_cu) != nullptr);
22440 /* If necessary, add it to the queue and load its DIEs.
22442 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22443 it doesn't mean they are currently loaded. Since we require them
22444 to be loaded, we must check for ourselves. */
22445 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
22446 || per_objfile->get_cu (per_cu) == nullptr)
22447 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22448 false, cu->lang ());
22450 target_cu = per_objfile->get_cu (per_cu);
22451 gdb_assert (target_cu != nullptr);
22453 else if (cu->dies == NULL)
22455 /* We're loading full DIEs during partial symbol reading. */
22456 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22457 language_minimal);
22460 *ref_cu = target_cu;
22461 temp_die.sect_off = sect_off;
22463 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22464 &temp_die,
22465 to_underlying (sect_off));
22468 /* Follow reference attribute ATTR of SRC_DIE.
22469 On entry *REF_CU is the CU of SRC_DIE.
22470 On exit *REF_CU is the CU of the result. */
22472 static struct die_info *
22473 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22474 struct dwarf2_cu **ref_cu)
22476 sect_offset sect_off = attr->get_ref_die_offset ();
22477 struct dwarf2_cu *cu = *ref_cu;
22478 struct die_info *die;
22480 die = follow_die_offset (sect_off,
22481 (attr->form == DW_FORM_GNU_ref_alt
22482 || cu->per_cu->is_dwz),
22483 ref_cu);
22484 if (!die)
22485 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22486 "at %s [in module %s]"),
22487 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22488 objfile_name (cu->per_objfile->objfile));
22490 return die;
22493 /* See read.h. */
22495 struct dwarf2_locexpr_baton
22496 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22497 dwarf2_per_cu_data *per_cu,
22498 dwarf2_per_objfile *per_objfile,
22499 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22500 bool resolve_abstract_p)
22502 struct die_info *die;
22503 struct attribute *attr;
22504 struct dwarf2_locexpr_baton retval;
22505 struct objfile *objfile = per_objfile->objfile;
22507 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22508 if (cu == nullptr)
22509 cu = load_cu (per_cu, per_objfile, false);
22511 if (cu == nullptr)
22513 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22514 Instead just throw an error, not much else we can do. */
22515 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22516 sect_offset_str (sect_off), objfile_name (objfile));
22519 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22520 if (!die)
22521 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22522 sect_offset_str (sect_off), objfile_name (objfile));
22524 attr = dwarf2_attr (die, DW_AT_location, cu);
22525 if (!attr && resolve_abstract_p
22526 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22527 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22529 CORE_ADDR pc = get_frame_pc ();
22530 CORE_ADDR baseaddr = objfile->text_section_offset ();
22531 struct gdbarch *gdbarch = objfile->arch ();
22533 for (const auto &cand_off
22534 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22536 struct dwarf2_cu *cand_cu = cu;
22537 struct die_info *cand
22538 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22539 if (!cand
22540 || !cand->parent
22541 || cand->parent->tag != DW_TAG_subprogram)
22542 continue;
22544 CORE_ADDR pc_low, pc_high;
22545 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22546 if (pc_low == ((CORE_ADDR) -1))
22547 continue;
22548 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22549 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22550 if (!(pc_low <= pc && pc < pc_high))
22551 continue;
22553 die = cand;
22554 attr = dwarf2_attr (die, DW_AT_location, cu);
22555 break;
22559 if (!attr)
22561 /* DWARF: "If there is no such attribute, then there is no effect.".
22562 DATA is ignored if SIZE is 0. */
22564 retval.data = NULL;
22565 retval.size = 0;
22567 else if (attr->form_is_section_offset ())
22569 struct dwarf2_loclist_baton loclist_baton;
22570 CORE_ADDR pc = get_frame_pc ();
22571 size_t size;
22573 fill_in_loclist_baton (cu, &loclist_baton, attr);
22575 retval.data = dwarf2_find_location_expression (&loclist_baton,
22576 &size, pc);
22577 retval.size = size;
22579 else
22581 if (!attr->form_is_block ())
22582 error (_("Dwarf Error: DIE at %s referenced in module %s "
22583 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22584 sect_offset_str (sect_off), objfile_name (objfile));
22586 struct dwarf_block *block = attr->as_block ();
22587 retval.data = block->data;
22588 retval.size = block->size;
22590 retval.per_objfile = per_objfile;
22591 retval.per_cu = cu->per_cu;
22593 per_objfile->age_comp_units ();
22595 return retval;
22598 /* See read.h. */
22600 struct dwarf2_locexpr_baton
22601 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22602 dwarf2_per_cu_data *per_cu,
22603 dwarf2_per_objfile *per_objfile,
22604 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22606 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22608 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22609 get_frame_pc);
22612 /* Write a constant of a given type as target-ordered bytes into
22613 OBSTACK. */
22615 static const gdb_byte *
22616 write_constant_as_bytes (struct obstack *obstack,
22617 enum bfd_endian byte_order,
22618 struct type *type,
22619 ULONGEST value,
22620 LONGEST *len)
22622 gdb_byte *result;
22624 *len = type->length ();
22625 result = (gdb_byte *) obstack_alloc (obstack, *len);
22626 store_unsigned_integer (result, *len, byte_order, value);
22628 return result;
22631 /* See read.h. */
22633 const gdb_byte *
22634 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22635 dwarf2_per_cu_data *per_cu,
22636 dwarf2_per_objfile *per_objfile,
22637 obstack *obstack,
22638 LONGEST *len)
22640 struct die_info *die;
22641 struct attribute *attr;
22642 const gdb_byte *result = NULL;
22643 struct type *type;
22644 LONGEST value;
22645 enum bfd_endian byte_order;
22646 struct objfile *objfile = per_objfile->objfile;
22648 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22649 if (cu == nullptr)
22650 cu = load_cu (per_cu, per_objfile, false);
22652 if (cu == nullptr)
22654 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22655 Instead just throw an error, not much else we can do. */
22656 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22657 sect_offset_str (sect_off), objfile_name (objfile));
22660 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22661 if (!die)
22662 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22663 sect_offset_str (sect_off), objfile_name (objfile));
22665 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22666 if (attr == NULL)
22667 return NULL;
22669 byte_order = (bfd_big_endian (objfile->obfd.get ())
22670 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22672 switch (attr->form)
22674 case DW_FORM_addr:
22675 case DW_FORM_addrx:
22676 case DW_FORM_GNU_addr_index:
22678 gdb_byte *tem;
22680 *len = cu->header.addr_size;
22681 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22682 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22683 result = tem;
22685 break;
22686 case DW_FORM_string:
22687 case DW_FORM_strp:
22688 case DW_FORM_strx:
22689 case DW_FORM_GNU_str_index:
22690 case DW_FORM_GNU_strp_alt:
22691 /* The string is already allocated on the objfile obstack, point
22692 directly to it. */
22694 const char *attr_name = attr->as_string ();
22695 result = (const gdb_byte *) attr_name;
22696 *len = strlen (attr_name);
22698 break;
22699 case DW_FORM_block1:
22700 case DW_FORM_block2:
22701 case DW_FORM_block4:
22702 case DW_FORM_block:
22703 case DW_FORM_exprloc:
22704 case DW_FORM_data16:
22706 struct dwarf_block *block = attr->as_block ();
22707 result = block->data;
22708 *len = block->size;
22710 break;
22712 /* The DW_AT_const_value attributes are supposed to carry the
22713 symbol's value "represented as it would be on the target
22714 architecture." By the time we get here, it's already been
22715 converted to host endianness, so we just need to sign- or
22716 zero-extend it as appropriate. */
22717 case DW_FORM_data1:
22718 type = die_type (die, cu);
22719 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22720 if (result == NULL)
22721 result = write_constant_as_bytes (obstack, byte_order,
22722 type, value, len);
22723 break;
22724 case DW_FORM_data2:
22725 type = die_type (die, cu);
22726 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22727 if (result == NULL)
22728 result = write_constant_as_bytes (obstack, byte_order,
22729 type, value, len);
22730 break;
22731 case DW_FORM_data4:
22732 type = die_type (die, cu);
22733 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22734 if (result == NULL)
22735 result = write_constant_as_bytes (obstack, byte_order,
22736 type, value, len);
22737 break;
22738 case DW_FORM_data8:
22739 type = die_type (die, cu);
22740 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22741 if (result == NULL)
22742 result = write_constant_as_bytes (obstack, byte_order,
22743 type, value, len);
22744 break;
22746 case DW_FORM_sdata:
22747 case DW_FORM_implicit_const:
22748 type = die_type (die, cu);
22749 result = write_constant_as_bytes (obstack, byte_order,
22750 type, attr->as_signed (), len);
22751 break;
22753 case DW_FORM_udata:
22754 type = die_type (die, cu);
22755 result = write_constant_as_bytes (obstack, byte_order,
22756 type, attr->as_unsigned (), len);
22757 break;
22759 default:
22760 complaint (_("unsupported const value attribute form: '%s'"),
22761 dwarf_form_name (attr->form));
22762 break;
22765 return result;
22768 /* See read.h. */
22770 struct type *
22771 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22772 dwarf2_per_cu_data *per_cu,
22773 dwarf2_per_objfile *per_objfile,
22774 const char **var_name)
22776 struct die_info *die;
22778 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22779 if (cu == nullptr)
22780 cu = load_cu (per_cu, per_objfile, false);
22782 if (cu == nullptr)
22783 return nullptr;
22785 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22786 if (!die)
22787 return NULL;
22789 if (var_name != nullptr)
22790 *var_name = var_decl_name (die, cu);
22791 return die_type (die, cu);
22794 /* See read.h. */
22796 struct type *
22797 dwarf2_get_die_type (cu_offset die_offset,
22798 dwarf2_per_cu_data *per_cu,
22799 dwarf2_per_objfile *per_objfile)
22801 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22802 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22805 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22806 On entry *REF_CU is the CU of SRC_DIE.
22807 On exit *REF_CU is the CU of the result.
22808 Returns NULL if the referenced DIE isn't found. */
22810 static struct die_info *
22811 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22812 struct dwarf2_cu **ref_cu)
22814 struct die_info temp_die;
22815 struct dwarf2_cu *sig_cu;
22816 struct die_info *die;
22817 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22820 /* While it might be nice to assert sig_type->type == NULL here,
22821 we can get here for DW_AT_imported_declaration where we need
22822 the DIE not the type. */
22824 /* If necessary, add it to the queue and load its DIEs.
22826 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22827 it doesn't mean they are currently loaded. Since we require them
22828 to be loaded, we must check for ourselves. */
22829 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22830 language_minimal)
22831 || per_objfile->get_cu (sig_type) == nullptr)
22832 read_signatured_type (sig_type, per_objfile);
22834 sig_cu = per_objfile->get_cu (sig_type);
22835 gdb_assert (sig_cu != NULL);
22836 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22837 temp_die.sect_off = sig_type->type_offset_in_section;
22838 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22839 to_underlying (temp_die.sect_off));
22840 if (die)
22842 /* For .gdb_index version 7 keep track of included TUs.
22843 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22844 if (per_objfile->per_bfd->index_table != NULL
22845 && !per_objfile->per_bfd->index_table->version_check ())
22847 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22850 *ref_cu = sig_cu;
22851 return die;
22854 return NULL;
22857 /* Follow signatured type referenced by ATTR in SRC_DIE.
22858 On entry *REF_CU is the CU of SRC_DIE.
22859 On exit *REF_CU is the CU of the result.
22860 The result is the DIE of the type.
22861 If the referenced type cannot be found an error is thrown. */
22863 static struct die_info *
22864 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22865 struct dwarf2_cu **ref_cu)
22867 ULONGEST signature = attr->as_signature ();
22868 struct signatured_type *sig_type;
22869 struct die_info *die;
22871 gdb_assert (attr->form == DW_FORM_ref_sig8);
22873 sig_type = lookup_signatured_type (*ref_cu, signature);
22874 /* sig_type will be NULL if the signatured type is missing from
22875 the debug info. */
22876 if (sig_type == NULL)
22878 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22879 " from DIE at %s [in module %s]"),
22880 hex_string (signature), sect_offset_str (src_die->sect_off),
22881 objfile_name ((*ref_cu)->per_objfile->objfile));
22884 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22885 if (die == NULL)
22887 dump_die_for_error (src_die);
22888 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22889 " from DIE at %s [in module %s]"),
22890 hex_string (signature), sect_offset_str (src_die->sect_off),
22891 objfile_name ((*ref_cu)->per_objfile->objfile));
22894 return die;
22897 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22898 reading in and processing the type unit if necessary. */
22900 static struct type *
22901 get_signatured_type (struct die_info *die, ULONGEST signature,
22902 struct dwarf2_cu *cu)
22904 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22905 struct signatured_type *sig_type;
22906 struct dwarf2_cu *type_cu;
22907 struct die_info *type_die;
22908 struct type *type;
22910 sig_type = lookup_signatured_type (cu, signature);
22911 /* sig_type will be NULL if the signatured type is missing from
22912 the debug info. */
22913 if (sig_type == NULL)
22915 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22916 " from DIE at %s [in module %s]"),
22917 hex_string (signature), sect_offset_str (die->sect_off),
22918 objfile_name (per_objfile->objfile));
22919 return build_error_marker_type (cu, die);
22922 /* If we already know the type we're done. */
22923 type = per_objfile->get_type_for_signatured_type (sig_type);
22924 if (type != nullptr)
22925 return type;
22927 type_cu = cu;
22928 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22929 if (type_die != NULL)
22931 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22932 is created. This is important, for example, because for c++ classes
22933 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22934 type = read_type_die (type_die, type_cu);
22935 if (type == NULL)
22937 complaint (_("Dwarf Error: Cannot build signatured type %s"
22938 " referenced from DIE at %s [in module %s]"),
22939 hex_string (signature), sect_offset_str (die->sect_off),
22940 objfile_name (per_objfile->objfile));
22941 type = build_error_marker_type (cu, die);
22944 else
22946 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22947 " from DIE at %s [in module %s]"),
22948 hex_string (signature), sect_offset_str (die->sect_off),
22949 objfile_name (per_objfile->objfile));
22950 type = build_error_marker_type (cu, die);
22953 per_objfile->set_type_for_signatured_type (sig_type, type);
22955 return type;
22958 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22959 reading in and processing the type unit if necessary. */
22961 static struct type *
22962 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22963 struct dwarf2_cu *cu) /* ARI: editCase function */
22965 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22966 if (attr->form_is_ref ())
22968 struct dwarf2_cu *type_cu = cu;
22969 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22971 return read_type_die (type_die, type_cu);
22973 else if (attr->form == DW_FORM_ref_sig8)
22975 return get_signatured_type (die, attr->as_signature (), cu);
22977 else
22979 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22981 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22982 " at %s [in module %s]"),
22983 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22984 objfile_name (per_objfile->objfile));
22985 return build_error_marker_type (cu, die);
22989 /* Load the DIEs associated with type unit PER_CU into memory. */
22991 static void
22992 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22993 dwarf2_per_objfile *per_objfile)
22995 struct signatured_type *sig_type;
22997 /* We have the per_cu, but we need the signatured_type.
22998 Fortunately this is an easy translation. */
22999 gdb_assert (per_cu->is_debug_types);
23000 sig_type = (struct signatured_type *) per_cu;
23002 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23004 read_signatured_type (sig_type, per_objfile);
23006 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23009 /* Read in a signatured type and build its CU and DIEs.
23010 If the type is a stub for the real type in a DWO file,
23011 read in the real type from the DWO file as well. */
23013 static void
23014 read_signatured_type (signatured_type *sig_type,
23015 dwarf2_per_objfile *per_objfile)
23017 gdb_assert (sig_type->is_debug_types);
23018 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23020 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23022 if (!reader.dummy_p)
23024 struct dwarf2_cu *cu = reader.cu;
23025 const gdb_byte *info_ptr = reader.info_ptr;
23027 gdb_assert (cu->die_hash == NULL);
23028 cu->die_hash =
23029 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
23030 die_hash,
23031 die_eq,
23032 NULL,
23033 &cu->comp_unit_obstack,
23034 hashtab_obstack_allocate,
23035 dummy_obstack_deallocate);
23037 if (reader.comp_unit_die->has_children)
23038 reader.comp_unit_die->child
23039 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23040 reader.comp_unit_die);
23041 cu->dies = reader.comp_unit_die;
23042 /* comp_unit_die is not stored in die_hash, no need. */
23044 /* We try not to read any attributes in this function, because
23045 not all CUs needed for references have been loaded yet, and
23046 symbol table processing isn't initialized. But we have to
23047 set the CU language, or we won't be able to build types
23048 correctly. Similarly, if we do not read the producer, we can
23049 not apply producer-specific interpretation. */
23050 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23052 reader.keep ();
23055 sig_type->tu_read = 1;
23058 /* Decode simple location descriptions.
23059 Given a pointer to a dwarf block that defines a location, compute
23060 the location and return the value. If COMPUTED is non-null, it is
23061 set to true to indicate that decoding was successful, and false
23062 otherwise. If COMPUTED is null, then this function may emit a
23063 complaint. */
23065 static CORE_ADDR
23066 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23068 struct objfile *objfile = cu->per_objfile->objfile;
23069 size_t i;
23070 size_t size = blk->size;
23071 const gdb_byte *data = blk->data;
23072 CORE_ADDR stack[64];
23073 int stacki;
23074 unsigned int bytes_read, unsnd;
23075 gdb_byte op;
23077 if (computed != nullptr)
23078 *computed = false;
23080 i = 0;
23081 stacki = 0;
23082 stack[stacki] = 0;
23083 stack[++stacki] = 0;
23085 while (i < size)
23087 op = data[i++];
23088 switch (op)
23090 case DW_OP_lit0:
23091 case DW_OP_lit1:
23092 case DW_OP_lit2:
23093 case DW_OP_lit3:
23094 case DW_OP_lit4:
23095 case DW_OP_lit5:
23096 case DW_OP_lit6:
23097 case DW_OP_lit7:
23098 case DW_OP_lit8:
23099 case DW_OP_lit9:
23100 case DW_OP_lit10:
23101 case DW_OP_lit11:
23102 case DW_OP_lit12:
23103 case DW_OP_lit13:
23104 case DW_OP_lit14:
23105 case DW_OP_lit15:
23106 case DW_OP_lit16:
23107 case DW_OP_lit17:
23108 case DW_OP_lit18:
23109 case DW_OP_lit19:
23110 case DW_OP_lit20:
23111 case DW_OP_lit21:
23112 case DW_OP_lit22:
23113 case DW_OP_lit23:
23114 case DW_OP_lit24:
23115 case DW_OP_lit25:
23116 case DW_OP_lit26:
23117 case DW_OP_lit27:
23118 case DW_OP_lit28:
23119 case DW_OP_lit29:
23120 case DW_OP_lit30:
23121 case DW_OP_lit31:
23122 stack[++stacki] = op - DW_OP_lit0;
23123 break;
23125 case DW_OP_reg0:
23126 case DW_OP_reg1:
23127 case DW_OP_reg2:
23128 case DW_OP_reg3:
23129 case DW_OP_reg4:
23130 case DW_OP_reg5:
23131 case DW_OP_reg6:
23132 case DW_OP_reg7:
23133 case DW_OP_reg8:
23134 case DW_OP_reg9:
23135 case DW_OP_reg10:
23136 case DW_OP_reg11:
23137 case DW_OP_reg12:
23138 case DW_OP_reg13:
23139 case DW_OP_reg14:
23140 case DW_OP_reg15:
23141 case DW_OP_reg16:
23142 case DW_OP_reg17:
23143 case DW_OP_reg18:
23144 case DW_OP_reg19:
23145 case DW_OP_reg20:
23146 case DW_OP_reg21:
23147 case DW_OP_reg22:
23148 case DW_OP_reg23:
23149 case DW_OP_reg24:
23150 case DW_OP_reg25:
23151 case DW_OP_reg26:
23152 case DW_OP_reg27:
23153 case DW_OP_reg28:
23154 case DW_OP_reg29:
23155 case DW_OP_reg30:
23156 case DW_OP_reg31:
23157 stack[++stacki] = op - DW_OP_reg0;
23158 if (i < size)
23160 if (computed == nullptr)
23161 dwarf2_complex_location_expr_complaint ();
23162 else
23163 return 0;
23165 break;
23167 case DW_OP_regx:
23168 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23169 i += bytes_read;
23170 stack[++stacki] = unsnd;
23171 if (i < size)
23173 if (computed == nullptr)
23174 dwarf2_complex_location_expr_complaint ();
23175 else
23176 return 0;
23178 break;
23180 case DW_OP_addr:
23181 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
23182 &data[i],
23183 &bytes_read);
23184 i += bytes_read;
23185 break;
23187 case DW_OP_const1u:
23188 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
23189 i += 1;
23190 break;
23192 case DW_OP_const1s:
23193 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
23194 i += 1;
23195 break;
23197 case DW_OP_const2u:
23198 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
23199 i += 2;
23200 break;
23202 case DW_OP_const2s:
23203 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
23204 i += 2;
23205 break;
23207 case DW_OP_const4u:
23208 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
23209 i += 4;
23210 break;
23212 case DW_OP_const4s:
23213 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
23214 i += 4;
23215 break;
23217 case DW_OP_const8u:
23218 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
23219 i += 8;
23220 break;
23222 case DW_OP_constu:
23223 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23224 &bytes_read);
23225 i += bytes_read;
23226 break;
23228 case DW_OP_consts:
23229 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23230 i += bytes_read;
23231 break;
23233 case DW_OP_dup:
23234 stack[stacki + 1] = stack[stacki];
23235 stacki++;
23236 break;
23238 case DW_OP_plus:
23239 stack[stacki - 1] += stack[stacki];
23240 stacki--;
23241 break;
23243 case DW_OP_plus_uconst:
23244 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23245 &bytes_read);
23246 i += bytes_read;
23247 break;
23249 case DW_OP_minus:
23250 stack[stacki - 1] -= stack[stacki];
23251 stacki--;
23252 break;
23254 case DW_OP_deref:
23255 /* If we're not the last op, then we definitely can't encode
23256 this using GDB's address_class enum. This is valid for partial
23257 global symbols, although the variable's address will be bogus
23258 in the psymtab. */
23259 if (i < size)
23261 if (computed == nullptr)
23262 dwarf2_complex_location_expr_complaint ();
23263 else
23264 return 0;
23266 break;
23268 case DW_OP_GNU_push_tls_address:
23269 case DW_OP_form_tls_address:
23270 /* The top of the stack has the offset from the beginning
23271 of the thread control block at which the variable is located. */
23272 /* Nothing should follow this operator, so the top of stack would
23273 be returned. */
23274 /* This is valid for partial global symbols, but the variable's
23275 address will be bogus in the psymtab. Make it always at least
23276 non-zero to not look as a variable garbage collected by linker
23277 which have DW_OP_addr 0. */
23278 if (i < size)
23280 if (computed == nullptr)
23281 dwarf2_complex_location_expr_complaint ();
23282 else
23283 return 0;
23285 stack[stacki]++;
23286 break;
23288 case DW_OP_GNU_uninit:
23289 if (computed != nullptr)
23290 return 0;
23291 break;
23293 case DW_OP_addrx:
23294 case DW_OP_GNU_addr_index:
23295 case DW_OP_GNU_const_index:
23296 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23297 &bytes_read);
23298 i += bytes_read;
23299 break;
23301 default:
23302 if (computed == nullptr)
23304 const char *name = get_DW_OP_name (op);
23306 if (name)
23307 complaint (_("unsupported stack op: '%s'"),
23308 name);
23309 else
23310 complaint (_("unsupported stack op: '%02x'"),
23311 op);
23314 return (stack[stacki]);
23317 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23318 outside of the allocated space. Also enforce minimum>0. */
23319 if (stacki >= ARRAY_SIZE (stack) - 1)
23321 if (computed == nullptr)
23322 complaint (_("location description stack overflow"));
23323 return 0;
23326 if (stacki <= 0)
23328 if (computed == nullptr)
23329 complaint (_("location description stack underflow"));
23330 return 0;
23334 if (computed != nullptr)
23335 *computed = true;
23336 return (stack[stacki]);
23339 /* memory allocation interface */
23341 static struct dwarf_block *
23342 dwarf_alloc_block (struct dwarf2_cu *cu)
23344 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23347 static struct die_info *
23348 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23350 struct die_info *die;
23351 size_t size = sizeof (struct die_info);
23353 if (num_attrs > 1)
23354 size += (num_attrs - 1) * sizeof (struct attribute);
23356 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23357 memset (die, 0, sizeof (struct die_info));
23358 return (die);
23363 /* Macro support. */
23365 /* An overload of dwarf_decode_macros that finds the correct section
23366 and ensures it is read in before calling the other overload. */
23368 static void
23369 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23370 int section_is_gnu)
23372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23373 struct objfile *objfile = per_objfile->objfile;
23374 const struct line_header *lh = cu->line_header;
23375 unsigned int offset_size = cu->header.offset_size;
23376 struct dwarf2_section_info *section;
23377 const char *section_name;
23379 if (cu->dwo_unit != nullptr)
23381 if (section_is_gnu)
23383 section = &cu->dwo_unit->dwo_file->sections.macro;
23384 section_name = ".debug_macro.dwo";
23386 else
23388 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23389 section_name = ".debug_macinfo.dwo";
23392 else
23394 if (section_is_gnu)
23396 section = &per_objfile->per_bfd->macro;
23397 section_name = ".debug_macro";
23399 else
23401 section = &per_objfile->per_bfd->macinfo;
23402 section_name = ".debug_macinfo";
23406 section->read (objfile);
23407 if (section->buffer == nullptr)
23409 complaint (_("missing %s section"), section_name);
23410 return;
23413 buildsym_compunit *builder = cu->get_builder ();
23415 struct dwarf2_section_info *str_offsets_section;
23416 struct dwarf2_section_info *str_section;
23417 gdb::optional<ULONGEST> str_offsets_base;
23419 if (cu->dwo_unit != nullptr)
23421 str_offsets_section = &cu->dwo_unit->dwo_file
23422 ->sections.str_offsets;
23423 str_section = &cu->dwo_unit->dwo_file->sections.str;
23424 str_offsets_base = cu->header.addr_size;
23426 else
23428 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23429 str_section = &per_objfile->per_bfd->str;
23430 str_offsets_base = cu->str_offsets_base;
23433 dwarf_decode_macros (per_objfile, builder, section, lh,
23434 offset_size, offset, str_section, str_offsets_section,
23435 str_offsets_base, section_is_gnu, cu);
23438 /* Return the .debug_loc section to use for CU.
23439 For DWO files use .debug_loc.dwo. */
23441 static struct dwarf2_section_info *
23442 cu_debug_loc_section (struct dwarf2_cu *cu)
23444 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23446 if (cu->dwo_unit)
23448 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23450 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23452 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23453 : &per_objfile->per_bfd->loc);
23456 /* Return the .debug_rnglists section to use for CU. */
23457 static struct dwarf2_section_info *
23458 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23460 if (cu->header.version < 5)
23461 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23462 cu->header.version);
23463 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23465 /* Make sure we read the .debug_rnglists section from the file that
23466 contains the DW_AT_ranges attribute we are reading. Normally that
23467 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23468 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23469 program. */
23470 if (cu->dwo_unit != nullptr
23471 && tag != DW_TAG_compile_unit
23472 && tag != DW_TAG_skeleton_unit)
23474 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23476 if (sections->rnglists.size > 0)
23477 return &sections->rnglists;
23478 else
23479 error (_(".debug_rnglists section is missing from .dwo file."));
23481 return &dwarf2_per_objfile->per_bfd->rnglists;
23484 /* A helper function that fills in a dwarf2_loclist_baton. */
23486 static void
23487 fill_in_loclist_baton (struct dwarf2_cu *cu,
23488 struct dwarf2_loclist_baton *baton,
23489 const struct attribute *attr)
23491 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23492 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23494 section->read (per_objfile->objfile);
23496 baton->per_objfile = per_objfile;
23497 baton->per_cu = cu->per_cu;
23498 gdb_assert (baton->per_cu);
23499 /* We don't know how long the location list is, but make sure we
23500 don't run off the edge of the section. */
23501 baton->size = section->size - attr->as_unsigned ();
23502 baton->data = section->buffer + attr->as_unsigned ();
23503 if (cu->base_address.has_value ())
23504 baton->base_address = *cu->base_address;
23505 else
23506 baton->base_address = 0;
23507 baton->from_dwo = cu->dwo_unit != NULL;
23510 static void
23511 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23512 struct dwarf2_cu *cu, int is_block)
23514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23515 struct objfile *objfile = per_objfile->objfile;
23516 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23518 if (attr->form_is_section_offset ()
23519 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23520 the section. If so, fall through to the complaint in the
23521 other branch. */
23522 && attr->as_unsigned () < section->get_size (objfile))
23524 struct dwarf2_loclist_baton *baton;
23526 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23528 fill_in_loclist_baton (cu, baton, attr);
23530 if (!cu->base_address.has_value ())
23531 complaint (_("Location list used without "
23532 "specifying the CU base address."));
23534 sym->set_aclass_index ((is_block
23535 ? dwarf2_loclist_block_index
23536 : dwarf2_loclist_index));
23537 SYMBOL_LOCATION_BATON (sym) = baton;
23539 else
23541 struct dwarf2_locexpr_baton *baton;
23543 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23544 baton->per_objfile = per_objfile;
23545 baton->per_cu = cu->per_cu;
23546 gdb_assert (baton->per_cu);
23548 if (attr->form_is_block ())
23550 /* Note that we're just copying the block's data pointer
23551 here, not the actual data. We're still pointing into the
23552 info_buffer for SYM's objfile; right now we never release
23553 that buffer, but when we do clean up properly this may
23554 need to change. */
23555 struct dwarf_block *block = attr->as_block ();
23556 baton->size = block->size;
23557 baton->data = block->data;
23559 else
23561 dwarf2_invalid_attrib_class_complaint ("location description",
23562 sym->natural_name ());
23563 baton->size = 0;
23566 sym->set_aclass_index ((is_block
23567 ? dwarf2_locexpr_block_index
23568 : dwarf2_locexpr_index));
23569 SYMBOL_LOCATION_BATON (sym) = baton;
23573 /* See read.h. */
23575 const comp_unit_head *
23576 dwarf2_per_cu_data::get_header () const
23578 if (!m_header_read_in)
23580 const gdb_byte *info_ptr
23581 = this->section->buffer + to_underlying (this->sect_off);
23583 read_comp_unit_head (&m_header, info_ptr, this->section,
23584 rcuh_kind::COMPILE);
23586 m_header_read_in = true;
23589 return &m_header;
23592 /* See read.h. */
23595 dwarf2_per_cu_data::addr_size () const
23597 return this->get_header ()->addr_size;
23600 /* See read.h. */
23603 dwarf2_per_cu_data::offset_size () const
23605 return this->get_header ()->offset_size;
23608 /* See read.h. */
23611 dwarf2_per_cu_data::ref_addr_size () const
23613 const comp_unit_head *header = this->get_header ();
23615 if (header->version == 2)
23616 return header->addr_size;
23617 else
23618 return header->offset_size;
23621 /* A helper function for dwarf2_find_containing_comp_unit that returns
23622 the index of the result, and that searches a vector. It will
23623 return a result even if the offset in question does not actually
23624 occur in any CU. This is separate so that it can be unit
23625 tested. */
23627 static int
23628 dwarf2_find_containing_comp_unit
23629 (sect_offset sect_off,
23630 unsigned int offset_in_dwz,
23631 const std::vector<dwarf2_per_cu_data_up> &all_units)
23633 int low, high;
23635 low = 0;
23636 high = all_units.size () - 1;
23637 while (high > low)
23639 struct dwarf2_per_cu_data *mid_cu;
23640 int mid = low + (high - low) / 2;
23642 mid_cu = all_units[mid].get ();
23643 if (mid_cu->is_dwz > offset_in_dwz
23644 || (mid_cu->is_dwz == offset_in_dwz
23645 && mid_cu->sect_off + mid_cu->length () > sect_off))
23646 high = mid;
23647 else
23648 low = mid + 1;
23650 gdb_assert (low == high);
23651 return low;
23654 /* Locate the .debug_info compilation unit from CU's objfile which contains
23655 the DIE at OFFSET. Raises an error on failure. */
23657 static struct dwarf2_per_cu_data *
23658 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23659 unsigned int offset_in_dwz,
23660 dwarf2_per_bfd *per_bfd)
23662 int low = dwarf2_find_containing_comp_unit
23663 (sect_off, offset_in_dwz, per_bfd->all_units);
23664 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
23666 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23668 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23669 error (_("Dwarf Error: could not find partial DIE containing "
23670 "offset %s [in module %s]"),
23671 sect_offset_str (sect_off),
23672 bfd_get_filename (per_bfd->obfd));
23674 gdb_assert (per_bfd->all_units[low-1]->sect_off
23675 <= sect_off);
23676 return per_bfd->all_units[low - 1].get ();
23678 else
23680 if (low == per_bfd->all_units.size () - 1
23681 && sect_off >= this_cu->sect_off + this_cu->length ())
23682 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23683 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
23684 return this_cu;
23688 #if GDB_SELF_TEST
23690 namespace selftests {
23691 namespace find_containing_comp_unit {
23693 static void
23694 run_test ()
23696 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23697 dwarf2_per_cu_data *one_ptr = one.get ();
23698 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23699 dwarf2_per_cu_data *two_ptr = two.get ();
23700 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23701 dwarf2_per_cu_data *three_ptr = three.get ();
23702 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23703 dwarf2_per_cu_data *four_ptr = four.get ();
23705 one->set_length (5);
23706 two->sect_off = sect_offset (one->length ());
23707 two->set_length (7);
23709 three->set_length (5);
23710 three->is_dwz = 1;
23711 four->sect_off = sect_offset (three->length ());
23712 four->set_length (7);
23713 four->is_dwz = 1;
23715 std::vector<dwarf2_per_cu_data_up> units;
23716 units.push_back (std::move (one));
23717 units.push_back (std::move (two));
23718 units.push_back (std::move (three));
23719 units.push_back (std::move (four));
23721 int result;
23723 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23724 SELF_CHECK (units[result].get () == one_ptr);
23725 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23726 SELF_CHECK (units[result].get () == one_ptr);
23727 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23728 SELF_CHECK (units[result].get () == two_ptr);
23730 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23731 SELF_CHECK (units[result].get () == three_ptr);
23732 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23733 SELF_CHECK (units[result].get () == three_ptr);
23734 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23735 SELF_CHECK (units[result].get () == four_ptr);
23741 #endif /* GDB_SELF_TEST */
23743 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23745 static void
23746 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23747 enum language pretend_language)
23749 struct attribute *attr;
23751 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23753 /* Set the language we're debugging. */
23754 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23755 enum language lang;
23756 if (cu->producer != nullptr
23757 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23759 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23760 attribute is not standardised yet. As a workaround for the
23761 language detection we fall back to the DW_AT_producer
23762 string. */
23763 lang = language_opencl;
23765 else if (cu->producer != nullptr
23766 && strstr (cu->producer, "GNU Go ") != NULL)
23768 /* Similar hack for Go. */
23769 lang = language_go;
23771 else if (attr != nullptr)
23772 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23773 else
23774 lang = pretend_language;
23776 cu->language_defn = language_def (lang);
23778 switch (comp_unit_die->tag)
23780 case DW_TAG_compile_unit:
23781 cu->per_cu->set_unit_type (DW_UT_compile);
23782 break;
23783 case DW_TAG_partial_unit:
23784 cu->per_cu->set_unit_type (DW_UT_partial);
23785 break;
23786 case DW_TAG_type_unit:
23787 cu->per_cu->set_unit_type (DW_UT_type);
23788 break;
23789 default:
23790 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23791 dwarf_tag_name (comp_unit_die->tag),
23792 sect_offset_str (cu->per_cu->sect_off));
23795 cu->per_cu->set_lang (lang);
23798 /* See read.h. */
23800 dwarf2_cu *
23801 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23803 auto it = m_dwarf2_cus.find (per_cu);
23804 if (it == m_dwarf2_cus.end ())
23805 return nullptr;
23807 return it->second.get ();
23810 /* See read.h. */
23812 void
23813 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
23814 std::unique_ptr<dwarf2_cu> cu)
23816 gdb_assert (this->get_cu (per_cu) == nullptr);
23818 m_dwarf2_cus[per_cu] = std::move (cu);
23821 /* See read.h. */
23823 void
23824 dwarf2_per_objfile::age_comp_units ()
23826 dwarf_read_debug_printf_v ("running");
23828 /* This is not expected to be called in the middle of CU expansion. There is
23829 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23830 loaded in memory. Calling age_comp_units while the queue is in use could
23831 make us free the DIEs for a CU that is in the queue and therefore break
23832 that invariant. */
23833 gdb_assert (!queue.has_value ());
23835 /* Start by clearing all marks. */
23836 for (const auto &pair : m_dwarf2_cus)
23837 pair.second->clear_mark ();
23839 /* Traverse all CUs, mark them and their dependencies if used recently
23840 enough. */
23841 for (const auto &pair : m_dwarf2_cus)
23843 dwarf2_cu *cu = pair.second.get ();
23845 cu->last_used++;
23846 if (cu->last_used <= dwarf_max_cache_age)
23847 cu->mark ();
23850 /* Delete all CUs still not marked. */
23851 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23853 dwarf2_cu *cu = it->second.get ();
23855 if (!cu->is_marked ())
23857 dwarf_read_debug_printf_v ("deleting old CU %s",
23858 sect_offset_str (cu->per_cu->sect_off));
23859 it = m_dwarf2_cus.erase (it);
23861 else
23862 it++;
23866 /* See read.h. */
23868 void
23869 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23871 auto it = m_dwarf2_cus.find (per_cu);
23872 if (it == m_dwarf2_cus.end ())
23873 return;
23875 m_dwarf2_cus.erase (it);
23878 dwarf2_per_objfile::~dwarf2_per_objfile ()
23880 remove_all_cus ();
23883 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23884 We store these in a hash table separate from the DIEs, and preserve them
23885 when the DIEs are flushed out of cache.
23887 The CU "per_cu" pointer is needed because offset alone is not enough to
23888 uniquely identify the type. A file may have multiple .debug_types sections,
23889 or the type may come from a DWO file. Furthermore, while it's more logical
23890 to use per_cu->section+offset, with Fission the section with the data is in
23891 the DWO file but we don't know that section at the point we need it.
23892 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23893 because we can enter the lookup routine, get_die_type_at_offset, from
23894 outside this file, and thus won't necessarily have PER_CU->cu.
23895 Fortunately, PER_CU is stable for the life of the objfile. */
23897 struct dwarf2_per_cu_offset_and_type
23899 const struct dwarf2_per_cu_data *per_cu;
23900 sect_offset sect_off;
23901 struct type *type;
23904 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23906 static hashval_t
23907 per_cu_offset_and_type_hash (const void *item)
23909 const struct dwarf2_per_cu_offset_and_type *ofs
23910 = (const struct dwarf2_per_cu_offset_and_type *) item;
23912 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23915 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23917 static int
23918 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23920 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23921 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23922 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23923 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23925 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23926 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23929 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23930 table if necessary. For convenience, return TYPE.
23932 The DIEs reading must have careful ordering to:
23933 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23934 reading current DIE.
23935 * Not trying to dereference contents of still incompletely read in types
23936 while reading in other DIEs.
23937 * Enable referencing still incompletely read in types just by a pointer to
23938 the type without accessing its fields.
23940 Therefore caller should follow these rules:
23941 * Try to fetch any prerequisite types we may need to build this DIE type
23942 before building the type and calling set_die_type.
23943 * After building type call set_die_type for current DIE as soon as
23944 possible before fetching more types to complete the current type.
23945 * Make the type as complete as possible before fetching more types. */
23947 static struct type *
23948 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23949 bool skip_data_location)
23951 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23952 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23953 struct objfile *objfile = per_objfile->objfile;
23954 struct attribute *attr;
23955 struct dynamic_prop prop;
23957 /* For Ada types, make sure that the gnat-specific data is always
23958 initialized (if not already set). There are a few types where
23959 we should not be doing so, because the type-specific area is
23960 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23961 where the type-specific area is used to store the floatformat).
23962 But this is not a problem, because the gnat-specific information
23963 is actually not needed for these types. */
23964 if (need_gnat_info (cu)
23965 && type->code () != TYPE_CODE_FUNC
23966 && type->code () != TYPE_CODE_FLT
23967 && type->code () != TYPE_CODE_METHODPTR
23968 && type->code () != TYPE_CODE_MEMBERPTR
23969 && type->code () != TYPE_CODE_METHOD
23970 && type->code () != TYPE_CODE_FIXED_POINT
23971 && !HAVE_GNAT_AUX_INFO (type))
23972 INIT_GNAT_SPECIFIC (type);
23974 /* Read DW_AT_allocated and set in type. */
23975 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23976 if (attr != NULL)
23978 struct type *prop_type = cu->addr_sized_int_type (false);
23979 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23980 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23983 /* Read DW_AT_associated and set in type. */
23984 attr = dwarf2_attr (die, DW_AT_associated, cu);
23985 if (attr != NULL)
23987 struct type *prop_type = cu->addr_sized_int_type (false);
23988 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23989 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23992 /* Read DW_AT_rank and set in type. */
23993 attr = dwarf2_attr (die, DW_AT_rank, cu);
23994 if (attr != NULL)
23996 struct type *prop_type = cu->addr_sized_int_type (false);
23997 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23998 type->add_dyn_prop (DYN_PROP_RANK, prop);
24001 /* Read DW_AT_data_location and set in type. */
24002 if (!skip_data_location)
24004 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24005 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24006 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24009 if (per_objfile->die_type_hash == NULL)
24010 per_objfile->die_type_hash
24011 = htab_up (htab_create_alloc (127,
24012 per_cu_offset_and_type_hash,
24013 per_cu_offset_and_type_eq,
24014 NULL, xcalloc, xfree));
24016 ofs.per_cu = cu->per_cu;
24017 ofs.sect_off = die->sect_off;
24018 ofs.type = type;
24019 slot = (struct dwarf2_per_cu_offset_and_type **)
24020 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24021 if (*slot)
24022 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24023 sect_offset_str (die->sect_off));
24024 *slot = XOBNEW (&objfile->objfile_obstack,
24025 struct dwarf2_per_cu_offset_and_type);
24026 **slot = ofs;
24027 return type;
24030 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24031 or return NULL if the die does not have a saved type. */
24033 static struct type *
24034 get_die_type_at_offset (sect_offset sect_off,
24035 dwarf2_per_cu_data *per_cu,
24036 dwarf2_per_objfile *per_objfile)
24038 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24040 if (per_objfile->die_type_hash == NULL)
24041 return NULL;
24043 ofs.per_cu = per_cu;
24044 ofs.sect_off = sect_off;
24045 slot = ((struct dwarf2_per_cu_offset_and_type *)
24046 htab_find (per_objfile->die_type_hash.get (), &ofs));
24047 if (slot)
24048 return slot->type;
24049 else
24050 return NULL;
24053 /* Look up the type for DIE in CU in die_type_hash,
24054 or return NULL if DIE does not have a saved type. */
24056 static struct type *
24057 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24059 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24062 struct cmd_list_element *set_dwarf_cmdlist;
24063 struct cmd_list_element *show_dwarf_cmdlist;
24065 static void
24066 show_check_physname (struct ui_file *file, int from_tty,
24067 struct cmd_list_element *c, const char *value)
24069 gdb_printf (file,
24070 _("Whether to check \"physname\" is %s.\n"),
24071 value);
24074 void _initialize_dwarf2_read ();
24075 void
24076 _initialize_dwarf2_read ()
24078 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24079 _("\
24080 Set DWARF specific variables.\n\
24081 Configure DWARF variables such as the cache size."),
24082 _("\
24083 Show DWARF specific variables.\n\
24084 Show DWARF variables such as the cache size."),
24085 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24086 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24088 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24089 &dwarf_max_cache_age, _("\
24090 Set the upper bound on the age of cached DWARF compilation units."), _("\
24091 Show the upper bound on the age of cached DWARF compilation units."), _("\
24092 A higher limit means that cached compilation units will be stored\n\
24093 in memory longer, and more total memory will be used. Zero disables\n\
24094 caching, which can slow down startup."),
24095 NULL,
24096 show_dwarf_max_cache_age,
24097 &set_dwarf_cmdlist,
24098 &show_dwarf_cmdlist);
24100 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24101 Set debugging of the DWARF reader."), _("\
24102 Show debugging of the DWARF reader."), _("\
24103 When enabled (non-zero), debugging messages are printed during DWARF\n\
24104 reading and symtab expansion. A value of 1 (one) provides basic\n\
24105 information. A value greater than 1 provides more verbose information."),
24106 NULL,
24107 NULL,
24108 &setdebuglist, &showdebuglist);
24110 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24111 Set debugging of the DWARF DIE reader."), _("\
24112 Show debugging of the DWARF DIE reader."), _("\
24113 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24114 The value is the maximum depth to print."),
24115 NULL,
24116 NULL,
24117 &setdebuglist, &showdebuglist);
24119 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24120 Set debugging of the dwarf line reader."), _("\
24121 Show debugging of the dwarf line reader."), _("\
24122 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24123 A value of 1 (one) provides basic information.\n\
24124 A value greater than 1 provides more verbose information."),
24125 NULL,
24126 NULL,
24127 &setdebuglist, &showdebuglist);
24129 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24130 Set cross-checking of \"physname\" code against demangler."), _("\
24131 Show cross-checking of \"physname\" code against demangler."), _("\
24132 When enabled, GDB's internal \"physname\" code is checked against\n\
24133 the demangler."),
24134 NULL, show_check_physname,
24135 &setdebuglist, &showdebuglist);
24137 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24138 no_class, &use_deprecated_index_sections, _("\
24139 Set whether to use deprecated gdb_index sections."), _("\
24140 Show whether to use deprecated gdb_index sections."), _("\
24141 When enabled, deprecated .gdb_index sections are used anyway.\n\
24142 Normally they are ignored either because of a missing feature or\n\
24143 performance issue.\n\
24144 Warning: This option must be enabled before gdb reads the file."),
24145 NULL,
24146 NULL,
24147 &setlist, &showlist);
24149 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24150 &dwarf2_locexpr_funcs);
24151 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24152 &dwarf2_loclist_funcs);
24154 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24155 &dwarf2_block_frame_base_locexpr_funcs);
24156 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24157 &dwarf2_block_frame_base_loclist_funcs);
24159 #if GDB_SELF_TEST
24160 selftests::register_test ("dw2_expand_symtabs_matching",
24161 selftests::dw2_expand_symtabs_matching::run_test);
24162 selftests::register_test ("dwarf2_find_containing_comp_unit",
24163 selftests::find_containing_comp_unit::run_test);
24164 #endif