Replace dwarf2_per_cu_data::cu backlink with per-objfile map
[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 (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
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 /* Return a type that is a generic pointer type, the size of which
424 matches the address size given in the compilation unit header for
425 this CU. */
426 struct type *addr_type () const;
427
428 /* Find an integer type the same size as the address size given in
429 the compilation unit header for this CU. UNSIGNED_P controls if
430 the integer is unsigned or not. */
431 struct type *addr_sized_int_type (bool unsigned_p) const;
432
433 /* The header of the compilation unit. */
434 struct comp_unit_head header {};
435
436 /* Base address of this compilation unit. */
437 gdb::optional<CORE_ADDR> base_address;
438
439 /* The language we are debugging. */
440 enum language language = language_unknown;
441 const struct language_defn *language_defn = nullptr;
442
443 const char *producer = nullptr;
444
445 private:
446 /* The symtab builder for this CU. This is only non-NULL when full
447 symbols are being read. */
448 std::unique_ptr<buildsym_compunit> m_builder;
449
450 public:
451 /* The generic symbol table building routines have separate lists for
452 file scope symbols and all all other scopes (local scopes). So
453 we need to select the right one to pass to add_symbol_to_list().
454 We do it by keeping a pointer to the correct list in list_in_scope.
455
456 FIXME: The original dwarf code just treated the file scope as the
457 first local scope, and all other local scopes as nested local
458 scopes, and worked fine. Check to see if we really need to
459 distinguish these in buildsym.c. */
460 struct pending **list_in_scope = nullptr;
461
462 /* Hash table holding all the loaded partial DIEs
463 with partial_die->offset.SECT_OFF as hash. */
464 htab_t partial_dies = nullptr;
465
466 /* Storage for things with the same lifetime as this read-in compilation
467 unit, including partial DIEs. */
468 auto_obstack comp_unit_obstack;
469
470 /* Backlink to our per_cu entry. */
471 struct dwarf2_per_cu_data *per_cu;
472
473 /* The dwarf2_per_objfile that owns this. */
474 struct dwarf2_per_objfile *per_objfile;
475
476 /* How many compilation units ago was this CU last referenced? */
477 int last_used = 0;
478
479 /* A hash table of DIE cu_offset for following references with
480 die_info->offset.sect_off as hash. */
481 htab_t die_hash = nullptr;
482
483 /* Full DIEs if read in. */
484 struct die_info *dies = nullptr;
485
486 /* A set of pointers to dwarf2_per_cu_data objects for compilation
487 units referenced by this one. Only set during full symbol processing;
488 partial symbol tables do not have dependencies. */
489 htab_t dependencies = nullptr;
490
491 /* Header data from the line table, during full symbol processing. */
492 struct line_header *line_header = nullptr;
493 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
494 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
495 this is the DW_TAG_compile_unit die for this CU. We'll hold on
496 to the line header as long as this DIE is being processed. See
497 process_die_scope. */
498 die_info *line_header_die_owner = nullptr;
499
500 /* A list of methods which need to have physnames computed
501 after all type information has been read. */
502 std::vector<delayed_method_info> method_list;
503
504 /* To be copied to symtab->call_site_htab. */
505 htab_t call_site_htab = nullptr;
506
507 /* Non-NULL if this CU came from a DWO file.
508 There is an invariant here that is important to remember:
509 Except for attributes copied from the top level DIE in the "main"
510 (or "stub") file in preparation for reading the DWO file
511 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
512 Either there isn't a DWO file (in which case this is NULL and the point
513 is moot), or there is and either we're not going to read it (in which
514 case this is NULL) or there is and we are reading it (in which case this
515 is non-NULL). */
516 struct dwo_unit *dwo_unit = nullptr;
517
518 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE. */
520 gdb::optional<ULONGEST> addr_base;
521
522 /* The DW_AT_rnglists_base attribute if present.
523 Note this value comes from the Fission stub CU/TU's DIE.
524 Also note that the value is zero in the non-DWO case so this value can
525 be used without needing to know whether DWO files are in use or not.
526 N.B. This does not apply to DW_AT_ranges appearing in
527 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
528 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
529 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
530 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
531 ULONGEST ranges_base = 0;
532
533 /* The DW_AT_loclists_base attribute if present. */
534 ULONGEST loclist_base = 0;
535
536 /* When reading debug info generated by older versions of rustc, we
537 have to rewrite some union types to be struct types with a
538 variant part. This rewriting must be done after the CU is fully
539 read in, because otherwise at the point of rewriting some struct
540 type might not have been fully processed. So, we keep a list of
541 all such types here and process them after expansion. */
542 std::vector<struct type *> rust_unions;
543
544 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
545 files, the value is implicitly zero. For DWARF 5 version DWO files, the
546 value is often implicit and is the size of the header of
547 .debug_str_offsets section (8 or 4, depending on the address size). */
548 gdb::optional<ULONGEST> str_offsets_base;
549
550 /* Mark used when releasing cached dies. */
551 bool mark : 1;
552
553 /* This CU references .debug_loc. See the symtab->locations_valid field.
554 This test is imperfect as there may exist optimized debug code not using
555 any location list and still facing inlining issues if handled as
556 unoptimized code. For a future better test see GCC PR other/32998. */
557 bool has_loclist : 1;
558
559 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
560 if all the producer_is_* fields are valid. This information is cached
561 because profiling CU expansion showed excessive time spent in
562 producer_is_gxx_lt_4_6. */
563 bool checked_producer : 1;
564 bool producer_is_gxx_lt_4_6 : 1;
565 bool producer_is_gcc_lt_4_3 : 1;
566 bool producer_is_icc : 1;
567 bool producer_is_icc_lt_14 : 1;
568 bool producer_is_codewarrior : 1;
569
570 /* When true, the file that we're processing is known to have
571 debugging info for C++ namespaces. GCC 3.3.x did not produce
572 this information, but later versions do. */
573
574 bool processing_has_namespace_info : 1;
575
576 struct partial_die_info *find_partial_die (sect_offset sect_off);
577
578 /* If this CU was inherited by another CU (via specification,
579 abstract_origin, etc), this is the ancestor CU. */
580 dwarf2_cu *ancestor;
581
582 /* Get the buildsym_compunit for this CU. */
583 buildsym_compunit *get_builder ()
584 {
585 /* If this CU has a builder associated with it, use that. */
586 if (m_builder != nullptr)
587 return m_builder.get ();
588
589 /* Otherwise, search ancestors for a valid builder. */
590 if (ancestor != nullptr)
591 return ancestor->get_builder ();
592
593 return nullptr;
594 }
595 };
596
597 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
598 This includes type_unit_group and quick_file_names. */
599
600 struct stmt_list_hash
601 {
602 /* The DWO unit this table is from or NULL if there is none. */
603 struct dwo_unit *dwo_unit;
604
605 /* Offset in .debug_line or .debug_line.dwo. */
606 sect_offset line_sect_off;
607 };
608
609 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
610 an object of this type. This contains elements of type unit groups
611 that can be shared across objfiles. The non-shareable parts are in
612 type_unit_group_unshareable. */
613
614 struct type_unit_group
615 {
616 /* dwarf2read.c's main "handle" on a TU symtab.
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
619 a "per_cu" handle on the symtab. */
620 struct dwarf2_per_cu_data per_cu;
621
622 /* The TUs that share this DW_AT_stmt_list entry.
623 This is added to while parsing type units to build partial symtabs,
624 and is deleted afterwards and not used again. */
625 std::vector<signatured_type *> *tus;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629 };
630
631 /* These sections are what may appear in a (real or virtual) DWO file. */
632
633 struct dwo_sections
634 {
635 struct dwarf2_section_info abbrev;
636 struct dwarf2_section_info line;
637 struct dwarf2_section_info loc;
638 struct dwarf2_section_info loclists;
639 struct dwarf2_section_info macinfo;
640 struct dwarf2_section_info macro;
641 struct dwarf2_section_info str;
642 struct dwarf2_section_info str_offsets;
643 /* In the case of a virtual DWO file, these two are unused. */
644 struct dwarf2_section_info info;
645 std::vector<dwarf2_section_info> types;
646 };
647
648 /* CUs/TUs in DWP/DWO files. */
649
650 struct dwo_unit
651 {
652 /* Backlink to the containing struct dwo_file. */
653 struct dwo_file *dwo_file;
654
655 /* The "id" that distinguishes this CU/TU.
656 .debug_info calls this "dwo_id", .debug_types calls this "signature".
657 Since signatures came first, we stick with it for consistency. */
658 ULONGEST signature;
659
660 /* The section this CU/TU lives in, in the DWO file. */
661 struct dwarf2_section_info *section;
662
663 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
664 sect_offset sect_off;
665 unsigned int length;
666
667 /* For types, offset in the type's DIE of the type defined by this TU. */
668 cu_offset type_offset_in_tu;
669 };
670
671 /* include/dwarf2.h defines the DWP section codes.
672 It defines a max value but it doesn't define a min value, which we
673 use for error checking, so provide one. */
674
675 enum dwp_v2_section_ids
676 {
677 DW_SECT_MIN = 1
678 };
679
680 /* Data for one DWO file.
681
682 This includes virtual DWO files (a virtual DWO file is a DWO file as it
683 appears in a DWP file). DWP files don't really have DWO files per se -
684 comdat folding of types "loses" the DWO file they came from, and from
685 a high level view DWP files appear to contain a mass of random types.
686 However, to maintain consistency with the non-DWP case we pretend DWP
687 files contain virtual DWO files, and we assign each TU with one virtual
688 DWO file (generally based on the line and abbrev section offsets -
689 a heuristic that seems to work in practice). */
690
691 struct dwo_file
692 {
693 dwo_file () = default;
694 DISABLE_COPY_AND_ASSIGN (dwo_file);
695
696 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
697 For virtual DWO files the name is constructed from the section offsets
698 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
699 from related CU+TUs. */
700 const char *dwo_name = nullptr;
701
702 /* The DW_AT_comp_dir attribute. */
703 const char *comp_dir = nullptr;
704
705 /* The bfd, when the file is open. Otherwise this is NULL.
706 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
707 gdb_bfd_ref_ptr dbfd;
708
709 /* The sections that make up this DWO file.
710 Remember that for virtual DWO files in DWP V2, these are virtual
711 sections (for lack of a better name). */
712 struct dwo_sections sections {};
713
714 /* The CUs in the file.
715 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
716 an extension to handle LLVM's Link Time Optimization output (where
717 multiple source files may be compiled into a single object/dwo pair). */
718 htab_up cus;
719
720 /* Table of TUs in the file.
721 Each element is a struct dwo_unit. */
722 htab_up tus;
723 };
724
725 /* These sections are what may appear in a DWP file. */
726
727 struct dwp_sections
728 {
729 /* These are used by both DWP version 1 and 2. */
730 struct dwarf2_section_info str;
731 struct dwarf2_section_info cu_index;
732 struct dwarf2_section_info tu_index;
733
734 /* These are only used by DWP version 2 files.
735 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
736 sections are referenced by section number, and are not recorded here.
737 In DWP version 2 there is at most one copy of all these sections, each
738 section being (effectively) comprised of the concatenation of all of the
739 individual sections that exist in the version 1 format.
740 To keep the code simple we treat each of these concatenated pieces as a
741 section itself (a virtual section?). */
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info info;
744 struct dwarf2_section_info line;
745 struct dwarf2_section_info loc;
746 struct dwarf2_section_info macinfo;
747 struct dwarf2_section_info macro;
748 struct dwarf2_section_info str_offsets;
749 struct dwarf2_section_info types;
750 };
751
752 /* These sections are what may appear in a virtual DWO file in DWP version 1.
753 A virtual DWO file is a DWO file as it appears in a DWP file. */
754
755 struct virtual_v1_dwo_sections
756 {
757 struct dwarf2_section_info abbrev;
758 struct dwarf2_section_info line;
759 struct dwarf2_section_info loc;
760 struct dwarf2_section_info macinfo;
761 struct dwarf2_section_info macro;
762 struct dwarf2_section_info str_offsets;
763 /* Each DWP hash table entry records one CU or one TU.
764 That is recorded here, and copied to dwo_unit.section. */
765 struct dwarf2_section_info info_or_types;
766 };
767
768 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
769 In version 2, the sections of the DWO files are concatenated together
770 and stored in one section of that name. Thus each ELF section contains
771 several "virtual" sections. */
772
773 struct virtual_v2_dwo_sections
774 {
775 bfd_size_type abbrev_offset;
776 bfd_size_type abbrev_size;
777
778 bfd_size_type line_offset;
779 bfd_size_type line_size;
780
781 bfd_size_type loc_offset;
782 bfd_size_type loc_size;
783
784 bfd_size_type macinfo_offset;
785 bfd_size_type macinfo_size;
786
787 bfd_size_type macro_offset;
788 bfd_size_type macro_size;
789
790 bfd_size_type str_offsets_offset;
791 bfd_size_type str_offsets_size;
792
793 /* Each DWP hash table entry records one CU or one TU.
794 That is recorded here, and copied to dwo_unit.section. */
795 bfd_size_type info_or_types_offset;
796 bfd_size_type info_or_types_size;
797 };
798
799 /* Contents of DWP hash tables. */
800
801 struct dwp_hash_table
802 {
803 uint32_t version, nr_columns;
804 uint32_t nr_units, nr_slots;
805 const gdb_byte *hash_table, *unit_table;
806 union
807 {
808 struct
809 {
810 const gdb_byte *indices;
811 } v1;
812 struct
813 {
814 /* This is indexed by column number and gives the id of the section
815 in that column. */
816 #define MAX_NR_V2_DWO_SECTIONS \
817 (1 /* .debug_info or .debug_types */ \
818 + 1 /* .debug_abbrev */ \
819 + 1 /* .debug_line */ \
820 + 1 /* .debug_loc */ \
821 + 1 /* .debug_str_offsets */ \
822 + 1 /* .debug_macro or .debug_macinfo */)
823 int section_ids[MAX_NR_V2_DWO_SECTIONS];
824 const gdb_byte *offsets;
825 const gdb_byte *sizes;
826 } v2;
827 } section_pool;
828 };
829
830 /* Data for one DWP file. */
831
832 struct dwp_file
833 {
834 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
835 : name (name_),
836 dbfd (std::move (abfd))
837 {
838 }
839
840 /* Name of the file. */
841 const char *name;
842
843 /* File format version. */
844 int version = 0;
845
846 /* The bfd. */
847 gdb_bfd_ref_ptr dbfd;
848
849 /* Section info for this file. */
850 struct dwp_sections sections {};
851
852 /* Table of CUs in the file. */
853 const struct dwp_hash_table *cus = nullptr;
854
855 /* Table of TUs in the file. */
856 const struct dwp_hash_table *tus = nullptr;
857
858 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
859 htab_up loaded_cus;
860 htab_up loaded_tus;
861
862 /* Table to map ELF section numbers to their sections.
863 This is only needed for the DWP V1 file format. */
864 unsigned int num_sections = 0;
865 asection **elf_sections = nullptr;
866 };
867
868 /* Struct used to pass misc. parameters to read_die_and_children, et
869 al. which are used for both .debug_info and .debug_types dies.
870 All parameters here are unchanging for the life of the call. This
871 struct exists to abstract away the constant parameters of die reading. */
872
873 struct die_reader_specs
874 {
875 /* The bfd of die_section. */
876 bfd* abfd;
877
878 /* The CU of the DIE we are parsing. */
879 struct dwarf2_cu *cu;
880
881 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
882 struct dwo_file *dwo_file;
883
884 /* The section the die comes from.
885 This is either .debug_info or .debug_types, or the .dwo variants. */
886 struct dwarf2_section_info *die_section;
887
888 /* die_section->buffer. */
889 const gdb_byte *buffer;
890
891 /* The end of the buffer. */
892 const gdb_byte *buffer_end;
893
894 /* The abbreviation table to use when reading the DIEs. */
895 struct abbrev_table *abbrev_table;
896 };
897
898 /* A subclass of die_reader_specs that holds storage and has complex
899 constructor and destructor behavior. */
900
901 class cutu_reader : public die_reader_specs
902 {
903 public:
904
905 cutu_reader (dwarf2_per_cu_data *this_cu,
906 dwarf2_per_objfile *per_objfile,
907 struct abbrev_table *abbrev_table,
908 dwarf2_cu *existing_cu,
909 bool skip_partial);
910
911 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 dwarf2_per_objfile *per_objfile,
913 struct dwarf2_cu *parent_cu = nullptr,
914 struct dwo_file *dwo_file = nullptr);
915
916 DISABLE_COPY_AND_ASSIGN (cutu_reader);
917
918 const gdb_byte *info_ptr = nullptr;
919 struct die_info *comp_unit_die = nullptr;
920 bool dummy_p = false;
921
922 /* Release the new CU, putting it on the chain. This cannot be done
923 for dummy CUs. */
924 void keep ();
925
926 private:
927 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
928 dwarf2_per_objfile *per_objfile,
929 dwarf2_cu *existing_cu);
930
931 struct dwarf2_per_cu_data *m_this_cu;
932 std::unique_ptr<dwarf2_cu> m_new_cu;
933
934 /* The ordinary abbreviation table. */
935 abbrev_table_up m_abbrev_table_holder;
936
937 /* The DWO abbreviation table. */
938 abbrev_table_up m_dwo_abbrev_table;
939 };
940
941 /* When we construct a partial symbol table entry we only
942 need this much information. */
943 struct partial_die_info : public allocate_on_obstack
944 {
945 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
946
947 /* Disable assign but still keep copy ctor, which is needed
948 load_partial_dies. */
949 partial_die_info& operator=(const partial_die_info& rhs) = delete;
950
951 /* Adjust the partial die before generating a symbol for it. This
952 function may set the is_external flag or change the DIE's
953 name. */
954 void fixup (struct dwarf2_cu *cu);
955
956 /* Read a minimal amount of information into the minimal die
957 structure. */
958 const gdb_byte *read (const struct die_reader_specs *reader,
959 const struct abbrev_info &abbrev,
960 const gdb_byte *info_ptr);
961
962 /* Offset of this DIE. */
963 const sect_offset sect_off;
964
965 /* DWARF-2 tag for this DIE. */
966 const ENUM_BITFIELD(dwarf_tag) tag : 16;
967
968 /* Assorted flags describing the data found in this DIE. */
969 const unsigned int has_children : 1;
970
971 unsigned int is_external : 1;
972 unsigned int is_declaration : 1;
973 unsigned int has_type : 1;
974 unsigned int has_specification : 1;
975 unsigned int has_pc_info : 1;
976 unsigned int may_be_inlined : 1;
977
978 /* This DIE has been marked DW_AT_main_subprogram. */
979 unsigned int main_subprogram : 1;
980
981 /* Flag set if the SCOPE field of this structure has been
982 computed. */
983 unsigned int scope_set : 1;
984
985 /* Flag set if the DIE has a byte_size attribute. */
986 unsigned int has_byte_size : 1;
987
988 /* Flag set if the DIE has a DW_AT_const_value attribute. */
989 unsigned int has_const_value : 1;
990
991 /* Flag set if any of the DIE's children are template arguments. */
992 unsigned int has_template_arguments : 1;
993
994 /* Flag set if fixup has been called on this die. */
995 unsigned int fixup_called : 1;
996
997 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
998 unsigned int is_dwz : 1;
999
1000 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1001 unsigned int spec_is_dwz : 1;
1002
1003 /* The name of this DIE. Normally the value of DW_AT_name, but
1004 sometimes a default name for unnamed DIEs. */
1005 const char *name = nullptr;
1006
1007 /* The linkage name, if present. */
1008 const char *linkage_name = nullptr;
1009
1010 /* The scope to prepend to our children. This is generally
1011 allocated on the comp_unit_obstack, so will disappear
1012 when this compilation unit leaves the cache. */
1013 const char *scope = nullptr;
1014
1015 /* Some data associated with the partial DIE. The tag determines
1016 which field is live. */
1017 union
1018 {
1019 /* The location description associated with this DIE, if any. */
1020 struct dwarf_block *locdesc;
1021 /* The offset of an import, for DW_TAG_imported_unit. */
1022 sect_offset sect_off;
1023 } d {};
1024
1025 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1026 CORE_ADDR lowpc = 0;
1027 CORE_ADDR highpc = 0;
1028
1029 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1030 DW_AT_sibling, if any. */
1031 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1032 could return DW_AT_sibling values to its caller load_partial_dies. */
1033 const gdb_byte *sibling = nullptr;
1034
1035 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1036 DW_AT_specification (or DW_AT_abstract_origin or
1037 DW_AT_extension). */
1038 sect_offset spec_offset {};
1039
1040 /* Pointers to this DIE's parent, first child, and next sibling,
1041 if any. */
1042 struct partial_die_info *die_parent = nullptr;
1043 struct partial_die_info *die_child = nullptr;
1044 struct partial_die_info *die_sibling = nullptr;
1045
1046 friend struct partial_die_info *
1047 dwarf2_cu::find_partial_die (sect_offset sect_off);
1048
1049 private:
1050 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1051 partial_die_info (sect_offset sect_off)
1052 : partial_die_info (sect_off, DW_TAG_padding, 0)
1053 {
1054 }
1055
1056 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1057 int has_children_)
1058 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1059 {
1060 is_external = 0;
1061 is_declaration = 0;
1062 has_type = 0;
1063 has_specification = 0;
1064 has_pc_info = 0;
1065 may_be_inlined = 0;
1066 main_subprogram = 0;
1067 scope_set = 0;
1068 has_byte_size = 0;
1069 has_const_value = 0;
1070 has_template_arguments = 0;
1071 fixup_called = 0;
1072 is_dwz = 0;
1073 spec_is_dwz = 0;
1074 }
1075 };
1076
1077 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1078 but this would require a corresponding change in unpack_field_as_long
1079 and friends. */
1080 static int bits_per_byte = 8;
1081
1082 struct variant_part_builder;
1083
1084 /* When reading a variant, we track a bit more information about the
1085 field, and store it in an object of this type. */
1086
1087 struct variant_field
1088 {
1089 int first_field = -1;
1090 int last_field = -1;
1091
1092 /* A variant can contain other variant parts. */
1093 std::vector<variant_part_builder> variant_parts;
1094
1095 /* If we see a DW_TAG_variant, then this will be set if this is the
1096 default branch. */
1097 bool default_branch = false;
1098 /* If we see a DW_AT_discr_value, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value = 0;
1101 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1102 data. */
1103 struct dwarf_block *discr_list_data = nullptr;
1104 };
1105
1106 /* This represents a DW_TAG_variant_part. */
1107
1108 struct variant_part_builder
1109 {
1110 /* The offset of the discriminant field. */
1111 sect_offset discriminant_offset {};
1112
1113 /* Variants that are direct children of this variant part. */
1114 std::vector<variant_field> variants;
1115
1116 /* True if we're currently reading a variant. */
1117 bool processing_variant = false;
1118 };
1119
1120 struct nextfield
1121 {
1122 int accessibility = 0;
1123 int virtuality = 0;
1124 /* Variant parts need to find the discriminant, which is a DIE
1125 reference. We track the section offset of each field to make
1126 this link. */
1127 sect_offset offset;
1128 struct field field {};
1129 };
1130
1131 struct fnfieldlist
1132 {
1133 const char *name = nullptr;
1134 std::vector<struct fn_field> fnfields;
1135 };
1136
1137 /* The routines that read and process dies for a C struct or C++ class
1138 pass lists of data member fields and lists of member function fields
1139 in an instance of a field_info structure, as defined below. */
1140 struct field_info
1141 {
1142 /* List of data member and baseclasses fields. */
1143 std::vector<struct nextfield> fields;
1144 std::vector<struct nextfield> baseclasses;
1145
1146 /* Set if the accessibility of one of the fields is not public. */
1147 int non_public_fields = 0;
1148
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
1152 std::vector<struct fnfieldlist> fnfieldlists;
1153
1154 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1155 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1156 std::vector<struct decl_field> typedef_field_list;
1157
1158 /* Nested types defined by this class and the number of elements in this
1159 list. */
1160 std::vector<struct decl_field> nested_types_list;
1161
1162 /* If non-null, this is the variant part we are currently
1163 reading. */
1164 variant_part_builder *current_variant_part = nullptr;
1165 /* This holds all the top-level variant parts attached to the type
1166 we're reading. */
1167 std::vector<variant_part_builder> variant_parts;
1168
1169 /* Return the total number of fields (including baseclasses). */
1170 int nfields () const
1171 {
1172 return fields.size () + baseclasses.size ();
1173 }
1174 };
1175
1176 /* Loaded secondary compilation units are kept in memory until they
1177 have not been referenced for the processing of this many
1178 compilation units. Set this to zero to disable caching. Cache
1179 sizes of up to at least twenty will improve startup time for
1180 typical inter-CU-reference binaries, at an obvious memory cost. */
1181 static int dwarf_max_cache_age = 5;
1182 static void
1183 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1184 struct cmd_list_element *c, const char *value)
1185 {
1186 fprintf_filtered (file, _("The upper bound on the age of cached "
1187 "DWARF compilation units is %s.\n"),
1188 value);
1189 }
1190 \f
1191 /* local function prototypes */
1192
1193 static void dwarf2_find_base_address (struct die_info *die,
1194 struct dwarf2_cu *cu);
1195
1196 static dwarf2_psymtab *create_partial_symtab
1197 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1198 const char *name);
1199
1200 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1201 const gdb_byte *info_ptr,
1202 struct die_info *type_unit_die);
1203
1204 static void dwarf2_build_psymtabs_hard
1205 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1206
1207 static void scan_partial_symbols (struct partial_die_info *,
1208 CORE_ADDR *, CORE_ADDR *,
1209 int, struct dwarf2_cu *);
1210
1211 static void add_partial_symbol (struct partial_die_info *,
1212 struct dwarf2_cu *);
1213
1214 static void add_partial_namespace (struct partial_die_info *pdi,
1215 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1216 int set_addrmap, struct dwarf2_cu *cu);
1217
1218 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1219 CORE_ADDR *highpc, int set_addrmap,
1220 struct dwarf2_cu *cu);
1221
1222 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1223 struct dwarf2_cu *cu);
1224
1225 static void add_partial_subprogram (struct partial_die_info *pdi,
1226 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1227 int need_pc, struct dwarf2_cu *cu);
1228
1229 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1230
1231 static struct partial_die_info *load_partial_dies
1232 (const struct die_reader_specs *, const gdb_byte *, int);
1233
1234 /* A pair of partial_die_info and compilation unit. */
1235 struct cu_partial_die_info
1236 {
1237 /* The compilation unit of the partial_die_info. */
1238 struct dwarf2_cu *cu;
1239 /* A partial_die_info. */
1240 struct partial_die_info *pdi;
1241
1242 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1243 : cu (cu),
1244 pdi (pdi)
1245 { /* Nothing. */ }
1246
1247 private:
1248 cu_partial_die_info () = delete;
1249 };
1250
1251 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1252 struct dwarf2_cu *);
1253
1254 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1255 struct attribute *, struct attr_abbrev *,
1256 const gdb_byte *, bool *need_reprocess);
1257
1258 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1259 struct attribute *attr);
1260
1261 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1262
1263 static sect_offset read_abbrev_offset
1264 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1265 struct dwarf2_section_info *, sect_offset);
1266
1267 static const char *read_indirect_string
1268 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1269 const struct comp_unit_head *, unsigned int *);
1270
1271 static const char *read_indirect_string_at_offset
1272 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1273
1274 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1275 const gdb_byte *,
1276 unsigned int *);
1277
1278 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1279 ULONGEST str_index);
1280
1281 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1282 ULONGEST str_index);
1283
1284 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1285
1286 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1287 struct dwarf2_cu *);
1288
1289 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1290 struct dwarf2_cu *cu);
1291
1292 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1293
1294 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
1297 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1298
1299 static struct die_info *die_specification (struct die_info *die,
1300 struct dwarf2_cu **);
1301
1302 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1303 struct dwarf2_cu *cu);
1304
1305 static void dwarf_decode_lines (struct line_header *, const char *,
1306 struct dwarf2_cu *, dwarf2_psymtab *,
1307 CORE_ADDR, int decode_mapping);
1308
1309 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1310 const char *);
1311
1312 static struct symbol *new_symbol (struct die_info *, struct type *,
1313 struct dwarf2_cu *, struct symbol * = NULL);
1314
1315 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1316 struct dwarf2_cu *);
1317
1318 static void dwarf2_const_value_attr (const struct attribute *attr,
1319 struct type *type,
1320 const char *name,
1321 struct obstack *obstack,
1322 struct dwarf2_cu *cu, LONGEST *value,
1323 const gdb_byte **bytes,
1324 struct dwarf2_locexpr_baton **baton);
1325
1326 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1327
1328 static int need_gnat_info (struct dwarf2_cu *);
1329
1330 static struct type *die_descriptive_type (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static void set_descriptive_type (struct type *, struct die_info *,
1334 struct dwarf2_cu *);
1335
1336 static struct type *die_containing_type (struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1340 struct dwarf2_cu *);
1341
1342 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1343
1344 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1345
1346 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1347
1348 static char *typename_concat (struct obstack *obs, const char *prefix,
1349 const char *suffix, int physname,
1350 struct dwarf2_cu *cu);
1351
1352 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1361
1362 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1365 struct dwarf2_cu *, dwarf2_psymtab *);
1366
1367 /* Return the .debug_loclists section to use for cu. */
1368 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1369
1370 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1371 values. Keep the items ordered with increasing constraints compliance. */
1372 enum pc_bounds_kind
1373 {
1374 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1375 PC_BOUNDS_NOT_PRESENT,
1376
1377 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1378 were present but they do not form a valid range of PC addresses. */
1379 PC_BOUNDS_INVALID,
1380
1381 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1382 PC_BOUNDS_RANGES,
1383
1384 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1385 PC_BOUNDS_HIGH_LOW,
1386 };
1387
1388 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1389 CORE_ADDR *, CORE_ADDR *,
1390 struct dwarf2_cu *,
1391 dwarf2_psymtab *);
1392
1393 static void get_scope_pc_bounds (struct die_info *,
1394 CORE_ADDR *, CORE_ADDR *,
1395 struct dwarf2_cu *);
1396
1397 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1398 CORE_ADDR, struct dwarf2_cu *);
1399
1400 static void dwarf2_add_field (struct field_info *, struct die_info *,
1401 struct dwarf2_cu *);
1402
1403 static void dwarf2_attach_fields_to_type (struct field_info *,
1404 struct type *, struct dwarf2_cu *);
1405
1406 static void dwarf2_add_member_fn (struct field_info *,
1407 struct die_info *, struct type *,
1408 struct dwarf2_cu *);
1409
1410 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1411 struct type *,
1412 struct dwarf2_cu *);
1413
1414 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1415
1416 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1419
1420 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1421
1422 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1423
1424 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1425
1426 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1427
1428 static struct type *read_module_type (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
1431 static const char *namespace_name (struct die_info *die,
1432 int *is_anonymous, struct dwarf2_cu *);
1433
1434 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1435
1436 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1437 bool * = nullptr);
1438
1439 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1440 struct dwarf2_cu *);
1441
1442 static struct die_info *read_die_and_siblings_1
1443 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1444 struct die_info *);
1445
1446 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1447 const gdb_byte *info_ptr,
1448 const gdb_byte **new_info_ptr,
1449 struct die_info *parent);
1450
1451 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1452 struct die_info **, const gdb_byte *,
1453 int);
1454
1455 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1456 struct die_info **, const gdb_byte *);
1457
1458 static void process_die (struct die_info *, struct dwarf2_cu *);
1459
1460 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1461 struct objfile *);
1462
1463 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1464
1465 static const char *dwarf2_full_name (const char *name,
1466 struct die_info *die,
1467 struct dwarf2_cu *cu);
1468
1469 static const char *dwarf2_physname (const char *name, struct die_info *die,
1470 struct dwarf2_cu *cu);
1471
1472 static struct die_info *dwarf2_extension (struct die_info *die,
1473 struct dwarf2_cu **);
1474
1475 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1476
1477 static void dump_die_for_error (struct die_info *);
1478
1479 static void dump_die_1 (struct ui_file *, int level, int max_level,
1480 struct die_info *);
1481
1482 /*static*/ void dump_die (struct die_info *, int max_level);
1483
1484 static void store_in_ref_table (struct die_info *,
1485 struct dwarf2_cu *);
1486
1487 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1488 const struct attribute *,
1489 struct dwarf2_cu **);
1490
1491 static struct die_info *follow_die_ref (struct die_info *,
1492 const struct attribute *,
1493 struct dwarf2_cu **);
1494
1495 static struct die_info *follow_die_sig (struct die_info *,
1496 const struct attribute *,
1497 struct dwarf2_cu **);
1498
1499 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1500 struct dwarf2_cu *);
1501
1502 static struct type *get_DW_AT_signature_type (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu *);
1505
1506 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1507 dwarf2_per_objfile *per_objfile);
1508
1509 static void read_signatured_type (signatured_type *sig_type,
1510 dwarf2_per_objfile *per_objfile);
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 struct type *set_die_type (struct die_info *, struct type *,
1550 struct dwarf2_cu *);
1551
1552 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1557 dwarf2_per_objfile *per_objfile,
1558 bool skip_partial,
1559 enum language pretend_language);
1560
1561 static void process_full_comp_unit (dwarf2_cu *cu,
1562 enum language pretend_language);
1563
1564 static void process_full_type_unit (dwarf2_cu *cu,
1565 enum language pretend_language);
1566
1567 static void dwarf2_add_dependence (struct dwarf2_cu *,
1568 struct dwarf2_per_cu_data *);
1569
1570 static void dwarf2_mark (struct dwarf2_cu *);
1571
1572 static struct type *get_die_type_at_offset (sect_offset,
1573 dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile);
1575
1576 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1577
1578 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1579 dwarf2_per_objfile *per_objfile,
1580 enum language pretend_language);
1581
1582 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1583
1584 /* Class, the destructor of which frees all allocated queue entries. This
1585 will only have work to do if an error was thrown while processing the
1586 dwarf. If no error was thrown then the queue entries should have all
1587 been processed, and freed, as we went along. */
1588
1589 class dwarf2_queue_guard
1590 {
1591 public:
1592 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1593 : m_per_objfile (per_objfile)
1594 {
1595 }
1596
1597 /* Free any entries remaining on the queue. There should only be
1598 entries left if we hit an error while processing the dwarf. */
1599 ~dwarf2_queue_guard ()
1600 {
1601 /* Ensure that no memory is allocated by the queue. */
1602 std::queue<dwarf2_queue_item> empty;
1603 std::swap (m_per_objfile->per_bfd->queue, empty);
1604 }
1605
1606 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1607
1608 private:
1609 dwarf2_per_objfile *m_per_objfile;
1610 };
1611
1612 dwarf2_queue_item::~dwarf2_queue_item ()
1613 {
1614 /* Anything still marked queued is likely to be in an
1615 inconsistent state, so discard it. */
1616 if (per_cu->queued)
1617 {
1618 per_objfile->remove_cu (per_cu);
1619 per_cu->queued = 0;
1620 }
1621 }
1622
1623 /* The return type of find_file_and_directory. Note, the enclosed
1624 string pointers are only valid while this object is valid. */
1625
1626 struct file_and_directory
1627 {
1628 /* The filename. This is never NULL. */
1629 const char *name;
1630
1631 /* The compilation directory. NULL if not known. If we needed to
1632 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1633 points directly to the DW_AT_comp_dir string attribute owned by
1634 the obstack that owns the DIE. */
1635 const char *comp_dir;
1636
1637 /* If we needed to build a new string for comp_dir, this is what
1638 owns the storage. */
1639 std::string comp_dir_storage;
1640 };
1641
1642 static file_and_directory find_file_and_directory (struct die_info *die,
1643 struct dwarf2_cu *cu);
1644
1645 static htab_up allocate_signatured_type_table ();
1646
1647 static htab_up allocate_dwo_unit_table ();
1648
1649 static struct dwo_unit *lookup_dwo_unit_in_dwp
1650 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1651 struct dwp_file *dwp_file, const char *comp_dir,
1652 ULONGEST signature, int is_debug_types);
1653
1654 static struct dwp_file *get_dwp_file
1655 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1656
1657 static struct dwo_unit *lookup_dwo_comp_unit
1658 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1659 ULONGEST signature);
1660
1661 static struct dwo_unit *lookup_dwo_type_unit
1662 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1663
1664 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1665
1666 /* A unique pointer to a dwo_file. */
1667
1668 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1669
1670 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1671
1672 static void check_producer (struct dwarf2_cu *cu);
1673
1674 static void free_line_header_voidp (void *arg);
1675 \f
1676 /* Various complaints about symbol reading that don't abort the process. */
1677
1678 static void
1679 dwarf2_debug_line_missing_file_complaint (void)
1680 {
1681 complaint (_(".debug_line section has line data without a file"));
1682 }
1683
1684 static void
1685 dwarf2_debug_line_missing_end_sequence_complaint (void)
1686 {
1687 complaint (_(".debug_line section has line "
1688 "program sequence without an end"));
1689 }
1690
1691 static void
1692 dwarf2_complex_location_expr_complaint (void)
1693 {
1694 complaint (_("location expression too complex"));
1695 }
1696
1697 static void
1698 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1699 int arg3)
1700 {
1701 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1702 arg1, arg2, arg3);
1703 }
1704
1705 static void
1706 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1707 {
1708 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1709 arg1, arg2);
1710 }
1711
1712 /* Hash function for line_header_hash. */
1713
1714 static hashval_t
1715 line_header_hash (const struct line_header *ofs)
1716 {
1717 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1718 }
1719
1720 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1721
1722 static hashval_t
1723 line_header_hash_voidp (const void *item)
1724 {
1725 const struct line_header *ofs = (const struct line_header *) item;
1726
1727 return line_header_hash (ofs);
1728 }
1729
1730 /* Equality function for line_header_hash. */
1731
1732 static int
1733 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1734 {
1735 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1736 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1737
1738 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1739 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1740 }
1741
1742 \f
1743
1744 /* See declaration. */
1745
1746 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1747 bool can_copy_)
1748 : obfd (obfd),
1749 can_copy (can_copy_)
1750 {
1751 if (names == NULL)
1752 names = &dwarf2_elf_names;
1753
1754 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1755 locate_sections (obfd, sec, *names);
1756 }
1757
1758 dwarf2_per_bfd::~dwarf2_per_bfd ()
1759 {
1760 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1761 per_cu->imported_symtabs_free ();
1762
1763 for (signatured_type *sig_type : all_type_units)
1764 sig_type->per_cu.imported_symtabs_free ();
1765
1766 /* Everything else should be on this->obstack. */
1767 }
1768
1769 /* See read.h. */
1770
1771 void
1772 dwarf2_per_objfile::remove_all_cus ()
1773 {
1774 for (auto pair : m_dwarf2_cus)
1775 delete pair.second;
1776
1777 m_dwarf2_cus.clear ();
1778 }
1779
1780 /* A helper class that calls free_cached_comp_units on
1781 destruction. */
1782
1783 class free_cached_comp_units
1784 {
1785 public:
1786
1787 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1788 : m_per_objfile (per_objfile)
1789 {
1790 }
1791
1792 ~free_cached_comp_units ()
1793 {
1794 m_per_objfile->remove_all_cus ();
1795 }
1796
1797 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1798
1799 private:
1800
1801 dwarf2_per_objfile *m_per_objfile;
1802 };
1803
1804 /* See read.h. */
1805
1806 bool
1807 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1808 {
1809 gdb_assert (per_cu->index < this->m_symtabs.size ());
1810
1811 return this->m_symtabs[per_cu->index] != nullptr;
1812 }
1813
1814 /* See read.h. */
1815
1816 compunit_symtab *
1817 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1818 {
1819 gdb_assert (per_cu->index < this->m_symtabs.size ());
1820
1821 return this->m_symtabs[per_cu->index];
1822 }
1823
1824 /* See read.h. */
1825
1826 void
1827 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1828 compunit_symtab *symtab)
1829 {
1830 gdb_assert (per_cu->index < this->m_symtabs.size ());
1831 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1832
1833 this->m_symtabs[per_cu->index] = symtab;
1834 }
1835
1836 /* Try to locate the sections we need for DWARF 2 debugging
1837 information and return true if we have enough to do something.
1838 NAMES points to the dwarf2 section names, or is NULL if the standard
1839 ELF names are used. CAN_COPY is true for formats where symbol
1840 interposition is possible and so symbol values must follow copy
1841 relocation rules. */
1842
1843 int
1844 dwarf2_has_info (struct objfile *objfile,
1845 const struct dwarf2_debug_sections *names,
1846 bool can_copy)
1847 {
1848 if (objfile->flags & OBJF_READNEVER)
1849 return 0;
1850
1851 struct dwarf2_per_objfile *dwarf2_per_objfile
1852 = get_dwarf2_per_objfile (objfile);
1853
1854 if (dwarf2_per_objfile == NULL)
1855 {
1856 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1857 sharing yet). */
1858 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1859
1860 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1861 }
1862
1863 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1864 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1865 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1866 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1867 }
1868
1869 /* When loading sections, we look either for uncompressed section or for
1870 compressed section names. */
1871
1872 static int
1873 section_is_p (const char *section_name,
1874 const struct dwarf2_section_names *names)
1875 {
1876 if (names->normal != NULL
1877 && strcmp (section_name, names->normal) == 0)
1878 return 1;
1879 if (names->compressed != NULL
1880 && strcmp (section_name, names->compressed) == 0)
1881 return 1;
1882 return 0;
1883 }
1884
1885 /* See declaration. */
1886
1887 void
1888 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1889 const dwarf2_debug_sections &names)
1890 {
1891 flagword aflag = bfd_section_flags (sectp);
1892
1893 if ((aflag & SEC_HAS_CONTENTS) == 0)
1894 {
1895 }
1896 else if (elf_section_data (sectp)->this_hdr.sh_size
1897 > bfd_get_file_size (abfd))
1898 {
1899 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1900 warning (_("Discarding section %s which has a section size (%s"
1901 ") larger than the file size [in module %s]"),
1902 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1903 bfd_get_filename (abfd));
1904 }
1905 else if (section_is_p (sectp->name, &names.info))
1906 {
1907 this->info.s.section = sectp;
1908 this->info.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.abbrev))
1911 {
1912 this->abbrev.s.section = sectp;
1913 this->abbrev.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.line))
1916 {
1917 this->line.s.section = sectp;
1918 this->line.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.loc))
1921 {
1922 this->loc.s.section = sectp;
1923 this->loc.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.loclists))
1926 {
1927 this->loclists.s.section = sectp;
1928 this->loclists.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.macinfo))
1931 {
1932 this->macinfo.s.section = sectp;
1933 this->macinfo.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.macro))
1936 {
1937 this->macro.s.section = sectp;
1938 this->macro.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.str))
1941 {
1942 this->str.s.section = sectp;
1943 this->str.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.str_offsets))
1946 {
1947 this->str_offsets.s.section = sectp;
1948 this->str_offsets.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line_str))
1951 {
1952 this->line_str.s.section = sectp;
1953 this->line_str.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.addr))
1956 {
1957 this->addr.s.section = sectp;
1958 this->addr.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.frame))
1961 {
1962 this->frame.s.section = sectp;
1963 this->frame.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.eh_frame))
1966 {
1967 this->eh_frame.s.section = sectp;
1968 this->eh_frame.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.ranges))
1971 {
1972 this->ranges.s.section = sectp;
1973 this->ranges.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.rnglists))
1976 {
1977 this->rnglists.s.section = sectp;
1978 this->rnglists.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.types))
1981 {
1982 struct dwarf2_section_info type_section;
1983
1984 memset (&type_section, 0, sizeof (type_section));
1985 type_section.s.section = sectp;
1986 type_section.size = bfd_section_size (sectp);
1987
1988 this->types.push_back (type_section);
1989 }
1990 else if (section_is_p (sectp->name, &names.gdb_index))
1991 {
1992 this->gdb_index.s.section = sectp;
1993 this->gdb_index.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.debug_names))
1996 {
1997 this->debug_names.s.section = sectp;
1998 this->debug_names.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_aranges))
2001 {
2002 this->debug_aranges.s.section = sectp;
2003 this->debug_aranges.size = bfd_section_size (sectp);
2004 }
2005
2006 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2007 && bfd_section_vma (sectp) == 0)
2008 this->has_section_at_zero = true;
2009 }
2010
2011 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2012 SECTION_NAME. */
2013
2014 void
2015 dwarf2_get_section_info (struct objfile *objfile,
2016 enum dwarf2_section_enum sect,
2017 asection **sectp, const gdb_byte **bufp,
2018 bfd_size_type *sizep)
2019 {
2020 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2021 struct dwarf2_section_info *info;
2022
2023 /* We may see an objfile without any DWARF, in which case we just
2024 return nothing. */
2025 if (data == NULL)
2026 {
2027 *sectp = NULL;
2028 *bufp = NULL;
2029 *sizep = 0;
2030 return;
2031 }
2032 switch (sect)
2033 {
2034 case DWARF2_DEBUG_FRAME:
2035 info = &data->per_bfd->frame;
2036 break;
2037 case DWARF2_EH_FRAME:
2038 info = &data->per_bfd->eh_frame;
2039 break;
2040 default:
2041 gdb_assert_not_reached ("unexpected section");
2042 }
2043
2044 info->read (objfile);
2045
2046 *sectp = info->get_bfd_section ();
2047 *bufp = info->buffer;
2048 *sizep = info->size;
2049 }
2050
2051 /* A helper function to find the sections for a .dwz file. */
2052
2053 static void
2054 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2055 {
2056 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2057
2058 /* Note that we only support the standard ELF names, because .dwz
2059 is ELF-only (at the time of writing). */
2060 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2061 {
2062 dwz_file->abbrev.s.section = sectp;
2063 dwz_file->abbrev.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2066 {
2067 dwz_file->info.s.section = sectp;
2068 dwz_file->info.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2071 {
2072 dwz_file->str.s.section = sectp;
2073 dwz_file->str.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2076 {
2077 dwz_file->line.s.section = sectp;
2078 dwz_file->line.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2081 {
2082 dwz_file->macro.s.section = sectp;
2083 dwz_file->macro.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2086 {
2087 dwz_file->gdb_index.s.section = sectp;
2088 dwz_file->gdb_index.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2091 {
2092 dwz_file->debug_names.s.section = sectp;
2093 dwz_file->debug_names.size = bfd_section_size (sectp);
2094 }
2095 }
2096
2097 /* See dwarf2read.h. */
2098
2099 struct dwz_file *
2100 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2101 {
2102 const char *filename;
2103 bfd_size_type buildid_len_arg;
2104 size_t buildid_len;
2105 bfd_byte *buildid;
2106
2107 if (per_bfd->dwz_file != NULL)
2108 return per_bfd->dwz_file.get ();
2109
2110 bfd_set_error (bfd_error_no_error);
2111 gdb::unique_xmalloc_ptr<char> data
2112 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2113 &buildid_len_arg, &buildid));
2114 if (data == NULL)
2115 {
2116 if (bfd_get_error () == bfd_error_no_error)
2117 return NULL;
2118 error (_("could not read '.gnu_debugaltlink' section: %s"),
2119 bfd_errmsg (bfd_get_error ()));
2120 }
2121
2122 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2123
2124 buildid_len = (size_t) buildid_len_arg;
2125
2126 filename = data.get ();
2127
2128 std::string abs_storage;
2129 if (!IS_ABSOLUTE_PATH (filename))
2130 {
2131 gdb::unique_xmalloc_ptr<char> abs
2132 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2133
2134 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2135 filename = abs_storage.c_str ();
2136 }
2137
2138 /* First try the file name given in the section. If that doesn't
2139 work, try to use the build-id instead. */
2140 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2141 if (dwz_bfd != NULL)
2142 {
2143 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2144 dwz_bfd.reset (nullptr);
2145 }
2146
2147 if (dwz_bfd == NULL)
2148 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2149
2150 if (dwz_bfd == nullptr)
2151 {
2152 gdb::unique_xmalloc_ptr<char> alt_filename;
2153 const char *origname = bfd_get_filename (per_bfd->obfd);
2154
2155 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2156 buildid_len,
2157 origname,
2158 &alt_filename));
2159
2160 if (fd.get () >= 0)
2161 {
2162 /* File successfully retrieved from server. */
2163 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2164
2165 if (dwz_bfd == nullptr)
2166 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2167 alt_filename.get ());
2168 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2169 dwz_bfd.reset (nullptr);
2170 }
2171 }
2172
2173 if (dwz_bfd == NULL)
2174 error (_("could not find '.gnu_debugaltlink' file for %s"),
2175 bfd_get_filename (per_bfd->obfd));
2176
2177 std::unique_ptr<struct dwz_file> result
2178 (new struct dwz_file (std::move (dwz_bfd)));
2179
2180 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2181 result.get ());
2182
2183 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2184 per_bfd->dwz_file = std::move (result);
2185 return per_bfd->dwz_file.get ();
2186 }
2187 \f
2188 /* DWARF quick_symbols_functions support. */
2189
2190 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2191 unique line tables, so we maintain a separate table of all .debug_line
2192 derived entries to support the sharing.
2193 All the quick functions need is the list of file names. We discard the
2194 line_header when we're done and don't need to record it here. */
2195 struct quick_file_names
2196 {
2197 /* The data used to construct the hash key. */
2198 struct stmt_list_hash hash;
2199
2200 /* The number of entries in file_names, real_names. */
2201 unsigned int num_file_names;
2202
2203 /* The file names from the line table, after being run through
2204 file_full_name. */
2205 const char **file_names;
2206
2207 /* The file names from the line table after being run through
2208 gdb_realpath. These are computed lazily. */
2209 const char **real_names;
2210 };
2211
2212 /* When using the index (and thus not using psymtabs), each CU has an
2213 object of this type. This is used to hold information needed by
2214 the various "quick" methods. */
2215 struct dwarf2_per_cu_quick_data
2216 {
2217 /* The file table. This can be NULL if there was no file table
2218 or it's currently not read in.
2219 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2220 struct quick_file_names *file_names;
2221
2222 /* A temporary mark bit used when iterating over all CUs in
2223 expand_symtabs_matching. */
2224 unsigned int mark : 1;
2225
2226 /* True if we've tried to read the file table and found there isn't one.
2227 There will be no point in trying to read it again next time. */
2228 unsigned int no_file_data : 1;
2229 };
2230
2231 /* Utility hash function for a stmt_list_hash. */
2232
2233 static hashval_t
2234 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2235 {
2236 hashval_t v = 0;
2237
2238 if (stmt_list_hash->dwo_unit != NULL)
2239 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2240 v += to_underlying (stmt_list_hash->line_sect_off);
2241 return v;
2242 }
2243
2244 /* Utility equality function for a stmt_list_hash. */
2245
2246 static int
2247 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2248 const struct stmt_list_hash *rhs)
2249 {
2250 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2251 return 0;
2252 if (lhs->dwo_unit != NULL
2253 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2254 return 0;
2255
2256 return lhs->line_sect_off == rhs->line_sect_off;
2257 }
2258
2259 /* Hash function for a quick_file_names. */
2260
2261 static hashval_t
2262 hash_file_name_entry (const void *e)
2263 {
2264 const struct quick_file_names *file_data
2265 = (const struct quick_file_names *) e;
2266
2267 return hash_stmt_list_entry (&file_data->hash);
2268 }
2269
2270 /* Equality function for a quick_file_names. */
2271
2272 static int
2273 eq_file_name_entry (const void *a, const void *b)
2274 {
2275 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2276 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2277
2278 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2279 }
2280
2281 /* Delete function for a quick_file_names. */
2282
2283 static void
2284 delete_file_name_entry (void *e)
2285 {
2286 struct quick_file_names *file_data = (struct quick_file_names *) e;
2287 int i;
2288
2289 for (i = 0; i < file_data->num_file_names; ++i)
2290 {
2291 xfree ((void*) file_data->file_names[i]);
2292 if (file_data->real_names)
2293 xfree ((void*) file_data->real_names[i]);
2294 }
2295
2296 /* The space for the struct itself lives on the obstack, so we don't
2297 free it here. */
2298 }
2299
2300 /* Create a quick_file_names hash table. */
2301
2302 static htab_up
2303 create_quick_file_names_table (unsigned int nr_initial_entries)
2304 {
2305 return htab_up (htab_create_alloc (nr_initial_entries,
2306 hash_file_name_entry, eq_file_name_entry,
2307 delete_file_name_entry, xcalloc, xfree));
2308 }
2309
2310 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2311 function is unrelated to symtabs, symtab would have to be created afterwards.
2312 You should call age_cached_comp_units after processing the CU. */
2313
2314 static dwarf2_cu *
2315 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2316 bool skip_partial)
2317 {
2318 if (per_cu->is_debug_types)
2319 load_full_type_unit (per_cu, per_objfile);
2320 else
2321 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2322
2323 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2324 if (cu == nullptr)
2325 return nullptr; /* Dummy CU. */
2326
2327 dwarf2_find_base_address (cu->dies, cu);
2328
2329 return cu;
2330 }
2331
2332 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2333
2334 static void
2335 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2336 dwarf2_per_objfile *dwarf2_per_objfile,
2337 bool skip_partial)
2338 {
2339 /* Skip type_unit_groups, reading the type units they contain
2340 is handled elsewhere. */
2341 if (per_cu->type_unit_group_p ())
2342 return;
2343
2344 /* The destructor of dwarf2_queue_guard frees any entries left on
2345 the queue. After this point we're guaranteed to leave this function
2346 with the dwarf queue empty. */
2347 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2348
2349 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2350 {
2351 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2352 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2353
2354 /* If we just loaded a CU from a DWO, and we're working with an index
2355 that may badly handle TUs, load all the TUs in that DWO as well.
2356 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2357 if (!per_cu->is_debug_types
2358 && cu != NULL
2359 && cu->dwo_unit != NULL
2360 && dwarf2_per_objfile->per_bfd->index_table != NULL
2361 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2362 /* DWP files aren't supported yet. */
2363 && get_dwp_file (dwarf2_per_objfile) == NULL)
2364 queue_and_load_all_dwo_tus (cu);
2365 }
2366
2367 process_queue (dwarf2_per_objfile);
2368
2369 /* Age the cache, releasing compilation units that have not
2370 been used recently. */
2371 dwarf2_per_objfile->age_comp_units ();
2372 }
2373
2374 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2375 the per-objfile for which this symtab is instantiated.
2376
2377 Returns the resulting symbol table. */
2378
2379 static struct compunit_symtab *
2380 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2381 dwarf2_per_objfile *dwarf2_per_objfile,
2382 bool skip_partial)
2383 {
2384 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2385
2386 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2387 {
2388 free_cached_comp_units freer (dwarf2_per_objfile);
2389 scoped_restore decrementer = increment_reading_symtab ();
2390 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2391 process_cu_includes (dwarf2_per_objfile);
2392 }
2393
2394 return dwarf2_per_objfile->get_symtab (per_cu);
2395 }
2396
2397 /* See declaration. */
2398
2399 dwarf2_per_cu_data *
2400 dwarf2_per_bfd::get_cutu (int index)
2401 {
2402 if (index >= this->all_comp_units.size ())
2403 {
2404 index -= this->all_comp_units.size ();
2405 gdb_assert (index < this->all_type_units.size ());
2406 return &this->all_type_units[index]->per_cu;
2407 }
2408
2409 return this->all_comp_units[index];
2410 }
2411
2412 /* See declaration. */
2413
2414 dwarf2_per_cu_data *
2415 dwarf2_per_bfd::get_cu (int index)
2416 {
2417 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2418
2419 return this->all_comp_units[index];
2420 }
2421
2422 /* See declaration. */
2423
2424 signatured_type *
2425 dwarf2_per_bfd::get_tu (int index)
2426 {
2427 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2428
2429 return this->all_type_units[index];
2430 }
2431
2432 /* See read.h. */
2433
2434 dwarf2_per_cu_data *
2435 dwarf2_per_bfd::allocate_per_cu ()
2436 {
2437 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2438 result->per_bfd = this;
2439 result->index = m_num_psymtabs++;
2440 return result;
2441 }
2442
2443 /* See read.h. */
2444
2445 signatured_type *
2446 dwarf2_per_bfd::allocate_signatured_type ()
2447 {
2448 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2449 result->per_cu.per_bfd = this;
2450 result->per_cu.index = m_num_psymtabs++;
2451 return result;
2452 }
2453
2454 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2455 obstack, and constructed with the specified field values. */
2456
2457 static dwarf2_per_cu_data *
2458 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2459 struct dwarf2_section_info *section,
2460 int is_dwz,
2461 sect_offset sect_off, ULONGEST length)
2462 {
2463 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2464 the_cu->sect_off = sect_off;
2465 the_cu->length = length;
2466 the_cu->section = section;
2467 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2468 struct dwarf2_per_cu_quick_data);
2469 the_cu->is_dwz = is_dwz;
2470 return the_cu;
2471 }
2472
2473 /* A helper for create_cus_from_index that handles a given list of
2474 CUs. */
2475
2476 static void
2477 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2478 const gdb_byte *cu_list, offset_type n_elements,
2479 struct dwarf2_section_info *section,
2480 int is_dwz)
2481 {
2482 for (offset_type i = 0; i < n_elements; i += 2)
2483 {
2484 gdb_static_assert (sizeof (ULONGEST) >= 8);
2485
2486 sect_offset sect_off
2487 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2488 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2489 cu_list += 2 * 8;
2490
2491 dwarf2_per_cu_data *per_cu
2492 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2493 length);
2494 per_bfd->all_comp_units.push_back (per_cu);
2495 }
2496 }
2497
2498 /* Read the CU list from the mapped index, and use it to create all
2499 the CU objects for PER_BFD. */
2500
2501 static void
2502 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2503 const gdb_byte *cu_list, offset_type cu_list_elements,
2504 const gdb_byte *dwz_list, offset_type dwz_elements)
2505 {
2506 gdb_assert (per_bfd->all_comp_units.empty ());
2507 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2508
2509 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2510 &per_bfd->info, 0);
2511
2512 if (dwz_elements == 0)
2513 return;
2514
2515 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2516 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2517 &dwz->info, 1);
2518 }
2519
2520 /* Create the signatured type hash table from the index. */
2521
2522 static void
2523 create_signatured_type_table_from_index
2524 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2525 const gdb_byte *bytes, offset_type elements)
2526 {
2527 gdb_assert (per_bfd->all_type_units.empty ());
2528 per_bfd->all_type_units.reserve (elements / 3);
2529
2530 htab_up sig_types_hash = allocate_signatured_type_table ();
2531
2532 for (offset_type i = 0; i < elements; i += 3)
2533 {
2534 struct signatured_type *sig_type;
2535 ULONGEST signature;
2536 void **slot;
2537 cu_offset type_offset_in_tu;
2538
2539 gdb_static_assert (sizeof (ULONGEST) >= 8);
2540 sect_offset sect_off
2541 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2542 type_offset_in_tu
2543 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2544 BFD_ENDIAN_LITTLE);
2545 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2546 bytes += 3 * 8;
2547
2548 sig_type = per_bfd->allocate_signatured_type ();
2549 sig_type->signature = signature;
2550 sig_type->type_offset_in_tu = type_offset_in_tu;
2551 sig_type->per_cu.is_debug_types = 1;
2552 sig_type->per_cu.section = section;
2553 sig_type->per_cu.sect_off = sect_off;
2554 sig_type->per_cu.v.quick
2555 = OBSTACK_ZALLOC (&per_bfd->obstack,
2556 struct dwarf2_per_cu_quick_data);
2557
2558 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2559 *slot = sig_type;
2560
2561 per_bfd->all_type_units.push_back (sig_type);
2562 }
2563
2564 per_bfd->signatured_types = std::move (sig_types_hash);
2565 }
2566
2567 /* Create the signatured type hash table from .debug_names. */
2568
2569 static void
2570 create_signatured_type_table_from_debug_names
2571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2572 const mapped_debug_names &map,
2573 struct dwarf2_section_info *section,
2574 struct dwarf2_section_info *abbrev_section)
2575 {
2576 struct objfile *objfile = dwarf2_per_objfile->objfile;
2577
2578 section->read (objfile);
2579 abbrev_section->read (objfile);
2580
2581 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2582 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2583
2584 htab_up sig_types_hash = allocate_signatured_type_table ();
2585
2586 for (uint32_t i = 0; i < map.tu_count; ++i)
2587 {
2588 struct signatured_type *sig_type;
2589 void **slot;
2590
2591 sect_offset sect_off
2592 = (sect_offset) (extract_unsigned_integer
2593 (map.tu_table_reordered + i * map.offset_size,
2594 map.offset_size,
2595 map.dwarf5_byte_order));
2596
2597 comp_unit_head cu_header;
2598 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2599 abbrev_section,
2600 section->buffer + to_underlying (sect_off),
2601 rcuh_kind::TYPE);
2602
2603 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2604 sig_type->signature = cu_header.signature;
2605 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2606 sig_type->per_cu.is_debug_types = 1;
2607 sig_type->per_cu.section = section;
2608 sig_type->per_cu.sect_off = sect_off;
2609 sig_type->per_cu.v.quick
2610 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2611 struct dwarf2_per_cu_quick_data);
2612
2613 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2614 *slot = sig_type;
2615
2616 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2617 }
2618
2619 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2620 }
2621
2622 /* Read the address map data from the mapped index, and use it to
2623 populate the objfile's psymtabs_addrmap. */
2624
2625 static void
2626 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2627 struct mapped_index *index)
2628 {
2629 struct objfile *objfile = dwarf2_per_objfile->objfile;
2630 struct gdbarch *gdbarch = objfile->arch ();
2631 const gdb_byte *iter, *end;
2632 struct addrmap *mutable_map;
2633 CORE_ADDR baseaddr;
2634
2635 auto_obstack temp_obstack;
2636
2637 mutable_map = addrmap_create_mutable (&temp_obstack);
2638
2639 iter = index->address_table.data ();
2640 end = iter + index->address_table.size ();
2641
2642 baseaddr = objfile->text_section_offset ();
2643
2644 while (iter < end)
2645 {
2646 ULONGEST hi, lo, cu_index;
2647 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2648 iter += 8;
2649 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2650 iter += 8;
2651 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2652 iter += 4;
2653
2654 if (lo > hi)
2655 {
2656 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2657 hex_string (lo), hex_string (hi));
2658 continue;
2659 }
2660
2661 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2662 {
2663 complaint (_(".gdb_index address table has invalid CU number %u"),
2664 (unsigned) cu_index);
2665 continue;
2666 }
2667
2668 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2669 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2670 addrmap_set_empty (mutable_map, lo, hi - 1,
2671 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2672 }
2673
2674 objfile->partial_symtabs->psymtabs_addrmap
2675 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2676 }
2677
2678 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2679 populate the objfile's psymtabs_addrmap. */
2680
2681 static void
2682 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2683 struct dwarf2_section_info *section)
2684 {
2685 struct objfile *objfile = dwarf2_per_objfile->objfile;
2686 bfd *abfd = objfile->obfd;
2687 struct gdbarch *gdbarch = objfile->arch ();
2688 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2689
2690 auto_obstack temp_obstack;
2691 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2692
2693 std::unordered_map<sect_offset,
2694 dwarf2_per_cu_data *,
2695 gdb::hash_enum<sect_offset>>
2696 debug_info_offset_to_per_cu;
2697 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2698 {
2699 const auto insertpair
2700 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2701 if (!insertpair.second)
2702 {
2703 warning (_("Section .debug_aranges in %s has duplicate "
2704 "debug_info_offset %s, ignoring .debug_aranges."),
2705 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2706 return;
2707 }
2708 }
2709
2710 section->read (objfile);
2711
2712 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2713
2714 const gdb_byte *addr = section->buffer;
2715
2716 while (addr < section->buffer + section->size)
2717 {
2718 const gdb_byte *const entry_addr = addr;
2719 unsigned int bytes_read;
2720
2721 const LONGEST entry_length = read_initial_length (abfd, addr,
2722 &bytes_read);
2723 addr += bytes_read;
2724
2725 const gdb_byte *const entry_end = addr + entry_length;
2726 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2727 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2728 if (addr + entry_length > section->buffer + section->size)
2729 {
2730 warning (_("Section .debug_aranges in %s entry at offset %s "
2731 "length %s exceeds section length %s, "
2732 "ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
2735 plongest (bytes_read + entry_length),
2736 pulongest (section->size));
2737 return;
2738 }
2739
2740 /* The version number. */
2741 const uint16_t version = read_2_bytes (abfd, addr);
2742 addr += 2;
2743 if (version != 2)
2744 {
2745 warning (_("Section .debug_aranges in %s entry at offset %s "
2746 "has unsupported version %d, ignoring .debug_aranges."),
2747 objfile_name (objfile),
2748 plongest (entry_addr - section->buffer), version);
2749 return;
2750 }
2751
2752 const uint64_t debug_info_offset
2753 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2754 addr += offset_size;
2755 const auto per_cu_it
2756 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2757 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2758 {
2759 warning (_("Section .debug_aranges in %s entry at offset %s "
2760 "debug_info_offset %s does not exists, "
2761 "ignoring .debug_aranges."),
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
2764 pulongest (debug_info_offset));
2765 return;
2766 }
2767 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2768
2769 const uint8_t address_size = *addr++;
2770 if (address_size < 1 || address_size > 8)
2771 {
2772 warning (_("Section .debug_aranges in %s entry at offset %s "
2773 "address_size %u is invalid, ignoring .debug_aranges."),
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer), address_size);
2776 return;
2777 }
2778
2779 const uint8_t segment_selector_size = *addr++;
2780 if (segment_selector_size != 0)
2781 {
2782 warning (_("Section .debug_aranges in %s entry at offset %s "
2783 "segment_selector_size %u is not supported, "
2784 "ignoring .debug_aranges."),
2785 objfile_name (objfile),
2786 plongest (entry_addr - section->buffer),
2787 segment_selector_size);
2788 return;
2789 }
2790
2791 /* Must pad to an alignment boundary that is twice the address
2792 size. It is undocumented by the DWARF standard but GCC does
2793 use it. */
2794 for (size_t padding = ((-(addr - section->buffer))
2795 & (2 * address_size - 1));
2796 padding > 0; padding--)
2797 if (*addr++ != 0)
2798 {
2799 warning (_("Section .debug_aranges in %s entry at offset %s "
2800 "padding is not zero, ignoring .debug_aranges."),
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer));
2803 return;
2804 }
2805
2806 for (;;)
2807 {
2808 if (addr + 2 * address_size > entry_end)
2809 {
2810 warning (_("Section .debug_aranges in %s entry at offset %s "
2811 "address list is not properly terminated, "
2812 "ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer));
2815 return;
2816 }
2817 ULONGEST start = extract_unsigned_integer (addr, address_size,
2818 dwarf5_byte_order);
2819 addr += address_size;
2820 ULONGEST length = extract_unsigned_integer (addr, address_size,
2821 dwarf5_byte_order);
2822 addr += address_size;
2823 if (start == 0 && length == 0)
2824 break;
2825 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2826 {
2827 /* Symbol was eliminated due to a COMDAT group. */
2828 continue;
2829 }
2830 ULONGEST end = start + length;
2831 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2832 - baseaddr);
2833 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2834 - baseaddr);
2835 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2836 }
2837 }
2838
2839 objfile->partial_symtabs->psymtabs_addrmap
2840 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2841 }
2842
2843 /* Find a slot in the mapped index INDEX for the object named NAME.
2844 If NAME is found, set *VEC_OUT to point to the CU vector in the
2845 constant pool and return true. If NAME cannot be found, return
2846 false. */
2847
2848 static bool
2849 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2850 offset_type **vec_out)
2851 {
2852 offset_type hash;
2853 offset_type slot, step;
2854 int (*cmp) (const char *, const char *);
2855
2856 gdb::unique_xmalloc_ptr<char> without_params;
2857 if (current_language->la_language == language_cplus
2858 || current_language->la_language == language_fortran
2859 || current_language->la_language == language_d)
2860 {
2861 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2862 not contain any. */
2863
2864 if (strchr (name, '(') != NULL)
2865 {
2866 without_params = cp_remove_params (name);
2867
2868 if (without_params != NULL)
2869 name = without_params.get ();
2870 }
2871 }
2872
2873 /* Index version 4 did not support case insensitive searches. But the
2874 indices for case insensitive languages are built in lowercase, therefore
2875 simulate our NAME being searched is also lowercased. */
2876 hash = mapped_index_string_hash ((index->version == 4
2877 && case_sensitivity == case_sensitive_off
2878 ? 5 : index->version),
2879 name);
2880
2881 slot = hash & (index->symbol_table.size () - 1);
2882 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2883 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2884
2885 for (;;)
2886 {
2887 const char *str;
2888
2889 const auto &bucket = index->symbol_table[slot];
2890 if (bucket.name == 0 && bucket.vec == 0)
2891 return false;
2892
2893 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2894 if (!cmp (name, str))
2895 {
2896 *vec_out = (offset_type *) (index->constant_pool
2897 + MAYBE_SWAP (bucket.vec));
2898 return true;
2899 }
2900
2901 slot = (slot + step) & (index->symbol_table.size () - 1);
2902 }
2903 }
2904
2905 /* A helper function that reads the .gdb_index from BUFFER and fills
2906 in MAP. FILENAME is the name of the file containing the data;
2907 it is used for error reporting. DEPRECATED_OK is true if it is
2908 ok to use deprecated sections.
2909
2910 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2911 out parameters that are filled in with information about the CU and
2912 TU lists in the section.
2913
2914 Returns true if all went well, false otherwise. */
2915
2916 static bool
2917 read_gdb_index_from_buffer (const char *filename,
2918 bool deprecated_ok,
2919 gdb::array_view<const gdb_byte> buffer,
2920 struct mapped_index *map,
2921 const gdb_byte **cu_list,
2922 offset_type *cu_list_elements,
2923 const gdb_byte **types_list,
2924 offset_type *types_list_elements)
2925 {
2926 const gdb_byte *addr = &buffer[0];
2927
2928 /* Version check. */
2929 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2930 /* Versions earlier than 3 emitted every copy of a psymbol. This
2931 causes the index to behave very poorly for certain requests. Version 3
2932 contained incomplete addrmap. So, it seems better to just ignore such
2933 indices. */
2934 if (version < 4)
2935 {
2936 static int warning_printed = 0;
2937 if (!warning_printed)
2938 {
2939 warning (_("Skipping obsolete .gdb_index section in %s."),
2940 filename);
2941 warning_printed = 1;
2942 }
2943 return 0;
2944 }
2945 /* Index version 4 uses a different hash function than index version
2946 5 and later.
2947
2948 Versions earlier than 6 did not emit psymbols for inlined
2949 functions. Using these files will cause GDB not to be able to
2950 set breakpoints on inlined functions by name, so we ignore these
2951 indices unless the user has done
2952 "set use-deprecated-index-sections on". */
2953 if (version < 6 && !deprecated_ok)
2954 {
2955 static int warning_printed = 0;
2956 if (!warning_printed)
2957 {
2958 warning (_("\
2959 Skipping deprecated .gdb_index section in %s.\n\
2960 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2961 to use the section anyway."),
2962 filename);
2963 warning_printed = 1;
2964 }
2965 return 0;
2966 }
2967 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2968 of the TU (for symbols coming from TUs),
2969 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2970 Plus gold-generated indices can have duplicate entries for global symbols,
2971 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2972 These are just performance bugs, and we can't distinguish gdb-generated
2973 indices from gold-generated ones, so issue no warning here. */
2974
2975 /* Indexes with higher version than the one supported by GDB may be no
2976 longer backward compatible. */
2977 if (version > 8)
2978 return 0;
2979
2980 map->version = version;
2981
2982 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2983
2984 int i = 0;
2985 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2986 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2987 / 8);
2988 ++i;
2989
2990 *types_list = addr + MAYBE_SWAP (metadata[i]);
2991 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2992 - MAYBE_SWAP (metadata[i]))
2993 / 8);
2994 ++i;
2995
2996 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2997 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2998 map->address_table
2999 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3000 ++i;
3001
3002 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3003 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3004 map->symbol_table
3005 = gdb::array_view<mapped_index::symbol_table_slot>
3006 ((mapped_index::symbol_table_slot *) symbol_table,
3007 (mapped_index::symbol_table_slot *) symbol_table_end);
3008
3009 ++i;
3010 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3011
3012 return 1;
3013 }
3014
3015 /* Callback types for dwarf2_read_gdb_index. */
3016
3017 typedef gdb::function_view
3018 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3019 get_gdb_index_contents_ftype;
3020 typedef gdb::function_view
3021 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3022 get_gdb_index_contents_dwz_ftype;
3023
3024 /* Read .gdb_index. If everything went ok, initialize the "quick"
3025 elements of all the CUs and return 1. Otherwise, return 0. */
3026
3027 static int
3028 dwarf2_read_gdb_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 get_gdb_index_contents_ftype get_gdb_index_contents,
3031 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3032 {
3033 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3034 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3035 struct dwz_file *dwz;
3036 struct objfile *objfile = dwarf2_per_objfile->objfile;
3037
3038 gdb::array_view<const gdb_byte> main_index_contents
3039 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3040
3041 if (main_index_contents.empty ())
3042 return 0;
3043
3044 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3045 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3046 use_deprecated_index_sections,
3047 main_index_contents, map.get (), &cu_list,
3048 &cu_list_elements, &types_list,
3049 &types_list_elements))
3050 return 0;
3051
3052 /* Don't use the index if it's empty. */
3053 if (map->symbol_table.empty ())
3054 return 0;
3055
3056 /* If there is a .dwz file, read it so we can get its CU list as
3057 well. */
3058 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3059 if (dwz != NULL)
3060 {
3061 struct mapped_index dwz_map;
3062 const gdb_byte *dwz_types_ignore;
3063 offset_type dwz_types_elements_ignore;
3064
3065 gdb::array_view<const gdb_byte> dwz_index_content
3066 = get_gdb_index_contents_dwz (objfile, dwz);
3067
3068 if (dwz_index_content.empty ())
3069 return 0;
3070
3071 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3072 1, dwz_index_content, &dwz_map,
3073 &dwz_list, &dwz_list_elements,
3074 &dwz_types_ignore,
3075 &dwz_types_elements_ignore))
3076 {
3077 warning (_("could not read '.gdb_index' section from %s; skipping"),
3078 bfd_get_filename (dwz->dwz_bfd.get ()));
3079 return 0;
3080 }
3081 }
3082
3083 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
3084 dwz_list, dwz_list_elements);
3085
3086 if (types_list_elements)
3087 {
3088 /* We can only handle a single .debug_types when we have an
3089 index. */
3090 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3091 return 0;
3092
3093 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3094
3095 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3096 section, types_list,
3097 types_list_elements);
3098 }
3099
3100 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3101
3102 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3103 dwarf2_per_objfile->per_bfd->using_index = 1;
3104 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3105 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3106
3107 return 1;
3108 }
3109
3110 /* die_reader_func for dw2_get_file_names. */
3111
3112 static void
3113 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3114 const gdb_byte *info_ptr,
3115 struct die_info *comp_unit_die)
3116 {
3117 struct dwarf2_cu *cu = reader->cu;
3118 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3119 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3120 struct dwarf2_per_cu_data *lh_cu;
3121 struct attribute *attr;
3122 void **slot;
3123 struct quick_file_names *qfn;
3124
3125 gdb_assert (! this_cu->is_debug_types);
3126
3127 /* Our callers never want to match partial units -- instead they
3128 will match the enclosing full CU. */
3129 if (comp_unit_die->tag == DW_TAG_partial_unit)
3130 {
3131 this_cu->v.quick->no_file_data = 1;
3132 return;
3133 }
3134
3135 lh_cu = this_cu;
3136 slot = NULL;
3137
3138 line_header_up lh;
3139 sect_offset line_offset {};
3140
3141 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3142 if (attr != nullptr)
3143 {
3144 struct quick_file_names find_entry;
3145
3146 line_offset = (sect_offset) DW_UNSND (attr);
3147
3148 /* We may have already read in this line header (TU line header sharing).
3149 If we have we're done. */
3150 find_entry.hash.dwo_unit = cu->dwo_unit;
3151 find_entry.hash.line_sect_off = line_offset;
3152 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3153 &find_entry, INSERT);
3154 if (*slot != NULL)
3155 {
3156 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3157 return;
3158 }
3159
3160 lh = dwarf_decode_line_header (line_offset, cu);
3161 }
3162 if (lh == NULL)
3163 {
3164 lh_cu->v.quick->no_file_data = 1;
3165 return;
3166 }
3167
3168 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3169 qfn->hash.dwo_unit = cu->dwo_unit;
3170 qfn->hash.line_sect_off = line_offset;
3171 gdb_assert (slot != NULL);
3172 *slot = qfn;
3173
3174 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3175
3176 int offset = 0;
3177 if (strcmp (fnd.name, "<unknown>") != 0)
3178 ++offset;
3179
3180 qfn->num_file_names = offset + lh->file_names_size ();
3181 qfn->file_names =
3182 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3183 qfn->num_file_names);
3184 if (offset != 0)
3185 qfn->file_names[0] = xstrdup (fnd.name);
3186 for (int i = 0; i < lh->file_names_size (); ++i)
3187 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3188 fnd.comp_dir).release ();
3189 qfn->real_names = NULL;
3190
3191 lh_cu->v.quick->file_names = qfn;
3192 }
3193
3194 /* A helper for the "quick" functions which attempts to read the line
3195 table for THIS_CU. */
3196
3197 static struct quick_file_names *
3198 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3199 dwarf2_per_objfile *per_objfile)
3200 {
3201 /* This should never be called for TUs. */
3202 gdb_assert (! this_cu->is_debug_types);
3203 /* Nor type unit groups. */
3204 gdb_assert (! this_cu->type_unit_group_p ());
3205
3206 if (this_cu->v.quick->file_names != NULL)
3207 return this_cu->v.quick->file_names;
3208 /* If we know there is no line data, no point in looking again. */
3209 if (this_cu->v.quick->no_file_data)
3210 return NULL;
3211
3212 cutu_reader reader (this_cu, per_objfile);
3213 if (!reader.dummy_p)
3214 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3215
3216 if (this_cu->v.quick->no_file_data)
3217 return NULL;
3218 return this_cu->v.quick->file_names;
3219 }
3220
3221 /* A helper for the "quick" functions which computes and caches the
3222 real path for a given file name from the line table. */
3223
3224 static const char *
3225 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3226 struct quick_file_names *qfn, int index)
3227 {
3228 if (qfn->real_names == NULL)
3229 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3230 qfn->num_file_names, const char *);
3231
3232 if (qfn->real_names[index] == NULL)
3233 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3234
3235 return qfn->real_names[index];
3236 }
3237
3238 static struct symtab *
3239 dw2_find_last_source_symtab (struct objfile *objfile)
3240 {
3241 struct dwarf2_per_objfile *dwarf2_per_objfile
3242 = get_dwarf2_per_objfile (objfile);
3243 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3244 compunit_symtab *cust
3245 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3246
3247 if (cust == NULL)
3248 return NULL;
3249
3250 return compunit_primary_filetab (cust);
3251 }
3252
3253 /* Traversal function for dw2_forget_cached_source_info. */
3254
3255 static int
3256 dw2_free_cached_file_names (void **slot, void *info)
3257 {
3258 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3259
3260 if (file_data->real_names)
3261 {
3262 int i;
3263
3264 for (i = 0; i < file_data->num_file_names; ++i)
3265 {
3266 xfree ((void*) file_data->real_names[i]);
3267 file_data->real_names[i] = NULL;
3268 }
3269 }
3270
3271 return 1;
3272 }
3273
3274 static void
3275 dw2_forget_cached_source_info (struct objfile *objfile)
3276 {
3277 struct dwarf2_per_objfile *dwarf2_per_objfile
3278 = get_dwarf2_per_objfile (objfile);
3279
3280 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3281 dw2_free_cached_file_names, NULL);
3282 }
3283
3284 /* Helper function for dw2_map_symtabs_matching_filename that expands
3285 the symtabs and calls the iterator. */
3286
3287 static int
3288 dw2_map_expand_apply (struct objfile *objfile,
3289 struct dwarf2_per_cu_data *per_cu,
3290 const char *name, const char *real_path,
3291 gdb::function_view<bool (symtab *)> callback)
3292 {
3293 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3294
3295 /* Don't visit already-expanded CUs. */
3296 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3297 if (per_objfile->symtab_set_p (per_cu))
3298 return 0;
3299
3300 /* This may expand more than one symtab, and we want to iterate over
3301 all of them. */
3302 dw2_instantiate_symtab (per_cu, per_objfile, false);
3303
3304 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3305 last_made, callback);
3306 }
3307
3308 /* Implementation of the map_symtabs_matching_filename method. */
3309
3310 static bool
3311 dw2_map_symtabs_matching_filename
3312 (struct objfile *objfile, const char *name, const char *real_path,
3313 gdb::function_view<bool (symtab *)> callback)
3314 {
3315 const char *name_basename = lbasename (name);
3316 struct dwarf2_per_objfile *dwarf2_per_objfile
3317 = get_dwarf2_per_objfile (objfile);
3318
3319 /* The rule is CUs specify all the files, including those used by
3320 any TU, so there's no need to scan TUs here. */
3321
3322 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3323 {
3324 /* We only need to look at symtabs not already expanded. */
3325 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3326 continue;
3327
3328 quick_file_names *file_data
3329 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3330 if (file_data == NULL)
3331 continue;
3332
3333 for (int j = 0; j < file_data->num_file_names; ++j)
3334 {
3335 const char *this_name = file_data->file_names[j];
3336 const char *this_real_name;
3337
3338 if (compare_filenames_for_search (this_name, name))
3339 {
3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3341 callback))
3342 return true;
3343 continue;
3344 }
3345
3346 /* Before we invoke realpath, which can get expensive when many
3347 files are involved, do a quick comparison of the basenames. */
3348 if (! basenames_may_differ
3349 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3350 continue;
3351
3352 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3353 file_data, j);
3354 if (compare_filenames_for_search (this_real_name, name))
3355 {
3356 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3357 callback))
3358 return true;
3359 continue;
3360 }
3361
3362 if (real_path != NULL)
3363 {
3364 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3365 gdb_assert (IS_ABSOLUTE_PATH (name));
3366 if (this_real_name != NULL
3367 && FILENAME_CMP (real_path, this_real_name) == 0)
3368 {
3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3370 callback))
3371 return true;
3372 continue;
3373 }
3374 }
3375 }
3376 }
3377
3378 return false;
3379 }
3380
3381 /* Struct used to manage iterating over all CUs looking for a symbol. */
3382
3383 struct dw2_symtab_iterator
3384 {
3385 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3386 struct dwarf2_per_objfile *dwarf2_per_objfile;
3387 /* If set, only look for symbols that match that block. Valid values are
3388 GLOBAL_BLOCK and STATIC_BLOCK. */
3389 gdb::optional<block_enum> block_index;
3390 /* The kind of symbol we're looking for. */
3391 domain_enum domain;
3392 /* The list of CUs from the index entry of the symbol,
3393 or NULL if not found. */
3394 offset_type *vec;
3395 /* The next element in VEC to look at. */
3396 int next;
3397 /* The number of elements in VEC, or zero if there is no match. */
3398 int length;
3399 /* Have we seen a global version of the symbol?
3400 If so we can ignore all further global instances.
3401 This is to work around gold/15646, inefficient gold-generated
3402 indices. */
3403 int global_seen;
3404 };
3405
3406 /* Initialize the index symtab iterator ITER. */
3407
3408 static void
3409 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3410 struct dwarf2_per_objfile *dwarf2_per_objfile,
3411 gdb::optional<block_enum> block_index,
3412 domain_enum domain,
3413 const char *name)
3414 {
3415 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3416 iter->block_index = block_index;
3417 iter->domain = domain;
3418 iter->next = 0;
3419 iter->global_seen = 0;
3420
3421 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3422
3423 /* index is NULL if OBJF_READNOW. */
3424 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3425 iter->length = MAYBE_SWAP (*iter->vec);
3426 else
3427 {
3428 iter->vec = NULL;
3429 iter->length = 0;
3430 }
3431 }
3432
3433 /* Return the next matching CU or NULL if there are no more. */
3434
3435 static struct dwarf2_per_cu_data *
3436 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3437 {
3438 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3439
3440 for ( ; iter->next < iter->length; ++iter->next)
3441 {
3442 offset_type cu_index_and_attrs =
3443 MAYBE_SWAP (iter->vec[iter->next + 1]);
3444 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3445 gdb_index_symbol_kind symbol_kind =
3446 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3447 /* Only check the symbol attributes if they're present.
3448 Indices prior to version 7 don't record them,
3449 and indices >= 7 may elide them for certain symbols
3450 (gold does this). */
3451 int attrs_valid =
3452 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3453 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3454
3455 /* Don't crash on bad data. */
3456 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3457 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3458 {
3459 complaint (_(".gdb_index entry has bad CU index"
3460 " [in module %s]"),
3461 objfile_name (dwarf2_per_objfile->objfile));
3462 continue;
3463 }
3464
3465 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3466
3467 /* Skip if already read in. */
3468 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3469 continue;
3470
3471 /* Check static vs global. */
3472 if (attrs_valid)
3473 {
3474 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3475
3476 if (iter->block_index.has_value ())
3477 {
3478 bool want_static = *iter->block_index == STATIC_BLOCK;
3479
3480 if (is_static != want_static)
3481 continue;
3482 }
3483
3484 /* Work around gold/15646. */
3485 if (!is_static && iter->global_seen)
3486 continue;
3487 if (!is_static)
3488 iter->global_seen = 1;
3489 }
3490
3491 /* Only check the symbol's kind if it has one. */
3492 if (attrs_valid)
3493 {
3494 switch (iter->domain)
3495 {
3496 case VAR_DOMAIN:
3497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3498 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3499 /* Some types are also in VAR_DOMAIN. */
3500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3501 continue;
3502 break;
3503 case STRUCT_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3505 continue;
3506 break;
3507 case LABEL_DOMAIN:
3508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3509 continue;
3510 break;
3511 case MODULE_DOMAIN:
3512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3513 continue;
3514 break;
3515 default:
3516 break;
3517 }
3518 }
3519
3520 ++iter->next;
3521 return per_cu;
3522 }
3523
3524 return NULL;
3525 }
3526
3527 static struct compunit_symtab *
3528 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3529 const char *name, domain_enum domain)
3530 {
3531 struct compunit_symtab *stab_best = NULL;
3532 struct dwarf2_per_objfile *dwarf2_per_objfile
3533 = get_dwarf2_per_objfile (objfile);
3534
3535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3536
3537 struct dw2_symtab_iterator iter;
3538 struct dwarf2_per_cu_data *per_cu;
3539
3540 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3541
3542 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3543 {
3544 struct symbol *sym, *with_opaque = NULL;
3545 struct compunit_symtab *stab
3546 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3547 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3548 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3549
3550 sym = block_find_symbol (block, name, domain,
3551 block_find_non_opaque_type_preferred,
3552 &with_opaque);
3553
3554 /* Some caution must be observed with overloaded functions
3555 and methods, since the index will not contain any overload
3556 information (but NAME might contain it). */
3557
3558 if (sym != NULL
3559 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3560 return stab;
3561 if (with_opaque != NULL
3562 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3563 stab_best = stab;
3564
3565 /* Keep looking through other CUs. */
3566 }
3567
3568 return stab_best;
3569 }
3570
3571 static void
3572 dw2_print_stats (struct objfile *objfile)
3573 {
3574 struct dwarf2_per_objfile *dwarf2_per_objfile
3575 = get_dwarf2_per_objfile (objfile);
3576 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3577 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3578 int count = 0;
3579
3580 for (int i = 0; i < total; ++i)
3581 {
3582 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3583
3584 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3585 ++count;
3586 }
3587 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3588 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3589 }
3590
3591 /* This dumps minimal information about the index.
3592 It is called via "mt print objfiles".
3593 One use is to verify .gdb_index has been loaded by the
3594 gdb.dwarf2/gdb-index.exp testcase. */
3595
3596 static void
3597 dw2_dump (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601
3602 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3603 printf_filtered (".gdb_index:");
3604 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3605 {
3606 printf_filtered (" version %d\n",
3607 dwarf2_per_objfile->per_bfd->index_table->version);
3608 }
3609 else
3610 printf_filtered (" faked for \"readnow\"\n");
3611 printf_filtered ("\n");
3612 }
3613
3614 static void
3615 dw2_expand_symtabs_for_function (struct objfile *objfile,
3616 const char *func_name)
3617 {
3618 struct dwarf2_per_objfile *dwarf2_per_objfile
3619 = get_dwarf2_per_objfile (objfile);
3620
3621 struct dw2_symtab_iterator iter;
3622 struct dwarf2_per_cu_data *per_cu;
3623
3624 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3625
3626 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3627 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3628
3629 }
3630
3631 static void
3632 dw2_expand_all_symtabs (struct objfile *objfile)
3633 {
3634 struct dwarf2_per_objfile *dwarf2_per_objfile
3635 = get_dwarf2_per_objfile (objfile);
3636 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3637 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3638
3639 for (int i = 0; i < total_units; ++i)
3640 {
3641 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3642
3643 /* We don't want to directly expand a partial CU, because if we
3644 read it with the wrong language, then assertion failures can
3645 be triggered later on. See PR symtab/23010. So, tell
3646 dw2_instantiate_symtab to skip partial CUs -- any important
3647 partial CU will be read via DW_TAG_imported_unit anyway. */
3648 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3649 }
3650 }
3651
3652 static void
3653 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3654 const char *fullname)
3655 {
3656 struct dwarf2_per_objfile *dwarf2_per_objfile
3657 = get_dwarf2_per_objfile (objfile);
3658
3659 /* We don't need to consider type units here.
3660 This is only called for examining code, e.g. expand_line_sal.
3661 There can be an order of magnitude (or more) more type units
3662 than comp units, and we avoid them if we can. */
3663
3664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3665 {
3666 /* We only need to look at symtabs not already expanded. */
3667 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3668 continue;
3669
3670 quick_file_names *file_data
3671 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3672 if (file_data == NULL)
3673 continue;
3674
3675 for (int j = 0; j < file_data->num_file_names; ++j)
3676 {
3677 const char *this_fullname = file_data->file_names[j];
3678
3679 if (filename_cmp (this_fullname, fullname) == 0)
3680 {
3681 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3682 break;
3683 }
3684 }
3685 }
3686 }
3687
3688 static void
3689 dw2_expand_symtabs_matching_symbol
3690 (mapped_index_base &index,
3691 const lookup_name_info &lookup_name_in,
3692 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3693 enum search_domain kind,
3694 gdb::function_view<bool (offset_type)> match_callback);
3695
3696 static void
3697 dw2_expand_symtabs_matching_one
3698 (dwarf2_per_cu_data *per_cu,
3699 dwarf2_per_objfile *per_objfile,
3700 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3701 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3702
3703 static void
3704 dw2_map_matching_symbols
3705 (struct objfile *objfile,
3706 const lookup_name_info &name, domain_enum domain,
3707 int global,
3708 gdb::function_view<symbol_found_callback_ftype> callback,
3709 symbol_compare_ftype *ordered_compare)
3710 {
3711 /* Used for Ada. */
3712 struct dwarf2_per_objfile *dwarf2_per_objfile
3713 = get_dwarf2_per_objfile (objfile);
3714
3715 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3716
3717 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3718 {
3719 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3720 here though if the current language is Ada for a non-Ada objfile
3721 using GNU index. */
3722 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3723
3724 const char *match_name = name.ada ().lookup_name ().c_str ();
3725 auto matcher = [&] (const char *symname)
3726 {
3727 if (ordered_compare == nullptr)
3728 return true;
3729 return ordered_compare (symname, match_name) == 0;
3730 };
3731
3732 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3733 [&] (offset_type namei)
3734 {
3735 struct dw2_symtab_iterator iter;
3736 struct dwarf2_per_cu_data *per_cu;
3737
3738 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3739 match_name);
3740 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3741 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3742 nullptr);
3743 return true;
3744 });
3745 }
3746 else
3747 {
3748 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3749 proceed assuming all symtabs have been read in. */
3750 }
3751
3752 for (compunit_symtab *cust : objfile->compunits ())
3753 {
3754 const struct block *block;
3755
3756 if (cust == NULL)
3757 continue;
3758 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3759 if (!iterate_over_symbols_terminated (block, name,
3760 domain, callback))
3761 return;
3762 }
3763 }
3764
3765 /* Starting from a search name, return the string that finds the upper
3766 bound of all strings that start with SEARCH_NAME in a sorted name
3767 list. Returns the empty string to indicate that the upper bound is
3768 the end of the list. */
3769
3770 static std::string
3771 make_sort_after_prefix_name (const char *search_name)
3772 {
3773 /* When looking to complete "func", we find the upper bound of all
3774 symbols that start with "func" by looking for where we'd insert
3775 the closest string that would follow "func" in lexicographical
3776 order. Usually, that's "func"-with-last-character-incremented,
3777 i.e. "fund". Mind non-ASCII characters, though. Usually those
3778 will be UTF-8 multi-byte sequences, but we can't be certain.
3779 Especially mind the 0xff character, which is a valid character in
3780 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3781 rule out compilers allowing it in identifiers. Note that
3782 conveniently, strcmp/strcasecmp are specified to compare
3783 characters interpreted as unsigned char. So what we do is treat
3784 the whole string as a base 256 number composed of a sequence of
3785 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3786 to 0, and carries 1 to the following more-significant position.
3787 If the very first character in SEARCH_NAME ends up incremented
3788 and carries/overflows, then the upper bound is the end of the
3789 list. The string after the empty string is also the empty
3790 string.
3791
3792 Some examples of this operation:
3793
3794 SEARCH_NAME => "+1" RESULT
3795
3796 "abc" => "abd"
3797 "ab\xff" => "ac"
3798 "\xff" "a" "\xff" => "\xff" "b"
3799 "\xff" => ""
3800 "\xff\xff" => ""
3801 "" => ""
3802
3803 Then, with these symbols for example:
3804
3805 func
3806 func1
3807 fund
3808
3809 completing "func" looks for symbols between "func" and
3810 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3811 which finds "func" and "func1", but not "fund".
3812
3813 And with:
3814
3815 funcÿ (Latin1 'ÿ' [0xff])
3816 funcÿ1
3817 fund
3818
3819 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3820 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3821
3822 And with:
3823
3824 ÿÿ (Latin1 'ÿ' [0xff])
3825 ÿÿ1
3826
3827 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3828 the end of the list.
3829 */
3830 std::string after = search_name;
3831 while (!after.empty () && (unsigned char) after.back () == 0xff)
3832 after.pop_back ();
3833 if (!after.empty ())
3834 after.back () = (unsigned char) after.back () + 1;
3835 return after;
3836 }
3837
3838 /* See declaration. */
3839
3840 std::pair<std::vector<name_component>::const_iterator,
3841 std::vector<name_component>::const_iterator>
3842 mapped_index_base::find_name_components_bounds
3843 (const lookup_name_info &lookup_name_without_params, language lang) const
3844 {
3845 auto *name_cmp
3846 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3847
3848 const char *lang_name
3849 = lookup_name_without_params.language_lookup_name (lang);
3850
3851 /* Comparison function object for lower_bound that matches against a
3852 given symbol name. */
3853 auto lookup_compare_lower = [&] (const name_component &elem,
3854 const char *name)
3855 {
3856 const char *elem_qualified = this->symbol_name_at (elem.idx);
3857 const char *elem_name = elem_qualified + elem.name_offset;
3858 return name_cmp (elem_name, name) < 0;
3859 };
3860
3861 /* Comparison function object for upper_bound that matches against a
3862 given symbol name. */
3863 auto lookup_compare_upper = [&] (const char *name,
3864 const name_component &elem)
3865 {
3866 const char *elem_qualified = this->symbol_name_at (elem.idx);
3867 const char *elem_name = elem_qualified + elem.name_offset;
3868 return name_cmp (name, elem_name) < 0;
3869 };
3870
3871 auto begin = this->name_components.begin ();
3872 auto end = this->name_components.end ();
3873
3874 /* Find the lower bound. */
3875 auto lower = [&] ()
3876 {
3877 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3878 return begin;
3879 else
3880 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3881 } ();
3882
3883 /* Find the upper bound. */
3884 auto upper = [&] ()
3885 {
3886 if (lookup_name_without_params.completion_mode ())
3887 {
3888 /* In completion mode, we want UPPER to point past all
3889 symbols names that have the same prefix. I.e., with
3890 these symbols, and completing "func":
3891
3892 function << lower bound
3893 function1
3894 other_function << upper bound
3895
3896 We find the upper bound by looking for the insertion
3897 point of "func"-with-last-character-incremented,
3898 i.e. "fund". */
3899 std::string after = make_sort_after_prefix_name (lang_name);
3900 if (after.empty ())
3901 return end;
3902 return std::lower_bound (lower, end, after.c_str (),
3903 lookup_compare_lower);
3904 }
3905 else
3906 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3907 } ();
3908
3909 return {lower, upper};
3910 }
3911
3912 /* See declaration. */
3913
3914 void
3915 mapped_index_base::build_name_components ()
3916 {
3917 if (!this->name_components.empty ())
3918 return;
3919
3920 this->name_components_casing = case_sensitivity;
3921 auto *name_cmp
3922 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3923
3924 /* The code below only knows how to break apart components of C++
3925 symbol names (and other languages that use '::' as
3926 namespace/module separator) and Ada symbol names. */
3927 auto count = this->symbol_name_count ();
3928 for (offset_type idx = 0; idx < count; idx++)
3929 {
3930 if (this->symbol_name_slot_invalid (idx))
3931 continue;
3932
3933 const char *name = this->symbol_name_at (idx);
3934
3935 /* Add each name component to the name component table. */
3936 unsigned int previous_len = 0;
3937
3938 if (strstr (name, "::") != nullptr)
3939 {
3940 for (unsigned int current_len = cp_find_first_component (name);
3941 name[current_len] != '\0';
3942 current_len += cp_find_first_component (name + current_len))
3943 {
3944 gdb_assert (name[current_len] == ':');
3945 this->name_components.push_back ({previous_len, idx});
3946 /* Skip the '::'. */
3947 current_len += 2;
3948 previous_len = current_len;
3949 }
3950 }
3951 else
3952 {
3953 /* Handle the Ada encoded (aka mangled) form here. */
3954 for (const char *iter = strstr (name, "__");
3955 iter != nullptr;
3956 iter = strstr (iter, "__"))
3957 {
3958 this->name_components.push_back ({previous_len, idx});
3959 iter += 2;
3960 previous_len = iter - name;
3961 }
3962 }
3963
3964 this->name_components.push_back ({previous_len, idx});
3965 }
3966
3967 /* Sort name_components elements by name. */
3968 auto name_comp_compare = [&] (const name_component &left,
3969 const name_component &right)
3970 {
3971 const char *left_qualified = this->symbol_name_at (left.idx);
3972 const char *right_qualified = this->symbol_name_at (right.idx);
3973
3974 const char *left_name = left_qualified + left.name_offset;
3975 const char *right_name = right_qualified + right.name_offset;
3976
3977 return name_cmp (left_name, right_name) < 0;
3978 };
3979
3980 std::sort (this->name_components.begin (),
3981 this->name_components.end (),
3982 name_comp_compare);
3983 }
3984
3985 /* Helper for dw2_expand_symtabs_matching that works with a
3986 mapped_index_base instead of the containing objfile. This is split
3987 to a separate function in order to be able to unit test the
3988 name_components matching using a mock mapped_index_base. For each
3989 symbol name that matches, calls MATCH_CALLBACK, passing it the
3990 symbol's index in the mapped_index_base symbol table. */
3991
3992 static void
3993 dw2_expand_symtabs_matching_symbol
3994 (mapped_index_base &index,
3995 const lookup_name_info &lookup_name_in,
3996 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3997 enum search_domain kind,
3998 gdb::function_view<bool (offset_type)> match_callback)
3999 {
4000 lookup_name_info lookup_name_without_params
4001 = lookup_name_in.make_ignore_params ();
4002
4003 /* Build the symbol name component sorted vector, if we haven't
4004 yet. */
4005 index.build_name_components ();
4006
4007 /* The same symbol may appear more than once in the range though.
4008 E.g., if we're looking for symbols that complete "w", and we have
4009 a symbol named "w1::w2", we'll find the two name components for
4010 that same symbol in the range. To be sure we only call the
4011 callback once per symbol, we first collect the symbol name
4012 indexes that matched in a temporary vector and ignore
4013 duplicates. */
4014 std::vector<offset_type> matches;
4015
4016 struct name_and_matcher
4017 {
4018 symbol_name_matcher_ftype *matcher;
4019 const char *name;
4020
4021 bool operator== (const name_and_matcher &other) const
4022 {
4023 return matcher == other.matcher && strcmp (name, other.name) == 0;
4024 }
4025 };
4026
4027 /* A vector holding all the different symbol name matchers, for all
4028 languages. */
4029 std::vector<name_and_matcher> matchers;
4030
4031 for (int i = 0; i < nr_languages; i++)
4032 {
4033 enum language lang_e = (enum language) i;
4034
4035 const language_defn *lang = language_def (lang_e);
4036 symbol_name_matcher_ftype *name_matcher
4037 = get_symbol_name_matcher (lang, lookup_name_without_params);
4038
4039 name_and_matcher key {
4040 name_matcher,
4041 lookup_name_without_params.language_lookup_name (lang_e)
4042 };
4043
4044 /* Don't insert the same comparison routine more than once.
4045 Note that we do this linear walk. This is not a problem in
4046 practice because the number of supported languages is
4047 low. */
4048 if (std::find (matchers.begin (), matchers.end (), key)
4049 != matchers.end ())
4050 continue;
4051 matchers.push_back (std::move (key));
4052
4053 auto bounds
4054 = index.find_name_components_bounds (lookup_name_without_params,
4055 lang_e);
4056
4057 /* Now for each symbol name in range, check to see if we have a name
4058 match, and if so, call the MATCH_CALLBACK callback. */
4059
4060 for (; bounds.first != bounds.second; ++bounds.first)
4061 {
4062 const char *qualified = index.symbol_name_at (bounds.first->idx);
4063
4064 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4065 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4066 continue;
4067
4068 matches.push_back (bounds.first->idx);
4069 }
4070 }
4071
4072 std::sort (matches.begin (), matches.end ());
4073
4074 /* Finally call the callback, once per match. */
4075 ULONGEST prev = -1;
4076 for (offset_type idx : matches)
4077 {
4078 if (prev != idx)
4079 {
4080 if (!match_callback (idx))
4081 break;
4082 prev = idx;
4083 }
4084 }
4085
4086 /* Above we use a type wider than idx's for 'prev', since 0 and
4087 (offset_type)-1 are both possible values. */
4088 static_assert (sizeof (prev) > sizeof (offset_type), "");
4089 }
4090
4091 #if GDB_SELF_TEST
4092
4093 namespace selftests { namespace dw2_expand_symtabs_matching {
4094
4095 /* A mock .gdb_index/.debug_names-like name index table, enough to
4096 exercise dw2_expand_symtabs_matching_symbol, which works with the
4097 mapped_index_base interface. Builds an index from the symbol list
4098 passed as parameter to the constructor. */
4099 class mock_mapped_index : public mapped_index_base
4100 {
4101 public:
4102 mock_mapped_index (gdb::array_view<const char *> symbols)
4103 : m_symbol_table (symbols)
4104 {}
4105
4106 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4107
4108 /* Return the number of names in the symbol table. */
4109 size_t symbol_name_count () const override
4110 {
4111 return m_symbol_table.size ();
4112 }
4113
4114 /* Get the name of the symbol at IDX in the symbol table. */
4115 const char *symbol_name_at (offset_type idx) const override
4116 {
4117 return m_symbol_table[idx];
4118 }
4119
4120 private:
4121 gdb::array_view<const char *> m_symbol_table;
4122 };
4123
4124 /* Convenience function that converts a NULL pointer to a "<null>"
4125 string, to pass to print routines. */
4126
4127 static const char *
4128 string_or_null (const char *str)
4129 {
4130 return str != NULL ? str : "<null>";
4131 }
4132
4133 /* Check if a lookup_name_info built from
4134 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4135 index. EXPECTED_LIST is the list of expected matches, in expected
4136 matching order. If no match expected, then an empty list is
4137 specified. Returns true on success. On failure prints a warning
4138 indicating the file:line that failed, and returns false. */
4139
4140 static bool
4141 check_match (const char *file, int line,
4142 mock_mapped_index &mock_index,
4143 const char *name, symbol_name_match_type match_type,
4144 bool completion_mode,
4145 std::initializer_list<const char *> expected_list)
4146 {
4147 lookup_name_info lookup_name (name, match_type, completion_mode);
4148
4149 bool matched = true;
4150
4151 auto mismatch = [&] (const char *expected_str,
4152 const char *got)
4153 {
4154 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4155 "expected=\"%s\", got=\"%s\"\n"),
4156 file, line,
4157 (match_type == symbol_name_match_type::FULL
4158 ? "FULL" : "WILD"),
4159 name, string_or_null (expected_str), string_or_null (got));
4160 matched = false;
4161 };
4162
4163 auto expected_it = expected_list.begin ();
4164 auto expected_end = expected_list.end ();
4165
4166 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4167 NULL, ALL_DOMAIN,
4168 [&] (offset_type idx)
4169 {
4170 const char *matched_name = mock_index.symbol_name_at (idx);
4171 const char *expected_str
4172 = expected_it == expected_end ? NULL : *expected_it++;
4173
4174 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4175 mismatch (expected_str, matched_name);
4176 return true;
4177 });
4178
4179 const char *expected_str
4180 = expected_it == expected_end ? NULL : *expected_it++;
4181 if (expected_str != NULL)
4182 mismatch (expected_str, NULL);
4183
4184 return matched;
4185 }
4186
4187 /* The symbols added to the mock mapped_index for testing (in
4188 canonical form). */
4189 static const char *test_symbols[] = {
4190 "function",
4191 "std::bar",
4192 "std::zfunction",
4193 "std::zfunction2",
4194 "w1::w2",
4195 "ns::foo<char*>",
4196 "ns::foo<int>",
4197 "ns::foo<long>",
4198 "ns2::tmpl<int>::foo2",
4199 "(anonymous namespace)::A::B::C",
4200
4201 /* These are used to check that the increment-last-char in the
4202 matching algorithm for completion doesn't match "t1_fund" when
4203 completing "t1_func". */
4204 "t1_func",
4205 "t1_func1",
4206 "t1_fund",
4207 "t1_fund1",
4208
4209 /* A UTF-8 name with multi-byte sequences to make sure that
4210 cp-name-parser understands this as a single identifier ("função"
4211 is "function" in PT). */
4212 u8"u8função",
4213
4214 /* \377 (0xff) is Latin1 'ÿ'. */
4215 "yfunc\377",
4216
4217 /* \377 (0xff) is Latin1 'ÿ'. */
4218 "\377",
4219 "\377\377123",
4220
4221 /* A name with all sorts of complications. Starts with "z" to make
4222 it easier for the completion tests below. */
4223 #define Z_SYM_NAME \
4224 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4225 "::tuple<(anonymous namespace)::ui*, " \
4226 "std::default_delete<(anonymous namespace)::ui>, void>"
4227
4228 Z_SYM_NAME
4229 };
4230
4231 /* Returns true if the mapped_index_base::find_name_component_bounds
4232 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4233 in completion mode. */
4234
4235 static bool
4236 check_find_bounds_finds (mapped_index_base &index,
4237 const char *search_name,
4238 gdb::array_view<const char *> expected_syms)
4239 {
4240 lookup_name_info lookup_name (search_name,
4241 symbol_name_match_type::FULL, true);
4242
4243 auto bounds = index.find_name_components_bounds (lookup_name,
4244 language_cplus);
4245
4246 size_t distance = std::distance (bounds.first, bounds.second);
4247 if (distance != expected_syms.size ())
4248 return false;
4249
4250 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4251 {
4252 auto nc_elem = bounds.first + exp_elem;
4253 const char *qualified = index.symbol_name_at (nc_elem->idx);
4254 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4255 return false;
4256 }
4257
4258 return true;
4259 }
4260
4261 /* Test the lower-level mapped_index::find_name_component_bounds
4262 method. */
4263
4264 static void
4265 test_mapped_index_find_name_component_bounds ()
4266 {
4267 mock_mapped_index mock_index (test_symbols);
4268
4269 mock_index.build_name_components ();
4270
4271 /* Test the lower-level mapped_index::find_name_component_bounds
4272 method in completion mode. */
4273 {
4274 static const char *expected_syms[] = {
4275 "t1_func",
4276 "t1_func1",
4277 };
4278
4279 SELF_CHECK (check_find_bounds_finds (mock_index,
4280 "t1_func", expected_syms));
4281 }
4282
4283 /* Check that the increment-last-char in the name matching algorithm
4284 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4285 {
4286 static const char *expected_syms1[] = {
4287 "\377",
4288 "\377\377123",
4289 };
4290 SELF_CHECK (check_find_bounds_finds (mock_index,
4291 "\377", expected_syms1));
4292
4293 static const char *expected_syms2[] = {
4294 "\377\377123",
4295 };
4296 SELF_CHECK (check_find_bounds_finds (mock_index,
4297 "\377\377", expected_syms2));
4298 }
4299 }
4300
4301 /* Test dw2_expand_symtabs_matching_symbol. */
4302
4303 static void
4304 test_dw2_expand_symtabs_matching_symbol ()
4305 {
4306 mock_mapped_index mock_index (test_symbols);
4307
4308 /* We let all tests run until the end even if some fails, for debug
4309 convenience. */
4310 bool any_mismatch = false;
4311
4312 /* Create the expected symbols list (an initializer_list). Needed
4313 because lists have commas, and we need to pass them to CHECK,
4314 which is a macro. */
4315 #define EXPECT(...) { __VA_ARGS__ }
4316
4317 /* Wrapper for check_match that passes down the current
4318 __FILE__/__LINE__. */
4319 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4320 any_mismatch |= !check_match (__FILE__, __LINE__, \
4321 mock_index, \
4322 NAME, MATCH_TYPE, COMPLETION_MODE, \
4323 EXPECTED_LIST)
4324
4325 /* Identity checks. */
4326 for (const char *sym : test_symbols)
4327 {
4328 /* Should be able to match all existing symbols. */
4329 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4330 EXPECT (sym));
4331
4332 /* Should be able to match all existing symbols with
4333 parameters. */
4334 std::string with_params = std::string (sym) + "(int)";
4335 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4336 EXPECT (sym));
4337
4338 /* Should be able to match all existing symbols with
4339 parameters and qualifiers. */
4340 with_params = std::string (sym) + " ( int ) const";
4341 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4342 EXPECT (sym));
4343
4344 /* This should really find sym, but cp-name-parser.y doesn't
4345 know about lvalue/rvalue qualifiers yet. */
4346 with_params = std::string (sym) + " ( int ) &&";
4347 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4348 {});
4349 }
4350
4351 /* Check that the name matching algorithm for completion doesn't get
4352 confused with Latin1 'ÿ' / 0xff. */
4353 {
4354 static const char str[] = "\377";
4355 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4356 EXPECT ("\377", "\377\377123"));
4357 }
4358
4359 /* Check that the increment-last-char in the matching algorithm for
4360 completion doesn't match "t1_fund" when completing "t1_func". */
4361 {
4362 static const char str[] = "t1_func";
4363 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4364 EXPECT ("t1_func", "t1_func1"));
4365 }
4366
4367 /* Check that completion mode works at each prefix of the expected
4368 symbol name. */
4369 {
4370 static const char str[] = "function(int)";
4371 size_t len = strlen (str);
4372 std::string lookup;
4373
4374 for (size_t i = 1; i < len; i++)
4375 {
4376 lookup.assign (str, i);
4377 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4378 EXPECT ("function"));
4379 }
4380 }
4381
4382 /* While "w" is a prefix of both components, the match function
4383 should still only be called once. */
4384 {
4385 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4386 EXPECT ("w1::w2"));
4387 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4388 EXPECT ("w1::w2"));
4389 }
4390
4391 /* Same, with a "complicated" symbol. */
4392 {
4393 static const char str[] = Z_SYM_NAME;
4394 size_t len = strlen (str);
4395 std::string lookup;
4396
4397 for (size_t i = 1; i < len; i++)
4398 {
4399 lookup.assign (str, i);
4400 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4401 EXPECT (Z_SYM_NAME));
4402 }
4403 }
4404
4405 /* In FULL mode, an incomplete symbol doesn't match. */
4406 {
4407 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4408 {});
4409 }
4410
4411 /* A complete symbol with parameters matches any overload, since the
4412 index has no overload info. */
4413 {
4414 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4415 EXPECT ("std::zfunction", "std::zfunction2"));
4416 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4417 EXPECT ("std::zfunction", "std::zfunction2"));
4418 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4419 EXPECT ("std::zfunction", "std::zfunction2"));
4420 }
4421
4422 /* Check that whitespace is ignored appropriately. A symbol with a
4423 template argument list. */
4424 {
4425 static const char expected[] = "ns::foo<int>";
4426 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4427 EXPECT (expected));
4428 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4429 EXPECT (expected));
4430 }
4431
4432 /* Check that whitespace is ignored appropriately. A symbol with a
4433 template argument list that includes a pointer. */
4434 {
4435 static const char expected[] = "ns::foo<char*>";
4436 /* Try both completion and non-completion modes. */
4437 static const bool completion_mode[2] = {false, true};
4438 for (size_t i = 0; i < 2; i++)
4439 {
4440 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4441 completion_mode[i], EXPECT (expected));
4442 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4443 completion_mode[i], EXPECT (expected));
4444
4445 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4446 completion_mode[i], EXPECT (expected));
4447 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4448 completion_mode[i], EXPECT (expected));
4449 }
4450 }
4451
4452 {
4453 /* Check method qualifiers are ignored. */
4454 static const char expected[] = "ns::foo<char*>";
4455 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4456 symbol_name_match_type::FULL, true, EXPECT (expected));
4457 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4458 symbol_name_match_type::FULL, true, EXPECT (expected));
4459 CHECK_MATCH ("foo < char * > ( int ) const",
4460 symbol_name_match_type::WILD, true, EXPECT (expected));
4461 CHECK_MATCH ("foo < char * > ( int ) &&",
4462 symbol_name_match_type::WILD, true, EXPECT (expected));
4463 }
4464
4465 /* Test lookup names that don't match anything. */
4466 {
4467 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4468 {});
4469
4470 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4471 {});
4472 }
4473
4474 /* Some wild matching tests, exercising "(anonymous namespace)",
4475 which should not be confused with a parameter list. */
4476 {
4477 static const char *syms[] = {
4478 "A::B::C",
4479 "B::C",
4480 "C",
4481 "A :: B :: C ( int )",
4482 "B :: C ( int )",
4483 "C ( int )",
4484 };
4485
4486 for (const char *s : syms)
4487 {
4488 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4489 EXPECT ("(anonymous namespace)::A::B::C"));
4490 }
4491 }
4492
4493 {
4494 static const char expected[] = "ns2::tmpl<int>::foo2";
4495 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4496 EXPECT (expected));
4497 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4498 EXPECT (expected));
4499 }
4500
4501 SELF_CHECK (!any_mismatch);
4502
4503 #undef EXPECT
4504 #undef CHECK_MATCH
4505 }
4506
4507 static void
4508 run_test ()
4509 {
4510 test_mapped_index_find_name_component_bounds ();
4511 test_dw2_expand_symtabs_matching_symbol ();
4512 }
4513
4514 }} // namespace selftests::dw2_expand_symtabs_matching
4515
4516 #endif /* GDB_SELF_TEST */
4517
4518 /* If FILE_MATCHER is NULL or if PER_CU has
4519 dwarf2_per_cu_quick_data::MARK set (see
4520 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4521 EXPANSION_NOTIFY on it. */
4522
4523 static void
4524 dw2_expand_symtabs_matching_one
4525 (dwarf2_per_cu_data *per_cu,
4526 dwarf2_per_objfile *per_objfile,
4527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4528 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4529 {
4530 if (file_matcher == NULL || per_cu->v.quick->mark)
4531 {
4532 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4533
4534 compunit_symtab *symtab
4535 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4536 gdb_assert (symtab != nullptr);
4537
4538 if (expansion_notify != NULL && symtab_was_null)
4539 expansion_notify (symtab);
4540 }
4541 }
4542
4543 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4544 matched, to expand corresponding CUs that were marked. IDX is the
4545 index of the symbol name that matched. */
4546
4547 static void
4548 dw2_expand_marked_cus
4549 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4550 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4551 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4552 search_domain kind)
4553 {
4554 offset_type *vec, vec_len, vec_idx;
4555 bool global_seen = false;
4556 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4557
4558 vec = (offset_type *) (index.constant_pool
4559 + MAYBE_SWAP (index.symbol_table[idx].vec));
4560 vec_len = MAYBE_SWAP (vec[0]);
4561 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4562 {
4563 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4564 /* This value is only valid for index versions >= 7. */
4565 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4566 gdb_index_symbol_kind symbol_kind =
4567 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4568 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4569 /* Only check the symbol attributes if they're present.
4570 Indices prior to version 7 don't record them,
4571 and indices >= 7 may elide them for certain symbols
4572 (gold does this). */
4573 int attrs_valid =
4574 (index.version >= 7
4575 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4576
4577 /* Work around gold/15646. */
4578 if (attrs_valid)
4579 {
4580 if (!is_static && global_seen)
4581 continue;
4582 if (!is_static)
4583 global_seen = true;
4584 }
4585
4586 /* Only check the symbol's kind if it has one. */
4587 if (attrs_valid)
4588 {
4589 switch (kind)
4590 {
4591 case VARIABLES_DOMAIN:
4592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4593 continue;
4594 break;
4595 case FUNCTIONS_DOMAIN:
4596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4597 continue;
4598 break;
4599 case TYPES_DOMAIN:
4600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4601 continue;
4602 break;
4603 case MODULES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4605 continue;
4606 break;
4607 default:
4608 break;
4609 }
4610 }
4611
4612 /* Don't crash on bad data. */
4613 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4614 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4615 {
4616 complaint (_(".gdb_index entry has bad CU index"
4617 " [in module %s]"),
4618 objfile_name (dwarf2_per_objfile->objfile));
4619 continue;
4620 }
4621
4622 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4623 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4624 expansion_notify);
4625 }
4626 }
4627
4628 /* If FILE_MATCHER is non-NULL, set all the
4629 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4630 that match FILE_MATCHER. */
4631
4632 static void
4633 dw_expand_symtabs_matching_file_matcher
4634 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4636 {
4637 if (file_matcher == NULL)
4638 return;
4639
4640 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4641 htab_eq_pointer,
4642 NULL, xcalloc, xfree));
4643 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4644 htab_eq_pointer,
4645 NULL, xcalloc, xfree));
4646
4647 /* The rule is CUs specify all the files, including those used by
4648 any TU, so there's no need to scan TUs here. */
4649
4650 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4651 {
4652 QUIT;
4653
4654 per_cu->v.quick->mark = 0;
4655
4656 /* We only need to look at symtabs not already expanded. */
4657 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4658 continue;
4659
4660 quick_file_names *file_data
4661 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4662 if (file_data == NULL)
4663 continue;
4664
4665 if (htab_find (visited_not_found.get (), file_data) != NULL)
4666 continue;
4667 else if (htab_find (visited_found.get (), file_data) != NULL)
4668 {
4669 per_cu->v.quick->mark = 1;
4670 continue;
4671 }
4672
4673 for (int j = 0; j < file_data->num_file_names; ++j)
4674 {
4675 const char *this_real_name;
4676
4677 if (file_matcher (file_data->file_names[j], false))
4678 {
4679 per_cu->v.quick->mark = 1;
4680 break;
4681 }
4682
4683 /* Before we invoke realpath, which can get expensive when many
4684 files are involved, do a quick comparison of the basenames. */
4685 if (!basenames_may_differ
4686 && !file_matcher (lbasename (file_data->file_names[j]),
4687 true))
4688 continue;
4689
4690 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4691 file_data, j);
4692 if (file_matcher (this_real_name, false))
4693 {
4694 per_cu->v.quick->mark = 1;
4695 break;
4696 }
4697 }
4698
4699 void **slot = htab_find_slot (per_cu->v.quick->mark
4700 ? visited_found.get ()
4701 : visited_not_found.get (),
4702 file_data, INSERT);
4703 *slot = file_data;
4704 }
4705 }
4706
4707 static void
4708 dw2_expand_symtabs_matching
4709 (struct objfile *objfile,
4710 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4711 const lookup_name_info *lookup_name,
4712 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4713 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4714 enum search_domain kind)
4715 {
4716 struct dwarf2_per_objfile *dwarf2_per_objfile
4717 = get_dwarf2_per_objfile (objfile);
4718
4719 /* index_table is NULL if OBJF_READNOW. */
4720 if (!dwarf2_per_objfile->per_bfd->index_table)
4721 return;
4722
4723 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4724
4725 if (symbol_matcher == NULL && lookup_name == NULL)
4726 {
4727 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4728 {
4729 QUIT;
4730
4731 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4732 file_matcher, expansion_notify);
4733 }
4734 return;
4735 }
4736
4737 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4738
4739 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4740 symbol_matcher,
4741 kind, [&] (offset_type idx)
4742 {
4743 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4744 expansion_notify, kind);
4745 return true;
4746 });
4747 }
4748
4749 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4750 symtab. */
4751
4752 static struct compunit_symtab *
4753 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4754 CORE_ADDR pc)
4755 {
4756 int i;
4757
4758 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4759 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4760 return cust;
4761
4762 if (cust->includes == NULL)
4763 return NULL;
4764
4765 for (i = 0; cust->includes[i]; ++i)
4766 {
4767 struct compunit_symtab *s = cust->includes[i];
4768
4769 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4770 if (s != NULL)
4771 return s;
4772 }
4773
4774 return NULL;
4775 }
4776
4777 static struct compunit_symtab *
4778 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4779 struct bound_minimal_symbol msymbol,
4780 CORE_ADDR pc,
4781 struct obj_section *section,
4782 int warn_if_readin)
4783 {
4784 struct dwarf2_per_cu_data *data;
4785 struct compunit_symtab *result;
4786
4787 if (!objfile->partial_symtabs->psymtabs_addrmap)
4788 return NULL;
4789
4790 CORE_ADDR baseaddr = objfile->text_section_offset ();
4791 data = (struct dwarf2_per_cu_data *) addrmap_find
4792 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4793 if (!data)
4794 return NULL;
4795
4796 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4797 if (warn_if_readin && per_objfile->symtab_set_p (data))
4798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4799 paddress (objfile->arch (), pc));
4800
4801 result = recursively_find_pc_sect_compunit_symtab
4802 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4803
4804 gdb_assert (result != NULL);
4805 return result;
4806 }
4807
4808 static void
4809 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4810 void *data, int need_fullname)
4811 {
4812 struct dwarf2_per_objfile *dwarf2_per_objfile
4813 = get_dwarf2_per_objfile (objfile);
4814
4815 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4816 {
4817 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4818
4819 htab_up visited (htab_create_alloc (10,
4820 htab_hash_pointer, htab_eq_pointer,
4821 NULL, xcalloc, xfree));
4822
4823 /* The rule is CUs specify all the files, including those used
4824 by any TU, so there's no need to scan TUs here. We can
4825 ignore file names coming from already-expanded CUs. */
4826
4827 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4828 {
4829 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4830 {
4831 void **slot = htab_find_slot (visited.get (),
4832 per_cu->v.quick->file_names,
4833 INSERT);
4834
4835 *slot = per_cu->v.quick->file_names;
4836 }
4837 }
4838
4839 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4840 {
4841 /* We only need to look at symtabs not already expanded. */
4842 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4843 continue;
4844
4845 quick_file_names *file_data
4846 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4847 if (file_data == NULL)
4848 continue;
4849
4850 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4851 if (*slot)
4852 {
4853 /* Already visited. */
4854 continue;
4855 }
4856 *slot = file_data;
4857
4858 for (int j = 0; j < file_data->num_file_names; ++j)
4859 {
4860 const char *filename = file_data->file_names[j];
4861 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4862 }
4863 }
4864 }
4865
4866 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4867 {
4868 gdb::unique_xmalloc_ptr<char> this_real_name;
4869
4870 if (need_fullname)
4871 this_real_name = gdb_realpath (filename);
4872 (*fun) (filename, this_real_name.get (), data);
4873 });
4874 }
4875
4876 static int
4877 dw2_has_symbols (struct objfile *objfile)
4878 {
4879 return 1;
4880 }
4881
4882 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4883 {
4884 dw2_has_symbols,
4885 dw2_find_last_source_symtab,
4886 dw2_forget_cached_source_info,
4887 dw2_map_symtabs_matching_filename,
4888 dw2_lookup_symbol,
4889 NULL,
4890 dw2_print_stats,
4891 dw2_dump,
4892 dw2_expand_symtabs_for_function,
4893 dw2_expand_all_symtabs,
4894 dw2_expand_symtabs_with_fullname,
4895 dw2_map_matching_symbols,
4896 dw2_expand_symtabs_matching,
4897 dw2_find_pc_sect_compunit_symtab,
4898 NULL,
4899 dw2_map_symbol_filenames
4900 };
4901
4902 /* DWARF-5 debug_names reader. */
4903
4904 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4905 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4906
4907 /* A helper function that reads the .debug_names section in SECTION
4908 and fills in MAP. FILENAME is the name of the file containing the
4909 section; it is used for error reporting.
4910
4911 Returns true if all went well, false otherwise. */
4912
4913 static bool
4914 read_debug_names_from_section (struct objfile *objfile,
4915 const char *filename,
4916 struct dwarf2_section_info *section,
4917 mapped_debug_names &map)
4918 {
4919 if (section->empty ())
4920 return false;
4921
4922 /* Older elfutils strip versions could keep the section in the main
4923 executable while splitting it for the separate debug info file. */
4924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4925 return false;
4926
4927 section->read (objfile);
4928
4929 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4930
4931 const gdb_byte *addr = section->buffer;
4932
4933 bfd *const abfd = section->get_bfd_owner ();
4934
4935 unsigned int bytes_read;
4936 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938
4939 map.dwarf5_is_dwarf64 = bytes_read != 4;
4940 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4941 if (bytes_read + length != section->size)
4942 {
4943 /* There may be multiple per-CU indices. */
4944 warning (_("Section .debug_names in %s length %s does not match "
4945 "section length %s, ignoring .debug_names."),
4946 filename, plongest (bytes_read + length),
4947 pulongest (section->size));
4948 return false;
4949 }
4950
4951 /* The version number. */
4952 uint16_t version = read_2_bytes (abfd, addr);
4953 addr += 2;
4954 if (version != 5)
4955 {
4956 warning (_("Section .debug_names in %s has unsupported version %d, "
4957 "ignoring .debug_names."),
4958 filename, version);
4959 return false;
4960 }
4961
4962 /* Padding. */
4963 uint16_t padding = read_2_bytes (abfd, addr);
4964 addr += 2;
4965 if (padding != 0)
4966 {
4967 warning (_("Section .debug_names in %s has unsupported padding %d, "
4968 "ignoring .debug_names."),
4969 filename, padding);
4970 return false;
4971 }
4972
4973 /* comp_unit_count - The number of CUs in the CU list. */
4974 map.cu_count = read_4_bytes (abfd, addr);
4975 addr += 4;
4976
4977 /* local_type_unit_count - The number of TUs in the local TU
4978 list. */
4979 map.tu_count = read_4_bytes (abfd, addr);
4980 addr += 4;
4981
4982 /* foreign_type_unit_count - The number of TUs in the foreign TU
4983 list. */
4984 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4985 addr += 4;
4986 if (foreign_tu_count != 0)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4989 "ignoring .debug_names."),
4990 filename, static_cast<unsigned long> (foreign_tu_count));
4991 return false;
4992 }
4993
4994 /* bucket_count - The number of hash buckets in the hash lookup
4995 table. */
4996 map.bucket_count = read_4_bytes (abfd, addr);
4997 addr += 4;
4998
4999 /* name_count - The number of unique names in the index. */
5000 map.name_count = read_4_bytes (abfd, addr);
5001 addr += 4;
5002
5003 /* abbrev_table_size - The size in bytes of the abbreviations
5004 table. */
5005 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5006 addr += 4;
5007
5008 /* augmentation_string_size - The size in bytes of the augmentation
5009 string. This value is rounded up to a multiple of 4. */
5010 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5011 addr += 4;
5012 map.augmentation_is_gdb = ((augmentation_string_size
5013 == sizeof (dwarf5_augmentation))
5014 && memcmp (addr, dwarf5_augmentation,
5015 sizeof (dwarf5_augmentation)) == 0);
5016 augmentation_string_size += (-augmentation_string_size) & 3;
5017 addr += augmentation_string_size;
5018
5019 /* List of CUs */
5020 map.cu_table_reordered = addr;
5021 addr += map.cu_count * map.offset_size;
5022
5023 /* List of Local TUs */
5024 map.tu_table_reordered = addr;
5025 addr += map.tu_count * map.offset_size;
5026
5027 /* Hash Lookup Table */
5028 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5029 addr += map.bucket_count * 4;
5030 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5031 addr += map.name_count * 4;
5032
5033 /* Name Table */
5034 map.name_table_string_offs_reordered = addr;
5035 addr += map.name_count * map.offset_size;
5036 map.name_table_entry_offs_reordered = addr;
5037 addr += map.name_count * map.offset_size;
5038
5039 const gdb_byte *abbrev_table_start = addr;
5040 for (;;)
5041 {
5042 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5043 addr += bytes_read;
5044 if (index_num == 0)
5045 break;
5046
5047 const auto insertpair
5048 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5049 if (!insertpair.second)
5050 {
5051 warning (_("Section .debug_names in %s has duplicate index %s, "
5052 "ignoring .debug_names."),
5053 filename, pulongest (index_num));
5054 return false;
5055 }
5056 mapped_debug_names::index_val &indexval = insertpair.first->second;
5057 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5058 addr += bytes_read;
5059
5060 for (;;)
5061 {
5062 mapped_debug_names::index_val::attr attr;
5063 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5064 addr += bytes_read;
5065 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5066 addr += bytes_read;
5067 if (attr.form == DW_FORM_implicit_const)
5068 {
5069 attr.implicit_const = read_signed_leb128 (abfd, addr,
5070 &bytes_read);
5071 addr += bytes_read;
5072 }
5073 if (attr.dw_idx == 0 && attr.form == 0)
5074 break;
5075 indexval.attr_vec.push_back (std::move (attr));
5076 }
5077 }
5078 if (addr != abbrev_table_start + abbrev_table_size)
5079 {
5080 warning (_("Section .debug_names in %s has abbreviation_table "
5081 "of size %s vs. written as %u, ignoring .debug_names."),
5082 filename, plongest (addr - abbrev_table_start),
5083 abbrev_table_size);
5084 return false;
5085 }
5086 map.entry_pool = addr;
5087
5088 return true;
5089 }
5090
5091 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5092 list. */
5093
5094 static void
5095 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5096 const mapped_debug_names &map,
5097 dwarf2_section_info &section,
5098 bool is_dwz)
5099 {
5100 if (!map.augmentation_is_gdb)
5101 {
5102 for (uint32_t i = 0; i < map.cu_count; ++i)
5103 {
5104 sect_offset sect_off
5105 = (sect_offset) (extract_unsigned_integer
5106 (map.cu_table_reordered + i * map.offset_size,
5107 map.offset_size,
5108 map.dwarf5_byte_order));
5109 /* We don't know the length of the CU, because the CU list in a
5110 .debug_names index can be incomplete, so we can't use the start of
5111 the next CU as end of this CU. We create the CUs here with length 0,
5112 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5113 dwarf2_per_cu_data *per_cu
5114 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5115 per_bfd->all_comp_units.push_back (per_cu);
5116 }
5117 }
5118
5119 sect_offset sect_off_prev;
5120 for (uint32_t i = 0; i <= map.cu_count; ++i)
5121 {
5122 sect_offset sect_off_next;
5123 if (i < map.cu_count)
5124 {
5125 sect_off_next
5126 = (sect_offset) (extract_unsigned_integer
5127 (map.cu_table_reordered + i * map.offset_size,
5128 map.offset_size,
5129 map.dwarf5_byte_order));
5130 }
5131 else
5132 sect_off_next = (sect_offset) section.size;
5133 if (i >= 1)
5134 {
5135 const ULONGEST length = sect_off_next - sect_off_prev;
5136 dwarf2_per_cu_data *per_cu
5137 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5138 sect_off_prev, length);
5139 per_bfd->all_comp_units.push_back (per_cu);
5140 }
5141 sect_off_prev = sect_off_next;
5142 }
5143 }
5144
5145 /* Read the CU list from the mapped index, and use it to create all
5146 the CU objects for this dwarf2_per_objfile. */
5147
5148 static void
5149 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5150 const mapped_debug_names &map,
5151 const mapped_debug_names &dwz_map)
5152 {
5153 gdb_assert (per_bfd->all_comp_units.empty ());
5154 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5155
5156 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5157 false /* is_dwz */);
5158
5159 if (dwz_map.cu_count == 0)
5160 return;
5161
5162 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5163 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5164 true /* is_dwz */);
5165 }
5166
5167 /* Read .debug_names. If everything went ok, initialize the "quick"
5168 elements of all the CUs and return true. Otherwise, return false. */
5169
5170 static bool
5171 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5172 {
5173 std::unique_ptr<mapped_debug_names> map
5174 (new mapped_debug_names (dwarf2_per_objfile));
5175 mapped_debug_names dwz_map (dwarf2_per_objfile);
5176 struct objfile *objfile = dwarf2_per_objfile->objfile;
5177
5178 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5179 &dwarf2_per_objfile->per_bfd->debug_names,
5180 *map))
5181 return false;
5182
5183 /* Don't use the index if it's empty. */
5184 if (map->name_count == 0)
5185 return false;
5186
5187 /* If there is a .dwz file, read it so we can get its CU list as
5188 well. */
5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5190 if (dwz != NULL)
5191 {
5192 if (!read_debug_names_from_section (objfile,
5193 bfd_get_filename (dwz->dwz_bfd.get ()),
5194 &dwz->debug_names, dwz_map))
5195 {
5196 warning (_("could not read '.debug_names' section from %s; skipping"),
5197 bfd_get_filename (dwz->dwz_bfd.get ()));
5198 return false;
5199 }
5200 }
5201
5202 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
5203
5204 if (map->tu_count != 0)
5205 {
5206 /* We can only handle a single .debug_types when we have an
5207 index. */
5208 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5209 return false;
5210
5211 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5212
5213 create_signatured_type_table_from_debug_names
5214 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5215 }
5216
5217 create_addrmap_from_aranges (dwarf2_per_objfile,
5218 &dwarf2_per_objfile->per_bfd->debug_aranges);
5219
5220 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5221 dwarf2_per_objfile->per_bfd->using_index = 1;
5222 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5223 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5224
5225 return true;
5226 }
5227
5228 /* Type used to manage iterating over all CUs looking for a symbol for
5229 .debug_names. */
5230
5231 class dw2_debug_names_iterator
5232 {
5233 public:
5234 dw2_debug_names_iterator (const mapped_debug_names &map,
5235 gdb::optional<block_enum> block_index,
5236 domain_enum domain,
5237 const char *name)
5238 : m_map (map), m_block_index (block_index), m_domain (domain),
5239 m_addr (find_vec_in_debug_names (map, name))
5240 {}
5241
5242 dw2_debug_names_iterator (const mapped_debug_names &map,
5243 search_domain search, uint32_t namei)
5244 : m_map (map),
5245 m_search (search),
5246 m_addr (find_vec_in_debug_names (map, namei))
5247 {}
5248
5249 dw2_debug_names_iterator (const mapped_debug_names &map,
5250 block_enum block_index, domain_enum domain,
5251 uint32_t namei)
5252 : m_map (map), m_block_index (block_index), m_domain (domain),
5253 m_addr (find_vec_in_debug_names (map, namei))
5254 {}
5255
5256 /* Return the next matching CU or NULL if there are no more. */
5257 dwarf2_per_cu_data *next ();
5258
5259 private:
5260 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5261 const char *name);
5262 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5263 uint32_t namei);
5264
5265 /* The internalized form of .debug_names. */
5266 const mapped_debug_names &m_map;
5267
5268 /* If set, only look for symbols that match that block. Valid values are
5269 GLOBAL_BLOCK and STATIC_BLOCK. */
5270 const gdb::optional<block_enum> m_block_index;
5271
5272 /* The kind of symbol we're looking for. */
5273 const domain_enum m_domain = UNDEF_DOMAIN;
5274 const search_domain m_search = ALL_DOMAIN;
5275
5276 /* The list of CUs from the index entry of the symbol, or NULL if
5277 not found. */
5278 const gdb_byte *m_addr;
5279 };
5280
5281 const char *
5282 mapped_debug_names::namei_to_name (uint32_t namei) const
5283 {
5284 const ULONGEST namei_string_offs
5285 = extract_unsigned_integer ((name_table_string_offs_reordered
5286 + namei * offset_size),
5287 offset_size,
5288 dwarf5_byte_order);
5289 return read_indirect_string_at_offset (dwarf2_per_objfile,
5290 namei_string_offs);
5291 }
5292
5293 /* Find a slot in .debug_names for the object named NAME. If NAME is
5294 found, return pointer to its pool data. If NAME cannot be found,
5295 return NULL. */
5296
5297 const gdb_byte *
5298 dw2_debug_names_iterator::find_vec_in_debug_names
5299 (const mapped_debug_names &map, const char *name)
5300 {
5301 int (*cmp) (const char *, const char *);
5302
5303 gdb::unique_xmalloc_ptr<char> without_params;
5304 if (current_language->la_language == language_cplus
5305 || current_language->la_language == language_fortran
5306 || current_language->la_language == language_d)
5307 {
5308 /* NAME is already canonical. Drop any qualifiers as
5309 .debug_names does not contain any. */
5310
5311 if (strchr (name, '(') != NULL)
5312 {
5313 without_params = cp_remove_params (name);
5314 if (without_params != NULL)
5315 name = without_params.get ();
5316 }
5317 }
5318
5319 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5320
5321 const uint32_t full_hash = dwarf5_djb_hash (name);
5322 uint32_t namei
5323 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5324 (map.bucket_table_reordered
5325 + (full_hash % map.bucket_count)), 4,
5326 map.dwarf5_byte_order);
5327 if (namei == 0)
5328 return NULL;
5329 --namei;
5330 if (namei >= map.name_count)
5331 {
5332 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5333 "[in module %s]"),
5334 namei, map.name_count,
5335 objfile_name (map.dwarf2_per_objfile->objfile));
5336 return NULL;
5337 }
5338
5339 for (;;)
5340 {
5341 const uint32_t namei_full_hash
5342 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5343 (map.hash_table_reordered + namei), 4,
5344 map.dwarf5_byte_order);
5345 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5346 return NULL;
5347
5348 if (full_hash == namei_full_hash)
5349 {
5350 const char *const namei_string = map.namei_to_name (namei);
5351
5352 #if 0 /* An expensive sanity check. */
5353 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5354 {
5355 complaint (_("Wrong .debug_names hash for string at index %u "
5356 "[in module %s]"),
5357 namei, objfile_name (dwarf2_per_objfile->objfile));
5358 return NULL;
5359 }
5360 #endif
5361
5362 if (cmp (namei_string, name) == 0)
5363 {
5364 const ULONGEST namei_entry_offs
5365 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5366 + namei * map.offset_size),
5367 map.offset_size, map.dwarf5_byte_order);
5368 return map.entry_pool + namei_entry_offs;
5369 }
5370 }
5371
5372 ++namei;
5373 if (namei >= map.name_count)
5374 return NULL;
5375 }
5376 }
5377
5378 const gdb_byte *
5379 dw2_debug_names_iterator::find_vec_in_debug_names
5380 (const mapped_debug_names &map, uint32_t namei)
5381 {
5382 if (namei >= map.name_count)
5383 {
5384 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5385 "[in module %s]"),
5386 namei, map.name_count,
5387 objfile_name (map.dwarf2_per_objfile->objfile));
5388 return NULL;
5389 }
5390
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397
5398 /* See dw2_debug_names_iterator. */
5399
5400 dwarf2_per_cu_data *
5401 dw2_debug_names_iterator::next ()
5402 {
5403 if (m_addr == NULL)
5404 return NULL;
5405
5406 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5407 struct objfile *objfile = dwarf2_per_objfile->objfile;
5408 bfd *const abfd = objfile->obfd;
5409
5410 again:
5411
5412 unsigned int bytes_read;
5413 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5414 m_addr += bytes_read;
5415 if (abbrev == 0)
5416 return NULL;
5417
5418 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5419 if (indexval_it == m_map.abbrev_map.cend ())
5420 {
5421 complaint (_("Wrong .debug_names undefined abbrev code %s "
5422 "[in module %s]"),
5423 pulongest (abbrev), objfile_name (objfile));
5424 return NULL;
5425 }
5426 const mapped_debug_names::index_val &indexval = indexval_it->second;
5427 enum class symbol_linkage {
5428 unknown,
5429 static_,
5430 extern_,
5431 } symbol_linkage_ = symbol_linkage::unknown;
5432 dwarf2_per_cu_data *per_cu = NULL;
5433 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5434 {
5435 ULONGEST ull;
5436 switch (attr.form)
5437 {
5438 case DW_FORM_implicit_const:
5439 ull = attr.implicit_const;
5440 break;
5441 case DW_FORM_flag_present:
5442 ull = 1;
5443 break;
5444 case DW_FORM_udata:
5445 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5446 m_addr += bytes_read;
5447 break;
5448 case DW_FORM_ref4:
5449 ull = read_4_bytes (abfd, m_addr);
5450 m_addr += 4;
5451 break;
5452 case DW_FORM_ref8:
5453 ull = read_8_bytes (abfd, m_addr);
5454 m_addr += 8;
5455 break;
5456 case DW_FORM_ref_sig8:
5457 ull = read_8_bytes (abfd, m_addr);
5458 m_addr += 8;
5459 break;
5460 default:
5461 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5462 dwarf_form_name (attr.form),
5463 objfile_name (objfile));
5464 return NULL;
5465 }
5466 switch (attr.dw_idx)
5467 {
5468 case DW_IDX_compile_unit:
5469 /* Don't crash on bad data. */
5470 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5471 {
5472 complaint (_(".debug_names entry has bad CU index %s"
5473 " [in module %s]"),
5474 pulongest (ull),
5475 objfile_name (dwarf2_per_objfile->objfile));
5476 continue;
5477 }
5478 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5479 break;
5480 case DW_IDX_type_unit:
5481 /* Don't crash on bad data. */
5482 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5483 {
5484 complaint (_(".debug_names entry has bad TU index %s"
5485 " [in module %s]"),
5486 pulongest (ull),
5487 objfile_name (dwarf2_per_objfile->objfile));
5488 continue;
5489 }
5490 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5491 break;
5492 case DW_IDX_die_offset:
5493 /* In a per-CU index (as opposed to a per-module index), index
5494 entries without CU attribute implicitly refer to the single CU. */
5495 if (per_cu == NULL)
5496 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5497 break;
5498 case DW_IDX_GNU_internal:
5499 if (!m_map.augmentation_is_gdb)
5500 break;
5501 symbol_linkage_ = symbol_linkage::static_;
5502 break;
5503 case DW_IDX_GNU_external:
5504 if (!m_map.augmentation_is_gdb)
5505 break;
5506 symbol_linkage_ = symbol_linkage::extern_;
5507 break;
5508 }
5509 }
5510
5511 /* Skip if already read in. */
5512 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5513 goto again;
5514
5515 /* Check static vs global. */
5516 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5517 {
5518 const bool want_static = *m_block_index == STATIC_BLOCK;
5519 const bool symbol_is_static =
5520 symbol_linkage_ == symbol_linkage::static_;
5521 if (want_static != symbol_is_static)
5522 goto again;
5523 }
5524
5525 /* Match dw2_symtab_iter_next, symbol_kind
5526 and debug_names::psymbol_tag. */
5527 switch (m_domain)
5528 {
5529 case VAR_DOMAIN:
5530 switch (indexval.dwarf_tag)
5531 {
5532 case DW_TAG_variable:
5533 case DW_TAG_subprogram:
5534 /* Some types are also in VAR_DOMAIN. */
5535 case DW_TAG_typedef:
5536 case DW_TAG_structure_type:
5537 break;
5538 default:
5539 goto again;
5540 }
5541 break;
5542 case STRUCT_DOMAIN:
5543 switch (indexval.dwarf_tag)
5544 {
5545 case DW_TAG_typedef:
5546 case DW_TAG_structure_type:
5547 break;
5548 default:
5549 goto again;
5550 }
5551 break;
5552 case LABEL_DOMAIN:
5553 switch (indexval.dwarf_tag)
5554 {
5555 case 0:
5556 case DW_TAG_variable:
5557 break;
5558 default:
5559 goto again;
5560 }
5561 break;
5562 case MODULE_DOMAIN:
5563 switch (indexval.dwarf_tag)
5564 {
5565 case DW_TAG_module:
5566 break;
5567 default:
5568 goto again;
5569 }
5570 break;
5571 default:
5572 break;
5573 }
5574
5575 /* Match dw2_expand_symtabs_matching, symbol_kind and
5576 debug_names::psymbol_tag. */
5577 switch (m_search)
5578 {
5579 case VARIABLES_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case DW_TAG_variable:
5583 break;
5584 default:
5585 goto again;
5586 }
5587 break;
5588 case FUNCTIONS_DOMAIN:
5589 switch (indexval.dwarf_tag)
5590 {
5591 case DW_TAG_subprogram:
5592 break;
5593 default:
5594 goto again;
5595 }
5596 break;
5597 case TYPES_DOMAIN:
5598 switch (indexval.dwarf_tag)
5599 {
5600 case DW_TAG_typedef:
5601 case DW_TAG_structure_type:
5602 break;
5603 default:
5604 goto again;
5605 }
5606 break;
5607 case MODULES_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_module:
5611 break;
5612 default:
5613 goto again;
5614 }
5615 default:
5616 break;
5617 }
5618
5619 return per_cu;
5620 }
5621
5622 static struct compunit_symtab *
5623 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5624 const char *name, domain_enum domain)
5625 {
5626 struct dwarf2_per_objfile *dwarf2_per_objfile
5627 = get_dwarf2_per_objfile (objfile);
5628
5629 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5630 if (!mapp)
5631 {
5632 /* index is NULL if OBJF_READNOW. */
5633 return NULL;
5634 }
5635 const auto &map = *mapp;
5636
5637 dw2_debug_names_iterator iter (map, block_index, domain, name);
5638
5639 struct compunit_symtab *stab_best = NULL;
5640 struct dwarf2_per_cu_data *per_cu;
5641 while ((per_cu = iter.next ()) != NULL)
5642 {
5643 struct symbol *sym, *with_opaque = NULL;
5644 compunit_symtab *stab
5645 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5646 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5647 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5648
5649 sym = block_find_symbol (block, name, domain,
5650 block_find_non_opaque_type_preferred,
5651 &with_opaque);
5652
5653 /* Some caution must be observed with overloaded functions and
5654 methods, since the index will not contain any overload
5655 information (but NAME might contain it). */
5656
5657 if (sym != NULL
5658 && strcmp_iw (sym->search_name (), name) == 0)
5659 return stab;
5660 if (with_opaque != NULL
5661 && strcmp_iw (with_opaque->search_name (), name) == 0)
5662 stab_best = stab;
5663
5664 /* Keep looking through other CUs. */
5665 }
5666
5667 return stab_best;
5668 }
5669
5670 /* This dumps minimal information about .debug_names. It is called
5671 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5672 uses this to verify that .debug_names has been loaded. */
5673
5674 static void
5675 dw2_debug_names_dump (struct objfile *objfile)
5676 {
5677 struct dwarf2_per_objfile *dwarf2_per_objfile
5678 = get_dwarf2_per_objfile (objfile);
5679
5680 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5681 printf_filtered (".debug_names:");
5682 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5683 printf_filtered (" exists\n");
5684 else
5685 printf_filtered (" faked for \"readnow\"\n");
5686 printf_filtered ("\n");
5687 }
5688
5689 static void
5690 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5691 const char *func_name)
5692 {
5693 struct dwarf2_per_objfile *dwarf2_per_objfile
5694 = get_dwarf2_per_objfile (objfile);
5695
5696 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5697 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5698 {
5699 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5700
5701 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5702
5703 struct dwarf2_per_cu_data *per_cu;
5704 while ((per_cu = iter.next ()) != NULL)
5705 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5706 }
5707 }
5708
5709 static void
5710 dw2_debug_names_map_matching_symbols
5711 (struct objfile *objfile,
5712 const lookup_name_info &name, domain_enum domain,
5713 int global,
5714 gdb::function_view<symbol_found_callback_ftype> callback,
5715 symbol_compare_ftype *ordered_compare)
5716 {
5717 struct dwarf2_per_objfile *dwarf2_per_objfile
5718 = get_dwarf2_per_objfile (objfile);
5719
5720 /* debug_names_table is NULL if OBJF_READNOW. */
5721 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5722 return;
5723
5724 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5725 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5726
5727 const char *match_name = name.ada ().lookup_name ().c_str ();
5728 auto matcher = [&] (const char *symname)
5729 {
5730 if (ordered_compare == nullptr)
5731 return true;
5732 return ordered_compare (symname, match_name) == 0;
5733 };
5734
5735 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5736 [&] (offset_type namei)
5737 {
5738 /* The name was matched, now expand corresponding CUs that were
5739 marked. */
5740 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5741
5742 struct dwarf2_per_cu_data *per_cu;
5743 while ((per_cu = iter.next ()) != NULL)
5744 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5745 nullptr);
5746 return true;
5747 });
5748
5749 /* It's a shame we couldn't do this inside the
5750 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5751 that have already been expanded. Instead, this loop matches what
5752 the psymtab code does. */
5753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5754 {
5755 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5756 if (symtab != nullptr)
5757 {
5758 const struct block *block
5759 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5760 if (!iterate_over_symbols_terminated (block, name,
5761 domain, callback))
5762 break;
5763 }
5764 }
5765 }
5766
5767 static void
5768 dw2_debug_names_expand_symtabs_matching
5769 (struct objfile *objfile,
5770 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5771 const lookup_name_info *lookup_name,
5772 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5773 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5774 enum search_domain kind)
5775 {
5776 struct dwarf2_per_objfile *dwarf2_per_objfile
5777 = get_dwarf2_per_objfile (objfile);
5778
5779 /* debug_names_table is NULL if OBJF_READNOW. */
5780 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5781 return;
5782
5783 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5784
5785 if (symbol_matcher == NULL && lookup_name == NULL)
5786 {
5787 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5788 {
5789 QUIT;
5790
5791 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5792 file_matcher, expansion_notify);
5793 }
5794 return;
5795 }
5796
5797 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5798
5799 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5800 symbol_matcher,
5801 kind, [&] (offset_type namei)
5802 {
5803 /* The name was matched, now expand corresponding CUs that were
5804 marked. */
5805 dw2_debug_names_iterator iter (map, kind, namei);
5806
5807 struct dwarf2_per_cu_data *per_cu;
5808 while ((per_cu = iter.next ()) != NULL)
5809 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5810 file_matcher, expansion_notify);
5811 return true;
5812 });
5813 }
5814
5815 const struct quick_symbol_functions dwarf2_debug_names_functions =
5816 {
5817 dw2_has_symbols,
5818 dw2_find_last_source_symtab,
5819 dw2_forget_cached_source_info,
5820 dw2_map_symtabs_matching_filename,
5821 dw2_debug_names_lookup_symbol,
5822 NULL,
5823 dw2_print_stats,
5824 dw2_debug_names_dump,
5825 dw2_debug_names_expand_symtabs_for_function,
5826 dw2_expand_all_symtabs,
5827 dw2_expand_symtabs_with_fullname,
5828 dw2_debug_names_map_matching_symbols,
5829 dw2_debug_names_expand_symtabs_matching,
5830 dw2_find_pc_sect_compunit_symtab,
5831 NULL,
5832 dw2_map_symbol_filenames
5833 };
5834
5835 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5836 to either a dwarf2_per_bfd or dwz_file object. */
5837
5838 template <typename T>
5839 static gdb::array_view<const gdb_byte>
5840 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5841 {
5842 dwarf2_section_info *section = &section_owner->gdb_index;
5843
5844 if (section->empty ())
5845 return {};
5846
5847 /* Older elfutils strip versions could keep the section in the main
5848 executable while splitting it for the separate debug info file. */
5849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5850 return {};
5851
5852 section->read (obj);
5853
5854 /* dwarf2_section_info::size is a bfd_size_type, while
5855 gdb::array_view works with size_t. On 32-bit hosts, with
5856 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5857 is 32-bit. So we need an explicit narrowing conversion here.
5858 This is fine, because it's impossible to allocate or mmap an
5859 array/buffer larger than what size_t can represent. */
5860 return gdb::make_array_view (section->buffer, section->size);
5861 }
5862
5863 /* Lookup the index cache for the contents of the index associated to
5864 DWARF2_OBJ. */
5865
5866 static gdb::array_view<const gdb_byte>
5867 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5868 {
5869 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5870 if (build_id == nullptr)
5871 return {};
5872
5873 return global_index_cache.lookup_gdb_index (build_id,
5874 &dwarf2_per_bfd->index_cache_res);
5875 }
5876
5877 /* Same as the above, but for DWZ. */
5878
5879 static gdb::array_view<const gdb_byte>
5880 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5881 {
5882 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5883 if (build_id == nullptr)
5884 return {};
5885
5886 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5887 }
5888
5889 /* See symfile.h. */
5890
5891 bool
5892 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5893 {
5894 struct dwarf2_per_objfile *dwarf2_per_objfile
5895 = get_dwarf2_per_objfile (objfile);
5896
5897 /* If we're about to read full symbols, don't bother with the
5898 indices. In this case we also don't care if some other debug
5899 format is making psymtabs, because they are all about to be
5900 expanded anyway. */
5901 if ((objfile->flags & OBJF_READNOW))
5902 {
5903 dwarf2_per_objfile->per_bfd->using_index = 1;
5904 create_all_comp_units (dwarf2_per_objfile);
5905 create_all_type_units (dwarf2_per_objfile);
5906 dwarf2_per_objfile->per_bfd->quick_file_names_table
5907 = create_quick_file_names_table
5908 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5909 dwarf2_per_objfile->resize_symtabs ();
5910
5911 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5912 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5913 {
5914 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5915
5916 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5917 struct dwarf2_per_cu_quick_data);
5918 }
5919
5920 /* Return 1 so that gdb sees the "quick" functions. However,
5921 these functions will be no-ops because we will have expanded
5922 all symtabs. */
5923 *index_kind = dw_index_kind::GDB_INDEX;
5924 return true;
5925 }
5926
5927 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5928 {
5929 *index_kind = dw_index_kind::DEBUG_NAMES;
5930 dwarf2_per_objfile->resize_symtabs ();
5931 return true;
5932 }
5933
5934 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5935 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5936 get_gdb_index_contents_from_section<dwz_file>))
5937 {
5938 *index_kind = dw_index_kind::GDB_INDEX;
5939 dwarf2_per_objfile->resize_symtabs ();
5940 return true;
5941 }
5942
5943 /* ... otherwise, try to find the index in the index cache. */
5944 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5945 get_gdb_index_contents_from_cache,
5946 get_gdb_index_contents_from_cache_dwz))
5947 {
5948 global_index_cache.hit ();
5949 *index_kind = dw_index_kind::GDB_INDEX;
5950 dwarf2_per_objfile->resize_symtabs ();
5951 return true;
5952 }
5953
5954 global_index_cache.miss ();
5955 return false;
5956 }
5957
5958 \f
5959
5960 /* Build a partial symbol table. */
5961
5962 void
5963 dwarf2_build_psymtabs (struct objfile *objfile)
5964 {
5965 struct dwarf2_per_objfile *dwarf2_per_objfile
5966 = get_dwarf2_per_objfile (objfile);
5967
5968 init_psymbol_list (objfile, 1024);
5969
5970 try
5971 {
5972 /* This isn't really ideal: all the data we allocate on the
5973 objfile's obstack is still uselessly kept around. However,
5974 freeing it seems unsafe. */
5975 psymtab_discarder psymtabs (objfile);
5976 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5977 psymtabs.keep ();
5978
5979 dwarf2_per_objfile->resize_symtabs ();
5980
5981 /* (maybe) store an index in the cache. */
5982 global_index_cache.store (dwarf2_per_objfile);
5983 }
5984 catch (const gdb_exception_error &except)
5985 {
5986 exception_print (gdb_stderr, except);
5987 }
5988 }
5989
5990 /* Find the base address of the compilation unit for range lists and
5991 location lists. It will normally be specified by DW_AT_low_pc.
5992 In DWARF-3 draft 4, the base address could be overridden by
5993 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5994 compilation units with discontinuous ranges. */
5995
5996 static void
5997 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5998 {
5999 struct attribute *attr;
6000
6001 cu->base_address.reset ();
6002
6003 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6004 if (attr != nullptr)
6005 cu->base_address = attr->value_as_address ();
6006 else
6007 {
6008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6009 if (attr != nullptr)
6010 cu->base_address = attr->value_as_address ();
6011 }
6012 }
6013
6014 /* Helper function that returns the proper abbrev section for
6015 THIS_CU. */
6016
6017 static struct dwarf2_section_info *
6018 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6019 {
6020 struct dwarf2_section_info *abbrev;
6021 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6022
6023 if (this_cu->is_dwz)
6024 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6025 else
6026 abbrev = &per_bfd->abbrev;
6027
6028 return abbrev;
6029 }
6030
6031 /* Fetch the abbreviation table offset from a comp or type unit header. */
6032
6033 static sect_offset
6034 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6035 struct dwarf2_section_info *section,
6036 sect_offset sect_off)
6037 {
6038 bfd *abfd = section->get_bfd_owner ();
6039 const gdb_byte *info_ptr;
6040 unsigned int initial_length_size, offset_size;
6041 uint16_t version;
6042
6043 section->read (dwarf2_per_objfile->objfile);
6044 info_ptr = section->buffer + to_underlying (sect_off);
6045 read_initial_length (abfd, info_ptr, &initial_length_size);
6046 offset_size = initial_length_size == 4 ? 4 : 8;
6047 info_ptr += initial_length_size;
6048
6049 version = read_2_bytes (abfd, info_ptr);
6050 info_ptr += 2;
6051 if (version >= 5)
6052 {
6053 /* Skip unit type and address size. */
6054 info_ptr += 2;
6055 }
6056
6057 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6058 }
6059
6060 /* A partial symtab that is used only for include files. */
6061 struct dwarf2_include_psymtab : public partial_symtab
6062 {
6063 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6064 : partial_symtab (filename, objfile)
6065 {
6066 }
6067
6068 void read_symtab (struct objfile *objfile) override
6069 {
6070 /* It's an include file, no symbols to read for it.
6071 Everything is in the includer symtab. */
6072
6073 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6074 expansion of the includer psymtab. We use the dependencies[0] field to
6075 model the includer. But if we go the regular route of calling
6076 expand_psymtab here, and having expand_psymtab call expand_dependencies
6077 to expand the includer, we'll only use expand_psymtab on the includer
6078 (making it a non-toplevel psymtab), while if we expand the includer via
6079 another path, we'll use read_symtab (making it a toplevel psymtab).
6080 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6081 psymtab, and trigger read_symtab on the includer here directly. */
6082 includer ()->read_symtab (objfile);
6083 }
6084
6085 void expand_psymtab (struct objfile *objfile) override
6086 {
6087 /* This is not called by read_symtab, and should not be called by any
6088 expand_dependencies. */
6089 gdb_assert (false);
6090 }
6091
6092 bool readin_p (struct objfile *objfile) const override
6093 {
6094 return includer ()->readin_p (objfile);
6095 }
6096
6097 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6098 {
6099 return nullptr;
6100 }
6101
6102 private:
6103 partial_symtab *includer () const
6104 {
6105 /* An include psymtab has exactly one dependency: the psymtab that
6106 includes it. */
6107 gdb_assert (this->number_of_dependencies == 1);
6108 return this->dependencies[0];
6109 }
6110 };
6111
6112 /* Allocate a new partial symtab for file named NAME and mark this new
6113 partial symtab as being an include of PST. */
6114
6115 static void
6116 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6117 struct objfile *objfile)
6118 {
6119 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6120
6121 if (!IS_ABSOLUTE_PATH (subpst->filename))
6122 subpst->dirname = pst->dirname;
6123
6124 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6125 subpst->dependencies[0] = pst;
6126 subpst->number_of_dependencies = 1;
6127 }
6128
6129 /* Read the Line Number Program data and extract the list of files
6130 included by the source file represented by PST. Build an include
6131 partial symtab for each of these included files. */
6132
6133 static void
6134 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6135 struct die_info *die,
6136 dwarf2_psymtab *pst)
6137 {
6138 line_header_up lh;
6139 struct attribute *attr;
6140
6141 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6142 if (attr != nullptr)
6143 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6144 if (lh == NULL)
6145 return; /* No linetable, so no includes. */
6146
6147 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6148 that we pass in the raw text_low here; that is ok because we're
6149 only decoding the line table to make include partial symtabs, and
6150 so the addresses aren't really used. */
6151 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6152 pst->raw_text_low (), 1);
6153 }
6154
6155 static hashval_t
6156 hash_signatured_type (const void *item)
6157 {
6158 const struct signatured_type *sig_type
6159 = (const struct signatured_type *) item;
6160
6161 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6162 return sig_type->signature;
6163 }
6164
6165 static int
6166 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6167 {
6168 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6169 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6170
6171 return lhs->signature == rhs->signature;
6172 }
6173
6174 /* Allocate a hash table for signatured types. */
6175
6176 static htab_up
6177 allocate_signatured_type_table ()
6178 {
6179 return htab_up (htab_create_alloc (41,
6180 hash_signatured_type,
6181 eq_signatured_type,
6182 NULL, xcalloc, xfree));
6183 }
6184
6185 /* A helper function to add a signatured type CU to a table. */
6186
6187 static int
6188 add_signatured_type_cu_to_table (void **slot, void *datum)
6189 {
6190 struct signatured_type *sigt = (struct signatured_type *) *slot;
6191 std::vector<signatured_type *> *all_type_units
6192 = (std::vector<signatured_type *> *) datum;
6193
6194 all_type_units->push_back (sigt);
6195
6196 return 1;
6197 }
6198
6199 /* A helper for create_debug_types_hash_table. Read types from SECTION
6200 and fill them into TYPES_HTAB. It will process only type units,
6201 therefore DW_UT_type. */
6202
6203 static void
6204 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6205 struct dwo_file *dwo_file,
6206 dwarf2_section_info *section, htab_up &types_htab,
6207 rcuh_kind section_kind)
6208 {
6209 struct objfile *objfile = dwarf2_per_objfile->objfile;
6210 struct dwarf2_section_info *abbrev_section;
6211 bfd *abfd;
6212 const gdb_byte *info_ptr, *end_ptr;
6213
6214 abbrev_section = (dwo_file != NULL
6215 ? &dwo_file->sections.abbrev
6216 : &dwarf2_per_objfile->per_bfd->abbrev);
6217
6218 if (dwarf_read_debug)
6219 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6220 section->get_name (),
6221 abbrev_section->get_file_name ());
6222
6223 section->read (objfile);
6224 info_ptr = section->buffer;
6225
6226 if (info_ptr == NULL)
6227 return;
6228
6229 /* We can't set abfd until now because the section may be empty or
6230 not present, in which case the bfd is unknown. */
6231 abfd = section->get_bfd_owner ();
6232
6233 /* We don't use cutu_reader here because we don't need to read
6234 any dies: the signature is in the header. */
6235
6236 end_ptr = info_ptr + section->size;
6237 while (info_ptr < end_ptr)
6238 {
6239 struct signatured_type *sig_type;
6240 struct dwo_unit *dwo_tu;
6241 void **slot;
6242 const gdb_byte *ptr = info_ptr;
6243 struct comp_unit_head header;
6244 unsigned int length;
6245
6246 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6247
6248 /* Initialize it due to a false compiler warning. */
6249 header.signature = -1;
6250 header.type_cu_offset_in_tu = (cu_offset) -1;
6251
6252 /* We need to read the type's signature in order to build the hash
6253 table, but we don't need anything else just yet. */
6254
6255 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6256 abbrev_section, ptr, section_kind);
6257
6258 length = header.get_length ();
6259
6260 /* Skip dummy type units. */
6261 if (ptr >= info_ptr + length
6262 || peek_abbrev_code (abfd, ptr) == 0
6263 || header.unit_type != DW_UT_type)
6264 {
6265 info_ptr += length;
6266 continue;
6267 }
6268
6269 if (types_htab == NULL)
6270 {
6271 if (dwo_file)
6272 types_htab = allocate_dwo_unit_table ();
6273 else
6274 types_htab = allocate_signatured_type_table ();
6275 }
6276
6277 if (dwo_file)
6278 {
6279 sig_type = NULL;
6280 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6281 struct dwo_unit);
6282 dwo_tu->dwo_file = dwo_file;
6283 dwo_tu->signature = header.signature;
6284 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6285 dwo_tu->section = section;
6286 dwo_tu->sect_off = sect_off;
6287 dwo_tu->length = length;
6288 }
6289 else
6290 {
6291 /* N.B.: type_offset is not usable if this type uses a DWO file.
6292 The real type_offset is in the DWO file. */
6293 dwo_tu = NULL;
6294 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6295 sig_type->signature = header.signature;
6296 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6297 sig_type->per_cu.is_debug_types = 1;
6298 sig_type->per_cu.section = section;
6299 sig_type->per_cu.sect_off = sect_off;
6300 sig_type->per_cu.length = length;
6301 }
6302
6303 slot = htab_find_slot (types_htab.get (),
6304 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6305 INSERT);
6306 gdb_assert (slot != NULL);
6307 if (*slot != NULL)
6308 {
6309 sect_offset dup_sect_off;
6310
6311 if (dwo_file)
6312 {
6313 const struct dwo_unit *dup_tu
6314 = (const struct dwo_unit *) *slot;
6315
6316 dup_sect_off = dup_tu->sect_off;
6317 }
6318 else
6319 {
6320 const struct signatured_type *dup_tu
6321 = (const struct signatured_type *) *slot;
6322
6323 dup_sect_off = dup_tu->per_cu.sect_off;
6324 }
6325
6326 complaint (_("debug type entry at offset %s is duplicate to"
6327 " the entry at offset %s, signature %s"),
6328 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6329 hex_string (header.signature));
6330 }
6331 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6332
6333 if (dwarf_read_debug > 1)
6334 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6335 sect_offset_str (sect_off),
6336 hex_string (header.signature));
6337
6338 info_ptr += length;
6339 }
6340 }
6341
6342 /* Create the hash table of all entries in the .debug_types
6343 (or .debug_types.dwo) section(s).
6344 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6345 otherwise it is NULL.
6346
6347 The result is a pointer to the hash table or NULL if there are no types.
6348
6349 Note: This function processes DWO files only, not DWP files. */
6350
6351 static void
6352 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6353 struct dwo_file *dwo_file,
6354 gdb::array_view<dwarf2_section_info> type_sections,
6355 htab_up &types_htab)
6356 {
6357 for (dwarf2_section_info &section : type_sections)
6358 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6359 types_htab, rcuh_kind::TYPE);
6360 }
6361
6362 /* Create the hash table of all entries in the .debug_types section,
6363 and initialize all_type_units.
6364 The result is zero if there is an error (e.g. missing .debug_types section),
6365 otherwise non-zero. */
6366
6367 static int
6368 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6369 {
6370 htab_up types_htab;
6371
6372 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6373 &dwarf2_per_objfile->per_bfd->info, types_htab,
6374 rcuh_kind::COMPILE);
6375 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6376 dwarf2_per_objfile->per_bfd->types, types_htab);
6377 if (types_htab == NULL)
6378 {
6379 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6380 return 0;
6381 }
6382
6383 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6384
6385 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6386 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6387 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6388
6389 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6390 add_signatured_type_cu_to_table,
6391 &dwarf2_per_objfile->per_bfd->all_type_units);
6392
6393 return 1;
6394 }
6395
6396 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6397 If SLOT is non-NULL, it is the entry to use in the hash table.
6398 Otherwise we find one. */
6399
6400 static struct signatured_type *
6401 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6402 void **slot)
6403 {
6404 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6405 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6406 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6407
6408 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6409
6410 dwarf2_per_objfile->resize_symtabs ();
6411
6412 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6413 sig_type->signature = sig;
6414 sig_type->per_cu.is_debug_types = 1;
6415 if (dwarf2_per_objfile->per_bfd->using_index)
6416 {
6417 sig_type->per_cu.v.quick =
6418 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6419 struct dwarf2_per_cu_quick_data);
6420 }
6421
6422 if (slot == NULL)
6423 {
6424 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6425 sig_type, INSERT);
6426 }
6427 gdb_assert (*slot == NULL);
6428 *slot = sig_type;
6429 /* The rest of sig_type must be filled in by the caller. */
6430 return sig_type;
6431 }
6432
6433 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6434 Fill in SIG_ENTRY with DWO_ENTRY. */
6435
6436 static void
6437 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6438 struct signatured_type *sig_entry,
6439 struct dwo_unit *dwo_entry)
6440 {
6441 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6442
6443 /* Make sure we're not clobbering something we don't expect to. */
6444 gdb_assert (! sig_entry->per_cu.queued);
6445 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6446 if (per_bfd->using_index)
6447 {
6448 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6449 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6450 }
6451 else
6452 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6453 gdb_assert (sig_entry->signature == dwo_entry->signature);
6454 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6455 gdb_assert (sig_entry->type_unit_group == NULL);
6456 gdb_assert (sig_entry->dwo_unit == NULL);
6457
6458 sig_entry->per_cu.section = dwo_entry->section;
6459 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6460 sig_entry->per_cu.length = dwo_entry->length;
6461 sig_entry->per_cu.reading_dwo_directly = 1;
6462 sig_entry->per_cu.per_bfd = per_bfd;
6463 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6464 sig_entry->dwo_unit = dwo_entry;
6465 }
6466
6467 /* Subroutine of lookup_signatured_type.
6468 If we haven't read the TU yet, create the signatured_type data structure
6469 for a TU to be read in directly from a DWO file, bypassing the stub.
6470 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6471 using .gdb_index, then when reading a CU we want to stay in the DWO file
6472 containing that CU. Otherwise we could end up reading several other DWO
6473 files (due to comdat folding) to process the transitive closure of all the
6474 mentioned TUs, and that can be slow. The current DWO file will have every
6475 type signature that it needs.
6476 We only do this for .gdb_index because in the psymtab case we already have
6477 to read all the DWOs to build the type unit groups. */
6478
6479 static struct signatured_type *
6480 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6481 {
6482 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6483 struct dwo_file *dwo_file;
6484 struct dwo_unit find_dwo_entry, *dwo_entry;
6485 struct signatured_type find_sig_entry, *sig_entry;
6486 void **slot;
6487
6488 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6489
6490 /* If TU skeletons have been removed then we may not have read in any
6491 TUs yet. */
6492 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6493 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6494
6495 /* We only ever need to read in one copy of a signatured type.
6496 Use the global signatured_types array to do our own comdat-folding
6497 of types. If this is the first time we're reading this TU, and
6498 the TU has an entry in .gdb_index, replace the recorded data from
6499 .gdb_index with this TU. */
6500
6501 find_sig_entry.signature = sig;
6502 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6503 &find_sig_entry, INSERT);
6504 sig_entry = (struct signatured_type *) *slot;
6505
6506 /* We can get here with the TU already read, *or* in the process of being
6507 read. Don't reassign the global entry to point to this DWO if that's
6508 the case. Also note that if the TU is already being read, it may not
6509 have come from a DWO, the program may be a mix of Fission-compiled
6510 code and non-Fission-compiled code. */
6511
6512 /* Have we already tried to read this TU?
6513 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6514 needn't exist in the global table yet). */
6515 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6516 return sig_entry;
6517
6518 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6519 dwo_unit of the TU itself. */
6520 dwo_file = cu->dwo_unit->dwo_file;
6521
6522 /* Ok, this is the first time we're reading this TU. */
6523 if (dwo_file->tus == NULL)
6524 return NULL;
6525 find_dwo_entry.signature = sig;
6526 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6527 &find_dwo_entry);
6528 if (dwo_entry == NULL)
6529 return NULL;
6530
6531 /* If the global table doesn't have an entry for this TU, add one. */
6532 if (sig_entry == NULL)
6533 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6534
6535 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6536 sig_entry->per_cu.tu_read = 1;
6537 return sig_entry;
6538 }
6539
6540 /* Subroutine of lookup_signatured_type.
6541 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6542 then try the DWP file. If the TU stub (skeleton) has been removed then
6543 it won't be in .gdb_index. */
6544
6545 static struct signatured_type *
6546 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6547 {
6548 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6549 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6550 struct dwo_unit *dwo_entry;
6551 struct signatured_type find_sig_entry, *sig_entry;
6552 void **slot;
6553
6554 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6555 gdb_assert (dwp_file != NULL);
6556
6557 /* If TU skeletons have been removed then we may not have read in any
6558 TUs yet. */
6559 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6560 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6561
6562 find_sig_entry.signature = sig;
6563 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6564 &find_sig_entry, INSERT);
6565 sig_entry = (struct signatured_type *) *slot;
6566
6567 /* Have we already tried to read this TU?
6568 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6569 needn't exist in the global table yet). */
6570 if (sig_entry != NULL)
6571 return sig_entry;
6572
6573 if (dwp_file->tus == NULL)
6574 return NULL;
6575 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6576 sig, 1 /* is_debug_types */);
6577 if (dwo_entry == NULL)
6578 return NULL;
6579
6580 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6581 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6582
6583 return sig_entry;
6584 }
6585
6586 /* Lookup a signature based type for DW_FORM_ref_sig8.
6587 Returns NULL if signature SIG is not present in the table.
6588 It is up to the caller to complain about this. */
6589
6590 static struct signatured_type *
6591 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6592 {
6593 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6594
6595 if (cu->dwo_unit
6596 && dwarf2_per_objfile->per_bfd->using_index)
6597 {
6598 /* We're in a DWO/DWP file, and we're using .gdb_index.
6599 These cases require special processing. */
6600 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6601 return lookup_dwo_signatured_type (cu, sig);
6602 else
6603 return lookup_dwp_signatured_type (cu, sig);
6604 }
6605 else
6606 {
6607 struct signatured_type find_entry, *entry;
6608
6609 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6610 return NULL;
6611 find_entry.signature = sig;
6612 entry = ((struct signatured_type *)
6613 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6614 &find_entry));
6615 return entry;
6616 }
6617 }
6618
6619 /* Low level DIE reading support. */
6620
6621 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6622
6623 static void
6624 init_cu_die_reader (struct die_reader_specs *reader,
6625 struct dwarf2_cu *cu,
6626 struct dwarf2_section_info *section,
6627 struct dwo_file *dwo_file,
6628 struct abbrev_table *abbrev_table)
6629 {
6630 gdb_assert (section->readin && section->buffer != NULL);
6631 reader->abfd = section->get_bfd_owner ();
6632 reader->cu = cu;
6633 reader->dwo_file = dwo_file;
6634 reader->die_section = section;
6635 reader->buffer = section->buffer;
6636 reader->buffer_end = section->buffer + section->size;
6637 reader->abbrev_table = abbrev_table;
6638 }
6639
6640 /* Subroutine of cutu_reader to simplify it.
6641 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6642 There's just a lot of work to do, and cutu_reader is big enough
6643 already.
6644
6645 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6646 from it to the DIE in the DWO. If NULL we are skipping the stub.
6647 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6648 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6649 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6650 STUB_COMP_DIR may be non-NULL.
6651 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6652 are filled in with the info of the DIE from the DWO file.
6653 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6654 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6655 kept around for at least as long as *RESULT_READER.
6656
6657 The result is non-zero if a valid (non-dummy) DIE was found. */
6658
6659 static int
6660 read_cutu_die_from_dwo (dwarf2_cu *cu,
6661 struct dwo_unit *dwo_unit,
6662 struct die_info *stub_comp_unit_die,
6663 const char *stub_comp_dir,
6664 struct die_reader_specs *result_reader,
6665 const gdb_byte **result_info_ptr,
6666 struct die_info **result_comp_unit_die,
6667 abbrev_table_up *result_dwo_abbrev_table)
6668 {
6669 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6670 dwarf2_per_cu_data *per_cu = cu->per_cu;
6671 struct objfile *objfile = dwarf2_per_objfile->objfile;
6672 bfd *abfd;
6673 const gdb_byte *begin_info_ptr, *info_ptr;
6674 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6675 int i,num_extra_attrs;
6676 struct dwarf2_section_info *dwo_abbrev_section;
6677 struct die_info *comp_unit_die;
6678
6679 /* At most one of these may be provided. */
6680 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6681
6682 /* These attributes aren't processed until later:
6683 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6684 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6685 referenced later. However, these attributes are found in the stub
6686 which we won't have later. In order to not impose this complication
6687 on the rest of the code, we read them here and copy them to the
6688 DWO CU/TU die. */
6689
6690 stmt_list = NULL;
6691 low_pc = NULL;
6692 high_pc = NULL;
6693 ranges = NULL;
6694 comp_dir = NULL;
6695
6696 if (stub_comp_unit_die != NULL)
6697 {
6698 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6699 DWO file. */
6700 if (!per_cu->is_debug_types)
6701 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6702 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6703 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6704 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6705 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6706
6707 cu->addr_base = stub_comp_unit_die->addr_base ();
6708
6709 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6710 here (if needed). We need the value before we can process
6711 DW_AT_ranges. */
6712 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6713 }
6714 else if (stub_comp_dir != NULL)
6715 {
6716 /* Reconstruct the comp_dir attribute to simplify the code below. */
6717 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6718 comp_dir->name = DW_AT_comp_dir;
6719 comp_dir->form = DW_FORM_string;
6720 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6721 DW_STRING (comp_dir) = stub_comp_dir;
6722 }
6723
6724 /* Set up for reading the DWO CU/TU. */
6725 cu->dwo_unit = dwo_unit;
6726 dwarf2_section_info *section = dwo_unit->section;
6727 section->read (objfile);
6728 abfd = section->get_bfd_owner ();
6729 begin_info_ptr = info_ptr = (section->buffer
6730 + to_underlying (dwo_unit->sect_off));
6731 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6732
6733 if (per_cu->is_debug_types)
6734 {
6735 signatured_type *sig_type = (struct signatured_type *) per_cu;
6736
6737 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6738 &cu->header, section,
6739 dwo_abbrev_section,
6740 info_ptr, rcuh_kind::TYPE);
6741 /* This is not an assert because it can be caused by bad debug info. */
6742 if (sig_type->signature != cu->header.signature)
6743 {
6744 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6745 " TU at offset %s [in module %s]"),
6746 hex_string (sig_type->signature),
6747 hex_string (cu->header.signature),
6748 sect_offset_str (dwo_unit->sect_off),
6749 bfd_get_filename (abfd));
6750 }
6751 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6752 /* For DWOs coming from DWP files, we don't know the CU length
6753 nor the type's offset in the TU until now. */
6754 dwo_unit->length = cu->header.get_length ();
6755 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6756
6757 /* Establish the type offset that can be used to lookup the type.
6758 For DWO files, we don't know it until now. */
6759 sig_type->type_offset_in_section
6760 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6761 }
6762 else
6763 {
6764 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6765 &cu->header, section,
6766 dwo_abbrev_section,
6767 info_ptr, rcuh_kind::COMPILE);
6768 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6769 /* For DWOs coming from DWP files, we don't know the CU length
6770 until now. */
6771 dwo_unit->length = cu->header.get_length ();
6772 }
6773
6774 *result_dwo_abbrev_table
6775 = abbrev_table::read (objfile, dwo_abbrev_section,
6776 cu->header.abbrev_sect_off);
6777 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6778 result_dwo_abbrev_table->get ());
6779
6780 /* Read in the die, but leave space to copy over the attributes
6781 from the stub. This has the benefit of simplifying the rest of
6782 the code - all the work to maintain the illusion of a single
6783 DW_TAG_{compile,type}_unit DIE is done here. */
6784 num_extra_attrs = ((stmt_list != NULL)
6785 + (low_pc != NULL)
6786 + (high_pc != NULL)
6787 + (ranges != NULL)
6788 + (comp_dir != NULL));
6789 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6790 num_extra_attrs);
6791
6792 /* Copy over the attributes from the stub to the DIE we just read in. */
6793 comp_unit_die = *result_comp_unit_die;
6794 i = comp_unit_die->num_attrs;
6795 if (stmt_list != NULL)
6796 comp_unit_die->attrs[i++] = *stmt_list;
6797 if (low_pc != NULL)
6798 comp_unit_die->attrs[i++] = *low_pc;
6799 if (high_pc != NULL)
6800 comp_unit_die->attrs[i++] = *high_pc;
6801 if (ranges != NULL)
6802 comp_unit_die->attrs[i++] = *ranges;
6803 if (comp_dir != NULL)
6804 comp_unit_die->attrs[i++] = *comp_dir;
6805 comp_unit_die->num_attrs += num_extra_attrs;
6806
6807 if (dwarf_die_debug)
6808 {
6809 fprintf_unfiltered (gdb_stdlog,
6810 "Read die from %s@0x%x of %s:\n",
6811 section->get_name (),
6812 (unsigned) (begin_info_ptr - section->buffer),
6813 bfd_get_filename (abfd));
6814 dump_die (comp_unit_die, dwarf_die_debug);
6815 }
6816
6817 /* Skip dummy compilation units. */
6818 if (info_ptr >= begin_info_ptr + dwo_unit->length
6819 || peek_abbrev_code (abfd, info_ptr) == 0)
6820 return 0;
6821
6822 *result_info_ptr = info_ptr;
6823 return 1;
6824 }
6825
6826 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6827 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6828 signature is part of the header. */
6829 static gdb::optional<ULONGEST>
6830 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6831 {
6832 if (cu->header.version >= 5)
6833 return cu->header.signature;
6834 struct attribute *attr;
6835 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6836 if (attr == nullptr)
6837 return gdb::optional<ULONGEST> ();
6838 return DW_UNSND (attr);
6839 }
6840
6841 /* Subroutine of cutu_reader to simplify it.
6842 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6843 Returns NULL if the specified DWO unit cannot be found. */
6844
6845 static struct dwo_unit *
6846 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6847 {
6848 dwarf2_per_cu_data *per_cu = cu->per_cu;
6849 struct dwo_unit *dwo_unit;
6850 const char *comp_dir;
6851
6852 gdb_assert (cu != NULL);
6853
6854 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6855 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6856 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6857
6858 if (per_cu->is_debug_types)
6859 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6860 else
6861 {
6862 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6863
6864 if (!signature.has_value ())
6865 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6866 " [in module %s]"),
6867 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6868
6869 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6870 }
6871
6872 return dwo_unit;
6873 }
6874
6875 /* Subroutine of cutu_reader to simplify it.
6876 See it for a description of the parameters.
6877 Read a TU directly from a DWO file, bypassing the stub. */
6878
6879 void
6880 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6881 dwarf2_per_objfile *per_objfile,
6882 dwarf2_cu *existing_cu)
6883 {
6884 struct signatured_type *sig_type;
6885
6886 /* Verify we can do the following downcast, and that we have the
6887 data we need. */
6888 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6889 sig_type = (struct signatured_type *) this_cu;
6890 gdb_assert (sig_type->dwo_unit != NULL);
6891
6892 dwarf2_cu *cu;
6893
6894 if (existing_cu != nullptr)
6895 {
6896 cu = existing_cu;
6897 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6898 /* There's no need to do the rereading_dwo_cu handling that
6899 cutu_reader does since we don't read the stub. */
6900 }
6901 else
6902 {
6903 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6904 in per_objfile yet. */
6905 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6906 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6907 cu = m_new_cu.get ();
6908 }
6909
6910 /* A future optimization, if needed, would be to use an existing
6911 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6912 could share abbrev tables. */
6913
6914 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6915 NULL /* stub_comp_unit_die */,
6916 sig_type->dwo_unit->dwo_file->comp_dir,
6917 this, &info_ptr,
6918 &comp_unit_die,
6919 &m_dwo_abbrev_table) == 0)
6920 {
6921 /* Dummy die. */
6922 dummy_p = true;
6923 }
6924 }
6925
6926 /* Initialize a CU (or TU) and read its DIEs.
6927 If the CU defers to a DWO file, read the DWO file as well.
6928
6929 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6930 Otherwise the table specified in the comp unit header is read in and used.
6931 This is an optimization for when we already have the abbrev table.
6932
6933 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6934 allocated. */
6935
6936 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6937 dwarf2_per_objfile *dwarf2_per_objfile,
6938 struct abbrev_table *abbrev_table,
6939 dwarf2_cu *existing_cu,
6940 bool skip_partial)
6941 : die_reader_specs {},
6942 m_this_cu (this_cu)
6943 {
6944 struct objfile *objfile = dwarf2_per_objfile->objfile;
6945 struct dwarf2_section_info *section = this_cu->section;
6946 bfd *abfd = section->get_bfd_owner ();
6947 const gdb_byte *begin_info_ptr;
6948 struct signatured_type *sig_type = NULL;
6949 struct dwarf2_section_info *abbrev_section;
6950 /* Non-zero if CU currently points to a DWO file and we need to
6951 reread it. When this happens we need to reread the skeleton die
6952 before we can reread the DWO file (this only applies to CUs, not TUs). */
6953 int rereading_dwo_cu = 0;
6954
6955 if (dwarf_die_debug)
6956 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6957 this_cu->is_debug_types ? "type" : "comp",
6958 sect_offset_str (this_cu->sect_off));
6959
6960 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6961 file (instead of going through the stub), short-circuit all of this. */
6962 if (this_cu->reading_dwo_directly)
6963 {
6964 /* Narrow down the scope of possibilities to have to understand. */
6965 gdb_assert (this_cu->is_debug_types);
6966 gdb_assert (abbrev_table == NULL);
6967 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
6968 return;
6969 }
6970
6971 /* This is cheap if the section is already read in. */
6972 section->read (objfile);
6973
6974 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6975
6976 abbrev_section = get_abbrev_section_for_cu (this_cu);
6977
6978 dwarf2_cu *cu;
6979
6980 if (existing_cu != nullptr)
6981 {
6982 cu = existing_cu;
6983 /* If this CU is from a DWO file we need to start over, we need to
6984 refetch the attributes from the skeleton CU.
6985 This could be optimized by retrieving those attributes from when we
6986 were here the first time: the previous comp_unit_die was stored in
6987 comp_unit_obstack. But there's no data yet that we need this
6988 optimization. */
6989 if (cu->dwo_unit != NULL)
6990 rereading_dwo_cu = 1;
6991 }
6992 else
6993 {
6994 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6995 in per_objfile yet. */
6996 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
6997 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
6998 cu = m_new_cu.get ();
6999 }
7000
7001 /* Get the header. */
7002 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7003 {
7004 /* We already have the header, there's no need to read it in again. */
7005 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7006 }
7007 else
7008 {
7009 if (this_cu->is_debug_types)
7010 {
7011 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7012 &cu->header, section,
7013 abbrev_section, info_ptr,
7014 rcuh_kind::TYPE);
7015
7016 /* Since per_cu is the first member of struct signatured_type,
7017 we can go from a pointer to one to a pointer to the other. */
7018 sig_type = (struct signatured_type *) this_cu;
7019 gdb_assert (sig_type->signature == cu->header.signature);
7020 gdb_assert (sig_type->type_offset_in_tu
7021 == cu->header.type_cu_offset_in_tu);
7022 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7023
7024 /* LENGTH has not been set yet for type units if we're
7025 using .gdb_index. */
7026 this_cu->length = cu->header.get_length ();
7027
7028 /* Establish the type offset that can be used to lookup the type. */
7029 sig_type->type_offset_in_section =
7030 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7031
7032 this_cu->dwarf_version = cu->header.version;
7033 }
7034 else
7035 {
7036 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7037 &cu->header, section,
7038 abbrev_section,
7039 info_ptr,
7040 rcuh_kind::COMPILE);
7041
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7043 if (this_cu->length == 0)
7044 this_cu->length = cu->header.get_length ();
7045 else
7046 gdb_assert (this_cu->length == cu->header.get_length ());
7047 this_cu->dwarf_version = cu->header.version;
7048 }
7049 }
7050
7051 /* Skip dummy compilation units. */
7052 if (info_ptr >= begin_info_ptr + this_cu->length
7053 || peek_abbrev_code (abfd, info_ptr) == 0)
7054 {
7055 dummy_p = true;
7056 return;
7057 }
7058
7059 /* If we don't have them yet, read the abbrevs for this compilation unit.
7060 And if we need to read them now, make sure they're freed when we're
7061 done. */
7062 if (abbrev_table != NULL)
7063 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7064 else
7065 {
7066 m_abbrev_table_holder
7067 = abbrev_table::read (objfile, abbrev_section,
7068 cu->header.abbrev_sect_off);
7069 abbrev_table = m_abbrev_table_holder.get ();
7070 }
7071
7072 /* Read the top level CU/TU die. */
7073 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7074 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7075
7076 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7077 {
7078 dummy_p = true;
7079 return;
7080 }
7081
7082 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7083 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7084 table from the DWO file and pass the ownership over to us. It will be
7085 referenced from READER, so we must make sure to free it after we're done
7086 with READER.
7087
7088 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7089 DWO CU, that this test will fail (the attribute will not be present). */
7090 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7091 if (dwo_name != nullptr)
7092 {
7093 struct dwo_unit *dwo_unit;
7094 struct die_info *dwo_comp_unit_die;
7095
7096 if (comp_unit_die->has_children)
7097 {
7098 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7099 " has children (offset %s) [in module %s]"),
7100 sect_offset_str (this_cu->sect_off),
7101 bfd_get_filename (abfd));
7102 }
7103 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7104 if (dwo_unit != NULL)
7105 {
7106 if (read_cutu_die_from_dwo (cu, dwo_unit,
7107 comp_unit_die, NULL,
7108 this, &info_ptr,
7109 &dwo_comp_unit_die,
7110 &m_dwo_abbrev_table) == 0)
7111 {
7112 /* Dummy die. */
7113 dummy_p = true;
7114 return;
7115 }
7116 comp_unit_die = dwo_comp_unit_die;
7117 }
7118 else
7119 {
7120 /* Yikes, we couldn't find the rest of the DIE, we only have
7121 the stub. A complaint has already been logged. There's
7122 not much more we can do except pass on the stub DIE to
7123 die_reader_func. We don't want to throw an error on bad
7124 debug info. */
7125 }
7126 }
7127 }
7128
7129 void
7130 cutu_reader::keep ()
7131 {
7132 /* Done, clean up. */
7133 gdb_assert (!dummy_p);
7134 if (m_new_cu != NULL)
7135 {
7136 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7137 now. */
7138 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7139 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7140 }
7141 }
7142
7143 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7144 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7145 assumed to have already done the lookup to find the DWO file).
7146
7147 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7148 THIS_CU->is_debug_types, but nothing else.
7149
7150 We fill in THIS_CU->length.
7151
7152 THIS_CU->cu is always freed when done.
7153 This is done in order to not leave THIS_CU->cu in a state where we have
7154 to care whether it refers to the "main" CU or the DWO CU.
7155
7156 When parent_cu is passed, it is used to provide a default value for
7157 str_offsets_base and addr_base from the parent. */
7158
7159 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7160 dwarf2_per_objfile *dwarf2_per_objfile,
7161 struct dwarf2_cu *parent_cu,
7162 struct dwo_file *dwo_file)
7163 : die_reader_specs {},
7164 m_this_cu (this_cu)
7165 {
7166 struct objfile *objfile = dwarf2_per_objfile->objfile;
7167 struct dwarf2_section_info *section = this_cu->section;
7168 bfd *abfd = section->get_bfd_owner ();
7169 struct dwarf2_section_info *abbrev_section;
7170 const gdb_byte *begin_info_ptr, *info_ptr;
7171
7172 if (dwarf_die_debug)
7173 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7174 this_cu->is_debug_types ? "type" : "comp",
7175 sect_offset_str (this_cu->sect_off));
7176
7177 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7178
7179 abbrev_section = (dwo_file != NULL
7180 ? &dwo_file->sections.abbrev
7181 : get_abbrev_section_for_cu (this_cu));
7182
7183 /* This is cheap if the section is already read in. */
7184 section->read (objfile);
7185
7186 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7187
7188 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7189 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7190 &m_new_cu->header, section,
7191 abbrev_section, info_ptr,
7192 (this_cu->is_debug_types
7193 ? rcuh_kind::TYPE
7194 : rcuh_kind::COMPILE));
7195
7196 if (parent_cu != nullptr)
7197 {
7198 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7199 m_new_cu->addr_base = parent_cu->addr_base;
7200 }
7201 this_cu->length = m_new_cu->header.get_length ();
7202
7203 /* Skip dummy compilation units. */
7204 if (info_ptr >= begin_info_ptr + this_cu->length
7205 || peek_abbrev_code (abfd, info_ptr) == 0)
7206 {
7207 dummy_p = true;
7208 return;
7209 }
7210
7211 m_abbrev_table_holder
7212 = abbrev_table::read (objfile, abbrev_section,
7213 m_new_cu->header.abbrev_sect_off);
7214
7215 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7216 m_abbrev_table_holder.get ());
7217 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7218 }
7219
7220 \f
7221 /* Type Unit Groups.
7222
7223 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7224 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7225 so that all types coming from the same compilation (.o file) are grouped
7226 together. A future step could be to put the types in the same symtab as
7227 the CU the types ultimately came from. */
7228
7229 static hashval_t
7230 hash_type_unit_group (const void *item)
7231 {
7232 const struct type_unit_group *tu_group
7233 = (const struct type_unit_group *) item;
7234
7235 return hash_stmt_list_entry (&tu_group->hash);
7236 }
7237
7238 static int
7239 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7240 {
7241 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7242 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7243
7244 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7245 }
7246
7247 /* Allocate a hash table for type unit groups. */
7248
7249 static htab_up
7250 allocate_type_unit_groups_table ()
7251 {
7252 return htab_up (htab_create_alloc (3,
7253 hash_type_unit_group,
7254 eq_type_unit_group,
7255 NULL, xcalloc, xfree));
7256 }
7257
7258 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7259 partial symtabs. We combine several TUs per psymtab to not let the size
7260 of any one psymtab grow too big. */
7261 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7262 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7263
7264 /* Helper routine for get_type_unit_group.
7265 Create the type_unit_group object used to hold one or more TUs. */
7266
7267 static struct type_unit_group *
7268 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7269 {
7270 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7271 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7272 struct dwarf2_per_cu_data *per_cu;
7273 struct type_unit_group *tu_group;
7274
7275 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7276 struct type_unit_group);
7277 per_cu = &tu_group->per_cu;
7278 per_cu->per_bfd = per_bfd;
7279
7280 if (per_bfd->using_index)
7281 {
7282 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7283 struct dwarf2_per_cu_quick_data);
7284 }
7285 else
7286 {
7287 unsigned int line_offset = to_underlying (line_offset_struct);
7288 dwarf2_psymtab *pst;
7289 std::string name;
7290
7291 /* Give the symtab a useful name for debug purposes. */
7292 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7293 name = string_printf ("<type_units_%d>",
7294 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7295 else
7296 name = string_printf ("<type_units_at_0x%x>", line_offset);
7297
7298 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7299 pst->anonymous = true;
7300 }
7301
7302 tu_group->hash.dwo_unit = cu->dwo_unit;
7303 tu_group->hash.line_sect_off = line_offset_struct;
7304
7305 return tu_group;
7306 }
7307
7308 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7309 STMT_LIST is a DW_AT_stmt_list attribute. */
7310
7311 static struct type_unit_group *
7312 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7313 {
7314 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7315 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7316 struct type_unit_group *tu_group;
7317 void **slot;
7318 unsigned int line_offset;
7319 struct type_unit_group type_unit_group_for_lookup;
7320
7321 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7322 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7323
7324 /* Do we need to create a new group, or can we use an existing one? */
7325
7326 if (stmt_list)
7327 {
7328 line_offset = DW_UNSND (stmt_list);
7329 ++tu_stats->nr_symtab_sharers;
7330 }
7331 else
7332 {
7333 /* Ugh, no stmt_list. Rare, but we have to handle it.
7334 We can do various things here like create one group per TU or
7335 spread them over multiple groups to split up the expansion work.
7336 To avoid worst case scenarios (too many groups or too large groups)
7337 we, umm, group them in bunches. */
7338 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7339 | (tu_stats->nr_stmt_less_type_units
7340 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7341 ++tu_stats->nr_stmt_less_type_units;
7342 }
7343
7344 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7345 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7346 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7347 &type_unit_group_for_lookup, INSERT);
7348 if (*slot != NULL)
7349 {
7350 tu_group = (struct type_unit_group *) *slot;
7351 gdb_assert (tu_group != NULL);
7352 }
7353 else
7354 {
7355 sect_offset line_offset_struct = (sect_offset) line_offset;
7356 tu_group = create_type_unit_group (cu, line_offset_struct);
7357 *slot = tu_group;
7358 ++tu_stats->nr_symtabs;
7359 }
7360
7361 return tu_group;
7362 }
7363 \f
7364 /* Partial symbol tables. */
7365
7366 /* Create a psymtab named NAME and assign it to PER_CU.
7367
7368 The caller must fill in the following details:
7369 dirname, textlow, texthigh. */
7370
7371 static dwarf2_psymtab *
7372 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7373 dwarf2_per_objfile *per_objfile,
7374 const char *name)
7375 {
7376 struct objfile *objfile = per_objfile->objfile;
7377 dwarf2_psymtab *pst;
7378
7379 pst = new dwarf2_psymtab (name, objfile, per_cu);
7380
7381 pst->psymtabs_addrmap_supported = true;
7382
7383 /* This is the glue that links PST into GDB's symbol API. */
7384 per_cu->v.psymtab = pst;
7385
7386 return pst;
7387 }
7388
7389 /* DIE reader function for process_psymtab_comp_unit. */
7390
7391 static void
7392 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7393 const gdb_byte *info_ptr,
7394 struct die_info *comp_unit_die,
7395 enum language pretend_language)
7396 {
7397 struct dwarf2_cu *cu = reader->cu;
7398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7399 struct objfile *objfile = per_objfile->objfile;
7400 struct gdbarch *gdbarch = objfile->arch ();
7401 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7402 CORE_ADDR baseaddr;
7403 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7404 dwarf2_psymtab *pst;
7405 enum pc_bounds_kind cu_bounds_kind;
7406 const char *filename;
7407
7408 gdb_assert (! per_cu->is_debug_types);
7409
7410 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7411
7412 /* Allocate a new partial symbol table structure. */
7413 gdb::unique_xmalloc_ptr<char> debug_filename;
7414 static const char artificial[] = "<artificial>";
7415 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7416 if (filename == NULL)
7417 filename = "";
7418 else if (strcmp (filename, artificial) == 0)
7419 {
7420 debug_filename.reset (concat (artificial, "@",
7421 sect_offset_str (per_cu->sect_off),
7422 (char *) NULL));
7423 filename = debug_filename.get ();
7424 }
7425
7426 pst = create_partial_symtab (per_cu, per_objfile, filename);
7427
7428 /* This must be done before calling dwarf2_build_include_psymtabs. */
7429 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7430
7431 baseaddr = objfile->text_section_offset ();
7432
7433 dwarf2_find_base_address (comp_unit_die, cu);
7434
7435 /* Possibly set the default values of LOWPC and HIGHPC from
7436 `DW_AT_ranges'. */
7437 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7438 &best_highpc, cu, pst);
7439 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7440 {
7441 CORE_ADDR low
7442 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7443 - baseaddr);
7444 CORE_ADDR high
7445 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7446 - baseaddr - 1);
7447 /* Store the contiguous range if it is not empty; it can be
7448 empty for CUs with no code. */
7449 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7450 low, high, pst);
7451 }
7452
7453 /* Check if comp unit has_children.
7454 If so, read the rest of the partial symbols from this comp unit.
7455 If not, there's no more debug_info for this comp unit. */
7456 if (comp_unit_die->has_children)
7457 {
7458 struct partial_die_info *first_die;
7459 CORE_ADDR lowpc, highpc;
7460
7461 lowpc = ((CORE_ADDR) -1);
7462 highpc = ((CORE_ADDR) 0);
7463
7464 first_die = load_partial_dies (reader, info_ptr, 1);
7465
7466 scan_partial_symbols (first_die, &lowpc, &highpc,
7467 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7468
7469 /* If we didn't find a lowpc, set it to highpc to avoid
7470 complaints from `maint check'. */
7471 if (lowpc == ((CORE_ADDR) -1))
7472 lowpc = highpc;
7473
7474 /* If the compilation unit didn't have an explicit address range,
7475 then use the information extracted from its child dies. */
7476 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7477 {
7478 best_lowpc = lowpc;
7479 best_highpc = highpc;
7480 }
7481 }
7482 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7483 best_lowpc + baseaddr)
7484 - baseaddr);
7485 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7486 best_highpc + baseaddr)
7487 - baseaddr);
7488
7489 end_psymtab_common (objfile, pst);
7490
7491 if (!cu->per_cu->imported_symtabs_empty ())
7492 {
7493 int i;
7494 int len = cu->per_cu->imported_symtabs_size ();
7495
7496 /* Fill in 'dependencies' here; we fill in 'users' in a
7497 post-pass. */
7498 pst->number_of_dependencies = len;
7499 pst->dependencies
7500 = objfile->partial_symtabs->allocate_dependencies (len);
7501 for (i = 0; i < len; ++i)
7502 {
7503 pst->dependencies[i]
7504 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7505 }
7506
7507 cu->per_cu->imported_symtabs_free ();
7508 }
7509
7510 /* Get the list of files included in the current compilation unit,
7511 and build a psymtab for each of them. */
7512 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7513
7514 if (dwarf_read_debug)
7515 fprintf_unfiltered (gdb_stdlog,
7516 "Psymtab for %s unit @%s: %s - %s"
7517 ", %d global, %d static syms\n",
7518 per_cu->is_debug_types ? "type" : "comp",
7519 sect_offset_str (per_cu->sect_off),
7520 paddress (gdbarch, pst->text_low (objfile)),
7521 paddress (gdbarch, pst->text_high (objfile)),
7522 pst->n_global_syms, pst->n_static_syms);
7523 }
7524
7525 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7526 Process compilation unit THIS_CU for a psymtab. */
7527
7528 static void
7529 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7530 dwarf2_per_objfile *per_objfile,
7531 bool want_partial_unit,
7532 enum language pretend_language)
7533 {
7534 /* If this compilation unit was already read in, free the
7535 cached copy in order to read it in again. This is
7536 necessary because we skipped some symbols when we first
7537 read in the compilation unit (see load_partial_dies).
7538 This problem could be avoided, but the benefit is unclear. */
7539 per_objfile->remove_cu (this_cu);
7540
7541 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7542
7543 switch (reader.comp_unit_die->tag)
7544 {
7545 case DW_TAG_compile_unit:
7546 this_cu->unit_type = DW_UT_compile;
7547 break;
7548 case DW_TAG_partial_unit:
7549 this_cu->unit_type = DW_UT_partial;
7550 break;
7551 default:
7552 abort ();
7553 }
7554
7555 if (reader.dummy_p)
7556 {
7557 /* Nothing. */
7558 }
7559 else if (this_cu->is_debug_types)
7560 build_type_psymtabs_reader (&reader, reader.info_ptr,
7561 reader.comp_unit_die);
7562 else if (want_partial_unit
7563 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7564 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7565 reader.comp_unit_die,
7566 pretend_language);
7567
7568 this_cu->lang = reader.cu->language;
7569
7570 /* Age out any secondary CUs. */
7571 per_objfile->age_comp_units ();
7572 }
7573
7574 /* Reader function for build_type_psymtabs. */
7575
7576 static void
7577 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7578 const gdb_byte *info_ptr,
7579 struct die_info *type_unit_die)
7580 {
7581 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7582 struct objfile *objfile = dwarf2_per_objfile->objfile;
7583 struct dwarf2_cu *cu = reader->cu;
7584 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7585 struct signatured_type *sig_type;
7586 struct type_unit_group *tu_group;
7587 struct attribute *attr;
7588 struct partial_die_info *first_die;
7589 CORE_ADDR lowpc, highpc;
7590 dwarf2_psymtab *pst;
7591
7592 gdb_assert (per_cu->is_debug_types);
7593 sig_type = (struct signatured_type *) per_cu;
7594
7595 if (! type_unit_die->has_children)
7596 return;
7597
7598 attr = type_unit_die->attr (DW_AT_stmt_list);
7599 tu_group = get_type_unit_group (cu, attr);
7600
7601 if (tu_group->tus == nullptr)
7602 tu_group->tus = new std::vector<signatured_type *>;
7603 tu_group->tus->push_back (sig_type);
7604
7605 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7606 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7607 pst->anonymous = true;
7608
7609 first_die = load_partial_dies (reader, info_ptr, 1);
7610
7611 lowpc = (CORE_ADDR) -1;
7612 highpc = (CORE_ADDR) 0;
7613 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7614
7615 end_psymtab_common (objfile, pst);
7616 }
7617
7618 /* Struct used to sort TUs by their abbreviation table offset. */
7619
7620 struct tu_abbrev_offset
7621 {
7622 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7623 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7624 {}
7625
7626 signatured_type *sig_type;
7627 sect_offset abbrev_offset;
7628 };
7629
7630 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7631
7632 static bool
7633 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7634 const struct tu_abbrev_offset &b)
7635 {
7636 return a.abbrev_offset < b.abbrev_offset;
7637 }
7638
7639 /* Efficiently read all the type units.
7640 This does the bulk of the work for build_type_psymtabs.
7641
7642 The efficiency is because we sort TUs by the abbrev table they use and
7643 only read each abbrev table once. In one program there are 200K TUs
7644 sharing 8K abbrev tables.
7645
7646 The main purpose of this function is to support building the
7647 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7648 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7649 can collapse the search space by grouping them by stmt_list.
7650 The savings can be significant, in the same program from above the 200K TUs
7651 share 8K stmt_list tables.
7652
7653 FUNC is expected to call get_type_unit_group, which will create the
7654 struct type_unit_group if necessary and add it to
7655 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7656
7657 static void
7658 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7659 {
7660 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7661 abbrev_table_up abbrev_table;
7662 sect_offset abbrev_offset;
7663
7664 /* It's up to the caller to not call us multiple times. */
7665 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7666
7667 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7668 return;
7669
7670 /* TUs typically share abbrev tables, and there can be way more TUs than
7671 abbrev tables. Sort by abbrev table to reduce the number of times we
7672 read each abbrev table in.
7673 Alternatives are to punt or to maintain a cache of abbrev tables.
7674 This is simpler and efficient enough for now.
7675
7676 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7677 symtab to use). Typically TUs with the same abbrev offset have the same
7678 stmt_list value too so in practice this should work well.
7679
7680 The basic algorithm here is:
7681
7682 sort TUs by abbrev table
7683 for each TU with same abbrev table:
7684 read abbrev table if first user
7685 read TU top level DIE
7686 [IWBN if DWO skeletons had DW_AT_stmt_list]
7687 call FUNC */
7688
7689 if (dwarf_read_debug)
7690 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7691
7692 /* Sort in a separate table to maintain the order of all_type_units
7693 for .gdb_index: TU indices directly index all_type_units. */
7694 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7695 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7696
7697 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7698 sorted_by_abbrev.emplace_back
7699 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7700 sig_type->per_cu.section,
7701 sig_type->per_cu.sect_off));
7702
7703 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7704 sort_tu_by_abbrev_offset);
7705
7706 abbrev_offset = (sect_offset) ~(unsigned) 0;
7707
7708 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7709 {
7710 /* Switch to the next abbrev table if necessary. */
7711 if (abbrev_table == NULL
7712 || tu.abbrev_offset != abbrev_offset)
7713 {
7714 abbrev_offset = tu.abbrev_offset;
7715 abbrev_table =
7716 abbrev_table::read (dwarf2_per_objfile->objfile,
7717 &dwarf2_per_objfile->per_bfd->abbrev,
7718 abbrev_offset);
7719 ++tu_stats->nr_uniq_abbrev_tables;
7720 }
7721
7722 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7723 abbrev_table.get (), nullptr, false);
7724 if (!reader.dummy_p)
7725 build_type_psymtabs_reader (&reader, reader.info_ptr,
7726 reader.comp_unit_die);
7727 }
7728 }
7729
7730 /* Print collected type unit statistics. */
7731
7732 static void
7733 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7734 {
7735 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7736
7737 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7738 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7739 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7740 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7741 tu_stats->nr_uniq_abbrev_tables);
7742 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7743 tu_stats->nr_symtabs);
7744 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7745 tu_stats->nr_symtab_sharers);
7746 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7747 tu_stats->nr_stmt_less_type_units);
7748 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7749 tu_stats->nr_all_type_units_reallocs);
7750 }
7751
7752 /* Traversal function for build_type_psymtabs. */
7753
7754 static int
7755 build_type_psymtab_dependencies (void **slot, void *info)
7756 {
7757 struct dwarf2_per_objfile *dwarf2_per_objfile
7758 = (struct dwarf2_per_objfile *) info;
7759 struct objfile *objfile = dwarf2_per_objfile->objfile;
7760 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7761 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7762 dwarf2_psymtab *pst = per_cu->v.psymtab;
7763 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7764 int i;
7765
7766 gdb_assert (len > 0);
7767 gdb_assert (per_cu->type_unit_group_p ());
7768
7769 pst->number_of_dependencies = len;
7770 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7771 for (i = 0; i < len; ++i)
7772 {
7773 struct signatured_type *iter = tu_group->tus->at (i);
7774 gdb_assert (iter->per_cu.is_debug_types);
7775 pst->dependencies[i] = iter->per_cu.v.psymtab;
7776 iter->type_unit_group = tu_group;
7777 }
7778
7779 delete tu_group->tus;
7780 tu_group->tus = nullptr;
7781
7782 return 1;
7783 }
7784
7785 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7786 Build partial symbol tables for the .debug_types comp-units. */
7787
7788 static void
7789 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7790 {
7791 if (! create_all_type_units (dwarf2_per_objfile))
7792 return;
7793
7794 build_type_psymtabs_1 (dwarf2_per_objfile);
7795 }
7796
7797 /* Traversal function for process_skeletonless_type_unit.
7798 Read a TU in a DWO file and build partial symbols for it. */
7799
7800 static int
7801 process_skeletonless_type_unit (void **slot, void *info)
7802 {
7803 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7804 struct dwarf2_per_objfile *dwarf2_per_objfile
7805 = (struct dwarf2_per_objfile *) info;
7806 struct signatured_type find_entry, *entry;
7807
7808 /* If this TU doesn't exist in the global table, add it and read it in. */
7809
7810 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7811 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7812
7813 find_entry.signature = dwo_unit->signature;
7814 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7815 &find_entry, INSERT);
7816 /* If we've already seen this type there's nothing to do. What's happening
7817 is we're doing our own version of comdat-folding here. */
7818 if (*slot != NULL)
7819 return 1;
7820
7821 /* This does the job that create_all_type_units would have done for
7822 this TU. */
7823 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7824 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7825 *slot = entry;
7826
7827 /* This does the job that build_type_psymtabs_1 would have done. */
7828 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7829 false);
7830 if (!reader.dummy_p)
7831 build_type_psymtabs_reader (&reader, reader.info_ptr,
7832 reader.comp_unit_die);
7833
7834 return 1;
7835 }
7836
7837 /* Traversal function for process_skeletonless_type_units. */
7838
7839 static int
7840 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7841 {
7842 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7843
7844 if (dwo_file->tus != NULL)
7845 htab_traverse_noresize (dwo_file->tus.get (),
7846 process_skeletonless_type_unit, info);
7847
7848 return 1;
7849 }
7850
7851 /* Scan all TUs of DWO files, verifying we've processed them.
7852 This is needed in case a TU was emitted without its skeleton.
7853 Note: This can't be done until we know what all the DWO files are. */
7854
7855 static void
7856 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7857 {
7858 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7859 if (get_dwp_file (dwarf2_per_objfile) == NULL
7860 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7861 {
7862 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7863 process_dwo_file_for_skeletonless_type_units,
7864 dwarf2_per_objfile);
7865 }
7866 }
7867
7868 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7869
7870 static void
7871 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7872 {
7873 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7874 {
7875 dwarf2_psymtab *pst = per_cu->v.psymtab;
7876
7877 if (pst == NULL)
7878 continue;
7879
7880 for (int j = 0; j < pst->number_of_dependencies; ++j)
7881 {
7882 /* Set the 'user' field only if it is not already set. */
7883 if (pst->dependencies[j]->user == NULL)
7884 pst->dependencies[j]->user = pst;
7885 }
7886 }
7887 }
7888
7889 /* Build the partial symbol table by doing a quick pass through the
7890 .debug_info and .debug_abbrev sections. */
7891
7892 static void
7893 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7894 {
7895 struct objfile *objfile = dwarf2_per_objfile->objfile;
7896
7897 if (dwarf_read_debug)
7898 {
7899 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7900 objfile_name (objfile));
7901 }
7902
7903 scoped_restore restore_reading_psyms
7904 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7905 true);
7906
7907 dwarf2_per_objfile->per_bfd->info.read (objfile);
7908
7909 /* Any cached compilation units will be linked by the per-objfile
7910 read_in_chain. Make sure to free them when we're done. */
7911 free_cached_comp_units freer (dwarf2_per_objfile);
7912
7913 build_type_psymtabs (dwarf2_per_objfile);
7914
7915 create_all_comp_units (dwarf2_per_objfile);
7916
7917 /* Create a temporary address map on a temporary obstack. We later
7918 copy this to the final obstack. */
7919 auto_obstack temp_obstack;
7920
7921 scoped_restore save_psymtabs_addrmap
7922 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7923 addrmap_create_mutable (&temp_obstack));
7924
7925 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7926 {
7927 if (per_cu->v.psymtab != NULL)
7928 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7929 continue;
7930 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7931 language_minimal);
7932 }
7933
7934 /* This has to wait until we read the CUs, we need the list of DWOs. */
7935 process_skeletonless_type_units (dwarf2_per_objfile);
7936
7937 /* Now that all TUs have been processed we can fill in the dependencies. */
7938 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7939 {
7940 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7941 build_type_psymtab_dependencies, dwarf2_per_objfile);
7942 }
7943
7944 if (dwarf_read_debug)
7945 print_tu_stats (dwarf2_per_objfile);
7946
7947 set_partial_user (dwarf2_per_objfile);
7948
7949 objfile->partial_symtabs->psymtabs_addrmap
7950 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7951 objfile->partial_symtabs->obstack ());
7952 /* At this point we want to keep the address map. */
7953 save_psymtabs_addrmap.release ();
7954
7955 if (dwarf_read_debug)
7956 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7957 objfile_name (objfile));
7958 }
7959
7960 /* Load the partial DIEs for a secondary CU into memory.
7961 This is also used when rereading a primary CU with load_all_dies. */
7962
7963 static void
7964 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7965 dwarf2_per_objfile *per_objfile,
7966 dwarf2_cu *existing_cu)
7967 {
7968 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7969
7970 if (!reader.dummy_p)
7971 {
7972 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7973 language_minimal);
7974
7975 /* Check if comp unit has_children.
7976 If so, read the rest of the partial symbols from this comp unit.
7977 If not, there's no more debug_info for this comp unit. */
7978 if (reader.comp_unit_die->has_children)
7979 load_partial_dies (&reader, reader.info_ptr, 0);
7980
7981 reader.keep ();
7982 }
7983 }
7984
7985 static void
7986 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7987 struct dwarf2_section_info *section,
7988 struct dwarf2_section_info *abbrev_section,
7989 unsigned int is_dwz)
7990 {
7991 const gdb_byte *info_ptr;
7992 struct objfile *objfile = dwarf2_per_objfile->objfile;
7993
7994 if (dwarf_read_debug)
7995 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7996 section->get_name (),
7997 section->get_file_name ());
7998
7999 section->read (objfile);
8000
8001 info_ptr = section->buffer;
8002
8003 while (info_ptr < section->buffer + section->size)
8004 {
8005 struct dwarf2_per_cu_data *this_cu;
8006
8007 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8008
8009 comp_unit_head cu_header;
8010 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8011 abbrev_section, info_ptr,
8012 rcuh_kind::COMPILE);
8013
8014 /* Save the compilation unit for later lookup. */
8015 if (cu_header.unit_type != DW_UT_type)
8016 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8017 else
8018 {
8019 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8020 sig_type->signature = cu_header.signature;
8021 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8022 this_cu = &sig_type->per_cu;
8023 }
8024 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8025 this_cu->sect_off = sect_off;
8026 this_cu->length = cu_header.length + cu_header.initial_length_size;
8027 this_cu->is_dwz = is_dwz;
8028 this_cu->section = section;
8029
8030 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8031
8032 info_ptr = info_ptr + this_cu->length;
8033 }
8034 }
8035
8036 /* Create a list of all compilation units in OBJFILE.
8037 This is only done for -readnow and building partial symtabs. */
8038
8039 static void
8040 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8041 {
8042 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8043 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8044 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8045
8046 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8047 if (dwz != NULL)
8048 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8049 1);
8050 }
8051
8052 /* Process all loaded DIEs for compilation unit CU, starting at
8053 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8054 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8055 DW_AT_ranges). See the comments of add_partial_subprogram on how
8056 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8057
8058 static void
8059 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8060 CORE_ADDR *highpc, int set_addrmap,
8061 struct dwarf2_cu *cu)
8062 {
8063 struct partial_die_info *pdi;
8064
8065 /* Now, march along the PDI's, descending into ones which have
8066 interesting children but skipping the children of the other ones,
8067 until we reach the end of the compilation unit. */
8068
8069 pdi = first_die;
8070
8071 while (pdi != NULL)
8072 {
8073 pdi->fixup (cu);
8074
8075 /* Anonymous namespaces or modules have no name but have interesting
8076 children, so we need to look at them. Ditto for anonymous
8077 enums. */
8078
8079 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8080 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8081 || pdi->tag == DW_TAG_imported_unit
8082 || pdi->tag == DW_TAG_inlined_subroutine)
8083 {
8084 switch (pdi->tag)
8085 {
8086 case DW_TAG_subprogram:
8087 case DW_TAG_inlined_subroutine:
8088 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8089 break;
8090 case DW_TAG_constant:
8091 case DW_TAG_variable:
8092 case DW_TAG_typedef:
8093 case DW_TAG_union_type:
8094 if (!pdi->is_declaration
8095 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8096 {
8097 add_partial_symbol (pdi, cu);
8098 }
8099 break;
8100 case DW_TAG_class_type:
8101 case DW_TAG_interface_type:
8102 case DW_TAG_structure_type:
8103 if (!pdi->is_declaration)
8104 {
8105 add_partial_symbol (pdi, cu);
8106 }
8107 if ((cu->language == language_rust
8108 || cu->language == language_cplus) && pdi->has_children)
8109 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8110 set_addrmap, cu);
8111 break;
8112 case DW_TAG_enumeration_type:
8113 if (!pdi->is_declaration)
8114 add_partial_enumeration (pdi, cu);
8115 break;
8116 case DW_TAG_base_type:
8117 case DW_TAG_subrange_type:
8118 /* File scope base type definitions are added to the partial
8119 symbol table. */
8120 add_partial_symbol (pdi, cu);
8121 break;
8122 case DW_TAG_namespace:
8123 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8124 break;
8125 case DW_TAG_module:
8126 if (!pdi->is_declaration)
8127 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8128 break;
8129 case DW_TAG_imported_unit:
8130 {
8131 struct dwarf2_per_cu_data *per_cu;
8132
8133 /* For now we don't handle imported units in type units. */
8134 if (cu->per_cu->is_debug_types)
8135 {
8136 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8137 " supported in type units [in module %s]"),
8138 objfile_name (cu->per_objfile->objfile));
8139 }
8140
8141 per_cu = dwarf2_find_containing_comp_unit
8142 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8143
8144 /* Go read the partial unit, if needed. */
8145 if (per_cu->v.psymtab == NULL)
8146 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8147 cu->language);
8148
8149 cu->per_cu->imported_symtabs_push (per_cu);
8150 }
8151 break;
8152 case DW_TAG_imported_declaration:
8153 add_partial_symbol (pdi, cu);
8154 break;
8155 default:
8156 break;
8157 }
8158 }
8159
8160 /* If the die has a sibling, skip to the sibling. */
8161
8162 pdi = pdi->die_sibling;
8163 }
8164 }
8165
8166 /* Functions used to compute the fully scoped name of a partial DIE.
8167
8168 Normally, this is simple. For C++, the parent DIE's fully scoped
8169 name is concatenated with "::" and the partial DIE's name.
8170 Enumerators are an exception; they use the scope of their parent
8171 enumeration type, i.e. the name of the enumeration type is not
8172 prepended to the enumerator.
8173
8174 There are two complexities. One is DW_AT_specification; in this
8175 case "parent" means the parent of the target of the specification,
8176 instead of the direct parent of the DIE. The other is compilers
8177 which do not emit DW_TAG_namespace; in this case we try to guess
8178 the fully qualified name of structure types from their members'
8179 linkage names. This must be done using the DIE's children rather
8180 than the children of any DW_AT_specification target. We only need
8181 to do this for structures at the top level, i.e. if the target of
8182 any DW_AT_specification (if any; otherwise the DIE itself) does not
8183 have a parent. */
8184
8185 /* Compute the scope prefix associated with PDI's parent, in
8186 compilation unit CU. The result will be allocated on CU's
8187 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8188 field. NULL is returned if no prefix is necessary. */
8189 static const char *
8190 partial_die_parent_scope (struct partial_die_info *pdi,
8191 struct dwarf2_cu *cu)
8192 {
8193 const char *grandparent_scope;
8194 struct partial_die_info *parent, *real_pdi;
8195
8196 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8197 then this means the parent of the specification DIE. */
8198
8199 real_pdi = pdi;
8200 while (real_pdi->has_specification)
8201 {
8202 auto res = find_partial_die (real_pdi->spec_offset,
8203 real_pdi->spec_is_dwz, cu);
8204 real_pdi = res.pdi;
8205 cu = res.cu;
8206 }
8207
8208 parent = real_pdi->die_parent;
8209 if (parent == NULL)
8210 return NULL;
8211
8212 if (parent->scope_set)
8213 return parent->scope;
8214
8215 parent->fixup (cu);
8216
8217 grandparent_scope = partial_die_parent_scope (parent, cu);
8218
8219 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8220 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8221 Work around this problem here. */
8222 if (cu->language == language_cplus
8223 && parent->tag == DW_TAG_namespace
8224 && strcmp (parent->name, "::") == 0
8225 && grandparent_scope == NULL)
8226 {
8227 parent->scope = NULL;
8228 parent->scope_set = 1;
8229 return NULL;
8230 }
8231
8232 /* Nested subroutines in Fortran get a prefix. */
8233 if (pdi->tag == DW_TAG_enumerator)
8234 /* Enumerators should not get the name of the enumeration as a prefix. */
8235 parent->scope = grandparent_scope;
8236 else if (parent->tag == DW_TAG_namespace
8237 || parent->tag == DW_TAG_module
8238 || parent->tag == DW_TAG_structure_type
8239 || parent->tag == DW_TAG_class_type
8240 || parent->tag == DW_TAG_interface_type
8241 || parent->tag == DW_TAG_union_type
8242 || parent->tag == DW_TAG_enumeration_type
8243 || (cu->language == language_fortran
8244 && parent->tag == DW_TAG_subprogram
8245 && pdi->tag == DW_TAG_subprogram))
8246 {
8247 if (grandparent_scope == NULL)
8248 parent->scope = parent->name;
8249 else
8250 parent->scope = typename_concat (&cu->comp_unit_obstack,
8251 grandparent_scope,
8252 parent->name, 0, cu);
8253 }
8254 else
8255 {
8256 /* FIXME drow/2004-04-01: What should we be doing with
8257 function-local names? For partial symbols, we should probably be
8258 ignoring them. */
8259 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8260 dwarf_tag_name (parent->tag),
8261 sect_offset_str (pdi->sect_off));
8262 parent->scope = grandparent_scope;
8263 }
8264
8265 parent->scope_set = 1;
8266 return parent->scope;
8267 }
8268
8269 /* Return the fully scoped name associated with PDI, from compilation unit
8270 CU. The result will be allocated with malloc. */
8271
8272 static gdb::unique_xmalloc_ptr<char>
8273 partial_die_full_name (struct partial_die_info *pdi,
8274 struct dwarf2_cu *cu)
8275 {
8276 const char *parent_scope;
8277
8278 /* If this is a template instantiation, we can not work out the
8279 template arguments from partial DIEs. So, unfortunately, we have
8280 to go through the full DIEs. At least any work we do building
8281 types here will be reused if full symbols are loaded later. */
8282 if (pdi->has_template_arguments)
8283 {
8284 pdi->fixup (cu);
8285
8286 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8287 {
8288 struct die_info *die;
8289 struct attribute attr;
8290 struct dwarf2_cu *ref_cu = cu;
8291
8292 /* DW_FORM_ref_addr is using section offset. */
8293 attr.name = (enum dwarf_attribute) 0;
8294 attr.form = DW_FORM_ref_addr;
8295 attr.u.unsnd = to_underlying (pdi->sect_off);
8296 die = follow_die_ref (NULL, &attr, &ref_cu);
8297
8298 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8299 }
8300 }
8301
8302 parent_scope = partial_die_parent_scope (pdi, cu);
8303 if (parent_scope == NULL)
8304 return NULL;
8305 else
8306 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8307 pdi->name, 0, cu));
8308 }
8309
8310 static void
8311 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8312 {
8313 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8314 struct objfile *objfile = dwarf2_per_objfile->objfile;
8315 struct gdbarch *gdbarch = objfile->arch ();
8316 CORE_ADDR addr = 0;
8317 const char *actual_name = NULL;
8318 CORE_ADDR baseaddr;
8319
8320 baseaddr = objfile->text_section_offset ();
8321
8322 gdb::unique_xmalloc_ptr<char> built_actual_name
8323 = partial_die_full_name (pdi, cu);
8324 if (built_actual_name != NULL)
8325 actual_name = built_actual_name.get ();
8326
8327 if (actual_name == NULL)
8328 actual_name = pdi->name;
8329
8330 partial_symbol psymbol;
8331 memset (&psymbol, 0, sizeof (psymbol));
8332 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8333 psymbol.ginfo.section = -1;
8334
8335 /* The code below indicates that the psymbol should be installed by
8336 setting this. */
8337 gdb::optional<psymbol_placement> where;
8338
8339 switch (pdi->tag)
8340 {
8341 case DW_TAG_inlined_subroutine:
8342 case DW_TAG_subprogram:
8343 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8344 - baseaddr);
8345 if (pdi->is_external
8346 || cu->language == language_ada
8347 || (cu->language == language_fortran
8348 && pdi->die_parent != NULL
8349 && pdi->die_parent->tag == DW_TAG_subprogram))
8350 {
8351 /* Normally, only "external" DIEs are part of the global scope.
8352 But in Ada and Fortran, we want to be able to access nested
8353 procedures globally. So all Ada and Fortran subprograms are
8354 stored in the global scope. */
8355 where = psymbol_placement::GLOBAL;
8356 }
8357 else
8358 where = psymbol_placement::STATIC;
8359
8360 psymbol.domain = VAR_DOMAIN;
8361 psymbol.aclass = LOC_BLOCK;
8362 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8363 psymbol.ginfo.value.address = addr;
8364
8365 if (pdi->main_subprogram && actual_name != NULL)
8366 set_objfile_main_name (objfile, actual_name, cu->language);
8367 break;
8368 case DW_TAG_constant:
8369 psymbol.domain = VAR_DOMAIN;
8370 psymbol.aclass = LOC_STATIC;
8371 where = (pdi->is_external
8372 ? psymbol_placement::GLOBAL
8373 : psymbol_placement::STATIC);
8374 break;
8375 case DW_TAG_variable:
8376 if (pdi->d.locdesc)
8377 addr = decode_locdesc (pdi->d.locdesc, cu);
8378
8379 if (pdi->d.locdesc
8380 && addr == 0
8381 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8382 {
8383 /* A global or static variable may also have been stripped
8384 out by the linker if unused, in which case its address
8385 will be nullified; do not add such variables into partial
8386 symbol table then. */
8387 }
8388 else if (pdi->is_external)
8389 {
8390 /* Global Variable.
8391 Don't enter into the minimal symbol tables as there is
8392 a minimal symbol table entry from the ELF symbols already.
8393 Enter into partial symbol table if it has a location
8394 descriptor or a type.
8395 If the location descriptor is missing, new_symbol will create
8396 a LOC_UNRESOLVED symbol, the address of the variable will then
8397 be determined from the minimal symbol table whenever the variable
8398 is referenced.
8399 The address for the partial symbol table entry is not
8400 used by GDB, but it comes in handy for debugging partial symbol
8401 table building. */
8402
8403 if (pdi->d.locdesc || pdi->has_type)
8404 {
8405 psymbol.domain = VAR_DOMAIN;
8406 psymbol.aclass = LOC_STATIC;
8407 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8408 psymbol.ginfo.value.address = addr;
8409 where = psymbol_placement::GLOBAL;
8410 }
8411 }
8412 else
8413 {
8414 int has_loc = pdi->d.locdesc != NULL;
8415
8416 /* Static Variable. Skip symbols whose value we cannot know (those
8417 without location descriptors or constant values). */
8418 if (!has_loc && !pdi->has_const_value)
8419 return;
8420
8421 psymbol.domain = VAR_DOMAIN;
8422 psymbol.aclass = LOC_STATIC;
8423 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8424 if (has_loc)
8425 psymbol.ginfo.value.address = addr;
8426 where = psymbol_placement::STATIC;
8427 }
8428 break;
8429 case DW_TAG_typedef:
8430 case DW_TAG_base_type:
8431 case DW_TAG_subrange_type:
8432 psymbol.domain = VAR_DOMAIN;
8433 psymbol.aclass = LOC_TYPEDEF;
8434 where = psymbol_placement::STATIC;
8435 break;
8436 case DW_TAG_imported_declaration:
8437 case DW_TAG_namespace:
8438 psymbol.domain = VAR_DOMAIN;
8439 psymbol.aclass = LOC_TYPEDEF;
8440 where = psymbol_placement::GLOBAL;
8441 break;
8442 case DW_TAG_module:
8443 /* With Fortran 77 there might be a "BLOCK DATA" module
8444 available without any name. If so, we skip the module as it
8445 doesn't bring any value. */
8446 if (actual_name != nullptr)
8447 {
8448 psymbol.domain = MODULE_DOMAIN;
8449 psymbol.aclass = LOC_TYPEDEF;
8450 where = psymbol_placement::GLOBAL;
8451 }
8452 break;
8453 case DW_TAG_class_type:
8454 case DW_TAG_interface_type:
8455 case DW_TAG_structure_type:
8456 case DW_TAG_union_type:
8457 case DW_TAG_enumeration_type:
8458 /* Skip external references. The DWARF standard says in the section
8459 about "Structure, Union, and Class Type Entries": "An incomplete
8460 structure, union or class type is represented by a structure,
8461 union or class entry that does not have a byte size attribute
8462 and that has a DW_AT_declaration attribute." */
8463 if (!pdi->has_byte_size && pdi->is_declaration)
8464 return;
8465
8466 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8467 static vs. global. */
8468 psymbol.domain = STRUCT_DOMAIN;
8469 psymbol.aclass = LOC_TYPEDEF;
8470 where = (cu->language == language_cplus
8471 ? psymbol_placement::GLOBAL
8472 : psymbol_placement::STATIC);
8473 break;
8474 case DW_TAG_enumerator:
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_CONST;
8477 where = (cu->language == language_cplus
8478 ? psymbol_placement::GLOBAL
8479 : psymbol_placement::STATIC);
8480 break;
8481 default:
8482 break;
8483 }
8484
8485 if (where.has_value ())
8486 {
8487 if (built_actual_name != nullptr)
8488 actual_name = objfile->intern (actual_name);
8489 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8490 psymbol.ginfo.set_linkage_name (actual_name);
8491 else
8492 {
8493 psymbol.ginfo.set_demangled_name (actual_name,
8494 &objfile->objfile_obstack);
8495 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8496 }
8497 add_psymbol_to_list (psymbol, *where, objfile);
8498 }
8499 }
8500
8501 /* Read a partial die corresponding to a namespace; also, add a symbol
8502 corresponding to that namespace to the symbol table. NAMESPACE is
8503 the name of the enclosing namespace. */
8504
8505 static void
8506 add_partial_namespace (struct partial_die_info *pdi,
8507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8508 int set_addrmap, struct dwarf2_cu *cu)
8509 {
8510 /* Add a symbol for the namespace. */
8511
8512 add_partial_symbol (pdi, cu);
8513
8514 /* Now scan partial symbols in that namespace. */
8515
8516 if (pdi->has_children)
8517 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8518 }
8519
8520 /* Read a partial die corresponding to a Fortran module. */
8521
8522 static void
8523 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8524 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8525 {
8526 /* Add a symbol for the namespace. */
8527
8528 add_partial_symbol (pdi, cu);
8529
8530 /* Now scan partial symbols in that module. */
8531
8532 if (pdi->has_children)
8533 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8534 }
8535
8536 /* Read a partial die corresponding to a subprogram or an inlined
8537 subprogram and create a partial symbol for that subprogram.
8538 When the CU language allows it, this routine also defines a partial
8539 symbol for each nested subprogram that this subprogram contains.
8540 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8541 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8542
8543 PDI may also be a lexical block, in which case we simply search
8544 recursively for subprograms defined inside that lexical block.
8545 Again, this is only performed when the CU language allows this
8546 type of definitions. */
8547
8548 static void
8549 add_partial_subprogram (struct partial_die_info *pdi,
8550 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8551 int set_addrmap, struct dwarf2_cu *cu)
8552 {
8553 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8554 {
8555 if (pdi->has_pc_info)
8556 {
8557 if (pdi->lowpc < *lowpc)
8558 *lowpc = pdi->lowpc;
8559 if (pdi->highpc > *highpc)
8560 *highpc = pdi->highpc;
8561 if (set_addrmap)
8562 {
8563 struct objfile *objfile = cu->per_objfile->objfile;
8564 struct gdbarch *gdbarch = objfile->arch ();
8565 CORE_ADDR baseaddr;
8566 CORE_ADDR this_highpc;
8567 CORE_ADDR this_lowpc;
8568
8569 baseaddr = objfile->text_section_offset ();
8570 this_lowpc
8571 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8572 pdi->lowpc + baseaddr)
8573 - baseaddr);
8574 this_highpc
8575 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8576 pdi->highpc + baseaddr)
8577 - baseaddr);
8578 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8579 this_lowpc, this_highpc - 1,
8580 cu->per_cu->v.psymtab);
8581 }
8582 }
8583
8584 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8585 {
8586 if (!pdi->is_declaration)
8587 /* Ignore subprogram DIEs that do not have a name, they are
8588 illegal. Do not emit a complaint at this point, we will
8589 do so when we convert this psymtab into a symtab. */
8590 if (pdi->name)
8591 add_partial_symbol (pdi, cu);
8592 }
8593 }
8594
8595 if (! pdi->has_children)
8596 return;
8597
8598 if (cu->language == language_ada || cu->language == language_fortran)
8599 {
8600 pdi = pdi->die_child;
8601 while (pdi != NULL)
8602 {
8603 pdi->fixup (cu);
8604 if (pdi->tag == DW_TAG_subprogram
8605 || pdi->tag == DW_TAG_inlined_subroutine
8606 || pdi->tag == DW_TAG_lexical_block)
8607 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8608 pdi = pdi->die_sibling;
8609 }
8610 }
8611 }
8612
8613 /* Read a partial die corresponding to an enumeration type. */
8614
8615 static void
8616 add_partial_enumeration (struct partial_die_info *enum_pdi,
8617 struct dwarf2_cu *cu)
8618 {
8619 struct partial_die_info *pdi;
8620
8621 if (enum_pdi->name != NULL)
8622 add_partial_symbol (enum_pdi, cu);
8623
8624 pdi = enum_pdi->die_child;
8625 while (pdi)
8626 {
8627 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8628 complaint (_("malformed enumerator DIE ignored"));
8629 else
8630 add_partial_symbol (pdi, cu);
8631 pdi = pdi->die_sibling;
8632 }
8633 }
8634
8635 /* Return the initial uleb128 in the die at INFO_PTR. */
8636
8637 static unsigned int
8638 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8639 {
8640 unsigned int bytes_read;
8641
8642 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8643 }
8644
8645 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8646 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8647
8648 Return the corresponding abbrev, or NULL if the number is zero (indicating
8649 an empty DIE). In either case *BYTES_READ will be set to the length of
8650 the initial number. */
8651
8652 static struct abbrev_info *
8653 peek_die_abbrev (const die_reader_specs &reader,
8654 const gdb_byte *info_ptr, unsigned int *bytes_read)
8655 {
8656 dwarf2_cu *cu = reader.cu;
8657 bfd *abfd = cu->per_objfile->objfile->obfd;
8658 unsigned int abbrev_number
8659 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8660
8661 if (abbrev_number == 0)
8662 return NULL;
8663
8664 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8665 if (!abbrev)
8666 {
8667 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8668 " at offset %s [in module %s]"),
8669 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8670 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8671 }
8672
8673 return abbrev;
8674 }
8675
8676 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8677 Returns a pointer to the end of a series of DIEs, terminated by an empty
8678 DIE. Any children of the skipped DIEs will also be skipped. */
8679
8680 static const gdb_byte *
8681 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8682 {
8683 while (1)
8684 {
8685 unsigned int bytes_read;
8686 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8687
8688 if (abbrev == NULL)
8689 return info_ptr + bytes_read;
8690 else
8691 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8692 }
8693 }
8694
8695 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8696 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8697 abbrev corresponding to that skipped uleb128 should be passed in
8698 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8699 children. */
8700
8701 static const gdb_byte *
8702 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8703 struct abbrev_info *abbrev)
8704 {
8705 unsigned int bytes_read;
8706 struct attribute attr;
8707 bfd *abfd = reader->abfd;
8708 struct dwarf2_cu *cu = reader->cu;
8709 const gdb_byte *buffer = reader->buffer;
8710 const gdb_byte *buffer_end = reader->buffer_end;
8711 unsigned int form, i;
8712
8713 for (i = 0; i < abbrev->num_attrs; i++)
8714 {
8715 /* The only abbrev we care about is DW_AT_sibling. */
8716 if (abbrev->attrs[i].name == DW_AT_sibling)
8717 {
8718 bool ignored;
8719 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8720 &ignored);
8721 if (attr.form == DW_FORM_ref_addr)
8722 complaint (_("ignoring absolute DW_AT_sibling"));
8723 else
8724 {
8725 sect_offset off = attr.get_ref_die_offset ();
8726 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8727
8728 if (sibling_ptr < info_ptr)
8729 complaint (_("DW_AT_sibling points backwards"));
8730 else if (sibling_ptr > reader->buffer_end)
8731 reader->die_section->overflow_complaint ();
8732 else
8733 return sibling_ptr;
8734 }
8735 }
8736
8737 /* If it isn't DW_AT_sibling, skip this attribute. */
8738 form = abbrev->attrs[i].form;
8739 skip_attribute:
8740 switch (form)
8741 {
8742 case DW_FORM_ref_addr:
8743 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8744 and later it is offset sized. */
8745 if (cu->header.version == 2)
8746 info_ptr += cu->header.addr_size;
8747 else
8748 info_ptr += cu->header.offset_size;
8749 break;
8750 case DW_FORM_GNU_ref_alt:
8751 info_ptr += cu->header.offset_size;
8752 break;
8753 case DW_FORM_addr:
8754 info_ptr += cu->header.addr_size;
8755 break;
8756 case DW_FORM_data1:
8757 case DW_FORM_ref1:
8758 case DW_FORM_flag:
8759 case DW_FORM_strx1:
8760 info_ptr += 1;
8761 break;
8762 case DW_FORM_flag_present:
8763 case DW_FORM_implicit_const:
8764 break;
8765 case DW_FORM_data2:
8766 case DW_FORM_ref2:
8767 case DW_FORM_strx2:
8768 info_ptr += 2;
8769 break;
8770 case DW_FORM_strx3:
8771 info_ptr += 3;
8772 break;
8773 case DW_FORM_data4:
8774 case DW_FORM_ref4:
8775 case DW_FORM_strx4:
8776 info_ptr += 4;
8777 break;
8778 case DW_FORM_data8:
8779 case DW_FORM_ref8:
8780 case DW_FORM_ref_sig8:
8781 info_ptr += 8;
8782 break;
8783 case DW_FORM_data16:
8784 info_ptr += 16;
8785 break;
8786 case DW_FORM_string:
8787 read_direct_string (abfd, info_ptr, &bytes_read);
8788 info_ptr += bytes_read;
8789 break;
8790 case DW_FORM_sec_offset:
8791 case DW_FORM_strp:
8792 case DW_FORM_GNU_strp_alt:
8793 info_ptr += cu->header.offset_size;
8794 break;
8795 case DW_FORM_exprloc:
8796 case DW_FORM_block:
8797 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8798 info_ptr += bytes_read;
8799 break;
8800 case DW_FORM_block1:
8801 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8802 break;
8803 case DW_FORM_block2:
8804 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8805 break;
8806 case DW_FORM_block4:
8807 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8808 break;
8809 case DW_FORM_addrx:
8810 case DW_FORM_strx:
8811 case DW_FORM_sdata:
8812 case DW_FORM_udata:
8813 case DW_FORM_ref_udata:
8814 case DW_FORM_GNU_addr_index:
8815 case DW_FORM_GNU_str_index:
8816 case DW_FORM_rnglistx:
8817 case DW_FORM_loclistx:
8818 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8819 break;
8820 case DW_FORM_indirect:
8821 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8822 info_ptr += bytes_read;
8823 /* We need to continue parsing from here, so just go back to
8824 the top. */
8825 goto skip_attribute;
8826
8827 default:
8828 error (_("Dwarf Error: Cannot handle %s "
8829 "in DWARF reader [in module %s]"),
8830 dwarf_form_name (form),
8831 bfd_get_filename (abfd));
8832 }
8833 }
8834
8835 if (abbrev->has_children)
8836 return skip_children (reader, info_ptr);
8837 else
8838 return info_ptr;
8839 }
8840
8841 /* Locate ORIG_PDI's sibling.
8842 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8843
8844 static const gdb_byte *
8845 locate_pdi_sibling (const struct die_reader_specs *reader,
8846 struct partial_die_info *orig_pdi,
8847 const gdb_byte *info_ptr)
8848 {
8849 /* Do we know the sibling already? */
8850
8851 if (orig_pdi->sibling)
8852 return orig_pdi->sibling;
8853
8854 /* Are there any children to deal with? */
8855
8856 if (!orig_pdi->has_children)
8857 return info_ptr;
8858
8859 /* Skip the children the long way. */
8860
8861 return skip_children (reader, info_ptr);
8862 }
8863
8864 /* Expand this partial symbol table into a full symbol table. SELF is
8865 not NULL. */
8866
8867 void
8868 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8869 {
8870 struct dwarf2_per_objfile *dwarf2_per_objfile
8871 = get_dwarf2_per_objfile (objfile);
8872
8873 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8874
8875 /* If this psymtab is constructed from a debug-only objfile, the
8876 has_section_at_zero flag will not necessarily be correct. We
8877 can get the correct value for this flag by looking at the data
8878 associated with the (presumably stripped) associated objfile. */
8879 if (objfile->separate_debug_objfile_backlink)
8880 {
8881 struct dwarf2_per_objfile *dpo_backlink
8882 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8883
8884 dwarf2_per_objfile->per_bfd->has_section_at_zero
8885 = dpo_backlink->per_bfd->has_section_at_zero;
8886 }
8887
8888 expand_psymtab (objfile);
8889
8890 process_cu_includes (dwarf2_per_objfile);
8891 }
8892 \f
8893 /* Reading in full CUs. */
8894
8895 /* Add PER_CU to the queue. */
8896
8897 static void
8898 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8899 dwarf2_per_objfile *per_objfile,
8900 enum language pretend_language)
8901 {
8902 per_cu->queued = 1;
8903 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8904 }
8905
8906 /* If PER_CU is not yet queued, add it to the queue.
8907 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8908 dependency.
8909 The result is non-zero if PER_CU was queued, otherwise the result is zero
8910 meaning either PER_CU is already queued or it is already loaded.
8911
8912 N.B. There is an invariant here that if a CU is queued then it is loaded.
8913 The caller is required to load PER_CU if we return non-zero. */
8914
8915 static int
8916 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8917 dwarf2_per_cu_data *per_cu,
8918 dwarf2_per_objfile *per_objfile,
8919 enum language pretend_language)
8920 {
8921 /* We may arrive here during partial symbol reading, if we need full
8922 DIEs to process an unusual case (e.g. template arguments). Do
8923 not queue PER_CU, just tell our caller to load its DIEs. */
8924 if (per_cu->per_bfd->reading_partial_symbols)
8925 {
8926 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8927
8928 if (cu == NULL || cu->dies == NULL)
8929 return 1;
8930 return 0;
8931 }
8932
8933 /* Mark the dependence relation so that we don't flush PER_CU
8934 too early. */
8935 if (dependent_cu != NULL)
8936 dwarf2_add_dependence (dependent_cu, per_cu);
8937
8938 /* If it's already on the queue, we have nothing to do. */
8939 if (per_cu->queued)
8940 return 0;
8941
8942 /* If the compilation unit is already loaded, just mark it as
8943 used. */
8944 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8945 if (cu != nullptr)
8946 {
8947 cu->last_used = 0;
8948 return 0;
8949 }
8950
8951 /* Add it to the queue. */
8952 queue_comp_unit (per_cu, per_objfile, pretend_language);
8953
8954 return 1;
8955 }
8956
8957 /* Process the queue. */
8958
8959 static void
8960 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8961 {
8962 if (dwarf_read_debug)
8963 {
8964 fprintf_unfiltered (gdb_stdlog,
8965 "Expanding one or more symtabs of objfile %s ...\n",
8966 objfile_name (dwarf2_per_objfile->objfile));
8967 }
8968
8969 /* The queue starts out with one item, but following a DIE reference
8970 may load a new CU, adding it to the end of the queue. */
8971 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8972 {
8973 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8974 dwarf2_per_cu_data *per_cu = item.per_cu;
8975
8976 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
8977 {
8978 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
8979
8980 /* Skip dummy CUs. */
8981 if (cu != nullptr)
8982 {
8983 unsigned int debug_print_threshold;
8984 char buf[100];
8985
8986 if (per_cu->is_debug_types)
8987 {
8988 struct signatured_type *sig_type =
8989 (struct signatured_type *) per_cu;
8990
8991 sprintf (buf, "TU %s at offset %s",
8992 hex_string (sig_type->signature),
8993 sect_offset_str (per_cu->sect_off));
8994 /* There can be 100s of TUs.
8995 Only print them in verbose mode. */
8996 debug_print_threshold = 2;
8997 }
8998 else
8999 {
9000 sprintf (buf, "CU at offset %s",
9001 sect_offset_str (per_cu->sect_off));
9002 debug_print_threshold = 1;
9003 }
9004
9005 if (dwarf_read_debug >= debug_print_threshold)
9006 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9007
9008 if (per_cu->is_debug_types)
9009 process_full_type_unit (cu, item.pretend_language);
9010 else
9011 process_full_comp_unit (cu, item.pretend_language);
9012
9013 if (dwarf_read_debug >= debug_print_threshold)
9014 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9015 }
9016 }
9017
9018 per_cu->queued = 0;
9019 dwarf2_per_objfile->per_bfd->queue.pop ();
9020 }
9021
9022 if (dwarf_read_debug)
9023 {
9024 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9025 objfile_name (dwarf2_per_objfile->objfile));
9026 }
9027 }
9028
9029 /* Read in full symbols for PST, and anything it depends on. */
9030
9031 void
9032 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9033 {
9034 gdb_assert (!readin_p (objfile));
9035
9036 expand_dependencies (objfile);
9037
9038 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9039 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9040 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9041 }
9042
9043 /* See psympriv.h. */
9044
9045 bool
9046 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9047 {
9048 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9049 return per_objfile->symtab_set_p (per_cu_data);
9050 }
9051
9052 /* See psympriv.h. */
9053
9054 compunit_symtab *
9055 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9056 {
9057 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9058 return per_objfile->get_symtab (per_cu_data);
9059 }
9060
9061 /* Trivial hash function for die_info: the hash value of a DIE
9062 is its offset in .debug_info for this objfile. */
9063
9064 static hashval_t
9065 die_hash (const void *item)
9066 {
9067 const struct die_info *die = (const struct die_info *) item;
9068
9069 return to_underlying (die->sect_off);
9070 }
9071
9072 /* Trivial comparison function for die_info structures: two DIEs
9073 are equal if they have the same offset. */
9074
9075 static int
9076 die_eq (const void *item_lhs, const void *item_rhs)
9077 {
9078 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9079 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9080
9081 return die_lhs->sect_off == die_rhs->sect_off;
9082 }
9083
9084 /* Load the DIEs associated with PER_CU into memory. */
9085
9086 static void
9087 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9088 dwarf2_per_objfile *per_objfile,
9089 bool skip_partial,
9090 enum language pretend_language)
9091 {
9092 gdb_assert (! this_cu->is_debug_types);
9093
9094 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9095 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9096 if (reader.dummy_p)
9097 return;
9098
9099 struct dwarf2_cu *cu = reader.cu;
9100 const gdb_byte *info_ptr = reader.info_ptr;
9101
9102 gdb_assert (cu->die_hash == NULL);
9103 cu->die_hash =
9104 htab_create_alloc_ex (cu->header.length / 12,
9105 die_hash,
9106 die_eq,
9107 NULL,
9108 &cu->comp_unit_obstack,
9109 hashtab_obstack_allocate,
9110 dummy_obstack_deallocate);
9111
9112 if (reader.comp_unit_die->has_children)
9113 reader.comp_unit_die->child
9114 = read_die_and_siblings (&reader, reader.info_ptr,
9115 &info_ptr, reader.comp_unit_die);
9116 cu->dies = reader.comp_unit_die;
9117 /* comp_unit_die is not stored in die_hash, no need. */
9118
9119 /* We try not to read any attributes in this function, because not
9120 all CUs needed for references have been loaded yet, and symbol
9121 table processing isn't initialized. But we have to set the CU language,
9122 or we won't be able to build types correctly.
9123 Similarly, if we do not read the producer, we can not apply
9124 producer-specific interpretation. */
9125 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9126
9127 reader.keep ();
9128 }
9129
9130 /* Add a DIE to the delayed physname list. */
9131
9132 static void
9133 add_to_method_list (struct type *type, int fnfield_index, int index,
9134 const char *name, struct die_info *die,
9135 struct dwarf2_cu *cu)
9136 {
9137 struct delayed_method_info mi;
9138 mi.type = type;
9139 mi.fnfield_index = fnfield_index;
9140 mi.index = index;
9141 mi.name = name;
9142 mi.die = die;
9143 cu->method_list.push_back (mi);
9144 }
9145
9146 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9147 "const" / "volatile". If so, decrements LEN by the length of the
9148 modifier and return true. Otherwise return false. */
9149
9150 template<size_t N>
9151 static bool
9152 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9153 {
9154 size_t mod_len = sizeof (mod) - 1;
9155 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9156 {
9157 len -= mod_len;
9158 return true;
9159 }
9160 return false;
9161 }
9162
9163 /* Compute the physnames of any methods on the CU's method list.
9164
9165 The computation of method physnames is delayed in order to avoid the
9166 (bad) condition that one of the method's formal parameters is of an as yet
9167 incomplete type. */
9168
9169 static void
9170 compute_delayed_physnames (struct dwarf2_cu *cu)
9171 {
9172 /* Only C++ delays computing physnames. */
9173 if (cu->method_list.empty ())
9174 return;
9175 gdb_assert (cu->language == language_cplus);
9176
9177 for (const delayed_method_info &mi : cu->method_list)
9178 {
9179 const char *physname;
9180 struct fn_fieldlist *fn_flp
9181 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9182 physname = dwarf2_physname (mi.name, mi.die, cu);
9183 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9184 = physname ? physname : "";
9185
9186 /* Since there's no tag to indicate whether a method is a
9187 const/volatile overload, extract that information out of the
9188 demangled name. */
9189 if (physname != NULL)
9190 {
9191 size_t len = strlen (physname);
9192
9193 while (1)
9194 {
9195 if (physname[len] == ')') /* shortcut */
9196 break;
9197 else if (check_modifier (physname, len, " const"))
9198 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9199 else if (check_modifier (physname, len, " volatile"))
9200 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9201 else
9202 break;
9203 }
9204 }
9205 }
9206
9207 /* The list is no longer needed. */
9208 cu->method_list.clear ();
9209 }
9210
9211 /* Go objects should be embedded in a DW_TAG_module DIE,
9212 and it's not clear if/how imported objects will appear.
9213 To keep Go support simple until that's worked out,
9214 go back through what we've read and create something usable.
9215 We could do this while processing each DIE, and feels kinda cleaner,
9216 but that way is more invasive.
9217 This is to, for example, allow the user to type "p var" or "b main"
9218 without having to specify the package name, and allow lookups
9219 of module.object to work in contexts that use the expression
9220 parser. */
9221
9222 static void
9223 fixup_go_packaging (struct dwarf2_cu *cu)
9224 {
9225 gdb::unique_xmalloc_ptr<char> package_name;
9226 struct pending *list;
9227 int i;
9228
9229 for (list = *cu->get_builder ()->get_global_symbols ();
9230 list != NULL;
9231 list = list->next)
9232 {
9233 for (i = 0; i < list->nsyms; ++i)
9234 {
9235 struct symbol *sym = list->symbol[i];
9236
9237 if (sym->language () == language_go
9238 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9239 {
9240 gdb::unique_xmalloc_ptr<char> this_package_name
9241 (go_symbol_package_name (sym));
9242
9243 if (this_package_name == NULL)
9244 continue;
9245 if (package_name == NULL)
9246 package_name = std::move (this_package_name);
9247 else
9248 {
9249 struct objfile *objfile = cu->per_objfile->objfile;
9250 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9251 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9252 (symbol_symtab (sym) != NULL
9253 ? symtab_to_filename_for_display
9254 (symbol_symtab (sym))
9255 : objfile_name (objfile)),
9256 this_package_name.get (), package_name.get ());
9257 }
9258 }
9259 }
9260 }
9261
9262 if (package_name != NULL)
9263 {
9264 struct objfile *objfile = cu->per_objfile->objfile;
9265 const char *saved_package_name = objfile->intern (package_name.get ());
9266 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9267 saved_package_name);
9268 struct symbol *sym;
9269
9270 sym = new (&objfile->objfile_obstack) symbol;
9271 sym->set_language (language_go, &objfile->objfile_obstack);
9272 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9273 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9274 e.g., "main" finds the "main" module and not C's main(). */
9275 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9276 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9277 SYMBOL_TYPE (sym) = type;
9278
9279 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9280 }
9281 }
9282
9283 /* Allocate a fully-qualified name consisting of the two parts on the
9284 obstack. */
9285
9286 static const char *
9287 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9288 {
9289 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9290 }
9291
9292 /* A helper that allocates a variant part to attach to a Rust enum
9293 type. OBSTACK is where the results should be allocated. TYPE is
9294 the type we're processing. DISCRIMINANT_INDEX is the index of the
9295 discriminant. It must be the index of one of the fields of TYPE.
9296 DEFAULT_INDEX is the index of the default field; or -1 if there is
9297 no default. RANGES is indexed by "effective" field number (the
9298 field index, but omitting the discriminant and default fields) and
9299 must hold the discriminant values used by the variants. Note that
9300 RANGES must have a lifetime at least as long as OBSTACK -- either
9301 already allocated on it, or static. */
9302
9303 static void
9304 alloc_rust_variant (struct obstack *obstack, struct type *type,
9305 int discriminant_index, int default_index,
9306 gdb::array_view<discriminant_range> ranges)
9307 {
9308 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9309 must be handled by the caller. */
9310 gdb_assert (discriminant_index >= 0
9311 && discriminant_index < type->num_fields ());
9312 gdb_assert (default_index == -1
9313 || (default_index >= 0 && default_index < type->num_fields ()));
9314
9315 /* We have one variant for each non-discriminant field. */
9316 int n_variants = type->num_fields () - 1;
9317
9318 variant *variants = new (obstack) variant[n_variants];
9319 int var_idx = 0;
9320 int range_idx = 0;
9321 for (int i = 0; i < type->num_fields (); ++i)
9322 {
9323 if (i == discriminant_index)
9324 continue;
9325
9326 variants[var_idx].first_field = i;
9327 variants[var_idx].last_field = i + 1;
9328
9329 /* The default field does not need a range, but other fields do.
9330 We skipped the discriminant above. */
9331 if (i != default_index)
9332 {
9333 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9334 ++range_idx;
9335 }
9336
9337 ++var_idx;
9338 }
9339
9340 gdb_assert (range_idx == ranges.size ());
9341 gdb_assert (var_idx == n_variants);
9342
9343 variant_part *part = new (obstack) variant_part;
9344 part->discriminant_index = discriminant_index;
9345 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9346 discriminant_index));
9347 part->variants = gdb::array_view<variant> (variants, n_variants);
9348
9349 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9350 gdb::array_view<variant_part> *prop_value
9351 = new (storage) gdb::array_view<variant_part> (part, 1);
9352
9353 struct dynamic_prop prop;
9354 prop.kind = PROP_VARIANT_PARTS;
9355 prop.data.variant_parts = prop_value;
9356
9357 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9358 }
9359
9360 /* Some versions of rustc emitted enums in an unusual way.
9361
9362 Ordinary enums were emitted as unions. The first element of each
9363 structure in the union was named "RUST$ENUM$DISR". This element
9364 held the discriminant.
9365
9366 These versions of Rust also implemented the "non-zero"
9367 optimization. When the enum had two values, and one is empty and
9368 the other holds a pointer that cannot be zero, the pointer is used
9369 as the discriminant, with a zero value meaning the empty variant.
9370 Here, the union's first member is of the form
9371 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9372 where the fieldnos are the indices of the fields that should be
9373 traversed in order to find the field (which may be several fields deep)
9374 and the variantname is the name of the variant of the case when the
9375 field is zero.
9376
9377 This function recognizes whether TYPE is of one of these forms,
9378 and, if so, smashes it to be a variant type. */
9379
9380 static void
9381 quirk_rust_enum (struct type *type, struct objfile *objfile)
9382 {
9383 gdb_assert (type->code () == TYPE_CODE_UNION);
9384
9385 /* We don't need to deal with empty enums. */
9386 if (type->num_fields () == 0)
9387 return;
9388
9389 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9390 if (type->num_fields () == 1
9391 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9392 {
9393 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9394
9395 /* Decode the field name to find the offset of the
9396 discriminant. */
9397 ULONGEST bit_offset = 0;
9398 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9399 while (name[0] >= '0' && name[0] <= '9')
9400 {
9401 char *tail;
9402 unsigned long index = strtoul (name, &tail, 10);
9403 name = tail;
9404 if (*name != '$'
9405 || index >= field_type->num_fields ()
9406 || (TYPE_FIELD_LOC_KIND (field_type, index)
9407 != FIELD_LOC_KIND_BITPOS))
9408 {
9409 complaint (_("Could not parse Rust enum encoding string \"%s\""
9410 "[in module %s]"),
9411 TYPE_FIELD_NAME (type, 0),
9412 objfile_name (objfile));
9413 return;
9414 }
9415 ++name;
9416
9417 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9418 field_type = TYPE_FIELD_TYPE (field_type, index);
9419 }
9420
9421 /* Smash this type to be a structure type. We have to do this
9422 because the type has already been recorded. */
9423 type->set_code (TYPE_CODE_STRUCT);
9424 type->set_num_fields (3);
9425 /* Save the field we care about. */
9426 struct field saved_field = type->field (0);
9427 type->set_fields
9428 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9429
9430 /* Put the discriminant at index 0. */
9431 TYPE_FIELD_TYPE (type, 0) = field_type;
9432 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9433 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9434 SET_FIELD_BITPOS (type->field (0), bit_offset);
9435
9436 /* The order of fields doesn't really matter, so put the real
9437 field at index 1 and the data-less field at index 2. */
9438 type->field (1) = saved_field;
9439 TYPE_FIELD_NAME (type, 1)
9440 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9441 TYPE_FIELD_TYPE (type, 1)->set_name
9442 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9443 TYPE_FIELD_NAME (type, 1)));
9444
9445 const char *dataless_name
9446 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9447 name);
9448 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9449 dataless_name);
9450 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9451 /* NAME points into the original discriminant name, which
9452 already has the correct lifetime. */
9453 TYPE_FIELD_NAME (type, 2) = name;
9454 SET_FIELD_BITPOS (type->field (2), 0);
9455
9456 /* Indicate that this is a variant type. */
9457 static discriminant_range ranges[1] = { { 0, 0 } };
9458 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9459 }
9460 /* A union with a single anonymous field is probably an old-style
9461 univariant enum. */
9462 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9463 {
9464 /* Smash this type to be a structure type. We have to do this
9465 because the type has already been recorded. */
9466 type->set_code (TYPE_CODE_STRUCT);
9467
9468 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9469 const char *variant_name
9470 = rust_last_path_segment (field_type->name ());
9471 TYPE_FIELD_NAME (type, 0) = variant_name;
9472 field_type->set_name
9473 (rust_fully_qualify (&objfile->objfile_obstack,
9474 type->name (), variant_name));
9475 }
9476 else
9477 {
9478 struct type *disr_type = nullptr;
9479 for (int i = 0; i < type->num_fields (); ++i)
9480 {
9481 disr_type = TYPE_FIELD_TYPE (type, i);
9482
9483 if (disr_type->code () != TYPE_CODE_STRUCT)
9484 {
9485 /* All fields of a true enum will be structs. */
9486 return;
9487 }
9488 else if (disr_type->num_fields () == 0)
9489 {
9490 /* Could be data-less variant, so keep going. */
9491 disr_type = nullptr;
9492 }
9493 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9494 "RUST$ENUM$DISR") != 0)
9495 {
9496 /* Not a Rust enum. */
9497 return;
9498 }
9499 else
9500 {
9501 /* Found one. */
9502 break;
9503 }
9504 }
9505
9506 /* If we got here without a discriminant, then it's probably
9507 just a union. */
9508 if (disr_type == nullptr)
9509 return;
9510
9511 /* Smash this type to be a structure type. We have to do this
9512 because the type has already been recorded. */
9513 type->set_code (TYPE_CODE_STRUCT);
9514
9515 /* Make space for the discriminant field. */
9516 struct field *disr_field = &disr_type->field (0);
9517 field *new_fields
9518 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9519 * sizeof (struct field)));
9520 memcpy (new_fields + 1, type->fields (),
9521 type->num_fields () * sizeof (struct field));
9522 type->set_fields (new_fields);
9523 type->set_num_fields (type->num_fields () + 1);
9524
9525 /* Install the discriminant at index 0 in the union. */
9526 type->field (0) = *disr_field;
9527 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9528 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9529
9530 /* We need a way to find the correct discriminant given a
9531 variant name. For convenience we build a map here. */
9532 struct type *enum_type = FIELD_TYPE (*disr_field);
9533 std::unordered_map<std::string, ULONGEST> discriminant_map;
9534 for (int i = 0; i < enum_type->num_fields (); ++i)
9535 {
9536 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9537 {
9538 const char *name
9539 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9540 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9541 }
9542 }
9543
9544 int n_fields = type->num_fields ();
9545 /* We don't need a range entry for the discriminant, but we do
9546 need one for every other field, as there is no default
9547 variant. */
9548 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9549 discriminant_range,
9550 n_fields - 1);
9551 /* Skip the discriminant here. */
9552 for (int i = 1; i < n_fields; ++i)
9553 {
9554 /* Find the final word in the name of this variant's type.
9555 That name can be used to look up the correct
9556 discriminant. */
9557 const char *variant_name
9558 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9559
9560 auto iter = discriminant_map.find (variant_name);
9561 if (iter != discriminant_map.end ())
9562 {
9563 ranges[i].low = iter->second;
9564 ranges[i].high = iter->second;
9565 }
9566
9567 /* Remove the discriminant field, if it exists. */
9568 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9569 if (sub_type->num_fields () > 0)
9570 {
9571 sub_type->set_num_fields (sub_type->num_fields () - 1);
9572 sub_type->set_fields (sub_type->fields () + 1);
9573 }
9574 TYPE_FIELD_NAME (type, i) = variant_name;
9575 sub_type->set_name
9576 (rust_fully_qualify (&objfile->objfile_obstack,
9577 type->name (), variant_name));
9578 }
9579
9580 /* Indicate that this is a variant type. */
9581 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9582 gdb::array_view<discriminant_range> (ranges,
9583 n_fields - 1));
9584 }
9585 }
9586
9587 /* Rewrite some Rust unions to be structures with variants parts. */
9588
9589 static void
9590 rust_union_quirks (struct dwarf2_cu *cu)
9591 {
9592 gdb_assert (cu->language == language_rust);
9593 for (type *type_ : cu->rust_unions)
9594 quirk_rust_enum (type_, cu->per_objfile->objfile);
9595 /* We don't need this any more. */
9596 cu->rust_unions.clear ();
9597 }
9598
9599 /* See read.h. */
9600
9601 type_unit_group_unshareable *
9602 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9603 {
9604 auto iter = this->m_type_units.find (tu_group);
9605 if (iter != this->m_type_units.end ())
9606 return iter->second.get ();
9607
9608 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9609 type_unit_group_unshareable *result = uniq.get ();
9610 this->m_type_units[tu_group] = std::move (uniq);
9611 return result;
9612 }
9613
9614 struct type *
9615 dwarf2_per_objfile::get_type_for_signatured_type
9616 (signatured_type *sig_type) const
9617 {
9618 auto iter = this->m_type_map.find (sig_type);
9619 if (iter == this->m_type_map.end ())
9620 return nullptr;
9621
9622 return iter->second;
9623 }
9624
9625 void dwarf2_per_objfile::set_type_for_signatured_type
9626 (signatured_type *sig_type, struct type *type)
9627 {
9628 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9629
9630 this->m_type_map[sig_type] = type;
9631 }
9632
9633 /* A helper function for computing the list of all symbol tables
9634 included by PER_CU. */
9635
9636 static void
9637 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9638 htab_t all_children, htab_t all_type_symtabs,
9639 dwarf2_per_cu_data *per_cu,
9640 dwarf2_per_objfile *per_objfile,
9641 struct compunit_symtab *immediate_parent)
9642 {
9643 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9644 if (*slot != NULL)
9645 {
9646 /* This inclusion and its children have been processed. */
9647 return;
9648 }
9649
9650 *slot = per_cu;
9651
9652 /* Only add a CU if it has a symbol table. */
9653 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9654 if (cust != NULL)
9655 {
9656 /* If this is a type unit only add its symbol table if we haven't
9657 seen it yet (type unit per_cu's can share symtabs). */
9658 if (per_cu->is_debug_types)
9659 {
9660 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9661 if (*slot == NULL)
9662 {
9663 *slot = cust;
9664 result->push_back (cust);
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
9667 }
9668 }
9669 else
9670 {
9671 result->push_back (cust);
9672 if (cust->user == NULL)
9673 cust->user = immediate_parent;
9674 }
9675 }
9676
9677 if (!per_cu->imported_symtabs_empty ())
9678 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9679 {
9680 recursively_compute_inclusions (result, all_children,
9681 all_type_symtabs, ptr, per_objfile,
9682 cust);
9683 }
9684 }
9685
9686 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9687 PER_CU. */
9688
9689 static void
9690 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9691 dwarf2_per_objfile *per_objfile)
9692 {
9693 gdb_assert (! per_cu->is_debug_types);
9694
9695 if (!per_cu->imported_symtabs_empty ())
9696 {
9697 int len;
9698 std::vector<compunit_symtab *> result_symtabs;
9699 htab_t all_children, all_type_symtabs;
9700 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9701
9702 /* If we don't have a symtab, we can just skip this case. */
9703 if (cust == NULL)
9704 return;
9705
9706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
9708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9709 NULL, xcalloc, xfree);
9710
9711 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9712 {
9713 recursively_compute_inclusions (&result_symtabs, all_children,
9714 all_type_symtabs, ptr, per_objfile,
9715 cust);
9716 }
9717
9718 /* Now we have a transitive closure of all the included symtabs. */
9719 len = result_symtabs.size ();
9720 cust->includes
9721 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9722 struct compunit_symtab *, len + 1);
9723 memcpy (cust->includes, result_symtabs.data (),
9724 len * sizeof (compunit_symtab *));
9725 cust->includes[len] = NULL;
9726
9727 htab_delete (all_children);
9728 htab_delete (all_type_symtabs);
9729 }
9730 }
9731
9732 /* Compute the 'includes' field for the symtabs of all the CUs we just
9733 read. */
9734
9735 static void
9736 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9737 {
9738 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9739 {
9740 if (! iter->is_debug_types)
9741 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9742 }
9743
9744 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9745 }
9746
9747 /* Generate full symbol information for CU, whose DIEs have
9748 already been loaded into memory. */
9749
9750 static void
9751 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9752 {
9753 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9754 struct objfile *objfile = dwarf2_per_objfile->objfile;
9755 struct gdbarch *gdbarch = objfile->arch ();
9756 CORE_ADDR lowpc, highpc;
9757 struct compunit_symtab *cust;
9758 CORE_ADDR baseaddr;
9759 struct block *static_block;
9760 CORE_ADDR addr;
9761
9762 baseaddr = objfile->text_section_offset ();
9763
9764 /* Clear the list here in case something was left over. */
9765 cu->method_list.clear ();
9766
9767 cu->language = pretend_language;
9768 cu->language_defn = language_def (cu->language);
9769
9770 /* Do line number decoding in read_file_scope () */
9771 process_die (cu->dies, cu);
9772
9773 /* For now fudge the Go package. */
9774 if (cu->language == language_go)
9775 fixup_go_packaging (cu);
9776
9777 /* Now that we have processed all the DIEs in the CU, all the types
9778 should be complete, and it should now be safe to compute all of the
9779 physnames. */
9780 compute_delayed_physnames (cu);
9781
9782 if (cu->language == language_rust)
9783 rust_union_quirks (cu);
9784
9785 /* Some compilers don't define a DW_AT_high_pc attribute for the
9786 compilation unit. If the DW_AT_high_pc is missing, synthesize
9787 it, by scanning the DIE's below the compilation unit. */
9788 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9789
9790 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9791 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9792
9793 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9794 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9795 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9796 addrmap to help ensure it has an accurate map of pc values belonging to
9797 this comp unit. */
9798 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9799
9800 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9801 SECT_OFF_TEXT (objfile),
9802 0);
9803
9804 if (cust != NULL)
9805 {
9806 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9807
9808 /* Set symtab language to language from DW_AT_language. If the
9809 compilation is from a C file generated by language preprocessors, do
9810 not set the language if it was already deduced by start_subfile. */
9811 if (!(cu->language == language_c
9812 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9813 COMPUNIT_FILETABS (cust)->language = cu->language;
9814
9815 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9816 produce DW_AT_location with location lists but it can be possibly
9817 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9818 there were bugs in prologue debug info, fixed later in GCC-4.5
9819 by "unwind info for epilogues" patch (which is not directly related).
9820
9821 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9822 needed, it would be wrong due to missing DW_AT_producer there.
9823
9824 Still one can confuse GDB by using non-standard GCC compilation
9825 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9826 */
9827 if (cu->has_loclist && gcc_4_minor >= 5)
9828 cust->locations_valid = 1;
9829
9830 if (gcc_4_minor >= 5)
9831 cust->epilogue_unwind_valid = 1;
9832
9833 cust->call_site_htab = cu->call_site_htab;
9834 }
9835
9836 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9837
9838 /* Push it for inclusion processing later. */
9839 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9840
9841 /* Not needed any more. */
9842 cu->reset_builder ();
9843 }
9844
9845 /* Generate full symbol information for type unit CU, whose DIEs have
9846 already been loaded into memory. */
9847
9848 static void
9849 process_full_type_unit (dwarf2_cu *cu,
9850 enum language pretend_language)
9851 {
9852 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9853 struct objfile *objfile = dwarf2_per_objfile->objfile;
9854 struct compunit_symtab *cust;
9855 struct signatured_type *sig_type;
9856
9857 gdb_assert (cu->per_cu->is_debug_types);
9858 sig_type = (struct signatured_type *) cu->per_cu;
9859
9860 /* Clear the list here in case something was left over. */
9861 cu->method_list.clear ();
9862
9863 cu->language = pretend_language;
9864 cu->language_defn = language_def (cu->language);
9865
9866 /* The symbol tables are set up in read_type_unit_scope. */
9867 process_die (cu->dies, cu);
9868
9869 /* For now fudge the Go package. */
9870 if (cu->language == language_go)
9871 fixup_go_packaging (cu);
9872
9873 /* Now that we have processed all the DIEs in the CU, all the types
9874 should be complete, and it should now be safe to compute all of the
9875 physnames. */
9876 compute_delayed_physnames (cu);
9877
9878 if (cu->language == language_rust)
9879 rust_union_quirks (cu);
9880
9881 /* TUs share symbol tables.
9882 If this is the first TU to use this symtab, complete the construction
9883 of it with end_expandable_symtab. Otherwise, complete the addition of
9884 this TU's symbols to the existing symtab. */
9885 type_unit_group_unshareable *tug_unshare =
9886 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9887 if (tug_unshare->compunit_symtab == NULL)
9888 {
9889 buildsym_compunit *builder = cu->get_builder ();
9890 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9891 tug_unshare->compunit_symtab = cust;
9892
9893 if (cust != NULL)
9894 {
9895 /* Set symtab language to language from DW_AT_language. If the
9896 compilation is from a C file generated by language preprocessors,
9897 do not set the language if it was already deduced by
9898 start_subfile. */
9899 if (!(cu->language == language_c
9900 && COMPUNIT_FILETABS (cust)->language != language_c))
9901 COMPUNIT_FILETABS (cust)->language = cu->language;
9902 }
9903 }
9904 else
9905 {
9906 cu->get_builder ()->augment_type_symtab ();
9907 cust = tug_unshare->compunit_symtab;
9908 }
9909
9910 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9911
9912 /* Not needed any more. */
9913 cu->reset_builder ();
9914 }
9915
9916 /* Process an imported unit DIE. */
9917
9918 static void
9919 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9920 {
9921 struct attribute *attr;
9922
9923 /* For now we don't handle imported units in type units. */
9924 if (cu->per_cu->is_debug_types)
9925 {
9926 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9927 " supported in type units [in module %s]"),
9928 objfile_name (cu->per_objfile->objfile));
9929 }
9930
9931 attr = dwarf2_attr (die, DW_AT_import, cu);
9932 if (attr != NULL)
9933 {
9934 sect_offset sect_off = attr->get_ref_die_offset ();
9935 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9936 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9937 dwarf2_per_cu_data *per_cu
9938 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9939
9940 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9941 into another compilation unit, at root level. Regard this as a hint,
9942 and ignore it. */
9943 if (die->parent && die->parent->parent == NULL
9944 && per_cu->unit_type == DW_UT_compile
9945 && per_cu->lang == language_cplus)
9946 return;
9947
9948 /* If necessary, add it to the queue and load its DIEs. */
9949 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9950 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9951
9952 cu->per_cu->imported_symtabs_push (per_cu);
9953 }
9954 }
9955
9956 /* RAII object that represents a process_die scope: i.e.,
9957 starts/finishes processing a DIE. */
9958 class process_die_scope
9959 {
9960 public:
9961 process_die_scope (die_info *die, dwarf2_cu *cu)
9962 : m_die (die), m_cu (cu)
9963 {
9964 /* We should only be processing DIEs not already in process. */
9965 gdb_assert (!m_die->in_process);
9966 m_die->in_process = true;
9967 }
9968
9969 ~process_die_scope ()
9970 {
9971 m_die->in_process = false;
9972
9973 /* If we're done processing the DIE for the CU that owns the line
9974 header, we don't need the line header anymore. */
9975 if (m_cu->line_header_die_owner == m_die)
9976 {
9977 delete m_cu->line_header;
9978 m_cu->line_header = NULL;
9979 m_cu->line_header_die_owner = NULL;
9980 }
9981 }
9982
9983 private:
9984 die_info *m_die;
9985 dwarf2_cu *m_cu;
9986 };
9987
9988 /* Process a die and its children. */
9989
9990 static void
9991 process_die (struct die_info *die, struct dwarf2_cu *cu)
9992 {
9993 process_die_scope scope (die, cu);
9994
9995 switch (die->tag)
9996 {
9997 case DW_TAG_padding:
9998 break;
9999 case DW_TAG_compile_unit:
10000 case DW_TAG_partial_unit:
10001 read_file_scope (die, cu);
10002 break;
10003 case DW_TAG_type_unit:
10004 read_type_unit_scope (die, cu);
10005 break;
10006 case DW_TAG_subprogram:
10007 /* Nested subprograms in Fortran get a prefix. */
10008 if (cu->language == language_fortran
10009 && die->parent != NULL
10010 && die->parent->tag == DW_TAG_subprogram)
10011 cu->processing_has_namespace_info = true;
10012 /* Fall through. */
10013 case DW_TAG_inlined_subroutine:
10014 read_func_scope (die, cu);
10015 break;
10016 case DW_TAG_lexical_block:
10017 case DW_TAG_try_block:
10018 case DW_TAG_catch_block:
10019 read_lexical_block_scope (die, cu);
10020 break;
10021 case DW_TAG_call_site:
10022 case DW_TAG_GNU_call_site:
10023 read_call_site_scope (die, cu);
10024 break;
10025 case DW_TAG_class_type:
10026 case DW_TAG_interface_type:
10027 case DW_TAG_structure_type:
10028 case DW_TAG_union_type:
10029 process_structure_scope (die, cu);
10030 break;
10031 case DW_TAG_enumeration_type:
10032 process_enumeration_scope (die, cu);
10033 break;
10034
10035 /* These dies have a type, but processing them does not create
10036 a symbol or recurse to process the children. Therefore we can
10037 read them on-demand through read_type_die. */
10038 case DW_TAG_subroutine_type:
10039 case DW_TAG_set_type:
10040 case DW_TAG_array_type:
10041 case DW_TAG_pointer_type:
10042 case DW_TAG_ptr_to_member_type:
10043 case DW_TAG_reference_type:
10044 case DW_TAG_rvalue_reference_type:
10045 case DW_TAG_string_type:
10046 break;
10047
10048 case DW_TAG_base_type:
10049 case DW_TAG_subrange_type:
10050 case DW_TAG_typedef:
10051 /* Add a typedef symbol for the type definition, if it has a
10052 DW_AT_name. */
10053 new_symbol (die, read_type_die (die, cu), cu);
10054 break;
10055 case DW_TAG_common_block:
10056 read_common_block (die, cu);
10057 break;
10058 case DW_TAG_common_inclusion:
10059 break;
10060 case DW_TAG_namespace:
10061 cu->processing_has_namespace_info = true;
10062 read_namespace (die, cu);
10063 break;
10064 case DW_TAG_module:
10065 cu->processing_has_namespace_info = true;
10066 read_module (die, cu);
10067 break;
10068 case DW_TAG_imported_declaration:
10069 cu->processing_has_namespace_info = true;
10070 if (read_namespace_alias (die, cu))
10071 break;
10072 /* The declaration is not a global namespace alias. */
10073 /* Fall through. */
10074 case DW_TAG_imported_module:
10075 cu->processing_has_namespace_info = true;
10076 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10077 || cu->language != language_fortran))
10078 complaint (_("Tag '%s' has unexpected children"),
10079 dwarf_tag_name (die->tag));
10080 read_import_statement (die, cu);
10081 break;
10082
10083 case DW_TAG_imported_unit:
10084 process_imported_unit_die (die, cu);
10085 break;
10086
10087 case DW_TAG_variable:
10088 read_variable (die, cu);
10089 break;
10090
10091 default:
10092 new_symbol (die, NULL, cu);
10093 break;
10094 }
10095 }
10096 \f
10097 /* DWARF name computation. */
10098
10099 /* A helper function for dwarf2_compute_name which determines whether DIE
10100 needs to have the name of the scope prepended to the name listed in the
10101 die. */
10102
10103 static int
10104 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10105 {
10106 struct attribute *attr;
10107
10108 switch (die->tag)
10109 {
10110 case DW_TAG_namespace:
10111 case DW_TAG_typedef:
10112 case DW_TAG_class_type:
10113 case DW_TAG_interface_type:
10114 case DW_TAG_structure_type:
10115 case DW_TAG_union_type:
10116 case DW_TAG_enumeration_type:
10117 case DW_TAG_enumerator:
10118 case DW_TAG_subprogram:
10119 case DW_TAG_inlined_subroutine:
10120 case DW_TAG_member:
10121 case DW_TAG_imported_declaration:
10122 return 1;
10123
10124 case DW_TAG_variable:
10125 case DW_TAG_constant:
10126 /* We only need to prefix "globally" visible variables. These include
10127 any variable marked with DW_AT_external or any variable that
10128 lives in a namespace. [Variables in anonymous namespaces
10129 require prefixing, but they are not DW_AT_external.] */
10130
10131 if (dwarf2_attr (die, DW_AT_specification, cu))
10132 {
10133 struct dwarf2_cu *spec_cu = cu;
10134
10135 return die_needs_namespace (die_specification (die, &spec_cu),
10136 spec_cu);
10137 }
10138
10139 attr = dwarf2_attr (die, DW_AT_external, cu);
10140 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10141 && die->parent->tag != DW_TAG_module)
10142 return 0;
10143 /* A variable in a lexical block of some kind does not need a
10144 namespace, even though in C++ such variables may be external
10145 and have a mangled name. */
10146 if (die->parent->tag == DW_TAG_lexical_block
10147 || die->parent->tag == DW_TAG_try_block
10148 || die->parent->tag == DW_TAG_catch_block
10149 || die->parent->tag == DW_TAG_subprogram)
10150 return 0;
10151 return 1;
10152
10153 default:
10154 return 0;
10155 }
10156 }
10157
10158 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10159 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10160 defined for the given DIE. */
10161
10162 static struct attribute *
10163 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10164 {
10165 struct attribute *attr;
10166
10167 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10168 if (attr == NULL)
10169 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10170
10171 return attr;
10172 }
10173
10174 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10175 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10176 defined for the given DIE. */
10177
10178 static const char *
10179 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10180 {
10181 const char *linkage_name;
10182
10183 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10184 if (linkage_name == NULL)
10185 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10186
10187 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10188 See https://github.com/rust-lang/rust/issues/32925. */
10189 if (cu->language == language_rust && linkage_name != NULL
10190 && strchr (linkage_name, '{') != NULL)
10191 linkage_name = NULL;
10192
10193 return linkage_name;
10194 }
10195
10196 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10197 compute the physname for the object, which include a method's:
10198 - formal parameters (C++),
10199 - receiver type (Go),
10200
10201 The term "physname" is a bit confusing.
10202 For C++, for example, it is the demangled name.
10203 For Go, for example, it's the mangled name.
10204
10205 For Ada, return the DIE's linkage name rather than the fully qualified
10206 name. PHYSNAME is ignored..
10207
10208 The result is allocated on the objfile->per_bfd's obstack and
10209 canonicalized. */
10210
10211 static const char *
10212 dwarf2_compute_name (const char *name,
10213 struct die_info *die, struct dwarf2_cu *cu,
10214 int physname)
10215 {
10216 struct objfile *objfile = cu->per_objfile->objfile;
10217
10218 if (name == NULL)
10219 name = dwarf2_name (die, cu);
10220
10221 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10222 but otherwise compute it by typename_concat inside GDB.
10223 FIXME: Actually this is not really true, or at least not always true.
10224 It's all very confusing. compute_and_set_names doesn't try to demangle
10225 Fortran names because there is no mangling standard. So new_symbol
10226 will set the demangled name to the result of dwarf2_full_name, and it is
10227 the demangled name that GDB uses if it exists. */
10228 if (cu->language == language_ada
10229 || (cu->language == language_fortran && physname))
10230 {
10231 /* For Ada unit, we prefer the linkage name over the name, as
10232 the former contains the exported name, which the user expects
10233 to be able to reference. Ideally, we want the user to be able
10234 to reference this entity using either natural or linkage name,
10235 but we haven't started looking at this enhancement yet. */
10236 const char *linkage_name = dw2_linkage_name (die, cu);
10237
10238 if (linkage_name != NULL)
10239 return linkage_name;
10240 }
10241
10242 /* These are the only languages we know how to qualify names in. */
10243 if (name != NULL
10244 && (cu->language == language_cplus
10245 || cu->language == language_fortran || cu->language == language_d
10246 || cu->language == language_rust))
10247 {
10248 if (die_needs_namespace (die, cu))
10249 {
10250 const char *prefix;
10251 const char *canonical_name = NULL;
10252
10253 string_file buf;
10254
10255 prefix = determine_prefix (die, cu);
10256 if (*prefix != '\0')
10257 {
10258 gdb::unique_xmalloc_ptr<char> prefixed_name
10259 (typename_concat (NULL, prefix, name, physname, cu));
10260
10261 buf.puts (prefixed_name.get ());
10262 }
10263 else
10264 buf.puts (name);
10265
10266 /* Template parameters may be specified in the DIE's DW_AT_name, or
10267 as children with DW_TAG_template_type_param or
10268 DW_TAG_value_type_param. If the latter, add them to the name
10269 here. If the name already has template parameters, then
10270 skip this step; some versions of GCC emit both, and
10271 it is more efficient to use the pre-computed name.
10272
10273 Something to keep in mind about this process: it is very
10274 unlikely, or in some cases downright impossible, to produce
10275 something that will match the mangled name of a function.
10276 If the definition of the function has the same debug info,
10277 we should be able to match up with it anyway. But fallbacks
10278 using the minimal symbol, for instance to find a method
10279 implemented in a stripped copy of libstdc++, will not work.
10280 If we do not have debug info for the definition, we will have to
10281 match them up some other way.
10282
10283 When we do name matching there is a related problem with function
10284 templates; two instantiated function templates are allowed to
10285 differ only by their return types, which we do not add here. */
10286
10287 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10288 {
10289 struct attribute *attr;
10290 struct die_info *child;
10291 int first = 1;
10292
10293 die->building_fullname = 1;
10294
10295 for (child = die->child; child != NULL; child = child->sibling)
10296 {
10297 struct type *type;
10298 LONGEST value;
10299 const gdb_byte *bytes;
10300 struct dwarf2_locexpr_baton *baton;
10301 struct value *v;
10302
10303 if (child->tag != DW_TAG_template_type_param
10304 && child->tag != DW_TAG_template_value_param)
10305 continue;
10306
10307 if (first)
10308 {
10309 buf.puts ("<");
10310 first = 0;
10311 }
10312 else
10313 buf.puts (", ");
10314
10315 attr = dwarf2_attr (child, DW_AT_type, cu);
10316 if (attr == NULL)
10317 {
10318 complaint (_("template parameter missing DW_AT_type"));
10319 buf.puts ("UNKNOWN_TYPE");
10320 continue;
10321 }
10322 type = die_type (child, cu);
10323
10324 if (child->tag == DW_TAG_template_type_param)
10325 {
10326 c_print_type (type, "", &buf, -1, 0, cu->language,
10327 &type_print_raw_options);
10328 continue;
10329 }
10330
10331 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10332 if (attr == NULL)
10333 {
10334 complaint (_("template parameter missing "
10335 "DW_AT_const_value"));
10336 buf.puts ("UNKNOWN_VALUE");
10337 continue;
10338 }
10339
10340 dwarf2_const_value_attr (attr, type, name,
10341 &cu->comp_unit_obstack, cu,
10342 &value, &bytes, &baton);
10343
10344 if (TYPE_NOSIGN (type))
10345 /* GDB prints characters as NUMBER 'CHAR'. If that's
10346 changed, this can use value_print instead. */
10347 c_printchar (value, type, &buf);
10348 else
10349 {
10350 struct value_print_options opts;
10351
10352 if (baton != NULL)
10353 v = dwarf2_evaluate_loc_desc (type, NULL,
10354 baton->data,
10355 baton->size,
10356 baton->per_cu,
10357 baton->per_objfile);
10358 else if (bytes != NULL)
10359 {
10360 v = allocate_value (type);
10361 memcpy (value_contents_writeable (v), bytes,
10362 TYPE_LENGTH (type));
10363 }
10364 else
10365 v = value_from_longest (type, value);
10366
10367 /* Specify decimal so that we do not depend on
10368 the radix. */
10369 get_formatted_print_options (&opts, 'd');
10370 opts.raw = 1;
10371 value_print (v, &buf, &opts);
10372 release_value (v);
10373 }
10374 }
10375
10376 die->building_fullname = 0;
10377
10378 if (!first)
10379 {
10380 /* Close the argument list, with a space if necessary
10381 (nested templates). */
10382 if (!buf.empty () && buf.string ().back () == '>')
10383 buf.puts (" >");
10384 else
10385 buf.puts (">");
10386 }
10387 }
10388
10389 /* For C++ methods, append formal parameter type
10390 information, if PHYSNAME. */
10391
10392 if (physname && die->tag == DW_TAG_subprogram
10393 && cu->language == language_cplus)
10394 {
10395 struct type *type = read_type_die (die, cu);
10396
10397 c_type_print_args (type, &buf, 1, cu->language,
10398 &type_print_raw_options);
10399
10400 if (cu->language == language_cplus)
10401 {
10402 /* Assume that an artificial first parameter is
10403 "this", but do not crash if it is not. RealView
10404 marks unnamed (and thus unused) parameters as
10405 artificial; there is no way to differentiate
10406 the two cases. */
10407 if (type->num_fields () > 0
10408 && TYPE_FIELD_ARTIFICIAL (type, 0)
10409 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10410 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10411 0))))
10412 buf.puts (" const");
10413 }
10414 }
10415
10416 const std::string &intermediate_name = buf.string ();
10417
10418 if (cu->language == language_cplus)
10419 canonical_name
10420 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10421 objfile);
10422
10423 /* If we only computed INTERMEDIATE_NAME, or if
10424 INTERMEDIATE_NAME is already canonical, then we need to
10425 intern it. */
10426 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10427 name = objfile->intern (intermediate_name);
10428 else
10429 name = canonical_name;
10430 }
10431 }
10432
10433 return name;
10434 }
10435
10436 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10437 If scope qualifiers are appropriate they will be added. The result
10438 will be allocated on the storage_obstack, or NULL if the DIE does
10439 not have a name. NAME may either be from a previous call to
10440 dwarf2_name or NULL.
10441
10442 The output string will be canonicalized (if C++). */
10443
10444 static const char *
10445 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10446 {
10447 return dwarf2_compute_name (name, die, cu, 0);
10448 }
10449
10450 /* Construct a physname for the given DIE in CU. NAME may either be
10451 from a previous call to dwarf2_name or NULL. The result will be
10452 allocated on the objfile_objstack or NULL if the DIE does not have a
10453 name.
10454
10455 The output string will be canonicalized (if C++). */
10456
10457 static const char *
10458 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10459 {
10460 struct objfile *objfile = cu->per_objfile->objfile;
10461 const char *retval, *mangled = NULL, *canon = NULL;
10462 int need_copy = 1;
10463
10464 /* In this case dwarf2_compute_name is just a shortcut not building anything
10465 on its own. */
10466 if (!die_needs_namespace (die, cu))
10467 return dwarf2_compute_name (name, die, cu, 1);
10468
10469 if (cu->language != language_rust)
10470 mangled = dw2_linkage_name (die, cu);
10471
10472 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10473 has computed. */
10474 gdb::unique_xmalloc_ptr<char> demangled;
10475 if (mangled != NULL)
10476 {
10477
10478 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10479 {
10480 /* Do nothing (do not demangle the symbol name). */
10481 }
10482 else if (cu->language == language_go)
10483 {
10484 /* This is a lie, but we already lie to the caller new_symbol.
10485 new_symbol assumes we return the mangled name.
10486 This just undoes that lie until things are cleaned up. */
10487 }
10488 else
10489 {
10490 /* Use DMGL_RET_DROP for C++ template functions to suppress
10491 their return type. It is easier for GDB users to search
10492 for such functions as `name(params)' than `long name(params)'.
10493 In such case the minimal symbol names do not match the full
10494 symbol names but for template functions there is never a need
10495 to look up their definition from their declaration so
10496 the only disadvantage remains the minimal symbol variant
10497 `long name(params)' does not have the proper inferior type. */
10498 demangled.reset (gdb_demangle (mangled,
10499 (DMGL_PARAMS | DMGL_ANSI
10500 | DMGL_RET_DROP)));
10501 }
10502 if (demangled)
10503 canon = demangled.get ();
10504 else
10505 {
10506 canon = mangled;
10507 need_copy = 0;
10508 }
10509 }
10510
10511 if (canon == NULL || check_physname)
10512 {
10513 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10514
10515 if (canon != NULL && strcmp (physname, canon) != 0)
10516 {
10517 /* It may not mean a bug in GDB. The compiler could also
10518 compute DW_AT_linkage_name incorrectly. But in such case
10519 GDB would need to be bug-to-bug compatible. */
10520
10521 complaint (_("Computed physname <%s> does not match demangled <%s> "
10522 "(from linkage <%s>) - DIE at %s [in module %s]"),
10523 physname, canon, mangled, sect_offset_str (die->sect_off),
10524 objfile_name (objfile));
10525
10526 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10527 is available here - over computed PHYSNAME. It is safer
10528 against both buggy GDB and buggy compilers. */
10529
10530 retval = canon;
10531 }
10532 else
10533 {
10534 retval = physname;
10535 need_copy = 0;
10536 }
10537 }
10538 else
10539 retval = canon;
10540
10541 if (need_copy)
10542 retval = objfile->intern (retval);
10543
10544 return retval;
10545 }
10546
10547 /* Inspect DIE in CU for a namespace alias. If one exists, record
10548 a new symbol for it.
10549
10550 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10551
10552 static int
10553 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10554 {
10555 struct attribute *attr;
10556
10557 /* If the die does not have a name, this is not a namespace
10558 alias. */
10559 attr = dwarf2_attr (die, DW_AT_name, cu);
10560 if (attr != NULL)
10561 {
10562 int num;
10563 struct die_info *d = die;
10564 struct dwarf2_cu *imported_cu = cu;
10565
10566 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10567 keep inspecting DIEs until we hit the underlying import. */
10568 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10569 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10570 {
10571 attr = dwarf2_attr (d, DW_AT_import, cu);
10572 if (attr == NULL)
10573 break;
10574
10575 d = follow_die_ref (d, attr, &imported_cu);
10576 if (d->tag != DW_TAG_imported_declaration)
10577 break;
10578 }
10579
10580 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10581 {
10582 complaint (_("DIE at %s has too many recursively imported "
10583 "declarations"), sect_offset_str (d->sect_off));
10584 return 0;
10585 }
10586
10587 if (attr != NULL)
10588 {
10589 struct type *type;
10590 sect_offset sect_off = attr->get_ref_die_offset ();
10591
10592 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10593 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10594 {
10595 /* This declaration is a global namespace alias. Add
10596 a symbol for it whose type is the aliased namespace. */
10597 new_symbol (die, type, cu);
10598 return 1;
10599 }
10600 }
10601 }
10602
10603 return 0;
10604 }
10605
10606 /* Return the using directives repository (global or local?) to use in the
10607 current context for CU.
10608
10609 For Ada, imported declarations can materialize renamings, which *may* be
10610 global. However it is impossible (for now?) in DWARF to distinguish
10611 "external" imported declarations and "static" ones. As all imported
10612 declarations seem to be static in all other languages, make them all CU-wide
10613 global only in Ada. */
10614
10615 static struct using_direct **
10616 using_directives (struct dwarf2_cu *cu)
10617 {
10618 if (cu->language == language_ada
10619 && cu->get_builder ()->outermost_context_p ())
10620 return cu->get_builder ()->get_global_using_directives ();
10621 else
10622 return cu->get_builder ()->get_local_using_directives ();
10623 }
10624
10625 /* Read the import statement specified by the given die and record it. */
10626
10627 static void
10628 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10629 {
10630 struct objfile *objfile = cu->per_objfile->objfile;
10631 struct attribute *import_attr;
10632 struct die_info *imported_die, *child_die;
10633 struct dwarf2_cu *imported_cu;
10634 const char *imported_name;
10635 const char *imported_name_prefix;
10636 const char *canonical_name;
10637 const char *import_alias;
10638 const char *imported_declaration = NULL;
10639 const char *import_prefix;
10640 std::vector<const char *> excludes;
10641
10642 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10643 if (import_attr == NULL)
10644 {
10645 complaint (_("Tag '%s' has no DW_AT_import"),
10646 dwarf_tag_name (die->tag));
10647 return;
10648 }
10649
10650 imported_cu = cu;
10651 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10652 imported_name = dwarf2_name (imported_die, imported_cu);
10653 if (imported_name == NULL)
10654 {
10655 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10656
10657 The import in the following code:
10658 namespace A
10659 {
10660 typedef int B;
10661 }
10662
10663 int main ()
10664 {
10665 using A::B;
10666 B b;
10667 return b;
10668 }
10669
10670 ...
10671 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10672 <52> DW_AT_decl_file : 1
10673 <53> DW_AT_decl_line : 6
10674 <54> DW_AT_import : <0x75>
10675 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10676 <59> DW_AT_name : B
10677 <5b> DW_AT_decl_file : 1
10678 <5c> DW_AT_decl_line : 2
10679 <5d> DW_AT_type : <0x6e>
10680 ...
10681 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10682 <76> DW_AT_byte_size : 4
10683 <77> DW_AT_encoding : 5 (signed)
10684
10685 imports the wrong die ( 0x75 instead of 0x58 ).
10686 This case will be ignored until the gcc bug is fixed. */
10687 return;
10688 }
10689
10690 /* Figure out the local name after import. */
10691 import_alias = dwarf2_name (die, cu);
10692
10693 /* Figure out where the statement is being imported to. */
10694 import_prefix = determine_prefix (die, cu);
10695
10696 /* Figure out what the scope of the imported die is and prepend it
10697 to the name of the imported die. */
10698 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10699
10700 if (imported_die->tag != DW_TAG_namespace
10701 && imported_die->tag != DW_TAG_module)
10702 {
10703 imported_declaration = imported_name;
10704 canonical_name = imported_name_prefix;
10705 }
10706 else if (strlen (imported_name_prefix) > 0)
10707 canonical_name = obconcat (&objfile->objfile_obstack,
10708 imported_name_prefix,
10709 (cu->language == language_d ? "." : "::"),
10710 imported_name, (char *) NULL);
10711 else
10712 canonical_name = imported_name;
10713
10714 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10715 for (child_die = die->child; child_die && child_die->tag;
10716 child_die = child_die->sibling)
10717 {
10718 /* DWARF-4: A Fortran use statement with a “rename list” may be
10719 represented by an imported module entry with an import attribute
10720 referring to the module and owned entries corresponding to those
10721 entities that are renamed as part of being imported. */
10722
10723 if (child_die->tag != DW_TAG_imported_declaration)
10724 {
10725 complaint (_("child DW_TAG_imported_declaration expected "
10726 "- DIE at %s [in module %s]"),
10727 sect_offset_str (child_die->sect_off),
10728 objfile_name (objfile));
10729 continue;
10730 }
10731
10732 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10733 if (import_attr == NULL)
10734 {
10735 complaint (_("Tag '%s' has no DW_AT_import"),
10736 dwarf_tag_name (child_die->tag));
10737 continue;
10738 }
10739
10740 imported_cu = cu;
10741 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10742 &imported_cu);
10743 imported_name = dwarf2_name (imported_die, imported_cu);
10744 if (imported_name == NULL)
10745 {
10746 complaint (_("child DW_TAG_imported_declaration has unknown "
10747 "imported name - DIE at %s [in module %s]"),
10748 sect_offset_str (child_die->sect_off),
10749 objfile_name (objfile));
10750 continue;
10751 }
10752
10753 excludes.push_back (imported_name);
10754
10755 process_die (child_die, cu);
10756 }
10757
10758 add_using_directive (using_directives (cu),
10759 import_prefix,
10760 canonical_name,
10761 import_alias,
10762 imported_declaration,
10763 excludes,
10764 0,
10765 &objfile->objfile_obstack);
10766 }
10767
10768 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10769 types, but gives them a size of zero. Starting with version 14,
10770 ICC is compatible with GCC. */
10771
10772 static bool
10773 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10774 {
10775 if (!cu->checked_producer)
10776 check_producer (cu);
10777
10778 return cu->producer_is_icc_lt_14;
10779 }
10780
10781 /* ICC generates a DW_AT_type for C void functions. This was observed on
10782 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10783 which says that void functions should not have a DW_AT_type. */
10784
10785 static bool
10786 producer_is_icc (struct dwarf2_cu *cu)
10787 {
10788 if (!cu->checked_producer)
10789 check_producer (cu);
10790
10791 return cu->producer_is_icc;
10792 }
10793
10794 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10795 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10796 this, it was first present in GCC release 4.3.0. */
10797
10798 static bool
10799 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10800 {
10801 if (!cu->checked_producer)
10802 check_producer (cu);
10803
10804 return cu->producer_is_gcc_lt_4_3;
10805 }
10806
10807 static file_and_directory
10808 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10809 {
10810 file_and_directory res;
10811
10812 /* Find the filename. Do not use dwarf2_name here, since the filename
10813 is not a source language identifier. */
10814 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10815 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10816
10817 if (res.comp_dir == NULL
10818 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10819 && IS_ABSOLUTE_PATH (res.name))
10820 {
10821 res.comp_dir_storage = ldirname (res.name);
10822 if (!res.comp_dir_storage.empty ())
10823 res.comp_dir = res.comp_dir_storage.c_str ();
10824 }
10825 if (res.comp_dir != NULL)
10826 {
10827 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10828 directory, get rid of it. */
10829 const char *cp = strchr (res.comp_dir, ':');
10830
10831 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10832 res.comp_dir = cp + 1;
10833 }
10834
10835 if (res.name == NULL)
10836 res.name = "<unknown>";
10837
10838 return res;
10839 }
10840
10841 /* Handle DW_AT_stmt_list for a compilation unit.
10842 DIE is the DW_TAG_compile_unit die for CU.
10843 COMP_DIR is the compilation directory. LOWPC is passed to
10844 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10845
10846 static void
10847 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10848 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10849 {
10850 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10851 struct attribute *attr;
10852 struct line_header line_header_local;
10853 hashval_t line_header_local_hash;
10854 void **slot;
10855 int decode_mapping;
10856
10857 gdb_assert (! cu->per_cu->is_debug_types);
10858
10859 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10860 if (attr == NULL)
10861 return;
10862
10863 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10864
10865 /* The line header hash table is only created if needed (it exists to
10866 prevent redundant reading of the line table for partial_units).
10867 If we're given a partial_unit, we'll need it. If we're given a
10868 compile_unit, then use the line header hash table if it's already
10869 created, but don't create one just yet. */
10870
10871 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10872 && die->tag == DW_TAG_partial_unit)
10873 {
10874 dwarf2_per_objfile->per_bfd->line_header_hash
10875 .reset (htab_create_alloc (127, line_header_hash_voidp,
10876 line_header_eq_voidp,
10877 free_line_header_voidp,
10878 xcalloc, xfree));
10879 }
10880
10881 line_header_local.sect_off = line_offset;
10882 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10883 line_header_local_hash = line_header_hash (&line_header_local);
10884 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10885 {
10886 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10887 &line_header_local,
10888 line_header_local_hash, NO_INSERT);
10889
10890 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10891 is not present in *SLOT (since if there is something in *SLOT then
10892 it will be for a partial_unit). */
10893 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10894 {
10895 gdb_assert (*slot != NULL);
10896 cu->line_header = (struct line_header *) *slot;
10897 return;
10898 }
10899 }
10900
10901 /* dwarf_decode_line_header does not yet provide sufficient information.
10902 We always have to call also dwarf_decode_lines for it. */
10903 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10904 if (lh == NULL)
10905 return;
10906
10907 cu->line_header = lh.release ();
10908 cu->line_header_die_owner = die;
10909
10910 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10911 slot = NULL;
10912 else
10913 {
10914 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10915 &line_header_local,
10916 line_header_local_hash, INSERT);
10917 gdb_assert (slot != NULL);
10918 }
10919 if (slot != NULL && *slot == NULL)
10920 {
10921 /* This newly decoded line number information unit will be owned
10922 by line_header_hash hash table. */
10923 *slot = cu->line_header;
10924 cu->line_header_die_owner = NULL;
10925 }
10926 else
10927 {
10928 /* We cannot free any current entry in (*slot) as that struct line_header
10929 may be already used by multiple CUs. Create only temporary decoded
10930 line_header for this CU - it may happen at most once for each line
10931 number information unit. And if we're not using line_header_hash
10932 then this is what we want as well. */
10933 gdb_assert (die->tag != DW_TAG_partial_unit);
10934 }
10935 decode_mapping = (die->tag != DW_TAG_partial_unit);
10936 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10937 decode_mapping);
10938
10939 }
10940
10941 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10942
10943 static void
10944 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10945 {
10946 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10947 struct objfile *objfile = dwarf2_per_objfile->objfile;
10948 struct gdbarch *gdbarch = objfile->arch ();
10949 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10950 CORE_ADDR highpc = ((CORE_ADDR) 0);
10951 struct attribute *attr;
10952 struct die_info *child_die;
10953 CORE_ADDR baseaddr;
10954
10955 prepare_one_comp_unit (cu, die, cu->language);
10956 baseaddr = objfile->text_section_offset ();
10957
10958 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10959
10960 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10961 from finish_block. */
10962 if (lowpc == ((CORE_ADDR) -1))
10963 lowpc = highpc;
10964 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10965
10966 file_and_directory fnd = find_file_and_directory (die, cu);
10967
10968 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10969 standardised yet. As a workaround for the language detection we fall
10970 back to the DW_AT_producer string. */
10971 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10972 cu->language = language_opencl;
10973
10974 /* Similar hack for Go. */
10975 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10976 set_cu_language (DW_LANG_Go, cu);
10977
10978 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10979
10980 /* Decode line number information if present. We do this before
10981 processing child DIEs, so that the line header table is available
10982 for DW_AT_decl_file. */
10983 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10984
10985 /* Process all dies in compilation unit. */
10986 if (die->child != NULL)
10987 {
10988 child_die = die->child;
10989 while (child_die && child_die->tag)
10990 {
10991 process_die (child_die, cu);
10992 child_die = child_die->sibling;
10993 }
10994 }
10995
10996 /* Decode macro information, if present. Dwarf 2 macro information
10997 refers to information in the line number info statement program
10998 header, so we can only read it if we've read the header
10999 successfully. */
11000 attr = dwarf2_attr (die, DW_AT_macros, cu);
11001 if (attr == NULL)
11002 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11003 if (attr && cu->line_header)
11004 {
11005 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11006 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11007
11008 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11009 }
11010 else
11011 {
11012 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11013 if (attr && cu->line_header)
11014 {
11015 unsigned int macro_offset = DW_UNSND (attr);
11016
11017 dwarf_decode_macros (cu, macro_offset, 0);
11018 }
11019 }
11020 }
11021
11022 void
11023 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11024 {
11025 struct type_unit_group *tu_group;
11026 int first_time;
11027 struct attribute *attr;
11028 unsigned int i;
11029 struct signatured_type *sig_type;
11030
11031 gdb_assert (per_cu->is_debug_types);
11032 sig_type = (struct signatured_type *) per_cu;
11033
11034 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11035
11036 /* If we're using .gdb_index (includes -readnow) then
11037 per_cu->type_unit_group may not have been set up yet. */
11038 if (sig_type->type_unit_group == NULL)
11039 sig_type->type_unit_group = get_type_unit_group (this, attr);
11040 tu_group = sig_type->type_unit_group;
11041
11042 /* If we've already processed this stmt_list there's no real need to
11043 do it again, we could fake it and just recreate the part we need
11044 (file name,index -> symtab mapping). If data shows this optimization
11045 is useful we can do it then. */
11046 type_unit_group_unshareable *tug_unshare
11047 = per_objfile->get_type_unit_group_unshareable (tu_group);
11048 first_time = tug_unshare->compunit_symtab == NULL;
11049
11050 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11051 debug info. */
11052 line_header_up lh;
11053 if (attr != NULL)
11054 {
11055 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11056 lh = dwarf_decode_line_header (line_offset, this);
11057 }
11058 if (lh == NULL)
11059 {
11060 if (first_time)
11061 start_symtab ("", NULL, 0);
11062 else
11063 {
11064 gdb_assert (tug_unshare->symtabs == NULL);
11065 gdb_assert (m_builder == nullptr);
11066 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11067 m_builder.reset (new struct buildsym_compunit
11068 (COMPUNIT_OBJFILE (cust), "",
11069 COMPUNIT_DIRNAME (cust),
11070 compunit_language (cust),
11071 0, cust));
11072 list_in_scope = get_builder ()->get_file_symbols ();
11073 }
11074 return;
11075 }
11076
11077 line_header = lh.release ();
11078 line_header_die_owner = die;
11079
11080 if (first_time)
11081 {
11082 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11083
11084 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11085 still initializing it, and our caller (a few levels up)
11086 process_full_type_unit still needs to know if this is the first
11087 time. */
11088
11089 tug_unshare->symtabs
11090 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11091 struct symtab *, line_header->file_names_size ());
11092
11093 auto &file_names = line_header->file_names ();
11094 for (i = 0; i < file_names.size (); ++i)
11095 {
11096 file_entry &fe = file_names[i];
11097 dwarf2_start_subfile (this, fe.name,
11098 fe.include_dir (line_header));
11099 buildsym_compunit *b = get_builder ();
11100 if (b->get_current_subfile ()->symtab == NULL)
11101 {
11102 /* NOTE: start_subfile will recognize when it's been
11103 passed a file it has already seen. So we can't
11104 assume there's a simple mapping from
11105 cu->line_header->file_names to subfiles, plus
11106 cu->line_header->file_names may contain dups. */
11107 b->get_current_subfile ()->symtab
11108 = allocate_symtab (cust, b->get_current_subfile ()->name);
11109 }
11110
11111 fe.symtab = b->get_current_subfile ()->symtab;
11112 tug_unshare->symtabs[i] = fe.symtab;
11113 }
11114 }
11115 else
11116 {
11117 gdb_assert (m_builder == nullptr);
11118 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11119 m_builder.reset (new struct buildsym_compunit
11120 (COMPUNIT_OBJFILE (cust), "",
11121 COMPUNIT_DIRNAME (cust),
11122 compunit_language (cust),
11123 0, cust));
11124 list_in_scope = get_builder ()->get_file_symbols ();
11125
11126 auto &file_names = line_header->file_names ();
11127 for (i = 0; i < file_names.size (); ++i)
11128 {
11129 file_entry &fe = file_names[i];
11130 fe.symtab = tug_unshare->symtabs[i];
11131 }
11132 }
11133
11134 /* The main symtab is allocated last. Type units don't have DW_AT_name
11135 so they don't have a "real" (so to speak) symtab anyway.
11136 There is later code that will assign the main symtab to all symbols
11137 that don't have one. We need to handle the case of a symbol with a
11138 missing symtab (DW_AT_decl_file) anyway. */
11139 }
11140
11141 /* Process DW_TAG_type_unit.
11142 For TUs we want to skip the first top level sibling if it's not the
11143 actual type being defined by this TU. In this case the first top
11144 level sibling is there to provide context only. */
11145
11146 static void
11147 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11148 {
11149 struct die_info *child_die;
11150
11151 prepare_one_comp_unit (cu, die, language_minimal);
11152
11153 /* Initialize (or reinitialize) the machinery for building symtabs.
11154 We do this before processing child DIEs, so that the line header table
11155 is available for DW_AT_decl_file. */
11156 cu->setup_type_unit_groups (die);
11157
11158 if (die->child != NULL)
11159 {
11160 child_die = die->child;
11161 while (child_die && child_die->tag)
11162 {
11163 process_die (child_die, cu);
11164 child_die = child_die->sibling;
11165 }
11166 }
11167 }
11168 \f
11169 /* DWO/DWP files.
11170
11171 http://gcc.gnu.org/wiki/DebugFission
11172 http://gcc.gnu.org/wiki/DebugFissionDWP
11173
11174 To simplify handling of both DWO files ("object" files with the DWARF info)
11175 and DWP files (a file with the DWOs packaged up into one file), we treat
11176 DWP files as having a collection of virtual DWO files. */
11177
11178 static hashval_t
11179 hash_dwo_file (const void *item)
11180 {
11181 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11182 hashval_t hash;
11183
11184 hash = htab_hash_string (dwo_file->dwo_name);
11185 if (dwo_file->comp_dir != NULL)
11186 hash += htab_hash_string (dwo_file->comp_dir);
11187 return hash;
11188 }
11189
11190 static int
11191 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11192 {
11193 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11194 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11195
11196 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11197 return 0;
11198 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11199 return lhs->comp_dir == rhs->comp_dir;
11200 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11201 }
11202
11203 /* Allocate a hash table for DWO files. */
11204
11205 static htab_up
11206 allocate_dwo_file_hash_table ()
11207 {
11208 auto delete_dwo_file = [] (void *item)
11209 {
11210 struct dwo_file *dwo_file = (struct dwo_file *) item;
11211
11212 delete dwo_file;
11213 };
11214
11215 return htab_up (htab_create_alloc (41,
11216 hash_dwo_file,
11217 eq_dwo_file,
11218 delete_dwo_file,
11219 xcalloc, xfree));
11220 }
11221
11222 /* Lookup DWO file DWO_NAME. */
11223
11224 static void **
11225 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11226 const char *dwo_name,
11227 const char *comp_dir)
11228 {
11229 struct dwo_file find_entry;
11230 void **slot;
11231
11232 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11233 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11234
11235 find_entry.dwo_name = dwo_name;
11236 find_entry.comp_dir = comp_dir;
11237 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11238 INSERT);
11239
11240 return slot;
11241 }
11242
11243 static hashval_t
11244 hash_dwo_unit (const void *item)
11245 {
11246 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11247
11248 /* This drops the top 32 bits of the id, but is ok for a hash. */
11249 return dwo_unit->signature;
11250 }
11251
11252 static int
11253 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11254 {
11255 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11256 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11257
11258 /* The signature is assumed to be unique within the DWO file.
11259 So while object file CU dwo_id's always have the value zero,
11260 that's OK, assuming each object file DWO file has only one CU,
11261 and that's the rule for now. */
11262 return lhs->signature == rhs->signature;
11263 }
11264
11265 /* Allocate a hash table for DWO CUs,TUs.
11266 There is one of these tables for each of CUs,TUs for each DWO file. */
11267
11268 static htab_up
11269 allocate_dwo_unit_table ()
11270 {
11271 /* Start out with a pretty small number.
11272 Generally DWO files contain only one CU and maybe some TUs. */
11273 return htab_up (htab_create_alloc (3,
11274 hash_dwo_unit,
11275 eq_dwo_unit,
11276 NULL, xcalloc, xfree));
11277 }
11278
11279 /* die_reader_func for create_dwo_cu. */
11280
11281 static void
11282 create_dwo_cu_reader (const struct die_reader_specs *reader,
11283 const gdb_byte *info_ptr,
11284 struct die_info *comp_unit_die,
11285 struct dwo_file *dwo_file,
11286 struct dwo_unit *dwo_unit)
11287 {
11288 struct dwarf2_cu *cu = reader->cu;
11289 sect_offset sect_off = cu->per_cu->sect_off;
11290 struct dwarf2_section_info *section = cu->per_cu->section;
11291
11292 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11293 if (!signature.has_value ())
11294 {
11295 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11296 " its dwo_id [in module %s]"),
11297 sect_offset_str (sect_off), dwo_file->dwo_name);
11298 return;
11299 }
11300
11301 dwo_unit->dwo_file = dwo_file;
11302 dwo_unit->signature = *signature;
11303 dwo_unit->section = section;
11304 dwo_unit->sect_off = sect_off;
11305 dwo_unit->length = cu->per_cu->length;
11306
11307 if (dwarf_read_debug)
11308 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11309 sect_offset_str (sect_off),
11310 hex_string (dwo_unit->signature));
11311 }
11312
11313 /* Create the dwo_units for the CUs in a DWO_FILE.
11314 Note: This function processes DWO files only, not DWP files. */
11315
11316 static void
11317 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11318 dwarf2_cu *cu, struct dwo_file &dwo_file,
11319 dwarf2_section_info &section, htab_up &cus_htab)
11320 {
11321 struct objfile *objfile = dwarf2_per_objfile->objfile;
11322 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11323 const gdb_byte *info_ptr, *end_ptr;
11324
11325 section.read (objfile);
11326 info_ptr = section.buffer;
11327
11328 if (info_ptr == NULL)
11329 return;
11330
11331 if (dwarf_read_debug)
11332 {
11333 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11334 section.get_name (),
11335 section.get_file_name ());
11336 }
11337
11338 end_ptr = info_ptr + section.size;
11339 while (info_ptr < end_ptr)
11340 {
11341 struct dwarf2_per_cu_data per_cu;
11342 struct dwo_unit read_unit {};
11343 struct dwo_unit *dwo_unit;
11344 void **slot;
11345 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11346
11347 memset (&per_cu, 0, sizeof (per_cu));
11348 per_cu.per_bfd = per_bfd;
11349 per_cu.is_debug_types = 0;
11350 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11351 per_cu.section = &section;
11352
11353 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11354 if (!reader.dummy_p)
11355 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11356 &dwo_file, &read_unit);
11357 info_ptr += per_cu.length;
11358
11359 // If the unit could not be parsed, skip it.
11360 if (read_unit.dwo_file == NULL)
11361 continue;
11362
11363 if (cus_htab == NULL)
11364 cus_htab = allocate_dwo_unit_table ();
11365
11366 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11367 struct dwo_unit);
11368 *dwo_unit = read_unit;
11369 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11370 gdb_assert (slot != NULL);
11371 if (*slot != NULL)
11372 {
11373 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11374 sect_offset dup_sect_off = dup_cu->sect_off;
11375
11376 complaint (_("debug cu entry at offset %s is duplicate to"
11377 " the entry at offset %s, signature %s"),
11378 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11379 hex_string (dwo_unit->signature));
11380 }
11381 *slot = (void *)dwo_unit;
11382 }
11383 }
11384
11385 /* DWP file .debug_{cu,tu}_index section format:
11386 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11387
11388 DWP Version 1:
11389
11390 Both index sections have the same format, and serve to map a 64-bit
11391 signature to a set of section numbers. Each section begins with a header,
11392 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11393 indexes, and a pool of 32-bit section numbers. The index sections will be
11394 aligned at 8-byte boundaries in the file.
11395
11396 The index section header consists of:
11397
11398 V, 32 bit version number
11399 -, 32 bits unused
11400 N, 32 bit number of compilation units or type units in the index
11401 M, 32 bit number of slots in the hash table
11402
11403 Numbers are recorded using the byte order of the application binary.
11404
11405 The hash table begins at offset 16 in the section, and consists of an array
11406 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11407 order of the application binary). Unused slots in the hash table are 0.
11408 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11409
11410 The parallel table begins immediately after the hash table
11411 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11412 array of 32-bit indexes (using the byte order of the application binary),
11413 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11414 table contains a 32-bit index into the pool of section numbers. For unused
11415 hash table slots, the corresponding entry in the parallel table will be 0.
11416
11417 The pool of section numbers begins immediately following the hash table
11418 (at offset 16 + 12 * M from the beginning of the section). The pool of
11419 section numbers consists of an array of 32-bit words (using the byte order
11420 of the application binary). Each item in the array is indexed starting
11421 from 0. The hash table entry provides the index of the first section
11422 number in the set. Additional section numbers in the set follow, and the
11423 set is terminated by a 0 entry (section number 0 is not used in ELF).
11424
11425 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11426 section must be the first entry in the set, and the .debug_abbrev.dwo must
11427 be the second entry. Other members of the set may follow in any order.
11428
11429 ---
11430
11431 DWP Version 2:
11432
11433 DWP Version 2 combines all the .debug_info, etc. sections into one,
11434 and the entries in the index tables are now offsets into these sections.
11435 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11436 section.
11437
11438 Index Section Contents:
11439 Header
11440 Hash Table of Signatures dwp_hash_table.hash_table
11441 Parallel Table of Indices dwp_hash_table.unit_table
11442 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11443 Table of Section Sizes dwp_hash_table.v2.sizes
11444
11445 The index section header consists of:
11446
11447 V, 32 bit version number
11448 L, 32 bit number of columns in the table of section offsets
11449 N, 32 bit number of compilation units or type units in the index
11450 M, 32 bit number of slots in the hash table
11451
11452 Numbers are recorded using the byte order of the application binary.
11453
11454 The hash table has the same format as version 1.
11455 The parallel table of indices has the same format as version 1,
11456 except that the entries are origin-1 indices into the table of sections
11457 offsets and the table of section sizes.
11458
11459 The table of offsets begins immediately following the parallel table
11460 (at offset 16 + 12 * M from the beginning of the section). The table is
11461 a two-dimensional array of 32-bit words (using the byte order of the
11462 application binary), with L columns and N+1 rows, in row-major order.
11463 Each row in the array is indexed starting from 0. The first row provides
11464 a key to the remaining rows: each column in this row provides an identifier
11465 for a debug section, and the offsets in the same column of subsequent rows
11466 refer to that section. The section identifiers are:
11467
11468 DW_SECT_INFO 1 .debug_info.dwo
11469 DW_SECT_TYPES 2 .debug_types.dwo
11470 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11471 DW_SECT_LINE 4 .debug_line.dwo
11472 DW_SECT_LOC 5 .debug_loc.dwo
11473 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11474 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11475 DW_SECT_MACRO 8 .debug_macro.dwo
11476
11477 The offsets provided by the CU and TU index sections are the base offsets
11478 for the contributions made by each CU or TU to the corresponding section
11479 in the package file. Each CU and TU header contains an abbrev_offset
11480 field, used to find the abbreviations table for that CU or TU within the
11481 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11482 be interpreted as relative to the base offset given in the index section.
11483 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11484 should be interpreted as relative to the base offset for .debug_line.dwo,
11485 and offsets into other debug sections obtained from DWARF attributes should
11486 also be interpreted as relative to the corresponding base offset.
11487
11488 The table of sizes begins immediately following the table of offsets.
11489 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11490 with L columns and N rows, in row-major order. Each row in the array is
11491 indexed starting from 1 (row 0 is shared by the two tables).
11492
11493 ---
11494
11495 Hash table lookup is handled the same in version 1 and 2:
11496
11497 We assume that N and M will not exceed 2^32 - 1.
11498 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11499
11500 Given a 64-bit compilation unit signature or a type signature S, an entry
11501 in the hash table is located as follows:
11502
11503 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11504 the low-order k bits all set to 1.
11505
11506 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11507
11508 3) If the hash table entry at index H matches the signature, use that
11509 entry. If the hash table entry at index H is unused (all zeroes),
11510 terminate the search: the signature is not present in the table.
11511
11512 4) Let H = (H + H') modulo M. Repeat at Step 3.
11513
11514 Because M > N and H' and M are relatively prime, the search is guaranteed
11515 to stop at an unused slot or find the match. */
11516
11517 /* Create a hash table to map DWO IDs to their CU/TU entry in
11518 .debug_{info,types}.dwo in DWP_FILE.
11519 Returns NULL if there isn't one.
11520 Note: This function processes DWP files only, not DWO files. */
11521
11522 static struct dwp_hash_table *
11523 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11524 struct dwp_file *dwp_file, int is_debug_types)
11525 {
11526 struct objfile *objfile = dwarf2_per_objfile->objfile;
11527 bfd *dbfd = dwp_file->dbfd.get ();
11528 const gdb_byte *index_ptr, *index_end;
11529 struct dwarf2_section_info *index;
11530 uint32_t version, nr_columns, nr_units, nr_slots;
11531 struct dwp_hash_table *htab;
11532
11533 if (is_debug_types)
11534 index = &dwp_file->sections.tu_index;
11535 else
11536 index = &dwp_file->sections.cu_index;
11537
11538 if (index->empty ())
11539 return NULL;
11540 index->read (objfile);
11541
11542 index_ptr = index->buffer;
11543 index_end = index_ptr + index->size;
11544
11545 version = read_4_bytes (dbfd, index_ptr);
11546 index_ptr += 4;
11547 if (version == 2)
11548 nr_columns = read_4_bytes (dbfd, index_ptr);
11549 else
11550 nr_columns = 0;
11551 index_ptr += 4;
11552 nr_units = read_4_bytes (dbfd, index_ptr);
11553 index_ptr += 4;
11554 nr_slots = read_4_bytes (dbfd, index_ptr);
11555 index_ptr += 4;
11556
11557 if (version != 1 && version != 2)
11558 {
11559 error (_("Dwarf Error: unsupported DWP file version (%s)"
11560 " [in module %s]"),
11561 pulongest (version), dwp_file->name);
11562 }
11563 if (nr_slots != (nr_slots & -nr_slots))
11564 {
11565 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11566 " is not power of 2 [in module %s]"),
11567 pulongest (nr_slots), dwp_file->name);
11568 }
11569
11570 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11571 htab->version = version;
11572 htab->nr_columns = nr_columns;
11573 htab->nr_units = nr_units;
11574 htab->nr_slots = nr_slots;
11575 htab->hash_table = index_ptr;
11576 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11577
11578 /* Exit early if the table is empty. */
11579 if (nr_slots == 0 || nr_units == 0
11580 || (version == 2 && nr_columns == 0))
11581 {
11582 /* All must be zero. */
11583 if (nr_slots != 0 || nr_units != 0
11584 || (version == 2 && nr_columns != 0))
11585 {
11586 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11587 " all zero [in modules %s]"),
11588 dwp_file->name);
11589 }
11590 return htab;
11591 }
11592
11593 if (version == 1)
11594 {
11595 htab->section_pool.v1.indices =
11596 htab->unit_table + sizeof (uint32_t) * nr_slots;
11597 /* It's harder to decide whether the section is too small in v1.
11598 V1 is deprecated anyway so we punt. */
11599 }
11600 else
11601 {
11602 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11603 int *ids = htab->section_pool.v2.section_ids;
11604 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11605 /* Reverse map for error checking. */
11606 int ids_seen[DW_SECT_MAX + 1];
11607 int i;
11608
11609 if (nr_columns < 2)
11610 {
11611 error (_("Dwarf Error: bad DWP hash table, too few columns"
11612 " in section table [in module %s]"),
11613 dwp_file->name);
11614 }
11615 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, too many columns"
11618 " in section table [in module %s]"),
11619 dwp_file->name);
11620 }
11621 memset (ids, 255, sizeof_ids);
11622 memset (ids_seen, 255, sizeof (ids_seen));
11623 for (i = 0; i < nr_columns; ++i)
11624 {
11625 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11626
11627 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11628 {
11629 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11630 " in section table [in module %s]"),
11631 id, dwp_file->name);
11632 }
11633 if (ids_seen[id] != -1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11636 " id %d in section table [in module %s]"),
11637 id, dwp_file->name);
11638 }
11639 ids_seen[id] = i;
11640 ids[i] = id;
11641 }
11642 /* Must have exactly one info or types section. */
11643 if (((ids_seen[DW_SECT_INFO] != -1)
11644 + (ids_seen[DW_SECT_TYPES] != -1))
11645 != 1)
11646 {
11647 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11648 " DWO info/types section [in module %s]"),
11649 dwp_file->name);
11650 }
11651 /* Must have an abbrev section. */
11652 if (ids_seen[DW_SECT_ABBREV] == -1)
11653 {
11654 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11655 " section [in module %s]"),
11656 dwp_file->name);
11657 }
11658 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11659 htab->section_pool.v2.sizes =
11660 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11661 * nr_units * nr_columns);
11662 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11663 * nr_units * nr_columns))
11664 > index_end)
11665 {
11666 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11667 " [in module %s]"),
11668 dwp_file->name);
11669 }
11670 }
11671
11672 return htab;
11673 }
11674
11675 /* Update SECTIONS with the data from SECTP.
11676
11677 This function is like the other "locate" section routines that are
11678 passed to bfd_map_over_sections, but in this context the sections to
11679 read comes from the DWP V1 hash table, not the full ELF section table.
11680
11681 The result is non-zero for success, or zero if an error was found. */
11682
11683 static int
11684 locate_v1_virtual_dwo_sections (asection *sectp,
11685 struct virtual_v1_dwo_sections *sections)
11686 {
11687 const struct dwop_section_names *names = &dwop_section_names;
11688
11689 if (section_is_p (sectp->name, &names->abbrev_dwo))
11690 {
11691 /* There can be only one. */
11692 if (sections->abbrev.s.section != NULL)
11693 return 0;
11694 sections->abbrev.s.section = sectp;
11695 sections->abbrev.size = bfd_section_size (sectp);
11696 }
11697 else if (section_is_p (sectp->name, &names->info_dwo)
11698 || section_is_p (sectp->name, &names->types_dwo))
11699 {
11700 /* There can be only one. */
11701 if (sections->info_or_types.s.section != NULL)
11702 return 0;
11703 sections->info_or_types.s.section = sectp;
11704 sections->info_or_types.size = bfd_section_size (sectp);
11705 }
11706 else if (section_is_p (sectp->name, &names->line_dwo))
11707 {
11708 /* There can be only one. */
11709 if (sections->line.s.section != NULL)
11710 return 0;
11711 sections->line.s.section = sectp;
11712 sections->line.size = bfd_section_size (sectp);
11713 }
11714 else if (section_is_p (sectp->name, &names->loc_dwo))
11715 {
11716 /* There can be only one. */
11717 if (sections->loc.s.section != NULL)
11718 return 0;
11719 sections->loc.s.section = sectp;
11720 sections->loc.size = bfd_section_size (sectp);
11721 }
11722 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11723 {
11724 /* There can be only one. */
11725 if (sections->macinfo.s.section != NULL)
11726 return 0;
11727 sections->macinfo.s.section = sectp;
11728 sections->macinfo.size = bfd_section_size (sectp);
11729 }
11730 else if (section_is_p (sectp->name, &names->macro_dwo))
11731 {
11732 /* There can be only one. */
11733 if (sections->macro.s.section != NULL)
11734 return 0;
11735 sections->macro.s.section = sectp;
11736 sections->macro.size = bfd_section_size (sectp);
11737 }
11738 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11739 {
11740 /* There can be only one. */
11741 if (sections->str_offsets.s.section != NULL)
11742 return 0;
11743 sections->str_offsets.s.section = sectp;
11744 sections->str_offsets.size = bfd_section_size (sectp);
11745 }
11746 else
11747 {
11748 /* No other kind of section is valid. */
11749 return 0;
11750 }
11751
11752 return 1;
11753 }
11754
11755 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11756 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11757 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11758 This is for DWP version 1 files. */
11759
11760 static struct dwo_unit *
11761 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11762 struct dwp_file *dwp_file,
11763 uint32_t unit_index,
11764 const char *comp_dir,
11765 ULONGEST signature, int is_debug_types)
11766 {
11767 const struct dwp_hash_table *dwp_htab =
11768 is_debug_types ? dwp_file->tus : dwp_file->cus;
11769 bfd *dbfd = dwp_file->dbfd.get ();
11770 const char *kind = is_debug_types ? "TU" : "CU";
11771 struct dwo_file *dwo_file;
11772 struct dwo_unit *dwo_unit;
11773 struct virtual_v1_dwo_sections sections;
11774 void **dwo_file_slot;
11775 int i;
11776
11777 gdb_assert (dwp_file->version == 1);
11778
11779 if (dwarf_read_debug)
11780 {
11781 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11782 kind,
11783 pulongest (unit_index), hex_string (signature),
11784 dwp_file->name);
11785 }
11786
11787 /* Fetch the sections of this DWO unit.
11788 Put a limit on the number of sections we look for so that bad data
11789 doesn't cause us to loop forever. */
11790
11791 #define MAX_NR_V1_DWO_SECTIONS \
11792 (1 /* .debug_info or .debug_types */ \
11793 + 1 /* .debug_abbrev */ \
11794 + 1 /* .debug_line */ \
11795 + 1 /* .debug_loc */ \
11796 + 1 /* .debug_str_offsets */ \
11797 + 1 /* .debug_macro or .debug_macinfo */ \
11798 + 1 /* trailing zero */)
11799
11800 memset (&sections, 0, sizeof (sections));
11801
11802 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11803 {
11804 asection *sectp;
11805 uint32_t section_nr =
11806 read_4_bytes (dbfd,
11807 dwp_htab->section_pool.v1.indices
11808 + (unit_index + i) * sizeof (uint32_t));
11809
11810 if (section_nr == 0)
11811 break;
11812 if (section_nr >= dwp_file->num_sections)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, section number too large"
11815 " [in module %s]"),
11816 dwp_file->name);
11817 }
11818
11819 sectp = dwp_file->elf_sections[section_nr];
11820 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11821 {
11822 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11823 " [in module %s]"),
11824 dwp_file->name);
11825 }
11826 }
11827
11828 if (i < 2
11829 || sections.info_or_types.empty ()
11830 || sections.abbrev.empty ())
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11833 " [in module %s]"),
11834 dwp_file->name);
11835 }
11836 if (i == MAX_NR_V1_DWO_SECTIONS)
11837 {
11838 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11839 " [in module %s]"),
11840 dwp_file->name);
11841 }
11842
11843 /* It's easier for the rest of the code if we fake a struct dwo_file and
11844 have dwo_unit "live" in that. At least for now.
11845
11846 The DWP file can be made up of a random collection of CUs and TUs.
11847 However, for each CU + set of TUs that came from the same original DWO
11848 file, we can combine them back into a virtual DWO file to save space
11849 (fewer struct dwo_file objects to allocate). Remember that for really
11850 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11851
11852 std::string virtual_dwo_name =
11853 string_printf ("virtual-dwo/%d-%d-%d-%d",
11854 sections.abbrev.get_id (),
11855 sections.line.get_id (),
11856 sections.loc.get_id (),
11857 sections.str_offsets.get_id ());
11858 /* Can we use an existing virtual DWO file? */
11859 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11860 virtual_dwo_name.c_str (),
11861 comp_dir);
11862 /* Create one if necessary. */
11863 if (*dwo_file_slot == NULL)
11864 {
11865 if (dwarf_read_debug)
11866 {
11867 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11868 virtual_dwo_name.c_str ());
11869 }
11870 dwo_file = new struct dwo_file;
11871 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11872 dwo_file->comp_dir = comp_dir;
11873 dwo_file->sections.abbrev = sections.abbrev;
11874 dwo_file->sections.line = sections.line;
11875 dwo_file->sections.loc = sections.loc;
11876 dwo_file->sections.macinfo = sections.macinfo;
11877 dwo_file->sections.macro = sections.macro;
11878 dwo_file->sections.str_offsets = sections.str_offsets;
11879 /* The "str" section is global to the entire DWP file. */
11880 dwo_file->sections.str = dwp_file->sections.str;
11881 /* The info or types section is assigned below to dwo_unit,
11882 there's no need to record it in dwo_file.
11883 Also, we can't simply record type sections in dwo_file because
11884 we record a pointer into the vector in dwo_unit. As we collect more
11885 types we'll grow the vector and eventually have to reallocate space
11886 for it, invalidating all copies of pointers into the previous
11887 contents. */
11888 *dwo_file_slot = dwo_file;
11889 }
11890 else
11891 {
11892 if (dwarf_read_debug)
11893 {
11894 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11895 virtual_dwo_name.c_str ());
11896 }
11897 dwo_file = (struct dwo_file *) *dwo_file_slot;
11898 }
11899
11900 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11901 dwo_unit->dwo_file = dwo_file;
11902 dwo_unit->signature = signature;
11903 dwo_unit->section =
11904 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11905 *dwo_unit->section = sections.info_or_types;
11906 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11907
11908 return dwo_unit;
11909 }
11910
11911 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11912 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11913 piece within that section used by a TU/CU, return a virtual section
11914 of just that piece. */
11915
11916 static struct dwarf2_section_info
11917 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11918 struct dwarf2_section_info *section,
11919 bfd_size_type offset, bfd_size_type size)
11920 {
11921 struct dwarf2_section_info result;
11922 asection *sectp;
11923
11924 gdb_assert (section != NULL);
11925 gdb_assert (!section->is_virtual);
11926
11927 memset (&result, 0, sizeof (result));
11928 result.s.containing_section = section;
11929 result.is_virtual = true;
11930
11931 if (size == 0)
11932 return result;
11933
11934 sectp = section->get_bfd_section ();
11935
11936 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11937 bounds of the real section. This is a pretty-rare event, so just
11938 flag an error (easier) instead of a warning and trying to cope. */
11939 if (sectp == NULL
11940 || offset + size > bfd_section_size (sectp))
11941 {
11942 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11943 " in section %s [in module %s]"),
11944 sectp ? bfd_section_name (sectp) : "<unknown>",
11945 objfile_name (dwarf2_per_objfile->objfile));
11946 }
11947
11948 result.virtual_offset = offset;
11949 result.size = size;
11950 return result;
11951 }
11952
11953 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11954 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11955 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11956 This is for DWP version 2 files. */
11957
11958 static struct dwo_unit *
11959 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11960 struct dwp_file *dwp_file,
11961 uint32_t unit_index,
11962 const char *comp_dir,
11963 ULONGEST signature, int is_debug_types)
11964 {
11965 const struct dwp_hash_table *dwp_htab =
11966 is_debug_types ? dwp_file->tus : dwp_file->cus;
11967 bfd *dbfd = dwp_file->dbfd.get ();
11968 const char *kind = is_debug_types ? "TU" : "CU";
11969 struct dwo_file *dwo_file;
11970 struct dwo_unit *dwo_unit;
11971 struct virtual_v2_dwo_sections sections;
11972 void **dwo_file_slot;
11973 int i;
11974
11975 gdb_assert (dwp_file->version == 2);
11976
11977 if (dwarf_read_debug)
11978 {
11979 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11980 kind,
11981 pulongest (unit_index), hex_string (signature),
11982 dwp_file->name);
11983 }
11984
11985 /* Fetch the section offsets of this DWO unit. */
11986
11987 memset (&sections, 0, sizeof (sections));
11988
11989 for (i = 0; i < dwp_htab->nr_columns; ++i)
11990 {
11991 uint32_t offset = read_4_bytes (dbfd,
11992 dwp_htab->section_pool.v2.offsets
11993 + (((unit_index - 1) * dwp_htab->nr_columns
11994 + i)
11995 * sizeof (uint32_t)));
11996 uint32_t size = read_4_bytes (dbfd,
11997 dwp_htab->section_pool.v2.sizes
11998 + (((unit_index - 1) * dwp_htab->nr_columns
11999 + i)
12000 * sizeof (uint32_t)));
12001
12002 switch (dwp_htab->section_pool.v2.section_ids[i])
12003 {
12004 case DW_SECT_INFO:
12005 case DW_SECT_TYPES:
12006 sections.info_or_types_offset = offset;
12007 sections.info_or_types_size = size;
12008 break;
12009 case DW_SECT_ABBREV:
12010 sections.abbrev_offset = offset;
12011 sections.abbrev_size = size;
12012 break;
12013 case DW_SECT_LINE:
12014 sections.line_offset = offset;
12015 sections.line_size = size;
12016 break;
12017 case DW_SECT_LOC:
12018 sections.loc_offset = offset;
12019 sections.loc_size = size;
12020 break;
12021 case DW_SECT_STR_OFFSETS:
12022 sections.str_offsets_offset = offset;
12023 sections.str_offsets_size = size;
12024 break;
12025 case DW_SECT_MACINFO:
12026 sections.macinfo_offset = offset;
12027 sections.macinfo_size = size;
12028 break;
12029 case DW_SECT_MACRO:
12030 sections.macro_offset = offset;
12031 sections.macro_size = size;
12032 break;
12033 }
12034 }
12035
12036 /* It's easier for the rest of the code if we fake a struct dwo_file and
12037 have dwo_unit "live" in that. At least for now.
12038
12039 The DWP file can be made up of a random collection of CUs and TUs.
12040 However, for each CU + set of TUs that came from the same original DWO
12041 file, we can combine them back into a virtual DWO file to save space
12042 (fewer struct dwo_file objects to allocate). Remember that for really
12043 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12044
12045 std::string virtual_dwo_name =
12046 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12047 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12048 (long) (sections.line_size ? sections.line_offset : 0),
12049 (long) (sections.loc_size ? sections.loc_offset : 0),
12050 (long) (sections.str_offsets_size
12051 ? sections.str_offsets_offset : 0));
12052 /* Can we use an existing virtual DWO file? */
12053 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12054 virtual_dwo_name.c_str (),
12055 comp_dir);
12056 /* Create one if necessary. */
12057 if (*dwo_file_slot == NULL)
12058 {
12059 if (dwarf_read_debug)
12060 {
12061 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12062 virtual_dwo_name.c_str ());
12063 }
12064 dwo_file = new struct dwo_file;
12065 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12066 dwo_file->comp_dir = comp_dir;
12067 dwo_file->sections.abbrev =
12068 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12069 sections.abbrev_offset, sections.abbrev_size);
12070 dwo_file->sections.line =
12071 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12072 sections.line_offset, sections.line_size);
12073 dwo_file->sections.loc =
12074 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12075 sections.loc_offset, sections.loc_size);
12076 dwo_file->sections.macinfo =
12077 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12078 sections.macinfo_offset, sections.macinfo_size);
12079 dwo_file->sections.macro =
12080 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12081 sections.macro_offset, sections.macro_size);
12082 dwo_file->sections.str_offsets =
12083 create_dwp_v2_section (dwarf2_per_objfile,
12084 &dwp_file->sections.str_offsets,
12085 sections.str_offsets_offset,
12086 sections.str_offsets_size);
12087 /* The "str" section is global to the entire DWP file. */
12088 dwo_file->sections.str = dwp_file->sections.str;
12089 /* The info or types section is assigned below to dwo_unit,
12090 there's no need to record it in dwo_file.
12091 Also, we can't simply record type sections in dwo_file because
12092 we record a pointer into the vector in dwo_unit. As we collect more
12093 types we'll grow the vector and eventually have to reallocate space
12094 for it, invalidating all copies of pointers into the previous
12095 contents. */
12096 *dwo_file_slot = dwo_file;
12097 }
12098 else
12099 {
12100 if (dwarf_read_debug)
12101 {
12102 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12103 virtual_dwo_name.c_str ());
12104 }
12105 dwo_file = (struct dwo_file *) *dwo_file_slot;
12106 }
12107
12108 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12109 dwo_unit->dwo_file = dwo_file;
12110 dwo_unit->signature = signature;
12111 dwo_unit->section =
12112 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12113 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12114 is_debug_types
12115 ? &dwp_file->sections.types
12116 : &dwp_file->sections.info,
12117 sections.info_or_types_offset,
12118 sections.info_or_types_size);
12119 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12120
12121 return dwo_unit;
12122 }
12123
12124 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12125 Returns NULL if the signature isn't found. */
12126
12127 static struct dwo_unit *
12128 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12129 struct dwp_file *dwp_file, const char *comp_dir,
12130 ULONGEST signature, int is_debug_types)
12131 {
12132 const struct dwp_hash_table *dwp_htab =
12133 is_debug_types ? dwp_file->tus : dwp_file->cus;
12134 bfd *dbfd = dwp_file->dbfd.get ();
12135 uint32_t mask = dwp_htab->nr_slots - 1;
12136 uint32_t hash = signature & mask;
12137 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12138 unsigned int i;
12139 void **slot;
12140 struct dwo_unit find_dwo_cu;
12141
12142 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12143 find_dwo_cu.signature = signature;
12144 slot = htab_find_slot (is_debug_types
12145 ? dwp_file->loaded_tus.get ()
12146 : dwp_file->loaded_cus.get (),
12147 &find_dwo_cu, INSERT);
12148
12149 if (*slot != NULL)
12150 return (struct dwo_unit *) *slot;
12151
12152 /* Use a for loop so that we don't loop forever on bad debug info. */
12153 for (i = 0; i < dwp_htab->nr_slots; ++i)
12154 {
12155 ULONGEST signature_in_table;
12156
12157 signature_in_table =
12158 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12159 if (signature_in_table == signature)
12160 {
12161 uint32_t unit_index =
12162 read_4_bytes (dbfd,
12163 dwp_htab->unit_table + hash * sizeof (uint32_t));
12164
12165 if (dwp_file->version == 1)
12166 {
12167 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12168 dwp_file, unit_index,
12169 comp_dir, signature,
12170 is_debug_types);
12171 }
12172 else
12173 {
12174 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12175 dwp_file, unit_index,
12176 comp_dir, signature,
12177 is_debug_types);
12178 }
12179 return (struct dwo_unit *) *slot;
12180 }
12181 if (signature_in_table == 0)
12182 return NULL;
12183 hash = (hash + hash2) & mask;
12184 }
12185
12186 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12187 " [in module %s]"),
12188 dwp_file->name);
12189 }
12190
12191 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12192 Open the file specified by FILE_NAME and hand it off to BFD for
12193 preliminary analysis. Return a newly initialized bfd *, which
12194 includes a canonicalized copy of FILE_NAME.
12195 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12196 SEARCH_CWD is true if the current directory is to be searched.
12197 It will be searched before debug-file-directory.
12198 If successful, the file is added to the bfd include table of the
12199 objfile's bfd (see gdb_bfd_record_inclusion).
12200 If unable to find/open the file, return NULL.
12201 NOTE: This function is derived from symfile_bfd_open. */
12202
12203 static gdb_bfd_ref_ptr
12204 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 const char *file_name, int is_dwp, int search_cwd)
12206 {
12207 int desc;
12208 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12209 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12210 to debug_file_directory. */
12211 const char *search_path;
12212 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12213
12214 gdb::unique_xmalloc_ptr<char> search_path_holder;
12215 if (search_cwd)
12216 {
12217 if (*debug_file_directory != '\0')
12218 {
12219 search_path_holder.reset (concat (".", dirname_separator_string,
12220 debug_file_directory,
12221 (char *) NULL));
12222 search_path = search_path_holder.get ();
12223 }
12224 else
12225 search_path = ".";
12226 }
12227 else
12228 search_path = debug_file_directory;
12229
12230 openp_flags flags = OPF_RETURN_REALPATH;
12231 if (is_dwp)
12232 flags |= OPF_SEARCH_IN_PATH;
12233
12234 gdb::unique_xmalloc_ptr<char> absolute_name;
12235 desc = openp (search_path, flags, file_name,
12236 O_RDONLY | O_BINARY, &absolute_name);
12237 if (desc < 0)
12238 return NULL;
12239
12240 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12241 gnutarget, desc));
12242 if (sym_bfd == NULL)
12243 return NULL;
12244 bfd_set_cacheable (sym_bfd.get (), 1);
12245
12246 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12247 return NULL;
12248
12249 /* Success. Record the bfd as having been included by the objfile's bfd.
12250 This is important because things like demangled_names_hash lives in the
12251 objfile's per_bfd space and may have references to things like symbol
12252 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12253 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12254
12255 return sym_bfd;
12256 }
12257
12258 /* Try to open DWO file FILE_NAME.
12259 COMP_DIR is the DW_AT_comp_dir attribute.
12260 The result is the bfd handle of the file.
12261 If there is a problem finding or opening the file, return NULL.
12262 Upon success, the canonicalized path of the file is stored in the bfd,
12263 same as symfile_bfd_open. */
12264
12265 static gdb_bfd_ref_ptr
12266 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12267 const char *file_name, const char *comp_dir)
12268 {
12269 if (IS_ABSOLUTE_PATH (file_name))
12270 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12271 0 /*is_dwp*/, 0 /*search_cwd*/);
12272
12273 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12274
12275 if (comp_dir != NULL)
12276 {
12277 gdb::unique_xmalloc_ptr<char> path_to_try
12278 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12279
12280 /* NOTE: If comp_dir is a relative path, this will also try the
12281 search path, which seems useful. */
12282 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12283 path_to_try.get (),
12284 0 /*is_dwp*/,
12285 1 /*search_cwd*/));
12286 if (abfd != NULL)
12287 return abfd;
12288 }
12289
12290 /* That didn't work, try debug-file-directory, which, despite its name,
12291 is a list of paths. */
12292
12293 if (*debug_file_directory == '\0')
12294 return NULL;
12295
12296 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12297 0 /*is_dwp*/, 1 /*search_cwd*/);
12298 }
12299
12300 /* This function is mapped across the sections and remembers the offset and
12301 size of each of the DWO debugging sections we are interested in. */
12302
12303 static void
12304 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12305 {
12306 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12307 const struct dwop_section_names *names = &dwop_section_names;
12308
12309 if (section_is_p (sectp->name, &names->abbrev_dwo))
12310 {
12311 dwo_sections->abbrev.s.section = sectp;
12312 dwo_sections->abbrev.size = bfd_section_size (sectp);
12313 }
12314 else if (section_is_p (sectp->name, &names->info_dwo))
12315 {
12316 dwo_sections->info.s.section = sectp;
12317 dwo_sections->info.size = bfd_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->line_dwo))
12320 {
12321 dwo_sections->line.s.section = sectp;
12322 dwo_sections->line.size = bfd_section_size (sectp);
12323 }
12324 else if (section_is_p (sectp->name, &names->loc_dwo))
12325 {
12326 dwo_sections->loc.s.section = sectp;
12327 dwo_sections->loc.size = bfd_section_size (sectp);
12328 }
12329 else if (section_is_p (sectp->name, &names->loclists_dwo))
12330 {
12331 dwo_sections->loclists.s.section = sectp;
12332 dwo_sections->loclists.size = bfd_section_size (sectp);
12333 }
12334 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12335 {
12336 dwo_sections->macinfo.s.section = sectp;
12337 dwo_sections->macinfo.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->macro_dwo))
12340 {
12341 dwo_sections->macro.s.section = sectp;
12342 dwo_sections->macro.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->str_dwo))
12345 {
12346 dwo_sections->str.s.section = sectp;
12347 dwo_sections->str.size = bfd_section_size (sectp);
12348 }
12349 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12350 {
12351 dwo_sections->str_offsets.s.section = sectp;
12352 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->types_dwo))
12355 {
12356 struct dwarf2_section_info type_section;
12357
12358 memset (&type_section, 0, sizeof (type_section));
12359 type_section.s.section = sectp;
12360 type_section.size = bfd_section_size (sectp);
12361 dwo_sections->types.push_back (type_section);
12362 }
12363 }
12364
12365 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12366 by PER_CU. This is for the non-DWP case.
12367 The result is NULL if DWO_NAME can't be found. */
12368
12369 static struct dwo_file *
12370 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12371 const char *comp_dir)
12372 {
12373 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12374
12375 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12376 if (dbfd == NULL)
12377 {
12378 if (dwarf_read_debug)
12379 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12380 return NULL;
12381 }
12382
12383 dwo_file_up dwo_file (new struct dwo_file);
12384 dwo_file->dwo_name = dwo_name;
12385 dwo_file->comp_dir = comp_dir;
12386 dwo_file->dbfd = std::move (dbfd);
12387
12388 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12389 &dwo_file->sections);
12390
12391 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12392 dwo_file->sections.info, dwo_file->cus);
12393
12394 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12395 dwo_file->sections.types, dwo_file->tus);
12396
12397 if (dwarf_read_debug)
12398 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12399
12400 return dwo_file.release ();
12401 }
12402
12403 /* This function is mapped across the sections and remembers the offset and
12404 size of each of the DWP debugging sections common to version 1 and 2 that
12405 we are interested in. */
12406
12407 static void
12408 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12409 void *dwp_file_ptr)
12410 {
12411 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12412 const struct dwop_section_names *names = &dwop_section_names;
12413 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12414
12415 /* Record the ELF section number for later lookup: this is what the
12416 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12417 gdb_assert (elf_section_nr < dwp_file->num_sections);
12418 dwp_file->elf_sections[elf_section_nr] = sectp;
12419
12420 /* Look for specific sections that we need. */
12421 if (section_is_p (sectp->name, &names->str_dwo))
12422 {
12423 dwp_file->sections.str.s.section = sectp;
12424 dwp_file->sections.str.size = bfd_section_size (sectp);
12425 }
12426 else if (section_is_p (sectp->name, &names->cu_index))
12427 {
12428 dwp_file->sections.cu_index.s.section = sectp;
12429 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12430 }
12431 else if (section_is_p (sectp->name, &names->tu_index))
12432 {
12433 dwp_file->sections.tu_index.s.section = sectp;
12434 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12435 }
12436 }
12437
12438 /* This function is mapped across the sections and remembers the offset and
12439 size of each of the DWP version 2 debugging sections that we are interested
12440 in. This is split into a separate function because we don't know if we
12441 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12442
12443 static void
12444 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12445 {
12446 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12447 const struct dwop_section_names *names = &dwop_section_names;
12448 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12449
12450 /* Record the ELF section number for later lookup: this is what the
12451 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12452 gdb_assert (elf_section_nr < dwp_file->num_sections);
12453 dwp_file->elf_sections[elf_section_nr] = sectp;
12454
12455 /* Look for specific sections that we need. */
12456 if (section_is_p (sectp->name, &names->abbrev_dwo))
12457 {
12458 dwp_file->sections.abbrev.s.section = sectp;
12459 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12460 }
12461 else if (section_is_p (sectp->name, &names->info_dwo))
12462 {
12463 dwp_file->sections.info.s.section = sectp;
12464 dwp_file->sections.info.size = bfd_section_size (sectp);
12465 }
12466 else if (section_is_p (sectp->name, &names->line_dwo))
12467 {
12468 dwp_file->sections.line.s.section = sectp;
12469 dwp_file->sections.line.size = bfd_section_size (sectp);
12470 }
12471 else if (section_is_p (sectp->name, &names->loc_dwo))
12472 {
12473 dwp_file->sections.loc.s.section = sectp;
12474 dwp_file->sections.loc.size = bfd_section_size (sectp);
12475 }
12476 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12477 {
12478 dwp_file->sections.macinfo.s.section = sectp;
12479 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12480 }
12481 else if (section_is_p (sectp->name, &names->macro_dwo))
12482 {
12483 dwp_file->sections.macro.s.section = sectp;
12484 dwp_file->sections.macro.size = bfd_section_size (sectp);
12485 }
12486 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12487 {
12488 dwp_file->sections.str_offsets.s.section = sectp;
12489 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12490 }
12491 else if (section_is_p (sectp->name, &names->types_dwo))
12492 {
12493 dwp_file->sections.types.s.section = sectp;
12494 dwp_file->sections.types.size = bfd_section_size (sectp);
12495 }
12496 }
12497
12498 /* Hash function for dwp_file loaded CUs/TUs. */
12499
12500 static hashval_t
12501 hash_dwp_loaded_cutus (const void *item)
12502 {
12503 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12504
12505 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12506 return dwo_unit->signature;
12507 }
12508
12509 /* Equality function for dwp_file loaded CUs/TUs. */
12510
12511 static int
12512 eq_dwp_loaded_cutus (const void *a, const void *b)
12513 {
12514 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12515 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12516
12517 return dua->signature == dub->signature;
12518 }
12519
12520 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12521
12522 static htab_up
12523 allocate_dwp_loaded_cutus_table ()
12524 {
12525 return htab_up (htab_create_alloc (3,
12526 hash_dwp_loaded_cutus,
12527 eq_dwp_loaded_cutus,
12528 NULL, xcalloc, xfree));
12529 }
12530
12531 /* Try to open DWP file FILE_NAME.
12532 The result is the bfd handle of the file.
12533 If there is a problem finding or opening the file, return NULL.
12534 Upon success, the canonicalized path of the file is stored in the bfd,
12535 same as symfile_bfd_open. */
12536
12537 static gdb_bfd_ref_ptr
12538 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12539 const char *file_name)
12540 {
12541 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12542 1 /*is_dwp*/,
12543 1 /*search_cwd*/));
12544 if (abfd != NULL)
12545 return abfd;
12546
12547 /* Work around upstream bug 15652.
12548 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12549 [Whether that's a "bug" is debatable, but it is getting in our way.]
12550 We have no real idea where the dwp file is, because gdb's realpath-ing
12551 of the executable's path may have discarded the needed info.
12552 [IWBN if the dwp file name was recorded in the executable, akin to
12553 .gnu_debuglink, but that doesn't exist yet.]
12554 Strip the directory from FILE_NAME and search again. */
12555 if (*debug_file_directory != '\0')
12556 {
12557 /* Don't implicitly search the current directory here.
12558 If the user wants to search "." to handle this case,
12559 it must be added to debug-file-directory. */
12560 return try_open_dwop_file (dwarf2_per_objfile,
12561 lbasename (file_name), 1 /*is_dwp*/,
12562 0 /*search_cwd*/);
12563 }
12564
12565 return NULL;
12566 }
12567
12568 /* Initialize the use of the DWP file for the current objfile.
12569 By convention the name of the DWP file is ${objfile}.dwp.
12570 The result is NULL if it can't be found. */
12571
12572 static std::unique_ptr<struct dwp_file>
12573 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12574 {
12575 struct objfile *objfile = dwarf2_per_objfile->objfile;
12576
12577 /* Try to find first .dwp for the binary file before any symbolic links
12578 resolving. */
12579
12580 /* If the objfile is a debug file, find the name of the real binary
12581 file and get the name of dwp file from there. */
12582 std::string dwp_name;
12583 if (objfile->separate_debug_objfile_backlink != NULL)
12584 {
12585 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12586 const char *backlink_basename = lbasename (backlink->original_name);
12587
12588 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12589 }
12590 else
12591 dwp_name = objfile->original_name;
12592
12593 dwp_name += ".dwp";
12594
12595 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12596 if (dbfd == NULL
12597 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12598 {
12599 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12600 dwp_name = objfile_name (objfile);
12601 dwp_name += ".dwp";
12602 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12603 }
12604
12605 if (dbfd == NULL)
12606 {
12607 if (dwarf_read_debug)
12608 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12609 return std::unique_ptr<dwp_file> ();
12610 }
12611
12612 const char *name = bfd_get_filename (dbfd.get ());
12613 std::unique_ptr<struct dwp_file> dwp_file
12614 (new struct dwp_file (name, std::move (dbfd)));
12615
12616 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12617 dwp_file->elf_sections =
12618 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12619 dwp_file->num_sections, asection *);
12620
12621 bfd_map_over_sections (dwp_file->dbfd.get (),
12622 dwarf2_locate_common_dwp_sections,
12623 dwp_file.get ());
12624
12625 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12626 0);
12627
12628 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12629 1);
12630
12631 /* The DWP file version is stored in the hash table. Oh well. */
12632 if (dwp_file->cus && dwp_file->tus
12633 && dwp_file->cus->version != dwp_file->tus->version)
12634 {
12635 /* Technically speaking, we should try to limp along, but this is
12636 pretty bizarre. We use pulongest here because that's the established
12637 portability solution (e.g, we cannot use %u for uint32_t). */
12638 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12639 " TU version %s [in DWP file %s]"),
12640 pulongest (dwp_file->cus->version),
12641 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12642 }
12643
12644 if (dwp_file->cus)
12645 dwp_file->version = dwp_file->cus->version;
12646 else if (dwp_file->tus)
12647 dwp_file->version = dwp_file->tus->version;
12648 else
12649 dwp_file->version = 2;
12650
12651 if (dwp_file->version == 2)
12652 bfd_map_over_sections (dwp_file->dbfd.get (),
12653 dwarf2_locate_v2_dwp_sections,
12654 dwp_file.get ());
12655
12656 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12657 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12658
12659 if (dwarf_read_debug)
12660 {
12661 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12662 fprintf_unfiltered (gdb_stdlog,
12663 " %s CUs, %s TUs\n",
12664 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12665 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12666 }
12667
12668 return dwp_file;
12669 }
12670
12671 /* Wrapper around open_and_init_dwp_file, only open it once. */
12672
12673 static struct dwp_file *
12674 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12675 {
12676 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12677 {
12678 dwarf2_per_objfile->per_bfd->dwp_file
12679 = open_and_init_dwp_file (dwarf2_per_objfile);
12680 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12681 }
12682 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12683 }
12684
12685 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12686 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12687 or in the DWP file for the objfile, referenced by THIS_UNIT.
12688 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12689 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12690
12691 This is called, for example, when wanting to read a variable with a
12692 complex location. Therefore we don't want to do file i/o for every call.
12693 Therefore we don't want to look for a DWO file on every call.
12694 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12695 then we check if we've already seen DWO_NAME, and only THEN do we check
12696 for a DWO file.
12697
12698 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12699 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12700
12701 static struct dwo_unit *
12702 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12703 ULONGEST signature, int is_debug_types)
12704 {
12705 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12706 struct objfile *objfile = dwarf2_per_objfile->objfile;
12707 const char *kind = is_debug_types ? "TU" : "CU";
12708 void **dwo_file_slot;
12709 struct dwo_file *dwo_file;
12710 struct dwp_file *dwp_file;
12711
12712 /* First see if there's a DWP file.
12713 If we have a DWP file but didn't find the DWO inside it, don't
12714 look for the original DWO file. It makes gdb behave differently
12715 depending on whether one is debugging in the build tree. */
12716
12717 dwp_file = get_dwp_file (dwarf2_per_objfile);
12718 if (dwp_file != NULL)
12719 {
12720 const struct dwp_hash_table *dwp_htab =
12721 is_debug_types ? dwp_file->tus : dwp_file->cus;
12722
12723 if (dwp_htab != NULL)
12724 {
12725 struct dwo_unit *dwo_cutu =
12726 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12727 signature, is_debug_types);
12728
12729 if (dwo_cutu != NULL)
12730 {
12731 if (dwarf_read_debug)
12732 {
12733 fprintf_unfiltered (gdb_stdlog,
12734 "Virtual DWO %s %s found: @%s\n",
12735 kind, hex_string (signature),
12736 host_address_to_string (dwo_cutu));
12737 }
12738 return dwo_cutu;
12739 }
12740 }
12741 }
12742 else
12743 {
12744 /* No DWP file, look for the DWO file. */
12745
12746 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12747 dwo_name, comp_dir);
12748 if (*dwo_file_slot == NULL)
12749 {
12750 /* Read in the file and build a table of the CUs/TUs it contains. */
12751 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12752 }
12753 /* NOTE: This will be NULL if unable to open the file. */
12754 dwo_file = (struct dwo_file *) *dwo_file_slot;
12755
12756 if (dwo_file != NULL)
12757 {
12758 struct dwo_unit *dwo_cutu = NULL;
12759
12760 if (is_debug_types && dwo_file->tus)
12761 {
12762 struct dwo_unit find_dwo_cutu;
12763
12764 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12765 find_dwo_cutu.signature = signature;
12766 dwo_cutu
12767 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12768 &find_dwo_cutu);
12769 }
12770 else if (!is_debug_types && dwo_file->cus)
12771 {
12772 struct dwo_unit find_dwo_cutu;
12773
12774 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12775 find_dwo_cutu.signature = signature;
12776 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12777 &find_dwo_cutu);
12778 }
12779
12780 if (dwo_cutu != NULL)
12781 {
12782 if (dwarf_read_debug)
12783 {
12784 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12785 kind, dwo_name, hex_string (signature),
12786 host_address_to_string (dwo_cutu));
12787 }
12788 return dwo_cutu;
12789 }
12790 }
12791 }
12792
12793 /* We didn't find it. This could mean a dwo_id mismatch, or
12794 someone deleted the DWO/DWP file, or the search path isn't set up
12795 correctly to find the file. */
12796
12797 if (dwarf_read_debug)
12798 {
12799 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12800 kind, dwo_name, hex_string (signature));
12801 }
12802
12803 /* This is a warning and not a complaint because it can be caused by
12804 pilot error (e.g., user accidentally deleting the DWO). */
12805 {
12806 /* Print the name of the DWP file if we looked there, helps the user
12807 better diagnose the problem. */
12808 std::string dwp_text;
12809
12810 if (dwp_file != NULL)
12811 dwp_text = string_printf (" [in DWP file %s]",
12812 lbasename (dwp_file->name));
12813
12814 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12815 " [in module %s]"),
12816 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12817 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12818 }
12819 return NULL;
12820 }
12821
12822 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12823 See lookup_dwo_cutu_unit for details. */
12824
12825 static struct dwo_unit *
12826 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12827 ULONGEST signature)
12828 {
12829 gdb_assert (!cu->per_cu->is_debug_types);
12830
12831 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12832 }
12833
12834 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12835 See lookup_dwo_cutu_unit for details. */
12836
12837 static struct dwo_unit *
12838 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12839 {
12840 gdb_assert (cu->per_cu->is_debug_types);
12841
12842 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12843
12844 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12845 }
12846
12847 /* Traversal function for queue_and_load_all_dwo_tus. */
12848
12849 static int
12850 queue_and_load_dwo_tu (void **slot, void *info)
12851 {
12852 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12853 dwarf2_cu *cu = (dwarf2_cu *) info;
12854 ULONGEST signature = dwo_unit->signature;
12855 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12856
12857 if (sig_type != NULL)
12858 {
12859 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12860
12861 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12862 a real dependency of PER_CU on SIG_TYPE. That is detected later
12863 while processing PER_CU. */
12864 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12865 load_full_type_unit (sig_cu, cu->per_objfile);
12866 cu->per_cu->imported_symtabs_push (sig_cu);
12867 }
12868
12869 return 1;
12870 }
12871
12872 /* Queue all TUs contained in the DWO of CU to be read in.
12873 The DWO may have the only definition of the type, though it may not be
12874 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12875 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12876
12877 static void
12878 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12879 {
12880 struct dwo_unit *dwo_unit;
12881 struct dwo_file *dwo_file;
12882
12883 gdb_assert (cu != nullptr);
12884 gdb_assert (!cu->per_cu->is_debug_types);
12885 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12886
12887 dwo_unit = cu->dwo_unit;
12888 gdb_assert (dwo_unit != NULL);
12889
12890 dwo_file = dwo_unit->dwo_file;
12891 if (dwo_file->tus != NULL)
12892 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12893 }
12894
12895 /* Read in various DIEs. */
12896
12897 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12898 Inherit only the children of the DW_AT_abstract_origin DIE not being
12899 already referenced by DW_AT_abstract_origin from the children of the
12900 current DIE. */
12901
12902 static void
12903 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12904 {
12905 struct die_info *child_die;
12906 sect_offset *offsetp;
12907 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12908 struct die_info *origin_die;
12909 /* Iterator of the ORIGIN_DIE children. */
12910 struct die_info *origin_child_die;
12911 struct attribute *attr;
12912 struct dwarf2_cu *origin_cu;
12913 struct pending **origin_previous_list_in_scope;
12914
12915 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12916 if (!attr)
12917 return;
12918
12919 /* Note that following die references may follow to a die in a
12920 different cu. */
12921
12922 origin_cu = cu;
12923 origin_die = follow_die_ref (die, attr, &origin_cu);
12924
12925 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12926 symbols in. */
12927 origin_previous_list_in_scope = origin_cu->list_in_scope;
12928 origin_cu->list_in_scope = cu->list_in_scope;
12929
12930 if (die->tag != origin_die->tag
12931 && !(die->tag == DW_TAG_inlined_subroutine
12932 && origin_die->tag == DW_TAG_subprogram))
12933 complaint (_("DIE %s and its abstract origin %s have different tags"),
12934 sect_offset_str (die->sect_off),
12935 sect_offset_str (origin_die->sect_off));
12936
12937 std::vector<sect_offset> offsets;
12938
12939 for (child_die = die->child;
12940 child_die && child_die->tag;
12941 child_die = child_die->sibling)
12942 {
12943 struct die_info *child_origin_die;
12944 struct dwarf2_cu *child_origin_cu;
12945
12946 /* We are trying to process concrete instance entries:
12947 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12948 it's not relevant to our analysis here. i.e. detecting DIEs that are
12949 present in the abstract instance but not referenced in the concrete
12950 one. */
12951 if (child_die->tag == DW_TAG_call_site
12952 || child_die->tag == DW_TAG_GNU_call_site)
12953 continue;
12954
12955 /* For each CHILD_DIE, find the corresponding child of
12956 ORIGIN_DIE. If there is more than one layer of
12957 DW_AT_abstract_origin, follow them all; there shouldn't be,
12958 but GCC versions at least through 4.4 generate this (GCC PR
12959 40573). */
12960 child_origin_die = child_die;
12961 child_origin_cu = cu;
12962 while (1)
12963 {
12964 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12965 child_origin_cu);
12966 if (attr == NULL)
12967 break;
12968 child_origin_die = follow_die_ref (child_origin_die, attr,
12969 &child_origin_cu);
12970 }
12971
12972 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12973 counterpart may exist. */
12974 if (child_origin_die != child_die)
12975 {
12976 if (child_die->tag != child_origin_die->tag
12977 && !(child_die->tag == DW_TAG_inlined_subroutine
12978 && child_origin_die->tag == DW_TAG_subprogram))
12979 complaint (_("Child DIE %s and its abstract origin %s have "
12980 "different tags"),
12981 sect_offset_str (child_die->sect_off),
12982 sect_offset_str (child_origin_die->sect_off));
12983 if (child_origin_die->parent != origin_die)
12984 complaint (_("Child DIE %s and its abstract origin %s have "
12985 "different parents"),
12986 sect_offset_str (child_die->sect_off),
12987 sect_offset_str (child_origin_die->sect_off));
12988 else
12989 offsets.push_back (child_origin_die->sect_off);
12990 }
12991 }
12992 std::sort (offsets.begin (), offsets.end ());
12993 sect_offset *offsets_end = offsets.data () + offsets.size ();
12994 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12995 if (offsetp[-1] == *offsetp)
12996 complaint (_("Multiple children of DIE %s refer "
12997 "to DIE %s as their abstract origin"),
12998 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12999
13000 offsetp = offsets.data ();
13001 origin_child_die = origin_die->child;
13002 while (origin_child_die && origin_child_die->tag)
13003 {
13004 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13005 while (offsetp < offsets_end
13006 && *offsetp < origin_child_die->sect_off)
13007 offsetp++;
13008 if (offsetp >= offsets_end
13009 || *offsetp > origin_child_die->sect_off)
13010 {
13011 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13012 Check whether we're already processing ORIGIN_CHILD_DIE.
13013 This can happen with mutually referenced abstract_origins.
13014 PR 16581. */
13015 if (!origin_child_die->in_process)
13016 process_die (origin_child_die, origin_cu);
13017 }
13018 origin_child_die = origin_child_die->sibling;
13019 }
13020 origin_cu->list_in_scope = origin_previous_list_in_scope;
13021
13022 if (cu != origin_cu)
13023 compute_delayed_physnames (origin_cu);
13024 }
13025
13026 static void
13027 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13028 {
13029 struct objfile *objfile = cu->per_objfile->objfile;
13030 struct gdbarch *gdbarch = objfile->arch ();
13031 struct context_stack *newobj;
13032 CORE_ADDR lowpc;
13033 CORE_ADDR highpc;
13034 struct die_info *child_die;
13035 struct attribute *attr, *call_line, *call_file;
13036 const char *name;
13037 CORE_ADDR baseaddr;
13038 struct block *block;
13039 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13040 std::vector<struct symbol *> template_args;
13041 struct template_symbol *templ_func = NULL;
13042
13043 if (inlined_func)
13044 {
13045 /* If we do not have call site information, we can't show the
13046 caller of this inlined function. That's too confusing, so
13047 only use the scope for local variables. */
13048 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13049 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13050 if (call_line == NULL || call_file == NULL)
13051 {
13052 read_lexical_block_scope (die, cu);
13053 return;
13054 }
13055 }
13056
13057 baseaddr = objfile->text_section_offset ();
13058
13059 name = dwarf2_name (die, cu);
13060
13061 /* Ignore functions with missing or empty names. These are actually
13062 illegal according to the DWARF standard. */
13063 if (name == NULL)
13064 {
13065 complaint (_("missing name for subprogram DIE at %s"),
13066 sect_offset_str (die->sect_off));
13067 return;
13068 }
13069
13070 /* Ignore functions with missing or invalid low and high pc attributes. */
13071 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13072 <= PC_BOUNDS_INVALID)
13073 {
13074 attr = dwarf2_attr (die, DW_AT_external, cu);
13075 if (!attr || !DW_UNSND (attr))
13076 complaint (_("cannot get low and high bounds "
13077 "for subprogram DIE at %s"),
13078 sect_offset_str (die->sect_off));
13079 return;
13080 }
13081
13082 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13083 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13084
13085 /* If we have any template arguments, then we must allocate a
13086 different sort of symbol. */
13087 for (child_die = die->child; child_die; child_die = child_die->sibling)
13088 {
13089 if (child_die->tag == DW_TAG_template_type_param
13090 || child_die->tag == DW_TAG_template_value_param)
13091 {
13092 templ_func = new (&objfile->objfile_obstack) template_symbol;
13093 templ_func->subclass = SYMBOL_TEMPLATE;
13094 break;
13095 }
13096 }
13097
13098 newobj = cu->get_builder ()->push_context (0, lowpc);
13099 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13100 (struct symbol *) templ_func);
13101
13102 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13103 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13104 cu->language);
13105
13106 /* If there is a location expression for DW_AT_frame_base, record
13107 it. */
13108 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13109 if (attr != nullptr)
13110 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13111
13112 /* If there is a location for the static link, record it. */
13113 newobj->static_link = NULL;
13114 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13115 if (attr != nullptr)
13116 {
13117 newobj->static_link
13118 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13119 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13120 cu->addr_type ());
13121 }
13122
13123 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13124
13125 if (die->child != NULL)
13126 {
13127 child_die = die->child;
13128 while (child_die && child_die->tag)
13129 {
13130 if (child_die->tag == DW_TAG_template_type_param
13131 || child_die->tag == DW_TAG_template_value_param)
13132 {
13133 struct symbol *arg = new_symbol (child_die, NULL, cu);
13134
13135 if (arg != NULL)
13136 template_args.push_back (arg);
13137 }
13138 else
13139 process_die (child_die, cu);
13140 child_die = child_die->sibling;
13141 }
13142 }
13143
13144 inherit_abstract_dies (die, cu);
13145
13146 /* If we have a DW_AT_specification, we might need to import using
13147 directives from the context of the specification DIE. See the
13148 comment in determine_prefix. */
13149 if (cu->language == language_cplus
13150 && dwarf2_attr (die, DW_AT_specification, cu))
13151 {
13152 struct dwarf2_cu *spec_cu = cu;
13153 struct die_info *spec_die = die_specification (die, &spec_cu);
13154
13155 while (spec_die)
13156 {
13157 child_die = spec_die->child;
13158 while (child_die && child_die->tag)
13159 {
13160 if (child_die->tag == DW_TAG_imported_module)
13161 process_die (child_die, spec_cu);
13162 child_die = child_die->sibling;
13163 }
13164
13165 /* In some cases, GCC generates specification DIEs that
13166 themselves contain DW_AT_specification attributes. */
13167 spec_die = die_specification (spec_die, &spec_cu);
13168 }
13169 }
13170
13171 struct context_stack cstk = cu->get_builder ()->pop_context ();
13172 /* Make a block for the local symbols within. */
13173 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13174 cstk.static_link, lowpc, highpc);
13175
13176 /* For C++, set the block's scope. */
13177 if ((cu->language == language_cplus
13178 || cu->language == language_fortran
13179 || cu->language == language_d
13180 || cu->language == language_rust)
13181 && cu->processing_has_namespace_info)
13182 block_set_scope (block, determine_prefix (die, cu),
13183 &objfile->objfile_obstack);
13184
13185 /* If we have address ranges, record them. */
13186 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13187
13188 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13189
13190 /* Attach template arguments to function. */
13191 if (!template_args.empty ())
13192 {
13193 gdb_assert (templ_func != NULL);
13194
13195 templ_func->n_template_arguments = template_args.size ();
13196 templ_func->template_arguments
13197 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13198 templ_func->n_template_arguments);
13199 memcpy (templ_func->template_arguments,
13200 template_args.data (),
13201 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13202
13203 /* Make sure that the symtab is set on the new symbols. Even
13204 though they don't appear in this symtab directly, other parts
13205 of gdb assume that symbols do, and this is reasonably
13206 true. */
13207 for (symbol *sym : template_args)
13208 symbol_set_symtab (sym, symbol_symtab (templ_func));
13209 }
13210
13211 /* In C++, we can have functions nested inside functions (e.g., when
13212 a function declares a class that has methods). This means that
13213 when we finish processing a function scope, we may need to go
13214 back to building a containing block's symbol lists. */
13215 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13216 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13217
13218 /* If we've finished processing a top-level function, subsequent
13219 symbols go in the file symbol list. */
13220 if (cu->get_builder ()->outermost_context_p ())
13221 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13222 }
13223
13224 /* Process all the DIES contained within a lexical block scope. Start
13225 a new scope, process the dies, and then close the scope. */
13226
13227 static void
13228 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13229 {
13230 struct objfile *objfile = cu->per_objfile->objfile;
13231 struct gdbarch *gdbarch = objfile->arch ();
13232 CORE_ADDR lowpc, highpc;
13233 struct die_info *child_die;
13234 CORE_ADDR baseaddr;
13235
13236 baseaddr = objfile->text_section_offset ();
13237
13238 /* Ignore blocks with missing or invalid low and high pc attributes. */
13239 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13240 as multiple lexical blocks? Handling children in a sane way would
13241 be nasty. Might be easier to properly extend generic blocks to
13242 describe ranges. */
13243 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13244 {
13245 case PC_BOUNDS_NOT_PRESENT:
13246 /* DW_TAG_lexical_block has no attributes, process its children as if
13247 there was no wrapping by that DW_TAG_lexical_block.
13248 GCC does no longer produces such DWARF since GCC r224161. */
13249 for (child_die = die->child;
13250 child_die != NULL && child_die->tag;
13251 child_die = child_die->sibling)
13252 {
13253 /* We might already be processing this DIE. This can happen
13254 in an unusual circumstance -- where a subroutine A
13255 appears lexically in another subroutine B, but A actually
13256 inlines B. The recursion is broken here, rather than in
13257 inherit_abstract_dies, because it seems better to simply
13258 drop concrete children here. */
13259 if (!child_die->in_process)
13260 process_die (child_die, cu);
13261 }
13262 return;
13263 case PC_BOUNDS_INVALID:
13264 return;
13265 }
13266 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13267 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13268
13269 cu->get_builder ()->push_context (0, lowpc);
13270 if (die->child != NULL)
13271 {
13272 child_die = die->child;
13273 while (child_die && child_die->tag)
13274 {
13275 process_die (child_die, cu);
13276 child_die = child_die->sibling;
13277 }
13278 }
13279 inherit_abstract_dies (die, cu);
13280 struct context_stack cstk = cu->get_builder ()->pop_context ();
13281
13282 if (*cu->get_builder ()->get_local_symbols () != NULL
13283 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13284 {
13285 struct block *block
13286 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13287 cstk.start_addr, highpc);
13288
13289 /* Note that recording ranges after traversing children, as we
13290 do here, means that recording a parent's ranges entails
13291 walking across all its children's ranges as they appear in
13292 the address map, which is quadratic behavior.
13293
13294 It would be nicer to record the parent's ranges before
13295 traversing its children, simply overriding whatever you find
13296 there. But since we don't even decide whether to create a
13297 block until after we've traversed its children, that's hard
13298 to do. */
13299 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13300 }
13301 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13302 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13303 }
13304
13305 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13306
13307 static void
13308 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13309 {
13310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13311 struct objfile *objfile = per_objfile->objfile;
13312 struct gdbarch *gdbarch = objfile->arch ();
13313 CORE_ADDR pc, baseaddr;
13314 struct attribute *attr;
13315 struct call_site *call_site, call_site_local;
13316 void **slot;
13317 int nparams;
13318 struct die_info *child_die;
13319
13320 baseaddr = objfile->text_section_offset ();
13321
13322 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13323 if (attr == NULL)
13324 {
13325 /* This was a pre-DWARF-5 GNU extension alias
13326 for DW_AT_call_return_pc. */
13327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13328 }
13329 if (!attr)
13330 {
13331 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13332 "DIE %s [in module %s]"),
13333 sect_offset_str (die->sect_off), objfile_name (objfile));
13334 return;
13335 }
13336 pc = attr->value_as_address () + baseaddr;
13337 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13338
13339 if (cu->call_site_htab == NULL)
13340 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13341 NULL, &objfile->objfile_obstack,
13342 hashtab_obstack_allocate, NULL);
13343 call_site_local.pc = pc;
13344 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13345 if (*slot != NULL)
13346 {
13347 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13348 "DIE %s [in module %s]"),
13349 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13350 objfile_name (objfile));
13351 return;
13352 }
13353
13354 /* Count parameters at the caller. */
13355
13356 nparams = 0;
13357 for (child_die = die->child; child_die && child_die->tag;
13358 child_die = child_die->sibling)
13359 {
13360 if (child_die->tag != DW_TAG_call_site_parameter
13361 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13362 {
13363 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13364 "DW_TAG_call_site child DIE %s [in module %s]"),
13365 child_die->tag, sect_offset_str (child_die->sect_off),
13366 objfile_name (objfile));
13367 continue;
13368 }
13369
13370 nparams++;
13371 }
13372
13373 call_site
13374 = ((struct call_site *)
13375 obstack_alloc (&objfile->objfile_obstack,
13376 sizeof (*call_site)
13377 + (sizeof (*call_site->parameter) * (nparams - 1))));
13378 *slot = call_site;
13379 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13380 call_site->pc = pc;
13381
13382 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13383 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13384 {
13385 struct die_info *func_die;
13386
13387 /* Skip also over DW_TAG_inlined_subroutine. */
13388 for (func_die = die->parent;
13389 func_die && func_die->tag != DW_TAG_subprogram
13390 && func_die->tag != DW_TAG_subroutine_type;
13391 func_die = func_die->parent);
13392
13393 /* DW_AT_call_all_calls is a superset
13394 of DW_AT_call_all_tail_calls. */
13395 if (func_die
13396 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13398 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13399 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13400 {
13401 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13402 not complete. But keep CALL_SITE for look ups via call_site_htab,
13403 both the initial caller containing the real return address PC and
13404 the final callee containing the current PC of a chain of tail
13405 calls do not need to have the tail call list complete. But any
13406 function candidate for a virtual tail call frame searched via
13407 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13408 determined unambiguously. */
13409 }
13410 else
13411 {
13412 struct type *func_type = NULL;
13413
13414 if (func_die)
13415 func_type = get_die_type (func_die, cu);
13416 if (func_type != NULL)
13417 {
13418 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13419
13420 /* Enlist this call site to the function. */
13421 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13422 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13423 }
13424 else
13425 complaint (_("Cannot find function owning DW_TAG_call_site "
13426 "DIE %s [in module %s]"),
13427 sect_offset_str (die->sect_off), objfile_name (objfile));
13428 }
13429 }
13430
13431 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13432 if (attr == NULL)
13433 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13434 if (attr == NULL)
13435 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13436 if (attr == NULL)
13437 {
13438 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13439 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13440 }
13441 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13442 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13443 /* Keep NULL DWARF_BLOCK. */;
13444 else if (attr->form_is_block ())
13445 {
13446 struct dwarf2_locexpr_baton *dlbaton;
13447
13448 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13449 dlbaton->data = DW_BLOCK (attr)->data;
13450 dlbaton->size = DW_BLOCK (attr)->size;
13451 dlbaton->per_objfile = per_objfile;
13452 dlbaton->per_cu = cu->per_cu;
13453
13454 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13455 }
13456 else if (attr->form_is_ref ())
13457 {
13458 struct dwarf2_cu *target_cu = cu;
13459 struct die_info *target_die;
13460
13461 target_die = follow_die_ref (die, attr, &target_cu);
13462 gdb_assert (target_cu->per_objfile->objfile == objfile);
13463 if (die_is_declaration (target_die, target_cu))
13464 {
13465 const char *target_physname;
13466
13467 /* Prefer the mangled name; otherwise compute the demangled one. */
13468 target_physname = dw2_linkage_name (target_die, target_cu);
13469 if (target_physname == NULL)
13470 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13471 if (target_physname == NULL)
13472 complaint (_("DW_AT_call_target target DIE has invalid "
13473 "physname, for referencing DIE %s [in module %s]"),
13474 sect_offset_str (die->sect_off), objfile_name (objfile));
13475 else
13476 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13477 }
13478 else
13479 {
13480 CORE_ADDR lowpc;
13481
13482 /* DW_AT_entry_pc should be preferred. */
13483 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13484 <= PC_BOUNDS_INVALID)
13485 complaint (_("DW_AT_call_target target DIE has invalid "
13486 "low pc, for referencing DIE %s [in module %s]"),
13487 sect_offset_str (die->sect_off), objfile_name (objfile));
13488 else
13489 {
13490 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13491 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13492 }
13493 }
13494 }
13495 else
13496 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13497 "block nor reference, for DIE %s [in module %s]"),
13498 sect_offset_str (die->sect_off), objfile_name (objfile));
13499
13500 call_site->per_cu = cu->per_cu;
13501 call_site->per_objfile = per_objfile;
13502
13503 for (child_die = die->child;
13504 child_die && child_die->tag;
13505 child_die = child_die->sibling)
13506 {
13507 struct call_site_parameter *parameter;
13508 struct attribute *loc, *origin;
13509
13510 if (child_die->tag != DW_TAG_call_site_parameter
13511 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13512 {
13513 /* Already printed the complaint above. */
13514 continue;
13515 }
13516
13517 gdb_assert (call_site->parameter_count < nparams);
13518 parameter = &call_site->parameter[call_site->parameter_count];
13519
13520 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13521 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13522 register is contained in DW_AT_call_value. */
13523
13524 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13525 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13526 if (origin == NULL)
13527 {
13528 /* This was a pre-DWARF-5 GNU extension alias
13529 for DW_AT_call_parameter. */
13530 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13531 }
13532 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13533 {
13534 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13535
13536 sect_offset sect_off = origin->get_ref_die_offset ();
13537 if (!cu->header.offset_in_cu_p (sect_off))
13538 {
13539 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13540 binding can be done only inside one CU. Such referenced DIE
13541 therefore cannot be even moved to DW_TAG_partial_unit. */
13542 complaint (_("DW_AT_call_parameter offset is not in CU for "
13543 "DW_TAG_call_site child DIE %s [in module %s]"),
13544 sect_offset_str (child_die->sect_off),
13545 objfile_name (objfile));
13546 continue;
13547 }
13548 parameter->u.param_cu_off
13549 = (cu_offset) (sect_off - cu->header.sect_off);
13550 }
13551 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13552 {
13553 complaint (_("No DW_FORM_block* DW_AT_location for "
13554 "DW_TAG_call_site child DIE %s [in module %s]"),
13555 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13556 continue;
13557 }
13558 else
13559 {
13560 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13561 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13562 if (parameter->u.dwarf_reg != -1)
13563 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13564 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13565 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13566 &parameter->u.fb_offset))
13567 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13568 else
13569 {
13570 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13571 "for DW_FORM_block* DW_AT_location is supported for "
13572 "DW_TAG_call_site child DIE %s "
13573 "[in module %s]"),
13574 sect_offset_str (child_die->sect_off),
13575 objfile_name (objfile));
13576 continue;
13577 }
13578 }
13579
13580 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13581 if (attr == NULL)
13582 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13583 if (attr == NULL || !attr->form_is_block ())
13584 {
13585 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13586 "DW_TAG_call_site child DIE %s [in module %s]"),
13587 sect_offset_str (child_die->sect_off),
13588 objfile_name (objfile));
13589 continue;
13590 }
13591 parameter->value = DW_BLOCK (attr)->data;
13592 parameter->value_size = DW_BLOCK (attr)->size;
13593
13594 /* Parameters are not pre-cleared by memset above. */
13595 parameter->data_value = NULL;
13596 parameter->data_value_size = 0;
13597 call_site->parameter_count++;
13598
13599 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13600 if (attr == NULL)
13601 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13602 if (attr != nullptr)
13603 {
13604 if (!attr->form_is_block ())
13605 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13606 "DW_TAG_call_site child DIE %s [in module %s]"),
13607 sect_offset_str (child_die->sect_off),
13608 objfile_name (objfile));
13609 else
13610 {
13611 parameter->data_value = DW_BLOCK (attr)->data;
13612 parameter->data_value_size = DW_BLOCK (attr)->size;
13613 }
13614 }
13615 }
13616 }
13617
13618 /* Helper function for read_variable. If DIE represents a virtual
13619 table, then return the type of the concrete object that is
13620 associated with the virtual table. Otherwise, return NULL. */
13621
13622 static struct type *
13623 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13624 {
13625 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13626 if (attr == NULL)
13627 return NULL;
13628
13629 /* Find the type DIE. */
13630 struct die_info *type_die = NULL;
13631 struct dwarf2_cu *type_cu = cu;
13632
13633 if (attr->form_is_ref ())
13634 type_die = follow_die_ref (die, attr, &type_cu);
13635 if (type_die == NULL)
13636 return NULL;
13637
13638 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13639 return NULL;
13640 return die_containing_type (type_die, type_cu);
13641 }
13642
13643 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13644
13645 static void
13646 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13647 {
13648 struct rust_vtable_symbol *storage = NULL;
13649
13650 if (cu->language == language_rust)
13651 {
13652 struct type *containing_type = rust_containing_type (die, cu);
13653
13654 if (containing_type != NULL)
13655 {
13656 struct objfile *objfile = cu->per_objfile->objfile;
13657
13658 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13659 storage->concrete_type = containing_type;
13660 storage->subclass = SYMBOL_RUST_VTABLE;
13661 }
13662 }
13663
13664 struct symbol *res = new_symbol (die, NULL, cu, storage);
13665 struct attribute *abstract_origin
13666 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13667 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13668 if (res == NULL && loc && abstract_origin)
13669 {
13670 /* We have a variable without a name, but with a location and an abstract
13671 origin. This may be a concrete instance of an abstract variable
13672 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13673 later. */
13674 struct dwarf2_cu *origin_cu = cu;
13675 struct die_info *origin_die
13676 = follow_die_ref (die, abstract_origin, &origin_cu);
13677 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13678 per_objfile->per_bfd->abstract_to_concrete
13679 [origin_die->sect_off].push_back (die->sect_off);
13680 }
13681 }
13682
13683 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13684 reading .debug_rnglists.
13685 Callback's type should be:
13686 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13687 Return true if the attributes are present and valid, otherwise,
13688 return false. */
13689
13690 template <typename Callback>
13691 static bool
13692 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13693 Callback &&callback)
13694 {
13695 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13696 struct objfile *objfile = dwarf2_per_objfile->objfile;
13697 bfd *obfd = objfile->obfd;
13698 /* Base address selection entry. */
13699 gdb::optional<CORE_ADDR> base;
13700 const gdb_byte *buffer;
13701 CORE_ADDR baseaddr;
13702 bool overflow = false;
13703
13704 base = cu->base_address;
13705
13706 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13707 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13708 {
13709 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13710 offset);
13711 return false;
13712 }
13713 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13714
13715 baseaddr = objfile->text_section_offset ();
13716
13717 while (1)
13718 {
13719 /* Initialize it due to a false compiler warning. */
13720 CORE_ADDR range_beginning = 0, range_end = 0;
13721 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13722 + dwarf2_per_objfile->per_bfd->rnglists.size);
13723 unsigned int bytes_read;
13724
13725 if (buffer == buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13731 switch (rlet)
13732 {
13733 case DW_RLE_end_of_list:
13734 break;
13735 case DW_RLE_base_address:
13736 if (buffer + cu->header.addr_size > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 base = cu->header.read_address (obfd, buffer, &bytes_read);
13742 buffer += bytes_read;
13743 break;
13744 case DW_RLE_start_length:
13745 if (buffer + cu->header.addr_size > buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 range_beginning = cu->header.read_address (obfd, buffer,
13751 &bytes_read);
13752 buffer += bytes_read;
13753 range_end = (range_beginning
13754 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13755 buffer += bytes_read;
13756 if (buffer > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 break;
13762 case DW_RLE_offset_pair:
13763 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13764 buffer += bytes_read;
13765 if (buffer > buf_end)
13766 {
13767 overflow = true;
13768 break;
13769 }
13770 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13771 buffer += bytes_read;
13772 if (buffer > buf_end)
13773 {
13774 overflow = true;
13775 break;
13776 }
13777 break;
13778 case DW_RLE_start_end:
13779 if (buffer + 2 * cu->header.addr_size > buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 range_beginning = cu->header.read_address (obfd, buffer,
13785 &bytes_read);
13786 buffer += bytes_read;
13787 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13788 buffer += bytes_read;
13789 break;
13790 default:
13791 complaint (_("Invalid .debug_rnglists data (no base address)"));
13792 return false;
13793 }
13794 if (rlet == DW_RLE_end_of_list || overflow)
13795 break;
13796 if (rlet == DW_RLE_base_address)
13797 continue;
13798
13799 if (!base.has_value ())
13800 {
13801 /* We have no valid base address for the ranges
13802 data. */
13803 complaint (_("Invalid .debug_rnglists data (no base address)"));
13804 return false;
13805 }
13806
13807 if (range_beginning > range_end)
13808 {
13809 /* Inverted range entries are invalid. */
13810 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13811 return false;
13812 }
13813
13814 /* Empty range entries have no effect. */
13815 if (range_beginning == range_end)
13816 continue;
13817
13818 range_beginning += *base;
13819 range_end += *base;
13820
13821 /* A not-uncommon case of bad debug info.
13822 Don't pollute the addrmap with bad data. */
13823 if (range_beginning + baseaddr == 0
13824 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13825 {
13826 complaint (_(".debug_rnglists entry has start address of zero"
13827 " [in module %s]"), objfile_name (objfile));
13828 continue;
13829 }
13830
13831 callback (range_beginning, range_end);
13832 }
13833
13834 if (overflow)
13835 {
13836 complaint (_("Offset %d is not terminated "
13837 "for DW_AT_ranges attribute"),
13838 offset);
13839 return false;
13840 }
13841
13842 return true;
13843 }
13844
13845 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13846 Callback's type should be:
13847 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13848 Return 1 if the attributes are present and valid, otherwise, return 0. */
13849
13850 template <typename Callback>
13851 static int
13852 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13853 Callback &&callback)
13854 {
13855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13856 struct objfile *objfile = per_objfile->objfile;
13857 struct comp_unit_head *cu_header = &cu->header;
13858 bfd *obfd = objfile->obfd;
13859 unsigned int addr_size = cu_header->addr_size;
13860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13861 /* Base address selection entry. */
13862 gdb::optional<CORE_ADDR> base;
13863 unsigned int dummy;
13864 const gdb_byte *buffer;
13865 CORE_ADDR baseaddr;
13866
13867 if (cu_header->version >= 5)
13868 return dwarf2_rnglists_process (offset, cu, callback);
13869
13870 base = cu->base_address;
13871
13872 per_objfile->per_bfd->ranges.read (objfile);
13873 if (offset >= per_objfile->per_bfd->ranges.size)
13874 {
13875 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13876 offset);
13877 return 0;
13878 }
13879 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13880
13881 baseaddr = objfile->text_section_offset ();
13882
13883 while (1)
13884 {
13885 CORE_ADDR range_beginning, range_end;
13886
13887 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13888 buffer += addr_size;
13889 range_end = cu->header.read_address (obfd, buffer, &dummy);
13890 buffer += addr_size;
13891 offset += 2 * addr_size;
13892
13893 /* An end of list marker is a pair of zero addresses. */
13894 if (range_beginning == 0 && range_end == 0)
13895 /* Found the end of list entry. */
13896 break;
13897
13898 /* Each base address selection entry is a pair of 2 values.
13899 The first is the largest possible address, the second is
13900 the base address. Check for a base address here. */
13901 if ((range_beginning & mask) == mask)
13902 {
13903 /* If we found the largest possible address, then we already
13904 have the base address in range_end. */
13905 base = range_end;
13906 continue;
13907 }
13908
13909 if (!base.has_value ())
13910 {
13911 /* We have no valid base address for the ranges
13912 data. */
13913 complaint (_("Invalid .debug_ranges data (no base address)"));
13914 return 0;
13915 }
13916
13917 if (range_beginning > range_end)
13918 {
13919 /* Inverted range entries are invalid. */
13920 complaint (_("Invalid .debug_ranges data (inverted range)"));
13921 return 0;
13922 }
13923
13924 /* Empty range entries have no effect. */
13925 if (range_beginning == range_end)
13926 continue;
13927
13928 range_beginning += *base;
13929 range_end += *base;
13930
13931 /* A not-uncommon case of bad debug info.
13932 Don't pollute the addrmap with bad data. */
13933 if (range_beginning + baseaddr == 0
13934 && !per_objfile->per_bfd->has_section_at_zero)
13935 {
13936 complaint (_(".debug_ranges entry has start address of zero"
13937 " [in module %s]"), objfile_name (objfile));
13938 continue;
13939 }
13940
13941 callback (range_beginning, range_end);
13942 }
13943
13944 return 1;
13945 }
13946
13947 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13948 Return 1 if the attributes are present and valid, otherwise, return 0.
13949 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13950
13951 static int
13952 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13953 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13954 dwarf2_psymtab *ranges_pst)
13955 {
13956 struct objfile *objfile = cu->per_objfile->objfile;
13957 struct gdbarch *gdbarch = objfile->arch ();
13958 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13959 int low_set = 0;
13960 CORE_ADDR low = 0;
13961 CORE_ADDR high = 0;
13962 int retval;
13963
13964 retval = dwarf2_ranges_process (offset, cu,
13965 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13966 {
13967 if (ranges_pst != NULL)
13968 {
13969 CORE_ADDR lowpc;
13970 CORE_ADDR highpc;
13971
13972 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13973 range_beginning + baseaddr)
13974 - baseaddr);
13975 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13976 range_end + baseaddr)
13977 - baseaddr);
13978 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13979 lowpc, highpc - 1, ranges_pst);
13980 }
13981
13982 /* FIXME: This is recording everything as a low-high
13983 segment of consecutive addresses. We should have a
13984 data structure for discontiguous block ranges
13985 instead. */
13986 if (! low_set)
13987 {
13988 low = range_beginning;
13989 high = range_end;
13990 low_set = 1;
13991 }
13992 else
13993 {
13994 if (range_beginning < low)
13995 low = range_beginning;
13996 if (range_end > high)
13997 high = range_end;
13998 }
13999 });
14000 if (!retval)
14001 return 0;
14002
14003 if (! low_set)
14004 /* If the first entry is an end-of-list marker, the range
14005 describes an empty scope, i.e. no instructions. */
14006 return 0;
14007
14008 if (low_return)
14009 *low_return = low;
14010 if (high_return)
14011 *high_return = high;
14012 return 1;
14013 }
14014
14015 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14016 definition for the return value. *LOWPC and *HIGHPC are set iff
14017 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14018
14019 static enum pc_bounds_kind
14020 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14021 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14022 dwarf2_psymtab *pst)
14023 {
14024 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14025 struct attribute *attr;
14026 struct attribute *attr_high;
14027 CORE_ADDR low = 0;
14028 CORE_ADDR high = 0;
14029 enum pc_bounds_kind ret;
14030
14031 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14032 if (attr_high)
14033 {
14034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14035 if (attr != nullptr)
14036 {
14037 low = attr->value_as_address ();
14038 high = attr_high->value_as_address ();
14039 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14040 high += low;
14041 }
14042 else
14043 /* Found high w/o low attribute. */
14044 return PC_BOUNDS_INVALID;
14045
14046 /* Found consecutive range of addresses. */
14047 ret = PC_BOUNDS_HIGH_LOW;
14048 }
14049 else
14050 {
14051 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14052 if (attr != NULL)
14053 {
14054 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14055 We take advantage of the fact that DW_AT_ranges does not appear
14056 in DW_TAG_compile_unit of DWO files. */
14057 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14058 unsigned int ranges_offset = (DW_UNSND (attr)
14059 + (need_ranges_base
14060 ? cu->ranges_base
14061 : 0));
14062
14063 /* Value of the DW_AT_ranges attribute is the offset in the
14064 .debug_ranges section. */
14065 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14066 return PC_BOUNDS_INVALID;
14067 /* Found discontinuous range of addresses. */
14068 ret = PC_BOUNDS_RANGES;
14069 }
14070 else
14071 return PC_BOUNDS_NOT_PRESENT;
14072 }
14073
14074 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14075 if (high <= low)
14076 return PC_BOUNDS_INVALID;
14077
14078 /* When using the GNU linker, .gnu.linkonce. sections are used to
14079 eliminate duplicate copies of functions and vtables and such.
14080 The linker will arbitrarily choose one and discard the others.
14081 The AT_*_pc values for such functions refer to local labels in
14082 these sections. If the section from that file was discarded, the
14083 labels are not in the output, so the relocs get a value of 0.
14084 If this is a discarded function, mark the pc bounds as invalid,
14085 so that GDB will ignore it. */
14086 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14087 return PC_BOUNDS_INVALID;
14088
14089 *lowpc = low;
14090 if (highpc)
14091 *highpc = high;
14092 return ret;
14093 }
14094
14095 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14096 its low and high PC addresses. Do nothing if these addresses could not
14097 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14098 and HIGHPC to the high address if greater than HIGHPC. */
14099
14100 static void
14101 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14102 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14103 struct dwarf2_cu *cu)
14104 {
14105 CORE_ADDR low, high;
14106 struct die_info *child = die->child;
14107
14108 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14109 {
14110 *lowpc = std::min (*lowpc, low);
14111 *highpc = std::max (*highpc, high);
14112 }
14113
14114 /* If the language does not allow nested subprograms (either inside
14115 subprograms or lexical blocks), we're done. */
14116 if (cu->language != language_ada)
14117 return;
14118
14119 /* Check all the children of the given DIE. If it contains nested
14120 subprograms, then check their pc bounds. Likewise, we need to
14121 check lexical blocks as well, as they may also contain subprogram
14122 definitions. */
14123 while (child && child->tag)
14124 {
14125 if (child->tag == DW_TAG_subprogram
14126 || child->tag == DW_TAG_lexical_block)
14127 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14128 child = child->sibling;
14129 }
14130 }
14131
14132 /* Get the low and high pc's represented by the scope DIE, and store
14133 them in *LOWPC and *HIGHPC. If the correct values can't be
14134 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14135
14136 static void
14137 get_scope_pc_bounds (struct die_info *die,
14138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14139 struct dwarf2_cu *cu)
14140 {
14141 CORE_ADDR best_low = (CORE_ADDR) -1;
14142 CORE_ADDR best_high = (CORE_ADDR) 0;
14143 CORE_ADDR current_low, current_high;
14144
14145 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14146 >= PC_BOUNDS_RANGES)
14147 {
14148 best_low = current_low;
14149 best_high = current_high;
14150 }
14151 else
14152 {
14153 struct die_info *child = die->child;
14154
14155 while (child && child->tag)
14156 {
14157 switch (child->tag) {
14158 case DW_TAG_subprogram:
14159 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14160 break;
14161 case DW_TAG_namespace:
14162 case DW_TAG_module:
14163 /* FIXME: carlton/2004-01-16: Should we do this for
14164 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14165 that current GCC's always emit the DIEs corresponding
14166 to definitions of methods of classes as children of a
14167 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14168 the DIEs giving the declarations, which could be
14169 anywhere). But I don't see any reason why the
14170 standards says that they have to be there. */
14171 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14172
14173 if (current_low != ((CORE_ADDR) -1))
14174 {
14175 best_low = std::min (best_low, current_low);
14176 best_high = std::max (best_high, current_high);
14177 }
14178 break;
14179 default:
14180 /* Ignore. */
14181 break;
14182 }
14183
14184 child = child->sibling;
14185 }
14186 }
14187
14188 *lowpc = best_low;
14189 *highpc = best_high;
14190 }
14191
14192 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14193 in DIE. */
14194
14195 static void
14196 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14197 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14198 {
14199 struct objfile *objfile = cu->per_objfile->objfile;
14200 struct gdbarch *gdbarch = objfile->arch ();
14201 struct attribute *attr;
14202 struct attribute *attr_high;
14203
14204 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14205 if (attr_high)
14206 {
14207 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14208 if (attr != nullptr)
14209 {
14210 CORE_ADDR low = attr->value_as_address ();
14211 CORE_ADDR high = attr_high->value_as_address ();
14212
14213 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14214 high += low;
14215
14216 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14217 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14218 cu->get_builder ()->record_block_range (block, low, high - 1);
14219 }
14220 }
14221
14222 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14223 if (attr != nullptr)
14224 {
14225 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14226 We take advantage of the fact that DW_AT_ranges does not appear
14227 in DW_TAG_compile_unit of DWO files. */
14228 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14229
14230 /* The value of the DW_AT_ranges attribute is the offset of the
14231 address range list in the .debug_ranges section. */
14232 unsigned long offset = (DW_UNSND (attr)
14233 + (need_ranges_base ? cu->ranges_base : 0));
14234
14235 std::vector<blockrange> blockvec;
14236 dwarf2_ranges_process (offset, cu,
14237 [&] (CORE_ADDR start, CORE_ADDR end)
14238 {
14239 start += baseaddr;
14240 end += baseaddr;
14241 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14242 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14243 cu->get_builder ()->record_block_range (block, start, end - 1);
14244 blockvec.emplace_back (start, end);
14245 });
14246
14247 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14248 }
14249 }
14250
14251 /* Check whether the producer field indicates either of GCC < 4.6, or the
14252 Intel C/C++ compiler, and cache the result in CU. */
14253
14254 static void
14255 check_producer (struct dwarf2_cu *cu)
14256 {
14257 int major, minor;
14258
14259 if (cu->producer == NULL)
14260 {
14261 /* For unknown compilers expect their behavior is DWARF version
14262 compliant.
14263
14264 GCC started to support .debug_types sections by -gdwarf-4 since
14265 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14266 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14267 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14268 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14269 }
14270 else if (producer_is_gcc (cu->producer, &major, &minor))
14271 {
14272 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14273 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14274 }
14275 else if (producer_is_icc (cu->producer, &major, &minor))
14276 {
14277 cu->producer_is_icc = true;
14278 cu->producer_is_icc_lt_14 = major < 14;
14279 }
14280 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14281 cu->producer_is_codewarrior = true;
14282 else
14283 {
14284 /* For other non-GCC compilers, expect their behavior is DWARF version
14285 compliant. */
14286 }
14287
14288 cu->checked_producer = true;
14289 }
14290
14291 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14292 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14293 during 4.6.0 experimental. */
14294
14295 static bool
14296 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14297 {
14298 if (!cu->checked_producer)
14299 check_producer (cu);
14300
14301 return cu->producer_is_gxx_lt_4_6;
14302 }
14303
14304
14305 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14306 with incorrect is_stmt attributes. */
14307
14308 static bool
14309 producer_is_codewarrior (struct dwarf2_cu *cu)
14310 {
14311 if (!cu->checked_producer)
14312 check_producer (cu);
14313
14314 return cu->producer_is_codewarrior;
14315 }
14316
14317 /* Return the default accessibility type if it is not overridden by
14318 DW_AT_accessibility. */
14319
14320 static enum dwarf_access_attribute
14321 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14322 {
14323 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14324 {
14325 /* The default DWARF 2 accessibility for members is public, the default
14326 accessibility for inheritance is private. */
14327
14328 if (die->tag != DW_TAG_inheritance)
14329 return DW_ACCESS_public;
14330 else
14331 return DW_ACCESS_private;
14332 }
14333 else
14334 {
14335 /* DWARF 3+ defines the default accessibility a different way. The same
14336 rules apply now for DW_TAG_inheritance as for the members and it only
14337 depends on the container kind. */
14338
14339 if (die->parent->tag == DW_TAG_class_type)
14340 return DW_ACCESS_private;
14341 else
14342 return DW_ACCESS_public;
14343 }
14344 }
14345
14346 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14347 offset. If the attribute was not found return 0, otherwise return
14348 1. If it was found but could not properly be handled, set *OFFSET
14349 to 0. */
14350
14351 static int
14352 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14353 LONGEST *offset)
14354 {
14355 struct attribute *attr;
14356
14357 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14358 if (attr != NULL)
14359 {
14360 *offset = 0;
14361
14362 /* Note that we do not check for a section offset first here.
14363 This is because DW_AT_data_member_location is new in DWARF 4,
14364 so if we see it, we can assume that a constant form is really
14365 a constant and not a section offset. */
14366 if (attr->form_is_constant ())
14367 *offset = attr->constant_value (0);
14368 else if (attr->form_is_section_offset ())
14369 dwarf2_complex_location_expr_complaint ();
14370 else if (attr->form_is_block ())
14371 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14372 else
14373 dwarf2_complex_location_expr_complaint ();
14374
14375 return 1;
14376 }
14377
14378 return 0;
14379 }
14380
14381 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14382
14383 static void
14384 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14385 struct field *field)
14386 {
14387 struct attribute *attr;
14388
14389 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14390 if (attr != NULL)
14391 {
14392 if (attr->form_is_constant ())
14393 {
14394 LONGEST offset = attr->constant_value (0);
14395 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14396 }
14397 else if (attr->form_is_section_offset ())
14398 dwarf2_complex_location_expr_complaint ();
14399 else if (attr->form_is_block ())
14400 {
14401 bool handled;
14402 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14403 if (handled)
14404 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14405 else
14406 {
14407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14408 struct objfile *objfile = per_objfile->objfile;
14409 struct dwarf2_locexpr_baton *dlbaton
14410 = XOBNEW (&objfile->objfile_obstack,
14411 struct dwarf2_locexpr_baton);
14412 dlbaton->data = DW_BLOCK (attr)->data;
14413 dlbaton->size = DW_BLOCK (attr)->size;
14414 /* When using this baton, we want to compute the address
14415 of the field, not the value. This is why
14416 is_reference is set to false here. */
14417 dlbaton->is_reference = false;
14418 dlbaton->per_objfile = per_objfile;
14419 dlbaton->per_cu = cu->per_cu;
14420
14421 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14422 }
14423 }
14424 else
14425 dwarf2_complex_location_expr_complaint ();
14426 }
14427 }
14428
14429 /* Add an aggregate field to the field list. */
14430
14431 static void
14432 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14433 struct dwarf2_cu *cu)
14434 {
14435 struct objfile *objfile = cu->per_objfile->objfile;
14436 struct gdbarch *gdbarch = objfile->arch ();
14437 struct nextfield *new_field;
14438 struct attribute *attr;
14439 struct field *fp;
14440 const char *fieldname = "";
14441
14442 if (die->tag == DW_TAG_inheritance)
14443 {
14444 fip->baseclasses.emplace_back ();
14445 new_field = &fip->baseclasses.back ();
14446 }
14447 else
14448 {
14449 fip->fields.emplace_back ();
14450 new_field = &fip->fields.back ();
14451 }
14452
14453 new_field->offset = die->sect_off;
14454
14455 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14456 if (attr != nullptr)
14457 new_field->accessibility = DW_UNSND (attr);
14458 else
14459 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14460 if (new_field->accessibility != DW_ACCESS_public)
14461 fip->non_public_fields = 1;
14462
14463 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14464 if (attr != nullptr)
14465 new_field->virtuality = DW_UNSND (attr);
14466 else
14467 new_field->virtuality = DW_VIRTUALITY_none;
14468
14469 fp = &new_field->field;
14470
14471 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14472 {
14473 /* Data member other than a C++ static data member. */
14474
14475 /* Get type of field. */
14476 fp->type = die_type (die, cu);
14477
14478 SET_FIELD_BITPOS (*fp, 0);
14479
14480 /* Get bit size of field (zero if none). */
14481 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14482 if (attr != nullptr)
14483 {
14484 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14485 }
14486 else
14487 {
14488 FIELD_BITSIZE (*fp) = 0;
14489 }
14490
14491 /* Get bit offset of field. */
14492 handle_data_member_location (die, cu, fp);
14493 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14494 if (attr != nullptr)
14495 {
14496 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14497 {
14498 /* For big endian bits, the DW_AT_bit_offset gives the
14499 additional bit offset from the MSB of the containing
14500 anonymous object to the MSB of the field. We don't
14501 have to do anything special since we don't need to
14502 know the size of the anonymous object. */
14503 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14504 }
14505 else
14506 {
14507 /* For little endian bits, compute the bit offset to the
14508 MSB of the anonymous object, subtract off the number of
14509 bits from the MSB of the field to the MSB of the
14510 object, and then subtract off the number of bits of
14511 the field itself. The result is the bit offset of
14512 the LSB of the field. */
14513 int anonymous_size;
14514 int bit_offset = DW_UNSND (attr);
14515
14516 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14517 if (attr != nullptr)
14518 {
14519 /* The size of the anonymous object containing
14520 the bit field is explicit, so use the
14521 indicated size (in bytes). */
14522 anonymous_size = DW_UNSND (attr);
14523 }
14524 else
14525 {
14526 /* The size of the anonymous object containing
14527 the bit field must be inferred from the type
14528 attribute of the data member containing the
14529 bit field. */
14530 anonymous_size = TYPE_LENGTH (fp->type);
14531 }
14532 SET_FIELD_BITPOS (*fp,
14533 (FIELD_BITPOS (*fp)
14534 + anonymous_size * bits_per_byte
14535 - bit_offset - FIELD_BITSIZE (*fp)));
14536 }
14537 }
14538 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14539 if (attr != NULL)
14540 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14541 + attr->constant_value (0)));
14542
14543 /* Get name of field. */
14544 fieldname = dwarf2_name (die, cu);
14545 if (fieldname == NULL)
14546 fieldname = "";
14547
14548 /* The name is already allocated along with this objfile, so we don't
14549 need to duplicate it for the type. */
14550 fp->name = fieldname;
14551
14552 /* Change accessibility for artificial fields (e.g. virtual table
14553 pointer or virtual base class pointer) to private. */
14554 if (dwarf2_attr (die, DW_AT_artificial, cu))
14555 {
14556 FIELD_ARTIFICIAL (*fp) = 1;
14557 new_field->accessibility = DW_ACCESS_private;
14558 fip->non_public_fields = 1;
14559 }
14560 }
14561 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14562 {
14563 /* C++ static member. */
14564
14565 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14566 is a declaration, but all versions of G++ as of this writing
14567 (so through at least 3.2.1) incorrectly generate
14568 DW_TAG_variable tags. */
14569
14570 const char *physname;
14571
14572 /* Get name of field. */
14573 fieldname = dwarf2_name (die, cu);
14574 if (fieldname == NULL)
14575 return;
14576
14577 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14578 if (attr
14579 /* Only create a symbol if this is an external value.
14580 new_symbol checks this and puts the value in the global symbol
14581 table, which we want. If it is not external, new_symbol
14582 will try to put the value in cu->list_in_scope which is wrong. */
14583 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14584 {
14585 /* A static const member, not much different than an enum as far as
14586 we're concerned, except that we can support more types. */
14587 new_symbol (die, NULL, cu);
14588 }
14589
14590 /* Get physical name. */
14591 physname = dwarf2_physname (fieldname, die, cu);
14592
14593 /* The name is already allocated along with this objfile, so we don't
14594 need to duplicate it for the type. */
14595 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14596 FIELD_TYPE (*fp) = die_type (die, cu);
14597 FIELD_NAME (*fp) = fieldname;
14598 }
14599 else if (die->tag == DW_TAG_inheritance)
14600 {
14601 /* C++ base class field. */
14602 handle_data_member_location (die, cu, fp);
14603 FIELD_BITSIZE (*fp) = 0;
14604 FIELD_TYPE (*fp) = die_type (die, cu);
14605 FIELD_NAME (*fp) = fp->type->name ();
14606 }
14607 else
14608 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14609 }
14610
14611 /* Can the type given by DIE define another type? */
14612
14613 static bool
14614 type_can_define_types (const struct die_info *die)
14615 {
14616 switch (die->tag)
14617 {
14618 case DW_TAG_typedef:
14619 case DW_TAG_class_type:
14620 case DW_TAG_structure_type:
14621 case DW_TAG_union_type:
14622 case DW_TAG_enumeration_type:
14623 return true;
14624
14625 default:
14626 return false;
14627 }
14628 }
14629
14630 /* Add a type definition defined in the scope of the FIP's class. */
14631
14632 static void
14633 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14634 struct dwarf2_cu *cu)
14635 {
14636 struct decl_field fp;
14637 memset (&fp, 0, sizeof (fp));
14638
14639 gdb_assert (type_can_define_types (die));
14640
14641 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14642 fp.name = dwarf2_name (die, cu);
14643 fp.type = read_type_die (die, cu);
14644
14645 /* Save accessibility. */
14646 enum dwarf_access_attribute accessibility;
14647 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14648 if (attr != NULL)
14649 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14650 else
14651 accessibility = dwarf2_default_access_attribute (die, cu);
14652 switch (accessibility)
14653 {
14654 case DW_ACCESS_public:
14655 /* The assumed value if neither private nor protected. */
14656 break;
14657 case DW_ACCESS_private:
14658 fp.is_private = 1;
14659 break;
14660 case DW_ACCESS_protected:
14661 fp.is_protected = 1;
14662 break;
14663 default:
14664 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14665 }
14666
14667 if (die->tag == DW_TAG_typedef)
14668 fip->typedef_field_list.push_back (fp);
14669 else
14670 fip->nested_types_list.push_back (fp);
14671 }
14672
14673 /* A convenience typedef that's used when finding the discriminant
14674 field for a variant part. */
14675 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14676 offset_map_type;
14677
14678 /* Compute the discriminant range for a given variant. OBSTACK is
14679 where the results will be stored. VARIANT is the variant to
14680 process. IS_UNSIGNED indicates whether the discriminant is signed
14681 or unsigned. */
14682
14683 static const gdb::array_view<discriminant_range>
14684 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14685 bool is_unsigned)
14686 {
14687 std::vector<discriminant_range> ranges;
14688
14689 if (variant.default_branch)
14690 return {};
14691
14692 if (variant.discr_list_data == nullptr)
14693 {
14694 discriminant_range r
14695 = {variant.discriminant_value, variant.discriminant_value};
14696 ranges.push_back (r);
14697 }
14698 else
14699 {
14700 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14701 variant.discr_list_data->size);
14702 while (!data.empty ())
14703 {
14704 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14705 {
14706 complaint (_("invalid discriminant marker: %d"), data[0]);
14707 break;
14708 }
14709 bool is_range = data[0] == DW_DSC_range;
14710 data = data.slice (1);
14711
14712 ULONGEST low, high;
14713 unsigned int bytes_read;
14714
14715 if (data.empty ())
14716 {
14717 complaint (_("DW_AT_discr_list missing low value"));
14718 break;
14719 }
14720 if (is_unsigned)
14721 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14722 else
14723 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14724 &bytes_read);
14725 data = data.slice (bytes_read);
14726
14727 if (is_range)
14728 {
14729 if (data.empty ())
14730 {
14731 complaint (_("DW_AT_discr_list missing high value"));
14732 break;
14733 }
14734 if (is_unsigned)
14735 high = read_unsigned_leb128 (nullptr, data.data (),
14736 &bytes_read);
14737 else
14738 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14739 &bytes_read);
14740 data = data.slice (bytes_read);
14741 }
14742 else
14743 high = low;
14744
14745 ranges.push_back ({ low, high });
14746 }
14747 }
14748
14749 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14750 ranges.size ());
14751 std::copy (ranges.begin (), ranges.end (), result);
14752 return gdb::array_view<discriminant_range> (result, ranges.size ());
14753 }
14754
14755 static const gdb::array_view<variant_part> create_variant_parts
14756 (struct obstack *obstack,
14757 const offset_map_type &offset_map,
14758 struct field_info *fi,
14759 const std::vector<variant_part_builder> &variant_parts);
14760
14761 /* Fill in a "struct variant" for a given variant field. RESULT is
14762 the variant to fill in. OBSTACK is where any needed allocations
14763 will be done. OFFSET_MAP holds the mapping from section offsets to
14764 fields for the type. FI describes the fields of the type we're
14765 processing. FIELD is the variant field we're converting. */
14766
14767 static void
14768 create_one_variant (variant &result, struct obstack *obstack,
14769 const offset_map_type &offset_map,
14770 struct field_info *fi, const variant_field &field)
14771 {
14772 result.discriminants = convert_variant_range (obstack, field, false);
14773 result.first_field = field.first_field + fi->baseclasses.size ();
14774 result.last_field = field.last_field + fi->baseclasses.size ();
14775 result.parts = create_variant_parts (obstack, offset_map, fi,
14776 field.variant_parts);
14777 }
14778
14779 /* Fill in a "struct variant_part" for a given variant part. RESULT
14780 is the variant part to fill in. OBSTACK is where any needed
14781 allocations will be done. OFFSET_MAP holds the mapping from
14782 section offsets to fields for the type. FI describes the fields of
14783 the type we're processing. BUILDER is the variant part to be
14784 converted. */
14785
14786 static void
14787 create_one_variant_part (variant_part &result,
14788 struct obstack *obstack,
14789 const offset_map_type &offset_map,
14790 struct field_info *fi,
14791 const variant_part_builder &builder)
14792 {
14793 auto iter = offset_map.find (builder.discriminant_offset);
14794 if (iter == offset_map.end ())
14795 {
14796 result.discriminant_index = -1;
14797 /* Doesn't matter. */
14798 result.is_unsigned = false;
14799 }
14800 else
14801 {
14802 result.discriminant_index = iter->second;
14803 result.is_unsigned
14804 = TYPE_UNSIGNED (FIELD_TYPE
14805 (fi->fields[result.discriminant_index].field));
14806 }
14807
14808 size_t n = builder.variants.size ();
14809 variant *output = new (obstack) variant[n];
14810 for (size_t i = 0; i < n; ++i)
14811 create_one_variant (output[i], obstack, offset_map, fi,
14812 builder.variants[i]);
14813
14814 result.variants = gdb::array_view<variant> (output, n);
14815 }
14816
14817 /* Create a vector of variant parts that can be attached to a type.
14818 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14819 holds the mapping from section offsets to fields for the type. FI
14820 describes the fields of the type we're processing. VARIANT_PARTS
14821 is the vector to convert. */
14822
14823 static const gdb::array_view<variant_part>
14824 create_variant_parts (struct obstack *obstack,
14825 const offset_map_type &offset_map,
14826 struct field_info *fi,
14827 const std::vector<variant_part_builder> &variant_parts)
14828 {
14829 if (variant_parts.empty ())
14830 return {};
14831
14832 size_t n = variant_parts.size ();
14833 variant_part *result = new (obstack) variant_part[n];
14834 for (size_t i = 0; i < n; ++i)
14835 create_one_variant_part (result[i], obstack, offset_map, fi,
14836 variant_parts[i]);
14837
14838 return gdb::array_view<variant_part> (result, n);
14839 }
14840
14841 /* Compute the variant part vector for FIP, attaching it to TYPE when
14842 done. */
14843
14844 static void
14845 add_variant_property (struct field_info *fip, struct type *type,
14846 struct dwarf2_cu *cu)
14847 {
14848 /* Map section offsets of fields to their field index. Note the
14849 field index here does not take the number of baseclasses into
14850 account. */
14851 offset_map_type offset_map;
14852 for (int i = 0; i < fip->fields.size (); ++i)
14853 offset_map[fip->fields[i].offset] = i;
14854
14855 struct objfile *objfile = cu->per_objfile->objfile;
14856 gdb::array_view<variant_part> parts
14857 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14858 fip->variant_parts);
14859
14860 struct dynamic_prop prop;
14861 prop.kind = PROP_VARIANT_PARTS;
14862 prop.data.variant_parts
14863 = ((gdb::array_view<variant_part> *)
14864 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14865
14866 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14867 }
14868
14869 /* Create the vector of fields, and attach it to the type. */
14870
14871 static void
14872 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14873 struct dwarf2_cu *cu)
14874 {
14875 int nfields = fip->nfields ();
14876
14877 /* Record the field count, allocate space for the array of fields,
14878 and create blank accessibility bitfields if necessary. */
14879 type->set_num_fields (nfields);
14880 type->set_fields
14881 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14882
14883 if (fip->non_public_fields && cu->language != language_ada)
14884 {
14885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14886
14887 TYPE_FIELD_PRIVATE_BITS (type) =
14888 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14889 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14890
14891 TYPE_FIELD_PROTECTED_BITS (type) =
14892 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14893 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14894
14895 TYPE_FIELD_IGNORE_BITS (type) =
14896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14897 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14898 }
14899
14900 /* If the type has baseclasses, allocate and clear a bit vector for
14901 TYPE_FIELD_VIRTUAL_BITS. */
14902 if (!fip->baseclasses.empty () && cu->language != language_ada)
14903 {
14904 int num_bytes = B_BYTES (fip->baseclasses.size ());
14905 unsigned char *pointer;
14906
14907 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14908 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14909 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14910 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14911 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14912 }
14913
14914 if (!fip->variant_parts.empty ())
14915 add_variant_property (fip, type, cu);
14916
14917 /* Copy the saved-up fields into the field vector. */
14918 for (int i = 0; i < nfields; ++i)
14919 {
14920 struct nextfield &field
14921 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14922 : fip->fields[i - fip->baseclasses.size ()]);
14923
14924 type->field (i) = field.field;
14925 switch (field.accessibility)
14926 {
14927 case DW_ACCESS_private:
14928 if (cu->language != language_ada)
14929 SET_TYPE_FIELD_PRIVATE (type, i);
14930 break;
14931
14932 case DW_ACCESS_protected:
14933 if (cu->language != language_ada)
14934 SET_TYPE_FIELD_PROTECTED (type, i);
14935 break;
14936
14937 case DW_ACCESS_public:
14938 break;
14939
14940 default:
14941 /* Unknown accessibility. Complain and treat it as public. */
14942 {
14943 complaint (_("unsupported accessibility %d"),
14944 field.accessibility);
14945 }
14946 break;
14947 }
14948 if (i < fip->baseclasses.size ())
14949 {
14950 switch (field.virtuality)
14951 {
14952 case DW_VIRTUALITY_virtual:
14953 case DW_VIRTUALITY_pure_virtual:
14954 if (cu->language == language_ada)
14955 error (_("unexpected virtuality in component of Ada type"));
14956 SET_TYPE_FIELD_VIRTUAL (type, i);
14957 break;
14958 }
14959 }
14960 }
14961 }
14962
14963 /* Return true if this member function is a constructor, false
14964 otherwise. */
14965
14966 static int
14967 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14968 {
14969 const char *fieldname;
14970 const char *type_name;
14971 int len;
14972
14973 if (die->parent == NULL)
14974 return 0;
14975
14976 if (die->parent->tag != DW_TAG_structure_type
14977 && die->parent->tag != DW_TAG_union_type
14978 && die->parent->tag != DW_TAG_class_type)
14979 return 0;
14980
14981 fieldname = dwarf2_name (die, cu);
14982 type_name = dwarf2_name (die->parent, cu);
14983 if (fieldname == NULL || type_name == NULL)
14984 return 0;
14985
14986 len = strlen (fieldname);
14987 return (strncmp (fieldname, type_name, len) == 0
14988 && (type_name[len] == '\0' || type_name[len] == '<'));
14989 }
14990
14991 /* Check if the given VALUE is a recognized enum
14992 dwarf_defaulted_attribute constant according to DWARF5 spec,
14993 Table 7.24. */
14994
14995 static bool
14996 is_valid_DW_AT_defaulted (ULONGEST value)
14997 {
14998 switch (value)
14999 {
15000 case DW_DEFAULTED_no:
15001 case DW_DEFAULTED_in_class:
15002 case DW_DEFAULTED_out_of_class:
15003 return true;
15004 }
15005
15006 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15007 return false;
15008 }
15009
15010 /* Add a member function to the proper fieldlist. */
15011
15012 static void
15013 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15014 struct type *type, struct dwarf2_cu *cu)
15015 {
15016 struct objfile *objfile = cu->per_objfile->objfile;
15017 struct attribute *attr;
15018 int i;
15019 struct fnfieldlist *flp = nullptr;
15020 struct fn_field *fnp;
15021 const char *fieldname;
15022 struct type *this_type;
15023 enum dwarf_access_attribute accessibility;
15024
15025 if (cu->language == language_ada)
15026 error (_("unexpected member function in Ada type"));
15027
15028 /* Get name of member function. */
15029 fieldname = dwarf2_name (die, cu);
15030 if (fieldname == NULL)
15031 return;
15032
15033 /* Look up member function name in fieldlist. */
15034 for (i = 0; i < fip->fnfieldlists.size (); i++)
15035 {
15036 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15037 {
15038 flp = &fip->fnfieldlists[i];
15039 break;
15040 }
15041 }
15042
15043 /* Create a new fnfieldlist if necessary. */
15044 if (flp == nullptr)
15045 {
15046 fip->fnfieldlists.emplace_back ();
15047 flp = &fip->fnfieldlists.back ();
15048 flp->name = fieldname;
15049 i = fip->fnfieldlists.size () - 1;
15050 }
15051
15052 /* Create a new member function field and add it to the vector of
15053 fnfieldlists. */
15054 flp->fnfields.emplace_back ();
15055 fnp = &flp->fnfields.back ();
15056
15057 /* Delay processing of the physname until later. */
15058 if (cu->language == language_cplus)
15059 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15060 die, cu);
15061 else
15062 {
15063 const char *physname = dwarf2_physname (fieldname, die, cu);
15064 fnp->physname = physname ? physname : "";
15065 }
15066
15067 fnp->type = alloc_type (objfile);
15068 this_type = read_type_die (die, cu);
15069 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15070 {
15071 int nparams = this_type->num_fields ();
15072
15073 /* TYPE is the domain of this method, and THIS_TYPE is the type
15074 of the method itself (TYPE_CODE_METHOD). */
15075 smash_to_method_type (fnp->type, type,
15076 TYPE_TARGET_TYPE (this_type),
15077 this_type->fields (),
15078 this_type->num_fields (),
15079 TYPE_VARARGS (this_type));
15080
15081 /* Handle static member functions.
15082 Dwarf2 has no clean way to discern C++ static and non-static
15083 member functions. G++ helps GDB by marking the first
15084 parameter for non-static member functions (which is the this
15085 pointer) as artificial. We obtain this information from
15086 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15087 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15088 fnp->voffset = VOFFSET_STATIC;
15089 }
15090 else
15091 complaint (_("member function type missing for '%s'"),
15092 dwarf2_full_name (fieldname, die, cu));
15093
15094 /* Get fcontext from DW_AT_containing_type if present. */
15095 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15096 fnp->fcontext = die_containing_type (die, cu);
15097
15098 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15099 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15100
15101 /* Get accessibility. */
15102 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15103 if (attr != nullptr)
15104 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15105 else
15106 accessibility = dwarf2_default_access_attribute (die, cu);
15107 switch (accessibility)
15108 {
15109 case DW_ACCESS_private:
15110 fnp->is_private = 1;
15111 break;
15112 case DW_ACCESS_protected:
15113 fnp->is_protected = 1;
15114 break;
15115 }
15116
15117 /* Check for artificial methods. */
15118 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15119 if (attr && DW_UNSND (attr) != 0)
15120 fnp->is_artificial = 1;
15121
15122 /* Check for defaulted methods. */
15123 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15124 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15125 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15126
15127 /* Check for deleted methods. */
15128 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15129 if (attr != nullptr && DW_UNSND (attr) != 0)
15130 fnp->is_deleted = 1;
15131
15132 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15133
15134 /* Get index in virtual function table if it is a virtual member
15135 function. For older versions of GCC, this is an offset in the
15136 appropriate virtual table, as specified by DW_AT_containing_type.
15137 For everyone else, it is an expression to be evaluated relative
15138 to the object address. */
15139
15140 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15141 if (attr != nullptr)
15142 {
15143 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15144 {
15145 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15146 {
15147 /* Old-style GCC. */
15148 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15149 }
15150 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15151 || (DW_BLOCK (attr)->size > 1
15152 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15153 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15154 {
15155 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15156 if ((fnp->voffset % cu->header.addr_size) != 0)
15157 dwarf2_complex_location_expr_complaint ();
15158 else
15159 fnp->voffset /= cu->header.addr_size;
15160 fnp->voffset += 2;
15161 }
15162 else
15163 dwarf2_complex_location_expr_complaint ();
15164
15165 if (!fnp->fcontext)
15166 {
15167 /* If there is no `this' field and no DW_AT_containing_type,
15168 we cannot actually find a base class context for the
15169 vtable! */
15170 if (this_type->num_fields () == 0
15171 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15172 {
15173 complaint (_("cannot determine context for virtual member "
15174 "function \"%s\" (offset %s)"),
15175 fieldname, sect_offset_str (die->sect_off));
15176 }
15177 else
15178 {
15179 fnp->fcontext
15180 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15181 }
15182 }
15183 }
15184 else if (attr->form_is_section_offset ())
15185 {
15186 dwarf2_complex_location_expr_complaint ();
15187 }
15188 else
15189 {
15190 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15191 fieldname);
15192 }
15193 }
15194 else
15195 {
15196 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15197 if (attr && DW_UNSND (attr))
15198 {
15199 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15200 complaint (_("Member function \"%s\" (offset %s) is virtual "
15201 "but the vtable offset is not specified"),
15202 fieldname, sect_offset_str (die->sect_off));
15203 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15204 TYPE_CPLUS_DYNAMIC (type) = 1;
15205 }
15206 }
15207 }
15208
15209 /* Create the vector of member function fields, and attach it to the type. */
15210
15211 static void
15212 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15213 struct dwarf2_cu *cu)
15214 {
15215 if (cu->language == language_ada)
15216 error (_("unexpected member functions in Ada type"));
15217
15218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15219 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15220 TYPE_ALLOC (type,
15221 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15222
15223 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15224 {
15225 struct fnfieldlist &nf = fip->fnfieldlists[i];
15226 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15227
15228 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15229 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15230 fn_flp->fn_fields = (struct fn_field *)
15231 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15232
15233 for (int k = 0; k < nf.fnfields.size (); ++k)
15234 fn_flp->fn_fields[k] = nf.fnfields[k];
15235 }
15236
15237 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15238 }
15239
15240 /* Returns non-zero if NAME is the name of a vtable member in CU's
15241 language, zero otherwise. */
15242 static int
15243 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15244 {
15245 static const char vptr[] = "_vptr";
15246
15247 /* Look for the C++ form of the vtable. */
15248 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15249 return 1;
15250
15251 return 0;
15252 }
15253
15254 /* GCC outputs unnamed structures that are really pointers to member
15255 functions, with the ABI-specified layout. If TYPE describes
15256 such a structure, smash it into a member function type.
15257
15258 GCC shouldn't do this; it should just output pointer to member DIEs.
15259 This is GCC PR debug/28767. */
15260
15261 static void
15262 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15263 {
15264 struct type *pfn_type, *self_type, *new_type;
15265
15266 /* Check for a structure with no name and two children. */
15267 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15268 return;
15269
15270 /* Check for __pfn and __delta members. */
15271 if (TYPE_FIELD_NAME (type, 0) == NULL
15272 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15273 || TYPE_FIELD_NAME (type, 1) == NULL
15274 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15275 return;
15276
15277 /* Find the type of the method. */
15278 pfn_type = TYPE_FIELD_TYPE (type, 0);
15279 if (pfn_type == NULL
15280 || pfn_type->code () != TYPE_CODE_PTR
15281 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15282 return;
15283
15284 /* Look for the "this" argument. */
15285 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15286 if (pfn_type->num_fields () == 0
15287 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15288 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15289 return;
15290
15291 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15292 new_type = alloc_type (objfile);
15293 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15294 pfn_type->fields (), pfn_type->num_fields (),
15295 TYPE_VARARGS (pfn_type));
15296 smash_to_methodptr_type (type, new_type);
15297 }
15298
15299 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15300 appropriate error checking and issuing complaints if there is a
15301 problem. */
15302
15303 static ULONGEST
15304 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15305 {
15306 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15307
15308 if (attr == nullptr)
15309 return 0;
15310
15311 if (!attr->form_is_constant ())
15312 {
15313 complaint (_("DW_AT_alignment must have constant form"
15314 " - DIE at %s [in module %s]"),
15315 sect_offset_str (die->sect_off),
15316 objfile_name (cu->per_objfile->objfile));
15317 return 0;
15318 }
15319
15320 ULONGEST align;
15321 if (attr->form == DW_FORM_sdata)
15322 {
15323 LONGEST val = DW_SND (attr);
15324 if (val < 0)
15325 {
15326 complaint (_("DW_AT_alignment value must not be negative"
15327 " - DIE at %s [in module %s]"),
15328 sect_offset_str (die->sect_off),
15329 objfile_name (cu->per_objfile->objfile));
15330 return 0;
15331 }
15332 align = val;
15333 }
15334 else
15335 align = DW_UNSND (attr);
15336
15337 if (align == 0)
15338 {
15339 complaint (_("DW_AT_alignment value must not be zero"
15340 " - DIE at %s [in module %s]"),
15341 sect_offset_str (die->sect_off),
15342 objfile_name (cu->per_objfile->objfile));
15343 return 0;
15344 }
15345 if ((align & (align - 1)) != 0)
15346 {
15347 complaint (_("DW_AT_alignment value must be a power of 2"
15348 " - DIE at %s [in module %s]"),
15349 sect_offset_str (die->sect_off),
15350 objfile_name (cu->per_objfile->objfile));
15351 return 0;
15352 }
15353
15354 return align;
15355 }
15356
15357 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15358 the alignment for TYPE. */
15359
15360 static void
15361 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15362 struct type *type)
15363 {
15364 if (!set_type_align (type, get_alignment (cu, die)))
15365 complaint (_("DW_AT_alignment value too large"
15366 " - DIE at %s [in module %s]"),
15367 sect_offset_str (die->sect_off),
15368 objfile_name (cu->per_objfile->objfile));
15369 }
15370
15371 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15372 constant for a type, according to DWARF5 spec, Table 5.5. */
15373
15374 static bool
15375 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15376 {
15377 switch (value)
15378 {
15379 case DW_CC_normal:
15380 case DW_CC_pass_by_reference:
15381 case DW_CC_pass_by_value:
15382 return true;
15383
15384 default:
15385 complaint (_("unrecognized DW_AT_calling_convention value "
15386 "(%s) for a type"), pulongest (value));
15387 return false;
15388 }
15389 }
15390
15391 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15393 also according to GNU-specific values (see include/dwarf2.h). */
15394
15395 static bool
15396 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15397 {
15398 switch (value)
15399 {
15400 case DW_CC_normal:
15401 case DW_CC_program:
15402 case DW_CC_nocall:
15403 return true;
15404
15405 case DW_CC_GNU_renesas_sh:
15406 case DW_CC_GNU_borland_fastcall_i386:
15407 case DW_CC_GDB_IBM_OpenCL:
15408 return true;
15409
15410 default:
15411 complaint (_("unrecognized DW_AT_calling_convention value "
15412 "(%s) for a subroutine"), pulongest (value));
15413 return false;
15414 }
15415 }
15416
15417 /* Called when we find the DIE that starts a structure or union scope
15418 (definition) to create a type for the structure or union. Fill in
15419 the type's name and general properties; the members will not be
15420 processed until process_structure_scope. A symbol table entry for
15421 the type will also not be done until process_structure_scope (assuming
15422 the type has a name).
15423
15424 NOTE: we need to call these functions regardless of whether or not the
15425 DIE has a DW_AT_name attribute, since it might be an anonymous
15426 structure or union. This gets the type entered into our set of
15427 user defined types. */
15428
15429 static struct type *
15430 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15431 {
15432 struct objfile *objfile = cu->per_objfile->objfile;
15433 struct type *type;
15434 struct attribute *attr;
15435 const char *name;
15436
15437 /* If the definition of this type lives in .debug_types, read that type.
15438 Don't follow DW_AT_specification though, that will take us back up
15439 the chain and we want to go down. */
15440 attr = die->attr (DW_AT_signature);
15441 if (attr != nullptr)
15442 {
15443 type = get_DW_AT_signature_type (die, attr, cu);
15444
15445 /* The type's CU may not be the same as CU.
15446 Ensure TYPE is recorded with CU in die_type_hash. */
15447 return set_die_type (die, type, cu);
15448 }
15449
15450 type = alloc_type (objfile);
15451 INIT_CPLUS_SPECIFIC (type);
15452
15453 name = dwarf2_name (die, cu);
15454 if (name != NULL)
15455 {
15456 if (cu->language == language_cplus
15457 || cu->language == language_d
15458 || cu->language == language_rust)
15459 {
15460 const char *full_name = dwarf2_full_name (name, die, cu);
15461
15462 /* dwarf2_full_name might have already finished building the DIE's
15463 type. If so, there is no need to continue. */
15464 if (get_die_type (die, cu) != NULL)
15465 return get_die_type (die, cu);
15466
15467 type->set_name (full_name);
15468 }
15469 else
15470 {
15471 /* The name is already allocated along with this objfile, so
15472 we don't need to duplicate it for the type. */
15473 type->set_name (name);
15474 }
15475 }
15476
15477 if (die->tag == DW_TAG_structure_type)
15478 {
15479 type->set_code (TYPE_CODE_STRUCT);
15480 }
15481 else if (die->tag == DW_TAG_union_type)
15482 {
15483 type->set_code (TYPE_CODE_UNION);
15484 }
15485 else
15486 {
15487 type->set_code (TYPE_CODE_STRUCT);
15488 }
15489
15490 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15491 TYPE_DECLARED_CLASS (type) = 1;
15492
15493 /* Store the calling convention in the type if it's available in
15494 the die. Otherwise the calling convention remains set to
15495 the default value DW_CC_normal. */
15496 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15497 if (attr != nullptr
15498 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15499 {
15500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15501 TYPE_CPLUS_CALLING_CONVENTION (type)
15502 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15503 }
15504
15505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15506 if (attr != nullptr)
15507 {
15508 if (attr->form_is_constant ())
15509 TYPE_LENGTH (type) = DW_UNSND (attr);
15510 else
15511 {
15512 struct dynamic_prop prop;
15513 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15514 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15515 TYPE_LENGTH (type) = 0;
15516 }
15517 }
15518 else
15519 {
15520 TYPE_LENGTH (type) = 0;
15521 }
15522
15523 maybe_set_alignment (cu, die, type);
15524
15525 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15526 {
15527 /* ICC<14 does not output the required DW_AT_declaration on
15528 incomplete types, but gives them a size of zero. */
15529 TYPE_STUB (type) = 1;
15530 }
15531 else
15532 TYPE_STUB_SUPPORTED (type) = 1;
15533
15534 if (die_is_declaration (die, cu))
15535 TYPE_STUB (type) = 1;
15536 else if (attr == NULL && die->child == NULL
15537 && producer_is_realview (cu->producer))
15538 /* RealView does not output the required DW_AT_declaration
15539 on incomplete types. */
15540 TYPE_STUB (type) = 1;
15541
15542 /* We need to add the type field to the die immediately so we don't
15543 infinitely recurse when dealing with pointers to the structure
15544 type within the structure itself. */
15545 set_die_type (die, type, cu);
15546
15547 /* set_die_type should be already done. */
15548 set_descriptive_type (type, die, cu);
15549
15550 return type;
15551 }
15552
15553 static void handle_struct_member_die
15554 (struct die_info *child_die,
15555 struct type *type,
15556 struct field_info *fi,
15557 std::vector<struct symbol *> *template_args,
15558 struct dwarf2_cu *cu);
15559
15560 /* A helper for handle_struct_member_die that handles
15561 DW_TAG_variant_part. */
15562
15563 static void
15564 handle_variant_part (struct die_info *die, struct type *type,
15565 struct field_info *fi,
15566 std::vector<struct symbol *> *template_args,
15567 struct dwarf2_cu *cu)
15568 {
15569 variant_part_builder *new_part;
15570 if (fi->current_variant_part == nullptr)
15571 {
15572 fi->variant_parts.emplace_back ();
15573 new_part = &fi->variant_parts.back ();
15574 }
15575 else if (!fi->current_variant_part->processing_variant)
15576 {
15577 complaint (_("nested DW_TAG_variant_part seen "
15578 "- DIE at %s [in module %s]"),
15579 sect_offset_str (die->sect_off),
15580 objfile_name (cu->per_objfile->objfile));
15581 return;
15582 }
15583 else
15584 {
15585 variant_field &current = fi->current_variant_part->variants.back ();
15586 current.variant_parts.emplace_back ();
15587 new_part = &current.variant_parts.back ();
15588 }
15589
15590 /* When we recurse, we want callees to add to this new variant
15591 part. */
15592 scoped_restore save_current_variant_part
15593 = make_scoped_restore (&fi->current_variant_part, new_part);
15594
15595 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15596 if (discr == NULL)
15597 {
15598 /* It's a univariant form, an extension we support. */
15599 }
15600 else if (discr->form_is_ref ())
15601 {
15602 struct dwarf2_cu *target_cu = cu;
15603 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15604
15605 new_part->discriminant_offset = target_die->sect_off;
15606 }
15607 else
15608 {
15609 complaint (_("DW_AT_discr does not have DIE reference form"
15610 " - DIE at %s [in module %s]"),
15611 sect_offset_str (die->sect_off),
15612 objfile_name (cu->per_objfile->objfile));
15613 }
15614
15615 for (die_info *child_die = die->child;
15616 child_die != NULL;
15617 child_die = child_die->sibling)
15618 handle_struct_member_die (child_die, type, fi, template_args, cu);
15619 }
15620
15621 /* A helper for handle_struct_member_die that handles
15622 DW_TAG_variant. */
15623
15624 static void
15625 handle_variant (struct die_info *die, struct type *type,
15626 struct field_info *fi,
15627 std::vector<struct symbol *> *template_args,
15628 struct dwarf2_cu *cu)
15629 {
15630 if (fi->current_variant_part == nullptr)
15631 {
15632 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15633 "- DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
15635 objfile_name (cu->per_objfile->objfile));
15636 return;
15637 }
15638 if (fi->current_variant_part->processing_variant)
15639 {
15640 complaint (_("nested DW_TAG_variant seen "
15641 "- DIE at %s [in module %s]"),
15642 sect_offset_str (die->sect_off),
15643 objfile_name (cu->per_objfile->objfile));
15644 return;
15645 }
15646
15647 scoped_restore save_processing_variant
15648 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15649 true);
15650
15651 fi->current_variant_part->variants.emplace_back ();
15652 variant_field &variant = fi->current_variant_part->variants.back ();
15653 variant.first_field = fi->fields.size ();
15654
15655 /* In a variant we want to get the discriminant and also add a
15656 field for our sole member child. */
15657 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15658 if (discr == nullptr)
15659 {
15660 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15661 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15662 variant.default_branch = true;
15663 else
15664 variant.discr_list_data = DW_BLOCK (discr);
15665 }
15666 else
15667 variant.discriminant_value = DW_UNSND (discr);
15668
15669 for (die_info *variant_child = die->child;
15670 variant_child != NULL;
15671 variant_child = variant_child->sibling)
15672 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15673
15674 variant.last_field = fi->fields.size ();
15675 }
15676
15677 /* A helper for process_structure_scope that handles a single member
15678 DIE. */
15679
15680 static void
15681 handle_struct_member_die (struct die_info *child_die, struct type *type,
15682 struct field_info *fi,
15683 std::vector<struct symbol *> *template_args,
15684 struct dwarf2_cu *cu)
15685 {
15686 if (child_die->tag == DW_TAG_member
15687 || child_die->tag == DW_TAG_variable)
15688 {
15689 /* NOTE: carlton/2002-11-05: A C++ static data member
15690 should be a DW_TAG_member that is a declaration, but
15691 all versions of G++ as of this writing (so through at
15692 least 3.2.1) incorrectly generate DW_TAG_variable
15693 tags for them instead. */
15694 dwarf2_add_field (fi, child_die, cu);
15695 }
15696 else if (child_die->tag == DW_TAG_subprogram)
15697 {
15698 /* Rust doesn't have member functions in the C++ sense.
15699 However, it does emit ordinary functions as children
15700 of a struct DIE. */
15701 if (cu->language == language_rust)
15702 read_func_scope (child_die, cu);
15703 else
15704 {
15705 /* C++ member function. */
15706 dwarf2_add_member_fn (fi, child_die, type, cu);
15707 }
15708 }
15709 else if (child_die->tag == DW_TAG_inheritance)
15710 {
15711 /* C++ base class field. */
15712 dwarf2_add_field (fi, child_die, cu);
15713 }
15714 else if (type_can_define_types (child_die))
15715 dwarf2_add_type_defn (fi, child_die, cu);
15716 else if (child_die->tag == DW_TAG_template_type_param
15717 || child_die->tag == DW_TAG_template_value_param)
15718 {
15719 struct symbol *arg = new_symbol (child_die, NULL, cu);
15720
15721 if (arg != NULL)
15722 template_args->push_back (arg);
15723 }
15724 else if (child_die->tag == DW_TAG_variant_part)
15725 handle_variant_part (child_die, type, fi, template_args, cu);
15726 else if (child_die->tag == DW_TAG_variant)
15727 handle_variant (child_die, type, fi, template_args, cu);
15728 }
15729
15730 /* Finish creating a structure or union type, including filling in
15731 its members and creating a symbol for it. */
15732
15733 static void
15734 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15735 {
15736 struct objfile *objfile = cu->per_objfile->objfile;
15737 struct die_info *child_die;
15738 struct type *type;
15739
15740 type = get_die_type (die, cu);
15741 if (type == NULL)
15742 type = read_structure_type (die, cu);
15743
15744 bool has_template_parameters = false;
15745 if (die->child != NULL && ! die_is_declaration (die, cu))
15746 {
15747 struct field_info fi;
15748 std::vector<struct symbol *> template_args;
15749
15750 child_die = die->child;
15751
15752 while (child_die && child_die->tag)
15753 {
15754 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15755 child_die = child_die->sibling;
15756 }
15757
15758 /* Attach template arguments to type. */
15759 if (!template_args.empty ())
15760 {
15761 has_template_parameters = true;
15762 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15763 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15764 TYPE_TEMPLATE_ARGUMENTS (type)
15765 = XOBNEWVEC (&objfile->objfile_obstack,
15766 struct symbol *,
15767 TYPE_N_TEMPLATE_ARGUMENTS (type));
15768 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15769 template_args.data (),
15770 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15771 * sizeof (struct symbol *)));
15772 }
15773
15774 /* Attach fields and member functions to the type. */
15775 if (fi.nfields () > 0)
15776 dwarf2_attach_fields_to_type (&fi, type, cu);
15777 if (!fi.fnfieldlists.empty ())
15778 {
15779 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15780
15781 /* Get the type which refers to the base class (possibly this
15782 class itself) which contains the vtable pointer for the current
15783 class from the DW_AT_containing_type attribute. This use of
15784 DW_AT_containing_type is a GNU extension. */
15785
15786 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15787 {
15788 struct type *t = die_containing_type (die, cu);
15789
15790 set_type_vptr_basetype (type, t);
15791 if (type == t)
15792 {
15793 int i;
15794
15795 /* Our own class provides vtbl ptr. */
15796 for (i = t->num_fields () - 1;
15797 i >= TYPE_N_BASECLASSES (t);
15798 --i)
15799 {
15800 const char *fieldname = TYPE_FIELD_NAME (t, i);
15801
15802 if (is_vtable_name (fieldname, cu))
15803 {
15804 set_type_vptr_fieldno (type, i);
15805 break;
15806 }
15807 }
15808
15809 /* Complain if virtual function table field not found. */
15810 if (i < TYPE_N_BASECLASSES (t))
15811 complaint (_("virtual function table pointer "
15812 "not found when defining class '%s'"),
15813 type->name () ? type->name () : "");
15814 }
15815 else
15816 {
15817 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15818 }
15819 }
15820 else if (cu->producer
15821 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15822 {
15823 /* The IBM XLC compiler does not provide direct indication
15824 of the containing type, but the vtable pointer is
15825 always named __vfp. */
15826
15827 int i;
15828
15829 for (i = type->num_fields () - 1;
15830 i >= TYPE_N_BASECLASSES (type);
15831 --i)
15832 {
15833 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15834 {
15835 set_type_vptr_fieldno (type, i);
15836 set_type_vptr_basetype (type, type);
15837 break;
15838 }
15839 }
15840 }
15841 }
15842
15843 /* Copy fi.typedef_field_list linked list elements content into the
15844 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15845 if (!fi.typedef_field_list.empty ())
15846 {
15847 int count = fi.typedef_field_list.size ();
15848
15849 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15850 TYPE_TYPEDEF_FIELD_ARRAY (type)
15851 = ((struct decl_field *)
15852 TYPE_ALLOC (type,
15853 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15854 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15855
15856 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15857 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15858 }
15859
15860 /* Copy fi.nested_types_list linked list elements content into the
15861 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15862 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15863 {
15864 int count = fi.nested_types_list.size ();
15865
15866 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15867 TYPE_NESTED_TYPES_ARRAY (type)
15868 = ((struct decl_field *)
15869 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15870 TYPE_NESTED_TYPES_COUNT (type) = count;
15871
15872 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15873 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15874 }
15875 }
15876
15877 quirk_gcc_member_function_pointer (type, objfile);
15878 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15879 cu->rust_unions.push_back (type);
15880
15881 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15882 snapshots) has been known to create a die giving a declaration
15883 for a class that has, as a child, a die giving a definition for a
15884 nested class. So we have to process our children even if the
15885 current die is a declaration. Normally, of course, a declaration
15886 won't have any children at all. */
15887
15888 child_die = die->child;
15889
15890 while (child_die != NULL && child_die->tag)
15891 {
15892 if (child_die->tag == DW_TAG_member
15893 || child_die->tag == DW_TAG_variable
15894 || child_die->tag == DW_TAG_inheritance
15895 || child_die->tag == DW_TAG_template_value_param
15896 || child_die->tag == DW_TAG_template_type_param)
15897 {
15898 /* Do nothing. */
15899 }
15900 else
15901 process_die (child_die, cu);
15902
15903 child_die = child_die->sibling;
15904 }
15905
15906 /* Do not consider external references. According to the DWARF standard,
15907 these DIEs are identified by the fact that they have no byte_size
15908 attribute, and a declaration attribute. */
15909 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15910 || !die_is_declaration (die, cu)
15911 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15912 {
15913 struct symbol *sym = new_symbol (die, type, cu);
15914
15915 if (has_template_parameters)
15916 {
15917 struct symtab *symtab;
15918 if (sym != nullptr)
15919 symtab = symbol_symtab (sym);
15920 else if (cu->line_header != nullptr)
15921 {
15922 /* Any related symtab will do. */
15923 symtab
15924 = cu->line_header->file_names ()[0].symtab;
15925 }
15926 else
15927 {
15928 symtab = nullptr;
15929 complaint (_("could not find suitable "
15930 "symtab for template parameter"
15931 " - DIE at %s [in module %s]"),
15932 sect_offset_str (die->sect_off),
15933 objfile_name (objfile));
15934 }
15935
15936 if (symtab != nullptr)
15937 {
15938 /* Make sure that the symtab is set on the new symbols.
15939 Even though they don't appear in this symtab directly,
15940 other parts of gdb assume that symbols do, and this is
15941 reasonably true. */
15942 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15943 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15944 }
15945 }
15946 }
15947 }
15948
15949 /* Assuming DIE is an enumeration type, and TYPE is its associated
15950 type, update TYPE using some information only available in DIE's
15951 children. In particular, the fields are computed. */
15952
15953 static void
15954 update_enumeration_type_from_children (struct die_info *die,
15955 struct type *type,
15956 struct dwarf2_cu *cu)
15957 {
15958 struct die_info *child_die;
15959 int unsigned_enum = 1;
15960 int flag_enum = 1;
15961
15962 auto_obstack obstack;
15963 std::vector<struct field> fields;
15964
15965 for (child_die = die->child;
15966 child_die != NULL && child_die->tag;
15967 child_die = child_die->sibling)
15968 {
15969 struct attribute *attr;
15970 LONGEST value;
15971 const gdb_byte *bytes;
15972 struct dwarf2_locexpr_baton *baton;
15973 const char *name;
15974
15975 if (child_die->tag != DW_TAG_enumerator)
15976 continue;
15977
15978 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15979 if (attr == NULL)
15980 continue;
15981
15982 name = dwarf2_name (child_die, cu);
15983 if (name == NULL)
15984 name = "<anonymous enumerator>";
15985
15986 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15987 &value, &bytes, &baton);
15988 if (value < 0)
15989 {
15990 unsigned_enum = 0;
15991 flag_enum = 0;
15992 }
15993 else
15994 {
15995 if (count_one_bits_ll (value) >= 2)
15996 flag_enum = 0;
15997 }
15998
15999 fields.emplace_back ();
16000 struct field &field = fields.back ();
16001 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16002 SET_FIELD_ENUMVAL (field, value);
16003 }
16004
16005 if (!fields.empty ())
16006 {
16007 type->set_num_fields (fields.size ());
16008 type->set_fields
16009 ((struct field *)
16010 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16011 memcpy (type->fields (), fields.data (),
16012 sizeof (struct field) * fields.size ());
16013 }
16014
16015 if (unsigned_enum)
16016 TYPE_UNSIGNED (type) = 1;
16017 if (flag_enum)
16018 TYPE_FLAG_ENUM (type) = 1;
16019 }
16020
16021 /* Given a DW_AT_enumeration_type die, set its type. We do not
16022 complete the type's fields yet, or create any symbols. */
16023
16024 static struct type *
16025 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16026 {
16027 struct objfile *objfile = cu->per_objfile->objfile;
16028 struct type *type;
16029 struct attribute *attr;
16030 const char *name;
16031
16032 /* If the definition of this type lives in .debug_types, read that type.
16033 Don't follow DW_AT_specification though, that will take us back up
16034 the chain and we want to go down. */
16035 attr = die->attr (DW_AT_signature);
16036 if (attr != nullptr)
16037 {
16038 type = get_DW_AT_signature_type (die, attr, cu);
16039
16040 /* The type's CU may not be the same as CU.
16041 Ensure TYPE is recorded with CU in die_type_hash. */
16042 return set_die_type (die, type, cu);
16043 }
16044
16045 type = alloc_type (objfile);
16046
16047 type->set_code (TYPE_CODE_ENUM);
16048 name = dwarf2_full_name (NULL, die, cu);
16049 if (name != NULL)
16050 type->set_name (name);
16051
16052 attr = dwarf2_attr (die, DW_AT_type, cu);
16053 if (attr != NULL)
16054 {
16055 struct type *underlying_type = die_type (die, cu);
16056
16057 TYPE_TARGET_TYPE (type) = underlying_type;
16058 }
16059
16060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16061 if (attr != nullptr)
16062 {
16063 TYPE_LENGTH (type) = DW_UNSND (attr);
16064 }
16065 else
16066 {
16067 TYPE_LENGTH (type) = 0;
16068 }
16069
16070 maybe_set_alignment (cu, die, type);
16071
16072 /* The enumeration DIE can be incomplete. In Ada, any type can be
16073 declared as private in the package spec, and then defined only
16074 inside the package body. Such types are known as Taft Amendment
16075 Types. When another package uses such a type, an incomplete DIE
16076 may be generated by the compiler. */
16077 if (die_is_declaration (die, cu))
16078 TYPE_STUB (type) = 1;
16079
16080 /* If this type has an underlying type that is not a stub, then we
16081 may use its attributes. We always use the "unsigned" attribute
16082 in this situation, because ordinarily we guess whether the type
16083 is unsigned -- but the guess can be wrong and the underlying type
16084 can tell us the reality. However, we defer to a local size
16085 attribute if one exists, because this lets the compiler override
16086 the underlying type if needed. */
16087 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16088 {
16089 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16090 underlying_type = check_typedef (underlying_type);
16091 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16092 if (TYPE_LENGTH (type) == 0)
16093 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16094 if (TYPE_RAW_ALIGN (type) == 0
16095 && TYPE_RAW_ALIGN (underlying_type) != 0)
16096 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16097 }
16098
16099 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16100
16101 set_die_type (die, type, cu);
16102
16103 /* Finish the creation of this type by using the enum's children.
16104 Note that, as usual, this must come after set_die_type to avoid
16105 infinite recursion when trying to compute the names of the
16106 enumerators. */
16107 update_enumeration_type_from_children (die, type, cu);
16108
16109 return type;
16110 }
16111
16112 /* Given a pointer to a die which begins an enumeration, process all
16113 the dies that define the members of the enumeration, and create the
16114 symbol for the enumeration type.
16115
16116 NOTE: We reverse the order of the element list. */
16117
16118 static void
16119 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16120 {
16121 struct type *this_type;
16122
16123 this_type = get_die_type (die, cu);
16124 if (this_type == NULL)
16125 this_type = read_enumeration_type (die, cu);
16126
16127 if (die->child != NULL)
16128 {
16129 struct die_info *child_die;
16130 const char *name;
16131
16132 child_die = die->child;
16133 while (child_die && child_die->tag)
16134 {
16135 if (child_die->tag != DW_TAG_enumerator)
16136 {
16137 process_die (child_die, cu);
16138 }
16139 else
16140 {
16141 name = dwarf2_name (child_die, cu);
16142 if (name)
16143 new_symbol (child_die, this_type, cu);
16144 }
16145
16146 child_die = child_die->sibling;
16147 }
16148 }
16149
16150 /* If we are reading an enum from a .debug_types unit, and the enum
16151 is a declaration, and the enum is not the signatured type in the
16152 unit, then we do not want to add a symbol for it. Adding a
16153 symbol would in some cases obscure the true definition of the
16154 enum, giving users an incomplete type when the definition is
16155 actually available. Note that we do not want to do this for all
16156 enums which are just declarations, because C++0x allows forward
16157 enum declarations. */
16158 if (cu->per_cu->is_debug_types
16159 && die_is_declaration (die, cu))
16160 {
16161 struct signatured_type *sig_type;
16162
16163 sig_type = (struct signatured_type *) cu->per_cu;
16164 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16165 if (sig_type->type_offset_in_section != die->sect_off)
16166 return;
16167 }
16168
16169 new_symbol (die, this_type, cu);
16170 }
16171
16172 /* Extract all information from a DW_TAG_array_type DIE and put it in
16173 the DIE's type field. For now, this only handles one dimensional
16174 arrays. */
16175
16176 static struct type *
16177 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16178 {
16179 struct objfile *objfile = cu->per_objfile->objfile;
16180 struct die_info *child_die;
16181 struct type *type;
16182 struct type *element_type, *range_type, *index_type;
16183 struct attribute *attr;
16184 const char *name;
16185 struct dynamic_prop *byte_stride_prop = NULL;
16186 unsigned int bit_stride = 0;
16187
16188 element_type = die_type (die, cu);
16189
16190 /* The die_type call above may have already set the type for this DIE. */
16191 type = get_die_type (die, cu);
16192 if (type)
16193 return type;
16194
16195 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16196 if (attr != NULL)
16197 {
16198 int stride_ok;
16199 struct type *prop_type = cu->addr_sized_int_type (false);
16200
16201 byte_stride_prop
16202 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16203 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16204 prop_type);
16205 if (!stride_ok)
16206 {
16207 complaint (_("unable to read array DW_AT_byte_stride "
16208 " - DIE at %s [in module %s]"),
16209 sect_offset_str (die->sect_off),
16210 objfile_name (cu->per_objfile->objfile));
16211 /* Ignore this attribute. We will likely not be able to print
16212 arrays of this type correctly, but there is little we can do
16213 to help if we cannot read the attribute's value. */
16214 byte_stride_prop = NULL;
16215 }
16216 }
16217
16218 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16219 if (attr != NULL)
16220 bit_stride = DW_UNSND (attr);
16221
16222 /* Irix 6.2 native cc creates array types without children for
16223 arrays with unspecified length. */
16224 if (die->child == NULL)
16225 {
16226 index_type = objfile_type (objfile)->builtin_int;
16227 range_type = create_static_range_type (NULL, index_type, 0, -1);
16228 type = create_array_type_with_stride (NULL, element_type, range_type,
16229 byte_stride_prop, bit_stride);
16230 return set_die_type (die, type, cu);
16231 }
16232
16233 std::vector<struct type *> range_types;
16234 child_die = die->child;
16235 while (child_die && child_die->tag)
16236 {
16237 if (child_die->tag == DW_TAG_subrange_type)
16238 {
16239 struct type *child_type = read_type_die (child_die, cu);
16240
16241 if (child_type != NULL)
16242 {
16243 /* The range type was succesfully read. Save it for the
16244 array type creation. */
16245 range_types.push_back (child_type);
16246 }
16247 }
16248 child_die = child_die->sibling;
16249 }
16250
16251 /* Dwarf2 dimensions are output from left to right, create the
16252 necessary array types in backwards order. */
16253
16254 type = element_type;
16255
16256 if (read_array_order (die, cu) == DW_ORD_col_major)
16257 {
16258 int i = 0;
16259
16260 while (i < range_types.size ())
16261 type = create_array_type_with_stride (NULL, type, range_types[i++],
16262 byte_stride_prop, bit_stride);
16263 }
16264 else
16265 {
16266 size_t ndim = range_types.size ();
16267 while (ndim-- > 0)
16268 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16269 byte_stride_prop, bit_stride);
16270 }
16271
16272 /* Understand Dwarf2 support for vector types (like they occur on
16273 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16274 array type. This is not part of the Dwarf2/3 standard yet, but a
16275 custom vendor extension. The main difference between a regular
16276 array and the vector variant is that vectors are passed by value
16277 to functions. */
16278 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16279 if (attr != nullptr)
16280 make_vector_type (type);
16281
16282 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16283 implementation may choose to implement triple vectors using this
16284 attribute. */
16285 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16286 if (attr != nullptr)
16287 {
16288 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16289 TYPE_LENGTH (type) = DW_UNSND (attr);
16290 else
16291 complaint (_("DW_AT_byte_size for array type smaller "
16292 "than the total size of elements"));
16293 }
16294
16295 name = dwarf2_name (die, cu);
16296 if (name)
16297 type->set_name (name);
16298
16299 maybe_set_alignment (cu, die, type);
16300
16301 /* Install the type in the die. */
16302 set_die_type (die, type, cu);
16303
16304 /* set_die_type should be already done. */
16305 set_descriptive_type (type, die, cu);
16306
16307 return type;
16308 }
16309
16310 static enum dwarf_array_dim_ordering
16311 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16312 {
16313 struct attribute *attr;
16314
16315 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16316
16317 if (attr != nullptr)
16318 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16319
16320 /* GNU F77 is a special case, as at 08/2004 array type info is the
16321 opposite order to the dwarf2 specification, but data is still
16322 laid out as per normal fortran.
16323
16324 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16325 version checking. */
16326
16327 if (cu->language == language_fortran
16328 && cu->producer && strstr (cu->producer, "GNU F77"))
16329 {
16330 return DW_ORD_row_major;
16331 }
16332
16333 switch (cu->language_defn->la_array_ordering)
16334 {
16335 case array_column_major:
16336 return DW_ORD_col_major;
16337 case array_row_major:
16338 default:
16339 return DW_ORD_row_major;
16340 };
16341 }
16342
16343 /* Extract all information from a DW_TAG_set_type DIE and put it in
16344 the DIE's type field. */
16345
16346 static struct type *
16347 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16348 {
16349 struct type *domain_type, *set_type;
16350 struct attribute *attr;
16351
16352 domain_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 set_type = get_die_type (die, cu);
16356 if (set_type)
16357 return set_type;
16358
16359 set_type = create_set_type (NULL, domain_type);
16360
16361 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16362 if (attr != nullptr)
16363 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16364
16365 maybe_set_alignment (cu, die, set_type);
16366
16367 return set_die_type (die, set_type, cu);
16368 }
16369
16370 /* A helper for read_common_block that creates a locexpr baton.
16371 SYM is the symbol which we are marking as computed.
16372 COMMON_DIE is the DIE for the common block.
16373 COMMON_LOC is the location expression attribute for the common
16374 block itself.
16375 MEMBER_LOC is the location expression attribute for the particular
16376 member of the common block that we are processing.
16377 CU is the CU from which the above come. */
16378
16379 static void
16380 mark_common_block_symbol_computed (struct symbol *sym,
16381 struct die_info *common_die,
16382 struct attribute *common_loc,
16383 struct attribute *member_loc,
16384 struct dwarf2_cu *cu)
16385 {
16386 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16387 struct objfile *objfile = per_objfile->objfile;
16388 struct dwarf2_locexpr_baton *baton;
16389 gdb_byte *ptr;
16390 unsigned int cu_off;
16391 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16392 LONGEST offset = 0;
16393
16394 gdb_assert (common_loc && member_loc);
16395 gdb_assert (common_loc->form_is_block ());
16396 gdb_assert (member_loc->form_is_block ()
16397 || member_loc->form_is_constant ());
16398
16399 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16400 baton->per_objfile = per_objfile;
16401 baton->per_cu = cu->per_cu;
16402 gdb_assert (baton->per_cu);
16403
16404 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16405
16406 if (member_loc->form_is_constant ())
16407 {
16408 offset = member_loc->constant_value (0);
16409 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16410 }
16411 else
16412 baton->size += DW_BLOCK (member_loc)->size;
16413
16414 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16415 baton->data = ptr;
16416
16417 *ptr++ = DW_OP_call4;
16418 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16419 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16420 ptr += 4;
16421
16422 if (member_loc->form_is_constant ())
16423 {
16424 *ptr++ = DW_OP_addr;
16425 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16426 ptr += cu->header.addr_size;
16427 }
16428 else
16429 {
16430 /* We have to copy the data here, because DW_OP_call4 will only
16431 use a DW_AT_location attribute. */
16432 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16433 ptr += DW_BLOCK (member_loc)->size;
16434 }
16435
16436 *ptr++ = DW_OP_plus;
16437 gdb_assert (ptr - baton->data == baton->size);
16438
16439 SYMBOL_LOCATION_BATON (sym) = baton;
16440 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16441 }
16442
16443 /* Create appropriate locally-scoped variables for all the
16444 DW_TAG_common_block entries. Also create a struct common_block
16445 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16446 is used to separate the common blocks name namespace from regular
16447 variable names. */
16448
16449 static void
16450 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16451 {
16452 struct attribute *attr;
16453
16454 attr = dwarf2_attr (die, DW_AT_location, cu);
16455 if (attr != nullptr)
16456 {
16457 /* Support the .debug_loc offsets. */
16458 if (attr->form_is_block ())
16459 {
16460 /* Ok. */
16461 }
16462 else if (attr->form_is_section_offset ())
16463 {
16464 dwarf2_complex_location_expr_complaint ();
16465 attr = NULL;
16466 }
16467 else
16468 {
16469 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16470 "common block member");
16471 attr = NULL;
16472 }
16473 }
16474
16475 if (die->child != NULL)
16476 {
16477 struct objfile *objfile = cu->per_objfile->objfile;
16478 struct die_info *child_die;
16479 size_t n_entries = 0, size;
16480 struct common_block *common_block;
16481 struct symbol *sym;
16482
16483 for (child_die = die->child;
16484 child_die && child_die->tag;
16485 child_die = child_die->sibling)
16486 ++n_entries;
16487
16488 size = (sizeof (struct common_block)
16489 + (n_entries - 1) * sizeof (struct symbol *));
16490 common_block
16491 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16492 size);
16493 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16494 common_block->n_entries = 0;
16495
16496 for (child_die = die->child;
16497 child_die && child_die->tag;
16498 child_die = child_die->sibling)
16499 {
16500 /* Create the symbol in the DW_TAG_common_block block in the current
16501 symbol scope. */
16502 sym = new_symbol (child_die, NULL, cu);
16503 if (sym != NULL)
16504 {
16505 struct attribute *member_loc;
16506
16507 common_block->contents[common_block->n_entries++] = sym;
16508
16509 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16510 cu);
16511 if (member_loc)
16512 {
16513 /* GDB has handled this for a long time, but it is
16514 not specified by DWARF. It seems to have been
16515 emitted by gfortran at least as recently as:
16516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16517 complaint (_("Variable in common block has "
16518 "DW_AT_data_member_location "
16519 "- DIE at %s [in module %s]"),
16520 sect_offset_str (child_die->sect_off),
16521 objfile_name (objfile));
16522
16523 if (member_loc->form_is_section_offset ())
16524 dwarf2_complex_location_expr_complaint ();
16525 else if (member_loc->form_is_constant ()
16526 || member_loc->form_is_block ())
16527 {
16528 if (attr != nullptr)
16529 mark_common_block_symbol_computed (sym, die, attr,
16530 member_loc, cu);
16531 }
16532 else
16533 dwarf2_complex_location_expr_complaint ();
16534 }
16535 }
16536 }
16537
16538 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16539 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16540 }
16541 }
16542
16543 /* Create a type for a C++ namespace. */
16544
16545 static struct type *
16546 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16547 {
16548 struct objfile *objfile = cu->per_objfile->objfile;
16549 const char *previous_prefix, *name;
16550 int is_anonymous;
16551 struct type *type;
16552
16553 /* For extensions, reuse the type of the original namespace. */
16554 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16555 {
16556 struct die_info *ext_die;
16557 struct dwarf2_cu *ext_cu = cu;
16558
16559 ext_die = dwarf2_extension (die, &ext_cu);
16560 type = read_type_die (ext_die, ext_cu);
16561
16562 /* EXT_CU may not be the same as CU.
16563 Ensure TYPE is recorded with CU in die_type_hash. */
16564 return set_die_type (die, type, cu);
16565 }
16566
16567 name = namespace_name (die, &is_anonymous, cu);
16568
16569 /* Now build the name of the current namespace. */
16570
16571 previous_prefix = determine_prefix (die, cu);
16572 if (previous_prefix[0] != '\0')
16573 name = typename_concat (&objfile->objfile_obstack,
16574 previous_prefix, name, 0, cu);
16575
16576 /* Create the type. */
16577 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16578
16579 return set_die_type (die, type, cu);
16580 }
16581
16582 /* Read a namespace scope. */
16583
16584 static void
16585 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16586 {
16587 struct objfile *objfile = cu->per_objfile->objfile;
16588 int is_anonymous;
16589
16590 /* Add a symbol associated to this if we haven't seen the namespace
16591 before. Also, add a using directive if it's an anonymous
16592 namespace. */
16593
16594 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16595 {
16596 struct type *type;
16597
16598 type = read_type_die (die, cu);
16599 new_symbol (die, type, cu);
16600
16601 namespace_name (die, &is_anonymous, cu);
16602 if (is_anonymous)
16603 {
16604 const char *previous_prefix = determine_prefix (die, cu);
16605
16606 std::vector<const char *> excludes;
16607 add_using_directive (using_directives (cu),
16608 previous_prefix, type->name (), NULL,
16609 NULL, excludes, 0, &objfile->objfile_obstack);
16610 }
16611 }
16612
16613 if (die->child != NULL)
16614 {
16615 struct die_info *child_die = die->child;
16616
16617 while (child_die && child_die->tag)
16618 {
16619 process_die (child_die, cu);
16620 child_die = child_die->sibling;
16621 }
16622 }
16623 }
16624
16625 /* Read a Fortran module as type. This DIE can be only a declaration used for
16626 imported module. Still we need that type as local Fortran "use ... only"
16627 declaration imports depend on the created type in determine_prefix. */
16628
16629 static struct type *
16630 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16631 {
16632 struct objfile *objfile = cu->per_objfile->objfile;
16633 const char *module_name;
16634 struct type *type;
16635
16636 module_name = dwarf2_name (die, cu);
16637 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16638
16639 return set_die_type (die, type, cu);
16640 }
16641
16642 /* Read a Fortran module. */
16643
16644 static void
16645 read_module (struct die_info *die, struct dwarf2_cu *cu)
16646 {
16647 struct die_info *child_die = die->child;
16648 struct type *type;
16649
16650 type = read_type_die (die, cu);
16651 new_symbol (die, type, cu);
16652
16653 while (child_die && child_die->tag)
16654 {
16655 process_die (child_die, cu);
16656 child_die = child_die->sibling;
16657 }
16658 }
16659
16660 /* Return the name of the namespace represented by DIE. Set
16661 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16662 namespace. */
16663
16664 static const char *
16665 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16666 {
16667 struct die_info *current_die;
16668 const char *name = NULL;
16669
16670 /* Loop through the extensions until we find a name. */
16671
16672 for (current_die = die;
16673 current_die != NULL;
16674 current_die = dwarf2_extension (die, &cu))
16675 {
16676 /* We don't use dwarf2_name here so that we can detect the absence
16677 of a name -> anonymous namespace. */
16678 name = dwarf2_string_attr (die, DW_AT_name, cu);
16679
16680 if (name != NULL)
16681 break;
16682 }
16683
16684 /* Is it an anonymous namespace? */
16685
16686 *is_anonymous = (name == NULL);
16687 if (*is_anonymous)
16688 name = CP_ANONYMOUS_NAMESPACE_STR;
16689
16690 return name;
16691 }
16692
16693 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16694 the user defined type vector. */
16695
16696 static struct type *
16697 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16698 {
16699 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16700 struct comp_unit_head *cu_header = &cu->header;
16701 struct type *type;
16702 struct attribute *attr_byte_size;
16703 struct attribute *attr_address_class;
16704 int byte_size, addr_class;
16705 struct type *target_type;
16706
16707 target_type = die_type (die, cu);
16708
16709 /* The die_type call above may have already set the type for this DIE. */
16710 type = get_die_type (die, cu);
16711 if (type)
16712 return type;
16713
16714 type = lookup_pointer_type (target_type);
16715
16716 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16717 if (attr_byte_size)
16718 byte_size = DW_UNSND (attr_byte_size);
16719 else
16720 byte_size = cu_header->addr_size;
16721
16722 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16723 if (attr_address_class)
16724 addr_class = DW_UNSND (attr_address_class);
16725 else
16726 addr_class = DW_ADDR_none;
16727
16728 ULONGEST alignment = get_alignment (cu, die);
16729
16730 /* If the pointer size, alignment, or address class is different
16731 than the default, create a type variant marked as such and set
16732 the length accordingly. */
16733 if (TYPE_LENGTH (type) != byte_size
16734 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16735 && alignment != TYPE_RAW_ALIGN (type))
16736 || addr_class != DW_ADDR_none)
16737 {
16738 if (gdbarch_address_class_type_flags_p (gdbarch))
16739 {
16740 int type_flags;
16741
16742 type_flags = gdbarch_address_class_type_flags
16743 (gdbarch, byte_size, addr_class);
16744 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16745 == 0);
16746 type = make_type_with_address_space (type, type_flags);
16747 }
16748 else if (TYPE_LENGTH (type) != byte_size)
16749 {
16750 complaint (_("invalid pointer size %d"), byte_size);
16751 }
16752 else if (TYPE_RAW_ALIGN (type) != alignment)
16753 {
16754 complaint (_("Invalid DW_AT_alignment"
16755 " - DIE at %s [in module %s]"),
16756 sect_offset_str (die->sect_off),
16757 objfile_name (cu->per_objfile->objfile));
16758 }
16759 else
16760 {
16761 /* Should we also complain about unhandled address classes? */
16762 }
16763 }
16764
16765 TYPE_LENGTH (type) = byte_size;
16766 set_type_align (type, alignment);
16767 return set_die_type (die, type, cu);
16768 }
16769
16770 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16771 the user defined type vector. */
16772
16773 static struct type *
16774 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16775 {
16776 struct type *type;
16777 struct type *to_type;
16778 struct type *domain;
16779
16780 to_type = die_type (die, cu);
16781 domain = die_containing_type (die, cu);
16782
16783 /* The calls above may have already set the type for this DIE. */
16784 type = get_die_type (die, cu);
16785 if (type)
16786 return type;
16787
16788 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16789 type = lookup_methodptr_type (to_type);
16790 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16791 {
16792 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16793
16794 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16795 to_type->fields (), to_type->num_fields (),
16796 TYPE_VARARGS (to_type));
16797 type = lookup_methodptr_type (new_type);
16798 }
16799 else
16800 type = lookup_memberptr_type (to_type, domain);
16801
16802 return set_die_type (die, type, cu);
16803 }
16804
16805 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16806 the user defined type vector. */
16807
16808 static struct type *
16809 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16810 enum type_code refcode)
16811 {
16812 struct comp_unit_head *cu_header = &cu->header;
16813 struct type *type, *target_type;
16814 struct attribute *attr;
16815
16816 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16817
16818 target_type = die_type (die, cu);
16819
16820 /* The die_type call above may have already set the type for this DIE. */
16821 type = get_die_type (die, cu);
16822 if (type)
16823 return type;
16824
16825 type = lookup_reference_type (target_type, refcode);
16826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16827 if (attr != nullptr)
16828 {
16829 TYPE_LENGTH (type) = DW_UNSND (attr);
16830 }
16831 else
16832 {
16833 TYPE_LENGTH (type) = cu_header->addr_size;
16834 }
16835 maybe_set_alignment (cu, die, type);
16836 return set_die_type (die, type, cu);
16837 }
16838
16839 /* Add the given cv-qualifiers to the element type of the array. GCC
16840 outputs DWARF type qualifiers that apply to an array, not the
16841 element type. But GDB relies on the array element type to carry
16842 the cv-qualifiers. This mimics section 6.7.3 of the C99
16843 specification. */
16844
16845 static struct type *
16846 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16847 struct type *base_type, int cnst, int voltl)
16848 {
16849 struct type *el_type, *inner_array;
16850
16851 base_type = copy_type (base_type);
16852 inner_array = base_type;
16853
16854 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16855 {
16856 TYPE_TARGET_TYPE (inner_array) =
16857 copy_type (TYPE_TARGET_TYPE (inner_array));
16858 inner_array = TYPE_TARGET_TYPE (inner_array);
16859 }
16860
16861 el_type = TYPE_TARGET_TYPE (inner_array);
16862 cnst |= TYPE_CONST (el_type);
16863 voltl |= TYPE_VOLATILE (el_type);
16864 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16865
16866 return set_die_type (die, base_type, cu);
16867 }
16868
16869 static struct type *
16870 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16871 {
16872 struct type *base_type, *cv_type;
16873
16874 base_type = die_type (die, cu);
16875
16876 /* The die_type call above may have already set the type for this DIE. */
16877 cv_type = get_die_type (die, cu);
16878 if (cv_type)
16879 return cv_type;
16880
16881 /* In case the const qualifier is applied to an array type, the element type
16882 is so qualified, not the array type (section 6.7.3 of C99). */
16883 if (base_type->code () == TYPE_CODE_ARRAY)
16884 return add_array_cv_type (die, cu, base_type, 1, 0);
16885
16886 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16887 return set_die_type (die, cv_type, cu);
16888 }
16889
16890 static struct type *
16891 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16892 {
16893 struct type *base_type, *cv_type;
16894
16895 base_type = die_type (die, cu);
16896
16897 /* The die_type call above may have already set the type for this DIE. */
16898 cv_type = get_die_type (die, cu);
16899 if (cv_type)
16900 return cv_type;
16901
16902 /* In case the volatile qualifier is applied to an array type, the
16903 element type is so qualified, not the array type (section 6.7.3
16904 of C99). */
16905 if (base_type->code () == TYPE_CODE_ARRAY)
16906 return add_array_cv_type (die, cu, base_type, 0, 1);
16907
16908 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16909 return set_die_type (die, cv_type, cu);
16910 }
16911
16912 /* Handle DW_TAG_restrict_type. */
16913
16914 static struct type *
16915 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16916 {
16917 struct type *base_type, *cv_type;
16918
16919 base_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 cv_type = get_die_type (die, cu);
16923 if (cv_type)
16924 return cv_type;
16925
16926 cv_type = make_restrict_type (base_type);
16927 return set_die_type (die, cv_type, cu);
16928 }
16929
16930 /* Handle DW_TAG_atomic_type. */
16931
16932 static struct type *
16933 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16934 {
16935 struct type *base_type, *cv_type;
16936
16937 base_type = die_type (die, cu);
16938
16939 /* The die_type call above may have already set the type for this DIE. */
16940 cv_type = get_die_type (die, cu);
16941 if (cv_type)
16942 return cv_type;
16943
16944 cv_type = make_atomic_type (base_type);
16945 return set_die_type (die, cv_type, cu);
16946 }
16947
16948 /* Extract all information from a DW_TAG_string_type DIE and add to
16949 the user defined type vector. It isn't really a user defined type,
16950 but it behaves like one, with other DIE's using an AT_user_def_type
16951 attribute to reference it. */
16952
16953 static struct type *
16954 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16955 {
16956 struct objfile *objfile = cu->per_objfile->objfile;
16957 struct gdbarch *gdbarch = objfile->arch ();
16958 struct type *type, *range_type, *index_type, *char_type;
16959 struct attribute *attr;
16960 struct dynamic_prop prop;
16961 bool length_is_constant = true;
16962 LONGEST length;
16963
16964 /* There are a couple of places where bit sizes might be made use of
16965 when parsing a DW_TAG_string_type, however, no producer that we know
16966 of make use of these. Handling bit sizes that are a multiple of the
16967 byte size is easy enough, but what about other bit sizes? Lets deal
16968 with that problem when we have to. Warn about these attributes being
16969 unsupported, then parse the type and ignore them like we always
16970 have. */
16971 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16972 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16973 {
16974 static bool warning_printed = false;
16975 if (!warning_printed)
16976 {
16977 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16978 "currently supported on DW_TAG_string_type."));
16979 warning_printed = true;
16980 }
16981 }
16982
16983 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16984 if (attr != nullptr && !attr->form_is_constant ())
16985 {
16986 /* The string length describes the location at which the length of
16987 the string can be found. The size of the length field can be
16988 specified with one of the attributes below. */
16989 struct type *prop_type;
16990 struct attribute *len
16991 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16992 if (len == nullptr)
16993 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16994 if (len != nullptr && len->form_is_constant ())
16995 {
16996 /* Pass 0 as the default as we know this attribute is constant
16997 and the default value will not be returned. */
16998 LONGEST sz = len->constant_value (0);
16999 prop_type = cu->per_objfile->int_type (sz, true);
17000 }
17001 else
17002 {
17003 /* If the size is not specified then we assume it is the size of
17004 an address on this target. */
17005 prop_type = cu->addr_sized_int_type (true);
17006 }
17007
17008 /* Convert the attribute into a dynamic property. */
17009 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17010 length = 1;
17011 else
17012 length_is_constant = false;
17013 }
17014 else if (attr != nullptr)
17015 {
17016 /* This DW_AT_string_length just contains the length with no
17017 indirection. There's no need to create a dynamic property in this
17018 case. Pass 0 for the default value as we know it will not be
17019 returned in this case. */
17020 length = attr->constant_value (0);
17021 }
17022 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17023 {
17024 /* We don't currently support non-constant byte sizes for strings. */
17025 length = attr->constant_value (1);
17026 }
17027 else
17028 {
17029 /* Use 1 as a fallback length if we have nothing else. */
17030 length = 1;
17031 }
17032
17033 index_type = objfile_type (objfile)->builtin_int;
17034 if (length_is_constant)
17035 range_type = create_static_range_type (NULL, index_type, 1, length);
17036 else
17037 {
17038 struct dynamic_prop low_bound;
17039
17040 low_bound.kind = PROP_CONST;
17041 low_bound.data.const_val = 1;
17042 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17043 }
17044 char_type = language_string_char_type (cu->language_defn, gdbarch);
17045 type = create_string_type (NULL, char_type, range_type);
17046
17047 return set_die_type (die, type, cu);
17048 }
17049
17050 /* Assuming that DIE corresponds to a function, returns nonzero
17051 if the function is prototyped. */
17052
17053 static int
17054 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17055 {
17056 struct attribute *attr;
17057
17058 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17059 if (attr && (DW_UNSND (attr) != 0))
17060 return 1;
17061
17062 /* The DWARF standard implies that the DW_AT_prototyped attribute
17063 is only meaningful for C, but the concept also extends to other
17064 languages that allow unprototyped functions (Eg: Objective C).
17065 For all other languages, assume that functions are always
17066 prototyped. */
17067 if (cu->language != language_c
17068 && cu->language != language_objc
17069 && cu->language != language_opencl)
17070 return 1;
17071
17072 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17073 prototyped and unprototyped functions; default to prototyped,
17074 since that is more common in modern code (and RealView warns
17075 about unprototyped functions). */
17076 if (producer_is_realview (cu->producer))
17077 return 1;
17078
17079 return 0;
17080 }
17081
17082 /* Handle DIES due to C code like:
17083
17084 struct foo
17085 {
17086 int (*funcp)(int a, long l);
17087 int b;
17088 };
17089
17090 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17091
17092 static struct type *
17093 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17094 {
17095 struct objfile *objfile = cu->per_objfile->objfile;
17096 struct type *type; /* Type that this function returns. */
17097 struct type *ftype; /* Function that returns above type. */
17098 struct attribute *attr;
17099
17100 type = die_type (die, cu);
17101
17102 /* The die_type call above may have already set the type for this DIE. */
17103 ftype = get_die_type (die, cu);
17104 if (ftype)
17105 return ftype;
17106
17107 ftype = lookup_function_type (type);
17108
17109 if (prototyped_function_p (die, cu))
17110 TYPE_PROTOTYPED (ftype) = 1;
17111
17112 /* Store the calling convention in the type if it's available in
17113 the subroutine die. Otherwise set the calling convention to
17114 the default value DW_CC_normal. */
17115 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17116 if (attr != nullptr
17117 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17118 TYPE_CALLING_CONVENTION (ftype)
17119 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17120 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17121 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17122 else
17123 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17124
17125 /* Record whether the function returns normally to its caller or not
17126 if the DWARF producer set that information. */
17127 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17128 if (attr && (DW_UNSND (attr) != 0))
17129 TYPE_NO_RETURN (ftype) = 1;
17130
17131 /* We need to add the subroutine type to the die immediately so
17132 we don't infinitely recurse when dealing with parameters
17133 declared as the same subroutine type. */
17134 set_die_type (die, ftype, cu);
17135
17136 if (die->child != NULL)
17137 {
17138 struct type *void_type = objfile_type (objfile)->builtin_void;
17139 struct die_info *child_die;
17140 int nparams, iparams;
17141
17142 /* Count the number of parameters.
17143 FIXME: GDB currently ignores vararg functions, but knows about
17144 vararg member functions. */
17145 nparams = 0;
17146 child_die = die->child;
17147 while (child_die && child_die->tag)
17148 {
17149 if (child_die->tag == DW_TAG_formal_parameter)
17150 nparams++;
17151 else if (child_die->tag == DW_TAG_unspecified_parameters)
17152 TYPE_VARARGS (ftype) = 1;
17153 child_die = child_die->sibling;
17154 }
17155
17156 /* Allocate storage for parameters and fill them in. */
17157 ftype->set_num_fields (nparams);
17158 ftype->set_fields
17159 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17160
17161 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17162 even if we error out during the parameters reading below. */
17163 for (iparams = 0; iparams < nparams; iparams++)
17164 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17165
17166 iparams = 0;
17167 child_die = die->child;
17168 while (child_die && child_die->tag)
17169 {
17170 if (child_die->tag == DW_TAG_formal_parameter)
17171 {
17172 struct type *arg_type;
17173
17174 /* DWARF version 2 has no clean way to discern C++
17175 static and non-static member functions. G++ helps
17176 GDB by marking the first parameter for non-static
17177 member functions (which is the this pointer) as
17178 artificial. We pass this information to
17179 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17180
17181 DWARF version 3 added DW_AT_object_pointer, which GCC
17182 4.5 does not yet generate. */
17183 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17184 if (attr != nullptr)
17185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17186 else
17187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17188 arg_type = die_type (child_die, cu);
17189
17190 /* RealView does not mark THIS as const, which the testsuite
17191 expects. GCC marks THIS as const in method definitions,
17192 but not in the class specifications (GCC PR 43053). */
17193 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17194 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17195 {
17196 int is_this = 0;
17197 struct dwarf2_cu *arg_cu = cu;
17198 const char *name = dwarf2_name (child_die, cu);
17199
17200 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17201 if (attr != nullptr)
17202 {
17203 /* If the compiler emits this, use it. */
17204 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17205 is_this = 1;
17206 }
17207 else if (name && strcmp (name, "this") == 0)
17208 /* Function definitions will have the argument names. */
17209 is_this = 1;
17210 else if (name == NULL && iparams == 0)
17211 /* Declarations may not have the names, so like
17212 elsewhere in GDB, assume an artificial first
17213 argument is "this". */
17214 is_this = 1;
17215
17216 if (is_this)
17217 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17218 arg_type, 0);
17219 }
17220
17221 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17222 iparams++;
17223 }
17224 child_die = child_die->sibling;
17225 }
17226 }
17227
17228 return ftype;
17229 }
17230
17231 static struct type *
17232 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17233 {
17234 struct objfile *objfile = cu->per_objfile->objfile;
17235 const char *name = NULL;
17236 struct type *this_type, *target_type;
17237
17238 name = dwarf2_full_name (NULL, die, cu);
17239 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17240 TYPE_TARGET_STUB (this_type) = 1;
17241 set_die_type (die, this_type, cu);
17242 target_type = die_type (die, cu);
17243 if (target_type != this_type)
17244 TYPE_TARGET_TYPE (this_type) = target_type;
17245 else
17246 {
17247 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17248 spec and cause infinite loops in GDB. */
17249 complaint (_("Self-referential DW_TAG_typedef "
17250 "- DIE at %s [in module %s]"),
17251 sect_offset_str (die->sect_off), objfile_name (objfile));
17252 TYPE_TARGET_TYPE (this_type) = NULL;
17253 }
17254 if (name == NULL)
17255 {
17256 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17257 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17258 Handle these by just returning the target type, rather than
17259 constructing an anonymous typedef type and trying to handle this
17260 elsewhere. */
17261 set_die_type (die, target_type, cu);
17262 return target_type;
17263 }
17264 return this_type;
17265 }
17266
17267 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17268 (which may be different from NAME) to the architecture back-end to allow
17269 it to guess the correct format if necessary. */
17270
17271 static struct type *
17272 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17273 const char *name_hint, enum bfd_endian byte_order)
17274 {
17275 struct gdbarch *gdbarch = objfile->arch ();
17276 const struct floatformat **format;
17277 struct type *type;
17278
17279 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17280 if (format)
17281 type = init_float_type (objfile, bits, name, format, byte_order);
17282 else
17283 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17284
17285 return type;
17286 }
17287
17288 /* Allocate an integer type of size BITS and name NAME. */
17289
17290 static struct type *
17291 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17292 int bits, int unsigned_p, const char *name)
17293 {
17294 struct type *type;
17295
17296 /* Versions of Intel's C Compiler generate an integer type called "void"
17297 instead of using DW_TAG_unspecified_type. This has been seen on
17298 at least versions 14, 17, and 18. */
17299 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17300 && strcmp (name, "void") == 0)
17301 type = objfile_type (objfile)->builtin_void;
17302 else
17303 type = init_integer_type (objfile, bits, unsigned_p, name);
17304
17305 return type;
17306 }
17307
17308 /* Initialise and return a floating point type of size BITS suitable for
17309 use as a component of a complex number. The NAME_HINT is passed through
17310 when initialising the floating point type and is the name of the complex
17311 type.
17312
17313 As DWARF doesn't currently provide an explicit name for the components
17314 of a complex number, but it can be helpful to have these components
17315 named, we try to select a suitable name based on the size of the
17316 component. */
17317 static struct type *
17318 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17319 struct objfile *objfile,
17320 int bits, const char *name_hint,
17321 enum bfd_endian byte_order)
17322 {
17323 gdbarch *gdbarch = objfile->arch ();
17324 struct type *tt = nullptr;
17325
17326 /* Try to find a suitable floating point builtin type of size BITS.
17327 We're going to use the name of this type as the name for the complex
17328 target type that we are about to create. */
17329 switch (cu->language)
17330 {
17331 case language_fortran:
17332 switch (bits)
17333 {
17334 case 32:
17335 tt = builtin_f_type (gdbarch)->builtin_real;
17336 break;
17337 case 64:
17338 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17339 break;
17340 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17341 case 128:
17342 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17343 break;
17344 }
17345 break;
17346 default:
17347 switch (bits)
17348 {
17349 case 32:
17350 tt = builtin_type (gdbarch)->builtin_float;
17351 break;
17352 case 64:
17353 tt = builtin_type (gdbarch)->builtin_double;
17354 break;
17355 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17356 case 128:
17357 tt = builtin_type (gdbarch)->builtin_long_double;
17358 break;
17359 }
17360 break;
17361 }
17362
17363 /* If the type we found doesn't match the size we were looking for, then
17364 pretend we didn't find a type at all, the complex target type we
17365 create will then be nameless. */
17366 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17367 tt = nullptr;
17368
17369 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17370 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17371 }
17372
17373 /* Find a representation of a given base type and install
17374 it in the TYPE field of the die. */
17375
17376 static struct type *
17377 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17378 {
17379 struct objfile *objfile = cu->per_objfile->objfile;
17380 struct type *type;
17381 struct attribute *attr;
17382 int encoding = 0, bits = 0;
17383 const char *name;
17384 gdbarch *arch;
17385
17386 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17387 if (attr != nullptr)
17388 encoding = DW_UNSND (attr);
17389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17390 if (attr != nullptr)
17391 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17392 name = dwarf2_name (die, cu);
17393 if (!name)
17394 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17395
17396 arch = objfile->arch ();
17397 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17398
17399 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17400 if (attr)
17401 {
17402 int endianity = DW_UNSND (attr);
17403
17404 switch (endianity)
17405 {
17406 case DW_END_big:
17407 byte_order = BFD_ENDIAN_BIG;
17408 break;
17409 case DW_END_little:
17410 byte_order = BFD_ENDIAN_LITTLE;
17411 break;
17412 default:
17413 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17414 break;
17415 }
17416 }
17417
17418 switch (encoding)
17419 {
17420 case DW_ATE_address:
17421 /* Turn DW_ATE_address into a void * pointer. */
17422 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17423 type = init_pointer_type (objfile, bits, name, type);
17424 break;
17425 case DW_ATE_boolean:
17426 type = init_boolean_type (objfile, bits, 1, name);
17427 break;
17428 case DW_ATE_complex_float:
17429 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17430 byte_order);
17431 if (type->code () == TYPE_CODE_ERROR)
17432 {
17433 if (name == nullptr)
17434 {
17435 struct obstack *obstack
17436 = &cu->per_objfile->objfile->objfile_obstack;
17437 name = obconcat (obstack, "_Complex ", type->name (),
17438 nullptr);
17439 }
17440 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17441 }
17442 else
17443 type = init_complex_type (name, type);
17444 break;
17445 case DW_ATE_decimal_float:
17446 type = init_decfloat_type (objfile, bits, name);
17447 break;
17448 case DW_ATE_float:
17449 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17450 break;
17451 case DW_ATE_signed:
17452 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17453 break;
17454 case DW_ATE_unsigned:
17455 if (cu->language == language_fortran
17456 && name
17457 && startswith (name, "character("))
17458 type = init_character_type (objfile, bits, 1, name);
17459 else
17460 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17461 break;
17462 case DW_ATE_signed_char:
17463 if (cu->language == language_ada || cu->language == language_m2
17464 || cu->language == language_pascal
17465 || cu->language == language_fortran)
17466 type = init_character_type (objfile, bits, 0, name);
17467 else
17468 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17469 break;
17470 case DW_ATE_unsigned_char:
17471 if (cu->language == language_ada || cu->language == language_m2
17472 || cu->language == language_pascal
17473 || cu->language == language_fortran
17474 || cu->language == language_rust)
17475 type = init_character_type (objfile, bits, 1, name);
17476 else
17477 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17478 break;
17479 case DW_ATE_UTF:
17480 {
17481 if (bits == 16)
17482 type = builtin_type (arch)->builtin_char16;
17483 else if (bits == 32)
17484 type = builtin_type (arch)->builtin_char32;
17485 else
17486 {
17487 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17488 bits);
17489 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17490 }
17491 return set_die_type (die, type, cu);
17492 }
17493 break;
17494
17495 default:
17496 complaint (_("unsupported DW_AT_encoding: '%s'"),
17497 dwarf_type_encoding_name (encoding));
17498 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17499 break;
17500 }
17501
17502 if (name && strcmp (name, "char") == 0)
17503 TYPE_NOSIGN (type) = 1;
17504
17505 maybe_set_alignment (cu, die, type);
17506
17507 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17508
17509 return set_die_type (die, type, cu);
17510 }
17511
17512 /* Parse dwarf attribute if it's a block, reference or constant and put the
17513 resulting value of the attribute into struct bound_prop.
17514 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17515
17516 static int
17517 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17518 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17519 struct type *default_type)
17520 {
17521 struct dwarf2_property_baton *baton;
17522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17523 struct objfile *objfile = per_objfile->objfile;
17524 struct obstack *obstack = &objfile->objfile_obstack;
17525
17526 gdb_assert (default_type != NULL);
17527
17528 if (attr == NULL || prop == NULL)
17529 return 0;
17530
17531 if (attr->form_is_block ())
17532 {
17533 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17534 baton->property_type = default_type;
17535 baton->locexpr.per_cu = cu->per_cu;
17536 baton->locexpr.per_objfile = per_objfile;
17537 baton->locexpr.size = DW_BLOCK (attr)->size;
17538 baton->locexpr.data = DW_BLOCK (attr)->data;
17539 switch (attr->name)
17540 {
17541 case DW_AT_string_length:
17542 baton->locexpr.is_reference = true;
17543 break;
17544 default:
17545 baton->locexpr.is_reference = false;
17546 break;
17547 }
17548 prop->data.baton = baton;
17549 prop->kind = PROP_LOCEXPR;
17550 gdb_assert (prop->data.baton != NULL);
17551 }
17552 else if (attr->form_is_ref ())
17553 {
17554 struct dwarf2_cu *target_cu = cu;
17555 struct die_info *target_die;
17556 struct attribute *target_attr;
17557
17558 target_die = follow_die_ref (die, attr, &target_cu);
17559 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17560 if (target_attr == NULL)
17561 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17562 target_cu);
17563 if (target_attr == NULL)
17564 return 0;
17565
17566 switch (target_attr->name)
17567 {
17568 case DW_AT_location:
17569 if (target_attr->form_is_section_offset ())
17570 {
17571 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17572 baton->property_type = die_type (target_die, target_cu);
17573 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17574 prop->data.baton = baton;
17575 prop->kind = PROP_LOCLIST;
17576 gdb_assert (prop->data.baton != NULL);
17577 }
17578 else if (target_attr->form_is_block ())
17579 {
17580 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17581 baton->property_type = die_type (target_die, target_cu);
17582 baton->locexpr.per_cu = cu->per_cu;
17583 baton->locexpr.per_objfile = per_objfile;
17584 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17585 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17586 baton->locexpr.is_reference = true;
17587 prop->data.baton = baton;
17588 prop->kind = PROP_LOCEXPR;
17589 gdb_assert (prop->data.baton != NULL);
17590 }
17591 else
17592 {
17593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17594 "dynamic property");
17595 return 0;
17596 }
17597 break;
17598 case DW_AT_data_member_location:
17599 {
17600 LONGEST offset;
17601
17602 if (!handle_data_member_location (target_die, target_cu,
17603 &offset))
17604 return 0;
17605
17606 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17607 baton->property_type = read_type_die (target_die->parent,
17608 target_cu);
17609 baton->offset_info.offset = offset;
17610 baton->offset_info.type = die_type (target_die, target_cu);
17611 prop->data.baton = baton;
17612 prop->kind = PROP_ADDR_OFFSET;
17613 break;
17614 }
17615 }
17616 }
17617 else if (attr->form_is_constant ())
17618 {
17619 prop->data.const_val = attr->constant_value (0);
17620 prop->kind = PROP_CONST;
17621 }
17622 else
17623 {
17624 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17625 dwarf2_name (die, cu));
17626 return 0;
17627 }
17628
17629 return 1;
17630 }
17631
17632 /* See read.h. */
17633
17634 struct type *
17635 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17636 {
17637 struct type *int_type;
17638
17639 /* Helper macro to examine the various builtin types. */
17640 #define TRY_TYPE(F) \
17641 int_type = (unsigned_p \
17642 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17643 : objfile_type (objfile)->builtin_ ## F); \
17644 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17645 return int_type
17646
17647 TRY_TYPE (char);
17648 TRY_TYPE (short);
17649 TRY_TYPE (int);
17650 TRY_TYPE (long);
17651 TRY_TYPE (long_long);
17652
17653 #undef TRY_TYPE
17654
17655 gdb_assert_not_reached ("unable to find suitable integer type");
17656 }
17657
17658 /* See read.h. */
17659
17660 struct type *
17661 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17662 {
17663 int addr_size = this->per_cu->addr_size ();
17664 return this->per_objfile->int_type (addr_size, unsigned_p);
17665 }
17666
17667 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17668 present (which is valid) then compute the default type based on the
17669 compilation units address size. */
17670
17671 static struct type *
17672 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17673 {
17674 struct type *index_type = die_type (die, cu);
17675
17676 /* Dwarf-2 specifications explicitly allows to create subrange types
17677 without specifying a base type.
17678 In that case, the base type must be set to the type of
17679 the lower bound, upper bound or count, in that order, if any of these
17680 three attributes references an object that has a type.
17681 If no base type is found, the Dwarf-2 specifications say that
17682 a signed integer type of size equal to the size of an address should
17683 be used.
17684 For the following C code: `extern char gdb_int [];'
17685 GCC produces an empty range DIE.
17686 FIXME: muller/2010-05-28: Possible references to object for low bound,
17687 high bound or count are not yet handled by this code. */
17688 if (index_type->code () == TYPE_CODE_VOID)
17689 index_type = cu->addr_sized_int_type (false);
17690
17691 return index_type;
17692 }
17693
17694 /* Read the given DW_AT_subrange DIE. */
17695
17696 static struct type *
17697 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17698 {
17699 struct type *base_type, *orig_base_type;
17700 struct type *range_type;
17701 struct attribute *attr;
17702 struct dynamic_prop low, high;
17703 int low_default_is_valid;
17704 int high_bound_is_count = 0;
17705 const char *name;
17706 ULONGEST negative_mask;
17707
17708 orig_base_type = read_subrange_index_type (die, cu);
17709
17710 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17711 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17712 creating the range type, but we use the result of check_typedef
17713 when examining properties of the type. */
17714 base_type = check_typedef (orig_base_type);
17715
17716 /* The die_type call above may have already set the type for this DIE. */
17717 range_type = get_die_type (die, cu);
17718 if (range_type)
17719 return range_type;
17720
17721 low.kind = PROP_CONST;
17722 high.kind = PROP_CONST;
17723 high.data.const_val = 0;
17724
17725 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17726 omitting DW_AT_lower_bound. */
17727 switch (cu->language)
17728 {
17729 case language_c:
17730 case language_cplus:
17731 low.data.const_val = 0;
17732 low_default_is_valid = 1;
17733 break;
17734 case language_fortran:
17735 low.data.const_val = 1;
17736 low_default_is_valid = 1;
17737 break;
17738 case language_d:
17739 case language_objc:
17740 case language_rust:
17741 low.data.const_val = 0;
17742 low_default_is_valid = (cu->header.version >= 4);
17743 break;
17744 case language_ada:
17745 case language_m2:
17746 case language_pascal:
17747 low.data.const_val = 1;
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 default:
17751 low.data.const_val = 0;
17752 low_default_is_valid = 0;
17753 break;
17754 }
17755
17756 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17757 if (attr != nullptr)
17758 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17759 else if (!low_default_is_valid)
17760 complaint (_("Missing DW_AT_lower_bound "
17761 "- DIE at %s [in module %s]"),
17762 sect_offset_str (die->sect_off),
17763 objfile_name (cu->per_objfile->objfile));
17764
17765 struct attribute *attr_ub, *attr_count;
17766 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17767 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17768 {
17769 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17770 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17771 {
17772 /* If bounds are constant do the final calculation here. */
17773 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17774 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17775 else
17776 high_bound_is_count = 1;
17777 }
17778 else
17779 {
17780 if (attr_ub != NULL)
17781 complaint (_("Unresolved DW_AT_upper_bound "
17782 "- DIE at %s [in module %s]"),
17783 sect_offset_str (die->sect_off),
17784 objfile_name (cu->per_objfile->objfile));
17785 if (attr_count != NULL)
17786 complaint (_("Unresolved DW_AT_count "
17787 "- DIE at %s [in module %s]"),
17788 sect_offset_str (die->sect_off),
17789 objfile_name (cu->per_objfile->objfile));
17790 }
17791 }
17792
17793 LONGEST bias = 0;
17794 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17795 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17796 bias = bias_attr->constant_value (0);
17797
17798 /* Normally, the DWARF producers are expected to use a signed
17799 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17800 But this is unfortunately not always the case, as witnessed
17801 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17802 is used instead. To work around that ambiguity, we treat
17803 the bounds as signed, and thus sign-extend their values, when
17804 the base type is signed. */
17805 negative_mask =
17806 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17807 if (low.kind == PROP_CONST
17808 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17809 low.data.const_val |= negative_mask;
17810 if (high.kind == PROP_CONST
17811 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17812 high.data.const_val |= negative_mask;
17813
17814 /* Check for bit and byte strides. */
17815 struct dynamic_prop byte_stride_prop;
17816 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17817 if (attr_byte_stride != nullptr)
17818 {
17819 struct type *prop_type = cu->addr_sized_int_type (false);
17820 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17821 prop_type);
17822 }
17823
17824 struct dynamic_prop bit_stride_prop;
17825 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17826 if (attr_bit_stride != nullptr)
17827 {
17828 /* It only makes sense to have either a bit or byte stride. */
17829 if (attr_byte_stride != nullptr)
17830 {
17831 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17832 "- DIE at %s [in module %s]"),
17833 sect_offset_str (die->sect_off),
17834 objfile_name (cu->per_objfile->objfile));
17835 attr_bit_stride = nullptr;
17836 }
17837 else
17838 {
17839 struct type *prop_type = cu->addr_sized_int_type (false);
17840 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17841 prop_type);
17842 }
17843 }
17844
17845 if (attr_byte_stride != nullptr
17846 || attr_bit_stride != nullptr)
17847 {
17848 bool byte_stride_p = (attr_byte_stride != nullptr);
17849 struct dynamic_prop *stride
17850 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17851
17852 range_type
17853 = create_range_type_with_stride (NULL, orig_base_type, &low,
17854 &high, bias, stride, byte_stride_p);
17855 }
17856 else
17857 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17858
17859 if (high_bound_is_count)
17860 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17861
17862 /* Ada expects an empty array on no boundary attributes. */
17863 if (attr == NULL && cu->language != language_ada)
17864 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17865
17866 name = dwarf2_name (die, cu);
17867 if (name)
17868 range_type->set_name (name);
17869
17870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17871 if (attr != nullptr)
17872 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17873
17874 maybe_set_alignment (cu, die, range_type);
17875
17876 set_die_type (die, range_type, cu);
17877
17878 /* set_die_type should be already done. */
17879 set_descriptive_type (range_type, die, cu);
17880
17881 return range_type;
17882 }
17883
17884 static struct type *
17885 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17886 {
17887 struct type *type;
17888
17889 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17890 type->set_name (dwarf2_name (die, cu));
17891
17892 /* In Ada, an unspecified type is typically used when the description
17893 of the type is deferred to a different unit. When encountering
17894 such a type, we treat it as a stub, and try to resolve it later on,
17895 when needed. */
17896 if (cu->language == language_ada)
17897 TYPE_STUB (type) = 1;
17898
17899 return set_die_type (die, type, cu);
17900 }
17901
17902 /* Read a single die and all its descendents. Set the die's sibling
17903 field to NULL; set other fields in the die correctly, and set all
17904 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17905 location of the info_ptr after reading all of those dies. PARENT
17906 is the parent of the die in question. */
17907
17908 static struct die_info *
17909 read_die_and_children (const struct die_reader_specs *reader,
17910 const gdb_byte *info_ptr,
17911 const gdb_byte **new_info_ptr,
17912 struct die_info *parent)
17913 {
17914 struct die_info *die;
17915 const gdb_byte *cur_ptr;
17916
17917 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17918 if (die == NULL)
17919 {
17920 *new_info_ptr = cur_ptr;
17921 return NULL;
17922 }
17923 store_in_ref_table (die, reader->cu);
17924
17925 if (die->has_children)
17926 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17927 else
17928 {
17929 die->child = NULL;
17930 *new_info_ptr = cur_ptr;
17931 }
17932
17933 die->sibling = NULL;
17934 die->parent = parent;
17935 return die;
17936 }
17937
17938 /* Read a die, all of its descendents, and all of its siblings; set
17939 all of the fields of all of the dies correctly. Arguments are as
17940 in read_die_and_children. */
17941
17942 static struct die_info *
17943 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17944 const gdb_byte *info_ptr,
17945 const gdb_byte **new_info_ptr,
17946 struct die_info *parent)
17947 {
17948 struct die_info *first_die, *last_sibling;
17949 const gdb_byte *cur_ptr;
17950
17951 cur_ptr = info_ptr;
17952 first_die = last_sibling = NULL;
17953
17954 while (1)
17955 {
17956 struct die_info *die
17957 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17958
17959 if (die == NULL)
17960 {
17961 *new_info_ptr = cur_ptr;
17962 return first_die;
17963 }
17964
17965 if (!first_die)
17966 first_die = die;
17967 else
17968 last_sibling->sibling = die;
17969
17970 last_sibling = die;
17971 }
17972 }
17973
17974 /* Read a die, all of its descendents, and all of its siblings; set
17975 all of the fields of all of the dies correctly. Arguments are as
17976 in read_die_and_children.
17977 This the main entry point for reading a DIE and all its children. */
17978
17979 static struct die_info *
17980 read_die_and_siblings (const struct die_reader_specs *reader,
17981 const gdb_byte *info_ptr,
17982 const gdb_byte **new_info_ptr,
17983 struct die_info *parent)
17984 {
17985 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17986 new_info_ptr, parent);
17987
17988 if (dwarf_die_debug)
17989 {
17990 fprintf_unfiltered (gdb_stdlog,
17991 "Read die from %s@0x%x of %s:\n",
17992 reader->die_section->get_name (),
17993 (unsigned) (info_ptr - reader->die_section->buffer),
17994 bfd_get_filename (reader->abfd));
17995 dump_die (die, dwarf_die_debug);
17996 }
17997
17998 return die;
17999 }
18000
18001 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18002 attributes.
18003 The caller is responsible for filling in the extra attributes
18004 and updating (*DIEP)->num_attrs.
18005 Set DIEP to point to a newly allocated die with its information,
18006 except for its child, sibling, and parent fields. */
18007
18008 static const gdb_byte *
18009 read_full_die_1 (const struct die_reader_specs *reader,
18010 struct die_info **diep, const gdb_byte *info_ptr,
18011 int num_extra_attrs)
18012 {
18013 unsigned int abbrev_number, bytes_read, i;
18014 struct abbrev_info *abbrev;
18015 struct die_info *die;
18016 struct dwarf2_cu *cu = reader->cu;
18017 bfd *abfd = reader->abfd;
18018
18019 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18020 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18021 info_ptr += bytes_read;
18022 if (!abbrev_number)
18023 {
18024 *diep = NULL;
18025 return info_ptr;
18026 }
18027
18028 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18029 if (!abbrev)
18030 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18031 abbrev_number,
18032 bfd_get_filename (abfd));
18033
18034 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18035 die->sect_off = sect_off;
18036 die->tag = abbrev->tag;
18037 die->abbrev = abbrev_number;
18038 die->has_children = abbrev->has_children;
18039
18040 /* Make the result usable.
18041 The caller needs to update num_attrs after adding the extra
18042 attributes. */
18043 die->num_attrs = abbrev->num_attrs;
18044
18045 std::vector<int> indexes_that_need_reprocess;
18046 for (i = 0; i < abbrev->num_attrs; ++i)
18047 {
18048 bool need_reprocess;
18049 info_ptr =
18050 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18051 info_ptr, &need_reprocess);
18052 if (need_reprocess)
18053 indexes_that_need_reprocess.push_back (i);
18054 }
18055
18056 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18057 if (attr != nullptr)
18058 cu->str_offsets_base = DW_UNSND (attr);
18059
18060 attr = die->attr (DW_AT_loclists_base);
18061 if (attr != nullptr)
18062 cu->loclist_base = DW_UNSND (attr);
18063
18064 auto maybe_addr_base = die->addr_base ();
18065 if (maybe_addr_base.has_value ())
18066 cu->addr_base = *maybe_addr_base;
18067 for (int index : indexes_that_need_reprocess)
18068 read_attribute_reprocess (reader, &die->attrs[index]);
18069 *diep = die;
18070 return info_ptr;
18071 }
18072
18073 /* Read a die and all its attributes.
18074 Set DIEP to point to a newly allocated die with its information,
18075 except for its child, sibling, and parent fields. */
18076
18077 static const gdb_byte *
18078 read_full_die (const struct die_reader_specs *reader,
18079 struct die_info **diep, const gdb_byte *info_ptr)
18080 {
18081 const gdb_byte *result;
18082
18083 result = read_full_die_1 (reader, diep, info_ptr, 0);
18084
18085 if (dwarf_die_debug)
18086 {
18087 fprintf_unfiltered (gdb_stdlog,
18088 "Read die from %s@0x%x of %s:\n",
18089 reader->die_section->get_name (),
18090 (unsigned) (info_ptr - reader->die_section->buffer),
18091 bfd_get_filename (reader->abfd));
18092 dump_die (*diep, dwarf_die_debug);
18093 }
18094
18095 return result;
18096 }
18097 \f
18098
18099 /* Returns nonzero if TAG represents a type that we might generate a partial
18100 symbol for. */
18101
18102 static int
18103 is_type_tag_for_partial (int tag)
18104 {
18105 switch (tag)
18106 {
18107 #if 0
18108 /* Some types that would be reasonable to generate partial symbols for,
18109 that we don't at present. */
18110 case DW_TAG_array_type:
18111 case DW_TAG_file_type:
18112 case DW_TAG_ptr_to_member_type:
18113 case DW_TAG_set_type:
18114 case DW_TAG_string_type:
18115 case DW_TAG_subroutine_type:
18116 #endif
18117 case DW_TAG_base_type:
18118 case DW_TAG_class_type:
18119 case DW_TAG_interface_type:
18120 case DW_TAG_enumeration_type:
18121 case DW_TAG_structure_type:
18122 case DW_TAG_subrange_type:
18123 case DW_TAG_typedef:
18124 case DW_TAG_union_type:
18125 return 1;
18126 default:
18127 return 0;
18128 }
18129 }
18130
18131 /* Load all DIEs that are interesting for partial symbols into memory. */
18132
18133 static struct partial_die_info *
18134 load_partial_dies (const struct die_reader_specs *reader,
18135 const gdb_byte *info_ptr, int building_psymtab)
18136 {
18137 struct dwarf2_cu *cu = reader->cu;
18138 struct objfile *objfile = cu->per_objfile->objfile;
18139 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18140 unsigned int bytes_read;
18141 unsigned int load_all = 0;
18142 int nesting_level = 1;
18143
18144 parent_die = NULL;
18145 last_die = NULL;
18146
18147 gdb_assert (cu->per_cu != NULL);
18148 if (cu->per_cu->load_all_dies)
18149 load_all = 1;
18150
18151 cu->partial_dies
18152 = htab_create_alloc_ex (cu->header.length / 12,
18153 partial_die_hash,
18154 partial_die_eq,
18155 NULL,
18156 &cu->comp_unit_obstack,
18157 hashtab_obstack_allocate,
18158 dummy_obstack_deallocate);
18159
18160 while (1)
18161 {
18162 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18163
18164 /* A NULL abbrev means the end of a series of children. */
18165 if (abbrev == NULL)
18166 {
18167 if (--nesting_level == 0)
18168 return first_die;
18169
18170 info_ptr += bytes_read;
18171 last_die = parent_die;
18172 parent_die = parent_die->die_parent;
18173 continue;
18174 }
18175
18176 /* Check for template arguments. We never save these; if
18177 they're seen, we just mark the parent, and go on our way. */
18178 if (parent_die != NULL
18179 && cu->language == language_cplus
18180 && (abbrev->tag == DW_TAG_template_type_param
18181 || abbrev->tag == DW_TAG_template_value_param))
18182 {
18183 parent_die->has_template_arguments = 1;
18184
18185 if (!load_all)
18186 {
18187 /* We don't need a partial DIE for the template argument. */
18188 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18189 continue;
18190 }
18191 }
18192
18193 /* We only recurse into c++ subprograms looking for template arguments.
18194 Skip their other children. */
18195 if (!load_all
18196 && cu->language == language_cplus
18197 && parent_die != NULL
18198 && parent_die->tag == DW_TAG_subprogram)
18199 {
18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18201 continue;
18202 }
18203
18204 /* Check whether this DIE is interesting enough to save. Normally
18205 we would not be interested in members here, but there may be
18206 later variables referencing them via DW_AT_specification (for
18207 static members). */
18208 if (!load_all
18209 && !is_type_tag_for_partial (abbrev->tag)
18210 && abbrev->tag != DW_TAG_constant
18211 && abbrev->tag != DW_TAG_enumerator
18212 && abbrev->tag != DW_TAG_subprogram
18213 && abbrev->tag != DW_TAG_inlined_subroutine
18214 && abbrev->tag != DW_TAG_lexical_block
18215 && abbrev->tag != DW_TAG_variable
18216 && abbrev->tag != DW_TAG_namespace
18217 && abbrev->tag != DW_TAG_module
18218 && abbrev->tag != DW_TAG_member
18219 && abbrev->tag != DW_TAG_imported_unit
18220 && abbrev->tag != DW_TAG_imported_declaration)
18221 {
18222 /* Otherwise we skip to the next sibling, if any. */
18223 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18224 continue;
18225 }
18226
18227 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18228 abbrev);
18229
18230 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18231
18232 /* This two-pass algorithm for processing partial symbols has a
18233 high cost in cache pressure. Thus, handle some simple cases
18234 here which cover the majority of C partial symbols. DIEs
18235 which neither have specification tags in them, nor could have
18236 specification tags elsewhere pointing at them, can simply be
18237 processed and discarded.
18238
18239 This segment is also optional; scan_partial_symbols and
18240 add_partial_symbol will handle these DIEs if we chain
18241 them in normally. When compilers which do not emit large
18242 quantities of duplicate debug information are more common,
18243 this code can probably be removed. */
18244
18245 /* Any complete simple types at the top level (pretty much all
18246 of them, for a language without namespaces), can be processed
18247 directly. */
18248 if (parent_die == NULL
18249 && pdi.has_specification == 0
18250 && pdi.is_declaration == 0
18251 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18252 || pdi.tag == DW_TAG_base_type
18253 || pdi.tag == DW_TAG_subrange_type))
18254 {
18255 if (building_psymtab && pdi.name != NULL)
18256 add_psymbol_to_list (pdi.name, false,
18257 VAR_DOMAIN, LOC_TYPEDEF, -1,
18258 psymbol_placement::STATIC,
18259 0, cu->language, objfile);
18260 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18261 continue;
18262 }
18263
18264 /* The exception for DW_TAG_typedef with has_children above is
18265 a workaround of GCC PR debug/47510. In the case of this complaint
18266 type_name_or_error will error on such types later.
18267
18268 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18269 it could not find the child DIEs referenced later, this is checked
18270 above. In correct DWARF DW_TAG_typedef should have no children. */
18271
18272 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18273 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18274 "- DIE at %s [in module %s]"),
18275 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18276
18277 /* If we're at the second level, and we're an enumerator, and
18278 our parent has no specification (meaning possibly lives in a
18279 namespace elsewhere), then we can add the partial symbol now
18280 instead of queueing it. */
18281 if (pdi.tag == DW_TAG_enumerator
18282 && parent_die != NULL
18283 && parent_die->die_parent == NULL
18284 && parent_die->tag == DW_TAG_enumeration_type
18285 && parent_die->has_specification == 0)
18286 {
18287 if (pdi.name == NULL)
18288 complaint (_("malformed enumerator DIE ignored"));
18289 else if (building_psymtab)
18290 add_psymbol_to_list (pdi.name, false,
18291 VAR_DOMAIN, LOC_CONST, -1,
18292 cu->language == language_cplus
18293 ? psymbol_placement::GLOBAL
18294 : psymbol_placement::STATIC,
18295 0, cu->language, objfile);
18296
18297 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18298 continue;
18299 }
18300
18301 struct partial_die_info *part_die
18302 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18303
18304 /* We'll save this DIE so link it in. */
18305 part_die->die_parent = parent_die;
18306 part_die->die_sibling = NULL;
18307 part_die->die_child = NULL;
18308
18309 if (last_die && last_die == parent_die)
18310 last_die->die_child = part_die;
18311 else if (last_die)
18312 last_die->die_sibling = part_die;
18313
18314 last_die = part_die;
18315
18316 if (first_die == NULL)
18317 first_die = part_die;
18318
18319 /* Maybe add the DIE to the hash table. Not all DIEs that we
18320 find interesting need to be in the hash table, because we
18321 also have the parent/sibling/child chains; only those that we
18322 might refer to by offset later during partial symbol reading.
18323
18324 For now this means things that might have be the target of a
18325 DW_AT_specification, DW_AT_abstract_origin, or
18326 DW_AT_extension. DW_AT_extension will refer only to
18327 namespaces; DW_AT_abstract_origin refers to functions (and
18328 many things under the function DIE, but we do not recurse
18329 into function DIEs during partial symbol reading) and
18330 possibly variables as well; DW_AT_specification refers to
18331 declarations. Declarations ought to have the DW_AT_declaration
18332 flag. It happens that GCC forgets to put it in sometimes, but
18333 only for functions, not for types.
18334
18335 Adding more things than necessary to the hash table is harmless
18336 except for the performance cost. Adding too few will result in
18337 wasted time in find_partial_die, when we reread the compilation
18338 unit with load_all_dies set. */
18339
18340 if (load_all
18341 || abbrev->tag == DW_TAG_constant
18342 || abbrev->tag == DW_TAG_subprogram
18343 || abbrev->tag == DW_TAG_variable
18344 || abbrev->tag == DW_TAG_namespace
18345 || part_die->is_declaration)
18346 {
18347 void **slot;
18348
18349 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18350 to_underlying (part_die->sect_off),
18351 INSERT);
18352 *slot = part_die;
18353 }
18354
18355 /* For some DIEs we want to follow their children (if any). For C
18356 we have no reason to follow the children of structures; for other
18357 languages we have to, so that we can get at method physnames
18358 to infer fully qualified class names, for DW_AT_specification,
18359 and for C++ template arguments. For C++, we also look one level
18360 inside functions to find template arguments (if the name of the
18361 function does not already contain the template arguments).
18362
18363 For Ada and Fortran, we need to scan the children of subprograms
18364 and lexical blocks as well because these languages allow the
18365 definition of nested entities that could be interesting for the
18366 debugger, such as nested subprograms for instance. */
18367 if (last_die->has_children
18368 && (load_all
18369 || last_die->tag == DW_TAG_namespace
18370 || last_die->tag == DW_TAG_module
18371 || last_die->tag == DW_TAG_enumeration_type
18372 || (cu->language == language_cplus
18373 && last_die->tag == DW_TAG_subprogram
18374 && (last_die->name == NULL
18375 || strchr (last_die->name, '<') == NULL))
18376 || (cu->language != language_c
18377 && (last_die->tag == DW_TAG_class_type
18378 || last_die->tag == DW_TAG_interface_type
18379 || last_die->tag == DW_TAG_structure_type
18380 || last_die->tag == DW_TAG_union_type))
18381 || ((cu->language == language_ada
18382 || cu->language == language_fortran)
18383 && (last_die->tag == DW_TAG_subprogram
18384 || last_die->tag == DW_TAG_lexical_block))))
18385 {
18386 nesting_level++;
18387 parent_die = last_die;
18388 continue;
18389 }
18390
18391 /* Otherwise we skip to the next sibling, if any. */
18392 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18393
18394 /* Back to the top, do it again. */
18395 }
18396 }
18397
18398 partial_die_info::partial_die_info (sect_offset sect_off_,
18399 struct abbrev_info *abbrev)
18400 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18401 {
18402 }
18403
18404 /* Read a minimal amount of information into the minimal die structure.
18405 INFO_PTR should point just after the initial uleb128 of a DIE. */
18406
18407 const gdb_byte *
18408 partial_die_info::read (const struct die_reader_specs *reader,
18409 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18410 {
18411 struct dwarf2_cu *cu = reader->cu;
18412 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18413 unsigned int i;
18414 int has_low_pc_attr = 0;
18415 int has_high_pc_attr = 0;
18416 int high_pc_relative = 0;
18417
18418 for (i = 0; i < abbrev.num_attrs; ++i)
18419 {
18420 attribute attr;
18421 bool need_reprocess;
18422 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18423 info_ptr, &need_reprocess);
18424 /* String and address offsets that need to do the reprocessing have
18425 already been read at this point, so there is no need to wait until
18426 the loop terminates to do the reprocessing. */
18427 if (need_reprocess)
18428 read_attribute_reprocess (reader, &attr);
18429 /* Store the data if it is of an attribute we want to keep in a
18430 partial symbol table. */
18431 switch (attr.name)
18432 {
18433 case DW_AT_name:
18434 switch (tag)
18435 {
18436 case DW_TAG_compile_unit:
18437 case DW_TAG_partial_unit:
18438 case DW_TAG_type_unit:
18439 /* Compilation units have a DW_AT_name that is a filename, not
18440 a source language identifier. */
18441 case DW_TAG_enumeration_type:
18442 case DW_TAG_enumerator:
18443 /* These tags always have simple identifiers already; no need
18444 to canonicalize them. */
18445 name = DW_STRING (&attr);
18446 break;
18447 default:
18448 {
18449 struct objfile *objfile = dwarf2_per_objfile->objfile;
18450
18451 name
18452 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18453 }
18454 break;
18455 }
18456 break;
18457 case DW_AT_linkage_name:
18458 case DW_AT_MIPS_linkage_name:
18459 /* Note that both forms of linkage name might appear. We
18460 assume they will be the same, and we only store the last
18461 one we see. */
18462 linkage_name = attr.value_as_string ();
18463 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18464 See https://github.com/rust-lang/rust/issues/32925. */
18465 if (cu->language == language_rust && linkage_name != NULL
18466 && strchr (linkage_name, '{') != NULL)
18467 linkage_name = NULL;
18468 break;
18469 case DW_AT_low_pc:
18470 has_low_pc_attr = 1;
18471 lowpc = attr.value_as_address ();
18472 break;
18473 case DW_AT_high_pc:
18474 has_high_pc_attr = 1;
18475 highpc = attr.value_as_address ();
18476 if (cu->header.version >= 4 && attr.form_is_constant ())
18477 high_pc_relative = 1;
18478 break;
18479 case DW_AT_location:
18480 /* Support the .debug_loc offsets. */
18481 if (attr.form_is_block ())
18482 {
18483 d.locdesc = DW_BLOCK (&attr);
18484 }
18485 else if (attr.form_is_section_offset ())
18486 {
18487 dwarf2_complex_location_expr_complaint ();
18488 }
18489 else
18490 {
18491 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18492 "partial symbol information");
18493 }
18494 break;
18495 case DW_AT_external:
18496 is_external = DW_UNSND (&attr);
18497 break;
18498 case DW_AT_declaration:
18499 is_declaration = DW_UNSND (&attr);
18500 break;
18501 case DW_AT_type:
18502 has_type = 1;
18503 break;
18504 case DW_AT_abstract_origin:
18505 case DW_AT_specification:
18506 case DW_AT_extension:
18507 has_specification = 1;
18508 spec_offset = attr.get_ref_die_offset ();
18509 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18510 || cu->per_cu->is_dwz);
18511 break;
18512 case DW_AT_sibling:
18513 /* Ignore absolute siblings, they might point outside of
18514 the current compile unit. */
18515 if (attr.form == DW_FORM_ref_addr)
18516 complaint (_("ignoring absolute DW_AT_sibling"));
18517 else
18518 {
18519 const gdb_byte *buffer = reader->buffer;
18520 sect_offset off = attr.get_ref_die_offset ();
18521 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18522
18523 if (sibling_ptr < info_ptr)
18524 complaint (_("DW_AT_sibling points backwards"));
18525 else if (sibling_ptr > reader->buffer_end)
18526 reader->die_section->overflow_complaint ();
18527 else
18528 sibling = sibling_ptr;
18529 }
18530 break;
18531 case DW_AT_byte_size:
18532 has_byte_size = 1;
18533 break;
18534 case DW_AT_const_value:
18535 has_const_value = 1;
18536 break;
18537 case DW_AT_calling_convention:
18538 /* DWARF doesn't provide a way to identify a program's source-level
18539 entry point. DW_AT_calling_convention attributes are only meant
18540 to describe functions' calling conventions.
18541
18542 However, because it's a necessary piece of information in
18543 Fortran, and before DWARF 4 DW_CC_program was the only
18544 piece of debugging information whose definition refers to
18545 a 'main program' at all, several compilers marked Fortran
18546 main programs with DW_CC_program --- even when those
18547 functions use the standard calling conventions.
18548
18549 Although DWARF now specifies a way to provide this
18550 information, we support this practice for backward
18551 compatibility. */
18552 if (DW_UNSND (&attr) == DW_CC_program
18553 && cu->language == language_fortran)
18554 main_subprogram = 1;
18555 break;
18556 case DW_AT_inline:
18557 if (DW_UNSND (&attr) == DW_INL_inlined
18558 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18559 may_be_inlined = 1;
18560 break;
18561
18562 case DW_AT_import:
18563 if (tag == DW_TAG_imported_unit)
18564 {
18565 d.sect_off = attr.get_ref_die_offset ();
18566 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18567 || cu->per_cu->is_dwz);
18568 }
18569 break;
18570
18571 case DW_AT_main_subprogram:
18572 main_subprogram = DW_UNSND (&attr);
18573 break;
18574
18575 case DW_AT_ranges:
18576 {
18577 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18578 but that requires a full DIE, so instead we just
18579 reimplement it. */
18580 int need_ranges_base = tag != DW_TAG_compile_unit;
18581 unsigned int ranges_offset = (DW_UNSND (&attr)
18582 + (need_ranges_base
18583 ? cu->ranges_base
18584 : 0));
18585
18586 /* Value of the DW_AT_ranges attribute is the offset in the
18587 .debug_ranges section. */
18588 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18589 nullptr))
18590 has_pc_info = 1;
18591 }
18592 break;
18593
18594 default:
18595 break;
18596 }
18597 }
18598
18599 /* For Ada, if both the name and the linkage name appear, we prefer
18600 the latter. This lets "catch exception" work better, regardless
18601 of the order in which the name and linkage name were emitted.
18602 Really, though, this is just a workaround for the fact that gdb
18603 doesn't store both the name and the linkage name. */
18604 if (cu->language == language_ada && linkage_name != nullptr)
18605 name = linkage_name;
18606
18607 if (high_pc_relative)
18608 highpc += lowpc;
18609
18610 if (has_low_pc_attr && has_high_pc_attr)
18611 {
18612 /* When using the GNU linker, .gnu.linkonce. sections are used to
18613 eliminate duplicate copies of functions and vtables and such.
18614 The linker will arbitrarily choose one and discard the others.
18615 The AT_*_pc values for such functions refer to local labels in
18616 these sections. If the section from that file was discarded, the
18617 labels are not in the output, so the relocs get a value of 0.
18618 If this is a discarded function, mark the pc bounds as invalid,
18619 so that GDB will ignore it. */
18620 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18621 {
18622 struct objfile *objfile = dwarf2_per_objfile->objfile;
18623 struct gdbarch *gdbarch = objfile->arch ();
18624
18625 complaint (_("DW_AT_low_pc %s is zero "
18626 "for DIE at %s [in module %s]"),
18627 paddress (gdbarch, lowpc),
18628 sect_offset_str (sect_off),
18629 objfile_name (objfile));
18630 }
18631 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18632 else if (lowpc >= highpc)
18633 {
18634 struct objfile *objfile = dwarf2_per_objfile->objfile;
18635 struct gdbarch *gdbarch = objfile->arch ();
18636
18637 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18638 "for DIE at %s [in module %s]"),
18639 paddress (gdbarch, lowpc),
18640 paddress (gdbarch, highpc),
18641 sect_offset_str (sect_off),
18642 objfile_name (objfile));
18643 }
18644 else
18645 has_pc_info = 1;
18646 }
18647
18648 return info_ptr;
18649 }
18650
18651 /* Find a cached partial DIE at OFFSET in CU. */
18652
18653 struct partial_die_info *
18654 dwarf2_cu::find_partial_die (sect_offset sect_off)
18655 {
18656 struct partial_die_info *lookup_die = NULL;
18657 struct partial_die_info part_die (sect_off);
18658
18659 lookup_die = ((struct partial_die_info *)
18660 htab_find_with_hash (partial_dies, &part_die,
18661 to_underlying (sect_off)));
18662
18663 return lookup_die;
18664 }
18665
18666 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18667 except in the case of .debug_types DIEs which do not reference
18668 outside their CU (they do however referencing other types via
18669 DW_FORM_ref_sig8). */
18670
18671 static const struct cu_partial_die_info
18672 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18673 {
18674 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18675 struct objfile *objfile = dwarf2_per_objfile->objfile;
18676 struct partial_die_info *pd = NULL;
18677
18678 if (offset_in_dwz == cu->per_cu->is_dwz
18679 && cu->header.offset_in_cu_p (sect_off))
18680 {
18681 pd = cu->find_partial_die (sect_off);
18682 if (pd != NULL)
18683 return { cu, pd };
18684 /* We missed recording what we needed.
18685 Load all dies and try again. */
18686 }
18687 else
18688 {
18689 /* TUs don't reference other CUs/TUs (except via type signatures). */
18690 if (cu->per_cu->is_debug_types)
18691 {
18692 error (_("Dwarf Error: Type Unit at offset %s contains"
18693 " external reference to offset %s [in module %s].\n"),
18694 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18695 bfd_get_filename (objfile->obfd));
18696 }
18697 dwarf2_per_cu_data *per_cu
18698 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18699 dwarf2_per_objfile);
18700
18701 cu = dwarf2_per_objfile->get_cu (per_cu);
18702 if (cu == NULL || cu->partial_dies == NULL)
18703 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
18704
18705 cu = dwarf2_per_objfile->get_cu (per_cu);
18706
18707 cu->last_used = 0;
18708 pd = cu->find_partial_die (sect_off);
18709 }
18710
18711 /* If we didn't find it, and not all dies have been loaded,
18712 load them all and try again. */
18713
18714 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18715 {
18716 cu->per_cu->load_all_dies = 1;
18717
18718 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18719 THIS_CU->cu may already be in use. So we can't just free it and
18720 replace its DIEs with the ones we read in. Instead, we leave those
18721 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18722 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18723 set. */
18724 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
18725
18726 pd = cu->find_partial_die (sect_off);
18727 }
18728
18729 if (pd == NULL)
18730 internal_error (__FILE__, __LINE__,
18731 _("could not find partial DIE %s "
18732 "in cache [from module %s]\n"),
18733 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18734 return { cu, pd };
18735 }
18736
18737 /* See if we can figure out if the class lives in a namespace. We do
18738 this by looking for a member function; its demangled name will
18739 contain namespace info, if there is any. */
18740
18741 static void
18742 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18743 struct dwarf2_cu *cu)
18744 {
18745 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18746 what template types look like, because the demangler
18747 frequently doesn't give the same name as the debug info. We
18748 could fix this by only using the demangled name to get the
18749 prefix (but see comment in read_structure_type). */
18750
18751 struct partial_die_info *real_pdi;
18752 struct partial_die_info *child_pdi;
18753
18754 /* If this DIE (this DIE's specification, if any) has a parent, then
18755 we should not do this. We'll prepend the parent's fully qualified
18756 name when we create the partial symbol. */
18757
18758 real_pdi = struct_pdi;
18759 while (real_pdi->has_specification)
18760 {
18761 auto res = find_partial_die (real_pdi->spec_offset,
18762 real_pdi->spec_is_dwz, cu);
18763 real_pdi = res.pdi;
18764 cu = res.cu;
18765 }
18766
18767 if (real_pdi->die_parent != NULL)
18768 return;
18769
18770 for (child_pdi = struct_pdi->die_child;
18771 child_pdi != NULL;
18772 child_pdi = child_pdi->die_sibling)
18773 {
18774 if (child_pdi->tag == DW_TAG_subprogram
18775 && child_pdi->linkage_name != NULL)
18776 {
18777 gdb::unique_xmalloc_ptr<char> actual_class_name
18778 (language_class_name_from_physname (cu->language_defn,
18779 child_pdi->linkage_name));
18780 if (actual_class_name != NULL)
18781 {
18782 struct objfile *objfile = cu->per_objfile->objfile;
18783 struct_pdi->name = objfile->intern (actual_class_name.get ());
18784 }
18785 break;
18786 }
18787 }
18788 }
18789
18790 /* Return true if a DIE with TAG may have the DW_AT_const_value
18791 attribute. */
18792
18793 static bool
18794 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18795 {
18796 switch (tag)
18797 {
18798 case DW_TAG_constant:
18799 case DW_TAG_enumerator:
18800 case DW_TAG_formal_parameter:
18801 case DW_TAG_template_value_param:
18802 case DW_TAG_variable:
18803 return true;
18804 }
18805
18806 return false;
18807 }
18808
18809 void
18810 partial_die_info::fixup (struct dwarf2_cu *cu)
18811 {
18812 /* Once we've fixed up a die, there's no point in doing so again.
18813 This also avoids a memory leak if we were to call
18814 guess_partial_die_structure_name multiple times. */
18815 if (fixup_called)
18816 return;
18817
18818 /* If we found a reference attribute and the DIE has no name, try
18819 to find a name in the referred to DIE. */
18820
18821 if (name == NULL && has_specification)
18822 {
18823 struct partial_die_info *spec_die;
18824
18825 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18826 spec_die = res.pdi;
18827 cu = res.cu;
18828
18829 spec_die->fixup (cu);
18830
18831 if (spec_die->name)
18832 {
18833 name = spec_die->name;
18834
18835 /* Copy DW_AT_external attribute if it is set. */
18836 if (spec_die->is_external)
18837 is_external = spec_die->is_external;
18838 }
18839 }
18840
18841 if (!has_const_value && has_specification
18842 && can_have_DW_AT_const_value_p (tag))
18843 {
18844 struct partial_die_info *spec_die;
18845
18846 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18847 spec_die = res.pdi;
18848 cu = res.cu;
18849
18850 spec_die->fixup (cu);
18851
18852 if (spec_die->has_const_value)
18853 {
18854 /* Copy DW_AT_const_value attribute if it is set. */
18855 has_const_value = spec_die->has_const_value;
18856 }
18857 }
18858
18859 /* Set default names for some unnamed DIEs. */
18860
18861 if (name == NULL && tag == DW_TAG_namespace)
18862 name = CP_ANONYMOUS_NAMESPACE_STR;
18863
18864 /* If there is no parent die to provide a namespace, and there are
18865 children, see if we can determine the namespace from their linkage
18866 name. */
18867 if (cu->language == language_cplus
18868 && !cu->per_objfile->per_bfd->types.empty ()
18869 && die_parent == NULL
18870 && has_children
18871 && (tag == DW_TAG_class_type
18872 || tag == DW_TAG_structure_type
18873 || tag == DW_TAG_union_type))
18874 guess_partial_die_structure_name (this, cu);
18875
18876 /* GCC might emit a nameless struct or union that has a linkage
18877 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18878 if (name == NULL
18879 && (tag == DW_TAG_class_type
18880 || tag == DW_TAG_interface_type
18881 || tag == DW_TAG_structure_type
18882 || tag == DW_TAG_union_type)
18883 && linkage_name != NULL)
18884 {
18885 gdb::unique_xmalloc_ptr<char> demangled
18886 (gdb_demangle (linkage_name, DMGL_TYPES));
18887 if (demangled != nullptr)
18888 {
18889 const char *base;
18890
18891 /* Strip any leading namespaces/classes, keep only the base name.
18892 DW_AT_name for named DIEs does not contain the prefixes. */
18893 base = strrchr (demangled.get (), ':');
18894 if (base && base > demangled.get () && base[-1] == ':')
18895 base++;
18896 else
18897 base = demangled.get ();
18898
18899 struct objfile *objfile = cu->per_objfile->objfile;
18900 name = objfile->intern (base);
18901 }
18902 }
18903
18904 fixup_called = 1;
18905 }
18906
18907 /* Read the .debug_loclists header contents from the given SECTION in the
18908 HEADER. */
18909 static void
18910 read_loclist_header (struct loclist_header *header,
18911 struct dwarf2_section_info *section)
18912 {
18913 unsigned int bytes_read;
18914 bfd *abfd = section->get_bfd_owner ();
18915 const gdb_byte *info_ptr = section->buffer;
18916 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18917 info_ptr += bytes_read;
18918 header->version = read_2_bytes (abfd, info_ptr);
18919 info_ptr += 2;
18920 header->addr_size = read_1_byte (abfd, info_ptr);
18921 info_ptr += 1;
18922 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18923 info_ptr += 1;
18924 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18925 }
18926
18927 /* Return the DW_AT_loclists_base value for the CU. */
18928 static ULONGEST
18929 lookup_loclist_base (struct dwarf2_cu *cu)
18930 {
18931 /* For the .dwo unit, the loclist_base points to the first offset following
18932 the header. The header consists of the following entities-
18933 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18934 bit format)
18935 2. version (2 bytes)
18936 3. address size (1 byte)
18937 4. segment selector size (1 byte)
18938 5. offset entry count (4 bytes)
18939 These sizes are derived as per the DWARFv5 standard. */
18940 if (cu->dwo_unit != nullptr)
18941 {
18942 if (cu->header.initial_length_size == 4)
18943 return LOCLIST_HEADER_SIZE32;
18944 return LOCLIST_HEADER_SIZE64;
18945 }
18946 return cu->loclist_base;
18947 }
18948
18949 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18950 array of offsets in the .debug_loclists section. */
18951 static CORE_ADDR
18952 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18953 {
18954 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18955 struct objfile *objfile = dwarf2_per_objfile->objfile;
18956 bfd *abfd = objfile->obfd;
18957 ULONGEST loclist_base = lookup_loclist_base (cu);
18958 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18959
18960 section->read (objfile);
18961 if (section->buffer == NULL)
18962 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18963 "section [in module %s]"), objfile_name (objfile));
18964 struct loclist_header header;
18965 read_loclist_header (&header, section);
18966 if (loclist_index >= header.offset_entry_count)
18967 complaint (_("DW_FORM_loclistx pointing outside of "
18968 ".debug_loclists offset array [in module %s]"),
18969 objfile_name (objfile));
18970 if (loclist_base + loclist_index * cu->header.offset_size
18971 >= section->size)
18972 complaint (_("DW_FORM_loclistx pointing outside of "
18973 ".debug_loclists section [in module %s]"),
18974 objfile_name (objfile));
18975 const gdb_byte *info_ptr
18976 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18977
18978 if (cu->header.offset_size == 4)
18979 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18980 else
18981 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18982 }
18983
18984 /* Process the attributes that had to be skipped in the first round. These
18985 attributes are the ones that need str_offsets_base or addr_base attributes.
18986 They could not have been processed in the first round, because at the time
18987 the values of str_offsets_base or addr_base may not have been known. */
18988 static void
18989 read_attribute_reprocess (const struct die_reader_specs *reader,
18990 struct attribute *attr)
18991 {
18992 struct dwarf2_cu *cu = reader->cu;
18993 switch (attr->form)
18994 {
18995 case DW_FORM_addrx:
18996 case DW_FORM_GNU_addr_index:
18997 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18998 break;
18999 case DW_FORM_loclistx:
19000 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19001 break;
19002 case DW_FORM_strx:
19003 case DW_FORM_strx1:
19004 case DW_FORM_strx2:
19005 case DW_FORM_strx3:
19006 case DW_FORM_strx4:
19007 case DW_FORM_GNU_str_index:
19008 {
19009 unsigned int str_index = DW_UNSND (attr);
19010 if (reader->dwo_file != NULL)
19011 {
19012 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19013 DW_STRING_IS_CANONICAL (attr) = 0;
19014 }
19015 else
19016 {
19017 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 }
19020 break;
19021 }
19022 default:
19023 gdb_assert_not_reached (_("Unexpected DWARF form."));
19024 }
19025 }
19026
19027 /* Read an attribute value described by an attribute form. */
19028
19029 static const gdb_byte *
19030 read_attribute_value (const struct die_reader_specs *reader,
19031 struct attribute *attr, unsigned form,
19032 LONGEST implicit_const, const gdb_byte *info_ptr,
19033 bool *need_reprocess)
19034 {
19035 struct dwarf2_cu *cu = reader->cu;
19036 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19037 struct objfile *objfile = dwarf2_per_objfile->objfile;
19038 bfd *abfd = reader->abfd;
19039 struct comp_unit_head *cu_header = &cu->header;
19040 unsigned int bytes_read;
19041 struct dwarf_block *blk;
19042 *need_reprocess = false;
19043
19044 attr->form = (enum dwarf_form) form;
19045 switch (form)
19046 {
19047 case DW_FORM_ref_addr:
19048 if (cu->header.version == 2)
19049 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19050 &bytes_read);
19051 else
19052 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19053 &bytes_read);
19054 info_ptr += bytes_read;
19055 break;
19056 case DW_FORM_GNU_ref_alt:
19057 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19058 info_ptr += bytes_read;
19059 break;
19060 case DW_FORM_addr:
19061 {
19062 struct gdbarch *gdbarch = objfile->arch ();
19063 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19064 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19065 info_ptr += bytes_read;
19066 }
19067 break;
19068 case DW_FORM_block2:
19069 blk = dwarf_alloc_block (cu);
19070 blk->size = read_2_bytes (abfd, info_ptr);
19071 info_ptr += 2;
19072 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19073 info_ptr += blk->size;
19074 DW_BLOCK (attr) = blk;
19075 break;
19076 case DW_FORM_block4:
19077 blk = dwarf_alloc_block (cu);
19078 blk->size = read_4_bytes (abfd, info_ptr);
19079 info_ptr += 4;
19080 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19081 info_ptr += blk->size;
19082 DW_BLOCK (attr) = blk;
19083 break;
19084 case DW_FORM_data2:
19085 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19086 info_ptr += 2;
19087 break;
19088 case DW_FORM_data4:
19089 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19090 info_ptr += 4;
19091 break;
19092 case DW_FORM_data8:
19093 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19094 info_ptr += 8;
19095 break;
19096 case DW_FORM_data16:
19097 blk = dwarf_alloc_block (cu);
19098 blk->size = 16;
19099 blk->data = read_n_bytes (abfd, info_ptr, 16);
19100 info_ptr += 16;
19101 DW_BLOCK (attr) = blk;
19102 break;
19103 case DW_FORM_sec_offset:
19104 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19105 info_ptr += bytes_read;
19106 break;
19107 case DW_FORM_loclistx:
19108 {
19109 *need_reprocess = true;
19110 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19111 info_ptr += bytes_read;
19112 }
19113 break;
19114 case DW_FORM_string:
19115 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19116 DW_STRING_IS_CANONICAL (attr) = 0;
19117 info_ptr += bytes_read;
19118 break;
19119 case DW_FORM_strp:
19120 if (!cu->per_cu->is_dwz)
19121 {
19122 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19123 abfd, info_ptr, cu_header,
19124 &bytes_read);
19125 DW_STRING_IS_CANONICAL (attr) = 0;
19126 info_ptr += bytes_read;
19127 break;
19128 }
19129 /* FALLTHROUGH */
19130 case DW_FORM_line_strp:
19131 if (!cu->per_cu->is_dwz)
19132 {
19133 DW_STRING (attr)
19134 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19135 &bytes_read);
19136 DW_STRING_IS_CANONICAL (attr) = 0;
19137 info_ptr += bytes_read;
19138 break;
19139 }
19140 /* FALLTHROUGH */
19141 case DW_FORM_GNU_strp_alt:
19142 {
19143 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19144 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19145 &bytes_read);
19146
19147 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19148 DW_STRING_IS_CANONICAL (attr) = 0;
19149 info_ptr += bytes_read;
19150 }
19151 break;
19152 case DW_FORM_exprloc:
19153 case DW_FORM_block:
19154 blk = dwarf_alloc_block (cu);
19155 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19156 info_ptr += bytes_read;
19157 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19158 info_ptr += blk->size;
19159 DW_BLOCK (attr) = blk;
19160 break;
19161 case DW_FORM_block1:
19162 blk = dwarf_alloc_block (cu);
19163 blk->size = read_1_byte (abfd, info_ptr);
19164 info_ptr += 1;
19165 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19166 info_ptr += blk->size;
19167 DW_BLOCK (attr) = blk;
19168 break;
19169 case DW_FORM_data1:
19170 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19171 info_ptr += 1;
19172 break;
19173 case DW_FORM_flag:
19174 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19175 info_ptr += 1;
19176 break;
19177 case DW_FORM_flag_present:
19178 DW_UNSND (attr) = 1;
19179 break;
19180 case DW_FORM_sdata:
19181 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19182 info_ptr += bytes_read;
19183 break;
19184 case DW_FORM_udata:
19185 case DW_FORM_rnglistx:
19186 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19187 info_ptr += bytes_read;
19188 break;
19189 case DW_FORM_ref1:
19190 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19191 + read_1_byte (abfd, info_ptr));
19192 info_ptr += 1;
19193 break;
19194 case DW_FORM_ref2:
19195 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19196 + read_2_bytes (abfd, info_ptr));
19197 info_ptr += 2;
19198 break;
19199 case DW_FORM_ref4:
19200 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19201 + read_4_bytes (abfd, info_ptr));
19202 info_ptr += 4;
19203 break;
19204 case DW_FORM_ref8:
19205 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19206 + read_8_bytes (abfd, info_ptr));
19207 info_ptr += 8;
19208 break;
19209 case DW_FORM_ref_sig8:
19210 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19211 info_ptr += 8;
19212 break;
19213 case DW_FORM_ref_udata:
19214 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19215 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19216 info_ptr += bytes_read;
19217 break;
19218 case DW_FORM_indirect:
19219 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19220 info_ptr += bytes_read;
19221 if (form == DW_FORM_implicit_const)
19222 {
19223 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19224 info_ptr += bytes_read;
19225 }
19226 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19227 info_ptr, need_reprocess);
19228 break;
19229 case DW_FORM_implicit_const:
19230 DW_SND (attr) = implicit_const;
19231 break;
19232 case DW_FORM_addrx:
19233 case DW_FORM_GNU_addr_index:
19234 *need_reprocess = true;
19235 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19236 info_ptr += bytes_read;
19237 break;
19238 case DW_FORM_strx:
19239 case DW_FORM_strx1:
19240 case DW_FORM_strx2:
19241 case DW_FORM_strx3:
19242 case DW_FORM_strx4:
19243 case DW_FORM_GNU_str_index:
19244 {
19245 ULONGEST str_index;
19246 if (form == DW_FORM_strx1)
19247 {
19248 str_index = read_1_byte (abfd, info_ptr);
19249 info_ptr += 1;
19250 }
19251 else if (form == DW_FORM_strx2)
19252 {
19253 str_index = read_2_bytes (abfd, info_ptr);
19254 info_ptr += 2;
19255 }
19256 else if (form == DW_FORM_strx3)
19257 {
19258 str_index = read_3_bytes (abfd, info_ptr);
19259 info_ptr += 3;
19260 }
19261 else if (form == DW_FORM_strx4)
19262 {
19263 str_index = read_4_bytes (abfd, info_ptr);
19264 info_ptr += 4;
19265 }
19266 else
19267 {
19268 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19269 info_ptr += bytes_read;
19270 }
19271 *need_reprocess = true;
19272 DW_UNSND (attr) = str_index;
19273 }
19274 break;
19275 default:
19276 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19277 dwarf_form_name (form),
19278 bfd_get_filename (abfd));
19279 }
19280
19281 /* Super hack. */
19282 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19283 attr->form = DW_FORM_GNU_ref_alt;
19284
19285 /* We have seen instances where the compiler tried to emit a byte
19286 size attribute of -1 which ended up being encoded as an unsigned
19287 0xffffffff. Although 0xffffffff is technically a valid size value,
19288 an object of this size seems pretty unlikely so we can relatively
19289 safely treat these cases as if the size attribute was invalid and
19290 treat them as zero by default. */
19291 if (attr->name == DW_AT_byte_size
19292 && form == DW_FORM_data4
19293 && DW_UNSND (attr) >= 0xffffffff)
19294 {
19295 complaint
19296 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19297 hex_string (DW_UNSND (attr)));
19298 DW_UNSND (attr) = 0;
19299 }
19300
19301 return info_ptr;
19302 }
19303
19304 /* Read an attribute described by an abbreviated attribute. */
19305
19306 static const gdb_byte *
19307 read_attribute (const struct die_reader_specs *reader,
19308 struct attribute *attr, struct attr_abbrev *abbrev,
19309 const gdb_byte *info_ptr, bool *need_reprocess)
19310 {
19311 attr->name = abbrev->name;
19312 return read_attribute_value (reader, attr, abbrev->form,
19313 abbrev->implicit_const, info_ptr,
19314 need_reprocess);
19315 }
19316
19317 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19318
19319 static const char *
19320 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19321 LONGEST str_offset)
19322 {
19323 return dwarf2_per_objfile->per_bfd->str.read_string
19324 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19325 }
19326
19327 /* Return pointer to string at .debug_str offset as read from BUF.
19328 BUF is assumed to be in a compilation unit described by CU_HEADER.
19329 Return *BYTES_READ_PTR count of bytes read from BUF. */
19330
19331 static const char *
19332 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19333 const gdb_byte *buf,
19334 const struct comp_unit_head *cu_header,
19335 unsigned int *bytes_read_ptr)
19336 {
19337 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19338
19339 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19340 }
19341
19342 /* See read.h. */
19343
19344 const char *
19345 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19346 const struct comp_unit_head *cu_header,
19347 unsigned int *bytes_read_ptr)
19348 {
19349 bfd *abfd = objfile->obfd;
19350 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19351
19352 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19353 }
19354
19355 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19356 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19357 ADDR_SIZE is the size of addresses from the CU header. */
19358
19359 static CORE_ADDR
19360 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19361 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19362 int addr_size)
19363 {
19364 struct objfile *objfile = dwarf2_per_objfile->objfile;
19365 bfd *abfd = objfile->obfd;
19366 const gdb_byte *info_ptr;
19367 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19368
19369 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19370 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19371 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19372 objfile_name (objfile));
19373 if (addr_base_or_zero + addr_index * addr_size
19374 >= dwarf2_per_objfile->per_bfd->addr.size)
19375 error (_("DW_FORM_addr_index pointing outside of "
19376 ".debug_addr section [in module %s]"),
19377 objfile_name (objfile));
19378 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19379 + addr_base_or_zero + addr_index * addr_size);
19380 if (addr_size == 4)
19381 return bfd_get_32 (abfd, info_ptr);
19382 else
19383 return bfd_get_64 (abfd, info_ptr);
19384 }
19385
19386 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19387
19388 static CORE_ADDR
19389 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19390 {
19391 return read_addr_index_1 (cu->per_objfile, addr_index,
19392 cu->addr_base, cu->header.addr_size);
19393 }
19394
19395 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19396
19397 static CORE_ADDR
19398 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19399 unsigned int *bytes_read)
19400 {
19401 bfd *abfd = cu->per_objfile->objfile->obfd;
19402 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19403
19404 return read_addr_index (cu, addr_index);
19405 }
19406
19407 /* See read.h. */
19408
19409 CORE_ADDR
19410 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19411 dwarf2_per_objfile *dwarf2_per_objfile,
19412 unsigned int addr_index)
19413 {
19414 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
19415 gdb::optional<ULONGEST> addr_base;
19416 int addr_size;
19417
19418 /* We need addr_base and addr_size.
19419 If we don't have PER_CU->cu, we have to get it.
19420 Nasty, but the alternative is storing the needed info in PER_CU,
19421 which at this point doesn't seem justified: it's not clear how frequently
19422 it would get used and it would increase the size of every PER_CU.
19423 Entry points like dwarf2_per_cu_addr_size do a similar thing
19424 so we're not in uncharted territory here.
19425 Alas we need to be a bit more complicated as addr_base is contained
19426 in the DIE.
19427
19428 We don't need to read the entire CU(/TU).
19429 We just need the header and top level die.
19430
19431 IWBN to use the aging mechanism to let us lazily later discard the CU.
19432 For now we skip this optimization. */
19433
19434 if (cu != NULL)
19435 {
19436 addr_base = cu->addr_base;
19437 addr_size = cu->header.addr_size;
19438 }
19439 else
19440 {
19441 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
19442 addr_base = reader.cu->addr_base;
19443 addr_size = reader.cu->header.addr_size;
19444 }
19445
19446 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19447 addr_size);
19448 }
19449
19450 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19451 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19452 DWO file. */
19453
19454 static const char *
19455 read_str_index (struct dwarf2_cu *cu,
19456 struct dwarf2_section_info *str_section,
19457 struct dwarf2_section_info *str_offsets_section,
19458 ULONGEST str_offsets_base, ULONGEST str_index)
19459 {
19460 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19461 struct objfile *objfile = dwarf2_per_objfile->objfile;
19462 const char *objf_name = objfile_name (objfile);
19463 bfd *abfd = objfile->obfd;
19464 const gdb_byte *info_ptr;
19465 ULONGEST str_offset;
19466 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19467
19468 str_section->read (objfile);
19469 str_offsets_section->read (objfile);
19470 if (str_section->buffer == NULL)
19471 error (_("%s used without %s section"
19472 " in CU at offset %s [in module %s]"),
19473 form_name, str_section->get_name (),
19474 sect_offset_str (cu->header.sect_off), objf_name);
19475 if (str_offsets_section->buffer == NULL)
19476 error (_("%s used without %s section"
19477 " in CU at offset %s [in module %s]"),
19478 form_name, str_section->get_name (),
19479 sect_offset_str (cu->header.sect_off), objf_name);
19480 info_ptr = (str_offsets_section->buffer
19481 + str_offsets_base
19482 + str_index * cu->header.offset_size);
19483 if (cu->header.offset_size == 4)
19484 str_offset = bfd_get_32 (abfd, info_ptr);
19485 else
19486 str_offset = bfd_get_64 (abfd, info_ptr);
19487 if (str_offset >= str_section->size)
19488 error (_("Offset from %s pointing outside of"
19489 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19490 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19491 return (const char *) (str_section->buffer + str_offset);
19492 }
19493
19494 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19495
19496 static const char *
19497 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19498 {
19499 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19500 ? reader->cu->header.addr_size : 0;
19501 return read_str_index (reader->cu,
19502 &reader->dwo_file->sections.str,
19503 &reader->dwo_file->sections.str_offsets,
19504 str_offsets_base, str_index);
19505 }
19506
19507 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19508
19509 static const char *
19510 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19511 {
19512 struct objfile *objfile = cu->per_objfile->objfile;
19513 const char *objf_name = objfile_name (objfile);
19514 static const char form_name[] = "DW_FORM_GNU_str_index";
19515 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19516
19517 if (!cu->str_offsets_base.has_value ())
19518 error (_("%s used in Fission stub without %s"
19519 " in CU at offset 0x%lx [in module %s]"),
19520 form_name, str_offsets_attr_name,
19521 (long) cu->header.offset_size, objf_name);
19522
19523 return read_str_index (cu,
19524 &cu->per_objfile->per_bfd->str,
19525 &cu->per_objfile->per_bfd->str_offsets,
19526 *cu->str_offsets_base, str_index);
19527 }
19528
19529 /* Return the length of an LEB128 number in BUF. */
19530
19531 static int
19532 leb128_size (const gdb_byte *buf)
19533 {
19534 const gdb_byte *begin = buf;
19535 gdb_byte byte;
19536
19537 while (1)
19538 {
19539 byte = *buf++;
19540 if ((byte & 128) == 0)
19541 return buf - begin;
19542 }
19543 }
19544
19545 static void
19546 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19547 {
19548 switch (lang)
19549 {
19550 case DW_LANG_C89:
19551 case DW_LANG_C99:
19552 case DW_LANG_C11:
19553 case DW_LANG_C:
19554 case DW_LANG_UPC:
19555 cu->language = language_c;
19556 break;
19557 case DW_LANG_Java:
19558 case DW_LANG_C_plus_plus:
19559 case DW_LANG_C_plus_plus_11:
19560 case DW_LANG_C_plus_plus_14:
19561 cu->language = language_cplus;
19562 break;
19563 case DW_LANG_D:
19564 cu->language = language_d;
19565 break;
19566 case DW_LANG_Fortran77:
19567 case DW_LANG_Fortran90:
19568 case DW_LANG_Fortran95:
19569 case DW_LANG_Fortran03:
19570 case DW_LANG_Fortran08:
19571 cu->language = language_fortran;
19572 break;
19573 case DW_LANG_Go:
19574 cu->language = language_go;
19575 break;
19576 case DW_LANG_Mips_Assembler:
19577 cu->language = language_asm;
19578 break;
19579 case DW_LANG_Ada83:
19580 case DW_LANG_Ada95:
19581 cu->language = language_ada;
19582 break;
19583 case DW_LANG_Modula2:
19584 cu->language = language_m2;
19585 break;
19586 case DW_LANG_Pascal83:
19587 cu->language = language_pascal;
19588 break;
19589 case DW_LANG_ObjC:
19590 cu->language = language_objc;
19591 break;
19592 case DW_LANG_Rust:
19593 case DW_LANG_Rust_old:
19594 cu->language = language_rust;
19595 break;
19596 case DW_LANG_Cobol74:
19597 case DW_LANG_Cobol85:
19598 default:
19599 cu->language = language_minimal;
19600 break;
19601 }
19602 cu->language_defn = language_def (cu->language);
19603 }
19604
19605 /* Return the named attribute or NULL if not there. */
19606
19607 static struct attribute *
19608 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19609 {
19610 for (;;)
19611 {
19612 unsigned int i;
19613 struct attribute *spec = NULL;
19614
19615 for (i = 0; i < die->num_attrs; ++i)
19616 {
19617 if (die->attrs[i].name == name)
19618 return &die->attrs[i];
19619 if (die->attrs[i].name == DW_AT_specification
19620 || die->attrs[i].name == DW_AT_abstract_origin)
19621 spec = &die->attrs[i];
19622 }
19623
19624 if (!spec)
19625 break;
19626
19627 die = follow_die_ref (die, spec, &cu);
19628 }
19629
19630 return NULL;
19631 }
19632
19633 /* Return the string associated with a string-typed attribute, or NULL if it
19634 is either not found or is of an incorrect type. */
19635
19636 static const char *
19637 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19638 {
19639 struct attribute *attr;
19640 const char *str = NULL;
19641
19642 attr = dwarf2_attr (die, name, cu);
19643
19644 if (attr != NULL)
19645 {
19646 str = attr->value_as_string ();
19647 if (str == nullptr)
19648 complaint (_("string type expected for attribute %s for "
19649 "DIE at %s in module %s"),
19650 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19651 objfile_name (cu->per_objfile->objfile));
19652 }
19653
19654 return str;
19655 }
19656
19657 /* Return the dwo name or NULL if not present. If present, it is in either
19658 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19659 static const char *
19660 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19661 {
19662 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19663 if (dwo_name == nullptr)
19664 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19665 return dwo_name;
19666 }
19667
19668 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19669 and holds a non-zero value. This function should only be used for
19670 DW_FORM_flag or DW_FORM_flag_present attributes. */
19671
19672 static int
19673 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19674 {
19675 struct attribute *attr = dwarf2_attr (die, name, cu);
19676
19677 return (attr && DW_UNSND (attr));
19678 }
19679
19680 static int
19681 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19682 {
19683 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19684 which value is non-zero. However, we have to be careful with
19685 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19686 (via dwarf2_flag_true_p) follows this attribute. So we may
19687 end up accidently finding a declaration attribute that belongs
19688 to a different DIE referenced by the specification attribute,
19689 even though the given DIE does not have a declaration attribute. */
19690 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19691 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19692 }
19693
19694 /* Return the die giving the specification for DIE, if there is
19695 one. *SPEC_CU is the CU containing DIE on input, and the CU
19696 containing the return value on output. If there is no
19697 specification, but there is an abstract origin, that is
19698 returned. */
19699
19700 static struct die_info *
19701 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19702 {
19703 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19704 *spec_cu);
19705
19706 if (spec_attr == NULL)
19707 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19708
19709 if (spec_attr == NULL)
19710 return NULL;
19711 else
19712 return follow_die_ref (die, spec_attr, spec_cu);
19713 }
19714
19715 /* Stub for free_line_header to match void * callback types. */
19716
19717 static void
19718 free_line_header_voidp (void *arg)
19719 {
19720 struct line_header *lh = (struct line_header *) arg;
19721
19722 delete lh;
19723 }
19724
19725 /* A convenience function to find the proper .debug_line section for a CU. */
19726
19727 static struct dwarf2_section_info *
19728 get_debug_line_section (struct dwarf2_cu *cu)
19729 {
19730 struct dwarf2_section_info *section;
19731 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19732
19733 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19734 DWO file. */
19735 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19736 section = &cu->dwo_unit->dwo_file->sections.line;
19737 else if (cu->per_cu->is_dwz)
19738 {
19739 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19740
19741 section = &dwz->line;
19742 }
19743 else
19744 section = &dwarf2_per_objfile->per_bfd->line;
19745
19746 return section;
19747 }
19748
19749 /* Read the statement program header starting at OFFSET in
19750 .debug_line, or .debug_line.dwo. Return a pointer
19751 to a struct line_header, allocated using xmalloc.
19752 Returns NULL if there is a problem reading the header, e.g., if it
19753 has a version we don't understand.
19754
19755 NOTE: the strings in the include directory and file name tables of
19756 the returned object point into the dwarf line section buffer,
19757 and must not be freed. */
19758
19759 static line_header_up
19760 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19761 {
19762 struct dwarf2_section_info *section;
19763 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19764
19765 section = get_debug_line_section (cu);
19766 section->read (dwarf2_per_objfile->objfile);
19767 if (section->buffer == NULL)
19768 {
19769 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19770 complaint (_("missing .debug_line.dwo section"));
19771 else
19772 complaint (_("missing .debug_line section"));
19773 return 0;
19774 }
19775
19776 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19777 dwarf2_per_objfile, section,
19778 &cu->header);
19779 }
19780
19781 /* Subroutine of dwarf_decode_lines to simplify it.
19782 Return the file name of the psymtab for the given file_entry.
19783 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19784 If space for the result is malloc'd, *NAME_HOLDER will be set.
19785 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19786
19787 static const char *
19788 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19789 const dwarf2_psymtab *pst,
19790 const char *comp_dir,
19791 gdb::unique_xmalloc_ptr<char> *name_holder)
19792 {
19793 const char *include_name = fe.name;
19794 const char *include_name_to_compare = include_name;
19795 const char *pst_filename;
19796 int file_is_pst;
19797
19798 const char *dir_name = fe.include_dir (lh);
19799
19800 gdb::unique_xmalloc_ptr<char> hold_compare;
19801 if (!IS_ABSOLUTE_PATH (include_name)
19802 && (dir_name != NULL || comp_dir != NULL))
19803 {
19804 /* Avoid creating a duplicate psymtab for PST.
19805 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19806 Before we do the comparison, however, we need to account
19807 for DIR_NAME and COMP_DIR.
19808 First prepend dir_name (if non-NULL). If we still don't
19809 have an absolute path prepend comp_dir (if non-NULL).
19810 However, the directory we record in the include-file's
19811 psymtab does not contain COMP_DIR (to match the
19812 corresponding symtab(s)).
19813
19814 Example:
19815
19816 bash$ cd /tmp
19817 bash$ gcc -g ./hello.c
19818 include_name = "hello.c"
19819 dir_name = "."
19820 DW_AT_comp_dir = comp_dir = "/tmp"
19821 DW_AT_name = "./hello.c"
19822
19823 */
19824
19825 if (dir_name != NULL)
19826 {
19827 name_holder->reset (concat (dir_name, SLASH_STRING,
19828 include_name, (char *) NULL));
19829 include_name = name_holder->get ();
19830 include_name_to_compare = include_name;
19831 }
19832 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19833 {
19834 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19835 include_name, (char *) NULL));
19836 include_name_to_compare = hold_compare.get ();
19837 }
19838 }
19839
19840 pst_filename = pst->filename;
19841 gdb::unique_xmalloc_ptr<char> copied_name;
19842 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19843 {
19844 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19845 pst_filename, (char *) NULL));
19846 pst_filename = copied_name.get ();
19847 }
19848
19849 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19850
19851 if (file_is_pst)
19852 return NULL;
19853 return include_name;
19854 }
19855
19856 /* State machine to track the state of the line number program. */
19857
19858 class lnp_state_machine
19859 {
19860 public:
19861 /* Initialize a machine state for the start of a line number
19862 program. */
19863 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19864 bool record_lines_p);
19865
19866 file_entry *current_file ()
19867 {
19868 /* lh->file_names is 0-based, but the file name numbers in the
19869 statement program are 1-based. */
19870 return m_line_header->file_name_at (m_file);
19871 }
19872
19873 /* Record the line in the state machine. END_SEQUENCE is true if
19874 we're processing the end of a sequence. */
19875 void record_line (bool end_sequence);
19876
19877 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19878 nop-out rest of the lines in this sequence. */
19879 void check_line_address (struct dwarf2_cu *cu,
19880 const gdb_byte *line_ptr,
19881 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19882
19883 void handle_set_discriminator (unsigned int discriminator)
19884 {
19885 m_discriminator = discriminator;
19886 m_line_has_non_zero_discriminator |= discriminator != 0;
19887 }
19888
19889 /* Handle DW_LNE_set_address. */
19890 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19891 {
19892 m_op_index = 0;
19893 address += baseaddr;
19894 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19895 }
19896
19897 /* Handle DW_LNS_advance_pc. */
19898 void handle_advance_pc (CORE_ADDR adjust);
19899
19900 /* Handle a special opcode. */
19901 void handle_special_opcode (unsigned char op_code);
19902
19903 /* Handle DW_LNS_advance_line. */
19904 void handle_advance_line (int line_delta)
19905 {
19906 advance_line (line_delta);
19907 }
19908
19909 /* Handle DW_LNS_set_file. */
19910 void handle_set_file (file_name_index file);
19911
19912 /* Handle DW_LNS_negate_stmt. */
19913 void handle_negate_stmt ()
19914 {
19915 m_is_stmt = !m_is_stmt;
19916 }
19917
19918 /* Handle DW_LNS_const_add_pc. */
19919 void handle_const_add_pc ();
19920
19921 /* Handle DW_LNS_fixed_advance_pc. */
19922 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19923 {
19924 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19925 m_op_index = 0;
19926 }
19927
19928 /* Handle DW_LNS_copy. */
19929 void handle_copy ()
19930 {
19931 record_line (false);
19932 m_discriminator = 0;
19933 }
19934
19935 /* Handle DW_LNE_end_sequence. */
19936 void handle_end_sequence ()
19937 {
19938 m_currently_recording_lines = true;
19939 }
19940
19941 private:
19942 /* Advance the line by LINE_DELTA. */
19943 void advance_line (int line_delta)
19944 {
19945 m_line += line_delta;
19946
19947 if (line_delta != 0)
19948 m_line_has_non_zero_discriminator = m_discriminator != 0;
19949 }
19950
19951 struct dwarf2_cu *m_cu;
19952
19953 gdbarch *m_gdbarch;
19954
19955 /* True if we're recording lines.
19956 Otherwise we're building partial symtabs and are just interested in
19957 finding include files mentioned by the line number program. */
19958 bool m_record_lines_p;
19959
19960 /* The line number header. */
19961 line_header *m_line_header;
19962
19963 /* These are part of the standard DWARF line number state machine,
19964 and initialized according to the DWARF spec. */
19965
19966 unsigned char m_op_index = 0;
19967 /* The line table index of the current file. */
19968 file_name_index m_file = 1;
19969 unsigned int m_line = 1;
19970
19971 /* These are initialized in the constructor. */
19972
19973 CORE_ADDR m_address;
19974 bool m_is_stmt;
19975 unsigned int m_discriminator;
19976
19977 /* Additional bits of state we need to track. */
19978
19979 /* The last file that we called dwarf2_start_subfile for.
19980 This is only used for TLLs. */
19981 unsigned int m_last_file = 0;
19982 /* The last file a line number was recorded for. */
19983 struct subfile *m_last_subfile = NULL;
19984
19985 /* When true, record the lines we decode. */
19986 bool m_currently_recording_lines = false;
19987
19988 /* The last line number that was recorded, used to coalesce
19989 consecutive entries for the same line. This can happen, for
19990 example, when discriminators are present. PR 17276. */
19991 unsigned int m_last_line = 0;
19992 bool m_line_has_non_zero_discriminator = false;
19993 };
19994
19995 void
19996 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19997 {
19998 CORE_ADDR addr_adj = (((m_op_index + adjust)
19999 / m_line_header->maximum_ops_per_instruction)
20000 * m_line_header->minimum_instruction_length);
20001 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20002 m_op_index = ((m_op_index + adjust)
20003 % m_line_header->maximum_ops_per_instruction);
20004 }
20005
20006 void
20007 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20008 {
20009 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20010 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20011 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20012 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20013 / m_line_header->maximum_ops_per_instruction)
20014 * m_line_header->minimum_instruction_length);
20015 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20016 m_op_index = ((m_op_index + adj_opcode_d)
20017 % m_line_header->maximum_ops_per_instruction);
20018
20019 int line_delta = m_line_header->line_base + adj_opcode_r;
20020 advance_line (line_delta);
20021 record_line (false);
20022 m_discriminator = 0;
20023 }
20024
20025 void
20026 lnp_state_machine::handle_set_file (file_name_index file)
20027 {
20028 m_file = file;
20029
20030 const file_entry *fe = current_file ();
20031 if (fe == NULL)
20032 dwarf2_debug_line_missing_file_complaint ();
20033 else if (m_record_lines_p)
20034 {
20035 const char *dir = fe->include_dir (m_line_header);
20036
20037 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20038 m_line_has_non_zero_discriminator = m_discriminator != 0;
20039 dwarf2_start_subfile (m_cu, fe->name, dir);
20040 }
20041 }
20042
20043 void
20044 lnp_state_machine::handle_const_add_pc ()
20045 {
20046 CORE_ADDR adjust
20047 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20048
20049 CORE_ADDR addr_adj
20050 = (((m_op_index + adjust)
20051 / m_line_header->maximum_ops_per_instruction)
20052 * m_line_header->minimum_instruction_length);
20053
20054 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20055 m_op_index = ((m_op_index + adjust)
20056 % m_line_header->maximum_ops_per_instruction);
20057 }
20058
20059 /* Return non-zero if we should add LINE to the line number table.
20060 LINE is the line to add, LAST_LINE is the last line that was added,
20061 LAST_SUBFILE is the subfile for LAST_LINE.
20062 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20063 had a non-zero discriminator.
20064
20065 We have to be careful in the presence of discriminators.
20066 E.g., for this line:
20067
20068 for (i = 0; i < 100000; i++);
20069
20070 clang can emit four line number entries for that one line,
20071 each with a different discriminator.
20072 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20073
20074 However, we want gdb to coalesce all four entries into one.
20075 Otherwise the user could stepi into the middle of the line and
20076 gdb would get confused about whether the pc really was in the
20077 middle of the line.
20078
20079 Things are further complicated by the fact that two consecutive
20080 line number entries for the same line is a heuristic used by gcc
20081 to denote the end of the prologue. So we can't just discard duplicate
20082 entries, we have to be selective about it. The heuristic we use is
20083 that we only collapse consecutive entries for the same line if at least
20084 one of those entries has a non-zero discriminator. PR 17276.
20085
20086 Note: Addresses in the line number state machine can never go backwards
20087 within one sequence, thus this coalescing is ok. */
20088
20089 static int
20090 dwarf_record_line_p (struct dwarf2_cu *cu,
20091 unsigned int line, unsigned int last_line,
20092 int line_has_non_zero_discriminator,
20093 struct subfile *last_subfile)
20094 {
20095 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20096 return 1;
20097 if (line != last_line)
20098 return 1;
20099 /* Same line for the same file that we've seen already.
20100 As a last check, for pr 17276, only record the line if the line
20101 has never had a non-zero discriminator. */
20102 if (!line_has_non_zero_discriminator)
20103 return 1;
20104 return 0;
20105 }
20106
20107 /* Use the CU's builder to record line number LINE beginning at
20108 address ADDRESS in the line table of subfile SUBFILE. */
20109
20110 static void
20111 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20112 unsigned int line, CORE_ADDR address, bool is_stmt,
20113 struct dwarf2_cu *cu)
20114 {
20115 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20116
20117 if (dwarf_line_debug)
20118 {
20119 fprintf_unfiltered (gdb_stdlog,
20120 "Recording line %u, file %s, address %s\n",
20121 line, lbasename (subfile->name),
20122 paddress (gdbarch, address));
20123 }
20124
20125 if (cu != nullptr)
20126 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20127 }
20128
20129 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20130 Mark the end of a set of line number records.
20131 The arguments are the same as for dwarf_record_line_1.
20132 If SUBFILE is NULL the request is ignored. */
20133
20134 static void
20135 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20136 CORE_ADDR address, struct dwarf2_cu *cu)
20137 {
20138 if (subfile == NULL)
20139 return;
20140
20141 if (dwarf_line_debug)
20142 {
20143 fprintf_unfiltered (gdb_stdlog,
20144 "Finishing current line, file %s, address %s\n",
20145 lbasename (subfile->name),
20146 paddress (gdbarch, address));
20147 }
20148
20149 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20150 }
20151
20152 void
20153 lnp_state_machine::record_line (bool end_sequence)
20154 {
20155 if (dwarf_line_debug)
20156 {
20157 fprintf_unfiltered (gdb_stdlog,
20158 "Processing actual line %u: file %u,"
20159 " address %s, is_stmt %u, discrim %u%s\n",
20160 m_line, m_file,
20161 paddress (m_gdbarch, m_address),
20162 m_is_stmt, m_discriminator,
20163 (end_sequence ? "\t(end sequence)" : ""));
20164 }
20165
20166 file_entry *fe = current_file ();
20167
20168 if (fe == NULL)
20169 dwarf2_debug_line_missing_file_complaint ();
20170 /* For now we ignore lines not starting on an instruction boundary.
20171 But not when processing end_sequence for compatibility with the
20172 previous version of the code. */
20173 else if (m_op_index == 0 || end_sequence)
20174 {
20175 fe->included_p = 1;
20176 if (m_record_lines_p)
20177 {
20178 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20179 || end_sequence)
20180 {
20181 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20182 m_currently_recording_lines ? m_cu : nullptr);
20183 }
20184
20185 if (!end_sequence)
20186 {
20187 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20188
20189 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20190 m_line_has_non_zero_discriminator,
20191 m_last_subfile))
20192 {
20193 buildsym_compunit *builder = m_cu->get_builder ();
20194 dwarf_record_line_1 (m_gdbarch,
20195 builder->get_current_subfile (),
20196 m_line, m_address, is_stmt,
20197 m_currently_recording_lines ? m_cu : nullptr);
20198 }
20199 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20200 m_last_line = m_line;
20201 }
20202 }
20203 }
20204 }
20205
20206 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20207 line_header *lh, bool record_lines_p)
20208 {
20209 m_cu = cu;
20210 m_gdbarch = arch;
20211 m_record_lines_p = record_lines_p;
20212 m_line_header = lh;
20213
20214 m_currently_recording_lines = true;
20215
20216 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20217 was a line entry for it so that the backend has a chance to adjust it
20218 and also record it in case it needs it. This is currently used by MIPS
20219 code, cf. `mips_adjust_dwarf2_line'. */
20220 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20221 m_is_stmt = lh->default_is_stmt;
20222 m_discriminator = 0;
20223 }
20224
20225 void
20226 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20227 const gdb_byte *line_ptr,
20228 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20229 {
20230 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20231 the pc range of the CU. However, we restrict the test to only ADDRESS
20232 values of zero to preserve GDB's previous behaviour which is to handle
20233 the specific case of a function being GC'd by the linker. */
20234
20235 if (address == 0 && address < unrelocated_lowpc)
20236 {
20237 /* This line table is for a function which has been
20238 GCd by the linker. Ignore it. PR gdb/12528 */
20239
20240 struct objfile *objfile = cu->per_objfile->objfile;
20241 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20242
20243 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20244 line_offset, objfile_name (objfile));
20245 m_currently_recording_lines = false;
20246 /* Note: m_currently_recording_lines is left as false until we see
20247 DW_LNE_end_sequence. */
20248 }
20249 }
20250
20251 /* Subroutine of dwarf_decode_lines to simplify it.
20252 Process the line number information in LH.
20253 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20254 program in order to set included_p for every referenced header. */
20255
20256 static void
20257 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20258 const int decode_for_pst_p, CORE_ADDR lowpc)
20259 {
20260 const gdb_byte *line_ptr, *extended_end;
20261 const gdb_byte *line_end;
20262 unsigned int bytes_read, extended_len;
20263 unsigned char op_code, extended_op;
20264 CORE_ADDR baseaddr;
20265 struct objfile *objfile = cu->per_objfile->objfile;
20266 bfd *abfd = objfile->obfd;
20267 struct gdbarch *gdbarch = objfile->arch ();
20268 /* True if we're recording line info (as opposed to building partial
20269 symtabs and just interested in finding include files mentioned by
20270 the line number program). */
20271 bool record_lines_p = !decode_for_pst_p;
20272
20273 baseaddr = objfile->text_section_offset ();
20274
20275 line_ptr = lh->statement_program_start;
20276 line_end = lh->statement_program_end;
20277
20278 /* Read the statement sequences until there's nothing left. */
20279 while (line_ptr < line_end)
20280 {
20281 /* The DWARF line number program state machine. Reset the state
20282 machine at the start of each sequence. */
20283 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20284 bool end_sequence = false;
20285
20286 if (record_lines_p)
20287 {
20288 /* Start a subfile for the current file of the state
20289 machine. */
20290 const file_entry *fe = state_machine.current_file ();
20291
20292 if (fe != NULL)
20293 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20294 }
20295
20296 /* Decode the table. */
20297 while (line_ptr < line_end && !end_sequence)
20298 {
20299 op_code = read_1_byte (abfd, line_ptr);
20300 line_ptr += 1;
20301
20302 if (op_code >= lh->opcode_base)
20303 {
20304 /* Special opcode. */
20305 state_machine.handle_special_opcode (op_code);
20306 }
20307 else switch (op_code)
20308 {
20309 case DW_LNS_extended_op:
20310 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20311 &bytes_read);
20312 line_ptr += bytes_read;
20313 extended_end = line_ptr + extended_len;
20314 extended_op = read_1_byte (abfd, line_ptr);
20315 line_ptr += 1;
20316 switch (extended_op)
20317 {
20318 case DW_LNE_end_sequence:
20319 state_machine.handle_end_sequence ();
20320 end_sequence = true;
20321 break;
20322 case DW_LNE_set_address:
20323 {
20324 CORE_ADDR address
20325 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20326 line_ptr += bytes_read;
20327
20328 state_machine.check_line_address (cu, line_ptr,
20329 lowpc - baseaddr, address);
20330 state_machine.handle_set_address (baseaddr, address);
20331 }
20332 break;
20333 case DW_LNE_define_file:
20334 {
20335 const char *cur_file;
20336 unsigned int mod_time, length;
20337 dir_index dindex;
20338
20339 cur_file = read_direct_string (abfd, line_ptr,
20340 &bytes_read);
20341 line_ptr += bytes_read;
20342 dindex = (dir_index)
20343 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345 mod_time =
20346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20347 line_ptr += bytes_read;
20348 length =
20349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20350 line_ptr += bytes_read;
20351 lh->add_file_name (cur_file, dindex, mod_time, length);
20352 }
20353 break;
20354 case DW_LNE_set_discriminator:
20355 {
20356 /* The discriminator is not interesting to the
20357 debugger; just ignore it. We still need to
20358 check its value though:
20359 if there are consecutive entries for the same
20360 (non-prologue) line we want to coalesce them.
20361 PR 17276. */
20362 unsigned int discr
20363 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20364 line_ptr += bytes_read;
20365
20366 state_machine.handle_set_discriminator (discr);
20367 }
20368 break;
20369 default:
20370 complaint (_("mangled .debug_line section"));
20371 return;
20372 }
20373 /* Make sure that we parsed the extended op correctly. If e.g.
20374 we expected a different address size than the producer used,
20375 we may have read the wrong number of bytes. */
20376 if (line_ptr != extended_end)
20377 {
20378 complaint (_("mangled .debug_line section"));
20379 return;
20380 }
20381 break;
20382 case DW_LNS_copy:
20383 state_machine.handle_copy ();
20384 break;
20385 case DW_LNS_advance_pc:
20386 {
20387 CORE_ADDR adjust
20388 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20389 line_ptr += bytes_read;
20390
20391 state_machine.handle_advance_pc (adjust);
20392 }
20393 break;
20394 case DW_LNS_advance_line:
20395 {
20396 int line_delta
20397 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20398 line_ptr += bytes_read;
20399
20400 state_machine.handle_advance_line (line_delta);
20401 }
20402 break;
20403 case DW_LNS_set_file:
20404 {
20405 file_name_index file
20406 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20407 &bytes_read);
20408 line_ptr += bytes_read;
20409
20410 state_machine.handle_set_file (file);
20411 }
20412 break;
20413 case DW_LNS_set_column:
20414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20415 line_ptr += bytes_read;
20416 break;
20417 case DW_LNS_negate_stmt:
20418 state_machine.handle_negate_stmt ();
20419 break;
20420 case DW_LNS_set_basic_block:
20421 break;
20422 /* Add to the address register of the state machine the
20423 address increment value corresponding to special opcode
20424 255. I.e., this value is scaled by the minimum
20425 instruction length since special opcode 255 would have
20426 scaled the increment. */
20427 case DW_LNS_const_add_pc:
20428 state_machine.handle_const_add_pc ();
20429 break;
20430 case DW_LNS_fixed_advance_pc:
20431 {
20432 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20433 line_ptr += 2;
20434
20435 state_machine.handle_fixed_advance_pc (addr_adj);
20436 }
20437 break;
20438 default:
20439 {
20440 /* Unknown standard opcode, ignore it. */
20441 int i;
20442
20443 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20444 {
20445 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20446 line_ptr += bytes_read;
20447 }
20448 }
20449 }
20450 }
20451
20452 if (!end_sequence)
20453 dwarf2_debug_line_missing_end_sequence_complaint ();
20454
20455 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20456 in which case we still finish recording the last line). */
20457 state_machine.record_line (true);
20458 }
20459 }
20460
20461 /* Decode the Line Number Program (LNP) for the given line_header
20462 structure and CU. The actual information extracted and the type
20463 of structures created from the LNP depends on the value of PST.
20464
20465 1. If PST is NULL, then this procedure uses the data from the program
20466 to create all necessary symbol tables, and their linetables.
20467
20468 2. If PST is not NULL, this procedure reads the program to determine
20469 the list of files included by the unit represented by PST, and
20470 builds all the associated partial symbol tables.
20471
20472 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20473 It is used for relative paths in the line table.
20474 NOTE: When processing partial symtabs (pst != NULL),
20475 comp_dir == pst->dirname.
20476
20477 NOTE: It is important that psymtabs have the same file name (via strcmp)
20478 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20479 symtab we don't use it in the name of the psymtabs we create.
20480 E.g. expand_line_sal requires this when finding psymtabs to expand.
20481 A good testcase for this is mb-inline.exp.
20482
20483 LOWPC is the lowest address in CU (or 0 if not known).
20484
20485 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20486 for its PC<->lines mapping information. Otherwise only the filename
20487 table is read in. */
20488
20489 static void
20490 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20491 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20492 CORE_ADDR lowpc, int decode_mapping)
20493 {
20494 struct objfile *objfile = cu->per_objfile->objfile;
20495 const int decode_for_pst_p = (pst != NULL);
20496
20497 if (decode_mapping)
20498 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20499
20500 if (decode_for_pst_p)
20501 {
20502 /* Now that we're done scanning the Line Header Program, we can
20503 create the psymtab of each included file. */
20504 for (auto &file_entry : lh->file_names ())
20505 if (file_entry.included_p == 1)
20506 {
20507 gdb::unique_xmalloc_ptr<char> name_holder;
20508 const char *include_name =
20509 psymtab_include_file_name (lh, file_entry, pst,
20510 comp_dir, &name_holder);
20511 if (include_name != NULL)
20512 dwarf2_create_include_psymtab (include_name, pst, objfile);
20513 }
20514 }
20515 else
20516 {
20517 /* Make sure a symtab is created for every file, even files
20518 which contain only variables (i.e. no code with associated
20519 line numbers). */
20520 buildsym_compunit *builder = cu->get_builder ();
20521 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20522
20523 for (auto &fe : lh->file_names ())
20524 {
20525 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20526 if (builder->get_current_subfile ()->symtab == NULL)
20527 {
20528 builder->get_current_subfile ()->symtab
20529 = allocate_symtab (cust,
20530 builder->get_current_subfile ()->name);
20531 }
20532 fe.symtab = builder->get_current_subfile ()->symtab;
20533 }
20534 }
20535 }
20536
20537 /* Start a subfile for DWARF. FILENAME is the name of the file and
20538 DIRNAME the name of the source directory which contains FILENAME
20539 or NULL if not known.
20540 This routine tries to keep line numbers from identical absolute and
20541 relative file names in a common subfile.
20542
20543 Using the `list' example from the GDB testsuite, which resides in
20544 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20545 of /srcdir/list0.c yields the following debugging information for list0.c:
20546
20547 DW_AT_name: /srcdir/list0.c
20548 DW_AT_comp_dir: /compdir
20549 files.files[0].name: list0.h
20550 files.files[0].dir: /srcdir
20551 files.files[1].name: list0.c
20552 files.files[1].dir: /srcdir
20553
20554 The line number information for list0.c has to end up in a single
20555 subfile, so that `break /srcdir/list0.c:1' works as expected.
20556 start_subfile will ensure that this happens provided that we pass the
20557 concatenation of files.files[1].dir and files.files[1].name as the
20558 subfile's name. */
20559
20560 static void
20561 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20562 const char *dirname)
20563 {
20564 gdb::unique_xmalloc_ptr<char> copy;
20565
20566 /* In order not to lose the line information directory,
20567 we concatenate it to the filename when it makes sense.
20568 Note that the Dwarf3 standard says (speaking of filenames in line
20569 information): ``The directory index is ignored for file names
20570 that represent full path names''. Thus ignoring dirname in the
20571 `else' branch below isn't an issue. */
20572
20573 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20574 {
20575 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20576 filename = copy.get ();
20577 }
20578
20579 cu->get_builder ()->start_subfile (filename);
20580 }
20581
20582 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20583 buildsym_compunit constructor. */
20584
20585 struct compunit_symtab *
20586 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20587 CORE_ADDR low_pc)
20588 {
20589 gdb_assert (m_builder == nullptr);
20590
20591 m_builder.reset (new struct buildsym_compunit
20592 (this->per_objfile->objfile,
20593 name, comp_dir, language, low_pc));
20594
20595 list_in_scope = get_builder ()->get_file_symbols ();
20596
20597 get_builder ()->record_debugformat ("DWARF 2");
20598 get_builder ()->record_producer (producer);
20599
20600 processing_has_namespace_info = false;
20601
20602 return get_builder ()->get_compunit_symtab ();
20603 }
20604
20605 static void
20606 var_decode_location (struct attribute *attr, struct symbol *sym,
20607 struct dwarf2_cu *cu)
20608 {
20609 struct objfile *objfile = cu->per_objfile->objfile;
20610 struct comp_unit_head *cu_header = &cu->header;
20611
20612 /* NOTE drow/2003-01-30: There used to be a comment and some special
20613 code here to turn a symbol with DW_AT_external and a
20614 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20615 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20616 with some versions of binutils) where shared libraries could have
20617 relocations against symbols in their debug information - the
20618 minimal symbol would have the right address, but the debug info
20619 would not. It's no longer necessary, because we will explicitly
20620 apply relocations when we read in the debug information now. */
20621
20622 /* A DW_AT_location attribute with no contents indicates that a
20623 variable has been optimized away. */
20624 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20625 {
20626 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20627 return;
20628 }
20629
20630 /* Handle one degenerate form of location expression specially, to
20631 preserve GDB's previous behavior when section offsets are
20632 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20633 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20634
20635 if (attr->form_is_block ()
20636 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20637 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20638 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20639 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20640 && (DW_BLOCK (attr)->size
20641 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20642 {
20643 unsigned int dummy;
20644
20645 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20646 SET_SYMBOL_VALUE_ADDRESS
20647 (sym, cu->header.read_address (objfile->obfd,
20648 DW_BLOCK (attr)->data + 1,
20649 &dummy));
20650 else
20651 SET_SYMBOL_VALUE_ADDRESS
20652 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20653 &dummy));
20654 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20655 fixup_symbol_section (sym, objfile);
20656 SET_SYMBOL_VALUE_ADDRESS
20657 (sym,
20658 SYMBOL_VALUE_ADDRESS (sym)
20659 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20660 return;
20661 }
20662
20663 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20664 expression evaluator, and use LOC_COMPUTED only when necessary
20665 (i.e. when the value of a register or memory location is
20666 referenced, or a thread-local block, etc.). Then again, it might
20667 not be worthwhile. I'm assuming that it isn't unless performance
20668 or memory numbers show me otherwise. */
20669
20670 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20671
20672 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20673 cu->has_loclist = true;
20674 }
20675
20676 /* Given a pointer to a DWARF information entry, figure out if we need
20677 to make a symbol table entry for it, and if so, create a new entry
20678 and return a pointer to it.
20679 If TYPE is NULL, determine symbol type from the die, otherwise
20680 used the passed type.
20681 If SPACE is not NULL, use it to hold the new symbol. If it is
20682 NULL, allocate a new symbol on the objfile's obstack. */
20683
20684 static struct symbol *
20685 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20686 struct symbol *space)
20687 {
20688 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20689 struct objfile *objfile = dwarf2_per_objfile->objfile;
20690 struct gdbarch *gdbarch = objfile->arch ();
20691 struct symbol *sym = NULL;
20692 const char *name;
20693 struct attribute *attr = NULL;
20694 struct attribute *attr2 = NULL;
20695 CORE_ADDR baseaddr;
20696 struct pending **list_to_add = NULL;
20697
20698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20699
20700 baseaddr = objfile->text_section_offset ();
20701
20702 name = dwarf2_name (die, cu);
20703 if (name)
20704 {
20705 int suppress_add = 0;
20706
20707 if (space)
20708 sym = space;
20709 else
20710 sym = new (&objfile->objfile_obstack) symbol;
20711 OBJSTAT (objfile, n_syms++);
20712
20713 /* Cache this symbol's name and the name's demangled form (if any). */
20714 sym->set_language (cu->language, &objfile->objfile_obstack);
20715 /* Fortran does not have mangling standard and the mangling does differ
20716 between gfortran, iFort etc. */
20717 const char *physname
20718 = (cu->language == language_fortran
20719 ? dwarf2_full_name (name, die, cu)
20720 : dwarf2_physname (name, die, cu));
20721 const char *linkagename = dw2_linkage_name (die, cu);
20722
20723 if (linkagename == nullptr || cu->language == language_ada)
20724 sym->set_linkage_name (physname);
20725 else
20726 {
20727 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20728 sym->set_linkage_name (linkagename);
20729 }
20730
20731 /* Default assumptions.
20732 Use the passed type or decode it from the die. */
20733 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20734 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20735 if (type != NULL)
20736 SYMBOL_TYPE (sym) = type;
20737 else
20738 SYMBOL_TYPE (sym) = die_type (die, cu);
20739 attr = dwarf2_attr (die,
20740 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20741 cu);
20742 if (attr != nullptr)
20743 {
20744 SYMBOL_LINE (sym) = DW_UNSND (attr);
20745 }
20746
20747 attr = dwarf2_attr (die,
20748 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20749 cu);
20750 if (attr != nullptr)
20751 {
20752 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20753 struct file_entry *fe;
20754
20755 if (cu->line_header != NULL)
20756 fe = cu->line_header->file_name_at (file_index);
20757 else
20758 fe = NULL;
20759
20760 if (fe == NULL)
20761 complaint (_("file index out of range"));
20762 else
20763 symbol_set_symtab (sym, fe->symtab);
20764 }
20765
20766 switch (die->tag)
20767 {
20768 case DW_TAG_label:
20769 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20770 if (attr != nullptr)
20771 {
20772 CORE_ADDR addr;
20773
20774 addr = attr->value_as_address ();
20775 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20776 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20777 }
20778 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20779 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20780 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20781 add_symbol_to_list (sym, cu->list_in_scope);
20782 break;
20783 case DW_TAG_subprogram:
20784 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20785 finish_block. */
20786 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20787 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20788 if ((attr2 && (DW_UNSND (attr2) != 0))
20789 || cu->language == language_ada
20790 || cu->language == language_fortran)
20791 {
20792 /* Subprograms marked external are stored as a global symbol.
20793 Ada and Fortran subprograms, whether marked external or
20794 not, are always stored as a global symbol, because we want
20795 to be able to access them globally. For instance, we want
20796 to be able to break on a nested subprogram without having
20797 to specify the context. */
20798 list_to_add = cu->get_builder ()->get_global_symbols ();
20799 }
20800 else
20801 {
20802 list_to_add = cu->list_in_scope;
20803 }
20804 break;
20805 case DW_TAG_inlined_subroutine:
20806 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20807 finish_block. */
20808 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20809 SYMBOL_INLINED (sym) = 1;
20810 list_to_add = cu->list_in_scope;
20811 break;
20812 case DW_TAG_template_value_param:
20813 suppress_add = 1;
20814 /* Fall through. */
20815 case DW_TAG_constant:
20816 case DW_TAG_variable:
20817 case DW_TAG_member:
20818 /* Compilation with minimal debug info may result in
20819 variables with missing type entries. Change the
20820 misleading `void' type to something sensible. */
20821 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20822 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20823
20824 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20825 /* In the case of DW_TAG_member, we should only be called for
20826 static const members. */
20827 if (die->tag == DW_TAG_member)
20828 {
20829 /* dwarf2_add_field uses die_is_declaration,
20830 so we do the same. */
20831 gdb_assert (die_is_declaration (die, cu));
20832 gdb_assert (attr);
20833 }
20834 if (attr != nullptr)
20835 {
20836 dwarf2_const_value (attr, sym, cu);
20837 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20838 if (!suppress_add)
20839 {
20840 if (attr2 && (DW_UNSND (attr2) != 0))
20841 list_to_add = cu->get_builder ()->get_global_symbols ();
20842 else
20843 list_to_add = cu->list_in_scope;
20844 }
20845 break;
20846 }
20847 attr = dwarf2_attr (die, DW_AT_location, cu);
20848 if (attr != nullptr)
20849 {
20850 var_decode_location (attr, sym, cu);
20851 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20852
20853 /* Fortran explicitly imports any global symbols to the local
20854 scope by DW_TAG_common_block. */
20855 if (cu->language == language_fortran && die->parent
20856 && die->parent->tag == DW_TAG_common_block)
20857 attr2 = NULL;
20858
20859 if (SYMBOL_CLASS (sym) == LOC_STATIC
20860 && SYMBOL_VALUE_ADDRESS (sym) == 0
20861 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20862 {
20863 /* When a static variable is eliminated by the linker,
20864 the corresponding debug information is not stripped
20865 out, but the variable address is set to null;
20866 do not add such variables into symbol table. */
20867 }
20868 else if (attr2 && (DW_UNSND (attr2) != 0))
20869 {
20870 if (SYMBOL_CLASS (sym) == LOC_STATIC
20871 && (objfile->flags & OBJF_MAINLINE) == 0
20872 && dwarf2_per_objfile->per_bfd->can_copy)
20873 {
20874 /* A global static variable might be subject to
20875 copy relocation. We first check for a local
20876 minsym, though, because maybe the symbol was
20877 marked hidden, in which case this would not
20878 apply. */
20879 bound_minimal_symbol found
20880 = (lookup_minimal_symbol_linkage
20881 (sym->linkage_name (), objfile));
20882 if (found.minsym != nullptr)
20883 sym->maybe_copied = 1;
20884 }
20885
20886 /* A variable with DW_AT_external is never static,
20887 but it may be block-scoped. */
20888 list_to_add
20889 = ((cu->list_in_scope
20890 == cu->get_builder ()->get_file_symbols ())
20891 ? cu->get_builder ()->get_global_symbols ()
20892 : cu->list_in_scope);
20893 }
20894 else
20895 list_to_add = cu->list_in_scope;
20896 }
20897 else
20898 {
20899 /* We do not know the address of this symbol.
20900 If it is an external symbol and we have type information
20901 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20902 The address of the variable will then be determined from
20903 the minimal symbol table whenever the variable is
20904 referenced. */
20905 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20906
20907 /* Fortran explicitly imports any global symbols to the local
20908 scope by DW_TAG_common_block. */
20909 if (cu->language == language_fortran && die->parent
20910 && die->parent->tag == DW_TAG_common_block)
20911 {
20912 /* SYMBOL_CLASS doesn't matter here because
20913 read_common_block is going to reset it. */
20914 if (!suppress_add)
20915 list_to_add = cu->list_in_scope;
20916 }
20917 else if (attr2 && (DW_UNSND (attr2) != 0)
20918 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20919 {
20920 /* A variable with DW_AT_external is never static, but it
20921 may be block-scoped. */
20922 list_to_add
20923 = ((cu->list_in_scope
20924 == cu->get_builder ()->get_file_symbols ())
20925 ? cu->get_builder ()->get_global_symbols ()
20926 : cu->list_in_scope);
20927
20928 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20929 }
20930 else if (!die_is_declaration (die, cu))
20931 {
20932 /* Use the default LOC_OPTIMIZED_OUT class. */
20933 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20934 if (!suppress_add)
20935 list_to_add = cu->list_in_scope;
20936 }
20937 }
20938 break;
20939 case DW_TAG_formal_parameter:
20940 {
20941 /* If we are inside a function, mark this as an argument. If
20942 not, we might be looking at an argument to an inlined function
20943 when we do not have enough information to show inlined frames;
20944 pretend it's a local variable in that case so that the user can
20945 still see it. */
20946 struct context_stack *curr
20947 = cu->get_builder ()->get_current_context_stack ();
20948 if (curr != nullptr && curr->name != nullptr)
20949 SYMBOL_IS_ARGUMENT (sym) = 1;
20950 attr = dwarf2_attr (die, DW_AT_location, cu);
20951 if (attr != nullptr)
20952 {
20953 var_decode_location (attr, sym, cu);
20954 }
20955 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20956 if (attr != nullptr)
20957 {
20958 dwarf2_const_value (attr, sym, cu);
20959 }
20960
20961 list_to_add = cu->list_in_scope;
20962 }
20963 break;
20964 case DW_TAG_unspecified_parameters:
20965 /* From varargs functions; gdb doesn't seem to have any
20966 interest in this information, so just ignore it for now.
20967 (FIXME?) */
20968 break;
20969 case DW_TAG_template_type_param:
20970 suppress_add = 1;
20971 /* Fall through. */
20972 case DW_TAG_class_type:
20973 case DW_TAG_interface_type:
20974 case DW_TAG_structure_type:
20975 case DW_TAG_union_type:
20976 case DW_TAG_set_type:
20977 case DW_TAG_enumeration_type:
20978 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20979 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20980
20981 {
20982 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20983 really ever be static objects: otherwise, if you try
20984 to, say, break of a class's method and you're in a file
20985 which doesn't mention that class, it won't work unless
20986 the check for all static symbols in lookup_symbol_aux
20987 saves you. See the OtherFileClass tests in
20988 gdb.c++/namespace.exp. */
20989
20990 if (!suppress_add)
20991 {
20992 buildsym_compunit *builder = cu->get_builder ();
20993 list_to_add
20994 = (cu->list_in_scope == builder->get_file_symbols ()
20995 && cu->language == language_cplus
20996 ? builder->get_global_symbols ()
20997 : cu->list_in_scope);
20998
20999 /* The semantics of C++ state that "struct foo {
21000 ... }" also defines a typedef for "foo". */
21001 if (cu->language == language_cplus
21002 || cu->language == language_ada
21003 || cu->language == language_d
21004 || cu->language == language_rust)
21005 {
21006 /* The symbol's name is already allocated along
21007 with this objfile, so we don't need to
21008 duplicate it for the type. */
21009 if (SYMBOL_TYPE (sym)->name () == 0)
21010 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21011 }
21012 }
21013 }
21014 break;
21015 case DW_TAG_typedef:
21016 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21017 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21018 list_to_add = cu->list_in_scope;
21019 break;
21020 case DW_TAG_base_type:
21021 case DW_TAG_subrange_type:
21022 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21023 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21024 list_to_add = cu->list_in_scope;
21025 break;
21026 case DW_TAG_enumerator:
21027 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21028 if (attr != nullptr)
21029 {
21030 dwarf2_const_value (attr, sym, cu);
21031 }
21032 {
21033 /* NOTE: carlton/2003-11-10: See comment above in the
21034 DW_TAG_class_type, etc. block. */
21035
21036 list_to_add
21037 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21038 && cu->language == language_cplus
21039 ? cu->get_builder ()->get_global_symbols ()
21040 : cu->list_in_scope);
21041 }
21042 break;
21043 case DW_TAG_imported_declaration:
21044 case DW_TAG_namespace:
21045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21046 list_to_add = cu->get_builder ()->get_global_symbols ();
21047 break;
21048 case DW_TAG_module:
21049 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21050 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21051 list_to_add = cu->get_builder ()->get_global_symbols ();
21052 break;
21053 case DW_TAG_common_block:
21054 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21055 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21056 add_symbol_to_list (sym, cu->list_in_scope);
21057 break;
21058 default:
21059 /* Not a tag we recognize. Hopefully we aren't processing
21060 trash data, but since we must specifically ignore things
21061 we don't recognize, there is nothing else we should do at
21062 this point. */
21063 complaint (_("unsupported tag: '%s'"),
21064 dwarf_tag_name (die->tag));
21065 break;
21066 }
21067
21068 if (suppress_add)
21069 {
21070 sym->hash_next = objfile->template_symbols;
21071 objfile->template_symbols = sym;
21072 list_to_add = NULL;
21073 }
21074
21075 if (list_to_add != NULL)
21076 add_symbol_to_list (sym, list_to_add);
21077
21078 /* For the benefit of old versions of GCC, check for anonymous
21079 namespaces based on the demangled name. */
21080 if (!cu->processing_has_namespace_info
21081 && cu->language == language_cplus)
21082 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21083 }
21084 return (sym);
21085 }
21086
21087 /* Given an attr with a DW_FORM_dataN value in host byte order,
21088 zero-extend it as appropriate for the symbol's type. The DWARF
21089 standard (v4) is not entirely clear about the meaning of using
21090 DW_FORM_dataN for a constant with a signed type, where the type is
21091 wider than the data. The conclusion of a discussion on the DWARF
21092 list was that this is unspecified. We choose to always zero-extend
21093 because that is the interpretation long in use by GCC. */
21094
21095 static gdb_byte *
21096 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21097 struct dwarf2_cu *cu, LONGEST *value, int bits)
21098 {
21099 struct objfile *objfile = cu->per_objfile->objfile;
21100 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21101 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21102 LONGEST l = DW_UNSND (attr);
21103
21104 if (bits < sizeof (*value) * 8)
21105 {
21106 l &= ((LONGEST) 1 << bits) - 1;
21107 *value = l;
21108 }
21109 else if (bits == sizeof (*value) * 8)
21110 *value = l;
21111 else
21112 {
21113 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21114 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21115 return bytes;
21116 }
21117
21118 return NULL;
21119 }
21120
21121 /* Read a constant value from an attribute. Either set *VALUE, or if
21122 the value does not fit in *VALUE, set *BYTES - either already
21123 allocated on the objfile obstack, or newly allocated on OBSTACK,
21124 or, set *BATON, if we translated the constant to a location
21125 expression. */
21126
21127 static void
21128 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21129 const char *name, struct obstack *obstack,
21130 struct dwarf2_cu *cu,
21131 LONGEST *value, const gdb_byte **bytes,
21132 struct dwarf2_locexpr_baton **baton)
21133 {
21134 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21135 struct objfile *objfile = per_objfile->objfile;
21136 struct comp_unit_head *cu_header = &cu->header;
21137 struct dwarf_block *blk;
21138 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21139 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21140
21141 *value = 0;
21142 *bytes = NULL;
21143 *baton = NULL;
21144
21145 switch (attr->form)
21146 {
21147 case DW_FORM_addr:
21148 case DW_FORM_addrx:
21149 case DW_FORM_GNU_addr_index:
21150 {
21151 gdb_byte *data;
21152
21153 if (TYPE_LENGTH (type) != cu_header->addr_size)
21154 dwarf2_const_value_length_mismatch_complaint (name,
21155 cu_header->addr_size,
21156 TYPE_LENGTH (type));
21157 /* Symbols of this form are reasonably rare, so we just
21158 piggyback on the existing location code rather than writing
21159 a new implementation of symbol_computed_ops. */
21160 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21161 (*baton)->per_objfile = per_objfile;
21162 (*baton)->per_cu = cu->per_cu;
21163 gdb_assert ((*baton)->per_cu);
21164
21165 (*baton)->size = 2 + cu_header->addr_size;
21166 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21167 (*baton)->data = data;
21168
21169 data[0] = DW_OP_addr;
21170 store_unsigned_integer (&data[1], cu_header->addr_size,
21171 byte_order, DW_ADDR (attr));
21172 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21173 }
21174 break;
21175 case DW_FORM_string:
21176 case DW_FORM_strp:
21177 case DW_FORM_strx:
21178 case DW_FORM_GNU_str_index:
21179 case DW_FORM_GNU_strp_alt:
21180 /* DW_STRING is already allocated on the objfile obstack, point
21181 directly to it. */
21182 *bytes = (const gdb_byte *) DW_STRING (attr);
21183 break;
21184 case DW_FORM_block1:
21185 case DW_FORM_block2:
21186 case DW_FORM_block4:
21187 case DW_FORM_block:
21188 case DW_FORM_exprloc:
21189 case DW_FORM_data16:
21190 blk = DW_BLOCK (attr);
21191 if (TYPE_LENGTH (type) != blk->size)
21192 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21193 TYPE_LENGTH (type));
21194 *bytes = blk->data;
21195 break;
21196
21197 /* The DW_AT_const_value attributes are supposed to carry the
21198 symbol's value "represented as it would be on the target
21199 architecture." By the time we get here, it's already been
21200 converted to host endianness, so we just need to sign- or
21201 zero-extend it as appropriate. */
21202 case DW_FORM_data1:
21203 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21204 break;
21205 case DW_FORM_data2:
21206 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21207 break;
21208 case DW_FORM_data4:
21209 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21210 break;
21211 case DW_FORM_data8:
21212 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21213 break;
21214
21215 case DW_FORM_sdata:
21216 case DW_FORM_implicit_const:
21217 *value = DW_SND (attr);
21218 break;
21219
21220 case DW_FORM_udata:
21221 *value = DW_UNSND (attr);
21222 break;
21223
21224 default:
21225 complaint (_("unsupported const value attribute form: '%s'"),
21226 dwarf_form_name (attr->form));
21227 *value = 0;
21228 break;
21229 }
21230 }
21231
21232
21233 /* Copy constant value from an attribute to a symbol. */
21234
21235 static void
21236 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21237 struct dwarf2_cu *cu)
21238 {
21239 struct objfile *objfile = cu->per_objfile->objfile;
21240 LONGEST value;
21241 const gdb_byte *bytes;
21242 struct dwarf2_locexpr_baton *baton;
21243
21244 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21245 sym->print_name (),
21246 &objfile->objfile_obstack, cu,
21247 &value, &bytes, &baton);
21248
21249 if (baton != NULL)
21250 {
21251 SYMBOL_LOCATION_BATON (sym) = baton;
21252 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21253 }
21254 else if (bytes != NULL)
21255 {
21256 SYMBOL_VALUE_BYTES (sym) = bytes;
21257 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21258 }
21259 else
21260 {
21261 SYMBOL_VALUE (sym) = value;
21262 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21263 }
21264 }
21265
21266 /* Return the type of the die in question using its DW_AT_type attribute. */
21267
21268 static struct type *
21269 die_type (struct die_info *die, struct dwarf2_cu *cu)
21270 {
21271 struct attribute *type_attr;
21272
21273 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21274 if (!type_attr)
21275 {
21276 struct objfile *objfile = cu->per_objfile->objfile;
21277 /* A missing DW_AT_type represents a void type. */
21278 return objfile_type (objfile)->builtin_void;
21279 }
21280
21281 return lookup_die_type (die, type_attr, cu);
21282 }
21283
21284 /* True iff CU's producer generates GNAT Ada auxiliary information
21285 that allows to find parallel types through that information instead
21286 of having to do expensive parallel lookups by type name. */
21287
21288 static int
21289 need_gnat_info (struct dwarf2_cu *cu)
21290 {
21291 /* Assume that the Ada compiler was GNAT, which always produces
21292 the auxiliary information. */
21293 return (cu->language == language_ada);
21294 }
21295
21296 /* Return the auxiliary type of the die in question using its
21297 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21298 attribute is not present. */
21299
21300 static struct type *
21301 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21302 {
21303 struct attribute *type_attr;
21304
21305 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21306 if (!type_attr)
21307 return NULL;
21308
21309 return lookup_die_type (die, type_attr, cu);
21310 }
21311
21312 /* If DIE has a descriptive_type attribute, then set the TYPE's
21313 descriptive type accordingly. */
21314
21315 static void
21316 set_descriptive_type (struct type *type, struct die_info *die,
21317 struct dwarf2_cu *cu)
21318 {
21319 struct type *descriptive_type = die_descriptive_type (die, cu);
21320
21321 if (descriptive_type)
21322 {
21323 ALLOCATE_GNAT_AUX_TYPE (type);
21324 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21325 }
21326 }
21327
21328 /* Return the containing type of the die in question using its
21329 DW_AT_containing_type attribute. */
21330
21331 static struct type *
21332 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21333 {
21334 struct attribute *type_attr;
21335 struct objfile *objfile = cu->per_objfile->objfile;
21336
21337 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21338 if (!type_attr)
21339 error (_("Dwarf Error: Problem turning containing type into gdb type "
21340 "[in module %s]"), objfile_name (objfile));
21341
21342 return lookup_die_type (die, type_attr, cu);
21343 }
21344
21345 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21346
21347 static struct type *
21348 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21349 {
21350 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21351 struct objfile *objfile = dwarf2_per_objfile->objfile;
21352 char *saved;
21353
21354 std::string message
21355 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21356 objfile_name (objfile),
21357 sect_offset_str (cu->header.sect_off),
21358 sect_offset_str (die->sect_off));
21359 saved = obstack_strdup (&objfile->objfile_obstack, message);
21360
21361 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21362 }
21363
21364 /* Look up the type of DIE in CU using its type attribute ATTR.
21365 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21366 DW_AT_containing_type.
21367 If there is no type substitute an error marker. */
21368
21369 static struct type *
21370 lookup_die_type (struct die_info *die, const struct attribute *attr,
21371 struct dwarf2_cu *cu)
21372 {
21373 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21374 struct objfile *objfile = dwarf2_per_objfile->objfile;
21375 struct type *this_type;
21376
21377 gdb_assert (attr->name == DW_AT_type
21378 || attr->name == DW_AT_GNAT_descriptive_type
21379 || attr->name == DW_AT_containing_type);
21380
21381 /* First see if we have it cached. */
21382
21383 if (attr->form == DW_FORM_GNU_ref_alt)
21384 {
21385 struct dwarf2_per_cu_data *per_cu;
21386 sect_offset sect_off = attr->get_ref_die_offset ();
21387
21388 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21389 dwarf2_per_objfile);
21390 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21391 }
21392 else if (attr->form_is_ref ())
21393 {
21394 sect_offset sect_off = attr->get_ref_die_offset ();
21395
21396 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21397 dwarf2_per_objfile);
21398 }
21399 else if (attr->form == DW_FORM_ref_sig8)
21400 {
21401 ULONGEST signature = DW_SIGNATURE (attr);
21402
21403 return get_signatured_type (die, signature, cu);
21404 }
21405 else
21406 {
21407 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21408 " at %s [in module %s]"),
21409 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21410 objfile_name (objfile));
21411 return build_error_marker_type (cu, die);
21412 }
21413
21414 /* If not cached we need to read it in. */
21415
21416 if (this_type == NULL)
21417 {
21418 struct die_info *type_die = NULL;
21419 struct dwarf2_cu *type_cu = cu;
21420
21421 if (attr->form_is_ref ())
21422 type_die = follow_die_ref (die, attr, &type_cu);
21423 if (type_die == NULL)
21424 return build_error_marker_type (cu, die);
21425 /* If we find the type now, it's probably because the type came
21426 from an inter-CU reference and the type's CU got expanded before
21427 ours. */
21428 this_type = read_type_die (type_die, type_cu);
21429 }
21430
21431 /* If we still don't have a type use an error marker. */
21432
21433 if (this_type == NULL)
21434 return build_error_marker_type (cu, die);
21435
21436 return this_type;
21437 }
21438
21439 /* Return the type in DIE, CU.
21440 Returns NULL for invalid types.
21441
21442 This first does a lookup in die_type_hash,
21443 and only reads the die in if necessary.
21444
21445 NOTE: This can be called when reading in partial or full symbols. */
21446
21447 static struct type *
21448 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21449 {
21450 struct type *this_type;
21451
21452 this_type = get_die_type (die, cu);
21453 if (this_type)
21454 return this_type;
21455
21456 return read_type_die_1 (die, cu);
21457 }
21458
21459 /* Read the type in DIE, CU.
21460 Returns NULL for invalid types. */
21461
21462 static struct type *
21463 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21464 {
21465 struct type *this_type = NULL;
21466
21467 switch (die->tag)
21468 {
21469 case DW_TAG_class_type:
21470 case DW_TAG_interface_type:
21471 case DW_TAG_structure_type:
21472 case DW_TAG_union_type:
21473 this_type = read_structure_type (die, cu);
21474 break;
21475 case DW_TAG_enumeration_type:
21476 this_type = read_enumeration_type (die, cu);
21477 break;
21478 case DW_TAG_subprogram:
21479 case DW_TAG_subroutine_type:
21480 case DW_TAG_inlined_subroutine:
21481 this_type = read_subroutine_type (die, cu);
21482 break;
21483 case DW_TAG_array_type:
21484 this_type = read_array_type (die, cu);
21485 break;
21486 case DW_TAG_set_type:
21487 this_type = read_set_type (die, cu);
21488 break;
21489 case DW_TAG_pointer_type:
21490 this_type = read_tag_pointer_type (die, cu);
21491 break;
21492 case DW_TAG_ptr_to_member_type:
21493 this_type = read_tag_ptr_to_member_type (die, cu);
21494 break;
21495 case DW_TAG_reference_type:
21496 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21497 break;
21498 case DW_TAG_rvalue_reference_type:
21499 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21500 break;
21501 case DW_TAG_const_type:
21502 this_type = read_tag_const_type (die, cu);
21503 break;
21504 case DW_TAG_volatile_type:
21505 this_type = read_tag_volatile_type (die, cu);
21506 break;
21507 case DW_TAG_restrict_type:
21508 this_type = read_tag_restrict_type (die, cu);
21509 break;
21510 case DW_TAG_string_type:
21511 this_type = read_tag_string_type (die, cu);
21512 break;
21513 case DW_TAG_typedef:
21514 this_type = read_typedef (die, cu);
21515 break;
21516 case DW_TAG_subrange_type:
21517 this_type = read_subrange_type (die, cu);
21518 break;
21519 case DW_TAG_base_type:
21520 this_type = read_base_type (die, cu);
21521 break;
21522 case DW_TAG_unspecified_type:
21523 this_type = read_unspecified_type (die, cu);
21524 break;
21525 case DW_TAG_namespace:
21526 this_type = read_namespace_type (die, cu);
21527 break;
21528 case DW_TAG_module:
21529 this_type = read_module_type (die, cu);
21530 break;
21531 case DW_TAG_atomic_type:
21532 this_type = read_tag_atomic_type (die, cu);
21533 break;
21534 default:
21535 complaint (_("unexpected tag in read_type_die: '%s'"),
21536 dwarf_tag_name (die->tag));
21537 break;
21538 }
21539
21540 return this_type;
21541 }
21542
21543 /* See if we can figure out if the class lives in a namespace. We do
21544 this by looking for a member function; its demangled name will
21545 contain namespace info, if there is any.
21546 Return the computed name or NULL.
21547 Space for the result is allocated on the objfile's obstack.
21548 This is the full-die version of guess_partial_die_structure_name.
21549 In this case we know DIE has no useful parent. */
21550
21551 static const char *
21552 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21553 {
21554 struct die_info *spec_die;
21555 struct dwarf2_cu *spec_cu;
21556 struct die_info *child;
21557 struct objfile *objfile = cu->per_objfile->objfile;
21558
21559 spec_cu = cu;
21560 spec_die = die_specification (die, &spec_cu);
21561 if (spec_die != NULL)
21562 {
21563 die = spec_die;
21564 cu = spec_cu;
21565 }
21566
21567 for (child = die->child;
21568 child != NULL;
21569 child = child->sibling)
21570 {
21571 if (child->tag == DW_TAG_subprogram)
21572 {
21573 const char *linkage_name = dw2_linkage_name (child, cu);
21574
21575 if (linkage_name != NULL)
21576 {
21577 gdb::unique_xmalloc_ptr<char> actual_name
21578 (language_class_name_from_physname (cu->language_defn,
21579 linkage_name));
21580 const char *name = NULL;
21581
21582 if (actual_name != NULL)
21583 {
21584 const char *die_name = dwarf2_name (die, cu);
21585
21586 if (die_name != NULL
21587 && strcmp (die_name, actual_name.get ()) != 0)
21588 {
21589 /* Strip off the class name from the full name.
21590 We want the prefix. */
21591 int die_name_len = strlen (die_name);
21592 int actual_name_len = strlen (actual_name.get ());
21593 const char *ptr = actual_name.get ();
21594
21595 /* Test for '::' as a sanity check. */
21596 if (actual_name_len > die_name_len + 2
21597 && ptr[actual_name_len - die_name_len - 1] == ':')
21598 name = obstack_strndup (
21599 &objfile->per_bfd->storage_obstack,
21600 ptr, actual_name_len - die_name_len - 2);
21601 }
21602 }
21603 return name;
21604 }
21605 }
21606 }
21607
21608 return NULL;
21609 }
21610
21611 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21612 prefix part in such case. See
21613 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21614
21615 static const char *
21616 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21617 {
21618 struct attribute *attr;
21619 const char *base;
21620
21621 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21622 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21623 return NULL;
21624
21625 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21626 return NULL;
21627
21628 attr = dw2_linkage_name_attr (die, cu);
21629 if (attr == NULL || DW_STRING (attr) == NULL)
21630 return NULL;
21631
21632 /* dwarf2_name had to be already called. */
21633 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21634
21635 /* Strip the base name, keep any leading namespaces/classes. */
21636 base = strrchr (DW_STRING (attr), ':');
21637 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21638 return "";
21639
21640 struct objfile *objfile = cu->per_objfile->objfile;
21641 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21642 DW_STRING (attr),
21643 &base[-1] - DW_STRING (attr));
21644 }
21645
21646 /* Return the name of the namespace/class that DIE is defined within,
21647 or "" if we can't tell. The caller should not xfree the result.
21648
21649 For example, if we're within the method foo() in the following
21650 code:
21651
21652 namespace N {
21653 class C {
21654 void foo () {
21655 }
21656 };
21657 }
21658
21659 then determine_prefix on foo's die will return "N::C". */
21660
21661 static const char *
21662 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21663 {
21664 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21665 struct die_info *parent, *spec_die;
21666 struct dwarf2_cu *spec_cu;
21667 struct type *parent_type;
21668 const char *retval;
21669
21670 if (cu->language != language_cplus
21671 && cu->language != language_fortran && cu->language != language_d
21672 && cu->language != language_rust)
21673 return "";
21674
21675 retval = anonymous_struct_prefix (die, cu);
21676 if (retval)
21677 return retval;
21678
21679 /* We have to be careful in the presence of DW_AT_specification.
21680 For example, with GCC 3.4, given the code
21681
21682 namespace N {
21683 void foo() {
21684 // Definition of N::foo.
21685 }
21686 }
21687
21688 then we'll have a tree of DIEs like this:
21689
21690 1: DW_TAG_compile_unit
21691 2: DW_TAG_namespace // N
21692 3: DW_TAG_subprogram // declaration of N::foo
21693 4: DW_TAG_subprogram // definition of N::foo
21694 DW_AT_specification // refers to die #3
21695
21696 Thus, when processing die #4, we have to pretend that we're in
21697 the context of its DW_AT_specification, namely the contex of die
21698 #3. */
21699 spec_cu = cu;
21700 spec_die = die_specification (die, &spec_cu);
21701 if (spec_die == NULL)
21702 parent = die->parent;
21703 else
21704 {
21705 parent = spec_die->parent;
21706 cu = spec_cu;
21707 }
21708
21709 if (parent == NULL)
21710 return "";
21711 else if (parent->building_fullname)
21712 {
21713 const char *name;
21714 const char *parent_name;
21715
21716 /* It has been seen on RealView 2.2 built binaries,
21717 DW_TAG_template_type_param types actually _defined_ as
21718 children of the parent class:
21719
21720 enum E {};
21721 template class <class Enum> Class{};
21722 Class<enum E> class_e;
21723
21724 1: DW_TAG_class_type (Class)
21725 2: DW_TAG_enumeration_type (E)
21726 3: DW_TAG_enumerator (enum1:0)
21727 3: DW_TAG_enumerator (enum2:1)
21728 ...
21729 2: DW_TAG_template_type_param
21730 DW_AT_type DW_FORM_ref_udata (E)
21731
21732 Besides being broken debug info, it can put GDB into an
21733 infinite loop. Consider:
21734
21735 When we're building the full name for Class<E>, we'll start
21736 at Class, and go look over its template type parameters,
21737 finding E. We'll then try to build the full name of E, and
21738 reach here. We're now trying to build the full name of E,
21739 and look over the parent DIE for containing scope. In the
21740 broken case, if we followed the parent DIE of E, we'd again
21741 find Class, and once again go look at its template type
21742 arguments, etc., etc. Simply don't consider such parent die
21743 as source-level parent of this die (it can't be, the language
21744 doesn't allow it), and break the loop here. */
21745 name = dwarf2_name (die, cu);
21746 parent_name = dwarf2_name (parent, cu);
21747 complaint (_("template param type '%s' defined within parent '%s'"),
21748 name ? name : "<unknown>",
21749 parent_name ? parent_name : "<unknown>");
21750 return "";
21751 }
21752 else
21753 switch (parent->tag)
21754 {
21755 case DW_TAG_namespace:
21756 parent_type = read_type_die (parent, cu);
21757 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21758 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21759 Work around this problem here. */
21760 if (cu->language == language_cplus
21761 && strcmp (parent_type->name (), "::") == 0)
21762 return "";
21763 /* We give a name to even anonymous namespaces. */
21764 return parent_type->name ();
21765 case DW_TAG_class_type:
21766 case DW_TAG_interface_type:
21767 case DW_TAG_structure_type:
21768 case DW_TAG_union_type:
21769 case DW_TAG_module:
21770 parent_type = read_type_die (parent, cu);
21771 if (parent_type->name () != NULL)
21772 return parent_type->name ();
21773 else
21774 /* An anonymous structure is only allowed non-static data
21775 members; no typedefs, no member functions, et cetera.
21776 So it does not need a prefix. */
21777 return "";
21778 case DW_TAG_compile_unit:
21779 case DW_TAG_partial_unit:
21780 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21781 if (cu->language == language_cplus
21782 && !dwarf2_per_objfile->per_bfd->types.empty ()
21783 && die->child != NULL
21784 && (die->tag == DW_TAG_class_type
21785 || die->tag == DW_TAG_structure_type
21786 || die->tag == DW_TAG_union_type))
21787 {
21788 const char *name = guess_full_die_structure_name (die, cu);
21789 if (name != NULL)
21790 return name;
21791 }
21792 return "";
21793 case DW_TAG_subprogram:
21794 /* Nested subroutines in Fortran get a prefix with the name
21795 of the parent's subroutine. */
21796 if (cu->language == language_fortran)
21797 {
21798 if ((die->tag == DW_TAG_subprogram)
21799 && (dwarf2_name (parent, cu) != NULL))
21800 return dwarf2_name (parent, cu);
21801 }
21802 return determine_prefix (parent, cu);
21803 case DW_TAG_enumeration_type:
21804 parent_type = read_type_die (parent, cu);
21805 if (TYPE_DECLARED_CLASS (parent_type))
21806 {
21807 if (parent_type->name () != NULL)
21808 return parent_type->name ();
21809 return "";
21810 }
21811 /* Fall through. */
21812 default:
21813 return determine_prefix (parent, cu);
21814 }
21815 }
21816
21817 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21818 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21819 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21820 an obconcat, otherwise allocate storage for the result. The CU argument is
21821 used to determine the language and hence, the appropriate separator. */
21822
21823 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21824
21825 static char *
21826 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21827 int physname, struct dwarf2_cu *cu)
21828 {
21829 const char *lead = "";
21830 const char *sep;
21831
21832 if (suffix == NULL || suffix[0] == '\0'
21833 || prefix == NULL || prefix[0] == '\0')
21834 sep = "";
21835 else if (cu->language == language_d)
21836 {
21837 /* For D, the 'main' function could be defined in any module, but it
21838 should never be prefixed. */
21839 if (strcmp (suffix, "D main") == 0)
21840 {
21841 prefix = "";
21842 sep = "";
21843 }
21844 else
21845 sep = ".";
21846 }
21847 else if (cu->language == language_fortran && physname)
21848 {
21849 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21850 DW_AT_MIPS_linkage_name is preferred and used instead. */
21851
21852 lead = "__";
21853 sep = "_MOD_";
21854 }
21855 else
21856 sep = "::";
21857
21858 if (prefix == NULL)
21859 prefix = "";
21860 if (suffix == NULL)
21861 suffix = "";
21862
21863 if (obs == NULL)
21864 {
21865 char *retval
21866 = ((char *)
21867 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21868
21869 strcpy (retval, lead);
21870 strcat (retval, prefix);
21871 strcat (retval, sep);
21872 strcat (retval, suffix);
21873 return retval;
21874 }
21875 else
21876 {
21877 /* We have an obstack. */
21878 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21879 }
21880 }
21881
21882 /* Get name of a die, return NULL if not found. */
21883
21884 static const char *
21885 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21886 struct objfile *objfile)
21887 {
21888 if (name && cu->language == language_cplus)
21889 {
21890 gdb::unique_xmalloc_ptr<char> canon_name
21891 = cp_canonicalize_string (name);
21892
21893 if (canon_name != nullptr)
21894 name = objfile->intern (canon_name.get ());
21895 }
21896
21897 return name;
21898 }
21899
21900 /* Get name of a die, return NULL if not found.
21901 Anonymous namespaces are converted to their magic string. */
21902
21903 static const char *
21904 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21905 {
21906 struct attribute *attr;
21907 struct objfile *objfile = cu->per_objfile->objfile;
21908
21909 attr = dwarf2_attr (die, DW_AT_name, cu);
21910 if ((!attr || !DW_STRING (attr))
21911 && die->tag != DW_TAG_namespace
21912 && die->tag != DW_TAG_class_type
21913 && die->tag != DW_TAG_interface_type
21914 && die->tag != DW_TAG_structure_type
21915 && die->tag != DW_TAG_union_type)
21916 return NULL;
21917
21918 switch (die->tag)
21919 {
21920 case DW_TAG_compile_unit:
21921 case DW_TAG_partial_unit:
21922 /* Compilation units have a DW_AT_name that is a filename, not
21923 a source language identifier. */
21924 case DW_TAG_enumeration_type:
21925 case DW_TAG_enumerator:
21926 /* These tags always have simple identifiers already; no need
21927 to canonicalize them. */
21928 return DW_STRING (attr);
21929
21930 case DW_TAG_namespace:
21931 if (attr != NULL && DW_STRING (attr) != NULL)
21932 return DW_STRING (attr);
21933 return CP_ANONYMOUS_NAMESPACE_STR;
21934
21935 case DW_TAG_class_type:
21936 case DW_TAG_interface_type:
21937 case DW_TAG_structure_type:
21938 case DW_TAG_union_type:
21939 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21940 structures or unions. These were of the form "._%d" in GCC 4.1,
21941 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21942 and GCC 4.4. We work around this problem by ignoring these. */
21943 if (attr && DW_STRING (attr)
21944 && (startswith (DW_STRING (attr), "._")
21945 || startswith (DW_STRING (attr), "<anonymous")))
21946 return NULL;
21947
21948 /* GCC might emit a nameless typedef that has a linkage name. See
21949 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21950 if (!attr || DW_STRING (attr) == NULL)
21951 {
21952 attr = dw2_linkage_name_attr (die, cu);
21953 if (attr == NULL || DW_STRING (attr) == NULL)
21954 return NULL;
21955
21956 /* Avoid demangling DW_STRING (attr) the second time on a second
21957 call for the same DIE. */
21958 if (!DW_STRING_IS_CANONICAL (attr))
21959 {
21960 gdb::unique_xmalloc_ptr<char> demangled
21961 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21962 if (demangled == nullptr)
21963 return nullptr;
21964
21965 DW_STRING (attr) = objfile->intern (demangled.get ());
21966 DW_STRING_IS_CANONICAL (attr) = 1;
21967 }
21968
21969 /* Strip any leading namespaces/classes, keep only the base name.
21970 DW_AT_name for named DIEs does not contain the prefixes. */
21971 const char *base = strrchr (DW_STRING (attr), ':');
21972 if (base && base > DW_STRING (attr) && base[-1] == ':')
21973 return &base[1];
21974 else
21975 return DW_STRING (attr);
21976 }
21977 break;
21978
21979 default:
21980 break;
21981 }
21982
21983 if (!DW_STRING_IS_CANONICAL (attr))
21984 {
21985 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21986 objfile);
21987 DW_STRING_IS_CANONICAL (attr) = 1;
21988 }
21989 return DW_STRING (attr);
21990 }
21991
21992 /* Return the die that this die in an extension of, or NULL if there
21993 is none. *EXT_CU is the CU containing DIE on input, and the CU
21994 containing the return value on output. */
21995
21996 static struct die_info *
21997 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21998 {
21999 struct attribute *attr;
22000
22001 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22002 if (attr == NULL)
22003 return NULL;
22004
22005 return follow_die_ref (die, attr, ext_cu);
22006 }
22007
22008 static void
22009 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22010 {
22011 unsigned int i;
22012
22013 print_spaces (indent, f);
22014 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22015 dwarf_tag_name (die->tag), die->abbrev,
22016 sect_offset_str (die->sect_off));
22017
22018 if (die->parent != NULL)
22019 {
22020 print_spaces (indent, f);
22021 fprintf_unfiltered (f, " parent at offset: %s\n",
22022 sect_offset_str (die->parent->sect_off));
22023 }
22024
22025 print_spaces (indent, f);
22026 fprintf_unfiltered (f, " has children: %s\n",
22027 dwarf_bool_name (die->child != NULL));
22028
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " attributes:\n");
22031
22032 for (i = 0; i < die->num_attrs; ++i)
22033 {
22034 print_spaces (indent, f);
22035 fprintf_unfiltered (f, " %s (%s) ",
22036 dwarf_attr_name (die->attrs[i].name),
22037 dwarf_form_name (die->attrs[i].form));
22038
22039 switch (die->attrs[i].form)
22040 {
22041 case DW_FORM_addr:
22042 case DW_FORM_addrx:
22043 case DW_FORM_GNU_addr_index:
22044 fprintf_unfiltered (f, "address: ");
22045 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22046 break;
22047 case DW_FORM_block2:
22048 case DW_FORM_block4:
22049 case DW_FORM_block:
22050 case DW_FORM_block1:
22051 fprintf_unfiltered (f, "block: size %s",
22052 pulongest (DW_BLOCK (&die->attrs[i])->size));
22053 break;
22054 case DW_FORM_exprloc:
22055 fprintf_unfiltered (f, "expression: size %s",
22056 pulongest (DW_BLOCK (&die->attrs[i])->size));
22057 break;
22058 case DW_FORM_data16:
22059 fprintf_unfiltered (f, "constant of 16 bytes");
22060 break;
22061 case DW_FORM_ref_addr:
22062 fprintf_unfiltered (f, "ref address: ");
22063 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22064 break;
22065 case DW_FORM_GNU_ref_alt:
22066 fprintf_unfiltered (f, "alt ref address: ");
22067 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22068 break;
22069 case DW_FORM_ref1:
22070 case DW_FORM_ref2:
22071 case DW_FORM_ref4:
22072 case DW_FORM_ref8:
22073 case DW_FORM_ref_udata:
22074 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22075 (long) (DW_UNSND (&die->attrs[i])));
22076 break;
22077 case DW_FORM_data1:
22078 case DW_FORM_data2:
22079 case DW_FORM_data4:
22080 case DW_FORM_data8:
22081 case DW_FORM_udata:
22082 case DW_FORM_sdata:
22083 fprintf_unfiltered (f, "constant: %s",
22084 pulongest (DW_UNSND (&die->attrs[i])));
22085 break;
22086 case DW_FORM_sec_offset:
22087 fprintf_unfiltered (f, "section offset: %s",
22088 pulongest (DW_UNSND (&die->attrs[i])));
22089 break;
22090 case DW_FORM_ref_sig8:
22091 fprintf_unfiltered (f, "signature: %s",
22092 hex_string (DW_SIGNATURE (&die->attrs[i])));
22093 break;
22094 case DW_FORM_string:
22095 case DW_FORM_strp:
22096 case DW_FORM_line_strp:
22097 case DW_FORM_strx:
22098 case DW_FORM_GNU_str_index:
22099 case DW_FORM_GNU_strp_alt:
22100 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22101 DW_STRING (&die->attrs[i])
22102 ? DW_STRING (&die->attrs[i]) : "",
22103 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22104 break;
22105 case DW_FORM_flag:
22106 if (DW_UNSND (&die->attrs[i]))
22107 fprintf_unfiltered (f, "flag: TRUE");
22108 else
22109 fprintf_unfiltered (f, "flag: FALSE");
22110 break;
22111 case DW_FORM_flag_present:
22112 fprintf_unfiltered (f, "flag: TRUE");
22113 break;
22114 case DW_FORM_indirect:
22115 /* The reader will have reduced the indirect form to
22116 the "base form" so this form should not occur. */
22117 fprintf_unfiltered (f,
22118 "unexpected attribute form: DW_FORM_indirect");
22119 break;
22120 case DW_FORM_implicit_const:
22121 fprintf_unfiltered (f, "constant: %s",
22122 plongest (DW_SND (&die->attrs[i])));
22123 break;
22124 default:
22125 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22126 die->attrs[i].form);
22127 break;
22128 }
22129 fprintf_unfiltered (f, "\n");
22130 }
22131 }
22132
22133 static void
22134 dump_die_for_error (struct die_info *die)
22135 {
22136 dump_die_shallow (gdb_stderr, 0, die);
22137 }
22138
22139 static void
22140 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22141 {
22142 int indent = level * 4;
22143
22144 gdb_assert (die != NULL);
22145
22146 if (level >= max_level)
22147 return;
22148
22149 dump_die_shallow (f, indent, die);
22150
22151 if (die->child != NULL)
22152 {
22153 print_spaces (indent, f);
22154 fprintf_unfiltered (f, " Children:");
22155 if (level + 1 < max_level)
22156 {
22157 fprintf_unfiltered (f, "\n");
22158 dump_die_1 (f, level + 1, max_level, die->child);
22159 }
22160 else
22161 {
22162 fprintf_unfiltered (f,
22163 " [not printed, max nesting level reached]\n");
22164 }
22165 }
22166
22167 if (die->sibling != NULL && level > 0)
22168 {
22169 dump_die_1 (f, level, max_level, die->sibling);
22170 }
22171 }
22172
22173 /* This is called from the pdie macro in gdbinit.in.
22174 It's not static so gcc will keep a copy callable from gdb. */
22175
22176 void
22177 dump_die (struct die_info *die, int max_level)
22178 {
22179 dump_die_1 (gdb_stdlog, 0, max_level, die);
22180 }
22181
22182 static void
22183 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22184 {
22185 void **slot;
22186
22187 slot = htab_find_slot_with_hash (cu->die_hash, die,
22188 to_underlying (die->sect_off),
22189 INSERT);
22190
22191 *slot = die;
22192 }
22193
22194 /* Follow reference or signature attribute ATTR of SRC_DIE.
22195 On entry *REF_CU is the CU of SRC_DIE.
22196 On exit *REF_CU is the CU of the result. */
22197
22198 static struct die_info *
22199 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22200 struct dwarf2_cu **ref_cu)
22201 {
22202 struct die_info *die;
22203
22204 if (attr->form_is_ref ())
22205 die = follow_die_ref (src_die, attr, ref_cu);
22206 else if (attr->form == DW_FORM_ref_sig8)
22207 die = follow_die_sig (src_die, attr, ref_cu);
22208 else
22209 {
22210 dump_die_for_error (src_die);
22211 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22212 objfile_name ((*ref_cu)->per_objfile->objfile));
22213 }
22214
22215 return die;
22216 }
22217
22218 /* Follow reference OFFSET.
22219 On entry *REF_CU is the CU of the source die referencing OFFSET.
22220 On exit *REF_CU is the CU of the result.
22221 Returns NULL if OFFSET is invalid. */
22222
22223 static struct die_info *
22224 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22225 struct dwarf2_cu **ref_cu)
22226 {
22227 struct die_info temp_die;
22228 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22229 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22230
22231 gdb_assert (cu->per_cu != NULL);
22232
22233 target_cu = cu;
22234
22235 if (cu->per_cu->is_debug_types)
22236 {
22237 /* .debug_types CUs cannot reference anything outside their CU.
22238 If they need to, they have to reference a signatured type via
22239 DW_FORM_ref_sig8. */
22240 if (!cu->header.offset_in_cu_p (sect_off))
22241 return NULL;
22242 }
22243 else if (offset_in_dwz != cu->per_cu->is_dwz
22244 || !cu->header.offset_in_cu_p (sect_off))
22245 {
22246 struct dwarf2_per_cu_data *per_cu;
22247
22248 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22249 dwarf2_per_objfile);
22250
22251 /* If necessary, add it to the queue and load its DIEs. */
22252 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22253 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22254
22255 target_cu = dwarf2_per_objfile->get_cu (per_cu);
22256 }
22257 else if (cu->dies == NULL)
22258 {
22259 /* We're loading full DIEs during partial symbol reading. */
22260 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22261 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22262 language_minimal);
22263 }
22264
22265 *ref_cu = target_cu;
22266 temp_die.sect_off = sect_off;
22267
22268 if (target_cu != cu)
22269 target_cu->ancestor = cu;
22270
22271 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22272 &temp_die,
22273 to_underlying (sect_off));
22274 }
22275
22276 /* Follow reference attribute ATTR of SRC_DIE.
22277 On entry *REF_CU is the CU of SRC_DIE.
22278 On exit *REF_CU is the CU of the result. */
22279
22280 static struct die_info *
22281 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22282 struct dwarf2_cu **ref_cu)
22283 {
22284 sect_offset sect_off = attr->get_ref_die_offset ();
22285 struct dwarf2_cu *cu = *ref_cu;
22286 struct die_info *die;
22287
22288 die = follow_die_offset (sect_off,
22289 (attr->form == DW_FORM_GNU_ref_alt
22290 || cu->per_cu->is_dwz),
22291 ref_cu);
22292 if (!die)
22293 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22294 "at %s [in module %s]"),
22295 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22296 objfile_name (cu->per_objfile->objfile));
22297
22298 return die;
22299 }
22300
22301 /* See read.h. */
22302
22303 struct dwarf2_locexpr_baton
22304 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22305 dwarf2_per_cu_data *per_cu,
22306 dwarf2_per_objfile *dwarf2_per_objfile,
22307 CORE_ADDR (*get_frame_pc) (void *baton),
22308 void *baton, bool resolve_abstract_p)
22309 {
22310 struct die_info *die;
22311 struct attribute *attr;
22312 struct dwarf2_locexpr_baton retval;
22313 struct objfile *objfile = dwarf2_per_objfile->objfile;
22314
22315 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
22316 if (cu == nullptr)
22317 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22318
22319 if (cu == nullptr)
22320 {
22321 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22322 Instead just throw an error, not much else we can do. */
22323 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22324 sect_offset_str (sect_off), objfile_name (objfile));
22325 }
22326
22327 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22328 if (!die)
22329 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22330 sect_offset_str (sect_off), objfile_name (objfile));
22331
22332 attr = dwarf2_attr (die, DW_AT_location, cu);
22333 if (!attr && resolve_abstract_p
22334 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22335 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22336 {
22337 CORE_ADDR pc = (*get_frame_pc) (baton);
22338 CORE_ADDR baseaddr = objfile->text_section_offset ();
22339 struct gdbarch *gdbarch = objfile->arch ();
22340
22341 for (const auto &cand_off
22342 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22343 {
22344 struct dwarf2_cu *cand_cu = cu;
22345 struct die_info *cand
22346 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22347 if (!cand
22348 || !cand->parent
22349 || cand->parent->tag != DW_TAG_subprogram)
22350 continue;
22351
22352 CORE_ADDR pc_low, pc_high;
22353 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22354 if (pc_low == ((CORE_ADDR) -1))
22355 continue;
22356 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22357 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22358 if (!(pc_low <= pc && pc < pc_high))
22359 continue;
22360
22361 die = cand;
22362 attr = dwarf2_attr (die, DW_AT_location, cu);
22363 break;
22364 }
22365 }
22366
22367 if (!attr)
22368 {
22369 /* DWARF: "If there is no such attribute, then there is no effect.".
22370 DATA is ignored if SIZE is 0. */
22371
22372 retval.data = NULL;
22373 retval.size = 0;
22374 }
22375 else if (attr->form_is_section_offset ())
22376 {
22377 struct dwarf2_loclist_baton loclist_baton;
22378 CORE_ADDR pc = (*get_frame_pc) (baton);
22379 size_t size;
22380
22381 fill_in_loclist_baton (cu, &loclist_baton, attr);
22382
22383 retval.data = dwarf2_find_location_expression (&loclist_baton,
22384 &size, pc);
22385 retval.size = size;
22386 }
22387 else
22388 {
22389 if (!attr->form_is_block ())
22390 error (_("Dwarf Error: DIE at %s referenced in module %s "
22391 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22392 sect_offset_str (sect_off), objfile_name (objfile));
22393
22394 retval.data = DW_BLOCK (attr)->data;
22395 retval.size = DW_BLOCK (attr)->size;
22396 }
22397 retval.per_objfile = dwarf2_per_objfile;
22398 retval.per_cu = cu->per_cu;
22399
22400 dwarf2_per_objfile->age_comp_units ();
22401
22402 return retval;
22403 }
22404
22405 /* See read.h. */
22406
22407 struct dwarf2_locexpr_baton
22408 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22409 dwarf2_per_cu_data *per_cu,
22410 dwarf2_per_objfile *per_objfile,
22411 CORE_ADDR (*get_frame_pc) (void *baton),
22412 void *baton)
22413 {
22414 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22415
22416 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22417 get_frame_pc, baton);
22418 }
22419
22420 /* Write a constant of a given type as target-ordered bytes into
22421 OBSTACK. */
22422
22423 static const gdb_byte *
22424 write_constant_as_bytes (struct obstack *obstack,
22425 enum bfd_endian byte_order,
22426 struct type *type,
22427 ULONGEST value,
22428 LONGEST *len)
22429 {
22430 gdb_byte *result;
22431
22432 *len = TYPE_LENGTH (type);
22433 result = (gdb_byte *) obstack_alloc (obstack, *len);
22434 store_unsigned_integer (result, *len, byte_order, value);
22435
22436 return result;
22437 }
22438
22439 /* See read.h. */
22440
22441 const gdb_byte *
22442 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22443 dwarf2_per_cu_data *per_cu,
22444 dwarf2_per_objfile *per_objfile,
22445 obstack *obstack,
22446 LONGEST *len)
22447 {
22448 struct die_info *die;
22449 struct attribute *attr;
22450 const gdb_byte *result = NULL;
22451 struct type *type;
22452 LONGEST value;
22453 enum bfd_endian byte_order;
22454 struct objfile *objfile = per_objfile->objfile;
22455
22456 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22457 if (cu == nullptr)
22458 cu = load_cu (per_cu, per_objfile, false);
22459
22460 if (cu == nullptr)
22461 {
22462 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22463 Instead just throw an error, not much else we can do. */
22464 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22465 sect_offset_str (sect_off), objfile_name (objfile));
22466 }
22467
22468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22469 if (!die)
22470 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22471 sect_offset_str (sect_off), objfile_name (objfile));
22472
22473 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22474 if (attr == NULL)
22475 return NULL;
22476
22477 byte_order = (bfd_big_endian (objfile->obfd)
22478 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22479
22480 switch (attr->form)
22481 {
22482 case DW_FORM_addr:
22483 case DW_FORM_addrx:
22484 case DW_FORM_GNU_addr_index:
22485 {
22486 gdb_byte *tem;
22487
22488 *len = cu->header.addr_size;
22489 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22490 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22491 result = tem;
22492 }
22493 break;
22494 case DW_FORM_string:
22495 case DW_FORM_strp:
22496 case DW_FORM_strx:
22497 case DW_FORM_GNU_str_index:
22498 case DW_FORM_GNU_strp_alt:
22499 /* DW_STRING is already allocated on the objfile obstack, point
22500 directly to it. */
22501 result = (const gdb_byte *) DW_STRING (attr);
22502 *len = strlen (DW_STRING (attr));
22503 break;
22504 case DW_FORM_block1:
22505 case DW_FORM_block2:
22506 case DW_FORM_block4:
22507 case DW_FORM_block:
22508 case DW_FORM_exprloc:
22509 case DW_FORM_data16:
22510 result = DW_BLOCK (attr)->data;
22511 *len = DW_BLOCK (attr)->size;
22512 break;
22513
22514 /* The DW_AT_const_value attributes are supposed to carry the
22515 symbol's value "represented as it would be on the target
22516 architecture." By the time we get here, it's already been
22517 converted to host endianness, so we just need to sign- or
22518 zero-extend it as appropriate. */
22519 case DW_FORM_data1:
22520 type = die_type (die, cu);
22521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22522 if (result == NULL)
22523 result = write_constant_as_bytes (obstack, byte_order,
22524 type, value, len);
22525 break;
22526 case DW_FORM_data2:
22527 type = die_type (die, cu);
22528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22529 if (result == NULL)
22530 result = write_constant_as_bytes (obstack, byte_order,
22531 type, value, len);
22532 break;
22533 case DW_FORM_data4:
22534 type = die_type (die, cu);
22535 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22536 if (result == NULL)
22537 result = write_constant_as_bytes (obstack, byte_order,
22538 type, value, len);
22539 break;
22540 case DW_FORM_data8:
22541 type = die_type (die, cu);
22542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22543 if (result == NULL)
22544 result = write_constant_as_bytes (obstack, byte_order,
22545 type, value, len);
22546 break;
22547
22548 case DW_FORM_sdata:
22549 case DW_FORM_implicit_const:
22550 type = die_type (die, cu);
22551 result = write_constant_as_bytes (obstack, byte_order,
22552 type, DW_SND (attr), len);
22553 break;
22554
22555 case DW_FORM_udata:
22556 type = die_type (die, cu);
22557 result = write_constant_as_bytes (obstack, byte_order,
22558 type, DW_UNSND (attr), len);
22559 break;
22560
22561 default:
22562 complaint (_("unsupported const value attribute form: '%s'"),
22563 dwarf_form_name (attr->form));
22564 break;
22565 }
22566
22567 return result;
22568 }
22569
22570 /* See read.h. */
22571
22572 struct type *
22573 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22574 dwarf2_per_cu_data *per_cu,
22575 dwarf2_per_objfile *per_objfile)
22576 {
22577 struct die_info *die;
22578
22579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22580 if (cu == nullptr)
22581 cu = load_cu (per_cu, per_objfile, false);
22582
22583 if (cu == nullptr)
22584 return nullptr;
22585
22586 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22587 if (!die)
22588 return NULL;
22589
22590 return die_type (die, cu);
22591 }
22592
22593 /* See read.h. */
22594
22595 struct type *
22596 dwarf2_get_die_type (cu_offset die_offset,
22597 dwarf2_per_cu_data *per_cu,
22598 dwarf2_per_objfile *per_objfile)
22599 {
22600 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22601 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22602 }
22603
22604 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22605 On entry *REF_CU is the CU of SRC_DIE.
22606 On exit *REF_CU is the CU of the result.
22607 Returns NULL if the referenced DIE isn't found. */
22608
22609 static struct die_info *
22610 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22611 struct dwarf2_cu **ref_cu)
22612 {
22613 struct die_info temp_die;
22614 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22615 struct die_info *die;
22616 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22617
22618
22619 /* While it might be nice to assert sig_type->type == NULL here,
22620 we can get here for DW_AT_imported_declaration where we need
22621 the DIE not the type. */
22622
22623 /* If necessary, add it to the queue and load its DIEs. */
22624
22625 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22626 language_minimal))
22627 read_signatured_type (sig_type, dwarf2_per_objfile);
22628
22629 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
22630 gdb_assert (sig_cu != NULL);
22631 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22632 temp_die.sect_off = sig_type->type_offset_in_section;
22633 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22634 to_underlying (temp_die.sect_off));
22635 if (die)
22636 {
22637 /* For .gdb_index version 7 keep track of included TUs.
22638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22639 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22640 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22641 {
22642 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22643 }
22644
22645 *ref_cu = sig_cu;
22646 if (sig_cu != cu)
22647 sig_cu->ancestor = cu;
22648
22649 return die;
22650 }
22651
22652 return NULL;
22653 }
22654
22655 /* Follow signatured type referenced by ATTR in SRC_DIE.
22656 On entry *REF_CU is the CU of SRC_DIE.
22657 On exit *REF_CU is the CU of the result.
22658 The result is the DIE of the type.
22659 If the referenced type cannot be found an error is thrown. */
22660
22661 static struct die_info *
22662 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22663 struct dwarf2_cu **ref_cu)
22664 {
22665 ULONGEST signature = DW_SIGNATURE (attr);
22666 struct signatured_type *sig_type;
22667 struct die_info *die;
22668
22669 gdb_assert (attr->form == DW_FORM_ref_sig8);
22670
22671 sig_type = lookup_signatured_type (*ref_cu, signature);
22672 /* sig_type will be NULL if the signatured type is missing from
22673 the debug info. */
22674 if (sig_type == NULL)
22675 {
22676 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22677 " from DIE at %s [in module %s]"),
22678 hex_string (signature), sect_offset_str (src_die->sect_off),
22679 objfile_name ((*ref_cu)->per_objfile->objfile));
22680 }
22681
22682 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22683 if (die == NULL)
22684 {
22685 dump_die_for_error (src_die);
22686 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22687 " from DIE at %s [in module %s]"),
22688 hex_string (signature), sect_offset_str (src_die->sect_off),
22689 objfile_name ((*ref_cu)->per_objfile->objfile));
22690 }
22691
22692 return die;
22693 }
22694
22695 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22696 reading in and processing the type unit if necessary. */
22697
22698 static struct type *
22699 get_signatured_type (struct die_info *die, ULONGEST signature,
22700 struct dwarf2_cu *cu)
22701 {
22702 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22703 struct signatured_type *sig_type;
22704 struct dwarf2_cu *type_cu;
22705 struct die_info *type_die;
22706 struct type *type;
22707
22708 sig_type = lookup_signatured_type (cu, signature);
22709 /* sig_type will be NULL if the signatured type is missing from
22710 the debug info. */
22711 if (sig_type == NULL)
22712 {
22713 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22714 " from DIE at %s [in module %s]"),
22715 hex_string (signature), sect_offset_str (die->sect_off),
22716 objfile_name (dwarf2_per_objfile->objfile));
22717 return build_error_marker_type (cu, die);
22718 }
22719
22720 /* If we already know the type we're done. */
22721 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22722 if (type != nullptr)
22723 return type;
22724
22725 type_cu = cu;
22726 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22727 if (type_die != NULL)
22728 {
22729 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22730 is created. This is important, for example, because for c++ classes
22731 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22732 type = read_type_die (type_die, type_cu);
22733 if (type == NULL)
22734 {
22735 complaint (_("Dwarf Error: Cannot build signatured type %s"
22736 " referenced from DIE at %s [in module %s]"),
22737 hex_string (signature), sect_offset_str (die->sect_off),
22738 objfile_name (dwarf2_per_objfile->objfile));
22739 type = build_error_marker_type (cu, die);
22740 }
22741 }
22742 else
22743 {
22744 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22745 " from DIE at %s [in module %s]"),
22746 hex_string (signature), sect_offset_str (die->sect_off),
22747 objfile_name (dwarf2_per_objfile->objfile));
22748 type = build_error_marker_type (cu, die);
22749 }
22750
22751 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22752
22753 return type;
22754 }
22755
22756 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22757 reading in and processing the type unit if necessary. */
22758
22759 static struct type *
22760 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22761 struct dwarf2_cu *cu) /* ARI: editCase function */
22762 {
22763 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22764 if (attr->form_is_ref ())
22765 {
22766 struct dwarf2_cu *type_cu = cu;
22767 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22768
22769 return read_type_die (type_die, type_cu);
22770 }
22771 else if (attr->form == DW_FORM_ref_sig8)
22772 {
22773 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22774 }
22775 else
22776 {
22777 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22778
22779 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22780 " at %s [in module %s]"),
22781 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22782 objfile_name (dwarf2_per_objfile->objfile));
22783 return build_error_marker_type (cu, die);
22784 }
22785 }
22786
22787 /* Load the DIEs associated with type unit PER_CU into memory. */
22788
22789 static void
22790 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22791 dwarf2_per_objfile *per_objfile)
22792 {
22793 struct signatured_type *sig_type;
22794
22795 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22796 gdb_assert (! per_cu->type_unit_group_p ());
22797
22798 /* We have the per_cu, but we need the signatured_type.
22799 Fortunately this is an easy translation. */
22800 gdb_assert (per_cu->is_debug_types);
22801 sig_type = (struct signatured_type *) per_cu;
22802
22803 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22804
22805 read_signatured_type (sig_type, per_objfile);
22806
22807 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22808 }
22809
22810 /* Read in a signatured type and build its CU and DIEs.
22811 If the type is a stub for the real type in a DWO file,
22812 read in the real type from the DWO file as well. */
22813
22814 static void
22815 read_signatured_type (signatured_type *sig_type,
22816 dwarf2_per_objfile *per_objfile)
22817 {
22818 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22819
22820 gdb_assert (per_cu->is_debug_types);
22821 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22822
22823 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22824
22825 if (!reader.dummy_p)
22826 {
22827 struct dwarf2_cu *cu = reader.cu;
22828 const gdb_byte *info_ptr = reader.info_ptr;
22829
22830 gdb_assert (cu->die_hash == NULL);
22831 cu->die_hash =
22832 htab_create_alloc_ex (cu->header.length / 12,
22833 die_hash,
22834 die_eq,
22835 NULL,
22836 &cu->comp_unit_obstack,
22837 hashtab_obstack_allocate,
22838 dummy_obstack_deallocate);
22839
22840 if (reader.comp_unit_die->has_children)
22841 reader.comp_unit_die->child
22842 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22843 reader.comp_unit_die);
22844 cu->dies = reader.comp_unit_die;
22845 /* comp_unit_die is not stored in die_hash, no need. */
22846
22847 /* We try not to read any attributes in this function, because
22848 not all CUs needed for references have been loaded yet, and
22849 symbol table processing isn't initialized. But we have to
22850 set the CU language, or we won't be able to build types
22851 correctly. Similarly, if we do not read the producer, we can
22852 not apply producer-specific interpretation. */
22853 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22854
22855 reader.keep ();
22856 }
22857
22858 sig_type->per_cu.tu_read = 1;
22859 }
22860
22861 /* Decode simple location descriptions.
22862 Given a pointer to a dwarf block that defines a location, compute
22863 the location and return the value. If COMPUTED is non-null, it is
22864 set to true to indicate that decoding was successful, and false
22865 otherwise. If COMPUTED is null, then this function may emit a
22866 complaint. */
22867
22868 static CORE_ADDR
22869 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22870 {
22871 struct objfile *objfile = cu->per_objfile->objfile;
22872 size_t i;
22873 size_t size = blk->size;
22874 const gdb_byte *data = blk->data;
22875 CORE_ADDR stack[64];
22876 int stacki;
22877 unsigned int bytes_read, unsnd;
22878 gdb_byte op;
22879
22880 if (computed != nullptr)
22881 *computed = false;
22882
22883 i = 0;
22884 stacki = 0;
22885 stack[stacki] = 0;
22886 stack[++stacki] = 0;
22887
22888 while (i < size)
22889 {
22890 op = data[i++];
22891 switch (op)
22892 {
22893 case DW_OP_lit0:
22894 case DW_OP_lit1:
22895 case DW_OP_lit2:
22896 case DW_OP_lit3:
22897 case DW_OP_lit4:
22898 case DW_OP_lit5:
22899 case DW_OP_lit6:
22900 case DW_OP_lit7:
22901 case DW_OP_lit8:
22902 case DW_OP_lit9:
22903 case DW_OP_lit10:
22904 case DW_OP_lit11:
22905 case DW_OP_lit12:
22906 case DW_OP_lit13:
22907 case DW_OP_lit14:
22908 case DW_OP_lit15:
22909 case DW_OP_lit16:
22910 case DW_OP_lit17:
22911 case DW_OP_lit18:
22912 case DW_OP_lit19:
22913 case DW_OP_lit20:
22914 case DW_OP_lit21:
22915 case DW_OP_lit22:
22916 case DW_OP_lit23:
22917 case DW_OP_lit24:
22918 case DW_OP_lit25:
22919 case DW_OP_lit26:
22920 case DW_OP_lit27:
22921 case DW_OP_lit28:
22922 case DW_OP_lit29:
22923 case DW_OP_lit30:
22924 case DW_OP_lit31:
22925 stack[++stacki] = op - DW_OP_lit0;
22926 break;
22927
22928 case DW_OP_reg0:
22929 case DW_OP_reg1:
22930 case DW_OP_reg2:
22931 case DW_OP_reg3:
22932 case DW_OP_reg4:
22933 case DW_OP_reg5:
22934 case DW_OP_reg6:
22935 case DW_OP_reg7:
22936 case DW_OP_reg8:
22937 case DW_OP_reg9:
22938 case DW_OP_reg10:
22939 case DW_OP_reg11:
22940 case DW_OP_reg12:
22941 case DW_OP_reg13:
22942 case DW_OP_reg14:
22943 case DW_OP_reg15:
22944 case DW_OP_reg16:
22945 case DW_OP_reg17:
22946 case DW_OP_reg18:
22947 case DW_OP_reg19:
22948 case DW_OP_reg20:
22949 case DW_OP_reg21:
22950 case DW_OP_reg22:
22951 case DW_OP_reg23:
22952 case DW_OP_reg24:
22953 case DW_OP_reg25:
22954 case DW_OP_reg26:
22955 case DW_OP_reg27:
22956 case DW_OP_reg28:
22957 case DW_OP_reg29:
22958 case DW_OP_reg30:
22959 case DW_OP_reg31:
22960 stack[++stacki] = op - DW_OP_reg0;
22961 if (i < size)
22962 {
22963 if (computed == nullptr)
22964 dwarf2_complex_location_expr_complaint ();
22965 else
22966 return 0;
22967 }
22968 break;
22969
22970 case DW_OP_regx:
22971 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22972 i += bytes_read;
22973 stack[++stacki] = unsnd;
22974 if (i < size)
22975 {
22976 if (computed == nullptr)
22977 dwarf2_complex_location_expr_complaint ();
22978 else
22979 return 0;
22980 }
22981 break;
22982
22983 case DW_OP_addr:
22984 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22985 &bytes_read);
22986 i += bytes_read;
22987 break;
22988
22989 case DW_OP_const1u:
22990 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22991 i += 1;
22992 break;
22993
22994 case DW_OP_const1s:
22995 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22996 i += 1;
22997 break;
22998
22999 case DW_OP_const2u:
23000 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23001 i += 2;
23002 break;
23003
23004 case DW_OP_const2s:
23005 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23006 i += 2;
23007 break;
23008
23009 case DW_OP_const4u:
23010 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23011 i += 4;
23012 break;
23013
23014 case DW_OP_const4s:
23015 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23016 i += 4;
23017 break;
23018
23019 case DW_OP_const8u:
23020 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23021 i += 8;
23022 break;
23023
23024 case DW_OP_constu:
23025 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_consts:
23031 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_dup:
23036 stack[stacki + 1] = stack[stacki];
23037 stacki++;
23038 break;
23039
23040 case DW_OP_plus:
23041 stack[stacki - 1] += stack[stacki];
23042 stacki--;
23043 break;
23044
23045 case DW_OP_plus_uconst:
23046 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_minus:
23052 stack[stacki - 1] -= stack[stacki];
23053 stacki--;
23054 break;
23055
23056 case DW_OP_deref:
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23060 in the psymtab. */
23061 if (i < size)
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
23068 break;
23069
23070 case DW_OP_GNU_push_tls_address:
23071 case DW_OP_form_tls_address:
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23075 be returned. */
23076 /* This is valid for partial global symbols, but the variable's
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
23080 if (i < size)
23081 {
23082 if (computed == nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23084 else
23085 return 0;
23086 }
23087 stack[stacki]++;
23088 break;
23089
23090 case DW_OP_GNU_uninit:
23091 if (computed != nullptr)
23092 return 0;
23093 break;
23094
23095 case DW_OP_addrx:
23096 case DW_OP_GNU_addr_index:
23097 case DW_OP_GNU_const_index:
23098 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23099 &bytes_read);
23100 i += bytes_read;
23101 break;
23102
23103 default:
23104 if (computed == nullptr)
23105 {
23106 const char *name = get_DW_OP_name (op);
23107
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
23115
23116 return (stack[stacki]);
23117 }
23118
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
23123 if (computed == nullptr)
23124 complaint (_("location description stack overflow"));
23125 return 0;
23126 }
23127
23128 if (stacki <= 0)
23129 {
23130 if (computed == nullptr)
23131 complaint (_("location description stack underflow"));
23132 return 0;
23133 }
23134 }
23135
23136 if (computed != nullptr)
23137 *computed = true;
23138 return (stack[stacki]);
23139 }
23140
23141 /* memory allocation interface */
23142
23143 static struct dwarf_block *
23144 dwarf_alloc_block (struct dwarf2_cu *cu)
23145 {
23146 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23147 }
23148
23149 static struct die_info *
23150 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23151 {
23152 struct die_info *die;
23153 size_t size = sizeof (struct die_info);
23154
23155 if (num_attrs > 1)
23156 size += (num_attrs - 1) * sizeof (struct attribute);
23157
23158 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23159 memset (die, 0, sizeof (struct die_info));
23160 return (die);
23161 }
23162
23163 \f
23164
23165 /* Macro support. */
23166
23167 /* An overload of dwarf_decode_macros that finds the correct section
23168 and ensures it is read in before calling the other overload. */
23169
23170 static void
23171 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23172 int section_is_gnu)
23173 {
23174 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23175 struct objfile *objfile = dwarf2_per_objfile->objfile;
23176 const struct line_header *lh = cu->line_header;
23177 unsigned int offset_size = cu->header.offset_size;
23178 struct dwarf2_section_info *section;
23179 const char *section_name;
23180
23181 if (cu->dwo_unit != nullptr)
23182 {
23183 if (section_is_gnu)
23184 {
23185 section = &cu->dwo_unit->dwo_file->sections.macro;
23186 section_name = ".debug_macro.dwo";
23187 }
23188 else
23189 {
23190 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23191 section_name = ".debug_macinfo.dwo";
23192 }
23193 }
23194 else
23195 {
23196 if (section_is_gnu)
23197 {
23198 section = &dwarf2_per_objfile->per_bfd->macro;
23199 section_name = ".debug_macro";
23200 }
23201 else
23202 {
23203 section = &dwarf2_per_objfile->per_bfd->macinfo;
23204 section_name = ".debug_macinfo";
23205 }
23206 }
23207
23208 section->read (objfile);
23209 if (section->buffer == nullptr)
23210 {
23211 complaint (_("missing %s section"), section_name);
23212 return;
23213 }
23214
23215 buildsym_compunit *builder = cu->get_builder ();
23216
23217 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23218 offset_size, offset, section_is_gnu);
23219 }
23220
23221 /* Return the .debug_loc section to use for CU.
23222 For DWO files use .debug_loc.dwo. */
23223
23224 static struct dwarf2_section_info *
23225 cu_debug_loc_section (struct dwarf2_cu *cu)
23226 {
23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23228
23229 if (cu->dwo_unit)
23230 {
23231 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23232
23233 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23234 }
23235 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23236 : &dwarf2_per_objfile->per_bfd->loc);
23237 }
23238
23239 /* A helper function that fills in a dwarf2_loclist_baton. */
23240
23241 static void
23242 fill_in_loclist_baton (struct dwarf2_cu *cu,
23243 struct dwarf2_loclist_baton *baton,
23244 const struct attribute *attr)
23245 {
23246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23248
23249 section->read (dwarf2_per_objfile->objfile);
23250
23251 baton->per_objfile = dwarf2_per_objfile;
23252 baton->per_cu = cu->per_cu;
23253 gdb_assert (baton->per_cu);
23254 /* We don't know how long the location list is, but make sure we
23255 don't run off the edge of the section. */
23256 baton->size = section->size - DW_UNSND (attr);
23257 baton->data = section->buffer + DW_UNSND (attr);
23258 if (cu->base_address.has_value ())
23259 baton->base_address = *cu->base_address;
23260 else
23261 baton->base_address = 0;
23262 baton->from_dwo = cu->dwo_unit != NULL;
23263 }
23264
23265 static void
23266 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23267 struct dwarf2_cu *cu, int is_block)
23268 {
23269 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23270 struct objfile *objfile = dwarf2_per_objfile->objfile;
23271 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23272
23273 if (attr->form_is_section_offset ()
23274 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23275 the section. If so, fall through to the complaint in the
23276 other branch. */
23277 && DW_UNSND (attr) < section->get_size (objfile))
23278 {
23279 struct dwarf2_loclist_baton *baton;
23280
23281 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23282
23283 fill_in_loclist_baton (cu, baton, attr);
23284
23285 if (!cu->base_address.has_value ())
23286 complaint (_("Location list used without "
23287 "specifying the CU base address."));
23288
23289 SYMBOL_ACLASS_INDEX (sym) = (is_block
23290 ? dwarf2_loclist_block_index
23291 : dwarf2_loclist_index);
23292 SYMBOL_LOCATION_BATON (sym) = baton;
23293 }
23294 else
23295 {
23296 struct dwarf2_locexpr_baton *baton;
23297
23298 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23299 baton->per_objfile = dwarf2_per_objfile;
23300 baton->per_cu = cu->per_cu;
23301 gdb_assert (baton->per_cu);
23302
23303 if (attr->form_is_block ())
23304 {
23305 /* Note that we're just copying the block's data pointer
23306 here, not the actual data. We're still pointing into the
23307 info_buffer for SYM's objfile; right now we never release
23308 that buffer, but when we do clean up properly this may
23309 need to change. */
23310 baton->size = DW_BLOCK (attr)->size;
23311 baton->data = DW_BLOCK (attr)->data;
23312 }
23313 else
23314 {
23315 dwarf2_invalid_attrib_class_complaint ("location description",
23316 sym->natural_name ());
23317 baton->size = 0;
23318 }
23319
23320 SYMBOL_ACLASS_INDEX (sym) = (is_block
23321 ? dwarf2_locexpr_block_index
23322 : dwarf2_locexpr_index);
23323 SYMBOL_LOCATION_BATON (sym) = baton;
23324 }
23325 }
23326
23327 /* See read.h. */
23328
23329 const comp_unit_head *
23330 dwarf2_per_cu_data::get_header () const
23331 {
23332 if (!m_header_read_in)
23333 {
23334 const gdb_byte *info_ptr
23335 = this->section->buffer + to_underlying (this->sect_off);
23336
23337 memset (&m_header, 0, sizeof (m_header));
23338
23339 read_comp_unit_head (&m_header, info_ptr, this->section,
23340 rcuh_kind::COMPILE);
23341 }
23342
23343 return &m_header;
23344 }
23345
23346 /* See read.h. */
23347
23348 int
23349 dwarf2_per_cu_data::addr_size () const
23350 {
23351 return this->get_header ()->addr_size;
23352 }
23353
23354 /* See read.h. */
23355
23356 int
23357 dwarf2_per_cu_data::offset_size () const
23358 {
23359 return this->get_header ()->offset_size;
23360 }
23361
23362 /* See read.h. */
23363
23364 int
23365 dwarf2_per_cu_data::ref_addr_size () const
23366 {
23367 const comp_unit_head *header = this->get_header ();
23368
23369 if (header->version == 2)
23370 return header->addr_size;
23371 else
23372 return header->offset_size;
23373 }
23374
23375 /* See read.h. */
23376
23377 struct type *
23378 dwarf2_cu::addr_type () const
23379 {
23380 struct objfile *objfile = this->per_objfile->objfile;
23381 struct type *void_type = objfile_type (objfile)->builtin_void;
23382 struct type *addr_type = lookup_pointer_type (void_type);
23383 int addr_size = this->per_cu->addr_size ();
23384
23385 if (TYPE_LENGTH (addr_type) == addr_size)
23386 return addr_type;
23387
23388 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23389 return addr_type;
23390 }
23391
23392 /* A helper function for dwarf2_find_containing_comp_unit that returns
23393 the index of the result, and that searches a vector. It will
23394 return a result even if the offset in question does not actually
23395 occur in any CU. This is separate so that it can be unit
23396 tested. */
23397
23398 static int
23399 dwarf2_find_containing_comp_unit
23400 (sect_offset sect_off,
23401 unsigned int offset_in_dwz,
23402 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23403 {
23404 int low, high;
23405
23406 low = 0;
23407 high = all_comp_units.size () - 1;
23408 while (high > low)
23409 {
23410 struct dwarf2_per_cu_data *mid_cu;
23411 int mid = low + (high - low) / 2;
23412
23413 mid_cu = all_comp_units[mid];
23414 if (mid_cu->is_dwz > offset_in_dwz
23415 || (mid_cu->is_dwz == offset_in_dwz
23416 && mid_cu->sect_off + mid_cu->length > sect_off))
23417 high = mid;
23418 else
23419 low = mid + 1;
23420 }
23421 gdb_assert (low == high);
23422 return low;
23423 }
23424
23425 /* Locate the .debug_info compilation unit from CU's objfile which contains
23426 the DIE at OFFSET. Raises an error on failure. */
23427
23428 static struct dwarf2_per_cu_data *
23429 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23430 unsigned int offset_in_dwz,
23431 struct dwarf2_per_objfile *dwarf2_per_objfile)
23432 {
23433 int low
23434 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23435 dwarf2_per_objfile->per_bfd->all_comp_units);
23436 struct dwarf2_per_cu_data *this_cu
23437 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23438
23439 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23440 {
23441 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23442 error (_("Dwarf Error: could not find partial DIE containing "
23443 "offset %s [in module %s]"),
23444 sect_offset_str (sect_off),
23445 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23446
23447 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23448 <= sect_off);
23449 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23450 }
23451 else
23452 {
23453 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23454 && sect_off >= this_cu->sect_off + this_cu->length)
23455 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23456 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23457 return this_cu;
23458 }
23459 }
23460
23461 #if GDB_SELF_TEST
23462
23463 namespace selftests {
23464 namespace find_containing_comp_unit {
23465
23466 static void
23467 run_test ()
23468 {
23469 struct dwarf2_per_cu_data one {};
23470 struct dwarf2_per_cu_data two {};
23471 struct dwarf2_per_cu_data three {};
23472 struct dwarf2_per_cu_data four {};
23473
23474 one.length = 5;
23475 two.sect_off = sect_offset (one.length);
23476 two.length = 7;
23477
23478 three.length = 5;
23479 three.is_dwz = 1;
23480 four.sect_off = sect_offset (three.length);
23481 four.length = 7;
23482 four.is_dwz = 1;
23483
23484 std::vector<dwarf2_per_cu_data *> units;
23485 units.push_back (&one);
23486 units.push_back (&two);
23487 units.push_back (&three);
23488 units.push_back (&four);
23489
23490 int result;
23491
23492 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23493 SELF_CHECK (units[result] == &one);
23494 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23495 SELF_CHECK (units[result] == &one);
23496 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23497 SELF_CHECK (units[result] == &two);
23498
23499 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23500 SELF_CHECK (units[result] == &three);
23501 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23502 SELF_CHECK (units[result] == &three);
23503 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23504 SELF_CHECK (units[result] == &four);
23505 }
23506
23507 }
23508 }
23509
23510 #endif /* GDB_SELF_TEST */
23511
23512 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23513
23514 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23515 dwarf2_per_objfile *per_objfile)
23516 : per_cu (per_cu),
23517 per_objfile (per_objfile),
23518 mark (false),
23519 has_loclist (false),
23520 checked_producer (false),
23521 producer_is_gxx_lt_4_6 (false),
23522 producer_is_gcc_lt_4_3 (false),
23523 producer_is_icc (false),
23524 producer_is_icc_lt_14 (false),
23525 producer_is_codewarrior (false),
23526 processing_has_namespace_info (false)
23527 {
23528 }
23529
23530 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23531
23532 static void
23533 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23534 enum language pretend_language)
23535 {
23536 struct attribute *attr;
23537
23538 /* Set the language we're debugging. */
23539 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23540 if (attr != nullptr)
23541 set_cu_language (DW_UNSND (attr), cu);
23542 else
23543 {
23544 cu->language = pretend_language;
23545 cu->language_defn = language_def (cu->language);
23546 }
23547
23548 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23549 }
23550
23551 /* See read.h. */
23552
23553 dwarf2_cu *
23554 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23555 {
23556 auto it = m_dwarf2_cus.find (per_cu);
23557 if (it == m_dwarf2_cus.end ())
23558 return nullptr;
23559
23560 return it->second;
23561 }
23562
23563 /* See read.h. */
23564
23565 void
23566 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23567 {
23568 gdb_assert (this->get_cu (per_cu) == nullptr);
23569
23570 m_dwarf2_cus[per_cu] = cu;
23571 }
23572
23573 /* See read.h. */
23574
23575 void
23576 dwarf2_per_objfile::age_comp_units ()
23577 {
23578 /* Start by clearing all marks. */
23579 for (auto pair : m_dwarf2_cus)
23580 pair.second->mark = false;
23581
23582 /* Traverse all CUs, mark them and their dependencies if used recently
23583 enough. */
23584 for (auto pair : m_dwarf2_cus)
23585 {
23586 dwarf2_cu *cu = pair.second;
23587
23588 cu->last_used++;
23589 if (cu->last_used <= dwarf_max_cache_age)
23590 dwarf2_mark (cu);
23591 }
23592
23593 /* Delete all CUs still not marked. */
23594 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23595 {
23596 dwarf2_cu *cu = it->second;
23597
23598 if (!cu->mark)
23599 {
23600 delete cu;
23601 it = m_dwarf2_cus.erase (it);
23602 }
23603 else
23604 it++;
23605 }
23606 }
23607
23608 /* See read.h. */
23609
23610 void
23611 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23612 {
23613 auto it = m_dwarf2_cus.find (per_cu);
23614 if (it == m_dwarf2_cus.end ())
23615 return;
23616
23617 delete it->second;
23618
23619 m_dwarf2_cus.erase (it);
23620 }
23621
23622 dwarf2_per_objfile::~dwarf2_per_objfile ()
23623 {
23624 remove_all_cus ();
23625 }
23626
23627 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23628 We store these in a hash table separate from the DIEs, and preserve them
23629 when the DIEs are flushed out of cache.
23630
23631 The CU "per_cu" pointer is needed because offset alone is not enough to
23632 uniquely identify the type. A file may have multiple .debug_types sections,
23633 or the type may come from a DWO file. Furthermore, while it's more logical
23634 to use per_cu->section+offset, with Fission the section with the data is in
23635 the DWO file but we don't know that section at the point we need it.
23636 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23637 because we can enter the lookup routine, get_die_type_at_offset, from
23638 outside this file, and thus won't necessarily have PER_CU->cu.
23639 Fortunately, PER_CU is stable for the life of the objfile. */
23640
23641 struct dwarf2_per_cu_offset_and_type
23642 {
23643 const struct dwarf2_per_cu_data *per_cu;
23644 sect_offset sect_off;
23645 struct type *type;
23646 };
23647
23648 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23649
23650 static hashval_t
23651 per_cu_offset_and_type_hash (const void *item)
23652 {
23653 const struct dwarf2_per_cu_offset_and_type *ofs
23654 = (const struct dwarf2_per_cu_offset_and_type *) item;
23655
23656 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23657 }
23658
23659 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23660
23661 static int
23662 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23663 {
23664 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23665 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23666 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23667 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23668
23669 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23670 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23671 }
23672
23673 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23674 table if necessary. For convenience, return TYPE.
23675
23676 The DIEs reading must have careful ordering to:
23677 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23678 reading current DIE.
23679 * Not trying to dereference contents of still incompletely read in types
23680 while reading in other DIEs.
23681 * Enable referencing still incompletely read in types just by a pointer to
23682 the type without accessing its fields.
23683
23684 Therefore caller should follow these rules:
23685 * Try to fetch any prerequisite types we may need to build this DIE type
23686 before building the type and calling set_die_type.
23687 * After building type call set_die_type for current DIE as soon as
23688 possible before fetching more types to complete the current type.
23689 * Make the type as complete as possible before fetching more types. */
23690
23691 static struct type *
23692 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23693 {
23694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23695 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23696 struct objfile *objfile = dwarf2_per_objfile->objfile;
23697 struct attribute *attr;
23698 struct dynamic_prop prop;
23699
23700 /* For Ada types, make sure that the gnat-specific data is always
23701 initialized (if not already set). There are a few types where
23702 we should not be doing so, because the type-specific area is
23703 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23704 where the type-specific area is used to store the floatformat).
23705 But this is not a problem, because the gnat-specific information
23706 is actually not needed for these types. */
23707 if (need_gnat_info (cu)
23708 && type->code () != TYPE_CODE_FUNC
23709 && type->code () != TYPE_CODE_FLT
23710 && type->code () != TYPE_CODE_METHODPTR
23711 && type->code () != TYPE_CODE_MEMBERPTR
23712 && type->code () != TYPE_CODE_METHOD
23713 && !HAVE_GNAT_AUX_INFO (type))
23714 INIT_GNAT_SPECIFIC (type);
23715
23716 /* Read DW_AT_allocated and set in type. */
23717 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23718 if (attr != NULL && attr->form_is_block ())
23719 {
23720 struct type *prop_type = cu->addr_sized_int_type (false);
23721 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23722 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23723 }
23724 else if (attr != NULL)
23725 {
23726 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23727 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23728 sect_offset_str (die->sect_off));
23729 }
23730
23731 /* Read DW_AT_associated and set in type. */
23732 attr = dwarf2_attr (die, DW_AT_associated, cu);
23733 if (attr != NULL && attr->form_is_block ())
23734 {
23735 struct type *prop_type = cu->addr_sized_int_type (false);
23736 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23737 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23738 }
23739 else if (attr != NULL)
23740 {
23741 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23742 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23743 sect_offset_str (die->sect_off));
23744 }
23745
23746 /* Read DW_AT_data_location and set in type. */
23747 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23748 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23749 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23750
23751 if (dwarf2_per_objfile->die_type_hash == NULL)
23752 dwarf2_per_objfile->die_type_hash
23753 = htab_up (htab_create_alloc (127,
23754 per_cu_offset_and_type_hash,
23755 per_cu_offset_and_type_eq,
23756 NULL, xcalloc, xfree));
23757
23758 ofs.per_cu = cu->per_cu;
23759 ofs.sect_off = die->sect_off;
23760 ofs.type = type;
23761 slot = (struct dwarf2_per_cu_offset_and_type **)
23762 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23763 if (*slot)
23764 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23765 sect_offset_str (die->sect_off));
23766 *slot = XOBNEW (&objfile->objfile_obstack,
23767 struct dwarf2_per_cu_offset_and_type);
23768 **slot = ofs;
23769 return type;
23770 }
23771
23772 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23773 or return NULL if the die does not have a saved type. */
23774
23775 static struct type *
23776 get_die_type_at_offset (sect_offset sect_off,
23777 dwarf2_per_cu_data *per_cu,
23778 dwarf2_per_objfile *dwarf2_per_objfile)
23779 {
23780 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23781
23782 if (dwarf2_per_objfile->die_type_hash == NULL)
23783 return NULL;
23784
23785 ofs.per_cu = per_cu;
23786 ofs.sect_off = sect_off;
23787 slot = ((struct dwarf2_per_cu_offset_and_type *)
23788 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23789 if (slot)
23790 return slot->type;
23791 else
23792 return NULL;
23793 }
23794
23795 /* Look up the type for DIE in CU in die_type_hash,
23796 or return NULL if DIE does not have a saved type. */
23797
23798 static struct type *
23799 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23800 {
23801 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23802 }
23803
23804 /* Add a dependence relationship from CU to REF_PER_CU. */
23805
23806 static void
23807 dwarf2_add_dependence (struct dwarf2_cu *cu,
23808 struct dwarf2_per_cu_data *ref_per_cu)
23809 {
23810 void **slot;
23811
23812 if (cu->dependencies == NULL)
23813 cu->dependencies
23814 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23815 NULL, &cu->comp_unit_obstack,
23816 hashtab_obstack_allocate,
23817 dummy_obstack_deallocate);
23818
23819 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23820 if (*slot == NULL)
23821 *slot = ref_per_cu;
23822 }
23823
23824 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23825 Set the mark field in every compilation unit in the
23826 cache that we must keep because we are keeping CU.
23827
23828 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23829
23830 static int
23831 dwarf2_mark_helper (void **slot, void *data)
23832 {
23833 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23834 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23835 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23836
23837 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23838 reading of the chain. As such dependencies remain valid it is not much
23839 useful to track and undo them during QUIT cleanups. */
23840 if (cu == nullptr)
23841 return 1;
23842
23843 if (cu->mark)
23844 return 1;
23845
23846 cu->mark = true;
23847
23848 if (cu->dependencies != nullptr)
23849 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23850
23851 return 1;
23852 }
23853
23854 /* Set the mark field in CU and in every other compilation unit in the
23855 cache that we must keep because we are keeping CU. */
23856
23857 static void
23858 dwarf2_mark (struct dwarf2_cu *cu)
23859 {
23860 if (cu->mark)
23861 return;
23862
23863 cu->mark = true;
23864
23865 if (cu->dependencies != nullptr)
23866 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23867 }
23868
23869 /* Trivial hash function for partial_die_info: the hash value of a DIE
23870 is its offset in .debug_info for this objfile. */
23871
23872 static hashval_t
23873 partial_die_hash (const void *item)
23874 {
23875 const struct partial_die_info *part_die
23876 = (const struct partial_die_info *) item;
23877
23878 return to_underlying (part_die->sect_off);
23879 }
23880
23881 /* Trivial comparison function for partial_die_info structures: two DIEs
23882 are equal if they have the same offset. */
23883
23884 static int
23885 partial_die_eq (const void *item_lhs, const void *item_rhs)
23886 {
23887 const struct partial_die_info *part_die_lhs
23888 = (const struct partial_die_info *) item_lhs;
23889 const struct partial_die_info *part_die_rhs
23890 = (const struct partial_die_info *) item_rhs;
23891
23892 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23893 }
23894
23895 struct cmd_list_element *set_dwarf_cmdlist;
23896 struct cmd_list_element *show_dwarf_cmdlist;
23897
23898 static void
23899 show_check_physname (struct ui_file *file, int from_tty,
23900 struct cmd_list_element *c, const char *value)
23901 {
23902 fprintf_filtered (file,
23903 _("Whether to check \"physname\" is %s.\n"),
23904 value);
23905 }
23906
23907 void _initialize_dwarf2_read ();
23908 void
23909 _initialize_dwarf2_read ()
23910 {
23911 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23912 Set DWARF specific variables.\n\
23913 Configure DWARF variables such as the cache size."),
23914 &set_dwarf_cmdlist, "maintenance set dwarf ",
23915 0/*allow-unknown*/, &maintenance_set_cmdlist);
23916
23917 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23918 Show DWARF specific variables.\n\
23919 Show DWARF variables such as the cache size."),
23920 &show_dwarf_cmdlist, "maintenance show dwarf ",
23921 0/*allow-unknown*/, &maintenance_show_cmdlist);
23922
23923 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23924 &dwarf_max_cache_age, _("\
23925 Set the upper bound on the age of cached DWARF compilation units."), _("\
23926 Show the upper bound on the age of cached DWARF compilation units."), _("\
23927 A higher limit means that cached compilation units will be stored\n\
23928 in memory longer, and more total memory will be used. Zero disables\n\
23929 caching, which can slow down startup."),
23930 NULL,
23931 show_dwarf_max_cache_age,
23932 &set_dwarf_cmdlist,
23933 &show_dwarf_cmdlist);
23934
23935 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23936 Set debugging of the DWARF reader."), _("\
23937 Show debugging of the DWARF reader."), _("\
23938 When enabled (non-zero), debugging messages are printed during DWARF\n\
23939 reading and symtab expansion. A value of 1 (one) provides basic\n\
23940 information. A value greater than 1 provides more verbose information."),
23941 NULL,
23942 NULL,
23943 &setdebuglist, &showdebuglist);
23944
23945 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23946 Set debugging of the DWARF DIE reader."), _("\
23947 Show debugging of the DWARF DIE reader."), _("\
23948 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23949 The value is the maximum depth to print."),
23950 NULL,
23951 NULL,
23952 &setdebuglist, &showdebuglist);
23953
23954 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23955 Set debugging of the dwarf line reader."), _("\
23956 Show debugging of the dwarf line reader."), _("\
23957 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23958 A value of 1 (one) provides basic information.\n\
23959 A value greater than 1 provides more verbose information."),
23960 NULL,
23961 NULL,
23962 &setdebuglist, &showdebuglist);
23963
23964 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23965 Set cross-checking of \"physname\" code against demangler."), _("\
23966 Show cross-checking of \"physname\" code against demangler."), _("\
23967 When enabled, GDB's internal \"physname\" code is checked against\n\
23968 the demangler."),
23969 NULL, show_check_physname,
23970 &setdebuglist, &showdebuglist);
23971
23972 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23973 no_class, &use_deprecated_index_sections, _("\
23974 Set whether to use deprecated gdb_index sections."), _("\
23975 Show whether to use deprecated gdb_index sections."), _("\
23976 When enabled, deprecated .gdb_index sections are used anyway.\n\
23977 Normally they are ignored either because of a missing feature or\n\
23978 performance issue.\n\
23979 Warning: This option must be enabled before gdb reads the file."),
23980 NULL,
23981 NULL,
23982 &setlist, &showlist);
23983
23984 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23985 &dwarf2_locexpr_funcs);
23986 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23987 &dwarf2_loclist_funcs);
23988
23989 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23990 &dwarf2_block_frame_base_locexpr_funcs);
23991 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23992 &dwarf2_block_frame_base_loclist_funcs);
23993
23994 #if GDB_SELF_TEST
23995 selftests::register_test ("dw2_expand_symtabs_matching",
23996 selftests::dw2_expand_symtabs_matching::run_test);
23997 selftests::register_test ("dwarf2_find_containing_comp_unit",
23998 selftests::find_containing_comp_unit::run_test);
23999 #endif
24000 }
This page took 0.499456 seconds and 5 git commands to generate.