]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Move die_info to new header
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* 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. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "bfd.h"
44 #include "elf-bfd.h"
45 #include "symtab.h"
46 #include "gdbtypes.h"
47 #include "objfiles.h"
48 #include "dwarf2.h"
49 #include "buildsym.h"
50 #include "demangle.h"
51 #include "gdb-demangle.h"
52 #include "filenames.h" /* for DOSish file names */
53 #include "language.h"
54 #include "complaints.h"
55 #include "dwarf2/expr.h"
56 #include "dwarf2/loc.h"
57 #include "cp-support.h"
58 #include "hashtab.h"
59 #include "command.h"
60 #include "gdbcmd.h"
61 #include "block.h"
62 #include "addrmap.h"
63 #include "typeprint.h"
64 #include "psympriv.h"
65 #include "c-lang.h"
66 #include "go-lang.h"
67 #include "valprint.h"
68 #include "gdbcore.h" /* for gnutarget */
69 #include "gdb/gdb-index.h"
70 #include "gdb_bfd.h"
71 #include "f-lang.h"
72 #include "source.h"
73 #include "build-id.h"
74 #include "namespace.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/hash_enum.h"
79 #include "filename-seen-cache.h"
80 #include "producer.h"
81 #include <fcntl.h>
82 #include <algorithm>
83 #include <unordered_map>
84 #include "gdbsupport/selftest.h"
85 #include "rust-lang.h"
86 #include "gdbsupport/pathstuff.h"
87 #include "count-one-bits.h"
88 #include "debuginfod-support.h"
89
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 unsigned int dwarf_line_debug = 0;
100
101 /* When true, cross-check physname against demangler. */
102 static bool check_physname = false;
103
104 /* When true, do not reject deprecated .gdb_index sections. */
105 static bool use_deprecated_index_sections = false;
106
107 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* An index into a (C++) symbol name component in a symbol name as
117 recorded in the mapped_index's symbol table. For each C++ symbol
118 in the symbol table, we record one entry for the start of each
119 component in the symbol in a table of name components, and then
120 sort the table, in order to be able to binary search symbol names,
121 ignoring leading namespaces, both completion and regular look up.
122 For example, for symbol "A::B::C", we'll have an entry that points
123 to "A::B::C", another that points to "B::C", and another for "C".
124 Note that function symbols in GDB index have no parameter
125 information, just the function/method names. You can convert a
126 name_component to a "const char *" using the
127 'mapped_index::symbol_name_at(offset_type)' method. */
128
129 struct name_component
130 {
131 /* Offset in the symbol name where the component starts. Stored as
132 a (32-bit) offset instead of a pointer to save memory and improve
133 locality on 64-bit architectures. */
134 offset_type name_offset;
135
136 /* The symbol's index in the symbol and constant pool tables of a
137 mapped_index. */
138 offset_type idx;
139 };
140
141 /* Base class containing bits shared by both .gdb_index and
142 .debug_name indexes. */
143
144 struct mapped_index_base
145 {
146 mapped_index_base () = default;
147 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
148
149 /* The name_component table (a sorted vector). See name_component's
150 description above. */
151 std::vector<name_component> name_components;
152
153 /* How NAME_COMPONENTS is sorted. */
154 enum case_sensitivity name_components_casing;
155
156 /* Return the number of names in the symbol table. */
157 virtual size_t symbol_name_count () const = 0;
158
159 /* Get the name of the symbol at IDX in the symbol table. */
160 virtual const char *symbol_name_at (offset_type idx) const = 0;
161
162 /* Return whether the name at IDX in the symbol table should be
163 ignored. */
164 virtual bool symbol_name_slot_invalid (offset_type idx) const
165 {
166 return false;
167 }
168
169 /* Build the symbol name component sorted vector, if we haven't
170 yet. */
171 void build_name_components ();
172
173 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
174 possible matches for LN_NO_PARAMS in the name component
175 vector. */
176 std::pair<std::vector<name_component>::const_iterator,
177 std::vector<name_component>::const_iterator>
178 find_name_components_bounds (const lookup_name_info &ln_no_params,
179 enum language lang) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version = 0;
199
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
202
203 /* The symbol table, implemented as a hash table. */
204 gdb::array_view<symbol_table_slot> symbol_table;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool = nullptr;
208
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
212 return bucket.name == 0 && bucket.vec == 0;
213 }
214
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
217 const char *symbol_name_at (offset_type idx) const override
218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
219
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
222 };
223
224 /* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
226 struct mapped_debug_names final : public mapped_index_base
227 {
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
274 };
275
276 /* See dwarf2read.h. */
277
278 dwarf2_per_objfile *
279 get_dwarf2_per_objfile (struct objfile *objfile)
280 {
281 return dwarf2_objfile_data_key.get (objfile);
282 }
283
284 /* Default names of the debugging sections. */
285
286 /* Note that if the debugging section has been compressed, it might
287 have a name like .zdebug_info. */
288
289 static const struct dwarf2_debug_sections dwarf2_elf_names =
290 {
291 { ".debug_info", ".zdebug_info" },
292 { ".debug_abbrev", ".zdebug_abbrev" },
293 { ".debug_line", ".zdebug_line" },
294 { ".debug_loc", ".zdebug_loc" },
295 { ".debug_loclists", ".zdebug_loclists" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_str_offsets", ".zdebug_str_offsets" },
300 { ".debug_line_str", ".zdebug_line_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_rnglists", ".zdebug_rnglists" },
303 { ".debug_types", ".zdebug_types" },
304 { ".debug_addr", ".zdebug_addr" },
305 { ".debug_frame", ".zdebug_frame" },
306 { ".eh_frame", NULL },
307 { ".gdb_index", ".zgdb_index" },
308 { ".debug_names", ".zdebug_names" },
309 { ".debug_aranges", ".zdebug_aranges" },
310 23
311 };
312
313 /* List of DWO/DWP sections. */
314
315 static const struct dwop_section_names
316 {
317 struct dwarf2_section_names abbrev_dwo;
318 struct dwarf2_section_names info_dwo;
319 struct dwarf2_section_names line_dwo;
320 struct dwarf2_section_names loc_dwo;
321 struct dwarf2_section_names loclists_dwo;
322 struct dwarf2_section_names macinfo_dwo;
323 struct dwarf2_section_names macro_dwo;
324 struct dwarf2_section_names str_dwo;
325 struct dwarf2_section_names str_offsets_dwo;
326 struct dwarf2_section_names types_dwo;
327 struct dwarf2_section_names cu_index;
328 struct dwarf2_section_names tu_index;
329 }
330 dwop_section_names =
331 {
332 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
333 { ".debug_info.dwo", ".zdebug_info.dwo" },
334 { ".debug_line.dwo", ".zdebug_line.dwo" },
335 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
336 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
337 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
338 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
339 { ".debug_str.dwo", ".zdebug_str.dwo" },
340 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
341 { ".debug_types.dwo", ".zdebug_types.dwo" },
342 { ".debug_cu_index", ".zdebug_cu_index" },
343 { ".debug_tu_index", ".zdebug_tu_index" },
344 };
345
346 /* local data types */
347
348 /* Type used for delaying computation of method physnames.
349 See comments for compute_delayed_physnames. */
350 struct delayed_method_info
351 {
352 /* The type to which the method is attached, i.e., its parent class. */
353 struct type *type;
354
355 /* The index of the method in the type's function fieldlists. */
356 int fnfield_index;
357
358 /* The index of the method in the fieldlist. */
359 int index;
360
361 /* The name of the DIE. */
362 const char *name;
363
364 /* The DIE associated with this method. */
365 struct die_info *die;
366 };
367
368 /* Internal state when decoding a particular compilation unit. */
369 struct dwarf2_cu
370 {
371 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
372 ~dwarf2_cu ();
373
374 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
375
376 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
377 Create the set of symtabs used by this TU, or if this TU is sharing
378 symtabs with another TU and the symtabs have already been created
379 then restore those symtabs in the line header.
380 We don't need the pc/line-number mapping for type units. */
381 void setup_type_unit_groups (struct die_info *die);
382
383 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
384 buildsym_compunit constructor. */
385 struct compunit_symtab *start_symtab (const char *name,
386 const char *comp_dir,
387 CORE_ADDR low_pc);
388
389 /* Reset the builder. */
390 void reset_builder () { m_builder.reset (); }
391
392 /* The header of the compilation unit. */
393 struct comp_unit_head header {};
394
395 /* Base address of this compilation unit. */
396 CORE_ADDR base_address = 0;
397
398 /* Non-zero if base_address has been set. */
399 int base_known = 0;
400
401 /* The language we are debugging. */
402 enum language language = language_unknown;
403 const struct language_defn *language_defn = nullptr;
404
405 const char *producer = nullptr;
406
407 private:
408 /* The symtab builder for this CU. This is only non-NULL when full
409 symbols are being read. */
410 std::unique_ptr<buildsym_compunit> m_builder;
411
412 public:
413 /* The generic symbol table building routines have separate lists for
414 file scope symbols and all all other scopes (local scopes). So
415 we need to select the right one to pass to add_symbol_to_list().
416 We do it by keeping a pointer to the correct list in list_in_scope.
417
418 FIXME: The original dwarf code just treated the file scope as the
419 first local scope, and all other local scopes as nested local
420 scopes, and worked fine. Check to see if we really need to
421 distinguish these in buildsym.c. */
422 struct pending **list_in_scope = nullptr;
423
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
426 htab_t partial_dies = nullptr;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 auto_obstack comp_unit_obstack;
431
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain = nullptr;
437
438 /* Backlink to our per_cu entry. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used = 0;
443
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
446 htab_t die_hash = nullptr;
447
448 /* Full DIEs if read in. */
449 struct die_info *dies = nullptr;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies = nullptr;
455
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header = nullptr;
458 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
459 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
460 this is the DW_TAG_compile_unit die for this CU. We'll hold on
461 to the line header as long as this DIE is being processed. See
462 process_die_scope. */
463 die_info *line_header_die_owner = nullptr;
464
465 /* A list of methods which need to have physnames computed
466 after all type information has been read. */
467 std::vector<delayed_method_info> method_list;
468
469 /* To be copied to symtab->call_site_htab. */
470 htab_t call_site_htab = nullptr;
471
472 /* Non-NULL if this CU came from a DWO file.
473 There is an invariant here that is important to remember:
474 Except for attributes copied from the top level DIE in the "main"
475 (or "stub") file in preparation for reading the DWO file
476 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
477 Either there isn't a DWO file (in which case this is NULL and the point
478 is moot), or there is and either we're not going to read it (in which
479 case this is NULL) or there is and we are reading it (in which case this
480 is non-NULL). */
481 struct dwo_unit *dwo_unit = nullptr;
482
483 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
484 Note this value comes from the Fission stub CU/TU's DIE. */
485 gdb::optional<ULONGEST> addr_base;
486
487 /* The DW_AT_rnglists_base attribute if present.
488 Note this value comes from the Fission stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
496 ULONGEST ranges_base = 0;
497
498 /* When reading debug info generated by older versions of rustc, we
499 have to rewrite some union types to be struct types with a
500 variant part. This rewriting must be done after the CU is fully
501 read in, because otherwise at the point of rewriting some struct
502 type might not have been fully processed. So, we keep a list of
503 all such types here and process them after expansion. */
504 std::vector<struct type *> rust_unions;
505
506 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
507 files, the value is implicitly zero. For DWARF 5 version DWO files, the
508 value is often implicit and is the size of the header of
509 .debug_str_offsets section (8 or 4, depending on the address size). */
510 gdb::optional<ULONGEST> str_offsets_base;
511
512 /* Mark used when releasing cached dies. */
513 bool mark : 1;
514
515 /* This CU references .debug_loc. See the symtab->locations_valid field.
516 This test is imperfect as there may exist optimized debug code not using
517 any location list and still facing inlining issues if handled as
518 unoptimized code. For a future better test see GCC PR other/32998. */
519 bool has_loclist : 1;
520
521 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
522 if all the producer_is_* fields are valid. This information is cached
523 because profiling CU expansion showed excessive time spent in
524 producer_is_gxx_lt_4_6. */
525 bool checked_producer : 1;
526 bool producer_is_gxx_lt_4_6 : 1;
527 bool producer_is_gcc_lt_4_3 : 1;
528 bool producer_is_icc : 1;
529 bool producer_is_icc_lt_14 : 1;
530 bool producer_is_codewarrior : 1;
531
532 /* When true, the file that we're processing is known to have
533 debugging info for C++ namespaces. GCC 3.3.x did not produce
534 this information, but later versions do. */
535
536 bool processing_has_namespace_info : 1;
537
538 struct partial_die_info *find_partial_die (sect_offset sect_off);
539
540 /* If this CU was inherited by another CU (via specification,
541 abstract_origin, etc), this is the ancestor CU. */
542 dwarf2_cu *ancestor;
543
544 /* Get the buildsym_compunit for this CU. */
545 buildsym_compunit *get_builder ()
546 {
547 /* If this CU has a builder associated with it, use that. */
548 if (m_builder != nullptr)
549 return m_builder.get ();
550
551 /* Otherwise, search ancestors for a valid builder. */
552 if (ancestor != nullptr)
553 return ancestor->get_builder ();
554
555 return nullptr;
556 }
557 };
558
559 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
560 This includes type_unit_group and quick_file_names. */
561
562 struct stmt_list_hash
563 {
564 /* The DWO unit this table is from or NULL if there is none. */
565 struct dwo_unit *dwo_unit;
566
567 /* Offset in .debug_line or .debug_line.dwo. */
568 sect_offset line_sect_off;
569 };
570
571 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
572 an object of this type. */
573
574 struct type_unit_group
575 {
576 /* dwarf2read.c's main "handle" on a TU symtab.
577 To simplify things we create an artificial CU that "includes" all the
578 type units using this stmt_list so that the rest of the code still has
579 a "per_cu" handle on the symtab. */
580 struct dwarf2_per_cu_data per_cu;
581
582 /* The TUs that share this DW_AT_stmt_list entry.
583 This is added to while parsing type units to build partial symtabs,
584 and is deleted afterwards and not used again. */
585 std::vector<signatured_type *> *tus;
586
587 /* The compunit symtab.
588 Type units in a group needn't all be defined in the same source file,
589 so we create an essentially anonymous symtab as the compunit symtab. */
590 struct compunit_symtab *compunit_symtab;
591
592 /* The data used to construct the hash key. */
593 struct stmt_list_hash hash;
594
595 /* The symbol tables for this TU (obtained from the files listed in
596 DW_AT_stmt_list).
597 WARNING: The order of entries here must match the order of entries
598 in the line header. After the first TU using this type_unit_group, the
599 line header for the subsequent TUs is recreated from this. This is done
600 because we need to use the same symtabs for each TU using the same
601 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
602 there's no guarantee the line header doesn't have duplicate entries. */
603 struct symtab **symtabs;
604 };
605
606 /* These sections are what may appear in a (real or virtual) DWO file. */
607
608 struct dwo_sections
609 {
610 struct dwarf2_section_info abbrev;
611 struct dwarf2_section_info line;
612 struct dwarf2_section_info loc;
613 struct dwarf2_section_info loclists;
614 struct dwarf2_section_info macinfo;
615 struct dwarf2_section_info macro;
616 struct dwarf2_section_info str;
617 struct dwarf2_section_info str_offsets;
618 /* In the case of a virtual DWO file, these two are unused. */
619 struct dwarf2_section_info info;
620 std::vector<dwarf2_section_info> types;
621 };
622
623 /* CUs/TUs in DWP/DWO files. */
624
625 struct dwo_unit
626 {
627 /* Backlink to the containing struct dwo_file. */
628 struct dwo_file *dwo_file;
629
630 /* The "id" that distinguishes this CU/TU.
631 .debug_info calls this "dwo_id", .debug_types calls this "signature".
632 Since signatures came first, we stick with it for consistency. */
633 ULONGEST signature;
634
635 /* The section this CU/TU lives in, in the DWO file. */
636 struct dwarf2_section_info *section;
637
638 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
639 sect_offset sect_off;
640 unsigned int length;
641
642 /* For types, offset in the type's DIE of the type defined by this TU. */
643 cu_offset type_offset_in_tu;
644 };
645
646 /* include/dwarf2.h defines the DWP section codes.
647 It defines a max value but it doesn't define a min value, which we
648 use for error checking, so provide one. */
649
650 enum dwp_v2_section_ids
651 {
652 DW_SECT_MIN = 1
653 };
654
655 /* Data for one DWO file.
656
657 This includes virtual DWO files (a virtual DWO file is a DWO file as it
658 appears in a DWP file). DWP files don't really have DWO files per se -
659 comdat folding of types "loses" the DWO file they came from, and from
660 a high level view DWP files appear to contain a mass of random types.
661 However, to maintain consistency with the non-DWP case we pretend DWP
662 files contain virtual DWO files, and we assign each TU with one virtual
663 DWO file (generally based on the line and abbrev section offsets -
664 a heuristic that seems to work in practice). */
665
666 struct dwo_file
667 {
668 dwo_file () = default;
669 DISABLE_COPY_AND_ASSIGN (dwo_file);
670
671 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
672 For virtual DWO files the name is constructed from the section offsets
673 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
674 from related CU+TUs. */
675 const char *dwo_name = nullptr;
676
677 /* The DW_AT_comp_dir attribute. */
678 const char *comp_dir = nullptr;
679
680 /* The bfd, when the file is open. Otherwise this is NULL.
681 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
682 gdb_bfd_ref_ptr dbfd;
683
684 /* The sections that make up this DWO file.
685 Remember that for virtual DWO files in DWP V2, these are virtual
686 sections (for lack of a better name). */
687 struct dwo_sections sections {};
688
689 /* The CUs in the file.
690 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
691 an extension to handle LLVM's Link Time Optimization output (where
692 multiple source files may be compiled into a single object/dwo pair). */
693 htab_up cus;
694
695 /* Table of TUs in the file.
696 Each element is a struct dwo_unit. */
697 htab_up tus;
698 };
699
700 /* These sections are what may appear in a DWP file. */
701
702 struct dwp_sections
703 {
704 /* These are used by both DWP version 1 and 2. */
705 struct dwarf2_section_info str;
706 struct dwarf2_section_info cu_index;
707 struct dwarf2_section_info tu_index;
708
709 /* These are only used by DWP version 2 files.
710 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
711 sections are referenced by section number, and are not recorded here.
712 In DWP version 2 there is at most one copy of all these sections, each
713 section being (effectively) comprised of the concatenation of all of the
714 individual sections that exist in the version 1 format.
715 To keep the code simple we treat each of these concatenated pieces as a
716 section itself (a virtual section?). */
717 struct dwarf2_section_info abbrev;
718 struct dwarf2_section_info info;
719 struct dwarf2_section_info line;
720 struct dwarf2_section_info loc;
721 struct dwarf2_section_info macinfo;
722 struct dwarf2_section_info macro;
723 struct dwarf2_section_info str_offsets;
724 struct dwarf2_section_info types;
725 };
726
727 /* These sections are what may appear in a virtual DWO file in DWP version 1.
728 A virtual DWO file is a DWO file as it appears in a DWP file. */
729
730 struct virtual_v1_dwo_sections
731 {
732 struct dwarf2_section_info abbrev;
733 struct dwarf2_section_info line;
734 struct dwarf2_section_info loc;
735 struct dwarf2_section_info macinfo;
736 struct dwarf2_section_info macro;
737 struct dwarf2_section_info str_offsets;
738 /* Each DWP hash table entry records one CU or one TU.
739 That is recorded here, and copied to dwo_unit.section. */
740 struct dwarf2_section_info info_or_types;
741 };
742
743 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
744 In version 2, the sections of the DWO files are concatenated together
745 and stored in one section of that name. Thus each ELF section contains
746 several "virtual" sections. */
747
748 struct virtual_v2_dwo_sections
749 {
750 bfd_size_type abbrev_offset;
751 bfd_size_type abbrev_size;
752
753 bfd_size_type line_offset;
754 bfd_size_type line_size;
755
756 bfd_size_type loc_offset;
757 bfd_size_type loc_size;
758
759 bfd_size_type macinfo_offset;
760 bfd_size_type macinfo_size;
761
762 bfd_size_type macro_offset;
763 bfd_size_type macro_size;
764
765 bfd_size_type str_offsets_offset;
766 bfd_size_type str_offsets_size;
767
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.section. */
770 bfd_size_type info_or_types_offset;
771 bfd_size_type info_or_types_size;
772 };
773
774 /* Contents of DWP hash tables. */
775
776 struct dwp_hash_table
777 {
778 uint32_t version, nr_columns;
779 uint32_t nr_units, nr_slots;
780 const gdb_byte *hash_table, *unit_table;
781 union
782 {
783 struct
784 {
785 const gdb_byte *indices;
786 } v1;
787 struct
788 {
789 /* This is indexed by column number and gives the id of the section
790 in that column. */
791 #define MAX_NR_V2_DWO_SECTIONS \
792 (1 /* .debug_info or .debug_types */ \
793 + 1 /* .debug_abbrev */ \
794 + 1 /* .debug_line */ \
795 + 1 /* .debug_loc */ \
796 + 1 /* .debug_str_offsets */ \
797 + 1 /* .debug_macro or .debug_macinfo */)
798 int section_ids[MAX_NR_V2_DWO_SECTIONS];
799 const gdb_byte *offsets;
800 const gdb_byte *sizes;
801 } v2;
802 } section_pool;
803 };
804
805 /* Data for one DWP file. */
806
807 struct dwp_file
808 {
809 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
810 : name (name_),
811 dbfd (std::move (abfd))
812 {
813 }
814
815 /* Name of the file. */
816 const char *name;
817
818 /* File format version. */
819 int version = 0;
820
821 /* The bfd. */
822 gdb_bfd_ref_ptr dbfd;
823
824 /* Section info for this file. */
825 struct dwp_sections sections {};
826
827 /* Table of CUs in the file. */
828 const struct dwp_hash_table *cus = nullptr;
829
830 /* Table of TUs in the file. */
831 const struct dwp_hash_table *tus = nullptr;
832
833 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
834 htab_up loaded_cus;
835 htab_up loaded_tus;
836
837 /* Table to map ELF section numbers to their sections.
838 This is only needed for the DWP V1 file format. */
839 unsigned int num_sections = 0;
840 asection **elf_sections = nullptr;
841 };
842
843 /* Struct used to pass misc. parameters to read_die_and_children, et
844 al. which are used for both .debug_info and .debug_types dies.
845 All parameters here are unchanging for the life of the call. This
846 struct exists to abstract away the constant parameters of die reading. */
847
848 struct die_reader_specs
849 {
850 /* The bfd of die_section. */
851 bfd* abfd;
852
853 /* The CU of the DIE we are parsing. */
854 struct dwarf2_cu *cu;
855
856 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
857 struct dwo_file *dwo_file;
858
859 /* The section the die comes from.
860 This is either .debug_info or .debug_types, or the .dwo variants. */
861 struct dwarf2_section_info *die_section;
862
863 /* die_section->buffer. */
864 const gdb_byte *buffer;
865
866 /* The end of the buffer. */
867 const gdb_byte *buffer_end;
868
869 /* The abbreviation table to use when reading the DIEs. */
870 struct abbrev_table *abbrev_table;
871 };
872
873 /* A subclass of die_reader_specs that holds storage and has complex
874 constructor and destructor behavior. */
875
876 class cutu_reader : public die_reader_specs
877 {
878 public:
879
880 cutu_reader (struct dwarf2_per_cu_data *this_cu,
881 struct abbrev_table *abbrev_table,
882 int use_existing_cu,
883 bool skip_partial);
884
885 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
886 struct dwarf2_cu *parent_cu = nullptr,
887 struct dwo_file *dwo_file = nullptr);
888
889 DISABLE_COPY_AND_ASSIGN (cutu_reader);
890
891 const gdb_byte *info_ptr = nullptr;
892 struct die_info *comp_unit_die = nullptr;
893 bool dummy_p = false;
894
895 /* Release the new CU, putting it on the chain. This cannot be done
896 for dummy CUs. */
897 void keep ();
898
899 private:
900 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
901 int use_existing_cu);
902
903 struct dwarf2_per_cu_data *m_this_cu;
904 std::unique_ptr<dwarf2_cu> m_new_cu;
905
906 /* The ordinary abbreviation table. */
907 abbrev_table_up m_abbrev_table_holder;
908
909 /* The DWO abbreviation table. */
910 abbrev_table_up m_dwo_abbrev_table;
911 };
912
913 /* When we construct a partial symbol table entry we only
914 need this much information. */
915 struct partial_die_info : public allocate_on_obstack
916 {
917 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
918
919 /* Disable assign but still keep copy ctor, which is needed
920 load_partial_dies. */
921 partial_die_info& operator=(const partial_die_info& rhs) = delete;
922
923 /* Adjust the partial die before generating a symbol for it. This
924 function may set the is_external flag or change the DIE's
925 name. */
926 void fixup (struct dwarf2_cu *cu);
927
928 /* Read a minimal amount of information into the minimal die
929 structure. */
930 const gdb_byte *read (const struct die_reader_specs *reader,
931 const struct abbrev_info &abbrev,
932 const gdb_byte *info_ptr);
933
934 /* Offset of this DIE. */
935 const sect_offset sect_off;
936
937 /* DWARF-2 tag for this DIE. */
938 const ENUM_BITFIELD(dwarf_tag) tag : 16;
939
940 /* Assorted flags describing the data found in this DIE. */
941 const unsigned int has_children : 1;
942
943 unsigned int is_external : 1;
944 unsigned int is_declaration : 1;
945 unsigned int has_type : 1;
946 unsigned int has_specification : 1;
947 unsigned int has_pc_info : 1;
948 unsigned int may_be_inlined : 1;
949
950 /* This DIE has been marked DW_AT_main_subprogram. */
951 unsigned int main_subprogram : 1;
952
953 /* Flag set if the SCOPE field of this structure has been
954 computed. */
955 unsigned int scope_set : 1;
956
957 /* Flag set if the DIE has a byte_size attribute. */
958 unsigned int has_byte_size : 1;
959
960 /* Flag set if the DIE has a DW_AT_const_value attribute. */
961 unsigned int has_const_value : 1;
962
963 /* Flag set if any of the DIE's children are template arguments. */
964 unsigned int has_template_arguments : 1;
965
966 /* Flag set if fixup has been called on this die. */
967 unsigned int fixup_called : 1;
968
969 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
970 unsigned int is_dwz : 1;
971
972 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
973 unsigned int spec_is_dwz : 1;
974
975 /* The name of this DIE. Normally the value of DW_AT_name, but
976 sometimes a default name for unnamed DIEs. */
977 const char *name = nullptr;
978
979 /* The linkage name, if present. */
980 const char *linkage_name = nullptr;
981
982 /* The scope to prepend to our children. This is generally
983 allocated on the comp_unit_obstack, so will disappear
984 when this compilation unit leaves the cache. */
985 const char *scope = nullptr;
986
987 /* Some data associated with the partial DIE. The tag determines
988 which field is live. */
989 union
990 {
991 /* The location description associated with this DIE, if any. */
992 struct dwarf_block *locdesc;
993 /* The offset of an import, for DW_TAG_imported_unit. */
994 sect_offset sect_off;
995 } d {};
996
997 /* If HAS_PC_INFO, the PC range associated with this DIE. */
998 CORE_ADDR lowpc = 0;
999 CORE_ADDR highpc = 0;
1000
1001 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1002 DW_AT_sibling, if any. */
1003 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1004 could return DW_AT_sibling values to its caller load_partial_dies. */
1005 const gdb_byte *sibling = nullptr;
1006
1007 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1008 DW_AT_specification (or DW_AT_abstract_origin or
1009 DW_AT_extension). */
1010 sect_offset spec_offset {};
1011
1012 /* Pointers to this DIE's parent, first child, and next sibling,
1013 if any. */
1014 struct partial_die_info *die_parent = nullptr;
1015 struct partial_die_info *die_child = nullptr;
1016 struct partial_die_info *die_sibling = nullptr;
1017
1018 friend struct partial_die_info *
1019 dwarf2_cu::find_partial_die (sect_offset sect_off);
1020
1021 private:
1022 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1023 partial_die_info (sect_offset sect_off)
1024 : partial_die_info (sect_off, DW_TAG_padding, 0)
1025 {
1026 }
1027
1028 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1029 int has_children_)
1030 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1031 {
1032 is_external = 0;
1033 is_declaration = 0;
1034 has_type = 0;
1035 has_specification = 0;
1036 has_pc_info = 0;
1037 may_be_inlined = 0;
1038 main_subprogram = 0;
1039 scope_set = 0;
1040 has_byte_size = 0;
1041 has_const_value = 0;
1042 has_template_arguments = 0;
1043 fixup_called = 0;
1044 is_dwz = 0;
1045 spec_is_dwz = 0;
1046 }
1047 };
1048
1049 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1050 but this would require a corresponding change in unpack_field_as_long
1051 and friends. */
1052 static int bits_per_byte = 8;
1053
1054 /* When reading a variant or variant part, we track a bit more
1055 information about the field, and store it in an object of this
1056 type. */
1057
1058 struct variant_field
1059 {
1060 /* If we see a DW_TAG_variant, then this will be the discriminant
1061 value. */
1062 ULONGEST discriminant_value;
1063 /* If we see a DW_TAG_variant, then this will be set if this is the
1064 default branch. */
1065 bool default_branch;
1066 /* While reading a DW_TAG_variant_part, this will be set if this
1067 field is the discriminant. */
1068 bool is_discriminant;
1069 };
1070
1071 struct nextfield
1072 {
1073 int accessibility = 0;
1074 int virtuality = 0;
1075 /* Extra information to describe a variant or variant part. */
1076 struct variant_field variant {};
1077 struct field field {};
1078 };
1079
1080 struct fnfieldlist
1081 {
1082 const char *name = nullptr;
1083 std::vector<struct fn_field> fnfields;
1084 };
1085
1086 /* The routines that read and process dies for a C struct or C++ class
1087 pass lists of data member fields and lists of member function fields
1088 in an instance of a field_info structure, as defined below. */
1089 struct field_info
1090 {
1091 /* List of data member and baseclasses fields. */
1092 std::vector<struct nextfield> fields;
1093 std::vector<struct nextfield> baseclasses;
1094
1095 /* Set if the accessibility of one of the fields is not public. */
1096 int non_public_fields = 0;
1097
1098 /* Member function fieldlist array, contains name of possibly overloaded
1099 member function, number of overloaded member functions and a pointer
1100 to the head of the member function field chain. */
1101 std::vector<struct fnfieldlist> fnfieldlists;
1102
1103 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1104 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1105 std::vector<struct decl_field> typedef_field_list;
1106
1107 /* Nested types defined by this class and the number of elements in this
1108 list. */
1109 std::vector<struct decl_field> nested_types_list;
1110
1111 /* Return the total number of fields (including baseclasses). */
1112 int nfields () const
1113 {
1114 return fields.size () + baseclasses.size ();
1115 }
1116 };
1117
1118 /* Loaded secondary compilation units are kept in memory until they
1119 have not been referenced for the processing of this many
1120 compilation units. Set this to zero to disable caching. Cache
1121 sizes of up to at least twenty will improve startup time for
1122 typical inter-CU-reference binaries, at an obvious memory cost. */
1123 static int dwarf_max_cache_age = 5;
1124 static void
1125 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1126 struct cmd_list_element *c, const char *value)
1127 {
1128 fprintf_filtered (file, _("The upper bound on the age of cached "
1129 "DWARF compilation units is %s.\n"),
1130 value);
1131 }
1132 \f
1133 /* local function prototypes */
1134
1135 static void dwarf2_find_base_address (struct die_info *die,
1136 struct dwarf2_cu *cu);
1137
1138 static dwarf2_psymtab *create_partial_symtab
1139 (struct dwarf2_per_cu_data *per_cu, const char *name);
1140
1141 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1142 const gdb_byte *info_ptr,
1143 struct die_info *type_unit_die);
1144
1145 static void dwarf2_build_psymtabs_hard
1146 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1147
1148 static void scan_partial_symbols (struct partial_die_info *,
1149 CORE_ADDR *, CORE_ADDR *,
1150 int, struct dwarf2_cu *);
1151
1152 static void add_partial_symbol (struct partial_die_info *,
1153 struct dwarf2_cu *);
1154
1155 static void add_partial_namespace (struct partial_die_info *pdi,
1156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1157 int set_addrmap, struct dwarf2_cu *cu);
1158
1159 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1160 CORE_ADDR *highpc, int set_addrmap,
1161 struct dwarf2_cu *cu);
1162
1163 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1164 struct dwarf2_cu *cu);
1165
1166 static void add_partial_subprogram (struct partial_die_info *pdi,
1167 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1168 int need_pc, struct dwarf2_cu *cu);
1169
1170 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1171
1172 static struct partial_die_info *load_partial_dies
1173 (const struct die_reader_specs *, const gdb_byte *, int);
1174
1175 /* A pair of partial_die_info and compilation unit. */
1176 struct cu_partial_die_info
1177 {
1178 /* The compilation unit of the partial_die_info. */
1179 struct dwarf2_cu *cu;
1180 /* A partial_die_info. */
1181 struct partial_die_info *pdi;
1182
1183 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1184 : cu (cu),
1185 pdi (pdi)
1186 { /* Nothing. */ }
1187
1188 private:
1189 cu_partial_die_info () = delete;
1190 };
1191
1192 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1193 struct dwarf2_cu *);
1194
1195 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1196 struct attribute *, struct attr_abbrev *,
1197 const gdb_byte *, bool *need_reprocess);
1198
1199 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1200 struct attribute *attr);
1201
1202 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1203
1204 static sect_offset read_abbrev_offset
1205 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1206 struct dwarf2_section_info *, sect_offset);
1207
1208 static const char *read_indirect_string
1209 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1210 const struct comp_unit_head *, unsigned int *);
1211
1212 static const char *read_indirect_string_at_offset
1213 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1214
1215 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1216 const gdb_byte *,
1217 unsigned int *);
1218
1219 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1220 ULONGEST str_index);
1221
1222 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1223 ULONGEST str_index);
1224
1225 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1226
1227 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1228 struct dwarf2_cu *);
1229
1230 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1231 unsigned int);
1232
1233 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1234 struct dwarf2_cu *cu);
1235
1236 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1237
1238 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1239 struct dwarf2_cu *cu);
1240
1241 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1242
1243 static struct die_info *die_specification (struct die_info *die,
1244 struct dwarf2_cu **);
1245
1246 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1247 struct dwarf2_cu *cu);
1248
1249 static void dwarf_decode_lines (struct line_header *, const char *,
1250 struct dwarf2_cu *, dwarf2_psymtab *,
1251 CORE_ADDR, int decode_mapping);
1252
1253 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1254 const char *);
1255
1256 static struct symbol *new_symbol (struct die_info *, struct type *,
1257 struct dwarf2_cu *, struct symbol * = NULL);
1258
1259 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1260 struct dwarf2_cu *);
1261
1262 static void dwarf2_const_value_attr (const struct attribute *attr,
1263 struct type *type,
1264 const char *name,
1265 struct obstack *obstack,
1266 struct dwarf2_cu *cu, LONGEST *value,
1267 const gdb_byte **bytes,
1268 struct dwarf2_locexpr_baton **baton);
1269
1270 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1271
1272 static int need_gnat_info (struct dwarf2_cu *);
1273
1274 static struct type *die_descriptive_type (struct die_info *,
1275 struct dwarf2_cu *);
1276
1277 static void set_descriptive_type (struct type *, struct die_info *,
1278 struct dwarf2_cu *);
1279
1280 static struct type *die_containing_type (struct die_info *,
1281 struct dwarf2_cu *);
1282
1283 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1284 struct dwarf2_cu *);
1285
1286 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1287
1288 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1289
1290 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1291
1292 static char *typename_concat (struct obstack *obs, const char *prefix,
1293 const char *suffix, int physname,
1294 struct dwarf2_cu *cu);
1295
1296 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1297
1298 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1299
1300 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1301
1302 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1303
1304 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1305
1306 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1307
1308 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1309 struct dwarf2_cu *, dwarf2_psymtab *);
1310
1311 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1312 values. Keep the items ordered with increasing constraints compliance. */
1313 enum pc_bounds_kind
1314 {
1315 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1316 PC_BOUNDS_NOT_PRESENT,
1317
1318 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1319 were present but they do not form a valid range of PC addresses. */
1320 PC_BOUNDS_INVALID,
1321
1322 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1323 PC_BOUNDS_RANGES,
1324
1325 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1326 PC_BOUNDS_HIGH_LOW,
1327 };
1328
1329 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *,
1332 dwarf2_psymtab *);
1333
1334 static void get_scope_pc_bounds (struct die_info *,
1335 CORE_ADDR *, CORE_ADDR *,
1336 struct dwarf2_cu *);
1337
1338 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1339 CORE_ADDR, struct dwarf2_cu *);
1340
1341 static void dwarf2_add_field (struct field_info *, struct die_info *,
1342 struct dwarf2_cu *);
1343
1344 static void dwarf2_attach_fields_to_type (struct field_info *,
1345 struct type *, struct dwarf2_cu *);
1346
1347 static void dwarf2_add_member_fn (struct field_info *,
1348 struct die_info *, struct type *,
1349 struct dwarf2_cu *);
1350
1351 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1352 struct type *,
1353 struct dwarf2_cu *);
1354
1355 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1356
1357 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1358
1359 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1360
1361 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1362
1363 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1364
1365 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1366
1367 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1368
1369 static struct type *read_module_type (struct die_info *die,
1370 struct dwarf2_cu *cu);
1371
1372 static const char *namespace_name (struct die_info *die,
1373 int *is_anonymous, struct dwarf2_cu *);
1374
1375 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1376
1377 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1378
1379 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1380 struct dwarf2_cu *);
1381
1382 static struct die_info *read_die_and_siblings_1
1383 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1384 struct die_info *);
1385
1386 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1387 const gdb_byte *info_ptr,
1388 const gdb_byte **new_info_ptr,
1389 struct die_info *parent);
1390
1391 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1392 struct die_info **, const gdb_byte *,
1393 int);
1394
1395 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1396 struct die_info **, const gdb_byte *);
1397
1398 static void process_die (struct die_info *, struct dwarf2_cu *);
1399
1400 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1401 struct objfile *);
1402
1403 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1404
1405 static const char *dwarf2_full_name (const char *name,
1406 struct die_info *die,
1407 struct dwarf2_cu *cu);
1408
1409 static const char *dwarf2_physname (const char *name, struct die_info *die,
1410 struct dwarf2_cu *cu);
1411
1412 static struct die_info *dwarf2_extension (struct die_info *die,
1413 struct dwarf2_cu **);
1414
1415 static const char *dwarf_tag_name (unsigned int);
1416
1417 static const char *dwarf_attr_name (unsigned int);
1418
1419 static const char *dwarf_form_name (unsigned int);
1420
1421 static const char *dwarf_bool_name (unsigned int);
1422
1423 static const char *dwarf_type_encoding_name (unsigned int);
1424
1425 static struct die_info *sibling_die (struct die_info *);
1426
1427 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1428
1429 static void dump_die_for_error (struct die_info *);
1430
1431 static void dump_die_1 (struct ui_file *, int level, int max_level,
1432 struct die_info *);
1433
1434 /*static*/ void dump_die (struct die_info *, int max_level);
1435
1436 static void store_in_ref_table (struct die_info *,
1437 struct dwarf2_cu *);
1438
1439 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1440
1441 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1442
1443 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1444 const struct attribute *,
1445 struct dwarf2_cu **);
1446
1447 static struct die_info *follow_die_ref (struct die_info *,
1448 const struct attribute *,
1449 struct dwarf2_cu **);
1450
1451 static struct die_info *follow_die_sig (struct die_info *,
1452 const struct attribute *,
1453 struct dwarf2_cu **);
1454
1455 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1456 struct dwarf2_cu *);
1457
1458 static struct type *get_DW_AT_signature_type (struct die_info *,
1459 const struct attribute *,
1460 struct dwarf2_cu *);
1461
1462 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1463
1464 static void read_signatured_type (struct signatured_type *);
1465
1466 static int attr_to_dynamic_prop (const struct attribute *attr,
1467 struct die_info *die, struct dwarf2_cu *cu,
1468 struct dynamic_prop *prop, struct type *type);
1469
1470 /* memory allocation interface */
1471
1472 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1473
1474 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1475
1476 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1477
1478 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1479 struct dwarf2_loclist_baton *baton,
1480 const struct attribute *attr);
1481
1482 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1483 struct symbol *sym,
1484 struct dwarf2_cu *cu,
1485 int is_block);
1486
1487 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1488 const gdb_byte *info_ptr,
1489 struct abbrev_info *abbrev);
1490
1491 static hashval_t partial_die_hash (const void *item);
1492
1493 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1494
1495 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1496 (sect_offset sect_off, unsigned int offset_in_dwz,
1497 struct dwarf2_per_objfile *dwarf2_per_objfile);
1498
1499 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1500 struct die_info *comp_unit_die,
1501 enum language pretend_language);
1502
1503 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1504
1505 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1506
1507 static struct type *set_die_type (struct die_info *, struct type *,
1508 struct dwarf2_cu *);
1509
1510 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1511
1512 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1513
1514 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1515 enum language);
1516
1517 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1518 enum language);
1519
1520 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1521 enum language);
1522
1523 static void dwarf2_add_dependence (struct dwarf2_cu *,
1524 struct dwarf2_per_cu_data *);
1525
1526 static void dwarf2_mark (struct dwarf2_cu *);
1527
1528 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1529
1530 static struct type *get_die_type_at_offset (sect_offset,
1531 struct dwarf2_per_cu_data *);
1532
1533 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1534
1535 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1536 enum language pretend_language);
1537
1538 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1539
1540 /* Class, the destructor of which frees all allocated queue entries. This
1541 will only have work to do if an error was thrown while processing the
1542 dwarf. If no error was thrown then the queue entries should have all
1543 been processed, and freed, as we went along. */
1544
1545 class dwarf2_queue_guard
1546 {
1547 public:
1548 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1549 : m_per_objfile (per_objfile)
1550 {
1551 }
1552
1553 /* Free any entries remaining on the queue. There should only be
1554 entries left if we hit an error while processing the dwarf. */
1555 ~dwarf2_queue_guard ()
1556 {
1557 /* Ensure that no memory is allocated by the queue. */
1558 std::queue<dwarf2_queue_item> empty;
1559 std::swap (m_per_objfile->queue, empty);
1560 }
1561
1562 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1563
1564 private:
1565 dwarf2_per_objfile *m_per_objfile;
1566 };
1567
1568 dwarf2_queue_item::~dwarf2_queue_item ()
1569 {
1570 /* Anything still marked queued is likely to be in an
1571 inconsistent state, so discard it. */
1572 if (per_cu->queued)
1573 {
1574 if (per_cu->cu != NULL)
1575 free_one_cached_comp_unit (per_cu);
1576 per_cu->queued = 0;
1577 }
1578 }
1579
1580 /* The return type of find_file_and_directory. Note, the enclosed
1581 string pointers are only valid while this object is valid. */
1582
1583 struct file_and_directory
1584 {
1585 /* The filename. This is never NULL. */
1586 const char *name;
1587
1588 /* The compilation directory. NULL if not known. If we needed to
1589 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1590 points directly to the DW_AT_comp_dir string attribute owned by
1591 the obstack that owns the DIE. */
1592 const char *comp_dir;
1593
1594 /* If we needed to build a new string for comp_dir, this is what
1595 owns the storage. */
1596 std::string comp_dir_storage;
1597 };
1598
1599 static file_and_directory find_file_and_directory (struct die_info *die,
1600 struct dwarf2_cu *cu);
1601
1602 static htab_up allocate_signatured_type_table ();
1603
1604 static htab_up allocate_dwo_unit_table ();
1605
1606 static struct dwo_unit *lookup_dwo_unit_in_dwp
1607 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1608 struct dwp_file *dwp_file, const char *comp_dir,
1609 ULONGEST signature, int is_debug_types);
1610
1611 static struct dwp_file *get_dwp_file
1612 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1613
1614 static struct dwo_unit *lookup_dwo_comp_unit
1615 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1616
1617 static struct dwo_unit *lookup_dwo_type_unit
1618 (struct signatured_type *, const char *, const char *);
1619
1620 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1621
1622 /* A unique pointer to a dwo_file. */
1623
1624 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1625
1626 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1627
1628 static void check_producer (struct dwarf2_cu *cu);
1629
1630 static void free_line_header_voidp (void *arg);
1631 \f
1632 /* Various complaints about symbol reading that don't abort the process. */
1633
1634 static void
1635 dwarf2_debug_line_missing_file_complaint (void)
1636 {
1637 complaint (_(".debug_line section has line data without a file"));
1638 }
1639
1640 static void
1641 dwarf2_debug_line_missing_end_sequence_complaint (void)
1642 {
1643 complaint (_(".debug_line section has line "
1644 "program sequence without an end"));
1645 }
1646
1647 static void
1648 dwarf2_complex_location_expr_complaint (void)
1649 {
1650 complaint (_("location expression too complex"));
1651 }
1652
1653 static void
1654 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1655 int arg3)
1656 {
1657 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1658 arg1, arg2, arg3);
1659 }
1660
1661 static void
1662 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1663 {
1664 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1665 arg1, arg2);
1666 }
1667
1668 /* Hash function for line_header_hash. */
1669
1670 static hashval_t
1671 line_header_hash (const struct line_header *ofs)
1672 {
1673 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1674 }
1675
1676 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1677
1678 static hashval_t
1679 line_header_hash_voidp (const void *item)
1680 {
1681 const struct line_header *ofs = (const struct line_header *) item;
1682
1683 return line_header_hash (ofs);
1684 }
1685
1686 /* Equality function for line_header_hash. */
1687
1688 static int
1689 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1690 {
1691 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1692 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1693
1694 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1695 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1696 }
1697
1698 \f
1699
1700 /* See declaration. */
1701
1702 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1703 const dwarf2_debug_sections *names,
1704 bool can_copy_)
1705 : objfile (objfile_),
1706 can_copy (can_copy_)
1707 {
1708 if (names == NULL)
1709 names = &dwarf2_elf_names;
1710
1711 bfd *obfd = objfile->obfd;
1712
1713 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1714 locate_sections (obfd, sec, *names);
1715 }
1716
1717 dwarf2_per_objfile::~dwarf2_per_objfile ()
1718 {
1719 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1720 free_cached_comp_units ();
1721
1722 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1723 per_cu->imported_symtabs_free ();
1724
1725 for (signatured_type *sig_type : all_type_units)
1726 sig_type->per_cu.imported_symtabs_free ();
1727
1728 /* Everything else should be on the objfile obstack. */
1729 }
1730
1731 /* See declaration. */
1732
1733 void
1734 dwarf2_per_objfile::free_cached_comp_units ()
1735 {
1736 dwarf2_per_cu_data *per_cu = read_in_chain;
1737 dwarf2_per_cu_data **last_chain = &read_in_chain;
1738 while (per_cu != NULL)
1739 {
1740 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1741
1742 delete per_cu->cu;
1743 *last_chain = next_cu;
1744 per_cu = next_cu;
1745 }
1746 }
1747
1748 /* A helper class that calls free_cached_comp_units on
1749 destruction. */
1750
1751 class free_cached_comp_units
1752 {
1753 public:
1754
1755 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1756 : m_per_objfile (per_objfile)
1757 {
1758 }
1759
1760 ~free_cached_comp_units ()
1761 {
1762 m_per_objfile->free_cached_comp_units ();
1763 }
1764
1765 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1766
1767 private:
1768
1769 dwarf2_per_objfile *m_per_objfile;
1770 };
1771
1772 /* Try to locate the sections we need for DWARF 2 debugging
1773 information and return true if we have enough to do something.
1774 NAMES points to the dwarf2 section names, or is NULL if the standard
1775 ELF names are used. CAN_COPY is true for formats where symbol
1776 interposition is possible and so symbol values must follow copy
1777 relocation rules. */
1778
1779 int
1780 dwarf2_has_info (struct objfile *objfile,
1781 const struct dwarf2_debug_sections *names,
1782 bool can_copy)
1783 {
1784 if (objfile->flags & OBJF_READNEVER)
1785 return 0;
1786
1787 struct dwarf2_per_objfile *dwarf2_per_objfile
1788 = get_dwarf2_per_objfile (objfile);
1789
1790 if (dwarf2_per_objfile == NULL)
1791 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1792 names,
1793 can_copy);
1794
1795 return (!dwarf2_per_objfile->info.is_virtual
1796 && dwarf2_per_objfile->info.s.section != NULL
1797 && !dwarf2_per_objfile->abbrev.is_virtual
1798 && dwarf2_per_objfile->abbrev.s.section != NULL);
1799 }
1800
1801 /* When loading sections, we look either for uncompressed section or for
1802 compressed section names. */
1803
1804 static int
1805 section_is_p (const char *section_name,
1806 const struct dwarf2_section_names *names)
1807 {
1808 if (names->normal != NULL
1809 && strcmp (section_name, names->normal) == 0)
1810 return 1;
1811 if (names->compressed != NULL
1812 && strcmp (section_name, names->compressed) == 0)
1813 return 1;
1814 return 0;
1815 }
1816
1817 /* See declaration. */
1818
1819 void
1820 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1821 const dwarf2_debug_sections &names)
1822 {
1823 flagword aflag = bfd_section_flags (sectp);
1824
1825 if ((aflag & SEC_HAS_CONTENTS) == 0)
1826 {
1827 }
1828 else if (elf_section_data (sectp)->this_hdr.sh_size
1829 > bfd_get_file_size (abfd))
1830 {
1831 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1832 warning (_("Discarding section %s which has a section size (%s"
1833 ") larger than the file size [in module %s]"),
1834 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1835 bfd_get_filename (abfd));
1836 }
1837 else if (section_is_p (sectp->name, &names.info))
1838 {
1839 this->info.s.section = sectp;
1840 this->info.size = bfd_section_size (sectp);
1841 }
1842 else if (section_is_p (sectp->name, &names.abbrev))
1843 {
1844 this->abbrev.s.section = sectp;
1845 this->abbrev.size = bfd_section_size (sectp);
1846 }
1847 else if (section_is_p (sectp->name, &names.line))
1848 {
1849 this->line.s.section = sectp;
1850 this->line.size = bfd_section_size (sectp);
1851 }
1852 else if (section_is_p (sectp->name, &names.loc))
1853 {
1854 this->loc.s.section = sectp;
1855 this->loc.size = bfd_section_size (sectp);
1856 }
1857 else if (section_is_p (sectp->name, &names.loclists))
1858 {
1859 this->loclists.s.section = sectp;
1860 this->loclists.size = bfd_section_size (sectp);
1861 }
1862 else if (section_is_p (sectp->name, &names.macinfo))
1863 {
1864 this->macinfo.s.section = sectp;
1865 this->macinfo.size = bfd_section_size (sectp);
1866 }
1867 else if (section_is_p (sectp->name, &names.macro))
1868 {
1869 this->macro.s.section = sectp;
1870 this->macro.size = bfd_section_size (sectp);
1871 }
1872 else if (section_is_p (sectp->name, &names.str))
1873 {
1874 this->str.s.section = sectp;
1875 this->str.size = bfd_section_size (sectp);
1876 }
1877 else if (section_is_p (sectp->name, &names.str_offsets))
1878 {
1879 this->str_offsets.s.section = sectp;
1880 this->str_offsets.size = bfd_section_size (sectp);
1881 }
1882 else if (section_is_p (sectp->name, &names.line_str))
1883 {
1884 this->line_str.s.section = sectp;
1885 this->line_str.size = bfd_section_size (sectp);
1886 }
1887 else if (section_is_p (sectp->name, &names.addr))
1888 {
1889 this->addr.s.section = sectp;
1890 this->addr.size = bfd_section_size (sectp);
1891 }
1892 else if (section_is_p (sectp->name, &names.frame))
1893 {
1894 this->frame.s.section = sectp;
1895 this->frame.size = bfd_section_size (sectp);
1896 }
1897 else if (section_is_p (sectp->name, &names.eh_frame))
1898 {
1899 this->eh_frame.s.section = sectp;
1900 this->eh_frame.size = bfd_section_size (sectp);
1901 }
1902 else if (section_is_p (sectp->name, &names.ranges))
1903 {
1904 this->ranges.s.section = sectp;
1905 this->ranges.size = bfd_section_size (sectp);
1906 }
1907 else if (section_is_p (sectp->name, &names.rnglists))
1908 {
1909 this->rnglists.s.section = sectp;
1910 this->rnglists.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.types))
1913 {
1914 struct dwarf2_section_info type_section;
1915
1916 memset (&type_section, 0, sizeof (type_section));
1917 type_section.s.section = sectp;
1918 type_section.size = bfd_section_size (sectp);
1919
1920 this->types.push_back (type_section);
1921 }
1922 else if (section_is_p (sectp->name, &names.gdb_index))
1923 {
1924 this->gdb_index.s.section = sectp;
1925 this->gdb_index.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.debug_names))
1928 {
1929 this->debug_names.s.section = sectp;
1930 this->debug_names.size = bfd_section_size (sectp);
1931 }
1932 else if (section_is_p (sectp->name, &names.debug_aranges))
1933 {
1934 this->debug_aranges.s.section = sectp;
1935 this->debug_aranges.size = bfd_section_size (sectp);
1936 }
1937
1938 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1939 && bfd_section_vma (sectp) == 0)
1940 this->has_section_at_zero = true;
1941 }
1942
1943 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1944 SECTION_NAME. */
1945
1946 void
1947 dwarf2_get_section_info (struct objfile *objfile,
1948 enum dwarf2_section_enum sect,
1949 asection **sectp, const gdb_byte **bufp,
1950 bfd_size_type *sizep)
1951 {
1952 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1953 struct dwarf2_section_info *info;
1954
1955 /* We may see an objfile without any DWARF, in which case we just
1956 return nothing. */
1957 if (data == NULL)
1958 {
1959 *sectp = NULL;
1960 *bufp = NULL;
1961 *sizep = 0;
1962 return;
1963 }
1964 switch (sect)
1965 {
1966 case DWARF2_DEBUG_FRAME:
1967 info = &data->frame;
1968 break;
1969 case DWARF2_EH_FRAME:
1970 info = &data->eh_frame;
1971 break;
1972 default:
1973 gdb_assert_not_reached ("unexpected section");
1974 }
1975
1976 info->read (objfile);
1977
1978 *sectp = info->get_bfd_section ();
1979 *bufp = info->buffer;
1980 *sizep = info->size;
1981 }
1982
1983 /* A helper function to find the sections for a .dwz file. */
1984
1985 static void
1986 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1987 {
1988 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1989
1990 /* Note that we only support the standard ELF names, because .dwz
1991 is ELF-only (at the time of writing). */
1992 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1993 {
1994 dwz_file->abbrev.s.section = sectp;
1995 dwz_file->abbrev.size = bfd_section_size (sectp);
1996 }
1997 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1998 {
1999 dwz_file->info.s.section = sectp;
2000 dwz_file->info.size = bfd_section_size (sectp);
2001 }
2002 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2003 {
2004 dwz_file->str.s.section = sectp;
2005 dwz_file->str.size = bfd_section_size (sectp);
2006 }
2007 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2008 {
2009 dwz_file->line.s.section = sectp;
2010 dwz_file->line.size = bfd_section_size (sectp);
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2013 {
2014 dwz_file->macro.s.section = sectp;
2015 dwz_file->macro.size = bfd_section_size (sectp);
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2018 {
2019 dwz_file->gdb_index.s.section = sectp;
2020 dwz_file->gdb_index.size = bfd_section_size (sectp);
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2023 {
2024 dwz_file->debug_names.s.section = sectp;
2025 dwz_file->debug_names.size = bfd_section_size (sectp);
2026 }
2027 }
2028
2029 /* See dwarf2read.h. */
2030
2031 struct dwz_file *
2032 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2033 {
2034 const char *filename;
2035 bfd_size_type buildid_len_arg;
2036 size_t buildid_len;
2037 bfd_byte *buildid;
2038
2039 if (dwarf2_per_objfile->dwz_file != NULL)
2040 return dwarf2_per_objfile->dwz_file.get ();
2041
2042 bfd_set_error (bfd_error_no_error);
2043 gdb::unique_xmalloc_ptr<char> data
2044 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2045 &buildid_len_arg, &buildid));
2046 if (data == NULL)
2047 {
2048 if (bfd_get_error () == bfd_error_no_error)
2049 return NULL;
2050 error (_("could not read '.gnu_debugaltlink' section: %s"),
2051 bfd_errmsg (bfd_get_error ()));
2052 }
2053
2054 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2055
2056 buildid_len = (size_t) buildid_len_arg;
2057
2058 filename = data.get ();
2059
2060 std::string abs_storage;
2061 if (!IS_ABSOLUTE_PATH (filename))
2062 {
2063 gdb::unique_xmalloc_ptr<char> abs
2064 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2065
2066 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2067 filename = abs_storage.c_str ();
2068 }
2069
2070 /* First try the file name given in the section. If that doesn't
2071 work, try to use the build-id instead. */
2072 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2073 if (dwz_bfd != NULL)
2074 {
2075 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2076 dwz_bfd.reset (nullptr);
2077 }
2078
2079 if (dwz_bfd == NULL)
2080 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2081
2082 if (dwz_bfd == nullptr)
2083 {
2084 gdb::unique_xmalloc_ptr<char> alt_filename;
2085 const char *origname = dwarf2_per_objfile->objfile->original_name;
2086
2087 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2088 buildid_len,
2089 origname,
2090 &alt_filename));
2091
2092 if (fd.get () >= 0)
2093 {
2094 /* File successfully retrieved from server. */
2095 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2096
2097 if (dwz_bfd == nullptr)
2098 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2099 alt_filename.get ());
2100 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2101 dwz_bfd.reset (nullptr);
2102 }
2103 }
2104
2105 if (dwz_bfd == NULL)
2106 error (_("could not find '.gnu_debugaltlink' file for %s"),
2107 objfile_name (dwarf2_per_objfile->objfile));
2108
2109 std::unique_ptr<struct dwz_file> result
2110 (new struct dwz_file (std::move (dwz_bfd)));
2111
2112 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2113 result.get ());
2114
2115 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2116 result->dwz_bfd.get ());
2117 dwarf2_per_objfile->dwz_file = std::move (result);
2118 return dwarf2_per_objfile->dwz_file.get ();
2119 }
2120 \f
2121 /* DWARF quick_symbols_functions support. */
2122
2123 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2124 unique line tables, so we maintain a separate table of all .debug_line
2125 derived entries to support the sharing.
2126 All the quick functions need is the list of file names. We discard the
2127 line_header when we're done and don't need to record it here. */
2128 struct quick_file_names
2129 {
2130 /* The data used to construct the hash key. */
2131 struct stmt_list_hash hash;
2132
2133 /* The number of entries in file_names, real_names. */
2134 unsigned int num_file_names;
2135
2136 /* The file names from the line table, after being run through
2137 file_full_name. */
2138 const char **file_names;
2139
2140 /* The file names from the line table after being run through
2141 gdb_realpath. These are computed lazily. */
2142 const char **real_names;
2143 };
2144
2145 /* When using the index (and thus not using psymtabs), each CU has an
2146 object of this type. This is used to hold information needed by
2147 the various "quick" methods. */
2148 struct dwarf2_per_cu_quick_data
2149 {
2150 /* The file table. This can be NULL if there was no file table
2151 or it's currently not read in.
2152 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2153 struct quick_file_names *file_names;
2154
2155 /* The corresponding symbol table. This is NULL if symbols for this
2156 CU have not yet been read. */
2157 struct compunit_symtab *compunit_symtab;
2158
2159 /* A temporary mark bit used when iterating over all CUs in
2160 expand_symtabs_matching. */
2161 unsigned int mark : 1;
2162
2163 /* True if we've tried to read the file table and found there isn't one.
2164 There will be no point in trying to read it again next time. */
2165 unsigned int no_file_data : 1;
2166 };
2167
2168 /* Utility hash function for a stmt_list_hash. */
2169
2170 static hashval_t
2171 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2172 {
2173 hashval_t v = 0;
2174
2175 if (stmt_list_hash->dwo_unit != NULL)
2176 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2177 v += to_underlying (stmt_list_hash->line_sect_off);
2178 return v;
2179 }
2180
2181 /* Utility equality function for a stmt_list_hash. */
2182
2183 static int
2184 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2185 const struct stmt_list_hash *rhs)
2186 {
2187 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2188 return 0;
2189 if (lhs->dwo_unit != NULL
2190 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2191 return 0;
2192
2193 return lhs->line_sect_off == rhs->line_sect_off;
2194 }
2195
2196 /* Hash function for a quick_file_names. */
2197
2198 static hashval_t
2199 hash_file_name_entry (const void *e)
2200 {
2201 const struct quick_file_names *file_data
2202 = (const struct quick_file_names *) e;
2203
2204 return hash_stmt_list_entry (&file_data->hash);
2205 }
2206
2207 /* Equality function for a quick_file_names. */
2208
2209 static int
2210 eq_file_name_entry (const void *a, const void *b)
2211 {
2212 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2213 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2214
2215 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2216 }
2217
2218 /* Delete function for a quick_file_names. */
2219
2220 static void
2221 delete_file_name_entry (void *e)
2222 {
2223 struct quick_file_names *file_data = (struct quick_file_names *) e;
2224 int i;
2225
2226 for (i = 0; i < file_data->num_file_names; ++i)
2227 {
2228 xfree ((void*) file_data->file_names[i]);
2229 if (file_data->real_names)
2230 xfree ((void*) file_data->real_names[i]);
2231 }
2232
2233 /* The space for the struct itself lives on objfile_obstack,
2234 so we don't free it here. */
2235 }
2236
2237 /* Create a quick_file_names hash table. */
2238
2239 static htab_up
2240 create_quick_file_names_table (unsigned int nr_initial_entries)
2241 {
2242 return htab_up (htab_create_alloc (nr_initial_entries,
2243 hash_file_name_entry, eq_file_name_entry,
2244 delete_file_name_entry, xcalloc, xfree));
2245 }
2246
2247 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2248 have to be created afterwards. You should call age_cached_comp_units after
2249 processing PER_CU->CU. dw2_setup must have been already called. */
2250
2251 static void
2252 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2253 {
2254 if (per_cu->is_debug_types)
2255 load_full_type_unit (per_cu);
2256 else
2257 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2258
2259 if (per_cu->cu == NULL)
2260 return; /* Dummy CU. */
2261
2262 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2263 }
2264
2265 /* Read in the symbols for PER_CU. */
2266
2267 static void
2268 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2269 {
2270 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2271
2272 /* Skip type_unit_groups, reading the type units they contain
2273 is handled elsewhere. */
2274 if (per_cu->type_unit_group_p ())
2275 return;
2276
2277 /* The destructor of dwarf2_queue_guard frees any entries left on
2278 the queue. After this point we're guaranteed to leave this function
2279 with the dwarf queue empty. */
2280 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2281
2282 if (dwarf2_per_objfile->using_index
2283 ? per_cu->v.quick->compunit_symtab == NULL
2284 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2285 {
2286 queue_comp_unit (per_cu, language_minimal);
2287 load_cu (per_cu, skip_partial);
2288
2289 /* If we just loaded a CU from a DWO, and we're working with an index
2290 that may badly handle TUs, load all the TUs in that DWO as well.
2291 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2292 if (!per_cu->is_debug_types
2293 && per_cu->cu != NULL
2294 && per_cu->cu->dwo_unit != NULL
2295 && dwarf2_per_objfile->index_table != NULL
2296 && dwarf2_per_objfile->index_table->version <= 7
2297 /* DWP files aren't supported yet. */
2298 && get_dwp_file (dwarf2_per_objfile) == NULL)
2299 queue_and_load_all_dwo_tus (per_cu);
2300 }
2301
2302 process_queue (dwarf2_per_objfile);
2303
2304 /* Age the cache, releasing compilation units that have not
2305 been used recently. */
2306 age_cached_comp_units (dwarf2_per_objfile);
2307 }
2308
2309 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2310 the objfile from which this CU came. Returns the resulting symbol
2311 table. */
2312
2313 static struct compunit_symtab *
2314 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2315 {
2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2317
2318 gdb_assert (dwarf2_per_objfile->using_index);
2319 if (!per_cu->v.quick->compunit_symtab)
2320 {
2321 free_cached_comp_units freer (dwarf2_per_objfile);
2322 scoped_restore decrementer = increment_reading_symtab ();
2323 dw2_do_instantiate_symtab (per_cu, skip_partial);
2324 process_cu_includes (dwarf2_per_objfile);
2325 }
2326
2327 return per_cu->v.quick->compunit_symtab;
2328 }
2329
2330 /* See declaration. */
2331
2332 dwarf2_per_cu_data *
2333 dwarf2_per_objfile::get_cutu (int index)
2334 {
2335 if (index >= this->all_comp_units.size ())
2336 {
2337 index -= this->all_comp_units.size ();
2338 gdb_assert (index < this->all_type_units.size ());
2339 return &this->all_type_units[index]->per_cu;
2340 }
2341
2342 return this->all_comp_units[index];
2343 }
2344
2345 /* See declaration. */
2346
2347 dwarf2_per_cu_data *
2348 dwarf2_per_objfile::get_cu (int index)
2349 {
2350 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2351
2352 return this->all_comp_units[index];
2353 }
2354
2355 /* See declaration. */
2356
2357 signatured_type *
2358 dwarf2_per_objfile::get_tu (int index)
2359 {
2360 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2361
2362 return this->all_type_units[index];
2363 }
2364
2365 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2366 objfile_obstack, and constructed with the specified field
2367 values. */
2368
2369 static dwarf2_per_cu_data *
2370 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 struct objfile *objfile = dwarf2_per_objfile->objfile;
2376 dwarf2_per_cu_data *the_cu
2377 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2378 struct dwarf2_per_cu_data);
2379 the_cu->sect_off = sect_off;
2380 the_cu->length = length;
2381 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2382 the_cu->section = section;
2383 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2384 struct dwarf2_per_cu_quick_data);
2385 the_cu->is_dwz = is_dwz;
2386 return the_cu;
2387 }
2388
2389 /* A helper for create_cus_from_index that handles a given list of
2390 CUs. */
2391
2392 static void
2393 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2394 const gdb_byte *cu_list, offset_type n_elements,
2395 struct dwarf2_section_info *section,
2396 int is_dwz)
2397 {
2398 for (offset_type i = 0; i < n_elements; i += 2)
2399 {
2400 gdb_static_assert (sizeof (ULONGEST) >= 8);
2401
2402 sect_offset sect_off
2403 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2404 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2405 cu_list += 2 * 8;
2406
2407 dwarf2_per_cu_data *per_cu
2408 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2409 sect_off, length);
2410 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2411 }
2412 }
2413
2414 /* Read the CU list from the mapped index, and use it to create all
2415 the CU objects for this objfile. */
2416
2417 static void
2418 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2419 const gdb_byte *cu_list, offset_type cu_list_elements,
2420 const gdb_byte *dwz_list, offset_type dwz_elements)
2421 {
2422 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2423 dwarf2_per_objfile->all_comp_units.reserve
2424 ((cu_list_elements + dwz_elements) / 2);
2425
2426 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2427 &dwarf2_per_objfile->info, 0);
2428
2429 if (dwz_elements == 0)
2430 return;
2431
2432 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2433 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2434 &dwz->info, 1);
2435 }
2436
2437 /* Create the signatured type hash table from the index. */
2438
2439 static void
2440 create_signatured_type_table_from_index
2441 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2442 struct dwarf2_section_info *section,
2443 const gdb_byte *bytes,
2444 offset_type elements)
2445 {
2446 struct objfile *objfile = dwarf2_per_objfile->objfile;
2447
2448 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2449 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2450
2451 htab_up sig_types_hash = allocate_signatured_type_table ();
2452
2453 for (offset_type i = 0; i < elements; i += 3)
2454 {
2455 struct signatured_type *sig_type;
2456 ULONGEST signature;
2457 void **slot;
2458 cu_offset type_offset_in_tu;
2459
2460 gdb_static_assert (sizeof (ULONGEST) >= 8);
2461 sect_offset sect_off
2462 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2463 type_offset_in_tu
2464 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2465 BFD_ENDIAN_LITTLE);
2466 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2467 bytes += 3 * 8;
2468
2469 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2470 struct signatured_type);
2471 sig_type->signature = signature;
2472 sig_type->type_offset_in_tu = type_offset_in_tu;
2473 sig_type->per_cu.is_debug_types = 1;
2474 sig_type->per_cu.section = section;
2475 sig_type->per_cu.sect_off = sect_off;
2476 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2477 sig_type->per_cu.v.quick
2478 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2479 struct dwarf2_per_cu_quick_data);
2480
2481 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2482 *slot = sig_type;
2483
2484 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2485 }
2486
2487 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2488 }
2489
2490 /* Create the signatured type hash table from .debug_names. */
2491
2492 static void
2493 create_signatured_type_table_from_debug_names
2494 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2495 const mapped_debug_names &map,
2496 struct dwarf2_section_info *section,
2497 struct dwarf2_section_info *abbrev_section)
2498 {
2499 struct objfile *objfile = dwarf2_per_objfile->objfile;
2500
2501 section->read (objfile);
2502 abbrev_section->read (objfile);
2503
2504 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2505 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2506
2507 htab_up sig_types_hash = allocate_signatured_type_table ();
2508
2509 for (uint32_t i = 0; i < map.tu_count; ++i)
2510 {
2511 struct signatured_type *sig_type;
2512 void **slot;
2513
2514 sect_offset sect_off
2515 = (sect_offset) (extract_unsigned_integer
2516 (map.tu_table_reordered + i * map.offset_size,
2517 map.offset_size,
2518 map.dwarf5_byte_order));
2519
2520 comp_unit_head cu_header;
2521 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2522 abbrev_section,
2523 section->buffer + to_underlying (sect_off),
2524 rcuh_kind::TYPE);
2525
2526 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2527 struct signatured_type);
2528 sig_type->signature = cu_header.signature;
2529 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2530 sig_type->per_cu.is_debug_types = 1;
2531 sig_type->per_cu.section = section;
2532 sig_type->per_cu.sect_off = sect_off;
2533 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2534 sig_type->per_cu.v.quick
2535 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2536 struct dwarf2_per_cu_quick_data);
2537
2538 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2539 *slot = sig_type;
2540
2541 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2542 }
2543
2544 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2545 }
2546
2547 /* Read the address map data from the mapped index, and use it to
2548 populate the objfile's psymtabs_addrmap. */
2549
2550 static void
2551 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2552 struct mapped_index *index)
2553 {
2554 struct objfile *objfile = dwarf2_per_objfile->objfile;
2555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2556 const gdb_byte *iter, *end;
2557 struct addrmap *mutable_map;
2558 CORE_ADDR baseaddr;
2559
2560 auto_obstack temp_obstack;
2561
2562 mutable_map = addrmap_create_mutable (&temp_obstack);
2563
2564 iter = index->address_table.data ();
2565 end = iter + index->address_table.size ();
2566
2567 baseaddr = objfile->text_section_offset ();
2568
2569 while (iter < end)
2570 {
2571 ULONGEST hi, lo, cu_index;
2572 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2573 iter += 8;
2574 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2575 iter += 8;
2576 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2577 iter += 4;
2578
2579 if (lo > hi)
2580 {
2581 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2582 hex_string (lo), hex_string (hi));
2583 continue;
2584 }
2585
2586 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2587 {
2588 complaint (_(".gdb_index address table has invalid CU number %u"),
2589 (unsigned) cu_index);
2590 continue;
2591 }
2592
2593 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2594 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2595 addrmap_set_empty (mutable_map, lo, hi - 1,
2596 dwarf2_per_objfile->get_cu (cu_index));
2597 }
2598
2599 objfile->partial_symtabs->psymtabs_addrmap
2600 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2601 }
2602
2603 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2604 populate the objfile's psymtabs_addrmap. */
2605
2606 static void
2607 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2608 struct dwarf2_section_info *section)
2609 {
2610 struct objfile *objfile = dwarf2_per_objfile->objfile;
2611 bfd *abfd = objfile->obfd;
2612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2613 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2614
2615 auto_obstack temp_obstack;
2616 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2617
2618 std::unordered_map<sect_offset,
2619 dwarf2_per_cu_data *,
2620 gdb::hash_enum<sect_offset>>
2621 debug_info_offset_to_per_cu;
2622 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2623 {
2624 const auto insertpair
2625 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2626 if (!insertpair.second)
2627 {
2628 warning (_("Section .debug_aranges in %s has duplicate "
2629 "debug_info_offset %s, ignoring .debug_aranges."),
2630 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2631 return;
2632 }
2633 }
2634
2635 section->read (objfile);
2636
2637 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2638
2639 const gdb_byte *addr = section->buffer;
2640
2641 while (addr < section->buffer + section->size)
2642 {
2643 const gdb_byte *const entry_addr = addr;
2644 unsigned int bytes_read;
2645
2646 const LONGEST entry_length = read_initial_length (abfd, addr,
2647 &bytes_read);
2648 addr += bytes_read;
2649
2650 const gdb_byte *const entry_end = addr + entry_length;
2651 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2652 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2653 if (addr + entry_length > section->buffer + section->size)
2654 {
2655 warning (_("Section .debug_aranges in %s entry at offset %s "
2656 "length %s exceeds section length %s, "
2657 "ignoring .debug_aranges."),
2658 objfile_name (objfile),
2659 plongest (entry_addr - section->buffer),
2660 plongest (bytes_read + entry_length),
2661 pulongest (section->size));
2662 return;
2663 }
2664
2665 /* The version number. */
2666 const uint16_t version = read_2_bytes (abfd, addr);
2667 addr += 2;
2668 if (version != 2)
2669 {
2670 warning (_("Section .debug_aranges in %s entry at offset %s "
2671 "has unsupported version %d, ignoring .debug_aranges."),
2672 objfile_name (objfile),
2673 plongest (entry_addr - section->buffer), version);
2674 return;
2675 }
2676
2677 const uint64_t debug_info_offset
2678 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2679 addr += offset_size;
2680 const auto per_cu_it
2681 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2682 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "debug_info_offset %s does not exists, "
2686 "ignoring .debug_aranges."),
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer),
2689 pulongest (debug_info_offset));
2690 return;
2691 }
2692 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2693
2694 const uint8_t address_size = *addr++;
2695 if (address_size < 1 || address_size > 8)
2696 {
2697 warning (_("Section .debug_aranges in %s entry at offset %s "
2698 "address_size %u is invalid, ignoring .debug_aranges."),
2699 objfile_name (objfile),
2700 plongest (entry_addr - section->buffer), address_size);
2701 return;
2702 }
2703
2704 const uint8_t segment_selector_size = *addr++;
2705 if (segment_selector_size != 0)
2706 {
2707 warning (_("Section .debug_aranges in %s entry at offset %s "
2708 "segment_selector_size %u is not supported, "
2709 "ignoring .debug_aranges."),
2710 objfile_name (objfile),
2711 plongest (entry_addr - section->buffer),
2712 segment_selector_size);
2713 return;
2714 }
2715
2716 /* Must pad to an alignment boundary that is twice the address
2717 size. It is undocumented by the DWARF standard but GCC does
2718 use it. */
2719 for (size_t padding = ((-(addr - section->buffer))
2720 & (2 * address_size - 1));
2721 padding > 0; padding--)
2722 if (*addr++ != 0)
2723 {
2724 warning (_("Section .debug_aranges in %s entry at offset %s "
2725 "padding is not zero, ignoring .debug_aranges."),
2726 objfile_name (objfile),
2727 plongest (entry_addr - section->buffer));
2728 return;
2729 }
2730
2731 for (;;)
2732 {
2733 if (addr + 2 * address_size > entry_end)
2734 {
2735 warning (_("Section .debug_aranges in %s entry at offset %s "
2736 "address list is not properly terminated, "
2737 "ignoring .debug_aranges."),
2738 objfile_name (objfile),
2739 plongest (entry_addr - section->buffer));
2740 return;
2741 }
2742 ULONGEST start = extract_unsigned_integer (addr, address_size,
2743 dwarf5_byte_order);
2744 addr += address_size;
2745 ULONGEST length = extract_unsigned_integer (addr, address_size,
2746 dwarf5_byte_order);
2747 addr += address_size;
2748 if (start == 0 && length == 0)
2749 break;
2750 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2751 {
2752 /* Symbol was eliminated due to a COMDAT group. */
2753 continue;
2754 }
2755 ULONGEST end = start + length;
2756 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2757 - baseaddr);
2758 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2759 - baseaddr);
2760 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2761 }
2762 }
2763
2764 objfile->partial_symtabs->psymtabs_addrmap
2765 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2766 }
2767
2768 /* Find a slot in the mapped index INDEX for the object named NAME.
2769 If NAME is found, set *VEC_OUT to point to the CU vector in the
2770 constant pool and return true. If NAME cannot be found, return
2771 false. */
2772
2773 static bool
2774 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2775 offset_type **vec_out)
2776 {
2777 offset_type hash;
2778 offset_type slot, step;
2779 int (*cmp) (const char *, const char *);
2780
2781 gdb::unique_xmalloc_ptr<char> without_params;
2782 if (current_language->la_language == language_cplus
2783 || current_language->la_language == language_fortran
2784 || current_language->la_language == language_d)
2785 {
2786 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2787 not contain any. */
2788
2789 if (strchr (name, '(') != NULL)
2790 {
2791 without_params = cp_remove_params (name);
2792
2793 if (without_params != NULL)
2794 name = without_params.get ();
2795 }
2796 }
2797
2798 /* Index version 4 did not support case insensitive searches. But the
2799 indices for case insensitive languages are built in lowercase, therefore
2800 simulate our NAME being searched is also lowercased. */
2801 hash = mapped_index_string_hash ((index->version == 4
2802 && case_sensitivity == case_sensitive_off
2803 ? 5 : index->version),
2804 name);
2805
2806 slot = hash & (index->symbol_table.size () - 1);
2807 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2808 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2809
2810 for (;;)
2811 {
2812 const char *str;
2813
2814 const auto &bucket = index->symbol_table[slot];
2815 if (bucket.name == 0 && bucket.vec == 0)
2816 return false;
2817
2818 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2819 if (!cmp (name, str))
2820 {
2821 *vec_out = (offset_type *) (index->constant_pool
2822 + MAYBE_SWAP (bucket.vec));
2823 return true;
2824 }
2825
2826 slot = (slot + step) & (index->symbol_table.size () - 1);
2827 }
2828 }
2829
2830 /* A helper function that reads the .gdb_index from BUFFER and fills
2831 in MAP. FILENAME is the name of the file containing the data;
2832 it is used for error reporting. DEPRECATED_OK is true if it is
2833 ok to use deprecated sections.
2834
2835 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2836 out parameters that are filled in with information about the CU and
2837 TU lists in the section.
2838
2839 Returns true if all went well, false otherwise. */
2840
2841 static bool
2842 read_gdb_index_from_buffer (struct objfile *objfile,
2843 const char *filename,
2844 bool deprecated_ok,
2845 gdb::array_view<const gdb_byte> buffer,
2846 struct mapped_index *map,
2847 const gdb_byte **cu_list,
2848 offset_type *cu_list_elements,
2849 const gdb_byte **types_list,
2850 offset_type *types_list_elements)
2851 {
2852 const gdb_byte *addr = &buffer[0];
2853
2854 /* Version check. */
2855 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2856 /* Versions earlier than 3 emitted every copy of a psymbol. This
2857 causes the index to behave very poorly for certain requests. Version 3
2858 contained incomplete addrmap. So, it seems better to just ignore such
2859 indices. */
2860 if (version < 4)
2861 {
2862 static int warning_printed = 0;
2863 if (!warning_printed)
2864 {
2865 warning (_("Skipping obsolete .gdb_index section in %s."),
2866 filename);
2867 warning_printed = 1;
2868 }
2869 return 0;
2870 }
2871 /* Index version 4 uses a different hash function than index version
2872 5 and later.
2873
2874 Versions earlier than 6 did not emit psymbols for inlined
2875 functions. Using these files will cause GDB not to be able to
2876 set breakpoints on inlined functions by name, so we ignore these
2877 indices unless the user has done
2878 "set use-deprecated-index-sections on". */
2879 if (version < 6 && !deprecated_ok)
2880 {
2881 static int warning_printed = 0;
2882 if (!warning_printed)
2883 {
2884 warning (_("\
2885 Skipping deprecated .gdb_index section in %s.\n\
2886 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2887 to use the section anyway."),
2888 filename);
2889 warning_printed = 1;
2890 }
2891 return 0;
2892 }
2893 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2894 of the TU (for symbols coming from TUs),
2895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2896 Plus gold-generated indices can have duplicate entries for global symbols,
2897 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2898 These are just performance bugs, and we can't distinguish gdb-generated
2899 indices from gold-generated ones, so issue no warning here. */
2900
2901 /* Indexes with higher version than the one supported by GDB may be no
2902 longer backward compatible. */
2903 if (version > 8)
2904 return 0;
2905
2906 map->version = version;
2907
2908 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2909
2910 int i = 0;
2911 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2912 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2913 / 8);
2914 ++i;
2915
2916 *types_list = addr + MAYBE_SWAP (metadata[i]);
2917 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2918 - MAYBE_SWAP (metadata[i]))
2919 / 8);
2920 ++i;
2921
2922 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2923 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2924 map->address_table
2925 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2926 ++i;
2927
2928 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2929 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2930 map->symbol_table
2931 = gdb::array_view<mapped_index::symbol_table_slot>
2932 ((mapped_index::symbol_table_slot *) symbol_table,
2933 (mapped_index::symbol_table_slot *) symbol_table_end);
2934
2935 ++i;
2936 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2937
2938 return 1;
2939 }
2940
2941 /* Callback types for dwarf2_read_gdb_index. */
2942
2943 typedef gdb::function_view
2944 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2945 get_gdb_index_contents_ftype;
2946 typedef gdb::function_view
2947 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2948 get_gdb_index_contents_dwz_ftype;
2949
2950 /* Read .gdb_index. If everything went ok, initialize the "quick"
2951 elements of all the CUs and return 1. Otherwise, return 0. */
2952
2953 static int
2954 dwarf2_read_gdb_index
2955 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2956 get_gdb_index_contents_ftype get_gdb_index_contents,
2957 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2958 {
2959 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2960 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2961 struct dwz_file *dwz;
2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
2963
2964 gdb::array_view<const gdb_byte> main_index_contents
2965 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2966
2967 if (main_index_contents.empty ())
2968 return 0;
2969
2970 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2971 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2972 use_deprecated_index_sections,
2973 main_index_contents, map.get (), &cu_list,
2974 &cu_list_elements, &types_list,
2975 &types_list_elements))
2976 return 0;
2977
2978 /* Don't use the index if it's empty. */
2979 if (map->symbol_table.empty ())
2980 return 0;
2981
2982 /* If there is a .dwz file, read it so we can get its CU list as
2983 well. */
2984 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2985 if (dwz != NULL)
2986 {
2987 struct mapped_index dwz_map;
2988 const gdb_byte *dwz_types_ignore;
2989 offset_type dwz_types_elements_ignore;
2990
2991 gdb::array_view<const gdb_byte> dwz_index_content
2992 = get_gdb_index_contents_dwz (objfile, dwz);
2993
2994 if (dwz_index_content.empty ())
2995 return 0;
2996
2997 if (!read_gdb_index_from_buffer (objfile,
2998 bfd_get_filename (dwz->dwz_bfd.get ()),
2999 1, dwz_index_content, &dwz_map,
3000 &dwz_list, &dwz_list_elements,
3001 &dwz_types_ignore,
3002 &dwz_types_elements_ignore))
3003 {
3004 warning (_("could not read '.gdb_index' section from %s; skipping"),
3005 bfd_get_filename (dwz->dwz_bfd.get ()));
3006 return 0;
3007 }
3008 }
3009
3010 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3011 dwz_list, dwz_list_elements);
3012
3013 if (types_list_elements)
3014 {
3015 /* We can only handle a single .debug_types when we have an
3016 index. */
3017 if (dwarf2_per_objfile->types.size () != 1)
3018 return 0;
3019
3020 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3021
3022 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3023 types_list, types_list_elements);
3024 }
3025
3026 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3027
3028 dwarf2_per_objfile->index_table = std::move (map);
3029 dwarf2_per_objfile->using_index = 1;
3030 dwarf2_per_objfile->quick_file_names_table =
3031 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3032
3033 return 1;
3034 }
3035
3036 /* die_reader_func for dw2_get_file_names. */
3037
3038 static void
3039 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3040 const gdb_byte *info_ptr,
3041 struct die_info *comp_unit_die)
3042 {
3043 struct dwarf2_cu *cu = reader->cu;
3044 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3045 struct dwarf2_per_objfile *dwarf2_per_objfile
3046 = cu->per_cu->dwarf2_per_objfile;
3047 struct objfile *objfile = dwarf2_per_objfile->objfile;
3048 struct dwarf2_per_cu_data *lh_cu;
3049 struct attribute *attr;
3050 void **slot;
3051 struct quick_file_names *qfn;
3052
3053 gdb_assert (! this_cu->is_debug_types);
3054
3055 /* Our callers never want to match partial units -- instead they
3056 will match the enclosing full CU. */
3057 if (comp_unit_die->tag == DW_TAG_partial_unit)
3058 {
3059 this_cu->v.quick->no_file_data = 1;
3060 return;
3061 }
3062
3063 lh_cu = this_cu;
3064 slot = NULL;
3065
3066 line_header_up lh;
3067 sect_offset line_offset {};
3068
3069 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3070 if (attr != nullptr)
3071 {
3072 struct quick_file_names find_entry;
3073
3074 line_offset = (sect_offset) DW_UNSND (attr);
3075
3076 /* We may have already read in this line header (TU line header sharing).
3077 If we have we're done. */
3078 find_entry.hash.dwo_unit = cu->dwo_unit;
3079 find_entry.hash.line_sect_off = line_offset;
3080 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3081 &find_entry, INSERT);
3082 if (*slot != NULL)
3083 {
3084 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3085 return;
3086 }
3087
3088 lh = dwarf_decode_line_header (line_offset, cu);
3089 }
3090 if (lh == NULL)
3091 {
3092 lh_cu->v.quick->no_file_data = 1;
3093 return;
3094 }
3095
3096 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3097 qfn->hash.dwo_unit = cu->dwo_unit;
3098 qfn->hash.line_sect_off = line_offset;
3099 gdb_assert (slot != NULL);
3100 *slot = qfn;
3101
3102 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3103
3104 int offset = 0;
3105 if (strcmp (fnd.name, "<unknown>") != 0)
3106 ++offset;
3107
3108 qfn->num_file_names = offset + lh->file_names_size ();
3109 qfn->file_names =
3110 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3111 if (offset != 0)
3112 qfn->file_names[0] = xstrdup (fnd.name);
3113 for (int i = 0; i < lh->file_names_size (); ++i)
3114 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3115 fnd.comp_dir).release ();
3116 qfn->real_names = NULL;
3117
3118 lh_cu->v.quick->file_names = qfn;
3119 }
3120
3121 /* A helper for the "quick" functions which attempts to read the line
3122 table for THIS_CU. */
3123
3124 static struct quick_file_names *
3125 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3126 {
3127 /* This should never be called for TUs. */
3128 gdb_assert (! this_cu->is_debug_types);
3129 /* Nor type unit groups. */
3130 gdb_assert (! this_cu->type_unit_group_p ());
3131
3132 if (this_cu->v.quick->file_names != NULL)
3133 return this_cu->v.quick->file_names;
3134 /* If we know there is no line data, no point in looking again. */
3135 if (this_cu->v.quick->no_file_data)
3136 return NULL;
3137
3138 cutu_reader reader (this_cu);
3139 if (!reader.dummy_p)
3140 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3141
3142 if (this_cu->v.quick->no_file_data)
3143 return NULL;
3144 return this_cu->v.quick->file_names;
3145 }
3146
3147 /* A helper for the "quick" functions which computes and caches the
3148 real path for a given file name from the line table. */
3149
3150 static const char *
3151 dw2_get_real_path (struct objfile *objfile,
3152 struct quick_file_names *qfn, int index)
3153 {
3154 if (qfn->real_names == NULL)
3155 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3156 qfn->num_file_names, const char *);
3157
3158 if (qfn->real_names[index] == NULL)
3159 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3160
3161 return qfn->real_names[index];
3162 }
3163
3164 static struct symtab *
3165 dw2_find_last_source_symtab (struct objfile *objfile)
3166 {
3167 struct dwarf2_per_objfile *dwarf2_per_objfile
3168 = get_dwarf2_per_objfile (objfile);
3169 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3170 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3171
3172 if (cust == NULL)
3173 return NULL;
3174
3175 return compunit_primary_filetab (cust);
3176 }
3177
3178 /* Traversal function for dw2_forget_cached_source_info. */
3179
3180 static int
3181 dw2_free_cached_file_names (void **slot, void *info)
3182 {
3183 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3184
3185 if (file_data->real_names)
3186 {
3187 int i;
3188
3189 for (i = 0; i < file_data->num_file_names; ++i)
3190 {
3191 xfree ((void*) file_data->real_names[i]);
3192 file_data->real_names[i] = NULL;
3193 }
3194 }
3195
3196 return 1;
3197 }
3198
3199 static void
3200 dw2_forget_cached_source_info (struct objfile *objfile)
3201 {
3202 struct dwarf2_per_objfile *dwarf2_per_objfile
3203 = get_dwarf2_per_objfile (objfile);
3204
3205 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3206 dw2_free_cached_file_names, NULL);
3207 }
3208
3209 /* Helper function for dw2_map_symtabs_matching_filename that expands
3210 the symtabs and calls the iterator. */
3211
3212 static int
3213 dw2_map_expand_apply (struct objfile *objfile,
3214 struct dwarf2_per_cu_data *per_cu,
3215 const char *name, const char *real_path,
3216 gdb::function_view<bool (symtab *)> callback)
3217 {
3218 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3219
3220 /* Don't visit already-expanded CUs. */
3221 if (per_cu->v.quick->compunit_symtab)
3222 return 0;
3223
3224 /* This may expand more than one symtab, and we want to iterate over
3225 all of them. */
3226 dw2_instantiate_symtab (per_cu, false);
3227
3228 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3229 last_made, callback);
3230 }
3231
3232 /* Implementation of the map_symtabs_matching_filename method. */
3233
3234 static bool
3235 dw2_map_symtabs_matching_filename
3236 (struct objfile *objfile, const char *name, const char *real_path,
3237 gdb::function_view<bool (symtab *)> callback)
3238 {
3239 const char *name_basename = lbasename (name);
3240 struct dwarf2_per_objfile *dwarf2_per_objfile
3241 = get_dwarf2_per_objfile (objfile);
3242
3243 /* The rule is CUs specify all the files, including those used by
3244 any TU, so there's no need to scan TUs here. */
3245
3246 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3247 {
3248 /* We only need to look at symtabs not already expanded. */
3249 if (per_cu->v.quick->compunit_symtab)
3250 continue;
3251
3252 quick_file_names *file_data = dw2_get_file_names (per_cu);
3253 if (file_data == NULL)
3254 continue;
3255
3256 for (int j = 0; j < file_data->num_file_names; ++j)
3257 {
3258 const char *this_name = file_data->file_names[j];
3259 const char *this_real_name;
3260
3261 if (compare_filenames_for_search (this_name, name))
3262 {
3263 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3264 callback))
3265 return true;
3266 continue;
3267 }
3268
3269 /* Before we invoke realpath, which can get expensive when many
3270 files are involved, do a quick comparison of the basenames. */
3271 if (! basenames_may_differ
3272 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3273 continue;
3274
3275 this_real_name = dw2_get_real_path (objfile, file_data, j);
3276 if (compare_filenames_for_search (this_real_name, name))
3277 {
3278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3279 callback))
3280 return true;
3281 continue;
3282 }
3283
3284 if (real_path != NULL)
3285 {
3286 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3287 gdb_assert (IS_ABSOLUTE_PATH (name));
3288 if (this_real_name != NULL
3289 && FILENAME_CMP (real_path, this_real_name) == 0)
3290 {
3291 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3292 callback))
3293 return true;
3294 continue;
3295 }
3296 }
3297 }
3298 }
3299
3300 return false;
3301 }
3302
3303 /* Struct used to manage iterating over all CUs looking for a symbol. */
3304
3305 struct dw2_symtab_iterator
3306 {
3307 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3308 struct dwarf2_per_objfile *dwarf2_per_objfile;
3309 /* If set, only look for symbols that match that block. Valid values are
3310 GLOBAL_BLOCK and STATIC_BLOCK. */
3311 gdb::optional<block_enum> block_index;
3312 /* The kind of symbol we're looking for. */
3313 domain_enum domain;
3314 /* The list of CUs from the index entry of the symbol,
3315 or NULL if not found. */
3316 offset_type *vec;
3317 /* The next element in VEC to look at. */
3318 int next;
3319 /* The number of elements in VEC, or zero if there is no match. */
3320 int length;
3321 /* Have we seen a global version of the symbol?
3322 If so we can ignore all further global instances.
3323 This is to work around gold/15646, inefficient gold-generated
3324 indices. */
3325 int global_seen;
3326 };
3327
3328 /* Initialize the index symtab iterator ITER. */
3329
3330 static void
3331 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3332 struct dwarf2_per_objfile *dwarf2_per_objfile,
3333 gdb::optional<block_enum> block_index,
3334 domain_enum domain,
3335 const char *name)
3336 {
3337 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3338 iter->block_index = block_index;
3339 iter->domain = domain;
3340 iter->next = 0;
3341 iter->global_seen = 0;
3342
3343 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3344
3345 /* index is NULL if OBJF_READNOW. */
3346 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3347 iter->length = MAYBE_SWAP (*iter->vec);
3348 else
3349 {
3350 iter->vec = NULL;
3351 iter->length = 0;
3352 }
3353 }
3354
3355 /* Return the next matching CU or NULL if there are no more. */
3356
3357 static struct dwarf2_per_cu_data *
3358 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3359 {
3360 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3361
3362 for ( ; iter->next < iter->length; ++iter->next)
3363 {
3364 offset_type cu_index_and_attrs =
3365 MAYBE_SWAP (iter->vec[iter->next + 1]);
3366 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3367 gdb_index_symbol_kind symbol_kind =
3368 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3369 /* Only check the symbol attributes if they're present.
3370 Indices prior to version 7 don't record them,
3371 and indices >= 7 may elide them for certain symbols
3372 (gold does this). */
3373 int attrs_valid =
3374 (dwarf2_per_objfile->index_table->version >= 7
3375 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3376
3377 /* Don't crash on bad data. */
3378 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3379 + dwarf2_per_objfile->all_type_units.size ()))
3380 {
3381 complaint (_(".gdb_index entry has bad CU index"
3382 " [in module %s]"),
3383 objfile_name (dwarf2_per_objfile->objfile));
3384 continue;
3385 }
3386
3387 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3388
3389 /* Skip if already read in. */
3390 if (per_cu->v.quick->compunit_symtab)
3391 continue;
3392
3393 /* Check static vs global. */
3394 if (attrs_valid)
3395 {
3396 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3397
3398 if (iter->block_index.has_value ())
3399 {
3400 bool want_static = *iter->block_index == STATIC_BLOCK;
3401
3402 if (is_static != want_static)
3403 continue;
3404 }
3405
3406 /* Work around gold/15646. */
3407 if (!is_static && iter->global_seen)
3408 continue;
3409 if (!is_static)
3410 iter->global_seen = 1;
3411 }
3412
3413 /* Only check the symbol's kind if it has one. */
3414 if (attrs_valid)
3415 {
3416 switch (iter->domain)
3417 {
3418 case VAR_DOMAIN:
3419 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3421 /* Some types are also in VAR_DOMAIN. */
3422 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3423 continue;
3424 break;
3425 case STRUCT_DOMAIN:
3426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3427 continue;
3428 break;
3429 case LABEL_DOMAIN:
3430 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3431 continue;
3432 break;
3433 case MODULE_DOMAIN:
3434 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3435 continue;
3436 break;
3437 default:
3438 break;
3439 }
3440 }
3441
3442 ++iter->next;
3443 return per_cu;
3444 }
3445
3446 return NULL;
3447 }
3448
3449 static struct compunit_symtab *
3450 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3451 const char *name, domain_enum domain)
3452 {
3453 struct compunit_symtab *stab_best = NULL;
3454 struct dwarf2_per_objfile *dwarf2_per_objfile
3455 = get_dwarf2_per_objfile (objfile);
3456
3457 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3458
3459 struct dw2_symtab_iterator iter;
3460 struct dwarf2_per_cu_data *per_cu;
3461
3462 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3463
3464 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3465 {
3466 struct symbol *sym, *with_opaque = NULL;
3467 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3468 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3469 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3470
3471 sym = block_find_symbol (block, name, domain,
3472 block_find_non_opaque_type_preferred,
3473 &with_opaque);
3474
3475 /* Some caution must be observed with overloaded functions
3476 and methods, since the index will not contain any overload
3477 information (but NAME might contain it). */
3478
3479 if (sym != NULL
3480 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3481 return stab;
3482 if (with_opaque != NULL
3483 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3484 stab_best = stab;
3485
3486 /* Keep looking through other CUs. */
3487 }
3488
3489 return stab_best;
3490 }
3491
3492 static void
3493 dw2_print_stats (struct objfile *objfile)
3494 {
3495 struct dwarf2_per_objfile *dwarf2_per_objfile
3496 = get_dwarf2_per_objfile (objfile);
3497 int total = (dwarf2_per_objfile->all_comp_units.size ()
3498 + dwarf2_per_objfile->all_type_units.size ());
3499 int count = 0;
3500
3501 for (int i = 0; i < total; ++i)
3502 {
3503 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3504
3505 if (!per_cu->v.quick->compunit_symtab)
3506 ++count;
3507 }
3508 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3509 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3510 }
3511
3512 /* This dumps minimal information about the index.
3513 It is called via "mt print objfiles".
3514 One use is to verify .gdb_index has been loaded by the
3515 gdb.dwarf2/gdb-index.exp testcase. */
3516
3517 static void
3518 dw2_dump (struct objfile *objfile)
3519 {
3520 struct dwarf2_per_objfile *dwarf2_per_objfile
3521 = get_dwarf2_per_objfile (objfile);
3522
3523 gdb_assert (dwarf2_per_objfile->using_index);
3524 printf_filtered (".gdb_index:");
3525 if (dwarf2_per_objfile->index_table != NULL)
3526 {
3527 printf_filtered (" version %d\n",
3528 dwarf2_per_objfile->index_table->version);
3529 }
3530 else
3531 printf_filtered (" faked for \"readnow\"\n");
3532 printf_filtered ("\n");
3533 }
3534
3535 static void
3536 dw2_expand_symtabs_for_function (struct objfile *objfile,
3537 const char *func_name)
3538 {
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
3541
3542 struct dw2_symtab_iterator iter;
3543 struct dwarf2_per_cu_data *per_cu;
3544
3545 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3546
3547 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3548 dw2_instantiate_symtab (per_cu, false);
3549
3550 }
3551
3552 static void
3553 dw2_expand_all_symtabs (struct objfile *objfile)
3554 {
3555 struct dwarf2_per_objfile *dwarf2_per_objfile
3556 = get_dwarf2_per_objfile (objfile);
3557 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3558 + dwarf2_per_objfile->all_type_units.size ());
3559
3560 for (int i = 0; i < total_units; ++i)
3561 {
3562 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3563
3564 /* We don't want to directly expand a partial CU, because if we
3565 read it with the wrong language, then assertion failures can
3566 be triggered later on. See PR symtab/23010. So, tell
3567 dw2_instantiate_symtab to skip partial CUs -- any important
3568 partial CU will be read via DW_TAG_imported_unit anyway. */
3569 dw2_instantiate_symtab (per_cu, true);
3570 }
3571 }
3572
3573 static void
3574 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3575 const char *fullname)
3576 {
3577 struct dwarf2_per_objfile *dwarf2_per_objfile
3578 = get_dwarf2_per_objfile (objfile);
3579
3580 /* We don't need to consider type units here.
3581 This is only called for examining code, e.g. expand_line_sal.
3582 There can be an order of magnitude (or more) more type units
3583 than comp units, and we avoid them if we can. */
3584
3585 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3586 {
3587 /* We only need to look at symtabs not already expanded. */
3588 if (per_cu->v.quick->compunit_symtab)
3589 continue;
3590
3591 quick_file_names *file_data = dw2_get_file_names (per_cu);
3592 if (file_data == NULL)
3593 continue;
3594
3595 for (int j = 0; j < file_data->num_file_names; ++j)
3596 {
3597 const char *this_fullname = file_data->file_names[j];
3598
3599 if (filename_cmp (this_fullname, fullname) == 0)
3600 {
3601 dw2_instantiate_symtab (per_cu, false);
3602 break;
3603 }
3604 }
3605 }
3606 }
3607
3608 static void
3609 dw2_map_matching_symbols
3610 (struct objfile *objfile,
3611 const lookup_name_info &name, domain_enum domain,
3612 int global,
3613 gdb::function_view<symbol_found_callback_ftype> callback,
3614 symbol_compare_ftype *ordered_compare)
3615 {
3616 /* Currently unimplemented; used for Ada. The function can be called if the
3617 current language is Ada for a non-Ada objfile using GNU index. As Ada
3618 does not look for non-Ada symbols this function should just return. */
3619 }
3620
3621 /* Starting from a search name, return the string that finds the upper
3622 bound of all strings that start with SEARCH_NAME in a sorted name
3623 list. Returns the empty string to indicate that the upper bound is
3624 the end of the list. */
3625
3626 static std::string
3627 make_sort_after_prefix_name (const char *search_name)
3628 {
3629 /* When looking to complete "func", we find the upper bound of all
3630 symbols that start with "func" by looking for where we'd insert
3631 the closest string that would follow "func" in lexicographical
3632 order. Usually, that's "func"-with-last-character-incremented,
3633 i.e. "fund". Mind non-ASCII characters, though. Usually those
3634 will be UTF-8 multi-byte sequences, but we can't be certain.
3635 Especially mind the 0xff character, which is a valid character in
3636 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3637 rule out compilers allowing it in identifiers. Note that
3638 conveniently, strcmp/strcasecmp are specified to compare
3639 characters interpreted as unsigned char. So what we do is treat
3640 the whole string as a base 256 number composed of a sequence of
3641 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3642 to 0, and carries 1 to the following more-significant position.
3643 If the very first character in SEARCH_NAME ends up incremented
3644 and carries/overflows, then the upper bound is the end of the
3645 list. The string after the empty string is also the empty
3646 string.
3647
3648 Some examples of this operation:
3649
3650 SEARCH_NAME => "+1" RESULT
3651
3652 "abc" => "abd"
3653 "ab\xff" => "ac"
3654 "\xff" "a" "\xff" => "\xff" "b"
3655 "\xff" => ""
3656 "\xff\xff" => ""
3657 "" => ""
3658
3659 Then, with these symbols for example:
3660
3661 func
3662 func1
3663 fund
3664
3665 completing "func" looks for symbols between "func" and
3666 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3667 which finds "func" and "func1", but not "fund".
3668
3669 And with:
3670
3671 funcÿ (Latin1 'ÿ' [0xff])
3672 funcÿ1
3673 fund
3674
3675 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3676 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3677
3678 And with:
3679
3680 ÿÿ (Latin1 'ÿ' [0xff])
3681 ÿÿ1
3682
3683 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3684 the end of the list.
3685 */
3686 std::string after = search_name;
3687 while (!after.empty () && (unsigned char) after.back () == 0xff)
3688 after.pop_back ();
3689 if (!after.empty ())
3690 after.back () = (unsigned char) after.back () + 1;
3691 return after;
3692 }
3693
3694 /* See declaration. */
3695
3696 std::pair<std::vector<name_component>::const_iterator,
3697 std::vector<name_component>::const_iterator>
3698 mapped_index_base::find_name_components_bounds
3699 (const lookup_name_info &lookup_name_without_params, language lang) const
3700 {
3701 auto *name_cmp
3702 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3703
3704 const char *lang_name
3705 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3706
3707 /* Comparison function object for lower_bound that matches against a
3708 given symbol name. */
3709 auto lookup_compare_lower = [&] (const name_component &elem,
3710 const char *name)
3711 {
3712 const char *elem_qualified = this->symbol_name_at (elem.idx);
3713 const char *elem_name = elem_qualified + elem.name_offset;
3714 return name_cmp (elem_name, name) < 0;
3715 };
3716
3717 /* Comparison function object for upper_bound that matches against a
3718 given symbol name. */
3719 auto lookup_compare_upper = [&] (const char *name,
3720 const name_component &elem)
3721 {
3722 const char *elem_qualified = this->symbol_name_at (elem.idx);
3723 const char *elem_name = elem_qualified + elem.name_offset;
3724 return name_cmp (name, elem_name) < 0;
3725 };
3726
3727 auto begin = this->name_components.begin ();
3728 auto end = this->name_components.end ();
3729
3730 /* Find the lower bound. */
3731 auto lower = [&] ()
3732 {
3733 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3734 return begin;
3735 else
3736 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3737 } ();
3738
3739 /* Find the upper bound. */
3740 auto upper = [&] ()
3741 {
3742 if (lookup_name_without_params.completion_mode ())
3743 {
3744 /* In completion mode, we want UPPER to point past all
3745 symbols names that have the same prefix. I.e., with
3746 these symbols, and completing "func":
3747
3748 function << lower bound
3749 function1
3750 other_function << upper bound
3751
3752 We find the upper bound by looking for the insertion
3753 point of "func"-with-last-character-incremented,
3754 i.e. "fund". */
3755 std::string after = make_sort_after_prefix_name (lang_name);
3756 if (after.empty ())
3757 return end;
3758 return std::lower_bound (lower, end, after.c_str (),
3759 lookup_compare_lower);
3760 }
3761 else
3762 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3763 } ();
3764
3765 return {lower, upper};
3766 }
3767
3768 /* See declaration. */
3769
3770 void
3771 mapped_index_base::build_name_components ()
3772 {
3773 if (!this->name_components.empty ())
3774 return;
3775
3776 this->name_components_casing = case_sensitivity;
3777 auto *name_cmp
3778 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3779
3780 /* The code below only knows how to break apart components of C++
3781 symbol names (and other languages that use '::' as
3782 namespace/module separator) and Ada symbol names. */
3783 auto count = this->symbol_name_count ();
3784 for (offset_type idx = 0; idx < count; idx++)
3785 {
3786 if (this->symbol_name_slot_invalid (idx))
3787 continue;
3788
3789 const char *name = this->symbol_name_at (idx);
3790
3791 /* Add each name component to the name component table. */
3792 unsigned int previous_len = 0;
3793
3794 if (strstr (name, "::") != nullptr)
3795 {
3796 for (unsigned int current_len = cp_find_first_component (name);
3797 name[current_len] != '\0';
3798 current_len += cp_find_first_component (name + current_len))
3799 {
3800 gdb_assert (name[current_len] == ':');
3801 this->name_components.push_back ({previous_len, idx});
3802 /* Skip the '::'. */
3803 current_len += 2;
3804 previous_len = current_len;
3805 }
3806 }
3807 else
3808 {
3809 /* Handle the Ada encoded (aka mangled) form here. */
3810 for (const char *iter = strstr (name, "__");
3811 iter != nullptr;
3812 iter = strstr (iter, "__"))
3813 {
3814 this->name_components.push_back ({previous_len, idx});
3815 iter += 2;
3816 previous_len = iter - name;
3817 }
3818 }
3819
3820 this->name_components.push_back ({previous_len, idx});
3821 }
3822
3823 /* Sort name_components elements by name. */
3824 auto name_comp_compare = [&] (const name_component &left,
3825 const name_component &right)
3826 {
3827 const char *left_qualified = this->symbol_name_at (left.idx);
3828 const char *right_qualified = this->symbol_name_at (right.idx);
3829
3830 const char *left_name = left_qualified + left.name_offset;
3831 const char *right_name = right_qualified + right.name_offset;
3832
3833 return name_cmp (left_name, right_name) < 0;
3834 };
3835
3836 std::sort (this->name_components.begin (),
3837 this->name_components.end (),
3838 name_comp_compare);
3839 }
3840
3841 /* Helper for dw2_expand_symtabs_matching that works with a
3842 mapped_index_base instead of the containing objfile. This is split
3843 to a separate function in order to be able to unit test the
3844 name_components matching using a mock mapped_index_base. For each
3845 symbol name that matches, calls MATCH_CALLBACK, passing it the
3846 symbol's index in the mapped_index_base symbol table. */
3847
3848 static void
3849 dw2_expand_symtabs_matching_symbol
3850 (mapped_index_base &index,
3851 const lookup_name_info &lookup_name_in,
3852 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3853 enum search_domain kind,
3854 gdb::function_view<bool (offset_type)> match_callback)
3855 {
3856 lookup_name_info lookup_name_without_params
3857 = lookup_name_in.make_ignore_params ();
3858
3859 /* Build the symbol name component sorted vector, if we haven't
3860 yet. */
3861 index.build_name_components ();
3862
3863 /* The same symbol may appear more than once in the range though.
3864 E.g., if we're looking for symbols that complete "w", and we have
3865 a symbol named "w1::w2", we'll find the two name components for
3866 that same symbol in the range. To be sure we only call the
3867 callback once per symbol, we first collect the symbol name
3868 indexes that matched in a temporary vector and ignore
3869 duplicates. */
3870 std::vector<offset_type> matches;
3871
3872 struct name_and_matcher
3873 {
3874 symbol_name_matcher_ftype *matcher;
3875 const std::string &name;
3876
3877 bool operator== (const name_and_matcher &other) const
3878 {
3879 return matcher == other.matcher && name == other.name;
3880 }
3881 };
3882
3883 /* A vector holding all the different symbol name matchers, for all
3884 languages. */
3885 std::vector<name_and_matcher> matchers;
3886
3887 for (int i = 0; i < nr_languages; i++)
3888 {
3889 enum language lang_e = (enum language) i;
3890
3891 const language_defn *lang = language_def (lang_e);
3892 symbol_name_matcher_ftype *name_matcher
3893 = get_symbol_name_matcher (lang, lookup_name_without_params);
3894
3895 name_and_matcher key {
3896 name_matcher,
3897 lookup_name_without_params.language_lookup_name (lang_e)
3898 };
3899
3900 /* Don't insert the same comparison routine more than once.
3901 Note that we do this linear walk. This is not a problem in
3902 practice because the number of supported languages is
3903 low. */
3904 if (std::find (matchers.begin (), matchers.end (), key)
3905 != matchers.end ())
3906 continue;
3907 matchers.push_back (std::move (key));
3908
3909 auto bounds
3910 = index.find_name_components_bounds (lookup_name_without_params,
3911 lang_e);
3912
3913 /* Now for each symbol name in range, check to see if we have a name
3914 match, and if so, call the MATCH_CALLBACK callback. */
3915
3916 for (; bounds.first != bounds.second; ++bounds.first)
3917 {
3918 const char *qualified = index.symbol_name_at (bounds.first->idx);
3919
3920 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3921 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3922 continue;
3923
3924 matches.push_back (bounds.first->idx);
3925 }
3926 }
3927
3928 std::sort (matches.begin (), matches.end ());
3929
3930 /* Finally call the callback, once per match. */
3931 ULONGEST prev = -1;
3932 for (offset_type idx : matches)
3933 {
3934 if (prev != idx)
3935 {
3936 if (!match_callback (idx))
3937 break;
3938 prev = idx;
3939 }
3940 }
3941
3942 /* Above we use a type wider than idx's for 'prev', since 0 and
3943 (offset_type)-1 are both possible values. */
3944 static_assert (sizeof (prev) > sizeof (offset_type), "");
3945 }
3946
3947 #if GDB_SELF_TEST
3948
3949 namespace selftests { namespace dw2_expand_symtabs_matching {
3950
3951 /* A mock .gdb_index/.debug_names-like name index table, enough to
3952 exercise dw2_expand_symtabs_matching_symbol, which works with the
3953 mapped_index_base interface. Builds an index from the symbol list
3954 passed as parameter to the constructor. */
3955 class mock_mapped_index : public mapped_index_base
3956 {
3957 public:
3958 mock_mapped_index (gdb::array_view<const char *> symbols)
3959 : m_symbol_table (symbols)
3960 {}
3961
3962 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3963
3964 /* Return the number of names in the symbol table. */
3965 size_t symbol_name_count () const override
3966 {
3967 return m_symbol_table.size ();
3968 }
3969
3970 /* Get the name of the symbol at IDX in the symbol table. */
3971 const char *symbol_name_at (offset_type idx) const override
3972 {
3973 return m_symbol_table[idx];
3974 }
3975
3976 private:
3977 gdb::array_view<const char *> m_symbol_table;
3978 };
3979
3980 /* Convenience function that converts a NULL pointer to a "<null>"
3981 string, to pass to print routines. */
3982
3983 static const char *
3984 string_or_null (const char *str)
3985 {
3986 return str != NULL ? str : "<null>";
3987 }
3988
3989 /* Check if a lookup_name_info built from
3990 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3991 index. EXPECTED_LIST is the list of expected matches, in expected
3992 matching order. If no match expected, then an empty list is
3993 specified. Returns true on success. On failure prints a warning
3994 indicating the file:line that failed, and returns false. */
3995
3996 static bool
3997 check_match (const char *file, int line,
3998 mock_mapped_index &mock_index,
3999 const char *name, symbol_name_match_type match_type,
4000 bool completion_mode,
4001 std::initializer_list<const char *> expected_list)
4002 {
4003 lookup_name_info lookup_name (name, match_type, completion_mode);
4004
4005 bool matched = true;
4006
4007 auto mismatch = [&] (const char *expected_str,
4008 const char *got)
4009 {
4010 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4011 "expected=\"%s\", got=\"%s\"\n"),
4012 file, line,
4013 (match_type == symbol_name_match_type::FULL
4014 ? "FULL" : "WILD"),
4015 name, string_or_null (expected_str), string_or_null (got));
4016 matched = false;
4017 };
4018
4019 auto expected_it = expected_list.begin ();
4020 auto expected_end = expected_list.end ();
4021
4022 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4023 NULL, ALL_DOMAIN,
4024 [&] (offset_type idx)
4025 {
4026 const char *matched_name = mock_index.symbol_name_at (idx);
4027 const char *expected_str
4028 = expected_it == expected_end ? NULL : *expected_it++;
4029
4030 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4031 mismatch (expected_str, matched_name);
4032 return true;
4033 });
4034
4035 const char *expected_str
4036 = expected_it == expected_end ? NULL : *expected_it++;
4037 if (expected_str != NULL)
4038 mismatch (expected_str, NULL);
4039
4040 return matched;
4041 }
4042
4043 /* The symbols added to the mock mapped_index for testing (in
4044 canonical form). */
4045 static const char *test_symbols[] = {
4046 "function",
4047 "std::bar",
4048 "std::zfunction",
4049 "std::zfunction2",
4050 "w1::w2",
4051 "ns::foo<char*>",
4052 "ns::foo<int>",
4053 "ns::foo<long>",
4054 "ns2::tmpl<int>::foo2",
4055 "(anonymous namespace)::A::B::C",
4056
4057 /* These are used to check that the increment-last-char in the
4058 matching algorithm for completion doesn't match "t1_fund" when
4059 completing "t1_func". */
4060 "t1_func",
4061 "t1_func1",
4062 "t1_fund",
4063 "t1_fund1",
4064
4065 /* A UTF-8 name with multi-byte sequences to make sure that
4066 cp-name-parser understands this as a single identifier ("função"
4067 is "function" in PT). */
4068 u8"u8função",
4069
4070 /* \377 (0xff) is Latin1 'ÿ'. */
4071 "yfunc\377",
4072
4073 /* \377 (0xff) is Latin1 'ÿ'. */
4074 "\377",
4075 "\377\377123",
4076
4077 /* A name with all sorts of complications. Starts with "z" to make
4078 it easier for the completion tests below. */
4079 #define Z_SYM_NAME \
4080 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4081 "::tuple<(anonymous namespace)::ui*, " \
4082 "std::default_delete<(anonymous namespace)::ui>, void>"
4083
4084 Z_SYM_NAME
4085 };
4086
4087 /* Returns true if the mapped_index_base::find_name_component_bounds
4088 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4089 in completion mode. */
4090
4091 static bool
4092 check_find_bounds_finds (mapped_index_base &index,
4093 const char *search_name,
4094 gdb::array_view<const char *> expected_syms)
4095 {
4096 lookup_name_info lookup_name (search_name,
4097 symbol_name_match_type::FULL, true);
4098
4099 auto bounds = index.find_name_components_bounds (lookup_name,
4100 language_cplus);
4101
4102 size_t distance = std::distance (bounds.first, bounds.second);
4103 if (distance != expected_syms.size ())
4104 return false;
4105
4106 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4107 {
4108 auto nc_elem = bounds.first + exp_elem;
4109 const char *qualified = index.symbol_name_at (nc_elem->idx);
4110 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4111 return false;
4112 }
4113
4114 return true;
4115 }
4116
4117 /* Test the lower-level mapped_index::find_name_component_bounds
4118 method. */
4119
4120 static void
4121 test_mapped_index_find_name_component_bounds ()
4122 {
4123 mock_mapped_index mock_index (test_symbols);
4124
4125 mock_index.build_name_components ();
4126
4127 /* Test the lower-level mapped_index::find_name_component_bounds
4128 method in completion mode. */
4129 {
4130 static const char *expected_syms[] = {
4131 "t1_func",
4132 "t1_func1",
4133 };
4134
4135 SELF_CHECK (check_find_bounds_finds (mock_index,
4136 "t1_func", expected_syms));
4137 }
4138
4139 /* Check that the increment-last-char in the name matching algorithm
4140 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4141 {
4142 static const char *expected_syms1[] = {
4143 "\377",
4144 "\377\377123",
4145 };
4146 SELF_CHECK (check_find_bounds_finds (mock_index,
4147 "\377", expected_syms1));
4148
4149 static const char *expected_syms2[] = {
4150 "\377\377123",
4151 };
4152 SELF_CHECK (check_find_bounds_finds (mock_index,
4153 "\377\377", expected_syms2));
4154 }
4155 }
4156
4157 /* Test dw2_expand_symtabs_matching_symbol. */
4158
4159 static void
4160 test_dw2_expand_symtabs_matching_symbol ()
4161 {
4162 mock_mapped_index mock_index (test_symbols);
4163
4164 /* We let all tests run until the end even if some fails, for debug
4165 convenience. */
4166 bool any_mismatch = false;
4167
4168 /* Create the expected symbols list (an initializer_list). Needed
4169 because lists have commas, and we need to pass them to CHECK,
4170 which is a macro. */
4171 #define EXPECT(...) { __VA_ARGS__ }
4172
4173 /* Wrapper for check_match that passes down the current
4174 __FILE__/__LINE__. */
4175 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4176 any_mismatch |= !check_match (__FILE__, __LINE__, \
4177 mock_index, \
4178 NAME, MATCH_TYPE, COMPLETION_MODE, \
4179 EXPECTED_LIST)
4180
4181 /* Identity checks. */
4182 for (const char *sym : test_symbols)
4183 {
4184 /* Should be able to match all existing symbols. */
4185 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4186 EXPECT (sym));
4187
4188 /* Should be able to match all existing symbols with
4189 parameters. */
4190 std::string with_params = std::string (sym) + "(int)";
4191 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4192 EXPECT (sym));
4193
4194 /* Should be able to match all existing symbols with
4195 parameters and qualifiers. */
4196 with_params = std::string (sym) + " ( int ) const";
4197 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4198 EXPECT (sym));
4199
4200 /* This should really find sym, but cp-name-parser.y doesn't
4201 know about lvalue/rvalue qualifiers yet. */
4202 with_params = std::string (sym) + " ( int ) &&";
4203 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4204 {});
4205 }
4206
4207 /* Check that the name matching algorithm for completion doesn't get
4208 confused with Latin1 'ÿ' / 0xff. */
4209 {
4210 static const char str[] = "\377";
4211 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4212 EXPECT ("\377", "\377\377123"));
4213 }
4214
4215 /* Check that the increment-last-char in the matching algorithm for
4216 completion doesn't match "t1_fund" when completing "t1_func". */
4217 {
4218 static const char str[] = "t1_func";
4219 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4220 EXPECT ("t1_func", "t1_func1"));
4221 }
4222
4223 /* Check that completion mode works at each prefix of the expected
4224 symbol name. */
4225 {
4226 static const char str[] = "function(int)";
4227 size_t len = strlen (str);
4228 std::string lookup;
4229
4230 for (size_t i = 1; i < len; i++)
4231 {
4232 lookup.assign (str, i);
4233 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4234 EXPECT ("function"));
4235 }
4236 }
4237
4238 /* While "w" is a prefix of both components, the match function
4239 should still only be called once. */
4240 {
4241 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4242 EXPECT ("w1::w2"));
4243 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4244 EXPECT ("w1::w2"));
4245 }
4246
4247 /* Same, with a "complicated" symbol. */
4248 {
4249 static const char str[] = Z_SYM_NAME;
4250 size_t len = strlen (str);
4251 std::string lookup;
4252
4253 for (size_t i = 1; i < len; i++)
4254 {
4255 lookup.assign (str, i);
4256 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4257 EXPECT (Z_SYM_NAME));
4258 }
4259 }
4260
4261 /* In FULL mode, an incomplete symbol doesn't match. */
4262 {
4263 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4264 {});
4265 }
4266
4267 /* A complete symbol with parameters matches any overload, since the
4268 index has no overload info. */
4269 {
4270 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4271 EXPECT ("std::zfunction", "std::zfunction2"));
4272 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4273 EXPECT ("std::zfunction", "std::zfunction2"));
4274 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4275 EXPECT ("std::zfunction", "std::zfunction2"));
4276 }
4277
4278 /* Check that whitespace is ignored appropriately. A symbol with a
4279 template argument list. */
4280 {
4281 static const char expected[] = "ns::foo<int>";
4282 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4283 EXPECT (expected));
4284 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4285 EXPECT (expected));
4286 }
4287
4288 /* Check that whitespace is ignored appropriately. A symbol with a
4289 template argument list that includes a pointer. */
4290 {
4291 static const char expected[] = "ns::foo<char*>";
4292 /* Try both completion and non-completion modes. */
4293 static const bool completion_mode[2] = {false, true};
4294 for (size_t i = 0; i < 2; i++)
4295 {
4296 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4297 completion_mode[i], EXPECT (expected));
4298 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4299 completion_mode[i], EXPECT (expected));
4300
4301 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4302 completion_mode[i], EXPECT (expected));
4303 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4304 completion_mode[i], EXPECT (expected));
4305 }
4306 }
4307
4308 {
4309 /* Check method qualifiers are ignored. */
4310 static const char expected[] = "ns::foo<char*>";
4311 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4312 symbol_name_match_type::FULL, true, EXPECT (expected));
4313 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4314 symbol_name_match_type::FULL, true, EXPECT (expected));
4315 CHECK_MATCH ("foo < char * > ( int ) const",
4316 symbol_name_match_type::WILD, true, EXPECT (expected));
4317 CHECK_MATCH ("foo < char * > ( int ) &&",
4318 symbol_name_match_type::WILD, true, EXPECT (expected));
4319 }
4320
4321 /* Test lookup names that don't match anything. */
4322 {
4323 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4324 {});
4325
4326 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4327 {});
4328 }
4329
4330 /* Some wild matching tests, exercising "(anonymous namespace)",
4331 which should not be confused with a parameter list. */
4332 {
4333 static const char *syms[] = {
4334 "A::B::C",
4335 "B::C",
4336 "C",
4337 "A :: B :: C ( int )",
4338 "B :: C ( int )",
4339 "C ( int )",
4340 };
4341
4342 for (const char *s : syms)
4343 {
4344 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4345 EXPECT ("(anonymous namespace)::A::B::C"));
4346 }
4347 }
4348
4349 {
4350 static const char expected[] = "ns2::tmpl<int>::foo2";
4351 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4352 EXPECT (expected));
4353 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4354 EXPECT (expected));
4355 }
4356
4357 SELF_CHECK (!any_mismatch);
4358
4359 #undef EXPECT
4360 #undef CHECK_MATCH
4361 }
4362
4363 static void
4364 run_test ()
4365 {
4366 test_mapped_index_find_name_component_bounds ();
4367 test_dw2_expand_symtabs_matching_symbol ();
4368 }
4369
4370 }} // namespace selftests::dw2_expand_symtabs_matching
4371
4372 #endif /* GDB_SELF_TEST */
4373
4374 /* If FILE_MATCHER is NULL or if PER_CU has
4375 dwarf2_per_cu_quick_data::MARK set (see
4376 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4377 EXPANSION_NOTIFY on it. */
4378
4379 static void
4380 dw2_expand_symtabs_matching_one
4381 (struct dwarf2_per_cu_data *per_cu,
4382 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4383 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4384 {
4385 if (file_matcher == NULL || per_cu->v.quick->mark)
4386 {
4387 bool symtab_was_null
4388 = (per_cu->v.quick->compunit_symtab == NULL);
4389
4390 dw2_instantiate_symtab (per_cu, false);
4391
4392 if (expansion_notify != NULL
4393 && symtab_was_null
4394 && per_cu->v.quick->compunit_symtab != NULL)
4395 expansion_notify (per_cu->v.quick->compunit_symtab);
4396 }
4397 }
4398
4399 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4400 matched, to expand corresponding CUs that were marked. IDX is the
4401 index of the symbol name that matched. */
4402
4403 static void
4404 dw2_expand_marked_cus
4405 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4406 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4407 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4408 search_domain kind)
4409 {
4410 offset_type *vec, vec_len, vec_idx;
4411 bool global_seen = false;
4412 mapped_index &index = *dwarf2_per_objfile->index_table;
4413
4414 vec = (offset_type *) (index.constant_pool
4415 + MAYBE_SWAP (index.symbol_table[idx].vec));
4416 vec_len = MAYBE_SWAP (vec[0]);
4417 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4418 {
4419 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4420 /* This value is only valid for index versions >= 7. */
4421 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4422 gdb_index_symbol_kind symbol_kind =
4423 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4424 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4425 /* Only check the symbol attributes if they're present.
4426 Indices prior to version 7 don't record them,
4427 and indices >= 7 may elide them for certain symbols
4428 (gold does this). */
4429 int attrs_valid =
4430 (index.version >= 7
4431 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4432
4433 /* Work around gold/15646. */
4434 if (attrs_valid)
4435 {
4436 if (!is_static && global_seen)
4437 continue;
4438 if (!is_static)
4439 global_seen = true;
4440 }
4441
4442 /* Only check the symbol's kind if it has one. */
4443 if (attrs_valid)
4444 {
4445 switch (kind)
4446 {
4447 case VARIABLES_DOMAIN:
4448 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4449 continue;
4450 break;
4451 case FUNCTIONS_DOMAIN:
4452 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4453 continue;
4454 break;
4455 case TYPES_DOMAIN:
4456 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4457 continue;
4458 break;
4459 case MODULES_DOMAIN:
4460 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4461 continue;
4462 break;
4463 default:
4464 break;
4465 }
4466 }
4467
4468 /* Don't crash on bad data. */
4469 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4470 + dwarf2_per_objfile->all_type_units.size ()))
4471 {
4472 complaint (_(".gdb_index entry has bad CU index"
4473 " [in module %s]"),
4474 objfile_name (dwarf2_per_objfile->objfile));
4475 continue;
4476 }
4477
4478 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4479 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4480 expansion_notify);
4481 }
4482 }
4483
4484 /* If FILE_MATCHER is non-NULL, set all the
4485 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4486 that match FILE_MATCHER. */
4487
4488 static void
4489 dw_expand_symtabs_matching_file_matcher
4490 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4491 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4492 {
4493 if (file_matcher == NULL)
4494 return;
4495
4496 objfile *const objfile = dwarf2_per_objfile->objfile;
4497
4498 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4499 htab_eq_pointer,
4500 NULL, xcalloc, xfree));
4501 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4502 htab_eq_pointer,
4503 NULL, xcalloc, xfree));
4504
4505 /* The rule is CUs specify all the files, including those used by
4506 any TU, so there's no need to scan TUs here. */
4507
4508 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4509 {
4510 QUIT;
4511
4512 per_cu->v.quick->mark = 0;
4513
4514 /* We only need to look at symtabs not already expanded. */
4515 if (per_cu->v.quick->compunit_symtab)
4516 continue;
4517
4518 quick_file_names *file_data = dw2_get_file_names (per_cu);
4519 if (file_data == NULL)
4520 continue;
4521
4522 if (htab_find (visited_not_found.get (), file_data) != NULL)
4523 continue;
4524 else if (htab_find (visited_found.get (), file_data) != NULL)
4525 {
4526 per_cu->v.quick->mark = 1;
4527 continue;
4528 }
4529
4530 for (int j = 0; j < file_data->num_file_names; ++j)
4531 {
4532 const char *this_real_name;
4533
4534 if (file_matcher (file_data->file_names[j], false))
4535 {
4536 per_cu->v.quick->mark = 1;
4537 break;
4538 }
4539
4540 /* Before we invoke realpath, which can get expensive when many
4541 files are involved, do a quick comparison of the basenames. */
4542 if (!basenames_may_differ
4543 && !file_matcher (lbasename (file_data->file_names[j]),
4544 true))
4545 continue;
4546
4547 this_real_name = dw2_get_real_path (objfile, file_data, j);
4548 if (file_matcher (this_real_name, false))
4549 {
4550 per_cu->v.quick->mark = 1;
4551 break;
4552 }
4553 }
4554
4555 void **slot = htab_find_slot (per_cu->v.quick->mark
4556 ? visited_found.get ()
4557 : visited_not_found.get (),
4558 file_data, INSERT);
4559 *slot = file_data;
4560 }
4561 }
4562
4563 static void
4564 dw2_expand_symtabs_matching
4565 (struct objfile *objfile,
4566 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4567 const lookup_name_info &lookup_name,
4568 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4569 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4570 enum search_domain kind)
4571 {
4572 struct dwarf2_per_objfile *dwarf2_per_objfile
4573 = get_dwarf2_per_objfile (objfile);
4574
4575 /* index_table is NULL if OBJF_READNOW. */
4576 if (!dwarf2_per_objfile->index_table)
4577 return;
4578
4579 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4580
4581 mapped_index &index = *dwarf2_per_objfile->index_table;
4582
4583 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4584 symbol_matcher,
4585 kind, [&] (offset_type idx)
4586 {
4587 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4588 expansion_notify, kind);
4589 return true;
4590 });
4591 }
4592
4593 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4594 symtab. */
4595
4596 static struct compunit_symtab *
4597 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4598 CORE_ADDR pc)
4599 {
4600 int i;
4601
4602 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4603 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4604 return cust;
4605
4606 if (cust->includes == NULL)
4607 return NULL;
4608
4609 for (i = 0; cust->includes[i]; ++i)
4610 {
4611 struct compunit_symtab *s = cust->includes[i];
4612
4613 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4614 if (s != NULL)
4615 return s;
4616 }
4617
4618 return NULL;
4619 }
4620
4621 static struct compunit_symtab *
4622 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4623 struct bound_minimal_symbol msymbol,
4624 CORE_ADDR pc,
4625 struct obj_section *section,
4626 int warn_if_readin)
4627 {
4628 struct dwarf2_per_cu_data *data;
4629 struct compunit_symtab *result;
4630
4631 if (!objfile->partial_symtabs->psymtabs_addrmap)
4632 return NULL;
4633
4634 CORE_ADDR baseaddr = objfile->text_section_offset ();
4635 data = (struct dwarf2_per_cu_data *) addrmap_find
4636 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4637 if (!data)
4638 return NULL;
4639
4640 if (warn_if_readin && data->v.quick->compunit_symtab)
4641 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4642 paddress (get_objfile_arch (objfile), pc));
4643
4644 result
4645 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4646 false),
4647 pc);
4648 gdb_assert (result != NULL);
4649 return result;
4650 }
4651
4652 static void
4653 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4654 void *data, int need_fullname)
4655 {
4656 struct dwarf2_per_objfile *dwarf2_per_objfile
4657 = get_dwarf2_per_objfile (objfile);
4658
4659 if (!dwarf2_per_objfile->filenames_cache)
4660 {
4661 dwarf2_per_objfile->filenames_cache.emplace ();
4662
4663 htab_up visited (htab_create_alloc (10,
4664 htab_hash_pointer, htab_eq_pointer,
4665 NULL, xcalloc, xfree));
4666
4667 /* The rule is CUs specify all the files, including those used
4668 by any TU, so there's no need to scan TUs here. We can
4669 ignore file names coming from already-expanded CUs. */
4670
4671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4672 {
4673 if (per_cu->v.quick->compunit_symtab)
4674 {
4675 void **slot = htab_find_slot (visited.get (),
4676 per_cu->v.quick->file_names,
4677 INSERT);
4678
4679 *slot = per_cu->v.quick->file_names;
4680 }
4681 }
4682
4683 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4684 {
4685 /* We only need to look at symtabs not already expanded. */
4686 if (per_cu->v.quick->compunit_symtab)
4687 continue;
4688
4689 quick_file_names *file_data = dw2_get_file_names (per_cu);
4690 if (file_data == NULL)
4691 continue;
4692
4693 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4694 if (*slot)
4695 {
4696 /* Already visited. */
4697 continue;
4698 }
4699 *slot = file_data;
4700
4701 for (int j = 0; j < file_data->num_file_names; ++j)
4702 {
4703 const char *filename = file_data->file_names[j];
4704 dwarf2_per_objfile->filenames_cache->seen (filename);
4705 }
4706 }
4707 }
4708
4709 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4710 {
4711 gdb::unique_xmalloc_ptr<char> this_real_name;
4712
4713 if (need_fullname)
4714 this_real_name = gdb_realpath (filename);
4715 (*fun) (filename, this_real_name.get (), data);
4716 });
4717 }
4718
4719 static int
4720 dw2_has_symbols (struct objfile *objfile)
4721 {
4722 return 1;
4723 }
4724
4725 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4726 {
4727 dw2_has_symbols,
4728 dw2_find_last_source_symtab,
4729 dw2_forget_cached_source_info,
4730 dw2_map_symtabs_matching_filename,
4731 dw2_lookup_symbol,
4732 dw2_print_stats,
4733 dw2_dump,
4734 dw2_expand_symtabs_for_function,
4735 dw2_expand_all_symtabs,
4736 dw2_expand_symtabs_with_fullname,
4737 dw2_map_matching_symbols,
4738 dw2_expand_symtabs_matching,
4739 dw2_find_pc_sect_compunit_symtab,
4740 NULL,
4741 dw2_map_symbol_filenames
4742 };
4743
4744 /* DWARF-5 debug_names reader. */
4745
4746 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4747 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4748
4749 /* A helper function that reads the .debug_names section in SECTION
4750 and fills in MAP. FILENAME is the name of the file containing the
4751 section; it is used for error reporting.
4752
4753 Returns true if all went well, false otherwise. */
4754
4755 static bool
4756 read_debug_names_from_section (struct objfile *objfile,
4757 const char *filename,
4758 struct dwarf2_section_info *section,
4759 mapped_debug_names &map)
4760 {
4761 if (section->empty ())
4762 return false;
4763
4764 /* Older elfutils strip versions could keep the section in the main
4765 executable while splitting it for the separate debug info file. */
4766 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4767 return false;
4768
4769 section->read (objfile);
4770
4771 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4772
4773 const gdb_byte *addr = section->buffer;
4774
4775 bfd *const abfd = section->get_bfd_owner ();
4776
4777 unsigned int bytes_read;
4778 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4779 addr += bytes_read;
4780
4781 map.dwarf5_is_dwarf64 = bytes_read != 4;
4782 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4783 if (bytes_read + length != section->size)
4784 {
4785 /* There may be multiple per-CU indices. */
4786 warning (_("Section .debug_names in %s length %s does not match "
4787 "section length %s, ignoring .debug_names."),
4788 filename, plongest (bytes_read + length),
4789 pulongest (section->size));
4790 return false;
4791 }
4792
4793 /* The version number. */
4794 uint16_t version = read_2_bytes (abfd, addr);
4795 addr += 2;
4796 if (version != 5)
4797 {
4798 warning (_("Section .debug_names in %s has unsupported version %d, "
4799 "ignoring .debug_names."),
4800 filename, version);
4801 return false;
4802 }
4803
4804 /* Padding. */
4805 uint16_t padding = read_2_bytes (abfd, addr);
4806 addr += 2;
4807 if (padding != 0)
4808 {
4809 warning (_("Section .debug_names in %s has unsupported padding %d, "
4810 "ignoring .debug_names."),
4811 filename, padding);
4812 return false;
4813 }
4814
4815 /* comp_unit_count - The number of CUs in the CU list. */
4816 map.cu_count = read_4_bytes (abfd, addr);
4817 addr += 4;
4818
4819 /* local_type_unit_count - The number of TUs in the local TU
4820 list. */
4821 map.tu_count = read_4_bytes (abfd, addr);
4822 addr += 4;
4823
4824 /* foreign_type_unit_count - The number of TUs in the foreign TU
4825 list. */
4826 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4827 addr += 4;
4828 if (foreign_tu_count != 0)
4829 {
4830 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4831 "ignoring .debug_names."),
4832 filename, static_cast<unsigned long> (foreign_tu_count));
4833 return false;
4834 }
4835
4836 /* bucket_count - The number of hash buckets in the hash lookup
4837 table. */
4838 map.bucket_count = read_4_bytes (abfd, addr);
4839 addr += 4;
4840
4841 /* name_count - The number of unique names in the index. */
4842 map.name_count = read_4_bytes (abfd, addr);
4843 addr += 4;
4844
4845 /* abbrev_table_size - The size in bytes of the abbreviations
4846 table. */
4847 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4848 addr += 4;
4849
4850 /* augmentation_string_size - The size in bytes of the augmentation
4851 string. This value is rounded up to a multiple of 4. */
4852 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4853 addr += 4;
4854 map.augmentation_is_gdb = ((augmentation_string_size
4855 == sizeof (dwarf5_augmentation))
4856 && memcmp (addr, dwarf5_augmentation,
4857 sizeof (dwarf5_augmentation)) == 0);
4858 augmentation_string_size += (-augmentation_string_size) & 3;
4859 addr += augmentation_string_size;
4860
4861 /* List of CUs */
4862 map.cu_table_reordered = addr;
4863 addr += map.cu_count * map.offset_size;
4864
4865 /* List of Local TUs */
4866 map.tu_table_reordered = addr;
4867 addr += map.tu_count * map.offset_size;
4868
4869 /* Hash Lookup Table */
4870 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4871 addr += map.bucket_count * 4;
4872 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4873 addr += map.name_count * 4;
4874
4875 /* Name Table */
4876 map.name_table_string_offs_reordered = addr;
4877 addr += map.name_count * map.offset_size;
4878 map.name_table_entry_offs_reordered = addr;
4879 addr += map.name_count * map.offset_size;
4880
4881 const gdb_byte *abbrev_table_start = addr;
4882 for (;;)
4883 {
4884 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4885 addr += bytes_read;
4886 if (index_num == 0)
4887 break;
4888
4889 const auto insertpair
4890 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4891 if (!insertpair.second)
4892 {
4893 warning (_("Section .debug_names in %s has duplicate index %s, "
4894 "ignoring .debug_names."),
4895 filename, pulongest (index_num));
4896 return false;
4897 }
4898 mapped_debug_names::index_val &indexval = insertpair.first->second;
4899 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4900 addr += bytes_read;
4901
4902 for (;;)
4903 {
4904 mapped_debug_names::index_val::attr attr;
4905 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4906 addr += bytes_read;
4907 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4908 addr += bytes_read;
4909 if (attr.form == DW_FORM_implicit_const)
4910 {
4911 attr.implicit_const = read_signed_leb128 (abfd, addr,
4912 &bytes_read);
4913 addr += bytes_read;
4914 }
4915 if (attr.dw_idx == 0 && attr.form == 0)
4916 break;
4917 indexval.attr_vec.push_back (std::move (attr));
4918 }
4919 }
4920 if (addr != abbrev_table_start + abbrev_table_size)
4921 {
4922 warning (_("Section .debug_names in %s has abbreviation_table "
4923 "of size %s vs. written as %u, ignoring .debug_names."),
4924 filename, plongest (addr - abbrev_table_start),
4925 abbrev_table_size);
4926 return false;
4927 }
4928 map.entry_pool = addr;
4929
4930 return true;
4931 }
4932
4933 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4934 list. */
4935
4936 static void
4937 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4938 const mapped_debug_names &map,
4939 dwarf2_section_info &section,
4940 bool is_dwz)
4941 {
4942 sect_offset sect_off_prev;
4943 for (uint32_t i = 0; i <= map.cu_count; ++i)
4944 {
4945 sect_offset sect_off_next;
4946 if (i < map.cu_count)
4947 {
4948 sect_off_next
4949 = (sect_offset) (extract_unsigned_integer
4950 (map.cu_table_reordered + i * map.offset_size,
4951 map.offset_size,
4952 map.dwarf5_byte_order));
4953 }
4954 else
4955 sect_off_next = (sect_offset) section.size;
4956 if (i >= 1)
4957 {
4958 const ULONGEST length = sect_off_next - sect_off_prev;
4959 dwarf2_per_cu_data *per_cu
4960 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4961 sect_off_prev, length);
4962 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4963 }
4964 sect_off_prev = sect_off_next;
4965 }
4966 }
4967
4968 /* Read the CU list from the mapped index, and use it to create all
4969 the CU objects for this dwarf2_per_objfile. */
4970
4971 static void
4972 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4973 const mapped_debug_names &map,
4974 const mapped_debug_names &dwz_map)
4975 {
4976 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4977 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4978
4979 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4980 dwarf2_per_objfile->info,
4981 false /* is_dwz */);
4982
4983 if (dwz_map.cu_count == 0)
4984 return;
4985
4986 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4987 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4988 true /* is_dwz */);
4989 }
4990
4991 /* Read .debug_names. If everything went ok, initialize the "quick"
4992 elements of all the CUs and return true. Otherwise, return false. */
4993
4994 static bool
4995 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4996 {
4997 std::unique_ptr<mapped_debug_names> map
4998 (new mapped_debug_names (dwarf2_per_objfile));
4999 mapped_debug_names dwz_map (dwarf2_per_objfile);
5000 struct objfile *objfile = dwarf2_per_objfile->objfile;
5001
5002 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5003 &dwarf2_per_objfile->debug_names,
5004 *map))
5005 return false;
5006
5007 /* Don't use the index if it's empty. */
5008 if (map->name_count == 0)
5009 return false;
5010
5011 /* If there is a .dwz file, read it so we can get its CU list as
5012 well. */
5013 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5014 if (dwz != NULL)
5015 {
5016 if (!read_debug_names_from_section (objfile,
5017 bfd_get_filename (dwz->dwz_bfd.get ()),
5018 &dwz->debug_names, dwz_map))
5019 {
5020 warning (_("could not read '.debug_names' section from %s; skipping"),
5021 bfd_get_filename (dwz->dwz_bfd.get ()));
5022 return false;
5023 }
5024 }
5025
5026 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5027
5028 if (map->tu_count != 0)
5029 {
5030 /* We can only handle a single .debug_types when we have an
5031 index. */
5032 if (dwarf2_per_objfile->types.size () != 1)
5033 return false;
5034
5035 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5036
5037 create_signatured_type_table_from_debug_names
5038 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5039 }
5040
5041 create_addrmap_from_aranges (dwarf2_per_objfile,
5042 &dwarf2_per_objfile->debug_aranges);
5043
5044 dwarf2_per_objfile->debug_names_table = std::move (map);
5045 dwarf2_per_objfile->using_index = 1;
5046 dwarf2_per_objfile->quick_file_names_table =
5047 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5048
5049 return true;
5050 }
5051
5052 /* Type used to manage iterating over all CUs looking for a symbol for
5053 .debug_names. */
5054
5055 class dw2_debug_names_iterator
5056 {
5057 public:
5058 dw2_debug_names_iterator (const mapped_debug_names &map,
5059 gdb::optional<block_enum> block_index,
5060 domain_enum domain,
5061 const char *name)
5062 : m_map (map), m_block_index (block_index), m_domain (domain),
5063 m_addr (find_vec_in_debug_names (map, name))
5064 {}
5065
5066 dw2_debug_names_iterator (const mapped_debug_names &map,
5067 search_domain search, uint32_t namei)
5068 : m_map (map),
5069 m_search (search),
5070 m_addr (find_vec_in_debug_names (map, namei))
5071 {}
5072
5073 dw2_debug_names_iterator (const mapped_debug_names &map,
5074 block_enum block_index, domain_enum domain,
5075 uint32_t namei)
5076 : m_map (map), m_block_index (block_index), m_domain (domain),
5077 m_addr (find_vec_in_debug_names (map, namei))
5078 {}
5079
5080 /* Return the next matching CU or NULL if there are no more. */
5081 dwarf2_per_cu_data *next ();
5082
5083 private:
5084 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5085 const char *name);
5086 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5087 uint32_t namei);
5088
5089 /* The internalized form of .debug_names. */
5090 const mapped_debug_names &m_map;
5091
5092 /* If set, only look for symbols that match that block. Valid values are
5093 GLOBAL_BLOCK and STATIC_BLOCK. */
5094 const gdb::optional<block_enum> m_block_index;
5095
5096 /* The kind of symbol we're looking for. */
5097 const domain_enum m_domain = UNDEF_DOMAIN;
5098 const search_domain m_search = ALL_DOMAIN;
5099
5100 /* The list of CUs from the index entry of the symbol, or NULL if
5101 not found. */
5102 const gdb_byte *m_addr;
5103 };
5104
5105 const char *
5106 mapped_debug_names::namei_to_name (uint32_t namei) const
5107 {
5108 const ULONGEST namei_string_offs
5109 = extract_unsigned_integer ((name_table_string_offs_reordered
5110 + namei * offset_size),
5111 offset_size,
5112 dwarf5_byte_order);
5113 return read_indirect_string_at_offset (dwarf2_per_objfile,
5114 namei_string_offs);
5115 }
5116
5117 /* Find a slot in .debug_names for the object named NAME. If NAME is
5118 found, return pointer to its pool data. If NAME cannot be found,
5119 return NULL. */
5120
5121 const gdb_byte *
5122 dw2_debug_names_iterator::find_vec_in_debug_names
5123 (const mapped_debug_names &map, const char *name)
5124 {
5125 int (*cmp) (const char *, const char *);
5126
5127 gdb::unique_xmalloc_ptr<char> without_params;
5128 if (current_language->la_language == language_cplus
5129 || current_language->la_language == language_fortran
5130 || current_language->la_language == language_d)
5131 {
5132 /* NAME is already canonical. Drop any qualifiers as
5133 .debug_names does not contain any. */
5134
5135 if (strchr (name, '(') != NULL)
5136 {
5137 without_params = cp_remove_params (name);
5138 if (without_params != NULL)
5139 name = without_params.get ();
5140 }
5141 }
5142
5143 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5144
5145 const uint32_t full_hash = dwarf5_djb_hash (name);
5146 uint32_t namei
5147 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5148 (map.bucket_table_reordered
5149 + (full_hash % map.bucket_count)), 4,
5150 map.dwarf5_byte_order);
5151 if (namei == 0)
5152 return NULL;
5153 --namei;
5154 if (namei >= map.name_count)
5155 {
5156 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5157 "[in module %s]"),
5158 namei, map.name_count,
5159 objfile_name (map.dwarf2_per_objfile->objfile));
5160 return NULL;
5161 }
5162
5163 for (;;)
5164 {
5165 const uint32_t namei_full_hash
5166 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5167 (map.hash_table_reordered + namei), 4,
5168 map.dwarf5_byte_order);
5169 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5170 return NULL;
5171
5172 if (full_hash == namei_full_hash)
5173 {
5174 const char *const namei_string = map.namei_to_name (namei);
5175
5176 #if 0 /* An expensive sanity check. */
5177 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5178 {
5179 complaint (_("Wrong .debug_names hash for string at index %u "
5180 "[in module %s]"),
5181 namei, objfile_name (dwarf2_per_objfile->objfile));
5182 return NULL;
5183 }
5184 #endif
5185
5186 if (cmp (namei_string, name) == 0)
5187 {
5188 const ULONGEST namei_entry_offs
5189 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5190 + namei * map.offset_size),
5191 map.offset_size, map.dwarf5_byte_order);
5192 return map.entry_pool + namei_entry_offs;
5193 }
5194 }
5195
5196 ++namei;
5197 if (namei >= map.name_count)
5198 return NULL;
5199 }
5200 }
5201
5202 const gdb_byte *
5203 dw2_debug_names_iterator::find_vec_in_debug_names
5204 (const mapped_debug_names &map, uint32_t namei)
5205 {
5206 if (namei >= map.name_count)
5207 {
5208 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5209 "[in module %s]"),
5210 namei, map.name_count,
5211 objfile_name (map.dwarf2_per_objfile->objfile));
5212 return NULL;
5213 }
5214
5215 const ULONGEST namei_entry_offs
5216 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5217 + namei * map.offset_size),
5218 map.offset_size, map.dwarf5_byte_order);
5219 return map.entry_pool + namei_entry_offs;
5220 }
5221
5222 /* See dw2_debug_names_iterator. */
5223
5224 dwarf2_per_cu_data *
5225 dw2_debug_names_iterator::next ()
5226 {
5227 if (m_addr == NULL)
5228 return NULL;
5229
5230 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5231 struct objfile *objfile = dwarf2_per_objfile->objfile;
5232 bfd *const abfd = objfile->obfd;
5233
5234 again:
5235
5236 unsigned int bytes_read;
5237 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5238 m_addr += bytes_read;
5239 if (abbrev == 0)
5240 return NULL;
5241
5242 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5243 if (indexval_it == m_map.abbrev_map.cend ())
5244 {
5245 complaint (_("Wrong .debug_names undefined abbrev code %s "
5246 "[in module %s]"),
5247 pulongest (abbrev), objfile_name (objfile));
5248 return NULL;
5249 }
5250 const mapped_debug_names::index_val &indexval = indexval_it->second;
5251 enum class symbol_linkage {
5252 unknown,
5253 static_,
5254 extern_,
5255 } symbol_linkage_ = symbol_linkage::unknown;
5256 dwarf2_per_cu_data *per_cu = NULL;
5257 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5258 {
5259 ULONGEST ull;
5260 switch (attr.form)
5261 {
5262 case DW_FORM_implicit_const:
5263 ull = attr.implicit_const;
5264 break;
5265 case DW_FORM_flag_present:
5266 ull = 1;
5267 break;
5268 case DW_FORM_udata:
5269 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5270 m_addr += bytes_read;
5271 break;
5272 default:
5273 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5274 dwarf_form_name (attr.form),
5275 objfile_name (objfile));
5276 return NULL;
5277 }
5278 switch (attr.dw_idx)
5279 {
5280 case DW_IDX_compile_unit:
5281 /* Don't crash on bad data. */
5282 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5283 {
5284 complaint (_(".debug_names entry has bad CU index %s"
5285 " [in module %s]"),
5286 pulongest (ull),
5287 objfile_name (dwarf2_per_objfile->objfile));
5288 continue;
5289 }
5290 per_cu = dwarf2_per_objfile->get_cutu (ull);
5291 break;
5292 case DW_IDX_type_unit:
5293 /* Don't crash on bad data. */
5294 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5295 {
5296 complaint (_(".debug_names entry has bad TU index %s"
5297 " [in module %s]"),
5298 pulongest (ull),
5299 objfile_name (dwarf2_per_objfile->objfile));
5300 continue;
5301 }
5302 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5303 break;
5304 case DW_IDX_GNU_internal:
5305 if (!m_map.augmentation_is_gdb)
5306 break;
5307 symbol_linkage_ = symbol_linkage::static_;
5308 break;
5309 case DW_IDX_GNU_external:
5310 if (!m_map.augmentation_is_gdb)
5311 break;
5312 symbol_linkage_ = symbol_linkage::extern_;
5313 break;
5314 }
5315 }
5316
5317 /* Skip if already read in. */
5318 if (per_cu->v.quick->compunit_symtab)
5319 goto again;
5320
5321 /* Check static vs global. */
5322 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5323 {
5324 const bool want_static = *m_block_index == STATIC_BLOCK;
5325 const bool symbol_is_static =
5326 symbol_linkage_ == symbol_linkage::static_;
5327 if (want_static != symbol_is_static)
5328 goto again;
5329 }
5330
5331 /* Match dw2_symtab_iter_next, symbol_kind
5332 and debug_names::psymbol_tag. */
5333 switch (m_domain)
5334 {
5335 case VAR_DOMAIN:
5336 switch (indexval.dwarf_tag)
5337 {
5338 case DW_TAG_variable:
5339 case DW_TAG_subprogram:
5340 /* Some types are also in VAR_DOMAIN. */
5341 case DW_TAG_typedef:
5342 case DW_TAG_structure_type:
5343 break;
5344 default:
5345 goto again;
5346 }
5347 break;
5348 case STRUCT_DOMAIN:
5349 switch (indexval.dwarf_tag)
5350 {
5351 case DW_TAG_typedef:
5352 case DW_TAG_structure_type:
5353 break;
5354 default:
5355 goto again;
5356 }
5357 break;
5358 case LABEL_DOMAIN:
5359 switch (indexval.dwarf_tag)
5360 {
5361 case 0:
5362 case DW_TAG_variable:
5363 break;
5364 default:
5365 goto again;
5366 }
5367 break;
5368 case MODULE_DOMAIN:
5369 switch (indexval.dwarf_tag)
5370 {
5371 case DW_TAG_module:
5372 break;
5373 default:
5374 goto again;
5375 }
5376 break;
5377 default:
5378 break;
5379 }
5380
5381 /* Match dw2_expand_symtabs_matching, symbol_kind and
5382 debug_names::psymbol_tag. */
5383 switch (m_search)
5384 {
5385 case VARIABLES_DOMAIN:
5386 switch (indexval.dwarf_tag)
5387 {
5388 case DW_TAG_variable:
5389 break;
5390 default:
5391 goto again;
5392 }
5393 break;
5394 case FUNCTIONS_DOMAIN:
5395 switch (indexval.dwarf_tag)
5396 {
5397 case DW_TAG_subprogram:
5398 break;
5399 default:
5400 goto again;
5401 }
5402 break;
5403 case TYPES_DOMAIN:
5404 switch (indexval.dwarf_tag)
5405 {
5406 case DW_TAG_typedef:
5407 case DW_TAG_structure_type:
5408 break;
5409 default:
5410 goto again;
5411 }
5412 break;
5413 case MODULES_DOMAIN:
5414 switch (indexval.dwarf_tag)
5415 {
5416 case DW_TAG_module:
5417 break;
5418 default:
5419 goto again;
5420 }
5421 default:
5422 break;
5423 }
5424
5425 return per_cu;
5426 }
5427
5428 static struct compunit_symtab *
5429 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5430 const char *name, domain_enum domain)
5431 {
5432 struct dwarf2_per_objfile *dwarf2_per_objfile
5433 = get_dwarf2_per_objfile (objfile);
5434
5435 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5436 if (!mapp)
5437 {
5438 /* index is NULL if OBJF_READNOW. */
5439 return NULL;
5440 }
5441 const auto &map = *mapp;
5442
5443 dw2_debug_names_iterator iter (map, block_index, domain, name);
5444
5445 struct compunit_symtab *stab_best = NULL;
5446 struct dwarf2_per_cu_data *per_cu;
5447 while ((per_cu = iter.next ()) != NULL)
5448 {
5449 struct symbol *sym, *with_opaque = NULL;
5450 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5451 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5452 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5453
5454 sym = block_find_symbol (block, name, domain,
5455 block_find_non_opaque_type_preferred,
5456 &with_opaque);
5457
5458 /* Some caution must be observed with overloaded functions and
5459 methods, since the index will not contain any overload
5460 information (but NAME might contain it). */
5461
5462 if (sym != NULL
5463 && strcmp_iw (sym->search_name (), name) == 0)
5464 return stab;
5465 if (with_opaque != NULL
5466 && strcmp_iw (with_opaque->search_name (), name) == 0)
5467 stab_best = stab;
5468
5469 /* Keep looking through other CUs. */
5470 }
5471
5472 return stab_best;
5473 }
5474
5475 /* This dumps minimal information about .debug_names. It is called
5476 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5477 uses this to verify that .debug_names has been loaded. */
5478
5479 static void
5480 dw2_debug_names_dump (struct objfile *objfile)
5481 {
5482 struct dwarf2_per_objfile *dwarf2_per_objfile
5483 = get_dwarf2_per_objfile (objfile);
5484
5485 gdb_assert (dwarf2_per_objfile->using_index);
5486 printf_filtered (".debug_names:");
5487 if (dwarf2_per_objfile->debug_names_table)
5488 printf_filtered (" exists\n");
5489 else
5490 printf_filtered (" faked for \"readnow\"\n");
5491 printf_filtered ("\n");
5492 }
5493
5494 static void
5495 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5496 const char *func_name)
5497 {
5498 struct dwarf2_per_objfile *dwarf2_per_objfile
5499 = get_dwarf2_per_objfile (objfile);
5500
5501 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5502 if (dwarf2_per_objfile->debug_names_table)
5503 {
5504 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5505
5506 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5507
5508 struct dwarf2_per_cu_data *per_cu;
5509 while ((per_cu = iter.next ()) != NULL)
5510 dw2_instantiate_symtab (per_cu, false);
5511 }
5512 }
5513
5514 static void
5515 dw2_debug_names_map_matching_symbols
5516 (struct objfile *objfile,
5517 const lookup_name_info &name, domain_enum domain,
5518 int global,
5519 gdb::function_view<symbol_found_callback_ftype> callback,
5520 symbol_compare_ftype *ordered_compare)
5521 {
5522 struct dwarf2_per_objfile *dwarf2_per_objfile
5523 = get_dwarf2_per_objfile (objfile);
5524
5525 /* debug_names_table is NULL if OBJF_READNOW. */
5526 if (!dwarf2_per_objfile->debug_names_table)
5527 return;
5528
5529 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5530 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5531
5532 const char *match_name = name.ada ().lookup_name ().c_str ();
5533 auto matcher = [&] (const char *symname)
5534 {
5535 if (ordered_compare == nullptr)
5536 return true;
5537 return ordered_compare (symname, match_name) == 0;
5538 };
5539
5540 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5541 [&] (offset_type namei)
5542 {
5543 /* The name was matched, now expand corresponding CUs that were
5544 marked. */
5545 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5546
5547 struct dwarf2_per_cu_data *per_cu;
5548 while ((per_cu = iter.next ()) != NULL)
5549 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5550 return true;
5551 });
5552
5553 /* It's a shame we couldn't do this inside the
5554 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5555 that have already been expanded. Instead, this loop matches what
5556 the psymtab code does. */
5557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5558 {
5559 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5560 if (cust != nullptr)
5561 {
5562 const struct block *block
5563 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5564 if (!iterate_over_symbols_terminated (block, name,
5565 domain, callback))
5566 break;
5567 }
5568 }
5569 }
5570
5571 static void
5572 dw2_debug_names_expand_symtabs_matching
5573 (struct objfile *objfile,
5574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5575 const lookup_name_info &lookup_name,
5576 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5577 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5578 enum search_domain kind)
5579 {
5580 struct dwarf2_per_objfile *dwarf2_per_objfile
5581 = get_dwarf2_per_objfile (objfile);
5582
5583 /* debug_names_table is NULL if OBJF_READNOW. */
5584 if (!dwarf2_per_objfile->debug_names_table)
5585 return;
5586
5587 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5588
5589 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5590
5591 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5592 symbol_matcher,
5593 kind, [&] (offset_type namei)
5594 {
5595 /* The name was matched, now expand corresponding CUs that were
5596 marked. */
5597 dw2_debug_names_iterator iter (map, kind, namei);
5598
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5602 expansion_notify);
5603 return true;
5604 });
5605 }
5606
5607 const struct quick_symbol_functions dwarf2_debug_names_functions =
5608 {
5609 dw2_has_symbols,
5610 dw2_find_last_source_symtab,
5611 dw2_forget_cached_source_info,
5612 dw2_map_symtabs_matching_filename,
5613 dw2_debug_names_lookup_symbol,
5614 dw2_print_stats,
5615 dw2_debug_names_dump,
5616 dw2_debug_names_expand_symtabs_for_function,
5617 dw2_expand_all_symtabs,
5618 dw2_expand_symtabs_with_fullname,
5619 dw2_debug_names_map_matching_symbols,
5620 dw2_debug_names_expand_symtabs_matching,
5621 dw2_find_pc_sect_compunit_symtab,
5622 NULL,
5623 dw2_map_symbol_filenames
5624 };
5625
5626 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5627 to either a dwarf2_per_objfile or dwz_file object. */
5628
5629 template <typename T>
5630 static gdb::array_view<const gdb_byte>
5631 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5632 {
5633 dwarf2_section_info *section = &section_owner->gdb_index;
5634
5635 if (section->empty ())
5636 return {};
5637
5638 /* Older elfutils strip versions could keep the section in the main
5639 executable while splitting it for the separate debug info file. */
5640 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5641 return {};
5642
5643 section->read (obj);
5644
5645 /* dwarf2_section_info::size is a bfd_size_type, while
5646 gdb::array_view works with size_t. On 32-bit hosts, with
5647 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5648 is 32-bit. So we need an explicit narrowing conversion here.
5649 This is fine, because it's impossible to allocate or mmap an
5650 array/buffer larger than what size_t can represent. */
5651 return gdb::make_array_view (section->buffer, section->size);
5652 }
5653
5654 /* Lookup the index cache for the contents of the index associated to
5655 DWARF2_OBJ. */
5656
5657 static gdb::array_view<const gdb_byte>
5658 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5659 {
5660 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5661 if (build_id == nullptr)
5662 return {};
5663
5664 return global_index_cache.lookup_gdb_index (build_id,
5665 &dwarf2_obj->index_cache_res);
5666 }
5667
5668 /* Same as the above, but for DWZ. */
5669
5670 static gdb::array_view<const gdb_byte>
5671 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5672 {
5673 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5674 if (build_id == nullptr)
5675 return {};
5676
5677 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5678 }
5679
5680 /* See symfile.h. */
5681
5682 bool
5683 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5684 {
5685 struct dwarf2_per_objfile *dwarf2_per_objfile
5686 = get_dwarf2_per_objfile (objfile);
5687
5688 /* If we're about to read full symbols, don't bother with the
5689 indices. In this case we also don't care if some other debug
5690 format is making psymtabs, because they are all about to be
5691 expanded anyway. */
5692 if ((objfile->flags & OBJF_READNOW))
5693 {
5694 dwarf2_per_objfile->using_index = 1;
5695 create_all_comp_units (dwarf2_per_objfile);
5696 create_all_type_units (dwarf2_per_objfile);
5697 dwarf2_per_objfile->quick_file_names_table
5698 = create_quick_file_names_table
5699 (dwarf2_per_objfile->all_comp_units.size ());
5700
5701 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5702 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5703 {
5704 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5705
5706 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5707 struct dwarf2_per_cu_quick_data);
5708 }
5709
5710 /* Return 1 so that gdb sees the "quick" functions. However,
5711 these functions will be no-ops because we will have expanded
5712 all symtabs. */
5713 *index_kind = dw_index_kind::GDB_INDEX;
5714 return true;
5715 }
5716
5717 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5718 {
5719 *index_kind = dw_index_kind::DEBUG_NAMES;
5720 return true;
5721 }
5722
5723 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5724 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5725 get_gdb_index_contents_from_section<dwz_file>))
5726 {
5727 *index_kind = dw_index_kind::GDB_INDEX;
5728 return true;
5729 }
5730
5731 /* ... otherwise, try to find the index in the index cache. */
5732 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5733 get_gdb_index_contents_from_cache,
5734 get_gdb_index_contents_from_cache_dwz))
5735 {
5736 global_index_cache.hit ();
5737 *index_kind = dw_index_kind::GDB_INDEX;
5738 return true;
5739 }
5740
5741 global_index_cache.miss ();
5742 return false;
5743 }
5744
5745 \f
5746
5747 /* Build a partial symbol table. */
5748
5749 void
5750 dwarf2_build_psymtabs (struct objfile *objfile)
5751 {
5752 struct dwarf2_per_objfile *dwarf2_per_objfile
5753 = get_dwarf2_per_objfile (objfile);
5754
5755 init_psymbol_list (objfile, 1024);
5756
5757 try
5758 {
5759 /* This isn't really ideal: all the data we allocate on the
5760 objfile's obstack is still uselessly kept around. However,
5761 freeing it seems unsafe. */
5762 psymtab_discarder psymtabs (objfile);
5763 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5764 psymtabs.keep ();
5765
5766 /* (maybe) store an index in the cache. */
5767 global_index_cache.store (dwarf2_per_objfile);
5768 }
5769 catch (const gdb_exception_error &except)
5770 {
5771 exception_print (gdb_stderr, except);
5772 }
5773 }
5774
5775 /* Find the base address of the compilation unit for range lists and
5776 location lists. It will normally be specified by DW_AT_low_pc.
5777 In DWARF-3 draft 4, the base address could be overridden by
5778 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5779 compilation units with discontinuous ranges. */
5780
5781 static void
5782 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5783 {
5784 struct attribute *attr;
5785
5786 cu->base_known = 0;
5787 cu->base_address = 0;
5788
5789 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5790 if (attr != nullptr)
5791 {
5792 cu->base_address = attr->value_as_address ();
5793 cu->base_known = 1;
5794 }
5795 else
5796 {
5797 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5798 if (attr != nullptr)
5799 {
5800 cu->base_address = attr->value_as_address ();
5801 cu->base_known = 1;
5802 }
5803 }
5804 }
5805
5806 /* Helper function that returns the proper abbrev section for
5807 THIS_CU. */
5808
5809 static struct dwarf2_section_info *
5810 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5811 {
5812 struct dwarf2_section_info *abbrev;
5813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5814
5815 if (this_cu->is_dwz)
5816 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5817 else
5818 abbrev = &dwarf2_per_objfile->abbrev;
5819
5820 return abbrev;
5821 }
5822
5823 /* Fetch the abbreviation table offset from a comp or type unit header. */
5824
5825 static sect_offset
5826 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5827 struct dwarf2_section_info *section,
5828 sect_offset sect_off)
5829 {
5830 bfd *abfd = section->get_bfd_owner ();
5831 const gdb_byte *info_ptr;
5832 unsigned int initial_length_size, offset_size;
5833 uint16_t version;
5834
5835 section->read (dwarf2_per_objfile->objfile);
5836 info_ptr = section->buffer + to_underlying (sect_off);
5837 read_initial_length (abfd, info_ptr, &initial_length_size);
5838 offset_size = initial_length_size == 4 ? 4 : 8;
5839 info_ptr += initial_length_size;
5840
5841 version = read_2_bytes (abfd, info_ptr);
5842 info_ptr += 2;
5843 if (version >= 5)
5844 {
5845 /* Skip unit type and address size. */
5846 info_ptr += 2;
5847 }
5848
5849 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5850 }
5851
5852 /* A partial symtab that is used only for include files. */
5853 struct dwarf2_include_psymtab : public partial_symtab
5854 {
5855 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5856 : partial_symtab (filename, objfile)
5857 {
5858 }
5859
5860 void read_symtab (struct objfile *objfile) override
5861 {
5862 expand_psymtab (objfile);
5863 }
5864
5865 void expand_psymtab (struct objfile *objfile) override
5866 {
5867 if (m_readin)
5868 return;
5869 /* It's an include file, no symbols to read for it.
5870 Everything is in the parent symtab. */
5871 read_dependencies (objfile);
5872 m_readin = true;
5873 }
5874
5875 bool readin_p () const override
5876 {
5877 return m_readin;
5878 }
5879
5880 struct compunit_symtab *get_compunit_symtab () const override
5881 {
5882 return nullptr;
5883 }
5884
5885 private:
5886
5887 bool m_readin = false;
5888 };
5889
5890 /* Allocate a new partial symtab for file named NAME and mark this new
5891 partial symtab as being an include of PST. */
5892
5893 static void
5894 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5895 struct objfile *objfile)
5896 {
5897 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5898
5899 if (!IS_ABSOLUTE_PATH (subpst->filename))
5900 {
5901 /* It shares objfile->objfile_obstack. */
5902 subpst->dirname = pst->dirname;
5903 }
5904
5905 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5906 subpst->dependencies[0] = pst;
5907 subpst->number_of_dependencies = 1;
5908 }
5909
5910 /* Read the Line Number Program data and extract the list of files
5911 included by the source file represented by PST. Build an include
5912 partial symtab for each of these included files. */
5913
5914 static void
5915 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5916 struct die_info *die,
5917 dwarf2_psymtab *pst)
5918 {
5919 line_header_up lh;
5920 struct attribute *attr;
5921
5922 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5923 if (attr != nullptr)
5924 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5925 if (lh == NULL)
5926 return; /* No linetable, so no includes. */
5927
5928 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5929 that we pass in the raw text_low here; that is ok because we're
5930 only decoding the line table to make include partial symtabs, and
5931 so the addresses aren't really used. */
5932 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5933 pst->raw_text_low (), 1);
5934 }
5935
5936 static hashval_t
5937 hash_signatured_type (const void *item)
5938 {
5939 const struct signatured_type *sig_type
5940 = (const struct signatured_type *) item;
5941
5942 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5943 return sig_type->signature;
5944 }
5945
5946 static int
5947 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5948 {
5949 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5950 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5951
5952 return lhs->signature == rhs->signature;
5953 }
5954
5955 /* Allocate a hash table for signatured types. */
5956
5957 static htab_up
5958 allocate_signatured_type_table ()
5959 {
5960 return htab_up (htab_create_alloc (41,
5961 hash_signatured_type,
5962 eq_signatured_type,
5963 NULL, xcalloc, xfree));
5964 }
5965
5966 /* A helper function to add a signatured type CU to a table. */
5967
5968 static int
5969 add_signatured_type_cu_to_table (void **slot, void *datum)
5970 {
5971 struct signatured_type *sigt = (struct signatured_type *) *slot;
5972 std::vector<signatured_type *> *all_type_units
5973 = (std::vector<signatured_type *> *) datum;
5974
5975 all_type_units->push_back (sigt);
5976
5977 return 1;
5978 }
5979
5980 /* A helper for create_debug_types_hash_table. Read types from SECTION
5981 and fill them into TYPES_HTAB. It will process only type units,
5982 therefore DW_UT_type. */
5983
5984 static void
5985 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5986 struct dwo_file *dwo_file,
5987 dwarf2_section_info *section, htab_up &types_htab,
5988 rcuh_kind section_kind)
5989 {
5990 struct objfile *objfile = dwarf2_per_objfile->objfile;
5991 struct dwarf2_section_info *abbrev_section;
5992 bfd *abfd;
5993 const gdb_byte *info_ptr, *end_ptr;
5994
5995 abbrev_section = (dwo_file != NULL
5996 ? &dwo_file->sections.abbrev
5997 : &dwarf2_per_objfile->abbrev);
5998
5999 if (dwarf_read_debug)
6000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6001 section->get_name (),
6002 abbrev_section->get_file_name ());
6003
6004 section->read (objfile);
6005 info_ptr = section->buffer;
6006
6007 if (info_ptr == NULL)
6008 return;
6009
6010 /* We can't set abfd until now because the section may be empty or
6011 not present, in which case the bfd is unknown. */
6012 abfd = section->get_bfd_owner ();
6013
6014 /* We don't use cutu_reader here because we don't need to read
6015 any dies: the signature is in the header. */
6016
6017 end_ptr = info_ptr + section->size;
6018 while (info_ptr < end_ptr)
6019 {
6020 struct signatured_type *sig_type;
6021 struct dwo_unit *dwo_tu;
6022 void **slot;
6023 const gdb_byte *ptr = info_ptr;
6024 struct comp_unit_head header;
6025 unsigned int length;
6026
6027 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6028
6029 /* Initialize it due to a false compiler warning. */
6030 header.signature = -1;
6031 header.type_cu_offset_in_tu = (cu_offset) -1;
6032
6033 /* We need to read the type's signature in order to build the hash
6034 table, but we don't need anything else just yet. */
6035
6036 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6037 abbrev_section, ptr, section_kind);
6038
6039 length = header.get_length ();
6040
6041 /* Skip dummy type units. */
6042 if (ptr >= info_ptr + length
6043 || peek_abbrev_code (abfd, ptr) == 0
6044 || header.unit_type != DW_UT_type)
6045 {
6046 info_ptr += length;
6047 continue;
6048 }
6049
6050 if (types_htab == NULL)
6051 {
6052 if (dwo_file)
6053 types_htab = allocate_dwo_unit_table ();
6054 else
6055 types_htab = allocate_signatured_type_table ();
6056 }
6057
6058 if (dwo_file)
6059 {
6060 sig_type = NULL;
6061 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6062 struct dwo_unit);
6063 dwo_tu->dwo_file = dwo_file;
6064 dwo_tu->signature = header.signature;
6065 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6066 dwo_tu->section = section;
6067 dwo_tu->sect_off = sect_off;
6068 dwo_tu->length = length;
6069 }
6070 else
6071 {
6072 /* N.B.: type_offset is not usable if this type uses a DWO file.
6073 The real type_offset is in the DWO file. */
6074 dwo_tu = NULL;
6075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6076 struct signatured_type);
6077 sig_type->signature = header.signature;
6078 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6079 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6080 sig_type->per_cu.is_debug_types = 1;
6081 sig_type->per_cu.section = section;
6082 sig_type->per_cu.sect_off = sect_off;
6083 sig_type->per_cu.length = length;
6084 }
6085
6086 slot = htab_find_slot (types_htab.get (),
6087 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6088 INSERT);
6089 gdb_assert (slot != NULL);
6090 if (*slot != NULL)
6091 {
6092 sect_offset dup_sect_off;
6093
6094 if (dwo_file)
6095 {
6096 const struct dwo_unit *dup_tu
6097 = (const struct dwo_unit *) *slot;
6098
6099 dup_sect_off = dup_tu->sect_off;
6100 }
6101 else
6102 {
6103 const struct signatured_type *dup_tu
6104 = (const struct signatured_type *) *slot;
6105
6106 dup_sect_off = dup_tu->per_cu.sect_off;
6107 }
6108
6109 complaint (_("debug type entry at offset %s is duplicate to"
6110 " the entry at offset %s, signature %s"),
6111 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6112 hex_string (header.signature));
6113 }
6114 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6115
6116 if (dwarf_read_debug > 1)
6117 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6118 sect_offset_str (sect_off),
6119 hex_string (header.signature));
6120
6121 info_ptr += length;
6122 }
6123 }
6124
6125 /* Create the hash table of all entries in the .debug_types
6126 (or .debug_types.dwo) section(s).
6127 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6128 otherwise it is NULL.
6129
6130 The result is a pointer to the hash table or NULL if there are no types.
6131
6132 Note: This function processes DWO files only, not DWP files. */
6133
6134 static void
6135 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6136 struct dwo_file *dwo_file,
6137 gdb::array_view<dwarf2_section_info> type_sections,
6138 htab_up &types_htab)
6139 {
6140 for (dwarf2_section_info &section : type_sections)
6141 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6142 types_htab, rcuh_kind::TYPE);
6143 }
6144
6145 /* Create the hash table of all entries in the .debug_types section,
6146 and initialize all_type_units.
6147 The result is zero if there is an error (e.g. missing .debug_types section),
6148 otherwise non-zero. */
6149
6150 static int
6151 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6152 {
6153 htab_up types_htab;
6154
6155 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6156 &dwarf2_per_objfile->info, types_htab,
6157 rcuh_kind::COMPILE);
6158 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6159 dwarf2_per_objfile->types, types_htab);
6160 if (types_htab == NULL)
6161 {
6162 dwarf2_per_objfile->signatured_types = NULL;
6163 return 0;
6164 }
6165
6166 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6167
6168 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6169 dwarf2_per_objfile->all_type_units.reserve
6170 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6171
6172 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6173 add_signatured_type_cu_to_table,
6174 &dwarf2_per_objfile->all_type_units);
6175
6176 return 1;
6177 }
6178
6179 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6180 If SLOT is non-NULL, it is the entry to use in the hash table.
6181 Otherwise we find one. */
6182
6183 static struct signatured_type *
6184 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6185 void **slot)
6186 {
6187 struct objfile *objfile = dwarf2_per_objfile->objfile;
6188
6189 if (dwarf2_per_objfile->all_type_units.size ()
6190 == dwarf2_per_objfile->all_type_units.capacity ())
6191 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6192
6193 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6194 struct signatured_type);
6195
6196 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6197 sig_type->signature = sig;
6198 sig_type->per_cu.is_debug_types = 1;
6199 if (dwarf2_per_objfile->using_index)
6200 {
6201 sig_type->per_cu.v.quick =
6202 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwarf2_per_cu_quick_data);
6204 }
6205
6206 if (slot == NULL)
6207 {
6208 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6209 sig_type, INSERT);
6210 }
6211 gdb_assert (*slot == NULL);
6212 *slot = sig_type;
6213 /* The rest of sig_type must be filled in by the caller. */
6214 return sig_type;
6215 }
6216
6217 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6218 Fill in SIG_ENTRY with DWO_ENTRY. */
6219
6220 static void
6221 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6222 struct signatured_type *sig_entry,
6223 struct dwo_unit *dwo_entry)
6224 {
6225 /* Make sure we're not clobbering something we don't expect to. */
6226 gdb_assert (! sig_entry->per_cu.queued);
6227 gdb_assert (sig_entry->per_cu.cu == NULL);
6228 if (dwarf2_per_objfile->using_index)
6229 {
6230 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6231 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6232 }
6233 else
6234 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6235 gdb_assert (sig_entry->signature == dwo_entry->signature);
6236 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6237 gdb_assert (sig_entry->type_unit_group == NULL);
6238 gdb_assert (sig_entry->dwo_unit == NULL);
6239
6240 sig_entry->per_cu.section = dwo_entry->section;
6241 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6242 sig_entry->per_cu.length = dwo_entry->length;
6243 sig_entry->per_cu.reading_dwo_directly = 1;
6244 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6245 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6246 sig_entry->dwo_unit = dwo_entry;
6247 }
6248
6249 /* Subroutine of lookup_signatured_type.
6250 If we haven't read the TU yet, create the signatured_type data structure
6251 for a TU to be read in directly from a DWO file, bypassing the stub.
6252 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6253 using .gdb_index, then when reading a CU we want to stay in the DWO file
6254 containing that CU. Otherwise we could end up reading several other DWO
6255 files (due to comdat folding) to process the transitive closure of all the
6256 mentioned TUs, and that can be slow. The current DWO file will have every
6257 type signature that it needs.
6258 We only do this for .gdb_index because in the psymtab case we already have
6259 to read all the DWOs to build the type unit groups. */
6260
6261 static struct signatured_type *
6262 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6263 {
6264 struct dwarf2_per_objfile *dwarf2_per_objfile
6265 = cu->per_cu->dwarf2_per_objfile;
6266 struct dwo_file *dwo_file;
6267 struct dwo_unit find_dwo_entry, *dwo_entry;
6268 struct signatured_type find_sig_entry, *sig_entry;
6269 void **slot;
6270
6271 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6272
6273 /* If TU skeletons have been removed then we may not have read in any
6274 TUs yet. */
6275 if (dwarf2_per_objfile->signatured_types == NULL)
6276 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6277
6278 /* We only ever need to read in one copy of a signatured type.
6279 Use the global signatured_types array to do our own comdat-folding
6280 of types. If this is the first time we're reading this TU, and
6281 the TU has an entry in .gdb_index, replace the recorded data from
6282 .gdb_index with this TU. */
6283
6284 find_sig_entry.signature = sig;
6285 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6286 &find_sig_entry, INSERT);
6287 sig_entry = (struct signatured_type *) *slot;
6288
6289 /* We can get here with the TU already read, *or* in the process of being
6290 read. Don't reassign the global entry to point to this DWO if that's
6291 the case. Also note that if the TU is already being read, it may not
6292 have come from a DWO, the program may be a mix of Fission-compiled
6293 code and non-Fission-compiled code. */
6294
6295 /* Have we already tried to read this TU?
6296 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6297 needn't exist in the global table yet). */
6298 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6299 return sig_entry;
6300
6301 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6302 dwo_unit of the TU itself. */
6303 dwo_file = cu->dwo_unit->dwo_file;
6304
6305 /* Ok, this is the first time we're reading this TU. */
6306 if (dwo_file->tus == NULL)
6307 return NULL;
6308 find_dwo_entry.signature = sig;
6309 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6310 &find_dwo_entry);
6311 if (dwo_entry == NULL)
6312 return NULL;
6313
6314 /* If the global table doesn't have an entry for this TU, add one. */
6315 if (sig_entry == NULL)
6316 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6317
6318 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6319 sig_entry->per_cu.tu_read = 1;
6320 return sig_entry;
6321 }
6322
6323 /* Subroutine of lookup_signatured_type.
6324 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6325 then try the DWP file. If the TU stub (skeleton) has been removed then
6326 it won't be in .gdb_index. */
6327
6328 static struct signatured_type *
6329 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6330 {
6331 struct dwarf2_per_objfile *dwarf2_per_objfile
6332 = cu->per_cu->dwarf2_per_objfile;
6333 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6334 struct dwo_unit *dwo_entry;
6335 struct signatured_type find_sig_entry, *sig_entry;
6336 void **slot;
6337
6338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6339 gdb_assert (dwp_file != NULL);
6340
6341 /* If TU skeletons have been removed then we may not have read in any
6342 TUs yet. */
6343 if (dwarf2_per_objfile->signatured_types == NULL)
6344 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6345
6346 find_sig_entry.signature = sig;
6347 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6348 &find_sig_entry, INSERT);
6349 sig_entry = (struct signatured_type *) *slot;
6350
6351 /* Have we already tried to read this TU?
6352 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6353 needn't exist in the global table yet). */
6354 if (sig_entry != NULL)
6355 return sig_entry;
6356
6357 if (dwp_file->tus == NULL)
6358 return NULL;
6359 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6360 sig, 1 /* is_debug_types */);
6361 if (dwo_entry == NULL)
6362 return NULL;
6363
6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6365 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6366
6367 return sig_entry;
6368 }
6369
6370 /* Lookup a signature based type for DW_FORM_ref_sig8.
6371 Returns NULL if signature SIG is not present in the table.
6372 It is up to the caller to complain about this. */
6373
6374 static struct signatured_type *
6375 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6376 {
6377 struct dwarf2_per_objfile *dwarf2_per_objfile
6378 = cu->per_cu->dwarf2_per_objfile;
6379
6380 if (cu->dwo_unit
6381 && dwarf2_per_objfile->using_index)
6382 {
6383 /* We're in a DWO/DWP file, and we're using .gdb_index.
6384 These cases require special processing. */
6385 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6386 return lookup_dwo_signatured_type (cu, sig);
6387 else
6388 return lookup_dwp_signatured_type (cu, sig);
6389 }
6390 else
6391 {
6392 struct signatured_type find_entry, *entry;
6393
6394 if (dwarf2_per_objfile->signatured_types == NULL)
6395 return NULL;
6396 find_entry.signature = sig;
6397 entry = ((struct signatured_type *)
6398 htab_find (dwarf2_per_objfile->signatured_types.get (),
6399 &find_entry));
6400 return entry;
6401 }
6402 }
6403
6404 /* Return the address base of the compile unit, which, if exists, is stored
6405 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6406 static gdb::optional<ULONGEST>
6407 lookup_addr_base (struct die_info *comp_unit_die)
6408 {
6409 struct attribute *attr;
6410 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6411 if (attr == nullptr)
6412 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6413 if (attr == nullptr)
6414 return gdb::optional<ULONGEST> ();
6415 return DW_UNSND (attr);
6416 }
6417
6418 /* Return range lists base of the compile unit, which, if exists, is stored
6419 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6420 static ULONGEST
6421 lookup_ranges_base (struct die_info *comp_unit_die)
6422 {
6423 struct attribute *attr;
6424 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6425 if (attr == nullptr)
6426 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6427 if (attr == nullptr)
6428 return 0;
6429 return DW_UNSND (attr);
6430 }
6431
6432 /* Low level DIE reading support. */
6433
6434 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6435
6436 static void
6437 init_cu_die_reader (struct die_reader_specs *reader,
6438 struct dwarf2_cu *cu,
6439 struct dwarf2_section_info *section,
6440 struct dwo_file *dwo_file,
6441 struct abbrev_table *abbrev_table)
6442 {
6443 gdb_assert (section->readin && section->buffer != NULL);
6444 reader->abfd = section->get_bfd_owner ();
6445 reader->cu = cu;
6446 reader->dwo_file = dwo_file;
6447 reader->die_section = section;
6448 reader->buffer = section->buffer;
6449 reader->buffer_end = section->buffer + section->size;
6450 reader->abbrev_table = abbrev_table;
6451 }
6452
6453 /* Subroutine of cutu_reader to simplify it.
6454 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6455 There's just a lot of work to do, and cutu_reader is big enough
6456 already.
6457
6458 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6459 from it to the DIE in the DWO. If NULL we are skipping the stub.
6460 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6461 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6462 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6463 STUB_COMP_DIR may be non-NULL.
6464 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6465 are filled in with the info of the DIE from the DWO file.
6466 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6467 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6468 kept around for at least as long as *RESULT_READER.
6469
6470 The result is non-zero if a valid (non-dummy) DIE was found. */
6471
6472 static int
6473 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6474 struct dwo_unit *dwo_unit,
6475 struct die_info *stub_comp_unit_die,
6476 const char *stub_comp_dir,
6477 struct die_reader_specs *result_reader,
6478 const gdb_byte **result_info_ptr,
6479 struct die_info **result_comp_unit_die,
6480 abbrev_table_up *result_dwo_abbrev_table)
6481 {
6482 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6483 struct objfile *objfile = dwarf2_per_objfile->objfile;
6484 struct dwarf2_cu *cu = this_cu->cu;
6485 bfd *abfd;
6486 const gdb_byte *begin_info_ptr, *info_ptr;
6487 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6488 int i,num_extra_attrs;
6489 struct dwarf2_section_info *dwo_abbrev_section;
6490 struct die_info *comp_unit_die;
6491
6492 /* At most one of these may be provided. */
6493 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6494
6495 /* These attributes aren't processed until later:
6496 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6497 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6498 referenced later. However, these attributes are found in the stub
6499 which we won't have later. In order to not impose this complication
6500 on the rest of the code, we read them here and copy them to the
6501 DWO CU/TU die. */
6502
6503 stmt_list = NULL;
6504 low_pc = NULL;
6505 high_pc = NULL;
6506 ranges = NULL;
6507 comp_dir = NULL;
6508
6509 if (stub_comp_unit_die != NULL)
6510 {
6511 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6512 DWO file. */
6513 if (! this_cu->is_debug_types)
6514 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6515 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6516 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6517 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6518 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6519
6520 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6521
6522 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6523 here (if needed). We need the value before we can process
6524 DW_AT_ranges. */
6525 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6526 }
6527 else if (stub_comp_dir != NULL)
6528 {
6529 /* Reconstruct the comp_dir attribute to simplify the code below. */
6530 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6531 comp_dir->name = DW_AT_comp_dir;
6532 comp_dir->form = DW_FORM_string;
6533 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6534 DW_STRING (comp_dir) = stub_comp_dir;
6535 }
6536
6537 /* Set up for reading the DWO CU/TU. */
6538 cu->dwo_unit = dwo_unit;
6539 dwarf2_section_info *section = dwo_unit->section;
6540 section->read (objfile);
6541 abfd = section->get_bfd_owner ();
6542 begin_info_ptr = info_ptr = (section->buffer
6543 + to_underlying (dwo_unit->sect_off));
6544 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6545
6546 if (this_cu->is_debug_types)
6547 {
6548 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6549
6550 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6551 &cu->header, section,
6552 dwo_abbrev_section,
6553 info_ptr, rcuh_kind::TYPE);
6554 /* This is not an assert because it can be caused by bad debug info. */
6555 if (sig_type->signature != cu->header.signature)
6556 {
6557 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6558 " TU at offset %s [in module %s]"),
6559 hex_string (sig_type->signature),
6560 hex_string (cu->header.signature),
6561 sect_offset_str (dwo_unit->sect_off),
6562 bfd_get_filename (abfd));
6563 }
6564 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6565 /* For DWOs coming from DWP files, we don't know the CU length
6566 nor the type's offset in the TU until now. */
6567 dwo_unit->length = cu->header.get_length ();
6568 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6569
6570 /* Establish the type offset that can be used to lookup the type.
6571 For DWO files, we don't know it until now. */
6572 sig_type->type_offset_in_section
6573 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6574 }
6575 else
6576 {
6577 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6578 &cu->header, section,
6579 dwo_abbrev_section,
6580 info_ptr, rcuh_kind::COMPILE);
6581 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6582 /* For DWOs coming from DWP files, we don't know the CU length
6583 until now. */
6584 dwo_unit->length = cu->header.get_length ();
6585 }
6586
6587 *result_dwo_abbrev_table
6588 = abbrev_table::read (objfile, dwo_abbrev_section,
6589 cu->header.abbrev_sect_off);
6590 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6591 result_dwo_abbrev_table->get ());
6592
6593 /* Read in the die, but leave space to copy over the attributes
6594 from the stub. This has the benefit of simplifying the rest of
6595 the code - all the work to maintain the illusion of a single
6596 DW_TAG_{compile,type}_unit DIE is done here. */
6597 num_extra_attrs = ((stmt_list != NULL)
6598 + (low_pc != NULL)
6599 + (high_pc != NULL)
6600 + (ranges != NULL)
6601 + (comp_dir != NULL));
6602 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6603 num_extra_attrs);
6604
6605 /* Copy over the attributes from the stub to the DIE we just read in. */
6606 comp_unit_die = *result_comp_unit_die;
6607 i = comp_unit_die->num_attrs;
6608 if (stmt_list != NULL)
6609 comp_unit_die->attrs[i++] = *stmt_list;
6610 if (low_pc != NULL)
6611 comp_unit_die->attrs[i++] = *low_pc;
6612 if (high_pc != NULL)
6613 comp_unit_die->attrs[i++] = *high_pc;
6614 if (ranges != NULL)
6615 comp_unit_die->attrs[i++] = *ranges;
6616 if (comp_dir != NULL)
6617 comp_unit_die->attrs[i++] = *comp_dir;
6618 comp_unit_die->num_attrs += num_extra_attrs;
6619
6620 if (dwarf_die_debug)
6621 {
6622 fprintf_unfiltered (gdb_stdlog,
6623 "Read die from %s@0x%x of %s:\n",
6624 section->get_name (),
6625 (unsigned) (begin_info_ptr - section->buffer),
6626 bfd_get_filename (abfd));
6627 dump_die (comp_unit_die, dwarf_die_debug);
6628 }
6629
6630 /* Skip dummy compilation units. */
6631 if (info_ptr >= begin_info_ptr + dwo_unit->length
6632 || peek_abbrev_code (abfd, info_ptr) == 0)
6633 return 0;
6634
6635 *result_info_ptr = info_ptr;
6636 return 1;
6637 }
6638
6639 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6640 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6641 signature is part of the header. */
6642 static gdb::optional<ULONGEST>
6643 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6644 {
6645 if (cu->header.version >= 5)
6646 return cu->header.signature;
6647 struct attribute *attr;
6648 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6649 if (attr == nullptr)
6650 return gdb::optional<ULONGEST> ();
6651 return DW_UNSND (attr);
6652 }
6653
6654 /* Subroutine of cutu_reader to simplify it.
6655 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6656 Returns NULL if the specified DWO unit cannot be found. */
6657
6658 static struct dwo_unit *
6659 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6660 struct die_info *comp_unit_die,
6661 const char *dwo_name)
6662 {
6663 struct dwarf2_cu *cu = this_cu->cu;
6664 struct dwo_unit *dwo_unit;
6665 const char *comp_dir;
6666
6667 gdb_assert (cu != NULL);
6668
6669 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6670 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6671 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6672
6673 if (this_cu->is_debug_types)
6674 {
6675 struct signatured_type *sig_type;
6676
6677 /* Since this_cu is the first member of struct signatured_type,
6678 we can go from a pointer to one to a pointer to the other. */
6679 sig_type = (struct signatured_type *) this_cu;
6680 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6681 }
6682 else
6683 {
6684 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6685 if (!signature.has_value ())
6686 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6687 " [in module %s]"),
6688 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6689 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6690 *signature);
6691 }
6692
6693 return dwo_unit;
6694 }
6695
6696 /* Subroutine of cutu_reader to simplify it.
6697 See it for a description of the parameters.
6698 Read a TU directly from a DWO file, bypassing the stub. */
6699
6700 void
6701 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6702 int use_existing_cu)
6703 {
6704 struct signatured_type *sig_type;
6705
6706 /* Verify we can do the following downcast, and that we have the
6707 data we need. */
6708 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6709 sig_type = (struct signatured_type *) this_cu;
6710 gdb_assert (sig_type->dwo_unit != NULL);
6711
6712 if (use_existing_cu && this_cu->cu != NULL)
6713 {
6714 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6715 /* There's no need to do the rereading_dwo_cu handling that
6716 cutu_reader does since we don't read the stub. */
6717 }
6718 else
6719 {
6720 /* If !use_existing_cu, this_cu->cu must be NULL. */
6721 gdb_assert (this_cu->cu == NULL);
6722 m_new_cu.reset (new dwarf2_cu (this_cu));
6723 }
6724
6725 /* A future optimization, if needed, would be to use an existing
6726 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6727 could share abbrev tables. */
6728
6729 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6730 NULL /* stub_comp_unit_die */,
6731 sig_type->dwo_unit->dwo_file->comp_dir,
6732 this, &info_ptr,
6733 &comp_unit_die,
6734 &m_dwo_abbrev_table) == 0)
6735 {
6736 /* Dummy die. */
6737 dummy_p = true;
6738 }
6739 }
6740
6741 /* Initialize a CU (or TU) and read its DIEs.
6742 If the CU defers to a DWO file, read the DWO file as well.
6743
6744 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6745 Otherwise the table specified in the comp unit header is read in and used.
6746 This is an optimization for when we already have the abbrev table.
6747
6748 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6749 Otherwise, a new CU is allocated with xmalloc. */
6750
6751 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6752 struct abbrev_table *abbrev_table,
6753 int use_existing_cu,
6754 bool skip_partial)
6755 : die_reader_specs {},
6756 m_this_cu (this_cu)
6757 {
6758 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6759 struct objfile *objfile = dwarf2_per_objfile->objfile;
6760 struct dwarf2_section_info *section = this_cu->section;
6761 bfd *abfd = section->get_bfd_owner ();
6762 struct dwarf2_cu *cu;
6763 const gdb_byte *begin_info_ptr;
6764 struct signatured_type *sig_type = NULL;
6765 struct dwarf2_section_info *abbrev_section;
6766 /* Non-zero if CU currently points to a DWO file and we need to
6767 reread it. When this happens we need to reread the skeleton die
6768 before we can reread the DWO file (this only applies to CUs, not TUs). */
6769 int rereading_dwo_cu = 0;
6770
6771 if (dwarf_die_debug)
6772 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6773 this_cu->is_debug_types ? "type" : "comp",
6774 sect_offset_str (this_cu->sect_off));
6775
6776 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6777 file (instead of going through the stub), short-circuit all of this. */
6778 if (this_cu->reading_dwo_directly)
6779 {
6780 /* Narrow down the scope of possibilities to have to understand. */
6781 gdb_assert (this_cu->is_debug_types);
6782 gdb_assert (abbrev_table == NULL);
6783 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6784 return;
6785 }
6786
6787 /* This is cheap if the section is already read in. */
6788 section->read (objfile);
6789
6790 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6791
6792 abbrev_section = get_abbrev_section_for_cu (this_cu);
6793
6794 if (use_existing_cu && this_cu->cu != NULL)
6795 {
6796 cu = this_cu->cu;
6797 /* If this CU is from a DWO file we need to start over, we need to
6798 refetch the attributes from the skeleton CU.
6799 This could be optimized by retrieving those attributes from when we
6800 were here the first time: the previous comp_unit_die was stored in
6801 comp_unit_obstack. But there's no data yet that we need this
6802 optimization. */
6803 if (cu->dwo_unit != NULL)
6804 rereading_dwo_cu = 1;
6805 }
6806 else
6807 {
6808 /* If !use_existing_cu, this_cu->cu must be NULL. */
6809 gdb_assert (this_cu->cu == NULL);
6810 m_new_cu.reset (new dwarf2_cu (this_cu));
6811 cu = m_new_cu.get ();
6812 }
6813
6814 /* Get the header. */
6815 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6816 {
6817 /* We already have the header, there's no need to read it in again. */
6818 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6819 }
6820 else
6821 {
6822 if (this_cu->is_debug_types)
6823 {
6824 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6825 &cu->header, section,
6826 abbrev_section, info_ptr,
6827 rcuh_kind::TYPE);
6828
6829 /* Since per_cu is the first member of struct signatured_type,
6830 we can go from a pointer to one to a pointer to the other. */
6831 sig_type = (struct signatured_type *) this_cu;
6832 gdb_assert (sig_type->signature == cu->header.signature);
6833 gdb_assert (sig_type->type_offset_in_tu
6834 == cu->header.type_cu_offset_in_tu);
6835 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6836
6837 /* LENGTH has not been set yet for type units if we're
6838 using .gdb_index. */
6839 this_cu->length = cu->header.get_length ();
6840
6841 /* Establish the type offset that can be used to lookup the type. */
6842 sig_type->type_offset_in_section =
6843 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6844
6845 this_cu->dwarf_version = cu->header.version;
6846 }
6847 else
6848 {
6849 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6850 &cu->header, section,
6851 abbrev_section,
6852 info_ptr,
6853 rcuh_kind::COMPILE);
6854
6855 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6856 gdb_assert (this_cu->length == cu->header.get_length ());
6857 this_cu->dwarf_version = cu->header.version;
6858 }
6859 }
6860
6861 /* Skip dummy compilation units. */
6862 if (info_ptr >= begin_info_ptr + this_cu->length
6863 || peek_abbrev_code (abfd, info_ptr) == 0)
6864 {
6865 dummy_p = true;
6866 return;
6867 }
6868
6869 /* If we don't have them yet, read the abbrevs for this compilation unit.
6870 And if we need to read them now, make sure they're freed when we're
6871 done. */
6872 if (abbrev_table != NULL)
6873 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6874 else
6875 {
6876 m_abbrev_table_holder
6877 = abbrev_table::read (objfile, abbrev_section,
6878 cu->header.abbrev_sect_off);
6879 abbrev_table = m_abbrev_table_holder.get ();
6880 }
6881
6882 /* Read the top level CU/TU die. */
6883 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6884 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6885
6886 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6887 {
6888 dummy_p = true;
6889 return;
6890 }
6891
6892 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6893 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6894 table from the DWO file and pass the ownership over to us. It will be
6895 referenced from READER, so we must make sure to free it after we're done
6896 with READER.
6897
6898 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6899 DWO CU, that this test will fail (the attribute will not be present). */
6900 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6901 if (dwo_name != nullptr)
6902 {
6903 struct dwo_unit *dwo_unit;
6904 struct die_info *dwo_comp_unit_die;
6905
6906 if (comp_unit_die->has_children)
6907 {
6908 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6909 " has children (offset %s) [in module %s]"),
6910 sect_offset_str (this_cu->sect_off),
6911 bfd_get_filename (abfd));
6912 }
6913 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6914 if (dwo_unit != NULL)
6915 {
6916 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6917 comp_unit_die, NULL,
6918 this, &info_ptr,
6919 &dwo_comp_unit_die,
6920 &m_dwo_abbrev_table) == 0)
6921 {
6922 /* Dummy die. */
6923 dummy_p = true;
6924 return;
6925 }
6926 comp_unit_die = dwo_comp_unit_die;
6927 }
6928 else
6929 {
6930 /* Yikes, we couldn't find the rest of the DIE, we only have
6931 the stub. A complaint has already been logged. There's
6932 not much more we can do except pass on the stub DIE to
6933 die_reader_func. We don't want to throw an error on bad
6934 debug info. */
6935 }
6936 }
6937 }
6938
6939 void
6940 cutu_reader::keep ()
6941 {
6942 /* Done, clean up. */
6943 gdb_assert (!dummy_p);
6944 if (m_new_cu != NULL)
6945 {
6946 struct dwarf2_per_objfile *dwarf2_per_objfile
6947 = m_this_cu->dwarf2_per_objfile;
6948 /* Link this CU into read_in_chain. */
6949 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6950 dwarf2_per_objfile->read_in_chain = m_this_cu;
6951 /* The chain owns it now. */
6952 m_new_cu.release ();
6953 }
6954 }
6955
6956 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6957 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6958 assumed to have already done the lookup to find the DWO file).
6959
6960 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6961 THIS_CU->is_debug_types, but nothing else.
6962
6963 We fill in THIS_CU->length.
6964
6965 THIS_CU->cu is always freed when done.
6966 This is done in order to not leave THIS_CU->cu in a state where we have
6967 to care whether it refers to the "main" CU or the DWO CU.
6968
6969 When parent_cu is passed, it is used to provide a default value for
6970 str_offsets_base and addr_base from the parent. */
6971
6972 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6973 struct dwarf2_cu *parent_cu,
6974 struct dwo_file *dwo_file)
6975 : die_reader_specs {},
6976 m_this_cu (this_cu)
6977 {
6978 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6979 struct objfile *objfile = dwarf2_per_objfile->objfile;
6980 struct dwarf2_section_info *section = this_cu->section;
6981 bfd *abfd = section->get_bfd_owner ();
6982 struct dwarf2_section_info *abbrev_section;
6983 const gdb_byte *begin_info_ptr, *info_ptr;
6984
6985 if (dwarf_die_debug)
6986 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6987 this_cu->is_debug_types ? "type" : "comp",
6988 sect_offset_str (this_cu->sect_off));
6989
6990 gdb_assert (this_cu->cu == NULL);
6991
6992 abbrev_section = (dwo_file != NULL
6993 ? &dwo_file->sections.abbrev
6994 : get_abbrev_section_for_cu (this_cu));
6995
6996 /* This is cheap if the section is already read in. */
6997 section->read (objfile);
6998
6999 m_new_cu.reset (new dwarf2_cu (this_cu));
7000
7001 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7002 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7003 &m_new_cu->header, section,
7004 abbrev_section, info_ptr,
7005 (this_cu->is_debug_types
7006 ? rcuh_kind::TYPE
7007 : rcuh_kind::COMPILE));
7008
7009 if (parent_cu != nullptr)
7010 {
7011 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7012 m_new_cu->addr_base = parent_cu->addr_base;
7013 }
7014 this_cu->length = m_new_cu->header.get_length ();
7015
7016 /* Skip dummy compilation units. */
7017 if (info_ptr >= begin_info_ptr + this_cu->length
7018 || peek_abbrev_code (abfd, info_ptr) == 0)
7019 {
7020 dummy_p = true;
7021 return;
7022 }
7023
7024 m_abbrev_table_holder
7025 = abbrev_table::read (objfile, abbrev_section,
7026 m_new_cu->header.abbrev_sect_off);
7027
7028 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7029 m_abbrev_table_holder.get ());
7030 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7031 }
7032
7033 \f
7034 /* Type Unit Groups.
7035
7036 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7037 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7038 so that all types coming from the same compilation (.o file) are grouped
7039 together. A future step could be to put the types in the same symtab as
7040 the CU the types ultimately came from. */
7041
7042 static hashval_t
7043 hash_type_unit_group (const void *item)
7044 {
7045 const struct type_unit_group *tu_group
7046 = (const struct type_unit_group *) item;
7047
7048 return hash_stmt_list_entry (&tu_group->hash);
7049 }
7050
7051 static int
7052 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7053 {
7054 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7055 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7056
7057 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7058 }
7059
7060 /* Allocate a hash table for type unit groups. */
7061
7062 static htab_up
7063 allocate_type_unit_groups_table ()
7064 {
7065 return htab_up (htab_create_alloc (3,
7066 hash_type_unit_group,
7067 eq_type_unit_group,
7068 NULL, xcalloc, xfree));
7069 }
7070
7071 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7072 partial symtabs. We combine several TUs per psymtab to not let the size
7073 of any one psymtab grow too big. */
7074 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7075 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7076
7077 /* Helper routine for get_type_unit_group.
7078 Create the type_unit_group object used to hold one or more TUs. */
7079
7080 static struct type_unit_group *
7081 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7082 {
7083 struct dwarf2_per_objfile *dwarf2_per_objfile
7084 = cu->per_cu->dwarf2_per_objfile;
7085 struct objfile *objfile = dwarf2_per_objfile->objfile;
7086 struct dwarf2_per_cu_data *per_cu;
7087 struct type_unit_group *tu_group;
7088
7089 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7090 struct type_unit_group);
7091 per_cu = &tu_group->per_cu;
7092 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7093
7094 if (dwarf2_per_objfile->using_index)
7095 {
7096 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7097 struct dwarf2_per_cu_quick_data);
7098 }
7099 else
7100 {
7101 unsigned int line_offset = to_underlying (line_offset_struct);
7102 dwarf2_psymtab *pst;
7103 std::string name;
7104
7105 /* Give the symtab a useful name for debug purposes. */
7106 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7107 name = string_printf ("<type_units_%d>",
7108 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7109 else
7110 name = string_printf ("<type_units_at_0x%x>", line_offset);
7111
7112 pst = create_partial_symtab (per_cu, name.c_str ());
7113 pst->anonymous = true;
7114 }
7115
7116 tu_group->hash.dwo_unit = cu->dwo_unit;
7117 tu_group->hash.line_sect_off = line_offset_struct;
7118
7119 return tu_group;
7120 }
7121
7122 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7123 STMT_LIST is a DW_AT_stmt_list attribute. */
7124
7125 static struct type_unit_group *
7126 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7127 {
7128 struct dwarf2_per_objfile *dwarf2_per_objfile
7129 = cu->per_cu->dwarf2_per_objfile;
7130 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7131 struct type_unit_group *tu_group;
7132 void **slot;
7133 unsigned int line_offset;
7134 struct type_unit_group type_unit_group_for_lookup;
7135
7136 if (dwarf2_per_objfile->type_unit_groups == NULL)
7137 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7138
7139 /* Do we need to create a new group, or can we use an existing one? */
7140
7141 if (stmt_list)
7142 {
7143 line_offset = DW_UNSND (stmt_list);
7144 ++tu_stats->nr_symtab_sharers;
7145 }
7146 else
7147 {
7148 /* Ugh, no stmt_list. Rare, but we have to handle it.
7149 We can do various things here like create one group per TU or
7150 spread them over multiple groups to split up the expansion work.
7151 To avoid worst case scenarios (too many groups or too large groups)
7152 we, umm, group them in bunches. */
7153 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7154 | (tu_stats->nr_stmt_less_type_units
7155 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7156 ++tu_stats->nr_stmt_less_type_units;
7157 }
7158
7159 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7160 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7161 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7162 &type_unit_group_for_lookup, INSERT);
7163 if (*slot != NULL)
7164 {
7165 tu_group = (struct type_unit_group *) *slot;
7166 gdb_assert (tu_group != NULL);
7167 }
7168 else
7169 {
7170 sect_offset line_offset_struct = (sect_offset) line_offset;
7171 tu_group = create_type_unit_group (cu, line_offset_struct);
7172 *slot = tu_group;
7173 ++tu_stats->nr_symtabs;
7174 }
7175
7176 return tu_group;
7177 }
7178 \f
7179 /* Partial symbol tables. */
7180
7181 /* Create a psymtab named NAME and assign it to PER_CU.
7182
7183 The caller must fill in the following details:
7184 dirname, textlow, texthigh. */
7185
7186 static dwarf2_psymtab *
7187 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7188 {
7189 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7190 dwarf2_psymtab *pst;
7191
7192 pst = new dwarf2_psymtab (name, objfile, 0);
7193
7194 pst->psymtabs_addrmap_supported = true;
7195
7196 /* This is the glue that links PST into GDB's symbol API. */
7197 pst->per_cu_data = per_cu;
7198 per_cu->v.psymtab = pst;
7199
7200 return pst;
7201 }
7202
7203 /* DIE reader function for process_psymtab_comp_unit. */
7204
7205 static void
7206 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7207 const gdb_byte *info_ptr,
7208 struct die_info *comp_unit_die,
7209 enum language pretend_language)
7210 {
7211 struct dwarf2_cu *cu = reader->cu;
7212 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7213 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7214 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7215 CORE_ADDR baseaddr;
7216 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7217 dwarf2_psymtab *pst;
7218 enum pc_bounds_kind cu_bounds_kind;
7219 const char *filename;
7220
7221 gdb_assert (! per_cu->is_debug_types);
7222
7223 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7224
7225 /* Allocate a new partial symbol table structure. */
7226 gdb::unique_xmalloc_ptr<char> debug_filename;
7227 static const char artificial[] = "<artificial>";
7228 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7229 if (filename == NULL)
7230 filename = "";
7231 else if (strcmp (filename, artificial) == 0)
7232 {
7233 debug_filename.reset (concat (artificial, "@",
7234 sect_offset_str (per_cu->sect_off),
7235 (char *) NULL));
7236 filename = debug_filename.get ();
7237 }
7238
7239 pst = create_partial_symtab (per_cu, filename);
7240
7241 /* This must be done before calling dwarf2_build_include_psymtabs. */
7242 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7243
7244 baseaddr = objfile->text_section_offset ();
7245
7246 dwarf2_find_base_address (comp_unit_die, cu);
7247
7248 /* Possibly set the default values of LOWPC and HIGHPC from
7249 `DW_AT_ranges'. */
7250 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7251 &best_highpc, cu, pst);
7252 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7253 {
7254 CORE_ADDR low
7255 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7256 - baseaddr);
7257 CORE_ADDR high
7258 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7259 - baseaddr - 1);
7260 /* Store the contiguous range if it is not empty; it can be
7261 empty for CUs with no code. */
7262 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7263 low, high, pst);
7264 }
7265
7266 /* Check if comp unit has_children.
7267 If so, read the rest of the partial symbols from this comp unit.
7268 If not, there's no more debug_info for this comp unit. */
7269 if (comp_unit_die->has_children)
7270 {
7271 struct partial_die_info *first_die;
7272 CORE_ADDR lowpc, highpc;
7273
7274 lowpc = ((CORE_ADDR) -1);
7275 highpc = ((CORE_ADDR) 0);
7276
7277 first_die = load_partial_dies (reader, info_ptr, 1);
7278
7279 scan_partial_symbols (first_die, &lowpc, &highpc,
7280 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7281
7282 /* If we didn't find a lowpc, set it to highpc to avoid
7283 complaints from `maint check'. */
7284 if (lowpc == ((CORE_ADDR) -1))
7285 lowpc = highpc;
7286
7287 /* If the compilation unit didn't have an explicit address range,
7288 then use the information extracted from its child dies. */
7289 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7290 {
7291 best_lowpc = lowpc;
7292 best_highpc = highpc;
7293 }
7294 }
7295 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7296 best_lowpc + baseaddr)
7297 - baseaddr);
7298 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7299 best_highpc + baseaddr)
7300 - baseaddr);
7301
7302 end_psymtab_common (objfile, pst);
7303
7304 if (!cu->per_cu->imported_symtabs_empty ())
7305 {
7306 int i;
7307 int len = cu->per_cu->imported_symtabs_size ();
7308
7309 /* Fill in 'dependencies' here; we fill in 'users' in a
7310 post-pass. */
7311 pst->number_of_dependencies = len;
7312 pst->dependencies
7313 = objfile->partial_symtabs->allocate_dependencies (len);
7314 for (i = 0; i < len; ++i)
7315 {
7316 pst->dependencies[i]
7317 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7318 }
7319
7320 cu->per_cu->imported_symtabs_free ();
7321 }
7322
7323 /* Get the list of files included in the current compilation unit,
7324 and build a psymtab for each of them. */
7325 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7326
7327 if (dwarf_read_debug)
7328 fprintf_unfiltered (gdb_stdlog,
7329 "Psymtab for %s unit @%s: %s - %s"
7330 ", %d global, %d static syms\n",
7331 per_cu->is_debug_types ? "type" : "comp",
7332 sect_offset_str (per_cu->sect_off),
7333 paddress (gdbarch, pst->text_low (objfile)),
7334 paddress (gdbarch, pst->text_high (objfile)),
7335 pst->n_global_syms, pst->n_static_syms);
7336 }
7337
7338 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7339 Process compilation unit THIS_CU for a psymtab. */
7340
7341 static void
7342 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7343 bool want_partial_unit,
7344 enum language pretend_language)
7345 {
7346 /* If this compilation unit was already read in, free the
7347 cached copy in order to read it in again. This is
7348 necessary because we skipped some symbols when we first
7349 read in the compilation unit (see load_partial_dies).
7350 This problem could be avoided, but the benefit is unclear. */
7351 if (this_cu->cu != NULL)
7352 free_one_cached_comp_unit (this_cu);
7353
7354 cutu_reader reader (this_cu, NULL, 0, false);
7355
7356 switch (reader.comp_unit_die->tag)
7357 {
7358 case DW_TAG_compile_unit:
7359 this_cu->unit_type = DW_UT_compile;
7360 break;
7361 case DW_TAG_partial_unit:
7362 this_cu->unit_type = DW_UT_partial;
7363 break;
7364 default:
7365 abort ();
7366 }
7367
7368 if (reader.dummy_p)
7369 {
7370 /* Nothing. */
7371 }
7372 else if (this_cu->is_debug_types)
7373 build_type_psymtabs_reader (&reader, reader.info_ptr,
7374 reader.comp_unit_die);
7375 else if (want_partial_unit
7376 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7377 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7378 reader.comp_unit_die,
7379 pretend_language);
7380
7381 this_cu->lang = this_cu->cu->language;
7382
7383 /* Age out any secondary CUs. */
7384 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7385 }
7386
7387 /* Reader function for build_type_psymtabs. */
7388
7389 static void
7390 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7391 const gdb_byte *info_ptr,
7392 struct die_info *type_unit_die)
7393 {
7394 struct dwarf2_per_objfile *dwarf2_per_objfile
7395 = reader->cu->per_cu->dwarf2_per_objfile;
7396 struct objfile *objfile = dwarf2_per_objfile->objfile;
7397 struct dwarf2_cu *cu = reader->cu;
7398 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7399 struct signatured_type *sig_type;
7400 struct type_unit_group *tu_group;
7401 struct attribute *attr;
7402 struct partial_die_info *first_die;
7403 CORE_ADDR lowpc, highpc;
7404 dwarf2_psymtab *pst;
7405
7406 gdb_assert (per_cu->is_debug_types);
7407 sig_type = (struct signatured_type *) per_cu;
7408
7409 if (! type_unit_die->has_children)
7410 return;
7411
7412 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7413 tu_group = get_type_unit_group (cu, attr);
7414
7415 if (tu_group->tus == nullptr)
7416 tu_group->tus = new std::vector<signatured_type *>;
7417 tu_group->tus->push_back (sig_type);
7418
7419 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7420 pst = create_partial_symtab (per_cu, "");
7421 pst->anonymous = true;
7422
7423 first_die = load_partial_dies (reader, info_ptr, 1);
7424
7425 lowpc = (CORE_ADDR) -1;
7426 highpc = (CORE_ADDR) 0;
7427 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7428
7429 end_psymtab_common (objfile, pst);
7430 }
7431
7432 /* Struct used to sort TUs by their abbreviation table offset. */
7433
7434 struct tu_abbrev_offset
7435 {
7436 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7437 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7438 {}
7439
7440 signatured_type *sig_type;
7441 sect_offset abbrev_offset;
7442 };
7443
7444 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7445
7446 static bool
7447 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7448 const struct tu_abbrev_offset &b)
7449 {
7450 return a.abbrev_offset < b.abbrev_offset;
7451 }
7452
7453 /* Efficiently read all the type units.
7454 This does the bulk of the work for build_type_psymtabs.
7455
7456 The efficiency is because we sort TUs by the abbrev table they use and
7457 only read each abbrev table once. In one program there are 200K TUs
7458 sharing 8K abbrev tables.
7459
7460 The main purpose of this function is to support building the
7461 dwarf2_per_objfile->type_unit_groups table.
7462 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7463 can collapse the search space by grouping them by stmt_list.
7464 The savings can be significant, in the same program from above the 200K TUs
7465 share 8K stmt_list tables.
7466
7467 FUNC is expected to call get_type_unit_group, which will create the
7468 struct type_unit_group if necessary and add it to
7469 dwarf2_per_objfile->type_unit_groups. */
7470
7471 static void
7472 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7473 {
7474 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7475 abbrev_table_up abbrev_table;
7476 sect_offset abbrev_offset;
7477
7478 /* It's up to the caller to not call us multiple times. */
7479 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7480
7481 if (dwarf2_per_objfile->all_type_units.empty ())
7482 return;
7483
7484 /* TUs typically share abbrev tables, and there can be way more TUs than
7485 abbrev tables. Sort by abbrev table to reduce the number of times we
7486 read each abbrev table in.
7487 Alternatives are to punt or to maintain a cache of abbrev tables.
7488 This is simpler and efficient enough for now.
7489
7490 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7491 symtab to use). Typically TUs with the same abbrev offset have the same
7492 stmt_list value too so in practice this should work well.
7493
7494 The basic algorithm here is:
7495
7496 sort TUs by abbrev table
7497 for each TU with same abbrev table:
7498 read abbrev table if first user
7499 read TU top level DIE
7500 [IWBN if DWO skeletons had DW_AT_stmt_list]
7501 call FUNC */
7502
7503 if (dwarf_read_debug)
7504 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7505
7506 /* Sort in a separate table to maintain the order of all_type_units
7507 for .gdb_index: TU indices directly index all_type_units. */
7508 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7509 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7510
7511 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7512 sorted_by_abbrev.emplace_back
7513 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7514 sig_type->per_cu.section,
7515 sig_type->per_cu.sect_off));
7516
7517 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7518 sort_tu_by_abbrev_offset);
7519
7520 abbrev_offset = (sect_offset) ~(unsigned) 0;
7521
7522 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7523 {
7524 /* Switch to the next abbrev table if necessary. */
7525 if (abbrev_table == NULL
7526 || tu.abbrev_offset != abbrev_offset)
7527 {
7528 abbrev_offset = tu.abbrev_offset;
7529 abbrev_table =
7530 abbrev_table::read (dwarf2_per_objfile->objfile,
7531 &dwarf2_per_objfile->abbrev,
7532 abbrev_offset);
7533 ++tu_stats->nr_uniq_abbrev_tables;
7534 }
7535
7536 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7537 0, false);
7538 if (!reader.dummy_p)
7539 build_type_psymtabs_reader (&reader, reader.info_ptr,
7540 reader.comp_unit_die);
7541 }
7542 }
7543
7544 /* Print collected type unit statistics. */
7545
7546 static void
7547 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7548 {
7549 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7550
7551 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7552 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7553 dwarf2_per_objfile->all_type_units.size ());
7554 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7555 tu_stats->nr_uniq_abbrev_tables);
7556 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7557 tu_stats->nr_symtabs);
7558 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7559 tu_stats->nr_symtab_sharers);
7560 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7561 tu_stats->nr_stmt_less_type_units);
7562 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7563 tu_stats->nr_all_type_units_reallocs);
7564 }
7565
7566 /* Traversal function for build_type_psymtabs. */
7567
7568 static int
7569 build_type_psymtab_dependencies (void **slot, void *info)
7570 {
7571 struct dwarf2_per_objfile *dwarf2_per_objfile
7572 = (struct dwarf2_per_objfile *) info;
7573 struct objfile *objfile = dwarf2_per_objfile->objfile;
7574 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7575 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7576 dwarf2_psymtab *pst = per_cu->v.psymtab;
7577 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7578 int i;
7579
7580 gdb_assert (len > 0);
7581 gdb_assert (per_cu->type_unit_group_p ());
7582
7583 pst->number_of_dependencies = len;
7584 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7585 for (i = 0; i < len; ++i)
7586 {
7587 struct signatured_type *iter = tu_group->tus->at (i);
7588 gdb_assert (iter->per_cu.is_debug_types);
7589 pst->dependencies[i] = iter->per_cu.v.psymtab;
7590 iter->type_unit_group = tu_group;
7591 }
7592
7593 delete tu_group->tus;
7594 tu_group->tus = nullptr;
7595
7596 return 1;
7597 }
7598
7599 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7600 Build partial symbol tables for the .debug_types comp-units. */
7601
7602 static void
7603 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7604 {
7605 if (! create_all_type_units (dwarf2_per_objfile))
7606 return;
7607
7608 build_type_psymtabs_1 (dwarf2_per_objfile);
7609 }
7610
7611 /* Traversal function for process_skeletonless_type_unit.
7612 Read a TU in a DWO file and build partial symbols for it. */
7613
7614 static int
7615 process_skeletonless_type_unit (void **slot, void *info)
7616 {
7617 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7618 struct dwarf2_per_objfile *dwarf2_per_objfile
7619 = (struct dwarf2_per_objfile *) info;
7620 struct signatured_type find_entry, *entry;
7621
7622 /* If this TU doesn't exist in the global table, add it and read it in. */
7623
7624 if (dwarf2_per_objfile->signatured_types == NULL)
7625 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7626
7627 find_entry.signature = dwo_unit->signature;
7628 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7629 &find_entry, INSERT);
7630 /* If we've already seen this type there's nothing to do. What's happening
7631 is we're doing our own version of comdat-folding here. */
7632 if (*slot != NULL)
7633 return 1;
7634
7635 /* This does the job that create_all_type_units would have done for
7636 this TU. */
7637 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7638 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7639 *slot = entry;
7640
7641 /* This does the job that build_type_psymtabs_1 would have done. */
7642 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7643 if (!reader.dummy_p)
7644 build_type_psymtabs_reader (&reader, reader.info_ptr,
7645 reader.comp_unit_die);
7646
7647 return 1;
7648 }
7649
7650 /* Traversal function for process_skeletonless_type_units. */
7651
7652 static int
7653 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7654 {
7655 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7656
7657 if (dwo_file->tus != NULL)
7658 htab_traverse_noresize (dwo_file->tus.get (),
7659 process_skeletonless_type_unit, info);
7660
7661 return 1;
7662 }
7663
7664 /* Scan all TUs of DWO files, verifying we've processed them.
7665 This is needed in case a TU was emitted without its skeleton.
7666 Note: This can't be done until we know what all the DWO files are. */
7667
7668 static void
7669 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7670 {
7671 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7672 if (get_dwp_file (dwarf2_per_objfile) == NULL
7673 && dwarf2_per_objfile->dwo_files != NULL)
7674 {
7675 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7676 process_dwo_file_for_skeletonless_type_units,
7677 dwarf2_per_objfile);
7678 }
7679 }
7680
7681 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7682
7683 static void
7684 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7685 {
7686 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7687 {
7688 dwarf2_psymtab *pst = per_cu->v.psymtab;
7689
7690 if (pst == NULL)
7691 continue;
7692
7693 for (int j = 0; j < pst->number_of_dependencies; ++j)
7694 {
7695 /* Set the 'user' field only if it is not already set. */
7696 if (pst->dependencies[j]->user == NULL)
7697 pst->dependencies[j]->user = pst;
7698 }
7699 }
7700 }
7701
7702 /* Build the partial symbol table by doing a quick pass through the
7703 .debug_info and .debug_abbrev sections. */
7704
7705 static void
7706 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7707 {
7708 struct objfile *objfile = dwarf2_per_objfile->objfile;
7709
7710 if (dwarf_read_debug)
7711 {
7712 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7713 objfile_name (objfile));
7714 }
7715
7716 scoped_restore restore_reading_psyms
7717 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7718 true);
7719
7720 dwarf2_per_objfile->info.read (objfile);
7721
7722 /* Any cached compilation units will be linked by the per-objfile
7723 read_in_chain. Make sure to free them when we're done. */
7724 free_cached_comp_units freer (dwarf2_per_objfile);
7725
7726 build_type_psymtabs (dwarf2_per_objfile);
7727
7728 create_all_comp_units (dwarf2_per_objfile);
7729
7730 /* Create a temporary address map on a temporary obstack. We later
7731 copy this to the final obstack. */
7732 auto_obstack temp_obstack;
7733
7734 scoped_restore save_psymtabs_addrmap
7735 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7736 addrmap_create_mutable (&temp_obstack));
7737
7738 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7739 process_psymtab_comp_unit (per_cu, false, language_minimal);
7740
7741 /* This has to wait until we read the CUs, we need the list of DWOs. */
7742 process_skeletonless_type_units (dwarf2_per_objfile);
7743
7744 /* Now that all TUs have been processed we can fill in the dependencies. */
7745 if (dwarf2_per_objfile->type_unit_groups != NULL)
7746 {
7747 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7748 build_type_psymtab_dependencies, dwarf2_per_objfile);
7749 }
7750
7751 if (dwarf_read_debug)
7752 print_tu_stats (dwarf2_per_objfile);
7753
7754 set_partial_user (dwarf2_per_objfile);
7755
7756 objfile->partial_symtabs->psymtabs_addrmap
7757 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7758 objfile->partial_symtabs->obstack ());
7759 /* At this point we want to keep the address map. */
7760 save_psymtabs_addrmap.release ();
7761
7762 if (dwarf_read_debug)
7763 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7764 objfile_name (objfile));
7765 }
7766
7767 /* Load the partial DIEs for a secondary CU into memory.
7768 This is also used when rereading a primary CU with load_all_dies. */
7769
7770 static void
7771 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7772 {
7773 cutu_reader reader (this_cu, NULL, 1, false);
7774
7775 if (!reader.dummy_p)
7776 {
7777 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7778 language_minimal);
7779
7780 /* Check if comp unit has_children.
7781 If so, read the rest of the partial symbols from this comp unit.
7782 If not, there's no more debug_info for this comp unit. */
7783 if (reader.comp_unit_die->has_children)
7784 load_partial_dies (&reader, reader.info_ptr, 0);
7785
7786 reader.keep ();
7787 }
7788 }
7789
7790 static void
7791 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7792 struct dwarf2_section_info *section,
7793 struct dwarf2_section_info *abbrev_section,
7794 unsigned int is_dwz)
7795 {
7796 const gdb_byte *info_ptr;
7797 struct objfile *objfile = dwarf2_per_objfile->objfile;
7798
7799 if (dwarf_read_debug)
7800 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7801 section->get_name (),
7802 section->get_file_name ());
7803
7804 section->read (objfile);
7805
7806 info_ptr = section->buffer;
7807
7808 while (info_ptr < section->buffer + section->size)
7809 {
7810 struct dwarf2_per_cu_data *this_cu;
7811
7812 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7813
7814 comp_unit_head cu_header;
7815 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7816 abbrev_section, info_ptr,
7817 rcuh_kind::COMPILE);
7818
7819 /* Save the compilation unit for later lookup. */
7820 if (cu_header.unit_type != DW_UT_type)
7821 {
7822 this_cu = XOBNEW (&objfile->objfile_obstack,
7823 struct dwarf2_per_cu_data);
7824 memset (this_cu, 0, sizeof (*this_cu));
7825 }
7826 else
7827 {
7828 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7829 struct signatured_type);
7830 memset (sig_type, 0, sizeof (*sig_type));
7831 sig_type->signature = cu_header.signature;
7832 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7833 this_cu = &sig_type->per_cu;
7834 }
7835 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7836 this_cu->sect_off = sect_off;
7837 this_cu->length = cu_header.length + cu_header.initial_length_size;
7838 this_cu->is_dwz = is_dwz;
7839 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7840 this_cu->section = section;
7841
7842 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7843
7844 info_ptr = info_ptr + this_cu->length;
7845 }
7846 }
7847
7848 /* Create a list of all compilation units in OBJFILE.
7849 This is only done for -readnow and building partial symtabs. */
7850
7851 static void
7852 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7853 {
7854 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7855 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7856 &dwarf2_per_objfile->abbrev, 0);
7857
7858 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7859 if (dwz != NULL)
7860 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7861 1);
7862 }
7863
7864 /* Process all loaded DIEs for compilation unit CU, starting at
7865 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7866 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7867 DW_AT_ranges). See the comments of add_partial_subprogram on how
7868 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7869
7870 static void
7871 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7872 CORE_ADDR *highpc, int set_addrmap,
7873 struct dwarf2_cu *cu)
7874 {
7875 struct partial_die_info *pdi;
7876
7877 /* Now, march along the PDI's, descending into ones which have
7878 interesting children but skipping the children of the other ones,
7879 until we reach the end of the compilation unit. */
7880
7881 pdi = first_die;
7882
7883 while (pdi != NULL)
7884 {
7885 pdi->fixup (cu);
7886
7887 /* Anonymous namespaces or modules have no name but have interesting
7888 children, so we need to look at them. Ditto for anonymous
7889 enums. */
7890
7891 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7892 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7893 || pdi->tag == DW_TAG_imported_unit
7894 || pdi->tag == DW_TAG_inlined_subroutine)
7895 {
7896 switch (pdi->tag)
7897 {
7898 case DW_TAG_subprogram:
7899 case DW_TAG_inlined_subroutine:
7900 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7901 break;
7902 case DW_TAG_constant:
7903 case DW_TAG_variable:
7904 case DW_TAG_typedef:
7905 case DW_TAG_union_type:
7906 if (!pdi->is_declaration)
7907 {
7908 add_partial_symbol (pdi, cu);
7909 }
7910 break;
7911 case DW_TAG_class_type:
7912 case DW_TAG_interface_type:
7913 case DW_TAG_structure_type:
7914 if (!pdi->is_declaration)
7915 {
7916 add_partial_symbol (pdi, cu);
7917 }
7918 if ((cu->language == language_rust
7919 || cu->language == language_cplus) && pdi->has_children)
7920 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7921 set_addrmap, cu);
7922 break;
7923 case DW_TAG_enumeration_type:
7924 if (!pdi->is_declaration)
7925 add_partial_enumeration (pdi, cu);
7926 break;
7927 case DW_TAG_base_type:
7928 case DW_TAG_subrange_type:
7929 /* File scope base type definitions are added to the partial
7930 symbol table. */
7931 add_partial_symbol (pdi, cu);
7932 break;
7933 case DW_TAG_namespace:
7934 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7935 break;
7936 case DW_TAG_module:
7937 if (!pdi->is_declaration)
7938 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7939 break;
7940 case DW_TAG_imported_unit:
7941 {
7942 struct dwarf2_per_cu_data *per_cu;
7943
7944 /* For now we don't handle imported units in type units. */
7945 if (cu->per_cu->is_debug_types)
7946 {
7947 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7948 " supported in type units [in module %s]"),
7949 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7950 }
7951
7952 per_cu = dwarf2_find_containing_comp_unit
7953 (pdi->d.sect_off, pdi->is_dwz,
7954 cu->per_cu->dwarf2_per_objfile);
7955
7956 /* Go read the partial unit, if needed. */
7957 if (per_cu->v.psymtab == NULL)
7958 process_psymtab_comp_unit (per_cu, true, cu->language);
7959
7960 cu->per_cu->imported_symtabs_push (per_cu);
7961 }
7962 break;
7963 case DW_TAG_imported_declaration:
7964 add_partial_symbol (pdi, cu);
7965 break;
7966 default:
7967 break;
7968 }
7969 }
7970
7971 /* If the die has a sibling, skip to the sibling. */
7972
7973 pdi = pdi->die_sibling;
7974 }
7975 }
7976
7977 /* Functions used to compute the fully scoped name of a partial DIE.
7978
7979 Normally, this is simple. For C++, the parent DIE's fully scoped
7980 name is concatenated with "::" and the partial DIE's name.
7981 Enumerators are an exception; they use the scope of their parent
7982 enumeration type, i.e. the name of the enumeration type is not
7983 prepended to the enumerator.
7984
7985 There are two complexities. One is DW_AT_specification; in this
7986 case "parent" means the parent of the target of the specification,
7987 instead of the direct parent of the DIE. The other is compilers
7988 which do not emit DW_TAG_namespace; in this case we try to guess
7989 the fully qualified name of structure types from their members'
7990 linkage names. This must be done using the DIE's children rather
7991 than the children of any DW_AT_specification target. We only need
7992 to do this for structures at the top level, i.e. if the target of
7993 any DW_AT_specification (if any; otherwise the DIE itself) does not
7994 have a parent. */
7995
7996 /* Compute the scope prefix associated with PDI's parent, in
7997 compilation unit CU. The result will be allocated on CU's
7998 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7999 field. NULL is returned if no prefix is necessary. */
8000 static const char *
8001 partial_die_parent_scope (struct partial_die_info *pdi,
8002 struct dwarf2_cu *cu)
8003 {
8004 const char *grandparent_scope;
8005 struct partial_die_info *parent, *real_pdi;
8006
8007 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8008 then this means the parent of the specification DIE. */
8009
8010 real_pdi = pdi;
8011 while (real_pdi->has_specification)
8012 {
8013 auto res = find_partial_die (real_pdi->spec_offset,
8014 real_pdi->spec_is_dwz, cu);
8015 real_pdi = res.pdi;
8016 cu = res.cu;
8017 }
8018
8019 parent = real_pdi->die_parent;
8020 if (parent == NULL)
8021 return NULL;
8022
8023 if (parent->scope_set)
8024 return parent->scope;
8025
8026 parent->fixup (cu);
8027
8028 grandparent_scope = partial_die_parent_scope (parent, cu);
8029
8030 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8031 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8032 Work around this problem here. */
8033 if (cu->language == language_cplus
8034 && parent->tag == DW_TAG_namespace
8035 && strcmp (parent->name, "::") == 0
8036 && grandparent_scope == NULL)
8037 {
8038 parent->scope = NULL;
8039 parent->scope_set = 1;
8040 return NULL;
8041 }
8042
8043 /* Nested subroutines in Fortran get a prefix. */
8044 if (pdi->tag == DW_TAG_enumerator)
8045 /* Enumerators should not get the name of the enumeration as a prefix. */
8046 parent->scope = grandparent_scope;
8047 else if (parent->tag == DW_TAG_namespace
8048 || parent->tag == DW_TAG_module
8049 || parent->tag == DW_TAG_structure_type
8050 || parent->tag == DW_TAG_class_type
8051 || parent->tag == DW_TAG_interface_type
8052 || parent->tag == DW_TAG_union_type
8053 || parent->tag == DW_TAG_enumeration_type
8054 || (cu->language == language_fortran
8055 && parent->tag == DW_TAG_subprogram
8056 && pdi->tag == DW_TAG_subprogram))
8057 {
8058 if (grandparent_scope == NULL)
8059 parent->scope = parent->name;
8060 else
8061 parent->scope = typename_concat (&cu->comp_unit_obstack,
8062 grandparent_scope,
8063 parent->name, 0, cu);
8064 }
8065 else
8066 {
8067 /* FIXME drow/2004-04-01: What should we be doing with
8068 function-local names? For partial symbols, we should probably be
8069 ignoring them. */
8070 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8071 dwarf_tag_name (parent->tag),
8072 sect_offset_str (pdi->sect_off));
8073 parent->scope = grandparent_scope;
8074 }
8075
8076 parent->scope_set = 1;
8077 return parent->scope;
8078 }
8079
8080 /* Return the fully scoped name associated with PDI, from compilation unit
8081 CU. The result will be allocated with malloc. */
8082
8083 static gdb::unique_xmalloc_ptr<char>
8084 partial_die_full_name (struct partial_die_info *pdi,
8085 struct dwarf2_cu *cu)
8086 {
8087 const char *parent_scope;
8088
8089 /* If this is a template instantiation, we can not work out the
8090 template arguments from partial DIEs. So, unfortunately, we have
8091 to go through the full DIEs. At least any work we do building
8092 types here will be reused if full symbols are loaded later. */
8093 if (pdi->has_template_arguments)
8094 {
8095 pdi->fixup (cu);
8096
8097 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8098 {
8099 struct die_info *die;
8100 struct attribute attr;
8101 struct dwarf2_cu *ref_cu = cu;
8102
8103 /* DW_FORM_ref_addr is using section offset. */
8104 attr.name = (enum dwarf_attribute) 0;
8105 attr.form = DW_FORM_ref_addr;
8106 attr.u.unsnd = to_underlying (pdi->sect_off);
8107 die = follow_die_ref (NULL, &attr, &ref_cu);
8108
8109 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8110 }
8111 }
8112
8113 parent_scope = partial_die_parent_scope (pdi, cu);
8114 if (parent_scope == NULL)
8115 return NULL;
8116 else
8117 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8118 pdi->name, 0, cu));
8119 }
8120
8121 static void
8122 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8123 {
8124 struct dwarf2_per_objfile *dwarf2_per_objfile
8125 = cu->per_cu->dwarf2_per_objfile;
8126 struct objfile *objfile = dwarf2_per_objfile->objfile;
8127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8128 CORE_ADDR addr = 0;
8129 const char *actual_name = NULL;
8130 CORE_ADDR baseaddr;
8131
8132 baseaddr = objfile->text_section_offset ();
8133
8134 gdb::unique_xmalloc_ptr<char> built_actual_name
8135 = partial_die_full_name (pdi, cu);
8136 if (built_actual_name != NULL)
8137 actual_name = built_actual_name.get ();
8138
8139 if (actual_name == NULL)
8140 actual_name = pdi->name;
8141
8142 switch (pdi->tag)
8143 {
8144 case DW_TAG_inlined_subroutine:
8145 case DW_TAG_subprogram:
8146 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8147 - baseaddr);
8148 if (pdi->is_external
8149 || cu->language == language_ada
8150 || (cu->language == language_fortran
8151 && pdi->die_parent != NULL
8152 && pdi->die_parent->tag == DW_TAG_subprogram))
8153 {
8154 /* Normally, only "external" DIEs are part of the global scope.
8155 But in Ada and Fortran, we want to be able to access nested
8156 procedures globally. So all Ada and Fortran subprograms are
8157 stored in the global scope. */
8158 add_psymbol_to_list (actual_name,
8159 built_actual_name != NULL,
8160 VAR_DOMAIN, LOC_BLOCK,
8161 SECT_OFF_TEXT (objfile),
8162 psymbol_placement::GLOBAL,
8163 addr,
8164 cu->language, objfile);
8165 }
8166 else
8167 {
8168 add_psymbol_to_list (actual_name,
8169 built_actual_name != NULL,
8170 VAR_DOMAIN, LOC_BLOCK,
8171 SECT_OFF_TEXT (objfile),
8172 psymbol_placement::STATIC,
8173 addr, cu->language, objfile);
8174 }
8175
8176 if (pdi->main_subprogram && actual_name != NULL)
8177 set_objfile_main_name (objfile, actual_name, cu->language);
8178 break;
8179 case DW_TAG_constant:
8180 add_psymbol_to_list (actual_name,
8181 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8182 -1, (pdi->is_external
8183 ? psymbol_placement::GLOBAL
8184 : psymbol_placement::STATIC),
8185 0, cu->language, objfile);
8186 break;
8187 case DW_TAG_variable:
8188 if (pdi->d.locdesc)
8189 addr = decode_locdesc (pdi->d.locdesc, cu);
8190
8191 if (pdi->d.locdesc
8192 && addr == 0
8193 && !dwarf2_per_objfile->has_section_at_zero)
8194 {
8195 /* A global or static variable may also have been stripped
8196 out by the linker if unused, in which case its address
8197 will be nullified; do not add such variables into partial
8198 symbol table then. */
8199 }
8200 else if (pdi->is_external)
8201 {
8202 /* Global Variable.
8203 Don't enter into the minimal symbol tables as there is
8204 a minimal symbol table entry from the ELF symbols already.
8205 Enter into partial symbol table if it has a location
8206 descriptor or a type.
8207 If the location descriptor is missing, new_symbol will create
8208 a LOC_UNRESOLVED symbol, the address of the variable will then
8209 be determined from the minimal symbol table whenever the variable
8210 is referenced.
8211 The address for the partial symbol table entry is not
8212 used by GDB, but it comes in handy for debugging partial symbol
8213 table building. */
8214
8215 if (pdi->d.locdesc || pdi->has_type)
8216 add_psymbol_to_list (actual_name,
8217 built_actual_name != NULL,
8218 VAR_DOMAIN, LOC_STATIC,
8219 SECT_OFF_TEXT (objfile),
8220 psymbol_placement::GLOBAL,
8221 addr, cu->language, objfile);
8222 }
8223 else
8224 {
8225 int has_loc = pdi->d.locdesc != NULL;
8226
8227 /* Static Variable. Skip symbols whose value we cannot know (those
8228 without location descriptors or constant values). */
8229 if (!has_loc && !pdi->has_const_value)
8230 return;
8231
8232 add_psymbol_to_list (actual_name,
8233 built_actual_name != NULL,
8234 VAR_DOMAIN, LOC_STATIC,
8235 SECT_OFF_TEXT (objfile),
8236 psymbol_placement::STATIC,
8237 has_loc ? addr : 0,
8238 cu->language, objfile);
8239 }
8240 break;
8241 case DW_TAG_typedef:
8242 case DW_TAG_base_type:
8243 case DW_TAG_subrange_type:
8244 add_psymbol_to_list (actual_name,
8245 built_actual_name != NULL,
8246 VAR_DOMAIN, LOC_TYPEDEF, -1,
8247 psymbol_placement::STATIC,
8248 0, cu->language, objfile);
8249 break;
8250 case DW_TAG_imported_declaration:
8251 case DW_TAG_namespace:
8252 add_psymbol_to_list (actual_name,
8253 built_actual_name != NULL,
8254 VAR_DOMAIN, LOC_TYPEDEF, -1,
8255 psymbol_placement::GLOBAL,
8256 0, cu->language, objfile);
8257 break;
8258 case DW_TAG_module:
8259 /* With Fortran 77 there might be a "BLOCK DATA" module
8260 available without any name. If so, we skip the module as it
8261 doesn't bring any value. */
8262 if (actual_name != nullptr)
8263 add_psymbol_to_list (actual_name,
8264 built_actual_name != NULL,
8265 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8266 psymbol_placement::GLOBAL,
8267 0, cu->language, objfile);
8268 break;
8269 case DW_TAG_class_type:
8270 case DW_TAG_interface_type:
8271 case DW_TAG_structure_type:
8272 case DW_TAG_union_type:
8273 case DW_TAG_enumeration_type:
8274 /* Skip external references. The DWARF standard says in the section
8275 about "Structure, Union, and Class Type Entries": "An incomplete
8276 structure, union or class type is represented by a structure,
8277 union or class entry that does not have a byte size attribute
8278 and that has a DW_AT_declaration attribute." */
8279 if (!pdi->has_byte_size && pdi->is_declaration)
8280 return;
8281
8282 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8283 static vs. global. */
8284 add_psymbol_to_list (actual_name,
8285 built_actual_name != NULL,
8286 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8287 cu->language == language_cplus
8288 ? psymbol_placement::GLOBAL
8289 : psymbol_placement::STATIC,
8290 0, cu->language, objfile);
8291
8292 break;
8293 case DW_TAG_enumerator:
8294 add_psymbol_to_list (actual_name,
8295 built_actual_name != NULL,
8296 VAR_DOMAIN, LOC_CONST, -1,
8297 cu->language == language_cplus
8298 ? psymbol_placement::GLOBAL
8299 : psymbol_placement::STATIC,
8300 0, cu->language, objfile);
8301 break;
8302 default:
8303 break;
8304 }
8305 }
8306
8307 /* Read a partial die corresponding to a namespace; also, add a symbol
8308 corresponding to that namespace to the symbol table. NAMESPACE is
8309 the name of the enclosing namespace. */
8310
8311 static void
8312 add_partial_namespace (struct partial_die_info *pdi,
8313 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8314 int set_addrmap, struct dwarf2_cu *cu)
8315 {
8316 /* Add a symbol for the namespace. */
8317
8318 add_partial_symbol (pdi, cu);
8319
8320 /* Now scan partial symbols in that namespace. */
8321
8322 if (pdi->has_children)
8323 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8324 }
8325
8326 /* Read a partial die corresponding to a Fortran module. */
8327
8328 static void
8329 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8330 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8331 {
8332 /* Add a symbol for the namespace. */
8333
8334 add_partial_symbol (pdi, cu);
8335
8336 /* Now scan partial symbols in that module. */
8337
8338 if (pdi->has_children)
8339 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8340 }
8341
8342 /* Read a partial die corresponding to a subprogram or an inlined
8343 subprogram and create a partial symbol for that subprogram.
8344 When the CU language allows it, this routine also defines a partial
8345 symbol for each nested subprogram that this subprogram contains.
8346 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8347 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8348
8349 PDI may also be a lexical block, in which case we simply search
8350 recursively for subprograms defined inside that lexical block.
8351 Again, this is only performed when the CU language allows this
8352 type of definitions. */
8353
8354 static void
8355 add_partial_subprogram (struct partial_die_info *pdi,
8356 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8357 int set_addrmap, struct dwarf2_cu *cu)
8358 {
8359 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8360 {
8361 if (pdi->has_pc_info)
8362 {
8363 if (pdi->lowpc < *lowpc)
8364 *lowpc = pdi->lowpc;
8365 if (pdi->highpc > *highpc)
8366 *highpc = pdi->highpc;
8367 if (set_addrmap)
8368 {
8369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8370 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8371 CORE_ADDR baseaddr;
8372 CORE_ADDR this_highpc;
8373 CORE_ADDR this_lowpc;
8374
8375 baseaddr = objfile->text_section_offset ();
8376 this_lowpc
8377 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8378 pdi->lowpc + baseaddr)
8379 - baseaddr);
8380 this_highpc
8381 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8382 pdi->highpc + baseaddr)
8383 - baseaddr);
8384 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8385 this_lowpc, this_highpc - 1,
8386 cu->per_cu->v.psymtab);
8387 }
8388 }
8389
8390 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8391 {
8392 if (!pdi->is_declaration)
8393 /* Ignore subprogram DIEs that do not have a name, they are
8394 illegal. Do not emit a complaint at this point, we will
8395 do so when we convert this psymtab into a symtab. */
8396 if (pdi->name)
8397 add_partial_symbol (pdi, cu);
8398 }
8399 }
8400
8401 if (! pdi->has_children)
8402 return;
8403
8404 if (cu->language == language_ada || cu->language == language_fortran)
8405 {
8406 pdi = pdi->die_child;
8407 while (pdi != NULL)
8408 {
8409 pdi->fixup (cu);
8410 if (pdi->tag == DW_TAG_subprogram
8411 || pdi->tag == DW_TAG_inlined_subroutine
8412 || pdi->tag == DW_TAG_lexical_block)
8413 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8414 pdi = pdi->die_sibling;
8415 }
8416 }
8417 }
8418
8419 /* Read a partial die corresponding to an enumeration type. */
8420
8421 static void
8422 add_partial_enumeration (struct partial_die_info *enum_pdi,
8423 struct dwarf2_cu *cu)
8424 {
8425 struct partial_die_info *pdi;
8426
8427 if (enum_pdi->name != NULL)
8428 add_partial_symbol (enum_pdi, cu);
8429
8430 pdi = enum_pdi->die_child;
8431 while (pdi)
8432 {
8433 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8434 complaint (_("malformed enumerator DIE ignored"));
8435 else
8436 add_partial_symbol (pdi, cu);
8437 pdi = pdi->die_sibling;
8438 }
8439 }
8440
8441 /* Return the initial uleb128 in the die at INFO_PTR. */
8442
8443 static unsigned int
8444 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8445 {
8446 unsigned int bytes_read;
8447
8448 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8449 }
8450
8451 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8452 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8453
8454 Return the corresponding abbrev, or NULL if the number is zero (indicating
8455 an empty DIE). In either case *BYTES_READ will be set to the length of
8456 the initial number. */
8457
8458 static struct abbrev_info *
8459 peek_die_abbrev (const die_reader_specs &reader,
8460 const gdb_byte *info_ptr, unsigned int *bytes_read)
8461 {
8462 dwarf2_cu *cu = reader.cu;
8463 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8464 unsigned int abbrev_number
8465 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8466
8467 if (abbrev_number == 0)
8468 return NULL;
8469
8470 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8471 if (!abbrev)
8472 {
8473 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8474 " at offset %s [in module %s]"),
8475 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8476 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8477 }
8478
8479 return abbrev;
8480 }
8481
8482 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8483 Returns a pointer to the end of a series of DIEs, terminated by an empty
8484 DIE. Any children of the skipped DIEs will also be skipped. */
8485
8486 static const gdb_byte *
8487 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8488 {
8489 while (1)
8490 {
8491 unsigned int bytes_read;
8492 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8493
8494 if (abbrev == NULL)
8495 return info_ptr + bytes_read;
8496 else
8497 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8498 }
8499 }
8500
8501 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8502 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8503 abbrev corresponding to that skipped uleb128 should be passed in
8504 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8505 children. */
8506
8507 static const gdb_byte *
8508 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8509 struct abbrev_info *abbrev)
8510 {
8511 unsigned int bytes_read;
8512 struct attribute attr;
8513 bfd *abfd = reader->abfd;
8514 struct dwarf2_cu *cu = reader->cu;
8515 const gdb_byte *buffer = reader->buffer;
8516 const gdb_byte *buffer_end = reader->buffer_end;
8517 unsigned int form, i;
8518
8519 for (i = 0; i < abbrev->num_attrs; i++)
8520 {
8521 /* The only abbrev we care about is DW_AT_sibling. */
8522 if (abbrev->attrs[i].name == DW_AT_sibling)
8523 {
8524 bool ignored;
8525 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8526 &ignored);
8527 if (attr.form == DW_FORM_ref_addr)
8528 complaint (_("ignoring absolute DW_AT_sibling"));
8529 else
8530 {
8531 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8532 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8533
8534 if (sibling_ptr < info_ptr)
8535 complaint (_("DW_AT_sibling points backwards"));
8536 else if (sibling_ptr > reader->buffer_end)
8537 reader->die_section->overflow_complaint ();
8538 else
8539 return sibling_ptr;
8540 }
8541 }
8542
8543 /* If it isn't DW_AT_sibling, skip this attribute. */
8544 form = abbrev->attrs[i].form;
8545 skip_attribute:
8546 switch (form)
8547 {
8548 case DW_FORM_ref_addr:
8549 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8550 and later it is offset sized. */
8551 if (cu->header.version == 2)
8552 info_ptr += cu->header.addr_size;
8553 else
8554 info_ptr += cu->header.offset_size;
8555 break;
8556 case DW_FORM_GNU_ref_alt:
8557 info_ptr += cu->header.offset_size;
8558 break;
8559 case DW_FORM_addr:
8560 info_ptr += cu->header.addr_size;
8561 break;
8562 case DW_FORM_data1:
8563 case DW_FORM_ref1:
8564 case DW_FORM_flag:
8565 case DW_FORM_strx1:
8566 info_ptr += 1;
8567 break;
8568 case DW_FORM_flag_present:
8569 case DW_FORM_implicit_const:
8570 break;
8571 case DW_FORM_data2:
8572 case DW_FORM_ref2:
8573 case DW_FORM_strx2:
8574 info_ptr += 2;
8575 break;
8576 case DW_FORM_strx3:
8577 info_ptr += 3;
8578 break;
8579 case DW_FORM_data4:
8580 case DW_FORM_ref4:
8581 case DW_FORM_strx4:
8582 info_ptr += 4;
8583 break;
8584 case DW_FORM_data8:
8585 case DW_FORM_ref8:
8586 case DW_FORM_ref_sig8:
8587 info_ptr += 8;
8588 break;
8589 case DW_FORM_data16:
8590 info_ptr += 16;
8591 break;
8592 case DW_FORM_string:
8593 read_direct_string (abfd, info_ptr, &bytes_read);
8594 info_ptr += bytes_read;
8595 break;
8596 case DW_FORM_sec_offset:
8597 case DW_FORM_strp:
8598 case DW_FORM_GNU_strp_alt:
8599 info_ptr += cu->header.offset_size;
8600 break;
8601 case DW_FORM_exprloc:
8602 case DW_FORM_block:
8603 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8604 info_ptr += bytes_read;
8605 break;
8606 case DW_FORM_block1:
8607 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8608 break;
8609 case DW_FORM_block2:
8610 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8611 break;
8612 case DW_FORM_block4:
8613 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8614 break;
8615 case DW_FORM_addrx:
8616 case DW_FORM_strx:
8617 case DW_FORM_sdata:
8618 case DW_FORM_udata:
8619 case DW_FORM_ref_udata:
8620 case DW_FORM_GNU_addr_index:
8621 case DW_FORM_GNU_str_index:
8622 case DW_FORM_rnglistx:
8623 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8624 break;
8625 case DW_FORM_indirect:
8626 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8627 info_ptr += bytes_read;
8628 /* We need to continue parsing from here, so just go back to
8629 the top. */
8630 goto skip_attribute;
8631
8632 default:
8633 error (_("Dwarf Error: Cannot handle %s "
8634 "in DWARF reader [in module %s]"),
8635 dwarf_form_name (form),
8636 bfd_get_filename (abfd));
8637 }
8638 }
8639
8640 if (abbrev->has_children)
8641 return skip_children (reader, info_ptr);
8642 else
8643 return info_ptr;
8644 }
8645
8646 /* Locate ORIG_PDI's sibling.
8647 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8648
8649 static const gdb_byte *
8650 locate_pdi_sibling (const struct die_reader_specs *reader,
8651 struct partial_die_info *orig_pdi,
8652 const gdb_byte *info_ptr)
8653 {
8654 /* Do we know the sibling already? */
8655
8656 if (orig_pdi->sibling)
8657 return orig_pdi->sibling;
8658
8659 /* Are there any children to deal with? */
8660
8661 if (!orig_pdi->has_children)
8662 return info_ptr;
8663
8664 /* Skip the children the long way. */
8665
8666 return skip_children (reader, info_ptr);
8667 }
8668
8669 /* Expand this partial symbol table into a full symbol table. SELF is
8670 not NULL. */
8671
8672 void
8673 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8674 {
8675 struct dwarf2_per_objfile *dwarf2_per_objfile
8676 = get_dwarf2_per_objfile (objfile);
8677
8678 gdb_assert (!readin);
8679 /* If this psymtab is constructed from a debug-only objfile, the
8680 has_section_at_zero flag will not necessarily be correct. We
8681 can get the correct value for this flag by looking at the data
8682 associated with the (presumably stripped) associated objfile. */
8683 if (objfile->separate_debug_objfile_backlink)
8684 {
8685 struct dwarf2_per_objfile *dpo_backlink
8686 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8687
8688 dwarf2_per_objfile->has_section_at_zero
8689 = dpo_backlink->has_section_at_zero;
8690 }
8691
8692 expand_psymtab (objfile);
8693
8694 process_cu_includes (dwarf2_per_objfile);
8695 }
8696 \f
8697 /* Reading in full CUs. */
8698
8699 /* Add PER_CU to the queue. */
8700
8701 static void
8702 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8703 enum language pretend_language)
8704 {
8705 per_cu->queued = 1;
8706 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8707 }
8708
8709 /* If PER_CU is not yet queued, add it to the queue.
8710 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8711 dependency.
8712 The result is non-zero if PER_CU was queued, otherwise the result is zero
8713 meaning either PER_CU is already queued or it is already loaded.
8714
8715 N.B. There is an invariant here that if a CU is queued then it is loaded.
8716 The caller is required to load PER_CU if we return non-zero. */
8717
8718 static int
8719 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8720 struct dwarf2_per_cu_data *per_cu,
8721 enum language pretend_language)
8722 {
8723 /* We may arrive here during partial symbol reading, if we need full
8724 DIEs to process an unusual case (e.g. template arguments). Do
8725 not queue PER_CU, just tell our caller to load its DIEs. */
8726 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8727 {
8728 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8729 return 1;
8730 return 0;
8731 }
8732
8733 /* Mark the dependence relation so that we don't flush PER_CU
8734 too early. */
8735 if (dependent_cu != NULL)
8736 dwarf2_add_dependence (dependent_cu, per_cu);
8737
8738 /* If it's already on the queue, we have nothing to do. */
8739 if (per_cu->queued)
8740 return 0;
8741
8742 /* If the compilation unit is already loaded, just mark it as
8743 used. */
8744 if (per_cu->cu != NULL)
8745 {
8746 per_cu->cu->last_used = 0;
8747 return 0;
8748 }
8749
8750 /* Add it to the queue. */
8751 queue_comp_unit (per_cu, pretend_language);
8752
8753 return 1;
8754 }
8755
8756 /* Process the queue. */
8757
8758 static void
8759 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8760 {
8761 if (dwarf_read_debug)
8762 {
8763 fprintf_unfiltered (gdb_stdlog,
8764 "Expanding one or more symtabs of objfile %s ...\n",
8765 objfile_name (dwarf2_per_objfile->objfile));
8766 }
8767
8768 /* The queue starts out with one item, but following a DIE reference
8769 may load a new CU, adding it to the end of the queue. */
8770 while (!dwarf2_per_objfile->queue.empty ())
8771 {
8772 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8773
8774 if ((dwarf2_per_objfile->using_index
8775 ? !item.per_cu->v.quick->compunit_symtab
8776 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8777 /* Skip dummy CUs. */
8778 && item.per_cu->cu != NULL)
8779 {
8780 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8781 unsigned int debug_print_threshold;
8782 char buf[100];
8783
8784 if (per_cu->is_debug_types)
8785 {
8786 struct signatured_type *sig_type =
8787 (struct signatured_type *) per_cu;
8788
8789 sprintf (buf, "TU %s at offset %s",
8790 hex_string (sig_type->signature),
8791 sect_offset_str (per_cu->sect_off));
8792 /* There can be 100s of TUs.
8793 Only print them in verbose mode. */
8794 debug_print_threshold = 2;
8795 }
8796 else
8797 {
8798 sprintf (buf, "CU at offset %s",
8799 sect_offset_str (per_cu->sect_off));
8800 debug_print_threshold = 1;
8801 }
8802
8803 if (dwarf_read_debug >= debug_print_threshold)
8804 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8805
8806 if (per_cu->is_debug_types)
8807 process_full_type_unit (per_cu, item.pretend_language);
8808 else
8809 process_full_comp_unit (per_cu, item.pretend_language);
8810
8811 if (dwarf_read_debug >= debug_print_threshold)
8812 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8813 }
8814
8815 item.per_cu->queued = 0;
8816 dwarf2_per_objfile->queue.pop ();
8817 }
8818
8819 if (dwarf_read_debug)
8820 {
8821 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8822 objfile_name (dwarf2_per_objfile->objfile));
8823 }
8824 }
8825
8826 /* Read in full symbols for PST, and anything it depends on. */
8827
8828 void
8829 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8830 {
8831 if (readin)
8832 return;
8833
8834 read_dependencies (objfile);
8835
8836 dw2_do_instantiate_symtab (per_cu_data, false);
8837 gdb_assert (get_compunit_symtab () != nullptr);
8838 }
8839
8840 /* Trivial hash function for die_info: the hash value of a DIE
8841 is its offset in .debug_info for this objfile. */
8842
8843 static hashval_t
8844 die_hash (const void *item)
8845 {
8846 const struct die_info *die = (const struct die_info *) item;
8847
8848 return to_underlying (die->sect_off);
8849 }
8850
8851 /* Trivial comparison function for die_info structures: two DIEs
8852 are equal if they have the same offset. */
8853
8854 static int
8855 die_eq (const void *item_lhs, const void *item_rhs)
8856 {
8857 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8858 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8859
8860 return die_lhs->sect_off == die_rhs->sect_off;
8861 }
8862
8863 /* Load the DIEs associated with PER_CU into memory. */
8864
8865 static void
8866 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8867 bool skip_partial,
8868 enum language pretend_language)
8869 {
8870 gdb_assert (! this_cu->is_debug_types);
8871
8872 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8873 if (reader.dummy_p)
8874 return;
8875
8876 struct dwarf2_cu *cu = reader.cu;
8877 const gdb_byte *info_ptr = reader.info_ptr;
8878
8879 gdb_assert (cu->die_hash == NULL);
8880 cu->die_hash =
8881 htab_create_alloc_ex (cu->header.length / 12,
8882 die_hash,
8883 die_eq,
8884 NULL,
8885 &cu->comp_unit_obstack,
8886 hashtab_obstack_allocate,
8887 dummy_obstack_deallocate);
8888
8889 if (reader.comp_unit_die->has_children)
8890 reader.comp_unit_die->child
8891 = read_die_and_siblings (&reader, reader.info_ptr,
8892 &info_ptr, reader.comp_unit_die);
8893 cu->dies = reader.comp_unit_die;
8894 /* comp_unit_die is not stored in die_hash, no need. */
8895
8896 /* We try not to read any attributes in this function, because not
8897 all CUs needed for references have been loaded yet, and symbol
8898 table processing isn't initialized. But we have to set the CU language,
8899 or we won't be able to build types correctly.
8900 Similarly, if we do not read the producer, we can not apply
8901 producer-specific interpretation. */
8902 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8903
8904 reader.keep ();
8905 }
8906
8907 /* Add a DIE to the delayed physname list. */
8908
8909 static void
8910 add_to_method_list (struct type *type, int fnfield_index, int index,
8911 const char *name, struct die_info *die,
8912 struct dwarf2_cu *cu)
8913 {
8914 struct delayed_method_info mi;
8915 mi.type = type;
8916 mi.fnfield_index = fnfield_index;
8917 mi.index = index;
8918 mi.name = name;
8919 mi.die = die;
8920 cu->method_list.push_back (mi);
8921 }
8922
8923 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8924 "const" / "volatile". If so, decrements LEN by the length of the
8925 modifier and return true. Otherwise return false. */
8926
8927 template<size_t N>
8928 static bool
8929 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8930 {
8931 size_t mod_len = sizeof (mod) - 1;
8932 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8933 {
8934 len -= mod_len;
8935 return true;
8936 }
8937 return false;
8938 }
8939
8940 /* Compute the physnames of any methods on the CU's method list.
8941
8942 The computation of method physnames is delayed in order to avoid the
8943 (bad) condition that one of the method's formal parameters is of an as yet
8944 incomplete type. */
8945
8946 static void
8947 compute_delayed_physnames (struct dwarf2_cu *cu)
8948 {
8949 /* Only C++ delays computing physnames. */
8950 if (cu->method_list.empty ())
8951 return;
8952 gdb_assert (cu->language == language_cplus);
8953
8954 for (const delayed_method_info &mi : cu->method_list)
8955 {
8956 const char *physname;
8957 struct fn_fieldlist *fn_flp
8958 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8959 physname = dwarf2_physname (mi.name, mi.die, cu);
8960 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8961 = physname ? physname : "";
8962
8963 /* Since there's no tag to indicate whether a method is a
8964 const/volatile overload, extract that information out of the
8965 demangled name. */
8966 if (physname != NULL)
8967 {
8968 size_t len = strlen (physname);
8969
8970 while (1)
8971 {
8972 if (physname[len] == ')') /* shortcut */
8973 break;
8974 else if (check_modifier (physname, len, " const"))
8975 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8976 else if (check_modifier (physname, len, " volatile"))
8977 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8978 else
8979 break;
8980 }
8981 }
8982 }
8983
8984 /* The list is no longer needed. */
8985 cu->method_list.clear ();
8986 }
8987
8988 /* Go objects should be embedded in a DW_TAG_module DIE,
8989 and it's not clear if/how imported objects will appear.
8990 To keep Go support simple until that's worked out,
8991 go back through what we've read and create something usable.
8992 We could do this while processing each DIE, and feels kinda cleaner,
8993 but that way is more invasive.
8994 This is to, for example, allow the user to type "p var" or "b main"
8995 without having to specify the package name, and allow lookups
8996 of module.object to work in contexts that use the expression
8997 parser. */
8998
8999 static void
9000 fixup_go_packaging (struct dwarf2_cu *cu)
9001 {
9002 gdb::unique_xmalloc_ptr<char> package_name;
9003 struct pending *list;
9004 int i;
9005
9006 for (list = *cu->get_builder ()->get_global_symbols ();
9007 list != NULL;
9008 list = list->next)
9009 {
9010 for (i = 0; i < list->nsyms; ++i)
9011 {
9012 struct symbol *sym = list->symbol[i];
9013
9014 if (sym->language () == language_go
9015 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9016 {
9017 gdb::unique_xmalloc_ptr<char> this_package_name
9018 (go_symbol_package_name (sym));
9019
9020 if (this_package_name == NULL)
9021 continue;
9022 if (package_name == NULL)
9023 package_name = std::move (this_package_name);
9024 else
9025 {
9026 struct objfile *objfile
9027 = cu->per_cu->dwarf2_per_objfile->objfile;
9028 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9029 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9030 (symbol_symtab (sym) != NULL
9031 ? symtab_to_filename_for_display
9032 (symbol_symtab (sym))
9033 : objfile_name (objfile)),
9034 this_package_name.get (), package_name.get ());
9035 }
9036 }
9037 }
9038 }
9039
9040 if (package_name != NULL)
9041 {
9042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9043 const char *saved_package_name = objfile->intern (package_name.get ());
9044 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9045 saved_package_name);
9046 struct symbol *sym;
9047
9048 sym = allocate_symbol (objfile);
9049 sym->set_language (language_go, &objfile->objfile_obstack);
9050 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9051 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9052 e.g., "main" finds the "main" module and not C's main(). */
9053 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9054 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9055 SYMBOL_TYPE (sym) = type;
9056
9057 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9058 }
9059 }
9060
9061 /* Allocate a fully-qualified name consisting of the two parts on the
9062 obstack. */
9063
9064 static const char *
9065 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9066 {
9067 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9068 }
9069
9070 /* A helper that allocates a struct discriminant_info to attach to a
9071 union type. */
9072
9073 static struct discriminant_info *
9074 alloc_discriminant_info (struct type *type, int discriminant_index,
9075 int default_index)
9076 {
9077 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9078 gdb_assert (discriminant_index == -1
9079 || (discriminant_index >= 0
9080 && discriminant_index < TYPE_NFIELDS (type)));
9081 gdb_assert (default_index == -1
9082 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9083
9084 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9085
9086 struct discriminant_info *disc
9087 = ((struct discriminant_info *)
9088 TYPE_ZALLOC (type,
9089 offsetof (struct discriminant_info, discriminants)
9090 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9091 disc->default_index = default_index;
9092 disc->discriminant_index = discriminant_index;
9093
9094 struct dynamic_prop prop;
9095 prop.kind = PROP_UNDEFINED;
9096 prop.data.baton = disc;
9097
9098 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9099
9100 return disc;
9101 }
9102
9103 /* Some versions of rustc emitted enums in an unusual way.
9104
9105 Ordinary enums were emitted as unions. The first element of each
9106 structure in the union was named "RUST$ENUM$DISR". This element
9107 held the discriminant.
9108
9109 These versions of Rust also implemented the "non-zero"
9110 optimization. When the enum had two values, and one is empty and
9111 the other holds a pointer that cannot be zero, the pointer is used
9112 as the discriminant, with a zero value meaning the empty variant.
9113 Here, the union's first member is of the form
9114 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9115 where the fieldnos are the indices of the fields that should be
9116 traversed in order to find the field (which may be several fields deep)
9117 and the variantname is the name of the variant of the case when the
9118 field is zero.
9119
9120 This function recognizes whether TYPE is of one of these forms,
9121 and, if so, smashes it to be a variant type. */
9122
9123 static void
9124 quirk_rust_enum (struct type *type, struct objfile *objfile)
9125 {
9126 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9127
9128 /* We don't need to deal with empty enums. */
9129 if (TYPE_NFIELDS (type) == 0)
9130 return;
9131
9132 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9133 if (TYPE_NFIELDS (type) == 1
9134 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9135 {
9136 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9137
9138 /* Decode the field name to find the offset of the
9139 discriminant. */
9140 ULONGEST bit_offset = 0;
9141 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9142 while (name[0] >= '0' && name[0] <= '9')
9143 {
9144 char *tail;
9145 unsigned long index = strtoul (name, &tail, 10);
9146 name = tail;
9147 if (*name != '$'
9148 || index >= TYPE_NFIELDS (field_type)
9149 || (TYPE_FIELD_LOC_KIND (field_type, index)
9150 != FIELD_LOC_KIND_BITPOS))
9151 {
9152 complaint (_("Could not parse Rust enum encoding string \"%s\""
9153 "[in module %s]"),
9154 TYPE_FIELD_NAME (type, 0),
9155 objfile_name (objfile));
9156 return;
9157 }
9158 ++name;
9159
9160 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9161 field_type = TYPE_FIELD_TYPE (field_type, index);
9162 }
9163
9164 /* Make a union to hold the variants. */
9165 struct type *union_type = alloc_type (objfile);
9166 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9167 TYPE_NFIELDS (union_type) = 3;
9168 TYPE_FIELDS (union_type)
9169 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9170 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9171 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9172
9173 /* Put the discriminant must at index 0. */
9174 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9175 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9176 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9177 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9178
9179 /* The order of fields doesn't really matter, so put the real
9180 field at index 1 and the data-less field at index 2. */
9181 struct discriminant_info *disc
9182 = alloc_discriminant_info (union_type, 0, 1);
9183 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9184 TYPE_FIELD_NAME (union_type, 1)
9185 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9186 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9187 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9188 TYPE_FIELD_NAME (union_type, 1));
9189
9190 const char *dataless_name
9191 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9192 name);
9193 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9194 dataless_name);
9195 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9196 /* NAME points into the original discriminant name, which
9197 already has the correct lifetime. */
9198 TYPE_FIELD_NAME (union_type, 2) = name;
9199 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9200 disc->discriminants[2] = 0;
9201
9202 /* Smash this type to be a structure type. We have to do this
9203 because the type has already been recorded. */
9204 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9205 TYPE_NFIELDS (type) = 1;
9206 TYPE_FIELDS (type)
9207 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9208
9209 /* Install the variant part. */
9210 TYPE_FIELD_TYPE (type, 0) = union_type;
9211 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9212 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9213 }
9214 /* A union with a single anonymous field is probably an old-style
9215 univariant enum. */
9216 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9217 {
9218 /* Smash this type to be a structure type. We have to do this
9219 because the type has already been recorded. */
9220 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9221
9222 /* Make a union to hold the variants. */
9223 struct type *union_type = alloc_type (objfile);
9224 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9225 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9226 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9227 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9228 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9229
9230 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9231 const char *variant_name
9232 = rust_last_path_segment (TYPE_NAME (field_type));
9233 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9234 TYPE_NAME (field_type)
9235 = rust_fully_qualify (&objfile->objfile_obstack,
9236 TYPE_NAME (type), variant_name);
9237
9238 /* Install the union in the outer struct type. */
9239 TYPE_NFIELDS (type) = 1;
9240 TYPE_FIELDS (type)
9241 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9242 TYPE_FIELD_TYPE (type, 0) = union_type;
9243 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9244 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9245
9246 alloc_discriminant_info (union_type, -1, 0);
9247 }
9248 else
9249 {
9250 struct type *disr_type = nullptr;
9251 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9252 {
9253 disr_type = TYPE_FIELD_TYPE (type, i);
9254
9255 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9256 {
9257 /* All fields of a true enum will be structs. */
9258 return;
9259 }
9260 else if (TYPE_NFIELDS (disr_type) == 0)
9261 {
9262 /* Could be data-less variant, so keep going. */
9263 disr_type = nullptr;
9264 }
9265 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9266 "RUST$ENUM$DISR") != 0)
9267 {
9268 /* Not a Rust enum. */
9269 return;
9270 }
9271 else
9272 {
9273 /* Found one. */
9274 break;
9275 }
9276 }
9277
9278 /* If we got here without a discriminant, then it's probably
9279 just a union. */
9280 if (disr_type == nullptr)
9281 return;
9282
9283 /* Smash this type to be a structure type. We have to do this
9284 because the type has already been recorded. */
9285 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9286
9287 /* Make a union to hold the variants. */
9288 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9289 struct type *union_type = alloc_type (objfile);
9290 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9291 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9292 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9293 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9294 TYPE_FIELDS (union_type)
9295 = (struct field *) TYPE_ZALLOC (union_type,
9296 (TYPE_NFIELDS (union_type)
9297 * sizeof (struct field)));
9298
9299 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9300 TYPE_NFIELDS (type) * sizeof (struct field));
9301
9302 /* Install the discriminant at index 0 in the union. */
9303 TYPE_FIELD (union_type, 0) = *disr_field;
9304 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9305 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9306
9307 /* Install the union in the outer struct type. */
9308 TYPE_FIELD_TYPE (type, 0) = union_type;
9309 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9310 TYPE_NFIELDS (type) = 1;
9311
9312 /* Set the size and offset of the union type. */
9313 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9314
9315 /* We need a way to find the correct discriminant given a
9316 variant name. For convenience we build a map here. */
9317 struct type *enum_type = FIELD_TYPE (*disr_field);
9318 std::unordered_map<std::string, ULONGEST> discriminant_map;
9319 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9320 {
9321 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9322 {
9323 const char *name
9324 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9325 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9326 }
9327 }
9328
9329 int n_fields = TYPE_NFIELDS (union_type);
9330 struct discriminant_info *disc
9331 = alloc_discriminant_info (union_type, 0, -1);
9332 /* Skip the discriminant here. */
9333 for (int i = 1; i < n_fields; ++i)
9334 {
9335 /* Find the final word in the name of this variant's type.
9336 That name can be used to look up the correct
9337 discriminant. */
9338 const char *variant_name
9339 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9340 i)));
9341
9342 auto iter = discriminant_map.find (variant_name);
9343 if (iter != discriminant_map.end ())
9344 disc->discriminants[i] = iter->second;
9345
9346 /* Remove the discriminant field, if it exists. */
9347 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9348 if (TYPE_NFIELDS (sub_type) > 0)
9349 {
9350 --TYPE_NFIELDS (sub_type);
9351 ++TYPE_FIELDS (sub_type);
9352 }
9353 TYPE_FIELD_NAME (union_type, i) = variant_name;
9354 TYPE_NAME (sub_type)
9355 = rust_fully_qualify (&objfile->objfile_obstack,
9356 TYPE_NAME (type), variant_name);
9357 }
9358 }
9359 }
9360
9361 /* Rewrite some Rust unions to be structures with variants parts. */
9362
9363 static void
9364 rust_union_quirks (struct dwarf2_cu *cu)
9365 {
9366 gdb_assert (cu->language == language_rust);
9367 for (type *type_ : cu->rust_unions)
9368 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9369 /* We don't need this any more. */
9370 cu->rust_unions.clear ();
9371 }
9372
9373 /* Return the symtab for PER_CU. This works properly regardless of
9374 whether we're using the index or psymtabs. */
9375
9376 static struct compunit_symtab *
9377 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9378 {
9379 return (per_cu->dwarf2_per_objfile->using_index
9380 ? per_cu->v.quick->compunit_symtab
9381 : per_cu->v.psymtab->compunit_symtab);
9382 }
9383
9384 /* A helper function for computing the list of all symbol tables
9385 included by PER_CU. */
9386
9387 static void
9388 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9389 htab_t all_children, htab_t all_type_symtabs,
9390 struct dwarf2_per_cu_data *per_cu,
9391 struct compunit_symtab *immediate_parent)
9392 {
9393 void **slot;
9394 struct compunit_symtab *cust;
9395
9396 slot = htab_find_slot (all_children, per_cu, INSERT);
9397 if (*slot != NULL)
9398 {
9399 /* This inclusion and its children have been processed. */
9400 return;
9401 }
9402
9403 *slot = per_cu;
9404 /* Only add a CU if it has a symbol table. */
9405 cust = get_compunit_symtab (per_cu);
9406 if (cust != NULL)
9407 {
9408 /* If this is a type unit only add its symbol table if we haven't
9409 seen it yet (type unit per_cu's can share symtabs). */
9410 if (per_cu->is_debug_types)
9411 {
9412 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9413 if (*slot == NULL)
9414 {
9415 *slot = cust;
9416 result->push_back (cust);
9417 if (cust->user == NULL)
9418 cust->user = immediate_parent;
9419 }
9420 }
9421 else
9422 {
9423 result->push_back (cust);
9424 if (cust->user == NULL)
9425 cust->user = immediate_parent;
9426 }
9427 }
9428
9429 if (!per_cu->imported_symtabs_empty ())
9430 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9431 {
9432 recursively_compute_inclusions (result, all_children,
9433 all_type_symtabs, ptr, cust);
9434 }
9435 }
9436
9437 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9438 PER_CU. */
9439
9440 static void
9441 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9442 {
9443 gdb_assert (! per_cu->is_debug_types);
9444
9445 if (!per_cu->imported_symtabs_empty ())
9446 {
9447 int len;
9448 std::vector<compunit_symtab *> result_symtabs;
9449 htab_t all_children, all_type_symtabs;
9450 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9451
9452 /* If we don't have a symtab, we can just skip this case. */
9453 if (cust == NULL)
9454 return;
9455
9456 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9457 NULL, xcalloc, xfree);
9458 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9459 NULL, xcalloc, xfree);
9460
9461 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9462 {
9463 recursively_compute_inclusions (&result_symtabs, all_children,
9464 all_type_symtabs, ptr, cust);
9465 }
9466
9467 /* Now we have a transitive closure of all the included symtabs. */
9468 len = result_symtabs.size ();
9469 cust->includes
9470 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9471 struct compunit_symtab *, len + 1);
9472 memcpy (cust->includes, result_symtabs.data (),
9473 len * sizeof (compunit_symtab *));
9474 cust->includes[len] = NULL;
9475
9476 htab_delete (all_children);
9477 htab_delete (all_type_symtabs);
9478 }
9479 }
9480
9481 /* Compute the 'includes' field for the symtabs of all the CUs we just
9482 read. */
9483
9484 static void
9485 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9486 {
9487 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9488 {
9489 if (! iter->is_debug_types)
9490 compute_compunit_symtab_includes (iter);
9491 }
9492
9493 dwarf2_per_objfile->just_read_cus.clear ();
9494 }
9495
9496 /* Generate full symbol information for PER_CU, whose DIEs have
9497 already been loaded into memory. */
9498
9499 static void
9500 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9501 enum language pretend_language)
9502 {
9503 struct dwarf2_cu *cu = per_cu->cu;
9504 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9505 struct objfile *objfile = dwarf2_per_objfile->objfile;
9506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9507 CORE_ADDR lowpc, highpc;
9508 struct compunit_symtab *cust;
9509 CORE_ADDR baseaddr;
9510 struct block *static_block;
9511 CORE_ADDR addr;
9512
9513 baseaddr = objfile->text_section_offset ();
9514
9515 /* Clear the list here in case something was left over. */
9516 cu->method_list.clear ();
9517
9518 cu->language = pretend_language;
9519 cu->language_defn = language_def (cu->language);
9520
9521 /* Do line number decoding in read_file_scope () */
9522 process_die (cu->dies, cu);
9523
9524 /* For now fudge the Go package. */
9525 if (cu->language == language_go)
9526 fixup_go_packaging (cu);
9527
9528 /* Now that we have processed all the DIEs in the CU, all the types
9529 should be complete, and it should now be safe to compute all of the
9530 physnames. */
9531 compute_delayed_physnames (cu);
9532
9533 if (cu->language == language_rust)
9534 rust_union_quirks (cu);
9535
9536 /* Some compilers don't define a DW_AT_high_pc attribute for the
9537 compilation unit. If the DW_AT_high_pc is missing, synthesize
9538 it, by scanning the DIE's below the compilation unit. */
9539 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9540
9541 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9542 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9543
9544 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9545 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9546 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9547 addrmap to help ensure it has an accurate map of pc values belonging to
9548 this comp unit. */
9549 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9550
9551 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9552 SECT_OFF_TEXT (objfile),
9553 0);
9554
9555 if (cust != NULL)
9556 {
9557 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9558
9559 /* Set symtab language to language from DW_AT_language. If the
9560 compilation is from a C file generated by language preprocessors, do
9561 not set the language if it was already deduced by start_subfile. */
9562 if (!(cu->language == language_c
9563 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9564 COMPUNIT_FILETABS (cust)->language = cu->language;
9565
9566 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9567 produce DW_AT_location with location lists but it can be possibly
9568 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9569 there were bugs in prologue debug info, fixed later in GCC-4.5
9570 by "unwind info for epilogues" patch (which is not directly related).
9571
9572 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9573 needed, it would be wrong due to missing DW_AT_producer there.
9574
9575 Still one can confuse GDB by using non-standard GCC compilation
9576 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9577 */
9578 if (cu->has_loclist && gcc_4_minor >= 5)
9579 cust->locations_valid = 1;
9580
9581 if (gcc_4_minor >= 5)
9582 cust->epilogue_unwind_valid = 1;
9583
9584 cust->call_site_htab = cu->call_site_htab;
9585 }
9586
9587 if (dwarf2_per_objfile->using_index)
9588 per_cu->v.quick->compunit_symtab = cust;
9589 else
9590 {
9591 dwarf2_psymtab *pst = per_cu->v.psymtab;
9592 pst->compunit_symtab = cust;
9593 pst->readin = true;
9594 }
9595
9596 /* Push it for inclusion processing later. */
9597 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9598
9599 /* Not needed any more. */
9600 cu->reset_builder ();
9601 }
9602
9603 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9604 already been loaded into memory. */
9605
9606 static void
9607 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9608 enum language pretend_language)
9609 {
9610 struct dwarf2_cu *cu = per_cu->cu;
9611 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9612 struct objfile *objfile = dwarf2_per_objfile->objfile;
9613 struct compunit_symtab *cust;
9614 struct signatured_type *sig_type;
9615
9616 gdb_assert (per_cu->is_debug_types);
9617 sig_type = (struct signatured_type *) per_cu;
9618
9619 /* Clear the list here in case something was left over. */
9620 cu->method_list.clear ();
9621
9622 cu->language = pretend_language;
9623 cu->language_defn = language_def (cu->language);
9624
9625 /* The symbol tables are set up in read_type_unit_scope. */
9626 process_die (cu->dies, cu);
9627
9628 /* For now fudge the Go package. */
9629 if (cu->language == language_go)
9630 fixup_go_packaging (cu);
9631
9632 /* Now that we have processed all the DIEs in the CU, all the types
9633 should be complete, and it should now be safe to compute all of the
9634 physnames. */
9635 compute_delayed_physnames (cu);
9636
9637 if (cu->language == language_rust)
9638 rust_union_quirks (cu);
9639
9640 /* TUs share symbol tables.
9641 If this is the first TU to use this symtab, complete the construction
9642 of it with end_expandable_symtab. Otherwise, complete the addition of
9643 this TU's symbols to the existing symtab. */
9644 if (sig_type->type_unit_group->compunit_symtab == NULL)
9645 {
9646 buildsym_compunit *builder = cu->get_builder ();
9647 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9648 sig_type->type_unit_group->compunit_symtab = cust;
9649
9650 if (cust != NULL)
9651 {
9652 /* Set symtab language to language from DW_AT_language. If the
9653 compilation is from a C file generated by language preprocessors,
9654 do not set the language if it was already deduced by
9655 start_subfile. */
9656 if (!(cu->language == language_c
9657 && COMPUNIT_FILETABS (cust)->language != language_c))
9658 COMPUNIT_FILETABS (cust)->language = cu->language;
9659 }
9660 }
9661 else
9662 {
9663 cu->get_builder ()->augment_type_symtab ();
9664 cust = sig_type->type_unit_group->compunit_symtab;
9665 }
9666
9667 if (dwarf2_per_objfile->using_index)
9668 per_cu->v.quick->compunit_symtab = cust;
9669 else
9670 {
9671 dwarf2_psymtab *pst = per_cu->v.psymtab;
9672 pst->compunit_symtab = cust;
9673 pst->readin = true;
9674 }
9675
9676 /* Not needed any more. */
9677 cu->reset_builder ();
9678 }
9679
9680 /* Process an imported unit DIE. */
9681
9682 static void
9683 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9684 {
9685 struct attribute *attr;
9686
9687 /* For now we don't handle imported units in type units. */
9688 if (cu->per_cu->is_debug_types)
9689 {
9690 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9691 " supported in type units [in module %s]"),
9692 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9693 }
9694
9695 attr = dwarf2_attr (die, DW_AT_import, cu);
9696 if (attr != NULL)
9697 {
9698 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9699 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9700 dwarf2_per_cu_data *per_cu
9701 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9702 cu->per_cu->dwarf2_per_objfile);
9703
9704 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9705 into another compilation unit, at root level. Regard this as a hint,
9706 and ignore it. */
9707 if (die->parent && die->parent->parent == NULL
9708 && per_cu->unit_type == DW_UT_compile
9709 && per_cu->lang == language_cplus)
9710 return;
9711
9712 /* If necessary, add it to the queue and load its DIEs. */
9713 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9714 load_full_comp_unit (per_cu, false, cu->language);
9715
9716 cu->per_cu->imported_symtabs_push (per_cu);
9717 }
9718 }
9719
9720 /* RAII object that represents a process_die scope: i.e.,
9721 starts/finishes processing a DIE. */
9722 class process_die_scope
9723 {
9724 public:
9725 process_die_scope (die_info *die, dwarf2_cu *cu)
9726 : m_die (die), m_cu (cu)
9727 {
9728 /* We should only be processing DIEs not already in process. */
9729 gdb_assert (!m_die->in_process);
9730 m_die->in_process = true;
9731 }
9732
9733 ~process_die_scope ()
9734 {
9735 m_die->in_process = false;
9736
9737 /* If we're done processing the DIE for the CU that owns the line
9738 header, we don't need the line header anymore. */
9739 if (m_cu->line_header_die_owner == m_die)
9740 {
9741 delete m_cu->line_header;
9742 m_cu->line_header = NULL;
9743 m_cu->line_header_die_owner = NULL;
9744 }
9745 }
9746
9747 private:
9748 die_info *m_die;
9749 dwarf2_cu *m_cu;
9750 };
9751
9752 /* Process a die and its children. */
9753
9754 static void
9755 process_die (struct die_info *die, struct dwarf2_cu *cu)
9756 {
9757 process_die_scope scope (die, cu);
9758
9759 switch (die->tag)
9760 {
9761 case DW_TAG_padding:
9762 break;
9763 case DW_TAG_compile_unit:
9764 case DW_TAG_partial_unit:
9765 read_file_scope (die, cu);
9766 break;
9767 case DW_TAG_type_unit:
9768 read_type_unit_scope (die, cu);
9769 break;
9770 case DW_TAG_subprogram:
9771 /* Nested subprograms in Fortran get a prefix. */
9772 if (cu->language == language_fortran
9773 && die->parent != NULL
9774 && die->parent->tag == DW_TAG_subprogram)
9775 cu->processing_has_namespace_info = true;
9776 /* Fall through. */
9777 case DW_TAG_inlined_subroutine:
9778 read_func_scope (die, cu);
9779 break;
9780 case DW_TAG_lexical_block:
9781 case DW_TAG_try_block:
9782 case DW_TAG_catch_block:
9783 read_lexical_block_scope (die, cu);
9784 break;
9785 case DW_TAG_call_site:
9786 case DW_TAG_GNU_call_site:
9787 read_call_site_scope (die, cu);
9788 break;
9789 case DW_TAG_class_type:
9790 case DW_TAG_interface_type:
9791 case DW_TAG_structure_type:
9792 case DW_TAG_union_type:
9793 process_structure_scope (die, cu);
9794 break;
9795 case DW_TAG_enumeration_type:
9796 process_enumeration_scope (die, cu);
9797 break;
9798
9799 /* These dies have a type, but processing them does not create
9800 a symbol or recurse to process the children. Therefore we can
9801 read them on-demand through read_type_die. */
9802 case DW_TAG_subroutine_type:
9803 case DW_TAG_set_type:
9804 case DW_TAG_array_type:
9805 case DW_TAG_pointer_type:
9806 case DW_TAG_ptr_to_member_type:
9807 case DW_TAG_reference_type:
9808 case DW_TAG_rvalue_reference_type:
9809 case DW_TAG_string_type:
9810 break;
9811
9812 case DW_TAG_base_type:
9813 case DW_TAG_subrange_type:
9814 case DW_TAG_typedef:
9815 /* Add a typedef symbol for the type definition, if it has a
9816 DW_AT_name. */
9817 new_symbol (die, read_type_die (die, cu), cu);
9818 break;
9819 case DW_TAG_common_block:
9820 read_common_block (die, cu);
9821 break;
9822 case DW_TAG_common_inclusion:
9823 break;
9824 case DW_TAG_namespace:
9825 cu->processing_has_namespace_info = true;
9826 read_namespace (die, cu);
9827 break;
9828 case DW_TAG_module:
9829 cu->processing_has_namespace_info = true;
9830 read_module (die, cu);
9831 break;
9832 case DW_TAG_imported_declaration:
9833 cu->processing_has_namespace_info = true;
9834 if (read_namespace_alias (die, cu))
9835 break;
9836 /* The declaration is not a global namespace alias. */
9837 /* Fall through. */
9838 case DW_TAG_imported_module:
9839 cu->processing_has_namespace_info = true;
9840 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9841 || cu->language != language_fortran))
9842 complaint (_("Tag '%s' has unexpected children"),
9843 dwarf_tag_name (die->tag));
9844 read_import_statement (die, cu);
9845 break;
9846
9847 case DW_TAG_imported_unit:
9848 process_imported_unit_die (die, cu);
9849 break;
9850
9851 case DW_TAG_variable:
9852 read_variable (die, cu);
9853 break;
9854
9855 default:
9856 new_symbol (die, NULL, cu);
9857 break;
9858 }
9859 }
9860 \f
9861 /* DWARF name computation. */
9862
9863 /* A helper function for dwarf2_compute_name which determines whether DIE
9864 needs to have the name of the scope prepended to the name listed in the
9865 die. */
9866
9867 static int
9868 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9869 {
9870 struct attribute *attr;
9871
9872 switch (die->tag)
9873 {
9874 case DW_TAG_namespace:
9875 case DW_TAG_typedef:
9876 case DW_TAG_class_type:
9877 case DW_TAG_interface_type:
9878 case DW_TAG_structure_type:
9879 case DW_TAG_union_type:
9880 case DW_TAG_enumeration_type:
9881 case DW_TAG_enumerator:
9882 case DW_TAG_subprogram:
9883 case DW_TAG_inlined_subroutine:
9884 case DW_TAG_member:
9885 case DW_TAG_imported_declaration:
9886 return 1;
9887
9888 case DW_TAG_variable:
9889 case DW_TAG_constant:
9890 /* We only need to prefix "globally" visible variables. These include
9891 any variable marked with DW_AT_external or any variable that
9892 lives in a namespace. [Variables in anonymous namespaces
9893 require prefixing, but they are not DW_AT_external.] */
9894
9895 if (dwarf2_attr (die, DW_AT_specification, cu))
9896 {
9897 struct dwarf2_cu *spec_cu = cu;
9898
9899 return die_needs_namespace (die_specification (die, &spec_cu),
9900 spec_cu);
9901 }
9902
9903 attr = dwarf2_attr (die, DW_AT_external, cu);
9904 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9905 && die->parent->tag != DW_TAG_module)
9906 return 0;
9907 /* A variable in a lexical block of some kind does not need a
9908 namespace, even though in C++ such variables may be external
9909 and have a mangled name. */
9910 if (die->parent->tag == DW_TAG_lexical_block
9911 || die->parent->tag == DW_TAG_try_block
9912 || die->parent->tag == DW_TAG_catch_block
9913 || die->parent->tag == DW_TAG_subprogram)
9914 return 0;
9915 return 1;
9916
9917 default:
9918 return 0;
9919 }
9920 }
9921
9922 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9923 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9924 defined for the given DIE. */
9925
9926 static struct attribute *
9927 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9928 {
9929 struct attribute *attr;
9930
9931 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9932 if (attr == NULL)
9933 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9934
9935 return attr;
9936 }
9937
9938 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9939 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9940 defined for the given DIE. */
9941
9942 static const char *
9943 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9944 {
9945 const char *linkage_name;
9946
9947 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9948 if (linkage_name == NULL)
9949 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9950
9951 return linkage_name;
9952 }
9953
9954 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9955 compute the physname for the object, which include a method's:
9956 - formal parameters (C++),
9957 - receiver type (Go),
9958
9959 The term "physname" is a bit confusing.
9960 For C++, for example, it is the demangled name.
9961 For Go, for example, it's the mangled name.
9962
9963 For Ada, return the DIE's linkage name rather than the fully qualified
9964 name. PHYSNAME is ignored..
9965
9966 The result is allocated on the objfile_obstack and canonicalized. */
9967
9968 static const char *
9969 dwarf2_compute_name (const char *name,
9970 struct die_info *die, struct dwarf2_cu *cu,
9971 int physname)
9972 {
9973 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9974
9975 if (name == NULL)
9976 name = dwarf2_name (die, cu);
9977
9978 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9979 but otherwise compute it by typename_concat inside GDB.
9980 FIXME: Actually this is not really true, or at least not always true.
9981 It's all very confusing. compute_and_set_names doesn't try to demangle
9982 Fortran names because there is no mangling standard. So new_symbol
9983 will set the demangled name to the result of dwarf2_full_name, and it is
9984 the demangled name that GDB uses if it exists. */
9985 if (cu->language == language_ada
9986 || (cu->language == language_fortran && physname))
9987 {
9988 /* For Ada unit, we prefer the linkage name over the name, as
9989 the former contains the exported name, which the user expects
9990 to be able to reference. Ideally, we want the user to be able
9991 to reference this entity using either natural or linkage name,
9992 but we haven't started looking at this enhancement yet. */
9993 const char *linkage_name = dw2_linkage_name (die, cu);
9994
9995 if (linkage_name != NULL)
9996 return linkage_name;
9997 }
9998
9999 /* These are the only languages we know how to qualify names in. */
10000 if (name != NULL
10001 && (cu->language == language_cplus
10002 || cu->language == language_fortran || cu->language == language_d
10003 || cu->language == language_rust))
10004 {
10005 if (die_needs_namespace (die, cu))
10006 {
10007 const char *prefix;
10008 const char *canonical_name = NULL;
10009
10010 string_file buf;
10011
10012 prefix = determine_prefix (die, cu);
10013 if (*prefix != '\0')
10014 {
10015 gdb::unique_xmalloc_ptr<char> prefixed_name
10016 (typename_concat (NULL, prefix, name, physname, cu));
10017
10018 buf.puts (prefixed_name.get ());
10019 }
10020 else
10021 buf.puts (name);
10022
10023 /* Template parameters may be specified in the DIE's DW_AT_name, or
10024 as children with DW_TAG_template_type_param or
10025 DW_TAG_value_type_param. If the latter, add them to the name
10026 here. If the name already has template parameters, then
10027 skip this step; some versions of GCC emit both, and
10028 it is more efficient to use the pre-computed name.
10029
10030 Something to keep in mind about this process: it is very
10031 unlikely, or in some cases downright impossible, to produce
10032 something that will match the mangled name of a function.
10033 If the definition of the function has the same debug info,
10034 we should be able to match up with it anyway. But fallbacks
10035 using the minimal symbol, for instance to find a method
10036 implemented in a stripped copy of libstdc++, will not work.
10037 If we do not have debug info for the definition, we will have to
10038 match them up some other way.
10039
10040 When we do name matching there is a related problem with function
10041 templates; two instantiated function templates are allowed to
10042 differ only by their return types, which we do not add here. */
10043
10044 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10045 {
10046 struct attribute *attr;
10047 struct die_info *child;
10048 int first = 1;
10049
10050 die->building_fullname = 1;
10051
10052 for (child = die->child; child != NULL; child = child->sibling)
10053 {
10054 struct type *type;
10055 LONGEST value;
10056 const gdb_byte *bytes;
10057 struct dwarf2_locexpr_baton *baton;
10058 struct value *v;
10059
10060 if (child->tag != DW_TAG_template_type_param
10061 && child->tag != DW_TAG_template_value_param)
10062 continue;
10063
10064 if (first)
10065 {
10066 buf.puts ("<");
10067 first = 0;
10068 }
10069 else
10070 buf.puts (", ");
10071
10072 attr = dwarf2_attr (child, DW_AT_type, cu);
10073 if (attr == NULL)
10074 {
10075 complaint (_("template parameter missing DW_AT_type"));
10076 buf.puts ("UNKNOWN_TYPE");
10077 continue;
10078 }
10079 type = die_type (child, cu);
10080
10081 if (child->tag == DW_TAG_template_type_param)
10082 {
10083 c_print_type (type, "", &buf, -1, 0, cu->language,
10084 &type_print_raw_options);
10085 continue;
10086 }
10087
10088 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10089 if (attr == NULL)
10090 {
10091 complaint (_("template parameter missing "
10092 "DW_AT_const_value"));
10093 buf.puts ("UNKNOWN_VALUE");
10094 continue;
10095 }
10096
10097 dwarf2_const_value_attr (attr, type, name,
10098 &cu->comp_unit_obstack, cu,
10099 &value, &bytes, &baton);
10100
10101 if (TYPE_NOSIGN (type))
10102 /* GDB prints characters as NUMBER 'CHAR'. If that's
10103 changed, this can use value_print instead. */
10104 c_printchar (value, type, &buf);
10105 else
10106 {
10107 struct value_print_options opts;
10108
10109 if (baton != NULL)
10110 v = dwarf2_evaluate_loc_desc (type, NULL,
10111 baton->data,
10112 baton->size,
10113 baton->per_cu);
10114 else if (bytes != NULL)
10115 {
10116 v = allocate_value (type);
10117 memcpy (value_contents_writeable (v), bytes,
10118 TYPE_LENGTH (type));
10119 }
10120 else
10121 v = value_from_longest (type, value);
10122
10123 /* Specify decimal so that we do not depend on
10124 the radix. */
10125 get_formatted_print_options (&opts, 'd');
10126 opts.raw = 1;
10127 value_print (v, &buf, &opts);
10128 release_value (v);
10129 }
10130 }
10131
10132 die->building_fullname = 0;
10133
10134 if (!first)
10135 {
10136 /* Close the argument list, with a space if necessary
10137 (nested templates). */
10138 if (!buf.empty () && buf.string ().back () == '>')
10139 buf.puts (" >");
10140 else
10141 buf.puts (">");
10142 }
10143 }
10144
10145 /* For C++ methods, append formal parameter type
10146 information, if PHYSNAME. */
10147
10148 if (physname && die->tag == DW_TAG_subprogram
10149 && cu->language == language_cplus)
10150 {
10151 struct type *type = read_type_die (die, cu);
10152
10153 c_type_print_args (type, &buf, 1, cu->language,
10154 &type_print_raw_options);
10155
10156 if (cu->language == language_cplus)
10157 {
10158 /* Assume that an artificial first parameter is
10159 "this", but do not crash if it is not. RealView
10160 marks unnamed (and thus unused) parameters as
10161 artificial; there is no way to differentiate
10162 the two cases. */
10163 if (TYPE_NFIELDS (type) > 0
10164 && TYPE_FIELD_ARTIFICIAL (type, 0)
10165 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10166 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10167 0))))
10168 buf.puts (" const");
10169 }
10170 }
10171
10172 const std::string &intermediate_name = buf.string ();
10173
10174 if (cu->language == language_cplus)
10175 canonical_name
10176 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10177 objfile);
10178
10179 /* If we only computed INTERMEDIATE_NAME, or if
10180 INTERMEDIATE_NAME is already canonical, then we need to
10181 intern it. */
10182 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10183 name = objfile->intern (intermediate_name);
10184 else
10185 name = canonical_name;
10186 }
10187 }
10188
10189 return name;
10190 }
10191
10192 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10193 If scope qualifiers are appropriate they will be added. The result
10194 will be allocated on the storage_obstack, or NULL if the DIE does
10195 not have a name. NAME may either be from a previous call to
10196 dwarf2_name or NULL.
10197
10198 The output string will be canonicalized (if C++). */
10199
10200 static const char *
10201 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10202 {
10203 return dwarf2_compute_name (name, die, cu, 0);
10204 }
10205
10206 /* Construct a physname for the given DIE in CU. NAME may either be
10207 from a previous call to dwarf2_name or NULL. The result will be
10208 allocated on the objfile_objstack or NULL if the DIE does not have a
10209 name.
10210
10211 The output string will be canonicalized (if C++). */
10212
10213 static const char *
10214 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10215 {
10216 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10217 const char *retval, *mangled = NULL, *canon = NULL;
10218 int need_copy = 1;
10219
10220 /* In this case dwarf2_compute_name is just a shortcut not building anything
10221 on its own. */
10222 if (!die_needs_namespace (die, cu))
10223 return dwarf2_compute_name (name, die, cu, 1);
10224
10225 mangled = dw2_linkage_name (die, cu);
10226
10227 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10228 See https://github.com/rust-lang/rust/issues/32925. */
10229 if (cu->language == language_rust && mangled != NULL
10230 && strchr (mangled, '{') != NULL)
10231 mangled = NULL;
10232
10233 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10234 has computed. */
10235 gdb::unique_xmalloc_ptr<char> demangled;
10236 if (mangled != NULL)
10237 {
10238
10239 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10240 {
10241 /* Do nothing (do not demangle the symbol name). */
10242 }
10243 else if (cu->language == language_go)
10244 {
10245 /* This is a lie, but we already lie to the caller new_symbol.
10246 new_symbol assumes we return the mangled name.
10247 This just undoes that lie until things are cleaned up. */
10248 }
10249 else
10250 {
10251 /* Use DMGL_RET_DROP for C++ template functions to suppress
10252 their return type. It is easier for GDB users to search
10253 for such functions as `name(params)' than `long name(params)'.
10254 In such case the minimal symbol names do not match the full
10255 symbol names but for template functions there is never a need
10256 to look up their definition from their declaration so
10257 the only disadvantage remains the minimal symbol variant
10258 `long name(params)' does not have the proper inferior type. */
10259 demangled.reset (gdb_demangle (mangled,
10260 (DMGL_PARAMS | DMGL_ANSI
10261 | DMGL_RET_DROP)));
10262 }
10263 if (demangled)
10264 canon = demangled.get ();
10265 else
10266 {
10267 canon = mangled;
10268 need_copy = 0;
10269 }
10270 }
10271
10272 if (canon == NULL || check_physname)
10273 {
10274 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10275
10276 if (canon != NULL && strcmp (physname, canon) != 0)
10277 {
10278 /* It may not mean a bug in GDB. The compiler could also
10279 compute DW_AT_linkage_name incorrectly. But in such case
10280 GDB would need to be bug-to-bug compatible. */
10281
10282 complaint (_("Computed physname <%s> does not match demangled <%s> "
10283 "(from linkage <%s>) - DIE at %s [in module %s]"),
10284 physname, canon, mangled, sect_offset_str (die->sect_off),
10285 objfile_name (objfile));
10286
10287 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10288 is available here - over computed PHYSNAME. It is safer
10289 against both buggy GDB and buggy compilers. */
10290
10291 retval = canon;
10292 }
10293 else
10294 {
10295 retval = physname;
10296 need_copy = 0;
10297 }
10298 }
10299 else
10300 retval = canon;
10301
10302 if (need_copy)
10303 retval = objfile->intern (retval);
10304
10305 return retval;
10306 }
10307
10308 /* Inspect DIE in CU for a namespace alias. If one exists, record
10309 a new symbol for it.
10310
10311 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10312
10313 static int
10314 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10315 {
10316 struct attribute *attr;
10317
10318 /* If the die does not have a name, this is not a namespace
10319 alias. */
10320 attr = dwarf2_attr (die, DW_AT_name, cu);
10321 if (attr != NULL)
10322 {
10323 int num;
10324 struct die_info *d = die;
10325 struct dwarf2_cu *imported_cu = cu;
10326
10327 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10328 keep inspecting DIEs until we hit the underlying import. */
10329 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10330 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10331 {
10332 attr = dwarf2_attr (d, DW_AT_import, cu);
10333 if (attr == NULL)
10334 break;
10335
10336 d = follow_die_ref (d, attr, &imported_cu);
10337 if (d->tag != DW_TAG_imported_declaration)
10338 break;
10339 }
10340
10341 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10342 {
10343 complaint (_("DIE at %s has too many recursively imported "
10344 "declarations"), sect_offset_str (d->sect_off));
10345 return 0;
10346 }
10347
10348 if (attr != NULL)
10349 {
10350 struct type *type;
10351 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10352
10353 type = get_die_type_at_offset (sect_off, cu->per_cu);
10354 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10355 {
10356 /* This declaration is a global namespace alias. Add
10357 a symbol for it whose type is the aliased namespace. */
10358 new_symbol (die, type, cu);
10359 return 1;
10360 }
10361 }
10362 }
10363
10364 return 0;
10365 }
10366
10367 /* Return the using directives repository (global or local?) to use in the
10368 current context for CU.
10369
10370 For Ada, imported declarations can materialize renamings, which *may* be
10371 global. However it is impossible (for now?) in DWARF to distinguish
10372 "external" imported declarations and "static" ones. As all imported
10373 declarations seem to be static in all other languages, make them all CU-wide
10374 global only in Ada. */
10375
10376 static struct using_direct **
10377 using_directives (struct dwarf2_cu *cu)
10378 {
10379 if (cu->language == language_ada
10380 && cu->get_builder ()->outermost_context_p ())
10381 return cu->get_builder ()->get_global_using_directives ();
10382 else
10383 return cu->get_builder ()->get_local_using_directives ();
10384 }
10385
10386 /* Read the import statement specified by the given die and record it. */
10387
10388 static void
10389 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10390 {
10391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10392 struct attribute *import_attr;
10393 struct die_info *imported_die, *child_die;
10394 struct dwarf2_cu *imported_cu;
10395 const char *imported_name;
10396 const char *imported_name_prefix;
10397 const char *canonical_name;
10398 const char *import_alias;
10399 const char *imported_declaration = NULL;
10400 const char *import_prefix;
10401 std::vector<const char *> excludes;
10402
10403 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10404 if (import_attr == NULL)
10405 {
10406 complaint (_("Tag '%s' has no DW_AT_import"),
10407 dwarf_tag_name (die->tag));
10408 return;
10409 }
10410
10411 imported_cu = cu;
10412 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10413 imported_name = dwarf2_name (imported_die, imported_cu);
10414 if (imported_name == NULL)
10415 {
10416 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10417
10418 The import in the following code:
10419 namespace A
10420 {
10421 typedef int B;
10422 }
10423
10424 int main ()
10425 {
10426 using A::B;
10427 B b;
10428 return b;
10429 }
10430
10431 ...
10432 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10433 <52> DW_AT_decl_file : 1
10434 <53> DW_AT_decl_line : 6
10435 <54> DW_AT_import : <0x75>
10436 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10437 <59> DW_AT_name : B
10438 <5b> DW_AT_decl_file : 1
10439 <5c> DW_AT_decl_line : 2
10440 <5d> DW_AT_type : <0x6e>
10441 ...
10442 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10443 <76> DW_AT_byte_size : 4
10444 <77> DW_AT_encoding : 5 (signed)
10445
10446 imports the wrong die ( 0x75 instead of 0x58 ).
10447 This case will be ignored until the gcc bug is fixed. */
10448 return;
10449 }
10450
10451 /* Figure out the local name after import. */
10452 import_alias = dwarf2_name (die, cu);
10453
10454 /* Figure out where the statement is being imported to. */
10455 import_prefix = determine_prefix (die, cu);
10456
10457 /* Figure out what the scope of the imported die is and prepend it
10458 to the name of the imported die. */
10459 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10460
10461 if (imported_die->tag != DW_TAG_namespace
10462 && imported_die->tag != DW_TAG_module)
10463 {
10464 imported_declaration = imported_name;
10465 canonical_name = imported_name_prefix;
10466 }
10467 else if (strlen (imported_name_prefix) > 0)
10468 canonical_name = obconcat (&objfile->objfile_obstack,
10469 imported_name_prefix,
10470 (cu->language == language_d ? "." : "::"),
10471 imported_name, (char *) NULL);
10472 else
10473 canonical_name = imported_name;
10474
10475 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10476 for (child_die = die->child; child_die && child_die->tag;
10477 child_die = sibling_die (child_die))
10478 {
10479 /* DWARF-4: A Fortran use statement with a “rename list” may be
10480 represented by an imported module entry with an import attribute
10481 referring to the module and owned entries corresponding to those
10482 entities that are renamed as part of being imported. */
10483
10484 if (child_die->tag != DW_TAG_imported_declaration)
10485 {
10486 complaint (_("child DW_TAG_imported_declaration expected "
10487 "- DIE at %s [in module %s]"),
10488 sect_offset_str (child_die->sect_off),
10489 objfile_name (objfile));
10490 continue;
10491 }
10492
10493 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10494 if (import_attr == NULL)
10495 {
10496 complaint (_("Tag '%s' has no DW_AT_import"),
10497 dwarf_tag_name (child_die->tag));
10498 continue;
10499 }
10500
10501 imported_cu = cu;
10502 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10503 &imported_cu);
10504 imported_name = dwarf2_name (imported_die, imported_cu);
10505 if (imported_name == NULL)
10506 {
10507 complaint (_("child DW_TAG_imported_declaration has unknown "
10508 "imported name - DIE at %s [in module %s]"),
10509 sect_offset_str (child_die->sect_off),
10510 objfile_name (objfile));
10511 continue;
10512 }
10513
10514 excludes.push_back (imported_name);
10515
10516 process_die (child_die, cu);
10517 }
10518
10519 add_using_directive (using_directives (cu),
10520 import_prefix,
10521 canonical_name,
10522 import_alias,
10523 imported_declaration,
10524 excludes,
10525 0,
10526 &objfile->objfile_obstack);
10527 }
10528
10529 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10530 types, but gives them a size of zero. Starting with version 14,
10531 ICC is compatible with GCC. */
10532
10533 static bool
10534 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10535 {
10536 if (!cu->checked_producer)
10537 check_producer (cu);
10538
10539 return cu->producer_is_icc_lt_14;
10540 }
10541
10542 /* ICC generates a DW_AT_type for C void functions. This was observed on
10543 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10544 which says that void functions should not have a DW_AT_type. */
10545
10546 static bool
10547 producer_is_icc (struct dwarf2_cu *cu)
10548 {
10549 if (!cu->checked_producer)
10550 check_producer (cu);
10551
10552 return cu->producer_is_icc;
10553 }
10554
10555 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10556 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10557 this, it was first present in GCC release 4.3.0. */
10558
10559 static bool
10560 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10561 {
10562 if (!cu->checked_producer)
10563 check_producer (cu);
10564
10565 return cu->producer_is_gcc_lt_4_3;
10566 }
10567
10568 static file_and_directory
10569 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10570 {
10571 file_and_directory res;
10572
10573 /* Find the filename. Do not use dwarf2_name here, since the filename
10574 is not a source language identifier. */
10575 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10576 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10577
10578 if (res.comp_dir == NULL
10579 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10580 && IS_ABSOLUTE_PATH (res.name))
10581 {
10582 res.comp_dir_storage = ldirname (res.name);
10583 if (!res.comp_dir_storage.empty ())
10584 res.comp_dir = res.comp_dir_storage.c_str ();
10585 }
10586 if (res.comp_dir != NULL)
10587 {
10588 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10589 directory, get rid of it. */
10590 const char *cp = strchr (res.comp_dir, ':');
10591
10592 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10593 res.comp_dir = cp + 1;
10594 }
10595
10596 if (res.name == NULL)
10597 res.name = "<unknown>";
10598
10599 return res;
10600 }
10601
10602 /* Handle DW_AT_stmt_list for a compilation unit.
10603 DIE is the DW_TAG_compile_unit die for CU.
10604 COMP_DIR is the compilation directory. LOWPC is passed to
10605 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10606
10607 static void
10608 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10609 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10610 {
10611 struct dwarf2_per_objfile *dwarf2_per_objfile
10612 = cu->per_cu->dwarf2_per_objfile;
10613 struct attribute *attr;
10614 struct line_header line_header_local;
10615 hashval_t line_header_local_hash;
10616 void **slot;
10617 int decode_mapping;
10618
10619 gdb_assert (! cu->per_cu->is_debug_types);
10620
10621 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10622 if (attr == NULL)
10623 return;
10624
10625 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10626
10627 /* The line header hash table is only created if needed (it exists to
10628 prevent redundant reading of the line table for partial_units).
10629 If we're given a partial_unit, we'll need it. If we're given a
10630 compile_unit, then use the line header hash table if it's already
10631 created, but don't create one just yet. */
10632
10633 if (dwarf2_per_objfile->line_header_hash == NULL
10634 && die->tag == DW_TAG_partial_unit)
10635 {
10636 dwarf2_per_objfile->line_header_hash
10637 .reset (htab_create_alloc (127, line_header_hash_voidp,
10638 line_header_eq_voidp,
10639 free_line_header_voidp,
10640 xcalloc, xfree));
10641 }
10642
10643 line_header_local.sect_off = line_offset;
10644 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10645 line_header_local_hash = line_header_hash (&line_header_local);
10646 if (dwarf2_per_objfile->line_header_hash != NULL)
10647 {
10648 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10649 &line_header_local,
10650 line_header_local_hash, NO_INSERT);
10651
10652 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10653 is not present in *SLOT (since if there is something in *SLOT then
10654 it will be for a partial_unit). */
10655 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10656 {
10657 gdb_assert (*slot != NULL);
10658 cu->line_header = (struct line_header *) *slot;
10659 return;
10660 }
10661 }
10662
10663 /* dwarf_decode_line_header does not yet provide sufficient information.
10664 We always have to call also dwarf_decode_lines for it. */
10665 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10666 if (lh == NULL)
10667 return;
10668
10669 cu->line_header = lh.release ();
10670 cu->line_header_die_owner = die;
10671
10672 if (dwarf2_per_objfile->line_header_hash == NULL)
10673 slot = NULL;
10674 else
10675 {
10676 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10677 &line_header_local,
10678 line_header_local_hash, INSERT);
10679 gdb_assert (slot != NULL);
10680 }
10681 if (slot != NULL && *slot == NULL)
10682 {
10683 /* This newly decoded line number information unit will be owned
10684 by line_header_hash hash table. */
10685 *slot = cu->line_header;
10686 cu->line_header_die_owner = NULL;
10687 }
10688 else
10689 {
10690 /* We cannot free any current entry in (*slot) as that struct line_header
10691 may be already used by multiple CUs. Create only temporary decoded
10692 line_header for this CU - it may happen at most once for each line
10693 number information unit. And if we're not using line_header_hash
10694 then this is what we want as well. */
10695 gdb_assert (die->tag != DW_TAG_partial_unit);
10696 }
10697 decode_mapping = (die->tag != DW_TAG_partial_unit);
10698 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10699 decode_mapping);
10700
10701 }
10702
10703 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10704
10705 static void
10706 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10707 {
10708 struct dwarf2_per_objfile *dwarf2_per_objfile
10709 = cu->per_cu->dwarf2_per_objfile;
10710 struct objfile *objfile = dwarf2_per_objfile->objfile;
10711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10712 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10713 CORE_ADDR highpc = ((CORE_ADDR) 0);
10714 struct attribute *attr;
10715 struct die_info *child_die;
10716 CORE_ADDR baseaddr;
10717
10718 prepare_one_comp_unit (cu, die, cu->language);
10719 baseaddr = objfile->text_section_offset ();
10720
10721 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10722
10723 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10724 from finish_block. */
10725 if (lowpc == ((CORE_ADDR) -1))
10726 lowpc = highpc;
10727 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10728
10729 file_and_directory fnd = find_file_and_directory (die, cu);
10730
10731 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10732 standardised yet. As a workaround for the language detection we fall
10733 back to the DW_AT_producer string. */
10734 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10735 cu->language = language_opencl;
10736
10737 /* Similar hack for Go. */
10738 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10739 set_cu_language (DW_LANG_Go, cu);
10740
10741 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10742
10743 /* Decode line number information if present. We do this before
10744 processing child DIEs, so that the line header table is available
10745 for DW_AT_decl_file. */
10746 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10747
10748 /* Process all dies in compilation unit. */
10749 if (die->child != NULL)
10750 {
10751 child_die = die->child;
10752 while (child_die && child_die->tag)
10753 {
10754 process_die (child_die, cu);
10755 child_die = sibling_die (child_die);
10756 }
10757 }
10758
10759 /* Decode macro information, if present. Dwarf 2 macro information
10760 refers to information in the line number info statement program
10761 header, so we can only read it if we've read the header
10762 successfully. */
10763 attr = dwarf2_attr (die, DW_AT_macros, cu);
10764 if (attr == NULL)
10765 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10766 if (attr && cu->line_header)
10767 {
10768 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10769 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10770
10771 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10772 }
10773 else
10774 {
10775 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10776 if (attr && cu->line_header)
10777 {
10778 unsigned int macro_offset = DW_UNSND (attr);
10779
10780 dwarf_decode_macros (cu, macro_offset, 0);
10781 }
10782 }
10783 }
10784
10785 void
10786 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10787 {
10788 struct type_unit_group *tu_group;
10789 int first_time;
10790 struct attribute *attr;
10791 unsigned int i;
10792 struct signatured_type *sig_type;
10793
10794 gdb_assert (per_cu->is_debug_types);
10795 sig_type = (struct signatured_type *) per_cu;
10796
10797 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10798
10799 /* If we're using .gdb_index (includes -readnow) then
10800 per_cu->type_unit_group may not have been set up yet. */
10801 if (sig_type->type_unit_group == NULL)
10802 sig_type->type_unit_group = get_type_unit_group (this, attr);
10803 tu_group = sig_type->type_unit_group;
10804
10805 /* If we've already processed this stmt_list there's no real need to
10806 do it again, we could fake it and just recreate the part we need
10807 (file name,index -> symtab mapping). If data shows this optimization
10808 is useful we can do it then. */
10809 first_time = tu_group->compunit_symtab == NULL;
10810
10811 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10812 debug info. */
10813 line_header_up lh;
10814 if (attr != NULL)
10815 {
10816 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10817 lh = dwarf_decode_line_header (line_offset, this);
10818 }
10819 if (lh == NULL)
10820 {
10821 if (first_time)
10822 start_symtab ("", NULL, 0);
10823 else
10824 {
10825 gdb_assert (tu_group->symtabs == NULL);
10826 gdb_assert (m_builder == nullptr);
10827 struct compunit_symtab *cust = tu_group->compunit_symtab;
10828 m_builder.reset (new struct buildsym_compunit
10829 (COMPUNIT_OBJFILE (cust), "",
10830 COMPUNIT_DIRNAME (cust),
10831 compunit_language (cust),
10832 0, cust));
10833 }
10834 return;
10835 }
10836
10837 line_header = lh.release ();
10838 line_header_die_owner = die;
10839
10840 if (first_time)
10841 {
10842 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10843
10844 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10845 still initializing it, and our caller (a few levels up)
10846 process_full_type_unit still needs to know if this is the first
10847 time. */
10848
10849 tu_group->symtabs
10850 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10851 struct symtab *, line_header->file_names_size ());
10852
10853 auto &file_names = line_header->file_names ();
10854 for (i = 0; i < file_names.size (); ++i)
10855 {
10856 file_entry &fe = file_names[i];
10857 dwarf2_start_subfile (this, fe.name,
10858 fe.include_dir (line_header));
10859 buildsym_compunit *b = get_builder ();
10860 if (b->get_current_subfile ()->symtab == NULL)
10861 {
10862 /* NOTE: start_subfile will recognize when it's been
10863 passed a file it has already seen. So we can't
10864 assume there's a simple mapping from
10865 cu->line_header->file_names to subfiles, plus
10866 cu->line_header->file_names may contain dups. */
10867 b->get_current_subfile ()->symtab
10868 = allocate_symtab (cust, b->get_current_subfile ()->name);
10869 }
10870
10871 fe.symtab = b->get_current_subfile ()->symtab;
10872 tu_group->symtabs[i] = fe.symtab;
10873 }
10874 }
10875 else
10876 {
10877 gdb_assert (m_builder == nullptr);
10878 struct compunit_symtab *cust = tu_group->compunit_symtab;
10879 m_builder.reset (new struct buildsym_compunit
10880 (COMPUNIT_OBJFILE (cust), "",
10881 COMPUNIT_DIRNAME (cust),
10882 compunit_language (cust),
10883 0, cust));
10884
10885 auto &file_names = line_header->file_names ();
10886 for (i = 0; i < file_names.size (); ++i)
10887 {
10888 file_entry &fe = file_names[i];
10889 fe.symtab = tu_group->symtabs[i];
10890 }
10891 }
10892
10893 /* The main symtab is allocated last. Type units don't have DW_AT_name
10894 so they don't have a "real" (so to speak) symtab anyway.
10895 There is later code that will assign the main symtab to all symbols
10896 that don't have one. We need to handle the case of a symbol with a
10897 missing symtab (DW_AT_decl_file) anyway. */
10898 }
10899
10900 /* Process DW_TAG_type_unit.
10901 For TUs we want to skip the first top level sibling if it's not the
10902 actual type being defined by this TU. In this case the first top
10903 level sibling is there to provide context only. */
10904
10905 static void
10906 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10907 {
10908 struct die_info *child_die;
10909
10910 prepare_one_comp_unit (cu, die, language_minimal);
10911
10912 /* Initialize (or reinitialize) the machinery for building symtabs.
10913 We do this before processing child DIEs, so that the line header table
10914 is available for DW_AT_decl_file. */
10915 cu->setup_type_unit_groups (die);
10916
10917 if (die->child != NULL)
10918 {
10919 child_die = die->child;
10920 while (child_die && child_die->tag)
10921 {
10922 process_die (child_die, cu);
10923 child_die = sibling_die (child_die);
10924 }
10925 }
10926 }
10927 \f
10928 /* DWO/DWP files.
10929
10930 http://gcc.gnu.org/wiki/DebugFission
10931 http://gcc.gnu.org/wiki/DebugFissionDWP
10932
10933 To simplify handling of both DWO files ("object" files with the DWARF info)
10934 and DWP files (a file with the DWOs packaged up into one file), we treat
10935 DWP files as having a collection of virtual DWO files. */
10936
10937 static hashval_t
10938 hash_dwo_file (const void *item)
10939 {
10940 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10941 hashval_t hash;
10942
10943 hash = htab_hash_string (dwo_file->dwo_name);
10944 if (dwo_file->comp_dir != NULL)
10945 hash += htab_hash_string (dwo_file->comp_dir);
10946 return hash;
10947 }
10948
10949 static int
10950 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10951 {
10952 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10953 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10954
10955 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10956 return 0;
10957 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10958 return lhs->comp_dir == rhs->comp_dir;
10959 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10960 }
10961
10962 /* Allocate a hash table for DWO files. */
10963
10964 static htab_up
10965 allocate_dwo_file_hash_table ()
10966 {
10967 auto delete_dwo_file = [] (void *item)
10968 {
10969 struct dwo_file *dwo_file = (struct dwo_file *) item;
10970
10971 delete dwo_file;
10972 };
10973
10974 return htab_up (htab_create_alloc (41,
10975 hash_dwo_file,
10976 eq_dwo_file,
10977 delete_dwo_file,
10978 xcalloc, xfree));
10979 }
10980
10981 /* Lookup DWO file DWO_NAME. */
10982
10983 static void **
10984 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10985 const char *dwo_name,
10986 const char *comp_dir)
10987 {
10988 struct dwo_file find_entry;
10989 void **slot;
10990
10991 if (dwarf2_per_objfile->dwo_files == NULL)
10992 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10993
10994 find_entry.dwo_name = dwo_name;
10995 find_entry.comp_dir = comp_dir;
10996 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10997 INSERT);
10998
10999 return slot;
11000 }
11001
11002 static hashval_t
11003 hash_dwo_unit (const void *item)
11004 {
11005 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11006
11007 /* This drops the top 32 bits of the id, but is ok for a hash. */
11008 return dwo_unit->signature;
11009 }
11010
11011 static int
11012 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11013 {
11014 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11015 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11016
11017 /* The signature is assumed to be unique within the DWO file.
11018 So while object file CU dwo_id's always have the value zero,
11019 that's OK, assuming each object file DWO file has only one CU,
11020 and that's the rule for now. */
11021 return lhs->signature == rhs->signature;
11022 }
11023
11024 /* Allocate a hash table for DWO CUs,TUs.
11025 There is one of these tables for each of CUs,TUs for each DWO file. */
11026
11027 static htab_up
11028 allocate_dwo_unit_table ()
11029 {
11030 /* Start out with a pretty small number.
11031 Generally DWO files contain only one CU and maybe some TUs. */
11032 return htab_up (htab_create_alloc (3,
11033 hash_dwo_unit,
11034 eq_dwo_unit,
11035 NULL, xcalloc, xfree));
11036 }
11037
11038 /* die_reader_func for create_dwo_cu. */
11039
11040 static void
11041 create_dwo_cu_reader (const struct die_reader_specs *reader,
11042 const gdb_byte *info_ptr,
11043 struct die_info *comp_unit_die,
11044 struct dwo_file *dwo_file,
11045 struct dwo_unit *dwo_unit)
11046 {
11047 struct dwarf2_cu *cu = reader->cu;
11048 sect_offset sect_off = cu->per_cu->sect_off;
11049 struct dwarf2_section_info *section = cu->per_cu->section;
11050
11051 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11052 if (!signature.has_value ())
11053 {
11054 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11055 " its dwo_id [in module %s]"),
11056 sect_offset_str (sect_off), dwo_file->dwo_name);
11057 return;
11058 }
11059
11060 dwo_unit->dwo_file = dwo_file;
11061 dwo_unit->signature = *signature;
11062 dwo_unit->section = section;
11063 dwo_unit->sect_off = sect_off;
11064 dwo_unit->length = cu->per_cu->length;
11065
11066 if (dwarf_read_debug)
11067 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11068 sect_offset_str (sect_off),
11069 hex_string (dwo_unit->signature));
11070 }
11071
11072 /* Create the dwo_units for the CUs in a DWO_FILE.
11073 Note: This function processes DWO files only, not DWP files. */
11074
11075 static void
11076 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11077 dwarf2_cu *cu, struct dwo_file &dwo_file,
11078 dwarf2_section_info &section, htab_up &cus_htab)
11079 {
11080 struct objfile *objfile = dwarf2_per_objfile->objfile;
11081 const gdb_byte *info_ptr, *end_ptr;
11082
11083 section.read (objfile);
11084 info_ptr = section.buffer;
11085
11086 if (info_ptr == NULL)
11087 return;
11088
11089 if (dwarf_read_debug)
11090 {
11091 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11092 section.get_name (),
11093 section.get_file_name ());
11094 }
11095
11096 end_ptr = info_ptr + section.size;
11097 while (info_ptr < end_ptr)
11098 {
11099 struct dwarf2_per_cu_data per_cu;
11100 struct dwo_unit read_unit {};
11101 struct dwo_unit *dwo_unit;
11102 void **slot;
11103 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11104
11105 memset (&per_cu, 0, sizeof (per_cu));
11106 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11107 per_cu.is_debug_types = 0;
11108 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11109 per_cu.section = &section;
11110
11111 cutu_reader reader (&per_cu, cu, &dwo_file);
11112 if (!reader.dummy_p)
11113 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11114 &dwo_file, &read_unit);
11115 info_ptr += per_cu.length;
11116
11117 // If the unit could not be parsed, skip it.
11118 if (read_unit.dwo_file == NULL)
11119 continue;
11120
11121 if (cus_htab == NULL)
11122 cus_htab = allocate_dwo_unit_table ();
11123
11124 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11125 *dwo_unit = read_unit;
11126 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11127 gdb_assert (slot != NULL);
11128 if (*slot != NULL)
11129 {
11130 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11131 sect_offset dup_sect_off = dup_cu->sect_off;
11132
11133 complaint (_("debug cu entry at offset %s is duplicate to"
11134 " the entry at offset %s, signature %s"),
11135 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11136 hex_string (dwo_unit->signature));
11137 }
11138 *slot = (void *)dwo_unit;
11139 }
11140 }
11141
11142 /* DWP file .debug_{cu,tu}_index section format:
11143 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11144
11145 DWP Version 1:
11146
11147 Both index sections have the same format, and serve to map a 64-bit
11148 signature to a set of section numbers. Each section begins with a header,
11149 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11150 indexes, and a pool of 32-bit section numbers. The index sections will be
11151 aligned at 8-byte boundaries in the file.
11152
11153 The index section header consists of:
11154
11155 V, 32 bit version number
11156 -, 32 bits unused
11157 N, 32 bit number of compilation units or type units in the index
11158 M, 32 bit number of slots in the hash table
11159
11160 Numbers are recorded using the byte order of the application binary.
11161
11162 The hash table begins at offset 16 in the section, and consists of an array
11163 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11164 order of the application binary). Unused slots in the hash table are 0.
11165 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11166
11167 The parallel table begins immediately after the hash table
11168 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11169 array of 32-bit indexes (using the byte order of the application binary),
11170 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11171 table contains a 32-bit index into the pool of section numbers. For unused
11172 hash table slots, the corresponding entry in the parallel table will be 0.
11173
11174 The pool of section numbers begins immediately following the hash table
11175 (at offset 16 + 12 * M from the beginning of the section). The pool of
11176 section numbers consists of an array of 32-bit words (using the byte order
11177 of the application binary). Each item in the array is indexed starting
11178 from 0. The hash table entry provides the index of the first section
11179 number in the set. Additional section numbers in the set follow, and the
11180 set is terminated by a 0 entry (section number 0 is not used in ELF).
11181
11182 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11183 section must be the first entry in the set, and the .debug_abbrev.dwo must
11184 be the second entry. Other members of the set may follow in any order.
11185
11186 ---
11187
11188 DWP Version 2:
11189
11190 DWP Version 2 combines all the .debug_info, etc. sections into one,
11191 and the entries in the index tables are now offsets into these sections.
11192 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11193 section.
11194
11195 Index Section Contents:
11196 Header
11197 Hash Table of Signatures dwp_hash_table.hash_table
11198 Parallel Table of Indices dwp_hash_table.unit_table
11199 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11200 Table of Section Sizes dwp_hash_table.v2.sizes
11201
11202 The index section header consists of:
11203
11204 V, 32 bit version number
11205 L, 32 bit number of columns in the table of section offsets
11206 N, 32 bit number of compilation units or type units in the index
11207 M, 32 bit number of slots in the hash table
11208
11209 Numbers are recorded using the byte order of the application binary.
11210
11211 The hash table has the same format as version 1.
11212 The parallel table of indices has the same format as version 1,
11213 except that the entries are origin-1 indices into the table of sections
11214 offsets and the table of section sizes.
11215
11216 The table of offsets begins immediately following the parallel table
11217 (at offset 16 + 12 * M from the beginning of the section). The table is
11218 a two-dimensional array of 32-bit words (using the byte order of the
11219 application binary), with L columns and N+1 rows, in row-major order.
11220 Each row in the array is indexed starting from 0. The first row provides
11221 a key to the remaining rows: each column in this row provides an identifier
11222 for a debug section, and the offsets in the same column of subsequent rows
11223 refer to that section. The section identifiers are:
11224
11225 DW_SECT_INFO 1 .debug_info.dwo
11226 DW_SECT_TYPES 2 .debug_types.dwo
11227 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11228 DW_SECT_LINE 4 .debug_line.dwo
11229 DW_SECT_LOC 5 .debug_loc.dwo
11230 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11231 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11232 DW_SECT_MACRO 8 .debug_macro.dwo
11233
11234 The offsets provided by the CU and TU index sections are the base offsets
11235 for the contributions made by each CU or TU to the corresponding section
11236 in the package file. Each CU and TU header contains an abbrev_offset
11237 field, used to find the abbreviations table for that CU or TU within the
11238 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11239 be interpreted as relative to the base offset given in the index section.
11240 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11241 should be interpreted as relative to the base offset for .debug_line.dwo,
11242 and offsets into other debug sections obtained from DWARF attributes should
11243 also be interpreted as relative to the corresponding base offset.
11244
11245 The table of sizes begins immediately following the table of offsets.
11246 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11247 with L columns and N rows, in row-major order. Each row in the array is
11248 indexed starting from 1 (row 0 is shared by the two tables).
11249
11250 ---
11251
11252 Hash table lookup is handled the same in version 1 and 2:
11253
11254 We assume that N and M will not exceed 2^32 - 1.
11255 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11256
11257 Given a 64-bit compilation unit signature or a type signature S, an entry
11258 in the hash table is located as follows:
11259
11260 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11261 the low-order k bits all set to 1.
11262
11263 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11264
11265 3) If the hash table entry at index H matches the signature, use that
11266 entry. If the hash table entry at index H is unused (all zeroes),
11267 terminate the search: the signature is not present in the table.
11268
11269 4) Let H = (H + H') modulo M. Repeat at Step 3.
11270
11271 Because M > N and H' and M are relatively prime, the search is guaranteed
11272 to stop at an unused slot or find the match. */
11273
11274 /* Create a hash table to map DWO IDs to their CU/TU entry in
11275 .debug_{info,types}.dwo in DWP_FILE.
11276 Returns NULL if there isn't one.
11277 Note: This function processes DWP files only, not DWO files. */
11278
11279 static struct dwp_hash_table *
11280 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11281 struct dwp_file *dwp_file, int is_debug_types)
11282 {
11283 struct objfile *objfile = dwarf2_per_objfile->objfile;
11284 bfd *dbfd = dwp_file->dbfd.get ();
11285 const gdb_byte *index_ptr, *index_end;
11286 struct dwarf2_section_info *index;
11287 uint32_t version, nr_columns, nr_units, nr_slots;
11288 struct dwp_hash_table *htab;
11289
11290 if (is_debug_types)
11291 index = &dwp_file->sections.tu_index;
11292 else
11293 index = &dwp_file->sections.cu_index;
11294
11295 if (index->empty ())
11296 return NULL;
11297 index->read (objfile);
11298
11299 index_ptr = index->buffer;
11300 index_end = index_ptr + index->size;
11301
11302 version = read_4_bytes (dbfd, index_ptr);
11303 index_ptr += 4;
11304 if (version == 2)
11305 nr_columns = read_4_bytes (dbfd, index_ptr);
11306 else
11307 nr_columns = 0;
11308 index_ptr += 4;
11309 nr_units = read_4_bytes (dbfd, index_ptr);
11310 index_ptr += 4;
11311 nr_slots = read_4_bytes (dbfd, index_ptr);
11312 index_ptr += 4;
11313
11314 if (version != 1 && version != 2)
11315 {
11316 error (_("Dwarf Error: unsupported DWP file version (%s)"
11317 " [in module %s]"),
11318 pulongest (version), dwp_file->name);
11319 }
11320 if (nr_slots != (nr_slots & -nr_slots))
11321 {
11322 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11323 " is not power of 2 [in module %s]"),
11324 pulongest (nr_slots), dwp_file->name);
11325 }
11326
11327 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11328 htab->version = version;
11329 htab->nr_columns = nr_columns;
11330 htab->nr_units = nr_units;
11331 htab->nr_slots = nr_slots;
11332 htab->hash_table = index_ptr;
11333 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11334
11335 /* Exit early if the table is empty. */
11336 if (nr_slots == 0 || nr_units == 0
11337 || (version == 2 && nr_columns == 0))
11338 {
11339 /* All must be zero. */
11340 if (nr_slots != 0 || nr_units != 0
11341 || (version == 2 && nr_columns != 0))
11342 {
11343 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11344 " all zero [in modules %s]"),
11345 dwp_file->name);
11346 }
11347 return htab;
11348 }
11349
11350 if (version == 1)
11351 {
11352 htab->section_pool.v1.indices =
11353 htab->unit_table + sizeof (uint32_t) * nr_slots;
11354 /* It's harder to decide whether the section is too small in v1.
11355 V1 is deprecated anyway so we punt. */
11356 }
11357 else
11358 {
11359 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11360 int *ids = htab->section_pool.v2.section_ids;
11361 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11362 /* Reverse map for error checking. */
11363 int ids_seen[DW_SECT_MAX + 1];
11364 int i;
11365
11366 if (nr_columns < 2)
11367 {
11368 error (_("Dwarf Error: bad DWP hash table, too few columns"
11369 " in section table [in module %s]"),
11370 dwp_file->name);
11371 }
11372 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11373 {
11374 error (_("Dwarf Error: bad DWP hash table, too many columns"
11375 " in section table [in module %s]"),
11376 dwp_file->name);
11377 }
11378 memset (ids, 255, sizeof_ids);
11379 memset (ids_seen, 255, sizeof (ids_seen));
11380 for (i = 0; i < nr_columns; ++i)
11381 {
11382 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11383
11384 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11385 {
11386 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11387 " in section table [in module %s]"),
11388 id, dwp_file->name);
11389 }
11390 if (ids_seen[id] != -1)
11391 {
11392 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11393 " id %d in section table [in module %s]"),
11394 id, dwp_file->name);
11395 }
11396 ids_seen[id] = i;
11397 ids[i] = id;
11398 }
11399 /* Must have exactly one info or types section. */
11400 if (((ids_seen[DW_SECT_INFO] != -1)
11401 + (ids_seen[DW_SECT_TYPES] != -1))
11402 != 1)
11403 {
11404 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11405 " DWO info/types section [in module %s]"),
11406 dwp_file->name);
11407 }
11408 /* Must have an abbrev section. */
11409 if (ids_seen[DW_SECT_ABBREV] == -1)
11410 {
11411 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11412 " section [in module %s]"),
11413 dwp_file->name);
11414 }
11415 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11416 htab->section_pool.v2.sizes =
11417 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11418 * nr_units * nr_columns);
11419 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11420 * nr_units * nr_columns))
11421 > index_end)
11422 {
11423 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11424 " [in module %s]"),
11425 dwp_file->name);
11426 }
11427 }
11428
11429 return htab;
11430 }
11431
11432 /* Update SECTIONS with the data from SECTP.
11433
11434 This function is like the other "locate" section routines that are
11435 passed to bfd_map_over_sections, but in this context the sections to
11436 read comes from the DWP V1 hash table, not the full ELF section table.
11437
11438 The result is non-zero for success, or zero if an error was found. */
11439
11440 static int
11441 locate_v1_virtual_dwo_sections (asection *sectp,
11442 struct virtual_v1_dwo_sections *sections)
11443 {
11444 const struct dwop_section_names *names = &dwop_section_names;
11445
11446 if (section_is_p (sectp->name, &names->abbrev_dwo))
11447 {
11448 /* There can be only one. */
11449 if (sections->abbrev.s.section != NULL)
11450 return 0;
11451 sections->abbrev.s.section = sectp;
11452 sections->abbrev.size = bfd_section_size (sectp);
11453 }
11454 else if (section_is_p (sectp->name, &names->info_dwo)
11455 || section_is_p (sectp->name, &names->types_dwo))
11456 {
11457 /* There can be only one. */
11458 if (sections->info_or_types.s.section != NULL)
11459 return 0;
11460 sections->info_or_types.s.section = sectp;
11461 sections->info_or_types.size = bfd_section_size (sectp);
11462 }
11463 else if (section_is_p (sectp->name, &names->line_dwo))
11464 {
11465 /* There can be only one. */
11466 if (sections->line.s.section != NULL)
11467 return 0;
11468 sections->line.s.section = sectp;
11469 sections->line.size = bfd_section_size (sectp);
11470 }
11471 else if (section_is_p (sectp->name, &names->loc_dwo))
11472 {
11473 /* There can be only one. */
11474 if (sections->loc.s.section != NULL)
11475 return 0;
11476 sections->loc.s.section = sectp;
11477 sections->loc.size = bfd_section_size (sectp);
11478 }
11479 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11480 {
11481 /* There can be only one. */
11482 if (sections->macinfo.s.section != NULL)
11483 return 0;
11484 sections->macinfo.s.section = sectp;
11485 sections->macinfo.size = bfd_section_size (sectp);
11486 }
11487 else if (section_is_p (sectp->name, &names->macro_dwo))
11488 {
11489 /* There can be only one. */
11490 if (sections->macro.s.section != NULL)
11491 return 0;
11492 sections->macro.s.section = sectp;
11493 sections->macro.size = bfd_section_size (sectp);
11494 }
11495 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11496 {
11497 /* There can be only one. */
11498 if (sections->str_offsets.s.section != NULL)
11499 return 0;
11500 sections->str_offsets.s.section = sectp;
11501 sections->str_offsets.size = bfd_section_size (sectp);
11502 }
11503 else
11504 {
11505 /* No other kind of section is valid. */
11506 return 0;
11507 }
11508
11509 return 1;
11510 }
11511
11512 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11513 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11514 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11515 This is for DWP version 1 files. */
11516
11517 static struct dwo_unit *
11518 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11519 struct dwp_file *dwp_file,
11520 uint32_t unit_index,
11521 const char *comp_dir,
11522 ULONGEST signature, int is_debug_types)
11523 {
11524 struct objfile *objfile = dwarf2_per_objfile->objfile;
11525 const struct dwp_hash_table *dwp_htab =
11526 is_debug_types ? dwp_file->tus : dwp_file->cus;
11527 bfd *dbfd = dwp_file->dbfd.get ();
11528 const char *kind = is_debug_types ? "TU" : "CU";
11529 struct dwo_file *dwo_file;
11530 struct dwo_unit *dwo_unit;
11531 struct virtual_v1_dwo_sections sections;
11532 void **dwo_file_slot;
11533 int i;
11534
11535 gdb_assert (dwp_file->version == 1);
11536
11537 if (dwarf_read_debug)
11538 {
11539 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11540 kind,
11541 pulongest (unit_index), hex_string (signature),
11542 dwp_file->name);
11543 }
11544
11545 /* Fetch the sections of this DWO unit.
11546 Put a limit on the number of sections we look for so that bad data
11547 doesn't cause us to loop forever. */
11548
11549 #define MAX_NR_V1_DWO_SECTIONS \
11550 (1 /* .debug_info or .debug_types */ \
11551 + 1 /* .debug_abbrev */ \
11552 + 1 /* .debug_line */ \
11553 + 1 /* .debug_loc */ \
11554 + 1 /* .debug_str_offsets */ \
11555 + 1 /* .debug_macro or .debug_macinfo */ \
11556 + 1 /* trailing zero */)
11557
11558 memset (&sections, 0, sizeof (sections));
11559
11560 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11561 {
11562 asection *sectp;
11563 uint32_t section_nr =
11564 read_4_bytes (dbfd,
11565 dwp_htab->section_pool.v1.indices
11566 + (unit_index + i) * sizeof (uint32_t));
11567
11568 if (section_nr == 0)
11569 break;
11570 if (section_nr >= dwp_file->num_sections)
11571 {
11572 error (_("Dwarf Error: bad DWP hash table, section number too large"
11573 " [in module %s]"),
11574 dwp_file->name);
11575 }
11576
11577 sectp = dwp_file->elf_sections[section_nr];
11578 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11581 " [in module %s]"),
11582 dwp_file->name);
11583 }
11584 }
11585
11586 if (i < 2
11587 || sections.info_or_types.empty ()
11588 || sections.abbrev.empty ())
11589 {
11590 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11591 " [in module %s]"),
11592 dwp_file->name);
11593 }
11594 if (i == MAX_NR_V1_DWO_SECTIONS)
11595 {
11596 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11597 " [in module %s]"),
11598 dwp_file->name);
11599 }
11600
11601 /* It's easier for the rest of the code if we fake a struct dwo_file and
11602 have dwo_unit "live" in that. At least for now.
11603
11604 The DWP file can be made up of a random collection of CUs and TUs.
11605 However, for each CU + set of TUs that came from the same original DWO
11606 file, we can combine them back into a virtual DWO file to save space
11607 (fewer struct dwo_file objects to allocate). Remember that for really
11608 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11609
11610 std::string virtual_dwo_name =
11611 string_printf ("virtual-dwo/%d-%d-%d-%d",
11612 sections.abbrev.get_id (),
11613 sections.line.get_id (),
11614 sections.loc.get_id (),
11615 sections.str_offsets.get_id ());
11616 /* Can we use an existing virtual DWO file? */
11617 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11618 virtual_dwo_name.c_str (),
11619 comp_dir);
11620 /* Create one if necessary. */
11621 if (*dwo_file_slot == NULL)
11622 {
11623 if (dwarf_read_debug)
11624 {
11625 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11626 virtual_dwo_name.c_str ());
11627 }
11628 dwo_file = new struct dwo_file;
11629 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11630 dwo_file->comp_dir = comp_dir;
11631 dwo_file->sections.abbrev = sections.abbrev;
11632 dwo_file->sections.line = sections.line;
11633 dwo_file->sections.loc = sections.loc;
11634 dwo_file->sections.macinfo = sections.macinfo;
11635 dwo_file->sections.macro = sections.macro;
11636 dwo_file->sections.str_offsets = sections.str_offsets;
11637 /* The "str" section is global to the entire DWP file. */
11638 dwo_file->sections.str = dwp_file->sections.str;
11639 /* The info or types section is assigned below to dwo_unit,
11640 there's no need to record it in dwo_file.
11641 Also, we can't simply record type sections in dwo_file because
11642 we record a pointer into the vector in dwo_unit. As we collect more
11643 types we'll grow the vector and eventually have to reallocate space
11644 for it, invalidating all copies of pointers into the previous
11645 contents. */
11646 *dwo_file_slot = dwo_file;
11647 }
11648 else
11649 {
11650 if (dwarf_read_debug)
11651 {
11652 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11653 virtual_dwo_name.c_str ());
11654 }
11655 dwo_file = (struct dwo_file *) *dwo_file_slot;
11656 }
11657
11658 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11659 dwo_unit->dwo_file = dwo_file;
11660 dwo_unit->signature = signature;
11661 dwo_unit->section =
11662 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11663 *dwo_unit->section = sections.info_or_types;
11664 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11665
11666 return dwo_unit;
11667 }
11668
11669 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11670 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11671 piece within that section used by a TU/CU, return a virtual section
11672 of just that piece. */
11673
11674 static struct dwarf2_section_info
11675 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11676 struct dwarf2_section_info *section,
11677 bfd_size_type offset, bfd_size_type size)
11678 {
11679 struct dwarf2_section_info result;
11680 asection *sectp;
11681
11682 gdb_assert (section != NULL);
11683 gdb_assert (!section->is_virtual);
11684
11685 memset (&result, 0, sizeof (result));
11686 result.s.containing_section = section;
11687 result.is_virtual = true;
11688
11689 if (size == 0)
11690 return result;
11691
11692 sectp = section->get_bfd_section ();
11693
11694 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11695 bounds of the real section. This is a pretty-rare event, so just
11696 flag an error (easier) instead of a warning and trying to cope. */
11697 if (sectp == NULL
11698 || offset + size > bfd_section_size (sectp))
11699 {
11700 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11701 " in section %s [in module %s]"),
11702 sectp ? bfd_section_name (sectp) : "<unknown>",
11703 objfile_name (dwarf2_per_objfile->objfile));
11704 }
11705
11706 result.virtual_offset = offset;
11707 result.size = size;
11708 return result;
11709 }
11710
11711 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11712 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11713 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11714 This is for DWP version 2 files. */
11715
11716 static struct dwo_unit *
11717 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11718 struct dwp_file *dwp_file,
11719 uint32_t unit_index,
11720 const char *comp_dir,
11721 ULONGEST signature, int is_debug_types)
11722 {
11723 struct objfile *objfile = dwarf2_per_objfile->objfile;
11724 const struct dwp_hash_table *dwp_htab =
11725 is_debug_types ? dwp_file->tus : dwp_file->cus;
11726 bfd *dbfd = dwp_file->dbfd.get ();
11727 const char *kind = is_debug_types ? "TU" : "CU";
11728 struct dwo_file *dwo_file;
11729 struct dwo_unit *dwo_unit;
11730 struct virtual_v2_dwo_sections sections;
11731 void **dwo_file_slot;
11732 int i;
11733
11734 gdb_assert (dwp_file->version == 2);
11735
11736 if (dwarf_read_debug)
11737 {
11738 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11739 kind,
11740 pulongest (unit_index), hex_string (signature),
11741 dwp_file->name);
11742 }
11743
11744 /* Fetch the section offsets of this DWO unit. */
11745
11746 memset (&sections, 0, sizeof (sections));
11747
11748 for (i = 0; i < dwp_htab->nr_columns; ++i)
11749 {
11750 uint32_t offset = read_4_bytes (dbfd,
11751 dwp_htab->section_pool.v2.offsets
11752 + (((unit_index - 1) * dwp_htab->nr_columns
11753 + i)
11754 * sizeof (uint32_t)));
11755 uint32_t size = read_4_bytes (dbfd,
11756 dwp_htab->section_pool.v2.sizes
11757 + (((unit_index - 1) * dwp_htab->nr_columns
11758 + i)
11759 * sizeof (uint32_t)));
11760
11761 switch (dwp_htab->section_pool.v2.section_ids[i])
11762 {
11763 case DW_SECT_INFO:
11764 case DW_SECT_TYPES:
11765 sections.info_or_types_offset = offset;
11766 sections.info_or_types_size = size;
11767 break;
11768 case DW_SECT_ABBREV:
11769 sections.abbrev_offset = offset;
11770 sections.abbrev_size = size;
11771 break;
11772 case DW_SECT_LINE:
11773 sections.line_offset = offset;
11774 sections.line_size = size;
11775 break;
11776 case DW_SECT_LOC:
11777 sections.loc_offset = offset;
11778 sections.loc_size = size;
11779 break;
11780 case DW_SECT_STR_OFFSETS:
11781 sections.str_offsets_offset = offset;
11782 sections.str_offsets_size = size;
11783 break;
11784 case DW_SECT_MACINFO:
11785 sections.macinfo_offset = offset;
11786 sections.macinfo_size = size;
11787 break;
11788 case DW_SECT_MACRO:
11789 sections.macro_offset = offset;
11790 sections.macro_size = size;
11791 break;
11792 }
11793 }
11794
11795 /* It's easier for the rest of the code if we fake a struct dwo_file and
11796 have dwo_unit "live" in that. At least for now.
11797
11798 The DWP file can be made up of a random collection of CUs and TUs.
11799 However, for each CU + set of TUs that came from the same original DWO
11800 file, we can combine them back into a virtual DWO file to save space
11801 (fewer struct dwo_file objects to allocate). Remember that for really
11802 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11803
11804 std::string virtual_dwo_name =
11805 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11806 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11807 (long) (sections.line_size ? sections.line_offset : 0),
11808 (long) (sections.loc_size ? sections.loc_offset : 0),
11809 (long) (sections.str_offsets_size
11810 ? sections.str_offsets_offset : 0));
11811 /* Can we use an existing virtual DWO file? */
11812 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11813 virtual_dwo_name.c_str (),
11814 comp_dir);
11815 /* Create one if necessary. */
11816 if (*dwo_file_slot == NULL)
11817 {
11818 if (dwarf_read_debug)
11819 {
11820 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11821 virtual_dwo_name.c_str ());
11822 }
11823 dwo_file = new struct dwo_file;
11824 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11825 dwo_file->comp_dir = comp_dir;
11826 dwo_file->sections.abbrev =
11827 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11828 sections.abbrev_offset, sections.abbrev_size);
11829 dwo_file->sections.line =
11830 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11831 sections.line_offset, sections.line_size);
11832 dwo_file->sections.loc =
11833 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11834 sections.loc_offset, sections.loc_size);
11835 dwo_file->sections.macinfo =
11836 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11837 sections.macinfo_offset, sections.macinfo_size);
11838 dwo_file->sections.macro =
11839 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11840 sections.macro_offset, sections.macro_size);
11841 dwo_file->sections.str_offsets =
11842 create_dwp_v2_section (dwarf2_per_objfile,
11843 &dwp_file->sections.str_offsets,
11844 sections.str_offsets_offset,
11845 sections.str_offsets_size);
11846 /* The "str" section is global to the entire DWP file. */
11847 dwo_file->sections.str = dwp_file->sections.str;
11848 /* The info or types section is assigned below to dwo_unit,
11849 there's no need to record it in dwo_file.
11850 Also, we can't simply record type sections in dwo_file because
11851 we record a pointer into the vector in dwo_unit. As we collect more
11852 types we'll grow the vector and eventually have to reallocate space
11853 for it, invalidating all copies of pointers into the previous
11854 contents. */
11855 *dwo_file_slot = dwo_file;
11856 }
11857 else
11858 {
11859 if (dwarf_read_debug)
11860 {
11861 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11862 virtual_dwo_name.c_str ());
11863 }
11864 dwo_file = (struct dwo_file *) *dwo_file_slot;
11865 }
11866
11867 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11868 dwo_unit->dwo_file = dwo_file;
11869 dwo_unit->signature = signature;
11870 dwo_unit->section =
11871 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11872 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11873 is_debug_types
11874 ? &dwp_file->sections.types
11875 : &dwp_file->sections.info,
11876 sections.info_or_types_offset,
11877 sections.info_or_types_size);
11878 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11879
11880 return dwo_unit;
11881 }
11882
11883 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11884 Returns NULL if the signature isn't found. */
11885
11886 static struct dwo_unit *
11887 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11888 struct dwp_file *dwp_file, const char *comp_dir,
11889 ULONGEST signature, int is_debug_types)
11890 {
11891 const struct dwp_hash_table *dwp_htab =
11892 is_debug_types ? dwp_file->tus : dwp_file->cus;
11893 bfd *dbfd = dwp_file->dbfd.get ();
11894 uint32_t mask = dwp_htab->nr_slots - 1;
11895 uint32_t hash = signature & mask;
11896 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11897 unsigned int i;
11898 void **slot;
11899 struct dwo_unit find_dwo_cu;
11900
11901 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11902 find_dwo_cu.signature = signature;
11903 slot = htab_find_slot (is_debug_types
11904 ? dwp_file->loaded_tus.get ()
11905 : dwp_file->loaded_cus.get (),
11906 &find_dwo_cu, INSERT);
11907
11908 if (*slot != NULL)
11909 return (struct dwo_unit *) *slot;
11910
11911 /* Use a for loop so that we don't loop forever on bad debug info. */
11912 for (i = 0; i < dwp_htab->nr_slots; ++i)
11913 {
11914 ULONGEST signature_in_table;
11915
11916 signature_in_table =
11917 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11918 if (signature_in_table == signature)
11919 {
11920 uint32_t unit_index =
11921 read_4_bytes (dbfd,
11922 dwp_htab->unit_table + hash * sizeof (uint32_t));
11923
11924 if (dwp_file->version == 1)
11925 {
11926 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11927 dwp_file, unit_index,
11928 comp_dir, signature,
11929 is_debug_types);
11930 }
11931 else
11932 {
11933 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11934 dwp_file, unit_index,
11935 comp_dir, signature,
11936 is_debug_types);
11937 }
11938 return (struct dwo_unit *) *slot;
11939 }
11940 if (signature_in_table == 0)
11941 return NULL;
11942 hash = (hash + hash2) & mask;
11943 }
11944
11945 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11946 " [in module %s]"),
11947 dwp_file->name);
11948 }
11949
11950 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11951 Open the file specified by FILE_NAME and hand it off to BFD for
11952 preliminary analysis. Return a newly initialized bfd *, which
11953 includes a canonicalized copy of FILE_NAME.
11954 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11955 SEARCH_CWD is true if the current directory is to be searched.
11956 It will be searched before debug-file-directory.
11957 If successful, the file is added to the bfd include table of the
11958 objfile's bfd (see gdb_bfd_record_inclusion).
11959 If unable to find/open the file, return NULL.
11960 NOTE: This function is derived from symfile_bfd_open. */
11961
11962 static gdb_bfd_ref_ptr
11963 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11964 const char *file_name, int is_dwp, int search_cwd)
11965 {
11966 int desc;
11967 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11968 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11969 to debug_file_directory. */
11970 const char *search_path;
11971 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11972
11973 gdb::unique_xmalloc_ptr<char> search_path_holder;
11974 if (search_cwd)
11975 {
11976 if (*debug_file_directory != '\0')
11977 {
11978 search_path_holder.reset (concat (".", dirname_separator_string,
11979 debug_file_directory,
11980 (char *) NULL));
11981 search_path = search_path_holder.get ();
11982 }
11983 else
11984 search_path = ".";
11985 }
11986 else
11987 search_path = debug_file_directory;
11988
11989 openp_flags flags = OPF_RETURN_REALPATH;
11990 if (is_dwp)
11991 flags |= OPF_SEARCH_IN_PATH;
11992
11993 gdb::unique_xmalloc_ptr<char> absolute_name;
11994 desc = openp (search_path, flags, file_name,
11995 O_RDONLY | O_BINARY, &absolute_name);
11996 if (desc < 0)
11997 return NULL;
11998
11999 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12000 gnutarget, desc));
12001 if (sym_bfd == NULL)
12002 return NULL;
12003 bfd_set_cacheable (sym_bfd.get (), 1);
12004
12005 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12006 return NULL;
12007
12008 /* Success. Record the bfd as having been included by the objfile's bfd.
12009 This is important because things like demangled_names_hash lives in the
12010 objfile's per_bfd space and may have references to things like symbol
12011 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12012 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12013
12014 return sym_bfd;
12015 }
12016
12017 /* Try to open DWO file FILE_NAME.
12018 COMP_DIR is the DW_AT_comp_dir attribute.
12019 The result is the bfd handle of the file.
12020 If there is a problem finding or opening the file, return NULL.
12021 Upon success, the canonicalized path of the file is stored in the bfd,
12022 same as symfile_bfd_open. */
12023
12024 static gdb_bfd_ref_ptr
12025 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12026 const char *file_name, const char *comp_dir)
12027 {
12028 if (IS_ABSOLUTE_PATH (file_name))
12029 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12030 0 /*is_dwp*/, 0 /*search_cwd*/);
12031
12032 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12033
12034 if (comp_dir != NULL)
12035 {
12036 gdb::unique_xmalloc_ptr<char> path_to_try
12037 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12038
12039 /* NOTE: If comp_dir is a relative path, this will also try the
12040 search path, which seems useful. */
12041 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12042 path_to_try.get (),
12043 0 /*is_dwp*/,
12044 1 /*search_cwd*/));
12045 if (abfd != NULL)
12046 return abfd;
12047 }
12048
12049 /* That didn't work, try debug-file-directory, which, despite its name,
12050 is a list of paths. */
12051
12052 if (*debug_file_directory == '\0')
12053 return NULL;
12054
12055 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12056 0 /*is_dwp*/, 1 /*search_cwd*/);
12057 }
12058
12059 /* This function is mapped across the sections and remembers the offset and
12060 size of each of the DWO debugging sections we are interested in. */
12061
12062 static void
12063 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12064 {
12065 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12066 const struct dwop_section_names *names = &dwop_section_names;
12067
12068 if (section_is_p (sectp->name, &names->abbrev_dwo))
12069 {
12070 dwo_sections->abbrev.s.section = sectp;
12071 dwo_sections->abbrev.size = bfd_section_size (sectp);
12072 }
12073 else if (section_is_p (sectp->name, &names->info_dwo))
12074 {
12075 dwo_sections->info.s.section = sectp;
12076 dwo_sections->info.size = bfd_section_size (sectp);
12077 }
12078 else if (section_is_p (sectp->name, &names->line_dwo))
12079 {
12080 dwo_sections->line.s.section = sectp;
12081 dwo_sections->line.size = bfd_section_size (sectp);
12082 }
12083 else if (section_is_p (sectp->name, &names->loc_dwo))
12084 {
12085 dwo_sections->loc.s.section = sectp;
12086 dwo_sections->loc.size = bfd_section_size (sectp);
12087 }
12088 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12089 {
12090 dwo_sections->macinfo.s.section = sectp;
12091 dwo_sections->macinfo.size = bfd_section_size (sectp);
12092 }
12093 else if (section_is_p (sectp->name, &names->macro_dwo))
12094 {
12095 dwo_sections->macro.s.section = sectp;
12096 dwo_sections->macro.size = bfd_section_size (sectp);
12097 }
12098 else if (section_is_p (sectp->name, &names->str_dwo))
12099 {
12100 dwo_sections->str.s.section = sectp;
12101 dwo_sections->str.size = bfd_section_size (sectp);
12102 }
12103 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12104 {
12105 dwo_sections->str_offsets.s.section = sectp;
12106 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12107 }
12108 else if (section_is_p (sectp->name, &names->types_dwo))
12109 {
12110 struct dwarf2_section_info type_section;
12111
12112 memset (&type_section, 0, sizeof (type_section));
12113 type_section.s.section = sectp;
12114 type_section.size = bfd_section_size (sectp);
12115 dwo_sections->types.push_back (type_section);
12116 }
12117 }
12118
12119 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12120 by PER_CU. This is for the non-DWP case.
12121 The result is NULL if DWO_NAME can't be found. */
12122
12123 static struct dwo_file *
12124 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12125 const char *dwo_name, const char *comp_dir)
12126 {
12127 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12128
12129 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12130 if (dbfd == NULL)
12131 {
12132 if (dwarf_read_debug)
12133 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12134 return NULL;
12135 }
12136
12137 dwo_file_up dwo_file (new struct dwo_file);
12138 dwo_file->dwo_name = dwo_name;
12139 dwo_file->comp_dir = comp_dir;
12140 dwo_file->dbfd = std::move (dbfd);
12141
12142 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12143 &dwo_file->sections);
12144
12145 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12146 dwo_file->sections.info, dwo_file->cus);
12147
12148 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12149 dwo_file->sections.types, dwo_file->tus);
12150
12151 if (dwarf_read_debug)
12152 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12153
12154 return dwo_file.release ();
12155 }
12156
12157 /* This function is mapped across the sections and remembers the offset and
12158 size of each of the DWP debugging sections common to version 1 and 2 that
12159 we are interested in. */
12160
12161 static void
12162 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12163 void *dwp_file_ptr)
12164 {
12165 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12166 const struct dwop_section_names *names = &dwop_section_names;
12167 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12168
12169 /* Record the ELF section number for later lookup: this is what the
12170 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12171 gdb_assert (elf_section_nr < dwp_file->num_sections);
12172 dwp_file->elf_sections[elf_section_nr] = sectp;
12173
12174 /* Look for specific sections that we need. */
12175 if (section_is_p (sectp->name, &names->str_dwo))
12176 {
12177 dwp_file->sections.str.s.section = sectp;
12178 dwp_file->sections.str.size = bfd_section_size (sectp);
12179 }
12180 else if (section_is_p (sectp->name, &names->cu_index))
12181 {
12182 dwp_file->sections.cu_index.s.section = sectp;
12183 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12184 }
12185 else if (section_is_p (sectp->name, &names->tu_index))
12186 {
12187 dwp_file->sections.tu_index.s.section = sectp;
12188 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12189 }
12190 }
12191
12192 /* This function is mapped across the sections and remembers the offset and
12193 size of each of the DWP version 2 debugging sections that we are interested
12194 in. This is split into a separate function because we don't know if we
12195 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12196
12197 static void
12198 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12199 {
12200 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12201 const struct dwop_section_names *names = &dwop_section_names;
12202 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12203
12204 /* Record the ELF section number for later lookup: this is what the
12205 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12206 gdb_assert (elf_section_nr < dwp_file->num_sections);
12207 dwp_file->elf_sections[elf_section_nr] = sectp;
12208
12209 /* Look for specific sections that we need. */
12210 if (section_is_p (sectp->name, &names->abbrev_dwo))
12211 {
12212 dwp_file->sections.abbrev.s.section = sectp;
12213 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->info_dwo))
12216 {
12217 dwp_file->sections.info.s.section = sectp;
12218 dwp_file->sections.info.size = bfd_section_size (sectp);
12219 }
12220 else if (section_is_p (sectp->name, &names->line_dwo))
12221 {
12222 dwp_file->sections.line.s.section = sectp;
12223 dwp_file->sections.line.size = bfd_section_size (sectp);
12224 }
12225 else if (section_is_p (sectp->name, &names->loc_dwo))
12226 {
12227 dwp_file->sections.loc.s.section = sectp;
12228 dwp_file->sections.loc.size = bfd_section_size (sectp);
12229 }
12230 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12231 {
12232 dwp_file->sections.macinfo.s.section = sectp;
12233 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->macro_dwo))
12236 {
12237 dwp_file->sections.macro.s.section = sectp;
12238 dwp_file->sections.macro.size = bfd_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12241 {
12242 dwp_file->sections.str_offsets.s.section = sectp;
12243 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12244 }
12245 else if (section_is_p (sectp->name, &names->types_dwo))
12246 {
12247 dwp_file->sections.types.s.section = sectp;
12248 dwp_file->sections.types.size = bfd_section_size (sectp);
12249 }
12250 }
12251
12252 /* Hash function for dwp_file loaded CUs/TUs. */
12253
12254 static hashval_t
12255 hash_dwp_loaded_cutus (const void *item)
12256 {
12257 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12258
12259 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12260 return dwo_unit->signature;
12261 }
12262
12263 /* Equality function for dwp_file loaded CUs/TUs. */
12264
12265 static int
12266 eq_dwp_loaded_cutus (const void *a, const void *b)
12267 {
12268 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12269 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12270
12271 return dua->signature == dub->signature;
12272 }
12273
12274 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12275
12276 static htab_up
12277 allocate_dwp_loaded_cutus_table ()
12278 {
12279 return htab_up (htab_create_alloc (3,
12280 hash_dwp_loaded_cutus,
12281 eq_dwp_loaded_cutus,
12282 NULL, xcalloc, xfree));
12283 }
12284
12285 /* Try to open DWP file FILE_NAME.
12286 The result is the bfd handle of the file.
12287 If there is a problem finding or opening the file, return NULL.
12288 Upon success, the canonicalized path of the file is stored in the bfd,
12289 same as symfile_bfd_open. */
12290
12291 static gdb_bfd_ref_ptr
12292 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12293 const char *file_name)
12294 {
12295 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12296 1 /*is_dwp*/,
12297 1 /*search_cwd*/));
12298 if (abfd != NULL)
12299 return abfd;
12300
12301 /* Work around upstream bug 15652.
12302 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12303 [Whether that's a "bug" is debatable, but it is getting in our way.]
12304 We have no real idea where the dwp file is, because gdb's realpath-ing
12305 of the executable's path may have discarded the needed info.
12306 [IWBN if the dwp file name was recorded in the executable, akin to
12307 .gnu_debuglink, but that doesn't exist yet.]
12308 Strip the directory from FILE_NAME and search again. */
12309 if (*debug_file_directory != '\0')
12310 {
12311 /* Don't implicitly search the current directory here.
12312 If the user wants to search "." to handle this case,
12313 it must be added to debug-file-directory. */
12314 return try_open_dwop_file (dwarf2_per_objfile,
12315 lbasename (file_name), 1 /*is_dwp*/,
12316 0 /*search_cwd*/);
12317 }
12318
12319 return NULL;
12320 }
12321
12322 /* Initialize the use of the DWP file for the current objfile.
12323 By convention the name of the DWP file is ${objfile}.dwp.
12324 The result is NULL if it can't be found. */
12325
12326 static std::unique_ptr<struct dwp_file>
12327 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12328 {
12329 struct objfile *objfile = dwarf2_per_objfile->objfile;
12330
12331 /* Try to find first .dwp for the binary file before any symbolic links
12332 resolving. */
12333
12334 /* If the objfile is a debug file, find the name of the real binary
12335 file and get the name of dwp file from there. */
12336 std::string dwp_name;
12337 if (objfile->separate_debug_objfile_backlink != NULL)
12338 {
12339 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12340 const char *backlink_basename = lbasename (backlink->original_name);
12341
12342 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12343 }
12344 else
12345 dwp_name = objfile->original_name;
12346
12347 dwp_name += ".dwp";
12348
12349 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12350 if (dbfd == NULL
12351 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12352 {
12353 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12354 dwp_name = objfile_name (objfile);
12355 dwp_name += ".dwp";
12356 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12357 }
12358
12359 if (dbfd == NULL)
12360 {
12361 if (dwarf_read_debug)
12362 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12363 return std::unique_ptr<dwp_file> ();
12364 }
12365
12366 const char *name = bfd_get_filename (dbfd.get ());
12367 std::unique_ptr<struct dwp_file> dwp_file
12368 (new struct dwp_file (name, std::move (dbfd)));
12369
12370 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12371 dwp_file->elf_sections =
12372 OBSTACK_CALLOC (&objfile->objfile_obstack,
12373 dwp_file->num_sections, asection *);
12374
12375 bfd_map_over_sections (dwp_file->dbfd.get (),
12376 dwarf2_locate_common_dwp_sections,
12377 dwp_file.get ());
12378
12379 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12380 0);
12381
12382 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12383 1);
12384
12385 /* The DWP file version is stored in the hash table. Oh well. */
12386 if (dwp_file->cus && dwp_file->tus
12387 && dwp_file->cus->version != dwp_file->tus->version)
12388 {
12389 /* Technically speaking, we should try to limp along, but this is
12390 pretty bizarre. We use pulongest here because that's the established
12391 portability solution (e.g, we cannot use %u for uint32_t). */
12392 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12393 " TU version %s [in DWP file %s]"),
12394 pulongest (dwp_file->cus->version),
12395 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12396 }
12397
12398 if (dwp_file->cus)
12399 dwp_file->version = dwp_file->cus->version;
12400 else if (dwp_file->tus)
12401 dwp_file->version = dwp_file->tus->version;
12402 else
12403 dwp_file->version = 2;
12404
12405 if (dwp_file->version == 2)
12406 bfd_map_over_sections (dwp_file->dbfd.get (),
12407 dwarf2_locate_v2_dwp_sections,
12408 dwp_file.get ());
12409
12410 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12411 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12412
12413 if (dwarf_read_debug)
12414 {
12415 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12416 fprintf_unfiltered (gdb_stdlog,
12417 " %s CUs, %s TUs\n",
12418 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12419 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12420 }
12421
12422 return dwp_file;
12423 }
12424
12425 /* Wrapper around open_and_init_dwp_file, only open it once. */
12426
12427 static struct dwp_file *
12428 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12429 {
12430 if (! dwarf2_per_objfile->dwp_checked)
12431 {
12432 dwarf2_per_objfile->dwp_file
12433 = open_and_init_dwp_file (dwarf2_per_objfile);
12434 dwarf2_per_objfile->dwp_checked = 1;
12435 }
12436 return dwarf2_per_objfile->dwp_file.get ();
12437 }
12438
12439 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12440 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12441 or in the DWP file for the objfile, referenced by THIS_UNIT.
12442 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12443 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12444
12445 This is called, for example, when wanting to read a variable with a
12446 complex location. Therefore we don't want to do file i/o for every call.
12447 Therefore we don't want to look for a DWO file on every call.
12448 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12449 then we check if we've already seen DWO_NAME, and only THEN do we check
12450 for a DWO file.
12451
12452 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12453 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12454
12455 static struct dwo_unit *
12456 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12457 const char *dwo_name, const char *comp_dir,
12458 ULONGEST signature, int is_debug_types)
12459 {
12460 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12461 struct objfile *objfile = dwarf2_per_objfile->objfile;
12462 const char *kind = is_debug_types ? "TU" : "CU";
12463 void **dwo_file_slot;
12464 struct dwo_file *dwo_file;
12465 struct dwp_file *dwp_file;
12466
12467 /* First see if there's a DWP file.
12468 If we have a DWP file but didn't find the DWO inside it, don't
12469 look for the original DWO file. It makes gdb behave differently
12470 depending on whether one is debugging in the build tree. */
12471
12472 dwp_file = get_dwp_file (dwarf2_per_objfile);
12473 if (dwp_file != NULL)
12474 {
12475 const struct dwp_hash_table *dwp_htab =
12476 is_debug_types ? dwp_file->tus : dwp_file->cus;
12477
12478 if (dwp_htab != NULL)
12479 {
12480 struct dwo_unit *dwo_cutu =
12481 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12482 signature, is_debug_types);
12483
12484 if (dwo_cutu != NULL)
12485 {
12486 if (dwarf_read_debug)
12487 {
12488 fprintf_unfiltered (gdb_stdlog,
12489 "Virtual DWO %s %s found: @%s\n",
12490 kind, hex_string (signature),
12491 host_address_to_string (dwo_cutu));
12492 }
12493 return dwo_cutu;
12494 }
12495 }
12496 }
12497 else
12498 {
12499 /* No DWP file, look for the DWO file. */
12500
12501 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12502 dwo_name, comp_dir);
12503 if (*dwo_file_slot == NULL)
12504 {
12505 /* Read in the file and build a table of the CUs/TUs it contains. */
12506 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12507 }
12508 /* NOTE: This will be NULL if unable to open the file. */
12509 dwo_file = (struct dwo_file *) *dwo_file_slot;
12510
12511 if (dwo_file != NULL)
12512 {
12513 struct dwo_unit *dwo_cutu = NULL;
12514
12515 if (is_debug_types && dwo_file->tus)
12516 {
12517 struct dwo_unit find_dwo_cutu;
12518
12519 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12520 find_dwo_cutu.signature = signature;
12521 dwo_cutu
12522 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12523 &find_dwo_cutu);
12524 }
12525 else if (!is_debug_types && dwo_file->cus)
12526 {
12527 struct dwo_unit find_dwo_cutu;
12528
12529 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12530 find_dwo_cutu.signature = signature;
12531 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12532 &find_dwo_cutu);
12533 }
12534
12535 if (dwo_cutu != NULL)
12536 {
12537 if (dwarf_read_debug)
12538 {
12539 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12540 kind, dwo_name, hex_string (signature),
12541 host_address_to_string (dwo_cutu));
12542 }
12543 return dwo_cutu;
12544 }
12545 }
12546 }
12547
12548 /* We didn't find it. This could mean a dwo_id mismatch, or
12549 someone deleted the DWO/DWP file, or the search path isn't set up
12550 correctly to find the file. */
12551
12552 if (dwarf_read_debug)
12553 {
12554 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12555 kind, dwo_name, hex_string (signature));
12556 }
12557
12558 /* This is a warning and not a complaint because it can be caused by
12559 pilot error (e.g., user accidentally deleting the DWO). */
12560 {
12561 /* Print the name of the DWP file if we looked there, helps the user
12562 better diagnose the problem. */
12563 std::string dwp_text;
12564
12565 if (dwp_file != NULL)
12566 dwp_text = string_printf (" [in DWP file %s]",
12567 lbasename (dwp_file->name));
12568
12569 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12570 " [in module %s]"),
12571 kind, dwo_name, hex_string (signature),
12572 dwp_text.c_str (),
12573 this_unit->is_debug_types ? "TU" : "CU",
12574 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12575 }
12576 return NULL;
12577 }
12578
12579 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12580 See lookup_dwo_cutu_unit for details. */
12581
12582 static struct dwo_unit *
12583 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12584 const char *dwo_name, const char *comp_dir,
12585 ULONGEST signature)
12586 {
12587 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12588 }
12589
12590 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12591 See lookup_dwo_cutu_unit for details. */
12592
12593 static struct dwo_unit *
12594 lookup_dwo_type_unit (struct signatured_type *this_tu,
12595 const char *dwo_name, const char *comp_dir)
12596 {
12597 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12598 }
12599
12600 /* Traversal function for queue_and_load_all_dwo_tus. */
12601
12602 static int
12603 queue_and_load_dwo_tu (void **slot, void *info)
12604 {
12605 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12606 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12607 ULONGEST signature = dwo_unit->signature;
12608 struct signatured_type *sig_type =
12609 lookup_dwo_signatured_type (per_cu->cu, signature);
12610
12611 if (sig_type != NULL)
12612 {
12613 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12614
12615 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12616 a real dependency of PER_CU on SIG_TYPE. That is detected later
12617 while processing PER_CU. */
12618 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12619 load_full_type_unit (sig_cu);
12620 per_cu->imported_symtabs_push (sig_cu);
12621 }
12622
12623 return 1;
12624 }
12625
12626 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12627 The DWO may have the only definition of the type, though it may not be
12628 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12629 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12630
12631 static void
12632 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12633 {
12634 struct dwo_unit *dwo_unit;
12635 struct dwo_file *dwo_file;
12636
12637 gdb_assert (!per_cu->is_debug_types);
12638 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12639 gdb_assert (per_cu->cu != NULL);
12640
12641 dwo_unit = per_cu->cu->dwo_unit;
12642 gdb_assert (dwo_unit != NULL);
12643
12644 dwo_file = dwo_unit->dwo_file;
12645 if (dwo_file->tus != NULL)
12646 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12647 per_cu);
12648 }
12649
12650 /* Read in various DIEs. */
12651
12652 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12653 Inherit only the children of the DW_AT_abstract_origin DIE not being
12654 already referenced by DW_AT_abstract_origin from the children of the
12655 current DIE. */
12656
12657 static void
12658 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12659 {
12660 struct die_info *child_die;
12661 sect_offset *offsetp;
12662 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12663 struct die_info *origin_die;
12664 /* Iterator of the ORIGIN_DIE children. */
12665 struct die_info *origin_child_die;
12666 struct attribute *attr;
12667 struct dwarf2_cu *origin_cu;
12668 struct pending **origin_previous_list_in_scope;
12669
12670 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12671 if (!attr)
12672 return;
12673
12674 /* Note that following die references may follow to a die in a
12675 different cu. */
12676
12677 origin_cu = cu;
12678 origin_die = follow_die_ref (die, attr, &origin_cu);
12679
12680 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12681 symbols in. */
12682 origin_previous_list_in_scope = origin_cu->list_in_scope;
12683 origin_cu->list_in_scope = cu->list_in_scope;
12684
12685 if (die->tag != origin_die->tag
12686 && !(die->tag == DW_TAG_inlined_subroutine
12687 && origin_die->tag == DW_TAG_subprogram))
12688 complaint (_("DIE %s and its abstract origin %s have different tags"),
12689 sect_offset_str (die->sect_off),
12690 sect_offset_str (origin_die->sect_off));
12691
12692 std::vector<sect_offset> offsets;
12693
12694 for (child_die = die->child;
12695 child_die && child_die->tag;
12696 child_die = sibling_die (child_die))
12697 {
12698 struct die_info *child_origin_die;
12699 struct dwarf2_cu *child_origin_cu;
12700
12701 /* We are trying to process concrete instance entries:
12702 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12703 it's not relevant to our analysis here. i.e. detecting DIEs that are
12704 present in the abstract instance but not referenced in the concrete
12705 one. */
12706 if (child_die->tag == DW_TAG_call_site
12707 || child_die->tag == DW_TAG_GNU_call_site)
12708 continue;
12709
12710 /* For each CHILD_DIE, find the corresponding child of
12711 ORIGIN_DIE. If there is more than one layer of
12712 DW_AT_abstract_origin, follow them all; there shouldn't be,
12713 but GCC versions at least through 4.4 generate this (GCC PR
12714 40573). */
12715 child_origin_die = child_die;
12716 child_origin_cu = cu;
12717 while (1)
12718 {
12719 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12720 child_origin_cu);
12721 if (attr == NULL)
12722 break;
12723 child_origin_die = follow_die_ref (child_origin_die, attr,
12724 &child_origin_cu);
12725 }
12726
12727 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12728 counterpart may exist. */
12729 if (child_origin_die != child_die)
12730 {
12731 if (child_die->tag != child_origin_die->tag
12732 && !(child_die->tag == DW_TAG_inlined_subroutine
12733 && child_origin_die->tag == DW_TAG_subprogram))
12734 complaint (_("Child DIE %s and its abstract origin %s have "
12735 "different tags"),
12736 sect_offset_str (child_die->sect_off),
12737 sect_offset_str (child_origin_die->sect_off));
12738 if (child_origin_die->parent != origin_die)
12739 complaint (_("Child DIE %s and its abstract origin %s have "
12740 "different parents"),
12741 sect_offset_str (child_die->sect_off),
12742 sect_offset_str (child_origin_die->sect_off));
12743 else
12744 offsets.push_back (child_origin_die->sect_off);
12745 }
12746 }
12747 std::sort (offsets.begin (), offsets.end ());
12748 sect_offset *offsets_end = offsets.data () + offsets.size ();
12749 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12750 if (offsetp[-1] == *offsetp)
12751 complaint (_("Multiple children of DIE %s refer "
12752 "to DIE %s as their abstract origin"),
12753 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12754
12755 offsetp = offsets.data ();
12756 origin_child_die = origin_die->child;
12757 while (origin_child_die && origin_child_die->tag)
12758 {
12759 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12760 while (offsetp < offsets_end
12761 && *offsetp < origin_child_die->sect_off)
12762 offsetp++;
12763 if (offsetp >= offsets_end
12764 || *offsetp > origin_child_die->sect_off)
12765 {
12766 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12767 Check whether we're already processing ORIGIN_CHILD_DIE.
12768 This can happen with mutually referenced abstract_origins.
12769 PR 16581. */
12770 if (!origin_child_die->in_process)
12771 process_die (origin_child_die, origin_cu);
12772 }
12773 origin_child_die = sibling_die (origin_child_die);
12774 }
12775 origin_cu->list_in_scope = origin_previous_list_in_scope;
12776
12777 if (cu != origin_cu)
12778 compute_delayed_physnames (origin_cu);
12779 }
12780
12781 static void
12782 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12783 {
12784 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12786 struct context_stack *newobj;
12787 CORE_ADDR lowpc;
12788 CORE_ADDR highpc;
12789 struct die_info *child_die;
12790 struct attribute *attr, *call_line, *call_file;
12791 const char *name;
12792 CORE_ADDR baseaddr;
12793 struct block *block;
12794 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12795 std::vector<struct symbol *> template_args;
12796 struct template_symbol *templ_func = NULL;
12797
12798 if (inlined_func)
12799 {
12800 /* If we do not have call site information, we can't show the
12801 caller of this inlined function. That's too confusing, so
12802 only use the scope for local variables. */
12803 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12804 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12805 if (call_line == NULL || call_file == NULL)
12806 {
12807 read_lexical_block_scope (die, cu);
12808 return;
12809 }
12810 }
12811
12812 baseaddr = objfile->text_section_offset ();
12813
12814 name = dwarf2_name (die, cu);
12815
12816 /* Ignore functions with missing or empty names. These are actually
12817 illegal according to the DWARF standard. */
12818 if (name == NULL)
12819 {
12820 complaint (_("missing name for subprogram DIE at %s"),
12821 sect_offset_str (die->sect_off));
12822 return;
12823 }
12824
12825 /* Ignore functions with missing or invalid low and high pc attributes. */
12826 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12827 <= PC_BOUNDS_INVALID)
12828 {
12829 attr = dwarf2_attr (die, DW_AT_external, cu);
12830 if (!attr || !DW_UNSND (attr))
12831 complaint (_("cannot get low and high bounds "
12832 "for subprogram DIE at %s"),
12833 sect_offset_str (die->sect_off));
12834 return;
12835 }
12836
12837 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12838 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12839
12840 /* If we have any template arguments, then we must allocate a
12841 different sort of symbol. */
12842 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12843 {
12844 if (child_die->tag == DW_TAG_template_type_param
12845 || child_die->tag == DW_TAG_template_value_param)
12846 {
12847 templ_func = allocate_template_symbol (objfile);
12848 templ_func->subclass = SYMBOL_TEMPLATE;
12849 break;
12850 }
12851 }
12852
12853 newobj = cu->get_builder ()->push_context (0, lowpc);
12854 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12855 (struct symbol *) templ_func);
12856
12857 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12858 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12859 cu->language);
12860
12861 /* If there is a location expression for DW_AT_frame_base, record
12862 it. */
12863 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12864 if (attr != nullptr)
12865 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12866
12867 /* If there is a location for the static link, record it. */
12868 newobj->static_link = NULL;
12869 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12870 if (attr != nullptr)
12871 {
12872 newobj->static_link
12873 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12874 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12875 cu->per_cu->addr_type ());
12876 }
12877
12878 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12879
12880 if (die->child != NULL)
12881 {
12882 child_die = die->child;
12883 while (child_die && child_die->tag)
12884 {
12885 if (child_die->tag == DW_TAG_template_type_param
12886 || child_die->tag == DW_TAG_template_value_param)
12887 {
12888 struct symbol *arg = new_symbol (child_die, NULL, cu);
12889
12890 if (arg != NULL)
12891 template_args.push_back (arg);
12892 }
12893 else
12894 process_die (child_die, cu);
12895 child_die = sibling_die (child_die);
12896 }
12897 }
12898
12899 inherit_abstract_dies (die, cu);
12900
12901 /* If we have a DW_AT_specification, we might need to import using
12902 directives from the context of the specification DIE. See the
12903 comment in determine_prefix. */
12904 if (cu->language == language_cplus
12905 && dwarf2_attr (die, DW_AT_specification, cu))
12906 {
12907 struct dwarf2_cu *spec_cu = cu;
12908 struct die_info *spec_die = die_specification (die, &spec_cu);
12909
12910 while (spec_die)
12911 {
12912 child_die = spec_die->child;
12913 while (child_die && child_die->tag)
12914 {
12915 if (child_die->tag == DW_TAG_imported_module)
12916 process_die (child_die, spec_cu);
12917 child_die = sibling_die (child_die);
12918 }
12919
12920 /* In some cases, GCC generates specification DIEs that
12921 themselves contain DW_AT_specification attributes. */
12922 spec_die = die_specification (spec_die, &spec_cu);
12923 }
12924 }
12925
12926 struct context_stack cstk = cu->get_builder ()->pop_context ();
12927 /* Make a block for the local symbols within. */
12928 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12929 cstk.static_link, lowpc, highpc);
12930
12931 /* For C++, set the block's scope. */
12932 if ((cu->language == language_cplus
12933 || cu->language == language_fortran
12934 || cu->language == language_d
12935 || cu->language == language_rust)
12936 && cu->processing_has_namespace_info)
12937 block_set_scope (block, determine_prefix (die, cu),
12938 &objfile->objfile_obstack);
12939
12940 /* If we have address ranges, record them. */
12941 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12942
12943 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12944
12945 /* Attach template arguments to function. */
12946 if (!template_args.empty ())
12947 {
12948 gdb_assert (templ_func != NULL);
12949
12950 templ_func->n_template_arguments = template_args.size ();
12951 templ_func->template_arguments
12952 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12953 templ_func->n_template_arguments);
12954 memcpy (templ_func->template_arguments,
12955 template_args.data (),
12956 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12957
12958 /* Make sure that the symtab is set on the new symbols. Even
12959 though they don't appear in this symtab directly, other parts
12960 of gdb assume that symbols do, and this is reasonably
12961 true. */
12962 for (symbol *sym : template_args)
12963 symbol_set_symtab (sym, symbol_symtab (templ_func));
12964 }
12965
12966 /* In C++, we can have functions nested inside functions (e.g., when
12967 a function declares a class that has methods). This means that
12968 when we finish processing a function scope, we may need to go
12969 back to building a containing block's symbol lists. */
12970 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12971 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12972
12973 /* If we've finished processing a top-level function, subsequent
12974 symbols go in the file symbol list. */
12975 if (cu->get_builder ()->outermost_context_p ())
12976 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12977 }
12978
12979 /* Process all the DIES contained within a lexical block scope. Start
12980 a new scope, process the dies, and then close the scope. */
12981
12982 static void
12983 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12984 {
12985 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12986 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12987 CORE_ADDR lowpc, highpc;
12988 struct die_info *child_die;
12989 CORE_ADDR baseaddr;
12990
12991 baseaddr = objfile->text_section_offset ();
12992
12993 /* Ignore blocks with missing or invalid low and high pc attributes. */
12994 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12995 as multiple lexical blocks? Handling children in a sane way would
12996 be nasty. Might be easier to properly extend generic blocks to
12997 describe ranges. */
12998 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12999 {
13000 case PC_BOUNDS_NOT_PRESENT:
13001 /* DW_TAG_lexical_block has no attributes, process its children as if
13002 there was no wrapping by that DW_TAG_lexical_block.
13003 GCC does no longer produces such DWARF since GCC r224161. */
13004 for (child_die = die->child;
13005 child_die != NULL && child_die->tag;
13006 child_die = sibling_die (child_die))
13007 process_die (child_die, cu);
13008 return;
13009 case PC_BOUNDS_INVALID:
13010 return;
13011 }
13012 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13013 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13014
13015 cu->get_builder ()->push_context (0, lowpc);
13016 if (die->child != NULL)
13017 {
13018 child_die = die->child;
13019 while (child_die && child_die->tag)
13020 {
13021 process_die (child_die, cu);
13022 child_die = sibling_die (child_die);
13023 }
13024 }
13025 inherit_abstract_dies (die, cu);
13026 struct context_stack cstk = cu->get_builder ()->pop_context ();
13027
13028 if (*cu->get_builder ()->get_local_symbols () != NULL
13029 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13030 {
13031 struct block *block
13032 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13033 cstk.start_addr, highpc);
13034
13035 /* Note that recording ranges after traversing children, as we
13036 do here, means that recording a parent's ranges entails
13037 walking across all its children's ranges as they appear in
13038 the address map, which is quadratic behavior.
13039
13040 It would be nicer to record the parent's ranges before
13041 traversing its children, simply overriding whatever you find
13042 there. But since we don't even decide whether to create a
13043 block until after we've traversed its children, that's hard
13044 to do. */
13045 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13046 }
13047 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13048 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13049 }
13050
13051 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13052
13053 static void
13054 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13055 {
13056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13058 CORE_ADDR pc, baseaddr;
13059 struct attribute *attr;
13060 struct call_site *call_site, call_site_local;
13061 void **slot;
13062 int nparams;
13063 struct die_info *child_die;
13064
13065 baseaddr = objfile->text_section_offset ();
13066
13067 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13068 if (attr == NULL)
13069 {
13070 /* This was a pre-DWARF-5 GNU extension alias
13071 for DW_AT_call_return_pc. */
13072 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13073 }
13074 if (!attr)
13075 {
13076 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13077 "DIE %s [in module %s]"),
13078 sect_offset_str (die->sect_off), objfile_name (objfile));
13079 return;
13080 }
13081 pc = attr->value_as_address () + baseaddr;
13082 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13083
13084 if (cu->call_site_htab == NULL)
13085 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13086 NULL, &objfile->objfile_obstack,
13087 hashtab_obstack_allocate, NULL);
13088 call_site_local.pc = pc;
13089 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13090 if (*slot != NULL)
13091 {
13092 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13093 "DIE %s [in module %s]"),
13094 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13095 objfile_name (objfile));
13096 return;
13097 }
13098
13099 /* Count parameters at the caller. */
13100
13101 nparams = 0;
13102 for (child_die = die->child; child_die && child_die->tag;
13103 child_die = sibling_die (child_die))
13104 {
13105 if (child_die->tag != DW_TAG_call_site_parameter
13106 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13107 {
13108 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13109 "DW_TAG_call_site child DIE %s [in module %s]"),
13110 child_die->tag, sect_offset_str (child_die->sect_off),
13111 objfile_name (objfile));
13112 continue;
13113 }
13114
13115 nparams++;
13116 }
13117
13118 call_site
13119 = ((struct call_site *)
13120 obstack_alloc (&objfile->objfile_obstack,
13121 sizeof (*call_site)
13122 + (sizeof (*call_site->parameter) * (nparams - 1))));
13123 *slot = call_site;
13124 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13125 call_site->pc = pc;
13126
13127 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13128 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13129 {
13130 struct die_info *func_die;
13131
13132 /* Skip also over DW_TAG_inlined_subroutine. */
13133 for (func_die = die->parent;
13134 func_die && func_die->tag != DW_TAG_subprogram
13135 && func_die->tag != DW_TAG_subroutine_type;
13136 func_die = func_die->parent);
13137
13138 /* DW_AT_call_all_calls is a superset
13139 of DW_AT_call_all_tail_calls. */
13140 if (func_die
13141 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13142 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13143 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13144 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13145 {
13146 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13147 not complete. But keep CALL_SITE for look ups via call_site_htab,
13148 both the initial caller containing the real return address PC and
13149 the final callee containing the current PC of a chain of tail
13150 calls do not need to have the tail call list complete. But any
13151 function candidate for a virtual tail call frame searched via
13152 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13153 determined unambiguously. */
13154 }
13155 else
13156 {
13157 struct type *func_type = NULL;
13158
13159 if (func_die)
13160 func_type = get_die_type (func_die, cu);
13161 if (func_type != NULL)
13162 {
13163 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13164
13165 /* Enlist this call site to the function. */
13166 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13167 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13168 }
13169 else
13170 complaint (_("Cannot find function owning DW_TAG_call_site "
13171 "DIE %s [in module %s]"),
13172 sect_offset_str (die->sect_off), objfile_name (objfile));
13173 }
13174 }
13175
13176 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13177 if (attr == NULL)
13178 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13179 if (attr == NULL)
13180 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13181 if (attr == NULL)
13182 {
13183 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13184 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13185 }
13186 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13187 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13188 /* Keep NULL DWARF_BLOCK. */;
13189 else if (attr->form_is_block ())
13190 {
13191 struct dwarf2_locexpr_baton *dlbaton;
13192
13193 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13194 dlbaton->data = DW_BLOCK (attr)->data;
13195 dlbaton->size = DW_BLOCK (attr)->size;
13196 dlbaton->per_cu = cu->per_cu;
13197
13198 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13199 }
13200 else if (attr->form_is_ref ())
13201 {
13202 struct dwarf2_cu *target_cu = cu;
13203 struct die_info *target_die;
13204
13205 target_die = follow_die_ref (die, attr, &target_cu);
13206 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13207 if (die_is_declaration (target_die, target_cu))
13208 {
13209 const char *target_physname;
13210
13211 /* Prefer the mangled name; otherwise compute the demangled one. */
13212 target_physname = dw2_linkage_name (target_die, target_cu);
13213 if (target_physname == NULL)
13214 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13215 if (target_physname == NULL)
13216 complaint (_("DW_AT_call_target target DIE has invalid "
13217 "physname, for referencing DIE %s [in module %s]"),
13218 sect_offset_str (die->sect_off), objfile_name (objfile));
13219 else
13220 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13221 }
13222 else
13223 {
13224 CORE_ADDR lowpc;
13225
13226 /* DW_AT_entry_pc should be preferred. */
13227 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13228 <= PC_BOUNDS_INVALID)
13229 complaint (_("DW_AT_call_target target DIE has invalid "
13230 "low pc, for referencing DIE %s [in module %s]"),
13231 sect_offset_str (die->sect_off), objfile_name (objfile));
13232 else
13233 {
13234 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13235 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13236 }
13237 }
13238 }
13239 else
13240 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13241 "block nor reference, for DIE %s [in module %s]"),
13242 sect_offset_str (die->sect_off), objfile_name (objfile));
13243
13244 call_site->per_cu = cu->per_cu;
13245
13246 for (child_die = die->child;
13247 child_die && child_die->tag;
13248 child_die = sibling_die (child_die))
13249 {
13250 struct call_site_parameter *parameter;
13251 struct attribute *loc, *origin;
13252
13253 if (child_die->tag != DW_TAG_call_site_parameter
13254 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13255 {
13256 /* Already printed the complaint above. */
13257 continue;
13258 }
13259
13260 gdb_assert (call_site->parameter_count < nparams);
13261 parameter = &call_site->parameter[call_site->parameter_count];
13262
13263 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13264 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13265 register is contained in DW_AT_call_value. */
13266
13267 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13268 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13269 if (origin == NULL)
13270 {
13271 /* This was a pre-DWARF-5 GNU extension alias
13272 for DW_AT_call_parameter. */
13273 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13274 }
13275 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13276 {
13277 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13278
13279 sect_offset sect_off
13280 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13281 if (!cu->header.offset_in_cu_p (sect_off))
13282 {
13283 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13284 binding can be done only inside one CU. Such referenced DIE
13285 therefore cannot be even moved to DW_TAG_partial_unit. */
13286 complaint (_("DW_AT_call_parameter offset is not in CU for "
13287 "DW_TAG_call_site child DIE %s [in module %s]"),
13288 sect_offset_str (child_die->sect_off),
13289 objfile_name (objfile));
13290 continue;
13291 }
13292 parameter->u.param_cu_off
13293 = (cu_offset) (sect_off - cu->header.sect_off);
13294 }
13295 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13296 {
13297 complaint (_("No DW_FORM_block* DW_AT_location for "
13298 "DW_TAG_call_site child DIE %s [in module %s]"),
13299 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13300 continue;
13301 }
13302 else
13303 {
13304 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13305 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13306 if (parameter->u.dwarf_reg != -1)
13307 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13308 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13309 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13310 &parameter->u.fb_offset))
13311 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13312 else
13313 {
13314 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13315 "for DW_FORM_block* DW_AT_location is supported for "
13316 "DW_TAG_call_site child DIE %s "
13317 "[in module %s]"),
13318 sect_offset_str (child_die->sect_off),
13319 objfile_name (objfile));
13320 continue;
13321 }
13322 }
13323
13324 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13325 if (attr == NULL)
13326 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13327 if (attr == NULL || !attr->form_is_block ())
13328 {
13329 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13330 "DW_TAG_call_site child DIE %s [in module %s]"),
13331 sect_offset_str (child_die->sect_off),
13332 objfile_name (objfile));
13333 continue;
13334 }
13335 parameter->value = DW_BLOCK (attr)->data;
13336 parameter->value_size = DW_BLOCK (attr)->size;
13337
13338 /* Parameters are not pre-cleared by memset above. */
13339 parameter->data_value = NULL;
13340 parameter->data_value_size = 0;
13341 call_site->parameter_count++;
13342
13343 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13344 if (attr == NULL)
13345 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13346 if (attr != nullptr)
13347 {
13348 if (!attr->form_is_block ())
13349 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13350 "DW_TAG_call_site child DIE %s [in module %s]"),
13351 sect_offset_str (child_die->sect_off),
13352 objfile_name (objfile));
13353 else
13354 {
13355 parameter->data_value = DW_BLOCK (attr)->data;
13356 parameter->data_value_size = DW_BLOCK (attr)->size;
13357 }
13358 }
13359 }
13360 }
13361
13362 /* Helper function for read_variable. If DIE represents a virtual
13363 table, then return the type of the concrete object that is
13364 associated with the virtual table. Otherwise, return NULL. */
13365
13366 static struct type *
13367 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13368 {
13369 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13370 if (attr == NULL)
13371 return NULL;
13372
13373 /* Find the type DIE. */
13374 struct die_info *type_die = NULL;
13375 struct dwarf2_cu *type_cu = cu;
13376
13377 if (attr->form_is_ref ())
13378 type_die = follow_die_ref (die, attr, &type_cu);
13379 if (type_die == NULL)
13380 return NULL;
13381
13382 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13383 return NULL;
13384 return die_containing_type (type_die, type_cu);
13385 }
13386
13387 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13388
13389 static void
13390 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13391 {
13392 struct rust_vtable_symbol *storage = NULL;
13393
13394 if (cu->language == language_rust)
13395 {
13396 struct type *containing_type = rust_containing_type (die, cu);
13397
13398 if (containing_type != NULL)
13399 {
13400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13401
13402 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13403 initialize_objfile_symbol (storage);
13404 storage->concrete_type = containing_type;
13405 storage->subclass = SYMBOL_RUST_VTABLE;
13406 }
13407 }
13408
13409 struct symbol *res = new_symbol (die, NULL, cu, storage);
13410 struct attribute *abstract_origin
13411 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13412 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13413 if (res == NULL && loc && abstract_origin)
13414 {
13415 /* We have a variable without a name, but with a location and an abstract
13416 origin. This may be a concrete instance of an abstract variable
13417 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13418 later. */
13419 struct dwarf2_cu *origin_cu = cu;
13420 struct die_info *origin_die
13421 = follow_die_ref (die, abstract_origin, &origin_cu);
13422 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13423 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13424 }
13425 }
13426
13427 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13428 reading .debug_rnglists.
13429 Callback's type should be:
13430 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13431 Return true if the attributes are present and valid, otherwise,
13432 return false. */
13433
13434 template <typename Callback>
13435 static bool
13436 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13437 Callback &&callback)
13438 {
13439 struct dwarf2_per_objfile *dwarf2_per_objfile
13440 = cu->per_cu->dwarf2_per_objfile;
13441 struct objfile *objfile = dwarf2_per_objfile->objfile;
13442 bfd *obfd = objfile->obfd;
13443 /* Base address selection entry. */
13444 CORE_ADDR base;
13445 int found_base;
13446 const gdb_byte *buffer;
13447 CORE_ADDR baseaddr;
13448 bool overflow = false;
13449
13450 found_base = cu->base_known;
13451 base = cu->base_address;
13452
13453 dwarf2_per_objfile->rnglists.read (objfile);
13454 if (offset >= dwarf2_per_objfile->rnglists.size)
13455 {
13456 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13457 offset);
13458 return false;
13459 }
13460 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13461
13462 baseaddr = objfile->text_section_offset ();
13463
13464 while (1)
13465 {
13466 /* Initialize it due to a false compiler warning. */
13467 CORE_ADDR range_beginning = 0, range_end = 0;
13468 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13469 + dwarf2_per_objfile->rnglists.size);
13470 unsigned int bytes_read;
13471
13472 if (buffer == buf_end)
13473 {
13474 overflow = true;
13475 break;
13476 }
13477 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13478 switch (rlet)
13479 {
13480 case DW_RLE_end_of_list:
13481 break;
13482 case DW_RLE_base_address:
13483 if (buffer + cu->header.addr_size > buf_end)
13484 {
13485 overflow = true;
13486 break;
13487 }
13488 base = cu->header.read_address (obfd, buffer, &bytes_read);
13489 found_base = 1;
13490 buffer += bytes_read;
13491 break;
13492 case DW_RLE_start_length:
13493 if (buffer + cu->header.addr_size > buf_end)
13494 {
13495 overflow = true;
13496 break;
13497 }
13498 range_beginning = cu->header.read_address (obfd, buffer,
13499 &bytes_read);
13500 buffer += bytes_read;
13501 range_end = (range_beginning
13502 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13503 buffer += bytes_read;
13504 if (buffer > buf_end)
13505 {
13506 overflow = true;
13507 break;
13508 }
13509 break;
13510 case DW_RLE_offset_pair:
13511 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13512 buffer += bytes_read;
13513 if (buffer > buf_end)
13514 {
13515 overflow = true;
13516 break;
13517 }
13518 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13519 buffer += bytes_read;
13520 if (buffer > buf_end)
13521 {
13522 overflow = true;
13523 break;
13524 }
13525 break;
13526 case DW_RLE_start_end:
13527 if (buffer + 2 * cu->header.addr_size > buf_end)
13528 {
13529 overflow = true;
13530 break;
13531 }
13532 range_beginning = cu->header.read_address (obfd, buffer,
13533 &bytes_read);
13534 buffer += bytes_read;
13535 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13536 buffer += bytes_read;
13537 break;
13538 default:
13539 complaint (_("Invalid .debug_rnglists data (no base address)"));
13540 return false;
13541 }
13542 if (rlet == DW_RLE_end_of_list || overflow)
13543 break;
13544 if (rlet == DW_RLE_base_address)
13545 continue;
13546
13547 if (!found_base)
13548 {
13549 /* We have no valid base address for the ranges
13550 data. */
13551 complaint (_("Invalid .debug_rnglists data (no base address)"));
13552 return false;
13553 }
13554
13555 if (range_beginning > range_end)
13556 {
13557 /* Inverted range entries are invalid. */
13558 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13559 return false;
13560 }
13561
13562 /* Empty range entries have no effect. */
13563 if (range_beginning == range_end)
13564 continue;
13565
13566 range_beginning += base;
13567 range_end += base;
13568
13569 /* A not-uncommon case of bad debug info.
13570 Don't pollute the addrmap with bad data. */
13571 if (range_beginning + baseaddr == 0
13572 && !dwarf2_per_objfile->has_section_at_zero)
13573 {
13574 complaint (_(".debug_rnglists entry has start address of zero"
13575 " [in module %s]"), objfile_name (objfile));
13576 continue;
13577 }
13578
13579 callback (range_beginning, range_end);
13580 }
13581
13582 if (overflow)
13583 {
13584 complaint (_("Offset %d is not terminated "
13585 "for DW_AT_ranges attribute"),
13586 offset);
13587 return false;
13588 }
13589
13590 return true;
13591 }
13592
13593 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13594 Callback's type should be:
13595 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13596 Return 1 if the attributes are present and valid, otherwise, return 0. */
13597
13598 template <typename Callback>
13599 static int
13600 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13601 Callback &&callback)
13602 {
13603 struct dwarf2_per_objfile *dwarf2_per_objfile
13604 = cu->per_cu->dwarf2_per_objfile;
13605 struct objfile *objfile = dwarf2_per_objfile->objfile;
13606 struct comp_unit_head *cu_header = &cu->header;
13607 bfd *obfd = objfile->obfd;
13608 unsigned int addr_size = cu_header->addr_size;
13609 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13610 /* Base address selection entry. */
13611 CORE_ADDR base;
13612 int found_base;
13613 unsigned int dummy;
13614 const gdb_byte *buffer;
13615 CORE_ADDR baseaddr;
13616
13617 if (cu_header->version >= 5)
13618 return dwarf2_rnglists_process (offset, cu, callback);
13619
13620 found_base = cu->base_known;
13621 base = cu->base_address;
13622
13623 dwarf2_per_objfile->ranges.read (objfile);
13624 if (offset >= dwarf2_per_objfile->ranges.size)
13625 {
13626 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13627 offset);
13628 return 0;
13629 }
13630 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13631
13632 baseaddr = objfile->text_section_offset ();
13633
13634 while (1)
13635 {
13636 CORE_ADDR range_beginning, range_end;
13637
13638 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13639 buffer += addr_size;
13640 range_end = cu->header.read_address (obfd, buffer, &dummy);
13641 buffer += addr_size;
13642 offset += 2 * addr_size;
13643
13644 /* An end of list marker is a pair of zero addresses. */
13645 if (range_beginning == 0 && range_end == 0)
13646 /* Found the end of list entry. */
13647 break;
13648
13649 /* Each base address selection entry is a pair of 2 values.
13650 The first is the largest possible address, the second is
13651 the base address. Check for a base address here. */
13652 if ((range_beginning & mask) == mask)
13653 {
13654 /* If we found the largest possible address, then we already
13655 have the base address in range_end. */
13656 base = range_end;
13657 found_base = 1;
13658 continue;
13659 }
13660
13661 if (!found_base)
13662 {
13663 /* We have no valid base address for the ranges
13664 data. */
13665 complaint (_("Invalid .debug_ranges data (no base address)"));
13666 return 0;
13667 }
13668
13669 if (range_beginning > range_end)
13670 {
13671 /* Inverted range entries are invalid. */
13672 complaint (_("Invalid .debug_ranges data (inverted range)"));
13673 return 0;
13674 }
13675
13676 /* Empty range entries have no effect. */
13677 if (range_beginning == range_end)
13678 continue;
13679
13680 range_beginning += base;
13681 range_end += base;
13682
13683 /* A not-uncommon case of bad debug info.
13684 Don't pollute the addrmap with bad data. */
13685 if (range_beginning + baseaddr == 0
13686 && !dwarf2_per_objfile->has_section_at_zero)
13687 {
13688 complaint (_(".debug_ranges entry has start address of zero"
13689 " [in module %s]"), objfile_name (objfile));
13690 continue;
13691 }
13692
13693 callback (range_beginning, range_end);
13694 }
13695
13696 return 1;
13697 }
13698
13699 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13700 Return 1 if the attributes are present and valid, otherwise, return 0.
13701 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13702
13703 static int
13704 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13705 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13706 dwarf2_psymtab *ranges_pst)
13707 {
13708 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13710 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13711 int low_set = 0;
13712 CORE_ADDR low = 0;
13713 CORE_ADDR high = 0;
13714 int retval;
13715
13716 retval = dwarf2_ranges_process (offset, cu,
13717 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13718 {
13719 if (ranges_pst != NULL)
13720 {
13721 CORE_ADDR lowpc;
13722 CORE_ADDR highpc;
13723
13724 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13725 range_beginning + baseaddr)
13726 - baseaddr);
13727 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13728 range_end + baseaddr)
13729 - baseaddr);
13730 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13731 lowpc, highpc - 1, ranges_pst);
13732 }
13733
13734 /* FIXME: This is recording everything as a low-high
13735 segment of consecutive addresses. We should have a
13736 data structure for discontiguous block ranges
13737 instead. */
13738 if (! low_set)
13739 {
13740 low = range_beginning;
13741 high = range_end;
13742 low_set = 1;
13743 }
13744 else
13745 {
13746 if (range_beginning < low)
13747 low = range_beginning;
13748 if (range_end > high)
13749 high = range_end;
13750 }
13751 });
13752 if (!retval)
13753 return 0;
13754
13755 if (! low_set)
13756 /* If the first entry is an end-of-list marker, the range
13757 describes an empty scope, i.e. no instructions. */
13758 return 0;
13759
13760 if (low_return)
13761 *low_return = low;
13762 if (high_return)
13763 *high_return = high;
13764 return 1;
13765 }
13766
13767 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13768 definition for the return value. *LOWPC and *HIGHPC are set iff
13769 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13770
13771 static enum pc_bounds_kind
13772 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13773 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13774 dwarf2_psymtab *pst)
13775 {
13776 struct dwarf2_per_objfile *dwarf2_per_objfile
13777 = cu->per_cu->dwarf2_per_objfile;
13778 struct attribute *attr;
13779 struct attribute *attr_high;
13780 CORE_ADDR low = 0;
13781 CORE_ADDR high = 0;
13782 enum pc_bounds_kind ret;
13783
13784 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13785 if (attr_high)
13786 {
13787 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13788 if (attr != nullptr)
13789 {
13790 low = attr->value_as_address ();
13791 high = attr_high->value_as_address ();
13792 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13793 high += low;
13794 }
13795 else
13796 /* Found high w/o low attribute. */
13797 return PC_BOUNDS_INVALID;
13798
13799 /* Found consecutive range of addresses. */
13800 ret = PC_BOUNDS_HIGH_LOW;
13801 }
13802 else
13803 {
13804 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13805 if (attr != NULL)
13806 {
13807 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13808 We take advantage of the fact that DW_AT_ranges does not appear
13809 in DW_TAG_compile_unit of DWO files. */
13810 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13811 unsigned int ranges_offset = (DW_UNSND (attr)
13812 + (need_ranges_base
13813 ? cu->ranges_base
13814 : 0));
13815
13816 /* Value of the DW_AT_ranges attribute is the offset in the
13817 .debug_ranges section. */
13818 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13819 return PC_BOUNDS_INVALID;
13820 /* Found discontinuous range of addresses. */
13821 ret = PC_BOUNDS_RANGES;
13822 }
13823 else
13824 return PC_BOUNDS_NOT_PRESENT;
13825 }
13826
13827 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13828 if (high <= low)
13829 return PC_BOUNDS_INVALID;
13830
13831 /* When using the GNU linker, .gnu.linkonce. sections are used to
13832 eliminate duplicate copies of functions and vtables and such.
13833 The linker will arbitrarily choose one and discard the others.
13834 The AT_*_pc values for such functions refer to local labels in
13835 these sections. If the section from that file was discarded, the
13836 labels are not in the output, so the relocs get a value of 0.
13837 If this is a discarded function, mark the pc bounds as invalid,
13838 so that GDB will ignore it. */
13839 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13840 return PC_BOUNDS_INVALID;
13841
13842 *lowpc = low;
13843 if (highpc)
13844 *highpc = high;
13845 return ret;
13846 }
13847
13848 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13849 its low and high PC addresses. Do nothing if these addresses could not
13850 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13851 and HIGHPC to the high address if greater than HIGHPC. */
13852
13853 static void
13854 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13855 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13856 struct dwarf2_cu *cu)
13857 {
13858 CORE_ADDR low, high;
13859 struct die_info *child = die->child;
13860
13861 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13862 {
13863 *lowpc = std::min (*lowpc, low);
13864 *highpc = std::max (*highpc, high);
13865 }
13866
13867 /* If the language does not allow nested subprograms (either inside
13868 subprograms or lexical blocks), we're done. */
13869 if (cu->language != language_ada)
13870 return;
13871
13872 /* Check all the children of the given DIE. If it contains nested
13873 subprograms, then check their pc bounds. Likewise, we need to
13874 check lexical blocks as well, as they may also contain subprogram
13875 definitions. */
13876 while (child && child->tag)
13877 {
13878 if (child->tag == DW_TAG_subprogram
13879 || child->tag == DW_TAG_lexical_block)
13880 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13881 child = sibling_die (child);
13882 }
13883 }
13884
13885 /* Get the low and high pc's represented by the scope DIE, and store
13886 them in *LOWPC and *HIGHPC. If the correct values can't be
13887 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13888
13889 static void
13890 get_scope_pc_bounds (struct die_info *die,
13891 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13892 struct dwarf2_cu *cu)
13893 {
13894 CORE_ADDR best_low = (CORE_ADDR) -1;
13895 CORE_ADDR best_high = (CORE_ADDR) 0;
13896 CORE_ADDR current_low, current_high;
13897
13898 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13899 >= PC_BOUNDS_RANGES)
13900 {
13901 best_low = current_low;
13902 best_high = current_high;
13903 }
13904 else
13905 {
13906 struct die_info *child = die->child;
13907
13908 while (child && child->tag)
13909 {
13910 switch (child->tag) {
13911 case DW_TAG_subprogram:
13912 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13913 break;
13914 case DW_TAG_namespace:
13915 case DW_TAG_module:
13916 /* FIXME: carlton/2004-01-16: Should we do this for
13917 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13918 that current GCC's always emit the DIEs corresponding
13919 to definitions of methods of classes as children of a
13920 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13921 the DIEs giving the declarations, which could be
13922 anywhere). But I don't see any reason why the
13923 standards says that they have to be there. */
13924 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13925
13926 if (current_low != ((CORE_ADDR) -1))
13927 {
13928 best_low = std::min (best_low, current_low);
13929 best_high = std::max (best_high, current_high);
13930 }
13931 break;
13932 default:
13933 /* Ignore. */
13934 break;
13935 }
13936
13937 child = sibling_die (child);
13938 }
13939 }
13940
13941 *lowpc = best_low;
13942 *highpc = best_high;
13943 }
13944
13945 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13946 in DIE. */
13947
13948 static void
13949 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13950 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13951 {
13952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13954 struct attribute *attr;
13955 struct attribute *attr_high;
13956
13957 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13958 if (attr_high)
13959 {
13960 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13961 if (attr != nullptr)
13962 {
13963 CORE_ADDR low = attr->value_as_address ();
13964 CORE_ADDR high = attr_high->value_as_address ();
13965
13966 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13967 high += low;
13968
13969 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13970 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13971 cu->get_builder ()->record_block_range (block, low, high - 1);
13972 }
13973 }
13974
13975 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13976 if (attr != nullptr)
13977 {
13978 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13979 We take advantage of the fact that DW_AT_ranges does not appear
13980 in DW_TAG_compile_unit of DWO files. */
13981 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13982
13983 /* The value of the DW_AT_ranges attribute is the offset of the
13984 address range list in the .debug_ranges section. */
13985 unsigned long offset = (DW_UNSND (attr)
13986 + (need_ranges_base ? cu->ranges_base : 0));
13987
13988 std::vector<blockrange> blockvec;
13989 dwarf2_ranges_process (offset, cu,
13990 [&] (CORE_ADDR start, CORE_ADDR end)
13991 {
13992 start += baseaddr;
13993 end += baseaddr;
13994 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13995 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13996 cu->get_builder ()->record_block_range (block, start, end - 1);
13997 blockvec.emplace_back (start, end);
13998 });
13999
14000 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14001 }
14002 }
14003
14004 /* Check whether the producer field indicates either of GCC < 4.6, or the
14005 Intel C/C++ compiler, and cache the result in CU. */
14006
14007 static void
14008 check_producer (struct dwarf2_cu *cu)
14009 {
14010 int major, minor;
14011
14012 if (cu->producer == NULL)
14013 {
14014 /* For unknown compilers expect their behavior is DWARF version
14015 compliant.
14016
14017 GCC started to support .debug_types sections by -gdwarf-4 since
14018 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14019 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14020 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14021 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14022 }
14023 else if (producer_is_gcc (cu->producer, &major, &minor))
14024 {
14025 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14026 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14027 }
14028 else if (producer_is_icc (cu->producer, &major, &minor))
14029 {
14030 cu->producer_is_icc = true;
14031 cu->producer_is_icc_lt_14 = major < 14;
14032 }
14033 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14034 cu->producer_is_codewarrior = true;
14035 else
14036 {
14037 /* For other non-GCC compilers, expect their behavior is DWARF version
14038 compliant. */
14039 }
14040
14041 cu->checked_producer = true;
14042 }
14043
14044 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14045 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14046 during 4.6.0 experimental. */
14047
14048 static bool
14049 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14050 {
14051 if (!cu->checked_producer)
14052 check_producer (cu);
14053
14054 return cu->producer_is_gxx_lt_4_6;
14055 }
14056
14057
14058 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14059 with incorrect is_stmt attributes. */
14060
14061 static bool
14062 producer_is_codewarrior (struct dwarf2_cu *cu)
14063 {
14064 if (!cu->checked_producer)
14065 check_producer (cu);
14066
14067 return cu->producer_is_codewarrior;
14068 }
14069
14070 /* Return the default accessibility type if it is not overridden by
14071 DW_AT_accessibility. */
14072
14073 static enum dwarf_access_attribute
14074 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14075 {
14076 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14077 {
14078 /* The default DWARF 2 accessibility for members is public, the default
14079 accessibility for inheritance is private. */
14080
14081 if (die->tag != DW_TAG_inheritance)
14082 return DW_ACCESS_public;
14083 else
14084 return DW_ACCESS_private;
14085 }
14086 else
14087 {
14088 /* DWARF 3+ defines the default accessibility a different way. The same
14089 rules apply now for DW_TAG_inheritance as for the members and it only
14090 depends on the container kind. */
14091
14092 if (die->parent->tag == DW_TAG_class_type)
14093 return DW_ACCESS_private;
14094 else
14095 return DW_ACCESS_public;
14096 }
14097 }
14098
14099 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14100 offset. If the attribute was not found return 0, otherwise return
14101 1. If it was found but could not properly be handled, set *OFFSET
14102 to 0. */
14103
14104 static int
14105 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14106 LONGEST *offset)
14107 {
14108 struct attribute *attr;
14109
14110 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14111 if (attr != NULL)
14112 {
14113 *offset = 0;
14114
14115 /* Note that we do not check for a section offset first here.
14116 This is because DW_AT_data_member_location is new in DWARF 4,
14117 so if we see it, we can assume that a constant form is really
14118 a constant and not a section offset. */
14119 if (attr->form_is_constant ())
14120 *offset = dwarf2_get_attr_constant_value (attr, 0);
14121 else if (attr->form_is_section_offset ())
14122 dwarf2_complex_location_expr_complaint ();
14123 else if (attr->form_is_block ())
14124 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14125 else
14126 dwarf2_complex_location_expr_complaint ();
14127
14128 return 1;
14129 }
14130
14131 return 0;
14132 }
14133
14134 /* Add an aggregate field to the field list. */
14135
14136 static void
14137 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14138 struct dwarf2_cu *cu)
14139 {
14140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14142 struct nextfield *new_field;
14143 struct attribute *attr;
14144 struct field *fp;
14145 const char *fieldname = "";
14146
14147 if (die->tag == DW_TAG_inheritance)
14148 {
14149 fip->baseclasses.emplace_back ();
14150 new_field = &fip->baseclasses.back ();
14151 }
14152 else
14153 {
14154 fip->fields.emplace_back ();
14155 new_field = &fip->fields.back ();
14156 }
14157
14158 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14159 if (attr != nullptr)
14160 new_field->accessibility = DW_UNSND (attr);
14161 else
14162 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14163 if (new_field->accessibility != DW_ACCESS_public)
14164 fip->non_public_fields = 1;
14165
14166 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14167 if (attr != nullptr)
14168 new_field->virtuality = DW_UNSND (attr);
14169 else
14170 new_field->virtuality = DW_VIRTUALITY_none;
14171
14172 fp = &new_field->field;
14173
14174 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14175 {
14176 LONGEST offset;
14177
14178 /* Data member other than a C++ static data member. */
14179
14180 /* Get type of field. */
14181 fp->type = die_type (die, cu);
14182
14183 SET_FIELD_BITPOS (*fp, 0);
14184
14185 /* Get bit size of field (zero if none). */
14186 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14187 if (attr != nullptr)
14188 {
14189 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14190 }
14191 else
14192 {
14193 FIELD_BITSIZE (*fp) = 0;
14194 }
14195
14196 /* Get bit offset of field. */
14197 if (handle_data_member_location (die, cu, &offset))
14198 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14199 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14200 if (attr != nullptr)
14201 {
14202 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14203 {
14204 /* For big endian bits, the DW_AT_bit_offset gives the
14205 additional bit offset from the MSB of the containing
14206 anonymous object to the MSB of the field. We don't
14207 have to do anything special since we don't need to
14208 know the size of the anonymous object. */
14209 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14210 }
14211 else
14212 {
14213 /* For little endian bits, compute the bit offset to the
14214 MSB of the anonymous object, subtract off the number of
14215 bits from the MSB of the field to the MSB of the
14216 object, and then subtract off the number of bits of
14217 the field itself. The result is the bit offset of
14218 the LSB of the field. */
14219 int anonymous_size;
14220 int bit_offset = DW_UNSND (attr);
14221
14222 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14223 if (attr != nullptr)
14224 {
14225 /* The size of the anonymous object containing
14226 the bit field is explicit, so use the
14227 indicated size (in bytes). */
14228 anonymous_size = DW_UNSND (attr);
14229 }
14230 else
14231 {
14232 /* The size of the anonymous object containing
14233 the bit field must be inferred from the type
14234 attribute of the data member containing the
14235 bit field. */
14236 anonymous_size = TYPE_LENGTH (fp->type);
14237 }
14238 SET_FIELD_BITPOS (*fp,
14239 (FIELD_BITPOS (*fp)
14240 + anonymous_size * bits_per_byte
14241 - bit_offset - FIELD_BITSIZE (*fp)));
14242 }
14243 }
14244 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14245 if (attr != NULL)
14246 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14247 + dwarf2_get_attr_constant_value (attr, 0)));
14248
14249 /* Get name of field. */
14250 fieldname = dwarf2_name (die, cu);
14251 if (fieldname == NULL)
14252 fieldname = "";
14253
14254 /* The name is already allocated along with this objfile, so we don't
14255 need to duplicate it for the type. */
14256 fp->name = fieldname;
14257
14258 /* Change accessibility for artificial fields (e.g. virtual table
14259 pointer or virtual base class pointer) to private. */
14260 if (dwarf2_attr (die, DW_AT_artificial, cu))
14261 {
14262 FIELD_ARTIFICIAL (*fp) = 1;
14263 new_field->accessibility = DW_ACCESS_private;
14264 fip->non_public_fields = 1;
14265 }
14266 }
14267 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14268 {
14269 /* C++ static member. */
14270
14271 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14272 is a declaration, but all versions of G++ as of this writing
14273 (so through at least 3.2.1) incorrectly generate
14274 DW_TAG_variable tags. */
14275
14276 const char *physname;
14277
14278 /* Get name of field. */
14279 fieldname = dwarf2_name (die, cu);
14280 if (fieldname == NULL)
14281 return;
14282
14283 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14284 if (attr
14285 /* Only create a symbol if this is an external value.
14286 new_symbol checks this and puts the value in the global symbol
14287 table, which we want. If it is not external, new_symbol
14288 will try to put the value in cu->list_in_scope which is wrong. */
14289 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14290 {
14291 /* A static const member, not much different than an enum as far as
14292 we're concerned, except that we can support more types. */
14293 new_symbol (die, NULL, cu);
14294 }
14295
14296 /* Get physical name. */
14297 physname = dwarf2_physname (fieldname, die, cu);
14298
14299 /* The name is already allocated along with this objfile, so we don't
14300 need to duplicate it for the type. */
14301 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14302 FIELD_TYPE (*fp) = die_type (die, cu);
14303 FIELD_NAME (*fp) = fieldname;
14304 }
14305 else if (die->tag == DW_TAG_inheritance)
14306 {
14307 LONGEST offset;
14308
14309 /* C++ base class field. */
14310 if (handle_data_member_location (die, cu, &offset))
14311 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14312 FIELD_BITSIZE (*fp) = 0;
14313 FIELD_TYPE (*fp) = die_type (die, cu);
14314 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14315 }
14316 else if (die->tag == DW_TAG_variant_part)
14317 {
14318 /* process_structure_scope will treat this DIE as a union. */
14319 process_structure_scope (die, cu);
14320
14321 /* The variant part is relative to the start of the enclosing
14322 structure. */
14323 SET_FIELD_BITPOS (*fp, 0);
14324 fp->type = get_die_type (die, cu);
14325 fp->artificial = 1;
14326 fp->name = "<<variant>>";
14327
14328 /* Normally a DW_TAG_variant_part won't have a size, but our
14329 representation requires one, so set it to the maximum of the
14330 child sizes, being sure to account for the offset at which
14331 each child is seen. */
14332 if (TYPE_LENGTH (fp->type) == 0)
14333 {
14334 unsigned max = 0;
14335 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14336 {
14337 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14338 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14339 if (len > max)
14340 max = len;
14341 }
14342 TYPE_LENGTH (fp->type) = max;
14343 }
14344 }
14345 else
14346 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14347 }
14348
14349 /* Can the type given by DIE define another type? */
14350
14351 static bool
14352 type_can_define_types (const struct die_info *die)
14353 {
14354 switch (die->tag)
14355 {
14356 case DW_TAG_typedef:
14357 case DW_TAG_class_type:
14358 case DW_TAG_structure_type:
14359 case DW_TAG_union_type:
14360 case DW_TAG_enumeration_type:
14361 return true;
14362
14363 default:
14364 return false;
14365 }
14366 }
14367
14368 /* Add a type definition defined in the scope of the FIP's class. */
14369
14370 static void
14371 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14372 struct dwarf2_cu *cu)
14373 {
14374 struct decl_field fp;
14375 memset (&fp, 0, sizeof (fp));
14376
14377 gdb_assert (type_can_define_types (die));
14378
14379 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14380 fp.name = dwarf2_name (die, cu);
14381 fp.type = read_type_die (die, cu);
14382
14383 /* Save accessibility. */
14384 enum dwarf_access_attribute accessibility;
14385 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14386 if (attr != NULL)
14387 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14388 else
14389 accessibility = dwarf2_default_access_attribute (die, cu);
14390 switch (accessibility)
14391 {
14392 case DW_ACCESS_public:
14393 /* The assumed value if neither private nor protected. */
14394 break;
14395 case DW_ACCESS_private:
14396 fp.is_private = 1;
14397 break;
14398 case DW_ACCESS_protected:
14399 fp.is_protected = 1;
14400 break;
14401 default:
14402 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14403 }
14404
14405 if (die->tag == DW_TAG_typedef)
14406 fip->typedef_field_list.push_back (fp);
14407 else
14408 fip->nested_types_list.push_back (fp);
14409 }
14410
14411 /* Create the vector of fields, and attach it to the type. */
14412
14413 static void
14414 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14415 struct dwarf2_cu *cu)
14416 {
14417 int nfields = fip->nfields ();
14418
14419 /* Record the field count, allocate space for the array of fields,
14420 and create blank accessibility bitfields if necessary. */
14421 TYPE_NFIELDS (type) = nfields;
14422 TYPE_FIELDS (type) = (struct field *)
14423 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14424
14425 if (fip->non_public_fields && cu->language != language_ada)
14426 {
14427 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14428
14429 TYPE_FIELD_PRIVATE_BITS (type) =
14430 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14431 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14432
14433 TYPE_FIELD_PROTECTED_BITS (type) =
14434 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14435 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14436
14437 TYPE_FIELD_IGNORE_BITS (type) =
14438 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14439 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14440 }
14441
14442 /* If the type has baseclasses, allocate and clear a bit vector for
14443 TYPE_FIELD_VIRTUAL_BITS. */
14444 if (!fip->baseclasses.empty () && cu->language != language_ada)
14445 {
14446 int num_bytes = B_BYTES (fip->baseclasses.size ());
14447 unsigned char *pointer;
14448
14449 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14450 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14451 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14452 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14453 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14454 }
14455
14456 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14457 {
14458 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14459
14460 for (int index = 0; index < nfields; ++index)
14461 {
14462 struct nextfield &field = fip->fields[index];
14463
14464 if (field.variant.is_discriminant)
14465 di->discriminant_index = index;
14466 else if (field.variant.default_branch)
14467 di->default_index = index;
14468 else
14469 di->discriminants[index] = field.variant.discriminant_value;
14470 }
14471 }
14472
14473 /* Copy the saved-up fields into the field vector. */
14474 for (int i = 0; i < nfields; ++i)
14475 {
14476 struct nextfield &field
14477 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14478 : fip->fields[i - fip->baseclasses.size ()]);
14479
14480 TYPE_FIELD (type, i) = field.field;
14481 switch (field.accessibility)
14482 {
14483 case DW_ACCESS_private:
14484 if (cu->language != language_ada)
14485 SET_TYPE_FIELD_PRIVATE (type, i);
14486 break;
14487
14488 case DW_ACCESS_protected:
14489 if (cu->language != language_ada)
14490 SET_TYPE_FIELD_PROTECTED (type, i);
14491 break;
14492
14493 case DW_ACCESS_public:
14494 break;
14495
14496 default:
14497 /* Unknown accessibility. Complain and treat it as public. */
14498 {
14499 complaint (_("unsupported accessibility %d"),
14500 field.accessibility);
14501 }
14502 break;
14503 }
14504 if (i < fip->baseclasses.size ())
14505 {
14506 switch (field.virtuality)
14507 {
14508 case DW_VIRTUALITY_virtual:
14509 case DW_VIRTUALITY_pure_virtual:
14510 if (cu->language == language_ada)
14511 error (_("unexpected virtuality in component of Ada type"));
14512 SET_TYPE_FIELD_VIRTUAL (type, i);
14513 break;
14514 }
14515 }
14516 }
14517 }
14518
14519 /* Return true if this member function is a constructor, false
14520 otherwise. */
14521
14522 static int
14523 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14524 {
14525 const char *fieldname;
14526 const char *type_name;
14527 int len;
14528
14529 if (die->parent == NULL)
14530 return 0;
14531
14532 if (die->parent->tag != DW_TAG_structure_type
14533 && die->parent->tag != DW_TAG_union_type
14534 && die->parent->tag != DW_TAG_class_type)
14535 return 0;
14536
14537 fieldname = dwarf2_name (die, cu);
14538 type_name = dwarf2_name (die->parent, cu);
14539 if (fieldname == NULL || type_name == NULL)
14540 return 0;
14541
14542 len = strlen (fieldname);
14543 return (strncmp (fieldname, type_name, len) == 0
14544 && (type_name[len] == '\0' || type_name[len] == '<'));
14545 }
14546
14547 /* Check if the given VALUE is a recognized enum
14548 dwarf_defaulted_attribute constant according to DWARF5 spec,
14549 Table 7.24. */
14550
14551 static bool
14552 is_valid_DW_AT_defaulted (ULONGEST value)
14553 {
14554 switch (value)
14555 {
14556 case DW_DEFAULTED_no:
14557 case DW_DEFAULTED_in_class:
14558 case DW_DEFAULTED_out_of_class:
14559 return true;
14560 }
14561
14562 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14563 return false;
14564 }
14565
14566 /* Add a member function to the proper fieldlist. */
14567
14568 static void
14569 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14570 struct type *type, struct dwarf2_cu *cu)
14571 {
14572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14573 struct attribute *attr;
14574 int i;
14575 struct fnfieldlist *flp = nullptr;
14576 struct fn_field *fnp;
14577 const char *fieldname;
14578 struct type *this_type;
14579 enum dwarf_access_attribute accessibility;
14580
14581 if (cu->language == language_ada)
14582 error (_("unexpected member function in Ada type"));
14583
14584 /* Get name of member function. */
14585 fieldname = dwarf2_name (die, cu);
14586 if (fieldname == NULL)
14587 return;
14588
14589 /* Look up member function name in fieldlist. */
14590 for (i = 0; i < fip->fnfieldlists.size (); i++)
14591 {
14592 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14593 {
14594 flp = &fip->fnfieldlists[i];
14595 break;
14596 }
14597 }
14598
14599 /* Create a new fnfieldlist if necessary. */
14600 if (flp == nullptr)
14601 {
14602 fip->fnfieldlists.emplace_back ();
14603 flp = &fip->fnfieldlists.back ();
14604 flp->name = fieldname;
14605 i = fip->fnfieldlists.size () - 1;
14606 }
14607
14608 /* Create a new member function field and add it to the vector of
14609 fnfieldlists. */
14610 flp->fnfields.emplace_back ();
14611 fnp = &flp->fnfields.back ();
14612
14613 /* Delay processing of the physname until later. */
14614 if (cu->language == language_cplus)
14615 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14616 die, cu);
14617 else
14618 {
14619 const char *physname = dwarf2_physname (fieldname, die, cu);
14620 fnp->physname = physname ? physname : "";
14621 }
14622
14623 fnp->type = alloc_type (objfile);
14624 this_type = read_type_die (die, cu);
14625 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14626 {
14627 int nparams = TYPE_NFIELDS (this_type);
14628
14629 /* TYPE is the domain of this method, and THIS_TYPE is the type
14630 of the method itself (TYPE_CODE_METHOD). */
14631 smash_to_method_type (fnp->type, type,
14632 TYPE_TARGET_TYPE (this_type),
14633 TYPE_FIELDS (this_type),
14634 TYPE_NFIELDS (this_type),
14635 TYPE_VARARGS (this_type));
14636
14637 /* Handle static member functions.
14638 Dwarf2 has no clean way to discern C++ static and non-static
14639 member functions. G++ helps GDB by marking the first
14640 parameter for non-static member functions (which is the this
14641 pointer) as artificial. We obtain this information from
14642 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14643 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14644 fnp->voffset = VOFFSET_STATIC;
14645 }
14646 else
14647 complaint (_("member function type missing for '%s'"),
14648 dwarf2_full_name (fieldname, die, cu));
14649
14650 /* Get fcontext from DW_AT_containing_type if present. */
14651 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14652 fnp->fcontext = die_containing_type (die, cu);
14653
14654 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14655 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14656
14657 /* Get accessibility. */
14658 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14659 if (attr != nullptr)
14660 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14661 else
14662 accessibility = dwarf2_default_access_attribute (die, cu);
14663 switch (accessibility)
14664 {
14665 case DW_ACCESS_private:
14666 fnp->is_private = 1;
14667 break;
14668 case DW_ACCESS_protected:
14669 fnp->is_protected = 1;
14670 break;
14671 }
14672
14673 /* Check for artificial methods. */
14674 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14675 if (attr && DW_UNSND (attr) != 0)
14676 fnp->is_artificial = 1;
14677
14678 /* Check for defaulted methods. */
14679 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14680 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14681 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14682
14683 /* Check for deleted methods. */
14684 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14685 if (attr != nullptr && DW_UNSND (attr) != 0)
14686 fnp->is_deleted = 1;
14687
14688 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14689
14690 /* Get index in virtual function table if it is a virtual member
14691 function. For older versions of GCC, this is an offset in the
14692 appropriate virtual table, as specified by DW_AT_containing_type.
14693 For everyone else, it is an expression to be evaluated relative
14694 to the object address. */
14695
14696 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14697 if (attr != nullptr)
14698 {
14699 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14700 {
14701 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14702 {
14703 /* Old-style GCC. */
14704 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14705 }
14706 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14707 || (DW_BLOCK (attr)->size > 1
14708 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14709 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14710 {
14711 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14712 if ((fnp->voffset % cu->header.addr_size) != 0)
14713 dwarf2_complex_location_expr_complaint ();
14714 else
14715 fnp->voffset /= cu->header.addr_size;
14716 fnp->voffset += 2;
14717 }
14718 else
14719 dwarf2_complex_location_expr_complaint ();
14720
14721 if (!fnp->fcontext)
14722 {
14723 /* If there is no `this' field and no DW_AT_containing_type,
14724 we cannot actually find a base class context for the
14725 vtable! */
14726 if (TYPE_NFIELDS (this_type) == 0
14727 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14728 {
14729 complaint (_("cannot determine context for virtual member "
14730 "function \"%s\" (offset %s)"),
14731 fieldname, sect_offset_str (die->sect_off));
14732 }
14733 else
14734 {
14735 fnp->fcontext
14736 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14737 }
14738 }
14739 }
14740 else if (attr->form_is_section_offset ())
14741 {
14742 dwarf2_complex_location_expr_complaint ();
14743 }
14744 else
14745 {
14746 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14747 fieldname);
14748 }
14749 }
14750 else
14751 {
14752 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14753 if (attr && DW_UNSND (attr))
14754 {
14755 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14756 complaint (_("Member function \"%s\" (offset %s) is virtual "
14757 "but the vtable offset is not specified"),
14758 fieldname, sect_offset_str (die->sect_off));
14759 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14760 TYPE_CPLUS_DYNAMIC (type) = 1;
14761 }
14762 }
14763 }
14764
14765 /* Create the vector of member function fields, and attach it to the type. */
14766
14767 static void
14768 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14769 struct dwarf2_cu *cu)
14770 {
14771 if (cu->language == language_ada)
14772 error (_("unexpected member functions in Ada type"));
14773
14774 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14775 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14776 TYPE_ALLOC (type,
14777 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14778
14779 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14780 {
14781 struct fnfieldlist &nf = fip->fnfieldlists[i];
14782 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14783
14784 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14785 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14786 fn_flp->fn_fields = (struct fn_field *)
14787 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14788
14789 for (int k = 0; k < nf.fnfields.size (); ++k)
14790 fn_flp->fn_fields[k] = nf.fnfields[k];
14791 }
14792
14793 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14794 }
14795
14796 /* Returns non-zero if NAME is the name of a vtable member in CU's
14797 language, zero otherwise. */
14798 static int
14799 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14800 {
14801 static const char vptr[] = "_vptr";
14802
14803 /* Look for the C++ form of the vtable. */
14804 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14805 return 1;
14806
14807 return 0;
14808 }
14809
14810 /* GCC outputs unnamed structures that are really pointers to member
14811 functions, with the ABI-specified layout. If TYPE describes
14812 such a structure, smash it into a member function type.
14813
14814 GCC shouldn't do this; it should just output pointer to member DIEs.
14815 This is GCC PR debug/28767. */
14816
14817 static void
14818 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14819 {
14820 struct type *pfn_type, *self_type, *new_type;
14821
14822 /* Check for a structure with no name and two children. */
14823 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14824 return;
14825
14826 /* Check for __pfn and __delta members. */
14827 if (TYPE_FIELD_NAME (type, 0) == NULL
14828 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14829 || TYPE_FIELD_NAME (type, 1) == NULL
14830 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14831 return;
14832
14833 /* Find the type of the method. */
14834 pfn_type = TYPE_FIELD_TYPE (type, 0);
14835 if (pfn_type == NULL
14836 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14837 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14838 return;
14839
14840 /* Look for the "this" argument. */
14841 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14842 if (TYPE_NFIELDS (pfn_type) == 0
14843 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14844 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14845 return;
14846
14847 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14848 new_type = alloc_type (objfile);
14849 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14850 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14851 TYPE_VARARGS (pfn_type));
14852 smash_to_methodptr_type (type, new_type);
14853 }
14854
14855 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14856 appropriate error checking and issuing complaints if there is a
14857 problem. */
14858
14859 static ULONGEST
14860 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14861 {
14862 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14863
14864 if (attr == nullptr)
14865 return 0;
14866
14867 if (!attr->form_is_constant ())
14868 {
14869 complaint (_("DW_AT_alignment must have constant form"
14870 " - DIE at %s [in module %s]"),
14871 sect_offset_str (die->sect_off),
14872 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14873 return 0;
14874 }
14875
14876 ULONGEST align;
14877 if (attr->form == DW_FORM_sdata)
14878 {
14879 LONGEST val = DW_SND (attr);
14880 if (val < 0)
14881 {
14882 complaint (_("DW_AT_alignment value must not be negative"
14883 " - DIE at %s [in module %s]"),
14884 sect_offset_str (die->sect_off),
14885 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14886 return 0;
14887 }
14888 align = val;
14889 }
14890 else
14891 align = DW_UNSND (attr);
14892
14893 if (align == 0)
14894 {
14895 complaint (_("DW_AT_alignment value must not be zero"
14896 " - DIE at %s [in module %s]"),
14897 sect_offset_str (die->sect_off),
14898 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14899 return 0;
14900 }
14901 if ((align & (align - 1)) != 0)
14902 {
14903 complaint (_("DW_AT_alignment value must be a power of 2"
14904 " - DIE at %s [in module %s]"),
14905 sect_offset_str (die->sect_off),
14906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14907 return 0;
14908 }
14909
14910 return align;
14911 }
14912
14913 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14914 the alignment for TYPE. */
14915
14916 static void
14917 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14918 struct type *type)
14919 {
14920 if (!set_type_align (type, get_alignment (cu, die)))
14921 complaint (_("DW_AT_alignment value too large"
14922 " - DIE at %s [in module %s]"),
14923 sect_offset_str (die->sect_off),
14924 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14925 }
14926
14927 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14928 constant for a type, according to DWARF5 spec, Table 5.5. */
14929
14930 static bool
14931 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14932 {
14933 switch (value)
14934 {
14935 case DW_CC_normal:
14936 case DW_CC_pass_by_reference:
14937 case DW_CC_pass_by_value:
14938 return true;
14939
14940 default:
14941 complaint (_("unrecognized DW_AT_calling_convention value "
14942 "(%s) for a type"), pulongest (value));
14943 return false;
14944 }
14945 }
14946
14947 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14948 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14949 also according to GNU-specific values (see include/dwarf2.h). */
14950
14951 static bool
14952 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14953 {
14954 switch (value)
14955 {
14956 case DW_CC_normal:
14957 case DW_CC_program:
14958 case DW_CC_nocall:
14959 return true;
14960
14961 case DW_CC_GNU_renesas_sh:
14962 case DW_CC_GNU_borland_fastcall_i386:
14963 case DW_CC_GDB_IBM_OpenCL:
14964 return true;
14965
14966 default:
14967 complaint (_("unrecognized DW_AT_calling_convention value "
14968 "(%s) for a subroutine"), pulongest (value));
14969 return false;
14970 }
14971 }
14972
14973 /* Called when we find the DIE that starts a structure or union scope
14974 (definition) to create a type for the structure or union. Fill in
14975 the type's name and general properties; the members will not be
14976 processed until process_structure_scope. A symbol table entry for
14977 the type will also not be done until process_structure_scope (assuming
14978 the type has a name).
14979
14980 NOTE: we need to call these functions regardless of whether or not the
14981 DIE has a DW_AT_name attribute, since it might be an anonymous
14982 structure or union. This gets the type entered into our set of
14983 user defined types. */
14984
14985 static struct type *
14986 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14987 {
14988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14989 struct type *type;
14990 struct attribute *attr;
14991 const char *name;
14992
14993 /* If the definition of this type lives in .debug_types, read that type.
14994 Don't follow DW_AT_specification though, that will take us back up
14995 the chain and we want to go down. */
14996 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14997 if (attr != nullptr)
14998 {
14999 type = get_DW_AT_signature_type (die, attr, cu);
15000
15001 /* The type's CU may not be the same as CU.
15002 Ensure TYPE is recorded with CU in die_type_hash. */
15003 return set_die_type (die, type, cu);
15004 }
15005
15006 type = alloc_type (objfile);
15007 INIT_CPLUS_SPECIFIC (type);
15008
15009 name = dwarf2_name (die, cu);
15010 if (name != NULL)
15011 {
15012 if (cu->language == language_cplus
15013 || cu->language == language_d
15014 || cu->language == language_rust)
15015 {
15016 const char *full_name = dwarf2_full_name (name, die, cu);
15017
15018 /* dwarf2_full_name might have already finished building the DIE's
15019 type. If so, there is no need to continue. */
15020 if (get_die_type (die, cu) != NULL)
15021 return get_die_type (die, cu);
15022
15023 TYPE_NAME (type) = full_name;
15024 }
15025 else
15026 {
15027 /* The name is already allocated along with this objfile, so
15028 we don't need to duplicate it for the type. */
15029 TYPE_NAME (type) = name;
15030 }
15031 }
15032
15033 if (die->tag == DW_TAG_structure_type)
15034 {
15035 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15036 }
15037 else if (die->tag == DW_TAG_union_type)
15038 {
15039 TYPE_CODE (type) = TYPE_CODE_UNION;
15040 }
15041 else if (die->tag == DW_TAG_variant_part)
15042 {
15043 TYPE_CODE (type) = TYPE_CODE_UNION;
15044 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15045 }
15046 else
15047 {
15048 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15049 }
15050
15051 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15052 TYPE_DECLARED_CLASS (type) = 1;
15053
15054 /* Store the calling convention in the type if it's available in
15055 the die. Otherwise the calling convention remains set to
15056 the default value DW_CC_normal. */
15057 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15058 if (attr != nullptr
15059 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15060 {
15061 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15062 TYPE_CPLUS_CALLING_CONVENTION (type)
15063 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15064 }
15065
15066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15067 if (attr != nullptr)
15068 {
15069 if (attr->form_is_constant ())
15070 TYPE_LENGTH (type) = DW_UNSND (attr);
15071 else
15072 {
15073 /* For the moment, dynamic type sizes are not supported
15074 by GDB's struct type. The actual size is determined
15075 on-demand when resolving the type of a given object,
15076 so set the type's length to zero for now. Otherwise,
15077 we record an expression as the length, and that expression
15078 could lead to a very large value, which could eventually
15079 lead to us trying to allocate that much memory when creating
15080 a value of that type. */
15081 TYPE_LENGTH (type) = 0;
15082 }
15083 }
15084 else
15085 {
15086 TYPE_LENGTH (type) = 0;
15087 }
15088
15089 maybe_set_alignment (cu, die, type);
15090
15091 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15092 {
15093 /* ICC<14 does not output the required DW_AT_declaration on
15094 incomplete types, but gives them a size of zero. */
15095 TYPE_STUB (type) = 1;
15096 }
15097 else
15098 TYPE_STUB_SUPPORTED (type) = 1;
15099
15100 if (die_is_declaration (die, cu))
15101 TYPE_STUB (type) = 1;
15102 else if (attr == NULL && die->child == NULL
15103 && producer_is_realview (cu->producer))
15104 /* RealView does not output the required DW_AT_declaration
15105 on incomplete types. */
15106 TYPE_STUB (type) = 1;
15107
15108 /* We need to add the type field to the die immediately so we don't
15109 infinitely recurse when dealing with pointers to the structure
15110 type within the structure itself. */
15111 set_die_type (die, type, cu);
15112
15113 /* set_die_type should be already done. */
15114 set_descriptive_type (type, die, cu);
15115
15116 return type;
15117 }
15118
15119 /* A helper for process_structure_scope that handles a single member
15120 DIE. */
15121
15122 static void
15123 handle_struct_member_die (struct die_info *child_die, struct type *type,
15124 struct field_info *fi,
15125 std::vector<struct symbol *> *template_args,
15126 struct dwarf2_cu *cu)
15127 {
15128 if (child_die->tag == DW_TAG_member
15129 || child_die->tag == DW_TAG_variable
15130 || child_die->tag == DW_TAG_variant_part)
15131 {
15132 /* NOTE: carlton/2002-11-05: A C++ static data member
15133 should be a DW_TAG_member that is a declaration, but
15134 all versions of G++ as of this writing (so through at
15135 least 3.2.1) incorrectly generate DW_TAG_variable
15136 tags for them instead. */
15137 dwarf2_add_field (fi, child_die, cu);
15138 }
15139 else if (child_die->tag == DW_TAG_subprogram)
15140 {
15141 /* Rust doesn't have member functions in the C++ sense.
15142 However, it does emit ordinary functions as children
15143 of a struct DIE. */
15144 if (cu->language == language_rust)
15145 read_func_scope (child_die, cu);
15146 else
15147 {
15148 /* C++ member function. */
15149 dwarf2_add_member_fn (fi, child_die, type, cu);
15150 }
15151 }
15152 else if (child_die->tag == DW_TAG_inheritance)
15153 {
15154 /* C++ base class field. */
15155 dwarf2_add_field (fi, child_die, cu);
15156 }
15157 else if (type_can_define_types (child_die))
15158 dwarf2_add_type_defn (fi, child_die, cu);
15159 else if (child_die->tag == DW_TAG_template_type_param
15160 || child_die->tag == DW_TAG_template_value_param)
15161 {
15162 struct symbol *arg = new_symbol (child_die, NULL, cu);
15163
15164 if (arg != NULL)
15165 template_args->push_back (arg);
15166 }
15167 else if (child_die->tag == DW_TAG_variant)
15168 {
15169 /* In a variant we want to get the discriminant and also add a
15170 field for our sole member child. */
15171 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15172
15173 for (die_info *variant_child = child_die->child;
15174 variant_child != NULL;
15175 variant_child = sibling_die (variant_child))
15176 {
15177 if (variant_child->tag == DW_TAG_member)
15178 {
15179 handle_struct_member_die (variant_child, type, fi,
15180 template_args, cu);
15181 /* Only handle the one. */
15182 break;
15183 }
15184 }
15185
15186 /* We don't handle this but we might as well report it if we see
15187 it. */
15188 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15189 complaint (_("DW_AT_discr_list is not supported yet"
15190 " - DIE at %s [in module %s]"),
15191 sect_offset_str (child_die->sect_off),
15192 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15193
15194 /* The first field was just added, so we can stash the
15195 discriminant there. */
15196 gdb_assert (!fi->fields.empty ());
15197 if (discr == NULL)
15198 fi->fields.back ().variant.default_branch = true;
15199 else
15200 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15201 }
15202 }
15203
15204 /* Finish creating a structure or union type, including filling in
15205 its members and creating a symbol for it. */
15206
15207 static void
15208 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15209 {
15210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15211 struct die_info *child_die;
15212 struct type *type;
15213
15214 type = get_die_type (die, cu);
15215 if (type == NULL)
15216 type = read_structure_type (die, cu);
15217
15218 /* When reading a DW_TAG_variant_part, we need to notice when we
15219 read the discriminant member, so we can record it later in the
15220 discriminant_info. */
15221 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15222 sect_offset discr_offset {};
15223 bool has_template_parameters = false;
15224
15225 if (is_variant_part)
15226 {
15227 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15228 if (discr == NULL)
15229 {
15230 /* Maybe it's a univariant form, an extension we support.
15231 In this case arrange not to check the offset. */
15232 is_variant_part = false;
15233 }
15234 else if (discr->form_is_ref ())
15235 {
15236 struct dwarf2_cu *target_cu = cu;
15237 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15238
15239 discr_offset = target_die->sect_off;
15240 }
15241 else
15242 {
15243 complaint (_("DW_AT_discr does not have DIE reference form"
15244 " - DIE at %s [in module %s]"),
15245 sect_offset_str (die->sect_off),
15246 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15247 is_variant_part = false;
15248 }
15249 }
15250
15251 if (die->child != NULL && ! die_is_declaration (die, cu))
15252 {
15253 struct field_info fi;
15254 std::vector<struct symbol *> template_args;
15255
15256 child_die = die->child;
15257
15258 while (child_die && child_die->tag)
15259 {
15260 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15261
15262 if (is_variant_part && discr_offset == child_die->sect_off)
15263 fi.fields.back ().variant.is_discriminant = true;
15264
15265 child_die = sibling_die (child_die);
15266 }
15267
15268 /* Attach template arguments to type. */
15269 if (!template_args.empty ())
15270 {
15271 has_template_parameters = true;
15272 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15273 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15274 TYPE_TEMPLATE_ARGUMENTS (type)
15275 = XOBNEWVEC (&objfile->objfile_obstack,
15276 struct symbol *,
15277 TYPE_N_TEMPLATE_ARGUMENTS (type));
15278 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15279 template_args.data (),
15280 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15281 * sizeof (struct symbol *)));
15282 }
15283
15284 /* Attach fields and member functions to the type. */
15285 if (fi.nfields () > 0)
15286 dwarf2_attach_fields_to_type (&fi, type, cu);
15287 if (!fi.fnfieldlists.empty ())
15288 {
15289 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15290
15291 /* Get the type which refers to the base class (possibly this
15292 class itself) which contains the vtable pointer for the current
15293 class from the DW_AT_containing_type attribute. This use of
15294 DW_AT_containing_type is a GNU extension. */
15295
15296 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15297 {
15298 struct type *t = die_containing_type (die, cu);
15299
15300 set_type_vptr_basetype (type, t);
15301 if (type == t)
15302 {
15303 int i;
15304
15305 /* Our own class provides vtbl ptr. */
15306 for (i = TYPE_NFIELDS (t) - 1;
15307 i >= TYPE_N_BASECLASSES (t);
15308 --i)
15309 {
15310 const char *fieldname = TYPE_FIELD_NAME (t, i);
15311
15312 if (is_vtable_name (fieldname, cu))
15313 {
15314 set_type_vptr_fieldno (type, i);
15315 break;
15316 }
15317 }
15318
15319 /* Complain if virtual function table field not found. */
15320 if (i < TYPE_N_BASECLASSES (t))
15321 complaint (_("virtual function table pointer "
15322 "not found when defining class '%s'"),
15323 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15324 }
15325 else
15326 {
15327 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15328 }
15329 }
15330 else if (cu->producer
15331 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15332 {
15333 /* The IBM XLC compiler does not provide direct indication
15334 of the containing type, but the vtable pointer is
15335 always named __vfp. */
15336
15337 int i;
15338
15339 for (i = TYPE_NFIELDS (type) - 1;
15340 i >= TYPE_N_BASECLASSES (type);
15341 --i)
15342 {
15343 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15344 {
15345 set_type_vptr_fieldno (type, i);
15346 set_type_vptr_basetype (type, type);
15347 break;
15348 }
15349 }
15350 }
15351 }
15352
15353 /* Copy fi.typedef_field_list linked list elements content into the
15354 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15355 if (!fi.typedef_field_list.empty ())
15356 {
15357 int count = fi.typedef_field_list.size ();
15358
15359 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15360 TYPE_TYPEDEF_FIELD_ARRAY (type)
15361 = ((struct decl_field *)
15362 TYPE_ALLOC (type,
15363 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15364 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15365
15366 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15367 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15368 }
15369
15370 /* Copy fi.nested_types_list linked list elements content into the
15371 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15372 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15373 {
15374 int count = fi.nested_types_list.size ();
15375
15376 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15377 TYPE_NESTED_TYPES_ARRAY (type)
15378 = ((struct decl_field *)
15379 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15380 TYPE_NESTED_TYPES_COUNT (type) = count;
15381
15382 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15383 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15384 }
15385 }
15386
15387 quirk_gcc_member_function_pointer (type, objfile);
15388 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15389 cu->rust_unions.push_back (type);
15390
15391 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15392 snapshots) has been known to create a die giving a declaration
15393 for a class that has, as a child, a die giving a definition for a
15394 nested class. So we have to process our children even if the
15395 current die is a declaration. Normally, of course, a declaration
15396 won't have any children at all. */
15397
15398 child_die = die->child;
15399
15400 while (child_die != NULL && child_die->tag)
15401 {
15402 if (child_die->tag == DW_TAG_member
15403 || child_die->tag == DW_TAG_variable
15404 || child_die->tag == DW_TAG_inheritance
15405 || child_die->tag == DW_TAG_template_value_param
15406 || child_die->tag == DW_TAG_template_type_param)
15407 {
15408 /* Do nothing. */
15409 }
15410 else
15411 process_die (child_die, cu);
15412
15413 child_die = sibling_die (child_die);
15414 }
15415
15416 /* Do not consider external references. According to the DWARF standard,
15417 these DIEs are identified by the fact that they have no byte_size
15418 attribute, and a declaration attribute. */
15419 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15420 || !die_is_declaration (die, cu))
15421 {
15422 struct symbol *sym = new_symbol (die, type, cu);
15423
15424 if (has_template_parameters)
15425 {
15426 struct symtab *symtab;
15427 if (sym != nullptr)
15428 symtab = symbol_symtab (sym);
15429 else if (cu->line_header != nullptr)
15430 {
15431 /* Any related symtab will do. */
15432 symtab
15433 = cu->line_header->file_names ()[0].symtab;
15434 }
15435 else
15436 {
15437 symtab = nullptr;
15438 complaint (_("could not find suitable "
15439 "symtab for template parameter"
15440 " - DIE at %s [in module %s]"),
15441 sect_offset_str (die->sect_off),
15442 objfile_name (objfile));
15443 }
15444
15445 if (symtab != nullptr)
15446 {
15447 /* Make sure that the symtab is set on the new symbols.
15448 Even though they don't appear in this symtab directly,
15449 other parts of gdb assume that symbols do, and this is
15450 reasonably true. */
15451 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15452 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15453 }
15454 }
15455 }
15456 }
15457
15458 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15459 update TYPE using some information only available in DIE's children. */
15460
15461 static void
15462 update_enumeration_type_from_children (struct die_info *die,
15463 struct type *type,
15464 struct dwarf2_cu *cu)
15465 {
15466 struct die_info *child_die;
15467 int unsigned_enum = 1;
15468 int flag_enum = 1;
15469
15470 auto_obstack obstack;
15471
15472 for (child_die = die->child;
15473 child_die != NULL && child_die->tag;
15474 child_die = sibling_die (child_die))
15475 {
15476 struct attribute *attr;
15477 LONGEST value;
15478 const gdb_byte *bytes;
15479 struct dwarf2_locexpr_baton *baton;
15480 const char *name;
15481
15482 if (child_die->tag != DW_TAG_enumerator)
15483 continue;
15484
15485 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15486 if (attr == NULL)
15487 continue;
15488
15489 name = dwarf2_name (child_die, cu);
15490 if (name == NULL)
15491 name = "<anonymous enumerator>";
15492
15493 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15494 &value, &bytes, &baton);
15495 if (value < 0)
15496 {
15497 unsigned_enum = 0;
15498 flag_enum = 0;
15499 }
15500 else
15501 {
15502 if (count_one_bits_ll (value) >= 2)
15503 flag_enum = 0;
15504 }
15505
15506 /* If we already know that the enum type is neither unsigned, nor
15507 a flag type, no need to look at the rest of the enumerates. */
15508 if (!unsigned_enum && !flag_enum)
15509 break;
15510 }
15511
15512 if (unsigned_enum)
15513 TYPE_UNSIGNED (type) = 1;
15514 if (flag_enum)
15515 TYPE_FLAG_ENUM (type) = 1;
15516 }
15517
15518 /* Given a DW_AT_enumeration_type die, set its type. We do not
15519 complete the type's fields yet, or create any symbols. */
15520
15521 static struct type *
15522 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15523 {
15524 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15525 struct type *type;
15526 struct attribute *attr;
15527 const char *name;
15528
15529 /* If the definition of this type lives in .debug_types, read that type.
15530 Don't follow DW_AT_specification though, that will take us back up
15531 the chain and we want to go down. */
15532 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15533 if (attr != nullptr)
15534 {
15535 type = get_DW_AT_signature_type (die, attr, cu);
15536
15537 /* The type's CU may not be the same as CU.
15538 Ensure TYPE is recorded with CU in die_type_hash. */
15539 return set_die_type (die, type, cu);
15540 }
15541
15542 type = alloc_type (objfile);
15543
15544 TYPE_CODE (type) = TYPE_CODE_ENUM;
15545 name = dwarf2_full_name (NULL, die, cu);
15546 if (name != NULL)
15547 TYPE_NAME (type) = name;
15548
15549 attr = dwarf2_attr (die, DW_AT_type, cu);
15550 if (attr != NULL)
15551 {
15552 struct type *underlying_type = die_type (die, cu);
15553
15554 TYPE_TARGET_TYPE (type) = underlying_type;
15555 }
15556
15557 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15558 if (attr != nullptr)
15559 {
15560 TYPE_LENGTH (type) = DW_UNSND (attr);
15561 }
15562 else
15563 {
15564 TYPE_LENGTH (type) = 0;
15565 }
15566
15567 maybe_set_alignment (cu, die, type);
15568
15569 /* The enumeration DIE can be incomplete. In Ada, any type can be
15570 declared as private in the package spec, and then defined only
15571 inside the package body. Such types are known as Taft Amendment
15572 Types. When another package uses such a type, an incomplete DIE
15573 may be generated by the compiler. */
15574 if (die_is_declaration (die, cu))
15575 TYPE_STUB (type) = 1;
15576
15577 /* Finish the creation of this type by using the enum's children.
15578 We must call this even when the underlying type has been provided
15579 so that we can determine if we're looking at a "flag" enum. */
15580 update_enumeration_type_from_children (die, type, cu);
15581
15582 /* If this type has an underlying type that is not a stub, then we
15583 may use its attributes. We always use the "unsigned" attribute
15584 in this situation, because ordinarily we guess whether the type
15585 is unsigned -- but the guess can be wrong and the underlying type
15586 can tell us the reality. However, we defer to a local size
15587 attribute if one exists, because this lets the compiler override
15588 the underlying type if needed. */
15589 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15590 {
15591 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15592 if (TYPE_LENGTH (type) == 0)
15593 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15594 if (TYPE_RAW_ALIGN (type) == 0
15595 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15596 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15597 }
15598
15599 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15600
15601 return set_die_type (die, type, cu);
15602 }
15603
15604 /* Given a pointer to a die which begins an enumeration, process all
15605 the dies that define the members of the enumeration, and create the
15606 symbol for the enumeration type.
15607
15608 NOTE: We reverse the order of the element list. */
15609
15610 static void
15611 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15612 {
15613 struct type *this_type;
15614
15615 this_type = get_die_type (die, cu);
15616 if (this_type == NULL)
15617 this_type = read_enumeration_type (die, cu);
15618
15619 if (die->child != NULL)
15620 {
15621 struct die_info *child_die;
15622 struct symbol *sym;
15623 std::vector<struct field> fields;
15624 const char *name;
15625
15626 child_die = die->child;
15627 while (child_die && child_die->tag)
15628 {
15629 if (child_die->tag != DW_TAG_enumerator)
15630 {
15631 process_die (child_die, cu);
15632 }
15633 else
15634 {
15635 name = dwarf2_name (child_die, cu);
15636 if (name)
15637 {
15638 sym = new_symbol (child_die, this_type, cu);
15639
15640 fields.emplace_back ();
15641 struct field &field = fields.back ();
15642
15643 FIELD_NAME (field) = sym->linkage_name ();
15644 FIELD_TYPE (field) = NULL;
15645 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15646 FIELD_BITSIZE (field) = 0;
15647 }
15648 }
15649
15650 child_die = sibling_die (child_die);
15651 }
15652
15653 if (!fields.empty ())
15654 {
15655 TYPE_NFIELDS (this_type) = fields.size ();
15656 TYPE_FIELDS (this_type) = (struct field *)
15657 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15658 memcpy (TYPE_FIELDS (this_type), fields.data (),
15659 sizeof (struct field) * fields.size ());
15660 }
15661 }
15662
15663 /* If we are reading an enum from a .debug_types unit, and the enum
15664 is a declaration, and the enum is not the signatured type in the
15665 unit, then we do not want to add a symbol for it. Adding a
15666 symbol would in some cases obscure the true definition of the
15667 enum, giving users an incomplete type when the definition is
15668 actually available. Note that we do not want to do this for all
15669 enums which are just declarations, because C++0x allows forward
15670 enum declarations. */
15671 if (cu->per_cu->is_debug_types
15672 && die_is_declaration (die, cu))
15673 {
15674 struct signatured_type *sig_type;
15675
15676 sig_type = (struct signatured_type *) cu->per_cu;
15677 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15678 if (sig_type->type_offset_in_section != die->sect_off)
15679 return;
15680 }
15681
15682 new_symbol (die, this_type, cu);
15683 }
15684
15685 /* Extract all information from a DW_TAG_array_type DIE and put it in
15686 the DIE's type field. For now, this only handles one dimensional
15687 arrays. */
15688
15689 static struct type *
15690 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15691 {
15692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15693 struct die_info *child_die;
15694 struct type *type;
15695 struct type *element_type, *range_type, *index_type;
15696 struct attribute *attr;
15697 const char *name;
15698 struct dynamic_prop *byte_stride_prop = NULL;
15699 unsigned int bit_stride = 0;
15700
15701 element_type = die_type (die, cu);
15702
15703 /* The die_type call above may have already set the type for this DIE. */
15704 type = get_die_type (die, cu);
15705 if (type)
15706 return type;
15707
15708 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15709 if (attr != NULL)
15710 {
15711 int stride_ok;
15712 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15713
15714 byte_stride_prop
15715 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15716 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15717 prop_type);
15718 if (!stride_ok)
15719 {
15720 complaint (_("unable to read array DW_AT_byte_stride "
15721 " - DIE at %s [in module %s]"),
15722 sect_offset_str (die->sect_off),
15723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15724 /* Ignore this attribute. We will likely not be able to print
15725 arrays of this type correctly, but there is little we can do
15726 to help if we cannot read the attribute's value. */
15727 byte_stride_prop = NULL;
15728 }
15729 }
15730
15731 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15732 if (attr != NULL)
15733 bit_stride = DW_UNSND (attr);
15734
15735 /* Irix 6.2 native cc creates array types without children for
15736 arrays with unspecified length. */
15737 if (die->child == NULL)
15738 {
15739 index_type = objfile_type (objfile)->builtin_int;
15740 range_type = create_static_range_type (NULL, index_type, 0, -1);
15741 type = create_array_type_with_stride (NULL, element_type, range_type,
15742 byte_stride_prop, bit_stride);
15743 return set_die_type (die, type, cu);
15744 }
15745
15746 std::vector<struct type *> range_types;
15747 child_die = die->child;
15748 while (child_die && child_die->tag)
15749 {
15750 if (child_die->tag == DW_TAG_subrange_type)
15751 {
15752 struct type *child_type = read_type_die (child_die, cu);
15753
15754 if (child_type != NULL)
15755 {
15756 /* The range type was succesfully read. Save it for the
15757 array type creation. */
15758 range_types.push_back (child_type);
15759 }
15760 }
15761 child_die = sibling_die (child_die);
15762 }
15763
15764 /* Dwarf2 dimensions are output from left to right, create the
15765 necessary array types in backwards order. */
15766
15767 type = element_type;
15768
15769 if (read_array_order (die, cu) == DW_ORD_col_major)
15770 {
15771 int i = 0;
15772
15773 while (i < range_types.size ())
15774 type = create_array_type_with_stride (NULL, type, range_types[i++],
15775 byte_stride_prop, bit_stride);
15776 }
15777 else
15778 {
15779 size_t ndim = range_types.size ();
15780 while (ndim-- > 0)
15781 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15782 byte_stride_prop, bit_stride);
15783 }
15784
15785 /* Understand Dwarf2 support for vector types (like they occur on
15786 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15787 array type. This is not part of the Dwarf2/3 standard yet, but a
15788 custom vendor extension. The main difference between a regular
15789 array and the vector variant is that vectors are passed by value
15790 to functions. */
15791 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15792 if (attr != nullptr)
15793 make_vector_type (type);
15794
15795 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15796 implementation may choose to implement triple vectors using this
15797 attribute. */
15798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15799 if (attr != nullptr)
15800 {
15801 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15802 TYPE_LENGTH (type) = DW_UNSND (attr);
15803 else
15804 complaint (_("DW_AT_byte_size for array type smaller "
15805 "than the total size of elements"));
15806 }
15807
15808 name = dwarf2_name (die, cu);
15809 if (name)
15810 TYPE_NAME (type) = name;
15811
15812 maybe_set_alignment (cu, die, type);
15813
15814 /* Install the type in the die. */
15815 set_die_type (die, type, cu);
15816
15817 /* set_die_type should be already done. */
15818 set_descriptive_type (type, die, cu);
15819
15820 return type;
15821 }
15822
15823 static enum dwarf_array_dim_ordering
15824 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15825 {
15826 struct attribute *attr;
15827
15828 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15829
15830 if (attr != nullptr)
15831 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15832
15833 /* GNU F77 is a special case, as at 08/2004 array type info is the
15834 opposite order to the dwarf2 specification, but data is still
15835 laid out as per normal fortran.
15836
15837 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15838 version checking. */
15839
15840 if (cu->language == language_fortran
15841 && cu->producer && strstr (cu->producer, "GNU F77"))
15842 {
15843 return DW_ORD_row_major;
15844 }
15845
15846 switch (cu->language_defn->la_array_ordering)
15847 {
15848 case array_column_major:
15849 return DW_ORD_col_major;
15850 case array_row_major:
15851 default:
15852 return DW_ORD_row_major;
15853 };
15854 }
15855
15856 /* Extract all information from a DW_TAG_set_type DIE and put it in
15857 the DIE's type field. */
15858
15859 static struct type *
15860 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15861 {
15862 struct type *domain_type, *set_type;
15863 struct attribute *attr;
15864
15865 domain_type = die_type (die, cu);
15866
15867 /* The die_type call above may have already set the type for this DIE. */
15868 set_type = get_die_type (die, cu);
15869 if (set_type)
15870 return set_type;
15871
15872 set_type = create_set_type (NULL, domain_type);
15873
15874 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15875 if (attr != nullptr)
15876 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15877
15878 maybe_set_alignment (cu, die, set_type);
15879
15880 return set_die_type (die, set_type, cu);
15881 }
15882
15883 /* A helper for read_common_block that creates a locexpr baton.
15884 SYM is the symbol which we are marking as computed.
15885 COMMON_DIE is the DIE for the common block.
15886 COMMON_LOC is the location expression attribute for the common
15887 block itself.
15888 MEMBER_LOC is the location expression attribute for the particular
15889 member of the common block that we are processing.
15890 CU is the CU from which the above come. */
15891
15892 static void
15893 mark_common_block_symbol_computed (struct symbol *sym,
15894 struct die_info *common_die,
15895 struct attribute *common_loc,
15896 struct attribute *member_loc,
15897 struct dwarf2_cu *cu)
15898 {
15899 struct dwarf2_per_objfile *dwarf2_per_objfile
15900 = cu->per_cu->dwarf2_per_objfile;
15901 struct objfile *objfile = dwarf2_per_objfile->objfile;
15902 struct dwarf2_locexpr_baton *baton;
15903 gdb_byte *ptr;
15904 unsigned int cu_off;
15905 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15906 LONGEST offset = 0;
15907
15908 gdb_assert (common_loc && member_loc);
15909 gdb_assert (common_loc->form_is_block ());
15910 gdb_assert (member_loc->form_is_block ()
15911 || member_loc->form_is_constant ());
15912
15913 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15914 baton->per_cu = cu->per_cu;
15915 gdb_assert (baton->per_cu);
15916
15917 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15918
15919 if (member_loc->form_is_constant ())
15920 {
15921 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15922 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15923 }
15924 else
15925 baton->size += DW_BLOCK (member_loc)->size;
15926
15927 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15928 baton->data = ptr;
15929
15930 *ptr++ = DW_OP_call4;
15931 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15932 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15933 ptr += 4;
15934
15935 if (member_loc->form_is_constant ())
15936 {
15937 *ptr++ = DW_OP_addr;
15938 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15939 ptr += cu->header.addr_size;
15940 }
15941 else
15942 {
15943 /* We have to copy the data here, because DW_OP_call4 will only
15944 use a DW_AT_location attribute. */
15945 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15946 ptr += DW_BLOCK (member_loc)->size;
15947 }
15948
15949 *ptr++ = DW_OP_plus;
15950 gdb_assert (ptr - baton->data == baton->size);
15951
15952 SYMBOL_LOCATION_BATON (sym) = baton;
15953 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15954 }
15955
15956 /* Create appropriate locally-scoped variables for all the
15957 DW_TAG_common_block entries. Also create a struct common_block
15958 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15959 is used to separate the common blocks name namespace from regular
15960 variable names. */
15961
15962 static void
15963 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15964 {
15965 struct attribute *attr;
15966
15967 attr = dwarf2_attr (die, DW_AT_location, cu);
15968 if (attr != nullptr)
15969 {
15970 /* Support the .debug_loc offsets. */
15971 if (attr->form_is_block ())
15972 {
15973 /* Ok. */
15974 }
15975 else if (attr->form_is_section_offset ())
15976 {
15977 dwarf2_complex_location_expr_complaint ();
15978 attr = NULL;
15979 }
15980 else
15981 {
15982 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15983 "common block member");
15984 attr = NULL;
15985 }
15986 }
15987
15988 if (die->child != NULL)
15989 {
15990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15991 struct die_info *child_die;
15992 size_t n_entries = 0, size;
15993 struct common_block *common_block;
15994 struct symbol *sym;
15995
15996 for (child_die = die->child;
15997 child_die && child_die->tag;
15998 child_die = sibling_die (child_die))
15999 ++n_entries;
16000
16001 size = (sizeof (struct common_block)
16002 + (n_entries - 1) * sizeof (struct symbol *));
16003 common_block
16004 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16005 size);
16006 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16007 common_block->n_entries = 0;
16008
16009 for (child_die = die->child;
16010 child_die && child_die->tag;
16011 child_die = sibling_die (child_die))
16012 {
16013 /* Create the symbol in the DW_TAG_common_block block in the current
16014 symbol scope. */
16015 sym = new_symbol (child_die, NULL, cu);
16016 if (sym != NULL)
16017 {
16018 struct attribute *member_loc;
16019
16020 common_block->contents[common_block->n_entries++] = sym;
16021
16022 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16023 cu);
16024 if (member_loc)
16025 {
16026 /* GDB has handled this for a long time, but it is
16027 not specified by DWARF. It seems to have been
16028 emitted by gfortran at least as recently as:
16029 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16030 complaint (_("Variable in common block has "
16031 "DW_AT_data_member_location "
16032 "- DIE at %s [in module %s]"),
16033 sect_offset_str (child_die->sect_off),
16034 objfile_name (objfile));
16035
16036 if (member_loc->form_is_section_offset ())
16037 dwarf2_complex_location_expr_complaint ();
16038 else if (member_loc->form_is_constant ()
16039 || member_loc->form_is_block ())
16040 {
16041 if (attr != nullptr)
16042 mark_common_block_symbol_computed (sym, die, attr,
16043 member_loc, cu);
16044 }
16045 else
16046 dwarf2_complex_location_expr_complaint ();
16047 }
16048 }
16049 }
16050
16051 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16052 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16053 }
16054 }
16055
16056 /* Create a type for a C++ namespace. */
16057
16058 static struct type *
16059 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16060 {
16061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16062 const char *previous_prefix, *name;
16063 int is_anonymous;
16064 struct type *type;
16065
16066 /* For extensions, reuse the type of the original namespace. */
16067 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16068 {
16069 struct die_info *ext_die;
16070 struct dwarf2_cu *ext_cu = cu;
16071
16072 ext_die = dwarf2_extension (die, &ext_cu);
16073 type = read_type_die (ext_die, ext_cu);
16074
16075 /* EXT_CU may not be the same as CU.
16076 Ensure TYPE is recorded with CU in die_type_hash. */
16077 return set_die_type (die, type, cu);
16078 }
16079
16080 name = namespace_name (die, &is_anonymous, cu);
16081
16082 /* Now build the name of the current namespace. */
16083
16084 previous_prefix = determine_prefix (die, cu);
16085 if (previous_prefix[0] != '\0')
16086 name = typename_concat (&objfile->objfile_obstack,
16087 previous_prefix, name, 0, cu);
16088
16089 /* Create the type. */
16090 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16091
16092 return set_die_type (die, type, cu);
16093 }
16094
16095 /* Read a namespace scope. */
16096
16097 static void
16098 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16099 {
16100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16101 int is_anonymous;
16102
16103 /* Add a symbol associated to this if we haven't seen the namespace
16104 before. Also, add a using directive if it's an anonymous
16105 namespace. */
16106
16107 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16108 {
16109 struct type *type;
16110
16111 type = read_type_die (die, cu);
16112 new_symbol (die, type, cu);
16113
16114 namespace_name (die, &is_anonymous, cu);
16115 if (is_anonymous)
16116 {
16117 const char *previous_prefix = determine_prefix (die, cu);
16118
16119 std::vector<const char *> excludes;
16120 add_using_directive (using_directives (cu),
16121 previous_prefix, TYPE_NAME (type), NULL,
16122 NULL, excludes, 0, &objfile->objfile_obstack);
16123 }
16124 }
16125
16126 if (die->child != NULL)
16127 {
16128 struct die_info *child_die = die->child;
16129
16130 while (child_die && child_die->tag)
16131 {
16132 process_die (child_die, cu);
16133 child_die = sibling_die (child_die);
16134 }
16135 }
16136 }
16137
16138 /* Read a Fortran module as type. This DIE can be only a declaration used for
16139 imported module. Still we need that type as local Fortran "use ... only"
16140 declaration imports depend on the created type in determine_prefix. */
16141
16142 static struct type *
16143 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16144 {
16145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16146 const char *module_name;
16147 struct type *type;
16148
16149 module_name = dwarf2_name (die, cu);
16150 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16151
16152 return set_die_type (die, type, cu);
16153 }
16154
16155 /* Read a Fortran module. */
16156
16157 static void
16158 read_module (struct die_info *die, struct dwarf2_cu *cu)
16159 {
16160 struct die_info *child_die = die->child;
16161 struct type *type;
16162
16163 type = read_type_die (die, cu);
16164 new_symbol (die, type, cu);
16165
16166 while (child_die && child_die->tag)
16167 {
16168 process_die (child_die, cu);
16169 child_die = sibling_die (child_die);
16170 }
16171 }
16172
16173 /* Return the name of the namespace represented by DIE. Set
16174 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16175 namespace. */
16176
16177 static const char *
16178 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16179 {
16180 struct die_info *current_die;
16181 const char *name = NULL;
16182
16183 /* Loop through the extensions until we find a name. */
16184
16185 for (current_die = die;
16186 current_die != NULL;
16187 current_die = dwarf2_extension (die, &cu))
16188 {
16189 /* We don't use dwarf2_name here so that we can detect the absence
16190 of a name -> anonymous namespace. */
16191 name = dwarf2_string_attr (die, DW_AT_name, cu);
16192
16193 if (name != NULL)
16194 break;
16195 }
16196
16197 /* Is it an anonymous namespace? */
16198
16199 *is_anonymous = (name == NULL);
16200 if (*is_anonymous)
16201 name = CP_ANONYMOUS_NAMESPACE_STR;
16202
16203 return name;
16204 }
16205
16206 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16207 the user defined type vector. */
16208
16209 static struct type *
16210 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16211 {
16212 struct gdbarch *gdbarch
16213 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16214 struct comp_unit_head *cu_header = &cu->header;
16215 struct type *type;
16216 struct attribute *attr_byte_size;
16217 struct attribute *attr_address_class;
16218 int byte_size, addr_class;
16219 struct type *target_type;
16220
16221 target_type = die_type (die, cu);
16222
16223 /* The die_type call above may have already set the type for this DIE. */
16224 type = get_die_type (die, cu);
16225 if (type)
16226 return type;
16227
16228 type = lookup_pointer_type (target_type);
16229
16230 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16231 if (attr_byte_size)
16232 byte_size = DW_UNSND (attr_byte_size);
16233 else
16234 byte_size = cu_header->addr_size;
16235
16236 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16237 if (attr_address_class)
16238 addr_class = DW_UNSND (attr_address_class);
16239 else
16240 addr_class = DW_ADDR_none;
16241
16242 ULONGEST alignment = get_alignment (cu, die);
16243
16244 /* If the pointer size, alignment, or address class is different
16245 than the default, create a type variant marked as such and set
16246 the length accordingly. */
16247 if (TYPE_LENGTH (type) != byte_size
16248 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16249 && alignment != TYPE_RAW_ALIGN (type))
16250 || addr_class != DW_ADDR_none)
16251 {
16252 if (gdbarch_address_class_type_flags_p (gdbarch))
16253 {
16254 int type_flags;
16255
16256 type_flags = gdbarch_address_class_type_flags
16257 (gdbarch, byte_size, addr_class);
16258 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16259 == 0);
16260 type = make_type_with_address_space (type, type_flags);
16261 }
16262 else if (TYPE_LENGTH (type) != byte_size)
16263 {
16264 complaint (_("invalid pointer size %d"), byte_size);
16265 }
16266 else if (TYPE_RAW_ALIGN (type) != alignment)
16267 {
16268 complaint (_("Invalid DW_AT_alignment"
16269 " - DIE at %s [in module %s]"),
16270 sect_offset_str (die->sect_off),
16271 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16272 }
16273 else
16274 {
16275 /* Should we also complain about unhandled address classes? */
16276 }
16277 }
16278
16279 TYPE_LENGTH (type) = byte_size;
16280 set_type_align (type, alignment);
16281 return set_die_type (die, type, cu);
16282 }
16283
16284 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16285 the user defined type vector. */
16286
16287 static struct type *
16288 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16289 {
16290 struct type *type;
16291 struct type *to_type;
16292 struct type *domain;
16293
16294 to_type = die_type (die, cu);
16295 domain = die_containing_type (die, cu);
16296
16297 /* The calls above may have already set the type for this DIE. */
16298 type = get_die_type (die, cu);
16299 if (type)
16300 return type;
16301
16302 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16303 type = lookup_methodptr_type (to_type);
16304 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16305 {
16306 struct type *new_type
16307 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16308
16309 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16310 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16311 TYPE_VARARGS (to_type));
16312 type = lookup_methodptr_type (new_type);
16313 }
16314 else
16315 type = lookup_memberptr_type (to_type, domain);
16316
16317 return set_die_type (die, type, cu);
16318 }
16319
16320 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16321 the user defined type vector. */
16322
16323 static struct type *
16324 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16325 enum type_code refcode)
16326 {
16327 struct comp_unit_head *cu_header = &cu->header;
16328 struct type *type, *target_type;
16329 struct attribute *attr;
16330
16331 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16332
16333 target_type = die_type (die, cu);
16334
16335 /* The die_type call above may have already set the type for this DIE. */
16336 type = get_die_type (die, cu);
16337 if (type)
16338 return type;
16339
16340 type = lookup_reference_type (target_type, refcode);
16341 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16342 if (attr != nullptr)
16343 {
16344 TYPE_LENGTH (type) = DW_UNSND (attr);
16345 }
16346 else
16347 {
16348 TYPE_LENGTH (type) = cu_header->addr_size;
16349 }
16350 maybe_set_alignment (cu, die, type);
16351 return set_die_type (die, type, cu);
16352 }
16353
16354 /* Add the given cv-qualifiers to the element type of the array. GCC
16355 outputs DWARF type qualifiers that apply to an array, not the
16356 element type. But GDB relies on the array element type to carry
16357 the cv-qualifiers. This mimics section 6.7.3 of the C99
16358 specification. */
16359
16360 static struct type *
16361 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16362 struct type *base_type, int cnst, int voltl)
16363 {
16364 struct type *el_type, *inner_array;
16365
16366 base_type = copy_type (base_type);
16367 inner_array = base_type;
16368
16369 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16370 {
16371 TYPE_TARGET_TYPE (inner_array) =
16372 copy_type (TYPE_TARGET_TYPE (inner_array));
16373 inner_array = TYPE_TARGET_TYPE (inner_array);
16374 }
16375
16376 el_type = TYPE_TARGET_TYPE (inner_array);
16377 cnst |= TYPE_CONST (el_type);
16378 voltl |= TYPE_VOLATILE (el_type);
16379 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16380
16381 return set_die_type (die, base_type, cu);
16382 }
16383
16384 static struct type *
16385 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16386 {
16387 struct type *base_type, *cv_type;
16388
16389 base_type = die_type (die, cu);
16390
16391 /* The die_type call above may have already set the type for this DIE. */
16392 cv_type = get_die_type (die, cu);
16393 if (cv_type)
16394 return cv_type;
16395
16396 /* In case the const qualifier is applied to an array type, the element type
16397 is so qualified, not the array type (section 6.7.3 of C99). */
16398 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16399 return add_array_cv_type (die, cu, base_type, 1, 0);
16400
16401 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16402 return set_die_type (die, cv_type, cu);
16403 }
16404
16405 static struct type *
16406 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16407 {
16408 struct type *base_type, *cv_type;
16409
16410 base_type = die_type (die, cu);
16411
16412 /* The die_type call above may have already set the type for this DIE. */
16413 cv_type = get_die_type (die, cu);
16414 if (cv_type)
16415 return cv_type;
16416
16417 /* In case the volatile qualifier is applied to an array type, the
16418 element type is so qualified, not the array type (section 6.7.3
16419 of C99). */
16420 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16421 return add_array_cv_type (die, cu, base_type, 0, 1);
16422
16423 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16424 return set_die_type (die, cv_type, cu);
16425 }
16426
16427 /* Handle DW_TAG_restrict_type. */
16428
16429 static struct type *
16430 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16431 {
16432 struct type *base_type, *cv_type;
16433
16434 base_type = die_type (die, cu);
16435
16436 /* The die_type call above may have already set the type for this DIE. */
16437 cv_type = get_die_type (die, cu);
16438 if (cv_type)
16439 return cv_type;
16440
16441 cv_type = make_restrict_type (base_type);
16442 return set_die_type (die, cv_type, cu);
16443 }
16444
16445 /* Handle DW_TAG_atomic_type. */
16446
16447 static struct type *
16448 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16449 {
16450 struct type *base_type, *cv_type;
16451
16452 base_type = die_type (die, cu);
16453
16454 /* The die_type call above may have already set the type for this DIE. */
16455 cv_type = get_die_type (die, cu);
16456 if (cv_type)
16457 return cv_type;
16458
16459 cv_type = make_atomic_type (base_type);
16460 return set_die_type (die, cv_type, cu);
16461 }
16462
16463 /* Extract all information from a DW_TAG_string_type DIE and add to
16464 the user defined type vector. It isn't really a user defined type,
16465 but it behaves like one, with other DIE's using an AT_user_def_type
16466 attribute to reference it. */
16467
16468 static struct type *
16469 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16470 {
16471 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16472 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16473 struct type *type, *range_type, *index_type, *char_type;
16474 struct attribute *attr;
16475 struct dynamic_prop prop;
16476 bool length_is_constant = true;
16477 LONGEST length;
16478
16479 /* There are a couple of places where bit sizes might be made use of
16480 when parsing a DW_TAG_string_type, however, no producer that we know
16481 of make use of these. Handling bit sizes that are a multiple of the
16482 byte size is easy enough, but what about other bit sizes? Lets deal
16483 with that problem when we have to. Warn about these attributes being
16484 unsupported, then parse the type and ignore them like we always
16485 have. */
16486 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16487 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16488 {
16489 static bool warning_printed = false;
16490 if (!warning_printed)
16491 {
16492 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16493 "currently supported on DW_TAG_string_type."));
16494 warning_printed = true;
16495 }
16496 }
16497
16498 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16499 if (attr != nullptr && !attr->form_is_constant ())
16500 {
16501 /* The string length describes the location at which the length of
16502 the string can be found. The size of the length field can be
16503 specified with one of the attributes below. */
16504 struct type *prop_type;
16505 struct attribute *len
16506 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16507 if (len == nullptr)
16508 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16509 if (len != nullptr && len->form_is_constant ())
16510 {
16511 /* Pass 0 as the default as we know this attribute is constant
16512 and the default value will not be returned. */
16513 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16514 prop_type = cu->per_cu->int_type (sz, true);
16515 }
16516 else
16517 {
16518 /* If the size is not specified then we assume it is the size of
16519 an address on this target. */
16520 prop_type = cu->per_cu->addr_sized_int_type (true);
16521 }
16522
16523 /* Convert the attribute into a dynamic property. */
16524 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16525 length = 1;
16526 else
16527 length_is_constant = false;
16528 }
16529 else if (attr != nullptr)
16530 {
16531 /* This DW_AT_string_length just contains the length with no
16532 indirection. There's no need to create a dynamic property in this
16533 case. Pass 0 for the default value as we know it will not be
16534 returned in this case. */
16535 length = dwarf2_get_attr_constant_value (attr, 0);
16536 }
16537 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16538 {
16539 /* We don't currently support non-constant byte sizes for strings. */
16540 length = dwarf2_get_attr_constant_value (attr, 1);
16541 }
16542 else
16543 {
16544 /* Use 1 as a fallback length if we have nothing else. */
16545 length = 1;
16546 }
16547
16548 index_type = objfile_type (objfile)->builtin_int;
16549 if (length_is_constant)
16550 range_type = create_static_range_type (NULL, index_type, 1, length);
16551 else
16552 {
16553 struct dynamic_prop low_bound;
16554
16555 low_bound.kind = PROP_CONST;
16556 low_bound.data.const_val = 1;
16557 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16558 }
16559 char_type = language_string_char_type (cu->language_defn, gdbarch);
16560 type = create_string_type (NULL, char_type, range_type);
16561
16562 return set_die_type (die, type, cu);
16563 }
16564
16565 /* Assuming that DIE corresponds to a function, returns nonzero
16566 if the function is prototyped. */
16567
16568 static int
16569 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16570 {
16571 struct attribute *attr;
16572
16573 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16574 if (attr && (DW_UNSND (attr) != 0))
16575 return 1;
16576
16577 /* The DWARF standard implies that the DW_AT_prototyped attribute
16578 is only meaningful for C, but the concept also extends to other
16579 languages that allow unprototyped functions (Eg: Objective C).
16580 For all other languages, assume that functions are always
16581 prototyped. */
16582 if (cu->language != language_c
16583 && cu->language != language_objc
16584 && cu->language != language_opencl)
16585 return 1;
16586
16587 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16588 prototyped and unprototyped functions; default to prototyped,
16589 since that is more common in modern code (and RealView warns
16590 about unprototyped functions). */
16591 if (producer_is_realview (cu->producer))
16592 return 1;
16593
16594 return 0;
16595 }
16596
16597 /* Handle DIES due to C code like:
16598
16599 struct foo
16600 {
16601 int (*funcp)(int a, long l);
16602 int b;
16603 };
16604
16605 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16606
16607 static struct type *
16608 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16609 {
16610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16611 struct type *type; /* Type that this function returns. */
16612 struct type *ftype; /* Function that returns above type. */
16613 struct attribute *attr;
16614
16615 type = die_type (die, cu);
16616
16617 /* The die_type call above may have already set the type for this DIE. */
16618 ftype = get_die_type (die, cu);
16619 if (ftype)
16620 return ftype;
16621
16622 ftype = lookup_function_type (type);
16623
16624 if (prototyped_function_p (die, cu))
16625 TYPE_PROTOTYPED (ftype) = 1;
16626
16627 /* Store the calling convention in the type if it's available in
16628 the subroutine die. Otherwise set the calling convention to
16629 the default value DW_CC_normal. */
16630 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16631 if (attr != nullptr
16632 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16633 TYPE_CALLING_CONVENTION (ftype)
16634 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16635 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16636 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16637 else
16638 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16639
16640 /* Record whether the function returns normally to its caller or not
16641 if the DWARF producer set that information. */
16642 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16643 if (attr && (DW_UNSND (attr) != 0))
16644 TYPE_NO_RETURN (ftype) = 1;
16645
16646 /* We need to add the subroutine type to the die immediately so
16647 we don't infinitely recurse when dealing with parameters
16648 declared as the same subroutine type. */
16649 set_die_type (die, ftype, cu);
16650
16651 if (die->child != NULL)
16652 {
16653 struct type *void_type = objfile_type (objfile)->builtin_void;
16654 struct die_info *child_die;
16655 int nparams, iparams;
16656
16657 /* Count the number of parameters.
16658 FIXME: GDB currently ignores vararg functions, but knows about
16659 vararg member functions. */
16660 nparams = 0;
16661 child_die = die->child;
16662 while (child_die && child_die->tag)
16663 {
16664 if (child_die->tag == DW_TAG_formal_parameter)
16665 nparams++;
16666 else if (child_die->tag == DW_TAG_unspecified_parameters)
16667 TYPE_VARARGS (ftype) = 1;
16668 child_die = sibling_die (child_die);
16669 }
16670
16671 /* Allocate storage for parameters and fill them in. */
16672 TYPE_NFIELDS (ftype) = nparams;
16673 TYPE_FIELDS (ftype) = (struct field *)
16674 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16675
16676 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16677 even if we error out during the parameters reading below. */
16678 for (iparams = 0; iparams < nparams; iparams++)
16679 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16680
16681 iparams = 0;
16682 child_die = die->child;
16683 while (child_die && child_die->tag)
16684 {
16685 if (child_die->tag == DW_TAG_formal_parameter)
16686 {
16687 struct type *arg_type;
16688
16689 /* DWARF version 2 has no clean way to discern C++
16690 static and non-static member functions. G++ helps
16691 GDB by marking the first parameter for non-static
16692 member functions (which is the this pointer) as
16693 artificial. We pass this information to
16694 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16695
16696 DWARF version 3 added DW_AT_object_pointer, which GCC
16697 4.5 does not yet generate. */
16698 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16699 if (attr != nullptr)
16700 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16701 else
16702 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16703 arg_type = die_type (child_die, cu);
16704
16705 /* RealView does not mark THIS as const, which the testsuite
16706 expects. GCC marks THIS as const in method definitions,
16707 but not in the class specifications (GCC PR 43053). */
16708 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16709 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16710 {
16711 int is_this = 0;
16712 struct dwarf2_cu *arg_cu = cu;
16713 const char *name = dwarf2_name (child_die, cu);
16714
16715 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16716 if (attr != nullptr)
16717 {
16718 /* If the compiler emits this, use it. */
16719 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16720 is_this = 1;
16721 }
16722 else if (name && strcmp (name, "this") == 0)
16723 /* Function definitions will have the argument names. */
16724 is_this = 1;
16725 else if (name == NULL && iparams == 0)
16726 /* Declarations may not have the names, so like
16727 elsewhere in GDB, assume an artificial first
16728 argument is "this". */
16729 is_this = 1;
16730
16731 if (is_this)
16732 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16733 arg_type, 0);
16734 }
16735
16736 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16737 iparams++;
16738 }
16739 child_die = sibling_die (child_die);
16740 }
16741 }
16742
16743 return ftype;
16744 }
16745
16746 static struct type *
16747 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16748 {
16749 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16750 const char *name = NULL;
16751 struct type *this_type, *target_type;
16752
16753 name = dwarf2_full_name (NULL, die, cu);
16754 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16755 TYPE_TARGET_STUB (this_type) = 1;
16756 set_die_type (die, this_type, cu);
16757 target_type = die_type (die, cu);
16758 if (target_type != this_type)
16759 TYPE_TARGET_TYPE (this_type) = target_type;
16760 else
16761 {
16762 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16763 spec and cause infinite loops in GDB. */
16764 complaint (_("Self-referential DW_TAG_typedef "
16765 "- DIE at %s [in module %s]"),
16766 sect_offset_str (die->sect_off), objfile_name (objfile));
16767 TYPE_TARGET_TYPE (this_type) = NULL;
16768 }
16769 if (name == NULL)
16770 {
16771 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16772 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16773 Handle these by just returning the target type, rather than
16774 constructing an anonymous typedef type and trying to handle this
16775 elsewhere. */
16776 set_die_type (die, target_type, cu);
16777 return target_type;
16778 }
16779 return this_type;
16780 }
16781
16782 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16783 (which may be different from NAME) to the architecture back-end to allow
16784 it to guess the correct format if necessary. */
16785
16786 static struct type *
16787 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16788 const char *name_hint, enum bfd_endian byte_order)
16789 {
16790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16791 const struct floatformat **format;
16792 struct type *type;
16793
16794 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16795 if (format)
16796 type = init_float_type (objfile, bits, name, format, byte_order);
16797 else
16798 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16799
16800 return type;
16801 }
16802
16803 /* Allocate an integer type of size BITS and name NAME. */
16804
16805 static struct type *
16806 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16807 int bits, int unsigned_p, const char *name)
16808 {
16809 struct type *type;
16810
16811 /* Versions of Intel's C Compiler generate an integer type called "void"
16812 instead of using DW_TAG_unspecified_type. This has been seen on
16813 at least versions 14, 17, and 18. */
16814 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16815 && strcmp (name, "void") == 0)
16816 type = objfile_type (objfile)->builtin_void;
16817 else
16818 type = init_integer_type (objfile, bits, unsigned_p, name);
16819
16820 return type;
16821 }
16822
16823 /* Initialise and return a floating point type of size BITS suitable for
16824 use as a component of a complex number. The NAME_HINT is passed through
16825 when initialising the floating point type and is the name of the complex
16826 type.
16827
16828 As DWARF doesn't currently provide an explicit name for the components
16829 of a complex number, but it can be helpful to have these components
16830 named, we try to select a suitable name based on the size of the
16831 component. */
16832 static struct type *
16833 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16834 struct objfile *objfile,
16835 int bits, const char *name_hint,
16836 enum bfd_endian byte_order)
16837 {
16838 gdbarch *gdbarch = get_objfile_arch (objfile);
16839 struct type *tt = nullptr;
16840
16841 /* Try to find a suitable floating point builtin type of size BITS.
16842 We're going to use the name of this type as the name for the complex
16843 target type that we are about to create. */
16844 switch (cu->language)
16845 {
16846 case language_fortran:
16847 switch (bits)
16848 {
16849 case 32:
16850 tt = builtin_f_type (gdbarch)->builtin_real;
16851 break;
16852 case 64:
16853 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16854 break;
16855 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16856 case 128:
16857 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16858 break;
16859 }
16860 break;
16861 default:
16862 switch (bits)
16863 {
16864 case 32:
16865 tt = builtin_type (gdbarch)->builtin_float;
16866 break;
16867 case 64:
16868 tt = builtin_type (gdbarch)->builtin_double;
16869 break;
16870 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16871 case 128:
16872 tt = builtin_type (gdbarch)->builtin_long_double;
16873 break;
16874 }
16875 break;
16876 }
16877
16878 /* If the type we found doesn't match the size we were looking for, then
16879 pretend we didn't find a type at all, the complex target type we
16880 create will then be nameless. */
16881 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16882 tt = nullptr;
16883
16884 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16885 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16886 }
16887
16888 /* Find a representation of a given base type and install
16889 it in the TYPE field of the die. */
16890
16891 static struct type *
16892 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16893 {
16894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16895 struct type *type;
16896 struct attribute *attr;
16897 int encoding = 0, bits = 0;
16898 const char *name;
16899 gdbarch *arch;
16900
16901 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16902 if (attr != nullptr)
16903 encoding = DW_UNSND (attr);
16904 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16905 if (attr != nullptr)
16906 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16907 name = dwarf2_name (die, cu);
16908 if (!name)
16909 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16910
16911 arch = get_objfile_arch (objfile);
16912 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16913
16914 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16915 if (attr)
16916 {
16917 int endianity = DW_UNSND (attr);
16918
16919 switch (endianity)
16920 {
16921 case DW_END_big:
16922 byte_order = BFD_ENDIAN_BIG;
16923 break;
16924 case DW_END_little:
16925 byte_order = BFD_ENDIAN_LITTLE;
16926 break;
16927 default:
16928 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16929 break;
16930 }
16931 }
16932
16933 switch (encoding)
16934 {
16935 case DW_ATE_address:
16936 /* Turn DW_ATE_address into a void * pointer. */
16937 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16938 type = init_pointer_type (objfile, bits, name, type);
16939 break;
16940 case DW_ATE_boolean:
16941 type = init_boolean_type (objfile, bits, 1, name);
16942 break;
16943 case DW_ATE_complex_float:
16944 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16945 byte_order);
16946 type = init_complex_type (objfile, name, type);
16947 break;
16948 case DW_ATE_decimal_float:
16949 type = init_decfloat_type (objfile, bits, name);
16950 break;
16951 case DW_ATE_float:
16952 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16953 break;
16954 case DW_ATE_signed:
16955 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16956 break;
16957 case DW_ATE_unsigned:
16958 if (cu->language == language_fortran
16959 && name
16960 && startswith (name, "character("))
16961 type = init_character_type (objfile, bits, 1, name);
16962 else
16963 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16964 break;
16965 case DW_ATE_signed_char:
16966 if (cu->language == language_ada || cu->language == language_m2
16967 || cu->language == language_pascal
16968 || cu->language == language_fortran)
16969 type = init_character_type (objfile, bits, 0, name);
16970 else
16971 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16972 break;
16973 case DW_ATE_unsigned_char:
16974 if (cu->language == language_ada || cu->language == language_m2
16975 || cu->language == language_pascal
16976 || cu->language == language_fortran
16977 || cu->language == language_rust)
16978 type = init_character_type (objfile, bits, 1, name);
16979 else
16980 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16981 break;
16982 case DW_ATE_UTF:
16983 {
16984 if (bits == 16)
16985 type = builtin_type (arch)->builtin_char16;
16986 else if (bits == 32)
16987 type = builtin_type (arch)->builtin_char32;
16988 else
16989 {
16990 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16991 bits);
16992 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16993 }
16994 return set_die_type (die, type, cu);
16995 }
16996 break;
16997
16998 default:
16999 complaint (_("unsupported DW_AT_encoding: '%s'"),
17000 dwarf_type_encoding_name (encoding));
17001 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17002 break;
17003 }
17004
17005 if (name && strcmp (name, "char") == 0)
17006 TYPE_NOSIGN (type) = 1;
17007
17008 maybe_set_alignment (cu, die, type);
17009
17010 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17011
17012 return set_die_type (die, type, cu);
17013 }
17014
17015 /* Parse dwarf attribute if it's a block, reference or constant and put the
17016 resulting value of the attribute into struct bound_prop.
17017 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17018
17019 static int
17020 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17021 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17022 struct type *default_type)
17023 {
17024 struct dwarf2_property_baton *baton;
17025 struct obstack *obstack
17026 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17027
17028 gdb_assert (default_type != NULL);
17029
17030 if (attr == NULL || prop == NULL)
17031 return 0;
17032
17033 if (attr->form_is_block ())
17034 {
17035 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17036 baton->property_type = default_type;
17037 baton->locexpr.per_cu = cu->per_cu;
17038 baton->locexpr.size = DW_BLOCK (attr)->size;
17039 baton->locexpr.data = DW_BLOCK (attr)->data;
17040 switch (attr->name)
17041 {
17042 case DW_AT_string_length:
17043 baton->locexpr.is_reference = true;
17044 break;
17045 default:
17046 baton->locexpr.is_reference = false;
17047 break;
17048 }
17049 prop->data.baton = baton;
17050 prop->kind = PROP_LOCEXPR;
17051 gdb_assert (prop->data.baton != NULL);
17052 }
17053 else if (attr->form_is_ref ())
17054 {
17055 struct dwarf2_cu *target_cu = cu;
17056 struct die_info *target_die;
17057 struct attribute *target_attr;
17058
17059 target_die = follow_die_ref (die, attr, &target_cu);
17060 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17061 if (target_attr == NULL)
17062 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17063 target_cu);
17064 if (target_attr == NULL)
17065 return 0;
17066
17067 switch (target_attr->name)
17068 {
17069 case DW_AT_location:
17070 if (target_attr->form_is_section_offset ())
17071 {
17072 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17073 baton->property_type = die_type (target_die, target_cu);
17074 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17075 prop->data.baton = baton;
17076 prop->kind = PROP_LOCLIST;
17077 gdb_assert (prop->data.baton != NULL);
17078 }
17079 else if (target_attr->form_is_block ())
17080 {
17081 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17082 baton->property_type = die_type (target_die, target_cu);
17083 baton->locexpr.per_cu = cu->per_cu;
17084 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17085 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17086 baton->locexpr.is_reference = true;
17087 prop->data.baton = baton;
17088 prop->kind = PROP_LOCEXPR;
17089 gdb_assert (prop->data.baton != NULL);
17090 }
17091 else
17092 {
17093 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17094 "dynamic property");
17095 return 0;
17096 }
17097 break;
17098 case DW_AT_data_member_location:
17099 {
17100 LONGEST offset;
17101
17102 if (!handle_data_member_location (target_die, target_cu,
17103 &offset))
17104 return 0;
17105
17106 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17107 baton->property_type = read_type_die (target_die->parent,
17108 target_cu);
17109 baton->offset_info.offset = offset;
17110 baton->offset_info.type = die_type (target_die, target_cu);
17111 prop->data.baton = baton;
17112 prop->kind = PROP_ADDR_OFFSET;
17113 break;
17114 }
17115 }
17116 }
17117 else if (attr->form_is_constant ())
17118 {
17119 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17120 prop->kind = PROP_CONST;
17121 }
17122 else
17123 {
17124 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17125 dwarf2_name (die, cu));
17126 return 0;
17127 }
17128
17129 return 1;
17130 }
17131
17132 /* See read.h. */
17133
17134 struct type *
17135 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17136 {
17137 struct objfile *objfile = dwarf2_per_objfile->objfile;
17138 struct type *int_type;
17139
17140 /* Helper macro to examine the various builtin types. */
17141 #define TRY_TYPE(F) \
17142 int_type = (unsigned_p \
17143 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17144 : objfile_type (objfile)->builtin_ ## F); \
17145 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17146 return int_type
17147
17148 TRY_TYPE (char);
17149 TRY_TYPE (short);
17150 TRY_TYPE (int);
17151 TRY_TYPE (long);
17152 TRY_TYPE (long_long);
17153
17154 #undef TRY_TYPE
17155
17156 gdb_assert_not_reached ("unable to find suitable integer type");
17157 }
17158
17159 /* See read.h. */
17160
17161 struct type *
17162 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17163 {
17164 int addr_size = this->addr_size ();
17165 return int_type (addr_size, unsigned_p);
17166 }
17167
17168 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17169 present (which is valid) then compute the default type based on the
17170 compilation units address size. */
17171
17172 static struct type *
17173 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17174 {
17175 struct type *index_type = die_type (die, cu);
17176
17177 /* Dwarf-2 specifications explicitly allows to create subrange types
17178 without specifying a base type.
17179 In that case, the base type must be set to the type of
17180 the lower bound, upper bound or count, in that order, if any of these
17181 three attributes references an object that has a type.
17182 If no base type is found, the Dwarf-2 specifications say that
17183 a signed integer type of size equal to the size of an address should
17184 be used.
17185 For the following C code: `extern char gdb_int [];'
17186 GCC produces an empty range DIE.
17187 FIXME: muller/2010-05-28: Possible references to object for low bound,
17188 high bound or count are not yet handled by this code. */
17189 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17190 index_type = cu->per_cu->addr_sized_int_type (false);
17191
17192 return index_type;
17193 }
17194
17195 /* Read the given DW_AT_subrange DIE. */
17196
17197 static struct type *
17198 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17199 {
17200 struct type *base_type, *orig_base_type;
17201 struct type *range_type;
17202 struct attribute *attr;
17203 struct dynamic_prop low, high;
17204 int low_default_is_valid;
17205 int high_bound_is_count = 0;
17206 const char *name;
17207 ULONGEST negative_mask;
17208
17209 orig_base_type = read_subrange_index_type (die, cu);
17210
17211 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17212 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17213 creating the range type, but we use the result of check_typedef
17214 when examining properties of the type. */
17215 base_type = check_typedef (orig_base_type);
17216
17217 /* The die_type call above may have already set the type for this DIE. */
17218 range_type = get_die_type (die, cu);
17219 if (range_type)
17220 return range_type;
17221
17222 low.kind = PROP_CONST;
17223 high.kind = PROP_CONST;
17224 high.data.const_val = 0;
17225
17226 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17227 omitting DW_AT_lower_bound. */
17228 switch (cu->language)
17229 {
17230 case language_c:
17231 case language_cplus:
17232 low.data.const_val = 0;
17233 low_default_is_valid = 1;
17234 break;
17235 case language_fortran:
17236 low.data.const_val = 1;
17237 low_default_is_valid = 1;
17238 break;
17239 case language_d:
17240 case language_objc:
17241 case language_rust:
17242 low.data.const_val = 0;
17243 low_default_is_valid = (cu->header.version >= 4);
17244 break;
17245 case language_ada:
17246 case language_m2:
17247 case language_pascal:
17248 low.data.const_val = 1;
17249 low_default_is_valid = (cu->header.version >= 4);
17250 break;
17251 default:
17252 low.data.const_val = 0;
17253 low_default_is_valid = 0;
17254 break;
17255 }
17256
17257 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17258 if (attr != nullptr)
17259 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17260 else if (!low_default_is_valid)
17261 complaint (_("Missing DW_AT_lower_bound "
17262 "- DIE at %s [in module %s]"),
17263 sect_offset_str (die->sect_off),
17264 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17265
17266 struct attribute *attr_ub, *attr_count;
17267 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17268 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17269 {
17270 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17271 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17272 {
17273 /* If bounds are constant do the final calculation here. */
17274 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17275 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17276 else
17277 high_bound_is_count = 1;
17278 }
17279 else
17280 {
17281 if (attr_ub != NULL)
17282 complaint (_("Unresolved DW_AT_upper_bound "
17283 "- DIE at %s [in module %s]"),
17284 sect_offset_str (die->sect_off),
17285 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17286 if (attr_count != NULL)
17287 complaint (_("Unresolved DW_AT_count "
17288 "- DIE at %s [in module %s]"),
17289 sect_offset_str (die->sect_off),
17290 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17291 }
17292 }
17293
17294 LONGEST bias = 0;
17295 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17296 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17297 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17298
17299 /* Normally, the DWARF producers are expected to use a signed
17300 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17301 But this is unfortunately not always the case, as witnessed
17302 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17303 is used instead. To work around that ambiguity, we treat
17304 the bounds as signed, and thus sign-extend their values, when
17305 the base type is signed. */
17306 negative_mask =
17307 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17308 if (low.kind == PROP_CONST
17309 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17310 low.data.const_val |= negative_mask;
17311 if (high.kind == PROP_CONST
17312 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17313 high.data.const_val |= negative_mask;
17314
17315 /* Check for bit and byte strides. */
17316 struct dynamic_prop byte_stride_prop;
17317 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17318 if (attr_byte_stride != nullptr)
17319 {
17320 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17321 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17322 prop_type);
17323 }
17324
17325 struct dynamic_prop bit_stride_prop;
17326 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17327 if (attr_bit_stride != nullptr)
17328 {
17329 /* It only makes sense to have either a bit or byte stride. */
17330 if (attr_byte_stride != nullptr)
17331 {
17332 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17333 "- DIE at %s [in module %s]"),
17334 sect_offset_str (die->sect_off),
17335 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17336 attr_bit_stride = nullptr;
17337 }
17338 else
17339 {
17340 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17341 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17342 prop_type);
17343 }
17344 }
17345
17346 if (attr_byte_stride != nullptr
17347 || attr_bit_stride != nullptr)
17348 {
17349 bool byte_stride_p = (attr_byte_stride != nullptr);
17350 struct dynamic_prop *stride
17351 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17352
17353 range_type
17354 = create_range_type_with_stride (NULL, orig_base_type, &low,
17355 &high, bias, stride, byte_stride_p);
17356 }
17357 else
17358 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17359
17360 if (high_bound_is_count)
17361 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17362
17363 /* Ada expects an empty array on no boundary attributes. */
17364 if (attr == NULL && cu->language != language_ada)
17365 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17366
17367 name = dwarf2_name (die, cu);
17368 if (name)
17369 TYPE_NAME (range_type) = name;
17370
17371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17372 if (attr != nullptr)
17373 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17374
17375 maybe_set_alignment (cu, die, range_type);
17376
17377 set_die_type (die, range_type, cu);
17378
17379 /* set_die_type should be already done. */
17380 set_descriptive_type (range_type, die, cu);
17381
17382 return range_type;
17383 }
17384
17385 static struct type *
17386 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17387 {
17388 struct type *type;
17389
17390 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17391 NULL);
17392 TYPE_NAME (type) = dwarf2_name (die, cu);
17393
17394 /* In Ada, an unspecified type is typically used when the description
17395 of the type is deferred to a different unit. When encountering
17396 such a type, we treat it as a stub, and try to resolve it later on,
17397 when needed. */
17398 if (cu->language == language_ada)
17399 TYPE_STUB (type) = 1;
17400
17401 return set_die_type (die, type, cu);
17402 }
17403
17404 /* Read a single die and all its descendents. Set the die's sibling
17405 field to NULL; set other fields in the die correctly, and set all
17406 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17407 location of the info_ptr after reading all of those dies. PARENT
17408 is the parent of the die in question. */
17409
17410 static struct die_info *
17411 read_die_and_children (const struct die_reader_specs *reader,
17412 const gdb_byte *info_ptr,
17413 const gdb_byte **new_info_ptr,
17414 struct die_info *parent)
17415 {
17416 struct die_info *die;
17417 const gdb_byte *cur_ptr;
17418
17419 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17420 if (die == NULL)
17421 {
17422 *new_info_ptr = cur_ptr;
17423 return NULL;
17424 }
17425 store_in_ref_table (die, reader->cu);
17426
17427 if (die->has_children)
17428 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17429 else
17430 {
17431 die->child = NULL;
17432 *new_info_ptr = cur_ptr;
17433 }
17434
17435 die->sibling = NULL;
17436 die->parent = parent;
17437 return die;
17438 }
17439
17440 /* Read a die, all of its descendents, and all of its siblings; set
17441 all of the fields of all of the dies correctly. Arguments are as
17442 in read_die_and_children. */
17443
17444 static struct die_info *
17445 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17446 const gdb_byte *info_ptr,
17447 const gdb_byte **new_info_ptr,
17448 struct die_info *parent)
17449 {
17450 struct die_info *first_die, *last_sibling;
17451 const gdb_byte *cur_ptr;
17452
17453 cur_ptr = info_ptr;
17454 first_die = last_sibling = NULL;
17455
17456 while (1)
17457 {
17458 struct die_info *die
17459 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17460
17461 if (die == NULL)
17462 {
17463 *new_info_ptr = cur_ptr;
17464 return first_die;
17465 }
17466
17467 if (!first_die)
17468 first_die = die;
17469 else
17470 last_sibling->sibling = die;
17471
17472 last_sibling = die;
17473 }
17474 }
17475
17476 /* Read a die, all of its descendents, and all of its siblings; set
17477 all of the fields of all of the dies correctly. Arguments are as
17478 in read_die_and_children.
17479 This the main entry point for reading a DIE and all its children. */
17480
17481 static struct die_info *
17482 read_die_and_siblings (const struct die_reader_specs *reader,
17483 const gdb_byte *info_ptr,
17484 const gdb_byte **new_info_ptr,
17485 struct die_info *parent)
17486 {
17487 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17488 new_info_ptr, parent);
17489
17490 if (dwarf_die_debug)
17491 {
17492 fprintf_unfiltered (gdb_stdlog,
17493 "Read die from %s@0x%x of %s:\n",
17494 reader->die_section->get_name (),
17495 (unsigned) (info_ptr - reader->die_section->buffer),
17496 bfd_get_filename (reader->abfd));
17497 dump_die (die, dwarf_die_debug);
17498 }
17499
17500 return die;
17501 }
17502
17503 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17504 attributes.
17505 The caller is responsible for filling in the extra attributes
17506 and updating (*DIEP)->num_attrs.
17507 Set DIEP to point to a newly allocated die with its information,
17508 except for its child, sibling, and parent fields. */
17509
17510 static const gdb_byte *
17511 read_full_die_1 (const struct die_reader_specs *reader,
17512 struct die_info **diep, const gdb_byte *info_ptr,
17513 int num_extra_attrs)
17514 {
17515 unsigned int abbrev_number, bytes_read, i;
17516 struct abbrev_info *abbrev;
17517 struct die_info *die;
17518 struct dwarf2_cu *cu = reader->cu;
17519 bfd *abfd = reader->abfd;
17520
17521 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17522 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17523 info_ptr += bytes_read;
17524 if (!abbrev_number)
17525 {
17526 *diep = NULL;
17527 return info_ptr;
17528 }
17529
17530 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17531 if (!abbrev)
17532 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17533 abbrev_number,
17534 bfd_get_filename (abfd));
17535
17536 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17537 die->sect_off = sect_off;
17538 die->tag = abbrev->tag;
17539 die->abbrev = abbrev_number;
17540 die->has_children = abbrev->has_children;
17541
17542 /* Make the result usable.
17543 The caller needs to update num_attrs after adding the extra
17544 attributes. */
17545 die->num_attrs = abbrev->num_attrs;
17546
17547 std::vector<int> indexes_that_need_reprocess;
17548 for (i = 0; i < abbrev->num_attrs; ++i)
17549 {
17550 bool need_reprocess;
17551 info_ptr =
17552 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17553 info_ptr, &need_reprocess);
17554 if (need_reprocess)
17555 indexes_that_need_reprocess.push_back (i);
17556 }
17557
17558 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17559 if (attr != nullptr)
17560 cu->str_offsets_base = DW_UNSND (attr);
17561
17562 auto maybe_addr_base = lookup_addr_base(die);
17563 if (maybe_addr_base.has_value ())
17564 cu->addr_base = *maybe_addr_base;
17565 for (int index : indexes_that_need_reprocess)
17566 read_attribute_reprocess (reader, &die->attrs[index]);
17567 *diep = die;
17568 return info_ptr;
17569 }
17570
17571 /* Read a die and all its attributes.
17572 Set DIEP to point to a newly allocated die with its information,
17573 except for its child, sibling, and parent fields. */
17574
17575 static const gdb_byte *
17576 read_full_die (const struct die_reader_specs *reader,
17577 struct die_info **diep, const gdb_byte *info_ptr)
17578 {
17579 const gdb_byte *result;
17580
17581 result = read_full_die_1 (reader, diep, info_ptr, 0);
17582
17583 if (dwarf_die_debug)
17584 {
17585 fprintf_unfiltered (gdb_stdlog,
17586 "Read die from %s@0x%x of %s:\n",
17587 reader->die_section->get_name (),
17588 (unsigned) (info_ptr - reader->die_section->buffer),
17589 bfd_get_filename (reader->abfd));
17590 dump_die (*diep, dwarf_die_debug);
17591 }
17592
17593 return result;
17594 }
17595 \f
17596
17597 /* Returns nonzero if TAG represents a type that we might generate a partial
17598 symbol for. */
17599
17600 static int
17601 is_type_tag_for_partial (int tag)
17602 {
17603 switch (tag)
17604 {
17605 #if 0
17606 /* Some types that would be reasonable to generate partial symbols for,
17607 that we don't at present. */
17608 case DW_TAG_array_type:
17609 case DW_TAG_file_type:
17610 case DW_TAG_ptr_to_member_type:
17611 case DW_TAG_set_type:
17612 case DW_TAG_string_type:
17613 case DW_TAG_subroutine_type:
17614 #endif
17615 case DW_TAG_base_type:
17616 case DW_TAG_class_type:
17617 case DW_TAG_interface_type:
17618 case DW_TAG_enumeration_type:
17619 case DW_TAG_structure_type:
17620 case DW_TAG_subrange_type:
17621 case DW_TAG_typedef:
17622 case DW_TAG_union_type:
17623 return 1;
17624 default:
17625 return 0;
17626 }
17627 }
17628
17629 /* Load all DIEs that are interesting for partial symbols into memory. */
17630
17631 static struct partial_die_info *
17632 load_partial_dies (const struct die_reader_specs *reader,
17633 const gdb_byte *info_ptr, int building_psymtab)
17634 {
17635 struct dwarf2_cu *cu = reader->cu;
17636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17637 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17638 unsigned int bytes_read;
17639 unsigned int load_all = 0;
17640 int nesting_level = 1;
17641
17642 parent_die = NULL;
17643 last_die = NULL;
17644
17645 gdb_assert (cu->per_cu != NULL);
17646 if (cu->per_cu->load_all_dies)
17647 load_all = 1;
17648
17649 cu->partial_dies
17650 = htab_create_alloc_ex (cu->header.length / 12,
17651 partial_die_hash,
17652 partial_die_eq,
17653 NULL,
17654 &cu->comp_unit_obstack,
17655 hashtab_obstack_allocate,
17656 dummy_obstack_deallocate);
17657
17658 while (1)
17659 {
17660 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17661
17662 /* A NULL abbrev means the end of a series of children. */
17663 if (abbrev == NULL)
17664 {
17665 if (--nesting_level == 0)
17666 return first_die;
17667
17668 info_ptr += bytes_read;
17669 last_die = parent_die;
17670 parent_die = parent_die->die_parent;
17671 continue;
17672 }
17673
17674 /* Check for template arguments. We never save these; if
17675 they're seen, we just mark the parent, and go on our way. */
17676 if (parent_die != NULL
17677 && cu->language == language_cplus
17678 && (abbrev->tag == DW_TAG_template_type_param
17679 || abbrev->tag == DW_TAG_template_value_param))
17680 {
17681 parent_die->has_template_arguments = 1;
17682
17683 if (!load_all)
17684 {
17685 /* We don't need a partial DIE for the template argument. */
17686 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17687 continue;
17688 }
17689 }
17690
17691 /* We only recurse into c++ subprograms looking for template arguments.
17692 Skip their other children. */
17693 if (!load_all
17694 && cu->language == language_cplus
17695 && parent_die != NULL
17696 && parent_die->tag == DW_TAG_subprogram)
17697 {
17698 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17699 continue;
17700 }
17701
17702 /* Check whether this DIE is interesting enough to save. Normally
17703 we would not be interested in members here, but there may be
17704 later variables referencing them via DW_AT_specification (for
17705 static members). */
17706 if (!load_all
17707 && !is_type_tag_for_partial (abbrev->tag)
17708 && abbrev->tag != DW_TAG_constant
17709 && abbrev->tag != DW_TAG_enumerator
17710 && abbrev->tag != DW_TAG_subprogram
17711 && abbrev->tag != DW_TAG_inlined_subroutine
17712 && abbrev->tag != DW_TAG_lexical_block
17713 && abbrev->tag != DW_TAG_variable
17714 && abbrev->tag != DW_TAG_namespace
17715 && abbrev->tag != DW_TAG_module
17716 && abbrev->tag != DW_TAG_member
17717 && abbrev->tag != DW_TAG_imported_unit
17718 && abbrev->tag != DW_TAG_imported_declaration)
17719 {
17720 /* Otherwise we skip to the next sibling, if any. */
17721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17722 continue;
17723 }
17724
17725 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17726 abbrev);
17727
17728 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17729
17730 /* This two-pass algorithm for processing partial symbols has a
17731 high cost in cache pressure. Thus, handle some simple cases
17732 here which cover the majority of C partial symbols. DIEs
17733 which neither have specification tags in them, nor could have
17734 specification tags elsewhere pointing at them, can simply be
17735 processed and discarded.
17736
17737 This segment is also optional; scan_partial_symbols and
17738 add_partial_symbol will handle these DIEs if we chain
17739 them in normally. When compilers which do not emit large
17740 quantities of duplicate debug information are more common,
17741 this code can probably be removed. */
17742
17743 /* Any complete simple types at the top level (pretty much all
17744 of them, for a language without namespaces), can be processed
17745 directly. */
17746 if (parent_die == NULL
17747 && pdi.has_specification == 0
17748 && pdi.is_declaration == 0
17749 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17750 || pdi.tag == DW_TAG_base_type
17751 || pdi.tag == DW_TAG_subrange_type))
17752 {
17753 if (building_psymtab && pdi.name != NULL)
17754 add_psymbol_to_list (pdi.name, false,
17755 VAR_DOMAIN, LOC_TYPEDEF, -1,
17756 psymbol_placement::STATIC,
17757 0, cu->language, objfile);
17758 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17759 continue;
17760 }
17761
17762 /* The exception for DW_TAG_typedef with has_children above is
17763 a workaround of GCC PR debug/47510. In the case of this complaint
17764 type_name_or_error will error on such types later.
17765
17766 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17767 it could not find the child DIEs referenced later, this is checked
17768 above. In correct DWARF DW_TAG_typedef should have no children. */
17769
17770 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17771 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17772 "- DIE at %s [in module %s]"),
17773 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17774
17775 /* If we're at the second level, and we're an enumerator, and
17776 our parent has no specification (meaning possibly lives in a
17777 namespace elsewhere), then we can add the partial symbol now
17778 instead of queueing it. */
17779 if (pdi.tag == DW_TAG_enumerator
17780 && parent_die != NULL
17781 && parent_die->die_parent == NULL
17782 && parent_die->tag == DW_TAG_enumeration_type
17783 && parent_die->has_specification == 0)
17784 {
17785 if (pdi.name == NULL)
17786 complaint (_("malformed enumerator DIE ignored"));
17787 else if (building_psymtab)
17788 add_psymbol_to_list (pdi.name, false,
17789 VAR_DOMAIN, LOC_CONST, -1,
17790 cu->language == language_cplus
17791 ? psymbol_placement::GLOBAL
17792 : psymbol_placement::STATIC,
17793 0, cu->language, objfile);
17794
17795 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17796 continue;
17797 }
17798
17799 struct partial_die_info *part_die
17800 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17801
17802 /* We'll save this DIE so link it in. */
17803 part_die->die_parent = parent_die;
17804 part_die->die_sibling = NULL;
17805 part_die->die_child = NULL;
17806
17807 if (last_die && last_die == parent_die)
17808 last_die->die_child = part_die;
17809 else if (last_die)
17810 last_die->die_sibling = part_die;
17811
17812 last_die = part_die;
17813
17814 if (first_die == NULL)
17815 first_die = part_die;
17816
17817 /* Maybe add the DIE to the hash table. Not all DIEs that we
17818 find interesting need to be in the hash table, because we
17819 also have the parent/sibling/child chains; only those that we
17820 might refer to by offset later during partial symbol reading.
17821
17822 For now this means things that might have be the target of a
17823 DW_AT_specification, DW_AT_abstract_origin, or
17824 DW_AT_extension. DW_AT_extension will refer only to
17825 namespaces; DW_AT_abstract_origin refers to functions (and
17826 many things under the function DIE, but we do not recurse
17827 into function DIEs during partial symbol reading) and
17828 possibly variables as well; DW_AT_specification refers to
17829 declarations. Declarations ought to have the DW_AT_declaration
17830 flag. It happens that GCC forgets to put it in sometimes, but
17831 only for functions, not for types.
17832
17833 Adding more things than necessary to the hash table is harmless
17834 except for the performance cost. Adding too few will result in
17835 wasted time in find_partial_die, when we reread the compilation
17836 unit with load_all_dies set. */
17837
17838 if (load_all
17839 || abbrev->tag == DW_TAG_constant
17840 || abbrev->tag == DW_TAG_subprogram
17841 || abbrev->tag == DW_TAG_variable
17842 || abbrev->tag == DW_TAG_namespace
17843 || part_die->is_declaration)
17844 {
17845 void **slot;
17846
17847 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17848 to_underlying (part_die->sect_off),
17849 INSERT);
17850 *slot = part_die;
17851 }
17852
17853 /* For some DIEs we want to follow their children (if any). For C
17854 we have no reason to follow the children of structures; for other
17855 languages we have to, so that we can get at method physnames
17856 to infer fully qualified class names, for DW_AT_specification,
17857 and for C++ template arguments. For C++, we also look one level
17858 inside functions to find template arguments (if the name of the
17859 function does not already contain the template arguments).
17860
17861 For Ada and Fortran, we need to scan the children of subprograms
17862 and lexical blocks as well because these languages allow the
17863 definition of nested entities that could be interesting for the
17864 debugger, such as nested subprograms for instance. */
17865 if (last_die->has_children
17866 && (load_all
17867 || last_die->tag == DW_TAG_namespace
17868 || last_die->tag == DW_TAG_module
17869 || last_die->tag == DW_TAG_enumeration_type
17870 || (cu->language == language_cplus
17871 && last_die->tag == DW_TAG_subprogram
17872 && (last_die->name == NULL
17873 || strchr (last_die->name, '<') == NULL))
17874 || (cu->language != language_c
17875 && (last_die->tag == DW_TAG_class_type
17876 || last_die->tag == DW_TAG_interface_type
17877 || last_die->tag == DW_TAG_structure_type
17878 || last_die->tag == DW_TAG_union_type))
17879 || ((cu->language == language_ada
17880 || cu->language == language_fortran)
17881 && (last_die->tag == DW_TAG_subprogram
17882 || last_die->tag == DW_TAG_lexical_block))))
17883 {
17884 nesting_level++;
17885 parent_die = last_die;
17886 continue;
17887 }
17888
17889 /* Otherwise we skip to the next sibling, if any. */
17890 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17891
17892 /* Back to the top, do it again. */
17893 }
17894 }
17895
17896 partial_die_info::partial_die_info (sect_offset sect_off_,
17897 struct abbrev_info *abbrev)
17898 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17899 {
17900 }
17901
17902 /* Read a minimal amount of information into the minimal die structure.
17903 INFO_PTR should point just after the initial uleb128 of a DIE. */
17904
17905 const gdb_byte *
17906 partial_die_info::read (const struct die_reader_specs *reader,
17907 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17908 {
17909 struct dwarf2_cu *cu = reader->cu;
17910 struct dwarf2_per_objfile *dwarf2_per_objfile
17911 = cu->per_cu->dwarf2_per_objfile;
17912 unsigned int i;
17913 int has_low_pc_attr = 0;
17914 int has_high_pc_attr = 0;
17915 int high_pc_relative = 0;
17916
17917 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17918 for (i = 0; i < abbrev.num_attrs; ++i)
17919 {
17920 bool need_reprocess;
17921 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17922 info_ptr, &need_reprocess);
17923 /* String and address offsets that need to do the reprocessing have
17924 already been read at this point, so there is no need to wait until
17925 the loop terminates to do the reprocessing. */
17926 if (need_reprocess)
17927 read_attribute_reprocess (reader, &attr_vec[i]);
17928 attribute &attr = attr_vec[i];
17929 /* Store the data if it is of an attribute we want to keep in a
17930 partial symbol table. */
17931 switch (attr.name)
17932 {
17933 case DW_AT_name:
17934 switch (tag)
17935 {
17936 case DW_TAG_compile_unit:
17937 case DW_TAG_partial_unit:
17938 case DW_TAG_type_unit:
17939 /* Compilation units have a DW_AT_name that is a filename, not
17940 a source language identifier. */
17941 case DW_TAG_enumeration_type:
17942 case DW_TAG_enumerator:
17943 /* These tags always have simple identifiers already; no need
17944 to canonicalize them. */
17945 name = DW_STRING (&attr);
17946 break;
17947 default:
17948 {
17949 struct objfile *objfile = dwarf2_per_objfile->objfile;
17950
17951 name
17952 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17953 }
17954 break;
17955 }
17956 break;
17957 case DW_AT_linkage_name:
17958 case DW_AT_MIPS_linkage_name:
17959 /* Note that both forms of linkage name might appear. We
17960 assume they will be the same, and we only store the last
17961 one we see. */
17962 linkage_name = DW_STRING (&attr);
17963 break;
17964 case DW_AT_low_pc:
17965 has_low_pc_attr = 1;
17966 lowpc = attr.value_as_address ();
17967 break;
17968 case DW_AT_high_pc:
17969 has_high_pc_attr = 1;
17970 highpc = attr.value_as_address ();
17971 if (cu->header.version >= 4 && attr.form_is_constant ())
17972 high_pc_relative = 1;
17973 break;
17974 case DW_AT_location:
17975 /* Support the .debug_loc offsets. */
17976 if (attr.form_is_block ())
17977 {
17978 d.locdesc = DW_BLOCK (&attr);
17979 }
17980 else if (attr.form_is_section_offset ())
17981 {
17982 dwarf2_complex_location_expr_complaint ();
17983 }
17984 else
17985 {
17986 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17987 "partial symbol information");
17988 }
17989 break;
17990 case DW_AT_external:
17991 is_external = DW_UNSND (&attr);
17992 break;
17993 case DW_AT_declaration:
17994 is_declaration = DW_UNSND (&attr);
17995 break;
17996 case DW_AT_type:
17997 has_type = 1;
17998 break;
17999 case DW_AT_abstract_origin:
18000 case DW_AT_specification:
18001 case DW_AT_extension:
18002 has_specification = 1;
18003 spec_offset = dwarf2_get_ref_die_offset (&attr);
18004 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18005 || cu->per_cu->is_dwz);
18006 break;
18007 case DW_AT_sibling:
18008 /* Ignore absolute siblings, they might point outside of
18009 the current compile unit. */
18010 if (attr.form == DW_FORM_ref_addr)
18011 complaint (_("ignoring absolute DW_AT_sibling"));
18012 else
18013 {
18014 const gdb_byte *buffer = reader->buffer;
18015 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18016 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18017
18018 if (sibling_ptr < info_ptr)
18019 complaint (_("DW_AT_sibling points backwards"));
18020 else if (sibling_ptr > reader->buffer_end)
18021 reader->die_section->overflow_complaint ();
18022 else
18023 sibling = sibling_ptr;
18024 }
18025 break;
18026 case DW_AT_byte_size:
18027 has_byte_size = 1;
18028 break;
18029 case DW_AT_const_value:
18030 has_const_value = 1;
18031 break;
18032 case DW_AT_calling_convention:
18033 /* DWARF doesn't provide a way to identify a program's source-level
18034 entry point. DW_AT_calling_convention attributes are only meant
18035 to describe functions' calling conventions.
18036
18037 However, because it's a necessary piece of information in
18038 Fortran, and before DWARF 4 DW_CC_program was the only
18039 piece of debugging information whose definition refers to
18040 a 'main program' at all, several compilers marked Fortran
18041 main programs with DW_CC_program --- even when those
18042 functions use the standard calling conventions.
18043
18044 Although DWARF now specifies a way to provide this
18045 information, we support this practice for backward
18046 compatibility. */
18047 if (DW_UNSND (&attr) == DW_CC_program
18048 && cu->language == language_fortran)
18049 main_subprogram = 1;
18050 break;
18051 case DW_AT_inline:
18052 if (DW_UNSND (&attr) == DW_INL_inlined
18053 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18054 may_be_inlined = 1;
18055 break;
18056
18057 case DW_AT_import:
18058 if (tag == DW_TAG_imported_unit)
18059 {
18060 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18061 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18062 || cu->per_cu->is_dwz);
18063 }
18064 break;
18065
18066 case DW_AT_main_subprogram:
18067 main_subprogram = DW_UNSND (&attr);
18068 break;
18069
18070 case DW_AT_ranges:
18071 {
18072 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18073 but that requires a full DIE, so instead we just
18074 reimplement it. */
18075 int need_ranges_base = tag != DW_TAG_compile_unit;
18076 unsigned int ranges_offset = (DW_UNSND (&attr)
18077 + (need_ranges_base
18078 ? cu->ranges_base
18079 : 0));
18080
18081 /* Value of the DW_AT_ranges attribute is the offset in the
18082 .debug_ranges section. */
18083 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18084 nullptr))
18085 has_pc_info = 1;
18086 }
18087 break;
18088
18089 default:
18090 break;
18091 }
18092 }
18093
18094 /* For Ada, if both the name and the linkage name appear, we prefer
18095 the latter. This lets "catch exception" work better, regardless
18096 of the order in which the name and linkage name were emitted.
18097 Really, though, this is just a workaround for the fact that gdb
18098 doesn't store both the name and the linkage name. */
18099 if (cu->language == language_ada && linkage_name != nullptr)
18100 name = linkage_name;
18101
18102 if (high_pc_relative)
18103 highpc += lowpc;
18104
18105 if (has_low_pc_attr && has_high_pc_attr)
18106 {
18107 /* When using the GNU linker, .gnu.linkonce. sections are used to
18108 eliminate duplicate copies of functions and vtables and such.
18109 The linker will arbitrarily choose one and discard the others.
18110 The AT_*_pc values for such functions refer to local labels in
18111 these sections. If the section from that file was discarded, the
18112 labels are not in the output, so the relocs get a value of 0.
18113 If this is a discarded function, mark the pc bounds as invalid,
18114 so that GDB will ignore it. */
18115 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18116 {
18117 struct objfile *objfile = dwarf2_per_objfile->objfile;
18118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18119
18120 complaint (_("DW_AT_low_pc %s is zero "
18121 "for DIE at %s [in module %s]"),
18122 paddress (gdbarch, lowpc),
18123 sect_offset_str (sect_off),
18124 objfile_name (objfile));
18125 }
18126 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18127 else if (lowpc >= highpc)
18128 {
18129 struct objfile *objfile = dwarf2_per_objfile->objfile;
18130 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18131
18132 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18133 "for DIE at %s [in module %s]"),
18134 paddress (gdbarch, lowpc),
18135 paddress (gdbarch, highpc),
18136 sect_offset_str (sect_off),
18137 objfile_name (objfile));
18138 }
18139 else
18140 has_pc_info = 1;
18141 }
18142
18143 return info_ptr;
18144 }
18145
18146 /* Find a cached partial DIE at OFFSET in CU. */
18147
18148 struct partial_die_info *
18149 dwarf2_cu::find_partial_die (sect_offset sect_off)
18150 {
18151 struct partial_die_info *lookup_die = NULL;
18152 struct partial_die_info part_die (sect_off);
18153
18154 lookup_die = ((struct partial_die_info *)
18155 htab_find_with_hash (partial_dies, &part_die,
18156 to_underlying (sect_off)));
18157
18158 return lookup_die;
18159 }
18160
18161 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18162 except in the case of .debug_types DIEs which do not reference
18163 outside their CU (they do however referencing other types via
18164 DW_FORM_ref_sig8). */
18165
18166 static const struct cu_partial_die_info
18167 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18168 {
18169 struct dwarf2_per_objfile *dwarf2_per_objfile
18170 = cu->per_cu->dwarf2_per_objfile;
18171 struct objfile *objfile = dwarf2_per_objfile->objfile;
18172 struct dwarf2_per_cu_data *per_cu = NULL;
18173 struct partial_die_info *pd = NULL;
18174
18175 if (offset_in_dwz == cu->per_cu->is_dwz
18176 && cu->header.offset_in_cu_p (sect_off))
18177 {
18178 pd = cu->find_partial_die (sect_off);
18179 if (pd != NULL)
18180 return { cu, pd };
18181 /* We missed recording what we needed.
18182 Load all dies and try again. */
18183 per_cu = cu->per_cu;
18184 }
18185 else
18186 {
18187 /* TUs don't reference other CUs/TUs (except via type signatures). */
18188 if (cu->per_cu->is_debug_types)
18189 {
18190 error (_("Dwarf Error: Type Unit at offset %s contains"
18191 " external reference to offset %s [in module %s].\n"),
18192 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18193 bfd_get_filename (objfile->obfd));
18194 }
18195 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18196 dwarf2_per_objfile);
18197
18198 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18199 load_partial_comp_unit (per_cu);
18200
18201 per_cu->cu->last_used = 0;
18202 pd = per_cu->cu->find_partial_die (sect_off);
18203 }
18204
18205 /* If we didn't find it, and not all dies have been loaded,
18206 load them all and try again. */
18207
18208 if (pd == NULL && per_cu->load_all_dies == 0)
18209 {
18210 per_cu->load_all_dies = 1;
18211
18212 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18213 THIS_CU->cu may already be in use. So we can't just free it and
18214 replace its DIEs with the ones we read in. Instead, we leave those
18215 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18216 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18217 set. */
18218 load_partial_comp_unit (per_cu);
18219
18220 pd = per_cu->cu->find_partial_die (sect_off);
18221 }
18222
18223 if (pd == NULL)
18224 internal_error (__FILE__, __LINE__,
18225 _("could not find partial DIE %s "
18226 "in cache [from module %s]\n"),
18227 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18228 return { per_cu->cu, pd };
18229 }
18230
18231 /* See if we can figure out if the class lives in a namespace. We do
18232 this by looking for a member function; its demangled name will
18233 contain namespace info, if there is any. */
18234
18235 static void
18236 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18237 struct dwarf2_cu *cu)
18238 {
18239 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18240 what template types look like, because the demangler
18241 frequently doesn't give the same name as the debug info. We
18242 could fix this by only using the demangled name to get the
18243 prefix (but see comment in read_structure_type). */
18244
18245 struct partial_die_info *real_pdi;
18246 struct partial_die_info *child_pdi;
18247
18248 /* If this DIE (this DIE's specification, if any) has a parent, then
18249 we should not do this. We'll prepend the parent's fully qualified
18250 name when we create the partial symbol. */
18251
18252 real_pdi = struct_pdi;
18253 while (real_pdi->has_specification)
18254 {
18255 auto res = find_partial_die (real_pdi->spec_offset,
18256 real_pdi->spec_is_dwz, cu);
18257 real_pdi = res.pdi;
18258 cu = res.cu;
18259 }
18260
18261 if (real_pdi->die_parent != NULL)
18262 return;
18263
18264 for (child_pdi = struct_pdi->die_child;
18265 child_pdi != NULL;
18266 child_pdi = child_pdi->die_sibling)
18267 {
18268 if (child_pdi->tag == DW_TAG_subprogram
18269 && child_pdi->linkage_name != NULL)
18270 {
18271 gdb::unique_xmalloc_ptr<char> actual_class_name
18272 (language_class_name_from_physname (cu->language_defn,
18273 child_pdi->linkage_name));
18274 if (actual_class_name != NULL)
18275 {
18276 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18277 struct_pdi->name = objfile->intern (actual_class_name.get ());
18278 }
18279 break;
18280 }
18281 }
18282 }
18283
18284 void
18285 partial_die_info::fixup (struct dwarf2_cu *cu)
18286 {
18287 /* Once we've fixed up a die, there's no point in doing so again.
18288 This also avoids a memory leak if we were to call
18289 guess_partial_die_structure_name multiple times. */
18290 if (fixup_called)
18291 return;
18292
18293 /* If we found a reference attribute and the DIE has no name, try
18294 to find a name in the referred to DIE. */
18295
18296 if (name == NULL && has_specification)
18297 {
18298 struct partial_die_info *spec_die;
18299
18300 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18301 spec_die = res.pdi;
18302 cu = res.cu;
18303
18304 spec_die->fixup (cu);
18305
18306 if (spec_die->name)
18307 {
18308 name = spec_die->name;
18309
18310 /* Copy DW_AT_external attribute if it is set. */
18311 if (spec_die->is_external)
18312 is_external = spec_die->is_external;
18313 }
18314 }
18315
18316 /* Set default names for some unnamed DIEs. */
18317
18318 if (name == NULL && tag == DW_TAG_namespace)
18319 name = CP_ANONYMOUS_NAMESPACE_STR;
18320
18321 /* If there is no parent die to provide a namespace, and there are
18322 children, see if we can determine the namespace from their linkage
18323 name. */
18324 if (cu->language == language_cplus
18325 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18326 && die_parent == NULL
18327 && has_children
18328 && (tag == DW_TAG_class_type
18329 || tag == DW_TAG_structure_type
18330 || tag == DW_TAG_union_type))
18331 guess_partial_die_structure_name (this, cu);
18332
18333 /* GCC might emit a nameless struct or union that has a linkage
18334 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18335 if (name == NULL
18336 && (tag == DW_TAG_class_type
18337 || tag == DW_TAG_interface_type
18338 || tag == DW_TAG_structure_type
18339 || tag == DW_TAG_union_type)
18340 && linkage_name != NULL)
18341 {
18342 gdb::unique_xmalloc_ptr<char> demangled
18343 (gdb_demangle (linkage_name, DMGL_TYPES));
18344 if (demangled != nullptr)
18345 {
18346 const char *base;
18347
18348 /* Strip any leading namespaces/classes, keep only the base name.
18349 DW_AT_name for named DIEs does not contain the prefixes. */
18350 base = strrchr (demangled.get (), ':');
18351 if (base && base > demangled.get () && base[-1] == ':')
18352 base++;
18353 else
18354 base = demangled.get ();
18355
18356 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18357 name = objfile->intern (base);
18358 }
18359 }
18360
18361 fixup_called = 1;
18362 }
18363
18364 /* Process the attributes that had to be skipped in the first round. These
18365 attributes are the ones that need str_offsets_base or addr_base attributes.
18366 They could not have been processed in the first round, because at the time
18367 the values of str_offsets_base or addr_base may not have been known. */
18368 void read_attribute_reprocess (const struct die_reader_specs *reader,
18369 struct attribute *attr)
18370 {
18371 struct dwarf2_cu *cu = reader->cu;
18372 switch (attr->form)
18373 {
18374 case DW_FORM_addrx:
18375 case DW_FORM_GNU_addr_index:
18376 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18377 break;
18378 case DW_FORM_strx:
18379 case DW_FORM_strx1:
18380 case DW_FORM_strx2:
18381 case DW_FORM_strx3:
18382 case DW_FORM_strx4:
18383 case DW_FORM_GNU_str_index:
18384 {
18385 unsigned int str_index = DW_UNSND (attr);
18386 if (reader->dwo_file != NULL)
18387 {
18388 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18389 DW_STRING_IS_CANONICAL (attr) = 0;
18390 }
18391 else
18392 {
18393 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18394 DW_STRING_IS_CANONICAL (attr) = 0;
18395 }
18396 break;
18397 }
18398 default:
18399 gdb_assert_not_reached (_("Unexpected DWARF form."));
18400 }
18401 }
18402
18403 /* Read an attribute value described by an attribute form. */
18404
18405 static const gdb_byte *
18406 read_attribute_value (const struct die_reader_specs *reader,
18407 struct attribute *attr, unsigned form,
18408 LONGEST implicit_const, const gdb_byte *info_ptr,
18409 bool *need_reprocess)
18410 {
18411 struct dwarf2_cu *cu = reader->cu;
18412 struct dwarf2_per_objfile *dwarf2_per_objfile
18413 = cu->per_cu->dwarf2_per_objfile;
18414 struct objfile *objfile = dwarf2_per_objfile->objfile;
18415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18416 bfd *abfd = reader->abfd;
18417 struct comp_unit_head *cu_header = &cu->header;
18418 unsigned int bytes_read;
18419 struct dwarf_block *blk;
18420 *need_reprocess = false;
18421
18422 attr->form = (enum dwarf_form) form;
18423 switch (form)
18424 {
18425 case DW_FORM_ref_addr:
18426 if (cu->header.version == 2)
18427 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18428 &bytes_read);
18429 else
18430 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18431 &bytes_read);
18432 info_ptr += bytes_read;
18433 break;
18434 case DW_FORM_GNU_ref_alt:
18435 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18436 info_ptr += bytes_read;
18437 break;
18438 case DW_FORM_addr:
18439 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18440 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18441 info_ptr += bytes_read;
18442 break;
18443 case DW_FORM_block2:
18444 blk = dwarf_alloc_block (cu);
18445 blk->size = read_2_bytes (abfd, info_ptr);
18446 info_ptr += 2;
18447 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18448 info_ptr += blk->size;
18449 DW_BLOCK (attr) = blk;
18450 break;
18451 case DW_FORM_block4:
18452 blk = dwarf_alloc_block (cu);
18453 blk->size = read_4_bytes (abfd, info_ptr);
18454 info_ptr += 4;
18455 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18456 info_ptr += blk->size;
18457 DW_BLOCK (attr) = blk;
18458 break;
18459 case DW_FORM_data2:
18460 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18461 info_ptr += 2;
18462 break;
18463 case DW_FORM_data4:
18464 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18465 info_ptr += 4;
18466 break;
18467 case DW_FORM_data8:
18468 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18469 info_ptr += 8;
18470 break;
18471 case DW_FORM_data16:
18472 blk = dwarf_alloc_block (cu);
18473 blk->size = 16;
18474 blk->data = read_n_bytes (abfd, info_ptr, 16);
18475 info_ptr += 16;
18476 DW_BLOCK (attr) = blk;
18477 break;
18478 case DW_FORM_sec_offset:
18479 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18480 info_ptr += bytes_read;
18481 break;
18482 case DW_FORM_string:
18483 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18484 DW_STRING_IS_CANONICAL (attr) = 0;
18485 info_ptr += bytes_read;
18486 break;
18487 case DW_FORM_strp:
18488 if (!cu->per_cu->is_dwz)
18489 {
18490 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18491 abfd, info_ptr, cu_header,
18492 &bytes_read);
18493 DW_STRING_IS_CANONICAL (attr) = 0;
18494 info_ptr += bytes_read;
18495 break;
18496 }
18497 /* FALLTHROUGH */
18498 case DW_FORM_line_strp:
18499 if (!cu->per_cu->is_dwz)
18500 {
18501 DW_STRING (attr)
18502 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18503 &bytes_read);
18504 DW_STRING_IS_CANONICAL (attr) = 0;
18505 info_ptr += bytes_read;
18506 break;
18507 }
18508 /* FALLTHROUGH */
18509 case DW_FORM_GNU_strp_alt:
18510 {
18511 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18512 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18513 &bytes_read);
18514
18515 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18516 DW_STRING_IS_CANONICAL (attr) = 0;
18517 info_ptr += bytes_read;
18518 }
18519 break;
18520 case DW_FORM_exprloc:
18521 case DW_FORM_block:
18522 blk = dwarf_alloc_block (cu);
18523 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18524 info_ptr += bytes_read;
18525 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18526 info_ptr += blk->size;
18527 DW_BLOCK (attr) = blk;
18528 break;
18529 case DW_FORM_block1:
18530 blk = dwarf_alloc_block (cu);
18531 blk->size = read_1_byte (abfd, info_ptr);
18532 info_ptr += 1;
18533 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18534 info_ptr += blk->size;
18535 DW_BLOCK (attr) = blk;
18536 break;
18537 case DW_FORM_data1:
18538 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18539 info_ptr += 1;
18540 break;
18541 case DW_FORM_flag:
18542 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18543 info_ptr += 1;
18544 break;
18545 case DW_FORM_flag_present:
18546 DW_UNSND (attr) = 1;
18547 break;
18548 case DW_FORM_sdata:
18549 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18550 info_ptr += bytes_read;
18551 break;
18552 case DW_FORM_udata:
18553 case DW_FORM_rnglistx:
18554 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18555 info_ptr += bytes_read;
18556 break;
18557 case DW_FORM_ref1:
18558 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18559 + read_1_byte (abfd, info_ptr));
18560 info_ptr += 1;
18561 break;
18562 case DW_FORM_ref2:
18563 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18564 + read_2_bytes (abfd, info_ptr));
18565 info_ptr += 2;
18566 break;
18567 case DW_FORM_ref4:
18568 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18569 + read_4_bytes (abfd, info_ptr));
18570 info_ptr += 4;
18571 break;
18572 case DW_FORM_ref8:
18573 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18574 + read_8_bytes (abfd, info_ptr));
18575 info_ptr += 8;
18576 break;
18577 case DW_FORM_ref_sig8:
18578 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18579 info_ptr += 8;
18580 break;
18581 case DW_FORM_ref_udata:
18582 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18583 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18584 info_ptr += bytes_read;
18585 break;
18586 case DW_FORM_indirect:
18587 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18588 info_ptr += bytes_read;
18589 if (form == DW_FORM_implicit_const)
18590 {
18591 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18592 info_ptr += bytes_read;
18593 }
18594 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18595 info_ptr, need_reprocess);
18596 break;
18597 case DW_FORM_implicit_const:
18598 DW_SND (attr) = implicit_const;
18599 break;
18600 case DW_FORM_addrx:
18601 case DW_FORM_GNU_addr_index:
18602 *need_reprocess = true;
18603 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18604 info_ptr += bytes_read;
18605 break;
18606 case DW_FORM_strx:
18607 case DW_FORM_strx1:
18608 case DW_FORM_strx2:
18609 case DW_FORM_strx3:
18610 case DW_FORM_strx4:
18611 case DW_FORM_GNU_str_index:
18612 {
18613 ULONGEST str_index;
18614 if (form == DW_FORM_strx1)
18615 {
18616 str_index = read_1_byte (abfd, info_ptr);
18617 info_ptr += 1;
18618 }
18619 else if (form == DW_FORM_strx2)
18620 {
18621 str_index = read_2_bytes (abfd, info_ptr);
18622 info_ptr += 2;
18623 }
18624 else if (form == DW_FORM_strx3)
18625 {
18626 str_index = read_3_bytes (abfd, info_ptr);
18627 info_ptr += 3;
18628 }
18629 else if (form == DW_FORM_strx4)
18630 {
18631 str_index = read_4_bytes (abfd, info_ptr);
18632 info_ptr += 4;
18633 }
18634 else
18635 {
18636 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18637 info_ptr += bytes_read;
18638 }
18639 *need_reprocess = true;
18640 DW_UNSND (attr) = str_index;
18641 }
18642 break;
18643 default:
18644 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18645 dwarf_form_name (form),
18646 bfd_get_filename (abfd));
18647 }
18648
18649 /* Super hack. */
18650 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18651 attr->form = DW_FORM_GNU_ref_alt;
18652
18653 /* We have seen instances where the compiler tried to emit a byte
18654 size attribute of -1 which ended up being encoded as an unsigned
18655 0xffffffff. Although 0xffffffff is technically a valid size value,
18656 an object of this size seems pretty unlikely so we can relatively
18657 safely treat these cases as if the size attribute was invalid and
18658 treat them as zero by default. */
18659 if (attr->name == DW_AT_byte_size
18660 && form == DW_FORM_data4
18661 && DW_UNSND (attr) >= 0xffffffff)
18662 {
18663 complaint
18664 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18665 hex_string (DW_UNSND (attr)));
18666 DW_UNSND (attr) = 0;
18667 }
18668
18669 return info_ptr;
18670 }
18671
18672 /* Read an attribute described by an abbreviated attribute. */
18673
18674 static const gdb_byte *
18675 read_attribute (const struct die_reader_specs *reader,
18676 struct attribute *attr, struct attr_abbrev *abbrev,
18677 const gdb_byte *info_ptr, bool *need_reprocess)
18678 {
18679 attr->name = abbrev->name;
18680 return read_attribute_value (reader, attr, abbrev->form,
18681 abbrev->implicit_const, info_ptr,
18682 need_reprocess);
18683 }
18684
18685 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18686
18687 static const char *
18688 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18689 LONGEST str_offset)
18690 {
18691 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18692 str_offset, "DW_FORM_strp");
18693 }
18694
18695 /* Return pointer to string at .debug_str offset as read from BUF.
18696 BUF is assumed to be in a compilation unit described by CU_HEADER.
18697 Return *BYTES_READ_PTR count of bytes read from BUF. */
18698
18699 static const char *
18700 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18701 const gdb_byte *buf,
18702 const struct comp_unit_head *cu_header,
18703 unsigned int *bytes_read_ptr)
18704 {
18705 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18706
18707 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18708 }
18709
18710 /* See read.h. */
18711
18712 const char *
18713 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18714 const struct comp_unit_head *cu_header,
18715 unsigned int *bytes_read_ptr)
18716 {
18717 bfd *abfd = objfile->obfd;
18718 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18719
18720 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18721 }
18722
18723 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18724 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18725 ADDR_SIZE is the size of addresses from the CU header. */
18726
18727 static CORE_ADDR
18728 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18729 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18730 int addr_size)
18731 {
18732 struct objfile *objfile = dwarf2_per_objfile->objfile;
18733 bfd *abfd = objfile->obfd;
18734 const gdb_byte *info_ptr;
18735 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18736
18737 dwarf2_per_objfile->addr.read (objfile);
18738 if (dwarf2_per_objfile->addr.buffer == NULL)
18739 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18740 objfile_name (objfile));
18741 if (addr_base_or_zero + addr_index * addr_size
18742 >= dwarf2_per_objfile->addr.size)
18743 error (_("DW_FORM_addr_index pointing outside of "
18744 ".debug_addr section [in module %s]"),
18745 objfile_name (objfile));
18746 info_ptr = (dwarf2_per_objfile->addr.buffer
18747 + addr_base_or_zero + addr_index * addr_size);
18748 if (addr_size == 4)
18749 return bfd_get_32 (abfd, info_ptr);
18750 else
18751 return bfd_get_64 (abfd, info_ptr);
18752 }
18753
18754 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18755
18756 static CORE_ADDR
18757 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18758 {
18759 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18760 cu->addr_base, cu->header.addr_size);
18761 }
18762
18763 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18764
18765 static CORE_ADDR
18766 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18767 unsigned int *bytes_read)
18768 {
18769 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18770 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18771
18772 return read_addr_index (cu, addr_index);
18773 }
18774
18775 /* See read.h. */
18776
18777 CORE_ADDR
18778 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18779 {
18780 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18781 struct dwarf2_cu *cu = per_cu->cu;
18782 gdb::optional<ULONGEST> addr_base;
18783 int addr_size;
18784
18785 /* We need addr_base and addr_size.
18786 If we don't have PER_CU->cu, we have to get it.
18787 Nasty, but the alternative is storing the needed info in PER_CU,
18788 which at this point doesn't seem justified: it's not clear how frequently
18789 it would get used and it would increase the size of every PER_CU.
18790 Entry points like dwarf2_per_cu_addr_size do a similar thing
18791 so we're not in uncharted territory here.
18792 Alas we need to be a bit more complicated as addr_base is contained
18793 in the DIE.
18794
18795 We don't need to read the entire CU(/TU).
18796 We just need the header and top level die.
18797
18798 IWBN to use the aging mechanism to let us lazily later discard the CU.
18799 For now we skip this optimization. */
18800
18801 if (cu != NULL)
18802 {
18803 addr_base = cu->addr_base;
18804 addr_size = cu->header.addr_size;
18805 }
18806 else
18807 {
18808 cutu_reader reader (per_cu, NULL, 0, false);
18809 addr_base = reader.cu->addr_base;
18810 addr_size = reader.cu->header.addr_size;
18811 }
18812
18813 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18814 addr_size);
18815 }
18816
18817 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18818 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18819 DWO file. */
18820
18821 static const char *
18822 read_str_index (struct dwarf2_cu *cu,
18823 struct dwarf2_section_info *str_section,
18824 struct dwarf2_section_info *str_offsets_section,
18825 ULONGEST str_offsets_base, ULONGEST str_index)
18826 {
18827 struct dwarf2_per_objfile *dwarf2_per_objfile
18828 = cu->per_cu->dwarf2_per_objfile;
18829 struct objfile *objfile = dwarf2_per_objfile->objfile;
18830 const char *objf_name = objfile_name (objfile);
18831 bfd *abfd = objfile->obfd;
18832 const gdb_byte *info_ptr;
18833 ULONGEST str_offset;
18834 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18835
18836 str_section->read (objfile);
18837 str_offsets_section->read (objfile);
18838 if (str_section->buffer == NULL)
18839 error (_("%s used without %s section"
18840 " in CU at offset %s [in module %s]"),
18841 form_name, str_section->get_name (),
18842 sect_offset_str (cu->header.sect_off), objf_name);
18843 if (str_offsets_section->buffer == NULL)
18844 error (_("%s used without %s section"
18845 " in CU at offset %s [in module %s]"),
18846 form_name, str_section->get_name (),
18847 sect_offset_str (cu->header.sect_off), objf_name);
18848 info_ptr = (str_offsets_section->buffer
18849 + str_offsets_base
18850 + str_index * cu->header.offset_size);
18851 if (cu->header.offset_size == 4)
18852 str_offset = bfd_get_32 (abfd, info_ptr);
18853 else
18854 str_offset = bfd_get_64 (abfd, info_ptr);
18855 if (str_offset >= str_section->size)
18856 error (_("Offset from %s pointing outside of"
18857 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18858 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18859 return (const char *) (str_section->buffer + str_offset);
18860 }
18861
18862 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18863
18864 static const char *
18865 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18866 {
18867 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18868 ? reader->cu->header.addr_size : 0;
18869 return read_str_index (reader->cu,
18870 &reader->dwo_file->sections.str,
18871 &reader->dwo_file->sections.str_offsets,
18872 str_offsets_base, str_index);
18873 }
18874
18875 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18876
18877 static const char *
18878 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18879 {
18880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18881 const char *objf_name = objfile_name (objfile);
18882 static const char form_name[] = "DW_FORM_GNU_str_index";
18883 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18884
18885 if (!cu->str_offsets_base.has_value ())
18886 error (_("%s used in Fission stub without %s"
18887 " in CU at offset 0x%lx [in module %s]"),
18888 form_name, str_offsets_attr_name,
18889 (long) cu->header.offset_size, objf_name);
18890
18891 return read_str_index (cu,
18892 &cu->per_cu->dwarf2_per_objfile->str,
18893 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18894 *cu->str_offsets_base, str_index);
18895 }
18896
18897 /* Return the length of an LEB128 number in BUF. */
18898
18899 static int
18900 leb128_size (const gdb_byte *buf)
18901 {
18902 const gdb_byte *begin = buf;
18903 gdb_byte byte;
18904
18905 while (1)
18906 {
18907 byte = *buf++;
18908 if ((byte & 128) == 0)
18909 return buf - begin;
18910 }
18911 }
18912
18913 static void
18914 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18915 {
18916 switch (lang)
18917 {
18918 case DW_LANG_C89:
18919 case DW_LANG_C99:
18920 case DW_LANG_C11:
18921 case DW_LANG_C:
18922 case DW_LANG_UPC:
18923 cu->language = language_c;
18924 break;
18925 case DW_LANG_Java:
18926 case DW_LANG_C_plus_plus:
18927 case DW_LANG_C_plus_plus_11:
18928 case DW_LANG_C_plus_plus_14:
18929 cu->language = language_cplus;
18930 break;
18931 case DW_LANG_D:
18932 cu->language = language_d;
18933 break;
18934 case DW_LANG_Fortran77:
18935 case DW_LANG_Fortran90:
18936 case DW_LANG_Fortran95:
18937 case DW_LANG_Fortran03:
18938 case DW_LANG_Fortran08:
18939 cu->language = language_fortran;
18940 break;
18941 case DW_LANG_Go:
18942 cu->language = language_go;
18943 break;
18944 case DW_LANG_Mips_Assembler:
18945 cu->language = language_asm;
18946 break;
18947 case DW_LANG_Ada83:
18948 case DW_LANG_Ada95:
18949 cu->language = language_ada;
18950 break;
18951 case DW_LANG_Modula2:
18952 cu->language = language_m2;
18953 break;
18954 case DW_LANG_Pascal83:
18955 cu->language = language_pascal;
18956 break;
18957 case DW_LANG_ObjC:
18958 cu->language = language_objc;
18959 break;
18960 case DW_LANG_Rust:
18961 case DW_LANG_Rust_old:
18962 cu->language = language_rust;
18963 break;
18964 case DW_LANG_Cobol74:
18965 case DW_LANG_Cobol85:
18966 default:
18967 cu->language = language_minimal;
18968 break;
18969 }
18970 cu->language_defn = language_def (cu->language);
18971 }
18972
18973 /* Return the named attribute or NULL if not there. */
18974
18975 static struct attribute *
18976 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18977 {
18978 for (;;)
18979 {
18980 unsigned int i;
18981 struct attribute *spec = NULL;
18982
18983 for (i = 0; i < die->num_attrs; ++i)
18984 {
18985 if (die->attrs[i].name == name)
18986 return &die->attrs[i];
18987 if (die->attrs[i].name == DW_AT_specification
18988 || die->attrs[i].name == DW_AT_abstract_origin)
18989 spec = &die->attrs[i];
18990 }
18991
18992 if (!spec)
18993 break;
18994
18995 die = follow_die_ref (die, spec, &cu);
18996 }
18997
18998 return NULL;
18999 }
19000
19001 /* Return the named attribute or NULL if not there,
19002 but do not follow DW_AT_specification, etc.
19003 This is for use in contexts where we're reading .debug_types dies.
19004 Following DW_AT_specification, DW_AT_abstract_origin will take us
19005 back up the chain, and we want to go down. */
19006
19007 static struct attribute *
19008 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19009 {
19010 unsigned int i;
19011
19012 for (i = 0; i < die->num_attrs; ++i)
19013 if (die->attrs[i].name == name)
19014 return &die->attrs[i];
19015
19016 return NULL;
19017 }
19018
19019 /* Return the string associated with a string-typed attribute, or NULL if it
19020 is either not found or is of an incorrect type. */
19021
19022 static const char *
19023 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19024 {
19025 struct attribute *attr;
19026 const char *str = NULL;
19027
19028 attr = dwarf2_attr (die, name, cu);
19029
19030 if (attr != NULL)
19031 {
19032 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19033 || attr->form == DW_FORM_string
19034 || attr->form == DW_FORM_strx
19035 || attr->form == DW_FORM_strx1
19036 || attr->form == DW_FORM_strx2
19037 || attr->form == DW_FORM_strx3
19038 || attr->form == DW_FORM_strx4
19039 || attr->form == DW_FORM_GNU_str_index
19040 || attr->form == DW_FORM_GNU_strp_alt)
19041 str = DW_STRING (attr);
19042 else
19043 complaint (_("string type expected for attribute %s for "
19044 "DIE at %s in module %s"),
19045 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19046 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19047 }
19048
19049 return str;
19050 }
19051
19052 /* Return the dwo name or NULL if not present. If present, it is in either
19053 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19054 static const char *
19055 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19056 {
19057 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19058 if (dwo_name == nullptr)
19059 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19060 return dwo_name;
19061 }
19062
19063 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19064 and holds a non-zero value. This function should only be used for
19065 DW_FORM_flag or DW_FORM_flag_present attributes. */
19066
19067 static int
19068 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19069 {
19070 struct attribute *attr = dwarf2_attr (die, name, cu);
19071
19072 return (attr && DW_UNSND (attr));
19073 }
19074
19075 static int
19076 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19077 {
19078 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19079 which value is non-zero. However, we have to be careful with
19080 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19081 (via dwarf2_flag_true_p) follows this attribute. So we may
19082 end up accidently finding a declaration attribute that belongs
19083 to a different DIE referenced by the specification attribute,
19084 even though the given DIE does not have a declaration attribute. */
19085 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19086 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19087 }
19088
19089 /* Return the die giving the specification for DIE, if there is
19090 one. *SPEC_CU is the CU containing DIE on input, and the CU
19091 containing the return value on output. If there is no
19092 specification, but there is an abstract origin, that is
19093 returned. */
19094
19095 static struct die_info *
19096 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19097 {
19098 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19099 *spec_cu);
19100
19101 if (spec_attr == NULL)
19102 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19103
19104 if (spec_attr == NULL)
19105 return NULL;
19106 else
19107 return follow_die_ref (die, spec_attr, spec_cu);
19108 }
19109
19110 /* Stub for free_line_header to match void * callback types. */
19111
19112 static void
19113 free_line_header_voidp (void *arg)
19114 {
19115 struct line_header *lh = (struct line_header *) arg;
19116
19117 delete lh;
19118 }
19119
19120 /* A convenience function to find the proper .debug_line section for a CU. */
19121
19122 static struct dwarf2_section_info *
19123 get_debug_line_section (struct dwarf2_cu *cu)
19124 {
19125 struct dwarf2_section_info *section;
19126 struct dwarf2_per_objfile *dwarf2_per_objfile
19127 = cu->per_cu->dwarf2_per_objfile;
19128
19129 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19130 DWO file. */
19131 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19132 section = &cu->dwo_unit->dwo_file->sections.line;
19133 else if (cu->per_cu->is_dwz)
19134 {
19135 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19136
19137 section = &dwz->line;
19138 }
19139 else
19140 section = &dwarf2_per_objfile->line;
19141
19142 return section;
19143 }
19144
19145 /* Read the statement program header starting at OFFSET in
19146 .debug_line, or .debug_line.dwo. Return a pointer
19147 to a struct line_header, allocated using xmalloc.
19148 Returns NULL if there is a problem reading the header, e.g., if it
19149 has a version we don't understand.
19150
19151 NOTE: the strings in the include directory and file name tables of
19152 the returned object point into the dwarf line section buffer,
19153 and must not be freed. */
19154
19155 static line_header_up
19156 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19157 {
19158 struct dwarf2_section_info *section;
19159 struct dwarf2_per_objfile *dwarf2_per_objfile
19160 = cu->per_cu->dwarf2_per_objfile;
19161
19162 section = get_debug_line_section (cu);
19163 section->read (dwarf2_per_objfile->objfile);
19164 if (section->buffer == NULL)
19165 {
19166 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19167 complaint (_("missing .debug_line.dwo section"));
19168 else
19169 complaint (_("missing .debug_line section"));
19170 return 0;
19171 }
19172
19173 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19174 dwarf2_per_objfile, section,
19175 &cu->header);
19176 }
19177
19178 /* Subroutine of dwarf_decode_lines to simplify it.
19179 Return the file name of the psymtab for the given file_entry.
19180 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19181 If space for the result is malloc'd, *NAME_HOLDER will be set.
19182 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19183
19184 static const char *
19185 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19186 const dwarf2_psymtab *pst,
19187 const char *comp_dir,
19188 gdb::unique_xmalloc_ptr<char> *name_holder)
19189 {
19190 const char *include_name = fe.name;
19191 const char *include_name_to_compare = include_name;
19192 const char *pst_filename;
19193 int file_is_pst;
19194
19195 const char *dir_name = fe.include_dir (lh);
19196
19197 gdb::unique_xmalloc_ptr<char> hold_compare;
19198 if (!IS_ABSOLUTE_PATH (include_name)
19199 && (dir_name != NULL || comp_dir != NULL))
19200 {
19201 /* Avoid creating a duplicate psymtab for PST.
19202 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19203 Before we do the comparison, however, we need to account
19204 for DIR_NAME and COMP_DIR.
19205 First prepend dir_name (if non-NULL). If we still don't
19206 have an absolute path prepend comp_dir (if non-NULL).
19207 However, the directory we record in the include-file's
19208 psymtab does not contain COMP_DIR (to match the
19209 corresponding symtab(s)).
19210
19211 Example:
19212
19213 bash$ cd /tmp
19214 bash$ gcc -g ./hello.c
19215 include_name = "hello.c"
19216 dir_name = "."
19217 DW_AT_comp_dir = comp_dir = "/tmp"
19218 DW_AT_name = "./hello.c"
19219
19220 */
19221
19222 if (dir_name != NULL)
19223 {
19224 name_holder->reset (concat (dir_name, SLASH_STRING,
19225 include_name, (char *) NULL));
19226 include_name = name_holder->get ();
19227 include_name_to_compare = include_name;
19228 }
19229 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19230 {
19231 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19232 include_name, (char *) NULL));
19233 include_name_to_compare = hold_compare.get ();
19234 }
19235 }
19236
19237 pst_filename = pst->filename;
19238 gdb::unique_xmalloc_ptr<char> copied_name;
19239 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19240 {
19241 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19242 pst_filename, (char *) NULL));
19243 pst_filename = copied_name.get ();
19244 }
19245
19246 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19247
19248 if (file_is_pst)
19249 return NULL;
19250 return include_name;
19251 }
19252
19253 /* State machine to track the state of the line number program. */
19254
19255 class lnp_state_machine
19256 {
19257 public:
19258 /* Initialize a machine state for the start of a line number
19259 program. */
19260 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19261 bool record_lines_p);
19262
19263 file_entry *current_file ()
19264 {
19265 /* lh->file_names is 0-based, but the file name numbers in the
19266 statement program are 1-based. */
19267 return m_line_header->file_name_at (m_file);
19268 }
19269
19270 /* Record the line in the state machine. END_SEQUENCE is true if
19271 we're processing the end of a sequence. */
19272 void record_line (bool end_sequence);
19273
19274 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19275 nop-out rest of the lines in this sequence. */
19276 void check_line_address (struct dwarf2_cu *cu,
19277 const gdb_byte *line_ptr,
19278 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19279
19280 void handle_set_discriminator (unsigned int discriminator)
19281 {
19282 m_discriminator = discriminator;
19283 m_line_has_non_zero_discriminator |= discriminator != 0;
19284 }
19285
19286 /* Handle DW_LNE_set_address. */
19287 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19288 {
19289 m_op_index = 0;
19290 address += baseaddr;
19291 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19292 }
19293
19294 /* Handle DW_LNS_advance_pc. */
19295 void handle_advance_pc (CORE_ADDR adjust);
19296
19297 /* Handle a special opcode. */
19298 void handle_special_opcode (unsigned char op_code);
19299
19300 /* Handle DW_LNS_advance_line. */
19301 void handle_advance_line (int line_delta)
19302 {
19303 advance_line (line_delta);
19304 }
19305
19306 /* Handle DW_LNS_set_file. */
19307 void handle_set_file (file_name_index file);
19308
19309 /* Handle DW_LNS_negate_stmt. */
19310 void handle_negate_stmt ()
19311 {
19312 m_is_stmt = !m_is_stmt;
19313 }
19314
19315 /* Handle DW_LNS_const_add_pc. */
19316 void handle_const_add_pc ();
19317
19318 /* Handle DW_LNS_fixed_advance_pc. */
19319 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19320 {
19321 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19322 m_op_index = 0;
19323 }
19324
19325 /* Handle DW_LNS_copy. */
19326 void handle_copy ()
19327 {
19328 record_line (false);
19329 m_discriminator = 0;
19330 }
19331
19332 /* Handle DW_LNE_end_sequence. */
19333 void handle_end_sequence ()
19334 {
19335 m_currently_recording_lines = true;
19336 }
19337
19338 private:
19339 /* Advance the line by LINE_DELTA. */
19340 void advance_line (int line_delta)
19341 {
19342 m_line += line_delta;
19343
19344 if (line_delta != 0)
19345 m_line_has_non_zero_discriminator = m_discriminator != 0;
19346 }
19347
19348 struct dwarf2_cu *m_cu;
19349
19350 gdbarch *m_gdbarch;
19351
19352 /* True if we're recording lines.
19353 Otherwise we're building partial symtabs and are just interested in
19354 finding include files mentioned by the line number program. */
19355 bool m_record_lines_p;
19356
19357 /* The line number header. */
19358 line_header *m_line_header;
19359
19360 /* These are part of the standard DWARF line number state machine,
19361 and initialized according to the DWARF spec. */
19362
19363 unsigned char m_op_index = 0;
19364 /* The line table index of the current file. */
19365 file_name_index m_file = 1;
19366 unsigned int m_line = 1;
19367
19368 /* These are initialized in the constructor. */
19369
19370 CORE_ADDR m_address;
19371 bool m_is_stmt;
19372 unsigned int m_discriminator;
19373
19374 /* Additional bits of state we need to track. */
19375
19376 /* The last file that we called dwarf2_start_subfile for.
19377 This is only used for TLLs. */
19378 unsigned int m_last_file = 0;
19379 /* The last file a line number was recorded for. */
19380 struct subfile *m_last_subfile = NULL;
19381
19382 /* When true, record the lines we decode. */
19383 bool m_currently_recording_lines = false;
19384
19385 /* The last line number that was recorded, used to coalesce
19386 consecutive entries for the same line. This can happen, for
19387 example, when discriminators are present. PR 17276. */
19388 unsigned int m_last_line = 0;
19389 bool m_line_has_non_zero_discriminator = false;
19390 };
19391
19392 void
19393 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19394 {
19395 CORE_ADDR addr_adj = (((m_op_index + adjust)
19396 / m_line_header->maximum_ops_per_instruction)
19397 * m_line_header->minimum_instruction_length);
19398 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19399 m_op_index = ((m_op_index + adjust)
19400 % m_line_header->maximum_ops_per_instruction);
19401 }
19402
19403 void
19404 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19405 {
19406 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19407 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19408 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19409 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19410 / m_line_header->maximum_ops_per_instruction)
19411 * m_line_header->minimum_instruction_length);
19412 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19413 m_op_index = ((m_op_index + adj_opcode_d)
19414 % m_line_header->maximum_ops_per_instruction);
19415
19416 int line_delta = m_line_header->line_base + adj_opcode_r;
19417 advance_line (line_delta);
19418 record_line (false);
19419 m_discriminator = 0;
19420 }
19421
19422 void
19423 lnp_state_machine::handle_set_file (file_name_index file)
19424 {
19425 m_file = file;
19426
19427 const file_entry *fe = current_file ();
19428 if (fe == NULL)
19429 dwarf2_debug_line_missing_file_complaint ();
19430 else if (m_record_lines_p)
19431 {
19432 const char *dir = fe->include_dir (m_line_header);
19433
19434 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19435 m_line_has_non_zero_discriminator = m_discriminator != 0;
19436 dwarf2_start_subfile (m_cu, fe->name, dir);
19437 }
19438 }
19439
19440 void
19441 lnp_state_machine::handle_const_add_pc ()
19442 {
19443 CORE_ADDR adjust
19444 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19445
19446 CORE_ADDR addr_adj
19447 = (((m_op_index + adjust)
19448 / m_line_header->maximum_ops_per_instruction)
19449 * m_line_header->minimum_instruction_length);
19450
19451 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19452 m_op_index = ((m_op_index + adjust)
19453 % m_line_header->maximum_ops_per_instruction);
19454 }
19455
19456 /* Return non-zero if we should add LINE to the line number table.
19457 LINE is the line to add, LAST_LINE is the last line that was added,
19458 LAST_SUBFILE is the subfile for LAST_LINE.
19459 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19460 had a non-zero discriminator.
19461
19462 We have to be careful in the presence of discriminators.
19463 E.g., for this line:
19464
19465 for (i = 0; i < 100000; i++);
19466
19467 clang can emit four line number entries for that one line,
19468 each with a different discriminator.
19469 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19470
19471 However, we want gdb to coalesce all four entries into one.
19472 Otherwise the user could stepi into the middle of the line and
19473 gdb would get confused about whether the pc really was in the
19474 middle of the line.
19475
19476 Things are further complicated by the fact that two consecutive
19477 line number entries for the same line is a heuristic used by gcc
19478 to denote the end of the prologue. So we can't just discard duplicate
19479 entries, we have to be selective about it. The heuristic we use is
19480 that we only collapse consecutive entries for the same line if at least
19481 one of those entries has a non-zero discriminator. PR 17276.
19482
19483 Note: Addresses in the line number state machine can never go backwards
19484 within one sequence, thus this coalescing is ok. */
19485
19486 static int
19487 dwarf_record_line_p (struct dwarf2_cu *cu,
19488 unsigned int line, unsigned int last_line,
19489 int line_has_non_zero_discriminator,
19490 struct subfile *last_subfile)
19491 {
19492 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19493 return 1;
19494 if (line != last_line)
19495 return 1;
19496 /* Same line for the same file that we've seen already.
19497 As a last check, for pr 17276, only record the line if the line
19498 has never had a non-zero discriminator. */
19499 if (!line_has_non_zero_discriminator)
19500 return 1;
19501 return 0;
19502 }
19503
19504 /* Use the CU's builder to record line number LINE beginning at
19505 address ADDRESS in the line table of subfile SUBFILE. */
19506
19507 static void
19508 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19509 unsigned int line, CORE_ADDR address, bool is_stmt,
19510 struct dwarf2_cu *cu)
19511 {
19512 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19513
19514 if (dwarf_line_debug)
19515 {
19516 fprintf_unfiltered (gdb_stdlog,
19517 "Recording line %u, file %s, address %s\n",
19518 line, lbasename (subfile->name),
19519 paddress (gdbarch, address));
19520 }
19521
19522 if (cu != nullptr)
19523 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19524 }
19525
19526 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19527 Mark the end of a set of line number records.
19528 The arguments are the same as for dwarf_record_line_1.
19529 If SUBFILE is NULL the request is ignored. */
19530
19531 static void
19532 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19533 CORE_ADDR address, struct dwarf2_cu *cu)
19534 {
19535 if (subfile == NULL)
19536 return;
19537
19538 if (dwarf_line_debug)
19539 {
19540 fprintf_unfiltered (gdb_stdlog,
19541 "Finishing current line, file %s, address %s\n",
19542 lbasename (subfile->name),
19543 paddress (gdbarch, address));
19544 }
19545
19546 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19547 }
19548
19549 void
19550 lnp_state_machine::record_line (bool end_sequence)
19551 {
19552 if (dwarf_line_debug)
19553 {
19554 fprintf_unfiltered (gdb_stdlog,
19555 "Processing actual line %u: file %u,"
19556 " address %s, is_stmt %u, discrim %u%s\n",
19557 m_line, m_file,
19558 paddress (m_gdbarch, m_address),
19559 m_is_stmt, m_discriminator,
19560 (end_sequence ? "\t(end sequence)" : ""));
19561 }
19562
19563 file_entry *fe = current_file ();
19564
19565 if (fe == NULL)
19566 dwarf2_debug_line_missing_file_complaint ();
19567 /* For now we ignore lines not starting on an instruction boundary.
19568 But not when processing end_sequence for compatibility with the
19569 previous version of the code. */
19570 else if (m_op_index == 0 || end_sequence)
19571 {
19572 fe->included_p = 1;
19573 if (m_record_lines_p)
19574 {
19575 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19576 || end_sequence)
19577 {
19578 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19579 m_currently_recording_lines ? m_cu : nullptr);
19580 }
19581
19582 if (!end_sequence)
19583 {
19584 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19585
19586 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19587 m_line_has_non_zero_discriminator,
19588 m_last_subfile))
19589 {
19590 buildsym_compunit *builder = m_cu->get_builder ();
19591 dwarf_record_line_1 (m_gdbarch,
19592 builder->get_current_subfile (),
19593 m_line, m_address, is_stmt,
19594 m_currently_recording_lines ? m_cu : nullptr);
19595 }
19596 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19597 m_last_line = m_line;
19598 }
19599 }
19600 }
19601 }
19602
19603 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19604 line_header *lh, bool record_lines_p)
19605 {
19606 m_cu = cu;
19607 m_gdbarch = arch;
19608 m_record_lines_p = record_lines_p;
19609 m_line_header = lh;
19610
19611 m_currently_recording_lines = true;
19612
19613 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19614 was a line entry for it so that the backend has a chance to adjust it
19615 and also record it in case it needs it. This is currently used by MIPS
19616 code, cf. `mips_adjust_dwarf2_line'. */
19617 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19618 m_is_stmt = lh->default_is_stmt;
19619 m_discriminator = 0;
19620 }
19621
19622 void
19623 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19624 const gdb_byte *line_ptr,
19625 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19626 {
19627 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19628 the pc range of the CU. However, we restrict the test to only ADDRESS
19629 values of zero to preserve GDB's previous behaviour which is to handle
19630 the specific case of a function being GC'd by the linker. */
19631
19632 if (address == 0 && address < unrelocated_lowpc)
19633 {
19634 /* This line table is for a function which has been
19635 GCd by the linker. Ignore it. PR gdb/12528 */
19636
19637 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19638 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19639
19640 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19641 line_offset, objfile_name (objfile));
19642 m_currently_recording_lines = false;
19643 /* Note: m_currently_recording_lines is left as false until we see
19644 DW_LNE_end_sequence. */
19645 }
19646 }
19647
19648 /* Subroutine of dwarf_decode_lines to simplify it.
19649 Process the line number information in LH.
19650 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19651 program in order to set included_p for every referenced header. */
19652
19653 static void
19654 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19655 const int decode_for_pst_p, CORE_ADDR lowpc)
19656 {
19657 const gdb_byte *line_ptr, *extended_end;
19658 const gdb_byte *line_end;
19659 unsigned int bytes_read, extended_len;
19660 unsigned char op_code, extended_op;
19661 CORE_ADDR baseaddr;
19662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19663 bfd *abfd = objfile->obfd;
19664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19665 /* True if we're recording line info (as opposed to building partial
19666 symtabs and just interested in finding include files mentioned by
19667 the line number program). */
19668 bool record_lines_p = !decode_for_pst_p;
19669
19670 baseaddr = objfile->text_section_offset ();
19671
19672 line_ptr = lh->statement_program_start;
19673 line_end = lh->statement_program_end;
19674
19675 /* Read the statement sequences until there's nothing left. */
19676 while (line_ptr < line_end)
19677 {
19678 /* The DWARF line number program state machine. Reset the state
19679 machine at the start of each sequence. */
19680 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19681 bool end_sequence = false;
19682
19683 if (record_lines_p)
19684 {
19685 /* Start a subfile for the current file of the state
19686 machine. */
19687 const file_entry *fe = state_machine.current_file ();
19688
19689 if (fe != NULL)
19690 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19691 }
19692
19693 /* Decode the table. */
19694 while (line_ptr < line_end && !end_sequence)
19695 {
19696 op_code = read_1_byte (abfd, line_ptr);
19697 line_ptr += 1;
19698
19699 if (op_code >= lh->opcode_base)
19700 {
19701 /* Special opcode. */
19702 state_machine.handle_special_opcode (op_code);
19703 }
19704 else switch (op_code)
19705 {
19706 case DW_LNS_extended_op:
19707 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19708 &bytes_read);
19709 line_ptr += bytes_read;
19710 extended_end = line_ptr + extended_len;
19711 extended_op = read_1_byte (abfd, line_ptr);
19712 line_ptr += 1;
19713 switch (extended_op)
19714 {
19715 case DW_LNE_end_sequence:
19716 state_machine.handle_end_sequence ();
19717 end_sequence = true;
19718 break;
19719 case DW_LNE_set_address:
19720 {
19721 CORE_ADDR address
19722 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19723 line_ptr += bytes_read;
19724
19725 state_machine.check_line_address (cu, line_ptr,
19726 lowpc - baseaddr, address);
19727 state_machine.handle_set_address (baseaddr, address);
19728 }
19729 break;
19730 case DW_LNE_define_file:
19731 {
19732 const char *cur_file;
19733 unsigned int mod_time, length;
19734 dir_index dindex;
19735
19736 cur_file = read_direct_string (abfd, line_ptr,
19737 &bytes_read);
19738 line_ptr += bytes_read;
19739 dindex = (dir_index)
19740 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19741 line_ptr += bytes_read;
19742 mod_time =
19743 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19744 line_ptr += bytes_read;
19745 length =
19746 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19747 line_ptr += bytes_read;
19748 lh->add_file_name (cur_file, dindex, mod_time, length);
19749 }
19750 break;
19751 case DW_LNE_set_discriminator:
19752 {
19753 /* The discriminator is not interesting to the
19754 debugger; just ignore it. We still need to
19755 check its value though:
19756 if there are consecutive entries for the same
19757 (non-prologue) line we want to coalesce them.
19758 PR 17276. */
19759 unsigned int discr
19760 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19761 line_ptr += bytes_read;
19762
19763 state_machine.handle_set_discriminator (discr);
19764 }
19765 break;
19766 default:
19767 complaint (_("mangled .debug_line section"));
19768 return;
19769 }
19770 /* Make sure that we parsed the extended op correctly. If e.g.
19771 we expected a different address size than the producer used,
19772 we may have read the wrong number of bytes. */
19773 if (line_ptr != extended_end)
19774 {
19775 complaint (_("mangled .debug_line section"));
19776 return;
19777 }
19778 break;
19779 case DW_LNS_copy:
19780 state_machine.handle_copy ();
19781 break;
19782 case DW_LNS_advance_pc:
19783 {
19784 CORE_ADDR adjust
19785 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19786 line_ptr += bytes_read;
19787
19788 state_machine.handle_advance_pc (adjust);
19789 }
19790 break;
19791 case DW_LNS_advance_line:
19792 {
19793 int line_delta
19794 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19795 line_ptr += bytes_read;
19796
19797 state_machine.handle_advance_line (line_delta);
19798 }
19799 break;
19800 case DW_LNS_set_file:
19801 {
19802 file_name_index file
19803 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19804 &bytes_read);
19805 line_ptr += bytes_read;
19806
19807 state_machine.handle_set_file (file);
19808 }
19809 break;
19810 case DW_LNS_set_column:
19811 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19812 line_ptr += bytes_read;
19813 break;
19814 case DW_LNS_negate_stmt:
19815 state_machine.handle_negate_stmt ();
19816 break;
19817 case DW_LNS_set_basic_block:
19818 break;
19819 /* Add to the address register of the state machine the
19820 address increment value corresponding to special opcode
19821 255. I.e., this value is scaled by the minimum
19822 instruction length since special opcode 255 would have
19823 scaled the increment. */
19824 case DW_LNS_const_add_pc:
19825 state_machine.handle_const_add_pc ();
19826 break;
19827 case DW_LNS_fixed_advance_pc:
19828 {
19829 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19830 line_ptr += 2;
19831
19832 state_machine.handle_fixed_advance_pc (addr_adj);
19833 }
19834 break;
19835 default:
19836 {
19837 /* Unknown standard opcode, ignore it. */
19838 int i;
19839
19840 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19841 {
19842 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19843 line_ptr += bytes_read;
19844 }
19845 }
19846 }
19847 }
19848
19849 if (!end_sequence)
19850 dwarf2_debug_line_missing_end_sequence_complaint ();
19851
19852 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19853 in which case we still finish recording the last line). */
19854 state_machine.record_line (true);
19855 }
19856 }
19857
19858 /* Decode the Line Number Program (LNP) for the given line_header
19859 structure and CU. The actual information extracted and the type
19860 of structures created from the LNP depends on the value of PST.
19861
19862 1. If PST is NULL, then this procedure uses the data from the program
19863 to create all necessary symbol tables, and their linetables.
19864
19865 2. If PST is not NULL, this procedure reads the program to determine
19866 the list of files included by the unit represented by PST, and
19867 builds all the associated partial symbol tables.
19868
19869 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19870 It is used for relative paths in the line table.
19871 NOTE: When processing partial symtabs (pst != NULL),
19872 comp_dir == pst->dirname.
19873
19874 NOTE: It is important that psymtabs have the same file name (via strcmp)
19875 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19876 symtab we don't use it in the name of the psymtabs we create.
19877 E.g. expand_line_sal requires this when finding psymtabs to expand.
19878 A good testcase for this is mb-inline.exp.
19879
19880 LOWPC is the lowest address in CU (or 0 if not known).
19881
19882 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19883 for its PC<->lines mapping information. Otherwise only the filename
19884 table is read in. */
19885
19886 static void
19887 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19888 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19889 CORE_ADDR lowpc, int decode_mapping)
19890 {
19891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19892 const int decode_for_pst_p = (pst != NULL);
19893
19894 if (decode_mapping)
19895 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19896
19897 if (decode_for_pst_p)
19898 {
19899 /* Now that we're done scanning the Line Header Program, we can
19900 create the psymtab of each included file. */
19901 for (auto &file_entry : lh->file_names ())
19902 if (file_entry.included_p == 1)
19903 {
19904 gdb::unique_xmalloc_ptr<char> name_holder;
19905 const char *include_name =
19906 psymtab_include_file_name (lh, file_entry, pst,
19907 comp_dir, &name_holder);
19908 if (include_name != NULL)
19909 dwarf2_create_include_psymtab (include_name, pst, objfile);
19910 }
19911 }
19912 else
19913 {
19914 /* Make sure a symtab is created for every file, even files
19915 which contain only variables (i.e. no code with associated
19916 line numbers). */
19917 buildsym_compunit *builder = cu->get_builder ();
19918 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19919
19920 for (auto &fe : lh->file_names ())
19921 {
19922 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19923 if (builder->get_current_subfile ()->symtab == NULL)
19924 {
19925 builder->get_current_subfile ()->symtab
19926 = allocate_symtab (cust,
19927 builder->get_current_subfile ()->name);
19928 }
19929 fe.symtab = builder->get_current_subfile ()->symtab;
19930 }
19931 }
19932 }
19933
19934 /* Start a subfile for DWARF. FILENAME is the name of the file and
19935 DIRNAME the name of the source directory which contains FILENAME
19936 or NULL if not known.
19937 This routine tries to keep line numbers from identical absolute and
19938 relative file names in a common subfile.
19939
19940 Using the `list' example from the GDB testsuite, which resides in
19941 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19942 of /srcdir/list0.c yields the following debugging information for list0.c:
19943
19944 DW_AT_name: /srcdir/list0.c
19945 DW_AT_comp_dir: /compdir
19946 files.files[0].name: list0.h
19947 files.files[0].dir: /srcdir
19948 files.files[1].name: list0.c
19949 files.files[1].dir: /srcdir
19950
19951 The line number information for list0.c has to end up in a single
19952 subfile, so that `break /srcdir/list0.c:1' works as expected.
19953 start_subfile will ensure that this happens provided that we pass the
19954 concatenation of files.files[1].dir and files.files[1].name as the
19955 subfile's name. */
19956
19957 static void
19958 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19959 const char *dirname)
19960 {
19961 gdb::unique_xmalloc_ptr<char> copy;
19962
19963 /* In order not to lose the line information directory,
19964 we concatenate it to the filename when it makes sense.
19965 Note that the Dwarf3 standard says (speaking of filenames in line
19966 information): ``The directory index is ignored for file names
19967 that represent full path names''. Thus ignoring dirname in the
19968 `else' branch below isn't an issue. */
19969
19970 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19971 {
19972 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19973 filename = copy.get ();
19974 }
19975
19976 cu->get_builder ()->start_subfile (filename);
19977 }
19978
19979 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19980 buildsym_compunit constructor. */
19981
19982 struct compunit_symtab *
19983 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19984 CORE_ADDR low_pc)
19985 {
19986 gdb_assert (m_builder == nullptr);
19987
19988 m_builder.reset (new struct buildsym_compunit
19989 (per_cu->dwarf2_per_objfile->objfile,
19990 name, comp_dir, language, low_pc));
19991
19992 list_in_scope = get_builder ()->get_file_symbols ();
19993
19994 get_builder ()->record_debugformat ("DWARF 2");
19995 get_builder ()->record_producer (producer);
19996
19997 processing_has_namespace_info = false;
19998
19999 return get_builder ()->get_compunit_symtab ();
20000 }
20001
20002 static void
20003 var_decode_location (struct attribute *attr, struct symbol *sym,
20004 struct dwarf2_cu *cu)
20005 {
20006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20007 struct comp_unit_head *cu_header = &cu->header;
20008
20009 /* NOTE drow/2003-01-30: There used to be a comment and some special
20010 code here to turn a symbol with DW_AT_external and a
20011 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20012 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20013 with some versions of binutils) where shared libraries could have
20014 relocations against symbols in their debug information - the
20015 minimal symbol would have the right address, but the debug info
20016 would not. It's no longer necessary, because we will explicitly
20017 apply relocations when we read in the debug information now. */
20018
20019 /* A DW_AT_location attribute with no contents indicates that a
20020 variable has been optimized away. */
20021 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20022 {
20023 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20024 return;
20025 }
20026
20027 /* Handle one degenerate form of location expression specially, to
20028 preserve GDB's previous behavior when section offsets are
20029 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20030 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20031
20032 if (attr->form_is_block ()
20033 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20034 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20035 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20036 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20037 && (DW_BLOCK (attr)->size
20038 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20039 {
20040 unsigned int dummy;
20041
20042 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20043 SET_SYMBOL_VALUE_ADDRESS
20044 (sym, cu->header.read_address (objfile->obfd,
20045 DW_BLOCK (attr)->data + 1,
20046 &dummy));
20047 else
20048 SET_SYMBOL_VALUE_ADDRESS
20049 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20050 &dummy));
20051 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20052 fixup_symbol_section (sym, objfile);
20053 SET_SYMBOL_VALUE_ADDRESS
20054 (sym,
20055 SYMBOL_VALUE_ADDRESS (sym)
20056 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20057 return;
20058 }
20059
20060 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20061 expression evaluator, and use LOC_COMPUTED only when necessary
20062 (i.e. when the value of a register or memory location is
20063 referenced, or a thread-local block, etc.). Then again, it might
20064 not be worthwhile. I'm assuming that it isn't unless performance
20065 or memory numbers show me otherwise. */
20066
20067 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20068
20069 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20070 cu->has_loclist = true;
20071 }
20072
20073 /* Given a pointer to a DWARF information entry, figure out if we need
20074 to make a symbol table entry for it, and if so, create a new entry
20075 and return a pointer to it.
20076 If TYPE is NULL, determine symbol type from the die, otherwise
20077 used the passed type.
20078 If SPACE is not NULL, use it to hold the new symbol. If it is
20079 NULL, allocate a new symbol on the objfile's obstack. */
20080
20081 static struct symbol *
20082 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20083 struct symbol *space)
20084 {
20085 struct dwarf2_per_objfile *dwarf2_per_objfile
20086 = cu->per_cu->dwarf2_per_objfile;
20087 struct objfile *objfile = dwarf2_per_objfile->objfile;
20088 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20089 struct symbol *sym = NULL;
20090 const char *name;
20091 struct attribute *attr = NULL;
20092 struct attribute *attr2 = NULL;
20093 CORE_ADDR baseaddr;
20094 struct pending **list_to_add = NULL;
20095
20096 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20097
20098 baseaddr = objfile->text_section_offset ();
20099
20100 name = dwarf2_name (die, cu);
20101 if (name)
20102 {
20103 const char *linkagename;
20104 int suppress_add = 0;
20105
20106 if (space)
20107 sym = space;
20108 else
20109 sym = allocate_symbol (objfile);
20110 OBJSTAT (objfile, n_syms++);
20111
20112 /* Cache this symbol's name and the name's demangled form (if any). */
20113 sym->set_language (cu->language, &objfile->objfile_obstack);
20114 linkagename = dwarf2_physname (name, die, cu);
20115 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20116
20117 /* Fortran does not have mangling standard and the mangling does differ
20118 between gfortran, iFort etc. */
20119 if (cu->language == language_fortran
20120 && symbol_get_demangled_name (sym) == NULL)
20121 symbol_set_demangled_name (sym,
20122 dwarf2_full_name (name, die, cu),
20123 NULL);
20124
20125 /* Default assumptions.
20126 Use the passed type or decode it from the die. */
20127 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20128 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20129 if (type != NULL)
20130 SYMBOL_TYPE (sym) = type;
20131 else
20132 SYMBOL_TYPE (sym) = die_type (die, cu);
20133 attr = dwarf2_attr (die,
20134 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20135 cu);
20136 if (attr != nullptr)
20137 {
20138 SYMBOL_LINE (sym) = DW_UNSND (attr);
20139 }
20140
20141 attr = dwarf2_attr (die,
20142 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20143 cu);
20144 if (attr != nullptr)
20145 {
20146 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20147 struct file_entry *fe;
20148
20149 if (cu->line_header != NULL)
20150 fe = cu->line_header->file_name_at (file_index);
20151 else
20152 fe = NULL;
20153
20154 if (fe == NULL)
20155 complaint (_("file index out of range"));
20156 else
20157 symbol_set_symtab (sym, fe->symtab);
20158 }
20159
20160 switch (die->tag)
20161 {
20162 case DW_TAG_label:
20163 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20164 if (attr != nullptr)
20165 {
20166 CORE_ADDR addr;
20167
20168 addr = attr->value_as_address ();
20169 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20170 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20171 }
20172 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20173 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20174 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20175 add_symbol_to_list (sym, cu->list_in_scope);
20176 break;
20177 case DW_TAG_subprogram:
20178 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20179 finish_block. */
20180 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20181 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20182 if ((attr2 && (DW_UNSND (attr2) != 0))
20183 || cu->language == language_ada
20184 || cu->language == language_fortran)
20185 {
20186 /* Subprograms marked external are stored as a global symbol.
20187 Ada and Fortran subprograms, whether marked external or
20188 not, are always stored as a global symbol, because we want
20189 to be able to access them globally. For instance, we want
20190 to be able to break on a nested subprogram without having
20191 to specify the context. */
20192 list_to_add = cu->get_builder ()->get_global_symbols ();
20193 }
20194 else
20195 {
20196 list_to_add = cu->list_in_scope;
20197 }
20198 break;
20199 case DW_TAG_inlined_subroutine:
20200 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20201 finish_block. */
20202 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20203 SYMBOL_INLINED (sym) = 1;
20204 list_to_add = cu->list_in_scope;
20205 break;
20206 case DW_TAG_template_value_param:
20207 suppress_add = 1;
20208 /* Fall through. */
20209 case DW_TAG_constant:
20210 case DW_TAG_variable:
20211 case DW_TAG_member:
20212 /* Compilation with minimal debug info may result in
20213 variables with missing type entries. Change the
20214 misleading `void' type to something sensible. */
20215 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20216 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20217
20218 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20219 /* In the case of DW_TAG_member, we should only be called for
20220 static const members. */
20221 if (die->tag == DW_TAG_member)
20222 {
20223 /* dwarf2_add_field uses die_is_declaration,
20224 so we do the same. */
20225 gdb_assert (die_is_declaration (die, cu));
20226 gdb_assert (attr);
20227 }
20228 if (attr != nullptr)
20229 {
20230 dwarf2_const_value (attr, sym, cu);
20231 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20232 if (!suppress_add)
20233 {
20234 if (attr2 && (DW_UNSND (attr2) != 0))
20235 list_to_add = cu->get_builder ()->get_global_symbols ();
20236 else
20237 list_to_add = cu->list_in_scope;
20238 }
20239 break;
20240 }
20241 attr = dwarf2_attr (die, DW_AT_location, cu);
20242 if (attr != nullptr)
20243 {
20244 var_decode_location (attr, sym, cu);
20245 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20246
20247 /* Fortran explicitly imports any global symbols to the local
20248 scope by DW_TAG_common_block. */
20249 if (cu->language == language_fortran && die->parent
20250 && die->parent->tag == DW_TAG_common_block)
20251 attr2 = NULL;
20252
20253 if (SYMBOL_CLASS (sym) == LOC_STATIC
20254 && SYMBOL_VALUE_ADDRESS (sym) == 0
20255 && !dwarf2_per_objfile->has_section_at_zero)
20256 {
20257 /* When a static variable is eliminated by the linker,
20258 the corresponding debug information is not stripped
20259 out, but the variable address is set to null;
20260 do not add such variables into symbol table. */
20261 }
20262 else if (attr2 && (DW_UNSND (attr2) != 0))
20263 {
20264 if (SYMBOL_CLASS (sym) == LOC_STATIC
20265 && (objfile->flags & OBJF_MAINLINE) == 0
20266 && dwarf2_per_objfile->can_copy)
20267 {
20268 /* A global static variable might be subject to
20269 copy relocation. We first check for a local
20270 minsym, though, because maybe the symbol was
20271 marked hidden, in which case this would not
20272 apply. */
20273 bound_minimal_symbol found
20274 = (lookup_minimal_symbol_linkage
20275 (sym->linkage_name (), objfile));
20276 if (found.minsym != nullptr)
20277 sym->maybe_copied = 1;
20278 }
20279
20280 /* A variable with DW_AT_external is never static,
20281 but it may be block-scoped. */
20282 list_to_add
20283 = ((cu->list_in_scope
20284 == cu->get_builder ()->get_file_symbols ())
20285 ? cu->get_builder ()->get_global_symbols ()
20286 : cu->list_in_scope);
20287 }
20288 else
20289 list_to_add = cu->list_in_scope;
20290 }
20291 else
20292 {
20293 /* We do not know the address of this symbol.
20294 If it is an external symbol and we have type information
20295 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20296 The address of the variable will then be determined from
20297 the minimal symbol table whenever the variable is
20298 referenced. */
20299 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20300
20301 /* Fortran explicitly imports any global symbols to the local
20302 scope by DW_TAG_common_block. */
20303 if (cu->language == language_fortran && die->parent
20304 && die->parent->tag == DW_TAG_common_block)
20305 {
20306 /* SYMBOL_CLASS doesn't matter here because
20307 read_common_block is going to reset it. */
20308 if (!suppress_add)
20309 list_to_add = cu->list_in_scope;
20310 }
20311 else if (attr2 && (DW_UNSND (attr2) != 0)
20312 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20313 {
20314 /* A variable with DW_AT_external is never static, but it
20315 may be block-scoped. */
20316 list_to_add
20317 = ((cu->list_in_scope
20318 == cu->get_builder ()->get_file_symbols ())
20319 ? cu->get_builder ()->get_global_symbols ()
20320 : cu->list_in_scope);
20321
20322 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20323 }
20324 else if (!die_is_declaration (die, cu))
20325 {
20326 /* Use the default LOC_OPTIMIZED_OUT class. */
20327 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20328 if (!suppress_add)
20329 list_to_add = cu->list_in_scope;
20330 }
20331 }
20332 break;
20333 case DW_TAG_formal_parameter:
20334 {
20335 /* If we are inside a function, mark this as an argument. If
20336 not, we might be looking at an argument to an inlined function
20337 when we do not have enough information to show inlined frames;
20338 pretend it's a local variable in that case so that the user can
20339 still see it. */
20340 struct context_stack *curr
20341 = cu->get_builder ()->get_current_context_stack ();
20342 if (curr != nullptr && curr->name != nullptr)
20343 SYMBOL_IS_ARGUMENT (sym) = 1;
20344 attr = dwarf2_attr (die, DW_AT_location, cu);
20345 if (attr != nullptr)
20346 {
20347 var_decode_location (attr, sym, cu);
20348 }
20349 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20350 if (attr != nullptr)
20351 {
20352 dwarf2_const_value (attr, sym, cu);
20353 }
20354
20355 list_to_add = cu->list_in_scope;
20356 }
20357 break;
20358 case DW_TAG_unspecified_parameters:
20359 /* From varargs functions; gdb doesn't seem to have any
20360 interest in this information, so just ignore it for now.
20361 (FIXME?) */
20362 break;
20363 case DW_TAG_template_type_param:
20364 suppress_add = 1;
20365 /* Fall through. */
20366 case DW_TAG_class_type:
20367 case DW_TAG_interface_type:
20368 case DW_TAG_structure_type:
20369 case DW_TAG_union_type:
20370 case DW_TAG_set_type:
20371 case DW_TAG_enumeration_type:
20372 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20373 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20374
20375 {
20376 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20377 really ever be static objects: otherwise, if you try
20378 to, say, break of a class's method and you're in a file
20379 which doesn't mention that class, it won't work unless
20380 the check for all static symbols in lookup_symbol_aux
20381 saves you. See the OtherFileClass tests in
20382 gdb.c++/namespace.exp. */
20383
20384 if (!suppress_add)
20385 {
20386 buildsym_compunit *builder = cu->get_builder ();
20387 list_to_add
20388 = (cu->list_in_scope == builder->get_file_symbols ()
20389 && cu->language == language_cplus
20390 ? builder->get_global_symbols ()
20391 : cu->list_in_scope);
20392
20393 /* The semantics of C++ state that "struct foo {
20394 ... }" also defines a typedef for "foo". */
20395 if (cu->language == language_cplus
20396 || cu->language == language_ada
20397 || cu->language == language_d
20398 || cu->language == language_rust)
20399 {
20400 /* The symbol's name is already allocated along
20401 with this objfile, so we don't need to
20402 duplicate it for the type. */
20403 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20404 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20405 }
20406 }
20407 }
20408 break;
20409 case DW_TAG_typedef:
20410 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20411 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20412 list_to_add = cu->list_in_scope;
20413 break;
20414 case DW_TAG_base_type:
20415 case DW_TAG_subrange_type:
20416 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20417 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20418 list_to_add = cu->list_in_scope;
20419 break;
20420 case DW_TAG_enumerator:
20421 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20422 if (attr != nullptr)
20423 {
20424 dwarf2_const_value (attr, sym, cu);
20425 }
20426 {
20427 /* NOTE: carlton/2003-11-10: See comment above in the
20428 DW_TAG_class_type, etc. block. */
20429
20430 list_to_add
20431 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20432 && cu->language == language_cplus
20433 ? cu->get_builder ()->get_global_symbols ()
20434 : cu->list_in_scope);
20435 }
20436 break;
20437 case DW_TAG_imported_declaration:
20438 case DW_TAG_namespace:
20439 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20440 list_to_add = cu->get_builder ()->get_global_symbols ();
20441 break;
20442 case DW_TAG_module:
20443 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20444 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20445 list_to_add = cu->get_builder ()->get_global_symbols ();
20446 break;
20447 case DW_TAG_common_block:
20448 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20449 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20450 add_symbol_to_list (sym, cu->list_in_scope);
20451 break;
20452 default:
20453 /* Not a tag we recognize. Hopefully we aren't processing
20454 trash data, but since we must specifically ignore things
20455 we don't recognize, there is nothing else we should do at
20456 this point. */
20457 complaint (_("unsupported tag: '%s'"),
20458 dwarf_tag_name (die->tag));
20459 break;
20460 }
20461
20462 if (suppress_add)
20463 {
20464 sym->hash_next = objfile->template_symbols;
20465 objfile->template_symbols = sym;
20466 list_to_add = NULL;
20467 }
20468
20469 if (list_to_add != NULL)
20470 add_symbol_to_list (sym, list_to_add);
20471
20472 /* For the benefit of old versions of GCC, check for anonymous
20473 namespaces based on the demangled name. */
20474 if (!cu->processing_has_namespace_info
20475 && cu->language == language_cplus)
20476 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20477 }
20478 return (sym);
20479 }
20480
20481 /* Given an attr with a DW_FORM_dataN value in host byte order,
20482 zero-extend it as appropriate for the symbol's type. The DWARF
20483 standard (v4) is not entirely clear about the meaning of using
20484 DW_FORM_dataN for a constant with a signed type, where the type is
20485 wider than the data. The conclusion of a discussion on the DWARF
20486 list was that this is unspecified. We choose to always zero-extend
20487 because that is the interpretation long in use by GCC. */
20488
20489 static gdb_byte *
20490 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20491 struct dwarf2_cu *cu, LONGEST *value, int bits)
20492 {
20493 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20494 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20495 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20496 LONGEST l = DW_UNSND (attr);
20497
20498 if (bits < sizeof (*value) * 8)
20499 {
20500 l &= ((LONGEST) 1 << bits) - 1;
20501 *value = l;
20502 }
20503 else if (bits == sizeof (*value) * 8)
20504 *value = l;
20505 else
20506 {
20507 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20508 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20509 return bytes;
20510 }
20511
20512 return NULL;
20513 }
20514
20515 /* Read a constant value from an attribute. Either set *VALUE, or if
20516 the value does not fit in *VALUE, set *BYTES - either already
20517 allocated on the objfile obstack, or newly allocated on OBSTACK,
20518 or, set *BATON, if we translated the constant to a location
20519 expression. */
20520
20521 static void
20522 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20523 const char *name, struct obstack *obstack,
20524 struct dwarf2_cu *cu,
20525 LONGEST *value, const gdb_byte **bytes,
20526 struct dwarf2_locexpr_baton **baton)
20527 {
20528 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20529 struct comp_unit_head *cu_header = &cu->header;
20530 struct dwarf_block *blk;
20531 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20532 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20533
20534 *value = 0;
20535 *bytes = NULL;
20536 *baton = NULL;
20537
20538 switch (attr->form)
20539 {
20540 case DW_FORM_addr:
20541 case DW_FORM_addrx:
20542 case DW_FORM_GNU_addr_index:
20543 {
20544 gdb_byte *data;
20545
20546 if (TYPE_LENGTH (type) != cu_header->addr_size)
20547 dwarf2_const_value_length_mismatch_complaint (name,
20548 cu_header->addr_size,
20549 TYPE_LENGTH (type));
20550 /* Symbols of this form are reasonably rare, so we just
20551 piggyback on the existing location code rather than writing
20552 a new implementation of symbol_computed_ops. */
20553 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20554 (*baton)->per_cu = cu->per_cu;
20555 gdb_assert ((*baton)->per_cu);
20556
20557 (*baton)->size = 2 + cu_header->addr_size;
20558 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20559 (*baton)->data = data;
20560
20561 data[0] = DW_OP_addr;
20562 store_unsigned_integer (&data[1], cu_header->addr_size,
20563 byte_order, DW_ADDR (attr));
20564 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20565 }
20566 break;
20567 case DW_FORM_string:
20568 case DW_FORM_strp:
20569 case DW_FORM_strx:
20570 case DW_FORM_GNU_str_index:
20571 case DW_FORM_GNU_strp_alt:
20572 /* DW_STRING is already allocated on the objfile obstack, point
20573 directly to it. */
20574 *bytes = (const gdb_byte *) DW_STRING (attr);
20575 break;
20576 case DW_FORM_block1:
20577 case DW_FORM_block2:
20578 case DW_FORM_block4:
20579 case DW_FORM_block:
20580 case DW_FORM_exprloc:
20581 case DW_FORM_data16:
20582 blk = DW_BLOCK (attr);
20583 if (TYPE_LENGTH (type) != blk->size)
20584 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20585 TYPE_LENGTH (type));
20586 *bytes = blk->data;
20587 break;
20588
20589 /* The DW_AT_const_value attributes are supposed to carry the
20590 symbol's value "represented as it would be on the target
20591 architecture." By the time we get here, it's already been
20592 converted to host endianness, so we just need to sign- or
20593 zero-extend it as appropriate. */
20594 case DW_FORM_data1:
20595 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20596 break;
20597 case DW_FORM_data2:
20598 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20599 break;
20600 case DW_FORM_data4:
20601 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20602 break;
20603 case DW_FORM_data8:
20604 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20605 break;
20606
20607 case DW_FORM_sdata:
20608 case DW_FORM_implicit_const:
20609 *value = DW_SND (attr);
20610 break;
20611
20612 case DW_FORM_udata:
20613 *value = DW_UNSND (attr);
20614 break;
20615
20616 default:
20617 complaint (_("unsupported const value attribute form: '%s'"),
20618 dwarf_form_name (attr->form));
20619 *value = 0;
20620 break;
20621 }
20622 }
20623
20624
20625 /* Copy constant value from an attribute to a symbol. */
20626
20627 static void
20628 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20629 struct dwarf2_cu *cu)
20630 {
20631 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20632 LONGEST value;
20633 const gdb_byte *bytes;
20634 struct dwarf2_locexpr_baton *baton;
20635
20636 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20637 sym->print_name (),
20638 &objfile->objfile_obstack, cu,
20639 &value, &bytes, &baton);
20640
20641 if (baton != NULL)
20642 {
20643 SYMBOL_LOCATION_BATON (sym) = baton;
20644 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20645 }
20646 else if (bytes != NULL)
20647 {
20648 SYMBOL_VALUE_BYTES (sym) = bytes;
20649 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20650 }
20651 else
20652 {
20653 SYMBOL_VALUE (sym) = value;
20654 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20655 }
20656 }
20657
20658 /* Return the type of the die in question using its DW_AT_type attribute. */
20659
20660 static struct type *
20661 die_type (struct die_info *die, struct dwarf2_cu *cu)
20662 {
20663 struct attribute *type_attr;
20664
20665 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20666 if (!type_attr)
20667 {
20668 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20669 /* A missing DW_AT_type represents a void type. */
20670 return objfile_type (objfile)->builtin_void;
20671 }
20672
20673 return lookup_die_type (die, type_attr, cu);
20674 }
20675
20676 /* True iff CU's producer generates GNAT Ada auxiliary information
20677 that allows to find parallel types through that information instead
20678 of having to do expensive parallel lookups by type name. */
20679
20680 static int
20681 need_gnat_info (struct dwarf2_cu *cu)
20682 {
20683 /* Assume that the Ada compiler was GNAT, which always produces
20684 the auxiliary information. */
20685 return (cu->language == language_ada);
20686 }
20687
20688 /* Return the auxiliary type of the die in question using its
20689 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20690 attribute is not present. */
20691
20692 static struct type *
20693 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20694 {
20695 struct attribute *type_attr;
20696
20697 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20698 if (!type_attr)
20699 return NULL;
20700
20701 return lookup_die_type (die, type_attr, cu);
20702 }
20703
20704 /* If DIE has a descriptive_type attribute, then set the TYPE's
20705 descriptive type accordingly. */
20706
20707 static void
20708 set_descriptive_type (struct type *type, struct die_info *die,
20709 struct dwarf2_cu *cu)
20710 {
20711 struct type *descriptive_type = die_descriptive_type (die, cu);
20712
20713 if (descriptive_type)
20714 {
20715 ALLOCATE_GNAT_AUX_TYPE (type);
20716 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20717 }
20718 }
20719
20720 /* Return the containing type of the die in question using its
20721 DW_AT_containing_type attribute. */
20722
20723 static struct type *
20724 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20725 {
20726 struct attribute *type_attr;
20727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20728
20729 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20730 if (!type_attr)
20731 error (_("Dwarf Error: Problem turning containing type into gdb type "
20732 "[in module %s]"), objfile_name (objfile));
20733
20734 return lookup_die_type (die, type_attr, cu);
20735 }
20736
20737 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20738
20739 static struct type *
20740 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20741 {
20742 struct dwarf2_per_objfile *dwarf2_per_objfile
20743 = cu->per_cu->dwarf2_per_objfile;
20744 struct objfile *objfile = dwarf2_per_objfile->objfile;
20745 char *saved;
20746
20747 std::string message
20748 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20749 objfile_name (objfile),
20750 sect_offset_str (cu->header.sect_off),
20751 sect_offset_str (die->sect_off));
20752 saved = obstack_strdup (&objfile->objfile_obstack, message);
20753
20754 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20755 }
20756
20757 /* Look up the type of DIE in CU using its type attribute ATTR.
20758 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20759 DW_AT_containing_type.
20760 If there is no type substitute an error marker. */
20761
20762 static struct type *
20763 lookup_die_type (struct die_info *die, const struct attribute *attr,
20764 struct dwarf2_cu *cu)
20765 {
20766 struct dwarf2_per_objfile *dwarf2_per_objfile
20767 = cu->per_cu->dwarf2_per_objfile;
20768 struct objfile *objfile = dwarf2_per_objfile->objfile;
20769 struct type *this_type;
20770
20771 gdb_assert (attr->name == DW_AT_type
20772 || attr->name == DW_AT_GNAT_descriptive_type
20773 || attr->name == DW_AT_containing_type);
20774
20775 /* First see if we have it cached. */
20776
20777 if (attr->form == DW_FORM_GNU_ref_alt)
20778 {
20779 struct dwarf2_per_cu_data *per_cu;
20780 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20781
20782 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20783 dwarf2_per_objfile);
20784 this_type = get_die_type_at_offset (sect_off, per_cu);
20785 }
20786 else if (attr->form_is_ref ())
20787 {
20788 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20789
20790 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20791 }
20792 else if (attr->form == DW_FORM_ref_sig8)
20793 {
20794 ULONGEST signature = DW_SIGNATURE (attr);
20795
20796 return get_signatured_type (die, signature, cu);
20797 }
20798 else
20799 {
20800 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20801 " at %s [in module %s]"),
20802 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20803 objfile_name (objfile));
20804 return build_error_marker_type (cu, die);
20805 }
20806
20807 /* If not cached we need to read it in. */
20808
20809 if (this_type == NULL)
20810 {
20811 struct die_info *type_die = NULL;
20812 struct dwarf2_cu *type_cu = cu;
20813
20814 if (attr->form_is_ref ())
20815 type_die = follow_die_ref (die, attr, &type_cu);
20816 if (type_die == NULL)
20817 return build_error_marker_type (cu, die);
20818 /* If we find the type now, it's probably because the type came
20819 from an inter-CU reference and the type's CU got expanded before
20820 ours. */
20821 this_type = read_type_die (type_die, type_cu);
20822 }
20823
20824 /* If we still don't have a type use an error marker. */
20825
20826 if (this_type == NULL)
20827 return build_error_marker_type (cu, die);
20828
20829 return this_type;
20830 }
20831
20832 /* Return the type in DIE, CU.
20833 Returns NULL for invalid types.
20834
20835 This first does a lookup in die_type_hash,
20836 and only reads the die in if necessary.
20837
20838 NOTE: This can be called when reading in partial or full symbols. */
20839
20840 static struct type *
20841 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20842 {
20843 struct type *this_type;
20844
20845 this_type = get_die_type (die, cu);
20846 if (this_type)
20847 return this_type;
20848
20849 return read_type_die_1 (die, cu);
20850 }
20851
20852 /* Read the type in DIE, CU.
20853 Returns NULL for invalid types. */
20854
20855 static struct type *
20856 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20857 {
20858 struct type *this_type = NULL;
20859
20860 switch (die->tag)
20861 {
20862 case DW_TAG_class_type:
20863 case DW_TAG_interface_type:
20864 case DW_TAG_structure_type:
20865 case DW_TAG_union_type:
20866 this_type = read_structure_type (die, cu);
20867 break;
20868 case DW_TAG_enumeration_type:
20869 this_type = read_enumeration_type (die, cu);
20870 break;
20871 case DW_TAG_subprogram:
20872 case DW_TAG_subroutine_type:
20873 case DW_TAG_inlined_subroutine:
20874 this_type = read_subroutine_type (die, cu);
20875 break;
20876 case DW_TAG_array_type:
20877 this_type = read_array_type (die, cu);
20878 break;
20879 case DW_TAG_set_type:
20880 this_type = read_set_type (die, cu);
20881 break;
20882 case DW_TAG_pointer_type:
20883 this_type = read_tag_pointer_type (die, cu);
20884 break;
20885 case DW_TAG_ptr_to_member_type:
20886 this_type = read_tag_ptr_to_member_type (die, cu);
20887 break;
20888 case DW_TAG_reference_type:
20889 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20890 break;
20891 case DW_TAG_rvalue_reference_type:
20892 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20893 break;
20894 case DW_TAG_const_type:
20895 this_type = read_tag_const_type (die, cu);
20896 break;
20897 case DW_TAG_volatile_type:
20898 this_type = read_tag_volatile_type (die, cu);
20899 break;
20900 case DW_TAG_restrict_type:
20901 this_type = read_tag_restrict_type (die, cu);
20902 break;
20903 case DW_TAG_string_type:
20904 this_type = read_tag_string_type (die, cu);
20905 break;
20906 case DW_TAG_typedef:
20907 this_type = read_typedef (die, cu);
20908 break;
20909 case DW_TAG_subrange_type:
20910 this_type = read_subrange_type (die, cu);
20911 break;
20912 case DW_TAG_base_type:
20913 this_type = read_base_type (die, cu);
20914 break;
20915 case DW_TAG_unspecified_type:
20916 this_type = read_unspecified_type (die, cu);
20917 break;
20918 case DW_TAG_namespace:
20919 this_type = read_namespace_type (die, cu);
20920 break;
20921 case DW_TAG_module:
20922 this_type = read_module_type (die, cu);
20923 break;
20924 case DW_TAG_atomic_type:
20925 this_type = read_tag_atomic_type (die, cu);
20926 break;
20927 default:
20928 complaint (_("unexpected tag in read_type_die: '%s'"),
20929 dwarf_tag_name (die->tag));
20930 break;
20931 }
20932
20933 return this_type;
20934 }
20935
20936 /* See if we can figure out if the class lives in a namespace. We do
20937 this by looking for a member function; its demangled name will
20938 contain namespace info, if there is any.
20939 Return the computed name or NULL.
20940 Space for the result is allocated on the objfile's obstack.
20941 This is the full-die version of guess_partial_die_structure_name.
20942 In this case we know DIE has no useful parent. */
20943
20944 static const char *
20945 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20946 {
20947 struct die_info *spec_die;
20948 struct dwarf2_cu *spec_cu;
20949 struct die_info *child;
20950 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20951
20952 spec_cu = cu;
20953 spec_die = die_specification (die, &spec_cu);
20954 if (spec_die != NULL)
20955 {
20956 die = spec_die;
20957 cu = spec_cu;
20958 }
20959
20960 for (child = die->child;
20961 child != NULL;
20962 child = child->sibling)
20963 {
20964 if (child->tag == DW_TAG_subprogram)
20965 {
20966 const char *linkage_name = dw2_linkage_name (child, cu);
20967
20968 if (linkage_name != NULL)
20969 {
20970 gdb::unique_xmalloc_ptr<char> actual_name
20971 (language_class_name_from_physname (cu->language_defn,
20972 linkage_name));
20973 const char *name = NULL;
20974
20975 if (actual_name != NULL)
20976 {
20977 const char *die_name = dwarf2_name (die, cu);
20978
20979 if (die_name != NULL
20980 && strcmp (die_name, actual_name.get ()) != 0)
20981 {
20982 /* Strip off the class name from the full name.
20983 We want the prefix. */
20984 int die_name_len = strlen (die_name);
20985 int actual_name_len = strlen (actual_name.get ());
20986 const char *ptr = actual_name.get ();
20987
20988 /* Test for '::' as a sanity check. */
20989 if (actual_name_len > die_name_len + 2
20990 && ptr[actual_name_len - die_name_len - 1] == ':')
20991 name = obstack_strndup (
20992 &objfile->per_bfd->storage_obstack,
20993 ptr, actual_name_len - die_name_len - 2);
20994 }
20995 }
20996 return name;
20997 }
20998 }
20999 }
21000
21001 return NULL;
21002 }
21003
21004 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21005 prefix part in such case. See
21006 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21007
21008 static const char *
21009 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21010 {
21011 struct attribute *attr;
21012 const char *base;
21013
21014 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21015 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21016 return NULL;
21017
21018 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21019 return NULL;
21020
21021 attr = dw2_linkage_name_attr (die, cu);
21022 if (attr == NULL || DW_STRING (attr) == NULL)
21023 return NULL;
21024
21025 /* dwarf2_name had to be already called. */
21026 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21027
21028 /* Strip the base name, keep any leading namespaces/classes. */
21029 base = strrchr (DW_STRING (attr), ':');
21030 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21031 return "";
21032
21033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21034 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21035 DW_STRING (attr),
21036 &base[-1] - DW_STRING (attr));
21037 }
21038
21039 /* Return the name of the namespace/class that DIE is defined within,
21040 or "" if we can't tell. The caller should not xfree the result.
21041
21042 For example, if we're within the method foo() in the following
21043 code:
21044
21045 namespace N {
21046 class C {
21047 void foo () {
21048 }
21049 };
21050 }
21051
21052 then determine_prefix on foo's die will return "N::C". */
21053
21054 static const char *
21055 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21056 {
21057 struct dwarf2_per_objfile *dwarf2_per_objfile
21058 = cu->per_cu->dwarf2_per_objfile;
21059 struct die_info *parent, *spec_die;
21060 struct dwarf2_cu *spec_cu;
21061 struct type *parent_type;
21062 const char *retval;
21063
21064 if (cu->language != language_cplus
21065 && cu->language != language_fortran && cu->language != language_d
21066 && cu->language != language_rust)
21067 return "";
21068
21069 retval = anonymous_struct_prefix (die, cu);
21070 if (retval)
21071 return retval;
21072
21073 /* We have to be careful in the presence of DW_AT_specification.
21074 For example, with GCC 3.4, given the code
21075
21076 namespace N {
21077 void foo() {
21078 // Definition of N::foo.
21079 }
21080 }
21081
21082 then we'll have a tree of DIEs like this:
21083
21084 1: DW_TAG_compile_unit
21085 2: DW_TAG_namespace // N
21086 3: DW_TAG_subprogram // declaration of N::foo
21087 4: DW_TAG_subprogram // definition of N::foo
21088 DW_AT_specification // refers to die #3
21089
21090 Thus, when processing die #4, we have to pretend that we're in
21091 the context of its DW_AT_specification, namely the contex of die
21092 #3. */
21093 spec_cu = cu;
21094 spec_die = die_specification (die, &spec_cu);
21095 if (spec_die == NULL)
21096 parent = die->parent;
21097 else
21098 {
21099 parent = spec_die->parent;
21100 cu = spec_cu;
21101 }
21102
21103 if (parent == NULL)
21104 return "";
21105 else if (parent->building_fullname)
21106 {
21107 const char *name;
21108 const char *parent_name;
21109
21110 /* It has been seen on RealView 2.2 built binaries,
21111 DW_TAG_template_type_param types actually _defined_ as
21112 children of the parent class:
21113
21114 enum E {};
21115 template class <class Enum> Class{};
21116 Class<enum E> class_e;
21117
21118 1: DW_TAG_class_type (Class)
21119 2: DW_TAG_enumeration_type (E)
21120 3: DW_TAG_enumerator (enum1:0)
21121 3: DW_TAG_enumerator (enum2:1)
21122 ...
21123 2: DW_TAG_template_type_param
21124 DW_AT_type DW_FORM_ref_udata (E)
21125
21126 Besides being broken debug info, it can put GDB into an
21127 infinite loop. Consider:
21128
21129 When we're building the full name for Class<E>, we'll start
21130 at Class, and go look over its template type parameters,
21131 finding E. We'll then try to build the full name of E, and
21132 reach here. We're now trying to build the full name of E,
21133 and look over the parent DIE for containing scope. In the
21134 broken case, if we followed the parent DIE of E, we'd again
21135 find Class, and once again go look at its template type
21136 arguments, etc., etc. Simply don't consider such parent die
21137 as source-level parent of this die (it can't be, the language
21138 doesn't allow it), and break the loop here. */
21139 name = dwarf2_name (die, cu);
21140 parent_name = dwarf2_name (parent, cu);
21141 complaint (_("template param type '%s' defined within parent '%s'"),
21142 name ? name : "<unknown>",
21143 parent_name ? parent_name : "<unknown>");
21144 return "";
21145 }
21146 else
21147 switch (parent->tag)
21148 {
21149 case DW_TAG_namespace:
21150 parent_type = read_type_die (parent, cu);
21151 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21152 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21153 Work around this problem here. */
21154 if (cu->language == language_cplus
21155 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21156 return "";
21157 /* We give a name to even anonymous namespaces. */
21158 return TYPE_NAME (parent_type);
21159 case DW_TAG_class_type:
21160 case DW_TAG_interface_type:
21161 case DW_TAG_structure_type:
21162 case DW_TAG_union_type:
21163 case DW_TAG_module:
21164 parent_type = read_type_die (parent, cu);
21165 if (TYPE_NAME (parent_type) != NULL)
21166 return TYPE_NAME (parent_type);
21167 else
21168 /* An anonymous structure is only allowed non-static data
21169 members; no typedefs, no member functions, et cetera.
21170 So it does not need a prefix. */
21171 return "";
21172 case DW_TAG_compile_unit:
21173 case DW_TAG_partial_unit:
21174 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21175 if (cu->language == language_cplus
21176 && !dwarf2_per_objfile->types.empty ()
21177 && die->child != NULL
21178 && (die->tag == DW_TAG_class_type
21179 || die->tag == DW_TAG_structure_type
21180 || die->tag == DW_TAG_union_type))
21181 {
21182 const char *name = guess_full_die_structure_name (die, cu);
21183 if (name != NULL)
21184 return name;
21185 }
21186 return "";
21187 case DW_TAG_subprogram:
21188 /* Nested subroutines in Fortran get a prefix with the name
21189 of the parent's subroutine. */
21190 if (cu->language == language_fortran)
21191 {
21192 if ((die->tag == DW_TAG_subprogram)
21193 && (dwarf2_name (parent, cu) != NULL))
21194 return dwarf2_name (parent, cu);
21195 }
21196 return determine_prefix (parent, cu);
21197 case DW_TAG_enumeration_type:
21198 parent_type = read_type_die (parent, cu);
21199 if (TYPE_DECLARED_CLASS (parent_type))
21200 {
21201 if (TYPE_NAME (parent_type) != NULL)
21202 return TYPE_NAME (parent_type);
21203 return "";
21204 }
21205 /* Fall through. */
21206 default:
21207 return determine_prefix (parent, cu);
21208 }
21209 }
21210
21211 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21212 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21213 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21214 an obconcat, otherwise allocate storage for the result. The CU argument is
21215 used to determine the language and hence, the appropriate separator. */
21216
21217 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21218
21219 static char *
21220 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21221 int physname, struct dwarf2_cu *cu)
21222 {
21223 const char *lead = "";
21224 const char *sep;
21225
21226 if (suffix == NULL || suffix[0] == '\0'
21227 || prefix == NULL || prefix[0] == '\0')
21228 sep = "";
21229 else if (cu->language == language_d)
21230 {
21231 /* For D, the 'main' function could be defined in any module, but it
21232 should never be prefixed. */
21233 if (strcmp (suffix, "D main") == 0)
21234 {
21235 prefix = "";
21236 sep = "";
21237 }
21238 else
21239 sep = ".";
21240 }
21241 else if (cu->language == language_fortran && physname)
21242 {
21243 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21244 DW_AT_MIPS_linkage_name is preferred and used instead. */
21245
21246 lead = "__";
21247 sep = "_MOD_";
21248 }
21249 else
21250 sep = "::";
21251
21252 if (prefix == NULL)
21253 prefix = "";
21254 if (suffix == NULL)
21255 suffix = "";
21256
21257 if (obs == NULL)
21258 {
21259 char *retval
21260 = ((char *)
21261 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21262
21263 strcpy (retval, lead);
21264 strcat (retval, prefix);
21265 strcat (retval, sep);
21266 strcat (retval, suffix);
21267 return retval;
21268 }
21269 else
21270 {
21271 /* We have an obstack. */
21272 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21273 }
21274 }
21275
21276 /* Return sibling of die, NULL if no sibling. */
21277
21278 static struct die_info *
21279 sibling_die (struct die_info *die)
21280 {
21281 return die->sibling;
21282 }
21283
21284 /* Get name of a die, return NULL if not found. */
21285
21286 static const char *
21287 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21288 struct objfile *objfile)
21289 {
21290 if (name && cu->language == language_cplus)
21291 {
21292 std::string canon_name = cp_canonicalize_string (name);
21293
21294 if (!canon_name.empty ())
21295 {
21296 if (canon_name != name)
21297 name = objfile->intern (canon_name);
21298 }
21299 }
21300
21301 return name;
21302 }
21303
21304 /* Get name of a die, return NULL if not found.
21305 Anonymous namespaces are converted to their magic string. */
21306
21307 static const char *
21308 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21309 {
21310 struct attribute *attr;
21311 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21312
21313 attr = dwarf2_attr (die, DW_AT_name, cu);
21314 if ((!attr || !DW_STRING (attr))
21315 && die->tag != DW_TAG_namespace
21316 && die->tag != DW_TAG_class_type
21317 && die->tag != DW_TAG_interface_type
21318 && die->tag != DW_TAG_structure_type
21319 && die->tag != DW_TAG_union_type)
21320 return NULL;
21321
21322 switch (die->tag)
21323 {
21324 case DW_TAG_compile_unit:
21325 case DW_TAG_partial_unit:
21326 /* Compilation units have a DW_AT_name that is a filename, not
21327 a source language identifier. */
21328 case DW_TAG_enumeration_type:
21329 case DW_TAG_enumerator:
21330 /* These tags always have simple identifiers already; no need
21331 to canonicalize them. */
21332 return DW_STRING (attr);
21333
21334 case DW_TAG_namespace:
21335 if (attr != NULL && DW_STRING (attr) != NULL)
21336 return DW_STRING (attr);
21337 return CP_ANONYMOUS_NAMESPACE_STR;
21338
21339 case DW_TAG_class_type:
21340 case DW_TAG_interface_type:
21341 case DW_TAG_structure_type:
21342 case DW_TAG_union_type:
21343 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21344 structures or unions. These were of the form "._%d" in GCC 4.1,
21345 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21346 and GCC 4.4. We work around this problem by ignoring these. */
21347 if (attr && DW_STRING (attr)
21348 && (startswith (DW_STRING (attr), "._")
21349 || startswith (DW_STRING (attr), "<anonymous")))
21350 return NULL;
21351
21352 /* GCC might emit a nameless typedef that has a linkage name. See
21353 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21354 if (!attr || DW_STRING (attr) == NULL)
21355 {
21356 attr = dw2_linkage_name_attr (die, cu);
21357 if (attr == NULL || DW_STRING (attr) == NULL)
21358 return NULL;
21359
21360 /* Avoid demangling DW_STRING (attr) the second time on a second
21361 call for the same DIE. */
21362 if (!DW_STRING_IS_CANONICAL (attr))
21363 {
21364 gdb::unique_xmalloc_ptr<char> demangled
21365 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21366 if (demangled == nullptr)
21367 return nullptr;
21368
21369 DW_STRING (attr) = objfile->intern (demangled.get ());
21370 DW_STRING_IS_CANONICAL (attr) = 1;
21371 }
21372
21373 /* Strip any leading namespaces/classes, keep only the base name.
21374 DW_AT_name for named DIEs does not contain the prefixes. */
21375 const char *base = strrchr (DW_STRING (attr), ':');
21376 if (base && base > DW_STRING (attr) && base[-1] == ':')
21377 return &base[1];
21378 else
21379 return DW_STRING (attr);
21380 }
21381 break;
21382
21383 default:
21384 break;
21385 }
21386
21387 if (!DW_STRING_IS_CANONICAL (attr))
21388 {
21389 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21390 objfile);
21391 DW_STRING_IS_CANONICAL (attr) = 1;
21392 }
21393 return DW_STRING (attr);
21394 }
21395
21396 /* Return the die that this die in an extension of, or NULL if there
21397 is none. *EXT_CU is the CU containing DIE on input, and the CU
21398 containing the return value on output. */
21399
21400 static struct die_info *
21401 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21402 {
21403 struct attribute *attr;
21404
21405 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21406 if (attr == NULL)
21407 return NULL;
21408
21409 return follow_die_ref (die, attr, ext_cu);
21410 }
21411
21412 /* A convenience function that returns an "unknown" DWARF name,
21413 including the value of V. STR is the name of the entity being
21414 printed, e.g., "TAG". */
21415
21416 static const char *
21417 dwarf_unknown (const char *str, unsigned v)
21418 {
21419 char *cell = get_print_cell ();
21420 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21421 return cell;
21422 }
21423
21424 /* Convert a DIE tag into its string name. */
21425
21426 static const char *
21427 dwarf_tag_name (unsigned tag)
21428 {
21429 const char *name = get_DW_TAG_name (tag);
21430
21431 if (name == NULL)
21432 return dwarf_unknown ("TAG", tag);
21433
21434 return name;
21435 }
21436
21437 /* Convert a DWARF attribute code into its string name. */
21438
21439 static const char *
21440 dwarf_attr_name (unsigned attr)
21441 {
21442 const char *name;
21443
21444 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21445 if (attr == DW_AT_MIPS_fde)
21446 return "DW_AT_MIPS_fde";
21447 #else
21448 if (attr == DW_AT_HP_block_index)
21449 return "DW_AT_HP_block_index";
21450 #endif
21451
21452 name = get_DW_AT_name (attr);
21453
21454 if (name == NULL)
21455 return dwarf_unknown ("AT", attr);
21456
21457 return name;
21458 }
21459
21460 /* Convert a DWARF value form code into its string name. */
21461
21462 static const char *
21463 dwarf_form_name (unsigned form)
21464 {
21465 const char *name = get_DW_FORM_name (form);
21466
21467 if (name == NULL)
21468 return dwarf_unknown ("FORM", form);
21469
21470 return name;
21471 }
21472
21473 static const char *
21474 dwarf_bool_name (unsigned mybool)
21475 {
21476 if (mybool)
21477 return "TRUE";
21478 else
21479 return "FALSE";
21480 }
21481
21482 /* Convert a DWARF type code into its string name. */
21483
21484 static const char *
21485 dwarf_type_encoding_name (unsigned enc)
21486 {
21487 const char *name = get_DW_ATE_name (enc);
21488
21489 if (name == NULL)
21490 return dwarf_unknown ("ATE", enc);
21491
21492 return name;
21493 }
21494
21495 static void
21496 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21497 {
21498 unsigned int i;
21499
21500 print_spaces (indent, f);
21501 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21502 dwarf_tag_name (die->tag), die->abbrev,
21503 sect_offset_str (die->sect_off));
21504
21505 if (die->parent != NULL)
21506 {
21507 print_spaces (indent, f);
21508 fprintf_unfiltered (f, " parent at offset: %s\n",
21509 sect_offset_str (die->parent->sect_off));
21510 }
21511
21512 print_spaces (indent, f);
21513 fprintf_unfiltered (f, " has children: %s\n",
21514 dwarf_bool_name (die->child != NULL));
21515
21516 print_spaces (indent, f);
21517 fprintf_unfiltered (f, " attributes:\n");
21518
21519 for (i = 0; i < die->num_attrs; ++i)
21520 {
21521 print_spaces (indent, f);
21522 fprintf_unfiltered (f, " %s (%s) ",
21523 dwarf_attr_name (die->attrs[i].name),
21524 dwarf_form_name (die->attrs[i].form));
21525
21526 switch (die->attrs[i].form)
21527 {
21528 case DW_FORM_addr:
21529 case DW_FORM_addrx:
21530 case DW_FORM_GNU_addr_index:
21531 fprintf_unfiltered (f, "address: ");
21532 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21533 break;
21534 case DW_FORM_block2:
21535 case DW_FORM_block4:
21536 case DW_FORM_block:
21537 case DW_FORM_block1:
21538 fprintf_unfiltered (f, "block: size %s",
21539 pulongest (DW_BLOCK (&die->attrs[i])->size));
21540 break;
21541 case DW_FORM_exprloc:
21542 fprintf_unfiltered (f, "expression: size %s",
21543 pulongest (DW_BLOCK (&die->attrs[i])->size));
21544 break;
21545 case DW_FORM_data16:
21546 fprintf_unfiltered (f, "constant of 16 bytes");
21547 break;
21548 case DW_FORM_ref_addr:
21549 fprintf_unfiltered (f, "ref address: ");
21550 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21551 break;
21552 case DW_FORM_GNU_ref_alt:
21553 fprintf_unfiltered (f, "alt ref address: ");
21554 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21555 break;
21556 case DW_FORM_ref1:
21557 case DW_FORM_ref2:
21558 case DW_FORM_ref4:
21559 case DW_FORM_ref8:
21560 case DW_FORM_ref_udata:
21561 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21562 (long) (DW_UNSND (&die->attrs[i])));
21563 break;
21564 case DW_FORM_data1:
21565 case DW_FORM_data2:
21566 case DW_FORM_data4:
21567 case DW_FORM_data8:
21568 case DW_FORM_udata:
21569 case DW_FORM_sdata:
21570 fprintf_unfiltered (f, "constant: %s",
21571 pulongest (DW_UNSND (&die->attrs[i])));
21572 break;
21573 case DW_FORM_sec_offset:
21574 fprintf_unfiltered (f, "section offset: %s",
21575 pulongest (DW_UNSND (&die->attrs[i])));
21576 break;
21577 case DW_FORM_ref_sig8:
21578 fprintf_unfiltered (f, "signature: %s",
21579 hex_string (DW_SIGNATURE (&die->attrs[i])));
21580 break;
21581 case DW_FORM_string:
21582 case DW_FORM_strp:
21583 case DW_FORM_line_strp:
21584 case DW_FORM_strx:
21585 case DW_FORM_GNU_str_index:
21586 case DW_FORM_GNU_strp_alt:
21587 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21588 DW_STRING (&die->attrs[i])
21589 ? DW_STRING (&die->attrs[i]) : "",
21590 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21591 break;
21592 case DW_FORM_flag:
21593 if (DW_UNSND (&die->attrs[i]))
21594 fprintf_unfiltered (f, "flag: TRUE");
21595 else
21596 fprintf_unfiltered (f, "flag: FALSE");
21597 break;
21598 case DW_FORM_flag_present:
21599 fprintf_unfiltered (f, "flag: TRUE");
21600 break;
21601 case DW_FORM_indirect:
21602 /* The reader will have reduced the indirect form to
21603 the "base form" so this form should not occur. */
21604 fprintf_unfiltered (f,
21605 "unexpected attribute form: DW_FORM_indirect");
21606 break;
21607 case DW_FORM_implicit_const:
21608 fprintf_unfiltered (f, "constant: %s",
21609 plongest (DW_SND (&die->attrs[i])));
21610 break;
21611 default:
21612 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21613 die->attrs[i].form);
21614 break;
21615 }
21616 fprintf_unfiltered (f, "\n");
21617 }
21618 }
21619
21620 static void
21621 dump_die_for_error (struct die_info *die)
21622 {
21623 dump_die_shallow (gdb_stderr, 0, die);
21624 }
21625
21626 static void
21627 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21628 {
21629 int indent = level * 4;
21630
21631 gdb_assert (die != NULL);
21632
21633 if (level >= max_level)
21634 return;
21635
21636 dump_die_shallow (f, indent, die);
21637
21638 if (die->child != NULL)
21639 {
21640 print_spaces (indent, f);
21641 fprintf_unfiltered (f, " Children:");
21642 if (level + 1 < max_level)
21643 {
21644 fprintf_unfiltered (f, "\n");
21645 dump_die_1 (f, level + 1, max_level, die->child);
21646 }
21647 else
21648 {
21649 fprintf_unfiltered (f,
21650 " [not printed, max nesting level reached]\n");
21651 }
21652 }
21653
21654 if (die->sibling != NULL && level > 0)
21655 {
21656 dump_die_1 (f, level, max_level, die->sibling);
21657 }
21658 }
21659
21660 /* This is called from the pdie macro in gdbinit.in.
21661 It's not static so gcc will keep a copy callable from gdb. */
21662
21663 void
21664 dump_die (struct die_info *die, int max_level)
21665 {
21666 dump_die_1 (gdb_stdlog, 0, max_level, die);
21667 }
21668
21669 static void
21670 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21671 {
21672 void **slot;
21673
21674 slot = htab_find_slot_with_hash (cu->die_hash, die,
21675 to_underlying (die->sect_off),
21676 INSERT);
21677
21678 *slot = die;
21679 }
21680
21681 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21682 required kind. */
21683
21684 static sect_offset
21685 dwarf2_get_ref_die_offset (const struct attribute *attr)
21686 {
21687 if (attr->form_is_ref ())
21688 return (sect_offset) DW_UNSND (attr);
21689
21690 complaint (_("unsupported die ref attribute form: '%s'"),
21691 dwarf_form_name (attr->form));
21692 return {};
21693 }
21694
21695 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21696 * the value held by the attribute is not constant. */
21697
21698 static LONGEST
21699 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21700 {
21701 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21702 return DW_SND (attr);
21703 else if (attr->form == DW_FORM_udata
21704 || attr->form == DW_FORM_data1
21705 || attr->form == DW_FORM_data2
21706 || attr->form == DW_FORM_data4
21707 || attr->form == DW_FORM_data8)
21708 return DW_UNSND (attr);
21709 else
21710 {
21711 /* For DW_FORM_data16 see attribute::form_is_constant. */
21712 complaint (_("Attribute value is not a constant (%s)"),
21713 dwarf_form_name (attr->form));
21714 return default_value;
21715 }
21716 }
21717
21718 /* Follow reference or signature attribute ATTR of SRC_DIE.
21719 On entry *REF_CU is the CU of SRC_DIE.
21720 On exit *REF_CU is the CU of the result. */
21721
21722 static struct die_info *
21723 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21724 struct dwarf2_cu **ref_cu)
21725 {
21726 struct die_info *die;
21727
21728 if (attr->form_is_ref ())
21729 die = follow_die_ref (src_die, attr, ref_cu);
21730 else if (attr->form == DW_FORM_ref_sig8)
21731 die = follow_die_sig (src_die, attr, ref_cu);
21732 else
21733 {
21734 dump_die_for_error (src_die);
21735 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21736 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21737 }
21738
21739 return die;
21740 }
21741
21742 /* Follow reference OFFSET.
21743 On entry *REF_CU is the CU of the source die referencing OFFSET.
21744 On exit *REF_CU is the CU of the result.
21745 Returns NULL if OFFSET is invalid. */
21746
21747 static struct die_info *
21748 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21749 struct dwarf2_cu **ref_cu)
21750 {
21751 struct die_info temp_die;
21752 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21753 struct dwarf2_per_objfile *dwarf2_per_objfile
21754 = cu->per_cu->dwarf2_per_objfile;
21755
21756 gdb_assert (cu->per_cu != NULL);
21757
21758 target_cu = cu;
21759
21760 if (cu->per_cu->is_debug_types)
21761 {
21762 /* .debug_types CUs cannot reference anything outside their CU.
21763 If they need to, they have to reference a signatured type via
21764 DW_FORM_ref_sig8. */
21765 if (!cu->header.offset_in_cu_p (sect_off))
21766 return NULL;
21767 }
21768 else if (offset_in_dwz != cu->per_cu->is_dwz
21769 || !cu->header.offset_in_cu_p (sect_off))
21770 {
21771 struct dwarf2_per_cu_data *per_cu;
21772
21773 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21774 dwarf2_per_objfile);
21775
21776 /* If necessary, add it to the queue and load its DIEs. */
21777 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21778 load_full_comp_unit (per_cu, false, cu->language);
21779
21780 target_cu = per_cu->cu;
21781 }
21782 else if (cu->dies == NULL)
21783 {
21784 /* We're loading full DIEs during partial symbol reading. */
21785 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21786 load_full_comp_unit (cu->per_cu, false, language_minimal);
21787 }
21788
21789 *ref_cu = target_cu;
21790 temp_die.sect_off = sect_off;
21791
21792 if (target_cu != cu)
21793 target_cu->ancestor = cu;
21794
21795 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21796 &temp_die,
21797 to_underlying (sect_off));
21798 }
21799
21800 /* Follow reference attribute ATTR of SRC_DIE.
21801 On entry *REF_CU is the CU of SRC_DIE.
21802 On exit *REF_CU is the CU of the result. */
21803
21804 static struct die_info *
21805 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21806 struct dwarf2_cu **ref_cu)
21807 {
21808 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21809 struct dwarf2_cu *cu = *ref_cu;
21810 struct die_info *die;
21811
21812 die = follow_die_offset (sect_off,
21813 (attr->form == DW_FORM_GNU_ref_alt
21814 || cu->per_cu->is_dwz),
21815 ref_cu);
21816 if (!die)
21817 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21818 "at %s [in module %s]"),
21819 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21820 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21821
21822 return die;
21823 }
21824
21825 /* See read.h. */
21826
21827 struct dwarf2_locexpr_baton
21828 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21829 dwarf2_per_cu_data *per_cu,
21830 CORE_ADDR (*get_frame_pc) (void *baton),
21831 void *baton, bool resolve_abstract_p)
21832 {
21833 struct dwarf2_cu *cu;
21834 struct die_info *die;
21835 struct attribute *attr;
21836 struct dwarf2_locexpr_baton retval;
21837 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21838 struct objfile *objfile = dwarf2_per_objfile->objfile;
21839
21840 if (per_cu->cu == NULL)
21841 load_cu (per_cu, false);
21842 cu = per_cu->cu;
21843 if (cu == NULL)
21844 {
21845 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21846 Instead just throw an error, not much else we can do. */
21847 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21848 sect_offset_str (sect_off), objfile_name (objfile));
21849 }
21850
21851 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21852 if (!die)
21853 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21854 sect_offset_str (sect_off), objfile_name (objfile));
21855
21856 attr = dwarf2_attr (die, DW_AT_location, cu);
21857 if (!attr && resolve_abstract_p
21858 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21859 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21860 {
21861 CORE_ADDR pc = (*get_frame_pc) (baton);
21862 CORE_ADDR baseaddr = objfile->text_section_offset ();
21863 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21864
21865 for (const auto &cand_off
21866 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21867 {
21868 struct dwarf2_cu *cand_cu = cu;
21869 struct die_info *cand
21870 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21871 if (!cand
21872 || !cand->parent
21873 || cand->parent->tag != DW_TAG_subprogram)
21874 continue;
21875
21876 CORE_ADDR pc_low, pc_high;
21877 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21878 if (pc_low == ((CORE_ADDR) -1))
21879 continue;
21880 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21881 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21882 if (!(pc_low <= pc && pc < pc_high))
21883 continue;
21884
21885 die = cand;
21886 attr = dwarf2_attr (die, DW_AT_location, cu);
21887 break;
21888 }
21889 }
21890
21891 if (!attr)
21892 {
21893 /* DWARF: "If there is no such attribute, then there is no effect.".
21894 DATA is ignored if SIZE is 0. */
21895
21896 retval.data = NULL;
21897 retval.size = 0;
21898 }
21899 else if (attr->form_is_section_offset ())
21900 {
21901 struct dwarf2_loclist_baton loclist_baton;
21902 CORE_ADDR pc = (*get_frame_pc) (baton);
21903 size_t size;
21904
21905 fill_in_loclist_baton (cu, &loclist_baton, attr);
21906
21907 retval.data = dwarf2_find_location_expression (&loclist_baton,
21908 &size, pc);
21909 retval.size = size;
21910 }
21911 else
21912 {
21913 if (!attr->form_is_block ())
21914 error (_("Dwarf Error: DIE at %s referenced in module %s "
21915 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21916 sect_offset_str (sect_off), objfile_name (objfile));
21917
21918 retval.data = DW_BLOCK (attr)->data;
21919 retval.size = DW_BLOCK (attr)->size;
21920 }
21921 retval.per_cu = cu->per_cu;
21922
21923 age_cached_comp_units (dwarf2_per_objfile);
21924
21925 return retval;
21926 }
21927
21928 /* See read.h. */
21929
21930 struct dwarf2_locexpr_baton
21931 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21932 dwarf2_per_cu_data *per_cu,
21933 CORE_ADDR (*get_frame_pc) (void *baton),
21934 void *baton)
21935 {
21936 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21937
21938 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21939 }
21940
21941 /* Write a constant of a given type as target-ordered bytes into
21942 OBSTACK. */
21943
21944 static const gdb_byte *
21945 write_constant_as_bytes (struct obstack *obstack,
21946 enum bfd_endian byte_order,
21947 struct type *type,
21948 ULONGEST value,
21949 LONGEST *len)
21950 {
21951 gdb_byte *result;
21952
21953 *len = TYPE_LENGTH (type);
21954 result = (gdb_byte *) obstack_alloc (obstack, *len);
21955 store_unsigned_integer (result, *len, byte_order, value);
21956
21957 return result;
21958 }
21959
21960 /* See read.h. */
21961
21962 const gdb_byte *
21963 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21964 dwarf2_per_cu_data *per_cu,
21965 obstack *obstack,
21966 LONGEST *len)
21967 {
21968 struct dwarf2_cu *cu;
21969 struct die_info *die;
21970 struct attribute *attr;
21971 const gdb_byte *result = NULL;
21972 struct type *type;
21973 LONGEST value;
21974 enum bfd_endian byte_order;
21975 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21976
21977 if (per_cu->cu == NULL)
21978 load_cu (per_cu, false);
21979 cu = per_cu->cu;
21980 if (cu == NULL)
21981 {
21982 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21983 Instead just throw an error, not much else we can do. */
21984 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21985 sect_offset_str (sect_off), objfile_name (objfile));
21986 }
21987
21988 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21989 if (!die)
21990 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21991 sect_offset_str (sect_off), objfile_name (objfile));
21992
21993 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21994 if (attr == NULL)
21995 return NULL;
21996
21997 byte_order = (bfd_big_endian (objfile->obfd)
21998 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21999
22000 switch (attr->form)
22001 {
22002 case DW_FORM_addr:
22003 case DW_FORM_addrx:
22004 case DW_FORM_GNU_addr_index:
22005 {
22006 gdb_byte *tem;
22007
22008 *len = cu->header.addr_size;
22009 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22010 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22011 result = tem;
22012 }
22013 break;
22014 case DW_FORM_string:
22015 case DW_FORM_strp:
22016 case DW_FORM_strx:
22017 case DW_FORM_GNU_str_index:
22018 case DW_FORM_GNU_strp_alt:
22019 /* DW_STRING is already allocated on the objfile obstack, point
22020 directly to it. */
22021 result = (const gdb_byte *) DW_STRING (attr);
22022 *len = strlen (DW_STRING (attr));
22023 break;
22024 case DW_FORM_block1:
22025 case DW_FORM_block2:
22026 case DW_FORM_block4:
22027 case DW_FORM_block:
22028 case DW_FORM_exprloc:
22029 case DW_FORM_data16:
22030 result = DW_BLOCK (attr)->data;
22031 *len = DW_BLOCK (attr)->size;
22032 break;
22033
22034 /* The DW_AT_const_value attributes are supposed to carry the
22035 symbol's value "represented as it would be on the target
22036 architecture." By the time we get here, it's already been
22037 converted to host endianness, so we just need to sign- or
22038 zero-extend it as appropriate. */
22039 case DW_FORM_data1:
22040 type = die_type (die, cu);
22041 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22042 if (result == NULL)
22043 result = write_constant_as_bytes (obstack, byte_order,
22044 type, value, len);
22045 break;
22046 case DW_FORM_data2:
22047 type = die_type (die, cu);
22048 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22049 if (result == NULL)
22050 result = write_constant_as_bytes (obstack, byte_order,
22051 type, value, len);
22052 break;
22053 case DW_FORM_data4:
22054 type = die_type (die, cu);
22055 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22056 if (result == NULL)
22057 result = write_constant_as_bytes (obstack, byte_order,
22058 type, value, len);
22059 break;
22060 case DW_FORM_data8:
22061 type = die_type (die, cu);
22062 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22063 if (result == NULL)
22064 result = write_constant_as_bytes (obstack, byte_order,
22065 type, value, len);
22066 break;
22067
22068 case DW_FORM_sdata:
22069 case DW_FORM_implicit_const:
22070 type = die_type (die, cu);
22071 result = write_constant_as_bytes (obstack, byte_order,
22072 type, DW_SND (attr), len);
22073 break;
22074
22075 case DW_FORM_udata:
22076 type = die_type (die, cu);
22077 result = write_constant_as_bytes (obstack, byte_order,
22078 type, DW_UNSND (attr), len);
22079 break;
22080
22081 default:
22082 complaint (_("unsupported const value attribute form: '%s'"),
22083 dwarf_form_name (attr->form));
22084 break;
22085 }
22086
22087 return result;
22088 }
22089
22090 /* See read.h. */
22091
22092 struct type *
22093 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22094 dwarf2_per_cu_data *per_cu)
22095 {
22096 struct dwarf2_cu *cu;
22097 struct die_info *die;
22098
22099 if (per_cu->cu == NULL)
22100 load_cu (per_cu, false);
22101 cu = per_cu->cu;
22102 if (!cu)
22103 return NULL;
22104
22105 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22106 if (!die)
22107 return NULL;
22108
22109 return die_type (die, cu);
22110 }
22111
22112 /* See read.h. */
22113
22114 struct type *
22115 dwarf2_get_die_type (cu_offset die_offset,
22116 struct dwarf2_per_cu_data *per_cu)
22117 {
22118 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22119 return get_die_type_at_offset (die_offset_sect, per_cu);
22120 }
22121
22122 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22123 On entry *REF_CU is the CU of SRC_DIE.
22124 On exit *REF_CU is the CU of the result.
22125 Returns NULL if the referenced DIE isn't found. */
22126
22127 static struct die_info *
22128 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22129 struct dwarf2_cu **ref_cu)
22130 {
22131 struct die_info temp_die;
22132 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22133 struct die_info *die;
22134
22135 /* While it might be nice to assert sig_type->type == NULL here,
22136 we can get here for DW_AT_imported_declaration where we need
22137 the DIE not the type. */
22138
22139 /* If necessary, add it to the queue and load its DIEs. */
22140
22141 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22142 read_signatured_type (sig_type);
22143
22144 sig_cu = sig_type->per_cu.cu;
22145 gdb_assert (sig_cu != NULL);
22146 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22147 temp_die.sect_off = sig_type->type_offset_in_section;
22148 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22149 to_underlying (temp_die.sect_off));
22150 if (die)
22151 {
22152 struct dwarf2_per_objfile *dwarf2_per_objfile
22153 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22154
22155 /* For .gdb_index version 7 keep track of included TUs.
22156 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22157 if (dwarf2_per_objfile->index_table != NULL
22158 && dwarf2_per_objfile->index_table->version <= 7)
22159 {
22160 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22161 }
22162
22163 *ref_cu = sig_cu;
22164 if (sig_cu != cu)
22165 sig_cu->ancestor = cu;
22166
22167 return die;
22168 }
22169
22170 return NULL;
22171 }
22172
22173 /* Follow signatured type referenced by ATTR in SRC_DIE.
22174 On entry *REF_CU is the CU of SRC_DIE.
22175 On exit *REF_CU is the CU of the result.
22176 The result is the DIE of the type.
22177 If the referenced type cannot be found an error is thrown. */
22178
22179 static struct die_info *
22180 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22181 struct dwarf2_cu **ref_cu)
22182 {
22183 ULONGEST signature = DW_SIGNATURE (attr);
22184 struct signatured_type *sig_type;
22185 struct die_info *die;
22186
22187 gdb_assert (attr->form == DW_FORM_ref_sig8);
22188
22189 sig_type = lookup_signatured_type (*ref_cu, signature);
22190 /* sig_type will be NULL if the signatured type is missing from
22191 the debug info. */
22192 if (sig_type == NULL)
22193 {
22194 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22195 " from DIE at %s [in module %s]"),
22196 hex_string (signature), sect_offset_str (src_die->sect_off),
22197 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22198 }
22199
22200 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22201 if (die == NULL)
22202 {
22203 dump_die_for_error (src_die);
22204 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22205 " from DIE at %s [in module %s]"),
22206 hex_string (signature), sect_offset_str (src_die->sect_off),
22207 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22208 }
22209
22210 return die;
22211 }
22212
22213 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22214 reading in and processing the type unit if necessary. */
22215
22216 static struct type *
22217 get_signatured_type (struct die_info *die, ULONGEST signature,
22218 struct dwarf2_cu *cu)
22219 {
22220 struct dwarf2_per_objfile *dwarf2_per_objfile
22221 = cu->per_cu->dwarf2_per_objfile;
22222 struct signatured_type *sig_type;
22223 struct dwarf2_cu *type_cu;
22224 struct die_info *type_die;
22225 struct type *type;
22226
22227 sig_type = lookup_signatured_type (cu, signature);
22228 /* sig_type will be NULL if the signatured type is missing from
22229 the debug info. */
22230 if (sig_type == NULL)
22231 {
22232 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22233 " from DIE at %s [in module %s]"),
22234 hex_string (signature), sect_offset_str (die->sect_off),
22235 objfile_name (dwarf2_per_objfile->objfile));
22236 return build_error_marker_type (cu, die);
22237 }
22238
22239 /* If we already know the type we're done. */
22240 if (sig_type->type != NULL)
22241 return sig_type->type;
22242
22243 type_cu = cu;
22244 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22245 if (type_die != NULL)
22246 {
22247 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22248 is created. This is important, for example, because for c++ classes
22249 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22250 type = read_type_die (type_die, type_cu);
22251 if (type == NULL)
22252 {
22253 complaint (_("Dwarf Error: Cannot build signatured type %s"
22254 " referenced from DIE at %s [in module %s]"),
22255 hex_string (signature), sect_offset_str (die->sect_off),
22256 objfile_name (dwarf2_per_objfile->objfile));
22257 type = build_error_marker_type (cu, die);
22258 }
22259 }
22260 else
22261 {
22262 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22263 " from DIE at %s [in module %s]"),
22264 hex_string (signature), sect_offset_str (die->sect_off),
22265 objfile_name (dwarf2_per_objfile->objfile));
22266 type = build_error_marker_type (cu, die);
22267 }
22268 sig_type->type = type;
22269
22270 return type;
22271 }
22272
22273 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22274 reading in and processing the type unit if necessary. */
22275
22276 static struct type *
22277 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22278 struct dwarf2_cu *cu) /* ARI: editCase function */
22279 {
22280 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22281 if (attr->form_is_ref ())
22282 {
22283 struct dwarf2_cu *type_cu = cu;
22284 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22285
22286 return read_type_die (type_die, type_cu);
22287 }
22288 else if (attr->form == DW_FORM_ref_sig8)
22289 {
22290 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22291 }
22292 else
22293 {
22294 struct dwarf2_per_objfile *dwarf2_per_objfile
22295 = cu->per_cu->dwarf2_per_objfile;
22296
22297 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22298 " at %s [in module %s]"),
22299 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22300 objfile_name (dwarf2_per_objfile->objfile));
22301 return build_error_marker_type (cu, die);
22302 }
22303 }
22304
22305 /* Load the DIEs associated with type unit PER_CU into memory. */
22306
22307 static void
22308 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22309 {
22310 struct signatured_type *sig_type;
22311
22312 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22313 gdb_assert (! per_cu->type_unit_group_p ());
22314
22315 /* We have the per_cu, but we need the signatured_type.
22316 Fortunately this is an easy translation. */
22317 gdb_assert (per_cu->is_debug_types);
22318 sig_type = (struct signatured_type *) per_cu;
22319
22320 gdb_assert (per_cu->cu == NULL);
22321
22322 read_signatured_type (sig_type);
22323
22324 gdb_assert (per_cu->cu != NULL);
22325 }
22326
22327 /* Read in a signatured type and build its CU and DIEs.
22328 If the type is a stub for the real type in a DWO file,
22329 read in the real type from the DWO file as well. */
22330
22331 static void
22332 read_signatured_type (struct signatured_type *sig_type)
22333 {
22334 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22335
22336 gdb_assert (per_cu->is_debug_types);
22337 gdb_assert (per_cu->cu == NULL);
22338
22339 cutu_reader reader (per_cu, NULL, 0, false);
22340
22341 if (!reader.dummy_p)
22342 {
22343 struct dwarf2_cu *cu = reader.cu;
22344 const gdb_byte *info_ptr = reader.info_ptr;
22345
22346 gdb_assert (cu->die_hash == NULL);
22347 cu->die_hash =
22348 htab_create_alloc_ex (cu->header.length / 12,
22349 die_hash,
22350 die_eq,
22351 NULL,
22352 &cu->comp_unit_obstack,
22353 hashtab_obstack_allocate,
22354 dummy_obstack_deallocate);
22355
22356 if (reader.comp_unit_die->has_children)
22357 reader.comp_unit_die->child
22358 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22359 reader.comp_unit_die);
22360 cu->dies = reader.comp_unit_die;
22361 /* comp_unit_die is not stored in die_hash, no need. */
22362
22363 /* We try not to read any attributes in this function, because
22364 not all CUs needed for references have been loaded yet, and
22365 symbol table processing isn't initialized. But we have to
22366 set the CU language, or we won't be able to build types
22367 correctly. Similarly, if we do not read the producer, we can
22368 not apply producer-specific interpretation. */
22369 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22370
22371 reader.keep ();
22372 }
22373
22374 sig_type->per_cu.tu_read = 1;
22375 }
22376
22377 /* Decode simple location descriptions.
22378 Given a pointer to a dwarf block that defines a location, compute
22379 the location and return the value.
22380
22381 NOTE drow/2003-11-18: This function is called in two situations
22382 now: for the address of static or global variables (partial symbols
22383 only) and for offsets into structures which are expected to be
22384 (more or less) constant. The partial symbol case should go away,
22385 and only the constant case should remain. That will let this
22386 function complain more accurately. A few special modes are allowed
22387 without complaint for global variables (for instance, global
22388 register values and thread-local values).
22389
22390 A location description containing no operations indicates that the
22391 object is optimized out. The return value is 0 for that case.
22392 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22393 callers will only want a very basic result and this can become a
22394 complaint.
22395
22396 Note that stack[0] is unused except as a default error return. */
22397
22398 static CORE_ADDR
22399 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22400 {
22401 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22402 size_t i;
22403 size_t size = blk->size;
22404 const gdb_byte *data = blk->data;
22405 CORE_ADDR stack[64];
22406 int stacki;
22407 unsigned int bytes_read, unsnd;
22408 gdb_byte op;
22409
22410 i = 0;
22411 stacki = 0;
22412 stack[stacki] = 0;
22413 stack[++stacki] = 0;
22414
22415 while (i < size)
22416 {
22417 op = data[i++];
22418 switch (op)
22419 {
22420 case DW_OP_lit0:
22421 case DW_OP_lit1:
22422 case DW_OP_lit2:
22423 case DW_OP_lit3:
22424 case DW_OP_lit4:
22425 case DW_OP_lit5:
22426 case DW_OP_lit6:
22427 case DW_OP_lit7:
22428 case DW_OP_lit8:
22429 case DW_OP_lit9:
22430 case DW_OP_lit10:
22431 case DW_OP_lit11:
22432 case DW_OP_lit12:
22433 case DW_OP_lit13:
22434 case DW_OP_lit14:
22435 case DW_OP_lit15:
22436 case DW_OP_lit16:
22437 case DW_OP_lit17:
22438 case DW_OP_lit18:
22439 case DW_OP_lit19:
22440 case DW_OP_lit20:
22441 case DW_OP_lit21:
22442 case DW_OP_lit22:
22443 case DW_OP_lit23:
22444 case DW_OP_lit24:
22445 case DW_OP_lit25:
22446 case DW_OP_lit26:
22447 case DW_OP_lit27:
22448 case DW_OP_lit28:
22449 case DW_OP_lit29:
22450 case DW_OP_lit30:
22451 case DW_OP_lit31:
22452 stack[++stacki] = op - DW_OP_lit0;
22453 break;
22454
22455 case DW_OP_reg0:
22456 case DW_OP_reg1:
22457 case DW_OP_reg2:
22458 case DW_OP_reg3:
22459 case DW_OP_reg4:
22460 case DW_OP_reg5:
22461 case DW_OP_reg6:
22462 case DW_OP_reg7:
22463 case DW_OP_reg8:
22464 case DW_OP_reg9:
22465 case DW_OP_reg10:
22466 case DW_OP_reg11:
22467 case DW_OP_reg12:
22468 case DW_OP_reg13:
22469 case DW_OP_reg14:
22470 case DW_OP_reg15:
22471 case DW_OP_reg16:
22472 case DW_OP_reg17:
22473 case DW_OP_reg18:
22474 case DW_OP_reg19:
22475 case DW_OP_reg20:
22476 case DW_OP_reg21:
22477 case DW_OP_reg22:
22478 case DW_OP_reg23:
22479 case DW_OP_reg24:
22480 case DW_OP_reg25:
22481 case DW_OP_reg26:
22482 case DW_OP_reg27:
22483 case DW_OP_reg28:
22484 case DW_OP_reg29:
22485 case DW_OP_reg30:
22486 case DW_OP_reg31:
22487 stack[++stacki] = op - DW_OP_reg0;
22488 if (i < size)
22489 dwarf2_complex_location_expr_complaint ();
22490 break;
22491
22492 case DW_OP_regx:
22493 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22494 i += bytes_read;
22495 stack[++stacki] = unsnd;
22496 if (i < size)
22497 dwarf2_complex_location_expr_complaint ();
22498 break;
22499
22500 case DW_OP_addr:
22501 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22502 &bytes_read);
22503 i += bytes_read;
22504 break;
22505
22506 case DW_OP_const1u:
22507 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22508 i += 1;
22509 break;
22510
22511 case DW_OP_const1s:
22512 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22513 i += 1;
22514 break;
22515
22516 case DW_OP_const2u:
22517 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22518 i += 2;
22519 break;
22520
22521 case DW_OP_const2s:
22522 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22523 i += 2;
22524 break;
22525
22526 case DW_OP_const4u:
22527 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22528 i += 4;
22529 break;
22530
22531 case DW_OP_const4s:
22532 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22533 i += 4;
22534 break;
22535
22536 case DW_OP_const8u:
22537 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22538 i += 8;
22539 break;
22540
22541 case DW_OP_constu:
22542 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22543 &bytes_read);
22544 i += bytes_read;
22545 break;
22546
22547 case DW_OP_consts:
22548 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22549 i += bytes_read;
22550 break;
22551
22552 case DW_OP_dup:
22553 stack[stacki + 1] = stack[stacki];
22554 stacki++;
22555 break;
22556
22557 case DW_OP_plus:
22558 stack[stacki - 1] += stack[stacki];
22559 stacki--;
22560 break;
22561
22562 case DW_OP_plus_uconst:
22563 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22564 &bytes_read);
22565 i += bytes_read;
22566 break;
22567
22568 case DW_OP_minus:
22569 stack[stacki - 1] -= stack[stacki];
22570 stacki--;
22571 break;
22572
22573 case DW_OP_deref:
22574 /* If we're not the last op, then we definitely can't encode
22575 this using GDB's address_class enum. This is valid for partial
22576 global symbols, although the variable's address will be bogus
22577 in the psymtab. */
22578 if (i < size)
22579 dwarf2_complex_location_expr_complaint ();
22580 break;
22581
22582 case DW_OP_GNU_push_tls_address:
22583 case DW_OP_form_tls_address:
22584 /* The top of the stack has the offset from the beginning
22585 of the thread control block at which the variable is located. */
22586 /* Nothing should follow this operator, so the top of stack would
22587 be returned. */
22588 /* This is valid for partial global symbols, but the variable's
22589 address will be bogus in the psymtab. Make it always at least
22590 non-zero to not look as a variable garbage collected by linker
22591 which have DW_OP_addr 0. */
22592 if (i < size)
22593 dwarf2_complex_location_expr_complaint ();
22594 stack[stacki]++;
22595 break;
22596
22597 case DW_OP_GNU_uninit:
22598 break;
22599
22600 case DW_OP_addrx:
22601 case DW_OP_GNU_addr_index:
22602 case DW_OP_GNU_const_index:
22603 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22604 &bytes_read);
22605 i += bytes_read;
22606 break;
22607
22608 default:
22609 {
22610 const char *name = get_DW_OP_name (op);
22611
22612 if (name)
22613 complaint (_("unsupported stack op: '%s'"),
22614 name);
22615 else
22616 complaint (_("unsupported stack op: '%02x'"),
22617 op);
22618 }
22619
22620 return (stack[stacki]);
22621 }
22622
22623 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22624 outside of the allocated space. Also enforce minimum>0. */
22625 if (stacki >= ARRAY_SIZE (stack) - 1)
22626 {
22627 complaint (_("location description stack overflow"));
22628 return 0;
22629 }
22630
22631 if (stacki <= 0)
22632 {
22633 complaint (_("location description stack underflow"));
22634 return 0;
22635 }
22636 }
22637 return (stack[stacki]);
22638 }
22639
22640 /* memory allocation interface */
22641
22642 static struct dwarf_block *
22643 dwarf_alloc_block (struct dwarf2_cu *cu)
22644 {
22645 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22646 }
22647
22648 static struct die_info *
22649 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22650 {
22651 struct die_info *die;
22652 size_t size = sizeof (struct die_info);
22653
22654 if (num_attrs > 1)
22655 size += (num_attrs - 1) * sizeof (struct attribute);
22656
22657 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22658 memset (die, 0, sizeof (struct die_info));
22659 return (die);
22660 }
22661
22662 \f
22663
22664 /* Macro support. */
22665
22666 /* An overload of dwarf_decode_macros that finds the correct section
22667 and ensures it is read in before calling the other overload. */
22668
22669 static void
22670 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22671 int section_is_gnu)
22672 {
22673 struct dwarf2_per_objfile *dwarf2_per_objfile
22674 = cu->per_cu->dwarf2_per_objfile;
22675 struct objfile *objfile = dwarf2_per_objfile->objfile;
22676 const struct line_header *lh = cu->line_header;
22677 unsigned int offset_size = cu->header.offset_size;
22678 struct dwarf2_section_info *section;
22679 const char *section_name;
22680
22681 if (cu->dwo_unit != nullptr)
22682 {
22683 if (section_is_gnu)
22684 {
22685 section = &cu->dwo_unit->dwo_file->sections.macro;
22686 section_name = ".debug_macro.dwo";
22687 }
22688 else
22689 {
22690 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22691 section_name = ".debug_macinfo.dwo";
22692 }
22693 }
22694 else
22695 {
22696 if (section_is_gnu)
22697 {
22698 section = &dwarf2_per_objfile->macro;
22699 section_name = ".debug_macro";
22700 }
22701 else
22702 {
22703 section = &dwarf2_per_objfile->macinfo;
22704 section_name = ".debug_macinfo";
22705 }
22706 }
22707
22708 section->read (objfile);
22709 if (section->buffer == nullptr)
22710 {
22711 complaint (_("missing %s section"), section_name);
22712 return;
22713 }
22714
22715 buildsym_compunit *builder = cu->get_builder ();
22716
22717 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22718 offset_size, offset, section_is_gnu);
22719 }
22720
22721 /* Return the .debug_loc section to use for CU.
22722 For DWO files use .debug_loc.dwo. */
22723
22724 static struct dwarf2_section_info *
22725 cu_debug_loc_section (struct dwarf2_cu *cu)
22726 {
22727 struct dwarf2_per_objfile *dwarf2_per_objfile
22728 = cu->per_cu->dwarf2_per_objfile;
22729
22730 if (cu->dwo_unit)
22731 {
22732 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22733
22734 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22735 }
22736 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22737 : &dwarf2_per_objfile->loc);
22738 }
22739
22740 /* A helper function that fills in a dwarf2_loclist_baton. */
22741
22742 static void
22743 fill_in_loclist_baton (struct dwarf2_cu *cu,
22744 struct dwarf2_loclist_baton *baton,
22745 const struct attribute *attr)
22746 {
22747 struct dwarf2_per_objfile *dwarf2_per_objfile
22748 = cu->per_cu->dwarf2_per_objfile;
22749 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22750
22751 section->read (dwarf2_per_objfile->objfile);
22752
22753 baton->per_cu = cu->per_cu;
22754 gdb_assert (baton->per_cu);
22755 /* We don't know how long the location list is, but make sure we
22756 don't run off the edge of the section. */
22757 baton->size = section->size - DW_UNSND (attr);
22758 baton->data = section->buffer + DW_UNSND (attr);
22759 baton->base_address = cu->base_address;
22760 baton->from_dwo = cu->dwo_unit != NULL;
22761 }
22762
22763 static void
22764 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22765 struct dwarf2_cu *cu, int is_block)
22766 {
22767 struct dwarf2_per_objfile *dwarf2_per_objfile
22768 = cu->per_cu->dwarf2_per_objfile;
22769 struct objfile *objfile = dwarf2_per_objfile->objfile;
22770 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22771
22772 if (attr->form_is_section_offset ()
22773 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22774 the section. If so, fall through to the complaint in the
22775 other branch. */
22776 && DW_UNSND (attr) < section->get_size (objfile))
22777 {
22778 struct dwarf2_loclist_baton *baton;
22779
22780 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22781
22782 fill_in_loclist_baton (cu, baton, attr);
22783
22784 if (cu->base_known == 0)
22785 complaint (_("Location list used without "
22786 "specifying the CU base address."));
22787
22788 SYMBOL_ACLASS_INDEX (sym) = (is_block
22789 ? dwarf2_loclist_block_index
22790 : dwarf2_loclist_index);
22791 SYMBOL_LOCATION_BATON (sym) = baton;
22792 }
22793 else
22794 {
22795 struct dwarf2_locexpr_baton *baton;
22796
22797 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22798 baton->per_cu = cu->per_cu;
22799 gdb_assert (baton->per_cu);
22800
22801 if (attr->form_is_block ())
22802 {
22803 /* Note that we're just copying the block's data pointer
22804 here, not the actual data. We're still pointing into the
22805 info_buffer for SYM's objfile; right now we never release
22806 that buffer, but when we do clean up properly this may
22807 need to change. */
22808 baton->size = DW_BLOCK (attr)->size;
22809 baton->data = DW_BLOCK (attr)->data;
22810 }
22811 else
22812 {
22813 dwarf2_invalid_attrib_class_complaint ("location description",
22814 sym->natural_name ());
22815 baton->size = 0;
22816 }
22817
22818 SYMBOL_ACLASS_INDEX (sym) = (is_block
22819 ? dwarf2_locexpr_block_index
22820 : dwarf2_locexpr_index);
22821 SYMBOL_LOCATION_BATON (sym) = baton;
22822 }
22823 }
22824
22825 /* See read.h. */
22826
22827 struct objfile *
22828 dwarf2_per_cu_data::objfile () const
22829 {
22830 struct objfile *objfile = dwarf2_per_objfile->objfile;
22831
22832 /* Return the master objfile, so that we can report and look up the
22833 correct file containing this variable. */
22834 if (objfile->separate_debug_objfile_backlink)
22835 objfile = objfile->separate_debug_objfile_backlink;
22836
22837 return objfile;
22838 }
22839
22840 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22841 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22842 CU_HEADERP first. */
22843
22844 static const struct comp_unit_head *
22845 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22846 const struct dwarf2_per_cu_data *per_cu)
22847 {
22848 const gdb_byte *info_ptr;
22849
22850 if (per_cu->cu)
22851 return &per_cu->cu->header;
22852
22853 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22854
22855 memset (cu_headerp, 0, sizeof (*cu_headerp));
22856 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22857 rcuh_kind::COMPILE);
22858
22859 return cu_headerp;
22860 }
22861
22862 /* See read.h. */
22863
22864 int
22865 dwarf2_per_cu_data::addr_size () const
22866 {
22867 struct comp_unit_head cu_header_local;
22868 const struct comp_unit_head *cu_headerp;
22869
22870 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22871
22872 return cu_headerp->addr_size;
22873 }
22874
22875 /* See read.h. */
22876
22877 int
22878 dwarf2_per_cu_data::offset_size () const
22879 {
22880 struct comp_unit_head cu_header_local;
22881 const struct comp_unit_head *cu_headerp;
22882
22883 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22884
22885 return cu_headerp->offset_size;
22886 }
22887
22888 /* See read.h. */
22889
22890 int
22891 dwarf2_per_cu_data::ref_addr_size () const
22892 {
22893 struct comp_unit_head cu_header_local;
22894 const struct comp_unit_head *cu_headerp;
22895
22896 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22897
22898 if (cu_headerp->version == 2)
22899 return cu_headerp->addr_size;
22900 else
22901 return cu_headerp->offset_size;
22902 }
22903
22904 /* See read.h. */
22905
22906 CORE_ADDR
22907 dwarf2_per_cu_data::text_offset () const
22908 {
22909 struct objfile *objfile = dwarf2_per_objfile->objfile;
22910
22911 return objfile->text_section_offset ();
22912 }
22913
22914 /* See read.h. */
22915
22916 struct type *
22917 dwarf2_per_cu_data::addr_type () const
22918 {
22919 struct objfile *objfile = dwarf2_per_objfile->objfile;
22920 struct type *void_type = objfile_type (objfile)->builtin_void;
22921 struct type *addr_type = lookup_pointer_type (void_type);
22922 int addr_size = this->addr_size ();
22923
22924 if (TYPE_LENGTH (addr_type) == addr_size)
22925 return addr_type;
22926
22927 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22928 return addr_type;
22929 }
22930
22931 /* A helper function for dwarf2_find_containing_comp_unit that returns
22932 the index of the result, and that searches a vector. It will
22933 return a result even if the offset in question does not actually
22934 occur in any CU. This is separate so that it can be unit
22935 tested. */
22936
22937 static int
22938 dwarf2_find_containing_comp_unit
22939 (sect_offset sect_off,
22940 unsigned int offset_in_dwz,
22941 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22942 {
22943 int low, high;
22944
22945 low = 0;
22946 high = all_comp_units.size () - 1;
22947 while (high > low)
22948 {
22949 struct dwarf2_per_cu_data *mid_cu;
22950 int mid = low + (high - low) / 2;
22951
22952 mid_cu = all_comp_units[mid];
22953 if (mid_cu->is_dwz > offset_in_dwz
22954 || (mid_cu->is_dwz == offset_in_dwz
22955 && mid_cu->sect_off + mid_cu->length > sect_off))
22956 high = mid;
22957 else
22958 low = mid + 1;
22959 }
22960 gdb_assert (low == high);
22961 return low;
22962 }
22963
22964 /* Locate the .debug_info compilation unit from CU's objfile which contains
22965 the DIE at OFFSET. Raises an error on failure. */
22966
22967 static struct dwarf2_per_cu_data *
22968 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22969 unsigned int offset_in_dwz,
22970 struct dwarf2_per_objfile *dwarf2_per_objfile)
22971 {
22972 int low
22973 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22974 dwarf2_per_objfile->all_comp_units);
22975 struct dwarf2_per_cu_data *this_cu
22976 = dwarf2_per_objfile->all_comp_units[low];
22977
22978 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22979 {
22980 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22981 error (_("Dwarf Error: could not find partial DIE containing "
22982 "offset %s [in module %s]"),
22983 sect_offset_str (sect_off),
22984 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22985
22986 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22987 <= sect_off);
22988 return dwarf2_per_objfile->all_comp_units[low-1];
22989 }
22990 else
22991 {
22992 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22993 && sect_off >= this_cu->sect_off + this_cu->length)
22994 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22995 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22996 return this_cu;
22997 }
22998 }
22999
23000 #if GDB_SELF_TEST
23001
23002 namespace selftests {
23003 namespace find_containing_comp_unit {
23004
23005 static void
23006 run_test ()
23007 {
23008 struct dwarf2_per_cu_data one {};
23009 struct dwarf2_per_cu_data two {};
23010 struct dwarf2_per_cu_data three {};
23011 struct dwarf2_per_cu_data four {};
23012
23013 one.length = 5;
23014 two.sect_off = sect_offset (one.length);
23015 two.length = 7;
23016
23017 three.length = 5;
23018 three.is_dwz = 1;
23019 four.sect_off = sect_offset (three.length);
23020 four.length = 7;
23021 four.is_dwz = 1;
23022
23023 std::vector<dwarf2_per_cu_data *> units;
23024 units.push_back (&one);
23025 units.push_back (&two);
23026 units.push_back (&three);
23027 units.push_back (&four);
23028
23029 int result;
23030
23031 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23032 SELF_CHECK (units[result] == &one);
23033 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23034 SELF_CHECK (units[result] == &one);
23035 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23036 SELF_CHECK (units[result] == &two);
23037
23038 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23039 SELF_CHECK (units[result] == &three);
23040 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23041 SELF_CHECK (units[result] == &three);
23042 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23043 SELF_CHECK (units[result] == &four);
23044 }
23045
23046 }
23047 }
23048
23049 #endif /* GDB_SELF_TEST */
23050
23051 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23052
23053 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23054 : per_cu (per_cu_),
23055 mark (false),
23056 has_loclist (false),
23057 checked_producer (false),
23058 producer_is_gxx_lt_4_6 (false),
23059 producer_is_gcc_lt_4_3 (false),
23060 producer_is_icc (false),
23061 producer_is_icc_lt_14 (false),
23062 producer_is_codewarrior (false),
23063 processing_has_namespace_info (false)
23064 {
23065 per_cu->cu = this;
23066 }
23067
23068 /* Destroy a dwarf2_cu. */
23069
23070 dwarf2_cu::~dwarf2_cu ()
23071 {
23072 per_cu->cu = NULL;
23073 }
23074
23075 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23076
23077 static void
23078 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23079 enum language pretend_language)
23080 {
23081 struct attribute *attr;
23082
23083 /* Set the language we're debugging. */
23084 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23085 if (attr != nullptr)
23086 set_cu_language (DW_UNSND (attr), cu);
23087 else
23088 {
23089 cu->language = pretend_language;
23090 cu->language_defn = language_def (cu->language);
23091 }
23092
23093 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23094 }
23095
23096 /* Increase the age counter on each cached compilation unit, and free
23097 any that are too old. */
23098
23099 static void
23100 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23101 {
23102 struct dwarf2_per_cu_data *per_cu, **last_chain;
23103
23104 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23105 per_cu = dwarf2_per_objfile->read_in_chain;
23106 while (per_cu != NULL)
23107 {
23108 per_cu->cu->last_used ++;
23109 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23110 dwarf2_mark (per_cu->cu);
23111 per_cu = per_cu->cu->read_in_chain;
23112 }
23113
23114 per_cu = dwarf2_per_objfile->read_in_chain;
23115 last_chain = &dwarf2_per_objfile->read_in_chain;
23116 while (per_cu != NULL)
23117 {
23118 struct dwarf2_per_cu_data *next_cu;
23119
23120 next_cu = per_cu->cu->read_in_chain;
23121
23122 if (!per_cu->cu->mark)
23123 {
23124 delete per_cu->cu;
23125 *last_chain = next_cu;
23126 }
23127 else
23128 last_chain = &per_cu->cu->read_in_chain;
23129
23130 per_cu = next_cu;
23131 }
23132 }
23133
23134 /* Remove a single compilation unit from the cache. */
23135
23136 static void
23137 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23138 {
23139 struct dwarf2_per_cu_data *per_cu, **last_chain;
23140 struct dwarf2_per_objfile *dwarf2_per_objfile
23141 = target_per_cu->dwarf2_per_objfile;
23142
23143 per_cu = dwarf2_per_objfile->read_in_chain;
23144 last_chain = &dwarf2_per_objfile->read_in_chain;
23145 while (per_cu != NULL)
23146 {
23147 struct dwarf2_per_cu_data *next_cu;
23148
23149 next_cu = per_cu->cu->read_in_chain;
23150
23151 if (per_cu == target_per_cu)
23152 {
23153 delete per_cu->cu;
23154 per_cu->cu = NULL;
23155 *last_chain = next_cu;
23156 break;
23157 }
23158 else
23159 last_chain = &per_cu->cu->read_in_chain;
23160
23161 per_cu = next_cu;
23162 }
23163 }
23164
23165 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23166 We store these in a hash table separate from the DIEs, and preserve them
23167 when the DIEs are flushed out of cache.
23168
23169 The CU "per_cu" pointer is needed because offset alone is not enough to
23170 uniquely identify the type. A file may have multiple .debug_types sections,
23171 or the type may come from a DWO file. Furthermore, while it's more logical
23172 to use per_cu->section+offset, with Fission the section with the data is in
23173 the DWO file but we don't know that section at the point we need it.
23174 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23175 because we can enter the lookup routine, get_die_type_at_offset, from
23176 outside this file, and thus won't necessarily have PER_CU->cu.
23177 Fortunately, PER_CU is stable for the life of the objfile. */
23178
23179 struct dwarf2_per_cu_offset_and_type
23180 {
23181 const struct dwarf2_per_cu_data *per_cu;
23182 sect_offset sect_off;
23183 struct type *type;
23184 };
23185
23186 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23187
23188 static hashval_t
23189 per_cu_offset_and_type_hash (const void *item)
23190 {
23191 const struct dwarf2_per_cu_offset_and_type *ofs
23192 = (const struct dwarf2_per_cu_offset_and_type *) item;
23193
23194 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23195 }
23196
23197 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23198
23199 static int
23200 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23201 {
23202 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23203 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23204 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23205 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23206
23207 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23208 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23209 }
23210
23211 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23212 table if necessary. For convenience, return TYPE.
23213
23214 The DIEs reading must have careful ordering to:
23215 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23216 reading current DIE.
23217 * Not trying to dereference contents of still incompletely read in types
23218 while reading in other DIEs.
23219 * Enable referencing still incompletely read in types just by a pointer to
23220 the type without accessing its fields.
23221
23222 Therefore caller should follow these rules:
23223 * Try to fetch any prerequisite types we may need to build this DIE type
23224 before building the type and calling set_die_type.
23225 * After building type call set_die_type for current DIE as soon as
23226 possible before fetching more types to complete the current type.
23227 * Make the type as complete as possible before fetching more types. */
23228
23229 static struct type *
23230 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23231 {
23232 struct dwarf2_per_objfile *dwarf2_per_objfile
23233 = cu->per_cu->dwarf2_per_objfile;
23234 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23235 struct objfile *objfile = dwarf2_per_objfile->objfile;
23236 struct attribute *attr;
23237 struct dynamic_prop prop;
23238
23239 /* For Ada types, make sure that the gnat-specific data is always
23240 initialized (if not already set). There are a few types where
23241 we should not be doing so, because the type-specific area is
23242 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23243 where the type-specific area is used to store the floatformat).
23244 But this is not a problem, because the gnat-specific information
23245 is actually not needed for these types. */
23246 if (need_gnat_info (cu)
23247 && TYPE_CODE (type) != TYPE_CODE_FUNC
23248 && TYPE_CODE (type) != TYPE_CODE_FLT
23249 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23250 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23251 && TYPE_CODE (type) != TYPE_CODE_METHOD
23252 && !HAVE_GNAT_AUX_INFO (type))
23253 INIT_GNAT_SPECIFIC (type);
23254
23255 /* Read DW_AT_allocated and set in type. */
23256 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23257 if (attr != NULL && attr->form_is_block ())
23258 {
23259 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23260 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23261 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23262 }
23263 else if (attr != NULL)
23264 {
23265 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23266 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23267 sect_offset_str (die->sect_off));
23268 }
23269
23270 /* Read DW_AT_associated and set in type. */
23271 attr = dwarf2_attr (die, DW_AT_associated, cu);
23272 if (attr != NULL && attr->form_is_block ())
23273 {
23274 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23275 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23276 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23277 }
23278 else if (attr != NULL)
23279 {
23280 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23281 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23282 sect_offset_str (die->sect_off));
23283 }
23284
23285 /* Read DW_AT_data_location and set in type. */
23286 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23287 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23288 cu->per_cu->addr_type ()))
23289 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23290
23291 if (dwarf2_per_objfile->die_type_hash == NULL)
23292 dwarf2_per_objfile->die_type_hash
23293 = htab_up (htab_create_alloc (127,
23294 per_cu_offset_and_type_hash,
23295 per_cu_offset_and_type_eq,
23296 NULL, xcalloc, xfree));
23297
23298 ofs.per_cu = cu->per_cu;
23299 ofs.sect_off = die->sect_off;
23300 ofs.type = type;
23301 slot = (struct dwarf2_per_cu_offset_and_type **)
23302 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23303 if (*slot)
23304 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23305 sect_offset_str (die->sect_off));
23306 *slot = XOBNEW (&objfile->objfile_obstack,
23307 struct dwarf2_per_cu_offset_and_type);
23308 **slot = ofs;
23309 return type;
23310 }
23311
23312 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23313 or return NULL if the die does not have a saved type. */
23314
23315 static struct type *
23316 get_die_type_at_offset (sect_offset sect_off,
23317 struct dwarf2_per_cu_data *per_cu)
23318 {
23319 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23320 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23321
23322 if (dwarf2_per_objfile->die_type_hash == NULL)
23323 return NULL;
23324
23325 ofs.per_cu = per_cu;
23326 ofs.sect_off = sect_off;
23327 slot = ((struct dwarf2_per_cu_offset_and_type *)
23328 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23329 if (slot)
23330 return slot->type;
23331 else
23332 return NULL;
23333 }
23334
23335 /* Look up the type for DIE in CU in die_type_hash,
23336 or return NULL if DIE does not have a saved type. */
23337
23338 static struct type *
23339 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23340 {
23341 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23342 }
23343
23344 /* Add a dependence relationship from CU to REF_PER_CU. */
23345
23346 static void
23347 dwarf2_add_dependence (struct dwarf2_cu *cu,
23348 struct dwarf2_per_cu_data *ref_per_cu)
23349 {
23350 void **slot;
23351
23352 if (cu->dependencies == NULL)
23353 cu->dependencies
23354 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23355 NULL, &cu->comp_unit_obstack,
23356 hashtab_obstack_allocate,
23357 dummy_obstack_deallocate);
23358
23359 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23360 if (*slot == NULL)
23361 *slot = ref_per_cu;
23362 }
23363
23364 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23365 Set the mark field in every compilation unit in the
23366 cache that we must keep because we are keeping CU. */
23367
23368 static int
23369 dwarf2_mark_helper (void **slot, void *data)
23370 {
23371 struct dwarf2_per_cu_data *per_cu;
23372
23373 per_cu = (struct dwarf2_per_cu_data *) *slot;
23374
23375 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23376 reading of the chain. As such dependencies remain valid it is not much
23377 useful to track and undo them during QUIT cleanups. */
23378 if (per_cu->cu == NULL)
23379 return 1;
23380
23381 if (per_cu->cu->mark)
23382 return 1;
23383 per_cu->cu->mark = true;
23384
23385 if (per_cu->cu->dependencies != NULL)
23386 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23387
23388 return 1;
23389 }
23390
23391 /* Set the mark field in CU and in every other compilation unit in the
23392 cache that we must keep because we are keeping CU. */
23393
23394 static void
23395 dwarf2_mark (struct dwarf2_cu *cu)
23396 {
23397 if (cu->mark)
23398 return;
23399 cu->mark = true;
23400 if (cu->dependencies != NULL)
23401 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23402 }
23403
23404 static void
23405 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23406 {
23407 while (per_cu)
23408 {
23409 per_cu->cu->mark = false;
23410 per_cu = per_cu->cu->read_in_chain;
23411 }
23412 }
23413
23414 /* Trivial hash function for partial_die_info: the hash value of a DIE
23415 is its offset in .debug_info for this objfile. */
23416
23417 static hashval_t
23418 partial_die_hash (const void *item)
23419 {
23420 const struct partial_die_info *part_die
23421 = (const struct partial_die_info *) item;
23422
23423 return to_underlying (part_die->sect_off);
23424 }
23425
23426 /* Trivial comparison function for partial_die_info structures: two DIEs
23427 are equal if they have the same offset. */
23428
23429 static int
23430 partial_die_eq (const void *item_lhs, const void *item_rhs)
23431 {
23432 const struct partial_die_info *part_die_lhs
23433 = (const struct partial_die_info *) item_lhs;
23434 const struct partial_die_info *part_die_rhs
23435 = (const struct partial_die_info *) item_rhs;
23436
23437 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23438 }
23439
23440 struct cmd_list_element *set_dwarf_cmdlist;
23441 struct cmd_list_element *show_dwarf_cmdlist;
23442
23443 static void
23444 set_dwarf_cmd (const char *args, int from_tty)
23445 {
23446 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23447 gdb_stdout);
23448 }
23449
23450 static void
23451 show_dwarf_cmd (const char *args, int from_tty)
23452 {
23453 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23454 }
23455
23456 static void
23457 show_check_physname (struct ui_file *file, int from_tty,
23458 struct cmd_list_element *c, const char *value)
23459 {
23460 fprintf_filtered (file,
23461 _("Whether to check \"physname\" is %s.\n"),
23462 value);
23463 }
23464
23465 void _initialize_dwarf2_read ();
23466 void
23467 _initialize_dwarf2_read ()
23468 {
23469 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23470 Set DWARF specific variables.\n\
23471 Configure DWARF variables such as the cache size."),
23472 &set_dwarf_cmdlist, "maintenance set dwarf ",
23473 0/*allow-unknown*/, &maintenance_set_cmdlist);
23474
23475 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23476 Show DWARF specific variables.\n\
23477 Show DWARF variables such as the cache size."),
23478 &show_dwarf_cmdlist, "maintenance show dwarf ",
23479 0/*allow-unknown*/, &maintenance_show_cmdlist);
23480
23481 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23482 &dwarf_max_cache_age, _("\
23483 Set the upper bound on the age of cached DWARF compilation units."), _("\
23484 Show the upper bound on the age of cached DWARF compilation units."), _("\
23485 A higher limit means that cached compilation units will be stored\n\
23486 in memory longer, and more total memory will be used. Zero disables\n\
23487 caching, which can slow down startup."),
23488 NULL,
23489 show_dwarf_max_cache_age,
23490 &set_dwarf_cmdlist,
23491 &show_dwarf_cmdlist);
23492
23493 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23494 Set debugging of the DWARF reader."), _("\
23495 Show debugging of the DWARF reader."), _("\
23496 When enabled (non-zero), debugging messages are printed during DWARF\n\
23497 reading and symtab expansion. A value of 1 (one) provides basic\n\
23498 information. A value greater than 1 provides more verbose information."),
23499 NULL,
23500 NULL,
23501 &setdebuglist, &showdebuglist);
23502
23503 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23504 Set debugging of the DWARF DIE reader."), _("\
23505 Show debugging of the DWARF DIE reader."), _("\
23506 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23507 The value is the maximum depth to print."),
23508 NULL,
23509 NULL,
23510 &setdebuglist, &showdebuglist);
23511
23512 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23513 Set debugging of the dwarf line reader."), _("\
23514 Show debugging of the dwarf line reader."), _("\
23515 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23516 A value of 1 (one) provides basic information.\n\
23517 A value greater than 1 provides more verbose information."),
23518 NULL,
23519 NULL,
23520 &setdebuglist, &showdebuglist);
23521
23522 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23523 Set cross-checking of \"physname\" code against demangler."), _("\
23524 Show cross-checking of \"physname\" code against demangler."), _("\
23525 When enabled, GDB's internal \"physname\" code is checked against\n\
23526 the demangler."),
23527 NULL, show_check_physname,
23528 &setdebuglist, &showdebuglist);
23529
23530 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23531 no_class, &use_deprecated_index_sections, _("\
23532 Set whether to use deprecated gdb_index sections."), _("\
23533 Show whether to use deprecated gdb_index sections."), _("\
23534 When enabled, deprecated .gdb_index sections are used anyway.\n\
23535 Normally they are ignored either because of a missing feature or\n\
23536 performance issue.\n\
23537 Warning: This option must be enabled before gdb reads the file."),
23538 NULL,
23539 NULL,
23540 &setlist, &showlist);
23541
23542 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23543 &dwarf2_locexpr_funcs);
23544 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23545 &dwarf2_loclist_funcs);
23546
23547 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23548 &dwarf2_block_frame_base_locexpr_funcs);
23549 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23550 &dwarf2_block_frame_base_loclist_funcs);
23551
23552 #if GDB_SELF_TEST
23553 selftests::register_test ("dw2_expand_symtabs_matching",
23554 selftests::dw2_expand_symtabs_matching::run_test);
23555 selftests::register_test ("dwarf2_find_containing_comp_unit",
23556 selftests::find_containing_comp_unit::run_test);
23557 #endif
23558 }