Update copyright year in gdbarch.sh doc/gdb.texinfo and doc/refcard.tex
[binutils-gdb.git] / gdb / dwarf2read.c
blob63d533a44598c5f8d740876a6bf248fa95740c42
1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2020 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 "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
120 struct name_component
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
135 struct mapped_index_base
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
157 return false;
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
184 const offset_type name;
185 const offset_type vec;
188 /* Index data format version. */
189 int version = 0;
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
200 bool symbol_name_slot_invalid (offset_type idx) const override
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
236 struct index_val
238 ULONGEST dwarf_tag;
239 struct attr
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
250 std::vector<attr> attr_vec;
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
255 const char *namei_to_name (uint32_t namei) const;
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
263 size_t symbol_name_count () const override
264 { return this->name_count; }
267 /* See dwarf2read.h. */
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
272 return dwarf2_objfile_data_key.get (objfile);
275 /* Default names of the debugging sections. */
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_line_str", ".zdebug_line_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_rnglists", ".zdebug_rnglists" },
293 { ".debug_types", ".zdebug_types" },
294 { ".debug_addr", ".zdebug_addr" },
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
297 { ".gdb_index", ".zgdb_index" },
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
303 /* List of DWO/DWP sections. */
305 static const struct dwop_section_names
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names loclists_dwo;
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
320 dwop_section_names =
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
336 /* local data types */
338 /* The data in a compilation unit header, after target2host
339 translation, looks like this. */
340 struct comp_unit_head
342 unsigned int length;
343 short version;
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
346 sect_offset abbrev_sect_off;
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
354 enum dwarf_unit_type unit_type;
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
358 sect_offset sect_off;
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
362 cu_offset first_die_cu_offset;
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
369 ULONGEST signature;
371 /* For types, offset in the type's DIE of the type defined by this TU. */
372 cu_offset type_cu_offset_in_tu;
375 /* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377 struct delayed_method_info
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
385 /* The index of the method in the fieldlist. */
386 int index;
388 /* The name of the DIE. */
389 const char *name;
391 /* The DIE associated with this method. */
392 struct die_info *die;
395 /* Internal state when decoding a particular compilation unit. */
396 struct dwarf2_cu
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
419 /* The header of the compilation unit. */
420 struct comp_unit_head header {};
422 /* Base address of this compilation unit. */
423 CORE_ADDR base_address = 0;
425 /* Non-zero if base_address has been set. */
426 int base_known = 0;
428 /* The language we are debugging. */
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
432 const char *producer = nullptr;
434 private:
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
437 std::unique_ptr<buildsym_compunit> m_builder;
439 public:
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
449 struct pending **list_in_scope = nullptr;
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
453 htab_t partial_dies = nullptr;
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
457 auto_obstack comp_unit_obstack;
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465 /* Backlink to our per_cu entry. */
466 struct dwarf2_per_cu_data *per_cu;
468 /* How many compilation units ago was this CU last referenced? */
469 int last_used = 0;
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
473 htab_t die_hash = nullptr;
475 /* Full DIEs if read in. */
476 struct die_info *dies = nullptr;
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
481 htab_t dependencies = nullptr;
483 /* Header data from the line table, during full symbol processing. */
484 struct line_header *line_header = nullptr;
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
490 die_info *line_header_die_owner = nullptr;
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
494 std::vector<delayed_method_info> method_list;
496 /* To be copied to symtab->call_site_htab. */
497 htab_t call_site_htab = nullptr;
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
508 struct dwo_unit *dwo_unit = nullptr;
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 ULONGEST addr_base = 0;
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
517 Note this value comes from the Fission stub CU/TU's DIE.
518 Also note that the value is zero in the non-DWO case so this value can
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
525 ULONGEST ranges_base = 0;
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
535 /* Mark used when releasing cached dies. */
536 bool mark : 1;
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
542 bool has_loclist : 1;
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
551 bool producer_is_icc : 1;
552 bool producer_is_icc_lt_14 : 1;
553 bool producer_is_codewarrior : 1;
555 /* When true, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
559 bool processing_has_namespace_info : 1;
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
578 return nullptr;
582 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
585 struct stmt_list_hash
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
590 /* Offset in .debug_line or .debug_line.dwo. */
591 sect_offset line_sect_off;
594 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
597 struct type_unit_group
599 /* dwarf2read.c's main "handle" on a TU symtab.
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
604 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
605 struct dwarf2_per_cu_data per_cu;
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
610 std::vector<signatured_type *> *tus;
612 /* The compunit symtab.
613 Type units in a group needn't all be defined in the same source file,
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
635 /* These sections are what may appear in a (real or virtual) DWO file. */
637 struct dwo_sections
639 struct dwarf2_section_info abbrev;
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
642 struct dwarf2_section_info loclists;
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
649 std::vector<dwarf2_section_info> types;
652 /* CUs/TUs in DWP/DWO files. */
654 struct dwo_unit
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
664 /* The section this CU/TU lives in, in the DWO file. */
665 struct dwarf2_section_info *section;
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
669 unsigned int length;
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
675 /* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
679 enum dwp_v2_section_ids
681 DW_SECT_MIN = 1
684 /* Data for one DWO file.
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
695 struct dwo_file
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
700 /* The DW_AT_GNU_dwo_name attribute.
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
704 const char *dwo_name = nullptr;
706 /* The DW_AT_comp_dir attribute. */
707 const char *comp_dir = nullptr;
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
711 gdb_bfd_ref_ptr dbfd;
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
716 struct dwo_sections sections {};
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
722 htab_t cus {};
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
726 htab_t tus {};
729 /* These sections are what may appear in a DWP file. */
731 struct dwp_sections
733 /* These are used by both DWP version 1 and 2. */
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
756 /* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
759 struct virtual_v1_dwo_sections
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 struct dwarf2_section_info info_or_types;
772 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
777 struct virtual_v2_dwo_sections
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
803 /* Contents of DWP hash tables. */
805 struct dwp_hash_table
807 uint32_t version, nr_columns;
808 uint32_t nr_units, nr_slots;
809 const gdb_byte *hash_table, *unit_table;
810 union
812 struct
814 const gdb_byte *indices;
815 } v1;
816 struct
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820 #define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
834 /* Data for one DWP file. */
836 struct dwp_file
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
844 /* Name of the file. */
845 const char *name;
847 /* File format version. */
848 int version = 0;
850 /* The bfd. */
851 gdb_bfd_ref_ptr dbfd;
853 /* Section info for this file. */
854 struct dwp_sections sections {};
856 /* Table of CUs in the file. */
857 const struct dwp_hash_table *cus = nullptr;
859 /* Table of TUs in the file. */
860 const struct dwp_hash_table *tus = nullptr;
862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
872 /* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
875 struct exists to abstract away the constant parameters of die reading. */
877 struct die_reader_specs
879 /* The bfd of die_section. */
880 bfd* abfd;
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
886 struct dwo_file *dwo_file;
888 /* The section the die comes from.
889 This is either .debug_info or .debug_types, or the .dwo variants. */
890 struct dwarf2_section_info *die_section;
892 /* die_section->buffer. */
893 const gdb_byte *buffer;
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
905 /* Type of function passed to init_cutu_and_read_dies, et.al. */
906 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
907 const gdb_byte *info_ptr,
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
912 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914 typedef int dir_index;
916 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918 typedef int file_name_index;
920 struct file_entry
922 file_entry () = default;
924 file_entry (const char *name_, dir_index d_index_,
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
927 d_index (d_index_),
928 mod_time (mod_time_),
929 length (length_)
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
934 const char *include_dir (const line_header *lh) const;
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
940 /* The directory index (1-based). */
941 dir_index d_index {};
943 unsigned int mod_time {};
945 unsigned int length {};
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
950 /* The associated symbol table, if any. */
951 struct symtab *symtab {};
954 /* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957 struct line_header
959 line_header ()
960 : offset_in_dwz {}
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
966 /* Add an entry to the file name table. */
967 void add_file_name (const char *name, dir_index d_index,
968 unsigned int mod_time, unsigned int length);
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
972 const char *include_dir_at (dir_index index) const
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
980 return NULL;
981 return m_include_dirs[vec_index];
984 bool is_valid_file_index (int file_index)
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
1001 return NULL;
1002 return &m_file_names[vec_index];
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1011 /* Offset of line number information in .debug_line section. */
1012 sect_offset sect_off {};
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1033 int file_names_size ()
1034 { return m_file_names.size(); }
1036 /* The start and end of the statement program following this
1037 header. These point into dwarf2_per_objfile->line_buffer. */
1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
1052 typedef std::unique_ptr<line_header> line_header_up;
1054 const char *
1055 file_entry::include_dir (const line_header *lh) const
1057 return lh->include_dir_at (d_index);
1060 /* When we construct a partial symbol table entry we only
1061 need this much information. */
1062 struct partial_die_info : public allocate_on_obstack
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1081 /* Offset of this DIE. */
1082 const sect_offset sect_off;
1084 /* DWARF-2 tag for this DIE. */
1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1087 /* Assorted flags describing the data found in this DIE. */
1088 const unsigned int has_children : 1;
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
1095 unsigned int may_be_inlined : 1;
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1113 /* Flag set if fixup has been called on this die. */
1114 unsigned int fixup_called : 1;
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1122 /* The name of this DIE. Normally the value of DW_AT_name, but
1123 sometimes a default name for unnamed DIEs. */
1124 const char *name = nullptr;
1126 /* The linkage name, if present. */
1127 const char *linkage_name = nullptr;
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
1132 const char *scope = nullptr;
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
1141 sect_offset sect_off;
1142 } d {};
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1149 DW_AT_sibling, if any. */
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
1152 const gdb_byte *sibling = nullptr;
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
1157 sect_offset spec_offset {};
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1196 /* This data structure holds the information of an abbrev. */
1197 struct abbrev_info
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1207 struct attr_abbrev
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
1216 /* Size of abbrev_table.abbrev_hash_table. */
1217 #define ABBREV_HASH_SIZE 121
1219 /* Top level data structure to contain an abbreviation table. */
1221 struct abbrev_table
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1226 m_abbrevs =
1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
1248 const sect_offset sect_off;
1250 /* Storage for the abbrev table. */
1251 auto_obstack abbrev_obstack;
1253 private:
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
1259 struct abbrev_info **m_abbrevs;
1262 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1264 /* Attributes have a name and a value. */
1265 struct attribute
1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1275 union
1277 const char *str;
1278 struct dwarf_block *blk;
1279 ULONGEST unsnd;
1280 LONGEST snd;
1281 CORE_ADDR addr;
1282 ULONGEST signature;
1287 /* This data structure holds a complete die structure. */
1288 struct die_info
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1293 /* Number of attributes */
1294 unsigned char num_attrs;
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1303 /* Abbrev number */
1304 unsigned int abbrev;
1306 /* Offset in .debug_info or .debug_types section. */
1307 sect_offset sect_off;
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
1312 together via their SIBLING fields. */
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
1323 /* Get at parts of an attribute structure. */
1325 #define DW_STRING(attr) ((attr)->u.str)
1326 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1327 #define DW_UNSND(attr) ((attr)->u.unsnd)
1328 #define DW_BLOCK(attr) ((attr)->u.blk)
1329 #define DW_SND(attr) ((attr)->u.snd)
1330 #define DW_ADDR(attr) ((attr)->u.addr)
1331 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1333 /* Blocks are a bunch of untyped bytes. */
1334 struct dwarf_block
1336 size_t size;
1338 /* Valid only if SIZE is not zero. */
1339 const gdb_byte *data;
1342 #ifndef ATTR_ALLOC_CHUNK
1343 #define ATTR_ALLOC_CHUNK 4
1344 #endif
1346 /* Allocate fields for structs, unions and enums in this size. */
1347 #ifndef DW_FIELD_ALLOC_CHUNK
1348 #define DW_FIELD_ALLOC_CHUNK 4
1349 #endif
1351 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1352 but this would require a corresponding change in unpack_field_as_long
1353 and friends. */
1354 static int bits_per_byte = 8;
1356 /* When reading a variant or variant part, we track a bit more
1357 information about the field, and store it in an object of this
1358 type. */
1360 struct variant_field
1362 /* If we see a DW_TAG_variant, then this will be the discriminant
1363 value. */
1364 ULONGEST discriminant_value;
1365 /* If we see a DW_TAG_variant, then this will be set if this is the
1366 default branch. */
1367 bool default_branch;
1368 /* While reading a DW_TAG_variant_part, this will be set if this
1369 field is the discriminant. */
1370 bool is_discriminant;
1373 struct nextfield
1375 int accessibility = 0;
1376 int virtuality = 0;
1377 /* Extra information to describe a variant or variant part. */
1378 struct variant_field variant {};
1379 struct field field {};
1382 struct fnfieldlist
1384 const char *name = nullptr;
1385 std::vector<struct fn_field> fnfields;
1388 /* The routines that read and process dies for a C struct or C++ class
1389 pass lists of data member fields and lists of member function fields
1390 in an instance of a field_info structure, as defined below. */
1391 struct field_info
1393 /* List of data member and baseclasses fields. */
1394 std::vector<struct nextfield> fields;
1395 std::vector<struct nextfield> baseclasses;
1397 /* Number of fields (including baseclasses). */
1398 int nfields = 0;
1400 /* Set if the accessibility of one of the fields is not public. */
1401 int non_public_fields = 0;
1403 /* Member function fieldlist array, contains name of possibly overloaded
1404 member function, number of overloaded member functions and a pointer
1405 to the head of the member function field chain. */
1406 std::vector<struct fnfieldlist> fnfieldlists;
1408 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1409 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1410 std::vector<struct decl_field> typedef_field_list;
1412 /* Nested types defined by this class and the number of elements in this
1413 list. */
1414 std::vector<struct decl_field> nested_types_list;
1417 /* One item on the queue of compilation units to read in full symbols
1418 for. */
1419 struct dwarf2_queue_item
1421 struct dwarf2_per_cu_data *per_cu;
1422 enum language pretend_language;
1423 struct dwarf2_queue_item *next;
1426 /* The current queue. */
1427 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1429 /* Loaded secondary compilation units are kept in memory until they
1430 have not been referenced for the processing of this many
1431 compilation units. Set this to zero to disable caching. Cache
1432 sizes of up to at least twenty will improve startup time for
1433 typical inter-CU-reference binaries, at an obvious memory cost. */
1434 static int dwarf_max_cache_age = 5;
1435 static void
1436 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1437 struct cmd_list_element *c, const char *value)
1439 fprintf_filtered (file, _("The upper bound on the age of cached "
1440 "DWARF compilation units is %s.\n"),
1441 value);
1444 /* local function prototypes */
1446 static const char *get_section_name (const struct dwarf2_section_info *);
1448 static const char *get_section_file_name (const struct dwarf2_section_info *);
1450 static void dwarf2_find_base_address (struct die_info *die,
1451 struct dwarf2_cu *cu);
1453 static struct partial_symtab *create_partial_symtab
1454 (struct dwarf2_per_cu_data *per_cu, const char *name);
1456 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1457 const gdb_byte *info_ptr,
1458 struct die_info *type_unit_die,
1459 int has_children, void *data);
1461 static void dwarf2_build_psymtabs_hard
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1464 static void scan_partial_symbols (struct partial_die_info *,
1465 CORE_ADDR *, CORE_ADDR *,
1466 int, struct dwarf2_cu *);
1468 static void add_partial_symbol (struct partial_die_info *,
1469 struct dwarf2_cu *);
1471 static void add_partial_namespace (struct partial_die_info *pdi,
1472 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1473 int set_addrmap, struct dwarf2_cu *cu);
1475 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1476 CORE_ADDR *highpc, int set_addrmap,
1477 struct dwarf2_cu *cu);
1479 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1480 struct dwarf2_cu *cu);
1482 static void add_partial_subprogram (struct partial_die_info *pdi,
1483 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1484 int need_pc, struct dwarf2_cu *cu);
1486 static void dwarf2_read_symtab (struct partial_symtab *,
1487 struct objfile *);
1489 static void psymtab_to_symtab_1 (struct partial_symtab *);
1491 static abbrev_table_up abbrev_table_read_table
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1493 sect_offset);
1495 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1497 static struct partial_die_info *load_partial_dies
1498 (const struct die_reader_specs *, const gdb_byte *, int);
1500 /* A pair of partial_die_info and compilation unit. */
1501 struct cu_partial_die_info
1503 /* The compilation unit of the partial_die_info. */
1504 struct dwarf2_cu *cu;
1505 /* A partial_die_info. */
1506 struct partial_die_info *pdi;
1508 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1509 : cu (cu),
1510 pdi (pdi)
1511 { /* Nothing. */ }
1513 private:
1514 cu_partial_die_info () = delete;
1517 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1518 struct dwarf2_cu *);
1520 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1521 struct attribute *, struct attr_abbrev *,
1522 const gdb_byte *);
1524 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1526 static int read_1_signed_byte (bfd *, const gdb_byte *);
1528 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1530 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1531 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1533 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1535 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1537 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1538 unsigned int *);
1540 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1542 static LONGEST read_checked_initial_length_and_offset
1543 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1544 unsigned int *, unsigned int *);
1546 static LONGEST read_offset (bfd *, const gdb_byte *,
1547 const struct comp_unit_head *,
1548 unsigned int *);
1550 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1552 static sect_offset read_abbrev_offset
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1554 struct dwarf2_section_info *, sect_offset);
1556 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1558 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1560 static const char *read_indirect_string
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1562 const struct comp_unit_head *, unsigned int *);
1564 static const char *read_indirect_line_string
1565 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1566 const struct comp_unit_head *, unsigned int *);
1568 static const char *read_indirect_string_at_offset
1569 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1570 LONGEST str_offset);
1572 static const char *read_indirect_string_from_dwz
1573 (struct objfile *objfile, struct dwz_file *, LONGEST);
1575 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1577 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1578 const gdb_byte *,
1579 unsigned int *);
1581 static const char *read_str_index (const struct die_reader_specs *reader,
1582 ULONGEST str_index);
1584 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1586 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1587 struct dwarf2_cu *);
1589 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1590 unsigned int);
1592 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1593 struct dwarf2_cu *cu);
1595 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1597 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1598 struct dwarf2_cu *cu);
1600 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1602 static struct die_info *die_specification (struct die_info *die,
1603 struct dwarf2_cu **);
1605 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1606 struct dwarf2_cu *cu);
1608 static void dwarf_decode_lines (struct line_header *, const char *,
1609 struct dwarf2_cu *, struct partial_symtab *,
1610 CORE_ADDR, int decode_mapping);
1612 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1613 const char *);
1615 static struct symbol *new_symbol (struct die_info *, struct type *,
1616 struct dwarf2_cu *, struct symbol * = NULL);
1618 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1619 struct dwarf2_cu *);
1621 static void dwarf2_const_value_attr (const struct attribute *attr,
1622 struct type *type,
1623 const char *name,
1624 struct obstack *obstack,
1625 struct dwarf2_cu *cu, LONGEST *value,
1626 const gdb_byte **bytes,
1627 struct dwarf2_locexpr_baton **baton);
1629 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1631 static int need_gnat_info (struct dwarf2_cu *);
1633 static struct type *die_descriptive_type (struct die_info *,
1634 struct dwarf2_cu *);
1636 static void set_descriptive_type (struct type *, struct die_info *,
1637 struct dwarf2_cu *);
1639 static struct type *die_containing_type (struct die_info *,
1640 struct dwarf2_cu *);
1642 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1643 struct dwarf2_cu *);
1645 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1647 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1649 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1651 static char *typename_concat (struct obstack *obs, const char *prefix,
1652 const char *suffix, int physname,
1653 struct dwarf2_cu *cu);
1655 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1657 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1659 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1661 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1663 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1665 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1667 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *, struct partial_symtab *);
1670 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1671 values. Keep the items ordered with increasing constraints compliance. */
1672 enum pc_bounds_kind
1674 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1675 PC_BOUNDS_NOT_PRESENT,
1677 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1678 were present but they do not form a valid range of PC addresses. */
1679 PC_BOUNDS_INVALID,
1681 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1682 PC_BOUNDS_RANGES,
1684 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1685 PC_BOUNDS_HIGH_LOW,
1688 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *,
1691 struct partial_symtab *);
1693 static void get_scope_pc_bounds (struct die_info *,
1694 CORE_ADDR *, CORE_ADDR *,
1695 struct dwarf2_cu *);
1697 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1698 CORE_ADDR, struct dwarf2_cu *);
1700 static void dwarf2_add_field (struct field_info *, struct die_info *,
1701 struct dwarf2_cu *);
1703 static void dwarf2_attach_fields_to_type (struct field_info *,
1704 struct type *, struct dwarf2_cu *);
1706 static void dwarf2_add_member_fn (struct field_info *,
1707 struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1710 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1711 struct type *,
1712 struct dwarf2_cu *);
1714 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1716 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1718 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1720 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1722 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1724 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1726 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1728 static struct type *read_module_type (struct die_info *die,
1729 struct dwarf2_cu *cu);
1731 static const char *namespace_name (struct die_info *die,
1732 int *is_anonymous, struct dwarf2_cu *);
1734 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1736 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1738 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1739 struct dwarf2_cu *);
1741 static struct die_info *read_die_and_siblings_1
1742 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1743 struct die_info *);
1745 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1746 const gdb_byte *info_ptr,
1747 const gdb_byte **new_info_ptr,
1748 struct die_info *parent);
1750 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1751 struct die_info **, const gdb_byte *,
1752 int *, int);
1754 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1755 struct die_info **, const gdb_byte *,
1756 int *);
1758 static void process_die (struct die_info *, struct dwarf2_cu *);
1760 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1761 struct obstack *);
1763 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1765 static const char *dwarf2_full_name (const char *name,
1766 struct die_info *die,
1767 struct dwarf2_cu *cu);
1769 static const char *dwarf2_physname (const char *name, struct die_info *die,
1770 struct dwarf2_cu *cu);
1772 static struct die_info *dwarf2_extension (struct die_info *die,
1773 struct dwarf2_cu **);
1775 static const char *dwarf_tag_name (unsigned int);
1777 static const char *dwarf_attr_name (unsigned int);
1779 static const char *dwarf_unit_type_name (int unit_type);
1781 static const char *dwarf_form_name (unsigned int);
1783 static const char *dwarf_bool_name (unsigned int);
1785 static const char *dwarf_type_encoding_name (unsigned int);
1787 static struct die_info *sibling_die (struct die_info *);
1789 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1791 static void dump_die_for_error (struct die_info *);
1793 static void dump_die_1 (struct ui_file *, int level, int max_level,
1794 struct die_info *);
1796 /*static*/ void dump_die (struct die_info *, int max_level);
1798 static void store_in_ref_table (struct die_info *,
1799 struct dwarf2_cu *);
1801 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1803 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1805 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1806 const struct attribute *,
1807 struct dwarf2_cu **);
1809 static struct die_info *follow_die_ref (struct die_info *,
1810 const struct attribute *,
1811 struct dwarf2_cu **);
1813 static struct die_info *follow_die_sig (struct die_info *,
1814 const struct attribute *,
1815 struct dwarf2_cu **);
1817 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1818 struct dwarf2_cu *);
1820 static struct type *get_DW_AT_signature_type (struct die_info *,
1821 const struct attribute *,
1822 struct dwarf2_cu *);
1824 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1826 static void read_signatured_type (struct signatured_type *);
1828 static int attr_to_dynamic_prop (const struct attribute *attr,
1829 struct die_info *die, struct dwarf2_cu *cu,
1830 struct dynamic_prop *prop, struct type *type);
1832 /* memory allocation interface */
1834 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1836 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1838 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1840 static int attr_form_is_block (const struct attribute *);
1842 static int attr_form_is_section_offset (const struct attribute *);
1844 static int attr_form_is_constant (const struct attribute *);
1846 static int attr_form_is_ref (const struct attribute *);
1848 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1849 struct dwarf2_loclist_baton *baton,
1850 const struct attribute *attr);
1852 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1853 struct symbol *sym,
1854 struct dwarf2_cu *cu,
1855 int is_block);
1857 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1858 const gdb_byte *info_ptr,
1859 struct abbrev_info *abbrev);
1861 static hashval_t partial_die_hash (const void *item);
1863 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1865 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1866 (sect_offset sect_off, unsigned int offset_in_dwz,
1867 struct dwarf2_per_objfile *dwarf2_per_objfile);
1869 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1870 struct die_info *comp_unit_die,
1871 enum language pretend_language);
1873 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1875 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1877 static struct type *set_die_type (struct die_info *, struct type *,
1878 struct dwarf2_cu *);
1880 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1882 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1884 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1885 enum language);
1887 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1888 enum language);
1890 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1891 enum language);
1893 static void dwarf2_add_dependence (struct dwarf2_cu *,
1894 struct dwarf2_per_cu_data *);
1896 static void dwarf2_mark (struct dwarf2_cu *);
1898 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1900 static struct type *get_die_type_at_offset (sect_offset,
1901 struct dwarf2_per_cu_data *);
1903 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1905 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1906 enum language pretend_language);
1908 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1910 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1911 static struct type *dwarf2_per_cu_addr_sized_int_type
1912 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1913 static struct type *dwarf2_per_cu_int_type
1914 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1915 bool unsigned_p);
1917 /* Class, the destructor of which frees all allocated queue entries. This
1918 will only have work to do if an error was thrown while processing the
1919 dwarf. If no error was thrown then the queue entries should have all
1920 been processed, and freed, as we went along. */
1922 class dwarf2_queue_guard
1924 public:
1925 dwarf2_queue_guard () = default;
1927 /* Free any entries remaining on the queue. There should only be
1928 entries left if we hit an error while processing the dwarf. */
1929 ~dwarf2_queue_guard ()
1931 struct dwarf2_queue_item *item, *last;
1933 item = dwarf2_queue;
1934 while (item)
1936 /* Anything still marked queued is likely to be in an
1937 inconsistent state, so discard it. */
1938 if (item->per_cu->queued)
1940 if (item->per_cu->cu != NULL)
1941 free_one_cached_comp_unit (item->per_cu);
1942 item->per_cu->queued = 0;
1945 last = item;
1946 item = item->next;
1947 xfree (last);
1950 dwarf2_queue = dwarf2_queue_tail = NULL;
1954 /* The return type of find_file_and_directory. Note, the enclosed
1955 string pointers are only valid while this object is valid. */
1957 struct file_and_directory
1959 /* The filename. This is never NULL. */
1960 const char *name;
1962 /* The compilation directory. NULL if not known. If we needed to
1963 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1964 points directly to the DW_AT_comp_dir string attribute owned by
1965 the obstack that owns the DIE. */
1966 const char *comp_dir;
1968 /* If we needed to build a new string for comp_dir, this is what
1969 owns the storage. */
1970 std::string comp_dir_storage;
1973 static file_and_directory find_file_and_directory (struct die_info *die,
1974 struct dwarf2_cu *cu);
1976 static char *file_full_name (int file, struct line_header *lh,
1977 const char *comp_dir);
1979 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1980 enum class rcuh_kind { COMPILE, TYPE };
1982 static const gdb_byte *read_and_check_comp_unit_head
1983 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1984 struct comp_unit_head *header,
1985 struct dwarf2_section_info *section,
1986 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1987 rcuh_kind section_kind);
1989 static void init_cutu_and_read_dies
1990 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1991 int use_existing_cu, int keep, bool skip_partial,
1992 die_reader_func_ftype *die_reader_func, void *data);
1994 static void init_cutu_and_read_dies_simple
1995 (struct dwarf2_per_cu_data *this_cu,
1996 die_reader_func_ftype *die_reader_func, void *data);
1998 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2000 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2002 static struct dwo_unit *lookup_dwo_unit_in_dwp
2003 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2004 struct dwp_file *dwp_file, const char *comp_dir,
2005 ULONGEST signature, int is_debug_types);
2007 static struct dwp_file *get_dwp_file
2008 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2010 static struct dwo_unit *lookup_dwo_comp_unit
2011 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2013 static struct dwo_unit *lookup_dwo_type_unit
2014 (struct signatured_type *, const char *, const char *);
2016 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2018 /* A unique pointer to a dwo_file. */
2020 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2022 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2024 static void check_producer (struct dwarf2_cu *cu);
2026 static void free_line_header_voidp (void *arg);
2028 /* Various complaints about symbol reading that don't abort the process. */
2030 static void
2031 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2033 complaint (_("statement list doesn't fit in .debug_line section"));
2036 static void
2037 dwarf2_debug_line_missing_file_complaint (void)
2039 complaint (_(".debug_line section has line data without a file"));
2042 static void
2043 dwarf2_debug_line_missing_end_sequence_complaint (void)
2045 complaint (_(".debug_line section has line "
2046 "program sequence without an end"));
2049 static void
2050 dwarf2_complex_location_expr_complaint (void)
2052 complaint (_("location expression too complex"));
2055 static void
2056 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2057 int arg3)
2059 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2060 arg1, arg2, arg3);
2063 static void
2064 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2066 complaint (_("debug info runs off end of %s section"
2067 " [in module %s]"),
2068 get_section_name (section),
2069 get_section_file_name (section));
2072 static void
2073 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2075 complaint (_("macro debug info contains a "
2076 "malformed macro definition:\n`%s'"),
2077 arg1);
2080 static void
2081 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2083 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2084 arg1, arg2);
2087 /* Hash function for line_header_hash. */
2089 static hashval_t
2090 line_header_hash (const struct line_header *ofs)
2092 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2095 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2097 static hashval_t
2098 line_header_hash_voidp (const void *item)
2100 const struct line_header *ofs = (const struct line_header *) item;
2102 return line_header_hash (ofs);
2105 /* Equality function for line_header_hash. */
2107 static int
2108 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2110 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2111 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2113 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2114 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2119 /* Read the given attribute value as an address, taking the attribute's
2120 form into account. */
2122 static CORE_ADDR
2123 attr_value_as_address (struct attribute *attr)
2125 CORE_ADDR addr;
2127 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2128 && attr->form != DW_FORM_GNU_addr_index)
2130 /* Aside from a few clearly defined exceptions, attributes that
2131 contain an address must always be in DW_FORM_addr form.
2132 Unfortunately, some compilers happen to be violating this
2133 requirement by encoding addresses using other forms, such
2134 as DW_FORM_data4 for example. For those broken compilers,
2135 we try to do our best, without any guarantee of success,
2136 to interpret the address correctly. It would also be nice
2137 to generate a complaint, but that would require us to maintain
2138 a list of legitimate cases where a non-address form is allowed,
2139 as well as update callers to pass in at least the CU's DWARF
2140 version. This is more overhead than what we're willing to
2141 expand for a pretty rare case. */
2142 addr = DW_UNSND (attr);
2144 else
2145 addr = DW_ADDR (attr);
2147 return addr;
2150 /* See declaration. */
2152 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2153 const dwarf2_debug_sections *names,
2154 bool can_copy_)
2155 : objfile (objfile_),
2156 can_copy (can_copy_)
2158 if (names == NULL)
2159 names = &dwarf2_elf_names;
2161 bfd *obfd = objfile->obfd;
2163 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2164 locate_sections (obfd, sec, *names);
2167 dwarf2_per_objfile::~dwarf2_per_objfile ()
2169 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2170 free_cached_comp_units ();
2172 if (quick_file_names_table)
2173 htab_delete (quick_file_names_table);
2175 if (line_header_hash)
2176 htab_delete (line_header_hash);
2178 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2179 per_cu->imported_symtabs_free ();
2181 for (signatured_type *sig_type : all_type_units)
2182 sig_type->per_cu.imported_symtabs_free ();
2184 /* Everything else should be on the objfile obstack. */
2187 /* See declaration. */
2189 void
2190 dwarf2_per_objfile::free_cached_comp_units ()
2192 dwarf2_per_cu_data *per_cu = read_in_chain;
2193 dwarf2_per_cu_data **last_chain = &read_in_chain;
2194 while (per_cu != NULL)
2196 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2198 delete per_cu->cu;
2199 *last_chain = next_cu;
2200 per_cu = next_cu;
2204 /* A helper class that calls free_cached_comp_units on
2205 destruction. */
2207 class free_cached_comp_units
2209 public:
2211 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2212 : m_per_objfile (per_objfile)
2216 ~free_cached_comp_units ()
2218 m_per_objfile->free_cached_comp_units ();
2221 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2223 private:
2225 dwarf2_per_objfile *m_per_objfile;
2228 /* Try to locate the sections we need for DWARF 2 debugging
2229 information and return true if we have enough to do something.
2230 NAMES points to the dwarf2 section names, or is NULL if the standard
2231 ELF names are used. CAN_COPY is true for formats where symbol
2232 interposition is possible and so symbol values must follow copy
2233 relocation rules. */
2236 dwarf2_has_info (struct objfile *objfile,
2237 const struct dwarf2_debug_sections *names,
2238 bool can_copy)
2240 if (objfile->flags & OBJF_READNEVER)
2241 return 0;
2243 struct dwarf2_per_objfile *dwarf2_per_objfile
2244 = get_dwarf2_per_objfile (objfile);
2246 if (dwarf2_per_objfile == NULL)
2247 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2248 names,
2249 can_copy);
2251 return (!dwarf2_per_objfile->info.is_virtual
2252 && dwarf2_per_objfile->info.s.section != NULL
2253 && !dwarf2_per_objfile->abbrev.is_virtual
2254 && dwarf2_per_objfile->abbrev.s.section != NULL);
2257 /* Return the containing section of virtual section SECTION. */
2259 static struct dwarf2_section_info *
2260 get_containing_section (const struct dwarf2_section_info *section)
2262 gdb_assert (section->is_virtual);
2263 return section->s.containing_section;
2266 /* Return the bfd owner of SECTION. */
2268 static struct bfd *
2269 get_section_bfd_owner (const struct dwarf2_section_info *section)
2271 if (section->is_virtual)
2273 section = get_containing_section (section);
2274 gdb_assert (!section->is_virtual);
2276 return section->s.section->owner;
2279 /* Return the bfd section of SECTION.
2280 Returns NULL if the section is not present. */
2282 static asection *
2283 get_section_bfd_section (const struct dwarf2_section_info *section)
2285 if (section->is_virtual)
2287 section = get_containing_section (section);
2288 gdb_assert (!section->is_virtual);
2290 return section->s.section;
2293 /* Return the name of SECTION. */
2295 static const char *
2296 get_section_name (const struct dwarf2_section_info *section)
2298 asection *sectp = get_section_bfd_section (section);
2300 gdb_assert (sectp != NULL);
2301 return bfd_section_name (sectp);
2304 /* Return the name of the file SECTION is in. */
2306 static const char *
2307 get_section_file_name (const struct dwarf2_section_info *section)
2309 bfd *abfd = get_section_bfd_owner (section);
2311 return bfd_get_filename (abfd);
2314 /* Return the id of SECTION.
2315 Returns 0 if SECTION doesn't exist. */
2317 static int
2318 get_section_id (const struct dwarf2_section_info *section)
2320 asection *sectp = get_section_bfd_section (section);
2322 if (sectp == NULL)
2323 return 0;
2324 return sectp->id;
2327 /* Return the flags of SECTION.
2328 SECTION (or containing section if this is a virtual section) must exist. */
2330 static int
2331 get_section_flags (const struct dwarf2_section_info *section)
2333 asection *sectp = get_section_bfd_section (section);
2335 gdb_assert (sectp != NULL);
2336 return bfd_section_flags (sectp);
2339 /* When loading sections, we look either for uncompressed section or for
2340 compressed section names. */
2342 static int
2343 section_is_p (const char *section_name,
2344 const struct dwarf2_section_names *names)
2346 if (names->normal != NULL
2347 && strcmp (section_name, names->normal) == 0)
2348 return 1;
2349 if (names->compressed != NULL
2350 && strcmp (section_name, names->compressed) == 0)
2351 return 1;
2352 return 0;
2355 /* See declaration. */
2357 void
2358 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2359 const dwarf2_debug_sections &names)
2361 flagword aflag = bfd_section_flags (sectp);
2363 if ((aflag & SEC_HAS_CONTENTS) == 0)
2366 else if (elf_section_data (sectp)->this_hdr.sh_size
2367 > bfd_get_file_size (abfd))
2369 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2370 warning (_("Discarding section %s which has a section size (%s"
2371 ") larger than the file size [in module %s]"),
2372 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2373 bfd_get_filename (abfd));
2375 else if (section_is_p (sectp->name, &names.info))
2377 this->info.s.section = sectp;
2378 this->info.size = bfd_section_size (sectp);
2380 else if (section_is_p (sectp->name, &names.abbrev))
2382 this->abbrev.s.section = sectp;
2383 this->abbrev.size = bfd_section_size (sectp);
2385 else if (section_is_p (sectp->name, &names.line))
2387 this->line.s.section = sectp;
2388 this->line.size = bfd_section_size (sectp);
2390 else if (section_is_p (sectp->name, &names.loc))
2392 this->loc.s.section = sectp;
2393 this->loc.size = bfd_section_size (sectp);
2395 else if (section_is_p (sectp->name, &names.loclists))
2397 this->loclists.s.section = sectp;
2398 this->loclists.size = bfd_section_size (sectp);
2400 else if (section_is_p (sectp->name, &names.macinfo))
2402 this->macinfo.s.section = sectp;
2403 this->macinfo.size = bfd_section_size (sectp);
2405 else if (section_is_p (sectp->name, &names.macro))
2407 this->macro.s.section = sectp;
2408 this->macro.size = bfd_section_size (sectp);
2410 else if (section_is_p (sectp->name, &names.str))
2412 this->str.s.section = sectp;
2413 this->str.size = bfd_section_size (sectp);
2415 else if (section_is_p (sectp->name, &names.line_str))
2417 this->line_str.s.section = sectp;
2418 this->line_str.size = bfd_section_size (sectp);
2420 else if (section_is_p (sectp->name, &names.addr))
2422 this->addr.s.section = sectp;
2423 this->addr.size = bfd_section_size (sectp);
2425 else if (section_is_p (sectp->name, &names.frame))
2427 this->frame.s.section = sectp;
2428 this->frame.size = bfd_section_size (sectp);
2430 else if (section_is_p (sectp->name, &names.eh_frame))
2432 this->eh_frame.s.section = sectp;
2433 this->eh_frame.size = bfd_section_size (sectp);
2435 else if (section_is_p (sectp->name, &names.ranges))
2437 this->ranges.s.section = sectp;
2438 this->ranges.size = bfd_section_size (sectp);
2440 else if (section_is_p (sectp->name, &names.rnglists))
2442 this->rnglists.s.section = sectp;
2443 this->rnglists.size = bfd_section_size (sectp);
2445 else if (section_is_p (sectp->name, &names.types))
2447 struct dwarf2_section_info type_section;
2449 memset (&type_section, 0, sizeof (type_section));
2450 type_section.s.section = sectp;
2451 type_section.size = bfd_section_size (sectp);
2453 this->types.push_back (type_section);
2455 else if (section_is_p (sectp->name, &names.gdb_index))
2457 this->gdb_index.s.section = sectp;
2458 this->gdb_index.size = bfd_section_size (sectp);
2460 else if (section_is_p (sectp->name, &names.debug_names))
2462 this->debug_names.s.section = sectp;
2463 this->debug_names.size = bfd_section_size (sectp);
2465 else if (section_is_p (sectp->name, &names.debug_aranges))
2467 this->debug_aranges.s.section = sectp;
2468 this->debug_aranges.size = bfd_section_size (sectp);
2471 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2472 && bfd_section_vma (sectp) == 0)
2473 this->has_section_at_zero = true;
2476 /* A helper function that decides whether a section is empty,
2477 or not present. */
2479 static int
2480 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2482 if (section->is_virtual)
2483 return section->size == 0;
2484 return section->s.section == NULL || section->size == 0;
2487 /* See dwarf2read.h. */
2489 void
2490 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2492 asection *sectp;
2493 bfd *abfd;
2494 gdb_byte *buf, *retbuf;
2496 if (info->readin)
2497 return;
2498 info->buffer = NULL;
2499 info->readin = true;
2501 if (dwarf2_section_empty_p (info))
2502 return;
2504 sectp = get_section_bfd_section (info);
2506 /* If this is a virtual section we need to read in the real one first. */
2507 if (info->is_virtual)
2509 struct dwarf2_section_info *containing_section =
2510 get_containing_section (info);
2512 gdb_assert (sectp != NULL);
2513 if ((sectp->flags & SEC_RELOC) != 0)
2515 error (_("Dwarf Error: DWP format V2 with relocations is not"
2516 " supported in section %s [in module %s]"),
2517 get_section_name (info), get_section_file_name (info));
2519 dwarf2_read_section (objfile, containing_section);
2520 /* Other code should have already caught virtual sections that don't
2521 fit. */
2522 gdb_assert (info->virtual_offset + info->size
2523 <= containing_section->size);
2524 /* If the real section is empty or there was a problem reading the
2525 section we shouldn't get here. */
2526 gdb_assert (containing_section->buffer != NULL);
2527 info->buffer = containing_section->buffer + info->virtual_offset;
2528 return;
2531 /* If the section has relocations, we must read it ourselves.
2532 Otherwise we attach it to the BFD. */
2533 if ((sectp->flags & SEC_RELOC) == 0)
2535 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2536 return;
2539 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2540 info->buffer = buf;
2542 /* When debugging .o files, we may need to apply relocations; see
2543 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2544 We never compress sections in .o files, so we only need to
2545 try this when the section is not compressed. */
2546 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2547 if (retbuf != NULL)
2549 info->buffer = retbuf;
2550 return;
2553 abfd = get_section_bfd_owner (info);
2554 gdb_assert (abfd != NULL);
2556 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2557 || bfd_bread (buf, info->size, abfd) != info->size)
2559 error (_("Dwarf Error: Can't read DWARF data"
2560 " in section %s [in module %s]"),
2561 bfd_section_name (sectp), bfd_get_filename (abfd));
2565 /* A helper function that returns the size of a section in a safe way.
2566 If you are positive that the section has been read before using the
2567 size, then it is safe to refer to the dwarf2_section_info object's
2568 "size" field directly. In other cases, you must call this
2569 function, because for compressed sections the size field is not set
2570 correctly until the section has been read. */
2572 static bfd_size_type
2573 dwarf2_section_size (struct objfile *objfile,
2574 struct dwarf2_section_info *info)
2576 if (!info->readin)
2577 dwarf2_read_section (objfile, info);
2578 return info->size;
2581 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2582 SECTION_NAME. */
2584 void
2585 dwarf2_get_section_info (struct objfile *objfile,
2586 enum dwarf2_section_enum sect,
2587 asection **sectp, const gdb_byte **bufp,
2588 bfd_size_type *sizep)
2590 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2591 struct dwarf2_section_info *info;
2593 /* We may see an objfile without any DWARF, in which case we just
2594 return nothing. */
2595 if (data == NULL)
2597 *sectp = NULL;
2598 *bufp = NULL;
2599 *sizep = 0;
2600 return;
2602 switch (sect)
2604 case DWARF2_DEBUG_FRAME:
2605 info = &data->frame;
2606 break;
2607 case DWARF2_EH_FRAME:
2608 info = &data->eh_frame;
2609 break;
2610 default:
2611 gdb_assert_not_reached ("unexpected section");
2614 dwarf2_read_section (objfile, info);
2616 *sectp = get_section_bfd_section (info);
2617 *bufp = info->buffer;
2618 *sizep = info->size;
2621 /* A helper function to find the sections for a .dwz file. */
2623 static void
2624 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2626 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2628 /* Note that we only support the standard ELF names, because .dwz
2629 is ELF-only (at the time of writing). */
2630 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2632 dwz_file->abbrev.s.section = sectp;
2633 dwz_file->abbrev.size = bfd_section_size (sectp);
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2637 dwz_file->info.s.section = sectp;
2638 dwz_file->info.size = bfd_section_size (sectp);
2640 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2642 dwz_file->str.s.section = sectp;
2643 dwz_file->str.size = bfd_section_size (sectp);
2645 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2647 dwz_file->line.s.section = sectp;
2648 dwz_file->line.size = bfd_section_size (sectp);
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2652 dwz_file->macro.s.section = sectp;
2653 dwz_file->macro.size = bfd_section_size (sectp);
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2657 dwz_file->gdb_index.s.section = sectp;
2658 dwz_file->gdb_index.size = bfd_section_size (sectp);
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2662 dwz_file->debug_names.s.section = sectp;
2663 dwz_file->debug_names.size = bfd_section_size (sectp);
2667 /* See dwarf2read.h. */
2669 struct dwz_file *
2670 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2672 const char *filename;
2673 bfd_size_type buildid_len_arg;
2674 size_t buildid_len;
2675 bfd_byte *buildid;
2677 if (dwarf2_per_objfile->dwz_file != NULL)
2678 return dwarf2_per_objfile->dwz_file.get ();
2680 bfd_set_error (bfd_error_no_error);
2681 gdb::unique_xmalloc_ptr<char> data
2682 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2683 &buildid_len_arg, &buildid));
2684 if (data == NULL)
2686 if (bfd_get_error () == bfd_error_no_error)
2687 return NULL;
2688 error (_("could not read '.gnu_debugaltlink' section: %s"),
2689 bfd_errmsg (bfd_get_error ()));
2692 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2694 buildid_len = (size_t) buildid_len_arg;
2696 filename = data.get ();
2698 std::string abs_storage;
2699 if (!IS_ABSOLUTE_PATH (filename))
2701 gdb::unique_xmalloc_ptr<char> abs
2702 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2704 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2705 filename = abs_storage.c_str ();
2708 /* First try the file name given in the section. If that doesn't
2709 work, try to use the build-id instead. */
2710 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2711 if (dwz_bfd != NULL)
2713 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2714 dwz_bfd.reset (nullptr);
2717 if (dwz_bfd == NULL)
2718 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2720 if (dwz_bfd == NULL)
2721 error (_("could not find '.gnu_debugaltlink' file for %s"),
2722 objfile_name (dwarf2_per_objfile->objfile));
2724 std::unique_ptr<struct dwz_file> result
2725 (new struct dwz_file (std::move (dwz_bfd)));
2727 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2728 result.get ());
2730 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2731 result->dwz_bfd.get ());
2732 dwarf2_per_objfile->dwz_file = std::move (result);
2733 return dwarf2_per_objfile->dwz_file.get ();
2736 /* DWARF quick_symbols_functions support. */
2738 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2739 unique line tables, so we maintain a separate table of all .debug_line
2740 derived entries to support the sharing.
2741 All the quick functions need is the list of file names. We discard the
2742 line_header when we're done and don't need to record it here. */
2743 struct quick_file_names
2745 /* The data used to construct the hash key. */
2746 struct stmt_list_hash hash;
2748 /* The number of entries in file_names, real_names. */
2749 unsigned int num_file_names;
2751 /* The file names from the line table, after being run through
2752 file_full_name. */
2753 const char **file_names;
2755 /* The file names from the line table after being run through
2756 gdb_realpath. These are computed lazily. */
2757 const char **real_names;
2760 /* When using the index (and thus not using psymtabs), each CU has an
2761 object of this type. This is used to hold information needed by
2762 the various "quick" methods. */
2763 struct dwarf2_per_cu_quick_data
2765 /* The file table. This can be NULL if there was no file table
2766 or it's currently not read in.
2767 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2768 struct quick_file_names *file_names;
2770 /* The corresponding symbol table. This is NULL if symbols for this
2771 CU have not yet been read. */
2772 struct compunit_symtab *compunit_symtab;
2774 /* A temporary mark bit used when iterating over all CUs in
2775 expand_symtabs_matching. */
2776 unsigned int mark : 1;
2778 /* True if we've tried to read the file table and found there isn't one.
2779 There will be no point in trying to read it again next time. */
2780 unsigned int no_file_data : 1;
2783 /* Utility hash function for a stmt_list_hash. */
2785 static hashval_t
2786 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2788 hashval_t v = 0;
2790 if (stmt_list_hash->dwo_unit != NULL)
2791 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2792 v += to_underlying (stmt_list_hash->line_sect_off);
2793 return v;
2796 /* Utility equality function for a stmt_list_hash. */
2798 static int
2799 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2800 const struct stmt_list_hash *rhs)
2802 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2803 return 0;
2804 if (lhs->dwo_unit != NULL
2805 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2806 return 0;
2808 return lhs->line_sect_off == rhs->line_sect_off;
2811 /* Hash function for a quick_file_names. */
2813 static hashval_t
2814 hash_file_name_entry (const void *e)
2816 const struct quick_file_names *file_data
2817 = (const struct quick_file_names *) e;
2819 return hash_stmt_list_entry (&file_data->hash);
2822 /* Equality function for a quick_file_names. */
2824 static int
2825 eq_file_name_entry (const void *a, const void *b)
2827 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2828 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2830 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2833 /* Delete function for a quick_file_names. */
2835 static void
2836 delete_file_name_entry (void *e)
2838 struct quick_file_names *file_data = (struct quick_file_names *) e;
2839 int i;
2841 for (i = 0; i < file_data->num_file_names; ++i)
2843 xfree ((void*) file_data->file_names[i]);
2844 if (file_data->real_names)
2845 xfree ((void*) file_data->real_names[i]);
2848 /* The space for the struct itself lives on objfile_obstack,
2849 so we don't free it here. */
2852 /* Create a quick_file_names hash table. */
2854 static htab_t
2855 create_quick_file_names_table (unsigned int nr_initial_entries)
2857 return htab_create_alloc (nr_initial_entries,
2858 hash_file_name_entry, eq_file_name_entry,
2859 delete_file_name_entry, xcalloc, xfree);
2862 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2863 have to be created afterwards. You should call age_cached_comp_units after
2864 processing PER_CU->CU. dw2_setup must have been already called. */
2866 static void
2867 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2869 if (per_cu->is_debug_types)
2870 load_full_type_unit (per_cu);
2871 else
2872 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2874 if (per_cu->cu == NULL)
2875 return; /* Dummy CU. */
2877 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2880 /* Read in the symbols for PER_CU. */
2882 static void
2883 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2885 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2887 /* Skip type_unit_groups, reading the type units they contain
2888 is handled elsewhere. */
2889 if (IS_TYPE_UNIT_GROUP (per_cu))
2890 return;
2892 /* The destructor of dwarf2_queue_guard frees any entries left on
2893 the queue. After this point we're guaranteed to leave this function
2894 with the dwarf queue empty. */
2895 dwarf2_queue_guard q_guard;
2897 if (dwarf2_per_objfile->using_index
2898 ? per_cu->v.quick->compunit_symtab == NULL
2899 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2901 queue_comp_unit (per_cu, language_minimal);
2902 load_cu (per_cu, skip_partial);
2904 /* If we just loaded a CU from a DWO, and we're working with an index
2905 that may badly handle TUs, load all the TUs in that DWO as well.
2906 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2907 if (!per_cu->is_debug_types
2908 && per_cu->cu != NULL
2909 && per_cu->cu->dwo_unit != NULL
2910 && dwarf2_per_objfile->index_table != NULL
2911 && dwarf2_per_objfile->index_table->version <= 7
2912 /* DWP files aren't supported yet. */
2913 && get_dwp_file (dwarf2_per_objfile) == NULL)
2914 queue_and_load_all_dwo_tus (per_cu);
2917 process_queue (dwarf2_per_objfile);
2919 /* Age the cache, releasing compilation units that have not
2920 been used recently. */
2921 age_cached_comp_units (dwarf2_per_objfile);
2924 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2925 the objfile from which this CU came. Returns the resulting symbol
2926 table. */
2928 static struct compunit_symtab *
2929 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2931 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2933 gdb_assert (dwarf2_per_objfile->using_index);
2934 if (!per_cu->v.quick->compunit_symtab)
2936 free_cached_comp_units freer (dwarf2_per_objfile);
2937 scoped_restore decrementer = increment_reading_symtab ();
2938 dw2_do_instantiate_symtab (per_cu, skip_partial);
2939 process_cu_includes (dwarf2_per_objfile);
2942 return per_cu->v.quick->compunit_symtab;
2945 /* See declaration. */
2947 dwarf2_per_cu_data *
2948 dwarf2_per_objfile::get_cutu (int index)
2950 if (index >= this->all_comp_units.size ())
2952 index -= this->all_comp_units.size ();
2953 gdb_assert (index < this->all_type_units.size ());
2954 return &this->all_type_units[index]->per_cu;
2957 return this->all_comp_units[index];
2960 /* See declaration. */
2962 dwarf2_per_cu_data *
2963 dwarf2_per_objfile::get_cu (int index)
2965 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2967 return this->all_comp_units[index];
2970 /* See declaration. */
2972 signatured_type *
2973 dwarf2_per_objfile::get_tu (int index)
2975 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2977 return this->all_type_units[index];
2980 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2981 objfile_obstack, and constructed with the specified field
2982 values. */
2984 static dwarf2_per_cu_data *
2985 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2986 struct dwarf2_section_info *section,
2987 int is_dwz,
2988 sect_offset sect_off, ULONGEST length)
2990 struct objfile *objfile = dwarf2_per_objfile->objfile;
2991 dwarf2_per_cu_data *the_cu
2992 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2993 struct dwarf2_per_cu_data);
2994 the_cu->sect_off = sect_off;
2995 the_cu->length = length;
2996 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2997 the_cu->section = section;
2998 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2999 struct dwarf2_per_cu_quick_data);
3000 the_cu->is_dwz = is_dwz;
3001 return the_cu;
3004 /* A helper for create_cus_from_index that handles a given list of
3005 CUs. */
3007 static void
3008 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3009 const gdb_byte *cu_list, offset_type n_elements,
3010 struct dwarf2_section_info *section,
3011 int is_dwz)
3013 for (offset_type i = 0; i < n_elements; i += 2)
3015 gdb_static_assert (sizeof (ULONGEST) >= 8);
3017 sect_offset sect_off
3018 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3019 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3020 cu_list += 2 * 8;
3022 dwarf2_per_cu_data *per_cu
3023 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3024 sect_off, length);
3025 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3029 /* Read the CU list from the mapped index, and use it to create all
3030 the CU objects for this objfile. */
3032 static void
3033 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3034 const gdb_byte *cu_list, offset_type cu_list_elements,
3035 const gdb_byte *dwz_list, offset_type dwz_elements)
3037 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3038 dwarf2_per_objfile->all_comp_units.reserve
3039 ((cu_list_elements + dwz_elements) / 2);
3041 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3042 &dwarf2_per_objfile->info, 0);
3044 if (dwz_elements == 0)
3045 return;
3047 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3048 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3049 &dwz->info, 1);
3052 /* Create the signatured type hash table from the index. */
3054 static void
3055 create_signatured_type_table_from_index
3056 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3057 struct dwarf2_section_info *section,
3058 const gdb_byte *bytes,
3059 offset_type elements)
3061 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3064 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3066 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3068 for (offset_type i = 0; i < elements; i += 3)
3070 struct signatured_type *sig_type;
3071 ULONGEST signature;
3072 void **slot;
3073 cu_offset type_offset_in_tu;
3075 gdb_static_assert (sizeof (ULONGEST) >= 8);
3076 sect_offset sect_off
3077 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3078 type_offset_in_tu
3079 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3080 BFD_ENDIAN_LITTLE);
3081 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3082 bytes += 3 * 8;
3084 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3085 struct signatured_type);
3086 sig_type->signature = signature;
3087 sig_type->type_offset_in_tu = type_offset_in_tu;
3088 sig_type->per_cu.is_debug_types = 1;
3089 sig_type->per_cu.section = section;
3090 sig_type->per_cu.sect_off = sect_off;
3091 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3092 sig_type->per_cu.v.quick
3093 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3094 struct dwarf2_per_cu_quick_data);
3096 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3097 *slot = sig_type;
3099 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3102 dwarf2_per_objfile->signatured_types = sig_types_hash;
3105 /* Create the signatured type hash table from .debug_names. */
3107 static void
3108 create_signatured_type_table_from_debug_names
3109 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3110 const mapped_debug_names &map,
3111 struct dwarf2_section_info *section,
3112 struct dwarf2_section_info *abbrev_section)
3114 struct objfile *objfile = dwarf2_per_objfile->objfile;
3116 dwarf2_read_section (objfile, section);
3117 dwarf2_read_section (objfile, abbrev_section);
3119 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3120 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3122 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3124 for (uint32_t i = 0; i < map.tu_count; ++i)
3126 struct signatured_type *sig_type;
3127 void **slot;
3129 sect_offset sect_off
3130 = (sect_offset) (extract_unsigned_integer
3131 (map.tu_table_reordered + i * map.offset_size,
3132 map.offset_size,
3133 map.dwarf5_byte_order));
3135 comp_unit_head cu_header;
3136 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3137 abbrev_section,
3138 section->buffer + to_underlying (sect_off),
3139 rcuh_kind::TYPE);
3141 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct signatured_type);
3143 sig_type->signature = cu_header.signature;
3144 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3145 sig_type->per_cu.is_debug_types = 1;
3146 sig_type->per_cu.section = section;
3147 sig_type->per_cu.sect_off = sect_off;
3148 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3149 sig_type->per_cu.v.quick
3150 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3151 struct dwarf2_per_cu_quick_data);
3153 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3154 *slot = sig_type;
3156 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3159 dwarf2_per_objfile->signatured_types = sig_types_hash;
3162 /* Read the address map data from the mapped index, and use it to
3163 populate the objfile's psymtabs_addrmap. */
3165 static void
3166 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3167 struct mapped_index *index)
3169 struct objfile *objfile = dwarf2_per_objfile->objfile;
3170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3171 const gdb_byte *iter, *end;
3172 struct addrmap *mutable_map;
3173 CORE_ADDR baseaddr;
3175 auto_obstack temp_obstack;
3177 mutable_map = addrmap_create_mutable (&temp_obstack);
3179 iter = index->address_table.data ();
3180 end = iter + index->address_table.size ();
3182 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184 while (iter < end)
3186 ULONGEST hi, lo, cu_index;
3187 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3188 iter += 8;
3189 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3190 iter += 8;
3191 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3192 iter += 4;
3194 if (lo > hi)
3196 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3197 hex_string (lo), hex_string (hi));
3198 continue;
3201 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3203 complaint (_(".gdb_index address table has invalid CU number %u"),
3204 (unsigned) cu_index);
3205 continue;
3208 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3209 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3210 addrmap_set_empty (mutable_map, lo, hi - 1,
3211 dwarf2_per_objfile->get_cu (cu_index));
3214 objfile->partial_symtabs->psymtabs_addrmap
3215 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3218 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3219 populate the objfile's psymtabs_addrmap. */
3221 static void
3222 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3223 struct dwarf2_section_info *section)
3225 struct objfile *objfile = dwarf2_per_objfile->objfile;
3226 bfd *abfd = objfile->obfd;
3227 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3228 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3229 SECT_OFF_TEXT (objfile));
3231 auto_obstack temp_obstack;
3232 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3234 std::unordered_map<sect_offset,
3235 dwarf2_per_cu_data *,
3236 gdb::hash_enum<sect_offset>>
3237 debug_info_offset_to_per_cu;
3238 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3240 const auto insertpair
3241 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3242 if (!insertpair.second)
3244 warning (_("Section .debug_aranges in %s has duplicate "
3245 "debug_info_offset %s, ignoring .debug_aranges."),
3246 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3247 return;
3251 dwarf2_read_section (objfile, section);
3253 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3255 const gdb_byte *addr = section->buffer;
3257 while (addr < section->buffer + section->size)
3259 const gdb_byte *const entry_addr = addr;
3260 unsigned int bytes_read;
3262 const LONGEST entry_length = read_initial_length (abfd, addr,
3263 &bytes_read);
3264 addr += bytes_read;
3266 const gdb_byte *const entry_end = addr + entry_length;
3267 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3268 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3269 if (addr + entry_length > section->buffer + section->size)
3271 warning (_("Section .debug_aranges in %s entry at offset %s "
3272 "length %s exceeds section length %s, "
3273 "ignoring .debug_aranges."),
3274 objfile_name (objfile),
3275 plongest (entry_addr - section->buffer),
3276 plongest (bytes_read + entry_length),
3277 pulongest (section->size));
3278 return;
3281 /* The version number. */
3282 const uint16_t version = read_2_bytes (abfd, addr);
3283 addr += 2;
3284 if (version != 2)
3286 warning (_("Section .debug_aranges in %s entry at offset %s "
3287 "has unsupported version %d, ignoring .debug_aranges."),
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer), version);
3290 return;
3293 const uint64_t debug_info_offset
3294 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3295 addr += offset_size;
3296 const auto per_cu_it
3297 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3298 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3300 warning (_("Section .debug_aranges in %s entry at offset %s "
3301 "debug_info_offset %s does not exists, "
3302 "ignoring .debug_aranges."),
3303 objfile_name (objfile),
3304 plongest (entry_addr - section->buffer),
3305 pulongest (debug_info_offset));
3306 return;
3308 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3310 const uint8_t address_size = *addr++;
3311 if (address_size < 1 || address_size > 8)
3313 warning (_("Section .debug_aranges in %s entry at offset %s "
3314 "address_size %u is invalid, ignoring .debug_aranges."),
3315 objfile_name (objfile),
3316 plongest (entry_addr - section->buffer), address_size);
3317 return;
3320 const uint8_t segment_selector_size = *addr++;
3321 if (segment_selector_size != 0)
3323 warning (_("Section .debug_aranges in %s entry at offset %s "
3324 "segment_selector_size %u is not supported, "
3325 "ignoring .debug_aranges."),
3326 objfile_name (objfile),
3327 plongest (entry_addr - section->buffer),
3328 segment_selector_size);
3329 return;
3332 /* Must pad to an alignment boundary that is twice the address
3333 size. It is undocumented by the DWARF standard but GCC does
3334 use it. */
3335 for (size_t padding = ((-(addr - section->buffer))
3336 & (2 * address_size - 1));
3337 padding > 0; padding--)
3338 if (*addr++ != 0)
3340 warning (_("Section .debug_aranges in %s entry at offset %s "
3341 "padding is not zero, ignoring .debug_aranges."),
3342 objfile_name (objfile),
3343 plongest (entry_addr - section->buffer));
3344 return;
3347 for (;;)
3349 if (addr + 2 * address_size > entry_end)
3351 warning (_("Section .debug_aranges in %s entry at offset %s "
3352 "address list is not properly terminated, "
3353 "ignoring .debug_aranges."),
3354 objfile_name (objfile),
3355 plongest (entry_addr - section->buffer));
3356 return;
3358 ULONGEST start = extract_unsigned_integer (addr, address_size,
3359 dwarf5_byte_order);
3360 addr += address_size;
3361 ULONGEST length = extract_unsigned_integer (addr, address_size,
3362 dwarf5_byte_order);
3363 addr += address_size;
3364 if (start == 0 && length == 0)
3365 break;
3366 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3368 /* Symbol was eliminated due to a COMDAT group. */
3369 continue;
3371 ULONGEST end = start + length;
3372 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3373 - baseaddr);
3374 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3375 - baseaddr);
3376 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3380 objfile->partial_symtabs->psymtabs_addrmap
3381 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3384 /* Find a slot in the mapped index INDEX for the object named NAME.
3385 If NAME is found, set *VEC_OUT to point to the CU vector in the
3386 constant pool and return true. If NAME cannot be found, return
3387 false. */
3389 static bool
3390 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3391 offset_type **vec_out)
3393 offset_type hash;
3394 offset_type slot, step;
3395 int (*cmp) (const char *, const char *);
3397 gdb::unique_xmalloc_ptr<char> without_params;
3398 if (current_language->la_language == language_cplus
3399 || current_language->la_language == language_fortran
3400 || current_language->la_language == language_d)
3402 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3403 not contain any. */
3405 if (strchr (name, '(') != NULL)
3407 without_params = cp_remove_params (name);
3409 if (without_params != NULL)
3410 name = without_params.get ();
3414 /* Index version 4 did not support case insensitive searches. But the
3415 indices for case insensitive languages are built in lowercase, therefore
3416 simulate our NAME being searched is also lowercased. */
3417 hash = mapped_index_string_hash ((index->version == 4
3418 && case_sensitivity == case_sensitive_off
3419 ? 5 : index->version),
3420 name);
3422 slot = hash & (index->symbol_table.size () - 1);
3423 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3424 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3426 for (;;)
3428 const char *str;
3430 const auto &bucket = index->symbol_table[slot];
3431 if (bucket.name == 0 && bucket.vec == 0)
3432 return false;
3434 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3435 if (!cmp (name, str))
3437 *vec_out = (offset_type *) (index->constant_pool
3438 + MAYBE_SWAP (bucket.vec));
3439 return true;
3442 slot = (slot + step) & (index->symbol_table.size () - 1);
3446 /* A helper function that reads the .gdb_index from BUFFER and fills
3447 in MAP. FILENAME is the name of the file containing the data;
3448 it is used for error reporting. DEPRECATED_OK is true if it is
3449 ok to use deprecated sections.
3451 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3452 out parameters that are filled in with information about the CU and
3453 TU lists in the section.
3455 Returns true if all went well, false otherwise. */
3457 static bool
3458 read_gdb_index_from_buffer (struct objfile *objfile,
3459 const char *filename,
3460 bool deprecated_ok,
3461 gdb::array_view<const gdb_byte> buffer,
3462 struct mapped_index *map,
3463 const gdb_byte **cu_list,
3464 offset_type *cu_list_elements,
3465 const gdb_byte **types_list,
3466 offset_type *types_list_elements)
3468 const gdb_byte *addr = &buffer[0];
3470 /* Version check. */
3471 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3472 /* Versions earlier than 3 emitted every copy of a psymbol. This
3473 causes the index to behave very poorly for certain requests. Version 3
3474 contained incomplete addrmap. So, it seems better to just ignore such
3475 indices. */
3476 if (version < 4)
3478 static int warning_printed = 0;
3479 if (!warning_printed)
3481 warning (_("Skipping obsolete .gdb_index section in %s."),
3482 filename);
3483 warning_printed = 1;
3485 return 0;
3487 /* Index version 4 uses a different hash function than index version
3488 5 and later.
3490 Versions earlier than 6 did not emit psymbols for inlined
3491 functions. Using these files will cause GDB not to be able to
3492 set breakpoints on inlined functions by name, so we ignore these
3493 indices unless the user has done
3494 "set use-deprecated-index-sections on". */
3495 if (version < 6 && !deprecated_ok)
3497 static int warning_printed = 0;
3498 if (!warning_printed)
3500 warning (_("\
3501 Skipping deprecated .gdb_index section in %s.\n\
3502 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3503 to use the section anyway."),
3504 filename);
3505 warning_printed = 1;
3507 return 0;
3509 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3510 of the TU (for symbols coming from TUs),
3511 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3512 Plus gold-generated indices can have duplicate entries for global symbols,
3513 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3514 These are just performance bugs, and we can't distinguish gdb-generated
3515 indices from gold-generated ones, so issue no warning here. */
3517 /* Indexes with higher version than the one supported by GDB may be no
3518 longer backward compatible. */
3519 if (version > 8)
3520 return 0;
3522 map->version = version;
3524 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3526 int i = 0;
3527 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3528 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3529 / 8);
3530 ++i;
3532 *types_list = addr + MAYBE_SWAP (metadata[i]);
3533 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3534 - MAYBE_SWAP (metadata[i]))
3535 / 8);
3536 ++i;
3538 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3539 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3540 map->address_table
3541 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3542 ++i;
3544 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3545 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3546 map->symbol_table
3547 = gdb::array_view<mapped_index::symbol_table_slot>
3548 ((mapped_index::symbol_table_slot *) symbol_table,
3549 (mapped_index::symbol_table_slot *) symbol_table_end);
3551 ++i;
3552 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3554 return 1;
3557 /* Callback types for dwarf2_read_gdb_index. */
3559 typedef gdb::function_view
3560 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3561 get_gdb_index_contents_ftype;
3562 typedef gdb::function_view
3563 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3564 get_gdb_index_contents_dwz_ftype;
3566 /* Read .gdb_index. If everything went ok, initialize the "quick"
3567 elements of all the CUs and return 1. Otherwise, return 0. */
3569 static int
3570 dwarf2_read_gdb_index
3571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3572 get_gdb_index_contents_ftype get_gdb_index_contents,
3573 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3575 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3576 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3577 struct dwz_file *dwz;
3578 struct objfile *objfile = dwarf2_per_objfile->objfile;
3580 gdb::array_view<const gdb_byte> main_index_contents
3581 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3583 if (main_index_contents.empty ())
3584 return 0;
3586 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3587 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3588 use_deprecated_index_sections,
3589 main_index_contents, map.get (), &cu_list,
3590 &cu_list_elements, &types_list,
3591 &types_list_elements))
3592 return 0;
3594 /* Don't use the index if it's empty. */
3595 if (map->symbol_table.empty ())
3596 return 0;
3598 /* If there is a .dwz file, read it so we can get its CU list as
3599 well. */
3600 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3601 if (dwz != NULL)
3603 struct mapped_index dwz_map;
3604 const gdb_byte *dwz_types_ignore;
3605 offset_type dwz_types_elements_ignore;
3607 gdb::array_view<const gdb_byte> dwz_index_content
3608 = get_gdb_index_contents_dwz (objfile, dwz);
3610 if (dwz_index_content.empty ())
3611 return 0;
3613 if (!read_gdb_index_from_buffer (objfile,
3614 bfd_get_filename (dwz->dwz_bfd.get ()),
3615 1, dwz_index_content, &dwz_map,
3616 &dwz_list, &dwz_list_elements,
3617 &dwz_types_ignore,
3618 &dwz_types_elements_ignore))
3620 warning (_("could not read '.gdb_index' section from %s; skipping"),
3621 bfd_get_filename (dwz->dwz_bfd.get ()));
3622 return 0;
3626 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3627 dwz_list, dwz_list_elements);
3629 if (types_list_elements)
3631 /* We can only handle a single .debug_types when we have an
3632 index. */
3633 if (dwarf2_per_objfile->types.size () != 1)
3634 return 0;
3636 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3638 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3639 types_list, types_list_elements);
3642 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3644 dwarf2_per_objfile->index_table = std::move (map);
3645 dwarf2_per_objfile->using_index = 1;
3646 dwarf2_per_objfile->quick_file_names_table =
3647 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3649 return 1;
3652 /* die_reader_func for dw2_get_file_names. */
3654 static void
3655 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3656 const gdb_byte *info_ptr,
3657 struct die_info *comp_unit_die,
3658 int has_children,
3659 void *data)
3661 struct dwarf2_cu *cu = reader->cu;
3662 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3663 struct dwarf2_per_objfile *dwarf2_per_objfile
3664 = cu->per_cu->dwarf2_per_objfile;
3665 struct objfile *objfile = dwarf2_per_objfile->objfile;
3666 struct dwarf2_per_cu_data *lh_cu;
3667 struct attribute *attr;
3668 void **slot;
3669 struct quick_file_names *qfn;
3671 gdb_assert (! this_cu->is_debug_types);
3673 /* Our callers never want to match partial units -- instead they
3674 will match the enclosing full CU. */
3675 if (comp_unit_die->tag == DW_TAG_partial_unit)
3677 this_cu->v.quick->no_file_data = 1;
3678 return;
3681 lh_cu = this_cu;
3682 slot = NULL;
3684 line_header_up lh;
3685 sect_offset line_offset {};
3687 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3688 if (attr != nullptr)
3690 struct quick_file_names find_entry;
3692 line_offset = (sect_offset) DW_UNSND (attr);
3694 /* We may have already read in this line header (TU line header sharing).
3695 If we have we're done. */
3696 find_entry.hash.dwo_unit = cu->dwo_unit;
3697 find_entry.hash.line_sect_off = line_offset;
3698 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3699 &find_entry, INSERT);
3700 if (*slot != NULL)
3702 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3703 return;
3706 lh = dwarf_decode_line_header (line_offset, cu);
3708 if (lh == NULL)
3710 lh_cu->v.quick->no_file_data = 1;
3711 return;
3714 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3715 qfn->hash.dwo_unit = cu->dwo_unit;
3716 qfn->hash.line_sect_off = line_offset;
3717 gdb_assert (slot != NULL);
3718 *slot = qfn;
3720 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3722 int offset = 0;
3723 if (strcmp (fnd.name, "<unknown>") != 0)
3724 ++offset;
3726 qfn->num_file_names = offset + lh->file_names_size ();
3727 qfn->file_names =
3728 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3729 if (offset != 0)
3730 qfn->file_names[0] = xstrdup (fnd.name);
3731 for (int i = 0; i < lh->file_names_size (); ++i)
3732 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3733 qfn->real_names = NULL;
3735 lh_cu->v.quick->file_names = qfn;
3738 /* A helper for the "quick" functions which attempts to read the line
3739 table for THIS_CU. */
3741 static struct quick_file_names *
3742 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3744 /* This should never be called for TUs. */
3745 gdb_assert (! this_cu->is_debug_types);
3746 /* Nor type unit groups. */
3747 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3749 if (this_cu->v.quick->file_names != NULL)
3750 return this_cu->v.quick->file_names;
3751 /* If we know there is no line data, no point in looking again. */
3752 if (this_cu->v.quick->no_file_data)
3753 return NULL;
3755 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3757 if (this_cu->v.quick->no_file_data)
3758 return NULL;
3759 return this_cu->v.quick->file_names;
3762 /* A helper for the "quick" functions which computes and caches the
3763 real path for a given file name from the line table. */
3765 static const char *
3766 dw2_get_real_path (struct objfile *objfile,
3767 struct quick_file_names *qfn, int index)
3769 if (qfn->real_names == NULL)
3770 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3771 qfn->num_file_names, const char *);
3773 if (qfn->real_names[index] == NULL)
3774 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3776 return qfn->real_names[index];
3779 static struct symtab *
3780 dw2_find_last_source_symtab (struct objfile *objfile)
3782 struct dwarf2_per_objfile *dwarf2_per_objfile
3783 = get_dwarf2_per_objfile (objfile);
3784 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3785 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3787 if (cust == NULL)
3788 return NULL;
3790 return compunit_primary_filetab (cust);
3793 /* Traversal function for dw2_forget_cached_source_info. */
3795 static int
3796 dw2_free_cached_file_names (void **slot, void *info)
3798 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3800 if (file_data->real_names)
3802 int i;
3804 for (i = 0; i < file_data->num_file_names; ++i)
3806 xfree ((void*) file_data->real_names[i]);
3807 file_data->real_names[i] = NULL;
3811 return 1;
3814 static void
3815 dw2_forget_cached_source_info (struct objfile *objfile)
3817 struct dwarf2_per_objfile *dwarf2_per_objfile
3818 = get_dwarf2_per_objfile (objfile);
3820 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3821 dw2_free_cached_file_names, NULL);
3824 /* Helper function for dw2_map_symtabs_matching_filename that expands
3825 the symtabs and calls the iterator. */
3827 static int
3828 dw2_map_expand_apply (struct objfile *objfile,
3829 struct dwarf2_per_cu_data *per_cu,
3830 const char *name, const char *real_path,
3831 gdb::function_view<bool (symtab *)> callback)
3833 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3835 /* Don't visit already-expanded CUs. */
3836 if (per_cu->v.quick->compunit_symtab)
3837 return 0;
3839 /* This may expand more than one symtab, and we want to iterate over
3840 all of them. */
3841 dw2_instantiate_symtab (per_cu, false);
3843 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3844 last_made, callback);
3847 /* Implementation of the map_symtabs_matching_filename method. */
3849 static bool
3850 dw2_map_symtabs_matching_filename
3851 (struct objfile *objfile, const char *name, const char *real_path,
3852 gdb::function_view<bool (symtab *)> callback)
3854 const char *name_basename = lbasename (name);
3855 struct dwarf2_per_objfile *dwarf2_per_objfile
3856 = get_dwarf2_per_objfile (objfile);
3858 /* The rule is CUs specify all the files, including those used by
3859 any TU, so there's no need to scan TUs here. */
3861 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3863 /* We only need to look at symtabs not already expanded. */
3864 if (per_cu->v.quick->compunit_symtab)
3865 continue;
3867 quick_file_names *file_data = dw2_get_file_names (per_cu);
3868 if (file_data == NULL)
3869 continue;
3871 for (int j = 0; j < file_data->num_file_names; ++j)
3873 const char *this_name = file_data->file_names[j];
3874 const char *this_real_name;
3876 if (compare_filenames_for_search (this_name, name))
3878 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3879 callback))
3880 return true;
3881 continue;
3884 /* Before we invoke realpath, which can get expensive when many
3885 files are involved, do a quick comparison of the basenames. */
3886 if (! basenames_may_differ
3887 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3888 continue;
3890 this_real_name = dw2_get_real_path (objfile, file_data, j);
3891 if (compare_filenames_for_search (this_real_name, name))
3893 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3894 callback))
3895 return true;
3896 continue;
3899 if (real_path != NULL)
3901 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3902 gdb_assert (IS_ABSOLUTE_PATH (name));
3903 if (this_real_name != NULL
3904 && FILENAME_CMP (real_path, this_real_name) == 0)
3906 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3907 callback))
3908 return true;
3909 continue;
3915 return false;
3918 /* Struct used to manage iterating over all CUs looking for a symbol. */
3920 struct dw2_symtab_iterator
3922 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3923 struct dwarf2_per_objfile *dwarf2_per_objfile;
3924 /* If set, only look for symbols that match that block. Valid values are
3925 GLOBAL_BLOCK and STATIC_BLOCK. */
3926 gdb::optional<block_enum> block_index;
3927 /* The kind of symbol we're looking for. */
3928 domain_enum domain;
3929 /* The list of CUs from the index entry of the symbol,
3930 or NULL if not found. */
3931 offset_type *vec;
3932 /* The next element in VEC to look at. */
3933 int next;
3934 /* The number of elements in VEC, or zero if there is no match. */
3935 int length;
3936 /* Have we seen a global version of the symbol?
3937 If so we can ignore all further global instances.
3938 This is to work around gold/15646, inefficient gold-generated
3939 indices. */
3940 int global_seen;
3943 /* Initialize the index symtab iterator ITER. */
3945 static void
3946 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3947 struct dwarf2_per_objfile *dwarf2_per_objfile,
3948 gdb::optional<block_enum> block_index,
3949 domain_enum domain,
3950 const char *name)
3952 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3953 iter->block_index = block_index;
3954 iter->domain = domain;
3955 iter->next = 0;
3956 iter->global_seen = 0;
3958 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3960 /* index is NULL if OBJF_READNOW. */
3961 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3962 iter->length = MAYBE_SWAP (*iter->vec);
3963 else
3965 iter->vec = NULL;
3966 iter->length = 0;
3970 /* Return the next matching CU or NULL if there are no more. */
3972 static struct dwarf2_per_cu_data *
3973 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3975 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3977 for ( ; iter->next < iter->length; ++iter->next)
3979 offset_type cu_index_and_attrs =
3980 MAYBE_SWAP (iter->vec[iter->next + 1]);
3981 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3982 gdb_index_symbol_kind symbol_kind =
3983 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3984 /* Only check the symbol attributes if they're present.
3985 Indices prior to version 7 don't record them,
3986 and indices >= 7 may elide them for certain symbols
3987 (gold does this). */
3988 int attrs_valid =
3989 (dwarf2_per_objfile->index_table->version >= 7
3990 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3992 /* Don't crash on bad data. */
3993 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3994 + dwarf2_per_objfile->all_type_units.size ()))
3996 complaint (_(".gdb_index entry has bad CU index"
3997 " [in module %s]"),
3998 objfile_name (dwarf2_per_objfile->objfile));
3999 continue;
4002 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4004 /* Skip if already read in. */
4005 if (per_cu->v.quick->compunit_symtab)
4006 continue;
4008 /* Check static vs global. */
4009 if (attrs_valid)
4011 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4013 if (iter->block_index.has_value ())
4015 bool want_static = *iter->block_index == STATIC_BLOCK;
4017 if (is_static != want_static)
4018 continue;
4021 /* Work around gold/15646. */
4022 if (!is_static && iter->global_seen)
4023 continue;
4024 if (!is_static)
4025 iter->global_seen = 1;
4028 /* Only check the symbol's kind if it has one. */
4029 if (attrs_valid)
4031 switch (iter->domain)
4033 case VAR_DOMAIN:
4034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4035 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4036 /* Some types are also in VAR_DOMAIN. */
4037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4038 continue;
4039 break;
4040 case STRUCT_DOMAIN:
4041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4042 continue;
4043 break;
4044 case LABEL_DOMAIN:
4045 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4046 continue;
4047 break;
4048 case MODULE_DOMAIN:
4049 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4050 continue;
4051 break;
4052 default:
4053 break;
4057 ++iter->next;
4058 return per_cu;
4061 return NULL;
4064 static struct compunit_symtab *
4065 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4066 const char *name, domain_enum domain)
4068 struct compunit_symtab *stab_best = NULL;
4069 struct dwarf2_per_objfile *dwarf2_per_objfile
4070 = get_dwarf2_per_objfile (objfile);
4072 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4074 struct dw2_symtab_iterator iter;
4075 struct dwarf2_per_cu_data *per_cu;
4077 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4079 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4081 struct symbol *sym, *with_opaque = NULL;
4082 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4083 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4084 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4086 sym = block_find_symbol (block, name, domain,
4087 block_find_non_opaque_type_preferred,
4088 &with_opaque);
4090 /* Some caution must be observed with overloaded functions
4091 and methods, since the index will not contain any overload
4092 information (but NAME might contain it). */
4094 if (sym != NULL
4095 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4096 return stab;
4097 if (with_opaque != NULL
4098 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4099 stab_best = stab;
4101 /* Keep looking through other CUs. */
4104 return stab_best;
4107 static void
4108 dw2_print_stats (struct objfile *objfile)
4110 struct dwarf2_per_objfile *dwarf2_per_objfile
4111 = get_dwarf2_per_objfile (objfile);
4112 int total = (dwarf2_per_objfile->all_comp_units.size ()
4113 + dwarf2_per_objfile->all_type_units.size ());
4114 int count = 0;
4116 for (int i = 0; i < total; ++i)
4118 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4120 if (!per_cu->v.quick->compunit_symtab)
4121 ++count;
4123 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4124 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4127 /* This dumps minimal information about the index.
4128 It is called via "mt print objfiles".
4129 One use is to verify .gdb_index has been loaded by the
4130 gdb.dwarf2/gdb-index.exp testcase. */
4132 static void
4133 dw2_dump (struct objfile *objfile)
4135 struct dwarf2_per_objfile *dwarf2_per_objfile
4136 = get_dwarf2_per_objfile (objfile);
4138 gdb_assert (dwarf2_per_objfile->using_index);
4139 printf_filtered (".gdb_index:");
4140 if (dwarf2_per_objfile->index_table != NULL)
4142 printf_filtered (" version %d\n",
4143 dwarf2_per_objfile->index_table->version);
4145 else
4146 printf_filtered (" faked for \"readnow\"\n");
4147 printf_filtered ("\n");
4150 static void
4151 dw2_expand_symtabs_for_function (struct objfile *objfile,
4152 const char *func_name)
4154 struct dwarf2_per_objfile *dwarf2_per_objfile
4155 = get_dwarf2_per_objfile (objfile);
4157 struct dw2_symtab_iterator iter;
4158 struct dwarf2_per_cu_data *per_cu;
4160 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4162 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4163 dw2_instantiate_symtab (per_cu, false);
4167 static void
4168 dw2_expand_all_symtabs (struct objfile *objfile)
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
4172 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4173 + dwarf2_per_objfile->all_type_units.size ());
4175 for (int i = 0; i < total_units; ++i)
4177 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4179 /* We don't want to directly expand a partial CU, because if we
4180 read it with the wrong language, then assertion failures can
4181 be triggered later on. See PR symtab/23010. So, tell
4182 dw2_instantiate_symtab to skip partial CUs -- any important
4183 partial CU will be read via DW_TAG_imported_unit anyway. */
4184 dw2_instantiate_symtab (per_cu, true);
4188 static void
4189 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4190 const char *fullname)
4192 struct dwarf2_per_objfile *dwarf2_per_objfile
4193 = get_dwarf2_per_objfile (objfile);
4195 /* We don't need to consider type units here.
4196 This is only called for examining code, e.g. expand_line_sal.
4197 There can be an order of magnitude (or more) more type units
4198 than comp units, and we avoid them if we can. */
4200 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4202 /* We only need to look at symtabs not already expanded. */
4203 if (per_cu->v.quick->compunit_symtab)
4204 continue;
4206 quick_file_names *file_data = dw2_get_file_names (per_cu);
4207 if (file_data == NULL)
4208 continue;
4210 for (int j = 0; j < file_data->num_file_names; ++j)
4212 const char *this_fullname = file_data->file_names[j];
4214 if (filename_cmp (this_fullname, fullname) == 0)
4216 dw2_instantiate_symtab (per_cu, false);
4217 break;
4223 static void
4224 dw2_map_matching_symbols
4225 (struct objfile *objfile,
4226 const lookup_name_info &name, domain_enum domain,
4227 int global,
4228 gdb::function_view<symbol_found_callback_ftype> callback,
4229 symbol_compare_ftype *ordered_compare)
4231 /* Currently unimplemented; used for Ada. The function can be called if the
4232 current language is Ada for a non-Ada objfile using GNU index. As Ada
4233 does not look for non-Ada symbols this function should just return. */
4236 /* Starting from a search name, return the string that finds the upper
4237 bound of all strings that start with SEARCH_NAME in a sorted name
4238 list. Returns the empty string to indicate that the upper bound is
4239 the end of the list. */
4241 static std::string
4242 make_sort_after_prefix_name (const char *search_name)
4244 /* When looking to complete "func", we find the upper bound of all
4245 symbols that start with "func" by looking for where we'd insert
4246 the closest string that would follow "func" in lexicographical
4247 order. Usually, that's "func"-with-last-character-incremented,
4248 i.e. "fund". Mind non-ASCII characters, though. Usually those
4249 will be UTF-8 multi-byte sequences, but we can't be certain.
4250 Especially mind the 0xff character, which is a valid character in
4251 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4252 rule out compilers allowing it in identifiers. Note that
4253 conveniently, strcmp/strcasecmp are specified to compare
4254 characters interpreted as unsigned char. So what we do is treat
4255 the whole string as a base 256 number composed of a sequence of
4256 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4257 to 0, and carries 1 to the following more-significant position.
4258 If the very first character in SEARCH_NAME ends up incremented
4259 and carries/overflows, then the upper bound is the end of the
4260 list. The string after the empty string is also the empty
4261 string.
4263 Some examples of this operation:
4265 SEARCH_NAME => "+1" RESULT
4267 "abc" => "abd"
4268 "ab\xff" => "ac"
4269 "\xff" "a" "\xff" => "\xff" "b"
4270 "\xff" => ""
4271 "\xff\xff" => ""
4272 "" => ""
4274 Then, with these symbols for example:
4276 func
4277 func1
4278 fund
4280 completing "func" looks for symbols between "func" and
4281 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4282 which finds "func" and "func1", but not "fund".
4284 And with:
4286 funcÿ (Latin1 'ÿ' [0xff])
4287 funcÿ1
4288 fund
4290 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4291 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4293 And with:
4295 ÿÿ (Latin1 'ÿ' [0xff])
4296 ÿÿ1
4298 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4299 the end of the list.
4301 std::string after = search_name;
4302 while (!after.empty () && (unsigned char) after.back () == 0xff)
4303 after.pop_back ();
4304 if (!after.empty ())
4305 after.back () = (unsigned char) after.back () + 1;
4306 return after;
4309 /* See declaration. */
4311 std::pair<std::vector<name_component>::const_iterator,
4312 std::vector<name_component>::const_iterator>
4313 mapped_index_base::find_name_components_bounds
4314 (const lookup_name_info &lookup_name_without_params, language lang) const
4316 auto *name_cmp
4317 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4319 const char *lang_name
4320 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4322 /* Comparison function object for lower_bound that matches against a
4323 given symbol name. */
4324 auto lookup_compare_lower = [&] (const name_component &elem,
4325 const char *name)
4327 const char *elem_qualified = this->symbol_name_at (elem.idx);
4328 const char *elem_name = elem_qualified + elem.name_offset;
4329 return name_cmp (elem_name, name) < 0;
4332 /* Comparison function object for upper_bound that matches against a
4333 given symbol name. */
4334 auto lookup_compare_upper = [&] (const char *name,
4335 const name_component &elem)
4337 const char *elem_qualified = this->symbol_name_at (elem.idx);
4338 const char *elem_name = elem_qualified + elem.name_offset;
4339 return name_cmp (name, elem_name) < 0;
4342 auto begin = this->name_components.begin ();
4343 auto end = this->name_components.end ();
4345 /* Find the lower bound. */
4346 auto lower = [&] ()
4348 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4349 return begin;
4350 else
4351 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4352 } ();
4354 /* Find the upper bound. */
4355 auto upper = [&] ()
4357 if (lookup_name_without_params.completion_mode ())
4359 /* In completion mode, we want UPPER to point past all
4360 symbols names that have the same prefix. I.e., with
4361 these symbols, and completing "func":
4363 function << lower bound
4364 function1
4365 other_function << upper bound
4367 We find the upper bound by looking for the insertion
4368 point of "func"-with-last-character-incremented,
4369 i.e. "fund". */
4370 std::string after = make_sort_after_prefix_name (lang_name);
4371 if (after.empty ())
4372 return end;
4373 return std::lower_bound (lower, end, after.c_str (),
4374 lookup_compare_lower);
4376 else
4377 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4378 } ();
4380 return {lower, upper};
4383 /* See declaration. */
4385 void
4386 mapped_index_base::build_name_components ()
4388 if (!this->name_components.empty ())
4389 return;
4391 this->name_components_casing = case_sensitivity;
4392 auto *name_cmp
4393 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4395 /* The code below only knows how to break apart components of C++
4396 symbol names (and other languages that use '::' as
4397 namespace/module separator) and Ada symbol names. */
4398 auto count = this->symbol_name_count ();
4399 for (offset_type idx = 0; idx < count; idx++)
4401 if (this->symbol_name_slot_invalid (idx))
4402 continue;
4404 const char *name = this->symbol_name_at (idx);
4406 /* Add each name component to the name component table. */
4407 unsigned int previous_len = 0;
4409 if (strstr (name, "::") != nullptr)
4411 for (unsigned int current_len = cp_find_first_component (name);
4412 name[current_len] != '\0';
4413 current_len += cp_find_first_component (name + current_len))
4415 gdb_assert (name[current_len] == ':');
4416 this->name_components.push_back ({previous_len, idx});
4417 /* Skip the '::'. */
4418 current_len += 2;
4419 previous_len = current_len;
4422 else
4424 /* Handle the Ada encoded (aka mangled) form here. */
4425 for (const char *iter = strstr (name, "__");
4426 iter != nullptr;
4427 iter = strstr (iter, "__"))
4429 this->name_components.push_back ({previous_len, idx});
4430 iter += 2;
4431 previous_len = iter - name;
4435 this->name_components.push_back ({previous_len, idx});
4438 /* Sort name_components elements by name. */
4439 auto name_comp_compare = [&] (const name_component &left,
4440 const name_component &right)
4442 const char *left_qualified = this->symbol_name_at (left.idx);
4443 const char *right_qualified = this->symbol_name_at (right.idx);
4445 const char *left_name = left_qualified + left.name_offset;
4446 const char *right_name = right_qualified + right.name_offset;
4448 return name_cmp (left_name, right_name) < 0;
4451 std::sort (this->name_components.begin (),
4452 this->name_components.end (),
4453 name_comp_compare);
4456 /* Helper for dw2_expand_symtabs_matching that works with a
4457 mapped_index_base instead of the containing objfile. This is split
4458 to a separate function in order to be able to unit test the
4459 name_components matching using a mock mapped_index_base. For each
4460 symbol name that matches, calls MATCH_CALLBACK, passing it the
4461 symbol's index in the mapped_index_base symbol table. */
4463 static void
4464 dw2_expand_symtabs_matching_symbol
4465 (mapped_index_base &index,
4466 const lookup_name_info &lookup_name_in,
4467 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4468 enum search_domain kind,
4469 gdb::function_view<bool (offset_type)> match_callback)
4471 lookup_name_info lookup_name_without_params
4472 = lookup_name_in.make_ignore_params ();
4474 /* Build the symbol name component sorted vector, if we haven't
4475 yet. */
4476 index.build_name_components ();
4478 /* The same symbol may appear more than once in the range though.
4479 E.g., if we're looking for symbols that complete "w", and we have
4480 a symbol named "w1::w2", we'll find the two name components for
4481 that same symbol in the range. To be sure we only call the
4482 callback once per symbol, we first collect the symbol name
4483 indexes that matched in a temporary vector and ignore
4484 duplicates. */
4485 std::vector<offset_type> matches;
4487 struct name_and_matcher
4489 symbol_name_matcher_ftype *matcher;
4490 const std::string &name;
4492 bool operator== (const name_and_matcher &other) const
4494 return matcher == other.matcher && name == other.name;
4498 /* A vector holding all the different symbol name matchers, for all
4499 languages. */
4500 std::vector<name_and_matcher> matchers;
4502 for (int i = 0; i < nr_languages; i++)
4504 enum language lang_e = (enum language) i;
4506 const language_defn *lang = language_def (lang_e);
4507 symbol_name_matcher_ftype *name_matcher
4508 = get_symbol_name_matcher (lang, lookup_name_without_params);
4510 name_and_matcher key {
4511 name_matcher,
4512 lookup_name_without_params.language_lookup_name (lang_e)
4515 /* Don't insert the same comparison routine more than once.
4516 Note that we do this linear walk. This is not a problem in
4517 practice because the number of supported languages is
4518 low. */
4519 if (std::find (matchers.begin (), matchers.end (), key)
4520 != matchers.end ())
4521 continue;
4522 matchers.push_back (std::move (key));
4524 auto bounds
4525 = index.find_name_components_bounds (lookup_name_without_params,
4526 lang_e);
4528 /* Now for each symbol name in range, check to see if we have a name
4529 match, and if so, call the MATCH_CALLBACK callback. */
4531 for (; bounds.first != bounds.second; ++bounds.first)
4533 const char *qualified = index.symbol_name_at (bounds.first->idx);
4535 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4536 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4537 continue;
4539 matches.push_back (bounds.first->idx);
4543 std::sort (matches.begin (), matches.end ());
4545 /* Finally call the callback, once per match. */
4546 ULONGEST prev = -1;
4547 for (offset_type idx : matches)
4549 if (prev != idx)
4551 if (!match_callback (idx))
4552 break;
4553 prev = idx;
4557 /* Above we use a type wider than idx's for 'prev', since 0 and
4558 (offset_type)-1 are both possible values. */
4559 static_assert (sizeof (prev) > sizeof (offset_type), "");
4562 #if GDB_SELF_TEST
4564 namespace selftests { namespace dw2_expand_symtabs_matching {
4566 /* A mock .gdb_index/.debug_names-like name index table, enough to
4567 exercise dw2_expand_symtabs_matching_symbol, which works with the
4568 mapped_index_base interface. Builds an index from the symbol list
4569 passed as parameter to the constructor. */
4570 class mock_mapped_index : public mapped_index_base
4572 public:
4573 mock_mapped_index (gdb::array_view<const char *> symbols)
4574 : m_symbol_table (symbols)
4577 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4579 /* Return the number of names in the symbol table. */
4580 size_t symbol_name_count () const override
4582 return m_symbol_table.size ();
4585 /* Get the name of the symbol at IDX in the symbol table. */
4586 const char *symbol_name_at (offset_type idx) const override
4588 return m_symbol_table[idx];
4591 private:
4592 gdb::array_view<const char *> m_symbol_table;
4595 /* Convenience function that converts a NULL pointer to a "<null>"
4596 string, to pass to print routines. */
4598 static const char *
4599 string_or_null (const char *str)
4601 return str != NULL ? str : "<null>";
4604 /* Check if a lookup_name_info built from
4605 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4606 index. EXPECTED_LIST is the list of expected matches, in expected
4607 matching order. If no match expected, then an empty list is
4608 specified. Returns true on success. On failure prints a warning
4609 indicating the file:line that failed, and returns false. */
4611 static bool
4612 check_match (const char *file, int line,
4613 mock_mapped_index &mock_index,
4614 const char *name, symbol_name_match_type match_type,
4615 bool completion_mode,
4616 std::initializer_list<const char *> expected_list)
4618 lookup_name_info lookup_name (name, match_type, completion_mode);
4620 bool matched = true;
4622 auto mismatch = [&] (const char *expected_str,
4623 const char *got)
4625 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4626 "expected=\"%s\", got=\"%s\"\n"),
4627 file, line,
4628 (match_type == symbol_name_match_type::FULL
4629 ? "FULL" : "WILD"),
4630 name, string_or_null (expected_str), string_or_null (got));
4631 matched = false;
4634 auto expected_it = expected_list.begin ();
4635 auto expected_end = expected_list.end ();
4637 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4638 NULL, ALL_DOMAIN,
4639 [&] (offset_type idx)
4641 const char *matched_name = mock_index.symbol_name_at (idx);
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4645 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4646 mismatch (expected_str, matched_name);
4647 return true;
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652 if (expected_str != NULL)
4653 mismatch (expected_str, NULL);
4655 return matched;
4658 /* The symbols added to the mock mapped_index for testing (in
4659 canonical form). */
4660 static const char *test_symbols[] = {
4661 "function",
4662 "std::bar",
4663 "std::zfunction",
4664 "std::zfunction2",
4665 "w1::w2",
4666 "ns::foo<char*>",
4667 "ns::foo<int>",
4668 "ns::foo<long>",
4669 "ns2::tmpl<int>::foo2",
4670 "(anonymous namespace)::A::B::C",
4672 /* These are used to check that the increment-last-char in the
4673 matching algorithm for completion doesn't match "t1_fund" when
4674 completing "t1_func". */
4675 "t1_func",
4676 "t1_func1",
4677 "t1_fund",
4678 "t1_fund1",
4680 /* A UTF-8 name with multi-byte sequences to make sure that
4681 cp-name-parser understands this as a single identifier ("função"
4682 is "function" in PT). */
4683 u8"u8função",
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "yfunc\377",
4688 /* \377 (0xff) is Latin1 'ÿ'. */
4689 "\377",
4690 "\377\377123",
4692 /* A name with all sorts of complications. Starts with "z" to make
4693 it easier for the completion tests below. */
4694 #define Z_SYM_NAME \
4695 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4696 "::tuple<(anonymous namespace)::ui*, " \
4697 "std::default_delete<(anonymous namespace)::ui>, void>"
4699 Z_SYM_NAME
4702 /* Returns true if the mapped_index_base::find_name_component_bounds
4703 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4704 in completion mode. */
4706 static bool
4707 check_find_bounds_finds (mapped_index_base &index,
4708 const char *search_name,
4709 gdb::array_view<const char *> expected_syms)
4711 lookup_name_info lookup_name (search_name,
4712 symbol_name_match_type::FULL, true);
4714 auto bounds = index.find_name_components_bounds (lookup_name,
4715 language_cplus);
4717 size_t distance = std::distance (bounds.first, bounds.second);
4718 if (distance != expected_syms.size ())
4719 return false;
4721 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4723 auto nc_elem = bounds.first + exp_elem;
4724 const char *qualified = index.symbol_name_at (nc_elem->idx);
4725 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4726 return false;
4729 return true;
4732 /* Test the lower-level mapped_index::find_name_component_bounds
4733 method. */
4735 static void
4736 test_mapped_index_find_name_component_bounds ()
4738 mock_mapped_index mock_index (test_symbols);
4740 mock_index.build_name_components ();
4742 /* Test the lower-level mapped_index::find_name_component_bounds
4743 method in completion mode. */
4745 static const char *expected_syms[] = {
4746 "t1_func",
4747 "t1_func1",
4750 SELF_CHECK (check_find_bounds_finds (mock_index,
4751 "t1_func", expected_syms));
4754 /* Check that the increment-last-char in the name matching algorithm
4755 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4757 static const char *expected_syms1[] = {
4758 "\377",
4759 "\377\377123",
4761 SELF_CHECK (check_find_bounds_finds (mock_index,
4762 "\377", expected_syms1));
4764 static const char *expected_syms2[] = {
4765 "\377\377123",
4767 SELF_CHECK (check_find_bounds_finds (mock_index,
4768 "\377\377", expected_syms2));
4772 /* Test dw2_expand_symtabs_matching_symbol. */
4774 static void
4775 test_dw2_expand_symtabs_matching_symbol ()
4777 mock_mapped_index mock_index (test_symbols);
4779 /* We let all tests run until the end even if some fails, for debug
4780 convenience. */
4781 bool any_mismatch = false;
4783 /* Create the expected symbols list (an initializer_list). Needed
4784 because lists have commas, and we need to pass them to CHECK,
4785 which is a macro. */
4786 #define EXPECT(...) { __VA_ARGS__ }
4788 /* Wrapper for check_match that passes down the current
4789 __FILE__/__LINE__. */
4790 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4791 any_mismatch |= !check_match (__FILE__, __LINE__, \
4792 mock_index, \
4793 NAME, MATCH_TYPE, COMPLETION_MODE, \
4794 EXPECTED_LIST)
4796 /* Identity checks. */
4797 for (const char *sym : test_symbols)
4799 /* Should be able to match all existing symbols. */
4800 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4801 EXPECT (sym));
4803 /* Should be able to match all existing symbols with
4804 parameters. */
4805 std::string with_params = std::string (sym) + "(int)";
4806 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4807 EXPECT (sym));
4809 /* Should be able to match all existing symbols with
4810 parameters and qualifiers. */
4811 with_params = std::string (sym) + " ( int ) const";
4812 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 EXPECT (sym));
4815 /* This should really find sym, but cp-name-parser.y doesn't
4816 know about lvalue/rvalue qualifiers yet. */
4817 with_params = std::string (sym) + " ( int ) &&";
4818 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4819 {});
4822 /* Check that the name matching algorithm for completion doesn't get
4823 confused with Latin1 'ÿ' / 0xff. */
4825 static const char str[] = "\377";
4826 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4827 EXPECT ("\377", "\377\377123"));
4830 /* Check that the increment-last-char in the matching algorithm for
4831 completion doesn't match "t1_fund" when completing "t1_func". */
4833 static const char str[] = "t1_func";
4834 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4835 EXPECT ("t1_func", "t1_func1"));
4838 /* Check that completion mode works at each prefix of the expected
4839 symbol name. */
4841 static const char str[] = "function(int)";
4842 size_t len = strlen (str);
4843 std::string lookup;
4845 for (size_t i = 1; i < len; i++)
4847 lookup.assign (str, i);
4848 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4849 EXPECT ("function"));
4853 /* While "w" is a prefix of both components, the match function
4854 should still only be called once. */
4856 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4857 EXPECT ("w1::w2"));
4858 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4859 EXPECT ("w1::w2"));
4862 /* Same, with a "complicated" symbol. */
4864 static const char str[] = Z_SYM_NAME;
4865 size_t len = strlen (str);
4866 std::string lookup;
4868 for (size_t i = 1; i < len; i++)
4870 lookup.assign (str, i);
4871 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4872 EXPECT (Z_SYM_NAME));
4876 /* In FULL mode, an incomplete symbol doesn't match. */
4878 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4879 {});
4882 /* A complete symbol with parameters matches any overload, since the
4883 index has no overload info. */
4885 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
4887 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4888 EXPECT ("std::zfunction", "std::zfunction2"));
4889 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4890 EXPECT ("std::zfunction", "std::zfunction2"));
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list. */
4896 static const char expected[] = "ns::foo<int>";
4897 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4898 EXPECT (expected));
4899 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4900 EXPECT (expected));
4903 /* Check that whitespace is ignored appropriately. A symbol with a
4904 template argument list that includes a pointer. */
4906 static const char expected[] = "ns::foo<char*>";
4907 /* Try both completion and non-completion modes. */
4908 static const bool completion_mode[2] = {false, true};
4909 for (size_t i = 0; i < 2; i++)
4911 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4912 completion_mode[i], EXPECT (expected));
4913 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4914 completion_mode[i], EXPECT (expected));
4916 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4917 completion_mode[i], EXPECT (expected));
4918 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4919 completion_mode[i], EXPECT (expected));
4924 /* Check method qualifiers are ignored. */
4925 static const char expected[] = "ns::foo<char*>";
4926 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4927 symbol_name_match_type::FULL, true, EXPECT (expected));
4928 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4929 symbol_name_match_type::FULL, true, EXPECT (expected));
4930 CHECK_MATCH ("foo < char * > ( int ) const",
4931 symbol_name_match_type::WILD, true, EXPECT (expected));
4932 CHECK_MATCH ("foo < char * > ( int ) &&",
4933 symbol_name_match_type::WILD, true, EXPECT (expected));
4936 /* Test lookup names that don't match anything. */
4938 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4939 {});
4941 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4942 {});
4945 /* Some wild matching tests, exercising "(anonymous namespace)",
4946 which should not be confused with a parameter list. */
4948 static const char *syms[] = {
4949 "A::B::C",
4950 "B::C",
4951 "C",
4952 "A :: B :: C ( int )",
4953 "B :: C ( int )",
4954 "C ( int )",
4957 for (const char *s : syms)
4959 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4960 EXPECT ("(anonymous namespace)::A::B::C"));
4965 static const char expected[] = "ns2::tmpl<int>::foo2";
4966 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4967 EXPECT (expected));
4968 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4969 EXPECT (expected));
4972 SELF_CHECK (!any_mismatch);
4974 #undef EXPECT
4975 #undef CHECK_MATCH
4978 static void
4979 run_test ()
4981 test_mapped_index_find_name_component_bounds ();
4982 test_dw2_expand_symtabs_matching_symbol ();
4985 }} // namespace selftests::dw2_expand_symtabs_matching
4987 #endif /* GDB_SELF_TEST */
4989 /* If FILE_MATCHER is NULL or if PER_CU has
4990 dwarf2_per_cu_quick_data::MARK set (see
4991 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4992 EXPANSION_NOTIFY on it. */
4994 static void
4995 dw2_expand_symtabs_matching_one
4996 (struct dwarf2_per_cu_data *per_cu,
4997 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4998 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5000 if (file_matcher == NULL || per_cu->v.quick->mark)
5002 bool symtab_was_null
5003 = (per_cu->v.quick->compunit_symtab == NULL);
5005 dw2_instantiate_symtab (per_cu, false);
5007 if (expansion_notify != NULL
5008 && symtab_was_null
5009 && per_cu->v.quick->compunit_symtab != NULL)
5010 expansion_notify (per_cu->v.quick->compunit_symtab);
5014 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5015 matched, to expand corresponding CUs that were marked. IDX is the
5016 index of the symbol name that matched. */
5018 static void
5019 dw2_expand_marked_cus
5020 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5021 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5022 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5023 search_domain kind)
5025 offset_type *vec, vec_len, vec_idx;
5026 bool global_seen = false;
5027 mapped_index &index = *dwarf2_per_objfile->index_table;
5029 vec = (offset_type *) (index.constant_pool
5030 + MAYBE_SWAP (index.symbol_table[idx].vec));
5031 vec_len = MAYBE_SWAP (vec[0]);
5032 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5034 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5035 /* This value is only valid for index versions >= 7. */
5036 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5037 gdb_index_symbol_kind symbol_kind =
5038 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5039 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5040 /* Only check the symbol attributes if they're present.
5041 Indices prior to version 7 don't record them,
5042 and indices >= 7 may elide them for certain symbols
5043 (gold does this). */
5044 int attrs_valid =
5045 (index.version >= 7
5046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5048 /* Work around gold/15646. */
5049 if (attrs_valid)
5051 if (!is_static && global_seen)
5052 continue;
5053 if (!is_static)
5054 global_seen = true;
5057 /* Only check the symbol's kind if it has one. */
5058 if (attrs_valid)
5060 switch (kind)
5062 case VARIABLES_DOMAIN:
5063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5064 continue;
5065 break;
5066 case FUNCTIONS_DOMAIN:
5067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5068 continue;
5069 break;
5070 case TYPES_DOMAIN:
5071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5072 continue;
5073 break;
5074 case MODULES_DOMAIN:
5075 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5076 continue;
5077 break;
5078 default:
5079 break;
5083 /* Don't crash on bad data. */
5084 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5085 + dwarf2_per_objfile->all_type_units.size ()))
5087 complaint (_(".gdb_index entry has bad CU index"
5088 " [in module %s]"),
5089 objfile_name (dwarf2_per_objfile->objfile));
5090 continue;
5093 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5094 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5095 expansion_notify);
5099 /* If FILE_MATCHER is non-NULL, set all the
5100 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5101 that match FILE_MATCHER. */
5103 static void
5104 dw_expand_symtabs_matching_file_matcher
5105 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5108 if (file_matcher == NULL)
5109 return;
5111 objfile *const objfile = dwarf2_per_objfile->objfile;
5113 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5114 htab_eq_pointer,
5115 NULL, xcalloc, xfree));
5116 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5117 htab_eq_pointer,
5118 NULL, xcalloc, xfree));
5120 /* The rule is CUs specify all the files, including those used by
5121 any TU, so there's no need to scan TUs here. */
5123 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5125 QUIT;
5127 per_cu->v.quick->mark = 0;
5129 /* We only need to look at symtabs not already expanded. */
5130 if (per_cu->v.quick->compunit_symtab)
5131 continue;
5133 quick_file_names *file_data = dw2_get_file_names (per_cu);
5134 if (file_data == NULL)
5135 continue;
5137 if (htab_find (visited_not_found.get (), file_data) != NULL)
5138 continue;
5139 else if (htab_find (visited_found.get (), file_data) != NULL)
5141 per_cu->v.quick->mark = 1;
5142 continue;
5145 for (int j = 0; j < file_data->num_file_names; ++j)
5147 const char *this_real_name;
5149 if (file_matcher (file_data->file_names[j], false))
5151 per_cu->v.quick->mark = 1;
5152 break;
5155 /* Before we invoke realpath, which can get expensive when many
5156 files are involved, do a quick comparison of the basenames. */
5157 if (!basenames_may_differ
5158 && !file_matcher (lbasename (file_data->file_names[j]),
5159 true))
5160 continue;
5162 this_real_name = dw2_get_real_path (objfile, file_data, j);
5163 if (file_matcher (this_real_name, false))
5165 per_cu->v.quick->mark = 1;
5166 break;
5170 void **slot = htab_find_slot (per_cu->v.quick->mark
5171 ? visited_found.get ()
5172 : visited_not_found.get (),
5173 file_data, INSERT);
5174 *slot = file_data;
5178 static void
5179 dw2_expand_symtabs_matching
5180 (struct objfile *objfile,
5181 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5182 const lookup_name_info &lookup_name,
5183 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5184 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5185 enum search_domain kind)
5187 struct dwarf2_per_objfile *dwarf2_per_objfile
5188 = get_dwarf2_per_objfile (objfile);
5190 /* index_table is NULL if OBJF_READNOW. */
5191 if (!dwarf2_per_objfile->index_table)
5192 return;
5194 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5196 mapped_index &index = *dwarf2_per_objfile->index_table;
5198 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5199 symbol_matcher,
5200 kind, [&] (offset_type idx)
5202 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5203 expansion_notify, kind);
5204 return true;
5208 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5209 symtab. */
5211 static struct compunit_symtab *
5212 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5213 CORE_ADDR pc)
5215 int i;
5217 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5218 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5219 return cust;
5221 if (cust->includes == NULL)
5222 return NULL;
5224 for (i = 0; cust->includes[i]; ++i)
5226 struct compunit_symtab *s = cust->includes[i];
5228 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5229 if (s != NULL)
5230 return s;
5233 return NULL;
5236 static struct compunit_symtab *
5237 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5238 struct bound_minimal_symbol msymbol,
5239 CORE_ADDR pc,
5240 struct obj_section *section,
5241 int warn_if_readin)
5243 struct dwarf2_per_cu_data *data;
5244 struct compunit_symtab *result;
5246 if (!objfile->partial_symtabs->psymtabs_addrmap)
5247 return NULL;
5249 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5250 SECT_OFF_TEXT (objfile));
5251 data = (struct dwarf2_per_cu_data *) addrmap_find
5252 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5253 if (!data)
5254 return NULL;
5256 if (warn_if_readin && data->v.quick->compunit_symtab)
5257 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5258 paddress (get_objfile_arch (objfile), pc));
5260 result
5261 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5262 false),
5263 pc);
5264 gdb_assert (result != NULL);
5265 return result;
5268 static void
5269 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5270 void *data, int need_fullname)
5272 struct dwarf2_per_objfile *dwarf2_per_objfile
5273 = get_dwarf2_per_objfile (objfile);
5275 if (!dwarf2_per_objfile->filenames_cache)
5277 dwarf2_per_objfile->filenames_cache.emplace ();
5279 htab_up visited (htab_create_alloc (10,
5280 htab_hash_pointer, htab_eq_pointer,
5281 NULL, xcalloc, xfree));
5283 /* The rule is CUs specify all the files, including those used
5284 by any TU, so there's no need to scan TUs here. We can
5285 ignore file names coming from already-expanded CUs. */
5287 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5289 if (per_cu->v.quick->compunit_symtab)
5291 void **slot = htab_find_slot (visited.get (),
5292 per_cu->v.quick->file_names,
5293 INSERT);
5295 *slot = per_cu->v.quick->file_names;
5299 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5301 /* We only need to look at symtabs not already expanded. */
5302 if (per_cu->v.quick->compunit_symtab)
5303 continue;
5305 quick_file_names *file_data = dw2_get_file_names (per_cu);
5306 if (file_data == NULL)
5307 continue;
5309 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5310 if (*slot)
5312 /* Already visited. */
5313 continue;
5315 *slot = file_data;
5317 for (int j = 0; j < file_data->num_file_names; ++j)
5319 const char *filename = file_data->file_names[j];
5320 dwarf2_per_objfile->filenames_cache->seen (filename);
5325 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5327 gdb::unique_xmalloc_ptr<char> this_real_name;
5329 if (need_fullname)
5330 this_real_name = gdb_realpath (filename);
5331 (*fun) (filename, this_real_name.get (), data);
5335 static int
5336 dw2_has_symbols (struct objfile *objfile)
5338 return 1;
5341 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5343 dw2_has_symbols,
5344 dw2_find_last_source_symtab,
5345 dw2_forget_cached_source_info,
5346 dw2_map_symtabs_matching_filename,
5347 dw2_lookup_symbol,
5348 dw2_print_stats,
5349 dw2_dump,
5350 dw2_expand_symtabs_for_function,
5351 dw2_expand_all_symtabs,
5352 dw2_expand_symtabs_with_fullname,
5353 dw2_map_matching_symbols,
5354 dw2_expand_symtabs_matching,
5355 dw2_find_pc_sect_compunit_symtab,
5356 NULL,
5357 dw2_map_symbol_filenames
5360 /* DWARF-5 debug_names reader. */
5362 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5363 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5365 /* A helper function that reads the .debug_names section in SECTION
5366 and fills in MAP. FILENAME is the name of the file containing the
5367 section; it is used for error reporting.
5369 Returns true if all went well, false otherwise. */
5371 static bool
5372 read_debug_names_from_section (struct objfile *objfile,
5373 const char *filename,
5374 struct dwarf2_section_info *section,
5375 mapped_debug_names &map)
5377 if (dwarf2_section_empty_p (section))
5378 return false;
5380 /* Older elfutils strip versions could keep the section in the main
5381 executable while splitting it for the separate debug info file. */
5382 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5383 return false;
5385 dwarf2_read_section (objfile, section);
5387 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5389 const gdb_byte *addr = section->buffer;
5391 bfd *const abfd = get_section_bfd_owner (section);
5393 unsigned int bytes_read;
5394 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5395 addr += bytes_read;
5397 map.dwarf5_is_dwarf64 = bytes_read != 4;
5398 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5399 if (bytes_read + length != section->size)
5401 /* There may be multiple per-CU indices. */
5402 warning (_("Section .debug_names in %s length %s does not match "
5403 "section length %s, ignoring .debug_names."),
5404 filename, plongest (bytes_read + length),
5405 pulongest (section->size));
5406 return false;
5409 /* The version number. */
5410 uint16_t version = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (version != 5)
5414 warning (_("Section .debug_names in %s has unsupported version %d, "
5415 "ignoring .debug_names."),
5416 filename, version);
5417 return false;
5420 /* Padding. */
5421 uint16_t padding = read_2_bytes (abfd, addr);
5422 addr += 2;
5423 if (padding != 0)
5425 warning (_("Section .debug_names in %s has unsupported padding %d, "
5426 "ignoring .debug_names."),
5427 filename, padding);
5428 return false;
5431 /* comp_unit_count - The number of CUs in the CU list. */
5432 map.cu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5435 /* local_type_unit_count - The number of TUs in the local TU
5436 list. */
5437 map.tu_count = read_4_bytes (abfd, addr);
5438 addr += 4;
5440 /* foreign_type_unit_count - The number of TUs in the foreign TU
5441 list. */
5442 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5443 addr += 4;
5444 if (foreign_tu_count != 0)
5446 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5447 "ignoring .debug_names."),
5448 filename, static_cast<unsigned long> (foreign_tu_count));
5449 return false;
5452 /* bucket_count - The number of hash buckets in the hash lookup
5453 table. */
5454 map.bucket_count = read_4_bytes (abfd, addr);
5455 addr += 4;
5457 /* name_count - The number of unique names in the index. */
5458 map.name_count = read_4_bytes (abfd, addr);
5459 addr += 4;
5461 /* abbrev_table_size - The size in bytes of the abbreviations
5462 table. */
5463 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5464 addr += 4;
5466 /* augmentation_string_size - The size in bytes of the augmentation
5467 string. This value is rounded up to a multiple of 4. */
5468 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5469 addr += 4;
5470 map.augmentation_is_gdb = ((augmentation_string_size
5471 == sizeof (dwarf5_augmentation))
5472 && memcmp (addr, dwarf5_augmentation,
5473 sizeof (dwarf5_augmentation)) == 0);
5474 augmentation_string_size += (-augmentation_string_size) & 3;
5475 addr += augmentation_string_size;
5477 /* List of CUs */
5478 map.cu_table_reordered = addr;
5479 addr += map.cu_count * map.offset_size;
5481 /* List of Local TUs */
5482 map.tu_table_reordered = addr;
5483 addr += map.tu_count * map.offset_size;
5485 /* Hash Lookup Table */
5486 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5487 addr += map.bucket_count * 4;
5488 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5489 addr += map.name_count * 4;
5491 /* Name Table */
5492 map.name_table_string_offs_reordered = addr;
5493 addr += map.name_count * map.offset_size;
5494 map.name_table_entry_offs_reordered = addr;
5495 addr += map.name_count * map.offset_size;
5497 const gdb_byte *abbrev_table_start = addr;
5498 for (;;)
5500 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5501 addr += bytes_read;
5502 if (index_num == 0)
5503 break;
5505 const auto insertpair
5506 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5507 if (!insertpair.second)
5509 warning (_("Section .debug_names in %s has duplicate index %s, "
5510 "ignoring .debug_names."),
5511 filename, pulongest (index_num));
5512 return false;
5514 mapped_debug_names::index_val &indexval = insertpair.first->second;
5515 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5518 for (;;)
5520 mapped_debug_names::index_val::attr attr;
5521 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5522 addr += bytes_read;
5523 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5524 addr += bytes_read;
5525 if (attr.form == DW_FORM_implicit_const)
5527 attr.implicit_const = read_signed_leb128 (abfd, addr,
5528 &bytes_read);
5529 addr += bytes_read;
5531 if (attr.dw_idx == 0 && attr.form == 0)
5532 break;
5533 indexval.attr_vec.push_back (std::move (attr));
5536 if (addr != abbrev_table_start + abbrev_table_size)
5538 warning (_("Section .debug_names in %s has abbreviation_table "
5539 "of size %s vs. written as %u, ignoring .debug_names."),
5540 filename, plongest (addr - abbrev_table_start),
5541 abbrev_table_size);
5542 return false;
5544 map.entry_pool = addr;
5546 return true;
5549 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5550 list. */
5552 static void
5553 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5554 const mapped_debug_names &map,
5555 dwarf2_section_info &section,
5556 bool is_dwz)
5558 sect_offset sect_off_prev;
5559 for (uint32_t i = 0; i <= map.cu_count; ++i)
5561 sect_offset sect_off_next;
5562 if (i < map.cu_count)
5564 sect_off_next
5565 = (sect_offset) (extract_unsigned_integer
5566 (map.cu_table_reordered + i * map.offset_size,
5567 map.offset_size,
5568 map.dwarf5_byte_order));
5570 else
5571 sect_off_next = (sect_offset) section.size;
5572 if (i >= 1)
5574 const ULONGEST length = sect_off_next - sect_off_prev;
5575 dwarf2_per_cu_data *per_cu
5576 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5577 sect_off_prev, length);
5578 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5580 sect_off_prev = sect_off_next;
5584 /* Read the CU list from the mapped index, and use it to create all
5585 the CU objects for this dwarf2_per_objfile. */
5587 static void
5588 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5589 const mapped_debug_names &map,
5590 const mapped_debug_names &dwz_map)
5592 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5593 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5596 dwarf2_per_objfile->info,
5597 false /* is_dwz */);
5599 if (dwz_map.cu_count == 0)
5600 return;
5602 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5603 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5604 true /* is_dwz */);
5607 /* Read .debug_names. If everything went ok, initialize the "quick"
5608 elements of all the CUs and return true. Otherwise, return false. */
5610 static bool
5611 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5613 std::unique_ptr<mapped_debug_names> map
5614 (new mapped_debug_names (dwarf2_per_objfile));
5615 mapped_debug_names dwz_map (dwarf2_per_objfile);
5616 struct objfile *objfile = dwarf2_per_objfile->objfile;
5618 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5619 &dwarf2_per_objfile->debug_names,
5620 *map))
5621 return false;
5623 /* Don't use the index if it's empty. */
5624 if (map->name_count == 0)
5625 return false;
5627 /* If there is a .dwz file, read it so we can get its CU list as
5628 well. */
5629 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5630 if (dwz != NULL)
5632 if (!read_debug_names_from_section (objfile,
5633 bfd_get_filename (dwz->dwz_bfd.get ()),
5634 &dwz->debug_names, dwz_map))
5636 warning (_("could not read '.debug_names' section from %s; skipping"),
5637 bfd_get_filename (dwz->dwz_bfd.get ()));
5638 return false;
5642 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5644 if (map->tu_count != 0)
5646 /* We can only handle a single .debug_types when we have an
5647 index. */
5648 if (dwarf2_per_objfile->types.size () != 1)
5649 return false;
5651 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5653 create_signatured_type_table_from_debug_names
5654 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5657 create_addrmap_from_aranges (dwarf2_per_objfile,
5658 &dwarf2_per_objfile->debug_aranges);
5660 dwarf2_per_objfile->debug_names_table = std::move (map);
5661 dwarf2_per_objfile->using_index = 1;
5662 dwarf2_per_objfile->quick_file_names_table =
5663 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5665 return true;
5668 /* Type used to manage iterating over all CUs looking for a symbol for
5669 .debug_names. */
5671 class dw2_debug_names_iterator
5673 public:
5674 dw2_debug_names_iterator (const mapped_debug_names &map,
5675 gdb::optional<block_enum> block_index,
5676 domain_enum domain,
5677 const char *name)
5678 : m_map (map), m_block_index (block_index), m_domain (domain),
5679 m_addr (find_vec_in_debug_names (map, name))
5682 dw2_debug_names_iterator (const mapped_debug_names &map,
5683 search_domain search, uint32_t namei)
5684 : m_map (map),
5685 m_search (search),
5686 m_addr (find_vec_in_debug_names (map, namei))
5689 dw2_debug_names_iterator (const mapped_debug_names &map,
5690 block_enum block_index, domain_enum domain,
5691 uint32_t namei)
5692 : m_map (map), m_block_index (block_index), m_domain (domain),
5693 m_addr (find_vec_in_debug_names (map, namei))
5696 /* Return the next matching CU or NULL if there are no more. */
5697 dwarf2_per_cu_data *next ();
5699 private:
5700 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5701 const char *name);
5702 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5703 uint32_t namei);
5705 /* The internalized form of .debug_names. */
5706 const mapped_debug_names &m_map;
5708 /* If set, only look for symbols that match that block. Valid values are
5709 GLOBAL_BLOCK and STATIC_BLOCK. */
5710 const gdb::optional<block_enum> m_block_index;
5712 /* The kind of symbol we're looking for. */
5713 const domain_enum m_domain = UNDEF_DOMAIN;
5714 const search_domain m_search = ALL_DOMAIN;
5716 /* The list of CUs from the index entry of the symbol, or NULL if
5717 not found. */
5718 const gdb_byte *m_addr;
5721 const char *
5722 mapped_debug_names::namei_to_name (uint32_t namei) const
5724 const ULONGEST namei_string_offs
5725 = extract_unsigned_integer ((name_table_string_offs_reordered
5726 + namei * offset_size),
5727 offset_size,
5728 dwarf5_byte_order);
5729 return read_indirect_string_at_offset
5730 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5733 /* Find a slot in .debug_names for the object named NAME. If NAME is
5734 found, return pointer to its pool data. If NAME cannot be found,
5735 return NULL. */
5737 const gdb_byte *
5738 dw2_debug_names_iterator::find_vec_in_debug_names
5739 (const mapped_debug_names &map, const char *name)
5741 int (*cmp) (const char *, const char *);
5743 gdb::unique_xmalloc_ptr<char> without_params;
5744 if (current_language->la_language == language_cplus
5745 || current_language->la_language == language_fortran
5746 || current_language->la_language == language_d)
5748 /* NAME is already canonical. Drop any qualifiers as
5749 .debug_names does not contain any. */
5751 if (strchr (name, '(') != NULL)
5753 without_params = cp_remove_params (name);
5754 if (without_params != NULL)
5755 name = without_params.get ();
5759 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5761 const uint32_t full_hash = dwarf5_djb_hash (name);
5762 uint32_t namei
5763 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5764 (map.bucket_table_reordered
5765 + (full_hash % map.bucket_count)), 4,
5766 map.dwarf5_byte_order);
5767 if (namei == 0)
5768 return NULL;
5769 --namei;
5770 if (namei >= map.name_count)
5772 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5773 "[in module %s]"),
5774 namei, map.name_count,
5775 objfile_name (map.dwarf2_per_objfile->objfile));
5776 return NULL;
5779 for (;;)
5781 const uint32_t namei_full_hash
5782 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5783 (map.hash_table_reordered + namei), 4,
5784 map.dwarf5_byte_order);
5785 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5786 return NULL;
5788 if (full_hash == namei_full_hash)
5790 const char *const namei_string = map.namei_to_name (namei);
5792 #if 0 /* An expensive sanity check. */
5793 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5795 complaint (_("Wrong .debug_names hash for string at index %u "
5796 "[in module %s]"),
5797 namei, objfile_name (dwarf2_per_objfile->objfile));
5798 return NULL;
5800 #endif
5802 if (cmp (namei_string, name) == 0)
5804 const ULONGEST namei_entry_offs
5805 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5806 + namei * map.offset_size),
5807 map.offset_size, map.dwarf5_byte_order);
5808 return map.entry_pool + namei_entry_offs;
5812 ++namei;
5813 if (namei >= map.name_count)
5814 return NULL;
5818 const gdb_byte *
5819 dw2_debug_names_iterator::find_vec_in_debug_names
5820 (const mapped_debug_names &map, uint32_t namei)
5822 if (namei >= map.name_count)
5824 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5825 "[in module %s]"),
5826 namei, map.name_count,
5827 objfile_name (map.dwarf2_per_objfile->objfile));
5828 return NULL;
5831 const ULONGEST namei_entry_offs
5832 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5833 + namei * map.offset_size),
5834 map.offset_size, map.dwarf5_byte_order);
5835 return map.entry_pool + namei_entry_offs;
5838 /* See dw2_debug_names_iterator. */
5840 dwarf2_per_cu_data *
5841 dw2_debug_names_iterator::next ()
5843 if (m_addr == NULL)
5844 return NULL;
5846 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5848 bfd *const abfd = objfile->obfd;
5850 again:
5852 unsigned int bytes_read;
5853 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5854 m_addr += bytes_read;
5855 if (abbrev == 0)
5856 return NULL;
5858 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5859 if (indexval_it == m_map.abbrev_map.cend ())
5861 complaint (_("Wrong .debug_names undefined abbrev code %s "
5862 "[in module %s]"),
5863 pulongest (abbrev), objfile_name (objfile));
5864 return NULL;
5866 const mapped_debug_names::index_val &indexval = indexval_it->second;
5867 enum class symbol_linkage {
5868 unknown,
5869 static_,
5870 extern_,
5871 } symbol_linkage_ = symbol_linkage::unknown;
5872 dwarf2_per_cu_data *per_cu = NULL;
5873 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 ULONGEST ull;
5876 switch (attr.form)
5878 case DW_FORM_implicit_const:
5879 ull = attr.implicit_const;
5880 break;
5881 case DW_FORM_flag_present:
5882 ull = 1;
5883 break;
5884 case DW_FORM_udata:
5885 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5886 m_addr += bytes_read;
5887 break;
5888 default:
5889 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5890 dwarf_form_name (attr.form),
5891 objfile_name (objfile));
5892 return NULL;
5894 switch (attr.dw_idx)
5896 case DW_IDX_compile_unit:
5897 /* Don't crash on bad data. */
5898 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5900 complaint (_(".debug_names entry has bad CU index %s"
5901 " [in module %s]"),
5902 pulongest (ull),
5903 objfile_name (dwarf2_per_objfile->objfile));
5904 continue;
5906 per_cu = dwarf2_per_objfile->get_cutu (ull);
5907 break;
5908 case DW_IDX_type_unit:
5909 /* Don't crash on bad data. */
5910 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5912 complaint (_(".debug_names entry has bad TU index %s"
5913 " [in module %s]"),
5914 pulongest (ull),
5915 objfile_name (dwarf2_per_objfile->objfile));
5916 continue;
5918 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5919 break;
5920 case DW_IDX_GNU_internal:
5921 if (!m_map.augmentation_is_gdb)
5922 break;
5923 symbol_linkage_ = symbol_linkage::static_;
5924 break;
5925 case DW_IDX_GNU_external:
5926 if (!m_map.augmentation_is_gdb)
5927 break;
5928 symbol_linkage_ = symbol_linkage::extern_;
5929 break;
5933 /* Skip if already read in. */
5934 if (per_cu->v.quick->compunit_symtab)
5935 goto again;
5937 /* Check static vs global. */
5938 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5940 const bool want_static = *m_block_index == STATIC_BLOCK;
5941 const bool symbol_is_static =
5942 symbol_linkage_ == symbol_linkage::static_;
5943 if (want_static != symbol_is_static)
5944 goto again;
5947 /* Match dw2_symtab_iter_next, symbol_kind
5948 and debug_names::psymbol_tag. */
5949 switch (m_domain)
5951 case VAR_DOMAIN:
5952 switch (indexval.dwarf_tag)
5954 case DW_TAG_variable:
5955 case DW_TAG_subprogram:
5956 /* Some types are also in VAR_DOMAIN. */
5957 case DW_TAG_typedef:
5958 case DW_TAG_structure_type:
5959 break;
5960 default:
5961 goto again;
5963 break;
5964 case STRUCT_DOMAIN:
5965 switch (indexval.dwarf_tag)
5967 case DW_TAG_typedef:
5968 case DW_TAG_structure_type:
5969 break;
5970 default:
5971 goto again;
5973 break;
5974 case LABEL_DOMAIN:
5975 switch (indexval.dwarf_tag)
5977 case 0:
5978 case DW_TAG_variable:
5979 break;
5980 default:
5981 goto again;
5983 break;
5984 case MODULE_DOMAIN:
5985 switch (indexval.dwarf_tag)
5987 case DW_TAG_module:
5988 break;
5989 default:
5990 goto again;
5992 break;
5993 default:
5994 break;
5997 /* Match dw2_expand_symtabs_matching, symbol_kind and
5998 debug_names::psymbol_tag. */
5999 switch (m_search)
6001 case VARIABLES_DOMAIN:
6002 switch (indexval.dwarf_tag)
6004 case DW_TAG_variable:
6005 break;
6006 default:
6007 goto again;
6009 break;
6010 case FUNCTIONS_DOMAIN:
6011 switch (indexval.dwarf_tag)
6013 case DW_TAG_subprogram:
6014 break;
6015 default:
6016 goto again;
6018 break;
6019 case TYPES_DOMAIN:
6020 switch (indexval.dwarf_tag)
6022 case DW_TAG_typedef:
6023 case DW_TAG_structure_type:
6024 break;
6025 default:
6026 goto again;
6028 break;
6029 case MODULES_DOMAIN:
6030 switch (indexval.dwarf_tag)
6032 case DW_TAG_module:
6033 break;
6034 default:
6035 goto again;
6037 default:
6038 break;
6041 return per_cu;
6044 static struct compunit_symtab *
6045 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6046 const char *name, domain_enum domain)
6048 struct dwarf2_per_objfile *dwarf2_per_objfile
6049 = get_dwarf2_per_objfile (objfile);
6051 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6052 if (!mapp)
6054 /* index is NULL if OBJF_READNOW. */
6055 return NULL;
6057 const auto &map = *mapp;
6059 dw2_debug_names_iterator iter (map, block_index, domain, name);
6061 struct compunit_symtab *stab_best = NULL;
6062 struct dwarf2_per_cu_data *per_cu;
6063 while ((per_cu = iter.next ()) != NULL)
6065 struct symbol *sym, *with_opaque = NULL;
6066 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6067 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6068 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6070 sym = block_find_symbol (block, name, domain,
6071 block_find_non_opaque_type_preferred,
6072 &with_opaque);
6074 /* Some caution must be observed with overloaded functions and
6075 methods, since the index will not contain any overload
6076 information (but NAME might contain it). */
6078 if (sym != NULL
6079 && strcmp_iw (sym->search_name (), name) == 0)
6080 return stab;
6081 if (with_opaque != NULL
6082 && strcmp_iw (with_opaque->search_name (), name) == 0)
6083 stab_best = stab;
6085 /* Keep looking through other CUs. */
6088 return stab_best;
6091 /* This dumps minimal information about .debug_names. It is called
6092 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6093 uses this to verify that .debug_names has been loaded. */
6095 static void
6096 dw2_debug_names_dump (struct objfile *objfile)
6098 struct dwarf2_per_objfile *dwarf2_per_objfile
6099 = get_dwarf2_per_objfile (objfile);
6101 gdb_assert (dwarf2_per_objfile->using_index);
6102 printf_filtered (".debug_names:");
6103 if (dwarf2_per_objfile->debug_names_table)
6104 printf_filtered (" exists\n");
6105 else
6106 printf_filtered (" faked for \"readnow\"\n");
6107 printf_filtered ("\n");
6110 static void
6111 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6112 const char *func_name)
6114 struct dwarf2_per_objfile *dwarf2_per_objfile
6115 = get_dwarf2_per_objfile (objfile);
6117 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6118 if (dwarf2_per_objfile->debug_names_table)
6120 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6122 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6124 struct dwarf2_per_cu_data *per_cu;
6125 while ((per_cu = iter.next ()) != NULL)
6126 dw2_instantiate_symtab (per_cu, false);
6130 static void
6131 dw2_debug_names_map_matching_symbols
6132 (struct objfile *objfile,
6133 const lookup_name_info &name, domain_enum domain,
6134 int global,
6135 gdb::function_view<symbol_found_callback_ftype> callback,
6136 symbol_compare_ftype *ordered_compare)
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
6141 /* debug_names_table is NULL if OBJF_READNOW. */
6142 if (!dwarf2_per_objfile->debug_names_table)
6143 return;
6145 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6146 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6148 const char *match_name = name.ada ().lookup_name ().c_str ();
6149 auto matcher = [&] (const char *symname)
6151 if (ordered_compare == nullptr)
6152 return true;
6153 return ordered_compare (symname, match_name) == 0;
6156 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6157 [&] (offset_type namei)
6159 /* The name was matched, now expand corresponding CUs that were
6160 marked. */
6161 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6163 struct dwarf2_per_cu_data *per_cu;
6164 while ((per_cu = iter.next ()) != NULL)
6165 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6166 return true;
6169 /* It's a shame we couldn't do this inside the
6170 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6171 that have already been expanded. Instead, this loop matches what
6172 the psymtab code does. */
6173 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6175 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6176 if (cust != nullptr)
6178 const struct block *block
6179 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6180 if (!iterate_over_symbols_terminated (block, name,
6181 domain, callback))
6182 break;
6187 static void
6188 dw2_debug_names_expand_symtabs_matching
6189 (struct objfile *objfile,
6190 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6191 const lookup_name_info &lookup_name,
6192 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6193 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6194 enum search_domain kind)
6196 struct dwarf2_per_objfile *dwarf2_per_objfile
6197 = get_dwarf2_per_objfile (objfile);
6199 /* debug_names_table is NULL if OBJF_READNOW. */
6200 if (!dwarf2_per_objfile->debug_names_table)
6201 return;
6203 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6205 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6207 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6208 symbol_matcher,
6209 kind, [&] (offset_type namei)
6211 /* The name was matched, now expand corresponding CUs that were
6212 marked. */
6213 dw2_debug_names_iterator iter (map, kind, namei);
6215 struct dwarf2_per_cu_data *per_cu;
6216 while ((per_cu = iter.next ()) != NULL)
6217 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6218 expansion_notify);
6219 return true;
6223 const struct quick_symbol_functions dwarf2_debug_names_functions =
6225 dw2_has_symbols,
6226 dw2_find_last_source_symtab,
6227 dw2_forget_cached_source_info,
6228 dw2_map_symtabs_matching_filename,
6229 dw2_debug_names_lookup_symbol,
6230 dw2_print_stats,
6231 dw2_debug_names_dump,
6232 dw2_debug_names_expand_symtabs_for_function,
6233 dw2_expand_all_symtabs,
6234 dw2_expand_symtabs_with_fullname,
6235 dw2_debug_names_map_matching_symbols,
6236 dw2_debug_names_expand_symtabs_matching,
6237 dw2_find_pc_sect_compunit_symtab,
6238 NULL,
6239 dw2_map_symbol_filenames
6242 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6243 to either a dwarf2_per_objfile or dwz_file object. */
6245 template <typename T>
6246 static gdb::array_view<const gdb_byte>
6247 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6249 dwarf2_section_info *section = &section_owner->gdb_index;
6251 if (dwarf2_section_empty_p (section))
6252 return {};
6254 /* Older elfutils strip versions could keep the section in the main
6255 executable while splitting it for the separate debug info file. */
6256 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6257 return {};
6259 dwarf2_read_section (obj, section);
6261 /* dwarf2_section_info::size is a bfd_size_type, while
6262 gdb::array_view works with size_t. On 32-bit hosts, with
6263 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6264 is 32-bit. So we need an explicit narrowing conversion here.
6265 This is fine, because it's impossible to allocate or mmap an
6266 array/buffer larger than what size_t can represent. */
6267 return gdb::make_array_view (section->buffer, section->size);
6270 /* Lookup the index cache for the contents of the index associated to
6271 DWARF2_OBJ. */
6273 static gdb::array_view<const gdb_byte>
6274 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6276 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6277 if (build_id == nullptr)
6278 return {};
6280 return global_index_cache.lookup_gdb_index (build_id,
6281 &dwarf2_obj->index_cache_res);
6284 /* Same as the above, but for DWZ. */
6286 static gdb::array_view<const gdb_byte>
6287 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6289 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6290 if (build_id == nullptr)
6291 return {};
6293 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6296 /* See symfile.h. */
6298 bool
6299 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6301 struct dwarf2_per_objfile *dwarf2_per_objfile
6302 = get_dwarf2_per_objfile (objfile);
6304 /* If we're about to read full symbols, don't bother with the
6305 indices. In this case we also don't care if some other debug
6306 format is making psymtabs, because they are all about to be
6307 expanded anyway. */
6308 if ((objfile->flags & OBJF_READNOW))
6310 dwarf2_per_objfile->using_index = 1;
6311 create_all_comp_units (dwarf2_per_objfile);
6312 create_all_type_units (dwarf2_per_objfile);
6313 dwarf2_per_objfile->quick_file_names_table
6314 = create_quick_file_names_table
6315 (dwarf2_per_objfile->all_comp_units.size ());
6317 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6318 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6320 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6322 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6323 struct dwarf2_per_cu_quick_data);
6326 /* Return 1 so that gdb sees the "quick" functions. However,
6327 these functions will be no-ops because we will have expanded
6328 all symtabs. */
6329 *index_kind = dw_index_kind::GDB_INDEX;
6330 return true;
6333 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6335 *index_kind = dw_index_kind::DEBUG_NAMES;
6336 return true;
6339 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6340 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6341 get_gdb_index_contents_from_section<dwz_file>))
6343 *index_kind = dw_index_kind::GDB_INDEX;
6344 return true;
6347 /* ... otherwise, try to find the index in the index cache. */
6348 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6349 get_gdb_index_contents_from_cache,
6350 get_gdb_index_contents_from_cache_dwz))
6352 global_index_cache.hit ();
6353 *index_kind = dw_index_kind::GDB_INDEX;
6354 return true;
6357 global_index_cache.miss ();
6358 return false;
6363 /* Build a partial symbol table. */
6365 void
6366 dwarf2_build_psymtabs (struct objfile *objfile)
6368 struct dwarf2_per_objfile *dwarf2_per_objfile
6369 = get_dwarf2_per_objfile (objfile);
6371 init_psymbol_list (objfile, 1024);
6375 /* This isn't really ideal: all the data we allocate on the
6376 objfile's obstack is still uselessly kept around. However,
6377 freeing it seems unsafe. */
6378 psymtab_discarder psymtabs (objfile);
6379 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6380 psymtabs.keep ();
6382 /* (maybe) store an index in the cache. */
6383 global_index_cache.store (dwarf2_per_objfile);
6385 catch (const gdb_exception_error &except)
6387 exception_print (gdb_stderr, except);
6391 /* Return the total length of the CU described by HEADER. */
6393 static unsigned int
6394 get_cu_length (const struct comp_unit_head *header)
6396 return header->initial_length_size + header->length;
6399 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6401 static inline bool
6402 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6404 sect_offset bottom = cu_header->sect_off;
6405 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6407 return sect_off >= bottom && sect_off < top;
6410 /* Find the base address of the compilation unit for range lists and
6411 location lists. It will normally be specified by DW_AT_low_pc.
6412 In DWARF-3 draft 4, the base address could be overridden by
6413 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6414 compilation units with discontinuous ranges. */
6416 static void
6417 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6419 struct attribute *attr;
6421 cu->base_known = 0;
6422 cu->base_address = 0;
6424 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6425 if (attr != nullptr)
6427 cu->base_address = attr_value_as_address (attr);
6428 cu->base_known = 1;
6430 else
6432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6433 if (attr != nullptr)
6435 cu->base_address = attr_value_as_address (attr);
6436 cu->base_known = 1;
6441 /* Read in the comp unit header information from the debug_info at info_ptr.
6442 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6443 NOTE: This leaves members offset, first_die_offset to be filled in
6444 by the caller. */
6446 static const gdb_byte *
6447 read_comp_unit_head (struct comp_unit_head *cu_header,
6448 const gdb_byte *info_ptr,
6449 struct dwarf2_section_info *section,
6450 rcuh_kind section_kind)
6452 int signed_addr;
6453 unsigned int bytes_read;
6454 const char *filename = get_section_file_name (section);
6455 bfd *abfd = get_section_bfd_owner (section);
6457 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6458 cu_header->initial_length_size = bytes_read;
6459 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6460 info_ptr += bytes_read;
6461 cu_header->version = read_2_bytes (abfd, info_ptr);
6462 if (cu_header->version < 2 || cu_header->version > 5)
6463 error (_("Dwarf Error: wrong version in compilation unit header "
6464 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6465 cu_header->version, filename);
6466 info_ptr += 2;
6467 if (cu_header->version < 5)
6468 switch (section_kind)
6470 case rcuh_kind::COMPILE:
6471 cu_header->unit_type = DW_UT_compile;
6472 break;
6473 case rcuh_kind::TYPE:
6474 cu_header->unit_type = DW_UT_type;
6475 break;
6476 default:
6477 internal_error (__FILE__, __LINE__,
6478 _("read_comp_unit_head: invalid section_kind"));
6480 else
6482 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6483 (read_1_byte (abfd, info_ptr));
6484 info_ptr += 1;
6485 switch (cu_header->unit_type)
6487 case DW_UT_compile:
6488 case DW_UT_partial:
6489 case DW_UT_skeleton:
6490 case DW_UT_split_compile:
6491 if (section_kind != rcuh_kind::COMPILE)
6492 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6493 "(is %s, should be %s) [in module %s]"),
6494 dwarf_unit_type_name (cu_header->unit_type),
6495 dwarf_unit_type_name (DW_UT_type), filename);
6496 break;
6497 case DW_UT_type:
6498 case DW_UT_split_type:
6499 section_kind = rcuh_kind::TYPE;
6500 break;
6501 default:
6502 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6503 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6504 "[in module %s]"), cu_header->unit_type,
6505 dwarf_unit_type_name (DW_UT_compile),
6506 dwarf_unit_type_name (DW_UT_skeleton),
6507 dwarf_unit_type_name (DW_UT_split_compile),
6508 dwarf_unit_type_name (DW_UT_type),
6509 dwarf_unit_type_name (DW_UT_split_type), filename);
6512 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6513 info_ptr += 1;
6515 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6516 cu_header,
6517 &bytes_read);
6518 info_ptr += bytes_read;
6519 if (cu_header->version < 5)
6521 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6522 info_ptr += 1;
6524 signed_addr = bfd_get_sign_extend_vma (abfd);
6525 if (signed_addr < 0)
6526 internal_error (__FILE__, __LINE__,
6527 _("read_comp_unit_head: dwarf from non elf file"));
6528 cu_header->signed_addr_p = signed_addr;
6530 bool header_has_signature = section_kind == rcuh_kind::TYPE
6531 || cu_header->unit_type == DW_UT_skeleton
6532 || cu_header->unit_type == DW_UT_split_compile;
6534 if (header_has_signature)
6536 cu_header->signature = read_8_bytes (abfd, info_ptr);
6537 info_ptr += 8;
6540 if (section_kind == rcuh_kind::TYPE)
6542 LONGEST type_offset;
6543 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6544 info_ptr += bytes_read;
6545 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6546 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6547 error (_("Dwarf Error: Too big type_offset in compilation unit "
6548 "header (is %s) [in module %s]"), plongest (type_offset),
6549 filename);
6552 return info_ptr;
6555 /* Helper function that returns the proper abbrev section for
6556 THIS_CU. */
6558 static struct dwarf2_section_info *
6559 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6561 struct dwarf2_section_info *abbrev;
6562 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6564 if (this_cu->is_dwz)
6565 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6566 else
6567 abbrev = &dwarf2_per_objfile->abbrev;
6569 return abbrev;
6572 /* Subroutine of read_and_check_comp_unit_head and
6573 read_and_check_type_unit_head to simplify them.
6574 Perform various error checking on the header. */
6576 static void
6577 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6578 struct comp_unit_head *header,
6579 struct dwarf2_section_info *section,
6580 struct dwarf2_section_info *abbrev_section)
6582 const char *filename = get_section_file_name (section);
6584 if (to_underlying (header->abbrev_sect_off)
6585 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6586 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6587 "(offset %s + 6) [in module %s]"),
6588 sect_offset_str (header->abbrev_sect_off),
6589 sect_offset_str (header->sect_off),
6590 filename);
6592 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6593 avoid potential 32-bit overflow. */
6594 if (((ULONGEST) header->sect_off + get_cu_length (header))
6595 > section->size)
6596 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6597 "(offset %s + 0) [in module %s]"),
6598 header->length, sect_offset_str (header->sect_off),
6599 filename);
6602 /* Read in a CU/TU header and perform some basic error checking.
6603 The contents of the header are stored in HEADER.
6604 The result is a pointer to the start of the first DIE. */
6606 static const gdb_byte *
6607 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6608 struct comp_unit_head *header,
6609 struct dwarf2_section_info *section,
6610 struct dwarf2_section_info *abbrev_section,
6611 const gdb_byte *info_ptr,
6612 rcuh_kind section_kind)
6614 const gdb_byte *beg_of_comp_unit = info_ptr;
6616 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6618 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6620 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6622 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6623 abbrev_section);
6625 return info_ptr;
6628 /* Fetch the abbreviation table offset from a comp or type unit header. */
6630 static sect_offset
6631 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6632 struct dwarf2_section_info *section,
6633 sect_offset sect_off)
6635 bfd *abfd = get_section_bfd_owner (section);
6636 const gdb_byte *info_ptr;
6637 unsigned int initial_length_size, offset_size;
6638 uint16_t version;
6640 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6641 info_ptr = section->buffer + to_underlying (sect_off);
6642 read_initial_length (abfd, info_ptr, &initial_length_size);
6643 offset_size = initial_length_size == 4 ? 4 : 8;
6644 info_ptr += initial_length_size;
6646 version = read_2_bytes (abfd, info_ptr);
6647 info_ptr += 2;
6648 if (version >= 5)
6650 /* Skip unit type and address size. */
6651 info_ptr += 2;
6654 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6657 /* Allocate a new partial symtab for file named NAME and mark this new
6658 partial symtab as being an include of PST. */
6660 static void
6661 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6662 struct objfile *objfile)
6664 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6666 if (!IS_ABSOLUTE_PATH (subpst->filename))
6668 /* It shares objfile->objfile_obstack. */
6669 subpst->dirname = pst->dirname;
6672 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6673 subpst->dependencies[0] = pst;
6674 subpst->number_of_dependencies = 1;
6676 subpst->read_symtab = pst->read_symtab;
6678 /* No private part is necessary for include psymtabs. This property
6679 can be used to differentiate between such include psymtabs and
6680 the regular ones. */
6681 subpst->read_symtab_private = NULL;
6684 /* Read the Line Number Program data and extract the list of files
6685 included by the source file represented by PST. Build an include
6686 partial symtab for each of these included files. */
6688 static void
6689 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6690 struct die_info *die,
6691 struct partial_symtab *pst)
6693 line_header_up lh;
6694 struct attribute *attr;
6696 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6697 if (attr != nullptr)
6698 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6699 if (lh == NULL)
6700 return; /* No linetable, so no includes. */
6702 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6703 that we pass in the raw text_low here; that is ok because we're
6704 only decoding the line table to make include partial symtabs, and
6705 so the addresses aren't really used. */
6706 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6707 pst->raw_text_low (), 1);
6710 static hashval_t
6711 hash_signatured_type (const void *item)
6713 const struct signatured_type *sig_type
6714 = (const struct signatured_type *) item;
6716 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6717 return sig_type->signature;
6720 static int
6721 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6723 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6724 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6726 return lhs->signature == rhs->signature;
6729 /* Allocate a hash table for signatured types. */
6731 static htab_t
6732 allocate_signatured_type_table (struct objfile *objfile)
6734 return htab_create_alloc_ex (41,
6735 hash_signatured_type,
6736 eq_signatured_type,
6737 NULL,
6738 &objfile->objfile_obstack,
6739 hashtab_obstack_allocate,
6740 dummy_obstack_deallocate);
6743 /* A helper function to add a signatured type CU to a table. */
6745 static int
6746 add_signatured_type_cu_to_table (void **slot, void *datum)
6748 struct signatured_type *sigt = (struct signatured_type *) *slot;
6749 std::vector<signatured_type *> *all_type_units
6750 = (std::vector<signatured_type *> *) datum;
6752 all_type_units->push_back (sigt);
6754 return 1;
6757 /* A helper for create_debug_types_hash_table. Read types from SECTION
6758 and fill them into TYPES_HTAB. It will process only type units,
6759 therefore DW_UT_type. */
6761 static void
6762 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6763 struct dwo_file *dwo_file,
6764 dwarf2_section_info *section, htab_t &types_htab,
6765 rcuh_kind section_kind)
6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
6768 struct dwarf2_section_info *abbrev_section;
6769 bfd *abfd;
6770 const gdb_byte *info_ptr, *end_ptr;
6772 abbrev_section = (dwo_file != NULL
6773 ? &dwo_file->sections.abbrev
6774 : &dwarf2_per_objfile->abbrev);
6776 if (dwarf_read_debug)
6777 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6778 get_section_name (section),
6779 get_section_file_name (abbrev_section));
6781 dwarf2_read_section (objfile, section);
6782 info_ptr = section->buffer;
6784 if (info_ptr == NULL)
6785 return;
6787 /* We can't set abfd until now because the section may be empty or
6788 not present, in which case the bfd is unknown. */
6789 abfd = get_section_bfd_owner (section);
6791 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6792 because we don't need to read any dies: the signature is in the
6793 header. */
6795 end_ptr = info_ptr + section->size;
6796 while (info_ptr < end_ptr)
6798 struct signatured_type *sig_type;
6799 struct dwo_unit *dwo_tu;
6800 void **slot;
6801 const gdb_byte *ptr = info_ptr;
6802 struct comp_unit_head header;
6803 unsigned int length;
6805 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6807 /* Initialize it due to a false compiler warning. */
6808 header.signature = -1;
6809 header.type_cu_offset_in_tu = (cu_offset) -1;
6811 /* We need to read the type's signature in order to build the hash
6812 table, but we don't need anything else just yet. */
6814 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6815 abbrev_section, ptr, section_kind);
6817 length = get_cu_length (&header);
6819 /* Skip dummy type units. */
6820 if (ptr >= info_ptr + length
6821 || peek_abbrev_code (abfd, ptr) == 0
6822 || header.unit_type != DW_UT_type)
6824 info_ptr += length;
6825 continue;
6828 if (types_htab == NULL)
6830 if (dwo_file)
6831 types_htab = allocate_dwo_unit_table (objfile);
6832 else
6833 types_htab = allocate_signatured_type_table (objfile);
6836 if (dwo_file)
6838 sig_type = NULL;
6839 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6840 struct dwo_unit);
6841 dwo_tu->dwo_file = dwo_file;
6842 dwo_tu->signature = header.signature;
6843 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6844 dwo_tu->section = section;
6845 dwo_tu->sect_off = sect_off;
6846 dwo_tu->length = length;
6848 else
6850 /* N.B.: type_offset is not usable if this type uses a DWO file.
6851 The real type_offset is in the DWO file. */
6852 dwo_tu = NULL;
6853 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6854 struct signatured_type);
6855 sig_type->signature = header.signature;
6856 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6857 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6858 sig_type->per_cu.is_debug_types = 1;
6859 sig_type->per_cu.section = section;
6860 sig_type->per_cu.sect_off = sect_off;
6861 sig_type->per_cu.length = length;
6864 slot = htab_find_slot (types_htab,
6865 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6866 INSERT);
6867 gdb_assert (slot != NULL);
6868 if (*slot != NULL)
6870 sect_offset dup_sect_off;
6872 if (dwo_file)
6874 const struct dwo_unit *dup_tu
6875 = (const struct dwo_unit *) *slot;
6877 dup_sect_off = dup_tu->sect_off;
6879 else
6881 const struct signatured_type *dup_tu
6882 = (const struct signatured_type *) *slot;
6884 dup_sect_off = dup_tu->per_cu.sect_off;
6887 complaint (_("debug type entry at offset %s is duplicate to"
6888 " the entry at offset %s, signature %s"),
6889 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6890 hex_string (header.signature));
6892 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6894 if (dwarf_read_debug > 1)
6895 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6896 sect_offset_str (sect_off),
6897 hex_string (header.signature));
6899 info_ptr += length;
6903 /* Create the hash table of all entries in the .debug_types
6904 (or .debug_types.dwo) section(s).
6905 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6906 otherwise it is NULL.
6908 The result is a pointer to the hash table or NULL if there are no types.
6910 Note: This function processes DWO files only, not DWP files. */
6912 static void
6913 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6914 struct dwo_file *dwo_file,
6915 gdb::array_view<dwarf2_section_info> type_sections,
6916 htab_t &types_htab)
6918 for (dwarf2_section_info &section : type_sections)
6919 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6920 types_htab, rcuh_kind::TYPE);
6923 /* Create the hash table of all entries in the .debug_types section,
6924 and initialize all_type_units.
6925 The result is zero if there is an error (e.g. missing .debug_types section),
6926 otherwise non-zero. */
6928 static int
6929 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6931 htab_t types_htab = NULL;
6933 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6934 &dwarf2_per_objfile->info, types_htab,
6935 rcuh_kind::COMPILE);
6936 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6937 dwarf2_per_objfile->types, types_htab);
6938 if (types_htab == NULL)
6940 dwarf2_per_objfile->signatured_types = NULL;
6941 return 0;
6944 dwarf2_per_objfile->signatured_types = types_htab;
6946 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6947 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6949 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6950 &dwarf2_per_objfile->all_type_units);
6952 return 1;
6955 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6956 If SLOT is non-NULL, it is the entry to use in the hash table.
6957 Otherwise we find one. */
6959 static struct signatured_type *
6960 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6961 void **slot)
6963 struct objfile *objfile = dwarf2_per_objfile->objfile;
6965 if (dwarf2_per_objfile->all_type_units.size ()
6966 == dwarf2_per_objfile->all_type_units.capacity ())
6967 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6969 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6970 struct signatured_type);
6972 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6973 sig_type->signature = sig;
6974 sig_type->per_cu.is_debug_types = 1;
6975 if (dwarf2_per_objfile->using_index)
6977 sig_type->per_cu.v.quick =
6978 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6979 struct dwarf2_per_cu_quick_data);
6982 if (slot == NULL)
6984 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6985 sig_type, INSERT);
6987 gdb_assert (*slot == NULL);
6988 *slot = sig_type;
6989 /* The rest of sig_type must be filled in by the caller. */
6990 return sig_type;
6993 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6994 Fill in SIG_ENTRY with DWO_ENTRY. */
6996 static void
6997 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6998 struct signatured_type *sig_entry,
6999 struct dwo_unit *dwo_entry)
7001 /* Make sure we're not clobbering something we don't expect to. */
7002 gdb_assert (! sig_entry->per_cu.queued);
7003 gdb_assert (sig_entry->per_cu.cu == NULL);
7004 if (dwarf2_per_objfile->using_index)
7006 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7007 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7009 else
7010 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7011 gdb_assert (sig_entry->signature == dwo_entry->signature);
7012 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7013 gdb_assert (sig_entry->type_unit_group == NULL);
7014 gdb_assert (sig_entry->dwo_unit == NULL);
7016 sig_entry->per_cu.section = dwo_entry->section;
7017 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7018 sig_entry->per_cu.length = dwo_entry->length;
7019 sig_entry->per_cu.reading_dwo_directly = 1;
7020 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7021 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7022 sig_entry->dwo_unit = dwo_entry;
7025 /* Subroutine of lookup_signatured_type.
7026 If we haven't read the TU yet, create the signatured_type data structure
7027 for a TU to be read in directly from a DWO file, bypassing the stub.
7028 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7029 using .gdb_index, then when reading a CU we want to stay in the DWO file
7030 containing that CU. Otherwise we could end up reading several other DWO
7031 files (due to comdat folding) to process the transitive closure of all the
7032 mentioned TUs, and that can be slow. The current DWO file will have every
7033 type signature that it needs.
7034 We only do this for .gdb_index because in the psymtab case we already have
7035 to read all the DWOs to build the type unit groups. */
7037 static struct signatured_type *
7038 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7040 struct dwarf2_per_objfile *dwarf2_per_objfile
7041 = cu->per_cu->dwarf2_per_objfile;
7042 struct objfile *objfile = dwarf2_per_objfile->objfile;
7043 struct dwo_file *dwo_file;
7044 struct dwo_unit find_dwo_entry, *dwo_entry;
7045 struct signatured_type find_sig_entry, *sig_entry;
7046 void **slot;
7048 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7050 /* If TU skeletons have been removed then we may not have read in any
7051 TUs yet. */
7052 if (dwarf2_per_objfile->signatured_types == NULL)
7054 dwarf2_per_objfile->signatured_types
7055 = allocate_signatured_type_table (objfile);
7058 /* We only ever need to read in one copy of a signatured type.
7059 Use the global signatured_types array to do our own comdat-folding
7060 of types. If this is the first time we're reading this TU, and
7061 the TU has an entry in .gdb_index, replace the recorded data from
7062 .gdb_index with this TU. */
7064 find_sig_entry.signature = sig;
7065 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7066 &find_sig_entry, INSERT);
7067 sig_entry = (struct signatured_type *) *slot;
7069 /* We can get here with the TU already read, *or* in the process of being
7070 read. Don't reassign the global entry to point to this DWO if that's
7071 the case. Also note that if the TU is already being read, it may not
7072 have come from a DWO, the program may be a mix of Fission-compiled
7073 code and non-Fission-compiled code. */
7075 /* Have we already tried to read this TU?
7076 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7077 needn't exist in the global table yet). */
7078 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7079 return sig_entry;
7081 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7082 dwo_unit of the TU itself. */
7083 dwo_file = cu->dwo_unit->dwo_file;
7085 /* Ok, this is the first time we're reading this TU. */
7086 if (dwo_file->tus == NULL)
7087 return NULL;
7088 find_dwo_entry.signature = sig;
7089 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7090 if (dwo_entry == NULL)
7091 return NULL;
7093 /* If the global table doesn't have an entry for this TU, add one. */
7094 if (sig_entry == NULL)
7095 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7097 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7098 sig_entry->per_cu.tu_read = 1;
7099 return sig_entry;
7102 /* Subroutine of lookup_signatured_type.
7103 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7104 then try the DWP file. If the TU stub (skeleton) has been removed then
7105 it won't be in .gdb_index. */
7107 static struct signatured_type *
7108 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7110 struct dwarf2_per_objfile *dwarf2_per_objfile
7111 = cu->per_cu->dwarf2_per_objfile;
7112 struct objfile *objfile = dwarf2_per_objfile->objfile;
7113 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7114 struct dwo_unit *dwo_entry;
7115 struct signatured_type find_sig_entry, *sig_entry;
7116 void **slot;
7118 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7119 gdb_assert (dwp_file != NULL);
7121 /* If TU skeletons have been removed then we may not have read in any
7122 TUs yet. */
7123 if (dwarf2_per_objfile->signatured_types == NULL)
7125 dwarf2_per_objfile->signatured_types
7126 = allocate_signatured_type_table (objfile);
7129 find_sig_entry.signature = sig;
7130 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7131 &find_sig_entry, INSERT);
7132 sig_entry = (struct signatured_type *) *slot;
7134 /* Have we already tried to read this TU?
7135 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7136 needn't exist in the global table yet). */
7137 if (sig_entry != NULL)
7138 return sig_entry;
7140 if (dwp_file->tus == NULL)
7141 return NULL;
7142 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7143 sig, 1 /* is_debug_types */);
7144 if (dwo_entry == NULL)
7145 return NULL;
7147 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7148 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7150 return sig_entry;
7153 /* Lookup a signature based type for DW_FORM_ref_sig8.
7154 Returns NULL if signature SIG is not present in the table.
7155 It is up to the caller to complain about this. */
7157 static struct signatured_type *
7158 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7160 struct dwarf2_per_objfile *dwarf2_per_objfile
7161 = cu->per_cu->dwarf2_per_objfile;
7163 if (cu->dwo_unit
7164 && dwarf2_per_objfile->using_index)
7166 /* We're in a DWO/DWP file, and we're using .gdb_index.
7167 These cases require special processing. */
7168 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7169 return lookup_dwo_signatured_type (cu, sig);
7170 else
7171 return lookup_dwp_signatured_type (cu, sig);
7173 else
7175 struct signatured_type find_entry, *entry;
7177 if (dwarf2_per_objfile->signatured_types == NULL)
7178 return NULL;
7179 find_entry.signature = sig;
7180 entry = ((struct signatured_type *)
7181 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7182 return entry;
7186 /* Low level DIE reading support. */
7188 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7190 static void
7191 init_cu_die_reader (struct die_reader_specs *reader,
7192 struct dwarf2_cu *cu,
7193 struct dwarf2_section_info *section,
7194 struct dwo_file *dwo_file,
7195 struct abbrev_table *abbrev_table)
7197 gdb_assert (section->readin && section->buffer != NULL);
7198 reader->abfd = get_section_bfd_owner (section);
7199 reader->cu = cu;
7200 reader->dwo_file = dwo_file;
7201 reader->die_section = section;
7202 reader->buffer = section->buffer;
7203 reader->buffer_end = section->buffer + section->size;
7204 reader->comp_dir = NULL;
7205 reader->abbrev_table = abbrev_table;
7208 /* Subroutine of init_cutu_and_read_dies to simplify it.
7209 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7210 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7211 already.
7213 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7214 from it to the DIE in the DWO. If NULL we are skipping the stub.
7215 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7216 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7217 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7218 STUB_COMP_DIR may be non-NULL.
7219 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7220 are filled in with the info of the DIE from the DWO file.
7221 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7222 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7223 kept around for at least as long as *RESULT_READER.
7225 The result is non-zero if a valid (non-dummy) DIE was found. */
7227 static int
7228 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7229 struct dwo_unit *dwo_unit,
7230 struct die_info *stub_comp_unit_die,
7231 const char *stub_comp_dir,
7232 struct die_reader_specs *result_reader,
7233 const gdb_byte **result_info_ptr,
7234 struct die_info **result_comp_unit_die,
7235 int *result_has_children,
7236 abbrev_table_up *result_dwo_abbrev_table)
7238 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7239 struct objfile *objfile = dwarf2_per_objfile->objfile;
7240 struct dwarf2_cu *cu = this_cu->cu;
7241 bfd *abfd;
7242 const gdb_byte *begin_info_ptr, *info_ptr;
7243 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7244 int i,num_extra_attrs;
7245 struct dwarf2_section_info *dwo_abbrev_section;
7246 struct attribute *attr;
7247 struct die_info *comp_unit_die;
7249 /* At most one of these may be provided. */
7250 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7252 /* These attributes aren't processed until later:
7253 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7254 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7255 referenced later. However, these attributes are found in the stub
7256 which we won't have later. In order to not impose this complication
7257 on the rest of the code, we read them here and copy them to the
7258 DWO CU/TU die. */
7260 stmt_list = NULL;
7261 low_pc = NULL;
7262 high_pc = NULL;
7263 ranges = NULL;
7264 comp_dir = NULL;
7266 if (stub_comp_unit_die != NULL)
7268 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7269 DWO file. */
7270 if (! this_cu->is_debug_types)
7271 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7272 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7273 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7274 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7275 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7277 /* There should be a DW_AT_addr_base attribute here (if needed).
7278 We need the value before we can process DW_FORM_GNU_addr_index
7279 or DW_FORM_addrx. */
7280 cu->addr_base = 0;
7281 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7282 if (attr != nullptr)
7283 cu->addr_base = DW_UNSND (attr);
7285 /* There should be a DW_AT_ranges_base attribute here (if needed).
7286 We need the value before we can process DW_AT_ranges. */
7287 cu->ranges_base = 0;
7288 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7289 if (attr != nullptr)
7290 cu->ranges_base = DW_UNSND (attr);
7292 else if (stub_comp_dir != NULL)
7294 /* Reconstruct the comp_dir attribute to simplify the code below. */
7295 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7296 comp_dir->name = DW_AT_comp_dir;
7297 comp_dir->form = DW_FORM_string;
7298 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7299 DW_STRING (comp_dir) = stub_comp_dir;
7302 /* Set up for reading the DWO CU/TU. */
7303 cu->dwo_unit = dwo_unit;
7304 dwarf2_section_info *section = dwo_unit->section;
7305 dwarf2_read_section (objfile, section);
7306 abfd = get_section_bfd_owner (section);
7307 begin_info_ptr = info_ptr = (section->buffer
7308 + to_underlying (dwo_unit->sect_off));
7309 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7311 if (this_cu->is_debug_types)
7313 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7315 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7316 &cu->header, section,
7317 dwo_abbrev_section,
7318 info_ptr, rcuh_kind::TYPE);
7319 /* This is not an assert because it can be caused by bad debug info. */
7320 if (sig_type->signature != cu->header.signature)
7322 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7323 " TU at offset %s [in module %s]"),
7324 hex_string (sig_type->signature),
7325 hex_string (cu->header.signature),
7326 sect_offset_str (dwo_unit->sect_off),
7327 bfd_get_filename (abfd));
7329 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7330 /* For DWOs coming from DWP files, we don't know the CU length
7331 nor the type's offset in the TU until now. */
7332 dwo_unit->length = get_cu_length (&cu->header);
7333 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7335 /* Establish the type offset that can be used to lookup the type.
7336 For DWO files, we don't know it until now. */
7337 sig_type->type_offset_in_section
7338 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7340 else
7342 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7343 &cu->header, section,
7344 dwo_abbrev_section,
7345 info_ptr, rcuh_kind::COMPILE);
7346 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7347 /* For DWOs coming from DWP files, we don't know the CU length
7348 until now. */
7349 dwo_unit->length = get_cu_length (&cu->header);
7352 *result_dwo_abbrev_table
7353 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7354 cu->header.abbrev_sect_off);
7355 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7356 result_dwo_abbrev_table->get ());
7358 /* Read in the die, but leave space to copy over the attributes
7359 from the stub. This has the benefit of simplifying the rest of
7360 the code - all the work to maintain the illusion of a single
7361 DW_TAG_{compile,type}_unit DIE is done here. */
7362 num_extra_attrs = ((stmt_list != NULL)
7363 + (low_pc != NULL)
7364 + (high_pc != NULL)
7365 + (ranges != NULL)
7366 + (comp_dir != NULL));
7367 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7368 result_has_children, num_extra_attrs);
7370 /* Copy over the attributes from the stub to the DIE we just read in. */
7371 comp_unit_die = *result_comp_unit_die;
7372 i = comp_unit_die->num_attrs;
7373 if (stmt_list != NULL)
7374 comp_unit_die->attrs[i++] = *stmt_list;
7375 if (low_pc != NULL)
7376 comp_unit_die->attrs[i++] = *low_pc;
7377 if (high_pc != NULL)
7378 comp_unit_die->attrs[i++] = *high_pc;
7379 if (ranges != NULL)
7380 comp_unit_die->attrs[i++] = *ranges;
7381 if (comp_dir != NULL)
7382 comp_unit_die->attrs[i++] = *comp_dir;
7383 comp_unit_die->num_attrs += num_extra_attrs;
7385 if (dwarf_die_debug)
7387 fprintf_unfiltered (gdb_stdlog,
7388 "Read die from %s@0x%x of %s:\n",
7389 get_section_name (section),
7390 (unsigned) (begin_info_ptr - section->buffer),
7391 bfd_get_filename (abfd));
7392 dump_die (comp_unit_die, dwarf_die_debug);
7395 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7396 TUs by skipping the stub and going directly to the entry in the DWO file.
7397 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7398 to get it via circuitous means. Blech. */
7399 if (comp_dir != NULL)
7400 result_reader->comp_dir = DW_STRING (comp_dir);
7402 /* Skip dummy compilation units. */
7403 if (info_ptr >= begin_info_ptr + dwo_unit->length
7404 || peek_abbrev_code (abfd, info_ptr) == 0)
7405 return 0;
7407 *result_info_ptr = info_ptr;
7408 return 1;
7411 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7412 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7413 signature is part of the header. */
7414 static gdb::optional<ULONGEST>
7415 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7417 if (cu->header.version >= 5)
7418 return cu->header.signature;
7419 struct attribute *attr;
7420 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7421 if (attr == nullptr)
7422 return gdb::optional<ULONGEST> ();
7423 return DW_UNSND (attr);
7426 /* Subroutine of init_cutu_and_read_dies to simplify it.
7427 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7428 Returns NULL if the specified DWO unit cannot be found. */
7430 static struct dwo_unit *
7431 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7432 struct die_info *comp_unit_die)
7434 struct dwarf2_cu *cu = this_cu->cu;
7435 struct dwo_unit *dwo_unit;
7436 const char *comp_dir, *dwo_name;
7438 gdb_assert (cu != NULL);
7440 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7441 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7442 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7444 if (this_cu->is_debug_types)
7446 struct signatured_type *sig_type;
7448 /* Since this_cu is the first member of struct signatured_type,
7449 we can go from a pointer to one to a pointer to the other. */
7450 sig_type = (struct signatured_type *) this_cu;
7451 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7453 else
7455 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7456 if (!signature.has_value ())
7457 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7458 " [in module %s]"),
7459 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7460 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7461 *signature);
7464 return dwo_unit;
7467 /* Subroutine of init_cutu_and_read_dies to simplify it.
7468 See it for a description of the parameters.
7469 Read a TU directly from a DWO file, bypassing the stub. */
7471 static void
7472 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7473 int use_existing_cu, int keep,
7474 die_reader_func_ftype *die_reader_func,
7475 void *data)
7477 std::unique_ptr<dwarf2_cu> new_cu;
7478 struct signatured_type *sig_type;
7479 struct die_reader_specs reader;
7480 const gdb_byte *info_ptr;
7481 struct die_info *comp_unit_die;
7482 int has_children;
7483 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7485 /* Verify we can do the following downcast, and that we have the
7486 data we need. */
7487 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7488 sig_type = (struct signatured_type *) this_cu;
7489 gdb_assert (sig_type->dwo_unit != NULL);
7491 if (use_existing_cu && this_cu->cu != NULL)
7493 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7494 /* There's no need to do the rereading_dwo_cu handling that
7495 init_cutu_and_read_dies does since we don't read the stub. */
7497 else
7499 /* If !use_existing_cu, this_cu->cu must be NULL. */
7500 gdb_assert (this_cu->cu == NULL);
7501 new_cu.reset (new dwarf2_cu (this_cu));
7504 /* A future optimization, if needed, would be to use an existing
7505 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7506 could share abbrev tables. */
7508 /* The abbreviation table used by READER, this must live at least as long as
7509 READER. */
7510 abbrev_table_up dwo_abbrev_table;
7512 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7513 NULL /* stub_comp_unit_die */,
7514 sig_type->dwo_unit->dwo_file->comp_dir,
7515 &reader, &info_ptr,
7516 &comp_unit_die, &has_children,
7517 &dwo_abbrev_table) == 0)
7519 /* Dummy die. */
7520 return;
7523 /* All the "real" work is done here. */
7524 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7526 /* This duplicates the code in init_cutu_and_read_dies,
7527 but the alternative is making the latter more complex.
7528 This function is only for the special case of using DWO files directly:
7529 no point in overly complicating the general case just to handle this. */
7530 if (new_cu != NULL && keep)
7532 /* Link this CU into read_in_chain. */
7533 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7534 dwarf2_per_objfile->read_in_chain = this_cu;
7535 /* The chain owns it now. */
7536 new_cu.release ();
7540 /* Initialize a CU (or TU) and read its DIEs.
7541 If the CU defers to a DWO file, read the DWO file as well.
7543 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7544 Otherwise the table specified in the comp unit header is read in and used.
7545 This is an optimization for when we already have the abbrev table.
7547 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7548 Otherwise, a new CU is allocated with xmalloc.
7550 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7551 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7553 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7554 linker) then DIE_READER_FUNC will not get called. */
7556 static void
7557 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7558 struct abbrev_table *abbrev_table,
7559 int use_existing_cu, int keep,
7560 bool skip_partial,
7561 die_reader_func_ftype *die_reader_func,
7562 void *data)
7564 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7565 struct objfile *objfile = dwarf2_per_objfile->objfile;
7566 struct dwarf2_section_info *section = this_cu->section;
7567 bfd *abfd = get_section_bfd_owner (section);
7568 struct dwarf2_cu *cu;
7569 const gdb_byte *begin_info_ptr, *info_ptr;
7570 struct die_reader_specs reader;
7571 struct die_info *comp_unit_die;
7572 int has_children;
7573 struct signatured_type *sig_type = NULL;
7574 struct dwarf2_section_info *abbrev_section;
7575 /* Non-zero if CU currently points to a DWO file and we need to
7576 reread it. When this happens we need to reread the skeleton die
7577 before we can reread the DWO file (this only applies to CUs, not TUs). */
7578 int rereading_dwo_cu = 0;
7580 if (dwarf_die_debug)
7581 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7582 this_cu->is_debug_types ? "type" : "comp",
7583 sect_offset_str (this_cu->sect_off));
7585 if (use_existing_cu)
7586 gdb_assert (keep);
7588 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7589 file (instead of going through the stub), short-circuit all of this. */
7590 if (this_cu->reading_dwo_directly)
7592 /* Narrow down the scope of possibilities to have to understand. */
7593 gdb_assert (this_cu->is_debug_types);
7594 gdb_assert (abbrev_table == NULL);
7595 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7596 die_reader_func, data);
7597 return;
7600 /* This is cheap if the section is already read in. */
7601 dwarf2_read_section (objfile, section);
7603 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7605 abbrev_section = get_abbrev_section_for_cu (this_cu);
7607 std::unique_ptr<dwarf2_cu> new_cu;
7608 if (use_existing_cu && this_cu->cu != NULL)
7610 cu = this_cu->cu;
7611 /* If this CU is from a DWO file we need to start over, we need to
7612 refetch the attributes from the skeleton CU.
7613 This could be optimized by retrieving those attributes from when we
7614 were here the first time: the previous comp_unit_die was stored in
7615 comp_unit_obstack. But there's no data yet that we need this
7616 optimization. */
7617 if (cu->dwo_unit != NULL)
7618 rereading_dwo_cu = 1;
7620 else
7622 /* If !use_existing_cu, this_cu->cu must be NULL. */
7623 gdb_assert (this_cu->cu == NULL);
7624 new_cu.reset (new dwarf2_cu (this_cu));
7625 cu = new_cu.get ();
7628 /* Get the header. */
7629 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7631 /* We already have the header, there's no need to read it in again. */
7632 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7634 else
7636 if (this_cu->is_debug_types)
7638 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7639 &cu->header, section,
7640 abbrev_section, info_ptr,
7641 rcuh_kind::TYPE);
7643 /* Since per_cu is the first member of struct signatured_type,
7644 we can go from a pointer to one to a pointer to the other. */
7645 sig_type = (struct signatured_type *) this_cu;
7646 gdb_assert (sig_type->signature == cu->header.signature);
7647 gdb_assert (sig_type->type_offset_in_tu
7648 == cu->header.type_cu_offset_in_tu);
7649 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7651 /* LENGTH has not been set yet for type units if we're
7652 using .gdb_index. */
7653 this_cu->length = get_cu_length (&cu->header);
7655 /* Establish the type offset that can be used to lookup the type. */
7656 sig_type->type_offset_in_section =
7657 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7659 this_cu->dwarf_version = cu->header.version;
7661 else
7663 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7664 &cu->header, section,
7665 abbrev_section,
7666 info_ptr,
7667 rcuh_kind::COMPILE);
7669 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7670 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7671 this_cu->dwarf_version = cu->header.version;
7675 /* Skip dummy compilation units. */
7676 if (info_ptr >= begin_info_ptr + this_cu->length
7677 || peek_abbrev_code (abfd, info_ptr) == 0)
7678 return;
7680 /* If we don't have them yet, read the abbrevs for this compilation unit.
7681 And if we need to read them now, make sure they're freed when we're
7682 done (own the table through ABBREV_TABLE_HOLDER). */
7683 abbrev_table_up abbrev_table_holder;
7684 if (abbrev_table != NULL)
7685 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7686 else
7688 abbrev_table_holder
7689 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7690 cu->header.abbrev_sect_off);
7691 abbrev_table = abbrev_table_holder.get ();
7694 /* Read the top level CU/TU die. */
7695 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7696 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7698 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7699 return;
7701 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7702 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7703 table from the DWO file and pass the ownership over to us. It will be
7704 referenced from READER, so we must make sure to free it after we're done
7705 with READER.
7707 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7708 DWO CU, that this test will fail (the attribute will not be present). */
7709 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7710 abbrev_table_up dwo_abbrev_table;
7711 if (dwo_name != nullptr)
7713 struct dwo_unit *dwo_unit;
7714 struct die_info *dwo_comp_unit_die;
7716 if (has_children)
7718 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7719 " has children (offset %s) [in module %s]"),
7720 sect_offset_str (this_cu->sect_off),
7721 bfd_get_filename (abfd));
7723 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7724 if (dwo_unit != NULL)
7726 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7727 comp_unit_die, NULL,
7728 &reader, &info_ptr,
7729 &dwo_comp_unit_die, &has_children,
7730 &dwo_abbrev_table) == 0)
7732 /* Dummy die. */
7733 return;
7735 comp_unit_die = dwo_comp_unit_die;
7737 else
7739 /* Yikes, we couldn't find the rest of the DIE, we only have
7740 the stub. A complaint has already been logged. There's
7741 not much more we can do except pass on the stub DIE to
7742 die_reader_func. We don't want to throw an error on bad
7743 debug info. */
7747 /* All of the above is setup for this call. Yikes. */
7748 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7750 /* Done, clean up. */
7751 if (new_cu != NULL && keep)
7753 /* Link this CU into read_in_chain. */
7754 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7755 dwarf2_per_objfile->read_in_chain = this_cu;
7756 /* The chain owns it now. */
7757 new_cu.release ();
7761 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7762 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7763 to have already done the lookup to find the DWO file).
7765 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7766 THIS_CU->is_debug_types, but nothing else.
7768 We fill in THIS_CU->length.
7770 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7771 linker) then DIE_READER_FUNC will not get called.
7773 THIS_CU->cu is always freed when done.
7774 This is done in order to not leave THIS_CU->cu in a state where we have
7775 to care whether it refers to the "main" CU or the DWO CU. */
7777 static void
7778 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7779 struct dwo_file *dwo_file,
7780 die_reader_func_ftype *die_reader_func,
7781 void *data)
7783 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7784 struct objfile *objfile = dwarf2_per_objfile->objfile;
7785 struct dwarf2_section_info *section = this_cu->section;
7786 bfd *abfd = get_section_bfd_owner (section);
7787 struct dwarf2_section_info *abbrev_section;
7788 const gdb_byte *begin_info_ptr, *info_ptr;
7789 struct die_reader_specs reader;
7790 struct die_info *comp_unit_die;
7791 int has_children;
7793 if (dwarf_die_debug)
7794 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7795 this_cu->is_debug_types ? "type" : "comp",
7796 sect_offset_str (this_cu->sect_off));
7798 gdb_assert (this_cu->cu == NULL);
7800 abbrev_section = (dwo_file != NULL
7801 ? &dwo_file->sections.abbrev
7802 : get_abbrev_section_for_cu (this_cu));
7804 /* This is cheap if the section is already read in. */
7805 dwarf2_read_section (objfile, section);
7807 struct dwarf2_cu cu (this_cu);
7809 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7810 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7811 &cu.header, section,
7812 abbrev_section, info_ptr,
7813 (this_cu->is_debug_types
7814 ? rcuh_kind::TYPE
7815 : rcuh_kind::COMPILE));
7817 this_cu->length = get_cu_length (&cu.header);
7819 /* Skip dummy compilation units. */
7820 if (info_ptr >= begin_info_ptr + this_cu->length
7821 || peek_abbrev_code (abfd, info_ptr) == 0)
7822 return;
7824 abbrev_table_up abbrev_table
7825 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7826 cu.header.abbrev_sect_off);
7828 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7829 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7831 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7834 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7835 does not lookup the specified DWO file.
7836 This cannot be used to read DWO files.
7838 THIS_CU->cu is always freed when done.
7839 This is done in order to not leave THIS_CU->cu in a state where we have
7840 to care whether it refers to the "main" CU or the DWO CU.
7841 We can revisit this if the data shows there's a performance issue. */
7843 static void
7844 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7845 die_reader_func_ftype *die_reader_func,
7846 void *data)
7848 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7851 /* Type Unit Groups.
7853 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7854 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7855 so that all types coming from the same compilation (.o file) are grouped
7856 together. A future step could be to put the types in the same symtab as
7857 the CU the types ultimately came from. */
7859 static hashval_t
7860 hash_type_unit_group (const void *item)
7862 const struct type_unit_group *tu_group
7863 = (const struct type_unit_group *) item;
7865 return hash_stmt_list_entry (&tu_group->hash);
7868 static int
7869 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7871 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7872 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7874 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7877 /* Allocate a hash table for type unit groups. */
7879 static htab_t
7880 allocate_type_unit_groups_table (struct objfile *objfile)
7882 return htab_create_alloc_ex (3,
7883 hash_type_unit_group,
7884 eq_type_unit_group,
7885 NULL,
7886 &objfile->objfile_obstack,
7887 hashtab_obstack_allocate,
7888 dummy_obstack_deallocate);
7891 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7892 partial symtabs. We combine several TUs per psymtab to not let the size
7893 of any one psymtab grow too big. */
7894 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7895 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7897 /* Helper routine for get_type_unit_group.
7898 Create the type_unit_group object used to hold one or more TUs. */
7900 static struct type_unit_group *
7901 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7903 struct dwarf2_per_objfile *dwarf2_per_objfile
7904 = cu->per_cu->dwarf2_per_objfile;
7905 struct objfile *objfile = dwarf2_per_objfile->objfile;
7906 struct dwarf2_per_cu_data *per_cu;
7907 struct type_unit_group *tu_group;
7909 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7910 struct type_unit_group);
7911 per_cu = &tu_group->per_cu;
7912 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7914 if (dwarf2_per_objfile->using_index)
7916 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7917 struct dwarf2_per_cu_quick_data);
7919 else
7921 unsigned int line_offset = to_underlying (line_offset_struct);
7922 struct partial_symtab *pst;
7923 std::string name;
7925 /* Give the symtab a useful name for debug purposes. */
7926 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7927 name = string_printf ("<type_units_%d>",
7928 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7929 else
7930 name = string_printf ("<type_units_at_0x%x>", line_offset);
7932 pst = create_partial_symtab (per_cu, name.c_str ());
7933 pst->anonymous = 1;
7936 tu_group->hash.dwo_unit = cu->dwo_unit;
7937 tu_group->hash.line_sect_off = line_offset_struct;
7939 return tu_group;
7942 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7943 STMT_LIST is a DW_AT_stmt_list attribute. */
7945 static struct type_unit_group *
7946 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7948 struct dwarf2_per_objfile *dwarf2_per_objfile
7949 = cu->per_cu->dwarf2_per_objfile;
7950 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7951 struct type_unit_group *tu_group;
7952 void **slot;
7953 unsigned int line_offset;
7954 struct type_unit_group type_unit_group_for_lookup;
7956 if (dwarf2_per_objfile->type_unit_groups == NULL)
7958 dwarf2_per_objfile->type_unit_groups =
7959 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7962 /* Do we need to create a new group, or can we use an existing one? */
7964 if (stmt_list)
7966 line_offset = DW_UNSND (stmt_list);
7967 ++tu_stats->nr_symtab_sharers;
7969 else
7971 /* Ugh, no stmt_list. Rare, but we have to handle it.
7972 We can do various things here like create one group per TU or
7973 spread them over multiple groups to split up the expansion work.
7974 To avoid worst case scenarios (too many groups or too large groups)
7975 we, umm, group them in bunches. */
7976 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7977 | (tu_stats->nr_stmt_less_type_units
7978 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7979 ++tu_stats->nr_stmt_less_type_units;
7982 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7983 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7984 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7985 &type_unit_group_for_lookup, INSERT);
7986 if (*slot != NULL)
7988 tu_group = (struct type_unit_group *) *slot;
7989 gdb_assert (tu_group != NULL);
7991 else
7993 sect_offset line_offset_struct = (sect_offset) line_offset;
7994 tu_group = create_type_unit_group (cu, line_offset_struct);
7995 *slot = tu_group;
7996 ++tu_stats->nr_symtabs;
7999 return tu_group;
8002 /* Partial symbol tables. */
8004 /* Create a psymtab named NAME and assign it to PER_CU.
8006 The caller must fill in the following details:
8007 dirname, textlow, texthigh. */
8009 static struct partial_symtab *
8010 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8012 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8013 struct partial_symtab *pst;
8015 pst = start_psymtab_common (objfile, name, 0);
8017 pst->psymtabs_addrmap_supported = 1;
8019 /* This is the glue that links PST into GDB's symbol API. */
8020 pst->read_symtab_private = per_cu;
8021 pst->read_symtab = dwarf2_read_symtab;
8022 per_cu->v.psymtab = pst;
8024 return pst;
8027 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8028 type. */
8030 struct process_psymtab_comp_unit_data
8032 /* True if we are reading a DW_TAG_partial_unit. */
8034 int want_partial_unit;
8036 /* The "pretend" language that is used if the CU doesn't declare a
8037 language. */
8039 enum language pretend_language;
8042 /* die_reader_func for process_psymtab_comp_unit. */
8044 static void
8045 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8046 const gdb_byte *info_ptr,
8047 struct die_info *comp_unit_die,
8048 int has_children,
8049 void *data)
8051 struct dwarf2_cu *cu = reader->cu;
8052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8054 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8055 CORE_ADDR baseaddr;
8056 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8057 struct partial_symtab *pst;
8058 enum pc_bounds_kind cu_bounds_kind;
8059 const char *filename;
8060 struct process_psymtab_comp_unit_data *info
8061 = (struct process_psymtab_comp_unit_data *) data;
8063 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8064 return;
8066 gdb_assert (! per_cu->is_debug_types);
8068 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8070 /* Allocate a new partial symbol table structure. */
8071 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8072 if (filename == NULL)
8073 filename = "";
8075 pst = create_partial_symtab (per_cu, filename);
8077 /* This must be done before calling dwarf2_build_include_psymtabs. */
8078 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8082 dwarf2_find_base_address (comp_unit_die, cu);
8084 /* Possibly set the default values of LOWPC and HIGHPC from
8085 `DW_AT_ranges'. */
8086 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8087 &best_highpc, cu, pst);
8088 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8090 CORE_ADDR low
8091 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8092 - baseaddr);
8093 CORE_ADDR high
8094 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8095 - baseaddr - 1);
8096 /* Store the contiguous range if it is not empty; it can be
8097 empty for CUs with no code. */
8098 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8099 low, high, pst);
8102 /* Check if comp unit has_children.
8103 If so, read the rest of the partial symbols from this comp unit.
8104 If not, there's no more debug_info for this comp unit. */
8105 if (has_children)
8107 struct partial_die_info *first_die;
8108 CORE_ADDR lowpc, highpc;
8110 lowpc = ((CORE_ADDR) -1);
8111 highpc = ((CORE_ADDR) 0);
8113 first_die = load_partial_dies (reader, info_ptr, 1);
8115 scan_partial_symbols (first_die, &lowpc, &highpc,
8116 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8118 /* If we didn't find a lowpc, set it to highpc to avoid
8119 complaints from `maint check'. */
8120 if (lowpc == ((CORE_ADDR) -1))
8121 lowpc = highpc;
8123 /* If the compilation unit didn't have an explicit address range,
8124 then use the information extracted from its child dies. */
8125 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8127 best_lowpc = lowpc;
8128 best_highpc = highpc;
8131 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8132 best_lowpc + baseaddr)
8133 - baseaddr);
8134 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8135 best_highpc + baseaddr)
8136 - baseaddr);
8138 end_psymtab_common (objfile, pst);
8140 if (!cu->per_cu->imported_symtabs_empty ())
8142 int i;
8143 int len = cu->per_cu->imported_symtabs_size ();
8145 /* Fill in 'dependencies' here; we fill in 'users' in a
8146 post-pass. */
8147 pst->number_of_dependencies = len;
8148 pst->dependencies
8149 = objfile->partial_symtabs->allocate_dependencies (len);
8150 for (i = 0; i < len; ++i)
8152 pst->dependencies[i]
8153 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8156 cu->per_cu->imported_symtabs_free ();
8159 /* Get the list of files included in the current compilation unit,
8160 and build a psymtab for each of them. */
8161 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8163 if (dwarf_read_debug)
8164 fprintf_unfiltered (gdb_stdlog,
8165 "Psymtab for %s unit @%s: %s - %s"
8166 ", %d global, %d static syms\n",
8167 per_cu->is_debug_types ? "type" : "comp",
8168 sect_offset_str (per_cu->sect_off),
8169 paddress (gdbarch, pst->text_low (objfile)),
8170 paddress (gdbarch, pst->text_high (objfile)),
8171 pst->n_global_syms, pst->n_static_syms);
8174 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8175 Process compilation unit THIS_CU for a psymtab. */
8177 static void
8178 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8179 int want_partial_unit,
8180 enum language pretend_language)
8182 /* If this compilation unit was already read in, free the
8183 cached copy in order to read it in again. This is
8184 necessary because we skipped some symbols when we first
8185 read in the compilation unit (see load_partial_dies).
8186 This problem could be avoided, but the benefit is unclear. */
8187 if (this_cu->cu != NULL)
8188 free_one_cached_comp_unit (this_cu);
8190 if (this_cu->is_debug_types)
8191 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8192 build_type_psymtabs_reader, NULL);
8193 else
8195 process_psymtab_comp_unit_data info;
8196 info.want_partial_unit = want_partial_unit;
8197 info.pretend_language = pretend_language;
8198 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8199 process_psymtab_comp_unit_reader, &info);
8202 /* Age out any secondary CUs. */
8203 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8206 /* Reader function for build_type_psymtabs. */
8208 static void
8209 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8210 const gdb_byte *info_ptr,
8211 struct die_info *type_unit_die,
8212 int has_children,
8213 void *data)
8215 struct dwarf2_per_objfile *dwarf2_per_objfile
8216 = reader->cu->per_cu->dwarf2_per_objfile;
8217 struct objfile *objfile = dwarf2_per_objfile->objfile;
8218 struct dwarf2_cu *cu = reader->cu;
8219 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8220 struct signatured_type *sig_type;
8221 struct type_unit_group *tu_group;
8222 struct attribute *attr;
8223 struct partial_die_info *first_die;
8224 CORE_ADDR lowpc, highpc;
8225 struct partial_symtab *pst;
8227 gdb_assert (data == NULL);
8228 gdb_assert (per_cu->is_debug_types);
8229 sig_type = (struct signatured_type *) per_cu;
8231 if (! has_children)
8232 return;
8234 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8235 tu_group = get_type_unit_group (cu, attr);
8237 if (tu_group->tus == nullptr)
8238 tu_group->tus = new std::vector<signatured_type *>;
8239 tu_group->tus->push_back (sig_type);
8241 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8242 pst = create_partial_symtab (per_cu, "");
8243 pst->anonymous = 1;
8245 first_die = load_partial_dies (reader, info_ptr, 1);
8247 lowpc = (CORE_ADDR) -1;
8248 highpc = (CORE_ADDR) 0;
8249 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251 end_psymtab_common (objfile, pst);
8254 /* Struct used to sort TUs by their abbreviation table offset. */
8256 struct tu_abbrev_offset
8258 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8259 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8262 signatured_type *sig_type;
8263 sect_offset abbrev_offset;
8266 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8268 static bool
8269 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8270 const struct tu_abbrev_offset &b)
8272 return a.abbrev_offset < b.abbrev_offset;
8275 /* Efficiently read all the type units.
8276 This does the bulk of the work for build_type_psymtabs.
8278 The efficiency is because we sort TUs by the abbrev table they use and
8279 only read each abbrev table once. In one program there are 200K TUs
8280 sharing 8K abbrev tables.
8282 The main purpose of this function is to support building the
8283 dwarf2_per_objfile->type_unit_groups table.
8284 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8285 can collapse the search space by grouping them by stmt_list.
8286 The savings can be significant, in the same program from above the 200K TUs
8287 share 8K stmt_list tables.
8289 FUNC is expected to call get_type_unit_group, which will create the
8290 struct type_unit_group if necessary and add it to
8291 dwarf2_per_objfile->type_unit_groups. */
8293 static void
8294 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8296 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8297 abbrev_table_up abbrev_table;
8298 sect_offset abbrev_offset;
8300 /* It's up to the caller to not call us multiple times. */
8301 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303 if (dwarf2_per_objfile->all_type_units.empty ())
8304 return;
8306 /* TUs typically share abbrev tables, and there can be way more TUs than
8307 abbrev tables. Sort by abbrev table to reduce the number of times we
8308 read each abbrev table in.
8309 Alternatives are to punt or to maintain a cache of abbrev tables.
8310 This is simpler and efficient enough for now.
8312 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8313 symtab to use). Typically TUs with the same abbrev offset have the same
8314 stmt_list value too so in practice this should work well.
8316 The basic algorithm here is:
8318 sort TUs by abbrev table
8319 for each TU with same abbrev table:
8320 read abbrev table if first user
8321 read TU top level DIE
8322 [IWBN if DWO skeletons had DW_AT_stmt_list]
8323 call FUNC */
8325 if (dwarf_read_debug)
8326 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328 /* Sort in a separate table to maintain the order of all_type_units
8329 for .gdb_index: TU indices directly index all_type_units. */
8330 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8331 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8334 sorted_by_abbrev.emplace_back
8335 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8336 sig_type->per_cu.section,
8337 sig_type->per_cu.sect_off));
8339 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8340 sort_tu_by_abbrev_offset);
8342 abbrev_offset = (sect_offset) ~(unsigned) 0;
8344 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8346 /* Switch to the next abbrev table if necessary. */
8347 if (abbrev_table == NULL
8348 || tu.abbrev_offset != abbrev_offset)
8350 abbrev_offset = tu.abbrev_offset;
8351 abbrev_table =
8352 abbrev_table_read_table (dwarf2_per_objfile,
8353 &dwarf2_per_objfile->abbrev,
8354 abbrev_offset);
8355 ++tu_stats->nr_uniq_abbrev_tables;
8358 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8359 0, 0, false, build_type_psymtabs_reader, NULL);
8363 /* Print collected type unit statistics. */
8365 static void
8366 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8368 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8370 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8371 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8372 dwarf2_per_objfile->all_type_units.size ());
8373 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8374 tu_stats->nr_uniq_abbrev_tables);
8375 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8376 tu_stats->nr_symtabs);
8377 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8378 tu_stats->nr_symtab_sharers);
8379 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8380 tu_stats->nr_stmt_less_type_units);
8381 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8382 tu_stats->nr_all_type_units_reallocs);
8385 /* Traversal function for build_type_psymtabs. */
8387 static int
8388 build_type_psymtab_dependencies (void **slot, void *info)
8390 struct dwarf2_per_objfile *dwarf2_per_objfile
8391 = (struct dwarf2_per_objfile *) info;
8392 struct objfile *objfile = dwarf2_per_objfile->objfile;
8393 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8394 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8395 struct partial_symtab *pst = per_cu->v.psymtab;
8396 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8397 int i;
8399 gdb_assert (len > 0);
8400 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8402 pst->number_of_dependencies = len;
8403 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8404 for (i = 0; i < len; ++i)
8406 struct signatured_type *iter = tu_group->tus->at (i);
8407 gdb_assert (iter->per_cu.is_debug_types);
8408 pst->dependencies[i] = iter->per_cu.v.psymtab;
8409 iter->type_unit_group = tu_group;
8412 delete tu_group->tus;
8413 tu_group->tus = nullptr;
8415 return 1;
8418 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8419 Build partial symbol tables for the .debug_types comp-units. */
8421 static void
8422 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8424 if (! create_all_type_units (dwarf2_per_objfile))
8425 return;
8427 build_type_psymtabs_1 (dwarf2_per_objfile);
8430 /* Traversal function for process_skeletonless_type_unit.
8431 Read a TU in a DWO file and build partial symbols for it. */
8433 static int
8434 process_skeletonless_type_unit (void **slot, void *info)
8436 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8437 struct dwarf2_per_objfile *dwarf2_per_objfile
8438 = (struct dwarf2_per_objfile *) info;
8439 struct signatured_type find_entry, *entry;
8441 /* If this TU doesn't exist in the global table, add it and read it in. */
8443 if (dwarf2_per_objfile->signatured_types == NULL)
8445 dwarf2_per_objfile->signatured_types
8446 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8449 find_entry.signature = dwo_unit->signature;
8450 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8451 INSERT);
8452 /* If we've already seen this type there's nothing to do. What's happening
8453 is we're doing our own version of comdat-folding here. */
8454 if (*slot != NULL)
8455 return 1;
8457 /* This does the job that create_all_type_units would have done for
8458 this TU. */
8459 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8460 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8461 *slot = entry;
8463 /* This does the job that build_type_psymtabs_1 would have done. */
8464 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8465 build_type_psymtabs_reader, NULL);
8467 return 1;
8470 /* Traversal function for process_skeletonless_type_units. */
8472 static int
8473 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477 if (dwo_file->tus != NULL)
8479 htab_traverse_noresize (dwo_file->tus,
8480 process_skeletonless_type_unit, info);
8483 return 1;
8486 /* Scan all TUs of DWO files, verifying we've processed them.
8487 This is needed in case a TU was emitted without its skeleton.
8488 Note: This can't be done until we know what all the DWO files are. */
8490 static void
8491 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8493 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8494 if (get_dwp_file (dwarf2_per_objfile) == NULL
8495 && dwarf2_per_objfile->dwo_files != NULL)
8497 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8498 process_dwo_file_for_skeletonless_type_units,
8499 dwarf2_per_objfile);
8503 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8505 static void
8506 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8508 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 struct partial_symtab *pst = per_cu->v.psymtab;
8512 if (pst == NULL)
8513 continue;
8515 for (int j = 0; j < pst->number_of_dependencies; ++j)
8517 /* Set the 'user' field only if it is not already set. */
8518 if (pst->dependencies[j]->user == NULL)
8519 pst->dependencies[j]->user = pst;
8524 /* Build the partial symbol table by doing a quick pass through the
8525 .debug_info and .debug_abbrev sections. */
8527 static void
8528 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8530 struct objfile *objfile = dwarf2_per_objfile->objfile;
8532 if (dwarf_read_debug)
8534 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8535 objfile_name (objfile));
8538 dwarf2_per_objfile->reading_partial_symbols = 1;
8540 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8542 /* Any cached compilation units will be linked by the per-objfile
8543 read_in_chain. Make sure to free them when we're done. */
8544 free_cached_comp_units freer (dwarf2_per_objfile);
8546 build_type_psymtabs (dwarf2_per_objfile);
8548 create_all_comp_units (dwarf2_per_objfile);
8550 /* Create a temporary address map on a temporary obstack. We later
8551 copy this to the final obstack. */
8552 auto_obstack temp_obstack;
8554 scoped_restore save_psymtabs_addrmap
8555 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8556 addrmap_create_mutable (&temp_obstack));
8558 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8559 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8561 /* This has to wait until we read the CUs, we need the list of DWOs. */
8562 process_skeletonless_type_units (dwarf2_per_objfile);
8564 /* Now that all TUs have been processed we can fill in the dependencies. */
8565 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8568 build_type_psymtab_dependencies, dwarf2_per_objfile);
8571 if (dwarf_read_debug)
8572 print_tu_stats (dwarf2_per_objfile);
8574 set_partial_user (dwarf2_per_objfile);
8576 objfile->partial_symtabs->psymtabs_addrmap
8577 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8578 objfile->partial_symtabs->obstack ());
8579 /* At this point we want to keep the address map. */
8580 save_psymtabs_addrmap.release ();
8582 if (dwarf_read_debug)
8583 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8584 objfile_name (objfile));
8587 /* die_reader_func for load_partial_comp_unit. */
8589 static void
8590 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8591 const gdb_byte *info_ptr,
8592 struct die_info *comp_unit_die,
8593 int has_children,
8594 void *data)
8596 struct dwarf2_cu *cu = reader->cu;
8598 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8600 /* Check if comp unit has_children.
8601 If so, read the rest of the partial symbols from this comp unit.
8602 If not, there's no more debug_info for this comp unit. */
8603 if (has_children)
8604 load_partial_dies (reader, info_ptr, 0);
8607 /* Load the partial DIEs for a secondary CU into memory.
8608 This is also used when rereading a primary CU with load_all_dies. */
8610 static void
8611 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8613 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8614 load_partial_comp_unit_reader, NULL);
8617 static void
8618 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8619 struct dwarf2_section_info *section,
8620 struct dwarf2_section_info *abbrev_section,
8621 unsigned int is_dwz)
8623 const gdb_byte *info_ptr;
8624 struct objfile *objfile = dwarf2_per_objfile->objfile;
8626 if (dwarf_read_debug)
8627 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8628 get_section_name (section),
8629 get_section_file_name (section));
8631 dwarf2_read_section (objfile, section);
8633 info_ptr = section->buffer;
8635 while (info_ptr < section->buffer + section->size)
8637 struct dwarf2_per_cu_data *this_cu;
8639 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8641 comp_unit_head cu_header;
8642 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8643 abbrev_section, info_ptr,
8644 rcuh_kind::COMPILE);
8646 /* Save the compilation unit for later lookup. */
8647 if (cu_header.unit_type != DW_UT_type)
8649 this_cu = XOBNEW (&objfile->objfile_obstack,
8650 struct dwarf2_per_cu_data);
8651 memset (this_cu, 0, sizeof (*this_cu));
8653 else
8655 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8656 struct signatured_type);
8657 memset (sig_type, 0, sizeof (*sig_type));
8658 sig_type->signature = cu_header.signature;
8659 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8660 this_cu = &sig_type->per_cu;
8662 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8663 this_cu->sect_off = sect_off;
8664 this_cu->length = cu_header.length + cu_header.initial_length_size;
8665 this_cu->is_dwz = is_dwz;
8666 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8667 this_cu->section = section;
8669 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8671 info_ptr = info_ptr + this_cu->length;
8675 /* Create a list of all compilation units in OBJFILE.
8676 This is only done for -readnow and building partial symtabs. */
8678 static void
8679 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8681 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8682 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8683 &dwarf2_per_objfile->abbrev, 0);
8685 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8686 if (dwz != NULL)
8687 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8691 /* Process all loaded DIEs for compilation unit CU, starting at
8692 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8693 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8694 DW_AT_ranges). See the comments of add_partial_subprogram on how
8695 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8697 static void
8698 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8699 CORE_ADDR *highpc, int set_addrmap,
8700 struct dwarf2_cu *cu)
8702 struct partial_die_info *pdi;
8704 /* Now, march along the PDI's, descending into ones which have
8705 interesting children but skipping the children of the other ones,
8706 until we reach the end of the compilation unit. */
8708 pdi = first_die;
8710 while (pdi != NULL)
8712 pdi->fixup (cu);
8714 /* Anonymous namespaces or modules have no name but have interesting
8715 children, so we need to look at them. Ditto for anonymous
8716 enums. */
8718 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8719 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8720 || pdi->tag == DW_TAG_imported_unit
8721 || pdi->tag == DW_TAG_inlined_subroutine)
8723 switch (pdi->tag)
8725 case DW_TAG_subprogram:
8726 case DW_TAG_inlined_subroutine:
8727 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8728 break;
8729 case DW_TAG_constant:
8730 case DW_TAG_variable:
8731 case DW_TAG_typedef:
8732 case DW_TAG_union_type:
8733 if (!pdi->is_declaration)
8735 add_partial_symbol (pdi, cu);
8737 break;
8738 case DW_TAG_class_type:
8739 case DW_TAG_interface_type:
8740 case DW_TAG_structure_type:
8741 if (!pdi->is_declaration)
8743 add_partial_symbol (pdi, cu);
8745 if ((cu->language == language_rust
8746 || cu->language == language_cplus) && pdi->has_children)
8747 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8748 set_addrmap, cu);
8749 break;
8750 case DW_TAG_enumeration_type:
8751 if (!pdi->is_declaration)
8752 add_partial_enumeration (pdi, cu);
8753 break;
8754 case DW_TAG_base_type:
8755 case DW_TAG_subrange_type:
8756 /* File scope base type definitions are added to the partial
8757 symbol table. */
8758 add_partial_symbol (pdi, cu);
8759 break;
8760 case DW_TAG_namespace:
8761 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8762 break;
8763 case DW_TAG_module:
8764 if (!pdi->is_declaration)
8765 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8766 break;
8767 case DW_TAG_imported_unit:
8769 struct dwarf2_per_cu_data *per_cu;
8771 /* For now we don't handle imported units in type units. */
8772 if (cu->per_cu->is_debug_types)
8774 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8775 " supported in type units [in module %s]"),
8776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8779 per_cu = dwarf2_find_containing_comp_unit
8780 (pdi->d.sect_off, pdi->is_dwz,
8781 cu->per_cu->dwarf2_per_objfile);
8783 /* Go read the partial unit, if needed. */
8784 if (per_cu->v.psymtab == NULL)
8785 process_psymtab_comp_unit (per_cu, 1, cu->language);
8787 cu->per_cu->imported_symtabs_push (per_cu);
8789 break;
8790 case DW_TAG_imported_declaration:
8791 add_partial_symbol (pdi, cu);
8792 break;
8793 default:
8794 break;
8798 /* If the die has a sibling, skip to the sibling. */
8800 pdi = pdi->die_sibling;
8804 /* Functions used to compute the fully scoped name of a partial DIE.
8806 Normally, this is simple. For C++, the parent DIE's fully scoped
8807 name is concatenated with "::" and the partial DIE's name.
8808 Enumerators are an exception; they use the scope of their parent
8809 enumeration type, i.e. the name of the enumeration type is not
8810 prepended to the enumerator.
8812 There are two complexities. One is DW_AT_specification; in this
8813 case "parent" means the parent of the target of the specification,
8814 instead of the direct parent of the DIE. The other is compilers
8815 which do not emit DW_TAG_namespace; in this case we try to guess
8816 the fully qualified name of structure types from their members'
8817 linkage names. This must be done using the DIE's children rather
8818 than the children of any DW_AT_specification target. We only need
8819 to do this for structures at the top level, i.e. if the target of
8820 any DW_AT_specification (if any; otherwise the DIE itself) does not
8821 have a parent. */
8823 /* Compute the scope prefix associated with PDI's parent, in
8824 compilation unit CU. The result will be allocated on CU's
8825 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8826 field. NULL is returned if no prefix is necessary. */
8827 static const char *
8828 partial_die_parent_scope (struct partial_die_info *pdi,
8829 struct dwarf2_cu *cu)
8831 const char *grandparent_scope;
8832 struct partial_die_info *parent, *real_pdi;
8834 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8835 then this means the parent of the specification DIE. */
8837 real_pdi = pdi;
8838 while (real_pdi->has_specification)
8840 auto res = find_partial_die (real_pdi->spec_offset,
8841 real_pdi->spec_is_dwz, cu);
8842 real_pdi = res.pdi;
8843 cu = res.cu;
8846 parent = real_pdi->die_parent;
8847 if (parent == NULL)
8848 return NULL;
8850 if (parent->scope_set)
8851 return parent->scope;
8853 parent->fixup (cu);
8855 grandparent_scope = partial_die_parent_scope (parent, cu);
8857 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8858 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8859 Work around this problem here. */
8860 if (cu->language == language_cplus
8861 && parent->tag == DW_TAG_namespace
8862 && strcmp (parent->name, "::") == 0
8863 && grandparent_scope == NULL)
8865 parent->scope = NULL;
8866 parent->scope_set = 1;
8867 return NULL;
8870 /* Nested subroutines in Fortran get a prefix. */
8871 if (pdi->tag == DW_TAG_enumerator)
8872 /* Enumerators should not get the name of the enumeration as a prefix. */
8873 parent->scope = grandparent_scope;
8874 else if (parent->tag == DW_TAG_namespace
8875 || parent->tag == DW_TAG_module
8876 || parent->tag == DW_TAG_structure_type
8877 || parent->tag == DW_TAG_class_type
8878 || parent->tag == DW_TAG_interface_type
8879 || parent->tag == DW_TAG_union_type
8880 || parent->tag == DW_TAG_enumeration_type
8881 || (cu->language == language_fortran
8882 && parent->tag == DW_TAG_subprogram
8883 && pdi->tag == DW_TAG_subprogram))
8885 if (grandparent_scope == NULL)
8886 parent->scope = parent->name;
8887 else
8888 parent->scope = typename_concat (&cu->comp_unit_obstack,
8889 grandparent_scope,
8890 parent->name, 0, cu);
8892 else
8894 /* FIXME drow/2004-04-01: What should we be doing with
8895 function-local names? For partial symbols, we should probably be
8896 ignoring them. */
8897 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8898 dwarf_tag_name (parent->tag),
8899 sect_offset_str (pdi->sect_off));
8900 parent->scope = grandparent_scope;
8903 parent->scope_set = 1;
8904 return parent->scope;
8907 /* Return the fully scoped name associated with PDI, from compilation unit
8908 CU. The result will be allocated with malloc. */
8910 static char *
8911 partial_die_full_name (struct partial_die_info *pdi,
8912 struct dwarf2_cu *cu)
8914 const char *parent_scope;
8916 /* If this is a template instantiation, we can not work out the
8917 template arguments from partial DIEs. So, unfortunately, we have
8918 to go through the full DIEs. At least any work we do building
8919 types here will be reused if full symbols are loaded later. */
8920 if (pdi->has_template_arguments)
8922 pdi->fixup (cu);
8924 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8926 struct die_info *die;
8927 struct attribute attr;
8928 struct dwarf2_cu *ref_cu = cu;
8930 /* DW_FORM_ref_addr is using section offset. */
8931 attr.name = (enum dwarf_attribute) 0;
8932 attr.form = DW_FORM_ref_addr;
8933 attr.u.unsnd = to_underlying (pdi->sect_off);
8934 die = follow_die_ref (NULL, &attr, &ref_cu);
8936 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8940 parent_scope = partial_die_parent_scope (pdi, cu);
8941 if (parent_scope == NULL)
8942 return NULL;
8943 else
8944 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8947 static void
8948 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8950 struct dwarf2_per_objfile *dwarf2_per_objfile
8951 = cu->per_cu->dwarf2_per_objfile;
8952 struct objfile *objfile = dwarf2_per_objfile->objfile;
8953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8954 CORE_ADDR addr = 0;
8955 const char *actual_name = NULL;
8956 CORE_ADDR baseaddr;
8957 char *built_actual_name;
8959 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8961 built_actual_name = partial_die_full_name (pdi, cu);
8962 if (built_actual_name != NULL)
8963 actual_name = built_actual_name;
8965 if (actual_name == NULL)
8966 actual_name = pdi->name;
8968 switch (pdi->tag)
8970 case DW_TAG_inlined_subroutine:
8971 case DW_TAG_subprogram:
8972 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8973 - baseaddr);
8974 if (pdi->is_external
8975 || cu->language == language_ada
8976 || (cu->language == language_fortran
8977 && pdi->die_parent != NULL
8978 && pdi->die_parent->tag == DW_TAG_subprogram))
8980 /* Normally, only "external" DIEs are part of the global scope.
8981 But in Ada and Fortran, we want to be able to access nested
8982 procedures globally. So all Ada and Fortran subprograms are
8983 stored in the global scope. */
8984 add_psymbol_to_list (actual_name,
8985 built_actual_name != NULL,
8986 VAR_DOMAIN, LOC_BLOCK,
8987 SECT_OFF_TEXT (objfile),
8988 psymbol_placement::GLOBAL,
8989 addr,
8990 cu->language, objfile);
8992 else
8994 add_psymbol_to_list (actual_name,
8995 built_actual_name != NULL,
8996 VAR_DOMAIN, LOC_BLOCK,
8997 SECT_OFF_TEXT (objfile),
8998 psymbol_placement::STATIC,
8999 addr, cu->language, objfile);
9002 if (pdi->main_subprogram && actual_name != NULL)
9003 set_objfile_main_name (objfile, actual_name, cu->language);
9004 break;
9005 case DW_TAG_constant:
9006 add_psymbol_to_list (actual_name,
9007 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9008 -1, (pdi->is_external
9009 ? psymbol_placement::GLOBAL
9010 : psymbol_placement::STATIC),
9011 0, cu->language, objfile);
9012 break;
9013 case DW_TAG_variable:
9014 if (pdi->d.locdesc)
9015 addr = decode_locdesc (pdi->d.locdesc, cu);
9017 if (pdi->d.locdesc
9018 && addr == 0
9019 && !dwarf2_per_objfile->has_section_at_zero)
9021 /* A global or static variable may also have been stripped
9022 out by the linker if unused, in which case its address
9023 will be nullified; do not add such variables into partial
9024 symbol table then. */
9026 else if (pdi->is_external)
9028 /* Global Variable.
9029 Don't enter into the minimal symbol tables as there is
9030 a minimal symbol table entry from the ELF symbols already.
9031 Enter into partial symbol table if it has a location
9032 descriptor or a type.
9033 If the location descriptor is missing, new_symbol will create
9034 a LOC_UNRESOLVED symbol, the address of the variable will then
9035 be determined from the minimal symbol table whenever the variable
9036 is referenced.
9037 The address for the partial symbol table entry is not
9038 used by GDB, but it comes in handy for debugging partial symbol
9039 table building. */
9041 if (pdi->d.locdesc || pdi->has_type)
9042 add_psymbol_to_list (actual_name,
9043 built_actual_name != NULL,
9044 VAR_DOMAIN, LOC_STATIC,
9045 SECT_OFF_TEXT (objfile),
9046 psymbol_placement::GLOBAL,
9047 addr, cu->language, objfile);
9049 else
9051 int has_loc = pdi->d.locdesc != NULL;
9053 /* Static Variable. Skip symbols whose value we cannot know (those
9054 without location descriptors or constant values). */
9055 if (!has_loc && !pdi->has_const_value)
9057 xfree (built_actual_name);
9058 return;
9061 add_psymbol_to_list (actual_name,
9062 built_actual_name != NULL,
9063 VAR_DOMAIN, LOC_STATIC,
9064 SECT_OFF_TEXT (objfile),
9065 psymbol_placement::STATIC,
9066 has_loc ? addr : 0,
9067 cu->language, objfile);
9069 break;
9070 case DW_TAG_typedef:
9071 case DW_TAG_base_type:
9072 case DW_TAG_subrange_type:
9073 add_psymbol_to_list (actual_name,
9074 built_actual_name != NULL,
9075 VAR_DOMAIN, LOC_TYPEDEF, -1,
9076 psymbol_placement::STATIC,
9077 0, cu->language, objfile);
9078 break;
9079 case DW_TAG_imported_declaration:
9080 case DW_TAG_namespace:
9081 add_psymbol_to_list (actual_name,
9082 built_actual_name != NULL,
9083 VAR_DOMAIN, LOC_TYPEDEF, -1,
9084 psymbol_placement::GLOBAL,
9085 0, cu->language, objfile);
9086 break;
9087 case DW_TAG_module:
9088 /* With Fortran 77 there might be a "BLOCK DATA" module
9089 available without any name. If so, we skip the module as it
9090 doesn't bring any value. */
9091 if (actual_name != nullptr)
9092 add_psymbol_to_list (actual_name,
9093 built_actual_name != NULL,
9094 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9095 psymbol_placement::GLOBAL,
9096 0, cu->language, objfile);
9097 break;
9098 case DW_TAG_class_type:
9099 case DW_TAG_interface_type:
9100 case DW_TAG_structure_type:
9101 case DW_TAG_union_type:
9102 case DW_TAG_enumeration_type:
9103 /* Skip external references. The DWARF standard says in the section
9104 about "Structure, Union, and Class Type Entries": "An incomplete
9105 structure, union or class type is represented by a structure,
9106 union or class entry that does not have a byte size attribute
9107 and that has a DW_AT_declaration attribute." */
9108 if (!pdi->has_byte_size && pdi->is_declaration)
9110 xfree (built_actual_name);
9111 return;
9114 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9115 static vs. global. */
9116 add_psymbol_to_list (actual_name,
9117 built_actual_name != NULL,
9118 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9119 cu->language == language_cplus
9120 ? psymbol_placement::GLOBAL
9121 : psymbol_placement::STATIC,
9122 0, cu->language, objfile);
9124 break;
9125 case DW_TAG_enumerator:
9126 add_psymbol_to_list (actual_name,
9127 built_actual_name != NULL,
9128 VAR_DOMAIN, LOC_CONST, -1,
9129 cu->language == language_cplus
9130 ? psymbol_placement::GLOBAL
9131 : psymbol_placement::STATIC,
9132 0, cu->language, objfile);
9133 break;
9134 default:
9135 break;
9138 xfree (built_actual_name);
9141 /* Read a partial die corresponding to a namespace; also, add a symbol
9142 corresponding to that namespace to the symbol table. NAMESPACE is
9143 the name of the enclosing namespace. */
9145 static void
9146 add_partial_namespace (struct partial_die_info *pdi,
9147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9148 int set_addrmap, struct dwarf2_cu *cu)
9150 /* Add a symbol for the namespace. */
9152 add_partial_symbol (pdi, cu);
9154 /* Now scan partial symbols in that namespace. */
9156 if (pdi->has_children)
9157 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9160 /* Read a partial die corresponding to a Fortran module. */
9162 static void
9163 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9164 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9166 /* Add a symbol for the namespace. */
9168 add_partial_symbol (pdi, cu);
9170 /* Now scan partial symbols in that module. */
9172 if (pdi->has_children)
9173 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9176 /* Read a partial die corresponding to a subprogram or an inlined
9177 subprogram and create a partial symbol for that subprogram.
9178 When the CU language allows it, this routine also defines a partial
9179 symbol for each nested subprogram that this subprogram contains.
9180 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9181 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9183 PDI may also be a lexical block, in which case we simply search
9184 recursively for subprograms defined inside that lexical block.
9185 Again, this is only performed when the CU language allows this
9186 type of definitions. */
9188 static void
9189 add_partial_subprogram (struct partial_die_info *pdi,
9190 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9191 int set_addrmap, struct dwarf2_cu *cu)
9193 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9195 if (pdi->has_pc_info)
9197 if (pdi->lowpc < *lowpc)
9198 *lowpc = pdi->lowpc;
9199 if (pdi->highpc > *highpc)
9200 *highpc = pdi->highpc;
9201 if (set_addrmap)
9203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9205 CORE_ADDR baseaddr;
9206 CORE_ADDR this_highpc;
9207 CORE_ADDR this_lowpc;
9209 baseaddr = ANOFFSET (objfile->section_offsets,
9210 SECT_OFF_TEXT (objfile));
9211 this_lowpc
9212 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9213 pdi->lowpc + baseaddr)
9214 - baseaddr);
9215 this_highpc
9216 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9217 pdi->highpc + baseaddr)
9218 - baseaddr);
9219 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9220 this_lowpc, this_highpc - 1,
9221 cu->per_cu->v.psymtab);
9225 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9227 if (!pdi->is_declaration)
9228 /* Ignore subprogram DIEs that do not have a name, they are
9229 illegal. Do not emit a complaint at this point, we will
9230 do so when we convert this psymtab into a symtab. */
9231 if (pdi->name)
9232 add_partial_symbol (pdi, cu);
9236 if (! pdi->has_children)
9237 return;
9239 if (cu->language == language_ada || cu->language == language_fortran)
9241 pdi = pdi->die_child;
9242 while (pdi != NULL)
9244 pdi->fixup (cu);
9245 if (pdi->tag == DW_TAG_subprogram
9246 || pdi->tag == DW_TAG_inlined_subroutine
9247 || pdi->tag == DW_TAG_lexical_block)
9248 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9249 pdi = pdi->die_sibling;
9254 /* Read a partial die corresponding to an enumeration type. */
9256 static void
9257 add_partial_enumeration (struct partial_die_info *enum_pdi,
9258 struct dwarf2_cu *cu)
9260 struct partial_die_info *pdi;
9262 if (enum_pdi->name != NULL)
9263 add_partial_symbol (enum_pdi, cu);
9265 pdi = enum_pdi->die_child;
9266 while (pdi)
9268 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9269 complaint (_("malformed enumerator DIE ignored"));
9270 else
9271 add_partial_symbol (pdi, cu);
9272 pdi = pdi->die_sibling;
9276 /* Return the initial uleb128 in the die at INFO_PTR. */
9278 static unsigned int
9279 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9281 unsigned int bytes_read;
9283 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9286 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9287 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9289 Return the corresponding abbrev, or NULL if the number is zero (indicating
9290 an empty DIE). In either case *BYTES_READ will be set to the length of
9291 the initial number. */
9293 static struct abbrev_info *
9294 peek_die_abbrev (const die_reader_specs &reader,
9295 const gdb_byte *info_ptr, unsigned int *bytes_read)
9297 dwarf2_cu *cu = reader.cu;
9298 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9299 unsigned int abbrev_number
9300 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9302 if (abbrev_number == 0)
9303 return NULL;
9305 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9306 if (!abbrev)
9308 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9309 " at offset %s [in module %s]"),
9310 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9311 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9314 return abbrev;
9317 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9318 Returns a pointer to the end of a series of DIEs, terminated by an empty
9319 DIE. Any children of the skipped DIEs will also be skipped. */
9321 static const gdb_byte *
9322 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9324 while (1)
9326 unsigned int bytes_read;
9327 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9329 if (abbrev == NULL)
9330 return info_ptr + bytes_read;
9331 else
9332 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9336 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9337 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9338 abbrev corresponding to that skipped uleb128 should be passed in
9339 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9340 children. */
9342 static const gdb_byte *
9343 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9344 struct abbrev_info *abbrev)
9346 unsigned int bytes_read;
9347 struct attribute attr;
9348 bfd *abfd = reader->abfd;
9349 struct dwarf2_cu *cu = reader->cu;
9350 const gdb_byte *buffer = reader->buffer;
9351 const gdb_byte *buffer_end = reader->buffer_end;
9352 unsigned int form, i;
9354 for (i = 0; i < abbrev->num_attrs; i++)
9356 /* The only abbrev we care about is DW_AT_sibling. */
9357 if (abbrev->attrs[i].name == DW_AT_sibling)
9359 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9360 if (attr.form == DW_FORM_ref_addr)
9361 complaint (_("ignoring absolute DW_AT_sibling"));
9362 else
9364 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9365 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9367 if (sibling_ptr < info_ptr)
9368 complaint (_("DW_AT_sibling points backwards"));
9369 else if (sibling_ptr > reader->buffer_end)
9370 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9371 else
9372 return sibling_ptr;
9376 /* If it isn't DW_AT_sibling, skip this attribute. */
9377 form = abbrev->attrs[i].form;
9378 skip_attribute:
9379 switch (form)
9381 case DW_FORM_ref_addr:
9382 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9383 and later it is offset sized. */
9384 if (cu->header.version == 2)
9385 info_ptr += cu->header.addr_size;
9386 else
9387 info_ptr += cu->header.offset_size;
9388 break;
9389 case DW_FORM_GNU_ref_alt:
9390 info_ptr += cu->header.offset_size;
9391 break;
9392 case DW_FORM_addr:
9393 info_ptr += cu->header.addr_size;
9394 break;
9395 case DW_FORM_data1:
9396 case DW_FORM_ref1:
9397 case DW_FORM_flag:
9398 case DW_FORM_strx1:
9399 info_ptr += 1;
9400 break;
9401 case DW_FORM_flag_present:
9402 case DW_FORM_implicit_const:
9403 break;
9404 case DW_FORM_data2:
9405 case DW_FORM_ref2:
9406 case DW_FORM_strx2:
9407 info_ptr += 2;
9408 break;
9409 case DW_FORM_strx3:
9410 info_ptr += 3;
9411 break;
9412 case DW_FORM_data4:
9413 case DW_FORM_ref4:
9414 case DW_FORM_strx4:
9415 info_ptr += 4;
9416 break;
9417 case DW_FORM_data8:
9418 case DW_FORM_ref8:
9419 case DW_FORM_ref_sig8:
9420 info_ptr += 8;
9421 break;
9422 case DW_FORM_data16:
9423 info_ptr += 16;
9424 break;
9425 case DW_FORM_string:
9426 read_direct_string (abfd, info_ptr, &bytes_read);
9427 info_ptr += bytes_read;
9428 break;
9429 case DW_FORM_sec_offset:
9430 case DW_FORM_strp:
9431 case DW_FORM_GNU_strp_alt:
9432 info_ptr += cu->header.offset_size;
9433 break;
9434 case DW_FORM_exprloc:
9435 case DW_FORM_block:
9436 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9437 info_ptr += bytes_read;
9438 break;
9439 case DW_FORM_block1:
9440 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9441 break;
9442 case DW_FORM_block2:
9443 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9444 break;
9445 case DW_FORM_block4:
9446 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9447 break;
9448 case DW_FORM_addrx:
9449 case DW_FORM_strx:
9450 case DW_FORM_sdata:
9451 case DW_FORM_udata:
9452 case DW_FORM_ref_udata:
9453 case DW_FORM_GNU_addr_index:
9454 case DW_FORM_GNU_str_index:
9455 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9456 break;
9457 case DW_FORM_indirect:
9458 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9459 info_ptr += bytes_read;
9460 /* We need to continue parsing from here, so just go back to
9461 the top. */
9462 goto skip_attribute;
9464 default:
9465 error (_("Dwarf Error: Cannot handle %s "
9466 "in DWARF reader [in module %s]"),
9467 dwarf_form_name (form),
9468 bfd_get_filename (abfd));
9472 if (abbrev->has_children)
9473 return skip_children (reader, info_ptr);
9474 else
9475 return info_ptr;
9478 /* Locate ORIG_PDI's sibling.
9479 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9481 static const gdb_byte *
9482 locate_pdi_sibling (const struct die_reader_specs *reader,
9483 struct partial_die_info *orig_pdi,
9484 const gdb_byte *info_ptr)
9486 /* Do we know the sibling already? */
9488 if (orig_pdi->sibling)
9489 return orig_pdi->sibling;
9491 /* Are there any children to deal with? */
9493 if (!orig_pdi->has_children)
9494 return info_ptr;
9496 /* Skip the children the long way. */
9498 return skip_children (reader, info_ptr);
9501 /* Expand this partial symbol table into a full symbol table. SELF is
9502 not NULL. */
9504 static void
9505 dwarf2_read_symtab (struct partial_symtab *self,
9506 struct objfile *objfile)
9508 struct dwarf2_per_objfile *dwarf2_per_objfile
9509 = get_dwarf2_per_objfile (objfile);
9511 if (self->readin)
9513 warning (_("bug: psymtab for %s is already read in."),
9514 self->filename);
9516 else
9518 if (info_verbose)
9520 printf_filtered (_("Reading in symbols for %s..."),
9521 self->filename);
9522 gdb_flush (gdb_stdout);
9525 /* If this psymtab is constructed from a debug-only objfile, the
9526 has_section_at_zero flag will not necessarily be correct. We
9527 can get the correct value for this flag by looking at the data
9528 associated with the (presumably stripped) associated objfile. */
9529 if (objfile->separate_debug_objfile_backlink)
9531 struct dwarf2_per_objfile *dpo_backlink
9532 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9534 dwarf2_per_objfile->has_section_at_zero
9535 = dpo_backlink->has_section_at_zero;
9538 dwarf2_per_objfile->reading_partial_symbols = 0;
9540 psymtab_to_symtab_1 (self);
9542 /* Finish up the debug error message. */
9543 if (info_verbose)
9544 printf_filtered (_("done.\n"));
9547 process_cu_includes (dwarf2_per_objfile);
9550 /* Reading in full CUs. */
9552 /* Add PER_CU to the queue. */
9554 static void
9555 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9556 enum language pretend_language)
9558 struct dwarf2_queue_item *item;
9560 per_cu->queued = 1;
9561 item = XNEW (struct dwarf2_queue_item);
9562 item->per_cu = per_cu;
9563 item->pretend_language = pretend_language;
9564 item->next = NULL;
9566 if (dwarf2_queue == NULL)
9567 dwarf2_queue = item;
9568 else
9569 dwarf2_queue_tail->next = item;
9571 dwarf2_queue_tail = item;
9574 /* If PER_CU is not yet queued, add it to the queue.
9575 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9576 dependency.
9577 The result is non-zero if PER_CU was queued, otherwise the result is zero
9578 meaning either PER_CU is already queued or it is already loaded.
9580 N.B. There is an invariant here that if a CU is queued then it is loaded.
9581 The caller is required to load PER_CU if we return non-zero. */
9583 static int
9584 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9585 struct dwarf2_per_cu_data *per_cu,
9586 enum language pretend_language)
9588 /* We may arrive here during partial symbol reading, if we need full
9589 DIEs to process an unusual case (e.g. template arguments). Do
9590 not queue PER_CU, just tell our caller to load its DIEs. */
9591 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9593 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9594 return 1;
9595 return 0;
9598 /* Mark the dependence relation so that we don't flush PER_CU
9599 too early. */
9600 if (dependent_cu != NULL)
9601 dwarf2_add_dependence (dependent_cu, per_cu);
9603 /* If it's already on the queue, we have nothing to do. */
9604 if (per_cu->queued)
9605 return 0;
9607 /* If the compilation unit is already loaded, just mark it as
9608 used. */
9609 if (per_cu->cu != NULL)
9611 per_cu->cu->last_used = 0;
9612 return 0;
9615 /* Add it to the queue. */
9616 queue_comp_unit (per_cu, pretend_language);
9618 return 1;
9621 /* Process the queue. */
9623 static void
9624 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9626 struct dwarf2_queue_item *item, *next_item;
9628 if (dwarf_read_debug)
9630 fprintf_unfiltered (gdb_stdlog,
9631 "Expanding one or more symtabs of objfile %s ...\n",
9632 objfile_name (dwarf2_per_objfile->objfile));
9635 /* The queue starts out with one item, but following a DIE reference
9636 may load a new CU, adding it to the end of the queue. */
9637 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9639 if ((dwarf2_per_objfile->using_index
9640 ? !item->per_cu->v.quick->compunit_symtab
9641 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9642 /* Skip dummy CUs. */
9643 && item->per_cu->cu != NULL)
9645 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9646 unsigned int debug_print_threshold;
9647 char buf[100];
9649 if (per_cu->is_debug_types)
9651 struct signatured_type *sig_type =
9652 (struct signatured_type *) per_cu;
9654 sprintf (buf, "TU %s at offset %s",
9655 hex_string (sig_type->signature),
9656 sect_offset_str (per_cu->sect_off));
9657 /* There can be 100s of TUs.
9658 Only print them in verbose mode. */
9659 debug_print_threshold = 2;
9661 else
9663 sprintf (buf, "CU at offset %s",
9664 sect_offset_str (per_cu->sect_off));
9665 debug_print_threshold = 1;
9668 if (dwarf_read_debug >= debug_print_threshold)
9669 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9671 if (per_cu->is_debug_types)
9672 process_full_type_unit (per_cu, item->pretend_language);
9673 else
9674 process_full_comp_unit (per_cu, item->pretend_language);
9676 if (dwarf_read_debug >= debug_print_threshold)
9677 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9680 item->per_cu->queued = 0;
9681 next_item = item->next;
9682 xfree (item);
9685 dwarf2_queue_tail = NULL;
9687 if (dwarf_read_debug)
9689 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9690 objfile_name (dwarf2_per_objfile->objfile));
9694 /* Read in full symbols for PST, and anything it depends on. */
9696 static void
9697 psymtab_to_symtab_1 (struct partial_symtab *pst)
9699 struct dwarf2_per_cu_data *per_cu;
9700 int i;
9702 if (pst->readin)
9703 return;
9705 for (i = 0; i < pst->number_of_dependencies; i++)
9706 if (!pst->dependencies[i]->readin
9707 && pst->dependencies[i]->user == NULL)
9709 /* Inform about additional files that need to be read in. */
9710 if (info_verbose)
9712 /* FIXME: i18n: Need to make this a single string. */
9713 fputs_filtered (" ", gdb_stdout);
9714 wrap_here ("");
9715 fputs_filtered ("and ", gdb_stdout);
9716 wrap_here ("");
9717 printf_filtered ("%s...", pst->dependencies[i]->filename);
9718 wrap_here (""); /* Flush output. */
9719 gdb_flush (gdb_stdout);
9721 psymtab_to_symtab_1 (pst->dependencies[i]);
9724 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9726 if (per_cu == NULL)
9728 /* It's an include file, no symbols to read for it.
9729 Everything is in the parent symtab. */
9730 pst->readin = 1;
9731 return;
9734 dw2_do_instantiate_symtab (per_cu, false);
9737 /* Trivial hash function for die_info: the hash value of a DIE
9738 is its offset in .debug_info for this objfile. */
9740 static hashval_t
9741 die_hash (const void *item)
9743 const struct die_info *die = (const struct die_info *) item;
9745 return to_underlying (die->sect_off);
9748 /* Trivial comparison function for die_info structures: two DIEs
9749 are equal if they have the same offset. */
9751 static int
9752 die_eq (const void *item_lhs, const void *item_rhs)
9754 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9755 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9757 return die_lhs->sect_off == die_rhs->sect_off;
9760 /* die_reader_func for load_full_comp_unit.
9761 This is identical to read_signatured_type_reader,
9762 but is kept separate for now. */
9764 static void
9765 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9766 const gdb_byte *info_ptr,
9767 struct die_info *comp_unit_die,
9768 int has_children,
9769 void *data)
9771 struct dwarf2_cu *cu = reader->cu;
9772 enum language *language_ptr = (enum language *) data;
9774 gdb_assert (cu->die_hash == NULL);
9775 cu->die_hash =
9776 htab_create_alloc_ex (cu->header.length / 12,
9777 die_hash,
9778 die_eq,
9779 NULL,
9780 &cu->comp_unit_obstack,
9781 hashtab_obstack_allocate,
9782 dummy_obstack_deallocate);
9784 if (has_children)
9785 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9786 &info_ptr, comp_unit_die);
9787 cu->dies = comp_unit_die;
9788 /* comp_unit_die is not stored in die_hash, no need. */
9790 /* We try not to read any attributes in this function, because not
9791 all CUs needed for references have been loaded yet, and symbol
9792 table processing isn't initialized. But we have to set the CU language,
9793 or we won't be able to build types correctly.
9794 Similarly, if we do not read the producer, we can not apply
9795 producer-specific interpretation. */
9796 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9799 /* Load the DIEs associated with PER_CU into memory. */
9801 static void
9802 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9803 bool skip_partial,
9804 enum language pretend_language)
9806 gdb_assert (! this_cu->is_debug_types);
9808 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9809 load_full_comp_unit_reader, &pretend_language);
9812 /* Add a DIE to the delayed physname list. */
9814 static void
9815 add_to_method_list (struct type *type, int fnfield_index, int index,
9816 const char *name, struct die_info *die,
9817 struct dwarf2_cu *cu)
9819 struct delayed_method_info mi;
9820 mi.type = type;
9821 mi.fnfield_index = fnfield_index;
9822 mi.index = index;
9823 mi.name = name;
9824 mi.die = die;
9825 cu->method_list.push_back (mi);
9828 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9829 "const" / "volatile". If so, decrements LEN by the length of the
9830 modifier and return true. Otherwise return false. */
9832 template<size_t N>
9833 static bool
9834 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9836 size_t mod_len = sizeof (mod) - 1;
9837 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9839 len -= mod_len;
9840 return true;
9842 return false;
9845 /* Compute the physnames of any methods on the CU's method list.
9847 The computation of method physnames is delayed in order to avoid the
9848 (bad) condition that one of the method's formal parameters is of an as yet
9849 incomplete type. */
9851 static void
9852 compute_delayed_physnames (struct dwarf2_cu *cu)
9854 /* Only C++ delays computing physnames. */
9855 if (cu->method_list.empty ())
9856 return;
9857 gdb_assert (cu->language == language_cplus);
9859 for (const delayed_method_info &mi : cu->method_list)
9861 const char *physname;
9862 struct fn_fieldlist *fn_flp
9863 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9864 physname = dwarf2_physname (mi.name, mi.die, cu);
9865 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9866 = physname ? physname : "";
9868 /* Since there's no tag to indicate whether a method is a
9869 const/volatile overload, extract that information out of the
9870 demangled name. */
9871 if (physname != NULL)
9873 size_t len = strlen (physname);
9875 while (1)
9877 if (physname[len] == ')') /* shortcut */
9878 break;
9879 else if (check_modifier (physname, len, " const"))
9880 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9881 else if (check_modifier (physname, len, " volatile"))
9882 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9883 else
9884 break;
9889 /* The list is no longer needed. */
9890 cu->method_list.clear ();
9893 /* Go objects should be embedded in a DW_TAG_module DIE,
9894 and it's not clear if/how imported objects will appear.
9895 To keep Go support simple until that's worked out,
9896 go back through what we've read and create something usable.
9897 We could do this while processing each DIE, and feels kinda cleaner,
9898 but that way is more invasive.
9899 This is to, for example, allow the user to type "p var" or "b main"
9900 without having to specify the package name, and allow lookups
9901 of module.object to work in contexts that use the expression
9902 parser. */
9904 static void
9905 fixup_go_packaging (struct dwarf2_cu *cu)
9907 char *package_name = NULL;
9908 struct pending *list;
9909 int i;
9911 for (list = *cu->get_builder ()->get_global_symbols ();
9912 list != NULL;
9913 list = list->next)
9915 for (i = 0; i < list->nsyms; ++i)
9917 struct symbol *sym = list->symbol[i];
9919 if (sym->language () == language_go
9920 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9922 char *this_package_name = go_symbol_package_name (sym);
9924 if (this_package_name == NULL)
9925 continue;
9926 if (package_name == NULL)
9927 package_name = this_package_name;
9928 else
9930 struct objfile *objfile
9931 = cu->per_cu->dwarf2_per_objfile->objfile;
9932 if (strcmp (package_name, this_package_name) != 0)
9933 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9934 (symbol_symtab (sym) != NULL
9935 ? symtab_to_filename_for_display
9936 (symbol_symtab (sym))
9937 : objfile_name (objfile)),
9938 this_package_name, package_name);
9939 xfree (this_package_name);
9945 if (package_name != NULL)
9947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9948 const char *saved_package_name
9949 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9950 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9951 saved_package_name);
9952 struct symbol *sym;
9954 sym = allocate_symbol (objfile);
9955 sym->set_language (language_go, &objfile->objfile_obstack);
9956 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9957 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9958 e.g., "main" finds the "main" module and not C's main(). */
9959 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9960 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9961 SYMBOL_TYPE (sym) = type;
9963 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9965 xfree (package_name);
9969 /* Allocate a fully-qualified name consisting of the two parts on the
9970 obstack. */
9972 static const char *
9973 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9975 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9978 /* A helper that allocates a struct discriminant_info to attach to a
9979 union type. */
9981 static struct discriminant_info *
9982 alloc_discriminant_info (struct type *type, int discriminant_index,
9983 int default_index)
9985 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9986 gdb_assert (discriminant_index == -1
9987 || (discriminant_index >= 0
9988 && discriminant_index < TYPE_NFIELDS (type)));
9989 gdb_assert (default_index == -1
9990 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9992 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9994 struct discriminant_info *disc
9995 = ((struct discriminant_info *)
9996 TYPE_ZALLOC (type,
9997 offsetof (struct discriminant_info, discriminants)
9998 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9999 disc->default_index = default_index;
10000 disc->discriminant_index = discriminant_index;
10002 struct dynamic_prop prop;
10003 prop.kind = PROP_UNDEFINED;
10004 prop.data.baton = disc;
10006 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10008 return disc;
10011 /* Some versions of rustc emitted enums in an unusual way.
10013 Ordinary enums were emitted as unions. The first element of each
10014 structure in the union was named "RUST$ENUM$DISR". This element
10015 held the discriminant.
10017 These versions of Rust also implemented the "non-zero"
10018 optimization. When the enum had two values, and one is empty and
10019 the other holds a pointer that cannot be zero, the pointer is used
10020 as the discriminant, with a zero value meaning the empty variant.
10021 Here, the union's first member is of the form
10022 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10023 where the fieldnos are the indices of the fields that should be
10024 traversed in order to find the field (which may be several fields deep)
10025 and the variantname is the name of the variant of the case when the
10026 field is zero.
10028 This function recognizes whether TYPE is of one of these forms,
10029 and, if so, smashes it to be a variant type. */
10031 static void
10032 quirk_rust_enum (struct type *type, struct objfile *objfile)
10034 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10036 /* We don't need to deal with empty enums. */
10037 if (TYPE_NFIELDS (type) == 0)
10038 return;
10040 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10041 if (TYPE_NFIELDS (type) == 1
10042 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10044 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10046 /* Decode the field name to find the offset of the
10047 discriminant. */
10048 ULONGEST bit_offset = 0;
10049 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10050 while (name[0] >= '0' && name[0] <= '9')
10052 char *tail;
10053 unsigned long index = strtoul (name, &tail, 10);
10054 name = tail;
10055 if (*name != '$'
10056 || index >= TYPE_NFIELDS (field_type)
10057 || (TYPE_FIELD_LOC_KIND (field_type, index)
10058 != FIELD_LOC_KIND_BITPOS))
10060 complaint (_("Could not parse Rust enum encoding string \"%s\""
10061 "[in module %s]"),
10062 TYPE_FIELD_NAME (type, 0),
10063 objfile_name (objfile));
10064 return;
10066 ++name;
10068 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10069 field_type = TYPE_FIELD_TYPE (field_type, index);
10072 /* Make a union to hold the variants. */
10073 struct type *union_type = alloc_type (objfile);
10074 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10075 TYPE_NFIELDS (union_type) = 3;
10076 TYPE_FIELDS (union_type)
10077 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10078 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10079 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10081 /* Put the discriminant must at index 0. */
10082 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10083 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10084 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10085 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10087 /* The order of fields doesn't really matter, so put the real
10088 field at index 1 and the data-less field at index 2. */
10089 struct discriminant_info *disc
10090 = alloc_discriminant_info (union_type, 0, 1);
10091 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10092 TYPE_FIELD_NAME (union_type, 1)
10093 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10094 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10095 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10096 TYPE_FIELD_NAME (union_type, 1));
10098 const char *dataless_name
10099 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10100 name);
10101 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10102 dataless_name);
10103 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10104 /* NAME points into the original discriminant name, which
10105 already has the correct lifetime. */
10106 TYPE_FIELD_NAME (union_type, 2) = name;
10107 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10108 disc->discriminants[2] = 0;
10110 /* Smash this type to be a structure type. We have to do this
10111 because the type has already been recorded. */
10112 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10113 TYPE_NFIELDS (type) = 1;
10114 TYPE_FIELDS (type)
10115 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10117 /* Install the variant part. */
10118 TYPE_FIELD_TYPE (type, 0) = union_type;
10119 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10120 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10122 /* A union with a single anonymous field is probably an old-style
10123 univariant enum. */
10124 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10126 /* Smash this type to be a structure type. We have to do this
10127 because the type has already been recorded. */
10128 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130 /* Make a union to hold the variants. */
10131 struct type *union_type = alloc_type (objfile);
10132 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10133 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10134 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10135 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10136 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10138 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10139 const char *variant_name
10140 = rust_last_path_segment (TYPE_NAME (field_type));
10141 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10142 TYPE_NAME (field_type)
10143 = rust_fully_qualify (&objfile->objfile_obstack,
10144 TYPE_NAME (type), variant_name);
10146 /* Install the union in the outer struct type. */
10147 TYPE_NFIELDS (type) = 1;
10148 TYPE_FIELDS (type)
10149 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10150 TYPE_FIELD_TYPE (type, 0) = union_type;
10151 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10152 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10154 alloc_discriminant_info (union_type, -1, 0);
10156 else
10158 struct type *disr_type = nullptr;
10159 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10161 disr_type = TYPE_FIELD_TYPE (type, i);
10163 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10165 /* All fields of a true enum will be structs. */
10166 return;
10168 else if (TYPE_NFIELDS (disr_type) == 0)
10170 /* Could be data-less variant, so keep going. */
10171 disr_type = nullptr;
10173 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10174 "RUST$ENUM$DISR") != 0)
10176 /* Not a Rust enum. */
10177 return;
10179 else
10181 /* Found one. */
10182 break;
10186 /* If we got here without a discriminant, then it's probably
10187 just a union. */
10188 if (disr_type == nullptr)
10189 return;
10191 /* Smash this type to be a structure type. We have to do this
10192 because the type has already been recorded. */
10193 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10195 /* Make a union to hold the variants. */
10196 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10197 struct type *union_type = alloc_type (objfile);
10198 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10199 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10200 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10201 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10202 TYPE_FIELDS (union_type)
10203 = (struct field *) TYPE_ZALLOC (union_type,
10204 (TYPE_NFIELDS (union_type)
10205 * sizeof (struct field)));
10207 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10208 TYPE_NFIELDS (type) * sizeof (struct field));
10210 /* Install the discriminant at index 0 in the union. */
10211 TYPE_FIELD (union_type, 0) = *disr_field;
10212 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10213 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10215 /* Install the union in the outer struct type. */
10216 TYPE_FIELD_TYPE (type, 0) = union_type;
10217 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10218 TYPE_NFIELDS (type) = 1;
10220 /* Set the size and offset of the union type. */
10221 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10223 /* We need a way to find the correct discriminant given a
10224 variant name. For convenience we build a map here. */
10225 struct type *enum_type = FIELD_TYPE (*disr_field);
10226 std::unordered_map<std::string, ULONGEST> discriminant_map;
10227 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10229 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10231 const char *name
10232 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10233 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10237 int n_fields = TYPE_NFIELDS (union_type);
10238 struct discriminant_info *disc
10239 = alloc_discriminant_info (union_type, 0, -1);
10240 /* Skip the discriminant here. */
10241 for (int i = 1; i < n_fields; ++i)
10243 /* Find the final word in the name of this variant's type.
10244 That name can be used to look up the correct
10245 discriminant. */
10246 const char *variant_name
10247 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10248 i)));
10250 auto iter = discriminant_map.find (variant_name);
10251 if (iter != discriminant_map.end ())
10252 disc->discriminants[i] = iter->second;
10254 /* Remove the discriminant field, if it exists. */
10255 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10256 if (TYPE_NFIELDS (sub_type) > 0)
10258 --TYPE_NFIELDS (sub_type);
10259 ++TYPE_FIELDS (sub_type);
10261 TYPE_FIELD_NAME (union_type, i) = variant_name;
10262 TYPE_NAME (sub_type)
10263 = rust_fully_qualify (&objfile->objfile_obstack,
10264 TYPE_NAME (type), variant_name);
10269 /* Rewrite some Rust unions to be structures with variants parts. */
10271 static void
10272 rust_union_quirks (struct dwarf2_cu *cu)
10274 gdb_assert (cu->language == language_rust);
10275 for (type *type_ : cu->rust_unions)
10276 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10277 /* We don't need this any more. */
10278 cu->rust_unions.clear ();
10281 /* Return the symtab for PER_CU. This works properly regardless of
10282 whether we're using the index or psymtabs. */
10284 static struct compunit_symtab *
10285 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10287 return (per_cu->dwarf2_per_objfile->using_index
10288 ? per_cu->v.quick->compunit_symtab
10289 : per_cu->v.psymtab->compunit_symtab);
10292 /* A helper function for computing the list of all symbol tables
10293 included by PER_CU. */
10295 static void
10296 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10297 htab_t all_children, htab_t all_type_symtabs,
10298 struct dwarf2_per_cu_data *per_cu,
10299 struct compunit_symtab *immediate_parent)
10301 void **slot;
10302 struct compunit_symtab *cust;
10304 slot = htab_find_slot (all_children, per_cu, INSERT);
10305 if (*slot != NULL)
10307 /* This inclusion and its children have been processed. */
10308 return;
10311 *slot = per_cu;
10312 /* Only add a CU if it has a symbol table. */
10313 cust = get_compunit_symtab (per_cu);
10314 if (cust != NULL)
10316 /* If this is a type unit only add its symbol table if we haven't
10317 seen it yet (type unit per_cu's can share symtabs). */
10318 if (per_cu->is_debug_types)
10320 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10321 if (*slot == NULL)
10323 *slot = cust;
10324 result->push_back (cust);
10325 if (cust->user == NULL)
10326 cust->user = immediate_parent;
10329 else
10331 result->push_back (cust);
10332 if (cust->user == NULL)
10333 cust->user = immediate_parent;
10337 if (!per_cu->imported_symtabs_empty ())
10338 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10340 recursively_compute_inclusions (result, all_children,
10341 all_type_symtabs, ptr, cust);
10345 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10346 PER_CU. */
10348 static void
10349 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10351 gdb_assert (! per_cu->is_debug_types);
10353 if (!per_cu->imported_symtabs_empty ())
10355 int len;
10356 std::vector<compunit_symtab *> result_symtabs;
10357 htab_t all_children, all_type_symtabs;
10358 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10360 /* If we don't have a symtab, we can just skip this case. */
10361 if (cust == NULL)
10362 return;
10364 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10365 NULL, xcalloc, xfree);
10366 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10367 NULL, xcalloc, xfree);
10369 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10371 recursively_compute_inclusions (&result_symtabs, all_children,
10372 all_type_symtabs, ptr, cust);
10375 /* Now we have a transitive closure of all the included symtabs. */
10376 len = result_symtabs.size ();
10377 cust->includes
10378 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10379 struct compunit_symtab *, len + 1);
10380 memcpy (cust->includes, result_symtabs.data (),
10381 len * sizeof (compunit_symtab *));
10382 cust->includes[len] = NULL;
10384 htab_delete (all_children);
10385 htab_delete (all_type_symtabs);
10389 /* Compute the 'includes' field for the symtabs of all the CUs we just
10390 read. */
10392 static void
10393 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10395 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10397 if (! iter->is_debug_types)
10398 compute_compunit_symtab_includes (iter);
10401 dwarf2_per_objfile->just_read_cus.clear ();
10404 /* Generate full symbol information for PER_CU, whose DIEs have
10405 already been loaded into memory. */
10407 static void
10408 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10409 enum language pretend_language)
10411 struct dwarf2_cu *cu = per_cu->cu;
10412 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10413 struct objfile *objfile = dwarf2_per_objfile->objfile;
10414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10415 CORE_ADDR lowpc, highpc;
10416 struct compunit_symtab *cust;
10417 CORE_ADDR baseaddr;
10418 struct block *static_block;
10419 CORE_ADDR addr;
10421 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10423 /* Clear the list here in case something was left over. */
10424 cu->method_list.clear ();
10426 cu->language = pretend_language;
10427 cu->language_defn = language_def (cu->language);
10429 /* Do line number decoding in read_file_scope () */
10430 process_die (cu->dies, cu);
10432 /* For now fudge the Go package. */
10433 if (cu->language == language_go)
10434 fixup_go_packaging (cu);
10436 /* Now that we have processed all the DIEs in the CU, all the types
10437 should be complete, and it should now be safe to compute all of the
10438 physnames. */
10439 compute_delayed_physnames (cu);
10441 if (cu->language == language_rust)
10442 rust_union_quirks (cu);
10444 /* Some compilers don't define a DW_AT_high_pc attribute for the
10445 compilation unit. If the DW_AT_high_pc is missing, synthesize
10446 it, by scanning the DIE's below the compilation unit. */
10447 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10449 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10450 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10452 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10453 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10454 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10455 addrmap to help ensure it has an accurate map of pc values belonging to
10456 this comp unit. */
10457 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10459 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10460 SECT_OFF_TEXT (objfile),
10463 if (cust != NULL)
10465 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10467 /* Set symtab language to language from DW_AT_language. If the
10468 compilation is from a C file generated by language preprocessors, do
10469 not set the language if it was already deduced by start_subfile. */
10470 if (!(cu->language == language_c
10471 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10472 COMPUNIT_FILETABS (cust)->language = cu->language;
10474 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10475 produce DW_AT_location with location lists but it can be possibly
10476 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10477 there were bugs in prologue debug info, fixed later in GCC-4.5
10478 by "unwind info for epilogues" patch (which is not directly related).
10480 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10481 needed, it would be wrong due to missing DW_AT_producer there.
10483 Still one can confuse GDB by using non-standard GCC compilation
10484 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10486 if (cu->has_loclist && gcc_4_minor >= 5)
10487 cust->locations_valid = 1;
10489 if (gcc_4_minor >= 5)
10490 cust->epilogue_unwind_valid = 1;
10492 cust->call_site_htab = cu->call_site_htab;
10495 if (dwarf2_per_objfile->using_index)
10496 per_cu->v.quick->compunit_symtab = cust;
10497 else
10499 struct partial_symtab *pst = per_cu->v.psymtab;
10500 pst->compunit_symtab = cust;
10501 pst->readin = 1;
10504 /* Push it for inclusion processing later. */
10505 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10507 /* Not needed any more. */
10508 cu->reset_builder ();
10511 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10512 already been loaded into memory. */
10514 static void
10515 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10516 enum language pretend_language)
10518 struct dwarf2_cu *cu = per_cu->cu;
10519 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10520 struct objfile *objfile = dwarf2_per_objfile->objfile;
10521 struct compunit_symtab *cust;
10522 struct signatured_type *sig_type;
10524 gdb_assert (per_cu->is_debug_types);
10525 sig_type = (struct signatured_type *) per_cu;
10527 /* Clear the list here in case something was left over. */
10528 cu->method_list.clear ();
10530 cu->language = pretend_language;
10531 cu->language_defn = language_def (cu->language);
10533 /* The symbol tables are set up in read_type_unit_scope. */
10534 process_die (cu->dies, cu);
10536 /* For now fudge the Go package. */
10537 if (cu->language == language_go)
10538 fixup_go_packaging (cu);
10540 /* Now that we have processed all the DIEs in the CU, all the types
10541 should be complete, and it should now be safe to compute all of the
10542 physnames. */
10543 compute_delayed_physnames (cu);
10545 if (cu->language == language_rust)
10546 rust_union_quirks (cu);
10548 /* TUs share symbol tables.
10549 If this is the first TU to use this symtab, complete the construction
10550 of it with end_expandable_symtab. Otherwise, complete the addition of
10551 this TU's symbols to the existing symtab. */
10552 if (sig_type->type_unit_group->compunit_symtab == NULL)
10554 buildsym_compunit *builder = cu->get_builder ();
10555 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10556 sig_type->type_unit_group->compunit_symtab = cust;
10558 if (cust != NULL)
10560 /* Set symtab language to language from DW_AT_language. If the
10561 compilation is from a C file generated by language preprocessors,
10562 do not set the language if it was already deduced by
10563 start_subfile. */
10564 if (!(cu->language == language_c
10565 && COMPUNIT_FILETABS (cust)->language != language_c))
10566 COMPUNIT_FILETABS (cust)->language = cu->language;
10569 else
10571 cu->get_builder ()->augment_type_symtab ();
10572 cust = sig_type->type_unit_group->compunit_symtab;
10575 if (dwarf2_per_objfile->using_index)
10576 per_cu->v.quick->compunit_symtab = cust;
10577 else
10579 struct partial_symtab *pst = per_cu->v.psymtab;
10580 pst->compunit_symtab = cust;
10581 pst->readin = 1;
10584 /* Not needed any more. */
10585 cu->reset_builder ();
10588 /* Process an imported unit DIE. */
10590 static void
10591 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10593 struct attribute *attr;
10595 /* For now we don't handle imported units in type units. */
10596 if (cu->per_cu->is_debug_types)
10598 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10599 " supported in type units [in module %s]"),
10600 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10603 attr = dwarf2_attr (die, DW_AT_import, cu);
10604 if (attr != NULL)
10606 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10607 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10608 dwarf2_per_cu_data *per_cu
10609 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10610 cu->per_cu->dwarf2_per_objfile);
10612 /* If necessary, add it to the queue and load its DIEs. */
10613 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10614 load_full_comp_unit (per_cu, false, cu->language);
10616 cu->per_cu->imported_symtabs_push (per_cu);
10620 /* RAII object that represents a process_die scope: i.e.,
10621 starts/finishes processing a DIE. */
10622 class process_die_scope
10624 public:
10625 process_die_scope (die_info *die, dwarf2_cu *cu)
10626 : m_die (die), m_cu (cu)
10628 /* We should only be processing DIEs not already in process. */
10629 gdb_assert (!m_die->in_process);
10630 m_die->in_process = true;
10633 ~process_die_scope ()
10635 m_die->in_process = false;
10637 /* If we're done processing the DIE for the CU that owns the line
10638 header, we don't need the line header anymore. */
10639 if (m_cu->line_header_die_owner == m_die)
10641 delete m_cu->line_header;
10642 m_cu->line_header = NULL;
10643 m_cu->line_header_die_owner = NULL;
10647 private:
10648 die_info *m_die;
10649 dwarf2_cu *m_cu;
10652 /* Process a die and its children. */
10654 static void
10655 process_die (struct die_info *die, struct dwarf2_cu *cu)
10657 process_die_scope scope (die, cu);
10659 switch (die->tag)
10661 case DW_TAG_padding:
10662 break;
10663 case DW_TAG_compile_unit:
10664 case DW_TAG_partial_unit:
10665 read_file_scope (die, cu);
10666 break;
10667 case DW_TAG_type_unit:
10668 read_type_unit_scope (die, cu);
10669 break;
10670 case DW_TAG_subprogram:
10671 /* Nested subprograms in Fortran get a prefix. */
10672 if (cu->language == language_fortran
10673 && die->parent != NULL
10674 && die->parent->tag == DW_TAG_subprogram)
10675 cu->processing_has_namespace_info = true;
10676 /* Fall through. */
10677 case DW_TAG_inlined_subroutine:
10678 read_func_scope (die, cu);
10679 break;
10680 case DW_TAG_lexical_block:
10681 case DW_TAG_try_block:
10682 case DW_TAG_catch_block:
10683 read_lexical_block_scope (die, cu);
10684 break;
10685 case DW_TAG_call_site:
10686 case DW_TAG_GNU_call_site:
10687 read_call_site_scope (die, cu);
10688 break;
10689 case DW_TAG_class_type:
10690 case DW_TAG_interface_type:
10691 case DW_TAG_structure_type:
10692 case DW_TAG_union_type:
10693 process_structure_scope (die, cu);
10694 break;
10695 case DW_TAG_enumeration_type:
10696 process_enumeration_scope (die, cu);
10697 break;
10699 /* These dies have a type, but processing them does not create
10700 a symbol or recurse to process the children. Therefore we can
10701 read them on-demand through read_type_die. */
10702 case DW_TAG_subroutine_type:
10703 case DW_TAG_set_type:
10704 case DW_TAG_array_type:
10705 case DW_TAG_pointer_type:
10706 case DW_TAG_ptr_to_member_type:
10707 case DW_TAG_reference_type:
10708 case DW_TAG_rvalue_reference_type:
10709 case DW_TAG_string_type:
10710 break;
10712 case DW_TAG_base_type:
10713 case DW_TAG_subrange_type:
10714 case DW_TAG_typedef:
10715 /* Add a typedef symbol for the type definition, if it has a
10716 DW_AT_name. */
10717 new_symbol (die, read_type_die (die, cu), cu);
10718 break;
10719 case DW_TAG_common_block:
10720 read_common_block (die, cu);
10721 break;
10722 case DW_TAG_common_inclusion:
10723 break;
10724 case DW_TAG_namespace:
10725 cu->processing_has_namespace_info = true;
10726 read_namespace (die, cu);
10727 break;
10728 case DW_TAG_module:
10729 cu->processing_has_namespace_info = true;
10730 read_module (die, cu);
10731 break;
10732 case DW_TAG_imported_declaration:
10733 cu->processing_has_namespace_info = true;
10734 if (read_namespace_alias (die, cu))
10735 break;
10736 /* The declaration is not a global namespace alias. */
10737 /* Fall through. */
10738 case DW_TAG_imported_module:
10739 cu->processing_has_namespace_info = true;
10740 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10741 || cu->language != language_fortran))
10742 complaint (_("Tag '%s' has unexpected children"),
10743 dwarf_tag_name (die->tag));
10744 read_import_statement (die, cu);
10745 break;
10747 case DW_TAG_imported_unit:
10748 process_imported_unit_die (die, cu);
10749 break;
10751 case DW_TAG_variable:
10752 read_variable (die, cu);
10753 break;
10755 default:
10756 new_symbol (die, NULL, cu);
10757 break;
10761 /* DWARF name computation. */
10763 /* A helper function for dwarf2_compute_name which determines whether DIE
10764 needs to have the name of the scope prepended to the name listed in the
10765 die. */
10767 static int
10768 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10770 struct attribute *attr;
10772 switch (die->tag)
10774 case DW_TAG_namespace:
10775 case DW_TAG_typedef:
10776 case DW_TAG_class_type:
10777 case DW_TAG_interface_type:
10778 case DW_TAG_structure_type:
10779 case DW_TAG_union_type:
10780 case DW_TAG_enumeration_type:
10781 case DW_TAG_enumerator:
10782 case DW_TAG_subprogram:
10783 case DW_TAG_inlined_subroutine:
10784 case DW_TAG_member:
10785 case DW_TAG_imported_declaration:
10786 return 1;
10788 case DW_TAG_variable:
10789 case DW_TAG_constant:
10790 /* We only need to prefix "globally" visible variables. These include
10791 any variable marked with DW_AT_external or any variable that
10792 lives in a namespace. [Variables in anonymous namespaces
10793 require prefixing, but they are not DW_AT_external.] */
10795 if (dwarf2_attr (die, DW_AT_specification, cu))
10797 struct dwarf2_cu *spec_cu = cu;
10799 return die_needs_namespace (die_specification (die, &spec_cu),
10800 spec_cu);
10803 attr = dwarf2_attr (die, DW_AT_external, cu);
10804 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10805 && die->parent->tag != DW_TAG_module)
10806 return 0;
10807 /* A variable in a lexical block of some kind does not need a
10808 namespace, even though in C++ such variables may be external
10809 and have a mangled name. */
10810 if (die->parent->tag == DW_TAG_lexical_block
10811 || die->parent->tag == DW_TAG_try_block
10812 || die->parent->tag == DW_TAG_catch_block
10813 || die->parent->tag == DW_TAG_subprogram)
10814 return 0;
10815 return 1;
10817 default:
10818 return 0;
10822 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10823 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10824 defined for the given DIE. */
10826 static struct attribute *
10827 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10829 struct attribute *attr;
10831 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10832 if (attr == NULL)
10833 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10835 return attr;
10838 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10839 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10840 defined for the given DIE. */
10842 static const char *
10843 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10845 const char *linkage_name;
10847 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10848 if (linkage_name == NULL)
10849 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10851 return linkage_name;
10854 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10855 compute the physname for the object, which include a method's:
10856 - formal parameters (C++),
10857 - receiver type (Go),
10859 The term "physname" is a bit confusing.
10860 For C++, for example, it is the demangled name.
10861 For Go, for example, it's the mangled name.
10863 For Ada, return the DIE's linkage name rather than the fully qualified
10864 name. PHYSNAME is ignored..
10866 The result is allocated on the objfile_obstack and canonicalized. */
10868 static const char *
10869 dwarf2_compute_name (const char *name,
10870 struct die_info *die, struct dwarf2_cu *cu,
10871 int physname)
10873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10875 if (name == NULL)
10876 name = dwarf2_name (die, cu);
10878 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10879 but otherwise compute it by typename_concat inside GDB.
10880 FIXME: Actually this is not really true, or at least not always true.
10881 It's all very confusing. compute_and_set_names doesn't try to demangle
10882 Fortran names because there is no mangling standard. So new_symbol
10883 will set the demangled name to the result of dwarf2_full_name, and it is
10884 the demangled name that GDB uses if it exists. */
10885 if (cu->language == language_ada
10886 || (cu->language == language_fortran && physname))
10888 /* For Ada unit, we prefer the linkage name over the name, as
10889 the former contains the exported name, which the user expects
10890 to be able to reference. Ideally, we want the user to be able
10891 to reference this entity using either natural or linkage name,
10892 but we haven't started looking at this enhancement yet. */
10893 const char *linkage_name = dw2_linkage_name (die, cu);
10895 if (linkage_name != NULL)
10896 return linkage_name;
10899 /* These are the only languages we know how to qualify names in. */
10900 if (name != NULL
10901 && (cu->language == language_cplus
10902 || cu->language == language_fortran || cu->language == language_d
10903 || cu->language == language_rust))
10905 if (die_needs_namespace (die, cu))
10907 const char *prefix;
10908 const char *canonical_name = NULL;
10910 string_file buf;
10912 prefix = determine_prefix (die, cu);
10913 if (*prefix != '\0')
10915 char *prefixed_name = typename_concat (NULL, prefix, name,
10916 physname, cu);
10918 buf.puts (prefixed_name);
10919 xfree (prefixed_name);
10921 else
10922 buf.puts (name);
10924 /* Template parameters may be specified in the DIE's DW_AT_name, or
10925 as children with DW_TAG_template_type_param or
10926 DW_TAG_value_type_param. If the latter, add them to the name
10927 here. If the name already has template parameters, then
10928 skip this step; some versions of GCC emit both, and
10929 it is more efficient to use the pre-computed name.
10931 Something to keep in mind about this process: it is very
10932 unlikely, or in some cases downright impossible, to produce
10933 something that will match the mangled name of a function.
10934 If the definition of the function has the same debug info,
10935 we should be able to match up with it anyway. But fallbacks
10936 using the minimal symbol, for instance to find a method
10937 implemented in a stripped copy of libstdc++, will not work.
10938 If we do not have debug info for the definition, we will have to
10939 match them up some other way.
10941 When we do name matching there is a related problem with function
10942 templates; two instantiated function templates are allowed to
10943 differ only by their return types, which we do not add here. */
10945 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10947 struct attribute *attr;
10948 struct die_info *child;
10949 int first = 1;
10951 die->building_fullname = 1;
10953 for (child = die->child; child != NULL; child = child->sibling)
10955 struct type *type;
10956 LONGEST value;
10957 const gdb_byte *bytes;
10958 struct dwarf2_locexpr_baton *baton;
10959 struct value *v;
10961 if (child->tag != DW_TAG_template_type_param
10962 && child->tag != DW_TAG_template_value_param)
10963 continue;
10965 if (first)
10967 buf.puts ("<");
10968 first = 0;
10970 else
10971 buf.puts (", ");
10973 attr = dwarf2_attr (child, DW_AT_type, cu);
10974 if (attr == NULL)
10976 complaint (_("template parameter missing DW_AT_type"));
10977 buf.puts ("UNKNOWN_TYPE");
10978 continue;
10980 type = die_type (child, cu);
10982 if (child->tag == DW_TAG_template_type_param)
10984 c_print_type (type, "", &buf, -1, 0, cu->language,
10985 &type_print_raw_options);
10986 continue;
10989 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10990 if (attr == NULL)
10992 complaint (_("template parameter missing "
10993 "DW_AT_const_value"));
10994 buf.puts ("UNKNOWN_VALUE");
10995 continue;
10998 dwarf2_const_value_attr (attr, type, name,
10999 &cu->comp_unit_obstack, cu,
11000 &value, &bytes, &baton);
11002 if (TYPE_NOSIGN (type))
11003 /* GDB prints characters as NUMBER 'CHAR'. If that's
11004 changed, this can use value_print instead. */
11005 c_printchar (value, type, &buf);
11006 else
11008 struct value_print_options opts;
11010 if (baton != NULL)
11011 v = dwarf2_evaluate_loc_desc (type, NULL,
11012 baton->data,
11013 baton->size,
11014 baton->per_cu);
11015 else if (bytes != NULL)
11017 v = allocate_value (type);
11018 memcpy (value_contents_writeable (v), bytes,
11019 TYPE_LENGTH (type));
11021 else
11022 v = value_from_longest (type, value);
11024 /* Specify decimal so that we do not depend on
11025 the radix. */
11026 get_formatted_print_options (&opts, 'd');
11027 opts.raw = 1;
11028 value_print (v, &buf, &opts);
11029 release_value (v);
11033 die->building_fullname = 0;
11035 if (!first)
11037 /* Close the argument list, with a space if necessary
11038 (nested templates). */
11039 if (!buf.empty () && buf.string ().back () == '>')
11040 buf.puts (" >");
11041 else
11042 buf.puts (">");
11046 /* For C++ methods, append formal parameter type
11047 information, if PHYSNAME. */
11049 if (physname && die->tag == DW_TAG_subprogram
11050 && cu->language == language_cplus)
11052 struct type *type = read_type_die (die, cu);
11054 c_type_print_args (type, &buf, 1, cu->language,
11055 &type_print_raw_options);
11057 if (cu->language == language_cplus)
11059 /* Assume that an artificial first parameter is
11060 "this", but do not crash if it is not. RealView
11061 marks unnamed (and thus unused) parameters as
11062 artificial; there is no way to differentiate
11063 the two cases. */
11064 if (TYPE_NFIELDS (type) > 0
11065 && TYPE_FIELD_ARTIFICIAL (type, 0)
11066 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11067 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11068 0))))
11069 buf.puts (" const");
11073 const std::string &intermediate_name = buf.string ();
11075 if (cu->language == language_cplus)
11076 canonical_name
11077 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11078 &objfile->per_bfd->storage_obstack);
11080 /* If we only computed INTERMEDIATE_NAME, or if
11081 INTERMEDIATE_NAME is already canonical, then we need to
11082 copy it to the appropriate obstack. */
11083 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11084 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11085 intermediate_name);
11086 else
11087 name = canonical_name;
11091 return name;
11094 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11095 If scope qualifiers are appropriate they will be added. The result
11096 will be allocated on the storage_obstack, or NULL if the DIE does
11097 not have a name. NAME may either be from a previous call to
11098 dwarf2_name or NULL.
11100 The output string will be canonicalized (if C++). */
11102 static const char *
11103 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11105 return dwarf2_compute_name (name, die, cu, 0);
11108 /* Construct a physname for the given DIE in CU. NAME may either be
11109 from a previous call to dwarf2_name or NULL. The result will be
11110 allocated on the objfile_objstack or NULL if the DIE does not have a
11111 name.
11113 The output string will be canonicalized (if C++). */
11115 static const char *
11116 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11119 const char *retval, *mangled = NULL, *canon = NULL;
11120 int need_copy = 1;
11122 /* In this case dwarf2_compute_name is just a shortcut not building anything
11123 on its own. */
11124 if (!die_needs_namespace (die, cu))
11125 return dwarf2_compute_name (name, die, cu, 1);
11127 mangled = dw2_linkage_name (die, cu);
11129 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11130 See https://github.com/rust-lang/rust/issues/32925. */
11131 if (cu->language == language_rust && mangled != NULL
11132 && strchr (mangled, '{') != NULL)
11133 mangled = NULL;
11135 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11136 has computed. */
11137 gdb::unique_xmalloc_ptr<char> demangled;
11138 if (mangled != NULL)
11141 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11143 /* Do nothing (do not demangle the symbol name). */
11145 else if (cu->language == language_go)
11147 /* This is a lie, but we already lie to the caller new_symbol.
11148 new_symbol assumes we return the mangled name.
11149 This just undoes that lie until things are cleaned up. */
11151 else
11153 /* Use DMGL_RET_DROP for C++ template functions to suppress
11154 their return type. It is easier for GDB users to search
11155 for such functions as `name(params)' than `long name(params)'.
11156 In such case the minimal symbol names do not match the full
11157 symbol names but for template functions there is never a need
11158 to look up their definition from their declaration so
11159 the only disadvantage remains the minimal symbol variant
11160 `long name(params)' does not have the proper inferior type. */
11161 demangled.reset (gdb_demangle (mangled,
11162 (DMGL_PARAMS | DMGL_ANSI
11163 | DMGL_RET_DROP)));
11165 if (demangled)
11166 canon = demangled.get ();
11167 else
11169 canon = mangled;
11170 need_copy = 0;
11174 if (canon == NULL || check_physname)
11176 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11178 if (canon != NULL && strcmp (physname, canon) != 0)
11180 /* It may not mean a bug in GDB. The compiler could also
11181 compute DW_AT_linkage_name incorrectly. But in such case
11182 GDB would need to be bug-to-bug compatible. */
11184 complaint (_("Computed physname <%s> does not match demangled <%s> "
11185 "(from linkage <%s>) - DIE at %s [in module %s]"),
11186 physname, canon, mangled, sect_offset_str (die->sect_off),
11187 objfile_name (objfile));
11189 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11190 is available here - over computed PHYSNAME. It is safer
11191 against both buggy GDB and buggy compilers. */
11193 retval = canon;
11195 else
11197 retval = physname;
11198 need_copy = 0;
11201 else
11202 retval = canon;
11204 if (need_copy)
11205 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11207 return retval;
11210 /* Inspect DIE in CU for a namespace alias. If one exists, record
11211 a new symbol for it.
11213 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11215 static int
11216 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11218 struct attribute *attr;
11220 /* If the die does not have a name, this is not a namespace
11221 alias. */
11222 attr = dwarf2_attr (die, DW_AT_name, cu);
11223 if (attr != NULL)
11225 int num;
11226 struct die_info *d = die;
11227 struct dwarf2_cu *imported_cu = cu;
11229 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11230 keep inspecting DIEs until we hit the underlying import. */
11231 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11232 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11234 attr = dwarf2_attr (d, DW_AT_import, cu);
11235 if (attr == NULL)
11236 break;
11238 d = follow_die_ref (d, attr, &imported_cu);
11239 if (d->tag != DW_TAG_imported_declaration)
11240 break;
11243 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11245 complaint (_("DIE at %s has too many recursively imported "
11246 "declarations"), sect_offset_str (d->sect_off));
11247 return 0;
11250 if (attr != NULL)
11252 struct type *type;
11253 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11255 type = get_die_type_at_offset (sect_off, cu->per_cu);
11256 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11258 /* This declaration is a global namespace alias. Add
11259 a symbol for it whose type is the aliased namespace. */
11260 new_symbol (die, type, cu);
11261 return 1;
11266 return 0;
11269 /* Return the using directives repository (global or local?) to use in the
11270 current context for CU.
11272 For Ada, imported declarations can materialize renamings, which *may* be
11273 global. However it is impossible (for now?) in DWARF to distinguish
11274 "external" imported declarations and "static" ones. As all imported
11275 declarations seem to be static in all other languages, make them all CU-wide
11276 global only in Ada. */
11278 static struct using_direct **
11279 using_directives (struct dwarf2_cu *cu)
11281 if (cu->language == language_ada
11282 && cu->get_builder ()->outermost_context_p ())
11283 return cu->get_builder ()->get_global_using_directives ();
11284 else
11285 return cu->get_builder ()->get_local_using_directives ();
11288 /* Read the import statement specified by the given die and record it. */
11290 static void
11291 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11294 struct attribute *import_attr;
11295 struct die_info *imported_die, *child_die;
11296 struct dwarf2_cu *imported_cu;
11297 const char *imported_name;
11298 const char *imported_name_prefix;
11299 const char *canonical_name;
11300 const char *import_alias;
11301 const char *imported_declaration = NULL;
11302 const char *import_prefix;
11303 std::vector<const char *> excludes;
11305 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11306 if (import_attr == NULL)
11308 complaint (_("Tag '%s' has no DW_AT_import"),
11309 dwarf_tag_name (die->tag));
11310 return;
11313 imported_cu = cu;
11314 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11315 imported_name = dwarf2_name (imported_die, imported_cu);
11316 if (imported_name == NULL)
11318 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11320 The import in the following code:
11321 namespace A
11323 typedef int B;
11326 int main ()
11328 using A::B;
11329 B b;
11330 return b;
11334 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11335 <52> DW_AT_decl_file : 1
11336 <53> DW_AT_decl_line : 6
11337 <54> DW_AT_import : <0x75>
11338 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11339 <59> DW_AT_name : B
11340 <5b> DW_AT_decl_file : 1
11341 <5c> DW_AT_decl_line : 2
11342 <5d> DW_AT_type : <0x6e>
11344 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11345 <76> DW_AT_byte_size : 4
11346 <77> DW_AT_encoding : 5 (signed)
11348 imports the wrong die ( 0x75 instead of 0x58 ).
11349 This case will be ignored until the gcc bug is fixed. */
11350 return;
11353 /* Figure out the local name after import. */
11354 import_alias = dwarf2_name (die, cu);
11356 /* Figure out where the statement is being imported to. */
11357 import_prefix = determine_prefix (die, cu);
11359 /* Figure out what the scope of the imported die is and prepend it
11360 to the name of the imported die. */
11361 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11363 if (imported_die->tag != DW_TAG_namespace
11364 && imported_die->tag != DW_TAG_module)
11366 imported_declaration = imported_name;
11367 canonical_name = imported_name_prefix;
11369 else if (strlen (imported_name_prefix) > 0)
11370 canonical_name = obconcat (&objfile->objfile_obstack,
11371 imported_name_prefix,
11372 (cu->language == language_d ? "." : "::"),
11373 imported_name, (char *) NULL);
11374 else
11375 canonical_name = imported_name;
11377 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11378 for (child_die = die->child; child_die && child_die->tag;
11379 child_die = sibling_die (child_die))
11381 /* DWARF-4: A Fortran use statement with a “rename list” may be
11382 represented by an imported module entry with an import attribute
11383 referring to the module and owned entries corresponding to those
11384 entities that are renamed as part of being imported. */
11386 if (child_die->tag != DW_TAG_imported_declaration)
11388 complaint (_("child DW_TAG_imported_declaration expected "
11389 "- DIE at %s [in module %s]"),
11390 sect_offset_str (child_die->sect_off),
11391 objfile_name (objfile));
11392 continue;
11395 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11396 if (import_attr == NULL)
11398 complaint (_("Tag '%s' has no DW_AT_import"),
11399 dwarf_tag_name (child_die->tag));
11400 continue;
11403 imported_cu = cu;
11404 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11405 &imported_cu);
11406 imported_name = dwarf2_name (imported_die, imported_cu);
11407 if (imported_name == NULL)
11409 complaint (_("child DW_TAG_imported_declaration has unknown "
11410 "imported name - DIE at %s [in module %s]"),
11411 sect_offset_str (child_die->sect_off),
11412 objfile_name (objfile));
11413 continue;
11416 excludes.push_back (imported_name);
11418 process_die (child_die, cu);
11421 add_using_directive (using_directives (cu),
11422 import_prefix,
11423 canonical_name,
11424 import_alias,
11425 imported_declaration,
11426 excludes,
11428 &objfile->objfile_obstack);
11431 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11432 types, but gives them a size of zero. Starting with version 14,
11433 ICC is compatible with GCC. */
11435 static bool
11436 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11438 if (!cu->checked_producer)
11439 check_producer (cu);
11441 return cu->producer_is_icc_lt_14;
11444 /* ICC generates a DW_AT_type for C void functions. This was observed on
11445 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11446 which says that void functions should not have a DW_AT_type. */
11448 static bool
11449 producer_is_icc (struct dwarf2_cu *cu)
11451 if (!cu->checked_producer)
11452 check_producer (cu);
11454 return cu->producer_is_icc;
11457 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11458 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11459 this, it was first present in GCC release 4.3.0. */
11461 static bool
11462 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11464 if (!cu->checked_producer)
11465 check_producer (cu);
11467 return cu->producer_is_gcc_lt_4_3;
11470 static file_and_directory
11471 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11473 file_and_directory res;
11475 /* Find the filename. Do not use dwarf2_name here, since the filename
11476 is not a source language identifier. */
11477 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11478 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11480 if (res.comp_dir == NULL
11481 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11482 && IS_ABSOLUTE_PATH (res.name))
11484 res.comp_dir_storage = ldirname (res.name);
11485 if (!res.comp_dir_storage.empty ())
11486 res.comp_dir = res.comp_dir_storage.c_str ();
11488 if (res.comp_dir != NULL)
11490 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11491 directory, get rid of it. */
11492 const char *cp = strchr (res.comp_dir, ':');
11494 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11495 res.comp_dir = cp + 1;
11498 if (res.name == NULL)
11499 res.name = "<unknown>";
11501 return res;
11504 /* Handle DW_AT_stmt_list for a compilation unit.
11505 DIE is the DW_TAG_compile_unit die for CU.
11506 COMP_DIR is the compilation directory. LOWPC is passed to
11507 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11509 static void
11510 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11511 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11513 struct dwarf2_per_objfile *dwarf2_per_objfile
11514 = cu->per_cu->dwarf2_per_objfile;
11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
11516 struct attribute *attr;
11517 struct line_header line_header_local;
11518 hashval_t line_header_local_hash;
11519 void **slot;
11520 int decode_mapping;
11522 gdb_assert (! cu->per_cu->is_debug_types);
11524 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11525 if (attr == NULL)
11526 return;
11528 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11530 /* The line header hash table is only created if needed (it exists to
11531 prevent redundant reading of the line table for partial_units).
11532 If we're given a partial_unit, we'll need it. If we're given a
11533 compile_unit, then use the line header hash table if it's already
11534 created, but don't create one just yet. */
11536 if (dwarf2_per_objfile->line_header_hash == NULL
11537 && die->tag == DW_TAG_partial_unit)
11539 dwarf2_per_objfile->line_header_hash
11540 = htab_create_alloc_ex (127, line_header_hash_voidp,
11541 line_header_eq_voidp,
11542 free_line_header_voidp,
11543 &objfile->objfile_obstack,
11544 hashtab_obstack_allocate,
11545 dummy_obstack_deallocate);
11548 line_header_local.sect_off = line_offset;
11549 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11550 line_header_local_hash = line_header_hash (&line_header_local);
11551 if (dwarf2_per_objfile->line_header_hash != NULL)
11553 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11554 &line_header_local,
11555 line_header_local_hash, NO_INSERT);
11557 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11558 is not present in *SLOT (since if there is something in *SLOT then
11559 it will be for a partial_unit). */
11560 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11562 gdb_assert (*slot != NULL);
11563 cu->line_header = (struct line_header *) *slot;
11564 return;
11568 /* dwarf_decode_line_header does not yet provide sufficient information.
11569 We always have to call also dwarf_decode_lines for it. */
11570 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11571 if (lh == NULL)
11572 return;
11574 cu->line_header = lh.release ();
11575 cu->line_header_die_owner = die;
11577 if (dwarf2_per_objfile->line_header_hash == NULL)
11578 slot = NULL;
11579 else
11581 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11582 &line_header_local,
11583 line_header_local_hash, INSERT);
11584 gdb_assert (slot != NULL);
11586 if (slot != NULL && *slot == NULL)
11588 /* This newly decoded line number information unit will be owned
11589 by line_header_hash hash table. */
11590 *slot = cu->line_header;
11591 cu->line_header_die_owner = NULL;
11593 else
11595 /* We cannot free any current entry in (*slot) as that struct line_header
11596 may be already used by multiple CUs. Create only temporary decoded
11597 line_header for this CU - it may happen at most once for each line
11598 number information unit. And if we're not using line_header_hash
11599 then this is what we want as well. */
11600 gdb_assert (die->tag != DW_TAG_partial_unit);
11602 decode_mapping = (die->tag != DW_TAG_partial_unit);
11603 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11604 decode_mapping);
11608 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11610 static void
11611 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11613 struct dwarf2_per_objfile *dwarf2_per_objfile
11614 = cu->per_cu->dwarf2_per_objfile;
11615 struct objfile *objfile = dwarf2_per_objfile->objfile;
11616 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11617 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11618 CORE_ADDR highpc = ((CORE_ADDR) 0);
11619 struct attribute *attr;
11620 struct die_info *child_die;
11621 CORE_ADDR baseaddr;
11623 prepare_one_comp_unit (cu, die, cu->language);
11624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11626 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11628 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11629 from finish_block. */
11630 if (lowpc == ((CORE_ADDR) -1))
11631 lowpc = highpc;
11632 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11634 file_and_directory fnd = find_file_and_directory (die, cu);
11636 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11637 standardised yet. As a workaround for the language detection we fall
11638 back to the DW_AT_producer string. */
11639 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11640 cu->language = language_opencl;
11642 /* Similar hack for Go. */
11643 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11644 set_cu_language (DW_LANG_Go, cu);
11646 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11648 /* Decode line number information if present. We do this before
11649 processing child DIEs, so that the line header table is available
11650 for DW_AT_decl_file. */
11651 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11653 /* Process all dies in compilation unit. */
11654 if (die->child != NULL)
11656 child_die = die->child;
11657 while (child_die && child_die->tag)
11659 process_die (child_die, cu);
11660 child_die = sibling_die (child_die);
11664 /* Decode macro information, if present. Dwarf 2 macro information
11665 refers to information in the line number info statement program
11666 header, so we can only read it if we've read the header
11667 successfully. */
11668 attr = dwarf2_attr (die, DW_AT_macros, cu);
11669 if (attr == NULL)
11670 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11671 if (attr && cu->line_header)
11673 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11674 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11676 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11678 else
11680 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11681 if (attr && cu->line_header)
11683 unsigned int macro_offset = DW_UNSND (attr);
11685 dwarf_decode_macros (cu, macro_offset, 0);
11690 void
11691 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11693 struct type_unit_group *tu_group;
11694 int first_time;
11695 struct attribute *attr;
11696 unsigned int i;
11697 struct signatured_type *sig_type;
11699 gdb_assert (per_cu->is_debug_types);
11700 sig_type = (struct signatured_type *) per_cu;
11702 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11704 /* If we're using .gdb_index (includes -readnow) then
11705 per_cu->type_unit_group may not have been set up yet. */
11706 if (sig_type->type_unit_group == NULL)
11707 sig_type->type_unit_group = get_type_unit_group (this, attr);
11708 tu_group = sig_type->type_unit_group;
11710 /* If we've already processed this stmt_list there's no real need to
11711 do it again, we could fake it and just recreate the part we need
11712 (file name,index -> symtab mapping). If data shows this optimization
11713 is useful we can do it then. */
11714 first_time = tu_group->compunit_symtab == NULL;
11716 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11717 debug info. */
11718 line_header_up lh;
11719 if (attr != NULL)
11721 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11722 lh = dwarf_decode_line_header (line_offset, this);
11724 if (lh == NULL)
11726 if (first_time)
11727 start_symtab ("", NULL, 0);
11728 else
11730 gdb_assert (tu_group->symtabs == NULL);
11731 gdb_assert (m_builder == nullptr);
11732 struct compunit_symtab *cust = tu_group->compunit_symtab;
11733 m_builder.reset (new struct buildsym_compunit
11734 (COMPUNIT_OBJFILE (cust), "",
11735 COMPUNIT_DIRNAME (cust),
11736 compunit_language (cust),
11737 0, cust));
11739 return;
11742 line_header = lh.release ();
11743 line_header_die_owner = die;
11745 if (first_time)
11747 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11749 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11750 still initializing it, and our caller (a few levels up)
11751 process_full_type_unit still needs to know if this is the first
11752 time. */
11754 tu_group->num_symtabs = line_header->file_names_size ();
11755 tu_group->symtabs = XNEWVEC (struct symtab *,
11756 line_header->file_names_size ());
11758 auto &file_names = line_header->file_names ();
11759 for (i = 0; i < file_names.size (); ++i)
11761 file_entry &fe = file_names[i];
11762 dwarf2_start_subfile (this, fe.name,
11763 fe.include_dir (line_header));
11764 buildsym_compunit *b = get_builder ();
11765 if (b->get_current_subfile ()->symtab == NULL)
11767 /* NOTE: start_subfile will recognize when it's been
11768 passed a file it has already seen. So we can't
11769 assume there's a simple mapping from
11770 cu->line_header->file_names to subfiles, plus
11771 cu->line_header->file_names may contain dups. */
11772 b->get_current_subfile ()->symtab
11773 = allocate_symtab (cust, b->get_current_subfile ()->name);
11776 fe.symtab = b->get_current_subfile ()->symtab;
11777 tu_group->symtabs[i] = fe.symtab;
11780 else
11782 gdb_assert (m_builder == nullptr);
11783 struct compunit_symtab *cust = tu_group->compunit_symtab;
11784 m_builder.reset (new struct buildsym_compunit
11785 (COMPUNIT_OBJFILE (cust), "",
11786 COMPUNIT_DIRNAME (cust),
11787 compunit_language (cust),
11788 0, cust));
11790 auto &file_names = line_header->file_names ();
11791 for (i = 0; i < file_names.size (); ++i)
11793 file_entry &fe = file_names[i];
11794 fe.symtab = tu_group->symtabs[i];
11798 /* The main symtab is allocated last. Type units don't have DW_AT_name
11799 so they don't have a "real" (so to speak) symtab anyway.
11800 There is later code that will assign the main symtab to all symbols
11801 that don't have one. We need to handle the case of a symbol with a
11802 missing symtab (DW_AT_decl_file) anyway. */
11805 /* Process DW_TAG_type_unit.
11806 For TUs we want to skip the first top level sibling if it's not the
11807 actual type being defined by this TU. In this case the first top
11808 level sibling is there to provide context only. */
11810 static void
11811 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11813 struct die_info *child_die;
11815 prepare_one_comp_unit (cu, die, language_minimal);
11817 /* Initialize (or reinitialize) the machinery for building symtabs.
11818 We do this before processing child DIEs, so that the line header table
11819 is available for DW_AT_decl_file. */
11820 cu->setup_type_unit_groups (die);
11822 if (die->child != NULL)
11824 child_die = die->child;
11825 while (child_die && child_die->tag)
11827 process_die (child_die, cu);
11828 child_die = sibling_die (child_die);
11833 /* DWO/DWP files.
11835 http://gcc.gnu.org/wiki/DebugFission
11836 http://gcc.gnu.org/wiki/DebugFissionDWP
11838 To simplify handling of both DWO files ("object" files with the DWARF info)
11839 and DWP files (a file with the DWOs packaged up into one file), we treat
11840 DWP files as having a collection of virtual DWO files. */
11842 static hashval_t
11843 hash_dwo_file (const void *item)
11845 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11846 hashval_t hash;
11848 hash = htab_hash_string (dwo_file->dwo_name);
11849 if (dwo_file->comp_dir != NULL)
11850 hash += htab_hash_string (dwo_file->comp_dir);
11851 return hash;
11854 static int
11855 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11857 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11858 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11860 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11861 return 0;
11862 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11863 return lhs->comp_dir == rhs->comp_dir;
11864 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11867 /* Allocate a hash table for DWO files. */
11869 static htab_up
11870 allocate_dwo_file_hash_table (struct objfile *objfile)
11872 auto delete_dwo_file = [] (void *item)
11874 struct dwo_file *dwo_file = (struct dwo_file *) item;
11876 delete dwo_file;
11879 return htab_up (htab_create_alloc_ex (41,
11880 hash_dwo_file,
11881 eq_dwo_file,
11882 delete_dwo_file,
11883 &objfile->objfile_obstack,
11884 hashtab_obstack_allocate,
11885 dummy_obstack_deallocate));
11888 /* Lookup DWO file DWO_NAME. */
11890 static void **
11891 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11892 const char *dwo_name,
11893 const char *comp_dir)
11895 struct dwo_file find_entry;
11896 void **slot;
11898 if (dwarf2_per_objfile->dwo_files == NULL)
11899 dwarf2_per_objfile->dwo_files
11900 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11902 find_entry.dwo_name = dwo_name;
11903 find_entry.comp_dir = comp_dir;
11904 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11905 INSERT);
11907 return slot;
11910 static hashval_t
11911 hash_dwo_unit (const void *item)
11913 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11915 /* This drops the top 32 bits of the id, but is ok for a hash. */
11916 return dwo_unit->signature;
11919 static int
11920 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11922 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11923 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11925 /* The signature is assumed to be unique within the DWO file.
11926 So while object file CU dwo_id's always have the value zero,
11927 that's OK, assuming each object file DWO file has only one CU,
11928 and that's the rule for now. */
11929 return lhs->signature == rhs->signature;
11932 /* Allocate a hash table for DWO CUs,TUs.
11933 There is one of these tables for each of CUs,TUs for each DWO file. */
11935 static htab_t
11936 allocate_dwo_unit_table (struct objfile *objfile)
11938 /* Start out with a pretty small number.
11939 Generally DWO files contain only one CU and maybe some TUs. */
11940 return htab_create_alloc_ex (3,
11941 hash_dwo_unit,
11942 eq_dwo_unit,
11943 NULL,
11944 &objfile->objfile_obstack,
11945 hashtab_obstack_allocate,
11946 dummy_obstack_deallocate);
11949 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11951 struct create_dwo_cu_data
11953 struct dwo_file *dwo_file;
11954 struct dwo_unit dwo_unit;
11957 /* die_reader_func for create_dwo_cu. */
11959 static void
11960 create_dwo_cu_reader (const struct die_reader_specs *reader,
11961 const gdb_byte *info_ptr,
11962 struct die_info *comp_unit_die,
11963 int has_children,
11964 void *datap)
11966 struct dwarf2_cu *cu = reader->cu;
11967 sect_offset sect_off = cu->per_cu->sect_off;
11968 struct dwarf2_section_info *section = cu->per_cu->section;
11969 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11970 struct dwo_file *dwo_file = data->dwo_file;
11971 struct dwo_unit *dwo_unit = &data->dwo_unit;
11973 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11974 if (!signature.has_value ())
11976 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11977 " its dwo_id [in module %s]"),
11978 sect_offset_str (sect_off), dwo_file->dwo_name);
11979 return;
11982 dwo_unit->dwo_file = dwo_file;
11983 dwo_unit->signature = *signature;
11984 dwo_unit->section = section;
11985 dwo_unit->sect_off = sect_off;
11986 dwo_unit->length = cu->per_cu->length;
11988 if (dwarf_read_debug)
11989 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11990 sect_offset_str (sect_off),
11991 hex_string (dwo_unit->signature));
11994 /* Create the dwo_units for the CUs in a DWO_FILE.
11995 Note: This function processes DWO files only, not DWP files. */
11997 static void
11998 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11999 struct dwo_file &dwo_file, dwarf2_section_info &section,
12000 htab_t &cus_htab)
12002 struct objfile *objfile = dwarf2_per_objfile->objfile;
12003 const gdb_byte *info_ptr, *end_ptr;
12005 dwarf2_read_section (objfile, &section);
12006 info_ptr = section.buffer;
12008 if (info_ptr == NULL)
12009 return;
12011 if (dwarf_read_debug)
12013 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12014 get_section_name (&section),
12015 get_section_file_name (&section));
12018 end_ptr = info_ptr + section.size;
12019 while (info_ptr < end_ptr)
12021 struct dwarf2_per_cu_data per_cu;
12022 struct create_dwo_cu_data create_dwo_cu_data;
12023 struct dwo_unit *dwo_unit;
12024 void **slot;
12025 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12027 memset (&create_dwo_cu_data.dwo_unit, 0,
12028 sizeof (create_dwo_cu_data.dwo_unit));
12029 memset (&per_cu, 0, sizeof (per_cu));
12030 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12031 per_cu.is_debug_types = 0;
12032 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12033 per_cu.section = &section;
12034 create_dwo_cu_data.dwo_file = &dwo_file;
12036 init_cutu_and_read_dies_no_follow (
12037 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12038 info_ptr += per_cu.length;
12040 // If the unit could not be parsed, skip it.
12041 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12042 continue;
12044 if (cus_htab == NULL)
12045 cus_htab = allocate_dwo_unit_table (objfile);
12047 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12048 *dwo_unit = create_dwo_cu_data.dwo_unit;
12049 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12050 gdb_assert (slot != NULL);
12051 if (*slot != NULL)
12053 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12054 sect_offset dup_sect_off = dup_cu->sect_off;
12056 complaint (_("debug cu entry at offset %s is duplicate to"
12057 " the entry at offset %s, signature %s"),
12058 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12059 hex_string (dwo_unit->signature));
12061 *slot = (void *)dwo_unit;
12065 /* DWP file .debug_{cu,tu}_index section format:
12066 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12068 DWP Version 1:
12070 Both index sections have the same format, and serve to map a 64-bit
12071 signature to a set of section numbers. Each section begins with a header,
12072 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12073 indexes, and a pool of 32-bit section numbers. The index sections will be
12074 aligned at 8-byte boundaries in the file.
12076 The index section header consists of:
12078 V, 32 bit version number
12079 -, 32 bits unused
12080 N, 32 bit number of compilation units or type units in the index
12081 M, 32 bit number of slots in the hash table
12083 Numbers are recorded using the byte order of the application binary.
12085 The hash table begins at offset 16 in the section, and consists of an array
12086 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12087 order of the application binary). Unused slots in the hash table are 0.
12088 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12090 The parallel table begins immediately after the hash table
12091 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12092 array of 32-bit indexes (using the byte order of the application binary),
12093 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12094 table contains a 32-bit index into the pool of section numbers. For unused
12095 hash table slots, the corresponding entry in the parallel table will be 0.
12097 The pool of section numbers begins immediately following the hash table
12098 (at offset 16 + 12 * M from the beginning of the section). The pool of
12099 section numbers consists of an array of 32-bit words (using the byte order
12100 of the application binary). Each item in the array is indexed starting
12101 from 0. The hash table entry provides the index of the first section
12102 number in the set. Additional section numbers in the set follow, and the
12103 set is terminated by a 0 entry (section number 0 is not used in ELF).
12105 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12106 section must be the first entry in the set, and the .debug_abbrev.dwo must
12107 be the second entry. Other members of the set may follow in any order.
12111 DWP Version 2:
12113 DWP Version 2 combines all the .debug_info, etc. sections into one,
12114 and the entries in the index tables are now offsets into these sections.
12115 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12116 section.
12118 Index Section Contents:
12119 Header
12120 Hash Table of Signatures dwp_hash_table.hash_table
12121 Parallel Table of Indices dwp_hash_table.unit_table
12122 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12123 Table of Section Sizes dwp_hash_table.v2.sizes
12125 The index section header consists of:
12127 V, 32 bit version number
12128 L, 32 bit number of columns in the table of section offsets
12129 N, 32 bit number of compilation units or type units in the index
12130 M, 32 bit number of slots in the hash table
12132 Numbers are recorded using the byte order of the application binary.
12134 The hash table has the same format as version 1.
12135 The parallel table of indices has the same format as version 1,
12136 except that the entries are origin-1 indices into the table of sections
12137 offsets and the table of section sizes.
12139 The table of offsets begins immediately following the parallel table
12140 (at offset 16 + 12 * M from the beginning of the section). The table is
12141 a two-dimensional array of 32-bit words (using the byte order of the
12142 application binary), with L columns and N+1 rows, in row-major order.
12143 Each row in the array is indexed starting from 0. The first row provides
12144 a key to the remaining rows: each column in this row provides an identifier
12145 for a debug section, and the offsets in the same column of subsequent rows
12146 refer to that section. The section identifiers are:
12148 DW_SECT_INFO 1 .debug_info.dwo
12149 DW_SECT_TYPES 2 .debug_types.dwo
12150 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12151 DW_SECT_LINE 4 .debug_line.dwo
12152 DW_SECT_LOC 5 .debug_loc.dwo
12153 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12154 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12155 DW_SECT_MACRO 8 .debug_macro.dwo
12157 The offsets provided by the CU and TU index sections are the base offsets
12158 for the contributions made by each CU or TU to the corresponding section
12159 in the package file. Each CU and TU header contains an abbrev_offset
12160 field, used to find the abbreviations table for that CU or TU within the
12161 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12162 be interpreted as relative to the base offset given in the index section.
12163 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12164 should be interpreted as relative to the base offset for .debug_line.dwo,
12165 and offsets into other debug sections obtained from DWARF attributes should
12166 also be interpreted as relative to the corresponding base offset.
12168 The table of sizes begins immediately following the table of offsets.
12169 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12170 with L columns and N rows, in row-major order. Each row in the array is
12171 indexed starting from 1 (row 0 is shared by the two tables).
12175 Hash table lookup is handled the same in version 1 and 2:
12177 We assume that N and M will not exceed 2^32 - 1.
12178 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12180 Given a 64-bit compilation unit signature or a type signature S, an entry
12181 in the hash table is located as follows:
12183 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12184 the low-order k bits all set to 1.
12186 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12188 3) If the hash table entry at index H matches the signature, use that
12189 entry. If the hash table entry at index H is unused (all zeroes),
12190 terminate the search: the signature is not present in the table.
12192 4) Let H = (H + H') modulo M. Repeat at Step 3.
12194 Because M > N and H' and M are relatively prime, the search is guaranteed
12195 to stop at an unused slot or find the match. */
12197 /* Create a hash table to map DWO IDs to their CU/TU entry in
12198 .debug_{info,types}.dwo in DWP_FILE.
12199 Returns NULL if there isn't one.
12200 Note: This function processes DWP files only, not DWO files. */
12202 static struct dwp_hash_table *
12203 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12204 struct dwp_file *dwp_file, int is_debug_types)
12206 struct objfile *objfile = dwarf2_per_objfile->objfile;
12207 bfd *dbfd = dwp_file->dbfd.get ();
12208 const gdb_byte *index_ptr, *index_end;
12209 struct dwarf2_section_info *index;
12210 uint32_t version, nr_columns, nr_units, nr_slots;
12211 struct dwp_hash_table *htab;
12213 if (is_debug_types)
12214 index = &dwp_file->sections.tu_index;
12215 else
12216 index = &dwp_file->sections.cu_index;
12218 if (dwarf2_section_empty_p (index))
12219 return NULL;
12220 dwarf2_read_section (objfile, index);
12222 index_ptr = index->buffer;
12223 index_end = index_ptr + index->size;
12225 version = read_4_bytes (dbfd, index_ptr);
12226 index_ptr += 4;
12227 if (version == 2)
12228 nr_columns = read_4_bytes (dbfd, index_ptr);
12229 else
12230 nr_columns = 0;
12231 index_ptr += 4;
12232 nr_units = read_4_bytes (dbfd, index_ptr);
12233 index_ptr += 4;
12234 nr_slots = read_4_bytes (dbfd, index_ptr);
12235 index_ptr += 4;
12237 if (version != 1 && version != 2)
12239 error (_("Dwarf Error: unsupported DWP file version (%s)"
12240 " [in module %s]"),
12241 pulongest (version), dwp_file->name);
12243 if (nr_slots != (nr_slots & -nr_slots))
12245 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12246 " is not power of 2 [in module %s]"),
12247 pulongest (nr_slots), dwp_file->name);
12250 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12251 htab->version = version;
12252 htab->nr_columns = nr_columns;
12253 htab->nr_units = nr_units;
12254 htab->nr_slots = nr_slots;
12255 htab->hash_table = index_ptr;
12256 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12258 /* Exit early if the table is empty. */
12259 if (nr_slots == 0 || nr_units == 0
12260 || (version == 2 && nr_columns == 0))
12262 /* All must be zero. */
12263 if (nr_slots != 0 || nr_units != 0
12264 || (version == 2 && nr_columns != 0))
12266 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12267 " all zero [in modules %s]"),
12268 dwp_file->name);
12270 return htab;
12273 if (version == 1)
12275 htab->section_pool.v1.indices =
12276 htab->unit_table + sizeof (uint32_t) * nr_slots;
12277 /* It's harder to decide whether the section is too small in v1.
12278 V1 is deprecated anyway so we punt. */
12280 else
12282 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12283 int *ids = htab->section_pool.v2.section_ids;
12284 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12285 /* Reverse map for error checking. */
12286 int ids_seen[DW_SECT_MAX + 1];
12287 int i;
12289 if (nr_columns < 2)
12291 error (_("Dwarf Error: bad DWP hash table, too few columns"
12292 " in section table [in module %s]"),
12293 dwp_file->name);
12295 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12297 error (_("Dwarf Error: bad DWP hash table, too many columns"
12298 " in section table [in module %s]"),
12299 dwp_file->name);
12301 memset (ids, 255, sizeof_ids);
12302 memset (ids_seen, 255, sizeof (ids_seen));
12303 for (i = 0; i < nr_columns; ++i)
12305 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12307 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12309 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12310 " in section table [in module %s]"),
12311 id, dwp_file->name);
12313 if (ids_seen[id] != -1)
12315 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12316 " id %d in section table [in module %s]"),
12317 id, dwp_file->name);
12319 ids_seen[id] = i;
12320 ids[i] = id;
12322 /* Must have exactly one info or types section. */
12323 if (((ids_seen[DW_SECT_INFO] != -1)
12324 + (ids_seen[DW_SECT_TYPES] != -1))
12325 != 1)
12327 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12328 " DWO info/types section [in module %s]"),
12329 dwp_file->name);
12331 /* Must have an abbrev section. */
12332 if (ids_seen[DW_SECT_ABBREV] == -1)
12334 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12335 " section [in module %s]"),
12336 dwp_file->name);
12338 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12339 htab->section_pool.v2.sizes =
12340 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12341 * nr_units * nr_columns);
12342 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12343 * nr_units * nr_columns))
12344 > index_end)
12346 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12347 " [in module %s]"),
12348 dwp_file->name);
12352 return htab;
12355 /* Update SECTIONS with the data from SECTP.
12357 This function is like the other "locate" section routines that are
12358 passed to bfd_map_over_sections, but in this context the sections to
12359 read comes from the DWP V1 hash table, not the full ELF section table.
12361 The result is non-zero for success, or zero if an error was found. */
12363 static int
12364 locate_v1_virtual_dwo_sections (asection *sectp,
12365 struct virtual_v1_dwo_sections *sections)
12367 const struct dwop_section_names *names = &dwop_section_names;
12369 if (section_is_p (sectp->name, &names->abbrev_dwo))
12371 /* There can be only one. */
12372 if (sections->abbrev.s.section != NULL)
12373 return 0;
12374 sections->abbrev.s.section = sectp;
12375 sections->abbrev.size = bfd_section_size (sectp);
12377 else if (section_is_p (sectp->name, &names->info_dwo)
12378 || section_is_p (sectp->name, &names->types_dwo))
12380 /* There can be only one. */
12381 if (sections->info_or_types.s.section != NULL)
12382 return 0;
12383 sections->info_or_types.s.section = sectp;
12384 sections->info_or_types.size = bfd_section_size (sectp);
12386 else if (section_is_p (sectp->name, &names->line_dwo))
12388 /* There can be only one. */
12389 if (sections->line.s.section != NULL)
12390 return 0;
12391 sections->line.s.section = sectp;
12392 sections->line.size = bfd_section_size (sectp);
12394 else if (section_is_p (sectp->name, &names->loc_dwo))
12396 /* There can be only one. */
12397 if (sections->loc.s.section != NULL)
12398 return 0;
12399 sections->loc.s.section = sectp;
12400 sections->loc.size = bfd_section_size (sectp);
12402 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 /* There can be only one. */
12405 if (sections->macinfo.s.section != NULL)
12406 return 0;
12407 sections->macinfo.s.section = sectp;
12408 sections->macinfo.size = bfd_section_size (sectp);
12410 else if (section_is_p (sectp->name, &names->macro_dwo))
12412 /* There can be only one. */
12413 if (sections->macro.s.section != NULL)
12414 return 0;
12415 sections->macro.s.section = sectp;
12416 sections->macro.size = bfd_section_size (sectp);
12418 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12420 /* There can be only one. */
12421 if (sections->str_offsets.s.section != NULL)
12422 return 0;
12423 sections->str_offsets.s.section = sectp;
12424 sections->str_offsets.size = bfd_section_size (sectp);
12426 else
12428 /* No other kind of section is valid. */
12429 return 0;
12432 return 1;
12435 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12436 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12437 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12438 This is for DWP version 1 files. */
12440 static struct dwo_unit *
12441 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12442 struct dwp_file *dwp_file,
12443 uint32_t unit_index,
12444 const char *comp_dir,
12445 ULONGEST signature, int is_debug_types)
12447 struct objfile *objfile = dwarf2_per_objfile->objfile;
12448 const struct dwp_hash_table *dwp_htab =
12449 is_debug_types ? dwp_file->tus : dwp_file->cus;
12450 bfd *dbfd = dwp_file->dbfd.get ();
12451 const char *kind = is_debug_types ? "TU" : "CU";
12452 struct dwo_file *dwo_file;
12453 struct dwo_unit *dwo_unit;
12454 struct virtual_v1_dwo_sections sections;
12455 void **dwo_file_slot;
12456 int i;
12458 gdb_assert (dwp_file->version == 1);
12460 if (dwarf_read_debug)
12462 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12463 kind,
12464 pulongest (unit_index), hex_string (signature),
12465 dwp_file->name);
12468 /* Fetch the sections of this DWO unit.
12469 Put a limit on the number of sections we look for so that bad data
12470 doesn't cause us to loop forever. */
12472 #define MAX_NR_V1_DWO_SECTIONS \
12473 (1 /* .debug_info or .debug_types */ \
12474 + 1 /* .debug_abbrev */ \
12475 + 1 /* .debug_line */ \
12476 + 1 /* .debug_loc */ \
12477 + 1 /* .debug_str_offsets */ \
12478 + 1 /* .debug_macro or .debug_macinfo */ \
12479 + 1 /* trailing zero */)
12481 memset (&sections, 0, sizeof (sections));
12483 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12485 asection *sectp;
12486 uint32_t section_nr =
12487 read_4_bytes (dbfd,
12488 dwp_htab->section_pool.v1.indices
12489 + (unit_index + i) * sizeof (uint32_t));
12491 if (section_nr == 0)
12492 break;
12493 if (section_nr >= dwp_file->num_sections)
12495 error (_("Dwarf Error: bad DWP hash table, section number too large"
12496 " [in module %s]"),
12497 dwp_file->name);
12500 sectp = dwp_file->elf_sections[section_nr];
12501 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12503 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12504 " [in module %s]"),
12505 dwp_file->name);
12509 if (i < 2
12510 || dwarf2_section_empty_p (&sections.info_or_types)
12511 || dwarf2_section_empty_p (&sections.abbrev))
12513 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12514 " [in module %s]"),
12515 dwp_file->name);
12517 if (i == MAX_NR_V1_DWO_SECTIONS)
12519 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12520 " [in module %s]"),
12521 dwp_file->name);
12524 /* It's easier for the rest of the code if we fake a struct dwo_file and
12525 have dwo_unit "live" in that. At least for now.
12527 The DWP file can be made up of a random collection of CUs and TUs.
12528 However, for each CU + set of TUs that came from the same original DWO
12529 file, we can combine them back into a virtual DWO file to save space
12530 (fewer struct dwo_file objects to allocate). Remember that for really
12531 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12533 std::string virtual_dwo_name =
12534 string_printf ("virtual-dwo/%d-%d-%d-%d",
12535 get_section_id (&sections.abbrev),
12536 get_section_id (&sections.line),
12537 get_section_id (&sections.loc),
12538 get_section_id (&sections.str_offsets));
12539 /* Can we use an existing virtual DWO file? */
12540 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12541 virtual_dwo_name.c_str (),
12542 comp_dir);
12543 /* Create one if necessary. */
12544 if (*dwo_file_slot == NULL)
12546 if (dwarf_read_debug)
12548 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12549 virtual_dwo_name.c_str ());
12551 dwo_file = new struct dwo_file;
12552 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12553 virtual_dwo_name);
12554 dwo_file->comp_dir = comp_dir;
12555 dwo_file->sections.abbrev = sections.abbrev;
12556 dwo_file->sections.line = sections.line;
12557 dwo_file->sections.loc = sections.loc;
12558 dwo_file->sections.macinfo = sections.macinfo;
12559 dwo_file->sections.macro = sections.macro;
12560 dwo_file->sections.str_offsets = sections.str_offsets;
12561 /* The "str" section is global to the entire DWP file. */
12562 dwo_file->sections.str = dwp_file->sections.str;
12563 /* The info or types section is assigned below to dwo_unit,
12564 there's no need to record it in dwo_file.
12565 Also, we can't simply record type sections in dwo_file because
12566 we record a pointer into the vector in dwo_unit. As we collect more
12567 types we'll grow the vector and eventually have to reallocate space
12568 for it, invalidating all copies of pointers into the previous
12569 contents. */
12570 *dwo_file_slot = dwo_file;
12572 else
12574 if (dwarf_read_debug)
12576 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12577 virtual_dwo_name.c_str ());
12579 dwo_file = (struct dwo_file *) *dwo_file_slot;
12582 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12583 dwo_unit->dwo_file = dwo_file;
12584 dwo_unit->signature = signature;
12585 dwo_unit->section =
12586 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12587 *dwo_unit->section = sections.info_or_types;
12588 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12590 return dwo_unit;
12593 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12594 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12595 piece within that section used by a TU/CU, return a virtual section
12596 of just that piece. */
12598 static struct dwarf2_section_info
12599 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12600 struct dwarf2_section_info *section,
12601 bfd_size_type offset, bfd_size_type size)
12603 struct dwarf2_section_info result;
12604 asection *sectp;
12606 gdb_assert (section != NULL);
12607 gdb_assert (!section->is_virtual);
12609 memset (&result, 0, sizeof (result));
12610 result.s.containing_section = section;
12611 result.is_virtual = true;
12613 if (size == 0)
12614 return result;
12616 sectp = get_section_bfd_section (section);
12618 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12619 bounds of the real section. This is a pretty-rare event, so just
12620 flag an error (easier) instead of a warning and trying to cope. */
12621 if (sectp == NULL
12622 || offset + size > bfd_section_size (sectp))
12624 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12625 " in section %s [in module %s]"),
12626 sectp ? bfd_section_name (sectp) : "<unknown>",
12627 objfile_name (dwarf2_per_objfile->objfile));
12630 result.virtual_offset = offset;
12631 result.size = size;
12632 return result;
12635 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12636 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12637 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12638 This is for DWP version 2 files. */
12640 static struct dwo_unit *
12641 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12642 struct dwp_file *dwp_file,
12643 uint32_t unit_index,
12644 const char *comp_dir,
12645 ULONGEST signature, int is_debug_types)
12647 struct objfile *objfile = dwarf2_per_objfile->objfile;
12648 const struct dwp_hash_table *dwp_htab =
12649 is_debug_types ? dwp_file->tus : dwp_file->cus;
12650 bfd *dbfd = dwp_file->dbfd.get ();
12651 const char *kind = is_debug_types ? "TU" : "CU";
12652 struct dwo_file *dwo_file;
12653 struct dwo_unit *dwo_unit;
12654 struct virtual_v2_dwo_sections sections;
12655 void **dwo_file_slot;
12656 int i;
12658 gdb_assert (dwp_file->version == 2);
12660 if (dwarf_read_debug)
12662 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12663 kind,
12664 pulongest (unit_index), hex_string (signature),
12665 dwp_file->name);
12668 /* Fetch the section offsets of this DWO unit. */
12670 memset (&sections, 0, sizeof (sections));
12672 for (i = 0; i < dwp_htab->nr_columns; ++i)
12674 uint32_t offset = read_4_bytes (dbfd,
12675 dwp_htab->section_pool.v2.offsets
12676 + (((unit_index - 1) * dwp_htab->nr_columns
12677 + i)
12678 * sizeof (uint32_t)));
12679 uint32_t size = read_4_bytes (dbfd,
12680 dwp_htab->section_pool.v2.sizes
12681 + (((unit_index - 1) * dwp_htab->nr_columns
12682 + i)
12683 * sizeof (uint32_t)));
12685 switch (dwp_htab->section_pool.v2.section_ids[i])
12687 case DW_SECT_INFO:
12688 case DW_SECT_TYPES:
12689 sections.info_or_types_offset = offset;
12690 sections.info_or_types_size = size;
12691 break;
12692 case DW_SECT_ABBREV:
12693 sections.abbrev_offset = offset;
12694 sections.abbrev_size = size;
12695 break;
12696 case DW_SECT_LINE:
12697 sections.line_offset = offset;
12698 sections.line_size = size;
12699 break;
12700 case DW_SECT_LOC:
12701 sections.loc_offset = offset;
12702 sections.loc_size = size;
12703 break;
12704 case DW_SECT_STR_OFFSETS:
12705 sections.str_offsets_offset = offset;
12706 sections.str_offsets_size = size;
12707 break;
12708 case DW_SECT_MACINFO:
12709 sections.macinfo_offset = offset;
12710 sections.macinfo_size = size;
12711 break;
12712 case DW_SECT_MACRO:
12713 sections.macro_offset = offset;
12714 sections.macro_size = size;
12715 break;
12719 /* It's easier for the rest of the code if we fake a struct dwo_file and
12720 have dwo_unit "live" in that. At least for now.
12722 The DWP file can be made up of a random collection of CUs and TUs.
12723 However, for each CU + set of TUs that came from the same original DWO
12724 file, we can combine them back into a virtual DWO file to save space
12725 (fewer struct dwo_file objects to allocate). Remember that for really
12726 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12728 std::string virtual_dwo_name =
12729 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12730 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12731 (long) (sections.line_size ? sections.line_offset : 0),
12732 (long) (sections.loc_size ? sections.loc_offset : 0),
12733 (long) (sections.str_offsets_size
12734 ? sections.str_offsets_offset : 0));
12735 /* Can we use an existing virtual DWO file? */
12736 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12737 virtual_dwo_name.c_str (),
12738 comp_dir);
12739 /* Create one if necessary. */
12740 if (*dwo_file_slot == NULL)
12742 if (dwarf_read_debug)
12744 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12745 virtual_dwo_name.c_str ());
12747 dwo_file = new struct dwo_file;
12748 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12749 virtual_dwo_name);
12750 dwo_file->comp_dir = comp_dir;
12751 dwo_file->sections.abbrev =
12752 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12753 sections.abbrev_offset, sections.abbrev_size);
12754 dwo_file->sections.line =
12755 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12756 sections.line_offset, sections.line_size);
12757 dwo_file->sections.loc =
12758 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12759 sections.loc_offset, sections.loc_size);
12760 dwo_file->sections.macinfo =
12761 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12762 sections.macinfo_offset, sections.macinfo_size);
12763 dwo_file->sections.macro =
12764 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12765 sections.macro_offset, sections.macro_size);
12766 dwo_file->sections.str_offsets =
12767 create_dwp_v2_section (dwarf2_per_objfile,
12768 &dwp_file->sections.str_offsets,
12769 sections.str_offsets_offset,
12770 sections.str_offsets_size);
12771 /* The "str" section is global to the entire DWP file. */
12772 dwo_file->sections.str = dwp_file->sections.str;
12773 /* The info or types section is assigned below to dwo_unit,
12774 there's no need to record it in dwo_file.
12775 Also, we can't simply record type sections in dwo_file because
12776 we record a pointer into the vector in dwo_unit. As we collect more
12777 types we'll grow the vector and eventually have to reallocate space
12778 for it, invalidating all copies of pointers into the previous
12779 contents. */
12780 *dwo_file_slot = dwo_file;
12782 else
12784 if (dwarf_read_debug)
12786 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12787 virtual_dwo_name.c_str ());
12789 dwo_file = (struct dwo_file *) *dwo_file_slot;
12792 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12793 dwo_unit->dwo_file = dwo_file;
12794 dwo_unit->signature = signature;
12795 dwo_unit->section =
12796 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12797 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12798 is_debug_types
12799 ? &dwp_file->sections.types
12800 : &dwp_file->sections.info,
12801 sections.info_or_types_offset,
12802 sections.info_or_types_size);
12803 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12805 return dwo_unit;
12808 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12809 Returns NULL if the signature isn't found. */
12811 static struct dwo_unit *
12812 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12813 struct dwp_file *dwp_file, const char *comp_dir,
12814 ULONGEST signature, int is_debug_types)
12816 const struct dwp_hash_table *dwp_htab =
12817 is_debug_types ? dwp_file->tus : dwp_file->cus;
12818 bfd *dbfd = dwp_file->dbfd.get ();
12819 uint32_t mask = dwp_htab->nr_slots - 1;
12820 uint32_t hash = signature & mask;
12821 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12822 unsigned int i;
12823 void **slot;
12824 struct dwo_unit find_dwo_cu;
12826 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12827 find_dwo_cu.signature = signature;
12828 slot = htab_find_slot (is_debug_types
12829 ? dwp_file->loaded_tus
12830 : dwp_file->loaded_cus,
12831 &find_dwo_cu, INSERT);
12833 if (*slot != NULL)
12834 return (struct dwo_unit *) *slot;
12836 /* Use a for loop so that we don't loop forever on bad debug info. */
12837 for (i = 0; i < dwp_htab->nr_slots; ++i)
12839 ULONGEST signature_in_table;
12841 signature_in_table =
12842 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12843 if (signature_in_table == signature)
12845 uint32_t unit_index =
12846 read_4_bytes (dbfd,
12847 dwp_htab->unit_table + hash * sizeof (uint32_t));
12849 if (dwp_file->version == 1)
12851 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12852 dwp_file, unit_index,
12853 comp_dir, signature,
12854 is_debug_types);
12856 else
12858 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12859 dwp_file, unit_index,
12860 comp_dir, signature,
12861 is_debug_types);
12863 return (struct dwo_unit *) *slot;
12865 if (signature_in_table == 0)
12866 return NULL;
12867 hash = (hash + hash2) & mask;
12870 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12871 " [in module %s]"),
12872 dwp_file->name);
12875 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12876 Open the file specified by FILE_NAME and hand it off to BFD for
12877 preliminary analysis. Return a newly initialized bfd *, which
12878 includes a canonicalized copy of FILE_NAME.
12879 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12880 SEARCH_CWD is true if the current directory is to be searched.
12881 It will be searched before debug-file-directory.
12882 If successful, the file is added to the bfd include table of the
12883 objfile's bfd (see gdb_bfd_record_inclusion).
12884 If unable to find/open the file, return NULL.
12885 NOTE: This function is derived from symfile_bfd_open. */
12887 static gdb_bfd_ref_ptr
12888 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12889 const char *file_name, int is_dwp, int search_cwd)
12891 int desc;
12892 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12893 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12894 to debug_file_directory. */
12895 const char *search_path;
12896 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12898 gdb::unique_xmalloc_ptr<char> search_path_holder;
12899 if (search_cwd)
12901 if (*debug_file_directory != '\0')
12903 search_path_holder.reset (concat (".", dirname_separator_string,
12904 debug_file_directory,
12905 (char *) NULL));
12906 search_path = search_path_holder.get ();
12908 else
12909 search_path = ".";
12911 else
12912 search_path = debug_file_directory;
12914 openp_flags flags = OPF_RETURN_REALPATH;
12915 if (is_dwp)
12916 flags |= OPF_SEARCH_IN_PATH;
12918 gdb::unique_xmalloc_ptr<char> absolute_name;
12919 desc = openp (search_path, flags, file_name,
12920 O_RDONLY | O_BINARY, &absolute_name);
12921 if (desc < 0)
12922 return NULL;
12924 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12925 gnutarget, desc));
12926 if (sym_bfd == NULL)
12927 return NULL;
12928 bfd_set_cacheable (sym_bfd.get (), 1);
12930 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12931 return NULL;
12933 /* Success. Record the bfd as having been included by the objfile's bfd.
12934 This is important because things like demangled_names_hash lives in the
12935 objfile's per_bfd space and may have references to things like symbol
12936 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12937 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12939 return sym_bfd;
12942 /* Try to open DWO file FILE_NAME.
12943 COMP_DIR is the DW_AT_comp_dir attribute.
12944 The result is the bfd handle of the file.
12945 If there is a problem finding or opening the file, return NULL.
12946 Upon success, the canonicalized path of the file is stored in the bfd,
12947 same as symfile_bfd_open. */
12949 static gdb_bfd_ref_ptr
12950 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12951 const char *file_name, const char *comp_dir)
12953 if (IS_ABSOLUTE_PATH (file_name))
12954 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12955 0 /*is_dwp*/, 0 /*search_cwd*/);
12957 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12959 if (comp_dir != NULL)
12961 char *path_to_try = concat (comp_dir, SLASH_STRING,
12962 file_name, (char *) NULL);
12964 /* NOTE: If comp_dir is a relative path, this will also try the
12965 search path, which seems useful. */
12966 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12967 path_to_try,
12968 0 /*is_dwp*/,
12969 1 /*search_cwd*/));
12970 xfree (path_to_try);
12971 if (abfd != NULL)
12972 return abfd;
12975 /* That didn't work, try debug-file-directory, which, despite its name,
12976 is a list of paths. */
12978 if (*debug_file_directory == '\0')
12979 return NULL;
12981 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12982 0 /*is_dwp*/, 1 /*search_cwd*/);
12985 /* This function is mapped across the sections and remembers the offset and
12986 size of each of the DWO debugging sections we are interested in. */
12988 static void
12989 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12991 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12992 const struct dwop_section_names *names = &dwop_section_names;
12994 if (section_is_p (sectp->name, &names->abbrev_dwo))
12996 dwo_sections->abbrev.s.section = sectp;
12997 dwo_sections->abbrev.size = bfd_section_size (sectp);
12999 else if (section_is_p (sectp->name, &names->info_dwo))
13001 dwo_sections->info.s.section = sectp;
13002 dwo_sections->info.size = bfd_section_size (sectp);
13004 else if (section_is_p (sectp->name, &names->line_dwo))
13006 dwo_sections->line.s.section = sectp;
13007 dwo_sections->line.size = bfd_section_size (sectp);
13009 else if (section_is_p (sectp->name, &names->loc_dwo))
13011 dwo_sections->loc.s.section = sectp;
13012 dwo_sections->loc.size = bfd_section_size (sectp);
13014 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13016 dwo_sections->macinfo.s.section = sectp;
13017 dwo_sections->macinfo.size = bfd_section_size (sectp);
13019 else if (section_is_p (sectp->name, &names->macro_dwo))
13021 dwo_sections->macro.s.section = sectp;
13022 dwo_sections->macro.size = bfd_section_size (sectp);
13024 else if (section_is_p (sectp->name, &names->str_dwo))
13026 dwo_sections->str.s.section = sectp;
13027 dwo_sections->str.size = bfd_section_size (sectp);
13029 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13031 dwo_sections->str_offsets.s.section = sectp;
13032 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13034 else if (section_is_p (sectp->name, &names->types_dwo))
13036 struct dwarf2_section_info type_section;
13038 memset (&type_section, 0, sizeof (type_section));
13039 type_section.s.section = sectp;
13040 type_section.size = bfd_section_size (sectp);
13041 dwo_sections->types.push_back (type_section);
13045 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13046 by PER_CU. This is for the non-DWP case.
13047 The result is NULL if DWO_NAME can't be found. */
13049 static struct dwo_file *
13050 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13051 const char *dwo_name, const char *comp_dir)
13053 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13055 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13056 if (dbfd == NULL)
13058 if (dwarf_read_debug)
13059 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13060 return NULL;
13063 dwo_file_up dwo_file (new struct dwo_file);
13064 dwo_file->dwo_name = dwo_name;
13065 dwo_file->comp_dir = comp_dir;
13066 dwo_file->dbfd = std::move (dbfd);
13068 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13069 &dwo_file->sections);
13071 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13072 dwo_file->cus);
13074 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13075 dwo_file->sections.types, dwo_file->tus);
13077 if (dwarf_read_debug)
13078 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13080 return dwo_file.release ();
13083 /* This function is mapped across the sections and remembers the offset and
13084 size of each of the DWP debugging sections common to version 1 and 2 that
13085 we are interested in. */
13087 static void
13088 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13089 void *dwp_file_ptr)
13091 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13092 const struct dwop_section_names *names = &dwop_section_names;
13093 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13095 /* Record the ELF section number for later lookup: this is what the
13096 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13097 gdb_assert (elf_section_nr < dwp_file->num_sections);
13098 dwp_file->elf_sections[elf_section_nr] = sectp;
13100 /* Look for specific sections that we need. */
13101 if (section_is_p (sectp->name, &names->str_dwo))
13103 dwp_file->sections.str.s.section = sectp;
13104 dwp_file->sections.str.size = bfd_section_size (sectp);
13106 else if (section_is_p (sectp->name, &names->cu_index))
13108 dwp_file->sections.cu_index.s.section = sectp;
13109 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13111 else if (section_is_p (sectp->name, &names->tu_index))
13113 dwp_file->sections.tu_index.s.section = sectp;
13114 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13118 /* This function is mapped across the sections and remembers the offset and
13119 size of each of the DWP version 2 debugging sections that we are interested
13120 in. This is split into a separate function because we don't know if we
13121 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13123 static void
13124 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13126 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13127 const struct dwop_section_names *names = &dwop_section_names;
13128 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13130 /* Record the ELF section number for later lookup: this is what the
13131 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13132 gdb_assert (elf_section_nr < dwp_file->num_sections);
13133 dwp_file->elf_sections[elf_section_nr] = sectp;
13135 /* Look for specific sections that we need. */
13136 if (section_is_p (sectp->name, &names->abbrev_dwo))
13138 dwp_file->sections.abbrev.s.section = sectp;
13139 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13141 else if (section_is_p (sectp->name, &names->info_dwo))
13143 dwp_file->sections.info.s.section = sectp;
13144 dwp_file->sections.info.size = bfd_section_size (sectp);
13146 else if (section_is_p (sectp->name, &names->line_dwo))
13148 dwp_file->sections.line.s.section = sectp;
13149 dwp_file->sections.line.size = bfd_section_size (sectp);
13151 else if (section_is_p (sectp->name, &names->loc_dwo))
13153 dwp_file->sections.loc.s.section = sectp;
13154 dwp_file->sections.loc.size = bfd_section_size (sectp);
13156 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13158 dwp_file->sections.macinfo.s.section = sectp;
13159 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13161 else if (section_is_p (sectp->name, &names->macro_dwo))
13163 dwp_file->sections.macro.s.section = sectp;
13164 dwp_file->sections.macro.size = bfd_section_size (sectp);
13166 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13168 dwp_file->sections.str_offsets.s.section = sectp;
13169 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13171 else if (section_is_p (sectp->name, &names->types_dwo))
13173 dwp_file->sections.types.s.section = sectp;
13174 dwp_file->sections.types.size = bfd_section_size (sectp);
13178 /* Hash function for dwp_file loaded CUs/TUs. */
13180 static hashval_t
13181 hash_dwp_loaded_cutus (const void *item)
13183 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13185 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13186 return dwo_unit->signature;
13189 /* Equality function for dwp_file loaded CUs/TUs. */
13191 static int
13192 eq_dwp_loaded_cutus (const void *a, const void *b)
13194 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13195 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13197 return dua->signature == dub->signature;
13200 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13202 static htab_t
13203 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13205 return htab_create_alloc_ex (3,
13206 hash_dwp_loaded_cutus,
13207 eq_dwp_loaded_cutus,
13208 NULL,
13209 &objfile->objfile_obstack,
13210 hashtab_obstack_allocate,
13211 dummy_obstack_deallocate);
13214 /* Try to open DWP file FILE_NAME.
13215 The result is the bfd handle of the file.
13216 If there is a problem finding or opening the file, return NULL.
13217 Upon success, the canonicalized path of the file is stored in the bfd,
13218 same as symfile_bfd_open. */
13220 static gdb_bfd_ref_ptr
13221 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13222 const char *file_name)
13224 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13225 1 /*is_dwp*/,
13226 1 /*search_cwd*/));
13227 if (abfd != NULL)
13228 return abfd;
13230 /* Work around upstream bug 15652.
13231 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13232 [Whether that's a "bug" is debatable, but it is getting in our way.]
13233 We have no real idea where the dwp file is, because gdb's realpath-ing
13234 of the executable's path may have discarded the needed info.
13235 [IWBN if the dwp file name was recorded in the executable, akin to
13236 .gnu_debuglink, but that doesn't exist yet.]
13237 Strip the directory from FILE_NAME and search again. */
13238 if (*debug_file_directory != '\0')
13240 /* Don't implicitly search the current directory here.
13241 If the user wants to search "." to handle this case,
13242 it must be added to debug-file-directory. */
13243 return try_open_dwop_file (dwarf2_per_objfile,
13244 lbasename (file_name), 1 /*is_dwp*/,
13245 0 /*search_cwd*/);
13248 return NULL;
13251 /* Initialize the use of the DWP file for the current objfile.
13252 By convention the name of the DWP file is ${objfile}.dwp.
13253 The result is NULL if it can't be found. */
13255 static std::unique_ptr<struct dwp_file>
13256 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13258 struct objfile *objfile = dwarf2_per_objfile->objfile;
13260 /* Try to find first .dwp for the binary file before any symbolic links
13261 resolving. */
13263 /* If the objfile is a debug file, find the name of the real binary
13264 file and get the name of dwp file from there. */
13265 std::string dwp_name;
13266 if (objfile->separate_debug_objfile_backlink != NULL)
13268 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13269 const char *backlink_basename = lbasename (backlink->original_name);
13271 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13273 else
13274 dwp_name = objfile->original_name;
13276 dwp_name += ".dwp";
13278 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13279 if (dbfd == NULL
13280 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13282 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13283 dwp_name = objfile_name (objfile);
13284 dwp_name += ".dwp";
13285 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13288 if (dbfd == NULL)
13290 if (dwarf_read_debug)
13291 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13292 return std::unique_ptr<dwp_file> ();
13295 const char *name = bfd_get_filename (dbfd.get ());
13296 std::unique_ptr<struct dwp_file> dwp_file
13297 (new struct dwp_file (name, std::move (dbfd)));
13299 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13300 dwp_file->elf_sections =
13301 OBSTACK_CALLOC (&objfile->objfile_obstack,
13302 dwp_file->num_sections, asection *);
13304 bfd_map_over_sections (dwp_file->dbfd.get (),
13305 dwarf2_locate_common_dwp_sections,
13306 dwp_file.get ());
13308 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13311 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13314 /* The DWP file version is stored in the hash table. Oh well. */
13315 if (dwp_file->cus && dwp_file->tus
13316 && dwp_file->cus->version != dwp_file->tus->version)
13318 /* Technically speaking, we should try to limp along, but this is
13319 pretty bizarre. We use pulongest here because that's the established
13320 portability solution (e.g, we cannot use %u for uint32_t). */
13321 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13322 " TU version %s [in DWP file %s]"),
13323 pulongest (dwp_file->cus->version),
13324 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13327 if (dwp_file->cus)
13328 dwp_file->version = dwp_file->cus->version;
13329 else if (dwp_file->tus)
13330 dwp_file->version = dwp_file->tus->version;
13331 else
13332 dwp_file->version = 2;
13334 if (dwp_file->version == 2)
13335 bfd_map_over_sections (dwp_file->dbfd.get (),
13336 dwarf2_locate_v2_dwp_sections,
13337 dwp_file.get ());
13339 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13340 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13342 if (dwarf_read_debug)
13344 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13345 fprintf_unfiltered (gdb_stdlog,
13346 " %s CUs, %s TUs\n",
13347 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13348 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13351 return dwp_file;
13354 /* Wrapper around open_and_init_dwp_file, only open it once. */
13356 static struct dwp_file *
13357 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13359 if (! dwarf2_per_objfile->dwp_checked)
13361 dwarf2_per_objfile->dwp_file
13362 = open_and_init_dwp_file (dwarf2_per_objfile);
13363 dwarf2_per_objfile->dwp_checked = 1;
13365 return dwarf2_per_objfile->dwp_file.get ();
13368 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13369 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13370 or in the DWP file for the objfile, referenced by THIS_UNIT.
13371 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13372 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13374 This is called, for example, when wanting to read a variable with a
13375 complex location. Therefore we don't want to do file i/o for every call.
13376 Therefore we don't want to look for a DWO file on every call.
13377 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13378 then we check if we've already seen DWO_NAME, and only THEN do we check
13379 for a DWO file.
13381 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13382 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13384 static struct dwo_unit *
13385 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13386 const char *dwo_name, const char *comp_dir,
13387 ULONGEST signature, int is_debug_types)
13389 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13390 struct objfile *objfile = dwarf2_per_objfile->objfile;
13391 const char *kind = is_debug_types ? "TU" : "CU";
13392 void **dwo_file_slot;
13393 struct dwo_file *dwo_file;
13394 struct dwp_file *dwp_file;
13396 /* First see if there's a DWP file.
13397 If we have a DWP file but didn't find the DWO inside it, don't
13398 look for the original DWO file. It makes gdb behave differently
13399 depending on whether one is debugging in the build tree. */
13401 dwp_file = get_dwp_file (dwarf2_per_objfile);
13402 if (dwp_file != NULL)
13404 const struct dwp_hash_table *dwp_htab =
13405 is_debug_types ? dwp_file->tus : dwp_file->cus;
13407 if (dwp_htab != NULL)
13409 struct dwo_unit *dwo_cutu =
13410 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13411 signature, is_debug_types);
13413 if (dwo_cutu != NULL)
13415 if (dwarf_read_debug)
13417 fprintf_unfiltered (gdb_stdlog,
13418 "Virtual DWO %s %s found: @%s\n",
13419 kind, hex_string (signature),
13420 host_address_to_string (dwo_cutu));
13422 return dwo_cutu;
13426 else
13428 /* No DWP file, look for the DWO file. */
13430 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13431 dwo_name, comp_dir);
13432 if (*dwo_file_slot == NULL)
13434 /* Read in the file and build a table of the CUs/TUs it contains. */
13435 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13437 /* NOTE: This will be NULL if unable to open the file. */
13438 dwo_file = (struct dwo_file *) *dwo_file_slot;
13440 if (dwo_file != NULL)
13442 struct dwo_unit *dwo_cutu = NULL;
13444 if (is_debug_types && dwo_file->tus)
13446 struct dwo_unit find_dwo_cutu;
13448 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13449 find_dwo_cutu.signature = signature;
13450 dwo_cutu
13451 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13453 else if (!is_debug_types && dwo_file->cus)
13455 struct dwo_unit find_dwo_cutu;
13457 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13458 find_dwo_cutu.signature = signature;
13459 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13460 &find_dwo_cutu);
13463 if (dwo_cutu != NULL)
13465 if (dwarf_read_debug)
13467 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13468 kind, dwo_name, hex_string (signature),
13469 host_address_to_string (dwo_cutu));
13471 return dwo_cutu;
13476 /* We didn't find it. This could mean a dwo_id mismatch, or
13477 someone deleted the DWO/DWP file, or the search path isn't set up
13478 correctly to find the file. */
13480 if (dwarf_read_debug)
13482 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13483 kind, dwo_name, hex_string (signature));
13486 /* This is a warning and not a complaint because it can be caused by
13487 pilot error (e.g., user accidentally deleting the DWO). */
13489 /* Print the name of the DWP file if we looked there, helps the user
13490 better diagnose the problem. */
13491 std::string dwp_text;
13493 if (dwp_file != NULL)
13494 dwp_text = string_printf (" [in DWP file %s]",
13495 lbasename (dwp_file->name));
13497 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13498 " [in module %s]"),
13499 kind, dwo_name, hex_string (signature),
13500 dwp_text.c_str (),
13501 this_unit->is_debug_types ? "TU" : "CU",
13502 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13504 return NULL;
13507 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13508 See lookup_dwo_cutu_unit for details. */
13510 static struct dwo_unit *
13511 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13512 const char *dwo_name, const char *comp_dir,
13513 ULONGEST signature)
13515 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13518 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13519 See lookup_dwo_cutu_unit for details. */
13521 static struct dwo_unit *
13522 lookup_dwo_type_unit (struct signatured_type *this_tu,
13523 const char *dwo_name, const char *comp_dir)
13525 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13528 /* Traversal function for queue_and_load_all_dwo_tus. */
13530 static int
13531 queue_and_load_dwo_tu (void **slot, void *info)
13533 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13534 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13535 ULONGEST signature = dwo_unit->signature;
13536 struct signatured_type *sig_type =
13537 lookup_dwo_signatured_type (per_cu->cu, signature);
13539 if (sig_type != NULL)
13541 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13543 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13544 a real dependency of PER_CU on SIG_TYPE. That is detected later
13545 while processing PER_CU. */
13546 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13547 load_full_type_unit (sig_cu);
13548 per_cu->imported_symtabs_push (sig_cu);
13551 return 1;
13554 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13555 The DWO may have the only definition of the type, though it may not be
13556 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13557 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13559 static void
13560 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13562 struct dwo_unit *dwo_unit;
13563 struct dwo_file *dwo_file;
13565 gdb_assert (!per_cu->is_debug_types);
13566 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13567 gdb_assert (per_cu->cu != NULL);
13569 dwo_unit = per_cu->cu->dwo_unit;
13570 gdb_assert (dwo_unit != NULL);
13572 dwo_file = dwo_unit->dwo_file;
13573 if (dwo_file->tus != NULL)
13574 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13577 /* Read in various DIEs. */
13579 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13580 Inherit only the children of the DW_AT_abstract_origin DIE not being
13581 already referenced by DW_AT_abstract_origin from the children of the
13582 current DIE. */
13584 static void
13585 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13587 struct die_info *child_die;
13588 sect_offset *offsetp;
13589 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13590 struct die_info *origin_die;
13591 /* Iterator of the ORIGIN_DIE children. */
13592 struct die_info *origin_child_die;
13593 struct attribute *attr;
13594 struct dwarf2_cu *origin_cu;
13595 struct pending **origin_previous_list_in_scope;
13597 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13598 if (!attr)
13599 return;
13601 /* Note that following die references may follow to a die in a
13602 different cu. */
13604 origin_cu = cu;
13605 origin_die = follow_die_ref (die, attr, &origin_cu);
13607 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13608 symbols in. */
13609 origin_previous_list_in_scope = origin_cu->list_in_scope;
13610 origin_cu->list_in_scope = cu->list_in_scope;
13612 if (die->tag != origin_die->tag
13613 && !(die->tag == DW_TAG_inlined_subroutine
13614 && origin_die->tag == DW_TAG_subprogram))
13615 complaint (_("DIE %s and its abstract origin %s have different tags"),
13616 sect_offset_str (die->sect_off),
13617 sect_offset_str (origin_die->sect_off));
13619 std::vector<sect_offset> offsets;
13621 for (child_die = die->child;
13622 child_die && child_die->tag;
13623 child_die = sibling_die (child_die))
13625 struct die_info *child_origin_die;
13626 struct dwarf2_cu *child_origin_cu;
13628 /* We are trying to process concrete instance entries:
13629 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13630 it's not relevant to our analysis here. i.e. detecting DIEs that are
13631 present in the abstract instance but not referenced in the concrete
13632 one. */
13633 if (child_die->tag == DW_TAG_call_site
13634 || child_die->tag == DW_TAG_GNU_call_site)
13635 continue;
13637 /* For each CHILD_DIE, find the corresponding child of
13638 ORIGIN_DIE. If there is more than one layer of
13639 DW_AT_abstract_origin, follow them all; there shouldn't be,
13640 but GCC versions at least through 4.4 generate this (GCC PR
13641 40573). */
13642 child_origin_die = child_die;
13643 child_origin_cu = cu;
13644 while (1)
13646 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13647 child_origin_cu);
13648 if (attr == NULL)
13649 break;
13650 child_origin_die = follow_die_ref (child_origin_die, attr,
13651 &child_origin_cu);
13654 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13655 counterpart may exist. */
13656 if (child_origin_die != child_die)
13658 if (child_die->tag != child_origin_die->tag
13659 && !(child_die->tag == DW_TAG_inlined_subroutine
13660 && child_origin_die->tag == DW_TAG_subprogram))
13661 complaint (_("Child DIE %s and its abstract origin %s have "
13662 "different tags"),
13663 sect_offset_str (child_die->sect_off),
13664 sect_offset_str (child_origin_die->sect_off));
13665 if (child_origin_die->parent != origin_die)
13666 complaint (_("Child DIE %s and its abstract origin %s have "
13667 "different parents"),
13668 sect_offset_str (child_die->sect_off),
13669 sect_offset_str (child_origin_die->sect_off));
13670 else
13671 offsets.push_back (child_origin_die->sect_off);
13674 std::sort (offsets.begin (), offsets.end ());
13675 sect_offset *offsets_end = offsets.data () + offsets.size ();
13676 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13677 if (offsetp[-1] == *offsetp)
13678 complaint (_("Multiple children of DIE %s refer "
13679 "to DIE %s as their abstract origin"),
13680 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13682 offsetp = offsets.data ();
13683 origin_child_die = origin_die->child;
13684 while (origin_child_die && origin_child_die->tag)
13686 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13687 while (offsetp < offsets_end
13688 && *offsetp < origin_child_die->sect_off)
13689 offsetp++;
13690 if (offsetp >= offsets_end
13691 || *offsetp > origin_child_die->sect_off)
13693 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13694 Check whether we're already processing ORIGIN_CHILD_DIE.
13695 This can happen with mutually referenced abstract_origins.
13696 PR 16581. */
13697 if (!origin_child_die->in_process)
13698 process_die (origin_child_die, origin_cu);
13700 origin_child_die = sibling_die (origin_child_die);
13702 origin_cu->list_in_scope = origin_previous_list_in_scope;
13704 if (cu != origin_cu)
13705 compute_delayed_physnames (origin_cu);
13708 static void
13709 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13712 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13713 struct context_stack *newobj;
13714 CORE_ADDR lowpc;
13715 CORE_ADDR highpc;
13716 struct die_info *child_die;
13717 struct attribute *attr, *call_line, *call_file;
13718 const char *name;
13719 CORE_ADDR baseaddr;
13720 struct block *block;
13721 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13722 std::vector<struct symbol *> template_args;
13723 struct template_symbol *templ_func = NULL;
13725 if (inlined_func)
13727 /* If we do not have call site information, we can't show the
13728 caller of this inlined function. That's too confusing, so
13729 only use the scope for local variables. */
13730 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13731 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13732 if (call_line == NULL || call_file == NULL)
13734 read_lexical_block_scope (die, cu);
13735 return;
13739 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13741 name = dwarf2_name (die, cu);
13743 /* Ignore functions with missing or empty names. These are actually
13744 illegal according to the DWARF standard. */
13745 if (name == NULL)
13747 complaint (_("missing name for subprogram DIE at %s"),
13748 sect_offset_str (die->sect_off));
13749 return;
13752 /* Ignore functions with missing or invalid low and high pc attributes. */
13753 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13754 <= PC_BOUNDS_INVALID)
13756 attr = dwarf2_attr (die, DW_AT_external, cu);
13757 if (!attr || !DW_UNSND (attr))
13758 complaint (_("cannot get low and high bounds "
13759 "for subprogram DIE at %s"),
13760 sect_offset_str (die->sect_off));
13761 return;
13764 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13765 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13767 /* If we have any template arguments, then we must allocate a
13768 different sort of symbol. */
13769 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13771 if (child_die->tag == DW_TAG_template_type_param
13772 || child_die->tag == DW_TAG_template_value_param)
13774 templ_func = allocate_template_symbol (objfile);
13775 templ_func->subclass = SYMBOL_TEMPLATE;
13776 break;
13780 newobj = cu->get_builder ()->push_context (0, lowpc);
13781 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13782 (struct symbol *) templ_func);
13784 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13785 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13786 cu->language);
13788 /* If there is a location expression for DW_AT_frame_base, record
13789 it. */
13790 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13791 if (attr != nullptr)
13792 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13794 /* If there is a location for the static link, record it. */
13795 newobj->static_link = NULL;
13796 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13797 if (attr != nullptr)
13799 newobj->static_link
13800 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13801 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13802 dwarf2_per_cu_addr_type (cu->per_cu));
13805 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13807 if (die->child != NULL)
13809 child_die = die->child;
13810 while (child_die && child_die->tag)
13812 if (child_die->tag == DW_TAG_template_type_param
13813 || child_die->tag == DW_TAG_template_value_param)
13815 struct symbol *arg = new_symbol (child_die, NULL, cu);
13817 if (arg != NULL)
13818 template_args.push_back (arg);
13820 else
13821 process_die (child_die, cu);
13822 child_die = sibling_die (child_die);
13826 inherit_abstract_dies (die, cu);
13828 /* If we have a DW_AT_specification, we might need to import using
13829 directives from the context of the specification DIE. See the
13830 comment in determine_prefix. */
13831 if (cu->language == language_cplus
13832 && dwarf2_attr (die, DW_AT_specification, cu))
13834 struct dwarf2_cu *spec_cu = cu;
13835 struct die_info *spec_die = die_specification (die, &spec_cu);
13837 while (spec_die)
13839 child_die = spec_die->child;
13840 while (child_die && child_die->tag)
13842 if (child_die->tag == DW_TAG_imported_module)
13843 process_die (child_die, spec_cu);
13844 child_die = sibling_die (child_die);
13847 /* In some cases, GCC generates specification DIEs that
13848 themselves contain DW_AT_specification attributes. */
13849 spec_die = die_specification (spec_die, &spec_cu);
13853 struct context_stack cstk = cu->get_builder ()->pop_context ();
13854 /* Make a block for the local symbols within. */
13855 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13856 cstk.static_link, lowpc, highpc);
13858 /* For C++, set the block's scope. */
13859 if ((cu->language == language_cplus
13860 || cu->language == language_fortran
13861 || cu->language == language_d
13862 || cu->language == language_rust)
13863 && cu->processing_has_namespace_info)
13864 block_set_scope (block, determine_prefix (die, cu),
13865 &objfile->objfile_obstack);
13867 /* If we have address ranges, record them. */
13868 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13870 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13872 /* Attach template arguments to function. */
13873 if (!template_args.empty ())
13875 gdb_assert (templ_func != NULL);
13877 templ_func->n_template_arguments = template_args.size ();
13878 templ_func->template_arguments
13879 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13880 templ_func->n_template_arguments);
13881 memcpy (templ_func->template_arguments,
13882 template_args.data (),
13883 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13885 /* Make sure that the symtab is set on the new symbols. Even
13886 though they don't appear in this symtab directly, other parts
13887 of gdb assume that symbols do, and this is reasonably
13888 true. */
13889 for (symbol *sym : template_args)
13890 symbol_set_symtab (sym, symbol_symtab (templ_func));
13893 /* In C++, we can have functions nested inside functions (e.g., when
13894 a function declares a class that has methods). This means that
13895 when we finish processing a function scope, we may need to go
13896 back to building a containing block's symbol lists. */
13897 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13898 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13900 /* If we've finished processing a top-level function, subsequent
13901 symbols go in the file symbol list. */
13902 if (cu->get_builder ()->outermost_context_p ())
13903 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13906 /* Process all the DIES contained within a lexical block scope. Start
13907 a new scope, process the dies, and then close the scope. */
13909 static void
13910 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13914 CORE_ADDR lowpc, highpc;
13915 struct die_info *child_die;
13916 CORE_ADDR baseaddr;
13918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13920 /* Ignore blocks with missing or invalid low and high pc attributes. */
13921 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13922 as multiple lexical blocks? Handling children in a sane way would
13923 be nasty. Might be easier to properly extend generic blocks to
13924 describe ranges. */
13925 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13927 case PC_BOUNDS_NOT_PRESENT:
13928 /* DW_TAG_lexical_block has no attributes, process its children as if
13929 there was no wrapping by that DW_TAG_lexical_block.
13930 GCC does no longer produces such DWARF since GCC r224161. */
13931 for (child_die = die->child;
13932 child_die != NULL && child_die->tag;
13933 child_die = sibling_die (child_die))
13934 process_die (child_die, cu);
13935 return;
13936 case PC_BOUNDS_INVALID:
13937 return;
13939 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13940 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13942 cu->get_builder ()->push_context (0, lowpc);
13943 if (die->child != NULL)
13945 child_die = die->child;
13946 while (child_die && child_die->tag)
13948 process_die (child_die, cu);
13949 child_die = sibling_die (child_die);
13952 inherit_abstract_dies (die, cu);
13953 struct context_stack cstk = cu->get_builder ()->pop_context ();
13955 if (*cu->get_builder ()->get_local_symbols () != NULL
13956 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13958 struct block *block
13959 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13960 cstk.start_addr, highpc);
13962 /* Note that recording ranges after traversing children, as we
13963 do here, means that recording a parent's ranges entails
13964 walking across all its children's ranges as they appear in
13965 the address map, which is quadratic behavior.
13967 It would be nicer to record the parent's ranges before
13968 traversing its children, simply overriding whatever you find
13969 there. But since we don't even decide whether to create a
13970 block until after we've traversed its children, that's hard
13971 to do. */
13972 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13974 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13975 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13978 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13980 static void
13981 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13984 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13985 CORE_ADDR pc, baseaddr;
13986 struct attribute *attr;
13987 struct call_site *call_site, call_site_local;
13988 void **slot;
13989 int nparams;
13990 struct die_info *child_die;
13992 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13994 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13995 if (attr == NULL)
13997 /* This was a pre-DWARF-5 GNU extension alias
13998 for DW_AT_call_return_pc. */
13999 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14001 if (!attr)
14003 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
14004 "DIE %s [in module %s]"),
14005 sect_offset_str (die->sect_off), objfile_name (objfile));
14006 return;
14008 pc = attr_value_as_address (attr) + baseaddr;
14009 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14011 if (cu->call_site_htab == NULL)
14012 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14013 NULL, &objfile->objfile_obstack,
14014 hashtab_obstack_allocate, NULL);
14015 call_site_local.pc = pc;
14016 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14017 if (*slot != NULL)
14019 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14020 "DIE %s [in module %s]"),
14021 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14022 objfile_name (objfile));
14023 return;
14026 /* Count parameters at the caller. */
14028 nparams = 0;
14029 for (child_die = die->child; child_die && child_die->tag;
14030 child_die = sibling_die (child_die))
14032 if (child_die->tag != DW_TAG_call_site_parameter
14033 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14035 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14036 "DW_TAG_call_site child DIE %s [in module %s]"),
14037 child_die->tag, sect_offset_str (child_die->sect_off),
14038 objfile_name (objfile));
14039 continue;
14042 nparams++;
14045 call_site
14046 = ((struct call_site *)
14047 obstack_alloc (&objfile->objfile_obstack,
14048 sizeof (*call_site)
14049 + (sizeof (*call_site->parameter) * (nparams - 1))));
14050 *slot = call_site;
14051 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14052 call_site->pc = pc;
14054 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14055 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14057 struct die_info *func_die;
14059 /* Skip also over DW_TAG_inlined_subroutine. */
14060 for (func_die = die->parent;
14061 func_die && func_die->tag != DW_TAG_subprogram
14062 && func_die->tag != DW_TAG_subroutine_type;
14063 func_die = func_die->parent);
14065 /* DW_AT_call_all_calls is a superset
14066 of DW_AT_call_all_tail_calls. */
14067 if (func_die
14068 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14069 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14070 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14071 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14073 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14074 not complete. But keep CALL_SITE for look ups via call_site_htab,
14075 both the initial caller containing the real return address PC and
14076 the final callee containing the current PC of a chain of tail
14077 calls do not need to have the tail call list complete. But any
14078 function candidate for a virtual tail call frame searched via
14079 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14080 determined unambiguously. */
14082 else
14084 struct type *func_type = NULL;
14086 if (func_die)
14087 func_type = get_die_type (func_die, cu);
14088 if (func_type != NULL)
14090 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14092 /* Enlist this call site to the function. */
14093 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14094 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14096 else
14097 complaint (_("Cannot find function owning DW_TAG_call_site "
14098 "DIE %s [in module %s]"),
14099 sect_offset_str (die->sect_off), objfile_name (objfile));
14103 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14104 if (attr == NULL)
14105 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14106 if (attr == NULL)
14107 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14108 if (attr == NULL)
14110 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14111 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14113 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14114 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14115 /* Keep NULL DWARF_BLOCK. */;
14116 else if (attr_form_is_block (attr))
14118 struct dwarf2_locexpr_baton *dlbaton;
14120 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14121 dlbaton->data = DW_BLOCK (attr)->data;
14122 dlbaton->size = DW_BLOCK (attr)->size;
14123 dlbaton->per_cu = cu->per_cu;
14125 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14127 else if (attr_form_is_ref (attr))
14129 struct dwarf2_cu *target_cu = cu;
14130 struct die_info *target_die;
14132 target_die = follow_die_ref (die, attr, &target_cu);
14133 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14134 if (die_is_declaration (target_die, target_cu))
14136 const char *target_physname;
14138 /* Prefer the mangled name; otherwise compute the demangled one. */
14139 target_physname = dw2_linkage_name (target_die, target_cu);
14140 if (target_physname == NULL)
14141 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14142 if (target_physname == NULL)
14143 complaint (_("DW_AT_call_target target DIE has invalid "
14144 "physname, for referencing DIE %s [in module %s]"),
14145 sect_offset_str (die->sect_off), objfile_name (objfile));
14146 else
14147 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14149 else
14151 CORE_ADDR lowpc;
14153 /* DW_AT_entry_pc should be preferred. */
14154 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14155 <= PC_BOUNDS_INVALID)
14156 complaint (_("DW_AT_call_target target DIE has invalid "
14157 "low pc, for referencing DIE %s [in module %s]"),
14158 sect_offset_str (die->sect_off), objfile_name (objfile));
14159 else
14161 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14162 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14166 else
14167 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14168 "block nor reference, for DIE %s [in module %s]"),
14169 sect_offset_str (die->sect_off), objfile_name (objfile));
14171 call_site->per_cu = cu->per_cu;
14173 for (child_die = die->child;
14174 child_die && child_die->tag;
14175 child_die = sibling_die (child_die))
14177 struct call_site_parameter *parameter;
14178 struct attribute *loc, *origin;
14180 if (child_die->tag != DW_TAG_call_site_parameter
14181 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14183 /* Already printed the complaint above. */
14184 continue;
14187 gdb_assert (call_site->parameter_count < nparams);
14188 parameter = &call_site->parameter[call_site->parameter_count];
14190 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14191 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14192 register is contained in DW_AT_call_value. */
14194 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14195 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14196 if (origin == NULL)
14198 /* This was a pre-DWARF-5 GNU extension alias
14199 for DW_AT_call_parameter. */
14200 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14202 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14204 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14206 sect_offset sect_off
14207 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14208 if (!offset_in_cu_p (&cu->header, sect_off))
14210 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14211 binding can be done only inside one CU. Such referenced DIE
14212 therefore cannot be even moved to DW_TAG_partial_unit. */
14213 complaint (_("DW_AT_call_parameter offset is not in CU for "
14214 "DW_TAG_call_site child DIE %s [in module %s]"),
14215 sect_offset_str (child_die->sect_off),
14216 objfile_name (objfile));
14217 continue;
14219 parameter->u.param_cu_off
14220 = (cu_offset) (sect_off - cu->header.sect_off);
14222 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14224 complaint (_("No DW_FORM_block* DW_AT_location for "
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14227 continue;
14229 else
14231 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14232 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14233 if (parameter->u.dwarf_reg != -1)
14234 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14235 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14236 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14237 &parameter->u.fb_offset))
14238 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14239 else
14241 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14242 "for DW_FORM_block* DW_AT_location is supported for "
14243 "DW_TAG_call_site child DIE %s "
14244 "[in module %s]"),
14245 sect_offset_str (child_die->sect_off),
14246 objfile_name (objfile));
14247 continue;
14251 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14252 if (attr == NULL)
14253 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14254 if (!attr_form_is_block (attr))
14256 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14257 "DW_TAG_call_site child DIE %s [in module %s]"),
14258 sect_offset_str (child_die->sect_off),
14259 objfile_name (objfile));
14260 continue;
14262 parameter->value = DW_BLOCK (attr)->data;
14263 parameter->value_size = DW_BLOCK (attr)->size;
14265 /* Parameters are not pre-cleared by memset above. */
14266 parameter->data_value = NULL;
14267 parameter->data_value_size = 0;
14268 call_site->parameter_count++;
14270 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14271 if (attr == NULL)
14272 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14273 if (attr != nullptr)
14275 if (!attr_form_is_block (attr))
14276 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14277 "DW_TAG_call_site child DIE %s [in module %s]"),
14278 sect_offset_str (child_die->sect_off),
14279 objfile_name (objfile));
14280 else
14282 parameter->data_value = DW_BLOCK (attr)->data;
14283 parameter->data_value_size = DW_BLOCK (attr)->size;
14289 /* Helper function for read_variable. If DIE represents a virtual
14290 table, then return the type of the concrete object that is
14291 associated with the virtual table. Otherwise, return NULL. */
14293 static struct type *
14294 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14296 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14297 if (attr == NULL)
14298 return NULL;
14300 /* Find the type DIE. */
14301 struct die_info *type_die = NULL;
14302 struct dwarf2_cu *type_cu = cu;
14304 if (attr_form_is_ref (attr))
14305 type_die = follow_die_ref (die, attr, &type_cu);
14306 if (type_die == NULL)
14307 return NULL;
14309 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14310 return NULL;
14311 return die_containing_type (type_die, type_cu);
14314 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14316 static void
14317 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14319 struct rust_vtable_symbol *storage = NULL;
14321 if (cu->language == language_rust)
14323 struct type *containing_type = rust_containing_type (die, cu);
14325 if (containing_type != NULL)
14327 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14329 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14330 initialize_objfile_symbol (storage);
14331 storage->concrete_type = containing_type;
14332 storage->subclass = SYMBOL_RUST_VTABLE;
14336 struct symbol *res = new_symbol (die, NULL, cu, storage);
14337 struct attribute *abstract_origin
14338 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14339 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14340 if (res == NULL && loc && abstract_origin)
14342 /* We have a variable without a name, but with a location and an abstract
14343 origin. This may be a concrete instance of an abstract variable
14344 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14345 later. */
14346 struct dwarf2_cu *origin_cu = cu;
14347 struct die_info *origin_die
14348 = follow_die_ref (die, abstract_origin, &origin_cu);
14349 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14350 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14354 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14355 reading .debug_rnglists.
14356 Callback's type should be:
14357 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14358 Return true if the attributes are present and valid, otherwise,
14359 return false. */
14361 template <typename Callback>
14362 static bool
14363 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14364 Callback &&callback)
14366 struct dwarf2_per_objfile *dwarf2_per_objfile
14367 = cu->per_cu->dwarf2_per_objfile;
14368 struct objfile *objfile = dwarf2_per_objfile->objfile;
14369 bfd *obfd = objfile->obfd;
14370 /* Base address selection entry. */
14371 CORE_ADDR base;
14372 int found_base;
14373 const gdb_byte *buffer;
14374 CORE_ADDR baseaddr;
14375 bool overflow = false;
14377 found_base = cu->base_known;
14378 base = cu->base_address;
14380 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14381 if (offset >= dwarf2_per_objfile->rnglists.size)
14383 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14384 offset);
14385 return false;
14387 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14389 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14391 while (1)
14393 /* Initialize it due to a false compiler warning. */
14394 CORE_ADDR range_beginning = 0, range_end = 0;
14395 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14396 + dwarf2_per_objfile->rnglists.size);
14397 unsigned int bytes_read;
14399 if (buffer == buf_end)
14401 overflow = true;
14402 break;
14404 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14405 switch (rlet)
14407 case DW_RLE_end_of_list:
14408 break;
14409 case DW_RLE_base_address:
14410 if (buffer + cu->header.addr_size > buf_end)
14412 overflow = true;
14413 break;
14415 base = read_address (obfd, buffer, cu, &bytes_read);
14416 found_base = 1;
14417 buffer += bytes_read;
14418 break;
14419 case DW_RLE_start_length:
14420 if (buffer + cu->header.addr_size > buf_end)
14422 overflow = true;
14423 break;
14425 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14426 buffer += bytes_read;
14427 range_end = (range_beginning
14428 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14429 buffer += bytes_read;
14430 if (buffer > buf_end)
14432 overflow = true;
14433 break;
14435 break;
14436 case DW_RLE_offset_pair:
14437 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14438 buffer += bytes_read;
14439 if (buffer > buf_end)
14441 overflow = true;
14442 break;
14444 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14445 buffer += bytes_read;
14446 if (buffer > buf_end)
14448 overflow = true;
14449 break;
14451 break;
14452 case DW_RLE_start_end:
14453 if (buffer + 2 * cu->header.addr_size > buf_end)
14455 overflow = true;
14456 break;
14458 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14459 buffer += bytes_read;
14460 range_end = read_address (obfd, buffer, cu, &bytes_read);
14461 buffer += bytes_read;
14462 break;
14463 default:
14464 complaint (_("Invalid .debug_rnglists data (no base address)"));
14465 return false;
14467 if (rlet == DW_RLE_end_of_list || overflow)
14468 break;
14469 if (rlet == DW_RLE_base_address)
14470 continue;
14472 if (!found_base)
14474 /* We have no valid base address for the ranges
14475 data. */
14476 complaint (_("Invalid .debug_rnglists data (no base address)"));
14477 return false;
14480 if (range_beginning > range_end)
14482 /* Inverted range entries are invalid. */
14483 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14484 return false;
14487 /* Empty range entries have no effect. */
14488 if (range_beginning == range_end)
14489 continue;
14491 range_beginning += base;
14492 range_end += base;
14494 /* A not-uncommon case of bad debug info.
14495 Don't pollute the addrmap with bad data. */
14496 if (range_beginning + baseaddr == 0
14497 && !dwarf2_per_objfile->has_section_at_zero)
14499 complaint (_(".debug_rnglists entry has start address of zero"
14500 " [in module %s]"), objfile_name (objfile));
14501 continue;
14504 callback (range_beginning, range_end);
14507 if (overflow)
14509 complaint (_("Offset %d is not terminated "
14510 "for DW_AT_ranges attribute"),
14511 offset);
14512 return false;
14515 return true;
14518 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14519 Callback's type should be:
14520 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14521 Return 1 if the attributes are present and valid, otherwise, return 0. */
14523 template <typename Callback>
14524 static int
14525 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14526 Callback &&callback)
14528 struct dwarf2_per_objfile *dwarf2_per_objfile
14529 = cu->per_cu->dwarf2_per_objfile;
14530 struct objfile *objfile = dwarf2_per_objfile->objfile;
14531 struct comp_unit_head *cu_header = &cu->header;
14532 bfd *obfd = objfile->obfd;
14533 unsigned int addr_size = cu_header->addr_size;
14534 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14535 /* Base address selection entry. */
14536 CORE_ADDR base;
14537 int found_base;
14538 unsigned int dummy;
14539 const gdb_byte *buffer;
14540 CORE_ADDR baseaddr;
14542 if (cu_header->version >= 5)
14543 return dwarf2_rnglists_process (offset, cu, callback);
14545 found_base = cu->base_known;
14546 base = cu->base_address;
14548 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14549 if (offset >= dwarf2_per_objfile->ranges.size)
14551 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14552 offset);
14553 return 0;
14555 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14559 while (1)
14561 CORE_ADDR range_beginning, range_end;
14563 range_beginning = read_address (obfd, buffer, cu, &dummy);
14564 buffer += addr_size;
14565 range_end = read_address (obfd, buffer, cu, &dummy);
14566 buffer += addr_size;
14567 offset += 2 * addr_size;
14569 /* An end of list marker is a pair of zero addresses. */
14570 if (range_beginning == 0 && range_end == 0)
14571 /* Found the end of list entry. */
14572 break;
14574 /* Each base address selection entry is a pair of 2 values.
14575 The first is the largest possible address, the second is
14576 the base address. Check for a base address here. */
14577 if ((range_beginning & mask) == mask)
14579 /* If we found the largest possible address, then we already
14580 have the base address in range_end. */
14581 base = range_end;
14582 found_base = 1;
14583 continue;
14586 if (!found_base)
14588 /* We have no valid base address for the ranges
14589 data. */
14590 complaint (_("Invalid .debug_ranges data (no base address)"));
14591 return 0;
14594 if (range_beginning > range_end)
14596 /* Inverted range entries are invalid. */
14597 complaint (_("Invalid .debug_ranges data (inverted range)"));
14598 return 0;
14601 /* Empty range entries have no effect. */
14602 if (range_beginning == range_end)
14603 continue;
14605 range_beginning += base;
14606 range_end += base;
14608 /* A not-uncommon case of bad debug info.
14609 Don't pollute the addrmap with bad data. */
14610 if (range_beginning + baseaddr == 0
14611 && !dwarf2_per_objfile->has_section_at_zero)
14613 complaint (_(".debug_ranges entry has start address of zero"
14614 " [in module %s]"), objfile_name (objfile));
14615 continue;
14618 callback (range_beginning, range_end);
14621 return 1;
14624 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14625 Return 1 if the attributes are present and valid, otherwise, return 0.
14626 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14628 static int
14629 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14630 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14631 struct partial_symtab *ranges_pst)
14633 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14634 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14635 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14636 SECT_OFF_TEXT (objfile));
14637 int low_set = 0;
14638 CORE_ADDR low = 0;
14639 CORE_ADDR high = 0;
14640 int retval;
14642 retval = dwarf2_ranges_process (offset, cu,
14643 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14645 if (ranges_pst != NULL)
14647 CORE_ADDR lowpc;
14648 CORE_ADDR highpc;
14650 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14651 range_beginning + baseaddr)
14652 - baseaddr);
14653 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14654 range_end + baseaddr)
14655 - baseaddr);
14656 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14657 lowpc, highpc - 1, ranges_pst);
14660 /* FIXME: This is recording everything as a low-high
14661 segment of consecutive addresses. We should have a
14662 data structure for discontiguous block ranges
14663 instead. */
14664 if (! low_set)
14666 low = range_beginning;
14667 high = range_end;
14668 low_set = 1;
14670 else
14672 if (range_beginning < low)
14673 low = range_beginning;
14674 if (range_end > high)
14675 high = range_end;
14678 if (!retval)
14679 return 0;
14681 if (! low_set)
14682 /* If the first entry is an end-of-list marker, the range
14683 describes an empty scope, i.e. no instructions. */
14684 return 0;
14686 if (low_return)
14687 *low_return = low;
14688 if (high_return)
14689 *high_return = high;
14690 return 1;
14693 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14694 definition for the return value. *LOWPC and *HIGHPC are set iff
14695 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14697 static enum pc_bounds_kind
14698 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14699 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14700 struct partial_symtab *pst)
14702 struct dwarf2_per_objfile *dwarf2_per_objfile
14703 = cu->per_cu->dwarf2_per_objfile;
14704 struct attribute *attr;
14705 struct attribute *attr_high;
14706 CORE_ADDR low = 0;
14707 CORE_ADDR high = 0;
14708 enum pc_bounds_kind ret;
14710 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14711 if (attr_high)
14713 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14714 if (attr != nullptr)
14716 low = attr_value_as_address (attr);
14717 high = attr_value_as_address (attr_high);
14718 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14719 high += low;
14721 else
14722 /* Found high w/o low attribute. */
14723 return PC_BOUNDS_INVALID;
14725 /* Found consecutive range of addresses. */
14726 ret = PC_BOUNDS_HIGH_LOW;
14728 else
14730 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14731 if (attr != NULL)
14733 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14734 We take advantage of the fact that DW_AT_ranges does not appear
14735 in DW_TAG_compile_unit of DWO files. */
14736 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14737 unsigned int ranges_offset = (DW_UNSND (attr)
14738 + (need_ranges_base
14739 ? cu->ranges_base
14740 : 0));
14742 /* Value of the DW_AT_ranges attribute is the offset in the
14743 .debug_ranges section. */
14744 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14745 return PC_BOUNDS_INVALID;
14746 /* Found discontinuous range of addresses. */
14747 ret = PC_BOUNDS_RANGES;
14749 else
14750 return PC_BOUNDS_NOT_PRESENT;
14753 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14754 if (high <= low)
14755 return PC_BOUNDS_INVALID;
14757 /* When using the GNU linker, .gnu.linkonce. sections are used to
14758 eliminate duplicate copies of functions and vtables and such.
14759 The linker will arbitrarily choose one and discard the others.
14760 The AT_*_pc values for such functions refer to local labels in
14761 these sections. If the section from that file was discarded, the
14762 labels are not in the output, so the relocs get a value of 0.
14763 If this is a discarded function, mark the pc bounds as invalid,
14764 so that GDB will ignore it. */
14765 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14766 return PC_BOUNDS_INVALID;
14768 *lowpc = low;
14769 if (highpc)
14770 *highpc = high;
14771 return ret;
14774 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14775 its low and high PC addresses. Do nothing if these addresses could not
14776 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14777 and HIGHPC to the high address if greater than HIGHPC. */
14779 static void
14780 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14781 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14782 struct dwarf2_cu *cu)
14784 CORE_ADDR low, high;
14785 struct die_info *child = die->child;
14787 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14789 *lowpc = std::min (*lowpc, low);
14790 *highpc = std::max (*highpc, high);
14793 /* If the language does not allow nested subprograms (either inside
14794 subprograms or lexical blocks), we're done. */
14795 if (cu->language != language_ada)
14796 return;
14798 /* Check all the children of the given DIE. If it contains nested
14799 subprograms, then check their pc bounds. Likewise, we need to
14800 check lexical blocks as well, as they may also contain subprogram
14801 definitions. */
14802 while (child && child->tag)
14804 if (child->tag == DW_TAG_subprogram
14805 || child->tag == DW_TAG_lexical_block)
14806 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14807 child = sibling_die (child);
14811 /* Get the low and high pc's represented by the scope DIE, and store
14812 them in *LOWPC and *HIGHPC. If the correct values can't be
14813 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14815 static void
14816 get_scope_pc_bounds (struct die_info *die,
14817 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14818 struct dwarf2_cu *cu)
14820 CORE_ADDR best_low = (CORE_ADDR) -1;
14821 CORE_ADDR best_high = (CORE_ADDR) 0;
14822 CORE_ADDR current_low, current_high;
14824 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14825 >= PC_BOUNDS_RANGES)
14827 best_low = current_low;
14828 best_high = current_high;
14830 else
14832 struct die_info *child = die->child;
14834 while (child && child->tag)
14836 switch (child->tag) {
14837 case DW_TAG_subprogram:
14838 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14839 break;
14840 case DW_TAG_namespace:
14841 case DW_TAG_module:
14842 /* FIXME: carlton/2004-01-16: Should we do this for
14843 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14844 that current GCC's always emit the DIEs corresponding
14845 to definitions of methods of classes as children of a
14846 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14847 the DIEs giving the declarations, which could be
14848 anywhere). But I don't see any reason why the
14849 standards says that they have to be there. */
14850 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14852 if (current_low != ((CORE_ADDR) -1))
14854 best_low = std::min (best_low, current_low);
14855 best_high = std::max (best_high, current_high);
14857 break;
14858 default:
14859 /* Ignore. */
14860 break;
14863 child = sibling_die (child);
14867 *lowpc = best_low;
14868 *highpc = best_high;
14871 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14872 in DIE. */
14874 static void
14875 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14876 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14878 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14880 struct attribute *attr;
14881 struct attribute *attr_high;
14883 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14884 if (attr_high)
14886 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14887 if (attr != nullptr)
14889 CORE_ADDR low = attr_value_as_address (attr);
14890 CORE_ADDR high = attr_value_as_address (attr_high);
14892 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14893 high += low;
14895 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14896 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14897 cu->get_builder ()->record_block_range (block, low, high - 1);
14901 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14902 if (attr != nullptr)
14904 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14905 We take advantage of the fact that DW_AT_ranges does not appear
14906 in DW_TAG_compile_unit of DWO files. */
14907 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14909 /* The value of the DW_AT_ranges attribute is the offset of the
14910 address range list in the .debug_ranges section. */
14911 unsigned long offset = (DW_UNSND (attr)
14912 + (need_ranges_base ? cu->ranges_base : 0));
14914 std::vector<blockrange> blockvec;
14915 dwarf2_ranges_process (offset, cu,
14916 [&] (CORE_ADDR start, CORE_ADDR end)
14918 start += baseaddr;
14919 end += baseaddr;
14920 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14921 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14922 cu->get_builder ()->record_block_range (block, start, end - 1);
14923 blockvec.emplace_back (start, end);
14926 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14930 /* Check whether the producer field indicates either of GCC < 4.6, or the
14931 Intel C/C++ compiler, and cache the result in CU. */
14933 static void
14934 check_producer (struct dwarf2_cu *cu)
14936 int major, minor;
14938 if (cu->producer == NULL)
14940 /* For unknown compilers expect their behavior is DWARF version
14941 compliant.
14943 GCC started to support .debug_types sections by -gdwarf-4 since
14944 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14945 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14946 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14947 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14949 else if (producer_is_gcc (cu->producer, &major, &minor))
14951 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14952 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14954 else if (producer_is_icc (cu->producer, &major, &minor))
14956 cu->producer_is_icc = true;
14957 cu->producer_is_icc_lt_14 = major < 14;
14959 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14960 cu->producer_is_codewarrior = true;
14961 else
14963 /* For other non-GCC compilers, expect their behavior is DWARF version
14964 compliant. */
14967 cu->checked_producer = true;
14970 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14971 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14972 during 4.6.0 experimental. */
14974 static bool
14975 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14977 if (!cu->checked_producer)
14978 check_producer (cu);
14980 return cu->producer_is_gxx_lt_4_6;
14984 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14985 with incorrect is_stmt attributes. */
14987 static bool
14988 producer_is_codewarrior (struct dwarf2_cu *cu)
14990 if (!cu->checked_producer)
14991 check_producer (cu);
14993 return cu->producer_is_codewarrior;
14996 /* Return the default accessibility type if it is not overridden by
14997 DW_AT_accessibility. */
14999 static enum dwarf_access_attribute
15000 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15002 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15004 /* The default DWARF 2 accessibility for members is public, the default
15005 accessibility for inheritance is private. */
15007 if (die->tag != DW_TAG_inheritance)
15008 return DW_ACCESS_public;
15009 else
15010 return DW_ACCESS_private;
15012 else
15014 /* DWARF 3+ defines the default accessibility a different way. The same
15015 rules apply now for DW_TAG_inheritance as for the members and it only
15016 depends on the container kind. */
15018 if (die->parent->tag == DW_TAG_class_type)
15019 return DW_ACCESS_private;
15020 else
15021 return DW_ACCESS_public;
15025 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15026 offset. If the attribute was not found return 0, otherwise return
15027 1. If it was found but could not properly be handled, set *OFFSET
15028 to 0. */
15030 static int
15031 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15032 LONGEST *offset)
15034 struct attribute *attr;
15036 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15037 if (attr != NULL)
15039 *offset = 0;
15041 /* Note that we do not check for a section offset first here.
15042 This is because DW_AT_data_member_location is new in DWARF 4,
15043 so if we see it, we can assume that a constant form is really
15044 a constant and not a section offset. */
15045 if (attr_form_is_constant (attr))
15046 *offset = dwarf2_get_attr_constant_value (attr, 0);
15047 else if (attr_form_is_section_offset (attr))
15048 dwarf2_complex_location_expr_complaint ();
15049 else if (attr_form_is_block (attr))
15050 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15051 else
15052 dwarf2_complex_location_expr_complaint ();
15054 return 1;
15057 return 0;
15060 /* Add an aggregate field to the field list. */
15062 static void
15063 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15064 struct dwarf2_cu *cu)
15066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15068 struct nextfield *new_field;
15069 struct attribute *attr;
15070 struct field *fp;
15071 const char *fieldname = "";
15073 if (die->tag == DW_TAG_inheritance)
15075 fip->baseclasses.emplace_back ();
15076 new_field = &fip->baseclasses.back ();
15078 else
15080 fip->fields.emplace_back ();
15081 new_field = &fip->fields.back ();
15084 fip->nfields++;
15086 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15087 if (attr != nullptr)
15088 new_field->accessibility = DW_UNSND (attr);
15089 else
15090 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15091 if (new_field->accessibility != DW_ACCESS_public)
15092 fip->non_public_fields = 1;
15094 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15095 if (attr != nullptr)
15096 new_field->virtuality = DW_UNSND (attr);
15097 else
15098 new_field->virtuality = DW_VIRTUALITY_none;
15100 fp = &new_field->field;
15102 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15104 LONGEST offset;
15106 /* Data member other than a C++ static data member. */
15108 /* Get type of field. */
15109 fp->type = die_type (die, cu);
15111 SET_FIELD_BITPOS (*fp, 0);
15113 /* Get bit size of field (zero if none). */
15114 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15115 if (attr != nullptr)
15117 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15119 else
15121 FIELD_BITSIZE (*fp) = 0;
15124 /* Get bit offset of field. */
15125 if (handle_data_member_location (die, cu, &offset))
15126 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15127 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15128 if (attr != nullptr)
15130 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15132 /* For big endian bits, the DW_AT_bit_offset gives the
15133 additional bit offset from the MSB of the containing
15134 anonymous object to the MSB of the field. We don't
15135 have to do anything special since we don't need to
15136 know the size of the anonymous object. */
15137 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15139 else
15141 /* For little endian bits, compute the bit offset to the
15142 MSB of the anonymous object, subtract off the number of
15143 bits from the MSB of the field to the MSB of the
15144 object, and then subtract off the number of bits of
15145 the field itself. The result is the bit offset of
15146 the LSB of the field. */
15147 int anonymous_size;
15148 int bit_offset = DW_UNSND (attr);
15150 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15151 if (attr != nullptr)
15153 /* The size of the anonymous object containing
15154 the bit field is explicit, so use the
15155 indicated size (in bytes). */
15156 anonymous_size = DW_UNSND (attr);
15158 else
15160 /* The size of the anonymous object containing
15161 the bit field must be inferred from the type
15162 attribute of the data member containing the
15163 bit field. */
15164 anonymous_size = TYPE_LENGTH (fp->type);
15166 SET_FIELD_BITPOS (*fp,
15167 (FIELD_BITPOS (*fp)
15168 + anonymous_size * bits_per_byte
15169 - bit_offset - FIELD_BITSIZE (*fp)));
15172 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15173 if (attr != NULL)
15174 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15175 + dwarf2_get_attr_constant_value (attr, 0)));
15177 /* Get name of field. */
15178 fieldname = dwarf2_name (die, cu);
15179 if (fieldname == NULL)
15180 fieldname = "";
15182 /* The name is already allocated along with this objfile, so we don't
15183 need to duplicate it for the type. */
15184 fp->name = fieldname;
15186 /* Change accessibility for artificial fields (e.g. virtual table
15187 pointer or virtual base class pointer) to private. */
15188 if (dwarf2_attr (die, DW_AT_artificial, cu))
15190 FIELD_ARTIFICIAL (*fp) = 1;
15191 new_field->accessibility = DW_ACCESS_private;
15192 fip->non_public_fields = 1;
15195 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15197 /* C++ static member. */
15199 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15200 is a declaration, but all versions of G++ as of this writing
15201 (so through at least 3.2.1) incorrectly generate
15202 DW_TAG_variable tags. */
15204 const char *physname;
15206 /* Get name of field. */
15207 fieldname = dwarf2_name (die, cu);
15208 if (fieldname == NULL)
15209 return;
15211 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15212 if (attr
15213 /* Only create a symbol if this is an external value.
15214 new_symbol checks this and puts the value in the global symbol
15215 table, which we want. If it is not external, new_symbol
15216 will try to put the value in cu->list_in_scope which is wrong. */
15217 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15219 /* A static const member, not much different than an enum as far as
15220 we're concerned, except that we can support more types. */
15221 new_symbol (die, NULL, cu);
15224 /* Get physical name. */
15225 physname = dwarf2_physname (fieldname, die, cu);
15227 /* The name is already allocated along with this objfile, so we don't
15228 need to duplicate it for the type. */
15229 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15230 FIELD_TYPE (*fp) = die_type (die, cu);
15231 FIELD_NAME (*fp) = fieldname;
15233 else if (die->tag == DW_TAG_inheritance)
15235 LONGEST offset;
15237 /* C++ base class field. */
15238 if (handle_data_member_location (die, cu, &offset))
15239 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15240 FIELD_BITSIZE (*fp) = 0;
15241 FIELD_TYPE (*fp) = die_type (die, cu);
15242 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15244 else if (die->tag == DW_TAG_variant_part)
15246 /* process_structure_scope will treat this DIE as a union. */
15247 process_structure_scope (die, cu);
15249 /* The variant part is relative to the start of the enclosing
15250 structure. */
15251 SET_FIELD_BITPOS (*fp, 0);
15252 fp->type = get_die_type (die, cu);
15253 fp->artificial = 1;
15254 fp->name = "<<variant>>";
15256 /* Normally a DW_TAG_variant_part won't have a size, but our
15257 representation requires one, so set it to the maximum of the
15258 child sizes, being sure to account for the offset at which
15259 each child is seen. */
15260 if (TYPE_LENGTH (fp->type) == 0)
15262 unsigned max = 0;
15263 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15265 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15266 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15267 if (len > max)
15268 max = len;
15270 TYPE_LENGTH (fp->type) = max;
15273 else
15274 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15277 /* Can the type given by DIE define another type? */
15279 static bool
15280 type_can_define_types (const struct die_info *die)
15282 switch (die->tag)
15284 case DW_TAG_typedef:
15285 case DW_TAG_class_type:
15286 case DW_TAG_structure_type:
15287 case DW_TAG_union_type:
15288 case DW_TAG_enumeration_type:
15289 return true;
15291 default:
15292 return false;
15296 /* Add a type definition defined in the scope of the FIP's class. */
15298 static void
15299 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15300 struct dwarf2_cu *cu)
15302 struct decl_field fp;
15303 memset (&fp, 0, sizeof (fp));
15305 gdb_assert (type_can_define_types (die));
15307 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15308 fp.name = dwarf2_name (die, cu);
15309 fp.type = read_type_die (die, cu);
15311 /* Save accessibility. */
15312 enum dwarf_access_attribute accessibility;
15313 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15314 if (attr != NULL)
15315 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15316 else
15317 accessibility = dwarf2_default_access_attribute (die, cu);
15318 switch (accessibility)
15320 case DW_ACCESS_public:
15321 /* The assumed value if neither private nor protected. */
15322 break;
15323 case DW_ACCESS_private:
15324 fp.is_private = 1;
15325 break;
15326 case DW_ACCESS_protected:
15327 fp.is_protected = 1;
15328 break;
15329 default:
15330 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15333 if (die->tag == DW_TAG_typedef)
15334 fip->typedef_field_list.push_back (fp);
15335 else
15336 fip->nested_types_list.push_back (fp);
15339 /* Create the vector of fields, and attach it to the type. */
15341 static void
15342 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15343 struct dwarf2_cu *cu)
15345 int nfields = fip->nfields;
15347 /* Record the field count, allocate space for the array of fields,
15348 and create blank accessibility bitfields if necessary. */
15349 TYPE_NFIELDS (type) = nfields;
15350 TYPE_FIELDS (type) = (struct field *)
15351 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15353 if (fip->non_public_fields && cu->language != language_ada)
15355 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15357 TYPE_FIELD_PRIVATE_BITS (type) =
15358 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15359 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15361 TYPE_FIELD_PROTECTED_BITS (type) =
15362 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15363 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15365 TYPE_FIELD_IGNORE_BITS (type) =
15366 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15367 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15370 /* If the type has baseclasses, allocate and clear a bit vector for
15371 TYPE_FIELD_VIRTUAL_BITS. */
15372 if (!fip->baseclasses.empty () && cu->language != language_ada)
15374 int num_bytes = B_BYTES (fip->baseclasses.size ());
15375 unsigned char *pointer;
15377 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15378 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15379 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15380 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15381 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15384 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15386 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15388 for (int index = 0; index < nfields; ++index)
15390 struct nextfield &field = fip->fields[index];
15392 if (field.variant.is_discriminant)
15393 di->discriminant_index = index;
15394 else if (field.variant.default_branch)
15395 di->default_index = index;
15396 else
15397 di->discriminants[index] = field.variant.discriminant_value;
15401 /* Copy the saved-up fields into the field vector. */
15402 for (int i = 0; i < nfields; ++i)
15404 struct nextfield &field
15405 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15406 : fip->fields[i - fip->baseclasses.size ()]);
15408 TYPE_FIELD (type, i) = field.field;
15409 switch (field.accessibility)
15411 case DW_ACCESS_private:
15412 if (cu->language != language_ada)
15413 SET_TYPE_FIELD_PRIVATE (type, i);
15414 break;
15416 case DW_ACCESS_protected:
15417 if (cu->language != language_ada)
15418 SET_TYPE_FIELD_PROTECTED (type, i);
15419 break;
15421 case DW_ACCESS_public:
15422 break;
15424 default:
15425 /* Unknown accessibility. Complain and treat it as public. */
15427 complaint (_("unsupported accessibility %d"),
15428 field.accessibility);
15430 break;
15432 if (i < fip->baseclasses.size ())
15434 switch (field.virtuality)
15436 case DW_VIRTUALITY_virtual:
15437 case DW_VIRTUALITY_pure_virtual:
15438 if (cu->language == language_ada)
15439 error (_("unexpected virtuality in component of Ada type"));
15440 SET_TYPE_FIELD_VIRTUAL (type, i);
15441 break;
15447 /* Return true if this member function is a constructor, false
15448 otherwise. */
15450 static int
15451 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15453 const char *fieldname;
15454 const char *type_name;
15455 int len;
15457 if (die->parent == NULL)
15458 return 0;
15460 if (die->parent->tag != DW_TAG_structure_type
15461 && die->parent->tag != DW_TAG_union_type
15462 && die->parent->tag != DW_TAG_class_type)
15463 return 0;
15465 fieldname = dwarf2_name (die, cu);
15466 type_name = dwarf2_name (die->parent, cu);
15467 if (fieldname == NULL || type_name == NULL)
15468 return 0;
15470 len = strlen (fieldname);
15471 return (strncmp (fieldname, type_name, len) == 0
15472 && (type_name[len] == '\0' || type_name[len] == '<'));
15475 /* Check if the given VALUE is a recognized enum
15476 dwarf_defaulted_attribute constant according to DWARF5 spec,
15477 Table 7.24. */
15479 static bool
15480 is_valid_DW_AT_defaulted (ULONGEST value)
15482 switch (value)
15484 case DW_DEFAULTED_no:
15485 case DW_DEFAULTED_in_class:
15486 case DW_DEFAULTED_out_of_class:
15487 return true;
15490 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15491 return false;
15494 /* Add a member function to the proper fieldlist. */
15496 static void
15497 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15498 struct type *type, struct dwarf2_cu *cu)
15500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15501 struct attribute *attr;
15502 int i;
15503 struct fnfieldlist *flp = nullptr;
15504 struct fn_field *fnp;
15505 const char *fieldname;
15506 struct type *this_type;
15507 enum dwarf_access_attribute accessibility;
15509 if (cu->language == language_ada)
15510 error (_("unexpected member function in Ada type"));
15512 /* Get name of member function. */
15513 fieldname = dwarf2_name (die, cu);
15514 if (fieldname == NULL)
15515 return;
15517 /* Look up member function name in fieldlist. */
15518 for (i = 0; i < fip->fnfieldlists.size (); i++)
15520 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15522 flp = &fip->fnfieldlists[i];
15523 break;
15527 /* Create a new fnfieldlist if necessary. */
15528 if (flp == nullptr)
15530 fip->fnfieldlists.emplace_back ();
15531 flp = &fip->fnfieldlists.back ();
15532 flp->name = fieldname;
15533 i = fip->fnfieldlists.size () - 1;
15536 /* Create a new member function field and add it to the vector of
15537 fnfieldlists. */
15538 flp->fnfields.emplace_back ();
15539 fnp = &flp->fnfields.back ();
15541 /* Delay processing of the physname until later. */
15542 if (cu->language == language_cplus)
15543 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15544 die, cu);
15545 else
15547 const char *physname = dwarf2_physname (fieldname, die, cu);
15548 fnp->physname = physname ? physname : "";
15551 fnp->type = alloc_type (objfile);
15552 this_type = read_type_die (die, cu);
15553 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15555 int nparams = TYPE_NFIELDS (this_type);
15557 /* TYPE is the domain of this method, and THIS_TYPE is the type
15558 of the method itself (TYPE_CODE_METHOD). */
15559 smash_to_method_type (fnp->type, type,
15560 TYPE_TARGET_TYPE (this_type),
15561 TYPE_FIELDS (this_type),
15562 TYPE_NFIELDS (this_type),
15563 TYPE_VARARGS (this_type));
15565 /* Handle static member functions.
15566 Dwarf2 has no clean way to discern C++ static and non-static
15567 member functions. G++ helps GDB by marking the first
15568 parameter for non-static member functions (which is the this
15569 pointer) as artificial. We obtain this information from
15570 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15571 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15572 fnp->voffset = VOFFSET_STATIC;
15574 else
15575 complaint (_("member function type missing for '%s'"),
15576 dwarf2_full_name (fieldname, die, cu));
15578 /* Get fcontext from DW_AT_containing_type if present. */
15579 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15580 fnp->fcontext = die_containing_type (die, cu);
15582 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15583 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15585 /* Get accessibility. */
15586 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15587 if (attr != nullptr)
15588 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15589 else
15590 accessibility = dwarf2_default_access_attribute (die, cu);
15591 switch (accessibility)
15593 case DW_ACCESS_private:
15594 fnp->is_private = 1;
15595 break;
15596 case DW_ACCESS_protected:
15597 fnp->is_protected = 1;
15598 break;
15601 /* Check for artificial methods. */
15602 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15603 if (attr && DW_UNSND (attr) != 0)
15604 fnp->is_artificial = 1;
15606 /* Check for defaulted methods. */
15607 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15608 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15609 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15611 /* Check for deleted methods. */
15612 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15613 if (attr != nullptr && DW_UNSND (attr) != 0)
15614 fnp->is_deleted = 1;
15616 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15618 /* Get index in virtual function table if it is a virtual member
15619 function. For older versions of GCC, this is an offset in the
15620 appropriate virtual table, as specified by DW_AT_containing_type.
15621 For everyone else, it is an expression to be evaluated relative
15622 to the object address. */
15624 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15625 if (attr != nullptr)
15627 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15629 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15631 /* Old-style GCC. */
15632 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15634 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15635 || (DW_BLOCK (attr)->size > 1
15636 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15637 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15639 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15640 if ((fnp->voffset % cu->header.addr_size) != 0)
15641 dwarf2_complex_location_expr_complaint ();
15642 else
15643 fnp->voffset /= cu->header.addr_size;
15644 fnp->voffset += 2;
15646 else
15647 dwarf2_complex_location_expr_complaint ();
15649 if (!fnp->fcontext)
15651 /* If there is no `this' field and no DW_AT_containing_type,
15652 we cannot actually find a base class context for the
15653 vtable! */
15654 if (TYPE_NFIELDS (this_type) == 0
15655 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15657 complaint (_("cannot determine context for virtual member "
15658 "function \"%s\" (offset %s)"),
15659 fieldname, sect_offset_str (die->sect_off));
15661 else
15663 fnp->fcontext
15664 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15668 else if (attr_form_is_section_offset (attr))
15670 dwarf2_complex_location_expr_complaint ();
15672 else
15674 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15675 fieldname);
15678 else
15680 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15681 if (attr && DW_UNSND (attr))
15683 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15684 complaint (_("Member function \"%s\" (offset %s) is virtual "
15685 "but the vtable offset is not specified"),
15686 fieldname, sect_offset_str (die->sect_off));
15687 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15688 TYPE_CPLUS_DYNAMIC (type) = 1;
15693 /* Create the vector of member function fields, and attach it to the type. */
15695 static void
15696 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15697 struct dwarf2_cu *cu)
15699 if (cu->language == language_ada)
15700 error (_("unexpected member functions in Ada type"));
15702 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15703 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15704 TYPE_ALLOC (type,
15705 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15707 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15709 struct fnfieldlist &nf = fip->fnfieldlists[i];
15710 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15712 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15713 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15714 fn_flp->fn_fields = (struct fn_field *)
15715 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15717 for (int k = 0; k < nf.fnfields.size (); ++k)
15718 fn_flp->fn_fields[k] = nf.fnfields[k];
15721 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15724 /* Returns non-zero if NAME is the name of a vtable member in CU's
15725 language, zero otherwise. */
15726 static int
15727 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15729 static const char vptr[] = "_vptr";
15731 /* Look for the C++ form of the vtable. */
15732 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15733 return 1;
15735 return 0;
15738 /* GCC outputs unnamed structures that are really pointers to member
15739 functions, with the ABI-specified layout. If TYPE describes
15740 such a structure, smash it into a member function type.
15742 GCC shouldn't do this; it should just output pointer to member DIEs.
15743 This is GCC PR debug/28767. */
15745 static void
15746 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15748 struct type *pfn_type, *self_type, *new_type;
15750 /* Check for a structure with no name and two children. */
15751 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15752 return;
15754 /* Check for __pfn and __delta members. */
15755 if (TYPE_FIELD_NAME (type, 0) == NULL
15756 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15757 || TYPE_FIELD_NAME (type, 1) == NULL
15758 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15759 return;
15761 /* Find the type of the method. */
15762 pfn_type = TYPE_FIELD_TYPE (type, 0);
15763 if (pfn_type == NULL
15764 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15765 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15766 return;
15768 /* Look for the "this" argument. */
15769 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15770 if (TYPE_NFIELDS (pfn_type) == 0
15771 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15772 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15773 return;
15775 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15776 new_type = alloc_type (objfile);
15777 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15778 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15779 TYPE_VARARGS (pfn_type));
15780 smash_to_methodptr_type (type, new_type);
15783 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15784 appropriate error checking and issuing complaints if there is a
15785 problem. */
15787 static ULONGEST
15788 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15790 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15792 if (attr == nullptr)
15793 return 0;
15795 if (!attr_form_is_constant (attr))
15797 complaint (_("DW_AT_alignment must have constant form"
15798 " - DIE at %s [in module %s]"),
15799 sect_offset_str (die->sect_off),
15800 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15801 return 0;
15804 ULONGEST align;
15805 if (attr->form == DW_FORM_sdata)
15807 LONGEST val = DW_SND (attr);
15808 if (val < 0)
15810 complaint (_("DW_AT_alignment value must not be negative"
15811 " - DIE at %s [in module %s]"),
15812 sect_offset_str (die->sect_off),
15813 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15814 return 0;
15816 align = val;
15818 else
15819 align = DW_UNSND (attr);
15821 if (align == 0)
15823 complaint (_("DW_AT_alignment value must not be zero"
15824 " - DIE at %s [in module %s]"),
15825 sect_offset_str (die->sect_off),
15826 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15827 return 0;
15829 if ((align & (align - 1)) != 0)
15831 complaint (_("DW_AT_alignment value must be a power of 2"
15832 " - DIE at %s [in module %s]"),
15833 sect_offset_str (die->sect_off),
15834 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15835 return 0;
15838 return align;
15841 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15842 the alignment for TYPE. */
15844 static void
15845 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15846 struct type *type)
15848 if (!set_type_align (type, get_alignment (cu, die)))
15849 complaint (_("DW_AT_alignment value too large"
15850 " - DIE at %s [in module %s]"),
15851 sect_offset_str (die->sect_off),
15852 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15855 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15856 constant for a type, according to DWARF5 spec, Table 5.5. */
15858 static bool
15859 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15861 switch (value)
15863 case DW_CC_normal:
15864 case DW_CC_pass_by_reference:
15865 case DW_CC_pass_by_value:
15866 return true;
15868 default:
15869 complaint (_("unrecognized DW_AT_calling_convention value "
15870 "(%s) for a type"), pulongest (value));
15871 return false;
15875 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15876 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15877 also according to GNU-specific values (see include/dwarf2.h). */
15879 static bool
15880 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15882 switch (value)
15884 case DW_CC_normal:
15885 case DW_CC_program:
15886 case DW_CC_nocall:
15887 return true;
15889 case DW_CC_GNU_renesas_sh:
15890 case DW_CC_GNU_borland_fastcall_i386:
15891 case DW_CC_GDB_IBM_OpenCL:
15892 return true;
15894 default:
15895 complaint (_("unrecognized DW_AT_calling_convention value "
15896 "(%s) for a subroutine"), pulongest (value));
15897 return false;
15901 /* Called when we find the DIE that starts a structure or union scope
15902 (definition) to create a type for the structure or union. Fill in
15903 the type's name and general properties; the members will not be
15904 processed until process_structure_scope. A symbol table entry for
15905 the type will also not be done until process_structure_scope (assuming
15906 the type has a name).
15908 NOTE: we need to call these functions regardless of whether or not the
15909 DIE has a DW_AT_name attribute, since it might be an anonymous
15910 structure or union. This gets the type entered into our set of
15911 user defined types. */
15913 static struct type *
15914 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15916 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15917 struct type *type;
15918 struct attribute *attr;
15919 const char *name;
15921 /* If the definition of this type lives in .debug_types, read that type.
15922 Don't follow DW_AT_specification though, that will take us back up
15923 the chain and we want to go down. */
15924 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15925 if (attr != nullptr)
15927 type = get_DW_AT_signature_type (die, attr, cu);
15929 /* The type's CU may not be the same as CU.
15930 Ensure TYPE is recorded with CU in die_type_hash. */
15931 return set_die_type (die, type, cu);
15934 type = alloc_type (objfile);
15935 INIT_CPLUS_SPECIFIC (type);
15937 name = dwarf2_name (die, cu);
15938 if (name != NULL)
15940 if (cu->language == language_cplus
15941 || cu->language == language_d
15942 || cu->language == language_rust)
15944 const char *full_name = dwarf2_full_name (name, die, cu);
15946 /* dwarf2_full_name might have already finished building the DIE's
15947 type. If so, there is no need to continue. */
15948 if (get_die_type (die, cu) != NULL)
15949 return get_die_type (die, cu);
15951 TYPE_NAME (type) = full_name;
15953 else
15955 /* The name is already allocated along with this objfile, so
15956 we don't need to duplicate it for the type. */
15957 TYPE_NAME (type) = name;
15961 if (die->tag == DW_TAG_structure_type)
15963 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15965 else if (die->tag == DW_TAG_union_type)
15967 TYPE_CODE (type) = TYPE_CODE_UNION;
15969 else if (die->tag == DW_TAG_variant_part)
15971 TYPE_CODE (type) = TYPE_CODE_UNION;
15972 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15974 else
15976 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15979 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15980 TYPE_DECLARED_CLASS (type) = 1;
15982 /* Store the calling convention in the type if it's available in
15983 the die. Otherwise the calling convention remains set to
15984 the default value DW_CC_normal. */
15985 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15986 if (attr != nullptr
15987 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15989 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15990 TYPE_CPLUS_CALLING_CONVENTION (type)
15991 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15994 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15995 if (attr != nullptr)
15997 if (attr_form_is_constant (attr))
15998 TYPE_LENGTH (type) = DW_UNSND (attr);
15999 else
16001 /* For the moment, dynamic type sizes are not supported
16002 by GDB's struct type. The actual size is determined
16003 on-demand when resolving the type of a given object,
16004 so set the type's length to zero for now. Otherwise,
16005 we record an expression as the length, and that expression
16006 could lead to a very large value, which could eventually
16007 lead to us trying to allocate that much memory when creating
16008 a value of that type. */
16009 TYPE_LENGTH (type) = 0;
16012 else
16014 TYPE_LENGTH (type) = 0;
16017 maybe_set_alignment (cu, die, type);
16019 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16021 /* ICC<14 does not output the required DW_AT_declaration on
16022 incomplete types, but gives them a size of zero. */
16023 TYPE_STUB (type) = 1;
16025 else
16026 TYPE_STUB_SUPPORTED (type) = 1;
16028 if (die_is_declaration (die, cu))
16029 TYPE_STUB (type) = 1;
16030 else if (attr == NULL && die->child == NULL
16031 && producer_is_realview (cu->producer))
16032 /* RealView does not output the required DW_AT_declaration
16033 on incomplete types. */
16034 TYPE_STUB (type) = 1;
16036 /* We need to add the type field to the die immediately so we don't
16037 infinitely recurse when dealing with pointers to the structure
16038 type within the structure itself. */
16039 set_die_type (die, type, cu);
16041 /* set_die_type should be already done. */
16042 set_descriptive_type (type, die, cu);
16044 return type;
16047 /* A helper for process_structure_scope that handles a single member
16048 DIE. */
16050 static void
16051 handle_struct_member_die (struct die_info *child_die, struct type *type,
16052 struct field_info *fi,
16053 std::vector<struct symbol *> *template_args,
16054 struct dwarf2_cu *cu)
16056 if (child_die->tag == DW_TAG_member
16057 || child_die->tag == DW_TAG_variable
16058 || child_die->tag == DW_TAG_variant_part)
16060 /* NOTE: carlton/2002-11-05: A C++ static data member
16061 should be a DW_TAG_member that is a declaration, but
16062 all versions of G++ as of this writing (so through at
16063 least 3.2.1) incorrectly generate DW_TAG_variable
16064 tags for them instead. */
16065 dwarf2_add_field (fi, child_die, cu);
16067 else if (child_die->tag == DW_TAG_subprogram)
16069 /* Rust doesn't have member functions in the C++ sense.
16070 However, it does emit ordinary functions as children
16071 of a struct DIE. */
16072 if (cu->language == language_rust)
16073 read_func_scope (child_die, cu);
16074 else
16076 /* C++ member function. */
16077 dwarf2_add_member_fn (fi, child_die, type, cu);
16080 else if (child_die->tag == DW_TAG_inheritance)
16082 /* C++ base class field. */
16083 dwarf2_add_field (fi, child_die, cu);
16085 else if (type_can_define_types (child_die))
16086 dwarf2_add_type_defn (fi, child_die, cu);
16087 else if (child_die->tag == DW_TAG_template_type_param
16088 || child_die->tag == DW_TAG_template_value_param)
16090 struct symbol *arg = new_symbol (child_die, NULL, cu);
16092 if (arg != NULL)
16093 template_args->push_back (arg);
16095 else if (child_die->tag == DW_TAG_variant)
16097 /* In a variant we want to get the discriminant and also add a
16098 field for our sole member child. */
16099 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16101 for (die_info *variant_child = child_die->child;
16102 variant_child != NULL;
16103 variant_child = sibling_die (variant_child))
16105 if (variant_child->tag == DW_TAG_member)
16107 handle_struct_member_die (variant_child, type, fi,
16108 template_args, cu);
16109 /* Only handle the one. */
16110 break;
16114 /* We don't handle this but we might as well report it if we see
16115 it. */
16116 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16117 complaint (_("DW_AT_discr_list is not supported yet"
16118 " - DIE at %s [in module %s]"),
16119 sect_offset_str (child_die->sect_off),
16120 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16122 /* The first field was just added, so we can stash the
16123 discriminant there. */
16124 gdb_assert (!fi->fields.empty ());
16125 if (discr == NULL)
16126 fi->fields.back ().variant.default_branch = true;
16127 else
16128 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16132 /* Finish creating a structure or union type, including filling in
16133 its members and creating a symbol for it. */
16135 static void
16136 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16138 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16139 struct die_info *child_die;
16140 struct type *type;
16142 type = get_die_type (die, cu);
16143 if (type == NULL)
16144 type = read_structure_type (die, cu);
16146 /* When reading a DW_TAG_variant_part, we need to notice when we
16147 read the discriminant member, so we can record it later in the
16148 discriminant_info. */
16149 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16150 sect_offset discr_offset {};
16151 bool has_template_parameters = false;
16153 if (is_variant_part)
16155 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16156 if (discr == NULL)
16158 /* Maybe it's a univariant form, an extension we support.
16159 In this case arrange not to check the offset. */
16160 is_variant_part = false;
16162 else if (attr_form_is_ref (discr))
16164 struct dwarf2_cu *target_cu = cu;
16165 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16167 discr_offset = target_die->sect_off;
16169 else
16171 complaint (_("DW_AT_discr does not have DIE reference form"
16172 " - DIE at %s [in module %s]"),
16173 sect_offset_str (die->sect_off),
16174 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16175 is_variant_part = false;
16179 if (die->child != NULL && ! die_is_declaration (die, cu))
16181 struct field_info fi;
16182 std::vector<struct symbol *> template_args;
16184 child_die = die->child;
16186 while (child_die && child_die->tag)
16188 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16190 if (is_variant_part && discr_offset == child_die->sect_off)
16191 fi.fields.back ().variant.is_discriminant = true;
16193 child_die = sibling_die (child_die);
16196 /* Attach template arguments to type. */
16197 if (!template_args.empty ())
16199 has_template_parameters = true;
16200 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16201 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16202 TYPE_TEMPLATE_ARGUMENTS (type)
16203 = XOBNEWVEC (&objfile->objfile_obstack,
16204 struct symbol *,
16205 TYPE_N_TEMPLATE_ARGUMENTS (type));
16206 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16207 template_args.data (),
16208 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16209 * sizeof (struct symbol *)));
16212 /* Attach fields and member functions to the type. */
16213 if (fi.nfields)
16214 dwarf2_attach_fields_to_type (&fi, type, cu);
16215 if (!fi.fnfieldlists.empty ())
16217 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16219 /* Get the type which refers to the base class (possibly this
16220 class itself) which contains the vtable pointer for the current
16221 class from the DW_AT_containing_type attribute. This use of
16222 DW_AT_containing_type is a GNU extension. */
16224 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16226 struct type *t = die_containing_type (die, cu);
16228 set_type_vptr_basetype (type, t);
16229 if (type == t)
16231 int i;
16233 /* Our own class provides vtbl ptr. */
16234 for (i = TYPE_NFIELDS (t) - 1;
16235 i >= TYPE_N_BASECLASSES (t);
16236 --i)
16238 const char *fieldname = TYPE_FIELD_NAME (t, i);
16240 if (is_vtable_name (fieldname, cu))
16242 set_type_vptr_fieldno (type, i);
16243 break;
16247 /* Complain if virtual function table field not found. */
16248 if (i < TYPE_N_BASECLASSES (t))
16249 complaint (_("virtual function table pointer "
16250 "not found when defining class '%s'"),
16251 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16253 else
16255 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16258 else if (cu->producer
16259 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16261 /* The IBM XLC compiler does not provide direct indication
16262 of the containing type, but the vtable pointer is
16263 always named __vfp. */
16265 int i;
16267 for (i = TYPE_NFIELDS (type) - 1;
16268 i >= TYPE_N_BASECLASSES (type);
16269 --i)
16271 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16273 set_type_vptr_fieldno (type, i);
16274 set_type_vptr_basetype (type, type);
16275 break;
16281 /* Copy fi.typedef_field_list linked list elements content into the
16282 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16283 if (!fi.typedef_field_list.empty ())
16285 int count = fi.typedef_field_list.size ();
16287 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16288 TYPE_TYPEDEF_FIELD_ARRAY (type)
16289 = ((struct decl_field *)
16290 TYPE_ALLOC (type,
16291 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16292 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16294 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16295 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16298 /* Copy fi.nested_types_list linked list elements content into the
16299 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16300 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16302 int count = fi.nested_types_list.size ();
16304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16305 TYPE_NESTED_TYPES_ARRAY (type)
16306 = ((struct decl_field *)
16307 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16308 TYPE_NESTED_TYPES_COUNT (type) = count;
16310 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16311 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16315 quirk_gcc_member_function_pointer (type, objfile);
16316 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16317 cu->rust_unions.push_back (type);
16319 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16320 snapshots) has been known to create a die giving a declaration
16321 for a class that has, as a child, a die giving a definition for a
16322 nested class. So we have to process our children even if the
16323 current die is a declaration. Normally, of course, a declaration
16324 won't have any children at all. */
16326 child_die = die->child;
16328 while (child_die != NULL && child_die->tag)
16330 if (child_die->tag == DW_TAG_member
16331 || child_die->tag == DW_TAG_variable
16332 || child_die->tag == DW_TAG_inheritance
16333 || child_die->tag == DW_TAG_template_value_param
16334 || child_die->tag == DW_TAG_template_type_param)
16336 /* Do nothing. */
16338 else
16339 process_die (child_die, cu);
16341 child_die = sibling_die (child_die);
16344 /* Do not consider external references. According to the DWARF standard,
16345 these DIEs are identified by the fact that they have no byte_size
16346 attribute, and a declaration attribute. */
16347 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16348 || !die_is_declaration (die, cu))
16350 struct symbol *sym = new_symbol (die, type, cu);
16352 if (has_template_parameters)
16354 struct symtab *symtab;
16355 if (sym != nullptr)
16356 symtab = symbol_symtab (sym);
16357 else if (cu->line_header != nullptr)
16359 /* Any related symtab will do. */
16360 symtab
16361 = cu->line_header->file_names ()[0].symtab;
16363 else
16365 symtab = nullptr;
16366 complaint (_("could not find suitable "
16367 "symtab for template parameter"
16368 " - DIE at %s [in module %s]"),
16369 sect_offset_str (die->sect_off),
16370 objfile_name (objfile));
16373 if (symtab != nullptr)
16375 /* Make sure that the symtab is set on the new symbols.
16376 Even though they don't appear in this symtab directly,
16377 other parts of gdb assume that symbols do, and this is
16378 reasonably true. */
16379 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16380 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16386 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16387 update TYPE using some information only available in DIE's children. */
16389 static void
16390 update_enumeration_type_from_children (struct die_info *die,
16391 struct type *type,
16392 struct dwarf2_cu *cu)
16394 struct die_info *child_die;
16395 int unsigned_enum = 1;
16396 int flag_enum = 1;
16397 ULONGEST mask = 0;
16399 auto_obstack obstack;
16401 for (child_die = die->child;
16402 child_die != NULL && child_die->tag;
16403 child_die = sibling_die (child_die))
16405 struct attribute *attr;
16406 LONGEST value;
16407 const gdb_byte *bytes;
16408 struct dwarf2_locexpr_baton *baton;
16409 const char *name;
16411 if (child_die->tag != DW_TAG_enumerator)
16412 continue;
16414 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16415 if (attr == NULL)
16416 continue;
16418 name = dwarf2_name (child_die, cu);
16419 if (name == NULL)
16420 name = "<anonymous enumerator>";
16422 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16423 &value, &bytes, &baton);
16424 if (value < 0)
16426 unsigned_enum = 0;
16427 flag_enum = 0;
16429 else if ((mask & value) != 0)
16430 flag_enum = 0;
16431 else
16432 mask |= value;
16434 /* If we already know that the enum type is neither unsigned, nor
16435 a flag type, no need to look at the rest of the enumerates. */
16436 if (!unsigned_enum && !flag_enum)
16437 break;
16440 if (unsigned_enum)
16441 TYPE_UNSIGNED (type) = 1;
16442 if (flag_enum)
16443 TYPE_FLAG_ENUM (type) = 1;
16446 /* Given a DW_AT_enumeration_type die, set its type. We do not
16447 complete the type's fields yet, or create any symbols. */
16449 static struct type *
16450 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16453 struct type *type;
16454 struct attribute *attr;
16455 const char *name;
16457 /* If the definition of this type lives in .debug_types, read that type.
16458 Don't follow DW_AT_specification though, that will take us back up
16459 the chain and we want to go down. */
16460 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16461 if (attr != nullptr)
16463 type = get_DW_AT_signature_type (die, attr, cu);
16465 /* The type's CU may not be the same as CU.
16466 Ensure TYPE is recorded with CU in die_type_hash. */
16467 return set_die_type (die, type, cu);
16470 type = alloc_type (objfile);
16472 TYPE_CODE (type) = TYPE_CODE_ENUM;
16473 name = dwarf2_full_name (NULL, die, cu);
16474 if (name != NULL)
16475 TYPE_NAME (type) = name;
16477 attr = dwarf2_attr (die, DW_AT_type, cu);
16478 if (attr != NULL)
16480 struct type *underlying_type = die_type (die, cu);
16482 TYPE_TARGET_TYPE (type) = underlying_type;
16485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16486 if (attr != nullptr)
16488 TYPE_LENGTH (type) = DW_UNSND (attr);
16490 else
16492 TYPE_LENGTH (type) = 0;
16495 maybe_set_alignment (cu, die, type);
16497 /* The enumeration DIE can be incomplete. In Ada, any type can be
16498 declared as private in the package spec, and then defined only
16499 inside the package body. Such types are known as Taft Amendment
16500 Types. When another package uses such a type, an incomplete DIE
16501 may be generated by the compiler. */
16502 if (die_is_declaration (die, cu))
16503 TYPE_STUB (type) = 1;
16505 /* Finish the creation of this type by using the enum's children.
16506 We must call this even when the underlying type has been provided
16507 so that we can determine if we're looking at a "flag" enum. */
16508 update_enumeration_type_from_children (die, type, cu);
16510 /* If this type has an underlying type that is not a stub, then we
16511 may use its attributes. We always use the "unsigned" attribute
16512 in this situation, because ordinarily we guess whether the type
16513 is unsigned -- but the guess can be wrong and the underlying type
16514 can tell us the reality. However, we defer to a local size
16515 attribute if one exists, because this lets the compiler override
16516 the underlying type if needed. */
16517 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16519 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16520 if (TYPE_LENGTH (type) == 0)
16521 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16522 if (TYPE_RAW_ALIGN (type) == 0
16523 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16524 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16527 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16529 return set_die_type (die, type, cu);
16532 /* Given a pointer to a die which begins an enumeration, process all
16533 the dies that define the members of the enumeration, and create the
16534 symbol for the enumeration type.
16536 NOTE: We reverse the order of the element list. */
16538 static void
16539 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16541 struct type *this_type;
16543 this_type = get_die_type (die, cu);
16544 if (this_type == NULL)
16545 this_type = read_enumeration_type (die, cu);
16547 if (die->child != NULL)
16549 struct die_info *child_die;
16550 struct symbol *sym;
16551 struct field *fields = NULL;
16552 int num_fields = 0;
16553 const char *name;
16555 child_die = die->child;
16556 while (child_die && child_die->tag)
16558 if (child_die->tag != DW_TAG_enumerator)
16560 process_die (child_die, cu);
16562 else
16564 name = dwarf2_name (child_die, cu);
16565 if (name)
16567 sym = new_symbol (child_die, this_type, cu);
16569 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16571 fields = (struct field *)
16572 xrealloc (fields,
16573 (num_fields + DW_FIELD_ALLOC_CHUNK)
16574 * sizeof (struct field));
16577 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
16578 FIELD_TYPE (fields[num_fields]) = NULL;
16579 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16580 FIELD_BITSIZE (fields[num_fields]) = 0;
16582 num_fields++;
16586 child_die = sibling_die (child_die);
16589 if (num_fields)
16591 TYPE_NFIELDS (this_type) = num_fields;
16592 TYPE_FIELDS (this_type) = (struct field *)
16593 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16594 memcpy (TYPE_FIELDS (this_type), fields,
16595 sizeof (struct field) * num_fields);
16596 xfree (fields);
16600 /* If we are reading an enum from a .debug_types unit, and the enum
16601 is a declaration, and the enum is not the signatured type in the
16602 unit, then we do not want to add a symbol for it. Adding a
16603 symbol would in some cases obscure the true definition of the
16604 enum, giving users an incomplete type when the definition is
16605 actually available. Note that we do not want to do this for all
16606 enums which are just declarations, because C++0x allows forward
16607 enum declarations. */
16608 if (cu->per_cu->is_debug_types
16609 && die_is_declaration (die, cu))
16611 struct signatured_type *sig_type;
16613 sig_type = (struct signatured_type *) cu->per_cu;
16614 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16615 if (sig_type->type_offset_in_section != die->sect_off)
16616 return;
16619 new_symbol (die, this_type, cu);
16622 /* Extract all information from a DW_TAG_array_type DIE and put it in
16623 the DIE's type field. For now, this only handles one dimensional
16624 arrays. */
16626 static struct type *
16627 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16630 struct die_info *child_die;
16631 struct type *type;
16632 struct type *element_type, *range_type, *index_type;
16633 struct attribute *attr;
16634 const char *name;
16635 struct dynamic_prop *byte_stride_prop = NULL;
16636 unsigned int bit_stride = 0;
16638 element_type = die_type (die, cu);
16640 /* The die_type call above may have already set the type for this DIE. */
16641 type = get_die_type (die, cu);
16642 if (type)
16643 return type;
16645 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16646 if (attr != NULL)
16648 int stride_ok;
16649 struct type *prop_type
16650 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16652 byte_stride_prop
16653 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16654 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16655 prop_type);
16656 if (!stride_ok)
16658 complaint (_("unable to read array DW_AT_byte_stride "
16659 " - DIE at %s [in module %s]"),
16660 sect_offset_str (die->sect_off),
16661 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16662 /* Ignore this attribute. We will likely not be able to print
16663 arrays of this type correctly, but there is little we can do
16664 to help if we cannot read the attribute's value. */
16665 byte_stride_prop = NULL;
16669 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16670 if (attr != NULL)
16671 bit_stride = DW_UNSND (attr);
16673 /* Irix 6.2 native cc creates array types without children for
16674 arrays with unspecified length. */
16675 if (die->child == NULL)
16677 index_type = objfile_type (objfile)->builtin_int;
16678 range_type = create_static_range_type (NULL, index_type, 0, -1);
16679 type = create_array_type_with_stride (NULL, element_type, range_type,
16680 byte_stride_prop, bit_stride);
16681 return set_die_type (die, type, cu);
16684 std::vector<struct type *> range_types;
16685 child_die = die->child;
16686 while (child_die && child_die->tag)
16688 if (child_die->tag == DW_TAG_subrange_type)
16690 struct type *child_type = read_type_die (child_die, cu);
16692 if (child_type != NULL)
16694 /* The range type was succesfully read. Save it for the
16695 array type creation. */
16696 range_types.push_back (child_type);
16699 child_die = sibling_die (child_die);
16702 /* Dwarf2 dimensions are output from left to right, create the
16703 necessary array types in backwards order. */
16705 type = element_type;
16707 if (read_array_order (die, cu) == DW_ORD_col_major)
16709 int i = 0;
16711 while (i < range_types.size ())
16712 type = create_array_type_with_stride (NULL, type, range_types[i++],
16713 byte_stride_prop, bit_stride);
16715 else
16717 size_t ndim = range_types.size ();
16718 while (ndim-- > 0)
16719 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16720 byte_stride_prop, bit_stride);
16723 /* Understand Dwarf2 support for vector types (like they occur on
16724 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16725 array type. This is not part of the Dwarf2/3 standard yet, but a
16726 custom vendor extension. The main difference between a regular
16727 array and the vector variant is that vectors are passed by value
16728 to functions. */
16729 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16730 if (attr != nullptr)
16731 make_vector_type (type);
16733 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16734 implementation may choose to implement triple vectors using this
16735 attribute. */
16736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16737 if (attr != nullptr)
16739 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16740 TYPE_LENGTH (type) = DW_UNSND (attr);
16741 else
16742 complaint (_("DW_AT_byte_size for array type smaller "
16743 "than the total size of elements"));
16746 name = dwarf2_name (die, cu);
16747 if (name)
16748 TYPE_NAME (type) = name;
16750 maybe_set_alignment (cu, die, type);
16752 /* Install the type in the die. */
16753 set_die_type (die, type, cu);
16755 /* set_die_type should be already done. */
16756 set_descriptive_type (type, die, cu);
16758 return type;
16761 static enum dwarf_array_dim_ordering
16762 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16764 struct attribute *attr;
16766 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16768 if (attr != nullptr)
16769 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16771 /* GNU F77 is a special case, as at 08/2004 array type info is the
16772 opposite order to the dwarf2 specification, but data is still
16773 laid out as per normal fortran.
16775 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16776 version checking. */
16778 if (cu->language == language_fortran
16779 && cu->producer && strstr (cu->producer, "GNU F77"))
16781 return DW_ORD_row_major;
16784 switch (cu->language_defn->la_array_ordering)
16786 case array_column_major:
16787 return DW_ORD_col_major;
16788 case array_row_major:
16789 default:
16790 return DW_ORD_row_major;
16794 /* Extract all information from a DW_TAG_set_type DIE and put it in
16795 the DIE's type field. */
16797 static struct type *
16798 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16800 struct type *domain_type, *set_type;
16801 struct attribute *attr;
16803 domain_type = die_type (die, cu);
16805 /* The die_type call above may have already set the type for this DIE. */
16806 set_type = get_die_type (die, cu);
16807 if (set_type)
16808 return set_type;
16810 set_type = create_set_type (NULL, domain_type);
16812 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16813 if (attr != nullptr)
16814 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16816 maybe_set_alignment (cu, die, set_type);
16818 return set_die_type (die, set_type, cu);
16821 /* A helper for read_common_block that creates a locexpr baton.
16822 SYM is the symbol which we are marking as computed.
16823 COMMON_DIE is the DIE for the common block.
16824 COMMON_LOC is the location expression attribute for the common
16825 block itself.
16826 MEMBER_LOC is the location expression attribute for the particular
16827 member of the common block that we are processing.
16828 CU is the CU from which the above come. */
16830 static void
16831 mark_common_block_symbol_computed (struct symbol *sym,
16832 struct die_info *common_die,
16833 struct attribute *common_loc,
16834 struct attribute *member_loc,
16835 struct dwarf2_cu *cu)
16837 struct dwarf2_per_objfile *dwarf2_per_objfile
16838 = cu->per_cu->dwarf2_per_objfile;
16839 struct objfile *objfile = dwarf2_per_objfile->objfile;
16840 struct dwarf2_locexpr_baton *baton;
16841 gdb_byte *ptr;
16842 unsigned int cu_off;
16843 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16844 LONGEST offset = 0;
16846 gdb_assert (common_loc && member_loc);
16847 gdb_assert (attr_form_is_block (common_loc));
16848 gdb_assert (attr_form_is_block (member_loc)
16849 || attr_form_is_constant (member_loc));
16851 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16852 baton->per_cu = cu->per_cu;
16853 gdb_assert (baton->per_cu);
16855 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16857 if (attr_form_is_constant (member_loc))
16859 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16860 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16862 else
16863 baton->size += DW_BLOCK (member_loc)->size;
16865 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16866 baton->data = ptr;
16868 *ptr++ = DW_OP_call4;
16869 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16870 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16871 ptr += 4;
16873 if (attr_form_is_constant (member_loc))
16875 *ptr++ = DW_OP_addr;
16876 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16877 ptr += cu->header.addr_size;
16879 else
16881 /* We have to copy the data here, because DW_OP_call4 will only
16882 use a DW_AT_location attribute. */
16883 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16884 ptr += DW_BLOCK (member_loc)->size;
16887 *ptr++ = DW_OP_plus;
16888 gdb_assert (ptr - baton->data == baton->size);
16890 SYMBOL_LOCATION_BATON (sym) = baton;
16891 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16894 /* Create appropriate locally-scoped variables for all the
16895 DW_TAG_common_block entries. Also create a struct common_block
16896 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16897 is used to separate the common blocks name namespace from regular
16898 variable names. */
16900 static void
16901 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16903 struct attribute *attr;
16905 attr = dwarf2_attr (die, DW_AT_location, cu);
16906 if (attr != nullptr)
16908 /* Support the .debug_loc offsets. */
16909 if (attr_form_is_block (attr))
16911 /* Ok. */
16913 else if (attr_form_is_section_offset (attr))
16915 dwarf2_complex_location_expr_complaint ();
16916 attr = NULL;
16918 else
16920 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16921 "common block member");
16922 attr = NULL;
16926 if (die->child != NULL)
16928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16929 struct die_info *child_die;
16930 size_t n_entries = 0, size;
16931 struct common_block *common_block;
16932 struct symbol *sym;
16934 for (child_die = die->child;
16935 child_die && child_die->tag;
16936 child_die = sibling_die (child_die))
16937 ++n_entries;
16939 size = (sizeof (struct common_block)
16940 + (n_entries - 1) * sizeof (struct symbol *));
16941 common_block
16942 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16943 size);
16944 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16945 common_block->n_entries = 0;
16947 for (child_die = die->child;
16948 child_die && child_die->tag;
16949 child_die = sibling_die (child_die))
16951 /* Create the symbol in the DW_TAG_common_block block in the current
16952 symbol scope. */
16953 sym = new_symbol (child_die, NULL, cu);
16954 if (sym != NULL)
16956 struct attribute *member_loc;
16958 common_block->contents[common_block->n_entries++] = sym;
16960 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16961 cu);
16962 if (member_loc)
16964 /* GDB has handled this for a long time, but it is
16965 not specified by DWARF. It seems to have been
16966 emitted by gfortran at least as recently as:
16967 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16968 complaint (_("Variable in common block has "
16969 "DW_AT_data_member_location "
16970 "- DIE at %s [in module %s]"),
16971 sect_offset_str (child_die->sect_off),
16972 objfile_name (objfile));
16974 if (attr_form_is_section_offset (member_loc))
16975 dwarf2_complex_location_expr_complaint ();
16976 else if (attr_form_is_constant (member_loc)
16977 || attr_form_is_block (member_loc))
16979 if (attr != nullptr)
16980 mark_common_block_symbol_computed (sym, die, attr,
16981 member_loc, cu);
16983 else
16984 dwarf2_complex_location_expr_complaint ();
16989 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16990 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16994 /* Create a type for a C++ namespace. */
16996 static struct type *
16997 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17000 const char *previous_prefix, *name;
17001 int is_anonymous;
17002 struct type *type;
17004 /* For extensions, reuse the type of the original namespace. */
17005 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17007 struct die_info *ext_die;
17008 struct dwarf2_cu *ext_cu = cu;
17010 ext_die = dwarf2_extension (die, &ext_cu);
17011 type = read_type_die (ext_die, ext_cu);
17013 /* EXT_CU may not be the same as CU.
17014 Ensure TYPE is recorded with CU in die_type_hash. */
17015 return set_die_type (die, type, cu);
17018 name = namespace_name (die, &is_anonymous, cu);
17020 /* Now build the name of the current namespace. */
17022 previous_prefix = determine_prefix (die, cu);
17023 if (previous_prefix[0] != '\0')
17024 name = typename_concat (&objfile->objfile_obstack,
17025 previous_prefix, name, 0, cu);
17027 /* Create the type. */
17028 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17030 return set_die_type (die, type, cu);
17033 /* Read a namespace scope. */
17035 static void
17036 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17039 int is_anonymous;
17041 /* Add a symbol associated to this if we haven't seen the namespace
17042 before. Also, add a using directive if it's an anonymous
17043 namespace. */
17045 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17047 struct type *type;
17049 type = read_type_die (die, cu);
17050 new_symbol (die, type, cu);
17052 namespace_name (die, &is_anonymous, cu);
17053 if (is_anonymous)
17055 const char *previous_prefix = determine_prefix (die, cu);
17057 std::vector<const char *> excludes;
17058 add_using_directive (using_directives (cu),
17059 previous_prefix, TYPE_NAME (type), NULL,
17060 NULL, excludes, 0, &objfile->objfile_obstack);
17064 if (die->child != NULL)
17066 struct die_info *child_die = die->child;
17068 while (child_die && child_die->tag)
17070 process_die (child_die, cu);
17071 child_die = sibling_die (child_die);
17076 /* Read a Fortran module as type. This DIE can be only a declaration used for
17077 imported module. Still we need that type as local Fortran "use ... only"
17078 declaration imports depend on the created type in determine_prefix. */
17080 static struct type *
17081 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17084 const char *module_name;
17085 struct type *type;
17087 module_name = dwarf2_name (die, cu);
17088 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17090 return set_die_type (die, type, cu);
17093 /* Read a Fortran module. */
17095 static void
17096 read_module (struct die_info *die, struct dwarf2_cu *cu)
17098 struct die_info *child_die = die->child;
17099 struct type *type;
17101 type = read_type_die (die, cu);
17102 new_symbol (die, type, cu);
17104 while (child_die && child_die->tag)
17106 process_die (child_die, cu);
17107 child_die = sibling_die (child_die);
17111 /* Return the name of the namespace represented by DIE. Set
17112 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17113 namespace. */
17115 static const char *
17116 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17118 struct die_info *current_die;
17119 const char *name = NULL;
17121 /* Loop through the extensions until we find a name. */
17123 for (current_die = die;
17124 current_die != NULL;
17125 current_die = dwarf2_extension (die, &cu))
17127 /* We don't use dwarf2_name here so that we can detect the absence
17128 of a name -> anonymous namespace. */
17129 name = dwarf2_string_attr (die, DW_AT_name, cu);
17131 if (name != NULL)
17132 break;
17135 /* Is it an anonymous namespace? */
17137 *is_anonymous = (name == NULL);
17138 if (*is_anonymous)
17139 name = CP_ANONYMOUS_NAMESPACE_STR;
17141 return name;
17144 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17145 the user defined type vector. */
17147 static struct type *
17148 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17150 struct gdbarch *gdbarch
17151 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17152 struct comp_unit_head *cu_header = &cu->header;
17153 struct type *type;
17154 struct attribute *attr_byte_size;
17155 struct attribute *attr_address_class;
17156 int byte_size, addr_class;
17157 struct type *target_type;
17159 target_type = die_type (die, cu);
17161 /* The die_type call above may have already set the type for this DIE. */
17162 type = get_die_type (die, cu);
17163 if (type)
17164 return type;
17166 type = lookup_pointer_type (target_type);
17168 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17169 if (attr_byte_size)
17170 byte_size = DW_UNSND (attr_byte_size);
17171 else
17172 byte_size = cu_header->addr_size;
17174 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17175 if (attr_address_class)
17176 addr_class = DW_UNSND (attr_address_class);
17177 else
17178 addr_class = DW_ADDR_none;
17180 ULONGEST alignment = get_alignment (cu, die);
17182 /* If the pointer size, alignment, or address class is different
17183 than the default, create a type variant marked as such and set
17184 the length accordingly. */
17185 if (TYPE_LENGTH (type) != byte_size
17186 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17187 && alignment != TYPE_RAW_ALIGN (type))
17188 || addr_class != DW_ADDR_none)
17190 if (gdbarch_address_class_type_flags_p (gdbarch))
17192 int type_flags;
17194 type_flags = gdbarch_address_class_type_flags
17195 (gdbarch, byte_size, addr_class);
17196 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17197 == 0);
17198 type = make_type_with_address_space (type, type_flags);
17200 else if (TYPE_LENGTH (type) != byte_size)
17202 complaint (_("invalid pointer size %d"), byte_size);
17204 else if (TYPE_RAW_ALIGN (type) != alignment)
17206 complaint (_("Invalid DW_AT_alignment"
17207 " - DIE at %s [in module %s]"),
17208 sect_offset_str (die->sect_off),
17209 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17211 else
17213 /* Should we also complain about unhandled address classes? */
17217 TYPE_LENGTH (type) = byte_size;
17218 set_type_align (type, alignment);
17219 return set_die_type (die, type, cu);
17222 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17223 the user defined type vector. */
17225 static struct type *
17226 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17228 struct type *type;
17229 struct type *to_type;
17230 struct type *domain;
17232 to_type = die_type (die, cu);
17233 domain = die_containing_type (die, cu);
17235 /* The calls above may have already set the type for this DIE. */
17236 type = get_die_type (die, cu);
17237 if (type)
17238 return type;
17240 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17241 type = lookup_methodptr_type (to_type);
17242 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17244 struct type *new_type
17245 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17247 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17248 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17249 TYPE_VARARGS (to_type));
17250 type = lookup_methodptr_type (new_type);
17252 else
17253 type = lookup_memberptr_type (to_type, domain);
17255 return set_die_type (die, type, cu);
17258 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17259 the user defined type vector. */
17261 static struct type *
17262 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17263 enum type_code refcode)
17265 struct comp_unit_head *cu_header = &cu->header;
17266 struct type *type, *target_type;
17267 struct attribute *attr;
17269 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17271 target_type = die_type (die, cu);
17273 /* The die_type call above may have already set the type for this DIE. */
17274 type = get_die_type (die, cu);
17275 if (type)
17276 return type;
17278 type = lookup_reference_type (target_type, refcode);
17279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17280 if (attr != nullptr)
17282 TYPE_LENGTH (type) = DW_UNSND (attr);
17284 else
17286 TYPE_LENGTH (type) = cu_header->addr_size;
17288 maybe_set_alignment (cu, die, type);
17289 return set_die_type (die, type, cu);
17292 /* Add the given cv-qualifiers to the element type of the array. GCC
17293 outputs DWARF type qualifiers that apply to an array, not the
17294 element type. But GDB relies on the array element type to carry
17295 the cv-qualifiers. This mimics section 6.7.3 of the C99
17296 specification. */
17298 static struct type *
17299 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17300 struct type *base_type, int cnst, int voltl)
17302 struct type *el_type, *inner_array;
17304 base_type = copy_type (base_type);
17305 inner_array = base_type;
17307 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17309 TYPE_TARGET_TYPE (inner_array) =
17310 copy_type (TYPE_TARGET_TYPE (inner_array));
17311 inner_array = TYPE_TARGET_TYPE (inner_array);
17314 el_type = TYPE_TARGET_TYPE (inner_array);
17315 cnst |= TYPE_CONST (el_type);
17316 voltl |= TYPE_VOLATILE (el_type);
17317 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17319 return set_die_type (die, base_type, cu);
17322 static struct type *
17323 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17325 struct type *base_type, *cv_type;
17327 base_type = die_type (die, cu);
17329 /* The die_type call above may have already set the type for this DIE. */
17330 cv_type = get_die_type (die, cu);
17331 if (cv_type)
17332 return cv_type;
17334 /* In case the const qualifier is applied to an array type, the element type
17335 is so qualified, not the array type (section 6.7.3 of C99). */
17336 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17337 return add_array_cv_type (die, cu, base_type, 1, 0);
17339 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17340 return set_die_type (die, cv_type, cu);
17343 static struct type *
17344 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17346 struct type *base_type, *cv_type;
17348 base_type = die_type (die, cu);
17350 /* The die_type call above may have already set the type for this DIE. */
17351 cv_type = get_die_type (die, cu);
17352 if (cv_type)
17353 return cv_type;
17355 /* In case the volatile qualifier is applied to an array type, the
17356 element type is so qualified, not the array type (section 6.7.3
17357 of C99). */
17358 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17359 return add_array_cv_type (die, cu, base_type, 0, 1);
17361 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17362 return set_die_type (die, cv_type, cu);
17365 /* Handle DW_TAG_restrict_type. */
17367 static struct type *
17368 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17370 struct type *base_type, *cv_type;
17372 base_type = die_type (die, cu);
17374 /* The die_type call above may have already set the type for this DIE. */
17375 cv_type = get_die_type (die, cu);
17376 if (cv_type)
17377 return cv_type;
17379 cv_type = make_restrict_type (base_type);
17380 return set_die_type (die, cv_type, cu);
17383 /* Handle DW_TAG_atomic_type. */
17385 static struct type *
17386 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17388 struct type *base_type, *cv_type;
17390 base_type = die_type (die, cu);
17392 /* The die_type call above may have already set the type for this DIE. */
17393 cv_type = get_die_type (die, cu);
17394 if (cv_type)
17395 return cv_type;
17397 cv_type = make_atomic_type (base_type);
17398 return set_die_type (die, cv_type, cu);
17401 /* Extract all information from a DW_TAG_string_type DIE and add to
17402 the user defined type vector. It isn't really a user defined type,
17403 but it behaves like one, with other DIE's using an AT_user_def_type
17404 attribute to reference it. */
17406 static struct type *
17407 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17409 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17411 struct type *type, *range_type, *index_type, *char_type;
17412 struct attribute *attr;
17413 struct dynamic_prop prop;
17414 bool length_is_constant = true;
17415 LONGEST length;
17417 /* There are a couple of places where bit sizes might be made use of
17418 when parsing a DW_TAG_string_type, however, no producer that we know
17419 of make use of these. Handling bit sizes that are a multiple of the
17420 byte size is easy enough, but what about other bit sizes? Lets deal
17421 with that problem when we have to. Warn about these attributes being
17422 unsupported, then parse the type and ignore them like we always
17423 have. */
17424 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17425 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17427 static bool warning_printed = false;
17428 if (!warning_printed)
17430 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17431 "currently supported on DW_TAG_string_type."));
17432 warning_printed = true;
17436 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17437 if (attr != nullptr && !attr_form_is_constant (attr))
17439 /* The string length describes the location at which the length of
17440 the string can be found. The size of the length field can be
17441 specified with one of the attributes below. */
17442 struct type *prop_type;
17443 struct attribute *len
17444 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17445 if (len == nullptr)
17446 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17447 if (len != nullptr && attr_form_is_constant (len))
17449 /* Pass 0 as the default as we know this attribute is constant
17450 and the default value will not be returned. */
17451 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17452 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17454 else
17456 /* If the size is not specified then we assume it is the size of
17457 an address on this target. */
17458 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17461 /* Convert the attribute into a dynamic property. */
17462 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17463 length = 1;
17464 else
17465 length_is_constant = false;
17467 else if (attr != nullptr)
17469 /* This DW_AT_string_length just contains the length with no
17470 indirection. There's no need to create a dynamic property in this
17471 case. Pass 0 for the default value as we know it will not be
17472 returned in this case. */
17473 length = dwarf2_get_attr_constant_value (attr, 0);
17475 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17477 /* We don't currently support non-constant byte sizes for strings. */
17478 length = dwarf2_get_attr_constant_value (attr, 1);
17480 else
17482 /* Use 1 as a fallback length if we have nothing else. */
17483 length = 1;
17486 index_type = objfile_type (objfile)->builtin_int;
17487 if (length_is_constant)
17488 range_type = create_static_range_type (NULL, index_type, 1, length);
17489 else
17491 struct dynamic_prop low_bound;
17493 low_bound.kind = PROP_CONST;
17494 low_bound.data.const_val = 1;
17495 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17497 char_type = language_string_char_type (cu->language_defn, gdbarch);
17498 type = create_string_type (NULL, char_type, range_type);
17500 return set_die_type (die, type, cu);
17503 /* Assuming that DIE corresponds to a function, returns nonzero
17504 if the function is prototyped. */
17506 static int
17507 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17509 struct attribute *attr;
17511 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17512 if (attr && (DW_UNSND (attr) != 0))
17513 return 1;
17515 /* The DWARF standard implies that the DW_AT_prototyped attribute
17516 is only meaningful for C, but the concept also extends to other
17517 languages that allow unprototyped functions (Eg: Objective C).
17518 For all other languages, assume that functions are always
17519 prototyped. */
17520 if (cu->language != language_c
17521 && cu->language != language_objc
17522 && cu->language != language_opencl)
17523 return 1;
17525 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17526 prototyped and unprototyped functions; default to prototyped,
17527 since that is more common in modern code (and RealView warns
17528 about unprototyped functions). */
17529 if (producer_is_realview (cu->producer))
17530 return 1;
17532 return 0;
17535 /* Handle DIES due to C code like:
17537 struct foo
17539 int (*funcp)(int a, long l);
17540 int b;
17543 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17545 static struct type *
17546 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17548 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17549 struct type *type; /* Type that this function returns. */
17550 struct type *ftype; /* Function that returns above type. */
17551 struct attribute *attr;
17553 type = die_type (die, cu);
17555 /* The die_type call above may have already set the type for this DIE. */
17556 ftype = get_die_type (die, cu);
17557 if (ftype)
17558 return ftype;
17560 ftype = lookup_function_type (type);
17562 if (prototyped_function_p (die, cu))
17563 TYPE_PROTOTYPED (ftype) = 1;
17565 /* Store the calling convention in the type if it's available in
17566 the subroutine die. Otherwise set the calling convention to
17567 the default value DW_CC_normal. */
17568 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17569 if (attr != nullptr
17570 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17571 TYPE_CALLING_CONVENTION (ftype)
17572 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17573 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17574 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17575 else
17576 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17578 /* Record whether the function returns normally to its caller or not
17579 if the DWARF producer set that information. */
17580 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17581 if (attr && (DW_UNSND (attr) != 0))
17582 TYPE_NO_RETURN (ftype) = 1;
17584 /* We need to add the subroutine type to the die immediately so
17585 we don't infinitely recurse when dealing with parameters
17586 declared as the same subroutine type. */
17587 set_die_type (die, ftype, cu);
17589 if (die->child != NULL)
17591 struct type *void_type = objfile_type (objfile)->builtin_void;
17592 struct die_info *child_die;
17593 int nparams, iparams;
17595 /* Count the number of parameters.
17596 FIXME: GDB currently ignores vararg functions, but knows about
17597 vararg member functions. */
17598 nparams = 0;
17599 child_die = die->child;
17600 while (child_die && child_die->tag)
17602 if (child_die->tag == DW_TAG_formal_parameter)
17603 nparams++;
17604 else if (child_die->tag == DW_TAG_unspecified_parameters)
17605 TYPE_VARARGS (ftype) = 1;
17606 child_die = sibling_die (child_die);
17609 /* Allocate storage for parameters and fill them in. */
17610 TYPE_NFIELDS (ftype) = nparams;
17611 TYPE_FIELDS (ftype) = (struct field *)
17612 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17614 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17615 even if we error out during the parameters reading below. */
17616 for (iparams = 0; iparams < nparams; iparams++)
17617 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17619 iparams = 0;
17620 child_die = die->child;
17621 while (child_die && child_die->tag)
17623 if (child_die->tag == DW_TAG_formal_parameter)
17625 struct type *arg_type;
17627 /* DWARF version 2 has no clean way to discern C++
17628 static and non-static member functions. G++ helps
17629 GDB by marking the first parameter for non-static
17630 member functions (which is the this pointer) as
17631 artificial. We pass this information to
17632 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17634 DWARF version 3 added DW_AT_object_pointer, which GCC
17635 4.5 does not yet generate. */
17636 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17637 if (attr != nullptr)
17638 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17639 else
17640 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17641 arg_type = die_type (child_die, cu);
17643 /* RealView does not mark THIS as const, which the testsuite
17644 expects. GCC marks THIS as const in method definitions,
17645 but not in the class specifications (GCC PR 43053). */
17646 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17647 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17649 int is_this = 0;
17650 struct dwarf2_cu *arg_cu = cu;
17651 const char *name = dwarf2_name (child_die, cu);
17653 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17654 if (attr != nullptr)
17656 /* If the compiler emits this, use it. */
17657 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17658 is_this = 1;
17660 else if (name && strcmp (name, "this") == 0)
17661 /* Function definitions will have the argument names. */
17662 is_this = 1;
17663 else if (name == NULL && iparams == 0)
17664 /* Declarations may not have the names, so like
17665 elsewhere in GDB, assume an artificial first
17666 argument is "this". */
17667 is_this = 1;
17669 if (is_this)
17670 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17671 arg_type, 0);
17674 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17675 iparams++;
17677 child_die = sibling_die (child_die);
17681 return ftype;
17684 static struct type *
17685 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17688 const char *name = NULL;
17689 struct type *this_type, *target_type;
17691 name = dwarf2_full_name (NULL, die, cu);
17692 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17693 TYPE_TARGET_STUB (this_type) = 1;
17694 set_die_type (die, this_type, cu);
17695 target_type = die_type (die, cu);
17696 if (target_type != this_type)
17697 TYPE_TARGET_TYPE (this_type) = target_type;
17698 else
17700 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17701 spec and cause infinite loops in GDB. */
17702 complaint (_("Self-referential DW_TAG_typedef "
17703 "- DIE at %s [in module %s]"),
17704 sect_offset_str (die->sect_off), objfile_name (objfile));
17705 TYPE_TARGET_TYPE (this_type) = NULL;
17707 return this_type;
17710 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17711 (which may be different from NAME) to the architecture back-end to allow
17712 it to guess the correct format if necessary. */
17714 static struct type *
17715 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17716 const char *name_hint, enum bfd_endian byte_order)
17718 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17719 const struct floatformat **format;
17720 struct type *type;
17722 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17723 if (format)
17724 type = init_float_type (objfile, bits, name, format, byte_order);
17725 else
17726 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17728 return type;
17731 /* Allocate an integer type of size BITS and name NAME. */
17733 static struct type *
17734 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17735 int bits, int unsigned_p, const char *name)
17737 struct type *type;
17739 /* Versions of Intel's C Compiler generate an integer type called "void"
17740 instead of using DW_TAG_unspecified_type. This has been seen on
17741 at least versions 14, 17, and 18. */
17742 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17743 && strcmp (name, "void") == 0)
17744 type = objfile_type (objfile)->builtin_void;
17745 else
17746 type = init_integer_type (objfile, bits, unsigned_p, name);
17748 return type;
17751 /* Initialise and return a floating point type of size BITS suitable for
17752 use as a component of a complex number. The NAME_HINT is passed through
17753 when initialising the floating point type and is the name of the complex
17754 type.
17756 As DWARF doesn't currently provide an explicit name for the components
17757 of a complex number, but it can be helpful to have these components
17758 named, we try to select a suitable name based on the size of the
17759 component. */
17760 static struct type *
17761 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17762 struct objfile *objfile,
17763 int bits, const char *name_hint,
17764 enum bfd_endian byte_order)
17766 gdbarch *gdbarch = get_objfile_arch (objfile);
17767 struct type *tt = nullptr;
17769 /* Try to find a suitable floating point builtin type of size BITS.
17770 We're going to use the name of this type as the name for the complex
17771 target type that we are about to create. */
17772 switch (cu->language)
17774 case language_fortran:
17775 switch (bits)
17777 case 32:
17778 tt = builtin_f_type (gdbarch)->builtin_real;
17779 break;
17780 case 64:
17781 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17782 break;
17783 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17784 case 128:
17785 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17786 break;
17788 break;
17789 default:
17790 switch (bits)
17792 case 32:
17793 tt = builtin_type (gdbarch)->builtin_float;
17794 break;
17795 case 64:
17796 tt = builtin_type (gdbarch)->builtin_double;
17797 break;
17798 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17799 case 128:
17800 tt = builtin_type (gdbarch)->builtin_long_double;
17801 break;
17803 break;
17806 /* If the type we found doesn't match the size we were looking for, then
17807 pretend we didn't find a type at all, the complex target type we
17808 create will then be nameless. */
17809 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17810 tt = nullptr;
17812 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17813 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17816 /* Find a representation of a given base type and install
17817 it in the TYPE field of the die. */
17819 static struct type *
17820 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17823 struct type *type;
17824 struct attribute *attr;
17825 int encoding = 0, bits = 0;
17826 const char *name;
17827 gdbarch *arch;
17829 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17830 if (attr != nullptr)
17831 encoding = DW_UNSND (attr);
17832 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17833 if (attr != nullptr)
17834 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17835 name = dwarf2_name (die, cu);
17836 if (!name)
17837 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17839 arch = get_objfile_arch (objfile);
17840 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17842 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17843 if (attr)
17845 int endianity = DW_UNSND (attr);
17847 switch (endianity)
17849 case DW_END_big:
17850 byte_order = BFD_ENDIAN_BIG;
17851 break;
17852 case DW_END_little:
17853 byte_order = BFD_ENDIAN_LITTLE;
17854 break;
17855 default:
17856 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17857 break;
17861 switch (encoding)
17863 case DW_ATE_address:
17864 /* Turn DW_ATE_address into a void * pointer. */
17865 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17866 type = init_pointer_type (objfile, bits, name, type);
17867 break;
17868 case DW_ATE_boolean:
17869 type = init_boolean_type (objfile, bits, 1, name);
17870 break;
17871 case DW_ATE_complex_float:
17872 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17873 byte_order);
17874 type = init_complex_type (objfile, name, type);
17875 break;
17876 case DW_ATE_decimal_float:
17877 type = init_decfloat_type (objfile, bits, name);
17878 break;
17879 case DW_ATE_float:
17880 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17881 break;
17882 case DW_ATE_signed:
17883 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17884 break;
17885 case DW_ATE_unsigned:
17886 if (cu->language == language_fortran
17887 && name
17888 && startswith (name, "character("))
17889 type = init_character_type (objfile, bits, 1, name);
17890 else
17891 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17892 break;
17893 case DW_ATE_signed_char:
17894 if (cu->language == language_ada || cu->language == language_m2
17895 || cu->language == language_pascal
17896 || cu->language == language_fortran)
17897 type = init_character_type (objfile, bits, 0, name);
17898 else
17899 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17900 break;
17901 case DW_ATE_unsigned_char:
17902 if (cu->language == language_ada || cu->language == language_m2
17903 || cu->language == language_pascal
17904 || cu->language == language_fortran
17905 || cu->language == language_rust)
17906 type = init_character_type (objfile, bits, 1, name);
17907 else
17908 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17909 break;
17910 case DW_ATE_UTF:
17912 if (bits == 16)
17913 type = builtin_type (arch)->builtin_char16;
17914 else if (bits == 32)
17915 type = builtin_type (arch)->builtin_char32;
17916 else
17918 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17919 bits);
17920 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17922 return set_die_type (die, type, cu);
17924 break;
17926 default:
17927 complaint (_("unsupported DW_AT_encoding: '%s'"),
17928 dwarf_type_encoding_name (encoding));
17929 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17930 break;
17933 if (name && strcmp (name, "char") == 0)
17934 TYPE_NOSIGN (type) = 1;
17936 maybe_set_alignment (cu, die, type);
17938 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17940 return set_die_type (die, type, cu);
17943 /* Parse dwarf attribute if it's a block, reference or constant and put the
17944 resulting value of the attribute into struct bound_prop.
17945 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17947 static int
17948 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17949 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17950 struct type *default_type)
17952 struct dwarf2_property_baton *baton;
17953 struct obstack *obstack
17954 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17956 gdb_assert (default_type != NULL);
17958 if (attr == NULL || prop == NULL)
17959 return 0;
17961 if (attr_form_is_block (attr))
17963 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17964 baton->property_type = default_type;
17965 baton->locexpr.per_cu = cu->per_cu;
17966 baton->locexpr.size = DW_BLOCK (attr)->size;
17967 baton->locexpr.data = DW_BLOCK (attr)->data;
17968 switch (attr->name)
17970 case DW_AT_string_length:
17971 baton->locexpr.is_reference = true;
17972 break;
17973 default:
17974 baton->locexpr.is_reference = false;
17975 break;
17977 prop->data.baton = baton;
17978 prop->kind = PROP_LOCEXPR;
17979 gdb_assert (prop->data.baton != NULL);
17981 else if (attr_form_is_ref (attr))
17983 struct dwarf2_cu *target_cu = cu;
17984 struct die_info *target_die;
17985 struct attribute *target_attr;
17987 target_die = follow_die_ref (die, attr, &target_cu);
17988 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17989 if (target_attr == NULL)
17990 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17991 target_cu);
17992 if (target_attr == NULL)
17993 return 0;
17995 switch (target_attr->name)
17997 case DW_AT_location:
17998 if (attr_form_is_section_offset (target_attr))
18000 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18001 baton->property_type = die_type (target_die, target_cu);
18002 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18003 prop->data.baton = baton;
18004 prop->kind = PROP_LOCLIST;
18005 gdb_assert (prop->data.baton != NULL);
18007 else if (attr_form_is_block (target_attr))
18009 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18010 baton->property_type = die_type (target_die, target_cu);
18011 baton->locexpr.per_cu = cu->per_cu;
18012 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18013 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18014 baton->locexpr.is_reference = true;
18015 prop->data.baton = baton;
18016 prop->kind = PROP_LOCEXPR;
18017 gdb_assert (prop->data.baton != NULL);
18019 else
18021 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18022 "dynamic property");
18023 return 0;
18025 break;
18026 case DW_AT_data_member_location:
18028 LONGEST offset;
18030 if (!handle_data_member_location (target_die, target_cu,
18031 &offset))
18032 return 0;
18034 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18035 baton->property_type = read_type_die (target_die->parent,
18036 target_cu);
18037 baton->offset_info.offset = offset;
18038 baton->offset_info.type = die_type (target_die, target_cu);
18039 prop->data.baton = baton;
18040 prop->kind = PROP_ADDR_OFFSET;
18041 break;
18045 else if (attr_form_is_constant (attr))
18047 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18048 prop->kind = PROP_CONST;
18050 else
18052 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18053 dwarf2_name (die, cu));
18054 return 0;
18057 return 1;
18060 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18061 UNSIGNED_P controls if the integer is unsigned or not. */
18063 static struct type *
18064 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18065 int size_in_bytes, bool unsigned_p)
18067 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18068 struct type *int_type;
18070 /* Helper macro to examine the various builtin types. */
18071 #define TRY_TYPE(F) \
18072 int_type = (unsigned_p \
18073 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18074 : objfile_type (objfile)->builtin_ ## F); \
18075 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18076 return int_type
18078 TRY_TYPE (char);
18079 TRY_TYPE (short);
18080 TRY_TYPE (int);
18081 TRY_TYPE (long);
18082 TRY_TYPE (long_long);
18084 #undef TRY_TYPE
18086 gdb_assert_not_reached ("unable to find suitable integer type");
18089 /* Find an integer type the same size as the address size given in the
18090 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18091 is unsigned or not. */
18093 static struct type *
18094 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18095 bool unsigned_p)
18097 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18098 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18101 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18102 present (which is valid) then compute the default type based on the
18103 compilation units address size. */
18105 static struct type *
18106 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18108 struct type *index_type = die_type (die, cu);
18110 /* Dwarf-2 specifications explicitly allows to create subrange types
18111 without specifying a base type.
18112 In that case, the base type must be set to the type of
18113 the lower bound, upper bound or count, in that order, if any of these
18114 three attributes references an object that has a type.
18115 If no base type is found, the Dwarf-2 specifications say that
18116 a signed integer type of size equal to the size of an address should
18117 be used.
18118 For the following C code: `extern char gdb_int [];'
18119 GCC produces an empty range DIE.
18120 FIXME: muller/2010-05-28: Possible references to object for low bound,
18121 high bound or count are not yet handled by this code. */
18122 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18123 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18125 return index_type;
18128 /* Read the given DW_AT_subrange DIE. */
18130 static struct type *
18131 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18133 struct type *base_type, *orig_base_type;
18134 struct type *range_type;
18135 struct attribute *attr;
18136 struct dynamic_prop low, high;
18137 int low_default_is_valid;
18138 int high_bound_is_count = 0;
18139 const char *name;
18140 ULONGEST negative_mask;
18142 orig_base_type = read_subrange_index_type (die, cu);
18144 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18145 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18146 creating the range type, but we use the result of check_typedef
18147 when examining properties of the type. */
18148 base_type = check_typedef (orig_base_type);
18150 /* The die_type call above may have already set the type for this DIE. */
18151 range_type = get_die_type (die, cu);
18152 if (range_type)
18153 return range_type;
18155 low.kind = PROP_CONST;
18156 high.kind = PROP_CONST;
18157 high.data.const_val = 0;
18159 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18160 omitting DW_AT_lower_bound. */
18161 switch (cu->language)
18163 case language_c:
18164 case language_cplus:
18165 low.data.const_val = 0;
18166 low_default_is_valid = 1;
18167 break;
18168 case language_fortran:
18169 low.data.const_val = 1;
18170 low_default_is_valid = 1;
18171 break;
18172 case language_d:
18173 case language_objc:
18174 case language_rust:
18175 low.data.const_val = 0;
18176 low_default_is_valid = (cu->header.version >= 4);
18177 break;
18178 case language_ada:
18179 case language_m2:
18180 case language_pascal:
18181 low.data.const_val = 1;
18182 low_default_is_valid = (cu->header.version >= 4);
18183 break;
18184 default:
18185 low.data.const_val = 0;
18186 low_default_is_valid = 0;
18187 break;
18190 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18191 if (attr != nullptr)
18192 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18193 else if (!low_default_is_valid)
18194 complaint (_("Missing DW_AT_lower_bound "
18195 "- DIE at %s [in module %s]"),
18196 sect_offset_str (die->sect_off),
18197 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18199 struct attribute *attr_ub, *attr_count;
18200 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18201 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18203 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18204 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18206 /* If bounds are constant do the final calculation here. */
18207 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18208 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18209 else
18210 high_bound_is_count = 1;
18212 else
18214 if (attr_ub != NULL)
18215 complaint (_("Unresolved DW_AT_upper_bound "
18216 "- DIE at %s [in module %s]"),
18217 sect_offset_str (die->sect_off),
18218 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18219 if (attr_count != NULL)
18220 complaint (_("Unresolved DW_AT_count "
18221 "- DIE at %s [in module %s]"),
18222 sect_offset_str (die->sect_off),
18223 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18227 LONGEST bias = 0;
18228 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18229 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18230 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18232 /* Normally, the DWARF producers are expected to use a signed
18233 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18234 But this is unfortunately not always the case, as witnessed
18235 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18236 is used instead. To work around that ambiguity, we treat
18237 the bounds as signed, and thus sign-extend their values, when
18238 the base type is signed. */
18239 negative_mask =
18240 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18241 if (low.kind == PROP_CONST
18242 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18243 low.data.const_val |= negative_mask;
18244 if (high.kind == PROP_CONST
18245 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18246 high.data.const_val |= negative_mask;
18248 /* Check for bit and byte strides. */
18249 struct dynamic_prop byte_stride_prop;
18250 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18251 if (attr_byte_stride != nullptr)
18253 struct type *prop_type
18254 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18255 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18256 prop_type);
18259 struct dynamic_prop bit_stride_prop;
18260 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18261 if (attr_bit_stride != nullptr)
18263 /* It only makes sense to have either a bit or byte stride. */
18264 if (attr_byte_stride != nullptr)
18266 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18267 "- DIE at %s [in module %s]"),
18268 sect_offset_str (die->sect_off),
18269 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18270 attr_bit_stride = nullptr;
18272 else
18274 struct type *prop_type
18275 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18276 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18277 prop_type);
18281 if (attr_byte_stride != nullptr
18282 || attr_bit_stride != nullptr)
18284 bool byte_stride_p = (attr_byte_stride != nullptr);
18285 struct dynamic_prop *stride
18286 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18288 range_type
18289 = create_range_type_with_stride (NULL, orig_base_type, &low,
18290 &high, bias, stride, byte_stride_p);
18292 else
18293 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18295 if (high_bound_is_count)
18296 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18298 /* Ada expects an empty array on no boundary attributes. */
18299 if (attr == NULL && cu->language != language_ada)
18300 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18302 name = dwarf2_name (die, cu);
18303 if (name)
18304 TYPE_NAME (range_type) = name;
18306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18307 if (attr != nullptr)
18308 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18310 maybe_set_alignment (cu, die, range_type);
18312 set_die_type (die, range_type, cu);
18314 /* set_die_type should be already done. */
18315 set_descriptive_type (range_type, die, cu);
18317 return range_type;
18320 static struct type *
18321 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18323 struct type *type;
18325 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18326 NULL);
18327 TYPE_NAME (type) = dwarf2_name (die, cu);
18329 /* In Ada, an unspecified type is typically used when the description
18330 of the type is deferred to a different unit. When encountering
18331 such a type, we treat it as a stub, and try to resolve it later on,
18332 when needed. */
18333 if (cu->language == language_ada)
18334 TYPE_STUB (type) = 1;
18336 return set_die_type (die, type, cu);
18339 /* Read a single die and all its descendents. Set the die's sibling
18340 field to NULL; set other fields in the die correctly, and set all
18341 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18342 location of the info_ptr after reading all of those dies. PARENT
18343 is the parent of the die in question. */
18345 static struct die_info *
18346 read_die_and_children (const struct die_reader_specs *reader,
18347 const gdb_byte *info_ptr,
18348 const gdb_byte **new_info_ptr,
18349 struct die_info *parent)
18351 struct die_info *die;
18352 const gdb_byte *cur_ptr;
18353 int has_children;
18355 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18356 if (die == NULL)
18358 *new_info_ptr = cur_ptr;
18359 return NULL;
18361 store_in_ref_table (die, reader->cu);
18363 if (has_children)
18364 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18365 else
18367 die->child = NULL;
18368 *new_info_ptr = cur_ptr;
18371 die->sibling = NULL;
18372 die->parent = parent;
18373 return die;
18376 /* Read a die, all of its descendents, and all of its siblings; set
18377 all of the fields of all of the dies correctly. Arguments are as
18378 in read_die_and_children. */
18380 static struct die_info *
18381 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18382 const gdb_byte *info_ptr,
18383 const gdb_byte **new_info_ptr,
18384 struct die_info *parent)
18386 struct die_info *first_die, *last_sibling;
18387 const gdb_byte *cur_ptr;
18389 cur_ptr = info_ptr;
18390 first_die = last_sibling = NULL;
18392 while (1)
18394 struct die_info *die
18395 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18397 if (die == NULL)
18399 *new_info_ptr = cur_ptr;
18400 return first_die;
18403 if (!first_die)
18404 first_die = die;
18405 else
18406 last_sibling->sibling = die;
18408 last_sibling = die;
18412 /* Read a die, all of its descendents, and all of its siblings; set
18413 all of the fields of all of the dies correctly. Arguments are as
18414 in read_die_and_children.
18415 This the main entry point for reading a DIE and all its children. */
18417 static struct die_info *
18418 read_die_and_siblings (const struct die_reader_specs *reader,
18419 const gdb_byte *info_ptr,
18420 const gdb_byte **new_info_ptr,
18421 struct die_info *parent)
18423 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18424 new_info_ptr, parent);
18426 if (dwarf_die_debug)
18428 fprintf_unfiltered (gdb_stdlog,
18429 "Read die from %s@0x%x of %s:\n",
18430 get_section_name (reader->die_section),
18431 (unsigned) (info_ptr - reader->die_section->buffer),
18432 bfd_get_filename (reader->abfd));
18433 dump_die (die, dwarf_die_debug);
18436 return die;
18439 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18440 attributes.
18441 The caller is responsible for filling in the extra attributes
18442 and updating (*DIEP)->num_attrs.
18443 Set DIEP to point to a newly allocated die with its information,
18444 except for its child, sibling, and parent fields.
18445 Set HAS_CHILDREN to tell whether the die has children or not. */
18447 static const gdb_byte *
18448 read_full_die_1 (const struct die_reader_specs *reader,
18449 struct die_info **diep, const gdb_byte *info_ptr,
18450 int *has_children, int num_extra_attrs)
18452 unsigned int abbrev_number, bytes_read, i;
18453 struct abbrev_info *abbrev;
18454 struct die_info *die;
18455 struct dwarf2_cu *cu = reader->cu;
18456 bfd *abfd = reader->abfd;
18458 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18459 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18460 info_ptr += bytes_read;
18461 if (!abbrev_number)
18463 *diep = NULL;
18464 *has_children = 0;
18465 return info_ptr;
18468 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18469 if (!abbrev)
18470 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18471 abbrev_number,
18472 bfd_get_filename (abfd));
18474 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18475 die->sect_off = sect_off;
18476 die->tag = abbrev->tag;
18477 die->abbrev = abbrev_number;
18479 /* Make the result usable.
18480 The caller needs to update num_attrs after adding the extra
18481 attributes. */
18482 die->num_attrs = abbrev->num_attrs;
18484 for (i = 0; i < abbrev->num_attrs; ++i)
18485 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18486 info_ptr);
18488 *diep = die;
18489 *has_children = abbrev->has_children;
18490 return info_ptr;
18493 /* Read a die and all its attributes.
18494 Set DIEP to point to a newly allocated die with its information,
18495 except for its child, sibling, and parent fields.
18496 Set HAS_CHILDREN to tell whether the die has children or not. */
18498 static const gdb_byte *
18499 read_full_die (const struct die_reader_specs *reader,
18500 struct die_info **diep, const gdb_byte *info_ptr,
18501 int *has_children)
18503 const gdb_byte *result;
18505 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18507 if (dwarf_die_debug)
18509 fprintf_unfiltered (gdb_stdlog,
18510 "Read die from %s@0x%x of %s:\n",
18511 get_section_name (reader->die_section),
18512 (unsigned) (info_ptr - reader->die_section->buffer),
18513 bfd_get_filename (reader->abfd));
18514 dump_die (*diep, dwarf_die_debug);
18517 return result;
18520 /* Abbreviation tables.
18522 In DWARF version 2, the description of the debugging information is
18523 stored in a separate .debug_abbrev section. Before we read any
18524 dies from a section we read in all abbreviations and install them
18525 in a hash table. */
18527 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18529 struct abbrev_info *
18530 abbrev_table::alloc_abbrev ()
18532 struct abbrev_info *abbrev;
18534 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18535 memset (abbrev, 0, sizeof (struct abbrev_info));
18537 return abbrev;
18540 /* Add an abbreviation to the table. */
18542 void
18543 abbrev_table::add_abbrev (unsigned int abbrev_number,
18544 struct abbrev_info *abbrev)
18546 unsigned int hash_number;
18548 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18549 abbrev->next = m_abbrevs[hash_number];
18550 m_abbrevs[hash_number] = abbrev;
18553 /* Look up an abbrev in the table.
18554 Returns NULL if the abbrev is not found. */
18556 struct abbrev_info *
18557 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18559 unsigned int hash_number;
18560 struct abbrev_info *abbrev;
18562 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18563 abbrev = m_abbrevs[hash_number];
18565 while (abbrev)
18567 if (abbrev->number == abbrev_number)
18568 return abbrev;
18569 abbrev = abbrev->next;
18571 return NULL;
18574 /* Read in an abbrev table. */
18576 static abbrev_table_up
18577 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18578 struct dwarf2_section_info *section,
18579 sect_offset sect_off)
18581 struct objfile *objfile = dwarf2_per_objfile->objfile;
18582 bfd *abfd = get_section_bfd_owner (section);
18583 const gdb_byte *abbrev_ptr;
18584 struct abbrev_info *cur_abbrev;
18585 unsigned int abbrev_number, bytes_read, abbrev_name;
18586 unsigned int abbrev_form;
18587 struct attr_abbrev *cur_attrs;
18588 unsigned int allocated_attrs;
18590 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18592 dwarf2_read_section (objfile, section);
18593 abbrev_ptr = section->buffer + to_underlying (sect_off);
18594 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18595 abbrev_ptr += bytes_read;
18597 allocated_attrs = ATTR_ALLOC_CHUNK;
18598 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18600 /* Loop until we reach an abbrev number of 0. */
18601 while (abbrev_number)
18603 cur_abbrev = abbrev_table->alloc_abbrev ();
18605 /* read in abbrev header */
18606 cur_abbrev->number = abbrev_number;
18607 cur_abbrev->tag
18608 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18609 abbrev_ptr += bytes_read;
18610 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18611 abbrev_ptr += 1;
18613 /* now read in declarations */
18614 for (;;)
18616 LONGEST implicit_const;
18618 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18619 abbrev_ptr += bytes_read;
18620 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18621 abbrev_ptr += bytes_read;
18622 if (abbrev_form == DW_FORM_implicit_const)
18624 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18625 &bytes_read);
18626 abbrev_ptr += bytes_read;
18628 else
18630 /* Initialize it due to a false compiler warning. */
18631 implicit_const = -1;
18634 if (abbrev_name == 0)
18635 break;
18637 if (cur_abbrev->num_attrs == allocated_attrs)
18639 allocated_attrs += ATTR_ALLOC_CHUNK;
18640 cur_attrs
18641 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18644 cur_attrs[cur_abbrev->num_attrs].name
18645 = (enum dwarf_attribute) abbrev_name;
18646 cur_attrs[cur_abbrev->num_attrs].form
18647 = (enum dwarf_form) abbrev_form;
18648 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18649 ++cur_abbrev->num_attrs;
18652 cur_abbrev->attrs =
18653 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18654 cur_abbrev->num_attrs);
18655 memcpy (cur_abbrev->attrs, cur_attrs,
18656 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18658 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18660 /* Get next abbreviation.
18661 Under Irix6 the abbreviations for a compilation unit are not
18662 always properly terminated with an abbrev number of 0.
18663 Exit loop if we encounter an abbreviation which we have
18664 already read (which means we are about to read the abbreviations
18665 for the next compile unit) or if the end of the abbreviation
18666 table is reached. */
18667 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18668 break;
18669 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18670 abbrev_ptr += bytes_read;
18671 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18672 break;
18675 xfree (cur_attrs);
18676 return abbrev_table;
18679 /* Returns nonzero if TAG represents a type that we might generate a partial
18680 symbol for. */
18682 static int
18683 is_type_tag_for_partial (int tag)
18685 switch (tag)
18687 #if 0
18688 /* Some types that would be reasonable to generate partial symbols for,
18689 that we don't at present. */
18690 case DW_TAG_array_type:
18691 case DW_TAG_file_type:
18692 case DW_TAG_ptr_to_member_type:
18693 case DW_TAG_set_type:
18694 case DW_TAG_string_type:
18695 case DW_TAG_subroutine_type:
18696 #endif
18697 case DW_TAG_base_type:
18698 case DW_TAG_class_type:
18699 case DW_TAG_interface_type:
18700 case DW_TAG_enumeration_type:
18701 case DW_TAG_structure_type:
18702 case DW_TAG_subrange_type:
18703 case DW_TAG_typedef:
18704 case DW_TAG_union_type:
18705 return 1;
18706 default:
18707 return 0;
18711 /* Load all DIEs that are interesting for partial symbols into memory. */
18713 static struct partial_die_info *
18714 load_partial_dies (const struct die_reader_specs *reader,
18715 const gdb_byte *info_ptr, int building_psymtab)
18717 struct dwarf2_cu *cu = reader->cu;
18718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18719 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18720 unsigned int bytes_read;
18721 unsigned int load_all = 0;
18722 int nesting_level = 1;
18724 parent_die = NULL;
18725 last_die = NULL;
18727 gdb_assert (cu->per_cu != NULL);
18728 if (cu->per_cu->load_all_dies)
18729 load_all = 1;
18731 cu->partial_dies
18732 = htab_create_alloc_ex (cu->header.length / 12,
18733 partial_die_hash,
18734 partial_die_eq,
18735 NULL,
18736 &cu->comp_unit_obstack,
18737 hashtab_obstack_allocate,
18738 dummy_obstack_deallocate);
18740 while (1)
18742 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18744 /* A NULL abbrev means the end of a series of children. */
18745 if (abbrev == NULL)
18747 if (--nesting_level == 0)
18748 return first_die;
18750 info_ptr += bytes_read;
18751 last_die = parent_die;
18752 parent_die = parent_die->die_parent;
18753 continue;
18756 /* Check for template arguments. We never save these; if
18757 they're seen, we just mark the parent, and go on our way. */
18758 if (parent_die != NULL
18759 && cu->language == language_cplus
18760 && (abbrev->tag == DW_TAG_template_type_param
18761 || abbrev->tag == DW_TAG_template_value_param))
18763 parent_die->has_template_arguments = 1;
18765 if (!load_all)
18767 /* We don't need a partial DIE for the template argument. */
18768 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18769 continue;
18773 /* We only recurse into c++ subprograms looking for template arguments.
18774 Skip their other children. */
18775 if (!load_all
18776 && cu->language == language_cplus
18777 && parent_die != NULL
18778 && parent_die->tag == DW_TAG_subprogram)
18780 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18781 continue;
18784 /* Check whether this DIE is interesting enough to save. Normally
18785 we would not be interested in members here, but there may be
18786 later variables referencing them via DW_AT_specification (for
18787 static members). */
18788 if (!load_all
18789 && !is_type_tag_for_partial (abbrev->tag)
18790 && abbrev->tag != DW_TAG_constant
18791 && abbrev->tag != DW_TAG_enumerator
18792 && abbrev->tag != DW_TAG_subprogram
18793 && abbrev->tag != DW_TAG_inlined_subroutine
18794 && abbrev->tag != DW_TAG_lexical_block
18795 && abbrev->tag != DW_TAG_variable
18796 && abbrev->tag != DW_TAG_namespace
18797 && abbrev->tag != DW_TAG_module
18798 && abbrev->tag != DW_TAG_member
18799 && abbrev->tag != DW_TAG_imported_unit
18800 && abbrev->tag != DW_TAG_imported_declaration)
18802 /* Otherwise we skip to the next sibling, if any. */
18803 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18804 continue;
18807 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18808 abbrev);
18810 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18812 /* This two-pass algorithm for processing partial symbols has a
18813 high cost in cache pressure. Thus, handle some simple cases
18814 here which cover the majority of C partial symbols. DIEs
18815 which neither have specification tags in them, nor could have
18816 specification tags elsewhere pointing at them, can simply be
18817 processed and discarded.
18819 This segment is also optional; scan_partial_symbols and
18820 add_partial_symbol will handle these DIEs if we chain
18821 them in normally. When compilers which do not emit large
18822 quantities of duplicate debug information are more common,
18823 this code can probably be removed. */
18825 /* Any complete simple types at the top level (pretty much all
18826 of them, for a language without namespaces), can be processed
18827 directly. */
18828 if (parent_die == NULL
18829 && pdi.has_specification == 0
18830 && pdi.is_declaration == 0
18831 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18832 || pdi.tag == DW_TAG_base_type
18833 || pdi.tag == DW_TAG_subrange_type))
18835 if (building_psymtab && pdi.name != NULL)
18836 add_psymbol_to_list (pdi.name, false,
18837 VAR_DOMAIN, LOC_TYPEDEF, -1,
18838 psymbol_placement::STATIC,
18839 0, cu->language, objfile);
18840 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18841 continue;
18844 /* The exception for DW_TAG_typedef with has_children above is
18845 a workaround of GCC PR debug/47510. In the case of this complaint
18846 type_name_or_error will error on such types later.
18848 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18849 it could not find the child DIEs referenced later, this is checked
18850 above. In correct DWARF DW_TAG_typedef should have no children. */
18852 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18853 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18854 "- DIE at %s [in module %s]"),
18855 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18857 /* If we're at the second level, and we're an enumerator, and
18858 our parent has no specification (meaning possibly lives in a
18859 namespace elsewhere), then we can add the partial symbol now
18860 instead of queueing it. */
18861 if (pdi.tag == DW_TAG_enumerator
18862 && parent_die != NULL
18863 && parent_die->die_parent == NULL
18864 && parent_die->tag == DW_TAG_enumeration_type
18865 && parent_die->has_specification == 0)
18867 if (pdi.name == NULL)
18868 complaint (_("malformed enumerator DIE ignored"));
18869 else if (building_psymtab)
18870 add_psymbol_to_list (pdi.name, false,
18871 VAR_DOMAIN, LOC_CONST, -1,
18872 cu->language == language_cplus
18873 ? psymbol_placement::GLOBAL
18874 : psymbol_placement::STATIC,
18875 0, cu->language, objfile);
18877 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18878 continue;
18881 struct partial_die_info *part_die
18882 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18884 /* We'll save this DIE so link it in. */
18885 part_die->die_parent = parent_die;
18886 part_die->die_sibling = NULL;
18887 part_die->die_child = NULL;
18889 if (last_die && last_die == parent_die)
18890 last_die->die_child = part_die;
18891 else if (last_die)
18892 last_die->die_sibling = part_die;
18894 last_die = part_die;
18896 if (first_die == NULL)
18897 first_die = part_die;
18899 /* Maybe add the DIE to the hash table. Not all DIEs that we
18900 find interesting need to be in the hash table, because we
18901 also have the parent/sibling/child chains; only those that we
18902 might refer to by offset later during partial symbol reading.
18904 For now this means things that might have be the target of a
18905 DW_AT_specification, DW_AT_abstract_origin, or
18906 DW_AT_extension. DW_AT_extension will refer only to
18907 namespaces; DW_AT_abstract_origin refers to functions (and
18908 many things under the function DIE, but we do not recurse
18909 into function DIEs during partial symbol reading) and
18910 possibly variables as well; DW_AT_specification refers to
18911 declarations. Declarations ought to have the DW_AT_declaration
18912 flag. It happens that GCC forgets to put it in sometimes, but
18913 only for functions, not for types.
18915 Adding more things than necessary to the hash table is harmless
18916 except for the performance cost. Adding too few will result in
18917 wasted time in find_partial_die, when we reread the compilation
18918 unit with load_all_dies set. */
18920 if (load_all
18921 || abbrev->tag == DW_TAG_constant
18922 || abbrev->tag == DW_TAG_subprogram
18923 || abbrev->tag == DW_TAG_variable
18924 || abbrev->tag == DW_TAG_namespace
18925 || part_die->is_declaration)
18927 void **slot;
18929 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18930 to_underlying (part_die->sect_off),
18931 INSERT);
18932 *slot = part_die;
18935 /* For some DIEs we want to follow their children (if any). For C
18936 we have no reason to follow the children of structures; for other
18937 languages we have to, so that we can get at method physnames
18938 to infer fully qualified class names, for DW_AT_specification,
18939 and for C++ template arguments. For C++, we also look one level
18940 inside functions to find template arguments (if the name of the
18941 function does not already contain the template arguments).
18943 For Ada and Fortran, we need to scan the children of subprograms
18944 and lexical blocks as well because these languages allow the
18945 definition of nested entities that could be interesting for the
18946 debugger, such as nested subprograms for instance. */
18947 if (last_die->has_children
18948 && (load_all
18949 || last_die->tag == DW_TAG_namespace
18950 || last_die->tag == DW_TAG_module
18951 || last_die->tag == DW_TAG_enumeration_type
18952 || (cu->language == language_cplus
18953 && last_die->tag == DW_TAG_subprogram
18954 && (last_die->name == NULL
18955 || strchr (last_die->name, '<') == NULL))
18956 || (cu->language != language_c
18957 && (last_die->tag == DW_TAG_class_type
18958 || last_die->tag == DW_TAG_interface_type
18959 || last_die->tag == DW_TAG_structure_type
18960 || last_die->tag == DW_TAG_union_type))
18961 || ((cu->language == language_ada
18962 || cu->language == language_fortran)
18963 && (last_die->tag == DW_TAG_subprogram
18964 || last_die->tag == DW_TAG_lexical_block))))
18966 nesting_level++;
18967 parent_die = last_die;
18968 continue;
18971 /* Otherwise we skip to the next sibling, if any. */
18972 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18974 /* Back to the top, do it again. */
18978 partial_die_info::partial_die_info (sect_offset sect_off_,
18979 struct abbrev_info *abbrev)
18980 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18984 /* Read a minimal amount of information into the minimal die structure.
18985 INFO_PTR should point just after the initial uleb128 of a DIE. */
18987 const gdb_byte *
18988 partial_die_info::read (const struct die_reader_specs *reader,
18989 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18991 struct dwarf2_cu *cu = reader->cu;
18992 struct dwarf2_per_objfile *dwarf2_per_objfile
18993 = cu->per_cu->dwarf2_per_objfile;
18994 unsigned int i;
18995 int has_low_pc_attr = 0;
18996 int has_high_pc_attr = 0;
18997 int high_pc_relative = 0;
18999 for (i = 0; i < abbrev.num_attrs; ++i)
19001 struct attribute attr;
19003 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19005 /* Store the data if it is of an attribute we want to keep in a
19006 partial symbol table. */
19007 switch (attr.name)
19009 case DW_AT_name:
19010 switch (tag)
19012 case DW_TAG_compile_unit:
19013 case DW_TAG_partial_unit:
19014 case DW_TAG_type_unit:
19015 /* Compilation units have a DW_AT_name that is a filename, not
19016 a source language identifier. */
19017 case DW_TAG_enumeration_type:
19018 case DW_TAG_enumerator:
19019 /* These tags always have simple identifiers already; no need
19020 to canonicalize them. */
19021 name = DW_STRING (&attr);
19022 break;
19023 default:
19025 struct objfile *objfile = dwarf2_per_objfile->objfile;
19027 name
19028 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19029 &objfile->per_bfd->storage_obstack);
19031 break;
19033 break;
19034 case DW_AT_linkage_name:
19035 case DW_AT_MIPS_linkage_name:
19036 /* Note that both forms of linkage name might appear. We
19037 assume they will be the same, and we only store the last
19038 one we see. */
19039 linkage_name = DW_STRING (&attr);
19040 break;
19041 case DW_AT_low_pc:
19042 has_low_pc_attr = 1;
19043 lowpc = attr_value_as_address (&attr);
19044 break;
19045 case DW_AT_high_pc:
19046 has_high_pc_attr = 1;
19047 highpc = attr_value_as_address (&attr);
19048 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19049 high_pc_relative = 1;
19050 break;
19051 case DW_AT_location:
19052 /* Support the .debug_loc offsets. */
19053 if (attr_form_is_block (&attr))
19055 d.locdesc = DW_BLOCK (&attr);
19057 else if (attr_form_is_section_offset (&attr))
19059 dwarf2_complex_location_expr_complaint ();
19061 else
19063 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19064 "partial symbol information");
19066 break;
19067 case DW_AT_external:
19068 is_external = DW_UNSND (&attr);
19069 break;
19070 case DW_AT_declaration:
19071 is_declaration = DW_UNSND (&attr);
19072 break;
19073 case DW_AT_type:
19074 has_type = 1;
19075 break;
19076 case DW_AT_abstract_origin:
19077 case DW_AT_specification:
19078 case DW_AT_extension:
19079 has_specification = 1;
19080 spec_offset = dwarf2_get_ref_die_offset (&attr);
19081 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19082 || cu->per_cu->is_dwz);
19083 break;
19084 case DW_AT_sibling:
19085 /* Ignore absolute siblings, they might point outside of
19086 the current compile unit. */
19087 if (attr.form == DW_FORM_ref_addr)
19088 complaint (_("ignoring absolute DW_AT_sibling"));
19089 else
19091 const gdb_byte *buffer = reader->buffer;
19092 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19093 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19095 if (sibling_ptr < info_ptr)
19096 complaint (_("DW_AT_sibling points backwards"));
19097 else if (sibling_ptr > reader->buffer_end)
19098 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19099 else
19100 sibling = sibling_ptr;
19102 break;
19103 case DW_AT_byte_size:
19104 has_byte_size = 1;
19105 break;
19106 case DW_AT_const_value:
19107 has_const_value = 1;
19108 break;
19109 case DW_AT_calling_convention:
19110 /* DWARF doesn't provide a way to identify a program's source-level
19111 entry point. DW_AT_calling_convention attributes are only meant
19112 to describe functions' calling conventions.
19114 However, because it's a necessary piece of information in
19115 Fortran, and before DWARF 4 DW_CC_program was the only
19116 piece of debugging information whose definition refers to
19117 a 'main program' at all, several compilers marked Fortran
19118 main programs with DW_CC_program --- even when those
19119 functions use the standard calling conventions.
19121 Although DWARF now specifies a way to provide this
19122 information, we support this practice for backward
19123 compatibility. */
19124 if (DW_UNSND (&attr) == DW_CC_program
19125 && cu->language == language_fortran)
19126 main_subprogram = 1;
19127 break;
19128 case DW_AT_inline:
19129 if (DW_UNSND (&attr) == DW_INL_inlined
19130 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19131 may_be_inlined = 1;
19132 break;
19134 case DW_AT_import:
19135 if (tag == DW_TAG_imported_unit)
19137 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19138 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19139 || cu->per_cu->is_dwz);
19141 break;
19143 case DW_AT_main_subprogram:
19144 main_subprogram = DW_UNSND (&attr);
19145 break;
19147 case DW_AT_ranges:
19149 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19150 but that requires a full DIE, so instead we just
19151 reimplement it. */
19152 int need_ranges_base = tag != DW_TAG_compile_unit;
19153 unsigned int ranges_offset = (DW_UNSND (&attr)
19154 + (need_ranges_base
19155 ? cu->ranges_base
19156 : 0));
19158 /* Value of the DW_AT_ranges attribute is the offset in the
19159 .debug_ranges section. */
19160 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19161 nullptr))
19162 has_pc_info = 1;
19164 break;
19166 default:
19167 break;
19171 /* For Ada, if both the name and the linkage name appear, we prefer
19172 the latter. This lets "catch exception" work better, regardless
19173 of the order in which the name and linkage name were emitted.
19174 Really, though, this is just a workaround for the fact that gdb
19175 doesn't store both the name and the linkage name. */
19176 if (cu->language == language_ada && linkage_name != nullptr)
19177 name = linkage_name;
19179 if (high_pc_relative)
19180 highpc += lowpc;
19182 if (has_low_pc_attr && has_high_pc_attr)
19184 /* When using the GNU linker, .gnu.linkonce. sections are used to
19185 eliminate duplicate copies of functions and vtables and such.
19186 The linker will arbitrarily choose one and discard the others.
19187 The AT_*_pc values for such functions refer to local labels in
19188 these sections. If the section from that file was discarded, the
19189 labels are not in the output, so the relocs get a value of 0.
19190 If this is a discarded function, mark the pc bounds as invalid,
19191 so that GDB will ignore it. */
19192 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19194 struct objfile *objfile = dwarf2_per_objfile->objfile;
19195 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19197 complaint (_("DW_AT_low_pc %s is zero "
19198 "for DIE at %s [in module %s]"),
19199 paddress (gdbarch, lowpc),
19200 sect_offset_str (sect_off),
19201 objfile_name (objfile));
19203 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19204 else if (lowpc >= highpc)
19206 struct objfile *objfile = dwarf2_per_objfile->objfile;
19207 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19209 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19210 "for DIE at %s [in module %s]"),
19211 paddress (gdbarch, lowpc),
19212 paddress (gdbarch, highpc),
19213 sect_offset_str (sect_off),
19214 objfile_name (objfile));
19216 else
19217 has_pc_info = 1;
19220 return info_ptr;
19223 /* Find a cached partial DIE at OFFSET in CU. */
19225 struct partial_die_info *
19226 dwarf2_cu::find_partial_die (sect_offset sect_off)
19228 struct partial_die_info *lookup_die = NULL;
19229 struct partial_die_info part_die (sect_off);
19231 lookup_die = ((struct partial_die_info *)
19232 htab_find_with_hash (partial_dies, &part_die,
19233 to_underlying (sect_off)));
19235 return lookup_die;
19238 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19239 except in the case of .debug_types DIEs which do not reference
19240 outside their CU (they do however referencing other types via
19241 DW_FORM_ref_sig8). */
19243 static const struct cu_partial_die_info
19244 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19246 struct dwarf2_per_objfile *dwarf2_per_objfile
19247 = cu->per_cu->dwarf2_per_objfile;
19248 struct objfile *objfile = dwarf2_per_objfile->objfile;
19249 struct dwarf2_per_cu_data *per_cu = NULL;
19250 struct partial_die_info *pd = NULL;
19252 if (offset_in_dwz == cu->per_cu->is_dwz
19253 && offset_in_cu_p (&cu->header, sect_off))
19255 pd = cu->find_partial_die (sect_off);
19256 if (pd != NULL)
19257 return { cu, pd };
19258 /* We missed recording what we needed.
19259 Load all dies and try again. */
19260 per_cu = cu->per_cu;
19262 else
19264 /* TUs don't reference other CUs/TUs (except via type signatures). */
19265 if (cu->per_cu->is_debug_types)
19267 error (_("Dwarf Error: Type Unit at offset %s contains"
19268 " external reference to offset %s [in module %s].\n"),
19269 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19270 bfd_get_filename (objfile->obfd));
19272 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19273 dwarf2_per_objfile);
19275 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19276 load_partial_comp_unit (per_cu);
19278 per_cu->cu->last_used = 0;
19279 pd = per_cu->cu->find_partial_die (sect_off);
19282 /* If we didn't find it, and not all dies have been loaded,
19283 load them all and try again. */
19285 if (pd == NULL && per_cu->load_all_dies == 0)
19287 per_cu->load_all_dies = 1;
19289 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19290 THIS_CU->cu may already be in use. So we can't just free it and
19291 replace its DIEs with the ones we read in. Instead, we leave those
19292 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19293 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19294 set. */
19295 load_partial_comp_unit (per_cu);
19297 pd = per_cu->cu->find_partial_die (sect_off);
19300 if (pd == NULL)
19301 internal_error (__FILE__, __LINE__,
19302 _("could not find partial DIE %s "
19303 "in cache [from module %s]\n"),
19304 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19305 return { per_cu->cu, pd };
19308 /* See if we can figure out if the class lives in a namespace. We do
19309 this by looking for a member function; its demangled name will
19310 contain namespace info, if there is any. */
19312 static void
19313 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19314 struct dwarf2_cu *cu)
19316 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19317 what template types look like, because the demangler
19318 frequently doesn't give the same name as the debug info. We
19319 could fix this by only using the demangled name to get the
19320 prefix (but see comment in read_structure_type). */
19322 struct partial_die_info *real_pdi;
19323 struct partial_die_info *child_pdi;
19325 /* If this DIE (this DIE's specification, if any) has a parent, then
19326 we should not do this. We'll prepend the parent's fully qualified
19327 name when we create the partial symbol. */
19329 real_pdi = struct_pdi;
19330 while (real_pdi->has_specification)
19332 auto res = find_partial_die (real_pdi->spec_offset,
19333 real_pdi->spec_is_dwz, cu);
19334 real_pdi = res.pdi;
19335 cu = res.cu;
19338 if (real_pdi->die_parent != NULL)
19339 return;
19341 for (child_pdi = struct_pdi->die_child;
19342 child_pdi != NULL;
19343 child_pdi = child_pdi->die_sibling)
19345 if (child_pdi->tag == DW_TAG_subprogram
19346 && child_pdi->linkage_name != NULL)
19348 char *actual_class_name
19349 = language_class_name_from_physname (cu->language_defn,
19350 child_pdi->linkage_name);
19351 if (actual_class_name != NULL)
19353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19354 struct_pdi->name
19355 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19356 actual_class_name);
19357 xfree (actual_class_name);
19359 break;
19364 void
19365 partial_die_info::fixup (struct dwarf2_cu *cu)
19367 /* Once we've fixed up a die, there's no point in doing so again.
19368 This also avoids a memory leak if we were to call
19369 guess_partial_die_structure_name multiple times. */
19370 if (fixup_called)
19371 return;
19373 /* If we found a reference attribute and the DIE has no name, try
19374 to find a name in the referred to DIE. */
19376 if (name == NULL && has_specification)
19378 struct partial_die_info *spec_die;
19380 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19381 spec_die = res.pdi;
19382 cu = res.cu;
19384 spec_die->fixup (cu);
19386 if (spec_die->name)
19388 name = spec_die->name;
19390 /* Copy DW_AT_external attribute if it is set. */
19391 if (spec_die->is_external)
19392 is_external = spec_die->is_external;
19396 /* Set default names for some unnamed DIEs. */
19398 if (name == NULL && tag == DW_TAG_namespace)
19399 name = CP_ANONYMOUS_NAMESPACE_STR;
19401 /* If there is no parent die to provide a namespace, and there are
19402 children, see if we can determine the namespace from their linkage
19403 name. */
19404 if (cu->language == language_cplus
19405 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19406 && die_parent == NULL
19407 && has_children
19408 && (tag == DW_TAG_class_type
19409 || tag == DW_TAG_structure_type
19410 || tag == DW_TAG_union_type))
19411 guess_partial_die_structure_name (this, cu);
19413 /* GCC might emit a nameless struct or union that has a linkage
19414 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19415 if (name == NULL
19416 && (tag == DW_TAG_class_type
19417 || tag == DW_TAG_interface_type
19418 || tag == DW_TAG_structure_type
19419 || tag == DW_TAG_union_type)
19420 && linkage_name != NULL)
19422 char *demangled;
19424 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19425 if (demangled)
19427 const char *base;
19429 /* Strip any leading namespaces/classes, keep only the base name.
19430 DW_AT_name for named DIEs does not contain the prefixes. */
19431 base = strrchr (demangled, ':');
19432 if (base && base > demangled && base[-1] == ':')
19433 base++;
19434 else
19435 base = demangled;
19437 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19438 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19439 xfree (demangled);
19443 fixup_called = 1;
19446 /* Read an attribute value described by an attribute form. */
19448 static const gdb_byte *
19449 read_attribute_value (const struct die_reader_specs *reader,
19450 struct attribute *attr, unsigned form,
19451 LONGEST implicit_const, const gdb_byte *info_ptr)
19453 struct dwarf2_cu *cu = reader->cu;
19454 struct dwarf2_per_objfile *dwarf2_per_objfile
19455 = cu->per_cu->dwarf2_per_objfile;
19456 struct objfile *objfile = dwarf2_per_objfile->objfile;
19457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19458 bfd *abfd = reader->abfd;
19459 struct comp_unit_head *cu_header = &cu->header;
19460 unsigned int bytes_read;
19461 struct dwarf_block *blk;
19463 attr->form = (enum dwarf_form) form;
19464 switch (form)
19466 case DW_FORM_ref_addr:
19467 if (cu->header.version == 2)
19468 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19469 else
19470 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19471 &cu->header, &bytes_read);
19472 info_ptr += bytes_read;
19473 break;
19474 case DW_FORM_GNU_ref_alt:
19475 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19476 info_ptr += bytes_read;
19477 break;
19478 case DW_FORM_addr:
19479 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19480 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19481 info_ptr += bytes_read;
19482 break;
19483 case DW_FORM_block2:
19484 blk = dwarf_alloc_block (cu);
19485 blk->size = read_2_bytes (abfd, info_ptr);
19486 info_ptr += 2;
19487 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19488 info_ptr += blk->size;
19489 DW_BLOCK (attr) = blk;
19490 break;
19491 case DW_FORM_block4:
19492 blk = dwarf_alloc_block (cu);
19493 blk->size = read_4_bytes (abfd, info_ptr);
19494 info_ptr += 4;
19495 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19496 info_ptr += blk->size;
19497 DW_BLOCK (attr) = blk;
19498 break;
19499 case DW_FORM_data2:
19500 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19501 info_ptr += 2;
19502 break;
19503 case DW_FORM_data4:
19504 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19505 info_ptr += 4;
19506 break;
19507 case DW_FORM_data8:
19508 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19509 info_ptr += 8;
19510 break;
19511 case DW_FORM_data16:
19512 blk = dwarf_alloc_block (cu);
19513 blk->size = 16;
19514 blk->data = read_n_bytes (abfd, info_ptr, 16);
19515 info_ptr += 16;
19516 DW_BLOCK (attr) = blk;
19517 break;
19518 case DW_FORM_sec_offset:
19519 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19520 info_ptr += bytes_read;
19521 break;
19522 case DW_FORM_string:
19523 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19524 DW_STRING_IS_CANONICAL (attr) = 0;
19525 info_ptr += bytes_read;
19526 break;
19527 case DW_FORM_strp:
19528 if (!cu->per_cu->is_dwz)
19530 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19531 abfd, info_ptr, cu_header,
19532 &bytes_read);
19533 DW_STRING_IS_CANONICAL (attr) = 0;
19534 info_ptr += bytes_read;
19535 break;
19537 /* FALLTHROUGH */
19538 case DW_FORM_line_strp:
19539 if (!cu->per_cu->is_dwz)
19541 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19542 abfd, info_ptr,
19543 cu_header, &bytes_read);
19544 DW_STRING_IS_CANONICAL (attr) = 0;
19545 info_ptr += bytes_read;
19546 break;
19548 /* FALLTHROUGH */
19549 case DW_FORM_GNU_strp_alt:
19551 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19552 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19553 &bytes_read);
19555 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19556 dwz, str_offset);
19557 DW_STRING_IS_CANONICAL (attr) = 0;
19558 info_ptr += bytes_read;
19560 break;
19561 case DW_FORM_exprloc:
19562 case DW_FORM_block:
19563 blk = dwarf_alloc_block (cu);
19564 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19565 info_ptr += bytes_read;
19566 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19567 info_ptr += blk->size;
19568 DW_BLOCK (attr) = blk;
19569 break;
19570 case DW_FORM_block1:
19571 blk = dwarf_alloc_block (cu);
19572 blk->size = read_1_byte (abfd, info_ptr);
19573 info_ptr += 1;
19574 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19575 info_ptr += blk->size;
19576 DW_BLOCK (attr) = blk;
19577 break;
19578 case DW_FORM_data1:
19579 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19580 info_ptr += 1;
19581 break;
19582 case DW_FORM_flag:
19583 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19584 info_ptr += 1;
19585 break;
19586 case DW_FORM_flag_present:
19587 DW_UNSND (attr) = 1;
19588 break;
19589 case DW_FORM_sdata:
19590 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19591 info_ptr += bytes_read;
19592 break;
19593 case DW_FORM_udata:
19594 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19595 info_ptr += bytes_read;
19596 break;
19597 case DW_FORM_ref1:
19598 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19599 + read_1_byte (abfd, info_ptr));
19600 info_ptr += 1;
19601 break;
19602 case DW_FORM_ref2:
19603 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19604 + read_2_bytes (abfd, info_ptr));
19605 info_ptr += 2;
19606 break;
19607 case DW_FORM_ref4:
19608 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19609 + read_4_bytes (abfd, info_ptr));
19610 info_ptr += 4;
19611 break;
19612 case DW_FORM_ref8:
19613 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19614 + read_8_bytes (abfd, info_ptr));
19615 info_ptr += 8;
19616 break;
19617 case DW_FORM_ref_sig8:
19618 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19619 info_ptr += 8;
19620 break;
19621 case DW_FORM_ref_udata:
19622 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19623 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19624 info_ptr += bytes_read;
19625 break;
19626 case DW_FORM_indirect:
19627 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19628 info_ptr += bytes_read;
19629 if (form == DW_FORM_implicit_const)
19631 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19632 info_ptr += bytes_read;
19634 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19635 info_ptr);
19636 break;
19637 case DW_FORM_implicit_const:
19638 DW_SND (attr) = implicit_const;
19639 break;
19640 case DW_FORM_addrx:
19641 case DW_FORM_GNU_addr_index:
19642 if (reader->dwo_file == NULL)
19644 /* For now flag a hard error.
19645 Later we can turn this into a complaint. */
19646 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19647 dwarf_form_name (form),
19648 bfd_get_filename (abfd));
19650 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19651 info_ptr += bytes_read;
19652 break;
19653 case DW_FORM_strx:
19654 case DW_FORM_strx1:
19655 case DW_FORM_strx2:
19656 case DW_FORM_strx3:
19657 case DW_FORM_strx4:
19658 case DW_FORM_GNU_str_index:
19659 if (reader->dwo_file == NULL)
19661 /* For now flag a hard error.
19662 Later we can turn this into a complaint if warranted. */
19663 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19664 dwarf_form_name (form),
19665 bfd_get_filename (abfd));
19668 ULONGEST str_index;
19669 if (form == DW_FORM_strx1)
19671 str_index = read_1_byte (abfd, info_ptr);
19672 info_ptr += 1;
19674 else if (form == DW_FORM_strx2)
19676 str_index = read_2_bytes (abfd, info_ptr);
19677 info_ptr += 2;
19679 else if (form == DW_FORM_strx3)
19681 str_index = read_3_bytes (abfd, info_ptr);
19682 info_ptr += 3;
19684 else if (form == DW_FORM_strx4)
19686 str_index = read_4_bytes (abfd, info_ptr);
19687 info_ptr += 4;
19689 else
19691 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19692 info_ptr += bytes_read;
19694 DW_STRING (attr) = read_str_index (reader, str_index);
19695 DW_STRING_IS_CANONICAL (attr) = 0;
19697 break;
19698 default:
19699 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19700 dwarf_form_name (form),
19701 bfd_get_filename (abfd));
19704 /* Super hack. */
19705 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19706 attr->form = DW_FORM_GNU_ref_alt;
19708 /* We have seen instances where the compiler tried to emit a byte
19709 size attribute of -1 which ended up being encoded as an unsigned
19710 0xffffffff. Although 0xffffffff is technically a valid size value,
19711 an object of this size seems pretty unlikely so we can relatively
19712 safely treat these cases as if the size attribute was invalid and
19713 treat them as zero by default. */
19714 if (attr->name == DW_AT_byte_size
19715 && form == DW_FORM_data4
19716 && DW_UNSND (attr) >= 0xffffffff)
19718 complaint
19719 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19720 hex_string (DW_UNSND (attr)));
19721 DW_UNSND (attr) = 0;
19724 return info_ptr;
19727 /* Read an attribute described by an abbreviated attribute. */
19729 static const gdb_byte *
19730 read_attribute (const struct die_reader_specs *reader,
19731 struct attribute *attr, struct attr_abbrev *abbrev,
19732 const gdb_byte *info_ptr)
19734 attr->name = abbrev->name;
19735 return read_attribute_value (reader, attr, abbrev->form,
19736 abbrev->implicit_const, info_ptr);
19739 /* Read dwarf information from a buffer. */
19741 static unsigned int
19742 read_1_byte (bfd *abfd, const gdb_byte *buf)
19744 return bfd_get_8 (abfd, buf);
19747 static int
19748 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19750 return bfd_get_signed_8 (abfd, buf);
19753 static unsigned int
19754 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19756 return bfd_get_16 (abfd, buf);
19759 static int
19760 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19762 return bfd_get_signed_16 (abfd, buf);
19765 static unsigned int
19766 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19768 unsigned int result = 0;
19769 for (int i = 0; i < 3; ++i)
19771 unsigned char byte = bfd_get_8 (abfd, buf);
19772 buf++;
19773 result |= ((unsigned int) byte << (i * 8));
19775 return result;
19778 static unsigned int
19779 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19781 return bfd_get_32 (abfd, buf);
19784 static int
19785 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19787 return bfd_get_signed_32 (abfd, buf);
19790 static ULONGEST
19791 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19793 return bfd_get_64 (abfd, buf);
19796 static CORE_ADDR
19797 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19798 unsigned int *bytes_read)
19800 struct comp_unit_head *cu_header = &cu->header;
19801 CORE_ADDR retval = 0;
19803 if (cu_header->signed_addr_p)
19805 switch (cu_header->addr_size)
19807 case 2:
19808 retval = bfd_get_signed_16 (abfd, buf);
19809 break;
19810 case 4:
19811 retval = bfd_get_signed_32 (abfd, buf);
19812 break;
19813 case 8:
19814 retval = bfd_get_signed_64 (abfd, buf);
19815 break;
19816 default:
19817 internal_error (__FILE__, __LINE__,
19818 _("read_address: bad switch, signed [in module %s]"),
19819 bfd_get_filename (abfd));
19822 else
19824 switch (cu_header->addr_size)
19826 case 2:
19827 retval = bfd_get_16 (abfd, buf);
19828 break;
19829 case 4:
19830 retval = bfd_get_32 (abfd, buf);
19831 break;
19832 case 8:
19833 retval = bfd_get_64 (abfd, buf);
19834 break;
19835 default:
19836 internal_error (__FILE__, __LINE__,
19837 _("read_address: bad switch, "
19838 "unsigned [in module %s]"),
19839 bfd_get_filename (abfd));
19843 *bytes_read = cu_header->addr_size;
19844 return retval;
19847 /* Read the initial length from a section. The (draft) DWARF 3
19848 specification allows the initial length to take up either 4 bytes
19849 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19850 bytes describe the length and all offsets will be 8 bytes in length
19851 instead of 4.
19853 An older, non-standard 64-bit format is also handled by this
19854 function. The older format in question stores the initial length
19855 as an 8-byte quantity without an escape value. Lengths greater
19856 than 2^32 aren't very common which means that the initial 4 bytes
19857 is almost always zero. Since a length value of zero doesn't make
19858 sense for the 32-bit format, this initial zero can be considered to
19859 be an escape value which indicates the presence of the older 64-bit
19860 format. As written, the code can't detect (old format) lengths
19861 greater than 4GB. If it becomes necessary to handle lengths
19862 somewhat larger than 4GB, we could allow other small values (such
19863 as the non-sensical values of 1, 2, and 3) to also be used as
19864 escape values indicating the presence of the old format.
19866 The value returned via bytes_read should be used to increment the
19867 relevant pointer after calling read_initial_length().
19869 [ Note: read_initial_length() and read_offset() are based on the
19870 document entitled "DWARF Debugging Information Format", revision
19871 3, draft 8, dated November 19, 2001. This document was obtained
19872 from:
19874 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19876 This document is only a draft and is subject to change. (So beware.)
19878 Details regarding the older, non-standard 64-bit format were
19879 determined empirically by examining 64-bit ELF files produced by
19880 the SGI toolchain on an IRIX 6.5 machine.
19882 - Kevin, July 16, 2002
19883 ] */
19885 static LONGEST
19886 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19888 LONGEST length = bfd_get_32 (abfd, buf);
19890 if (length == 0xffffffff)
19892 length = bfd_get_64 (abfd, buf + 4);
19893 *bytes_read = 12;
19895 else if (length == 0)
19897 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19898 length = bfd_get_64 (abfd, buf);
19899 *bytes_read = 8;
19901 else
19903 *bytes_read = 4;
19906 return length;
19909 /* Cover function for read_initial_length.
19910 Returns the length of the object at BUF, and stores the size of the
19911 initial length in *BYTES_READ and stores the size that offsets will be in
19912 *OFFSET_SIZE.
19913 If the initial length size is not equivalent to that specified in
19914 CU_HEADER then issue a complaint.
19915 This is useful when reading non-comp-unit headers. */
19917 static LONGEST
19918 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19919 const struct comp_unit_head *cu_header,
19920 unsigned int *bytes_read,
19921 unsigned int *offset_size)
19923 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19925 gdb_assert (cu_header->initial_length_size == 4
19926 || cu_header->initial_length_size == 8
19927 || cu_header->initial_length_size == 12);
19929 if (cu_header->initial_length_size != *bytes_read)
19930 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19932 *offset_size = (*bytes_read == 4) ? 4 : 8;
19933 return length;
19936 /* Read an offset from the data stream. The size of the offset is
19937 given by cu_header->offset_size. */
19939 static LONGEST
19940 read_offset (bfd *abfd, const gdb_byte *buf,
19941 const struct comp_unit_head *cu_header,
19942 unsigned int *bytes_read)
19944 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19946 *bytes_read = cu_header->offset_size;
19947 return offset;
19950 /* Read an offset from the data stream. */
19952 static LONGEST
19953 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19955 LONGEST retval = 0;
19957 switch (offset_size)
19959 case 4:
19960 retval = bfd_get_32 (abfd, buf);
19961 break;
19962 case 8:
19963 retval = bfd_get_64 (abfd, buf);
19964 break;
19965 default:
19966 internal_error (__FILE__, __LINE__,
19967 _("read_offset_1: bad switch [in module %s]"),
19968 bfd_get_filename (abfd));
19971 return retval;
19974 static const gdb_byte *
19975 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19977 /* If the size of a host char is 8 bits, we can return a pointer
19978 to the buffer, otherwise we have to copy the data to a buffer
19979 allocated on the temporary obstack. */
19980 gdb_assert (HOST_CHAR_BIT == 8);
19981 return buf;
19984 static const char *
19985 read_direct_string (bfd *abfd, const gdb_byte *buf,
19986 unsigned int *bytes_read_ptr)
19988 /* If the size of a host char is 8 bits, we can return a pointer
19989 to the string, otherwise we have to copy the string to a buffer
19990 allocated on the temporary obstack. */
19991 gdb_assert (HOST_CHAR_BIT == 8);
19992 if (*buf == '\0')
19994 *bytes_read_ptr = 1;
19995 return NULL;
19997 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19998 return (const char *) buf;
20001 /* Return pointer to string at section SECT offset STR_OFFSET with error
20002 reporting strings FORM_NAME and SECT_NAME. */
20004 static const char *
20005 read_indirect_string_at_offset_from (struct objfile *objfile,
20006 bfd *abfd, LONGEST str_offset,
20007 struct dwarf2_section_info *sect,
20008 const char *form_name,
20009 const char *sect_name)
20011 dwarf2_read_section (objfile, sect);
20012 if (sect->buffer == NULL)
20013 error (_("%s used without %s section [in module %s]"),
20014 form_name, sect_name, bfd_get_filename (abfd));
20015 if (str_offset >= sect->size)
20016 error (_("%s pointing outside of %s section [in module %s]"),
20017 form_name, sect_name, bfd_get_filename (abfd));
20018 gdb_assert (HOST_CHAR_BIT == 8);
20019 if (sect->buffer[str_offset] == '\0')
20020 return NULL;
20021 return (const char *) (sect->buffer + str_offset);
20024 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20026 static const char *
20027 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20028 bfd *abfd, LONGEST str_offset)
20030 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20031 abfd, str_offset,
20032 &dwarf2_per_objfile->str,
20033 "DW_FORM_strp", ".debug_str");
20036 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20038 static const char *
20039 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20040 bfd *abfd, LONGEST str_offset)
20042 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20043 abfd, str_offset,
20044 &dwarf2_per_objfile->line_str,
20045 "DW_FORM_line_strp",
20046 ".debug_line_str");
20049 /* Read a string at offset STR_OFFSET in the .debug_str section from
20050 the .dwz file DWZ. Throw an error if the offset is too large. If
20051 the string consists of a single NUL byte, return NULL; otherwise
20052 return a pointer to the string. */
20054 static const char *
20055 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20056 LONGEST str_offset)
20058 dwarf2_read_section (objfile, &dwz->str);
20060 if (dwz->str.buffer == NULL)
20061 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20062 "section [in module %s]"),
20063 bfd_get_filename (dwz->dwz_bfd.get ()));
20064 if (str_offset >= dwz->str.size)
20065 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20066 ".debug_str section [in module %s]"),
20067 bfd_get_filename (dwz->dwz_bfd.get ()));
20068 gdb_assert (HOST_CHAR_BIT == 8);
20069 if (dwz->str.buffer[str_offset] == '\0')
20070 return NULL;
20071 return (const char *) (dwz->str.buffer + str_offset);
20074 /* Return pointer to string at .debug_str offset as read from BUF.
20075 BUF is assumed to be in a compilation unit described by CU_HEADER.
20076 Return *BYTES_READ_PTR count of bytes read from BUF. */
20078 static const char *
20079 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20080 const gdb_byte *buf,
20081 const struct comp_unit_head *cu_header,
20082 unsigned int *bytes_read_ptr)
20084 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20086 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20089 /* Return pointer to string at .debug_line_str offset as read from BUF.
20090 BUF is assumed to be in a compilation unit described by CU_HEADER.
20091 Return *BYTES_READ_PTR count of bytes read from BUF. */
20093 static const char *
20094 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20095 bfd *abfd, const gdb_byte *buf,
20096 const struct comp_unit_head *cu_header,
20097 unsigned int *bytes_read_ptr)
20099 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20101 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20102 str_offset);
20105 ULONGEST
20106 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20107 unsigned int *bytes_read_ptr)
20109 ULONGEST result;
20110 unsigned int num_read;
20111 int shift;
20112 unsigned char byte;
20114 result = 0;
20115 shift = 0;
20116 num_read = 0;
20117 while (1)
20119 byte = bfd_get_8 (abfd, buf);
20120 buf++;
20121 num_read++;
20122 result |= ((ULONGEST) (byte & 127) << shift);
20123 if ((byte & 128) == 0)
20125 break;
20127 shift += 7;
20129 *bytes_read_ptr = num_read;
20130 return result;
20133 static LONGEST
20134 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20135 unsigned int *bytes_read_ptr)
20137 ULONGEST result;
20138 int shift, num_read;
20139 unsigned char byte;
20141 result = 0;
20142 shift = 0;
20143 num_read = 0;
20144 while (1)
20146 byte = bfd_get_8 (abfd, buf);
20147 buf++;
20148 num_read++;
20149 result |= ((ULONGEST) (byte & 127) << shift);
20150 shift += 7;
20151 if ((byte & 128) == 0)
20153 break;
20156 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20157 result |= -(((ULONGEST) 1) << shift);
20158 *bytes_read_ptr = num_read;
20159 return result;
20162 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20163 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20164 ADDR_SIZE is the size of addresses from the CU header. */
20166 static CORE_ADDR
20167 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20168 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20170 struct objfile *objfile = dwarf2_per_objfile->objfile;
20171 bfd *abfd = objfile->obfd;
20172 const gdb_byte *info_ptr;
20174 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20175 if (dwarf2_per_objfile->addr.buffer == NULL)
20176 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20177 objfile_name (objfile));
20178 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20179 error (_("DW_FORM_addr_index pointing outside of "
20180 ".debug_addr section [in module %s]"),
20181 objfile_name (objfile));
20182 info_ptr = (dwarf2_per_objfile->addr.buffer
20183 + addr_base + addr_index * addr_size);
20184 if (addr_size == 4)
20185 return bfd_get_32 (abfd, info_ptr);
20186 else
20187 return bfd_get_64 (abfd, info_ptr);
20190 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20192 static CORE_ADDR
20193 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20195 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20196 cu->addr_base, cu->header.addr_size);
20199 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20201 static CORE_ADDR
20202 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20203 unsigned int *bytes_read)
20205 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20206 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20208 return read_addr_index (cu, addr_index);
20211 /* Data structure to pass results from dwarf2_read_addr_index_reader
20212 back to dwarf2_read_addr_index. */
20214 struct dwarf2_read_addr_index_data
20216 ULONGEST addr_base;
20217 int addr_size;
20220 /* die_reader_func for dwarf2_read_addr_index. */
20222 static void
20223 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20224 const gdb_byte *info_ptr,
20225 struct die_info *comp_unit_die,
20226 int has_children,
20227 void *data)
20229 struct dwarf2_cu *cu = reader->cu;
20230 struct dwarf2_read_addr_index_data *aidata =
20231 (struct dwarf2_read_addr_index_data *) data;
20233 aidata->addr_base = cu->addr_base;
20234 aidata->addr_size = cu->header.addr_size;
20237 /* Given an index in .debug_addr, fetch the value.
20238 NOTE: This can be called during dwarf expression evaluation,
20239 long after the debug information has been read, and thus per_cu->cu
20240 may no longer exist. */
20242 CORE_ADDR
20243 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20244 unsigned int addr_index)
20246 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20247 struct dwarf2_cu *cu = per_cu->cu;
20248 ULONGEST addr_base;
20249 int addr_size;
20251 /* We need addr_base and addr_size.
20252 If we don't have PER_CU->cu, we have to get it.
20253 Nasty, but the alternative is storing the needed info in PER_CU,
20254 which at this point doesn't seem justified: it's not clear how frequently
20255 it would get used and it would increase the size of every PER_CU.
20256 Entry points like dwarf2_per_cu_addr_size do a similar thing
20257 so we're not in uncharted territory here.
20258 Alas we need to be a bit more complicated as addr_base is contained
20259 in the DIE.
20261 We don't need to read the entire CU(/TU).
20262 We just need the header and top level die.
20264 IWBN to use the aging mechanism to let us lazily later discard the CU.
20265 For now we skip this optimization. */
20267 if (cu != NULL)
20269 addr_base = cu->addr_base;
20270 addr_size = cu->header.addr_size;
20272 else
20274 struct dwarf2_read_addr_index_data aidata;
20276 /* Note: We can't use init_cutu_and_read_dies_simple here,
20277 we need addr_base. */
20278 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20279 dwarf2_read_addr_index_reader, &aidata);
20280 addr_base = aidata.addr_base;
20281 addr_size = aidata.addr_size;
20284 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20285 addr_size);
20288 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20289 This is only used by the Fission support. */
20291 static const char *
20292 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20294 struct dwarf2_cu *cu = reader->cu;
20295 struct dwarf2_per_objfile *dwarf2_per_objfile
20296 = cu->per_cu->dwarf2_per_objfile;
20297 struct objfile *objfile = dwarf2_per_objfile->objfile;
20298 const char *objf_name = objfile_name (objfile);
20299 bfd *abfd = objfile->obfd;
20300 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20301 struct dwarf2_section_info *str_offsets_section =
20302 &reader->dwo_file->sections.str_offsets;
20303 const gdb_byte *info_ptr;
20304 ULONGEST str_offset;
20305 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20307 dwarf2_read_section (objfile, str_section);
20308 dwarf2_read_section (objfile, str_offsets_section);
20309 if (str_section->buffer == NULL)
20310 error (_("%s used without .debug_str.dwo section"
20311 " in CU at offset %s [in module %s]"),
20312 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20313 if (str_offsets_section->buffer == NULL)
20314 error (_("%s used without .debug_str_offsets.dwo section"
20315 " in CU at offset %s [in module %s]"),
20316 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20317 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20318 error (_("%s pointing outside of .debug_str_offsets.dwo"
20319 " section in CU at offset %s [in module %s]"),
20320 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20321 info_ptr = (str_offsets_section->buffer
20322 + str_index * cu->header.offset_size);
20323 if (cu->header.offset_size == 4)
20324 str_offset = bfd_get_32 (abfd, info_ptr);
20325 else
20326 str_offset = bfd_get_64 (abfd, info_ptr);
20327 if (str_offset >= str_section->size)
20328 error (_("Offset from %s pointing outside of"
20329 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20330 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20331 return (const char *) (str_section->buffer + str_offset);
20334 /* Return the length of an LEB128 number in BUF. */
20336 static int
20337 leb128_size (const gdb_byte *buf)
20339 const gdb_byte *begin = buf;
20340 gdb_byte byte;
20342 while (1)
20344 byte = *buf++;
20345 if ((byte & 128) == 0)
20346 return buf - begin;
20350 static void
20351 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20353 switch (lang)
20355 case DW_LANG_C89:
20356 case DW_LANG_C99:
20357 case DW_LANG_C11:
20358 case DW_LANG_C:
20359 case DW_LANG_UPC:
20360 cu->language = language_c;
20361 break;
20362 case DW_LANG_Java:
20363 case DW_LANG_C_plus_plus:
20364 case DW_LANG_C_plus_plus_11:
20365 case DW_LANG_C_plus_plus_14:
20366 cu->language = language_cplus;
20367 break;
20368 case DW_LANG_D:
20369 cu->language = language_d;
20370 break;
20371 case DW_LANG_Fortran77:
20372 case DW_LANG_Fortran90:
20373 case DW_LANG_Fortran95:
20374 case DW_LANG_Fortran03:
20375 case DW_LANG_Fortran08:
20376 cu->language = language_fortran;
20377 break;
20378 case DW_LANG_Go:
20379 cu->language = language_go;
20380 break;
20381 case DW_LANG_Mips_Assembler:
20382 cu->language = language_asm;
20383 break;
20384 case DW_LANG_Ada83:
20385 case DW_LANG_Ada95:
20386 cu->language = language_ada;
20387 break;
20388 case DW_LANG_Modula2:
20389 cu->language = language_m2;
20390 break;
20391 case DW_LANG_Pascal83:
20392 cu->language = language_pascal;
20393 break;
20394 case DW_LANG_ObjC:
20395 cu->language = language_objc;
20396 break;
20397 case DW_LANG_Rust:
20398 case DW_LANG_Rust_old:
20399 cu->language = language_rust;
20400 break;
20401 case DW_LANG_Cobol74:
20402 case DW_LANG_Cobol85:
20403 default:
20404 cu->language = language_minimal;
20405 break;
20407 cu->language_defn = language_def (cu->language);
20410 /* Return the named attribute or NULL if not there. */
20412 static struct attribute *
20413 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20415 for (;;)
20417 unsigned int i;
20418 struct attribute *spec = NULL;
20420 for (i = 0; i < die->num_attrs; ++i)
20422 if (die->attrs[i].name == name)
20423 return &die->attrs[i];
20424 if (die->attrs[i].name == DW_AT_specification
20425 || die->attrs[i].name == DW_AT_abstract_origin)
20426 spec = &die->attrs[i];
20429 if (!spec)
20430 break;
20432 die = follow_die_ref (die, spec, &cu);
20435 return NULL;
20438 /* Return the named attribute or NULL if not there,
20439 but do not follow DW_AT_specification, etc.
20440 This is for use in contexts where we're reading .debug_types dies.
20441 Following DW_AT_specification, DW_AT_abstract_origin will take us
20442 back up the chain, and we want to go down. */
20444 static struct attribute *
20445 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20447 unsigned int i;
20449 for (i = 0; i < die->num_attrs; ++i)
20450 if (die->attrs[i].name == name)
20451 return &die->attrs[i];
20453 return NULL;
20456 /* Return the string associated with a string-typed attribute, or NULL if it
20457 is either not found or is of an incorrect type. */
20459 static const char *
20460 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20462 struct attribute *attr;
20463 const char *str = NULL;
20465 attr = dwarf2_attr (die, name, cu);
20467 if (attr != NULL)
20469 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20470 || attr->form == DW_FORM_string
20471 || attr->form == DW_FORM_strx
20472 || attr->form == DW_FORM_strx1
20473 || attr->form == DW_FORM_strx2
20474 || attr->form == DW_FORM_strx3
20475 || attr->form == DW_FORM_strx4
20476 || attr->form == DW_FORM_GNU_str_index
20477 || attr->form == DW_FORM_GNU_strp_alt)
20478 str = DW_STRING (attr);
20479 else
20480 complaint (_("string type expected for attribute %s for "
20481 "DIE at %s in module %s"),
20482 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20486 return str;
20489 /* Return the dwo name or NULL if not present. If present, it is in either
20490 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20491 static const char *
20492 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20494 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20495 if (dwo_name == nullptr)
20496 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20497 return dwo_name;
20500 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20501 and holds a non-zero value. This function should only be used for
20502 DW_FORM_flag or DW_FORM_flag_present attributes. */
20504 static int
20505 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20507 struct attribute *attr = dwarf2_attr (die, name, cu);
20509 return (attr && DW_UNSND (attr));
20512 static int
20513 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20515 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20516 which value is non-zero. However, we have to be careful with
20517 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20518 (via dwarf2_flag_true_p) follows this attribute. So we may
20519 end up accidently finding a declaration attribute that belongs
20520 to a different DIE referenced by the specification attribute,
20521 even though the given DIE does not have a declaration attribute. */
20522 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20523 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20526 /* Return the die giving the specification for DIE, if there is
20527 one. *SPEC_CU is the CU containing DIE on input, and the CU
20528 containing the return value on output. If there is no
20529 specification, but there is an abstract origin, that is
20530 returned. */
20532 static struct die_info *
20533 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20535 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20536 *spec_cu);
20538 if (spec_attr == NULL)
20539 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20541 if (spec_attr == NULL)
20542 return NULL;
20543 else
20544 return follow_die_ref (die, spec_attr, spec_cu);
20547 /* Stub for free_line_header to match void * callback types. */
20549 static void
20550 free_line_header_voidp (void *arg)
20552 struct line_header *lh = (struct line_header *) arg;
20554 delete lh;
20557 void
20558 line_header::add_include_dir (const char *include_dir)
20560 if (dwarf_line_debug >= 2)
20562 size_t new_size;
20563 if (version >= 5)
20564 new_size = m_include_dirs.size ();
20565 else
20566 new_size = m_include_dirs.size () + 1;
20567 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20568 new_size, include_dir);
20570 m_include_dirs.push_back (include_dir);
20573 void
20574 line_header::add_file_name (const char *name,
20575 dir_index d_index,
20576 unsigned int mod_time,
20577 unsigned int length)
20579 if (dwarf_line_debug >= 2)
20581 size_t new_size;
20582 if (version >= 5)
20583 new_size = file_names_size ();
20584 else
20585 new_size = file_names_size () + 1;
20586 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20587 new_size, name);
20589 m_file_names.emplace_back (name, d_index, mod_time, length);
20592 /* A convenience function to find the proper .debug_line section for a CU. */
20594 static struct dwarf2_section_info *
20595 get_debug_line_section (struct dwarf2_cu *cu)
20597 struct dwarf2_section_info *section;
20598 struct dwarf2_per_objfile *dwarf2_per_objfile
20599 = cu->per_cu->dwarf2_per_objfile;
20601 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20602 DWO file. */
20603 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20604 section = &cu->dwo_unit->dwo_file->sections.line;
20605 else if (cu->per_cu->is_dwz)
20607 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20609 section = &dwz->line;
20611 else
20612 section = &dwarf2_per_objfile->line;
20614 return section;
20617 /* Read directory or file name entry format, starting with byte of
20618 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20619 entries count and the entries themselves in the described entry
20620 format. */
20622 static void
20623 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20624 bfd *abfd, const gdb_byte **bufp,
20625 struct line_header *lh,
20626 const struct comp_unit_head *cu_header,
20627 void (*callback) (struct line_header *lh,
20628 const char *name,
20629 dir_index d_index,
20630 unsigned int mod_time,
20631 unsigned int length))
20633 gdb_byte format_count, formati;
20634 ULONGEST data_count, datai;
20635 const gdb_byte *buf = *bufp;
20636 const gdb_byte *format_header_data;
20637 unsigned int bytes_read;
20639 format_count = read_1_byte (abfd, buf);
20640 buf += 1;
20641 format_header_data = buf;
20642 for (formati = 0; formati < format_count; formati++)
20644 read_unsigned_leb128 (abfd, buf, &bytes_read);
20645 buf += bytes_read;
20646 read_unsigned_leb128 (abfd, buf, &bytes_read);
20647 buf += bytes_read;
20650 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20651 buf += bytes_read;
20652 for (datai = 0; datai < data_count; datai++)
20654 const gdb_byte *format = format_header_data;
20655 struct file_entry fe;
20657 for (formati = 0; formati < format_count; formati++)
20659 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20660 format += bytes_read;
20662 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20663 format += bytes_read;
20665 gdb::optional<const char *> string;
20666 gdb::optional<unsigned int> uint;
20668 switch (form)
20670 case DW_FORM_string:
20671 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20672 buf += bytes_read;
20673 break;
20675 case DW_FORM_line_strp:
20676 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20677 abfd, buf,
20678 cu_header,
20679 &bytes_read));
20680 buf += bytes_read;
20681 break;
20683 case DW_FORM_data1:
20684 uint.emplace (read_1_byte (abfd, buf));
20685 buf += 1;
20686 break;
20688 case DW_FORM_data2:
20689 uint.emplace (read_2_bytes (abfd, buf));
20690 buf += 2;
20691 break;
20693 case DW_FORM_data4:
20694 uint.emplace (read_4_bytes (abfd, buf));
20695 buf += 4;
20696 break;
20698 case DW_FORM_data8:
20699 uint.emplace (read_8_bytes (abfd, buf));
20700 buf += 8;
20701 break;
20703 case DW_FORM_data16:
20704 /* This is used for MD5, but file_entry does not record MD5s. */
20705 buf += 16;
20706 break;
20708 case DW_FORM_udata:
20709 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20710 buf += bytes_read;
20711 break;
20713 case DW_FORM_block:
20714 /* It is valid only for DW_LNCT_timestamp which is ignored by
20715 current GDB. */
20716 break;
20719 switch (content_type)
20721 case DW_LNCT_path:
20722 if (string.has_value ())
20723 fe.name = *string;
20724 break;
20725 case DW_LNCT_directory_index:
20726 if (uint.has_value ())
20727 fe.d_index = (dir_index) *uint;
20728 break;
20729 case DW_LNCT_timestamp:
20730 if (uint.has_value ())
20731 fe.mod_time = *uint;
20732 break;
20733 case DW_LNCT_size:
20734 if (uint.has_value ())
20735 fe.length = *uint;
20736 break;
20737 case DW_LNCT_MD5:
20738 break;
20739 default:
20740 complaint (_("Unknown format content type %s"),
20741 pulongest (content_type));
20745 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20748 *bufp = buf;
20751 /* Read the statement program header starting at OFFSET in
20752 .debug_line, or .debug_line.dwo. Return a pointer
20753 to a struct line_header, allocated using xmalloc.
20754 Returns NULL if there is a problem reading the header, e.g., if it
20755 has a version we don't understand.
20757 NOTE: the strings in the include directory and file name tables of
20758 the returned object point into the dwarf line section buffer,
20759 and must not be freed. */
20761 static line_header_up
20762 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20764 const gdb_byte *line_ptr;
20765 unsigned int bytes_read, offset_size;
20766 int i;
20767 const char *cur_dir, *cur_file;
20768 struct dwarf2_section_info *section;
20769 bfd *abfd;
20770 struct dwarf2_per_objfile *dwarf2_per_objfile
20771 = cu->per_cu->dwarf2_per_objfile;
20773 section = get_debug_line_section (cu);
20774 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20775 if (section->buffer == NULL)
20777 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20778 complaint (_("missing .debug_line.dwo section"));
20779 else
20780 complaint (_("missing .debug_line section"));
20781 return 0;
20784 /* We can't do this until we know the section is non-empty.
20785 Only then do we know we have such a section. */
20786 abfd = get_section_bfd_owner (section);
20788 /* Make sure that at least there's room for the total_length field.
20789 That could be 12 bytes long, but we're just going to fudge that. */
20790 if (to_underlying (sect_off) + 4 >= section->size)
20792 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20793 return 0;
20796 line_header_up lh (new line_header ());
20798 lh->sect_off = sect_off;
20799 lh->offset_in_dwz = cu->per_cu->is_dwz;
20801 line_ptr = section->buffer + to_underlying (sect_off);
20803 /* Read in the header. */
20804 lh->total_length =
20805 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20806 &bytes_read, &offset_size);
20807 line_ptr += bytes_read;
20809 const gdb_byte *start_here = line_ptr;
20811 if (line_ptr + lh->total_length > (section->buffer + section->size))
20813 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20814 return 0;
20816 lh->statement_program_end = start_here + lh->total_length;
20817 lh->version = read_2_bytes (abfd, line_ptr);
20818 line_ptr += 2;
20819 if (lh->version > 5)
20821 /* This is a version we don't understand. The format could have
20822 changed in ways we don't handle properly so just punt. */
20823 complaint (_("unsupported version in .debug_line section"));
20824 return NULL;
20826 if (lh->version >= 5)
20828 gdb_byte segment_selector_size;
20830 /* Skip address size. */
20831 read_1_byte (abfd, line_ptr);
20832 line_ptr += 1;
20834 segment_selector_size = read_1_byte (abfd, line_ptr);
20835 line_ptr += 1;
20836 if (segment_selector_size != 0)
20838 complaint (_("unsupported segment selector size %u "
20839 "in .debug_line section"),
20840 segment_selector_size);
20841 return NULL;
20844 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20845 line_ptr += offset_size;
20846 lh->statement_program_start = line_ptr + lh->header_length;
20847 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20848 line_ptr += 1;
20849 if (lh->version >= 4)
20851 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20852 line_ptr += 1;
20854 else
20855 lh->maximum_ops_per_instruction = 1;
20857 if (lh->maximum_ops_per_instruction == 0)
20859 lh->maximum_ops_per_instruction = 1;
20860 complaint (_("invalid maximum_ops_per_instruction "
20861 "in `.debug_line' section"));
20864 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20865 line_ptr += 1;
20866 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20867 line_ptr += 1;
20868 lh->line_range = read_1_byte (abfd, line_ptr);
20869 line_ptr += 1;
20870 lh->opcode_base = read_1_byte (abfd, line_ptr);
20871 line_ptr += 1;
20872 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20874 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20875 for (i = 1; i < lh->opcode_base; ++i)
20877 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20878 line_ptr += 1;
20881 if (lh->version >= 5)
20883 /* Read directory table. */
20884 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20885 &cu->header,
20886 [] (struct line_header *header, const char *name,
20887 dir_index d_index, unsigned int mod_time,
20888 unsigned int length)
20890 header->add_include_dir (name);
20893 /* Read file name table. */
20894 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20895 &cu->header,
20896 [] (struct line_header *header, const char *name,
20897 dir_index d_index, unsigned int mod_time,
20898 unsigned int length)
20900 header->add_file_name (name, d_index, mod_time, length);
20903 else
20905 /* Read directory table. */
20906 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20908 line_ptr += bytes_read;
20909 lh->add_include_dir (cur_dir);
20911 line_ptr += bytes_read;
20913 /* Read file name table. */
20914 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20916 unsigned int mod_time, length;
20917 dir_index d_index;
20919 line_ptr += bytes_read;
20920 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20921 line_ptr += bytes_read;
20922 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20923 line_ptr += bytes_read;
20924 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20925 line_ptr += bytes_read;
20927 lh->add_file_name (cur_file, d_index, mod_time, length);
20929 line_ptr += bytes_read;
20932 if (line_ptr > (section->buffer + section->size))
20933 complaint (_("line number info header doesn't "
20934 "fit in `.debug_line' section"));
20936 return lh;
20939 /* Subroutine of dwarf_decode_lines to simplify it.
20940 Return the file name of the psymtab for the given file_entry.
20941 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20942 If space for the result is malloc'd, *NAME_HOLDER will be set.
20943 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20945 static const char *
20946 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20947 const struct partial_symtab *pst,
20948 const char *comp_dir,
20949 gdb::unique_xmalloc_ptr<char> *name_holder)
20951 const char *include_name = fe.name;
20952 const char *include_name_to_compare = include_name;
20953 const char *pst_filename;
20954 int file_is_pst;
20956 const char *dir_name = fe.include_dir (lh);
20958 gdb::unique_xmalloc_ptr<char> hold_compare;
20959 if (!IS_ABSOLUTE_PATH (include_name)
20960 && (dir_name != NULL || comp_dir != NULL))
20962 /* Avoid creating a duplicate psymtab for PST.
20963 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20964 Before we do the comparison, however, we need to account
20965 for DIR_NAME and COMP_DIR.
20966 First prepend dir_name (if non-NULL). If we still don't
20967 have an absolute path prepend comp_dir (if non-NULL).
20968 However, the directory we record in the include-file's
20969 psymtab does not contain COMP_DIR (to match the
20970 corresponding symtab(s)).
20972 Example:
20974 bash$ cd /tmp
20975 bash$ gcc -g ./hello.c
20976 include_name = "hello.c"
20977 dir_name = "."
20978 DW_AT_comp_dir = comp_dir = "/tmp"
20979 DW_AT_name = "./hello.c"
20983 if (dir_name != NULL)
20985 name_holder->reset (concat (dir_name, SLASH_STRING,
20986 include_name, (char *) NULL));
20987 include_name = name_holder->get ();
20988 include_name_to_compare = include_name;
20990 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20992 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20993 include_name, (char *) NULL));
20994 include_name_to_compare = hold_compare.get ();
20998 pst_filename = pst->filename;
20999 gdb::unique_xmalloc_ptr<char> copied_name;
21000 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21002 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21003 pst_filename, (char *) NULL));
21004 pst_filename = copied_name.get ();
21007 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21009 if (file_is_pst)
21010 return NULL;
21011 return include_name;
21014 /* State machine to track the state of the line number program. */
21016 class lnp_state_machine
21018 public:
21019 /* Initialize a machine state for the start of a line number
21020 program. */
21021 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21022 bool record_lines_p);
21024 file_entry *current_file ()
21026 /* lh->file_names is 0-based, but the file name numbers in the
21027 statement program are 1-based. */
21028 return m_line_header->file_name_at (m_file);
21031 /* Record the line in the state machine. END_SEQUENCE is true if
21032 we're processing the end of a sequence. */
21033 void record_line (bool end_sequence);
21035 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21036 nop-out rest of the lines in this sequence. */
21037 void check_line_address (struct dwarf2_cu *cu,
21038 const gdb_byte *line_ptr,
21039 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21041 void handle_set_discriminator (unsigned int discriminator)
21043 m_discriminator = discriminator;
21044 m_line_has_non_zero_discriminator |= discriminator != 0;
21047 /* Handle DW_LNE_set_address. */
21048 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21050 m_op_index = 0;
21051 address += baseaddr;
21052 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21055 /* Handle DW_LNS_advance_pc. */
21056 void handle_advance_pc (CORE_ADDR adjust);
21058 /* Handle a special opcode. */
21059 void handle_special_opcode (unsigned char op_code);
21061 /* Handle DW_LNS_advance_line. */
21062 void handle_advance_line (int line_delta)
21064 advance_line (line_delta);
21067 /* Handle DW_LNS_set_file. */
21068 void handle_set_file (file_name_index file);
21070 /* Handle DW_LNS_negate_stmt. */
21071 void handle_negate_stmt ()
21073 m_is_stmt = !m_is_stmt;
21076 /* Handle DW_LNS_const_add_pc. */
21077 void handle_const_add_pc ();
21079 /* Handle DW_LNS_fixed_advance_pc. */
21080 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21082 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21083 m_op_index = 0;
21086 /* Handle DW_LNS_copy. */
21087 void handle_copy ()
21089 record_line (false);
21090 m_discriminator = 0;
21093 /* Handle DW_LNE_end_sequence. */
21094 void handle_end_sequence ()
21096 m_currently_recording_lines = true;
21099 private:
21100 /* Advance the line by LINE_DELTA. */
21101 void advance_line (int line_delta)
21103 m_line += line_delta;
21105 if (line_delta != 0)
21106 m_line_has_non_zero_discriminator = m_discriminator != 0;
21109 struct dwarf2_cu *m_cu;
21111 gdbarch *m_gdbarch;
21113 /* True if we're recording lines.
21114 Otherwise we're building partial symtabs and are just interested in
21115 finding include files mentioned by the line number program. */
21116 bool m_record_lines_p;
21118 /* The line number header. */
21119 line_header *m_line_header;
21121 /* These are part of the standard DWARF line number state machine,
21122 and initialized according to the DWARF spec. */
21124 unsigned char m_op_index = 0;
21125 /* The line table index of the current file. */
21126 file_name_index m_file = 1;
21127 unsigned int m_line = 1;
21129 /* These are initialized in the constructor. */
21131 CORE_ADDR m_address;
21132 bool m_is_stmt;
21133 unsigned int m_discriminator;
21135 /* Additional bits of state we need to track. */
21137 /* The last file that we called dwarf2_start_subfile for.
21138 This is only used for TLLs. */
21139 unsigned int m_last_file = 0;
21140 /* The last file a line number was recorded for. */
21141 struct subfile *m_last_subfile = NULL;
21143 /* When true, record the lines we decode. */
21144 bool m_currently_recording_lines = false;
21146 /* The last line number that was recorded, used to coalesce
21147 consecutive entries for the same line. This can happen, for
21148 example, when discriminators are present. PR 17276. */
21149 unsigned int m_last_line = 0;
21150 bool m_line_has_non_zero_discriminator = false;
21153 void
21154 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21156 CORE_ADDR addr_adj = (((m_op_index + adjust)
21157 / m_line_header->maximum_ops_per_instruction)
21158 * m_line_header->minimum_instruction_length);
21159 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21160 m_op_index = ((m_op_index + adjust)
21161 % m_line_header->maximum_ops_per_instruction);
21164 void
21165 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21167 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21168 CORE_ADDR addr_adj = (((m_op_index
21169 + (adj_opcode / m_line_header->line_range))
21170 / m_line_header->maximum_ops_per_instruction)
21171 * m_line_header->minimum_instruction_length);
21172 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21173 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21174 % m_line_header->maximum_ops_per_instruction);
21176 int line_delta = (m_line_header->line_base
21177 + (adj_opcode % m_line_header->line_range));
21178 advance_line (line_delta);
21179 record_line (false);
21180 m_discriminator = 0;
21183 void
21184 lnp_state_machine::handle_set_file (file_name_index file)
21186 m_file = file;
21188 const file_entry *fe = current_file ();
21189 if (fe == NULL)
21190 dwarf2_debug_line_missing_file_complaint ();
21191 else if (m_record_lines_p)
21193 const char *dir = fe->include_dir (m_line_header);
21195 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21196 m_line_has_non_zero_discriminator = m_discriminator != 0;
21197 dwarf2_start_subfile (m_cu, fe->name, dir);
21201 void
21202 lnp_state_machine::handle_const_add_pc ()
21204 CORE_ADDR adjust
21205 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21207 CORE_ADDR addr_adj
21208 = (((m_op_index + adjust)
21209 / m_line_header->maximum_ops_per_instruction)
21210 * m_line_header->minimum_instruction_length);
21212 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21213 m_op_index = ((m_op_index + adjust)
21214 % m_line_header->maximum_ops_per_instruction);
21217 /* Return non-zero if we should add LINE to the line number table.
21218 LINE is the line to add, LAST_LINE is the last line that was added,
21219 LAST_SUBFILE is the subfile for LAST_LINE.
21220 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21221 had a non-zero discriminator.
21223 We have to be careful in the presence of discriminators.
21224 E.g., for this line:
21226 for (i = 0; i < 100000; i++);
21228 clang can emit four line number entries for that one line,
21229 each with a different discriminator.
21230 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21232 However, we want gdb to coalesce all four entries into one.
21233 Otherwise the user could stepi into the middle of the line and
21234 gdb would get confused about whether the pc really was in the
21235 middle of the line.
21237 Things are further complicated by the fact that two consecutive
21238 line number entries for the same line is a heuristic used by gcc
21239 to denote the end of the prologue. So we can't just discard duplicate
21240 entries, we have to be selective about it. The heuristic we use is
21241 that we only collapse consecutive entries for the same line if at least
21242 one of those entries has a non-zero discriminator. PR 17276.
21244 Note: Addresses in the line number state machine can never go backwards
21245 within one sequence, thus this coalescing is ok. */
21247 static int
21248 dwarf_record_line_p (struct dwarf2_cu *cu,
21249 unsigned int line, unsigned int last_line,
21250 int line_has_non_zero_discriminator,
21251 struct subfile *last_subfile)
21253 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21254 return 1;
21255 if (line != last_line)
21256 return 1;
21257 /* Same line for the same file that we've seen already.
21258 As a last check, for pr 17276, only record the line if the line
21259 has never had a non-zero discriminator. */
21260 if (!line_has_non_zero_discriminator)
21261 return 1;
21262 return 0;
21265 /* Use the CU's builder to record line number LINE beginning at
21266 address ADDRESS in the line table of subfile SUBFILE. */
21268 static void
21269 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21270 unsigned int line, CORE_ADDR address,
21271 struct dwarf2_cu *cu)
21273 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21275 if (dwarf_line_debug)
21277 fprintf_unfiltered (gdb_stdlog,
21278 "Recording line %u, file %s, address %s\n",
21279 line, lbasename (subfile->name),
21280 paddress (gdbarch, address));
21283 if (cu != nullptr)
21284 cu->get_builder ()->record_line (subfile, line, addr);
21287 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21288 Mark the end of a set of line number records.
21289 The arguments are the same as for dwarf_record_line_1.
21290 If SUBFILE is NULL the request is ignored. */
21292 static void
21293 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21294 CORE_ADDR address, struct dwarf2_cu *cu)
21296 if (subfile == NULL)
21297 return;
21299 if (dwarf_line_debug)
21301 fprintf_unfiltered (gdb_stdlog,
21302 "Finishing current line, file %s, address %s\n",
21303 lbasename (subfile->name),
21304 paddress (gdbarch, address));
21307 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21310 void
21311 lnp_state_machine::record_line (bool end_sequence)
21313 if (dwarf_line_debug)
21315 fprintf_unfiltered (gdb_stdlog,
21316 "Processing actual line %u: file %u,"
21317 " address %s, is_stmt %u, discrim %u\n",
21318 m_line, m_file,
21319 paddress (m_gdbarch, m_address),
21320 m_is_stmt, m_discriminator);
21323 file_entry *fe = current_file ();
21325 if (fe == NULL)
21326 dwarf2_debug_line_missing_file_complaint ();
21327 /* For now we ignore lines not starting on an instruction boundary.
21328 But not when processing end_sequence for compatibility with the
21329 previous version of the code. */
21330 else if (m_op_index == 0 || end_sequence)
21332 fe->included_p = 1;
21333 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21335 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21336 || end_sequence)
21338 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21339 m_currently_recording_lines ? m_cu : nullptr);
21342 if (!end_sequence)
21344 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21345 m_line_has_non_zero_discriminator,
21346 m_last_subfile))
21348 buildsym_compunit *builder = m_cu->get_builder ();
21349 dwarf_record_line_1 (m_gdbarch,
21350 builder->get_current_subfile (),
21351 m_line, m_address,
21352 m_currently_recording_lines ? m_cu : nullptr);
21354 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21355 m_last_line = m_line;
21361 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21362 line_header *lh, bool record_lines_p)
21364 m_cu = cu;
21365 m_gdbarch = arch;
21366 m_record_lines_p = record_lines_p;
21367 m_line_header = lh;
21369 m_currently_recording_lines = true;
21371 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21372 was a line entry for it so that the backend has a chance to adjust it
21373 and also record it in case it needs it. This is currently used by MIPS
21374 code, cf. `mips_adjust_dwarf2_line'. */
21375 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21376 m_is_stmt = lh->default_is_stmt;
21377 m_discriminator = 0;
21380 void
21381 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21382 const gdb_byte *line_ptr,
21383 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21385 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21386 the pc range of the CU. However, we restrict the test to only ADDRESS
21387 values of zero to preserve GDB's previous behaviour which is to handle
21388 the specific case of a function being GC'd by the linker. */
21390 if (address == 0 && address < unrelocated_lowpc)
21392 /* This line table is for a function which has been
21393 GCd by the linker. Ignore it. PR gdb/12528 */
21395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21396 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21398 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21399 line_offset, objfile_name (objfile));
21400 m_currently_recording_lines = false;
21401 /* Note: m_currently_recording_lines is left as false until we see
21402 DW_LNE_end_sequence. */
21406 /* Subroutine of dwarf_decode_lines to simplify it.
21407 Process the line number information in LH.
21408 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21409 program in order to set included_p for every referenced header. */
21411 static void
21412 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21413 const int decode_for_pst_p, CORE_ADDR lowpc)
21415 const gdb_byte *line_ptr, *extended_end;
21416 const gdb_byte *line_end;
21417 unsigned int bytes_read, extended_len;
21418 unsigned char op_code, extended_op;
21419 CORE_ADDR baseaddr;
21420 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21421 bfd *abfd = objfile->obfd;
21422 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21423 /* True if we're recording line info (as opposed to building partial
21424 symtabs and just interested in finding include files mentioned by
21425 the line number program). */
21426 bool record_lines_p = !decode_for_pst_p;
21428 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21430 line_ptr = lh->statement_program_start;
21431 line_end = lh->statement_program_end;
21433 /* Read the statement sequences until there's nothing left. */
21434 while (line_ptr < line_end)
21436 /* The DWARF line number program state machine. Reset the state
21437 machine at the start of each sequence. */
21438 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21439 bool end_sequence = false;
21441 if (record_lines_p)
21443 /* Start a subfile for the current file of the state
21444 machine. */
21445 const file_entry *fe = state_machine.current_file ();
21447 if (fe != NULL)
21448 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21451 /* Decode the table. */
21452 while (line_ptr < line_end && !end_sequence)
21454 op_code = read_1_byte (abfd, line_ptr);
21455 line_ptr += 1;
21457 if (op_code >= lh->opcode_base)
21459 /* Special opcode. */
21460 state_machine.handle_special_opcode (op_code);
21462 else switch (op_code)
21464 case DW_LNS_extended_op:
21465 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21466 &bytes_read);
21467 line_ptr += bytes_read;
21468 extended_end = line_ptr + extended_len;
21469 extended_op = read_1_byte (abfd, line_ptr);
21470 line_ptr += 1;
21471 switch (extended_op)
21473 case DW_LNE_end_sequence:
21474 state_machine.handle_end_sequence ();
21475 end_sequence = true;
21476 break;
21477 case DW_LNE_set_address:
21479 CORE_ADDR address
21480 = read_address (abfd, line_ptr, cu, &bytes_read);
21481 line_ptr += bytes_read;
21483 state_machine.check_line_address (cu, line_ptr,
21484 lowpc - baseaddr, address);
21485 state_machine.handle_set_address (baseaddr, address);
21487 break;
21488 case DW_LNE_define_file:
21490 const char *cur_file;
21491 unsigned int mod_time, length;
21492 dir_index dindex;
21494 cur_file = read_direct_string (abfd, line_ptr,
21495 &bytes_read);
21496 line_ptr += bytes_read;
21497 dindex = (dir_index)
21498 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21499 line_ptr += bytes_read;
21500 mod_time =
21501 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21502 line_ptr += bytes_read;
21503 length =
21504 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21505 line_ptr += bytes_read;
21506 lh->add_file_name (cur_file, dindex, mod_time, length);
21508 break;
21509 case DW_LNE_set_discriminator:
21511 /* The discriminator is not interesting to the
21512 debugger; just ignore it. We still need to
21513 check its value though:
21514 if there are consecutive entries for the same
21515 (non-prologue) line we want to coalesce them.
21516 PR 17276. */
21517 unsigned int discr
21518 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21519 line_ptr += bytes_read;
21521 state_machine.handle_set_discriminator (discr);
21523 break;
21524 default:
21525 complaint (_("mangled .debug_line section"));
21526 return;
21528 /* Make sure that we parsed the extended op correctly. If e.g.
21529 we expected a different address size than the producer used,
21530 we may have read the wrong number of bytes. */
21531 if (line_ptr != extended_end)
21533 complaint (_("mangled .debug_line section"));
21534 return;
21536 break;
21537 case DW_LNS_copy:
21538 state_machine.handle_copy ();
21539 break;
21540 case DW_LNS_advance_pc:
21542 CORE_ADDR adjust
21543 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21544 line_ptr += bytes_read;
21546 state_machine.handle_advance_pc (adjust);
21548 break;
21549 case DW_LNS_advance_line:
21551 int line_delta
21552 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21553 line_ptr += bytes_read;
21555 state_machine.handle_advance_line (line_delta);
21557 break;
21558 case DW_LNS_set_file:
21560 file_name_index file
21561 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21562 &bytes_read);
21563 line_ptr += bytes_read;
21565 state_machine.handle_set_file (file);
21567 break;
21568 case DW_LNS_set_column:
21569 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21570 line_ptr += bytes_read;
21571 break;
21572 case DW_LNS_negate_stmt:
21573 state_machine.handle_negate_stmt ();
21574 break;
21575 case DW_LNS_set_basic_block:
21576 break;
21577 /* Add to the address register of the state machine the
21578 address increment value corresponding to special opcode
21579 255. I.e., this value is scaled by the minimum
21580 instruction length since special opcode 255 would have
21581 scaled the increment. */
21582 case DW_LNS_const_add_pc:
21583 state_machine.handle_const_add_pc ();
21584 break;
21585 case DW_LNS_fixed_advance_pc:
21587 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21588 line_ptr += 2;
21590 state_machine.handle_fixed_advance_pc (addr_adj);
21592 break;
21593 default:
21595 /* Unknown standard opcode, ignore it. */
21596 int i;
21598 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21600 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21601 line_ptr += bytes_read;
21607 if (!end_sequence)
21608 dwarf2_debug_line_missing_end_sequence_complaint ();
21610 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21611 in which case we still finish recording the last line). */
21612 state_machine.record_line (true);
21616 /* Decode the Line Number Program (LNP) for the given line_header
21617 structure and CU. The actual information extracted and the type
21618 of structures created from the LNP depends on the value of PST.
21620 1. If PST is NULL, then this procedure uses the data from the program
21621 to create all necessary symbol tables, and their linetables.
21623 2. If PST is not NULL, this procedure reads the program to determine
21624 the list of files included by the unit represented by PST, and
21625 builds all the associated partial symbol tables.
21627 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21628 It is used for relative paths in the line table.
21629 NOTE: When processing partial symtabs (pst != NULL),
21630 comp_dir == pst->dirname.
21632 NOTE: It is important that psymtabs have the same file name (via strcmp)
21633 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21634 symtab we don't use it in the name of the psymtabs we create.
21635 E.g. expand_line_sal requires this when finding psymtabs to expand.
21636 A good testcase for this is mb-inline.exp.
21638 LOWPC is the lowest address in CU (or 0 if not known).
21640 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21641 for its PC<->lines mapping information. Otherwise only the filename
21642 table is read in. */
21644 static void
21645 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21646 struct dwarf2_cu *cu, struct partial_symtab *pst,
21647 CORE_ADDR lowpc, int decode_mapping)
21649 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21650 const int decode_for_pst_p = (pst != NULL);
21652 if (decode_mapping)
21653 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21655 if (decode_for_pst_p)
21657 /* Now that we're done scanning the Line Header Program, we can
21658 create the psymtab of each included file. */
21659 for (auto &file_entry : lh->file_names ())
21660 if (file_entry.included_p == 1)
21662 gdb::unique_xmalloc_ptr<char> name_holder;
21663 const char *include_name =
21664 psymtab_include_file_name (lh, file_entry, pst,
21665 comp_dir, &name_holder);
21666 if (include_name != NULL)
21667 dwarf2_create_include_psymtab (include_name, pst, objfile);
21670 else
21672 /* Make sure a symtab is created for every file, even files
21673 which contain only variables (i.e. no code with associated
21674 line numbers). */
21675 buildsym_compunit *builder = cu->get_builder ();
21676 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21678 for (auto &fe : lh->file_names ())
21680 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21681 if (builder->get_current_subfile ()->symtab == NULL)
21683 builder->get_current_subfile ()->symtab
21684 = allocate_symtab (cust,
21685 builder->get_current_subfile ()->name);
21687 fe.symtab = builder->get_current_subfile ()->symtab;
21692 /* Start a subfile for DWARF. FILENAME is the name of the file and
21693 DIRNAME the name of the source directory which contains FILENAME
21694 or NULL if not known.
21695 This routine tries to keep line numbers from identical absolute and
21696 relative file names in a common subfile.
21698 Using the `list' example from the GDB testsuite, which resides in
21699 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21700 of /srcdir/list0.c yields the following debugging information for list0.c:
21702 DW_AT_name: /srcdir/list0.c
21703 DW_AT_comp_dir: /compdir
21704 files.files[0].name: list0.h
21705 files.files[0].dir: /srcdir
21706 files.files[1].name: list0.c
21707 files.files[1].dir: /srcdir
21709 The line number information for list0.c has to end up in a single
21710 subfile, so that `break /srcdir/list0.c:1' works as expected.
21711 start_subfile will ensure that this happens provided that we pass the
21712 concatenation of files.files[1].dir and files.files[1].name as the
21713 subfile's name. */
21715 static void
21716 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21717 const char *dirname)
21719 char *copy = NULL;
21721 /* In order not to lose the line information directory,
21722 we concatenate it to the filename when it makes sense.
21723 Note that the Dwarf3 standard says (speaking of filenames in line
21724 information): ``The directory index is ignored for file names
21725 that represent full path names''. Thus ignoring dirname in the
21726 `else' branch below isn't an issue. */
21728 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21730 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21731 filename = copy;
21734 cu->get_builder ()->start_subfile (filename);
21736 if (copy != NULL)
21737 xfree (copy);
21740 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21741 buildsym_compunit constructor. */
21743 struct compunit_symtab *
21744 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21745 CORE_ADDR low_pc)
21747 gdb_assert (m_builder == nullptr);
21749 m_builder.reset (new struct buildsym_compunit
21750 (per_cu->dwarf2_per_objfile->objfile,
21751 name, comp_dir, language, low_pc));
21753 list_in_scope = get_builder ()->get_file_symbols ();
21755 get_builder ()->record_debugformat ("DWARF 2");
21756 get_builder ()->record_producer (producer);
21758 processing_has_namespace_info = false;
21760 return get_builder ()->get_compunit_symtab ();
21763 static void
21764 var_decode_location (struct attribute *attr, struct symbol *sym,
21765 struct dwarf2_cu *cu)
21767 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21768 struct comp_unit_head *cu_header = &cu->header;
21770 /* NOTE drow/2003-01-30: There used to be a comment and some special
21771 code here to turn a symbol with DW_AT_external and a
21772 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21773 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21774 with some versions of binutils) where shared libraries could have
21775 relocations against symbols in their debug information - the
21776 minimal symbol would have the right address, but the debug info
21777 would not. It's no longer necessary, because we will explicitly
21778 apply relocations when we read in the debug information now. */
21780 /* A DW_AT_location attribute with no contents indicates that a
21781 variable has been optimized away. */
21782 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21784 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21785 return;
21788 /* Handle one degenerate form of location expression specially, to
21789 preserve GDB's previous behavior when section offsets are
21790 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21791 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21793 if (attr_form_is_block (attr)
21794 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21795 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21796 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21797 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21798 && (DW_BLOCK (attr)->size
21799 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21801 unsigned int dummy;
21803 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21804 SET_SYMBOL_VALUE_ADDRESS (sym,
21805 read_address (objfile->obfd,
21806 DW_BLOCK (attr)->data + 1,
21807 cu, &dummy));
21808 else
21809 SET_SYMBOL_VALUE_ADDRESS
21810 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21811 &dummy));
21812 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21813 fixup_symbol_section (sym, objfile);
21814 SET_SYMBOL_VALUE_ADDRESS (sym,
21815 SYMBOL_VALUE_ADDRESS (sym)
21816 + ANOFFSET (objfile->section_offsets,
21817 SYMBOL_SECTION (sym)));
21818 return;
21821 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21822 expression evaluator, and use LOC_COMPUTED only when necessary
21823 (i.e. when the value of a register or memory location is
21824 referenced, or a thread-local block, etc.). Then again, it might
21825 not be worthwhile. I'm assuming that it isn't unless performance
21826 or memory numbers show me otherwise. */
21828 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21830 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21831 cu->has_loclist = true;
21834 /* Given a pointer to a DWARF information entry, figure out if we need
21835 to make a symbol table entry for it, and if so, create a new entry
21836 and return a pointer to it.
21837 If TYPE is NULL, determine symbol type from the die, otherwise
21838 used the passed type.
21839 If SPACE is not NULL, use it to hold the new symbol. If it is
21840 NULL, allocate a new symbol on the objfile's obstack. */
21842 static struct symbol *
21843 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21844 struct symbol *space)
21846 struct dwarf2_per_objfile *dwarf2_per_objfile
21847 = cu->per_cu->dwarf2_per_objfile;
21848 struct objfile *objfile = dwarf2_per_objfile->objfile;
21849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21850 struct symbol *sym = NULL;
21851 const char *name;
21852 struct attribute *attr = NULL;
21853 struct attribute *attr2 = NULL;
21854 CORE_ADDR baseaddr;
21855 struct pending **list_to_add = NULL;
21857 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21859 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21861 name = dwarf2_name (die, cu);
21862 if (name)
21864 const char *linkagename;
21865 int suppress_add = 0;
21867 if (space)
21868 sym = space;
21869 else
21870 sym = allocate_symbol (objfile);
21871 OBJSTAT (objfile, n_syms++);
21873 /* Cache this symbol's name and the name's demangled form (if any). */
21874 sym->set_language (cu->language, &objfile->objfile_obstack);
21875 linkagename = dwarf2_physname (name, die, cu);
21876 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21878 /* Fortran does not have mangling standard and the mangling does differ
21879 between gfortran, iFort etc. */
21880 if (cu->language == language_fortran
21881 && symbol_get_demangled_name (sym) == NULL)
21882 symbol_set_demangled_name (sym,
21883 dwarf2_full_name (name, die, cu),
21884 NULL);
21886 /* Default assumptions.
21887 Use the passed type or decode it from the die. */
21888 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21889 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21890 if (type != NULL)
21891 SYMBOL_TYPE (sym) = type;
21892 else
21893 SYMBOL_TYPE (sym) = die_type (die, cu);
21894 attr = dwarf2_attr (die,
21895 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21896 cu);
21897 if (attr != nullptr)
21899 SYMBOL_LINE (sym) = DW_UNSND (attr);
21902 attr = dwarf2_attr (die,
21903 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21904 cu);
21905 if (attr != nullptr)
21907 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21908 struct file_entry *fe;
21910 if (cu->line_header != NULL)
21911 fe = cu->line_header->file_name_at (file_index);
21912 else
21913 fe = NULL;
21915 if (fe == NULL)
21916 complaint (_("file index out of range"));
21917 else
21918 symbol_set_symtab (sym, fe->symtab);
21921 switch (die->tag)
21923 case DW_TAG_label:
21924 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21925 if (attr != nullptr)
21927 CORE_ADDR addr;
21929 addr = attr_value_as_address (attr);
21930 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21931 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21933 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21934 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21935 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21936 add_symbol_to_list (sym, cu->list_in_scope);
21937 break;
21938 case DW_TAG_subprogram:
21939 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21940 finish_block. */
21941 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21943 if ((attr2 && (DW_UNSND (attr2) != 0))
21944 || cu->language == language_ada
21945 || cu->language == language_fortran)
21947 /* Subprograms marked external are stored as a global symbol.
21948 Ada and Fortran subprograms, whether marked external or
21949 not, are always stored as a global symbol, because we want
21950 to be able to access them globally. For instance, we want
21951 to be able to break on a nested subprogram without having
21952 to specify the context. */
21953 list_to_add = cu->get_builder ()->get_global_symbols ();
21955 else
21957 list_to_add = cu->list_in_scope;
21959 break;
21960 case DW_TAG_inlined_subroutine:
21961 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21962 finish_block. */
21963 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21964 SYMBOL_INLINED (sym) = 1;
21965 list_to_add = cu->list_in_scope;
21966 break;
21967 case DW_TAG_template_value_param:
21968 suppress_add = 1;
21969 /* Fall through. */
21970 case DW_TAG_constant:
21971 case DW_TAG_variable:
21972 case DW_TAG_member:
21973 /* Compilation with minimal debug info may result in
21974 variables with missing type entries. Change the
21975 misleading `void' type to something sensible. */
21976 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21977 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21979 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21980 /* In the case of DW_TAG_member, we should only be called for
21981 static const members. */
21982 if (die->tag == DW_TAG_member)
21984 /* dwarf2_add_field uses die_is_declaration,
21985 so we do the same. */
21986 gdb_assert (die_is_declaration (die, cu));
21987 gdb_assert (attr);
21989 if (attr != nullptr)
21991 dwarf2_const_value (attr, sym, cu);
21992 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21993 if (!suppress_add)
21995 if (attr2 && (DW_UNSND (attr2) != 0))
21996 list_to_add = cu->get_builder ()->get_global_symbols ();
21997 else
21998 list_to_add = cu->list_in_scope;
22000 break;
22002 attr = dwarf2_attr (die, DW_AT_location, cu);
22003 if (attr != nullptr)
22005 var_decode_location (attr, sym, cu);
22006 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22008 /* Fortran explicitly imports any global symbols to the local
22009 scope by DW_TAG_common_block. */
22010 if (cu->language == language_fortran && die->parent
22011 && die->parent->tag == DW_TAG_common_block)
22012 attr2 = NULL;
22014 if (SYMBOL_CLASS (sym) == LOC_STATIC
22015 && SYMBOL_VALUE_ADDRESS (sym) == 0
22016 && !dwarf2_per_objfile->has_section_at_zero)
22018 /* When a static variable is eliminated by the linker,
22019 the corresponding debug information is not stripped
22020 out, but the variable address is set to null;
22021 do not add such variables into symbol table. */
22023 else if (attr2 && (DW_UNSND (attr2) != 0))
22025 if (SYMBOL_CLASS (sym) == LOC_STATIC
22026 && (objfile->flags & OBJF_MAINLINE) == 0
22027 && dwarf2_per_objfile->can_copy)
22029 /* A global static variable might be subject to
22030 copy relocation. We first check for a local
22031 minsym, though, because maybe the symbol was
22032 marked hidden, in which case this would not
22033 apply. */
22034 bound_minimal_symbol found
22035 = (lookup_minimal_symbol_linkage
22036 (sym->linkage_name (), objfile));
22037 if (found.minsym != nullptr)
22038 sym->maybe_copied = 1;
22041 /* A variable with DW_AT_external is never static,
22042 but it may be block-scoped. */
22043 list_to_add
22044 = ((cu->list_in_scope
22045 == cu->get_builder ()->get_file_symbols ())
22046 ? cu->get_builder ()->get_global_symbols ()
22047 : cu->list_in_scope);
22049 else
22050 list_to_add = cu->list_in_scope;
22052 else
22054 /* We do not know the address of this symbol.
22055 If it is an external symbol and we have type information
22056 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22057 The address of the variable will then be determined from
22058 the minimal symbol table whenever the variable is
22059 referenced. */
22060 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22062 /* Fortran explicitly imports any global symbols to the local
22063 scope by DW_TAG_common_block. */
22064 if (cu->language == language_fortran && die->parent
22065 && die->parent->tag == DW_TAG_common_block)
22067 /* SYMBOL_CLASS doesn't matter here because
22068 read_common_block is going to reset it. */
22069 if (!suppress_add)
22070 list_to_add = cu->list_in_scope;
22072 else if (attr2 && (DW_UNSND (attr2) != 0)
22073 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22075 /* A variable with DW_AT_external is never static, but it
22076 may be block-scoped. */
22077 list_to_add
22078 = ((cu->list_in_scope
22079 == cu->get_builder ()->get_file_symbols ())
22080 ? cu->get_builder ()->get_global_symbols ()
22081 : cu->list_in_scope);
22083 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22085 else if (!die_is_declaration (die, cu))
22087 /* Use the default LOC_OPTIMIZED_OUT class. */
22088 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22089 if (!suppress_add)
22090 list_to_add = cu->list_in_scope;
22093 break;
22094 case DW_TAG_formal_parameter:
22096 /* If we are inside a function, mark this as an argument. If
22097 not, we might be looking at an argument to an inlined function
22098 when we do not have enough information to show inlined frames;
22099 pretend it's a local variable in that case so that the user can
22100 still see it. */
22101 struct context_stack *curr
22102 = cu->get_builder ()->get_current_context_stack ();
22103 if (curr != nullptr && curr->name != nullptr)
22104 SYMBOL_IS_ARGUMENT (sym) = 1;
22105 attr = dwarf2_attr (die, DW_AT_location, cu);
22106 if (attr != nullptr)
22108 var_decode_location (attr, sym, cu);
22110 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22111 if (attr != nullptr)
22113 dwarf2_const_value (attr, sym, cu);
22116 list_to_add = cu->list_in_scope;
22118 break;
22119 case DW_TAG_unspecified_parameters:
22120 /* From varargs functions; gdb doesn't seem to have any
22121 interest in this information, so just ignore it for now.
22122 (FIXME?) */
22123 break;
22124 case DW_TAG_template_type_param:
22125 suppress_add = 1;
22126 /* Fall through. */
22127 case DW_TAG_class_type:
22128 case DW_TAG_interface_type:
22129 case DW_TAG_structure_type:
22130 case DW_TAG_union_type:
22131 case DW_TAG_set_type:
22132 case DW_TAG_enumeration_type:
22133 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22134 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22137 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22138 really ever be static objects: otherwise, if you try
22139 to, say, break of a class's method and you're in a file
22140 which doesn't mention that class, it won't work unless
22141 the check for all static symbols in lookup_symbol_aux
22142 saves you. See the OtherFileClass tests in
22143 gdb.c++/namespace.exp. */
22145 if (!suppress_add)
22147 buildsym_compunit *builder = cu->get_builder ();
22148 list_to_add
22149 = (cu->list_in_scope == builder->get_file_symbols ()
22150 && cu->language == language_cplus
22151 ? builder->get_global_symbols ()
22152 : cu->list_in_scope);
22154 /* The semantics of C++ state that "struct foo {
22155 ... }" also defines a typedef for "foo". */
22156 if (cu->language == language_cplus
22157 || cu->language == language_ada
22158 || cu->language == language_d
22159 || cu->language == language_rust)
22161 /* The symbol's name is already allocated along
22162 with this objfile, so we don't need to
22163 duplicate it for the type. */
22164 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22165 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22169 break;
22170 case DW_TAG_typedef:
22171 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22172 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22173 list_to_add = cu->list_in_scope;
22174 break;
22175 case DW_TAG_base_type:
22176 case DW_TAG_subrange_type:
22177 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22178 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22179 list_to_add = cu->list_in_scope;
22180 break;
22181 case DW_TAG_enumerator:
22182 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22183 if (attr != nullptr)
22185 dwarf2_const_value (attr, sym, cu);
22188 /* NOTE: carlton/2003-11-10: See comment above in the
22189 DW_TAG_class_type, etc. block. */
22191 list_to_add
22192 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22193 && cu->language == language_cplus
22194 ? cu->get_builder ()->get_global_symbols ()
22195 : cu->list_in_scope);
22197 break;
22198 case DW_TAG_imported_declaration:
22199 case DW_TAG_namespace:
22200 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22201 list_to_add = cu->get_builder ()->get_global_symbols ();
22202 break;
22203 case DW_TAG_module:
22204 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22205 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22206 list_to_add = cu->get_builder ()->get_global_symbols ();
22207 break;
22208 case DW_TAG_common_block:
22209 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22210 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22211 add_symbol_to_list (sym, cu->list_in_scope);
22212 break;
22213 default:
22214 /* Not a tag we recognize. Hopefully we aren't processing
22215 trash data, but since we must specifically ignore things
22216 we don't recognize, there is nothing else we should do at
22217 this point. */
22218 complaint (_("unsupported tag: '%s'"),
22219 dwarf_tag_name (die->tag));
22220 break;
22223 if (suppress_add)
22225 sym->hash_next = objfile->template_symbols;
22226 objfile->template_symbols = sym;
22227 list_to_add = NULL;
22230 if (list_to_add != NULL)
22231 add_symbol_to_list (sym, list_to_add);
22233 /* For the benefit of old versions of GCC, check for anonymous
22234 namespaces based on the demangled name. */
22235 if (!cu->processing_has_namespace_info
22236 && cu->language == language_cplus)
22237 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22239 return (sym);
22242 /* Given an attr with a DW_FORM_dataN value in host byte order,
22243 zero-extend it as appropriate for the symbol's type. The DWARF
22244 standard (v4) is not entirely clear about the meaning of using
22245 DW_FORM_dataN for a constant with a signed type, where the type is
22246 wider than the data. The conclusion of a discussion on the DWARF
22247 list was that this is unspecified. We choose to always zero-extend
22248 because that is the interpretation long in use by GCC. */
22250 static gdb_byte *
22251 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22252 struct dwarf2_cu *cu, LONGEST *value, int bits)
22254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22255 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22256 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22257 LONGEST l = DW_UNSND (attr);
22259 if (bits < sizeof (*value) * 8)
22261 l &= ((LONGEST) 1 << bits) - 1;
22262 *value = l;
22264 else if (bits == sizeof (*value) * 8)
22265 *value = l;
22266 else
22268 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22269 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22270 return bytes;
22273 return NULL;
22276 /* Read a constant value from an attribute. Either set *VALUE, or if
22277 the value does not fit in *VALUE, set *BYTES - either already
22278 allocated on the objfile obstack, or newly allocated on OBSTACK,
22279 or, set *BATON, if we translated the constant to a location
22280 expression. */
22282 static void
22283 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22284 const char *name, struct obstack *obstack,
22285 struct dwarf2_cu *cu,
22286 LONGEST *value, const gdb_byte **bytes,
22287 struct dwarf2_locexpr_baton **baton)
22289 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22290 struct comp_unit_head *cu_header = &cu->header;
22291 struct dwarf_block *blk;
22292 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22293 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22295 *value = 0;
22296 *bytes = NULL;
22297 *baton = NULL;
22299 switch (attr->form)
22301 case DW_FORM_addr:
22302 case DW_FORM_addrx:
22303 case DW_FORM_GNU_addr_index:
22305 gdb_byte *data;
22307 if (TYPE_LENGTH (type) != cu_header->addr_size)
22308 dwarf2_const_value_length_mismatch_complaint (name,
22309 cu_header->addr_size,
22310 TYPE_LENGTH (type));
22311 /* Symbols of this form are reasonably rare, so we just
22312 piggyback on the existing location code rather than writing
22313 a new implementation of symbol_computed_ops. */
22314 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22315 (*baton)->per_cu = cu->per_cu;
22316 gdb_assert ((*baton)->per_cu);
22318 (*baton)->size = 2 + cu_header->addr_size;
22319 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22320 (*baton)->data = data;
22322 data[0] = DW_OP_addr;
22323 store_unsigned_integer (&data[1], cu_header->addr_size,
22324 byte_order, DW_ADDR (attr));
22325 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22327 break;
22328 case DW_FORM_string:
22329 case DW_FORM_strp:
22330 case DW_FORM_strx:
22331 case DW_FORM_GNU_str_index:
22332 case DW_FORM_GNU_strp_alt:
22333 /* DW_STRING is already allocated on the objfile obstack, point
22334 directly to it. */
22335 *bytes = (const gdb_byte *) DW_STRING (attr);
22336 break;
22337 case DW_FORM_block1:
22338 case DW_FORM_block2:
22339 case DW_FORM_block4:
22340 case DW_FORM_block:
22341 case DW_FORM_exprloc:
22342 case DW_FORM_data16:
22343 blk = DW_BLOCK (attr);
22344 if (TYPE_LENGTH (type) != blk->size)
22345 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22346 TYPE_LENGTH (type));
22347 *bytes = blk->data;
22348 break;
22350 /* The DW_AT_const_value attributes are supposed to carry the
22351 symbol's value "represented as it would be on the target
22352 architecture." By the time we get here, it's already been
22353 converted to host endianness, so we just need to sign- or
22354 zero-extend it as appropriate. */
22355 case DW_FORM_data1:
22356 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22357 break;
22358 case DW_FORM_data2:
22359 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22360 break;
22361 case DW_FORM_data4:
22362 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22363 break;
22364 case DW_FORM_data8:
22365 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22366 break;
22368 case DW_FORM_sdata:
22369 case DW_FORM_implicit_const:
22370 *value = DW_SND (attr);
22371 break;
22373 case DW_FORM_udata:
22374 *value = DW_UNSND (attr);
22375 break;
22377 default:
22378 complaint (_("unsupported const value attribute form: '%s'"),
22379 dwarf_form_name (attr->form));
22380 *value = 0;
22381 break;
22386 /* Copy constant value from an attribute to a symbol. */
22388 static void
22389 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22390 struct dwarf2_cu *cu)
22392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22393 LONGEST value;
22394 const gdb_byte *bytes;
22395 struct dwarf2_locexpr_baton *baton;
22397 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22398 sym->print_name (),
22399 &objfile->objfile_obstack, cu,
22400 &value, &bytes, &baton);
22402 if (baton != NULL)
22404 SYMBOL_LOCATION_BATON (sym) = baton;
22405 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22407 else if (bytes != NULL)
22409 SYMBOL_VALUE_BYTES (sym) = bytes;
22410 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22412 else
22414 SYMBOL_VALUE (sym) = value;
22415 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22419 /* Return the type of the die in question using its DW_AT_type attribute. */
22421 static struct type *
22422 die_type (struct die_info *die, struct dwarf2_cu *cu)
22424 struct attribute *type_attr;
22426 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22427 if (!type_attr)
22429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22430 /* A missing DW_AT_type represents a void type. */
22431 return objfile_type (objfile)->builtin_void;
22434 return lookup_die_type (die, type_attr, cu);
22437 /* True iff CU's producer generates GNAT Ada auxiliary information
22438 that allows to find parallel types through that information instead
22439 of having to do expensive parallel lookups by type name. */
22441 static int
22442 need_gnat_info (struct dwarf2_cu *cu)
22444 /* Assume that the Ada compiler was GNAT, which always produces
22445 the auxiliary information. */
22446 return (cu->language == language_ada);
22449 /* Return the auxiliary type of the die in question using its
22450 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22451 attribute is not present. */
22453 static struct type *
22454 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22456 struct attribute *type_attr;
22458 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22459 if (!type_attr)
22460 return NULL;
22462 return lookup_die_type (die, type_attr, cu);
22465 /* If DIE has a descriptive_type attribute, then set the TYPE's
22466 descriptive type accordingly. */
22468 static void
22469 set_descriptive_type (struct type *type, struct die_info *die,
22470 struct dwarf2_cu *cu)
22472 struct type *descriptive_type = die_descriptive_type (die, cu);
22474 if (descriptive_type)
22476 ALLOCATE_GNAT_AUX_TYPE (type);
22477 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22481 /* Return the containing type of the die in question using its
22482 DW_AT_containing_type attribute. */
22484 static struct type *
22485 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22487 struct attribute *type_attr;
22488 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22490 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22491 if (!type_attr)
22492 error (_("Dwarf Error: Problem turning containing type into gdb type "
22493 "[in module %s]"), objfile_name (objfile));
22495 return lookup_die_type (die, type_attr, cu);
22498 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22500 static struct type *
22501 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22503 struct dwarf2_per_objfile *dwarf2_per_objfile
22504 = cu->per_cu->dwarf2_per_objfile;
22505 struct objfile *objfile = dwarf2_per_objfile->objfile;
22506 char *saved;
22508 std::string message
22509 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22510 objfile_name (objfile),
22511 sect_offset_str (cu->header.sect_off),
22512 sect_offset_str (die->sect_off));
22513 saved = obstack_strdup (&objfile->objfile_obstack, message);
22515 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22518 /* Look up the type of DIE in CU using its type attribute ATTR.
22519 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22520 DW_AT_containing_type.
22521 If there is no type substitute an error marker. */
22523 static struct type *
22524 lookup_die_type (struct die_info *die, const struct attribute *attr,
22525 struct dwarf2_cu *cu)
22527 struct dwarf2_per_objfile *dwarf2_per_objfile
22528 = cu->per_cu->dwarf2_per_objfile;
22529 struct objfile *objfile = dwarf2_per_objfile->objfile;
22530 struct type *this_type;
22532 gdb_assert (attr->name == DW_AT_type
22533 || attr->name == DW_AT_GNAT_descriptive_type
22534 || attr->name == DW_AT_containing_type);
22536 /* First see if we have it cached. */
22538 if (attr->form == DW_FORM_GNU_ref_alt)
22540 struct dwarf2_per_cu_data *per_cu;
22541 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22543 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22544 dwarf2_per_objfile);
22545 this_type = get_die_type_at_offset (sect_off, per_cu);
22547 else if (attr_form_is_ref (attr))
22549 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22551 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22553 else if (attr->form == DW_FORM_ref_sig8)
22555 ULONGEST signature = DW_SIGNATURE (attr);
22557 return get_signatured_type (die, signature, cu);
22559 else
22561 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22562 " at %s [in module %s]"),
22563 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22564 objfile_name (objfile));
22565 return build_error_marker_type (cu, die);
22568 /* If not cached we need to read it in. */
22570 if (this_type == NULL)
22572 struct die_info *type_die = NULL;
22573 struct dwarf2_cu *type_cu = cu;
22575 if (attr_form_is_ref (attr))
22576 type_die = follow_die_ref (die, attr, &type_cu);
22577 if (type_die == NULL)
22578 return build_error_marker_type (cu, die);
22579 /* If we find the type now, it's probably because the type came
22580 from an inter-CU reference and the type's CU got expanded before
22581 ours. */
22582 this_type = read_type_die (type_die, type_cu);
22585 /* If we still don't have a type use an error marker. */
22587 if (this_type == NULL)
22588 return build_error_marker_type (cu, die);
22590 return this_type;
22593 /* Return the type in DIE, CU.
22594 Returns NULL for invalid types.
22596 This first does a lookup in die_type_hash,
22597 and only reads the die in if necessary.
22599 NOTE: This can be called when reading in partial or full symbols. */
22601 static struct type *
22602 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22604 struct type *this_type;
22606 this_type = get_die_type (die, cu);
22607 if (this_type)
22608 return this_type;
22610 return read_type_die_1 (die, cu);
22613 /* Read the type in DIE, CU.
22614 Returns NULL for invalid types. */
22616 static struct type *
22617 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22619 struct type *this_type = NULL;
22621 switch (die->tag)
22623 case DW_TAG_class_type:
22624 case DW_TAG_interface_type:
22625 case DW_TAG_structure_type:
22626 case DW_TAG_union_type:
22627 this_type = read_structure_type (die, cu);
22628 break;
22629 case DW_TAG_enumeration_type:
22630 this_type = read_enumeration_type (die, cu);
22631 break;
22632 case DW_TAG_subprogram:
22633 case DW_TAG_subroutine_type:
22634 case DW_TAG_inlined_subroutine:
22635 this_type = read_subroutine_type (die, cu);
22636 break;
22637 case DW_TAG_array_type:
22638 this_type = read_array_type (die, cu);
22639 break;
22640 case DW_TAG_set_type:
22641 this_type = read_set_type (die, cu);
22642 break;
22643 case DW_TAG_pointer_type:
22644 this_type = read_tag_pointer_type (die, cu);
22645 break;
22646 case DW_TAG_ptr_to_member_type:
22647 this_type = read_tag_ptr_to_member_type (die, cu);
22648 break;
22649 case DW_TAG_reference_type:
22650 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22651 break;
22652 case DW_TAG_rvalue_reference_type:
22653 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22654 break;
22655 case DW_TAG_const_type:
22656 this_type = read_tag_const_type (die, cu);
22657 break;
22658 case DW_TAG_volatile_type:
22659 this_type = read_tag_volatile_type (die, cu);
22660 break;
22661 case DW_TAG_restrict_type:
22662 this_type = read_tag_restrict_type (die, cu);
22663 break;
22664 case DW_TAG_string_type:
22665 this_type = read_tag_string_type (die, cu);
22666 break;
22667 case DW_TAG_typedef:
22668 this_type = read_typedef (die, cu);
22669 break;
22670 case DW_TAG_subrange_type:
22671 this_type = read_subrange_type (die, cu);
22672 break;
22673 case DW_TAG_base_type:
22674 this_type = read_base_type (die, cu);
22675 break;
22676 case DW_TAG_unspecified_type:
22677 this_type = read_unspecified_type (die, cu);
22678 break;
22679 case DW_TAG_namespace:
22680 this_type = read_namespace_type (die, cu);
22681 break;
22682 case DW_TAG_module:
22683 this_type = read_module_type (die, cu);
22684 break;
22685 case DW_TAG_atomic_type:
22686 this_type = read_tag_atomic_type (die, cu);
22687 break;
22688 default:
22689 complaint (_("unexpected tag in read_type_die: '%s'"),
22690 dwarf_tag_name (die->tag));
22691 break;
22694 return this_type;
22697 /* See if we can figure out if the class lives in a namespace. We do
22698 this by looking for a member function; its demangled name will
22699 contain namespace info, if there is any.
22700 Return the computed name or NULL.
22701 Space for the result is allocated on the objfile's obstack.
22702 This is the full-die version of guess_partial_die_structure_name.
22703 In this case we know DIE has no useful parent. */
22705 static char *
22706 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22708 struct die_info *spec_die;
22709 struct dwarf2_cu *spec_cu;
22710 struct die_info *child;
22711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22713 spec_cu = cu;
22714 spec_die = die_specification (die, &spec_cu);
22715 if (spec_die != NULL)
22717 die = spec_die;
22718 cu = spec_cu;
22721 for (child = die->child;
22722 child != NULL;
22723 child = child->sibling)
22725 if (child->tag == DW_TAG_subprogram)
22727 const char *linkage_name = dw2_linkage_name (child, cu);
22729 if (linkage_name != NULL)
22731 char *actual_name
22732 = language_class_name_from_physname (cu->language_defn,
22733 linkage_name);
22734 char *name = NULL;
22736 if (actual_name != NULL)
22738 const char *die_name = dwarf2_name (die, cu);
22740 if (die_name != NULL
22741 && strcmp (die_name, actual_name) != 0)
22743 /* Strip off the class name from the full name.
22744 We want the prefix. */
22745 int die_name_len = strlen (die_name);
22746 int actual_name_len = strlen (actual_name);
22748 /* Test for '::' as a sanity check. */
22749 if (actual_name_len > die_name_len + 2
22750 && actual_name[actual_name_len
22751 - die_name_len - 1] == ':')
22752 name = obstack_strndup (
22753 &objfile->per_bfd->storage_obstack,
22754 actual_name, actual_name_len - die_name_len - 2);
22757 xfree (actual_name);
22758 return name;
22763 return NULL;
22766 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22767 prefix part in such case. See
22768 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22770 static const char *
22771 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22773 struct attribute *attr;
22774 const char *base;
22776 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22777 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22778 return NULL;
22780 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22781 return NULL;
22783 attr = dw2_linkage_name_attr (die, cu);
22784 if (attr == NULL || DW_STRING (attr) == NULL)
22785 return NULL;
22787 /* dwarf2_name had to be already called. */
22788 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22790 /* Strip the base name, keep any leading namespaces/classes. */
22791 base = strrchr (DW_STRING (attr), ':');
22792 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22793 return "";
22795 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22796 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22797 DW_STRING (attr),
22798 &base[-1] - DW_STRING (attr));
22801 /* Return the name of the namespace/class that DIE is defined within,
22802 or "" if we can't tell. The caller should not xfree the result.
22804 For example, if we're within the method foo() in the following
22805 code:
22807 namespace N {
22808 class C {
22809 void foo () {
22814 then determine_prefix on foo's die will return "N::C". */
22816 static const char *
22817 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22819 struct dwarf2_per_objfile *dwarf2_per_objfile
22820 = cu->per_cu->dwarf2_per_objfile;
22821 struct die_info *parent, *spec_die;
22822 struct dwarf2_cu *spec_cu;
22823 struct type *parent_type;
22824 const char *retval;
22826 if (cu->language != language_cplus
22827 && cu->language != language_fortran && cu->language != language_d
22828 && cu->language != language_rust)
22829 return "";
22831 retval = anonymous_struct_prefix (die, cu);
22832 if (retval)
22833 return retval;
22835 /* We have to be careful in the presence of DW_AT_specification.
22836 For example, with GCC 3.4, given the code
22838 namespace N {
22839 void foo() {
22840 // Definition of N::foo.
22844 then we'll have a tree of DIEs like this:
22846 1: DW_TAG_compile_unit
22847 2: DW_TAG_namespace // N
22848 3: DW_TAG_subprogram // declaration of N::foo
22849 4: DW_TAG_subprogram // definition of N::foo
22850 DW_AT_specification // refers to die #3
22852 Thus, when processing die #4, we have to pretend that we're in
22853 the context of its DW_AT_specification, namely the contex of die
22854 #3. */
22855 spec_cu = cu;
22856 spec_die = die_specification (die, &spec_cu);
22857 if (spec_die == NULL)
22858 parent = die->parent;
22859 else
22861 parent = spec_die->parent;
22862 cu = spec_cu;
22865 if (parent == NULL)
22866 return "";
22867 else if (parent->building_fullname)
22869 const char *name;
22870 const char *parent_name;
22872 /* It has been seen on RealView 2.2 built binaries,
22873 DW_TAG_template_type_param types actually _defined_ as
22874 children of the parent class:
22876 enum E {};
22877 template class <class Enum> Class{};
22878 Class<enum E> class_e;
22880 1: DW_TAG_class_type (Class)
22881 2: DW_TAG_enumeration_type (E)
22882 3: DW_TAG_enumerator (enum1:0)
22883 3: DW_TAG_enumerator (enum2:1)
22885 2: DW_TAG_template_type_param
22886 DW_AT_type DW_FORM_ref_udata (E)
22888 Besides being broken debug info, it can put GDB into an
22889 infinite loop. Consider:
22891 When we're building the full name for Class<E>, we'll start
22892 at Class, and go look over its template type parameters,
22893 finding E. We'll then try to build the full name of E, and
22894 reach here. We're now trying to build the full name of E,
22895 and look over the parent DIE for containing scope. In the
22896 broken case, if we followed the parent DIE of E, we'd again
22897 find Class, and once again go look at its template type
22898 arguments, etc., etc. Simply don't consider such parent die
22899 as source-level parent of this die (it can't be, the language
22900 doesn't allow it), and break the loop here. */
22901 name = dwarf2_name (die, cu);
22902 parent_name = dwarf2_name (parent, cu);
22903 complaint (_("template param type '%s' defined within parent '%s'"),
22904 name ? name : "<unknown>",
22905 parent_name ? parent_name : "<unknown>");
22906 return "";
22908 else
22909 switch (parent->tag)
22911 case DW_TAG_namespace:
22912 parent_type = read_type_die (parent, cu);
22913 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22914 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22915 Work around this problem here. */
22916 if (cu->language == language_cplus
22917 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22918 return "";
22919 /* We give a name to even anonymous namespaces. */
22920 return TYPE_NAME (parent_type);
22921 case DW_TAG_class_type:
22922 case DW_TAG_interface_type:
22923 case DW_TAG_structure_type:
22924 case DW_TAG_union_type:
22925 case DW_TAG_module:
22926 parent_type = read_type_die (parent, cu);
22927 if (TYPE_NAME (parent_type) != NULL)
22928 return TYPE_NAME (parent_type);
22929 else
22930 /* An anonymous structure is only allowed non-static data
22931 members; no typedefs, no member functions, et cetera.
22932 So it does not need a prefix. */
22933 return "";
22934 case DW_TAG_compile_unit:
22935 case DW_TAG_partial_unit:
22936 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22937 if (cu->language == language_cplus
22938 && !dwarf2_per_objfile->types.empty ()
22939 && die->child != NULL
22940 && (die->tag == DW_TAG_class_type
22941 || die->tag == DW_TAG_structure_type
22942 || die->tag == DW_TAG_union_type))
22944 char *name = guess_full_die_structure_name (die, cu);
22945 if (name != NULL)
22946 return name;
22948 return "";
22949 case DW_TAG_subprogram:
22950 /* Nested subroutines in Fortran get a prefix with the name
22951 of the parent's subroutine. */
22952 if (cu->language == language_fortran)
22954 if ((die->tag == DW_TAG_subprogram)
22955 && (dwarf2_name (parent, cu) != NULL))
22956 return dwarf2_name (parent, cu);
22958 return determine_prefix (parent, cu);
22959 case DW_TAG_enumeration_type:
22960 parent_type = read_type_die (parent, cu);
22961 if (TYPE_DECLARED_CLASS (parent_type))
22963 if (TYPE_NAME (parent_type) != NULL)
22964 return TYPE_NAME (parent_type);
22965 return "";
22967 /* Fall through. */
22968 default:
22969 return determine_prefix (parent, cu);
22973 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22974 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22975 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22976 an obconcat, otherwise allocate storage for the result. The CU argument is
22977 used to determine the language and hence, the appropriate separator. */
22979 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22981 static char *
22982 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22983 int physname, struct dwarf2_cu *cu)
22985 const char *lead = "";
22986 const char *sep;
22988 if (suffix == NULL || suffix[0] == '\0'
22989 || prefix == NULL || prefix[0] == '\0')
22990 sep = "";
22991 else if (cu->language == language_d)
22993 /* For D, the 'main' function could be defined in any module, but it
22994 should never be prefixed. */
22995 if (strcmp (suffix, "D main") == 0)
22997 prefix = "";
22998 sep = "";
23000 else
23001 sep = ".";
23003 else if (cu->language == language_fortran && physname)
23005 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23006 DW_AT_MIPS_linkage_name is preferred and used instead. */
23008 lead = "__";
23009 sep = "_MOD_";
23011 else
23012 sep = "::";
23014 if (prefix == NULL)
23015 prefix = "";
23016 if (suffix == NULL)
23017 suffix = "";
23019 if (obs == NULL)
23021 char *retval
23022 = ((char *)
23023 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23025 strcpy (retval, lead);
23026 strcat (retval, prefix);
23027 strcat (retval, sep);
23028 strcat (retval, suffix);
23029 return retval;
23031 else
23033 /* We have an obstack. */
23034 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23038 /* Return sibling of die, NULL if no sibling. */
23040 static struct die_info *
23041 sibling_die (struct die_info *die)
23043 return die->sibling;
23046 /* Get name of a die, return NULL if not found. */
23048 static const char *
23049 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23050 struct obstack *obstack)
23052 if (name && cu->language == language_cplus)
23054 std::string canon_name = cp_canonicalize_string (name);
23056 if (!canon_name.empty ())
23058 if (canon_name != name)
23059 name = obstack_strdup (obstack, canon_name);
23063 return name;
23066 /* Get name of a die, return NULL if not found.
23067 Anonymous namespaces are converted to their magic string. */
23069 static const char *
23070 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23072 struct attribute *attr;
23073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23075 attr = dwarf2_attr (die, DW_AT_name, cu);
23076 if ((!attr || !DW_STRING (attr))
23077 && die->tag != DW_TAG_namespace
23078 && die->tag != DW_TAG_class_type
23079 && die->tag != DW_TAG_interface_type
23080 && die->tag != DW_TAG_structure_type
23081 && die->tag != DW_TAG_union_type)
23082 return NULL;
23084 switch (die->tag)
23086 case DW_TAG_compile_unit:
23087 case DW_TAG_partial_unit:
23088 /* Compilation units have a DW_AT_name that is a filename, not
23089 a source language identifier. */
23090 case DW_TAG_enumeration_type:
23091 case DW_TAG_enumerator:
23092 /* These tags always have simple identifiers already; no need
23093 to canonicalize them. */
23094 return DW_STRING (attr);
23096 case DW_TAG_namespace:
23097 if (attr != NULL && DW_STRING (attr) != NULL)
23098 return DW_STRING (attr);
23099 return CP_ANONYMOUS_NAMESPACE_STR;
23101 case DW_TAG_class_type:
23102 case DW_TAG_interface_type:
23103 case DW_TAG_structure_type:
23104 case DW_TAG_union_type:
23105 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23106 structures or unions. These were of the form "._%d" in GCC 4.1,
23107 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23108 and GCC 4.4. We work around this problem by ignoring these. */
23109 if (attr && DW_STRING (attr)
23110 && (startswith (DW_STRING (attr), "._")
23111 || startswith (DW_STRING (attr), "<anonymous")))
23112 return NULL;
23114 /* GCC might emit a nameless typedef that has a linkage name. See
23115 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23116 if (!attr || DW_STRING (attr) == NULL)
23118 char *demangled = NULL;
23120 attr = dw2_linkage_name_attr (die, cu);
23121 if (attr == NULL || DW_STRING (attr) == NULL)
23122 return NULL;
23124 /* Avoid demangling DW_STRING (attr) the second time on a second
23125 call for the same DIE. */
23126 if (!DW_STRING_IS_CANONICAL (attr))
23127 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
23129 if (demangled)
23131 const char *base;
23133 /* FIXME: we already did this for the partial symbol... */
23134 DW_STRING (attr)
23135 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23136 demangled);
23137 DW_STRING_IS_CANONICAL (attr) = 1;
23138 xfree (demangled);
23140 /* Strip any leading namespaces/classes, keep only the base name.
23141 DW_AT_name for named DIEs does not contain the prefixes. */
23142 base = strrchr (DW_STRING (attr), ':');
23143 if (base && base > DW_STRING (attr) && base[-1] == ':')
23144 return &base[1];
23145 else
23146 return DW_STRING (attr);
23149 break;
23151 default:
23152 break;
23155 if (!DW_STRING_IS_CANONICAL (attr))
23157 DW_STRING (attr)
23158 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23159 &objfile->per_bfd->storage_obstack);
23160 DW_STRING_IS_CANONICAL (attr) = 1;
23162 return DW_STRING (attr);
23165 /* Return the die that this die in an extension of, or NULL if there
23166 is none. *EXT_CU is the CU containing DIE on input, and the CU
23167 containing the return value on output. */
23169 static struct die_info *
23170 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23172 struct attribute *attr;
23174 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23175 if (attr == NULL)
23176 return NULL;
23178 return follow_die_ref (die, attr, ext_cu);
23181 /* A convenience function that returns an "unknown" DWARF name,
23182 including the value of V. STR is the name of the entity being
23183 printed, e.g., "TAG". */
23185 static const char *
23186 dwarf_unknown (const char *str, unsigned v)
23188 char *cell = get_print_cell ();
23189 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23190 return cell;
23193 /* Convert a DIE tag into its string name. */
23195 static const char *
23196 dwarf_tag_name (unsigned tag)
23198 const char *name = get_DW_TAG_name (tag);
23200 if (name == NULL)
23201 return dwarf_unknown ("TAG", tag);
23203 return name;
23206 /* Convert a DWARF attribute code into its string name. */
23208 static const char *
23209 dwarf_attr_name (unsigned attr)
23211 const char *name;
23213 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23214 if (attr == DW_AT_MIPS_fde)
23215 return "DW_AT_MIPS_fde";
23216 #else
23217 if (attr == DW_AT_HP_block_index)
23218 return "DW_AT_HP_block_index";
23219 #endif
23221 name = get_DW_AT_name (attr);
23223 if (name == NULL)
23224 return dwarf_unknown ("AT", attr);
23226 return name;
23229 /* Convert a unit type to corresponding DW_UT name. */
23231 static const char *
23232 dwarf_unit_type_name (int unit_type) {
23233 switch (unit_type)
23235 case 0x01:
23236 return "DW_UT_compile (0x01)";
23237 case 0x02:
23238 return "DW_UT_type (0x02)";
23239 case 0x03:
23240 return "DW_UT_partial (0x03)";
23241 case 0x04:
23242 return "DW_UT_skeleton (0x04)";
23243 case 0x05:
23244 return "DW_UT_split_compile (0x05)";
23245 case 0x06:
23246 return "DW_UT_split_type (0x06)";
23247 case 0x80:
23248 return "DW_UT_lo_user (0x80)";
23249 case 0xff:
23250 return "DW_UT_hi_user (0xff)";
23251 default:
23252 return nullptr;
23256 /* Convert a DWARF value form code into its string name. */
23258 static const char *
23259 dwarf_form_name (unsigned form)
23261 const char *name = get_DW_FORM_name (form);
23263 if (name == NULL)
23264 return dwarf_unknown ("FORM", form);
23266 return name;
23269 static const char *
23270 dwarf_bool_name (unsigned mybool)
23272 if (mybool)
23273 return "TRUE";
23274 else
23275 return "FALSE";
23278 /* Convert a DWARF type code into its string name. */
23280 static const char *
23281 dwarf_type_encoding_name (unsigned enc)
23283 const char *name = get_DW_ATE_name (enc);
23285 if (name == NULL)
23286 return dwarf_unknown ("ATE", enc);
23288 return name;
23291 static void
23292 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23294 unsigned int i;
23296 print_spaces (indent, f);
23297 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23298 dwarf_tag_name (die->tag), die->abbrev,
23299 sect_offset_str (die->sect_off));
23301 if (die->parent != NULL)
23303 print_spaces (indent, f);
23304 fprintf_unfiltered (f, " parent at offset: %s\n",
23305 sect_offset_str (die->parent->sect_off));
23308 print_spaces (indent, f);
23309 fprintf_unfiltered (f, " has children: %s\n",
23310 dwarf_bool_name (die->child != NULL));
23312 print_spaces (indent, f);
23313 fprintf_unfiltered (f, " attributes:\n");
23315 for (i = 0; i < die->num_attrs; ++i)
23317 print_spaces (indent, f);
23318 fprintf_unfiltered (f, " %s (%s) ",
23319 dwarf_attr_name (die->attrs[i].name),
23320 dwarf_form_name (die->attrs[i].form));
23322 switch (die->attrs[i].form)
23324 case DW_FORM_addr:
23325 case DW_FORM_addrx:
23326 case DW_FORM_GNU_addr_index:
23327 fprintf_unfiltered (f, "address: ");
23328 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23329 break;
23330 case DW_FORM_block2:
23331 case DW_FORM_block4:
23332 case DW_FORM_block:
23333 case DW_FORM_block1:
23334 fprintf_unfiltered (f, "block: size %s",
23335 pulongest (DW_BLOCK (&die->attrs[i])->size));
23336 break;
23337 case DW_FORM_exprloc:
23338 fprintf_unfiltered (f, "expression: size %s",
23339 pulongest (DW_BLOCK (&die->attrs[i])->size));
23340 break;
23341 case DW_FORM_data16:
23342 fprintf_unfiltered (f, "constant of 16 bytes");
23343 break;
23344 case DW_FORM_ref_addr:
23345 fprintf_unfiltered (f, "ref address: ");
23346 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23347 break;
23348 case DW_FORM_GNU_ref_alt:
23349 fprintf_unfiltered (f, "alt ref address: ");
23350 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23351 break;
23352 case DW_FORM_ref1:
23353 case DW_FORM_ref2:
23354 case DW_FORM_ref4:
23355 case DW_FORM_ref8:
23356 case DW_FORM_ref_udata:
23357 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23358 (long) (DW_UNSND (&die->attrs[i])));
23359 break;
23360 case DW_FORM_data1:
23361 case DW_FORM_data2:
23362 case DW_FORM_data4:
23363 case DW_FORM_data8:
23364 case DW_FORM_udata:
23365 case DW_FORM_sdata:
23366 fprintf_unfiltered (f, "constant: %s",
23367 pulongest (DW_UNSND (&die->attrs[i])));
23368 break;
23369 case DW_FORM_sec_offset:
23370 fprintf_unfiltered (f, "section offset: %s",
23371 pulongest (DW_UNSND (&die->attrs[i])));
23372 break;
23373 case DW_FORM_ref_sig8:
23374 fprintf_unfiltered (f, "signature: %s",
23375 hex_string (DW_SIGNATURE (&die->attrs[i])));
23376 break;
23377 case DW_FORM_string:
23378 case DW_FORM_strp:
23379 case DW_FORM_line_strp:
23380 case DW_FORM_strx:
23381 case DW_FORM_GNU_str_index:
23382 case DW_FORM_GNU_strp_alt:
23383 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23384 DW_STRING (&die->attrs[i])
23385 ? DW_STRING (&die->attrs[i]) : "",
23386 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23387 break;
23388 case DW_FORM_flag:
23389 if (DW_UNSND (&die->attrs[i]))
23390 fprintf_unfiltered (f, "flag: TRUE");
23391 else
23392 fprintf_unfiltered (f, "flag: FALSE");
23393 break;
23394 case DW_FORM_flag_present:
23395 fprintf_unfiltered (f, "flag: TRUE");
23396 break;
23397 case DW_FORM_indirect:
23398 /* The reader will have reduced the indirect form to
23399 the "base form" so this form should not occur. */
23400 fprintf_unfiltered (f,
23401 "unexpected attribute form: DW_FORM_indirect");
23402 break;
23403 case DW_FORM_implicit_const:
23404 fprintf_unfiltered (f, "constant: %s",
23405 plongest (DW_SND (&die->attrs[i])));
23406 break;
23407 default:
23408 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23409 die->attrs[i].form);
23410 break;
23412 fprintf_unfiltered (f, "\n");
23416 static void
23417 dump_die_for_error (struct die_info *die)
23419 dump_die_shallow (gdb_stderr, 0, die);
23422 static void
23423 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23425 int indent = level * 4;
23427 gdb_assert (die != NULL);
23429 if (level >= max_level)
23430 return;
23432 dump_die_shallow (f, indent, die);
23434 if (die->child != NULL)
23436 print_spaces (indent, f);
23437 fprintf_unfiltered (f, " Children:");
23438 if (level + 1 < max_level)
23440 fprintf_unfiltered (f, "\n");
23441 dump_die_1 (f, level + 1, max_level, die->child);
23443 else
23445 fprintf_unfiltered (f,
23446 " [not printed, max nesting level reached]\n");
23450 if (die->sibling != NULL && level > 0)
23452 dump_die_1 (f, level, max_level, die->sibling);
23456 /* This is called from the pdie macro in gdbinit.in.
23457 It's not static so gcc will keep a copy callable from gdb. */
23459 void
23460 dump_die (struct die_info *die, int max_level)
23462 dump_die_1 (gdb_stdlog, 0, max_level, die);
23465 static void
23466 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23468 void **slot;
23470 slot = htab_find_slot_with_hash (cu->die_hash, die,
23471 to_underlying (die->sect_off),
23472 INSERT);
23474 *slot = die;
23477 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23478 required kind. */
23480 static sect_offset
23481 dwarf2_get_ref_die_offset (const struct attribute *attr)
23483 if (attr_form_is_ref (attr))
23484 return (sect_offset) DW_UNSND (attr);
23486 complaint (_("unsupported die ref attribute form: '%s'"),
23487 dwarf_form_name (attr->form));
23488 return {};
23491 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23492 * the value held by the attribute is not constant. */
23494 static LONGEST
23495 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23497 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23498 return DW_SND (attr);
23499 else if (attr->form == DW_FORM_udata
23500 || attr->form == DW_FORM_data1
23501 || attr->form == DW_FORM_data2
23502 || attr->form == DW_FORM_data4
23503 || attr->form == DW_FORM_data8)
23504 return DW_UNSND (attr);
23505 else
23507 /* For DW_FORM_data16 see attr_form_is_constant. */
23508 complaint (_("Attribute value is not a constant (%s)"),
23509 dwarf_form_name (attr->form));
23510 return default_value;
23514 /* Follow reference or signature attribute ATTR of SRC_DIE.
23515 On entry *REF_CU is the CU of SRC_DIE.
23516 On exit *REF_CU is the CU of the result. */
23518 static struct die_info *
23519 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23520 struct dwarf2_cu **ref_cu)
23522 struct die_info *die;
23524 if (attr_form_is_ref (attr))
23525 die = follow_die_ref (src_die, attr, ref_cu);
23526 else if (attr->form == DW_FORM_ref_sig8)
23527 die = follow_die_sig (src_die, attr, ref_cu);
23528 else
23530 dump_die_for_error (src_die);
23531 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23532 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23535 return die;
23538 /* Follow reference OFFSET.
23539 On entry *REF_CU is the CU of the source die referencing OFFSET.
23540 On exit *REF_CU is the CU of the result.
23541 Returns NULL if OFFSET is invalid. */
23543 static struct die_info *
23544 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23545 struct dwarf2_cu **ref_cu)
23547 struct die_info temp_die;
23548 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23549 struct dwarf2_per_objfile *dwarf2_per_objfile
23550 = cu->per_cu->dwarf2_per_objfile;
23552 gdb_assert (cu->per_cu != NULL);
23554 target_cu = cu;
23556 if (cu->per_cu->is_debug_types)
23558 /* .debug_types CUs cannot reference anything outside their CU.
23559 If they need to, they have to reference a signatured type via
23560 DW_FORM_ref_sig8. */
23561 if (!offset_in_cu_p (&cu->header, sect_off))
23562 return NULL;
23564 else if (offset_in_dwz != cu->per_cu->is_dwz
23565 || !offset_in_cu_p (&cu->header, sect_off))
23567 struct dwarf2_per_cu_data *per_cu;
23569 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23570 dwarf2_per_objfile);
23572 /* If necessary, add it to the queue and load its DIEs. */
23573 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23574 load_full_comp_unit (per_cu, false, cu->language);
23576 target_cu = per_cu->cu;
23578 else if (cu->dies == NULL)
23580 /* We're loading full DIEs during partial symbol reading. */
23581 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23582 load_full_comp_unit (cu->per_cu, false, language_minimal);
23585 *ref_cu = target_cu;
23586 temp_die.sect_off = sect_off;
23588 if (target_cu != cu)
23589 target_cu->ancestor = cu;
23591 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23592 &temp_die,
23593 to_underlying (sect_off));
23596 /* Follow reference attribute ATTR of SRC_DIE.
23597 On entry *REF_CU is the CU of SRC_DIE.
23598 On exit *REF_CU is the CU of the result. */
23600 static struct die_info *
23601 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23602 struct dwarf2_cu **ref_cu)
23604 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23605 struct dwarf2_cu *cu = *ref_cu;
23606 struct die_info *die;
23608 die = follow_die_offset (sect_off,
23609 (attr->form == DW_FORM_GNU_ref_alt
23610 || cu->per_cu->is_dwz),
23611 ref_cu);
23612 if (!die)
23613 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23614 "at %s [in module %s]"),
23615 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23616 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23618 return die;
23621 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23622 Returned value is intended for DW_OP_call*. Returned
23623 dwarf2_locexpr_baton->data has lifetime of
23624 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23626 struct dwarf2_locexpr_baton
23627 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23628 struct dwarf2_per_cu_data *per_cu,
23629 CORE_ADDR (*get_frame_pc) (void *baton),
23630 void *baton, bool resolve_abstract_p)
23632 struct dwarf2_cu *cu;
23633 struct die_info *die;
23634 struct attribute *attr;
23635 struct dwarf2_locexpr_baton retval;
23636 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23637 struct objfile *objfile = dwarf2_per_objfile->objfile;
23639 if (per_cu->cu == NULL)
23640 load_cu (per_cu, false);
23641 cu = per_cu->cu;
23642 if (cu == NULL)
23644 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23645 Instead just throw an error, not much else we can do. */
23646 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23647 sect_offset_str (sect_off), objfile_name (objfile));
23650 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23651 if (!die)
23652 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23653 sect_offset_str (sect_off), objfile_name (objfile));
23655 attr = dwarf2_attr (die, DW_AT_location, cu);
23656 if (!attr && resolve_abstract_p
23657 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23658 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23660 CORE_ADDR pc = (*get_frame_pc) (baton);
23661 CORE_ADDR baseaddr
23662 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23665 for (const auto &cand_off
23666 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23668 struct dwarf2_cu *cand_cu = cu;
23669 struct die_info *cand
23670 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23671 if (!cand
23672 || !cand->parent
23673 || cand->parent->tag != DW_TAG_subprogram)
23674 continue;
23676 CORE_ADDR pc_low, pc_high;
23677 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23678 if (pc_low == ((CORE_ADDR) -1))
23679 continue;
23680 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23681 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23682 if (!(pc_low <= pc && pc < pc_high))
23683 continue;
23685 die = cand;
23686 attr = dwarf2_attr (die, DW_AT_location, cu);
23687 break;
23691 if (!attr)
23693 /* DWARF: "If there is no such attribute, then there is no effect.".
23694 DATA is ignored if SIZE is 0. */
23696 retval.data = NULL;
23697 retval.size = 0;
23699 else if (attr_form_is_section_offset (attr))
23701 struct dwarf2_loclist_baton loclist_baton;
23702 CORE_ADDR pc = (*get_frame_pc) (baton);
23703 size_t size;
23705 fill_in_loclist_baton (cu, &loclist_baton, attr);
23707 retval.data = dwarf2_find_location_expression (&loclist_baton,
23708 &size, pc);
23709 retval.size = size;
23711 else
23713 if (!attr_form_is_block (attr))
23714 error (_("Dwarf Error: DIE at %s referenced in module %s "
23715 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23716 sect_offset_str (sect_off), objfile_name (objfile));
23718 retval.data = DW_BLOCK (attr)->data;
23719 retval.size = DW_BLOCK (attr)->size;
23721 retval.per_cu = cu->per_cu;
23723 age_cached_comp_units (dwarf2_per_objfile);
23725 return retval;
23728 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23729 offset. */
23731 struct dwarf2_locexpr_baton
23732 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23733 struct dwarf2_per_cu_data *per_cu,
23734 CORE_ADDR (*get_frame_pc) (void *baton),
23735 void *baton)
23737 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23739 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23742 /* Write a constant of a given type as target-ordered bytes into
23743 OBSTACK. */
23745 static const gdb_byte *
23746 write_constant_as_bytes (struct obstack *obstack,
23747 enum bfd_endian byte_order,
23748 struct type *type,
23749 ULONGEST value,
23750 LONGEST *len)
23752 gdb_byte *result;
23754 *len = TYPE_LENGTH (type);
23755 result = (gdb_byte *) obstack_alloc (obstack, *len);
23756 store_unsigned_integer (result, *len, byte_order, value);
23758 return result;
23761 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23762 pointer to the constant bytes and set LEN to the length of the
23763 data. If memory is needed, allocate it on OBSTACK. If the DIE
23764 does not have a DW_AT_const_value, return NULL. */
23766 const gdb_byte *
23767 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23768 struct dwarf2_per_cu_data *per_cu,
23769 struct obstack *obstack,
23770 LONGEST *len)
23772 struct dwarf2_cu *cu;
23773 struct die_info *die;
23774 struct attribute *attr;
23775 const gdb_byte *result = NULL;
23776 struct type *type;
23777 LONGEST value;
23778 enum bfd_endian byte_order;
23779 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23781 if (per_cu->cu == NULL)
23782 load_cu (per_cu, false);
23783 cu = per_cu->cu;
23784 if (cu == NULL)
23786 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23787 Instead just throw an error, not much else we can do. */
23788 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23789 sect_offset_str (sect_off), objfile_name (objfile));
23792 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23793 if (!die)
23794 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23795 sect_offset_str (sect_off), objfile_name (objfile));
23797 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23798 if (attr == NULL)
23799 return NULL;
23801 byte_order = (bfd_big_endian (objfile->obfd)
23802 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23804 switch (attr->form)
23806 case DW_FORM_addr:
23807 case DW_FORM_addrx:
23808 case DW_FORM_GNU_addr_index:
23810 gdb_byte *tem;
23812 *len = cu->header.addr_size;
23813 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23814 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23815 result = tem;
23817 break;
23818 case DW_FORM_string:
23819 case DW_FORM_strp:
23820 case DW_FORM_strx:
23821 case DW_FORM_GNU_str_index:
23822 case DW_FORM_GNU_strp_alt:
23823 /* DW_STRING is already allocated on the objfile obstack, point
23824 directly to it. */
23825 result = (const gdb_byte *) DW_STRING (attr);
23826 *len = strlen (DW_STRING (attr));
23827 break;
23828 case DW_FORM_block1:
23829 case DW_FORM_block2:
23830 case DW_FORM_block4:
23831 case DW_FORM_block:
23832 case DW_FORM_exprloc:
23833 case DW_FORM_data16:
23834 result = DW_BLOCK (attr)->data;
23835 *len = DW_BLOCK (attr)->size;
23836 break;
23838 /* The DW_AT_const_value attributes are supposed to carry the
23839 symbol's value "represented as it would be on the target
23840 architecture." By the time we get here, it's already been
23841 converted to host endianness, so we just need to sign- or
23842 zero-extend it as appropriate. */
23843 case DW_FORM_data1:
23844 type = die_type (die, cu);
23845 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23846 if (result == NULL)
23847 result = write_constant_as_bytes (obstack, byte_order,
23848 type, value, len);
23849 break;
23850 case DW_FORM_data2:
23851 type = die_type (die, cu);
23852 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23853 if (result == NULL)
23854 result = write_constant_as_bytes (obstack, byte_order,
23855 type, value, len);
23856 break;
23857 case DW_FORM_data4:
23858 type = die_type (die, cu);
23859 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23860 if (result == NULL)
23861 result = write_constant_as_bytes (obstack, byte_order,
23862 type, value, len);
23863 break;
23864 case DW_FORM_data8:
23865 type = die_type (die, cu);
23866 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23867 if (result == NULL)
23868 result = write_constant_as_bytes (obstack, byte_order,
23869 type, value, len);
23870 break;
23872 case DW_FORM_sdata:
23873 case DW_FORM_implicit_const:
23874 type = die_type (die, cu);
23875 result = write_constant_as_bytes (obstack, byte_order,
23876 type, DW_SND (attr), len);
23877 break;
23879 case DW_FORM_udata:
23880 type = die_type (die, cu);
23881 result = write_constant_as_bytes (obstack, byte_order,
23882 type, DW_UNSND (attr), len);
23883 break;
23885 default:
23886 complaint (_("unsupported const value attribute form: '%s'"),
23887 dwarf_form_name (attr->form));
23888 break;
23891 return result;
23894 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23895 valid type for this die is found. */
23897 struct type *
23898 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23899 struct dwarf2_per_cu_data *per_cu)
23901 struct dwarf2_cu *cu;
23902 struct die_info *die;
23904 if (per_cu->cu == NULL)
23905 load_cu (per_cu, false);
23906 cu = per_cu->cu;
23907 if (!cu)
23908 return NULL;
23910 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23911 if (!die)
23912 return NULL;
23914 return die_type (die, cu);
23917 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23918 PER_CU. */
23920 struct type *
23921 dwarf2_get_die_type (cu_offset die_offset,
23922 struct dwarf2_per_cu_data *per_cu)
23924 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23925 return get_die_type_at_offset (die_offset_sect, per_cu);
23928 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23929 On entry *REF_CU is the CU of SRC_DIE.
23930 On exit *REF_CU is the CU of the result.
23931 Returns NULL if the referenced DIE isn't found. */
23933 static struct die_info *
23934 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23935 struct dwarf2_cu **ref_cu)
23937 struct die_info temp_die;
23938 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23939 struct die_info *die;
23941 /* While it might be nice to assert sig_type->type == NULL here,
23942 we can get here for DW_AT_imported_declaration where we need
23943 the DIE not the type. */
23945 /* If necessary, add it to the queue and load its DIEs. */
23947 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23948 read_signatured_type (sig_type);
23950 sig_cu = sig_type->per_cu.cu;
23951 gdb_assert (sig_cu != NULL);
23952 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23953 temp_die.sect_off = sig_type->type_offset_in_section;
23954 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23955 to_underlying (temp_die.sect_off));
23956 if (die)
23958 struct dwarf2_per_objfile *dwarf2_per_objfile
23959 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23961 /* For .gdb_index version 7 keep track of included TUs.
23962 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23963 if (dwarf2_per_objfile->index_table != NULL
23964 && dwarf2_per_objfile->index_table->version <= 7)
23966 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23969 *ref_cu = sig_cu;
23970 if (sig_cu != cu)
23971 sig_cu->ancestor = cu;
23973 return die;
23976 return NULL;
23979 /* Follow signatured type referenced by ATTR in SRC_DIE.
23980 On entry *REF_CU is the CU of SRC_DIE.
23981 On exit *REF_CU is the CU of the result.
23982 The result is the DIE of the type.
23983 If the referenced type cannot be found an error is thrown. */
23985 static struct die_info *
23986 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23987 struct dwarf2_cu **ref_cu)
23989 ULONGEST signature = DW_SIGNATURE (attr);
23990 struct signatured_type *sig_type;
23991 struct die_info *die;
23993 gdb_assert (attr->form == DW_FORM_ref_sig8);
23995 sig_type = lookup_signatured_type (*ref_cu, signature);
23996 /* sig_type will be NULL if the signatured type is missing from
23997 the debug info. */
23998 if (sig_type == NULL)
24000 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24001 " from DIE at %s [in module %s]"),
24002 hex_string (signature), sect_offset_str (src_die->sect_off),
24003 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24006 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24007 if (die == NULL)
24009 dump_die_for_error (src_die);
24010 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24011 " from DIE at %s [in module %s]"),
24012 hex_string (signature), sect_offset_str (src_die->sect_off),
24013 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24016 return die;
24019 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24020 reading in and processing the type unit if necessary. */
24022 static struct type *
24023 get_signatured_type (struct die_info *die, ULONGEST signature,
24024 struct dwarf2_cu *cu)
24026 struct dwarf2_per_objfile *dwarf2_per_objfile
24027 = cu->per_cu->dwarf2_per_objfile;
24028 struct signatured_type *sig_type;
24029 struct dwarf2_cu *type_cu;
24030 struct die_info *type_die;
24031 struct type *type;
24033 sig_type = lookup_signatured_type (cu, signature);
24034 /* sig_type will be NULL if the signatured type is missing from
24035 the debug info. */
24036 if (sig_type == NULL)
24038 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24039 " from DIE at %s [in module %s]"),
24040 hex_string (signature), sect_offset_str (die->sect_off),
24041 objfile_name (dwarf2_per_objfile->objfile));
24042 return build_error_marker_type (cu, die);
24045 /* If we already know the type we're done. */
24046 if (sig_type->type != NULL)
24047 return sig_type->type;
24049 type_cu = cu;
24050 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24051 if (type_die != NULL)
24053 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24054 is created. This is important, for example, because for c++ classes
24055 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24056 type = read_type_die (type_die, type_cu);
24057 if (type == NULL)
24059 complaint (_("Dwarf Error: Cannot build signatured type %s"
24060 " referenced from DIE at %s [in module %s]"),
24061 hex_string (signature), sect_offset_str (die->sect_off),
24062 objfile_name (dwarf2_per_objfile->objfile));
24063 type = build_error_marker_type (cu, die);
24066 else
24068 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24069 " from DIE at %s [in module %s]"),
24070 hex_string (signature), sect_offset_str (die->sect_off),
24071 objfile_name (dwarf2_per_objfile->objfile));
24072 type = build_error_marker_type (cu, die);
24074 sig_type->type = type;
24076 return type;
24079 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24080 reading in and processing the type unit if necessary. */
24082 static struct type *
24083 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24084 struct dwarf2_cu *cu) /* ARI: editCase function */
24086 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24087 if (attr_form_is_ref (attr))
24089 struct dwarf2_cu *type_cu = cu;
24090 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24092 return read_type_die (type_die, type_cu);
24094 else if (attr->form == DW_FORM_ref_sig8)
24096 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24098 else
24100 struct dwarf2_per_objfile *dwarf2_per_objfile
24101 = cu->per_cu->dwarf2_per_objfile;
24103 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24104 " at %s [in module %s]"),
24105 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24106 objfile_name (dwarf2_per_objfile->objfile));
24107 return build_error_marker_type (cu, die);
24111 /* Load the DIEs associated with type unit PER_CU into memory. */
24113 static void
24114 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24116 struct signatured_type *sig_type;
24118 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24119 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24121 /* We have the per_cu, but we need the signatured_type.
24122 Fortunately this is an easy translation. */
24123 gdb_assert (per_cu->is_debug_types);
24124 sig_type = (struct signatured_type *) per_cu;
24126 gdb_assert (per_cu->cu == NULL);
24128 read_signatured_type (sig_type);
24130 gdb_assert (per_cu->cu != NULL);
24133 /* die_reader_func for read_signatured_type.
24134 This is identical to load_full_comp_unit_reader,
24135 but is kept separate for now. */
24137 static void
24138 read_signatured_type_reader (const struct die_reader_specs *reader,
24139 const gdb_byte *info_ptr,
24140 struct die_info *comp_unit_die,
24141 int has_children,
24142 void *data)
24144 struct dwarf2_cu *cu = reader->cu;
24146 gdb_assert (cu->die_hash == NULL);
24147 cu->die_hash =
24148 htab_create_alloc_ex (cu->header.length / 12,
24149 die_hash,
24150 die_eq,
24151 NULL,
24152 &cu->comp_unit_obstack,
24153 hashtab_obstack_allocate,
24154 dummy_obstack_deallocate);
24156 if (has_children)
24157 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24158 &info_ptr, comp_unit_die);
24159 cu->dies = comp_unit_die;
24160 /* comp_unit_die is not stored in die_hash, no need. */
24162 /* We try not to read any attributes in this function, because not
24163 all CUs needed for references have been loaded yet, and symbol
24164 table processing isn't initialized. But we have to set the CU language,
24165 or we won't be able to build types correctly.
24166 Similarly, if we do not read the producer, we can not apply
24167 producer-specific interpretation. */
24168 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24171 /* Read in a signatured type and build its CU and DIEs.
24172 If the type is a stub for the real type in a DWO file,
24173 read in the real type from the DWO file as well. */
24175 static void
24176 read_signatured_type (struct signatured_type *sig_type)
24178 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24180 gdb_assert (per_cu->is_debug_types);
24181 gdb_assert (per_cu->cu == NULL);
24183 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24184 read_signatured_type_reader, NULL);
24185 sig_type->per_cu.tu_read = 1;
24188 /* Decode simple location descriptions.
24189 Given a pointer to a dwarf block that defines a location, compute
24190 the location and return the value.
24192 NOTE drow/2003-11-18: This function is called in two situations
24193 now: for the address of static or global variables (partial symbols
24194 only) and for offsets into structures which are expected to be
24195 (more or less) constant. The partial symbol case should go away,
24196 and only the constant case should remain. That will let this
24197 function complain more accurately. A few special modes are allowed
24198 without complaint for global variables (for instance, global
24199 register values and thread-local values).
24201 A location description containing no operations indicates that the
24202 object is optimized out. The return value is 0 for that case.
24203 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24204 callers will only want a very basic result and this can become a
24205 complaint.
24207 Note that stack[0] is unused except as a default error return. */
24209 static CORE_ADDR
24210 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24212 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24213 size_t i;
24214 size_t size = blk->size;
24215 const gdb_byte *data = blk->data;
24216 CORE_ADDR stack[64];
24217 int stacki;
24218 unsigned int bytes_read, unsnd;
24219 gdb_byte op;
24221 i = 0;
24222 stacki = 0;
24223 stack[stacki] = 0;
24224 stack[++stacki] = 0;
24226 while (i < size)
24228 op = data[i++];
24229 switch (op)
24231 case DW_OP_lit0:
24232 case DW_OP_lit1:
24233 case DW_OP_lit2:
24234 case DW_OP_lit3:
24235 case DW_OP_lit4:
24236 case DW_OP_lit5:
24237 case DW_OP_lit6:
24238 case DW_OP_lit7:
24239 case DW_OP_lit8:
24240 case DW_OP_lit9:
24241 case DW_OP_lit10:
24242 case DW_OP_lit11:
24243 case DW_OP_lit12:
24244 case DW_OP_lit13:
24245 case DW_OP_lit14:
24246 case DW_OP_lit15:
24247 case DW_OP_lit16:
24248 case DW_OP_lit17:
24249 case DW_OP_lit18:
24250 case DW_OP_lit19:
24251 case DW_OP_lit20:
24252 case DW_OP_lit21:
24253 case DW_OP_lit22:
24254 case DW_OP_lit23:
24255 case DW_OP_lit24:
24256 case DW_OP_lit25:
24257 case DW_OP_lit26:
24258 case DW_OP_lit27:
24259 case DW_OP_lit28:
24260 case DW_OP_lit29:
24261 case DW_OP_lit30:
24262 case DW_OP_lit31:
24263 stack[++stacki] = op - DW_OP_lit0;
24264 break;
24266 case DW_OP_reg0:
24267 case DW_OP_reg1:
24268 case DW_OP_reg2:
24269 case DW_OP_reg3:
24270 case DW_OP_reg4:
24271 case DW_OP_reg5:
24272 case DW_OP_reg6:
24273 case DW_OP_reg7:
24274 case DW_OP_reg8:
24275 case DW_OP_reg9:
24276 case DW_OP_reg10:
24277 case DW_OP_reg11:
24278 case DW_OP_reg12:
24279 case DW_OP_reg13:
24280 case DW_OP_reg14:
24281 case DW_OP_reg15:
24282 case DW_OP_reg16:
24283 case DW_OP_reg17:
24284 case DW_OP_reg18:
24285 case DW_OP_reg19:
24286 case DW_OP_reg20:
24287 case DW_OP_reg21:
24288 case DW_OP_reg22:
24289 case DW_OP_reg23:
24290 case DW_OP_reg24:
24291 case DW_OP_reg25:
24292 case DW_OP_reg26:
24293 case DW_OP_reg27:
24294 case DW_OP_reg28:
24295 case DW_OP_reg29:
24296 case DW_OP_reg30:
24297 case DW_OP_reg31:
24298 stack[++stacki] = op - DW_OP_reg0;
24299 if (i < size)
24300 dwarf2_complex_location_expr_complaint ();
24301 break;
24303 case DW_OP_regx:
24304 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24305 i += bytes_read;
24306 stack[++stacki] = unsnd;
24307 if (i < size)
24308 dwarf2_complex_location_expr_complaint ();
24309 break;
24311 case DW_OP_addr:
24312 stack[++stacki] = read_address (objfile->obfd, &data[i],
24313 cu, &bytes_read);
24314 i += bytes_read;
24315 break;
24317 case DW_OP_const1u:
24318 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24319 i += 1;
24320 break;
24322 case DW_OP_const1s:
24323 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24324 i += 1;
24325 break;
24327 case DW_OP_const2u:
24328 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24329 i += 2;
24330 break;
24332 case DW_OP_const2s:
24333 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24334 i += 2;
24335 break;
24337 case DW_OP_const4u:
24338 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24339 i += 4;
24340 break;
24342 case DW_OP_const4s:
24343 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24344 i += 4;
24345 break;
24347 case DW_OP_const8u:
24348 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24349 i += 8;
24350 break;
24352 case DW_OP_constu:
24353 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24354 &bytes_read);
24355 i += bytes_read;
24356 break;
24358 case DW_OP_consts:
24359 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24360 i += bytes_read;
24361 break;
24363 case DW_OP_dup:
24364 stack[stacki + 1] = stack[stacki];
24365 stacki++;
24366 break;
24368 case DW_OP_plus:
24369 stack[stacki - 1] += stack[stacki];
24370 stacki--;
24371 break;
24373 case DW_OP_plus_uconst:
24374 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24375 &bytes_read);
24376 i += bytes_read;
24377 break;
24379 case DW_OP_minus:
24380 stack[stacki - 1] -= stack[stacki];
24381 stacki--;
24382 break;
24384 case DW_OP_deref:
24385 /* If we're not the last op, then we definitely can't encode
24386 this using GDB's address_class enum. This is valid for partial
24387 global symbols, although the variable's address will be bogus
24388 in the psymtab. */
24389 if (i < size)
24390 dwarf2_complex_location_expr_complaint ();
24391 break;
24393 case DW_OP_GNU_push_tls_address:
24394 case DW_OP_form_tls_address:
24395 /* The top of the stack has the offset from the beginning
24396 of the thread control block at which the variable is located. */
24397 /* Nothing should follow this operator, so the top of stack would
24398 be returned. */
24399 /* This is valid for partial global symbols, but the variable's
24400 address will be bogus in the psymtab. Make it always at least
24401 non-zero to not look as a variable garbage collected by linker
24402 which have DW_OP_addr 0. */
24403 if (i < size)
24404 dwarf2_complex_location_expr_complaint ();
24405 stack[stacki]++;
24406 break;
24408 case DW_OP_GNU_uninit:
24409 break;
24411 case DW_OP_addrx:
24412 case DW_OP_GNU_addr_index:
24413 case DW_OP_GNU_const_index:
24414 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24415 &bytes_read);
24416 i += bytes_read;
24417 break;
24419 default:
24421 const char *name = get_DW_OP_name (op);
24423 if (name)
24424 complaint (_("unsupported stack op: '%s'"),
24425 name);
24426 else
24427 complaint (_("unsupported stack op: '%02x'"),
24428 op);
24431 return (stack[stacki]);
24434 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24435 outside of the allocated space. Also enforce minimum>0. */
24436 if (stacki >= ARRAY_SIZE (stack) - 1)
24438 complaint (_("location description stack overflow"));
24439 return 0;
24442 if (stacki <= 0)
24444 complaint (_("location description stack underflow"));
24445 return 0;
24448 return (stack[stacki]);
24451 /* memory allocation interface */
24453 static struct dwarf_block *
24454 dwarf_alloc_block (struct dwarf2_cu *cu)
24456 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24459 static struct die_info *
24460 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24462 struct die_info *die;
24463 size_t size = sizeof (struct die_info);
24465 if (num_attrs > 1)
24466 size += (num_attrs - 1) * sizeof (struct attribute);
24468 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24469 memset (die, 0, sizeof (struct die_info));
24470 return (die);
24474 /* Macro support. */
24476 /* Return file name relative to the compilation directory of file number I in
24477 *LH's file name table. The result is allocated using xmalloc; the caller is
24478 responsible for freeing it. */
24480 static char *
24481 file_file_name (int file, struct line_header *lh)
24483 /* Is the file number a valid index into the line header's file name
24484 table? Remember that file numbers start with one, not zero. */
24485 if (lh->is_valid_file_index (file))
24487 const file_entry *fe = lh->file_name_at (file);
24489 if (!IS_ABSOLUTE_PATH (fe->name))
24491 const char *dir = fe->include_dir (lh);
24492 if (dir != NULL)
24493 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24495 return xstrdup (fe->name);
24497 else
24499 /* The compiler produced a bogus file number. We can at least
24500 record the macro definitions made in the file, even if we
24501 won't be able to find the file by name. */
24502 char fake_name[80];
24504 xsnprintf (fake_name, sizeof (fake_name),
24505 "<bad macro file number %d>", file);
24507 complaint (_("bad file number in macro information (%d)"),
24508 file);
24510 return xstrdup (fake_name);
24514 /* Return the full name of file number I in *LH's file name table.
24515 Use COMP_DIR as the name of the current directory of the
24516 compilation. The result is allocated using xmalloc; the caller is
24517 responsible for freeing it. */
24518 static char *
24519 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24521 /* Is the file number a valid index into the line header's file name
24522 table? Remember that file numbers start with one, not zero. */
24523 if (lh->is_valid_file_index (file))
24525 char *relative = file_file_name (file, lh);
24527 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24528 return relative;
24529 return reconcat (relative, comp_dir, SLASH_STRING,
24530 relative, (char *) NULL);
24532 else
24533 return file_file_name (file, lh);
24537 static struct macro_source_file *
24538 macro_start_file (struct dwarf2_cu *cu,
24539 int file, int line,
24540 struct macro_source_file *current_file,
24541 struct line_header *lh)
24543 /* File name relative to the compilation directory of this source file. */
24544 char *file_name = file_file_name (file, lh);
24546 if (! current_file)
24548 /* Note: We don't create a macro table for this compilation unit
24549 at all until we actually get a filename. */
24550 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24552 /* If we have no current file, then this must be the start_file
24553 directive for the compilation unit's main source file. */
24554 current_file = macro_set_main (macro_table, file_name);
24555 macro_define_special (macro_table);
24557 else
24558 current_file = macro_include (current_file, line, file_name);
24560 xfree (file_name);
24562 return current_file;
24565 static const char *
24566 consume_improper_spaces (const char *p, const char *body)
24568 if (*p == ' ')
24570 complaint (_("macro definition contains spaces "
24571 "in formal argument list:\n`%s'"),
24572 body);
24574 while (*p == ' ')
24575 p++;
24578 return p;
24582 static void
24583 parse_macro_definition (struct macro_source_file *file, int line,
24584 const char *body)
24586 const char *p;
24588 /* The body string takes one of two forms. For object-like macro
24589 definitions, it should be:
24591 <macro name> " " <definition>
24593 For function-like macro definitions, it should be:
24595 <macro name> "() " <definition>
24597 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24599 Spaces may appear only where explicitly indicated, and in the
24600 <definition>.
24602 The Dwarf 2 spec says that an object-like macro's name is always
24603 followed by a space, but versions of GCC around March 2002 omit
24604 the space when the macro's definition is the empty string.
24606 The Dwarf 2 spec says that there should be no spaces between the
24607 formal arguments in a function-like macro's formal argument list,
24608 but versions of GCC around March 2002 include spaces after the
24609 commas. */
24612 /* Find the extent of the macro name. The macro name is terminated
24613 by either a space or null character (for an object-like macro) or
24614 an opening paren (for a function-like macro). */
24615 for (p = body; *p; p++)
24616 if (*p == ' ' || *p == '(')
24617 break;
24619 if (*p == ' ' || *p == '\0')
24621 /* It's an object-like macro. */
24622 int name_len = p - body;
24623 char *name = savestring (body, name_len);
24624 const char *replacement;
24626 if (*p == ' ')
24627 replacement = body + name_len + 1;
24628 else
24630 dwarf2_macro_malformed_definition_complaint (body);
24631 replacement = body + name_len;
24634 macro_define_object (file, line, name, replacement);
24636 xfree (name);
24638 else if (*p == '(')
24640 /* It's a function-like macro. */
24641 char *name = savestring (body, p - body);
24642 int argc = 0;
24643 int argv_size = 1;
24644 char **argv = XNEWVEC (char *, argv_size);
24646 p++;
24648 p = consume_improper_spaces (p, body);
24650 /* Parse the formal argument list. */
24651 while (*p && *p != ')')
24653 /* Find the extent of the current argument name. */
24654 const char *arg_start = p;
24656 while (*p && *p != ',' && *p != ')' && *p != ' ')
24657 p++;
24659 if (! *p || p == arg_start)
24660 dwarf2_macro_malformed_definition_complaint (body);
24661 else
24663 /* Make sure argv has room for the new argument. */
24664 if (argc >= argv_size)
24666 argv_size *= 2;
24667 argv = XRESIZEVEC (char *, argv, argv_size);
24670 argv[argc++] = savestring (arg_start, p - arg_start);
24673 p = consume_improper_spaces (p, body);
24675 /* Consume the comma, if present. */
24676 if (*p == ',')
24678 p++;
24680 p = consume_improper_spaces (p, body);
24684 if (*p == ')')
24686 p++;
24688 if (*p == ' ')
24689 /* Perfectly formed definition, no complaints. */
24690 macro_define_function (file, line, name,
24691 argc, (const char **) argv,
24692 p + 1);
24693 else if (*p == '\0')
24695 /* Complain, but do define it. */
24696 dwarf2_macro_malformed_definition_complaint (body);
24697 macro_define_function (file, line, name,
24698 argc, (const char **) argv,
24701 else
24702 /* Just complain. */
24703 dwarf2_macro_malformed_definition_complaint (body);
24705 else
24706 /* Just complain. */
24707 dwarf2_macro_malformed_definition_complaint (body);
24709 xfree (name);
24711 int i;
24713 for (i = 0; i < argc; i++)
24714 xfree (argv[i]);
24716 xfree (argv);
24718 else
24719 dwarf2_macro_malformed_definition_complaint (body);
24722 /* Skip some bytes from BYTES according to the form given in FORM.
24723 Returns the new pointer. */
24725 static const gdb_byte *
24726 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24727 enum dwarf_form form,
24728 unsigned int offset_size,
24729 struct dwarf2_section_info *section)
24731 unsigned int bytes_read;
24733 switch (form)
24735 case DW_FORM_data1:
24736 case DW_FORM_flag:
24737 ++bytes;
24738 break;
24740 case DW_FORM_data2:
24741 bytes += 2;
24742 break;
24744 case DW_FORM_data4:
24745 bytes += 4;
24746 break;
24748 case DW_FORM_data8:
24749 bytes += 8;
24750 break;
24752 case DW_FORM_data16:
24753 bytes += 16;
24754 break;
24756 case DW_FORM_string:
24757 read_direct_string (abfd, bytes, &bytes_read);
24758 bytes += bytes_read;
24759 break;
24761 case DW_FORM_sec_offset:
24762 case DW_FORM_strp:
24763 case DW_FORM_GNU_strp_alt:
24764 bytes += offset_size;
24765 break;
24767 case DW_FORM_block:
24768 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24769 bytes += bytes_read;
24770 break;
24772 case DW_FORM_block1:
24773 bytes += 1 + read_1_byte (abfd, bytes);
24774 break;
24775 case DW_FORM_block2:
24776 bytes += 2 + read_2_bytes (abfd, bytes);
24777 break;
24778 case DW_FORM_block4:
24779 bytes += 4 + read_4_bytes (abfd, bytes);
24780 break;
24782 case DW_FORM_addrx:
24783 case DW_FORM_sdata:
24784 case DW_FORM_strx:
24785 case DW_FORM_udata:
24786 case DW_FORM_GNU_addr_index:
24787 case DW_FORM_GNU_str_index:
24788 bytes = gdb_skip_leb128 (bytes, buffer_end);
24789 if (bytes == NULL)
24791 dwarf2_section_buffer_overflow_complaint (section);
24792 return NULL;
24794 break;
24796 case DW_FORM_implicit_const:
24797 break;
24799 default:
24801 complaint (_("invalid form 0x%x in `%s'"),
24802 form, get_section_name (section));
24803 return NULL;
24807 return bytes;
24810 /* A helper for dwarf_decode_macros that handles skipping an unknown
24811 opcode. Returns an updated pointer to the macro data buffer; or,
24812 on error, issues a complaint and returns NULL. */
24814 static const gdb_byte *
24815 skip_unknown_opcode (unsigned int opcode,
24816 const gdb_byte **opcode_definitions,
24817 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24818 bfd *abfd,
24819 unsigned int offset_size,
24820 struct dwarf2_section_info *section)
24822 unsigned int bytes_read, i;
24823 unsigned long arg;
24824 const gdb_byte *defn;
24826 if (opcode_definitions[opcode] == NULL)
24828 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24829 opcode);
24830 return NULL;
24833 defn = opcode_definitions[opcode];
24834 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24835 defn += bytes_read;
24837 for (i = 0; i < arg; ++i)
24839 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24840 (enum dwarf_form) defn[i], offset_size,
24841 section);
24842 if (mac_ptr == NULL)
24844 /* skip_form_bytes already issued the complaint. */
24845 return NULL;
24849 return mac_ptr;
24852 /* A helper function which parses the header of a macro section.
24853 If the macro section is the extended (for now called "GNU") type,
24854 then this updates *OFFSET_SIZE. Returns a pointer to just after
24855 the header, or issues a complaint and returns NULL on error. */
24857 static const gdb_byte *
24858 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24859 bfd *abfd,
24860 const gdb_byte *mac_ptr,
24861 unsigned int *offset_size,
24862 int section_is_gnu)
24864 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24866 if (section_is_gnu)
24868 unsigned int version, flags;
24870 version = read_2_bytes (abfd, mac_ptr);
24871 if (version != 4 && version != 5)
24873 complaint (_("unrecognized version `%d' in .debug_macro section"),
24874 version);
24875 return NULL;
24877 mac_ptr += 2;
24879 flags = read_1_byte (abfd, mac_ptr);
24880 ++mac_ptr;
24881 *offset_size = (flags & 1) ? 8 : 4;
24883 if ((flags & 2) != 0)
24884 /* We don't need the line table offset. */
24885 mac_ptr += *offset_size;
24887 /* Vendor opcode descriptions. */
24888 if ((flags & 4) != 0)
24890 unsigned int i, count;
24892 count = read_1_byte (abfd, mac_ptr);
24893 ++mac_ptr;
24894 for (i = 0; i < count; ++i)
24896 unsigned int opcode, bytes_read;
24897 unsigned long arg;
24899 opcode = read_1_byte (abfd, mac_ptr);
24900 ++mac_ptr;
24901 opcode_definitions[opcode] = mac_ptr;
24902 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24903 mac_ptr += bytes_read;
24904 mac_ptr += arg;
24909 return mac_ptr;
24912 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24913 including DW_MACRO_import. */
24915 static void
24916 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24917 bfd *abfd,
24918 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24919 struct macro_source_file *current_file,
24920 struct line_header *lh,
24921 struct dwarf2_section_info *section,
24922 int section_is_gnu, int section_is_dwz,
24923 unsigned int offset_size,
24924 htab_t include_hash)
24926 struct dwarf2_per_objfile *dwarf2_per_objfile
24927 = cu->per_cu->dwarf2_per_objfile;
24928 struct objfile *objfile = dwarf2_per_objfile->objfile;
24929 enum dwarf_macro_record_type macinfo_type;
24930 int at_commandline;
24931 const gdb_byte *opcode_definitions[256];
24933 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24934 &offset_size, section_is_gnu);
24935 if (mac_ptr == NULL)
24937 /* We already issued a complaint. */
24938 return;
24941 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24942 GDB is still reading the definitions from command line. First
24943 DW_MACINFO_start_file will need to be ignored as it was already executed
24944 to create CURRENT_FILE for the main source holding also the command line
24945 definitions. On first met DW_MACINFO_start_file this flag is reset to
24946 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24948 at_commandline = 1;
24952 /* Do we at least have room for a macinfo type byte? */
24953 if (mac_ptr >= mac_end)
24955 dwarf2_section_buffer_overflow_complaint (section);
24956 break;
24959 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24960 mac_ptr++;
24962 /* Note that we rely on the fact that the corresponding GNU and
24963 DWARF constants are the same. */
24964 DIAGNOSTIC_PUSH
24965 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24966 switch (macinfo_type)
24968 /* A zero macinfo type indicates the end of the macro
24969 information. */
24970 case 0:
24971 break;
24973 case DW_MACRO_define:
24974 case DW_MACRO_undef:
24975 case DW_MACRO_define_strp:
24976 case DW_MACRO_undef_strp:
24977 case DW_MACRO_define_sup:
24978 case DW_MACRO_undef_sup:
24980 unsigned int bytes_read;
24981 int line;
24982 const char *body;
24983 int is_define;
24985 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24986 mac_ptr += bytes_read;
24988 if (macinfo_type == DW_MACRO_define
24989 || macinfo_type == DW_MACRO_undef)
24991 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24992 mac_ptr += bytes_read;
24994 else
24996 LONGEST str_offset;
24998 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24999 mac_ptr += offset_size;
25001 if (macinfo_type == DW_MACRO_define_sup
25002 || macinfo_type == DW_MACRO_undef_sup
25003 || section_is_dwz)
25005 struct dwz_file *dwz
25006 = dwarf2_get_dwz_file (dwarf2_per_objfile);
25008 body = read_indirect_string_from_dwz (objfile,
25009 dwz, str_offset);
25011 else
25012 body = read_indirect_string_at_offset (dwarf2_per_objfile,
25013 abfd, str_offset);
25016 is_define = (macinfo_type == DW_MACRO_define
25017 || macinfo_type == DW_MACRO_define_strp
25018 || macinfo_type == DW_MACRO_define_sup);
25019 if (! current_file)
25021 /* DWARF violation as no main source is present. */
25022 complaint (_("debug info with no main source gives macro %s "
25023 "on line %d: %s"),
25024 is_define ? _("definition") : _("undefinition"),
25025 line, body);
25026 break;
25028 if ((line == 0 && !at_commandline)
25029 || (line != 0 && at_commandline))
25030 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
25031 at_commandline ? _("command-line") : _("in-file"),
25032 is_define ? _("definition") : _("undefinition"),
25033 line == 0 ? _("zero") : _("non-zero"), line, body);
25035 if (body == NULL)
25037 /* Fedora's rpm-build's "debugedit" binary
25038 corrupted .debug_macro sections.
25040 For more info, see
25041 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25042 complaint (_("debug info gives %s invalid macro %s "
25043 "without body (corrupted?) at line %d "
25044 "on file %s"),
25045 at_commandline ? _("command-line") : _("in-file"),
25046 is_define ? _("definition") : _("undefinition"),
25047 line, current_file->filename);
25049 else if (is_define)
25050 parse_macro_definition (current_file, line, body);
25051 else
25053 gdb_assert (macinfo_type == DW_MACRO_undef
25054 || macinfo_type == DW_MACRO_undef_strp
25055 || macinfo_type == DW_MACRO_undef_sup);
25056 macro_undef (current_file, line, body);
25059 break;
25061 case DW_MACRO_start_file:
25063 unsigned int bytes_read;
25064 int line, file;
25066 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25067 mac_ptr += bytes_read;
25068 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25069 mac_ptr += bytes_read;
25071 if ((line == 0 && !at_commandline)
25072 || (line != 0 && at_commandline))
25073 complaint (_("debug info gives source %d included "
25074 "from %s at %s line %d"),
25075 file, at_commandline ? _("command-line") : _("file"),
25076 line == 0 ? _("zero") : _("non-zero"), line);
25078 if (at_commandline)
25080 /* This DW_MACRO_start_file was executed in the
25081 pass one. */
25082 at_commandline = 0;
25084 else
25085 current_file = macro_start_file (cu, file, line, current_file,
25086 lh);
25088 break;
25090 case DW_MACRO_end_file:
25091 if (! current_file)
25092 complaint (_("macro debug info has an unmatched "
25093 "`close_file' directive"));
25094 else
25096 current_file = current_file->included_by;
25097 if (! current_file)
25099 enum dwarf_macro_record_type next_type;
25101 /* GCC circa March 2002 doesn't produce the zero
25102 type byte marking the end of the compilation
25103 unit. Complain if it's not there, but exit no
25104 matter what. */
25106 /* Do we at least have room for a macinfo type byte? */
25107 if (mac_ptr >= mac_end)
25109 dwarf2_section_buffer_overflow_complaint (section);
25110 return;
25113 /* We don't increment mac_ptr here, so this is just
25114 a look-ahead. */
25115 next_type
25116 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25117 mac_ptr);
25118 if (next_type != 0)
25119 complaint (_("no terminating 0-type entry for "
25120 "macros in `.debug_macinfo' section"));
25122 return;
25125 break;
25127 case DW_MACRO_import:
25128 case DW_MACRO_import_sup:
25130 LONGEST offset;
25131 void **slot;
25132 bfd *include_bfd = abfd;
25133 struct dwarf2_section_info *include_section = section;
25134 const gdb_byte *include_mac_end = mac_end;
25135 int is_dwz = section_is_dwz;
25136 const gdb_byte *new_mac_ptr;
25138 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25139 mac_ptr += offset_size;
25141 if (macinfo_type == DW_MACRO_import_sup)
25143 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25145 dwarf2_read_section (objfile, &dwz->macro);
25147 include_section = &dwz->macro;
25148 include_bfd = get_section_bfd_owner (include_section);
25149 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25150 is_dwz = 1;
25153 new_mac_ptr = include_section->buffer + offset;
25154 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25156 if (*slot != NULL)
25158 /* This has actually happened; see
25159 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25160 complaint (_("recursive DW_MACRO_import in "
25161 ".debug_macro section"));
25163 else
25165 *slot = (void *) new_mac_ptr;
25167 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25168 include_mac_end, current_file, lh,
25169 section, section_is_gnu, is_dwz,
25170 offset_size, include_hash);
25172 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25175 break;
25177 case DW_MACINFO_vendor_ext:
25178 if (!section_is_gnu)
25180 unsigned int bytes_read;
25182 /* This reads the constant, but since we don't recognize
25183 any vendor extensions, we ignore it. */
25184 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25185 mac_ptr += bytes_read;
25186 read_direct_string (abfd, mac_ptr, &bytes_read);
25187 mac_ptr += bytes_read;
25189 /* We don't recognize any vendor extensions. */
25190 break;
25192 /* FALLTHROUGH */
25194 default:
25195 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25196 mac_ptr, mac_end, abfd, offset_size,
25197 section);
25198 if (mac_ptr == NULL)
25199 return;
25200 break;
25202 DIAGNOSTIC_POP
25203 } while (macinfo_type != 0);
25206 static void
25207 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25208 int section_is_gnu)
25210 struct dwarf2_per_objfile *dwarf2_per_objfile
25211 = cu->per_cu->dwarf2_per_objfile;
25212 struct objfile *objfile = dwarf2_per_objfile->objfile;
25213 struct line_header *lh = cu->line_header;
25214 bfd *abfd;
25215 const gdb_byte *mac_ptr, *mac_end;
25216 struct macro_source_file *current_file = 0;
25217 enum dwarf_macro_record_type macinfo_type;
25218 unsigned int offset_size = cu->header.offset_size;
25219 const gdb_byte *opcode_definitions[256];
25220 void **slot;
25221 struct dwarf2_section_info *section;
25222 const char *section_name;
25224 if (cu->dwo_unit != NULL)
25226 if (section_is_gnu)
25228 section = &cu->dwo_unit->dwo_file->sections.macro;
25229 section_name = ".debug_macro.dwo";
25231 else
25233 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25234 section_name = ".debug_macinfo.dwo";
25237 else
25239 if (section_is_gnu)
25241 section = &dwarf2_per_objfile->macro;
25242 section_name = ".debug_macro";
25244 else
25246 section = &dwarf2_per_objfile->macinfo;
25247 section_name = ".debug_macinfo";
25251 dwarf2_read_section (objfile, section);
25252 if (section->buffer == NULL)
25254 complaint (_("missing %s section"), section_name);
25255 return;
25257 abfd = get_section_bfd_owner (section);
25259 /* First pass: Find the name of the base filename.
25260 This filename is needed in order to process all macros whose definition
25261 (or undefinition) comes from the command line. These macros are defined
25262 before the first DW_MACINFO_start_file entry, and yet still need to be
25263 associated to the base file.
25265 To determine the base file name, we scan the macro definitions until we
25266 reach the first DW_MACINFO_start_file entry. We then initialize
25267 CURRENT_FILE accordingly so that any macro definition found before the
25268 first DW_MACINFO_start_file can still be associated to the base file. */
25270 mac_ptr = section->buffer + offset;
25271 mac_end = section->buffer + section->size;
25273 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25274 &offset_size, section_is_gnu);
25275 if (mac_ptr == NULL)
25277 /* We already issued a complaint. */
25278 return;
25283 /* Do we at least have room for a macinfo type byte? */
25284 if (mac_ptr >= mac_end)
25286 /* Complaint is printed during the second pass as GDB will probably
25287 stop the first pass earlier upon finding
25288 DW_MACINFO_start_file. */
25289 break;
25292 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25293 mac_ptr++;
25295 /* Note that we rely on the fact that the corresponding GNU and
25296 DWARF constants are the same. */
25297 DIAGNOSTIC_PUSH
25298 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25299 switch (macinfo_type)
25301 /* A zero macinfo type indicates the end of the macro
25302 information. */
25303 case 0:
25304 break;
25306 case DW_MACRO_define:
25307 case DW_MACRO_undef:
25308 /* Only skip the data by MAC_PTR. */
25310 unsigned int bytes_read;
25312 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25313 mac_ptr += bytes_read;
25314 read_direct_string (abfd, mac_ptr, &bytes_read);
25315 mac_ptr += bytes_read;
25317 break;
25319 case DW_MACRO_start_file:
25321 unsigned int bytes_read;
25322 int line, file;
25324 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25325 mac_ptr += bytes_read;
25326 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25327 mac_ptr += bytes_read;
25329 current_file = macro_start_file (cu, file, line, current_file, lh);
25331 break;
25333 case DW_MACRO_end_file:
25334 /* No data to skip by MAC_PTR. */
25335 break;
25337 case DW_MACRO_define_strp:
25338 case DW_MACRO_undef_strp:
25339 case DW_MACRO_define_sup:
25340 case DW_MACRO_undef_sup:
25342 unsigned int bytes_read;
25344 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25345 mac_ptr += bytes_read;
25346 mac_ptr += offset_size;
25348 break;
25350 case DW_MACRO_import:
25351 case DW_MACRO_import_sup:
25352 /* Note that, according to the spec, a transparent include
25353 chain cannot call DW_MACRO_start_file. So, we can just
25354 skip this opcode. */
25355 mac_ptr += offset_size;
25356 break;
25358 case DW_MACINFO_vendor_ext:
25359 /* Only skip the data by MAC_PTR. */
25360 if (!section_is_gnu)
25362 unsigned int bytes_read;
25364 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25365 mac_ptr += bytes_read;
25366 read_direct_string (abfd, mac_ptr, &bytes_read);
25367 mac_ptr += bytes_read;
25369 /* FALLTHROUGH */
25371 default:
25372 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25373 mac_ptr, mac_end, abfd, offset_size,
25374 section);
25375 if (mac_ptr == NULL)
25376 return;
25377 break;
25379 DIAGNOSTIC_POP
25380 } while (macinfo_type != 0 && current_file == NULL);
25382 /* Second pass: Process all entries.
25384 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25385 command-line macro definitions/undefinitions. This flag is unset when we
25386 reach the first DW_MACINFO_start_file entry. */
25388 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25389 htab_eq_pointer,
25390 NULL, xcalloc, xfree));
25391 mac_ptr = section->buffer + offset;
25392 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25393 *slot = (void *) mac_ptr;
25394 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25395 current_file, lh, section,
25396 section_is_gnu, 0, offset_size,
25397 include_hash.get ());
25400 /* Check if the attribute's form is a DW_FORM_block*
25401 if so return true else false. */
25403 static int
25404 attr_form_is_block (const struct attribute *attr)
25406 return (attr == NULL ? 0 :
25407 attr->form == DW_FORM_block1
25408 || attr->form == DW_FORM_block2
25409 || attr->form == DW_FORM_block4
25410 || attr->form == DW_FORM_block
25411 || attr->form == DW_FORM_exprloc);
25414 /* Return non-zero if ATTR's value is a section offset --- classes
25415 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25416 You may use DW_UNSND (attr) to retrieve such offsets.
25418 Section 7.5.4, "Attribute Encodings", explains that no attribute
25419 may have a value that belongs to more than one of these classes; it
25420 would be ambiguous if we did, because we use the same forms for all
25421 of them. */
25423 static int
25424 attr_form_is_section_offset (const struct attribute *attr)
25426 return (attr->form == DW_FORM_data4
25427 || attr->form == DW_FORM_data8
25428 || attr->form == DW_FORM_sec_offset);
25431 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25432 zero otherwise. When this function returns true, you can apply
25433 dwarf2_get_attr_constant_value to it.
25435 However, note that for some attributes you must check
25436 attr_form_is_section_offset before using this test. DW_FORM_data4
25437 and DW_FORM_data8 are members of both the constant class, and of
25438 the classes that contain offsets into other debug sections
25439 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25440 that, if an attribute's can be either a constant or one of the
25441 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25442 taken as section offsets, not constants.
25444 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25445 cannot handle that. */
25447 static int
25448 attr_form_is_constant (const struct attribute *attr)
25450 switch (attr->form)
25452 case DW_FORM_sdata:
25453 case DW_FORM_udata:
25454 case DW_FORM_data1:
25455 case DW_FORM_data2:
25456 case DW_FORM_data4:
25457 case DW_FORM_data8:
25458 case DW_FORM_implicit_const:
25459 return 1;
25460 default:
25461 return 0;
25466 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25467 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25469 static int
25470 attr_form_is_ref (const struct attribute *attr)
25472 switch (attr->form)
25474 case DW_FORM_ref_addr:
25475 case DW_FORM_ref1:
25476 case DW_FORM_ref2:
25477 case DW_FORM_ref4:
25478 case DW_FORM_ref8:
25479 case DW_FORM_ref_udata:
25480 case DW_FORM_GNU_ref_alt:
25481 return 1;
25482 default:
25483 return 0;
25487 /* Return the .debug_loc section to use for CU.
25488 For DWO files use .debug_loc.dwo. */
25490 static struct dwarf2_section_info *
25491 cu_debug_loc_section (struct dwarf2_cu *cu)
25493 struct dwarf2_per_objfile *dwarf2_per_objfile
25494 = cu->per_cu->dwarf2_per_objfile;
25496 if (cu->dwo_unit)
25498 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25500 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25502 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25503 : &dwarf2_per_objfile->loc);
25506 /* A helper function that fills in a dwarf2_loclist_baton. */
25508 static void
25509 fill_in_loclist_baton (struct dwarf2_cu *cu,
25510 struct dwarf2_loclist_baton *baton,
25511 const struct attribute *attr)
25513 struct dwarf2_per_objfile *dwarf2_per_objfile
25514 = cu->per_cu->dwarf2_per_objfile;
25515 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25517 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25519 baton->per_cu = cu->per_cu;
25520 gdb_assert (baton->per_cu);
25521 /* We don't know how long the location list is, but make sure we
25522 don't run off the edge of the section. */
25523 baton->size = section->size - DW_UNSND (attr);
25524 baton->data = section->buffer + DW_UNSND (attr);
25525 baton->base_address = cu->base_address;
25526 baton->from_dwo = cu->dwo_unit != NULL;
25529 static void
25530 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25531 struct dwarf2_cu *cu, int is_block)
25533 struct dwarf2_per_objfile *dwarf2_per_objfile
25534 = cu->per_cu->dwarf2_per_objfile;
25535 struct objfile *objfile = dwarf2_per_objfile->objfile;
25536 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25538 if (attr_form_is_section_offset (attr)
25539 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25540 the section. If so, fall through to the complaint in the
25541 other branch. */
25542 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25544 struct dwarf2_loclist_baton *baton;
25546 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25548 fill_in_loclist_baton (cu, baton, attr);
25550 if (cu->base_known == 0)
25551 complaint (_("Location list used without "
25552 "specifying the CU base address."));
25554 SYMBOL_ACLASS_INDEX (sym) = (is_block
25555 ? dwarf2_loclist_block_index
25556 : dwarf2_loclist_index);
25557 SYMBOL_LOCATION_BATON (sym) = baton;
25559 else
25561 struct dwarf2_locexpr_baton *baton;
25563 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25564 baton->per_cu = cu->per_cu;
25565 gdb_assert (baton->per_cu);
25567 if (attr_form_is_block (attr))
25569 /* Note that we're just copying the block's data pointer
25570 here, not the actual data. We're still pointing into the
25571 info_buffer for SYM's objfile; right now we never release
25572 that buffer, but when we do clean up properly this may
25573 need to change. */
25574 baton->size = DW_BLOCK (attr)->size;
25575 baton->data = DW_BLOCK (attr)->data;
25577 else
25579 dwarf2_invalid_attrib_class_complaint ("location description",
25580 sym->natural_name ());
25581 baton->size = 0;
25584 SYMBOL_ACLASS_INDEX (sym) = (is_block
25585 ? dwarf2_locexpr_block_index
25586 : dwarf2_locexpr_index);
25587 SYMBOL_LOCATION_BATON (sym) = baton;
25591 /* Return the OBJFILE associated with the compilation unit CU. If CU
25592 came from a separate debuginfo file, then the master objfile is
25593 returned. */
25595 struct objfile *
25596 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25598 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25600 /* Return the master objfile, so that we can report and look up the
25601 correct file containing this variable. */
25602 if (objfile->separate_debug_objfile_backlink)
25603 objfile = objfile->separate_debug_objfile_backlink;
25605 return objfile;
25608 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25609 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25610 CU_HEADERP first. */
25612 static const struct comp_unit_head *
25613 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25614 struct dwarf2_per_cu_data *per_cu)
25616 const gdb_byte *info_ptr;
25618 if (per_cu->cu)
25619 return &per_cu->cu->header;
25621 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25623 memset (cu_headerp, 0, sizeof (*cu_headerp));
25624 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25625 rcuh_kind::COMPILE);
25627 return cu_headerp;
25630 /* Return the address size given in the compilation unit header for CU. */
25633 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25635 struct comp_unit_head cu_header_local;
25636 const struct comp_unit_head *cu_headerp;
25638 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25640 return cu_headerp->addr_size;
25643 /* Return the offset size given in the compilation unit header for CU. */
25646 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25648 struct comp_unit_head cu_header_local;
25649 const struct comp_unit_head *cu_headerp;
25651 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25653 return cu_headerp->offset_size;
25656 /* See its dwarf2loc.h declaration. */
25659 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25661 struct comp_unit_head cu_header_local;
25662 const struct comp_unit_head *cu_headerp;
25664 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25666 if (cu_headerp->version == 2)
25667 return cu_headerp->addr_size;
25668 else
25669 return cu_headerp->offset_size;
25672 /* Return the text offset of the CU. The returned offset comes from
25673 this CU's objfile. If this objfile came from a separate debuginfo
25674 file, then the offset may be different from the corresponding
25675 offset in the parent objfile. */
25677 CORE_ADDR
25678 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25680 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25682 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25685 /* Return a type that is a generic pointer type, the size of which matches
25686 the address size given in the compilation unit header for PER_CU. */
25687 static struct type *
25688 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25690 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25691 struct type *void_type = objfile_type (objfile)->builtin_void;
25692 struct type *addr_type = lookup_pointer_type (void_type);
25693 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25695 if (TYPE_LENGTH (addr_type) == addr_size)
25696 return addr_type;
25698 addr_type
25699 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25700 return addr_type;
25703 /* Return DWARF version number of PER_CU. */
25705 short
25706 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25708 return per_cu->dwarf_version;
25711 /* Locate the .debug_info compilation unit from CU's objfile which contains
25712 the DIE at OFFSET. Raises an error on failure. */
25714 static struct dwarf2_per_cu_data *
25715 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25716 unsigned int offset_in_dwz,
25717 struct dwarf2_per_objfile *dwarf2_per_objfile)
25719 struct dwarf2_per_cu_data *this_cu;
25720 int low, high;
25722 low = 0;
25723 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25724 while (high > low)
25726 struct dwarf2_per_cu_data *mid_cu;
25727 int mid = low + (high - low) / 2;
25729 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25730 if (mid_cu->is_dwz > offset_in_dwz
25731 || (mid_cu->is_dwz == offset_in_dwz
25732 && mid_cu->sect_off + mid_cu->length >= sect_off))
25733 high = mid;
25734 else
25735 low = mid + 1;
25737 gdb_assert (low == high);
25738 this_cu = dwarf2_per_objfile->all_comp_units[low];
25739 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25741 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25742 error (_("Dwarf Error: could not find partial DIE containing "
25743 "offset %s [in module %s]"),
25744 sect_offset_str (sect_off),
25745 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25747 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25748 <= sect_off);
25749 return dwarf2_per_objfile->all_comp_units[low-1];
25751 else
25753 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25754 && sect_off >= this_cu->sect_off + this_cu->length)
25755 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25756 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25757 return this_cu;
25761 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25763 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25764 : per_cu (per_cu_),
25765 mark (false),
25766 has_loclist (false),
25767 checked_producer (false),
25768 producer_is_gxx_lt_4_6 (false),
25769 producer_is_gcc_lt_4_3 (false),
25770 producer_is_icc (false),
25771 producer_is_icc_lt_14 (false),
25772 producer_is_codewarrior (false),
25773 processing_has_namespace_info (false)
25775 per_cu->cu = this;
25778 /* Destroy a dwarf2_cu. */
25780 dwarf2_cu::~dwarf2_cu ()
25782 per_cu->cu = NULL;
25785 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25787 static void
25788 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25789 enum language pretend_language)
25791 struct attribute *attr;
25793 /* Set the language we're debugging. */
25794 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25795 if (attr != nullptr)
25796 set_cu_language (DW_UNSND (attr), cu);
25797 else
25799 cu->language = pretend_language;
25800 cu->language_defn = language_def (cu->language);
25803 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25806 /* Increase the age counter on each cached compilation unit, and free
25807 any that are too old. */
25809 static void
25810 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25812 struct dwarf2_per_cu_data *per_cu, **last_chain;
25814 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25815 per_cu = dwarf2_per_objfile->read_in_chain;
25816 while (per_cu != NULL)
25818 per_cu->cu->last_used ++;
25819 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25820 dwarf2_mark (per_cu->cu);
25821 per_cu = per_cu->cu->read_in_chain;
25824 per_cu = dwarf2_per_objfile->read_in_chain;
25825 last_chain = &dwarf2_per_objfile->read_in_chain;
25826 while (per_cu != NULL)
25828 struct dwarf2_per_cu_data *next_cu;
25830 next_cu = per_cu->cu->read_in_chain;
25832 if (!per_cu->cu->mark)
25834 delete per_cu->cu;
25835 *last_chain = next_cu;
25837 else
25838 last_chain = &per_cu->cu->read_in_chain;
25840 per_cu = next_cu;
25844 /* Remove a single compilation unit from the cache. */
25846 static void
25847 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25849 struct dwarf2_per_cu_data *per_cu, **last_chain;
25850 struct dwarf2_per_objfile *dwarf2_per_objfile
25851 = target_per_cu->dwarf2_per_objfile;
25853 per_cu = dwarf2_per_objfile->read_in_chain;
25854 last_chain = &dwarf2_per_objfile->read_in_chain;
25855 while (per_cu != NULL)
25857 struct dwarf2_per_cu_data *next_cu;
25859 next_cu = per_cu->cu->read_in_chain;
25861 if (per_cu == target_per_cu)
25863 delete per_cu->cu;
25864 per_cu->cu = NULL;
25865 *last_chain = next_cu;
25866 break;
25868 else
25869 last_chain = &per_cu->cu->read_in_chain;
25871 per_cu = next_cu;
25875 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25876 We store these in a hash table separate from the DIEs, and preserve them
25877 when the DIEs are flushed out of cache.
25879 The CU "per_cu" pointer is needed because offset alone is not enough to
25880 uniquely identify the type. A file may have multiple .debug_types sections,
25881 or the type may come from a DWO file. Furthermore, while it's more logical
25882 to use per_cu->section+offset, with Fission the section with the data is in
25883 the DWO file but we don't know that section at the point we need it.
25884 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25885 because we can enter the lookup routine, get_die_type_at_offset, from
25886 outside this file, and thus won't necessarily have PER_CU->cu.
25887 Fortunately, PER_CU is stable for the life of the objfile. */
25889 struct dwarf2_per_cu_offset_and_type
25891 const struct dwarf2_per_cu_data *per_cu;
25892 sect_offset sect_off;
25893 struct type *type;
25896 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25898 static hashval_t
25899 per_cu_offset_and_type_hash (const void *item)
25901 const struct dwarf2_per_cu_offset_and_type *ofs
25902 = (const struct dwarf2_per_cu_offset_and_type *) item;
25904 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25907 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25909 static int
25910 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25912 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25913 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25914 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25915 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25917 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25918 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25921 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25922 table if necessary. For convenience, return TYPE.
25924 The DIEs reading must have careful ordering to:
25925 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25926 reading current DIE.
25927 * Not trying to dereference contents of still incompletely read in types
25928 while reading in other DIEs.
25929 * Enable referencing still incompletely read in types just by a pointer to
25930 the type without accessing its fields.
25932 Therefore caller should follow these rules:
25933 * Try to fetch any prerequisite types we may need to build this DIE type
25934 before building the type and calling set_die_type.
25935 * After building type call set_die_type for current DIE as soon as
25936 possible before fetching more types to complete the current type.
25937 * Make the type as complete as possible before fetching more types. */
25939 static struct type *
25940 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25942 struct dwarf2_per_objfile *dwarf2_per_objfile
25943 = cu->per_cu->dwarf2_per_objfile;
25944 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25945 struct objfile *objfile = dwarf2_per_objfile->objfile;
25946 struct attribute *attr;
25947 struct dynamic_prop prop;
25949 /* For Ada types, make sure that the gnat-specific data is always
25950 initialized (if not already set). There are a few types where
25951 we should not be doing so, because the type-specific area is
25952 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25953 where the type-specific area is used to store the floatformat).
25954 But this is not a problem, because the gnat-specific information
25955 is actually not needed for these types. */
25956 if (need_gnat_info (cu)
25957 && TYPE_CODE (type) != TYPE_CODE_FUNC
25958 && TYPE_CODE (type) != TYPE_CODE_FLT
25959 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25960 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25961 && TYPE_CODE (type) != TYPE_CODE_METHOD
25962 && !HAVE_GNAT_AUX_INFO (type))
25963 INIT_GNAT_SPECIFIC (type);
25965 /* Read DW_AT_allocated and set in type. */
25966 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25967 if (attr_form_is_block (attr))
25969 struct type *prop_type
25970 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25971 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25972 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25974 else if (attr != NULL)
25976 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25977 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25978 sect_offset_str (die->sect_off));
25981 /* Read DW_AT_associated and set in type. */
25982 attr = dwarf2_attr (die, DW_AT_associated, cu);
25983 if (attr_form_is_block (attr))
25985 struct type *prop_type
25986 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25987 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25988 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25990 else if (attr != NULL)
25992 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25993 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25994 sect_offset_str (die->sect_off));
25997 /* Read DW_AT_data_location and set in type. */
25998 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25999 if (attr_to_dynamic_prop (attr, die, cu, &prop,
26000 dwarf2_per_cu_addr_type (cu->per_cu)))
26001 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
26003 if (dwarf2_per_objfile->die_type_hash == NULL)
26005 dwarf2_per_objfile->die_type_hash =
26006 htab_create_alloc_ex (127,
26007 per_cu_offset_and_type_hash,
26008 per_cu_offset_and_type_eq,
26009 NULL,
26010 &objfile->objfile_obstack,
26011 hashtab_obstack_allocate,
26012 dummy_obstack_deallocate);
26015 ofs.per_cu = cu->per_cu;
26016 ofs.sect_off = die->sect_off;
26017 ofs.type = type;
26018 slot = (struct dwarf2_per_cu_offset_and_type **)
26019 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
26020 if (*slot)
26021 complaint (_("A problem internal to GDB: DIE %s has type already set"),
26022 sect_offset_str (die->sect_off));
26023 *slot = XOBNEW (&objfile->objfile_obstack,
26024 struct dwarf2_per_cu_offset_and_type);
26025 **slot = ofs;
26026 return type;
26029 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26030 or return NULL if the die does not have a saved type. */
26032 static struct type *
26033 get_die_type_at_offset (sect_offset sect_off,
26034 struct dwarf2_per_cu_data *per_cu)
26036 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26037 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26039 if (dwarf2_per_objfile->die_type_hash == NULL)
26040 return NULL;
26042 ofs.per_cu = per_cu;
26043 ofs.sect_off = sect_off;
26044 slot = ((struct dwarf2_per_cu_offset_and_type *)
26045 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26046 if (slot)
26047 return slot->type;
26048 else
26049 return NULL;
26052 /* Look up the type for DIE in CU in die_type_hash,
26053 or return NULL if DIE does not have a saved type. */
26055 static struct type *
26056 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26058 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26061 /* Add a dependence relationship from CU to REF_PER_CU. */
26063 static void
26064 dwarf2_add_dependence (struct dwarf2_cu *cu,
26065 struct dwarf2_per_cu_data *ref_per_cu)
26067 void **slot;
26069 if (cu->dependencies == NULL)
26070 cu->dependencies
26071 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26072 NULL, &cu->comp_unit_obstack,
26073 hashtab_obstack_allocate,
26074 dummy_obstack_deallocate);
26076 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26077 if (*slot == NULL)
26078 *slot = ref_per_cu;
26081 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26082 Set the mark field in every compilation unit in the
26083 cache that we must keep because we are keeping CU. */
26085 static int
26086 dwarf2_mark_helper (void **slot, void *data)
26088 struct dwarf2_per_cu_data *per_cu;
26090 per_cu = (struct dwarf2_per_cu_data *) *slot;
26092 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26093 reading of the chain. As such dependencies remain valid it is not much
26094 useful to track and undo them during QUIT cleanups. */
26095 if (per_cu->cu == NULL)
26096 return 1;
26098 if (per_cu->cu->mark)
26099 return 1;
26100 per_cu->cu->mark = true;
26102 if (per_cu->cu->dependencies != NULL)
26103 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26105 return 1;
26108 /* Set the mark field in CU and in every other compilation unit in the
26109 cache that we must keep because we are keeping CU. */
26111 static void
26112 dwarf2_mark (struct dwarf2_cu *cu)
26114 if (cu->mark)
26115 return;
26116 cu->mark = true;
26117 if (cu->dependencies != NULL)
26118 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26121 static void
26122 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26124 while (per_cu)
26126 per_cu->cu->mark = false;
26127 per_cu = per_cu->cu->read_in_chain;
26131 /* Trivial hash function for partial_die_info: the hash value of a DIE
26132 is its offset in .debug_info for this objfile. */
26134 static hashval_t
26135 partial_die_hash (const void *item)
26137 const struct partial_die_info *part_die
26138 = (const struct partial_die_info *) item;
26140 return to_underlying (part_die->sect_off);
26143 /* Trivial comparison function for partial_die_info structures: two DIEs
26144 are equal if they have the same offset. */
26146 static int
26147 partial_die_eq (const void *item_lhs, const void *item_rhs)
26149 const struct partial_die_info *part_die_lhs
26150 = (const struct partial_die_info *) item_lhs;
26151 const struct partial_die_info *part_die_rhs
26152 = (const struct partial_die_info *) item_rhs;
26154 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26157 struct cmd_list_element *set_dwarf_cmdlist;
26158 struct cmd_list_element *show_dwarf_cmdlist;
26160 static void
26161 set_dwarf_cmd (const char *args, int from_tty)
26163 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26164 gdb_stdout);
26167 static void
26168 show_dwarf_cmd (const char *args, int from_tty)
26170 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26173 bool dwarf_always_disassemble;
26175 static void
26176 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26177 struct cmd_list_element *c, const char *value)
26179 fprintf_filtered (file,
26180 _("Whether to always disassemble "
26181 "DWARF expressions is %s.\n"),
26182 value);
26185 static void
26186 show_check_physname (struct ui_file *file, int from_tty,
26187 struct cmd_list_element *c, const char *value)
26189 fprintf_filtered (file,
26190 _("Whether to check \"physname\" is %s.\n"),
26191 value);
26194 void
26195 _initialize_dwarf2_read (void)
26197 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26198 Set DWARF specific variables.\n\
26199 Configure DWARF variables such as the cache size."),
26200 &set_dwarf_cmdlist, "maintenance set dwarf ",
26201 0/*allow-unknown*/, &maintenance_set_cmdlist);
26203 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26204 Show DWARF specific variables.\n\
26205 Show DWARF variables such as the cache size."),
26206 &show_dwarf_cmdlist, "maintenance show dwarf ",
26207 0/*allow-unknown*/, &maintenance_show_cmdlist);
26209 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26210 &dwarf_max_cache_age, _("\
26211 Set the upper bound on the age of cached DWARF compilation units."), _("\
26212 Show the upper bound on the age of cached DWARF compilation units."), _("\
26213 A higher limit means that cached compilation units will be stored\n\
26214 in memory longer, and more total memory will be used. Zero disables\n\
26215 caching, which can slow down startup."),
26216 NULL,
26217 show_dwarf_max_cache_age,
26218 &set_dwarf_cmdlist,
26219 &show_dwarf_cmdlist);
26221 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26222 &dwarf_always_disassemble, _("\
26223 Set whether `info address' always disassembles DWARF expressions."), _("\
26224 Show whether `info address' always disassembles DWARF expressions."), _("\
26225 When enabled, DWARF expressions are always printed in an assembly-like\n\
26226 syntax. When disabled, expressions will be printed in a more\n\
26227 conversational style, when possible."),
26228 NULL,
26229 show_dwarf_always_disassemble,
26230 &set_dwarf_cmdlist,
26231 &show_dwarf_cmdlist);
26233 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26234 Set debugging of the DWARF reader."), _("\
26235 Show debugging of the DWARF reader."), _("\
26236 When enabled (non-zero), debugging messages are printed during DWARF\n\
26237 reading and symtab expansion. A value of 1 (one) provides basic\n\
26238 information. A value greater than 1 provides more verbose information."),
26239 NULL,
26240 NULL,
26241 &setdebuglist, &showdebuglist);
26243 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26244 Set debugging of the DWARF DIE reader."), _("\
26245 Show debugging of the DWARF DIE reader."), _("\
26246 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26247 The value is the maximum depth to print."),
26248 NULL,
26249 NULL,
26250 &setdebuglist, &showdebuglist);
26252 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26253 Set debugging of the dwarf line reader."), _("\
26254 Show debugging of the dwarf line reader."), _("\
26255 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26256 A value of 1 (one) provides basic information.\n\
26257 A value greater than 1 provides more verbose information."),
26258 NULL,
26259 NULL,
26260 &setdebuglist, &showdebuglist);
26262 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26263 Set cross-checking of \"physname\" code against demangler."), _("\
26264 Show cross-checking of \"physname\" code against demangler."), _("\
26265 When enabled, GDB's internal \"physname\" code is checked against\n\
26266 the demangler."),
26267 NULL, show_check_physname,
26268 &setdebuglist, &showdebuglist);
26270 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26271 no_class, &use_deprecated_index_sections, _("\
26272 Set whether to use deprecated gdb_index sections."), _("\
26273 Show whether to use deprecated gdb_index sections."), _("\
26274 When enabled, deprecated .gdb_index sections are used anyway.\n\
26275 Normally they are ignored either because of a missing feature or\n\
26276 performance issue.\n\
26277 Warning: This option must be enabled before gdb reads the file."),
26278 NULL,
26279 NULL,
26280 &setlist, &showlist);
26282 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26283 &dwarf2_locexpr_funcs);
26284 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26285 &dwarf2_loclist_funcs);
26287 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26288 &dwarf2_block_frame_base_locexpr_funcs);
26289 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26290 &dwarf2_block_frame_base_loclist_funcs);
26292 #if GDB_SELF_TEST
26293 selftests::register_test ("dw2_expand_symtabs_matching",
26294 selftests::dw2_expand_symtabs_matching::run_test);
26295 #endif