b2734dbee93f47e965b777406784a645e5964828
[deliverable/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 "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 gdb::optional<CORE_ADDR> base_address;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
543 /* Mark used when releasing cached dies. */
544 bool mark : 1;
545
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
550 bool has_loclist : 1;
551
552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
559 bool producer_is_icc : 1;
560 bool producer_is_icc_lt_14 : 1;
561 bool producer_is_codewarrior : 1;
562
563 /* When true, the file that we're processing is known to have
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
567 bool processing_has_namespace_info : 1;
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
588 };
589
590 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593 struct stmt_list_hash
594 {
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
599 sect_offset line_sect_off;
600 };
601
602 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
603 an object of this type. */
604
605 struct type_unit_group
606 {
607 /* dwarf2read.c's main "handle" on a TU symtab.
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
610 a "per_cu" handle on the symtab. */
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635 };
636
637 /* These sections are what may appear in a (real or virtual) DWO file. */
638
639 struct dwo_sections
640 {
641 struct dwarf2_section_info abbrev;
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
644 struct dwarf2_section_info loclists;
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
651 std::vector<dwarf2_section_info> types;
652 };
653
654 /* CUs/TUs in DWP/DWO files. */
655
656 struct dwo_unit
657 {
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
667 struct dwarf2_section_info *section;
668
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675 };
676
677 /* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681 enum dwp_v2_section_ids
682 {
683 DW_SECT_MIN = 1
684 };
685
686 /* Data for one DWO file.
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
696
697 struct dwo_file
698 {
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
706 const char *dwo_name = nullptr;
707
708 /* The DW_AT_comp_dir attribute. */
709 const char *comp_dir = nullptr;
710
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 gdb_bfd_ref_ptr dbfd;
714
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
718 struct dwo_sections sections {};
719
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
724 htab_up cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_up tus;
729 };
730
731 /* These sections are what may appear in a DWP file. */
732
733 struct dwp_sections
734 {
735 /* These are used by both DWP version 1 and 2. */
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
756 };
757
758 /* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
760
761 struct virtual_v1_dwo_sections
762 {
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 struct dwarf2_section_info info_or_types;
772 };
773
774 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779 struct virtual_v2_dwo_sections
780 {
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803 };
804
805 /* Contents of DWP hash tables. */
806
807 struct dwp_hash_table
808 {
809 uint32_t version, nr_columns;
810 uint32_t nr_units, nr_slots;
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822 #define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
834 };
835
836 /* Data for one DWP file. */
837
838 struct dwp_file
839 {
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
846 /* Name of the file. */
847 const char *name;
848
849 /* File format version. */
850 int version = 0;
851
852 /* The bfd. */
853 gdb_bfd_ref_ptr dbfd;
854
855 /* Section info for this file. */
856 struct dwp_sections sections {};
857
858 /* Table of CUs in the file. */
859 const struct dwp_hash_table *cus = nullptr;
860
861 /* Table of TUs in the file. */
862 const struct dwp_hash_table *tus = nullptr;
863
864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
865 htab_up loaded_cus;
866 htab_up loaded_tus;
867
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
872 };
873
874 /* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
877 struct exists to abstract away the constant parameters of die reading. */
878
879 struct die_reader_specs
880 {
881 /* The bfd of die_section. */
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
888 struct dwo_file *dwo_file;
889
890 /* The section the die comes from.
891 This is either .debug_info or .debug_types, or the .dwo variants. */
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
895 const gdb_byte *buffer;
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
899
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
902 };
903
904 /* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907 class cutu_reader : public die_reader_specs
908 {
909 public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
913 int use_existing_cu,
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
924 bool dummy_p = false;
925
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
930 private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
932 int use_existing_cu);
933
934 struct dwarf2_per_cu_data *m_this_cu;
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942 };
943
944 /* When we construct a partial symbol table entry we only
945 need this much information. */
946 struct partial_die_info : public allocate_on_obstack
947 {
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
965 /* Offset of this DIE. */
966 const sect_offset sect_off;
967
968 /* DWARF-2 tag for this DIE. */
969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
970
971 /* Assorted flags describing the data found in this DIE. */
972 const unsigned int has_children : 1;
973
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
979 unsigned int may_be_inlined : 1;
980
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
997 /* Flag set if fixup has been called on this die. */
998 unsigned int fixup_called : 1;
999
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
1006 /* The name of this DIE. Normally the value of DW_AT_name, but
1007 sometimes a default name for unnamed DIEs. */
1008 const char *name = nullptr;
1009
1010 /* The linkage name, if present. */
1011 const char *linkage_name = nullptr;
1012
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
1016 const char *scope = nullptr;
1017
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
1025 sect_offset sect_off;
1026 } d {};
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
1031
1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1033 DW_AT_sibling, if any. */
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
1036 const gdb_byte *sibling = nullptr;
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
1041 sect_offset spec_offset {};
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
1078 };
1079
1080 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083 static int bits_per_byte = 8;
1084
1085 struct variant_part_builder;
1086
1087 /* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
1089
1090 struct variant_field
1091 {
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107 };
1108
1109 /* This represents a DW_TAG_variant_part. */
1110
1111 struct variant_part_builder
1112 {
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
1121 };
1122
1123 struct nextfield
1124 {
1125 int accessibility = 0;
1126 int virtuality = 0;
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
1131 struct field field {};
1132 };
1133
1134 struct fnfieldlist
1135 {
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
1138 };
1139
1140 /* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143 struct field_info
1144 {
1145 /* List of data member and baseclasses fields. */
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
1148
1149 /* Set if the accessibility of one of the fields is not public. */
1150 int non_public_fields = 0;
1151
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 std::vector<struct fnfieldlist> fnfieldlists;
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1159 std::vector<struct decl_field> typedef_field_list;
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
1163 std::vector<struct decl_field> nested_types_list;
1164
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
1177 };
1178
1179 /* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
1184 static int dwarf_max_cache_age = 5;
1185 static void
1186 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
1188 {
1189 fprintf_filtered (file, _("The upper bound on the age of cached "
1190 "DWARF compilation units is %s.\n"),
1191 value);
1192 }
1193 \f
1194 /* local function prototypes */
1195
1196 static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
1199 static dwarf2_psymtab *create_partial_symtab
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
1202 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
1204 struct die_info *type_unit_die);
1205
1206 static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1208
1209 static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
1211 int, struct dwarf2_cu *);
1212
1213 static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
1215
1216 static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1218 int set_addrmap, struct dwarf2_cu *cu);
1219
1220 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1221 CORE_ADDR *highpc, int set_addrmap,
1222 struct dwarf2_cu *cu);
1223
1224 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
1226
1227 static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1229 int need_pc, struct dwarf2_cu *cu);
1230
1231 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1232
1233 static struct partial_die_info *load_partial_dies
1234 (const struct die_reader_specs *, const gdb_byte *, int);
1235
1236 /* A pair of partial_die_info and compilation unit. */
1237 struct cu_partial_die_info
1238 {
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
1247 { /* Nothing. */ }
1248
1249 private:
1250 cu_partial_die_info () = delete;
1251 };
1252
1253 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
1255
1256 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
1258 const gdb_byte *, bool *need_reprocess);
1259
1260 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1264
1265 static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
1268
1269 static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
1272
1273 static const char *read_indirect_string_at_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1275
1276 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
1278 unsigned int *);
1279
1280 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
1290
1291 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
1294 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
1296 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
1299 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1300
1301 static struct die_info *die_specification (struct die_info *die,
1302 struct dwarf2_cu **);
1303
1304 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1305 struct dwarf2_cu *cu);
1306
1307 static void dwarf_decode_lines (struct line_header *, const char *,
1308 struct dwarf2_cu *, dwarf2_psymtab *,
1309 CORE_ADDR, int decode_mapping);
1310
1311 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
1313
1314 static struct symbol *new_symbol (struct die_info *, struct type *,
1315 struct dwarf2_cu *, struct symbol * = NULL);
1316
1317 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1318 struct dwarf2_cu *);
1319
1320 static void dwarf2_const_value_attr (const struct attribute *attr,
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
1324 struct dwarf2_cu *cu, LONGEST *value,
1325 const gdb_byte **bytes,
1326 struct dwarf2_locexpr_baton **baton);
1327
1328 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1329
1330 static int need_gnat_info (struct dwarf2_cu *);
1331
1332 static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
1334
1335 static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
1340
1341 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1342 struct dwarf2_cu *);
1343
1344 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1345
1346 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
1348 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1349
1350 static char *typename_concat (struct obstack *obs, const char *prefix,
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
1353
1354 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1361
1362 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
1366 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *, dwarf2_psymtab *);
1368
1369 /* Return the .debug_loclists section to use for cu. */
1370 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
1372 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1373 values. Keep the items ordered with increasing constraints compliance. */
1374 enum pc_bounds_kind
1375 {
1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1377 PC_BOUNDS_NOT_PRESENT,
1378
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388 };
1389
1390 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
1393 dwarf2_psymtab *);
1394
1395 static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
1399 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
1402 static void dwarf2_add_field (struct field_info *, struct die_info *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_attach_fields_to_type (struct field_info *,
1406 struct type *, struct dwarf2_cu *);
1407
1408 static void dwarf2_add_member_fn (struct field_info *,
1409 struct die_info *, struct type *,
1410 struct dwarf2_cu *);
1411
1412 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1413 struct type *,
1414 struct dwarf2_cu *);
1415
1416 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1419
1420 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1421
1422 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
1424 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1425
1426 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
1428 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
1430 static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
1433 static const char *namespace_name (struct die_info *die,
1434 int *is_anonymous, struct dwarf2_cu *);
1435
1436 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1437
1438 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
1440
1441 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1442 struct dwarf2_cu *);
1443
1444 static struct die_info *read_die_and_siblings_1
1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1446 struct die_info *);
1447
1448 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
1451 struct die_info *parent);
1452
1453 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
1455 int);
1456
1457 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1458 struct die_info **, const gdb_byte *);
1459
1460 static void process_die (struct die_info *, struct dwarf2_cu *);
1461
1462 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1463 struct objfile *);
1464
1465 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1466
1467 static const char *dwarf2_full_name (const char *name,
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
1471 static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
1474 static struct die_info *dwarf2_extension (struct die_info *die,
1475 struct dwarf2_cu **);
1476
1477 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479 static void dump_die_for_error (struct die_info *);
1480
1481 static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
1483
1484 /*static*/ void dump_die (struct die_info *, int max_level);
1485
1486 static void store_in_ref_table (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1490 const struct attribute *,
1491 struct dwarf2_cu **);
1492
1493 static struct die_info *follow_die_ref (struct die_info *,
1494 const struct attribute *,
1495 struct dwarf2_cu **);
1496
1497 static struct die_info *follow_die_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504 static struct type *get_DW_AT_signature_type (struct die_info *,
1505 const struct attribute *,
1506 struct dwarf2_cu *);
1507
1508 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1509
1510 static void read_signatured_type (struct signatured_type *);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1550
1551 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1552
1553 static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1555
1556 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1559
1560 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1561 enum language);
1562
1563 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
1565
1566 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
1572 static void dwarf2_mark (struct dwarf2_cu *);
1573
1574 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
1576 static struct type *get_die_type_at_offset (sect_offset,
1577 struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1580
1581 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
1584 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1585
1586 /* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591 class dwarf2_queue_guard
1592 {
1593 public:
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->per_bfd->queue, empty);
1606 }
1607
1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1609
1610 private:
1611 dwarf2_per_objfile *m_per_objfile;
1612 };
1613
1614 dwarf2_queue_item::~dwarf2_queue_item ()
1615 {
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624 }
1625
1626 /* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629 struct file_and_directory
1630 {
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643 };
1644
1645 static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
1647
1648 static htab_up allocate_signatured_type_table ();
1649
1650 static htab_up allocate_dwo_unit_table ();
1651
1652 static struct dwo_unit *lookup_dwo_unit_in_dwp
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
1655 ULONGEST signature, int is_debug_types);
1656
1657 static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1659
1660 static struct dwo_unit *lookup_dwo_comp_unit
1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1662
1663 static struct dwo_unit *lookup_dwo_type_unit
1664 (struct signatured_type *, const char *, const char *);
1665
1666 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
1668 /* A unique pointer to a dwo_file. */
1669
1670 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1671
1672 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static void check_producer (struct dwarf2_cu *cu);
1675
1676 static void free_line_header_voidp (void *arg);
1677 \f
1678 /* Various complaints about symbol reading that don't abort the process. */
1679
1680 static void
1681 dwarf2_debug_line_missing_file_complaint (void)
1682 {
1683 complaint (_(".debug_line section has line data without a file"));
1684 }
1685
1686 static void
1687 dwarf2_debug_line_missing_end_sequence_complaint (void)
1688 {
1689 complaint (_(".debug_line section has line "
1690 "program sequence without an end"));
1691 }
1692
1693 static void
1694 dwarf2_complex_location_expr_complaint (void)
1695 {
1696 complaint (_("location expression too complex"));
1697 }
1698
1699 static void
1700 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702 {
1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1704 arg1, arg2, arg3);
1705 }
1706
1707 static void
1708 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709 {
1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1711 arg1, arg2);
1712 }
1713
1714 /* Hash function for line_header_hash. */
1715
1716 static hashval_t
1717 line_header_hash (const struct line_header *ofs)
1718 {
1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1720 }
1721
1722 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724 static hashval_t
1725 line_header_hash_voidp (const void *item)
1726 {
1727 const struct line_header *ofs = (const struct line_header *) item;
1728
1729 return line_header_hash (ofs);
1730 }
1731
1732 /* Equality function for line_header_hash. */
1733
1734 static int
1735 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736 {
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1739
1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742 }
1743
1744 \f
1745
1746 /* See declaration. */
1747
1748 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1749 bool can_copy_)
1750 : can_copy (can_copy_)
1751 {
1752 if (names == NULL)
1753 names = &dwarf2_elf_names;
1754
1755 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1756 locate_sections (obfd, sec, *names);
1757 }
1758
1759 dwarf2_per_bfd::~dwarf2_per_bfd ()
1760 {
1761 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1762 free_cached_comp_units ();
1763
1764 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1765 per_cu->imported_symtabs_free ();
1766
1767 for (signatured_type *sig_type : all_type_units)
1768 sig_type->per_cu.imported_symtabs_free ();
1769
1770 /* Everything else should be on this->obstack. */
1771 }
1772
1773 /* See declaration. */
1774
1775 void
1776 dwarf2_per_bfd::free_cached_comp_units ()
1777 {
1778 dwarf2_per_cu_data *per_cu = read_in_chain;
1779 dwarf2_per_cu_data **last_chain = &read_in_chain;
1780 while (per_cu != NULL)
1781 {
1782 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1783
1784 delete per_cu->cu;
1785 *last_chain = next_cu;
1786 per_cu = next_cu;
1787 }
1788 }
1789
1790 /* A helper class that calls free_cached_comp_units on
1791 destruction. */
1792
1793 class free_cached_comp_units
1794 {
1795 public:
1796
1797 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1798 : m_per_objfile (per_objfile)
1799 {
1800 }
1801
1802 ~free_cached_comp_units ()
1803 {
1804 m_per_objfile->per_bfd->free_cached_comp_units ();
1805 }
1806
1807 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1808
1809 private:
1810
1811 dwarf2_per_objfile *m_per_objfile;
1812 };
1813
1814 /* Try to locate the sections we need for DWARF 2 debugging
1815 information and return true if we have enough to do something.
1816 NAMES points to the dwarf2 section names, or is NULL if the standard
1817 ELF names are used. CAN_COPY is true for formats where symbol
1818 interposition is possible and so symbol values must follow copy
1819 relocation rules. */
1820
1821 int
1822 dwarf2_has_info (struct objfile *objfile,
1823 const struct dwarf2_debug_sections *names,
1824 bool can_copy)
1825 {
1826 if (objfile->flags & OBJF_READNEVER)
1827 return 0;
1828
1829 struct dwarf2_per_objfile *dwarf2_per_objfile
1830 = get_dwarf2_per_objfile (objfile);
1831
1832 if (dwarf2_per_objfile == NULL)
1833 {
1834 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1835 sharing yet). */
1836 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1837
1838 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1839 }
1840
1841 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1842 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1843 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1844 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1845 }
1846
1847 /* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
1849
1850 static int
1851 section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
1853 {
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
1861 }
1862
1863 /* See declaration. */
1864
1865 void
1866 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
1868 {
1869 flagword aflag = bfd_section_flags (sectp);
1870
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
1883 else if (section_is_p (sectp->name, &names.info))
1884 {
1885 this->info.s.section = sectp;
1886 this->info.size = bfd_section_size (sectp);
1887 }
1888 else if (section_is_p (sectp->name, &names.abbrev))
1889 {
1890 this->abbrev.s.section = sectp;
1891 this->abbrev.size = bfd_section_size (sectp);
1892 }
1893 else if (section_is_p (sectp->name, &names.line))
1894 {
1895 this->line.s.section = sectp;
1896 this->line.size = bfd_section_size (sectp);
1897 }
1898 else if (section_is_p (sectp->name, &names.loc))
1899 {
1900 this->loc.s.section = sectp;
1901 this->loc.size = bfd_section_size (sectp);
1902 }
1903 else if (section_is_p (sectp->name, &names.loclists))
1904 {
1905 this->loclists.s.section = sectp;
1906 this->loclists.size = bfd_section_size (sectp);
1907 }
1908 else if (section_is_p (sectp->name, &names.macinfo))
1909 {
1910 this->macinfo.s.section = sectp;
1911 this->macinfo.size = bfd_section_size (sectp);
1912 }
1913 else if (section_is_p (sectp->name, &names.macro))
1914 {
1915 this->macro.s.section = sectp;
1916 this->macro.size = bfd_section_size (sectp);
1917 }
1918 else if (section_is_p (sectp->name, &names.str))
1919 {
1920 this->str.s.section = sectp;
1921 this->str.size = bfd_section_size (sectp);
1922 }
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
1928 else if (section_is_p (sectp->name, &names.line_str))
1929 {
1930 this->line_str.s.section = sectp;
1931 this->line_str.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.addr))
1934 {
1935 this->addr.s.section = sectp;
1936 this->addr.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.frame))
1939 {
1940 this->frame.s.section = sectp;
1941 this->frame.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.eh_frame))
1944 {
1945 this->eh_frame.s.section = sectp;
1946 this->eh_frame.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.ranges))
1949 {
1950 this->ranges.s.section = sectp;
1951 this->ranges.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.rnglists))
1954 {
1955 this->rnglists.s.section = sectp;
1956 this->rnglists.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.types))
1959 {
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
1963 type_section.s.section = sectp;
1964 type_section.size = bfd_section_size (sectp);
1965
1966 this->types.push_back (type_section);
1967 }
1968 else if (section_is_p (sectp->name, &names.gdb_index))
1969 {
1970 this->gdb_index.s.section = sectp;
1971 this->gdb_index.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
1976 this->debug_names.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
1981 this->debug_aranges.size = bfd_section_size (sectp);
1982 }
1983
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
1986 this->has_section_at_zero = true;
1987 }
1988
1989 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1990 SECTION_NAME. */
1991
1992 void
1993 dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
1995 asection **sectp, const gdb_byte **bufp,
1996 bfd_size_type *sizep)
1997 {
1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1999 struct dwarf2_section_info *info;
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->per_bfd->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->per_bfd->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
2021
2022 info->read (objfile);
2023
2024 *sectp = info->get_bfd_section ();
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027 }
2028
2029 /* A helper function to find the sections for a .dwz file. */
2030
2031 static void
2032 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033 {
2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
2040 dwz_file->abbrev.s.section = sectp;
2041 dwz_file->abbrev.size = bfd_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
2045 dwz_file->info.s.section = sectp;
2046 dwz_file->info.size = bfd_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
2050 dwz_file->str.s.section = sectp;
2051 dwz_file->str.size = bfd_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
2055 dwz_file->line.s.section = sectp;
2056 dwz_file->line.size = bfd_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
2060 dwz_file->macro.s.section = sectp;
2061 dwz_file->macro.size = bfd_section_size (sectp);
2062 }
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
2065 dwz_file->gdb_index.s.section = sectp;
2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
2071 dwz_file->debug_names.size = bfd_section_size (sectp);
2072 }
2073 }
2074
2075 /* See dwarf2read.h. */
2076
2077 struct dwz_file *
2078 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2079 {
2080 const char *filename;
2081 bfd_size_type buildid_len_arg;
2082 size_t buildid_len;
2083 bfd_byte *buildid;
2084
2085 if (dwarf2_per_objfile->per_bfd->dwz_file != NULL)
2086 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2087
2088 bfd_set_error (bfd_error_no_error);
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2101
2102 buildid_len = (size_t) buildid_len_arg;
2103
2104 filename = data.get ();
2105
2106 std::string abs_storage;
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2111
2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2113 filename = abs_storage.c_str ();
2114 }
2115
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2119 if (dwz_bfd != NULL)
2120 {
2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2122 dwz_bfd.reset (nullptr);
2123 }
2124
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
2157
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
2160
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->per_bfd->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
2165 }
2166 \f
2167 /* DWARF quick_symbols_functions support. */
2168
2169 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174 struct quick_file_names
2175 {
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189 };
2190
2191 /* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194 struct dwarf2_per_cu_quick_data
2195 {
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
2203 struct compunit_symtab *compunit_symtab;
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212 };
2213
2214 /* Utility hash function for a stmt_list_hash. */
2215
2216 static hashval_t
2217 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218 {
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2223 v += to_underlying (stmt_list_hash->line_sect_off);
2224 return v;
2225 }
2226
2227 /* Utility equality function for a stmt_list_hash. */
2228
2229 static int
2230 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232 {
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
2239 return lhs->line_sect_off == rhs->line_sect_off;
2240 }
2241
2242 /* Hash function for a quick_file_names. */
2243
2244 static hashval_t
2245 hash_file_name_entry (const void *e)
2246 {
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
2249
2250 return hash_stmt_list_entry (&file_data->hash);
2251 }
2252
2253 /* Equality function for a quick_file_names. */
2254
2255 static int
2256 eq_file_name_entry (const void *a, const void *b)
2257 {
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2260
2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2262 }
2263
2264 /* Delete function for a quick_file_names. */
2265
2266 static void
2267 delete_file_name_entry (void *e)
2268 {
2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on the obstack, so we don't
2280 free it here. */
2281 }
2282
2283 /* Create a quick_file_names hash table. */
2284
2285 static htab_up
2286 create_quick_file_names_table (unsigned int nr_initial_entries)
2287 {
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
2291 }
2292
2293 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297 static void
2298 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2299 {
2300 if (per_cu->is_debug_types)
2301 load_full_type_unit (per_cu);
2302 else
2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2304
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2309 }
2310
2311 /* Read in the symbols for PER_CU. */
2312
2313 static void
2314 dw2_do_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 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
2320 if (per_cu->type_unit_group_p ())
2321 return;
2322
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2327
2328 if (dwarf2_per_objfile->per_bfd->using_index
2329 ? per_cu->v.quick->compunit_symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
2333 load_cu (per_cu, skip_partial);
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
2339 && per_cu->cu != NULL
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->per_bfd->index_table != NULL
2342 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
2345 queue_and_load_all_dwo_tus (per_cu);
2346 }
2347
2348 process_queue (dwarf2_per_objfile);
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
2352 age_cached_comp_units (dwarf2_per_objfile);
2353 }
2354
2355 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2358
2359 static struct compunit_symtab *
2360 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2361 {
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
2364 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2365 if (!per_cu->v.quick->compunit_symtab)
2366 {
2367 free_cached_comp_units freer (dwarf2_per_objfile);
2368 scoped_restore decrementer = increment_reading_symtab ();
2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
2370 process_cu_includes (dwarf2_per_objfile);
2371 }
2372
2373 return per_cu->v.quick->compunit_symtab;
2374 }
2375
2376 /* See declaration. */
2377
2378 dwarf2_per_cu_data *
2379 dwarf2_per_bfd::get_cutu (int index)
2380 {
2381 if (index >= this->all_comp_units.size ())
2382 {
2383 index -= this->all_comp_units.size ();
2384 gdb_assert (index < this->all_type_units.size ());
2385 return &this->all_type_units[index]->per_cu;
2386 }
2387
2388 return this->all_comp_units[index];
2389 }
2390
2391 /* See declaration. */
2392
2393 dwarf2_per_cu_data *
2394 dwarf2_per_bfd::get_cu (int index)
2395 {
2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2397
2398 return this->all_comp_units[index];
2399 }
2400
2401 /* See declaration. */
2402
2403 signatured_type *
2404 dwarf2_per_bfd::get_tu (int index)
2405 {
2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2407
2408 return this->all_type_units[index];
2409 }
2410
2411 /* See read.h. */
2412
2413 dwarf2_per_cu_data *
2414 dwarf2_per_bfd::allocate_per_cu ()
2415 {
2416 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2417 result->index = m_num_psymtabs++;
2418 return result;
2419 }
2420
2421 /* See read.h. */
2422
2423 signatured_type *
2424 dwarf2_per_bfd::allocate_signatured_type ()
2425 {
2426 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2427 result->per_cu.index = m_num_psymtabs++;
2428 return result;
2429 }
2430
2431 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2432 obstack, and constructed with the specified field values. */
2433
2434 static dwarf2_per_cu_data *
2435 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2436 struct dwarf2_section_info *section,
2437 int is_dwz,
2438 sect_offset sect_off, ULONGEST length)
2439 {
2440 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2441 the_cu->sect_off = sect_off;
2442 the_cu->length = length;
2443 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2444 the_cu->section = section;
2445 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2446 struct dwarf2_per_cu_quick_data);
2447 the_cu->is_dwz = is_dwz;
2448 return the_cu;
2449 }
2450
2451 /* A helper for create_cus_from_index that handles a given list of
2452 CUs. */
2453
2454 static void
2455 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2456 const gdb_byte *cu_list, offset_type n_elements,
2457 struct dwarf2_section_info *section,
2458 int is_dwz)
2459 {
2460 for (offset_type i = 0; i < n_elements; i += 2)
2461 {
2462 gdb_static_assert (sizeof (ULONGEST) >= 8);
2463
2464 sect_offset sect_off
2465 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2466 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2467 cu_list += 2 * 8;
2468
2469 dwarf2_per_cu_data *per_cu
2470 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2471 sect_off, length);
2472 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2473 }
2474 }
2475
2476 /* Read the CU list from the mapped index, and use it to create all
2477 the CU objects for this objfile. */
2478
2479 static void
2480 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2481 const gdb_byte *cu_list, offset_type cu_list_elements,
2482 const gdb_byte *dwz_list, offset_type dwz_elements)
2483 {
2484 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2485 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2486 ((cu_list_elements + dwz_elements) / 2);
2487
2488 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2489 &dwarf2_per_objfile->per_bfd->info, 0);
2490
2491 if (dwz_elements == 0)
2492 return;
2493
2494 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2495 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2496 &dwz->info, 1);
2497 }
2498
2499 /* Create the signatured type hash table from the index. */
2500
2501 static void
2502 create_signatured_type_table_from_index
2503 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2504 struct dwarf2_section_info *section,
2505 const gdb_byte *bytes,
2506 offset_type elements)
2507 {
2508 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2509 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2510
2511 htab_up sig_types_hash = allocate_signatured_type_table ();
2512
2513 for (offset_type i = 0; i < elements; i += 3)
2514 {
2515 struct signatured_type *sig_type;
2516 ULONGEST signature;
2517 void **slot;
2518 cu_offset type_offset_in_tu;
2519
2520 gdb_static_assert (sizeof (ULONGEST) >= 8);
2521 sect_offset sect_off
2522 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2523 type_offset_in_tu
2524 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2525 BFD_ENDIAN_LITTLE);
2526 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2527 bytes += 3 * 8;
2528
2529 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2530 sig_type->signature = signature;
2531 sig_type->type_offset_in_tu = type_offset_in_tu;
2532 sig_type->per_cu.is_debug_types = 1;
2533 sig_type->per_cu.section = section;
2534 sig_type->per_cu.sect_off = sect_off;
2535 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2536 sig_type->per_cu.v.quick
2537 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2538 struct dwarf2_per_cu_quick_data);
2539
2540 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2541 *slot = sig_type;
2542
2543 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2544 }
2545
2546 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2547 }
2548
2549 /* Create the signatured type hash table from .debug_names. */
2550
2551 static void
2552 create_signatured_type_table_from_debug_names
2553 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2554 const mapped_debug_names &map,
2555 struct dwarf2_section_info *section,
2556 struct dwarf2_section_info *abbrev_section)
2557 {
2558 struct objfile *objfile = dwarf2_per_objfile->objfile;
2559
2560 section->read (objfile);
2561 abbrev_section->read (objfile);
2562
2563 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2564 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2565
2566 htab_up sig_types_hash = allocate_signatured_type_table ();
2567
2568 for (uint32_t i = 0; i < map.tu_count; ++i)
2569 {
2570 struct signatured_type *sig_type;
2571 void **slot;
2572
2573 sect_offset sect_off
2574 = (sect_offset) (extract_unsigned_integer
2575 (map.tu_table_reordered + i * map.offset_size,
2576 map.offset_size,
2577 map.dwarf5_byte_order));
2578
2579 comp_unit_head cu_header;
2580 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2581 abbrev_section,
2582 section->buffer + to_underlying (sect_off),
2583 rcuh_kind::TYPE);
2584
2585 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2586 sig_type->signature = cu_header.signature;
2587 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2588 sig_type->per_cu.is_debug_types = 1;
2589 sig_type->per_cu.section = section;
2590 sig_type->per_cu.sect_off = sect_off;
2591 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2592 sig_type->per_cu.v.quick
2593 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2594 struct dwarf2_per_cu_quick_data);
2595
2596 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2597 *slot = sig_type;
2598
2599 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2600 }
2601
2602 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2603 }
2604
2605 /* Read the address map data from the mapped index, and use it to
2606 populate the objfile's psymtabs_addrmap. */
2607
2608 static void
2609 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2610 struct mapped_index *index)
2611 {
2612 struct objfile *objfile = dwarf2_per_objfile->objfile;
2613 struct gdbarch *gdbarch = objfile->arch ();
2614 const gdb_byte *iter, *end;
2615 struct addrmap *mutable_map;
2616 CORE_ADDR baseaddr;
2617
2618 auto_obstack temp_obstack;
2619
2620 mutable_map = addrmap_create_mutable (&temp_obstack);
2621
2622 iter = index->address_table.data ();
2623 end = iter + index->address_table.size ();
2624
2625 baseaddr = objfile->text_section_offset ();
2626
2627 while (iter < end)
2628 {
2629 ULONGEST hi, lo, cu_index;
2630 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2631 iter += 8;
2632 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2633 iter += 8;
2634 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2635 iter += 4;
2636
2637 if (lo > hi)
2638 {
2639 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2640 hex_string (lo), hex_string (hi));
2641 continue;
2642 }
2643
2644 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2645 {
2646 complaint (_(".gdb_index address table has invalid CU number %u"),
2647 (unsigned) cu_index);
2648 continue;
2649 }
2650
2651 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2652 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2653 addrmap_set_empty (mutable_map, lo, hi - 1,
2654 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2655 }
2656
2657 objfile->partial_symtabs->psymtabs_addrmap
2658 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2659 }
2660
2661 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2662 populate the objfile's psymtabs_addrmap. */
2663
2664 static void
2665 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2666 struct dwarf2_section_info *section)
2667 {
2668 struct objfile *objfile = dwarf2_per_objfile->objfile;
2669 bfd *abfd = objfile->obfd;
2670 struct gdbarch *gdbarch = objfile->arch ();
2671 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2672
2673 auto_obstack temp_obstack;
2674 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2675
2676 std::unordered_map<sect_offset,
2677 dwarf2_per_cu_data *,
2678 gdb::hash_enum<sect_offset>>
2679 debug_info_offset_to_per_cu;
2680 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2681 {
2682 const auto insertpair
2683 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2684 if (!insertpair.second)
2685 {
2686 warning (_("Section .debug_aranges in %s has duplicate "
2687 "debug_info_offset %s, ignoring .debug_aranges."),
2688 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2689 return;
2690 }
2691 }
2692
2693 section->read (objfile);
2694
2695 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2696
2697 const gdb_byte *addr = section->buffer;
2698
2699 while (addr < section->buffer + section->size)
2700 {
2701 const gdb_byte *const entry_addr = addr;
2702 unsigned int bytes_read;
2703
2704 const LONGEST entry_length = read_initial_length (abfd, addr,
2705 &bytes_read);
2706 addr += bytes_read;
2707
2708 const gdb_byte *const entry_end = addr + entry_length;
2709 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2710 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2711 if (addr + entry_length > section->buffer + section->size)
2712 {
2713 warning (_("Section .debug_aranges in %s entry at offset %s "
2714 "length %s exceeds section length %s, "
2715 "ignoring .debug_aranges."),
2716 objfile_name (objfile),
2717 plongest (entry_addr - section->buffer),
2718 plongest (bytes_read + entry_length),
2719 pulongest (section->size));
2720 return;
2721 }
2722
2723 /* The version number. */
2724 const uint16_t version = read_2_bytes (abfd, addr);
2725 addr += 2;
2726 if (version != 2)
2727 {
2728 warning (_("Section .debug_aranges in %s entry at offset %s "
2729 "has unsupported version %d, ignoring .debug_aranges."),
2730 objfile_name (objfile),
2731 plongest (entry_addr - section->buffer), version);
2732 return;
2733 }
2734
2735 const uint64_t debug_info_offset
2736 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2737 addr += offset_size;
2738 const auto per_cu_it
2739 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2740 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2741 {
2742 warning (_("Section .debug_aranges in %s entry at offset %s "
2743 "debug_info_offset %s does not exists, "
2744 "ignoring .debug_aranges."),
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer),
2747 pulongest (debug_info_offset));
2748 return;
2749 }
2750 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2751
2752 const uint8_t address_size = *addr++;
2753 if (address_size < 1 || address_size > 8)
2754 {
2755 warning (_("Section .debug_aranges in %s entry at offset %s "
2756 "address_size %u is invalid, ignoring .debug_aranges."),
2757 objfile_name (objfile),
2758 plongest (entry_addr - section->buffer), address_size);
2759 return;
2760 }
2761
2762 const uint8_t segment_selector_size = *addr++;
2763 if (segment_selector_size != 0)
2764 {
2765 warning (_("Section .debug_aranges in %s entry at offset %s "
2766 "segment_selector_size %u is not supported, "
2767 "ignoring .debug_aranges."),
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer),
2770 segment_selector_size);
2771 return;
2772 }
2773
2774 /* Must pad to an alignment boundary that is twice the address
2775 size. It is undocumented by the DWARF standard but GCC does
2776 use it. */
2777 for (size_t padding = ((-(addr - section->buffer))
2778 & (2 * address_size - 1));
2779 padding > 0; padding--)
2780 if (*addr++ != 0)
2781 {
2782 warning (_("Section .debug_aranges in %s entry at offset %s "
2783 "padding is not zero, ignoring .debug_aranges."),
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
2786 return;
2787 }
2788
2789 for (;;)
2790 {
2791 if (addr + 2 * address_size > entry_end)
2792 {
2793 warning (_("Section .debug_aranges in %s entry at offset %s "
2794 "address list is not properly terminated, "
2795 "ignoring .debug_aranges."),
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer));
2798 return;
2799 }
2800 ULONGEST start = extract_unsigned_integer (addr, address_size,
2801 dwarf5_byte_order);
2802 addr += address_size;
2803 ULONGEST length = extract_unsigned_integer (addr, address_size,
2804 dwarf5_byte_order);
2805 addr += address_size;
2806 if (start == 0 && length == 0)
2807 break;
2808 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2809 {
2810 /* Symbol was eliminated due to a COMDAT group. */
2811 continue;
2812 }
2813 ULONGEST end = start + length;
2814 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2815 - baseaddr);
2816 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2817 - baseaddr);
2818 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2819 }
2820 }
2821
2822 objfile->partial_symtabs->psymtabs_addrmap
2823 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2824 }
2825
2826 /* Find a slot in the mapped index INDEX for the object named NAME.
2827 If NAME is found, set *VEC_OUT to point to the CU vector in the
2828 constant pool and return true. If NAME cannot be found, return
2829 false. */
2830
2831 static bool
2832 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2833 offset_type **vec_out)
2834 {
2835 offset_type hash;
2836 offset_type slot, step;
2837 int (*cmp) (const char *, const char *);
2838
2839 gdb::unique_xmalloc_ptr<char> without_params;
2840 if (current_language->la_language == language_cplus
2841 || current_language->la_language == language_fortran
2842 || current_language->la_language == language_d)
2843 {
2844 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2845 not contain any. */
2846
2847 if (strchr (name, '(') != NULL)
2848 {
2849 without_params = cp_remove_params (name);
2850
2851 if (without_params != NULL)
2852 name = without_params.get ();
2853 }
2854 }
2855
2856 /* Index version 4 did not support case insensitive searches. But the
2857 indices for case insensitive languages are built in lowercase, therefore
2858 simulate our NAME being searched is also lowercased. */
2859 hash = mapped_index_string_hash ((index->version == 4
2860 && case_sensitivity == case_sensitive_off
2861 ? 5 : index->version),
2862 name);
2863
2864 slot = hash & (index->symbol_table.size () - 1);
2865 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2866 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2867
2868 for (;;)
2869 {
2870 const char *str;
2871
2872 const auto &bucket = index->symbol_table[slot];
2873 if (bucket.name == 0 && bucket.vec == 0)
2874 return false;
2875
2876 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2877 if (!cmp (name, str))
2878 {
2879 *vec_out = (offset_type *) (index->constant_pool
2880 + MAYBE_SWAP (bucket.vec));
2881 return true;
2882 }
2883
2884 slot = (slot + step) & (index->symbol_table.size () - 1);
2885 }
2886 }
2887
2888 /* A helper function that reads the .gdb_index from BUFFER and fills
2889 in MAP. FILENAME is the name of the file containing the data;
2890 it is used for error reporting. DEPRECATED_OK is true if it is
2891 ok to use deprecated sections.
2892
2893 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2894 out parameters that are filled in with information about the CU and
2895 TU lists in the section.
2896
2897 Returns true if all went well, false otherwise. */
2898
2899 static bool
2900 read_gdb_index_from_buffer (const char *filename,
2901 bool deprecated_ok,
2902 gdb::array_view<const gdb_byte> buffer,
2903 struct mapped_index *map,
2904 const gdb_byte **cu_list,
2905 offset_type *cu_list_elements,
2906 const gdb_byte **types_list,
2907 offset_type *types_list_elements)
2908 {
2909 const gdb_byte *addr = &buffer[0];
2910
2911 /* Version check. */
2912 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2913 /* Versions earlier than 3 emitted every copy of a psymbol. This
2914 causes the index to behave very poorly for certain requests. Version 3
2915 contained incomplete addrmap. So, it seems better to just ignore such
2916 indices. */
2917 if (version < 4)
2918 {
2919 static int warning_printed = 0;
2920 if (!warning_printed)
2921 {
2922 warning (_("Skipping obsolete .gdb_index section in %s."),
2923 filename);
2924 warning_printed = 1;
2925 }
2926 return 0;
2927 }
2928 /* Index version 4 uses a different hash function than index version
2929 5 and later.
2930
2931 Versions earlier than 6 did not emit psymbols for inlined
2932 functions. Using these files will cause GDB not to be able to
2933 set breakpoints on inlined functions by name, so we ignore these
2934 indices unless the user has done
2935 "set use-deprecated-index-sections on". */
2936 if (version < 6 && !deprecated_ok)
2937 {
2938 static int warning_printed = 0;
2939 if (!warning_printed)
2940 {
2941 warning (_("\
2942 Skipping deprecated .gdb_index section in %s.\n\
2943 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2944 to use the section anyway."),
2945 filename);
2946 warning_printed = 1;
2947 }
2948 return 0;
2949 }
2950 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2951 of the TU (for symbols coming from TUs),
2952 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2953 Plus gold-generated indices can have duplicate entries for global symbols,
2954 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2955 These are just performance bugs, and we can't distinguish gdb-generated
2956 indices from gold-generated ones, so issue no warning here. */
2957
2958 /* Indexes with higher version than the one supported by GDB may be no
2959 longer backward compatible. */
2960 if (version > 8)
2961 return 0;
2962
2963 map->version = version;
2964
2965 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2966
2967 int i = 0;
2968 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2969 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2970 / 8);
2971 ++i;
2972
2973 *types_list = addr + MAYBE_SWAP (metadata[i]);
2974 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2975 - MAYBE_SWAP (metadata[i]))
2976 / 8);
2977 ++i;
2978
2979 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2980 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2981 map->address_table
2982 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2983 ++i;
2984
2985 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2986 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2987 map->symbol_table
2988 = gdb::array_view<mapped_index::symbol_table_slot>
2989 ((mapped_index::symbol_table_slot *) symbol_table,
2990 (mapped_index::symbol_table_slot *) symbol_table_end);
2991
2992 ++i;
2993 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2994
2995 return 1;
2996 }
2997
2998 /* Callback types for dwarf2_read_gdb_index. */
2999
3000 typedef gdb::function_view
3001 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3002 get_gdb_index_contents_ftype;
3003 typedef gdb::function_view
3004 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3005 get_gdb_index_contents_dwz_ftype;
3006
3007 /* Read .gdb_index. If everything went ok, initialize the "quick"
3008 elements of all the CUs and return 1. Otherwise, return 0. */
3009
3010 static int
3011 dwarf2_read_gdb_index
3012 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3013 get_gdb_index_contents_ftype get_gdb_index_contents,
3014 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3015 {
3016 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3017 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3018 struct dwz_file *dwz;
3019 struct objfile *objfile = dwarf2_per_objfile->objfile;
3020
3021 gdb::array_view<const gdb_byte> main_index_contents
3022 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3023
3024 if (main_index_contents.empty ())
3025 return 0;
3026
3027 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3028 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3029 use_deprecated_index_sections,
3030 main_index_contents, map.get (), &cu_list,
3031 &cu_list_elements, &types_list,
3032 &types_list_elements))
3033 return 0;
3034
3035 /* Don't use the index if it's empty. */
3036 if (map->symbol_table.empty ())
3037 return 0;
3038
3039 /* If there is a .dwz file, read it so we can get its CU list as
3040 well. */
3041 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3042 if (dwz != NULL)
3043 {
3044 struct mapped_index dwz_map;
3045 const gdb_byte *dwz_types_ignore;
3046 offset_type dwz_types_elements_ignore;
3047
3048 gdb::array_view<const gdb_byte> dwz_index_content
3049 = get_gdb_index_contents_dwz (objfile, dwz);
3050
3051 if (dwz_index_content.empty ())
3052 return 0;
3053
3054 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3055 1, dwz_index_content, &dwz_map,
3056 &dwz_list, &dwz_list_elements,
3057 &dwz_types_ignore,
3058 &dwz_types_elements_ignore))
3059 {
3060 warning (_("could not read '.gdb_index' section from %s; skipping"),
3061 bfd_get_filename (dwz->dwz_bfd.get ()));
3062 return 0;
3063 }
3064 }
3065
3066 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3067 dwz_list, dwz_list_elements);
3068
3069 if (types_list_elements)
3070 {
3071 /* We can only handle a single .debug_types when we have an
3072 index. */
3073 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3074 return 0;
3075
3076 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3077
3078 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3079 types_list, types_list_elements);
3080 }
3081
3082 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3083
3084 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3085 dwarf2_per_objfile->per_bfd->using_index = 1;
3086 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3087 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3088
3089 return 1;
3090 }
3091
3092 /* die_reader_func for dw2_get_file_names. */
3093
3094 static void
3095 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3096 const gdb_byte *info_ptr,
3097 struct die_info *comp_unit_die)
3098 {
3099 struct dwarf2_cu *cu = reader->cu;
3100 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3101 struct dwarf2_per_objfile *dwarf2_per_objfile
3102 = cu->per_cu->dwarf2_per_objfile;
3103 struct dwarf2_per_cu_data *lh_cu;
3104 struct attribute *attr;
3105 void **slot;
3106 struct quick_file_names *qfn;
3107
3108 gdb_assert (! this_cu->is_debug_types);
3109
3110 /* Our callers never want to match partial units -- instead they
3111 will match the enclosing full CU. */
3112 if (comp_unit_die->tag == DW_TAG_partial_unit)
3113 {
3114 this_cu->v.quick->no_file_data = 1;
3115 return;
3116 }
3117
3118 lh_cu = this_cu;
3119 slot = NULL;
3120
3121 line_header_up lh;
3122 sect_offset line_offset {};
3123
3124 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3125 if (attr != nullptr)
3126 {
3127 struct quick_file_names find_entry;
3128
3129 line_offset = (sect_offset) DW_UNSND (attr);
3130
3131 /* We may have already read in this line header (TU line header sharing).
3132 If we have we're done. */
3133 find_entry.hash.dwo_unit = cu->dwo_unit;
3134 find_entry.hash.line_sect_off = line_offset;
3135 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3136 &find_entry, INSERT);
3137 if (*slot != NULL)
3138 {
3139 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3140 return;
3141 }
3142
3143 lh = dwarf_decode_line_header (line_offset, cu);
3144 }
3145 if (lh == NULL)
3146 {
3147 lh_cu->v.quick->no_file_data = 1;
3148 return;
3149 }
3150
3151 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3152 qfn->hash.dwo_unit = cu->dwo_unit;
3153 qfn->hash.line_sect_off = line_offset;
3154 gdb_assert (slot != NULL);
3155 *slot = qfn;
3156
3157 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3158
3159 int offset = 0;
3160 if (strcmp (fnd.name, "<unknown>") != 0)
3161 ++offset;
3162
3163 qfn->num_file_names = offset + lh->file_names_size ();
3164 qfn->file_names =
3165 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3166 qfn->num_file_names);
3167 if (offset != 0)
3168 qfn->file_names[0] = xstrdup (fnd.name);
3169 for (int i = 0; i < lh->file_names_size (); ++i)
3170 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3171 fnd.comp_dir).release ();
3172 qfn->real_names = NULL;
3173
3174 lh_cu->v.quick->file_names = qfn;
3175 }
3176
3177 /* A helper for the "quick" functions which attempts to read the line
3178 table for THIS_CU. */
3179
3180 static struct quick_file_names *
3181 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3182 {
3183 /* This should never be called for TUs. */
3184 gdb_assert (! this_cu->is_debug_types);
3185 /* Nor type unit groups. */
3186 gdb_assert (! this_cu->type_unit_group_p ());
3187
3188 if (this_cu->v.quick->file_names != NULL)
3189 return this_cu->v.quick->file_names;
3190 /* If we know there is no line data, no point in looking again. */
3191 if (this_cu->v.quick->no_file_data)
3192 return NULL;
3193
3194 cutu_reader reader (this_cu);
3195 if (!reader.dummy_p)
3196 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3197
3198 if (this_cu->v.quick->no_file_data)
3199 return NULL;
3200 return this_cu->v.quick->file_names;
3201 }
3202
3203 /* A helper for the "quick" functions which computes and caches the
3204 real path for a given file name from the line table. */
3205
3206 static const char *
3207 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3208 struct quick_file_names *qfn, int index)
3209 {
3210 if (qfn->real_names == NULL)
3211 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3212 qfn->num_file_names, const char *);
3213
3214 if (qfn->real_names[index] == NULL)
3215 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3216
3217 return qfn->real_names[index];
3218 }
3219
3220 static struct symtab *
3221 dw2_find_last_source_symtab (struct objfile *objfile)
3222 {
3223 struct dwarf2_per_objfile *dwarf2_per_objfile
3224 = get_dwarf2_per_objfile (objfile);
3225 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3226 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3227
3228 if (cust == NULL)
3229 return NULL;
3230
3231 return compunit_primary_filetab (cust);
3232 }
3233
3234 /* Traversal function for dw2_forget_cached_source_info. */
3235
3236 static int
3237 dw2_free_cached_file_names (void **slot, void *info)
3238 {
3239 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3240
3241 if (file_data->real_names)
3242 {
3243 int i;
3244
3245 for (i = 0; i < file_data->num_file_names; ++i)
3246 {
3247 xfree ((void*) file_data->real_names[i]);
3248 file_data->real_names[i] = NULL;
3249 }
3250 }
3251
3252 return 1;
3253 }
3254
3255 static void
3256 dw2_forget_cached_source_info (struct objfile *objfile)
3257 {
3258 struct dwarf2_per_objfile *dwarf2_per_objfile
3259 = get_dwarf2_per_objfile (objfile);
3260
3261 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3262 dw2_free_cached_file_names, NULL);
3263 }
3264
3265 /* Helper function for dw2_map_symtabs_matching_filename that expands
3266 the symtabs and calls the iterator. */
3267
3268 static int
3269 dw2_map_expand_apply (struct objfile *objfile,
3270 struct dwarf2_per_cu_data *per_cu,
3271 const char *name, const char *real_path,
3272 gdb::function_view<bool (symtab *)> callback)
3273 {
3274 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3275
3276 /* Don't visit already-expanded CUs. */
3277 if (per_cu->v.quick->compunit_symtab)
3278 return 0;
3279
3280 /* This may expand more than one symtab, and we want to iterate over
3281 all of them. */
3282 dw2_instantiate_symtab (per_cu, false);
3283
3284 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3285 last_made, callback);
3286 }
3287
3288 /* Implementation of the map_symtabs_matching_filename method. */
3289
3290 static bool
3291 dw2_map_symtabs_matching_filename
3292 (struct objfile *objfile, const char *name, const char *real_path,
3293 gdb::function_view<bool (symtab *)> callback)
3294 {
3295 const char *name_basename = lbasename (name);
3296 struct dwarf2_per_objfile *dwarf2_per_objfile
3297 = get_dwarf2_per_objfile (objfile);
3298
3299 /* The rule is CUs specify all the files, including those used by
3300 any TU, so there's no need to scan TUs here. */
3301
3302 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3303 {
3304 /* We only need to look at symtabs not already expanded. */
3305 if (per_cu->v.quick->compunit_symtab)
3306 continue;
3307
3308 quick_file_names *file_data = dw2_get_file_names (per_cu);
3309 if (file_data == NULL)
3310 continue;
3311
3312 for (int j = 0; j < file_data->num_file_names; ++j)
3313 {
3314 const char *this_name = file_data->file_names[j];
3315 const char *this_real_name;
3316
3317 if (compare_filenames_for_search (this_name, name))
3318 {
3319 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3320 callback))
3321 return true;
3322 continue;
3323 }
3324
3325 /* Before we invoke realpath, which can get expensive when many
3326 files are involved, do a quick comparison of the basenames. */
3327 if (! basenames_may_differ
3328 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3329 continue;
3330
3331 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3332 file_data, j);
3333 if (compare_filenames_for_search (this_real_name, name))
3334 {
3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3336 callback))
3337 return true;
3338 continue;
3339 }
3340
3341 if (real_path != NULL)
3342 {
3343 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3344 gdb_assert (IS_ABSOLUTE_PATH (name));
3345 if (this_real_name != NULL
3346 && FILENAME_CMP (real_path, this_real_name) == 0)
3347 {
3348 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3349 callback))
3350 return true;
3351 continue;
3352 }
3353 }
3354 }
3355 }
3356
3357 return false;
3358 }
3359
3360 /* Struct used to manage iterating over all CUs looking for a symbol. */
3361
3362 struct dw2_symtab_iterator
3363 {
3364 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3365 struct dwarf2_per_objfile *dwarf2_per_objfile;
3366 /* If set, only look for symbols that match that block. Valid values are
3367 GLOBAL_BLOCK and STATIC_BLOCK. */
3368 gdb::optional<block_enum> block_index;
3369 /* The kind of symbol we're looking for. */
3370 domain_enum domain;
3371 /* The list of CUs from the index entry of the symbol,
3372 or NULL if not found. */
3373 offset_type *vec;
3374 /* The next element in VEC to look at. */
3375 int next;
3376 /* The number of elements in VEC, or zero if there is no match. */
3377 int length;
3378 /* Have we seen a global version of the symbol?
3379 If so we can ignore all further global instances.
3380 This is to work around gold/15646, inefficient gold-generated
3381 indices. */
3382 int global_seen;
3383 };
3384
3385 /* Initialize the index symtab iterator ITER. */
3386
3387 static void
3388 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3389 struct dwarf2_per_objfile *dwarf2_per_objfile,
3390 gdb::optional<block_enum> block_index,
3391 domain_enum domain,
3392 const char *name)
3393 {
3394 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3395 iter->block_index = block_index;
3396 iter->domain = domain;
3397 iter->next = 0;
3398 iter->global_seen = 0;
3399
3400 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3401
3402 /* index is NULL if OBJF_READNOW. */
3403 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3404 iter->length = MAYBE_SWAP (*iter->vec);
3405 else
3406 {
3407 iter->vec = NULL;
3408 iter->length = 0;
3409 }
3410 }
3411
3412 /* Return the next matching CU or NULL if there are no more. */
3413
3414 static struct dwarf2_per_cu_data *
3415 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3416 {
3417 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3418
3419 for ( ; iter->next < iter->length; ++iter->next)
3420 {
3421 offset_type cu_index_and_attrs =
3422 MAYBE_SWAP (iter->vec[iter->next + 1]);
3423 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3424 gdb_index_symbol_kind symbol_kind =
3425 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3426 /* Only check the symbol attributes if they're present.
3427 Indices prior to version 7 don't record them,
3428 and indices >= 7 may elide them for certain symbols
3429 (gold does this). */
3430 int attrs_valid =
3431 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3432 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3433
3434 /* Don't crash on bad data. */
3435 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3436 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3437 {
3438 complaint (_(".gdb_index entry has bad CU index"
3439 " [in module %s]"),
3440 objfile_name (dwarf2_per_objfile->objfile));
3441 continue;
3442 }
3443
3444 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3445
3446 /* Skip if already read in. */
3447 if (per_cu->v.quick->compunit_symtab)
3448 continue;
3449
3450 /* Check static vs global. */
3451 if (attrs_valid)
3452 {
3453 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3454
3455 if (iter->block_index.has_value ())
3456 {
3457 bool want_static = *iter->block_index == STATIC_BLOCK;
3458
3459 if (is_static != want_static)
3460 continue;
3461 }
3462
3463 /* Work around gold/15646. */
3464 if (!is_static && iter->global_seen)
3465 continue;
3466 if (!is_static)
3467 iter->global_seen = 1;
3468 }
3469
3470 /* Only check the symbol's kind if it has one. */
3471 if (attrs_valid)
3472 {
3473 switch (iter->domain)
3474 {
3475 case VAR_DOMAIN:
3476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3477 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3478 /* Some types are also in VAR_DOMAIN. */
3479 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3480 continue;
3481 break;
3482 case STRUCT_DOMAIN:
3483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3484 continue;
3485 break;
3486 case LABEL_DOMAIN:
3487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3488 continue;
3489 break;
3490 case MODULE_DOMAIN:
3491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3492 continue;
3493 break;
3494 default:
3495 break;
3496 }
3497 }
3498
3499 ++iter->next;
3500 return per_cu;
3501 }
3502
3503 return NULL;
3504 }
3505
3506 static struct compunit_symtab *
3507 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3508 const char *name, domain_enum domain)
3509 {
3510 struct compunit_symtab *stab_best = NULL;
3511 struct dwarf2_per_objfile *dwarf2_per_objfile
3512 = get_dwarf2_per_objfile (objfile);
3513
3514 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3515
3516 struct dw2_symtab_iterator iter;
3517 struct dwarf2_per_cu_data *per_cu;
3518
3519 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3520
3521 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3522 {
3523 struct symbol *sym, *with_opaque = NULL;
3524 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3525 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3526 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3527
3528 sym = block_find_symbol (block, name, domain,
3529 block_find_non_opaque_type_preferred,
3530 &with_opaque);
3531
3532 /* Some caution must be observed with overloaded functions
3533 and methods, since the index will not contain any overload
3534 information (but NAME might contain it). */
3535
3536 if (sym != NULL
3537 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3538 return stab;
3539 if (with_opaque != NULL
3540 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3541 stab_best = stab;
3542
3543 /* Keep looking through other CUs. */
3544 }
3545
3546 return stab_best;
3547 }
3548
3549 static void
3550 dw2_print_stats (struct objfile *objfile)
3551 {
3552 struct dwarf2_per_objfile *dwarf2_per_objfile
3553 = get_dwarf2_per_objfile (objfile);
3554 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3555 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3556 int count = 0;
3557
3558 for (int i = 0; i < total; ++i)
3559 {
3560 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3561
3562 if (!per_cu->v.quick->compunit_symtab)
3563 ++count;
3564 }
3565 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3566 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3567 }
3568
3569 /* This dumps minimal information about the index.
3570 It is called via "mt print objfiles".
3571 One use is to verify .gdb_index has been loaded by the
3572 gdb.dwarf2/gdb-index.exp testcase. */
3573
3574 static void
3575 dw2_dump (struct objfile *objfile)
3576 {
3577 struct dwarf2_per_objfile *dwarf2_per_objfile
3578 = get_dwarf2_per_objfile (objfile);
3579
3580 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3581 printf_filtered (".gdb_index:");
3582 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3583 {
3584 printf_filtered (" version %d\n",
3585 dwarf2_per_objfile->per_bfd->index_table->version);
3586 }
3587 else
3588 printf_filtered (" faked for \"readnow\"\n");
3589 printf_filtered ("\n");
3590 }
3591
3592 static void
3593 dw2_expand_symtabs_for_function (struct objfile *objfile,
3594 const char *func_name)
3595 {
3596 struct dwarf2_per_objfile *dwarf2_per_objfile
3597 = get_dwarf2_per_objfile (objfile);
3598
3599 struct dw2_symtab_iterator iter;
3600 struct dwarf2_per_cu_data *per_cu;
3601
3602 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3603
3604 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3605 dw2_instantiate_symtab (per_cu, false);
3606
3607 }
3608
3609 static void
3610 dw2_expand_all_symtabs (struct objfile *objfile)
3611 {
3612 struct dwarf2_per_objfile *dwarf2_per_objfile
3613 = get_dwarf2_per_objfile (objfile);
3614 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3615 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3616
3617 for (int i = 0; i < total_units; ++i)
3618 {
3619 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3620
3621 /* We don't want to directly expand a partial CU, because if we
3622 read it with the wrong language, then assertion failures can
3623 be triggered later on. See PR symtab/23010. So, tell
3624 dw2_instantiate_symtab to skip partial CUs -- any important
3625 partial CU will be read via DW_TAG_imported_unit anyway. */
3626 dw2_instantiate_symtab (per_cu, true);
3627 }
3628 }
3629
3630 static void
3631 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3632 const char *fullname)
3633 {
3634 struct dwarf2_per_objfile *dwarf2_per_objfile
3635 = get_dwarf2_per_objfile (objfile);
3636
3637 /* We don't need to consider type units here.
3638 This is only called for examining code, e.g. expand_line_sal.
3639 There can be an order of magnitude (or more) more type units
3640 than comp units, and we avoid them if we can. */
3641
3642 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3643 {
3644 /* We only need to look at symtabs not already expanded. */
3645 if (per_cu->v.quick->compunit_symtab)
3646 continue;
3647
3648 quick_file_names *file_data = dw2_get_file_names (per_cu);
3649 if (file_data == NULL)
3650 continue;
3651
3652 for (int j = 0; j < file_data->num_file_names; ++j)
3653 {
3654 const char *this_fullname = file_data->file_names[j];
3655
3656 if (filename_cmp (this_fullname, fullname) == 0)
3657 {
3658 dw2_instantiate_symtab (per_cu, false);
3659 break;
3660 }
3661 }
3662 }
3663 }
3664
3665 static void
3666 dw2_expand_symtabs_matching_symbol
3667 (mapped_index_base &index,
3668 const lookup_name_info &lookup_name_in,
3669 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3670 enum search_domain kind,
3671 gdb::function_view<bool (offset_type)> match_callback);
3672
3673 static void
3674 dw2_expand_symtabs_matching_one
3675 (struct dwarf2_per_cu_data *per_cu,
3676 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3677 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3678
3679 static void
3680 dw2_map_matching_symbols
3681 (struct objfile *objfile,
3682 const lookup_name_info &name, domain_enum domain,
3683 int global,
3684 gdb::function_view<symbol_found_callback_ftype> callback,
3685 symbol_compare_ftype *ordered_compare)
3686 {
3687 /* Used for Ada. */
3688 struct dwarf2_per_objfile *dwarf2_per_objfile
3689 = get_dwarf2_per_objfile (objfile);
3690
3691 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3692
3693 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3694 {
3695 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3696 here though if the current language is Ada for a non-Ada objfile
3697 using GNU index. */
3698 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3699
3700 const char *match_name = name.ada ().lookup_name ().c_str ();
3701 auto matcher = [&] (const char *symname)
3702 {
3703 if (ordered_compare == nullptr)
3704 return true;
3705 return ordered_compare (symname, match_name) == 0;
3706 };
3707
3708 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3709 [&] (offset_type namei)
3710 {
3711 struct dw2_symtab_iterator iter;
3712 struct dwarf2_per_cu_data *per_cu;
3713
3714 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3715 match_name);
3716 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3717 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
3718 return true;
3719 });
3720 }
3721 else
3722 {
3723 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3724 proceed assuming all symtabs have been read in. */
3725 }
3726
3727 for (compunit_symtab *cust : objfile->compunits ())
3728 {
3729 const struct block *block;
3730
3731 if (cust == NULL)
3732 continue;
3733 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3734 if (!iterate_over_symbols_terminated (block, name,
3735 domain, callback))
3736 return;
3737 }
3738 }
3739
3740 /* Starting from a search name, return the string that finds the upper
3741 bound of all strings that start with SEARCH_NAME in a sorted name
3742 list. Returns the empty string to indicate that the upper bound is
3743 the end of the list. */
3744
3745 static std::string
3746 make_sort_after_prefix_name (const char *search_name)
3747 {
3748 /* When looking to complete "func", we find the upper bound of all
3749 symbols that start with "func" by looking for where we'd insert
3750 the closest string that would follow "func" in lexicographical
3751 order. Usually, that's "func"-with-last-character-incremented,
3752 i.e. "fund". Mind non-ASCII characters, though. Usually those
3753 will be UTF-8 multi-byte sequences, but we can't be certain.
3754 Especially mind the 0xff character, which is a valid character in
3755 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3756 rule out compilers allowing it in identifiers. Note that
3757 conveniently, strcmp/strcasecmp are specified to compare
3758 characters interpreted as unsigned char. So what we do is treat
3759 the whole string as a base 256 number composed of a sequence of
3760 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3761 to 0, and carries 1 to the following more-significant position.
3762 If the very first character in SEARCH_NAME ends up incremented
3763 and carries/overflows, then the upper bound is the end of the
3764 list. The string after the empty string is also the empty
3765 string.
3766
3767 Some examples of this operation:
3768
3769 SEARCH_NAME => "+1" RESULT
3770
3771 "abc" => "abd"
3772 "ab\xff" => "ac"
3773 "\xff" "a" "\xff" => "\xff" "b"
3774 "\xff" => ""
3775 "\xff\xff" => ""
3776 "" => ""
3777
3778 Then, with these symbols for example:
3779
3780 func
3781 func1
3782 fund
3783
3784 completing "func" looks for symbols between "func" and
3785 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3786 which finds "func" and "func1", but not "fund".
3787
3788 And with:
3789
3790 funcÿ (Latin1 'ÿ' [0xff])
3791 funcÿ1
3792 fund
3793
3794 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3795 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3796
3797 And with:
3798
3799 ÿÿ (Latin1 'ÿ' [0xff])
3800 ÿÿ1
3801
3802 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3803 the end of the list.
3804 */
3805 std::string after = search_name;
3806 while (!after.empty () && (unsigned char) after.back () == 0xff)
3807 after.pop_back ();
3808 if (!after.empty ())
3809 after.back () = (unsigned char) after.back () + 1;
3810 return after;
3811 }
3812
3813 /* See declaration. */
3814
3815 std::pair<std::vector<name_component>::const_iterator,
3816 std::vector<name_component>::const_iterator>
3817 mapped_index_base::find_name_components_bounds
3818 (const lookup_name_info &lookup_name_without_params, language lang) const
3819 {
3820 auto *name_cmp
3821 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3822
3823 const char *lang_name
3824 = lookup_name_without_params.language_lookup_name (lang);
3825
3826 /* Comparison function object for lower_bound that matches against a
3827 given symbol name. */
3828 auto lookup_compare_lower = [&] (const name_component &elem,
3829 const char *name)
3830 {
3831 const char *elem_qualified = this->symbol_name_at (elem.idx);
3832 const char *elem_name = elem_qualified + elem.name_offset;
3833 return name_cmp (elem_name, name) < 0;
3834 };
3835
3836 /* Comparison function object for upper_bound that matches against a
3837 given symbol name. */
3838 auto lookup_compare_upper = [&] (const char *name,
3839 const name_component &elem)
3840 {
3841 const char *elem_qualified = this->symbol_name_at (elem.idx);
3842 const char *elem_name = elem_qualified + elem.name_offset;
3843 return name_cmp (name, elem_name) < 0;
3844 };
3845
3846 auto begin = this->name_components.begin ();
3847 auto end = this->name_components.end ();
3848
3849 /* Find the lower bound. */
3850 auto lower = [&] ()
3851 {
3852 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3853 return begin;
3854 else
3855 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3856 } ();
3857
3858 /* Find the upper bound. */
3859 auto upper = [&] ()
3860 {
3861 if (lookup_name_without_params.completion_mode ())
3862 {
3863 /* In completion mode, we want UPPER to point past all
3864 symbols names that have the same prefix. I.e., with
3865 these symbols, and completing "func":
3866
3867 function << lower bound
3868 function1
3869 other_function << upper bound
3870
3871 We find the upper bound by looking for the insertion
3872 point of "func"-with-last-character-incremented,
3873 i.e. "fund". */
3874 std::string after = make_sort_after_prefix_name (lang_name);
3875 if (after.empty ())
3876 return end;
3877 return std::lower_bound (lower, end, after.c_str (),
3878 lookup_compare_lower);
3879 }
3880 else
3881 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3882 } ();
3883
3884 return {lower, upper};
3885 }
3886
3887 /* See declaration. */
3888
3889 void
3890 mapped_index_base::build_name_components ()
3891 {
3892 if (!this->name_components.empty ())
3893 return;
3894
3895 this->name_components_casing = case_sensitivity;
3896 auto *name_cmp
3897 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3898
3899 /* The code below only knows how to break apart components of C++
3900 symbol names (and other languages that use '::' as
3901 namespace/module separator) and Ada symbol names. */
3902 auto count = this->symbol_name_count ();
3903 for (offset_type idx = 0; idx < count; idx++)
3904 {
3905 if (this->symbol_name_slot_invalid (idx))
3906 continue;
3907
3908 const char *name = this->symbol_name_at (idx);
3909
3910 /* Add each name component to the name component table. */
3911 unsigned int previous_len = 0;
3912
3913 if (strstr (name, "::") != nullptr)
3914 {
3915 for (unsigned int current_len = cp_find_first_component (name);
3916 name[current_len] != '\0';
3917 current_len += cp_find_first_component (name + current_len))
3918 {
3919 gdb_assert (name[current_len] == ':');
3920 this->name_components.push_back ({previous_len, idx});
3921 /* Skip the '::'. */
3922 current_len += 2;
3923 previous_len = current_len;
3924 }
3925 }
3926 else
3927 {
3928 /* Handle the Ada encoded (aka mangled) form here. */
3929 for (const char *iter = strstr (name, "__");
3930 iter != nullptr;
3931 iter = strstr (iter, "__"))
3932 {
3933 this->name_components.push_back ({previous_len, idx});
3934 iter += 2;
3935 previous_len = iter - name;
3936 }
3937 }
3938
3939 this->name_components.push_back ({previous_len, idx});
3940 }
3941
3942 /* Sort name_components elements by name. */
3943 auto name_comp_compare = [&] (const name_component &left,
3944 const name_component &right)
3945 {
3946 const char *left_qualified = this->symbol_name_at (left.idx);
3947 const char *right_qualified = this->symbol_name_at (right.idx);
3948
3949 const char *left_name = left_qualified + left.name_offset;
3950 const char *right_name = right_qualified + right.name_offset;
3951
3952 return name_cmp (left_name, right_name) < 0;
3953 };
3954
3955 std::sort (this->name_components.begin (),
3956 this->name_components.end (),
3957 name_comp_compare);
3958 }
3959
3960 /* Helper for dw2_expand_symtabs_matching that works with a
3961 mapped_index_base instead of the containing objfile. This is split
3962 to a separate function in order to be able to unit test the
3963 name_components matching using a mock mapped_index_base. For each
3964 symbol name that matches, calls MATCH_CALLBACK, passing it the
3965 symbol's index in the mapped_index_base symbol table. */
3966
3967 static void
3968 dw2_expand_symtabs_matching_symbol
3969 (mapped_index_base &index,
3970 const lookup_name_info &lookup_name_in,
3971 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3972 enum search_domain kind,
3973 gdb::function_view<bool (offset_type)> match_callback)
3974 {
3975 lookup_name_info lookup_name_without_params
3976 = lookup_name_in.make_ignore_params ();
3977
3978 /* Build the symbol name component sorted vector, if we haven't
3979 yet. */
3980 index.build_name_components ();
3981
3982 /* The same symbol may appear more than once in the range though.
3983 E.g., if we're looking for symbols that complete "w", and we have
3984 a symbol named "w1::w2", we'll find the two name components for
3985 that same symbol in the range. To be sure we only call the
3986 callback once per symbol, we first collect the symbol name
3987 indexes that matched in a temporary vector and ignore
3988 duplicates. */
3989 std::vector<offset_type> matches;
3990
3991 struct name_and_matcher
3992 {
3993 symbol_name_matcher_ftype *matcher;
3994 const char *name;
3995
3996 bool operator== (const name_and_matcher &other) const
3997 {
3998 return matcher == other.matcher && strcmp (name, other.name) == 0;
3999 }
4000 };
4001
4002 /* A vector holding all the different symbol name matchers, for all
4003 languages. */
4004 std::vector<name_and_matcher> matchers;
4005
4006 for (int i = 0; i < nr_languages; i++)
4007 {
4008 enum language lang_e = (enum language) i;
4009
4010 const language_defn *lang = language_def (lang_e);
4011 symbol_name_matcher_ftype *name_matcher
4012 = get_symbol_name_matcher (lang, lookup_name_without_params);
4013
4014 name_and_matcher key {
4015 name_matcher,
4016 lookup_name_without_params.language_lookup_name (lang_e)
4017 };
4018
4019 /* Don't insert the same comparison routine more than once.
4020 Note that we do this linear walk. This is not a problem in
4021 practice because the number of supported languages is
4022 low. */
4023 if (std::find (matchers.begin (), matchers.end (), key)
4024 != matchers.end ())
4025 continue;
4026 matchers.push_back (std::move (key));
4027
4028 auto bounds
4029 = index.find_name_components_bounds (lookup_name_without_params,
4030 lang_e);
4031
4032 /* Now for each symbol name in range, check to see if we have a name
4033 match, and if so, call the MATCH_CALLBACK callback. */
4034
4035 for (; bounds.first != bounds.second; ++bounds.first)
4036 {
4037 const char *qualified = index.symbol_name_at (bounds.first->idx);
4038
4039 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4040 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4041 continue;
4042
4043 matches.push_back (bounds.first->idx);
4044 }
4045 }
4046
4047 std::sort (matches.begin (), matches.end ());
4048
4049 /* Finally call the callback, once per match. */
4050 ULONGEST prev = -1;
4051 for (offset_type idx : matches)
4052 {
4053 if (prev != idx)
4054 {
4055 if (!match_callback (idx))
4056 break;
4057 prev = idx;
4058 }
4059 }
4060
4061 /* Above we use a type wider than idx's for 'prev', since 0 and
4062 (offset_type)-1 are both possible values. */
4063 static_assert (sizeof (prev) > sizeof (offset_type), "");
4064 }
4065
4066 #if GDB_SELF_TEST
4067
4068 namespace selftests { namespace dw2_expand_symtabs_matching {
4069
4070 /* A mock .gdb_index/.debug_names-like name index table, enough to
4071 exercise dw2_expand_symtabs_matching_symbol, which works with the
4072 mapped_index_base interface. Builds an index from the symbol list
4073 passed as parameter to the constructor. */
4074 class mock_mapped_index : public mapped_index_base
4075 {
4076 public:
4077 mock_mapped_index (gdb::array_view<const char *> symbols)
4078 : m_symbol_table (symbols)
4079 {}
4080
4081 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4082
4083 /* Return the number of names in the symbol table. */
4084 size_t symbol_name_count () const override
4085 {
4086 return m_symbol_table.size ();
4087 }
4088
4089 /* Get the name of the symbol at IDX in the symbol table. */
4090 const char *symbol_name_at (offset_type idx) const override
4091 {
4092 return m_symbol_table[idx];
4093 }
4094
4095 private:
4096 gdb::array_view<const char *> m_symbol_table;
4097 };
4098
4099 /* Convenience function that converts a NULL pointer to a "<null>"
4100 string, to pass to print routines. */
4101
4102 static const char *
4103 string_or_null (const char *str)
4104 {
4105 return str != NULL ? str : "<null>";
4106 }
4107
4108 /* Check if a lookup_name_info built from
4109 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4110 index. EXPECTED_LIST is the list of expected matches, in expected
4111 matching order. If no match expected, then an empty list is
4112 specified. Returns true on success. On failure prints a warning
4113 indicating the file:line that failed, and returns false. */
4114
4115 static bool
4116 check_match (const char *file, int line,
4117 mock_mapped_index &mock_index,
4118 const char *name, symbol_name_match_type match_type,
4119 bool completion_mode,
4120 std::initializer_list<const char *> expected_list)
4121 {
4122 lookup_name_info lookup_name (name, match_type, completion_mode);
4123
4124 bool matched = true;
4125
4126 auto mismatch = [&] (const char *expected_str,
4127 const char *got)
4128 {
4129 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4130 "expected=\"%s\", got=\"%s\"\n"),
4131 file, line,
4132 (match_type == symbol_name_match_type::FULL
4133 ? "FULL" : "WILD"),
4134 name, string_or_null (expected_str), string_or_null (got));
4135 matched = false;
4136 };
4137
4138 auto expected_it = expected_list.begin ();
4139 auto expected_end = expected_list.end ();
4140
4141 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4142 NULL, ALL_DOMAIN,
4143 [&] (offset_type idx)
4144 {
4145 const char *matched_name = mock_index.symbol_name_at (idx);
4146 const char *expected_str
4147 = expected_it == expected_end ? NULL : *expected_it++;
4148
4149 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4150 mismatch (expected_str, matched_name);
4151 return true;
4152 });
4153
4154 const char *expected_str
4155 = expected_it == expected_end ? NULL : *expected_it++;
4156 if (expected_str != NULL)
4157 mismatch (expected_str, NULL);
4158
4159 return matched;
4160 }
4161
4162 /* The symbols added to the mock mapped_index for testing (in
4163 canonical form). */
4164 static const char *test_symbols[] = {
4165 "function",
4166 "std::bar",
4167 "std::zfunction",
4168 "std::zfunction2",
4169 "w1::w2",
4170 "ns::foo<char*>",
4171 "ns::foo<int>",
4172 "ns::foo<long>",
4173 "ns2::tmpl<int>::foo2",
4174 "(anonymous namespace)::A::B::C",
4175
4176 /* These are used to check that the increment-last-char in the
4177 matching algorithm for completion doesn't match "t1_fund" when
4178 completing "t1_func". */
4179 "t1_func",
4180 "t1_func1",
4181 "t1_fund",
4182 "t1_fund1",
4183
4184 /* A UTF-8 name with multi-byte sequences to make sure that
4185 cp-name-parser understands this as a single identifier ("função"
4186 is "function" in PT). */
4187 u8"u8função",
4188
4189 /* \377 (0xff) is Latin1 'ÿ'. */
4190 "yfunc\377",
4191
4192 /* \377 (0xff) is Latin1 'ÿ'. */
4193 "\377",
4194 "\377\377123",
4195
4196 /* A name with all sorts of complications. Starts with "z" to make
4197 it easier for the completion tests below. */
4198 #define Z_SYM_NAME \
4199 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4200 "::tuple<(anonymous namespace)::ui*, " \
4201 "std::default_delete<(anonymous namespace)::ui>, void>"
4202
4203 Z_SYM_NAME
4204 };
4205
4206 /* Returns true if the mapped_index_base::find_name_component_bounds
4207 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4208 in completion mode. */
4209
4210 static bool
4211 check_find_bounds_finds (mapped_index_base &index,
4212 const char *search_name,
4213 gdb::array_view<const char *> expected_syms)
4214 {
4215 lookup_name_info lookup_name (search_name,
4216 symbol_name_match_type::FULL, true);
4217
4218 auto bounds = index.find_name_components_bounds (lookup_name,
4219 language_cplus);
4220
4221 size_t distance = std::distance (bounds.first, bounds.second);
4222 if (distance != expected_syms.size ())
4223 return false;
4224
4225 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4226 {
4227 auto nc_elem = bounds.first + exp_elem;
4228 const char *qualified = index.symbol_name_at (nc_elem->idx);
4229 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4230 return false;
4231 }
4232
4233 return true;
4234 }
4235
4236 /* Test the lower-level mapped_index::find_name_component_bounds
4237 method. */
4238
4239 static void
4240 test_mapped_index_find_name_component_bounds ()
4241 {
4242 mock_mapped_index mock_index (test_symbols);
4243
4244 mock_index.build_name_components ();
4245
4246 /* Test the lower-level mapped_index::find_name_component_bounds
4247 method in completion mode. */
4248 {
4249 static const char *expected_syms[] = {
4250 "t1_func",
4251 "t1_func1",
4252 };
4253
4254 SELF_CHECK (check_find_bounds_finds (mock_index,
4255 "t1_func", expected_syms));
4256 }
4257
4258 /* Check that the increment-last-char in the name matching algorithm
4259 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4260 {
4261 static const char *expected_syms1[] = {
4262 "\377",
4263 "\377\377123",
4264 };
4265 SELF_CHECK (check_find_bounds_finds (mock_index,
4266 "\377", expected_syms1));
4267
4268 static const char *expected_syms2[] = {
4269 "\377\377123",
4270 };
4271 SELF_CHECK (check_find_bounds_finds (mock_index,
4272 "\377\377", expected_syms2));
4273 }
4274 }
4275
4276 /* Test dw2_expand_symtabs_matching_symbol. */
4277
4278 static void
4279 test_dw2_expand_symtabs_matching_symbol ()
4280 {
4281 mock_mapped_index mock_index (test_symbols);
4282
4283 /* We let all tests run until the end even if some fails, for debug
4284 convenience. */
4285 bool any_mismatch = false;
4286
4287 /* Create the expected symbols list (an initializer_list). Needed
4288 because lists have commas, and we need to pass them to CHECK,
4289 which is a macro. */
4290 #define EXPECT(...) { __VA_ARGS__ }
4291
4292 /* Wrapper for check_match that passes down the current
4293 __FILE__/__LINE__. */
4294 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4295 any_mismatch |= !check_match (__FILE__, __LINE__, \
4296 mock_index, \
4297 NAME, MATCH_TYPE, COMPLETION_MODE, \
4298 EXPECTED_LIST)
4299
4300 /* Identity checks. */
4301 for (const char *sym : test_symbols)
4302 {
4303 /* Should be able to match all existing symbols. */
4304 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4305 EXPECT (sym));
4306
4307 /* Should be able to match all existing symbols with
4308 parameters. */
4309 std::string with_params = std::string (sym) + "(int)";
4310 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4311 EXPECT (sym));
4312
4313 /* Should be able to match all existing symbols with
4314 parameters and qualifiers. */
4315 with_params = std::string (sym) + " ( int ) const";
4316 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4317 EXPECT (sym));
4318
4319 /* This should really find sym, but cp-name-parser.y doesn't
4320 know about lvalue/rvalue qualifiers yet. */
4321 with_params = std::string (sym) + " ( int ) &&";
4322 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4323 {});
4324 }
4325
4326 /* Check that the name matching algorithm for completion doesn't get
4327 confused with Latin1 'ÿ' / 0xff. */
4328 {
4329 static const char str[] = "\377";
4330 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4331 EXPECT ("\377", "\377\377123"));
4332 }
4333
4334 /* Check that the increment-last-char in the matching algorithm for
4335 completion doesn't match "t1_fund" when completing "t1_func". */
4336 {
4337 static const char str[] = "t1_func";
4338 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4339 EXPECT ("t1_func", "t1_func1"));
4340 }
4341
4342 /* Check that completion mode works at each prefix of the expected
4343 symbol name. */
4344 {
4345 static const char str[] = "function(int)";
4346 size_t len = strlen (str);
4347 std::string lookup;
4348
4349 for (size_t i = 1; i < len; i++)
4350 {
4351 lookup.assign (str, i);
4352 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4353 EXPECT ("function"));
4354 }
4355 }
4356
4357 /* While "w" is a prefix of both components, the match function
4358 should still only be called once. */
4359 {
4360 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4361 EXPECT ("w1::w2"));
4362 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4363 EXPECT ("w1::w2"));
4364 }
4365
4366 /* Same, with a "complicated" symbol. */
4367 {
4368 static const char str[] = Z_SYM_NAME;
4369 size_t len = strlen (str);
4370 std::string lookup;
4371
4372 for (size_t i = 1; i < len; i++)
4373 {
4374 lookup.assign (str, i);
4375 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4376 EXPECT (Z_SYM_NAME));
4377 }
4378 }
4379
4380 /* In FULL mode, an incomplete symbol doesn't match. */
4381 {
4382 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4383 {});
4384 }
4385
4386 /* A complete symbol with parameters matches any overload, since the
4387 index has no overload info. */
4388 {
4389 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4390 EXPECT ("std::zfunction", "std::zfunction2"));
4391 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4392 EXPECT ("std::zfunction", "std::zfunction2"));
4393 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4394 EXPECT ("std::zfunction", "std::zfunction2"));
4395 }
4396
4397 /* Check that whitespace is ignored appropriately. A symbol with a
4398 template argument list. */
4399 {
4400 static const char expected[] = "ns::foo<int>";
4401 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4402 EXPECT (expected));
4403 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4404 EXPECT (expected));
4405 }
4406
4407 /* Check that whitespace is ignored appropriately. A symbol with a
4408 template argument list that includes a pointer. */
4409 {
4410 static const char expected[] = "ns::foo<char*>";
4411 /* Try both completion and non-completion modes. */
4412 static const bool completion_mode[2] = {false, true};
4413 for (size_t i = 0; i < 2; i++)
4414 {
4415 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4416 completion_mode[i], EXPECT (expected));
4417 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4418 completion_mode[i], EXPECT (expected));
4419
4420 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4421 completion_mode[i], EXPECT (expected));
4422 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4423 completion_mode[i], EXPECT (expected));
4424 }
4425 }
4426
4427 {
4428 /* Check method qualifiers are ignored. */
4429 static const char expected[] = "ns::foo<char*>";
4430 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4431 symbol_name_match_type::FULL, true, EXPECT (expected));
4432 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4433 symbol_name_match_type::FULL, true, EXPECT (expected));
4434 CHECK_MATCH ("foo < char * > ( int ) const",
4435 symbol_name_match_type::WILD, true, EXPECT (expected));
4436 CHECK_MATCH ("foo < char * > ( int ) &&",
4437 symbol_name_match_type::WILD, true, EXPECT (expected));
4438 }
4439
4440 /* Test lookup names that don't match anything. */
4441 {
4442 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4443 {});
4444
4445 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4446 {});
4447 }
4448
4449 /* Some wild matching tests, exercising "(anonymous namespace)",
4450 which should not be confused with a parameter list. */
4451 {
4452 static const char *syms[] = {
4453 "A::B::C",
4454 "B::C",
4455 "C",
4456 "A :: B :: C ( int )",
4457 "B :: C ( int )",
4458 "C ( int )",
4459 };
4460
4461 for (const char *s : syms)
4462 {
4463 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4464 EXPECT ("(anonymous namespace)::A::B::C"));
4465 }
4466 }
4467
4468 {
4469 static const char expected[] = "ns2::tmpl<int>::foo2";
4470 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4471 EXPECT (expected));
4472 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4473 EXPECT (expected));
4474 }
4475
4476 SELF_CHECK (!any_mismatch);
4477
4478 #undef EXPECT
4479 #undef CHECK_MATCH
4480 }
4481
4482 static void
4483 run_test ()
4484 {
4485 test_mapped_index_find_name_component_bounds ();
4486 test_dw2_expand_symtabs_matching_symbol ();
4487 }
4488
4489 }} // namespace selftests::dw2_expand_symtabs_matching
4490
4491 #endif /* GDB_SELF_TEST */
4492
4493 /* If FILE_MATCHER is NULL or if PER_CU has
4494 dwarf2_per_cu_quick_data::MARK set (see
4495 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4496 EXPANSION_NOTIFY on it. */
4497
4498 static void
4499 dw2_expand_symtabs_matching_one
4500 (struct dwarf2_per_cu_data *per_cu,
4501 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4502 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4503 {
4504 if (file_matcher == NULL || per_cu->v.quick->mark)
4505 {
4506 bool symtab_was_null
4507 = (per_cu->v.quick->compunit_symtab == NULL);
4508
4509 dw2_instantiate_symtab (per_cu, false);
4510
4511 if (expansion_notify != NULL
4512 && symtab_was_null
4513 && per_cu->v.quick->compunit_symtab != NULL)
4514 expansion_notify (per_cu->v.quick->compunit_symtab);
4515 }
4516 }
4517
4518 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4519 matched, to expand corresponding CUs that were marked. IDX is the
4520 index of the symbol name that matched. */
4521
4522 static void
4523 dw2_expand_marked_cus
4524 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4525 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4526 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4527 search_domain kind)
4528 {
4529 offset_type *vec, vec_len, vec_idx;
4530 bool global_seen = false;
4531 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4532
4533 vec = (offset_type *) (index.constant_pool
4534 + MAYBE_SWAP (index.symbol_table[idx].vec));
4535 vec_len = MAYBE_SWAP (vec[0]);
4536 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4537 {
4538 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4539 /* This value is only valid for index versions >= 7. */
4540 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4541 gdb_index_symbol_kind symbol_kind =
4542 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4543 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4544 /* Only check the symbol attributes if they're present.
4545 Indices prior to version 7 don't record them,
4546 and indices >= 7 may elide them for certain symbols
4547 (gold does this). */
4548 int attrs_valid =
4549 (index.version >= 7
4550 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4551
4552 /* Work around gold/15646. */
4553 if (attrs_valid)
4554 {
4555 if (!is_static && global_seen)
4556 continue;
4557 if (!is_static)
4558 global_seen = true;
4559 }
4560
4561 /* Only check the symbol's kind if it has one. */
4562 if (attrs_valid)
4563 {
4564 switch (kind)
4565 {
4566 case VARIABLES_DOMAIN:
4567 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4568 continue;
4569 break;
4570 case FUNCTIONS_DOMAIN:
4571 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4572 continue;
4573 break;
4574 case TYPES_DOMAIN:
4575 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4576 continue;
4577 break;
4578 case MODULES_DOMAIN:
4579 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4580 continue;
4581 break;
4582 default:
4583 break;
4584 }
4585 }
4586
4587 /* Don't crash on bad data. */
4588 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4589 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4590 {
4591 complaint (_(".gdb_index entry has bad CU index"
4592 " [in module %s]"),
4593 objfile_name (dwarf2_per_objfile->objfile));
4594 continue;
4595 }
4596
4597 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4598 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4599 expansion_notify);
4600 }
4601 }
4602
4603 /* If FILE_MATCHER is non-NULL, set all the
4604 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4605 that match FILE_MATCHER. */
4606
4607 static void
4608 dw_expand_symtabs_matching_file_matcher
4609 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4610 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4611 {
4612 if (file_matcher == NULL)
4613 return;
4614
4615 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4616 htab_eq_pointer,
4617 NULL, xcalloc, xfree));
4618 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4619 htab_eq_pointer,
4620 NULL, xcalloc, xfree));
4621
4622 /* The rule is CUs specify all the files, including those used by
4623 any TU, so there's no need to scan TUs here. */
4624
4625 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4626 {
4627 QUIT;
4628
4629 per_cu->v.quick->mark = 0;
4630
4631 /* We only need to look at symtabs not already expanded. */
4632 if (per_cu->v.quick->compunit_symtab)
4633 continue;
4634
4635 quick_file_names *file_data = dw2_get_file_names (per_cu);
4636 if (file_data == NULL)
4637 continue;
4638
4639 if (htab_find (visited_not_found.get (), file_data) != NULL)
4640 continue;
4641 else if (htab_find (visited_found.get (), file_data) != NULL)
4642 {
4643 per_cu->v.quick->mark = 1;
4644 continue;
4645 }
4646
4647 for (int j = 0; j < file_data->num_file_names; ++j)
4648 {
4649 const char *this_real_name;
4650
4651 if (file_matcher (file_data->file_names[j], false))
4652 {
4653 per_cu->v.quick->mark = 1;
4654 break;
4655 }
4656
4657 /* Before we invoke realpath, which can get expensive when many
4658 files are involved, do a quick comparison of the basenames. */
4659 if (!basenames_may_differ
4660 && !file_matcher (lbasename (file_data->file_names[j]),
4661 true))
4662 continue;
4663
4664 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4665 file_data, j);
4666 if (file_matcher (this_real_name, false))
4667 {
4668 per_cu->v.quick->mark = 1;
4669 break;
4670 }
4671 }
4672
4673 void **slot = htab_find_slot (per_cu->v.quick->mark
4674 ? visited_found.get ()
4675 : visited_not_found.get (),
4676 file_data, INSERT);
4677 *slot = file_data;
4678 }
4679 }
4680
4681 static void
4682 dw2_expand_symtabs_matching
4683 (struct objfile *objfile,
4684 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4685 const lookup_name_info *lookup_name,
4686 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4687 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4688 enum search_domain kind)
4689 {
4690 struct dwarf2_per_objfile *dwarf2_per_objfile
4691 = get_dwarf2_per_objfile (objfile);
4692
4693 /* index_table is NULL if OBJF_READNOW. */
4694 if (!dwarf2_per_objfile->per_bfd->index_table)
4695 return;
4696
4697 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4698
4699 if (symbol_matcher == NULL && lookup_name == NULL)
4700 {
4701 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4702 {
4703 QUIT;
4704
4705 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4706 expansion_notify);
4707 }
4708 return;
4709 }
4710
4711 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4712
4713 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4714 symbol_matcher,
4715 kind, [&] (offset_type idx)
4716 {
4717 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4718 expansion_notify, kind);
4719 return true;
4720 });
4721 }
4722
4723 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4724 symtab. */
4725
4726 static struct compunit_symtab *
4727 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4728 CORE_ADDR pc)
4729 {
4730 int i;
4731
4732 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4733 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4734 return cust;
4735
4736 if (cust->includes == NULL)
4737 return NULL;
4738
4739 for (i = 0; cust->includes[i]; ++i)
4740 {
4741 struct compunit_symtab *s = cust->includes[i];
4742
4743 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4744 if (s != NULL)
4745 return s;
4746 }
4747
4748 return NULL;
4749 }
4750
4751 static struct compunit_symtab *
4752 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4753 struct bound_minimal_symbol msymbol,
4754 CORE_ADDR pc,
4755 struct obj_section *section,
4756 int warn_if_readin)
4757 {
4758 struct dwarf2_per_cu_data *data;
4759 struct compunit_symtab *result;
4760
4761 if (!objfile->partial_symtabs->psymtabs_addrmap)
4762 return NULL;
4763
4764 CORE_ADDR baseaddr = objfile->text_section_offset ();
4765 data = (struct dwarf2_per_cu_data *) addrmap_find
4766 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4767 if (!data)
4768 return NULL;
4769
4770 if (warn_if_readin && data->v.quick->compunit_symtab)
4771 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4772 paddress (objfile->arch (), pc));
4773
4774 result
4775 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4776 false),
4777 pc);
4778 gdb_assert (result != NULL);
4779 return result;
4780 }
4781
4782 static void
4783 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4784 void *data, int need_fullname)
4785 {
4786 struct dwarf2_per_objfile *dwarf2_per_objfile
4787 = get_dwarf2_per_objfile (objfile);
4788
4789 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4790 {
4791 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4792
4793 htab_up visited (htab_create_alloc (10,
4794 htab_hash_pointer, htab_eq_pointer,
4795 NULL, xcalloc, xfree));
4796
4797 /* The rule is CUs specify all the files, including those used
4798 by any TU, so there's no need to scan TUs here. We can
4799 ignore file names coming from already-expanded CUs. */
4800
4801 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4802 {
4803 if (per_cu->v.quick->compunit_symtab)
4804 {
4805 void **slot = htab_find_slot (visited.get (),
4806 per_cu->v.quick->file_names,
4807 INSERT);
4808
4809 *slot = per_cu->v.quick->file_names;
4810 }
4811 }
4812
4813 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4814 {
4815 /* We only need to look at symtabs not already expanded. */
4816 if (per_cu->v.quick->compunit_symtab)
4817 continue;
4818
4819 quick_file_names *file_data = dw2_get_file_names (per_cu);
4820 if (file_data == NULL)
4821 continue;
4822
4823 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4824 if (*slot)
4825 {
4826 /* Already visited. */
4827 continue;
4828 }
4829 *slot = file_data;
4830
4831 for (int j = 0; j < file_data->num_file_names; ++j)
4832 {
4833 const char *filename = file_data->file_names[j];
4834 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4835 }
4836 }
4837 }
4838
4839 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4840 {
4841 gdb::unique_xmalloc_ptr<char> this_real_name;
4842
4843 if (need_fullname)
4844 this_real_name = gdb_realpath (filename);
4845 (*fun) (filename, this_real_name.get (), data);
4846 });
4847 }
4848
4849 static int
4850 dw2_has_symbols (struct objfile *objfile)
4851 {
4852 return 1;
4853 }
4854
4855 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4856 {
4857 dw2_has_symbols,
4858 dw2_find_last_source_symtab,
4859 dw2_forget_cached_source_info,
4860 dw2_map_symtabs_matching_filename,
4861 dw2_lookup_symbol,
4862 NULL,
4863 dw2_print_stats,
4864 dw2_dump,
4865 dw2_expand_symtabs_for_function,
4866 dw2_expand_all_symtabs,
4867 dw2_expand_symtabs_with_fullname,
4868 dw2_map_matching_symbols,
4869 dw2_expand_symtabs_matching,
4870 dw2_find_pc_sect_compunit_symtab,
4871 NULL,
4872 dw2_map_symbol_filenames
4873 };
4874
4875 /* DWARF-5 debug_names reader. */
4876
4877 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4878 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4879
4880 /* A helper function that reads the .debug_names section in SECTION
4881 and fills in MAP. FILENAME is the name of the file containing the
4882 section; it is used for error reporting.
4883
4884 Returns true if all went well, false otherwise. */
4885
4886 static bool
4887 read_debug_names_from_section (struct objfile *objfile,
4888 const char *filename,
4889 struct dwarf2_section_info *section,
4890 mapped_debug_names &map)
4891 {
4892 if (section->empty ())
4893 return false;
4894
4895 /* Older elfutils strip versions could keep the section in the main
4896 executable while splitting it for the separate debug info file. */
4897 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4898 return false;
4899
4900 section->read (objfile);
4901
4902 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4903
4904 const gdb_byte *addr = section->buffer;
4905
4906 bfd *const abfd = section->get_bfd_owner ();
4907
4908 unsigned int bytes_read;
4909 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4910 addr += bytes_read;
4911
4912 map.dwarf5_is_dwarf64 = bytes_read != 4;
4913 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4914 if (bytes_read + length != section->size)
4915 {
4916 /* There may be multiple per-CU indices. */
4917 warning (_("Section .debug_names in %s length %s does not match "
4918 "section length %s, ignoring .debug_names."),
4919 filename, plongest (bytes_read + length),
4920 pulongest (section->size));
4921 return false;
4922 }
4923
4924 /* The version number. */
4925 uint16_t version = read_2_bytes (abfd, addr);
4926 addr += 2;
4927 if (version != 5)
4928 {
4929 warning (_("Section .debug_names in %s has unsupported version %d, "
4930 "ignoring .debug_names."),
4931 filename, version);
4932 return false;
4933 }
4934
4935 /* Padding. */
4936 uint16_t padding = read_2_bytes (abfd, addr);
4937 addr += 2;
4938 if (padding != 0)
4939 {
4940 warning (_("Section .debug_names in %s has unsupported padding %d, "
4941 "ignoring .debug_names."),
4942 filename, padding);
4943 return false;
4944 }
4945
4946 /* comp_unit_count - The number of CUs in the CU list. */
4947 map.cu_count = read_4_bytes (abfd, addr);
4948 addr += 4;
4949
4950 /* local_type_unit_count - The number of TUs in the local TU
4951 list. */
4952 map.tu_count = read_4_bytes (abfd, addr);
4953 addr += 4;
4954
4955 /* foreign_type_unit_count - The number of TUs in the foreign TU
4956 list. */
4957 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4958 addr += 4;
4959 if (foreign_tu_count != 0)
4960 {
4961 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4962 "ignoring .debug_names."),
4963 filename, static_cast<unsigned long> (foreign_tu_count));
4964 return false;
4965 }
4966
4967 /* bucket_count - The number of hash buckets in the hash lookup
4968 table. */
4969 map.bucket_count = read_4_bytes (abfd, addr);
4970 addr += 4;
4971
4972 /* name_count - The number of unique names in the index. */
4973 map.name_count = read_4_bytes (abfd, addr);
4974 addr += 4;
4975
4976 /* abbrev_table_size - The size in bytes of the abbreviations
4977 table. */
4978 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4979 addr += 4;
4980
4981 /* augmentation_string_size - The size in bytes of the augmentation
4982 string. This value is rounded up to a multiple of 4. */
4983 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4984 addr += 4;
4985 map.augmentation_is_gdb = ((augmentation_string_size
4986 == sizeof (dwarf5_augmentation))
4987 && memcmp (addr, dwarf5_augmentation,
4988 sizeof (dwarf5_augmentation)) == 0);
4989 augmentation_string_size += (-augmentation_string_size) & 3;
4990 addr += augmentation_string_size;
4991
4992 /* List of CUs */
4993 map.cu_table_reordered = addr;
4994 addr += map.cu_count * map.offset_size;
4995
4996 /* List of Local TUs */
4997 map.tu_table_reordered = addr;
4998 addr += map.tu_count * map.offset_size;
4999
5000 /* Hash Lookup Table */
5001 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5002 addr += map.bucket_count * 4;
5003 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5004 addr += map.name_count * 4;
5005
5006 /* Name Table */
5007 map.name_table_string_offs_reordered = addr;
5008 addr += map.name_count * map.offset_size;
5009 map.name_table_entry_offs_reordered = addr;
5010 addr += map.name_count * map.offset_size;
5011
5012 const gdb_byte *abbrev_table_start = addr;
5013 for (;;)
5014 {
5015 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5016 addr += bytes_read;
5017 if (index_num == 0)
5018 break;
5019
5020 const auto insertpair
5021 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5022 if (!insertpair.second)
5023 {
5024 warning (_("Section .debug_names in %s has duplicate index %s, "
5025 "ignoring .debug_names."),
5026 filename, pulongest (index_num));
5027 return false;
5028 }
5029 mapped_debug_names::index_val &indexval = insertpair.first->second;
5030 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5031 addr += bytes_read;
5032
5033 for (;;)
5034 {
5035 mapped_debug_names::index_val::attr attr;
5036 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5037 addr += bytes_read;
5038 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5039 addr += bytes_read;
5040 if (attr.form == DW_FORM_implicit_const)
5041 {
5042 attr.implicit_const = read_signed_leb128 (abfd, addr,
5043 &bytes_read);
5044 addr += bytes_read;
5045 }
5046 if (attr.dw_idx == 0 && attr.form == 0)
5047 break;
5048 indexval.attr_vec.push_back (std::move (attr));
5049 }
5050 }
5051 if (addr != abbrev_table_start + abbrev_table_size)
5052 {
5053 warning (_("Section .debug_names in %s has abbreviation_table "
5054 "of size %s vs. written as %u, ignoring .debug_names."),
5055 filename, plongest (addr - abbrev_table_start),
5056 abbrev_table_size);
5057 return false;
5058 }
5059 map.entry_pool = addr;
5060
5061 return true;
5062 }
5063
5064 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5065 list. */
5066
5067 static void
5068 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5069 const mapped_debug_names &map,
5070 dwarf2_section_info &section,
5071 bool is_dwz)
5072 {
5073 if (!map.augmentation_is_gdb)
5074 {
5075 for (uint32_t i = 0; i < map.cu_count; ++i)
5076 {
5077 sect_offset sect_off
5078 = (sect_offset) (extract_unsigned_integer
5079 (map.cu_table_reordered + i * map.offset_size,
5080 map.offset_size,
5081 map.dwarf5_byte_order));
5082 /* We don't know the length of the CU, because the CU list in a
5083 .debug_names index can be incomplete, so we can't use the start of
5084 the next CU as end of this CU. We create the CUs here with length 0,
5085 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5086 dwarf2_per_cu_data *per_cu
5087 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5088 sect_off, 0);
5089 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5090 }
5091 }
5092
5093 sect_offset sect_off_prev;
5094 for (uint32_t i = 0; i <= map.cu_count; ++i)
5095 {
5096 sect_offset sect_off_next;
5097 if (i < map.cu_count)
5098 {
5099 sect_off_next
5100 = (sect_offset) (extract_unsigned_integer
5101 (map.cu_table_reordered + i * map.offset_size,
5102 map.offset_size,
5103 map.dwarf5_byte_order));
5104 }
5105 else
5106 sect_off_next = (sect_offset) section.size;
5107 if (i >= 1)
5108 {
5109 const ULONGEST length = sect_off_next - sect_off_prev;
5110 dwarf2_per_cu_data *per_cu
5111 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5112 sect_off_prev, length);
5113 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5114 }
5115 sect_off_prev = sect_off_next;
5116 }
5117 }
5118
5119 /* Read the CU list from the mapped index, and use it to create all
5120 the CU objects for this dwarf2_per_objfile. */
5121
5122 static void
5123 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5124 const mapped_debug_names &map,
5125 const mapped_debug_names &dwz_map)
5126 {
5127 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5128 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5129
5130 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5131 dwarf2_per_objfile->per_bfd->info,
5132 false /* is_dwz */);
5133
5134 if (dwz_map.cu_count == 0)
5135 return;
5136
5137 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5138 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5139 true /* is_dwz */);
5140 }
5141
5142 /* Read .debug_names. If everything went ok, initialize the "quick"
5143 elements of all the CUs and return true. Otherwise, return false. */
5144
5145 static bool
5146 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5147 {
5148 std::unique_ptr<mapped_debug_names> map
5149 (new mapped_debug_names (dwarf2_per_objfile));
5150 mapped_debug_names dwz_map (dwarf2_per_objfile);
5151 struct objfile *objfile = dwarf2_per_objfile->objfile;
5152
5153 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5154 &dwarf2_per_objfile->per_bfd->debug_names,
5155 *map))
5156 return false;
5157
5158 /* Don't use the index if it's empty. */
5159 if (map->name_count == 0)
5160 return false;
5161
5162 /* If there is a .dwz file, read it so we can get its CU list as
5163 well. */
5164 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5165 if (dwz != NULL)
5166 {
5167 if (!read_debug_names_from_section (objfile,
5168 bfd_get_filename (dwz->dwz_bfd.get ()),
5169 &dwz->debug_names, dwz_map))
5170 {
5171 warning (_("could not read '.debug_names' section from %s; skipping"),
5172 bfd_get_filename (dwz->dwz_bfd.get ()));
5173 return false;
5174 }
5175 }
5176
5177 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5178
5179 if (map->tu_count != 0)
5180 {
5181 /* We can only handle a single .debug_types when we have an
5182 index. */
5183 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5184 return false;
5185
5186 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5187
5188 create_signatured_type_table_from_debug_names
5189 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5190 }
5191
5192 create_addrmap_from_aranges (dwarf2_per_objfile,
5193 &dwarf2_per_objfile->per_bfd->debug_aranges);
5194
5195 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5196 dwarf2_per_objfile->per_bfd->using_index = 1;
5197 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5198 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5199
5200 return true;
5201 }
5202
5203 /* Type used to manage iterating over all CUs looking for a symbol for
5204 .debug_names. */
5205
5206 class dw2_debug_names_iterator
5207 {
5208 public:
5209 dw2_debug_names_iterator (const mapped_debug_names &map,
5210 gdb::optional<block_enum> block_index,
5211 domain_enum domain,
5212 const char *name)
5213 : m_map (map), m_block_index (block_index), m_domain (domain),
5214 m_addr (find_vec_in_debug_names (map, name))
5215 {}
5216
5217 dw2_debug_names_iterator (const mapped_debug_names &map,
5218 search_domain search, uint32_t namei)
5219 : m_map (map),
5220 m_search (search),
5221 m_addr (find_vec_in_debug_names (map, namei))
5222 {}
5223
5224 dw2_debug_names_iterator (const mapped_debug_names &map,
5225 block_enum block_index, domain_enum domain,
5226 uint32_t namei)
5227 : m_map (map), m_block_index (block_index), m_domain (domain),
5228 m_addr (find_vec_in_debug_names (map, namei))
5229 {}
5230
5231 /* Return the next matching CU or NULL if there are no more. */
5232 dwarf2_per_cu_data *next ();
5233
5234 private:
5235 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5236 const char *name);
5237 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5238 uint32_t namei);
5239
5240 /* The internalized form of .debug_names. */
5241 const mapped_debug_names &m_map;
5242
5243 /* If set, only look for symbols that match that block. Valid values are
5244 GLOBAL_BLOCK and STATIC_BLOCK. */
5245 const gdb::optional<block_enum> m_block_index;
5246
5247 /* The kind of symbol we're looking for. */
5248 const domain_enum m_domain = UNDEF_DOMAIN;
5249 const search_domain m_search = ALL_DOMAIN;
5250
5251 /* The list of CUs from the index entry of the symbol, or NULL if
5252 not found. */
5253 const gdb_byte *m_addr;
5254 };
5255
5256 const char *
5257 mapped_debug_names::namei_to_name (uint32_t namei) const
5258 {
5259 const ULONGEST namei_string_offs
5260 = extract_unsigned_integer ((name_table_string_offs_reordered
5261 + namei * offset_size),
5262 offset_size,
5263 dwarf5_byte_order);
5264 return read_indirect_string_at_offset (dwarf2_per_objfile,
5265 namei_string_offs);
5266 }
5267
5268 /* Find a slot in .debug_names for the object named NAME. If NAME is
5269 found, return pointer to its pool data. If NAME cannot be found,
5270 return NULL. */
5271
5272 const gdb_byte *
5273 dw2_debug_names_iterator::find_vec_in_debug_names
5274 (const mapped_debug_names &map, const char *name)
5275 {
5276 int (*cmp) (const char *, const char *);
5277
5278 gdb::unique_xmalloc_ptr<char> without_params;
5279 if (current_language->la_language == language_cplus
5280 || current_language->la_language == language_fortran
5281 || current_language->la_language == language_d)
5282 {
5283 /* NAME is already canonical. Drop any qualifiers as
5284 .debug_names does not contain any. */
5285
5286 if (strchr (name, '(') != NULL)
5287 {
5288 without_params = cp_remove_params (name);
5289 if (without_params != NULL)
5290 name = without_params.get ();
5291 }
5292 }
5293
5294 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5295
5296 const uint32_t full_hash = dwarf5_djb_hash (name);
5297 uint32_t namei
5298 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5299 (map.bucket_table_reordered
5300 + (full_hash % map.bucket_count)), 4,
5301 map.dwarf5_byte_order);
5302 if (namei == 0)
5303 return NULL;
5304 --namei;
5305 if (namei >= map.name_count)
5306 {
5307 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5308 "[in module %s]"),
5309 namei, map.name_count,
5310 objfile_name (map.dwarf2_per_objfile->objfile));
5311 return NULL;
5312 }
5313
5314 for (;;)
5315 {
5316 const uint32_t namei_full_hash
5317 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5318 (map.hash_table_reordered + namei), 4,
5319 map.dwarf5_byte_order);
5320 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5321 return NULL;
5322
5323 if (full_hash == namei_full_hash)
5324 {
5325 const char *const namei_string = map.namei_to_name (namei);
5326
5327 #if 0 /* An expensive sanity check. */
5328 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5329 {
5330 complaint (_("Wrong .debug_names hash for string at index %u "
5331 "[in module %s]"),
5332 namei, objfile_name (dwarf2_per_objfile->objfile));
5333 return NULL;
5334 }
5335 #endif
5336
5337 if (cmp (namei_string, name) == 0)
5338 {
5339 const ULONGEST namei_entry_offs
5340 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5341 + namei * map.offset_size),
5342 map.offset_size, map.dwarf5_byte_order);
5343 return map.entry_pool + namei_entry_offs;
5344 }
5345 }
5346
5347 ++namei;
5348 if (namei >= map.name_count)
5349 return NULL;
5350 }
5351 }
5352
5353 const gdb_byte *
5354 dw2_debug_names_iterator::find_vec_in_debug_names
5355 (const mapped_debug_names &map, uint32_t namei)
5356 {
5357 if (namei >= map.name_count)
5358 {
5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5360 "[in module %s]"),
5361 namei, map.name_count,
5362 objfile_name (map.dwarf2_per_objfile->objfile));
5363 return NULL;
5364 }
5365
5366 const ULONGEST namei_entry_offs
5367 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5368 + namei * map.offset_size),
5369 map.offset_size, map.dwarf5_byte_order);
5370 return map.entry_pool + namei_entry_offs;
5371 }
5372
5373 /* See dw2_debug_names_iterator. */
5374
5375 dwarf2_per_cu_data *
5376 dw2_debug_names_iterator::next ()
5377 {
5378 if (m_addr == NULL)
5379 return NULL;
5380
5381 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5382 struct objfile *objfile = dwarf2_per_objfile->objfile;
5383 bfd *const abfd = objfile->obfd;
5384
5385 again:
5386
5387 unsigned int bytes_read;
5388 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5389 m_addr += bytes_read;
5390 if (abbrev == 0)
5391 return NULL;
5392
5393 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5394 if (indexval_it == m_map.abbrev_map.cend ())
5395 {
5396 complaint (_("Wrong .debug_names undefined abbrev code %s "
5397 "[in module %s]"),
5398 pulongest (abbrev), objfile_name (objfile));
5399 return NULL;
5400 }
5401 const mapped_debug_names::index_val &indexval = indexval_it->second;
5402 enum class symbol_linkage {
5403 unknown,
5404 static_,
5405 extern_,
5406 } symbol_linkage_ = symbol_linkage::unknown;
5407 dwarf2_per_cu_data *per_cu = NULL;
5408 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5409 {
5410 ULONGEST ull;
5411 switch (attr.form)
5412 {
5413 case DW_FORM_implicit_const:
5414 ull = attr.implicit_const;
5415 break;
5416 case DW_FORM_flag_present:
5417 ull = 1;
5418 break;
5419 case DW_FORM_udata:
5420 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5421 m_addr += bytes_read;
5422 break;
5423 case DW_FORM_ref4:
5424 ull = read_4_bytes (abfd, m_addr);
5425 m_addr += 4;
5426 break;
5427 case DW_FORM_ref8:
5428 ull = read_8_bytes (abfd, m_addr);
5429 m_addr += 8;
5430 break;
5431 case DW_FORM_ref_sig8:
5432 ull = read_8_bytes (abfd, m_addr);
5433 m_addr += 8;
5434 break;
5435 default:
5436 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5437 dwarf_form_name (attr.form),
5438 objfile_name (objfile));
5439 return NULL;
5440 }
5441 switch (attr.dw_idx)
5442 {
5443 case DW_IDX_compile_unit:
5444 /* Don't crash on bad data. */
5445 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5446 {
5447 complaint (_(".debug_names entry has bad CU index %s"
5448 " [in module %s]"),
5449 pulongest (ull),
5450 objfile_name (dwarf2_per_objfile->objfile));
5451 continue;
5452 }
5453 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5454 break;
5455 case DW_IDX_type_unit:
5456 /* Don't crash on bad data. */
5457 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5458 {
5459 complaint (_(".debug_names entry has bad TU index %s"
5460 " [in module %s]"),
5461 pulongest (ull),
5462 objfile_name (dwarf2_per_objfile->objfile));
5463 continue;
5464 }
5465 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5466 break;
5467 case DW_IDX_die_offset:
5468 /* In a per-CU index (as opposed to a per-module index), index
5469 entries without CU attribute implicitly refer to the single CU. */
5470 if (per_cu == NULL)
5471 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5472 break;
5473 case DW_IDX_GNU_internal:
5474 if (!m_map.augmentation_is_gdb)
5475 break;
5476 symbol_linkage_ = symbol_linkage::static_;
5477 break;
5478 case DW_IDX_GNU_external:
5479 if (!m_map.augmentation_is_gdb)
5480 break;
5481 symbol_linkage_ = symbol_linkage::extern_;
5482 break;
5483 }
5484 }
5485
5486 /* Skip if already read in. */
5487 if (per_cu->v.quick->compunit_symtab)
5488 goto again;
5489
5490 /* Check static vs global. */
5491 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5492 {
5493 const bool want_static = *m_block_index == STATIC_BLOCK;
5494 const bool symbol_is_static =
5495 symbol_linkage_ == symbol_linkage::static_;
5496 if (want_static != symbol_is_static)
5497 goto again;
5498 }
5499
5500 /* Match dw2_symtab_iter_next, symbol_kind
5501 and debug_names::psymbol_tag. */
5502 switch (m_domain)
5503 {
5504 case VAR_DOMAIN:
5505 switch (indexval.dwarf_tag)
5506 {
5507 case DW_TAG_variable:
5508 case DW_TAG_subprogram:
5509 /* Some types are also in VAR_DOMAIN. */
5510 case DW_TAG_typedef:
5511 case DW_TAG_structure_type:
5512 break;
5513 default:
5514 goto again;
5515 }
5516 break;
5517 case STRUCT_DOMAIN:
5518 switch (indexval.dwarf_tag)
5519 {
5520 case DW_TAG_typedef:
5521 case DW_TAG_structure_type:
5522 break;
5523 default:
5524 goto again;
5525 }
5526 break;
5527 case LABEL_DOMAIN:
5528 switch (indexval.dwarf_tag)
5529 {
5530 case 0:
5531 case DW_TAG_variable:
5532 break;
5533 default:
5534 goto again;
5535 }
5536 break;
5537 case MODULE_DOMAIN:
5538 switch (indexval.dwarf_tag)
5539 {
5540 case DW_TAG_module:
5541 break;
5542 default:
5543 goto again;
5544 }
5545 break;
5546 default:
5547 break;
5548 }
5549
5550 /* Match dw2_expand_symtabs_matching, symbol_kind and
5551 debug_names::psymbol_tag. */
5552 switch (m_search)
5553 {
5554 case VARIABLES_DOMAIN:
5555 switch (indexval.dwarf_tag)
5556 {
5557 case DW_TAG_variable:
5558 break;
5559 default:
5560 goto again;
5561 }
5562 break;
5563 case FUNCTIONS_DOMAIN:
5564 switch (indexval.dwarf_tag)
5565 {
5566 case DW_TAG_subprogram:
5567 break;
5568 default:
5569 goto again;
5570 }
5571 break;
5572 case TYPES_DOMAIN:
5573 switch (indexval.dwarf_tag)
5574 {
5575 case DW_TAG_typedef:
5576 case DW_TAG_structure_type:
5577 break;
5578 default:
5579 goto again;
5580 }
5581 break;
5582 case MODULES_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case DW_TAG_module:
5586 break;
5587 default:
5588 goto again;
5589 }
5590 default:
5591 break;
5592 }
5593
5594 return per_cu;
5595 }
5596
5597 static struct compunit_symtab *
5598 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5599 const char *name, domain_enum domain)
5600 {
5601 struct dwarf2_per_objfile *dwarf2_per_objfile
5602 = get_dwarf2_per_objfile (objfile);
5603
5604 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5605 if (!mapp)
5606 {
5607 /* index is NULL if OBJF_READNOW. */
5608 return NULL;
5609 }
5610 const auto &map = *mapp;
5611
5612 dw2_debug_names_iterator iter (map, block_index, domain, name);
5613
5614 struct compunit_symtab *stab_best = NULL;
5615 struct dwarf2_per_cu_data *per_cu;
5616 while ((per_cu = iter.next ()) != NULL)
5617 {
5618 struct symbol *sym, *with_opaque = NULL;
5619 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5620 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5621 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5622
5623 sym = block_find_symbol (block, name, domain,
5624 block_find_non_opaque_type_preferred,
5625 &with_opaque);
5626
5627 /* Some caution must be observed with overloaded functions and
5628 methods, since the index will not contain any overload
5629 information (but NAME might contain it). */
5630
5631 if (sym != NULL
5632 && strcmp_iw (sym->search_name (), name) == 0)
5633 return stab;
5634 if (with_opaque != NULL
5635 && strcmp_iw (with_opaque->search_name (), name) == 0)
5636 stab_best = stab;
5637
5638 /* Keep looking through other CUs. */
5639 }
5640
5641 return stab_best;
5642 }
5643
5644 /* This dumps minimal information about .debug_names. It is called
5645 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5646 uses this to verify that .debug_names has been loaded. */
5647
5648 static void
5649 dw2_debug_names_dump (struct objfile *objfile)
5650 {
5651 struct dwarf2_per_objfile *dwarf2_per_objfile
5652 = get_dwarf2_per_objfile (objfile);
5653
5654 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5655 printf_filtered (".debug_names:");
5656 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5657 printf_filtered (" exists\n");
5658 else
5659 printf_filtered (" faked for \"readnow\"\n");
5660 printf_filtered ("\n");
5661 }
5662
5663 static void
5664 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5665 const char *func_name)
5666 {
5667 struct dwarf2_per_objfile *dwarf2_per_objfile
5668 = get_dwarf2_per_objfile (objfile);
5669
5670 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5671 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5672 {
5673 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5674
5675 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5676
5677 struct dwarf2_per_cu_data *per_cu;
5678 while ((per_cu = iter.next ()) != NULL)
5679 dw2_instantiate_symtab (per_cu, false);
5680 }
5681 }
5682
5683 static void
5684 dw2_debug_names_map_matching_symbols
5685 (struct objfile *objfile,
5686 const lookup_name_info &name, domain_enum domain,
5687 int global,
5688 gdb::function_view<symbol_found_callback_ftype> callback,
5689 symbol_compare_ftype *ordered_compare)
5690 {
5691 struct dwarf2_per_objfile *dwarf2_per_objfile
5692 = get_dwarf2_per_objfile (objfile);
5693
5694 /* debug_names_table is NULL if OBJF_READNOW. */
5695 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5696 return;
5697
5698 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5699 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5700
5701 const char *match_name = name.ada ().lookup_name ().c_str ();
5702 auto matcher = [&] (const char *symname)
5703 {
5704 if (ordered_compare == nullptr)
5705 return true;
5706 return ordered_compare (symname, match_name) == 0;
5707 };
5708
5709 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5710 [&] (offset_type namei)
5711 {
5712 /* The name was matched, now expand corresponding CUs that were
5713 marked. */
5714 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5715
5716 struct dwarf2_per_cu_data *per_cu;
5717 while ((per_cu = iter.next ()) != NULL)
5718 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5719 return true;
5720 });
5721
5722 /* It's a shame we couldn't do this inside the
5723 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5724 that have already been expanded. Instead, this loop matches what
5725 the psymtab code does. */
5726 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5727 {
5728 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5729 if (cust != nullptr)
5730 {
5731 const struct block *block
5732 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5733 if (!iterate_over_symbols_terminated (block, name,
5734 domain, callback))
5735 break;
5736 }
5737 }
5738 }
5739
5740 static void
5741 dw2_debug_names_expand_symtabs_matching
5742 (struct objfile *objfile,
5743 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5744 const lookup_name_info *lookup_name,
5745 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5746 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5747 enum search_domain kind)
5748 {
5749 struct dwarf2_per_objfile *dwarf2_per_objfile
5750 = get_dwarf2_per_objfile (objfile);
5751
5752 /* debug_names_table is NULL if OBJF_READNOW. */
5753 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5754 return;
5755
5756 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5757
5758 if (symbol_matcher == NULL && lookup_name == NULL)
5759 {
5760 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5761 {
5762 QUIT;
5763
5764 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5765 expansion_notify);
5766 }
5767 return;
5768 }
5769
5770 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5771
5772 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5773 symbol_matcher,
5774 kind, [&] (offset_type namei)
5775 {
5776 /* The name was matched, now expand corresponding CUs that were
5777 marked. */
5778 dw2_debug_names_iterator iter (map, kind, namei);
5779
5780 struct dwarf2_per_cu_data *per_cu;
5781 while ((per_cu = iter.next ()) != NULL)
5782 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5783 expansion_notify);
5784 return true;
5785 });
5786 }
5787
5788 const struct quick_symbol_functions dwarf2_debug_names_functions =
5789 {
5790 dw2_has_symbols,
5791 dw2_find_last_source_symtab,
5792 dw2_forget_cached_source_info,
5793 dw2_map_symtabs_matching_filename,
5794 dw2_debug_names_lookup_symbol,
5795 NULL,
5796 dw2_print_stats,
5797 dw2_debug_names_dump,
5798 dw2_debug_names_expand_symtabs_for_function,
5799 dw2_expand_all_symtabs,
5800 dw2_expand_symtabs_with_fullname,
5801 dw2_debug_names_map_matching_symbols,
5802 dw2_debug_names_expand_symtabs_matching,
5803 dw2_find_pc_sect_compunit_symtab,
5804 NULL,
5805 dw2_map_symbol_filenames
5806 };
5807
5808 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5809 to either a dwarf2_per_bfd or dwz_file object. */
5810
5811 template <typename T>
5812 static gdb::array_view<const gdb_byte>
5813 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5814 {
5815 dwarf2_section_info *section = &section_owner->gdb_index;
5816
5817 if (section->empty ())
5818 return {};
5819
5820 /* Older elfutils strip versions could keep the section in the main
5821 executable while splitting it for the separate debug info file. */
5822 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5823 return {};
5824
5825 section->read (obj);
5826
5827 /* dwarf2_section_info::size is a bfd_size_type, while
5828 gdb::array_view works with size_t. On 32-bit hosts, with
5829 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5830 is 32-bit. So we need an explicit narrowing conversion here.
5831 This is fine, because it's impossible to allocate or mmap an
5832 array/buffer larger than what size_t can represent. */
5833 return gdb::make_array_view (section->buffer, section->size);
5834 }
5835
5836 /* Lookup the index cache for the contents of the index associated to
5837 DWARF2_OBJ. */
5838
5839 static gdb::array_view<const gdb_byte>
5840 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5841 {
5842 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5843 if (build_id == nullptr)
5844 return {};
5845
5846 return global_index_cache.lookup_gdb_index (build_id,
5847 &dwarf2_per_bfd->index_cache_res);
5848 }
5849
5850 /* Same as the above, but for DWZ. */
5851
5852 static gdb::array_view<const gdb_byte>
5853 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5854 {
5855 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5856 if (build_id == nullptr)
5857 return {};
5858
5859 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5860 }
5861
5862 /* See symfile.h. */
5863
5864 bool
5865 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5866 {
5867 struct dwarf2_per_objfile *dwarf2_per_objfile
5868 = get_dwarf2_per_objfile (objfile);
5869
5870 /* If we're about to read full symbols, don't bother with the
5871 indices. In this case we also don't care if some other debug
5872 format is making psymtabs, because they are all about to be
5873 expanded anyway. */
5874 if ((objfile->flags & OBJF_READNOW))
5875 {
5876 dwarf2_per_objfile->per_bfd->using_index = 1;
5877 create_all_comp_units (dwarf2_per_objfile);
5878 create_all_type_units (dwarf2_per_objfile);
5879 dwarf2_per_objfile->per_bfd->quick_file_names_table
5880 = create_quick_file_names_table
5881 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5882
5883 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5884 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5885 {
5886 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5887
5888 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5889 struct dwarf2_per_cu_quick_data);
5890 }
5891
5892 /* Return 1 so that gdb sees the "quick" functions. However,
5893 these functions will be no-ops because we will have expanded
5894 all symtabs. */
5895 *index_kind = dw_index_kind::GDB_INDEX;
5896 return true;
5897 }
5898
5899 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5900 {
5901 *index_kind = dw_index_kind::DEBUG_NAMES;
5902 return true;
5903 }
5904
5905 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5906 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5907 get_gdb_index_contents_from_section<dwz_file>))
5908 {
5909 *index_kind = dw_index_kind::GDB_INDEX;
5910 return true;
5911 }
5912
5913 /* ... otherwise, try to find the index in the index cache. */
5914 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5915 get_gdb_index_contents_from_cache,
5916 get_gdb_index_contents_from_cache_dwz))
5917 {
5918 global_index_cache.hit ();
5919 *index_kind = dw_index_kind::GDB_INDEX;
5920 return true;
5921 }
5922
5923 global_index_cache.miss ();
5924 return false;
5925 }
5926
5927 \f
5928
5929 /* Build a partial symbol table. */
5930
5931 void
5932 dwarf2_build_psymtabs (struct objfile *objfile)
5933 {
5934 struct dwarf2_per_objfile *dwarf2_per_objfile
5935 = get_dwarf2_per_objfile (objfile);
5936
5937 init_psymbol_list (objfile, 1024);
5938
5939 try
5940 {
5941 /* This isn't really ideal: all the data we allocate on the
5942 objfile's obstack is still uselessly kept around. However,
5943 freeing it seems unsafe. */
5944 psymtab_discarder psymtabs (objfile);
5945 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5946 psymtabs.keep ();
5947
5948 /* (maybe) store an index in the cache. */
5949 global_index_cache.store (dwarf2_per_objfile);
5950 }
5951 catch (const gdb_exception_error &except)
5952 {
5953 exception_print (gdb_stderr, except);
5954 }
5955 }
5956
5957 /* Find the base address of the compilation unit for range lists and
5958 location lists. It will normally be specified by DW_AT_low_pc.
5959 In DWARF-3 draft 4, the base address could be overridden by
5960 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5961 compilation units with discontinuous ranges. */
5962
5963 static void
5964 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5965 {
5966 struct attribute *attr;
5967
5968 cu->base_address.reset ();
5969
5970 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5971 if (attr != nullptr)
5972 cu->base_address = attr->value_as_address ();
5973 else
5974 {
5975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5976 if (attr != nullptr)
5977 cu->base_address = attr->value_as_address ();
5978 }
5979 }
5980
5981 /* Helper function that returns the proper abbrev section for
5982 THIS_CU. */
5983
5984 static struct dwarf2_section_info *
5985 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5986 {
5987 struct dwarf2_section_info *abbrev;
5988 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5989
5990 if (this_cu->is_dwz)
5991 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5992 else
5993 abbrev = &dwarf2_per_objfile->per_bfd->abbrev;
5994
5995 return abbrev;
5996 }
5997
5998 /* Fetch the abbreviation table offset from a comp or type unit header. */
5999
6000 static sect_offset
6001 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6002 struct dwarf2_section_info *section,
6003 sect_offset sect_off)
6004 {
6005 bfd *abfd = section->get_bfd_owner ();
6006 const gdb_byte *info_ptr;
6007 unsigned int initial_length_size, offset_size;
6008 uint16_t version;
6009
6010 section->read (dwarf2_per_objfile->objfile);
6011 info_ptr = section->buffer + to_underlying (sect_off);
6012 read_initial_length (abfd, info_ptr, &initial_length_size);
6013 offset_size = initial_length_size == 4 ? 4 : 8;
6014 info_ptr += initial_length_size;
6015
6016 version = read_2_bytes (abfd, info_ptr);
6017 info_ptr += 2;
6018 if (version >= 5)
6019 {
6020 /* Skip unit type and address size. */
6021 info_ptr += 2;
6022 }
6023
6024 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6025 }
6026
6027 /* A partial symtab that is used only for include files. */
6028 struct dwarf2_include_psymtab : public partial_symtab
6029 {
6030 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6031 : partial_symtab (filename, objfile)
6032 {
6033 }
6034
6035 void read_symtab (struct objfile *objfile) override
6036 {
6037 /* It's an include file, no symbols to read for it.
6038 Everything is in the includer symtab. */
6039
6040 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6041 expansion of the includer psymtab. We use the dependencies[0] field to
6042 model the includer. But if we go the regular route of calling
6043 expand_psymtab here, and having expand_psymtab call expand_dependencies
6044 to expand the includer, we'll only use expand_psymtab on the includer
6045 (making it a non-toplevel psymtab), while if we expand the includer via
6046 another path, we'll use read_symtab (making it a toplevel psymtab).
6047 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6048 psymtab, and trigger read_symtab on the includer here directly. */
6049 includer ()->read_symtab (objfile);
6050 }
6051
6052 void expand_psymtab (struct objfile *objfile) override
6053 {
6054 /* This is not called by read_symtab, and should not be called by any
6055 expand_dependencies. */
6056 gdb_assert (false);
6057 }
6058
6059 bool readin_p (struct objfile *objfile) const override
6060 {
6061 return includer ()->readin_p (objfile);
6062 }
6063
6064 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6065 {
6066 return nullptr;
6067 }
6068
6069 private:
6070 partial_symtab *includer () const
6071 {
6072 /* An include psymtab has exactly one dependency: the psymtab that
6073 includes it. */
6074 gdb_assert (this->number_of_dependencies == 1);
6075 return this->dependencies[0];
6076 }
6077 };
6078
6079 /* Allocate a new partial symtab for file named NAME and mark this new
6080 partial symtab as being an include of PST. */
6081
6082 static void
6083 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6084 struct objfile *objfile)
6085 {
6086 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6087
6088 if (!IS_ABSOLUTE_PATH (subpst->filename))
6089 subpst->dirname = pst->dirname;
6090
6091 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6092 subpst->dependencies[0] = pst;
6093 subpst->number_of_dependencies = 1;
6094 }
6095
6096 /* Read the Line Number Program data and extract the list of files
6097 included by the source file represented by PST. Build an include
6098 partial symtab for each of these included files. */
6099
6100 static void
6101 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6102 struct die_info *die,
6103 dwarf2_psymtab *pst)
6104 {
6105 line_header_up lh;
6106 struct attribute *attr;
6107
6108 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6109 if (attr != nullptr)
6110 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6111 if (lh == NULL)
6112 return; /* No linetable, so no includes. */
6113
6114 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6115 that we pass in the raw text_low here; that is ok because we're
6116 only decoding the line table to make include partial symtabs, and
6117 so the addresses aren't really used. */
6118 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6119 pst->raw_text_low (), 1);
6120 }
6121
6122 static hashval_t
6123 hash_signatured_type (const void *item)
6124 {
6125 const struct signatured_type *sig_type
6126 = (const struct signatured_type *) item;
6127
6128 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6129 return sig_type->signature;
6130 }
6131
6132 static int
6133 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6134 {
6135 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6136 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6137
6138 return lhs->signature == rhs->signature;
6139 }
6140
6141 /* Allocate a hash table for signatured types. */
6142
6143 static htab_up
6144 allocate_signatured_type_table ()
6145 {
6146 return htab_up (htab_create_alloc (41,
6147 hash_signatured_type,
6148 eq_signatured_type,
6149 NULL, xcalloc, xfree));
6150 }
6151
6152 /* A helper function to add a signatured type CU to a table. */
6153
6154 static int
6155 add_signatured_type_cu_to_table (void **slot, void *datum)
6156 {
6157 struct signatured_type *sigt = (struct signatured_type *) *slot;
6158 std::vector<signatured_type *> *all_type_units
6159 = (std::vector<signatured_type *> *) datum;
6160
6161 all_type_units->push_back (sigt);
6162
6163 return 1;
6164 }
6165
6166 /* A helper for create_debug_types_hash_table. Read types from SECTION
6167 and fill them into TYPES_HTAB. It will process only type units,
6168 therefore DW_UT_type. */
6169
6170 static void
6171 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6172 struct dwo_file *dwo_file,
6173 dwarf2_section_info *section, htab_up &types_htab,
6174 rcuh_kind section_kind)
6175 {
6176 struct objfile *objfile = dwarf2_per_objfile->objfile;
6177 struct dwarf2_section_info *abbrev_section;
6178 bfd *abfd;
6179 const gdb_byte *info_ptr, *end_ptr;
6180
6181 abbrev_section = (dwo_file != NULL
6182 ? &dwo_file->sections.abbrev
6183 : &dwarf2_per_objfile->per_bfd->abbrev);
6184
6185 if (dwarf_read_debug)
6186 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6187 section->get_name (),
6188 abbrev_section->get_file_name ());
6189
6190 section->read (objfile);
6191 info_ptr = section->buffer;
6192
6193 if (info_ptr == NULL)
6194 return;
6195
6196 /* We can't set abfd until now because the section may be empty or
6197 not present, in which case the bfd is unknown. */
6198 abfd = section->get_bfd_owner ();
6199
6200 /* We don't use cutu_reader here because we don't need to read
6201 any dies: the signature is in the header. */
6202
6203 end_ptr = info_ptr + section->size;
6204 while (info_ptr < end_ptr)
6205 {
6206 struct signatured_type *sig_type;
6207 struct dwo_unit *dwo_tu;
6208 void **slot;
6209 const gdb_byte *ptr = info_ptr;
6210 struct comp_unit_head header;
6211 unsigned int length;
6212
6213 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6214
6215 /* Initialize it due to a false compiler warning. */
6216 header.signature = -1;
6217 header.type_cu_offset_in_tu = (cu_offset) -1;
6218
6219 /* We need to read the type's signature in order to build the hash
6220 table, but we don't need anything else just yet. */
6221
6222 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6223 abbrev_section, ptr, section_kind);
6224
6225 length = header.get_length ();
6226
6227 /* Skip dummy type units. */
6228 if (ptr >= info_ptr + length
6229 || peek_abbrev_code (abfd, ptr) == 0
6230 || header.unit_type != DW_UT_type)
6231 {
6232 info_ptr += length;
6233 continue;
6234 }
6235
6236 if (types_htab == NULL)
6237 {
6238 if (dwo_file)
6239 types_htab = allocate_dwo_unit_table ();
6240 else
6241 types_htab = allocate_signatured_type_table ();
6242 }
6243
6244 if (dwo_file)
6245 {
6246 sig_type = NULL;
6247 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6248 struct dwo_unit);
6249 dwo_tu->dwo_file = dwo_file;
6250 dwo_tu->signature = header.signature;
6251 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6252 dwo_tu->section = section;
6253 dwo_tu->sect_off = sect_off;
6254 dwo_tu->length = length;
6255 }
6256 else
6257 {
6258 /* N.B.: type_offset is not usable if this type uses a DWO file.
6259 The real type_offset is in the DWO file. */
6260 dwo_tu = NULL;
6261 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6262 sig_type->signature = header.signature;
6263 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6264 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6265 sig_type->per_cu.is_debug_types = 1;
6266 sig_type->per_cu.section = section;
6267 sig_type->per_cu.sect_off = sect_off;
6268 sig_type->per_cu.length = length;
6269 }
6270
6271 slot = htab_find_slot (types_htab.get (),
6272 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6273 INSERT);
6274 gdb_assert (slot != NULL);
6275 if (*slot != NULL)
6276 {
6277 sect_offset dup_sect_off;
6278
6279 if (dwo_file)
6280 {
6281 const struct dwo_unit *dup_tu
6282 = (const struct dwo_unit *) *slot;
6283
6284 dup_sect_off = dup_tu->sect_off;
6285 }
6286 else
6287 {
6288 const struct signatured_type *dup_tu
6289 = (const struct signatured_type *) *slot;
6290
6291 dup_sect_off = dup_tu->per_cu.sect_off;
6292 }
6293
6294 complaint (_("debug type entry at offset %s is duplicate to"
6295 " the entry at offset %s, signature %s"),
6296 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6297 hex_string (header.signature));
6298 }
6299 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6300
6301 if (dwarf_read_debug > 1)
6302 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6303 sect_offset_str (sect_off),
6304 hex_string (header.signature));
6305
6306 info_ptr += length;
6307 }
6308 }
6309
6310 /* Create the hash table of all entries in the .debug_types
6311 (or .debug_types.dwo) section(s).
6312 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6313 otherwise it is NULL.
6314
6315 The result is a pointer to the hash table or NULL if there are no types.
6316
6317 Note: This function processes DWO files only, not DWP files. */
6318
6319 static void
6320 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6321 struct dwo_file *dwo_file,
6322 gdb::array_view<dwarf2_section_info> type_sections,
6323 htab_up &types_htab)
6324 {
6325 for (dwarf2_section_info &section : type_sections)
6326 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6327 types_htab, rcuh_kind::TYPE);
6328 }
6329
6330 /* Create the hash table of all entries in the .debug_types section,
6331 and initialize all_type_units.
6332 The result is zero if there is an error (e.g. missing .debug_types section),
6333 otherwise non-zero. */
6334
6335 static int
6336 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6337 {
6338 htab_up types_htab;
6339
6340 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6341 &dwarf2_per_objfile->per_bfd->info, types_htab,
6342 rcuh_kind::COMPILE);
6343 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6344 dwarf2_per_objfile->per_bfd->types, types_htab);
6345 if (types_htab == NULL)
6346 {
6347 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6348 return 0;
6349 }
6350
6351 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6352
6353 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6354 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6355 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6356
6357 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6358 add_signatured_type_cu_to_table,
6359 &dwarf2_per_objfile->per_bfd->all_type_units);
6360
6361 return 1;
6362 }
6363
6364 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6365 If SLOT is non-NULL, it is the entry to use in the hash table.
6366 Otherwise we find one. */
6367
6368 static struct signatured_type *
6369 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6370 void **slot)
6371 {
6372 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6373 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6374 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6375
6376 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6377
6378 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6379 sig_type->signature = sig;
6380 sig_type->per_cu.is_debug_types = 1;
6381 if (dwarf2_per_objfile->per_bfd->using_index)
6382 {
6383 sig_type->per_cu.v.quick =
6384 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6385 struct dwarf2_per_cu_quick_data);
6386 }
6387
6388 if (slot == NULL)
6389 {
6390 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6391 sig_type, INSERT);
6392 }
6393 gdb_assert (*slot == NULL);
6394 *slot = sig_type;
6395 /* The rest of sig_type must be filled in by the caller. */
6396 return sig_type;
6397 }
6398
6399 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6400 Fill in SIG_ENTRY with DWO_ENTRY. */
6401
6402 static void
6403 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6404 struct signatured_type *sig_entry,
6405 struct dwo_unit *dwo_entry)
6406 {
6407 /* Make sure we're not clobbering something we don't expect to. */
6408 gdb_assert (! sig_entry->per_cu.queued);
6409 gdb_assert (sig_entry->per_cu.cu == NULL);
6410 if (dwarf2_per_objfile->per_bfd->using_index)
6411 {
6412 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6413 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6414 }
6415 else
6416 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6417 gdb_assert (sig_entry->signature == dwo_entry->signature);
6418 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6419 gdb_assert (sig_entry->type_unit_group == NULL);
6420 gdb_assert (sig_entry->dwo_unit == NULL);
6421
6422 sig_entry->per_cu.section = dwo_entry->section;
6423 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6424 sig_entry->per_cu.length = dwo_entry->length;
6425 sig_entry->per_cu.reading_dwo_directly = 1;
6426 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6427 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6428 sig_entry->dwo_unit = dwo_entry;
6429 }
6430
6431 /* Subroutine of lookup_signatured_type.
6432 If we haven't read the TU yet, create the signatured_type data structure
6433 for a TU to be read in directly from a DWO file, bypassing the stub.
6434 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6435 using .gdb_index, then when reading a CU we want to stay in the DWO file
6436 containing that CU. Otherwise we could end up reading several other DWO
6437 files (due to comdat folding) to process the transitive closure of all the
6438 mentioned TUs, and that can be slow. The current DWO file will have every
6439 type signature that it needs.
6440 We only do this for .gdb_index because in the psymtab case we already have
6441 to read all the DWOs to build the type unit groups. */
6442
6443 static struct signatured_type *
6444 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6445 {
6446 struct dwarf2_per_objfile *dwarf2_per_objfile
6447 = cu->per_cu->dwarf2_per_objfile;
6448 struct dwo_file *dwo_file;
6449 struct dwo_unit find_dwo_entry, *dwo_entry;
6450 struct signatured_type find_sig_entry, *sig_entry;
6451 void **slot;
6452
6453 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6454
6455 /* If TU skeletons have been removed then we may not have read in any
6456 TUs yet. */
6457 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6458 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6459
6460 /* We only ever need to read in one copy of a signatured type.
6461 Use the global signatured_types array to do our own comdat-folding
6462 of types. If this is the first time we're reading this TU, and
6463 the TU has an entry in .gdb_index, replace the recorded data from
6464 .gdb_index with this TU. */
6465
6466 find_sig_entry.signature = sig;
6467 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6468 &find_sig_entry, INSERT);
6469 sig_entry = (struct signatured_type *) *slot;
6470
6471 /* We can get here with the TU already read, *or* in the process of being
6472 read. Don't reassign the global entry to point to this DWO if that's
6473 the case. Also note that if the TU is already being read, it may not
6474 have come from a DWO, the program may be a mix of Fission-compiled
6475 code and non-Fission-compiled code. */
6476
6477 /* Have we already tried to read this TU?
6478 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6479 needn't exist in the global table yet). */
6480 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6481 return sig_entry;
6482
6483 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6484 dwo_unit of the TU itself. */
6485 dwo_file = cu->dwo_unit->dwo_file;
6486
6487 /* Ok, this is the first time we're reading this TU. */
6488 if (dwo_file->tus == NULL)
6489 return NULL;
6490 find_dwo_entry.signature = sig;
6491 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6492 &find_dwo_entry);
6493 if (dwo_entry == NULL)
6494 return NULL;
6495
6496 /* If the global table doesn't have an entry for this TU, add one. */
6497 if (sig_entry == NULL)
6498 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6499
6500 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6501 sig_entry->per_cu.tu_read = 1;
6502 return sig_entry;
6503 }
6504
6505 /* Subroutine of lookup_signatured_type.
6506 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6507 then try the DWP file. If the TU stub (skeleton) has been removed then
6508 it won't be in .gdb_index. */
6509
6510 static struct signatured_type *
6511 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6512 {
6513 struct dwarf2_per_objfile *dwarf2_per_objfile
6514 = cu->per_cu->dwarf2_per_objfile;
6515 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6516 struct dwo_unit *dwo_entry;
6517 struct signatured_type find_sig_entry, *sig_entry;
6518 void **slot;
6519
6520 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6521 gdb_assert (dwp_file != NULL);
6522
6523 /* If TU skeletons have been removed then we may not have read in any
6524 TUs yet. */
6525 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6526 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6527
6528 find_sig_entry.signature = sig;
6529 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6530 &find_sig_entry, INSERT);
6531 sig_entry = (struct signatured_type *) *slot;
6532
6533 /* Have we already tried to read this TU?
6534 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6535 needn't exist in the global table yet). */
6536 if (sig_entry != NULL)
6537 return sig_entry;
6538
6539 if (dwp_file->tus == NULL)
6540 return NULL;
6541 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6542 sig, 1 /* is_debug_types */);
6543 if (dwo_entry == NULL)
6544 return NULL;
6545
6546 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6547 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6548
6549 return sig_entry;
6550 }
6551
6552 /* Lookup a signature based type for DW_FORM_ref_sig8.
6553 Returns NULL if signature SIG is not present in the table.
6554 It is up to the caller to complain about this. */
6555
6556 static struct signatured_type *
6557 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6558 {
6559 struct dwarf2_per_objfile *dwarf2_per_objfile
6560 = cu->per_cu->dwarf2_per_objfile;
6561
6562 if (cu->dwo_unit
6563 && dwarf2_per_objfile->per_bfd->using_index)
6564 {
6565 /* We're in a DWO/DWP file, and we're using .gdb_index.
6566 These cases require special processing. */
6567 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6568 return lookup_dwo_signatured_type (cu, sig);
6569 else
6570 return lookup_dwp_signatured_type (cu, sig);
6571 }
6572 else
6573 {
6574 struct signatured_type find_entry, *entry;
6575
6576 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6577 return NULL;
6578 find_entry.signature = sig;
6579 entry = ((struct signatured_type *)
6580 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6581 &find_entry));
6582 return entry;
6583 }
6584 }
6585
6586 /* Low level DIE reading support. */
6587
6588 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6589
6590 static void
6591 init_cu_die_reader (struct die_reader_specs *reader,
6592 struct dwarf2_cu *cu,
6593 struct dwarf2_section_info *section,
6594 struct dwo_file *dwo_file,
6595 struct abbrev_table *abbrev_table)
6596 {
6597 gdb_assert (section->readin && section->buffer != NULL);
6598 reader->abfd = section->get_bfd_owner ();
6599 reader->cu = cu;
6600 reader->dwo_file = dwo_file;
6601 reader->die_section = section;
6602 reader->buffer = section->buffer;
6603 reader->buffer_end = section->buffer + section->size;
6604 reader->abbrev_table = abbrev_table;
6605 }
6606
6607 /* Subroutine of cutu_reader to simplify it.
6608 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6609 There's just a lot of work to do, and cutu_reader is big enough
6610 already.
6611
6612 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6613 from it to the DIE in the DWO. If NULL we are skipping the stub.
6614 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6615 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6616 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6617 STUB_COMP_DIR may be non-NULL.
6618 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6619 are filled in with the info of the DIE from the DWO file.
6620 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6621 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6622 kept around for at least as long as *RESULT_READER.
6623
6624 The result is non-zero if a valid (non-dummy) DIE was found. */
6625
6626 static int
6627 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6628 struct dwo_unit *dwo_unit,
6629 struct die_info *stub_comp_unit_die,
6630 const char *stub_comp_dir,
6631 struct die_reader_specs *result_reader,
6632 const gdb_byte **result_info_ptr,
6633 struct die_info **result_comp_unit_die,
6634 abbrev_table_up *result_dwo_abbrev_table)
6635 {
6636 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6637 struct objfile *objfile = dwarf2_per_objfile->objfile;
6638 struct dwarf2_cu *cu = this_cu->cu;
6639 bfd *abfd;
6640 const gdb_byte *begin_info_ptr, *info_ptr;
6641 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6642 int i,num_extra_attrs;
6643 struct dwarf2_section_info *dwo_abbrev_section;
6644 struct die_info *comp_unit_die;
6645
6646 /* At most one of these may be provided. */
6647 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6648
6649 /* These attributes aren't processed until later:
6650 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6651 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6652 referenced later. However, these attributes are found in the stub
6653 which we won't have later. In order to not impose this complication
6654 on the rest of the code, we read them here and copy them to the
6655 DWO CU/TU die. */
6656
6657 stmt_list = NULL;
6658 low_pc = NULL;
6659 high_pc = NULL;
6660 ranges = NULL;
6661 comp_dir = NULL;
6662
6663 if (stub_comp_unit_die != NULL)
6664 {
6665 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6666 DWO file. */
6667 if (! this_cu->is_debug_types)
6668 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6669 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6670 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6671 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6672 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6673
6674 cu->addr_base = stub_comp_unit_die->addr_base ();
6675
6676 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6677 here (if needed). We need the value before we can process
6678 DW_AT_ranges. */
6679 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6680 }
6681 else if (stub_comp_dir != NULL)
6682 {
6683 /* Reconstruct the comp_dir attribute to simplify the code below. */
6684 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6685 comp_dir->name = DW_AT_comp_dir;
6686 comp_dir->form = DW_FORM_string;
6687 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6688 DW_STRING (comp_dir) = stub_comp_dir;
6689 }
6690
6691 /* Set up for reading the DWO CU/TU. */
6692 cu->dwo_unit = dwo_unit;
6693 dwarf2_section_info *section = dwo_unit->section;
6694 section->read (objfile);
6695 abfd = section->get_bfd_owner ();
6696 begin_info_ptr = info_ptr = (section->buffer
6697 + to_underlying (dwo_unit->sect_off));
6698 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6699
6700 if (this_cu->is_debug_types)
6701 {
6702 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6703
6704 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6705 &cu->header, section,
6706 dwo_abbrev_section,
6707 info_ptr, rcuh_kind::TYPE);
6708 /* This is not an assert because it can be caused by bad debug info. */
6709 if (sig_type->signature != cu->header.signature)
6710 {
6711 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6712 " TU at offset %s [in module %s]"),
6713 hex_string (sig_type->signature),
6714 hex_string (cu->header.signature),
6715 sect_offset_str (dwo_unit->sect_off),
6716 bfd_get_filename (abfd));
6717 }
6718 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6719 /* For DWOs coming from DWP files, we don't know the CU length
6720 nor the type's offset in the TU until now. */
6721 dwo_unit->length = cu->header.get_length ();
6722 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6723
6724 /* Establish the type offset that can be used to lookup the type.
6725 For DWO files, we don't know it until now. */
6726 sig_type->type_offset_in_section
6727 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6728 }
6729 else
6730 {
6731 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6732 &cu->header, section,
6733 dwo_abbrev_section,
6734 info_ptr, rcuh_kind::COMPILE);
6735 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6736 /* For DWOs coming from DWP files, we don't know the CU length
6737 until now. */
6738 dwo_unit->length = cu->header.get_length ();
6739 }
6740
6741 *result_dwo_abbrev_table
6742 = abbrev_table::read (objfile, dwo_abbrev_section,
6743 cu->header.abbrev_sect_off);
6744 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6745 result_dwo_abbrev_table->get ());
6746
6747 /* Read in the die, but leave space to copy over the attributes
6748 from the stub. This has the benefit of simplifying the rest of
6749 the code - all the work to maintain the illusion of a single
6750 DW_TAG_{compile,type}_unit DIE is done here. */
6751 num_extra_attrs = ((stmt_list != NULL)
6752 + (low_pc != NULL)
6753 + (high_pc != NULL)
6754 + (ranges != NULL)
6755 + (comp_dir != NULL));
6756 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6757 num_extra_attrs);
6758
6759 /* Copy over the attributes from the stub to the DIE we just read in. */
6760 comp_unit_die = *result_comp_unit_die;
6761 i = comp_unit_die->num_attrs;
6762 if (stmt_list != NULL)
6763 comp_unit_die->attrs[i++] = *stmt_list;
6764 if (low_pc != NULL)
6765 comp_unit_die->attrs[i++] = *low_pc;
6766 if (high_pc != NULL)
6767 comp_unit_die->attrs[i++] = *high_pc;
6768 if (ranges != NULL)
6769 comp_unit_die->attrs[i++] = *ranges;
6770 if (comp_dir != NULL)
6771 comp_unit_die->attrs[i++] = *comp_dir;
6772 comp_unit_die->num_attrs += num_extra_attrs;
6773
6774 if (dwarf_die_debug)
6775 {
6776 fprintf_unfiltered (gdb_stdlog,
6777 "Read die from %s@0x%x of %s:\n",
6778 section->get_name (),
6779 (unsigned) (begin_info_ptr - section->buffer),
6780 bfd_get_filename (abfd));
6781 dump_die (comp_unit_die, dwarf_die_debug);
6782 }
6783
6784 /* Skip dummy compilation units. */
6785 if (info_ptr >= begin_info_ptr + dwo_unit->length
6786 || peek_abbrev_code (abfd, info_ptr) == 0)
6787 return 0;
6788
6789 *result_info_ptr = info_ptr;
6790 return 1;
6791 }
6792
6793 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6794 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6795 signature is part of the header. */
6796 static gdb::optional<ULONGEST>
6797 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6798 {
6799 if (cu->header.version >= 5)
6800 return cu->header.signature;
6801 struct attribute *attr;
6802 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6803 if (attr == nullptr)
6804 return gdb::optional<ULONGEST> ();
6805 return DW_UNSND (attr);
6806 }
6807
6808 /* Subroutine of cutu_reader to simplify it.
6809 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6810 Returns NULL if the specified DWO unit cannot be found. */
6811
6812 static struct dwo_unit *
6813 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6814 struct die_info *comp_unit_die,
6815 const char *dwo_name)
6816 {
6817 struct dwarf2_cu *cu = this_cu->cu;
6818 struct dwo_unit *dwo_unit;
6819 const char *comp_dir;
6820
6821 gdb_assert (cu != NULL);
6822
6823 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6824 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6825 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6826
6827 if (this_cu->is_debug_types)
6828 {
6829 struct signatured_type *sig_type;
6830
6831 /* Since this_cu is the first member of struct signatured_type,
6832 we can go from a pointer to one to a pointer to the other. */
6833 sig_type = (struct signatured_type *) this_cu;
6834 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6835 }
6836 else
6837 {
6838 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6839 if (!signature.has_value ())
6840 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6841 " [in module %s]"),
6842 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6843 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6844 *signature);
6845 }
6846
6847 return dwo_unit;
6848 }
6849
6850 /* Subroutine of cutu_reader to simplify it.
6851 See it for a description of the parameters.
6852 Read a TU directly from a DWO file, bypassing the stub. */
6853
6854 void
6855 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6856 int use_existing_cu)
6857 {
6858 struct signatured_type *sig_type;
6859
6860 /* Verify we can do the following downcast, and that we have the
6861 data we need. */
6862 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6863 sig_type = (struct signatured_type *) this_cu;
6864 gdb_assert (sig_type->dwo_unit != NULL);
6865
6866 if (use_existing_cu && this_cu->cu != NULL)
6867 {
6868 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6869 /* There's no need to do the rereading_dwo_cu handling that
6870 cutu_reader does since we don't read the stub. */
6871 }
6872 else
6873 {
6874 /* If !use_existing_cu, this_cu->cu must be NULL. */
6875 gdb_assert (this_cu->cu == NULL);
6876 m_new_cu.reset (new dwarf2_cu (this_cu));
6877 }
6878
6879 /* A future optimization, if needed, would be to use an existing
6880 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6881 could share abbrev tables. */
6882
6883 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6884 NULL /* stub_comp_unit_die */,
6885 sig_type->dwo_unit->dwo_file->comp_dir,
6886 this, &info_ptr,
6887 &comp_unit_die,
6888 &m_dwo_abbrev_table) == 0)
6889 {
6890 /* Dummy die. */
6891 dummy_p = true;
6892 }
6893 }
6894
6895 /* Initialize a CU (or TU) and read its DIEs.
6896 If the CU defers to a DWO file, read the DWO file as well.
6897
6898 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6899 Otherwise the table specified in the comp unit header is read in and used.
6900 This is an optimization for when we already have the abbrev table.
6901
6902 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6903 Otherwise, a new CU is allocated with xmalloc. */
6904
6905 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6906 struct abbrev_table *abbrev_table,
6907 int use_existing_cu,
6908 bool skip_partial)
6909 : die_reader_specs {},
6910 m_this_cu (this_cu)
6911 {
6912 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6913 struct objfile *objfile = dwarf2_per_objfile->objfile;
6914 struct dwarf2_section_info *section = this_cu->section;
6915 bfd *abfd = section->get_bfd_owner ();
6916 struct dwarf2_cu *cu;
6917 const gdb_byte *begin_info_ptr;
6918 struct signatured_type *sig_type = NULL;
6919 struct dwarf2_section_info *abbrev_section;
6920 /* Non-zero if CU currently points to a DWO file and we need to
6921 reread it. When this happens we need to reread the skeleton die
6922 before we can reread the DWO file (this only applies to CUs, not TUs). */
6923 int rereading_dwo_cu = 0;
6924
6925 if (dwarf_die_debug)
6926 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6927 this_cu->is_debug_types ? "type" : "comp",
6928 sect_offset_str (this_cu->sect_off));
6929
6930 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6931 file (instead of going through the stub), short-circuit all of this. */
6932 if (this_cu->reading_dwo_directly)
6933 {
6934 /* Narrow down the scope of possibilities to have to understand. */
6935 gdb_assert (this_cu->is_debug_types);
6936 gdb_assert (abbrev_table == NULL);
6937 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6938 return;
6939 }
6940
6941 /* This is cheap if the section is already read in. */
6942 section->read (objfile);
6943
6944 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6945
6946 abbrev_section = get_abbrev_section_for_cu (this_cu);
6947
6948 if (use_existing_cu && this_cu->cu != NULL)
6949 {
6950 cu = this_cu->cu;
6951 /* If this CU is from a DWO file we need to start over, we need to
6952 refetch the attributes from the skeleton CU.
6953 This could be optimized by retrieving those attributes from when we
6954 were here the first time: the previous comp_unit_die was stored in
6955 comp_unit_obstack. But there's no data yet that we need this
6956 optimization. */
6957 if (cu->dwo_unit != NULL)
6958 rereading_dwo_cu = 1;
6959 }
6960 else
6961 {
6962 /* If !use_existing_cu, this_cu->cu must be NULL. */
6963 gdb_assert (this_cu->cu == NULL);
6964 m_new_cu.reset (new dwarf2_cu (this_cu));
6965 cu = m_new_cu.get ();
6966 }
6967
6968 /* Get the header. */
6969 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6970 {
6971 /* We already have the header, there's no need to read it in again. */
6972 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6973 }
6974 else
6975 {
6976 if (this_cu->is_debug_types)
6977 {
6978 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6979 &cu->header, section,
6980 abbrev_section, info_ptr,
6981 rcuh_kind::TYPE);
6982
6983 /* Since per_cu is the first member of struct signatured_type,
6984 we can go from a pointer to one to a pointer to the other. */
6985 sig_type = (struct signatured_type *) this_cu;
6986 gdb_assert (sig_type->signature == cu->header.signature);
6987 gdb_assert (sig_type->type_offset_in_tu
6988 == cu->header.type_cu_offset_in_tu);
6989 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6990
6991 /* LENGTH has not been set yet for type units if we're
6992 using .gdb_index. */
6993 this_cu->length = cu->header.get_length ();
6994
6995 /* Establish the type offset that can be used to lookup the type. */
6996 sig_type->type_offset_in_section =
6997 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6998
6999 this_cu->dwarf_version = cu->header.version;
7000 }
7001 else
7002 {
7003 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7004 &cu->header, section,
7005 abbrev_section,
7006 info_ptr,
7007 rcuh_kind::COMPILE);
7008
7009 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7010 if (this_cu->length == 0)
7011 this_cu->length = cu->header.get_length ();
7012 else
7013 gdb_assert (this_cu->length == cu->header.get_length ());
7014 this_cu->dwarf_version = cu->header.version;
7015 }
7016 }
7017
7018 /* Skip dummy compilation units. */
7019 if (info_ptr >= begin_info_ptr + this_cu->length
7020 || peek_abbrev_code (abfd, info_ptr) == 0)
7021 {
7022 dummy_p = true;
7023 return;
7024 }
7025
7026 /* If we don't have them yet, read the abbrevs for this compilation unit.
7027 And if we need to read them now, make sure they're freed when we're
7028 done. */
7029 if (abbrev_table != NULL)
7030 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7031 else
7032 {
7033 m_abbrev_table_holder
7034 = abbrev_table::read (objfile, abbrev_section,
7035 cu->header.abbrev_sect_off);
7036 abbrev_table = m_abbrev_table_holder.get ();
7037 }
7038
7039 /* Read the top level CU/TU die. */
7040 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7041 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7042
7043 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7044 {
7045 dummy_p = true;
7046 return;
7047 }
7048
7049 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7050 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7051 table from the DWO file and pass the ownership over to us. It will be
7052 referenced from READER, so we must make sure to free it after we're done
7053 with READER.
7054
7055 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7056 DWO CU, that this test will fail (the attribute will not be present). */
7057 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7058 if (dwo_name != nullptr)
7059 {
7060 struct dwo_unit *dwo_unit;
7061 struct die_info *dwo_comp_unit_die;
7062
7063 if (comp_unit_die->has_children)
7064 {
7065 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7066 " has children (offset %s) [in module %s]"),
7067 sect_offset_str (this_cu->sect_off),
7068 bfd_get_filename (abfd));
7069 }
7070 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7071 if (dwo_unit != NULL)
7072 {
7073 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7074 comp_unit_die, NULL,
7075 this, &info_ptr,
7076 &dwo_comp_unit_die,
7077 &m_dwo_abbrev_table) == 0)
7078 {
7079 /* Dummy die. */
7080 dummy_p = true;
7081 return;
7082 }
7083 comp_unit_die = dwo_comp_unit_die;
7084 }
7085 else
7086 {
7087 /* Yikes, we couldn't find the rest of the DIE, we only have
7088 the stub. A complaint has already been logged. There's
7089 not much more we can do except pass on the stub DIE to
7090 die_reader_func. We don't want to throw an error on bad
7091 debug info. */
7092 }
7093 }
7094 }
7095
7096 void
7097 cutu_reader::keep ()
7098 {
7099 /* Done, clean up. */
7100 gdb_assert (!dummy_p);
7101 if (m_new_cu != NULL)
7102 {
7103 struct dwarf2_per_objfile *dwarf2_per_objfile
7104 = m_this_cu->dwarf2_per_objfile;
7105 /* Link this CU into read_in_chain. */
7106 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7107 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7108 /* The chain owns it now. */
7109 m_new_cu.release ();
7110 }
7111 }
7112
7113 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7114 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7115 assumed to have already done the lookup to find the DWO file).
7116
7117 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7118 THIS_CU->is_debug_types, but nothing else.
7119
7120 We fill in THIS_CU->length.
7121
7122 THIS_CU->cu is always freed when done.
7123 This is done in order to not leave THIS_CU->cu in a state where we have
7124 to care whether it refers to the "main" CU or the DWO CU.
7125
7126 When parent_cu is passed, it is used to provide a default value for
7127 str_offsets_base and addr_base from the parent. */
7128
7129 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7130 struct dwarf2_cu *parent_cu,
7131 struct dwo_file *dwo_file)
7132 : die_reader_specs {},
7133 m_this_cu (this_cu)
7134 {
7135 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7136 struct objfile *objfile = dwarf2_per_objfile->objfile;
7137 struct dwarf2_section_info *section = this_cu->section;
7138 bfd *abfd = section->get_bfd_owner ();
7139 struct dwarf2_section_info *abbrev_section;
7140 const gdb_byte *begin_info_ptr, *info_ptr;
7141
7142 if (dwarf_die_debug)
7143 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7144 this_cu->is_debug_types ? "type" : "comp",
7145 sect_offset_str (this_cu->sect_off));
7146
7147 gdb_assert (this_cu->cu == NULL);
7148
7149 abbrev_section = (dwo_file != NULL
7150 ? &dwo_file->sections.abbrev
7151 : get_abbrev_section_for_cu (this_cu));
7152
7153 /* This is cheap if the section is already read in. */
7154 section->read (objfile);
7155
7156 m_new_cu.reset (new dwarf2_cu (this_cu));
7157
7158 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7159 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7160 &m_new_cu->header, section,
7161 abbrev_section, info_ptr,
7162 (this_cu->is_debug_types
7163 ? rcuh_kind::TYPE
7164 : rcuh_kind::COMPILE));
7165
7166 if (parent_cu != nullptr)
7167 {
7168 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7169 m_new_cu->addr_base = parent_cu->addr_base;
7170 }
7171 this_cu->length = m_new_cu->header.get_length ();
7172
7173 /* Skip dummy compilation units. */
7174 if (info_ptr >= begin_info_ptr + this_cu->length
7175 || peek_abbrev_code (abfd, info_ptr) == 0)
7176 {
7177 dummy_p = true;
7178 return;
7179 }
7180
7181 m_abbrev_table_holder
7182 = abbrev_table::read (objfile, abbrev_section,
7183 m_new_cu->header.abbrev_sect_off);
7184
7185 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7186 m_abbrev_table_holder.get ());
7187 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7188 }
7189
7190 \f
7191 /* Type Unit Groups.
7192
7193 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7194 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7195 so that all types coming from the same compilation (.o file) are grouped
7196 together. A future step could be to put the types in the same symtab as
7197 the CU the types ultimately came from. */
7198
7199 static hashval_t
7200 hash_type_unit_group (const void *item)
7201 {
7202 const struct type_unit_group *tu_group
7203 = (const struct type_unit_group *) item;
7204
7205 return hash_stmt_list_entry (&tu_group->hash);
7206 }
7207
7208 static int
7209 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7210 {
7211 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7212 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7213
7214 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7215 }
7216
7217 /* Allocate a hash table for type unit groups. */
7218
7219 static htab_up
7220 allocate_type_unit_groups_table ()
7221 {
7222 return htab_up (htab_create_alloc (3,
7223 hash_type_unit_group,
7224 eq_type_unit_group,
7225 NULL, xcalloc, xfree));
7226 }
7227
7228 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7229 partial symtabs. We combine several TUs per psymtab to not let the size
7230 of any one psymtab grow too big. */
7231 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7232 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7233
7234 /* Helper routine for get_type_unit_group.
7235 Create the type_unit_group object used to hold one or more TUs. */
7236
7237 static struct type_unit_group *
7238 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7239 {
7240 struct dwarf2_per_objfile *dwarf2_per_objfile
7241 = cu->per_cu->dwarf2_per_objfile;
7242 struct dwarf2_per_cu_data *per_cu;
7243 struct type_unit_group *tu_group;
7244
7245 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7246 struct type_unit_group);
7247 per_cu = &tu_group->per_cu;
7248 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7249
7250 if (dwarf2_per_objfile->per_bfd->using_index)
7251 {
7252 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7253 struct dwarf2_per_cu_quick_data);
7254 }
7255 else
7256 {
7257 unsigned int line_offset = to_underlying (line_offset_struct);
7258 dwarf2_psymtab *pst;
7259 std::string name;
7260
7261 /* Give the symtab a useful name for debug purposes. */
7262 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7263 name = string_printf ("<type_units_%d>",
7264 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7265 else
7266 name = string_printf ("<type_units_at_0x%x>", line_offset);
7267
7268 pst = create_partial_symtab (per_cu, name.c_str ());
7269 pst->anonymous = true;
7270 }
7271
7272 tu_group->hash.dwo_unit = cu->dwo_unit;
7273 tu_group->hash.line_sect_off = line_offset_struct;
7274
7275 return tu_group;
7276 }
7277
7278 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7279 STMT_LIST is a DW_AT_stmt_list attribute. */
7280
7281 static struct type_unit_group *
7282 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7283 {
7284 struct dwarf2_per_objfile *dwarf2_per_objfile
7285 = cu->per_cu->dwarf2_per_objfile;
7286 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7287 struct type_unit_group *tu_group;
7288 void **slot;
7289 unsigned int line_offset;
7290 struct type_unit_group type_unit_group_for_lookup;
7291
7292 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7293 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7294
7295 /* Do we need to create a new group, or can we use an existing one? */
7296
7297 if (stmt_list)
7298 {
7299 line_offset = DW_UNSND (stmt_list);
7300 ++tu_stats->nr_symtab_sharers;
7301 }
7302 else
7303 {
7304 /* Ugh, no stmt_list. Rare, but we have to handle it.
7305 We can do various things here like create one group per TU or
7306 spread them over multiple groups to split up the expansion work.
7307 To avoid worst case scenarios (too many groups or too large groups)
7308 we, umm, group them in bunches. */
7309 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7310 | (tu_stats->nr_stmt_less_type_units
7311 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7312 ++tu_stats->nr_stmt_less_type_units;
7313 }
7314
7315 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7316 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7317 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7318 &type_unit_group_for_lookup, INSERT);
7319 if (*slot != NULL)
7320 {
7321 tu_group = (struct type_unit_group *) *slot;
7322 gdb_assert (tu_group != NULL);
7323 }
7324 else
7325 {
7326 sect_offset line_offset_struct = (sect_offset) line_offset;
7327 tu_group = create_type_unit_group (cu, line_offset_struct);
7328 *slot = tu_group;
7329 ++tu_stats->nr_symtabs;
7330 }
7331
7332 return tu_group;
7333 }
7334 \f
7335 /* Partial symbol tables. */
7336
7337 /* Create a psymtab named NAME and assign it to PER_CU.
7338
7339 The caller must fill in the following details:
7340 dirname, textlow, texthigh. */
7341
7342 static dwarf2_psymtab *
7343 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7344 {
7345 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7346 dwarf2_psymtab *pst;
7347
7348 pst = new dwarf2_psymtab (name, objfile, per_cu);
7349
7350 pst->psymtabs_addrmap_supported = true;
7351
7352 /* This is the glue that links PST into GDB's symbol API. */
7353 per_cu->v.psymtab = pst;
7354
7355 return pst;
7356 }
7357
7358 /* DIE reader function for process_psymtab_comp_unit. */
7359
7360 static void
7361 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7362 const gdb_byte *info_ptr,
7363 struct die_info *comp_unit_die,
7364 enum language pretend_language)
7365 {
7366 struct dwarf2_cu *cu = reader->cu;
7367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7368 struct gdbarch *gdbarch = objfile->arch ();
7369 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7370 CORE_ADDR baseaddr;
7371 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7372 dwarf2_psymtab *pst;
7373 enum pc_bounds_kind cu_bounds_kind;
7374 const char *filename;
7375
7376 gdb_assert (! per_cu->is_debug_types);
7377
7378 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7379
7380 /* Allocate a new partial symbol table structure. */
7381 gdb::unique_xmalloc_ptr<char> debug_filename;
7382 static const char artificial[] = "<artificial>";
7383 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7384 if (filename == NULL)
7385 filename = "";
7386 else if (strcmp (filename, artificial) == 0)
7387 {
7388 debug_filename.reset (concat (artificial, "@",
7389 sect_offset_str (per_cu->sect_off),
7390 (char *) NULL));
7391 filename = debug_filename.get ();
7392 }
7393
7394 pst = create_partial_symtab (per_cu, filename);
7395
7396 /* This must be done before calling dwarf2_build_include_psymtabs. */
7397 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7398
7399 baseaddr = objfile->text_section_offset ();
7400
7401 dwarf2_find_base_address (comp_unit_die, cu);
7402
7403 /* Possibly set the default values of LOWPC and HIGHPC from
7404 `DW_AT_ranges'. */
7405 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7406 &best_highpc, cu, pst);
7407 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7408 {
7409 CORE_ADDR low
7410 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7411 - baseaddr);
7412 CORE_ADDR high
7413 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7414 - baseaddr - 1);
7415 /* Store the contiguous range if it is not empty; it can be
7416 empty for CUs with no code. */
7417 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7418 low, high, pst);
7419 }
7420
7421 /* Check if comp unit has_children.
7422 If so, read the rest of the partial symbols from this comp unit.
7423 If not, there's no more debug_info for this comp unit. */
7424 if (comp_unit_die->has_children)
7425 {
7426 struct partial_die_info *first_die;
7427 CORE_ADDR lowpc, highpc;
7428
7429 lowpc = ((CORE_ADDR) -1);
7430 highpc = ((CORE_ADDR) 0);
7431
7432 first_die = load_partial_dies (reader, info_ptr, 1);
7433
7434 scan_partial_symbols (first_die, &lowpc, &highpc,
7435 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7436
7437 /* If we didn't find a lowpc, set it to highpc to avoid
7438 complaints from `maint check'. */
7439 if (lowpc == ((CORE_ADDR) -1))
7440 lowpc = highpc;
7441
7442 /* If the compilation unit didn't have an explicit address range,
7443 then use the information extracted from its child dies. */
7444 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7445 {
7446 best_lowpc = lowpc;
7447 best_highpc = highpc;
7448 }
7449 }
7450 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7451 best_lowpc + baseaddr)
7452 - baseaddr);
7453 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7454 best_highpc + baseaddr)
7455 - baseaddr);
7456
7457 end_psymtab_common (objfile, pst);
7458
7459 if (!cu->per_cu->imported_symtabs_empty ())
7460 {
7461 int i;
7462 int len = cu->per_cu->imported_symtabs_size ();
7463
7464 /* Fill in 'dependencies' here; we fill in 'users' in a
7465 post-pass. */
7466 pst->number_of_dependencies = len;
7467 pst->dependencies
7468 = objfile->partial_symtabs->allocate_dependencies (len);
7469 for (i = 0; i < len; ++i)
7470 {
7471 pst->dependencies[i]
7472 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7473 }
7474
7475 cu->per_cu->imported_symtabs_free ();
7476 }
7477
7478 /* Get the list of files included in the current compilation unit,
7479 and build a psymtab for each of them. */
7480 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7481
7482 if (dwarf_read_debug)
7483 fprintf_unfiltered (gdb_stdlog,
7484 "Psymtab for %s unit @%s: %s - %s"
7485 ", %d global, %d static syms\n",
7486 per_cu->is_debug_types ? "type" : "comp",
7487 sect_offset_str (per_cu->sect_off),
7488 paddress (gdbarch, pst->text_low (objfile)),
7489 paddress (gdbarch, pst->text_high (objfile)),
7490 pst->n_global_syms, pst->n_static_syms);
7491 }
7492
7493 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7494 Process compilation unit THIS_CU for a psymtab. */
7495
7496 static void
7497 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7498 bool want_partial_unit,
7499 enum language pretend_language)
7500 {
7501 /* If this compilation unit was already read in, free the
7502 cached copy in order to read it in again. This is
7503 necessary because we skipped some symbols when we first
7504 read in the compilation unit (see load_partial_dies).
7505 This problem could be avoided, but the benefit is unclear. */
7506 if (this_cu->cu != NULL)
7507 free_one_cached_comp_unit (this_cu);
7508
7509 cutu_reader reader (this_cu, NULL, 0, false);
7510
7511 switch (reader.comp_unit_die->tag)
7512 {
7513 case DW_TAG_compile_unit:
7514 this_cu->unit_type = DW_UT_compile;
7515 break;
7516 case DW_TAG_partial_unit:
7517 this_cu->unit_type = DW_UT_partial;
7518 break;
7519 default:
7520 abort ();
7521 }
7522
7523 if (reader.dummy_p)
7524 {
7525 /* Nothing. */
7526 }
7527 else if (this_cu->is_debug_types)
7528 build_type_psymtabs_reader (&reader, reader.info_ptr,
7529 reader.comp_unit_die);
7530 else if (want_partial_unit
7531 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7532 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7533 reader.comp_unit_die,
7534 pretend_language);
7535
7536 this_cu->lang = this_cu->cu->language;
7537
7538 /* Age out any secondary CUs. */
7539 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7540 }
7541
7542 /* Reader function for build_type_psymtabs. */
7543
7544 static void
7545 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7546 const gdb_byte *info_ptr,
7547 struct die_info *type_unit_die)
7548 {
7549 struct dwarf2_per_objfile *dwarf2_per_objfile
7550 = reader->cu->per_cu->dwarf2_per_objfile;
7551 struct objfile *objfile = dwarf2_per_objfile->objfile;
7552 struct dwarf2_cu *cu = reader->cu;
7553 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7554 struct signatured_type *sig_type;
7555 struct type_unit_group *tu_group;
7556 struct attribute *attr;
7557 struct partial_die_info *first_die;
7558 CORE_ADDR lowpc, highpc;
7559 dwarf2_psymtab *pst;
7560
7561 gdb_assert (per_cu->is_debug_types);
7562 sig_type = (struct signatured_type *) per_cu;
7563
7564 if (! type_unit_die->has_children)
7565 return;
7566
7567 attr = type_unit_die->attr (DW_AT_stmt_list);
7568 tu_group = get_type_unit_group (cu, attr);
7569
7570 if (tu_group->tus == nullptr)
7571 tu_group->tus = new std::vector<signatured_type *>;
7572 tu_group->tus->push_back (sig_type);
7573
7574 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7575 pst = create_partial_symtab (per_cu, "");
7576 pst->anonymous = true;
7577
7578 first_die = load_partial_dies (reader, info_ptr, 1);
7579
7580 lowpc = (CORE_ADDR) -1;
7581 highpc = (CORE_ADDR) 0;
7582 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7583
7584 end_psymtab_common (objfile, pst);
7585 }
7586
7587 /* Struct used to sort TUs by their abbreviation table offset. */
7588
7589 struct tu_abbrev_offset
7590 {
7591 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7592 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7593 {}
7594
7595 signatured_type *sig_type;
7596 sect_offset abbrev_offset;
7597 };
7598
7599 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7600
7601 static bool
7602 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7603 const struct tu_abbrev_offset &b)
7604 {
7605 return a.abbrev_offset < b.abbrev_offset;
7606 }
7607
7608 /* Efficiently read all the type units.
7609 This does the bulk of the work for build_type_psymtabs.
7610
7611 The efficiency is because we sort TUs by the abbrev table they use and
7612 only read each abbrev table once. In one program there are 200K TUs
7613 sharing 8K abbrev tables.
7614
7615 The main purpose of this function is to support building the
7616 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7617 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7618 can collapse the search space by grouping them by stmt_list.
7619 The savings can be significant, in the same program from above the 200K TUs
7620 share 8K stmt_list tables.
7621
7622 FUNC is expected to call get_type_unit_group, which will create the
7623 struct type_unit_group if necessary and add it to
7624 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7625
7626 static void
7627 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7628 {
7629 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7630 abbrev_table_up abbrev_table;
7631 sect_offset abbrev_offset;
7632
7633 /* It's up to the caller to not call us multiple times. */
7634 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7635
7636 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7637 return;
7638
7639 /* TUs typically share abbrev tables, and there can be way more TUs than
7640 abbrev tables. Sort by abbrev table to reduce the number of times we
7641 read each abbrev table in.
7642 Alternatives are to punt or to maintain a cache of abbrev tables.
7643 This is simpler and efficient enough for now.
7644
7645 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7646 symtab to use). Typically TUs with the same abbrev offset have the same
7647 stmt_list value too so in practice this should work well.
7648
7649 The basic algorithm here is:
7650
7651 sort TUs by abbrev table
7652 for each TU with same abbrev table:
7653 read abbrev table if first user
7654 read TU top level DIE
7655 [IWBN if DWO skeletons had DW_AT_stmt_list]
7656 call FUNC */
7657
7658 if (dwarf_read_debug)
7659 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7660
7661 /* Sort in a separate table to maintain the order of all_type_units
7662 for .gdb_index: TU indices directly index all_type_units. */
7663 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7664 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7665
7666 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7667 sorted_by_abbrev.emplace_back
7668 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7669 sig_type->per_cu.section,
7670 sig_type->per_cu.sect_off));
7671
7672 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7673 sort_tu_by_abbrev_offset);
7674
7675 abbrev_offset = (sect_offset) ~(unsigned) 0;
7676
7677 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7678 {
7679 /* Switch to the next abbrev table if necessary. */
7680 if (abbrev_table == NULL
7681 || tu.abbrev_offset != abbrev_offset)
7682 {
7683 abbrev_offset = tu.abbrev_offset;
7684 abbrev_table =
7685 abbrev_table::read (dwarf2_per_objfile->objfile,
7686 &dwarf2_per_objfile->per_bfd->abbrev,
7687 abbrev_offset);
7688 ++tu_stats->nr_uniq_abbrev_tables;
7689 }
7690
7691 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7692 0, false);
7693 if (!reader.dummy_p)
7694 build_type_psymtabs_reader (&reader, reader.info_ptr,
7695 reader.comp_unit_die);
7696 }
7697 }
7698
7699 /* Print collected type unit statistics. */
7700
7701 static void
7702 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7703 {
7704 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7705
7706 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7707 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7708 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7709 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7710 tu_stats->nr_uniq_abbrev_tables);
7711 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7712 tu_stats->nr_symtabs);
7713 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7714 tu_stats->nr_symtab_sharers);
7715 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7716 tu_stats->nr_stmt_less_type_units);
7717 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7718 tu_stats->nr_all_type_units_reallocs);
7719 }
7720
7721 /* Traversal function for build_type_psymtabs. */
7722
7723 static int
7724 build_type_psymtab_dependencies (void **slot, void *info)
7725 {
7726 struct dwarf2_per_objfile *dwarf2_per_objfile
7727 = (struct dwarf2_per_objfile *) info;
7728 struct objfile *objfile = dwarf2_per_objfile->objfile;
7729 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7730 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7731 dwarf2_psymtab *pst = per_cu->v.psymtab;
7732 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7733 int i;
7734
7735 gdb_assert (len > 0);
7736 gdb_assert (per_cu->type_unit_group_p ());
7737
7738 pst->number_of_dependencies = len;
7739 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7740 for (i = 0; i < len; ++i)
7741 {
7742 struct signatured_type *iter = tu_group->tus->at (i);
7743 gdb_assert (iter->per_cu.is_debug_types);
7744 pst->dependencies[i] = iter->per_cu.v.psymtab;
7745 iter->type_unit_group = tu_group;
7746 }
7747
7748 delete tu_group->tus;
7749 tu_group->tus = nullptr;
7750
7751 return 1;
7752 }
7753
7754 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7755 Build partial symbol tables for the .debug_types comp-units. */
7756
7757 static void
7758 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7759 {
7760 if (! create_all_type_units (dwarf2_per_objfile))
7761 return;
7762
7763 build_type_psymtabs_1 (dwarf2_per_objfile);
7764 }
7765
7766 /* Traversal function for process_skeletonless_type_unit.
7767 Read a TU in a DWO file and build partial symbols for it. */
7768
7769 static int
7770 process_skeletonless_type_unit (void **slot, void *info)
7771 {
7772 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7773 struct dwarf2_per_objfile *dwarf2_per_objfile
7774 = (struct dwarf2_per_objfile *) info;
7775 struct signatured_type find_entry, *entry;
7776
7777 /* If this TU doesn't exist in the global table, add it and read it in. */
7778
7779 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7780 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7781
7782 find_entry.signature = dwo_unit->signature;
7783 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7784 &find_entry, INSERT);
7785 /* If we've already seen this type there's nothing to do. What's happening
7786 is we're doing our own version of comdat-folding here. */
7787 if (*slot != NULL)
7788 return 1;
7789
7790 /* This does the job that create_all_type_units would have done for
7791 this TU. */
7792 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7793 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7794 *slot = entry;
7795
7796 /* This does the job that build_type_psymtabs_1 would have done. */
7797 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7798 if (!reader.dummy_p)
7799 build_type_psymtabs_reader (&reader, reader.info_ptr,
7800 reader.comp_unit_die);
7801
7802 return 1;
7803 }
7804
7805 /* Traversal function for process_skeletonless_type_units. */
7806
7807 static int
7808 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7809 {
7810 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7811
7812 if (dwo_file->tus != NULL)
7813 htab_traverse_noresize (dwo_file->tus.get (),
7814 process_skeletonless_type_unit, info);
7815
7816 return 1;
7817 }
7818
7819 /* Scan all TUs of DWO files, verifying we've processed them.
7820 This is needed in case a TU was emitted without its skeleton.
7821 Note: This can't be done until we know what all the DWO files are. */
7822
7823 static void
7824 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7825 {
7826 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7827 if (get_dwp_file (dwarf2_per_objfile) == NULL
7828 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7829 {
7830 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7831 process_dwo_file_for_skeletonless_type_units,
7832 dwarf2_per_objfile);
7833 }
7834 }
7835
7836 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7837
7838 static void
7839 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7840 {
7841 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7842 {
7843 dwarf2_psymtab *pst = per_cu->v.psymtab;
7844
7845 if (pst == NULL)
7846 continue;
7847
7848 for (int j = 0; j < pst->number_of_dependencies; ++j)
7849 {
7850 /* Set the 'user' field only if it is not already set. */
7851 if (pst->dependencies[j]->user == NULL)
7852 pst->dependencies[j]->user = pst;
7853 }
7854 }
7855 }
7856
7857 /* Build the partial symbol table by doing a quick pass through the
7858 .debug_info and .debug_abbrev sections. */
7859
7860 static void
7861 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7862 {
7863 struct objfile *objfile = dwarf2_per_objfile->objfile;
7864
7865 if (dwarf_read_debug)
7866 {
7867 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7868 objfile_name (objfile));
7869 }
7870
7871 scoped_restore restore_reading_psyms
7872 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7873 true);
7874
7875 dwarf2_per_objfile->per_bfd->info.read (objfile);
7876
7877 /* Any cached compilation units will be linked by the per-objfile
7878 read_in_chain. Make sure to free them when we're done. */
7879 free_cached_comp_units freer (dwarf2_per_objfile);
7880
7881 build_type_psymtabs (dwarf2_per_objfile);
7882
7883 create_all_comp_units (dwarf2_per_objfile);
7884
7885 /* Create a temporary address map on a temporary obstack. We later
7886 copy this to the final obstack. */
7887 auto_obstack temp_obstack;
7888
7889 scoped_restore save_psymtabs_addrmap
7890 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7891 addrmap_create_mutable (&temp_obstack));
7892
7893 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7894 {
7895 if (per_cu->v.psymtab != NULL)
7896 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7897 continue;
7898 process_psymtab_comp_unit (per_cu, false, language_minimal);
7899 }
7900
7901 /* This has to wait until we read the CUs, we need the list of DWOs. */
7902 process_skeletonless_type_units (dwarf2_per_objfile);
7903
7904 /* Now that all TUs have been processed we can fill in the dependencies. */
7905 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7906 {
7907 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7908 build_type_psymtab_dependencies, dwarf2_per_objfile);
7909 }
7910
7911 if (dwarf_read_debug)
7912 print_tu_stats (dwarf2_per_objfile);
7913
7914 set_partial_user (dwarf2_per_objfile);
7915
7916 objfile->partial_symtabs->psymtabs_addrmap
7917 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7918 objfile->partial_symtabs->obstack ());
7919 /* At this point we want to keep the address map. */
7920 save_psymtabs_addrmap.release ();
7921
7922 if (dwarf_read_debug)
7923 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7924 objfile_name (objfile));
7925 }
7926
7927 /* Load the partial DIEs for a secondary CU into memory.
7928 This is also used when rereading a primary CU with load_all_dies. */
7929
7930 static void
7931 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7932 {
7933 cutu_reader reader (this_cu, NULL, 1, false);
7934
7935 if (!reader.dummy_p)
7936 {
7937 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7938 language_minimal);
7939
7940 /* Check if comp unit has_children.
7941 If so, read the rest of the partial symbols from this comp unit.
7942 If not, there's no more debug_info for this comp unit. */
7943 if (reader.comp_unit_die->has_children)
7944 load_partial_dies (&reader, reader.info_ptr, 0);
7945
7946 reader.keep ();
7947 }
7948 }
7949
7950 static void
7951 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7952 struct dwarf2_section_info *section,
7953 struct dwarf2_section_info *abbrev_section,
7954 unsigned int is_dwz)
7955 {
7956 const gdb_byte *info_ptr;
7957 struct objfile *objfile = dwarf2_per_objfile->objfile;
7958
7959 if (dwarf_read_debug)
7960 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7961 section->get_name (),
7962 section->get_file_name ());
7963
7964 section->read (objfile);
7965
7966 info_ptr = section->buffer;
7967
7968 while (info_ptr < section->buffer + section->size)
7969 {
7970 struct dwarf2_per_cu_data *this_cu;
7971
7972 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7973
7974 comp_unit_head cu_header;
7975 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7976 abbrev_section, info_ptr,
7977 rcuh_kind::COMPILE);
7978
7979 /* Save the compilation unit for later lookup. */
7980 if (cu_header.unit_type != DW_UT_type)
7981 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
7982 else
7983 {
7984 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
7985 sig_type->signature = cu_header.signature;
7986 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7987 this_cu = &sig_type->per_cu;
7988 }
7989 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7990 this_cu->sect_off = sect_off;
7991 this_cu->length = cu_header.length + cu_header.initial_length_size;
7992 this_cu->is_dwz = is_dwz;
7993 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7994 this_cu->section = section;
7995
7996 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
7997
7998 info_ptr = info_ptr + this_cu->length;
7999 }
8000 }
8001
8002 /* Create a list of all compilation units in OBJFILE.
8003 This is only done for -readnow and building partial symtabs. */
8004
8005 static void
8006 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8007 {
8008 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8009 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8010 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8011
8012 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8013 if (dwz != NULL)
8014 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8015 1);
8016 }
8017
8018 /* Process all loaded DIEs for compilation unit CU, starting at
8019 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8020 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8021 DW_AT_ranges). See the comments of add_partial_subprogram on how
8022 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8023
8024 static void
8025 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8026 CORE_ADDR *highpc, int set_addrmap,
8027 struct dwarf2_cu *cu)
8028 {
8029 struct partial_die_info *pdi;
8030
8031 /* Now, march along the PDI's, descending into ones which have
8032 interesting children but skipping the children of the other ones,
8033 until we reach the end of the compilation unit. */
8034
8035 pdi = first_die;
8036
8037 while (pdi != NULL)
8038 {
8039 pdi->fixup (cu);
8040
8041 /* Anonymous namespaces or modules have no name but have interesting
8042 children, so we need to look at them. Ditto for anonymous
8043 enums. */
8044
8045 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8046 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8047 || pdi->tag == DW_TAG_imported_unit
8048 || pdi->tag == DW_TAG_inlined_subroutine)
8049 {
8050 switch (pdi->tag)
8051 {
8052 case DW_TAG_subprogram:
8053 case DW_TAG_inlined_subroutine:
8054 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8055 break;
8056 case DW_TAG_constant:
8057 case DW_TAG_variable:
8058 case DW_TAG_typedef:
8059 case DW_TAG_union_type:
8060 if (!pdi->is_declaration
8061 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8062 {
8063 add_partial_symbol (pdi, cu);
8064 }
8065 break;
8066 case DW_TAG_class_type:
8067 case DW_TAG_interface_type:
8068 case DW_TAG_structure_type:
8069 if (!pdi->is_declaration)
8070 {
8071 add_partial_symbol (pdi, cu);
8072 }
8073 if ((cu->language == language_rust
8074 || cu->language == language_cplus) && pdi->has_children)
8075 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8076 set_addrmap, cu);
8077 break;
8078 case DW_TAG_enumeration_type:
8079 if (!pdi->is_declaration)
8080 add_partial_enumeration (pdi, cu);
8081 break;
8082 case DW_TAG_base_type:
8083 case DW_TAG_subrange_type:
8084 /* File scope base type definitions are added to the partial
8085 symbol table. */
8086 add_partial_symbol (pdi, cu);
8087 break;
8088 case DW_TAG_namespace:
8089 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8090 break;
8091 case DW_TAG_module:
8092 if (!pdi->is_declaration)
8093 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8094 break;
8095 case DW_TAG_imported_unit:
8096 {
8097 struct dwarf2_per_cu_data *per_cu;
8098
8099 /* For now we don't handle imported units in type units. */
8100 if (cu->per_cu->is_debug_types)
8101 {
8102 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8103 " supported in type units [in module %s]"),
8104 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8105 }
8106
8107 per_cu = dwarf2_find_containing_comp_unit
8108 (pdi->d.sect_off, pdi->is_dwz,
8109 cu->per_cu->dwarf2_per_objfile);
8110
8111 /* Go read the partial unit, if needed. */
8112 if (per_cu->v.psymtab == NULL)
8113 process_psymtab_comp_unit (per_cu, true, cu->language);
8114
8115 cu->per_cu->imported_symtabs_push (per_cu);
8116 }
8117 break;
8118 case DW_TAG_imported_declaration:
8119 add_partial_symbol (pdi, cu);
8120 break;
8121 default:
8122 break;
8123 }
8124 }
8125
8126 /* If the die has a sibling, skip to the sibling. */
8127
8128 pdi = pdi->die_sibling;
8129 }
8130 }
8131
8132 /* Functions used to compute the fully scoped name of a partial DIE.
8133
8134 Normally, this is simple. For C++, the parent DIE's fully scoped
8135 name is concatenated with "::" and the partial DIE's name.
8136 Enumerators are an exception; they use the scope of their parent
8137 enumeration type, i.e. the name of the enumeration type is not
8138 prepended to the enumerator.
8139
8140 There are two complexities. One is DW_AT_specification; in this
8141 case "parent" means the parent of the target of the specification,
8142 instead of the direct parent of the DIE. The other is compilers
8143 which do not emit DW_TAG_namespace; in this case we try to guess
8144 the fully qualified name of structure types from their members'
8145 linkage names. This must be done using the DIE's children rather
8146 than the children of any DW_AT_specification target. We only need
8147 to do this for structures at the top level, i.e. if the target of
8148 any DW_AT_specification (if any; otherwise the DIE itself) does not
8149 have a parent. */
8150
8151 /* Compute the scope prefix associated with PDI's parent, in
8152 compilation unit CU. The result will be allocated on CU's
8153 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8154 field. NULL is returned if no prefix is necessary. */
8155 static const char *
8156 partial_die_parent_scope (struct partial_die_info *pdi,
8157 struct dwarf2_cu *cu)
8158 {
8159 const char *grandparent_scope;
8160 struct partial_die_info *parent, *real_pdi;
8161
8162 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8163 then this means the parent of the specification DIE. */
8164
8165 real_pdi = pdi;
8166 while (real_pdi->has_specification)
8167 {
8168 auto res = find_partial_die (real_pdi->spec_offset,
8169 real_pdi->spec_is_dwz, cu);
8170 real_pdi = res.pdi;
8171 cu = res.cu;
8172 }
8173
8174 parent = real_pdi->die_parent;
8175 if (parent == NULL)
8176 return NULL;
8177
8178 if (parent->scope_set)
8179 return parent->scope;
8180
8181 parent->fixup (cu);
8182
8183 grandparent_scope = partial_die_parent_scope (parent, cu);
8184
8185 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8186 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8187 Work around this problem here. */
8188 if (cu->language == language_cplus
8189 && parent->tag == DW_TAG_namespace
8190 && strcmp (parent->name, "::") == 0
8191 && grandparent_scope == NULL)
8192 {
8193 parent->scope = NULL;
8194 parent->scope_set = 1;
8195 return NULL;
8196 }
8197
8198 /* Nested subroutines in Fortran get a prefix. */
8199 if (pdi->tag == DW_TAG_enumerator)
8200 /* Enumerators should not get the name of the enumeration as a prefix. */
8201 parent->scope = grandparent_scope;
8202 else if (parent->tag == DW_TAG_namespace
8203 || parent->tag == DW_TAG_module
8204 || parent->tag == DW_TAG_structure_type
8205 || parent->tag == DW_TAG_class_type
8206 || parent->tag == DW_TAG_interface_type
8207 || parent->tag == DW_TAG_union_type
8208 || parent->tag == DW_TAG_enumeration_type
8209 || (cu->language == language_fortran
8210 && parent->tag == DW_TAG_subprogram
8211 && pdi->tag == DW_TAG_subprogram))
8212 {
8213 if (grandparent_scope == NULL)
8214 parent->scope = parent->name;
8215 else
8216 parent->scope = typename_concat (&cu->comp_unit_obstack,
8217 grandparent_scope,
8218 parent->name, 0, cu);
8219 }
8220 else
8221 {
8222 /* FIXME drow/2004-04-01: What should we be doing with
8223 function-local names? For partial symbols, we should probably be
8224 ignoring them. */
8225 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8226 dwarf_tag_name (parent->tag),
8227 sect_offset_str (pdi->sect_off));
8228 parent->scope = grandparent_scope;
8229 }
8230
8231 parent->scope_set = 1;
8232 return parent->scope;
8233 }
8234
8235 /* Return the fully scoped name associated with PDI, from compilation unit
8236 CU. The result will be allocated with malloc. */
8237
8238 static gdb::unique_xmalloc_ptr<char>
8239 partial_die_full_name (struct partial_die_info *pdi,
8240 struct dwarf2_cu *cu)
8241 {
8242 const char *parent_scope;
8243
8244 /* If this is a template instantiation, we can not work out the
8245 template arguments from partial DIEs. So, unfortunately, we have
8246 to go through the full DIEs. At least any work we do building
8247 types here will be reused if full symbols are loaded later. */
8248 if (pdi->has_template_arguments)
8249 {
8250 pdi->fixup (cu);
8251
8252 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8253 {
8254 struct die_info *die;
8255 struct attribute attr;
8256 struct dwarf2_cu *ref_cu = cu;
8257
8258 /* DW_FORM_ref_addr is using section offset. */
8259 attr.name = (enum dwarf_attribute) 0;
8260 attr.form = DW_FORM_ref_addr;
8261 attr.u.unsnd = to_underlying (pdi->sect_off);
8262 die = follow_die_ref (NULL, &attr, &ref_cu);
8263
8264 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8265 }
8266 }
8267
8268 parent_scope = partial_die_parent_scope (pdi, cu);
8269 if (parent_scope == NULL)
8270 return NULL;
8271 else
8272 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8273 pdi->name, 0, cu));
8274 }
8275
8276 static void
8277 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8278 {
8279 struct dwarf2_per_objfile *dwarf2_per_objfile
8280 = cu->per_cu->dwarf2_per_objfile;
8281 struct objfile *objfile = dwarf2_per_objfile->objfile;
8282 struct gdbarch *gdbarch = objfile->arch ();
8283 CORE_ADDR addr = 0;
8284 const char *actual_name = NULL;
8285 CORE_ADDR baseaddr;
8286
8287 baseaddr = objfile->text_section_offset ();
8288
8289 gdb::unique_xmalloc_ptr<char> built_actual_name
8290 = partial_die_full_name (pdi, cu);
8291 if (built_actual_name != NULL)
8292 actual_name = built_actual_name.get ();
8293
8294 if (actual_name == NULL)
8295 actual_name = pdi->name;
8296
8297 partial_symbol psymbol;
8298 memset (&psymbol, 0, sizeof (psymbol));
8299 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8300 psymbol.ginfo.section = -1;
8301
8302 /* The code below indicates that the psymbol should be installed by
8303 setting this. */
8304 gdb::optional<psymbol_placement> where;
8305
8306 switch (pdi->tag)
8307 {
8308 case DW_TAG_inlined_subroutine:
8309 case DW_TAG_subprogram:
8310 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8311 - baseaddr);
8312 if (pdi->is_external
8313 || cu->language == language_ada
8314 || (cu->language == language_fortran
8315 && pdi->die_parent != NULL
8316 && pdi->die_parent->tag == DW_TAG_subprogram))
8317 {
8318 /* Normally, only "external" DIEs are part of the global scope.
8319 But in Ada and Fortran, we want to be able to access nested
8320 procedures globally. So all Ada and Fortran subprograms are
8321 stored in the global scope. */
8322 where = psymbol_placement::GLOBAL;
8323 }
8324 else
8325 where = psymbol_placement::STATIC;
8326
8327 psymbol.domain = VAR_DOMAIN;
8328 psymbol.aclass = LOC_BLOCK;
8329 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8330 psymbol.ginfo.value.address = addr;
8331
8332 if (pdi->main_subprogram && actual_name != NULL)
8333 set_objfile_main_name (objfile, actual_name, cu->language);
8334 break;
8335 case DW_TAG_constant:
8336 psymbol.domain = VAR_DOMAIN;
8337 psymbol.aclass = LOC_STATIC;
8338 where = (pdi->is_external
8339 ? psymbol_placement::GLOBAL
8340 : psymbol_placement::STATIC);
8341 break;
8342 case DW_TAG_variable:
8343 if (pdi->d.locdesc)
8344 addr = decode_locdesc (pdi->d.locdesc, cu);
8345
8346 if (pdi->d.locdesc
8347 && addr == 0
8348 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8349 {
8350 /* A global or static variable may also have been stripped
8351 out by the linker if unused, in which case its address
8352 will be nullified; do not add such variables into partial
8353 symbol table then. */
8354 }
8355 else if (pdi->is_external)
8356 {
8357 /* Global Variable.
8358 Don't enter into the minimal symbol tables as there is
8359 a minimal symbol table entry from the ELF symbols already.
8360 Enter into partial symbol table if it has a location
8361 descriptor or a type.
8362 If the location descriptor is missing, new_symbol will create
8363 a LOC_UNRESOLVED symbol, the address of the variable will then
8364 be determined from the minimal symbol table whenever the variable
8365 is referenced.
8366 The address for the partial symbol table entry is not
8367 used by GDB, but it comes in handy for debugging partial symbol
8368 table building. */
8369
8370 if (pdi->d.locdesc || pdi->has_type)
8371 {
8372 psymbol.domain = VAR_DOMAIN;
8373 psymbol.aclass = LOC_STATIC;
8374 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8375 psymbol.ginfo.value.address = addr;
8376 where = psymbol_placement::GLOBAL;
8377 }
8378 }
8379 else
8380 {
8381 int has_loc = pdi->d.locdesc != NULL;
8382
8383 /* Static Variable. Skip symbols whose value we cannot know (those
8384 without location descriptors or constant values). */
8385 if (!has_loc && !pdi->has_const_value)
8386 return;
8387
8388 psymbol.domain = VAR_DOMAIN;
8389 psymbol.aclass = LOC_STATIC;
8390 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8391 if (has_loc)
8392 psymbol.ginfo.value.address = addr;
8393 where = psymbol_placement::STATIC;
8394 }
8395 break;
8396 case DW_TAG_typedef:
8397 case DW_TAG_base_type:
8398 case DW_TAG_subrange_type:
8399 psymbol.domain = VAR_DOMAIN;
8400 psymbol.aclass = LOC_TYPEDEF;
8401 where = psymbol_placement::STATIC;
8402 break;
8403 case DW_TAG_imported_declaration:
8404 case DW_TAG_namespace:
8405 psymbol.domain = VAR_DOMAIN;
8406 psymbol.aclass = LOC_TYPEDEF;
8407 where = psymbol_placement::GLOBAL;
8408 break;
8409 case DW_TAG_module:
8410 /* With Fortran 77 there might be a "BLOCK DATA" module
8411 available without any name. If so, we skip the module as it
8412 doesn't bring any value. */
8413 if (actual_name != nullptr)
8414 {
8415 psymbol.domain = MODULE_DOMAIN;
8416 psymbol.aclass = LOC_TYPEDEF;
8417 where = psymbol_placement::GLOBAL;
8418 }
8419 break;
8420 case DW_TAG_class_type:
8421 case DW_TAG_interface_type:
8422 case DW_TAG_structure_type:
8423 case DW_TAG_union_type:
8424 case DW_TAG_enumeration_type:
8425 /* Skip external references. The DWARF standard says in the section
8426 about "Structure, Union, and Class Type Entries": "An incomplete
8427 structure, union or class type is represented by a structure,
8428 union or class entry that does not have a byte size attribute
8429 and that has a DW_AT_declaration attribute." */
8430 if (!pdi->has_byte_size && pdi->is_declaration)
8431 return;
8432
8433 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8434 static vs. global. */
8435 psymbol.domain = STRUCT_DOMAIN;
8436 psymbol.aclass = LOC_TYPEDEF;
8437 where = (cu->language == language_cplus
8438 ? psymbol_placement::GLOBAL
8439 : psymbol_placement::STATIC);
8440 break;
8441 case DW_TAG_enumerator:
8442 psymbol.domain = VAR_DOMAIN;
8443 psymbol.aclass = LOC_CONST;
8444 where = (cu->language == language_cplus
8445 ? psymbol_placement::GLOBAL
8446 : psymbol_placement::STATIC);
8447 break;
8448 default:
8449 break;
8450 }
8451
8452 if (where.has_value ())
8453 {
8454 if (built_actual_name != nullptr)
8455 actual_name = objfile->intern (actual_name);
8456 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8457 psymbol.ginfo.set_linkage_name (actual_name);
8458 else
8459 {
8460 psymbol.ginfo.set_demangled_name (actual_name,
8461 &objfile->objfile_obstack);
8462 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8463 }
8464 add_psymbol_to_list (psymbol, *where, objfile);
8465 }
8466 }
8467
8468 /* Read a partial die corresponding to a namespace; also, add a symbol
8469 corresponding to that namespace to the symbol table. NAMESPACE is
8470 the name of the enclosing namespace. */
8471
8472 static void
8473 add_partial_namespace (struct partial_die_info *pdi,
8474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8475 int set_addrmap, struct dwarf2_cu *cu)
8476 {
8477 /* Add a symbol for the namespace. */
8478
8479 add_partial_symbol (pdi, cu);
8480
8481 /* Now scan partial symbols in that namespace. */
8482
8483 if (pdi->has_children)
8484 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8485 }
8486
8487 /* Read a partial die corresponding to a Fortran module. */
8488
8489 static void
8490 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8491 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8492 {
8493 /* Add a symbol for the namespace. */
8494
8495 add_partial_symbol (pdi, cu);
8496
8497 /* Now scan partial symbols in that module. */
8498
8499 if (pdi->has_children)
8500 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8501 }
8502
8503 /* Read a partial die corresponding to a subprogram or an inlined
8504 subprogram and create a partial symbol for that subprogram.
8505 When the CU language allows it, this routine also defines a partial
8506 symbol for each nested subprogram that this subprogram contains.
8507 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8508 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8509
8510 PDI may also be a lexical block, in which case we simply search
8511 recursively for subprograms defined inside that lexical block.
8512 Again, this is only performed when the CU language allows this
8513 type of definitions. */
8514
8515 static void
8516 add_partial_subprogram (struct partial_die_info *pdi,
8517 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8518 int set_addrmap, struct dwarf2_cu *cu)
8519 {
8520 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8521 {
8522 if (pdi->has_pc_info)
8523 {
8524 if (pdi->lowpc < *lowpc)
8525 *lowpc = pdi->lowpc;
8526 if (pdi->highpc > *highpc)
8527 *highpc = pdi->highpc;
8528 if (set_addrmap)
8529 {
8530 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8531 struct gdbarch *gdbarch = objfile->arch ();
8532 CORE_ADDR baseaddr;
8533 CORE_ADDR this_highpc;
8534 CORE_ADDR this_lowpc;
8535
8536 baseaddr = objfile->text_section_offset ();
8537 this_lowpc
8538 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8539 pdi->lowpc + baseaddr)
8540 - baseaddr);
8541 this_highpc
8542 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8543 pdi->highpc + baseaddr)
8544 - baseaddr);
8545 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8546 this_lowpc, this_highpc - 1,
8547 cu->per_cu->v.psymtab);
8548 }
8549 }
8550
8551 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8552 {
8553 if (!pdi->is_declaration)
8554 /* Ignore subprogram DIEs that do not have a name, they are
8555 illegal. Do not emit a complaint at this point, we will
8556 do so when we convert this psymtab into a symtab. */
8557 if (pdi->name)
8558 add_partial_symbol (pdi, cu);
8559 }
8560 }
8561
8562 if (! pdi->has_children)
8563 return;
8564
8565 if (cu->language == language_ada || cu->language == language_fortran)
8566 {
8567 pdi = pdi->die_child;
8568 while (pdi != NULL)
8569 {
8570 pdi->fixup (cu);
8571 if (pdi->tag == DW_TAG_subprogram
8572 || pdi->tag == DW_TAG_inlined_subroutine
8573 || pdi->tag == DW_TAG_lexical_block)
8574 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8575 pdi = pdi->die_sibling;
8576 }
8577 }
8578 }
8579
8580 /* Read a partial die corresponding to an enumeration type. */
8581
8582 static void
8583 add_partial_enumeration (struct partial_die_info *enum_pdi,
8584 struct dwarf2_cu *cu)
8585 {
8586 struct partial_die_info *pdi;
8587
8588 if (enum_pdi->name != NULL)
8589 add_partial_symbol (enum_pdi, cu);
8590
8591 pdi = enum_pdi->die_child;
8592 while (pdi)
8593 {
8594 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8595 complaint (_("malformed enumerator DIE ignored"));
8596 else
8597 add_partial_symbol (pdi, cu);
8598 pdi = pdi->die_sibling;
8599 }
8600 }
8601
8602 /* Return the initial uleb128 in the die at INFO_PTR. */
8603
8604 static unsigned int
8605 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8606 {
8607 unsigned int bytes_read;
8608
8609 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8610 }
8611
8612 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8613 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8614
8615 Return the corresponding abbrev, or NULL if the number is zero (indicating
8616 an empty DIE). In either case *BYTES_READ will be set to the length of
8617 the initial number. */
8618
8619 static struct abbrev_info *
8620 peek_die_abbrev (const die_reader_specs &reader,
8621 const gdb_byte *info_ptr, unsigned int *bytes_read)
8622 {
8623 dwarf2_cu *cu = reader.cu;
8624 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8625 unsigned int abbrev_number
8626 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8627
8628 if (abbrev_number == 0)
8629 return NULL;
8630
8631 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8632 if (!abbrev)
8633 {
8634 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8635 " at offset %s [in module %s]"),
8636 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8637 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8638 }
8639
8640 return abbrev;
8641 }
8642
8643 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8644 Returns a pointer to the end of a series of DIEs, terminated by an empty
8645 DIE. Any children of the skipped DIEs will also be skipped. */
8646
8647 static const gdb_byte *
8648 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8649 {
8650 while (1)
8651 {
8652 unsigned int bytes_read;
8653 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8654
8655 if (abbrev == NULL)
8656 return info_ptr + bytes_read;
8657 else
8658 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8659 }
8660 }
8661
8662 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8663 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8664 abbrev corresponding to that skipped uleb128 should be passed in
8665 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8666 children. */
8667
8668 static const gdb_byte *
8669 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8670 struct abbrev_info *abbrev)
8671 {
8672 unsigned int bytes_read;
8673 struct attribute attr;
8674 bfd *abfd = reader->abfd;
8675 struct dwarf2_cu *cu = reader->cu;
8676 const gdb_byte *buffer = reader->buffer;
8677 const gdb_byte *buffer_end = reader->buffer_end;
8678 unsigned int form, i;
8679
8680 for (i = 0; i < abbrev->num_attrs; i++)
8681 {
8682 /* The only abbrev we care about is DW_AT_sibling. */
8683 if (abbrev->attrs[i].name == DW_AT_sibling)
8684 {
8685 bool ignored;
8686 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8687 &ignored);
8688 if (attr.form == DW_FORM_ref_addr)
8689 complaint (_("ignoring absolute DW_AT_sibling"));
8690 else
8691 {
8692 sect_offset off = attr.get_ref_die_offset ();
8693 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8694
8695 if (sibling_ptr < info_ptr)
8696 complaint (_("DW_AT_sibling points backwards"));
8697 else if (sibling_ptr > reader->buffer_end)
8698 reader->die_section->overflow_complaint ();
8699 else
8700 return sibling_ptr;
8701 }
8702 }
8703
8704 /* If it isn't DW_AT_sibling, skip this attribute. */
8705 form = abbrev->attrs[i].form;
8706 skip_attribute:
8707 switch (form)
8708 {
8709 case DW_FORM_ref_addr:
8710 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8711 and later it is offset sized. */
8712 if (cu->header.version == 2)
8713 info_ptr += cu->header.addr_size;
8714 else
8715 info_ptr += cu->header.offset_size;
8716 break;
8717 case DW_FORM_GNU_ref_alt:
8718 info_ptr += cu->header.offset_size;
8719 break;
8720 case DW_FORM_addr:
8721 info_ptr += cu->header.addr_size;
8722 break;
8723 case DW_FORM_data1:
8724 case DW_FORM_ref1:
8725 case DW_FORM_flag:
8726 case DW_FORM_strx1:
8727 info_ptr += 1;
8728 break;
8729 case DW_FORM_flag_present:
8730 case DW_FORM_implicit_const:
8731 break;
8732 case DW_FORM_data2:
8733 case DW_FORM_ref2:
8734 case DW_FORM_strx2:
8735 info_ptr += 2;
8736 break;
8737 case DW_FORM_strx3:
8738 info_ptr += 3;
8739 break;
8740 case DW_FORM_data4:
8741 case DW_FORM_ref4:
8742 case DW_FORM_strx4:
8743 info_ptr += 4;
8744 break;
8745 case DW_FORM_data8:
8746 case DW_FORM_ref8:
8747 case DW_FORM_ref_sig8:
8748 info_ptr += 8;
8749 break;
8750 case DW_FORM_data16:
8751 info_ptr += 16;
8752 break;
8753 case DW_FORM_string:
8754 read_direct_string (abfd, info_ptr, &bytes_read);
8755 info_ptr += bytes_read;
8756 break;
8757 case DW_FORM_sec_offset:
8758 case DW_FORM_strp:
8759 case DW_FORM_GNU_strp_alt:
8760 info_ptr += cu->header.offset_size;
8761 break;
8762 case DW_FORM_exprloc:
8763 case DW_FORM_block:
8764 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8765 info_ptr += bytes_read;
8766 break;
8767 case DW_FORM_block1:
8768 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8769 break;
8770 case DW_FORM_block2:
8771 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8772 break;
8773 case DW_FORM_block4:
8774 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8775 break;
8776 case DW_FORM_addrx:
8777 case DW_FORM_strx:
8778 case DW_FORM_sdata:
8779 case DW_FORM_udata:
8780 case DW_FORM_ref_udata:
8781 case DW_FORM_GNU_addr_index:
8782 case DW_FORM_GNU_str_index:
8783 case DW_FORM_rnglistx:
8784 case DW_FORM_loclistx:
8785 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8786 break;
8787 case DW_FORM_indirect:
8788 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8789 info_ptr += bytes_read;
8790 /* We need to continue parsing from here, so just go back to
8791 the top. */
8792 goto skip_attribute;
8793
8794 default:
8795 error (_("Dwarf Error: Cannot handle %s "
8796 "in DWARF reader [in module %s]"),
8797 dwarf_form_name (form),
8798 bfd_get_filename (abfd));
8799 }
8800 }
8801
8802 if (abbrev->has_children)
8803 return skip_children (reader, info_ptr);
8804 else
8805 return info_ptr;
8806 }
8807
8808 /* Locate ORIG_PDI's sibling.
8809 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8810
8811 static const gdb_byte *
8812 locate_pdi_sibling (const struct die_reader_specs *reader,
8813 struct partial_die_info *orig_pdi,
8814 const gdb_byte *info_ptr)
8815 {
8816 /* Do we know the sibling already? */
8817
8818 if (orig_pdi->sibling)
8819 return orig_pdi->sibling;
8820
8821 /* Are there any children to deal with? */
8822
8823 if (!orig_pdi->has_children)
8824 return info_ptr;
8825
8826 /* Skip the children the long way. */
8827
8828 return skip_children (reader, info_ptr);
8829 }
8830
8831 /* Expand this partial symbol table into a full symbol table. SELF is
8832 not NULL. */
8833
8834 void
8835 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8836 {
8837 struct dwarf2_per_objfile *dwarf2_per_objfile
8838 = get_dwarf2_per_objfile (objfile);
8839
8840 gdb_assert (!readin);
8841 /* If this psymtab is constructed from a debug-only objfile, the
8842 has_section_at_zero flag will not necessarily be correct. We
8843 can get the correct value for this flag by looking at the data
8844 associated with the (presumably stripped) associated objfile. */
8845 if (objfile->separate_debug_objfile_backlink)
8846 {
8847 struct dwarf2_per_objfile *dpo_backlink
8848 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8849
8850 dwarf2_per_objfile->per_bfd->has_section_at_zero
8851 = dpo_backlink->per_bfd->has_section_at_zero;
8852 }
8853
8854 expand_psymtab (objfile);
8855
8856 process_cu_includes (dwarf2_per_objfile);
8857 }
8858 \f
8859 /* Reading in full CUs. */
8860
8861 /* Add PER_CU to the queue. */
8862
8863 static void
8864 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8865 enum language pretend_language)
8866 {
8867 per_cu->queued = 1;
8868 per_cu->dwarf2_per_objfile->per_bfd->queue.emplace (per_cu, pretend_language);
8869 }
8870
8871 /* If PER_CU is not yet queued, add it to the queue.
8872 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8873 dependency.
8874 The result is non-zero if PER_CU was queued, otherwise the result is zero
8875 meaning either PER_CU is already queued or it is already loaded.
8876
8877 N.B. There is an invariant here that if a CU is queued then it is loaded.
8878 The caller is required to load PER_CU if we return non-zero. */
8879
8880 static int
8881 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8882 struct dwarf2_per_cu_data *per_cu,
8883 enum language pretend_language)
8884 {
8885 /* We may arrive here during partial symbol reading, if we need full
8886 DIEs to process an unusual case (e.g. template arguments). Do
8887 not queue PER_CU, just tell our caller to load its DIEs. */
8888 if (per_cu->dwarf2_per_objfile->per_bfd->reading_partial_symbols)
8889 {
8890 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8891 return 1;
8892 return 0;
8893 }
8894
8895 /* Mark the dependence relation so that we don't flush PER_CU
8896 too early. */
8897 if (dependent_cu != NULL)
8898 dwarf2_add_dependence (dependent_cu, per_cu);
8899
8900 /* If it's already on the queue, we have nothing to do. */
8901 if (per_cu->queued)
8902 return 0;
8903
8904 /* If the compilation unit is already loaded, just mark it as
8905 used. */
8906 if (per_cu->cu != NULL)
8907 {
8908 per_cu->cu->last_used = 0;
8909 return 0;
8910 }
8911
8912 /* Add it to the queue. */
8913 queue_comp_unit (per_cu, pretend_language);
8914
8915 return 1;
8916 }
8917
8918 /* Process the queue. */
8919
8920 static void
8921 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8922 {
8923 if (dwarf_read_debug)
8924 {
8925 fprintf_unfiltered (gdb_stdlog,
8926 "Expanding one or more symtabs of objfile %s ...\n",
8927 objfile_name (dwarf2_per_objfile->objfile));
8928 }
8929
8930 /* The queue starts out with one item, but following a DIE reference
8931 may load a new CU, adding it to the end of the queue. */
8932 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8933 {
8934 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8935
8936 if ((dwarf2_per_objfile->per_bfd->using_index
8937 ? !item.per_cu->v.quick->compunit_symtab
8938 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8939 /* Skip dummy CUs. */
8940 && item.per_cu->cu != NULL)
8941 {
8942 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8943 unsigned int debug_print_threshold;
8944 char buf[100];
8945
8946 if (per_cu->is_debug_types)
8947 {
8948 struct signatured_type *sig_type =
8949 (struct signatured_type *) per_cu;
8950
8951 sprintf (buf, "TU %s at offset %s",
8952 hex_string (sig_type->signature),
8953 sect_offset_str (per_cu->sect_off));
8954 /* There can be 100s of TUs.
8955 Only print them in verbose mode. */
8956 debug_print_threshold = 2;
8957 }
8958 else
8959 {
8960 sprintf (buf, "CU at offset %s",
8961 sect_offset_str (per_cu->sect_off));
8962 debug_print_threshold = 1;
8963 }
8964
8965 if (dwarf_read_debug >= debug_print_threshold)
8966 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8967
8968 if (per_cu->is_debug_types)
8969 process_full_type_unit (per_cu, item.pretend_language);
8970 else
8971 process_full_comp_unit (per_cu, item.pretend_language);
8972
8973 if (dwarf_read_debug >= debug_print_threshold)
8974 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8975 }
8976
8977 item.per_cu->queued = 0;
8978 dwarf2_per_objfile->per_bfd->queue.pop ();
8979 }
8980
8981 if (dwarf_read_debug)
8982 {
8983 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8984 objfile_name (dwarf2_per_objfile->objfile));
8985 }
8986 }
8987
8988 /* Read in full symbols for PST, and anything it depends on. */
8989
8990 void
8991 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8992 {
8993 gdb_assert (!readin);
8994
8995 expand_dependencies (objfile);
8996
8997 dw2_do_instantiate_symtab (per_cu_data, false);
8998 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8999 }
9000
9001 /* Trivial hash function for die_info: the hash value of a DIE
9002 is its offset in .debug_info for this objfile. */
9003
9004 static hashval_t
9005 die_hash (const void *item)
9006 {
9007 const struct die_info *die = (const struct die_info *) item;
9008
9009 return to_underlying (die->sect_off);
9010 }
9011
9012 /* Trivial comparison function for die_info structures: two DIEs
9013 are equal if they have the same offset. */
9014
9015 static int
9016 die_eq (const void *item_lhs, const void *item_rhs)
9017 {
9018 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9019 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9020
9021 return die_lhs->sect_off == die_rhs->sect_off;
9022 }
9023
9024 /* Load the DIEs associated with PER_CU into memory. */
9025
9026 static void
9027 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9028 bool skip_partial,
9029 enum language pretend_language)
9030 {
9031 gdb_assert (! this_cu->is_debug_types);
9032
9033 cutu_reader reader (this_cu, NULL, 1, skip_partial);
9034 if (reader.dummy_p)
9035 return;
9036
9037 struct dwarf2_cu *cu = reader.cu;
9038 const gdb_byte *info_ptr = reader.info_ptr;
9039
9040 gdb_assert (cu->die_hash == NULL);
9041 cu->die_hash =
9042 htab_create_alloc_ex (cu->header.length / 12,
9043 die_hash,
9044 die_eq,
9045 NULL,
9046 &cu->comp_unit_obstack,
9047 hashtab_obstack_allocate,
9048 dummy_obstack_deallocate);
9049
9050 if (reader.comp_unit_die->has_children)
9051 reader.comp_unit_die->child
9052 = read_die_and_siblings (&reader, reader.info_ptr,
9053 &info_ptr, reader.comp_unit_die);
9054 cu->dies = reader.comp_unit_die;
9055 /* comp_unit_die is not stored in die_hash, no need. */
9056
9057 /* We try not to read any attributes in this function, because not
9058 all CUs needed for references have been loaded yet, and symbol
9059 table processing isn't initialized. But we have to set the CU language,
9060 or we won't be able to build types correctly.
9061 Similarly, if we do not read the producer, we can not apply
9062 producer-specific interpretation. */
9063 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9064
9065 reader.keep ();
9066 }
9067
9068 /* Add a DIE to the delayed physname list. */
9069
9070 static void
9071 add_to_method_list (struct type *type, int fnfield_index, int index,
9072 const char *name, struct die_info *die,
9073 struct dwarf2_cu *cu)
9074 {
9075 struct delayed_method_info mi;
9076 mi.type = type;
9077 mi.fnfield_index = fnfield_index;
9078 mi.index = index;
9079 mi.name = name;
9080 mi.die = die;
9081 cu->method_list.push_back (mi);
9082 }
9083
9084 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9085 "const" / "volatile". If so, decrements LEN by the length of the
9086 modifier and return true. Otherwise return false. */
9087
9088 template<size_t N>
9089 static bool
9090 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9091 {
9092 size_t mod_len = sizeof (mod) - 1;
9093 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9094 {
9095 len -= mod_len;
9096 return true;
9097 }
9098 return false;
9099 }
9100
9101 /* Compute the physnames of any methods on the CU's method list.
9102
9103 The computation of method physnames is delayed in order to avoid the
9104 (bad) condition that one of the method's formal parameters is of an as yet
9105 incomplete type. */
9106
9107 static void
9108 compute_delayed_physnames (struct dwarf2_cu *cu)
9109 {
9110 /* Only C++ delays computing physnames. */
9111 if (cu->method_list.empty ())
9112 return;
9113 gdb_assert (cu->language == language_cplus);
9114
9115 for (const delayed_method_info &mi : cu->method_list)
9116 {
9117 const char *physname;
9118 struct fn_fieldlist *fn_flp
9119 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9120 physname = dwarf2_physname (mi.name, mi.die, cu);
9121 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9122 = physname ? physname : "";
9123
9124 /* Since there's no tag to indicate whether a method is a
9125 const/volatile overload, extract that information out of the
9126 demangled name. */
9127 if (physname != NULL)
9128 {
9129 size_t len = strlen (physname);
9130
9131 while (1)
9132 {
9133 if (physname[len] == ')') /* shortcut */
9134 break;
9135 else if (check_modifier (physname, len, " const"))
9136 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9137 else if (check_modifier (physname, len, " volatile"))
9138 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9139 else
9140 break;
9141 }
9142 }
9143 }
9144
9145 /* The list is no longer needed. */
9146 cu->method_list.clear ();
9147 }
9148
9149 /* Go objects should be embedded in a DW_TAG_module DIE,
9150 and it's not clear if/how imported objects will appear.
9151 To keep Go support simple until that's worked out,
9152 go back through what we've read and create something usable.
9153 We could do this while processing each DIE, and feels kinda cleaner,
9154 but that way is more invasive.
9155 This is to, for example, allow the user to type "p var" or "b main"
9156 without having to specify the package name, and allow lookups
9157 of module.object to work in contexts that use the expression
9158 parser. */
9159
9160 static void
9161 fixup_go_packaging (struct dwarf2_cu *cu)
9162 {
9163 gdb::unique_xmalloc_ptr<char> package_name;
9164 struct pending *list;
9165 int i;
9166
9167 for (list = *cu->get_builder ()->get_global_symbols ();
9168 list != NULL;
9169 list = list->next)
9170 {
9171 for (i = 0; i < list->nsyms; ++i)
9172 {
9173 struct symbol *sym = list->symbol[i];
9174
9175 if (sym->language () == language_go
9176 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9177 {
9178 gdb::unique_xmalloc_ptr<char> this_package_name
9179 (go_symbol_package_name (sym));
9180
9181 if (this_package_name == NULL)
9182 continue;
9183 if (package_name == NULL)
9184 package_name = std::move (this_package_name);
9185 else
9186 {
9187 struct objfile *objfile
9188 = cu->per_cu->dwarf2_per_objfile->objfile;
9189 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9190 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9191 (symbol_symtab (sym) != NULL
9192 ? symtab_to_filename_for_display
9193 (symbol_symtab (sym))
9194 : objfile_name (objfile)),
9195 this_package_name.get (), package_name.get ());
9196 }
9197 }
9198 }
9199 }
9200
9201 if (package_name != NULL)
9202 {
9203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9204 const char *saved_package_name = objfile->intern (package_name.get ());
9205 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9206 saved_package_name);
9207 struct symbol *sym;
9208
9209 sym = new (&objfile->objfile_obstack) symbol;
9210 sym->set_language (language_go, &objfile->objfile_obstack);
9211 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9212 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9213 e.g., "main" finds the "main" module and not C's main(). */
9214 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9215 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9216 SYMBOL_TYPE (sym) = type;
9217
9218 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9219 }
9220 }
9221
9222 /* Allocate a fully-qualified name consisting of the two parts on the
9223 obstack. */
9224
9225 static const char *
9226 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9227 {
9228 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9229 }
9230
9231 /* A helper that allocates a variant part to attach to a Rust enum
9232 type. OBSTACK is where the results should be allocated. TYPE is
9233 the type we're processing. DISCRIMINANT_INDEX is the index of the
9234 discriminant. It must be the index of one of the fields of TYPE.
9235 DEFAULT_INDEX is the index of the default field; or -1 if there is
9236 no default. RANGES is indexed by "effective" field number (the
9237 field index, but omitting the discriminant and default fields) and
9238 must hold the discriminant values used by the variants. Note that
9239 RANGES must have a lifetime at least as long as OBSTACK -- either
9240 already allocated on it, or static. */
9241
9242 static void
9243 alloc_rust_variant (struct obstack *obstack, struct type *type,
9244 int discriminant_index, int default_index,
9245 gdb::array_view<discriminant_range> ranges)
9246 {
9247 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9248 must be handled by the caller. */
9249 gdb_assert (discriminant_index >= 0
9250 && discriminant_index < type->num_fields ());
9251 gdb_assert (default_index == -1
9252 || (default_index >= 0 && default_index < type->num_fields ()));
9253
9254 /* We have one variant for each non-discriminant field. */
9255 int n_variants = type->num_fields () - 1;
9256
9257 variant *variants = new (obstack) variant[n_variants];
9258 int var_idx = 0;
9259 int range_idx = 0;
9260 for (int i = 0; i < type->num_fields (); ++i)
9261 {
9262 if (i == discriminant_index)
9263 continue;
9264
9265 variants[var_idx].first_field = i;
9266 variants[var_idx].last_field = i + 1;
9267
9268 /* The default field does not need a range, but other fields do.
9269 We skipped the discriminant above. */
9270 if (i != default_index)
9271 {
9272 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9273 ++range_idx;
9274 }
9275
9276 ++var_idx;
9277 }
9278
9279 gdb_assert (range_idx == ranges.size ());
9280 gdb_assert (var_idx == n_variants);
9281
9282 variant_part *part = new (obstack) variant_part;
9283 part->discriminant_index = discriminant_index;
9284 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9285 discriminant_index));
9286 part->variants = gdb::array_view<variant> (variants, n_variants);
9287
9288 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9289 gdb::array_view<variant_part> *prop_value
9290 = new (storage) gdb::array_view<variant_part> (part, 1);
9291
9292 struct dynamic_prop prop;
9293 prop.kind = PROP_VARIANT_PARTS;
9294 prop.data.variant_parts = prop_value;
9295
9296 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9297 }
9298
9299 /* Some versions of rustc emitted enums in an unusual way.
9300
9301 Ordinary enums were emitted as unions. The first element of each
9302 structure in the union was named "RUST$ENUM$DISR". This element
9303 held the discriminant.
9304
9305 These versions of Rust also implemented the "non-zero"
9306 optimization. When the enum had two values, and one is empty and
9307 the other holds a pointer that cannot be zero, the pointer is used
9308 as the discriminant, with a zero value meaning the empty variant.
9309 Here, the union's first member is of the form
9310 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9311 where the fieldnos are the indices of the fields that should be
9312 traversed in order to find the field (which may be several fields deep)
9313 and the variantname is the name of the variant of the case when the
9314 field is zero.
9315
9316 This function recognizes whether TYPE is of one of these forms,
9317 and, if so, smashes it to be a variant type. */
9318
9319 static void
9320 quirk_rust_enum (struct type *type, struct objfile *objfile)
9321 {
9322 gdb_assert (type->code () == TYPE_CODE_UNION);
9323
9324 /* We don't need to deal with empty enums. */
9325 if (type->num_fields () == 0)
9326 return;
9327
9328 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9329 if (type->num_fields () == 1
9330 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9331 {
9332 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9333
9334 /* Decode the field name to find the offset of the
9335 discriminant. */
9336 ULONGEST bit_offset = 0;
9337 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9338 while (name[0] >= '0' && name[0] <= '9')
9339 {
9340 char *tail;
9341 unsigned long index = strtoul (name, &tail, 10);
9342 name = tail;
9343 if (*name != '$'
9344 || index >= field_type->num_fields ()
9345 || (TYPE_FIELD_LOC_KIND (field_type, index)
9346 != FIELD_LOC_KIND_BITPOS))
9347 {
9348 complaint (_("Could not parse Rust enum encoding string \"%s\""
9349 "[in module %s]"),
9350 TYPE_FIELD_NAME (type, 0),
9351 objfile_name (objfile));
9352 return;
9353 }
9354 ++name;
9355
9356 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9357 field_type = TYPE_FIELD_TYPE (field_type, index);
9358 }
9359
9360 /* Smash this type to be a structure type. We have to do this
9361 because the type has already been recorded. */
9362 type->set_code (TYPE_CODE_STRUCT);
9363 type->set_num_fields (3);
9364 /* Save the field we care about. */
9365 struct field saved_field = type->field (0);
9366 type->set_fields
9367 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9368
9369 /* Put the discriminant at index 0. */
9370 TYPE_FIELD_TYPE (type, 0) = field_type;
9371 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9372 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9373 SET_FIELD_BITPOS (type->field (0), bit_offset);
9374
9375 /* The order of fields doesn't really matter, so put the real
9376 field at index 1 and the data-less field at index 2. */
9377 type->field (1) = saved_field;
9378 TYPE_FIELD_NAME (type, 1)
9379 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9380 TYPE_FIELD_TYPE (type, 1)->set_name
9381 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9382 TYPE_FIELD_NAME (type, 1)));
9383
9384 const char *dataless_name
9385 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9386 name);
9387 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9388 dataless_name);
9389 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9390 /* NAME points into the original discriminant name, which
9391 already has the correct lifetime. */
9392 TYPE_FIELD_NAME (type, 2) = name;
9393 SET_FIELD_BITPOS (type->field (2), 0);
9394
9395 /* Indicate that this is a variant type. */
9396 static discriminant_range ranges[1] = { { 0, 0 } };
9397 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9398 }
9399 /* A union with a single anonymous field is probably an old-style
9400 univariant enum. */
9401 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9402 {
9403 /* Smash this type to be a structure type. We have to do this
9404 because the type has already been recorded. */
9405 type->set_code (TYPE_CODE_STRUCT);
9406
9407 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9408 const char *variant_name
9409 = rust_last_path_segment (field_type->name ());
9410 TYPE_FIELD_NAME (type, 0) = variant_name;
9411 field_type->set_name
9412 (rust_fully_qualify (&objfile->objfile_obstack,
9413 type->name (), variant_name));
9414 }
9415 else
9416 {
9417 struct type *disr_type = nullptr;
9418 for (int i = 0; i < type->num_fields (); ++i)
9419 {
9420 disr_type = TYPE_FIELD_TYPE (type, i);
9421
9422 if (disr_type->code () != TYPE_CODE_STRUCT)
9423 {
9424 /* All fields of a true enum will be structs. */
9425 return;
9426 }
9427 else if (disr_type->num_fields () == 0)
9428 {
9429 /* Could be data-less variant, so keep going. */
9430 disr_type = nullptr;
9431 }
9432 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9433 "RUST$ENUM$DISR") != 0)
9434 {
9435 /* Not a Rust enum. */
9436 return;
9437 }
9438 else
9439 {
9440 /* Found one. */
9441 break;
9442 }
9443 }
9444
9445 /* If we got here without a discriminant, then it's probably
9446 just a union. */
9447 if (disr_type == nullptr)
9448 return;
9449
9450 /* Smash this type to be a structure type. We have to do this
9451 because the type has already been recorded. */
9452 type->set_code (TYPE_CODE_STRUCT);
9453
9454 /* Make space for the discriminant field. */
9455 struct field *disr_field = &disr_type->field (0);
9456 field *new_fields
9457 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9458 * sizeof (struct field)));
9459 memcpy (new_fields + 1, type->fields (),
9460 type->num_fields () * sizeof (struct field));
9461 type->set_fields (new_fields);
9462 type->set_num_fields (type->num_fields () + 1);
9463
9464 /* Install the discriminant at index 0 in the union. */
9465 type->field (0) = *disr_field;
9466 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9467 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9468
9469 /* We need a way to find the correct discriminant given a
9470 variant name. For convenience we build a map here. */
9471 struct type *enum_type = FIELD_TYPE (*disr_field);
9472 std::unordered_map<std::string, ULONGEST> discriminant_map;
9473 for (int i = 0; i < enum_type->num_fields (); ++i)
9474 {
9475 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9476 {
9477 const char *name
9478 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9479 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9480 }
9481 }
9482
9483 int n_fields = type->num_fields ();
9484 /* We don't need a range entry for the discriminant, but we do
9485 need one for every other field, as there is no default
9486 variant. */
9487 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9488 discriminant_range,
9489 n_fields - 1);
9490 /* Skip the discriminant here. */
9491 for (int i = 1; i < n_fields; ++i)
9492 {
9493 /* Find the final word in the name of this variant's type.
9494 That name can be used to look up the correct
9495 discriminant. */
9496 const char *variant_name
9497 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9498
9499 auto iter = discriminant_map.find (variant_name);
9500 if (iter != discriminant_map.end ())
9501 {
9502 ranges[i].low = iter->second;
9503 ranges[i].high = iter->second;
9504 }
9505
9506 /* Remove the discriminant field, if it exists. */
9507 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9508 if (sub_type->num_fields () > 0)
9509 {
9510 sub_type->set_num_fields (sub_type->num_fields () - 1);
9511 sub_type->set_fields (sub_type->fields () + 1);
9512 }
9513 TYPE_FIELD_NAME (type, i) = variant_name;
9514 sub_type->set_name
9515 (rust_fully_qualify (&objfile->objfile_obstack,
9516 type->name (), variant_name));
9517 }
9518
9519 /* Indicate that this is a variant type. */
9520 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9521 gdb::array_view<discriminant_range> (ranges,
9522 n_fields - 1));
9523 }
9524 }
9525
9526 /* Rewrite some Rust unions to be structures with variants parts. */
9527
9528 static void
9529 rust_union_quirks (struct dwarf2_cu *cu)
9530 {
9531 gdb_assert (cu->language == language_rust);
9532 for (type *type_ : cu->rust_unions)
9533 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9534 /* We don't need this any more. */
9535 cu->rust_unions.clear ();
9536 }
9537
9538 /* Return the symtab for PER_CU. This works properly regardless of
9539 whether we're using the index or psymtabs. */
9540
9541 static struct compunit_symtab *
9542 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9543 {
9544 return (per_cu->dwarf2_per_objfile->per_bfd->using_index
9545 ? per_cu->v.quick->compunit_symtab
9546 : per_cu->v.psymtab->compunit_symtab);
9547 }
9548
9549 /* A helper function for computing the list of all symbol tables
9550 included by PER_CU. */
9551
9552 static void
9553 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9554 htab_t all_children, htab_t all_type_symtabs,
9555 struct dwarf2_per_cu_data *per_cu,
9556 struct compunit_symtab *immediate_parent)
9557 {
9558 void **slot;
9559 struct compunit_symtab *cust;
9560
9561 slot = htab_find_slot (all_children, per_cu, INSERT);
9562 if (*slot != NULL)
9563 {
9564 /* This inclusion and its children have been processed. */
9565 return;
9566 }
9567
9568 *slot = per_cu;
9569 /* Only add a CU if it has a symbol table. */
9570 cust = get_compunit_symtab (per_cu);
9571 if (cust != NULL)
9572 {
9573 /* If this is a type unit only add its symbol table if we haven't
9574 seen it yet (type unit per_cu's can share symtabs). */
9575 if (per_cu->is_debug_types)
9576 {
9577 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9578 if (*slot == NULL)
9579 {
9580 *slot = cust;
9581 result->push_back (cust);
9582 if (cust->user == NULL)
9583 cust->user = immediate_parent;
9584 }
9585 }
9586 else
9587 {
9588 result->push_back (cust);
9589 if (cust->user == NULL)
9590 cust->user = immediate_parent;
9591 }
9592 }
9593
9594 if (!per_cu->imported_symtabs_empty ())
9595 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9596 {
9597 recursively_compute_inclusions (result, all_children,
9598 all_type_symtabs, ptr, cust);
9599 }
9600 }
9601
9602 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9603 PER_CU. */
9604
9605 static void
9606 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9607 {
9608 gdb_assert (! per_cu->is_debug_types);
9609
9610 if (!per_cu->imported_symtabs_empty ())
9611 {
9612 int len;
9613 std::vector<compunit_symtab *> result_symtabs;
9614 htab_t all_children, all_type_symtabs;
9615 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9616
9617 /* If we don't have a symtab, we can just skip this case. */
9618 if (cust == NULL)
9619 return;
9620
9621 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9622 NULL, xcalloc, xfree);
9623 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9624 NULL, xcalloc, xfree);
9625
9626 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9627 {
9628 recursively_compute_inclusions (&result_symtabs, all_children,
9629 all_type_symtabs, ptr, cust);
9630 }
9631
9632 /* Now we have a transitive closure of all the included symtabs. */
9633 len = result_symtabs.size ();
9634 cust->includes
9635 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9636 struct compunit_symtab *, len + 1);
9637 memcpy (cust->includes, result_symtabs.data (),
9638 len * sizeof (compunit_symtab *));
9639 cust->includes[len] = NULL;
9640
9641 htab_delete (all_children);
9642 htab_delete (all_type_symtabs);
9643 }
9644 }
9645
9646 /* Compute the 'includes' field for the symtabs of all the CUs we just
9647 read. */
9648
9649 static void
9650 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9651 {
9652 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9653 {
9654 if (! iter->is_debug_types)
9655 compute_compunit_symtab_includes (iter);
9656 }
9657
9658 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9659 }
9660
9661 /* Generate full symbol information for PER_CU, whose DIEs have
9662 already been loaded into memory. */
9663
9664 static void
9665 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9666 enum language pretend_language)
9667 {
9668 struct dwarf2_cu *cu = per_cu->cu;
9669 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9670 struct objfile *objfile = dwarf2_per_objfile->objfile;
9671 struct gdbarch *gdbarch = objfile->arch ();
9672 CORE_ADDR lowpc, highpc;
9673 struct compunit_symtab *cust;
9674 CORE_ADDR baseaddr;
9675 struct block *static_block;
9676 CORE_ADDR addr;
9677
9678 baseaddr = objfile->text_section_offset ();
9679
9680 /* Clear the list here in case something was left over. */
9681 cu->method_list.clear ();
9682
9683 cu->language = pretend_language;
9684 cu->language_defn = language_def (cu->language);
9685
9686 /* Do line number decoding in read_file_scope () */
9687 process_die (cu->dies, cu);
9688
9689 /* For now fudge the Go package. */
9690 if (cu->language == language_go)
9691 fixup_go_packaging (cu);
9692
9693 /* Now that we have processed all the DIEs in the CU, all the types
9694 should be complete, and it should now be safe to compute all of the
9695 physnames. */
9696 compute_delayed_physnames (cu);
9697
9698 if (cu->language == language_rust)
9699 rust_union_quirks (cu);
9700
9701 /* Some compilers don't define a DW_AT_high_pc attribute for the
9702 compilation unit. If the DW_AT_high_pc is missing, synthesize
9703 it, by scanning the DIE's below the compilation unit. */
9704 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9705
9706 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9707 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9708
9709 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9710 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9711 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9712 addrmap to help ensure it has an accurate map of pc values belonging to
9713 this comp unit. */
9714 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9715
9716 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9717 SECT_OFF_TEXT (objfile),
9718 0);
9719
9720 if (cust != NULL)
9721 {
9722 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9723
9724 /* Set symtab language to language from DW_AT_language. If the
9725 compilation is from a C file generated by language preprocessors, do
9726 not set the language if it was already deduced by start_subfile. */
9727 if (!(cu->language == language_c
9728 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9729 COMPUNIT_FILETABS (cust)->language = cu->language;
9730
9731 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9732 produce DW_AT_location with location lists but it can be possibly
9733 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9734 there were bugs in prologue debug info, fixed later in GCC-4.5
9735 by "unwind info for epilogues" patch (which is not directly related).
9736
9737 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9738 needed, it would be wrong due to missing DW_AT_producer there.
9739
9740 Still one can confuse GDB by using non-standard GCC compilation
9741 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9742 */
9743 if (cu->has_loclist && gcc_4_minor >= 5)
9744 cust->locations_valid = 1;
9745
9746 if (gcc_4_minor >= 5)
9747 cust->epilogue_unwind_valid = 1;
9748
9749 cust->call_site_htab = cu->call_site_htab;
9750 }
9751
9752 if (dwarf2_per_objfile->per_bfd->using_index)
9753 per_cu->v.quick->compunit_symtab = cust;
9754 else
9755 {
9756 dwarf2_psymtab *pst = per_cu->v.psymtab;
9757 pst->compunit_symtab = cust;
9758 pst->readin = true;
9759 }
9760
9761 /* Push it for inclusion processing later. */
9762 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9763
9764 /* Not needed any more. */
9765 cu->reset_builder ();
9766 }
9767
9768 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9769 already been loaded into memory. */
9770
9771 static void
9772 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9773 enum language pretend_language)
9774 {
9775 struct dwarf2_cu *cu = per_cu->cu;
9776 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9777 struct objfile *objfile = dwarf2_per_objfile->objfile;
9778 struct compunit_symtab *cust;
9779 struct signatured_type *sig_type;
9780
9781 gdb_assert (per_cu->is_debug_types);
9782 sig_type = (struct signatured_type *) per_cu;
9783
9784 /* Clear the list here in case something was left over. */
9785 cu->method_list.clear ();
9786
9787 cu->language = pretend_language;
9788 cu->language_defn = language_def (cu->language);
9789
9790 /* The symbol tables are set up in read_type_unit_scope. */
9791 process_die (cu->dies, cu);
9792
9793 /* For now fudge the Go package. */
9794 if (cu->language == language_go)
9795 fixup_go_packaging (cu);
9796
9797 /* Now that we have processed all the DIEs in the CU, all the types
9798 should be complete, and it should now be safe to compute all of the
9799 physnames. */
9800 compute_delayed_physnames (cu);
9801
9802 if (cu->language == language_rust)
9803 rust_union_quirks (cu);
9804
9805 /* TUs share symbol tables.
9806 If this is the first TU to use this symtab, complete the construction
9807 of it with end_expandable_symtab. Otherwise, complete the addition of
9808 this TU's symbols to the existing symtab. */
9809 if (sig_type->type_unit_group->compunit_symtab == NULL)
9810 {
9811 buildsym_compunit *builder = cu->get_builder ();
9812 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9813 sig_type->type_unit_group->compunit_symtab = cust;
9814
9815 if (cust != NULL)
9816 {
9817 /* Set symtab language to language from DW_AT_language. If the
9818 compilation is from a C file generated by language preprocessors,
9819 do not set the language if it was already deduced by
9820 start_subfile. */
9821 if (!(cu->language == language_c
9822 && COMPUNIT_FILETABS (cust)->language != language_c))
9823 COMPUNIT_FILETABS (cust)->language = cu->language;
9824 }
9825 }
9826 else
9827 {
9828 cu->get_builder ()->augment_type_symtab ();
9829 cust = sig_type->type_unit_group->compunit_symtab;
9830 }
9831
9832 if (dwarf2_per_objfile->per_bfd->using_index)
9833 per_cu->v.quick->compunit_symtab = cust;
9834 else
9835 {
9836 dwarf2_psymtab *pst = per_cu->v.psymtab;
9837 pst->compunit_symtab = cust;
9838 pst->readin = true;
9839 }
9840
9841 /* Not needed any more. */
9842 cu->reset_builder ();
9843 }
9844
9845 /* Process an imported unit DIE. */
9846
9847 static void
9848 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9849 {
9850 struct attribute *attr;
9851
9852 /* For now we don't handle imported units in type units. */
9853 if (cu->per_cu->is_debug_types)
9854 {
9855 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9856 " supported in type units [in module %s]"),
9857 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9858 }
9859
9860 attr = dwarf2_attr (die, DW_AT_import, cu);
9861 if (attr != NULL)
9862 {
9863 sect_offset sect_off = attr->get_ref_die_offset ();
9864 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9865 dwarf2_per_cu_data *per_cu
9866 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9867 cu->per_cu->dwarf2_per_objfile);
9868
9869 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9870 into another compilation unit, at root level. Regard this as a hint,
9871 and ignore it. */
9872 if (die->parent && die->parent->parent == NULL
9873 && per_cu->unit_type == DW_UT_compile
9874 && per_cu->lang == language_cplus)
9875 return;
9876
9877 /* If necessary, add it to the queue and load its DIEs. */
9878 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9879 load_full_comp_unit (per_cu, false, cu->language);
9880
9881 cu->per_cu->imported_symtabs_push (per_cu);
9882 }
9883 }
9884
9885 /* RAII object that represents a process_die scope: i.e.,
9886 starts/finishes processing a DIE. */
9887 class process_die_scope
9888 {
9889 public:
9890 process_die_scope (die_info *die, dwarf2_cu *cu)
9891 : m_die (die), m_cu (cu)
9892 {
9893 /* We should only be processing DIEs not already in process. */
9894 gdb_assert (!m_die->in_process);
9895 m_die->in_process = true;
9896 }
9897
9898 ~process_die_scope ()
9899 {
9900 m_die->in_process = false;
9901
9902 /* If we're done processing the DIE for the CU that owns the line
9903 header, we don't need the line header anymore. */
9904 if (m_cu->line_header_die_owner == m_die)
9905 {
9906 delete m_cu->line_header;
9907 m_cu->line_header = NULL;
9908 m_cu->line_header_die_owner = NULL;
9909 }
9910 }
9911
9912 private:
9913 die_info *m_die;
9914 dwarf2_cu *m_cu;
9915 };
9916
9917 /* Process a die and its children. */
9918
9919 static void
9920 process_die (struct die_info *die, struct dwarf2_cu *cu)
9921 {
9922 process_die_scope scope (die, cu);
9923
9924 switch (die->tag)
9925 {
9926 case DW_TAG_padding:
9927 break;
9928 case DW_TAG_compile_unit:
9929 case DW_TAG_partial_unit:
9930 read_file_scope (die, cu);
9931 break;
9932 case DW_TAG_type_unit:
9933 read_type_unit_scope (die, cu);
9934 break;
9935 case DW_TAG_subprogram:
9936 /* Nested subprograms in Fortran get a prefix. */
9937 if (cu->language == language_fortran
9938 && die->parent != NULL
9939 && die->parent->tag == DW_TAG_subprogram)
9940 cu->processing_has_namespace_info = true;
9941 /* Fall through. */
9942 case DW_TAG_inlined_subroutine:
9943 read_func_scope (die, cu);
9944 break;
9945 case DW_TAG_lexical_block:
9946 case DW_TAG_try_block:
9947 case DW_TAG_catch_block:
9948 read_lexical_block_scope (die, cu);
9949 break;
9950 case DW_TAG_call_site:
9951 case DW_TAG_GNU_call_site:
9952 read_call_site_scope (die, cu);
9953 break;
9954 case DW_TAG_class_type:
9955 case DW_TAG_interface_type:
9956 case DW_TAG_structure_type:
9957 case DW_TAG_union_type:
9958 process_structure_scope (die, cu);
9959 break;
9960 case DW_TAG_enumeration_type:
9961 process_enumeration_scope (die, cu);
9962 break;
9963
9964 /* These dies have a type, but processing them does not create
9965 a symbol or recurse to process the children. Therefore we can
9966 read them on-demand through read_type_die. */
9967 case DW_TAG_subroutine_type:
9968 case DW_TAG_set_type:
9969 case DW_TAG_array_type:
9970 case DW_TAG_pointer_type:
9971 case DW_TAG_ptr_to_member_type:
9972 case DW_TAG_reference_type:
9973 case DW_TAG_rvalue_reference_type:
9974 case DW_TAG_string_type:
9975 break;
9976
9977 case DW_TAG_base_type:
9978 case DW_TAG_subrange_type:
9979 case DW_TAG_typedef:
9980 /* Add a typedef symbol for the type definition, if it has a
9981 DW_AT_name. */
9982 new_symbol (die, read_type_die (die, cu), cu);
9983 break;
9984 case DW_TAG_common_block:
9985 read_common_block (die, cu);
9986 break;
9987 case DW_TAG_common_inclusion:
9988 break;
9989 case DW_TAG_namespace:
9990 cu->processing_has_namespace_info = true;
9991 read_namespace (die, cu);
9992 break;
9993 case DW_TAG_module:
9994 cu->processing_has_namespace_info = true;
9995 read_module (die, cu);
9996 break;
9997 case DW_TAG_imported_declaration:
9998 cu->processing_has_namespace_info = true;
9999 if (read_namespace_alias (die, cu))
10000 break;
10001 /* The declaration is not a global namespace alias. */
10002 /* Fall through. */
10003 case DW_TAG_imported_module:
10004 cu->processing_has_namespace_info = true;
10005 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10006 || cu->language != language_fortran))
10007 complaint (_("Tag '%s' has unexpected children"),
10008 dwarf_tag_name (die->tag));
10009 read_import_statement (die, cu);
10010 break;
10011
10012 case DW_TAG_imported_unit:
10013 process_imported_unit_die (die, cu);
10014 break;
10015
10016 case DW_TAG_variable:
10017 read_variable (die, cu);
10018 break;
10019
10020 default:
10021 new_symbol (die, NULL, cu);
10022 break;
10023 }
10024 }
10025 \f
10026 /* DWARF name computation. */
10027
10028 /* A helper function for dwarf2_compute_name which determines whether DIE
10029 needs to have the name of the scope prepended to the name listed in the
10030 die. */
10031
10032 static int
10033 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10034 {
10035 struct attribute *attr;
10036
10037 switch (die->tag)
10038 {
10039 case DW_TAG_namespace:
10040 case DW_TAG_typedef:
10041 case DW_TAG_class_type:
10042 case DW_TAG_interface_type:
10043 case DW_TAG_structure_type:
10044 case DW_TAG_union_type:
10045 case DW_TAG_enumeration_type:
10046 case DW_TAG_enumerator:
10047 case DW_TAG_subprogram:
10048 case DW_TAG_inlined_subroutine:
10049 case DW_TAG_member:
10050 case DW_TAG_imported_declaration:
10051 return 1;
10052
10053 case DW_TAG_variable:
10054 case DW_TAG_constant:
10055 /* We only need to prefix "globally" visible variables. These include
10056 any variable marked with DW_AT_external or any variable that
10057 lives in a namespace. [Variables in anonymous namespaces
10058 require prefixing, but they are not DW_AT_external.] */
10059
10060 if (dwarf2_attr (die, DW_AT_specification, cu))
10061 {
10062 struct dwarf2_cu *spec_cu = cu;
10063
10064 return die_needs_namespace (die_specification (die, &spec_cu),
10065 spec_cu);
10066 }
10067
10068 attr = dwarf2_attr (die, DW_AT_external, cu);
10069 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10070 && die->parent->tag != DW_TAG_module)
10071 return 0;
10072 /* A variable in a lexical block of some kind does not need a
10073 namespace, even though in C++ such variables may be external
10074 and have a mangled name. */
10075 if (die->parent->tag == DW_TAG_lexical_block
10076 || die->parent->tag == DW_TAG_try_block
10077 || die->parent->tag == DW_TAG_catch_block
10078 || die->parent->tag == DW_TAG_subprogram)
10079 return 0;
10080 return 1;
10081
10082 default:
10083 return 0;
10084 }
10085 }
10086
10087 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10088 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10089 defined for the given DIE. */
10090
10091 static struct attribute *
10092 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10093 {
10094 struct attribute *attr;
10095
10096 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10097 if (attr == NULL)
10098 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10099
10100 return attr;
10101 }
10102
10103 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10104 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10105 defined for the given DIE. */
10106
10107 static const char *
10108 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10109 {
10110 const char *linkage_name;
10111
10112 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10113 if (linkage_name == NULL)
10114 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10115
10116 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10117 See https://github.com/rust-lang/rust/issues/32925. */
10118 if (cu->language == language_rust && linkage_name != NULL
10119 && strchr (linkage_name, '{') != NULL)
10120 linkage_name = NULL;
10121
10122 return linkage_name;
10123 }
10124
10125 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10126 compute the physname for the object, which include a method's:
10127 - formal parameters (C++),
10128 - receiver type (Go),
10129
10130 The term "physname" is a bit confusing.
10131 For C++, for example, it is the demangled name.
10132 For Go, for example, it's the mangled name.
10133
10134 For Ada, return the DIE's linkage name rather than the fully qualified
10135 name. PHYSNAME is ignored..
10136
10137 The result is allocated on the objfile->per_bfd's obstack and
10138 canonicalized. */
10139
10140 static const char *
10141 dwarf2_compute_name (const char *name,
10142 struct die_info *die, struct dwarf2_cu *cu,
10143 int physname)
10144 {
10145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10146
10147 if (name == NULL)
10148 name = dwarf2_name (die, cu);
10149
10150 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10151 but otherwise compute it by typename_concat inside GDB.
10152 FIXME: Actually this is not really true, or at least not always true.
10153 It's all very confusing. compute_and_set_names doesn't try to demangle
10154 Fortran names because there is no mangling standard. So new_symbol
10155 will set the demangled name to the result of dwarf2_full_name, and it is
10156 the demangled name that GDB uses if it exists. */
10157 if (cu->language == language_ada
10158 || (cu->language == language_fortran && physname))
10159 {
10160 /* For Ada unit, we prefer the linkage name over the name, as
10161 the former contains the exported name, which the user expects
10162 to be able to reference. Ideally, we want the user to be able
10163 to reference this entity using either natural or linkage name,
10164 but we haven't started looking at this enhancement yet. */
10165 const char *linkage_name = dw2_linkage_name (die, cu);
10166
10167 if (linkage_name != NULL)
10168 return linkage_name;
10169 }
10170
10171 /* These are the only languages we know how to qualify names in. */
10172 if (name != NULL
10173 && (cu->language == language_cplus
10174 || cu->language == language_fortran || cu->language == language_d
10175 || cu->language == language_rust))
10176 {
10177 if (die_needs_namespace (die, cu))
10178 {
10179 const char *prefix;
10180 const char *canonical_name = NULL;
10181
10182 string_file buf;
10183
10184 prefix = determine_prefix (die, cu);
10185 if (*prefix != '\0')
10186 {
10187 gdb::unique_xmalloc_ptr<char> prefixed_name
10188 (typename_concat (NULL, prefix, name, physname, cu));
10189
10190 buf.puts (prefixed_name.get ());
10191 }
10192 else
10193 buf.puts (name);
10194
10195 /* Template parameters may be specified in the DIE's DW_AT_name, or
10196 as children with DW_TAG_template_type_param or
10197 DW_TAG_value_type_param. If the latter, add them to the name
10198 here. If the name already has template parameters, then
10199 skip this step; some versions of GCC emit both, and
10200 it is more efficient to use the pre-computed name.
10201
10202 Something to keep in mind about this process: it is very
10203 unlikely, or in some cases downright impossible, to produce
10204 something that will match the mangled name of a function.
10205 If the definition of the function has the same debug info,
10206 we should be able to match up with it anyway. But fallbacks
10207 using the minimal symbol, for instance to find a method
10208 implemented in a stripped copy of libstdc++, will not work.
10209 If we do not have debug info for the definition, we will have to
10210 match them up some other way.
10211
10212 When we do name matching there is a related problem with function
10213 templates; two instantiated function templates are allowed to
10214 differ only by their return types, which we do not add here. */
10215
10216 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10217 {
10218 struct attribute *attr;
10219 struct die_info *child;
10220 int first = 1;
10221
10222 die->building_fullname = 1;
10223
10224 for (child = die->child; child != NULL; child = child->sibling)
10225 {
10226 struct type *type;
10227 LONGEST value;
10228 const gdb_byte *bytes;
10229 struct dwarf2_locexpr_baton *baton;
10230 struct value *v;
10231
10232 if (child->tag != DW_TAG_template_type_param
10233 && child->tag != DW_TAG_template_value_param)
10234 continue;
10235
10236 if (first)
10237 {
10238 buf.puts ("<");
10239 first = 0;
10240 }
10241 else
10242 buf.puts (", ");
10243
10244 attr = dwarf2_attr (child, DW_AT_type, cu);
10245 if (attr == NULL)
10246 {
10247 complaint (_("template parameter missing DW_AT_type"));
10248 buf.puts ("UNKNOWN_TYPE");
10249 continue;
10250 }
10251 type = die_type (child, cu);
10252
10253 if (child->tag == DW_TAG_template_type_param)
10254 {
10255 c_print_type (type, "", &buf, -1, 0, cu->language,
10256 &type_print_raw_options);
10257 continue;
10258 }
10259
10260 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10261 if (attr == NULL)
10262 {
10263 complaint (_("template parameter missing "
10264 "DW_AT_const_value"));
10265 buf.puts ("UNKNOWN_VALUE");
10266 continue;
10267 }
10268
10269 dwarf2_const_value_attr (attr, type, name,
10270 &cu->comp_unit_obstack, cu,
10271 &value, &bytes, &baton);
10272
10273 if (TYPE_NOSIGN (type))
10274 /* GDB prints characters as NUMBER 'CHAR'. If that's
10275 changed, this can use value_print instead. */
10276 c_printchar (value, type, &buf);
10277 else
10278 {
10279 struct value_print_options opts;
10280
10281 if (baton != NULL)
10282 v = dwarf2_evaluate_loc_desc (type, NULL,
10283 baton->data,
10284 baton->size,
10285 baton->per_cu);
10286 else if (bytes != NULL)
10287 {
10288 v = allocate_value (type);
10289 memcpy (value_contents_writeable (v), bytes,
10290 TYPE_LENGTH (type));
10291 }
10292 else
10293 v = value_from_longest (type, value);
10294
10295 /* Specify decimal so that we do not depend on
10296 the radix. */
10297 get_formatted_print_options (&opts, 'd');
10298 opts.raw = 1;
10299 value_print (v, &buf, &opts);
10300 release_value (v);
10301 }
10302 }
10303
10304 die->building_fullname = 0;
10305
10306 if (!first)
10307 {
10308 /* Close the argument list, with a space if necessary
10309 (nested templates). */
10310 if (!buf.empty () && buf.string ().back () == '>')
10311 buf.puts (" >");
10312 else
10313 buf.puts (">");
10314 }
10315 }
10316
10317 /* For C++ methods, append formal parameter type
10318 information, if PHYSNAME. */
10319
10320 if (physname && die->tag == DW_TAG_subprogram
10321 && cu->language == language_cplus)
10322 {
10323 struct type *type = read_type_die (die, cu);
10324
10325 c_type_print_args (type, &buf, 1, cu->language,
10326 &type_print_raw_options);
10327
10328 if (cu->language == language_cplus)
10329 {
10330 /* Assume that an artificial first parameter is
10331 "this", but do not crash if it is not. RealView
10332 marks unnamed (and thus unused) parameters as
10333 artificial; there is no way to differentiate
10334 the two cases. */
10335 if (type->num_fields () > 0
10336 && TYPE_FIELD_ARTIFICIAL (type, 0)
10337 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10338 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10339 0))))
10340 buf.puts (" const");
10341 }
10342 }
10343
10344 const std::string &intermediate_name = buf.string ();
10345
10346 if (cu->language == language_cplus)
10347 canonical_name
10348 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10349 objfile);
10350
10351 /* If we only computed INTERMEDIATE_NAME, or if
10352 INTERMEDIATE_NAME is already canonical, then we need to
10353 intern it. */
10354 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10355 name = objfile->intern (intermediate_name);
10356 else
10357 name = canonical_name;
10358 }
10359 }
10360
10361 return name;
10362 }
10363
10364 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10365 If scope qualifiers are appropriate they will be added. The result
10366 will be allocated on the storage_obstack, or NULL if the DIE does
10367 not have a name. NAME may either be from a previous call to
10368 dwarf2_name or NULL.
10369
10370 The output string will be canonicalized (if C++). */
10371
10372 static const char *
10373 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10374 {
10375 return dwarf2_compute_name (name, die, cu, 0);
10376 }
10377
10378 /* Construct a physname for the given DIE in CU. NAME may either be
10379 from a previous call to dwarf2_name or NULL. The result will be
10380 allocated on the objfile_objstack or NULL if the DIE does not have a
10381 name.
10382
10383 The output string will be canonicalized (if C++). */
10384
10385 static const char *
10386 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10387 {
10388 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10389 const char *retval, *mangled = NULL, *canon = NULL;
10390 int need_copy = 1;
10391
10392 /* In this case dwarf2_compute_name is just a shortcut not building anything
10393 on its own. */
10394 if (!die_needs_namespace (die, cu))
10395 return dwarf2_compute_name (name, die, cu, 1);
10396
10397 if (cu->language != language_rust)
10398 mangled = dw2_linkage_name (die, cu);
10399
10400 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10401 has computed. */
10402 gdb::unique_xmalloc_ptr<char> demangled;
10403 if (mangled != NULL)
10404 {
10405
10406 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10407 {
10408 /* Do nothing (do not demangle the symbol name). */
10409 }
10410 else if (cu->language == language_go)
10411 {
10412 /* This is a lie, but we already lie to the caller new_symbol.
10413 new_symbol assumes we return the mangled name.
10414 This just undoes that lie until things are cleaned up. */
10415 }
10416 else
10417 {
10418 /* Use DMGL_RET_DROP for C++ template functions to suppress
10419 their return type. It is easier for GDB users to search
10420 for such functions as `name(params)' than `long name(params)'.
10421 In such case the minimal symbol names do not match the full
10422 symbol names but for template functions there is never a need
10423 to look up their definition from their declaration so
10424 the only disadvantage remains the minimal symbol variant
10425 `long name(params)' does not have the proper inferior type. */
10426 demangled.reset (gdb_demangle (mangled,
10427 (DMGL_PARAMS | DMGL_ANSI
10428 | DMGL_RET_DROP)));
10429 }
10430 if (demangled)
10431 canon = demangled.get ();
10432 else
10433 {
10434 canon = mangled;
10435 need_copy = 0;
10436 }
10437 }
10438
10439 if (canon == NULL || check_physname)
10440 {
10441 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10442
10443 if (canon != NULL && strcmp (physname, canon) != 0)
10444 {
10445 /* It may not mean a bug in GDB. The compiler could also
10446 compute DW_AT_linkage_name incorrectly. But in such case
10447 GDB would need to be bug-to-bug compatible. */
10448
10449 complaint (_("Computed physname <%s> does not match demangled <%s> "
10450 "(from linkage <%s>) - DIE at %s [in module %s]"),
10451 physname, canon, mangled, sect_offset_str (die->sect_off),
10452 objfile_name (objfile));
10453
10454 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10455 is available here - over computed PHYSNAME. It is safer
10456 against both buggy GDB and buggy compilers. */
10457
10458 retval = canon;
10459 }
10460 else
10461 {
10462 retval = physname;
10463 need_copy = 0;
10464 }
10465 }
10466 else
10467 retval = canon;
10468
10469 if (need_copy)
10470 retval = objfile->intern (retval);
10471
10472 return retval;
10473 }
10474
10475 /* Inspect DIE in CU for a namespace alias. If one exists, record
10476 a new symbol for it.
10477
10478 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10479
10480 static int
10481 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10482 {
10483 struct attribute *attr;
10484
10485 /* If the die does not have a name, this is not a namespace
10486 alias. */
10487 attr = dwarf2_attr (die, DW_AT_name, cu);
10488 if (attr != NULL)
10489 {
10490 int num;
10491 struct die_info *d = die;
10492 struct dwarf2_cu *imported_cu = cu;
10493
10494 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10495 keep inspecting DIEs until we hit the underlying import. */
10496 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10497 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10498 {
10499 attr = dwarf2_attr (d, DW_AT_import, cu);
10500 if (attr == NULL)
10501 break;
10502
10503 d = follow_die_ref (d, attr, &imported_cu);
10504 if (d->tag != DW_TAG_imported_declaration)
10505 break;
10506 }
10507
10508 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10509 {
10510 complaint (_("DIE at %s has too many recursively imported "
10511 "declarations"), sect_offset_str (d->sect_off));
10512 return 0;
10513 }
10514
10515 if (attr != NULL)
10516 {
10517 struct type *type;
10518 sect_offset sect_off = attr->get_ref_die_offset ();
10519
10520 type = get_die_type_at_offset (sect_off, cu->per_cu);
10521 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10522 {
10523 /* This declaration is a global namespace alias. Add
10524 a symbol for it whose type is the aliased namespace. */
10525 new_symbol (die, type, cu);
10526 return 1;
10527 }
10528 }
10529 }
10530
10531 return 0;
10532 }
10533
10534 /* Return the using directives repository (global or local?) to use in the
10535 current context for CU.
10536
10537 For Ada, imported declarations can materialize renamings, which *may* be
10538 global. However it is impossible (for now?) in DWARF to distinguish
10539 "external" imported declarations and "static" ones. As all imported
10540 declarations seem to be static in all other languages, make them all CU-wide
10541 global only in Ada. */
10542
10543 static struct using_direct **
10544 using_directives (struct dwarf2_cu *cu)
10545 {
10546 if (cu->language == language_ada
10547 && cu->get_builder ()->outermost_context_p ())
10548 return cu->get_builder ()->get_global_using_directives ();
10549 else
10550 return cu->get_builder ()->get_local_using_directives ();
10551 }
10552
10553 /* Read the import statement specified by the given die and record it. */
10554
10555 static void
10556 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10557 {
10558 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10559 struct attribute *import_attr;
10560 struct die_info *imported_die, *child_die;
10561 struct dwarf2_cu *imported_cu;
10562 const char *imported_name;
10563 const char *imported_name_prefix;
10564 const char *canonical_name;
10565 const char *import_alias;
10566 const char *imported_declaration = NULL;
10567 const char *import_prefix;
10568 std::vector<const char *> excludes;
10569
10570 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10571 if (import_attr == NULL)
10572 {
10573 complaint (_("Tag '%s' has no DW_AT_import"),
10574 dwarf_tag_name (die->tag));
10575 return;
10576 }
10577
10578 imported_cu = cu;
10579 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10580 imported_name = dwarf2_name (imported_die, imported_cu);
10581 if (imported_name == NULL)
10582 {
10583 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10584
10585 The import in the following code:
10586 namespace A
10587 {
10588 typedef int B;
10589 }
10590
10591 int main ()
10592 {
10593 using A::B;
10594 B b;
10595 return b;
10596 }
10597
10598 ...
10599 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10600 <52> DW_AT_decl_file : 1
10601 <53> DW_AT_decl_line : 6
10602 <54> DW_AT_import : <0x75>
10603 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10604 <59> DW_AT_name : B
10605 <5b> DW_AT_decl_file : 1
10606 <5c> DW_AT_decl_line : 2
10607 <5d> DW_AT_type : <0x6e>
10608 ...
10609 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10610 <76> DW_AT_byte_size : 4
10611 <77> DW_AT_encoding : 5 (signed)
10612
10613 imports the wrong die ( 0x75 instead of 0x58 ).
10614 This case will be ignored until the gcc bug is fixed. */
10615 return;
10616 }
10617
10618 /* Figure out the local name after import. */
10619 import_alias = dwarf2_name (die, cu);
10620
10621 /* Figure out where the statement is being imported to. */
10622 import_prefix = determine_prefix (die, cu);
10623
10624 /* Figure out what the scope of the imported die is and prepend it
10625 to the name of the imported die. */
10626 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10627
10628 if (imported_die->tag != DW_TAG_namespace
10629 && imported_die->tag != DW_TAG_module)
10630 {
10631 imported_declaration = imported_name;
10632 canonical_name = imported_name_prefix;
10633 }
10634 else if (strlen (imported_name_prefix) > 0)
10635 canonical_name = obconcat (&objfile->objfile_obstack,
10636 imported_name_prefix,
10637 (cu->language == language_d ? "." : "::"),
10638 imported_name, (char *) NULL);
10639 else
10640 canonical_name = imported_name;
10641
10642 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10643 for (child_die = die->child; child_die && child_die->tag;
10644 child_die = child_die->sibling)
10645 {
10646 /* DWARF-4: A Fortran use statement with a “rename list” may be
10647 represented by an imported module entry with an import attribute
10648 referring to the module and owned entries corresponding to those
10649 entities that are renamed as part of being imported. */
10650
10651 if (child_die->tag != DW_TAG_imported_declaration)
10652 {
10653 complaint (_("child DW_TAG_imported_declaration expected "
10654 "- DIE at %s [in module %s]"),
10655 sect_offset_str (child_die->sect_off),
10656 objfile_name (objfile));
10657 continue;
10658 }
10659
10660 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10661 if (import_attr == NULL)
10662 {
10663 complaint (_("Tag '%s' has no DW_AT_import"),
10664 dwarf_tag_name (child_die->tag));
10665 continue;
10666 }
10667
10668 imported_cu = cu;
10669 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10670 &imported_cu);
10671 imported_name = dwarf2_name (imported_die, imported_cu);
10672 if (imported_name == NULL)
10673 {
10674 complaint (_("child DW_TAG_imported_declaration has unknown "
10675 "imported name - DIE at %s [in module %s]"),
10676 sect_offset_str (child_die->sect_off),
10677 objfile_name (objfile));
10678 continue;
10679 }
10680
10681 excludes.push_back (imported_name);
10682
10683 process_die (child_die, cu);
10684 }
10685
10686 add_using_directive (using_directives (cu),
10687 import_prefix,
10688 canonical_name,
10689 import_alias,
10690 imported_declaration,
10691 excludes,
10692 0,
10693 &objfile->objfile_obstack);
10694 }
10695
10696 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10697 types, but gives them a size of zero. Starting with version 14,
10698 ICC is compatible with GCC. */
10699
10700 static bool
10701 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10702 {
10703 if (!cu->checked_producer)
10704 check_producer (cu);
10705
10706 return cu->producer_is_icc_lt_14;
10707 }
10708
10709 /* ICC generates a DW_AT_type for C void functions. This was observed on
10710 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10711 which says that void functions should not have a DW_AT_type. */
10712
10713 static bool
10714 producer_is_icc (struct dwarf2_cu *cu)
10715 {
10716 if (!cu->checked_producer)
10717 check_producer (cu);
10718
10719 return cu->producer_is_icc;
10720 }
10721
10722 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10723 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10724 this, it was first present in GCC release 4.3.0. */
10725
10726 static bool
10727 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10728 {
10729 if (!cu->checked_producer)
10730 check_producer (cu);
10731
10732 return cu->producer_is_gcc_lt_4_3;
10733 }
10734
10735 static file_and_directory
10736 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10737 {
10738 file_and_directory res;
10739
10740 /* Find the filename. Do not use dwarf2_name here, since the filename
10741 is not a source language identifier. */
10742 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10743 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10744
10745 if (res.comp_dir == NULL
10746 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10747 && IS_ABSOLUTE_PATH (res.name))
10748 {
10749 res.comp_dir_storage = ldirname (res.name);
10750 if (!res.comp_dir_storage.empty ())
10751 res.comp_dir = res.comp_dir_storage.c_str ();
10752 }
10753 if (res.comp_dir != NULL)
10754 {
10755 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10756 directory, get rid of it. */
10757 const char *cp = strchr (res.comp_dir, ':');
10758
10759 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10760 res.comp_dir = cp + 1;
10761 }
10762
10763 if (res.name == NULL)
10764 res.name = "<unknown>";
10765
10766 return res;
10767 }
10768
10769 /* Handle DW_AT_stmt_list for a compilation unit.
10770 DIE is the DW_TAG_compile_unit die for CU.
10771 COMP_DIR is the compilation directory. LOWPC is passed to
10772 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10773
10774 static void
10775 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10776 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10777 {
10778 struct dwarf2_per_objfile *dwarf2_per_objfile
10779 = cu->per_cu->dwarf2_per_objfile;
10780 struct attribute *attr;
10781 struct line_header line_header_local;
10782 hashval_t line_header_local_hash;
10783 void **slot;
10784 int decode_mapping;
10785
10786 gdb_assert (! cu->per_cu->is_debug_types);
10787
10788 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10789 if (attr == NULL)
10790 return;
10791
10792 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10793
10794 /* The line header hash table is only created if needed (it exists to
10795 prevent redundant reading of the line table for partial_units).
10796 If we're given a partial_unit, we'll need it. If we're given a
10797 compile_unit, then use the line header hash table if it's already
10798 created, but don't create one just yet. */
10799
10800 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10801 && die->tag == DW_TAG_partial_unit)
10802 {
10803 dwarf2_per_objfile->per_bfd->line_header_hash
10804 .reset (htab_create_alloc (127, line_header_hash_voidp,
10805 line_header_eq_voidp,
10806 free_line_header_voidp,
10807 xcalloc, xfree));
10808 }
10809
10810 line_header_local.sect_off = line_offset;
10811 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10812 line_header_local_hash = line_header_hash (&line_header_local);
10813 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10814 {
10815 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10816 &line_header_local,
10817 line_header_local_hash, NO_INSERT);
10818
10819 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10820 is not present in *SLOT (since if there is something in *SLOT then
10821 it will be for a partial_unit). */
10822 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10823 {
10824 gdb_assert (*slot != NULL);
10825 cu->line_header = (struct line_header *) *slot;
10826 return;
10827 }
10828 }
10829
10830 /* dwarf_decode_line_header does not yet provide sufficient information.
10831 We always have to call also dwarf_decode_lines for it. */
10832 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10833 if (lh == NULL)
10834 return;
10835
10836 cu->line_header = lh.release ();
10837 cu->line_header_die_owner = die;
10838
10839 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10840 slot = NULL;
10841 else
10842 {
10843 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10844 &line_header_local,
10845 line_header_local_hash, INSERT);
10846 gdb_assert (slot != NULL);
10847 }
10848 if (slot != NULL && *slot == NULL)
10849 {
10850 /* This newly decoded line number information unit will be owned
10851 by line_header_hash hash table. */
10852 *slot = cu->line_header;
10853 cu->line_header_die_owner = NULL;
10854 }
10855 else
10856 {
10857 /* We cannot free any current entry in (*slot) as that struct line_header
10858 may be already used by multiple CUs. Create only temporary decoded
10859 line_header for this CU - it may happen at most once for each line
10860 number information unit. And if we're not using line_header_hash
10861 then this is what we want as well. */
10862 gdb_assert (die->tag != DW_TAG_partial_unit);
10863 }
10864 decode_mapping = (die->tag != DW_TAG_partial_unit);
10865 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10866 decode_mapping);
10867
10868 }
10869
10870 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10871
10872 static void
10873 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10874 {
10875 struct dwarf2_per_objfile *dwarf2_per_objfile
10876 = cu->per_cu->dwarf2_per_objfile;
10877 struct objfile *objfile = dwarf2_per_objfile->objfile;
10878 struct gdbarch *gdbarch = objfile->arch ();
10879 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10880 CORE_ADDR highpc = ((CORE_ADDR) 0);
10881 struct attribute *attr;
10882 struct die_info *child_die;
10883 CORE_ADDR baseaddr;
10884
10885 prepare_one_comp_unit (cu, die, cu->language);
10886 baseaddr = objfile->text_section_offset ();
10887
10888 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10889
10890 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10891 from finish_block. */
10892 if (lowpc == ((CORE_ADDR) -1))
10893 lowpc = highpc;
10894 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10895
10896 file_and_directory fnd = find_file_and_directory (die, cu);
10897
10898 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10899 standardised yet. As a workaround for the language detection we fall
10900 back to the DW_AT_producer string. */
10901 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10902 cu->language = language_opencl;
10903
10904 /* Similar hack for Go. */
10905 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10906 set_cu_language (DW_LANG_Go, cu);
10907
10908 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10909
10910 /* Decode line number information if present. We do this before
10911 processing child DIEs, so that the line header table is available
10912 for DW_AT_decl_file. */
10913 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10914
10915 /* Process all dies in compilation unit. */
10916 if (die->child != NULL)
10917 {
10918 child_die = die->child;
10919 while (child_die && child_die->tag)
10920 {
10921 process_die (child_die, cu);
10922 child_die = child_die->sibling;
10923 }
10924 }
10925
10926 /* Decode macro information, if present. Dwarf 2 macro information
10927 refers to information in the line number info statement program
10928 header, so we can only read it if we've read the header
10929 successfully. */
10930 attr = dwarf2_attr (die, DW_AT_macros, cu);
10931 if (attr == NULL)
10932 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10933 if (attr && cu->line_header)
10934 {
10935 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10936 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10937
10938 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10939 }
10940 else
10941 {
10942 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10943 if (attr && cu->line_header)
10944 {
10945 unsigned int macro_offset = DW_UNSND (attr);
10946
10947 dwarf_decode_macros (cu, macro_offset, 0);
10948 }
10949 }
10950 }
10951
10952 void
10953 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10954 {
10955 struct type_unit_group *tu_group;
10956 int first_time;
10957 struct attribute *attr;
10958 unsigned int i;
10959 struct signatured_type *sig_type;
10960
10961 gdb_assert (per_cu->is_debug_types);
10962 sig_type = (struct signatured_type *) per_cu;
10963
10964 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10965
10966 /* If we're using .gdb_index (includes -readnow) then
10967 per_cu->type_unit_group may not have been set up yet. */
10968 if (sig_type->type_unit_group == NULL)
10969 sig_type->type_unit_group = get_type_unit_group (this, attr);
10970 tu_group = sig_type->type_unit_group;
10971
10972 /* If we've already processed this stmt_list there's no real need to
10973 do it again, we could fake it and just recreate the part we need
10974 (file name,index -> symtab mapping). If data shows this optimization
10975 is useful we can do it then. */
10976 first_time = tu_group->compunit_symtab == NULL;
10977
10978 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10979 debug info. */
10980 line_header_up lh;
10981 if (attr != NULL)
10982 {
10983 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10984 lh = dwarf_decode_line_header (line_offset, this);
10985 }
10986 if (lh == NULL)
10987 {
10988 if (first_time)
10989 start_symtab ("", NULL, 0);
10990 else
10991 {
10992 gdb_assert (tu_group->symtabs == NULL);
10993 gdb_assert (m_builder == nullptr);
10994 struct compunit_symtab *cust = tu_group->compunit_symtab;
10995 m_builder.reset (new struct buildsym_compunit
10996 (COMPUNIT_OBJFILE (cust), "",
10997 COMPUNIT_DIRNAME (cust),
10998 compunit_language (cust),
10999 0, cust));
11000 list_in_scope = get_builder ()->get_file_symbols ();
11001 }
11002 return;
11003 }
11004
11005 line_header = lh.release ();
11006 line_header_die_owner = die;
11007
11008 if (first_time)
11009 {
11010 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11011
11012 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11013 still initializing it, and our caller (a few levels up)
11014 process_full_type_unit still needs to know if this is the first
11015 time. */
11016
11017 tu_group->symtabs
11018 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11019 struct symtab *, line_header->file_names_size ());
11020
11021 auto &file_names = line_header->file_names ();
11022 for (i = 0; i < file_names.size (); ++i)
11023 {
11024 file_entry &fe = file_names[i];
11025 dwarf2_start_subfile (this, fe.name,
11026 fe.include_dir (line_header));
11027 buildsym_compunit *b = get_builder ();
11028 if (b->get_current_subfile ()->symtab == NULL)
11029 {
11030 /* NOTE: start_subfile will recognize when it's been
11031 passed a file it has already seen. So we can't
11032 assume there's a simple mapping from
11033 cu->line_header->file_names to subfiles, plus
11034 cu->line_header->file_names may contain dups. */
11035 b->get_current_subfile ()->symtab
11036 = allocate_symtab (cust, b->get_current_subfile ()->name);
11037 }
11038
11039 fe.symtab = b->get_current_subfile ()->symtab;
11040 tu_group->symtabs[i] = fe.symtab;
11041 }
11042 }
11043 else
11044 {
11045 gdb_assert (m_builder == nullptr);
11046 struct compunit_symtab *cust = tu_group->compunit_symtab;
11047 m_builder.reset (new struct buildsym_compunit
11048 (COMPUNIT_OBJFILE (cust), "",
11049 COMPUNIT_DIRNAME (cust),
11050 compunit_language (cust),
11051 0, cust));
11052 list_in_scope = get_builder ()->get_file_symbols ();
11053
11054 auto &file_names = line_header->file_names ();
11055 for (i = 0; i < file_names.size (); ++i)
11056 {
11057 file_entry &fe = file_names[i];
11058 fe.symtab = tu_group->symtabs[i];
11059 }
11060 }
11061
11062 /* The main symtab is allocated last. Type units don't have DW_AT_name
11063 so they don't have a "real" (so to speak) symtab anyway.
11064 There is later code that will assign the main symtab to all symbols
11065 that don't have one. We need to handle the case of a symbol with a
11066 missing symtab (DW_AT_decl_file) anyway. */
11067 }
11068
11069 /* Process DW_TAG_type_unit.
11070 For TUs we want to skip the first top level sibling if it's not the
11071 actual type being defined by this TU. In this case the first top
11072 level sibling is there to provide context only. */
11073
11074 static void
11075 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11076 {
11077 struct die_info *child_die;
11078
11079 prepare_one_comp_unit (cu, die, language_minimal);
11080
11081 /* Initialize (or reinitialize) the machinery for building symtabs.
11082 We do this before processing child DIEs, so that the line header table
11083 is available for DW_AT_decl_file. */
11084 cu->setup_type_unit_groups (die);
11085
11086 if (die->child != NULL)
11087 {
11088 child_die = die->child;
11089 while (child_die && child_die->tag)
11090 {
11091 process_die (child_die, cu);
11092 child_die = child_die->sibling;
11093 }
11094 }
11095 }
11096 \f
11097 /* DWO/DWP files.
11098
11099 http://gcc.gnu.org/wiki/DebugFission
11100 http://gcc.gnu.org/wiki/DebugFissionDWP
11101
11102 To simplify handling of both DWO files ("object" files with the DWARF info)
11103 and DWP files (a file with the DWOs packaged up into one file), we treat
11104 DWP files as having a collection of virtual DWO files. */
11105
11106 static hashval_t
11107 hash_dwo_file (const void *item)
11108 {
11109 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11110 hashval_t hash;
11111
11112 hash = htab_hash_string (dwo_file->dwo_name);
11113 if (dwo_file->comp_dir != NULL)
11114 hash += htab_hash_string (dwo_file->comp_dir);
11115 return hash;
11116 }
11117
11118 static int
11119 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11120 {
11121 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11122 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11123
11124 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11125 return 0;
11126 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11127 return lhs->comp_dir == rhs->comp_dir;
11128 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11129 }
11130
11131 /* Allocate a hash table for DWO files. */
11132
11133 static htab_up
11134 allocate_dwo_file_hash_table ()
11135 {
11136 auto delete_dwo_file = [] (void *item)
11137 {
11138 struct dwo_file *dwo_file = (struct dwo_file *) item;
11139
11140 delete dwo_file;
11141 };
11142
11143 return htab_up (htab_create_alloc (41,
11144 hash_dwo_file,
11145 eq_dwo_file,
11146 delete_dwo_file,
11147 xcalloc, xfree));
11148 }
11149
11150 /* Lookup DWO file DWO_NAME. */
11151
11152 static void **
11153 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11154 const char *dwo_name,
11155 const char *comp_dir)
11156 {
11157 struct dwo_file find_entry;
11158 void **slot;
11159
11160 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11161 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11162
11163 find_entry.dwo_name = dwo_name;
11164 find_entry.comp_dir = comp_dir;
11165 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11166 INSERT);
11167
11168 return slot;
11169 }
11170
11171 static hashval_t
11172 hash_dwo_unit (const void *item)
11173 {
11174 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11175
11176 /* This drops the top 32 bits of the id, but is ok for a hash. */
11177 return dwo_unit->signature;
11178 }
11179
11180 static int
11181 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11182 {
11183 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11184 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11185
11186 /* The signature is assumed to be unique within the DWO file.
11187 So while object file CU dwo_id's always have the value zero,
11188 that's OK, assuming each object file DWO file has only one CU,
11189 and that's the rule for now. */
11190 return lhs->signature == rhs->signature;
11191 }
11192
11193 /* Allocate a hash table for DWO CUs,TUs.
11194 There is one of these tables for each of CUs,TUs for each DWO file. */
11195
11196 static htab_up
11197 allocate_dwo_unit_table ()
11198 {
11199 /* Start out with a pretty small number.
11200 Generally DWO files contain only one CU and maybe some TUs. */
11201 return htab_up (htab_create_alloc (3,
11202 hash_dwo_unit,
11203 eq_dwo_unit,
11204 NULL, xcalloc, xfree));
11205 }
11206
11207 /* die_reader_func for create_dwo_cu. */
11208
11209 static void
11210 create_dwo_cu_reader (const struct die_reader_specs *reader,
11211 const gdb_byte *info_ptr,
11212 struct die_info *comp_unit_die,
11213 struct dwo_file *dwo_file,
11214 struct dwo_unit *dwo_unit)
11215 {
11216 struct dwarf2_cu *cu = reader->cu;
11217 sect_offset sect_off = cu->per_cu->sect_off;
11218 struct dwarf2_section_info *section = cu->per_cu->section;
11219
11220 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11221 if (!signature.has_value ())
11222 {
11223 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11224 " its dwo_id [in module %s]"),
11225 sect_offset_str (sect_off), dwo_file->dwo_name);
11226 return;
11227 }
11228
11229 dwo_unit->dwo_file = dwo_file;
11230 dwo_unit->signature = *signature;
11231 dwo_unit->section = section;
11232 dwo_unit->sect_off = sect_off;
11233 dwo_unit->length = cu->per_cu->length;
11234
11235 if (dwarf_read_debug)
11236 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11237 sect_offset_str (sect_off),
11238 hex_string (dwo_unit->signature));
11239 }
11240
11241 /* Create the dwo_units for the CUs in a DWO_FILE.
11242 Note: This function processes DWO files only, not DWP files. */
11243
11244 static void
11245 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11246 dwarf2_cu *cu, struct dwo_file &dwo_file,
11247 dwarf2_section_info &section, htab_up &cus_htab)
11248 {
11249 struct objfile *objfile = dwarf2_per_objfile->objfile;
11250 const gdb_byte *info_ptr, *end_ptr;
11251
11252 section.read (objfile);
11253 info_ptr = section.buffer;
11254
11255 if (info_ptr == NULL)
11256 return;
11257
11258 if (dwarf_read_debug)
11259 {
11260 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11261 section.get_name (),
11262 section.get_file_name ());
11263 }
11264
11265 end_ptr = info_ptr + section.size;
11266 while (info_ptr < end_ptr)
11267 {
11268 struct dwarf2_per_cu_data per_cu;
11269 struct dwo_unit read_unit {};
11270 struct dwo_unit *dwo_unit;
11271 void **slot;
11272 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11273
11274 memset (&per_cu, 0, sizeof (per_cu));
11275 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11276 per_cu.is_debug_types = 0;
11277 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11278 per_cu.section = &section;
11279
11280 cutu_reader reader (&per_cu, cu, &dwo_file);
11281 if (!reader.dummy_p)
11282 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11283 &dwo_file, &read_unit);
11284 info_ptr += per_cu.length;
11285
11286 // If the unit could not be parsed, skip it.
11287 if (read_unit.dwo_file == NULL)
11288 continue;
11289
11290 if (cus_htab == NULL)
11291 cus_htab = allocate_dwo_unit_table ();
11292
11293 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
11294 struct dwo_unit);
11295 *dwo_unit = read_unit;
11296 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11297 gdb_assert (slot != NULL);
11298 if (*slot != NULL)
11299 {
11300 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11301 sect_offset dup_sect_off = dup_cu->sect_off;
11302
11303 complaint (_("debug cu entry at offset %s is duplicate to"
11304 " the entry at offset %s, signature %s"),
11305 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11306 hex_string (dwo_unit->signature));
11307 }
11308 *slot = (void *)dwo_unit;
11309 }
11310 }
11311
11312 /* DWP file .debug_{cu,tu}_index section format:
11313 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11314
11315 DWP Version 1:
11316
11317 Both index sections have the same format, and serve to map a 64-bit
11318 signature to a set of section numbers. Each section begins with a header,
11319 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11320 indexes, and a pool of 32-bit section numbers. The index sections will be
11321 aligned at 8-byte boundaries in the file.
11322
11323 The index section header consists of:
11324
11325 V, 32 bit version number
11326 -, 32 bits unused
11327 N, 32 bit number of compilation units or type units in the index
11328 M, 32 bit number of slots in the hash table
11329
11330 Numbers are recorded using the byte order of the application binary.
11331
11332 The hash table begins at offset 16 in the section, and consists of an array
11333 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11334 order of the application binary). Unused slots in the hash table are 0.
11335 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11336
11337 The parallel table begins immediately after the hash table
11338 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11339 array of 32-bit indexes (using the byte order of the application binary),
11340 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11341 table contains a 32-bit index into the pool of section numbers. For unused
11342 hash table slots, the corresponding entry in the parallel table will be 0.
11343
11344 The pool of section numbers begins immediately following the hash table
11345 (at offset 16 + 12 * M from the beginning of the section). The pool of
11346 section numbers consists of an array of 32-bit words (using the byte order
11347 of the application binary). Each item in the array is indexed starting
11348 from 0. The hash table entry provides the index of the first section
11349 number in the set. Additional section numbers in the set follow, and the
11350 set is terminated by a 0 entry (section number 0 is not used in ELF).
11351
11352 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11353 section must be the first entry in the set, and the .debug_abbrev.dwo must
11354 be the second entry. Other members of the set may follow in any order.
11355
11356 ---
11357
11358 DWP Version 2:
11359
11360 DWP Version 2 combines all the .debug_info, etc. sections into one,
11361 and the entries in the index tables are now offsets into these sections.
11362 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11363 section.
11364
11365 Index Section Contents:
11366 Header
11367 Hash Table of Signatures dwp_hash_table.hash_table
11368 Parallel Table of Indices dwp_hash_table.unit_table
11369 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11370 Table of Section Sizes dwp_hash_table.v2.sizes
11371
11372 The index section header consists of:
11373
11374 V, 32 bit version number
11375 L, 32 bit number of columns in the table of section offsets
11376 N, 32 bit number of compilation units or type units in the index
11377 M, 32 bit number of slots in the hash table
11378
11379 Numbers are recorded using the byte order of the application binary.
11380
11381 The hash table has the same format as version 1.
11382 The parallel table of indices has the same format as version 1,
11383 except that the entries are origin-1 indices into the table of sections
11384 offsets and the table of section sizes.
11385
11386 The table of offsets begins immediately following the parallel table
11387 (at offset 16 + 12 * M from the beginning of the section). The table is
11388 a two-dimensional array of 32-bit words (using the byte order of the
11389 application binary), with L columns and N+1 rows, in row-major order.
11390 Each row in the array is indexed starting from 0. The first row provides
11391 a key to the remaining rows: each column in this row provides an identifier
11392 for a debug section, and the offsets in the same column of subsequent rows
11393 refer to that section. The section identifiers are:
11394
11395 DW_SECT_INFO 1 .debug_info.dwo
11396 DW_SECT_TYPES 2 .debug_types.dwo
11397 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11398 DW_SECT_LINE 4 .debug_line.dwo
11399 DW_SECT_LOC 5 .debug_loc.dwo
11400 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11401 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11402 DW_SECT_MACRO 8 .debug_macro.dwo
11403
11404 The offsets provided by the CU and TU index sections are the base offsets
11405 for the contributions made by each CU or TU to the corresponding section
11406 in the package file. Each CU and TU header contains an abbrev_offset
11407 field, used to find the abbreviations table for that CU or TU within the
11408 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11409 be interpreted as relative to the base offset given in the index section.
11410 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11411 should be interpreted as relative to the base offset for .debug_line.dwo,
11412 and offsets into other debug sections obtained from DWARF attributes should
11413 also be interpreted as relative to the corresponding base offset.
11414
11415 The table of sizes begins immediately following the table of offsets.
11416 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11417 with L columns and N rows, in row-major order. Each row in the array is
11418 indexed starting from 1 (row 0 is shared by the two tables).
11419
11420 ---
11421
11422 Hash table lookup is handled the same in version 1 and 2:
11423
11424 We assume that N and M will not exceed 2^32 - 1.
11425 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11426
11427 Given a 64-bit compilation unit signature or a type signature S, an entry
11428 in the hash table is located as follows:
11429
11430 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11431 the low-order k bits all set to 1.
11432
11433 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11434
11435 3) If the hash table entry at index H matches the signature, use that
11436 entry. If the hash table entry at index H is unused (all zeroes),
11437 terminate the search: the signature is not present in the table.
11438
11439 4) Let H = (H + H') modulo M. Repeat at Step 3.
11440
11441 Because M > N and H' and M are relatively prime, the search is guaranteed
11442 to stop at an unused slot or find the match. */
11443
11444 /* Create a hash table to map DWO IDs to their CU/TU entry in
11445 .debug_{info,types}.dwo in DWP_FILE.
11446 Returns NULL if there isn't one.
11447 Note: This function processes DWP files only, not DWO files. */
11448
11449 static struct dwp_hash_table *
11450 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11451 struct dwp_file *dwp_file, int is_debug_types)
11452 {
11453 struct objfile *objfile = dwarf2_per_objfile->objfile;
11454 bfd *dbfd = dwp_file->dbfd.get ();
11455 const gdb_byte *index_ptr, *index_end;
11456 struct dwarf2_section_info *index;
11457 uint32_t version, nr_columns, nr_units, nr_slots;
11458 struct dwp_hash_table *htab;
11459
11460 if (is_debug_types)
11461 index = &dwp_file->sections.tu_index;
11462 else
11463 index = &dwp_file->sections.cu_index;
11464
11465 if (index->empty ())
11466 return NULL;
11467 index->read (objfile);
11468
11469 index_ptr = index->buffer;
11470 index_end = index_ptr + index->size;
11471
11472 version = read_4_bytes (dbfd, index_ptr);
11473 index_ptr += 4;
11474 if (version == 2)
11475 nr_columns = read_4_bytes (dbfd, index_ptr);
11476 else
11477 nr_columns = 0;
11478 index_ptr += 4;
11479 nr_units = read_4_bytes (dbfd, index_ptr);
11480 index_ptr += 4;
11481 nr_slots = read_4_bytes (dbfd, index_ptr);
11482 index_ptr += 4;
11483
11484 if (version != 1 && version != 2)
11485 {
11486 error (_("Dwarf Error: unsupported DWP file version (%s)"
11487 " [in module %s]"),
11488 pulongest (version), dwp_file->name);
11489 }
11490 if (nr_slots != (nr_slots & -nr_slots))
11491 {
11492 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11493 " is not power of 2 [in module %s]"),
11494 pulongest (nr_slots), dwp_file->name);
11495 }
11496
11497 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11498 htab->version = version;
11499 htab->nr_columns = nr_columns;
11500 htab->nr_units = nr_units;
11501 htab->nr_slots = nr_slots;
11502 htab->hash_table = index_ptr;
11503 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11504
11505 /* Exit early if the table is empty. */
11506 if (nr_slots == 0 || nr_units == 0
11507 || (version == 2 && nr_columns == 0))
11508 {
11509 /* All must be zero. */
11510 if (nr_slots != 0 || nr_units != 0
11511 || (version == 2 && nr_columns != 0))
11512 {
11513 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11514 " all zero [in modules %s]"),
11515 dwp_file->name);
11516 }
11517 return htab;
11518 }
11519
11520 if (version == 1)
11521 {
11522 htab->section_pool.v1.indices =
11523 htab->unit_table + sizeof (uint32_t) * nr_slots;
11524 /* It's harder to decide whether the section is too small in v1.
11525 V1 is deprecated anyway so we punt. */
11526 }
11527 else
11528 {
11529 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11530 int *ids = htab->section_pool.v2.section_ids;
11531 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11532 /* Reverse map for error checking. */
11533 int ids_seen[DW_SECT_MAX + 1];
11534 int i;
11535
11536 if (nr_columns < 2)
11537 {
11538 error (_("Dwarf Error: bad DWP hash table, too few columns"
11539 " in section table [in module %s]"),
11540 dwp_file->name);
11541 }
11542 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, too many columns"
11545 " in section table [in module %s]"),
11546 dwp_file->name);
11547 }
11548 memset (ids, 255, sizeof_ids);
11549 memset (ids_seen, 255, sizeof (ids_seen));
11550 for (i = 0; i < nr_columns; ++i)
11551 {
11552 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11553
11554 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11555 {
11556 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11557 " in section table [in module %s]"),
11558 id, dwp_file->name);
11559 }
11560 if (ids_seen[id] != -1)
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11563 " id %d in section table [in module %s]"),
11564 id, dwp_file->name);
11565 }
11566 ids_seen[id] = i;
11567 ids[i] = id;
11568 }
11569 /* Must have exactly one info or types section. */
11570 if (((ids_seen[DW_SECT_INFO] != -1)
11571 + (ids_seen[DW_SECT_TYPES] != -1))
11572 != 1)
11573 {
11574 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11575 " DWO info/types section [in module %s]"),
11576 dwp_file->name);
11577 }
11578 /* Must have an abbrev section. */
11579 if (ids_seen[DW_SECT_ABBREV] == -1)
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11582 " section [in module %s]"),
11583 dwp_file->name);
11584 }
11585 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11586 htab->section_pool.v2.sizes =
11587 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11588 * nr_units * nr_columns);
11589 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11590 * nr_units * nr_columns))
11591 > index_end)
11592 {
11593 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11594 " [in module %s]"),
11595 dwp_file->name);
11596 }
11597 }
11598
11599 return htab;
11600 }
11601
11602 /* Update SECTIONS with the data from SECTP.
11603
11604 This function is like the other "locate" section routines that are
11605 passed to bfd_map_over_sections, but in this context the sections to
11606 read comes from the DWP V1 hash table, not the full ELF section table.
11607
11608 The result is non-zero for success, or zero if an error was found. */
11609
11610 static int
11611 locate_v1_virtual_dwo_sections (asection *sectp,
11612 struct virtual_v1_dwo_sections *sections)
11613 {
11614 const struct dwop_section_names *names = &dwop_section_names;
11615
11616 if (section_is_p (sectp->name, &names->abbrev_dwo))
11617 {
11618 /* There can be only one. */
11619 if (sections->abbrev.s.section != NULL)
11620 return 0;
11621 sections->abbrev.s.section = sectp;
11622 sections->abbrev.size = bfd_section_size (sectp);
11623 }
11624 else if (section_is_p (sectp->name, &names->info_dwo)
11625 || section_is_p (sectp->name, &names->types_dwo))
11626 {
11627 /* There can be only one. */
11628 if (sections->info_or_types.s.section != NULL)
11629 return 0;
11630 sections->info_or_types.s.section = sectp;
11631 sections->info_or_types.size = bfd_section_size (sectp);
11632 }
11633 else if (section_is_p (sectp->name, &names->line_dwo))
11634 {
11635 /* There can be only one. */
11636 if (sections->line.s.section != NULL)
11637 return 0;
11638 sections->line.s.section = sectp;
11639 sections->line.size = bfd_section_size (sectp);
11640 }
11641 else if (section_is_p (sectp->name, &names->loc_dwo))
11642 {
11643 /* There can be only one. */
11644 if (sections->loc.s.section != NULL)
11645 return 0;
11646 sections->loc.s.section = sectp;
11647 sections->loc.size = bfd_section_size (sectp);
11648 }
11649 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11650 {
11651 /* There can be only one. */
11652 if (sections->macinfo.s.section != NULL)
11653 return 0;
11654 sections->macinfo.s.section = sectp;
11655 sections->macinfo.size = bfd_section_size (sectp);
11656 }
11657 else if (section_is_p (sectp->name, &names->macro_dwo))
11658 {
11659 /* There can be only one. */
11660 if (sections->macro.s.section != NULL)
11661 return 0;
11662 sections->macro.s.section = sectp;
11663 sections->macro.size = bfd_section_size (sectp);
11664 }
11665 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11666 {
11667 /* There can be only one. */
11668 if (sections->str_offsets.s.section != NULL)
11669 return 0;
11670 sections->str_offsets.s.section = sectp;
11671 sections->str_offsets.size = bfd_section_size (sectp);
11672 }
11673 else
11674 {
11675 /* No other kind of section is valid. */
11676 return 0;
11677 }
11678
11679 return 1;
11680 }
11681
11682 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11683 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11684 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11685 This is for DWP version 1 files. */
11686
11687 static struct dwo_unit *
11688 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11689 struct dwp_file *dwp_file,
11690 uint32_t unit_index,
11691 const char *comp_dir,
11692 ULONGEST signature, int is_debug_types)
11693 {
11694 const struct dwp_hash_table *dwp_htab =
11695 is_debug_types ? dwp_file->tus : dwp_file->cus;
11696 bfd *dbfd = dwp_file->dbfd.get ();
11697 const char *kind = is_debug_types ? "TU" : "CU";
11698 struct dwo_file *dwo_file;
11699 struct dwo_unit *dwo_unit;
11700 struct virtual_v1_dwo_sections sections;
11701 void **dwo_file_slot;
11702 int i;
11703
11704 gdb_assert (dwp_file->version == 1);
11705
11706 if (dwarf_read_debug)
11707 {
11708 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11709 kind,
11710 pulongest (unit_index), hex_string (signature),
11711 dwp_file->name);
11712 }
11713
11714 /* Fetch the sections of this DWO unit.
11715 Put a limit on the number of sections we look for so that bad data
11716 doesn't cause us to loop forever. */
11717
11718 #define MAX_NR_V1_DWO_SECTIONS \
11719 (1 /* .debug_info or .debug_types */ \
11720 + 1 /* .debug_abbrev */ \
11721 + 1 /* .debug_line */ \
11722 + 1 /* .debug_loc */ \
11723 + 1 /* .debug_str_offsets */ \
11724 + 1 /* .debug_macro or .debug_macinfo */ \
11725 + 1 /* trailing zero */)
11726
11727 memset (&sections, 0, sizeof (sections));
11728
11729 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11730 {
11731 asection *sectp;
11732 uint32_t section_nr =
11733 read_4_bytes (dbfd,
11734 dwp_htab->section_pool.v1.indices
11735 + (unit_index + i) * sizeof (uint32_t));
11736
11737 if (section_nr == 0)
11738 break;
11739 if (section_nr >= dwp_file->num_sections)
11740 {
11741 error (_("Dwarf Error: bad DWP hash table, section number too large"
11742 " [in module %s]"),
11743 dwp_file->name);
11744 }
11745
11746 sectp = dwp_file->elf_sections[section_nr];
11747 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11748 {
11749 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11750 " [in module %s]"),
11751 dwp_file->name);
11752 }
11753 }
11754
11755 if (i < 2
11756 || sections.info_or_types.empty ()
11757 || sections.abbrev.empty ())
11758 {
11759 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11760 " [in module %s]"),
11761 dwp_file->name);
11762 }
11763 if (i == MAX_NR_V1_DWO_SECTIONS)
11764 {
11765 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11766 " [in module %s]"),
11767 dwp_file->name);
11768 }
11769
11770 /* It's easier for the rest of the code if we fake a struct dwo_file and
11771 have dwo_unit "live" in that. At least for now.
11772
11773 The DWP file can be made up of a random collection of CUs and TUs.
11774 However, for each CU + set of TUs that came from the same original DWO
11775 file, we can combine them back into a virtual DWO file to save space
11776 (fewer struct dwo_file objects to allocate). Remember that for really
11777 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11778
11779 std::string virtual_dwo_name =
11780 string_printf ("virtual-dwo/%d-%d-%d-%d",
11781 sections.abbrev.get_id (),
11782 sections.line.get_id (),
11783 sections.loc.get_id (),
11784 sections.str_offsets.get_id ());
11785 /* Can we use an existing virtual DWO file? */
11786 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11787 virtual_dwo_name.c_str (),
11788 comp_dir);
11789 /* Create one if necessary. */
11790 if (*dwo_file_slot == NULL)
11791 {
11792 if (dwarf_read_debug)
11793 {
11794 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11795 virtual_dwo_name.c_str ());
11796 }
11797 dwo_file = new struct dwo_file;
11798 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11799 dwo_file->comp_dir = comp_dir;
11800 dwo_file->sections.abbrev = sections.abbrev;
11801 dwo_file->sections.line = sections.line;
11802 dwo_file->sections.loc = sections.loc;
11803 dwo_file->sections.macinfo = sections.macinfo;
11804 dwo_file->sections.macro = sections.macro;
11805 dwo_file->sections.str_offsets = sections.str_offsets;
11806 /* The "str" section is global to the entire DWP file. */
11807 dwo_file->sections.str = dwp_file->sections.str;
11808 /* The info or types section is assigned below to dwo_unit,
11809 there's no need to record it in dwo_file.
11810 Also, we can't simply record type sections in dwo_file because
11811 we record a pointer into the vector in dwo_unit. As we collect more
11812 types we'll grow the vector and eventually have to reallocate space
11813 for it, invalidating all copies of pointers into the previous
11814 contents. */
11815 *dwo_file_slot = dwo_file;
11816 }
11817 else
11818 {
11819 if (dwarf_read_debug)
11820 {
11821 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11822 virtual_dwo_name.c_str ());
11823 }
11824 dwo_file = (struct dwo_file *) *dwo_file_slot;
11825 }
11826
11827 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11828 dwo_unit->dwo_file = dwo_file;
11829 dwo_unit->signature = signature;
11830 dwo_unit->section =
11831 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11832 *dwo_unit->section = sections.info_or_types;
11833 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11834
11835 return dwo_unit;
11836 }
11837
11838 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11839 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11840 piece within that section used by a TU/CU, return a virtual section
11841 of just that piece. */
11842
11843 static struct dwarf2_section_info
11844 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11845 struct dwarf2_section_info *section,
11846 bfd_size_type offset, bfd_size_type size)
11847 {
11848 struct dwarf2_section_info result;
11849 asection *sectp;
11850
11851 gdb_assert (section != NULL);
11852 gdb_assert (!section->is_virtual);
11853
11854 memset (&result, 0, sizeof (result));
11855 result.s.containing_section = section;
11856 result.is_virtual = true;
11857
11858 if (size == 0)
11859 return result;
11860
11861 sectp = section->get_bfd_section ();
11862
11863 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11864 bounds of the real section. This is a pretty-rare event, so just
11865 flag an error (easier) instead of a warning and trying to cope. */
11866 if (sectp == NULL
11867 || offset + size > bfd_section_size (sectp))
11868 {
11869 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11870 " in section %s [in module %s]"),
11871 sectp ? bfd_section_name (sectp) : "<unknown>",
11872 objfile_name (dwarf2_per_objfile->objfile));
11873 }
11874
11875 result.virtual_offset = offset;
11876 result.size = size;
11877 return result;
11878 }
11879
11880 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11881 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11882 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11883 This is for DWP version 2 files. */
11884
11885 static struct dwo_unit *
11886 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11887 struct dwp_file *dwp_file,
11888 uint32_t unit_index,
11889 const char *comp_dir,
11890 ULONGEST signature, int is_debug_types)
11891 {
11892 const struct dwp_hash_table *dwp_htab =
11893 is_debug_types ? dwp_file->tus : dwp_file->cus;
11894 bfd *dbfd = dwp_file->dbfd.get ();
11895 const char *kind = is_debug_types ? "TU" : "CU";
11896 struct dwo_file *dwo_file;
11897 struct dwo_unit *dwo_unit;
11898 struct virtual_v2_dwo_sections sections;
11899 void **dwo_file_slot;
11900 int i;
11901
11902 gdb_assert (dwp_file->version == 2);
11903
11904 if (dwarf_read_debug)
11905 {
11906 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11907 kind,
11908 pulongest (unit_index), hex_string (signature),
11909 dwp_file->name);
11910 }
11911
11912 /* Fetch the section offsets of this DWO unit. */
11913
11914 memset (&sections, 0, sizeof (sections));
11915
11916 for (i = 0; i < dwp_htab->nr_columns; ++i)
11917 {
11918 uint32_t offset = read_4_bytes (dbfd,
11919 dwp_htab->section_pool.v2.offsets
11920 + (((unit_index - 1) * dwp_htab->nr_columns
11921 + i)
11922 * sizeof (uint32_t)));
11923 uint32_t size = read_4_bytes (dbfd,
11924 dwp_htab->section_pool.v2.sizes
11925 + (((unit_index - 1) * dwp_htab->nr_columns
11926 + i)
11927 * sizeof (uint32_t)));
11928
11929 switch (dwp_htab->section_pool.v2.section_ids[i])
11930 {
11931 case DW_SECT_INFO:
11932 case DW_SECT_TYPES:
11933 sections.info_or_types_offset = offset;
11934 sections.info_or_types_size = size;
11935 break;
11936 case DW_SECT_ABBREV:
11937 sections.abbrev_offset = offset;
11938 sections.abbrev_size = size;
11939 break;
11940 case DW_SECT_LINE:
11941 sections.line_offset = offset;
11942 sections.line_size = size;
11943 break;
11944 case DW_SECT_LOC:
11945 sections.loc_offset = offset;
11946 sections.loc_size = size;
11947 break;
11948 case DW_SECT_STR_OFFSETS:
11949 sections.str_offsets_offset = offset;
11950 sections.str_offsets_size = size;
11951 break;
11952 case DW_SECT_MACINFO:
11953 sections.macinfo_offset = offset;
11954 sections.macinfo_size = size;
11955 break;
11956 case DW_SECT_MACRO:
11957 sections.macro_offset = offset;
11958 sections.macro_size = size;
11959 break;
11960 }
11961 }
11962
11963 /* It's easier for the rest of the code if we fake a struct dwo_file and
11964 have dwo_unit "live" in that. At least for now.
11965
11966 The DWP file can be made up of a random collection of CUs and TUs.
11967 However, for each CU + set of TUs that came from the same original DWO
11968 file, we can combine them back into a virtual DWO file to save space
11969 (fewer struct dwo_file objects to allocate). Remember that for really
11970 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11971
11972 std::string virtual_dwo_name =
11973 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11974 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11975 (long) (sections.line_size ? sections.line_offset : 0),
11976 (long) (sections.loc_size ? sections.loc_offset : 0),
11977 (long) (sections.str_offsets_size
11978 ? sections.str_offsets_offset : 0));
11979 /* Can we use an existing virtual DWO file? */
11980 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11981 virtual_dwo_name.c_str (),
11982 comp_dir);
11983 /* Create one if necessary. */
11984 if (*dwo_file_slot == NULL)
11985 {
11986 if (dwarf_read_debug)
11987 {
11988 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11989 virtual_dwo_name.c_str ());
11990 }
11991 dwo_file = new struct dwo_file;
11992 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11993 dwo_file->comp_dir = comp_dir;
11994 dwo_file->sections.abbrev =
11995 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11996 sections.abbrev_offset, sections.abbrev_size);
11997 dwo_file->sections.line =
11998 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11999 sections.line_offset, sections.line_size);
12000 dwo_file->sections.loc =
12001 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12002 sections.loc_offset, sections.loc_size);
12003 dwo_file->sections.macinfo =
12004 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12005 sections.macinfo_offset, sections.macinfo_size);
12006 dwo_file->sections.macro =
12007 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12008 sections.macro_offset, sections.macro_size);
12009 dwo_file->sections.str_offsets =
12010 create_dwp_v2_section (dwarf2_per_objfile,
12011 &dwp_file->sections.str_offsets,
12012 sections.str_offsets_offset,
12013 sections.str_offsets_size);
12014 /* The "str" section is global to the entire DWP file. */
12015 dwo_file->sections.str = dwp_file->sections.str;
12016 /* The info or types section is assigned below to dwo_unit,
12017 there's no need to record it in dwo_file.
12018 Also, we can't simply record type sections in dwo_file because
12019 we record a pointer into the vector in dwo_unit. As we collect more
12020 types we'll grow the vector and eventually have to reallocate space
12021 for it, invalidating all copies of pointers into the previous
12022 contents. */
12023 *dwo_file_slot = dwo_file;
12024 }
12025 else
12026 {
12027 if (dwarf_read_debug)
12028 {
12029 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12030 virtual_dwo_name.c_str ());
12031 }
12032 dwo_file = (struct dwo_file *) *dwo_file_slot;
12033 }
12034
12035 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12036 dwo_unit->dwo_file = dwo_file;
12037 dwo_unit->signature = signature;
12038 dwo_unit->section =
12039 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12040 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12041 is_debug_types
12042 ? &dwp_file->sections.types
12043 : &dwp_file->sections.info,
12044 sections.info_or_types_offset,
12045 sections.info_or_types_size);
12046 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12047
12048 return dwo_unit;
12049 }
12050
12051 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12052 Returns NULL if the signature isn't found. */
12053
12054 static struct dwo_unit *
12055 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12056 struct dwp_file *dwp_file, const char *comp_dir,
12057 ULONGEST signature, int is_debug_types)
12058 {
12059 const struct dwp_hash_table *dwp_htab =
12060 is_debug_types ? dwp_file->tus : dwp_file->cus;
12061 bfd *dbfd = dwp_file->dbfd.get ();
12062 uint32_t mask = dwp_htab->nr_slots - 1;
12063 uint32_t hash = signature & mask;
12064 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12065 unsigned int i;
12066 void **slot;
12067 struct dwo_unit find_dwo_cu;
12068
12069 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12070 find_dwo_cu.signature = signature;
12071 slot = htab_find_slot (is_debug_types
12072 ? dwp_file->loaded_tus.get ()
12073 : dwp_file->loaded_cus.get (),
12074 &find_dwo_cu, INSERT);
12075
12076 if (*slot != NULL)
12077 return (struct dwo_unit *) *slot;
12078
12079 /* Use a for loop so that we don't loop forever on bad debug info. */
12080 for (i = 0; i < dwp_htab->nr_slots; ++i)
12081 {
12082 ULONGEST signature_in_table;
12083
12084 signature_in_table =
12085 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12086 if (signature_in_table == signature)
12087 {
12088 uint32_t unit_index =
12089 read_4_bytes (dbfd,
12090 dwp_htab->unit_table + hash * sizeof (uint32_t));
12091
12092 if (dwp_file->version == 1)
12093 {
12094 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12095 dwp_file, unit_index,
12096 comp_dir, signature,
12097 is_debug_types);
12098 }
12099 else
12100 {
12101 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12102 dwp_file, unit_index,
12103 comp_dir, signature,
12104 is_debug_types);
12105 }
12106 return (struct dwo_unit *) *slot;
12107 }
12108 if (signature_in_table == 0)
12109 return NULL;
12110 hash = (hash + hash2) & mask;
12111 }
12112
12113 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12114 " [in module %s]"),
12115 dwp_file->name);
12116 }
12117
12118 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12119 Open the file specified by FILE_NAME and hand it off to BFD for
12120 preliminary analysis. Return a newly initialized bfd *, which
12121 includes a canonicalized copy of FILE_NAME.
12122 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12123 SEARCH_CWD is true if the current directory is to be searched.
12124 It will be searched before debug-file-directory.
12125 If successful, the file is added to the bfd include table of the
12126 objfile's bfd (see gdb_bfd_record_inclusion).
12127 If unable to find/open the file, return NULL.
12128 NOTE: This function is derived from symfile_bfd_open. */
12129
12130 static gdb_bfd_ref_ptr
12131 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12132 const char *file_name, int is_dwp, int search_cwd)
12133 {
12134 int desc;
12135 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12136 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12137 to debug_file_directory. */
12138 const char *search_path;
12139 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12140
12141 gdb::unique_xmalloc_ptr<char> search_path_holder;
12142 if (search_cwd)
12143 {
12144 if (*debug_file_directory != '\0')
12145 {
12146 search_path_holder.reset (concat (".", dirname_separator_string,
12147 debug_file_directory,
12148 (char *) NULL));
12149 search_path = search_path_holder.get ();
12150 }
12151 else
12152 search_path = ".";
12153 }
12154 else
12155 search_path = debug_file_directory;
12156
12157 openp_flags flags = OPF_RETURN_REALPATH;
12158 if (is_dwp)
12159 flags |= OPF_SEARCH_IN_PATH;
12160
12161 gdb::unique_xmalloc_ptr<char> absolute_name;
12162 desc = openp (search_path, flags, file_name,
12163 O_RDONLY | O_BINARY, &absolute_name);
12164 if (desc < 0)
12165 return NULL;
12166
12167 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12168 gnutarget, desc));
12169 if (sym_bfd == NULL)
12170 return NULL;
12171 bfd_set_cacheable (sym_bfd.get (), 1);
12172
12173 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12174 return NULL;
12175
12176 /* Success. Record the bfd as having been included by the objfile's bfd.
12177 This is important because things like demangled_names_hash lives in the
12178 objfile's per_bfd space and may have references to things like symbol
12179 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12180 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12181
12182 return sym_bfd;
12183 }
12184
12185 /* Try to open DWO file FILE_NAME.
12186 COMP_DIR is the DW_AT_comp_dir attribute.
12187 The result is the bfd handle of the file.
12188 If there is a problem finding or opening the file, return NULL.
12189 Upon success, the canonicalized path of the file is stored in the bfd,
12190 same as symfile_bfd_open. */
12191
12192 static gdb_bfd_ref_ptr
12193 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12194 const char *file_name, const char *comp_dir)
12195 {
12196 if (IS_ABSOLUTE_PATH (file_name))
12197 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12198 0 /*is_dwp*/, 0 /*search_cwd*/);
12199
12200 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12201
12202 if (comp_dir != NULL)
12203 {
12204 gdb::unique_xmalloc_ptr<char> path_to_try
12205 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12206
12207 /* NOTE: If comp_dir is a relative path, this will also try the
12208 search path, which seems useful. */
12209 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12210 path_to_try.get (),
12211 0 /*is_dwp*/,
12212 1 /*search_cwd*/));
12213 if (abfd != NULL)
12214 return abfd;
12215 }
12216
12217 /* That didn't work, try debug-file-directory, which, despite its name,
12218 is a list of paths. */
12219
12220 if (*debug_file_directory == '\0')
12221 return NULL;
12222
12223 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12224 0 /*is_dwp*/, 1 /*search_cwd*/);
12225 }
12226
12227 /* This function is mapped across the sections and remembers the offset and
12228 size of each of the DWO debugging sections we are interested in. */
12229
12230 static void
12231 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12232 {
12233 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12234 const struct dwop_section_names *names = &dwop_section_names;
12235
12236 if (section_is_p (sectp->name, &names->abbrev_dwo))
12237 {
12238 dwo_sections->abbrev.s.section = sectp;
12239 dwo_sections->abbrev.size = bfd_section_size (sectp);
12240 }
12241 else if (section_is_p (sectp->name, &names->info_dwo))
12242 {
12243 dwo_sections->info.s.section = sectp;
12244 dwo_sections->info.size = bfd_section_size (sectp);
12245 }
12246 else if (section_is_p (sectp->name, &names->line_dwo))
12247 {
12248 dwo_sections->line.s.section = sectp;
12249 dwo_sections->line.size = bfd_section_size (sectp);
12250 }
12251 else if (section_is_p (sectp->name, &names->loc_dwo))
12252 {
12253 dwo_sections->loc.s.section = sectp;
12254 dwo_sections->loc.size = bfd_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->loclists_dwo))
12257 {
12258 dwo_sections->loclists.s.section = sectp;
12259 dwo_sections->loclists.size = bfd_section_size (sectp);
12260 }
12261 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12262 {
12263 dwo_sections->macinfo.s.section = sectp;
12264 dwo_sections->macinfo.size = bfd_section_size (sectp);
12265 }
12266 else if (section_is_p (sectp->name, &names->macro_dwo))
12267 {
12268 dwo_sections->macro.s.section = sectp;
12269 dwo_sections->macro.size = bfd_section_size (sectp);
12270 }
12271 else if (section_is_p (sectp->name, &names->str_dwo))
12272 {
12273 dwo_sections->str.s.section = sectp;
12274 dwo_sections->str.size = bfd_section_size (sectp);
12275 }
12276 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12277 {
12278 dwo_sections->str_offsets.s.section = sectp;
12279 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12280 }
12281 else if (section_is_p (sectp->name, &names->types_dwo))
12282 {
12283 struct dwarf2_section_info type_section;
12284
12285 memset (&type_section, 0, sizeof (type_section));
12286 type_section.s.section = sectp;
12287 type_section.size = bfd_section_size (sectp);
12288 dwo_sections->types.push_back (type_section);
12289 }
12290 }
12291
12292 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12293 by PER_CU. This is for the non-DWP case.
12294 The result is NULL if DWO_NAME can't be found. */
12295
12296 static struct dwo_file *
12297 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12298 const char *dwo_name, const char *comp_dir)
12299 {
12300 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12301
12302 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12303 if (dbfd == NULL)
12304 {
12305 if (dwarf_read_debug)
12306 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12307 return NULL;
12308 }
12309
12310 dwo_file_up dwo_file (new struct dwo_file);
12311 dwo_file->dwo_name = dwo_name;
12312 dwo_file->comp_dir = comp_dir;
12313 dwo_file->dbfd = std::move (dbfd);
12314
12315 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12316 &dwo_file->sections);
12317
12318 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12319 dwo_file->sections.info, dwo_file->cus);
12320
12321 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12322 dwo_file->sections.types, dwo_file->tus);
12323
12324 if (dwarf_read_debug)
12325 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12326
12327 return dwo_file.release ();
12328 }
12329
12330 /* This function is mapped across the sections and remembers the offset and
12331 size of each of the DWP debugging sections common to version 1 and 2 that
12332 we are interested in. */
12333
12334 static void
12335 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12336 void *dwp_file_ptr)
12337 {
12338 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12339 const struct dwop_section_names *names = &dwop_section_names;
12340 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12341
12342 /* Record the ELF section number for later lookup: this is what the
12343 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12344 gdb_assert (elf_section_nr < dwp_file->num_sections);
12345 dwp_file->elf_sections[elf_section_nr] = sectp;
12346
12347 /* Look for specific sections that we need. */
12348 if (section_is_p (sectp->name, &names->str_dwo))
12349 {
12350 dwp_file->sections.str.s.section = sectp;
12351 dwp_file->sections.str.size = bfd_section_size (sectp);
12352 }
12353 else if (section_is_p (sectp->name, &names->cu_index))
12354 {
12355 dwp_file->sections.cu_index.s.section = sectp;
12356 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12357 }
12358 else if (section_is_p (sectp->name, &names->tu_index))
12359 {
12360 dwp_file->sections.tu_index.s.section = sectp;
12361 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12362 }
12363 }
12364
12365 /* This function is mapped across the sections and remembers the offset and
12366 size of each of the DWP version 2 debugging sections that we are interested
12367 in. This is split into a separate function because we don't know if we
12368 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12369
12370 static void
12371 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12372 {
12373 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12374 const struct dwop_section_names *names = &dwop_section_names;
12375 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12376
12377 /* Record the ELF section number for later lookup: this is what the
12378 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12379 gdb_assert (elf_section_nr < dwp_file->num_sections);
12380 dwp_file->elf_sections[elf_section_nr] = sectp;
12381
12382 /* Look for specific sections that we need. */
12383 if (section_is_p (sectp->name, &names->abbrev_dwo))
12384 {
12385 dwp_file->sections.abbrev.s.section = sectp;
12386 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12387 }
12388 else if (section_is_p (sectp->name, &names->info_dwo))
12389 {
12390 dwp_file->sections.info.s.section = sectp;
12391 dwp_file->sections.info.size = bfd_section_size (sectp);
12392 }
12393 else if (section_is_p (sectp->name, &names->line_dwo))
12394 {
12395 dwp_file->sections.line.s.section = sectp;
12396 dwp_file->sections.line.size = bfd_section_size (sectp);
12397 }
12398 else if (section_is_p (sectp->name, &names->loc_dwo))
12399 {
12400 dwp_file->sections.loc.s.section = sectp;
12401 dwp_file->sections.loc.size = bfd_section_size (sectp);
12402 }
12403 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 {
12405 dwp_file->sections.macinfo.s.section = sectp;
12406 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12407 }
12408 else if (section_is_p (sectp->name, &names->macro_dwo))
12409 {
12410 dwp_file->sections.macro.s.section = sectp;
12411 dwp_file->sections.macro.size = bfd_section_size (sectp);
12412 }
12413 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12414 {
12415 dwp_file->sections.str_offsets.s.section = sectp;
12416 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12417 }
12418 else if (section_is_p (sectp->name, &names->types_dwo))
12419 {
12420 dwp_file->sections.types.s.section = sectp;
12421 dwp_file->sections.types.size = bfd_section_size (sectp);
12422 }
12423 }
12424
12425 /* Hash function for dwp_file loaded CUs/TUs. */
12426
12427 static hashval_t
12428 hash_dwp_loaded_cutus (const void *item)
12429 {
12430 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12431
12432 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12433 return dwo_unit->signature;
12434 }
12435
12436 /* Equality function for dwp_file loaded CUs/TUs. */
12437
12438 static int
12439 eq_dwp_loaded_cutus (const void *a, const void *b)
12440 {
12441 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12442 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12443
12444 return dua->signature == dub->signature;
12445 }
12446
12447 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12448
12449 static htab_up
12450 allocate_dwp_loaded_cutus_table ()
12451 {
12452 return htab_up (htab_create_alloc (3,
12453 hash_dwp_loaded_cutus,
12454 eq_dwp_loaded_cutus,
12455 NULL, xcalloc, xfree));
12456 }
12457
12458 /* Try to open DWP file FILE_NAME.
12459 The result is the bfd handle of the file.
12460 If there is a problem finding or opening the file, return NULL.
12461 Upon success, the canonicalized path of the file is stored in the bfd,
12462 same as symfile_bfd_open. */
12463
12464 static gdb_bfd_ref_ptr
12465 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12466 const char *file_name)
12467 {
12468 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12469 1 /*is_dwp*/,
12470 1 /*search_cwd*/));
12471 if (abfd != NULL)
12472 return abfd;
12473
12474 /* Work around upstream bug 15652.
12475 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12476 [Whether that's a "bug" is debatable, but it is getting in our way.]
12477 We have no real idea where the dwp file is, because gdb's realpath-ing
12478 of the executable's path may have discarded the needed info.
12479 [IWBN if the dwp file name was recorded in the executable, akin to
12480 .gnu_debuglink, but that doesn't exist yet.]
12481 Strip the directory from FILE_NAME and search again. */
12482 if (*debug_file_directory != '\0')
12483 {
12484 /* Don't implicitly search the current directory here.
12485 If the user wants to search "." to handle this case,
12486 it must be added to debug-file-directory. */
12487 return try_open_dwop_file (dwarf2_per_objfile,
12488 lbasename (file_name), 1 /*is_dwp*/,
12489 0 /*search_cwd*/);
12490 }
12491
12492 return NULL;
12493 }
12494
12495 /* Initialize the use of the DWP file for the current objfile.
12496 By convention the name of the DWP file is ${objfile}.dwp.
12497 The result is NULL if it can't be found. */
12498
12499 static std::unique_ptr<struct dwp_file>
12500 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12501 {
12502 struct objfile *objfile = dwarf2_per_objfile->objfile;
12503
12504 /* Try to find first .dwp for the binary file before any symbolic links
12505 resolving. */
12506
12507 /* If the objfile is a debug file, find the name of the real binary
12508 file and get the name of dwp file from there. */
12509 std::string dwp_name;
12510 if (objfile->separate_debug_objfile_backlink != NULL)
12511 {
12512 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12513 const char *backlink_basename = lbasename (backlink->original_name);
12514
12515 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12516 }
12517 else
12518 dwp_name = objfile->original_name;
12519
12520 dwp_name += ".dwp";
12521
12522 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12523 if (dbfd == NULL
12524 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12525 {
12526 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12527 dwp_name = objfile_name (objfile);
12528 dwp_name += ".dwp";
12529 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12530 }
12531
12532 if (dbfd == NULL)
12533 {
12534 if (dwarf_read_debug)
12535 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12536 return std::unique_ptr<dwp_file> ();
12537 }
12538
12539 const char *name = bfd_get_filename (dbfd.get ());
12540 std::unique_ptr<struct dwp_file> dwp_file
12541 (new struct dwp_file (name, std::move (dbfd)));
12542
12543 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12544 dwp_file->elf_sections =
12545 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12546 dwp_file->num_sections, asection *);
12547
12548 bfd_map_over_sections (dwp_file->dbfd.get (),
12549 dwarf2_locate_common_dwp_sections,
12550 dwp_file.get ());
12551
12552 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12553 0);
12554
12555 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12556 1);
12557
12558 /* The DWP file version is stored in the hash table. Oh well. */
12559 if (dwp_file->cus && dwp_file->tus
12560 && dwp_file->cus->version != dwp_file->tus->version)
12561 {
12562 /* Technically speaking, we should try to limp along, but this is
12563 pretty bizarre. We use pulongest here because that's the established
12564 portability solution (e.g, we cannot use %u for uint32_t). */
12565 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12566 " TU version %s [in DWP file %s]"),
12567 pulongest (dwp_file->cus->version),
12568 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12569 }
12570
12571 if (dwp_file->cus)
12572 dwp_file->version = dwp_file->cus->version;
12573 else if (dwp_file->tus)
12574 dwp_file->version = dwp_file->tus->version;
12575 else
12576 dwp_file->version = 2;
12577
12578 if (dwp_file->version == 2)
12579 bfd_map_over_sections (dwp_file->dbfd.get (),
12580 dwarf2_locate_v2_dwp_sections,
12581 dwp_file.get ());
12582
12583 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12584 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12585
12586 if (dwarf_read_debug)
12587 {
12588 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12589 fprintf_unfiltered (gdb_stdlog,
12590 " %s CUs, %s TUs\n",
12591 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12592 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12593 }
12594
12595 return dwp_file;
12596 }
12597
12598 /* Wrapper around open_and_init_dwp_file, only open it once. */
12599
12600 static struct dwp_file *
12601 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12602 {
12603 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12604 {
12605 dwarf2_per_objfile->per_bfd->dwp_file
12606 = open_and_init_dwp_file (dwarf2_per_objfile);
12607 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12608 }
12609 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12610 }
12611
12612 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12613 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12614 or in the DWP file for the objfile, referenced by THIS_UNIT.
12615 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12616 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12617
12618 This is called, for example, when wanting to read a variable with a
12619 complex location. Therefore we don't want to do file i/o for every call.
12620 Therefore we don't want to look for a DWO file on every call.
12621 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12622 then we check if we've already seen DWO_NAME, and only THEN do we check
12623 for a DWO file.
12624
12625 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12626 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12627
12628 static struct dwo_unit *
12629 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12630 const char *dwo_name, const char *comp_dir,
12631 ULONGEST signature, int is_debug_types)
12632 {
12633 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12634 struct objfile *objfile = dwarf2_per_objfile->objfile;
12635 const char *kind = is_debug_types ? "TU" : "CU";
12636 void **dwo_file_slot;
12637 struct dwo_file *dwo_file;
12638 struct dwp_file *dwp_file;
12639
12640 /* First see if there's a DWP file.
12641 If we have a DWP file but didn't find the DWO inside it, don't
12642 look for the original DWO file. It makes gdb behave differently
12643 depending on whether one is debugging in the build tree. */
12644
12645 dwp_file = get_dwp_file (dwarf2_per_objfile);
12646 if (dwp_file != NULL)
12647 {
12648 const struct dwp_hash_table *dwp_htab =
12649 is_debug_types ? dwp_file->tus : dwp_file->cus;
12650
12651 if (dwp_htab != NULL)
12652 {
12653 struct dwo_unit *dwo_cutu =
12654 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12655 signature, is_debug_types);
12656
12657 if (dwo_cutu != NULL)
12658 {
12659 if (dwarf_read_debug)
12660 {
12661 fprintf_unfiltered (gdb_stdlog,
12662 "Virtual DWO %s %s found: @%s\n",
12663 kind, hex_string (signature),
12664 host_address_to_string (dwo_cutu));
12665 }
12666 return dwo_cutu;
12667 }
12668 }
12669 }
12670 else
12671 {
12672 /* No DWP file, look for the DWO file. */
12673
12674 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12675 dwo_name, comp_dir);
12676 if (*dwo_file_slot == NULL)
12677 {
12678 /* Read in the file and build a table of the CUs/TUs it contains. */
12679 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12680 }
12681 /* NOTE: This will be NULL if unable to open the file. */
12682 dwo_file = (struct dwo_file *) *dwo_file_slot;
12683
12684 if (dwo_file != NULL)
12685 {
12686 struct dwo_unit *dwo_cutu = NULL;
12687
12688 if (is_debug_types && dwo_file->tus)
12689 {
12690 struct dwo_unit find_dwo_cutu;
12691
12692 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12693 find_dwo_cutu.signature = signature;
12694 dwo_cutu
12695 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12696 &find_dwo_cutu);
12697 }
12698 else if (!is_debug_types && dwo_file->cus)
12699 {
12700 struct dwo_unit find_dwo_cutu;
12701
12702 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12703 find_dwo_cutu.signature = signature;
12704 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12705 &find_dwo_cutu);
12706 }
12707
12708 if (dwo_cutu != NULL)
12709 {
12710 if (dwarf_read_debug)
12711 {
12712 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12713 kind, dwo_name, hex_string (signature),
12714 host_address_to_string (dwo_cutu));
12715 }
12716 return dwo_cutu;
12717 }
12718 }
12719 }
12720
12721 /* We didn't find it. This could mean a dwo_id mismatch, or
12722 someone deleted the DWO/DWP file, or the search path isn't set up
12723 correctly to find the file. */
12724
12725 if (dwarf_read_debug)
12726 {
12727 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12728 kind, dwo_name, hex_string (signature));
12729 }
12730
12731 /* This is a warning and not a complaint because it can be caused by
12732 pilot error (e.g., user accidentally deleting the DWO). */
12733 {
12734 /* Print the name of the DWP file if we looked there, helps the user
12735 better diagnose the problem. */
12736 std::string dwp_text;
12737
12738 if (dwp_file != NULL)
12739 dwp_text = string_printf (" [in DWP file %s]",
12740 lbasename (dwp_file->name));
12741
12742 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12743 " [in module %s]"),
12744 kind, dwo_name, hex_string (signature),
12745 dwp_text.c_str (),
12746 this_unit->is_debug_types ? "TU" : "CU",
12747 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12748 }
12749 return NULL;
12750 }
12751
12752 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12753 See lookup_dwo_cutu_unit for details. */
12754
12755 static struct dwo_unit *
12756 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12757 const char *dwo_name, const char *comp_dir,
12758 ULONGEST signature)
12759 {
12760 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12761 }
12762
12763 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12764 See lookup_dwo_cutu_unit for details. */
12765
12766 static struct dwo_unit *
12767 lookup_dwo_type_unit (struct signatured_type *this_tu,
12768 const char *dwo_name, const char *comp_dir)
12769 {
12770 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12771 }
12772
12773 /* Traversal function for queue_and_load_all_dwo_tus. */
12774
12775 static int
12776 queue_and_load_dwo_tu (void **slot, void *info)
12777 {
12778 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12779 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12780 ULONGEST signature = dwo_unit->signature;
12781 struct signatured_type *sig_type =
12782 lookup_dwo_signatured_type (per_cu->cu, signature);
12783
12784 if (sig_type != NULL)
12785 {
12786 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12787
12788 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12789 a real dependency of PER_CU on SIG_TYPE. That is detected later
12790 while processing PER_CU. */
12791 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12792 load_full_type_unit (sig_cu);
12793 per_cu->imported_symtabs_push (sig_cu);
12794 }
12795
12796 return 1;
12797 }
12798
12799 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12800 The DWO may have the only definition of the type, though it may not be
12801 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12802 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12803
12804 static void
12805 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12806 {
12807 struct dwo_unit *dwo_unit;
12808 struct dwo_file *dwo_file;
12809
12810 gdb_assert (!per_cu->is_debug_types);
12811 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12812 gdb_assert (per_cu->cu != NULL);
12813
12814 dwo_unit = per_cu->cu->dwo_unit;
12815 gdb_assert (dwo_unit != NULL);
12816
12817 dwo_file = dwo_unit->dwo_file;
12818 if (dwo_file->tus != NULL)
12819 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12820 per_cu);
12821 }
12822
12823 /* Read in various DIEs. */
12824
12825 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12826 Inherit only the children of the DW_AT_abstract_origin DIE not being
12827 already referenced by DW_AT_abstract_origin from the children of the
12828 current DIE. */
12829
12830 static void
12831 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12832 {
12833 struct die_info *child_die;
12834 sect_offset *offsetp;
12835 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12836 struct die_info *origin_die;
12837 /* Iterator of the ORIGIN_DIE children. */
12838 struct die_info *origin_child_die;
12839 struct attribute *attr;
12840 struct dwarf2_cu *origin_cu;
12841 struct pending **origin_previous_list_in_scope;
12842
12843 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12844 if (!attr)
12845 return;
12846
12847 /* Note that following die references may follow to a die in a
12848 different cu. */
12849
12850 origin_cu = cu;
12851 origin_die = follow_die_ref (die, attr, &origin_cu);
12852
12853 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12854 symbols in. */
12855 origin_previous_list_in_scope = origin_cu->list_in_scope;
12856 origin_cu->list_in_scope = cu->list_in_scope;
12857
12858 if (die->tag != origin_die->tag
12859 && !(die->tag == DW_TAG_inlined_subroutine
12860 && origin_die->tag == DW_TAG_subprogram))
12861 complaint (_("DIE %s and its abstract origin %s have different tags"),
12862 sect_offset_str (die->sect_off),
12863 sect_offset_str (origin_die->sect_off));
12864
12865 std::vector<sect_offset> offsets;
12866
12867 for (child_die = die->child;
12868 child_die && child_die->tag;
12869 child_die = child_die->sibling)
12870 {
12871 struct die_info *child_origin_die;
12872 struct dwarf2_cu *child_origin_cu;
12873
12874 /* We are trying to process concrete instance entries:
12875 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12876 it's not relevant to our analysis here. i.e. detecting DIEs that are
12877 present in the abstract instance but not referenced in the concrete
12878 one. */
12879 if (child_die->tag == DW_TAG_call_site
12880 || child_die->tag == DW_TAG_GNU_call_site)
12881 continue;
12882
12883 /* For each CHILD_DIE, find the corresponding child of
12884 ORIGIN_DIE. If there is more than one layer of
12885 DW_AT_abstract_origin, follow them all; there shouldn't be,
12886 but GCC versions at least through 4.4 generate this (GCC PR
12887 40573). */
12888 child_origin_die = child_die;
12889 child_origin_cu = cu;
12890 while (1)
12891 {
12892 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12893 child_origin_cu);
12894 if (attr == NULL)
12895 break;
12896 child_origin_die = follow_die_ref (child_origin_die, attr,
12897 &child_origin_cu);
12898 }
12899
12900 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12901 counterpart may exist. */
12902 if (child_origin_die != child_die)
12903 {
12904 if (child_die->tag != child_origin_die->tag
12905 && !(child_die->tag == DW_TAG_inlined_subroutine
12906 && child_origin_die->tag == DW_TAG_subprogram))
12907 complaint (_("Child DIE %s and its abstract origin %s have "
12908 "different tags"),
12909 sect_offset_str (child_die->sect_off),
12910 sect_offset_str (child_origin_die->sect_off));
12911 if (child_origin_die->parent != origin_die)
12912 complaint (_("Child DIE %s and its abstract origin %s have "
12913 "different parents"),
12914 sect_offset_str (child_die->sect_off),
12915 sect_offset_str (child_origin_die->sect_off));
12916 else
12917 offsets.push_back (child_origin_die->sect_off);
12918 }
12919 }
12920 std::sort (offsets.begin (), offsets.end ());
12921 sect_offset *offsets_end = offsets.data () + offsets.size ();
12922 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12923 if (offsetp[-1] == *offsetp)
12924 complaint (_("Multiple children of DIE %s refer "
12925 "to DIE %s as their abstract origin"),
12926 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12927
12928 offsetp = offsets.data ();
12929 origin_child_die = origin_die->child;
12930 while (origin_child_die && origin_child_die->tag)
12931 {
12932 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12933 while (offsetp < offsets_end
12934 && *offsetp < origin_child_die->sect_off)
12935 offsetp++;
12936 if (offsetp >= offsets_end
12937 || *offsetp > origin_child_die->sect_off)
12938 {
12939 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12940 Check whether we're already processing ORIGIN_CHILD_DIE.
12941 This can happen with mutually referenced abstract_origins.
12942 PR 16581. */
12943 if (!origin_child_die->in_process)
12944 process_die (origin_child_die, origin_cu);
12945 }
12946 origin_child_die = origin_child_die->sibling;
12947 }
12948 origin_cu->list_in_scope = origin_previous_list_in_scope;
12949
12950 if (cu != origin_cu)
12951 compute_delayed_physnames (origin_cu);
12952 }
12953
12954 static void
12955 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12956 {
12957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12958 struct gdbarch *gdbarch = objfile->arch ();
12959 struct context_stack *newobj;
12960 CORE_ADDR lowpc;
12961 CORE_ADDR highpc;
12962 struct die_info *child_die;
12963 struct attribute *attr, *call_line, *call_file;
12964 const char *name;
12965 CORE_ADDR baseaddr;
12966 struct block *block;
12967 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12968 std::vector<struct symbol *> template_args;
12969 struct template_symbol *templ_func = NULL;
12970
12971 if (inlined_func)
12972 {
12973 /* If we do not have call site information, we can't show the
12974 caller of this inlined function. That's too confusing, so
12975 only use the scope for local variables. */
12976 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12977 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12978 if (call_line == NULL || call_file == NULL)
12979 {
12980 read_lexical_block_scope (die, cu);
12981 return;
12982 }
12983 }
12984
12985 baseaddr = objfile->text_section_offset ();
12986
12987 name = dwarf2_name (die, cu);
12988
12989 /* Ignore functions with missing or empty names. These are actually
12990 illegal according to the DWARF standard. */
12991 if (name == NULL)
12992 {
12993 complaint (_("missing name for subprogram DIE at %s"),
12994 sect_offset_str (die->sect_off));
12995 return;
12996 }
12997
12998 /* Ignore functions with missing or invalid low and high pc attributes. */
12999 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13000 <= PC_BOUNDS_INVALID)
13001 {
13002 attr = dwarf2_attr (die, DW_AT_external, cu);
13003 if (!attr || !DW_UNSND (attr))
13004 complaint (_("cannot get low and high bounds "
13005 "for subprogram DIE at %s"),
13006 sect_offset_str (die->sect_off));
13007 return;
13008 }
13009
13010 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13011 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13012
13013 /* If we have any template arguments, then we must allocate a
13014 different sort of symbol. */
13015 for (child_die = die->child; child_die; child_die = child_die->sibling)
13016 {
13017 if (child_die->tag == DW_TAG_template_type_param
13018 || child_die->tag == DW_TAG_template_value_param)
13019 {
13020 templ_func = new (&objfile->objfile_obstack) template_symbol;
13021 templ_func->subclass = SYMBOL_TEMPLATE;
13022 break;
13023 }
13024 }
13025
13026 newobj = cu->get_builder ()->push_context (0, lowpc);
13027 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13028 (struct symbol *) templ_func);
13029
13030 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13031 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13032 cu->language);
13033
13034 /* If there is a location expression for DW_AT_frame_base, record
13035 it. */
13036 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13037 if (attr != nullptr)
13038 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13039
13040 /* If there is a location for the static link, record it. */
13041 newobj->static_link = NULL;
13042 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13043 if (attr != nullptr)
13044 {
13045 newobj->static_link
13046 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13047 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13048 cu->per_cu->addr_type ());
13049 }
13050
13051 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13052
13053 if (die->child != NULL)
13054 {
13055 child_die = die->child;
13056 while (child_die && child_die->tag)
13057 {
13058 if (child_die->tag == DW_TAG_template_type_param
13059 || child_die->tag == DW_TAG_template_value_param)
13060 {
13061 struct symbol *arg = new_symbol (child_die, NULL, cu);
13062
13063 if (arg != NULL)
13064 template_args.push_back (arg);
13065 }
13066 else
13067 process_die (child_die, cu);
13068 child_die = child_die->sibling;
13069 }
13070 }
13071
13072 inherit_abstract_dies (die, cu);
13073
13074 /* If we have a DW_AT_specification, we might need to import using
13075 directives from the context of the specification DIE. See the
13076 comment in determine_prefix. */
13077 if (cu->language == language_cplus
13078 && dwarf2_attr (die, DW_AT_specification, cu))
13079 {
13080 struct dwarf2_cu *spec_cu = cu;
13081 struct die_info *spec_die = die_specification (die, &spec_cu);
13082
13083 while (spec_die)
13084 {
13085 child_die = spec_die->child;
13086 while (child_die && child_die->tag)
13087 {
13088 if (child_die->tag == DW_TAG_imported_module)
13089 process_die (child_die, spec_cu);
13090 child_die = child_die->sibling;
13091 }
13092
13093 /* In some cases, GCC generates specification DIEs that
13094 themselves contain DW_AT_specification attributes. */
13095 spec_die = die_specification (spec_die, &spec_cu);
13096 }
13097 }
13098
13099 struct context_stack cstk = cu->get_builder ()->pop_context ();
13100 /* Make a block for the local symbols within. */
13101 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13102 cstk.static_link, lowpc, highpc);
13103
13104 /* For C++, set the block's scope. */
13105 if ((cu->language == language_cplus
13106 || cu->language == language_fortran
13107 || cu->language == language_d
13108 || cu->language == language_rust)
13109 && cu->processing_has_namespace_info)
13110 block_set_scope (block, determine_prefix (die, cu),
13111 &objfile->objfile_obstack);
13112
13113 /* If we have address ranges, record them. */
13114 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13115
13116 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13117
13118 /* Attach template arguments to function. */
13119 if (!template_args.empty ())
13120 {
13121 gdb_assert (templ_func != NULL);
13122
13123 templ_func->n_template_arguments = template_args.size ();
13124 templ_func->template_arguments
13125 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13126 templ_func->n_template_arguments);
13127 memcpy (templ_func->template_arguments,
13128 template_args.data (),
13129 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13130
13131 /* Make sure that the symtab is set on the new symbols. Even
13132 though they don't appear in this symtab directly, other parts
13133 of gdb assume that symbols do, and this is reasonably
13134 true. */
13135 for (symbol *sym : template_args)
13136 symbol_set_symtab (sym, symbol_symtab (templ_func));
13137 }
13138
13139 /* In C++, we can have functions nested inside functions (e.g., when
13140 a function declares a class that has methods). This means that
13141 when we finish processing a function scope, we may need to go
13142 back to building a containing block's symbol lists. */
13143 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13144 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13145
13146 /* If we've finished processing a top-level function, subsequent
13147 symbols go in the file symbol list. */
13148 if (cu->get_builder ()->outermost_context_p ())
13149 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13150 }
13151
13152 /* Process all the DIES contained within a lexical block scope. Start
13153 a new scope, process the dies, and then close the scope. */
13154
13155 static void
13156 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13157 {
13158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13159 struct gdbarch *gdbarch = objfile->arch ();
13160 CORE_ADDR lowpc, highpc;
13161 struct die_info *child_die;
13162 CORE_ADDR baseaddr;
13163
13164 baseaddr = objfile->text_section_offset ();
13165
13166 /* Ignore blocks with missing or invalid low and high pc attributes. */
13167 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13168 as multiple lexical blocks? Handling children in a sane way would
13169 be nasty. Might be easier to properly extend generic blocks to
13170 describe ranges. */
13171 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13172 {
13173 case PC_BOUNDS_NOT_PRESENT:
13174 /* DW_TAG_lexical_block has no attributes, process its children as if
13175 there was no wrapping by that DW_TAG_lexical_block.
13176 GCC does no longer produces such DWARF since GCC r224161. */
13177 for (child_die = die->child;
13178 child_die != NULL && child_die->tag;
13179 child_die = child_die->sibling)
13180 {
13181 /* We might already be processing this DIE. This can happen
13182 in an unusual circumstance -- where a subroutine A
13183 appears lexically in another subroutine B, but A actually
13184 inlines B. The recursion is broken here, rather than in
13185 inherit_abstract_dies, because it seems better to simply
13186 drop concrete children here. */
13187 if (!child_die->in_process)
13188 process_die (child_die, cu);
13189 }
13190 return;
13191 case PC_BOUNDS_INVALID:
13192 return;
13193 }
13194 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13195 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13196
13197 cu->get_builder ()->push_context (0, lowpc);
13198 if (die->child != NULL)
13199 {
13200 child_die = die->child;
13201 while (child_die && child_die->tag)
13202 {
13203 process_die (child_die, cu);
13204 child_die = child_die->sibling;
13205 }
13206 }
13207 inherit_abstract_dies (die, cu);
13208 struct context_stack cstk = cu->get_builder ()->pop_context ();
13209
13210 if (*cu->get_builder ()->get_local_symbols () != NULL
13211 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13212 {
13213 struct block *block
13214 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13215 cstk.start_addr, highpc);
13216
13217 /* Note that recording ranges after traversing children, as we
13218 do here, means that recording a parent's ranges entails
13219 walking across all its children's ranges as they appear in
13220 the address map, which is quadratic behavior.
13221
13222 It would be nicer to record the parent's ranges before
13223 traversing its children, simply overriding whatever you find
13224 there. But since we don't even decide whether to create a
13225 block until after we've traversed its children, that's hard
13226 to do. */
13227 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13228 }
13229 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13230 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13231 }
13232
13233 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13234
13235 static void
13236 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13237 {
13238 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
13239 struct objfile *objfile = per_objfile->objfile;
13240 struct gdbarch *gdbarch = objfile->arch ();
13241 CORE_ADDR pc, baseaddr;
13242 struct attribute *attr;
13243 struct call_site *call_site, call_site_local;
13244 void **slot;
13245 int nparams;
13246 struct die_info *child_die;
13247
13248 baseaddr = objfile->text_section_offset ();
13249
13250 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13251 if (attr == NULL)
13252 {
13253 /* This was a pre-DWARF-5 GNU extension alias
13254 for DW_AT_call_return_pc. */
13255 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13256 }
13257 if (!attr)
13258 {
13259 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13260 "DIE %s [in module %s]"),
13261 sect_offset_str (die->sect_off), objfile_name (objfile));
13262 return;
13263 }
13264 pc = attr->value_as_address () + baseaddr;
13265 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13266
13267 if (cu->call_site_htab == NULL)
13268 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13269 NULL, &objfile->objfile_obstack,
13270 hashtab_obstack_allocate, NULL);
13271 call_site_local.pc = pc;
13272 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13273 if (*slot != NULL)
13274 {
13275 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13276 "DIE %s [in module %s]"),
13277 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13278 objfile_name (objfile));
13279 return;
13280 }
13281
13282 /* Count parameters at the caller. */
13283
13284 nparams = 0;
13285 for (child_die = die->child; child_die && child_die->tag;
13286 child_die = child_die->sibling)
13287 {
13288 if (child_die->tag != DW_TAG_call_site_parameter
13289 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13290 {
13291 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13292 "DW_TAG_call_site child DIE %s [in module %s]"),
13293 child_die->tag, sect_offset_str (child_die->sect_off),
13294 objfile_name (objfile));
13295 continue;
13296 }
13297
13298 nparams++;
13299 }
13300
13301 call_site
13302 = ((struct call_site *)
13303 obstack_alloc (&objfile->objfile_obstack,
13304 sizeof (*call_site)
13305 + (sizeof (*call_site->parameter) * (nparams - 1))));
13306 *slot = call_site;
13307 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13308 call_site->pc = pc;
13309
13310 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13311 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13312 {
13313 struct die_info *func_die;
13314
13315 /* Skip also over DW_TAG_inlined_subroutine. */
13316 for (func_die = die->parent;
13317 func_die && func_die->tag != DW_TAG_subprogram
13318 && func_die->tag != DW_TAG_subroutine_type;
13319 func_die = func_die->parent);
13320
13321 /* DW_AT_call_all_calls is a superset
13322 of DW_AT_call_all_tail_calls. */
13323 if (func_die
13324 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13325 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13326 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13327 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13328 {
13329 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13330 not complete. But keep CALL_SITE for look ups via call_site_htab,
13331 both the initial caller containing the real return address PC and
13332 the final callee containing the current PC of a chain of tail
13333 calls do not need to have the tail call list complete. But any
13334 function candidate for a virtual tail call frame searched via
13335 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13336 determined unambiguously. */
13337 }
13338 else
13339 {
13340 struct type *func_type = NULL;
13341
13342 if (func_die)
13343 func_type = get_die_type (func_die, cu);
13344 if (func_type != NULL)
13345 {
13346 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13347
13348 /* Enlist this call site to the function. */
13349 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13350 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13351 }
13352 else
13353 complaint (_("Cannot find function owning DW_TAG_call_site "
13354 "DIE %s [in module %s]"),
13355 sect_offset_str (die->sect_off), objfile_name (objfile));
13356 }
13357 }
13358
13359 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13360 if (attr == NULL)
13361 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13362 if (attr == NULL)
13363 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13364 if (attr == NULL)
13365 {
13366 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13367 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13368 }
13369 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13370 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13371 /* Keep NULL DWARF_BLOCK. */;
13372 else if (attr->form_is_block ())
13373 {
13374 struct dwarf2_locexpr_baton *dlbaton;
13375
13376 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13377 dlbaton->data = DW_BLOCK (attr)->data;
13378 dlbaton->size = DW_BLOCK (attr)->size;
13379 dlbaton->per_objfile = per_objfile;
13380 dlbaton->per_cu = cu->per_cu;
13381
13382 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13383 }
13384 else if (attr->form_is_ref ())
13385 {
13386 struct dwarf2_cu *target_cu = cu;
13387 struct die_info *target_die;
13388
13389 target_die = follow_die_ref (die, attr, &target_cu);
13390 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13391 if (die_is_declaration (target_die, target_cu))
13392 {
13393 const char *target_physname;
13394
13395 /* Prefer the mangled name; otherwise compute the demangled one. */
13396 target_physname = dw2_linkage_name (target_die, target_cu);
13397 if (target_physname == NULL)
13398 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13399 if (target_physname == NULL)
13400 complaint (_("DW_AT_call_target target DIE has invalid "
13401 "physname, for referencing DIE %s [in module %s]"),
13402 sect_offset_str (die->sect_off), objfile_name (objfile));
13403 else
13404 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13405 }
13406 else
13407 {
13408 CORE_ADDR lowpc;
13409
13410 /* DW_AT_entry_pc should be preferred. */
13411 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13412 <= PC_BOUNDS_INVALID)
13413 complaint (_("DW_AT_call_target target DIE has invalid "
13414 "low pc, for referencing DIE %s [in module %s]"),
13415 sect_offset_str (die->sect_off), objfile_name (objfile));
13416 else
13417 {
13418 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13419 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13420 }
13421 }
13422 }
13423 else
13424 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13425 "block nor reference, for DIE %s [in module %s]"),
13426 sect_offset_str (die->sect_off), objfile_name (objfile));
13427
13428 call_site->per_cu = cu->per_cu;
13429
13430 for (child_die = die->child;
13431 child_die && child_die->tag;
13432 child_die = child_die->sibling)
13433 {
13434 struct call_site_parameter *parameter;
13435 struct attribute *loc, *origin;
13436
13437 if (child_die->tag != DW_TAG_call_site_parameter
13438 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13439 {
13440 /* Already printed the complaint above. */
13441 continue;
13442 }
13443
13444 gdb_assert (call_site->parameter_count < nparams);
13445 parameter = &call_site->parameter[call_site->parameter_count];
13446
13447 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13448 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13449 register is contained in DW_AT_call_value. */
13450
13451 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13452 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13453 if (origin == NULL)
13454 {
13455 /* This was a pre-DWARF-5 GNU extension alias
13456 for DW_AT_call_parameter. */
13457 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13458 }
13459 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13460 {
13461 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13462
13463 sect_offset sect_off = origin->get_ref_die_offset ();
13464 if (!cu->header.offset_in_cu_p (sect_off))
13465 {
13466 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13467 binding can be done only inside one CU. Such referenced DIE
13468 therefore cannot be even moved to DW_TAG_partial_unit. */
13469 complaint (_("DW_AT_call_parameter offset is not in CU for "
13470 "DW_TAG_call_site child DIE %s [in module %s]"),
13471 sect_offset_str (child_die->sect_off),
13472 objfile_name (objfile));
13473 continue;
13474 }
13475 parameter->u.param_cu_off
13476 = (cu_offset) (sect_off - cu->header.sect_off);
13477 }
13478 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13479 {
13480 complaint (_("No DW_FORM_block* DW_AT_location for "
13481 "DW_TAG_call_site child DIE %s [in module %s]"),
13482 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13483 continue;
13484 }
13485 else
13486 {
13487 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13488 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13489 if (parameter->u.dwarf_reg != -1)
13490 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13491 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13492 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13493 &parameter->u.fb_offset))
13494 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13495 else
13496 {
13497 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13498 "for DW_FORM_block* DW_AT_location is supported for "
13499 "DW_TAG_call_site child DIE %s "
13500 "[in module %s]"),
13501 sect_offset_str (child_die->sect_off),
13502 objfile_name (objfile));
13503 continue;
13504 }
13505 }
13506
13507 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13508 if (attr == NULL)
13509 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13510 if (attr == NULL || !attr->form_is_block ())
13511 {
13512 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13513 "DW_TAG_call_site child DIE %s [in module %s]"),
13514 sect_offset_str (child_die->sect_off),
13515 objfile_name (objfile));
13516 continue;
13517 }
13518 parameter->value = DW_BLOCK (attr)->data;
13519 parameter->value_size = DW_BLOCK (attr)->size;
13520
13521 /* Parameters are not pre-cleared by memset above. */
13522 parameter->data_value = NULL;
13523 parameter->data_value_size = 0;
13524 call_site->parameter_count++;
13525
13526 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13527 if (attr == NULL)
13528 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13529 if (attr != nullptr)
13530 {
13531 if (!attr->form_is_block ())
13532 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13533 "DW_TAG_call_site child DIE %s [in module %s]"),
13534 sect_offset_str (child_die->sect_off),
13535 objfile_name (objfile));
13536 else
13537 {
13538 parameter->data_value = DW_BLOCK (attr)->data;
13539 parameter->data_value_size = DW_BLOCK (attr)->size;
13540 }
13541 }
13542 }
13543 }
13544
13545 /* Helper function for read_variable. If DIE represents a virtual
13546 table, then return the type of the concrete object that is
13547 associated with the virtual table. Otherwise, return NULL. */
13548
13549 static struct type *
13550 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13551 {
13552 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13553 if (attr == NULL)
13554 return NULL;
13555
13556 /* Find the type DIE. */
13557 struct die_info *type_die = NULL;
13558 struct dwarf2_cu *type_cu = cu;
13559
13560 if (attr->form_is_ref ())
13561 type_die = follow_die_ref (die, attr, &type_cu);
13562 if (type_die == NULL)
13563 return NULL;
13564
13565 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13566 return NULL;
13567 return die_containing_type (type_die, type_cu);
13568 }
13569
13570 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13571
13572 static void
13573 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13574 {
13575 struct rust_vtable_symbol *storage = NULL;
13576
13577 if (cu->language == language_rust)
13578 {
13579 struct type *containing_type = rust_containing_type (die, cu);
13580
13581 if (containing_type != NULL)
13582 {
13583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13584
13585 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13586 storage->concrete_type = containing_type;
13587 storage->subclass = SYMBOL_RUST_VTABLE;
13588 }
13589 }
13590
13591 struct symbol *res = new_symbol (die, NULL, cu, storage);
13592 struct attribute *abstract_origin
13593 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13594 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13595 if (res == NULL && loc && abstract_origin)
13596 {
13597 /* We have a variable without a name, but with a location and an abstract
13598 origin. This may be a concrete instance of an abstract variable
13599 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13600 later. */
13601 struct dwarf2_cu *origin_cu = cu;
13602 struct die_info *origin_die
13603 = follow_die_ref (die, abstract_origin, &origin_cu);
13604 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13605 dpo->per_bfd->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13606 }
13607 }
13608
13609 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13610 reading .debug_rnglists.
13611 Callback's type should be:
13612 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13613 Return true if the attributes are present and valid, otherwise,
13614 return false. */
13615
13616 template <typename Callback>
13617 static bool
13618 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13619 Callback &&callback)
13620 {
13621 struct dwarf2_per_objfile *dwarf2_per_objfile
13622 = cu->per_cu->dwarf2_per_objfile;
13623 struct objfile *objfile = dwarf2_per_objfile->objfile;
13624 bfd *obfd = objfile->obfd;
13625 /* Base address selection entry. */
13626 gdb::optional<CORE_ADDR> base;
13627 const gdb_byte *buffer;
13628 CORE_ADDR baseaddr;
13629 bool overflow = false;
13630
13631 base = cu->base_address;
13632
13633 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13634 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13635 {
13636 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13637 offset);
13638 return false;
13639 }
13640 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13641
13642 baseaddr = objfile->text_section_offset ();
13643
13644 while (1)
13645 {
13646 /* Initialize it due to a false compiler warning. */
13647 CORE_ADDR range_beginning = 0, range_end = 0;
13648 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13649 + dwarf2_per_objfile->per_bfd->rnglists.size);
13650 unsigned int bytes_read;
13651
13652 if (buffer == buf_end)
13653 {
13654 overflow = true;
13655 break;
13656 }
13657 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13658 switch (rlet)
13659 {
13660 case DW_RLE_end_of_list:
13661 break;
13662 case DW_RLE_base_address:
13663 if (buffer + cu->header.addr_size > buf_end)
13664 {
13665 overflow = true;
13666 break;
13667 }
13668 base = cu->header.read_address (obfd, buffer, &bytes_read);
13669 buffer += bytes_read;
13670 break;
13671 case DW_RLE_start_length:
13672 if (buffer + cu->header.addr_size > buf_end)
13673 {
13674 overflow = true;
13675 break;
13676 }
13677 range_beginning = cu->header.read_address (obfd, buffer,
13678 &bytes_read);
13679 buffer += bytes_read;
13680 range_end = (range_beginning
13681 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13682 buffer += bytes_read;
13683 if (buffer > buf_end)
13684 {
13685 overflow = true;
13686 break;
13687 }
13688 break;
13689 case DW_RLE_offset_pair:
13690 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13691 buffer += bytes_read;
13692 if (buffer > buf_end)
13693 {
13694 overflow = true;
13695 break;
13696 }
13697 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13698 buffer += bytes_read;
13699 if (buffer > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
13704 break;
13705 case DW_RLE_start_end:
13706 if (buffer + 2 * cu->header.addr_size > buf_end)
13707 {
13708 overflow = true;
13709 break;
13710 }
13711 range_beginning = cu->header.read_address (obfd, buffer,
13712 &bytes_read);
13713 buffer += bytes_read;
13714 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13715 buffer += bytes_read;
13716 break;
13717 default:
13718 complaint (_("Invalid .debug_rnglists data (no base address)"));
13719 return false;
13720 }
13721 if (rlet == DW_RLE_end_of_list || overflow)
13722 break;
13723 if (rlet == DW_RLE_base_address)
13724 continue;
13725
13726 if (!base.has_value ())
13727 {
13728 /* We have no valid base address for the ranges
13729 data. */
13730 complaint (_("Invalid .debug_rnglists data (no base address)"));
13731 return false;
13732 }
13733
13734 if (range_beginning > range_end)
13735 {
13736 /* Inverted range entries are invalid. */
13737 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13738 return false;
13739 }
13740
13741 /* Empty range entries have no effect. */
13742 if (range_beginning == range_end)
13743 continue;
13744
13745 range_beginning += *base;
13746 range_end += *base;
13747
13748 /* A not-uncommon case of bad debug info.
13749 Don't pollute the addrmap with bad data. */
13750 if (range_beginning + baseaddr == 0
13751 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13752 {
13753 complaint (_(".debug_rnglists entry has start address of zero"
13754 " [in module %s]"), objfile_name (objfile));
13755 continue;
13756 }
13757
13758 callback (range_beginning, range_end);
13759 }
13760
13761 if (overflow)
13762 {
13763 complaint (_("Offset %d is not terminated "
13764 "for DW_AT_ranges attribute"),
13765 offset);
13766 return false;
13767 }
13768
13769 return true;
13770 }
13771
13772 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13773 Callback's type should be:
13774 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13775 Return 1 if the attributes are present and valid, otherwise, return 0. */
13776
13777 template <typename Callback>
13778 static int
13779 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13780 Callback &&callback)
13781 {
13782 struct dwarf2_per_objfile *dwarf2_per_objfile
13783 = cu->per_cu->dwarf2_per_objfile;
13784 struct objfile *objfile = dwarf2_per_objfile->objfile;
13785 struct comp_unit_head *cu_header = &cu->header;
13786 bfd *obfd = objfile->obfd;
13787 unsigned int addr_size = cu_header->addr_size;
13788 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13789 /* Base address selection entry. */
13790 gdb::optional<CORE_ADDR> base;
13791 unsigned int dummy;
13792 const gdb_byte *buffer;
13793 CORE_ADDR baseaddr;
13794
13795 if (cu_header->version >= 5)
13796 return dwarf2_rnglists_process (offset, cu, callback);
13797
13798 base = cu->base_address;
13799
13800 dwarf2_per_objfile->per_bfd->ranges.read (objfile);
13801 if (offset >= dwarf2_per_objfile->per_bfd->ranges.size)
13802 {
13803 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13804 offset);
13805 return 0;
13806 }
13807 buffer = dwarf2_per_objfile->per_bfd->ranges.buffer + offset;
13808
13809 baseaddr = objfile->text_section_offset ();
13810
13811 while (1)
13812 {
13813 CORE_ADDR range_beginning, range_end;
13814
13815 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13816 buffer += addr_size;
13817 range_end = cu->header.read_address (obfd, buffer, &dummy);
13818 buffer += addr_size;
13819 offset += 2 * addr_size;
13820
13821 /* An end of list marker is a pair of zero addresses. */
13822 if (range_beginning == 0 && range_end == 0)
13823 /* Found the end of list entry. */
13824 break;
13825
13826 /* Each base address selection entry is a pair of 2 values.
13827 The first is the largest possible address, the second is
13828 the base address. Check for a base address here. */
13829 if ((range_beginning & mask) == mask)
13830 {
13831 /* If we found the largest possible address, then we already
13832 have the base address in range_end. */
13833 base = range_end;
13834 continue;
13835 }
13836
13837 if (!base.has_value ())
13838 {
13839 /* We have no valid base address for the ranges
13840 data. */
13841 complaint (_("Invalid .debug_ranges data (no base address)"));
13842 return 0;
13843 }
13844
13845 if (range_beginning > range_end)
13846 {
13847 /* Inverted range entries are invalid. */
13848 complaint (_("Invalid .debug_ranges data (inverted range)"));
13849 return 0;
13850 }
13851
13852 /* Empty range entries have no effect. */
13853 if (range_beginning == range_end)
13854 continue;
13855
13856 range_beginning += *base;
13857 range_end += *base;
13858
13859 /* A not-uncommon case of bad debug info.
13860 Don't pollute the addrmap with bad data. */
13861 if (range_beginning + baseaddr == 0
13862 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13863 {
13864 complaint (_(".debug_ranges entry has start address of zero"
13865 " [in module %s]"), objfile_name (objfile));
13866 continue;
13867 }
13868
13869 callback (range_beginning, range_end);
13870 }
13871
13872 return 1;
13873 }
13874
13875 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13876 Return 1 if the attributes are present and valid, otherwise, return 0.
13877 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13878
13879 static int
13880 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13881 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13882 dwarf2_psymtab *ranges_pst)
13883 {
13884 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13885 struct gdbarch *gdbarch = objfile->arch ();
13886 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13887 int low_set = 0;
13888 CORE_ADDR low = 0;
13889 CORE_ADDR high = 0;
13890 int retval;
13891
13892 retval = dwarf2_ranges_process (offset, cu,
13893 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13894 {
13895 if (ranges_pst != NULL)
13896 {
13897 CORE_ADDR lowpc;
13898 CORE_ADDR highpc;
13899
13900 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13901 range_beginning + baseaddr)
13902 - baseaddr);
13903 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13904 range_end + baseaddr)
13905 - baseaddr);
13906 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13907 lowpc, highpc - 1, ranges_pst);
13908 }
13909
13910 /* FIXME: This is recording everything as a low-high
13911 segment of consecutive addresses. We should have a
13912 data structure for discontiguous block ranges
13913 instead. */
13914 if (! low_set)
13915 {
13916 low = range_beginning;
13917 high = range_end;
13918 low_set = 1;
13919 }
13920 else
13921 {
13922 if (range_beginning < low)
13923 low = range_beginning;
13924 if (range_end > high)
13925 high = range_end;
13926 }
13927 });
13928 if (!retval)
13929 return 0;
13930
13931 if (! low_set)
13932 /* If the first entry is an end-of-list marker, the range
13933 describes an empty scope, i.e. no instructions. */
13934 return 0;
13935
13936 if (low_return)
13937 *low_return = low;
13938 if (high_return)
13939 *high_return = high;
13940 return 1;
13941 }
13942
13943 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13944 definition for the return value. *LOWPC and *HIGHPC are set iff
13945 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13946
13947 static enum pc_bounds_kind
13948 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13949 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13950 dwarf2_psymtab *pst)
13951 {
13952 struct dwarf2_per_objfile *dwarf2_per_objfile
13953 = cu->per_cu->dwarf2_per_objfile;
13954 struct attribute *attr;
13955 struct attribute *attr_high;
13956 CORE_ADDR low = 0;
13957 CORE_ADDR high = 0;
13958 enum pc_bounds_kind ret;
13959
13960 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13961 if (attr_high)
13962 {
13963 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13964 if (attr != nullptr)
13965 {
13966 low = attr->value_as_address ();
13967 high = attr_high->value_as_address ();
13968 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13969 high += low;
13970 }
13971 else
13972 /* Found high w/o low attribute. */
13973 return PC_BOUNDS_INVALID;
13974
13975 /* Found consecutive range of addresses. */
13976 ret = PC_BOUNDS_HIGH_LOW;
13977 }
13978 else
13979 {
13980 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13981 if (attr != NULL)
13982 {
13983 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13984 We take advantage of the fact that DW_AT_ranges does not appear
13985 in DW_TAG_compile_unit of DWO files. */
13986 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13987 unsigned int ranges_offset = (DW_UNSND (attr)
13988 + (need_ranges_base
13989 ? cu->ranges_base
13990 : 0));
13991
13992 /* Value of the DW_AT_ranges attribute is the offset in the
13993 .debug_ranges section. */
13994 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13995 return PC_BOUNDS_INVALID;
13996 /* Found discontinuous range of addresses. */
13997 ret = PC_BOUNDS_RANGES;
13998 }
13999 else
14000 return PC_BOUNDS_NOT_PRESENT;
14001 }
14002
14003 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14004 if (high <= low)
14005 return PC_BOUNDS_INVALID;
14006
14007 /* When using the GNU linker, .gnu.linkonce. sections are used to
14008 eliminate duplicate copies of functions and vtables and such.
14009 The linker will arbitrarily choose one and discard the others.
14010 The AT_*_pc values for such functions refer to local labels in
14011 these sections. If the section from that file was discarded, the
14012 labels are not in the output, so the relocs get a value of 0.
14013 If this is a discarded function, mark the pc bounds as invalid,
14014 so that GDB will ignore it. */
14015 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14016 return PC_BOUNDS_INVALID;
14017
14018 *lowpc = low;
14019 if (highpc)
14020 *highpc = high;
14021 return ret;
14022 }
14023
14024 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14025 its low and high PC addresses. Do nothing if these addresses could not
14026 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14027 and HIGHPC to the high address if greater than HIGHPC. */
14028
14029 static void
14030 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14032 struct dwarf2_cu *cu)
14033 {
14034 CORE_ADDR low, high;
14035 struct die_info *child = die->child;
14036
14037 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14038 {
14039 *lowpc = std::min (*lowpc, low);
14040 *highpc = std::max (*highpc, high);
14041 }
14042
14043 /* If the language does not allow nested subprograms (either inside
14044 subprograms or lexical blocks), we're done. */
14045 if (cu->language != language_ada)
14046 return;
14047
14048 /* Check all the children of the given DIE. If it contains nested
14049 subprograms, then check their pc bounds. Likewise, we need to
14050 check lexical blocks as well, as they may also contain subprogram
14051 definitions. */
14052 while (child && child->tag)
14053 {
14054 if (child->tag == DW_TAG_subprogram
14055 || child->tag == DW_TAG_lexical_block)
14056 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14057 child = child->sibling;
14058 }
14059 }
14060
14061 /* Get the low and high pc's represented by the scope DIE, and store
14062 them in *LOWPC and *HIGHPC. If the correct values can't be
14063 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14064
14065 static void
14066 get_scope_pc_bounds (struct die_info *die,
14067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14068 struct dwarf2_cu *cu)
14069 {
14070 CORE_ADDR best_low = (CORE_ADDR) -1;
14071 CORE_ADDR best_high = (CORE_ADDR) 0;
14072 CORE_ADDR current_low, current_high;
14073
14074 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14075 >= PC_BOUNDS_RANGES)
14076 {
14077 best_low = current_low;
14078 best_high = current_high;
14079 }
14080 else
14081 {
14082 struct die_info *child = die->child;
14083
14084 while (child && child->tag)
14085 {
14086 switch (child->tag) {
14087 case DW_TAG_subprogram:
14088 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14089 break;
14090 case DW_TAG_namespace:
14091 case DW_TAG_module:
14092 /* FIXME: carlton/2004-01-16: Should we do this for
14093 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14094 that current GCC's always emit the DIEs corresponding
14095 to definitions of methods of classes as children of a
14096 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14097 the DIEs giving the declarations, which could be
14098 anywhere). But I don't see any reason why the
14099 standards says that they have to be there. */
14100 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14101
14102 if (current_low != ((CORE_ADDR) -1))
14103 {
14104 best_low = std::min (best_low, current_low);
14105 best_high = std::max (best_high, current_high);
14106 }
14107 break;
14108 default:
14109 /* Ignore. */
14110 break;
14111 }
14112
14113 child = child->sibling;
14114 }
14115 }
14116
14117 *lowpc = best_low;
14118 *highpc = best_high;
14119 }
14120
14121 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14122 in DIE. */
14123
14124 static void
14125 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14126 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14127 {
14128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14129 struct gdbarch *gdbarch = objfile->arch ();
14130 struct attribute *attr;
14131 struct attribute *attr_high;
14132
14133 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14134 if (attr_high)
14135 {
14136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14137 if (attr != nullptr)
14138 {
14139 CORE_ADDR low = attr->value_as_address ();
14140 CORE_ADDR high = attr_high->value_as_address ();
14141
14142 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14143 high += low;
14144
14145 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14146 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14147 cu->get_builder ()->record_block_range (block, low, high - 1);
14148 }
14149 }
14150
14151 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14152 if (attr != nullptr)
14153 {
14154 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14155 We take advantage of the fact that DW_AT_ranges does not appear
14156 in DW_TAG_compile_unit of DWO files. */
14157 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14158
14159 /* The value of the DW_AT_ranges attribute is the offset of the
14160 address range list in the .debug_ranges section. */
14161 unsigned long offset = (DW_UNSND (attr)
14162 + (need_ranges_base ? cu->ranges_base : 0));
14163
14164 std::vector<blockrange> blockvec;
14165 dwarf2_ranges_process (offset, cu,
14166 [&] (CORE_ADDR start, CORE_ADDR end)
14167 {
14168 start += baseaddr;
14169 end += baseaddr;
14170 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14171 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14172 cu->get_builder ()->record_block_range (block, start, end - 1);
14173 blockvec.emplace_back (start, end);
14174 });
14175
14176 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14177 }
14178 }
14179
14180 /* Check whether the producer field indicates either of GCC < 4.6, or the
14181 Intel C/C++ compiler, and cache the result in CU. */
14182
14183 static void
14184 check_producer (struct dwarf2_cu *cu)
14185 {
14186 int major, minor;
14187
14188 if (cu->producer == NULL)
14189 {
14190 /* For unknown compilers expect their behavior is DWARF version
14191 compliant.
14192
14193 GCC started to support .debug_types sections by -gdwarf-4 since
14194 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14195 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14196 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14197 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14198 }
14199 else if (producer_is_gcc (cu->producer, &major, &minor))
14200 {
14201 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14202 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14203 }
14204 else if (producer_is_icc (cu->producer, &major, &minor))
14205 {
14206 cu->producer_is_icc = true;
14207 cu->producer_is_icc_lt_14 = major < 14;
14208 }
14209 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14210 cu->producer_is_codewarrior = true;
14211 else
14212 {
14213 /* For other non-GCC compilers, expect their behavior is DWARF version
14214 compliant. */
14215 }
14216
14217 cu->checked_producer = true;
14218 }
14219
14220 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14221 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14222 during 4.6.0 experimental. */
14223
14224 static bool
14225 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14226 {
14227 if (!cu->checked_producer)
14228 check_producer (cu);
14229
14230 return cu->producer_is_gxx_lt_4_6;
14231 }
14232
14233
14234 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14235 with incorrect is_stmt attributes. */
14236
14237 static bool
14238 producer_is_codewarrior (struct dwarf2_cu *cu)
14239 {
14240 if (!cu->checked_producer)
14241 check_producer (cu);
14242
14243 return cu->producer_is_codewarrior;
14244 }
14245
14246 /* Return the default accessibility type if it is not overridden by
14247 DW_AT_accessibility. */
14248
14249 static enum dwarf_access_attribute
14250 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14251 {
14252 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14253 {
14254 /* The default DWARF 2 accessibility for members is public, the default
14255 accessibility for inheritance is private. */
14256
14257 if (die->tag != DW_TAG_inheritance)
14258 return DW_ACCESS_public;
14259 else
14260 return DW_ACCESS_private;
14261 }
14262 else
14263 {
14264 /* DWARF 3+ defines the default accessibility a different way. The same
14265 rules apply now for DW_TAG_inheritance as for the members and it only
14266 depends on the container kind. */
14267
14268 if (die->parent->tag == DW_TAG_class_type)
14269 return DW_ACCESS_private;
14270 else
14271 return DW_ACCESS_public;
14272 }
14273 }
14274
14275 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14276 offset. If the attribute was not found return 0, otherwise return
14277 1. If it was found but could not properly be handled, set *OFFSET
14278 to 0. */
14279
14280 static int
14281 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14282 LONGEST *offset)
14283 {
14284 struct attribute *attr;
14285
14286 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14287 if (attr != NULL)
14288 {
14289 *offset = 0;
14290
14291 /* Note that we do not check for a section offset first here.
14292 This is because DW_AT_data_member_location is new in DWARF 4,
14293 so if we see it, we can assume that a constant form is really
14294 a constant and not a section offset. */
14295 if (attr->form_is_constant ())
14296 *offset = attr->constant_value (0);
14297 else if (attr->form_is_section_offset ())
14298 dwarf2_complex_location_expr_complaint ();
14299 else if (attr->form_is_block ())
14300 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14301 else
14302 dwarf2_complex_location_expr_complaint ();
14303
14304 return 1;
14305 }
14306
14307 return 0;
14308 }
14309
14310 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14311
14312 static void
14313 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14314 struct field *field)
14315 {
14316 struct attribute *attr;
14317
14318 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14319 if (attr != NULL)
14320 {
14321 if (attr->form_is_constant ())
14322 {
14323 LONGEST offset = attr->constant_value (0);
14324 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14325 }
14326 else if (attr->form_is_section_offset ())
14327 dwarf2_complex_location_expr_complaint ();
14328 else if (attr->form_is_block ())
14329 {
14330 bool handled;
14331 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14332 if (handled)
14333 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14334 else
14335 {
14336 struct objfile *objfile
14337 = cu->per_cu->dwarf2_per_objfile->objfile;
14338 struct dwarf2_locexpr_baton *dlbaton
14339 = XOBNEW (&objfile->objfile_obstack,
14340 struct dwarf2_locexpr_baton);
14341 dlbaton->data = DW_BLOCK (attr)->data;
14342 dlbaton->size = DW_BLOCK (attr)->size;
14343 /* When using this baton, we want to compute the address
14344 of the field, not the value. This is why
14345 is_reference is set to false here. */
14346 dlbaton->is_reference = false;
14347 dlbaton->per_objfile = cu->per_cu->dwarf2_per_objfile;
14348 dlbaton->per_cu = cu->per_cu;
14349
14350 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14351 }
14352 }
14353 else
14354 dwarf2_complex_location_expr_complaint ();
14355 }
14356 }
14357
14358 /* Add an aggregate field to the field list. */
14359
14360 static void
14361 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14362 struct dwarf2_cu *cu)
14363 {
14364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14365 struct gdbarch *gdbarch = objfile->arch ();
14366 struct nextfield *new_field;
14367 struct attribute *attr;
14368 struct field *fp;
14369 const char *fieldname = "";
14370
14371 if (die->tag == DW_TAG_inheritance)
14372 {
14373 fip->baseclasses.emplace_back ();
14374 new_field = &fip->baseclasses.back ();
14375 }
14376 else
14377 {
14378 fip->fields.emplace_back ();
14379 new_field = &fip->fields.back ();
14380 }
14381
14382 new_field->offset = die->sect_off;
14383
14384 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14385 if (attr != nullptr)
14386 new_field->accessibility = DW_UNSND (attr);
14387 else
14388 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14389 if (new_field->accessibility != DW_ACCESS_public)
14390 fip->non_public_fields = 1;
14391
14392 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14393 if (attr != nullptr)
14394 new_field->virtuality = DW_UNSND (attr);
14395 else
14396 new_field->virtuality = DW_VIRTUALITY_none;
14397
14398 fp = &new_field->field;
14399
14400 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14401 {
14402 /* Data member other than a C++ static data member. */
14403
14404 /* Get type of field. */
14405 fp->type = die_type (die, cu);
14406
14407 SET_FIELD_BITPOS (*fp, 0);
14408
14409 /* Get bit size of field (zero if none). */
14410 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14411 if (attr != nullptr)
14412 {
14413 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14414 }
14415 else
14416 {
14417 FIELD_BITSIZE (*fp) = 0;
14418 }
14419
14420 /* Get bit offset of field. */
14421 handle_data_member_location (die, cu, fp);
14422 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14423 if (attr != nullptr)
14424 {
14425 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14426 {
14427 /* For big endian bits, the DW_AT_bit_offset gives the
14428 additional bit offset from the MSB of the containing
14429 anonymous object to the MSB of the field. We don't
14430 have to do anything special since we don't need to
14431 know the size of the anonymous object. */
14432 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14433 }
14434 else
14435 {
14436 /* For little endian bits, compute the bit offset to the
14437 MSB of the anonymous object, subtract off the number of
14438 bits from the MSB of the field to the MSB of the
14439 object, and then subtract off the number of bits of
14440 the field itself. The result is the bit offset of
14441 the LSB of the field. */
14442 int anonymous_size;
14443 int bit_offset = DW_UNSND (attr);
14444
14445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14446 if (attr != nullptr)
14447 {
14448 /* The size of the anonymous object containing
14449 the bit field is explicit, so use the
14450 indicated size (in bytes). */
14451 anonymous_size = DW_UNSND (attr);
14452 }
14453 else
14454 {
14455 /* The size of the anonymous object containing
14456 the bit field must be inferred from the type
14457 attribute of the data member containing the
14458 bit field. */
14459 anonymous_size = TYPE_LENGTH (fp->type);
14460 }
14461 SET_FIELD_BITPOS (*fp,
14462 (FIELD_BITPOS (*fp)
14463 + anonymous_size * bits_per_byte
14464 - bit_offset - FIELD_BITSIZE (*fp)));
14465 }
14466 }
14467 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14468 if (attr != NULL)
14469 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14470 + attr->constant_value (0)));
14471
14472 /* Get name of field. */
14473 fieldname = dwarf2_name (die, cu);
14474 if (fieldname == NULL)
14475 fieldname = "";
14476
14477 /* The name is already allocated along with this objfile, so we don't
14478 need to duplicate it for the type. */
14479 fp->name = fieldname;
14480
14481 /* Change accessibility for artificial fields (e.g. virtual table
14482 pointer or virtual base class pointer) to private. */
14483 if (dwarf2_attr (die, DW_AT_artificial, cu))
14484 {
14485 FIELD_ARTIFICIAL (*fp) = 1;
14486 new_field->accessibility = DW_ACCESS_private;
14487 fip->non_public_fields = 1;
14488 }
14489 }
14490 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14491 {
14492 /* C++ static member. */
14493
14494 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14495 is a declaration, but all versions of G++ as of this writing
14496 (so through at least 3.2.1) incorrectly generate
14497 DW_TAG_variable tags. */
14498
14499 const char *physname;
14500
14501 /* Get name of field. */
14502 fieldname = dwarf2_name (die, cu);
14503 if (fieldname == NULL)
14504 return;
14505
14506 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14507 if (attr
14508 /* Only create a symbol if this is an external value.
14509 new_symbol checks this and puts the value in the global symbol
14510 table, which we want. If it is not external, new_symbol
14511 will try to put the value in cu->list_in_scope which is wrong. */
14512 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14513 {
14514 /* A static const member, not much different than an enum as far as
14515 we're concerned, except that we can support more types. */
14516 new_symbol (die, NULL, cu);
14517 }
14518
14519 /* Get physical name. */
14520 physname = dwarf2_physname (fieldname, die, cu);
14521
14522 /* The name is already allocated along with this objfile, so we don't
14523 need to duplicate it for the type. */
14524 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14525 FIELD_TYPE (*fp) = die_type (die, cu);
14526 FIELD_NAME (*fp) = fieldname;
14527 }
14528 else if (die->tag == DW_TAG_inheritance)
14529 {
14530 /* C++ base class field. */
14531 handle_data_member_location (die, cu, fp);
14532 FIELD_BITSIZE (*fp) = 0;
14533 FIELD_TYPE (*fp) = die_type (die, cu);
14534 FIELD_NAME (*fp) = fp->type->name ();
14535 }
14536 else
14537 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14538 }
14539
14540 /* Can the type given by DIE define another type? */
14541
14542 static bool
14543 type_can_define_types (const struct die_info *die)
14544 {
14545 switch (die->tag)
14546 {
14547 case DW_TAG_typedef:
14548 case DW_TAG_class_type:
14549 case DW_TAG_structure_type:
14550 case DW_TAG_union_type:
14551 case DW_TAG_enumeration_type:
14552 return true;
14553
14554 default:
14555 return false;
14556 }
14557 }
14558
14559 /* Add a type definition defined in the scope of the FIP's class. */
14560
14561 static void
14562 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14563 struct dwarf2_cu *cu)
14564 {
14565 struct decl_field fp;
14566 memset (&fp, 0, sizeof (fp));
14567
14568 gdb_assert (type_can_define_types (die));
14569
14570 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14571 fp.name = dwarf2_name (die, cu);
14572 fp.type = read_type_die (die, cu);
14573
14574 /* Save accessibility. */
14575 enum dwarf_access_attribute accessibility;
14576 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14577 if (attr != NULL)
14578 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14579 else
14580 accessibility = dwarf2_default_access_attribute (die, cu);
14581 switch (accessibility)
14582 {
14583 case DW_ACCESS_public:
14584 /* The assumed value if neither private nor protected. */
14585 break;
14586 case DW_ACCESS_private:
14587 fp.is_private = 1;
14588 break;
14589 case DW_ACCESS_protected:
14590 fp.is_protected = 1;
14591 break;
14592 default:
14593 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14594 }
14595
14596 if (die->tag == DW_TAG_typedef)
14597 fip->typedef_field_list.push_back (fp);
14598 else
14599 fip->nested_types_list.push_back (fp);
14600 }
14601
14602 /* A convenience typedef that's used when finding the discriminant
14603 field for a variant part. */
14604 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14605 offset_map_type;
14606
14607 /* Compute the discriminant range for a given variant. OBSTACK is
14608 where the results will be stored. VARIANT is the variant to
14609 process. IS_UNSIGNED indicates whether the discriminant is signed
14610 or unsigned. */
14611
14612 static const gdb::array_view<discriminant_range>
14613 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14614 bool is_unsigned)
14615 {
14616 std::vector<discriminant_range> ranges;
14617
14618 if (variant.default_branch)
14619 return {};
14620
14621 if (variant.discr_list_data == nullptr)
14622 {
14623 discriminant_range r
14624 = {variant.discriminant_value, variant.discriminant_value};
14625 ranges.push_back (r);
14626 }
14627 else
14628 {
14629 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14630 variant.discr_list_data->size);
14631 while (!data.empty ())
14632 {
14633 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14634 {
14635 complaint (_("invalid discriminant marker: %d"), data[0]);
14636 break;
14637 }
14638 bool is_range = data[0] == DW_DSC_range;
14639 data = data.slice (1);
14640
14641 ULONGEST low, high;
14642 unsigned int bytes_read;
14643
14644 if (data.empty ())
14645 {
14646 complaint (_("DW_AT_discr_list missing low value"));
14647 break;
14648 }
14649 if (is_unsigned)
14650 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14651 else
14652 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14653 &bytes_read);
14654 data = data.slice (bytes_read);
14655
14656 if (is_range)
14657 {
14658 if (data.empty ())
14659 {
14660 complaint (_("DW_AT_discr_list missing high value"));
14661 break;
14662 }
14663 if (is_unsigned)
14664 high = read_unsigned_leb128 (nullptr, data.data (),
14665 &bytes_read);
14666 else
14667 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14668 &bytes_read);
14669 data = data.slice (bytes_read);
14670 }
14671 else
14672 high = low;
14673
14674 ranges.push_back ({ low, high });
14675 }
14676 }
14677
14678 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14679 ranges.size ());
14680 std::copy (ranges.begin (), ranges.end (), result);
14681 return gdb::array_view<discriminant_range> (result, ranges.size ());
14682 }
14683
14684 static const gdb::array_view<variant_part> create_variant_parts
14685 (struct obstack *obstack,
14686 const offset_map_type &offset_map,
14687 struct field_info *fi,
14688 const std::vector<variant_part_builder> &variant_parts);
14689
14690 /* Fill in a "struct variant" for a given variant field. RESULT is
14691 the variant to fill in. OBSTACK is where any needed allocations
14692 will be done. OFFSET_MAP holds the mapping from section offsets to
14693 fields for the type. FI describes the fields of the type we're
14694 processing. FIELD is the variant field we're converting. */
14695
14696 static void
14697 create_one_variant (variant &result, struct obstack *obstack,
14698 const offset_map_type &offset_map,
14699 struct field_info *fi, const variant_field &field)
14700 {
14701 result.discriminants = convert_variant_range (obstack, field, false);
14702 result.first_field = field.first_field + fi->baseclasses.size ();
14703 result.last_field = field.last_field + fi->baseclasses.size ();
14704 result.parts = create_variant_parts (obstack, offset_map, fi,
14705 field.variant_parts);
14706 }
14707
14708 /* Fill in a "struct variant_part" for a given variant part. RESULT
14709 is the variant part to fill in. OBSTACK is where any needed
14710 allocations will be done. OFFSET_MAP holds the mapping from
14711 section offsets to fields for the type. FI describes the fields of
14712 the type we're processing. BUILDER is the variant part to be
14713 converted. */
14714
14715 static void
14716 create_one_variant_part (variant_part &result,
14717 struct obstack *obstack,
14718 const offset_map_type &offset_map,
14719 struct field_info *fi,
14720 const variant_part_builder &builder)
14721 {
14722 auto iter = offset_map.find (builder.discriminant_offset);
14723 if (iter == offset_map.end ())
14724 {
14725 result.discriminant_index = -1;
14726 /* Doesn't matter. */
14727 result.is_unsigned = false;
14728 }
14729 else
14730 {
14731 result.discriminant_index = iter->second;
14732 result.is_unsigned
14733 = TYPE_UNSIGNED (FIELD_TYPE
14734 (fi->fields[result.discriminant_index].field));
14735 }
14736
14737 size_t n = builder.variants.size ();
14738 variant *output = new (obstack) variant[n];
14739 for (size_t i = 0; i < n; ++i)
14740 create_one_variant (output[i], obstack, offset_map, fi,
14741 builder.variants[i]);
14742
14743 result.variants = gdb::array_view<variant> (output, n);
14744 }
14745
14746 /* Create a vector of variant parts that can be attached to a type.
14747 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14748 holds the mapping from section offsets to fields for the type. FI
14749 describes the fields of the type we're processing. VARIANT_PARTS
14750 is the vector to convert. */
14751
14752 static const gdb::array_view<variant_part>
14753 create_variant_parts (struct obstack *obstack,
14754 const offset_map_type &offset_map,
14755 struct field_info *fi,
14756 const std::vector<variant_part_builder> &variant_parts)
14757 {
14758 if (variant_parts.empty ())
14759 return {};
14760
14761 size_t n = variant_parts.size ();
14762 variant_part *result = new (obstack) variant_part[n];
14763 for (size_t i = 0; i < n; ++i)
14764 create_one_variant_part (result[i], obstack, offset_map, fi,
14765 variant_parts[i]);
14766
14767 return gdb::array_view<variant_part> (result, n);
14768 }
14769
14770 /* Compute the variant part vector for FIP, attaching it to TYPE when
14771 done. */
14772
14773 static void
14774 add_variant_property (struct field_info *fip, struct type *type,
14775 struct dwarf2_cu *cu)
14776 {
14777 /* Map section offsets of fields to their field index. Note the
14778 field index here does not take the number of baseclasses into
14779 account. */
14780 offset_map_type offset_map;
14781 for (int i = 0; i < fip->fields.size (); ++i)
14782 offset_map[fip->fields[i].offset] = i;
14783
14784 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14785 gdb::array_view<variant_part> parts
14786 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14787 fip->variant_parts);
14788
14789 struct dynamic_prop prop;
14790 prop.kind = PROP_VARIANT_PARTS;
14791 prop.data.variant_parts
14792 = ((gdb::array_view<variant_part> *)
14793 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14794
14795 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14796 }
14797
14798 /* Create the vector of fields, and attach it to the type. */
14799
14800 static void
14801 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14802 struct dwarf2_cu *cu)
14803 {
14804 int nfields = fip->nfields ();
14805
14806 /* Record the field count, allocate space for the array of fields,
14807 and create blank accessibility bitfields if necessary. */
14808 type->set_num_fields (nfields);
14809 type->set_fields
14810 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14811
14812 if (fip->non_public_fields && cu->language != language_ada)
14813 {
14814 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14815
14816 TYPE_FIELD_PRIVATE_BITS (type) =
14817 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14818 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14819
14820 TYPE_FIELD_PROTECTED_BITS (type) =
14821 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14822 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14823
14824 TYPE_FIELD_IGNORE_BITS (type) =
14825 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14826 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14827 }
14828
14829 /* If the type has baseclasses, allocate and clear a bit vector for
14830 TYPE_FIELD_VIRTUAL_BITS. */
14831 if (!fip->baseclasses.empty () && cu->language != language_ada)
14832 {
14833 int num_bytes = B_BYTES (fip->baseclasses.size ());
14834 unsigned char *pointer;
14835
14836 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14837 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14838 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14839 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14840 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14841 }
14842
14843 if (!fip->variant_parts.empty ())
14844 add_variant_property (fip, type, cu);
14845
14846 /* Copy the saved-up fields into the field vector. */
14847 for (int i = 0; i < nfields; ++i)
14848 {
14849 struct nextfield &field
14850 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14851 : fip->fields[i - fip->baseclasses.size ()]);
14852
14853 type->field (i) = field.field;
14854 switch (field.accessibility)
14855 {
14856 case DW_ACCESS_private:
14857 if (cu->language != language_ada)
14858 SET_TYPE_FIELD_PRIVATE (type, i);
14859 break;
14860
14861 case DW_ACCESS_protected:
14862 if (cu->language != language_ada)
14863 SET_TYPE_FIELD_PROTECTED (type, i);
14864 break;
14865
14866 case DW_ACCESS_public:
14867 break;
14868
14869 default:
14870 /* Unknown accessibility. Complain and treat it as public. */
14871 {
14872 complaint (_("unsupported accessibility %d"),
14873 field.accessibility);
14874 }
14875 break;
14876 }
14877 if (i < fip->baseclasses.size ())
14878 {
14879 switch (field.virtuality)
14880 {
14881 case DW_VIRTUALITY_virtual:
14882 case DW_VIRTUALITY_pure_virtual:
14883 if (cu->language == language_ada)
14884 error (_("unexpected virtuality in component of Ada type"));
14885 SET_TYPE_FIELD_VIRTUAL (type, i);
14886 break;
14887 }
14888 }
14889 }
14890 }
14891
14892 /* Return true if this member function is a constructor, false
14893 otherwise. */
14894
14895 static int
14896 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14897 {
14898 const char *fieldname;
14899 const char *type_name;
14900 int len;
14901
14902 if (die->parent == NULL)
14903 return 0;
14904
14905 if (die->parent->tag != DW_TAG_structure_type
14906 && die->parent->tag != DW_TAG_union_type
14907 && die->parent->tag != DW_TAG_class_type)
14908 return 0;
14909
14910 fieldname = dwarf2_name (die, cu);
14911 type_name = dwarf2_name (die->parent, cu);
14912 if (fieldname == NULL || type_name == NULL)
14913 return 0;
14914
14915 len = strlen (fieldname);
14916 return (strncmp (fieldname, type_name, len) == 0
14917 && (type_name[len] == '\0' || type_name[len] == '<'));
14918 }
14919
14920 /* Check if the given VALUE is a recognized enum
14921 dwarf_defaulted_attribute constant according to DWARF5 spec,
14922 Table 7.24. */
14923
14924 static bool
14925 is_valid_DW_AT_defaulted (ULONGEST value)
14926 {
14927 switch (value)
14928 {
14929 case DW_DEFAULTED_no:
14930 case DW_DEFAULTED_in_class:
14931 case DW_DEFAULTED_out_of_class:
14932 return true;
14933 }
14934
14935 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14936 return false;
14937 }
14938
14939 /* Add a member function to the proper fieldlist. */
14940
14941 static void
14942 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14943 struct type *type, struct dwarf2_cu *cu)
14944 {
14945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14946 struct attribute *attr;
14947 int i;
14948 struct fnfieldlist *flp = nullptr;
14949 struct fn_field *fnp;
14950 const char *fieldname;
14951 struct type *this_type;
14952 enum dwarf_access_attribute accessibility;
14953
14954 if (cu->language == language_ada)
14955 error (_("unexpected member function in Ada type"));
14956
14957 /* Get name of member function. */
14958 fieldname = dwarf2_name (die, cu);
14959 if (fieldname == NULL)
14960 return;
14961
14962 /* Look up member function name in fieldlist. */
14963 for (i = 0; i < fip->fnfieldlists.size (); i++)
14964 {
14965 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14966 {
14967 flp = &fip->fnfieldlists[i];
14968 break;
14969 }
14970 }
14971
14972 /* Create a new fnfieldlist if necessary. */
14973 if (flp == nullptr)
14974 {
14975 fip->fnfieldlists.emplace_back ();
14976 flp = &fip->fnfieldlists.back ();
14977 flp->name = fieldname;
14978 i = fip->fnfieldlists.size () - 1;
14979 }
14980
14981 /* Create a new member function field and add it to the vector of
14982 fnfieldlists. */
14983 flp->fnfields.emplace_back ();
14984 fnp = &flp->fnfields.back ();
14985
14986 /* Delay processing of the physname until later. */
14987 if (cu->language == language_cplus)
14988 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14989 die, cu);
14990 else
14991 {
14992 const char *physname = dwarf2_physname (fieldname, die, cu);
14993 fnp->physname = physname ? physname : "";
14994 }
14995
14996 fnp->type = alloc_type (objfile);
14997 this_type = read_type_die (die, cu);
14998 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14999 {
15000 int nparams = this_type->num_fields ();
15001
15002 /* TYPE is the domain of this method, and THIS_TYPE is the type
15003 of the method itself (TYPE_CODE_METHOD). */
15004 smash_to_method_type (fnp->type, type,
15005 TYPE_TARGET_TYPE (this_type),
15006 this_type->fields (),
15007 this_type->num_fields (),
15008 TYPE_VARARGS (this_type));
15009
15010 /* Handle static member functions.
15011 Dwarf2 has no clean way to discern C++ static and non-static
15012 member functions. G++ helps GDB by marking the first
15013 parameter for non-static member functions (which is the this
15014 pointer) as artificial. We obtain this information from
15015 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15016 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15017 fnp->voffset = VOFFSET_STATIC;
15018 }
15019 else
15020 complaint (_("member function type missing for '%s'"),
15021 dwarf2_full_name (fieldname, die, cu));
15022
15023 /* Get fcontext from DW_AT_containing_type if present. */
15024 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15025 fnp->fcontext = die_containing_type (die, cu);
15026
15027 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15028 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15029
15030 /* Get accessibility. */
15031 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15032 if (attr != nullptr)
15033 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15034 else
15035 accessibility = dwarf2_default_access_attribute (die, cu);
15036 switch (accessibility)
15037 {
15038 case DW_ACCESS_private:
15039 fnp->is_private = 1;
15040 break;
15041 case DW_ACCESS_protected:
15042 fnp->is_protected = 1;
15043 break;
15044 }
15045
15046 /* Check for artificial methods. */
15047 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15048 if (attr && DW_UNSND (attr) != 0)
15049 fnp->is_artificial = 1;
15050
15051 /* Check for defaulted methods. */
15052 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15053 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15054 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15055
15056 /* Check for deleted methods. */
15057 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15058 if (attr != nullptr && DW_UNSND (attr) != 0)
15059 fnp->is_deleted = 1;
15060
15061 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15062
15063 /* Get index in virtual function table if it is a virtual member
15064 function. For older versions of GCC, this is an offset in the
15065 appropriate virtual table, as specified by DW_AT_containing_type.
15066 For everyone else, it is an expression to be evaluated relative
15067 to the object address. */
15068
15069 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15070 if (attr != nullptr)
15071 {
15072 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15073 {
15074 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15075 {
15076 /* Old-style GCC. */
15077 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15078 }
15079 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15080 || (DW_BLOCK (attr)->size > 1
15081 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15082 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15083 {
15084 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15085 if ((fnp->voffset % cu->header.addr_size) != 0)
15086 dwarf2_complex_location_expr_complaint ();
15087 else
15088 fnp->voffset /= cu->header.addr_size;
15089 fnp->voffset += 2;
15090 }
15091 else
15092 dwarf2_complex_location_expr_complaint ();
15093
15094 if (!fnp->fcontext)
15095 {
15096 /* If there is no `this' field and no DW_AT_containing_type,
15097 we cannot actually find a base class context for the
15098 vtable! */
15099 if (this_type->num_fields () == 0
15100 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15101 {
15102 complaint (_("cannot determine context for virtual member "
15103 "function \"%s\" (offset %s)"),
15104 fieldname, sect_offset_str (die->sect_off));
15105 }
15106 else
15107 {
15108 fnp->fcontext
15109 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15110 }
15111 }
15112 }
15113 else if (attr->form_is_section_offset ())
15114 {
15115 dwarf2_complex_location_expr_complaint ();
15116 }
15117 else
15118 {
15119 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15120 fieldname);
15121 }
15122 }
15123 else
15124 {
15125 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15126 if (attr && DW_UNSND (attr))
15127 {
15128 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15129 complaint (_("Member function \"%s\" (offset %s) is virtual "
15130 "but the vtable offset is not specified"),
15131 fieldname, sect_offset_str (die->sect_off));
15132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15133 TYPE_CPLUS_DYNAMIC (type) = 1;
15134 }
15135 }
15136 }
15137
15138 /* Create the vector of member function fields, and attach it to the type. */
15139
15140 static void
15141 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15142 struct dwarf2_cu *cu)
15143 {
15144 if (cu->language == language_ada)
15145 error (_("unexpected member functions in Ada type"));
15146
15147 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15148 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15149 TYPE_ALLOC (type,
15150 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15151
15152 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15153 {
15154 struct fnfieldlist &nf = fip->fnfieldlists[i];
15155 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15156
15157 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15158 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15159 fn_flp->fn_fields = (struct fn_field *)
15160 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15161
15162 for (int k = 0; k < nf.fnfields.size (); ++k)
15163 fn_flp->fn_fields[k] = nf.fnfields[k];
15164 }
15165
15166 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15167 }
15168
15169 /* Returns non-zero if NAME is the name of a vtable member in CU's
15170 language, zero otherwise. */
15171 static int
15172 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15173 {
15174 static const char vptr[] = "_vptr";
15175
15176 /* Look for the C++ form of the vtable. */
15177 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15178 return 1;
15179
15180 return 0;
15181 }
15182
15183 /* GCC outputs unnamed structures that are really pointers to member
15184 functions, with the ABI-specified layout. If TYPE describes
15185 such a structure, smash it into a member function type.
15186
15187 GCC shouldn't do this; it should just output pointer to member DIEs.
15188 This is GCC PR debug/28767. */
15189
15190 static void
15191 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15192 {
15193 struct type *pfn_type, *self_type, *new_type;
15194
15195 /* Check for a structure with no name and two children. */
15196 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15197 return;
15198
15199 /* Check for __pfn and __delta members. */
15200 if (TYPE_FIELD_NAME (type, 0) == NULL
15201 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15202 || TYPE_FIELD_NAME (type, 1) == NULL
15203 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15204 return;
15205
15206 /* Find the type of the method. */
15207 pfn_type = TYPE_FIELD_TYPE (type, 0);
15208 if (pfn_type == NULL
15209 || pfn_type->code () != TYPE_CODE_PTR
15210 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15211 return;
15212
15213 /* Look for the "this" argument. */
15214 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15215 if (pfn_type->num_fields () == 0
15216 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15217 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15218 return;
15219
15220 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15221 new_type = alloc_type (objfile);
15222 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15223 pfn_type->fields (), pfn_type->num_fields (),
15224 TYPE_VARARGS (pfn_type));
15225 smash_to_methodptr_type (type, new_type);
15226 }
15227
15228 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15229 appropriate error checking and issuing complaints if there is a
15230 problem. */
15231
15232 static ULONGEST
15233 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15234 {
15235 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15236
15237 if (attr == nullptr)
15238 return 0;
15239
15240 if (!attr->form_is_constant ())
15241 {
15242 complaint (_("DW_AT_alignment must have constant form"
15243 " - DIE at %s [in module %s]"),
15244 sect_offset_str (die->sect_off),
15245 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15246 return 0;
15247 }
15248
15249 ULONGEST align;
15250 if (attr->form == DW_FORM_sdata)
15251 {
15252 LONGEST val = DW_SND (attr);
15253 if (val < 0)
15254 {
15255 complaint (_("DW_AT_alignment value must not be negative"
15256 " - DIE at %s [in module %s]"),
15257 sect_offset_str (die->sect_off),
15258 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15259 return 0;
15260 }
15261 align = val;
15262 }
15263 else
15264 align = DW_UNSND (attr);
15265
15266 if (align == 0)
15267 {
15268 complaint (_("DW_AT_alignment value must not be zero"
15269 " - DIE at %s [in module %s]"),
15270 sect_offset_str (die->sect_off),
15271 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15272 return 0;
15273 }
15274 if ((align & (align - 1)) != 0)
15275 {
15276 complaint (_("DW_AT_alignment value must be a power of 2"
15277 " - DIE at %s [in module %s]"),
15278 sect_offset_str (die->sect_off),
15279 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15280 return 0;
15281 }
15282
15283 return align;
15284 }
15285
15286 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15287 the alignment for TYPE. */
15288
15289 static void
15290 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15291 struct type *type)
15292 {
15293 if (!set_type_align (type, get_alignment (cu, die)))
15294 complaint (_("DW_AT_alignment value too large"
15295 " - DIE at %s [in module %s]"),
15296 sect_offset_str (die->sect_off),
15297 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15298 }
15299
15300 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15301 constant for a type, according to DWARF5 spec, Table 5.5. */
15302
15303 static bool
15304 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15305 {
15306 switch (value)
15307 {
15308 case DW_CC_normal:
15309 case DW_CC_pass_by_reference:
15310 case DW_CC_pass_by_value:
15311 return true;
15312
15313 default:
15314 complaint (_("unrecognized DW_AT_calling_convention value "
15315 "(%s) for a type"), pulongest (value));
15316 return false;
15317 }
15318 }
15319
15320 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15321 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15322 also according to GNU-specific values (see include/dwarf2.h). */
15323
15324 static bool
15325 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15326 {
15327 switch (value)
15328 {
15329 case DW_CC_normal:
15330 case DW_CC_program:
15331 case DW_CC_nocall:
15332 return true;
15333
15334 case DW_CC_GNU_renesas_sh:
15335 case DW_CC_GNU_borland_fastcall_i386:
15336 case DW_CC_GDB_IBM_OpenCL:
15337 return true;
15338
15339 default:
15340 complaint (_("unrecognized DW_AT_calling_convention value "
15341 "(%s) for a subroutine"), pulongest (value));
15342 return false;
15343 }
15344 }
15345
15346 /* Called when we find the DIE that starts a structure or union scope
15347 (definition) to create a type for the structure or union. Fill in
15348 the type's name and general properties; the members will not be
15349 processed until process_structure_scope. A symbol table entry for
15350 the type will also not be done until process_structure_scope (assuming
15351 the type has a name).
15352
15353 NOTE: we need to call these functions regardless of whether or not the
15354 DIE has a DW_AT_name attribute, since it might be an anonymous
15355 structure or union. This gets the type entered into our set of
15356 user defined types. */
15357
15358 static struct type *
15359 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15360 {
15361 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15362 struct type *type;
15363 struct attribute *attr;
15364 const char *name;
15365
15366 /* If the definition of this type lives in .debug_types, read that type.
15367 Don't follow DW_AT_specification though, that will take us back up
15368 the chain and we want to go down. */
15369 attr = die->attr (DW_AT_signature);
15370 if (attr != nullptr)
15371 {
15372 type = get_DW_AT_signature_type (die, attr, cu);
15373
15374 /* The type's CU may not be the same as CU.
15375 Ensure TYPE is recorded with CU in die_type_hash. */
15376 return set_die_type (die, type, cu);
15377 }
15378
15379 type = alloc_type (objfile);
15380 INIT_CPLUS_SPECIFIC (type);
15381
15382 name = dwarf2_name (die, cu);
15383 if (name != NULL)
15384 {
15385 if (cu->language == language_cplus
15386 || cu->language == language_d
15387 || cu->language == language_rust)
15388 {
15389 const char *full_name = dwarf2_full_name (name, die, cu);
15390
15391 /* dwarf2_full_name might have already finished building the DIE's
15392 type. If so, there is no need to continue. */
15393 if (get_die_type (die, cu) != NULL)
15394 return get_die_type (die, cu);
15395
15396 type->set_name (full_name);
15397 }
15398 else
15399 {
15400 /* The name is already allocated along with this objfile, so
15401 we don't need to duplicate it for the type. */
15402 type->set_name (name);
15403 }
15404 }
15405
15406 if (die->tag == DW_TAG_structure_type)
15407 {
15408 type->set_code (TYPE_CODE_STRUCT);
15409 }
15410 else if (die->tag == DW_TAG_union_type)
15411 {
15412 type->set_code (TYPE_CODE_UNION);
15413 }
15414 else
15415 {
15416 type->set_code (TYPE_CODE_STRUCT);
15417 }
15418
15419 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15420 TYPE_DECLARED_CLASS (type) = 1;
15421
15422 /* Store the calling convention in the type if it's available in
15423 the die. Otherwise the calling convention remains set to
15424 the default value DW_CC_normal. */
15425 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15426 if (attr != nullptr
15427 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15428 {
15429 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15430 TYPE_CPLUS_CALLING_CONVENTION (type)
15431 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15432 }
15433
15434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15435 if (attr != nullptr)
15436 {
15437 if (attr->form_is_constant ())
15438 TYPE_LENGTH (type) = DW_UNSND (attr);
15439 else
15440 {
15441 struct dynamic_prop prop;
15442 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15443 cu->per_cu->addr_type ()))
15444 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15445 TYPE_LENGTH (type) = 0;
15446 }
15447 }
15448 else
15449 {
15450 TYPE_LENGTH (type) = 0;
15451 }
15452
15453 maybe_set_alignment (cu, die, type);
15454
15455 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15456 {
15457 /* ICC<14 does not output the required DW_AT_declaration on
15458 incomplete types, but gives them a size of zero. */
15459 TYPE_STUB (type) = 1;
15460 }
15461 else
15462 TYPE_STUB_SUPPORTED (type) = 1;
15463
15464 if (die_is_declaration (die, cu))
15465 TYPE_STUB (type) = 1;
15466 else if (attr == NULL && die->child == NULL
15467 && producer_is_realview (cu->producer))
15468 /* RealView does not output the required DW_AT_declaration
15469 on incomplete types. */
15470 TYPE_STUB (type) = 1;
15471
15472 /* We need to add the type field to the die immediately so we don't
15473 infinitely recurse when dealing with pointers to the structure
15474 type within the structure itself. */
15475 set_die_type (die, type, cu);
15476
15477 /* set_die_type should be already done. */
15478 set_descriptive_type (type, die, cu);
15479
15480 return type;
15481 }
15482
15483 static void handle_struct_member_die
15484 (struct die_info *child_die,
15485 struct type *type,
15486 struct field_info *fi,
15487 std::vector<struct symbol *> *template_args,
15488 struct dwarf2_cu *cu);
15489
15490 /* A helper for handle_struct_member_die that handles
15491 DW_TAG_variant_part. */
15492
15493 static void
15494 handle_variant_part (struct die_info *die, struct type *type,
15495 struct field_info *fi,
15496 std::vector<struct symbol *> *template_args,
15497 struct dwarf2_cu *cu)
15498 {
15499 variant_part_builder *new_part;
15500 if (fi->current_variant_part == nullptr)
15501 {
15502 fi->variant_parts.emplace_back ();
15503 new_part = &fi->variant_parts.back ();
15504 }
15505 else if (!fi->current_variant_part->processing_variant)
15506 {
15507 complaint (_("nested DW_TAG_variant_part seen "
15508 "- DIE at %s [in module %s]"),
15509 sect_offset_str (die->sect_off),
15510 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15511 return;
15512 }
15513 else
15514 {
15515 variant_field &current = fi->current_variant_part->variants.back ();
15516 current.variant_parts.emplace_back ();
15517 new_part = &current.variant_parts.back ();
15518 }
15519
15520 /* When we recurse, we want callees to add to this new variant
15521 part. */
15522 scoped_restore save_current_variant_part
15523 = make_scoped_restore (&fi->current_variant_part, new_part);
15524
15525 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15526 if (discr == NULL)
15527 {
15528 /* It's a univariant form, an extension we support. */
15529 }
15530 else if (discr->form_is_ref ())
15531 {
15532 struct dwarf2_cu *target_cu = cu;
15533 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15534
15535 new_part->discriminant_offset = target_die->sect_off;
15536 }
15537 else
15538 {
15539 complaint (_("DW_AT_discr does not have DIE reference form"
15540 " - DIE at %s [in module %s]"),
15541 sect_offset_str (die->sect_off),
15542 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15543 }
15544
15545 for (die_info *child_die = die->child;
15546 child_die != NULL;
15547 child_die = child_die->sibling)
15548 handle_struct_member_die (child_die, type, fi, template_args, cu);
15549 }
15550
15551 /* A helper for handle_struct_member_die that handles
15552 DW_TAG_variant. */
15553
15554 static void
15555 handle_variant (struct die_info *die, struct type *type,
15556 struct field_info *fi,
15557 std::vector<struct symbol *> *template_args,
15558 struct dwarf2_cu *cu)
15559 {
15560 if (fi->current_variant_part == nullptr)
15561 {
15562 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15563 "- DIE at %s [in module %s]"),
15564 sect_offset_str (die->sect_off),
15565 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15566 return;
15567 }
15568 if (fi->current_variant_part->processing_variant)
15569 {
15570 complaint (_("nested DW_TAG_variant seen "
15571 "- DIE at %s [in module %s]"),
15572 sect_offset_str (die->sect_off),
15573 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15574 return;
15575 }
15576
15577 scoped_restore save_processing_variant
15578 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15579 true);
15580
15581 fi->current_variant_part->variants.emplace_back ();
15582 variant_field &variant = fi->current_variant_part->variants.back ();
15583 variant.first_field = fi->fields.size ();
15584
15585 /* In a variant we want to get the discriminant and also add a
15586 field for our sole member child. */
15587 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15588 if (discr == nullptr)
15589 {
15590 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15591 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15592 variant.default_branch = true;
15593 else
15594 variant.discr_list_data = DW_BLOCK (discr);
15595 }
15596 else
15597 variant.discriminant_value = DW_UNSND (discr);
15598
15599 for (die_info *variant_child = die->child;
15600 variant_child != NULL;
15601 variant_child = variant_child->sibling)
15602 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15603
15604 variant.last_field = fi->fields.size ();
15605 }
15606
15607 /* A helper for process_structure_scope that handles a single member
15608 DIE. */
15609
15610 static void
15611 handle_struct_member_die (struct die_info *child_die, struct type *type,
15612 struct field_info *fi,
15613 std::vector<struct symbol *> *template_args,
15614 struct dwarf2_cu *cu)
15615 {
15616 if (child_die->tag == DW_TAG_member
15617 || child_die->tag == DW_TAG_variable)
15618 {
15619 /* NOTE: carlton/2002-11-05: A C++ static data member
15620 should be a DW_TAG_member that is a declaration, but
15621 all versions of G++ as of this writing (so through at
15622 least 3.2.1) incorrectly generate DW_TAG_variable
15623 tags for them instead. */
15624 dwarf2_add_field (fi, child_die, cu);
15625 }
15626 else if (child_die->tag == DW_TAG_subprogram)
15627 {
15628 /* Rust doesn't have member functions in the C++ sense.
15629 However, it does emit ordinary functions as children
15630 of a struct DIE. */
15631 if (cu->language == language_rust)
15632 read_func_scope (child_die, cu);
15633 else
15634 {
15635 /* C++ member function. */
15636 dwarf2_add_member_fn (fi, child_die, type, cu);
15637 }
15638 }
15639 else if (child_die->tag == DW_TAG_inheritance)
15640 {
15641 /* C++ base class field. */
15642 dwarf2_add_field (fi, child_die, cu);
15643 }
15644 else if (type_can_define_types (child_die))
15645 dwarf2_add_type_defn (fi, child_die, cu);
15646 else if (child_die->tag == DW_TAG_template_type_param
15647 || child_die->tag == DW_TAG_template_value_param)
15648 {
15649 struct symbol *arg = new_symbol (child_die, NULL, cu);
15650
15651 if (arg != NULL)
15652 template_args->push_back (arg);
15653 }
15654 else if (child_die->tag == DW_TAG_variant_part)
15655 handle_variant_part (child_die, type, fi, template_args, cu);
15656 else if (child_die->tag == DW_TAG_variant)
15657 handle_variant (child_die, type, fi, template_args, cu);
15658 }
15659
15660 /* Finish creating a structure or union type, including filling in
15661 its members and creating a symbol for it. */
15662
15663 static void
15664 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15665 {
15666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15667 struct die_info *child_die;
15668 struct type *type;
15669
15670 type = get_die_type (die, cu);
15671 if (type == NULL)
15672 type = read_structure_type (die, cu);
15673
15674 bool has_template_parameters = false;
15675 if (die->child != NULL && ! die_is_declaration (die, cu))
15676 {
15677 struct field_info fi;
15678 std::vector<struct symbol *> template_args;
15679
15680 child_die = die->child;
15681
15682 while (child_die && child_die->tag)
15683 {
15684 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15685 child_die = child_die->sibling;
15686 }
15687
15688 /* Attach template arguments to type. */
15689 if (!template_args.empty ())
15690 {
15691 has_template_parameters = true;
15692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15693 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15694 TYPE_TEMPLATE_ARGUMENTS (type)
15695 = XOBNEWVEC (&objfile->objfile_obstack,
15696 struct symbol *,
15697 TYPE_N_TEMPLATE_ARGUMENTS (type));
15698 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15699 template_args.data (),
15700 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15701 * sizeof (struct symbol *)));
15702 }
15703
15704 /* Attach fields and member functions to the type. */
15705 if (fi.nfields () > 0)
15706 dwarf2_attach_fields_to_type (&fi, type, cu);
15707 if (!fi.fnfieldlists.empty ())
15708 {
15709 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15710
15711 /* Get the type which refers to the base class (possibly this
15712 class itself) which contains the vtable pointer for the current
15713 class from the DW_AT_containing_type attribute. This use of
15714 DW_AT_containing_type is a GNU extension. */
15715
15716 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15717 {
15718 struct type *t = die_containing_type (die, cu);
15719
15720 set_type_vptr_basetype (type, t);
15721 if (type == t)
15722 {
15723 int i;
15724
15725 /* Our own class provides vtbl ptr. */
15726 for (i = t->num_fields () - 1;
15727 i >= TYPE_N_BASECLASSES (t);
15728 --i)
15729 {
15730 const char *fieldname = TYPE_FIELD_NAME (t, i);
15731
15732 if (is_vtable_name (fieldname, cu))
15733 {
15734 set_type_vptr_fieldno (type, i);
15735 break;
15736 }
15737 }
15738
15739 /* Complain if virtual function table field not found. */
15740 if (i < TYPE_N_BASECLASSES (t))
15741 complaint (_("virtual function table pointer "
15742 "not found when defining class '%s'"),
15743 type->name () ? type->name () : "");
15744 }
15745 else
15746 {
15747 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15748 }
15749 }
15750 else if (cu->producer
15751 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15752 {
15753 /* The IBM XLC compiler does not provide direct indication
15754 of the containing type, but the vtable pointer is
15755 always named __vfp. */
15756
15757 int i;
15758
15759 for (i = type->num_fields () - 1;
15760 i >= TYPE_N_BASECLASSES (type);
15761 --i)
15762 {
15763 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15764 {
15765 set_type_vptr_fieldno (type, i);
15766 set_type_vptr_basetype (type, type);
15767 break;
15768 }
15769 }
15770 }
15771 }
15772
15773 /* Copy fi.typedef_field_list linked list elements content into the
15774 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15775 if (!fi.typedef_field_list.empty ())
15776 {
15777 int count = fi.typedef_field_list.size ();
15778
15779 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15780 TYPE_TYPEDEF_FIELD_ARRAY (type)
15781 = ((struct decl_field *)
15782 TYPE_ALLOC (type,
15783 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15784 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15785
15786 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15787 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15788 }
15789
15790 /* Copy fi.nested_types_list linked list elements content into the
15791 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15792 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15793 {
15794 int count = fi.nested_types_list.size ();
15795
15796 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15797 TYPE_NESTED_TYPES_ARRAY (type)
15798 = ((struct decl_field *)
15799 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15800 TYPE_NESTED_TYPES_COUNT (type) = count;
15801
15802 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15803 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15804 }
15805 }
15806
15807 quirk_gcc_member_function_pointer (type, objfile);
15808 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15809 cu->rust_unions.push_back (type);
15810
15811 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15812 snapshots) has been known to create a die giving a declaration
15813 for a class that has, as a child, a die giving a definition for a
15814 nested class. So we have to process our children even if the
15815 current die is a declaration. Normally, of course, a declaration
15816 won't have any children at all. */
15817
15818 child_die = die->child;
15819
15820 while (child_die != NULL && child_die->tag)
15821 {
15822 if (child_die->tag == DW_TAG_member
15823 || child_die->tag == DW_TAG_variable
15824 || child_die->tag == DW_TAG_inheritance
15825 || child_die->tag == DW_TAG_template_value_param
15826 || child_die->tag == DW_TAG_template_type_param)
15827 {
15828 /* Do nothing. */
15829 }
15830 else
15831 process_die (child_die, cu);
15832
15833 child_die = child_die->sibling;
15834 }
15835
15836 /* Do not consider external references. According to the DWARF standard,
15837 these DIEs are identified by the fact that they have no byte_size
15838 attribute, and a declaration attribute. */
15839 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15840 || !die_is_declaration (die, cu)
15841 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15842 {
15843 struct symbol *sym = new_symbol (die, type, cu);
15844
15845 if (has_template_parameters)
15846 {
15847 struct symtab *symtab;
15848 if (sym != nullptr)
15849 symtab = symbol_symtab (sym);
15850 else if (cu->line_header != nullptr)
15851 {
15852 /* Any related symtab will do. */
15853 symtab
15854 = cu->line_header->file_names ()[0].symtab;
15855 }
15856 else
15857 {
15858 symtab = nullptr;
15859 complaint (_("could not find suitable "
15860 "symtab for template parameter"
15861 " - DIE at %s [in module %s]"),
15862 sect_offset_str (die->sect_off),
15863 objfile_name (objfile));
15864 }
15865
15866 if (symtab != nullptr)
15867 {
15868 /* Make sure that the symtab is set on the new symbols.
15869 Even though they don't appear in this symtab directly,
15870 other parts of gdb assume that symbols do, and this is
15871 reasonably true. */
15872 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15873 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15874 }
15875 }
15876 }
15877 }
15878
15879 /* Assuming DIE is an enumeration type, and TYPE is its associated
15880 type, update TYPE using some information only available in DIE's
15881 children. In particular, the fields are computed. */
15882
15883 static void
15884 update_enumeration_type_from_children (struct die_info *die,
15885 struct type *type,
15886 struct dwarf2_cu *cu)
15887 {
15888 struct die_info *child_die;
15889 int unsigned_enum = 1;
15890 int flag_enum = 1;
15891
15892 auto_obstack obstack;
15893 std::vector<struct field> fields;
15894
15895 for (child_die = die->child;
15896 child_die != NULL && child_die->tag;
15897 child_die = child_die->sibling)
15898 {
15899 struct attribute *attr;
15900 LONGEST value;
15901 const gdb_byte *bytes;
15902 struct dwarf2_locexpr_baton *baton;
15903 const char *name;
15904
15905 if (child_die->tag != DW_TAG_enumerator)
15906 continue;
15907
15908 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15909 if (attr == NULL)
15910 continue;
15911
15912 name = dwarf2_name (child_die, cu);
15913 if (name == NULL)
15914 name = "<anonymous enumerator>";
15915
15916 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15917 &value, &bytes, &baton);
15918 if (value < 0)
15919 {
15920 unsigned_enum = 0;
15921 flag_enum = 0;
15922 }
15923 else
15924 {
15925 if (count_one_bits_ll (value) >= 2)
15926 flag_enum = 0;
15927 }
15928
15929 fields.emplace_back ();
15930 struct field &field = fields.back ();
15931 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15932 SET_FIELD_ENUMVAL (field, value);
15933 }
15934
15935 if (!fields.empty ())
15936 {
15937 type->set_num_fields (fields.size ());
15938 type->set_fields
15939 ((struct field *)
15940 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15941 memcpy (type->fields (), fields.data (),
15942 sizeof (struct field) * fields.size ());
15943 }
15944
15945 if (unsigned_enum)
15946 TYPE_UNSIGNED (type) = 1;
15947 if (flag_enum)
15948 TYPE_FLAG_ENUM (type) = 1;
15949 }
15950
15951 /* Given a DW_AT_enumeration_type die, set its type. We do not
15952 complete the type's fields yet, or create any symbols. */
15953
15954 static struct type *
15955 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15956 {
15957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15958 struct type *type;
15959 struct attribute *attr;
15960 const char *name;
15961
15962 /* If the definition of this type lives in .debug_types, read that type.
15963 Don't follow DW_AT_specification though, that will take us back up
15964 the chain and we want to go down. */
15965 attr = die->attr (DW_AT_signature);
15966 if (attr != nullptr)
15967 {
15968 type = get_DW_AT_signature_type (die, attr, cu);
15969
15970 /* The type's CU may not be the same as CU.
15971 Ensure TYPE is recorded with CU in die_type_hash. */
15972 return set_die_type (die, type, cu);
15973 }
15974
15975 type = alloc_type (objfile);
15976
15977 type->set_code (TYPE_CODE_ENUM);
15978 name = dwarf2_full_name (NULL, die, cu);
15979 if (name != NULL)
15980 type->set_name (name);
15981
15982 attr = dwarf2_attr (die, DW_AT_type, cu);
15983 if (attr != NULL)
15984 {
15985 struct type *underlying_type = die_type (die, cu);
15986
15987 TYPE_TARGET_TYPE (type) = underlying_type;
15988 }
15989
15990 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15991 if (attr != nullptr)
15992 {
15993 TYPE_LENGTH (type) = DW_UNSND (attr);
15994 }
15995 else
15996 {
15997 TYPE_LENGTH (type) = 0;
15998 }
15999
16000 maybe_set_alignment (cu, die, type);
16001
16002 /* The enumeration DIE can be incomplete. In Ada, any type can be
16003 declared as private in the package spec, and then defined only
16004 inside the package body. Such types are known as Taft Amendment
16005 Types. When another package uses such a type, an incomplete DIE
16006 may be generated by the compiler. */
16007 if (die_is_declaration (die, cu))
16008 TYPE_STUB (type) = 1;
16009
16010 /* If this type has an underlying type that is not a stub, then we
16011 may use its attributes. We always use the "unsigned" attribute
16012 in this situation, because ordinarily we guess whether the type
16013 is unsigned -- but the guess can be wrong and the underlying type
16014 can tell us the reality. However, we defer to a local size
16015 attribute if one exists, because this lets the compiler override
16016 the underlying type if needed. */
16017 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16018 {
16019 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16020 underlying_type = check_typedef (underlying_type);
16021 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16022 if (TYPE_LENGTH (type) == 0)
16023 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16024 if (TYPE_RAW_ALIGN (type) == 0
16025 && TYPE_RAW_ALIGN (underlying_type) != 0)
16026 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16027 }
16028
16029 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16030
16031 set_die_type (die, type, cu);
16032
16033 /* Finish the creation of this type by using the enum's children.
16034 Note that, as usual, this must come after set_die_type to avoid
16035 infinite recursion when trying to compute the names of the
16036 enumerators. */
16037 update_enumeration_type_from_children (die, type, cu);
16038
16039 return type;
16040 }
16041
16042 /* Given a pointer to a die which begins an enumeration, process all
16043 the dies that define the members of the enumeration, and create the
16044 symbol for the enumeration type.
16045
16046 NOTE: We reverse the order of the element list. */
16047
16048 static void
16049 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16050 {
16051 struct type *this_type;
16052
16053 this_type = get_die_type (die, cu);
16054 if (this_type == NULL)
16055 this_type = read_enumeration_type (die, cu);
16056
16057 if (die->child != NULL)
16058 {
16059 struct die_info *child_die;
16060 const char *name;
16061
16062 child_die = die->child;
16063 while (child_die && child_die->tag)
16064 {
16065 if (child_die->tag != DW_TAG_enumerator)
16066 {
16067 process_die (child_die, cu);
16068 }
16069 else
16070 {
16071 name = dwarf2_name (child_die, cu);
16072 if (name)
16073 new_symbol (child_die, this_type, cu);
16074 }
16075
16076 child_die = child_die->sibling;
16077 }
16078 }
16079
16080 /* If we are reading an enum from a .debug_types unit, and the enum
16081 is a declaration, and the enum is not the signatured type in the
16082 unit, then we do not want to add a symbol for it. Adding a
16083 symbol would in some cases obscure the true definition of the
16084 enum, giving users an incomplete type when the definition is
16085 actually available. Note that we do not want to do this for all
16086 enums which are just declarations, because C++0x allows forward
16087 enum declarations. */
16088 if (cu->per_cu->is_debug_types
16089 && die_is_declaration (die, cu))
16090 {
16091 struct signatured_type *sig_type;
16092
16093 sig_type = (struct signatured_type *) cu->per_cu;
16094 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16095 if (sig_type->type_offset_in_section != die->sect_off)
16096 return;
16097 }
16098
16099 new_symbol (die, this_type, cu);
16100 }
16101
16102 /* Extract all information from a DW_TAG_array_type DIE and put it in
16103 the DIE's type field. For now, this only handles one dimensional
16104 arrays. */
16105
16106 static struct type *
16107 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16108 {
16109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16110 struct die_info *child_die;
16111 struct type *type;
16112 struct type *element_type, *range_type, *index_type;
16113 struct attribute *attr;
16114 const char *name;
16115 struct dynamic_prop *byte_stride_prop = NULL;
16116 unsigned int bit_stride = 0;
16117
16118 element_type = die_type (die, cu);
16119
16120 /* The die_type call above may have already set the type for this DIE. */
16121 type = get_die_type (die, cu);
16122 if (type)
16123 return type;
16124
16125 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16126 if (attr != NULL)
16127 {
16128 int stride_ok;
16129 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16130
16131 byte_stride_prop
16132 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16133 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16134 prop_type);
16135 if (!stride_ok)
16136 {
16137 complaint (_("unable to read array DW_AT_byte_stride "
16138 " - DIE at %s [in module %s]"),
16139 sect_offset_str (die->sect_off),
16140 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16141 /* Ignore this attribute. We will likely not be able to print
16142 arrays of this type correctly, but there is little we can do
16143 to help if we cannot read the attribute's value. */
16144 byte_stride_prop = NULL;
16145 }
16146 }
16147
16148 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16149 if (attr != NULL)
16150 bit_stride = DW_UNSND (attr);
16151
16152 /* Irix 6.2 native cc creates array types without children for
16153 arrays with unspecified length. */
16154 if (die->child == NULL)
16155 {
16156 index_type = objfile_type (objfile)->builtin_int;
16157 range_type = create_static_range_type (NULL, index_type, 0, -1);
16158 type = create_array_type_with_stride (NULL, element_type, range_type,
16159 byte_stride_prop, bit_stride);
16160 return set_die_type (die, type, cu);
16161 }
16162
16163 std::vector<struct type *> range_types;
16164 child_die = die->child;
16165 while (child_die && child_die->tag)
16166 {
16167 if (child_die->tag == DW_TAG_subrange_type)
16168 {
16169 struct type *child_type = read_type_die (child_die, cu);
16170
16171 if (child_type != NULL)
16172 {
16173 /* The range type was succesfully read. Save it for the
16174 array type creation. */
16175 range_types.push_back (child_type);
16176 }
16177 }
16178 child_die = child_die->sibling;
16179 }
16180
16181 /* Dwarf2 dimensions are output from left to right, create the
16182 necessary array types in backwards order. */
16183
16184 type = element_type;
16185
16186 if (read_array_order (die, cu) == DW_ORD_col_major)
16187 {
16188 int i = 0;
16189
16190 while (i < range_types.size ())
16191 type = create_array_type_with_stride (NULL, type, range_types[i++],
16192 byte_stride_prop, bit_stride);
16193 }
16194 else
16195 {
16196 size_t ndim = range_types.size ();
16197 while (ndim-- > 0)
16198 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16199 byte_stride_prop, bit_stride);
16200 }
16201
16202 /* Understand Dwarf2 support for vector types (like they occur on
16203 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16204 array type. This is not part of the Dwarf2/3 standard yet, but a
16205 custom vendor extension. The main difference between a regular
16206 array and the vector variant is that vectors are passed by value
16207 to functions. */
16208 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16209 if (attr != nullptr)
16210 make_vector_type (type);
16211
16212 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16213 implementation may choose to implement triple vectors using this
16214 attribute. */
16215 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16216 if (attr != nullptr)
16217 {
16218 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16219 TYPE_LENGTH (type) = DW_UNSND (attr);
16220 else
16221 complaint (_("DW_AT_byte_size for array type smaller "
16222 "than the total size of elements"));
16223 }
16224
16225 name = dwarf2_name (die, cu);
16226 if (name)
16227 type->set_name (name);
16228
16229 maybe_set_alignment (cu, die, type);
16230
16231 /* Install the type in the die. */
16232 set_die_type (die, type, cu);
16233
16234 /* set_die_type should be already done. */
16235 set_descriptive_type (type, die, cu);
16236
16237 return type;
16238 }
16239
16240 static enum dwarf_array_dim_ordering
16241 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16242 {
16243 struct attribute *attr;
16244
16245 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16246
16247 if (attr != nullptr)
16248 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16249
16250 /* GNU F77 is a special case, as at 08/2004 array type info is the
16251 opposite order to the dwarf2 specification, but data is still
16252 laid out as per normal fortran.
16253
16254 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16255 version checking. */
16256
16257 if (cu->language == language_fortran
16258 && cu->producer && strstr (cu->producer, "GNU F77"))
16259 {
16260 return DW_ORD_row_major;
16261 }
16262
16263 switch (cu->language_defn->la_array_ordering)
16264 {
16265 case array_column_major:
16266 return DW_ORD_col_major;
16267 case array_row_major:
16268 default:
16269 return DW_ORD_row_major;
16270 };
16271 }
16272
16273 /* Extract all information from a DW_TAG_set_type DIE and put it in
16274 the DIE's type field. */
16275
16276 static struct type *
16277 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16278 {
16279 struct type *domain_type, *set_type;
16280 struct attribute *attr;
16281
16282 domain_type = die_type (die, cu);
16283
16284 /* The die_type call above may have already set the type for this DIE. */
16285 set_type = get_die_type (die, cu);
16286 if (set_type)
16287 return set_type;
16288
16289 set_type = create_set_type (NULL, domain_type);
16290
16291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16292 if (attr != nullptr)
16293 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16294
16295 maybe_set_alignment (cu, die, set_type);
16296
16297 return set_die_type (die, set_type, cu);
16298 }
16299
16300 /* A helper for read_common_block that creates a locexpr baton.
16301 SYM is the symbol which we are marking as computed.
16302 COMMON_DIE is the DIE for the common block.
16303 COMMON_LOC is the location expression attribute for the common
16304 block itself.
16305 MEMBER_LOC is the location expression attribute for the particular
16306 member of the common block that we are processing.
16307 CU is the CU from which the above come. */
16308
16309 static void
16310 mark_common_block_symbol_computed (struct symbol *sym,
16311 struct die_info *common_die,
16312 struct attribute *common_loc,
16313 struct attribute *member_loc,
16314 struct dwarf2_cu *cu)
16315 {
16316 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
16317 struct objfile *objfile = per_objfile->objfile;
16318 struct dwarf2_locexpr_baton *baton;
16319 gdb_byte *ptr;
16320 unsigned int cu_off;
16321 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16322 LONGEST offset = 0;
16323
16324 gdb_assert (common_loc && member_loc);
16325 gdb_assert (common_loc->form_is_block ());
16326 gdb_assert (member_loc->form_is_block ()
16327 || member_loc->form_is_constant ());
16328
16329 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16330 baton->per_objfile = per_objfile;
16331 baton->per_cu = cu->per_cu;
16332 gdb_assert (baton->per_cu);
16333
16334 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16335
16336 if (member_loc->form_is_constant ())
16337 {
16338 offset = member_loc->constant_value (0);
16339 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16340 }
16341 else
16342 baton->size += DW_BLOCK (member_loc)->size;
16343
16344 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16345 baton->data = ptr;
16346
16347 *ptr++ = DW_OP_call4;
16348 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16349 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16350 ptr += 4;
16351
16352 if (member_loc->form_is_constant ())
16353 {
16354 *ptr++ = DW_OP_addr;
16355 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16356 ptr += cu->header.addr_size;
16357 }
16358 else
16359 {
16360 /* We have to copy the data here, because DW_OP_call4 will only
16361 use a DW_AT_location attribute. */
16362 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16363 ptr += DW_BLOCK (member_loc)->size;
16364 }
16365
16366 *ptr++ = DW_OP_plus;
16367 gdb_assert (ptr - baton->data == baton->size);
16368
16369 SYMBOL_LOCATION_BATON (sym) = baton;
16370 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16371 }
16372
16373 /* Create appropriate locally-scoped variables for all the
16374 DW_TAG_common_block entries. Also create a struct common_block
16375 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16376 is used to separate the common blocks name namespace from regular
16377 variable names. */
16378
16379 static void
16380 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16381 {
16382 struct attribute *attr;
16383
16384 attr = dwarf2_attr (die, DW_AT_location, cu);
16385 if (attr != nullptr)
16386 {
16387 /* Support the .debug_loc offsets. */
16388 if (attr->form_is_block ())
16389 {
16390 /* Ok. */
16391 }
16392 else if (attr->form_is_section_offset ())
16393 {
16394 dwarf2_complex_location_expr_complaint ();
16395 attr = NULL;
16396 }
16397 else
16398 {
16399 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16400 "common block member");
16401 attr = NULL;
16402 }
16403 }
16404
16405 if (die->child != NULL)
16406 {
16407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16408 struct die_info *child_die;
16409 size_t n_entries = 0, size;
16410 struct common_block *common_block;
16411 struct symbol *sym;
16412
16413 for (child_die = die->child;
16414 child_die && child_die->tag;
16415 child_die = child_die->sibling)
16416 ++n_entries;
16417
16418 size = (sizeof (struct common_block)
16419 + (n_entries - 1) * sizeof (struct symbol *));
16420 common_block
16421 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16422 size);
16423 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16424 common_block->n_entries = 0;
16425
16426 for (child_die = die->child;
16427 child_die && child_die->tag;
16428 child_die = child_die->sibling)
16429 {
16430 /* Create the symbol in the DW_TAG_common_block block in the current
16431 symbol scope. */
16432 sym = new_symbol (child_die, NULL, cu);
16433 if (sym != NULL)
16434 {
16435 struct attribute *member_loc;
16436
16437 common_block->contents[common_block->n_entries++] = sym;
16438
16439 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16440 cu);
16441 if (member_loc)
16442 {
16443 /* GDB has handled this for a long time, but it is
16444 not specified by DWARF. It seems to have been
16445 emitted by gfortran at least as recently as:
16446 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16447 complaint (_("Variable in common block has "
16448 "DW_AT_data_member_location "
16449 "- DIE at %s [in module %s]"),
16450 sect_offset_str (child_die->sect_off),
16451 objfile_name (objfile));
16452
16453 if (member_loc->form_is_section_offset ())
16454 dwarf2_complex_location_expr_complaint ();
16455 else if (member_loc->form_is_constant ()
16456 || member_loc->form_is_block ())
16457 {
16458 if (attr != nullptr)
16459 mark_common_block_symbol_computed (sym, die, attr,
16460 member_loc, cu);
16461 }
16462 else
16463 dwarf2_complex_location_expr_complaint ();
16464 }
16465 }
16466 }
16467
16468 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16469 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16470 }
16471 }
16472
16473 /* Create a type for a C++ namespace. */
16474
16475 static struct type *
16476 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16477 {
16478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16479 const char *previous_prefix, *name;
16480 int is_anonymous;
16481 struct type *type;
16482
16483 /* For extensions, reuse the type of the original namespace. */
16484 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16485 {
16486 struct die_info *ext_die;
16487 struct dwarf2_cu *ext_cu = cu;
16488
16489 ext_die = dwarf2_extension (die, &ext_cu);
16490 type = read_type_die (ext_die, ext_cu);
16491
16492 /* EXT_CU may not be the same as CU.
16493 Ensure TYPE is recorded with CU in die_type_hash. */
16494 return set_die_type (die, type, cu);
16495 }
16496
16497 name = namespace_name (die, &is_anonymous, cu);
16498
16499 /* Now build the name of the current namespace. */
16500
16501 previous_prefix = determine_prefix (die, cu);
16502 if (previous_prefix[0] != '\0')
16503 name = typename_concat (&objfile->objfile_obstack,
16504 previous_prefix, name, 0, cu);
16505
16506 /* Create the type. */
16507 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16508
16509 return set_die_type (die, type, cu);
16510 }
16511
16512 /* Read a namespace scope. */
16513
16514 static void
16515 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16516 {
16517 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16518 int is_anonymous;
16519
16520 /* Add a symbol associated to this if we haven't seen the namespace
16521 before. Also, add a using directive if it's an anonymous
16522 namespace. */
16523
16524 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16525 {
16526 struct type *type;
16527
16528 type = read_type_die (die, cu);
16529 new_symbol (die, type, cu);
16530
16531 namespace_name (die, &is_anonymous, cu);
16532 if (is_anonymous)
16533 {
16534 const char *previous_prefix = determine_prefix (die, cu);
16535
16536 std::vector<const char *> excludes;
16537 add_using_directive (using_directives (cu),
16538 previous_prefix, type->name (), NULL,
16539 NULL, excludes, 0, &objfile->objfile_obstack);
16540 }
16541 }
16542
16543 if (die->child != NULL)
16544 {
16545 struct die_info *child_die = die->child;
16546
16547 while (child_die && child_die->tag)
16548 {
16549 process_die (child_die, cu);
16550 child_die = child_die->sibling;
16551 }
16552 }
16553 }
16554
16555 /* Read a Fortran module as type. This DIE can be only a declaration used for
16556 imported module. Still we need that type as local Fortran "use ... only"
16557 declaration imports depend on the created type in determine_prefix. */
16558
16559 static struct type *
16560 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16561 {
16562 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16563 const char *module_name;
16564 struct type *type;
16565
16566 module_name = dwarf2_name (die, cu);
16567 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16568
16569 return set_die_type (die, type, cu);
16570 }
16571
16572 /* Read a Fortran module. */
16573
16574 static void
16575 read_module (struct die_info *die, struct dwarf2_cu *cu)
16576 {
16577 struct die_info *child_die = die->child;
16578 struct type *type;
16579
16580 type = read_type_die (die, cu);
16581 new_symbol (die, type, cu);
16582
16583 while (child_die && child_die->tag)
16584 {
16585 process_die (child_die, cu);
16586 child_die = child_die->sibling;
16587 }
16588 }
16589
16590 /* Return the name of the namespace represented by DIE. Set
16591 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16592 namespace. */
16593
16594 static const char *
16595 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16596 {
16597 struct die_info *current_die;
16598 const char *name = NULL;
16599
16600 /* Loop through the extensions until we find a name. */
16601
16602 for (current_die = die;
16603 current_die != NULL;
16604 current_die = dwarf2_extension (die, &cu))
16605 {
16606 /* We don't use dwarf2_name here so that we can detect the absence
16607 of a name -> anonymous namespace. */
16608 name = dwarf2_string_attr (die, DW_AT_name, cu);
16609
16610 if (name != NULL)
16611 break;
16612 }
16613
16614 /* Is it an anonymous namespace? */
16615
16616 *is_anonymous = (name == NULL);
16617 if (*is_anonymous)
16618 name = CP_ANONYMOUS_NAMESPACE_STR;
16619
16620 return name;
16621 }
16622
16623 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16624 the user defined type vector. */
16625
16626 static struct type *
16627 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16628 {
16629 struct gdbarch *gdbarch
16630 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
16631 struct comp_unit_head *cu_header = &cu->header;
16632 struct type *type;
16633 struct attribute *attr_byte_size;
16634 struct attribute *attr_address_class;
16635 int byte_size, addr_class;
16636 struct type *target_type;
16637
16638 target_type = die_type (die, cu);
16639
16640 /* The die_type call above may have already set the type for this DIE. */
16641 type = get_die_type (die, cu);
16642 if (type)
16643 return type;
16644
16645 type = lookup_pointer_type (target_type);
16646
16647 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16648 if (attr_byte_size)
16649 byte_size = DW_UNSND (attr_byte_size);
16650 else
16651 byte_size = cu_header->addr_size;
16652
16653 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16654 if (attr_address_class)
16655 addr_class = DW_UNSND (attr_address_class);
16656 else
16657 addr_class = DW_ADDR_none;
16658
16659 ULONGEST alignment = get_alignment (cu, die);
16660
16661 /* If the pointer size, alignment, or address class is different
16662 than the default, create a type variant marked as such and set
16663 the length accordingly. */
16664 if (TYPE_LENGTH (type) != byte_size
16665 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16666 && alignment != TYPE_RAW_ALIGN (type))
16667 || addr_class != DW_ADDR_none)
16668 {
16669 if (gdbarch_address_class_type_flags_p (gdbarch))
16670 {
16671 int type_flags;
16672
16673 type_flags = gdbarch_address_class_type_flags
16674 (gdbarch, byte_size, addr_class);
16675 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16676 == 0);
16677 type = make_type_with_address_space (type, type_flags);
16678 }
16679 else if (TYPE_LENGTH (type) != byte_size)
16680 {
16681 complaint (_("invalid pointer size %d"), byte_size);
16682 }
16683 else if (TYPE_RAW_ALIGN (type) != alignment)
16684 {
16685 complaint (_("Invalid DW_AT_alignment"
16686 " - DIE at %s [in module %s]"),
16687 sect_offset_str (die->sect_off),
16688 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16689 }
16690 else
16691 {
16692 /* Should we also complain about unhandled address classes? */
16693 }
16694 }
16695
16696 TYPE_LENGTH (type) = byte_size;
16697 set_type_align (type, alignment);
16698 return set_die_type (die, type, cu);
16699 }
16700
16701 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16702 the user defined type vector. */
16703
16704 static struct type *
16705 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16706 {
16707 struct type *type;
16708 struct type *to_type;
16709 struct type *domain;
16710
16711 to_type = die_type (die, cu);
16712 domain = die_containing_type (die, cu);
16713
16714 /* The calls above may have already set the type for this DIE. */
16715 type = get_die_type (die, cu);
16716 if (type)
16717 return type;
16718
16719 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16720 type = lookup_methodptr_type (to_type);
16721 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16722 {
16723 struct type *new_type
16724 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16725
16726 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16727 to_type->fields (), to_type->num_fields (),
16728 TYPE_VARARGS (to_type));
16729 type = lookup_methodptr_type (new_type);
16730 }
16731 else
16732 type = lookup_memberptr_type (to_type, domain);
16733
16734 return set_die_type (die, type, cu);
16735 }
16736
16737 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16738 the user defined type vector. */
16739
16740 static struct type *
16741 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16742 enum type_code refcode)
16743 {
16744 struct comp_unit_head *cu_header = &cu->header;
16745 struct type *type, *target_type;
16746 struct attribute *attr;
16747
16748 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16749
16750 target_type = die_type (die, cu);
16751
16752 /* The die_type call above may have already set the type for this DIE. */
16753 type = get_die_type (die, cu);
16754 if (type)
16755 return type;
16756
16757 type = lookup_reference_type (target_type, refcode);
16758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16759 if (attr != nullptr)
16760 {
16761 TYPE_LENGTH (type) = DW_UNSND (attr);
16762 }
16763 else
16764 {
16765 TYPE_LENGTH (type) = cu_header->addr_size;
16766 }
16767 maybe_set_alignment (cu, die, type);
16768 return set_die_type (die, type, cu);
16769 }
16770
16771 /* Add the given cv-qualifiers to the element type of the array. GCC
16772 outputs DWARF type qualifiers that apply to an array, not the
16773 element type. But GDB relies on the array element type to carry
16774 the cv-qualifiers. This mimics section 6.7.3 of the C99
16775 specification. */
16776
16777 static struct type *
16778 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16779 struct type *base_type, int cnst, int voltl)
16780 {
16781 struct type *el_type, *inner_array;
16782
16783 base_type = copy_type (base_type);
16784 inner_array = base_type;
16785
16786 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16787 {
16788 TYPE_TARGET_TYPE (inner_array) =
16789 copy_type (TYPE_TARGET_TYPE (inner_array));
16790 inner_array = TYPE_TARGET_TYPE (inner_array);
16791 }
16792
16793 el_type = TYPE_TARGET_TYPE (inner_array);
16794 cnst |= TYPE_CONST (el_type);
16795 voltl |= TYPE_VOLATILE (el_type);
16796 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16797
16798 return set_die_type (die, base_type, cu);
16799 }
16800
16801 static struct type *
16802 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16803 {
16804 struct type *base_type, *cv_type;
16805
16806 base_type = die_type (die, cu);
16807
16808 /* The die_type call above may have already set the type for this DIE. */
16809 cv_type = get_die_type (die, cu);
16810 if (cv_type)
16811 return cv_type;
16812
16813 /* In case the const qualifier is applied to an array type, the element type
16814 is so qualified, not the array type (section 6.7.3 of C99). */
16815 if (base_type->code () == TYPE_CODE_ARRAY)
16816 return add_array_cv_type (die, cu, base_type, 1, 0);
16817
16818 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16819 return set_die_type (die, cv_type, cu);
16820 }
16821
16822 static struct type *
16823 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16824 {
16825 struct type *base_type, *cv_type;
16826
16827 base_type = die_type (die, cu);
16828
16829 /* The die_type call above may have already set the type for this DIE. */
16830 cv_type = get_die_type (die, cu);
16831 if (cv_type)
16832 return cv_type;
16833
16834 /* In case the volatile qualifier is applied to an array type, the
16835 element type is so qualified, not the array type (section 6.7.3
16836 of C99). */
16837 if (base_type->code () == TYPE_CODE_ARRAY)
16838 return add_array_cv_type (die, cu, base_type, 0, 1);
16839
16840 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16841 return set_die_type (die, cv_type, cu);
16842 }
16843
16844 /* Handle DW_TAG_restrict_type. */
16845
16846 static struct type *
16847 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16848 {
16849 struct type *base_type, *cv_type;
16850
16851 base_type = die_type (die, cu);
16852
16853 /* The die_type call above may have already set the type for this DIE. */
16854 cv_type = get_die_type (die, cu);
16855 if (cv_type)
16856 return cv_type;
16857
16858 cv_type = make_restrict_type (base_type);
16859 return set_die_type (die, cv_type, cu);
16860 }
16861
16862 /* Handle DW_TAG_atomic_type. */
16863
16864 static struct type *
16865 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16866 {
16867 struct type *base_type, *cv_type;
16868
16869 base_type = die_type (die, cu);
16870
16871 /* The die_type call above may have already set the type for this DIE. */
16872 cv_type = get_die_type (die, cu);
16873 if (cv_type)
16874 return cv_type;
16875
16876 cv_type = make_atomic_type (base_type);
16877 return set_die_type (die, cv_type, cu);
16878 }
16879
16880 /* Extract all information from a DW_TAG_string_type DIE and add to
16881 the user defined type vector. It isn't really a user defined type,
16882 but it behaves like one, with other DIE's using an AT_user_def_type
16883 attribute to reference it. */
16884
16885 static struct type *
16886 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16887 {
16888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16889 struct gdbarch *gdbarch = objfile->arch ();
16890 struct type *type, *range_type, *index_type, *char_type;
16891 struct attribute *attr;
16892 struct dynamic_prop prop;
16893 bool length_is_constant = true;
16894 LONGEST length;
16895
16896 /* There are a couple of places where bit sizes might be made use of
16897 when parsing a DW_TAG_string_type, however, no producer that we know
16898 of make use of these. Handling bit sizes that are a multiple of the
16899 byte size is easy enough, but what about other bit sizes? Lets deal
16900 with that problem when we have to. Warn about these attributes being
16901 unsupported, then parse the type and ignore them like we always
16902 have. */
16903 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16904 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16905 {
16906 static bool warning_printed = false;
16907 if (!warning_printed)
16908 {
16909 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16910 "currently supported on DW_TAG_string_type."));
16911 warning_printed = true;
16912 }
16913 }
16914
16915 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16916 if (attr != nullptr && !attr->form_is_constant ())
16917 {
16918 /* The string length describes the location at which the length of
16919 the string can be found. The size of the length field can be
16920 specified with one of the attributes below. */
16921 struct type *prop_type;
16922 struct attribute *len
16923 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16924 if (len == nullptr)
16925 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16926 if (len != nullptr && len->form_is_constant ())
16927 {
16928 /* Pass 0 as the default as we know this attribute is constant
16929 and the default value will not be returned. */
16930 LONGEST sz = len->constant_value (0);
16931 prop_type = cu->per_cu->int_type (sz, true);
16932 }
16933 else
16934 {
16935 /* If the size is not specified then we assume it is the size of
16936 an address on this target. */
16937 prop_type = cu->per_cu->addr_sized_int_type (true);
16938 }
16939
16940 /* Convert the attribute into a dynamic property. */
16941 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16942 length = 1;
16943 else
16944 length_is_constant = false;
16945 }
16946 else if (attr != nullptr)
16947 {
16948 /* This DW_AT_string_length just contains the length with no
16949 indirection. There's no need to create a dynamic property in this
16950 case. Pass 0 for the default value as we know it will not be
16951 returned in this case. */
16952 length = attr->constant_value (0);
16953 }
16954 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16955 {
16956 /* We don't currently support non-constant byte sizes for strings. */
16957 length = attr->constant_value (1);
16958 }
16959 else
16960 {
16961 /* Use 1 as a fallback length if we have nothing else. */
16962 length = 1;
16963 }
16964
16965 index_type = objfile_type (objfile)->builtin_int;
16966 if (length_is_constant)
16967 range_type = create_static_range_type (NULL, index_type, 1, length);
16968 else
16969 {
16970 struct dynamic_prop low_bound;
16971
16972 low_bound.kind = PROP_CONST;
16973 low_bound.data.const_val = 1;
16974 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16975 }
16976 char_type = language_string_char_type (cu->language_defn, gdbarch);
16977 type = create_string_type (NULL, char_type, range_type);
16978
16979 return set_die_type (die, type, cu);
16980 }
16981
16982 /* Assuming that DIE corresponds to a function, returns nonzero
16983 if the function is prototyped. */
16984
16985 static int
16986 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16987 {
16988 struct attribute *attr;
16989
16990 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16991 if (attr && (DW_UNSND (attr) != 0))
16992 return 1;
16993
16994 /* The DWARF standard implies that the DW_AT_prototyped attribute
16995 is only meaningful for C, but the concept also extends to other
16996 languages that allow unprototyped functions (Eg: Objective C).
16997 For all other languages, assume that functions are always
16998 prototyped. */
16999 if (cu->language != language_c
17000 && cu->language != language_objc
17001 && cu->language != language_opencl)
17002 return 1;
17003
17004 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17005 prototyped and unprototyped functions; default to prototyped,
17006 since that is more common in modern code (and RealView warns
17007 about unprototyped functions). */
17008 if (producer_is_realview (cu->producer))
17009 return 1;
17010
17011 return 0;
17012 }
17013
17014 /* Handle DIES due to C code like:
17015
17016 struct foo
17017 {
17018 int (*funcp)(int a, long l);
17019 int b;
17020 };
17021
17022 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17023
17024 static struct type *
17025 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17026 {
17027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17028 struct type *type; /* Type that this function returns. */
17029 struct type *ftype; /* Function that returns above type. */
17030 struct attribute *attr;
17031
17032 type = die_type (die, cu);
17033
17034 /* The die_type call above may have already set the type for this DIE. */
17035 ftype = get_die_type (die, cu);
17036 if (ftype)
17037 return ftype;
17038
17039 ftype = lookup_function_type (type);
17040
17041 if (prototyped_function_p (die, cu))
17042 TYPE_PROTOTYPED (ftype) = 1;
17043
17044 /* Store the calling convention in the type if it's available in
17045 the subroutine die. Otherwise set the calling convention to
17046 the default value DW_CC_normal. */
17047 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17048 if (attr != nullptr
17049 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17050 TYPE_CALLING_CONVENTION (ftype)
17051 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17052 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17053 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17054 else
17055 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17056
17057 /* Record whether the function returns normally to its caller or not
17058 if the DWARF producer set that information. */
17059 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17060 if (attr && (DW_UNSND (attr) != 0))
17061 TYPE_NO_RETURN (ftype) = 1;
17062
17063 /* We need to add the subroutine type to the die immediately so
17064 we don't infinitely recurse when dealing with parameters
17065 declared as the same subroutine type. */
17066 set_die_type (die, ftype, cu);
17067
17068 if (die->child != NULL)
17069 {
17070 struct type *void_type = objfile_type (objfile)->builtin_void;
17071 struct die_info *child_die;
17072 int nparams, iparams;
17073
17074 /* Count the number of parameters.
17075 FIXME: GDB currently ignores vararg functions, but knows about
17076 vararg member functions. */
17077 nparams = 0;
17078 child_die = die->child;
17079 while (child_die && child_die->tag)
17080 {
17081 if (child_die->tag == DW_TAG_formal_parameter)
17082 nparams++;
17083 else if (child_die->tag == DW_TAG_unspecified_parameters)
17084 TYPE_VARARGS (ftype) = 1;
17085 child_die = child_die->sibling;
17086 }
17087
17088 /* Allocate storage for parameters and fill them in. */
17089 ftype->set_num_fields (nparams);
17090 ftype->set_fields
17091 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17092
17093 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17094 even if we error out during the parameters reading below. */
17095 for (iparams = 0; iparams < nparams; iparams++)
17096 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17097
17098 iparams = 0;
17099 child_die = die->child;
17100 while (child_die && child_die->tag)
17101 {
17102 if (child_die->tag == DW_TAG_formal_parameter)
17103 {
17104 struct type *arg_type;
17105
17106 /* DWARF version 2 has no clean way to discern C++
17107 static and non-static member functions. G++ helps
17108 GDB by marking the first parameter for non-static
17109 member functions (which is the this pointer) as
17110 artificial. We pass this information to
17111 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17112
17113 DWARF version 3 added DW_AT_object_pointer, which GCC
17114 4.5 does not yet generate. */
17115 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17116 if (attr != nullptr)
17117 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17118 else
17119 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17120 arg_type = die_type (child_die, cu);
17121
17122 /* RealView does not mark THIS as const, which the testsuite
17123 expects. GCC marks THIS as const in method definitions,
17124 but not in the class specifications (GCC PR 43053). */
17125 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17126 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17127 {
17128 int is_this = 0;
17129 struct dwarf2_cu *arg_cu = cu;
17130 const char *name = dwarf2_name (child_die, cu);
17131
17132 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17133 if (attr != nullptr)
17134 {
17135 /* If the compiler emits this, use it. */
17136 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17137 is_this = 1;
17138 }
17139 else if (name && strcmp (name, "this") == 0)
17140 /* Function definitions will have the argument names. */
17141 is_this = 1;
17142 else if (name == NULL && iparams == 0)
17143 /* Declarations may not have the names, so like
17144 elsewhere in GDB, assume an artificial first
17145 argument is "this". */
17146 is_this = 1;
17147
17148 if (is_this)
17149 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17150 arg_type, 0);
17151 }
17152
17153 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17154 iparams++;
17155 }
17156 child_die = child_die->sibling;
17157 }
17158 }
17159
17160 return ftype;
17161 }
17162
17163 static struct type *
17164 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17165 {
17166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17167 const char *name = NULL;
17168 struct type *this_type, *target_type;
17169
17170 name = dwarf2_full_name (NULL, die, cu);
17171 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17172 TYPE_TARGET_STUB (this_type) = 1;
17173 set_die_type (die, this_type, cu);
17174 target_type = die_type (die, cu);
17175 if (target_type != this_type)
17176 TYPE_TARGET_TYPE (this_type) = target_type;
17177 else
17178 {
17179 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17180 spec and cause infinite loops in GDB. */
17181 complaint (_("Self-referential DW_TAG_typedef "
17182 "- DIE at %s [in module %s]"),
17183 sect_offset_str (die->sect_off), objfile_name (objfile));
17184 TYPE_TARGET_TYPE (this_type) = NULL;
17185 }
17186 if (name == NULL)
17187 {
17188 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17189 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17190 Handle these by just returning the target type, rather than
17191 constructing an anonymous typedef type and trying to handle this
17192 elsewhere. */
17193 set_die_type (die, target_type, cu);
17194 return target_type;
17195 }
17196 return this_type;
17197 }
17198
17199 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17200 (which may be different from NAME) to the architecture back-end to allow
17201 it to guess the correct format if necessary. */
17202
17203 static struct type *
17204 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17205 const char *name_hint, enum bfd_endian byte_order)
17206 {
17207 struct gdbarch *gdbarch = objfile->arch ();
17208 const struct floatformat **format;
17209 struct type *type;
17210
17211 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17212 if (format)
17213 type = init_float_type (objfile, bits, name, format, byte_order);
17214 else
17215 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17216
17217 return type;
17218 }
17219
17220 /* Allocate an integer type of size BITS and name NAME. */
17221
17222 static struct type *
17223 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17224 int bits, int unsigned_p, const char *name)
17225 {
17226 struct type *type;
17227
17228 /* Versions of Intel's C Compiler generate an integer type called "void"
17229 instead of using DW_TAG_unspecified_type. This has been seen on
17230 at least versions 14, 17, and 18. */
17231 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17232 && strcmp (name, "void") == 0)
17233 type = objfile_type (objfile)->builtin_void;
17234 else
17235 type = init_integer_type (objfile, bits, unsigned_p, name);
17236
17237 return type;
17238 }
17239
17240 /* Initialise and return a floating point type of size BITS suitable for
17241 use as a component of a complex number. The NAME_HINT is passed through
17242 when initialising the floating point type and is the name of the complex
17243 type.
17244
17245 As DWARF doesn't currently provide an explicit name for the components
17246 of a complex number, but it can be helpful to have these components
17247 named, we try to select a suitable name based on the size of the
17248 component. */
17249 static struct type *
17250 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17251 struct objfile *objfile,
17252 int bits, const char *name_hint,
17253 enum bfd_endian byte_order)
17254 {
17255 gdbarch *gdbarch = objfile->arch ();
17256 struct type *tt = nullptr;
17257
17258 /* Try to find a suitable floating point builtin type of size BITS.
17259 We're going to use the name of this type as the name for the complex
17260 target type that we are about to create. */
17261 switch (cu->language)
17262 {
17263 case language_fortran:
17264 switch (bits)
17265 {
17266 case 32:
17267 tt = builtin_f_type (gdbarch)->builtin_real;
17268 break;
17269 case 64:
17270 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17271 break;
17272 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17273 case 128:
17274 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17275 break;
17276 }
17277 break;
17278 default:
17279 switch (bits)
17280 {
17281 case 32:
17282 tt = builtin_type (gdbarch)->builtin_float;
17283 break;
17284 case 64:
17285 tt = builtin_type (gdbarch)->builtin_double;
17286 break;
17287 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17288 case 128:
17289 tt = builtin_type (gdbarch)->builtin_long_double;
17290 break;
17291 }
17292 break;
17293 }
17294
17295 /* If the type we found doesn't match the size we were looking for, then
17296 pretend we didn't find a type at all, the complex target type we
17297 create will then be nameless. */
17298 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17299 tt = nullptr;
17300
17301 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17302 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17303 }
17304
17305 /* Find a representation of a given base type and install
17306 it in the TYPE field of the die. */
17307
17308 static struct type *
17309 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17310 {
17311 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17312 struct type *type;
17313 struct attribute *attr;
17314 int encoding = 0, bits = 0;
17315 const char *name;
17316 gdbarch *arch;
17317
17318 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17319 if (attr != nullptr)
17320 encoding = DW_UNSND (attr);
17321 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17322 if (attr != nullptr)
17323 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17324 name = dwarf2_name (die, cu);
17325 if (!name)
17326 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17327
17328 arch = objfile->arch ();
17329 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17330
17331 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17332 if (attr)
17333 {
17334 int endianity = DW_UNSND (attr);
17335
17336 switch (endianity)
17337 {
17338 case DW_END_big:
17339 byte_order = BFD_ENDIAN_BIG;
17340 break;
17341 case DW_END_little:
17342 byte_order = BFD_ENDIAN_LITTLE;
17343 break;
17344 default:
17345 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17346 break;
17347 }
17348 }
17349
17350 switch (encoding)
17351 {
17352 case DW_ATE_address:
17353 /* Turn DW_ATE_address into a void * pointer. */
17354 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17355 type = init_pointer_type (objfile, bits, name, type);
17356 break;
17357 case DW_ATE_boolean:
17358 type = init_boolean_type (objfile, bits, 1, name);
17359 break;
17360 case DW_ATE_complex_float:
17361 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17362 byte_order);
17363 if (type->code () == TYPE_CODE_ERROR)
17364 {
17365 if (name == nullptr)
17366 {
17367 struct obstack *obstack
17368 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17369 name = obconcat (obstack, "_Complex ", type->name (),
17370 nullptr);
17371 }
17372 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17373 }
17374 else
17375 type = init_complex_type (name, type);
17376 break;
17377 case DW_ATE_decimal_float:
17378 type = init_decfloat_type (objfile, bits, name);
17379 break;
17380 case DW_ATE_float:
17381 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17382 break;
17383 case DW_ATE_signed:
17384 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17385 break;
17386 case DW_ATE_unsigned:
17387 if (cu->language == language_fortran
17388 && name
17389 && startswith (name, "character("))
17390 type = init_character_type (objfile, bits, 1, name);
17391 else
17392 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17393 break;
17394 case DW_ATE_signed_char:
17395 if (cu->language == language_ada || cu->language == language_m2
17396 || cu->language == language_pascal
17397 || cu->language == language_fortran)
17398 type = init_character_type (objfile, bits, 0, name);
17399 else
17400 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17401 break;
17402 case DW_ATE_unsigned_char:
17403 if (cu->language == language_ada || cu->language == language_m2
17404 || cu->language == language_pascal
17405 || cu->language == language_fortran
17406 || cu->language == language_rust)
17407 type = init_character_type (objfile, bits, 1, name);
17408 else
17409 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17410 break;
17411 case DW_ATE_UTF:
17412 {
17413 if (bits == 16)
17414 type = builtin_type (arch)->builtin_char16;
17415 else if (bits == 32)
17416 type = builtin_type (arch)->builtin_char32;
17417 else
17418 {
17419 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17420 bits);
17421 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17422 }
17423 return set_die_type (die, type, cu);
17424 }
17425 break;
17426
17427 default:
17428 complaint (_("unsupported DW_AT_encoding: '%s'"),
17429 dwarf_type_encoding_name (encoding));
17430 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17431 break;
17432 }
17433
17434 if (name && strcmp (name, "char") == 0)
17435 TYPE_NOSIGN (type) = 1;
17436
17437 maybe_set_alignment (cu, die, type);
17438
17439 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17440
17441 return set_die_type (die, type, cu);
17442 }
17443
17444 /* Parse dwarf attribute if it's a block, reference or constant and put the
17445 resulting value of the attribute into struct bound_prop.
17446 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17447
17448 static int
17449 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17450 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17451 struct type *default_type)
17452 {
17453 struct dwarf2_property_baton *baton;
17454 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
17455 struct objfile *objfile = per_objfile->objfile;
17456 struct obstack *obstack = &objfile->objfile_obstack;
17457
17458 gdb_assert (default_type != NULL);
17459
17460 if (attr == NULL || prop == NULL)
17461 return 0;
17462
17463 if (attr->form_is_block ())
17464 {
17465 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17466 baton->property_type = default_type;
17467 baton->locexpr.per_cu = cu->per_cu;
17468 baton->locexpr.per_objfile = per_objfile;
17469 baton->locexpr.size = DW_BLOCK (attr)->size;
17470 baton->locexpr.data = DW_BLOCK (attr)->data;
17471 switch (attr->name)
17472 {
17473 case DW_AT_string_length:
17474 baton->locexpr.is_reference = true;
17475 break;
17476 default:
17477 baton->locexpr.is_reference = false;
17478 break;
17479 }
17480 prop->data.baton = baton;
17481 prop->kind = PROP_LOCEXPR;
17482 gdb_assert (prop->data.baton != NULL);
17483 }
17484 else if (attr->form_is_ref ())
17485 {
17486 struct dwarf2_cu *target_cu = cu;
17487 struct die_info *target_die;
17488 struct attribute *target_attr;
17489
17490 target_die = follow_die_ref (die, attr, &target_cu);
17491 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17492 if (target_attr == NULL)
17493 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17494 target_cu);
17495 if (target_attr == NULL)
17496 return 0;
17497
17498 switch (target_attr->name)
17499 {
17500 case DW_AT_location:
17501 if (target_attr->form_is_section_offset ())
17502 {
17503 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17504 baton->property_type = die_type (target_die, target_cu);
17505 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17506 prop->data.baton = baton;
17507 prop->kind = PROP_LOCLIST;
17508 gdb_assert (prop->data.baton != NULL);
17509 }
17510 else if (target_attr->form_is_block ())
17511 {
17512 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17513 baton->property_type = die_type (target_die, target_cu);
17514 baton->locexpr.per_cu = cu->per_cu;
17515 baton->locexpr.per_objfile = per_objfile;
17516 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17517 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17518 baton->locexpr.is_reference = true;
17519 prop->data.baton = baton;
17520 prop->kind = PROP_LOCEXPR;
17521 gdb_assert (prop->data.baton != NULL);
17522 }
17523 else
17524 {
17525 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17526 "dynamic property");
17527 return 0;
17528 }
17529 break;
17530 case DW_AT_data_member_location:
17531 {
17532 LONGEST offset;
17533
17534 if (!handle_data_member_location (target_die, target_cu,
17535 &offset))
17536 return 0;
17537
17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17539 baton->property_type = read_type_die (target_die->parent,
17540 target_cu);
17541 baton->offset_info.offset = offset;
17542 baton->offset_info.type = die_type (target_die, target_cu);
17543 prop->data.baton = baton;
17544 prop->kind = PROP_ADDR_OFFSET;
17545 break;
17546 }
17547 }
17548 }
17549 else if (attr->form_is_constant ())
17550 {
17551 prop->data.const_val = attr->constant_value (0);
17552 prop->kind = PROP_CONST;
17553 }
17554 else
17555 {
17556 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17557 dwarf2_name (die, cu));
17558 return 0;
17559 }
17560
17561 return 1;
17562 }
17563
17564 /* See read.h. */
17565
17566 struct type *
17567 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17568 {
17569 struct objfile *objfile = dwarf2_per_objfile->objfile;
17570 struct type *int_type;
17571
17572 /* Helper macro to examine the various builtin types. */
17573 #define TRY_TYPE(F) \
17574 int_type = (unsigned_p \
17575 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17576 : objfile_type (objfile)->builtin_ ## F); \
17577 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17578 return int_type
17579
17580 TRY_TYPE (char);
17581 TRY_TYPE (short);
17582 TRY_TYPE (int);
17583 TRY_TYPE (long);
17584 TRY_TYPE (long_long);
17585
17586 #undef TRY_TYPE
17587
17588 gdb_assert_not_reached ("unable to find suitable integer type");
17589 }
17590
17591 /* See read.h. */
17592
17593 struct type *
17594 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17595 {
17596 int addr_size = this->addr_size ();
17597 return int_type (addr_size, unsigned_p);
17598 }
17599
17600 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17601 present (which is valid) then compute the default type based on the
17602 compilation units address size. */
17603
17604 static struct type *
17605 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17606 {
17607 struct type *index_type = die_type (die, cu);
17608
17609 /* Dwarf-2 specifications explicitly allows to create subrange types
17610 without specifying a base type.
17611 In that case, the base type must be set to the type of
17612 the lower bound, upper bound or count, in that order, if any of these
17613 three attributes references an object that has a type.
17614 If no base type is found, the Dwarf-2 specifications say that
17615 a signed integer type of size equal to the size of an address should
17616 be used.
17617 For the following C code: `extern char gdb_int [];'
17618 GCC produces an empty range DIE.
17619 FIXME: muller/2010-05-28: Possible references to object for low bound,
17620 high bound or count are not yet handled by this code. */
17621 if (index_type->code () == TYPE_CODE_VOID)
17622 index_type = cu->per_cu->addr_sized_int_type (false);
17623
17624 return index_type;
17625 }
17626
17627 /* Read the given DW_AT_subrange DIE. */
17628
17629 static struct type *
17630 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17631 {
17632 struct type *base_type, *orig_base_type;
17633 struct type *range_type;
17634 struct attribute *attr;
17635 struct dynamic_prop low, high;
17636 int low_default_is_valid;
17637 int high_bound_is_count = 0;
17638 const char *name;
17639 ULONGEST negative_mask;
17640
17641 orig_base_type = read_subrange_index_type (die, cu);
17642
17643 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17644 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17645 creating the range type, but we use the result of check_typedef
17646 when examining properties of the type. */
17647 base_type = check_typedef (orig_base_type);
17648
17649 /* The die_type call above may have already set the type for this DIE. */
17650 range_type = get_die_type (die, cu);
17651 if (range_type)
17652 return range_type;
17653
17654 low.kind = PROP_CONST;
17655 high.kind = PROP_CONST;
17656 high.data.const_val = 0;
17657
17658 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17659 omitting DW_AT_lower_bound. */
17660 switch (cu->language)
17661 {
17662 case language_c:
17663 case language_cplus:
17664 low.data.const_val = 0;
17665 low_default_is_valid = 1;
17666 break;
17667 case language_fortran:
17668 low.data.const_val = 1;
17669 low_default_is_valid = 1;
17670 break;
17671 case language_d:
17672 case language_objc:
17673 case language_rust:
17674 low.data.const_val = 0;
17675 low_default_is_valid = (cu->header.version >= 4);
17676 break;
17677 case language_ada:
17678 case language_m2:
17679 case language_pascal:
17680 low.data.const_val = 1;
17681 low_default_is_valid = (cu->header.version >= 4);
17682 break;
17683 default:
17684 low.data.const_val = 0;
17685 low_default_is_valid = 0;
17686 break;
17687 }
17688
17689 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17690 if (attr != nullptr)
17691 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17692 else if (!low_default_is_valid)
17693 complaint (_("Missing DW_AT_lower_bound "
17694 "- DIE at %s [in module %s]"),
17695 sect_offset_str (die->sect_off),
17696 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17697
17698 struct attribute *attr_ub, *attr_count;
17699 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17700 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17701 {
17702 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17703 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17704 {
17705 /* If bounds are constant do the final calculation here. */
17706 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17707 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17708 else
17709 high_bound_is_count = 1;
17710 }
17711 else
17712 {
17713 if (attr_ub != NULL)
17714 complaint (_("Unresolved DW_AT_upper_bound "
17715 "- DIE at %s [in module %s]"),
17716 sect_offset_str (die->sect_off),
17717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17718 if (attr_count != NULL)
17719 complaint (_("Unresolved DW_AT_count "
17720 "- DIE at %s [in module %s]"),
17721 sect_offset_str (die->sect_off),
17722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17723 }
17724 }
17725
17726 LONGEST bias = 0;
17727 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17728 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17729 bias = bias_attr->constant_value (0);
17730
17731 /* Normally, the DWARF producers are expected to use a signed
17732 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17733 But this is unfortunately not always the case, as witnessed
17734 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17735 is used instead. To work around that ambiguity, we treat
17736 the bounds as signed, and thus sign-extend their values, when
17737 the base type is signed. */
17738 negative_mask =
17739 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17740 if (low.kind == PROP_CONST
17741 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17742 low.data.const_val |= negative_mask;
17743 if (high.kind == PROP_CONST
17744 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17745 high.data.const_val |= negative_mask;
17746
17747 /* Check for bit and byte strides. */
17748 struct dynamic_prop byte_stride_prop;
17749 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17750 if (attr_byte_stride != nullptr)
17751 {
17752 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17753 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17754 prop_type);
17755 }
17756
17757 struct dynamic_prop bit_stride_prop;
17758 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17759 if (attr_bit_stride != nullptr)
17760 {
17761 /* It only makes sense to have either a bit or byte stride. */
17762 if (attr_byte_stride != nullptr)
17763 {
17764 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17765 "- DIE at %s [in module %s]"),
17766 sect_offset_str (die->sect_off),
17767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17768 attr_bit_stride = nullptr;
17769 }
17770 else
17771 {
17772 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17773 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17774 prop_type);
17775 }
17776 }
17777
17778 if (attr_byte_stride != nullptr
17779 || attr_bit_stride != nullptr)
17780 {
17781 bool byte_stride_p = (attr_byte_stride != nullptr);
17782 struct dynamic_prop *stride
17783 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17784
17785 range_type
17786 = create_range_type_with_stride (NULL, orig_base_type, &low,
17787 &high, bias, stride, byte_stride_p);
17788 }
17789 else
17790 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17791
17792 if (high_bound_is_count)
17793 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17794
17795 /* Ada expects an empty array on no boundary attributes. */
17796 if (attr == NULL && cu->language != language_ada)
17797 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17798
17799 name = dwarf2_name (die, cu);
17800 if (name)
17801 range_type->set_name (name);
17802
17803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17804 if (attr != nullptr)
17805 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17806
17807 maybe_set_alignment (cu, die, range_type);
17808
17809 set_die_type (die, range_type, cu);
17810
17811 /* set_die_type should be already done. */
17812 set_descriptive_type (range_type, die, cu);
17813
17814 return range_type;
17815 }
17816
17817 static struct type *
17818 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17819 {
17820 struct type *type;
17821
17822 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17823 NULL);
17824 type->set_name (dwarf2_name (die, cu));
17825
17826 /* In Ada, an unspecified type is typically used when the description
17827 of the type is deferred to a different unit. When encountering
17828 such a type, we treat it as a stub, and try to resolve it later on,
17829 when needed. */
17830 if (cu->language == language_ada)
17831 TYPE_STUB (type) = 1;
17832
17833 return set_die_type (die, type, cu);
17834 }
17835
17836 /* Read a single die and all its descendents. Set the die's sibling
17837 field to NULL; set other fields in the die correctly, and set all
17838 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17839 location of the info_ptr after reading all of those dies. PARENT
17840 is the parent of the die in question. */
17841
17842 static struct die_info *
17843 read_die_and_children (const struct die_reader_specs *reader,
17844 const gdb_byte *info_ptr,
17845 const gdb_byte **new_info_ptr,
17846 struct die_info *parent)
17847 {
17848 struct die_info *die;
17849 const gdb_byte *cur_ptr;
17850
17851 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17852 if (die == NULL)
17853 {
17854 *new_info_ptr = cur_ptr;
17855 return NULL;
17856 }
17857 store_in_ref_table (die, reader->cu);
17858
17859 if (die->has_children)
17860 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17861 else
17862 {
17863 die->child = NULL;
17864 *new_info_ptr = cur_ptr;
17865 }
17866
17867 die->sibling = NULL;
17868 die->parent = parent;
17869 return die;
17870 }
17871
17872 /* Read a die, all of its descendents, and all of its siblings; set
17873 all of the fields of all of the dies correctly. Arguments are as
17874 in read_die_and_children. */
17875
17876 static struct die_info *
17877 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17878 const gdb_byte *info_ptr,
17879 const gdb_byte **new_info_ptr,
17880 struct die_info *parent)
17881 {
17882 struct die_info *first_die, *last_sibling;
17883 const gdb_byte *cur_ptr;
17884
17885 cur_ptr = info_ptr;
17886 first_die = last_sibling = NULL;
17887
17888 while (1)
17889 {
17890 struct die_info *die
17891 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17892
17893 if (die == NULL)
17894 {
17895 *new_info_ptr = cur_ptr;
17896 return first_die;
17897 }
17898
17899 if (!first_die)
17900 first_die = die;
17901 else
17902 last_sibling->sibling = die;
17903
17904 last_sibling = die;
17905 }
17906 }
17907
17908 /* Read a die, all of its descendents, and all of its siblings; set
17909 all of the fields of all of the dies correctly. Arguments are as
17910 in read_die_and_children.
17911 This the main entry point for reading a DIE and all its children. */
17912
17913 static struct die_info *
17914 read_die_and_siblings (const struct die_reader_specs *reader,
17915 const gdb_byte *info_ptr,
17916 const gdb_byte **new_info_ptr,
17917 struct die_info *parent)
17918 {
17919 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17920 new_info_ptr, parent);
17921
17922 if (dwarf_die_debug)
17923 {
17924 fprintf_unfiltered (gdb_stdlog,
17925 "Read die from %s@0x%x of %s:\n",
17926 reader->die_section->get_name (),
17927 (unsigned) (info_ptr - reader->die_section->buffer),
17928 bfd_get_filename (reader->abfd));
17929 dump_die (die, dwarf_die_debug);
17930 }
17931
17932 return die;
17933 }
17934
17935 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17936 attributes.
17937 The caller is responsible for filling in the extra attributes
17938 and updating (*DIEP)->num_attrs.
17939 Set DIEP to point to a newly allocated die with its information,
17940 except for its child, sibling, and parent fields. */
17941
17942 static const gdb_byte *
17943 read_full_die_1 (const struct die_reader_specs *reader,
17944 struct die_info **diep, const gdb_byte *info_ptr,
17945 int num_extra_attrs)
17946 {
17947 unsigned int abbrev_number, bytes_read, i;
17948 struct abbrev_info *abbrev;
17949 struct die_info *die;
17950 struct dwarf2_cu *cu = reader->cu;
17951 bfd *abfd = reader->abfd;
17952
17953 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17954 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17955 info_ptr += bytes_read;
17956 if (!abbrev_number)
17957 {
17958 *diep = NULL;
17959 return info_ptr;
17960 }
17961
17962 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17963 if (!abbrev)
17964 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17965 abbrev_number,
17966 bfd_get_filename (abfd));
17967
17968 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17969 die->sect_off = sect_off;
17970 die->tag = abbrev->tag;
17971 die->abbrev = abbrev_number;
17972 die->has_children = abbrev->has_children;
17973
17974 /* Make the result usable.
17975 The caller needs to update num_attrs after adding the extra
17976 attributes. */
17977 die->num_attrs = abbrev->num_attrs;
17978
17979 std::vector<int> indexes_that_need_reprocess;
17980 for (i = 0; i < abbrev->num_attrs; ++i)
17981 {
17982 bool need_reprocess;
17983 info_ptr =
17984 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17985 info_ptr, &need_reprocess);
17986 if (need_reprocess)
17987 indexes_that_need_reprocess.push_back (i);
17988 }
17989
17990 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17991 if (attr != nullptr)
17992 cu->str_offsets_base = DW_UNSND (attr);
17993
17994 attr = die->attr (DW_AT_loclists_base);
17995 if (attr != nullptr)
17996 cu->loclist_base = DW_UNSND (attr);
17997
17998 auto maybe_addr_base = die->addr_base ();
17999 if (maybe_addr_base.has_value ())
18000 cu->addr_base = *maybe_addr_base;
18001 for (int index : indexes_that_need_reprocess)
18002 read_attribute_reprocess (reader, &die->attrs[index]);
18003 *diep = die;
18004 return info_ptr;
18005 }
18006
18007 /* Read a die and all its attributes.
18008 Set DIEP to point to a newly allocated die with its information,
18009 except for its child, sibling, and parent fields. */
18010
18011 static const gdb_byte *
18012 read_full_die (const struct die_reader_specs *reader,
18013 struct die_info **diep, const gdb_byte *info_ptr)
18014 {
18015 const gdb_byte *result;
18016
18017 result = read_full_die_1 (reader, diep, info_ptr, 0);
18018
18019 if (dwarf_die_debug)
18020 {
18021 fprintf_unfiltered (gdb_stdlog,
18022 "Read die from %s@0x%x of %s:\n",
18023 reader->die_section->get_name (),
18024 (unsigned) (info_ptr - reader->die_section->buffer),
18025 bfd_get_filename (reader->abfd));
18026 dump_die (*diep, dwarf_die_debug);
18027 }
18028
18029 return result;
18030 }
18031 \f
18032
18033 /* Returns nonzero if TAG represents a type that we might generate a partial
18034 symbol for. */
18035
18036 static int
18037 is_type_tag_for_partial (int tag)
18038 {
18039 switch (tag)
18040 {
18041 #if 0
18042 /* Some types that would be reasonable to generate partial symbols for,
18043 that we don't at present. */
18044 case DW_TAG_array_type:
18045 case DW_TAG_file_type:
18046 case DW_TAG_ptr_to_member_type:
18047 case DW_TAG_set_type:
18048 case DW_TAG_string_type:
18049 case DW_TAG_subroutine_type:
18050 #endif
18051 case DW_TAG_base_type:
18052 case DW_TAG_class_type:
18053 case DW_TAG_interface_type:
18054 case DW_TAG_enumeration_type:
18055 case DW_TAG_structure_type:
18056 case DW_TAG_subrange_type:
18057 case DW_TAG_typedef:
18058 case DW_TAG_union_type:
18059 return 1;
18060 default:
18061 return 0;
18062 }
18063 }
18064
18065 /* Load all DIEs that are interesting for partial symbols into memory. */
18066
18067 static struct partial_die_info *
18068 load_partial_dies (const struct die_reader_specs *reader,
18069 const gdb_byte *info_ptr, int building_psymtab)
18070 {
18071 struct dwarf2_cu *cu = reader->cu;
18072 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18073 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18074 unsigned int bytes_read;
18075 unsigned int load_all = 0;
18076 int nesting_level = 1;
18077
18078 parent_die = NULL;
18079 last_die = NULL;
18080
18081 gdb_assert (cu->per_cu != NULL);
18082 if (cu->per_cu->load_all_dies)
18083 load_all = 1;
18084
18085 cu->partial_dies
18086 = htab_create_alloc_ex (cu->header.length / 12,
18087 partial_die_hash,
18088 partial_die_eq,
18089 NULL,
18090 &cu->comp_unit_obstack,
18091 hashtab_obstack_allocate,
18092 dummy_obstack_deallocate);
18093
18094 while (1)
18095 {
18096 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18097
18098 /* A NULL abbrev means the end of a series of children. */
18099 if (abbrev == NULL)
18100 {
18101 if (--nesting_level == 0)
18102 return first_die;
18103
18104 info_ptr += bytes_read;
18105 last_die = parent_die;
18106 parent_die = parent_die->die_parent;
18107 continue;
18108 }
18109
18110 /* Check for template arguments. We never save these; if
18111 they're seen, we just mark the parent, and go on our way. */
18112 if (parent_die != NULL
18113 && cu->language == language_cplus
18114 && (abbrev->tag == DW_TAG_template_type_param
18115 || abbrev->tag == DW_TAG_template_value_param))
18116 {
18117 parent_die->has_template_arguments = 1;
18118
18119 if (!load_all)
18120 {
18121 /* We don't need a partial DIE for the template argument. */
18122 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18123 continue;
18124 }
18125 }
18126
18127 /* We only recurse into c++ subprograms looking for template arguments.
18128 Skip their other children. */
18129 if (!load_all
18130 && cu->language == language_cplus
18131 && parent_die != NULL
18132 && parent_die->tag == DW_TAG_subprogram)
18133 {
18134 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18135 continue;
18136 }
18137
18138 /* Check whether this DIE is interesting enough to save. Normally
18139 we would not be interested in members here, but there may be
18140 later variables referencing them via DW_AT_specification (for
18141 static members). */
18142 if (!load_all
18143 && !is_type_tag_for_partial (abbrev->tag)
18144 && abbrev->tag != DW_TAG_constant
18145 && abbrev->tag != DW_TAG_enumerator
18146 && abbrev->tag != DW_TAG_subprogram
18147 && abbrev->tag != DW_TAG_inlined_subroutine
18148 && abbrev->tag != DW_TAG_lexical_block
18149 && abbrev->tag != DW_TAG_variable
18150 && abbrev->tag != DW_TAG_namespace
18151 && abbrev->tag != DW_TAG_module
18152 && abbrev->tag != DW_TAG_member
18153 && abbrev->tag != DW_TAG_imported_unit
18154 && abbrev->tag != DW_TAG_imported_declaration)
18155 {
18156 /* Otherwise we skip to the next sibling, if any. */
18157 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18158 continue;
18159 }
18160
18161 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18162 abbrev);
18163
18164 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18165
18166 /* This two-pass algorithm for processing partial symbols has a
18167 high cost in cache pressure. Thus, handle some simple cases
18168 here which cover the majority of C partial symbols. DIEs
18169 which neither have specification tags in them, nor could have
18170 specification tags elsewhere pointing at them, can simply be
18171 processed and discarded.
18172
18173 This segment is also optional; scan_partial_symbols and
18174 add_partial_symbol will handle these DIEs if we chain
18175 them in normally. When compilers which do not emit large
18176 quantities of duplicate debug information are more common,
18177 this code can probably be removed. */
18178
18179 /* Any complete simple types at the top level (pretty much all
18180 of them, for a language without namespaces), can be processed
18181 directly. */
18182 if (parent_die == NULL
18183 && pdi.has_specification == 0
18184 && pdi.is_declaration == 0
18185 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18186 || pdi.tag == DW_TAG_base_type
18187 || pdi.tag == DW_TAG_subrange_type))
18188 {
18189 if (building_psymtab && pdi.name != NULL)
18190 add_psymbol_to_list (pdi.name, false,
18191 VAR_DOMAIN, LOC_TYPEDEF, -1,
18192 psymbol_placement::STATIC,
18193 0, cu->language, objfile);
18194 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18195 continue;
18196 }
18197
18198 /* The exception for DW_TAG_typedef with has_children above is
18199 a workaround of GCC PR debug/47510. In the case of this complaint
18200 type_name_or_error will error on such types later.
18201
18202 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18203 it could not find the child DIEs referenced later, this is checked
18204 above. In correct DWARF DW_TAG_typedef should have no children. */
18205
18206 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18207 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18208 "- DIE at %s [in module %s]"),
18209 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18210
18211 /* If we're at the second level, and we're an enumerator, and
18212 our parent has no specification (meaning possibly lives in a
18213 namespace elsewhere), then we can add the partial symbol now
18214 instead of queueing it. */
18215 if (pdi.tag == DW_TAG_enumerator
18216 && parent_die != NULL
18217 && parent_die->die_parent == NULL
18218 && parent_die->tag == DW_TAG_enumeration_type
18219 && parent_die->has_specification == 0)
18220 {
18221 if (pdi.name == NULL)
18222 complaint (_("malformed enumerator DIE ignored"));
18223 else if (building_psymtab)
18224 add_psymbol_to_list (pdi.name, false,
18225 VAR_DOMAIN, LOC_CONST, -1,
18226 cu->language == language_cplus
18227 ? psymbol_placement::GLOBAL
18228 : psymbol_placement::STATIC,
18229 0, cu->language, objfile);
18230
18231 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18232 continue;
18233 }
18234
18235 struct partial_die_info *part_die
18236 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18237
18238 /* We'll save this DIE so link it in. */
18239 part_die->die_parent = parent_die;
18240 part_die->die_sibling = NULL;
18241 part_die->die_child = NULL;
18242
18243 if (last_die && last_die == parent_die)
18244 last_die->die_child = part_die;
18245 else if (last_die)
18246 last_die->die_sibling = part_die;
18247
18248 last_die = part_die;
18249
18250 if (first_die == NULL)
18251 first_die = part_die;
18252
18253 /* Maybe add the DIE to the hash table. Not all DIEs that we
18254 find interesting need to be in the hash table, because we
18255 also have the parent/sibling/child chains; only those that we
18256 might refer to by offset later during partial symbol reading.
18257
18258 For now this means things that might have be the target of a
18259 DW_AT_specification, DW_AT_abstract_origin, or
18260 DW_AT_extension. DW_AT_extension will refer only to
18261 namespaces; DW_AT_abstract_origin refers to functions (and
18262 many things under the function DIE, but we do not recurse
18263 into function DIEs during partial symbol reading) and
18264 possibly variables as well; DW_AT_specification refers to
18265 declarations. Declarations ought to have the DW_AT_declaration
18266 flag. It happens that GCC forgets to put it in sometimes, but
18267 only for functions, not for types.
18268
18269 Adding more things than necessary to the hash table is harmless
18270 except for the performance cost. Adding too few will result in
18271 wasted time in find_partial_die, when we reread the compilation
18272 unit with load_all_dies set. */
18273
18274 if (load_all
18275 || abbrev->tag == DW_TAG_constant
18276 || abbrev->tag == DW_TAG_subprogram
18277 || abbrev->tag == DW_TAG_variable
18278 || abbrev->tag == DW_TAG_namespace
18279 || part_die->is_declaration)
18280 {
18281 void **slot;
18282
18283 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18284 to_underlying (part_die->sect_off),
18285 INSERT);
18286 *slot = part_die;
18287 }
18288
18289 /* For some DIEs we want to follow their children (if any). For C
18290 we have no reason to follow the children of structures; for other
18291 languages we have to, so that we can get at method physnames
18292 to infer fully qualified class names, for DW_AT_specification,
18293 and for C++ template arguments. For C++, we also look one level
18294 inside functions to find template arguments (if the name of the
18295 function does not already contain the template arguments).
18296
18297 For Ada and Fortran, we need to scan the children of subprograms
18298 and lexical blocks as well because these languages allow the
18299 definition of nested entities that could be interesting for the
18300 debugger, such as nested subprograms for instance. */
18301 if (last_die->has_children
18302 && (load_all
18303 || last_die->tag == DW_TAG_namespace
18304 || last_die->tag == DW_TAG_module
18305 || last_die->tag == DW_TAG_enumeration_type
18306 || (cu->language == language_cplus
18307 && last_die->tag == DW_TAG_subprogram
18308 && (last_die->name == NULL
18309 || strchr (last_die->name, '<') == NULL))
18310 || (cu->language != language_c
18311 && (last_die->tag == DW_TAG_class_type
18312 || last_die->tag == DW_TAG_interface_type
18313 || last_die->tag == DW_TAG_structure_type
18314 || last_die->tag == DW_TAG_union_type))
18315 || ((cu->language == language_ada
18316 || cu->language == language_fortran)
18317 && (last_die->tag == DW_TAG_subprogram
18318 || last_die->tag == DW_TAG_lexical_block))))
18319 {
18320 nesting_level++;
18321 parent_die = last_die;
18322 continue;
18323 }
18324
18325 /* Otherwise we skip to the next sibling, if any. */
18326 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18327
18328 /* Back to the top, do it again. */
18329 }
18330 }
18331
18332 partial_die_info::partial_die_info (sect_offset sect_off_,
18333 struct abbrev_info *abbrev)
18334 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18335 {
18336 }
18337
18338 /* Read a minimal amount of information into the minimal die structure.
18339 INFO_PTR should point just after the initial uleb128 of a DIE. */
18340
18341 const gdb_byte *
18342 partial_die_info::read (const struct die_reader_specs *reader,
18343 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18344 {
18345 struct dwarf2_cu *cu = reader->cu;
18346 struct dwarf2_per_objfile *dwarf2_per_objfile
18347 = cu->per_cu->dwarf2_per_objfile;
18348 unsigned int i;
18349 int has_low_pc_attr = 0;
18350 int has_high_pc_attr = 0;
18351 int high_pc_relative = 0;
18352
18353 for (i = 0; i < abbrev.num_attrs; ++i)
18354 {
18355 attribute attr;
18356 bool need_reprocess;
18357 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18358 info_ptr, &need_reprocess);
18359 /* String and address offsets that need to do the reprocessing have
18360 already been read at this point, so there is no need to wait until
18361 the loop terminates to do the reprocessing. */
18362 if (need_reprocess)
18363 read_attribute_reprocess (reader, &attr);
18364 /* Store the data if it is of an attribute we want to keep in a
18365 partial symbol table. */
18366 switch (attr.name)
18367 {
18368 case DW_AT_name:
18369 switch (tag)
18370 {
18371 case DW_TAG_compile_unit:
18372 case DW_TAG_partial_unit:
18373 case DW_TAG_type_unit:
18374 /* Compilation units have a DW_AT_name that is a filename, not
18375 a source language identifier. */
18376 case DW_TAG_enumeration_type:
18377 case DW_TAG_enumerator:
18378 /* These tags always have simple identifiers already; no need
18379 to canonicalize them. */
18380 name = DW_STRING (&attr);
18381 break;
18382 default:
18383 {
18384 struct objfile *objfile = dwarf2_per_objfile->objfile;
18385
18386 name
18387 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18388 }
18389 break;
18390 }
18391 break;
18392 case DW_AT_linkage_name:
18393 case DW_AT_MIPS_linkage_name:
18394 /* Note that both forms of linkage name might appear. We
18395 assume they will be the same, and we only store the last
18396 one we see. */
18397 linkage_name = attr.value_as_string ();
18398 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18399 See https://github.com/rust-lang/rust/issues/32925. */
18400 if (cu->language == language_rust && linkage_name != NULL
18401 && strchr (linkage_name, '{') != NULL)
18402 linkage_name = NULL;
18403 break;
18404 case DW_AT_low_pc:
18405 has_low_pc_attr = 1;
18406 lowpc = attr.value_as_address ();
18407 break;
18408 case DW_AT_high_pc:
18409 has_high_pc_attr = 1;
18410 highpc = attr.value_as_address ();
18411 if (cu->header.version >= 4 && attr.form_is_constant ())
18412 high_pc_relative = 1;
18413 break;
18414 case DW_AT_location:
18415 /* Support the .debug_loc offsets. */
18416 if (attr.form_is_block ())
18417 {
18418 d.locdesc = DW_BLOCK (&attr);
18419 }
18420 else if (attr.form_is_section_offset ())
18421 {
18422 dwarf2_complex_location_expr_complaint ();
18423 }
18424 else
18425 {
18426 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18427 "partial symbol information");
18428 }
18429 break;
18430 case DW_AT_external:
18431 is_external = DW_UNSND (&attr);
18432 break;
18433 case DW_AT_declaration:
18434 is_declaration = DW_UNSND (&attr);
18435 break;
18436 case DW_AT_type:
18437 has_type = 1;
18438 break;
18439 case DW_AT_abstract_origin:
18440 case DW_AT_specification:
18441 case DW_AT_extension:
18442 has_specification = 1;
18443 spec_offset = attr.get_ref_die_offset ();
18444 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18445 || cu->per_cu->is_dwz);
18446 break;
18447 case DW_AT_sibling:
18448 /* Ignore absolute siblings, they might point outside of
18449 the current compile unit. */
18450 if (attr.form == DW_FORM_ref_addr)
18451 complaint (_("ignoring absolute DW_AT_sibling"));
18452 else
18453 {
18454 const gdb_byte *buffer = reader->buffer;
18455 sect_offset off = attr.get_ref_die_offset ();
18456 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18457
18458 if (sibling_ptr < info_ptr)
18459 complaint (_("DW_AT_sibling points backwards"));
18460 else if (sibling_ptr > reader->buffer_end)
18461 reader->die_section->overflow_complaint ();
18462 else
18463 sibling = sibling_ptr;
18464 }
18465 break;
18466 case DW_AT_byte_size:
18467 has_byte_size = 1;
18468 break;
18469 case DW_AT_const_value:
18470 has_const_value = 1;
18471 break;
18472 case DW_AT_calling_convention:
18473 /* DWARF doesn't provide a way to identify a program's source-level
18474 entry point. DW_AT_calling_convention attributes are only meant
18475 to describe functions' calling conventions.
18476
18477 However, because it's a necessary piece of information in
18478 Fortran, and before DWARF 4 DW_CC_program was the only
18479 piece of debugging information whose definition refers to
18480 a 'main program' at all, several compilers marked Fortran
18481 main programs with DW_CC_program --- even when those
18482 functions use the standard calling conventions.
18483
18484 Although DWARF now specifies a way to provide this
18485 information, we support this practice for backward
18486 compatibility. */
18487 if (DW_UNSND (&attr) == DW_CC_program
18488 && cu->language == language_fortran)
18489 main_subprogram = 1;
18490 break;
18491 case DW_AT_inline:
18492 if (DW_UNSND (&attr) == DW_INL_inlined
18493 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18494 may_be_inlined = 1;
18495 break;
18496
18497 case DW_AT_import:
18498 if (tag == DW_TAG_imported_unit)
18499 {
18500 d.sect_off = attr.get_ref_die_offset ();
18501 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18502 || cu->per_cu->is_dwz);
18503 }
18504 break;
18505
18506 case DW_AT_main_subprogram:
18507 main_subprogram = DW_UNSND (&attr);
18508 break;
18509
18510 case DW_AT_ranges:
18511 {
18512 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18513 but that requires a full DIE, so instead we just
18514 reimplement it. */
18515 int need_ranges_base = tag != DW_TAG_compile_unit;
18516 unsigned int ranges_offset = (DW_UNSND (&attr)
18517 + (need_ranges_base
18518 ? cu->ranges_base
18519 : 0));
18520
18521 /* Value of the DW_AT_ranges attribute is the offset in the
18522 .debug_ranges section. */
18523 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18524 nullptr))
18525 has_pc_info = 1;
18526 }
18527 break;
18528
18529 default:
18530 break;
18531 }
18532 }
18533
18534 /* For Ada, if both the name and the linkage name appear, we prefer
18535 the latter. This lets "catch exception" work better, regardless
18536 of the order in which the name and linkage name were emitted.
18537 Really, though, this is just a workaround for the fact that gdb
18538 doesn't store both the name and the linkage name. */
18539 if (cu->language == language_ada && linkage_name != nullptr)
18540 name = linkage_name;
18541
18542 if (high_pc_relative)
18543 highpc += lowpc;
18544
18545 if (has_low_pc_attr && has_high_pc_attr)
18546 {
18547 /* When using the GNU linker, .gnu.linkonce. sections are used to
18548 eliminate duplicate copies of functions and vtables and such.
18549 The linker will arbitrarily choose one and discard the others.
18550 The AT_*_pc values for such functions refer to local labels in
18551 these sections. If the section from that file was discarded, the
18552 labels are not in the output, so the relocs get a value of 0.
18553 If this is a discarded function, mark the pc bounds as invalid,
18554 so that GDB will ignore it. */
18555 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18556 {
18557 struct objfile *objfile = dwarf2_per_objfile->objfile;
18558 struct gdbarch *gdbarch = objfile->arch ();
18559
18560 complaint (_("DW_AT_low_pc %s is zero "
18561 "for DIE at %s [in module %s]"),
18562 paddress (gdbarch, lowpc),
18563 sect_offset_str (sect_off),
18564 objfile_name (objfile));
18565 }
18566 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18567 else if (lowpc >= highpc)
18568 {
18569 struct objfile *objfile = dwarf2_per_objfile->objfile;
18570 struct gdbarch *gdbarch = objfile->arch ();
18571
18572 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18573 "for DIE at %s [in module %s]"),
18574 paddress (gdbarch, lowpc),
18575 paddress (gdbarch, highpc),
18576 sect_offset_str (sect_off),
18577 objfile_name (objfile));
18578 }
18579 else
18580 has_pc_info = 1;
18581 }
18582
18583 return info_ptr;
18584 }
18585
18586 /* Find a cached partial DIE at OFFSET in CU. */
18587
18588 struct partial_die_info *
18589 dwarf2_cu::find_partial_die (sect_offset sect_off)
18590 {
18591 struct partial_die_info *lookup_die = NULL;
18592 struct partial_die_info part_die (sect_off);
18593
18594 lookup_die = ((struct partial_die_info *)
18595 htab_find_with_hash (partial_dies, &part_die,
18596 to_underlying (sect_off)));
18597
18598 return lookup_die;
18599 }
18600
18601 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18602 except in the case of .debug_types DIEs which do not reference
18603 outside their CU (they do however referencing other types via
18604 DW_FORM_ref_sig8). */
18605
18606 static const struct cu_partial_die_info
18607 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18608 {
18609 struct dwarf2_per_objfile *dwarf2_per_objfile
18610 = cu->per_cu->dwarf2_per_objfile;
18611 struct objfile *objfile = dwarf2_per_objfile->objfile;
18612 struct dwarf2_per_cu_data *per_cu = NULL;
18613 struct partial_die_info *pd = NULL;
18614
18615 if (offset_in_dwz == cu->per_cu->is_dwz
18616 && cu->header.offset_in_cu_p (sect_off))
18617 {
18618 pd = cu->find_partial_die (sect_off);
18619 if (pd != NULL)
18620 return { cu, pd };
18621 /* We missed recording what we needed.
18622 Load all dies and try again. */
18623 per_cu = cu->per_cu;
18624 }
18625 else
18626 {
18627 /* TUs don't reference other CUs/TUs (except via type signatures). */
18628 if (cu->per_cu->is_debug_types)
18629 {
18630 error (_("Dwarf Error: Type Unit at offset %s contains"
18631 " external reference to offset %s [in module %s].\n"),
18632 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18633 bfd_get_filename (objfile->obfd));
18634 }
18635 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18636 dwarf2_per_objfile);
18637
18638 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18639 load_partial_comp_unit (per_cu);
18640
18641 per_cu->cu->last_used = 0;
18642 pd = per_cu->cu->find_partial_die (sect_off);
18643 }
18644
18645 /* If we didn't find it, and not all dies have been loaded,
18646 load them all and try again. */
18647
18648 if (pd == NULL && per_cu->load_all_dies == 0)
18649 {
18650 per_cu->load_all_dies = 1;
18651
18652 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18653 THIS_CU->cu may already be in use. So we can't just free it and
18654 replace its DIEs with the ones we read in. Instead, we leave those
18655 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18656 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18657 set. */
18658 load_partial_comp_unit (per_cu);
18659
18660 pd = per_cu->cu->find_partial_die (sect_off);
18661 }
18662
18663 if (pd == NULL)
18664 internal_error (__FILE__, __LINE__,
18665 _("could not find partial DIE %s "
18666 "in cache [from module %s]\n"),
18667 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18668 return { per_cu->cu, pd };
18669 }
18670
18671 /* See if we can figure out if the class lives in a namespace. We do
18672 this by looking for a member function; its demangled name will
18673 contain namespace info, if there is any. */
18674
18675 static void
18676 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18677 struct dwarf2_cu *cu)
18678 {
18679 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18680 what template types look like, because the demangler
18681 frequently doesn't give the same name as the debug info. We
18682 could fix this by only using the demangled name to get the
18683 prefix (but see comment in read_structure_type). */
18684
18685 struct partial_die_info *real_pdi;
18686 struct partial_die_info *child_pdi;
18687
18688 /* If this DIE (this DIE's specification, if any) has a parent, then
18689 we should not do this. We'll prepend the parent's fully qualified
18690 name when we create the partial symbol. */
18691
18692 real_pdi = struct_pdi;
18693 while (real_pdi->has_specification)
18694 {
18695 auto res = find_partial_die (real_pdi->spec_offset,
18696 real_pdi->spec_is_dwz, cu);
18697 real_pdi = res.pdi;
18698 cu = res.cu;
18699 }
18700
18701 if (real_pdi->die_parent != NULL)
18702 return;
18703
18704 for (child_pdi = struct_pdi->die_child;
18705 child_pdi != NULL;
18706 child_pdi = child_pdi->die_sibling)
18707 {
18708 if (child_pdi->tag == DW_TAG_subprogram
18709 && child_pdi->linkage_name != NULL)
18710 {
18711 gdb::unique_xmalloc_ptr<char> actual_class_name
18712 (language_class_name_from_physname (cu->language_defn,
18713 child_pdi->linkage_name));
18714 if (actual_class_name != NULL)
18715 {
18716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18717 struct_pdi->name = objfile->intern (actual_class_name.get ());
18718 }
18719 break;
18720 }
18721 }
18722 }
18723
18724 /* Return true if a DIE with TAG may have the DW_AT_const_value
18725 attribute. */
18726
18727 static bool
18728 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18729 {
18730 switch (tag)
18731 {
18732 case DW_TAG_constant:
18733 case DW_TAG_enumerator:
18734 case DW_TAG_formal_parameter:
18735 case DW_TAG_template_value_param:
18736 case DW_TAG_variable:
18737 return true;
18738 }
18739
18740 return false;
18741 }
18742
18743 void
18744 partial_die_info::fixup (struct dwarf2_cu *cu)
18745 {
18746 /* Once we've fixed up a die, there's no point in doing so again.
18747 This also avoids a memory leak if we were to call
18748 guess_partial_die_structure_name multiple times. */
18749 if (fixup_called)
18750 return;
18751
18752 /* If we found a reference attribute and the DIE has no name, try
18753 to find a name in the referred to DIE. */
18754
18755 if (name == NULL && has_specification)
18756 {
18757 struct partial_die_info *spec_die;
18758
18759 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18760 spec_die = res.pdi;
18761 cu = res.cu;
18762
18763 spec_die->fixup (cu);
18764
18765 if (spec_die->name)
18766 {
18767 name = spec_die->name;
18768
18769 /* Copy DW_AT_external attribute if it is set. */
18770 if (spec_die->is_external)
18771 is_external = spec_die->is_external;
18772 }
18773 }
18774
18775 if (!has_const_value && has_specification
18776 && can_have_DW_AT_const_value_p (tag))
18777 {
18778 struct partial_die_info *spec_die;
18779
18780 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18781 spec_die = res.pdi;
18782 cu = res.cu;
18783
18784 spec_die->fixup (cu);
18785
18786 if (spec_die->has_const_value)
18787 {
18788 /* Copy DW_AT_const_value attribute if it is set. */
18789 has_const_value = spec_die->has_const_value;
18790 }
18791 }
18792
18793 /* Set default names for some unnamed DIEs. */
18794
18795 if (name == NULL && tag == DW_TAG_namespace)
18796 name = CP_ANONYMOUS_NAMESPACE_STR;
18797
18798 /* If there is no parent die to provide a namespace, and there are
18799 children, see if we can determine the namespace from their linkage
18800 name. */
18801 if (cu->language == language_cplus
18802 && !cu->per_cu->dwarf2_per_objfile->per_bfd->types.empty ()
18803 && die_parent == NULL
18804 && has_children
18805 && (tag == DW_TAG_class_type
18806 || tag == DW_TAG_structure_type
18807 || tag == DW_TAG_union_type))
18808 guess_partial_die_structure_name (this, cu);
18809
18810 /* GCC might emit a nameless struct or union that has a linkage
18811 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18812 if (name == NULL
18813 && (tag == DW_TAG_class_type
18814 || tag == DW_TAG_interface_type
18815 || tag == DW_TAG_structure_type
18816 || tag == DW_TAG_union_type)
18817 && linkage_name != NULL)
18818 {
18819 gdb::unique_xmalloc_ptr<char> demangled
18820 (gdb_demangle (linkage_name, DMGL_TYPES));
18821 if (demangled != nullptr)
18822 {
18823 const char *base;
18824
18825 /* Strip any leading namespaces/classes, keep only the base name.
18826 DW_AT_name for named DIEs does not contain the prefixes. */
18827 base = strrchr (demangled.get (), ':');
18828 if (base && base > demangled.get () && base[-1] == ':')
18829 base++;
18830 else
18831 base = demangled.get ();
18832
18833 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18834 name = objfile->intern (base);
18835 }
18836 }
18837
18838 fixup_called = 1;
18839 }
18840
18841 /* Read the .debug_loclists header contents from the given SECTION in the
18842 HEADER. */
18843 static void
18844 read_loclist_header (struct loclist_header *header,
18845 struct dwarf2_section_info *section)
18846 {
18847 unsigned int bytes_read;
18848 bfd *abfd = section->get_bfd_owner ();
18849 const gdb_byte *info_ptr = section->buffer;
18850 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18851 info_ptr += bytes_read;
18852 header->version = read_2_bytes (abfd, info_ptr);
18853 info_ptr += 2;
18854 header->addr_size = read_1_byte (abfd, info_ptr);
18855 info_ptr += 1;
18856 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18857 info_ptr += 1;
18858 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18859 }
18860
18861 /* Return the DW_AT_loclists_base value for the CU. */
18862 static ULONGEST
18863 lookup_loclist_base (struct dwarf2_cu *cu)
18864 {
18865 /* For the .dwo unit, the loclist_base points to the first offset following
18866 the header. The header consists of the following entities-
18867 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18868 bit format)
18869 2. version (2 bytes)
18870 3. address size (1 byte)
18871 4. segment selector size (1 byte)
18872 5. offset entry count (4 bytes)
18873 These sizes are derived as per the DWARFv5 standard. */
18874 if (cu->dwo_unit != nullptr)
18875 {
18876 if (cu->header.initial_length_size == 4)
18877 return LOCLIST_HEADER_SIZE32;
18878 return LOCLIST_HEADER_SIZE64;
18879 }
18880 return cu->loclist_base;
18881 }
18882
18883 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18884 array of offsets in the .debug_loclists section. */
18885 static CORE_ADDR
18886 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18887 {
18888 struct dwarf2_per_objfile *dwarf2_per_objfile
18889 = cu->per_cu->dwarf2_per_objfile;
18890 struct objfile *objfile = dwarf2_per_objfile->objfile;
18891 bfd *abfd = objfile->obfd;
18892 ULONGEST loclist_base = lookup_loclist_base (cu);
18893 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18894
18895 section->read (objfile);
18896 if (section->buffer == NULL)
18897 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18898 "section [in module %s]"), objfile_name (objfile));
18899 struct loclist_header header;
18900 read_loclist_header (&header, section);
18901 if (loclist_index >= header.offset_entry_count)
18902 complaint (_("DW_FORM_loclistx pointing outside of "
18903 ".debug_loclists offset array [in module %s]"),
18904 objfile_name (objfile));
18905 if (loclist_base + loclist_index * cu->header.offset_size
18906 >= section->size)
18907 complaint (_("DW_FORM_loclistx pointing outside of "
18908 ".debug_loclists section [in module %s]"),
18909 objfile_name (objfile));
18910 const gdb_byte *info_ptr
18911 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18912
18913 if (cu->header.offset_size == 4)
18914 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18915 else
18916 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18917 }
18918
18919 /* Process the attributes that had to be skipped in the first round. These
18920 attributes are the ones that need str_offsets_base or addr_base attributes.
18921 They could not have been processed in the first round, because at the time
18922 the values of str_offsets_base or addr_base may not have been known. */
18923 static void
18924 read_attribute_reprocess (const struct die_reader_specs *reader,
18925 struct attribute *attr)
18926 {
18927 struct dwarf2_cu *cu = reader->cu;
18928 switch (attr->form)
18929 {
18930 case DW_FORM_addrx:
18931 case DW_FORM_GNU_addr_index:
18932 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18933 break;
18934 case DW_FORM_loclistx:
18935 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18936 break;
18937 case DW_FORM_strx:
18938 case DW_FORM_strx1:
18939 case DW_FORM_strx2:
18940 case DW_FORM_strx3:
18941 case DW_FORM_strx4:
18942 case DW_FORM_GNU_str_index:
18943 {
18944 unsigned int str_index = DW_UNSND (attr);
18945 if (reader->dwo_file != NULL)
18946 {
18947 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18948 DW_STRING_IS_CANONICAL (attr) = 0;
18949 }
18950 else
18951 {
18952 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18953 DW_STRING_IS_CANONICAL (attr) = 0;
18954 }
18955 break;
18956 }
18957 default:
18958 gdb_assert_not_reached (_("Unexpected DWARF form."));
18959 }
18960 }
18961
18962 /* Read an attribute value described by an attribute form. */
18963
18964 static const gdb_byte *
18965 read_attribute_value (const struct die_reader_specs *reader,
18966 struct attribute *attr, unsigned form,
18967 LONGEST implicit_const, const gdb_byte *info_ptr,
18968 bool *need_reprocess)
18969 {
18970 struct dwarf2_cu *cu = reader->cu;
18971 struct dwarf2_per_objfile *dwarf2_per_objfile
18972 = cu->per_cu->dwarf2_per_objfile;
18973 struct objfile *objfile = dwarf2_per_objfile->objfile;
18974 bfd *abfd = reader->abfd;
18975 struct comp_unit_head *cu_header = &cu->header;
18976 unsigned int bytes_read;
18977 struct dwarf_block *blk;
18978 *need_reprocess = false;
18979
18980 attr->form = (enum dwarf_form) form;
18981 switch (form)
18982 {
18983 case DW_FORM_ref_addr:
18984 if (cu->header.version == 2)
18985 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18986 &bytes_read);
18987 else
18988 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18989 &bytes_read);
18990 info_ptr += bytes_read;
18991 break;
18992 case DW_FORM_GNU_ref_alt:
18993 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18994 info_ptr += bytes_read;
18995 break;
18996 case DW_FORM_addr:
18997 {
18998 struct gdbarch *gdbarch = objfile->arch ();
18999 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19000 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19001 info_ptr += bytes_read;
19002 }
19003 break;
19004 case DW_FORM_block2:
19005 blk = dwarf_alloc_block (cu);
19006 blk->size = read_2_bytes (abfd, info_ptr);
19007 info_ptr += 2;
19008 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19009 info_ptr += blk->size;
19010 DW_BLOCK (attr) = blk;
19011 break;
19012 case DW_FORM_block4:
19013 blk = dwarf_alloc_block (cu);
19014 blk->size = read_4_bytes (abfd, info_ptr);
19015 info_ptr += 4;
19016 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19017 info_ptr += blk->size;
19018 DW_BLOCK (attr) = blk;
19019 break;
19020 case DW_FORM_data2:
19021 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19022 info_ptr += 2;
19023 break;
19024 case DW_FORM_data4:
19025 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19026 info_ptr += 4;
19027 break;
19028 case DW_FORM_data8:
19029 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19030 info_ptr += 8;
19031 break;
19032 case DW_FORM_data16:
19033 blk = dwarf_alloc_block (cu);
19034 blk->size = 16;
19035 blk->data = read_n_bytes (abfd, info_ptr, 16);
19036 info_ptr += 16;
19037 DW_BLOCK (attr) = blk;
19038 break;
19039 case DW_FORM_sec_offset:
19040 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19041 info_ptr += bytes_read;
19042 break;
19043 case DW_FORM_loclistx:
19044 {
19045 *need_reprocess = true;
19046 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19047 info_ptr += bytes_read;
19048 }
19049 break;
19050 case DW_FORM_string:
19051 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19052 DW_STRING_IS_CANONICAL (attr) = 0;
19053 info_ptr += bytes_read;
19054 break;
19055 case DW_FORM_strp:
19056 if (!cu->per_cu->is_dwz)
19057 {
19058 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19059 abfd, info_ptr, cu_header,
19060 &bytes_read);
19061 DW_STRING_IS_CANONICAL (attr) = 0;
19062 info_ptr += bytes_read;
19063 break;
19064 }
19065 /* FALLTHROUGH */
19066 case DW_FORM_line_strp:
19067 if (!cu->per_cu->is_dwz)
19068 {
19069 DW_STRING (attr)
19070 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19071 &bytes_read);
19072 DW_STRING_IS_CANONICAL (attr) = 0;
19073 info_ptr += bytes_read;
19074 break;
19075 }
19076 /* FALLTHROUGH */
19077 case DW_FORM_GNU_strp_alt:
19078 {
19079 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19080 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19081 &bytes_read);
19082
19083 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19084 DW_STRING_IS_CANONICAL (attr) = 0;
19085 info_ptr += bytes_read;
19086 }
19087 break;
19088 case DW_FORM_exprloc:
19089 case DW_FORM_block:
19090 blk = dwarf_alloc_block (cu);
19091 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19094 info_ptr += blk->size;
19095 DW_BLOCK (attr) = blk;
19096 break;
19097 case DW_FORM_block1:
19098 blk = dwarf_alloc_block (cu);
19099 blk->size = read_1_byte (abfd, info_ptr);
19100 info_ptr += 1;
19101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19102 info_ptr += blk->size;
19103 DW_BLOCK (attr) = blk;
19104 break;
19105 case DW_FORM_data1:
19106 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19107 info_ptr += 1;
19108 break;
19109 case DW_FORM_flag:
19110 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19111 info_ptr += 1;
19112 break;
19113 case DW_FORM_flag_present:
19114 DW_UNSND (attr) = 1;
19115 break;
19116 case DW_FORM_sdata:
19117 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19118 info_ptr += bytes_read;
19119 break;
19120 case DW_FORM_udata:
19121 case DW_FORM_rnglistx:
19122 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19123 info_ptr += bytes_read;
19124 break;
19125 case DW_FORM_ref1:
19126 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19127 + read_1_byte (abfd, info_ptr));
19128 info_ptr += 1;
19129 break;
19130 case DW_FORM_ref2:
19131 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19132 + read_2_bytes (abfd, info_ptr));
19133 info_ptr += 2;
19134 break;
19135 case DW_FORM_ref4:
19136 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19137 + read_4_bytes (abfd, info_ptr));
19138 info_ptr += 4;
19139 break;
19140 case DW_FORM_ref8:
19141 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19142 + read_8_bytes (abfd, info_ptr));
19143 info_ptr += 8;
19144 break;
19145 case DW_FORM_ref_sig8:
19146 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19147 info_ptr += 8;
19148 break;
19149 case DW_FORM_ref_udata:
19150 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19151 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19152 info_ptr += bytes_read;
19153 break;
19154 case DW_FORM_indirect:
19155 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19156 info_ptr += bytes_read;
19157 if (form == DW_FORM_implicit_const)
19158 {
19159 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19160 info_ptr += bytes_read;
19161 }
19162 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19163 info_ptr, need_reprocess);
19164 break;
19165 case DW_FORM_implicit_const:
19166 DW_SND (attr) = implicit_const;
19167 break;
19168 case DW_FORM_addrx:
19169 case DW_FORM_GNU_addr_index:
19170 *need_reprocess = true;
19171 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19172 info_ptr += bytes_read;
19173 break;
19174 case DW_FORM_strx:
19175 case DW_FORM_strx1:
19176 case DW_FORM_strx2:
19177 case DW_FORM_strx3:
19178 case DW_FORM_strx4:
19179 case DW_FORM_GNU_str_index:
19180 {
19181 ULONGEST str_index;
19182 if (form == DW_FORM_strx1)
19183 {
19184 str_index = read_1_byte (abfd, info_ptr);
19185 info_ptr += 1;
19186 }
19187 else if (form == DW_FORM_strx2)
19188 {
19189 str_index = read_2_bytes (abfd, info_ptr);
19190 info_ptr += 2;
19191 }
19192 else if (form == DW_FORM_strx3)
19193 {
19194 str_index = read_3_bytes (abfd, info_ptr);
19195 info_ptr += 3;
19196 }
19197 else if (form == DW_FORM_strx4)
19198 {
19199 str_index = read_4_bytes (abfd, info_ptr);
19200 info_ptr += 4;
19201 }
19202 else
19203 {
19204 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19205 info_ptr += bytes_read;
19206 }
19207 *need_reprocess = true;
19208 DW_UNSND (attr) = str_index;
19209 }
19210 break;
19211 default:
19212 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19213 dwarf_form_name (form),
19214 bfd_get_filename (abfd));
19215 }
19216
19217 /* Super hack. */
19218 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19219 attr->form = DW_FORM_GNU_ref_alt;
19220
19221 /* We have seen instances where the compiler tried to emit a byte
19222 size attribute of -1 which ended up being encoded as an unsigned
19223 0xffffffff. Although 0xffffffff is technically a valid size value,
19224 an object of this size seems pretty unlikely so we can relatively
19225 safely treat these cases as if the size attribute was invalid and
19226 treat them as zero by default. */
19227 if (attr->name == DW_AT_byte_size
19228 && form == DW_FORM_data4
19229 && DW_UNSND (attr) >= 0xffffffff)
19230 {
19231 complaint
19232 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19233 hex_string (DW_UNSND (attr)));
19234 DW_UNSND (attr) = 0;
19235 }
19236
19237 return info_ptr;
19238 }
19239
19240 /* Read an attribute described by an abbreviated attribute. */
19241
19242 static const gdb_byte *
19243 read_attribute (const struct die_reader_specs *reader,
19244 struct attribute *attr, struct attr_abbrev *abbrev,
19245 const gdb_byte *info_ptr, bool *need_reprocess)
19246 {
19247 attr->name = abbrev->name;
19248 return read_attribute_value (reader, attr, abbrev->form,
19249 abbrev->implicit_const, info_ptr,
19250 need_reprocess);
19251 }
19252
19253 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19254
19255 static const char *
19256 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19257 LONGEST str_offset)
19258 {
19259 return dwarf2_per_objfile->per_bfd->str.read_string
19260 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19261 }
19262
19263 /* Return pointer to string at .debug_str offset as read from BUF.
19264 BUF is assumed to be in a compilation unit described by CU_HEADER.
19265 Return *BYTES_READ_PTR count of bytes read from BUF. */
19266
19267 static const char *
19268 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19269 const gdb_byte *buf,
19270 const struct comp_unit_head *cu_header,
19271 unsigned int *bytes_read_ptr)
19272 {
19273 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19274
19275 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19276 }
19277
19278 /* See read.h. */
19279
19280 const char *
19281 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19282 const struct comp_unit_head *cu_header,
19283 unsigned int *bytes_read_ptr)
19284 {
19285 bfd *abfd = objfile->obfd;
19286 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19287
19288 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19289 }
19290
19291 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19292 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19293 ADDR_SIZE is the size of addresses from the CU header. */
19294
19295 static CORE_ADDR
19296 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19297 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19298 int addr_size)
19299 {
19300 struct objfile *objfile = dwarf2_per_objfile->objfile;
19301 bfd *abfd = objfile->obfd;
19302 const gdb_byte *info_ptr;
19303 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19304
19305 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19306 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19307 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19308 objfile_name (objfile));
19309 if (addr_base_or_zero + addr_index * addr_size
19310 >= dwarf2_per_objfile->per_bfd->addr.size)
19311 error (_("DW_FORM_addr_index pointing outside of "
19312 ".debug_addr section [in module %s]"),
19313 objfile_name (objfile));
19314 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19315 + addr_base_or_zero + addr_index * addr_size);
19316 if (addr_size == 4)
19317 return bfd_get_32 (abfd, info_ptr);
19318 else
19319 return bfd_get_64 (abfd, info_ptr);
19320 }
19321
19322 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19323
19324 static CORE_ADDR
19325 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19326 {
19327 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19328 cu->addr_base, cu->header.addr_size);
19329 }
19330
19331 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19332
19333 static CORE_ADDR
19334 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19335 unsigned int *bytes_read)
19336 {
19337 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19338 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19339
19340 return read_addr_index (cu, addr_index);
19341 }
19342
19343 /* See read.h. */
19344
19345 CORE_ADDR
19346 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19347 {
19348 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19349 struct dwarf2_cu *cu = per_cu->cu;
19350 gdb::optional<ULONGEST> addr_base;
19351 int addr_size;
19352
19353 /* We need addr_base and addr_size.
19354 If we don't have PER_CU->cu, we have to get it.
19355 Nasty, but the alternative is storing the needed info in PER_CU,
19356 which at this point doesn't seem justified: it's not clear how frequently
19357 it would get used and it would increase the size of every PER_CU.
19358 Entry points like dwarf2_per_cu_addr_size do a similar thing
19359 so we're not in uncharted territory here.
19360 Alas we need to be a bit more complicated as addr_base is contained
19361 in the DIE.
19362
19363 We don't need to read the entire CU(/TU).
19364 We just need the header and top level die.
19365
19366 IWBN to use the aging mechanism to let us lazily later discard the CU.
19367 For now we skip this optimization. */
19368
19369 if (cu != NULL)
19370 {
19371 addr_base = cu->addr_base;
19372 addr_size = cu->header.addr_size;
19373 }
19374 else
19375 {
19376 cutu_reader reader (per_cu, NULL, 0, false);
19377 addr_base = reader.cu->addr_base;
19378 addr_size = reader.cu->header.addr_size;
19379 }
19380
19381 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19382 addr_size);
19383 }
19384
19385 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19386 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19387 DWO file. */
19388
19389 static const char *
19390 read_str_index (struct dwarf2_cu *cu,
19391 struct dwarf2_section_info *str_section,
19392 struct dwarf2_section_info *str_offsets_section,
19393 ULONGEST str_offsets_base, ULONGEST str_index)
19394 {
19395 struct dwarf2_per_objfile *dwarf2_per_objfile
19396 = cu->per_cu->dwarf2_per_objfile;
19397 struct objfile *objfile = dwarf2_per_objfile->objfile;
19398 const char *objf_name = objfile_name (objfile);
19399 bfd *abfd = objfile->obfd;
19400 const gdb_byte *info_ptr;
19401 ULONGEST str_offset;
19402 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19403
19404 str_section->read (objfile);
19405 str_offsets_section->read (objfile);
19406 if (str_section->buffer == NULL)
19407 error (_("%s used without %s section"
19408 " in CU at offset %s [in module %s]"),
19409 form_name, str_section->get_name (),
19410 sect_offset_str (cu->header.sect_off), objf_name);
19411 if (str_offsets_section->buffer == NULL)
19412 error (_("%s used without %s section"
19413 " in CU at offset %s [in module %s]"),
19414 form_name, str_section->get_name (),
19415 sect_offset_str (cu->header.sect_off), objf_name);
19416 info_ptr = (str_offsets_section->buffer
19417 + str_offsets_base
19418 + str_index * cu->header.offset_size);
19419 if (cu->header.offset_size == 4)
19420 str_offset = bfd_get_32 (abfd, info_ptr);
19421 else
19422 str_offset = bfd_get_64 (abfd, info_ptr);
19423 if (str_offset >= str_section->size)
19424 error (_("Offset from %s pointing outside of"
19425 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19426 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19427 return (const char *) (str_section->buffer + str_offset);
19428 }
19429
19430 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19431
19432 static const char *
19433 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19434 {
19435 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19436 ? reader->cu->header.addr_size : 0;
19437 return read_str_index (reader->cu,
19438 &reader->dwo_file->sections.str,
19439 &reader->dwo_file->sections.str_offsets,
19440 str_offsets_base, str_index);
19441 }
19442
19443 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19444
19445 static const char *
19446 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19447 {
19448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19449 const char *objf_name = objfile_name (objfile);
19450 static const char form_name[] = "DW_FORM_GNU_str_index";
19451 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19452
19453 if (!cu->str_offsets_base.has_value ())
19454 error (_("%s used in Fission stub without %s"
19455 " in CU at offset 0x%lx [in module %s]"),
19456 form_name, str_offsets_attr_name,
19457 (long) cu->header.offset_size, objf_name);
19458
19459 return read_str_index (cu,
19460 &cu->per_cu->dwarf2_per_objfile->per_bfd->str,
19461 &cu->per_cu->dwarf2_per_objfile->per_bfd->str_offsets,
19462 *cu->str_offsets_base, str_index);
19463 }
19464
19465 /* Return the length of an LEB128 number in BUF. */
19466
19467 static int
19468 leb128_size (const gdb_byte *buf)
19469 {
19470 const gdb_byte *begin = buf;
19471 gdb_byte byte;
19472
19473 while (1)
19474 {
19475 byte = *buf++;
19476 if ((byte & 128) == 0)
19477 return buf - begin;
19478 }
19479 }
19480
19481 static void
19482 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19483 {
19484 switch (lang)
19485 {
19486 case DW_LANG_C89:
19487 case DW_LANG_C99:
19488 case DW_LANG_C11:
19489 case DW_LANG_C:
19490 case DW_LANG_UPC:
19491 cu->language = language_c;
19492 break;
19493 case DW_LANG_Java:
19494 case DW_LANG_C_plus_plus:
19495 case DW_LANG_C_plus_plus_11:
19496 case DW_LANG_C_plus_plus_14:
19497 cu->language = language_cplus;
19498 break;
19499 case DW_LANG_D:
19500 cu->language = language_d;
19501 break;
19502 case DW_LANG_Fortran77:
19503 case DW_LANG_Fortran90:
19504 case DW_LANG_Fortran95:
19505 case DW_LANG_Fortran03:
19506 case DW_LANG_Fortran08:
19507 cu->language = language_fortran;
19508 break;
19509 case DW_LANG_Go:
19510 cu->language = language_go;
19511 break;
19512 case DW_LANG_Mips_Assembler:
19513 cu->language = language_asm;
19514 break;
19515 case DW_LANG_Ada83:
19516 case DW_LANG_Ada95:
19517 cu->language = language_ada;
19518 break;
19519 case DW_LANG_Modula2:
19520 cu->language = language_m2;
19521 break;
19522 case DW_LANG_Pascal83:
19523 cu->language = language_pascal;
19524 break;
19525 case DW_LANG_ObjC:
19526 cu->language = language_objc;
19527 break;
19528 case DW_LANG_Rust:
19529 case DW_LANG_Rust_old:
19530 cu->language = language_rust;
19531 break;
19532 case DW_LANG_Cobol74:
19533 case DW_LANG_Cobol85:
19534 default:
19535 cu->language = language_minimal;
19536 break;
19537 }
19538 cu->language_defn = language_def (cu->language);
19539 }
19540
19541 /* Return the named attribute or NULL if not there. */
19542
19543 static struct attribute *
19544 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19545 {
19546 for (;;)
19547 {
19548 unsigned int i;
19549 struct attribute *spec = NULL;
19550
19551 for (i = 0; i < die->num_attrs; ++i)
19552 {
19553 if (die->attrs[i].name == name)
19554 return &die->attrs[i];
19555 if (die->attrs[i].name == DW_AT_specification
19556 || die->attrs[i].name == DW_AT_abstract_origin)
19557 spec = &die->attrs[i];
19558 }
19559
19560 if (!spec)
19561 break;
19562
19563 die = follow_die_ref (die, spec, &cu);
19564 }
19565
19566 return NULL;
19567 }
19568
19569 /* Return the string associated with a string-typed attribute, or NULL if it
19570 is either not found or is of an incorrect type. */
19571
19572 static const char *
19573 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19574 {
19575 struct attribute *attr;
19576 const char *str = NULL;
19577
19578 attr = dwarf2_attr (die, name, cu);
19579
19580 if (attr != NULL)
19581 {
19582 str = attr->value_as_string ();
19583 if (str == nullptr)
19584 complaint (_("string type expected for attribute %s for "
19585 "DIE at %s in module %s"),
19586 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19587 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19588 }
19589
19590 return str;
19591 }
19592
19593 /* Return the dwo name or NULL if not present. If present, it is in either
19594 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19595 static const char *
19596 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19597 {
19598 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19599 if (dwo_name == nullptr)
19600 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19601 return dwo_name;
19602 }
19603
19604 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19605 and holds a non-zero value. This function should only be used for
19606 DW_FORM_flag or DW_FORM_flag_present attributes. */
19607
19608 static int
19609 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19610 {
19611 struct attribute *attr = dwarf2_attr (die, name, cu);
19612
19613 return (attr && DW_UNSND (attr));
19614 }
19615
19616 static int
19617 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19618 {
19619 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19620 which value is non-zero. However, we have to be careful with
19621 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19622 (via dwarf2_flag_true_p) follows this attribute. So we may
19623 end up accidently finding a declaration attribute that belongs
19624 to a different DIE referenced by the specification attribute,
19625 even though the given DIE does not have a declaration attribute. */
19626 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19627 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19628 }
19629
19630 /* Return the die giving the specification for DIE, if there is
19631 one. *SPEC_CU is the CU containing DIE on input, and the CU
19632 containing the return value on output. If there is no
19633 specification, but there is an abstract origin, that is
19634 returned. */
19635
19636 static struct die_info *
19637 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19638 {
19639 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19640 *spec_cu);
19641
19642 if (spec_attr == NULL)
19643 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19644
19645 if (spec_attr == NULL)
19646 return NULL;
19647 else
19648 return follow_die_ref (die, spec_attr, spec_cu);
19649 }
19650
19651 /* Stub for free_line_header to match void * callback types. */
19652
19653 static void
19654 free_line_header_voidp (void *arg)
19655 {
19656 struct line_header *lh = (struct line_header *) arg;
19657
19658 delete lh;
19659 }
19660
19661 /* A convenience function to find the proper .debug_line section for a CU. */
19662
19663 static struct dwarf2_section_info *
19664 get_debug_line_section (struct dwarf2_cu *cu)
19665 {
19666 struct dwarf2_section_info *section;
19667 struct dwarf2_per_objfile *dwarf2_per_objfile
19668 = cu->per_cu->dwarf2_per_objfile;
19669
19670 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19671 DWO file. */
19672 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19673 section = &cu->dwo_unit->dwo_file->sections.line;
19674 else if (cu->per_cu->is_dwz)
19675 {
19676 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19677
19678 section = &dwz->line;
19679 }
19680 else
19681 section = &dwarf2_per_objfile->per_bfd->line;
19682
19683 return section;
19684 }
19685
19686 /* Read the statement program header starting at OFFSET in
19687 .debug_line, or .debug_line.dwo. Return a pointer
19688 to a struct line_header, allocated using xmalloc.
19689 Returns NULL if there is a problem reading the header, e.g., if it
19690 has a version we don't understand.
19691
19692 NOTE: the strings in the include directory and file name tables of
19693 the returned object point into the dwarf line section buffer,
19694 and must not be freed. */
19695
19696 static line_header_up
19697 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19698 {
19699 struct dwarf2_section_info *section;
19700 struct dwarf2_per_objfile *dwarf2_per_objfile
19701 = cu->per_cu->dwarf2_per_objfile;
19702
19703 section = get_debug_line_section (cu);
19704 section->read (dwarf2_per_objfile->objfile);
19705 if (section->buffer == NULL)
19706 {
19707 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19708 complaint (_("missing .debug_line.dwo section"));
19709 else
19710 complaint (_("missing .debug_line section"));
19711 return 0;
19712 }
19713
19714 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19715 dwarf2_per_objfile, section,
19716 &cu->header);
19717 }
19718
19719 /* Subroutine of dwarf_decode_lines to simplify it.
19720 Return the file name of the psymtab for the given file_entry.
19721 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19722 If space for the result is malloc'd, *NAME_HOLDER will be set.
19723 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19724
19725 static const char *
19726 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19727 const dwarf2_psymtab *pst,
19728 const char *comp_dir,
19729 gdb::unique_xmalloc_ptr<char> *name_holder)
19730 {
19731 const char *include_name = fe.name;
19732 const char *include_name_to_compare = include_name;
19733 const char *pst_filename;
19734 int file_is_pst;
19735
19736 const char *dir_name = fe.include_dir (lh);
19737
19738 gdb::unique_xmalloc_ptr<char> hold_compare;
19739 if (!IS_ABSOLUTE_PATH (include_name)
19740 && (dir_name != NULL || comp_dir != NULL))
19741 {
19742 /* Avoid creating a duplicate psymtab for PST.
19743 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19744 Before we do the comparison, however, we need to account
19745 for DIR_NAME and COMP_DIR.
19746 First prepend dir_name (if non-NULL). If we still don't
19747 have an absolute path prepend comp_dir (if non-NULL).
19748 However, the directory we record in the include-file's
19749 psymtab does not contain COMP_DIR (to match the
19750 corresponding symtab(s)).
19751
19752 Example:
19753
19754 bash$ cd /tmp
19755 bash$ gcc -g ./hello.c
19756 include_name = "hello.c"
19757 dir_name = "."
19758 DW_AT_comp_dir = comp_dir = "/tmp"
19759 DW_AT_name = "./hello.c"
19760
19761 */
19762
19763 if (dir_name != NULL)
19764 {
19765 name_holder->reset (concat (dir_name, SLASH_STRING,
19766 include_name, (char *) NULL));
19767 include_name = name_holder->get ();
19768 include_name_to_compare = include_name;
19769 }
19770 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19771 {
19772 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19773 include_name, (char *) NULL));
19774 include_name_to_compare = hold_compare.get ();
19775 }
19776 }
19777
19778 pst_filename = pst->filename;
19779 gdb::unique_xmalloc_ptr<char> copied_name;
19780 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19781 {
19782 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19783 pst_filename, (char *) NULL));
19784 pst_filename = copied_name.get ();
19785 }
19786
19787 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19788
19789 if (file_is_pst)
19790 return NULL;
19791 return include_name;
19792 }
19793
19794 /* State machine to track the state of the line number program. */
19795
19796 class lnp_state_machine
19797 {
19798 public:
19799 /* Initialize a machine state for the start of a line number
19800 program. */
19801 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19802 bool record_lines_p);
19803
19804 file_entry *current_file ()
19805 {
19806 /* lh->file_names is 0-based, but the file name numbers in the
19807 statement program are 1-based. */
19808 return m_line_header->file_name_at (m_file);
19809 }
19810
19811 /* Record the line in the state machine. END_SEQUENCE is true if
19812 we're processing the end of a sequence. */
19813 void record_line (bool end_sequence);
19814
19815 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19816 nop-out rest of the lines in this sequence. */
19817 void check_line_address (struct dwarf2_cu *cu,
19818 const gdb_byte *line_ptr,
19819 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19820
19821 void handle_set_discriminator (unsigned int discriminator)
19822 {
19823 m_discriminator = discriminator;
19824 m_line_has_non_zero_discriminator |= discriminator != 0;
19825 }
19826
19827 /* Handle DW_LNE_set_address. */
19828 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19829 {
19830 m_op_index = 0;
19831 address += baseaddr;
19832 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19833 }
19834
19835 /* Handle DW_LNS_advance_pc. */
19836 void handle_advance_pc (CORE_ADDR adjust);
19837
19838 /* Handle a special opcode. */
19839 void handle_special_opcode (unsigned char op_code);
19840
19841 /* Handle DW_LNS_advance_line. */
19842 void handle_advance_line (int line_delta)
19843 {
19844 advance_line (line_delta);
19845 }
19846
19847 /* Handle DW_LNS_set_file. */
19848 void handle_set_file (file_name_index file);
19849
19850 /* Handle DW_LNS_negate_stmt. */
19851 void handle_negate_stmt ()
19852 {
19853 m_is_stmt = !m_is_stmt;
19854 }
19855
19856 /* Handle DW_LNS_const_add_pc. */
19857 void handle_const_add_pc ();
19858
19859 /* Handle DW_LNS_fixed_advance_pc. */
19860 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19861 {
19862 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19863 m_op_index = 0;
19864 }
19865
19866 /* Handle DW_LNS_copy. */
19867 void handle_copy ()
19868 {
19869 record_line (false);
19870 m_discriminator = 0;
19871 }
19872
19873 /* Handle DW_LNE_end_sequence. */
19874 void handle_end_sequence ()
19875 {
19876 m_currently_recording_lines = true;
19877 }
19878
19879 private:
19880 /* Advance the line by LINE_DELTA. */
19881 void advance_line (int line_delta)
19882 {
19883 m_line += line_delta;
19884
19885 if (line_delta != 0)
19886 m_line_has_non_zero_discriminator = m_discriminator != 0;
19887 }
19888
19889 struct dwarf2_cu *m_cu;
19890
19891 gdbarch *m_gdbarch;
19892
19893 /* True if we're recording lines.
19894 Otherwise we're building partial symtabs and are just interested in
19895 finding include files mentioned by the line number program. */
19896 bool m_record_lines_p;
19897
19898 /* The line number header. */
19899 line_header *m_line_header;
19900
19901 /* These are part of the standard DWARF line number state machine,
19902 and initialized according to the DWARF spec. */
19903
19904 unsigned char m_op_index = 0;
19905 /* The line table index of the current file. */
19906 file_name_index m_file = 1;
19907 unsigned int m_line = 1;
19908
19909 /* These are initialized in the constructor. */
19910
19911 CORE_ADDR m_address;
19912 bool m_is_stmt;
19913 unsigned int m_discriminator;
19914
19915 /* Additional bits of state we need to track. */
19916
19917 /* The last file that we called dwarf2_start_subfile for.
19918 This is only used for TLLs. */
19919 unsigned int m_last_file = 0;
19920 /* The last file a line number was recorded for. */
19921 struct subfile *m_last_subfile = NULL;
19922
19923 /* When true, record the lines we decode. */
19924 bool m_currently_recording_lines = false;
19925
19926 /* The last line number that was recorded, used to coalesce
19927 consecutive entries for the same line. This can happen, for
19928 example, when discriminators are present. PR 17276. */
19929 unsigned int m_last_line = 0;
19930 bool m_line_has_non_zero_discriminator = false;
19931 };
19932
19933 void
19934 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19935 {
19936 CORE_ADDR addr_adj = (((m_op_index + adjust)
19937 / m_line_header->maximum_ops_per_instruction)
19938 * m_line_header->minimum_instruction_length);
19939 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19940 m_op_index = ((m_op_index + adjust)
19941 % m_line_header->maximum_ops_per_instruction);
19942 }
19943
19944 void
19945 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19946 {
19947 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19948 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19949 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19950 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19951 / m_line_header->maximum_ops_per_instruction)
19952 * m_line_header->minimum_instruction_length);
19953 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19954 m_op_index = ((m_op_index + adj_opcode_d)
19955 % m_line_header->maximum_ops_per_instruction);
19956
19957 int line_delta = m_line_header->line_base + adj_opcode_r;
19958 advance_line (line_delta);
19959 record_line (false);
19960 m_discriminator = 0;
19961 }
19962
19963 void
19964 lnp_state_machine::handle_set_file (file_name_index file)
19965 {
19966 m_file = file;
19967
19968 const file_entry *fe = current_file ();
19969 if (fe == NULL)
19970 dwarf2_debug_line_missing_file_complaint ();
19971 else if (m_record_lines_p)
19972 {
19973 const char *dir = fe->include_dir (m_line_header);
19974
19975 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19976 m_line_has_non_zero_discriminator = m_discriminator != 0;
19977 dwarf2_start_subfile (m_cu, fe->name, dir);
19978 }
19979 }
19980
19981 void
19982 lnp_state_machine::handle_const_add_pc ()
19983 {
19984 CORE_ADDR adjust
19985 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19986
19987 CORE_ADDR addr_adj
19988 = (((m_op_index + adjust)
19989 / m_line_header->maximum_ops_per_instruction)
19990 * m_line_header->minimum_instruction_length);
19991
19992 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19993 m_op_index = ((m_op_index + adjust)
19994 % m_line_header->maximum_ops_per_instruction);
19995 }
19996
19997 /* Return non-zero if we should add LINE to the line number table.
19998 LINE is the line to add, LAST_LINE is the last line that was added,
19999 LAST_SUBFILE is the subfile for LAST_LINE.
20000 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20001 had a non-zero discriminator.
20002
20003 We have to be careful in the presence of discriminators.
20004 E.g., for this line:
20005
20006 for (i = 0; i < 100000; i++);
20007
20008 clang can emit four line number entries for that one line,
20009 each with a different discriminator.
20010 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20011
20012 However, we want gdb to coalesce all four entries into one.
20013 Otherwise the user could stepi into the middle of the line and
20014 gdb would get confused about whether the pc really was in the
20015 middle of the line.
20016
20017 Things are further complicated by the fact that two consecutive
20018 line number entries for the same line is a heuristic used by gcc
20019 to denote the end of the prologue. So we can't just discard duplicate
20020 entries, we have to be selective about it. The heuristic we use is
20021 that we only collapse consecutive entries for the same line if at least
20022 one of those entries has a non-zero discriminator. PR 17276.
20023
20024 Note: Addresses in the line number state machine can never go backwards
20025 within one sequence, thus this coalescing is ok. */
20026
20027 static int
20028 dwarf_record_line_p (struct dwarf2_cu *cu,
20029 unsigned int line, unsigned int last_line,
20030 int line_has_non_zero_discriminator,
20031 struct subfile *last_subfile)
20032 {
20033 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20034 return 1;
20035 if (line != last_line)
20036 return 1;
20037 /* Same line for the same file that we've seen already.
20038 As a last check, for pr 17276, only record the line if the line
20039 has never had a non-zero discriminator. */
20040 if (!line_has_non_zero_discriminator)
20041 return 1;
20042 return 0;
20043 }
20044
20045 /* Use the CU's builder to record line number LINE beginning at
20046 address ADDRESS in the line table of subfile SUBFILE. */
20047
20048 static void
20049 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20050 unsigned int line, CORE_ADDR address, bool is_stmt,
20051 struct dwarf2_cu *cu)
20052 {
20053 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20054
20055 if (dwarf_line_debug)
20056 {
20057 fprintf_unfiltered (gdb_stdlog,
20058 "Recording line %u, file %s, address %s\n",
20059 line, lbasename (subfile->name),
20060 paddress (gdbarch, address));
20061 }
20062
20063 if (cu != nullptr)
20064 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20065 }
20066
20067 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20068 Mark the end of a set of line number records.
20069 The arguments are the same as for dwarf_record_line_1.
20070 If SUBFILE is NULL the request is ignored. */
20071
20072 static void
20073 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20074 CORE_ADDR address, struct dwarf2_cu *cu)
20075 {
20076 if (subfile == NULL)
20077 return;
20078
20079 if (dwarf_line_debug)
20080 {
20081 fprintf_unfiltered (gdb_stdlog,
20082 "Finishing current line, file %s, address %s\n",
20083 lbasename (subfile->name),
20084 paddress (gdbarch, address));
20085 }
20086
20087 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20088 }
20089
20090 void
20091 lnp_state_machine::record_line (bool end_sequence)
20092 {
20093 if (dwarf_line_debug)
20094 {
20095 fprintf_unfiltered (gdb_stdlog,
20096 "Processing actual line %u: file %u,"
20097 " address %s, is_stmt %u, discrim %u%s\n",
20098 m_line, m_file,
20099 paddress (m_gdbarch, m_address),
20100 m_is_stmt, m_discriminator,
20101 (end_sequence ? "\t(end sequence)" : ""));
20102 }
20103
20104 file_entry *fe = current_file ();
20105
20106 if (fe == NULL)
20107 dwarf2_debug_line_missing_file_complaint ();
20108 /* For now we ignore lines not starting on an instruction boundary.
20109 But not when processing end_sequence for compatibility with the
20110 previous version of the code. */
20111 else if (m_op_index == 0 || end_sequence)
20112 {
20113 fe->included_p = 1;
20114 if (m_record_lines_p)
20115 {
20116 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20117 || end_sequence)
20118 {
20119 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20120 m_currently_recording_lines ? m_cu : nullptr);
20121 }
20122
20123 if (!end_sequence)
20124 {
20125 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20126
20127 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20128 m_line_has_non_zero_discriminator,
20129 m_last_subfile))
20130 {
20131 buildsym_compunit *builder = m_cu->get_builder ();
20132 dwarf_record_line_1 (m_gdbarch,
20133 builder->get_current_subfile (),
20134 m_line, m_address, is_stmt,
20135 m_currently_recording_lines ? m_cu : nullptr);
20136 }
20137 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20138 m_last_line = m_line;
20139 }
20140 }
20141 }
20142 }
20143
20144 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20145 line_header *lh, bool record_lines_p)
20146 {
20147 m_cu = cu;
20148 m_gdbarch = arch;
20149 m_record_lines_p = record_lines_p;
20150 m_line_header = lh;
20151
20152 m_currently_recording_lines = true;
20153
20154 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20155 was a line entry for it so that the backend has a chance to adjust it
20156 and also record it in case it needs it. This is currently used by MIPS
20157 code, cf. `mips_adjust_dwarf2_line'. */
20158 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20159 m_is_stmt = lh->default_is_stmt;
20160 m_discriminator = 0;
20161 }
20162
20163 void
20164 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20165 const gdb_byte *line_ptr,
20166 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20167 {
20168 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20169 the pc range of the CU. However, we restrict the test to only ADDRESS
20170 values of zero to preserve GDB's previous behaviour which is to handle
20171 the specific case of a function being GC'd by the linker. */
20172
20173 if (address == 0 && address < unrelocated_lowpc)
20174 {
20175 /* This line table is for a function which has been
20176 GCd by the linker. Ignore it. PR gdb/12528 */
20177
20178 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20179 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20180
20181 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20182 line_offset, objfile_name (objfile));
20183 m_currently_recording_lines = false;
20184 /* Note: m_currently_recording_lines is left as false until we see
20185 DW_LNE_end_sequence. */
20186 }
20187 }
20188
20189 /* Subroutine of dwarf_decode_lines to simplify it.
20190 Process the line number information in LH.
20191 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20192 program in order to set included_p for every referenced header. */
20193
20194 static void
20195 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20196 const int decode_for_pst_p, CORE_ADDR lowpc)
20197 {
20198 const gdb_byte *line_ptr, *extended_end;
20199 const gdb_byte *line_end;
20200 unsigned int bytes_read, extended_len;
20201 unsigned char op_code, extended_op;
20202 CORE_ADDR baseaddr;
20203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20204 bfd *abfd = objfile->obfd;
20205 struct gdbarch *gdbarch = objfile->arch ();
20206 /* True if we're recording line info (as opposed to building partial
20207 symtabs and just interested in finding include files mentioned by
20208 the line number program). */
20209 bool record_lines_p = !decode_for_pst_p;
20210
20211 baseaddr = objfile->text_section_offset ();
20212
20213 line_ptr = lh->statement_program_start;
20214 line_end = lh->statement_program_end;
20215
20216 /* Read the statement sequences until there's nothing left. */
20217 while (line_ptr < line_end)
20218 {
20219 /* The DWARF line number program state machine. Reset the state
20220 machine at the start of each sequence. */
20221 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20222 bool end_sequence = false;
20223
20224 if (record_lines_p)
20225 {
20226 /* Start a subfile for the current file of the state
20227 machine. */
20228 const file_entry *fe = state_machine.current_file ();
20229
20230 if (fe != NULL)
20231 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20232 }
20233
20234 /* Decode the table. */
20235 while (line_ptr < line_end && !end_sequence)
20236 {
20237 op_code = read_1_byte (abfd, line_ptr);
20238 line_ptr += 1;
20239
20240 if (op_code >= lh->opcode_base)
20241 {
20242 /* Special opcode. */
20243 state_machine.handle_special_opcode (op_code);
20244 }
20245 else switch (op_code)
20246 {
20247 case DW_LNS_extended_op:
20248 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20249 &bytes_read);
20250 line_ptr += bytes_read;
20251 extended_end = line_ptr + extended_len;
20252 extended_op = read_1_byte (abfd, line_ptr);
20253 line_ptr += 1;
20254 switch (extended_op)
20255 {
20256 case DW_LNE_end_sequence:
20257 state_machine.handle_end_sequence ();
20258 end_sequence = true;
20259 break;
20260 case DW_LNE_set_address:
20261 {
20262 CORE_ADDR address
20263 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20264 line_ptr += bytes_read;
20265
20266 state_machine.check_line_address (cu, line_ptr,
20267 lowpc - baseaddr, address);
20268 state_machine.handle_set_address (baseaddr, address);
20269 }
20270 break;
20271 case DW_LNE_define_file:
20272 {
20273 const char *cur_file;
20274 unsigned int mod_time, length;
20275 dir_index dindex;
20276
20277 cur_file = read_direct_string (abfd, line_ptr,
20278 &bytes_read);
20279 line_ptr += bytes_read;
20280 dindex = (dir_index)
20281 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20282 line_ptr += bytes_read;
20283 mod_time =
20284 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20285 line_ptr += bytes_read;
20286 length =
20287 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20288 line_ptr += bytes_read;
20289 lh->add_file_name (cur_file, dindex, mod_time, length);
20290 }
20291 break;
20292 case DW_LNE_set_discriminator:
20293 {
20294 /* The discriminator is not interesting to the
20295 debugger; just ignore it. We still need to
20296 check its value though:
20297 if there are consecutive entries for the same
20298 (non-prologue) line we want to coalesce them.
20299 PR 17276. */
20300 unsigned int discr
20301 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20302 line_ptr += bytes_read;
20303
20304 state_machine.handle_set_discriminator (discr);
20305 }
20306 break;
20307 default:
20308 complaint (_("mangled .debug_line section"));
20309 return;
20310 }
20311 /* Make sure that we parsed the extended op correctly. If e.g.
20312 we expected a different address size than the producer used,
20313 we may have read the wrong number of bytes. */
20314 if (line_ptr != extended_end)
20315 {
20316 complaint (_("mangled .debug_line section"));
20317 return;
20318 }
20319 break;
20320 case DW_LNS_copy:
20321 state_machine.handle_copy ();
20322 break;
20323 case DW_LNS_advance_pc:
20324 {
20325 CORE_ADDR adjust
20326 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20327 line_ptr += bytes_read;
20328
20329 state_machine.handle_advance_pc (adjust);
20330 }
20331 break;
20332 case DW_LNS_advance_line:
20333 {
20334 int line_delta
20335 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20336 line_ptr += bytes_read;
20337
20338 state_machine.handle_advance_line (line_delta);
20339 }
20340 break;
20341 case DW_LNS_set_file:
20342 {
20343 file_name_index file
20344 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20345 &bytes_read);
20346 line_ptr += bytes_read;
20347
20348 state_machine.handle_set_file (file);
20349 }
20350 break;
20351 case DW_LNS_set_column:
20352 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20353 line_ptr += bytes_read;
20354 break;
20355 case DW_LNS_negate_stmt:
20356 state_machine.handle_negate_stmt ();
20357 break;
20358 case DW_LNS_set_basic_block:
20359 break;
20360 /* Add to the address register of the state machine the
20361 address increment value corresponding to special opcode
20362 255. I.e., this value is scaled by the minimum
20363 instruction length since special opcode 255 would have
20364 scaled the increment. */
20365 case DW_LNS_const_add_pc:
20366 state_machine.handle_const_add_pc ();
20367 break;
20368 case DW_LNS_fixed_advance_pc:
20369 {
20370 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20371 line_ptr += 2;
20372
20373 state_machine.handle_fixed_advance_pc (addr_adj);
20374 }
20375 break;
20376 default:
20377 {
20378 /* Unknown standard opcode, ignore it. */
20379 int i;
20380
20381 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20382 {
20383 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20384 line_ptr += bytes_read;
20385 }
20386 }
20387 }
20388 }
20389
20390 if (!end_sequence)
20391 dwarf2_debug_line_missing_end_sequence_complaint ();
20392
20393 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20394 in which case we still finish recording the last line). */
20395 state_machine.record_line (true);
20396 }
20397 }
20398
20399 /* Decode the Line Number Program (LNP) for the given line_header
20400 structure and CU. The actual information extracted and the type
20401 of structures created from the LNP depends on the value of PST.
20402
20403 1. If PST is NULL, then this procedure uses the data from the program
20404 to create all necessary symbol tables, and their linetables.
20405
20406 2. If PST is not NULL, this procedure reads the program to determine
20407 the list of files included by the unit represented by PST, and
20408 builds all the associated partial symbol tables.
20409
20410 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20411 It is used for relative paths in the line table.
20412 NOTE: When processing partial symtabs (pst != NULL),
20413 comp_dir == pst->dirname.
20414
20415 NOTE: It is important that psymtabs have the same file name (via strcmp)
20416 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20417 symtab we don't use it in the name of the psymtabs we create.
20418 E.g. expand_line_sal requires this when finding psymtabs to expand.
20419 A good testcase for this is mb-inline.exp.
20420
20421 LOWPC is the lowest address in CU (or 0 if not known).
20422
20423 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20424 for its PC<->lines mapping information. Otherwise only the filename
20425 table is read in. */
20426
20427 static void
20428 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20429 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20430 CORE_ADDR lowpc, int decode_mapping)
20431 {
20432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20433 const int decode_for_pst_p = (pst != NULL);
20434
20435 if (decode_mapping)
20436 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20437
20438 if (decode_for_pst_p)
20439 {
20440 /* Now that we're done scanning the Line Header Program, we can
20441 create the psymtab of each included file. */
20442 for (auto &file_entry : lh->file_names ())
20443 if (file_entry.included_p == 1)
20444 {
20445 gdb::unique_xmalloc_ptr<char> name_holder;
20446 const char *include_name =
20447 psymtab_include_file_name (lh, file_entry, pst,
20448 comp_dir, &name_holder);
20449 if (include_name != NULL)
20450 dwarf2_create_include_psymtab (include_name, pst, objfile);
20451 }
20452 }
20453 else
20454 {
20455 /* Make sure a symtab is created for every file, even files
20456 which contain only variables (i.e. no code with associated
20457 line numbers). */
20458 buildsym_compunit *builder = cu->get_builder ();
20459 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20460
20461 for (auto &fe : lh->file_names ())
20462 {
20463 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20464 if (builder->get_current_subfile ()->symtab == NULL)
20465 {
20466 builder->get_current_subfile ()->symtab
20467 = allocate_symtab (cust,
20468 builder->get_current_subfile ()->name);
20469 }
20470 fe.symtab = builder->get_current_subfile ()->symtab;
20471 }
20472 }
20473 }
20474
20475 /* Start a subfile for DWARF. FILENAME is the name of the file and
20476 DIRNAME the name of the source directory which contains FILENAME
20477 or NULL if not known.
20478 This routine tries to keep line numbers from identical absolute and
20479 relative file names in a common subfile.
20480
20481 Using the `list' example from the GDB testsuite, which resides in
20482 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20483 of /srcdir/list0.c yields the following debugging information for list0.c:
20484
20485 DW_AT_name: /srcdir/list0.c
20486 DW_AT_comp_dir: /compdir
20487 files.files[0].name: list0.h
20488 files.files[0].dir: /srcdir
20489 files.files[1].name: list0.c
20490 files.files[1].dir: /srcdir
20491
20492 The line number information for list0.c has to end up in a single
20493 subfile, so that `break /srcdir/list0.c:1' works as expected.
20494 start_subfile will ensure that this happens provided that we pass the
20495 concatenation of files.files[1].dir and files.files[1].name as the
20496 subfile's name. */
20497
20498 static void
20499 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20500 const char *dirname)
20501 {
20502 gdb::unique_xmalloc_ptr<char> copy;
20503
20504 /* In order not to lose the line information directory,
20505 we concatenate it to the filename when it makes sense.
20506 Note that the Dwarf3 standard says (speaking of filenames in line
20507 information): ``The directory index is ignored for file names
20508 that represent full path names''. Thus ignoring dirname in the
20509 `else' branch below isn't an issue. */
20510
20511 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20512 {
20513 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20514 filename = copy.get ();
20515 }
20516
20517 cu->get_builder ()->start_subfile (filename);
20518 }
20519
20520 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20521 buildsym_compunit constructor. */
20522
20523 struct compunit_symtab *
20524 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20525 CORE_ADDR low_pc)
20526 {
20527 gdb_assert (m_builder == nullptr);
20528
20529 m_builder.reset (new struct buildsym_compunit
20530 (per_cu->dwarf2_per_objfile->objfile,
20531 name, comp_dir, language, low_pc));
20532
20533 list_in_scope = get_builder ()->get_file_symbols ();
20534
20535 get_builder ()->record_debugformat ("DWARF 2");
20536 get_builder ()->record_producer (producer);
20537
20538 processing_has_namespace_info = false;
20539
20540 return get_builder ()->get_compunit_symtab ();
20541 }
20542
20543 static void
20544 var_decode_location (struct attribute *attr, struct symbol *sym,
20545 struct dwarf2_cu *cu)
20546 {
20547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20548 struct comp_unit_head *cu_header = &cu->header;
20549
20550 /* NOTE drow/2003-01-30: There used to be a comment and some special
20551 code here to turn a symbol with DW_AT_external and a
20552 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20553 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20554 with some versions of binutils) where shared libraries could have
20555 relocations against symbols in their debug information - the
20556 minimal symbol would have the right address, but the debug info
20557 would not. It's no longer necessary, because we will explicitly
20558 apply relocations when we read in the debug information now. */
20559
20560 /* A DW_AT_location attribute with no contents indicates that a
20561 variable has been optimized away. */
20562 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20563 {
20564 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20565 return;
20566 }
20567
20568 /* Handle one degenerate form of location expression specially, to
20569 preserve GDB's previous behavior when section offsets are
20570 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20571 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20572
20573 if (attr->form_is_block ()
20574 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20575 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20576 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20577 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20578 && (DW_BLOCK (attr)->size
20579 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20580 {
20581 unsigned int dummy;
20582
20583 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20584 SET_SYMBOL_VALUE_ADDRESS
20585 (sym, cu->header.read_address (objfile->obfd,
20586 DW_BLOCK (attr)->data + 1,
20587 &dummy));
20588 else
20589 SET_SYMBOL_VALUE_ADDRESS
20590 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20591 &dummy));
20592 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20593 fixup_symbol_section (sym, objfile);
20594 SET_SYMBOL_VALUE_ADDRESS
20595 (sym,
20596 SYMBOL_VALUE_ADDRESS (sym)
20597 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20598 return;
20599 }
20600
20601 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20602 expression evaluator, and use LOC_COMPUTED only when necessary
20603 (i.e. when the value of a register or memory location is
20604 referenced, or a thread-local block, etc.). Then again, it might
20605 not be worthwhile. I'm assuming that it isn't unless performance
20606 or memory numbers show me otherwise. */
20607
20608 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20609
20610 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20611 cu->has_loclist = true;
20612 }
20613
20614 /* Given a pointer to a DWARF information entry, figure out if we need
20615 to make a symbol table entry for it, and if so, create a new entry
20616 and return a pointer to it.
20617 If TYPE is NULL, determine symbol type from the die, otherwise
20618 used the passed type.
20619 If SPACE is not NULL, use it to hold the new symbol. If it is
20620 NULL, allocate a new symbol on the objfile's obstack. */
20621
20622 static struct symbol *
20623 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20624 struct symbol *space)
20625 {
20626 struct dwarf2_per_objfile *dwarf2_per_objfile
20627 = cu->per_cu->dwarf2_per_objfile;
20628 struct objfile *objfile = dwarf2_per_objfile->objfile;
20629 struct gdbarch *gdbarch = objfile->arch ();
20630 struct symbol *sym = NULL;
20631 const char *name;
20632 struct attribute *attr = NULL;
20633 struct attribute *attr2 = NULL;
20634 CORE_ADDR baseaddr;
20635 struct pending **list_to_add = NULL;
20636
20637 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20638
20639 baseaddr = objfile->text_section_offset ();
20640
20641 name = dwarf2_name (die, cu);
20642 if (name)
20643 {
20644 int suppress_add = 0;
20645
20646 if (space)
20647 sym = space;
20648 else
20649 sym = new (&objfile->objfile_obstack) symbol;
20650 OBJSTAT (objfile, n_syms++);
20651
20652 /* Cache this symbol's name and the name's demangled form (if any). */
20653 sym->set_language (cu->language, &objfile->objfile_obstack);
20654 /* Fortran does not have mangling standard and the mangling does differ
20655 between gfortran, iFort etc. */
20656 const char *physname
20657 = (cu->language == language_fortran
20658 ? dwarf2_full_name (name, die, cu)
20659 : dwarf2_physname (name, die, cu));
20660 const char *linkagename = dw2_linkage_name (die, cu);
20661
20662 if (linkagename == nullptr || cu->language == language_ada)
20663 sym->set_linkage_name (physname);
20664 else
20665 {
20666 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20667 sym->set_linkage_name (linkagename);
20668 }
20669
20670 /* Default assumptions.
20671 Use the passed type or decode it from the die. */
20672 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20673 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20674 if (type != NULL)
20675 SYMBOL_TYPE (sym) = type;
20676 else
20677 SYMBOL_TYPE (sym) = die_type (die, cu);
20678 attr = dwarf2_attr (die,
20679 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20680 cu);
20681 if (attr != nullptr)
20682 {
20683 SYMBOL_LINE (sym) = DW_UNSND (attr);
20684 }
20685
20686 attr = dwarf2_attr (die,
20687 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20688 cu);
20689 if (attr != nullptr)
20690 {
20691 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20692 struct file_entry *fe;
20693
20694 if (cu->line_header != NULL)
20695 fe = cu->line_header->file_name_at (file_index);
20696 else
20697 fe = NULL;
20698
20699 if (fe == NULL)
20700 complaint (_("file index out of range"));
20701 else
20702 symbol_set_symtab (sym, fe->symtab);
20703 }
20704
20705 switch (die->tag)
20706 {
20707 case DW_TAG_label:
20708 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20709 if (attr != nullptr)
20710 {
20711 CORE_ADDR addr;
20712
20713 addr = attr->value_as_address ();
20714 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20715 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20716 }
20717 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20718 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20719 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20720 add_symbol_to_list (sym, cu->list_in_scope);
20721 break;
20722 case DW_TAG_subprogram:
20723 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20724 finish_block. */
20725 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20726 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20727 if ((attr2 && (DW_UNSND (attr2) != 0))
20728 || cu->language == language_ada
20729 || cu->language == language_fortran)
20730 {
20731 /* Subprograms marked external are stored as a global symbol.
20732 Ada and Fortran subprograms, whether marked external or
20733 not, are always stored as a global symbol, because we want
20734 to be able to access them globally. For instance, we want
20735 to be able to break on a nested subprogram without having
20736 to specify the context. */
20737 list_to_add = cu->get_builder ()->get_global_symbols ();
20738 }
20739 else
20740 {
20741 list_to_add = cu->list_in_scope;
20742 }
20743 break;
20744 case DW_TAG_inlined_subroutine:
20745 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20746 finish_block. */
20747 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20748 SYMBOL_INLINED (sym) = 1;
20749 list_to_add = cu->list_in_scope;
20750 break;
20751 case DW_TAG_template_value_param:
20752 suppress_add = 1;
20753 /* Fall through. */
20754 case DW_TAG_constant:
20755 case DW_TAG_variable:
20756 case DW_TAG_member:
20757 /* Compilation with minimal debug info may result in
20758 variables with missing type entries. Change the
20759 misleading `void' type to something sensible. */
20760 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20761 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20762
20763 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20764 /* In the case of DW_TAG_member, we should only be called for
20765 static const members. */
20766 if (die->tag == DW_TAG_member)
20767 {
20768 /* dwarf2_add_field uses die_is_declaration,
20769 so we do the same. */
20770 gdb_assert (die_is_declaration (die, cu));
20771 gdb_assert (attr);
20772 }
20773 if (attr != nullptr)
20774 {
20775 dwarf2_const_value (attr, sym, cu);
20776 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20777 if (!suppress_add)
20778 {
20779 if (attr2 && (DW_UNSND (attr2) != 0))
20780 list_to_add = cu->get_builder ()->get_global_symbols ();
20781 else
20782 list_to_add = cu->list_in_scope;
20783 }
20784 break;
20785 }
20786 attr = dwarf2_attr (die, DW_AT_location, cu);
20787 if (attr != nullptr)
20788 {
20789 var_decode_location (attr, sym, cu);
20790 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20791
20792 /* Fortran explicitly imports any global symbols to the local
20793 scope by DW_TAG_common_block. */
20794 if (cu->language == language_fortran && die->parent
20795 && die->parent->tag == DW_TAG_common_block)
20796 attr2 = NULL;
20797
20798 if (SYMBOL_CLASS (sym) == LOC_STATIC
20799 && SYMBOL_VALUE_ADDRESS (sym) == 0
20800 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20801 {
20802 /* When a static variable is eliminated by the linker,
20803 the corresponding debug information is not stripped
20804 out, but the variable address is set to null;
20805 do not add such variables into symbol table. */
20806 }
20807 else if (attr2 && (DW_UNSND (attr2) != 0))
20808 {
20809 if (SYMBOL_CLASS (sym) == LOC_STATIC
20810 && (objfile->flags & OBJF_MAINLINE) == 0
20811 && dwarf2_per_objfile->per_bfd->can_copy)
20812 {
20813 /* A global static variable might be subject to
20814 copy relocation. We first check for a local
20815 minsym, though, because maybe the symbol was
20816 marked hidden, in which case this would not
20817 apply. */
20818 bound_minimal_symbol found
20819 = (lookup_minimal_symbol_linkage
20820 (sym->linkage_name (), objfile));
20821 if (found.minsym != nullptr)
20822 sym->maybe_copied = 1;
20823 }
20824
20825 /* A variable with DW_AT_external is never static,
20826 but it may be block-scoped. */
20827 list_to_add
20828 = ((cu->list_in_scope
20829 == cu->get_builder ()->get_file_symbols ())
20830 ? cu->get_builder ()->get_global_symbols ()
20831 : cu->list_in_scope);
20832 }
20833 else
20834 list_to_add = cu->list_in_scope;
20835 }
20836 else
20837 {
20838 /* We do not know the address of this symbol.
20839 If it is an external symbol and we have type information
20840 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20841 The address of the variable will then be determined from
20842 the minimal symbol table whenever the variable is
20843 referenced. */
20844 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20845
20846 /* Fortran explicitly imports any global symbols to the local
20847 scope by DW_TAG_common_block. */
20848 if (cu->language == language_fortran && die->parent
20849 && die->parent->tag == DW_TAG_common_block)
20850 {
20851 /* SYMBOL_CLASS doesn't matter here because
20852 read_common_block is going to reset it. */
20853 if (!suppress_add)
20854 list_to_add = cu->list_in_scope;
20855 }
20856 else if (attr2 && (DW_UNSND (attr2) != 0)
20857 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20858 {
20859 /* A variable with DW_AT_external is never static, but it
20860 may be block-scoped. */
20861 list_to_add
20862 = ((cu->list_in_scope
20863 == cu->get_builder ()->get_file_symbols ())
20864 ? cu->get_builder ()->get_global_symbols ()
20865 : cu->list_in_scope);
20866
20867 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20868 }
20869 else if (!die_is_declaration (die, cu))
20870 {
20871 /* Use the default LOC_OPTIMIZED_OUT class. */
20872 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20873 if (!suppress_add)
20874 list_to_add = cu->list_in_scope;
20875 }
20876 }
20877 break;
20878 case DW_TAG_formal_parameter:
20879 {
20880 /* If we are inside a function, mark this as an argument. If
20881 not, we might be looking at an argument to an inlined function
20882 when we do not have enough information to show inlined frames;
20883 pretend it's a local variable in that case so that the user can
20884 still see it. */
20885 struct context_stack *curr
20886 = cu->get_builder ()->get_current_context_stack ();
20887 if (curr != nullptr && curr->name != nullptr)
20888 SYMBOL_IS_ARGUMENT (sym) = 1;
20889 attr = dwarf2_attr (die, DW_AT_location, cu);
20890 if (attr != nullptr)
20891 {
20892 var_decode_location (attr, sym, cu);
20893 }
20894 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20895 if (attr != nullptr)
20896 {
20897 dwarf2_const_value (attr, sym, cu);
20898 }
20899
20900 list_to_add = cu->list_in_scope;
20901 }
20902 break;
20903 case DW_TAG_unspecified_parameters:
20904 /* From varargs functions; gdb doesn't seem to have any
20905 interest in this information, so just ignore it for now.
20906 (FIXME?) */
20907 break;
20908 case DW_TAG_template_type_param:
20909 suppress_add = 1;
20910 /* Fall through. */
20911 case DW_TAG_class_type:
20912 case DW_TAG_interface_type:
20913 case DW_TAG_structure_type:
20914 case DW_TAG_union_type:
20915 case DW_TAG_set_type:
20916 case DW_TAG_enumeration_type:
20917 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20918 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20919
20920 {
20921 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20922 really ever be static objects: otherwise, if you try
20923 to, say, break of a class's method and you're in a file
20924 which doesn't mention that class, it won't work unless
20925 the check for all static symbols in lookup_symbol_aux
20926 saves you. See the OtherFileClass tests in
20927 gdb.c++/namespace.exp. */
20928
20929 if (!suppress_add)
20930 {
20931 buildsym_compunit *builder = cu->get_builder ();
20932 list_to_add
20933 = (cu->list_in_scope == builder->get_file_symbols ()
20934 && cu->language == language_cplus
20935 ? builder->get_global_symbols ()
20936 : cu->list_in_scope);
20937
20938 /* The semantics of C++ state that "struct foo {
20939 ... }" also defines a typedef for "foo". */
20940 if (cu->language == language_cplus
20941 || cu->language == language_ada
20942 || cu->language == language_d
20943 || cu->language == language_rust)
20944 {
20945 /* The symbol's name is already allocated along
20946 with this objfile, so we don't need to
20947 duplicate it for the type. */
20948 if (SYMBOL_TYPE (sym)->name () == 0)
20949 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
20950 }
20951 }
20952 }
20953 break;
20954 case DW_TAG_typedef:
20955 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20956 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20957 list_to_add = cu->list_in_scope;
20958 break;
20959 case DW_TAG_base_type:
20960 case DW_TAG_subrange_type:
20961 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20962 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20963 list_to_add = cu->list_in_scope;
20964 break;
20965 case DW_TAG_enumerator:
20966 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20967 if (attr != nullptr)
20968 {
20969 dwarf2_const_value (attr, sym, cu);
20970 }
20971 {
20972 /* NOTE: carlton/2003-11-10: See comment above in the
20973 DW_TAG_class_type, etc. block. */
20974
20975 list_to_add
20976 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20977 && cu->language == language_cplus
20978 ? cu->get_builder ()->get_global_symbols ()
20979 : cu->list_in_scope);
20980 }
20981 break;
20982 case DW_TAG_imported_declaration:
20983 case DW_TAG_namespace:
20984 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20985 list_to_add = cu->get_builder ()->get_global_symbols ();
20986 break;
20987 case DW_TAG_module:
20988 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20989 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20990 list_to_add = cu->get_builder ()->get_global_symbols ();
20991 break;
20992 case DW_TAG_common_block:
20993 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20994 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20995 add_symbol_to_list (sym, cu->list_in_scope);
20996 break;
20997 default:
20998 /* Not a tag we recognize. Hopefully we aren't processing
20999 trash data, but since we must specifically ignore things
21000 we don't recognize, there is nothing else we should do at
21001 this point. */
21002 complaint (_("unsupported tag: '%s'"),
21003 dwarf_tag_name (die->tag));
21004 break;
21005 }
21006
21007 if (suppress_add)
21008 {
21009 sym->hash_next = objfile->template_symbols;
21010 objfile->template_symbols = sym;
21011 list_to_add = NULL;
21012 }
21013
21014 if (list_to_add != NULL)
21015 add_symbol_to_list (sym, list_to_add);
21016
21017 /* For the benefit of old versions of GCC, check for anonymous
21018 namespaces based on the demangled name. */
21019 if (!cu->processing_has_namespace_info
21020 && cu->language == language_cplus)
21021 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21022 }
21023 return (sym);
21024 }
21025
21026 /* Given an attr with a DW_FORM_dataN value in host byte order,
21027 zero-extend it as appropriate for the symbol's type. The DWARF
21028 standard (v4) is not entirely clear about the meaning of using
21029 DW_FORM_dataN for a constant with a signed type, where the type is
21030 wider than the data. The conclusion of a discussion on the DWARF
21031 list was that this is unspecified. We choose to always zero-extend
21032 because that is the interpretation long in use by GCC. */
21033
21034 static gdb_byte *
21035 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21036 struct dwarf2_cu *cu, LONGEST *value, int bits)
21037 {
21038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21039 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21040 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21041 LONGEST l = DW_UNSND (attr);
21042
21043 if (bits < sizeof (*value) * 8)
21044 {
21045 l &= ((LONGEST) 1 << bits) - 1;
21046 *value = l;
21047 }
21048 else if (bits == sizeof (*value) * 8)
21049 *value = l;
21050 else
21051 {
21052 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21053 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21054 return bytes;
21055 }
21056
21057 return NULL;
21058 }
21059
21060 /* Read a constant value from an attribute. Either set *VALUE, or if
21061 the value does not fit in *VALUE, set *BYTES - either already
21062 allocated on the objfile obstack, or newly allocated on OBSTACK,
21063 or, set *BATON, if we translated the constant to a location
21064 expression. */
21065
21066 static void
21067 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21068 const char *name, struct obstack *obstack,
21069 struct dwarf2_cu *cu,
21070 LONGEST *value, const gdb_byte **bytes,
21071 struct dwarf2_locexpr_baton **baton)
21072 {
21073 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
21074 struct objfile *objfile = per_objfile->objfile;
21075 struct comp_unit_head *cu_header = &cu->header;
21076 struct dwarf_block *blk;
21077 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21078 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21079
21080 *value = 0;
21081 *bytes = NULL;
21082 *baton = NULL;
21083
21084 switch (attr->form)
21085 {
21086 case DW_FORM_addr:
21087 case DW_FORM_addrx:
21088 case DW_FORM_GNU_addr_index:
21089 {
21090 gdb_byte *data;
21091
21092 if (TYPE_LENGTH (type) != cu_header->addr_size)
21093 dwarf2_const_value_length_mismatch_complaint (name,
21094 cu_header->addr_size,
21095 TYPE_LENGTH (type));
21096 /* Symbols of this form are reasonably rare, so we just
21097 piggyback on the existing location code rather than writing
21098 a new implementation of symbol_computed_ops. */
21099 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21100 (*baton)->per_objfile = per_objfile;
21101 (*baton)->per_cu = cu->per_cu;
21102 gdb_assert ((*baton)->per_cu);
21103
21104 (*baton)->size = 2 + cu_header->addr_size;
21105 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21106 (*baton)->data = data;
21107
21108 data[0] = DW_OP_addr;
21109 store_unsigned_integer (&data[1], cu_header->addr_size,
21110 byte_order, DW_ADDR (attr));
21111 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21112 }
21113 break;
21114 case DW_FORM_string:
21115 case DW_FORM_strp:
21116 case DW_FORM_strx:
21117 case DW_FORM_GNU_str_index:
21118 case DW_FORM_GNU_strp_alt:
21119 /* DW_STRING is already allocated on the objfile obstack, point
21120 directly to it. */
21121 *bytes = (const gdb_byte *) DW_STRING (attr);
21122 break;
21123 case DW_FORM_block1:
21124 case DW_FORM_block2:
21125 case DW_FORM_block4:
21126 case DW_FORM_block:
21127 case DW_FORM_exprloc:
21128 case DW_FORM_data16:
21129 blk = DW_BLOCK (attr);
21130 if (TYPE_LENGTH (type) != blk->size)
21131 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21132 TYPE_LENGTH (type));
21133 *bytes = blk->data;
21134 break;
21135
21136 /* The DW_AT_const_value attributes are supposed to carry the
21137 symbol's value "represented as it would be on the target
21138 architecture." By the time we get here, it's already been
21139 converted to host endianness, so we just need to sign- or
21140 zero-extend it as appropriate. */
21141 case DW_FORM_data1:
21142 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21143 break;
21144 case DW_FORM_data2:
21145 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21146 break;
21147 case DW_FORM_data4:
21148 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21149 break;
21150 case DW_FORM_data8:
21151 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21152 break;
21153
21154 case DW_FORM_sdata:
21155 case DW_FORM_implicit_const:
21156 *value = DW_SND (attr);
21157 break;
21158
21159 case DW_FORM_udata:
21160 *value = DW_UNSND (attr);
21161 break;
21162
21163 default:
21164 complaint (_("unsupported const value attribute form: '%s'"),
21165 dwarf_form_name (attr->form));
21166 *value = 0;
21167 break;
21168 }
21169 }
21170
21171
21172 /* Copy constant value from an attribute to a symbol. */
21173
21174 static void
21175 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21176 struct dwarf2_cu *cu)
21177 {
21178 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21179 LONGEST value;
21180 const gdb_byte *bytes;
21181 struct dwarf2_locexpr_baton *baton;
21182
21183 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21184 sym->print_name (),
21185 &objfile->objfile_obstack, cu,
21186 &value, &bytes, &baton);
21187
21188 if (baton != NULL)
21189 {
21190 SYMBOL_LOCATION_BATON (sym) = baton;
21191 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21192 }
21193 else if (bytes != NULL)
21194 {
21195 SYMBOL_VALUE_BYTES (sym) = bytes;
21196 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21197 }
21198 else
21199 {
21200 SYMBOL_VALUE (sym) = value;
21201 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21202 }
21203 }
21204
21205 /* Return the type of the die in question using its DW_AT_type attribute. */
21206
21207 static struct type *
21208 die_type (struct die_info *die, struct dwarf2_cu *cu)
21209 {
21210 struct attribute *type_attr;
21211
21212 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21213 if (!type_attr)
21214 {
21215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21216 /* A missing DW_AT_type represents a void type. */
21217 return objfile_type (objfile)->builtin_void;
21218 }
21219
21220 return lookup_die_type (die, type_attr, cu);
21221 }
21222
21223 /* True iff CU's producer generates GNAT Ada auxiliary information
21224 that allows to find parallel types through that information instead
21225 of having to do expensive parallel lookups by type name. */
21226
21227 static int
21228 need_gnat_info (struct dwarf2_cu *cu)
21229 {
21230 /* Assume that the Ada compiler was GNAT, which always produces
21231 the auxiliary information. */
21232 return (cu->language == language_ada);
21233 }
21234
21235 /* Return the auxiliary type of the die in question using its
21236 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21237 attribute is not present. */
21238
21239 static struct type *
21240 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21241 {
21242 struct attribute *type_attr;
21243
21244 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21245 if (!type_attr)
21246 return NULL;
21247
21248 return lookup_die_type (die, type_attr, cu);
21249 }
21250
21251 /* If DIE has a descriptive_type attribute, then set the TYPE's
21252 descriptive type accordingly. */
21253
21254 static void
21255 set_descriptive_type (struct type *type, struct die_info *die,
21256 struct dwarf2_cu *cu)
21257 {
21258 struct type *descriptive_type = die_descriptive_type (die, cu);
21259
21260 if (descriptive_type)
21261 {
21262 ALLOCATE_GNAT_AUX_TYPE (type);
21263 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21264 }
21265 }
21266
21267 /* Return the containing type of the die in question using its
21268 DW_AT_containing_type attribute. */
21269
21270 static struct type *
21271 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21272 {
21273 struct attribute *type_attr;
21274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21275
21276 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21277 if (!type_attr)
21278 error (_("Dwarf Error: Problem turning containing type into gdb type "
21279 "[in module %s]"), objfile_name (objfile));
21280
21281 return lookup_die_type (die, type_attr, cu);
21282 }
21283
21284 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21285
21286 static struct type *
21287 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21288 {
21289 struct dwarf2_per_objfile *dwarf2_per_objfile
21290 = cu->per_cu->dwarf2_per_objfile;
21291 struct objfile *objfile = dwarf2_per_objfile->objfile;
21292 char *saved;
21293
21294 std::string message
21295 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21296 objfile_name (objfile),
21297 sect_offset_str (cu->header.sect_off),
21298 sect_offset_str (die->sect_off));
21299 saved = obstack_strdup (&objfile->objfile_obstack, message);
21300
21301 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21302 }
21303
21304 /* Look up the type of DIE in CU using its type attribute ATTR.
21305 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21306 DW_AT_containing_type.
21307 If there is no type substitute an error marker. */
21308
21309 static struct type *
21310 lookup_die_type (struct die_info *die, const struct attribute *attr,
21311 struct dwarf2_cu *cu)
21312 {
21313 struct dwarf2_per_objfile *dwarf2_per_objfile
21314 = cu->per_cu->dwarf2_per_objfile;
21315 struct objfile *objfile = dwarf2_per_objfile->objfile;
21316 struct type *this_type;
21317
21318 gdb_assert (attr->name == DW_AT_type
21319 || attr->name == DW_AT_GNAT_descriptive_type
21320 || attr->name == DW_AT_containing_type);
21321
21322 /* First see if we have it cached. */
21323
21324 if (attr->form == DW_FORM_GNU_ref_alt)
21325 {
21326 struct dwarf2_per_cu_data *per_cu;
21327 sect_offset sect_off = attr->get_ref_die_offset ();
21328
21329 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21330 dwarf2_per_objfile);
21331 this_type = get_die_type_at_offset (sect_off, per_cu);
21332 }
21333 else if (attr->form_is_ref ())
21334 {
21335 sect_offset sect_off = attr->get_ref_die_offset ();
21336
21337 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21338 }
21339 else if (attr->form == DW_FORM_ref_sig8)
21340 {
21341 ULONGEST signature = DW_SIGNATURE (attr);
21342
21343 return get_signatured_type (die, signature, cu);
21344 }
21345 else
21346 {
21347 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21348 " at %s [in module %s]"),
21349 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21350 objfile_name (objfile));
21351 return build_error_marker_type (cu, die);
21352 }
21353
21354 /* If not cached we need to read it in. */
21355
21356 if (this_type == NULL)
21357 {
21358 struct die_info *type_die = NULL;
21359 struct dwarf2_cu *type_cu = cu;
21360
21361 if (attr->form_is_ref ())
21362 type_die = follow_die_ref (die, attr, &type_cu);
21363 if (type_die == NULL)
21364 return build_error_marker_type (cu, die);
21365 /* If we find the type now, it's probably because the type came
21366 from an inter-CU reference and the type's CU got expanded before
21367 ours. */
21368 this_type = read_type_die (type_die, type_cu);
21369 }
21370
21371 /* If we still don't have a type use an error marker. */
21372
21373 if (this_type == NULL)
21374 return build_error_marker_type (cu, die);
21375
21376 return this_type;
21377 }
21378
21379 /* Return the type in DIE, CU.
21380 Returns NULL for invalid types.
21381
21382 This first does a lookup in die_type_hash,
21383 and only reads the die in if necessary.
21384
21385 NOTE: This can be called when reading in partial or full symbols. */
21386
21387 static struct type *
21388 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21389 {
21390 struct type *this_type;
21391
21392 this_type = get_die_type (die, cu);
21393 if (this_type)
21394 return this_type;
21395
21396 return read_type_die_1 (die, cu);
21397 }
21398
21399 /* Read the type in DIE, CU.
21400 Returns NULL for invalid types. */
21401
21402 static struct type *
21403 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21404 {
21405 struct type *this_type = NULL;
21406
21407 switch (die->tag)
21408 {
21409 case DW_TAG_class_type:
21410 case DW_TAG_interface_type:
21411 case DW_TAG_structure_type:
21412 case DW_TAG_union_type:
21413 this_type = read_structure_type (die, cu);
21414 break;
21415 case DW_TAG_enumeration_type:
21416 this_type = read_enumeration_type (die, cu);
21417 break;
21418 case DW_TAG_subprogram:
21419 case DW_TAG_subroutine_type:
21420 case DW_TAG_inlined_subroutine:
21421 this_type = read_subroutine_type (die, cu);
21422 break;
21423 case DW_TAG_array_type:
21424 this_type = read_array_type (die, cu);
21425 break;
21426 case DW_TAG_set_type:
21427 this_type = read_set_type (die, cu);
21428 break;
21429 case DW_TAG_pointer_type:
21430 this_type = read_tag_pointer_type (die, cu);
21431 break;
21432 case DW_TAG_ptr_to_member_type:
21433 this_type = read_tag_ptr_to_member_type (die, cu);
21434 break;
21435 case DW_TAG_reference_type:
21436 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21437 break;
21438 case DW_TAG_rvalue_reference_type:
21439 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21440 break;
21441 case DW_TAG_const_type:
21442 this_type = read_tag_const_type (die, cu);
21443 break;
21444 case DW_TAG_volatile_type:
21445 this_type = read_tag_volatile_type (die, cu);
21446 break;
21447 case DW_TAG_restrict_type:
21448 this_type = read_tag_restrict_type (die, cu);
21449 break;
21450 case DW_TAG_string_type:
21451 this_type = read_tag_string_type (die, cu);
21452 break;
21453 case DW_TAG_typedef:
21454 this_type = read_typedef (die, cu);
21455 break;
21456 case DW_TAG_subrange_type:
21457 this_type = read_subrange_type (die, cu);
21458 break;
21459 case DW_TAG_base_type:
21460 this_type = read_base_type (die, cu);
21461 break;
21462 case DW_TAG_unspecified_type:
21463 this_type = read_unspecified_type (die, cu);
21464 break;
21465 case DW_TAG_namespace:
21466 this_type = read_namespace_type (die, cu);
21467 break;
21468 case DW_TAG_module:
21469 this_type = read_module_type (die, cu);
21470 break;
21471 case DW_TAG_atomic_type:
21472 this_type = read_tag_atomic_type (die, cu);
21473 break;
21474 default:
21475 complaint (_("unexpected tag in read_type_die: '%s'"),
21476 dwarf_tag_name (die->tag));
21477 break;
21478 }
21479
21480 return this_type;
21481 }
21482
21483 /* See if we can figure out if the class lives in a namespace. We do
21484 this by looking for a member function; its demangled name will
21485 contain namespace info, if there is any.
21486 Return the computed name or NULL.
21487 Space for the result is allocated on the objfile's obstack.
21488 This is the full-die version of guess_partial_die_structure_name.
21489 In this case we know DIE has no useful parent. */
21490
21491 static const char *
21492 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21493 {
21494 struct die_info *spec_die;
21495 struct dwarf2_cu *spec_cu;
21496 struct die_info *child;
21497 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21498
21499 spec_cu = cu;
21500 spec_die = die_specification (die, &spec_cu);
21501 if (spec_die != NULL)
21502 {
21503 die = spec_die;
21504 cu = spec_cu;
21505 }
21506
21507 for (child = die->child;
21508 child != NULL;
21509 child = child->sibling)
21510 {
21511 if (child->tag == DW_TAG_subprogram)
21512 {
21513 const char *linkage_name = dw2_linkage_name (child, cu);
21514
21515 if (linkage_name != NULL)
21516 {
21517 gdb::unique_xmalloc_ptr<char> actual_name
21518 (language_class_name_from_physname (cu->language_defn,
21519 linkage_name));
21520 const char *name = NULL;
21521
21522 if (actual_name != NULL)
21523 {
21524 const char *die_name = dwarf2_name (die, cu);
21525
21526 if (die_name != NULL
21527 && strcmp (die_name, actual_name.get ()) != 0)
21528 {
21529 /* Strip off the class name from the full name.
21530 We want the prefix. */
21531 int die_name_len = strlen (die_name);
21532 int actual_name_len = strlen (actual_name.get ());
21533 const char *ptr = actual_name.get ();
21534
21535 /* Test for '::' as a sanity check. */
21536 if (actual_name_len > die_name_len + 2
21537 && ptr[actual_name_len - die_name_len - 1] == ':')
21538 name = obstack_strndup (
21539 &objfile->per_bfd->storage_obstack,
21540 ptr, actual_name_len - die_name_len - 2);
21541 }
21542 }
21543 return name;
21544 }
21545 }
21546 }
21547
21548 return NULL;
21549 }
21550
21551 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21552 prefix part in such case. See
21553 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21554
21555 static const char *
21556 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21557 {
21558 struct attribute *attr;
21559 const char *base;
21560
21561 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21562 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21563 return NULL;
21564
21565 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21566 return NULL;
21567
21568 attr = dw2_linkage_name_attr (die, cu);
21569 if (attr == NULL || DW_STRING (attr) == NULL)
21570 return NULL;
21571
21572 /* dwarf2_name had to be already called. */
21573 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21574
21575 /* Strip the base name, keep any leading namespaces/classes. */
21576 base = strrchr (DW_STRING (attr), ':');
21577 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21578 return "";
21579
21580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21581 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21582 DW_STRING (attr),
21583 &base[-1] - DW_STRING (attr));
21584 }
21585
21586 /* Return the name of the namespace/class that DIE is defined within,
21587 or "" if we can't tell. The caller should not xfree the result.
21588
21589 For example, if we're within the method foo() in the following
21590 code:
21591
21592 namespace N {
21593 class C {
21594 void foo () {
21595 }
21596 };
21597 }
21598
21599 then determine_prefix on foo's die will return "N::C". */
21600
21601 static const char *
21602 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21603 {
21604 struct dwarf2_per_objfile *dwarf2_per_objfile
21605 = cu->per_cu->dwarf2_per_objfile;
21606 struct die_info *parent, *spec_die;
21607 struct dwarf2_cu *spec_cu;
21608 struct type *parent_type;
21609 const char *retval;
21610
21611 if (cu->language != language_cplus
21612 && cu->language != language_fortran && cu->language != language_d
21613 && cu->language != language_rust)
21614 return "";
21615
21616 retval = anonymous_struct_prefix (die, cu);
21617 if (retval)
21618 return retval;
21619
21620 /* We have to be careful in the presence of DW_AT_specification.
21621 For example, with GCC 3.4, given the code
21622
21623 namespace N {
21624 void foo() {
21625 // Definition of N::foo.
21626 }
21627 }
21628
21629 then we'll have a tree of DIEs like this:
21630
21631 1: DW_TAG_compile_unit
21632 2: DW_TAG_namespace // N
21633 3: DW_TAG_subprogram // declaration of N::foo
21634 4: DW_TAG_subprogram // definition of N::foo
21635 DW_AT_specification // refers to die #3
21636
21637 Thus, when processing die #4, we have to pretend that we're in
21638 the context of its DW_AT_specification, namely the contex of die
21639 #3. */
21640 spec_cu = cu;
21641 spec_die = die_specification (die, &spec_cu);
21642 if (spec_die == NULL)
21643 parent = die->parent;
21644 else
21645 {
21646 parent = spec_die->parent;
21647 cu = spec_cu;
21648 }
21649
21650 if (parent == NULL)
21651 return "";
21652 else if (parent->building_fullname)
21653 {
21654 const char *name;
21655 const char *parent_name;
21656
21657 /* It has been seen on RealView 2.2 built binaries,
21658 DW_TAG_template_type_param types actually _defined_ as
21659 children of the parent class:
21660
21661 enum E {};
21662 template class <class Enum> Class{};
21663 Class<enum E> class_e;
21664
21665 1: DW_TAG_class_type (Class)
21666 2: DW_TAG_enumeration_type (E)
21667 3: DW_TAG_enumerator (enum1:0)
21668 3: DW_TAG_enumerator (enum2:1)
21669 ...
21670 2: DW_TAG_template_type_param
21671 DW_AT_type DW_FORM_ref_udata (E)
21672
21673 Besides being broken debug info, it can put GDB into an
21674 infinite loop. Consider:
21675
21676 When we're building the full name for Class<E>, we'll start
21677 at Class, and go look over its template type parameters,
21678 finding E. We'll then try to build the full name of E, and
21679 reach here. We're now trying to build the full name of E,
21680 and look over the parent DIE for containing scope. In the
21681 broken case, if we followed the parent DIE of E, we'd again
21682 find Class, and once again go look at its template type
21683 arguments, etc., etc. Simply don't consider such parent die
21684 as source-level parent of this die (it can't be, the language
21685 doesn't allow it), and break the loop here. */
21686 name = dwarf2_name (die, cu);
21687 parent_name = dwarf2_name (parent, cu);
21688 complaint (_("template param type '%s' defined within parent '%s'"),
21689 name ? name : "<unknown>",
21690 parent_name ? parent_name : "<unknown>");
21691 return "";
21692 }
21693 else
21694 switch (parent->tag)
21695 {
21696 case DW_TAG_namespace:
21697 parent_type = read_type_die (parent, cu);
21698 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21699 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21700 Work around this problem here. */
21701 if (cu->language == language_cplus
21702 && strcmp (parent_type->name (), "::") == 0)
21703 return "";
21704 /* We give a name to even anonymous namespaces. */
21705 return parent_type->name ();
21706 case DW_TAG_class_type:
21707 case DW_TAG_interface_type:
21708 case DW_TAG_structure_type:
21709 case DW_TAG_union_type:
21710 case DW_TAG_module:
21711 parent_type = read_type_die (parent, cu);
21712 if (parent_type->name () != NULL)
21713 return parent_type->name ();
21714 else
21715 /* An anonymous structure is only allowed non-static data
21716 members; no typedefs, no member functions, et cetera.
21717 So it does not need a prefix. */
21718 return "";
21719 case DW_TAG_compile_unit:
21720 case DW_TAG_partial_unit:
21721 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21722 if (cu->language == language_cplus
21723 && !dwarf2_per_objfile->per_bfd->types.empty ()
21724 && die->child != NULL
21725 && (die->tag == DW_TAG_class_type
21726 || die->tag == DW_TAG_structure_type
21727 || die->tag == DW_TAG_union_type))
21728 {
21729 const char *name = guess_full_die_structure_name (die, cu);
21730 if (name != NULL)
21731 return name;
21732 }
21733 return "";
21734 case DW_TAG_subprogram:
21735 /* Nested subroutines in Fortran get a prefix with the name
21736 of the parent's subroutine. */
21737 if (cu->language == language_fortran)
21738 {
21739 if ((die->tag == DW_TAG_subprogram)
21740 && (dwarf2_name (parent, cu) != NULL))
21741 return dwarf2_name (parent, cu);
21742 }
21743 return determine_prefix (parent, cu);
21744 case DW_TAG_enumeration_type:
21745 parent_type = read_type_die (parent, cu);
21746 if (TYPE_DECLARED_CLASS (parent_type))
21747 {
21748 if (parent_type->name () != NULL)
21749 return parent_type->name ();
21750 return "";
21751 }
21752 /* Fall through. */
21753 default:
21754 return determine_prefix (parent, cu);
21755 }
21756 }
21757
21758 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21759 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21760 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21761 an obconcat, otherwise allocate storage for the result. The CU argument is
21762 used to determine the language and hence, the appropriate separator. */
21763
21764 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21765
21766 static char *
21767 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21768 int physname, struct dwarf2_cu *cu)
21769 {
21770 const char *lead = "";
21771 const char *sep;
21772
21773 if (suffix == NULL || suffix[0] == '\0'
21774 || prefix == NULL || prefix[0] == '\0')
21775 sep = "";
21776 else if (cu->language == language_d)
21777 {
21778 /* For D, the 'main' function could be defined in any module, but it
21779 should never be prefixed. */
21780 if (strcmp (suffix, "D main") == 0)
21781 {
21782 prefix = "";
21783 sep = "";
21784 }
21785 else
21786 sep = ".";
21787 }
21788 else if (cu->language == language_fortran && physname)
21789 {
21790 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21791 DW_AT_MIPS_linkage_name is preferred and used instead. */
21792
21793 lead = "__";
21794 sep = "_MOD_";
21795 }
21796 else
21797 sep = "::";
21798
21799 if (prefix == NULL)
21800 prefix = "";
21801 if (suffix == NULL)
21802 suffix = "";
21803
21804 if (obs == NULL)
21805 {
21806 char *retval
21807 = ((char *)
21808 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21809
21810 strcpy (retval, lead);
21811 strcat (retval, prefix);
21812 strcat (retval, sep);
21813 strcat (retval, suffix);
21814 return retval;
21815 }
21816 else
21817 {
21818 /* We have an obstack. */
21819 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21820 }
21821 }
21822
21823 /* Get name of a die, return NULL if not found. */
21824
21825 static const char *
21826 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21827 struct objfile *objfile)
21828 {
21829 if (name && cu->language == language_cplus)
21830 {
21831 gdb::unique_xmalloc_ptr<char> canon_name
21832 = cp_canonicalize_string (name);
21833
21834 if (canon_name != nullptr)
21835 name = objfile->intern (canon_name.get ());
21836 }
21837
21838 return name;
21839 }
21840
21841 /* Get name of a die, return NULL if not found.
21842 Anonymous namespaces are converted to their magic string. */
21843
21844 static const char *
21845 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21846 {
21847 struct attribute *attr;
21848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21849
21850 attr = dwarf2_attr (die, DW_AT_name, cu);
21851 if ((!attr || !DW_STRING (attr))
21852 && die->tag != DW_TAG_namespace
21853 && die->tag != DW_TAG_class_type
21854 && die->tag != DW_TAG_interface_type
21855 && die->tag != DW_TAG_structure_type
21856 && die->tag != DW_TAG_union_type)
21857 return NULL;
21858
21859 switch (die->tag)
21860 {
21861 case DW_TAG_compile_unit:
21862 case DW_TAG_partial_unit:
21863 /* Compilation units have a DW_AT_name that is a filename, not
21864 a source language identifier. */
21865 case DW_TAG_enumeration_type:
21866 case DW_TAG_enumerator:
21867 /* These tags always have simple identifiers already; no need
21868 to canonicalize them. */
21869 return DW_STRING (attr);
21870
21871 case DW_TAG_namespace:
21872 if (attr != NULL && DW_STRING (attr) != NULL)
21873 return DW_STRING (attr);
21874 return CP_ANONYMOUS_NAMESPACE_STR;
21875
21876 case DW_TAG_class_type:
21877 case DW_TAG_interface_type:
21878 case DW_TAG_structure_type:
21879 case DW_TAG_union_type:
21880 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21881 structures or unions. These were of the form "._%d" in GCC 4.1,
21882 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21883 and GCC 4.4. We work around this problem by ignoring these. */
21884 if (attr && DW_STRING (attr)
21885 && (startswith (DW_STRING (attr), "._")
21886 || startswith (DW_STRING (attr), "<anonymous")))
21887 return NULL;
21888
21889 /* GCC might emit a nameless typedef that has a linkage name. See
21890 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21891 if (!attr || DW_STRING (attr) == NULL)
21892 {
21893 attr = dw2_linkage_name_attr (die, cu);
21894 if (attr == NULL || DW_STRING (attr) == NULL)
21895 return NULL;
21896
21897 /* Avoid demangling DW_STRING (attr) the second time on a second
21898 call for the same DIE. */
21899 if (!DW_STRING_IS_CANONICAL (attr))
21900 {
21901 gdb::unique_xmalloc_ptr<char> demangled
21902 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21903 if (demangled == nullptr)
21904 return nullptr;
21905
21906 DW_STRING (attr) = objfile->intern (demangled.get ());
21907 DW_STRING_IS_CANONICAL (attr) = 1;
21908 }
21909
21910 /* Strip any leading namespaces/classes, keep only the base name.
21911 DW_AT_name for named DIEs does not contain the prefixes. */
21912 const char *base = strrchr (DW_STRING (attr), ':');
21913 if (base && base > DW_STRING (attr) && base[-1] == ':')
21914 return &base[1];
21915 else
21916 return DW_STRING (attr);
21917 }
21918 break;
21919
21920 default:
21921 break;
21922 }
21923
21924 if (!DW_STRING_IS_CANONICAL (attr))
21925 {
21926 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21927 objfile);
21928 DW_STRING_IS_CANONICAL (attr) = 1;
21929 }
21930 return DW_STRING (attr);
21931 }
21932
21933 /* Return the die that this die in an extension of, or NULL if there
21934 is none. *EXT_CU is the CU containing DIE on input, and the CU
21935 containing the return value on output. */
21936
21937 static struct die_info *
21938 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21939 {
21940 struct attribute *attr;
21941
21942 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21943 if (attr == NULL)
21944 return NULL;
21945
21946 return follow_die_ref (die, attr, ext_cu);
21947 }
21948
21949 static void
21950 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21951 {
21952 unsigned int i;
21953
21954 print_spaces (indent, f);
21955 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21956 dwarf_tag_name (die->tag), die->abbrev,
21957 sect_offset_str (die->sect_off));
21958
21959 if (die->parent != NULL)
21960 {
21961 print_spaces (indent, f);
21962 fprintf_unfiltered (f, " parent at offset: %s\n",
21963 sect_offset_str (die->parent->sect_off));
21964 }
21965
21966 print_spaces (indent, f);
21967 fprintf_unfiltered (f, " has children: %s\n",
21968 dwarf_bool_name (die->child != NULL));
21969
21970 print_spaces (indent, f);
21971 fprintf_unfiltered (f, " attributes:\n");
21972
21973 for (i = 0; i < die->num_attrs; ++i)
21974 {
21975 print_spaces (indent, f);
21976 fprintf_unfiltered (f, " %s (%s) ",
21977 dwarf_attr_name (die->attrs[i].name),
21978 dwarf_form_name (die->attrs[i].form));
21979
21980 switch (die->attrs[i].form)
21981 {
21982 case DW_FORM_addr:
21983 case DW_FORM_addrx:
21984 case DW_FORM_GNU_addr_index:
21985 fprintf_unfiltered (f, "address: ");
21986 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21987 break;
21988 case DW_FORM_block2:
21989 case DW_FORM_block4:
21990 case DW_FORM_block:
21991 case DW_FORM_block1:
21992 fprintf_unfiltered (f, "block: size %s",
21993 pulongest (DW_BLOCK (&die->attrs[i])->size));
21994 break;
21995 case DW_FORM_exprloc:
21996 fprintf_unfiltered (f, "expression: size %s",
21997 pulongest (DW_BLOCK (&die->attrs[i])->size));
21998 break;
21999 case DW_FORM_data16:
22000 fprintf_unfiltered (f, "constant of 16 bytes");
22001 break;
22002 case DW_FORM_ref_addr:
22003 fprintf_unfiltered (f, "ref address: ");
22004 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22005 break;
22006 case DW_FORM_GNU_ref_alt:
22007 fprintf_unfiltered (f, "alt ref address: ");
22008 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22009 break;
22010 case DW_FORM_ref1:
22011 case DW_FORM_ref2:
22012 case DW_FORM_ref4:
22013 case DW_FORM_ref8:
22014 case DW_FORM_ref_udata:
22015 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22016 (long) (DW_UNSND (&die->attrs[i])));
22017 break;
22018 case DW_FORM_data1:
22019 case DW_FORM_data2:
22020 case DW_FORM_data4:
22021 case DW_FORM_data8:
22022 case DW_FORM_udata:
22023 case DW_FORM_sdata:
22024 fprintf_unfiltered (f, "constant: %s",
22025 pulongest (DW_UNSND (&die->attrs[i])));
22026 break;
22027 case DW_FORM_sec_offset:
22028 fprintf_unfiltered (f, "section offset: %s",
22029 pulongest (DW_UNSND (&die->attrs[i])));
22030 break;
22031 case DW_FORM_ref_sig8:
22032 fprintf_unfiltered (f, "signature: %s",
22033 hex_string (DW_SIGNATURE (&die->attrs[i])));
22034 break;
22035 case DW_FORM_string:
22036 case DW_FORM_strp:
22037 case DW_FORM_line_strp:
22038 case DW_FORM_strx:
22039 case DW_FORM_GNU_str_index:
22040 case DW_FORM_GNU_strp_alt:
22041 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22042 DW_STRING (&die->attrs[i])
22043 ? DW_STRING (&die->attrs[i]) : "",
22044 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22045 break;
22046 case DW_FORM_flag:
22047 if (DW_UNSND (&die->attrs[i]))
22048 fprintf_unfiltered (f, "flag: TRUE");
22049 else
22050 fprintf_unfiltered (f, "flag: FALSE");
22051 break;
22052 case DW_FORM_flag_present:
22053 fprintf_unfiltered (f, "flag: TRUE");
22054 break;
22055 case DW_FORM_indirect:
22056 /* The reader will have reduced the indirect form to
22057 the "base form" so this form should not occur. */
22058 fprintf_unfiltered (f,
22059 "unexpected attribute form: DW_FORM_indirect");
22060 break;
22061 case DW_FORM_implicit_const:
22062 fprintf_unfiltered (f, "constant: %s",
22063 plongest (DW_SND (&die->attrs[i])));
22064 break;
22065 default:
22066 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22067 die->attrs[i].form);
22068 break;
22069 }
22070 fprintf_unfiltered (f, "\n");
22071 }
22072 }
22073
22074 static void
22075 dump_die_for_error (struct die_info *die)
22076 {
22077 dump_die_shallow (gdb_stderr, 0, die);
22078 }
22079
22080 static void
22081 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22082 {
22083 int indent = level * 4;
22084
22085 gdb_assert (die != NULL);
22086
22087 if (level >= max_level)
22088 return;
22089
22090 dump_die_shallow (f, indent, die);
22091
22092 if (die->child != NULL)
22093 {
22094 print_spaces (indent, f);
22095 fprintf_unfiltered (f, " Children:");
22096 if (level + 1 < max_level)
22097 {
22098 fprintf_unfiltered (f, "\n");
22099 dump_die_1 (f, level + 1, max_level, die->child);
22100 }
22101 else
22102 {
22103 fprintf_unfiltered (f,
22104 " [not printed, max nesting level reached]\n");
22105 }
22106 }
22107
22108 if (die->sibling != NULL && level > 0)
22109 {
22110 dump_die_1 (f, level, max_level, die->sibling);
22111 }
22112 }
22113
22114 /* This is called from the pdie macro in gdbinit.in.
22115 It's not static so gcc will keep a copy callable from gdb. */
22116
22117 void
22118 dump_die (struct die_info *die, int max_level)
22119 {
22120 dump_die_1 (gdb_stdlog, 0, max_level, die);
22121 }
22122
22123 static void
22124 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22125 {
22126 void **slot;
22127
22128 slot = htab_find_slot_with_hash (cu->die_hash, die,
22129 to_underlying (die->sect_off),
22130 INSERT);
22131
22132 *slot = die;
22133 }
22134
22135 /* Follow reference or signature attribute ATTR of SRC_DIE.
22136 On entry *REF_CU is the CU of SRC_DIE.
22137 On exit *REF_CU is the CU of the result. */
22138
22139 static struct die_info *
22140 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22141 struct dwarf2_cu **ref_cu)
22142 {
22143 struct die_info *die;
22144
22145 if (attr->form_is_ref ())
22146 die = follow_die_ref (src_die, attr, ref_cu);
22147 else if (attr->form == DW_FORM_ref_sig8)
22148 die = follow_die_sig (src_die, attr, ref_cu);
22149 else
22150 {
22151 dump_die_for_error (src_die);
22152 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22153 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22154 }
22155
22156 return die;
22157 }
22158
22159 /* Follow reference OFFSET.
22160 On entry *REF_CU is the CU of the source die referencing OFFSET.
22161 On exit *REF_CU is the CU of the result.
22162 Returns NULL if OFFSET is invalid. */
22163
22164 static struct die_info *
22165 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22166 struct dwarf2_cu **ref_cu)
22167 {
22168 struct die_info temp_die;
22169 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22170 struct dwarf2_per_objfile *dwarf2_per_objfile
22171 = cu->per_cu->dwarf2_per_objfile;
22172
22173 gdb_assert (cu->per_cu != NULL);
22174
22175 target_cu = cu;
22176
22177 if (cu->per_cu->is_debug_types)
22178 {
22179 /* .debug_types CUs cannot reference anything outside their CU.
22180 If they need to, they have to reference a signatured type via
22181 DW_FORM_ref_sig8. */
22182 if (!cu->header.offset_in_cu_p (sect_off))
22183 return NULL;
22184 }
22185 else if (offset_in_dwz != cu->per_cu->is_dwz
22186 || !cu->header.offset_in_cu_p (sect_off))
22187 {
22188 struct dwarf2_per_cu_data *per_cu;
22189
22190 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22191 dwarf2_per_objfile);
22192
22193 /* If necessary, add it to the queue and load its DIEs. */
22194 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22195 load_full_comp_unit (per_cu, false, cu->language);
22196
22197 target_cu = per_cu->cu;
22198 }
22199 else if (cu->dies == NULL)
22200 {
22201 /* We're loading full DIEs during partial symbol reading. */
22202 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22203 load_full_comp_unit (cu->per_cu, false, language_minimal);
22204 }
22205
22206 *ref_cu = target_cu;
22207 temp_die.sect_off = sect_off;
22208
22209 if (target_cu != cu)
22210 target_cu->ancestor = cu;
22211
22212 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22213 &temp_die,
22214 to_underlying (sect_off));
22215 }
22216
22217 /* Follow reference attribute ATTR of SRC_DIE.
22218 On entry *REF_CU is the CU of SRC_DIE.
22219 On exit *REF_CU is the CU of the result. */
22220
22221 static struct die_info *
22222 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22223 struct dwarf2_cu **ref_cu)
22224 {
22225 sect_offset sect_off = attr->get_ref_die_offset ();
22226 struct dwarf2_cu *cu = *ref_cu;
22227 struct die_info *die;
22228
22229 die = follow_die_offset (sect_off,
22230 (attr->form == DW_FORM_GNU_ref_alt
22231 || cu->per_cu->is_dwz),
22232 ref_cu);
22233 if (!die)
22234 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22235 "at %s [in module %s]"),
22236 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22238
22239 return die;
22240 }
22241
22242 /* See read.h. */
22243
22244 struct dwarf2_locexpr_baton
22245 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22246 dwarf2_per_cu_data *per_cu,
22247 CORE_ADDR (*get_frame_pc) (void *baton),
22248 void *baton, bool resolve_abstract_p)
22249 {
22250 struct dwarf2_cu *cu;
22251 struct die_info *die;
22252 struct attribute *attr;
22253 struct dwarf2_locexpr_baton retval;
22254 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22255 struct objfile *objfile = dwarf2_per_objfile->objfile;
22256
22257 if (per_cu->cu == NULL)
22258 load_cu (per_cu, false);
22259 cu = per_cu->cu;
22260 if (cu == NULL)
22261 {
22262 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22263 Instead just throw an error, not much else we can do. */
22264 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22265 sect_offset_str (sect_off), objfile_name (objfile));
22266 }
22267
22268 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22269 if (!die)
22270 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22271 sect_offset_str (sect_off), objfile_name (objfile));
22272
22273 attr = dwarf2_attr (die, DW_AT_location, cu);
22274 if (!attr && resolve_abstract_p
22275 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22276 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22277 {
22278 CORE_ADDR pc = (*get_frame_pc) (baton);
22279 CORE_ADDR baseaddr = objfile->text_section_offset ();
22280 struct gdbarch *gdbarch = objfile->arch ();
22281
22282 for (const auto &cand_off
22283 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22284 {
22285 struct dwarf2_cu *cand_cu = cu;
22286 struct die_info *cand
22287 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22288 if (!cand
22289 || !cand->parent
22290 || cand->parent->tag != DW_TAG_subprogram)
22291 continue;
22292
22293 CORE_ADDR pc_low, pc_high;
22294 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22295 if (pc_low == ((CORE_ADDR) -1))
22296 continue;
22297 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22298 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22299 if (!(pc_low <= pc && pc < pc_high))
22300 continue;
22301
22302 die = cand;
22303 attr = dwarf2_attr (die, DW_AT_location, cu);
22304 break;
22305 }
22306 }
22307
22308 if (!attr)
22309 {
22310 /* DWARF: "If there is no such attribute, then there is no effect.".
22311 DATA is ignored if SIZE is 0. */
22312
22313 retval.data = NULL;
22314 retval.size = 0;
22315 }
22316 else if (attr->form_is_section_offset ())
22317 {
22318 struct dwarf2_loclist_baton loclist_baton;
22319 CORE_ADDR pc = (*get_frame_pc) (baton);
22320 size_t size;
22321
22322 fill_in_loclist_baton (cu, &loclist_baton, attr);
22323
22324 retval.data = dwarf2_find_location_expression (&loclist_baton,
22325 &size, pc);
22326 retval.size = size;
22327 }
22328 else
22329 {
22330 if (!attr->form_is_block ())
22331 error (_("Dwarf Error: DIE at %s referenced in module %s "
22332 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22333 sect_offset_str (sect_off), objfile_name (objfile));
22334
22335 retval.data = DW_BLOCK (attr)->data;
22336 retval.size = DW_BLOCK (attr)->size;
22337 }
22338 retval.per_objfile = dwarf2_per_objfile;
22339 retval.per_cu = cu->per_cu;
22340
22341 age_cached_comp_units (dwarf2_per_objfile);
22342
22343 return retval;
22344 }
22345
22346 /* See read.h. */
22347
22348 struct dwarf2_locexpr_baton
22349 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22350 dwarf2_per_cu_data *per_cu,
22351 CORE_ADDR (*get_frame_pc) (void *baton),
22352 void *baton)
22353 {
22354 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22355
22356 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22357 }
22358
22359 /* Write a constant of a given type as target-ordered bytes into
22360 OBSTACK. */
22361
22362 static const gdb_byte *
22363 write_constant_as_bytes (struct obstack *obstack,
22364 enum bfd_endian byte_order,
22365 struct type *type,
22366 ULONGEST value,
22367 LONGEST *len)
22368 {
22369 gdb_byte *result;
22370
22371 *len = TYPE_LENGTH (type);
22372 result = (gdb_byte *) obstack_alloc (obstack, *len);
22373 store_unsigned_integer (result, *len, byte_order, value);
22374
22375 return result;
22376 }
22377
22378 /* See read.h. */
22379
22380 const gdb_byte *
22381 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22382 dwarf2_per_cu_data *per_cu,
22383 obstack *obstack,
22384 LONGEST *len)
22385 {
22386 struct dwarf2_cu *cu;
22387 struct die_info *die;
22388 struct attribute *attr;
22389 const gdb_byte *result = NULL;
22390 struct type *type;
22391 LONGEST value;
22392 enum bfd_endian byte_order;
22393 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22394
22395 if (per_cu->cu == NULL)
22396 load_cu (per_cu, false);
22397 cu = per_cu->cu;
22398 if (cu == NULL)
22399 {
22400 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22401 Instead just throw an error, not much else we can do. */
22402 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22403 sect_offset_str (sect_off), objfile_name (objfile));
22404 }
22405
22406 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22407 if (!die)
22408 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22409 sect_offset_str (sect_off), objfile_name (objfile));
22410
22411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22412 if (attr == NULL)
22413 return NULL;
22414
22415 byte_order = (bfd_big_endian (objfile->obfd)
22416 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22417
22418 switch (attr->form)
22419 {
22420 case DW_FORM_addr:
22421 case DW_FORM_addrx:
22422 case DW_FORM_GNU_addr_index:
22423 {
22424 gdb_byte *tem;
22425
22426 *len = cu->header.addr_size;
22427 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22428 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22429 result = tem;
22430 }
22431 break;
22432 case DW_FORM_string:
22433 case DW_FORM_strp:
22434 case DW_FORM_strx:
22435 case DW_FORM_GNU_str_index:
22436 case DW_FORM_GNU_strp_alt:
22437 /* DW_STRING is already allocated on the objfile obstack, point
22438 directly to it. */
22439 result = (const gdb_byte *) DW_STRING (attr);
22440 *len = strlen (DW_STRING (attr));
22441 break;
22442 case DW_FORM_block1:
22443 case DW_FORM_block2:
22444 case DW_FORM_block4:
22445 case DW_FORM_block:
22446 case DW_FORM_exprloc:
22447 case DW_FORM_data16:
22448 result = DW_BLOCK (attr)->data;
22449 *len = DW_BLOCK (attr)->size;
22450 break;
22451
22452 /* The DW_AT_const_value attributes are supposed to carry the
22453 symbol's value "represented as it would be on the target
22454 architecture." By the time we get here, it's already been
22455 converted to host endianness, so we just need to sign- or
22456 zero-extend it as appropriate. */
22457 case DW_FORM_data1:
22458 type = die_type (die, cu);
22459 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22460 if (result == NULL)
22461 result = write_constant_as_bytes (obstack, byte_order,
22462 type, value, len);
22463 break;
22464 case DW_FORM_data2:
22465 type = die_type (die, cu);
22466 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22467 if (result == NULL)
22468 result = write_constant_as_bytes (obstack, byte_order,
22469 type, value, len);
22470 break;
22471 case DW_FORM_data4:
22472 type = die_type (die, cu);
22473 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22474 if (result == NULL)
22475 result = write_constant_as_bytes (obstack, byte_order,
22476 type, value, len);
22477 break;
22478 case DW_FORM_data8:
22479 type = die_type (die, cu);
22480 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22481 if (result == NULL)
22482 result = write_constant_as_bytes (obstack, byte_order,
22483 type, value, len);
22484 break;
22485
22486 case DW_FORM_sdata:
22487 case DW_FORM_implicit_const:
22488 type = die_type (die, cu);
22489 result = write_constant_as_bytes (obstack, byte_order,
22490 type, DW_SND (attr), len);
22491 break;
22492
22493 case DW_FORM_udata:
22494 type = die_type (die, cu);
22495 result = write_constant_as_bytes (obstack, byte_order,
22496 type, DW_UNSND (attr), len);
22497 break;
22498
22499 default:
22500 complaint (_("unsupported const value attribute form: '%s'"),
22501 dwarf_form_name (attr->form));
22502 break;
22503 }
22504
22505 return result;
22506 }
22507
22508 /* See read.h. */
22509
22510 struct type *
22511 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22512 dwarf2_per_cu_data *per_cu)
22513 {
22514 struct dwarf2_cu *cu;
22515 struct die_info *die;
22516
22517 if (per_cu->cu == NULL)
22518 load_cu (per_cu, false);
22519 cu = per_cu->cu;
22520 if (!cu)
22521 return NULL;
22522
22523 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22524 if (!die)
22525 return NULL;
22526
22527 return die_type (die, cu);
22528 }
22529
22530 /* See read.h. */
22531
22532 struct type *
22533 dwarf2_get_die_type (cu_offset die_offset,
22534 struct dwarf2_per_cu_data *per_cu)
22535 {
22536 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22537 return get_die_type_at_offset (die_offset_sect, per_cu);
22538 }
22539
22540 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22541 On entry *REF_CU is the CU of SRC_DIE.
22542 On exit *REF_CU is the CU of the result.
22543 Returns NULL if the referenced DIE isn't found. */
22544
22545 static struct die_info *
22546 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22547 struct dwarf2_cu **ref_cu)
22548 {
22549 struct die_info temp_die;
22550 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22551 struct die_info *die;
22552
22553 /* While it might be nice to assert sig_type->type == NULL here,
22554 we can get here for DW_AT_imported_declaration where we need
22555 the DIE not the type. */
22556
22557 /* If necessary, add it to the queue and load its DIEs. */
22558
22559 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22560 read_signatured_type (sig_type);
22561
22562 sig_cu = sig_type->per_cu.cu;
22563 gdb_assert (sig_cu != NULL);
22564 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22565 temp_die.sect_off = sig_type->type_offset_in_section;
22566 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22567 to_underlying (temp_die.sect_off));
22568 if (die)
22569 {
22570 struct dwarf2_per_objfile *dwarf2_per_objfile
22571 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22572
22573 /* For .gdb_index version 7 keep track of included TUs.
22574 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22575 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22576 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22577 {
22578 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22579 }
22580
22581 *ref_cu = sig_cu;
22582 if (sig_cu != cu)
22583 sig_cu->ancestor = cu;
22584
22585 return die;
22586 }
22587
22588 return NULL;
22589 }
22590
22591 /* Follow signatured type referenced by ATTR in SRC_DIE.
22592 On entry *REF_CU is the CU of SRC_DIE.
22593 On exit *REF_CU is the CU of the result.
22594 The result is the DIE of the type.
22595 If the referenced type cannot be found an error is thrown. */
22596
22597 static struct die_info *
22598 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22599 struct dwarf2_cu **ref_cu)
22600 {
22601 ULONGEST signature = DW_SIGNATURE (attr);
22602 struct signatured_type *sig_type;
22603 struct die_info *die;
22604
22605 gdb_assert (attr->form == DW_FORM_ref_sig8);
22606
22607 sig_type = lookup_signatured_type (*ref_cu, signature);
22608 /* sig_type will be NULL if the signatured type is missing from
22609 the debug info. */
22610 if (sig_type == NULL)
22611 {
22612 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22613 " from DIE at %s [in module %s]"),
22614 hex_string (signature), sect_offset_str (src_die->sect_off),
22615 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22616 }
22617
22618 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22619 if (die == NULL)
22620 {
22621 dump_die_for_error (src_die);
22622 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22623 " from DIE at %s [in module %s]"),
22624 hex_string (signature), sect_offset_str (src_die->sect_off),
22625 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22626 }
22627
22628 return die;
22629 }
22630
22631 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22632 reading in and processing the type unit if necessary. */
22633
22634 static struct type *
22635 get_signatured_type (struct die_info *die, ULONGEST signature,
22636 struct dwarf2_cu *cu)
22637 {
22638 struct dwarf2_per_objfile *dwarf2_per_objfile
22639 = cu->per_cu->dwarf2_per_objfile;
22640 struct signatured_type *sig_type;
22641 struct dwarf2_cu *type_cu;
22642 struct die_info *type_die;
22643 struct type *type;
22644
22645 sig_type = lookup_signatured_type (cu, signature);
22646 /* sig_type will be NULL if the signatured type is missing from
22647 the debug info. */
22648 if (sig_type == NULL)
22649 {
22650 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22651 " from DIE at %s [in module %s]"),
22652 hex_string (signature), sect_offset_str (die->sect_off),
22653 objfile_name (dwarf2_per_objfile->objfile));
22654 return build_error_marker_type (cu, die);
22655 }
22656
22657 /* If we already know the type we're done. */
22658 if (sig_type->type != NULL)
22659 return sig_type->type;
22660
22661 type_cu = cu;
22662 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22663 if (type_die != NULL)
22664 {
22665 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22666 is created. This is important, for example, because for c++ classes
22667 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22668 type = read_type_die (type_die, type_cu);
22669 if (type == NULL)
22670 {
22671 complaint (_("Dwarf Error: Cannot build signatured type %s"
22672 " referenced from DIE at %s [in module %s]"),
22673 hex_string (signature), sect_offset_str (die->sect_off),
22674 objfile_name (dwarf2_per_objfile->objfile));
22675 type = build_error_marker_type (cu, die);
22676 }
22677 }
22678 else
22679 {
22680 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22681 " from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (die->sect_off),
22683 objfile_name (dwarf2_per_objfile->objfile));
22684 type = build_error_marker_type (cu, die);
22685 }
22686 sig_type->type = type;
22687
22688 return type;
22689 }
22690
22691 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22692 reading in and processing the type unit if necessary. */
22693
22694 static struct type *
22695 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22696 struct dwarf2_cu *cu) /* ARI: editCase function */
22697 {
22698 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22699 if (attr->form_is_ref ())
22700 {
22701 struct dwarf2_cu *type_cu = cu;
22702 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22703
22704 return read_type_die (type_die, type_cu);
22705 }
22706 else if (attr->form == DW_FORM_ref_sig8)
22707 {
22708 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22709 }
22710 else
22711 {
22712 struct dwarf2_per_objfile *dwarf2_per_objfile
22713 = cu->per_cu->dwarf2_per_objfile;
22714
22715 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22716 " at %s [in module %s]"),
22717 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22718 objfile_name (dwarf2_per_objfile->objfile));
22719 return build_error_marker_type (cu, die);
22720 }
22721 }
22722
22723 /* Load the DIEs associated with type unit PER_CU into memory. */
22724
22725 static void
22726 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22727 {
22728 struct signatured_type *sig_type;
22729
22730 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22731 gdb_assert (! per_cu->type_unit_group_p ());
22732
22733 /* We have the per_cu, but we need the signatured_type.
22734 Fortunately this is an easy translation. */
22735 gdb_assert (per_cu->is_debug_types);
22736 sig_type = (struct signatured_type *) per_cu;
22737
22738 gdb_assert (per_cu->cu == NULL);
22739
22740 read_signatured_type (sig_type);
22741
22742 gdb_assert (per_cu->cu != NULL);
22743 }
22744
22745 /* Read in a signatured type and build its CU and DIEs.
22746 If the type is a stub for the real type in a DWO file,
22747 read in the real type from the DWO file as well. */
22748
22749 static void
22750 read_signatured_type (struct signatured_type *sig_type)
22751 {
22752 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22753
22754 gdb_assert (per_cu->is_debug_types);
22755 gdb_assert (per_cu->cu == NULL);
22756
22757 cutu_reader reader (per_cu, NULL, 0, false);
22758
22759 if (!reader.dummy_p)
22760 {
22761 struct dwarf2_cu *cu = reader.cu;
22762 const gdb_byte *info_ptr = reader.info_ptr;
22763
22764 gdb_assert (cu->die_hash == NULL);
22765 cu->die_hash =
22766 htab_create_alloc_ex (cu->header.length / 12,
22767 die_hash,
22768 die_eq,
22769 NULL,
22770 &cu->comp_unit_obstack,
22771 hashtab_obstack_allocate,
22772 dummy_obstack_deallocate);
22773
22774 if (reader.comp_unit_die->has_children)
22775 reader.comp_unit_die->child
22776 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22777 reader.comp_unit_die);
22778 cu->dies = reader.comp_unit_die;
22779 /* comp_unit_die is not stored in die_hash, no need. */
22780
22781 /* We try not to read any attributes in this function, because
22782 not all CUs needed for references have been loaded yet, and
22783 symbol table processing isn't initialized. But we have to
22784 set the CU language, or we won't be able to build types
22785 correctly. Similarly, if we do not read the producer, we can
22786 not apply producer-specific interpretation. */
22787 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22788
22789 reader.keep ();
22790 }
22791
22792 sig_type->per_cu.tu_read = 1;
22793 }
22794
22795 /* Decode simple location descriptions.
22796 Given a pointer to a dwarf block that defines a location, compute
22797 the location and return the value. If COMPUTED is non-null, it is
22798 set to true to indicate that decoding was successful, and false
22799 otherwise. If COMPUTED is null, then this function may emit a
22800 complaint. */
22801
22802 static CORE_ADDR
22803 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22804 {
22805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22806 size_t i;
22807 size_t size = blk->size;
22808 const gdb_byte *data = blk->data;
22809 CORE_ADDR stack[64];
22810 int stacki;
22811 unsigned int bytes_read, unsnd;
22812 gdb_byte op;
22813
22814 if (computed != nullptr)
22815 *computed = false;
22816
22817 i = 0;
22818 stacki = 0;
22819 stack[stacki] = 0;
22820 stack[++stacki] = 0;
22821
22822 while (i < size)
22823 {
22824 op = data[i++];
22825 switch (op)
22826 {
22827 case DW_OP_lit0:
22828 case DW_OP_lit1:
22829 case DW_OP_lit2:
22830 case DW_OP_lit3:
22831 case DW_OP_lit4:
22832 case DW_OP_lit5:
22833 case DW_OP_lit6:
22834 case DW_OP_lit7:
22835 case DW_OP_lit8:
22836 case DW_OP_lit9:
22837 case DW_OP_lit10:
22838 case DW_OP_lit11:
22839 case DW_OP_lit12:
22840 case DW_OP_lit13:
22841 case DW_OP_lit14:
22842 case DW_OP_lit15:
22843 case DW_OP_lit16:
22844 case DW_OP_lit17:
22845 case DW_OP_lit18:
22846 case DW_OP_lit19:
22847 case DW_OP_lit20:
22848 case DW_OP_lit21:
22849 case DW_OP_lit22:
22850 case DW_OP_lit23:
22851 case DW_OP_lit24:
22852 case DW_OP_lit25:
22853 case DW_OP_lit26:
22854 case DW_OP_lit27:
22855 case DW_OP_lit28:
22856 case DW_OP_lit29:
22857 case DW_OP_lit30:
22858 case DW_OP_lit31:
22859 stack[++stacki] = op - DW_OP_lit0;
22860 break;
22861
22862 case DW_OP_reg0:
22863 case DW_OP_reg1:
22864 case DW_OP_reg2:
22865 case DW_OP_reg3:
22866 case DW_OP_reg4:
22867 case DW_OP_reg5:
22868 case DW_OP_reg6:
22869 case DW_OP_reg7:
22870 case DW_OP_reg8:
22871 case DW_OP_reg9:
22872 case DW_OP_reg10:
22873 case DW_OP_reg11:
22874 case DW_OP_reg12:
22875 case DW_OP_reg13:
22876 case DW_OP_reg14:
22877 case DW_OP_reg15:
22878 case DW_OP_reg16:
22879 case DW_OP_reg17:
22880 case DW_OP_reg18:
22881 case DW_OP_reg19:
22882 case DW_OP_reg20:
22883 case DW_OP_reg21:
22884 case DW_OP_reg22:
22885 case DW_OP_reg23:
22886 case DW_OP_reg24:
22887 case DW_OP_reg25:
22888 case DW_OP_reg26:
22889 case DW_OP_reg27:
22890 case DW_OP_reg28:
22891 case DW_OP_reg29:
22892 case DW_OP_reg30:
22893 case DW_OP_reg31:
22894 stack[++stacki] = op - DW_OP_reg0;
22895 if (i < size)
22896 {
22897 if (computed == nullptr)
22898 dwarf2_complex_location_expr_complaint ();
22899 else
22900 return 0;
22901 }
22902 break;
22903
22904 case DW_OP_regx:
22905 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22906 i += bytes_read;
22907 stack[++stacki] = unsnd;
22908 if (i < size)
22909 {
22910 if (computed == nullptr)
22911 dwarf2_complex_location_expr_complaint ();
22912 else
22913 return 0;
22914 }
22915 break;
22916
22917 case DW_OP_addr:
22918 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22919 &bytes_read);
22920 i += bytes_read;
22921 break;
22922
22923 case DW_OP_const1u:
22924 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22925 i += 1;
22926 break;
22927
22928 case DW_OP_const1s:
22929 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22930 i += 1;
22931 break;
22932
22933 case DW_OP_const2u:
22934 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22935 i += 2;
22936 break;
22937
22938 case DW_OP_const2s:
22939 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22940 i += 2;
22941 break;
22942
22943 case DW_OP_const4u:
22944 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22945 i += 4;
22946 break;
22947
22948 case DW_OP_const4s:
22949 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22950 i += 4;
22951 break;
22952
22953 case DW_OP_const8u:
22954 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22955 i += 8;
22956 break;
22957
22958 case DW_OP_constu:
22959 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22960 &bytes_read);
22961 i += bytes_read;
22962 break;
22963
22964 case DW_OP_consts:
22965 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22966 i += bytes_read;
22967 break;
22968
22969 case DW_OP_dup:
22970 stack[stacki + 1] = stack[stacki];
22971 stacki++;
22972 break;
22973
22974 case DW_OP_plus:
22975 stack[stacki - 1] += stack[stacki];
22976 stacki--;
22977 break;
22978
22979 case DW_OP_plus_uconst:
22980 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22981 &bytes_read);
22982 i += bytes_read;
22983 break;
22984
22985 case DW_OP_minus:
22986 stack[stacki - 1] -= stack[stacki];
22987 stacki--;
22988 break;
22989
22990 case DW_OP_deref:
22991 /* If we're not the last op, then we definitely can't encode
22992 this using GDB's address_class enum. This is valid for partial
22993 global symbols, although the variable's address will be bogus
22994 in the psymtab. */
22995 if (i < size)
22996 {
22997 if (computed == nullptr)
22998 dwarf2_complex_location_expr_complaint ();
22999 else
23000 return 0;
23001 }
23002 break;
23003
23004 case DW_OP_GNU_push_tls_address:
23005 case DW_OP_form_tls_address:
23006 /* The top of the stack has the offset from the beginning
23007 of the thread control block at which the variable is located. */
23008 /* Nothing should follow this operator, so the top of stack would
23009 be returned. */
23010 /* This is valid for partial global symbols, but the variable's
23011 address will be bogus in the psymtab. Make it always at least
23012 non-zero to not look as a variable garbage collected by linker
23013 which have DW_OP_addr 0. */
23014 if (i < size)
23015 {
23016 if (computed == nullptr)
23017 dwarf2_complex_location_expr_complaint ();
23018 else
23019 return 0;
23020 }
23021 stack[stacki]++;
23022 break;
23023
23024 case DW_OP_GNU_uninit:
23025 if (computed != nullptr)
23026 return 0;
23027 break;
23028
23029 case DW_OP_addrx:
23030 case DW_OP_GNU_addr_index:
23031 case DW_OP_GNU_const_index:
23032 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23033 &bytes_read);
23034 i += bytes_read;
23035 break;
23036
23037 default:
23038 if (computed == nullptr)
23039 {
23040 const char *name = get_DW_OP_name (op);
23041
23042 if (name)
23043 complaint (_("unsupported stack op: '%s'"),
23044 name);
23045 else
23046 complaint (_("unsupported stack op: '%02x'"),
23047 op);
23048 }
23049
23050 return (stack[stacki]);
23051 }
23052
23053 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23054 outside of the allocated space. Also enforce minimum>0. */
23055 if (stacki >= ARRAY_SIZE (stack) - 1)
23056 {
23057 if (computed == nullptr)
23058 complaint (_("location description stack overflow"));
23059 return 0;
23060 }
23061
23062 if (stacki <= 0)
23063 {
23064 if (computed == nullptr)
23065 complaint (_("location description stack underflow"));
23066 return 0;
23067 }
23068 }
23069
23070 if (computed != nullptr)
23071 *computed = true;
23072 return (stack[stacki]);
23073 }
23074
23075 /* memory allocation interface */
23076
23077 static struct dwarf_block *
23078 dwarf_alloc_block (struct dwarf2_cu *cu)
23079 {
23080 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23081 }
23082
23083 static struct die_info *
23084 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23085 {
23086 struct die_info *die;
23087 size_t size = sizeof (struct die_info);
23088
23089 if (num_attrs > 1)
23090 size += (num_attrs - 1) * sizeof (struct attribute);
23091
23092 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23093 memset (die, 0, sizeof (struct die_info));
23094 return (die);
23095 }
23096
23097 \f
23098
23099 /* Macro support. */
23100
23101 /* An overload of dwarf_decode_macros that finds the correct section
23102 and ensures it is read in before calling the other overload. */
23103
23104 static void
23105 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23106 int section_is_gnu)
23107 {
23108 struct dwarf2_per_objfile *dwarf2_per_objfile
23109 = cu->per_cu->dwarf2_per_objfile;
23110 struct objfile *objfile = dwarf2_per_objfile->objfile;
23111 const struct line_header *lh = cu->line_header;
23112 unsigned int offset_size = cu->header.offset_size;
23113 struct dwarf2_section_info *section;
23114 const char *section_name;
23115
23116 if (cu->dwo_unit != nullptr)
23117 {
23118 if (section_is_gnu)
23119 {
23120 section = &cu->dwo_unit->dwo_file->sections.macro;
23121 section_name = ".debug_macro.dwo";
23122 }
23123 else
23124 {
23125 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23126 section_name = ".debug_macinfo.dwo";
23127 }
23128 }
23129 else
23130 {
23131 if (section_is_gnu)
23132 {
23133 section = &dwarf2_per_objfile->per_bfd->macro;
23134 section_name = ".debug_macro";
23135 }
23136 else
23137 {
23138 section = &dwarf2_per_objfile->per_bfd->macinfo;
23139 section_name = ".debug_macinfo";
23140 }
23141 }
23142
23143 section->read (objfile);
23144 if (section->buffer == nullptr)
23145 {
23146 complaint (_("missing %s section"), section_name);
23147 return;
23148 }
23149
23150 buildsym_compunit *builder = cu->get_builder ();
23151
23152 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23153 offset_size, offset, section_is_gnu);
23154 }
23155
23156 /* Return the .debug_loc section to use for CU.
23157 For DWO files use .debug_loc.dwo. */
23158
23159 static struct dwarf2_section_info *
23160 cu_debug_loc_section (struct dwarf2_cu *cu)
23161 {
23162 struct dwarf2_per_objfile *dwarf2_per_objfile
23163 = cu->per_cu->dwarf2_per_objfile;
23164
23165 if (cu->dwo_unit)
23166 {
23167 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23168
23169 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23170 }
23171 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23172 : &dwarf2_per_objfile->per_bfd->loc);
23173 }
23174
23175 /* A helper function that fills in a dwarf2_loclist_baton. */
23176
23177 static void
23178 fill_in_loclist_baton (struct dwarf2_cu *cu,
23179 struct dwarf2_loclist_baton *baton,
23180 const struct attribute *attr)
23181 {
23182 struct dwarf2_per_objfile *dwarf2_per_objfile
23183 = cu->per_cu->dwarf2_per_objfile;
23184 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23185
23186 section->read (dwarf2_per_objfile->objfile);
23187
23188 baton->per_objfile = dwarf2_per_objfile;
23189 baton->per_cu = cu->per_cu;
23190 gdb_assert (baton->per_cu);
23191 /* We don't know how long the location list is, but make sure we
23192 don't run off the edge of the section. */
23193 baton->size = section->size - DW_UNSND (attr);
23194 baton->data = section->buffer + DW_UNSND (attr);
23195 if (cu->base_address.has_value ())
23196 baton->base_address = *cu->base_address;
23197 else
23198 baton->base_address = 0;
23199 baton->from_dwo = cu->dwo_unit != NULL;
23200 }
23201
23202 static void
23203 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23204 struct dwarf2_cu *cu, int is_block)
23205 {
23206 struct dwarf2_per_objfile *dwarf2_per_objfile
23207 = cu->per_cu->dwarf2_per_objfile;
23208 struct objfile *objfile = dwarf2_per_objfile->objfile;
23209 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23210
23211 if (attr->form_is_section_offset ()
23212 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23213 the section. If so, fall through to the complaint in the
23214 other branch. */
23215 && DW_UNSND (attr) < section->get_size (objfile))
23216 {
23217 struct dwarf2_loclist_baton *baton;
23218
23219 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23220
23221 fill_in_loclist_baton (cu, baton, attr);
23222
23223 if (!cu->base_address.has_value ())
23224 complaint (_("Location list used without "
23225 "specifying the CU base address."));
23226
23227 SYMBOL_ACLASS_INDEX (sym) = (is_block
23228 ? dwarf2_loclist_block_index
23229 : dwarf2_loclist_index);
23230 SYMBOL_LOCATION_BATON (sym) = baton;
23231 }
23232 else
23233 {
23234 struct dwarf2_locexpr_baton *baton;
23235
23236 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23237 baton->per_objfile = dwarf2_per_objfile;
23238 baton->per_cu = cu->per_cu;
23239 gdb_assert (baton->per_cu);
23240
23241 if (attr->form_is_block ())
23242 {
23243 /* Note that we're just copying the block's data pointer
23244 here, not the actual data. We're still pointing into the
23245 info_buffer for SYM's objfile; right now we never release
23246 that buffer, but when we do clean up properly this may
23247 need to change. */
23248 baton->size = DW_BLOCK (attr)->size;
23249 baton->data = DW_BLOCK (attr)->data;
23250 }
23251 else
23252 {
23253 dwarf2_invalid_attrib_class_complaint ("location description",
23254 sym->natural_name ());
23255 baton->size = 0;
23256 }
23257
23258 SYMBOL_ACLASS_INDEX (sym) = (is_block
23259 ? dwarf2_locexpr_block_index
23260 : dwarf2_locexpr_index);
23261 SYMBOL_LOCATION_BATON (sym) = baton;
23262 }
23263 }
23264
23265 /* See read.h. */
23266
23267 struct objfile *
23268 dwarf2_per_cu_data::objfile () const
23269 {
23270 struct objfile *objfile = dwarf2_per_objfile->objfile;
23271
23272 /* Return the master objfile, so that we can report and look up the
23273 correct file containing this variable. */
23274 if (objfile->separate_debug_objfile_backlink)
23275 objfile = objfile->separate_debug_objfile_backlink;
23276
23277 return objfile;
23278 }
23279
23280 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23281 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23282 CU_HEADERP first. */
23283
23284 static const struct comp_unit_head *
23285 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23286 const struct dwarf2_per_cu_data *per_cu)
23287 {
23288 const gdb_byte *info_ptr;
23289
23290 if (per_cu->cu)
23291 return &per_cu->cu->header;
23292
23293 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23294
23295 memset (cu_headerp, 0, sizeof (*cu_headerp));
23296 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23297 rcuh_kind::COMPILE);
23298
23299 return cu_headerp;
23300 }
23301
23302 /* See read.h. */
23303
23304 int
23305 dwarf2_per_cu_data::addr_size () const
23306 {
23307 struct comp_unit_head cu_header_local;
23308 const struct comp_unit_head *cu_headerp;
23309
23310 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23311
23312 return cu_headerp->addr_size;
23313 }
23314
23315 /* See read.h. */
23316
23317 int
23318 dwarf2_per_cu_data::offset_size () const
23319 {
23320 struct comp_unit_head cu_header_local;
23321 const struct comp_unit_head *cu_headerp;
23322
23323 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23324
23325 return cu_headerp->offset_size;
23326 }
23327
23328 /* See read.h. */
23329
23330 int
23331 dwarf2_per_cu_data::ref_addr_size () const
23332 {
23333 struct comp_unit_head cu_header_local;
23334 const struct comp_unit_head *cu_headerp;
23335
23336 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23337
23338 if (cu_headerp->version == 2)
23339 return cu_headerp->addr_size;
23340 else
23341 return cu_headerp->offset_size;
23342 }
23343
23344 /* See read.h. */
23345
23346 CORE_ADDR
23347 dwarf2_per_cu_data::text_offset () const
23348 {
23349 struct objfile *objfile = dwarf2_per_objfile->objfile;
23350
23351 return objfile->text_section_offset ();
23352 }
23353
23354 /* See read.h. */
23355
23356 struct type *
23357 dwarf2_per_cu_data::addr_type () const
23358 {
23359 struct objfile *objfile = dwarf2_per_objfile->objfile;
23360 struct type *void_type = objfile_type (objfile)->builtin_void;
23361 struct type *addr_type = lookup_pointer_type (void_type);
23362 int addr_size = this->addr_size ();
23363
23364 if (TYPE_LENGTH (addr_type) == addr_size)
23365 return addr_type;
23366
23367 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23368 return addr_type;
23369 }
23370
23371 /* A helper function for dwarf2_find_containing_comp_unit that returns
23372 the index of the result, and that searches a vector. It will
23373 return a result even if the offset in question does not actually
23374 occur in any CU. This is separate so that it can be unit
23375 tested. */
23376
23377 static int
23378 dwarf2_find_containing_comp_unit
23379 (sect_offset sect_off,
23380 unsigned int offset_in_dwz,
23381 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23382 {
23383 int low, high;
23384
23385 low = 0;
23386 high = all_comp_units.size () - 1;
23387 while (high > low)
23388 {
23389 struct dwarf2_per_cu_data *mid_cu;
23390 int mid = low + (high - low) / 2;
23391
23392 mid_cu = all_comp_units[mid];
23393 if (mid_cu->is_dwz > offset_in_dwz
23394 || (mid_cu->is_dwz == offset_in_dwz
23395 && mid_cu->sect_off + mid_cu->length > sect_off))
23396 high = mid;
23397 else
23398 low = mid + 1;
23399 }
23400 gdb_assert (low == high);
23401 return low;
23402 }
23403
23404 /* Locate the .debug_info compilation unit from CU's objfile which contains
23405 the DIE at OFFSET. Raises an error on failure. */
23406
23407 static struct dwarf2_per_cu_data *
23408 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23409 unsigned int offset_in_dwz,
23410 struct dwarf2_per_objfile *dwarf2_per_objfile)
23411 {
23412 int low
23413 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23414 dwarf2_per_objfile->per_bfd->all_comp_units);
23415 struct dwarf2_per_cu_data *this_cu
23416 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23417
23418 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23419 {
23420 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23421 error (_("Dwarf Error: could not find partial DIE containing "
23422 "offset %s [in module %s]"),
23423 sect_offset_str (sect_off),
23424 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23425
23426 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23427 <= sect_off);
23428 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23429 }
23430 else
23431 {
23432 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23433 && sect_off >= this_cu->sect_off + this_cu->length)
23434 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23435 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23436 return this_cu;
23437 }
23438 }
23439
23440 #if GDB_SELF_TEST
23441
23442 namespace selftests {
23443 namespace find_containing_comp_unit {
23444
23445 static void
23446 run_test ()
23447 {
23448 struct dwarf2_per_cu_data one {};
23449 struct dwarf2_per_cu_data two {};
23450 struct dwarf2_per_cu_data three {};
23451 struct dwarf2_per_cu_data four {};
23452
23453 one.length = 5;
23454 two.sect_off = sect_offset (one.length);
23455 two.length = 7;
23456
23457 three.length = 5;
23458 three.is_dwz = 1;
23459 four.sect_off = sect_offset (three.length);
23460 four.length = 7;
23461 four.is_dwz = 1;
23462
23463 std::vector<dwarf2_per_cu_data *> units;
23464 units.push_back (&one);
23465 units.push_back (&two);
23466 units.push_back (&three);
23467 units.push_back (&four);
23468
23469 int result;
23470
23471 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23472 SELF_CHECK (units[result] == &one);
23473 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23474 SELF_CHECK (units[result] == &one);
23475 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23476 SELF_CHECK (units[result] == &two);
23477
23478 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23479 SELF_CHECK (units[result] == &three);
23480 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23481 SELF_CHECK (units[result] == &three);
23482 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23483 SELF_CHECK (units[result] == &four);
23484 }
23485
23486 }
23487 }
23488
23489 #endif /* GDB_SELF_TEST */
23490
23491 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23492
23493 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23494 : per_cu (per_cu_),
23495 mark (false),
23496 has_loclist (false),
23497 checked_producer (false),
23498 producer_is_gxx_lt_4_6 (false),
23499 producer_is_gcc_lt_4_3 (false),
23500 producer_is_icc (false),
23501 producer_is_icc_lt_14 (false),
23502 producer_is_codewarrior (false),
23503 processing_has_namespace_info (false)
23504 {
23505 per_cu->cu = this;
23506 }
23507
23508 /* Destroy a dwarf2_cu. */
23509
23510 dwarf2_cu::~dwarf2_cu ()
23511 {
23512 per_cu->cu = NULL;
23513 }
23514
23515 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23516
23517 static void
23518 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23519 enum language pretend_language)
23520 {
23521 struct attribute *attr;
23522
23523 /* Set the language we're debugging. */
23524 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23525 if (attr != nullptr)
23526 set_cu_language (DW_UNSND (attr), cu);
23527 else
23528 {
23529 cu->language = pretend_language;
23530 cu->language_defn = language_def (cu->language);
23531 }
23532
23533 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23534 }
23535
23536 /* Increase the age counter on each cached compilation unit, and free
23537 any that are too old. */
23538
23539 static void
23540 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23541 {
23542 struct dwarf2_per_cu_data *per_cu, **last_chain;
23543
23544 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23545 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23546 while (per_cu != NULL)
23547 {
23548 per_cu->cu->last_used ++;
23549 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23550 dwarf2_mark (per_cu->cu);
23551 per_cu = per_cu->cu->read_in_chain;
23552 }
23553
23554 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23555 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23556 while (per_cu != NULL)
23557 {
23558 struct dwarf2_per_cu_data *next_cu;
23559
23560 next_cu = per_cu->cu->read_in_chain;
23561
23562 if (!per_cu->cu->mark)
23563 {
23564 delete per_cu->cu;
23565 *last_chain = next_cu;
23566 }
23567 else
23568 last_chain = &per_cu->cu->read_in_chain;
23569
23570 per_cu = next_cu;
23571 }
23572 }
23573
23574 /* Remove a single compilation unit from the cache. */
23575
23576 static void
23577 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23578 {
23579 struct dwarf2_per_cu_data *per_cu, **last_chain;
23580 struct dwarf2_per_objfile *dwarf2_per_objfile
23581 = target_per_cu->dwarf2_per_objfile;
23582
23583 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23584 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23585 while (per_cu != NULL)
23586 {
23587 struct dwarf2_per_cu_data *next_cu;
23588
23589 next_cu = per_cu->cu->read_in_chain;
23590
23591 if (per_cu == target_per_cu)
23592 {
23593 delete per_cu->cu;
23594 per_cu->cu = NULL;
23595 *last_chain = next_cu;
23596 break;
23597 }
23598 else
23599 last_chain = &per_cu->cu->read_in_chain;
23600
23601 per_cu = next_cu;
23602 }
23603 }
23604
23605 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23606 We store these in a hash table separate from the DIEs, and preserve them
23607 when the DIEs are flushed out of cache.
23608
23609 The CU "per_cu" pointer is needed because offset alone is not enough to
23610 uniquely identify the type. A file may have multiple .debug_types sections,
23611 or the type may come from a DWO file. Furthermore, while it's more logical
23612 to use per_cu->section+offset, with Fission the section with the data is in
23613 the DWO file but we don't know that section at the point we need it.
23614 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23615 because we can enter the lookup routine, get_die_type_at_offset, from
23616 outside this file, and thus won't necessarily have PER_CU->cu.
23617 Fortunately, PER_CU is stable for the life of the objfile. */
23618
23619 struct dwarf2_per_cu_offset_and_type
23620 {
23621 const struct dwarf2_per_cu_data *per_cu;
23622 sect_offset sect_off;
23623 struct type *type;
23624 };
23625
23626 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23627
23628 static hashval_t
23629 per_cu_offset_and_type_hash (const void *item)
23630 {
23631 const struct dwarf2_per_cu_offset_and_type *ofs
23632 = (const struct dwarf2_per_cu_offset_and_type *) item;
23633
23634 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23635 }
23636
23637 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23638
23639 static int
23640 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23641 {
23642 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23643 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23644 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23645 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23646
23647 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23648 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23649 }
23650
23651 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23652 table if necessary. For convenience, return TYPE.
23653
23654 The DIEs reading must have careful ordering to:
23655 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23656 reading current DIE.
23657 * Not trying to dereference contents of still incompletely read in types
23658 while reading in other DIEs.
23659 * Enable referencing still incompletely read in types just by a pointer to
23660 the type without accessing its fields.
23661
23662 Therefore caller should follow these rules:
23663 * Try to fetch any prerequisite types we may need to build this DIE type
23664 before building the type and calling set_die_type.
23665 * After building type call set_die_type for current DIE as soon as
23666 possible before fetching more types to complete the current type.
23667 * Make the type as complete as possible before fetching more types. */
23668
23669 static struct type *
23670 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23671 {
23672 struct dwarf2_per_objfile *dwarf2_per_objfile
23673 = cu->per_cu->dwarf2_per_objfile;
23674 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23675 struct objfile *objfile = dwarf2_per_objfile->objfile;
23676 struct attribute *attr;
23677 struct dynamic_prop prop;
23678
23679 /* For Ada types, make sure that the gnat-specific data is always
23680 initialized (if not already set). There are a few types where
23681 we should not be doing so, because the type-specific area is
23682 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23683 where the type-specific area is used to store the floatformat).
23684 But this is not a problem, because the gnat-specific information
23685 is actually not needed for these types. */
23686 if (need_gnat_info (cu)
23687 && type->code () != TYPE_CODE_FUNC
23688 && type->code () != TYPE_CODE_FLT
23689 && type->code () != TYPE_CODE_METHODPTR
23690 && type->code () != TYPE_CODE_MEMBERPTR
23691 && type->code () != TYPE_CODE_METHOD
23692 && !HAVE_GNAT_AUX_INFO (type))
23693 INIT_GNAT_SPECIFIC (type);
23694
23695 /* Read DW_AT_allocated and set in type. */
23696 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23697 if (attr != NULL && attr->form_is_block ())
23698 {
23699 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23700 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23701 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23702 }
23703 else if (attr != NULL)
23704 {
23705 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23706 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23707 sect_offset_str (die->sect_off));
23708 }
23709
23710 /* Read DW_AT_associated and set in type. */
23711 attr = dwarf2_attr (die, DW_AT_associated, cu);
23712 if (attr != NULL && attr->form_is_block ())
23713 {
23714 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23715 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23716 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23717 }
23718 else if (attr != NULL)
23719 {
23720 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23721 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23722 sect_offset_str (die->sect_off));
23723 }
23724
23725 /* Read DW_AT_data_location and set in type. */
23726 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23727 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23728 cu->per_cu->addr_type ()))
23729 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23730
23731 if (dwarf2_per_objfile->per_bfd->die_type_hash == NULL)
23732 dwarf2_per_objfile->per_bfd->die_type_hash
23733 = htab_up (htab_create_alloc (127,
23734 per_cu_offset_and_type_hash,
23735 per_cu_offset_and_type_eq,
23736 NULL, xcalloc, xfree));
23737
23738 ofs.per_cu = cu->per_cu;
23739 ofs.sect_off = die->sect_off;
23740 ofs.type = type;
23741 slot = (struct dwarf2_per_cu_offset_and_type **)
23742 htab_find_slot (dwarf2_per_objfile->per_bfd->die_type_hash.get (), &ofs, INSERT);
23743 if (*slot)
23744 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23745 sect_offset_str (die->sect_off));
23746 *slot = XOBNEW (&objfile->objfile_obstack,
23747 struct dwarf2_per_cu_offset_and_type);
23748 **slot = ofs;
23749 return type;
23750 }
23751
23752 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23753 or return NULL if the die does not have a saved type. */
23754
23755 static struct type *
23756 get_die_type_at_offset (sect_offset sect_off,
23757 struct dwarf2_per_cu_data *per_cu)
23758 {
23759 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23760 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23761
23762 if (dwarf2_per_objfile->per_bfd->die_type_hash == NULL)
23763 return NULL;
23764
23765 ofs.per_cu = per_cu;
23766 ofs.sect_off = sect_off;
23767 slot = ((struct dwarf2_per_cu_offset_and_type *)
23768 htab_find (dwarf2_per_objfile->per_bfd->die_type_hash.get (), &ofs));
23769 if (slot)
23770 return slot->type;
23771 else
23772 return NULL;
23773 }
23774
23775 /* Look up the type for DIE in CU in die_type_hash,
23776 or return NULL if DIE does not have a saved type. */
23777
23778 static struct type *
23779 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23780 {
23781 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23782 }
23783
23784 /* Add a dependence relationship from CU to REF_PER_CU. */
23785
23786 static void
23787 dwarf2_add_dependence (struct dwarf2_cu *cu,
23788 struct dwarf2_per_cu_data *ref_per_cu)
23789 {
23790 void **slot;
23791
23792 if (cu->dependencies == NULL)
23793 cu->dependencies
23794 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23795 NULL, &cu->comp_unit_obstack,
23796 hashtab_obstack_allocate,
23797 dummy_obstack_deallocate);
23798
23799 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23800 if (*slot == NULL)
23801 *slot = ref_per_cu;
23802 }
23803
23804 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23805 Set the mark field in every compilation unit in the
23806 cache that we must keep because we are keeping CU. */
23807
23808 static int
23809 dwarf2_mark_helper (void **slot, void *data)
23810 {
23811 struct dwarf2_per_cu_data *per_cu;
23812
23813 per_cu = (struct dwarf2_per_cu_data *) *slot;
23814
23815 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23816 reading of the chain. As such dependencies remain valid it is not much
23817 useful to track and undo them during QUIT cleanups. */
23818 if (per_cu->cu == NULL)
23819 return 1;
23820
23821 if (per_cu->cu->mark)
23822 return 1;
23823 per_cu->cu->mark = true;
23824
23825 if (per_cu->cu->dependencies != NULL)
23826 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23827
23828 return 1;
23829 }
23830
23831 /* Set the mark field in CU and in every other compilation unit in the
23832 cache that we must keep because we are keeping CU. */
23833
23834 static void
23835 dwarf2_mark (struct dwarf2_cu *cu)
23836 {
23837 if (cu->mark)
23838 return;
23839 cu->mark = true;
23840 if (cu->dependencies != NULL)
23841 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23842 }
23843
23844 static void
23845 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23846 {
23847 while (per_cu)
23848 {
23849 per_cu->cu->mark = false;
23850 per_cu = per_cu->cu->read_in_chain;
23851 }
23852 }
23853
23854 /* Trivial hash function for partial_die_info: the hash value of a DIE
23855 is its offset in .debug_info for this objfile. */
23856
23857 static hashval_t
23858 partial_die_hash (const void *item)
23859 {
23860 const struct partial_die_info *part_die
23861 = (const struct partial_die_info *) item;
23862
23863 return to_underlying (part_die->sect_off);
23864 }
23865
23866 /* Trivial comparison function for partial_die_info structures: two DIEs
23867 are equal if they have the same offset. */
23868
23869 static int
23870 partial_die_eq (const void *item_lhs, const void *item_rhs)
23871 {
23872 const struct partial_die_info *part_die_lhs
23873 = (const struct partial_die_info *) item_lhs;
23874 const struct partial_die_info *part_die_rhs
23875 = (const struct partial_die_info *) item_rhs;
23876
23877 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23878 }
23879
23880 struct cmd_list_element *set_dwarf_cmdlist;
23881 struct cmd_list_element *show_dwarf_cmdlist;
23882
23883 static void
23884 show_check_physname (struct ui_file *file, int from_tty,
23885 struct cmd_list_element *c, const char *value)
23886 {
23887 fprintf_filtered (file,
23888 _("Whether to check \"physname\" is %s.\n"),
23889 value);
23890 }
23891
23892 void _initialize_dwarf2_read ();
23893 void
23894 _initialize_dwarf2_read ()
23895 {
23896 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23897 Set DWARF specific variables.\n\
23898 Configure DWARF variables such as the cache size."),
23899 &set_dwarf_cmdlist, "maintenance set dwarf ",
23900 0/*allow-unknown*/, &maintenance_set_cmdlist);
23901
23902 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23903 Show DWARF specific variables.\n\
23904 Show DWARF variables such as the cache size."),
23905 &show_dwarf_cmdlist, "maintenance show dwarf ",
23906 0/*allow-unknown*/, &maintenance_show_cmdlist);
23907
23908 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23909 &dwarf_max_cache_age, _("\
23910 Set the upper bound on the age of cached DWARF compilation units."), _("\
23911 Show the upper bound on the age of cached DWARF compilation units."), _("\
23912 A higher limit means that cached compilation units will be stored\n\
23913 in memory longer, and more total memory will be used. Zero disables\n\
23914 caching, which can slow down startup."),
23915 NULL,
23916 show_dwarf_max_cache_age,
23917 &set_dwarf_cmdlist,
23918 &show_dwarf_cmdlist);
23919
23920 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23921 Set debugging of the DWARF reader."), _("\
23922 Show debugging of the DWARF reader."), _("\
23923 When enabled (non-zero), debugging messages are printed during DWARF\n\
23924 reading and symtab expansion. A value of 1 (one) provides basic\n\
23925 information. A value greater than 1 provides more verbose information."),
23926 NULL,
23927 NULL,
23928 &setdebuglist, &showdebuglist);
23929
23930 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23931 Set debugging of the DWARF DIE reader."), _("\
23932 Show debugging of the DWARF DIE reader."), _("\
23933 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23934 The value is the maximum depth to print."),
23935 NULL,
23936 NULL,
23937 &setdebuglist, &showdebuglist);
23938
23939 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23940 Set debugging of the dwarf line reader."), _("\
23941 Show debugging of the dwarf line reader."), _("\
23942 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23943 A value of 1 (one) provides basic information.\n\
23944 A value greater than 1 provides more verbose information."),
23945 NULL,
23946 NULL,
23947 &setdebuglist, &showdebuglist);
23948
23949 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23950 Set cross-checking of \"physname\" code against demangler."), _("\
23951 Show cross-checking of \"physname\" code against demangler."), _("\
23952 When enabled, GDB's internal \"physname\" code is checked against\n\
23953 the demangler."),
23954 NULL, show_check_physname,
23955 &setdebuglist, &showdebuglist);
23956
23957 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23958 no_class, &use_deprecated_index_sections, _("\
23959 Set whether to use deprecated gdb_index sections."), _("\
23960 Show whether to use deprecated gdb_index sections."), _("\
23961 When enabled, deprecated .gdb_index sections are used anyway.\n\
23962 Normally they are ignored either because of a missing feature or\n\
23963 performance issue.\n\
23964 Warning: This option must be enabled before gdb reads the file."),
23965 NULL,
23966 NULL,
23967 &setlist, &showlist);
23968
23969 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23970 &dwarf2_locexpr_funcs);
23971 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23972 &dwarf2_loclist_funcs);
23973
23974 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23975 &dwarf2_block_frame_base_locexpr_funcs);
23976 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23977 &dwarf2_block_frame_base_loclist_funcs);
23978
23979 #if GDB_SELF_TEST
23980 selftests::register_test ("dw2_expand_symtabs_matching",
23981 selftests::dw2_expand_symtabs_matching::run_test);
23982 selftests::register_test ("dwarf2_find_containing_comp_unit",
23983 selftests::find_containing_comp_unit::run_test);
23984 #endif
23985 }
This page took 0.670374 seconds and 3 git commands to generate.