Move signatured_type::type to unshareable object
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
... / ...
CommitLineData
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. */
94static unsigned int dwarf_read_debug = 0;
95
96/* When non-zero, dump DIEs after they are read in. */
97static unsigned int dwarf_die_debug = 0;
98
99/* When non-zero, dump line number entries as they are read in. */
100unsigned int dwarf_line_debug = 0;
101
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
104
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
107
108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static 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
136struct 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
151struct 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. */
189protected:
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. */
195struct 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. */
233struct 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
285dwarf2_per_objfile *
286get_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
296static 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
322static 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}
337dwop_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. */
357struct 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. */
381struct 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. */
400struct dwarf2_cu
401{
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
434 /* The header of the compilation unit. */
435 struct comp_unit_head header {};
436
437 /* Base address of this compilation unit. */
438 gdb::optional<CORE_ADDR> base_address;
439
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
443
444 const char *producer = nullptr;
445
446private:
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
450
451public:
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
462
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
470
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
476
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used = 0;
485
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
488 htab_t die_hash = nullptr;
489
490 /* Full DIEs if read in. */
491 struct die_info *dies = nullptr;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies = nullptr;
497
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header = nullptr;
500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
505 die_info *line_header_die_owner = nullptr;
506
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
509 std::vector<delayed_method_info> method_list;
510
511 /* To be copied to symtab->call_site_htab. */
512 htab_t call_site_htab = nullptr;
513
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
523 struct dwo_unit *dwo_unit = nullptr;
524
525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
526 Note this value comes from the Fission stub CU/TU's DIE. */
527 gdb::optional<ULONGEST> addr_base;
528
529 /* The DW_AT_rnglists_base attribute if present.
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
557 /* Mark used when releasing cached dies. */
558 bool mark : 1;
559
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
564 bool has_loclist : 1;
565
566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
573 bool producer_is_icc : 1;
574 bool producer_is_icc_lt_14 : 1;
575 bool producer_is_codewarrior : 1;
576
577 /* When true, the file that we're processing is known to have
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
581 bool processing_has_namespace_info : 1;
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
602};
603
604/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607struct stmt_list_hash
608{
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
613 sect_offset line_sect_off;
614};
615
616/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
617 an object of this type. This contains elements of type unit groups
618 that can be shared across objfiles. The non-shareable parts are in
619 type_unit_group_unshareable. */
620
621struct type_unit_group
622{
623 /* dwarf2read.c's main "handle" on a TU symtab.
624 To simplify things we create an artificial CU that "includes" all the
625 type units using this stmt_list so that the rest of the code still has
626 a "per_cu" handle on the symtab. */
627 struct dwarf2_per_cu_data per_cu;
628
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
632 std::vector<signatured_type *> *tus;
633
634 /* The data used to construct the hash key. */
635 struct stmt_list_hash hash;
636};
637
638/* These sections are what may appear in a (real or virtual) DWO file. */
639
640struct dwo_sections
641{
642 struct dwarf2_section_info abbrev;
643 struct dwarf2_section_info line;
644 struct dwarf2_section_info loc;
645 struct dwarf2_section_info loclists;
646 struct dwarf2_section_info macinfo;
647 struct dwarf2_section_info macro;
648 struct dwarf2_section_info str;
649 struct dwarf2_section_info str_offsets;
650 /* In the case of a virtual DWO file, these two are unused. */
651 struct dwarf2_section_info info;
652 std::vector<dwarf2_section_info> types;
653};
654
655/* CUs/TUs in DWP/DWO files. */
656
657struct dwo_unit
658{
659 /* Backlink to the containing struct dwo_file. */
660 struct dwo_file *dwo_file;
661
662 /* The "id" that distinguishes this CU/TU.
663 .debug_info calls this "dwo_id", .debug_types calls this "signature".
664 Since signatures came first, we stick with it for consistency. */
665 ULONGEST signature;
666
667 /* The section this CU/TU lives in, in the DWO file. */
668 struct dwarf2_section_info *section;
669
670 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
671 sect_offset sect_off;
672 unsigned int length;
673
674 /* For types, offset in the type's DIE of the type defined by this TU. */
675 cu_offset type_offset_in_tu;
676};
677
678/* include/dwarf2.h defines the DWP section codes.
679 It defines a max value but it doesn't define a min value, which we
680 use for error checking, so provide one. */
681
682enum dwp_v2_section_ids
683{
684 DW_SECT_MIN = 1
685};
686
687/* Data for one DWO file.
688
689 This includes virtual DWO files (a virtual DWO file is a DWO file as it
690 appears in a DWP file). DWP files don't really have DWO files per se -
691 comdat folding of types "loses" the DWO file they came from, and from
692 a high level view DWP files appear to contain a mass of random types.
693 However, to maintain consistency with the non-DWP case we pretend DWP
694 files contain virtual DWO files, and we assign each TU with one virtual
695 DWO file (generally based on the line and abbrev section offsets -
696 a heuristic that seems to work in practice). */
697
698struct dwo_file
699{
700 dwo_file () = default;
701 DISABLE_COPY_AND_ASSIGN (dwo_file);
702
703 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
704 For virtual DWO files the name is constructed from the section offsets
705 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
706 from related CU+TUs. */
707 const char *dwo_name = nullptr;
708
709 /* The DW_AT_comp_dir attribute. */
710 const char *comp_dir = nullptr;
711
712 /* The bfd, when the file is open. Otherwise this is NULL.
713 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
714 gdb_bfd_ref_ptr dbfd;
715
716 /* The sections that make up this DWO file.
717 Remember that for virtual DWO files in DWP V2, these are virtual
718 sections (for lack of a better name). */
719 struct dwo_sections sections {};
720
721 /* The CUs in the file.
722 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
723 an extension to handle LLVM's Link Time Optimization output (where
724 multiple source files may be compiled into a single object/dwo pair). */
725 htab_up cus;
726
727 /* Table of TUs in the file.
728 Each element is a struct dwo_unit. */
729 htab_up tus;
730};
731
732/* These sections are what may appear in a DWP file. */
733
734struct dwp_sections
735{
736 /* These are used by both DWP version 1 and 2. */
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info cu_index;
739 struct dwarf2_section_info tu_index;
740
741 /* These are only used by DWP version 2 files.
742 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
743 sections are referenced by section number, and are not recorded here.
744 In DWP version 2 there is at most one copy of all these sections, each
745 section being (effectively) comprised of the concatenation of all of the
746 individual sections that exist in the version 1 format.
747 To keep the code simple we treat each of these concatenated pieces as a
748 section itself (a virtual section?). */
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info info;
751 struct dwarf2_section_info line;
752 struct dwarf2_section_info loc;
753 struct dwarf2_section_info macinfo;
754 struct dwarf2_section_info macro;
755 struct dwarf2_section_info str_offsets;
756 struct dwarf2_section_info types;
757};
758
759/* These sections are what may appear in a virtual DWO file in DWP version 1.
760 A virtual DWO file is a DWO file as it appears in a DWP file. */
761
762struct virtual_v1_dwo_sections
763{
764 struct dwarf2_section_info abbrev;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info macinfo;
768 struct dwarf2_section_info macro;
769 struct dwarf2_section_info str_offsets;
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 struct dwarf2_section_info info_or_types;
773};
774
775/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
776 In version 2, the sections of the DWO files are concatenated together
777 and stored in one section of that name. Thus each ELF section contains
778 several "virtual" sections. */
779
780struct virtual_v2_dwo_sections
781{
782 bfd_size_type abbrev_offset;
783 bfd_size_type abbrev_size;
784
785 bfd_size_type line_offset;
786 bfd_size_type line_size;
787
788 bfd_size_type loc_offset;
789 bfd_size_type loc_size;
790
791 bfd_size_type macinfo_offset;
792 bfd_size_type macinfo_size;
793
794 bfd_size_type macro_offset;
795 bfd_size_type macro_size;
796
797 bfd_size_type str_offsets_offset;
798 bfd_size_type str_offsets_size;
799
800 /* Each DWP hash table entry records one CU or one TU.
801 That is recorded here, and copied to dwo_unit.section. */
802 bfd_size_type info_or_types_offset;
803 bfd_size_type info_or_types_size;
804};
805
806/* Contents of DWP hash tables. */
807
808struct dwp_hash_table
809{
810 uint32_t version, nr_columns;
811 uint32_t nr_units, nr_slots;
812 const gdb_byte *hash_table, *unit_table;
813 union
814 {
815 struct
816 {
817 const gdb_byte *indices;
818 } v1;
819 struct
820 {
821 /* This is indexed by column number and gives the id of the section
822 in that column. */
823#define MAX_NR_V2_DWO_SECTIONS \
824 (1 /* .debug_info or .debug_types */ \
825 + 1 /* .debug_abbrev */ \
826 + 1 /* .debug_line */ \
827 + 1 /* .debug_loc */ \
828 + 1 /* .debug_str_offsets */ \
829 + 1 /* .debug_macro or .debug_macinfo */)
830 int section_ids[MAX_NR_V2_DWO_SECTIONS];
831 const gdb_byte *offsets;
832 const gdb_byte *sizes;
833 } v2;
834 } section_pool;
835};
836
837/* Data for one DWP file. */
838
839struct dwp_file
840{
841 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
842 : name (name_),
843 dbfd (std::move (abfd))
844 {
845 }
846
847 /* Name of the file. */
848 const char *name;
849
850 /* File format version. */
851 int version = 0;
852
853 /* The bfd. */
854 gdb_bfd_ref_ptr dbfd;
855
856 /* Section info for this file. */
857 struct dwp_sections sections {};
858
859 /* Table of CUs in the file. */
860 const struct dwp_hash_table *cus = nullptr;
861
862 /* Table of TUs in the file. */
863 const struct dwp_hash_table *tus = nullptr;
864
865 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
866 htab_up loaded_cus;
867 htab_up loaded_tus;
868
869 /* Table to map ELF section numbers to their sections.
870 This is only needed for the DWP V1 file format. */
871 unsigned int num_sections = 0;
872 asection **elf_sections = nullptr;
873};
874
875/* Struct used to pass misc. parameters to read_die_and_children, et
876 al. which are used for both .debug_info and .debug_types dies.
877 All parameters here are unchanging for the life of the call. This
878 struct exists to abstract away the constant parameters of die reading. */
879
880struct die_reader_specs
881{
882 /* The bfd of die_section. */
883 bfd* abfd;
884
885 /* The CU of the DIE we are parsing. */
886 struct dwarf2_cu *cu;
887
888 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
889 struct dwo_file *dwo_file;
890
891 /* The section the die comes from.
892 This is either .debug_info or .debug_types, or the .dwo variants. */
893 struct dwarf2_section_info *die_section;
894
895 /* die_section->buffer. */
896 const gdb_byte *buffer;
897
898 /* The end of the buffer. */
899 const gdb_byte *buffer_end;
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
903};
904
905/* A subclass of die_reader_specs that holds storage and has complex
906 constructor and destructor behavior. */
907
908class cutu_reader : public die_reader_specs
909{
910public:
911
912 cutu_reader (dwarf2_per_cu_data *this_cu,
913 dwarf2_per_objfile *per_objfile,
914 struct abbrev_table *abbrev_table,
915 int use_existing_cu,
916 bool skip_partial);
917
918 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
919 dwarf2_per_objfile *per_objfile,
920 struct dwarf2_cu *parent_cu = nullptr,
921 struct dwo_file *dwo_file = nullptr);
922
923 DISABLE_COPY_AND_ASSIGN (cutu_reader);
924
925 const gdb_byte *info_ptr = nullptr;
926 struct die_info *comp_unit_die = nullptr;
927 bool dummy_p = false;
928
929 /* Release the new CU, putting it on the chain. This cannot be done
930 for dummy CUs. */
931 void keep ();
932
933private:
934 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
935 dwarf2_per_objfile *per_objfile,
936 int use_existing_cu);
937
938 struct dwarf2_per_cu_data *m_this_cu;
939 std::unique_ptr<dwarf2_cu> m_new_cu;
940
941 /* The ordinary abbreviation table. */
942 abbrev_table_up m_abbrev_table_holder;
943
944 /* The DWO abbreviation table. */
945 abbrev_table_up m_dwo_abbrev_table;
946};
947
948/* When we construct a partial symbol table entry we only
949 need this much information. */
950struct partial_die_info : public allocate_on_obstack
951 {
952 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
953
954 /* Disable assign but still keep copy ctor, which is needed
955 load_partial_dies. */
956 partial_die_info& operator=(const partial_die_info& rhs) = delete;
957
958 /* Adjust the partial die before generating a symbol for it. This
959 function may set the is_external flag or change the DIE's
960 name. */
961 void fixup (struct dwarf2_cu *cu);
962
963 /* Read a minimal amount of information into the minimal die
964 structure. */
965 const gdb_byte *read (const struct die_reader_specs *reader,
966 const struct abbrev_info &abbrev,
967 const gdb_byte *info_ptr);
968
969 /* Offset of this DIE. */
970 const sect_offset sect_off;
971
972 /* DWARF-2 tag for this DIE. */
973 const ENUM_BITFIELD(dwarf_tag) tag : 16;
974
975 /* Assorted flags describing the data found in this DIE. */
976 const unsigned int has_children : 1;
977
978 unsigned int is_external : 1;
979 unsigned int is_declaration : 1;
980 unsigned int has_type : 1;
981 unsigned int has_specification : 1;
982 unsigned int has_pc_info : 1;
983 unsigned int may_be_inlined : 1;
984
985 /* This DIE has been marked DW_AT_main_subprogram. */
986 unsigned int main_subprogram : 1;
987
988 /* Flag set if the SCOPE field of this structure has been
989 computed. */
990 unsigned int scope_set : 1;
991
992 /* Flag set if the DIE has a byte_size attribute. */
993 unsigned int has_byte_size : 1;
994
995 /* Flag set if the DIE has a DW_AT_const_value attribute. */
996 unsigned int has_const_value : 1;
997
998 /* Flag set if any of the DIE's children are template arguments. */
999 unsigned int has_template_arguments : 1;
1000
1001 /* Flag set if fixup has been called on this die. */
1002 unsigned int fixup_called : 1;
1003
1004 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1005 unsigned int is_dwz : 1;
1006
1007 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1008 unsigned int spec_is_dwz : 1;
1009
1010 /* The name of this DIE. Normally the value of DW_AT_name, but
1011 sometimes a default name for unnamed DIEs. */
1012 const char *name = nullptr;
1013
1014 /* The linkage name, if present. */
1015 const char *linkage_name = nullptr;
1016
1017 /* The scope to prepend to our children. This is generally
1018 allocated on the comp_unit_obstack, so will disappear
1019 when this compilation unit leaves the cache. */
1020 const char *scope = nullptr;
1021
1022 /* Some data associated with the partial DIE. The tag determines
1023 which field is live. */
1024 union
1025 {
1026 /* The location description associated with this DIE, if any. */
1027 struct dwarf_block *locdesc;
1028 /* The offset of an import, for DW_TAG_imported_unit. */
1029 sect_offset sect_off;
1030 } d {};
1031
1032 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1033 CORE_ADDR lowpc = 0;
1034 CORE_ADDR highpc = 0;
1035
1036 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1037 DW_AT_sibling, if any. */
1038 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1039 could return DW_AT_sibling values to its caller load_partial_dies. */
1040 const gdb_byte *sibling = nullptr;
1041
1042 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1043 DW_AT_specification (or DW_AT_abstract_origin or
1044 DW_AT_extension). */
1045 sect_offset spec_offset {};
1046
1047 /* Pointers to this DIE's parent, first child, and next sibling,
1048 if any. */
1049 struct partial_die_info *die_parent = nullptr;
1050 struct partial_die_info *die_child = nullptr;
1051 struct partial_die_info *die_sibling = nullptr;
1052
1053 friend struct partial_die_info *
1054 dwarf2_cu::find_partial_die (sect_offset sect_off);
1055
1056 private:
1057 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1058 partial_die_info (sect_offset sect_off)
1059 : partial_die_info (sect_off, DW_TAG_padding, 0)
1060 {
1061 }
1062
1063 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1064 int has_children_)
1065 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1066 {
1067 is_external = 0;
1068 is_declaration = 0;
1069 has_type = 0;
1070 has_specification = 0;
1071 has_pc_info = 0;
1072 may_be_inlined = 0;
1073 main_subprogram = 0;
1074 scope_set = 0;
1075 has_byte_size = 0;
1076 has_const_value = 0;
1077 has_template_arguments = 0;
1078 fixup_called = 0;
1079 is_dwz = 0;
1080 spec_is_dwz = 0;
1081 }
1082 };
1083
1084/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1085 but this would require a corresponding change in unpack_field_as_long
1086 and friends. */
1087static int bits_per_byte = 8;
1088
1089struct variant_part_builder;
1090
1091/* When reading a variant, we track a bit more information about the
1092 field, and store it in an object of this type. */
1093
1094struct variant_field
1095{
1096 int first_field = -1;
1097 int last_field = -1;
1098
1099 /* A variant can contain other variant parts. */
1100 std::vector<variant_part_builder> variant_parts;
1101
1102 /* If we see a DW_TAG_variant, then this will be set if this is the
1103 default branch. */
1104 bool default_branch = false;
1105 /* If we see a DW_AT_discr_value, then this will be the discriminant
1106 value. */
1107 ULONGEST discriminant_value = 0;
1108 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1109 data. */
1110 struct dwarf_block *discr_list_data = nullptr;
1111};
1112
1113/* This represents a DW_TAG_variant_part. */
1114
1115struct variant_part_builder
1116{
1117 /* The offset of the discriminant field. */
1118 sect_offset discriminant_offset {};
1119
1120 /* Variants that are direct children of this variant part. */
1121 std::vector<variant_field> variants;
1122
1123 /* True if we're currently reading a variant. */
1124 bool processing_variant = false;
1125};
1126
1127struct nextfield
1128{
1129 int accessibility = 0;
1130 int virtuality = 0;
1131 /* Variant parts need to find the discriminant, which is a DIE
1132 reference. We track the section offset of each field to make
1133 this link. */
1134 sect_offset offset;
1135 struct field field {};
1136};
1137
1138struct fnfieldlist
1139{
1140 const char *name = nullptr;
1141 std::vector<struct fn_field> fnfields;
1142};
1143
1144/* The routines that read and process dies for a C struct or C++ class
1145 pass lists of data member fields and lists of member function fields
1146 in an instance of a field_info structure, as defined below. */
1147struct field_info
1148 {
1149 /* List of data member and baseclasses fields. */
1150 std::vector<struct nextfield> fields;
1151 std::vector<struct nextfield> baseclasses;
1152
1153 /* Set if the accessibility of one of the fields is not public. */
1154 int non_public_fields = 0;
1155
1156 /* Member function fieldlist array, contains name of possibly overloaded
1157 member function, number of overloaded member functions and a pointer
1158 to the head of the member function field chain. */
1159 std::vector<struct fnfieldlist> fnfieldlists;
1160
1161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1162 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1163 std::vector<struct decl_field> typedef_field_list;
1164
1165 /* Nested types defined by this class and the number of elements in this
1166 list. */
1167 std::vector<struct decl_field> nested_types_list;
1168
1169 /* If non-null, this is the variant part we are currently
1170 reading. */
1171 variant_part_builder *current_variant_part = nullptr;
1172 /* This holds all the top-level variant parts attached to the type
1173 we're reading. */
1174 std::vector<variant_part_builder> variant_parts;
1175
1176 /* Return the total number of fields (including baseclasses). */
1177 int nfields () const
1178 {
1179 return fields.size () + baseclasses.size ();
1180 }
1181 };
1182
1183/* Loaded secondary compilation units are kept in memory until they
1184 have not been referenced for the processing of this many
1185 compilation units. Set this to zero to disable caching. Cache
1186 sizes of up to at least twenty will improve startup time for
1187 typical inter-CU-reference binaries, at an obvious memory cost. */
1188static int dwarf_max_cache_age = 5;
1189static void
1190show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1191 struct cmd_list_element *c, const char *value)
1192{
1193 fprintf_filtered (file, _("The upper bound on the age of cached "
1194 "DWARF compilation units is %s.\n"),
1195 value);
1196}
1197\f
1198/* local function prototypes */
1199
1200static void dwarf2_find_base_address (struct die_info *die,
1201 struct dwarf2_cu *cu);
1202
1203static dwarf2_psymtab *create_partial_symtab
1204 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1205 const char *name);
1206
1207static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
1209 struct die_info *type_unit_die);
1210
1211static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1213
1214static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
1216 int, struct dwarf2_cu *);
1217
1218static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
1220
1221static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1223 int set_addrmap, struct dwarf2_cu *cu);
1224
1225static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1226 CORE_ADDR *highpc, int set_addrmap,
1227 struct dwarf2_cu *cu);
1228
1229static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
1231
1232static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1234 int need_pc, struct dwarf2_cu *cu);
1235
1236static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1237
1238static struct partial_die_info *load_partial_dies
1239 (const struct die_reader_specs *, const gdb_byte *, int);
1240
1241/* A pair of partial_die_info and compilation unit. */
1242struct cu_partial_die_info
1243{
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
1252 { /* Nothing. */ }
1253
1254private:
1255 cu_partial_die_info () = delete;
1256};
1257
1258static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
1260
1261static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
1263 const gdb_byte *, bool *need_reprocess);
1264
1265static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1269
1270static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
1273
1274static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
1277
1278static const char *read_indirect_string_at_offset
1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1280
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
1283 unsigned int *);
1284
1285static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
1290
1291static void set_cu_language (unsigned int, struct dwarf2_cu *);
1292
1293static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
1295
1296static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
1299static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
1301static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
1304static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1305
1306static struct die_info *die_specification (struct die_info *die,
1307 struct dwarf2_cu **);
1308
1309static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1310 struct dwarf2_cu *cu);
1311
1312static void dwarf_decode_lines (struct line_header *, const char *,
1313 struct dwarf2_cu *, dwarf2_psymtab *,
1314 CORE_ADDR, int decode_mapping);
1315
1316static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
1318
1319static struct symbol *new_symbol (struct die_info *, struct type *,
1320 struct dwarf2_cu *, struct symbol * = NULL);
1321
1322static void dwarf2_const_value (const struct attribute *, struct symbol *,
1323 struct dwarf2_cu *);
1324
1325static void dwarf2_const_value_attr (const struct attribute *attr,
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
1329 struct dwarf2_cu *cu, LONGEST *value,
1330 const gdb_byte **bytes,
1331 struct dwarf2_locexpr_baton **baton);
1332
1333static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1334
1335static int need_gnat_info (struct dwarf2_cu *);
1336
1337static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
1339
1340static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
1343static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
1345
1346static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1347 struct dwarf2_cu *);
1348
1349static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1350
1351static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
1353static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1354
1355static char *typename_concat (struct obstack *obs, const char *prefix,
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
1358
1359static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1360
1361static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
1363static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1364
1365static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1366
1367static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
1369static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
1371static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1372 struct dwarf2_cu *, dwarf2_psymtab *);
1373
1374/* Return the .debug_loclists section to use for cu. */
1375static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
1377/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1378 values. Keep the items ordered with increasing constraints compliance. */
1379enum pc_bounds_kind
1380{
1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1382 PC_BOUNDS_NOT_PRESENT,
1383
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393};
1394
1395static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
1398 dwarf2_psymtab *);
1399
1400static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
1404static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
1407static void dwarf2_add_field (struct field_info *, struct die_info *,
1408 struct dwarf2_cu *);
1409
1410static void dwarf2_attach_fields_to_type (struct field_info *,
1411 struct type *, struct dwarf2_cu *);
1412
1413static void dwarf2_add_member_fn (struct field_info *,
1414 struct die_info *, struct type *,
1415 struct dwarf2_cu *);
1416
1417static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1418 struct type *,
1419 struct dwarf2_cu *);
1420
1421static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1422
1423static void read_common_block (struct die_info *, struct dwarf2_cu *);
1424
1425static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1426
1427static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
1429static struct using_direct **using_directives (struct dwarf2_cu *cu);
1430
1431static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
1433static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
1435static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
1438static const char *namespace_name (struct die_info *die,
1439 int *is_anonymous, struct dwarf2_cu *);
1440
1441static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1442
1443static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
1445
1446static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1447 struct dwarf2_cu *);
1448
1449static struct die_info *read_die_and_siblings_1
1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1451 struct die_info *);
1452
1453static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
1456 struct die_info *parent);
1457
1458static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
1460 int);
1461
1462static const gdb_byte *read_full_die (const struct die_reader_specs *,
1463 struct die_info **, const gdb_byte *);
1464
1465static void process_die (struct die_info *, struct dwarf2_cu *);
1466
1467static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1468 struct objfile *);
1469
1470static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1471
1472static const char *dwarf2_full_name (const char *name,
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
1476static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479static struct die_info *dwarf2_extension (struct die_info *die,
1480 struct dwarf2_cu **);
1481
1482static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484static void dump_die_for_error (struct die_info *);
1485
1486static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
1488
1489/*static*/ void dump_die (struct die_info *, int max_level);
1490
1491static void store_in_ref_table (struct die_info *,
1492 struct dwarf2_cu *);
1493
1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498static struct die_info *follow_die_ref (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502static struct die_info *follow_die_sig (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu **);
1505
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
1510 const struct attribute *,
1511 struct dwarf2_cu *);
1512
1513static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1514 dwarf2_per_objfile *per_objfile);
1515
1516static void read_signatured_type (signatured_type *sig_type,
1517 dwarf2_per_objfile *per_objfile);
1518
1519static int attr_to_dynamic_prop (const struct attribute *attr,
1520 struct die_info *die, struct dwarf2_cu *cu,
1521 struct dynamic_prop *prop, struct type *type);
1522
1523/* memory allocation interface */
1524
1525static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1526
1527static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1528
1529static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1530
1531static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1532 struct dwarf2_loclist_baton *baton,
1533 const struct attribute *attr);
1534
1535static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1536 struct symbol *sym,
1537 struct dwarf2_cu *cu,
1538 int is_block);
1539
1540static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct abbrev_info *abbrev);
1543
1544static hashval_t partial_die_hash (const void *item);
1545
1546static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1547
1548static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1549 (sect_offset sect_off, unsigned int offset_in_dwz,
1550 struct dwarf2_per_objfile *dwarf2_per_objfile);
1551
1552static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1553 struct die_info *comp_unit_die,
1554 enum language pretend_language);
1555
1556static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1557
1558static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1559 dwarf2_per_objfile *per_objfile);
1560
1561static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1563
1564static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1565
1566static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
1572
1573static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 enum language pretend_language);
1576
1577static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1578 dwarf2_per_objfile *per_objfile,
1579 enum language pretend_language);
1580
1581static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
1584static void dwarf2_mark (struct dwarf2_cu *);
1585
1586static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
1588static struct type *get_die_type_at_offset (sect_offset,
1589 dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile);
1591
1592static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1593
1594static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1595 dwarf2_per_objfile *per_objfile,
1596 enum language pretend_language);
1597
1598static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1599
1600/* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605class dwarf2_queue_guard
1606{
1607public:
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
1619 std::swap (m_per_objfile->per_bfd->queue, empty);
1620 }
1621
1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1623
1624private:
1625 dwarf2_per_objfile *m_per_objfile;
1626};
1627
1628dwarf2_queue_item::~dwarf2_queue_item ()
1629{
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
1635 free_one_cached_comp_unit (per_cu, per_objfile);
1636 per_cu->queued = 0;
1637 }
1638}
1639
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662static htab_up allocate_signatured_type_table ();
1663
1664static htab_up allocate_dwo_unit_table ();
1665
1666static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
1669 ULONGEST signature, int is_debug_types);
1670
1671static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674static struct dwo_unit *lookup_dwo_comp_unit
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
1677
1678static struct dwo_unit *lookup_dwo_type_unit
1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1680
1681static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
1683/* A unique pointer to a dwo_file. */
1684
1685typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1686
1687static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1688
1689static void check_producer (struct dwarf2_cu *cu);
1690
1691static void free_line_header_voidp (void *arg);
1692\f
1693/* Various complaints about symbol reading that don't abort the process. */
1694
1695static void
1696dwarf2_debug_line_missing_file_complaint (void)
1697{
1698 complaint (_(".debug_line section has line data without a file"));
1699}
1700
1701static void
1702dwarf2_debug_line_missing_end_sequence_complaint (void)
1703{
1704 complaint (_(".debug_line section has line "
1705 "program sequence without an end"));
1706}
1707
1708static void
1709dwarf2_complex_location_expr_complaint (void)
1710{
1711 complaint (_("location expression too complex"));
1712}
1713
1714static void
1715dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717{
1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1719 arg1, arg2, arg3);
1720}
1721
1722static void
1723dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724{
1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1726 arg1, arg2);
1727}
1728
1729/* Hash function for line_header_hash. */
1730
1731static hashval_t
1732line_header_hash (const struct line_header *ofs)
1733{
1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1735}
1736
1737/* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash_voidp (const void *item)
1741{
1742 const struct line_header *ofs = (const struct line_header *) item;
1743
1744 return line_header_hash (ofs);
1745}
1746
1747/* Equality function for line_header_hash. */
1748
1749static int
1750line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751{
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1754
1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757}
1758
1759\f
1760
1761/* See declaration. */
1762
1763dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
1765 : obfd (obfd),
1766 can_copy (can_copy_)
1767{
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773}
1774
1775dwarf2_per_bfd::~dwarf2_per_bfd ()
1776{
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1781 per_cu->imported_symtabs_free ();
1782
1783 for (signatured_type *sig_type : all_type_units)
1784 sig_type->per_cu.imported_symtabs_free ();
1785
1786 /* Everything else should be on this->obstack. */
1787}
1788
1789/* See declaration. */
1790
1791void
1792dwarf2_per_bfd::free_cached_comp_units ()
1793{
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
1800 delete per_cu->cu;
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804}
1805
1806/* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809class free_cached_comp_units
1810{
1811public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
1820 m_per_objfile->per_bfd->free_cached_comp_units ();
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828};
1829
1830/* See read.h. */
1831
1832bool
1833dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838}
1839
1840/* See read.h. */
1841
1842compunit_symtab *
1843dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848}
1849
1850/* See read.h. */
1851
1852void
1853dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855{
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860}
1861
1862/* Try to locate the sections we need for DWARF 2 debugging
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
1868
1869int
1870dwarf2_has_info (struct objfile *objfile,
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
1873{
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
1888
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1893}
1894
1895/* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
1897
1898static int
1899section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
1901{
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
1909}
1910
1911/* See declaration. */
1912
1913void
1914dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
1916{
1917 flagword aflag = bfd_section_flags (sectp);
1918
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
1931 else if (section_is_p (sectp->name, &names.info))
1932 {
1933 this->info.s.section = sectp;
1934 this->info.size = bfd_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names.abbrev))
1937 {
1938 this->abbrev.s.section = sectp;
1939 this->abbrev.size = bfd_section_size (sectp);
1940 }
1941 else if (section_is_p (sectp->name, &names.line))
1942 {
1943 this->line.s.section = sectp;
1944 this->line.size = bfd_section_size (sectp);
1945 }
1946 else if (section_is_p (sectp->name, &names.loc))
1947 {
1948 this->loc.s.section = sectp;
1949 this->loc.size = bfd_section_size (sectp);
1950 }
1951 else if (section_is_p (sectp->name, &names.loclists))
1952 {
1953 this->loclists.s.section = sectp;
1954 this->loclists.size = bfd_section_size (sectp);
1955 }
1956 else if (section_is_p (sectp->name, &names.macinfo))
1957 {
1958 this->macinfo.s.section = sectp;
1959 this->macinfo.size = bfd_section_size (sectp);
1960 }
1961 else if (section_is_p (sectp->name, &names.macro))
1962 {
1963 this->macro.s.section = sectp;
1964 this->macro.size = bfd_section_size (sectp);
1965 }
1966 else if (section_is_p (sectp->name, &names.str))
1967 {
1968 this->str.s.section = sectp;
1969 this->str.size = bfd_section_size (sectp);
1970 }
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &names.line_str))
1977 {
1978 this->line_str.s.section = sectp;
1979 this->line_str.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &names.addr))
1982 {
1983 this->addr.s.section = sectp;
1984 this->addr.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &names.frame))
1987 {
1988 this->frame.s.section = sectp;
1989 this->frame.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &names.eh_frame))
1992 {
1993 this->eh_frame.s.section = sectp;
1994 this->eh_frame.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &names.ranges))
1997 {
1998 this->ranges.s.section = sectp;
1999 this->ranges.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &names.rnglists))
2002 {
2003 this->rnglists.s.section = sectp;
2004 this->rnglists.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &names.types))
2007 {
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
2011 type_section.s.section = sectp;
2012 type_section.size = bfd_section_size (sectp);
2013
2014 this->types.push_back (type_section);
2015 }
2016 else if (section_is_p (sectp->name, &names.gdb_index))
2017 {
2018 this->gdb_index.s.section = sectp;
2019 this->gdb_index.size = bfd_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
2024 this->debug_names.size = bfd_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
2029 this->debug_aranges.size = bfd_section_size (sectp);
2030 }
2031
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
2034 this->has_section_at_zero = true;
2035}
2036
2037/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2038 SECTION_NAME. */
2039
2040void
2041dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
2043 asection **sectp, const gdb_byte **bufp,
2044 bfd_size_type *sizep)
2045{
2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2047 struct dwarf2_section_info *info;
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
2061 info = &data->per_bfd->frame;
2062 break;
2063 case DWARF2_EH_FRAME:
2064 info = &data->per_bfd->eh_frame;
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
2069
2070 info->read (objfile);
2071
2072 *sectp = info->get_bfd_section ();
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075}
2076
2077/* A helper function to find the sections for a .dwz file. */
2078
2079static void
2080locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081{
2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
2088 dwz_file->abbrev.s.section = sectp;
2089 dwz_file->abbrev.size = bfd_section_size (sectp);
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
2093 dwz_file->info.s.section = sectp;
2094 dwz_file->info.size = bfd_section_size (sectp);
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
2098 dwz_file->str.s.section = sectp;
2099 dwz_file->str.size = bfd_section_size (sectp);
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
2103 dwz_file->line.s.section = sectp;
2104 dwz_file->line.size = bfd_section_size (sectp);
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
2108 dwz_file->macro.s.section = sectp;
2109 dwz_file->macro.size = bfd_section_size (sectp);
2110 }
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
2113 dwz_file->gdb_index.s.section = sectp;
2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2115 }
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
2119 dwz_file->debug_names.size = bfd_section_size (sectp);
2120 }
2121}
2122
2123/* See dwarf2read.h. */
2124
2125struct dwz_file *
2126dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2127{
2128 const char *filename;
2129 bfd_size_type buildid_len_arg;
2130 size_t buildid_len;
2131 bfd_byte *buildid;
2132
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
2135
2136 bfd_set_error (bfd_error_no_error);
2137 gdb::unique_xmalloc_ptr<char> data
2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2139 &buildid_len_arg, &buildid));
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2149
2150 buildid_len = (size_t) buildid_len_arg;
2151
2152 filename = data.get ();
2153
2154 std::string abs_storage;
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
2157 gdb::unique_xmalloc_ptr<char> abs
2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2159
2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2161 filename = abs_storage.c_str ();
2162 }
2163
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2167 if (dwz_bfd != NULL)
2168 {
2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2170 dwz_bfd.reset (nullptr);
2171 }
2172
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
2179 const char *origname = bfd_get_filename (per_bfd->obfd);
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
2201 bfd_get_filename (per_bfd->obfd));
2202
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
2205
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
2208
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
2212}
2213\f
2214/* DWARF quick_symbols_functions support. */
2215
2216/* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221struct quick_file_names
2222{
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236};
2237
2238/* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241struct dwarf2_per_cu_quick_data
2242{
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2246 struct quick_file_names *file_names;
2247
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255};
2256
2257/* Utility hash function for a stmt_list_hash. */
2258
2259static hashval_t
2260hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261{
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2266 v += to_underlying (stmt_list_hash->line_sect_off);
2267 return v;
2268}
2269
2270/* Utility equality function for a stmt_list_hash. */
2271
2272static int
2273eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275{
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
2282 return lhs->line_sect_off == rhs->line_sect_off;
2283}
2284
2285/* Hash function for a quick_file_names. */
2286
2287static hashval_t
2288hash_file_name_entry (const void *e)
2289{
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
2292
2293 return hash_stmt_list_entry (&file_data->hash);
2294}
2295
2296/* Equality function for a quick_file_names. */
2297
2298static int
2299eq_file_name_entry (const void *a, const void *b)
2300{
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2303
2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2305}
2306
2307/* Delete function for a quick_file_names. */
2308
2309static void
2310delete_file_name_entry (void *e)
2311{
2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
2324}
2325
2326/* Create a quick_file_names hash table. */
2327
2328static htab_up
2329create_quick_file_names_table (unsigned int nr_initial_entries)
2330{
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
2334}
2335
2336/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
2339
2340static void
2341load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
2343{
2344 if (per_cu->is_debug_types)
2345 load_full_type_unit (per_cu, per_objfile);
2346 else
2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2348
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2353}
2354
2355/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2356
2357static void
2358dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
2361{
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
2364 if (per_cu->type_unit_group_p ())
2365 return;
2366
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2371
2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2373 {
2374 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
2381 && per_cu->cu != NULL
2382 && per_cu->cu->dwo_unit != NULL
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2385 /* DWP files aren't supported yet. */
2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
2387 queue_and_load_all_dwo_tus (per_cu);
2388 }
2389
2390 process_queue (dwarf2_per_objfile);
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
2394 age_cached_comp_units (dwarf2_per_objfile);
2395}
2396
2397/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2401
2402static struct compunit_symtab *
2403dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
2406{
2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2410 {
2411 free_cached_comp_units freer (dwarf2_per_objfile);
2412 scoped_restore decrementer = increment_reading_symtab ();
2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2414 process_cu_includes (dwarf2_per_objfile);
2415 }
2416
2417 return dwarf2_per_objfile->get_symtab (per_cu);
2418}
2419
2420/* See declaration. */
2421
2422dwarf2_per_cu_data *
2423dwarf2_per_bfd::get_cutu (int index)
2424{
2425 if (index >= this->all_comp_units.size ())
2426 {
2427 index -= this->all_comp_units.size ();
2428 gdb_assert (index < this->all_type_units.size ());
2429 return &this->all_type_units[index]->per_cu;
2430 }
2431
2432 return this->all_comp_units[index];
2433}
2434
2435/* See declaration. */
2436
2437dwarf2_per_cu_data *
2438dwarf2_per_bfd::get_cu (int index)
2439{
2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2441
2442 return this->all_comp_units[index];
2443}
2444
2445/* See declaration. */
2446
2447signatured_type *
2448dwarf2_per_bfd::get_tu (int index)
2449{
2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2451
2452 return this->all_type_units[index];
2453}
2454
2455/* See read.h. */
2456
2457dwarf2_per_cu_data *
2458dwarf2_per_bfd::allocate_per_cu ()
2459{
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2461 result->per_bfd = this;
2462 result->index = m_num_psymtabs++;
2463 return result;
2464}
2465
2466/* See read.h. */
2467
2468signatured_type *
2469dwarf2_per_bfd::allocate_signatured_type ()
2470{
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2472 result->per_cu.per_bfd = this;
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475}
2476
2477/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
2479
2480static dwarf2_per_cu_data *
2481create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485{
2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
2489 the_cu->section = section;
2490 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2491 struct dwarf2_per_cu_quick_data);
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494}
2495
2496/* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2498
2499static void
2500create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
2503 int is_dwz)
2504{
2505 for (offset_type i = 0; i < n_elements; i += 2)
2506 {
2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2512 cu_list += 2 * 8;
2513
2514 dwarf2_per_cu_data *per_cu
2515 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2516 sect_off, length);
2517 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2518 }
2519}
2520
2521/* Read the CU list from the mapped index, and use it to create all
2522 the CU objects for this objfile. */
2523
2524static void
2525create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528{
2529 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2530 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2531 ((cu_list_elements + dwz_elements) / 2);
2532
2533 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2534 &dwarf2_per_objfile->per_bfd->info, 0);
2535
2536 if (dwz_elements == 0)
2537 return;
2538
2539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2540 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2541 &dwz->info, 1);
2542}
2543
2544/* Create the signatured type hash table from the index. */
2545
2546static void
2547create_signatured_type_table_from_index
2548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct dwarf2_section_info *section,
2550 const gdb_byte *bytes,
2551 offset_type elements)
2552{
2553 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2554 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table ();
2557
2558 for (offset_type i = 0; i < elements; i += 3)
2559 {
2560 struct signatured_type *sig_type;
2561 ULONGEST signature;
2562 void **slot;
2563 cu_offset type_offset_in_tu;
2564
2565 gdb_static_assert (sizeof (ULONGEST) >= 8);
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2568 type_offset_in_tu
2569 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2570 BFD_ENDIAN_LITTLE);
2571 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2572 bytes += 3 * 8;
2573
2574 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2575 sig_type->signature = signature;
2576 sig_type->type_offset_in_tu = type_offset_in_tu;
2577 sig_type->per_cu.is_debug_types = 1;
2578 sig_type->per_cu.section = section;
2579 sig_type->per_cu.sect_off = sect_off;
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2585 *slot = sig_type;
2586
2587 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2588 }
2589
2590 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2591}
2592
2593/* Create the signatured type hash table from .debug_names. */
2594
2595static void
2596create_signatured_type_table_from_debug_names
2597 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 const mapped_debug_names &map,
2599 struct dwarf2_section_info *section,
2600 struct dwarf2_section_info *abbrev_section)
2601{
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603
2604 section->read (objfile);
2605 abbrev_section->read (objfile);
2606
2607 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2608 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2609
2610 htab_up sig_types_hash = allocate_signatured_type_table ();
2611
2612 for (uint32_t i = 0; i < map.tu_count; ++i)
2613 {
2614 struct signatured_type *sig_type;
2615 void **slot;
2616
2617 sect_offset sect_off
2618 = (sect_offset) (extract_unsigned_integer
2619 (map.tu_table_reordered + i * map.offset_size,
2620 map.offset_size,
2621 map.dwarf5_byte_order));
2622
2623 comp_unit_head cu_header;
2624 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2625 abbrev_section,
2626 section->buffer + to_underlying (sect_off),
2627 rcuh_kind::TYPE);
2628
2629 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2630 sig_type->signature = cu_header.signature;
2631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2632 sig_type->per_cu.is_debug_types = 1;
2633 sig_type->per_cu.section = section;
2634 sig_type->per_cu.sect_off = sect_off;
2635 sig_type->per_cu.v.quick
2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2637 struct dwarf2_per_cu_quick_data);
2638
2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2640 *slot = sig_type;
2641
2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2643 }
2644
2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2646}
2647
2648/* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651static void
2652create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
2654{
2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
2656 struct gdbarch *gdbarch = objfile->arch ();
2657 const gdb_byte *iter, *end;
2658 struct addrmap *mutable_map;
2659 CORE_ADDR baseaddr;
2660
2661 auto_obstack temp_obstack;
2662
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
2667
2668 baseaddr = objfile->text_section_offset ();
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
2679
2680 if (lo > hi)
2681 {
2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2683 hex_string (lo), hex_string (hi));
2684 continue;
2685 }
2686
2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2688 {
2689 complaint (_(".gdb_index address table has invalid CU number %u"),
2690 (unsigned) cu_index);
2691 continue;
2692 }
2693
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2696 addrmap_set_empty (mutable_map, lo, hi - 1,
2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2698 }
2699
2700 objfile->partial_symtabs->psymtabs_addrmap
2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2702}
2703
2704/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707static void
2708create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2709 struct dwarf2_section_info *section)
2710{
2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
2712 bfd *abfd = objfile->obfd;
2713 struct gdbarch *gdbarch = objfile->arch ();
2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2724 {
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2732 return;
2733 }
2734 }
2735
2736 section->read (objfile);
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
2771 warning (_("Section .debug_aranges in %s entry at offset %s "
2772 "has unsupported version %d, ignoring .debug_aranges."),
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "address_size %u is invalid, ignoring .debug_aranges."),
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
2808 warning (_("Section .debug_aranges in %s entry at offset %s "
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "padding is not zero, ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
2836 warning (_("Section .debug_aranges in %s entry at offset %s "
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
2865 objfile->partial_symtabs->psymtabs_addrmap
2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2867}
2868
2869/* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2873
2874static bool
2875find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877{
2878 offset_type hash;
2879 offset_type slot, step;
2880 int (*cmp) (const char *, const char *);
2881
2882 gdb::unique_xmalloc_ptr<char> without_params;
2883 if (current_language->la_language == language_cplus
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
2889
2890 if (strchr (name, '(') != NULL)
2891 {
2892 without_params = cp_remove_params (name);
2893
2894 if (without_params != NULL)
2895 name = without_params.get ();
2896 }
2897 }
2898
2899 /* Index version 4 did not support case insensitive searches. But the
2900 indices for case insensitive languages are built in lowercase, therefore
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2910
2911 for (;;)
2912 {
2913 const char *str;
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
2917 return false;
2918
2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2920 if (!cmp (name, str))
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
2923 + MAYBE_SWAP (bucket.vec));
2924 return true;
2925 }
2926
2927 slot = (slot + step) & (index->symbol_table.size () - 1);
2928 }
2929}
2930
2931/* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
2933 it is used for error reporting. DEPRECATED_OK is true if it is
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
2940 Returns true if all went well, false otherwise. */
2941
2942static bool
2943read_gdb_index_from_buffer (const char *filename,
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951{
2952 const gdb_byte *addr = &buffer[0];
2953
2954 /* Version check. */
2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
2957 causes the index to behave very poorly for certain requests. Version 3
2958 contained incomplete addrmap. So, it seems better to just ignore such
2959 indices. */
2960 if (version < 4)
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2966 filename);
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2979 if (version < 6 && !deprecated_ok)
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
2984 warning (_("\
2985Skipping deprecated .gdb_index section in %s.\n\
2986Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987to use the section anyway."),
2988 filename);
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
3000
3001 /* Indexes with higher version than the one supported by GDB may be no
3002 longer backward compatible. */
3003 if (version > 8)
3004 return 0;
3005
3006 map->version = version;
3007
3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3009
3010 int i = 0;
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
3014 ++i;
3015
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
3020 ++i;
3021
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3026 ++i;
3027
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
3034
3035 ++i;
3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3037
3038 return 1;
3039}
3040
3041/* Callback types for dwarf2_read_gdb_index. */
3042
3043typedef gdb::function_view
3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3045 get_gdb_index_contents_ftype;
3046typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
3050/* Read .gdb_index. If everything went ok, initialize the "quick"
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053static int
3054dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3058{
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3061 struct dwz_file *dwz;
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063
3064 gdb::array_view<const gdb_byte> main_index_contents
3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
3076 return 0;
3077
3078 /* Don't use the index if it's empty. */
3079 if (map->symbol_table.empty ())
3080 return 0;
3081
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3085 if (dwz != NULL)
3086 {
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3098 1, dwz_index_content, &dwz_map,
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
3104 bfd_get_filename (dwz->dwz_bfd.get ()));
3105 return 0;
3106 }
3107 }
3108
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
3111
3112 if (types_list_elements)
3113 {
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3117 return 0;
3118
3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3120
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
3123 }
3124
3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3126
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3131
3132 return 1;
3133}
3134
3135/* die_reader_func for dw2_get_file_names. */
3136
3137static void
3138dw2_get_file_names_reader (const struct die_reader_specs *reader,
3139 const gdb_byte *info_ptr,
3140 struct die_info *comp_unit_die)
3141{
3142 struct dwarf2_cu *cu = reader->cu;
3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3145 struct dwarf2_per_cu_data *lh_cu;
3146 struct attribute *attr;
3147 void **slot;
3148 struct quick_file_names *qfn;
3149
3150 gdb_assert (! this_cu->is_debug_types);
3151
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
3160 lh_cu = this_cu;
3161 slot = NULL;
3162
3163 line_header_up lh;
3164 sect_offset line_offset {};
3165
3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3167 if (attr != nullptr)
3168 {
3169 struct quick_file_names find_entry;
3170
3171 line_offset = (sect_offset) DW_UNSND (attr);
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
3175 find_entry.hash.dwo_unit = cu->dwo_unit;
3176 find_entry.hash.line_sect_off = line_offset;
3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3182 return;
3183 }
3184
3185 lh = dwarf_decode_line_header (line_offset, cu);
3186 }
3187 if (lh == NULL)
3188 {
3189 lh_cu->v.quick->no_file_data = 1;
3190 return;
3191 }
3192
3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3194 qfn->hash.dwo_unit = cu->dwo_unit;
3195 qfn->hash.line_sect_off = line_offset;
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
3198
3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3200
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
3205 qfn->num_file_names = offset + lh->file_names_size ();
3206 qfn->file_names =
3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3208 qfn->num_file_names);
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
3211 for (int i = 0; i < lh->file_names_size (); ++i)
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
3214 qfn->real_names = NULL;
3215
3216 lh_cu->v.quick->file_names = qfn;
3217}
3218
3219/* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222static struct quick_file_names *
3223dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
3225{
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
3229 gdb_assert (! this_cu->type_unit_group_p ());
3230
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
3237 cutu_reader reader (this_cu, per_objfile);
3238 if (!reader.dummy_p)
3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
3244}
3245
3246/* A helper for the "quick" functions which computes and caches the
3247 real path for a given file name from the line table. */
3248
3249static const char *
3250dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3251 struct quick_file_names *qfn, int index)
3252{
3253 if (qfn->real_names == NULL)
3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3255 qfn->num_file_names, const char *);
3256
3257 if (qfn->real_names[index] == NULL)
3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3259
3260 return qfn->real_names[index];
3261}
3262
3263static struct symtab *
3264dw2_find_last_source_symtab (struct objfile *objfile)
3265{
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3271
3272 if (cust == NULL)
3273 return NULL;
3274
3275 return compunit_primary_filetab (cust);
3276}
3277
3278/* Traversal function for dw2_forget_cached_source_info. */
3279
3280static int
3281dw2_free_cached_file_names (void **slot, void *info)
3282{
3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3284
3285 if (file_data->real_names)
3286 {
3287 int i;
3288
3289 for (i = 0; i < file_data->num_file_names; ++i)
3290 {
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
3293 }
3294 }
3295
3296 return 1;
3297}
3298
3299static void
3300dw2_forget_cached_source_info (struct objfile *objfile)
3301{
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
3304
3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3306 dw2_free_cached_file_names, NULL);
3307}
3308
3309/* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312static int
3313dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
3315 const char *name, const char *real_path,
3316 gdb::function_view<bool (symtab *)> callback)
3317{
3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3319
3320 /* Don't visit already-expanded CUs. */
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
3328
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
3331}
3332
3333/* Implementation of the map_symtabs_matching_filename method. */
3334
3335static bool
3336dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
3339{
3340 const char *name_basename = lbasename (name);
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
3343
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
3346
3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3348 {
3349 /* We only need to look at symtabs not already expanded. */
3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3351 continue;
3352
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3355 if (file_data == NULL)
3356 continue;
3357
3358 for (int j = 0; j < file_data->num_file_names; ++j)
3359 {
3360 const char *this_name = file_data->file_names[j];
3361 const char *this_real_name;
3362
3363 if (compare_filenames_for_search (this_name, name))
3364 {
3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3366 callback))
3367 return true;
3368 continue;
3369 }
3370
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
3379 if (compare_filenames_for_search (this_real_name, name))
3380 {
3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3382 callback))
3383 return true;
3384 continue;
3385 }
3386
3387 if (real_path != NULL)
3388 {
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
3391 if (this_real_name != NULL
3392 && FILENAME_CMP (real_path, this_real_name) == 0)
3393 {
3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3395 callback))
3396 return true;
3397 continue;
3398 }
3399 }
3400 }
3401 }
3402
3403 return false;
3404}
3405
3406/* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408struct dw2_symtab_iterator
3409{
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
3414 gdb::optional<block_enum> block_index;
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
3429};
3430
3431/* Initialize the index symtab iterator ITER. */
3432
3433static void
3434dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
3436 gdb::optional<block_enum> block_index,
3437 domain_enum domain,
3438 const char *name)
3439{
3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
3444 iter->global_seen = 0;
3445
3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456}
3457
3458/* Return the next matching CU or NULL if there are no more. */
3459
3460static struct dwarf2_per_cu_data *
3461dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462{
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3480 /* Don't crash on bad data. */
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3483 {
3484 complaint (_(".gdb_index entry has bad CU index"
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3487 continue;
3488 }
3489
3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3491
3492 /* Skip if already read in. */
3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3494 continue;
3495
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550}
3551
3552static struct compunit_symtab *
3553dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3554 const char *name, domain_enum domain)
3555{
3556 struct compunit_symtab *stab_best = NULL;
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
3559
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
3564
3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3566
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3574
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
3578
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
3582
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
3589
3590 /* Keep looking through other CUs. */
3591 }
3592
3593 return stab_best;
3594}
3595
3596static void
3597dw2_print_stats (struct objfile *objfile)
3598{
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3603 int count = 0;
3604
3605 for (int i = 0; i < total; ++i)
3606 {
3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3608
3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3610 ++count;
3611 }
3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614}
3615
3616/* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
3621static void
3622dw2_dump (struct objfile *objfile)
3623{
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3628 printf_filtered (".gdb_index:");
3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3630 {
3631 printf_filtered (" version %d\n",
3632 dwarf2_per_objfile->per_bfd->index_table->version);
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
3637}
3638
3639static void
3640dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642{
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
3645
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
3648
3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3650
3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3653
3654}
3655
3656static void
3657dw2_expand_all_symtabs (struct objfile *objfile)
3658{
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3663
3664 for (int i = 0; i < total_units; ++i)
3665 {
3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3667
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3674 }
3675}
3676
3677static void
3678dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
3680{
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = get_dwarf2_per_objfile (objfile);
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3690 {
3691 /* We only need to look at symtabs not already expanded. */
3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3693 continue;
3694
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3697 if (file_data == NULL)
3698 continue;
3699
3700 for (int j = 0; j < file_data->num_file_names; ++j)
3701 {
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
3705 {
3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3707 break;
3708 }
3709 }
3710 }
3711}
3712
3713static void
3714dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
3720
3721static void
3722dw2_expand_symtabs_matching_one
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
3728static void
3729dw2_map_matching_symbols
3730 (struct objfile *objfile,
3731 const lookup_name_info &name, domain_enum domain,
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
3734 symbol_compare_ftype *ordered_compare)
3735{
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
3746 using GNU index. */
3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3748
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
3788}
3789
3790/* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795static std::string
3796make_sort_after_prefix_name (const char *search_name)
3797{
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861}
3862
3863/* See declaration. */
3864
3865std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
3867mapped_index_base::find_name_components_bounds
3868 (const lookup_name_info &lookup_name_without_params, language lang) const
3869{
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3872
3873 const char *lang_name
3874 = lookup_name_without_params.language_lookup_name (lang);
3875
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3903 return begin;
3904 else
3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
3911 if (lookup_name_without_params.completion_mode ())
3912 {
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3924 std::string after = make_sort_after_prefix_name (lang_name);
3925 if (after.empty ())
3926 return end;
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3929 }
3930 else
3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3932 } ();
3933
3934 return {lower, upper};
3935}
3936
3937/* See declaration. */
3938
3939void
3940mapped_index_base::build_name_components ()
3941{
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3951 namespace/module separator) and Ada symbol names. */
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
3954 {
3955 if (this->symbol_name_slot_invalid (idx))
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
3977 {
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
3987 }
3988
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008}
4009
4010/* Helper for dw2_expand_symtabs_matching that works with a
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
4015 symbol's index in the mapped_index_base symbol table. */
4016
4017static void
4018dw2_expand_symtabs_matching_symbol
4019 (mapped_index_base &index,
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
4023 gdb::function_view<bool (offset_type)> match_callback)
4024{
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
4040
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
4044 const char *name;
4045
4046 bool operator== (const name_and_matcher &other) const
4047 {
4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
4063
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
4075 continue;
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
4092
4093 matches.push_back (bounds.first->idx);
4094 }
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
4105 if (!match_callback (idx))
4106 break;
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114}
4115
4116#if GDB_SELF_TEST
4117
4118namespace selftests { namespace dw2_expand_symtabs_matching {
4119
4120/* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124class mock_mapped_index : public mapped_index_base
4125{
4126public:
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
4129 {}
4130
4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4132
4133 /* Return the number of names in the symbol table. */
4134 size_t symbol_name_count () const override
4135 {
4136 return m_symbol_table.size ();
4137 }
4138
4139 /* Get the name of the symbol at IDX in the symbol table. */
4140 const char *symbol_name_at (offset_type idx) const override
4141 {
4142 return m_symbol_table[idx];
4143 }
4144
4145private:
4146 gdb::array_view<const char *> m_symbol_table;
4147};
4148
4149/* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152static const char *
4153string_or_null (const char *str)
4154{
4155 return str != NULL ? str : "<null>";
4156}
4157
4158/* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165static bool
4166check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171{
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
4195 const char *matched_name = mock_index.symbol_name_at (idx);
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
4201 return true;
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210}
4211
4212/* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
4225
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248#define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254};
4255
4256/* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
4259
4260static bool
4261check_find_bounds_finds (mapped_index_base &index,
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264{
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284}
4285
4286/* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
4289static void
4290test_mapped_index_find_name_component_bounds ()
4291{
4292 mock_mapped_index mock_index (test_symbols);
4293
4294 mock_index.build_name_components ();
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
4302 };
4303
4304 SELF_CHECK (check_find_bounds_finds (mock_index,
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
4315 SELF_CHECK (check_find_bounds_finds (mock_index,
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
4321 SELF_CHECK (check_find_bounds_finds (mock_index,
4322 "\377\377", expected_syms2));
4323 }
4324}
4325
4326/* Test dw2_expand_symtabs_matching_symbol. */
4327
4328static void
4329test_dw2_expand_symtabs_matching_symbol ()
4330{
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340#define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
4526 SELF_CHECK (!any_mismatch);
4527
4528#undef EXPECT
4529#undef CHECK_MATCH
4530}
4531
4532static void
4533run_test ()
4534{
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537}
4538
4539}} // namespace selftests::dw2_expand_symtabs_matching
4540
4541#endif /* GDB_SELF_TEST */
4542
4543/* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548static void
4549dw2_expand_symtabs_matching_one
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554{
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4558
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4561 gdb_assert (symtab != nullptr);
4562
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4565 }
4566}
4567
4568/* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572static void
4573dw2_expand_marked_cus
4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578{
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4582
4583 vec = (offset_type *) (index.constant_pool
4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
4604 {
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
4610
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
4615 {
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4622 continue;
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
4632 default:
4633 break;
4634 }
4635 }
4636
4637 /* Don't crash on bad data. */
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4640 {
4641 complaint (_(".gdb_index entry has bad CU index"
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
4644 continue;
4645 }
4646
4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4649 expansion_notify);
4650 }
4651}
4652
4653/* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
4657static void
4658dw_expand_symtabs_matching_file_matcher
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4661{
4662 if (file_matcher == NULL)
4663 return;
4664
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
4674
4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4676 {
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4683 continue;
4684
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
4728 *slot = file_data;
4729 }
4730}
4731
4732static void
4733dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4736 const lookup_name_info *lookup_name,
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740{
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
4743
4744 /* index_table is NULL if OBJF_READNOW. */
4745 if (!dwarf2_per_objfile->per_bfd->index_table)
4746 return;
4747
4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4749
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4753 {
4754 QUIT;
4755
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
4758 }
4759 return;
4760 }
4761
4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4763
4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4769 expansion_notify, kind);
4770 return true;
4771 });
4772}
4773
4774/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777static struct compunit_symtab *
4778recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780{
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800}
4801
4802static struct compunit_symtab *
4803dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808{
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
4813 return NULL;
4814
4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4818 if (!data)
4819 return NULL;
4820
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4824 paddress (objfile->arch (), pc));
4825
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
4829 gdb_assert (result != NULL);
4830 return result;
4831}
4832
4833static void
4834dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836{
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
4839
4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4841 {
4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4853 {
4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4865 {
4866 /* We only need to look at symtabs not already expanded. */
4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4868 continue;
4869
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4872 if (file_data == NULL)
4873 continue;
4874
4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4887 }
4888 }
4889 }
4890
4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899}
4900
4901static int
4902dw2_has_symbols (struct objfile *objfile)
4903{
4904 return 1;
4905}
4906
4907const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908{
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
4914 NULL,
4915 dw2_print_stats,
4916 dw2_dump,
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925};
4926
4927/* DWARF-5 debug_names reader. */
4928
4929/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932/* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938static bool
4939read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943{
4944 if (section->empty ())
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4950 return false;
4951
4952 section->read (objfile);
4953
4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4955
4956 const gdb_byte *addr = section->buffer;
4957
4958 bfd *const abfd = section->get_bfd_owner ();
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114}
5115
5116/* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119static void
5120create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
5123 bool is_dwz)
5124{
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5142 }
5143 }
5144
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
5162 dwarf2_per_cu_data *per_cu
5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5164 sect_off_prev, length);
5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169}
5170
5171/* Read the CU list from the mapped index, and use it to create all
5172 the CU objects for this dwarf2_per_objfile. */
5173
5174static void
5175create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178{
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5181
5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5183 dwarf2_per_objfile->per_bfd->info,
5184 false /* is_dwz */);
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5191 true /* is_dwz */);
5192}
5193
5194/* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197static bool
5198dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5199{
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5206 &dwarf2_per_objfile->per_bfd->debug_names,
5207 *map))
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
5211 if (map->name_count == 0)
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
5220 bfd_get_filename (dwz->dwz_bfd.get ()),
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
5224 bfd_get_filename (dwz->dwz_bfd.get ()));
5225 return false;
5226 }
5227 }
5228
5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5230
5231 if (map->tu_count != 0)
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5236 return false;
5237
5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5239
5240 create_signatured_type_table_from_debug_names
5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5242 }
5243
5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
5246
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5251
5252 return true;
5253}
5254
5255/* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258class dw2_debug_names_iterator
5259{
5260public:
5261 dw2_debug_names_iterator (const mapped_debug_names &map,
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
5264 const char *name)
5265 : m_map (map), m_block_index (block_index), m_domain (domain),
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306};
5307
5308const char *
5309mapped_debug_names::namei_to_name (uint32_t namei) const
5310{
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
5318}
5319
5320/* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324const gdb_byte *
5325dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327{
5328 int (*cmp) (const char *, const char *);
5329
5330 gdb::unique_xmalloc_ptr<char> without_params;
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
5340 without_params = cp_remove_params (name);
5341 if (without_params != NULL)
5342 name = without_params.get ();
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5360 "[in module %s]"),
5361 namei, map.name_count,
5362 objfile_name (map.dwarf2_per_objfile->objfile));
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379#if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
5382 complaint (_("Wrong .debug_names hash for string at index %u "
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387#endif
5388
5389 if (cmp (namei_string, name) == 0)
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
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403}
5404
5405const gdb_byte *
5406dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408{
5409 if (namei >= map.name_count)
5410 {
5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5412 "[in module %s]"),
5413 namei, map.name_count,
5414 objfile_name (map.dwarf2_per_objfile->objfile));
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423}
5424
5425/* See dw2_debug_names_iterator. */
5426
5427dwarf2_per_cu_data *
5428dw2_debug_names_iterator::next ()
5429{
5430 if (m_addr == NULL)
5431 return NULL;
5432
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
5449 "[in module %s]"),
5450 pulongest (abbrev), objfile_name (objfile));
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
5458 } symbol_linkage_ = symbol_linkage::unknown;
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
5487 default:
5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5489 dwarf_form_name (attr.form),
5490 objfile_name (objfile));
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5498 {
5499 complaint (_(".debug_names entry has bad CU index %s"
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5506 break;
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5510 {
5511 complaint (_(".debug_names entry has bad TU index %s"
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5518 break;
5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5524 break;
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
5528 symbol_linkage_ = symbol_linkage::static_;
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
5533 symbol_linkage_ = symbol_linkage::extern_;
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5540 goto again;
5541
5542 /* Check static vs global. */
5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5544 {
5545 const bool want_static = *m_block_index == STATIC_BLOCK;
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
5548 if (want_static != symbol_is_static)
5549 goto again;
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
5605 {
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
5608 {
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
5613 }
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
5617 {
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
5622 }
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
5642 default:
5643 break;
5644 }
5645
5646 return per_cu;
5647}
5648
5649static struct compunit_symtab *
5650dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5651 const char *name, domain_enum domain)
5652{
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
5655
5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5657 if (!mapp)
5658 {
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
5663
5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
5665
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5675
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
5679
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
5683
5684 if (sym != NULL
5685 && strcmp_iw (sym->search_name (), name) == 0)
5686 return stab;
5687 if (with_opaque != NULL
5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
5689 stab_best = stab;
5690
5691 /* Keep looking through other CUs. */
5692 }
5693
5694 return stab_best;
5695}
5696
5697/* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
5700
5701static void
5702dw2_debug_names_dump (struct objfile *objfile)
5703{
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5708 printf_filtered (".debug_names:");
5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
5714}
5715
5716static void
5717dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
5719{
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
5722
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5725 {
5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5727
5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5729
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5733 }
5734}
5735
5736static void
5737dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743{
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5749 return;
5750
5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5781 {
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
5784 {
5785 const struct block *block
5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792}
5793
5794static void
5795dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5798 const lookup_name_info *lookup_name,
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802{
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
5805
5806 /* debug_names_table is NULL if OBJF_READNOW. */
5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5808 return;
5809
5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5811
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5815 {
5816 QUIT;
5817
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
5820 }
5821 return;
5822 }
5823
5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5825
5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
5829 {
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
5833
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
5838 return true;
5839 });
5840}
5841
5842const struct quick_symbol_functions dwarf2_debug_names_functions =
5843{
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
5847 dw2_map_symtabs_matching_filename,
5848 dw2_debug_names_lookup_symbol,
5849 NULL,
5850 dw2_print_stats,
5851 dw2_debug_names_dump,
5852 dw2_debug_names_expand_symtabs_for_function,
5853 dw2_expand_all_symtabs,
5854 dw2_expand_symtabs_with_fullname,
5855 dw2_debug_names_map_matching_symbols,
5856 dw2_debug_names_expand_symtabs_matching,
5857 dw2_find_pc_sect_compunit_symtab,
5858 NULL,
5859 dw2_map_symbol_filenames
5860};
5861
5862/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5863 to either a dwarf2_per_bfd or dwz_file object. */
5864
5865template <typename T>
5866static gdb::array_view<const gdb_byte>
5867get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868{
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
5871 if (section->empty ())
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5877 return {};
5878
5879 section->read (obj);
5880
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
5888}
5889
5890/* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893static gdb::array_view<const gdb_byte>
5894get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5895{
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5901 &dwarf2_per_bfd->index_cache_res);
5902}
5903
5904/* Same as the above, but for DWZ. */
5905
5906static gdb::array_view<const gdb_byte>
5907get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908{
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914}
5915
5916/* See symfile.h. */
5917
5918bool
5919dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5920{
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5930 dwarf2_per_objfile->per_bfd->using_index = 1;
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
5934 = create_quick_file_names_table
5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5936 dwarf2_per_objfile->resize_symtabs ();
5937
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5940 {
5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5942
5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5944 struct dwarf2_per_cu_quick_data);
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
5952 }
5953
5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
5957 dwarf2_per_objfile->resize_symtabs ();
5958 return true;
5959 }
5960
5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5963 get_gdb_index_contents_from_section<dwz_file>))
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
5966 dwarf2_per_objfile->resize_symtabs ();
5967 return true;
5968 }
5969
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
5977 dwarf2_per_objfile->resize_symtabs ();
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
5982 return false;
5983}
5984
5985\f
5986
5987/* Build a partial symbol table. */
5988
5989void
5990dwarf2_build_psymtabs (struct objfile *objfile)
5991{
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
5994
5995 init_psymbol_list (objfile, 1024);
5996
5997 try
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
6002 psymtab_discarder psymtabs (objfile);
6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6004 psymtabs.keep ();
6005
6006 dwarf2_per_objfile->resize_symtabs ();
6007
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
6010 }
6011 catch (const gdb_exception_error &except)
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
6015}
6016
6017/* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023static void
6024dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025{
6026 struct attribute *attr;
6027
6028 cu->base_address.reset ();
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6031 if (attr != nullptr)
6032 cu->base_address = attr->value_as_address ();
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6036 if (attr != nullptr)
6037 cu->base_address = attr->value_as_address ();
6038 }
6039}
6040
6041/* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044static struct dwarf2_section_info *
6045get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046{
6047 struct dwarf2_section_info *abbrev;
6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6049
6050 if (this_cu->is_dwz)
6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6052 else
6053 abbrev = &per_bfd->abbrev;
6054
6055 return abbrev;
6056}
6057
6058/* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060static sect_offset
6061read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
6063 sect_offset sect_off)
6064{
6065 bfd *abfd = section->get_bfd_owner ();
6066 const gdb_byte *info_ptr;
6067 unsigned int initial_length_size, offset_size;
6068 uint16_t version;
6069
6070 section->read (dwarf2_per_objfile->objfile);
6071 info_ptr = section->buffer + to_underlying (sect_off);
6072 read_initial_length (abfd, info_ptr, &initial_length_size);
6073 offset_size = initial_length_size == 4 ? 4 : 8;
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6085}
6086
6087/* A partial symtab that is used only for include files. */
6088struct dwarf2_include_psymtab : public partial_symtab
6089{
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
6117 }
6118
6119 bool readin_p (struct objfile *objfile) const override
6120 {
6121 return includer ()->readin_p (objfile);
6122 }
6123
6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6125 {
6126 return nullptr;
6127 }
6128
6129private:
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
6137};
6138
6139/* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142static void
6143dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6144 struct objfile *objfile)
6145{
6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6147
6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
6149 subpst->dirname = pst->dirname;
6150
6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
6154}
6155
6156/* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
6158 partial symtab for each of these included files. */
6159
6160static void
6161dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6162 struct die_info *die,
6163 dwarf2_psymtab *pst)
6164{
6165 line_header_up lh;
6166 struct attribute *attr;
6167
6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6169 if (attr != nullptr)
6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6179 pst->raw_text_low (), 1);
6180}
6181
6182static hashval_t
6183hash_signatured_type (const void *item)
6184{
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
6187
6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6189 return sig_type->signature;
6190}
6191
6192static int
6193eq_signatured_type (const void *item_lhs, const void *item_rhs)
6194{
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6197
6198 return lhs->signature == rhs->signature;
6199}
6200
6201/* Allocate a hash table for signatured types. */
6202
6203static htab_up
6204allocate_signatured_type_table ()
6205{
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
6210}
6211
6212/* A helper function to add a signatured type CU to a table. */
6213
6214static int
6215add_signatured_type_cu_to_table (void **slot, void *datum)
6216{
6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
6220
6221 all_type_units->push_back (sigt);
6222
6223 return 1;
6224}
6225
6226/* A helper for create_debug_types_hash_table. Read types from SECTION
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
6229
6230static void
6231create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
6233 dwarf2_section_info *section, htab_up &types_htab,
6234 rcuh_kind section_kind)
6235{
6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
6237 struct dwarf2_section_info *abbrev_section;
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
6240
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
6243 : &dwarf2_per_objfile->per_bfd->abbrev);
6244
6245 if (dwarf_read_debug)
6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
6249
6250 section->read (objfile);
6251 info_ptr = section->buffer;
6252
6253 if (info_ptr == NULL)
6254 return;
6255
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
6258 abfd = section->get_bfd_owner ();
6259
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
6262
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
6272
6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6274
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
6277 header.type_cu_offset_in_tu = (cu_offset) -1;
6278
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
6281
6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6283 abbrev_section, ptr, section_kind);
6284
6285 length = header.get_length ();
6286
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
6295
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
6299 types_htab = allocate_dwo_unit_table ();
6300 else
6301 types_htab = allocate_signatured_type_table ();
6302 }
6303
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
6310 dwo_tu->signature = header.signature;
6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6312 dwo_tu->section = section;
6313 dwo_tu->sect_off = sect_off;
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6322 sig_type->signature = header.signature;
6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
6326 sig_type->per_cu.sect_off = sect_off;
6327 sig_type->per_cu.length = length;
6328 }
6329
6330 slot = htab_find_slot (types_htab.get (),
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
6336 sect_offset dup_sect_off;
6337
6338 if (dwo_file)
6339 {
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
6343 dup_sect_off = dup_tu->sect_off;
6344 }
6345 else
6346 {
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
6350 dup_sect_off = dup_tu->per_cu.sect_off;
6351 }
6352
6353 complaint (_("debug type entry at offset %s is duplicate to"
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6356 hex_string (header.signature));
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6359
6360 if (dwarf_read_debug > 1)
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
6363 hex_string (header.signature));
6364
6365 info_ptr += length;
6366 }
6367}
6368
6369/* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
6373
6374 The result is a pointer to the hash table or NULL if there are no types.
6375
6376 Note: This function processes DWO files only, not DWP files. */
6377
6378static void
6379create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
6381 gdb::array_view<dwarf2_section_info> type_sections,
6382 htab_up &types_htab)
6383{
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6386 types_htab, rcuh_kind::TYPE);
6387}
6388
6389/* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394static int
6395create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6396{
6397 htab_up types_htab;
6398
6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
6401 rcuh_kind::COMPILE);
6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6403 dwarf2_per_objfile->per_bfd->types, types_htab);
6404 if (types_htab == NULL)
6405 {
6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6407 return 0;
6408 }
6409
6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6411
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6415
6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6417 add_signatured_type_cu_to_table,
6418 &dwarf2_per_objfile->per_bfd->all_type_units);
6419
6420 return 1;
6421}
6422
6423/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427static struct signatured_type *
6428add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6430{
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6434
6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6436
6437 dwarf2_per_objfile->resize_symtabs ();
6438
6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
6442 if (dwarf2_per_objfile->per_bfd->using_index)
6443 {
6444 sig_type->per_cu.v.quick =
6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458}
6459
6460/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463static void
6464fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467{
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
6470 /* Make sure we're not clobbering something we don't expect to. */
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
6473 if (per_bfd->using_index)
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6482 gdb_assert (sig_entry->type_unit_group == NULL);
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
6489 sig_entry->per_cu.per_bfd = per_bfd;
6490 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6491 sig_entry->dwo_unit = dwo_entry;
6492}
6493
6494/* Subroutine of lookup_signatured_type.
6495 If we haven't read the TU yet, create the signatured_type data structure
6496 for a TU to be read in directly from a DWO file, bypassing the stub.
6497 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6498 using .gdb_index, then when reading a CU we want to stay in the DWO file
6499 containing that CU. Otherwise we could end up reading several other DWO
6500 files (due to comdat folding) to process the transitive closure of all the
6501 mentioned TUs, and that can be slow. The current DWO file will have every
6502 type signature that it needs.
6503 We only do this for .gdb_index because in the psymtab case we already have
6504 to read all the DWOs to build the type unit groups. */
6505
6506static struct signatured_type *
6507lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6508{
6509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6510 struct dwo_file *dwo_file;
6511 struct dwo_unit find_dwo_entry, *dwo_entry;
6512 struct signatured_type find_sig_entry, *sig_entry;
6513 void **slot;
6514
6515 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6516
6517 /* If TU skeletons have been removed then we may not have read in any
6518 TUs yet. */
6519 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6520 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6521
6522 /* We only ever need to read in one copy of a signatured type.
6523 Use the global signatured_types array to do our own comdat-folding
6524 of types. If this is the first time we're reading this TU, and
6525 the TU has an entry in .gdb_index, replace the recorded data from
6526 .gdb_index with this TU. */
6527
6528 find_sig_entry.signature = sig;
6529 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6530 &find_sig_entry, INSERT);
6531 sig_entry = (struct signatured_type *) *slot;
6532
6533 /* We can get here with the TU already read, *or* in the process of being
6534 read. Don't reassign the global entry to point to this DWO if that's
6535 the case. Also note that if the TU is already being read, it may not
6536 have come from a DWO, the program may be a mix of Fission-compiled
6537 code and non-Fission-compiled code. */
6538
6539 /* Have we already tried to read this TU?
6540 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6541 needn't exist in the global table yet). */
6542 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6543 return sig_entry;
6544
6545 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6546 dwo_unit of the TU itself. */
6547 dwo_file = cu->dwo_unit->dwo_file;
6548
6549 /* Ok, this is the first time we're reading this TU. */
6550 if (dwo_file->tus == NULL)
6551 return NULL;
6552 find_dwo_entry.signature = sig;
6553 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6554 &find_dwo_entry);
6555 if (dwo_entry == NULL)
6556 return NULL;
6557
6558 /* If the global table doesn't have an entry for this TU, add one. */
6559 if (sig_entry == NULL)
6560 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6561
6562 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6563 sig_entry->per_cu.tu_read = 1;
6564 return sig_entry;
6565}
6566
6567/* Subroutine of lookup_signatured_type.
6568 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6569 then try the DWP file. If the TU stub (skeleton) has been removed then
6570 it won't be in .gdb_index. */
6571
6572static struct signatured_type *
6573lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6574{
6575 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6576 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6577 struct dwo_unit *dwo_entry;
6578 struct signatured_type find_sig_entry, *sig_entry;
6579 void **slot;
6580
6581 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6582 gdb_assert (dwp_file != NULL);
6583
6584 /* If TU skeletons have been removed then we may not have read in any
6585 TUs yet. */
6586 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6587 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6588
6589 find_sig_entry.signature = sig;
6590 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6591 &find_sig_entry, INSERT);
6592 sig_entry = (struct signatured_type *) *slot;
6593
6594 /* Have we already tried to read this TU?
6595 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6596 needn't exist in the global table yet). */
6597 if (sig_entry != NULL)
6598 return sig_entry;
6599
6600 if (dwp_file->tus == NULL)
6601 return NULL;
6602 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6603 sig, 1 /* is_debug_types */);
6604 if (dwo_entry == NULL)
6605 return NULL;
6606
6607 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6608 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6609
6610 return sig_entry;
6611}
6612
6613/* Lookup a signature based type for DW_FORM_ref_sig8.
6614 Returns NULL if signature SIG is not present in the table.
6615 It is up to the caller to complain about this. */
6616
6617static struct signatured_type *
6618lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6619{
6620 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6621
6622 if (cu->dwo_unit
6623 && dwarf2_per_objfile->per_bfd->using_index)
6624 {
6625 /* We're in a DWO/DWP file, and we're using .gdb_index.
6626 These cases require special processing. */
6627 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6628 return lookup_dwo_signatured_type (cu, sig);
6629 else
6630 return lookup_dwp_signatured_type (cu, sig);
6631 }
6632 else
6633 {
6634 struct signatured_type find_entry, *entry;
6635
6636 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6637 return NULL;
6638 find_entry.signature = sig;
6639 entry = ((struct signatured_type *)
6640 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6641 &find_entry));
6642 return entry;
6643 }
6644}
6645
6646/* Low level DIE reading support. */
6647
6648/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6649
6650static void
6651init_cu_die_reader (struct die_reader_specs *reader,
6652 struct dwarf2_cu *cu,
6653 struct dwarf2_section_info *section,
6654 struct dwo_file *dwo_file,
6655 struct abbrev_table *abbrev_table)
6656{
6657 gdb_assert (section->readin && section->buffer != NULL);
6658 reader->abfd = section->get_bfd_owner ();
6659 reader->cu = cu;
6660 reader->dwo_file = dwo_file;
6661 reader->die_section = section;
6662 reader->buffer = section->buffer;
6663 reader->buffer_end = section->buffer + section->size;
6664 reader->abbrev_table = abbrev_table;
6665}
6666
6667/* Subroutine of cutu_reader to simplify it.
6668 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6669 There's just a lot of work to do, and cutu_reader is big enough
6670 already.
6671
6672 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6673 from it to the DIE in the DWO. If NULL we are skipping the stub.
6674 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6675 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6676 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6677 STUB_COMP_DIR may be non-NULL.
6678 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6679 are filled in with the info of the DIE from the DWO file.
6680 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6681 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6682 kept around for at least as long as *RESULT_READER.
6683
6684 The result is non-zero if a valid (non-dummy) DIE was found. */
6685
6686static int
6687read_cutu_die_from_dwo (dwarf2_cu *cu,
6688 struct dwo_unit *dwo_unit,
6689 struct die_info *stub_comp_unit_die,
6690 const char *stub_comp_dir,
6691 struct die_reader_specs *result_reader,
6692 const gdb_byte **result_info_ptr,
6693 struct die_info **result_comp_unit_die,
6694 abbrev_table_up *result_dwo_abbrev_table)
6695{
6696 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6697 dwarf2_per_cu_data *per_cu = cu->per_cu;
6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
6699 bfd *abfd;
6700 const gdb_byte *begin_info_ptr, *info_ptr;
6701 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6702 int i,num_extra_attrs;
6703 struct dwarf2_section_info *dwo_abbrev_section;
6704 struct die_info *comp_unit_die;
6705
6706 /* At most one of these may be provided. */
6707 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6708
6709 /* These attributes aren't processed until later:
6710 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6711 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6712 referenced later. However, these attributes are found in the stub
6713 which we won't have later. In order to not impose this complication
6714 on the rest of the code, we read them here and copy them to the
6715 DWO CU/TU die. */
6716
6717 stmt_list = NULL;
6718 low_pc = NULL;
6719 high_pc = NULL;
6720 ranges = NULL;
6721 comp_dir = NULL;
6722
6723 if (stub_comp_unit_die != NULL)
6724 {
6725 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6726 DWO file. */
6727 if (!per_cu->is_debug_types)
6728 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6729 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6730 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6731 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6732 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6733
6734 cu->addr_base = stub_comp_unit_die->addr_base ();
6735
6736 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6737 here (if needed). We need the value before we can process
6738 DW_AT_ranges. */
6739 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6740 }
6741 else if (stub_comp_dir != NULL)
6742 {
6743 /* Reconstruct the comp_dir attribute to simplify the code below. */
6744 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6745 comp_dir->name = DW_AT_comp_dir;
6746 comp_dir->form = DW_FORM_string;
6747 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6748 DW_STRING (comp_dir) = stub_comp_dir;
6749 }
6750
6751 /* Set up for reading the DWO CU/TU. */
6752 cu->dwo_unit = dwo_unit;
6753 dwarf2_section_info *section = dwo_unit->section;
6754 section->read (objfile);
6755 abfd = section->get_bfd_owner ();
6756 begin_info_ptr = info_ptr = (section->buffer
6757 + to_underlying (dwo_unit->sect_off));
6758 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6759
6760 if (per_cu->is_debug_types)
6761 {
6762 signatured_type *sig_type = (struct signatured_type *) per_cu;
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::TYPE);
6768 /* This is not an assert because it can be caused by bad debug info. */
6769 if (sig_type->signature != cu->header.signature)
6770 {
6771 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6772 " TU at offset %s [in module %s]"),
6773 hex_string (sig_type->signature),
6774 hex_string (cu->header.signature),
6775 sect_offset_str (dwo_unit->sect_off),
6776 bfd_get_filename (abfd));
6777 }
6778 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6779 /* For DWOs coming from DWP files, we don't know the CU length
6780 nor the type's offset in the TU until now. */
6781 dwo_unit->length = cu->header.get_length ();
6782 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6783
6784 /* Establish the type offset that can be used to lookup the type.
6785 For DWO files, we don't know it until now. */
6786 sig_type->type_offset_in_section
6787 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6788 }
6789 else
6790 {
6791 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6792 &cu->header, section,
6793 dwo_abbrev_section,
6794 info_ptr, rcuh_kind::COMPILE);
6795 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6796 /* For DWOs coming from DWP files, we don't know the CU length
6797 until now. */
6798 dwo_unit->length = cu->header.get_length ();
6799 }
6800
6801 *result_dwo_abbrev_table
6802 = abbrev_table::read (objfile, dwo_abbrev_section,
6803 cu->header.abbrev_sect_off);
6804 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6805 result_dwo_abbrev_table->get ());
6806
6807 /* Read in the die, but leave space to copy over the attributes
6808 from the stub. This has the benefit of simplifying the rest of
6809 the code - all the work to maintain the illusion of a single
6810 DW_TAG_{compile,type}_unit DIE is done here. */
6811 num_extra_attrs = ((stmt_list != NULL)
6812 + (low_pc != NULL)
6813 + (high_pc != NULL)
6814 + (ranges != NULL)
6815 + (comp_dir != NULL));
6816 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6817 num_extra_attrs);
6818
6819 /* Copy over the attributes from the stub to the DIE we just read in. */
6820 comp_unit_die = *result_comp_unit_die;
6821 i = comp_unit_die->num_attrs;
6822 if (stmt_list != NULL)
6823 comp_unit_die->attrs[i++] = *stmt_list;
6824 if (low_pc != NULL)
6825 comp_unit_die->attrs[i++] = *low_pc;
6826 if (high_pc != NULL)
6827 comp_unit_die->attrs[i++] = *high_pc;
6828 if (ranges != NULL)
6829 comp_unit_die->attrs[i++] = *ranges;
6830 if (comp_dir != NULL)
6831 comp_unit_die->attrs[i++] = *comp_dir;
6832 comp_unit_die->num_attrs += num_extra_attrs;
6833
6834 if (dwarf_die_debug)
6835 {
6836 fprintf_unfiltered (gdb_stdlog,
6837 "Read die from %s@0x%x of %s:\n",
6838 section->get_name (),
6839 (unsigned) (begin_info_ptr - section->buffer),
6840 bfd_get_filename (abfd));
6841 dump_die (comp_unit_die, dwarf_die_debug);
6842 }
6843
6844 /* Skip dummy compilation units. */
6845 if (info_ptr >= begin_info_ptr + dwo_unit->length
6846 || peek_abbrev_code (abfd, info_ptr) == 0)
6847 return 0;
6848
6849 *result_info_ptr = info_ptr;
6850 return 1;
6851}
6852
6853/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6854 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6855 signature is part of the header. */
6856static gdb::optional<ULONGEST>
6857lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6858{
6859 if (cu->header.version >= 5)
6860 return cu->header.signature;
6861 struct attribute *attr;
6862 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6863 if (attr == nullptr)
6864 return gdb::optional<ULONGEST> ();
6865 return DW_UNSND (attr);
6866}
6867
6868/* Subroutine of cutu_reader to simplify it.
6869 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6870 Returns NULL if the specified DWO unit cannot be found. */
6871
6872static struct dwo_unit *
6873lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6874{
6875 dwarf2_per_cu_data *per_cu = cu->per_cu;
6876 struct dwo_unit *dwo_unit;
6877 const char *comp_dir;
6878
6879 gdb_assert (cu != NULL);
6880
6881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6882 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6883 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6884
6885 if (per_cu->is_debug_types)
6886 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6887 else
6888 {
6889 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6890
6891 if (!signature.has_value ())
6892 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6893 " [in module %s]"),
6894 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6895
6896 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6897 }
6898
6899 return dwo_unit;
6900}
6901
6902/* Subroutine of cutu_reader to simplify it.
6903 See it for a description of the parameters.
6904 Read a TU directly from a DWO file, bypassing the stub. */
6905
6906void
6907cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6908 dwarf2_per_objfile *per_objfile,
6909 int use_existing_cu)
6910{
6911 struct signatured_type *sig_type;
6912
6913 /* Verify we can do the following downcast, and that we have the
6914 data we need. */
6915 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6916 sig_type = (struct signatured_type *) this_cu;
6917 gdb_assert (sig_type->dwo_unit != NULL);
6918
6919 if (use_existing_cu && this_cu->cu != NULL)
6920 {
6921 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6922 /* There's no need to do the rereading_dwo_cu handling that
6923 cutu_reader does since we don't read the stub. */
6924 }
6925 else
6926 {
6927 /* If !use_existing_cu, this_cu->cu must be NULL. */
6928 gdb_assert (this_cu->cu == NULL);
6929 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6930 }
6931
6932 /* A future optimization, if needed, would be to use an existing
6933 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6934 could share abbrev tables. */
6935
6936 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
6937 NULL /* stub_comp_unit_die */,
6938 sig_type->dwo_unit->dwo_file->comp_dir,
6939 this, &info_ptr,
6940 &comp_unit_die,
6941 &m_dwo_abbrev_table) == 0)
6942 {
6943 /* Dummy die. */
6944 dummy_p = true;
6945 }
6946}
6947
6948/* Initialize a CU (or TU) and read its DIEs.
6949 If the CU defers to a DWO file, read the DWO file as well.
6950
6951 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6952 Otherwise the table specified in the comp unit header is read in and used.
6953 This is an optimization for when we already have the abbrev table.
6954
6955 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6956 Otherwise, a new CU is allocated with xmalloc. */
6957
6958cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6959 dwarf2_per_objfile *dwarf2_per_objfile,
6960 struct abbrev_table *abbrev_table,
6961 int use_existing_cu,
6962 bool skip_partial)
6963 : die_reader_specs {},
6964 m_this_cu (this_cu)
6965{
6966 struct objfile *objfile = dwarf2_per_objfile->objfile;
6967 struct dwarf2_section_info *section = this_cu->section;
6968 bfd *abfd = section->get_bfd_owner ();
6969 struct dwarf2_cu *cu;
6970 const gdb_byte *begin_info_ptr;
6971 struct signatured_type *sig_type = NULL;
6972 struct dwarf2_section_info *abbrev_section;
6973 /* Non-zero if CU currently points to a DWO file and we need to
6974 reread it. When this happens we need to reread the skeleton die
6975 before we can reread the DWO file (this only applies to CUs, not TUs). */
6976 int rereading_dwo_cu = 0;
6977
6978 if (dwarf_die_debug)
6979 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6980 this_cu->is_debug_types ? "type" : "comp",
6981 sect_offset_str (this_cu->sect_off));
6982
6983 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6984 file (instead of going through the stub), short-circuit all of this. */
6985 if (this_cu->reading_dwo_directly)
6986 {
6987 /* Narrow down the scope of possibilities to have to understand. */
6988 gdb_assert (this_cu->is_debug_types);
6989 gdb_assert (abbrev_table == NULL);
6990 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
6991 return;
6992 }
6993
6994 /* This is cheap if the section is already read in. */
6995 section->read (objfile);
6996
6997 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6998
6999 abbrev_section = get_abbrev_section_for_cu (this_cu);
7000
7001 if (use_existing_cu && this_cu->cu != NULL)
7002 {
7003 cu = this_cu->cu;
7004 /* If this CU is from a DWO file we need to start over, we need to
7005 refetch the attributes from the skeleton CU.
7006 This could be optimized by retrieving those attributes from when we
7007 were here the first time: the previous comp_unit_die was stored in
7008 comp_unit_obstack. But there's no data yet that we need this
7009 optimization. */
7010 if (cu->dwo_unit != NULL)
7011 rereading_dwo_cu = 1;
7012 }
7013 else
7014 {
7015 /* If !use_existing_cu, this_cu->cu must be NULL. */
7016 gdb_assert (this_cu->cu == NULL);
7017 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7018 cu = m_new_cu.get ();
7019 }
7020
7021 /* Get the header. */
7022 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7023 {
7024 /* We already have the header, there's no need to read it in again. */
7025 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7026 }
7027 else
7028 {
7029 if (this_cu->is_debug_types)
7030 {
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
7033 abbrev_section, info_ptr,
7034 rcuh_kind::TYPE);
7035
7036 /* Since per_cu is the first member of struct signatured_type,
7037 we can go from a pointer to one to a pointer to the other. */
7038 sig_type = (struct signatured_type *) this_cu;
7039 gdb_assert (sig_type->signature == cu->header.signature);
7040 gdb_assert (sig_type->type_offset_in_tu
7041 == cu->header.type_cu_offset_in_tu);
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7043
7044 /* LENGTH has not been set yet for type units if we're
7045 using .gdb_index. */
7046 this_cu->length = cu->header.get_length ();
7047
7048 /* Establish the type offset that can be used to lookup the type. */
7049 sig_type->type_offset_in_section =
7050 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7051
7052 this_cu->dwarf_version = cu->header.version;
7053 }
7054 else
7055 {
7056 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7057 &cu->header, section,
7058 abbrev_section,
7059 info_ptr,
7060 rcuh_kind::COMPILE);
7061
7062 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7063 if (this_cu->length == 0)
7064 this_cu->length = cu->header.get_length ();
7065 else
7066 gdb_assert (this_cu->length == cu->header.get_length ());
7067 this_cu->dwarf_version = cu->header.version;
7068 }
7069 }
7070
7071 /* Skip dummy compilation units. */
7072 if (info_ptr >= begin_info_ptr + this_cu->length
7073 || peek_abbrev_code (abfd, info_ptr) == 0)
7074 {
7075 dummy_p = true;
7076 return;
7077 }
7078
7079 /* If we don't have them yet, read the abbrevs for this compilation unit.
7080 And if we need to read them now, make sure they're freed when we're
7081 done. */
7082 if (abbrev_table != NULL)
7083 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7084 else
7085 {
7086 m_abbrev_table_holder
7087 = abbrev_table::read (objfile, abbrev_section,
7088 cu->header.abbrev_sect_off);
7089 abbrev_table = m_abbrev_table_holder.get ();
7090 }
7091
7092 /* Read the top level CU/TU die. */
7093 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7095
7096 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7097 {
7098 dummy_p = true;
7099 return;
7100 }
7101
7102 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7103 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7104 table from the DWO file and pass the ownership over to us. It will be
7105 referenced from READER, so we must make sure to free it after we're done
7106 with READER.
7107
7108 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7109 DWO CU, that this test will fail (the attribute will not be present). */
7110 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7111 if (dwo_name != nullptr)
7112 {
7113 struct dwo_unit *dwo_unit;
7114 struct die_info *dwo_comp_unit_die;
7115
7116 if (comp_unit_die->has_children)
7117 {
7118 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7119 " has children (offset %s) [in module %s]"),
7120 sect_offset_str (this_cu->sect_off),
7121 bfd_get_filename (abfd));
7122 }
7123 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7124 if (dwo_unit != NULL)
7125 {
7126 if (read_cutu_die_from_dwo (cu, dwo_unit,
7127 comp_unit_die, NULL,
7128 this, &info_ptr,
7129 &dwo_comp_unit_die,
7130 &m_dwo_abbrev_table) == 0)
7131 {
7132 /* Dummy die. */
7133 dummy_p = true;
7134 return;
7135 }
7136 comp_unit_die = dwo_comp_unit_die;
7137 }
7138 else
7139 {
7140 /* Yikes, we couldn't find the rest of the DIE, we only have
7141 the stub. A complaint has already been logged. There's
7142 not much more we can do except pass on the stub DIE to
7143 die_reader_func. We don't want to throw an error on bad
7144 debug info. */
7145 }
7146 }
7147}
7148
7149void
7150cutu_reader::keep ()
7151{
7152 /* Done, clean up. */
7153 gdb_assert (!dummy_p);
7154 if (m_new_cu != NULL)
7155 {
7156 /* We know that m_this_cu->cu is set, since we are in the process of
7157 parsing the CU. */
7158 gdb_assert (m_this_cu->cu != nullptr);
7159 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7160
7161 /* Link this CU into read_in_chain. */
7162 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7163 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7164 /* The chain owns it now. */
7165 m_new_cu.release ();
7166 }
7167}
7168
7169/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7170 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7171 assumed to have already done the lookup to find the DWO file).
7172
7173 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7174 THIS_CU->is_debug_types, but nothing else.
7175
7176 We fill in THIS_CU->length.
7177
7178 THIS_CU->cu is always freed when done.
7179 This is done in order to not leave THIS_CU->cu in a state where we have
7180 to care whether it refers to the "main" CU or the DWO CU.
7181
7182 When parent_cu is passed, it is used to provide a default value for
7183 str_offsets_base and addr_base from the parent. */
7184
7185cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7186 dwarf2_per_objfile *dwarf2_per_objfile,
7187 struct dwarf2_cu *parent_cu,
7188 struct dwo_file *dwo_file)
7189 : die_reader_specs {},
7190 m_this_cu (this_cu)
7191{
7192 struct objfile *objfile = dwarf2_per_objfile->objfile;
7193 struct dwarf2_section_info *section = this_cu->section;
7194 bfd *abfd = section->get_bfd_owner ();
7195 struct dwarf2_section_info *abbrev_section;
7196 const gdb_byte *begin_info_ptr, *info_ptr;
7197
7198 if (dwarf_die_debug)
7199 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7200 this_cu->is_debug_types ? "type" : "comp",
7201 sect_offset_str (this_cu->sect_off));
7202
7203 gdb_assert (this_cu->cu == NULL);
7204
7205 abbrev_section = (dwo_file != NULL
7206 ? &dwo_file->sections.abbrev
7207 : get_abbrev_section_for_cu (this_cu));
7208
7209 /* This is cheap if the section is already read in. */
7210 section->read (objfile);
7211
7212 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7213
7214 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7215 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7216 &m_new_cu->header, section,
7217 abbrev_section, info_ptr,
7218 (this_cu->is_debug_types
7219 ? rcuh_kind::TYPE
7220 : rcuh_kind::COMPILE));
7221
7222 if (parent_cu != nullptr)
7223 {
7224 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7225 m_new_cu->addr_base = parent_cu->addr_base;
7226 }
7227 this_cu->length = m_new_cu->header.get_length ();
7228
7229 /* Skip dummy compilation units. */
7230 if (info_ptr >= begin_info_ptr + this_cu->length
7231 || peek_abbrev_code (abfd, info_ptr) == 0)
7232 {
7233 dummy_p = true;
7234 return;
7235 }
7236
7237 m_abbrev_table_holder
7238 = abbrev_table::read (objfile, abbrev_section,
7239 m_new_cu->header.abbrev_sect_off);
7240
7241 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7242 m_abbrev_table_holder.get ());
7243 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7244}
7245
7246\f
7247/* Type Unit Groups.
7248
7249 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7250 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7251 so that all types coming from the same compilation (.o file) are grouped
7252 together. A future step could be to put the types in the same symtab as
7253 the CU the types ultimately came from. */
7254
7255static hashval_t
7256hash_type_unit_group (const void *item)
7257{
7258 const struct type_unit_group *tu_group
7259 = (const struct type_unit_group *) item;
7260
7261 return hash_stmt_list_entry (&tu_group->hash);
7262}
7263
7264static int
7265eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7266{
7267 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7268 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7269
7270 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7271}
7272
7273/* Allocate a hash table for type unit groups. */
7274
7275static htab_up
7276allocate_type_unit_groups_table ()
7277{
7278 return htab_up (htab_create_alloc (3,
7279 hash_type_unit_group,
7280 eq_type_unit_group,
7281 NULL, xcalloc, xfree));
7282}
7283
7284/* Type units that don't have DW_AT_stmt_list are grouped into their own
7285 partial symtabs. We combine several TUs per psymtab to not let the size
7286 of any one psymtab grow too big. */
7287#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7288#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7289
7290/* Helper routine for get_type_unit_group.
7291 Create the type_unit_group object used to hold one or more TUs. */
7292
7293static struct type_unit_group *
7294create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7295{
7296 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7297 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7298 struct dwarf2_per_cu_data *per_cu;
7299 struct type_unit_group *tu_group;
7300
7301 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7302 struct type_unit_group);
7303 per_cu = &tu_group->per_cu;
7304 per_cu->per_bfd = per_bfd;
7305
7306 if (per_bfd->using_index)
7307 {
7308 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7309 struct dwarf2_per_cu_quick_data);
7310 }
7311 else
7312 {
7313 unsigned int line_offset = to_underlying (line_offset_struct);
7314 dwarf2_psymtab *pst;
7315 std::string name;
7316
7317 /* Give the symtab a useful name for debug purposes. */
7318 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7319 name = string_printf ("<type_units_%d>",
7320 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7321 else
7322 name = string_printf ("<type_units_at_0x%x>", line_offset);
7323
7324 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7325 pst->anonymous = true;
7326 }
7327
7328 tu_group->hash.dwo_unit = cu->dwo_unit;
7329 tu_group->hash.line_sect_off = line_offset_struct;
7330
7331 return tu_group;
7332}
7333
7334/* Look up the type_unit_group for type unit CU, and create it if necessary.
7335 STMT_LIST is a DW_AT_stmt_list attribute. */
7336
7337static struct type_unit_group *
7338get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7339{
7340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7341 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7342 struct type_unit_group *tu_group;
7343 void **slot;
7344 unsigned int line_offset;
7345 struct type_unit_group type_unit_group_for_lookup;
7346
7347 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7348 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7349
7350 /* Do we need to create a new group, or can we use an existing one? */
7351
7352 if (stmt_list)
7353 {
7354 line_offset = DW_UNSND (stmt_list);
7355 ++tu_stats->nr_symtab_sharers;
7356 }
7357 else
7358 {
7359 /* Ugh, no stmt_list. Rare, but we have to handle it.
7360 We can do various things here like create one group per TU or
7361 spread them over multiple groups to split up the expansion work.
7362 To avoid worst case scenarios (too many groups or too large groups)
7363 we, umm, group them in bunches. */
7364 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7365 | (tu_stats->nr_stmt_less_type_units
7366 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7367 ++tu_stats->nr_stmt_less_type_units;
7368 }
7369
7370 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7371 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7372 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7373 &type_unit_group_for_lookup, INSERT);
7374 if (*slot != NULL)
7375 {
7376 tu_group = (struct type_unit_group *) *slot;
7377 gdb_assert (tu_group != NULL);
7378 }
7379 else
7380 {
7381 sect_offset line_offset_struct = (sect_offset) line_offset;
7382 tu_group = create_type_unit_group (cu, line_offset_struct);
7383 *slot = tu_group;
7384 ++tu_stats->nr_symtabs;
7385 }
7386
7387 return tu_group;
7388}
7389\f
7390/* Partial symbol tables. */
7391
7392/* Create a psymtab named NAME and assign it to PER_CU.
7393
7394 The caller must fill in the following details:
7395 dirname, textlow, texthigh. */
7396
7397static dwarf2_psymtab *
7398create_partial_symtab (dwarf2_per_cu_data *per_cu,
7399 dwarf2_per_objfile *per_objfile,
7400 const char *name)
7401{
7402 struct objfile *objfile = per_objfile->objfile;
7403 dwarf2_psymtab *pst;
7404
7405 pst = new dwarf2_psymtab (name, objfile, per_cu);
7406
7407 pst->psymtabs_addrmap_supported = true;
7408
7409 /* This is the glue that links PST into GDB's symbol API. */
7410 per_cu->v.psymtab = pst;
7411
7412 return pst;
7413}
7414
7415/* DIE reader function for process_psymtab_comp_unit. */
7416
7417static void
7418process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7419 const gdb_byte *info_ptr,
7420 struct die_info *comp_unit_die,
7421 enum language pretend_language)
7422{
7423 struct dwarf2_cu *cu = reader->cu;
7424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7425 struct objfile *objfile = per_objfile->objfile;
7426 struct gdbarch *gdbarch = objfile->arch ();
7427 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7428 CORE_ADDR baseaddr;
7429 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7430 dwarf2_psymtab *pst;
7431 enum pc_bounds_kind cu_bounds_kind;
7432 const char *filename;
7433
7434 gdb_assert (! per_cu->is_debug_types);
7435
7436 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7437
7438 /* Allocate a new partial symbol table structure. */
7439 gdb::unique_xmalloc_ptr<char> debug_filename;
7440 static const char artificial[] = "<artificial>";
7441 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7442 if (filename == NULL)
7443 filename = "";
7444 else if (strcmp (filename, artificial) == 0)
7445 {
7446 debug_filename.reset (concat (artificial, "@",
7447 sect_offset_str (per_cu->sect_off),
7448 (char *) NULL));
7449 filename = debug_filename.get ();
7450 }
7451
7452 pst = create_partial_symtab (per_cu, per_objfile, filename);
7453
7454 /* This must be done before calling dwarf2_build_include_psymtabs. */
7455 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7456
7457 baseaddr = objfile->text_section_offset ();
7458
7459 dwarf2_find_base_address (comp_unit_die, cu);
7460
7461 /* Possibly set the default values of LOWPC and HIGHPC from
7462 `DW_AT_ranges'. */
7463 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7464 &best_highpc, cu, pst);
7465 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7466 {
7467 CORE_ADDR low
7468 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7469 - baseaddr);
7470 CORE_ADDR high
7471 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7472 - baseaddr - 1);
7473 /* Store the contiguous range if it is not empty; it can be
7474 empty for CUs with no code. */
7475 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7476 low, high, pst);
7477 }
7478
7479 /* Check if comp unit has_children.
7480 If so, read the rest of the partial symbols from this comp unit.
7481 If not, there's no more debug_info for this comp unit. */
7482 if (comp_unit_die->has_children)
7483 {
7484 struct partial_die_info *first_die;
7485 CORE_ADDR lowpc, highpc;
7486
7487 lowpc = ((CORE_ADDR) -1);
7488 highpc = ((CORE_ADDR) 0);
7489
7490 first_die = load_partial_dies (reader, info_ptr, 1);
7491
7492 scan_partial_symbols (first_die, &lowpc, &highpc,
7493 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7494
7495 /* If we didn't find a lowpc, set it to highpc to avoid
7496 complaints from `maint check'. */
7497 if (lowpc == ((CORE_ADDR) -1))
7498 lowpc = highpc;
7499
7500 /* If the compilation unit didn't have an explicit address range,
7501 then use the information extracted from its child dies. */
7502 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7503 {
7504 best_lowpc = lowpc;
7505 best_highpc = highpc;
7506 }
7507 }
7508 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7509 best_lowpc + baseaddr)
7510 - baseaddr);
7511 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7512 best_highpc + baseaddr)
7513 - baseaddr);
7514
7515 end_psymtab_common (objfile, pst);
7516
7517 if (!cu->per_cu->imported_symtabs_empty ())
7518 {
7519 int i;
7520 int len = cu->per_cu->imported_symtabs_size ();
7521
7522 /* Fill in 'dependencies' here; we fill in 'users' in a
7523 post-pass. */
7524 pst->number_of_dependencies = len;
7525 pst->dependencies
7526 = objfile->partial_symtabs->allocate_dependencies (len);
7527 for (i = 0; i < len; ++i)
7528 {
7529 pst->dependencies[i]
7530 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7531 }
7532
7533 cu->per_cu->imported_symtabs_free ();
7534 }
7535
7536 /* Get the list of files included in the current compilation unit,
7537 and build a psymtab for each of them. */
7538 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7539
7540 if (dwarf_read_debug)
7541 fprintf_unfiltered (gdb_stdlog,
7542 "Psymtab for %s unit @%s: %s - %s"
7543 ", %d global, %d static syms\n",
7544 per_cu->is_debug_types ? "type" : "comp",
7545 sect_offset_str (per_cu->sect_off),
7546 paddress (gdbarch, pst->text_low (objfile)),
7547 paddress (gdbarch, pst->text_high (objfile)),
7548 pst->n_global_syms, pst->n_static_syms);
7549}
7550
7551/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7552 Process compilation unit THIS_CU for a psymtab. */
7553
7554static void
7555process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7556 dwarf2_per_objfile *per_objfile,
7557 bool want_partial_unit,
7558 enum language pretend_language)
7559{
7560 /* If this compilation unit was already read in, free the
7561 cached copy in order to read it in again. This is
7562 necessary because we skipped some symbols when we first
7563 read in the compilation unit (see load_partial_dies).
7564 This problem could be avoided, but the benefit is unclear. */
7565 if (this_cu->cu != NULL)
7566 free_one_cached_comp_unit (this_cu, per_objfile);
7567
7568 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7569
7570 switch (reader.comp_unit_die->tag)
7571 {
7572 case DW_TAG_compile_unit:
7573 this_cu->unit_type = DW_UT_compile;
7574 break;
7575 case DW_TAG_partial_unit:
7576 this_cu->unit_type = DW_UT_partial;
7577 break;
7578 default:
7579 abort ();
7580 }
7581
7582 if (reader.dummy_p)
7583 {
7584 /* Nothing. */
7585 }
7586 else if (this_cu->is_debug_types)
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die);
7589 else if (want_partial_unit
7590 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7591 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die,
7593 pretend_language);
7594
7595 this_cu->lang = this_cu->cu->language;
7596
7597 /* Age out any secondary CUs. */
7598 age_cached_comp_units (per_objfile);
7599}
7600
7601/* Reader function for build_type_psymtabs. */
7602
7603static void
7604build_type_psymtabs_reader (const struct die_reader_specs *reader,
7605 const gdb_byte *info_ptr,
7606 struct die_info *type_unit_die)
7607{
7608 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7609 struct objfile *objfile = dwarf2_per_objfile->objfile;
7610 struct dwarf2_cu *cu = reader->cu;
7611 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7612 struct signatured_type *sig_type;
7613 struct type_unit_group *tu_group;
7614 struct attribute *attr;
7615 struct partial_die_info *first_die;
7616 CORE_ADDR lowpc, highpc;
7617 dwarf2_psymtab *pst;
7618
7619 gdb_assert (per_cu->is_debug_types);
7620 sig_type = (struct signatured_type *) per_cu;
7621
7622 if (! type_unit_die->has_children)
7623 return;
7624
7625 attr = type_unit_die->attr (DW_AT_stmt_list);
7626 tu_group = get_type_unit_group (cu, attr);
7627
7628 if (tu_group->tus == nullptr)
7629 tu_group->tus = new std::vector<signatured_type *>;
7630 tu_group->tus->push_back (sig_type);
7631
7632 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7633 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7634 pst->anonymous = true;
7635
7636 first_die = load_partial_dies (reader, info_ptr, 1);
7637
7638 lowpc = (CORE_ADDR) -1;
7639 highpc = (CORE_ADDR) 0;
7640 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7641
7642 end_psymtab_common (objfile, pst);
7643}
7644
7645/* Struct used to sort TUs by their abbreviation table offset. */
7646
7647struct tu_abbrev_offset
7648{
7649 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7650 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7651 {}
7652
7653 signatured_type *sig_type;
7654 sect_offset abbrev_offset;
7655};
7656
7657/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7658
7659static bool
7660sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7661 const struct tu_abbrev_offset &b)
7662{
7663 return a.abbrev_offset < b.abbrev_offset;
7664}
7665
7666/* Efficiently read all the type units.
7667 This does the bulk of the work for build_type_psymtabs.
7668
7669 The efficiency is because we sort TUs by the abbrev table they use and
7670 only read each abbrev table once. In one program there are 200K TUs
7671 sharing 8K abbrev tables.
7672
7673 The main purpose of this function is to support building the
7674 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7675 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7676 can collapse the search space by grouping them by stmt_list.
7677 The savings can be significant, in the same program from above the 200K TUs
7678 share 8K stmt_list tables.
7679
7680 FUNC is expected to call get_type_unit_group, which will create the
7681 struct type_unit_group if necessary and add it to
7682 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7683
7684static void
7685build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7686{
7687 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7688 abbrev_table_up abbrev_table;
7689 sect_offset abbrev_offset;
7690
7691 /* It's up to the caller to not call us multiple times. */
7692 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7693
7694 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7695 return;
7696
7697 /* TUs typically share abbrev tables, and there can be way more TUs than
7698 abbrev tables. Sort by abbrev table to reduce the number of times we
7699 read each abbrev table in.
7700 Alternatives are to punt or to maintain a cache of abbrev tables.
7701 This is simpler and efficient enough for now.
7702
7703 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7704 symtab to use). Typically TUs with the same abbrev offset have the same
7705 stmt_list value too so in practice this should work well.
7706
7707 The basic algorithm here is:
7708
7709 sort TUs by abbrev table
7710 for each TU with same abbrev table:
7711 read abbrev table if first user
7712 read TU top level DIE
7713 [IWBN if DWO skeletons had DW_AT_stmt_list]
7714 call FUNC */
7715
7716 if (dwarf_read_debug)
7717 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7718
7719 /* Sort in a separate table to maintain the order of all_type_units
7720 for .gdb_index: TU indices directly index all_type_units. */
7721 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7722 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7723
7724 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7725 sorted_by_abbrev.emplace_back
7726 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7727 sig_type->per_cu.section,
7728 sig_type->per_cu.sect_off));
7729
7730 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7731 sort_tu_by_abbrev_offset);
7732
7733 abbrev_offset = (sect_offset) ~(unsigned) 0;
7734
7735 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7736 {
7737 /* Switch to the next abbrev table if necessary. */
7738 if (abbrev_table == NULL
7739 || tu.abbrev_offset != abbrev_offset)
7740 {
7741 abbrev_offset = tu.abbrev_offset;
7742 abbrev_table =
7743 abbrev_table::read (dwarf2_per_objfile->objfile,
7744 &dwarf2_per_objfile->per_bfd->abbrev,
7745 abbrev_offset);
7746 ++tu_stats->nr_uniq_abbrev_tables;
7747 }
7748
7749 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7750 abbrev_table.get (), 0, false);
7751 if (!reader.dummy_p)
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
7753 reader.comp_unit_die);
7754 }
7755}
7756
7757/* Print collected type unit statistics. */
7758
7759static void
7760print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7761{
7762 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7763
7764 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7765 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7766 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7767 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7768 tu_stats->nr_uniq_abbrev_tables);
7769 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7770 tu_stats->nr_symtabs);
7771 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7772 tu_stats->nr_symtab_sharers);
7773 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7774 tu_stats->nr_stmt_less_type_units);
7775 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7776 tu_stats->nr_all_type_units_reallocs);
7777}
7778
7779/* Traversal function for build_type_psymtabs. */
7780
7781static int
7782build_type_psymtab_dependencies (void **slot, void *info)
7783{
7784 struct dwarf2_per_objfile *dwarf2_per_objfile
7785 = (struct dwarf2_per_objfile *) info;
7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
7787 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7788 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7789 dwarf2_psymtab *pst = per_cu->v.psymtab;
7790 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7791 int i;
7792
7793 gdb_assert (len > 0);
7794 gdb_assert (per_cu->type_unit_group_p ());
7795
7796 pst->number_of_dependencies = len;
7797 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7798 for (i = 0; i < len; ++i)
7799 {
7800 struct signatured_type *iter = tu_group->tus->at (i);
7801 gdb_assert (iter->per_cu.is_debug_types);
7802 pst->dependencies[i] = iter->per_cu.v.psymtab;
7803 iter->type_unit_group = tu_group;
7804 }
7805
7806 delete tu_group->tus;
7807 tu_group->tus = nullptr;
7808
7809 return 1;
7810}
7811
7812/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7813 Build partial symbol tables for the .debug_types comp-units. */
7814
7815static void
7816build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7817{
7818 if (! create_all_type_units (dwarf2_per_objfile))
7819 return;
7820
7821 build_type_psymtabs_1 (dwarf2_per_objfile);
7822}
7823
7824/* Traversal function for process_skeletonless_type_unit.
7825 Read a TU in a DWO file and build partial symbols for it. */
7826
7827static int
7828process_skeletonless_type_unit (void **slot, void *info)
7829{
7830 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7831 struct dwarf2_per_objfile *dwarf2_per_objfile
7832 = (struct dwarf2_per_objfile *) info;
7833 struct signatured_type find_entry, *entry;
7834
7835 /* If this TU doesn't exist in the global table, add it and read it in. */
7836
7837 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7838 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7839
7840 find_entry.signature = dwo_unit->signature;
7841 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7842 &find_entry, INSERT);
7843 /* If we've already seen this type there's nothing to do. What's happening
7844 is we're doing our own version of comdat-folding here. */
7845 if (*slot != NULL)
7846 return 1;
7847
7848 /* This does the job that create_all_type_units would have done for
7849 this TU. */
7850 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7851 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7852 *slot = entry;
7853
7854 /* This does the job that build_type_psymtabs_1 would have done. */
7855 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7856 if (!reader.dummy_p)
7857 build_type_psymtabs_reader (&reader, reader.info_ptr,
7858 reader.comp_unit_die);
7859
7860 return 1;
7861}
7862
7863/* Traversal function for process_skeletonless_type_units. */
7864
7865static int
7866process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7867{
7868 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7869
7870 if (dwo_file->tus != NULL)
7871 htab_traverse_noresize (dwo_file->tus.get (),
7872 process_skeletonless_type_unit, info);
7873
7874 return 1;
7875}
7876
7877/* Scan all TUs of DWO files, verifying we've processed them.
7878 This is needed in case a TU was emitted without its skeleton.
7879 Note: This can't be done until we know what all the DWO files are. */
7880
7881static void
7882process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7883{
7884 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7885 if (get_dwp_file (dwarf2_per_objfile) == NULL
7886 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7887 {
7888 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7889 process_dwo_file_for_skeletonless_type_units,
7890 dwarf2_per_objfile);
7891 }
7892}
7893
7894/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7895
7896static void
7897set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7898{
7899 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7900 {
7901 dwarf2_psymtab *pst = per_cu->v.psymtab;
7902
7903 if (pst == NULL)
7904 continue;
7905
7906 for (int j = 0; j < pst->number_of_dependencies; ++j)
7907 {
7908 /* Set the 'user' field only if it is not already set. */
7909 if (pst->dependencies[j]->user == NULL)
7910 pst->dependencies[j]->user = pst;
7911 }
7912 }
7913}
7914
7915/* Build the partial symbol table by doing a quick pass through the
7916 .debug_info and .debug_abbrev sections. */
7917
7918static void
7919dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7920{
7921 struct objfile *objfile = dwarf2_per_objfile->objfile;
7922
7923 if (dwarf_read_debug)
7924 {
7925 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7926 objfile_name (objfile));
7927 }
7928
7929 scoped_restore restore_reading_psyms
7930 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7931 true);
7932
7933 dwarf2_per_objfile->per_bfd->info.read (objfile);
7934
7935 /* Any cached compilation units will be linked by the per-objfile
7936 read_in_chain. Make sure to free them when we're done. */
7937 free_cached_comp_units freer (dwarf2_per_objfile);
7938
7939 build_type_psymtabs (dwarf2_per_objfile);
7940
7941 create_all_comp_units (dwarf2_per_objfile);
7942
7943 /* Create a temporary address map on a temporary obstack. We later
7944 copy this to the final obstack. */
7945 auto_obstack temp_obstack;
7946
7947 scoped_restore save_psymtabs_addrmap
7948 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7949 addrmap_create_mutable (&temp_obstack));
7950
7951 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7952 {
7953 if (per_cu->v.psymtab != NULL)
7954 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7955 continue;
7956 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7957 language_minimal);
7958 }
7959
7960 /* This has to wait until we read the CUs, we need the list of DWOs. */
7961 process_skeletonless_type_units (dwarf2_per_objfile);
7962
7963 /* Now that all TUs have been processed we can fill in the dependencies. */
7964 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7965 {
7966 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7967 build_type_psymtab_dependencies, dwarf2_per_objfile);
7968 }
7969
7970 if (dwarf_read_debug)
7971 print_tu_stats (dwarf2_per_objfile);
7972
7973 set_partial_user (dwarf2_per_objfile);
7974
7975 objfile->partial_symtabs->psymtabs_addrmap
7976 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7977 objfile->partial_symtabs->obstack ());
7978 /* At this point we want to keep the address map. */
7979 save_psymtabs_addrmap.release ();
7980
7981 if (dwarf_read_debug)
7982 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7983 objfile_name (objfile));
7984}
7985
7986/* Load the partial DIEs for a secondary CU into memory.
7987 This is also used when rereading a primary CU with load_all_dies. */
7988
7989static void
7990load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7991 dwarf2_per_objfile *per_objfile)
7992{
7993 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
7994
7995 if (!reader.dummy_p)
7996 {
7997 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7998 language_minimal);
7999
8000 /* Check if comp unit has_children.
8001 If so, read the rest of the partial symbols from this comp unit.
8002 If not, there's no more debug_info for this comp unit. */
8003 if (reader.comp_unit_die->has_children)
8004 load_partial_dies (&reader, reader.info_ptr, 0);
8005
8006 reader.keep ();
8007 }
8008}
8009
8010static void
8011read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8012 struct dwarf2_section_info *section,
8013 struct dwarf2_section_info *abbrev_section,
8014 unsigned int is_dwz)
8015{
8016 const gdb_byte *info_ptr;
8017 struct objfile *objfile = dwarf2_per_objfile->objfile;
8018
8019 if (dwarf_read_debug)
8020 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8021 section->get_name (),
8022 section->get_file_name ());
8023
8024 section->read (objfile);
8025
8026 info_ptr = section->buffer;
8027
8028 while (info_ptr < section->buffer + section->size)
8029 {
8030 struct dwarf2_per_cu_data *this_cu;
8031
8032 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8033
8034 comp_unit_head cu_header;
8035 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8036 abbrev_section, info_ptr,
8037 rcuh_kind::COMPILE);
8038
8039 /* Save the compilation unit for later lookup. */
8040 if (cu_header.unit_type != DW_UT_type)
8041 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8042 else
8043 {
8044 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8045 sig_type->signature = cu_header.signature;
8046 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8047 this_cu = &sig_type->per_cu;
8048 }
8049 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8050 this_cu->sect_off = sect_off;
8051 this_cu->length = cu_header.length + cu_header.initial_length_size;
8052 this_cu->is_dwz = is_dwz;
8053 this_cu->section = section;
8054
8055 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8056
8057 info_ptr = info_ptr + this_cu->length;
8058 }
8059}
8060
8061/* Create a list of all compilation units in OBJFILE.
8062 This is only done for -readnow and building partial symtabs. */
8063
8064static void
8065create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8066{
8067 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8068 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8069 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8070
8071 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8072 if (dwz != NULL)
8073 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8074 1);
8075}
8076
8077/* Process all loaded DIEs for compilation unit CU, starting at
8078 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8079 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8080 DW_AT_ranges). See the comments of add_partial_subprogram on how
8081 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8082
8083static void
8084scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8085 CORE_ADDR *highpc, int set_addrmap,
8086 struct dwarf2_cu *cu)
8087{
8088 struct partial_die_info *pdi;
8089
8090 /* Now, march along the PDI's, descending into ones which have
8091 interesting children but skipping the children of the other ones,
8092 until we reach the end of the compilation unit. */
8093
8094 pdi = first_die;
8095
8096 while (pdi != NULL)
8097 {
8098 pdi->fixup (cu);
8099
8100 /* Anonymous namespaces or modules have no name but have interesting
8101 children, so we need to look at them. Ditto for anonymous
8102 enums. */
8103
8104 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8105 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8106 || pdi->tag == DW_TAG_imported_unit
8107 || pdi->tag == DW_TAG_inlined_subroutine)
8108 {
8109 switch (pdi->tag)
8110 {
8111 case DW_TAG_subprogram:
8112 case DW_TAG_inlined_subroutine:
8113 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8114 break;
8115 case DW_TAG_constant:
8116 case DW_TAG_variable:
8117 case DW_TAG_typedef:
8118 case DW_TAG_union_type:
8119 if (!pdi->is_declaration
8120 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8121 {
8122 add_partial_symbol (pdi, cu);
8123 }
8124 break;
8125 case DW_TAG_class_type:
8126 case DW_TAG_interface_type:
8127 case DW_TAG_structure_type:
8128 if (!pdi->is_declaration)
8129 {
8130 add_partial_symbol (pdi, cu);
8131 }
8132 if ((cu->language == language_rust
8133 || cu->language == language_cplus) && pdi->has_children)
8134 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8135 set_addrmap, cu);
8136 break;
8137 case DW_TAG_enumeration_type:
8138 if (!pdi->is_declaration)
8139 add_partial_enumeration (pdi, cu);
8140 break;
8141 case DW_TAG_base_type:
8142 case DW_TAG_subrange_type:
8143 /* File scope base type definitions are added to the partial
8144 symbol table. */
8145 add_partial_symbol (pdi, cu);
8146 break;
8147 case DW_TAG_namespace:
8148 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8149 break;
8150 case DW_TAG_module:
8151 if (!pdi->is_declaration)
8152 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8153 break;
8154 case DW_TAG_imported_unit:
8155 {
8156 struct dwarf2_per_cu_data *per_cu;
8157
8158 /* For now we don't handle imported units in type units. */
8159 if (cu->per_cu->is_debug_types)
8160 {
8161 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8162 " supported in type units [in module %s]"),
8163 objfile_name (cu->per_objfile->objfile));
8164 }
8165
8166 per_cu = dwarf2_find_containing_comp_unit
8167 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8168
8169 /* Go read the partial unit, if needed. */
8170 if (per_cu->v.psymtab == NULL)
8171 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8172 cu->language);
8173
8174 cu->per_cu->imported_symtabs_push (per_cu);
8175 }
8176 break;
8177 case DW_TAG_imported_declaration:
8178 add_partial_symbol (pdi, cu);
8179 break;
8180 default:
8181 break;
8182 }
8183 }
8184
8185 /* If the die has a sibling, skip to the sibling. */
8186
8187 pdi = pdi->die_sibling;
8188 }
8189}
8190
8191/* Functions used to compute the fully scoped name of a partial DIE.
8192
8193 Normally, this is simple. For C++, the parent DIE's fully scoped
8194 name is concatenated with "::" and the partial DIE's name.
8195 Enumerators are an exception; they use the scope of their parent
8196 enumeration type, i.e. the name of the enumeration type is not
8197 prepended to the enumerator.
8198
8199 There are two complexities. One is DW_AT_specification; in this
8200 case "parent" means the parent of the target of the specification,
8201 instead of the direct parent of the DIE. The other is compilers
8202 which do not emit DW_TAG_namespace; in this case we try to guess
8203 the fully qualified name of structure types from their members'
8204 linkage names. This must be done using the DIE's children rather
8205 than the children of any DW_AT_specification target. We only need
8206 to do this for structures at the top level, i.e. if the target of
8207 any DW_AT_specification (if any; otherwise the DIE itself) does not
8208 have a parent. */
8209
8210/* Compute the scope prefix associated with PDI's parent, in
8211 compilation unit CU. The result will be allocated on CU's
8212 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8213 field. NULL is returned if no prefix is necessary. */
8214static const char *
8215partial_die_parent_scope (struct partial_die_info *pdi,
8216 struct dwarf2_cu *cu)
8217{
8218 const char *grandparent_scope;
8219 struct partial_die_info *parent, *real_pdi;
8220
8221 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8222 then this means the parent of the specification DIE. */
8223
8224 real_pdi = pdi;
8225 while (real_pdi->has_specification)
8226 {
8227 auto res = find_partial_die (real_pdi->spec_offset,
8228 real_pdi->spec_is_dwz, cu);
8229 real_pdi = res.pdi;
8230 cu = res.cu;
8231 }
8232
8233 parent = real_pdi->die_parent;
8234 if (parent == NULL)
8235 return NULL;
8236
8237 if (parent->scope_set)
8238 return parent->scope;
8239
8240 parent->fixup (cu);
8241
8242 grandparent_scope = partial_die_parent_scope (parent, cu);
8243
8244 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8245 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8246 Work around this problem here. */
8247 if (cu->language == language_cplus
8248 && parent->tag == DW_TAG_namespace
8249 && strcmp (parent->name, "::") == 0
8250 && grandparent_scope == NULL)
8251 {
8252 parent->scope = NULL;
8253 parent->scope_set = 1;
8254 return NULL;
8255 }
8256
8257 /* Nested subroutines in Fortran get a prefix. */
8258 if (pdi->tag == DW_TAG_enumerator)
8259 /* Enumerators should not get the name of the enumeration as a prefix. */
8260 parent->scope = grandparent_scope;
8261 else if (parent->tag == DW_TAG_namespace
8262 || parent->tag == DW_TAG_module
8263 || parent->tag == DW_TAG_structure_type
8264 || parent->tag == DW_TAG_class_type
8265 || parent->tag == DW_TAG_interface_type
8266 || parent->tag == DW_TAG_union_type
8267 || parent->tag == DW_TAG_enumeration_type
8268 || (cu->language == language_fortran
8269 && parent->tag == DW_TAG_subprogram
8270 && pdi->tag == DW_TAG_subprogram))
8271 {
8272 if (grandparent_scope == NULL)
8273 parent->scope = parent->name;
8274 else
8275 parent->scope = typename_concat (&cu->comp_unit_obstack,
8276 grandparent_scope,
8277 parent->name, 0, cu);
8278 }
8279 else
8280 {
8281 /* FIXME drow/2004-04-01: What should we be doing with
8282 function-local names? For partial symbols, we should probably be
8283 ignoring them. */
8284 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8285 dwarf_tag_name (parent->tag),
8286 sect_offset_str (pdi->sect_off));
8287 parent->scope = grandparent_scope;
8288 }
8289
8290 parent->scope_set = 1;
8291 return parent->scope;
8292}
8293
8294/* Return the fully scoped name associated with PDI, from compilation unit
8295 CU. The result will be allocated with malloc. */
8296
8297static gdb::unique_xmalloc_ptr<char>
8298partial_die_full_name (struct partial_die_info *pdi,
8299 struct dwarf2_cu *cu)
8300{
8301 const char *parent_scope;
8302
8303 /* If this is a template instantiation, we can not work out the
8304 template arguments from partial DIEs. So, unfortunately, we have
8305 to go through the full DIEs. At least any work we do building
8306 types here will be reused if full symbols are loaded later. */
8307 if (pdi->has_template_arguments)
8308 {
8309 pdi->fixup (cu);
8310
8311 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8312 {
8313 struct die_info *die;
8314 struct attribute attr;
8315 struct dwarf2_cu *ref_cu = cu;
8316
8317 /* DW_FORM_ref_addr is using section offset. */
8318 attr.name = (enum dwarf_attribute) 0;
8319 attr.form = DW_FORM_ref_addr;
8320 attr.u.unsnd = to_underlying (pdi->sect_off);
8321 die = follow_die_ref (NULL, &attr, &ref_cu);
8322
8323 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8324 }
8325 }
8326
8327 parent_scope = partial_die_parent_scope (pdi, cu);
8328 if (parent_scope == NULL)
8329 return NULL;
8330 else
8331 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8332 pdi->name, 0, cu));
8333}
8334
8335static void
8336add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8337{
8338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8339 struct objfile *objfile = dwarf2_per_objfile->objfile;
8340 struct gdbarch *gdbarch = objfile->arch ();
8341 CORE_ADDR addr = 0;
8342 const char *actual_name = NULL;
8343 CORE_ADDR baseaddr;
8344
8345 baseaddr = objfile->text_section_offset ();
8346
8347 gdb::unique_xmalloc_ptr<char> built_actual_name
8348 = partial_die_full_name (pdi, cu);
8349 if (built_actual_name != NULL)
8350 actual_name = built_actual_name.get ();
8351
8352 if (actual_name == NULL)
8353 actual_name = pdi->name;
8354
8355 partial_symbol psymbol;
8356 memset (&psymbol, 0, sizeof (psymbol));
8357 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8358 psymbol.ginfo.section = -1;
8359
8360 /* The code below indicates that the psymbol should be installed by
8361 setting this. */
8362 gdb::optional<psymbol_placement> where;
8363
8364 switch (pdi->tag)
8365 {
8366 case DW_TAG_inlined_subroutine:
8367 case DW_TAG_subprogram:
8368 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8369 - baseaddr);
8370 if (pdi->is_external
8371 || cu->language == language_ada
8372 || (cu->language == language_fortran
8373 && pdi->die_parent != NULL
8374 && pdi->die_parent->tag == DW_TAG_subprogram))
8375 {
8376 /* Normally, only "external" DIEs are part of the global scope.
8377 But in Ada and Fortran, we want to be able to access nested
8378 procedures globally. So all Ada and Fortran subprograms are
8379 stored in the global scope. */
8380 where = psymbol_placement::GLOBAL;
8381 }
8382 else
8383 where = psymbol_placement::STATIC;
8384
8385 psymbol.domain = VAR_DOMAIN;
8386 psymbol.aclass = LOC_BLOCK;
8387 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8388 psymbol.ginfo.value.address = addr;
8389
8390 if (pdi->main_subprogram && actual_name != NULL)
8391 set_objfile_main_name (objfile, actual_name, cu->language);
8392 break;
8393 case DW_TAG_constant:
8394 psymbol.domain = VAR_DOMAIN;
8395 psymbol.aclass = LOC_STATIC;
8396 where = (pdi->is_external
8397 ? psymbol_placement::GLOBAL
8398 : psymbol_placement::STATIC);
8399 break;
8400 case DW_TAG_variable:
8401 if (pdi->d.locdesc)
8402 addr = decode_locdesc (pdi->d.locdesc, cu);
8403
8404 if (pdi->d.locdesc
8405 && addr == 0
8406 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8407 {
8408 /* A global or static variable may also have been stripped
8409 out by the linker if unused, in which case its address
8410 will be nullified; do not add such variables into partial
8411 symbol table then. */
8412 }
8413 else if (pdi->is_external)
8414 {
8415 /* Global Variable.
8416 Don't enter into the minimal symbol tables as there is
8417 a minimal symbol table entry from the ELF symbols already.
8418 Enter into partial symbol table if it has a location
8419 descriptor or a type.
8420 If the location descriptor is missing, new_symbol will create
8421 a LOC_UNRESOLVED symbol, the address of the variable will then
8422 be determined from the minimal symbol table whenever the variable
8423 is referenced.
8424 The address for the partial symbol table entry is not
8425 used by GDB, but it comes in handy for debugging partial symbol
8426 table building. */
8427
8428 if (pdi->d.locdesc || pdi->has_type)
8429 {
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8433 psymbol.ginfo.value.address = addr;
8434 where = psymbol_placement::GLOBAL;
8435 }
8436 }
8437 else
8438 {
8439 int has_loc = pdi->d.locdesc != NULL;
8440
8441 /* Static Variable. Skip symbols whose value we cannot know (those
8442 without location descriptors or constant values). */
8443 if (!has_loc && !pdi->has_const_value)
8444 return;
8445
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_STATIC;
8448 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8449 if (has_loc)
8450 psymbol.ginfo.value.address = addr;
8451 where = psymbol_placement::STATIC;
8452 }
8453 break;
8454 case DW_TAG_typedef:
8455 case DW_TAG_base_type:
8456 case DW_TAG_subrange_type:
8457 psymbol.domain = VAR_DOMAIN;
8458 psymbol.aclass = LOC_TYPEDEF;
8459 where = psymbol_placement::STATIC;
8460 break;
8461 case DW_TAG_imported_declaration:
8462 case DW_TAG_namespace:
8463 psymbol.domain = VAR_DOMAIN;
8464 psymbol.aclass = LOC_TYPEDEF;
8465 where = psymbol_placement::GLOBAL;
8466 break;
8467 case DW_TAG_module:
8468 /* With Fortran 77 there might be a "BLOCK DATA" module
8469 available without any name. If so, we skip the module as it
8470 doesn't bring any value. */
8471 if (actual_name != nullptr)
8472 {
8473 psymbol.domain = MODULE_DOMAIN;
8474 psymbol.aclass = LOC_TYPEDEF;
8475 where = psymbol_placement::GLOBAL;
8476 }
8477 break;
8478 case DW_TAG_class_type:
8479 case DW_TAG_interface_type:
8480 case DW_TAG_structure_type:
8481 case DW_TAG_union_type:
8482 case DW_TAG_enumeration_type:
8483 /* Skip external references. The DWARF standard says in the section
8484 about "Structure, Union, and Class Type Entries": "An incomplete
8485 structure, union or class type is represented by a structure,
8486 union or class entry that does not have a byte size attribute
8487 and that has a DW_AT_declaration attribute." */
8488 if (!pdi->has_byte_size && pdi->is_declaration)
8489 return;
8490
8491 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8492 static vs. global. */
8493 psymbol.domain = STRUCT_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = (cu->language == language_cplus
8496 ? psymbol_placement::GLOBAL
8497 : psymbol_placement::STATIC);
8498 break;
8499 case DW_TAG_enumerator:
8500 psymbol.domain = VAR_DOMAIN;
8501 psymbol.aclass = LOC_CONST;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
8505 break;
8506 default:
8507 break;
8508 }
8509
8510 if (where.has_value ())
8511 {
8512 if (built_actual_name != nullptr)
8513 actual_name = objfile->intern (actual_name);
8514 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8515 psymbol.ginfo.set_linkage_name (actual_name);
8516 else
8517 {
8518 psymbol.ginfo.set_demangled_name (actual_name,
8519 &objfile->objfile_obstack);
8520 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8521 }
8522 add_psymbol_to_list (psymbol, *where, objfile);
8523 }
8524}
8525
8526/* Read a partial die corresponding to a namespace; also, add a symbol
8527 corresponding to that namespace to the symbol table. NAMESPACE is
8528 the name of the enclosing namespace. */
8529
8530static void
8531add_partial_namespace (struct partial_die_info *pdi,
8532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8533 int set_addrmap, struct dwarf2_cu *cu)
8534{
8535 /* Add a symbol for the namespace. */
8536
8537 add_partial_symbol (pdi, cu);
8538
8539 /* Now scan partial symbols in that namespace. */
8540
8541 if (pdi->has_children)
8542 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8543}
8544
8545/* Read a partial die corresponding to a Fortran module. */
8546
8547static void
8548add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8549 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8550{
8551 /* Add a symbol for the namespace. */
8552
8553 add_partial_symbol (pdi, cu);
8554
8555 /* Now scan partial symbols in that module. */
8556
8557 if (pdi->has_children)
8558 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8559}
8560
8561/* Read a partial die corresponding to a subprogram or an inlined
8562 subprogram and create a partial symbol for that subprogram.
8563 When the CU language allows it, this routine also defines a partial
8564 symbol for each nested subprogram that this subprogram contains.
8565 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8566 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8567
8568 PDI may also be a lexical block, in which case we simply search
8569 recursively for subprograms defined inside that lexical block.
8570 Again, this is only performed when the CU language allows this
8571 type of definitions. */
8572
8573static void
8574add_partial_subprogram (struct partial_die_info *pdi,
8575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8576 int set_addrmap, struct dwarf2_cu *cu)
8577{
8578 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8579 {
8580 if (pdi->has_pc_info)
8581 {
8582 if (pdi->lowpc < *lowpc)
8583 *lowpc = pdi->lowpc;
8584 if (pdi->highpc > *highpc)
8585 *highpc = pdi->highpc;
8586 if (set_addrmap)
8587 {
8588 struct objfile *objfile = cu->per_objfile->objfile;
8589 struct gdbarch *gdbarch = objfile->arch ();
8590 CORE_ADDR baseaddr;
8591 CORE_ADDR this_highpc;
8592 CORE_ADDR this_lowpc;
8593
8594 baseaddr = objfile->text_section_offset ();
8595 this_lowpc
8596 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8597 pdi->lowpc + baseaddr)
8598 - baseaddr);
8599 this_highpc
8600 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8601 pdi->highpc + baseaddr)
8602 - baseaddr);
8603 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8604 this_lowpc, this_highpc - 1,
8605 cu->per_cu->v.psymtab);
8606 }
8607 }
8608
8609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8610 {
8611 if (!pdi->is_declaration)
8612 /* Ignore subprogram DIEs that do not have a name, they are
8613 illegal. Do not emit a complaint at this point, we will
8614 do so when we convert this psymtab into a symtab. */
8615 if (pdi->name)
8616 add_partial_symbol (pdi, cu);
8617 }
8618 }
8619
8620 if (! pdi->has_children)
8621 return;
8622
8623 if (cu->language == language_ada || cu->language == language_fortran)
8624 {
8625 pdi = pdi->die_child;
8626 while (pdi != NULL)
8627 {
8628 pdi->fixup (cu);
8629 if (pdi->tag == DW_TAG_subprogram
8630 || pdi->tag == DW_TAG_inlined_subroutine
8631 || pdi->tag == DW_TAG_lexical_block)
8632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8633 pdi = pdi->die_sibling;
8634 }
8635 }
8636}
8637
8638/* Read a partial die corresponding to an enumeration type. */
8639
8640static void
8641add_partial_enumeration (struct partial_die_info *enum_pdi,
8642 struct dwarf2_cu *cu)
8643{
8644 struct partial_die_info *pdi;
8645
8646 if (enum_pdi->name != NULL)
8647 add_partial_symbol (enum_pdi, cu);
8648
8649 pdi = enum_pdi->die_child;
8650 while (pdi)
8651 {
8652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8653 complaint (_("malformed enumerator DIE ignored"));
8654 else
8655 add_partial_symbol (pdi, cu);
8656 pdi = pdi->die_sibling;
8657 }
8658}
8659
8660/* Return the initial uleb128 in the die at INFO_PTR. */
8661
8662static unsigned int
8663peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8664{
8665 unsigned int bytes_read;
8666
8667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8668}
8669
8670/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8672
8673 Return the corresponding abbrev, or NULL if the number is zero (indicating
8674 an empty DIE). In either case *BYTES_READ will be set to the length of
8675 the initial number. */
8676
8677static struct abbrev_info *
8678peek_die_abbrev (const die_reader_specs &reader,
8679 const gdb_byte *info_ptr, unsigned int *bytes_read)
8680{
8681 dwarf2_cu *cu = reader.cu;
8682 bfd *abfd = cu->per_objfile->objfile->obfd;
8683 unsigned int abbrev_number
8684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8685
8686 if (abbrev_number == 0)
8687 return NULL;
8688
8689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8690 if (!abbrev)
8691 {
8692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8693 " at offset %s [in module %s]"),
8694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8696 }
8697
8698 return abbrev;
8699}
8700
8701/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8702 Returns a pointer to the end of a series of DIEs, terminated by an empty
8703 DIE. Any children of the skipped DIEs will also be skipped. */
8704
8705static const gdb_byte *
8706skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8707{
8708 while (1)
8709 {
8710 unsigned int bytes_read;
8711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8712
8713 if (abbrev == NULL)
8714 return info_ptr + bytes_read;
8715 else
8716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8717 }
8718}
8719
8720/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8722 abbrev corresponding to that skipped uleb128 should be passed in
8723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8724 children. */
8725
8726static const gdb_byte *
8727skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8728 struct abbrev_info *abbrev)
8729{
8730 unsigned int bytes_read;
8731 struct attribute attr;
8732 bfd *abfd = reader->abfd;
8733 struct dwarf2_cu *cu = reader->cu;
8734 const gdb_byte *buffer = reader->buffer;
8735 const gdb_byte *buffer_end = reader->buffer_end;
8736 unsigned int form, i;
8737
8738 for (i = 0; i < abbrev->num_attrs; i++)
8739 {
8740 /* The only abbrev we care about is DW_AT_sibling. */
8741 if (abbrev->attrs[i].name == DW_AT_sibling)
8742 {
8743 bool ignored;
8744 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8745 &ignored);
8746 if (attr.form == DW_FORM_ref_addr)
8747 complaint (_("ignoring absolute DW_AT_sibling"));
8748 else
8749 {
8750 sect_offset off = attr.get_ref_die_offset ();
8751 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8752
8753 if (sibling_ptr < info_ptr)
8754 complaint (_("DW_AT_sibling points backwards"));
8755 else if (sibling_ptr > reader->buffer_end)
8756 reader->die_section->overflow_complaint ();
8757 else
8758 return sibling_ptr;
8759 }
8760 }
8761
8762 /* If it isn't DW_AT_sibling, skip this attribute. */
8763 form = abbrev->attrs[i].form;
8764 skip_attribute:
8765 switch (form)
8766 {
8767 case DW_FORM_ref_addr:
8768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8769 and later it is offset sized. */
8770 if (cu->header.version == 2)
8771 info_ptr += cu->header.addr_size;
8772 else
8773 info_ptr += cu->header.offset_size;
8774 break;
8775 case DW_FORM_GNU_ref_alt:
8776 info_ptr += cu->header.offset_size;
8777 break;
8778 case DW_FORM_addr:
8779 info_ptr += cu->header.addr_size;
8780 break;
8781 case DW_FORM_data1:
8782 case DW_FORM_ref1:
8783 case DW_FORM_flag:
8784 case DW_FORM_strx1:
8785 info_ptr += 1;
8786 break;
8787 case DW_FORM_flag_present:
8788 case DW_FORM_implicit_const:
8789 break;
8790 case DW_FORM_data2:
8791 case DW_FORM_ref2:
8792 case DW_FORM_strx2:
8793 info_ptr += 2;
8794 break;
8795 case DW_FORM_strx3:
8796 info_ptr += 3;
8797 break;
8798 case DW_FORM_data4:
8799 case DW_FORM_ref4:
8800 case DW_FORM_strx4:
8801 info_ptr += 4;
8802 break;
8803 case DW_FORM_data8:
8804 case DW_FORM_ref8:
8805 case DW_FORM_ref_sig8:
8806 info_ptr += 8;
8807 break;
8808 case DW_FORM_data16:
8809 info_ptr += 16;
8810 break;
8811 case DW_FORM_string:
8812 read_direct_string (abfd, info_ptr, &bytes_read);
8813 info_ptr += bytes_read;
8814 break;
8815 case DW_FORM_sec_offset:
8816 case DW_FORM_strp:
8817 case DW_FORM_GNU_strp_alt:
8818 info_ptr += cu->header.offset_size;
8819 break;
8820 case DW_FORM_exprloc:
8821 case DW_FORM_block:
8822 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8823 info_ptr += bytes_read;
8824 break;
8825 case DW_FORM_block1:
8826 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8827 break;
8828 case DW_FORM_block2:
8829 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8830 break;
8831 case DW_FORM_block4:
8832 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8833 break;
8834 case DW_FORM_addrx:
8835 case DW_FORM_strx:
8836 case DW_FORM_sdata:
8837 case DW_FORM_udata:
8838 case DW_FORM_ref_udata:
8839 case DW_FORM_GNU_addr_index:
8840 case DW_FORM_GNU_str_index:
8841 case DW_FORM_rnglistx:
8842 case DW_FORM_loclistx:
8843 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8844 break;
8845 case DW_FORM_indirect:
8846 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8847 info_ptr += bytes_read;
8848 /* We need to continue parsing from here, so just go back to
8849 the top. */
8850 goto skip_attribute;
8851
8852 default:
8853 error (_("Dwarf Error: Cannot handle %s "
8854 "in DWARF reader [in module %s]"),
8855 dwarf_form_name (form),
8856 bfd_get_filename (abfd));
8857 }
8858 }
8859
8860 if (abbrev->has_children)
8861 return skip_children (reader, info_ptr);
8862 else
8863 return info_ptr;
8864}
8865
8866/* Locate ORIG_PDI's sibling.
8867 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8868
8869static const gdb_byte *
8870locate_pdi_sibling (const struct die_reader_specs *reader,
8871 struct partial_die_info *orig_pdi,
8872 const gdb_byte *info_ptr)
8873{
8874 /* Do we know the sibling already? */
8875
8876 if (orig_pdi->sibling)
8877 return orig_pdi->sibling;
8878
8879 /* Are there any children to deal with? */
8880
8881 if (!orig_pdi->has_children)
8882 return info_ptr;
8883
8884 /* Skip the children the long way. */
8885
8886 return skip_children (reader, info_ptr);
8887}
8888
8889/* Expand this partial symbol table into a full symbol table. SELF is
8890 not NULL. */
8891
8892void
8893dwarf2_psymtab::read_symtab (struct objfile *objfile)
8894{
8895 struct dwarf2_per_objfile *dwarf2_per_objfile
8896 = get_dwarf2_per_objfile (objfile);
8897
8898 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8899
8900 /* If this psymtab is constructed from a debug-only objfile, the
8901 has_section_at_zero flag will not necessarily be correct. We
8902 can get the correct value for this flag by looking at the data
8903 associated with the (presumably stripped) associated objfile. */
8904 if (objfile->separate_debug_objfile_backlink)
8905 {
8906 struct dwarf2_per_objfile *dpo_backlink
8907 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8908
8909 dwarf2_per_objfile->per_bfd->has_section_at_zero
8910 = dpo_backlink->per_bfd->has_section_at_zero;
8911 }
8912
8913 expand_psymtab (objfile);
8914
8915 process_cu_includes (dwarf2_per_objfile);
8916}
8917\f
8918/* Reading in full CUs. */
8919
8920/* Add PER_CU to the queue. */
8921
8922static void
8923queue_comp_unit (dwarf2_per_cu_data *per_cu,
8924 dwarf2_per_objfile *per_objfile,
8925 enum language pretend_language)
8926{
8927 per_cu->queued = 1;
8928 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8929}
8930
8931/* If PER_CU is not yet queued, add it to the queue.
8932 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8933 dependency.
8934 The result is non-zero if PER_CU was queued, otherwise the result is zero
8935 meaning either PER_CU is already queued or it is already loaded.
8936
8937 N.B. There is an invariant here that if a CU is queued then it is loaded.
8938 The caller is required to load PER_CU if we return non-zero. */
8939
8940static int
8941maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8942 dwarf2_per_cu_data *per_cu,
8943 dwarf2_per_objfile *per_objfile,
8944 enum language pretend_language)
8945{
8946 /* We may arrive here during partial symbol reading, if we need full
8947 DIEs to process an unusual case (e.g. template arguments). Do
8948 not queue PER_CU, just tell our caller to load its DIEs. */
8949 if (per_cu->per_bfd->reading_partial_symbols)
8950 {
8951 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8952 return 1;
8953 return 0;
8954 }
8955
8956 /* Mark the dependence relation so that we don't flush PER_CU
8957 too early. */
8958 if (dependent_cu != NULL)
8959 dwarf2_add_dependence (dependent_cu, per_cu);
8960
8961 /* If it's already on the queue, we have nothing to do. */
8962 if (per_cu->queued)
8963 return 0;
8964
8965 /* If the compilation unit is already loaded, just mark it as
8966 used. */
8967 if (per_cu->cu != NULL)
8968 {
8969 per_cu->cu->last_used = 0;
8970 return 0;
8971 }
8972
8973 /* Add it to the queue. */
8974 queue_comp_unit (per_cu, per_objfile, pretend_language);
8975
8976 return 1;
8977}
8978
8979/* Process the queue. */
8980
8981static void
8982process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8983{
8984 if (dwarf_read_debug)
8985 {
8986 fprintf_unfiltered (gdb_stdlog,
8987 "Expanding one or more symtabs of objfile %s ...\n",
8988 objfile_name (dwarf2_per_objfile->objfile));
8989 }
8990
8991 /* The queue starts out with one item, but following a DIE reference
8992 may load a new CU, adding it to the end of the queue. */
8993 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8994 {
8995 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8996
8997 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
8998 /* Skip dummy CUs. */
8999 && item.per_cu->cu != NULL)
9000 {
9001 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9002 unsigned int debug_print_threshold;
9003 char buf[100];
9004
9005 if (per_cu->is_debug_types)
9006 {
9007 struct signatured_type *sig_type =
9008 (struct signatured_type *) per_cu;
9009
9010 sprintf (buf, "TU %s at offset %s",
9011 hex_string (sig_type->signature),
9012 sect_offset_str (per_cu->sect_off));
9013 /* There can be 100s of TUs.
9014 Only print them in verbose mode. */
9015 debug_print_threshold = 2;
9016 }
9017 else
9018 {
9019 sprintf (buf, "CU at offset %s",
9020 sect_offset_str (per_cu->sect_off));
9021 debug_print_threshold = 1;
9022 }
9023
9024 if (dwarf_read_debug >= debug_print_threshold)
9025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9026
9027 if (per_cu->is_debug_types)
9028 process_full_type_unit (per_cu, dwarf2_per_objfile,
9029 item.pretend_language);
9030 else
9031 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9032 item.pretend_language);
9033
9034 if (dwarf_read_debug >= debug_print_threshold)
9035 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9036 }
9037
9038 item.per_cu->queued = 0;
9039 dwarf2_per_objfile->per_bfd->queue.pop ();
9040 }
9041
9042 if (dwarf_read_debug)
9043 {
9044 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9045 objfile_name (dwarf2_per_objfile->objfile));
9046 }
9047}
9048
9049/* Read in full symbols for PST, and anything it depends on. */
9050
9051void
9052dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9053{
9054 gdb_assert (!readin_p (objfile));
9055
9056 expand_dependencies (objfile);
9057
9058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9059 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9060 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9061}
9062
9063/* See psympriv.h. */
9064
9065bool
9066dwarf2_psymtab::readin_p (struct objfile *objfile) const
9067{
9068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9069 return per_objfile->symtab_set_p (per_cu_data);
9070}
9071
9072/* See psympriv.h. */
9073
9074compunit_symtab *
9075dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9076{
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 return per_objfile->get_symtab (per_cu_data);
9079}
9080
9081/* Trivial hash function for die_info: the hash value of a DIE
9082 is its offset in .debug_info for this objfile. */
9083
9084static hashval_t
9085die_hash (const void *item)
9086{
9087 const struct die_info *die = (const struct die_info *) item;
9088
9089 return to_underlying (die->sect_off);
9090}
9091
9092/* Trivial comparison function for die_info structures: two DIEs
9093 are equal if they have the same offset. */
9094
9095static int
9096die_eq (const void *item_lhs, const void *item_rhs)
9097{
9098 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9099 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9100
9101 return die_lhs->sect_off == die_rhs->sect_off;
9102}
9103
9104/* Load the DIEs associated with PER_CU into memory. */
9105
9106static void
9107load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9108 dwarf2_per_objfile *per_objfile,
9109 bool skip_partial,
9110 enum language pretend_language)
9111{
9112 gdb_assert (! this_cu->is_debug_types);
9113
9114 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9115 if (reader.dummy_p)
9116 return;
9117
9118 struct dwarf2_cu *cu = reader.cu;
9119 const gdb_byte *info_ptr = reader.info_ptr;
9120
9121 gdb_assert (cu->die_hash == NULL);
9122 cu->die_hash =
9123 htab_create_alloc_ex (cu->header.length / 12,
9124 die_hash,
9125 die_eq,
9126 NULL,
9127 &cu->comp_unit_obstack,
9128 hashtab_obstack_allocate,
9129 dummy_obstack_deallocate);
9130
9131 if (reader.comp_unit_die->has_children)
9132 reader.comp_unit_die->child
9133 = read_die_and_siblings (&reader, reader.info_ptr,
9134 &info_ptr, reader.comp_unit_die);
9135 cu->dies = reader.comp_unit_die;
9136 /* comp_unit_die is not stored in die_hash, no need. */
9137
9138 /* We try not to read any attributes in this function, because not
9139 all CUs needed for references have been loaded yet, and symbol
9140 table processing isn't initialized. But we have to set the CU language,
9141 or we won't be able to build types correctly.
9142 Similarly, if we do not read the producer, we can not apply
9143 producer-specific interpretation. */
9144 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9145
9146 reader.keep ();
9147}
9148
9149/* Add a DIE to the delayed physname list. */
9150
9151static void
9152add_to_method_list (struct type *type, int fnfield_index, int index,
9153 const char *name, struct die_info *die,
9154 struct dwarf2_cu *cu)
9155{
9156 struct delayed_method_info mi;
9157 mi.type = type;
9158 mi.fnfield_index = fnfield_index;
9159 mi.index = index;
9160 mi.name = name;
9161 mi.die = die;
9162 cu->method_list.push_back (mi);
9163}
9164
9165/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9166 "const" / "volatile". If so, decrements LEN by the length of the
9167 modifier and return true. Otherwise return false. */
9168
9169template<size_t N>
9170static bool
9171check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9172{
9173 size_t mod_len = sizeof (mod) - 1;
9174 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9175 {
9176 len -= mod_len;
9177 return true;
9178 }
9179 return false;
9180}
9181
9182/* Compute the physnames of any methods on the CU's method list.
9183
9184 The computation of method physnames is delayed in order to avoid the
9185 (bad) condition that one of the method's formal parameters is of an as yet
9186 incomplete type. */
9187
9188static void
9189compute_delayed_physnames (struct dwarf2_cu *cu)
9190{
9191 /* Only C++ delays computing physnames. */
9192 if (cu->method_list.empty ())
9193 return;
9194 gdb_assert (cu->language == language_cplus);
9195
9196 for (const delayed_method_info &mi : cu->method_list)
9197 {
9198 const char *physname;
9199 struct fn_fieldlist *fn_flp
9200 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9201 physname = dwarf2_physname (mi.name, mi.die, cu);
9202 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9203 = physname ? physname : "";
9204
9205 /* Since there's no tag to indicate whether a method is a
9206 const/volatile overload, extract that information out of the
9207 demangled name. */
9208 if (physname != NULL)
9209 {
9210 size_t len = strlen (physname);
9211
9212 while (1)
9213 {
9214 if (physname[len] == ')') /* shortcut */
9215 break;
9216 else if (check_modifier (physname, len, " const"))
9217 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9218 else if (check_modifier (physname, len, " volatile"))
9219 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9220 else
9221 break;
9222 }
9223 }
9224 }
9225
9226 /* The list is no longer needed. */
9227 cu->method_list.clear ();
9228}
9229
9230/* Go objects should be embedded in a DW_TAG_module DIE,
9231 and it's not clear if/how imported objects will appear.
9232 To keep Go support simple until that's worked out,
9233 go back through what we've read and create something usable.
9234 We could do this while processing each DIE, and feels kinda cleaner,
9235 but that way is more invasive.
9236 This is to, for example, allow the user to type "p var" or "b main"
9237 without having to specify the package name, and allow lookups
9238 of module.object to work in contexts that use the expression
9239 parser. */
9240
9241static void
9242fixup_go_packaging (struct dwarf2_cu *cu)
9243{
9244 gdb::unique_xmalloc_ptr<char> package_name;
9245 struct pending *list;
9246 int i;
9247
9248 for (list = *cu->get_builder ()->get_global_symbols ();
9249 list != NULL;
9250 list = list->next)
9251 {
9252 for (i = 0; i < list->nsyms; ++i)
9253 {
9254 struct symbol *sym = list->symbol[i];
9255
9256 if (sym->language () == language_go
9257 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9258 {
9259 gdb::unique_xmalloc_ptr<char> this_package_name
9260 (go_symbol_package_name (sym));
9261
9262 if (this_package_name == NULL)
9263 continue;
9264 if (package_name == NULL)
9265 package_name = std::move (this_package_name);
9266 else
9267 {
9268 struct objfile *objfile = cu->per_objfile->objfile;
9269 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9270 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9271 (symbol_symtab (sym) != NULL
9272 ? symtab_to_filename_for_display
9273 (symbol_symtab (sym))
9274 : objfile_name (objfile)),
9275 this_package_name.get (), package_name.get ());
9276 }
9277 }
9278 }
9279 }
9280
9281 if (package_name != NULL)
9282 {
9283 struct objfile *objfile = cu->per_objfile->objfile;
9284 const char *saved_package_name = objfile->intern (package_name.get ());
9285 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9286 saved_package_name);
9287 struct symbol *sym;
9288
9289 sym = new (&objfile->objfile_obstack) symbol;
9290 sym->set_language (language_go, &objfile->objfile_obstack);
9291 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9292 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9293 e.g., "main" finds the "main" module and not C's main(). */
9294 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9296 SYMBOL_TYPE (sym) = type;
9297
9298 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9299 }
9300}
9301
9302/* Allocate a fully-qualified name consisting of the two parts on the
9303 obstack. */
9304
9305static const char *
9306rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9307{
9308 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9309}
9310
9311/* A helper that allocates a variant part to attach to a Rust enum
9312 type. OBSTACK is where the results should be allocated. TYPE is
9313 the type we're processing. DISCRIMINANT_INDEX is the index of the
9314 discriminant. It must be the index of one of the fields of TYPE.
9315 DEFAULT_INDEX is the index of the default field; or -1 if there is
9316 no default. RANGES is indexed by "effective" field number (the
9317 field index, but omitting the discriminant and default fields) and
9318 must hold the discriminant values used by the variants. Note that
9319 RANGES must have a lifetime at least as long as OBSTACK -- either
9320 already allocated on it, or static. */
9321
9322static void
9323alloc_rust_variant (struct obstack *obstack, struct type *type,
9324 int discriminant_index, int default_index,
9325 gdb::array_view<discriminant_range> ranges)
9326{
9327 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9328 must be handled by the caller. */
9329 gdb_assert (discriminant_index >= 0
9330 && discriminant_index < type->num_fields ());
9331 gdb_assert (default_index == -1
9332 || (default_index >= 0 && default_index < type->num_fields ()));
9333
9334 /* We have one variant for each non-discriminant field. */
9335 int n_variants = type->num_fields () - 1;
9336
9337 variant *variants = new (obstack) variant[n_variants];
9338 int var_idx = 0;
9339 int range_idx = 0;
9340 for (int i = 0; i < type->num_fields (); ++i)
9341 {
9342 if (i == discriminant_index)
9343 continue;
9344
9345 variants[var_idx].first_field = i;
9346 variants[var_idx].last_field = i + 1;
9347
9348 /* The default field does not need a range, but other fields do.
9349 We skipped the discriminant above. */
9350 if (i != default_index)
9351 {
9352 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9353 ++range_idx;
9354 }
9355
9356 ++var_idx;
9357 }
9358
9359 gdb_assert (range_idx == ranges.size ());
9360 gdb_assert (var_idx == n_variants);
9361
9362 variant_part *part = new (obstack) variant_part;
9363 part->discriminant_index = discriminant_index;
9364 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9365 discriminant_index));
9366 part->variants = gdb::array_view<variant> (variants, n_variants);
9367
9368 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9369 gdb::array_view<variant_part> *prop_value
9370 = new (storage) gdb::array_view<variant_part> (part, 1);
9371
9372 struct dynamic_prop prop;
9373 prop.kind = PROP_VARIANT_PARTS;
9374 prop.data.variant_parts = prop_value;
9375
9376 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9377}
9378
9379/* Some versions of rustc emitted enums in an unusual way.
9380
9381 Ordinary enums were emitted as unions. The first element of each
9382 structure in the union was named "RUST$ENUM$DISR". This element
9383 held the discriminant.
9384
9385 These versions of Rust also implemented the "non-zero"
9386 optimization. When the enum had two values, and one is empty and
9387 the other holds a pointer that cannot be zero, the pointer is used
9388 as the discriminant, with a zero value meaning the empty variant.
9389 Here, the union's first member is of the form
9390 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9391 where the fieldnos are the indices of the fields that should be
9392 traversed in order to find the field (which may be several fields deep)
9393 and the variantname is the name of the variant of the case when the
9394 field is zero.
9395
9396 This function recognizes whether TYPE is of one of these forms,
9397 and, if so, smashes it to be a variant type. */
9398
9399static void
9400quirk_rust_enum (struct type *type, struct objfile *objfile)
9401{
9402 gdb_assert (type->code () == TYPE_CODE_UNION);
9403
9404 /* We don't need to deal with empty enums. */
9405 if (type->num_fields () == 0)
9406 return;
9407
9408#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9409 if (type->num_fields () == 1
9410 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9411 {
9412 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9413
9414 /* Decode the field name to find the offset of the
9415 discriminant. */
9416 ULONGEST bit_offset = 0;
9417 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9418 while (name[0] >= '0' && name[0] <= '9')
9419 {
9420 char *tail;
9421 unsigned long index = strtoul (name, &tail, 10);
9422 name = tail;
9423 if (*name != '$'
9424 || index >= field_type->num_fields ()
9425 || (TYPE_FIELD_LOC_KIND (field_type, index)
9426 != FIELD_LOC_KIND_BITPOS))
9427 {
9428 complaint (_("Could not parse Rust enum encoding string \"%s\""
9429 "[in module %s]"),
9430 TYPE_FIELD_NAME (type, 0),
9431 objfile_name (objfile));
9432 return;
9433 }
9434 ++name;
9435
9436 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9437 field_type = TYPE_FIELD_TYPE (field_type, index);
9438 }
9439
9440 /* Smash this type to be a structure type. We have to do this
9441 because the type has already been recorded. */
9442 type->set_code (TYPE_CODE_STRUCT);
9443 type->set_num_fields (3);
9444 /* Save the field we care about. */
9445 struct field saved_field = type->field (0);
9446 type->set_fields
9447 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9448
9449 /* Put the discriminant at index 0. */
9450 TYPE_FIELD_TYPE (type, 0) = field_type;
9451 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9452 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9453 SET_FIELD_BITPOS (type->field (0), bit_offset);
9454
9455 /* The order of fields doesn't really matter, so put the real
9456 field at index 1 and the data-less field at index 2. */
9457 type->field (1) = saved_field;
9458 TYPE_FIELD_NAME (type, 1)
9459 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9460 TYPE_FIELD_TYPE (type, 1)->set_name
9461 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9462 TYPE_FIELD_NAME (type, 1)));
9463
9464 const char *dataless_name
9465 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9466 name);
9467 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9468 dataless_name);
9469 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9470 /* NAME points into the original discriminant name, which
9471 already has the correct lifetime. */
9472 TYPE_FIELD_NAME (type, 2) = name;
9473 SET_FIELD_BITPOS (type->field (2), 0);
9474
9475 /* Indicate that this is a variant type. */
9476 static discriminant_range ranges[1] = { { 0, 0 } };
9477 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9478 }
9479 /* A union with a single anonymous field is probably an old-style
9480 univariant enum. */
9481 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9482 {
9483 /* Smash this type to be a structure type. We have to do this
9484 because the type has already been recorded. */
9485 type->set_code (TYPE_CODE_STRUCT);
9486
9487 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9488 const char *variant_name
9489 = rust_last_path_segment (field_type->name ());
9490 TYPE_FIELD_NAME (type, 0) = variant_name;
9491 field_type->set_name
9492 (rust_fully_qualify (&objfile->objfile_obstack,
9493 type->name (), variant_name));
9494 }
9495 else
9496 {
9497 struct type *disr_type = nullptr;
9498 for (int i = 0; i < type->num_fields (); ++i)
9499 {
9500 disr_type = TYPE_FIELD_TYPE (type, i);
9501
9502 if (disr_type->code () != TYPE_CODE_STRUCT)
9503 {
9504 /* All fields of a true enum will be structs. */
9505 return;
9506 }
9507 else if (disr_type->num_fields () == 0)
9508 {
9509 /* Could be data-less variant, so keep going. */
9510 disr_type = nullptr;
9511 }
9512 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9513 "RUST$ENUM$DISR") != 0)
9514 {
9515 /* Not a Rust enum. */
9516 return;
9517 }
9518 else
9519 {
9520 /* Found one. */
9521 break;
9522 }
9523 }
9524
9525 /* If we got here without a discriminant, then it's probably
9526 just a union. */
9527 if (disr_type == nullptr)
9528 return;
9529
9530 /* Smash this type to be a structure type. We have to do this
9531 because the type has already been recorded. */
9532 type->set_code (TYPE_CODE_STRUCT);
9533
9534 /* Make space for the discriminant field. */
9535 struct field *disr_field = &disr_type->field (0);
9536 field *new_fields
9537 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9538 * sizeof (struct field)));
9539 memcpy (new_fields + 1, type->fields (),
9540 type->num_fields () * sizeof (struct field));
9541 type->set_fields (new_fields);
9542 type->set_num_fields (type->num_fields () + 1);
9543
9544 /* Install the discriminant at index 0 in the union. */
9545 type->field (0) = *disr_field;
9546 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9547 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9548
9549 /* We need a way to find the correct discriminant given a
9550 variant name. For convenience we build a map here. */
9551 struct type *enum_type = FIELD_TYPE (*disr_field);
9552 std::unordered_map<std::string, ULONGEST> discriminant_map;
9553 for (int i = 0; i < enum_type->num_fields (); ++i)
9554 {
9555 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9556 {
9557 const char *name
9558 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9559 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9560 }
9561 }
9562
9563 int n_fields = type->num_fields ();
9564 /* We don't need a range entry for the discriminant, but we do
9565 need one for every other field, as there is no default
9566 variant. */
9567 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9568 discriminant_range,
9569 n_fields - 1);
9570 /* Skip the discriminant here. */
9571 for (int i = 1; i < n_fields; ++i)
9572 {
9573 /* Find the final word in the name of this variant's type.
9574 That name can be used to look up the correct
9575 discriminant. */
9576 const char *variant_name
9577 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9578
9579 auto iter = discriminant_map.find (variant_name);
9580 if (iter != discriminant_map.end ())
9581 {
9582 ranges[i].low = iter->second;
9583 ranges[i].high = iter->second;
9584 }
9585
9586 /* Remove the discriminant field, if it exists. */
9587 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9588 if (sub_type->num_fields () > 0)
9589 {
9590 sub_type->set_num_fields (sub_type->num_fields () - 1);
9591 sub_type->set_fields (sub_type->fields () + 1);
9592 }
9593 TYPE_FIELD_NAME (type, i) = variant_name;
9594 sub_type->set_name
9595 (rust_fully_qualify (&objfile->objfile_obstack,
9596 type->name (), variant_name));
9597 }
9598
9599 /* Indicate that this is a variant type. */
9600 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9601 gdb::array_view<discriminant_range> (ranges,
9602 n_fields - 1));
9603 }
9604}
9605
9606/* Rewrite some Rust unions to be structures with variants parts. */
9607
9608static void
9609rust_union_quirks (struct dwarf2_cu *cu)
9610{
9611 gdb_assert (cu->language == language_rust);
9612 for (type *type_ : cu->rust_unions)
9613 quirk_rust_enum (type_, cu->per_objfile->objfile);
9614 /* We don't need this any more. */
9615 cu->rust_unions.clear ();
9616}
9617
9618/* See read.h. */
9619
9620type_unit_group_unshareable *
9621dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9622{
9623 auto iter = this->m_type_units.find (tu_group);
9624 if (iter != this->m_type_units.end ())
9625 return iter->second.get ();
9626
9627 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9628 type_unit_group_unshareable *result = uniq.get ();
9629 this->m_type_units[tu_group] = std::move (uniq);
9630 return result;
9631}
9632
9633struct type *
9634dwarf2_per_objfile::get_type_for_signatured_type
9635 (signatured_type *sig_type) const
9636{
9637 auto iter = this->m_type_map.find (sig_type);
9638 if (iter == this->m_type_map.end ())
9639 return nullptr;
9640
9641 return iter->second;
9642}
9643
9644void dwarf2_per_objfile::set_type_for_signatured_type
9645 (signatured_type *sig_type, struct type *type)
9646{
9647 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9648
9649 this->m_type_map[sig_type] = type;
9650}
9651
9652/* A helper function for computing the list of all symbol tables
9653 included by PER_CU. */
9654
9655static void
9656recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9657 htab_t all_children, htab_t all_type_symtabs,
9658 dwarf2_per_cu_data *per_cu,
9659 dwarf2_per_objfile *per_objfile,
9660 struct compunit_symtab *immediate_parent)
9661{
9662 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9663 if (*slot != NULL)
9664 {
9665 /* This inclusion and its children have been processed. */
9666 return;
9667 }
9668
9669 *slot = per_cu;
9670
9671 /* Only add a CU if it has a symbol table. */
9672 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9673 if (cust != NULL)
9674 {
9675 /* If this is a type unit only add its symbol table if we haven't
9676 seen it yet (type unit per_cu's can share symtabs). */
9677 if (per_cu->is_debug_types)
9678 {
9679 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9680 if (*slot == NULL)
9681 {
9682 *slot = cust;
9683 result->push_back (cust);
9684 if (cust->user == NULL)
9685 cust->user = immediate_parent;
9686 }
9687 }
9688 else
9689 {
9690 result->push_back (cust);
9691 if (cust->user == NULL)
9692 cust->user = immediate_parent;
9693 }
9694 }
9695
9696 if (!per_cu->imported_symtabs_empty ())
9697 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9698 {
9699 recursively_compute_inclusions (result, all_children,
9700 all_type_symtabs, ptr, per_objfile,
9701 cust);
9702 }
9703}
9704
9705/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9706 PER_CU. */
9707
9708static void
9709compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9710 dwarf2_per_objfile *per_objfile)
9711{
9712 gdb_assert (! per_cu->is_debug_types);
9713
9714 if (!per_cu->imported_symtabs_empty ())
9715 {
9716 int len;
9717 std::vector<compunit_symtab *> result_symtabs;
9718 htab_t all_children, all_type_symtabs;
9719 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9720
9721 /* If we don't have a symtab, we can just skip this case. */
9722 if (cust == NULL)
9723 return;
9724
9725 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9726 NULL, xcalloc, xfree);
9727 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9728 NULL, xcalloc, xfree);
9729
9730 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9731 {
9732 recursively_compute_inclusions (&result_symtabs, all_children,
9733 all_type_symtabs, ptr, per_objfile,
9734 cust);
9735 }
9736
9737 /* Now we have a transitive closure of all the included symtabs. */
9738 len = result_symtabs.size ();
9739 cust->includes
9740 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9741 struct compunit_symtab *, len + 1);
9742 memcpy (cust->includes, result_symtabs.data (),
9743 len * sizeof (compunit_symtab *));
9744 cust->includes[len] = NULL;
9745
9746 htab_delete (all_children);
9747 htab_delete (all_type_symtabs);
9748 }
9749}
9750
9751/* Compute the 'includes' field for the symtabs of all the CUs we just
9752 read. */
9753
9754static void
9755process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9756{
9757 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9758 {
9759 if (! iter->is_debug_types)
9760 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9761 }
9762
9763 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9764}
9765
9766/* Generate full symbol information for PER_CU, whose DIEs have
9767 already been loaded into memory. */
9768
9769static void
9770process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9771 dwarf2_per_objfile *dwarf2_per_objfile,
9772 enum language pretend_language)
9773{
9774 struct dwarf2_cu *cu = per_cu->cu;
9775 struct objfile *objfile = dwarf2_per_objfile->objfile;
9776 struct gdbarch *gdbarch = objfile->arch ();
9777 CORE_ADDR lowpc, highpc;
9778 struct compunit_symtab *cust;
9779 CORE_ADDR baseaddr;
9780 struct block *static_block;
9781 CORE_ADDR addr;
9782
9783 baseaddr = objfile->text_section_offset ();
9784
9785 /* Clear the list here in case something was left over. */
9786 cu->method_list.clear ();
9787
9788 cu->language = pretend_language;
9789 cu->language_defn = language_def (cu->language);
9790
9791 /* Do line number decoding in read_file_scope () */
9792 process_die (cu->dies, cu);
9793
9794 /* For now fudge the Go package. */
9795 if (cu->language == language_go)
9796 fixup_go_packaging (cu);
9797
9798 /* Now that we have processed all the DIEs in the CU, all the types
9799 should be complete, and it should now be safe to compute all of the
9800 physnames. */
9801 compute_delayed_physnames (cu);
9802
9803 if (cu->language == language_rust)
9804 rust_union_quirks (cu);
9805
9806 /* Some compilers don't define a DW_AT_high_pc attribute for the
9807 compilation unit. If the DW_AT_high_pc is missing, synthesize
9808 it, by scanning the DIE's below the compilation unit. */
9809 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9810
9811 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9812 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9813
9814 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9815 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9816 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9817 addrmap to help ensure it has an accurate map of pc values belonging to
9818 this comp unit. */
9819 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9820
9821 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9822 SECT_OFF_TEXT (objfile),
9823 0);
9824
9825 if (cust != NULL)
9826 {
9827 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9828
9829 /* Set symtab language to language from DW_AT_language. If the
9830 compilation is from a C file generated by language preprocessors, do
9831 not set the language if it was already deduced by start_subfile. */
9832 if (!(cu->language == language_c
9833 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9834 COMPUNIT_FILETABS (cust)->language = cu->language;
9835
9836 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9837 produce DW_AT_location with location lists but it can be possibly
9838 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9839 there were bugs in prologue debug info, fixed later in GCC-4.5
9840 by "unwind info for epilogues" patch (which is not directly related).
9841
9842 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9843 needed, it would be wrong due to missing DW_AT_producer there.
9844
9845 Still one can confuse GDB by using non-standard GCC compilation
9846 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9847 */
9848 if (cu->has_loclist && gcc_4_minor >= 5)
9849 cust->locations_valid = 1;
9850
9851 if (gcc_4_minor >= 5)
9852 cust->epilogue_unwind_valid = 1;
9853
9854 cust->call_site_htab = cu->call_site_htab;
9855 }
9856
9857 dwarf2_per_objfile->set_symtab (per_cu, cust);
9858
9859 /* Push it for inclusion processing later. */
9860 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9861
9862 /* Not needed any more. */
9863 cu->reset_builder ();
9864}
9865
9866/* Generate full symbol information for type unit PER_CU, whose DIEs have
9867 already been loaded into memory. */
9868
9869static void
9870process_full_type_unit (dwarf2_per_cu_data *per_cu,
9871 dwarf2_per_objfile *dwarf2_per_objfile,
9872 enum language pretend_language)
9873{
9874 struct dwarf2_cu *cu = per_cu->cu;
9875 struct objfile *objfile = dwarf2_per_objfile->objfile;
9876 struct compunit_symtab *cust;
9877 struct signatured_type *sig_type;
9878
9879 gdb_assert (per_cu->is_debug_types);
9880 sig_type = (struct signatured_type *) per_cu;
9881
9882 /* Clear the list here in case something was left over. */
9883 cu->method_list.clear ();
9884
9885 cu->language = pretend_language;
9886 cu->language_defn = language_def (cu->language);
9887
9888 /* The symbol tables are set up in read_type_unit_scope. */
9889 process_die (cu->dies, cu);
9890
9891 /* For now fudge the Go package. */
9892 if (cu->language == language_go)
9893 fixup_go_packaging (cu);
9894
9895 /* Now that we have processed all the DIEs in the CU, all the types
9896 should be complete, and it should now be safe to compute all of the
9897 physnames. */
9898 compute_delayed_physnames (cu);
9899
9900 if (cu->language == language_rust)
9901 rust_union_quirks (cu);
9902
9903 /* TUs share symbol tables.
9904 If this is the first TU to use this symtab, complete the construction
9905 of it with end_expandable_symtab. Otherwise, complete the addition of
9906 this TU's symbols to the existing symtab. */
9907 type_unit_group_unshareable *tug_unshare =
9908 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9909 if (tug_unshare->compunit_symtab == NULL)
9910 {
9911 buildsym_compunit *builder = cu->get_builder ();
9912 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9913 tug_unshare->compunit_symtab = cust;
9914
9915 if (cust != NULL)
9916 {
9917 /* Set symtab language to language from DW_AT_language. If the
9918 compilation is from a C file generated by language preprocessors,
9919 do not set the language if it was already deduced by
9920 start_subfile. */
9921 if (!(cu->language == language_c
9922 && COMPUNIT_FILETABS (cust)->language != language_c))
9923 COMPUNIT_FILETABS (cust)->language = cu->language;
9924 }
9925 }
9926 else
9927 {
9928 cu->get_builder ()->augment_type_symtab ();
9929 cust = tug_unshare->compunit_symtab;
9930 }
9931
9932 dwarf2_per_objfile->set_symtab (per_cu, cust);
9933
9934 /* Not needed any more. */
9935 cu->reset_builder ();
9936}
9937
9938/* Process an imported unit DIE. */
9939
9940static void
9941process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9942{
9943 struct attribute *attr;
9944
9945 /* For now we don't handle imported units in type units. */
9946 if (cu->per_cu->is_debug_types)
9947 {
9948 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9949 " supported in type units [in module %s]"),
9950 objfile_name (cu->per_objfile->objfile));
9951 }
9952
9953 attr = dwarf2_attr (die, DW_AT_import, cu);
9954 if (attr != NULL)
9955 {
9956 sect_offset sect_off = attr->get_ref_die_offset ();
9957 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9958 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9959 dwarf2_per_cu_data *per_cu
9960 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9961
9962 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9963 into another compilation unit, at root level. Regard this as a hint,
9964 and ignore it. */
9965 if (die->parent && die->parent->parent == NULL
9966 && per_cu->unit_type == DW_UT_compile
9967 && per_cu->lang == language_cplus)
9968 return;
9969
9970 /* If necessary, add it to the queue and load its DIEs. */
9971 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9972 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9973
9974 cu->per_cu->imported_symtabs_push (per_cu);
9975 }
9976}
9977
9978/* RAII object that represents a process_die scope: i.e.,
9979 starts/finishes processing a DIE. */
9980class process_die_scope
9981{
9982public:
9983 process_die_scope (die_info *die, dwarf2_cu *cu)
9984 : m_die (die), m_cu (cu)
9985 {
9986 /* We should only be processing DIEs not already in process. */
9987 gdb_assert (!m_die->in_process);
9988 m_die->in_process = true;
9989 }
9990
9991 ~process_die_scope ()
9992 {
9993 m_die->in_process = false;
9994
9995 /* If we're done processing the DIE for the CU that owns the line
9996 header, we don't need the line header anymore. */
9997 if (m_cu->line_header_die_owner == m_die)
9998 {
9999 delete m_cu->line_header;
10000 m_cu->line_header = NULL;
10001 m_cu->line_header_die_owner = NULL;
10002 }
10003 }
10004
10005private:
10006 die_info *m_die;
10007 dwarf2_cu *m_cu;
10008};
10009
10010/* Process a die and its children. */
10011
10012static void
10013process_die (struct die_info *die, struct dwarf2_cu *cu)
10014{
10015 process_die_scope scope (die, cu);
10016
10017 switch (die->tag)
10018 {
10019 case DW_TAG_padding:
10020 break;
10021 case DW_TAG_compile_unit:
10022 case DW_TAG_partial_unit:
10023 read_file_scope (die, cu);
10024 break;
10025 case DW_TAG_type_unit:
10026 read_type_unit_scope (die, cu);
10027 break;
10028 case DW_TAG_subprogram:
10029 /* Nested subprograms in Fortran get a prefix. */
10030 if (cu->language == language_fortran
10031 && die->parent != NULL
10032 && die->parent->tag == DW_TAG_subprogram)
10033 cu->processing_has_namespace_info = true;
10034 /* Fall through. */
10035 case DW_TAG_inlined_subroutine:
10036 read_func_scope (die, cu);
10037 break;
10038 case DW_TAG_lexical_block:
10039 case DW_TAG_try_block:
10040 case DW_TAG_catch_block:
10041 read_lexical_block_scope (die, cu);
10042 break;
10043 case DW_TAG_call_site:
10044 case DW_TAG_GNU_call_site:
10045 read_call_site_scope (die, cu);
10046 break;
10047 case DW_TAG_class_type:
10048 case DW_TAG_interface_type:
10049 case DW_TAG_structure_type:
10050 case DW_TAG_union_type:
10051 process_structure_scope (die, cu);
10052 break;
10053 case DW_TAG_enumeration_type:
10054 process_enumeration_scope (die, cu);
10055 break;
10056
10057 /* These dies have a type, but processing them does not create
10058 a symbol or recurse to process the children. Therefore we can
10059 read them on-demand through read_type_die. */
10060 case DW_TAG_subroutine_type:
10061 case DW_TAG_set_type:
10062 case DW_TAG_array_type:
10063 case DW_TAG_pointer_type:
10064 case DW_TAG_ptr_to_member_type:
10065 case DW_TAG_reference_type:
10066 case DW_TAG_rvalue_reference_type:
10067 case DW_TAG_string_type:
10068 break;
10069
10070 case DW_TAG_base_type:
10071 case DW_TAG_subrange_type:
10072 case DW_TAG_typedef:
10073 /* Add a typedef symbol for the type definition, if it has a
10074 DW_AT_name. */
10075 new_symbol (die, read_type_die (die, cu), cu);
10076 break;
10077 case DW_TAG_common_block:
10078 read_common_block (die, cu);
10079 break;
10080 case DW_TAG_common_inclusion:
10081 break;
10082 case DW_TAG_namespace:
10083 cu->processing_has_namespace_info = true;
10084 read_namespace (die, cu);
10085 break;
10086 case DW_TAG_module:
10087 cu->processing_has_namespace_info = true;
10088 read_module (die, cu);
10089 break;
10090 case DW_TAG_imported_declaration:
10091 cu->processing_has_namespace_info = true;
10092 if (read_namespace_alias (die, cu))
10093 break;
10094 /* The declaration is not a global namespace alias. */
10095 /* Fall through. */
10096 case DW_TAG_imported_module:
10097 cu->processing_has_namespace_info = true;
10098 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10099 || cu->language != language_fortran))
10100 complaint (_("Tag '%s' has unexpected children"),
10101 dwarf_tag_name (die->tag));
10102 read_import_statement (die, cu);
10103 break;
10104
10105 case DW_TAG_imported_unit:
10106 process_imported_unit_die (die, cu);
10107 break;
10108
10109 case DW_TAG_variable:
10110 read_variable (die, cu);
10111 break;
10112
10113 default:
10114 new_symbol (die, NULL, cu);
10115 break;
10116 }
10117}
10118\f
10119/* DWARF name computation. */
10120
10121/* A helper function for dwarf2_compute_name which determines whether DIE
10122 needs to have the name of the scope prepended to the name listed in the
10123 die. */
10124
10125static int
10126die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10127{
10128 struct attribute *attr;
10129
10130 switch (die->tag)
10131 {
10132 case DW_TAG_namespace:
10133 case DW_TAG_typedef:
10134 case DW_TAG_class_type:
10135 case DW_TAG_interface_type:
10136 case DW_TAG_structure_type:
10137 case DW_TAG_union_type:
10138 case DW_TAG_enumeration_type:
10139 case DW_TAG_enumerator:
10140 case DW_TAG_subprogram:
10141 case DW_TAG_inlined_subroutine:
10142 case DW_TAG_member:
10143 case DW_TAG_imported_declaration:
10144 return 1;
10145
10146 case DW_TAG_variable:
10147 case DW_TAG_constant:
10148 /* We only need to prefix "globally" visible variables. These include
10149 any variable marked with DW_AT_external or any variable that
10150 lives in a namespace. [Variables in anonymous namespaces
10151 require prefixing, but they are not DW_AT_external.] */
10152
10153 if (dwarf2_attr (die, DW_AT_specification, cu))
10154 {
10155 struct dwarf2_cu *spec_cu = cu;
10156
10157 return die_needs_namespace (die_specification (die, &spec_cu),
10158 spec_cu);
10159 }
10160
10161 attr = dwarf2_attr (die, DW_AT_external, cu);
10162 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10163 && die->parent->tag != DW_TAG_module)
10164 return 0;
10165 /* A variable in a lexical block of some kind does not need a
10166 namespace, even though in C++ such variables may be external
10167 and have a mangled name. */
10168 if (die->parent->tag == DW_TAG_lexical_block
10169 || die->parent->tag == DW_TAG_try_block
10170 || die->parent->tag == DW_TAG_catch_block
10171 || die->parent->tag == DW_TAG_subprogram)
10172 return 0;
10173 return 1;
10174
10175 default:
10176 return 0;
10177 }
10178}
10179
10180/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10181 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10182 defined for the given DIE. */
10183
10184static struct attribute *
10185dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10186{
10187 struct attribute *attr;
10188
10189 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10190 if (attr == NULL)
10191 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10192
10193 return attr;
10194}
10195
10196/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10197 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10198 defined for the given DIE. */
10199
10200static const char *
10201dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10202{
10203 const char *linkage_name;
10204
10205 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10206 if (linkage_name == NULL)
10207 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10208
10209 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10210 See https://github.com/rust-lang/rust/issues/32925. */
10211 if (cu->language == language_rust && linkage_name != NULL
10212 && strchr (linkage_name, '{') != NULL)
10213 linkage_name = NULL;
10214
10215 return linkage_name;
10216}
10217
10218/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10219 compute the physname for the object, which include a method's:
10220 - formal parameters (C++),
10221 - receiver type (Go),
10222
10223 The term "physname" is a bit confusing.
10224 For C++, for example, it is the demangled name.
10225 For Go, for example, it's the mangled name.
10226
10227 For Ada, return the DIE's linkage name rather than the fully qualified
10228 name. PHYSNAME is ignored..
10229
10230 The result is allocated on the objfile->per_bfd's obstack and
10231 canonicalized. */
10232
10233static const char *
10234dwarf2_compute_name (const char *name,
10235 struct die_info *die, struct dwarf2_cu *cu,
10236 int physname)
10237{
10238 struct objfile *objfile = cu->per_objfile->objfile;
10239
10240 if (name == NULL)
10241 name = dwarf2_name (die, cu);
10242
10243 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10244 but otherwise compute it by typename_concat inside GDB.
10245 FIXME: Actually this is not really true, or at least not always true.
10246 It's all very confusing. compute_and_set_names doesn't try to demangle
10247 Fortran names because there is no mangling standard. So new_symbol
10248 will set the demangled name to the result of dwarf2_full_name, and it is
10249 the demangled name that GDB uses if it exists. */
10250 if (cu->language == language_ada
10251 || (cu->language == language_fortran && physname))
10252 {
10253 /* For Ada unit, we prefer the linkage name over the name, as
10254 the former contains the exported name, which the user expects
10255 to be able to reference. Ideally, we want the user to be able
10256 to reference this entity using either natural or linkage name,
10257 but we haven't started looking at this enhancement yet. */
10258 const char *linkage_name = dw2_linkage_name (die, cu);
10259
10260 if (linkage_name != NULL)
10261 return linkage_name;
10262 }
10263
10264 /* These are the only languages we know how to qualify names in. */
10265 if (name != NULL
10266 && (cu->language == language_cplus
10267 || cu->language == language_fortran || cu->language == language_d
10268 || cu->language == language_rust))
10269 {
10270 if (die_needs_namespace (die, cu))
10271 {
10272 const char *prefix;
10273 const char *canonical_name = NULL;
10274
10275 string_file buf;
10276
10277 prefix = determine_prefix (die, cu);
10278 if (*prefix != '\0')
10279 {
10280 gdb::unique_xmalloc_ptr<char> prefixed_name
10281 (typename_concat (NULL, prefix, name, physname, cu));
10282
10283 buf.puts (prefixed_name.get ());
10284 }
10285 else
10286 buf.puts (name);
10287
10288 /* Template parameters may be specified in the DIE's DW_AT_name, or
10289 as children with DW_TAG_template_type_param or
10290 DW_TAG_value_type_param. If the latter, add them to the name
10291 here. If the name already has template parameters, then
10292 skip this step; some versions of GCC emit both, and
10293 it is more efficient to use the pre-computed name.
10294
10295 Something to keep in mind about this process: it is very
10296 unlikely, or in some cases downright impossible, to produce
10297 something that will match the mangled name of a function.
10298 If the definition of the function has the same debug info,
10299 we should be able to match up with it anyway. But fallbacks
10300 using the minimal symbol, for instance to find a method
10301 implemented in a stripped copy of libstdc++, will not work.
10302 If we do not have debug info for the definition, we will have to
10303 match them up some other way.
10304
10305 When we do name matching there is a related problem with function
10306 templates; two instantiated function templates are allowed to
10307 differ only by their return types, which we do not add here. */
10308
10309 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10310 {
10311 struct attribute *attr;
10312 struct die_info *child;
10313 int first = 1;
10314
10315 die->building_fullname = 1;
10316
10317 for (child = die->child; child != NULL; child = child->sibling)
10318 {
10319 struct type *type;
10320 LONGEST value;
10321 const gdb_byte *bytes;
10322 struct dwarf2_locexpr_baton *baton;
10323 struct value *v;
10324
10325 if (child->tag != DW_TAG_template_type_param
10326 && child->tag != DW_TAG_template_value_param)
10327 continue;
10328
10329 if (first)
10330 {
10331 buf.puts ("<");
10332 first = 0;
10333 }
10334 else
10335 buf.puts (", ");
10336
10337 attr = dwarf2_attr (child, DW_AT_type, cu);
10338 if (attr == NULL)
10339 {
10340 complaint (_("template parameter missing DW_AT_type"));
10341 buf.puts ("UNKNOWN_TYPE");
10342 continue;
10343 }
10344 type = die_type (child, cu);
10345
10346 if (child->tag == DW_TAG_template_type_param)
10347 {
10348 c_print_type (type, "", &buf, -1, 0, cu->language,
10349 &type_print_raw_options);
10350 continue;
10351 }
10352
10353 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10354 if (attr == NULL)
10355 {
10356 complaint (_("template parameter missing "
10357 "DW_AT_const_value"));
10358 buf.puts ("UNKNOWN_VALUE");
10359 continue;
10360 }
10361
10362 dwarf2_const_value_attr (attr, type, name,
10363 &cu->comp_unit_obstack, cu,
10364 &value, &bytes, &baton);
10365
10366 if (TYPE_NOSIGN (type))
10367 /* GDB prints characters as NUMBER 'CHAR'. If that's
10368 changed, this can use value_print instead. */
10369 c_printchar (value, type, &buf);
10370 else
10371 {
10372 struct value_print_options opts;
10373
10374 if (baton != NULL)
10375 v = dwarf2_evaluate_loc_desc (type, NULL,
10376 baton->data,
10377 baton->size,
10378 baton->per_cu,
10379 baton->per_objfile);
10380 else if (bytes != NULL)
10381 {
10382 v = allocate_value (type);
10383 memcpy (value_contents_writeable (v), bytes,
10384 TYPE_LENGTH (type));
10385 }
10386 else
10387 v = value_from_longest (type, value);
10388
10389 /* Specify decimal so that we do not depend on
10390 the radix. */
10391 get_formatted_print_options (&opts, 'd');
10392 opts.raw = 1;
10393 value_print (v, &buf, &opts);
10394 release_value (v);
10395 }
10396 }
10397
10398 die->building_fullname = 0;
10399
10400 if (!first)
10401 {
10402 /* Close the argument list, with a space if necessary
10403 (nested templates). */
10404 if (!buf.empty () && buf.string ().back () == '>')
10405 buf.puts (" >");
10406 else
10407 buf.puts (">");
10408 }
10409 }
10410
10411 /* For C++ methods, append formal parameter type
10412 information, if PHYSNAME. */
10413
10414 if (physname && die->tag == DW_TAG_subprogram
10415 && cu->language == language_cplus)
10416 {
10417 struct type *type = read_type_die (die, cu);
10418
10419 c_type_print_args (type, &buf, 1, cu->language,
10420 &type_print_raw_options);
10421
10422 if (cu->language == language_cplus)
10423 {
10424 /* Assume that an artificial first parameter is
10425 "this", but do not crash if it is not. RealView
10426 marks unnamed (and thus unused) parameters as
10427 artificial; there is no way to differentiate
10428 the two cases. */
10429 if (type->num_fields () > 0
10430 && TYPE_FIELD_ARTIFICIAL (type, 0)
10431 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10432 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10433 0))))
10434 buf.puts (" const");
10435 }
10436 }
10437
10438 const std::string &intermediate_name = buf.string ();
10439
10440 if (cu->language == language_cplus)
10441 canonical_name
10442 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10443 objfile);
10444
10445 /* If we only computed INTERMEDIATE_NAME, or if
10446 INTERMEDIATE_NAME is already canonical, then we need to
10447 intern it. */
10448 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10449 name = objfile->intern (intermediate_name);
10450 else
10451 name = canonical_name;
10452 }
10453 }
10454
10455 return name;
10456}
10457
10458/* Return the fully qualified name of DIE, based on its DW_AT_name.
10459 If scope qualifiers are appropriate they will be added. The result
10460 will be allocated on the storage_obstack, or NULL if the DIE does
10461 not have a name. NAME may either be from a previous call to
10462 dwarf2_name or NULL.
10463
10464 The output string will be canonicalized (if C++). */
10465
10466static const char *
10467dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10468{
10469 return dwarf2_compute_name (name, die, cu, 0);
10470}
10471
10472/* Construct a physname for the given DIE in CU. NAME may either be
10473 from a previous call to dwarf2_name or NULL. The result will be
10474 allocated on the objfile_objstack or NULL if the DIE does not have a
10475 name.
10476
10477 The output string will be canonicalized (if C++). */
10478
10479static const char *
10480dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10481{
10482 struct objfile *objfile = cu->per_objfile->objfile;
10483 const char *retval, *mangled = NULL, *canon = NULL;
10484 int need_copy = 1;
10485
10486 /* In this case dwarf2_compute_name is just a shortcut not building anything
10487 on its own. */
10488 if (!die_needs_namespace (die, cu))
10489 return dwarf2_compute_name (name, die, cu, 1);
10490
10491 if (cu->language != language_rust)
10492 mangled = dw2_linkage_name (die, cu);
10493
10494 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10495 has computed. */
10496 gdb::unique_xmalloc_ptr<char> demangled;
10497 if (mangled != NULL)
10498 {
10499
10500 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10501 {
10502 /* Do nothing (do not demangle the symbol name). */
10503 }
10504 else if (cu->language == language_go)
10505 {
10506 /* This is a lie, but we already lie to the caller new_symbol.
10507 new_symbol assumes we return the mangled name.
10508 This just undoes that lie until things are cleaned up. */
10509 }
10510 else
10511 {
10512 /* Use DMGL_RET_DROP for C++ template functions to suppress
10513 their return type. It is easier for GDB users to search
10514 for such functions as `name(params)' than `long name(params)'.
10515 In such case the minimal symbol names do not match the full
10516 symbol names but for template functions there is never a need
10517 to look up their definition from their declaration so
10518 the only disadvantage remains the minimal symbol variant
10519 `long name(params)' does not have the proper inferior type. */
10520 demangled.reset (gdb_demangle (mangled,
10521 (DMGL_PARAMS | DMGL_ANSI
10522 | DMGL_RET_DROP)));
10523 }
10524 if (demangled)
10525 canon = demangled.get ();
10526 else
10527 {
10528 canon = mangled;
10529 need_copy = 0;
10530 }
10531 }
10532
10533 if (canon == NULL || check_physname)
10534 {
10535 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10536
10537 if (canon != NULL && strcmp (physname, canon) != 0)
10538 {
10539 /* It may not mean a bug in GDB. The compiler could also
10540 compute DW_AT_linkage_name incorrectly. But in such case
10541 GDB would need to be bug-to-bug compatible. */
10542
10543 complaint (_("Computed physname <%s> does not match demangled <%s> "
10544 "(from linkage <%s>) - DIE at %s [in module %s]"),
10545 physname, canon, mangled, sect_offset_str (die->sect_off),
10546 objfile_name (objfile));
10547
10548 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10549 is available here - over computed PHYSNAME. It is safer
10550 against both buggy GDB and buggy compilers. */
10551
10552 retval = canon;
10553 }
10554 else
10555 {
10556 retval = physname;
10557 need_copy = 0;
10558 }
10559 }
10560 else
10561 retval = canon;
10562
10563 if (need_copy)
10564 retval = objfile->intern (retval);
10565
10566 return retval;
10567}
10568
10569/* Inspect DIE in CU for a namespace alias. If one exists, record
10570 a new symbol for it.
10571
10572 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10573
10574static int
10575read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10576{
10577 struct attribute *attr;
10578
10579 /* If the die does not have a name, this is not a namespace
10580 alias. */
10581 attr = dwarf2_attr (die, DW_AT_name, cu);
10582 if (attr != NULL)
10583 {
10584 int num;
10585 struct die_info *d = die;
10586 struct dwarf2_cu *imported_cu = cu;
10587
10588 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10589 keep inspecting DIEs until we hit the underlying import. */
10590#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10591 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10592 {
10593 attr = dwarf2_attr (d, DW_AT_import, cu);
10594 if (attr == NULL)
10595 break;
10596
10597 d = follow_die_ref (d, attr, &imported_cu);
10598 if (d->tag != DW_TAG_imported_declaration)
10599 break;
10600 }
10601
10602 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10603 {
10604 complaint (_("DIE at %s has too many recursively imported "
10605 "declarations"), sect_offset_str (d->sect_off));
10606 return 0;
10607 }
10608
10609 if (attr != NULL)
10610 {
10611 struct type *type;
10612 sect_offset sect_off = attr->get_ref_die_offset ();
10613
10614 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10615 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10616 {
10617 /* This declaration is a global namespace alias. Add
10618 a symbol for it whose type is the aliased namespace. */
10619 new_symbol (die, type, cu);
10620 return 1;
10621 }
10622 }
10623 }
10624
10625 return 0;
10626}
10627
10628/* Return the using directives repository (global or local?) to use in the
10629 current context for CU.
10630
10631 For Ada, imported declarations can materialize renamings, which *may* be
10632 global. However it is impossible (for now?) in DWARF to distinguish
10633 "external" imported declarations and "static" ones. As all imported
10634 declarations seem to be static in all other languages, make them all CU-wide
10635 global only in Ada. */
10636
10637static struct using_direct **
10638using_directives (struct dwarf2_cu *cu)
10639{
10640 if (cu->language == language_ada
10641 && cu->get_builder ()->outermost_context_p ())
10642 return cu->get_builder ()->get_global_using_directives ();
10643 else
10644 return cu->get_builder ()->get_local_using_directives ();
10645}
10646
10647/* Read the import statement specified by the given die and record it. */
10648
10649static void
10650read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10651{
10652 struct objfile *objfile = cu->per_objfile->objfile;
10653 struct attribute *import_attr;
10654 struct die_info *imported_die, *child_die;
10655 struct dwarf2_cu *imported_cu;
10656 const char *imported_name;
10657 const char *imported_name_prefix;
10658 const char *canonical_name;
10659 const char *import_alias;
10660 const char *imported_declaration = NULL;
10661 const char *import_prefix;
10662 std::vector<const char *> excludes;
10663
10664 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10665 if (import_attr == NULL)
10666 {
10667 complaint (_("Tag '%s' has no DW_AT_import"),
10668 dwarf_tag_name (die->tag));
10669 return;
10670 }
10671
10672 imported_cu = cu;
10673 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10674 imported_name = dwarf2_name (imported_die, imported_cu);
10675 if (imported_name == NULL)
10676 {
10677 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10678
10679 The import in the following code:
10680 namespace A
10681 {
10682 typedef int B;
10683 }
10684
10685 int main ()
10686 {
10687 using A::B;
10688 B b;
10689 return b;
10690 }
10691
10692 ...
10693 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10694 <52> DW_AT_decl_file : 1
10695 <53> DW_AT_decl_line : 6
10696 <54> DW_AT_import : <0x75>
10697 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10698 <59> DW_AT_name : B
10699 <5b> DW_AT_decl_file : 1
10700 <5c> DW_AT_decl_line : 2
10701 <5d> DW_AT_type : <0x6e>
10702 ...
10703 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10704 <76> DW_AT_byte_size : 4
10705 <77> DW_AT_encoding : 5 (signed)
10706
10707 imports the wrong die ( 0x75 instead of 0x58 ).
10708 This case will be ignored until the gcc bug is fixed. */
10709 return;
10710 }
10711
10712 /* Figure out the local name after import. */
10713 import_alias = dwarf2_name (die, cu);
10714
10715 /* Figure out where the statement is being imported to. */
10716 import_prefix = determine_prefix (die, cu);
10717
10718 /* Figure out what the scope of the imported die is and prepend it
10719 to the name of the imported die. */
10720 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10721
10722 if (imported_die->tag != DW_TAG_namespace
10723 && imported_die->tag != DW_TAG_module)
10724 {
10725 imported_declaration = imported_name;
10726 canonical_name = imported_name_prefix;
10727 }
10728 else if (strlen (imported_name_prefix) > 0)
10729 canonical_name = obconcat (&objfile->objfile_obstack,
10730 imported_name_prefix,
10731 (cu->language == language_d ? "." : "::"),
10732 imported_name, (char *) NULL);
10733 else
10734 canonical_name = imported_name;
10735
10736 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10737 for (child_die = die->child; child_die && child_die->tag;
10738 child_die = child_die->sibling)
10739 {
10740 /* DWARF-4: A Fortran use statement with a “rename list” may be
10741 represented by an imported module entry with an import attribute
10742 referring to the module and owned entries corresponding to those
10743 entities that are renamed as part of being imported. */
10744
10745 if (child_die->tag != DW_TAG_imported_declaration)
10746 {
10747 complaint (_("child DW_TAG_imported_declaration expected "
10748 "- DIE at %s [in module %s]"),
10749 sect_offset_str (child_die->sect_off),
10750 objfile_name (objfile));
10751 continue;
10752 }
10753
10754 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10755 if (import_attr == NULL)
10756 {
10757 complaint (_("Tag '%s' has no DW_AT_import"),
10758 dwarf_tag_name (child_die->tag));
10759 continue;
10760 }
10761
10762 imported_cu = cu;
10763 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10764 &imported_cu);
10765 imported_name = dwarf2_name (imported_die, imported_cu);
10766 if (imported_name == NULL)
10767 {
10768 complaint (_("child DW_TAG_imported_declaration has unknown "
10769 "imported name - DIE at %s [in module %s]"),
10770 sect_offset_str (child_die->sect_off),
10771 objfile_name (objfile));
10772 continue;
10773 }
10774
10775 excludes.push_back (imported_name);
10776
10777 process_die (child_die, cu);
10778 }
10779
10780 add_using_directive (using_directives (cu),
10781 import_prefix,
10782 canonical_name,
10783 import_alias,
10784 imported_declaration,
10785 excludes,
10786 0,
10787 &objfile->objfile_obstack);
10788}
10789
10790/* ICC<14 does not output the required DW_AT_declaration on incomplete
10791 types, but gives them a size of zero. Starting with version 14,
10792 ICC is compatible with GCC. */
10793
10794static bool
10795producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10796{
10797 if (!cu->checked_producer)
10798 check_producer (cu);
10799
10800 return cu->producer_is_icc_lt_14;
10801}
10802
10803/* ICC generates a DW_AT_type for C void functions. This was observed on
10804 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10805 which says that void functions should not have a DW_AT_type. */
10806
10807static bool
10808producer_is_icc (struct dwarf2_cu *cu)
10809{
10810 if (!cu->checked_producer)
10811 check_producer (cu);
10812
10813 return cu->producer_is_icc;
10814}
10815
10816/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10817 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10818 this, it was first present in GCC release 4.3.0. */
10819
10820static bool
10821producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10822{
10823 if (!cu->checked_producer)
10824 check_producer (cu);
10825
10826 return cu->producer_is_gcc_lt_4_3;
10827}
10828
10829static file_and_directory
10830find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10831{
10832 file_and_directory res;
10833
10834 /* Find the filename. Do not use dwarf2_name here, since the filename
10835 is not a source language identifier. */
10836 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10837 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10838
10839 if (res.comp_dir == NULL
10840 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10841 && IS_ABSOLUTE_PATH (res.name))
10842 {
10843 res.comp_dir_storage = ldirname (res.name);
10844 if (!res.comp_dir_storage.empty ())
10845 res.comp_dir = res.comp_dir_storage.c_str ();
10846 }
10847 if (res.comp_dir != NULL)
10848 {
10849 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10850 directory, get rid of it. */
10851 const char *cp = strchr (res.comp_dir, ':');
10852
10853 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10854 res.comp_dir = cp + 1;
10855 }
10856
10857 if (res.name == NULL)
10858 res.name = "<unknown>";
10859
10860 return res;
10861}
10862
10863/* Handle DW_AT_stmt_list for a compilation unit.
10864 DIE is the DW_TAG_compile_unit die for CU.
10865 COMP_DIR is the compilation directory. LOWPC is passed to
10866 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10867
10868static void
10869handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10870 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10871{
10872 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10873 struct attribute *attr;
10874 struct line_header line_header_local;
10875 hashval_t line_header_local_hash;
10876 void **slot;
10877 int decode_mapping;
10878
10879 gdb_assert (! cu->per_cu->is_debug_types);
10880
10881 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10882 if (attr == NULL)
10883 return;
10884
10885 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10886
10887 /* The line header hash table is only created if needed (it exists to
10888 prevent redundant reading of the line table for partial_units).
10889 If we're given a partial_unit, we'll need it. If we're given a
10890 compile_unit, then use the line header hash table if it's already
10891 created, but don't create one just yet. */
10892
10893 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10894 && die->tag == DW_TAG_partial_unit)
10895 {
10896 dwarf2_per_objfile->per_bfd->line_header_hash
10897 .reset (htab_create_alloc (127, line_header_hash_voidp,
10898 line_header_eq_voidp,
10899 free_line_header_voidp,
10900 xcalloc, xfree));
10901 }
10902
10903 line_header_local.sect_off = line_offset;
10904 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10905 line_header_local_hash = line_header_hash (&line_header_local);
10906 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10907 {
10908 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10909 &line_header_local,
10910 line_header_local_hash, NO_INSERT);
10911
10912 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10913 is not present in *SLOT (since if there is something in *SLOT then
10914 it will be for a partial_unit). */
10915 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10916 {
10917 gdb_assert (*slot != NULL);
10918 cu->line_header = (struct line_header *) *slot;
10919 return;
10920 }
10921 }
10922
10923 /* dwarf_decode_line_header does not yet provide sufficient information.
10924 We always have to call also dwarf_decode_lines for it. */
10925 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10926 if (lh == NULL)
10927 return;
10928
10929 cu->line_header = lh.release ();
10930 cu->line_header_die_owner = die;
10931
10932 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10933 slot = NULL;
10934 else
10935 {
10936 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10937 &line_header_local,
10938 line_header_local_hash, INSERT);
10939 gdb_assert (slot != NULL);
10940 }
10941 if (slot != NULL && *slot == NULL)
10942 {
10943 /* This newly decoded line number information unit will be owned
10944 by line_header_hash hash table. */
10945 *slot = cu->line_header;
10946 cu->line_header_die_owner = NULL;
10947 }
10948 else
10949 {
10950 /* We cannot free any current entry in (*slot) as that struct line_header
10951 may be already used by multiple CUs. Create only temporary decoded
10952 line_header for this CU - it may happen at most once for each line
10953 number information unit. And if we're not using line_header_hash
10954 then this is what we want as well. */
10955 gdb_assert (die->tag != DW_TAG_partial_unit);
10956 }
10957 decode_mapping = (die->tag != DW_TAG_partial_unit);
10958 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10959 decode_mapping);
10960
10961}
10962
10963/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10964
10965static void
10966read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10967{
10968 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10969 struct objfile *objfile = dwarf2_per_objfile->objfile;
10970 struct gdbarch *gdbarch = objfile->arch ();
10971 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10972 CORE_ADDR highpc = ((CORE_ADDR) 0);
10973 struct attribute *attr;
10974 struct die_info *child_die;
10975 CORE_ADDR baseaddr;
10976
10977 prepare_one_comp_unit (cu, die, cu->language);
10978 baseaddr = objfile->text_section_offset ();
10979
10980 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10981
10982 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10983 from finish_block. */
10984 if (lowpc == ((CORE_ADDR) -1))
10985 lowpc = highpc;
10986 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10987
10988 file_and_directory fnd = find_file_and_directory (die, cu);
10989
10990 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10991 standardised yet. As a workaround for the language detection we fall
10992 back to the DW_AT_producer string. */
10993 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10994 cu->language = language_opencl;
10995
10996 /* Similar hack for Go. */
10997 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10998 set_cu_language (DW_LANG_Go, cu);
10999
11000 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11001
11002 /* Decode line number information if present. We do this before
11003 processing child DIEs, so that the line header table is available
11004 for DW_AT_decl_file. */
11005 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11006
11007 /* Process all dies in compilation unit. */
11008 if (die->child != NULL)
11009 {
11010 child_die = die->child;
11011 while (child_die && child_die->tag)
11012 {
11013 process_die (child_die, cu);
11014 child_die = child_die->sibling;
11015 }
11016 }
11017
11018 /* Decode macro information, if present. Dwarf 2 macro information
11019 refers to information in the line number info statement program
11020 header, so we can only read it if we've read the header
11021 successfully. */
11022 attr = dwarf2_attr (die, DW_AT_macros, cu);
11023 if (attr == NULL)
11024 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11025 if (attr && cu->line_header)
11026 {
11027 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11028 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11029
11030 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11031 }
11032 else
11033 {
11034 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11035 if (attr && cu->line_header)
11036 {
11037 unsigned int macro_offset = DW_UNSND (attr);
11038
11039 dwarf_decode_macros (cu, macro_offset, 0);
11040 }
11041 }
11042}
11043
11044void
11045dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11046{
11047 struct type_unit_group *tu_group;
11048 int first_time;
11049 struct attribute *attr;
11050 unsigned int i;
11051 struct signatured_type *sig_type;
11052
11053 gdb_assert (per_cu->is_debug_types);
11054 sig_type = (struct signatured_type *) per_cu;
11055
11056 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11057
11058 /* If we're using .gdb_index (includes -readnow) then
11059 per_cu->type_unit_group may not have been set up yet. */
11060 if (sig_type->type_unit_group == NULL)
11061 sig_type->type_unit_group = get_type_unit_group (this, attr);
11062 tu_group = sig_type->type_unit_group;
11063
11064 /* If we've already processed this stmt_list there's no real need to
11065 do it again, we could fake it and just recreate the part we need
11066 (file name,index -> symtab mapping). If data shows this optimization
11067 is useful we can do it then. */
11068 type_unit_group_unshareable *tug_unshare
11069 = per_objfile->get_type_unit_group_unshareable (tu_group);
11070 first_time = tug_unshare->compunit_symtab == NULL;
11071
11072 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11073 debug info. */
11074 line_header_up lh;
11075 if (attr != NULL)
11076 {
11077 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11078 lh = dwarf_decode_line_header (line_offset, this);
11079 }
11080 if (lh == NULL)
11081 {
11082 if (first_time)
11083 start_symtab ("", NULL, 0);
11084 else
11085 {
11086 gdb_assert (tug_unshare->symtabs == NULL);
11087 gdb_assert (m_builder == nullptr);
11088 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11089 m_builder.reset (new struct buildsym_compunit
11090 (COMPUNIT_OBJFILE (cust), "",
11091 COMPUNIT_DIRNAME (cust),
11092 compunit_language (cust),
11093 0, cust));
11094 list_in_scope = get_builder ()->get_file_symbols ();
11095 }
11096 return;
11097 }
11098
11099 line_header = lh.release ();
11100 line_header_die_owner = die;
11101
11102 if (first_time)
11103 {
11104 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11105
11106 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11107 still initializing it, and our caller (a few levels up)
11108 process_full_type_unit still needs to know if this is the first
11109 time. */
11110
11111 tug_unshare->symtabs
11112 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11113 struct symtab *, line_header->file_names_size ());
11114
11115 auto &file_names = line_header->file_names ();
11116 for (i = 0; i < file_names.size (); ++i)
11117 {
11118 file_entry &fe = file_names[i];
11119 dwarf2_start_subfile (this, fe.name,
11120 fe.include_dir (line_header));
11121 buildsym_compunit *b = get_builder ();
11122 if (b->get_current_subfile ()->symtab == NULL)
11123 {
11124 /* NOTE: start_subfile will recognize when it's been
11125 passed a file it has already seen. So we can't
11126 assume there's a simple mapping from
11127 cu->line_header->file_names to subfiles, plus
11128 cu->line_header->file_names may contain dups. */
11129 b->get_current_subfile ()->symtab
11130 = allocate_symtab (cust, b->get_current_subfile ()->name);
11131 }
11132
11133 fe.symtab = b->get_current_subfile ()->symtab;
11134 tug_unshare->symtabs[i] = fe.symtab;
11135 }
11136 }
11137 else
11138 {
11139 gdb_assert (m_builder == nullptr);
11140 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11141 m_builder.reset (new struct buildsym_compunit
11142 (COMPUNIT_OBJFILE (cust), "",
11143 COMPUNIT_DIRNAME (cust),
11144 compunit_language (cust),
11145 0, cust));
11146 list_in_scope = get_builder ()->get_file_symbols ();
11147
11148 auto &file_names = line_header->file_names ();
11149 for (i = 0; i < file_names.size (); ++i)
11150 {
11151 file_entry &fe = file_names[i];
11152 fe.symtab = tug_unshare->symtabs[i];
11153 }
11154 }
11155
11156 /* The main symtab is allocated last. Type units don't have DW_AT_name
11157 so they don't have a "real" (so to speak) symtab anyway.
11158 There is later code that will assign the main symtab to all symbols
11159 that don't have one. We need to handle the case of a symbol with a
11160 missing symtab (DW_AT_decl_file) anyway. */
11161}
11162
11163/* Process DW_TAG_type_unit.
11164 For TUs we want to skip the first top level sibling if it's not the
11165 actual type being defined by this TU. In this case the first top
11166 level sibling is there to provide context only. */
11167
11168static void
11169read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11170{
11171 struct die_info *child_die;
11172
11173 prepare_one_comp_unit (cu, die, language_minimal);
11174
11175 /* Initialize (or reinitialize) the machinery for building symtabs.
11176 We do this before processing child DIEs, so that the line header table
11177 is available for DW_AT_decl_file. */
11178 cu->setup_type_unit_groups (die);
11179
11180 if (die->child != NULL)
11181 {
11182 child_die = die->child;
11183 while (child_die && child_die->tag)
11184 {
11185 process_die (child_die, cu);
11186 child_die = child_die->sibling;
11187 }
11188 }
11189}
11190\f
11191/* DWO/DWP files.
11192
11193 http://gcc.gnu.org/wiki/DebugFission
11194 http://gcc.gnu.org/wiki/DebugFissionDWP
11195
11196 To simplify handling of both DWO files ("object" files with the DWARF info)
11197 and DWP files (a file with the DWOs packaged up into one file), we treat
11198 DWP files as having a collection of virtual DWO files. */
11199
11200static hashval_t
11201hash_dwo_file (const void *item)
11202{
11203 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11204 hashval_t hash;
11205
11206 hash = htab_hash_string (dwo_file->dwo_name);
11207 if (dwo_file->comp_dir != NULL)
11208 hash += htab_hash_string (dwo_file->comp_dir);
11209 return hash;
11210}
11211
11212static int
11213eq_dwo_file (const void *item_lhs, const void *item_rhs)
11214{
11215 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11216 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11217
11218 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11219 return 0;
11220 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11221 return lhs->comp_dir == rhs->comp_dir;
11222 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11223}
11224
11225/* Allocate a hash table for DWO files. */
11226
11227static htab_up
11228allocate_dwo_file_hash_table ()
11229{
11230 auto delete_dwo_file = [] (void *item)
11231 {
11232 struct dwo_file *dwo_file = (struct dwo_file *) item;
11233
11234 delete dwo_file;
11235 };
11236
11237 return htab_up (htab_create_alloc (41,
11238 hash_dwo_file,
11239 eq_dwo_file,
11240 delete_dwo_file,
11241 xcalloc, xfree));
11242}
11243
11244/* Lookup DWO file DWO_NAME. */
11245
11246static void **
11247lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11248 const char *dwo_name,
11249 const char *comp_dir)
11250{
11251 struct dwo_file find_entry;
11252 void **slot;
11253
11254 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11255 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11256
11257 find_entry.dwo_name = dwo_name;
11258 find_entry.comp_dir = comp_dir;
11259 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11260 INSERT);
11261
11262 return slot;
11263}
11264
11265static hashval_t
11266hash_dwo_unit (const void *item)
11267{
11268 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11269
11270 /* This drops the top 32 bits of the id, but is ok for a hash. */
11271 return dwo_unit->signature;
11272}
11273
11274static int
11275eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11276{
11277 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11278 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11279
11280 /* The signature is assumed to be unique within the DWO file.
11281 So while object file CU dwo_id's always have the value zero,
11282 that's OK, assuming each object file DWO file has only one CU,
11283 and that's the rule for now. */
11284 return lhs->signature == rhs->signature;
11285}
11286
11287/* Allocate a hash table for DWO CUs,TUs.
11288 There is one of these tables for each of CUs,TUs for each DWO file. */
11289
11290static htab_up
11291allocate_dwo_unit_table ()
11292{
11293 /* Start out with a pretty small number.
11294 Generally DWO files contain only one CU and maybe some TUs. */
11295 return htab_up (htab_create_alloc (3,
11296 hash_dwo_unit,
11297 eq_dwo_unit,
11298 NULL, xcalloc, xfree));
11299}
11300
11301/* die_reader_func for create_dwo_cu. */
11302
11303static void
11304create_dwo_cu_reader (const struct die_reader_specs *reader,
11305 const gdb_byte *info_ptr,
11306 struct die_info *comp_unit_die,
11307 struct dwo_file *dwo_file,
11308 struct dwo_unit *dwo_unit)
11309{
11310 struct dwarf2_cu *cu = reader->cu;
11311 sect_offset sect_off = cu->per_cu->sect_off;
11312 struct dwarf2_section_info *section = cu->per_cu->section;
11313
11314 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11315 if (!signature.has_value ())
11316 {
11317 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11318 " its dwo_id [in module %s]"),
11319 sect_offset_str (sect_off), dwo_file->dwo_name);
11320 return;
11321 }
11322
11323 dwo_unit->dwo_file = dwo_file;
11324 dwo_unit->signature = *signature;
11325 dwo_unit->section = section;
11326 dwo_unit->sect_off = sect_off;
11327 dwo_unit->length = cu->per_cu->length;
11328
11329 if (dwarf_read_debug)
11330 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11331 sect_offset_str (sect_off),
11332 hex_string (dwo_unit->signature));
11333}
11334
11335/* Create the dwo_units for the CUs in a DWO_FILE.
11336 Note: This function processes DWO files only, not DWP files. */
11337
11338static void
11339create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11340 dwarf2_cu *cu, struct dwo_file &dwo_file,
11341 dwarf2_section_info &section, htab_up &cus_htab)
11342{
11343 struct objfile *objfile = dwarf2_per_objfile->objfile;
11344 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11345 const gdb_byte *info_ptr, *end_ptr;
11346
11347 section.read (objfile);
11348 info_ptr = section.buffer;
11349
11350 if (info_ptr == NULL)
11351 return;
11352
11353 if (dwarf_read_debug)
11354 {
11355 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11356 section.get_name (),
11357 section.get_file_name ());
11358 }
11359
11360 end_ptr = info_ptr + section.size;
11361 while (info_ptr < end_ptr)
11362 {
11363 struct dwarf2_per_cu_data per_cu;
11364 struct dwo_unit read_unit {};
11365 struct dwo_unit *dwo_unit;
11366 void **slot;
11367 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11368
11369 memset (&per_cu, 0, sizeof (per_cu));
11370 per_cu.per_bfd = per_bfd;
11371 per_cu.is_debug_types = 0;
11372 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11373 per_cu.section = &section;
11374
11375 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11376 if (!reader.dummy_p)
11377 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11378 &dwo_file, &read_unit);
11379 info_ptr += per_cu.length;
11380
11381 // If the unit could not be parsed, skip it.
11382 if (read_unit.dwo_file == NULL)
11383 continue;
11384
11385 if (cus_htab == NULL)
11386 cus_htab = allocate_dwo_unit_table ();
11387
11388 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11389 struct dwo_unit);
11390 *dwo_unit = read_unit;
11391 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11392 gdb_assert (slot != NULL);
11393 if (*slot != NULL)
11394 {
11395 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11396 sect_offset dup_sect_off = dup_cu->sect_off;
11397
11398 complaint (_("debug cu entry at offset %s is duplicate to"
11399 " the entry at offset %s, signature %s"),
11400 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11401 hex_string (dwo_unit->signature));
11402 }
11403 *slot = (void *)dwo_unit;
11404 }
11405}
11406
11407/* DWP file .debug_{cu,tu}_index section format:
11408 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11409
11410 DWP Version 1:
11411
11412 Both index sections have the same format, and serve to map a 64-bit
11413 signature to a set of section numbers. Each section begins with a header,
11414 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11415 indexes, and a pool of 32-bit section numbers. The index sections will be
11416 aligned at 8-byte boundaries in the file.
11417
11418 The index section header consists of:
11419
11420 V, 32 bit version number
11421 -, 32 bits unused
11422 N, 32 bit number of compilation units or type units in the index
11423 M, 32 bit number of slots in the hash table
11424
11425 Numbers are recorded using the byte order of the application binary.
11426
11427 The hash table begins at offset 16 in the section, and consists of an array
11428 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11429 order of the application binary). Unused slots in the hash table are 0.
11430 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11431
11432 The parallel table begins immediately after the hash table
11433 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11434 array of 32-bit indexes (using the byte order of the application binary),
11435 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11436 table contains a 32-bit index into the pool of section numbers. For unused
11437 hash table slots, the corresponding entry in the parallel table will be 0.
11438
11439 The pool of section numbers begins immediately following the hash table
11440 (at offset 16 + 12 * M from the beginning of the section). The pool of
11441 section numbers consists of an array of 32-bit words (using the byte order
11442 of the application binary). Each item in the array is indexed starting
11443 from 0. The hash table entry provides the index of the first section
11444 number in the set. Additional section numbers in the set follow, and the
11445 set is terminated by a 0 entry (section number 0 is not used in ELF).
11446
11447 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11448 section must be the first entry in the set, and the .debug_abbrev.dwo must
11449 be the second entry. Other members of the set may follow in any order.
11450
11451 ---
11452
11453 DWP Version 2:
11454
11455 DWP Version 2 combines all the .debug_info, etc. sections into one,
11456 and the entries in the index tables are now offsets into these sections.
11457 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11458 section.
11459
11460 Index Section Contents:
11461 Header
11462 Hash Table of Signatures dwp_hash_table.hash_table
11463 Parallel Table of Indices dwp_hash_table.unit_table
11464 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11465 Table of Section Sizes dwp_hash_table.v2.sizes
11466
11467 The index section header consists of:
11468
11469 V, 32 bit version number
11470 L, 32 bit number of columns in the table of section offsets
11471 N, 32 bit number of compilation units or type units in the index
11472 M, 32 bit number of slots in the hash table
11473
11474 Numbers are recorded using the byte order of the application binary.
11475
11476 The hash table has the same format as version 1.
11477 The parallel table of indices has the same format as version 1,
11478 except that the entries are origin-1 indices into the table of sections
11479 offsets and the table of section sizes.
11480
11481 The table of offsets begins immediately following the parallel table
11482 (at offset 16 + 12 * M from the beginning of the section). The table is
11483 a two-dimensional array of 32-bit words (using the byte order of the
11484 application binary), with L columns and N+1 rows, in row-major order.
11485 Each row in the array is indexed starting from 0. The first row provides
11486 a key to the remaining rows: each column in this row provides an identifier
11487 for a debug section, and the offsets in the same column of subsequent rows
11488 refer to that section. The section identifiers are:
11489
11490 DW_SECT_INFO 1 .debug_info.dwo
11491 DW_SECT_TYPES 2 .debug_types.dwo
11492 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11493 DW_SECT_LINE 4 .debug_line.dwo
11494 DW_SECT_LOC 5 .debug_loc.dwo
11495 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11496 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11497 DW_SECT_MACRO 8 .debug_macro.dwo
11498
11499 The offsets provided by the CU and TU index sections are the base offsets
11500 for the contributions made by each CU or TU to the corresponding section
11501 in the package file. Each CU and TU header contains an abbrev_offset
11502 field, used to find the abbreviations table for that CU or TU within the
11503 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11504 be interpreted as relative to the base offset given in the index section.
11505 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11506 should be interpreted as relative to the base offset for .debug_line.dwo,
11507 and offsets into other debug sections obtained from DWARF attributes should
11508 also be interpreted as relative to the corresponding base offset.
11509
11510 The table of sizes begins immediately following the table of offsets.
11511 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11512 with L columns and N rows, in row-major order. Each row in the array is
11513 indexed starting from 1 (row 0 is shared by the two tables).
11514
11515 ---
11516
11517 Hash table lookup is handled the same in version 1 and 2:
11518
11519 We assume that N and M will not exceed 2^32 - 1.
11520 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11521
11522 Given a 64-bit compilation unit signature or a type signature S, an entry
11523 in the hash table is located as follows:
11524
11525 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11526 the low-order k bits all set to 1.
11527
11528 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11529
11530 3) If the hash table entry at index H matches the signature, use that
11531 entry. If the hash table entry at index H is unused (all zeroes),
11532 terminate the search: the signature is not present in the table.
11533
11534 4) Let H = (H + H') modulo M. Repeat at Step 3.
11535
11536 Because M > N and H' and M are relatively prime, the search is guaranteed
11537 to stop at an unused slot or find the match. */
11538
11539/* Create a hash table to map DWO IDs to their CU/TU entry in
11540 .debug_{info,types}.dwo in DWP_FILE.
11541 Returns NULL if there isn't one.
11542 Note: This function processes DWP files only, not DWO files. */
11543
11544static struct dwp_hash_table *
11545create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11546 struct dwp_file *dwp_file, int is_debug_types)
11547{
11548 struct objfile *objfile = dwarf2_per_objfile->objfile;
11549 bfd *dbfd = dwp_file->dbfd.get ();
11550 const gdb_byte *index_ptr, *index_end;
11551 struct dwarf2_section_info *index;
11552 uint32_t version, nr_columns, nr_units, nr_slots;
11553 struct dwp_hash_table *htab;
11554
11555 if (is_debug_types)
11556 index = &dwp_file->sections.tu_index;
11557 else
11558 index = &dwp_file->sections.cu_index;
11559
11560 if (index->empty ())
11561 return NULL;
11562 index->read (objfile);
11563
11564 index_ptr = index->buffer;
11565 index_end = index_ptr + index->size;
11566
11567 version = read_4_bytes (dbfd, index_ptr);
11568 index_ptr += 4;
11569 if (version == 2)
11570 nr_columns = read_4_bytes (dbfd, index_ptr);
11571 else
11572 nr_columns = 0;
11573 index_ptr += 4;
11574 nr_units = read_4_bytes (dbfd, index_ptr);
11575 index_ptr += 4;
11576 nr_slots = read_4_bytes (dbfd, index_ptr);
11577 index_ptr += 4;
11578
11579 if (version != 1 && version != 2)
11580 {
11581 error (_("Dwarf Error: unsupported DWP file version (%s)"
11582 " [in module %s]"),
11583 pulongest (version), dwp_file->name);
11584 }
11585 if (nr_slots != (nr_slots & -nr_slots))
11586 {
11587 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11588 " is not power of 2 [in module %s]"),
11589 pulongest (nr_slots), dwp_file->name);
11590 }
11591
11592 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11593 htab->version = version;
11594 htab->nr_columns = nr_columns;
11595 htab->nr_units = nr_units;
11596 htab->nr_slots = nr_slots;
11597 htab->hash_table = index_ptr;
11598 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11599
11600 /* Exit early if the table is empty. */
11601 if (nr_slots == 0 || nr_units == 0
11602 || (version == 2 && nr_columns == 0))
11603 {
11604 /* All must be zero. */
11605 if (nr_slots != 0 || nr_units != 0
11606 || (version == 2 && nr_columns != 0))
11607 {
11608 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11609 " all zero [in modules %s]"),
11610 dwp_file->name);
11611 }
11612 return htab;
11613 }
11614
11615 if (version == 1)
11616 {
11617 htab->section_pool.v1.indices =
11618 htab->unit_table + sizeof (uint32_t) * nr_slots;
11619 /* It's harder to decide whether the section is too small in v1.
11620 V1 is deprecated anyway so we punt. */
11621 }
11622 else
11623 {
11624 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11625 int *ids = htab->section_pool.v2.section_ids;
11626 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11627 /* Reverse map for error checking. */
11628 int ids_seen[DW_SECT_MAX + 1];
11629 int i;
11630
11631 if (nr_columns < 2)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, too few columns"
11634 " in section table [in module %s]"),
11635 dwp_file->name);
11636 }
11637 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, too many columns"
11640 " in section table [in module %s]"),
11641 dwp_file->name);
11642 }
11643 memset (ids, 255, sizeof_ids);
11644 memset (ids_seen, 255, sizeof (ids_seen));
11645 for (i = 0; i < nr_columns; ++i)
11646 {
11647 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11648
11649 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11652 " in section table [in module %s]"),
11653 id, dwp_file->name);
11654 }
11655 if (ids_seen[id] != -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11658 " id %d in section table [in module %s]"),
11659 id, dwp_file->name);
11660 }
11661 ids_seen[id] = i;
11662 ids[i] = id;
11663 }
11664 /* Must have exactly one info or types section. */
11665 if (((ids_seen[DW_SECT_INFO] != -1)
11666 + (ids_seen[DW_SECT_TYPES] != -1))
11667 != 1)
11668 {
11669 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11670 " DWO info/types section [in module %s]"),
11671 dwp_file->name);
11672 }
11673 /* Must have an abbrev section. */
11674 if (ids_seen[DW_SECT_ABBREV] == -1)
11675 {
11676 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11677 " section [in module %s]"),
11678 dwp_file->name);
11679 }
11680 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11681 htab->section_pool.v2.sizes =
11682 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11683 * nr_units * nr_columns);
11684 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11685 * nr_units * nr_columns))
11686 > index_end)
11687 {
11688 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11689 " [in module %s]"),
11690 dwp_file->name);
11691 }
11692 }
11693
11694 return htab;
11695}
11696
11697/* Update SECTIONS with the data from SECTP.
11698
11699 This function is like the other "locate" section routines that are
11700 passed to bfd_map_over_sections, but in this context the sections to
11701 read comes from the DWP V1 hash table, not the full ELF section table.
11702
11703 The result is non-zero for success, or zero if an error was found. */
11704
11705static int
11706locate_v1_virtual_dwo_sections (asection *sectp,
11707 struct virtual_v1_dwo_sections *sections)
11708{
11709 const struct dwop_section_names *names = &dwop_section_names;
11710
11711 if (section_is_p (sectp->name, &names->abbrev_dwo))
11712 {
11713 /* There can be only one. */
11714 if (sections->abbrev.s.section != NULL)
11715 return 0;
11716 sections->abbrev.s.section = sectp;
11717 sections->abbrev.size = bfd_section_size (sectp);
11718 }
11719 else if (section_is_p (sectp->name, &names->info_dwo)
11720 || section_is_p (sectp->name, &names->types_dwo))
11721 {
11722 /* There can be only one. */
11723 if (sections->info_or_types.s.section != NULL)
11724 return 0;
11725 sections->info_or_types.s.section = sectp;
11726 sections->info_or_types.size = bfd_section_size (sectp);
11727 }
11728 else if (section_is_p (sectp->name, &names->line_dwo))
11729 {
11730 /* There can be only one. */
11731 if (sections->line.s.section != NULL)
11732 return 0;
11733 sections->line.s.section = sectp;
11734 sections->line.size = bfd_section_size (sectp);
11735 }
11736 else if (section_is_p (sectp->name, &names->loc_dwo))
11737 {
11738 /* There can be only one. */
11739 if (sections->loc.s.section != NULL)
11740 return 0;
11741 sections->loc.s.section = sectp;
11742 sections->loc.size = bfd_section_size (sectp);
11743 }
11744 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11745 {
11746 /* There can be only one. */
11747 if (sections->macinfo.s.section != NULL)
11748 return 0;
11749 sections->macinfo.s.section = sectp;
11750 sections->macinfo.size = bfd_section_size (sectp);
11751 }
11752 else if (section_is_p (sectp->name, &names->macro_dwo))
11753 {
11754 /* There can be only one. */
11755 if (sections->macro.s.section != NULL)
11756 return 0;
11757 sections->macro.s.section = sectp;
11758 sections->macro.size = bfd_section_size (sectp);
11759 }
11760 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11761 {
11762 /* There can be only one. */
11763 if (sections->str_offsets.s.section != NULL)
11764 return 0;
11765 sections->str_offsets.s.section = sectp;
11766 sections->str_offsets.size = bfd_section_size (sectp);
11767 }
11768 else
11769 {
11770 /* No other kind of section is valid. */
11771 return 0;
11772 }
11773
11774 return 1;
11775}
11776
11777/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11778 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11779 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11780 This is for DWP version 1 files. */
11781
11782static struct dwo_unit *
11783create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11784 struct dwp_file *dwp_file,
11785 uint32_t unit_index,
11786 const char *comp_dir,
11787 ULONGEST signature, int is_debug_types)
11788{
11789 const struct dwp_hash_table *dwp_htab =
11790 is_debug_types ? dwp_file->tus : dwp_file->cus;
11791 bfd *dbfd = dwp_file->dbfd.get ();
11792 const char *kind = is_debug_types ? "TU" : "CU";
11793 struct dwo_file *dwo_file;
11794 struct dwo_unit *dwo_unit;
11795 struct virtual_v1_dwo_sections sections;
11796 void **dwo_file_slot;
11797 int i;
11798
11799 gdb_assert (dwp_file->version == 1);
11800
11801 if (dwarf_read_debug)
11802 {
11803 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11804 kind,
11805 pulongest (unit_index), hex_string (signature),
11806 dwp_file->name);
11807 }
11808
11809 /* Fetch the sections of this DWO unit.
11810 Put a limit on the number of sections we look for so that bad data
11811 doesn't cause us to loop forever. */
11812
11813#define MAX_NR_V1_DWO_SECTIONS \
11814 (1 /* .debug_info or .debug_types */ \
11815 + 1 /* .debug_abbrev */ \
11816 + 1 /* .debug_line */ \
11817 + 1 /* .debug_loc */ \
11818 + 1 /* .debug_str_offsets */ \
11819 + 1 /* .debug_macro or .debug_macinfo */ \
11820 + 1 /* trailing zero */)
11821
11822 memset (&sections, 0, sizeof (sections));
11823
11824 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11825 {
11826 asection *sectp;
11827 uint32_t section_nr =
11828 read_4_bytes (dbfd,
11829 dwp_htab->section_pool.v1.indices
11830 + (unit_index + i) * sizeof (uint32_t));
11831
11832 if (section_nr == 0)
11833 break;
11834 if (section_nr >= dwp_file->num_sections)
11835 {
11836 error (_("Dwarf Error: bad DWP hash table, section number too large"
11837 " [in module %s]"),
11838 dwp_file->name);
11839 }
11840
11841 sectp = dwp_file->elf_sections[section_nr];
11842 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11843 {
11844 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11845 " [in module %s]"),
11846 dwp_file->name);
11847 }
11848 }
11849
11850 if (i < 2
11851 || sections.info_or_types.empty ()
11852 || sections.abbrev.empty ())
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11855 " [in module %s]"),
11856 dwp_file->name);
11857 }
11858 if (i == MAX_NR_V1_DWO_SECTIONS)
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11861 " [in module %s]"),
11862 dwp_file->name);
11863 }
11864
11865 /* It's easier for the rest of the code if we fake a struct dwo_file and
11866 have dwo_unit "live" in that. At least for now.
11867
11868 The DWP file can be made up of a random collection of CUs and TUs.
11869 However, for each CU + set of TUs that came from the same original DWO
11870 file, we can combine them back into a virtual DWO file to save space
11871 (fewer struct dwo_file objects to allocate). Remember that for really
11872 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11873
11874 std::string virtual_dwo_name =
11875 string_printf ("virtual-dwo/%d-%d-%d-%d",
11876 sections.abbrev.get_id (),
11877 sections.line.get_id (),
11878 sections.loc.get_id (),
11879 sections.str_offsets.get_id ());
11880 /* Can we use an existing virtual DWO file? */
11881 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11882 virtual_dwo_name.c_str (),
11883 comp_dir);
11884 /* Create one if necessary. */
11885 if (*dwo_file_slot == NULL)
11886 {
11887 if (dwarf_read_debug)
11888 {
11889 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11890 virtual_dwo_name.c_str ());
11891 }
11892 dwo_file = new struct dwo_file;
11893 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11894 dwo_file->comp_dir = comp_dir;
11895 dwo_file->sections.abbrev = sections.abbrev;
11896 dwo_file->sections.line = sections.line;
11897 dwo_file->sections.loc = sections.loc;
11898 dwo_file->sections.macinfo = sections.macinfo;
11899 dwo_file->sections.macro = sections.macro;
11900 dwo_file->sections.str_offsets = sections.str_offsets;
11901 /* The "str" section is global to the entire DWP file. */
11902 dwo_file->sections.str = dwp_file->sections.str;
11903 /* The info or types section is assigned below to dwo_unit,
11904 there's no need to record it in dwo_file.
11905 Also, we can't simply record type sections in dwo_file because
11906 we record a pointer into the vector in dwo_unit. As we collect more
11907 types we'll grow the vector and eventually have to reallocate space
11908 for it, invalidating all copies of pointers into the previous
11909 contents. */
11910 *dwo_file_slot = dwo_file;
11911 }
11912 else
11913 {
11914 if (dwarf_read_debug)
11915 {
11916 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11917 virtual_dwo_name.c_str ());
11918 }
11919 dwo_file = (struct dwo_file *) *dwo_file_slot;
11920 }
11921
11922 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11923 dwo_unit->dwo_file = dwo_file;
11924 dwo_unit->signature = signature;
11925 dwo_unit->section =
11926 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11927 *dwo_unit->section = sections.info_or_types;
11928 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11929
11930 return dwo_unit;
11931}
11932
11933/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11934 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11935 piece within that section used by a TU/CU, return a virtual section
11936 of just that piece. */
11937
11938static struct dwarf2_section_info
11939create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11940 struct dwarf2_section_info *section,
11941 bfd_size_type offset, bfd_size_type size)
11942{
11943 struct dwarf2_section_info result;
11944 asection *sectp;
11945
11946 gdb_assert (section != NULL);
11947 gdb_assert (!section->is_virtual);
11948
11949 memset (&result, 0, sizeof (result));
11950 result.s.containing_section = section;
11951 result.is_virtual = true;
11952
11953 if (size == 0)
11954 return result;
11955
11956 sectp = section->get_bfd_section ();
11957
11958 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11959 bounds of the real section. This is a pretty-rare event, so just
11960 flag an error (easier) instead of a warning and trying to cope. */
11961 if (sectp == NULL
11962 || offset + size > bfd_section_size (sectp))
11963 {
11964 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11965 " in section %s [in module %s]"),
11966 sectp ? bfd_section_name (sectp) : "<unknown>",
11967 objfile_name (dwarf2_per_objfile->objfile));
11968 }
11969
11970 result.virtual_offset = offset;
11971 result.size = size;
11972 return result;
11973}
11974
11975/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11976 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11977 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11978 This is for DWP version 2 files. */
11979
11980static struct dwo_unit *
11981create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11982 struct dwp_file *dwp_file,
11983 uint32_t unit_index,
11984 const char *comp_dir,
11985 ULONGEST signature, int is_debug_types)
11986{
11987 const struct dwp_hash_table *dwp_htab =
11988 is_debug_types ? dwp_file->tus : dwp_file->cus;
11989 bfd *dbfd = dwp_file->dbfd.get ();
11990 const char *kind = is_debug_types ? "TU" : "CU";
11991 struct dwo_file *dwo_file;
11992 struct dwo_unit *dwo_unit;
11993 struct virtual_v2_dwo_sections sections;
11994 void **dwo_file_slot;
11995 int i;
11996
11997 gdb_assert (dwp_file->version == 2);
11998
11999 if (dwarf_read_debug)
12000 {
12001 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12002 kind,
12003 pulongest (unit_index), hex_string (signature),
12004 dwp_file->name);
12005 }
12006
12007 /* Fetch the section offsets of this DWO unit. */
12008
12009 memset (&sections, 0, sizeof (sections));
12010
12011 for (i = 0; i < dwp_htab->nr_columns; ++i)
12012 {
12013 uint32_t offset = read_4_bytes (dbfd,
12014 dwp_htab->section_pool.v2.offsets
12015 + (((unit_index - 1) * dwp_htab->nr_columns
12016 + i)
12017 * sizeof (uint32_t)));
12018 uint32_t size = read_4_bytes (dbfd,
12019 dwp_htab->section_pool.v2.sizes
12020 + (((unit_index - 1) * dwp_htab->nr_columns
12021 + i)
12022 * sizeof (uint32_t)));
12023
12024 switch (dwp_htab->section_pool.v2.section_ids[i])
12025 {
12026 case DW_SECT_INFO:
12027 case DW_SECT_TYPES:
12028 sections.info_or_types_offset = offset;
12029 sections.info_or_types_size = size;
12030 break;
12031 case DW_SECT_ABBREV:
12032 sections.abbrev_offset = offset;
12033 sections.abbrev_size = size;
12034 break;
12035 case DW_SECT_LINE:
12036 sections.line_offset = offset;
12037 sections.line_size = size;
12038 break;
12039 case DW_SECT_LOC:
12040 sections.loc_offset = offset;
12041 sections.loc_size = size;
12042 break;
12043 case DW_SECT_STR_OFFSETS:
12044 sections.str_offsets_offset = offset;
12045 sections.str_offsets_size = size;
12046 break;
12047 case DW_SECT_MACINFO:
12048 sections.macinfo_offset = offset;
12049 sections.macinfo_size = size;
12050 break;
12051 case DW_SECT_MACRO:
12052 sections.macro_offset = offset;
12053 sections.macro_size = size;
12054 break;
12055 }
12056 }
12057
12058 /* It's easier for the rest of the code if we fake a struct dwo_file and
12059 have dwo_unit "live" in that. At least for now.
12060
12061 The DWP file can be made up of a random collection of CUs and TUs.
12062 However, for each CU + set of TUs that came from the same original DWO
12063 file, we can combine them back into a virtual DWO file to save space
12064 (fewer struct dwo_file objects to allocate). Remember that for really
12065 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12066
12067 std::string virtual_dwo_name =
12068 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12069 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12070 (long) (sections.line_size ? sections.line_offset : 0),
12071 (long) (sections.loc_size ? sections.loc_offset : 0),
12072 (long) (sections.str_offsets_size
12073 ? sections.str_offsets_offset : 0));
12074 /* Can we use an existing virtual DWO file? */
12075 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12076 virtual_dwo_name.c_str (),
12077 comp_dir);
12078 /* Create one if necessary. */
12079 if (*dwo_file_slot == NULL)
12080 {
12081 if (dwarf_read_debug)
12082 {
12083 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12084 virtual_dwo_name.c_str ());
12085 }
12086 dwo_file = new struct dwo_file;
12087 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12088 dwo_file->comp_dir = comp_dir;
12089 dwo_file->sections.abbrev =
12090 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12091 sections.abbrev_offset, sections.abbrev_size);
12092 dwo_file->sections.line =
12093 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12094 sections.line_offset, sections.line_size);
12095 dwo_file->sections.loc =
12096 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12097 sections.loc_offset, sections.loc_size);
12098 dwo_file->sections.macinfo =
12099 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12100 sections.macinfo_offset, sections.macinfo_size);
12101 dwo_file->sections.macro =
12102 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12103 sections.macro_offset, sections.macro_size);
12104 dwo_file->sections.str_offsets =
12105 create_dwp_v2_section (dwarf2_per_objfile,
12106 &dwp_file->sections.str_offsets,
12107 sections.str_offsets_offset,
12108 sections.str_offsets_size);
12109 /* The "str" section is global to the entire DWP file. */
12110 dwo_file->sections.str = dwp_file->sections.str;
12111 /* The info or types section is assigned below to dwo_unit,
12112 there's no need to record it in dwo_file.
12113 Also, we can't simply record type sections in dwo_file because
12114 we record a pointer into the vector in dwo_unit. As we collect more
12115 types we'll grow the vector and eventually have to reallocate space
12116 for it, invalidating all copies of pointers into the previous
12117 contents. */
12118 *dwo_file_slot = dwo_file;
12119 }
12120 else
12121 {
12122 if (dwarf_read_debug)
12123 {
12124 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12125 virtual_dwo_name.c_str ());
12126 }
12127 dwo_file = (struct dwo_file *) *dwo_file_slot;
12128 }
12129
12130 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12131 dwo_unit->dwo_file = dwo_file;
12132 dwo_unit->signature = signature;
12133 dwo_unit->section =
12134 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12135 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12136 is_debug_types
12137 ? &dwp_file->sections.types
12138 : &dwp_file->sections.info,
12139 sections.info_or_types_offset,
12140 sections.info_or_types_size);
12141 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12142
12143 return dwo_unit;
12144}
12145
12146/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12147 Returns NULL if the signature isn't found. */
12148
12149static struct dwo_unit *
12150lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12151 struct dwp_file *dwp_file, const char *comp_dir,
12152 ULONGEST signature, int is_debug_types)
12153{
12154 const struct dwp_hash_table *dwp_htab =
12155 is_debug_types ? dwp_file->tus : dwp_file->cus;
12156 bfd *dbfd = dwp_file->dbfd.get ();
12157 uint32_t mask = dwp_htab->nr_slots - 1;
12158 uint32_t hash = signature & mask;
12159 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12160 unsigned int i;
12161 void **slot;
12162 struct dwo_unit find_dwo_cu;
12163
12164 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12165 find_dwo_cu.signature = signature;
12166 slot = htab_find_slot (is_debug_types
12167 ? dwp_file->loaded_tus.get ()
12168 : dwp_file->loaded_cus.get (),
12169 &find_dwo_cu, INSERT);
12170
12171 if (*slot != NULL)
12172 return (struct dwo_unit *) *slot;
12173
12174 /* Use a for loop so that we don't loop forever on bad debug info. */
12175 for (i = 0; i < dwp_htab->nr_slots; ++i)
12176 {
12177 ULONGEST signature_in_table;
12178
12179 signature_in_table =
12180 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12181 if (signature_in_table == signature)
12182 {
12183 uint32_t unit_index =
12184 read_4_bytes (dbfd,
12185 dwp_htab->unit_table + hash * sizeof (uint32_t));
12186
12187 if (dwp_file->version == 1)
12188 {
12189 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12190 dwp_file, unit_index,
12191 comp_dir, signature,
12192 is_debug_types);
12193 }
12194 else
12195 {
12196 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12197 dwp_file, unit_index,
12198 comp_dir, signature,
12199 is_debug_types);
12200 }
12201 return (struct dwo_unit *) *slot;
12202 }
12203 if (signature_in_table == 0)
12204 return NULL;
12205 hash = (hash + hash2) & mask;
12206 }
12207
12208 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12209 " [in module %s]"),
12210 dwp_file->name);
12211}
12212
12213/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12214 Open the file specified by FILE_NAME and hand it off to BFD for
12215 preliminary analysis. Return a newly initialized bfd *, which
12216 includes a canonicalized copy of FILE_NAME.
12217 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12218 SEARCH_CWD is true if the current directory is to be searched.
12219 It will be searched before debug-file-directory.
12220 If successful, the file is added to the bfd include table of the
12221 objfile's bfd (see gdb_bfd_record_inclusion).
12222 If unable to find/open the file, return NULL.
12223 NOTE: This function is derived from symfile_bfd_open. */
12224
12225static gdb_bfd_ref_ptr
12226try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12227 const char *file_name, int is_dwp, int search_cwd)
12228{
12229 int desc;
12230 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12231 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12232 to debug_file_directory. */
12233 const char *search_path;
12234 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12235
12236 gdb::unique_xmalloc_ptr<char> search_path_holder;
12237 if (search_cwd)
12238 {
12239 if (*debug_file_directory != '\0')
12240 {
12241 search_path_holder.reset (concat (".", dirname_separator_string,
12242 debug_file_directory,
12243 (char *) NULL));
12244 search_path = search_path_holder.get ();
12245 }
12246 else
12247 search_path = ".";
12248 }
12249 else
12250 search_path = debug_file_directory;
12251
12252 openp_flags flags = OPF_RETURN_REALPATH;
12253 if (is_dwp)
12254 flags |= OPF_SEARCH_IN_PATH;
12255
12256 gdb::unique_xmalloc_ptr<char> absolute_name;
12257 desc = openp (search_path, flags, file_name,
12258 O_RDONLY | O_BINARY, &absolute_name);
12259 if (desc < 0)
12260 return NULL;
12261
12262 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12263 gnutarget, desc));
12264 if (sym_bfd == NULL)
12265 return NULL;
12266 bfd_set_cacheable (sym_bfd.get (), 1);
12267
12268 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12269 return NULL;
12270
12271 /* Success. Record the bfd as having been included by the objfile's bfd.
12272 This is important because things like demangled_names_hash lives in the
12273 objfile's per_bfd space and may have references to things like symbol
12274 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12275 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12276
12277 return sym_bfd;
12278}
12279
12280/* Try to open DWO file FILE_NAME.
12281 COMP_DIR is the DW_AT_comp_dir attribute.
12282 The result is the bfd handle of the file.
12283 If there is a problem finding or opening the file, return NULL.
12284 Upon success, the canonicalized path of the file is stored in the bfd,
12285 same as symfile_bfd_open. */
12286
12287static gdb_bfd_ref_ptr
12288open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12289 const char *file_name, const char *comp_dir)
12290{
12291 if (IS_ABSOLUTE_PATH (file_name))
12292 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12293 0 /*is_dwp*/, 0 /*search_cwd*/);
12294
12295 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12296
12297 if (comp_dir != NULL)
12298 {
12299 gdb::unique_xmalloc_ptr<char> path_to_try
12300 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12301
12302 /* NOTE: If comp_dir is a relative path, this will also try the
12303 search path, which seems useful. */
12304 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12305 path_to_try.get (),
12306 0 /*is_dwp*/,
12307 1 /*search_cwd*/));
12308 if (abfd != NULL)
12309 return abfd;
12310 }
12311
12312 /* That didn't work, try debug-file-directory, which, despite its name,
12313 is a list of paths. */
12314
12315 if (*debug_file_directory == '\0')
12316 return NULL;
12317
12318 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12319 0 /*is_dwp*/, 1 /*search_cwd*/);
12320}
12321
12322/* This function is mapped across the sections and remembers the offset and
12323 size of each of the DWO debugging sections we are interested in. */
12324
12325static void
12326dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12327{
12328 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12329 const struct dwop_section_names *names = &dwop_section_names;
12330
12331 if (section_is_p (sectp->name, &names->abbrev_dwo))
12332 {
12333 dwo_sections->abbrev.s.section = sectp;
12334 dwo_sections->abbrev.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->info_dwo))
12337 {
12338 dwo_sections->info.s.section = sectp;
12339 dwo_sections->info.size = bfd_section_size (sectp);
12340 }
12341 else if (section_is_p (sectp->name, &names->line_dwo))
12342 {
12343 dwo_sections->line.s.section = sectp;
12344 dwo_sections->line.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->loc_dwo))
12347 {
12348 dwo_sections->loc.s.section = sectp;
12349 dwo_sections->loc.size = bfd_section_size (sectp);
12350 }
12351 else if (section_is_p (sectp->name, &names->loclists_dwo))
12352 {
12353 dwo_sections->loclists.s.section = sectp;
12354 dwo_sections->loclists.size = bfd_section_size (sectp);
12355 }
12356 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12357 {
12358 dwo_sections->macinfo.s.section = sectp;
12359 dwo_sections->macinfo.size = bfd_section_size (sectp);
12360 }
12361 else if (section_is_p (sectp->name, &names->macro_dwo))
12362 {
12363 dwo_sections->macro.s.section = sectp;
12364 dwo_sections->macro.size = bfd_section_size (sectp);
12365 }
12366 else if (section_is_p (sectp->name, &names->str_dwo))
12367 {
12368 dwo_sections->str.s.section = sectp;
12369 dwo_sections->str.size = bfd_section_size (sectp);
12370 }
12371 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12372 {
12373 dwo_sections->str_offsets.s.section = sectp;
12374 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12375 }
12376 else if (section_is_p (sectp->name, &names->types_dwo))
12377 {
12378 struct dwarf2_section_info type_section;
12379
12380 memset (&type_section, 0, sizeof (type_section));
12381 type_section.s.section = sectp;
12382 type_section.size = bfd_section_size (sectp);
12383 dwo_sections->types.push_back (type_section);
12384 }
12385}
12386
12387/* Initialize the use of the DWO file specified by DWO_NAME and referenced
12388 by PER_CU. This is for the non-DWP case.
12389 The result is NULL if DWO_NAME can't be found. */
12390
12391static struct dwo_file *
12392open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12393 const char *comp_dir)
12394{
12395 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12396
12397 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12398 if (dbfd == NULL)
12399 {
12400 if (dwarf_read_debug)
12401 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12402 return NULL;
12403 }
12404
12405 dwo_file_up dwo_file (new struct dwo_file);
12406 dwo_file->dwo_name = dwo_name;
12407 dwo_file->comp_dir = comp_dir;
12408 dwo_file->dbfd = std::move (dbfd);
12409
12410 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12411 &dwo_file->sections);
12412
12413 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12414 dwo_file->sections.info, dwo_file->cus);
12415
12416 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12417 dwo_file->sections.types, dwo_file->tus);
12418
12419 if (dwarf_read_debug)
12420 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12421
12422 return dwo_file.release ();
12423}
12424
12425/* This function is mapped across the sections and remembers the offset and
12426 size of each of the DWP debugging sections common to version 1 and 2 that
12427 we are interested in. */
12428
12429static void
12430dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12431 void *dwp_file_ptr)
12432{
12433 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12434 const struct dwop_section_names *names = &dwop_section_names;
12435 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12436
12437 /* Record the ELF section number for later lookup: this is what the
12438 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12439 gdb_assert (elf_section_nr < dwp_file->num_sections);
12440 dwp_file->elf_sections[elf_section_nr] = sectp;
12441
12442 /* Look for specific sections that we need. */
12443 if (section_is_p (sectp->name, &names->str_dwo))
12444 {
12445 dwp_file->sections.str.s.section = sectp;
12446 dwp_file->sections.str.size = bfd_section_size (sectp);
12447 }
12448 else if (section_is_p (sectp->name, &names->cu_index))
12449 {
12450 dwp_file->sections.cu_index.s.section = sectp;
12451 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12452 }
12453 else if (section_is_p (sectp->name, &names->tu_index))
12454 {
12455 dwp_file->sections.tu_index.s.section = sectp;
12456 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12457 }
12458}
12459
12460/* This function is mapped across the sections and remembers the offset and
12461 size of each of the DWP version 2 debugging sections that we are interested
12462 in. This is split into a separate function because we don't know if we
12463 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12464
12465static void
12466dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12467{
12468 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12469 const struct dwop_section_names *names = &dwop_section_names;
12470 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12471
12472 /* Record the ELF section number for later lookup: this is what the
12473 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12474 gdb_assert (elf_section_nr < dwp_file->num_sections);
12475 dwp_file->elf_sections[elf_section_nr] = sectp;
12476
12477 /* Look for specific sections that we need. */
12478 if (section_is_p (sectp->name, &names->abbrev_dwo))
12479 {
12480 dwp_file->sections.abbrev.s.section = sectp;
12481 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12482 }
12483 else if (section_is_p (sectp->name, &names->info_dwo))
12484 {
12485 dwp_file->sections.info.s.section = sectp;
12486 dwp_file->sections.info.size = bfd_section_size (sectp);
12487 }
12488 else if (section_is_p (sectp->name, &names->line_dwo))
12489 {
12490 dwp_file->sections.line.s.section = sectp;
12491 dwp_file->sections.line.size = bfd_section_size (sectp);
12492 }
12493 else if (section_is_p (sectp->name, &names->loc_dwo))
12494 {
12495 dwp_file->sections.loc.s.section = sectp;
12496 dwp_file->sections.loc.size = bfd_section_size (sectp);
12497 }
12498 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12499 {
12500 dwp_file->sections.macinfo.s.section = sectp;
12501 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12502 }
12503 else if (section_is_p (sectp->name, &names->macro_dwo))
12504 {
12505 dwp_file->sections.macro.s.section = sectp;
12506 dwp_file->sections.macro.size = bfd_section_size (sectp);
12507 }
12508 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12509 {
12510 dwp_file->sections.str_offsets.s.section = sectp;
12511 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12512 }
12513 else if (section_is_p (sectp->name, &names->types_dwo))
12514 {
12515 dwp_file->sections.types.s.section = sectp;
12516 dwp_file->sections.types.size = bfd_section_size (sectp);
12517 }
12518}
12519
12520/* Hash function for dwp_file loaded CUs/TUs. */
12521
12522static hashval_t
12523hash_dwp_loaded_cutus (const void *item)
12524{
12525 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12526
12527 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12528 return dwo_unit->signature;
12529}
12530
12531/* Equality function for dwp_file loaded CUs/TUs. */
12532
12533static int
12534eq_dwp_loaded_cutus (const void *a, const void *b)
12535{
12536 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12537 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12538
12539 return dua->signature == dub->signature;
12540}
12541
12542/* Allocate a hash table for dwp_file loaded CUs/TUs. */
12543
12544static htab_up
12545allocate_dwp_loaded_cutus_table ()
12546{
12547 return htab_up (htab_create_alloc (3,
12548 hash_dwp_loaded_cutus,
12549 eq_dwp_loaded_cutus,
12550 NULL, xcalloc, xfree));
12551}
12552
12553/* Try to open DWP file FILE_NAME.
12554 The result is the bfd handle of the file.
12555 If there is a problem finding or opening the file, return NULL.
12556 Upon success, the canonicalized path of the file is stored in the bfd,
12557 same as symfile_bfd_open. */
12558
12559static gdb_bfd_ref_ptr
12560open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12561 const char *file_name)
12562{
12563 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12564 1 /*is_dwp*/,
12565 1 /*search_cwd*/));
12566 if (abfd != NULL)
12567 return abfd;
12568
12569 /* Work around upstream bug 15652.
12570 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12571 [Whether that's a "bug" is debatable, but it is getting in our way.]
12572 We have no real idea where the dwp file is, because gdb's realpath-ing
12573 of the executable's path may have discarded the needed info.
12574 [IWBN if the dwp file name was recorded in the executable, akin to
12575 .gnu_debuglink, but that doesn't exist yet.]
12576 Strip the directory from FILE_NAME and search again. */
12577 if (*debug_file_directory != '\0')
12578 {
12579 /* Don't implicitly search the current directory here.
12580 If the user wants to search "." to handle this case,
12581 it must be added to debug-file-directory. */
12582 return try_open_dwop_file (dwarf2_per_objfile,
12583 lbasename (file_name), 1 /*is_dwp*/,
12584 0 /*search_cwd*/);
12585 }
12586
12587 return NULL;
12588}
12589
12590/* Initialize the use of the DWP file for the current objfile.
12591 By convention the name of the DWP file is ${objfile}.dwp.
12592 The result is NULL if it can't be found. */
12593
12594static std::unique_ptr<struct dwp_file>
12595open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12596{
12597 struct objfile *objfile = dwarf2_per_objfile->objfile;
12598
12599 /* Try to find first .dwp for the binary file before any symbolic links
12600 resolving. */
12601
12602 /* If the objfile is a debug file, find the name of the real binary
12603 file and get the name of dwp file from there. */
12604 std::string dwp_name;
12605 if (objfile->separate_debug_objfile_backlink != NULL)
12606 {
12607 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12608 const char *backlink_basename = lbasename (backlink->original_name);
12609
12610 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12611 }
12612 else
12613 dwp_name = objfile->original_name;
12614
12615 dwp_name += ".dwp";
12616
12617 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12618 if (dbfd == NULL
12619 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12620 {
12621 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12622 dwp_name = objfile_name (objfile);
12623 dwp_name += ".dwp";
12624 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12625 }
12626
12627 if (dbfd == NULL)
12628 {
12629 if (dwarf_read_debug)
12630 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12631 return std::unique_ptr<dwp_file> ();
12632 }
12633
12634 const char *name = bfd_get_filename (dbfd.get ());
12635 std::unique_ptr<struct dwp_file> dwp_file
12636 (new struct dwp_file (name, std::move (dbfd)));
12637
12638 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12639 dwp_file->elf_sections =
12640 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12641 dwp_file->num_sections, asection *);
12642
12643 bfd_map_over_sections (dwp_file->dbfd.get (),
12644 dwarf2_locate_common_dwp_sections,
12645 dwp_file.get ());
12646
12647 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12648 0);
12649
12650 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12651 1);
12652
12653 /* The DWP file version is stored in the hash table. Oh well. */
12654 if (dwp_file->cus && dwp_file->tus
12655 && dwp_file->cus->version != dwp_file->tus->version)
12656 {
12657 /* Technically speaking, we should try to limp along, but this is
12658 pretty bizarre. We use pulongest here because that's the established
12659 portability solution (e.g, we cannot use %u for uint32_t). */
12660 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12661 " TU version %s [in DWP file %s]"),
12662 pulongest (dwp_file->cus->version),
12663 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12664 }
12665
12666 if (dwp_file->cus)
12667 dwp_file->version = dwp_file->cus->version;
12668 else if (dwp_file->tus)
12669 dwp_file->version = dwp_file->tus->version;
12670 else
12671 dwp_file->version = 2;
12672
12673 if (dwp_file->version == 2)
12674 bfd_map_over_sections (dwp_file->dbfd.get (),
12675 dwarf2_locate_v2_dwp_sections,
12676 dwp_file.get ());
12677
12678 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12679 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12680
12681 if (dwarf_read_debug)
12682 {
12683 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12684 fprintf_unfiltered (gdb_stdlog,
12685 " %s CUs, %s TUs\n",
12686 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12687 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12688 }
12689
12690 return dwp_file;
12691}
12692
12693/* Wrapper around open_and_init_dwp_file, only open it once. */
12694
12695static struct dwp_file *
12696get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12697{
12698 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12699 {
12700 dwarf2_per_objfile->per_bfd->dwp_file
12701 = open_and_init_dwp_file (dwarf2_per_objfile);
12702 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12703 }
12704 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12705}
12706
12707/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12708 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12709 or in the DWP file for the objfile, referenced by THIS_UNIT.
12710 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12711 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12712
12713 This is called, for example, when wanting to read a variable with a
12714 complex location. Therefore we don't want to do file i/o for every call.
12715 Therefore we don't want to look for a DWO file on every call.
12716 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12717 then we check if we've already seen DWO_NAME, and only THEN do we check
12718 for a DWO file.
12719
12720 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12721 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12722
12723static struct dwo_unit *
12724lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12725 ULONGEST signature, int is_debug_types)
12726{
12727 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12728 struct objfile *objfile = dwarf2_per_objfile->objfile;
12729 const char *kind = is_debug_types ? "TU" : "CU";
12730 void **dwo_file_slot;
12731 struct dwo_file *dwo_file;
12732 struct dwp_file *dwp_file;
12733
12734 /* First see if there's a DWP file.
12735 If we have a DWP file but didn't find the DWO inside it, don't
12736 look for the original DWO file. It makes gdb behave differently
12737 depending on whether one is debugging in the build tree. */
12738
12739 dwp_file = get_dwp_file (dwarf2_per_objfile);
12740 if (dwp_file != NULL)
12741 {
12742 const struct dwp_hash_table *dwp_htab =
12743 is_debug_types ? dwp_file->tus : dwp_file->cus;
12744
12745 if (dwp_htab != NULL)
12746 {
12747 struct dwo_unit *dwo_cutu =
12748 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12749 signature, is_debug_types);
12750
12751 if (dwo_cutu != NULL)
12752 {
12753 if (dwarf_read_debug)
12754 {
12755 fprintf_unfiltered (gdb_stdlog,
12756 "Virtual DWO %s %s found: @%s\n",
12757 kind, hex_string (signature),
12758 host_address_to_string (dwo_cutu));
12759 }
12760 return dwo_cutu;
12761 }
12762 }
12763 }
12764 else
12765 {
12766 /* No DWP file, look for the DWO file. */
12767
12768 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12769 dwo_name, comp_dir);
12770 if (*dwo_file_slot == NULL)
12771 {
12772 /* Read in the file and build a table of the CUs/TUs it contains. */
12773 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12774 }
12775 /* NOTE: This will be NULL if unable to open the file. */
12776 dwo_file = (struct dwo_file *) *dwo_file_slot;
12777
12778 if (dwo_file != NULL)
12779 {
12780 struct dwo_unit *dwo_cutu = NULL;
12781
12782 if (is_debug_types && dwo_file->tus)
12783 {
12784 struct dwo_unit find_dwo_cutu;
12785
12786 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12787 find_dwo_cutu.signature = signature;
12788 dwo_cutu
12789 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12790 &find_dwo_cutu);
12791 }
12792 else if (!is_debug_types && dwo_file->cus)
12793 {
12794 struct dwo_unit find_dwo_cutu;
12795
12796 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12797 find_dwo_cutu.signature = signature;
12798 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12799 &find_dwo_cutu);
12800 }
12801
12802 if (dwo_cutu != NULL)
12803 {
12804 if (dwarf_read_debug)
12805 {
12806 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12807 kind, dwo_name, hex_string (signature),
12808 host_address_to_string (dwo_cutu));
12809 }
12810 return dwo_cutu;
12811 }
12812 }
12813 }
12814
12815 /* We didn't find it. This could mean a dwo_id mismatch, or
12816 someone deleted the DWO/DWP file, or the search path isn't set up
12817 correctly to find the file. */
12818
12819 if (dwarf_read_debug)
12820 {
12821 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12822 kind, dwo_name, hex_string (signature));
12823 }
12824
12825 /* This is a warning and not a complaint because it can be caused by
12826 pilot error (e.g., user accidentally deleting the DWO). */
12827 {
12828 /* Print the name of the DWP file if we looked there, helps the user
12829 better diagnose the problem. */
12830 std::string dwp_text;
12831
12832 if (dwp_file != NULL)
12833 dwp_text = string_printf (" [in DWP file %s]",
12834 lbasename (dwp_file->name));
12835
12836 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12837 " [in module %s]"),
12838 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12839 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12840 }
12841 return NULL;
12842}
12843
12844/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12845 See lookup_dwo_cutu_unit for details. */
12846
12847static struct dwo_unit *
12848lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12849 ULONGEST signature)
12850{
12851 gdb_assert (!cu->per_cu->is_debug_types);
12852
12853 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12854}
12855
12856/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12857 See lookup_dwo_cutu_unit for details. */
12858
12859static struct dwo_unit *
12860lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12861{
12862 gdb_assert (cu->per_cu->is_debug_types);
12863
12864 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12865
12866 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12867}
12868
12869/* Traversal function for queue_and_load_all_dwo_tus. */
12870
12871static int
12872queue_and_load_dwo_tu (void **slot, void *info)
12873{
12874 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12875 dwarf2_cu *cu = (dwarf2_cu *) info;
12876 ULONGEST signature = dwo_unit->signature;
12877 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12878
12879 if (sig_type != NULL)
12880 {
12881 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12882
12883 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12884 a real dependency of PER_CU on SIG_TYPE. That is detected later
12885 while processing PER_CU. */
12886 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12887 load_full_type_unit (sig_cu, cu->per_objfile);
12888 cu->per_cu->imported_symtabs_push (sig_cu);
12889 }
12890
12891 return 1;
12892}
12893
12894/* Queue all TUs contained in the DWO of PER_CU to be read in.
12895 The DWO may have the only definition of the type, though it may not be
12896 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12897 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12898
12899static void
12900queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12901{
12902 struct dwo_unit *dwo_unit;
12903 struct dwo_file *dwo_file;
12904
12905 gdb_assert (!per_cu->is_debug_types);
12906 gdb_assert (per_cu->cu != NULL);
12907 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
12908
12909 dwo_unit = per_cu->cu->dwo_unit;
12910 gdb_assert (dwo_unit != NULL);
12911
12912 dwo_file = dwo_unit->dwo_file;
12913 if (dwo_file->tus != NULL)
12914 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12915 per_cu->cu);
12916}
12917
12918/* Read in various DIEs. */
12919
12920/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12921 Inherit only the children of the DW_AT_abstract_origin DIE not being
12922 already referenced by DW_AT_abstract_origin from the children of the
12923 current DIE. */
12924
12925static void
12926inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12927{
12928 struct die_info *child_die;
12929 sect_offset *offsetp;
12930 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12931 struct die_info *origin_die;
12932 /* Iterator of the ORIGIN_DIE children. */
12933 struct die_info *origin_child_die;
12934 struct attribute *attr;
12935 struct dwarf2_cu *origin_cu;
12936 struct pending **origin_previous_list_in_scope;
12937
12938 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12939 if (!attr)
12940 return;
12941
12942 /* Note that following die references may follow to a die in a
12943 different cu. */
12944
12945 origin_cu = cu;
12946 origin_die = follow_die_ref (die, attr, &origin_cu);
12947
12948 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12949 symbols in. */
12950 origin_previous_list_in_scope = origin_cu->list_in_scope;
12951 origin_cu->list_in_scope = cu->list_in_scope;
12952
12953 if (die->tag != origin_die->tag
12954 && !(die->tag == DW_TAG_inlined_subroutine
12955 && origin_die->tag == DW_TAG_subprogram))
12956 complaint (_("DIE %s and its abstract origin %s have different tags"),
12957 sect_offset_str (die->sect_off),
12958 sect_offset_str (origin_die->sect_off));
12959
12960 std::vector<sect_offset> offsets;
12961
12962 for (child_die = die->child;
12963 child_die && child_die->tag;
12964 child_die = child_die->sibling)
12965 {
12966 struct die_info *child_origin_die;
12967 struct dwarf2_cu *child_origin_cu;
12968
12969 /* We are trying to process concrete instance entries:
12970 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12971 it's not relevant to our analysis here. i.e. detecting DIEs that are
12972 present in the abstract instance but not referenced in the concrete
12973 one. */
12974 if (child_die->tag == DW_TAG_call_site
12975 || child_die->tag == DW_TAG_GNU_call_site)
12976 continue;
12977
12978 /* For each CHILD_DIE, find the corresponding child of
12979 ORIGIN_DIE. If there is more than one layer of
12980 DW_AT_abstract_origin, follow them all; there shouldn't be,
12981 but GCC versions at least through 4.4 generate this (GCC PR
12982 40573). */
12983 child_origin_die = child_die;
12984 child_origin_cu = cu;
12985 while (1)
12986 {
12987 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12988 child_origin_cu);
12989 if (attr == NULL)
12990 break;
12991 child_origin_die = follow_die_ref (child_origin_die, attr,
12992 &child_origin_cu);
12993 }
12994
12995 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12996 counterpart may exist. */
12997 if (child_origin_die != child_die)
12998 {
12999 if (child_die->tag != child_origin_die->tag
13000 && !(child_die->tag == DW_TAG_inlined_subroutine
13001 && child_origin_die->tag == DW_TAG_subprogram))
13002 complaint (_("Child DIE %s and its abstract origin %s have "
13003 "different tags"),
13004 sect_offset_str (child_die->sect_off),
13005 sect_offset_str (child_origin_die->sect_off));
13006 if (child_origin_die->parent != origin_die)
13007 complaint (_("Child DIE %s and its abstract origin %s have "
13008 "different parents"),
13009 sect_offset_str (child_die->sect_off),
13010 sect_offset_str (child_origin_die->sect_off));
13011 else
13012 offsets.push_back (child_origin_die->sect_off);
13013 }
13014 }
13015 std::sort (offsets.begin (), offsets.end ());
13016 sect_offset *offsets_end = offsets.data () + offsets.size ();
13017 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13018 if (offsetp[-1] == *offsetp)
13019 complaint (_("Multiple children of DIE %s refer "
13020 "to DIE %s as their abstract origin"),
13021 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13022
13023 offsetp = offsets.data ();
13024 origin_child_die = origin_die->child;
13025 while (origin_child_die && origin_child_die->tag)
13026 {
13027 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13028 while (offsetp < offsets_end
13029 && *offsetp < origin_child_die->sect_off)
13030 offsetp++;
13031 if (offsetp >= offsets_end
13032 || *offsetp > origin_child_die->sect_off)
13033 {
13034 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13035 Check whether we're already processing ORIGIN_CHILD_DIE.
13036 This can happen with mutually referenced abstract_origins.
13037 PR 16581. */
13038 if (!origin_child_die->in_process)
13039 process_die (origin_child_die, origin_cu);
13040 }
13041 origin_child_die = origin_child_die->sibling;
13042 }
13043 origin_cu->list_in_scope = origin_previous_list_in_scope;
13044
13045 if (cu != origin_cu)
13046 compute_delayed_physnames (origin_cu);
13047}
13048
13049static void
13050read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13051{
13052 struct objfile *objfile = cu->per_objfile->objfile;
13053 struct gdbarch *gdbarch = objfile->arch ();
13054 struct context_stack *newobj;
13055 CORE_ADDR lowpc;
13056 CORE_ADDR highpc;
13057 struct die_info *child_die;
13058 struct attribute *attr, *call_line, *call_file;
13059 const char *name;
13060 CORE_ADDR baseaddr;
13061 struct block *block;
13062 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13063 std::vector<struct symbol *> template_args;
13064 struct template_symbol *templ_func = NULL;
13065
13066 if (inlined_func)
13067 {
13068 /* If we do not have call site information, we can't show the
13069 caller of this inlined function. That's too confusing, so
13070 only use the scope for local variables. */
13071 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13072 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13073 if (call_line == NULL || call_file == NULL)
13074 {
13075 read_lexical_block_scope (die, cu);
13076 return;
13077 }
13078 }
13079
13080 baseaddr = objfile->text_section_offset ();
13081
13082 name = dwarf2_name (die, cu);
13083
13084 /* Ignore functions with missing or empty names. These are actually
13085 illegal according to the DWARF standard. */
13086 if (name == NULL)
13087 {
13088 complaint (_("missing name for subprogram DIE at %s"),
13089 sect_offset_str (die->sect_off));
13090 return;
13091 }
13092
13093 /* Ignore functions with missing or invalid low and high pc attributes. */
13094 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13095 <= PC_BOUNDS_INVALID)
13096 {
13097 attr = dwarf2_attr (die, DW_AT_external, cu);
13098 if (!attr || !DW_UNSND (attr))
13099 complaint (_("cannot get low and high bounds "
13100 "for subprogram DIE at %s"),
13101 sect_offset_str (die->sect_off));
13102 return;
13103 }
13104
13105 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13106 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13107
13108 /* If we have any template arguments, then we must allocate a
13109 different sort of symbol. */
13110 for (child_die = die->child; child_die; child_die = child_die->sibling)
13111 {
13112 if (child_die->tag == DW_TAG_template_type_param
13113 || child_die->tag == DW_TAG_template_value_param)
13114 {
13115 templ_func = new (&objfile->objfile_obstack) template_symbol;
13116 templ_func->subclass = SYMBOL_TEMPLATE;
13117 break;
13118 }
13119 }
13120
13121 newobj = cu->get_builder ()->push_context (0, lowpc);
13122 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13123 (struct symbol *) templ_func);
13124
13125 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13126 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13127 cu->language);
13128
13129 /* If there is a location expression for DW_AT_frame_base, record
13130 it. */
13131 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13132 if (attr != nullptr)
13133 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13134
13135 /* If there is a location for the static link, record it. */
13136 newobj->static_link = NULL;
13137 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13138 if (attr != nullptr)
13139 {
13140 newobj->static_link
13141 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13142 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13143 cu->addr_type ());
13144 }
13145
13146 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13147
13148 if (die->child != NULL)
13149 {
13150 child_die = die->child;
13151 while (child_die && child_die->tag)
13152 {
13153 if (child_die->tag == DW_TAG_template_type_param
13154 || child_die->tag == DW_TAG_template_value_param)
13155 {
13156 struct symbol *arg = new_symbol (child_die, NULL, cu);
13157
13158 if (arg != NULL)
13159 template_args.push_back (arg);
13160 }
13161 else
13162 process_die (child_die, cu);
13163 child_die = child_die->sibling;
13164 }
13165 }
13166
13167 inherit_abstract_dies (die, cu);
13168
13169 /* If we have a DW_AT_specification, we might need to import using
13170 directives from the context of the specification DIE. See the
13171 comment in determine_prefix. */
13172 if (cu->language == language_cplus
13173 && dwarf2_attr (die, DW_AT_specification, cu))
13174 {
13175 struct dwarf2_cu *spec_cu = cu;
13176 struct die_info *spec_die = die_specification (die, &spec_cu);
13177
13178 while (spec_die)
13179 {
13180 child_die = spec_die->child;
13181 while (child_die && child_die->tag)
13182 {
13183 if (child_die->tag == DW_TAG_imported_module)
13184 process_die (child_die, spec_cu);
13185 child_die = child_die->sibling;
13186 }
13187
13188 /* In some cases, GCC generates specification DIEs that
13189 themselves contain DW_AT_specification attributes. */
13190 spec_die = die_specification (spec_die, &spec_cu);
13191 }
13192 }
13193
13194 struct context_stack cstk = cu->get_builder ()->pop_context ();
13195 /* Make a block for the local symbols within. */
13196 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13197 cstk.static_link, lowpc, highpc);
13198
13199 /* For C++, set the block's scope. */
13200 if ((cu->language == language_cplus
13201 || cu->language == language_fortran
13202 || cu->language == language_d
13203 || cu->language == language_rust)
13204 && cu->processing_has_namespace_info)
13205 block_set_scope (block, determine_prefix (die, cu),
13206 &objfile->objfile_obstack);
13207
13208 /* If we have address ranges, record them. */
13209 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13210
13211 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13212
13213 /* Attach template arguments to function. */
13214 if (!template_args.empty ())
13215 {
13216 gdb_assert (templ_func != NULL);
13217
13218 templ_func->n_template_arguments = template_args.size ();
13219 templ_func->template_arguments
13220 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13221 templ_func->n_template_arguments);
13222 memcpy (templ_func->template_arguments,
13223 template_args.data (),
13224 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13225
13226 /* Make sure that the symtab is set on the new symbols. Even
13227 though they don't appear in this symtab directly, other parts
13228 of gdb assume that symbols do, and this is reasonably
13229 true. */
13230 for (symbol *sym : template_args)
13231 symbol_set_symtab (sym, symbol_symtab (templ_func));
13232 }
13233
13234 /* In C++, we can have functions nested inside functions (e.g., when
13235 a function declares a class that has methods). This means that
13236 when we finish processing a function scope, we may need to go
13237 back to building a containing block's symbol lists. */
13238 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13239 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13240
13241 /* If we've finished processing a top-level function, subsequent
13242 symbols go in the file symbol list. */
13243 if (cu->get_builder ()->outermost_context_p ())
13244 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13245}
13246
13247/* Process all the DIES contained within a lexical block scope. Start
13248 a new scope, process the dies, and then close the scope. */
13249
13250static void
13251read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13252{
13253 struct objfile *objfile = cu->per_objfile->objfile;
13254 struct gdbarch *gdbarch = objfile->arch ();
13255 CORE_ADDR lowpc, highpc;
13256 struct die_info *child_die;
13257 CORE_ADDR baseaddr;
13258
13259 baseaddr = objfile->text_section_offset ();
13260
13261 /* Ignore blocks with missing or invalid low and high pc attributes. */
13262 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13263 as multiple lexical blocks? Handling children in a sane way would
13264 be nasty. Might be easier to properly extend generic blocks to
13265 describe ranges. */
13266 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13267 {
13268 case PC_BOUNDS_NOT_PRESENT:
13269 /* DW_TAG_lexical_block has no attributes, process its children as if
13270 there was no wrapping by that DW_TAG_lexical_block.
13271 GCC does no longer produces such DWARF since GCC r224161. */
13272 for (child_die = die->child;
13273 child_die != NULL && child_die->tag;
13274 child_die = child_die->sibling)
13275 {
13276 /* We might already be processing this DIE. This can happen
13277 in an unusual circumstance -- where a subroutine A
13278 appears lexically in another subroutine B, but A actually
13279 inlines B. The recursion is broken here, rather than in
13280 inherit_abstract_dies, because it seems better to simply
13281 drop concrete children here. */
13282 if (!child_die->in_process)
13283 process_die (child_die, cu);
13284 }
13285 return;
13286 case PC_BOUNDS_INVALID:
13287 return;
13288 }
13289 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13290 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13291
13292 cu->get_builder ()->push_context (0, lowpc);
13293 if (die->child != NULL)
13294 {
13295 child_die = die->child;
13296 while (child_die && child_die->tag)
13297 {
13298 process_die (child_die, cu);
13299 child_die = child_die->sibling;
13300 }
13301 }
13302 inherit_abstract_dies (die, cu);
13303 struct context_stack cstk = cu->get_builder ()->pop_context ();
13304
13305 if (*cu->get_builder ()->get_local_symbols () != NULL
13306 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13307 {
13308 struct block *block
13309 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13310 cstk.start_addr, highpc);
13311
13312 /* Note that recording ranges after traversing children, as we
13313 do here, means that recording a parent's ranges entails
13314 walking across all its children's ranges as they appear in
13315 the address map, which is quadratic behavior.
13316
13317 It would be nicer to record the parent's ranges before
13318 traversing its children, simply overriding whatever you find
13319 there. But since we don't even decide whether to create a
13320 block until after we've traversed its children, that's hard
13321 to do. */
13322 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13323 }
13324 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13325 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13326}
13327
13328/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13329
13330static void
13331read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13332{
13333 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13334 struct objfile *objfile = per_objfile->objfile;
13335 struct gdbarch *gdbarch = objfile->arch ();
13336 CORE_ADDR pc, baseaddr;
13337 struct attribute *attr;
13338 struct call_site *call_site, call_site_local;
13339 void **slot;
13340 int nparams;
13341 struct die_info *child_die;
13342
13343 baseaddr = objfile->text_section_offset ();
13344
13345 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13346 if (attr == NULL)
13347 {
13348 /* This was a pre-DWARF-5 GNU extension alias
13349 for DW_AT_call_return_pc. */
13350 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13351 }
13352 if (!attr)
13353 {
13354 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13355 "DIE %s [in module %s]"),
13356 sect_offset_str (die->sect_off), objfile_name (objfile));
13357 return;
13358 }
13359 pc = attr->value_as_address () + baseaddr;
13360 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13361
13362 if (cu->call_site_htab == NULL)
13363 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13364 NULL, &objfile->objfile_obstack,
13365 hashtab_obstack_allocate, NULL);
13366 call_site_local.pc = pc;
13367 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13368 if (*slot != NULL)
13369 {
13370 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13371 "DIE %s [in module %s]"),
13372 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13373 objfile_name (objfile));
13374 return;
13375 }
13376
13377 /* Count parameters at the caller. */
13378
13379 nparams = 0;
13380 for (child_die = die->child; child_die && child_die->tag;
13381 child_die = child_die->sibling)
13382 {
13383 if (child_die->tag != DW_TAG_call_site_parameter
13384 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13385 {
13386 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13387 "DW_TAG_call_site child DIE %s [in module %s]"),
13388 child_die->tag, sect_offset_str (child_die->sect_off),
13389 objfile_name (objfile));
13390 continue;
13391 }
13392
13393 nparams++;
13394 }
13395
13396 call_site
13397 = ((struct call_site *)
13398 obstack_alloc (&objfile->objfile_obstack,
13399 sizeof (*call_site)
13400 + (sizeof (*call_site->parameter) * (nparams - 1))));
13401 *slot = call_site;
13402 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13403 call_site->pc = pc;
13404
13405 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13406 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13407 {
13408 struct die_info *func_die;
13409
13410 /* Skip also over DW_TAG_inlined_subroutine. */
13411 for (func_die = die->parent;
13412 func_die && func_die->tag != DW_TAG_subprogram
13413 && func_die->tag != DW_TAG_subroutine_type;
13414 func_die = func_die->parent);
13415
13416 /* DW_AT_call_all_calls is a superset
13417 of DW_AT_call_all_tail_calls. */
13418 if (func_die
13419 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13420 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13421 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13422 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13423 {
13424 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13425 not complete. But keep CALL_SITE for look ups via call_site_htab,
13426 both the initial caller containing the real return address PC and
13427 the final callee containing the current PC of a chain of tail
13428 calls do not need to have the tail call list complete. But any
13429 function candidate for a virtual tail call frame searched via
13430 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13431 determined unambiguously. */
13432 }
13433 else
13434 {
13435 struct type *func_type = NULL;
13436
13437 if (func_die)
13438 func_type = get_die_type (func_die, cu);
13439 if (func_type != NULL)
13440 {
13441 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13442
13443 /* Enlist this call site to the function. */
13444 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13445 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13446 }
13447 else
13448 complaint (_("Cannot find function owning DW_TAG_call_site "
13449 "DIE %s [in module %s]"),
13450 sect_offset_str (die->sect_off), objfile_name (objfile));
13451 }
13452 }
13453
13454 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13455 if (attr == NULL)
13456 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13457 if (attr == NULL)
13458 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13459 if (attr == NULL)
13460 {
13461 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13462 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13463 }
13464 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13465 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13466 /* Keep NULL DWARF_BLOCK. */;
13467 else if (attr->form_is_block ())
13468 {
13469 struct dwarf2_locexpr_baton *dlbaton;
13470
13471 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13472 dlbaton->data = DW_BLOCK (attr)->data;
13473 dlbaton->size = DW_BLOCK (attr)->size;
13474 dlbaton->per_objfile = per_objfile;
13475 dlbaton->per_cu = cu->per_cu;
13476
13477 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13478 }
13479 else if (attr->form_is_ref ())
13480 {
13481 struct dwarf2_cu *target_cu = cu;
13482 struct die_info *target_die;
13483
13484 target_die = follow_die_ref (die, attr, &target_cu);
13485 gdb_assert (target_cu->per_objfile->objfile == objfile);
13486 if (die_is_declaration (target_die, target_cu))
13487 {
13488 const char *target_physname;
13489
13490 /* Prefer the mangled name; otherwise compute the demangled one. */
13491 target_physname = dw2_linkage_name (target_die, target_cu);
13492 if (target_physname == NULL)
13493 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13494 if (target_physname == NULL)
13495 complaint (_("DW_AT_call_target target DIE has invalid "
13496 "physname, for referencing DIE %s [in module %s]"),
13497 sect_offset_str (die->sect_off), objfile_name (objfile));
13498 else
13499 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13500 }
13501 else
13502 {
13503 CORE_ADDR lowpc;
13504
13505 /* DW_AT_entry_pc should be preferred. */
13506 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13507 <= PC_BOUNDS_INVALID)
13508 complaint (_("DW_AT_call_target target DIE has invalid "
13509 "low pc, for referencing DIE %s [in module %s]"),
13510 sect_offset_str (die->sect_off), objfile_name (objfile));
13511 else
13512 {
13513 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13514 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13515 }
13516 }
13517 }
13518 else
13519 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13520 "block nor reference, for DIE %s [in module %s]"),
13521 sect_offset_str (die->sect_off), objfile_name (objfile));
13522
13523 call_site->per_cu = cu->per_cu;
13524 call_site->per_objfile = per_objfile;
13525
13526 for (child_die = die->child;
13527 child_die && child_die->tag;
13528 child_die = child_die->sibling)
13529 {
13530 struct call_site_parameter *parameter;
13531 struct attribute *loc, *origin;
13532
13533 if (child_die->tag != DW_TAG_call_site_parameter
13534 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13535 {
13536 /* Already printed the complaint above. */
13537 continue;
13538 }
13539
13540 gdb_assert (call_site->parameter_count < nparams);
13541 parameter = &call_site->parameter[call_site->parameter_count];
13542
13543 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13544 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13545 register is contained in DW_AT_call_value. */
13546
13547 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13548 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13549 if (origin == NULL)
13550 {
13551 /* This was a pre-DWARF-5 GNU extension alias
13552 for DW_AT_call_parameter. */
13553 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13554 }
13555 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13556 {
13557 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13558
13559 sect_offset sect_off = origin->get_ref_die_offset ();
13560 if (!cu->header.offset_in_cu_p (sect_off))
13561 {
13562 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13563 binding can be done only inside one CU. Such referenced DIE
13564 therefore cannot be even moved to DW_TAG_partial_unit. */
13565 complaint (_("DW_AT_call_parameter offset is not in CU for "
13566 "DW_TAG_call_site child DIE %s [in module %s]"),
13567 sect_offset_str (child_die->sect_off),
13568 objfile_name (objfile));
13569 continue;
13570 }
13571 parameter->u.param_cu_off
13572 = (cu_offset) (sect_off - cu->header.sect_off);
13573 }
13574 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13575 {
13576 complaint (_("No DW_FORM_block* DW_AT_location for "
13577 "DW_TAG_call_site child DIE %s [in module %s]"),
13578 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13579 continue;
13580 }
13581 else
13582 {
13583 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13584 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13585 if (parameter->u.dwarf_reg != -1)
13586 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13587 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13588 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13589 &parameter->u.fb_offset))
13590 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13591 else
13592 {
13593 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13594 "for DW_FORM_block* DW_AT_location is supported for "
13595 "DW_TAG_call_site child DIE %s "
13596 "[in module %s]"),
13597 sect_offset_str (child_die->sect_off),
13598 objfile_name (objfile));
13599 continue;
13600 }
13601 }
13602
13603 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13604 if (attr == NULL)
13605 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13606 if (attr == NULL || !attr->form_is_block ())
13607 {
13608 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13609 "DW_TAG_call_site child DIE %s [in module %s]"),
13610 sect_offset_str (child_die->sect_off),
13611 objfile_name (objfile));
13612 continue;
13613 }
13614 parameter->value = DW_BLOCK (attr)->data;
13615 parameter->value_size = DW_BLOCK (attr)->size;
13616
13617 /* Parameters are not pre-cleared by memset above. */
13618 parameter->data_value = NULL;
13619 parameter->data_value_size = 0;
13620 call_site->parameter_count++;
13621
13622 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13623 if (attr == NULL)
13624 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13625 if (attr != nullptr)
13626 {
13627 if (!attr->form_is_block ())
13628 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13629 "DW_TAG_call_site child DIE %s [in module %s]"),
13630 sect_offset_str (child_die->sect_off),
13631 objfile_name (objfile));
13632 else
13633 {
13634 parameter->data_value = DW_BLOCK (attr)->data;
13635 parameter->data_value_size = DW_BLOCK (attr)->size;
13636 }
13637 }
13638 }
13639}
13640
13641/* Helper function for read_variable. If DIE represents a virtual
13642 table, then return the type of the concrete object that is
13643 associated with the virtual table. Otherwise, return NULL. */
13644
13645static struct type *
13646rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13647{
13648 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13649 if (attr == NULL)
13650 return NULL;
13651
13652 /* Find the type DIE. */
13653 struct die_info *type_die = NULL;
13654 struct dwarf2_cu *type_cu = cu;
13655
13656 if (attr->form_is_ref ())
13657 type_die = follow_die_ref (die, attr, &type_cu);
13658 if (type_die == NULL)
13659 return NULL;
13660
13661 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13662 return NULL;
13663 return die_containing_type (type_die, type_cu);
13664}
13665
13666/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13667
13668static void
13669read_variable (struct die_info *die, struct dwarf2_cu *cu)
13670{
13671 struct rust_vtable_symbol *storage = NULL;
13672
13673 if (cu->language == language_rust)
13674 {
13675 struct type *containing_type = rust_containing_type (die, cu);
13676
13677 if (containing_type != NULL)
13678 {
13679 struct objfile *objfile = cu->per_objfile->objfile;
13680
13681 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13682 storage->concrete_type = containing_type;
13683 storage->subclass = SYMBOL_RUST_VTABLE;
13684 }
13685 }
13686
13687 struct symbol *res = new_symbol (die, NULL, cu, storage);
13688 struct attribute *abstract_origin
13689 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13690 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13691 if (res == NULL && loc && abstract_origin)
13692 {
13693 /* We have a variable without a name, but with a location and an abstract
13694 origin. This may be a concrete instance of an abstract variable
13695 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13696 later. */
13697 struct dwarf2_cu *origin_cu = cu;
13698 struct die_info *origin_die
13699 = follow_die_ref (die, abstract_origin, &origin_cu);
13700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13701 per_objfile->per_bfd->abstract_to_concrete
13702 [origin_die->sect_off].push_back (die->sect_off);
13703 }
13704}
13705
13706/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13707 reading .debug_rnglists.
13708 Callback's type should be:
13709 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13710 Return true if the attributes are present and valid, otherwise,
13711 return false. */
13712
13713template <typename Callback>
13714static bool
13715dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13716 Callback &&callback)
13717{
13718 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13719 struct objfile *objfile = dwarf2_per_objfile->objfile;
13720 bfd *obfd = objfile->obfd;
13721 /* Base address selection entry. */
13722 gdb::optional<CORE_ADDR> base;
13723 const gdb_byte *buffer;
13724 CORE_ADDR baseaddr;
13725 bool overflow = false;
13726
13727 base = cu->base_address;
13728
13729 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13730 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13731 {
13732 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13733 offset);
13734 return false;
13735 }
13736 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13737
13738 baseaddr = objfile->text_section_offset ();
13739
13740 while (1)
13741 {
13742 /* Initialize it due to a false compiler warning. */
13743 CORE_ADDR range_beginning = 0, range_end = 0;
13744 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13745 + dwarf2_per_objfile->per_bfd->rnglists.size);
13746 unsigned int bytes_read;
13747
13748 if (buffer == buf_end)
13749 {
13750 overflow = true;
13751 break;
13752 }
13753 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13754 switch (rlet)
13755 {
13756 case DW_RLE_end_of_list:
13757 break;
13758 case DW_RLE_base_address:
13759 if (buffer + cu->header.addr_size > buf_end)
13760 {
13761 overflow = true;
13762 break;
13763 }
13764 base = cu->header.read_address (obfd, buffer, &bytes_read);
13765 buffer += bytes_read;
13766 break;
13767 case DW_RLE_start_length:
13768 if (buffer + cu->header.addr_size > buf_end)
13769 {
13770 overflow = true;
13771 break;
13772 }
13773 range_beginning = cu->header.read_address (obfd, buffer,
13774 &bytes_read);
13775 buffer += bytes_read;
13776 range_end = (range_beginning
13777 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13778 buffer += bytes_read;
13779 if (buffer > buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 break;
13785 case DW_RLE_offset_pair:
13786 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13787 buffer += bytes_read;
13788 if (buffer > buf_end)
13789 {
13790 overflow = true;
13791 break;
13792 }
13793 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13794 buffer += bytes_read;
13795 if (buffer > buf_end)
13796 {
13797 overflow = true;
13798 break;
13799 }
13800 break;
13801 case DW_RLE_start_end:
13802 if (buffer + 2 * cu->header.addr_size > buf_end)
13803 {
13804 overflow = true;
13805 break;
13806 }
13807 range_beginning = cu->header.read_address (obfd, buffer,
13808 &bytes_read);
13809 buffer += bytes_read;
13810 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13811 buffer += bytes_read;
13812 break;
13813 default:
13814 complaint (_("Invalid .debug_rnglists data (no base address)"));
13815 return false;
13816 }
13817 if (rlet == DW_RLE_end_of_list || overflow)
13818 break;
13819 if (rlet == DW_RLE_base_address)
13820 continue;
13821
13822 if (!base.has_value ())
13823 {
13824 /* We have no valid base address for the ranges
13825 data. */
13826 complaint (_("Invalid .debug_rnglists data (no base address)"));
13827 return false;
13828 }
13829
13830 if (range_beginning > range_end)
13831 {
13832 /* Inverted range entries are invalid. */
13833 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13834 return false;
13835 }
13836
13837 /* Empty range entries have no effect. */
13838 if (range_beginning == range_end)
13839 continue;
13840
13841 range_beginning += *base;
13842 range_end += *base;
13843
13844 /* A not-uncommon case of bad debug info.
13845 Don't pollute the addrmap with bad data. */
13846 if (range_beginning + baseaddr == 0
13847 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13848 {
13849 complaint (_(".debug_rnglists entry has start address of zero"
13850 " [in module %s]"), objfile_name (objfile));
13851 continue;
13852 }
13853
13854 callback (range_beginning, range_end);
13855 }
13856
13857 if (overflow)
13858 {
13859 complaint (_("Offset %d is not terminated "
13860 "for DW_AT_ranges attribute"),
13861 offset);
13862 return false;
13863 }
13864
13865 return true;
13866}
13867
13868/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13869 Callback's type should be:
13870 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13871 Return 1 if the attributes are present and valid, otherwise, return 0. */
13872
13873template <typename Callback>
13874static int
13875dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13876 Callback &&callback)
13877{
13878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13879 struct objfile *objfile = per_objfile->objfile;
13880 struct comp_unit_head *cu_header = &cu->header;
13881 bfd *obfd = objfile->obfd;
13882 unsigned int addr_size = cu_header->addr_size;
13883 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13884 /* Base address selection entry. */
13885 gdb::optional<CORE_ADDR> base;
13886 unsigned int dummy;
13887 const gdb_byte *buffer;
13888 CORE_ADDR baseaddr;
13889
13890 if (cu_header->version >= 5)
13891 return dwarf2_rnglists_process (offset, cu, callback);
13892
13893 base = cu->base_address;
13894
13895 per_objfile->per_bfd->ranges.read (objfile);
13896 if (offset >= per_objfile->per_bfd->ranges.size)
13897 {
13898 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13899 offset);
13900 return 0;
13901 }
13902 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13903
13904 baseaddr = objfile->text_section_offset ();
13905
13906 while (1)
13907 {
13908 CORE_ADDR range_beginning, range_end;
13909
13910 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13911 buffer += addr_size;
13912 range_end = cu->header.read_address (obfd, buffer, &dummy);
13913 buffer += addr_size;
13914 offset += 2 * addr_size;
13915
13916 /* An end of list marker is a pair of zero addresses. */
13917 if (range_beginning == 0 && range_end == 0)
13918 /* Found the end of list entry. */
13919 break;
13920
13921 /* Each base address selection entry is a pair of 2 values.
13922 The first is the largest possible address, the second is
13923 the base address. Check for a base address here. */
13924 if ((range_beginning & mask) == mask)
13925 {
13926 /* If we found the largest possible address, then we already
13927 have the base address in range_end. */
13928 base = range_end;
13929 continue;
13930 }
13931
13932 if (!base.has_value ())
13933 {
13934 /* We have no valid base address for the ranges
13935 data. */
13936 complaint (_("Invalid .debug_ranges data (no base address)"));
13937 return 0;
13938 }
13939
13940 if (range_beginning > range_end)
13941 {
13942 /* Inverted range entries are invalid. */
13943 complaint (_("Invalid .debug_ranges data (inverted range)"));
13944 return 0;
13945 }
13946
13947 /* Empty range entries have no effect. */
13948 if (range_beginning == range_end)
13949 continue;
13950
13951 range_beginning += *base;
13952 range_end += *base;
13953
13954 /* A not-uncommon case of bad debug info.
13955 Don't pollute the addrmap with bad data. */
13956 if (range_beginning + baseaddr == 0
13957 && !per_objfile->per_bfd->has_section_at_zero)
13958 {
13959 complaint (_(".debug_ranges entry has start address of zero"
13960 " [in module %s]"), objfile_name (objfile));
13961 continue;
13962 }
13963
13964 callback (range_beginning, range_end);
13965 }
13966
13967 return 1;
13968}
13969
13970/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13971 Return 1 if the attributes are present and valid, otherwise, return 0.
13972 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13973
13974static int
13975dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13976 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13977 dwarf2_psymtab *ranges_pst)
13978{
13979 struct objfile *objfile = cu->per_objfile->objfile;
13980 struct gdbarch *gdbarch = objfile->arch ();
13981 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13982 int low_set = 0;
13983 CORE_ADDR low = 0;
13984 CORE_ADDR high = 0;
13985 int retval;
13986
13987 retval = dwarf2_ranges_process (offset, cu,
13988 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13989 {
13990 if (ranges_pst != NULL)
13991 {
13992 CORE_ADDR lowpc;
13993 CORE_ADDR highpc;
13994
13995 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13996 range_beginning + baseaddr)
13997 - baseaddr);
13998 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13999 range_end + baseaddr)
14000 - baseaddr);
14001 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14002 lowpc, highpc - 1, ranges_pst);
14003 }
14004
14005 /* FIXME: This is recording everything as a low-high
14006 segment of consecutive addresses. We should have a
14007 data structure for discontiguous block ranges
14008 instead. */
14009 if (! low_set)
14010 {
14011 low = range_beginning;
14012 high = range_end;
14013 low_set = 1;
14014 }
14015 else
14016 {
14017 if (range_beginning < low)
14018 low = range_beginning;
14019 if (range_end > high)
14020 high = range_end;
14021 }
14022 });
14023 if (!retval)
14024 return 0;
14025
14026 if (! low_set)
14027 /* If the first entry is an end-of-list marker, the range
14028 describes an empty scope, i.e. no instructions. */
14029 return 0;
14030
14031 if (low_return)
14032 *low_return = low;
14033 if (high_return)
14034 *high_return = high;
14035 return 1;
14036}
14037
14038/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14039 definition for the return value. *LOWPC and *HIGHPC are set iff
14040 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14041
14042static enum pc_bounds_kind
14043dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14044 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14045 dwarf2_psymtab *pst)
14046{
14047 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14048 struct attribute *attr;
14049 struct attribute *attr_high;
14050 CORE_ADDR low = 0;
14051 CORE_ADDR high = 0;
14052 enum pc_bounds_kind ret;
14053
14054 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14055 if (attr_high)
14056 {
14057 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14058 if (attr != nullptr)
14059 {
14060 low = attr->value_as_address ();
14061 high = attr_high->value_as_address ();
14062 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14063 high += low;
14064 }
14065 else
14066 /* Found high w/o low attribute. */
14067 return PC_BOUNDS_INVALID;
14068
14069 /* Found consecutive range of addresses. */
14070 ret = PC_BOUNDS_HIGH_LOW;
14071 }
14072 else
14073 {
14074 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14075 if (attr != NULL)
14076 {
14077 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14078 We take advantage of the fact that DW_AT_ranges does not appear
14079 in DW_TAG_compile_unit of DWO files. */
14080 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14081 unsigned int ranges_offset = (DW_UNSND (attr)
14082 + (need_ranges_base
14083 ? cu->ranges_base
14084 : 0));
14085
14086 /* Value of the DW_AT_ranges attribute is the offset in the
14087 .debug_ranges section. */
14088 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14089 return PC_BOUNDS_INVALID;
14090 /* Found discontinuous range of addresses. */
14091 ret = PC_BOUNDS_RANGES;
14092 }
14093 else
14094 return PC_BOUNDS_NOT_PRESENT;
14095 }
14096
14097 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14098 if (high <= low)
14099 return PC_BOUNDS_INVALID;
14100
14101 /* When using the GNU linker, .gnu.linkonce. sections are used to
14102 eliminate duplicate copies of functions and vtables and such.
14103 The linker will arbitrarily choose one and discard the others.
14104 The AT_*_pc values for such functions refer to local labels in
14105 these sections. If the section from that file was discarded, the
14106 labels are not in the output, so the relocs get a value of 0.
14107 If this is a discarded function, mark the pc bounds as invalid,
14108 so that GDB will ignore it. */
14109 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14110 return PC_BOUNDS_INVALID;
14111
14112 *lowpc = low;
14113 if (highpc)
14114 *highpc = high;
14115 return ret;
14116}
14117
14118/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14119 its low and high PC addresses. Do nothing if these addresses could not
14120 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14121 and HIGHPC to the high address if greater than HIGHPC. */
14122
14123static void
14124dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14125 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14126 struct dwarf2_cu *cu)
14127{
14128 CORE_ADDR low, high;
14129 struct die_info *child = die->child;
14130
14131 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14132 {
14133 *lowpc = std::min (*lowpc, low);
14134 *highpc = std::max (*highpc, high);
14135 }
14136
14137 /* If the language does not allow nested subprograms (either inside
14138 subprograms or lexical blocks), we're done. */
14139 if (cu->language != language_ada)
14140 return;
14141
14142 /* Check all the children of the given DIE. If it contains nested
14143 subprograms, then check their pc bounds. Likewise, we need to
14144 check lexical blocks as well, as they may also contain subprogram
14145 definitions. */
14146 while (child && child->tag)
14147 {
14148 if (child->tag == DW_TAG_subprogram
14149 || child->tag == DW_TAG_lexical_block)
14150 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14151 child = child->sibling;
14152 }
14153}
14154
14155/* Get the low and high pc's represented by the scope DIE, and store
14156 them in *LOWPC and *HIGHPC. If the correct values can't be
14157 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14158
14159static void
14160get_scope_pc_bounds (struct die_info *die,
14161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14162 struct dwarf2_cu *cu)
14163{
14164 CORE_ADDR best_low = (CORE_ADDR) -1;
14165 CORE_ADDR best_high = (CORE_ADDR) 0;
14166 CORE_ADDR current_low, current_high;
14167
14168 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14169 >= PC_BOUNDS_RANGES)
14170 {
14171 best_low = current_low;
14172 best_high = current_high;
14173 }
14174 else
14175 {
14176 struct die_info *child = die->child;
14177
14178 while (child && child->tag)
14179 {
14180 switch (child->tag) {
14181 case DW_TAG_subprogram:
14182 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14183 break;
14184 case DW_TAG_namespace:
14185 case DW_TAG_module:
14186 /* FIXME: carlton/2004-01-16: Should we do this for
14187 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14188 that current GCC's always emit the DIEs corresponding
14189 to definitions of methods of classes as children of a
14190 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14191 the DIEs giving the declarations, which could be
14192 anywhere). But I don't see any reason why the
14193 standards says that they have to be there. */
14194 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14195
14196 if (current_low != ((CORE_ADDR) -1))
14197 {
14198 best_low = std::min (best_low, current_low);
14199 best_high = std::max (best_high, current_high);
14200 }
14201 break;
14202 default:
14203 /* Ignore. */
14204 break;
14205 }
14206
14207 child = child->sibling;
14208 }
14209 }
14210
14211 *lowpc = best_low;
14212 *highpc = best_high;
14213}
14214
14215/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14216 in DIE. */
14217
14218static void
14219dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14220 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14221{
14222 struct objfile *objfile = cu->per_objfile->objfile;
14223 struct gdbarch *gdbarch = objfile->arch ();
14224 struct attribute *attr;
14225 struct attribute *attr_high;
14226
14227 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14228 if (attr_high)
14229 {
14230 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14231 if (attr != nullptr)
14232 {
14233 CORE_ADDR low = attr->value_as_address ();
14234 CORE_ADDR high = attr_high->value_as_address ();
14235
14236 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14237 high += low;
14238
14239 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14240 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14241 cu->get_builder ()->record_block_range (block, low, high - 1);
14242 }
14243 }
14244
14245 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14246 if (attr != nullptr)
14247 {
14248 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14249 We take advantage of the fact that DW_AT_ranges does not appear
14250 in DW_TAG_compile_unit of DWO files. */
14251 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14252
14253 /* The value of the DW_AT_ranges attribute is the offset of the
14254 address range list in the .debug_ranges section. */
14255 unsigned long offset = (DW_UNSND (attr)
14256 + (need_ranges_base ? cu->ranges_base : 0));
14257
14258 std::vector<blockrange> blockvec;
14259 dwarf2_ranges_process (offset, cu,
14260 [&] (CORE_ADDR start, CORE_ADDR end)
14261 {
14262 start += baseaddr;
14263 end += baseaddr;
14264 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14265 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14266 cu->get_builder ()->record_block_range (block, start, end - 1);
14267 blockvec.emplace_back (start, end);
14268 });
14269
14270 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14271 }
14272}
14273
14274/* Check whether the producer field indicates either of GCC < 4.6, or the
14275 Intel C/C++ compiler, and cache the result in CU. */
14276
14277static void
14278check_producer (struct dwarf2_cu *cu)
14279{
14280 int major, minor;
14281
14282 if (cu->producer == NULL)
14283 {
14284 /* For unknown compilers expect their behavior is DWARF version
14285 compliant.
14286
14287 GCC started to support .debug_types sections by -gdwarf-4 since
14288 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14289 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14290 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14291 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14292 }
14293 else if (producer_is_gcc (cu->producer, &major, &minor))
14294 {
14295 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14296 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14297 }
14298 else if (producer_is_icc (cu->producer, &major, &minor))
14299 {
14300 cu->producer_is_icc = true;
14301 cu->producer_is_icc_lt_14 = major < 14;
14302 }
14303 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14304 cu->producer_is_codewarrior = true;
14305 else
14306 {
14307 /* For other non-GCC compilers, expect their behavior is DWARF version
14308 compliant. */
14309 }
14310
14311 cu->checked_producer = true;
14312}
14313
14314/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14315 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14316 during 4.6.0 experimental. */
14317
14318static bool
14319producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14320{
14321 if (!cu->checked_producer)
14322 check_producer (cu);
14323
14324 return cu->producer_is_gxx_lt_4_6;
14325}
14326
14327
14328/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14329 with incorrect is_stmt attributes. */
14330
14331static bool
14332producer_is_codewarrior (struct dwarf2_cu *cu)
14333{
14334 if (!cu->checked_producer)
14335 check_producer (cu);
14336
14337 return cu->producer_is_codewarrior;
14338}
14339
14340/* Return the default accessibility type if it is not overridden by
14341 DW_AT_accessibility. */
14342
14343static enum dwarf_access_attribute
14344dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14345{
14346 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14347 {
14348 /* The default DWARF 2 accessibility for members is public, the default
14349 accessibility for inheritance is private. */
14350
14351 if (die->tag != DW_TAG_inheritance)
14352 return DW_ACCESS_public;
14353 else
14354 return DW_ACCESS_private;
14355 }
14356 else
14357 {
14358 /* DWARF 3+ defines the default accessibility a different way. The same
14359 rules apply now for DW_TAG_inheritance as for the members and it only
14360 depends on the container kind. */
14361
14362 if (die->parent->tag == DW_TAG_class_type)
14363 return DW_ACCESS_private;
14364 else
14365 return DW_ACCESS_public;
14366 }
14367}
14368
14369/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14370 offset. If the attribute was not found return 0, otherwise return
14371 1. If it was found but could not properly be handled, set *OFFSET
14372 to 0. */
14373
14374static int
14375handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14376 LONGEST *offset)
14377{
14378 struct attribute *attr;
14379
14380 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14381 if (attr != NULL)
14382 {
14383 *offset = 0;
14384
14385 /* Note that we do not check for a section offset first here.
14386 This is because DW_AT_data_member_location is new in DWARF 4,
14387 so if we see it, we can assume that a constant form is really
14388 a constant and not a section offset. */
14389 if (attr->form_is_constant ())
14390 *offset = attr->constant_value (0);
14391 else if (attr->form_is_section_offset ())
14392 dwarf2_complex_location_expr_complaint ();
14393 else if (attr->form_is_block ())
14394 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14395 else
14396 dwarf2_complex_location_expr_complaint ();
14397
14398 return 1;
14399 }
14400
14401 return 0;
14402}
14403
14404/* Look for DW_AT_data_member_location and store the results in FIELD. */
14405
14406static void
14407handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14408 struct field *field)
14409{
14410 struct attribute *attr;
14411
14412 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14413 if (attr != NULL)
14414 {
14415 if (attr->form_is_constant ())
14416 {
14417 LONGEST offset = attr->constant_value (0);
14418 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14419 }
14420 else if (attr->form_is_section_offset ())
14421 dwarf2_complex_location_expr_complaint ();
14422 else if (attr->form_is_block ())
14423 {
14424 bool handled;
14425 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14426 if (handled)
14427 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14428 else
14429 {
14430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14431 struct objfile *objfile = per_objfile->objfile;
14432 struct dwarf2_locexpr_baton *dlbaton
14433 = XOBNEW (&objfile->objfile_obstack,
14434 struct dwarf2_locexpr_baton);
14435 dlbaton->data = DW_BLOCK (attr)->data;
14436 dlbaton->size = DW_BLOCK (attr)->size;
14437 /* When using this baton, we want to compute the address
14438 of the field, not the value. This is why
14439 is_reference is set to false here. */
14440 dlbaton->is_reference = false;
14441 dlbaton->per_objfile = per_objfile;
14442 dlbaton->per_cu = cu->per_cu;
14443
14444 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14445 }
14446 }
14447 else
14448 dwarf2_complex_location_expr_complaint ();
14449 }
14450}
14451
14452/* Add an aggregate field to the field list. */
14453
14454static void
14455dwarf2_add_field (struct field_info *fip, struct die_info *die,
14456 struct dwarf2_cu *cu)
14457{
14458 struct objfile *objfile = cu->per_objfile->objfile;
14459 struct gdbarch *gdbarch = objfile->arch ();
14460 struct nextfield *new_field;
14461 struct attribute *attr;
14462 struct field *fp;
14463 const char *fieldname = "";
14464
14465 if (die->tag == DW_TAG_inheritance)
14466 {
14467 fip->baseclasses.emplace_back ();
14468 new_field = &fip->baseclasses.back ();
14469 }
14470 else
14471 {
14472 fip->fields.emplace_back ();
14473 new_field = &fip->fields.back ();
14474 }
14475
14476 new_field->offset = die->sect_off;
14477
14478 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14479 if (attr != nullptr)
14480 new_field->accessibility = DW_UNSND (attr);
14481 else
14482 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14483 if (new_field->accessibility != DW_ACCESS_public)
14484 fip->non_public_fields = 1;
14485
14486 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14487 if (attr != nullptr)
14488 new_field->virtuality = DW_UNSND (attr);
14489 else
14490 new_field->virtuality = DW_VIRTUALITY_none;
14491
14492 fp = &new_field->field;
14493
14494 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14495 {
14496 /* Data member other than a C++ static data member. */
14497
14498 /* Get type of field. */
14499 fp->type = die_type (die, cu);
14500
14501 SET_FIELD_BITPOS (*fp, 0);
14502
14503 /* Get bit size of field (zero if none). */
14504 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14505 if (attr != nullptr)
14506 {
14507 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14508 }
14509 else
14510 {
14511 FIELD_BITSIZE (*fp) = 0;
14512 }
14513
14514 /* Get bit offset of field. */
14515 handle_data_member_location (die, cu, fp);
14516 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14517 if (attr != nullptr)
14518 {
14519 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14520 {
14521 /* For big endian bits, the DW_AT_bit_offset gives the
14522 additional bit offset from the MSB of the containing
14523 anonymous object to the MSB of the field. We don't
14524 have to do anything special since we don't need to
14525 know the size of the anonymous object. */
14526 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14527 }
14528 else
14529 {
14530 /* For little endian bits, compute the bit offset to the
14531 MSB of the anonymous object, subtract off the number of
14532 bits from the MSB of the field to the MSB of the
14533 object, and then subtract off the number of bits of
14534 the field itself. The result is the bit offset of
14535 the LSB of the field. */
14536 int anonymous_size;
14537 int bit_offset = DW_UNSND (attr);
14538
14539 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14540 if (attr != nullptr)
14541 {
14542 /* The size of the anonymous object containing
14543 the bit field is explicit, so use the
14544 indicated size (in bytes). */
14545 anonymous_size = DW_UNSND (attr);
14546 }
14547 else
14548 {
14549 /* The size of the anonymous object containing
14550 the bit field must be inferred from the type
14551 attribute of the data member containing the
14552 bit field. */
14553 anonymous_size = TYPE_LENGTH (fp->type);
14554 }
14555 SET_FIELD_BITPOS (*fp,
14556 (FIELD_BITPOS (*fp)
14557 + anonymous_size * bits_per_byte
14558 - bit_offset - FIELD_BITSIZE (*fp)));
14559 }
14560 }
14561 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14562 if (attr != NULL)
14563 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14564 + attr->constant_value (0)));
14565
14566 /* Get name of field. */
14567 fieldname = dwarf2_name (die, cu);
14568 if (fieldname == NULL)
14569 fieldname = "";
14570
14571 /* The name is already allocated along with this objfile, so we don't
14572 need to duplicate it for the type. */
14573 fp->name = fieldname;
14574
14575 /* Change accessibility for artificial fields (e.g. virtual table
14576 pointer or virtual base class pointer) to private. */
14577 if (dwarf2_attr (die, DW_AT_artificial, cu))
14578 {
14579 FIELD_ARTIFICIAL (*fp) = 1;
14580 new_field->accessibility = DW_ACCESS_private;
14581 fip->non_public_fields = 1;
14582 }
14583 }
14584 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14585 {
14586 /* C++ static member. */
14587
14588 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14589 is a declaration, but all versions of G++ as of this writing
14590 (so through at least 3.2.1) incorrectly generate
14591 DW_TAG_variable tags. */
14592
14593 const char *physname;
14594
14595 /* Get name of field. */
14596 fieldname = dwarf2_name (die, cu);
14597 if (fieldname == NULL)
14598 return;
14599
14600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14601 if (attr
14602 /* Only create a symbol if this is an external value.
14603 new_symbol checks this and puts the value in the global symbol
14604 table, which we want. If it is not external, new_symbol
14605 will try to put the value in cu->list_in_scope which is wrong. */
14606 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14607 {
14608 /* A static const member, not much different than an enum as far as
14609 we're concerned, except that we can support more types. */
14610 new_symbol (die, NULL, cu);
14611 }
14612
14613 /* Get physical name. */
14614 physname = dwarf2_physname (fieldname, die, cu);
14615
14616 /* The name is already allocated along with this objfile, so we don't
14617 need to duplicate it for the type. */
14618 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14619 FIELD_TYPE (*fp) = die_type (die, cu);
14620 FIELD_NAME (*fp) = fieldname;
14621 }
14622 else if (die->tag == DW_TAG_inheritance)
14623 {
14624 /* C++ base class field. */
14625 handle_data_member_location (die, cu, fp);
14626 FIELD_BITSIZE (*fp) = 0;
14627 FIELD_TYPE (*fp) = die_type (die, cu);
14628 FIELD_NAME (*fp) = fp->type->name ();
14629 }
14630 else
14631 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14632}
14633
14634/* Can the type given by DIE define another type? */
14635
14636static bool
14637type_can_define_types (const struct die_info *die)
14638{
14639 switch (die->tag)
14640 {
14641 case DW_TAG_typedef:
14642 case DW_TAG_class_type:
14643 case DW_TAG_structure_type:
14644 case DW_TAG_union_type:
14645 case DW_TAG_enumeration_type:
14646 return true;
14647
14648 default:
14649 return false;
14650 }
14651}
14652
14653/* Add a type definition defined in the scope of the FIP's class. */
14654
14655static void
14656dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14657 struct dwarf2_cu *cu)
14658{
14659 struct decl_field fp;
14660 memset (&fp, 0, sizeof (fp));
14661
14662 gdb_assert (type_can_define_types (die));
14663
14664 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14665 fp.name = dwarf2_name (die, cu);
14666 fp.type = read_type_die (die, cu);
14667
14668 /* Save accessibility. */
14669 enum dwarf_access_attribute accessibility;
14670 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14671 if (attr != NULL)
14672 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14673 else
14674 accessibility = dwarf2_default_access_attribute (die, cu);
14675 switch (accessibility)
14676 {
14677 case DW_ACCESS_public:
14678 /* The assumed value if neither private nor protected. */
14679 break;
14680 case DW_ACCESS_private:
14681 fp.is_private = 1;
14682 break;
14683 case DW_ACCESS_protected:
14684 fp.is_protected = 1;
14685 break;
14686 default:
14687 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14688 }
14689
14690 if (die->tag == DW_TAG_typedef)
14691 fip->typedef_field_list.push_back (fp);
14692 else
14693 fip->nested_types_list.push_back (fp);
14694}
14695
14696/* A convenience typedef that's used when finding the discriminant
14697 field for a variant part. */
14698typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14699 offset_map_type;
14700
14701/* Compute the discriminant range for a given variant. OBSTACK is
14702 where the results will be stored. VARIANT is the variant to
14703 process. IS_UNSIGNED indicates whether the discriminant is signed
14704 or unsigned. */
14705
14706static const gdb::array_view<discriminant_range>
14707convert_variant_range (struct obstack *obstack, const variant_field &variant,
14708 bool is_unsigned)
14709{
14710 std::vector<discriminant_range> ranges;
14711
14712 if (variant.default_branch)
14713 return {};
14714
14715 if (variant.discr_list_data == nullptr)
14716 {
14717 discriminant_range r
14718 = {variant.discriminant_value, variant.discriminant_value};
14719 ranges.push_back (r);
14720 }
14721 else
14722 {
14723 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14724 variant.discr_list_data->size);
14725 while (!data.empty ())
14726 {
14727 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14728 {
14729 complaint (_("invalid discriminant marker: %d"), data[0]);
14730 break;
14731 }
14732 bool is_range = data[0] == DW_DSC_range;
14733 data = data.slice (1);
14734
14735 ULONGEST low, high;
14736 unsigned int bytes_read;
14737
14738 if (data.empty ())
14739 {
14740 complaint (_("DW_AT_discr_list missing low value"));
14741 break;
14742 }
14743 if (is_unsigned)
14744 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14745 else
14746 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14747 &bytes_read);
14748 data = data.slice (bytes_read);
14749
14750 if (is_range)
14751 {
14752 if (data.empty ())
14753 {
14754 complaint (_("DW_AT_discr_list missing high value"));
14755 break;
14756 }
14757 if (is_unsigned)
14758 high = read_unsigned_leb128 (nullptr, data.data (),
14759 &bytes_read);
14760 else
14761 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14762 &bytes_read);
14763 data = data.slice (bytes_read);
14764 }
14765 else
14766 high = low;
14767
14768 ranges.push_back ({ low, high });
14769 }
14770 }
14771
14772 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14773 ranges.size ());
14774 std::copy (ranges.begin (), ranges.end (), result);
14775 return gdb::array_view<discriminant_range> (result, ranges.size ());
14776}
14777
14778static const gdb::array_view<variant_part> create_variant_parts
14779 (struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const std::vector<variant_part_builder> &variant_parts);
14783
14784/* Fill in a "struct variant" for a given variant field. RESULT is
14785 the variant to fill in. OBSTACK is where any needed allocations
14786 will be done. OFFSET_MAP holds the mapping from section offsets to
14787 fields for the type. FI describes the fields of the type we're
14788 processing. FIELD is the variant field we're converting. */
14789
14790static void
14791create_one_variant (variant &result, struct obstack *obstack,
14792 const offset_map_type &offset_map,
14793 struct field_info *fi, const variant_field &field)
14794{
14795 result.discriminants = convert_variant_range (obstack, field, false);
14796 result.first_field = field.first_field + fi->baseclasses.size ();
14797 result.last_field = field.last_field + fi->baseclasses.size ();
14798 result.parts = create_variant_parts (obstack, offset_map, fi,
14799 field.variant_parts);
14800}
14801
14802/* Fill in a "struct variant_part" for a given variant part. RESULT
14803 is the variant part to fill in. OBSTACK is where any needed
14804 allocations will be done. OFFSET_MAP holds the mapping from
14805 section offsets to fields for the type. FI describes the fields of
14806 the type we're processing. BUILDER is the variant part to be
14807 converted. */
14808
14809static void
14810create_one_variant_part (variant_part &result,
14811 struct obstack *obstack,
14812 const offset_map_type &offset_map,
14813 struct field_info *fi,
14814 const variant_part_builder &builder)
14815{
14816 auto iter = offset_map.find (builder.discriminant_offset);
14817 if (iter == offset_map.end ())
14818 {
14819 result.discriminant_index = -1;
14820 /* Doesn't matter. */
14821 result.is_unsigned = false;
14822 }
14823 else
14824 {
14825 result.discriminant_index = iter->second;
14826 result.is_unsigned
14827 = TYPE_UNSIGNED (FIELD_TYPE
14828 (fi->fields[result.discriminant_index].field));
14829 }
14830
14831 size_t n = builder.variants.size ();
14832 variant *output = new (obstack) variant[n];
14833 for (size_t i = 0; i < n; ++i)
14834 create_one_variant (output[i], obstack, offset_map, fi,
14835 builder.variants[i]);
14836
14837 result.variants = gdb::array_view<variant> (output, n);
14838}
14839
14840/* Create a vector of variant parts that can be attached to a type.
14841 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14842 holds the mapping from section offsets to fields for the type. FI
14843 describes the fields of the type we're processing. VARIANT_PARTS
14844 is the vector to convert. */
14845
14846static const gdb::array_view<variant_part>
14847create_variant_parts (struct obstack *obstack,
14848 const offset_map_type &offset_map,
14849 struct field_info *fi,
14850 const std::vector<variant_part_builder> &variant_parts)
14851{
14852 if (variant_parts.empty ())
14853 return {};
14854
14855 size_t n = variant_parts.size ();
14856 variant_part *result = new (obstack) variant_part[n];
14857 for (size_t i = 0; i < n; ++i)
14858 create_one_variant_part (result[i], obstack, offset_map, fi,
14859 variant_parts[i]);
14860
14861 return gdb::array_view<variant_part> (result, n);
14862}
14863
14864/* Compute the variant part vector for FIP, attaching it to TYPE when
14865 done. */
14866
14867static void
14868add_variant_property (struct field_info *fip, struct type *type,
14869 struct dwarf2_cu *cu)
14870{
14871 /* Map section offsets of fields to their field index. Note the
14872 field index here does not take the number of baseclasses into
14873 account. */
14874 offset_map_type offset_map;
14875 for (int i = 0; i < fip->fields.size (); ++i)
14876 offset_map[fip->fields[i].offset] = i;
14877
14878 struct objfile *objfile = cu->per_objfile->objfile;
14879 gdb::array_view<variant_part> parts
14880 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14881 fip->variant_parts);
14882
14883 struct dynamic_prop prop;
14884 prop.kind = PROP_VARIANT_PARTS;
14885 prop.data.variant_parts
14886 = ((gdb::array_view<variant_part> *)
14887 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14888
14889 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14890}
14891
14892/* Create the vector of fields, and attach it to the type. */
14893
14894static void
14895dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14896 struct dwarf2_cu *cu)
14897{
14898 int nfields = fip->nfields ();
14899
14900 /* Record the field count, allocate space for the array of fields,
14901 and create blank accessibility bitfields if necessary. */
14902 type->set_num_fields (nfields);
14903 type->set_fields
14904 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14905
14906 if (fip->non_public_fields && cu->language != language_ada)
14907 {
14908 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14909
14910 TYPE_FIELD_PRIVATE_BITS (type) =
14911 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14912 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14913
14914 TYPE_FIELD_PROTECTED_BITS (type) =
14915 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14916 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14917
14918 TYPE_FIELD_IGNORE_BITS (type) =
14919 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14920 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14921 }
14922
14923 /* If the type has baseclasses, allocate and clear a bit vector for
14924 TYPE_FIELD_VIRTUAL_BITS. */
14925 if (!fip->baseclasses.empty () && cu->language != language_ada)
14926 {
14927 int num_bytes = B_BYTES (fip->baseclasses.size ());
14928 unsigned char *pointer;
14929
14930 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14931 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14932 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14933 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14934 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14935 }
14936
14937 if (!fip->variant_parts.empty ())
14938 add_variant_property (fip, type, cu);
14939
14940 /* Copy the saved-up fields into the field vector. */
14941 for (int i = 0; i < nfields; ++i)
14942 {
14943 struct nextfield &field
14944 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14945 : fip->fields[i - fip->baseclasses.size ()]);
14946
14947 type->field (i) = field.field;
14948 switch (field.accessibility)
14949 {
14950 case DW_ACCESS_private:
14951 if (cu->language != language_ada)
14952 SET_TYPE_FIELD_PRIVATE (type, i);
14953 break;
14954
14955 case DW_ACCESS_protected:
14956 if (cu->language != language_ada)
14957 SET_TYPE_FIELD_PROTECTED (type, i);
14958 break;
14959
14960 case DW_ACCESS_public:
14961 break;
14962
14963 default:
14964 /* Unknown accessibility. Complain and treat it as public. */
14965 {
14966 complaint (_("unsupported accessibility %d"),
14967 field.accessibility);
14968 }
14969 break;
14970 }
14971 if (i < fip->baseclasses.size ())
14972 {
14973 switch (field.virtuality)
14974 {
14975 case DW_VIRTUALITY_virtual:
14976 case DW_VIRTUALITY_pure_virtual:
14977 if (cu->language == language_ada)
14978 error (_("unexpected virtuality in component of Ada type"));
14979 SET_TYPE_FIELD_VIRTUAL (type, i);
14980 break;
14981 }
14982 }
14983 }
14984}
14985
14986/* Return true if this member function is a constructor, false
14987 otherwise. */
14988
14989static int
14990dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14991{
14992 const char *fieldname;
14993 const char *type_name;
14994 int len;
14995
14996 if (die->parent == NULL)
14997 return 0;
14998
14999 if (die->parent->tag != DW_TAG_structure_type
15000 && die->parent->tag != DW_TAG_union_type
15001 && die->parent->tag != DW_TAG_class_type)
15002 return 0;
15003
15004 fieldname = dwarf2_name (die, cu);
15005 type_name = dwarf2_name (die->parent, cu);
15006 if (fieldname == NULL || type_name == NULL)
15007 return 0;
15008
15009 len = strlen (fieldname);
15010 return (strncmp (fieldname, type_name, len) == 0
15011 && (type_name[len] == '\0' || type_name[len] == '<'));
15012}
15013
15014/* Check if the given VALUE is a recognized enum
15015 dwarf_defaulted_attribute constant according to DWARF5 spec,
15016 Table 7.24. */
15017
15018static bool
15019is_valid_DW_AT_defaulted (ULONGEST value)
15020{
15021 switch (value)
15022 {
15023 case DW_DEFAULTED_no:
15024 case DW_DEFAULTED_in_class:
15025 case DW_DEFAULTED_out_of_class:
15026 return true;
15027 }
15028
15029 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15030 return false;
15031}
15032
15033/* Add a member function to the proper fieldlist. */
15034
15035static void
15036dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15037 struct type *type, struct dwarf2_cu *cu)
15038{
15039 struct objfile *objfile = cu->per_objfile->objfile;
15040 struct attribute *attr;
15041 int i;
15042 struct fnfieldlist *flp = nullptr;
15043 struct fn_field *fnp;
15044 const char *fieldname;
15045 struct type *this_type;
15046 enum dwarf_access_attribute accessibility;
15047
15048 if (cu->language == language_ada)
15049 error (_("unexpected member function in Ada type"));
15050
15051 /* Get name of member function. */
15052 fieldname = dwarf2_name (die, cu);
15053 if (fieldname == NULL)
15054 return;
15055
15056 /* Look up member function name in fieldlist. */
15057 for (i = 0; i < fip->fnfieldlists.size (); i++)
15058 {
15059 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15060 {
15061 flp = &fip->fnfieldlists[i];
15062 break;
15063 }
15064 }
15065
15066 /* Create a new fnfieldlist if necessary. */
15067 if (flp == nullptr)
15068 {
15069 fip->fnfieldlists.emplace_back ();
15070 flp = &fip->fnfieldlists.back ();
15071 flp->name = fieldname;
15072 i = fip->fnfieldlists.size () - 1;
15073 }
15074
15075 /* Create a new member function field and add it to the vector of
15076 fnfieldlists. */
15077 flp->fnfields.emplace_back ();
15078 fnp = &flp->fnfields.back ();
15079
15080 /* Delay processing of the physname until later. */
15081 if (cu->language == language_cplus)
15082 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15083 die, cu);
15084 else
15085 {
15086 const char *physname = dwarf2_physname (fieldname, die, cu);
15087 fnp->physname = physname ? physname : "";
15088 }
15089
15090 fnp->type = alloc_type (objfile);
15091 this_type = read_type_die (die, cu);
15092 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15093 {
15094 int nparams = this_type->num_fields ();
15095
15096 /* TYPE is the domain of this method, and THIS_TYPE is the type
15097 of the method itself (TYPE_CODE_METHOD). */
15098 smash_to_method_type (fnp->type, type,
15099 TYPE_TARGET_TYPE (this_type),
15100 this_type->fields (),
15101 this_type->num_fields (),
15102 TYPE_VARARGS (this_type));
15103
15104 /* Handle static member functions.
15105 Dwarf2 has no clean way to discern C++ static and non-static
15106 member functions. G++ helps GDB by marking the first
15107 parameter for non-static member functions (which is the this
15108 pointer) as artificial. We obtain this information from
15109 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15110 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15111 fnp->voffset = VOFFSET_STATIC;
15112 }
15113 else
15114 complaint (_("member function type missing for '%s'"),
15115 dwarf2_full_name (fieldname, die, cu));
15116
15117 /* Get fcontext from DW_AT_containing_type if present. */
15118 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15119 fnp->fcontext = die_containing_type (die, cu);
15120
15121 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15122 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15123
15124 /* Get accessibility. */
15125 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15126 if (attr != nullptr)
15127 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15128 else
15129 accessibility = dwarf2_default_access_attribute (die, cu);
15130 switch (accessibility)
15131 {
15132 case DW_ACCESS_private:
15133 fnp->is_private = 1;
15134 break;
15135 case DW_ACCESS_protected:
15136 fnp->is_protected = 1;
15137 break;
15138 }
15139
15140 /* Check for artificial methods. */
15141 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15142 if (attr && DW_UNSND (attr) != 0)
15143 fnp->is_artificial = 1;
15144
15145 /* Check for defaulted methods. */
15146 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15147 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15148 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15149
15150 /* Check for deleted methods. */
15151 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15152 if (attr != nullptr && DW_UNSND (attr) != 0)
15153 fnp->is_deleted = 1;
15154
15155 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15156
15157 /* Get index in virtual function table if it is a virtual member
15158 function. For older versions of GCC, this is an offset in the
15159 appropriate virtual table, as specified by DW_AT_containing_type.
15160 For everyone else, it is an expression to be evaluated relative
15161 to the object address. */
15162
15163 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15164 if (attr != nullptr)
15165 {
15166 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15167 {
15168 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15169 {
15170 /* Old-style GCC. */
15171 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15172 }
15173 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15174 || (DW_BLOCK (attr)->size > 1
15175 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15176 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15177 {
15178 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15179 if ((fnp->voffset % cu->header.addr_size) != 0)
15180 dwarf2_complex_location_expr_complaint ();
15181 else
15182 fnp->voffset /= cu->header.addr_size;
15183 fnp->voffset += 2;
15184 }
15185 else
15186 dwarf2_complex_location_expr_complaint ();
15187
15188 if (!fnp->fcontext)
15189 {
15190 /* If there is no `this' field and no DW_AT_containing_type,
15191 we cannot actually find a base class context for the
15192 vtable! */
15193 if (this_type->num_fields () == 0
15194 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15195 {
15196 complaint (_("cannot determine context for virtual member "
15197 "function \"%s\" (offset %s)"),
15198 fieldname, sect_offset_str (die->sect_off));
15199 }
15200 else
15201 {
15202 fnp->fcontext
15203 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15204 }
15205 }
15206 }
15207 else if (attr->form_is_section_offset ())
15208 {
15209 dwarf2_complex_location_expr_complaint ();
15210 }
15211 else
15212 {
15213 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15214 fieldname);
15215 }
15216 }
15217 else
15218 {
15219 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15220 if (attr && DW_UNSND (attr))
15221 {
15222 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15223 complaint (_("Member function \"%s\" (offset %s) is virtual "
15224 "but the vtable offset is not specified"),
15225 fieldname, sect_offset_str (die->sect_off));
15226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15227 TYPE_CPLUS_DYNAMIC (type) = 1;
15228 }
15229 }
15230}
15231
15232/* Create the vector of member function fields, and attach it to the type. */
15233
15234static void
15235dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15236 struct dwarf2_cu *cu)
15237{
15238 if (cu->language == language_ada)
15239 error (_("unexpected member functions in Ada type"));
15240
15241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15242 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15243 TYPE_ALLOC (type,
15244 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15245
15246 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15247 {
15248 struct fnfieldlist &nf = fip->fnfieldlists[i];
15249 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15250
15251 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15252 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15253 fn_flp->fn_fields = (struct fn_field *)
15254 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15255
15256 for (int k = 0; k < nf.fnfields.size (); ++k)
15257 fn_flp->fn_fields[k] = nf.fnfields[k];
15258 }
15259
15260 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15261}
15262
15263/* Returns non-zero if NAME is the name of a vtable member in CU's
15264 language, zero otherwise. */
15265static int
15266is_vtable_name (const char *name, struct dwarf2_cu *cu)
15267{
15268 static const char vptr[] = "_vptr";
15269
15270 /* Look for the C++ form of the vtable. */
15271 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15272 return 1;
15273
15274 return 0;
15275}
15276
15277/* GCC outputs unnamed structures that are really pointers to member
15278 functions, with the ABI-specified layout. If TYPE describes
15279 such a structure, smash it into a member function type.
15280
15281 GCC shouldn't do this; it should just output pointer to member DIEs.
15282 This is GCC PR debug/28767. */
15283
15284static void
15285quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15286{
15287 struct type *pfn_type, *self_type, *new_type;
15288
15289 /* Check for a structure with no name and two children. */
15290 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15291 return;
15292
15293 /* Check for __pfn and __delta members. */
15294 if (TYPE_FIELD_NAME (type, 0) == NULL
15295 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15296 || TYPE_FIELD_NAME (type, 1) == NULL
15297 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15298 return;
15299
15300 /* Find the type of the method. */
15301 pfn_type = TYPE_FIELD_TYPE (type, 0);
15302 if (pfn_type == NULL
15303 || pfn_type->code () != TYPE_CODE_PTR
15304 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15305 return;
15306
15307 /* Look for the "this" argument. */
15308 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15309 if (pfn_type->num_fields () == 0
15310 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15311 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15312 return;
15313
15314 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15315 new_type = alloc_type (objfile);
15316 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15317 pfn_type->fields (), pfn_type->num_fields (),
15318 TYPE_VARARGS (pfn_type));
15319 smash_to_methodptr_type (type, new_type);
15320}
15321
15322/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15323 appropriate error checking and issuing complaints if there is a
15324 problem. */
15325
15326static ULONGEST
15327get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15328{
15329 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15330
15331 if (attr == nullptr)
15332 return 0;
15333
15334 if (!attr->form_is_constant ())
15335 {
15336 complaint (_("DW_AT_alignment must have constant form"
15337 " - DIE at %s [in module %s]"),
15338 sect_offset_str (die->sect_off),
15339 objfile_name (cu->per_objfile->objfile));
15340 return 0;
15341 }
15342
15343 ULONGEST align;
15344 if (attr->form == DW_FORM_sdata)
15345 {
15346 LONGEST val = DW_SND (attr);
15347 if (val < 0)
15348 {
15349 complaint (_("DW_AT_alignment value must not be negative"
15350 " - DIE at %s [in module %s]"),
15351 sect_offset_str (die->sect_off),
15352 objfile_name (cu->per_objfile->objfile));
15353 return 0;
15354 }
15355 align = val;
15356 }
15357 else
15358 align = DW_UNSND (attr);
15359
15360 if (align == 0)
15361 {
15362 complaint (_("DW_AT_alignment value must not be zero"
15363 " - DIE at %s [in module %s]"),
15364 sect_offset_str (die->sect_off),
15365 objfile_name (cu->per_objfile->objfile));
15366 return 0;
15367 }
15368 if ((align & (align - 1)) != 0)
15369 {
15370 complaint (_("DW_AT_alignment value must be a power of 2"
15371 " - DIE at %s [in module %s]"),
15372 sect_offset_str (die->sect_off),
15373 objfile_name (cu->per_objfile->objfile));
15374 return 0;
15375 }
15376
15377 return align;
15378}
15379
15380/* If the DIE has a DW_AT_alignment attribute, use its value to set
15381 the alignment for TYPE. */
15382
15383static void
15384maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15385 struct type *type)
15386{
15387 if (!set_type_align (type, get_alignment (cu, die)))
15388 complaint (_("DW_AT_alignment value too large"
15389 " - DIE at %s [in module %s]"),
15390 sect_offset_str (die->sect_off),
15391 objfile_name (cu->per_objfile->objfile));
15392}
15393
15394/* Check if the given VALUE is a valid enum dwarf_calling_convention
15395 constant for a type, according to DWARF5 spec, Table 5.5. */
15396
15397static bool
15398is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15399{
15400 switch (value)
15401 {
15402 case DW_CC_normal:
15403 case DW_CC_pass_by_reference:
15404 case DW_CC_pass_by_value:
15405 return true;
15406
15407 default:
15408 complaint (_("unrecognized DW_AT_calling_convention value "
15409 "(%s) for a type"), pulongest (value));
15410 return false;
15411 }
15412}
15413
15414/* Check if the given VALUE is a valid enum dwarf_calling_convention
15415 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15416 also according to GNU-specific values (see include/dwarf2.h). */
15417
15418static bool
15419is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15420{
15421 switch (value)
15422 {
15423 case DW_CC_normal:
15424 case DW_CC_program:
15425 case DW_CC_nocall:
15426 return true;
15427
15428 case DW_CC_GNU_renesas_sh:
15429 case DW_CC_GNU_borland_fastcall_i386:
15430 case DW_CC_GDB_IBM_OpenCL:
15431 return true;
15432
15433 default:
15434 complaint (_("unrecognized DW_AT_calling_convention value "
15435 "(%s) for a subroutine"), pulongest (value));
15436 return false;
15437 }
15438}
15439
15440/* Called when we find the DIE that starts a structure or union scope
15441 (definition) to create a type for the structure or union. Fill in
15442 the type's name and general properties; the members will not be
15443 processed until process_structure_scope. A symbol table entry for
15444 the type will also not be done until process_structure_scope (assuming
15445 the type has a name).
15446
15447 NOTE: we need to call these functions regardless of whether or not the
15448 DIE has a DW_AT_name attribute, since it might be an anonymous
15449 structure or union. This gets the type entered into our set of
15450 user defined types. */
15451
15452static struct type *
15453read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15454{
15455 struct objfile *objfile = cu->per_objfile->objfile;
15456 struct type *type;
15457 struct attribute *attr;
15458 const char *name;
15459
15460 /* If the definition of this type lives in .debug_types, read that type.
15461 Don't follow DW_AT_specification though, that will take us back up
15462 the chain and we want to go down. */
15463 attr = die->attr (DW_AT_signature);
15464 if (attr != nullptr)
15465 {
15466 type = get_DW_AT_signature_type (die, attr, cu);
15467
15468 /* The type's CU may not be the same as CU.
15469 Ensure TYPE is recorded with CU in die_type_hash. */
15470 return set_die_type (die, type, cu);
15471 }
15472
15473 type = alloc_type (objfile);
15474 INIT_CPLUS_SPECIFIC (type);
15475
15476 name = dwarf2_name (die, cu);
15477 if (name != NULL)
15478 {
15479 if (cu->language == language_cplus
15480 || cu->language == language_d
15481 || cu->language == language_rust)
15482 {
15483 const char *full_name = dwarf2_full_name (name, die, cu);
15484
15485 /* dwarf2_full_name might have already finished building the DIE's
15486 type. If so, there is no need to continue. */
15487 if (get_die_type (die, cu) != NULL)
15488 return get_die_type (die, cu);
15489
15490 type->set_name (full_name);
15491 }
15492 else
15493 {
15494 /* The name is already allocated along with this objfile, so
15495 we don't need to duplicate it for the type. */
15496 type->set_name (name);
15497 }
15498 }
15499
15500 if (die->tag == DW_TAG_structure_type)
15501 {
15502 type->set_code (TYPE_CODE_STRUCT);
15503 }
15504 else if (die->tag == DW_TAG_union_type)
15505 {
15506 type->set_code (TYPE_CODE_UNION);
15507 }
15508 else
15509 {
15510 type->set_code (TYPE_CODE_STRUCT);
15511 }
15512
15513 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15514 TYPE_DECLARED_CLASS (type) = 1;
15515
15516 /* Store the calling convention in the type if it's available in
15517 the die. Otherwise the calling convention remains set to
15518 the default value DW_CC_normal. */
15519 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15520 if (attr != nullptr
15521 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15522 {
15523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15524 TYPE_CPLUS_CALLING_CONVENTION (type)
15525 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15526 }
15527
15528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15529 if (attr != nullptr)
15530 {
15531 if (attr->form_is_constant ())
15532 TYPE_LENGTH (type) = DW_UNSND (attr);
15533 else
15534 {
15535 struct dynamic_prop prop;
15536 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15537 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15538 TYPE_LENGTH (type) = 0;
15539 }
15540 }
15541 else
15542 {
15543 TYPE_LENGTH (type) = 0;
15544 }
15545
15546 maybe_set_alignment (cu, die, type);
15547
15548 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15549 {
15550 /* ICC<14 does not output the required DW_AT_declaration on
15551 incomplete types, but gives them a size of zero. */
15552 TYPE_STUB (type) = 1;
15553 }
15554 else
15555 TYPE_STUB_SUPPORTED (type) = 1;
15556
15557 if (die_is_declaration (die, cu))
15558 TYPE_STUB (type) = 1;
15559 else if (attr == NULL && die->child == NULL
15560 && producer_is_realview (cu->producer))
15561 /* RealView does not output the required DW_AT_declaration
15562 on incomplete types. */
15563 TYPE_STUB (type) = 1;
15564
15565 /* We need to add the type field to the die immediately so we don't
15566 infinitely recurse when dealing with pointers to the structure
15567 type within the structure itself. */
15568 set_die_type (die, type, cu);
15569
15570 /* set_die_type should be already done. */
15571 set_descriptive_type (type, die, cu);
15572
15573 return type;
15574}
15575
15576static void handle_struct_member_die
15577 (struct die_info *child_die,
15578 struct type *type,
15579 struct field_info *fi,
15580 std::vector<struct symbol *> *template_args,
15581 struct dwarf2_cu *cu);
15582
15583/* A helper for handle_struct_member_die that handles
15584 DW_TAG_variant_part. */
15585
15586static void
15587handle_variant_part (struct die_info *die, struct type *type,
15588 struct field_info *fi,
15589 std::vector<struct symbol *> *template_args,
15590 struct dwarf2_cu *cu)
15591{
15592 variant_part_builder *new_part;
15593 if (fi->current_variant_part == nullptr)
15594 {
15595 fi->variant_parts.emplace_back ();
15596 new_part = &fi->variant_parts.back ();
15597 }
15598 else if (!fi->current_variant_part->processing_variant)
15599 {
15600 complaint (_("nested DW_TAG_variant_part seen "
15601 "- DIE at %s [in module %s]"),
15602 sect_offset_str (die->sect_off),
15603 objfile_name (cu->per_objfile->objfile));
15604 return;
15605 }
15606 else
15607 {
15608 variant_field &current = fi->current_variant_part->variants.back ();
15609 current.variant_parts.emplace_back ();
15610 new_part = &current.variant_parts.back ();
15611 }
15612
15613 /* When we recurse, we want callees to add to this new variant
15614 part. */
15615 scoped_restore save_current_variant_part
15616 = make_scoped_restore (&fi->current_variant_part, new_part);
15617
15618 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15619 if (discr == NULL)
15620 {
15621 /* It's a univariant form, an extension we support. */
15622 }
15623 else if (discr->form_is_ref ())
15624 {
15625 struct dwarf2_cu *target_cu = cu;
15626 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15627
15628 new_part->discriminant_offset = target_die->sect_off;
15629 }
15630 else
15631 {
15632 complaint (_("DW_AT_discr does not have DIE reference form"
15633 " - DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
15635 objfile_name (cu->per_objfile->objfile));
15636 }
15637
15638 for (die_info *child_die = die->child;
15639 child_die != NULL;
15640 child_die = child_die->sibling)
15641 handle_struct_member_die (child_die, type, fi, template_args, cu);
15642}
15643
15644/* A helper for handle_struct_member_die that handles
15645 DW_TAG_variant. */
15646
15647static void
15648handle_variant (struct die_info *die, struct type *type,
15649 struct field_info *fi,
15650 std::vector<struct symbol *> *template_args,
15651 struct dwarf2_cu *cu)
15652{
15653 if (fi->current_variant_part == nullptr)
15654 {
15655 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15656 "- DIE at %s [in module %s]"),
15657 sect_offset_str (die->sect_off),
15658 objfile_name (cu->per_objfile->objfile));
15659 return;
15660 }
15661 if (fi->current_variant_part->processing_variant)
15662 {
15663 complaint (_("nested DW_TAG_variant seen "
15664 "- DIE at %s [in module %s]"),
15665 sect_offset_str (die->sect_off),
15666 objfile_name (cu->per_objfile->objfile));
15667 return;
15668 }
15669
15670 scoped_restore save_processing_variant
15671 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15672 true);
15673
15674 fi->current_variant_part->variants.emplace_back ();
15675 variant_field &variant = fi->current_variant_part->variants.back ();
15676 variant.first_field = fi->fields.size ();
15677
15678 /* In a variant we want to get the discriminant and also add a
15679 field for our sole member child. */
15680 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15681 if (discr == nullptr)
15682 {
15683 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15684 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15685 variant.default_branch = true;
15686 else
15687 variant.discr_list_data = DW_BLOCK (discr);
15688 }
15689 else
15690 variant.discriminant_value = DW_UNSND (discr);
15691
15692 for (die_info *variant_child = die->child;
15693 variant_child != NULL;
15694 variant_child = variant_child->sibling)
15695 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15696
15697 variant.last_field = fi->fields.size ();
15698}
15699
15700/* A helper for process_structure_scope that handles a single member
15701 DIE. */
15702
15703static void
15704handle_struct_member_die (struct die_info *child_die, struct type *type,
15705 struct field_info *fi,
15706 std::vector<struct symbol *> *template_args,
15707 struct dwarf2_cu *cu)
15708{
15709 if (child_die->tag == DW_TAG_member
15710 || child_die->tag == DW_TAG_variable)
15711 {
15712 /* NOTE: carlton/2002-11-05: A C++ static data member
15713 should be a DW_TAG_member that is a declaration, but
15714 all versions of G++ as of this writing (so through at
15715 least 3.2.1) incorrectly generate DW_TAG_variable
15716 tags for them instead. */
15717 dwarf2_add_field (fi, child_die, cu);
15718 }
15719 else if (child_die->tag == DW_TAG_subprogram)
15720 {
15721 /* Rust doesn't have member functions in the C++ sense.
15722 However, it does emit ordinary functions as children
15723 of a struct DIE. */
15724 if (cu->language == language_rust)
15725 read_func_scope (child_die, cu);
15726 else
15727 {
15728 /* C++ member function. */
15729 dwarf2_add_member_fn (fi, child_die, type, cu);
15730 }
15731 }
15732 else if (child_die->tag == DW_TAG_inheritance)
15733 {
15734 /* C++ base class field. */
15735 dwarf2_add_field (fi, child_die, cu);
15736 }
15737 else if (type_can_define_types (child_die))
15738 dwarf2_add_type_defn (fi, child_die, cu);
15739 else if (child_die->tag == DW_TAG_template_type_param
15740 || child_die->tag == DW_TAG_template_value_param)
15741 {
15742 struct symbol *arg = new_symbol (child_die, NULL, cu);
15743
15744 if (arg != NULL)
15745 template_args->push_back (arg);
15746 }
15747 else if (child_die->tag == DW_TAG_variant_part)
15748 handle_variant_part (child_die, type, fi, template_args, cu);
15749 else if (child_die->tag == DW_TAG_variant)
15750 handle_variant (child_die, type, fi, template_args, cu);
15751}
15752
15753/* Finish creating a structure or union type, including filling in
15754 its members and creating a symbol for it. */
15755
15756static void
15757process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15758{
15759 struct objfile *objfile = cu->per_objfile->objfile;
15760 struct die_info *child_die;
15761 struct type *type;
15762
15763 type = get_die_type (die, cu);
15764 if (type == NULL)
15765 type = read_structure_type (die, cu);
15766
15767 bool has_template_parameters = false;
15768 if (die->child != NULL && ! die_is_declaration (die, cu))
15769 {
15770 struct field_info fi;
15771 std::vector<struct symbol *> template_args;
15772
15773 child_die = die->child;
15774
15775 while (child_die && child_die->tag)
15776 {
15777 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15778 child_die = child_die->sibling;
15779 }
15780
15781 /* Attach template arguments to type. */
15782 if (!template_args.empty ())
15783 {
15784 has_template_parameters = true;
15785 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15786 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15787 TYPE_TEMPLATE_ARGUMENTS (type)
15788 = XOBNEWVEC (&objfile->objfile_obstack,
15789 struct symbol *,
15790 TYPE_N_TEMPLATE_ARGUMENTS (type));
15791 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15792 template_args.data (),
15793 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15794 * sizeof (struct symbol *)));
15795 }
15796
15797 /* Attach fields and member functions to the type. */
15798 if (fi.nfields () > 0)
15799 dwarf2_attach_fields_to_type (&fi, type, cu);
15800 if (!fi.fnfieldlists.empty ())
15801 {
15802 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15803
15804 /* Get the type which refers to the base class (possibly this
15805 class itself) which contains the vtable pointer for the current
15806 class from the DW_AT_containing_type attribute. This use of
15807 DW_AT_containing_type is a GNU extension. */
15808
15809 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15810 {
15811 struct type *t = die_containing_type (die, cu);
15812
15813 set_type_vptr_basetype (type, t);
15814 if (type == t)
15815 {
15816 int i;
15817
15818 /* Our own class provides vtbl ptr. */
15819 for (i = t->num_fields () - 1;
15820 i >= TYPE_N_BASECLASSES (t);
15821 --i)
15822 {
15823 const char *fieldname = TYPE_FIELD_NAME (t, i);
15824
15825 if (is_vtable_name (fieldname, cu))
15826 {
15827 set_type_vptr_fieldno (type, i);
15828 break;
15829 }
15830 }
15831
15832 /* Complain if virtual function table field not found. */
15833 if (i < TYPE_N_BASECLASSES (t))
15834 complaint (_("virtual function table pointer "
15835 "not found when defining class '%s'"),
15836 type->name () ? type->name () : "");
15837 }
15838 else
15839 {
15840 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15841 }
15842 }
15843 else if (cu->producer
15844 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15845 {
15846 /* The IBM XLC compiler does not provide direct indication
15847 of the containing type, but the vtable pointer is
15848 always named __vfp. */
15849
15850 int i;
15851
15852 for (i = type->num_fields () - 1;
15853 i >= TYPE_N_BASECLASSES (type);
15854 --i)
15855 {
15856 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15857 {
15858 set_type_vptr_fieldno (type, i);
15859 set_type_vptr_basetype (type, type);
15860 break;
15861 }
15862 }
15863 }
15864 }
15865
15866 /* Copy fi.typedef_field_list linked list elements content into the
15867 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15868 if (!fi.typedef_field_list.empty ())
15869 {
15870 int count = fi.typedef_field_list.size ();
15871
15872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15873 TYPE_TYPEDEF_FIELD_ARRAY (type)
15874 = ((struct decl_field *)
15875 TYPE_ALLOC (type,
15876 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15877 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15878
15879 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15880 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15881 }
15882
15883 /* Copy fi.nested_types_list linked list elements content into the
15884 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15885 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15886 {
15887 int count = fi.nested_types_list.size ();
15888
15889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15890 TYPE_NESTED_TYPES_ARRAY (type)
15891 = ((struct decl_field *)
15892 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15893 TYPE_NESTED_TYPES_COUNT (type) = count;
15894
15895 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15896 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15897 }
15898 }
15899
15900 quirk_gcc_member_function_pointer (type, objfile);
15901 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15902 cu->rust_unions.push_back (type);
15903
15904 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15905 snapshots) has been known to create a die giving a declaration
15906 for a class that has, as a child, a die giving a definition for a
15907 nested class. So we have to process our children even if the
15908 current die is a declaration. Normally, of course, a declaration
15909 won't have any children at all. */
15910
15911 child_die = die->child;
15912
15913 while (child_die != NULL && child_die->tag)
15914 {
15915 if (child_die->tag == DW_TAG_member
15916 || child_die->tag == DW_TAG_variable
15917 || child_die->tag == DW_TAG_inheritance
15918 || child_die->tag == DW_TAG_template_value_param
15919 || child_die->tag == DW_TAG_template_type_param)
15920 {
15921 /* Do nothing. */
15922 }
15923 else
15924 process_die (child_die, cu);
15925
15926 child_die = child_die->sibling;
15927 }
15928
15929 /* Do not consider external references. According to the DWARF standard,
15930 these DIEs are identified by the fact that they have no byte_size
15931 attribute, and a declaration attribute. */
15932 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15933 || !die_is_declaration (die, cu)
15934 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15935 {
15936 struct symbol *sym = new_symbol (die, type, cu);
15937
15938 if (has_template_parameters)
15939 {
15940 struct symtab *symtab;
15941 if (sym != nullptr)
15942 symtab = symbol_symtab (sym);
15943 else if (cu->line_header != nullptr)
15944 {
15945 /* Any related symtab will do. */
15946 symtab
15947 = cu->line_header->file_names ()[0].symtab;
15948 }
15949 else
15950 {
15951 symtab = nullptr;
15952 complaint (_("could not find suitable "
15953 "symtab for template parameter"
15954 " - DIE at %s [in module %s]"),
15955 sect_offset_str (die->sect_off),
15956 objfile_name (objfile));
15957 }
15958
15959 if (symtab != nullptr)
15960 {
15961 /* Make sure that the symtab is set on the new symbols.
15962 Even though they don't appear in this symtab directly,
15963 other parts of gdb assume that symbols do, and this is
15964 reasonably true. */
15965 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15966 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15967 }
15968 }
15969 }
15970}
15971
15972/* Assuming DIE is an enumeration type, and TYPE is its associated
15973 type, update TYPE using some information only available in DIE's
15974 children. In particular, the fields are computed. */
15975
15976static void
15977update_enumeration_type_from_children (struct die_info *die,
15978 struct type *type,
15979 struct dwarf2_cu *cu)
15980{
15981 struct die_info *child_die;
15982 int unsigned_enum = 1;
15983 int flag_enum = 1;
15984
15985 auto_obstack obstack;
15986 std::vector<struct field> fields;
15987
15988 for (child_die = die->child;
15989 child_die != NULL && child_die->tag;
15990 child_die = child_die->sibling)
15991 {
15992 struct attribute *attr;
15993 LONGEST value;
15994 const gdb_byte *bytes;
15995 struct dwarf2_locexpr_baton *baton;
15996 const char *name;
15997
15998 if (child_die->tag != DW_TAG_enumerator)
15999 continue;
16000
16001 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16002 if (attr == NULL)
16003 continue;
16004
16005 name = dwarf2_name (child_die, cu);
16006 if (name == NULL)
16007 name = "<anonymous enumerator>";
16008
16009 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16010 &value, &bytes, &baton);
16011 if (value < 0)
16012 {
16013 unsigned_enum = 0;
16014 flag_enum = 0;
16015 }
16016 else
16017 {
16018 if (count_one_bits_ll (value) >= 2)
16019 flag_enum = 0;
16020 }
16021
16022 fields.emplace_back ();
16023 struct field &field = fields.back ();
16024 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16025 SET_FIELD_ENUMVAL (field, value);
16026 }
16027
16028 if (!fields.empty ())
16029 {
16030 type->set_num_fields (fields.size ());
16031 type->set_fields
16032 ((struct field *)
16033 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16034 memcpy (type->fields (), fields.data (),
16035 sizeof (struct field) * fields.size ());
16036 }
16037
16038 if (unsigned_enum)
16039 TYPE_UNSIGNED (type) = 1;
16040 if (flag_enum)
16041 TYPE_FLAG_ENUM (type) = 1;
16042}
16043
16044/* Given a DW_AT_enumeration_type die, set its type. We do not
16045 complete the type's fields yet, or create any symbols. */
16046
16047static struct type *
16048read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16049{
16050 struct objfile *objfile = cu->per_objfile->objfile;
16051 struct type *type;
16052 struct attribute *attr;
16053 const char *name;
16054
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
16058 attr = die->attr (DW_AT_signature);
16059 if (attr != nullptr)
16060 {
16061 type = get_DW_AT_signature_type (die, attr, cu);
16062
16063 /* The type's CU may not be the same as CU.
16064 Ensure TYPE is recorded with CU in die_type_hash. */
16065 return set_die_type (die, type, cu);
16066 }
16067
16068 type = alloc_type (objfile);
16069
16070 type->set_code (TYPE_CODE_ENUM);
16071 name = dwarf2_full_name (NULL, die, cu);
16072 if (name != NULL)
16073 type->set_name (name);
16074
16075 attr = dwarf2_attr (die, DW_AT_type, cu);
16076 if (attr != NULL)
16077 {
16078 struct type *underlying_type = die_type (die, cu);
16079
16080 TYPE_TARGET_TYPE (type) = underlying_type;
16081 }
16082
16083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16084 if (attr != nullptr)
16085 {
16086 TYPE_LENGTH (type) = DW_UNSND (attr);
16087 }
16088 else
16089 {
16090 TYPE_LENGTH (type) = 0;
16091 }
16092
16093 maybe_set_alignment (cu, die, type);
16094
16095 /* The enumeration DIE can be incomplete. In Ada, any type can be
16096 declared as private in the package spec, and then defined only
16097 inside the package body. Such types are known as Taft Amendment
16098 Types. When another package uses such a type, an incomplete DIE
16099 may be generated by the compiler. */
16100 if (die_is_declaration (die, cu))
16101 TYPE_STUB (type) = 1;
16102
16103 /* If this type has an underlying type that is not a stub, then we
16104 may use its attributes. We always use the "unsigned" attribute
16105 in this situation, because ordinarily we guess whether the type
16106 is unsigned -- but the guess can be wrong and the underlying type
16107 can tell us the reality. However, we defer to a local size
16108 attribute if one exists, because this lets the compiler override
16109 the underlying type if needed. */
16110 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16111 {
16112 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16113 underlying_type = check_typedef (underlying_type);
16114 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16115 if (TYPE_LENGTH (type) == 0)
16116 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16117 if (TYPE_RAW_ALIGN (type) == 0
16118 && TYPE_RAW_ALIGN (underlying_type) != 0)
16119 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16120 }
16121
16122 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16123
16124 set_die_type (die, type, cu);
16125
16126 /* Finish the creation of this type by using the enum's children.
16127 Note that, as usual, this must come after set_die_type to avoid
16128 infinite recursion when trying to compute the names of the
16129 enumerators. */
16130 update_enumeration_type_from_children (die, type, cu);
16131
16132 return type;
16133}
16134
16135/* Given a pointer to a die which begins an enumeration, process all
16136 the dies that define the members of the enumeration, and create the
16137 symbol for the enumeration type.
16138
16139 NOTE: We reverse the order of the element list. */
16140
16141static void
16142process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16143{
16144 struct type *this_type;
16145
16146 this_type = get_die_type (die, cu);
16147 if (this_type == NULL)
16148 this_type = read_enumeration_type (die, cu);
16149
16150 if (die->child != NULL)
16151 {
16152 struct die_info *child_die;
16153 const char *name;
16154
16155 child_die = die->child;
16156 while (child_die && child_die->tag)
16157 {
16158 if (child_die->tag != DW_TAG_enumerator)
16159 {
16160 process_die (child_die, cu);
16161 }
16162 else
16163 {
16164 name = dwarf2_name (child_die, cu);
16165 if (name)
16166 new_symbol (child_die, this_type, cu);
16167 }
16168
16169 child_die = child_die->sibling;
16170 }
16171 }
16172
16173 /* If we are reading an enum from a .debug_types unit, and the enum
16174 is a declaration, and the enum is not the signatured type in the
16175 unit, then we do not want to add a symbol for it. Adding a
16176 symbol would in some cases obscure the true definition of the
16177 enum, giving users an incomplete type when the definition is
16178 actually available. Note that we do not want to do this for all
16179 enums which are just declarations, because C++0x allows forward
16180 enum declarations. */
16181 if (cu->per_cu->is_debug_types
16182 && die_is_declaration (die, cu))
16183 {
16184 struct signatured_type *sig_type;
16185
16186 sig_type = (struct signatured_type *) cu->per_cu;
16187 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16188 if (sig_type->type_offset_in_section != die->sect_off)
16189 return;
16190 }
16191
16192 new_symbol (die, this_type, cu);
16193}
16194
16195/* Extract all information from a DW_TAG_array_type DIE and put it in
16196 the DIE's type field. For now, this only handles one dimensional
16197 arrays. */
16198
16199static struct type *
16200read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16201{
16202 struct objfile *objfile = cu->per_objfile->objfile;
16203 struct die_info *child_die;
16204 struct type *type;
16205 struct type *element_type, *range_type, *index_type;
16206 struct attribute *attr;
16207 const char *name;
16208 struct dynamic_prop *byte_stride_prop = NULL;
16209 unsigned int bit_stride = 0;
16210
16211 element_type = die_type (die, cu);
16212
16213 /* The die_type call above may have already set the type for this DIE. */
16214 type = get_die_type (die, cu);
16215 if (type)
16216 return type;
16217
16218 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16219 if (attr != NULL)
16220 {
16221 int stride_ok;
16222 struct type *prop_type = cu->addr_sized_int_type (false);
16223
16224 byte_stride_prop
16225 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16226 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16227 prop_type);
16228 if (!stride_ok)
16229 {
16230 complaint (_("unable to read array DW_AT_byte_stride "
16231 " - DIE at %s [in module %s]"),
16232 sect_offset_str (die->sect_off),
16233 objfile_name (cu->per_objfile->objfile));
16234 /* Ignore this attribute. We will likely not be able to print
16235 arrays of this type correctly, but there is little we can do
16236 to help if we cannot read the attribute's value. */
16237 byte_stride_prop = NULL;
16238 }
16239 }
16240
16241 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16242 if (attr != NULL)
16243 bit_stride = DW_UNSND (attr);
16244
16245 /* Irix 6.2 native cc creates array types without children for
16246 arrays with unspecified length. */
16247 if (die->child == NULL)
16248 {
16249 index_type = objfile_type (objfile)->builtin_int;
16250 range_type = create_static_range_type (NULL, index_type, 0, -1);
16251 type = create_array_type_with_stride (NULL, element_type, range_type,
16252 byte_stride_prop, bit_stride);
16253 return set_die_type (die, type, cu);
16254 }
16255
16256 std::vector<struct type *> range_types;
16257 child_die = die->child;
16258 while (child_die && child_die->tag)
16259 {
16260 if (child_die->tag == DW_TAG_subrange_type)
16261 {
16262 struct type *child_type = read_type_die (child_die, cu);
16263
16264 if (child_type != NULL)
16265 {
16266 /* The range type was succesfully read. Save it for the
16267 array type creation. */
16268 range_types.push_back (child_type);
16269 }
16270 }
16271 child_die = child_die->sibling;
16272 }
16273
16274 /* Dwarf2 dimensions are output from left to right, create the
16275 necessary array types in backwards order. */
16276
16277 type = element_type;
16278
16279 if (read_array_order (die, cu) == DW_ORD_col_major)
16280 {
16281 int i = 0;
16282
16283 while (i < range_types.size ())
16284 type = create_array_type_with_stride (NULL, type, range_types[i++],
16285 byte_stride_prop, bit_stride);
16286 }
16287 else
16288 {
16289 size_t ndim = range_types.size ();
16290 while (ndim-- > 0)
16291 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16292 byte_stride_prop, bit_stride);
16293 }
16294
16295 /* Understand Dwarf2 support for vector types (like they occur on
16296 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16297 array type. This is not part of the Dwarf2/3 standard yet, but a
16298 custom vendor extension. The main difference between a regular
16299 array and the vector variant is that vectors are passed by value
16300 to functions. */
16301 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16302 if (attr != nullptr)
16303 make_vector_type (type);
16304
16305 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16306 implementation may choose to implement triple vectors using this
16307 attribute. */
16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16309 if (attr != nullptr)
16310 {
16311 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16312 TYPE_LENGTH (type) = DW_UNSND (attr);
16313 else
16314 complaint (_("DW_AT_byte_size for array type smaller "
16315 "than the total size of elements"));
16316 }
16317
16318 name = dwarf2_name (die, cu);
16319 if (name)
16320 type->set_name (name);
16321
16322 maybe_set_alignment (cu, die, type);
16323
16324 /* Install the type in the die. */
16325 set_die_type (die, type, cu);
16326
16327 /* set_die_type should be already done. */
16328 set_descriptive_type (type, die, cu);
16329
16330 return type;
16331}
16332
16333static enum dwarf_array_dim_ordering
16334read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16335{
16336 struct attribute *attr;
16337
16338 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16339
16340 if (attr != nullptr)
16341 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16342
16343 /* GNU F77 is a special case, as at 08/2004 array type info is the
16344 opposite order to the dwarf2 specification, but data is still
16345 laid out as per normal fortran.
16346
16347 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16348 version checking. */
16349
16350 if (cu->language == language_fortran
16351 && cu->producer && strstr (cu->producer, "GNU F77"))
16352 {
16353 return DW_ORD_row_major;
16354 }
16355
16356 switch (cu->language_defn->la_array_ordering)
16357 {
16358 case array_column_major:
16359 return DW_ORD_col_major;
16360 case array_row_major:
16361 default:
16362 return DW_ORD_row_major;
16363 };
16364}
16365
16366/* Extract all information from a DW_TAG_set_type DIE and put it in
16367 the DIE's type field. */
16368
16369static struct type *
16370read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16371{
16372 struct type *domain_type, *set_type;
16373 struct attribute *attr;
16374
16375 domain_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 set_type = get_die_type (die, cu);
16379 if (set_type)
16380 return set_type;
16381
16382 set_type = create_set_type (NULL, domain_type);
16383
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16385 if (attr != nullptr)
16386 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16387
16388 maybe_set_alignment (cu, die, set_type);
16389
16390 return set_die_type (die, set_type, cu);
16391}
16392
16393/* A helper for read_common_block that creates a locexpr baton.
16394 SYM is the symbol which we are marking as computed.
16395 COMMON_DIE is the DIE for the common block.
16396 COMMON_LOC is the location expression attribute for the common
16397 block itself.
16398 MEMBER_LOC is the location expression attribute for the particular
16399 member of the common block that we are processing.
16400 CU is the CU from which the above come. */
16401
16402static void
16403mark_common_block_symbol_computed (struct symbol *sym,
16404 struct die_info *common_die,
16405 struct attribute *common_loc,
16406 struct attribute *member_loc,
16407 struct dwarf2_cu *cu)
16408{
16409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16410 struct objfile *objfile = per_objfile->objfile;
16411 struct dwarf2_locexpr_baton *baton;
16412 gdb_byte *ptr;
16413 unsigned int cu_off;
16414 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16415 LONGEST offset = 0;
16416
16417 gdb_assert (common_loc && member_loc);
16418 gdb_assert (common_loc->form_is_block ());
16419 gdb_assert (member_loc->form_is_block ()
16420 || member_loc->form_is_constant ());
16421
16422 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16423 baton->per_objfile = per_objfile;
16424 baton->per_cu = cu->per_cu;
16425 gdb_assert (baton->per_cu);
16426
16427 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16428
16429 if (member_loc->form_is_constant ())
16430 {
16431 offset = member_loc->constant_value (0);
16432 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16433 }
16434 else
16435 baton->size += DW_BLOCK (member_loc)->size;
16436
16437 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16438 baton->data = ptr;
16439
16440 *ptr++ = DW_OP_call4;
16441 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16442 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16443 ptr += 4;
16444
16445 if (member_loc->form_is_constant ())
16446 {
16447 *ptr++ = DW_OP_addr;
16448 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16449 ptr += cu->header.addr_size;
16450 }
16451 else
16452 {
16453 /* We have to copy the data here, because DW_OP_call4 will only
16454 use a DW_AT_location attribute. */
16455 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16456 ptr += DW_BLOCK (member_loc)->size;
16457 }
16458
16459 *ptr++ = DW_OP_plus;
16460 gdb_assert (ptr - baton->data == baton->size);
16461
16462 SYMBOL_LOCATION_BATON (sym) = baton;
16463 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16464}
16465
16466/* Create appropriate locally-scoped variables for all the
16467 DW_TAG_common_block entries. Also create a struct common_block
16468 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16469 is used to separate the common blocks name namespace from regular
16470 variable names. */
16471
16472static void
16473read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16474{
16475 struct attribute *attr;
16476
16477 attr = dwarf2_attr (die, DW_AT_location, cu);
16478 if (attr != nullptr)
16479 {
16480 /* Support the .debug_loc offsets. */
16481 if (attr->form_is_block ())
16482 {
16483 /* Ok. */
16484 }
16485 else if (attr->form_is_section_offset ())
16486 {
16487 dwarf2_complex_location_expr_complaint ();
16488 attr = NULL;
16489 }
16490 else
16491 {
16492 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16493 "common block member");
16494 attr = NULL;
16495 }
16496 }
16497
16498 if (die->child != NULL)
16499 {
16500 struct objfile *objfile = cu->per_objfile->objfile;
16501 struct die_info *child_die;
16502 size_t n_entries = 0, size;
16503 struct common_block *common_block;
16504 struct symbol *sym;
16505
16506 for (child_die = die->child;
16507 child_die && child_die->tag;
16508 child_die = child_die->sibling)
16509 ++n_entries;
16510
16511 size = (sizeof (struct common_block)
16512 + (n_entries - 1) * sizeof (struct symbol *));
16513 common_block
16514 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16515 size);
16516 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16517 common_block->n_entries = 0;
16518
16519 for (child_die = die->child;
16520 child_die && child_die->tag;
16521 child_die = child_die->sibling)
16522 {
16523 /* Create the symbol in the DW_TAG_common_block block in the current
16524 symbol scope. */
16525 sym = new_symbol (child_die, NULL, cu);
16526 if (sym != NULL)
16527 {
16528 struct attribute *member_loc;
16529
16530 common_block->contents[common_block->n_entries++] = sym;
16531
16532 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16533 cu);
16534 if (member_loc)
16535 {
16536 /* GDB has handled this for a long time, but it is
16537 not specified by DWARF. It seems to have been
16538 emitted by gfortran at least as recently as:
16539 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16540 complaint (_("Variable in common block has "
16541 "DW_AT_data_member_location "
16542 "- DIE at %s [in module %s]"),
16543 sect_offset_str (child_die->sect_off),
16544 objfile_name (objfile));
16545
16546 if (member_loc->form_is_section_offset ())
16547 dwarf2_complex_location_expr_complaint ();
16548 else if (member_loc->form_is_constant ()
16549 || member_loc->form_is_block ())
16550 {
16551 if (attr != nullptr)
16552 mark_common_block_symbol_computed (sym, die, attr,
16553 member_loc, cu);
16554 }
16555 else
16556 dwarf2_complex_location_expr_complaint ();
16557 }
16558 }
16559 }
16560
16561 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16562 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16563 }
16564}
16565
16566/* Create a type for a C++ namespace. */
16567
16568static struct type *
16569read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16570{
16571 struct objfile *objfile = cu->per_objfile->objfile;
16572 const char *previous_prefix, *name;
16573 int is_anonymous;
16574 struct type *type;
16575
16576 /* For extensions, reuse the type of the original namespace. */
16577 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16578 {
16579 struct die_info *ext_die;
16580 struct dwarf2_cu *ext_cu = cu;
16581
16582 ext_die = dwarf2_extension (die, &ext_cu);
16583 type = read_type_die (ext_die, ext_cu);
16584
16585 /* EXT_CU may not be the same as CU.
16586 Ensure TYPE is recorded with CU in die_type_hash. */
16587 return set_die_type (die, type, cu);
16588 }
16589
16590 name = namespace_name (die, &is_anonymous, cu);
16591
16592 /* Now build the name of the current namespace. */
16593
16594 previous_prefix = determine_prefix (die, cu);
16595 if (previous_prefix[0] != '\0')
16596 name = typename_concat (&objfile->objfile_obstack,
16597 previous_prefix, name, 0, cu);
16598
16599 /* Create the type. */
16600 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16601
16602 return set_die_type (die, type, cu);
16603}
16604
16605/* Read a namespace scope. */
16606
16607static void
16608read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16609{
16610 struct objfile *objfile = cu->per_objfile->objfile;
16611 int is_anonymous;
16612
16613 /* Add a symbol associated to this if we haven't seen the namespace
16614 before. Also, add a using directive if it's an anonymous
16615 namespace. */
16616
16617 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16618 {
16619 struct type *type;
16620
16621 type = read_type_die (die, cu);
16622 new_symbol (die, type, cu);
16623
16624 namespace_name (die, &is_anonymous, cu);
16625 if (is_anonymous)
16626 {
16627 const char *previous_prefix = determine_prefix (die, cu);
16628
16629 std::vector<const char *> excludes;
16630 add_using_directive (using_directives (cu),
16631 previous_prefix, type->name (), NULL,
16632 NULL, excludes, 0, &objfile->objfile_obstack);
16633 }
16634 }
16635
16636 if (die->child != NULL)
16637 {
16638 struct die_info *child_die = die->child;
16639
16640 while (child_die && child_die->tag)
16641 {
16642 process_die (child_die, cu);
16643 child_die = child_die->sibling;
16644 }
16645 }
16646}
16647
16648/* Read a Fortran module as type. This DIE can be only a declaration used for
16649 imported module. Still we need that type as local Fortran "use ... only"
16650 declaration imports depend on the created type in determine_prefix. */
16651
16652static struct type *
16653read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16654{
16655 struct objfile *objfile = cu->per_objfile->objfile;
16656 const char *module_name;
16657 struct type *type;
16658
16659 module_name = dwarf2_name (die, cu);
16660 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16661
16662 return set_die_type (die, type, cu);
16663}
16664
16665/* Read a Fortran module. */
16666
16667static void
16668read_module (struct die_info *die, struct dwarf2_cu *cu)
16669{
16670 struct die_info *child_die = die->child;
16671 struct type *type;
16672
16673 type = read_type_die (die, cu);
16674 new_symbol (die, type, cu);
16675
16676 while (child_die && child_die->tag)
16677 {
16678 process_die (child_die, cu);
16679 child_die = child_die->sibling;
16680 }
16681}
16682
16683/* Return the name of the namespace represented by DIE. Set
16684 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16685 namespace. */
16686
16687static const char *
16688namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16689{
16690 struct die_info *current_die;
16691 const char *name = NULL;
16692
16693 /* Loop through the extensions until we find a name. */
16694
16695 for (current_die = die;
16696 current_die != NULL;
16697 current_die = dwarf2_extension (die, &cu))
16698 {
16699 /* We don't use dwarf2_name here so that we can detect the absence
16700 of a name -> anonymous namespace. */
16701 name = dwarf2_string_attr (die, DW_AT_name, cu);
16702
16703 if (name != NULL)
16704 break;
16705 }
16706
16707 /* Is it an anonymous namespace? */
16708
16709 *is_anonymous = (name == NULL);
16710 if (*is_anonymous)
16711 name = CP_ANONYMOUS_NAMESPACE_STR;
16712
16713 return name;
16714}
16715
16716/* Extract all information from a DW_TAG_pointer_type DIE and add to
16717 the user defined type vector. */
16718
16719static struct type *
16720read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16721{
16722 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16723 struct comp_unit_head *cu_header = &cu->header;
16724 struct type *type;
16725 struct attribute *attr_byte_size;
16726 struct attribute *attr_address_class;
16727 int byte_size, addr_class;
16728 struct type *target_type;
16729
16730 target_type = die_type (die, cu);
16731
16732 /* The die_type call above may have already set the type for this DIE. */
16733 type = get_die_type (die, cu);
16734 if (type)
16735 return type;
16736
16737 type = lookup_pointer_type (target_type);
16738
16739 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16740 if (attr_byte_size)
16741 byte_size = DW_UNSND (attr_byte_size);
16742 else
16743 byte_size = cu_header->addr_size;
16744
16745 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16746 if (attr_address_class)
16747 addr_class = DW_UNSND (attr_address_class);
16748 else
16749 addr_class = DW_ADDR_none;
16750
16751 ULONGEST alignment = get_alignment (cu, die);
16752
16753 /* If the pointer size, alignment, or address class is different
16754 than the default, create a type variant marked as such and set
16755 the length accordingly. */
16756 if (TYPE_LENGTH (type) != byte_size
16757 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16758 && alignment != TYPE_RAW_ALIGN (type))
16759 || addr_class != DW_ADDR_none)
16760 {
16761 if (gdbarch_address_class_type_flags_p (gdbarch))
16762 {
16763 int type_flags;
16764
16765 type_flags = gdbarch_address_class_type_flags
16766 (gdbarch, byte_size, addr_class);
16767 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16768 == 0);
16769 type = make_type_with_address_space (type, type_flags);
16770 }
16771 else if (TYPE_LENGTH (type) != byte_size)
16772 {
16773 complaint (_("invalid pointer size %d"), byte_size);
16774 }
16775 else if (TYPE_RAW_ALIGN (type) != alignment)
16776 {
16777 complaint (_("Invalid DW_AT_alignment"
16778 " - DIE at %s [in module %s]"),
16779 sect_offset_str (die->sect_off),
16780 objfile_name (cu->per_objfile->objfile));
16781 }
16782 else
16783 {
16784 /* Should we also complain about unhandled address classes? */
16785 }
16786 }
16787
16788 TYPE_LENGTH (type) = byte_size;
16789 set_type_align (type, alignment);
16790 return set_die_type (die, type, cu);
16791}
16792
16793/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16794 the user defined type vector. */
16795
16796static struct type *
16797read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16798{
16799 struct type *type;
16800 struct type *to_type;
16801 struct type *domain;
16802
16803 to_type = die_type (die, cu);
16804 domain = die_containing_type (die, cu);
16805
16806 /* The calls above may have already set the type for this DIE. */
16807 type = get_die_type (die, cu);
16808 if (type)
16809 return type;
16810
16811 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16812 type = lookup_methodptr_type (to_type);
16813 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16814 {
16815 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16816
16817 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16818 to_type->fields (), to_type->num_fields (),
16819 TYPE_VARARGS (to_type));
16820 type = lookup_methodptr_type (new_type);
16821 }
16822 else
16823 type = lookup_memberptr_type (to_type, domain);
16824
16825 return set_die_type (die, type, cu);
16826}
16827
16828/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16829 the user defined type vector. */
16830
16831static struct type *
16832read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16833 enum type_code refcode)
16834{
16835 struct comp_unit_head *cu_header = &cu->header;
16836 struct type *type, *target_type;
16837 struct attribute *attr;
16838
16839 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16840
16841 target_type = die_type (die, cu);
16842
16843 /* The die_type call above may have already set the type for this DIE. */
16844 type = get_die_type (die, cu);
16845 if (type)
16846 return type;
16847
16848 type = lookup_reference_type (target_type, refcode);
16849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16850 if (attr != nullptr)
16851 {
16852 TYPE_LENGTH (type) = DW_UNSND (attr);
16853 }
16854 else
16855 {
16856 TYPE_LENGTH (type) = cu_header->addr_size;
16857 }
16858 maybe_set_alignment (cu, die, type);
16859 return set_die_type (die, type, cu);
16860}
16861
16862/* Add the given cv-qualifiers to the element type of the array. GCC
16863 outputs DWARF type qualifiers that apply to an array, not the
16864 element type. But GDB relies on the array element type to carry
16865 the cv-qualifiers. This mimics section 6.7.3 of the C99
16866 specification. */
16867
16868static struct type *
16869add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16870 struct type *base_type, int cnst, int voltl)
16871{
16872 struct type *el_type, *inner_array;
16873
16874 base_type = copy_type (base_type);
16875 inner_array = base_type;
16876
16877 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16878 {
16879 TYPE_TARGET_TYPE (inner_array) =
16880 copy_type (TYPE_TARGET_TYPE (inner_array));
16881 inner_array = TYPE_TARGET_TYPE (inner_array);
16882 }
16883
16884 el_type = TYPE_TARGET_TYPE (inner_array);
16885 cnst |= TYPE_CONST (el_type);
16886 voltl |= TYPE_VOLATILE (el_type);
16887 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16888
16889 return set_die_type (die, base_type, cu);
16890}
16891
16892static struct type *
16893read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16894{
16895 struct type *base_type, *cv_type;
16896
16897 base_type = die_type (die, cu);
16898
16899 /* The die_type call above may have already set the type for this DIE. */
16900 cv_type = get_die_type (die, cu);
16901 if (cv_type)
16902 return cv_type;
16903
16904 /* In case the const qualifier is applied to an array type, the element type
16905 is so qualified, not the array type (section 6.7.3 of C99). */
16906 if (base_type->code () == TYPE_CODE_ARRAY)
16907 return add_array_cv_type (die, cu, base_type, 1, 0);
16908
16909 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16910 return set_die_type (die, cv_type, cu);
16911}
16912
16913static struct type *
16914read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16915{
16916 struct type *base_type, *cv_type;
16917
16918 base_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 cv_type = get_die_type (die, cu);
16922 if (cv_type)
16923 return cv_type;
16924
16925 /* In case the volatile qualifier is applied to an array type, the
16926 element type is so qualified, not the array type (section 6.7.3
16927 of C99). */
16928 if (base_type->code () == TYPE_CODE_ARRAY)
16929 return add_array_cv_type (die, cu, base_type, 0, 1);
16930
16931 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16932 return set_die_type (die, cv_type, cu);
16933}
16934
16935/* Handle DW_TAG_restrict_type. */
16936
16937static struct type *
16938read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16939{
16940 struct type *base_type, *cv_type;
16941
16942 base_type = die_type (die, cu);
16943
16944 /* The die_type call above may have already set the type for this DIE. */
16945 cv_type = get_die_type (die, cu);
16946 if (cv_type)
16947 return cv_type;
16948
16949 cv_type = make_restrict_type (base_type);
16950 return set_die_type (die, cv_type, cu);
16951}
16952
16953/* Handle DW_TAG_atomic_type. */
16954
16955static struct type *
16956read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16957{
16958 struct type *base_type, *cv_type;
16959
16960 base_type = die_type (die, cu);
16961
16962 /* The die_type call above may have already set the type for this DIE. */
16963 cv_type = get_die_type (die, cu);
16964 if (cv_type)
16965 return cv_type;
16966
16967 cv_type = make_atomic_type (base_type);
16968 return set_die_type (die, cv_type, cu);
16969}
16970
16971/* Extract all information from a DW_TAG_string_type DIE and add to
16972 the user defined type vector. It isn't really a user defined type,
16973 but it behaves like one, with other DIE's using an AT_user_def_type
16974 attribute to reference it. */
16975
16976static struct type *
16977read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16978{
16979 struct objfile *objfile = cu->per_objfile->objfile;
16980 struct gdbarch *gdbarch = objfile->arch ();
16981 struct type *type, *range_type, *index_type, *char_type;
16982 struct attribute *attr;
16983 struct dynamic_prop prop;
16984 bool length_is_constant = true;
16985 LONGEST length;
16986
16987 /* There are a couple of places where bit sizes might be made use of
16988 when parsing a DW_TAG_string_type, however, no producer that we know
16989 of make use of these. Handling bit sizes that are a multiple of the
16990 byte size is easy enough, but what about other bit sizes? Lets deal
16991 with that problem when we have to. Warn about these attributes being
16992 unsupported, then parse the type and ignore them like we always
16993 have. */
16994 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16995 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16996 {
16997 static bool warning_printed = false;
16998 if (!warning_printed)
16999 {
17000 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17001 "currently supported on DW_TAG_string_type."));
17002 warning_printed = true;
17003 }
17004 }
17005
17006 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17007 if (attr != nullptr && !attr->form_is_constant ())
17008 {
17009 /* The string length describes the location at which the length of
17010 the string can be found. The size of the length field can be
17011 specified with one of the attributes below. */
17012 struct type *prop_type;
17013 struct attribute *len
17014 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17015 if (len == nullptr)
17016 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17017 if (len != nullptr && len->form_is_constant ())
17018 {
17019 /* Pass 0 as the default as we know this attribute is constant
17020 and the default value will not be returned. */
17021 LONGEST sz = len->constant_value (0);
17022 prop_type = cu->per_objfile->int_type (sz, true);
17023 }
17024 else
17025 {
17026 /* If the size is not specified then we assume it is the size of
17027 an address on this target. */
17028 prop_type = cu->addr_sized_int_type (true);
17029 }
17030
17031 /* Convert the attribute into a dynamic property. */
17032 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17033 length = 1;
17034 else
17035 length_is_constant = false;
17036 }
17037 else if (attr != nullptr)
17038 {
17039 /* This DW_AT_string_length just contains the length with no
17040 indirection. There's no need to create a dynamic property in this
17041 case. Pass 0 for the default value as we know it will not be
17042 returned in this case. */
17043 length = attr->constant_value (0);
17044 }
17045 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17046 {
17047 /* We don't currently support non-constant byte sizes for strings. */
17048 length = attr->constant_value (1);
17049 }
17050 else
17051 {
17052 /* Use 1 as a fallback length if we have nothing else. */
17053 length = 1;
17054 }
17055
17056 index_type = objfile_type (objfile)->builtin_int;
17057 if (length_is_constant)
17058 range_type = create_static_range_type (NULL, index_type, 1, length);
17059 else
17060 {
17061 struct dynamic_prop low_bound;
17062
17063 low_bound.kind = PROP_CONST;
17064 low_bound.data.const_val = 1;
17065 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17066 }
17067 char_type = language_string_char_type (cu->language_defn, gdbarch);
17068 type = create_string_type (NULL, char_type, range_type);
17069
17070 return set_die_type (die, type, cu);
17071}
17072
17073/* Assuming that DIE corresponds to a function, returns nonzero
17074 if the function is prototyped. */
17075
17076static int
17077prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17078{
17079 struct attribute *attr;
17080
17081 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17082 if (attr && (DW_UNSND (attr) != 0))
17083 return 1;
17084
17085 /* The DWARF standard implies that the DW_AT_prototyped attribute
17086 is only meaningful for C, but the concept also extends to other
17087 languages that allow unprototyped functions (Eg: Objective C).
17088 For all other languages, assume that functions are always
17089 prototyped. */
17090 if (cu->language != language_c
17091 && cu->language != language_objc
17092 && cu->language != language_opencl)
17093 return 1;
17094
17095 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17096 prototyped and unprototyped functions; default to prototyped,
17097 since that is more common in modern code (and RealView warns
17098 about unprototyped functions). */
17099 if (producer_is_realview (cu->producer))
17100 return 1;
17101
17102 return 0;
17103}
17104
17105/* Handle DIES due to C code like:
17106
17107 struct foo
17108 {
17109 int (*funcp)(int a, long l);
17110 int b;
17111 };
17112
17113 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17114
17115static struct type *
17116read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17117{
17118 struct objfile *objfile = cu->per_objfile->objfile;
17119 struct type *type; /* Type that this function returns. */
17120 struct type *ftype; /* Function that returns above type. */
17121 struct attribute *attr;
17122
17123 type = die_type (die, cu);
17124
17125 /* The die_type call above may have already set the type for this DIE. */
17126 ftype = get_die_type (die, cu);
17127 if (ftype)
17128 return ftype;
17129
17130 ftype = lookup_function_type (type);
17131
17132 if (prototyped_function_p (die, cu))
17133 TYPE_PROTOTYPED (ftype) = 1;
17134
17135 /* Store the calling convention in the type if it's available in
17136 the subroutine die. Otherwise set the calling convention to
17137 the default value DW_CC_normal. */
17138 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17139 if (attr != nullptr
17140 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17141 TYPE_CALLING_CONVENTION (ftype)
17142 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17143 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17144 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17145 else
17146 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17147
17148 /* Record whether the function returns normally to its caller or not
17149 if the DWARF producer set that information. */
17150 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17151 if (attr && (DW_UNSND (attr) != 0))
17152 TYPE_NO_RETURN (ftype) = 1;
17153
17154 /* We need to add the subroutine type to the die immediately so
17155 we don't infinitely recurse when dealing with parameters
17156 declared as the same subroutine type. */
17157 set_die_type (die, ftype, cu);
17158
17159 if (die->child != NULL)
17160 {
17161 struct type *void_type = objfile_type (objfile)->builtin_void;
17162 struct die_info *child_die;
17163 int nparams, iparams;
17164
17165 /* Count the number of parameters.
17166 FIXME: GDB currently ignores vararg functions, but knows about
17167 vararg member functions. */
17168 nparams = 0;
17169 child_die = die->child;
17170 while (child_die && child_die->tag)
17171 {
17172 if (child_die->tag == DW_TAG_formal_parameter)
17173 nparams++;
17174 else if (child_die->tag == DW_TAG_unspecified_parameters)
17175 TYPE_VARARGS (ftype) = 1;
17176 child_die = child_die->sibling;
17177 }
17178
17179 /* Allocate storage for parameters and fill them in. */
17180 ftype->set_num_fields (nparams);
17181 ftype->set_fields
17182 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17183
17184 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17185 even if we error out during the parameters reading below. */
17186 for (iparams = 0; iparams < nparams; iparams++)
17187 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17188
17189 iparams = 0;
17190 child_die = die->child;
17191 while (child_die && child_die->tag)
17192 {
17193 if (child_die->tag == DW_TAG_formal_parameter)
17194 {
17195 struct type *arg_type;
17196
17197 /* DWARF version 2 has no clean way to discern C++
17198 static and non-static member functions. G++ helps
17199 GDB by marking the first parameter for non-static
17200 member functions (which is the this pointer) as
17201 artificial. We pass this information to
17202 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17203
17204 DWARF version 3 added DW_AT_object_pointer, which GCC
17205 4.5 does not yet generate. */
17206 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17207 if (attr != nullptr)
17208 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17209 else
17210 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17211 arg_type = die_type (child_die, cu);
17212
17213 /* RealView does not mark THIS as const, which the testsuite
17214 expects. GCC marks THIS as const in method definitions,
17215 but not in the class specifications (GCC PR 43053). */
17216 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17217 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17218 {
17219 int is_this = 0;
17220 struct dwarf2_cu *arg_cu = cu;
17221 const char *name = dwarf2_name (child_die, cu);
17222
17223 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17224 if (attr != nullptr)
17225 {
17226 /* If the compiler emits this, use it. */
17227 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17228 is_this = 1;
17229 }
17230 else if (name && strcmp (name, "this") == 0)
17231 /* Function definitions will have the argument names. */
17232 is_this = 1;
17233 else if (name == NULL && iparams == 0)
17234 /* Declarations may not have the names, so like
17235 elsewhere in GDB, assume an artificial first
17236 argument is "this". */
17237 is_this = 1;
17238
17239 if (is_this)
17240 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17241 arg_type, 0);
17242 }
17243
17244 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17245 iparams++;
17246 }
17247 child_die = child_die->sibling;
17248 }
17249 }
17250
17251 return ftype;
17252}
17253
17254static struct type *
17255read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17256{
17257 struct objfile *objfile = cu->per_objfile->objfile;
17258 const char *name = NULL;
17259 struct type *this_type, *target_type;
17260
17261 name = dwarf2_full_name (NULL, die, cu);
17262 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17263 TYPE_TARGET_STUB (this_type) = 1;
17264 set_die_type (die, this_type, cu);
17265 target_type = die_type (die, cu);
17266 if (target_type != this_type)
17267 TYPE_TARGET_TYPE (this_type) = target_type;
17268 else
17269 {
17270 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17271 spec and cause infinite loops in GDB. */
17272 complaint (_("Self-referential DW_TAG_typedef "
17273 "- DIE at %s [in module %s]"),
17274 sect_offset_str (die->sect_off), objfile_name (objfile));
17275 TYPE_TARGET_TYPE (this_type) = NULL;
17276 }
17277 if (name == NULL)
17278 {
17279 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17280 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17281 Handle these by just returning the target type, rather than
17282 constructing an anonymous typedef type and trying to handle this
17283 elsewhere. */
17284 set_die_type (die, target_type, cu);
17285 return target_type;
17286 }
17287 return this_type;
17288}
17289
17290/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17291 (which may be different from NAME) to the architecture back-end to allow
17292 it to guess the correct format if necessary. */
17293
17294static struct type *
17295dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17296 const char *name_hint, enum bfd_endian byte_order)
17297{
17298 struct gdbarch *gdbarch = objfile->arch ();
17299 const struct floatformat **format;
17300 struct type *type;
17301
17302 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17303 if (format)
17304 type = init_float_type (objfile, bits, name, format, byte_order);
17305 else
17306 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17307
17308 return type;
17309}
17310
17311/* Allocate an integer type of size BITS and name NAME. */
17312
17313static struct type *
17314dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17315 int bits, int unsigned_p, const char *name)
17316{
17317 struct type *type;
17318
17319 /* Versions of Intel's C Compiler generate an integer type called "void"
17320 instead of using DW_TAG_unspecified_type. This has been seen on
17321 at least versions 14, 17, and 18. */
17322 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17323 && strcmp (name, "void") == 0)
17324 type = objfile_type (objfile)->builtin_void;
17325 else
17326 type = init_integer_type (objfile, bits, unsigned_p, name);
17327
17328 return type;
17329}
17330
17331/* Initialise and return a floating point type of size BITS suitable for
17332 use as a component of a complex number. The NAME_HINT is passed through
17333 when initialising the floating point type and is the name of the complex
17334 type.
17335
17336 As DWARF doesn't currently provide an explicit name for the components
17337 of a complex number, but it can be helpful to have these components
17338 named, we try to select a suitable name based on the size of the
17339 component. */
17340static struct type *
17341dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17342 struct objfile *objfile,
17343 int bits, const char *name_hint,
17344 enum bfd_endian byte_order)
17345{
17346 gdbarch *gdbarch = objfile->arch ();
17347 struct type *tt = nullptr;
17348
17349 /* Try to find a suitable floating point builtin type of size BITS.
17350 We're going to use the name of this type as the name for the complex
17351 target type that we are about to create. */
17352 switch (cu->language)
17353 {
17354 case language_fortran:
17355 switch (bits)
17356 {
17357 case 32:
17358 tt = builtin_f_type (gdbarch)->builtin_real;
17359 break;
17360 case 64:
17361 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17362 break;
17363 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17364 case 128:
17365 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17366 break;
17367 }
17368 break;
17369 default:
17370 switch (bits)
17371 {
17372 case 32:
17373 tt = builtin_type (gdbarch)->builtin_float;
17374 break;
17375 case 64:
17376 tt = builtin_type (gdbarch)->builtin_double;
17377 break;
17378 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17379 case 128:
17380 tt = builtin_type (gdbarch)->builtin_long_double;
17381 break;
17382 }
17383 break;
17384 }
17385
17386 /* If the type we found doesn't match the size we were looking for, then
17387 pretend we didn't find a type at all, the complex target type we
17388 create will then be nameless. */
17389 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17390 tt = nullptr;
17391
17392 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17393 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17394}
17395
17396/* Find a representation of a given base type and install
17397 it in the TYPE field of the die. */
17398
17399static struct type *
17400read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17401{
17402 struct objfile *objfile = cu->per_objfile->objfile;
17403 struct type *type;
17404 struct attribute *attr;
17405 int encoding = 0, bits = 0;
17406 const char *name;
17407 gdbarch *arch;
17408
17409 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17410 if (attr != nullptr)
17411 encoding = DW_UNSND (attr);
17412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17413 if (attr != nullptr)
17414 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17415 name = dwarf2_name (die, cu);
17416 if (!name)
17417 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17418
17419 arch = objfile->arch ();
17420 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17421
17422 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17423 if (attr)
17424 {
17425 int endianity = DW_UNSND (attr);
17426
17427 switch (endianity)
17428 {
17429 case DW_END_big:
17430 byte_order = BFD_ENDIAN_BIG;
17431 break;
17432 case DW_END_little:
17433 byte_order = BFD_ENDIAN_LITTLE;
17434 break;
17435 default:
17436 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17437 break;
17438 }
17439 }
17440
17441 switch (encoding)
17442 {
17443 case DW_ATE_address:
17444 /* Turn DW_ATE_address into a void * pointer. */
17445 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17446 type = init_pointer_type (objfile, bits, name, type);
17447 break;
17448 case DW_ATE_boolean:
17449 type = init_boolean_type (objfile, bits, 1, name);
17450 break;
17451 case DW_ATE_complex_float:
17452 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17453 byte_order);
17454 if (type->code () == TYPE_CODE_ERROR)
17455 {
17456 if (name == nullptr)
17457 {
17458 struct obstack *obstack
17459 = &cu->per_objfile->objfile->objfile_obstack;
17460 name = obconcat (obstack, "_Complex ", type->name (),
17461 nullptr);
17462 }
17463 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17464 }
17465 else
17466 type = init_complex_type (name, type);
17467 break;
17468 case DW_ATE_decimal_float:
17469 type = init_decfloat_type (objfile, bits, name);
17470 break;
17471 case DW_ATE_float:
17472 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17473 break;
17474 case DW_ATE_signed:
17475 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17476 break;
17477 case DW_ATE_unsigned:
17478 if (cu->language == language_fortran
17479 && name
17480 && startswith (name, "character("))
17481 type = init_character_type (objfile, bits, 1, name);
17482 else
17483 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17484 break;
17485 case DW_ATE_signed_char:
17486 if (cu->language == language_ada || cu->language == language_m2
17487 || cu->language == language_pascal
17488 || cu->language == language_fortran)
17489 type = init_character_type (objfile, bits, 0, name);
17490 else
17491 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17492 break;
17493 case DW_ATE_unsigned_char:
17494 if (cu->language == language_ada || cu->language == language_m2
17495 || cu->language == language_pascal
17496 || cu->language == language_fortran
17497 || cu->language == language_rust)
17498 type = init_character_type (objfile, bits, 1, name);
17499 else
17500 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17501 break;
17502 case DW_ATE_UTF:
17503 {
17504 if (bits == 16)
17505 type = builtin_type (arch)->builtin_char16;
17506 else if (bits == 32)
17507 type = builtin_type (arch)->builtin_char32;
17508 else
17509 {
17510 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17511 bits);
17512 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17513 }
17514 return set_die_type (die, type, cu);
17515 }
17516 break;
17517
17518 default:
17519 complaint (_("unsupported DW_AT_encoding: '%s'"),
17520 dwarf_type_encoding_name (encoding));
17521 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17522 break;
17523 }
17524
17525 if (name && strcmp (name, "char") == 0)
17526 TYPE_NOSIGN (type) = 1;
17527
17528 maybe_set_alignment (cu, die, type);
17529
17530 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17531
17532 return set_die_type (die, type, cu);
17533}
17534
17535/* Parse dwarf attribute if it's a block, reference or constant and put the
17536 resulting value of the attribute into struct bound_prop.
17537 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17538
17539static int
17540attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17541 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17542 struct type *default_type)
17543{
17544 struct dwarf2_property_baton *baton;
17545 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17546 struct objfile *objfile = per_objfile->objfile;
17547 struct obstack *obstack = &objfile->objfile_obstack;
17548
17549 gdb_assert (default_type != NULL);
17550
17551 if (attr == NULL || prop == NULL)
17552 return 0;
17553
17554 if (attr->form_is_block ())
17555 {
17556 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17557 baton->property_type = default_type;
17558 baton->locexpr.per_cu = cu->per_cu;
17559 baton->locexpr.per_objfile = per_objfile;
17560 baton->locexpr.size = DW_BLOCK (attr)->size;
17561 baton->locexpr.data = DW_BLOCK (attr)->data;
17562 switch (attr->name)
17563 {
17564 case DW_AT_string_length:
17565 baton->locexpr.is_reference = true;
17566 break;
17567 default:
17568 baton->locexpr.is_reference = false;
17569 break;
17570 }
17571 prop->data.baton = baton;
17572 prop->kind = PROP_LOCEXPR;
17573 gdb_assert (prop->data.baton != NULL);
17574 }
17575 else if (attr->form_is_ref ())
17576 {
17577 struct dwarf2_cu *target_cu = cu;
17578 struct die_info *target_die;
17579 struct attribute *target_attr;
17580
17581 target_die = follow_die_ref (die, attr, &target_cu);
17582 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17583 if (target_attr == NULL)
17584 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17585 target_cu);
17586 if (target_attr == NULL)
17587 return 0;
17588
17589 switch (target_attr->name)
17590 {
17591 case DW_AT_location:
17592 if (target_attr->form_is_section_offset ())
17593 {
17594 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17595 baton->property_type = die_type (target_die, target_cu);
17596 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17597 prop->data.baton = baton;
17598 prop->kind = PROP_LOCLIST;
17599 gdb_assert (prop->data.baton != NULL);
17600 }
17601 else if (target_attr->form_is_block ())
17602 {
17603 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17604 baton->property_type = die_type (target_die, target_cu);
17605 baton->locexpr.per_cu = cu->per_cu;
17606 baton->locexpr.per_objfile = per_objfile;
17607 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17608 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17609 baton->locexpr.is_reference = true;
17610 prop->data.baton = baton;
17611 prop->kind = PROP_LOCEXPR;
17612 gdb_assert (prop->data.baton != NULL);
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17617 "dynamic property");
17618 return 0;
17619 }
17620 break;
17621 case DW_AT_data_member_location:
17622 {
17623 LONGEST offset;
17624
17625 if (!handle_data_member_location (target_die, target_cu,
17626 &offset))
17627 return 0;
17628
17629 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17630 baton->property_type = read_type_die (target_die->parent,
17631 target_cu);
17632 baton->offset_info.offset = offset;
17633 baton->offset_info.type = die_type (target_die, target_cu);
17634 prop->data.baton = baton;
17635 prop->kind = PROP_ADDR_OFFSET;
17636 break;
17637 }
17638 }
17639 }
17640 else if (attr->form_is_constant ())
17641 {
17642 prop->data.const_val = attr->constant_value (0);
17643 prop->kind = PROP_CONST;
17644 }
17645 else
17646 {
17647 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17648 dwarf2_name (die, cu));
17649 return 0;
17650 }
17651
17652 return 1;
17653}
17654
17655/* See read.h. */
17656
17657struct type *
17658dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17659{
17660 struct type *int_type;
17661
17662 /* Helper macro to examine the various builtin types. */
17663#define TRY_TYPE(F) \
17664 int_type = (unsigned_p \
17665 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17666 : objfile_type (objfile)->builtin_ ## F); \
17667 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17668 return int_type
17669
17670 TRY_TYPE (char);
17671 TRY_TYPE (short);
17672 TRY_TYPE (int);
17673 TRY_TYPE (long);
17674 TRY_TYPE (long_long);
17675
17676#undef TRY_TYPE
17677
17678 gdb_assert_not_reached ("unable to find suitable integer type");
17679}
17680
17681/* See read.h. */
17682
17683struct type *
17684dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17685{
17686 int addr_size = this->per_cu->addr_size ();
17687 return this->per_objfile->int_type (addr_size, unsigned_p);
17688}
17689
17690/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17691 present (which is valid) then compute the default type based on the
17692 compilation units address size. */
17693
17694static struct type *
17695read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17696{
17697 struct type *index_type = die_type (die, cu);
17698
17699 /* Dwarf-2 specifications explicitly allows to create subrange types
17700 without specifying a base type.
17701 In that case, the base type must be set to the type of
17702 the lower bound, upper bound or count, in that order, if any of these
17703 three attributes references an object that has a type.
17704 If no base type is found, the Dwarf-2 specifications say that
17705 a signed integer type of size equal to the size of an address should
17706 be used.
17707 For the following C code: `extern char gdb_int [];'
17708 GCC produces an empty range DIE.
17709 FIXME: muller/2010-05-28: Possible references to object for low bound,
17710 high bound or count are not yet handled by this code. */
17711 if (index_type->code () == TYPE_CODE_VOID)
17712 index_type = cu->addr_sized_int_type (false);
17713
17714 return index_type;
17715}
17716
17717/* Read the given DW_AT_subrange DIE. */
17718
17719static struct type *
17720read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17721{
17722 struct type *base_type, *orig_base_type;
17723 struct type *range_type;
17724 struct attribute *attr;
17725 struct dynamic_prop low, high;
17726 int low_default_is_valid;
17727 int high_bound_is_count = 0;
17728 const char *name;
17729 ULONGEST negative_mask;
17730
17731 orig_base_type = read_subrange_index_type (die, cu);
17732
17733 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17734 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17735 creating the range type, but we use the result of check_typedef
17736 when examining properties of the type. */
17737 base_type = check_typedef (orig_base_type);
17738
17739 /* The die_type call above may have already set the type for this DIE. */
17740 range_type = get_die_type (die, cu);
17741 if (range_type)
17742 return range_type;
17743
17744 low.kind = PROP_CONST;
17745 high.kind = PROP_CONST;
17746 high.data.const_val = 0;
17747
17748 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17749 omitting DW_AT_lower_bound. */
17750 switch (cu->language)
17751 {
17752 case language_c:
17753 case language_cplus:
17754 low.data.const_val = 0;
17755 low_default_is_valid = 1;
17756 break;
17757 case language_fortran:
17758 low.data.const_val = 1;
17759 low_default_is_valid = 1;
17760 break;
17761 case language_d:
17762 case language_objc:
17763 case language_rust:
17764 low.data.const_val = 0;
17765 low_default_is_valid = (cu->header.version >= 4);
17766 break;
17767 case language_ada:
17768 case language_m2:
17769 case language_pascal:
17770 low.data.const_val = 1;
17771 low_default_is_valid = (cu->header.version >= 4);
17772 break;
17773 default:
17774 low.data.const_val = 0;
17775 low_default_is_valid = 0;
17776 break;
17777 }
17778
17779 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17780 if (attr != nullptr)
17781 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17782 else if (!low_default_is_valid)
17783 complaint (_("Missing DW_AT_lower_bound "
17784 "- DIE at %s [in module %s]"),
17785 sect_offset_str (die->sect_off),
17786 objfile_name (cu->per_objfile->objfile));
17787
17788 struct attribute *attr_ub, *attr_count;
17789 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17790 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17791 {
17792 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17793 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17794 {
17795 /* If bounds are constant do the final calculation here. */
17796 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17797 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17798 else
17799 high_bound_is_count = 1;
17800 }
17801 else
17802 {
17803 if (attr_ub != NULL)
17804 complaint (_("Unresolved DW_AT_upper_bound "
17805 "- DIE at %s [in module %s]"),
17806 sect_offset_str (die->sect_off),
17807 objfile_name (cu->per_objfile->objfile));
17808 if (attr_count != NULL)
17809 complaint (_("Unresolved DW_AT_count "
17810 "- DIE at %s [in module %s]"),
17811 sect_offset_str (die->sect_off),
17812 objfile_name (cu->per_objfile->objfile));
17813 }
17814 }
17815
17816 LONGEST bias = 0;
17817 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17818 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17819 bias = bias_attr->constant_value (0);
17820
17821 /* Normally, the DWARF producers are expected to use a signed
17822 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17823 But this is unfortunately not always the case, as witnessed
17824 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17825 is used instead. To work around that ambiguity, we treat
17826 the bounds as signed, and thus sign-extend their values, when
17827 the base type is signed. */
17828 negative_mask =
17829 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17830 if (low.kind == PROP_CONST
17831 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17832 low.data.const_val |= negative_mask;
17833 if (high.kind == PROP_CONST
17834 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17835 high.data.const_val |= negative_mask;
17836
17837 /* Check for bit and byte strides. */
17838 struct dynamic_prop byte_stride_prop;
17839 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17840 if (attr_byte_stride != nullptr)
17841 {
17842 struct type *prop_type = cu->addr_sized_int_type (false);
17843 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17844 prop_type);
17845 }
17846
17847 struct dynamic_prop bit_stride_prop;
17848 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17849 if (attr_bit_stride != nullptr)
17850 {
17851 /* It only makes sense to have either a bit or byte stride. */
17852 if (attr_byte_stride != nullptr)
17853 {
17854 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17855 "- DIE at %s [in module %s]"),
17856 sect_offset_str (die->sect_off),
17857 objfile_name (cu->per_objfile->objfile));
17858 attr_bit_stride = nullptr;
17859 }
17860 else
17861 {
17862 struct type *prop_type = cu->addr_sized_int_type (false);
17863 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17864 prop_type);
17865 }
17866 }
17867
17868 if (attr_byte_stride != nullptr
17869 || attr_bit_stride != nullptr)
17870 {
17871 bool byte_stride_p = (attr_byte_stride != nullptr);
17872 struct dynamic_prop *stride
17873 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17874
17875 range_type
17876 = create_range_type_with_stride (NULL, orig_base_type, &low,
17877 &high, bias, stride, byte_stride_p);
17878 }
17879 else
17880 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17881
17882 if (high_bound_is_count)
17883 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17884
17885 /* Ada expects an empty array on no boundary attributes. */
17886 if (attr == NULL && cu->language != language_ada)
17887 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17888
17889 name = dwarf2_name (die, cu);
17890 if (name)
17891 range_type->set_name (name);
17892
17893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17894 if (attr != nullptr)
17895 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17896
17897 maybe_set_alignment (cu, die, range_type);
17898
17899 set_die_type (die, range_type, cu);
17900
17901 /* set_die_type should be already done. */
17902 set_descriptive_type (range_type, die, cu);
17903
17904 return range_type;
17905}
17906
17907static struct type *
17908read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17909{
17910 struct type *type;
17911
17912 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17913 type->set_name (dwarf2_name (die, cu));
17914
17915 /* In Ada, an unspecified type is typically used when the description
17916 of the type is deferred to a different unit. When encountering
17917 such a type, we treat it as a stub, and try to resolve it later on,
17918 when needed. */
17919 if (cu->language == language_ada)
17920 TYPE_STUB (type) = 1;
17921
17922 return set_die_type (die, type, cu);
17923}
17924
17925/* Read a single die and all its descendents. Set the die's sibling
17926 field to NULL; set other fields in the die correctly, and set all
17927 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17928 location of the info_ptr after reading all of those dies. PARENT
17929 is the parent of the die in question. */
17930
17931static struct die_info *
17932read_die_and_children (const struct die_reader_specs *reader,
17933 const gdb_byte *info_ptr,
17934 const gdb_byte **new_info_ptr,
17935 struct die_info *parent)
17936{
17937 struct die_info *die;
17938 const gdb_byte *cur_ptr;
17939
17940 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17941 if (die == NULL)
17942 {
17943 *new_info_ptr = cur_ptr;
17944 return NULL;
17945 }
17946 store_in_ref_table (die, reader->cu);
17947
17948 if (die->has_children)
17949 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17950 else
17951 {
17952 die->child = NULL;
17953 *new_info_ptr = cur_ptr;
17954 }
17955
17956 die->sibling = NULL;
17957 die->parent = parent;
17958 return die;
17959}
17960
17961/* Read a die, all of its descendents, and all of its siblings; set
17962 all of the fields of all of the dies correctly. Arguments are as
17963 in read_die_and_children. */
17964
17965static struct die_info *
17966read_die_and_siblings_1 (const struct die_reader_specs *reader,
17967 const gdb_byte *info_ptr,
17968 const gdb_byte **new_info_ptr,
17969 struct die_info *parent)
17970{
17971 struct die_info *first_die, *last_sibling;
17972 const gdb_byte *cur_ptr;
17973
17974 cur_ptr = info_ptr;
17975 first_die = last_sibling = NULL;
17976
17977 while (1)
17978 {
17979 struct die_info *die
17980 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17981
17982 if (die == NULL)
17983 {
17984 *new_info_ptr = cur_ptr;
17985 return first_die;
17986 }
17987
17988 if (!first_die)
17989 first_die = die;
17990 else
17991 last_sibling->sibling = die;
17992
17993 last_sibling = die;
17994 }
17995}
17996
17997/* Read a die, all of its descendents, and all of its siblings; set
17998 all of the fields of all of the dies correctly. Arguments are as
17999 in read_die_and_children.
18000 This the main entry point for reading a DIE and all its children. */
18001
18002static struct die_info *
18003read_die_and_siblings (const struct die_reader_specs *reader,
18004 const gdb_byte *info_ptr,
18005 const gdb_byte **new_info_ptr,
18006 struct die_info *parent)
18007{
18008 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18009 new_info_ptr, parent);
18010
18011 if (dwarf_die_debug)
18012 {
18013 fprintf_unfiltered (gdb_stdlog,
18014 "Read die from %s@0x%x of %s:\n",
18015 reader->die_section->get_name (),
18016 (unsigned) (info_ptr - reader->die_section->buffer),
18017 bfd_get_filename (reader->abfd));
18018 dump_die (die, dwarf_die_debug);
18019 }
18020
18021 return die;
18022}
18023
18024/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18025 attributes.
18026 The caller is responsible for filling in the extra attributes
18027 and updating (*DIEP)->num_attrs.
18028 Set DIEP to point to a newly allocated die with its information,
18029 except for its child, sibling, and parent fields. */
18030
18031static const gdb_byte *
18032read_full_die_1 (const struct die_reader_specs *reader,
18033 struct die_info **diep, const gdb_byte *info_ptr,
18034 int num_extra_attrs)
18035{
18036 unsigned int abbrev_number, bytes_read, i;
18037 struct abbrev_info *abbrev;
18038 struct die_info *die;
18039 struct dwarf2_cu *cu = reader->cu;
18040 bfd *abfd = reader->abfd;
18041
18042 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18043 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18044 info_ptr += bytes_read;
18045 if (!abbrev_number)
18046 {
18047 *diep = NULL;
18048 return info_ptr;
18049 }
18050
18051 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18052 if (!abbrev)
18053 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18054 abbrev_number,
18055 bfd_get_filename (abfd));
18056
18057 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18058 die->sect_off = sect_off;
18059 die->tag = abbrev->tag;
18060 die->abbrev = abbrev_number;
18061 die->has_children = abbrev->has_children;
18062
18063 /* Make the result usable.
18064 The caller needs to update num_attrs after adding the extra
18065 attributes. */
18066 die->num_attrs = abbrev->num_attrs;
18067
18068 std::vector<int> indexes_that_need_reprocess;
18069 for (i = 0; i < abbrev->num_attrs; ++i)
18070 {
18071 bool need_reprocess;
18072 info_ptr =
18073 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18074 info_ptr, &need_reprocess);
18075 if (need_reprocess)
18076 indexes_that_need_reprocess.push_back (i);
18077 }
18078
18079 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18080 if (attr != nullptr)
18081 cu->str_offsets_base = DW_UNSND (attr);
18082
18083 attr = die->attr (DW_AT_loclists_base);
18084 if (attr != nullptr)
18085 cu->loclist_base = DW_UNSND (attr);
18086
18087 auto maybe_addr_base = die->addr_base ();
18088 if (maybe_addr_base.has_value ())
18089 cu->addr_base = *maybe_addr_base;
18090 for (int index : indexes_that_need_reprocess)
18091 read_attribute_reprocess (reader, &die->attrs[index]);
18092 *diep = die;
18093 return info_ptr;
18094}
18095
18096/* Read a die and all its attributes.
18097 Set DIEP to point to a newly allocated die with its information,
18098 except for its child, sibling, and parent fields. */
18099
18100static const gdb_byte *
18101read_full_die (const struct die_reader_specs *reader,
18102 struct die_info **diep, const gdb_byte *info_ptr)
18103{
18104 const gdb_byte *result;
18105
18106 result = read_full_die_1 (reader, diep, info_ptr, 0);
18107
18108 if (dwarf_die_debug)
18109 {
18110 fprintf_unfiltered (gdb_stdlog,
18111 "Read die from %s@0x%x of %s:\n",
18112 reader->die_section->get_name (),
18113 (unsigned) (info_ptr - reader->die_section->buffer),
18114 bfd_get_filename (reader->abfd));
18115 dump_die (*diep, dwarf_die_debug);
18116 }
18117
18118 return result;
18119}
18120\f
18121
18122/* Returns nonzero if TAG represents a type that we might generate a partial
18123 symbol for. */
18124
18125static int
18126is_type_tag_for_partial (int tag)
18127{
18128 switch (tag)
18129 {
18130#if 0
18131 /* Some types that would be reasonable to generate partial symbols for,
18132 that we don't at present. */
18133 case DW_TAG_array_type:
18134 case DW_TAG_file_type:
18135 case DW_TAG_ptr_to_member_type:
18136 case DW_TAG_set_type:
18137 case DW_TAG_string_type:
18138 case DW_TAG_subroutine_type:
18139#endif
18140 case DW_TAG_base_type:
18141 case DW_TAG_class_type:
18142 case DW_TAG_interface_type:
18143 case DW_TAG_enumeration_type:
18144 case DW_TAG_structure_type:
18145 case DW_TAG_subrange_type:
18146 case DW_TAG_typedef:
18147 case DW_TAG_union_type:
18148 return 1;
18149 default:
18150 return 0;
18151 }
18152}
18153
18154/* Load all DIEs that are interesting for partial symbols into memory. */
18155
18156static struct partial_die_info *
18157load_partial_dies (const struct die_reader_specs *reader,
18158 const gdb_byte *info_ptr, int building_psymtab)
18159{
18160 struct dwarf2_cu *cu = reader->cu;
18161 struct objfile *objfile = cu->per_objfile->objfile;
18162 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18163 unsigned int bytes_read;
18164 unsigned int load_all = 0;
18165 int nesting_level = 1;
18166
18167 parent_die = NULL;
18168 last_die = NULL;
18169
18170 gdb_assert (cu->per_cu != NULL);
18171 if (cu->per_cu->load_all_dies)
18172 load_all = 1;
18173
18174 cu->partial_dies
18175 = htab_create_alloc_ex (cu->header.length / 12,
18176 partial_die_hash,
18177 partial_die_eq,
18178 NULL,
18179 &cu->comp_unit_obstack,
18180 hashtab_obstack_allocate,
18181 dummy_obstack_deallocate);
18182
18183 while (1)
18184 {
18185 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18186
18187 /* A NULL abbrev means the end of a series of children. */
18188 if (abbrev == NULL)
18189 {
18190 if (--nesting_level == 0)
18191 return first_die;
18192
18193 info_ptr += bytes_read;
18194 last_die = parent_die;
18195 parent_die = parent_die->die_parent;
18196 continue;
18197 }
18198
18199 /* Check for template arguments. We never save these; if
18200 they're seen, we just mark the parent, and go on our way. */
18201 if (parent_die != NULL
18202 && cu->language == language_cplus
18203 && (abbrev->tag == DW_TAG_template_type_param
18204 || abbrev->tag == DW_TAG_template_value_param))
18205 {
18206 parent_die->has_template_arguments = 1;
18207
18208 if (!load_all)
18209 {
18210 /* We don't need a partial DIE for the template argument. */
18211 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18212 continue;
18213 }
18214 }
18215
18216 /* We only recurse into c++ subprograms looking for template arguments.
18217 Skip their other children. */
18218 if (!load_all
18219 && cu->language == language_cplus
18220 && parent_die != NULL
18221 && parent_die->tag == DW_TAG_subprogram)
18222 {
18223 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18224 continue;
18225 }
18226
18227 /* Check whether this DIE is interesting enough to save. Normally
18228 we would not be interested in members here, but there may be
18229 later variables referencing them via DW_AT_specification (for
18230 static members). */
18231 if (!load_all
18232 && !is_type_tag_for_partial (abbrev->tag)
18233 && abbrev->tag != DW_TAG_constant
18234 && abbrev->tag != DW_TAG_enumerator
18235 && abbrev->tag != DW_TAG_subprogram
18236 && abbrev->tag != DW_TAG_inlined_subroutine
18237 && abbrev->tag != DW_TAG_lexical_block
18238 && abbrev->tag != DW_TAG_variable
18239 && abbrev->tag != DW_TAG_namespace
18240 && abbrev->tag != DW_TAG_module
18241 && abbrev->tag != DW_TAG_member
18242 && abbrev->tag != DW_TAG_imported_unit
18243 && abbrev->tag != DW_TAG_imported_declaration)
18244 {
18245 /* Otherwise we skip to the next sibling, if any. */
18246 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18247 continue;
18248 }
18249
18250 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18251 abbrev);
18252
18253 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18254
18255 /* This two-pass algorithm for processing partial symbols has a
18256 high cost in cache pressure. Thus, handle some simple cases
18257 here which cover the majority of C partial symbols. DIEs
18258 which neither have specification tags in them, nor could have
18259 specification tags elsewhere pointing at them, can simply be
18260 processed and discarded.
18261
18262 This segment is also optional; scan_partial_symbols and
18263 add_partial_symbol will handle these DIEs if we chain
18264 them in normally. When compilers which do not emit large
18265 quantities of duplicate debug information are more common,
18266 this code can probably be removed. */
18267
18268 /* Any complete simple types at the top level (pretty much all
18269 of them, for a language without namespaces), can be processed
18270 directly. */
18271 if (parent_die == NULL
18272 && pdi.has_specification == 0
18273 && pdi.is_declaration == 0
18274 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18275 || pdi.tag == DW_TAG_base_type
18276 || pdi.tag == DW_TAG_subrange_type))
18277 {
18278 if (building_psymtab && pdi.name != NULL)
18279 add_psymbol_to_list (pdi.name, false,
18280 VAR_DOMAIN, LOC_TYPEDEF, -1,
18281 psymbol_placement::STATIC,
18282 0, cu->language, objfile);
18283 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18284 continue;
18285 }
18286
18287 /* The exception for DW_TAG_typedef with has_children above is
18288 a workaround of GCC PR debug/47510. In the case of this complaint
18289 type_name_or_error will error on such types later.
18290
18291 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18292 it could not find the child DIEs referenced later, this is checked
18293 above. In correct DWARF DW_TAG_typedef should have no children. */
18294
18295 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18296 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18297 "- DIE at %s [in module %s]"),
18298 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18299
18300 /* If we're at the second level, and we're an enumerator, and
18301 our parent has no specification (meaning possibly lives in a
18302 namespace elsewhere), then we can add the partial symbol now
18303 instead of queueing it. */
18304 if (pdi.tag == DW_TAG_enumerator
18305 && parent_die != NULL
18306 && parent_die->die_parent == NULL
18307 && parent_die->tag == DW_TAG_enumeration_type
18308 && parent_die->has_specification == 0)
18309 {
18310 if (pdi.name == NULL)
18311 complaint (_("malformed enumerator DIE ignored"));
18312 else if (building_psymtab)
18313 add_psymbol_to_list (pdi.name, false,
18314 VAR_DOMAIN, LOC_CONST, -1,
18315 cu->language == language_cplus
18316 ? psymbol_placement::GLOBAL
18317 : psymbol_placement::STATIC,
18318 0, cu->language, objfile);
18319
18320 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18321 continue;
18322 }
18323
18324 struct partial_die_info *part_die
18325 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18326
18327 /* We'll save this DIE so link it in. */
18328 part_die->die_parent = parent_die;
18329 part_die->die_sibling = NULL;
18330 part_die->die_child = NULL;
18331
18332 if (last_die && last_die == parent_die)
18333 last_die->die_child = part_die;
18334 else if (last_die)
18335 last_die->die_sibling = part_die;
18336
18337 last_die = part_die;
18338
18339 if (first_die == NULL)
18340 first_die = part_die;
18341
18342 /* Maybe add the DIE to the hash table. Not all DIEs that we
18343 find interesting need to be in the hash table, because we
18344 also have the parent/sibling/child chains; only those that we
18345 might refer to by offset later during partial symbol reading.
18346
18347 For now this means things that might have be the target of a
18348 DW_AT_specification, DW_AT_abstract_origin, or
18349 DW_AT_extension. DW_AT_extension will refer only to
18350 namespaces; DW_AT_abstract_origin refers to functions (and
18351 many things under the function DIE, but we do not recurse
18352 into function DIEs during partial symbol reading) and
18353 possibly variables as well; DW_AT_specification refers to
18354 declarations. Declarations ought to have the DW_AT_declaration
18355 flag. It happens that GCC forgets to put it in sometimes, but
18356 only for functions, not for types.
18357
18358 Adding more things than necessary to the hash table is harmless
18359 except for the performance cost. Adding too few will result in
18360 wasted time in find_partial_die, when we reread the compilation
18361 unit with load_all_dies set. */
18362
18363 if (load_all
18364 || abbrev->tag == DW_TAG_constant
18365 || abbrev->tag == DW_TAG_subprogram
18366 || abbrev->tag == DW_TAG_variable
18367 || abbrev->tag == DW_TAG_namespace
18368 || part_die->is_declaration)
18369 {
18370 void **slot;
18371
18372 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18373 to_underlying (part_die->sect_off),
18374 INSERT);
18375 *slot = part_die;
18376 }
18377
18378 /* For some DIEs we want to follow their children (if any). For C
18379 we have no reason to follow the children of structures; for other
18380 languages we have to, so that we can get at method physnames
18381 to infer fully qualified class names, for DW_AT_specification,
18382 and for C++ template arguments. For C++, we also look one level
18383 inside functions to find template arguments (if the name of the
18384 function does not already contain the template arguments).
18385
18386 For Ada and Fortran, we need to scan the children of subprograms
18387 and lexical blocks as well because these languages allow the
18388 definition of nested entities that could be interesting for the
18389 debugger, such as nested subprograms for instance. */
18390 if (last_die->has_children
18391 && (load_all
18392 || last_die->tag == DW_TAG_namespace
18393 || last_die->tag == DW_TAG_module
18394 || last_die->tag == DW_TAG_enumeration_type
18395 || (cu->language == language_cplus
18396 && last_die->tag == DW_TAG_subprogram
18397 && (last_die->name == NULL
18398 || strchr (last_die->name, '<') == NULL))
18399 || (cu->language != language_c
18400 && (last_die->tag == DW_TAG_class_type
18401 || last_die->tag == DW_TAG_interface_type
18402 || last_die->tag == DW_TAG_structure_type
18403 || last_die->tag == DW_TAG_union_type))
18404 || ((cu->language == language_ada
18405 || cu->language == language_fortran)
18406 && (last_die->tag == DW_TAG_subprogram
18407 || last_die->tag == DW_TAG_lexical_block))))
18408 {
18409 nesting_level++;
18410 parent_die = last_die;
18411 continue;
18412 }
18413
18414 /* Otherwise we skip to the next sibling, if any. */
18415 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18416
18417 /* Back to the top, do it again. */
18418 }
18419}
18420
18421partial_die_info::partial_die_info (sect_offset sect_off_,
18422 struct abbrev_info *abbrev)
18423 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18424{
18425}
18426
18427/* Read a minimal amount of information into the minimal die structure.
18428 INFO_PTR should point just after the initial uleb128 of a DIE. */
18429
18430const gdb_byte *
18431partial_die_info::read (const struct die_reader_specs *reader,
18432 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18433{
18434 struct dwarf2_cu *cu = reader->cu;
18435 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18436 unsigned int i;
18437 int has_low_pc_attr = 0;
18438 int has_high_pc_attr = 0;
18439 int high_pc_relative = 0;
18440
18441 for (i = 0; i < abbrev.num_attrs; ++i)
18442 {
18443 attribute attr;
18444 bool need_reprocess;
18445 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18446 info_ptr, &need_reprocess);
18447 /* String and address offsets that need to do the reprocessing have
18448 already been read at this point, so there is no need to wait until
18449 the loop terminates to do the reprocessing. */
18450 if (need_reprocess)
18451 read_attribute_reprocess (reader, &attr);
18452 /* Store the data if it is of an attribute we want to keep in a
18453 partial symbol table. */
18454 switch (attr.name)
18455 {
18456 case DW_AT_name:
18457 switch (tag)
18458 {
18459 case DW_TAG_compile_unit:
18460 case DW_TAG_partial_unit:
18461 case DW_TAG_type_unit:
18462 /* Compilation units have a DW_AT_name that is a filename, not
18463 a source language identifier. */
18464 case DW_TAG_enumeration_type:
18465 case DW_TAG_enumerator:
18466 /* These tags always have simple identifiers already; no need
18467 to canonicalize them. */
18468 name = DW_STRING (&attr);
18469 break;
18470 default:
18471 {
18472 struct objfile *objfile = dwarf2_per_objfile->objfile;
18473
18474 name
18475 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18476 }
18477 break;
18478 }
18479 break;
18480 case DW_AT_linkage_name:
18481 case DW_AT_MIPS_linkage_name:
18482 /* Note that both forms of linkage name might appear. We
18483 assume they will be the same, and we only store the last
18484 one we see. */
18485 linkage_name = attr.value_as_string ();
18486 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18487 See https://github.com/rust-lang/rust/issues/32925. */
18488 if (cu->language == language_rust && linkage_name != NULL
18489 && strchr (linkage_name, '{') != NULL)
18490 linkage_name = NULL;
18491 break;
18492 case DW_AT_low_pc:
18493 has_low_pc_attr = 1;
18494 lowpc = attr.value_as_address ();
18495 break;
18496 case DW_AT_high_pc:
18497 has_high_pc_attr = 1;
18498 highpc = attr.value_as_address ();
18499 if (cu->header.version >= 4 && attr.form_is_constant ())
18500 high_pc_relative = 1;
18501 break;
18502 case DW_AT_location:
18503 /* Support the .debug_loc offsets. */
18504 if (attr.form_is_block ())
18505 {
18506 d.locdesc = DW_BLOCK (&attr);
18507 }
18508 else if (attr.form_is_section_offset ())
18509 {
18510 dwarf2_complex_location_expr_complaint ();
18511 }
18512 else
18513 {
18514 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18515 "partial symbol information");
18516 }
18517 break;
18518 case DW_AT_external:
18519 is_external = DW_UNSND (&attr);
18520 break;
18521 case DW_AT_declaration:
18522 is_declaration = DW_UNSND (&attr);
18523 break;
18524 case DW_AT_type:
18525 has_type = 1;
18526 break;
18527 case DW_AT_abstract_origin:
18528 case DW_AT_specification:
18529 case DW_AT_extension:
18530 has_specification = 1;
18531 spec_offset = attr.get_ref_die_offset ();
18532 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18533 || cu->per_cu->is_dwz);
18534 break;
18535 case DW_AT_sibling:
18536 /* Ignore absolute siblings, they might point outside of
18537 the current compile unit. */
18538 if (attr.form == DW_FORM_ref_addr)
18539 complaint (_("ignoring absolute DW_AT_sibling"));
18540 else
18541 {
18542 const gdb_byte *buffer = reader->buffer;
18543 sect_offset off = attr.get_ref_die_offset ();
18544 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18545
18546 if (sibling_ptr < info_ptr)
18547 complaint (_("DW_AT_sibling points backwards"));
18548 else if (sibling_ptr > reader->buffer_end)
18549 reader->die_section->overflow_complaint ();
18550 else
18551 sibling = sibling_ptr;
18552 }
18553 break;
18554 case DW_AT_byte_size:
18555 has_byte_size = 1;
18556 break;
18557 case DW_AT_const_value:
18558 has_const_value = 1;
18559 break;
18560 case DW_AT_calling_convention:
18561 /* DWARF doesn't provide a way to identify a program's source-level
18562 entry point. DW_AT_calling_convention attributes are only meant
18563 to describe functions' calling conventions.
18564
18565 However, because it's a necessary piece of information in
18566 Fortran, and before DWARF 4 DW_CC_program was the only
18567 piece of debugging information whose definition refers to
18568 a 'main program' at all, several compilers marked Fortran
18569 main programs with DW_CC_program --- even when those
18570 functions use the standard calling conventions.
18571
18572 Although DWARF now specifies a way to provide this
18573 information, we support this practice for backward
18574 compatibility. */
18575 if (DW_UNSND (&attr) == DW_CC_program
18576 && cu->language == language_fortran)
18577 main_subprogram = 1;
18578 break;
18579 case DW_AT_inline:
18580 if (DW_UNSND (&attr) == DW_INL_inlined
18581 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18582 may_be_inlined = 1;
18583 break;
18584
18585 case DW_AT_import:
18586 if (tag == DW_TAG_imported_unit)
18587 {
18588 d.sect_off = attr.get_ref_die_offset ();
18589 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18590 || cu->per_cu->is_dwz);
18591 }
18592 break;
18593
18594 case DW_AT_main_subprogram:
18595 main_subprogram = DW_UNSND (&attr);
18596 break;
18597
18598 case DW_AT_ranges:
18599 {
18600 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18601 but that requires a full DIE, so instead we just
18602 reimplement it. */
18603 int need_ranges_base = tag != DW_TAG_compile_unit;
18604 unsigned int ranges_offset = (DW_UNSND (&attr)
18605 + (need_ranges_base
18606 ? cu->ranges_base
18607 : 0));
18608
18609 /* Value of the DW_AT_ranges attribute is the offset in the
18610 .debug_ranges section. */
18611 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18612 nullptr))
18613 has_pc_info = 1;
18614 }
18615 break;
18616
18617 default:
18618 break;
18619 }
18620 }
18621
18622 /* For Ada, if both the name and the linkage name appear, we prefer
18623 the latter. This lets "catch exception" work better, regardless
18624 of the order in which the name and linkage name were emitted.
18625 Really, though, this is just a workaround for the fact that gdb
18626 doesn't store both the name and the linkage name. */
18627 if (cu->language == language_ada && linkage_name != nullptr)
18628 name = linkage_name;
18629
18630 if (high_pc_relative)
18631 highpc += lowpc;
18632
18633 if (has_low_pc_attr && has_high_pc_attr)
18634 {
18635 /* When using the GNU linker, .gnu.linkonce. sections are used to
18636 eliminate duplicate copies of functions and vtables and such.
18637 The linker will arbitrarily choose one and discard the others.
18638 The AT_*_pc values for such functions refer to local labels in
18639 these sections. If the section from that file was discarded, the
18640 labels are not in the output, so the relocs get a value of 0.
18641 If this is a discarded function, mark the pc bounds as invalid,
18642 so that GDB will ignore it. */
18643 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18644 {
18645 struct objfile *objfile = dwarf2_per_objfile->objfile;
18646 struct gdbarch *gdbarch = objfile->arch ();
18647
18648 complaint (_("DW_AT_low_pc %s is zero "
18649 "for DIE at %s [in module %s]"),
18650 paddress (gdbarch, lowpc),
18651 sect_offset_str (sect_off),
18652 objfile_name (objfile));
18653 }
18654 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18655 else if (lowpc >= highpc)
18656 {
18657 struct objfile *objfile = dwarf2_per_objfile->objfile;
18658 struct gdbarch *gdbarch = objfile->arch ();
18659
18660 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18661 "for DIE at %s [in module %s]"),
18662 paddress (gdbarch, lowpc),
18663 paddress (gdbarch, highpc),
18664 sect_offset_str (sect_off),
18665 objfile_name (objfile));
18666 }
18667 else
18668 has_pc_info = 1;
18669 }
18670
18671 return info_ptr;
18672}
18673
18674/* Find a cached partial DIE at OFFSET in CU. */
18675
18676struct partial_die_info *
18677dwarf2_cu::find_partial_die (sect_offset sect_off)
18678{
18679 struct partial_die_info *lookup_die = NULL;
18680 struct partial_die_info part_die (sect_off);
18681
18682 lookup_die = ((struct partial_die_info *)
18683 htab_find_with_hash (partial_dies, &part_die,
18684 to_underlying (sect_off)));
18685
18686 return lookup_die;
18687}
18688
18689/* Find a partial DIE at OFFSET, which may or may not be in CU,
18690 except in the case of .debug_types DIEs which do not reference
18691 outside their CU (they do however referencing other types via
18692 DW_FORM_ref_sig8). */
18693
18694static const struct cu_partial_die_info
18695find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18696{
18697 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18698 struct objfile *objfile = dwarf2_per_objfile->objfile;
18699 struct dwarf2_per_cu_data *per_cu = NULL;
18700 struct partial_die_info *pd = NULL;
18701
18702 if (offset_in_dwz == cu->per_cu->is_dwz
18703 && cu->header.offset_in_cu_p (sect_off))
18704 {
18705 pd = cu->find_partial_die (sect_off);
18706 if (pd != NULL)
18707 return { cu, pd };
18708 /* We missed recording what we needed.
18709 Load all dies and try again. */
18710 per_cu = cu->per_cu;
18711 }
18712 else
18713 {
18714 /* TUs don't reference other CUs/TUs (except via type signatures). */
18715 if (cu->per_cu->is_debug_types)
18716 {
18717 error (_("Dwarf Error: Type Unit at offset %s contains"
18718 " external reference to offset %s [in module %s].\n"),
18719 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18720 bfd_get_filename (objfile->obfd));
18721 }
18722 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18723 dwarf2_per_objfile);
18724
18725 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18726 load_partial_comp_unit (per_cu, cu->per_objfile);
18727
18728 per_cu->cu->last_used = 0;
18729 pd = per_cu->cu->find_partial_die (sect_off);
18730 }
18731
18732 /* If we didn't find it, and not all dies have been loaded,
18733 load them all and try again. */
18734
18735 if (pd == NULL && per_cu->load_all_dies == 0)
18736 {
18737 per_cu->load_all_dies = 1;
18738
18739 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18740 THIS_CU->cu may already be in use. So we can't just free it and
18741 replace its DIEs with the ones we read in. Instead, we leave those
18742 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18743 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18744 set. */
18745 load_partial_comp_unit (per_cu, cu->per_objfile);
18746
18747 pd = per_cu->cu->find_partial_die (sect_off);
18748 }
18749
18750 if (pd == NULL)
18751 internal_error (__FILE__, __LINE__,
18752 _("could not find partial DIE %s "
18753 "in cache [from module %s]\n"),
18754 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18755 return { per_cu->cu, pd };
18756}
18757
18758/* See if we can figure out if the class lives in a namespace. We do
18759 this by looking for a member function; its demangled name will
18760 contain namespace info, if there is any. */
18761
18762static void
18763guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18764 struct dwarf2_cu *cu)
18765{
18766 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18767 what template types look like, because the demangler
18768 frequently doesn't give the same name as the debug info. We
18769 could fix this by only using the demangled name to get the
18770 prefix (but see comment in read_structure_type). */
18771
18772 struct partial_die_info *real_pdi;
18773 struct partial_die_info *child_pdi;
18774
18775 /* If this DIE (this DIE's specification, if any) has a parent, then
18776 we should not do this. We'll prepend the parent's fully qualified
18777 name when we create the partial symbol. */
18778
18779 real_pdi = struct_pdi;
18780 while (real_pdi->has_specification)
18781 {
18782 auto res = find_partial_die (real_pdi->spec_offset,
18783 real_pdi->spec_is_dwz, cu);
18784 real_pdi = res.pdi;
18785 cu = res.cu;
18786 }
18787
18788 if (real_pdi->die_parent != NULL)
18789 return;
18790
18791 for (child_pdi = struct_pdi->die_child;
18792 child_pdi != NULL;
18793 child_pdi = child_pdi->die_sibling)
18794 {
18795 if (child_pdi->tag == DW_TAG_subprogram
18796 && child_pdi->linkage_name != NULL)
18797 {
18798 gdb::unique_xmalloc_ptr<char> actual_class_name
18799 (language_class_name_from_physname (cu->language_defn,
18800 child_pdi->linkage_name));
18801 if (actual_class_name != NULL)
18802 {
18803 struct objfile *objfile = cu->per_objfile->objfile;
18804 struct_pdi->name = objfile->intern (actual_class_name.get ());
18805 }
18806 break;
18807 }
18808 }
18809}
18810
18811/* Return true if a DIE with TAG may have the DW_AT_const_value
18812 attribute. */
18813
18814static bool
18815can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18816{
18817 switch (tag)
18818 {
18819 case DW_TAG_constant:
18820 case DW_TAG_enumerator:
18821 case DW_TAG_formal_parameter:
18822 case DW_TAG_template_value_param:
18823 case DW_TAG_variable:
18824 return true;
18825 }
18826
18827 return false;
18828}
18829
18830void
18831partial_die_info::fixup (struct dwarf2_cu *cu)
18832{
18833 /* Once we've fixed up a die, there's no point in doing so again.
18834 This also avoids a memory leak if we were to call
18835 guess_partial_die_structure_name multiple times. */
18836 if (fixup_called)
18837 return;
18838
18839 /* If we found a reference attribute and the DIE has no name, try
18840 to find a name in the referred to DIE. */
18841
18842 if (name == NULL && has_specification)
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->name)
18853 {
18854 name = spec_die->name;
18855
18856 /* Copy DW_AT_external attribute if it is set. */
18857 if (spec_die->is_external)
18858 is_external = spec_die->is_external;
18859 }
18860 }
18861
18862 if (!has_const_value && has_specification
18863 && can_have_DW_AT_const_value_p (tag))
18864 {
18865 struct partial_die_info *spec_die;
18866
18867 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18868 spec_die = res.pdi;
18869 cu = res.cu;
18870
18871 spec_die->fixup (cu);
18872
18873 if (spec_die->has_const_value)
18874 {
18875 /* Copy DW_AT_const_value attribute if it is set. */
18876 has_const_value = spec_die->has_const_value;
18877 }
18878 }
18879
18880 /* Set default names for some unnamed DIEs. */
18881
18882 if (name == NULL && tag == DW_TAG_namespace)
18883 name = CP_ANONYMOUS_NAMESPACE_STR;
18884
18885 /* If there is no parent die to provide a namespace, and there are
18886 children, see if we can determine the namespace from their linkage
18887 name. */
18888 if (cu->language == language_cplus
18889 && !cu->per_objfile->per_bfd->types.empty ()
18890 && die_parent == NULL
18891 && has_children
18892 && (tag == DW_TAG_class_type
18893 || tag == DW_TAG_structure_type
18894 || tag == DW_TAG_union_type))
18895 guess_partial_die_structure_name (this, cu);
18896
18897 /* GCC might emit a nameless struct or union that has a linkage
18898 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18899 if (name == NULL
18900 && (tag == DW_TAG_class_type
18901 || tag == DW_TAG_interface_type
18902 || tag == DW_TAG_structure_type
18903 || tag == DW_TAG_union_type)
18904 && linkage_name != NULL)
18905 {
18906 gdb::unique_xmalloc_ptr<char> demangled
18907 (gdb_demangle (linkage_name, DMGL_TYPES));
18908 if (demangled != nullptr)
18909 {
18910 const char *base;
18911
18912 /* Strip any leading namespaces/classes, keep only the base name.
18913 DW_AT_name for named DIEs does not contain the prefixes. */
18914 base = strrchr (demangled.get (), ':');
18915 if (base && base > demangled.get () && base[-1] == ':')
18916 base++;
18917 else
18918 base = demangled.get ();
18919
18920 struct objfile *objfile = cu->per_objfile->objfile;
18921 name = objfile->intern (base);
18922 }
18923 }
18924
18925 fixup_called = 1;
18926}
18927
18928/* Read the .debug_loclists header contents from the given SECTION in the
18929 HEADER. */
18930static void
18931read_loclist_header (struct loclist_header *header,
18932 struct dwarf2_section_info *section)
18933{
18934 unsigned int bytes_read;
18935 bfd *abfd = section->get_bfd_owner ();
18936 const gdb_byte *info_ptr = section->buffer;
18937 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18938 info_ptr += bytes_read;
18939 header->version = read_2_bytes (abfd, info_ptr);
18940 info_ptr += 2;
18941 header->addr_size = read_1_byte (abfd, info_ptr);
18942 info_ptr += 1;
18943 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18944 info_ptr += 1;
18945 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18946}
18947
18948/* Return the DW_AT_loclists_base value for the CU. */
18949static ULONGEST
18950lookup_loclist_base (struct dwarf2_cu *cu)
18951{
18952 /* For the .dwo unit, the loclist_base points to the first offset following
18953 the header. The header consists of the following entities-
18954 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18955 bit format)
18956 2. version (2 bytes)
18957 3. address size (1 byte)
18958 4. segment selector size (1 byte)
18959 5. offset entry count (4 bytes)
18960 These sizes are derived as per the DWARFv5 standard. */
18961 if (cu->dwo_unit != nullptr)
18962 {
18963 if (cu->header.initial_length_size == 4)
18964 return LOCLIST_HEADER_SIZE32;
18965 return LOCLIST_HEADER_SIZE64;
18966 }
18967 return cu->loclist_base;
18968}
18969
18970/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18971 array of offsets in the .debug_loclists section. */
18972static CORE_ADDR
18973read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18974{
18975 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18976 struct objfile *objfile = dwarf2_per_objfile->objfile;
18977 bfd *abfd = objfile->obfd;
18978 ULONGEST loclist_base = lookup_loclist_base (cu);
18979 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18980
18981 section->read (objfile);
18982 if (section->buffer == NULL)
18983 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18984 "section [in module %s]"), objfile_name (objfile));
18985 struct loclist_header header;
18986 read_loclist_header (&header, section);
18987 if (loclist_index >= header.offset_entry_count)
18988 complaint (_("DW_FORM_loclistx pointing outside of "
18989 ".debug_loclists offset array [in module %s]"),
18990 objfile_name (objfile));
18991 if (loclist_base + loclist_index * cu->header.offset_size
18992 >= section->size)
18993 complaint (_("DW_FORM_loclistx pointing outside of "
18994 ".debug_loclists section [in module %s]"),
18995 objfile_name (objfile));
18996 const gdb_byte *info_ptr
18997 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18998
18999 if (cu->header.offset_size == 4)
19000 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19001 else
19002 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19003}
19004
19005/* Process the attributes that had to be skipped in the first round. These
19006 attributes are the ones that need str_offsets_base or addr_base attributes.
19007 They could not have been processed in the first round, because at the time
19008 the values of str_offsets_base or addr_base may not have been known. */
19009static void
19010read_attribute_reprocess (const struct die_reader_specs *reader,
19011 struct attribute *attr)
19012{
19013 struct dwarf2_cu *cu = reader->cu;
19014 switch (attr->form)
19015 {
19016 case DW_FORM_addrx:
19017 case DW_FORM_GNU_addr_index:
19018 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19019 break;
19020 case DW_FORM_loclistx:
19021 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19022 break;
19023 case DW_FORM_strx:
19024 case DW_FORM_strx1:
19025 case DW_FORM_strx2:
19026 case DW_FORM_strx3:
19027 case DW_FORM_strx4:
19028 case DW_FORM_GNU_str_index:
19029 {
19030 unsigned int str_index = DW_UNSND (attr);
19031 if (reader->dwo_file != NULL)
19032 {
19033 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19034 DW_STRING_IS_CANONICAL (attr) = 0;
19035 }
19036 else
19037 {
19038 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19039 DW_STRING_IS_CANONICAL (attr) = 0;
19040 }
19041 break;
19042 }
19043 default:
19044 gdb_assert_not_reached (_("Unexpected DWARF form."));
19045 }
19046}
19047
19048/* Read an attribute value described by an attribute form. */
19049
19050static const gdb_byte *
19051read_attribute_value (const struct die_reader_specs *reader,
19052 struct attribute *attr, unsigned form,
19053 LONGEST implicit_const, const gdb_byte *info_ptr,
19054 bool *need_reprocess)
19055{
19056 struct dwarf2_cu *cu = reader->cu;
19057 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19058 struct objfile *objfile = dwarf2_per_objfile->objfile;
19059 bfd *abfd = reader->abfd;
19060 struct comp_unit_head *cu_header = &cu->header;
19061 unsigned int bytes_read;
19062 struct dwarf_block *blk;
19063 *need_reprocess = false;
19064
19065 attr->form = (enum dwarf_form) form;
19066 switch (form)
19067 {
19068 case DW_FORM_ref_addr:
19069 if (cu->header.version == 2)
19070 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19071 &bytes_read);
19072 else
19073 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19074 &bytes_read);
19075 info_ptr += bytes_read;
19076 break;
19077 case DW_FORM_GNU_ref_alt:
19078 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19079 info_ptr += bytes_read;
19080 break;
19081 case DW_FORM_addr:
19082 {
19083 struct gdbarch *gdbarch = objfile->arch ();
19084 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19085 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19086 info_ptr += bytes_read;
19087 }
19088 break;
19089 case DW_FORM_block2:
19090 blk = dwarf_alloc_block (cu);
19091 blk->size = read_2_bytes (abfd, info_ptr);
19092 info_ptr += 2;
19093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19094 info_ptr += blk->size;
19095 DW_BLOCK (attr) = blk;
19096 break;
19097 case DW_FORM_block4:
19098 blk = dwarf_alloc_block (cu);
19099 blk->size = read_4_bytes (abfd, info_ptr);
19100 info_ptr += 4;
19101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19102 info_ptr += blk->size;
19103 DW_BLOCK (attr) = blk;
19104 break;
19105 case DW_FORM_data2:
19106 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19107 info_ptr += 2;
19108 break;
19109 case DW_FORM_data4:
19110 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19111 info_ptr += 4;
19112 break;
19113 case DW_FORM_data8:
19114 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19115 info_ptr += 8;
19116 break;
19117 case DW_FORM_data16:
19118 blk = dwarf_alloc_block (cu);
19119 blk->size = 16;
19120 blk->data = read_n_bytes (abfd, info_ptr, 16);
19121 info_ptr += 16;
19122 DW_BLOCK (attr) = blk;
19123 break;
19124 case DW_FORM_sec_offset:
19125 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19126 info_ptr += bytes_read;
19127 break;
19128 case DW_FORM_loclistx:
19129 {
19130 *need_reprocess = true;
19131 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19132 info_ptr += bytes_read;
19133 }
19134 break;
19135 case DW_FORM_string:
19136 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19137 DW_STRING_IS_CANONICAL (attr) = 0;
19138 info_ptr += bytes_read;
19139 break;
19140 case DW_FORM_strp:
19141 if (!cu->per_cu->is_dwz)
19142 {
19143 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19144 abfd, info_ptr, cu_header,
19145 &bytes_read);
19146 DW_STRING_IS_CANONICAL (attr) = 0;
19147 info_ptr += bytes_read;
19148 break;
19149 }
19150 /* FALLTHROUGH */
19151 case DW_FORM_line_strp:
19152 if (!cu->per_cu->is_dwz)
19153 {
19154 DW_STRING (attr)
19155 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19156 &bytes_read);
19157 DW_STRING_IS_CANONICAL (attr) = 0;
19158 info_ptr += bytes_read;
19159 break;
19160 }
19161 /* FALLTHROUGH */
19162 case DW_FORM_GNU_strp_alt:
19163 {
19164 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19165 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19166 &bytes_read);
19167
19168 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19169 DW_STRING_IS_CANONICAL (attr) = 0;
19170 info_ptr += bytes_read;
19171 }
19172 break;
19173 case DW_FORM_exprloc:
19174 case DW_FORM_block:
19175 blk = dwarf_alloc_block (cu);
19176 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19179 info_ptr += blk->size;
19180 DW_BLOCK (attr) = blk;
19181 break;
19182 case DW_FORM_block1:
19183 blk = dwarf_alloc_block (cu);
19184 blk->size = read_1_byte (abfd, info_ptr);
19185 info_ptr += 1;
19186 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19187 info_ptr += blk->size;
19188 DW_BLOCK (attr) = blk;
19189 break;
19190 case DW_FORM_data1:
19191 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19192 info_ptr += 1;
19193 break;
19194 case DW_FORM_flag:
19195 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19196 info_ptr += 1;
19197 break;
19198 case DW_FORM_flag_present:
19199 DW_UNSND (attr) = 1;
19200 break;
19201 case DW_FORM_sdata:
19202 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19203 info_ptr += bytes_read;
19204 break;
19205 case DW_FORM_udata:
19206 case DW_FORM_rnglistx:
19207 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19208 info_ptr += bytes_read;
19209 break;
19210 case DW_FORM_ref1:
19211 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19212 + read_1_byte (abfd, info_ptr));
19213 info_ptr += 1;
19214 break;
19215 case DW_FORM_ref2:
19216 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19217 + read_2_bytes (abfd, info_ptr));
19218 info_ptr += 2;
19219 break;
19220 case DW_FORM_ref4:
19221 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19222 + read_4_bytes (abfd, info_ptr));
19223 info_ptr += 4;
19224 break;
19225 case DW_FORM_ref8:
19226 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19227 + read_8_bytes (abfd, info_ptr));
19228 info_ptr += 8;
19229 break;
19230 case DW_FORM_ref_sig8:
19231 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19232 info_ptr += 8;
19233 break;
19234 case DW_FORM_ref_udata:
19235 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19236 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19237 info_ptr += bytes_read;
19238 break;
19239 case DW_FORM_indirect:
19240 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19241 info_ptr += bytes_read;
19242 if (form == DW_FORM_implicit_const)
19243 {
19244 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19245 info_ptr += bytes_read;
19246 }
19247 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19248 info_ptr, need_reprocess);
19249 break;
19250 case DW_FORM_implicit_const:
19251 DW_SND (attr) = implicit_const;
19252 break;
19253 case DW_FORM_addrx:
19254 case DW_FORM_GNU_addr_index:
19255 *need_reprocess = true;
19256 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19257 info_ptr += bytes_read;
19258 break;
19259 case DW_FORM_strx:
19260 case DW_FORM_strx1:
19261 case DW_FORM_strx2:
19262 case DW_FORM_strx3:
19263 case DW_FORM_strx4:
19264 case DW_FORM_GNU_str_index:
19265 {
19266 ULONGEST str_index;
19267 if (form == DW_FORM_strx1)
19268 {
19269 str_index = read_1_byte (abfd, info_ptr);
19270 info_ptr += 1;
19271 }
19272 else if (form == DW_FORM_strx2)
19273 {
19274 str_index = read_2_bytes (abfd, info_ptr);
19275 info_ptr += 2;
19276 }
19277 else if (form == DW_FORM_strx3)
19278 {
19279 str_index = read_3_bytes (abfd, info_ptr);
19280 info_ptr += 3;
19281 }
19282 else if (form == DW_FORM_strx4)
19283 {
19284 str_index = read_4_bytes (abfd, info_ptr);
19285 info_ptr += 4;
19286 }
19287 else
19288 {
19289 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19290 info_ptr += bytes_read;
19291 }
19292 *need_reprocess = true;
19293 DW_UNSND (attr) = str_index;
19294 }
19295 break;
19296 default:
19297 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19298 dwarf_form_name (form),
19299 bfd_get_filename (abfd));
19300 }
19301
19302 /* Super hack. */
19303 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19304 attr->form = DW_FORM_GNU_ref_alt;
19305
19306 /* We have seen instances where the compiler tried to emit a byte
19307 size attribute of -1 which ended up being encoded as an unsigned
19308 0xffffffff. Although 0xffffffff is technically a valid size value,
19309 an object of this size seems pretty unlikely so we can relatively
19310 safely treat these cases as if the size attribute was invalid and
19311 treat them as zero by default. */
19312 if (attr->name == DW_AT_byte_size
19313 && form == DW_FORM_data4
19314 && DW_UNSND (attr) >= 0xffffffff)
19315 {
19316 complaint
19317 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19318 hex_string (DW_UNSND (attr)));
19319 DW_UNSND (attr) = 0;
19320 }
19321
19322 return info_ptr;
19323}
19324
19325/* Read an attribute described by an abbreviated attribute. */
19326
19327static const gdb_byte *
19328read_attribute (const struct die_reader_specs *reader,
19329 struct attribute *attr, struct attr_abbrev *abbrev,
19330 const gdb_byte *info_ptr, bool *need_reprocess)
19331{
19332 attr->name = abbrev->name;
19333 return read_attribute_value (reader, attr, abbrev->form,
19334 abbrev->implicit_const, info_ptr,
19335 need_reprocess);
19336}
19337
19338/* Return pointer to string at .debug_str offset STR_OFFSET. */
19339
19340static const char *
19341read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19342 LONGEST str_offset)
19343{
19344 return dwarf2_per_objfile->per_bfd->str.read_string
19345 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19346}
19347
19348/* Return pointer to string at .debug_str offset as read from BUF.
19349 BUF is assumed to be in a compilation unit described by CU_HEADER.
19350 Return *BYTES_READ_PTR count of bytes read from BUF. */
19351
19352static const char *
19353read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19354 const gdb_byte *buf,
19355 const struct comp_unit_head *cu_header,
19356 unsigned int *bytes_read_ptr)
19357{
19358 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19359
19360 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19361}
19362
19363/* See read.h. */
19364
19365const char *
19366dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19367 const struct comp_unit_head *cu_header,
19368 unsigned int *bytes_read_ptr)
19369{
19370 bfd *abfd = objfile->obfd;
19371 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19372
19373 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19374}
19375
19376/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19377 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19378 ADDR_SIZE is the size of addresses from the CU header. */
19379
19380static CORE_ADDR
19381read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19382 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19383 int addr_size)
19384{
19385 struct objfile *objfile = dwarf2_per_objfile->objfile;
19386 bfd *abfd = objfile->obfd;
19387 const gdb_byte *info_ptr;
19388 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19389
19390 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19391 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19392 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19393 objfile_name (objfile));
19394 if (addr_base_or_zero + addr_index * addr_size
19395 >= dwarf2_per_objfile->per_bfd->addr.size)
19396 error (_("DW_FORM_addr_index pointing outside of "
19397 ".debug_addr section [in module %s]"),
19398 objfile_name (objfile));
19399 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19400 + addr_base_or_zero + addr_index * addr_size);
19401 if (addr_size == 4)
19402 return bfd_get_32 (abfd, info_ptr);
19403 else
19404 return bfd_get_64 (abfd, info_ptr);
19405}
19406
19407/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19408
19409static CORE_ADDR
19410read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19411{
19412 return read_addr_index_1 (cu->per_objfile, addr_index,
19413 cu->addr_base, cu->header.addr_size);
19414}
19415
19416/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19417
19418static CORE_ADDR
19419read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19420 unsigned int *bytes_read)
19421{
19422 bfd *abfd = cu->per_objfile->objfile->obfd;
19423 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19424
19425 return read_addr_index (cu, addr_index);
19426}
19427
19428/* See read.h. */
19429
19430CORE_ADDR
19431dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19432 dwarf2_per_objfile *dwarf2_per_objfile,
19433 unsigned int addr_index)
19434{
19435 struct dwarf2_cu *cu = per_cu->cu;
19436 gdb::optional<ULONGEST> addr_base;
19437 int addr_size;
19438
19439 /* We need addr_base and addr_size.
19440 If we don't have PER_CU->cu, we have to get it.
19441 Nasty, but the alternative is storing the needed info in PER_CU,
19442 which at this point doesn't seem justified: it's not clear how frequently
19443 it would get used and it would increase the size of every PER_CU.
19444 Entry points like dwarf2_per_cu_addr_size do a similar thing
19445 so we're not in uncharted territory here.
19446 Alas we need to be a bit more complicated as addr_base is contained
19447 in the DIE.
19448
19449 We don't need to read the entire CU(/TU).
19450 We just need the header and top level die.
19451
19452 IWBN to use the aging mechanism to let us lazily later discard the CU.
19453 For now we skip this optimization. */
19454
19455 if (cu != NULL)
19456 {
19457 addr_base = cu->addr_base;
19458 addr_size = cu->header.addr_size;
19459 }
19460 else
19461 {
19462 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19463 addr_base = reader.cu->addr_base;
19464 addr_size = reader.cu->header.addr_size;
19465 }
19466
19467 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19468 addr_size);
19469}
19470
19471/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19472 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19473 DWO file. */
19474
19475static const char *
19476read_str_index (struct dwarf2_cu *cu,
19477 struct dwarf2_section_info *str_section,
19478 struct dwarf2_section_info *str_offsets_section,
19479 ULONGEST str_offsets_base, ULONGEST str_index)
19480{
19481 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19482 struct objfile *objfile = dwarf2_per_objfile->objfile;
19483 const char *objf_name = objfile_name (objfile);
19484 bfd *abfd = objfile->obfd;
19485 const gdb_byte *info_ptr;
19486 ULONGEST str_offset;
19487 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19488
19489 str_section->read (objfile);
19490 str_offsets_section->read (objfile);
19491 if (str_section->buffer == NULL)
19492 error (_("%s used without %s section"
19493 " in CU at offset %s [in module %s]"),
19494 form_name, str_section->get_name (),
19495 sect_offset_str (cu->header.sect_off), objf_name);
19496 if (str_offsets_section->buffer == NULL)
19497 error (_("%s used without %s section"
19498 " in CU at offset %s [in module %s]"),
19499 form_name, str_section->get_name (),
19500 sect_offset_str (cu->header.sect_off), objf_name);
19501 info_ptr = (str_offsets_section->buffer
19502 + str_offsets_base
19503 + str_index * cu->header.offset_size);
19504 if (cu->header.offset_size == 4)
19505 str_offset = bfd_get_32 (abfd, info_ptr);
19506 else
19507 str_offset = bfd_get_64 (abfd, info_ptr);
19508 if (str_offset >= str_section->size)
19509 error (_("Offset from %s pointing outside of"
19510 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19511 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19512 return (const char *) (str_section->buffer + str_offset);
19513}
19514
19515/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19516
19517static const char *
19518read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19519{
19520 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19521 ? reader->cu->header.addr_size : 0;
19522 return read_str_index (reader->cu,
19523 &reader->dwo_file->sections.str,
19524 &reader->dwo_file->sections.str_offsets,
19525 str_offsets_base, str_index);
19526}
19527
19528/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19529
19530static const char *
19531read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19532{
19533 struct objfile *objfile = cu->per_objfile->objfile;
19534 const char *objf_name = objfile_name (objfile);
19535 static const char form_name[] = "DW_FORM_GNU_str_index";
19536 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19537
19538 if (!cu->str_offsets_base.has_value ())
19539 error (_("%s used in Fission stub without %s"
19540 " in CU at offset 0x%lx [in module %s]"),
19541 form_name, str_offsets_attr_name,
19542 (long) cu->header.offset_size, objf_name);
19543
19544 return read_str_index (cu,
19545 &cu->per_objfile->per_bfd->str,
19546 &cu->per_objfile->per_bfd->str_offsets,
19547 *cu->str_offsets_base, str_index);
19548}
19549
19550/* Return the length of an LEB128 number in BUF. */
19551
19552static int
19553leb128_size (const gdb_byte *buf)
19554{
19555 const gdb_byte *begin = buf;
19556 gdb_byte byte;
19557
19558 while (1)
19559 {
19560 byte = *buf++;
19561 if ((byte & 128) == 0)
19562 return buf - begin;
19563 }
19564}
19565
19566static void
19567set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19568{
19569 switch (lang)
19570 {
19571 case DW_LANG_C89:
19572 case DW_LANG_C99:
19573 case DW_LANG_C11:
19574 case DW_LANG_C:
19575 case DW_LANG_UPC:
19576 cu->language = language_c;
19577 break;
19578 case DW_LANG_Java:
19579 case DW_LANG_C_plus_plus:
19580 case DW_LANG_C_plus_plus_11:
19581 case DW_LANG_C_plus_plus_14:
19582 cu->language = language_cplus;
19583 break;
19584 case DW_LANG_D:
19585 cu->language = language_d;
19586 break;
19587 case DW_LANG_Fortran77:
19588 case DW_LANG_Fortran90:
19589 case DW_LANG_Fortran95:
19590 case DW_LANG_Fortran03:
19591 case DW_LANG_Fortran08:
19592 cu->language = language_fortran;
19593 break;
19594 case DW_LANG_Go:
19595 cu->language = language_go;
19596 break;
19597 case DW_LANG_Mips_Assembler:
19598 cu->language = language_asm;
19599 break;
19600 case DW_LANG_Ada83:
19601 case DW_LANG_Ada95:
19602 cu->language = language_ada;
19603 break;
19604 case DW_LANG_Modula2:
19605 cu->language = language_m2;
19606 break;
19607 case DW_LANG_Pascal83:
19608 cu->language = language_pascal;
19609 break;
19610 case DW_LANG_ObjC:
19611 cu->language = language_objc;
19612 break;
19613 case DW_LANG_Rust:
19614 case DW_LANG_Rust_old:
19615 cu->language = language_rust;
19616 break;
19617 case DW_LANG_Cobol74:
19618 case DW_LANG_Cobol85:
19619 default:
19620 cu->language = language_minimal;
19621 break;
19622 }
19623 cu->language_defn = language_def (cu->language);
19624}
19625
19626/* Return the named attribute or NULL if not there. */
19627
19628static struct attribute *
19629dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19630{
19631 for (;;)
19632 {
19633 unsigned int i;
19634 struct attribute *spec = NULL;
19635
19636 for (i = 0; i < die->num_attrs; ++i)
19637 {
19638 if (die->attrs[i].name == name)
19639 return &die->attrs[i];
19640 if (die->attrs[i].name == DW_AT_specification
19641 || die->attrs[i].name == DW_AT_abstract_origin)
19642 spec = &die->attrs[i];
19643 }
19644
19645 if (!spec)
19646 break;
19647
19648 die = follow_die_ref (die, spec, &cu);
19649 }
19650
19651 return NULL;
19652}
19653
19654/* Return the string associated with a string-typed attribute, or NULL if it
19655 is either not found or is of an incorrect type. */
19656
19657static const char *
19658dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19659{
19660 struct attribute *attr;
19661 const char *str = NULL;
19662
19663 attr = dwarf2_attr (die, name, cu);
19664
19665 if (attr != NULL)
19666 {
19667 str = attr->value_as_string ();
19668 if (str == nullptr)
19669 complaint (_("string type expected for attribute %s for "
19670 "DIE at %s in module %s"),
19671 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19672 objfile_name (cu->per_objfile->objfile));
19673 }
19674
19675 return str;
19676}
19677
19678/* Return the dwo name or NULL if not present. If present, it is in either
19679 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19680static const char *
19681dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19682{
19683 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19684 if (dwo_name == nullptr)
19685 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19686 return dwo_name;
19687}
19688
19689/* Return non-zero iff the attribute NAME is defined for the given DIE,
19690 and holds a non-zero value. This function should only be used for
19691 DW_FORM_flag or DW_FORM_flag_present attributes. */
19692
19693static int
19694dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19695{
19696 struct attribute *attr = dwarf2_attr (die, name, cu);
19697
19698 return (attr && DW_UNSND (attr));
19699}
19700
19701static int
19702die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19703{
19704 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19705 which value is non-zero. However, we have to be careful with
19706 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19707 (via dwarf2_flag_true_p) follows this attribute. So we may
19708 end up accidently finding a declaration attribute that belongs
19709 to a different DIE referenced by the specification attribute,
19710 even though the given DIE does not have a declaration attribute. */
19711 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19712 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19713}
19714
19715/* Return the die giving the specification for DIE, if there is
19716 one. *SPEC_CU is the CU containing DIE on input, and the CU
19717 containing the return value on output. If there is no
19718 specification, but there is an abstract origin, that is
19719 returned. */
19720
19721static struct die_info *
19722die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19723{
19724 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19725 *spec_cu);
19726
19727 if (spec_attr == NULL)
19728 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19729
19730 if (spec_attr == NULL)
19731 return NULL;
19732 else
19733 return follow_die_ref (die, spec_attr, spec_cu);
19734}
19735
19736/* Stub for free_line_header to match void * callback types. */
19737
19738static void
19739free_line_header_voidp (void *arg)
19740{
19741 struct line_header *lh = (struct line_header *) arg;
19742
19743 delete lh;
19744}
19745
19746/* A convenience function to find the proper .debug_line section for a CU. */
19747
19748static struct dwarf2_section_info *
19749get_debug_line_section (struct dwarf2_cu *cu)
19750{
19751 struct dwarf2_section_info *section;
19752 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19753
19754 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19755 DWO file. */
19756 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19757 section = &cu->dwo_unit->dwo_file->sections.line;
19758 else if (cu->per_cu->is_dwz)
19759 {
19760 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19761
19762 section = &dwz->line;
19763 }
19764 else
19765 section = &dwarf2_per_objfile->per_bfd->line;
19766
19767 return section;
19768}
19769
19770/* Read the statement program header starting at OFFSET in
19771 .debug_line, or .debug_line.dwo. Return a pointer
19772 to a struct line_header, allocated using xmalloc.
19773 Returns NULL if there is a problem reading the header, e.g., if it
19774 has a version we don't understand.
19775
19776 NOTE: the strings in the include directory and file name tables of
19777 the returned object point into the dwarf line section buffer,
19778 and must not be freed. */
19779
19780static line_header_up
19781dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19782{
19783 struct dwarf2_section_info *section;
19784 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19785
19786 section = get_debug_line_section (cu);
19787 section->read (dwarf2_per_objfile->objfile);
19788 if (section->buffer == NULL)
19789 {
19790 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19791 complaint (_("missing .debug_line.dwo section"));
19792 else
19793 complaint (_("missing .debug_line section"));
19794 return 0;
19795 }
19796
19797 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19798 dwarf2_per_objfile, section,
19799 &cu->header);
19800}
19801
19802/* Subroutine of dwarf_decode_lines to simplify it.
19803 Return the file name of the psymtab for the given file_entry.
19804 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19805 If space for the result is malloc'd, *NAME_HOLDER will be set.
19806 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19807
19808static const char *
19809psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19810 const dwarf2_psymtab *pst,
19811 const char *comp_dir,
19812 gdb::unique_xmalloc_ptr<char> *name_holder)
19813{
19814 const char *include_name = fe.name;
19815 const char *include_name_to_compare = include_name;
19816 const char *pst_filename;
19817 int file_is_pst;
19818
19819 const char *dir_name = fe.include_dir (lh);
19820
19821 gdb::unique_xmalloc_ptr<char> hold_compare;
19822 if (!IS_ABSOLUTE_PATH (include_name)
19823 && (dir_name != NULL || comp_dir != NULL))
19824 {
19825 /* Avoid creating a duplicate psymtab for PST.
19826 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19827 Before we do the comparison, however, we need to account
19828 for DIR_NAME and COMP_DIR.
19829 First prepend dir_name (if non-NULL). If we still don't
19830 have an absolute path prepend comp_dir (if non-NULL).
19831 However, the directory we record in the include-file's
19832 psymtab does not contain COMP_DIR (to match the
19833 corresponding symtab(s)).
19834
19835 Example:
19836
19837 bash$ cd /tmp
19838 bash$ gcc -g ./hello.c
19839 include_name = "hello.c"
19840 dir_name = "."
19841 DW_AT_comp_dir = comp_dir = "/tmp"
19842 DW_AT_name = "./hello.c"
19843
19844 */
19845
19846 if (dir_name != NULL)
19847 {
19848 name_holder->reset (concat (dir_name, SLASH_STRING,
19849 include_name, (char *) NULL));
19850 include_name = name_holder->get ();
19851 include_name_to_compare = include_name;
19852 }
19853 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19854 {
19855 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19856 include_name, (char *) NULL));
19857 include_name_to_compare = hold_compare.get ();
19858 }
19859 }
19860
19861 pst_filename = pst->filename;
19862 gdb::unique_xmalloc_ptr<char> copied_name;
19863 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19864 {
19865 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19866 pst_filename, (char *) NULL));
19867 pst_filename = copied_name.get ();
19868 }
19869
19870 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19871
19872 if (file_is_pst)
19873 return NULL;
19874 return include_name;
19875}
19876
19877/* State machine to track the state of the line number program. */
19878
19879class lnp_state_machine
19880{
19881public:
19882 /* Initialize a machine state for the start of a line number
19883 program. */
19884 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19885 bool record_lines_p);
19886
19887 file_entry *current_file ()
19888 {
19889 /* lh->file_names is 0-based, but the file name numbers in the
19890 statement program are 1-based. */
19891 return m_line_header->file_name_at (m_file);
19892 }
19893
19894 /* Record the line in the state machine. END_SEQUENCE is true if
19895 we're processing the end of a sequence. */
19896 void record_line (bool end_sequence);
19897
19898 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19899 nop-out rest of the lines in this sequence. */
19900 void check_line_address (struct dwarf2_cu *cu,
19901 const gdb_byte *line_ptr,
19902 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19903
19904 void handle_set_discriminator (unsigned int discriminator)
19905 {
19906 m_discriminator = discriminator;
19907 m_line_has_non_zero_discriminator |= discriminator != 0;
19908 }
19909
19910 /* Handle DW_LNE_set_address. */
19911 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19912 {
19913 m_op_index = 0;
19914 address += baseaddr;
19915 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19916 }
19917
19918 /* Handle DW_LNS_advance_pc. */
19919 void handle_advance_pc (CORE_ADDR adjust);
19920
19921 /* Handle a special opcode. */
19922 void handle_special_opcode (unsigned char op_code);
19923
19924 /* Handle DW_LNS_advance_line. */
19925 void handle_advance_line (int line_delta)
19926 {
19927 advance_line (line_delta);
19928 }
19929
19930 /* Handle DW_LNS_set_file. */
19931 void handle_set_file (file_name_index file);
19932
19933 /* Handle DW_LNS_negate_stmt. */
19934 void handle_negate_stmt ()
19935 {
19936 m_is_stmt = !m_is_stmt;
19937 }
19938
19939 /* Handle DW_LNS_const_add_pc. */
19940 void handle_const_add_pc ();
19941
19942 /* Handle DW_LNS_fixed_advance_pc. */
19943 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19944 {
19945 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19946 m_op_index = 0;
19947 }
19948
19949 /* Handle DW_LNS_copy. */
19950 void handle_copy ()
19951 {
19952 record_line (false);
19953 m_discriminator = 0;
19954 }
19955
19956 /* Handle DW_LNE_end_sequence. */
19957 void handle_end_sequence ()
19958 {
19959 m_currently_recording_lines = true;
19960 }
19961
19962private:
19963 /* Advance the line by LINE_DELTA. */
19964 void advance_line (int line_delta)
19965 {
19966 m_line += line_delta;
19967
19968 if (line_delta != 0)
19969 m_line_has_non_zero_discriminator = m_discriminator != 0;
19970 }
19971
19972 struct dwarf2_cu *m_cu;
19973
19974 gdbarch *m_gdbarch;
19975
19976 /* True if we're recording lines.
19977 Otherwise we're building partial symtabs and are just interested in
19978 finding include files mentioned by the line number program. */
19979 bool m_record_lines_p;
19980
19981 /* The line number header. */
19982 line_header *m_line_header;
19983
19984 /* These are part of the standard DWARF line number state machine,
19985 and initialized according to the DWARF spec. */
19986
19987 unsigned char m_op_index = 0;
19988 /* The line table index of the current file. */
19989 file_name_index m_file = 1;
19990 unsigned int m_line = 1;
19991
19992 /* These are initialized in the constructor. */
19993
19994 CORE_ADDR m_address;
19995 bool m_is_stmt;
19996 unsigned int m_discriminator;
19997
19998 /* Additional bits of state we need to track. */
19999
20000 /* The last file that we called dwarf2_start_subfile for.
20001 This is only used for TLLs. */
20002 unsigned int m_last_file = 0;
20003 /* The last file a line number was recorded for. */
20004 struct subfile *m_last_subfile = NULL;
20005
20006 /* When true, record the lines we decode. */
20007 bool m_currently_recording_lines = false;
20008
20009 /* The last line number that was recorded, used to coalesce
20010 consecutive entries for the same line. This can happen, for
20011 example, when discriminators are present. PR 17276. */
20012 unsigned int m_last_line = 0;
20013 bool m_line_has_non_zero_discriminator = false;
20014};
20015
20016void
20017lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20018{
20019 CORE_ADDR addr_adj = (((m_op_index + adjust)
20020 / m_line_header->maximum_ops_per_instruction)
20021 * m_line_header->minimum_instruction_length);
20022 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20023 m_op_index = ((m_op_index + adjust)
20024 % m_line_header->maximum_ops_per_instruction);
20025}
20026
20027void
20028lnp_state_machine::handle_special_opcode (unsigned char op_code)
20029{
20030 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20031 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20032 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20033 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20034 / m_line_header->maximum_ops_per_instruction)
20035 * m_line_header->minimum_instruction_length);
20036 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20037 m_op_index = ((m_op_index + adj_opcode_d)
20038 % m_line_header->maximum_ops_per_instruction);
20039
20040 int line_delta = m_line_header->line_base + adj_opcode_r;
20041 advance_line (line_delta);
20042 record_line (false);
20043 m_discriminator = 0;
20044}
20045
20046void
20047lnp_state_machine::handle_set_file (file_name_index file)
20048{
20049 m_file = file;
20050
20051 const file_entry *fe = current_file ();
20052 if (fe == NULL)
20053 dwarf2_debug_line_missing_file_complaint ();
20054 else if (m_record_lines_p)
20055 {
20056 const char *dir = fe->include_dir (m_line_header);
20057
20058 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20059 m_line_has_non_zero_discriminator = m_discriminator != 0;
20060 dwarf2_start_subfile (m_cu, fe->name, dir);
20061 }
20062}
20063
20064void
20065lnp_state_machine::handle_const_add_pc ()
20066{
20067 CORE_ADDR adjust
20068 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20069
20070 CORE_ADDR addr_adj
20071 = (((m_op_index + adjust)
20072 / m_line_header->maximum_ops_per_instruction)
20073 * m_line_header->minimum_instruction_length);
20074
20075 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20076 m_op_index = ((m_op_index + adjust)
20077 % m_line_header->maximum_ops_per_instruction);
20078}
20079
20080/* Return non-zero if we should add LINE to the line number table.
20081 LINE is the line to add, LAST_LINE is the last line that was added,
20082 LAST_SUBFILE is the subfile for LAST_LINE.
20083 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20084 had a non-zero discriminator.
20085
20086 We have to be careful in the presence of discriminators.
20087 E.g., for this line:
20088
20089 for (i = 0; i < 100000; i++);
20090
20091 clang can emit four line number entries for that one line,
20092 each with a different discriminator.
20093 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20094
20095 However, we want gdb to coalesce all four entries into one.
20096 Otherwise the user could stepi into the middle of the line and
20097 gdb would get confused about whether the pc really was in the
20098 middle of the line.
20099
20100 Things are further complicated by the fact that two consecutive
20101 line number entries for the same line is a heuristic used by gcc
20102 to denote the end of the prologue. So we can't just discard duplicate
20103 entries, we have to be selective about it. The heuristic we use is
20104 that we only collapse consecutive entries for the same line if at least
20105 one of those entries has a non-zero discriminator. PR 17276.
20106
20107 Note: Addresses in the line number state machine can never go backwards
20108 within one sequence, thus this coalescing is ok. */
20109
20110static int
20111dwarf_record_line_p (struct dwarf2_cu *cu,
20112 unsigned int line, unsigned int last_line,
20113 int line_has_non_zero_discriminator,
20114 struct subfile *last_subfile)
20115{
20116 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20117 return 1;
20118 if (line != last_line)
20119 return 1;
20120 /* Same line for the same file that we've seen already.
20121 As a last check, for pr 17276, only record the line if the line
20122 has never had a non-zero discriminator. */
20123 if (!line_has_non_zero_discriminator)
20124 return 1;
20125 return 0;
20126}
20127
20128/* Use the CU's builder to record line number LINE beginning at
20129 address ADDRESS in the line table of subfile SUBFILE. */
20130
20131static void
20132dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20133 unsigned int line, CORE_ADDR address, bool is_stmt,
20134 struct dwarf2_cu *cu)
20135{
20136 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20137
20138 if (dwarf_line_debug)
20139 {
20140 fprintf_unfiltered (gdb_stdlog,
20141 "Recording line %u, file %s, address %s\n",
20142 line, lbasename (subfile->name),
20143 paddress (gdbarch, address));
20144 }
20145
20146 if (cu != nullptr)
20147 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20148}
20149
20150/* Subroutine of dwarf_decode_lines_1 to simplify it.
20151 Mark the end of a set of line number records.
20152 The arguments are the same as for dwarf_record_line_1.
20153 If SUBFILE is NULL the request is ignored. */
20154
20155static void
20156dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20157 CORE_ADDR address, struct dwarf2_cu *cu)
20158{
20159 if (subfile == NULL)
20160 return;
20161
20162 if (dwarf_line_debug)
20163 {
20164 fprintf_unfiltered (gdb_stdlog,
20165 "Finishing current line, file %s, address %s\n",
20166 lbasename (subfile->name),
20167 paddress (gdbarch, address));
20168 }
20169
20170 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20171}
20172
20173void
20174lnp_state_machine::record_line (bool end_sequence)
20175{
20176 if (dwarf_line_debug)
20177 {
20178 fprintf_unfiltered (gdb_stdlog,
20179 "Processing actual line %u: file %u,"
20180 " address %s, is_stmt %u, discrim %u%s\n",
20181 m_line, m_file,
20182 paddress (m_gdbarch, m_address),
20183 m_is_stmt, m_discriminator,
20184 (end_sequence ? "\t(end sequence)" : ""));
20185 }
20186
20187 file_entry *fe = current_file ();
20188
20189 if (fe == NULL)
20190 dwarf2_debug_line_missing_file_complaint ();
20191 /* For now we ignore lines not starting on an instruction boundary.
20192 But not when processing end_sequence for compatibility with the
20193 previous version of the code. */
20194 else if (m_op_index == 0 || end_sequence)
20195 {
20196 fe->included_p = 1;
20197 if (m_record_lines_p)
20198 {
20199 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20200 || end_sequence)
20201 {
20202 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20203 m_currently_recording_lines ? m_cu : nullptr);
20204 }
20205
20206 if (!end_sequence)
20207 {
20208 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20209
20210 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20211 m_line_has_non_zero_discriminator,
20212 m_last_subfile))
20213 {
20214 buildsym_compunit *builder = m_cu->get_builder ();
20215 dwarf_record_line_1 (m_gdbarch,
20216 builder->get_current_subfile (),
20217 m_line, m_address, is_stmt,
20218 m_currently_recording_lines ? m_cu : nullptr);
20219 }
20220 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20221 m_last_line = m_line;
20222 }
20223 }
20224 }
20225}
20226
20227lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20228 line_header *lh, bool record_lines_p)
20229{
20230 m_cu = cu;
20231 m_gdbarch = arch;
20232 m_record_lines_p = record_lines_p;
20233 m_line_header = lh;
20234
20235 m_currently_recording_lines = true;
20236
20237 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20238 was a line entry for it so that the backend has a chance to adjust it
20239 and also record it in case it needs it. This is currently used by MIPS
20240 code, cf. `mips_adjust_dwarf2_line'. */
20241 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20242 m_is_stmt = lh->default_is_stmt;
20243 m_discriminator = 0;
20244}
20245
20246void
20247lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20248 const gdb_byte *line_ptr,
20249 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20250{
20251 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20252 the pc range of the CU. However, we restrict the test to only ADDRESS
20253 values of zero to preserve GDB's previous behaviour which is to handle
20254 the specific case of a function being GC'd by the linker. */
20255
20256 if (address == 0 && address < unrelocated_lowpc)
20257 {
20258 /* This line table is for a function which has been
20259 GCd by the linker. Ignore it. PR gdb/12528 */
20260
20261 struct objfile *objfile = cu->per_objfile->objfile;
20262 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20263
20264 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20265 line_offset, objfile_name (objfile));
20266 m_currently_recording_lines = false;
20267 /* Note: m_currently_recording_lines is left as false until we see
20268 DW_LNE_end_sequence. */
20269 }
20270}
20271
20272/* Subroutine of dwarf_decode_lines to simplify it.
20273 Process the line number information in LH.
20274 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20275 program in order to set included_p for every referenced header. */
20276
20277static void
20278dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20279 const int decode_for_pst_p, CORE_ADDR lowpc)
20280{
20281 const gdb_byte *line_ptr, *extended_end;
20282 const gdb_byte *line_end;
20283 unsigned int bytes_read, extended_len;
20284 unsigned char op_code, extended_op;
20285 CORE_ADDR baseaddr;
20286 struct objfile *objfile = cu->per_objfile->objfile;
20287 bfd *abfd = objfile->obfd;
20288 struct gdbarch *gdbarch = objfile->arch ();
20289 /* True if we're recording line info (as opposed to building partial
20290 symtabs and just interested in finding include files mentioned by
20291 the line number program). */
20292 bool record_lines_p = !decode_for_pst_p;
20293
20294 baseaddr = objfile->text_section_offset ();
20295
20296 line_ptr = lh->statement_program_start;
20297 line_end = lh->statement_program_end;
20298
20299 /* Read the statement sequences until there's nothing left. */
20300 while (line_ptr < line_end)
20301 {
20302 /* The DWARF line number program state machine. Reset the state
20303 machine at the start of each sequence. */
20304 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20305 bool end_sequence = false;
20306
20307 if (record_lines_p)
20308 {
20309 /* Start a subfile for the current file of the state
20310 machine. */
20311 const file_entry *fe = state_machine.current_file ();
20312
20313 if (fe != NULL)
20314 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20315 }
20316
20317 /* Decode the table. */
20318 while (line_ptr < line_end && !end_sequence)
20319 {
20320 op_code = read_1_byte (abfd, line_ptr);
20321 line_ptr += 1;
20322
20323 if (op_code >= lh->opcode_base)
20324 {
20325 /* Special opcode. */
20326 state_machine.handle_special_opcode (op_code);
20327 }
20328 else switch (op_code)
20329 {
20330 case DW_LNS_extended_op:
20331 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20332 &bytes_read);
20333 line_ptr += bytes_read;
20334 extended_end = line_ptr + extended_len;
20335 extended_op = read_1_byte (abfd, line_ptr);
20336 line_ptr += 1;
20337 switch (extended_op)
20338 {
20339 case DW_LNE_end_sequence:
20340 state_machine.handle_end_sequence ();
20341 end_sequence = true;
20342 break;
20343 case DW_LNE_set_address:
20344 {
20345 CORE_ADDR address
20346 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20347 line_ptr += bytes_read;
20348
20349 state_machine.check_line_address (cu, line_ptr,
20350 lowpc - baseaddr, address);
20351 state_machine.handle_set_address (baseaddr, address);
20352 }
20353 break;
20354 case DW_LNE_define_file:
20355 {
20356 const char *cur_file;
20357 unsigned int mod_time, length;
20358 dir_index dindex;
20359
20360 cur_file = read_direct_string (abfd, line_ptr,
20361 &bytes_read);
20362 line_ptr += bytes_read;
20363 dindex = (dir_index)
20364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20365 line_ptr += bytes_read;
20366 mod_time =
20367 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369 length =
20370 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20371 line_ptr += bytes_read;
20372 lh->add_file_name (cur_file, dindex, mod_time, length);
20373 }
20374 break;
20375 case DW_LNE_set_discriminator:
20376 {
20377 /* The discriminator is not interesting to the
20378 debugger; just ignore it. We still need to
20379 check its value though:
20380 if there are consecutive entries for the same
20381 (non-prologue) line we want to coalesce them.
20382 PR 17276. */
20383 unsigned int discr
20384 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20385 line_ptr += bytes_read;
20386
20387 state_machine.handle_set_discriminator (discr);
20388 }
20389 break;
20390 default:
20391 complaint (_("mangled .debug_line section"));
20392 return;
20393 }
20394 /* Make sure that we parsed the extended op correctly. If e.g.
20395 we expected a different address size than the producer used,
20396 we may have read the wrong number of bytes. */
20397 if (line_ptr != extended_end)
20398 {
20399 complaint (_("mangled .debug_line section"));
20400 return;
20401 }
20402 break;
20403 case DW_LNS_copy:
20404 state_machine.handle_copy ();
20405 break;
20406 case DW_LNS_advance_pc:
20407 {
20408 CORE_ADDR adjust
20409 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20410 line_ptr += bytes_read;
20411
20412 state_machine.handle_advance_pc (adjust);
20413 }
20414 break;
20415 case DW_LNS_advance_line:
20416 {
20417 int line_delta
20418 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20419 line_ptr += bytes_read;
20420
20421 state_machine.handle_advance_line (line_delta);
20422 }
20423 break;
20424 case DW_LNS_set_file:
20425 {
20426 file_name_index file
20427 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20428 &bytes_read);
20429 line_ptr += bytes_read;
20430
20431 state_machine.handle_set_file (file);
20432 }
20433 break;
20434 case DW_LNS_set_column:
20435 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20436 line_ptr += bytes_read;
20437 break;
20438 case DW_LNS_negate_stmt:
20439 state_machine.handle_negate_stmt ();
20440 break;
20441 case DW_LNS_set_basic_block:
20442 break;
20443 /* Add to the address register of the state machine the
20444 address increment value corresponding to special opcode
20445 255. I.e., this value is scaled by the minimum
20446 instruction length since special opcode 255 would have
20447 scaled the increment. */
20448 case DW_LNS_const_add_pc:
20449 state_machine.handle_const_add_pc ();
20450 break;
20451 case DW_LNS_fixed_advance_pc:
20452 {
20453 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20454 line_ptr += 2;
20455
20456 state_machine.handle_fixed_advance_pc (addr_adj);
20457 }
20458 break;
20459 default:
20460 {
20461 /* Unknown standard opcode, ignore it. */
20462 int i;
20463
20464 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20465 {
20466 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20467 line_ptr += bytes_read;
20468 }
20469 }
20470 }
20471 }
20472
20473 if (!end_sequence)
20474 dwarf2_debug_line_missing_end_sequence_complaint ();
20475
20476 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20477 in which case we still finish recording the last line). */
20478 state_machine.record_line (true);
20479 }
20480}
20481
20482/* Decode the Line Number Program (LNP) for the given line_header
20483 structure and CU. The actual information extracted and the type
20484 of structures created from the LNP depends on the value of PST.
20485
20486 1. If PST is NULL, then this procedure uses the data from the program
20487 to create all necessary symbol tables, and their linetables.
20488
20489 2. If PST is not NULL, this procedure reads the program to determine
20490 the list of files included by the unit represented by PST, and
20491 builds all the associated partial symbol tables.
20492
20493 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20494 It is used for relative paths in the line table.
20495 NOTE: When processing partial symtabs (pst != NULL),
20496 comp_dir == pst->dirname.
20497
20498 NOTE: It is important that psymtabs have the same file name (via strcmp)
20499 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20500 symtab we don't use it in the name of the psymtabs we create.
20501 E.g. expand_line_sal requires this when finding psymtabs to expand.
20502 A good testcase for this is mb-inline.exp.
20503
20504 LOWPC is the lowest address in CU (or 0 if not known).
20505
20506 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20507 for its PC<->lines mapping information. Otherwise only the filename
20508 table is read in. */
20509
20510static void
20511dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20512 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20513 CORE_ADDR lowpc, int decode_mapping)
20514{
20515 struct objfile *objfile = cu->per_objfile->objfile;
20516 const int decode_for_pst_p = (pst != NULL);
20517
20518 if (decode_mapping)
20519 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20520
20521 if (decode_for_pst_p)
20522 {
20523 /* Now that we're done scanning the Line Header Program, we can
20524 create the psymtab of each included file. */
20525 for (auto &file_entry : lh->file_names ())
20526 if (file_entry.included_p == 1)
20527 {
20528 gdb::unique_xmalloc_ptr<char> name_holder;
20529 const char *include_name =
20530 psymtab_include_file_name (lh, file_entry, pst,
20531 comp_dir, &name_holder);
20532 if (include_name != NULL)
20533 dwarf2_create_include_psymtab (include_name, pst, objfile);
20534 }
20535 }
20536 else
20537 {
20538 /* Make sure a symtab is created for every file, even files
20539 which contain only variables (i.e. no code with associated
20540 line numbers). */
20541 buildsym_compunit *builder = cu->get_builder ();
20542 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20543
20544 for (auto &fe : lh->file_names ())
20545 {
20546 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20547 if (builder->get_current_subfile ()->symtab == NULL)
20548 {
20549 builder->get_current_subfile ()->symtab
20550 = allocate_symtab (cust,
20551 builder->get_current_subfile ()->name);
20552 }
20553 fe.symtab = builder->get_current_subfile ()->symtab;
20554 }
20555 }
20556}
20557
20558/* Start a subfile for DWARF. FILENAME is the name of the file and
20559 DIRNAME the name of the source directory which contains FILENAME
20560 or NULL if not known.
20561 This routine tries to keep line numbers from identical absolute and
20562 relative file names in a common subfile.
20563
20564 Using the `list' example from the GDB testsuite, which resides in
20565 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20566 of /srcdir/list0.c yields the following debugging information for list0.c:
20567
20568 DW_AT_name: /srcdir/list0.c
20569 DW_AT_comp_dir: /compdir
20570 files.files[0].name: list0.h
20571 files.files[0].dir: /srcdir
20572 files.files[1].name: list0.c
20573 files.files[1].dir: /srcdir
20574
20575 The line number information for list0.c has to end up in a single
20576 subfile, so that `break /srcdir/list0.c:1' works as expected.
20577 start_subfile will ensure that this happens provided that we pass the
20578 concatenation of files.files[1].dir and files.files[1].name as the
20579 subfile's name. */
20580
20581static void
20582dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20583 const char *dirname)
20584{
20585 gdb::unique_xmalloc_ptr<char> copy;
20586
20587 /* In order not to lose the line information directory,
20588 we concatenate it to the filename when it makes sense.
20589 Note that the Dwarf3 standard says (speaking of filenames in line
20590 information): ``The directory index is ignored for file names
20591 that represent full path names''. Thus ignoring dirname in the
20592 `else' branch below isn't an issue. */
20593
20594 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20595 {
20596 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20597 filename = copy.get ();
20598 }
20599
20600 cu->get_builder ()->start_subfile (filename);
20601}
20602
20603/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20604 buildsym_compunit constructor. */
20605
20606struct compunit_symtab *
20607dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20608 CORE_ADDR low_pc)
20609{
20610 gdb_assert (m_builder == nullptr);
20611
20612 m_builder.reset (new struct buildsym_compunit
20613 (this->per_objfile->objfile,
20614 name, comp_dir, language, low_pc));
20615
20616 list_in_scope = get_builder ()->get_file_symbols ();
20617
20618 get_builder ()->record_debugformat ("DWARF 2");
20619 get_builder ()->record_producer (producer);
20620
20621 processing_has_namespace_info = false;
20622
20623 return get_builder ()->get_compunit_symtab ();
20624}
20625
20626static void
20627var_decode_location (struct attribute *attr, struct symbol *sym,
20628 struct dwarf2_cu *cu)
20629{
20630 struct objfile *objfile = cu->per_objfile->objfile;
20631 struct comp_unit_head *cu_header = &cu->header;
20632
20633 /* NOTE drow/2003-01-30: There used to be a comment and some special
20634 code here to turn a symbol with DW_AT_external and a
20635 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20636 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20637 with some versions of binutils) where shared libraries could have
20638 relocations against symbols in their debug information - the
20639 minimal symbol would have the right address, but the debug info
20640 would not. It's no longer necessary, because we will explicitly
20641 apply relocations when we read in the debug information now. */
20642
20643 /* A DW_AT_location attribute with no contents indicates that a
20644 variable has been optimized away. */
20645 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20646 {
20647 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20648 return;
20649 }
20650
20651 /* Handle one degenerate form of location expression specially, to
20652 preserve GDB's previous behavior when section offsets are
20653 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20654 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20655
20656 if (attr->form_is_block ()
20657 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20658 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20659 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20660 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20661 && (DW_BLOCK (attr)->size
20662 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20663 {
20664 unsigned int dummy;
20665
20666 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20667 SET_SYMBOL_VALUE_ADDRESS
20668 (sym, cu->header.read_address (objfile->obfd,
20669 DW_BLOCK (attr)->data + 1,
20670 &dummy));
20671 else
20672 SET_SYMBOL_VALUE_ADDRESS
20673 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20674 &dummy));
20675 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20676 fixup_symbol_section (sym, objfile);
20677 SET_SYMBOL_VALUE_ADDRESS
20678 (sym,
20679 SYMBOL_VALUE_ADDRESS (sym)
20680 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20681 return;
20682 }
20683
20684 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20685 expression evaluator, and use LOC_COMPUTED only when necessary
20686 (i.e. when the value of a register or memory location is
20687 referenced, or a thread-local block, etc.). Then again, it might
20688 not be worthwhile. I'm assuming that it isn't unless performance
20689 or memory numbers show me otherwise. */
20690
20691 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20692
20693 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20694 cu->has_loclist = true;
20695}
20696
20697/* Given a pointer to a DWARF information entry, figure out if we need
20698 to make a symbol table entry for it, and if so, create a new entry
20699 and return a pointer to it.
20700 If TYPE is NULL, determine symbol type from the die, otherwise
20701 used the passed type.
20702 If SPACE is not NULL, use it to hold the new symbol. If it is
20703 NULL, allocate a new symbol on the objfile's obstack. */
20704
20705static struct symbol *
20706new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20707 struct symbol *space)
20708{
20709 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20710 struct objfile *objfile = dwarf2_per_objfile->objfile;
20711 struct gdbarch *gdbarch = objfile->arch ();
20712 struct symbol *sym = NULL;
20713 const char *name;
20714 struct attribute *attr = NULL;
20715 struct attribute *attr2 = NULL;
20716 CORE_ADDR baseaddr;
20717 struct pending **list_to_add = NULL;
20718
20719 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20720
20721 baseaddr = objfile->text_section_offset ();
20722
20723 name = dwarf2_name (die, cu);
20724 if (name)
20725 {
20726 int suppress_add = 0;
20727
20728 if (space)
20729 sym = space;
20730 else
20731 sym = new (&objfile->objfile_obstack) symbol;
20732 OBJSTAT (objfile, n_syms++);
20733
20734 /* Cache this symbol's name and the name's demangled form (if any). */
20735 sym->set_language (cu->language, &objfile->objfile_obstack);
20736 /* Fortran does not have mangling standard and the mangling does differ
20737 between gfortran, iFort etc. */
20738 const char *physname
20739 = (cu->language == language_fortran
20740 ? dwarf2_full_name (name, die, cu)
20741 : dwarf2_physname (name, die, cu));
20742 const char *linkagename = dw2_linkage_name (die, cu);
20743
20744 if (linkagename == nullptr || cu->language == language_ada)
20745 sym->set_linkage_name (physname);
20746 else
20747 {
20748 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20749 sym->set_linkage_name (linkagename);
20750 }
20751
20752 /* Default assumptions.
20753 Use the passed type or decode it from the die. */
20754 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20755 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20756 if (type != NULL)
20757 SYMBOL_TYPE (sym) = type;
20758 else
20759 SYMBOL_TYPE (sym) = die_type (die, cu);
20760 attr = dwarf2_attr (die,
20761 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20762 cu);
20763 if (attr != nullptr)
20764 {
20765 SYMBOL_LINE (sym) = DW_UNSND (attr);
20766 }
20767
20768 attr = dwarf2_attr (die,
20769 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20770 cu);
20771 if (attr != nullptr)
20772 {
20773 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20774 struct file_entry *fe;
20775
20776 if (cu->line_header != NULL)
20777 fe = cu->line_header->file_name_at (file_index);
20778 else
20779 fe = NULL;
20780
20781 if (fe == NULL)
20782 complaint (_("file index out of range"));
20783 else
20784 symbol_set_symtab (sym, fe->symtab);
20785 }
20786
20787 switch (die->tag)
20788 {
20789 case DW_TAG_label:
20790 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20791 if (attr != nullptr)
20792 {
20793 CORE_ADDR addr;
20794
20795 addr = attr->value_as_address ();
20796 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20797 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20798 }
20799 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20800 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20801 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20802 add_symbol_to_list (sym, cu->list_in_scope);
20803 break;
20804 case DW_TAG_subprogram:
20805 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20806 finish_block. */
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20808 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20809 if ((attr2 && (DW_UNSND (attr2) != 0))
20810 || cu->language == language_ada
20811 || cu->language == language_fortran)
20812 {
20813 /* Subprograms marked external are stored as a global symbol.
20814 Ada and Fortran subprograms, whether marked external or
20815 not, are always stored as a global symbol, because we want
20816 to be able to access them globally. For instance, we want
20817 to be able to break on a nested subprogram without having
20818 to specify the context. */
20819 list_to_add = cu->get_builder ()->get_global_symbols ();
20820 }
20821 else
20822 {
20823 list_to_add = cu->list_in_scope;
20824 }
20825 break;
20826 case DW_TAG_inlined_subroutine:
20827 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20828 finish_block. */
20829 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20830 SYMBOL_INLINED (sym) = 1;
20831 list_to_add = cu->list_in_scope;
20832 break;
20833 case DW_TAG_template_value_param:
20834 suppress_add = 1;
20835 /* Fall through. */
20836 case DW_TAG_constant:
20837 case DW_TAG_variable:
20838 case DW_TAG_member:
20839 /* Compilation with minimal debug info may result in
20840 variables with missing type entries. Change the
20841 misleading `void' type to something sensible. */
20842 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20843 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20844
20845 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20846 /* In the case of DW_TAG_member, we should only be called for
20847 static const members. */
20848 if (die->tag == DW_TAG_member)
20849 {
20850 /* dwarf2_add_field uses die_is_declaration,
20851 so we do the same. */
20852 gdb_assert (die_is_declaration (die, cu));
20853 gdb_assert (attr);
20854 }
20855 if (attr != nullptr)
20856 {
20857 dwarf2_const_value (attr, sym, cu);
20858 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20859 if (!suppress_add)
20860 {
20861 if (attr2 && (DW_UNSND (attr2) != 0))
20862 list_to_add = cu->get_builder ()->get_global_symbols ();
20863 else
20864 list_to_add = cu->list_in_scope;
20865 }
20866 break;
20867 }
20868 attr = dwarf2_attr (die, DW_AT_location, cu);
20869 if (attr != nullptr)
20870 {
20871 var_decode_location (attr, sym, cu);
20872 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20873
20874 /* Fortran explicitly imports any global symbols to the local
20875 scope by DW_TAG_common_block. */
20876 if (cu->language == language_fortran && die->parent
20877 && die->parent->tag == DW_TAG_common_block)
20878 attr2 = NULL;
20879
20880 if (SYMBOL_CLASS (sym) == LOC_STATIC
20881 && SYMBOL_VALUE_ADDRESS (sym) == 0
20882 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20883 {
20884 /* When a static variable is eliminated by the linker,
20885 the corresponding debug information is not stripped
20886 out, but the variable address is set to null;
20887 do not add such variables into symbol table. */
20888 }
20889 else if (attr2 && (DW_UNSND (attr2) != 0))
20890 {
20891 if (SYMBOL_CLASS (sym) == LOC_STATIC
20892 && (objfile->flags & OBJF_MAINLINE) == 0
20893 && dwarf2_per_objfile->per_bfd->can_copy)
20894 {
20895 /* A global static variable might be subject to
20896 copy relocation. We first check for a local
20897 minsym, though, because maybe the symbol was
20898 marked hidden, in which case this would not
20899 apply. */
20900 bound_minimal_symbol found
20901 = (lookup_minimal_symbol_linkage
20902 (sym->linkage_name (), objfile));
20903 if (found.minsym != nullptr)
20904 sym->maybe_copied = 1;
20905 }
20906
20907 /* A variable with DW_AT_external is never static,
20908 but it may be block-scoped. */
20909 list_to_add
20910 = ((cu->list_in_scope
20911 == cu->get_builder ()->get_file_symbols ())
20912 ? cu->get_builder ()->get_global_symbols ()
20913 : cu->list_in_scope);
20914 }
20915 else
20916 list_to_add = cu->list_in_scope;
20917 }
20918 else
20919 {
20920 /* We do not know the address of this symbol.
20921 If it is an external symbol and we have type information
20922 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20923 The address of the variable will then be determined from
20924 the minimal symbol table whenever the variable is
20925 referenced. */
20926 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20927
20928 /* Fortran explicitly imports any global symbols to the local
20929 scope by DW_TAG_common_block. */
20930 if (cu->language == language_fortran && die->parent
20931 && die->parent->tag == DW_TAG_common_block)
20932 {
20933 /* SYMBOL_CLASS doesn't matter here because
20934 read_common_block is going to reset it. */
20935 if (!suppress_add)
20936 list_to_add = cu->list_in_scope;
20937 }
20938 else if (attr2 && (DW_UNSND (attr2) != 0)
20939 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20940 {
20941 /* A variable with DW_AT_external is never static, but it
20942 may be block-scoped. */
20943 list_to_add
20944 = ((cu->list_in_scope
20945 == cu->get_builder ()->get_file_symbols ())
20946 ? cu->get_builder ()->get_global_symbols ()
20947 : cu->list_in_scope);
20948
20949 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20950 }
20951 else if (!die_is_declaration (die, cu))
20952 {
20953 /* Use the default LOC_OPTIMIZED_OUT class. */
20954 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20955 if (!suppress_add)
20956 list_to_add = cu->list_in_scope;
20957 }
20958 }
20959 break;
20960 case DW_TAG_formal_parameter:
20961 {
20962 /* If we are inside a function, mark this as an argument. If
20963 not, we might be looking at an argument to an inlined function
20964 when we do not have enough information to show inlined frames;
20965 pretend it's a local variable in that case so that the user can
20966 still see it. */
20967 struct context_stack *curr
20968 = cu->get_builder ()->get_current_context_stack ();
20969 if (curr != nullptr && curr->name != nullptr)
20970 SYMBOL_IS_ARGUMENT (sym) = 1;
20971 attr = dwarf2_attr (die, DW_AT_location, cu);
20972 if (attr != nullptr)
20973 {
20974 var_decode_location (attr, sym, cu);
20975 }
20976 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20977 if (attr != nullptr)
20978 {
20979 dwarf2_const_value (attr, sym, cu);
20980 }
20981
20982 list_to_add = cu->list_in_scope;
20983 }
20984 break;
20985 case DW_TAG_unspecified_parameters:
20986 /* From varargs functions; gdb doesn't seem to have any
20987 interest in this information, so just ignore it for now.
20988 (FIXME?) */
20989 break;
20990 case DW_TAG_template_type_param:
20991 suppress_add = 1;
20992 /* Fall through. */
20993 case DW_TAG_class_type:
20994 case DW_TAG_interface_type:
20995 case DW_TAG_structure_type:
20996 case DW_TAG_union_type:
20997 case DW_TAG_set_type:
20998 case DW_TAG_enumeration_type:
20999 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21000 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21001
21002 {
21003 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21004 really ever be static objects: otherwise, if you try
21005 to, say, break of a class's method and you're in a file
21006 which doesn't mention that class, it won't work unless
21007 the check for all static symbols in lookup_symbol_aux
21008 saves you. See the OtherFileClass tests in
21009 gdb.c++/namespace.exp. */
21010
21011 if (!suppress_add)
21012 {
21013 buildsym_compunit *builder = cu->get_builder ();
21014 list_to_add
21015 = (cu->list_in_scope == builder->get_file_symbols ()
21016 && cu->language == language_cplus
21017 ? builder->get_global_symbols ()
21018 : cu->list_in_scope);
21019
21020 /* The semantics of C++ state that "struct foo {
21021 ... }" also defines a typedef for "foo". */
21022 if (cu->language == language_cplus
21023 || cu->language == language_ada
21024 || cu->language == language_d
21025 || cu->language == language_rust)
21026 {
21027 /* The symbol's name is already allocated along
21028 with this objfile, so we don't need to
21029 duplicate it for the type. */
21030 if (SYMBOL_TYPE (sym)->name () == 0)
21031 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21032 }
21033 }
21034 }
21035 break;
21036 case DW_TAG_typedef:
21037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21038 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21039 list_to_add = cu->list_in_scope;
21040 break;
21041 case DW_TAG_base_type:
21042 case DW_TAG_subrange_type:
21043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21044 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21045 list_to_add = cu->list_in_scope;
21046 break;
21047 case DW_TAG_enumerator:
21048 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21049 if (attr != nullptr)
21050 {
21051 dwarf2_const_value (attr, sym, cu);
21052 }
21053 {
21054 /* NOTE: carlton/2003-11-10: See comment above in the
21055 DW_TAG_class_type, etc. block. */
21056
21057 list_to_add
21058 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21059 && cu->language == language_cplus
21060 ? cu->get_builder ()->get_global_symbols ()
21061 : cu->list_in_scope);
21062 }
21063 break;
21064 case DW_TAG_imported_declaration:
21065 case DW_TAG_namespace:
21066 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21067 list_to_add = cu->get_builder ()->get_global_symbols ();
21068 break;
21069 case DW_TAG_module:
21070 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21071 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21072 list_to_add = cu->get_builder ()->get_global_symbols ();
21073 break;
21074 case DW_TAG_common_block:
21075 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21076 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21077 add_symbol_to_list (sym, cu->list_in_scope);
21078 break;
21079 default:
21080 /* Not a tag we recognize. Hopefully we aren't processing
21081 trash data, but since we must specifically ignore things
21082 we don't recognize, there is nothing else we should do at
21083 this point. */
21084 complaint (_("unsupported tag: '%s'"),
21085 dwarf_tag_name (die->tag));
21086 break;
21087 }
21088
21089 if (suppress_add)
21090 {
21091 sym->hash_next = objfile->template_symbols;
21092 objfile->template_symbols = sym;
21093 list_to_add = NULL;
21094 }
21095
21096 if (list_to_add != NULL)
21097 add_symbol_to_list (sym, list_to_add);
21098
21099 /* For the benefit of old versions of GCC, check for anonymous
21100 namespaces based on the demangled name. */
21101 if (!cu->processing_has_namespace_info
21102 && cu->language == language_cplus)
21103 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21104 }
21105 return (sym);
21106}
21107
21108/* Given an attr with a DW_FORM_dataN value in host byte order,
21109 zero-extend it as appropriate for the symbol's type. The DWARF
21110 standard (v4) is not entirely clear about the meaning of using
21111 DW_FORM_dataN for a constant with a signed type, where the type is
21112 wider than the data. The conclusion of a discussion on the DWARF
21113 list was that this is unspecified. We choose to always zero-extend
21114 because that is the interpretation long in use by GCC. */
21115
21116static gdb_byte *
21117dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21118 struct dwarf2_cu *cu, LONGEST *value, int bits)
21119{
21120 struct objfile *objfile = cu->per_objfile->objfile;
21121 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21122 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21123 LONGEST l = DW_UNSND (attr);
21124
21125 if (bits < sizeof (*value) * 8)
21126 {
21127 l &= ((LONGEST) 1 << bits) - 1;
21128 *value = l;
21129 }
21130 else if (bits == sizeof (*value) * 8)
21131 *value = l;
21132 else
21133 {
21134 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21135 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21136 return bytes;
21137 }
21138
21139 return NULL;
21140}
21141
21142/* Read a constant value from an attribute. Either set *VALUE, or if
21143 the value does not fit in *VALUE, set *BYTES - either already
21144 allocated on the objfile obstack, or newly allocated on OBSTACK,
21145 or, set *BATON, if we translated the constant to a location
21146 expression. */
21147
21148static void
21149dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21150 const char *name, struct obstack *obstack,
21151 struct dwarf2_cu *cu,
21152 LONGEST *value, const gdb_byte **bytes,
21153 struct dwarf2_locexpr_baton **baton)
21154{
21155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21156 struct objfile *objfile = per_objfile->objfile;
21157 struct comp_unit_head *cu_header = &cu->header;
21158 struct dwarf_block *blk;
21159 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21160 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21161
21162 *value = 0;
21163 *bytes = NULL;
21164 *baton = NULL;
21165
21166 switch (attr->form)
21167 {
21168 case DW_FORM_addr:
21169 case DW_FORM_addrx:
21170 case DW_FORM_GNU_addr_index:
21171 {
21172 gdb_byte *data;
21173
21174 if (TYPE_LENGTH (type) != cu_header->addr_size)
21175 dwarf2_const_value_length_mismatch_complaint (name,
21176 cu_header->addr_size,
21177 TYPE_LENGTH (type));
21178 /* Symbols of this form are reasonably rare, so we just
21179 piggyback on the existing location code rather than writing
21180 a new implementation of symbol_computed_ops. */
21181 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21182 (*baton)->per_objfile = per_objfile;
21183 (*baton)->per_cu = cu->per_cu;
21184 gdb_assert ((*baton)->per_cu);
21185
21186 (*baton)->size = 2 + cu_header->addr_size;
21187 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21188 (*baton)->data = data;
21189
21190 data[0] = DW_OP_addr;
21191 store_unsigned_integer (&data[1], cu_header->addr_size,
21192 byte_order, DW_ADDR (attr));
21193 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21194 }
21195 break;
21196 case DW_FORM_string:
21197 case DW_FORM_strp:
21198 case DW_FORM_strx:
21199 case DW_FORM_GNU_str_index:
21200 case DW_FORM_GNU_strp_alt:
21201 /* DW_STRING is already allocated on the objfile obstack, point
21202 directly to it. */
21203 *bytes = (const gdb_byte *) DW_STRING (attr);
21204 break;
21205 case DW_FORM_block1:
21206 case DW_FORM_block2:
21207 case DW_FORM_block4:
21208 case DW_FORM_block:
21209 case DW_FORM_exprloc:
21210 case DW_FORM_data16:
21211 blk = DW_BLOCK (attr);
21212 if (TYPE_LENGTH (type) != blk->size)
21213 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21214 TYPE_LENGTH (type));
21215 *bytes = blk->data;
21216 break;
21217
21218 /* The DW_AT_const_value attributes are supposed to carry the
21219 symbol's value "represented as it would be on the target
21220 architecture." By the time we get here, it's already been
21221 converted to host endianness, so we just need to sign- or
21222 zero-extend it as appropriate. */
21223 case DW_FORM_data1:
21224 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21225 break;
21226 case DW_FORM_data2:
21227 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21228 break;
21229 case DW_FORM_data4:
21230 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21231 break;
21232 case DW_FORM_data8:
21233 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21234 break;
21235
21236 case DW_FORM_sdata:
21237 case DW_FORM_implicit_const:
21238 *value = DW_SND (attr);
21239 break;
21240
21241 case DW_FORM_udata:
21242 *value = DW_UNSND (attr);
21243 break;
21244
21245 default:
21246 complaint (_("unsupported const value attribute form: '%s'"),
21247 dwarf_form_name (attr->form));
21248 *value = 0;
21249 break;
21250 }
21251}
21252
21253
21254/* Copy constant value from an attribute to a symbol. */
21255
21256static void
21257dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21258 struct dwarf2_cu *cu)
21259{
21260 struct objfile *objfile = cu->per_objfile->objfile;
21261 LONGEST value;
21262 const gdb_byte *bytes;
21263 struct dwarf2_locexpr_baton *baton;
21264
21265 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21266 sym->print_name (),
21267 &objfile->objfile_obstack, cu,
21268 &value, &bytes, &baton);
21269
21270 if (baton != NULL)
21271 {
21272 SYMBOL_LOCATION_BATON (sym) = baton;
21273 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21274 }
21275 else if (bytes != NULL)
21276 {
21277 SYMBOL_VALUE_BYTES (sym) = bytes;
21278 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21279 }
21280 else
21281 {
21282 SYMBOL_VALUE (sym) = value;
21283 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21284 }
21285}
21286
21287/* Return the type of the die in question using its DW_AT_type attribute. */
21288
21289static struct type *
21290die_type (struct die_info *die, struct dwarf2_cu *cu)
21291{
21292 struct attribute *type_attr;
21293
21294 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21295 if (!type_attr)
21296 {
21297 struct objfile *objfile = cu->per_objfile->objfile;
21298 /* A missing DW_AT_type represents a void type. */
21299 return objfile_type (objfile)->builtin_void;
21300 }
21301
21302 return lookup_die_type (die, type_attr, cu);
21303}
21304
21305/* True iff CU's producer generates GNAT Ada auxiliary information
21306 that allows to find parallel types through that information instead
21307 of having to do expensive parallel lookups by type name. */
21308
21309static int
21310need_gnat_info (struct dwarf2_cu *cu)
21311{
21312 /* Assume that the Ada compiler was GNAT, which always produces
21313 the auxiliary information. */
21314 return (cu->language == language_ada);
21315}
21316
21317/* Return the auxiliary type of the die in question using its
21318 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21319 attribute is not present. */
21320
21321static struct type *
21322die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21323{
21324 struct attribute *type_attr;
21325
21326 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21327 if (!type_attr)
21328 return NULL;
21329
21330 return lookup_die_type (die, type_attr, cu);
21331}
21332
21333/* If DIE has a descriptive_type attribute, then set the TYPE's
21334 descriptive type accordingly. */
21335
21336static void
21337set_descriptive_type (struct type *type, struct die_info *die,
21338 struct dwarf2_cu *cu)
21339{
21340 struct type *descriptive_type = die_descriptive_type (die, cu);
21341
21342 if (descriptive_type)
21343 {
21344 ALLOCATE_GNAT_AUX_TYPE (type);
21345 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21346 }
21347}
21348
21349/* Return the containing type of the die in question using its
21350 DW_AT_containing_type attribute. */
21351
21352static struct type *
21353die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21354{
21355 struct attribute *type_attr;
21356 struct objfile *objfile = cu->per_objfile->objfile;
21357
21358 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21359 if (!type_attr)
21360 error (_("Dwarf Error: Problem turning containing type into gdb type "
21361 "[in module %s]"), objfile_name (objfile));
21362
21363 return lookup_die_type (die, type_attr, cu);
21364}
21365
21366/* Return an error marker type to use for the ill formed type in DIE/CU. */
21367
21368static struct type *
21369build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21370{
21371 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21372 struct objfile *objfile = dwarf2_per_objfile->objfile;
21373 char *saved;
21374
21375 std::string message
21376 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21377 objfile_name (objfile),
21378 sect_offset_str (cu->header.sect_off),
21379 sect_offset_str (die->sect_off));
21380 saved = obstack_strdup (&objfile->objfile_obstack, message);
21381
21382 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21383}
21384
21385/* Look up the type of DIE in CU using its type attribute ATTR.
21386 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21387 DW_AT_containing_type.
21388 If there is no type substitute an error marker. */
21389
21390static struct type *
21391lookup_die_type (struct die_info *die, const struct attribute *attr,
21392 struct dwarf2_cu *cu)
21393{
21394 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21395 struct objfile *objfile = dwarf2_per_objfile->objfile;
21396 struct type *this_type;
21397
21398 gdb_assert (attr->name == DW_AT_type
21399 || attr->name == DW_AT_GNAT_descriptive_type
21400 || attr->name == DW_AT_containing_type);
21401
21402 /* First see if we have it cached. */
21403
21404 if (attr->form == DW_FORM_GNU_ref_alt)
21405 {
21406 struct dwarf2_per_cu_data *per_cu;
21407 sect_offset sect_off = attr->get_ref_die_offset ();
21408
21409 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21410 dwarf2_per_objfile);
21411 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21412 }
21413 else if (attr->form_is_ref ())
21414 {
21415 sect_offset sect_off = attr->get_ref_die_offset ();
21416
21417 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21418 dwarf2_per_objfile);
21419 }
21420 else if (attr->form == DW_FORM_ref_sig8)
21421 {
21422 ULONGEST signature = DW_SIGNATURE (attr);
21423
21424 return get_signatured_type (die, signature, cu);
21425 }
21426 else
21427 {
21428 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21429 " at %s [in module %s]"),
21430 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21431 objfile_name (objfile));
21432 return build_error_marker_type (cu, die);
21433 }
21434
21435 /* If not cached we need to read it in. */
21436
21437 if (this_type == NULL)
21438 {
21439 struct die_info *type_die = NULL;
21440 struct dwarf2_cu *type_cu = cu;
21441
21442 if (attr->form_is_ref ())
21443 type_die = follow_die_ref (die, attr, &type_cu);
21444 if (type_die == NULL)
21445 return build_error_marker_type (cu, die);
21446 /* If we find the type now, it's probably because the type came
21447 from an inter-CU reference and the type's CU got expanded before
21448 ours. */
21449 this_type = read_type_die (type_die, type_cu);
21450 }
21451
21452 /* If we still don't have a type use an error marker. */
21453
21454 if (this_type == NULL)
21455 return build_error_marker_type (cu, die);
21456
21457 return this_type;
21458}
21459
21460/* Return the type in DIE, CU.
21461 Returns NULL for invalid types.
21462
21463 This first does a lookup in die_type_hash,
21464 and only reads the die in if necessary.
21465
21466 NOTE: This can be called when reading in partial or full symbols. */
21467
21468static struct type *
21469read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21470{
21471 struct type *this_type;
21472
21473 this_type = get_die_type (die, cu);
21474 if (this_type)
21475 return this_type;
21476
21477 return read_type_die_1 (die, cu);
21478}
21479
21480/* Read the type in DIE, CU.
21481 Returns NULL for invalid types. */
21482
21483static struct type *
21484read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21485{
21486 struct type *this_type = NULL;
21487
21488 switch (die->tag)
21489 {
21490 case DW_TAG_class_type:
21491 case DW_TAG_interface_type:
21492 case DW_TAG_structure_type:
21493 case DW_TAG_union_type:
21494 this_type = read_structure_type (die, cu);
21495 break;
21496 case DW_TAG_enumeration_type:
21497 this_type = read_enumeration_type (die, cu);
21498 break;
21499 case DW_TAG_subprogram:
21500 case DW_TAG_subroutine_type:
21501 case DW_TAG_inlined_subroutine:
21502 this_type = read_subroutine_type (die, cu);
21503 break;
21504 case DW_TAG_array_type:
21505 this_type = read_array_type (die, cu);
21506 break;
21507 case DW_TAG_set_type:
21508 this_type = read_set_type (die, cu);
21509 break;
21510 case DW_TAG_pointer_type:
21511 this_type = read_tag_pointer_type (die, cu);
21512 break;
21513 case DW_TAG_ptr_to_member_type:
21514 this_type = read_tag_ptr_to_member_type (die, cu);
21515 break;
21516 case DW_TAG_reference_type:
21517 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21518 break;
21519 case DW_TAG_rvalue_reference_type:
21520 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21521 break;
21522 case DW_TAG_const_type:
21523 this_type = read_tag_const_type (die, cu);
21524 break;
21525 case DW_TAG_volatile_type:
21526 this_type = read_tag_volatile_type (die, cu);
21527 break;
21528 case DW_TAG_restrict_type:
21529 this_type = read_tag_restrict_type (die, cu);
21530 break;
21531 case DW_TAG_string_type:
21532 this_type = read_tag_string_type (die, cu);
21533 break;
21534 case DW_TAG_typedef:
21535 this_type = read_typedef (die, cu);
21536 break;
21537 case DW_TAG_subrange_type:
21538 this_type = read_subrange_type (die, cu);
21539 break;
21540 case DW_TAG_base_type:
21541 this_type = read_base_type (die, cu);
21542 break;
21543 case DW_TAG_unspecified_type:
21544 this_type = read_unspecified_type (die, cu);
21545 break;
21546 case DW_TAG_namespace:
21547 this_type = read_namespace_type (die, cu);
21548 break;
21549 case DW_TAG_module:
21550 this_type = read_module_type (die, cu);
21551 break;
21552 case DW_TAG_atomic_type:
21553 this_type = read_tag_atomic_type (die, cu);
21554 break;
21555 default:
21556 complaint (_("unexpected tag in read_type_die: '%s'"),
21557 dwarf_tag_name (die->tag));
21558 break;
21559 }
21560
21561 return this_type;
21562}
21563
21564/* See if we can figure out if the class lives in a namespace. We do
21565 this by looking for a member function; its demangled name will
21566 contain namespace info, if there is any.
21567 Return the computed name or NULL.
21568 Space for the result is allocated on the objfile's obstack.
21569 This is the full-die version of guess_partial_die_structure_name.
21570 In this case we know DIE has no useful parent. */
21571
21572static const char *
21573guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21574{
21575 struct die_info *spec_die;
21576 struct dwarf2_cu *spec_cu;
21577 struct die_info *child;
21578 struct objfile *objfile = cu->per_objfile->objfile;
21579
21580 spec_cu = cu;
21581 spec_die = die_specification (die, &spec_cu);
21582 if (spec_die != NULL)
21583 {
21584 die = spec_die;
21585 cu = spec_cu;
21586 }
21587
21588 for (child = die->child;
21589 child != NULL;
21590 child = child->sibling)
21591 {
21592 if (child->tag == DW_TAG_subprogram)
21593 {
21594 const char *linkage_name = dw2_linkage_name (child, cu);
21595
21596 if (linkage_name != NULL)
21597 {
21598 gdb::unique_xmalloc_ptr<char> actual_name
21599 (language_class_name_from_physname (cu->language_defn,
21600 linkage_name));
21601 const char *name = NULL;
21602
21603 if (actual_name != NULL)
21604 {
21605 const char *die_name = dwarf2_name (die, cu);
21606
21607 if (die_name != NULL
21608 && strcmp (die_name, actual_name.get ()) != 0)
21609 {
21610 /* Strip off the class name from the full name.
21611 We want the prefix. */
21612 int die_name_len = strlen (die_name);
21613 int actual_name_len = strlen (actual_name.get ());
21614 const char *ptr = actual_name.get ();
21615
21616 /* Test for '::' as a sanity check. */
21617 if (actual_name_len > die_name_len + 2
21618 && ptr[actual_name_len - die_name_len - 1] == ':')
21619 name = obstack_strndup (
21620 &objfile->per_bfd->storage_obstack,
21621 ptr, actual_name_len - die_name_len - 2);
21622 }
21623 }
21624 return name;
21625 }
21626 }
21627 }
21628
21629 return NULL;
21630}
21631
21632/* GCC might emit a nameless typedef that has a linkage name. Determine the
21633 prefix part in such case. See
21634 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21635
21636static const char *
21637anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21638{
21639 struct attribute *attr;
21640 const char *base;
21641
21642 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21643 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21644 return NULL;
21645
21646 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21647 return NULL;
21648
21649 attr = dw2_linkage_name_attr (die, cu);
21650 if (attr == NULL || DW_STRING (attr) == NULL)
21651 return NULL;
21652
21653 /* dwarf2_name had to be already called. */
21654 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21655
21656 /* Strip the base name, keep any leading namespaces/classes. */
21657 base = strrchr (DW_STRING (attr), ':');
21658 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21659 return "";
21660
21661 struct objfile *objfile = cu->per_objfile->objfile;
21662 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21663 DW_STRING (attr),
21664 &base[-1] - DW_STRING (attr));
21665}
21666
21667/* Return the name of the namespace/class that DIE is defined within,
21668 or "" if we can't tell. The caller should not xfree the result.
21669
21670 For example, if we're within the method foo() in the following
21671 code:
21672
21673 namespace N {
21674 class C {
21675 void foo () {
21676 }
21677 };
21678 }
21679
21680 then determine_prefix on foo's die will return "N::C". */
21681
21682static const char *
21683determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21684{
21685 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21686 struct die_info *parent, *spec_die;
21687 struct dwarf2_cu *spec_cu;
21688 struct type *parent_type;
21689 const char *retval;
21690
21691 if (cu->language != language_cplus
21692 && cu->language != language_fortran && cu->language != language_d
21693 && cu->language != language_rust)
21694 return "";
21695
21696 retval = anonymous_struct_prefix (die, cu);
21697 if (retval)
21698 return retval;
21699
21700 /* We have to be careful in the presence of DW_AT_specification.
21701 For example, with GCC 3.4, given the code
21702
21703 namespace N {
21704 void foo() {
21705 // Definition of N::foo.
21706 }
21707 }
21708
21709 then we'll have a tree of DIEs like this:
21710
21711 1: DW_TAG_compile_unit
21712 2: DW_TAG_namespace // N
21713 3: DW_TAG_subprogram // declaration of N::foo
21714 4: DW_TAG_subprogram // definition of N::foo
21715 DW_AT_specification // refers to die #3
21716
21717 Thus, when processing die #4, we have to pretend that we're in
21718 the context of its DW_AT_specification, namely the contex of die
21719 #3. */
21720 spec_cu = cu;
21721 spec_die = die_specification (die, &spec_cu);
21722 if (spec_die == NULL)
21723 parent = die->parent;
21724 else
21725 {
21726 parent = spec_die->parent;
21727 cu = spec_cu;
21728 }
21729
21730 if (parent == NULL)
21731 return "";
21732 else if (parent->building_fullname)
21733 {
21734 const char *name;
21735 const char *parent_name;
21736
21737 /* It has been seen on RealView 2.2 built binaries,
21738 DW_TAG_template_type_param types actually _defined_ as
21739 children of the parent class:
21740
21741 enum E {};
21742 template class <class Enum> Class{};
21743 Class<enum E> class_e;
21744
21745 1: DW_TAG_class_type (Class)
21746 2: DW_TAG_enumeration_type (E)
21747 3: DW_TAG_enumerator (enum1:0)
21748 3: DW_TAG_enumerator (enum2:1)
21749 ...
21750 2: DW_TAG_template_type_param
21751 DW_AT_type DW_FORM_ref_udata (E)
21752
21753 Besides being broken debug info, it can put GDB into an
21754 infinite loop. Consider:
21755
21756 When we're building the full name for Class<E>, we'll start
21757 at Class, and go look over its template type parameters,
21758 finding E. We'll then try to build the full name of E, and
21759 reach here. We're now trying to build the full name of E,
21760 and look over the parent DIE for containing scope. In the
21761 broken case, if we followed the parent DIE of E, we'd again
21762 find Class, and once again go look at its template type
21763 arguments, etc., etc. Simply don't consider such parent die
21764 as source-level parent of this die (it can't be, the language
21765 doesn't allow it), and break the loop here. */
21766 name = dwarf2_name (die, cu);
21767 parent_name = dwarf2_name (parent, cu);
21768 complaint (_("template param type '%s' defined within parent '%s'"),
21769 name ? name : "<unknown>",
21770 parent_name ? parent_name : "<unknown>");
21771 return "";
21772 }
21773 else
21774 switch (parent->tag)
21775 {
21776 case DW_TAG_namespace:
21777 parent_type = read_type_die (parent, cu);
21778 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21779 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21780 Work around this problem here. */
21781 if (cu->language == language_cplus
21782 && strcmp (parent_type->name (), "::") == 0)
21783 return "";
21784 /* We give a name to even anonymous namespaces. */
21785 return parent_type->name ();
21786 case DW_TAG_class_type:
21787 case DW_TAG_interface_type:
21788 case DW_TAG_structure_type:
21789 case DW_TAG_union_type:
21790 case DW_TAG_module:
21791 parent_type = read_type_die (parent, cu);
21792 if (parent_type->name () != NULL)
21793 return parent_type->name ();
21794 else
21795 /* An anonymous structure is only allowed non-static data
21796 members; no typedefs, no member functions, et cetera.
21797 So it does not need a prefix. */
21798 return "";
21799 case DW_TAG_compile_unit:
21800 case DW_TAG_partial_unit:
21801 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21802 if (cu->language == language_cplus
21803 && !dwarf2_per_objfile->per_bfd->types.empty ()
21804 && die->child != NULL
21805 && (die->tag == DW_TAG_class_type
21806 || die->tag == DW_TAG_structure_type
21807 || die->tag == DW_TAG_union_type))
21808 {
21809 const char *name = guess_full_die_structure_name (die, cu);
21810 if (name != NULL)
21811 return name;
21812 }
21813 return "";
21814 case DW_TAG_subprogram:
21815 /* Nested subroutines in Fortran get a prefix with the name
21816 of the parent's subroutine. */
21817 if (cu->language == language_fortran)
21818 {
21819 if ((die->tag == DW_TAG_subprogram)
21820 && (dwarf2_name (parent, cu) != NULL))
21821 return dwarf2_name (parent, cu);
21822 }
21823 return determine_prefix (parent, cu);
21824 case DW_TAG_enumeration_type:
21825 parent_type = read_type_die (parent, cu);
21826 if (TYPE_DECLARED_CLASS (parent_type))
21827 {
21828 if (parent_type->name () != NULL)
21829 return parent_type->name ();
21830 return "";
21831 }
21832 /* Fall through. */
21833 default:
21834 return determine_prefix (parent, cu);
21835 }
21836}
21837
21838/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21839 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21840 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21841 an obconcat, otherwise allocate storage for the result. The CU argument is
21842 used to determine the language and hence, the appropriate separator. */
21843
21844#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21845
21846static char *
21847typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21848 int physname, struct dwarf2_cu *cu)
21849{
21850 const char *lead = "";
21851 const char *sep;
21852
21853 if (suffix == NULL || suffix[0] == '\0'
21854 || prefix == NULL || prefix[0] == '\0')
21855 sep = "";
21856 else if (cu->language == language_d)
21857 {
21858 /* For D, the 'main' function could be defined in any module, but it
21859 should never be prefixed. */
21860 if (strcmp (suffix, "D main") == 0)
21861 {
21862 prefix = "";
21863 sep = "";
21864 }
21865 else
21866 sep = ".";
21867 }
21868 else if (cu->language == language_fortran && physname)
21869 {
21870 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21871 DW_AT_MIPS_linkage_name is preferred and used instead. */
21872
21873 lead = "__";
21874 sep = "_MOD_";
21875 }
21876 else
21877 sep = "::";
21878
21879 if (prefix == NULL)
21880 prefix = "";
21881 if (suffix == NULL)
21882 suffix = "";
21883
21884 if (obs == NULL)
21885 {
21886 char *retval
21887 = ((char *)
21888 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21889
21890 strcpy (retval, lead);
21891 strcat (retval, prefix);
21892 strcat (retval, sep);
21893 strcat (retval, suffix);
21894 return retval;
21895 }
21896 else
21897 {
21898 /* We have an obstack. */
21899 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21900 }
21901}
21902
21903/* Get name of a die, return NULL if not found. */
21904
21905static const char *
21906dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21907 struct objfile *objfile)
21908{
21909 if (name && cu->language == language_cplus)
21910 {
21911 gdb::unique_xmalloc_ptr<char> canon_name
21912 = cp_canonicalize_string (name);
21913
21914 if (canon_name != nullptr)
21915 name = objfile->intern (canon_name.get ());
21916 }
21917
21918 return name;
21919}
21920
21921/* Get name of a die, return NULL if not found.
21922 Anonymous namespaces are converted to their magic string. */
21923
21924static const char *
21925dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21926{
21927 struct attribute *attr;
21928 struct objfile *objfile = cu->per_objfile->objfile;
21929
21930 attr = dwarf2_attr (die, DW_AT_name, cu);
21931 if ((!attr || !DW_STRING (attr))
21932 && die->tag != DW_TAG_namespace
21933 && die->tag != DW_TAG_class_type
21934 && die->tag != DW_TAG_interface_type
21935 && die->tag != DW_TAG_structure_type
21936 && die->tag != DW_TAG_union_type)
21937 return NULL;
21938
21939 switch (die->tag)
21940 {
21941 case DW_TAG_compile_unit:
21942 case DW_TAG_partial_unit:
21943 /* Compilation units have a DW_AT_name that is a filename, not
21944 a source language identifier. */
21945 case DW_TAG_enumeration_type:
21946 case DW_TAG_enumerator:
21947 /* These tags always have simple identifiers already; no need
21948 to canonicalize them. */
21949 return DW_STRING (attr);
21950
21951 case DW_TAG_namespace:
21952 if (attr != NULL && DW_STRING (attr) != NULL)
21953 return DW_STRING (attr);
21954 return CP_ANONYMOUS_NAMESPACE_STR;
21955
21956 case DW_TAG_class_type:
21957 case DW_TAG_interface_type:
21958 case DW_TAG_structure_type:
21959 case DW_TAG_union_type:
21960 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21961 structures or unions. These were of the form "._%d" in GCC 4.1,
21962 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21963 and GCC 4.4. We work around this problem by ignoring these. */
21964 if (attr && DW_STRING (attr)
21965 && (startswith (DW_STRING (attr), "._")
21966 || startswith (DW_STRING (attr), "<anonymous")))
21967 return NULL;
21968
21969 /* GCC might emit a nameless typedef that has a linkage name. See
21970 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21971 if (!attr || DW_STRING (attr) == NULL)
21972 {
21973 attr = dw2_linkage_name_attr (die, cu);
21974 if (attr == NULL || DW_STRING (attr) == NULL)
21975 return NULL;
21976
21977 /* Avoid demangling DW_STRING (attr) the second time on a second
21978 call for the same DIE. */
21979 if (!DW_STRING_IS_CANONICAL (attr))
21980 {
21981 gdb::unique_xmalloc_ptr<char> demangled
21982 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21983 if (demangled == nullptr)
21984 return nullptr;
21985
21986 DW_STRING (attr) = objfile->intern (demangled.get ());
21987 DW_STRING_IS_CANONICAL (attr) = 1;
21988 }
21989
21990 /* Strip any leading namespaces/classes, keep only the base name.
21991 DW_AT_name for named DIEs does not contain the prefixes. */
21992 const char *base = strrchr (DW_STRING (attr), ':');
21993 if (base && base > DW_STRING (attr) && base[-1] == ':')
21994 return &base[1];
21995 else
21996 return DW_STRING (attr);
21997 }
21998 break;
21999
22000 default:
22001 break;
22002 }
22003
22004 if (!DW_STRING_IS_CANONICAL (attr))
22005 {
22006 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22007 objfile);
22008 DW_STRING_IS_CANONICAL (attr) = 1;
22009 }
22010 return DW_STRING (attr);
22011}
22012
22013/* Return the die that this die in an extension of, or NULL if there
22014 is none. *EXT_CU is the CU containing DIE on input, and the CU
22015 containing the return value on output. */
22016
22017static struct die_info *
22018dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22019{
22020 struct attribute *attr;
22021
22022 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22023 if (attr == NULL)
22024 return NULL;
22025
22026 return follow_die_ref (die, attr, ext_cu);
22027}
22028
22029static void
22030dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22031{
22032 unsigned int i;
22033
22034 print_spaces (indent, f);
22035 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22036 dwarf_tag_name (die->tag), die->abbrev,
22037 sect_offset_str (die->sect_off));
22038
22039 if (die->parent != NULL)
22040 {
22041 print_spaces (indent, f);
22042 fprintf_unfiltered (f, " parent at offset: %s\n",
22043 sect_offset_str (die->parent->sect_off));
22044 }
22045
22046 print_spaces (indent, f);
22047 fprintf_unfiltered (f, " has children: %s\n",
22048 dwarf_bool_name (die->child != NULL));
22049
22050 print_spaces (indent, f);
22051 fprintf_unfiltered (f, " attributes:\n");
22052
22053 for (i = 0; i < die->num_attrs; ++i)
22054 {
22055 print_spaces (indent, f);
22056 fprintf_unfiltered (f, " %s (%s) ",
22057 dwarf_attr_name (die->attrs[i].name),
22058 dwarf_form_name (die->attrs[i].form));
22059
22060 switch (die->attrs[i].form)
22061 {
22062 case DW_FORM_addr:
22063 case DW_FORM_addrx:
22064 case DW_FORM_GNU_addr_index:
22065 fprintf_unfiltered (f, "address: ");
22066 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22067 break;
22068 case DW_FORM_block2:
22069 case DW_FORM_block4:
22070 case DW_FORM_block:
22071 case DW_FORM_block1:
22072 fprintf_unfiltered (f, "block: size %s",
22073 pulongest (DW_BLOCK (&die->attrs[i])->size));
22074 break;
22075 case DW_FORM_exprloc:
22076 fprintf_unfiltered (f, "expression: size %s",
22077 pulongest (DW_BLOCK (&die->attrs[i])->size));
22078 break;
22079 case DW_FORM_data16:
22080 fprintf_unfiltered (f, "constant of 16 bytes");
22081 break;
22082 case DW_FORM_ref_addr:
22083 fprintf_unfiltered (f, "ref address: ");
22084 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22085 break;
22086 case DW_FORM_GNU_ref_alt:
22087 fprintf_unfiltered (f, "alt ref address: ");
22088 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22089 break;
22090 case DW_FORM_ref1:
22091 case DW_FORM_ref2:
22092 case DW_FORM_ref4:
22093 case DW_FORM_ref8:
22094 case DW_FORM_ref_udata:
22095 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22096 (long) (DW_UNSND (&die->attrs[i])));
22097 break;
22098 case DW_FORM_data1:
22099 case DW_FORM_data2:
22100 case DW_FORM_data4:
22101 case DW_FORM_data8:
22102 case DW_FORM_udata:
22103 case DW_FORM_sdata:
22104 fprintf_unfiltered (f, "constant: %s",
22105 pulongest (DW_UNSND (&die->attrs[i])));
22106 break;
22107 case DW_FORM_sec_offset:
22108 fprintf_unfiltered (f, "section offset: %s",
22109 pulongest (DW_UNSND (&die->attrs[i])));
22110 break;
22111 case DW_FORM_ref_sig8:
22112 fprintf_unfiltered (f, "signature: %s",
22113 hex_string (DW_SIGNATURE (&die->attrs[i])));
22114 break;
22115 case DW_FORM_string:
22116 case DW_FORM_strp:
22117 case DW_FORM_line_strp:
22118 case DW_FORM_strx:
22119 case DW_FORM_GNU_str_index:
22120 case DW_FORM_GNU_strp_alt:
22121 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22122 DW_STRING (&die->attrs[i])
22123 ? DW_STRING (&die->attrs[i]) : "",
22124 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22125 break;
22126 case DW_FORM_flag:
22127 if (DW_UNSND (&die->attrs[i]))
22128 fprintf_unfiltered (f, "flag: TRUE");
22129 else
22130 fprintf_unfiltered (f, "flag: FALSE");
22131 break;
22132 case DW_FORM_flag_present:
22133 fprintf_unfiltered (f, "flag: TRUE");
22134 break;
22135 case DW_FORM_indirect:
22136 /* The reader will have reduced the indirect form to
22137 the "base form" so this form should not occur. */
22138 fprintf_unfiltered (f,
22139 "unexpected attribute form: DW_FORM_indirect");
22140 break;
22141 case DW_FORM_implicit_const:
22142 fprintf_unfiltered (f, "constant: %s",
22143 plongest (DW_SND (&die->attrs[i])));
22144 break;
22145 default:
22146 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22147 die->attrs[i].form);
22148 break;
22149 }
22150 fprintf_unfiltered (f, "\n");
22151 }
22152}
22153
22154static void
22155dump_die_for_error (struct die_info *die)
22156{
22157 dump_die_shallow (gdb_stderr, 0, die);
22158}
22159
22160static void
22161dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22162{
22163 int indent = level * 4;
22164
22165 gdb_assert (die != NULL);
22166
22167 if (level >= max_level)
22168 return;
22169
22170 dump_die_shallow (f, indent, die);
22171
22172 if (die->child != NULL)
22173 {
22174 print_spaces (indent, f);
22175 fprintf_unfiltered (f, " Children:");
22176 if (level + 1 < max_level)
22177 {
22178 fprintf_unfiltered (f, "\n");
22179 dump_die_1 (f, level + 1, max_level, die->child);
22180 }
22181 else
22182 {
22183 fprintf_unfiltered (f,
22184 " [not printed, max nesting level reached]\n");
22185 }
22186 }
22187
22188 if (die->sibling != NULL && level > 0)
22189 {
22190 dump_die_1 (f, level, max_level, die->sibling);
22191 }
22192}
22193
22194/* This is called from the pdie macro in gdbinit.in.
22195 It's not static so gcc will keep a copy callable from gdb. */
22196
22197void
22198dump_die (struct die_info *die, int max_level)
22199{
22200 dump_die_1 (gdb_stdlog, 0, max_level, die);
22201}
22202
22203static void
22204store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22205{
22206 void **slot;
22207
22208 slot = htab_find_slot_with_hash (cu->die_hash, die,
22209 to_underlying (die->sect_off),
22210 INSERT);
22211
22212 *slot = die;
22213}
22214
22215/* Follow reference or signature attribute ATTR of SRC_DIE.
22216 On entry *REF_CU is the CU of SRC_DIE.
22217 On exit *REF_CU is the CU of the result. */
22218
22219static struct die_info *
22220follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22221 struct dwarf2_cu **ref_cu)
22222{
22223 struct die_info *die;
22224
22225 if (attr->form_is_ref ())
22226 die = follow_die_ref (src_die, attr, ref_cu);
22227 else if (attr->form == DW_FORM_ref_sig8)
22228 die = follow_die_sig (src_die, attr, ref_cu);
22229 else
22230 {
22231 dump_die_for_error (src_die);
22232 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22233 objfile_name ((*ref_cu)->per_objfile->objfile));
22234 }
22235
22236 return die;
22237}
22238
22239/* Follow reference OFFSET.
22240 On entry *REF_CU is the CU of the source die referencing OFFSET.
22241 On exit *REF_CU is the CU of the result.
22242 Returns NULL if OFFSET is invalid. */
22243
22244static struct die_info *
22245follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22246 struct dwarf2_cu **ref_cu)
22247{
22248 struct die_info temp_die;
22249 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22250 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22251
22252 gdb_assert (cu->per_cu != NULL);
22253
22254 target_cu = cu;
22255
22256 if (cu->per_cu->is_debug_types)
22257 {
22258 /* .debug_types CUs cannot reference anything outside their CU.
22259 If they need to, they have to reference a signatured type via
22260 DW_FORM_ref_sig8. */
22261 if (!cu->header.offset_in_cu_p (sect_off))
22262 return NULL;
22263 }
22264 else if (offset_in_dwz != cu->per_cu->is_dwz
22265 || !cu->header.offset_in_cu_p (sect_off))
22266 {
22267 struct dwarf2_per_cu_data *per_cu;
22268
22269 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22270 dwarf2_per_objfile);
22271
22272 /* If necessary, add it to the queue and load its DIEs. */
22273 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22274 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22275
22276 target_cu = per_cu->cu;
22277 }
22278 else if (cu->dies == NULL)
22279 {
22280 /* We're loading full DIEs during partial symbol reading. */
22281 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22282 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22283 language_minimal);
22284 }
22285
22286 *ref_cu = target_cu;
22287 temp_die.sect_off = sect_off;
22288
22289 if (target_cu != cu)
22290 target_cu->ancestor = cu;
22291
22292 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22293 &temp_die,
22294 to_underlying (sect_off));
22295}
22296
22297/* Follow reference attribute ATTR of SRC_DIE.
22298 On entry *REF_CU is the CU of SRC_DIE.
22299 On exit *REF_CU is the CU of the result. */
22300
22301static struct die_info *
22302follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22303 struct dwarf2_cu **ref_cu)
22304{
22305 sect_offset sect_off = attr->get_ref_die_offset ();
22306 struct dwarf2_cu *cu = *ref_cu;
22307 struct die_info *die;
22308
22309 die = follow_die_offset (sect_off,
22310 (attr->form == DW_FORM_GNU_ref_alt
22311 || cu->per_cu->is_dwz),
22312 ref_cu);
22313 if (!die)
22314 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22315 "at %s [in module %s]"),
22316 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22317 objfile_name (cu->per_objfile->objfile));
22318
22319 return die;
22320}
22321
22322/* See read.h. */
22323
22324struct dwarf2_locexpr_baton
22325dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22326 dwarf2_per_cu_data *per_cu,
22327 dwarf2_per_objfile *dwarf2_per_objfile,
22328 CORE_ADDR (*get_frame_pc) (void *baton),
22329 void *baton, bool resolve_abstract_p)
22330{
22331 struct dwarf2_cu *cu;
22332 struct die_info *die;
22333 struct attribute *attr;
22334 struct dwarf2_locexpr_baton retval;
22335 struct objfile *objfile = dwarf2_per_objfile->objfile;
22336
22337 if (per_cu->cu == NULL)
22338 load_cu (per_cu, dwarf2_per_objfile, false);
22339 cu = per_cu->cu;
22340 if (cu == NULL)
22341 {
22342 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22343 Instead just throw an error, not much else we can do. */
22344 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22345 sect_offset_str (sect_off), objfile_name (objfile));
22346 }
22347
22348 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22349 if (!die)
22350 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22351 sect_offset_str (sect_off), objfile_name (objfile));
22352
22353 attr = dwarf2_attr (die, DW_AT_location, cu);
22354 if (!attr && resolve_abstract_p
22355 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22356 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22357 {
22358 CORE_ADDR pc = (*get_frame_pc) (baton);
22359 CORE_ADDR baseaddr = objfile->text_section_offset ();
22360 struct gdbarch *gdbarch = objfile->arch ();
22361
22362 for (const auto &cand_off
22363 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22364 {
22365 struct dwarf2_cu *cand_cu = cu;
22366 struct die_info *cand
22367 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22368 if (!cand
22369 || !cand->parent
22370 || cand->parent->tag != DW_TAG_subprogram)
22371 continue;
22372
22373 CORE_ADDR pc_low, pc_high;
22374 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22375 if (pc_low == ((CORE_ADDR) -1))
22376 continue;
22377 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22378 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22379 if (!(pc_low <= pc && pc < pc_high))
22380 continue;
22381
22382 die = cand;
22383 attr = dwarf2_attr (die, DW_AT_location, cu);
22384 break;
22385 }
22386 }
22387
22388 if (!attr)
22389 {
22390 /* DWARF: "If there is no such attribute, then there is no effect.".
22391 DATA is ignored if SIZE is 0. */
22392
22393 retval.data = NULL;
22394 retval.size = 0;
22395 }
22396 else if (attr->form_is_section_offset ())
22397 {
22398 struct dwarf2_loclist_baton loclist_baton;
22399 CORE_ADDR pc = (*get_frame_pc) (baton);
22400 size_t size;
22401
22402 fill_in_loclist_baton (cu, &loclist_baton, attr);
22403
22404 retval.data = dwarf2_find_location_expression (&loclist_baton,
22405 &size, pc);
22406 retval.size = size;
22407 }
22408 else
22409 {
22410 if (!attr->form_is_block ())
22411 error (_("Dwarf Error: DIE at %s referenced in module %s "
22412 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22413 sect_offset_str (sect_off), objfile_name (objfile));
22414
22415 retval.data = DW_BLOCK (attr)->data;
22416 retval.size = DW_BLOCK (attr)->size;
22417 }
22418 retval.per_objfile = dwarf2_per_objfile;
22419 retval.per_cu = cu->per_cu;
22420
22421 age_cached_comp_units (dwarf2_per_objfile);
22422
22423 return retval;
22424}
22425
22426/* See read.h. */
22427
22428struct dwarf2_locexpr_baton
22429dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22430 dwarf2_per_cu_data *per_cu,
22431 dwarf2_per_objfile *per_objfile,
22432 CORE_ADDR (*get_frame_pc) (void *baton),
22433 void *baton)
22434{
22435 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22436
22437 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22438 get_frame_pc, baton);
22439}
22440
22441/* Write a constant of a given type as target-ordered bytes into
22442 OBSTACK. */
22443
22444static const gdb_byte *
22445write_constant_as_bytes (struct obstack *obstack,
22446 enum bfd_endian byte_order,
22447 struct type *type,
22448 ULONGEST value,
22449 LONGEST *len)
22450{
22451 gdb_byte *result;
22452
22453 *len = TYPE_LENGTH (type);
22454 result = (gdb_byte *) obstack_alloc (obstack, *len);
22455 store_unsigned_integer (result, *len, byte_order, value);
22456
22457 return result;
22458}
22459
22460/* See read.h. */
22461
22462const gdb_byte *
22463dwarf2_fetch_constant_bytes (sect_offset sect_off,
22464 dwarf2_per_cu_data *per_cu,
22465 dwarf2_per_objfile *per_objfile,
22466 obstack *obstack,
22467 LONGEST *len)
22468{
22469 struct dwarf2_cu *cu;
22470 struct die_info *die;
22471 struct attribute *attr;
22472 const gdb_byte *result = NULL;
22473 struct type *type;
22474 LONGEST value;
22475 enum bfd_endian byte_order;
22476 struct objfile *objfile = per_objfile->objfile;
22477
22478 if (per_cu->cu == NULL)
22479 load_cu (per_cu, per_objfile, false);
22480 cu = per_cu->cu;
22481 if (cu == NULL)
22482 {
22483 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22484 Instead just throw an error, not much else we can do. */
22485 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22486 sect_offset_str (sect_off), objfile_name (objfile));
22487 }
22488
22489 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22490 if (!die)
22491 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22492 sect_offset_str (sect_off), objfile_name (objfile));
22493
22494 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22495 if (attr == NULL)
22496 return NULL;
22497
22498 byte_order = (bfd_big_endian (objfile->obfd)
22499 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22500
22501 switch (attr->form)
22502 {
22503 case DW_FORM_addr:
22504 case DW_FORM_addrx:
22505 case DW_FORM_GNU_addr_index:
22506 {
22507 gdb_byte *tem;
22508
22509 *len = cu->header.addr_size;
22510 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22511 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22512 result = tem;
22513 }
22514 break;
22515 case DW_FORM_string:
22516 case DW_FORM_strp:
22517 case DW_FORM_strx:
22518 case DW_FORM_GNU_str_index:
22519 case DW_FORM_GNU_strp_alt:
22520 /* DW_STRING is already allocated on the objfile obstack, point
22521 directly to it. */
22522 result = (const gdb_byte *) DW_STRING (attr);
22523 *len = strlen (DW_STRING (attr));
22524 break;
22525 case DW_FORM_block1:
22526 case DW_FORM_block2:
22527 case DW_FORM_block4:
22528 case DW_FORM_block:
22529 case DW_FORM_exprloc:
22530 case DW_FORM_data16:
22531 result = DW_BLOCK (attr)->data;
22532 *len = DW_BLOCK (attr)->size;
22533 break;
22534
22535 /* The DW_AT_const_value attributes are supposed to carry the
22536 symbol's value "represented as it would be on the target
22537 architecture." By the time we get here, it's already been
22538 converted to host endianness, so we just need to sign- or
22539 zero-extend it as appropriate. */
22540 case DW_FORM_data1:
22541 type = die_type (die, cu);
22542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22543 if (result == NULL)
22544 result = write_constant_as_bytes (obstack, byte_order,
22545 type, value, len);
22546 break;
22547 case DW_FORM_data2:
22548 type = die_type (die, cu);
22549 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22550 if (result == NULL)
22551 result = write_constant_as_bytes (obstack, byte_order,
22552 type, value, len);
22553 break;
22554 case DW_FORM_data4:
22555 type = die_type (die, cu);
22556 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22557 if (result == NULL)
22558 result = write_constant_as_bytes (obstack, byte_order,
22559 type, value, len);
22560 break;
22561 case DW_FORM_data8:
22562 type = die_type (die, cu);
22563 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22564 if (result == NULL)
22565 result = write_constant_as_bytes (obstack, byte_order,
22566 type, value, len);
22567 break;
22568
22569 case DW_FORM_sdata:
22570 case DW_FORM_implicit_const:
22571 type = die_type (die, cu);
22572 result = write_constant_as_bytes (obstack, byte_order,
22573 type, DW_SND (attr), len);
22574 break;
22575
22576 case DW_FORM_udata:
22577 type = die_type (die, cu);
22578 result = write_constant_as_bytes (obstack, byte_order,
22579 type, DW_UNSND (attr), len);
22580 break;
22581
22582 default:
22583 complaint (_("unsupported const value attribute form: '%s'"),
22584 dwarf_form_name (attr->form));
22585 break;
22586 }
22587
22588 return result;
22589}
22590
22591/* See read.h. */
22592
22593struct type *
22594dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22595 dwarf2_per_cu_data *per_cu,
22596 dwarf2_per_objfile *per_objfile)
22597{
22598 struct dwarf2_cu *cu;
22599 struct die_info *die;
22600
22601 if (per_cu->cu == NULL)
22602 load_cu (per_cu, per_objfile, false);
22603 cu = per_cu->cu;
22604 if (!cu)
22605 return NULL;
22606
22607 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22608 if (!die)
22609 return NULL;
22610
22611 return die_type (die, cu);
22612}
22613
22614/* See read.h. */
22615
22616struct type *
22617dwarf2_get_die_type (cu_offset die_offset,
22618 dwarf2_per_cu_data *per_cu,
22619 dwarf2_per_objfile *per_objfile)
22620{
22621 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22622 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22623}
22624
22625/* Follow type unit SIG_TYPE referenced by SRC_DIE.
22626 On entry *REF_CU is the CU of SRC_DIE.
22627 On exit *REF_CU is the CU of the result.
22628 Returns NULL if the referenced DIE isn't found. */
22629
22630static struct die_info *
22631follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22632 struct dwarf2_cu **ref_cu)
22633{
22634 struct die_info temp_die;
22635 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22636 struct die_info *die;
22637 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22638
22639
22640 /* While it might be nice to assert sig_type->type == NULL here,
22641 we can get here for DW_AT_imported_declaration where we need
22642 the DIE not the type. */
22643
22644 /* If necessary, add it to the queue and load its DIEs. */
22645
22646 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22647 language_minimal))
22648 read_signatured_type (sig_type, dwarf2_per_objfile);
22649
22650 sig_cu = sig_type->per_cu.cu;
22651 gdb_assert (sig_cu != NULL);
22652 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22653 temp_die.sect_off = sig_type->type_offset_in_section;
22654 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22655 to_underlying (temp_die.sect_off));
22656 if (die)
22657 {
22658 /* For .gdb_index version 7 keep track of included TUs.
22659 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22660 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22661 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22662 {
22663 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22664 }
22665
22666 *ref_cu = sig_cu;
22667 if (sig_cu != cu)
22668 sig_cu->ancestor = cu;
22669
22670 return die;
22671 }
22672
22673 return NULL;
22674}
22675
22676/* Follow signatured type referenced by ATTR in SRC_DIE.
22677 On entry *REF_CU is the CU of SRC_DIE.
22678 On exit *REF_CU is the CU of the result.
22679 The result is the DIE of the type.
22680 If the referenced type cannot be found an error is thrown. */
22681
22682static struct die_info *
22683follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22684 struct dwarf2_cu **ref_cu)
22685{
22686 ULONGEST signature = DW_SIGNATURE (attr);
22687 struct signatured_type *sig_type;
22688 struct die_info *die;
22689
22690 gdb_assert (attr->form == DW_FORM_ref_sig8);
22691
22692 sig_type = lookup_signatured_type (*ref_cu, signature);
22693 /* sig_type will be NULL if the signatured type is missing from
22694 the debug info. */
22695 if (sig_type == NULL)
22696 {
22697 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22698 " from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (src_die->sect_off),
22700 objfile_name ((*ref_cu)->per_objfile->objfile));
22701 }
22702
22703 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22704 if (die == NULL)
22705 {
22706 dump_die_for_error (src_die);
22707 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22708 " from DIE at %s [in module %s]"),
22709 hex_string (signature), sect_offset_str (src_die->sect_off),
22710 objfile_name ((*ref_cu)->per_objfile->objfile));
22711 }
22712
22713 return die;
22714}
22715
22716/* Get the type specified by SIGNATURE referenced in DIE/CU,
22717 reading in and processing the type unit if necessary. */
22718
22719static struct type *
22720get_signatured_type (struct die_info *die, ULONGEST signature,
22721 struct dwarf2_cu *cu)
22722{
22723 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22724 struct signatured_type *sig_type;
22725 struct dwarf2_cu *type_cu;
22726 struct die_info *type_die;
22727 struct type *type;
22728
22729 sig_type = lookup_signatured_type (cu, signature);
22730 /* sig_type will be NULL if the signatured type is missing from
22731 the debug info. */
22732 if (sig_type == NULL)
22733 {
22734 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22735 " from DIE at %s [in module %s]"),
22736 hex_string (signature), sect_offset_str (die->sect_off),
22737 objfile_name (dwarf2_per_objfile->objfile));
22738 return build_error_marker_type (cu, die);
22739 }
22740
22741 /* If we already know the type we're done. */
22742 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22743 if (type != nullptr)
22744 return type;
22745
22746 type_cu = cu;
22747 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22748 if (type_die != NULL)
22749 {
22750 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22751 is created. This is important, for example, because for c++ classes
22752 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22753 type = read_type_die (type_die, type_cu);
22754 if (type == NULL)
22755 {
22756 complaint (_("Dwarf Error: Cannot build signatured type %s"
22757 " referenced from DIE at %s [in module %s]"),
22758 hex_string (signature), sect_offset_str (die->sect_off),
22759 objfile_name (dwarf2_per_objfile->objfile));
22760 type = build_error_marker_type (cu, die);
22761 }
22762 }
22763 else
22764 {
22765 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22766 " from DIE at %s [in module %s]"),
22767 hex_string (signature), sect_offset_str (die->sect_off),
22768 objfile_name (dwarf2_per_objfile->objfile));
22769 type = build_error_marker_type (cu, die);
22770 }
22771
22772 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22773
22774 return type;
22775}
22776
22777/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22778 reading in and processing the type unit if necessary. */
22779
22780static struct type *
22781get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22782 struct dwarf2_cu *cu) /* ARI: editCase function */
22783{
22784 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22785 if (attr->form_is_ref ())
22786 {
22787 struct dwarf2_cu *type_cu = cu;
22788 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22789
22790 return read_type_die (type_die, type_cu);
22791 }
22792 else if (attr->form == DW_FORM_ref_sig8)
22793 {
22794 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22795 }
22796 else
22797 {
22798 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22799
22800 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22801 " at %s [in module %s]"),
22802 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22803 objfile_name (dwarf2_per_objfile->objfile));
22804 return build_error_marker_type (cu, die);
22805 }
22806}
22807
22808/* Load the DIEs associated with type unit PER_CU into memory. */
22809
22810static void
22811load_full_type_unit (dwarf2_per_cu_data *per_cu,
22812 dwarf2_per_objfile *per_objfile)
22813{
22814 struct signatured_type *sig_type;
22815
22816 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22817 gdb_assert (! per_cu->type_unit_group_p ());
22818
22819 /* We have the per_cu, but we need the signatured_type.
22820 Fortunately this is an easy translation. */
22821 gdb_assert (per_cu->is_debug_types);
22822 sig_type = (struct signatured_type *) per_cu;
22823
22824 gdb_assert (per_cu->cu == NULL);
22825
22826 read_signatured_type (sig_type, per_objfile);
22827
22828 gdb_assert (per_cu->cu != NULL);
22829}
22830
22831/* Read in a signatured type and build its CU and DIEs.
22832 If the type is a stub for the real type in a DWO file,
22833 read in the real type from the DWO file as well. */
22834
22835static void
22836read_signatured_type (signatured_type *sig_type,
22837 dwarf2_per_objfile *per_objfile)
22838{
22839 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22840
22841 gdb_assert (per_cu->is_debug_types);
22842 gdb_assert (per_cu->cu == NULL);
22843
22844 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22845
22846 if (!reader.dummy_p)
22847 {
22848 struct dwarf2_cu *cu = reader.cu;
22849 const gdb_byte *info_ptr = reader.info_ptr;
22850
22851 gdb_assert (cu->die_hash == NULL);
22852 cu->die_hash =
22853 htab_create_alloc_ex (cu->header.length / 12,
22854 die_hash,
22855 die_eq,
22856 NULL,
22857 &cu->comp_unit_obstack,
22858 hashtab_obstack_allocate,
22859 dummy_obstack_deallocate);
22860
22861 if (reader.comp_unit_die->has_children)
22862 reader.comp_unit_die->child
22863 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22864 reader.comp_unit_die);
22865 cu->dies = reader.comp_unit_die;
22866 /* comp_unit_die is not stored in die_hash, no need. */
22867
22868 /* We try not to read any attributes in this function, because
22869 not all CUs needed for references have been loaded yet, and
22870 symbol table processing isn't initialized. But we have to
22871 set the CU language, or we won't be able to build types
22872 correctly. Similarly, if we do not read the producer, we can
22873 not apply producer-specific interpretation. */
22874 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22875
22876 reader.keep ();
22877 }
22878
22879 sig_type->per_cu.tu_read = 1;
22880}
22881
22882/* Decode simple location descriptions.
22883 Given a pointer to a dwarf block that defines a location, compute
22884 the location and return the value. If COMPUTED is non-null, it is
22885 set to true to indicate that decoding was successful, and false
22886 otherwise. If COMPUTED is null, then this function may emit a
22887 complaint. */
22888
22889static CORE_ADDR
22890decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22891{
22892 struct objfile *objfile = cu->per_objfile->objfile;
22893 size_t i;
22894 size_t size = blk->size;
22895 const gdb_byte *data = blk->data;
22896 CORE_ADDR stack[64];
22897 int stacki;
22898 unsigned int bytes_read, unsnd;
22899 gdb_byte op;
22900
22901 if (computed != nullptr)
22902 *computed = false;
22903
22904 i = 0;
22905 stacki = 0;
22906 stack[stacki] = 0;
22907 stack[++stacki] = 0;
22908
22909 while (i < size)
22910 {
22911 op = data[i++];
22912 switch (op)
22913 {
22914 case DW_OP_lit0:
22915 case DW_OP_lit1:
22916 case DW_OP_lit2:
22917 case DW_OP_lit3:
22918 case DW_OP_lit4:
22919 case DW_OP_lit5:
22920 case DW_OP_lit6:
22921 case DW_OP_lit7:
22922 case DW_OP_lit8:
22923 case DW_OP_lit9:
22924 case DW_OP_lit10:
22925 case DW_OP_lit11:
22926 case DW_OP_lit12:
22927 case DW_OP_lit13:
22928 case DW_OP_lit14:
22929 case DW_OP_lit15:
22930 case DW_OP_lit16:
22931 case DW_OP_lit17:
22932 case DW_OP_lit18:
22933 case DW_OP_lit19:
22934 case DW_OP_lit20:
22935 case DW_OP_lit21:
22936 case DW_OP_lit22:
22937 case DW_OP_lit23:
22938 case DW_OP_lit24:
22939 case DW_OP_lit25:
22940 case DW_OP_lit26:
22941 case DW_OP_lit27:
22942 case DW_OP_lit28:
22943 case DW_OP_lit29:
22944 case DW_OP_lit30:
22945 case DW_OP_lit31:
22946 stack[++stacki] = op - DW_OP_lit0;
22947 break;
22948
22949 case DW_OP_reg0:
22950 case DW_OP_reg1:
22951 case DW_OP_reg2:
22952 case DW_OP_reg3:
22953 case DW_OP_reg4:
22954 case DW_OP_reg5:
22955 case DW_OP_reg6:
22956 case DW_OP_reg7:
22957 case DW_OP_reg8:
22958 case DW_OP_reg9:
22959 case DW_OP_reg10:
22960 case DW_OP_reg11:
22961 case DW_OP_reg12:
22962 case DW_OP_reg13:
22963 case DW_OP_reg14:
22964 case DW_OP_reg15:
22965 case DW_OP_reg16:
22966 case DW_OP_reg17:
22967 case DW_OP_reg18:
22968 case DW_OP_reg19:
22969 case DW_OP_reg20:
22970 case DW_OP_reg21:
22971 case DW_OP_reg22:
22972 case DW_OP_reg23:
22973 case DW_OP_reg24:
22974 case DW_OP_reg25:
22975 case DW_OP_reg26:
22976 case DW_OP_reg27:
22977 case DW_OP_reg28:
22978 case DW_OP_reg29:
22979 case DW_OP_reg30:
22980 case DW_OP_reg31:
22981 stack[++stacki] = op - DW_OP_reg0;
22982 if (i < size)
22983 {
22984 if (computed == nullptr)
22985 dwarf2_complex_location_expr_complaint ();
22986 else
22987 return 0;
22988 }
22989 break;
22990
22991 case DW_OP_regx:
22992 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22993 i += bytes_read;
22994 stack[++stacki] = unsnd;
22995 if (i < size)
22996 {
22997 if (computed == nullptr)
22998 dwarf2_complex_location_expr_complaint ();
22999 else
23000 return 0;
23001 }
23002 break;
23003
23004 case DW_OP_addr:
23005 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23006 &bytes_read);
23007 i += bytes_read;
23008 break;
23009
23010 case DW_OP_const1u:
23011 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23012 i += 1;
23013 break;
23014
23015 case DW_OP_const1s:
23016 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23017 i += 1;
23018 break;
23019
23020 case DW_OP_const2u:
23021 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23022 i += 2;
23023 break;
23024
23025 case DW_OP_const2s:
23026 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23027 i += 2;
23028 break;
23029
23030 case DW_OP_const4u:
23031 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23032 i += 4;
23033 break;
23034
23035 case DW_OP_const4s:
23036 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23037 i += 4;
23038 break;
23039
23040 case DW_OP_const8u:
23041 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23042 i += 8;
23043 break;
23044
23045 case DW_OP_constu:
23046 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_consts:
23052 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23053 i += bytes_read;
23054 break;
23055
23056 case DW_OP_dup:
23057 stack[stacki + 1] = stack[stacki];
23058 stacki++;
23059 break;
23060
23061 case DW_OP_plus:
23062 stack[stacki - 1] += stack[stacki];
23063 stacki--;
23064 break;
23065
23066 case DW_OP_plus_uconst:
23067 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23068 &bytes_read);
23069 i += bytes_read;
23070 break;
23071
23072 case DW_OP_minus:
23073 stack[stacki - 1] -= stack[stacki];
23074 stacki--;
23075 break;
23076
23077 case DW_OP_deref:
23078 /* If we're not the last op, then we definitely can't encode
23079 this using GDB's address_class enum. This is valid for partial
23080 global symbols, although the variable's address will be bogus
23081 in the psymtab. */
23082 if (i < size)
23083 {
23084 if (computed == nullptr)
23085 dwarf2_complex_location_expr_complaint ();
23086 else
23087 return 0;
23088 }
23089 break;
23090
23091 case DW_OP_GNU_push_tls_address:
23092 case DW_OP_form_tls_address:
23093 /* The top of the stack has the offset from the beginning
23094 of the thread control block at which the variable is located. */
23095 /* Nothing should follow this operator, so the top of stack would
23096 be returned. */
23097 /* This is valid for partial global symbols, but the variable's
23098 address will be bogus in the psymtab. Make it always at least
23099 non-zero to not look as a variable garbage collected by linker
23100 which have DW_OP_addr 0. */
23101 if (i < size)
23102 {
23103 if (computed == nullptr)
23104 dwarf2_complex_location_expr_complaint ();
23105 else
23106 return 0;
23107 }
23108 stack[stacki]++;
23109 break;
23110
23111 case DW_OP_GNU_uninit:
23112 if (computed != nullptr)
23113 return 0;
23114 break;
23115
23116 case DW_OP_addrx:
23117 case DW_OP_GNU_addr_index:
23118 case DW_OP_GNU_const_index:
23119 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23120 &bytes_read);
23121 i += bytes_read;
23122 break;
23123
23124 default:
23125 if (computed == nullptr)
23126 {
23127 const char *name = get_DW_OP_name (op);
23128
23129 if (name)
23130 complaint (_("unsupported stack op: '%s'"),
23131 name);
23132 else
23133 complaint (_("unsupported stack op: '%02x'"),
23134 op);
23135 }
23136
23137 return (stack[stacki]);
23138 }
23139
23140 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23141 outside of the allocated space. Also enforce minimum>0. */
23142 if (stacki >= ARRAY_SIZE (stack) - 1)
23143 {
23144 if (computed == nullptr)
23145 complaint (_("location description stack overflow"));
23146 return 0;
23147 }
23148
23149 if (stacki <= 0)
23150 {
23151 if (computed == nullptr)
23152 complaint (_("location description stack underflow"));
23153 return 0;
23154 }
23155 }
23156
23157 if (computed != nullptr)
23158 *computed = true;
23159 return (stack[stacki]);
23160}
23161
23162/* memory allocation interface */
23163
23164static struct dwarf_block *
23165dwarf_alloc_block (struct dwarf2_cu *cu)
23166{
23167 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23168}
23169
23170static struct die_info *
23171dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23172{
23173 struct die_info *die;
23174 size_t size = sizeof (struct die_info);
23175
23176 if (num_attrs > 1)
23177 size += (num_attrs - 1) * sizeof (struct attribute);
23178
23179 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23180 memset (die, 0, sizeof (struct die_info));
23181 return (die);
23182}
23183
23184\f
23185
23186/* Macro support. */
23187
23188/* An overload of dwarf_decode_macros that finds the correct section
23189 and ensures it is read in before calling the other overload. */
23190
23191static void
23192dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23193 int section_is_gnu)
23194{
23195 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23196 struct objfile *objfile = dwarf2_per_objfile->objfile;
23197 const struct line_header *lh = cu->line_header;
23198 unsigned int offset_size = cu->header.offset_size;
23199 struct dwarf2_section_info *section;
23200 const char *section_name;
23201
23202 if (cu->dwo_unit != nullptr)
23203 {
23204 if (section_is_gnu)
23205 {
23206 section = &cu->dwo_unit->dwo_file->sections.macro;
23207 section_name = ".debug_macro.dwo";
23208 }
23209 else
23210 {
23211 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23212 section_name = ".debug_macinfo.dwo";
23213 }
23214 }
23215 else
23216 {
23217 if (section_is_gnu)
23218 {
23219 section = &dwarf2_per_objfile->per_bfd->macro;
23220 section_name = ".debug_macro";
23221 }
23222 else
23223 {
23224 section = &dwarf2_per_objfile->per_bfd->macinfo;
23225 section_name = ".debug_macinfo";
23226 }
23227 }
23228
23229 section->read (objfile);
23230 if (section->buffer == nullptr)
23231 {
23232 complaint (_("missing %s section"), section_name);
23233 return;
23234 }
23235
23236 buildsym_compunit *builder = cu->get_builder ();
23237
23238 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23239 offset_size, offset, section_is_gnu);
23240}
23241
23242/* Return the .debug_loc section to use for CU.
23243 For DWO files use .debug_loc.dwo. */
23244
23245static struct dwarf2_section_info *
23246cu_debug_loc_section (struct dwarf2_cu *cu)
23247{
23248 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23249
23250 if (cu->dwo_unit)
23251 {
23252 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23253
23254 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23255 }
23256 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23257 : &dwarf2_per_objfile->per_bfd->loc);
23258}
23259
23260/* A helper function that fills in a dwarf2_loclist_baton. */
23261
23262static void
23263fill_in_loclist_baton (struct dwarf2_cu *cu,
23264 struct dwarf2_loclist_baton *baton,
23265 const struct attribute *attr)
23266{
23267 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23268 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23269
23270 section->read (dwarf2_per_objfile->objfile);
23271
23272 baton->per_objfile = dwarf2_per_objfile;
23273 baton->per_cu = cu->per_cu;
23274 gdb_assert (baton->per_cu);
23275 /* We don't know how long the location list is, but make sure we
23276 don't run off the edge of the section. */
23277 baton->size = section->size - DW_UNSND (attr);
23278 baton->data = section->buffer + DW_UNSND (attr);
23279 if (cu->base_address.has_value ())
23280 baton->base_address = *cu->base_address;
23281 else
23282 baton->base_address = 0;
23283 baton->from_dwo = cu->dwo_unit != NULL;
23284}
23285
23286static void
23287dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23288 struct dwarf2_cu *cu, int is_block)
23289{
23290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23291 struct objfile *objfile = dwarf2_per_objfile->objfile;
23292 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23293
23294 if (attr->form_is_section_offset ()
23295 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23296 the section. If so, fall through to the complaint in the
23297 other branch. */
23298 && DW_UNSND (attr) < section->get_size (objfile))
23299 {
23300 struct dwarf2_loclist_baton *baton;
23301
23302 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23303
23304 fill_in_loclist_baton (cu, baton, attr);
23305
23306 if (!cu->base_address.has_value ())
23307 complaint (_("Location list used without "
23308 "specifying the CU base address."));
23309
23310 SYMBOL_ACLASS_INDEX (sym) = (is_block
23311 ? dwarf2_loclist_block_index
23312 : dwarf2_loclist_index);
23313 SYMBOL_LOCATION_BATON (sym) = baton;
23314 }
23315 else
23316 {
23317 struct dwarf2_locexpr_baton *baton;
23318
23319 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23320 baton->per_objfile = dwarf2_per_objfile;
23321 baton->per_cu = cu->per_cu;
23322 gdb_assert (baton->per_cu);
23323
23324 if (attr->form_is_block ())
23325 {
23326 /* Note that we're just copying the block's data pointer
23327 here, not the actual data. We're still pointing into the
23328 info_buffer for SYM's objfile; right now we never release
23329 that buffer, but when we do clean up properly this may
23330 need to change. */
23331 baton->size = DW_BLOCK (attr)->size;
23332 baton->data = DW_BLOCK (attr)->data;
23333 }
23334 else
23335 {
23336 dwarf2_invalid_attrib_class_complaint ("location description",
23337 sym->natural_name ());
23338 baton->size = 0;
23339 }
23340
23341 SYMBOL_ACLASS_INDEX (sym) = (is_block
23342 ? dwarf2_locexpr_block_index
23343 : dwarf2_locexpr_index);
23344 SYMBOL_LOCATION_BATON (sym) = baton;
23345 }
23346}
23347
23348/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23349 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23350 CU_HEADERP first. */
23351
23352static const struct comp_unit_head *
23353per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23354 const struct dwarf2_per_cu_data *per_cu)
23355{
23356 const gdb_byte *info_ptr;
23357
23358 if (per_cu->cu)
23359 return &per_cu->cu->header;
23360
23361 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23362
23363 memset (cu_headerp, 0, sizeof (*cu_headerp));
23364 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23365 rcuh_kind::COMPILE);
23366
23367 return cu_headerp;
23368}
23369
23370/* See read.h. */
23371
23372int
23373dwarf2_per_cu_data::addr_size () const
23374{
23375 struct comp_unit_head cu_header_local;
23376 const struct comp_unit_head *cu_headerp;
23377
23378 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23379
23380 return cu_headerp->addr_size;
23381}
23382
23383/* See read.h. */
23384
23385int
23386dwarf2_per_cu_data::offset_size () const
23387{
23388 struct comp_unit_head cu_header_local;
23389 const struct comp_unit_head *cu_headerp;
23390
23391 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23392
23393 return cu_headerp->offset_size;
23394}
23395
23396/* See read.h. */
23397
23398int
23399dwarf2_per_cu_data::ref_addr_size () const
23400{
23401 struct comp_unit_head cu_header_local;
23402 const struct comp_unit_head *cu_headerp;
23403
23404 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23405
23406 if (cu_headerp->version == 2)
23407 return cu_headerp->addr_size;
23408 else
23409 return cu_headerp->offset_size;
23410}
23411
23412/* See read.h. */
23413
23414struct type *
23415dwarf2_cu::addr_type () const
23416{
23417 struct objfile *objfile = this->per_objfile->objfile;
23418 struct type *void_type = objfile_type (objfile)->builtin_void;
23419 struct type *addr_type = lookup_pointer_type (void_type);
23420 int addr_size = this->per_cu->addr_size ();
23421
23422 if (TYPE_LENGTH (addr_type) == addr_size)
23423 return addr_type;
23424
23425 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23426 return addr_type;
23427}
23428
23429/* A helper function for dwarf2_find_containing_comp_unit that returns
23430 the index of the result, and that searches a vector. It will
23431 return a result even if the offset in question does not actually
23432 occur in any CU. This is separate so that it can be unit
23433 tested. */
23434
23435static int
23436dwarf2_find_containing_comp_unit
23437 (sect_offset sect_off,
23438 unsigned int offset_in_dwz,
23439 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23440{
23441 int low, high;
23442
23443 low = 0;
23444 high = all_comp_units.size () - 1;
23445 while (high > low)
23446 {
23447 struct dwarf2_per_cu_data *mid_cu;
23448 int mid = low + (high - low) / 2;
23449
23450 mid_cu = all_comp_units[mid];
23451 if (mid_cu->is_dwz > offset_in_dwz
23452 || (mid_cu->is_dwz == offset_in_dwz
23453 && mid_cu->sect_off + mid_cu->length > sect_off))
23454 high = mid;
23455 else
23456 low = mid + 1;
23457 }
23458 gdb_assert (low == high);
23459 return low;
23460}
23461
23462/* Locate the .debug_info compilation unit from CU's objfile which contains
23463 the DIE at OFFSET. Raises an error on failure. */
23464
23465static struct dwarf2_per_cu_data *
23466dwarf2_find_containing_comp_unit (sect_offset sect_off,
23467 unsigned int offset_in_dwz,
23468 struct dwarf2_per_objfile *dwarf2_per_objfile)
23469{
23470 int low
23471 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23472 dwarf2_per_objfile->per_bfd->all_comp_units);
23473 struct dwarf2_per_cu_data *this_cu
23474 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23475
23476 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23477 {
23478 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23479 error (_("Dwarf Error: could not find partial DIE containing "
23480 "offset %s [in module %s]"),
23481 sect_offset_str (sect_off),
23482 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23483
23484 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23485 <= sect_off);
23486 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23487 }
23488 else
23489 {
23490 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23491 && sect_off >= this_cu->sect_off + this_cu->length)
23492 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23493 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23494 return this_cu;
23495 }
23496}
23497
23498#if GDB_SELF_TEST
23499
23500namespace selftests {
23501namespace find_containing_comp_unit {
23502
23503static void
23504run_test ()
23505{
23506 struct dwarf2_per_cu_data one {};
23507 struct dwarf2_per_cu_data two {};
23508 struct dwarf2_per_cu_data three {};
23509 struct dwarf2_per_cu_data four {};
23510
23511 one.length = 5;
23512 two.sect_off = sect_offset (one.length);
23513 two.length = 7;
23514
23515 three.length = 5;
23516 three.is_dwz = 1;
23517 four.sect_off = sect_offset (three.length);
23518 four.length = 7;
23519 four.is_dwz = 1;
23520
23521 std::vector<dwarf2_per_cu_data *> units;
23522 units.push_back (&one);
23523 units.push_back (&two);
23524 units.push_back (&three);
23525 units.push_back (&four);
23526
23527 int result;
23528
23529 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23530 SELF_CHECK (units[result] == &one);
23531 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23532 SELF_CHECK (units[result] == &one);
23533 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23534 SELF_CHECK (units[result] == &two);
23535
23536 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23537 SELF_CHECK (units[result] == &three);
23538 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23539 SELF_CHECK (units[result] == &three);
23540 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23541 SELF_CHECK (units[result] == &four);
23542}
23543
23544}
23545}
23546
23547#endif /* GDB_SELF_TEST */
23548
23549/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23550
23551dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23552 dwarf2_per_objfile *per_objfile)
23553 : per_cu (per_cu),
23554 per_objfile (per_objfile),
23555 mark (false),
23556 has_loclist (false),
23557 checked_producer (false),
23558 producer_is_gxx_lt_4_6 (false),
23559 producer_is_gcc_lt_4_3 (false),
23560 producer_is_icc (false),
23561 producer_is_icc_lt_14 (false),
23562 producer_is_codewarrior (false),
23563 processing_has_namespace_info (false)
23564{
23565 per_cu->cu = this;
23566}
23567
23568/* Destroy a dwarf2_cu. */
23569
23570dwarf2_cu::~dwarf2_cu ()
23571{
23572 per_cu->cu = NULL;
23573}
23574
23575/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23576
23577static void
23578prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23579 enum language pretend_language)
23580{
23581 struct attribute *attr;
23582
23583 /* Set the language we're debugging. */
23584 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23585 if (attr != nullptr)
23586 set_cu_language (DW_UNSND (attr), cu);
23587 else
23588 {
23589 cu->language = pretend_language;
23590 cu->language_defn = language_def (cu->language);
23591 }
23592
23593 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23594}
23595
23596/* Increase the age counter on each cached compilation unit, and free
23597 any that are too old. */
23598
23599static void
23600age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23601{
23602 struct dwarf2_per_cu_data *per_cu, **last_chain;
23603
23604 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23605 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23606 while (per_cu != NULL)
23607 {
23608 per_cu->cu->last_used ++;
23609 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23610 dwarf2_mark (per_cu->cu);
23611 per_cu = per_cu->cu->read_in_chain;
23612 }
23613
23614 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23615 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23616 while (per_cu != NULL)
23617 {
23618 struct dwarf2_per_cu_data *next_cu;
23619
23620 next_cu = per_cu->cu->read_in_chain;
23621
23622 if (!per_cu->cu->mark)
23623 {
23624 delete per_cu->cu;
23625 *last_chain = next_cu;
23626 }
23627 else
23628 last_chain = &per_cu->cu->read_in_chain;
23629
23630 per_cu = next_cu;
23631 }
23632}
23633
23634/* Remove a single compilation unit from the cache. */
23635
23636static void
23637free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23638 dwarf2_per_objfile *dwarf2_per_objfile)
23639{
23640 struct dwarf2_per_cu_data *per_cu, **last_chain;
23641
23642 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23643 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23644 while (per_cu != NULL)
23645 {
23646 struct dwarf2_per_cu_data *next_cu;
23647
23648 next_cu = per_cu->cu->read_in_chain;
23649
23650 if (per_cu == target_per_cu)
23651 {
23652 delete per_cu->cu;
23653 per_cu->cu = NULL;
23654 *last_chain = next_cu;
23655 break;
23656 }
23657 else
23658 last_chain = &per_cu->cu->read_in_chain;
23659
23660 per_cu = next_cu;
23661 }
23662}
23663
23664/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23665 We store these in a hash table separate from the DIEs, and preserve them
23666 when the DIEs are flushed out of cache.
23667
23668 The CU "per_cu" pointer is needed because offset alone is not enough to
23669 uniquely identify the type. A file may have multiple .debug_types sections,
23670 or the type may come from a DWO file. Furthermore, while it's more logical
23671 to use per_cu->section+offset, with Fission the section with the data is in
23672 the DWO file but we don't know that section at the point we need it.
23673 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23674 because we can enter the lookup routine, get_die_type_at_offset, from
23675 outside this file, and thus won't necessarily have PER_CU->cu.
23676 Fortunately, PER_CU is stable for the life of the objfile. */
23677
23678struct dwarf2_per_cu_offset_and_type
23679{
23680 const struct dwarf2_per_cu_data *per_cu;
23681 sect_offset sect_off;
23682 struct type *type;
23683};
23684
23685/* Hash function for a dwarf2_per_cu_offset_and_type. */
23686
23687static hashval_t
23688per_cu_offset_and_type_hash (const void *item)
23689{
23690 const struct dwarf2_per_cu_offset_and_type *ofs
23691 = (const struct dwarf2_per_cu_offset_and_type *) item;
23692
23693 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23694}
23695
23696/* Equality function for a dwarf2_per_cu_offset_and_type. */
23697
23698static int
23699per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23700{
23701 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23702 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23703 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23704 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23705
23706 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23707 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23708}
23709
23710/* Set the type associated with DIE to TYPE. Save it in CU's hash
23711 table if necessary. For convenience, return TYPE.
23712
23713 The DIEs reading must have careful ordering to:
23714 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23715 reading current DIE.
23716 * Not trying to dereference contents of still incompletely read in types
23717 while reading in other DIEs.
23718 * Enable referencing still incompletely read in types just by a pointer to
23719 the type without accessing its fields.
23720
23721 Therefore caller should follow these rules:
23722 * Try to fetch any prerequisite types we may need to build this DIE type
23723 before building the type and calling set_die_type.
23724 * After building type call set_die_type for current DIE as soon as
23725 possible before fetching more types to complete the current type.
23726 * Make the type as complete as possible before fetching more types. */
23727
23728static struct type *
23729set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23730{
23731 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23732 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23733 struct objfile *objfile = dwarf2_per_objfile->objfile;
23734 struct attribute *attr;
23735 struct dynamic_prop prop;
23736
23737 /* For Ada types, make sure that the gnat-specific data is always
23738 initialized (if not already set). There are a few types where
23739 we should not be doing so, because the type-specific area is
23740 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23741 where the type-specific area is used to store the floatformat).
23742 But this is not a problem, because the gnat-specific information
23743 is actually not needed for these types. */
23744 if (need_gnat_info (cu)
23745 && type->code () != TYPE_CODE_FUNC
23746 && type->code () != TYPE_CODE_FLT
23747 && type->code () != TYPE_CODE_METHODPTR
23748 && type->code () != TYPE_CODE_MEMBERPTR
23749 && type->code () != TYPE_CODE_METHOD
23750 && !HAVE_GNAT_AUX_INFO (type))
23751 INIT_GNAT_SPECIFIC (type);
23752
23753 /* Read DW_AT_allocated and set in type. */
23754 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23755 if (attr != NULL && attr->form_is_block ())
23756 {
23757 struct type *prop_type = cu->addr_sized_int_type (false);
23758 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23759 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23760 }
23761 else if (attr != NULL)
23762 {
23763 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23764 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23765 sect_offset_str (die->sect_off));
23766 }
23767
23768 /* Read DW_AT_associated and set in type. */
23769 attr = dwarf2_attr (die, DW_AT_associated, cu);
23770 if (attr != NULL && attr->form_is_block ())
23771 {
23772 struct type *prop_type = cu->addr_sized_int_type (false);
23773 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23774 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23775 }
23776 else if (attr != NULL)
23777 {
23778 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23779 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23780 sect_offset_str (die->sect_off));
23781 }
23782
23783 /* Read DW_AT_data_location and set in type. */
23784 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23785 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23786 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23787
23788 if (dwarf2_per_objfile->die_type_hash == NULL)
23789 dwarf2_per_objfile->die_type_hash
23790 = htab_up (htab_create_alloc (127,
23791 per_cu_offset_and_type_hash,
23792 per_cu_offset_and_type_eq,
23793 NULL, xcalloc, xfree));
23794
23795 ofs.per_cu = cu->per_cu;
23796 ofs.sect_off = die->sect_off;
23797 ofs.type = type;
23798 slot = (struct dwarf2_per_cu_offset_and_type **)
23799 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23800 if (*slot)
23801 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23802 sect_offset_str (die->sect_off));
23803 *slot = XOBNEW (&objfile->objfile_obstack,
23804 struct dwarf2_per_cu_offset_and_type);
23805 **slot = ofs;
23806 return type;
23807}
23808
23809/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23810 or return NULL if the die does not have a saved type. */
23811
23812static struct type *
23813get_die_type_at_offset (sect_offset sect_off,
23814 dwarf2_per_cu_data *per_cu,
23815 dwarf2_per_objfile *dwarf2_per_objfile)
23816{
23817 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23818
23819 if (dwarf2_per_objfile->die_type_hash == NULL)
23820 return NULL;
23821
23822 ofs.per_cu = per_cu;
23823 ofs.sect_off = sect_off;
23824 slot = ((struct dwarf2_per_cu_offset_and_type *)
23825 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23826 if (slot)
23827 return slot->type;
23828 else
23829 return NULL;
23830}
23831
23832/* Look up the type for DIE in CU in die_type_hash,
23833 or return NULL if DIE does not have a saved type. */
23834
23835static struct type *
23836get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23837{
23838 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23839}
23840
23841/* Add a dependence relationship from CU to REF_PER_CU. */
23842
23843static void
23844dwarf2_add_dependence (struct dwarf2_cu *cu,
23845 struct dwarf2_per_cu_data *ref_per_cu)
23846{
23847 void **slot;
23848
23849 if (cu->dependencies == NULL)
23850 cu->dependencies
23851 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23852 NULL, &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
23855
23856 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23857 if (*slot == NULL)
23858 *slot = ref_per_cu;
23859}
23860
23861/* Subroutine of dwarf2_mark to pass to htab_traverse.
23862 Set the mark field in every compilation unit in the
23863 cache that we must keep because we are keeping CU. */
23864
23865static int
23866dwarf2_mark_helper (void **slot, void *data)
23867{
23868 struct dwarf2_per_cu_data *per_cu;
23869
23870 per_cu = (struct dwarf2_per_cu_data *) *slot;
23871
23872 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23873 reading of the chain. As such dependencies remain valid it is not much
23874 useful to track and undo them during QUIT cleanups. */
23875 if (per_cu->cu == NULL)
23876 return 1;
23877
23878 if (per_cu->cu->mark)
23879 return 1;
23880 per_cu->cu->mark = true;
23881
23882 if (per_cu->cu->dependencies != NULL)
23883 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23884
23885 return 1;
23886}
23887
23888/* Set the mark field in CU and in every other compilation unit in the
23889 cache that we must keep because we are keeping CU. */
23890
23891static void
23892dwarf2_mark (struct dwarf2_cu *cu)
23893{
23894 if (cu->mark)
23895 return;
23896 cu->mark = true;
23897 if (cu->dependencies != NULL)
23898 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23899}
23900
23901static void
23902dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23903{
23904 while (per_cu)
23905 {
23906 per_cu->cu->mark = false;
23907 per_cu = per_cu->cu->read_in_chain;
23908 }
23909}
23910
23911/* Trivial hash function for partial_die_info: the hash value of a DIE
23912 is its offset in .debug_info for this objfile. */
23913
23914static hashval_t
23915partial_die_hash (const void *item)
23916{
23917 const struct partial_die_info *part_die
23918 = (const struct partial_die_info *) item;
23919
23920 return to_underlying (part_die->sect_off);
23921}
23922
23923/* Trivial comparison function for partial_die_info structures: two DIEs
23924 are equal if they have the same offset. */
23925
23926static int
23927partial_die_eq (const void *item_lhs, const void *item_rhs)
23928{
23929 const struct partial_die_info *part_die_lhs
23930 = (const struct partial_die_info *) item_lhs;
23931 const struct partial_die_info *part_die_rhs
23932 = (const struct partial_die_info *) item_rhs;
23933
23934 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23935}
23936
23937struct cmd_list_element *set_dwarf_cmdlist;
23938struct cmd_list_element *show_dwarf_cmdlist;
23939
23940static void
23941show_check_physname (struct ui_file *file, int from_tty,
23942 struct cmd_list_element *c, const char *value)
23943{
23944 fprintf_filtered (file,
23945 _("Whether to check \"physname\" is %s.\n"),
23946 value);
23947}
23948
23949void _initialize_dwarf2_read ();
23950void
23951_initialize_dwarf2_read ()
23952{
23953 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23954Set DWARF specific variables.\n\
23955Configure DWARF variables such as the cache size."),
23956 &set_dwarf_cmdlist, "maintenance set dwarf ",
23957 0/*allow-unknown*/, &maintenance_set_cmdlist);
23958
23959 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23960Show DWARF specific variables.\n\
23961Show DWARF variables such as the cache size."),
23962 &show_dwarf_cmdlist, "maintenance show dwarf ",
23963 0/*allow-unknown*/, &maintenance_show_cmdlist);
23964
23965 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23966 &dwarf_max_cache_age, _("\
23967Set the upper bound on the age of cached DWARF compilation units."), _("\
23968Show the upper bound on the age of cached DWARF compilation units."), _("\
23969A higher limit means that cached compilation units will be stored\n\
23970in memory longer, and more total memory will be used. Zero disables\n\
23971caching, which can slow down startup."),
23972 NULL,
23973 show_dwarf_max_cache_age,
23974 &set_dwarf_cmdlist,
23975 &show_dwarf_cmdlist);
23976
23977 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23978Set debugging of the DWARF reader."), _("\
23979Show debugging of the DWARF reader."), _("\
23980When enabled (non-zero), debugging messages are printed during DWARF\n\
23981reading and symtab expansion. A value of 1 (one) provides basic\n\
23982information. A value greater than 1 provides more verbose information."),
23983 NULL,
23984 NULL,
23985 &setdebuglist, &showdebuglist);
23986
23987 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23988Set debugging of the DWARF DIE reader."), _("\
23989Show debugging of the DWARF DIE reader."), _("\
23990When enabled (non-zero), DIEs are dumped after they are read in.\n\
23991The value is the maximum depth to print."),
23992 NULL,
23993 NULL,
23994 &setdebuglist, &showdebuglist);
23995
23996 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23997Set debugging of the dwarf line reader."), _("\
23998Show debugging of the dwarf line reader."), _("\
23999When enabled (non-zero), line number entries are dumped as they are read in.\n\
24000A value of 1 (one) provides basic information.\n\
24001A value greater than 1 provides more verbose information."),
24002 NULL,
24003 NULL,
24004 &setdebuglist, &showdebuglist);
24005
24006 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24007Set cross-checking of \"physname\" code against demangler."), _("\
24008Show cross-checking of \"physname\" code against demangler."), _("\
24009When enabled, GDB's internal \"physname\" code is checked against\n\
24010the demangler."),
24011 NULL, show_check_physname,
24012 &setdebuglist, &showdebuglist);
24013
24014 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24015 no_class, &use_deprecated_index_sections, _("\
24016Set whether to use deprecated gdb_index sections."), _("\
24017Show whether to use deprecated gdb_index sections."), _("\
24018When enabled, deprecated .gdb_index sections are used anyway.\n\
24019Normally they are ignored either because of a missing feature or\n\
24020performance issue.\n\
24021Warning: This option must be enabled before gdb reads the file."),
24022 NULL,
24023 NULL,
24024 &setlist, &showlist);
24025
24026 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24027 &dwarf2_locexpr_funcs);
24028 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24029 &dwarf2_loclist_funcs);
24030
24031 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24032 &dwarf2_block_frame_base_locexpr_funcs);
24033 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24034 &dwarf2_block_frame_base_loclist_funcs);
24035
24036#if GDB_SELF_TEST
24037 selftests::register_test ("dw2_expand_symtabs_matching",
24038 selftests::dw2_expand_symtabs_matching::run_test);
24039 selftests::register_test ("dwarf2_find_containing_comp_unit",
24040 selftests::find_containing_comp_unit::run_test);
24041#endif
24042}
This page took 0.108161 seconds and 4 git commands to generate.