Add dwarf2_per_objfile parameter to recursively_compute_inclusions
[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 /* The header of the compilation unit. */
425 struct comp_unit_head header {};
426
427 /* Base address of this compilation unit. */
428 gdb::optional<CORE_ADDR> base_address;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
473 /* How many compilation units ago was this CU last referenced? */
474 int last_used = 0;
475
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
478 htab_t die_hash = nullptr;
479
480 /* Full DIEs if read in. */
481 struct die_info *dies = nullptr;
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
486 htab_t dependencies = nullptr;
487
488 /* Header data from the line table, during full symbol processing. */
489 struct line_header *line_header = nullptr;
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
495 die_info *line_header_die_owner = nullptr;
496
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
499 std::vector<delayed_method_info> method_list;
500
501 /* To be copied to symtab->call_site_htab. */
502 htab_t call_site_htab = nullptr;
503
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
513 struct dwo_unit *dwo_unit = nullptr;
514
515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE. */
517 gdb::optional<ULONGEST> addr_base;
518
519 /* The DW_AT_rnglists_base attribute if present.
520 Note this value comes from the Fission stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base = 0;
529
530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592};
593
594/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597struct stmt_list_hash
598{
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604};
605
606/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
607 an object of this type. */
608
609struct type_unit_group
610{
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
620 std::vector<signatured_type *> *tus;
621
622 /* The compunit symtab.
623 Type units in a group needn't all be defined in the same source file,
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639};
640
641/* These sections are what may appear in a (real or virtual) DWO file. */
642
643struct dwo_sections
644{
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656};
657
658/* CUs/TUs in DWP/DWO files. */
659
660struct dwo_unit
661{
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679};
680
681/* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685enum dwp_v2_section_ids
686{
687 DW_SECT_MIN = 1
688};
689
690/* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701struct dwo_file
702{
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_up cus;
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_up tus;
733};
734
735/* These sections are what may appear in a DWP file. */
736
737struct dwp_sections
738{
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760};
761
762/* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765struct virtual_v1_dwo_sections
766{
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776};
777
778/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783struct virtual_v2_dwo_sections
784{
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807};
808
809/* Contents of DWP hash tables. */
810
811struct dwp_hash_table
812{
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826#define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838};
839
840/* Data for one DWP file. */
841
842struct dwp_file
843{
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_up loaded_cus;
870 htab_up loaded_tus;
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876};
877
878/* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
881 struct exists to abstract away the constant parameters of die reading. */
882
883struct die_reader_specs
884{
885 /* The bfd of die_section. */
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
892 struct dwo_file *dwo_file;
893
894 /* The section the die comes from.
895 This is either .debug_info or .debug_types, or the .dwo variants. */
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
899 const gdb_byte *buffer;
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
903
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
906};
907
908/* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911class cutu_reader : public die_reader_specs
912{
913public:
914
915 cutu_reader (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
917 struct abbrev_table *abbrev_table,
918 int use_existing_cu,
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 dwarf2_per_objfile *per_objfile,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
930 bool dummy_p = false;
931
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
936private:
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
939 int use_existing_cu);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949};
950
951/* When we construct a partial symbol table entry we only
952 need this much information. */
953struct partial_die_info : public allocate_on_obstack
954 {
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
972 /* Offset of this DIE. */
973 const sect_offset sect_off;
974
975 /* DWARF-2 tag for this DIE. */
976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
977
978 /* Assorted flags describing the data found in this DIE. */
979 const unsigned int has_children : 1;
980
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
986 unsigned int may_be_inlined : 1;
987
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
1004 /* Flag set if fixup has been called on this die. */
1005 unsigned int fixup_called : 1;
1006
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
1013 /* The name of this DIE. Normally the value of DW_AT_name, but
1014 sometimes a default name for unnamed DIEs. */
1015 const char *name = nullptr;
1016
1017 /* The linkage name, if present. */
1018 const char *linkage_name = nullptr;
1019
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
1023 const char *scope = nullptr;
1024
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
1032 sect_offset sect_off;
1033 } d {};
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
1038
1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1040 DW_AT_sibling, if any. */
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
1043 const gdb_byte *sibling = nullptr;
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
1048 sect_offset spec_offset {};
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
1085 };
1086
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
1092struct variant_part_builder;
1093
1094/* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
1096
1097struct variant_field
1098{
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114};
1115
1116/* This represents a DW_TAG_variant_part. */
1117
1118struct variant_part_builder
1119{
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
1128};
1129
1130struct nextfield
1131{
1132 int accessibility = 0;
1133 int virtuality = 0;
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
1138 struct field field {};
1139};
1140
1141struct fnfieldlist
1142{
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
1145};
1146
1147/* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150struct field_info
1151 {
1152 /* List of data member and baseclasses fields. */
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
1155
1156 /* Set if the accessibility of one of the fields is not public. */
1157 int non_public_fields = 0;
1158
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
1162 std::vector<struct fnfieldlist> fnfieldlists;
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1166 std::vector<struct decl_field> typedef_field_list;
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
1170 std::vector<struct decl_field> nested_types_list;
1171
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
1184 };
1185
1186/* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191static int dwarf_max_cache_age = 5;
1192static void
1193show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195{
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "DWARF compilation units is %s.\n"),
1198 value);
1199}
1200\f
1201/* local function prototypes */
1202
1203static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
1206static dwarf2_psymtab *create_partial_symtab
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
1209
1210static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
1212 struct die_info *type_unit_die);
1213
1214static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1216
1217static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
1219 int, struct dwarf2_cu *);
1220
1221static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
1223
1224static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1226 int set_addrmap, struct dwarf2_cu *cu);
1227
1228static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1229 CORE_ADDR *highpc, int set_addrmap,
1230 struct dwarf2_cu *cu);
1231
1232static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
1234
1235static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1237 int need_pc, struct dwarf2_cu *cu);
1238
1239static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1240
1241static struct partial_die_info *load_partial_dies
1242 (const struct die_reader_specs *, const gdb_byte *, int);
1243
1244/* A pair of partial_die_info and compilation unit. */
1245struct cu_partial_die_info
1246{
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
1255 { /* Nothing. */ }
1256
1257private:
1258 cu_partial_die_info () = delete;
1259};
1260
1261static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
1263
1264static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
1266 const gdb_byte *, bool *need_reprocess);
1267
1268static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1272
1273static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
1276
1277static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
1280
1281static const char *read_indirect_string_at_offset
1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1283
1284static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
1286 unsigned int *);
1287
1288static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
1293
1294static void set_cu_language (unsigned int, struct dwarf2_cu *);
1295
1296static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
1298
1299static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
1302static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
1304static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
1307static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1308
1309static struct die_info *die_specification (struct die_info *die,
1310 struct dwarf2_cu **);
1311
1312static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1313 struct dwarf2_cu *cu);
1314
1315static void dwarf_decode_lines (struct line_header *, const char *,
1316 struct dwarf2_cu *, dwarf2_psymtab *,
1317 CORE_ADDR, int decode_mapping);
1318
1319static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
1321
1322static struct symbol *new_symbol (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol * = NULL);
1324
1325static void dwarf2_const_value (const struct attribute *, struct symbol *,
1326 struct dwarf2_cu *);
1327
1328static void dwarf2_const_value_attr (const struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
1332 struct dwarf2_cu *cu, LONGEST *value,
1333 const gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
1335
1336static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338static int need_gnat_info (struct dwarf2_cu *);
1339
1340static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
1346static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
1348
1349static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1350 struct dwarf2_cu *);
1351
1352static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358static char *typename_concat (struct obstack *obs, const char *prefix,
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
1361
1362static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
1374static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1375 struct dwarf2_cu *, dwarf2_psymtab *);
1376
1377/* Return the .debug_loclists section to use for cu. */
1378static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
1380/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1381 values. Keep the items ordered with increasing constraints compliance. */
1382enum pc_bounds_kind
1383{
1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1385 PC_BOUNDS_NOT_PRESENT,
1386
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396};
1397
1398static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
1401 dwarf2_psymtab *);
1402
1403static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
1407static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
1410static void dwarf2_add_field (struct field_info *, struct die_info *,
1411 struct dwarf2_cu *);
1412
1413static void dwarf2_attach_fields_to_type (struct field_info *,
1414 struct type *, struct dwarf2_cu *);
1415
1416static void dwarf2_add_member_fn (struct field_info *,
1417 struct die_info *, struct type *,
1418 struct dwarf2_cu *);
1419
1420static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1421 struct type *,
1422 struct dwarf2_cu *);
1423
1424static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1425
1426static void read_common_block (struct die_info *, struct dwarf2_cu *);
1427
1428static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1429
1430static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
1432static struct using_direct **using_directives (struct dwarf2_cu *cu);
1433
1434static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
1436static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
1438static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
1441static const char *namespace_name (struct die_info *die,
1442 int *is_anonymous, struct dwarf2_cu *);
1443
1444static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1445
1446static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
1448
1449static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1450 struct dwarf2_cu *);
1451
1452static struct die_info *read_die_and_siblings_1
1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1454 struct die_info *);
1455
1456static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
1459 struct die_info *parent);
1460
1461static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
1463 int);
1464
1465static const gdb_byte *read_full_die (const struct die_reader_specs *,
1466 struct die_info **, const gdb_byte *);
1467
1468static void process_die (struct die_info *, struct dwarf2_cu *);
1469
1470static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1471 struct objfile *);
1472
1473static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1474
1475static const char *dwarf2_full_name (const char *name,
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
1482static struct die_info *dwarf2_extension (struct die_info *die,
1483 struct dwarf2_cu **);
1484
1485static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487static void dump_die_for_error (struct die_info *);
1488
1489static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
1491
1492/*static*/ void dump_die (struct die_info *, int max_level);
1493
1494static void store_in_ref_table (struct die_info *,
1495 struct dwarf2_cu *);
1496
1497static struct die_info *follow_die_ref_or_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501static struct die_info *follow_die_ref (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505static struct die_info *follow_die_sig (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu **);
1508
1509static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512static struct type *get_DW_AT_signature_type (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu *);
1515
1516static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
1518
1519static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
1521
1522static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
1524 struct dynamic_prop *prop, struct type *type);
1525
1526/* memory allocation interface */
1527
1528static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1529
1530static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1531
1532static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1533
1534static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
1536 const struct attribute *attr);
1537
1538static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1539 struct symbol *sym,
1540 struct dwarf2_cu *cu,
1541 int is_block);
1542
1543static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
1546
1547static hashval_t partial_die_hash (const void *item);
1548
1549static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
1551static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
1558
1559static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1562
1563static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1565
1566static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1569
1570static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
1574
1575static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1576 enum language);
1577
1578static void process_full_type_unit (struct dwarf2_per_cu_data *,
1579 enum 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 struct dwarf2_per_cu_data *);
1590
1591static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1592
1593static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1594 enum language pretend_language);
1595
1596static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1597
1598/* Class, the destructor of which frees all allocated queue entries. This
1599 will only have work to do if an error was thrown while processing the
1600 dwarf. If no error was thrown then the queue entries should have all
1601 been processed, and freed, as we went along. */
1602
1603class dwarf2_queue_guard
1604{
1605public:
1606 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1607 : m_per_objfile (per_objfile)
1608 {
1609 }
1610
1611 /* Free any entries remaining on the queue. There should only be
1612 entries left if we hit an error while processing the dwarf. */
1613 ~dwarf2_queue_guard ()
1614 {
1615 /* Ensure that no memory is allocated by the queue. */
1616 std::queue<dwarf2_queue_item> empty;
1617 std::swap (m_per_objfile->per_bfd->queue, empty);
1618 }
1619
1620 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1621
1622private:
1623 dwarf2_per_objfile *m_per_objfile;
1624};
1625
1626dwarf2_queue_item::~dwarf2_queue_item ()
1627{
1628 /* Anything still marked queued is likely to be in an
1629 inconsistent state, so discard it. */
1630 if (per_cu->queued)
1631 {
1632 if (per_cu->cu != NULL)
1633 free_one_cached_comp_unit (per_cu);
1634 per_cu->queued = 0;
1635 }
1636}
1637
1638/* The return type of find_file_and_directory. Note, the enclosed
1639 string pointers are only valid while this object is valid. */
1640
1641struct file_and_directory
1642{
1643 /* The filename. This is never NULL. */
1644 const char *name;
1645
1646 /* The compilation directory. NULL if not known. If we needed to
1647 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1648 points directly to the DW_AT_comp_dir string attribute owned by
1649 the obstack that owns the DIE. */
1650 const char *comp_dir;
1651
1652 /* If we needed to build a new string for comp_dir, this is what
1653 owns the storage. */
1654 std::string comp_dir_storage;
1655};
1656
1657static file_and_directory find_file_and_directory (struct die_info *die,
1658 struct dwarf2_cu *cu);
1659
1660static htab_up allocate_signatured_type_table ();
1661
1662static htab_up allocate_dwo_unit_table ();
1663
1664static struct dwo_unit *lookup_dwo_unit_in_dwp
1665 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1666 struct dwp_file *dwp_file, const char *comp_dir,
1667 ULONGEST signature, int is_debug_types);
1668
1669static struct dwp_file *get_dwp_file
1670 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1671
1672static struct dwo_unit *lookup_dwo_comp_unit
1673 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1674
1675static struct dwo_unit *lookup_dwo_type_unit
1676 (struct signatured_type *, const char *, const char *);
1677
1678static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1679
1680/* A unique pointer to a dwo_file. */
1681
1682typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1683
1684static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1685
1686static void check_producer (struct dwarf2_cu *cu);
1687
1688static void free_line_header_voidp (void *arg);
1689\f
1690/* Various complaints about symbol reading that don't abort the process. */
1691
1692static void
1693dwarf2_debug_line_missing_file_complaint (void)
1694{
1695 complaint (_(".debug_line section has line data without a file"));
1696}
1697
1698static void
1699dwarf2_debug_line_missing_end_sequence_complaint (void)
1700{
1701 complaint (_(".debug_line section has line "
1702 "program sequence without an end"));
1703}
1704
1705static void
1706dwarf2_complex_location_expr_complaint (void)
1707{
1708 complaint (_("location expression too complex"));
1709}
1710
1711static void
1712dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1713 int arg3)
1714{
1715 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1716 arg1, arg2, arg3);
1717}
1718
1719static void
1720dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1721{
1722 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1723 arg1, arg2);
1724}
1725
1726/* Hash function for line_header_hash. */
1727
1728static hashval_t
1729line_header_hash (const struct line_header *ofs)
1730{
1731 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1732}
1733
1734/* Hash function for htab_create_alloc_ex for line_header_hash. */
1735
1736static hashval_t
1737line_header_hash_voidp (const void *item)
1738{
1739 const struct line_header *ofs = (const struct line_header *) item;
1740
1741 return line_header_hash (ofs);
1742}
1743
1744/* Equality function for line_header_hash. */
1745
1746static int
1747line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1748{
1749 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1750 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1751
1752 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1753 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1754}
1755
1756\f
1757
1758/* See declaration. */
1759
1760dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1761 bool can_copy_)
1762 : obfd (obfd),
1763 can_copy (can_copy_)
1764{
1765 if (names == NULL)
1766 names = &dwarf2_elf_names;
1767
1768 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1769 locate_sections (obfd, sec, *names);
1770}
1771
1772dwarf2_per_bfd::~dwarf2_per_bfd ()
1773{
1774 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1775 free_cached_comp_units ();
1776
1777 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1778 per_cu->imported_symtabs_free ();
1779
1780 for (signatured_type *sig_type : all_type_units)
1781 sig_type->per_cu.imported_symtabs_free ();
1782
1783 /* Everything else should be on this->obstack. */
1784}
1785
1786/* See declaration. */
1787
1788void
1789dwarf2_per_bfd::free_cached_comp_units ()
1790{
1791 dwarf2_per_cu_data *per_cu = read_in_chain;
1792 dwarf2_per_cu_data **last_chain = &read_in_chain;
1793 while (per_cu != NULL)
1794 {
1795 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1796
1797 delete per_cu->cu;
1798 *last_chain = next_cu;
1799 per_cu = next_cu;
1800 }
1801}
1802
1803/* A helper class that calls free_cached_comp_units on
1804 destruction. */
1805
1806class free_cached_comp_units
1807{
1808public:
1809
1810 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1811 : m_per_objfile (per_objfile)
1812 {
1813 }
1814
1815 ~free_cached_comp_units ()
1816 {
1817 m_per_objfile->per_bfd->free_cached_comp_units ();
1818 }
1819
1820 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1821
1822private:
1823
1824 dwarf2_per_objfile *m_per_objfile;
1825};
1826
1827/* See read.h. */
1828
1829bool
1830dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1831{
1832 gdb_assert (per_cu->index < this->m_symtabs.size ());
1833
1834 return this->m_symtabs[per_cu->index] != nullptr;
1835}
1836
1837/* See read.h. */
1838
1839compunit_symtab *
1840dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1841{
1842 gdb_assert (per_cu->index < this->m_symtabs.size ());
1843
1844 return this->m_symtabs[per_cu->index];
1845}
1846
1847/* See read.h. */
1848
1849void
1850dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1851 compunit_symtab *symtab)
1852{
1853 gdb_assert (per_cu->index < this->m_symtabs.size ());
1854 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1855
1856 this->m_symtabs[per_cu->index] = symtab;
1857}
1858
1859/* Try to locate the sections we need for DWARF 2 debugging
1860 information and return true if we have enough to do something.
1861 NAMES points to the dwarf2 section names, or is NULL if the standard
1862 ELF names are used. CAN_COPY is true for formats where symbol
1863 interposition is possible and so symbol values must follow copy
1864 relocation rules. */
1865
1866int
1867dwarf2_has_info (struct objfile *objfile,
1868 const struct dwarf2_debug_sections *names,
1869 bool can_copy)
1870{
1871 if (objfile->flags & OBJF_READNEVER)
1872 return 0;
1873
1874 struct dwarf2_per_objfile *dwarf2_per_objfile
1875 = get_dwarf2_per_objfile (objfile);
1876
1877 if (dwarf2_per_objfile == NULL)
1878 {
1879 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1880 sharing yet). */
1881 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1882
1883 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1884 }
1885
1886 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1887 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1888 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1889 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1890}
1891
1892/* When loading sections, we look either for uncompressed section or for
1893 compressed section names. */
1894
1895static int
1896section_is_p (const char *section_name,
1897 const struct dwarf2_section_names *names)
1898{
1899 if (names->normal != NULL
1900 && strcmp (section_name, names->normal) == 0)
1901 return 1;
1902 if (names->compressed != NULL
1903 && strcmp (section_name, names->compressed) == 0)
1904 return 1;
1905 return 0;
1906}
1907
1908/* See declaration. */
1909
1910void
1911dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1912 const dwarf2_debug_sections &names)
1913{
1914 flagword aflag = bfd_section_flags (sectp);
1915
1916 if ((aflag & SEC_HAS_CONTENTS) == 0)
1917 {
1918 }
1919 else if (elf_section_data (sectp)->this_hdr.sh_size
1920 > bfd_get_file_size (abfd))
1921 {
1922 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1923 warning (_("Discarding section %s which has a section size (%s"
1924 ") larger than the file size [in module %s]"),
1925 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1926 bfd_get_filename (abfd));
1927 }
1928 else if (section_is_p (sectp->name, &names.info))
1929 {
1930 this->info.s.section = sectp;
1931 this->info.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.abbrev))
1934 {
1935 this->abbrev.s.section = sectp;
1936 this->abbrev.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.line))
1939 {
1940 this->line.s.section = sectp;
1941 this->line.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.loc))
1944 {
1945 this->loc.s.section = sectp;
1946 this->loc.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.loclists))
1949 {
1950 this->loclists.s.section = sectp;
1951 this->loclists.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.macinfo))
1954 {
1955 this->macinfo.s.section = sectp;
1956 this->macinfo.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.macro))
1959 {
1960 this->macro.s.section = sectp;
1961 this->macro.size = bfd_section_size (sectp);
1962 }
1963 else if (section_is_p (sectp->name, &names.str))
1964 {
1965 this->str.s.section = sectp;
1966 this->str.size = bfd_section_size (sectp);
1967 }
1968 else if (section_is_p (sectp->name, &names.str_offsets))
1969 {
1970 this->str_offsets.s.section = sectp;
1971 this->str_offsets.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.line_str))
1974 {
1975 this->line_str.s.section = sectp;
1976 this->line_str.size = bfd_section_size (sectp);
1977 }
1978 else if (section_is_p (sectp->name, &names.addr))
1979 {
1980 this->addr.s.section = sectp;
1981 this->addr.size = bfd_section_size (sectp);
1982 }
1983 else if (section_is_p (sectp->name, &names.frame))
1984 {
1985 this->frame.s.section = sectp;
1986 this->frame.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.eh_frame))
1989 {
1990 this->eh_frame.s.section = sectp;
1991 this->eh_frame.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.ranges))
1994 {
1995 this->ranges.s.section = sectp;
1996 this->ranges.size = bfd_section_size (sectp);
1997 }
1998 else if (section_is_p (sectp->name, &names.rnglists))
1999 {
2000 this->rnglists.s.section = sectp;
2001 this->rnglists.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.types))
2004 {
2005 struct dwarf2_section_info type_section;
2006
2007 memset (&type_section, 0, sizeof (type_section));
2008 type_section.s.section = sectp;
2009 type_section.size = bfd_section_size (sectp);
2010
2011 this->types.push_back (type_section);
2012 }
2013 else if (section_is_p (sectp->name, &names.gdb_index))
2014 {
2015 this->gdb_index.s.section = sectp;
2016 this->gdb_index.size = bfd_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &names.debug_names))
2019 {
2020 this->debug_names.s.section = sectp;
2021 this->debug_names.size = bfd_section_size (sectp);
2022 }
2023 else if (section_is_p (sectp->name, &names.debug_aranges))
2024 {
2025 this->debug_aranges.s.section = sectp;
2026 this->debug_aranges.size = bfd_section_size (sectp);
2027 }
2028
2029 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2030 && bfd_section_vma (sectp) == 0)
2031 this->has_section_at_zero = true;
2032}
2033
2034/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2035 SECTION_NAME. */
2036
2037void
2038dwarf2_get_section_info (struct objfile *objfile,
2039 enum dwarf2_section_enum sect,
2040 asection **sectp, const gdb_byte **bufp,
2041 bfd_size_type *sizep)
2042{
2043 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2044 struct dwarf2_section_info *info;
2045
2046 /* We may see an objfile without any DWARF, in which case we just
2047 return nothing. */
2048 if (data == NULL)
2049 {
2050 *sectp = NULL;
2051 *bufp = NULL;
2052 *sizep = 0;
2053 return;
2054 }
2055 switch (sect)
2056 {
2057 case DWARF2_DEBUG_FRAME:
2058 info = &data->per_bfd->frame;
2059 break;
2060 case DWARF2_EH_FRAME:
2061 info = &data->per_bfd->eh_frame;
2062 break;
2063 default:
2064 gdb_assert_not_reached ("unexpected section");
2065 }
2066
2067 info->read (objfile);
2068
2069 *sectp = info->get_bfd_section ();
2070 *bufp = info->buffer;
2071 *sizep = info->size;
2072}
2073
2074/* A helper function to find the sections for a .dwz file. */
2075
2076static void
2077locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2078{
2079 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2080
2081 /* Note that we only support the standard ELF names, because .dwz
2082 is ELF-only (at the time of writing). */
2083 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2084 {
2085 dwz_file->abbrev.s.section = sectp;
2086 dwz_file->abbrev.size = bfd_section_size (sectp);
2087 }
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2089 {
2090 dwz_file->info.s.section = sectp;
2091 dwz_file->info.size = bfd_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2094 {
2095 dwz_file->str.s.section = sectp;
2096 dwz_file->str.size = bfd_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2099 {
2100 dwz_file->line.s.section = sectp;
2101 dwz_file->line.size = bfd_section_size (sectp);
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2104 {
2105 dwz_file->macro.s.section = sectp;
2106 dwz_file->macro.size = bfd_section_size (sectp);
2107 }
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2109 {
2110 dwz_file->gdb_index.s.section = sectp;
2111 dwz_file->gdb_index.size = bfd_section_size (sectp);
2112 }
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2114 {
2115 dwz_file->debug_names.s.section = sectp;
2116 dwz_file->debug_names.size = bfd_section_size (sectp);
2117 }
2118}
2119
2120/* See dwarf2read.h. */
2121
2122struct dwz_file *
2123dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2124{
2125 const char *filename;
2126 bfd_size_type buildid_len_arg;
2127 size_t buildid_len;
2128 bfd_byte *buildid;
2129
2130 if (per_bfd->dwz_file != NULL)
2131 return per_bfd->dwz_file.get ();
2132
2133 bfd_set_error (bfd_error_no_error);
2134 gdb::unique_xmalloc_ptr<char> data
2135 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2136 &buildid_len_arg, &buildid));
2137 if (data == NULL)
2138 {
2139 if (bfd_get_error () == bfd_error_no_error)
2140 return NULL;
2141 error (_("could not read '.gnu_debugaltlink' section: %s"),
2142 bfd_errmsg (bfd_get_error ()));
2143 }
2144
2145 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2146
2147 buildid_len = (size_t) buildid_len_arg;
2148
2149 filename = data.get ();
2150
2151 std::string abs_storage;
2152 if (!IS_ABSOLUTE_PATH (filename))
2153 {
2154 gdb::unique_xmalloc_ptr<char> abs
2155 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2156
2157 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2158 filename = abs_storage.c_str ();
2159 }
2160
2161 /* First try the file name given in the section. If that doesn't
2162 work, try to use the build-id instead. */
2163 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2164 if (dwz_bfd != NULL)
2165 {
2166 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2167 dwz_bfd.reset (nullptr);
2168 }
2169
2170 if (dwz_bfd == NULL)
2171 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2172
2173 if (dwz_bfd == nullptr)
2174 {
2175 gdb::unique_xmalloc_ptr<char> alt_filename;
2176 const char *origname = bfd_get_filename (per_bfd->obfd);
2177
2178 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2179 buildid_len,
2180 origname,
2181 &alt_filename));
2182
2183 if (fd.get () >= 0)
2184 {
2185 /* File successfully retrieved from server. */
2186 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2187
2188 if (dwz_bfd == nullptr)
2189 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2190 alt_filename.get ());
2191 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2192 dwz_bfd.reset (nullptr);
2193 }
2194 }
2195
2196 if (dwz_bfd == NULL)
2197 error (_("could not find '.gnu_debugaltlink' file for %s"),
2198 bfd_get_filename (per_bfd->obfd));
2199
2200 std::unique_ptr<struct dwz_file> result
2201 (new struct dwz_file (std::move (dwz_bfd)));
2202
2203 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2204 result.get ());
2205
2206 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2207 per_bfd->dwz_file = std::move (result);
2208 return per_bfd->dwz_file.get ();
2209}
2210\f
2211/* DWARF quick_symbols_functions support. */
2212
2213/* TUs can share .debug_line entries, and there can be a lot more TUs than
2214 unique line tables, so we maintain a separate table of all .debug_line
2215 derived entries to support the sharing.
2216 All the quick functions need is the list of file names. We discard the
2217 line_header when we're done and don't need to record it here. */
2218struct quick_file_names
2219{
2220 /* The data used to construct the hash key. */
2221 struct stmt_list_hash hash;
2222
2223 /* The number of entries in file_names, real_names. */
2224 unsigned int num_file_names;
2225
2226 /* The file names from the line table, after being run through
2227 file_full_name. */
2228 const char **file_names;
2229
2230 /* The file names from the line table after being run through
2231 gdb_realpath. These are computed lazily. */
2232 const char **real_names;
2233};
2234
2235/* When using the index (and thus not using psymtabs), each CU has an
2236 object of this type. This is used to hold information needed by
2237 the various "quick" methods. */
2238struct dwarf2_per_cu_quick_data
2239{
2240 /* The file table. This can be NULL if there was no file table
2241 or it's currently not read in.
2242 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2243 struct quick_file_names *file_names;
2244
2245 /* A temporary mark bit used when iterating over all CUs in
2246 expand_symtabs_matching. */
2247 unsigned int mark : 1;
2248
2249 /* True if we've tried to read the file table and found there isn't one.
2250 There will be no point in trying to read it again next time. */
2251 unsigned int no_file_data : 1;
2252};
2253
2254/* Utility hash function for a stmt_list_hash. */
2255
2256static hashval_t
2257hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2258{
2259 hashval_t v = 0;
2260
2261 if (stmt_list_hash->dwo_unit != NULL)
2262 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2263 v += to_underlying (stmt_list_hash->line_sect_off);
2264 return v;
2265}
2266
2267/* Utility equality function for a stmt_list_hash. */
2268
2269static int
2270eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2271 const struct stmt_list_hash *rhs)
2272{
2273 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2274 return 0;
2275 if (lhs->dwo_unit != NULL
2276 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2277 return 0;
2278
2279 return lhs->line_sect_off == rhs->line_sect_off;
2280}
2281
2282/* Hash function for a quick_file_names. */
2283
2284static hashval_t
2285hash_file_name_entry (const void *e)
2286{
2287 const struct quick_file_names *file_data
2288 = (const struct quick_file_names *) e;
2289
2290 return hash_stmt_list_entry (&file_data->hash);
2291}
2292
2293/* Equality function for a quick_file_names. */
2294
2295static int
2296eq_file_name_entry (const void *a, const void *b)
2297{
2298 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2299 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2300
2301 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2302}
2303
2304/* Delete function for a quick_file_names. */
2305
2306static void
2307delete_file_name_entry (void *e)
2308{
2309 struct quick_file_names *file_data = (struct quick_file_names *) e;
2310 int i;
2311
2312 for (i = 0; i < file_data->num_file_names; ++i)
2313 {
2314 xfree ((void*) file_data->file_names[i]);
2315 if (file_data->real_names)
2316 xfree ((void*) file_data->real_names[i]);
2317 }
2318
2319 /* The space for the struct itself lives on the obstack, so we don't
2320 free it here. */
2321}
2322
2323/* Create a quick_file_names hash table. */
2324
2325static htab_up
2326create_quick_file_names_table (unsigned int nr_initial_entries)
2327{
2328 return htab_up (htab_create_alloc (nr_initial_entries,
2329 hash_file_name_entry, eq_file_name_entry,
2330 delete_file_name_entry, xcalloc, xfree));
2331}
2332
2333/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2334 function is unrelated to symtabs, symtab would have to be created afterwards.
2335 You should call age_cached_comp_units after processing the CU. */
2336
2337static void
2338load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2339 bool skip_partial)
2340{
2341 if (per_cu->is_debug_types)
2342 load_full_type_unit (per_cu, per_objfile);
2343 else
2344 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2345
2346 if (per_cu->cu == NULL)
2347 return; /* Dummy CU. */
2348
2349 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2350}
2351
2352/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2353
2354static void
2355dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2356 dwarf2_per_objfile *dwarf2_per_objfile,
2357 bool skip_partial)
2358{
2359 /* Skip type_unit_groups, reading the type units they contain
2360 is handled elsewhere. */
2361 if (per_cu->type_unit_group_p ())
2362 return;
2363
2364 /* The destructor of dwarf2_queue_guard frees any entries left on
2365 the queue. After this point we're guaranteed to leave this function
2366 with the dwarf queue empty. */
2367 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2368
2369 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2370 {
2371 queue_comp_unit (per_cu, language_minimal);
2372 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2373
2374 /* If we just loaded a CU from a DWO, and we're working with an index
2375 that may badly handle TUs, load all the TUs in that DWO as well.
2376 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2377 if (!per_cu->is_debug_types
2378 && per_cu->cu != NULL
2379 && per_cu->cu->dwo_unit != NULL
2380 && dwarf2_per_objfile->per_bfd->index_table != NULL
2381 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2382 /* DWP files aren't supported yet. */
2383 && get_dwp_file (dwarf2_per_objfile) == NULL)
2384 queue_and_load_all_dwo_tus (per_cu);
2385 }
2386
2387 process_queue (dwarf2_per_objfile);
2388
2389 /* Age the cache, releasing compilation units that have not
2390 been used recently. */
2391 age_cached_comp_units (dwarf2_per_objfile);
2392}
2393
2394/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2395 the per-objfile for which this symtab is instantiated.
2396
2397 Returns the resulting symbol table. */
2398
2399static struct compunit_symtab *
2400dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2401 dwarf2_per_objfile *dwarf2_per_objfile,
2402 bool skip_partial)
2403{
2404 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2405
2406 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2407 {
2408 free_cached_comp_units freer (dwarf2_per_objfile);
2409 scoped_restore decrementer = increment_reading_symtab ();
2410 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2411 process_cu_includes (dwarf2_per_objfile);
2412 }
2413
2414 return dwarf2_per_objfile->get_symtab (per_cu);
2415}
2416
2417/* See declaration. */
2418
2419dwarf2_per_cu_data *
2420dwarf2_per_bfd::get_cutu (int index)
2421{
2422 if (index >= this->all_comp_units.size ())
2423 {
2424 index -= this->all_comp_units.size ();
2425 gdb_assert (index < this->all_type_units.size ());
2426 return &this->all_type_units[index]->per_cu;
2427 }
2428
2429 return this->all_comp_units[index];
2430}
2431
2432/* See declaration. */
2433
2434dwarf2_per_cu_data *
2435dwarf2_per_bfd::get_cu (int index)
2436{
2437 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2438
2439 return this->all_comp_units[index];
2440}
2441
2442/* See declaration. */
2443
2444signatured_type *
2445dwarf2_per_bfd::get_tu (int index)
2446{
2447 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2448
2449 return this->all_type_units[index];
2450}
2451
2452/* See read.h. */
2453
2454dwarf2_per_cu_data *
2455dwarf2_per_bfd::allocate_per_cu ()
2456{
2457 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2458 result->per_bfd = this;
2459 result->index = m_num_psymtabs++;
2460 return result;
2461}
2462
2463/* See read.h. */
2464
2465signatured_type *
2466dwarf2_per_bfd::allocate_signatured_type ()
2467{
2468 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2469 result->per_cu.per_bfd = this;
2470 result->per_cu.index = m_num_psymtabs++;
2471 return result;
2472}
2473
2474/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2475 obstack, and constructed with the specified field values. */
2476
2477static dwarf2_per_cu_data *
2478create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2479 struct dwarf2_section_info *section,
2480 int is_dwz,
2481 sect_offset sect_off, ULONGEST length)
2482{
2483 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2484 the_cu->sect_off = sect_off;
2485 the_cu->length = length;
2486 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2487 the_cu->section = section;
2488 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2489 struct dwarf2_per_cu_quick_data);
2490 the_cu->is_dwz = is_dwz;
2491 return the_cu;
2492}
2493
2494/* A helper for create_cus_from_index that handles a given list of
2495 CUs. */
2496
2497static void
2498create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2499 const gdb_byte *cu_list, offset_type n_elements,
2500 struct dwarf2_section_info *section,
2501 int is_dwz)
2502{
2503 for (offset_type i = 0; i < n_elements; i += 2)
2504 {
2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
2506
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2509 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2510 cu_list += 2 * 8;
2511
2512 dwarf2_per_cu_data *per_cu
2513 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2514 sect_off, length);
2515 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2516 }
2517}
2518
2519/* Read the CU list from the mapped index, and use it to create all
2520 the CU objects for this objfile. */
2521
2522static void
2523create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2524 const gdb_byte *cu_list, offset_type cu_list_elements,
2525 const gdb_byte *dwz_list, offset_type dwz_elements)
2526{
2527 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2528 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2529 ((cu_list_elements + dwz_elements) / 2);
2530
2531 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2532 &dwarf2_per_objfile->per_bfd->info, 0);
2533
2534 if (dwz_elements == 0)
2535 return;
2536
2537 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2538 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2539 &dwz->info, 1);
2540}
2541
2542/* Create the signatured type hash table from the index. */
2543
2544static void
2545create_signatured_type_table_from_index
2546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2547 struct dwarf2_section_info *section,
2548 const gdb_byte *bytes,
2549 offset_type elements)
2550{
2551 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2552 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2553
2554 htab_up sig_types_hash = allocate_signatured_type_table ();
2555
2556 for (offset_type i = 0; i < elements; i += 3)
2557 {
2558 struct signatured_type *sig_type;
2559 ULONGEST signature;
2560 void **slot;
2561 cu_offset type_offset_in_tu;
2562
2563 gdb_static_assert (sizeof (ULONGEST) >= 8);
2564 sect_offset sect_off
2565 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2566 type_offset_in_tu
2567 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2568 BFD_ENDIAN_LITTLE);
2569 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2570 bytes += 3 * 8;
2571
2572 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2573 sig_type->signature = signature;
2574 sig_type->type_offset_in_tu = type_offset_in_tu;
2575 sig_type->per_cu.is_debug_types = 1;
2576 sig_type->per_cu.section = section;
2577 sig_type->per_cu.sect_off = sect_off;
2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2579 sig_type->per_cu.v.quick
2580 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2581 struct dwarf2_per_cu_quick_data);
2582
2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2584 *slot = sig_type;
2585
2586 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2587 }
2588
2589 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2590}
2591
2592/* Create the signatured type hash table from .debug_names. */
2593
2594static void
2595create_signatured_type_table_from_debug_names
2596 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2597 const mapped_debug_names &map,
2598 struct dwarf2_section_info *section,
2599 struct dwarf2_section_info *abbrev_section)
2600{
2601 struct objfile *objfile = dwarf2_per_objfile->objfile;
2602
2603 section->read (objfile);
2604 abbrev_section->read (objfile);
2605
2606 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2607 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2608
2609 htab_up sig_types_hash = allocate_signatured_type_table ();
2610
2611 for (uint32_t i = 0; i < map.tu_count; ++i)
2612 {
2613 struct signatured_type *sig_type;
2614 void **slot;
2615
2616 sect_offset sect_off
2617 = (sect_offset) (extract_unsigned_integer
2618 (map.tu_table_reordered + i * map.offset_size,
2619 map.offset_size,
2620 map.dwarf5_byte_order));
2621
2622 comp_unit_head cu_header;
2623 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2624 abbrev_section,
2625 section->buffer + to_underlying (sect_off),
2626 rcuh_kind::TYPE);
2627
2628 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2629 sig_type->signature = cu_header.signature;
2630 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2631 sig_type->per_cu.is_debug_types = 1;
2632 sig_type->per_cu.section = section;
2633 sig_type->per_cu.sect_off = sect_off;
2634 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
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.dwarf2_per_objfile = dwarf2_per_objfile;
6325 sig_type->per_cu.is_debug_types = 1;
6326 sig_type->per_cu.section = section;
6327 sig_type->per_cu.sect_off = sect_off;
6328 sig_type->per_cu.length = length;
6329 }
6330
6331 slot = htab_find_slot (types_htab.get (),
6332 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6333 INSERT);
6334 gdb_assert (slot != NULL);
6335 if (*slot != NULL)
6336 {
6337 sect_offset dup_sect_off;
6338
6339 if (dwo_file)
6340 {
6341 const struct dwo_unit *dup_tu
6342 = (const struct dwo_unit *) *slot;
6343
6344 dup_sect_off = dup_tu->sect_off;
6345 }
6346 else
6347 {
6348 const struct signatured_type *dup_tu
6349 = (const struct signatured_type *) *slot;
6350
6351 dup_sect_off = dup_tu->per_cu.sect_off;
6352 }
6353
6354 complaint (_("debug type entry at offset %s is duplicate to"
6355 " the entry at offset %s, signature %s"),
6356 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6357 hex_string (header.signature));
6358 }
6359 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6360
6361 if (dwarf_read_debug > 1)
6362 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6363 sect_offset_str (sect_off),
6364 hex_string (header.signature));
6365
6366 info_ptr += length;
6367 }
6368}
6369
6370/* Create the hash table of all entries in the .debug_types
6371 (or .debug_types.dwo) section(s).
6372 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6373 otherwise it is NULL.
6374
6375 The result is a pointer to the hash table or NULL if there are no types.
6376
6377 Note: This function processes DWO files only, not DWP files. */
6378
6379static void
6380create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6381 struct dwo_file *dwo_file,
6382 gdb::array_view<dwarf2_section_info> type_sections,
6383 htab_up &types_htab)
6384{
6385 for (dwarf2_section_info &section : type_sections)
6386 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6387 types_htab, rcuh_kind::TYPE);
6388}
6389
6390/* Create the hash table of all entries in the .debug_types section,
6391 and initialize all_type_units.
6392 The result is zero if there is an error (e.g. missing .debug_types section),
6393 otherwise non-zero. */
6394
6395static int
6396create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6397{
6398 htab_up types_htab;
6399
6400 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6401 &dwarf2_per_objfile->per_bfd->info, types_htab,
6402 rcuh_kind::COMPILE);
6403 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6404 dwarf2_per_objfile->per_bfd->types, types_htab);
6405 if (types_htab == NULL)
6406 {
6407 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6408 return 0;
6409 }
6410
6411 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6412
6413 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6414 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6415 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6416
6417 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6418 add_signatured_type_cu_to_table,
6419 &dwarf2_per_objfile->per_bfd->all_type_units);
6420
6421 return 1;
6422}
6423
6424/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6425 If SLOT is non-NULL, it is the entry to use in the hash table.
6426 Otherwise we find one. */
6427
6428static struct signatured_type *
6429add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6430 void **slot)
6431{
6432 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6433 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6434 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6435
6436 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6437
6438 dwarf2_per_objfile->resize_symtabs ();
6439
6440 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6441 sig_type->signature = sig;
6442 sig_type->per_cu.is_debug_types = 1;
6443 if (dwarf2_per_objfile->per_bfd->using_index)
6444 {
6445 sig_type->per_cu.v.quick =
6446 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6447 struct dwarf2_per_cu_quick_data);
6448 }
6449
6450 if (slot == NULL)
6451 {
6452 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6453 sig_type, INSERT);
6454 }
6455 gdb_assert (*slot == NULL);
6456 *slot = sig_type;
6457 /* The rest of sig_type must be filled in by the caller. */
6458 return sig_type;
6459}
6460
6461/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6462 Fill in SIG_ENTRY with DWO_ENTRY. */
6463
6464static void
6465fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6466 struct signatured_type *sig_entry,
6467 struct dwo_unit *dwo_entry)
6468{
6469 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6470
6471 /* Make sure we're not clobbering something we don't expect to. */
6472 gdb_assert (! sig_entry->per_cu.queued);
6473 gdb_assert (sig_entry->per_cu.cu == NULL);
6474 if (per_bfd->using_index)
6475 {
6476 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6477 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6478 }
6479 else
6480 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6481 gdb_assert (sig_entry->signature == dwo_entry->signature);
6482 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6483 gdb_assert (sig_entry->type_unit_group == NULL);
6484 gdb_assert (sig_entry->dwo_unit == NULL);
6485
6486 sig_entry->per_cu.section = dwo_entry->section;
6487 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6488 sig_entry->per_cu.length = dwo_entry->length;
6489 sig_entry->per_cu.reading_dwo_directly = 1;
6490 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6491 sig_entry->per_cu.per_bfd = per_bfd;
6492 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6493 sig_entry->dwo_unit = dwo_entry;
6494}
6495
6496/* Subroutine of lookup_signatured_type.
6497 If we haven't read the TU yet, create the signatured_type data structure
6498 for a TU to be read in directly from a DWO file, bypassing the stub.
6499 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6500 using .gdb_index, then when reading a CU we want to stay in the DWO file
6501 containing that CU. Otherwise we could end up reading several other DWO
6502 files (due to comdat folding) to process the transitive closure of all the
6503 mentioned TUs, and that can be slow. The current DWO file will have every
6504 type signature that it needs.
6505 We only do this for .gdb_index because in the psymtab case we already have
6506 to read all the DWOs to build the type unit groups. */
6507
6508static struct signatured_type *
6509lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6510{
6511 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6512 struct dwo_file *dwo_file;
6513 struct dwo_unit find_dwo_entry, *dwo_entry;
6514 struct signatured_type find_sig_entry, *sig_entry;
6515 void **slot;
6516
6517 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6518
6519 /* If TU skeletons have been removed then we may not have read in any
6520 TUs yet. */
6521 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6522 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6523
6524 /* We only ever need to read in one copy of a signatured type.
6525 Use the global signatured_types array to do our own comdat-folding
6526 of types. If this is the first time we're reading this TU, and
6527 the TU has an entry in .gdb_index, replace the recorded data from
6528 .gdb_index with this TU. */
6529
6530 find_sig_entry.signature = sig;
6531 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6532 &find_sig_entry, INSERT);
6533 sig_entry = (struct signatured_type *) *slot;
6534
6535 /* We can get here with the TU already read, *or* in the process of being
6536 read. Don't reassign the global entry to point to this DWO if that's
6537 the case. Also note that if the TU is already being read, it may not
6538 have come from a DWO, the program may be a mix of Fission-compiled
6539 code and non-Fission-compiled code. */
6540
6541 /* Have we already tried to read this TU?
6542 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6543 needn't exist in the global table yet). */
6544 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6545 return sig_entry;
6546
6547 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6548 dwo_unit of the TU itself. */
6549 dwo_file = cu->dwo_unit->dwo_file;
6550
6551 /* Ok, this is the first time we're reading this TU. */
6552 if (dwo_file->tus == NULL)
6553 return NULL;
6554 find_dwo_entry.signature = sig;
6555 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6556 &find_dwo_entry);
6557 if (dwo_entry == NULL)
6558 return NULL;
6559
6560 /* If the global table doesn't have an entry for this TU, add one. */
6561 if (sig_entry == NULL)
6562 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6563
6564 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6565 sig_entry->per_cu.tu_read = 1;
6566 return sig_entry;
6567}
6568
6569/* Subroutine of lookup_signatured_type.
6570 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6571 then try the DWP file. If the TU stub (skeleton) has been removed then
6572 it won't be in .gdb_index. */
6573
6574static struct signatured_type *
6575lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6576{
6577 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6578 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6579 struct dwo_unit *dwo_entry;
6580 struct signatured_type find_sig_entry, *sig_entry;
6581 void **slot;
6582
6583 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6584 gdb_assert (dwp_file != NULL);
6585
6586 /* If TU skeletons have been removed then we may not have read in any
6587 TUs yet. */
6588 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6589 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6590
6591 find_sig_entry.signature = sig;
6592 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6593 &find_sig_entry, INSERT);
6594 sig_entry = (struct signatured_type *) *slot;
6595
6596 /* Have we already tried to read this TU?
6597 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6598 needn't exist in the global table yet). */
6599 if (sig_entry != NULL)
6600 return sig_entry;
6601
6602 if (dwp_file->tus == NULL)
6603 return NULL;
6604 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6605 sig, 1 /* is_debug_types */);
6606 if (dwo_entry == NULL)
6607 return NULL;
6608
6609 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6610 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6611
6612 return sig_entry;
6613}
6614
6615/* Lookup a signature based type for DW_FORM_ref_sig8.
6616 Returns NULL if signature SIG is not present in the table.
6617 It is up to the caller to complain about this. */
6618
6619static struct signatured_type *
6620lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6621{
6622 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6623
6624 if (cu->dwo_unit
6625 && dwarf2_per_objfile->per_bfd->using_index)
6626 {
6627 /* We're in a DWO/DWP file, and we're using .gdb_index.
6628 These cases require special processing. */
6629 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6630 return lookup_dwo_signatured_type (cu, sig);
6631 else
6632 return lookup_dwp_signatured_type (cu, sig);
6633 }
6634 else
6635 {
6636 struct signatured_type find_entry, *entry;
6637
6638 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6639 return NULL;
6640 find_entry.signature = sig;
6641 entry = ((struct signatured_type *)
6642 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6643 &find_entry));
6644 return entry;
6645 }
6646}
6647
6648/* Low level DIE reading support. */
6649
6650/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6651
6652static void
6653init_cu_die_reader (struct die_reader_specs *reader,
6654 struct dwarf2_cu *cu,
6655 struct dwarf2_section_info *section,
6656 struct dwo_file *dwo_file,
6657 struct abbrev_table *abbrev_table)
6658{
6659 gdb_assert (section->readin && section->buffer != NULL);
6660 reader->abfd = section->get_bfd_owner ();
6661 reader->cu = cu;
6662 reader->dwo_file = dwo_file;
6663 reader->die_section = section;
6664 reader->buffer = section->buffer;
6665 reader->buffer_end = section->buffer + section->size;
6666 reader->abbrev_table = abbrev_table;
6667}
6668
6669/* Subroutine of cutu_reader to simplify it.
6670 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6671 There's just a lot of work to do, and cutu_reader is big enough
6672 already.
6673
6674 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6675 from it to the DIE in the DWO. If NULL we are skipping the stub.
6676 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6677 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6678 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6679 STUB_COMP_DIR may be non-NULL.
6680 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6681 are filled in with the info of the DIE from the DWO file.
6682 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6683 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6684 kept around for at least as long as *RESULT_READER.
6685
6686 The result is non-zero if a valid (non-dummy) DIE was found. */
6687
6688static int
6689read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6690 struct dwo_unit *dwo_unit,
6691 struct die_info *stub_comp_unit_die,
6692 const char *stub_comp_dir,
6693 struct die_reader_specs *result_reader,
6694 const gdb_byte **result_info_ptr,
6695 struct die_info **result_comp_unit_die,
6696 abbrev_table_up *result_dwo_abbrev_table)
6697{
6698 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6699 struct objfile *objfile = dwarf2_per_objfile->objfile;
6700 struct dwarf2_cu *cu = this_cu->cu;
6701 bfd *abfd;
6702 const gdb_byte *begin_info_ptr, *info_ptr;
6703 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6704 int i,num_extra_attrs;
6705 struct dwarf2_section_info *dwo_abbrev_section;
6706 struct die_info *comp_unit_die;
6707
6708 /* At most one of these may be provided. */
6709 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6710
6711 /* These attributes aren't processed until later:
6712 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6713 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6714 referenced later. However, these attributes are found in the stub
6715 which we won't have later. In order to not impose this complication
6716 on the rest of the code, we read them here and copy them to the
6717 DWO CU/TU die. */
6718
6719 stmt_list = NULL;
6720 low_pc = NULL;
6721 high_pc = NULL;
6722 ranges = NULL;
6723 comp_dir = NULL;
6724
6725 if (stub_comp_unit_die != NULL)
6726 {
6727 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6728 DWO file. */
6729 if (! this_cu->is_debug_types)
6730 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6731 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6732 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6733 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6734 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6735
6736 cu->addr_base = stub_comp_unit_die->addr_base ();
6737
6738 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6739 here (if needed). We need the value before we can process
6740 DW_AT_ranges. */
6741 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6742 }
6743 else if (stub_comp_dir != NULL)
6744 {
6745 /* Reconstruct the comp_dir attribute to simplify the code below. */
6746 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6747 comp_dir->name = DW_AT_comp_dir;
6748 comp_dir->form = DW_FORM_string;
6749 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6750 DW_STRING (comp_dir) = stub_comp_dir;
6751 }
6752
6753 /* Set up for reading the DWO CU/TU. */
6754 cu->dwo_unit = dwo_unit;
6755 dwarf2_section_info *section = dwo_unit->section;
6756 section->read (objfile);
6757 abfd = section->get_bfd_owner ();
6758 begin_info_ptr = info_ptr = (section->buffer
6759 + to_underlying (dwo_unit->sect_off));
6760 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6761
6762 if (this_cu->is_debug_types)
6763 {
6764 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6765
6766 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6767 &cu->header, section,
6768 dwo_abbrev_section,
6769 info_ptr, rcuh_kind::TYPE);
6770 /* This is not an assert because it can be caused by bad debug info. */
6771 if (sig_type->signature != cu->header.signature)
6772 {
6773 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6774 " TU at offset %s [in module %s]"),
6775 hex_string (sig_type->signature),
6776 hex_string (cu->header.signature),
6777 sect_offset_str (dwo_unit->sect_off),
6778 bfd_get_filename (abfd));
6779 }
6780 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6781 /* For DWOs coming from DWP files, we don't know the CU length
6782 nor the type's offset in the TU until now. */
6783 dwo_unit->length = cu->header.get_length ();
6784 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6785
6786 /* Establish the type offset that can be used to lookup the type.
6787 For DWO files, we don't know it until now. */
6788 sig_type->type_offset_in_section
6789 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6790 }
6791 else
6792 {
6793 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6794 &cu->header, section,
6795 dwo_abbrev_section,
6796 info_ptr, rcuh_kind::COMPILE);
6797 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6798 /* For DWOs coming from DWP files, we don't know the CU length
6799 until now. */
6800 dwo_unit->length = cu->header.get_length ();
6801 }
6802
6803 *result_dwo_abbrev_table
6804 = abbrev_table::read (objfile, dwo_abbrev_section,
6805 cu->header.abbrev_sect_off);
6806 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6807 result_dwo_abbrev_table->get ());
6808
6809 /* Read in the die, but leave space to copy over the attributes
6810 from the stub. This has the benefit of simplifying the rest of
6811 the code - all the work to maintain the illusion of a single
6812 DW_TAG_{compile,type}_unit DIE is done here. */
6813 num_extra_attrs = ((stmt_list != NULL)
6814 + (low_pc != NULL)
6815 + (high_pc != NULL)
6816 + (ranges != NULL)
6817 + (comp_dir != NULL));
6818 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6819 num_extra_attrs);
6820
6821 /* Copy over the attributes from the stub to the DIE we just read in. */
6822 comp_unit_die = *result_comp_unit_die;
6823 i = comp_unit_die->num_attrs;
6824 if (stmt_list != NULL)
6825 comp_unit_die->attrs[i++] = *stmt_list;
6826 if (low_pc != NULL)
6827 comp_unit_die->attrs[i++] = *low_pc;
6828 if (high_pc != NULL)
6829 comp_unit_die->attrs[i++] = *high_pc;
6830 if (ranges != NULL)
6831 comp_unit_die->attrs[i++] = *ranges;
6832 if (comp_dir != NULL)
6833 comp_unit_die->attrs[i++] = *comp_dir;
6834 comp_unit_die->num_attrs += num_extra_attrs;
6835
6836 if (dwarf_die_debug)
6837 {
6838 fprintf_unfiltered (gdb_stdlog,
6839 "Read die from %s@0x%x of %s:\n",
6840 section->get_name (),
6841 (unsigned) (begin_info_ptr - section->buffer),
6842 bfd_get_filename (abfd));
6843 dump_die (comp_unit_die, dwarf_die_debug);
6844 }
6845
6846 /* Skip dummy compilation units. */
6847 if (info_ptr >= begin_info_ptr + dwo_unit->length
6848 || peek_abbrev_code (abfd, info_ptr) == 0)
6849 return 0;
6850
6851 *result_info_ptr = info_ptr;
6852 return 1;
6853}
6854
6855/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6856 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6857 signature is part of the header. */
6858static gdb::optional<ULONGEST>
6859lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6860{
6861 if (cu->header.version >= 5)
6862 return cu->header.signature;
6863 struct attribute *attr;
6864 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6865 if (attr == nullptr)
6866 return gdb::optional<ULONGEST> ();
6867 return DW_UNSND (attr);
6868}
6869
6870/* Subroutine of cutu_reader to simplify it.
6871 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6872 Returns NULL if the specified DWO unit cannot be found. */
6873
6874static struct dwo_unit *
6875lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6876 struct die_info *comp_unit_die,
6877 const char *dwo_name)
6878{
6879 struct dwarf2_cu *cu = this_cu->cu;
6880 struct dwo_unit *dwo_unit;
6881 const char *comp_dir;
6882
6883 gdb_assert (cu != NULL);
6884
6885 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6886 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6887 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6888
6889 if (this_cu->is_debug_types)
6890 {
6891 struct signatured_type *sig_type;
6892
6893 /* Since this_cu is the first member of struct signatured_type,
6894 we can go from a pointer to one to a pointer to the other. */
6895 sig_type = (struct signatured_type *) this_cu;
6896 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6897 }
6898 else
6899 {
6900 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6901 if (!signature.has_value ())
6902 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6903 " [in module %s]"),
6904 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
6905 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6906 *signature);
6907 }
6908
6909 return dwo_unit;
6910}
6911
6912/* Subroutine of cutu_reader to simplify it.
6913 See it for a description of the parameters.
6914 Read a TU directly from a DWO file, bypassing the stub. */
6915
6916void
6917cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6918 dwarf2_per_objfile *per_objfile,
6919 int use_existing_cu)
6920{
6921 struct signatured_type *sig_type;
6922
6923 /* Verify we can do the following downcast, and that we have the
6924 data we need. */
6925 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6926 sig_type = (struct signatured_type *) this_cu;
6927 gdb_assert (sig_type->dwo_unit != NULL);
6928
6929 if (use_existing_cu && this_cu->cu != NULL)
6930 {
6931 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6932 /* There's no need to do the rereading_dwo_cu handling that
6933 cutu_reader does since we don't read the stub. */
6934 }
6935 else
6936 {
6937 /* If !use_existing_cu, this_cu->cu must be NULL. */
6938 gdb_assert (this_cu->cu == NULL);
6939 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6940 }
6941
6942 /* A future optimization, if needed, would be to use an existing
6943 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6944 could share abbrev tables. */
6945
6946 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6947 NULL /* stub_comp_unit_die */,
6948 sig_type->dwo_unit->dwo_file->comp_dir,
6949 this, &info_ptr,
6950 &comp_unit_die,
6951 &m_dwo_abbrev_table) == 0)
6952 {
6953 /* Dummy die. */
6954 dummy_p = true;
6955 }
6956}
6957
6958/* Initialize a CU (or TU) and read its DIEs.
6959 If the CU defers to a DWO file, read the DWO file as well.
6960
6961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6962 Otherwise the table specified in the comp unit header is read in and used.
6963 This is an optimization for when we already have the abbrev table.
6964
6965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6966 Otherwise, a new CU is allocated with xmalloc. */
6967
6968cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6969 dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct abbrev_table *abbrev_table,
6971 int use_existing_cu,
6972 bool skip_partial)
6973 : die_reader_specs {},
6974 m_this_cu (this_cu)
6975{
6976 struct objfile *objfile = dwarf2_per_objfile->objfile;
6977 struct dwarf2_section_info *section = this_cu->section;
6978 bfd *abfd = section->get_bfd_owner ();
6979 struct dwarf2_cu *cu;
6980 const gdb_byte *begin_info_ptr;
6981 struct signatured_type *sig_type = NULL;
6982 struct dwarf2_section_info *abbrev_section;
6983 /* Non-zero if CU currently points to a DWO file and we need to
6984 reread it. When this happens we need to reread the skeleton die
6985 before we can reread the DWO file (this only applies to CUs, not TUs). */
6986 int rereading_dwo_cu = 0;
6987
6988 if (dwarf_die_debug)
6989 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6990 this_cu->is_debug_types ? "type" : "comp",
6991 sect_offset_str (this_cu->sect_off));
6992
6993 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6994 file (instead of going through the stub), short-circuit all of this. */
6995 if (this_cu->reading_dwo_directly)
6996 {
6997 /* Narrow down the scope of possibilities to have to understand. */
6998 gdb_assert (this_cu->is_debug_types);
6999 gdb_assert (abbrev_table == NULL);
7000 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7001 return;
7002 }
7003
7004 /* This is cheap if the section is already read in. */
7005 section->read (objfile);
7006
7007 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7008
7009 abbrev_section = get_abbrev_section_for_cu (this_cu);
7010
7011 if (use_existing_cu && this_cu->cu != NULL)
7012 {
7013 cu = this_cu->cu;
7014 /* If this CU is from a DWO file we need to start over, we need to
7015 refetch the attributes from the skeleton CU.
7016 This could be optimized by retrieving those attributes from when we
7017 were here the first time: the previous comp_unit_die was stored in
7018 comp_unit_obstack. But there's no data yet that we need this
7019 optimization. */
7020 if (cu->dwo_unit != NULL)
7021 rereading_dwo_cu = 1;
7022 }
7023 else
7024 {
7025 /* If !use_existing_cu, this_cu->cu must be NULL. */
7026 gdb_assert (this_cu->cu == NULL);
7027 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7028 cu = m_new_cu.get ();
7029 }
7030
7031 /* Get the header. */
7032 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7033 {
7034 /* We already have the header, there's no need to read it in again. */
7035 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7036 }
7037 else
7038 {
7039 if (this_cu->is_debug_types)
7040 {
7041 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7042 &cu->header, section,
7043 abbrev_section, info_ptr,
7044 rcuh_kind::TYPE);
7045
7046 /* Since per_cu is the first member of struct signatured_type,
7047 we can go from a pointer to one to a pointer to the other. */
7048 sig_type = (struct signatured_type *) this_cu;
7049 gdb_assert (sig_type->signature == cu->header.signature);
7050 gdb_assert (sig_type->type_offset_in_tu
7051 == cu->header.type_cu_offset_in_tu);
7052 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7053
7054 /* LENGTH has not been set yet for type units if we're
7055 using .gdb_index. */
7056 this_cu->length = cu->header.get_length ();
7057
7058 /* Establish the type offset that can be used to lookup the type. */
7059 sig_type->type_offset_in_section =
7060 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7061
7062 this_cu->dwarf_version = cu->header.version;
7063 }
7064 else
7065 {
7066 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7067 &cu->header, section,
7068 abbrev_section,
7069 info_ptr,
7070 rcuh_kind::COMPILE);
7071
7072 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7073 if (this_cu->length == 0)
7074 this_cu->length = cu->header.get_length ();
7075 else
7076 gdb_assert (this_cu->length == cu->header.get_length ());
7077 this_cu->dwarf_version = cu->header.version;
7078 }
7079 }
7080
7081 /* Skip dummy compilation units. */
7082 if (info_ptr >= begin_info_ptr + this_cu->length
7083 || peek_abbrev_code (abfd, info_ptr) == 0)
7084 {
7085 dummy_p = true;
7086 return;
7087 }
7088
7089 /* If we don't have them yet, read the abbrevs for this compilation unit.
7090 And if we need to read them now, make sure they're freed when we're
7091 done. */
7092 if (abbrev_table != NULL)
7093 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7094 else
7095 {
7096 m_abbrev_table_holder
7097 = abbrev_table::read (objfile, abbrev_section,
7098 cu->header.abbrev_sect_off);
7099 abbrev_table = m_abbrev_table_holder.get ();
7100 }
7101
7102 /* Read the top level CU/TU die. */
7103 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7104 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7105
7106 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7107 {
7108 dummy_p = true;
7109 return;
7110 }
7111
7112 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7113 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7114 table from the DWO file and pass the ownership over to us. It will be
7115 referenced from READER, so we must make sure to free it after we're done
7116 with READER.
7117
7118 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7119 DWO CU, that this test will fail (the attribute will not be present). */
7120 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7121 if (dwo_name != nullptr)
7122 {
7123 struct dwo_unit *dwo_unit;
7124 struct die_info *dwo_comp_unit_die;
7125
7126 if (comp_unit_die->has_children)
7127 {
7128 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7129 " has children (offset %s) [in module %s]"),
7130 sect_offset_str (this_cu->sect_off),
7131 bfd_get_filename (abfd));
7132 }
7133 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7134 if (dwo_unit != NULL)
7135 {
7136 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7137 comp_unit_die, NULL,
7138 this, &info_ptr,
7139 &dwo_comp_unit_die,
7140 &m_dwo_abbrev_table) == 0)
7141 {
7142 /* Dummy die. */
7143 dummy_p = true;
7144 return;
7145 }
7146 comp_unit_die = dwo_comp_unit_die;
7147 }
7148 else
7149 {
7150 /* Yikes, we couldn't find the rest of the DIE, we only have
7151 the stub. A complaint has already been logged. There's
7152 not much more we can do except pass on the stub DIE to
7153 die_reader_func. We don't want to throw an error on bad
7154 debug info. */
7155 }
7156 }
7157}
7158
7159void
7160cutu_reader::keep ()
7161{
7162 /* Done, clean up. */
7163 gdb_assert (!dummy_p);
7164 if (m_new_cu != NULL)
7165 {
7166 /* We know that m_this_cu->cu is set, since we are in the process of
7167 parsing the CU. */
7168 gdb_assert (m_this_cu->cu != nullptr);
7169 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7170
7171 /* Link this CU into read_in_chain. */
7172 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7173 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7174 /* The chain owns it now. */
7175 m_new_cu.release ();
7176 }
7177}
7178
7179/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7180 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7181 assumed to have already done the lookup to find the DWO file).
7182
7183 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7184 THIS_CU->is_debug_types, but nothing else.
7185
7186 We fill in THIS_CU->length.
7187
7188 THIS_CU->cu is always freed when done.
7189 This is done in order to not leave THIS_CU->cu in a state where we have
7190 to care whether it refers to the "main" CU or the DWO CU.
7191
7192 When parent_cu is passed, it is used to provide a default value for
7193 str_offsets_base and addr_base from the parent. */
7194
7195cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7196 dwarf2_per_objfile *dwarf2_per_objfile,
7197 struct dwarf2_cu *parent_cu,
7198 struct dwo_file *dwo_file)
7199 : die_reader_specs {},
7200 m_this_cu (this_cu)
7201{
7202 struct objfile *objfile = dwarf2_per_objfile->objfile;
7203 struct dwarf2_section_info *section = this_cu->section;
7204 bfd *abfd = section->get_bfd_owner ();
7205 struct dwarf2_section_info *abbrev_section;
7206 const gdb_byte *begin_info_ptr, *info_ptr;
7207
7208 if (dwarf_die_debug)
7209 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7210 this_cu->is_debug_types ? "type" : "comp",
7211 sect_offset_str (this_cu->sect_off));
7212
7213 gdb_assert (this_cu->cu == NULL);
7214
7215 abbrev_section = (dwo_file != NULL
7216 ? &dwo_file->sections.abbrev
7217 : get_abbrev_section_for_cu (this_cu));
7218
7219 /* This is cheap if the section is already read in. */
7220 section->read (objfile);
7221
7222 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7223
7224 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7225 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7226 &m_new_cu->header, section,
7227 abbrev_section, info_ptr,
7228 (this_cu->is_debug_types
7229 ? rcuh_kind::TYPE
7230 : rcuh_kind::COMPILE));
7231
7232 if (parent_cu != nullptr)
7233 {
7234 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7235 m_new_cu->addr_base = parent_cu->addr_base;
7236 }
7237 this_cu->length = m_new_cu->header.get_length ();
7238
7239 /* Skip dummy compilation units. */
7240 if (info_ptr >= begin_info_ptr + this_cu->length
7241 || peek_abbrev_code (abfd, info_ptr) == 0)
7242 {
7243 dummy_p = true;
7244 return;
7245 }
7246
7247 m_abbrev_table_holder
7248 = abbrev_table::read (objfile, abbrev_section,
7249 m_new_cu->header.abbrev_sect_off);
7250
7251 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7252 m_abbrev_table_holder.get ());
7253 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7254}
7255
7256\f
7257/* Type Unit Groups.
7258
7259 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7260 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7261 so that all types coming from the same compilation (.o file) are grouped
7262 together. A future step could be to put the types in the same symtab as
7263 the CU the types ultimately came from. */
7264
7265static hashval_t
7266hash_type_unit_group (const void *item)
7267{
7268 const struct type_unit_group *tu_group
7269 = (const struct type_unit_group *) item;
7270
7271 return hash_stmt_list_entry (&tu_group->hash);
7272}
7273
7274static int
7275eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7276{
7277 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7278 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7279
7280 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7281}
7282
7283/* Allocate a hash table for type unit groups. */
7284
7285static htab_up
7286allocate_type_unit_groups_table ()
7287{
7288 return htab_up (htab_create_alloc (3,
7289 hash_type_unit_group,
7290 eq_type_unit_group,
7291 NULL, xcalloc, xfree));
7292}
7293
7294/* Type units that don't have DW_AT_stmt_list are grouped into their own
7295 partial symtabs. We combine several TUs per psymtab to not let the size
7296 of any one psymtab grow too big. */
7297#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7298#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7299
7300/* Helper routine for get_type_unit_group.
7301 Create the type_unit_group object used to hold one or more TUs. */
7302
7303static struct type_unit_group *
7304create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7305{
7306 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7307 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7308 struct dwarf2_per_cu_data *per_cu;
7309 struct type_unit_group *tu_group;
7310
7311 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7312 struct type_unit_group);
7313 per_cu = &tu_group->per_cu;
7314 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7315 per_cu->per_bfd = per_bfd;
7316
7317 if (per_bfd->using_index)
7318 {
7319 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7320 struct dwarf2_per_cu_quick_data);
7321 }
7322 else
7323 {
7324 unsigned int line_offset = to_underlying (line_offset_struct);
7325 dwarf2_psymtab *pst;
7326 std::string name;
7327
7328 /* Give the symtab a useful name for debug purposes. */
7329 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7330 name = string_printf ("<type_units_%d>",
7331 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7332 else
7333 name = string_printf ("<type_units_at_0x%x>", line_offset);
7334
7335 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7336 pst->anonymous = true;
7337 }
7338
7339 tu_group->hash.dwo_unit = cu->dwo_unit;
7340 tu_group->hash.line_sect_off = line_offset_struct;
7341
7342 return tu_group;
7343}
7344
7345/* Look up the type_unit_group for type unit CU, and create it if necessary.
7346 STMT_LIST is a DW_AT_stmt_list attribute. */
7347
7348static struct type_unit_group *
7349get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7350{
7351 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7352 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7353 struct type_unit_group *tu_group;
7354 void **slot;
7355 unsigned int line_offset;
7356 struct type_unit_group type_unit_group_for_lookup;
7357
7358 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7359 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7360
7361 /* Do we need to create a new group, or can we use an existing one? */
7362
7363 if (stmt_list)
7364 {
7365 line_offset = DW_UNSND (stmt_list);
7366 ++tu_stats->nr_symtab_sharers;
7367 }
7368 else
7369 {
7370 /* Ugh, no stmt_list. Rare, but we have to handle it.
7371 We can do various things here like create one group per TU or
7372 spread them over multiple groups to split up the expansion work.
7373 To avoid worst case scenarios (too many groups or too large groups)
7374 we, umm, group them in bunches. */
7375 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7376 | (tu_stats->nr_stmt_less_type_units
7377 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7378 ++tu_stats->nr_stmt_less_type_units;
7379 }
7380
7381 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7382 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7383 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7384 &type_unit_group_for_lookup, INSERT);
7385 if (*slot != NULL)
7386 {
7387 tu_group = (struct type_unit_group *) *slot;
7388 gdb_assert (tu_group != NULL);
7389 }
7390 else
7391 {
7392 sect_offset line_offset_struct = (sect_offset) line_offset;
7393 tu_group = create_type_unit_group (cu, line_offset_struct);
7394 *slot = tu_group;
7395 ++tu_stats->nr_symtabs;
7396 }
7397
7398 return tu_group;
7399}
7400\f
7401/* Partial symbol tables. */
7402
7403/* Create a psymtab named NAME and assign it to PER_CU.
7404
7405 The caller must fill in the following details:
7406 dirname, textlow, texthigh. */
7407
7408static dwarf2_psymtab *
7409create_partial_symtab (dwarf2_per_cu_data *per_cu,
7410 dwarf2_per_objfile *per_objfile,
7411 const char *name)
7412{
7413 struct objfile *objfile = per_objfile->objfile;
7414 dwarf2_psymtab *pst;
7415
7416 pst = new dwarf2_psymtab (name, objfile, per_cu);
7417
7418 pst->psymtabs_addrmap_supported = true;
7419
7420 /* This is the glue that links PST into GDB's symbol API. */
7421 per_cu->v.psymtab = pst;
7422
7423 return pst;
7424}
7425
7426/* DIE reader function for process_psymtab_comp_unit. */
7427
7428static void
7429process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7430 const gdb_byte *info_ptr,
7431 struct die_info *comp_unit_die,
7432 enum language pretend_language)
7433{
7434 struct dwarf2_cu *cu = reader->cu;
7435 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7436 struct objfile *objfile = per_objfile->objfile;
7437 struct gdbarch *gdbarch = objfile->arch ();
7438 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7439 CORE_ADDR baseaddr;
7440 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7441 dwarf2_psymtab *pst;
7442 enum pc_bounds_kind cu_bounds_kind;
7443 const char *filename;
7444
7445 gdb_assert (! per_cu->is_debug_types);
7446
7447 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7448
7449 /* Allocate a new partial symbol table structure. */
7450 gdb::unique_xmalloc_ptr<char> debug_filename;
7451 static const char artificial[] = "<artificial>";
7452 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7453 if (filename == NULL)
7454 filename = "";
7455 else if (strcmp (filename, artificial) == 0)
7456 {
7457 debug_filename.reset (concat (artificial, "@",
7458 sect_offset_str (per_cu->sect_off),
7459 (char *) NULL));
7460 filename = debug_filename.get ();
7461 }
7462
7463 pst = create_partial_symtab (per_cu, per_objfile, filename);
7464
7465 /* This must be done before calling dwarf2_build_include_psymtabs. */
7466 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7467
7468 baseaddr = objfile->text_section_offset ();
7469
7470 dwarf2_find_base_address (comp_unit_die, cu);
7471
7472 /* Possibly set the default values of LOWPC and HIGHPC from
7473 `DW_AT_ranges'. */
7474 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7475 &best_highpc, cu, pst);
7476 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7477 {
7478 CORE_ADDR low
7479 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7480 - baseaddr);
7481 CORE_ADDR high
7482 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7483 - baseaddr - 1);
7484 /* Store the contiguous range if it is not empty; it can be
7485 empty for CUs with no code. */
7486 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7487 low, high, pst);
7488 }
7489
7490 /* Check if comp unit has_children.
7491 If so, read the rest of the partial symbols from this comp unit.
7492 If not, there's no more debug_info for this comp unit. */
7493 if (comp_unit_die->has_children)
7494 {
7495 struct partial_die_info *first_die;
7496 CORE_ADDR lowpc, highpc;
7497
7498 lowpc = ((CORE_ADDR) -1);
7499 highpc = ((CORE_ADDR) 0);
7500
7501 first_die = load_partial_dies (reader, info_ptr, 1);
7502
7503 scan_partial_symbols (first_die, &lowpc, &highpc,
7504 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7505
7506 /* If we didn't find a lowpc, set it to highpc to avoid
7507 complaints from `maint check'. */
7508 if (lowpc == ((CORE_ADDR) -1))
7509 lowpc = highpc;
7510
7511 /* If the compilation unit didn't have an explicit address range,
7512 then use the information extracted from its child dies. */
7513 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7514 {
7515 best_lowpc = lowpc;
7516 best_highpc = highpc;
7517 }
7518 }
7519 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7520 best_lowpc + baseaddr)
7521 - baseaddr);
7522 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7523 best_highpc + baseaddr)
7524 - baseaddr);
7525
7526 end_psymtab_common (objfile, pst);
7527
7528 if (!cu->per_cu->imported_symtabs_empty ())
7529 {
7530 int i;
7531 int len = cu->per_cu->imported_symtabs_size ();
7532
7533 /* Fill in 'dependencies' here; we fill in 'users' in a
7534 post-pass. */
7535 pst->number_of_dependencies = len;
7536 pst->dependencies
7537 = objfile->partial_symtabs->allocate_dependencies (len);
7538 for (i = 0; i < len; ++i)
7539 {
7540 pst->dependencies[i]
7541 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7542 }
7543
7544 cu->per_cu->imported_symtabs_free ();
7545 }
7546
7547 /* Get the list of files included in the current compilation unit,
7548 and build a psymtab for each of them. */
7549 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7550
7551 if (dwarf_read_debug)
7552 fprintf_unfiltered (gdb_stdlog,
7553 "Psymtab for %s unit @%s: %s - %s"
7554 ", %d global, %d static syms\n",
7555 per_cu->is_debug_types ? "type" : "comp",
7556 sect_offset_str (per_cu->sect_off),
7557 paddress (gdbarch, pst->text_low (objfile)),
7558 paddress (gdbarch, pst->text_high (objfile)),
7559 pst->n_global_syms, pst->n_static_syms);
7560}
7561
7562/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7563 Process compilation unit THIS_CU for a psymtab. */
7564
7565static void
7566process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7567 dwarf2_per_objfile *per_objfile,
7568 bool want_partial_unit,
7569 enum language pretend_language)
7570{
7571 /* If this compilation unit was already read in, free the
7572 cached copy in order to read it in again. This is
7573 necessary because we skipped some symbols when we first
7574 read in the compilation unit (see load_partial_dies).
7575 This problem could be avoided, but the benefit is unclear. */
7576 if (this_cu->cu != NULL)
7577 free_one_cached_comp_unit (this_cu);
7578
7579 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7580
7581 switch (reader.comp_unit_die->tag)
7582 {
7583 case DW_TAG_compile_unit:
7584 this_cu->unit_type = DW_UT_compile;
7585 break;
7586 case DW_TAG_partial_unit:
7587 this_cu->unit_type = DW_UT_partial;
7588 break;
7589 default:
7590 abort ();
7591 }
7592
7593 if (reader.dummy_p)
7594 {
7595 /* Nothing. */
7596 }
7597 else if (this_cu->is_debug_types)
7598 build_type_psymtabs_reader (&reader, reader.info_ptr,
7599 reader.comp_unit_die);
7600 else if (want_partial_unit
7601 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7602 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7603 reader.comp_unit_die,
7604 pretend_language);
7605
7606 this_cu->lang = this_cu->cu->language;
7607
7608 /* Age out any secondary CUs. */
7609 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7610}
7611
7612/* Reader function for build_type_psymtabs. */
7613
7614static void
7615build_type_psymtabs_reader (const struct die_reader_specs *reader,
7616 const gdb_byte *info_ptr,
7617 struct die_info *type_unit_die)
7618{
7619 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7620 struct objfile *objfile = dwarf2_per_objfile->objfile;
7621 struct dwarf2_cu *cu = reader->cu;
7622 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7623 struct signatured_type *sig_type;
7624 struct type_unit_group *tu_group;
7625 struct attribute *attr;
7626 struct partial_die_info *first_die;
7627 CORE_ADDR lowpc, highpc;
7628 dwarf2_psymtab *pst;
7629
7630 gdb_assert (per_cu->is_debug_types);
7631 sig_type = (struct signatured_type *) per_cu;
7632
7633 if (! type_unit_die->has_children)
7634 return;
7635
7636 attr = type_unit_die->attr (DW_AT_stmt_list);
7637 tu_group = get_type_unit_group (cu, attr);
7638
7639 if (tu_group->tus == nullptr)
7640 tu_group->tus = new std::vector<signatured_type *>;
7641 tu_group->tus->push_back (sig_type);
7642
7643 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7644 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7645 pst->anonymous = true;
7646
7647 first_die = load_partial_dies (reader, info_ptr, 1);
7648
7649 lowpc = (CORE_ADDR) -1;
7650 highpc = (CORE_ADDR) 0;
7651 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7652
7653 end_psymtab_common (objfile, pst);
7654}
7655
7656/* Struct used to sort TUs by their abbreviation table offset. */
7657
7658struct tu_abbrev_offset
7659{
7660 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7661 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7662 {}
7663
7664 signatured_type *sig_type;
7665 sect_offset abbrev_offset;
7666};
7667
7668/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7669
7670static bool
7671sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7672 const struct tu_abbrev_offset &b)
7673{
7674 return a.abbrev_offset < b.abbrev_offset;
7675}
7676
7677/* Efficiently read all the type units.
7678 This does the bulk of the work for build_type_psymtabs.
7679
7680 The efficiency is because we sort TUs by the abbrev table they use and
7681 only read each abbrev table once. In one program there are 200K TUs
7682 sharing 8K abbrev tables.
7683
7684 The main purpose of this function is to support building the
7685 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7686 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7687 can collapse the search space by grouping them by stmt_list.
7688 The savings can be significant, in the same program from above the 200K TUs
7689 share 8K stmt_list tables.
7690
7691 FUNC is expected to call get_type_unit_group, which will create the
7692 struct type_unit_group if necessary and add it to
7693 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7694
7695static void
7696build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7697{
7698 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7699 abbrev_table_up abbrev_table;
7700 sect_offset abbrev_offset;
7701
7702 /* It's up to the caller to not call us multiple times. */
7703 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7704
7705 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7706 return;
7707
7708 /* TUs typically share abbrev tables, and there can be way more TUs than
7709 abbrev tables. Sort by abbrev table to reduce the number of times we
7710 read each abbrev table in.
7711 Alternatives are to punt or to maintain a cache of abbrev tables.
7712 This is simpler and efficient enough for now.
7713
7714 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7715 symtab to use). Typically TUs with the same abbrev offset have the same
7716 stmt_list value too so in practice this should work well.
7717
7718 The basic algorithm here is:
7719
7720 sort TUs by abbrev table
7721 for each TU with same abbrev table:
7722 read abbrev table if first user
7723 read TU top level DIE
7724 [IWBN if DWO skeletons had DW_AT_stmt_list]
7725 call FUNC */
7726
7727 if (dwarf_read_debug)
7728 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7729
7730 /* Sort in a separate table to maintain the order of all_type_units
7731 for .gdb_index: TU indices directly index all_type_units. */
7732 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7733 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7734
7735 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7736 sorted_by_abbrev.emplace_back
7737 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7738 sig_type->per_cu.section,
7739 sig_type->per_cu.sect_off));
7740
7741 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7742 sort_tu_by_abbrev_offset);
7743
7744 abbrev_offset = (sect_offset) ~(unsigned) 0;
7745
7746 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7747 {
7748 /* Switch to the next abbrev table if necessary. */
7749 if (abbrev_table == NULL
7750 || tu.abbrev_offset != abbrev_offset)
7751 {
7752 abbrev_offset = tu.abbrev_offset;
7753 abbrev_table =
7754 abbrev_table::read (dwarf2_per_objfile->objfile,
7755 &dwarf2_per_objfile->per_bfd->abbrev,
7756 abbrev_offset);
7757 ++tu_stats->nr_uniq_abbrev_tables;
7758 }
7759
7760 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7761 abbrev_table.get (), 0, false);
7762 if (!reader.dummy_p)
7763 build_type_psymtabs_reader (&reader, reader.info_ptr,
7764 reader.comp_unit_die);
7765 }
7766}
7767
7768/* Print collected type unit statistics. */
7769
7770static void
7771print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7772{
7773 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7774
7775 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7776 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7777 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7778 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7779 tu_stats->nr_uniq_abbrev_tables);
7780 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7781 tu_stats->nr_symtabs);
7782 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7783 tu_stats->nr_symtab_sharers);
7784 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7785 tu_stats->nr_stmt_less_type_units);
7786 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7787 tu_stats->nr_all_type_units_reallocs);
7788}
7789
7790/* Traversal function for build_type_psymtabs. */
7791
7792static int
7793build_type_psymtab_dependencies (void **slot, void *info)
7794{
7795 struct dwarf2_per_objfile *dwarf2_per_objfile
7796 = (struct dwarf2_per_objfile *) info;
7797 struct objfile *objfile = dwarf2_per_objfile->objfile;
7798 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7799 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7800 dwarf2_psymtab *pst = per_cu->v.psymtab;
7801 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7802 int i;
7803
7804 gdb_assert (len > 0);
7805 gdb_assert (per_cu->type_unit_group_p ());
7806
7807 pst->number_of_dependencies = len;
7808 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7809 for (i = 0; i < len; ++i)
7810 {
7811 struct signatured_type *iter = tu_group->tus->at (i);
7812 gdb_assert (iter->per_cu.is_debug_types);
7813 pst->dependencies[i] = iter->per_cu.v.psymtab;
7814 iter->type_unit_group = tu_group;
7815 }
7816
7817 delete tu_group->tus;
7818 tu_group->tus = nullptr;
7819
7820 return 1;
7821}
7822
7823/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7824 Build partial symbol tables for the .debug_types comp-units. */
7825
7826static void
7827build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7828{
7829 if (! create_all_type_units (dwarf2_per_objfile))
7830 return;
7831
7832 build_type_psymtabs_1 (dwarf2_per_objfile);
7833}
7834
7835/* Traversal function for process_skeletonless_type_unit.
7836 Read a TU in a DWO file and build partial symbols for it. */
7837
7838static int
7839process_skeletonless_type_unit (void **slot, void *info)
7840{
7841 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7842 struct dwarf2_per_objfile *dwarf2_per_objfile
7843 = (struct dwarf2_per_objfile *) info;
7844 struct signatured_type find_entry, *entry;
7845
7846 /* If this TU doesn't exist in the global table, add it and read it in. */
7847
7848 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7849 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7850
7851 find_entry.signature = dwo_unit->signature;
7852 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7853 &find_entry, INSERT);
7854 /* If we've already seen this type there's nothing to do. What's happening
7855 is we're doing our own version of comdat-folding here. */
7856 if (*slot != NULL)
7857 return 1;
7858
7859 /* This does the job that create_all_type_units would have done for
7860 this TU. */
7861 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7862 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7863 *slot = entry;
7864
7865 /* This does the job that build_type_psymtabs_1 would have done. */
7866 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7867 if (!reader.dummy_p)
7868 build_type_psymtabs_reader (&reader, reader.info_ptr,
7869 reader.comp_unit_die);
7870
7871 return 1;
7872}
7873
7874/* Traversal function for process_skeletonless_type_units. */
7875
7876static int
7877process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7878{
7879 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7880
7881 if (dwo_file->tus != NULL)
7882 htab_traverse_noresize (dwo_file->tus.get (),
7883 process_skeletonless_type_unit, info);
7884
7885 return 1;
7886}
7887
7888/* Scan all TUs of DWO files, verifying we've processed them.
7889 This is needed in case a TU was emitted without its skeleton.
7890 Note: This can't be done until we know what all the DWO files are. */
7891
7892static void
7893process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7894{
7895 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7896 if (get_dwp_file (dwarf2_per_objfile) == NULL
7897 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7898 {
7899 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7900 process_dwo_file_for_skeletonless_type_units,
7901 dwarf2_per_objfile);
7902 }
7903}
7904
7905/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7906
7907static void
7908set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7909{
7910 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7911 {
7912 dwarf2_psymtab *pst = per_cu->v.psymtab;
7913
7914 if (pst == NULL)
7915 continue;
7916
7917 for (int j = 0; j < pst->number_of_dependencies; ++j)
7918 {
7919 /* Set the 'user' field only if it is not already set. */
7920 if (pst->dependencies[j]->user == NULL)
7921 pst->dependencies[j]->user = pst;
7922 }
7923 }
7924}
7925
7926/* Build the partial symbol table by doing a quick pass through the
7927 .debug_info and .debug_abbrev sections. */
7928
7929static void
7930dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7931{
7932 struct objfile *objfile = dwarf2_per_objfile->objfile;
7933
7934 if (dwarf_read_debug)
7935 {
7936 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7937 objfile_name (objfile));
7938 }
7939
7940 scoped_restore restore_reading_psyms
7941 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7942 true);
7943
7944 dwarf2_per_objfile->per_bfd->info.read (objfile);
7945
7946 /* Any cached compilation units will be linked by the per-objfile
7947 read_in_chain. Make sure to free them when we're done. */
7948 free_cached_comp_units freer (dwarf2_per_objfile);
7949
7950 build_type_psymtabs (dwarf2_per_objfile);
7951
7952 create_all_comp_units (dwarf2_per_objfile);
7953
7954 /* Create a temporary address map on a temporary obstack. We later
7955 copy this to the final obstack. */
7956 auto_obstack temp_obstack;
7957
7958 scoped_restore save_psymtabs_addrmap
7959 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7960 addrmap_create_mutable (&temp_obstack));
7961
7962 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7963 {
7964 if (per_cu->v.psymtab != NULL)
7965 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7966 continue;
7967 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7968 language_minimal);
7969 }
7970
7971 /* This has to wait until we read the CUs, we need the list of DWOs. */
7972 process_skeletonless_type_units (dwarf2_per_objfile);
7973
7974 /* Now that all TUs have been processed we can fill in the dependencies. */
7975 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7976 {
7977 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7978 build_type_psymtab_dependencies, dwarf2_per_objfile);
7979 }
7980
7981 if (dwarf_read_debug)
7982 print_tu_stats (dwarf2_per_objfile);
7983
7984 set_partial_user (dwarf2_per_objfile);
7985
7986 objfile->partial_symtabs->psymtabs_addrmap
7987 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7988 objfile->partial_symtabs->obstack ());
7989 /* At this point we want to keep the address map. */
7990 save_psymtabs_addrmap.release ();
7991
7992 if (dwarf_read_debug)
7993 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7994 objfile_name (objfile));
7995}
7996
7997/* Load the partial DIEs for a secondary CU into memory.
7998 This is also used when rereading a primary CU with load_all_dies. */
7999
8000static void
8001load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8002 dwarf2_per_objfile *per_objfile)
8003{
8004 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8005
8006 if (!reader.dummy_p)
8007 {
8008 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8009 language_minimal);
8010
8011 /* Check if comp unit has_children.
8012 If so, read the rest of the partial symbols from this comp unit.
8013 If not, there's no more debug_info for this comp unit. */
8014 if (reader.comp_unit_die->has_children)
8015 load_partial_dies (&reader, reader.info_ptr, 0);
8016
8017 reader.keep ();
8018 }
8019}
8020
8021static void
8022read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8023 struct dwarf2_section_info *section,
8024 struct dwarf2_section_info *abbrev_section,
8025 unsigned int is_dwz)
8026{
8027 const gdb_byte *info_ptr;
8028 struct objfile *objfile = dwarf2_per_objfile->objfile;
8029
8030 if (dwarf_read_debug)
8031 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8032 section->get_name (),
8033 section->get_file_name ());
8034
8035 section->read (objfile);
8036
8037 info_ptr = section->buffer;
8038
8039 while (info_ptr < section->buffer + section->size)
8040 {
8041 struct dwarf2_per_cu_data *this_cu;
8042
8043 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8044
8045 comp_unit_head cu_header;
8046 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8047 abbrev_section, info_ptr,
8048 rcuh_kind::COMPILE);
8049
8050 /* Save the compilation unit for later lookup. */
8051 if (cu_header.unit_type != DW_UT_type)
8052 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8053 else
8054 {
8055 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8056 sig_type->signature = cu_header.signature;
8057 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8058 this_cu = &sig_type->per_cu;
8059 }
8060 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8061 this_cu->sect_off = sect_off;
8062 this_cu->length = cu_header.length + cu_header.initial_length_size;
8063 this_cu->is_dwz = is_dwz;
8064 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8065 this_cu->section = section;
8066
8067 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8068
8069 info_ptr = info_ptr + this_cu->length;
8070 }
8071}
8072
8073/* Create a list of all compilation units in OBJFILE.
8074 This is only done for -readnow and building partial symtabs. */
8075
8076static void
8077create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8078{
8079 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8080 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8081 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8082
8083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8084 if (dwz != NULL)
8085 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8086 1);
8087}
8088
8089/* Process all loaded DIEs for compilation unit CU, starting at
8090 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8091 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8092 DW_AT_ranges). See the comments of add_partial_subprogram on how
8093 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8094
8095static void
8096scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8097 CORE_ADDR *highpc, int set_addrmap,
8098 struct dwarf2_cu *cu)
8099{
8100 struct partial_die_info *pdi;
8101
8102 /* Now, march along the PDI's, descending into ones which have
8103 interesting children but skipping the children of the other ones,
8104 until we reach the end of the compilation unit. */
8105
8106 pdi = first_die;
8107
8108 while (pdi != NULL)
8109 {
8110 pdi->fixup (cu);
8111
8112 /* Anonymous namespaces or modules have no name but have interesting
8113 children, so we need to look at them. Ditto for anonymous
8114 enums. */
8115
8116 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8117 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8118 || pdi->tag == DW_TAG_imported_unit
8119 || pdi->tag == DW_TAG_inlined_subroutine)
8120 {
8121 switch (pdi->tag)
8122 {
8123 case DW_TAG_subprogram:
8124 case DW_TAG_inlined_subroutine:
8125 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8126 break;
8127 case DW_TAG_constant:
8128 case DW_TAG_variable:
8129 case DW_TAG_typedef:
8130 case DW_TAG_union_type:
8131 if (!pdi->is_declaration
8132 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8133 {
8134 add_partial_symbol (pdi, cu);
8135 }
8136 break;
8137 case DW_TAG_class_type:
8138 case DW_TAG_interface_type:
8139 case DW_TAG_structure_type:
8140 if (!pdi->is_declaration)
8141 {
8142 add_partial_symbol (pdi, cu);
8143 }
8144 if ((cu->language == language_rust
8145 || cu->language == language_cplus) && pdi->has_children)
8146 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8147 set_addrmap, cu);
8148 break;
8149 case DW_TAG_enumeration_type:
8150 if (!pdi->is_declaration)
8151 add_partial_enumeration (pdi, cu);
8152 break;
8153 case DW_TAG_base_type:
8154 case DW_TAG_subrange_type:
8155 /* File scope base type definitions are added to the partial
8156 symbol table. */
8157 add_partial_symbol (pdi, cu);
8158 break;
8159 case DW_TAG_namespace:
8160 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8161 break;
8162 case DW_TAG_module:
8163 if (!pdi->is_declaration)
8164 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8165 break;
8166 case DW_TAG_imported_unit:
8167 {
8168 struct dwarf2_per_cu_data *per_cu;
8169
8170 /* For now we don't handle imported units in type units. */
8171 if (cu->per_cu->is_debug_types)
8172 {
8173 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8174 " supported in type units [in module %s]"),
8175 objfile_name (cu->per_objfile->objfile));
8176 }
8177
8178 per_cu = dwarf2_find_containing_comp_unit
8179 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8180
8181 /* Go read the partial unit, if needed. */
8182 if (per_cu->v.psymtab == NULL)
8183 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8184 cu->language);
8185
8186 cu->per_cu->imported_symtabs_push (per_cu);
8187 }
8188 break;
8189 case DW_TAG_imported_declaration:
8190 add_partial_symbol (pdi, cu);
8191 break;
8192 default:
8193 break;
8194 }
8195 }
8196
8197 /* If the die has a sibling, skip to the sibling. */
8198
8199 pdi = pdi->die_sibling;
8200 }
8201}
8202
8203/* Functions used to compute the fully scoped name of a partial DIE.
8204
8205 Normally, this is simple. For C++, the parent DIE's fully scoped
8206 name is concatenated with "::" and the partial DIE's name.
8207 Enumerators are an exception; they use the scope of their parent
8208 enumeration type, i.e. the name of the enumeration type is not
8209 prepended to the enumerator.
8210
8211 There are two complexities. One is DW_AT_specification; in this
8212 case "parent" means the parent of the target of the specification,
8213 instead of the direct parent of the DIE. The other is compilers
8214 which do not emit DW_TAG_namespace; in this case we try to guess
8215 the fully qualified name of structure types from their members'
8216 linkage names. This must be done using the DIE's children rather
8217 than the children of any DW_AT_specification target. We only need
8218 to do this for structures at the top level, i.e. if the target of
8219 any DW_AT_specification (if any; otherwise the DIE itself) does not
8220 have a parent. */
8221
8222/* Compute the scope prefix associated with PDI's parent, in
8223 compilation unit CU. The result will be allocated on CU's
8224 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8225 field. NULL is returned if no prefix is necessary. */
8226static const char *
8227partial_die_parent_scope (struct partial_die_info *pdi,
8228 struct dwarf2_cu *cu)
8229{
8230 const char *grandparent_scope;
8231 struct partial_die_info *parent, *real_pdi;
8232
8233 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8234 then this means the parent of the specification DIE. */
8235
8236 real_pdi = pdi;
8237 while (real_pdi->has_specification)
8238 {
8239 auto res = find_partial_die (real_pdi->spec_offset,
8240 real_pdi->spec_is_dwz, cu);
8241 real_pdi = res.pdi;
8242 cu = res.cu;
8243 }
8244
8245 parent = real_pdi->die_parent;
8246 if (parent == NULL)
8247 return NULL;
8248
8249 if (parent->scope_set)
8250 return parent->scope;
8251
8252 parent->fixup (cu);
8253
8254 grandparent_scope = partial_die_parent_scope (parent, cu);
8255
8256 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8257 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8258 Work around this problem here. */
8259 if (cu->language == language_cplus
8260 && parent->tag == DW_TAG_namespace
8261 && strcmp (parent->name, "::") == 0
8262 && grandparent_scope == NULL)
8263 {
8264 parent->scope = NULL;
8265 parent->scope_set = 1;
8266 return NULL;
8267 }
8268
8269 /* Nested subroutines in Fortran get a prefix. */
8270 if (pdi->tag == DW_TAG_enumerator)
8271 /* Enumerators should not get the name of the enumeration as a prefix. */
8272 parent->scope = grandparent_scope;
8273 else if (parent->tag == DW_TAG_namespace
8274 || parent->tag == DW_TAG_module
8275 || parent->tag == DW_TAG_structure_type
8276 || parent->tag == DW_TAG_class_type
8277 || parent->tag == DW_TAG_interface_type
8278 || parent->tag == DW_TAG_union_type
8279 || parent->tag == DW_TAG_enumeration_type
8280 || (cu->language == language_fortran
8281 && parent->tag == DW_TAG_subprogram
8282 && pdi->tag == DW_TAG_subprogram))
8283 {
8284 if (grandparent_scope == NULL)
8285 parent->scope = parent->name;
8286 else
8287 parent->scope = typename_concat (&cu->comp_unit_obstack,
8288 grandparent_scope,
8289 parent->name, 0, cu);
8290 }
8291 else
8292 {
8293 /* FIXME drow/2004-04-01: What should we be doing with
8294 function-local names? For partial symbols, we should probably be
8295 ignoring them. */
8296 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8297 dwarf_tag_name (parent->tag),
8298 sect_offset_str (pdi->sect_off));
8299 parent->scope = grandparent_scope;
8300 }
8301
8302 parent->scope_set = 1;
8303 return parent->scope;
8304}
8305
8306/* Return the fully scoped name associated with PDI, from compilation unit
8307 CU. The result will be allocated with malloc. */
8308
8309static gdb::unique_xmalloc_ptr<char>
8310partial_die_full_name (struct partial_die_info *pdi,
8311 struct dwarf2_cu *cu)
8312{
8313 const char *parent_scope;
8314
8315 /* If this is a template instantiation, we can not work out the
8316 template arguments from partial DIEs. So, unfortunately, we have
8317 to go through the full DIEs. At least any work we do building
8318 types here will be reused if full symbols are loaded later. */
8319 if (pdi->has_template_arguments)
8320 {
8321 pdi->fixup (cu);
8322
8323 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8324 {
8325 struct die_info *die;
8326 struct attribute attr;
8327 struct dwarf2_cu *ref_cu = cu;
8328
8329 /* DW_FORM_ref_addr is using section offset. */
8330 attr.name = (enum dwarf_attribute) 0;
8331 attr.form = DW_FORM_ref_addr;
8332 attr.u.unsnd = to_underlying (pdi->sect_off);
8333 die = follow_die_ref (NULL, &attr, &ref_cu);
8334
8335 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8336 }
8337 }
8338
8339 parent_scope = partial_die_parent_scope (pdi, cu);
8340 if (parent_scope == NULL)
8341 return NULL;
8342 else
8343 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8344 pdi->name, 0, cu));
8345}
8346
8347static void
8348add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8349{
8350 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8351 struct objfile *objfile = dwarf2_per_objfile->objfile;
8352 struct gdbarch *gdbarch = objfile->arch ();
8353 CORE_ADDR addr = 0;
8354 const char *actual_name = NULL;
8355 CORE_ADDR baseaddr;
8356
8357 baseaddr = objfile->text_section_offset ();
8358
8359 gdb::unique_xmalloc_ptr<char> built_actual_name
8360 = partial_die_full_name (pdi, cu);
8361 if (built_actual_name != NULL)
8362 actual_name = built_actual_name.get ();
8363
8364 if (actual_name == NULL)
8365 actual_name = pdi->name;
8366
8367 partial_symbol psymbol;
8368 memset (&psymbol, 0, sizeof (psymbol));
8369 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8370 psymbol.ginfo.section = -1;
8371
8372 /* The code below indicates that the psymbol should be installed by
8373 setting this. */
8374 gdb::optional<psymbol_placement> where;
8375
8376 switch (pdi->tag)
8377 {
8378 case DW_TAG_inlined_subroutine:
8379 case DW_TAG_subprogram:
8380 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8381 - baseaddr);
8382 if (pdi->is_external
8383 || cu->language == language_ada
8384 || (cu->language == language_fortran
8385 && pdi->die_parent != NULL
8386 && pdi->die_parent->tag == DW_TAG_subprogram))
8387 {
8388 /* Normally, only "external" DIEs are part of the global scope.
8389 But in Ada and Fortran, we want to be able to access nested
8390 procedures globally. So all Ada and Fortran subprograms are
8391 stored in the global scope. */
8392 where = psymbol_placement::GLOBAL;
8393 }
8394 else
8395 where = psymbol_placement::STATIC;
8396
8397 psymbol.domain = VAR_DOMAIN;
8398 psymbol.aclass = LOC_BLOCK;
8399 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8400 psymbol.ginfo.value.address = addr;
8401
8402 if (pdi->main_subprogram && actual_name != NULL)
8403 set_objfile_main_name (objfile, actual_name, cu->language);
8404 break;
8405 case DW_TAG_constant:
8406 psymbol.domain = VAR_DOMAIN;
8407 psymbol.aclass = LOC_STATIC;
8408 where = (pdi->is_external
8409 ? psymbol_placement::GLOBAL
8410 : psymbol_placement::STATIC);
8411 break;
8412 case DW_TAG_variable:
8413 if (pdi->d.locdesc)
8414 addr = decode_locdesc (pdi->d.locdesc, cu);
8415
8416 if (pdi->d.locdesc
8417 && addr == 0
8418 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8419 {
8420 /* A global or static variable may also have been stripped
8421 out by the linker if unused, in which case its address
8422 will be nullified; do not add such variables into partial
8423 symbol table then. */
8424 }
8425 else if (pdi->is_external)
8426 {
8427 /* Global Variable.
8428 Don't enter into the minimal symbol tables as there is
8429 a minimal symbol table entry from the ELF symbols already.
8430 Enter into partial symbol table if it has a location
8431 descriptor or a type.
8432 If the location descriptor is missing, new_symbol will create
8433 a LOC_UNRESOLVED symbol, the address of the variable will then
8434 be determined from the minimal symbol table whenever the variable
8435 is referenced.
8436 The address for the partial symbol table entry is not
8437 used by GDB, but it comes in handy for debugging partial symbol
8438 table building. */
8439
8440 if (pdi->d.locdesc || pdi->has_type)
8441 {
8442 psymbol.domain = VAR_DOMAIN;
8443 psymbol.aclass = LOC_STATIC;
8444 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8445 psymbol.ginfo.value.address = addr;
8446 where = psymbol_placement::GLOBAL;
8447 }
8448 }
8449 else
8450 {
8451 int has_loc = pdi->d.locdesc != NULL;
8452
8453 /* Static Variable. Skip symbols whose value we cannot know (those
8454 without location descriptors or constant values). */
8455 if (!has_loc && !pdi->has_const_value)
8456 return;
8457
8458 psymbol.domain = VAR_DOMAIN;
8459 psymbol.aclass = LOC_STATIC;
8460 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8461 if (has_loc)
8462 psymbol.ginfo.value.address = addr;
8463 where = psymbol_placement::STATIC;
8464 }
8465 break;
8466 case DW_TAG_typedef:
8467 case DW_TAG_base_type:
8468 case DW_TAG_subrange_type:
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_TYPEDEF;
8471 where = psymbol_placement::STATIC;
8472 break;
8473 case DW_TAG_imported_declaration:
8474 case DW_TAG_namespace:
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_TYPEDEF;
8477 where = psymbol_placement::GLOBAL;
8478 break;
8479 case DW_TAG_module:
8480 /* With Fortran 77 there might be a "BLOCK DATA" module
8481 available without any name. If so, we skip the module as it
8482 doesn't bring any value. */
8483 if (actual_name != nullptr)
8484 {
8485 psymbol.domain = MODULE_DOMAIN;
8486 psymbol.aclass = LOC_TYPEDEF;
8487 where = psymbol_placement::GLOBAL;
8488 }
8489 break;
8490 case DW_TAG_class_type:
8491 case DW_TAG_interface_type:
8492 case DW_TAG_structure_type:
8493 case DW_TAG_union_type:
8494 case DW_TAG_enumeration_type:
8495 /* Skip external references. The DWARF standard says in the section
8496 about "Structure, Union, and Class Type Entries": "An incomplete
8497 structure, union or class type is represented by a structure,
8498 union or class entry that does not have a byte size attribute
8499 and that has a DW_AT_declaration attribute." */
8500 if (!pdi->has_byte_size && pdi->is_declaration)
8501 return;
8502
8503 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8504 static vs. global. */
8505 psymbol.domain = STRUCT_DOMAIN;
8506 psymbol.aclass = LOC_TYPEDEF;
8507 where = (cu->language == language_cplus
8508 ? psymbol_placement::GLOBAL
8509 : psymbol_placement::STATIC);
8510 break;
8511 case DW_TAG_enumerator:
8512 psymbol.domain = VAR_DOMAIN;
8513 psymbol.aclass = LOC_CONST;
8514 where = (cu->language == language_cplus
8515 ? psymbol_placement::GLOBAL
8516 : psymbol_placement::STATIC);
8517 break;
8518 default:
8519 break;
8520 }
8521
8522 if (where.has_value ())
8523 {
8524 if (built_actual_name != nullptr)
8525 actual_name = objfile->intern (actual_name);
8526 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8527 psymbol.ginfo.set_linkage_name (actual_name);
8528 else
8529 {
8530 psymbol.ginfo.set_demangled_name (actual_name,
8531 &objfile->objfile_obstack);
8532 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8533 }
8534 add_psymbol_to_list (psymbol, *where, objfile);
8535 }
8536}
8537
8538/* Read a partial die corresponding to a namespace; also, add a symbol
8539 corresponding to that namespace to the symbol table. NAMESPACE is
8540 the name of the enclosing namespace. */
8541
8542static void
8543add_partial_namespace (struct partial_die_info *pdi,
8544 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8545 int set_addrmap, struct dwarf2_cu *cu)
8546{
8547 /* Add a symbol for the namespace. */
8548
8549 add_partial_symbol (pdi, cu);
8550
8551 /* Now scan partial symbols in that namespace. */
8552
8553 if (pdi->has_children)
8554 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8555}
8556
8557/* Read a partial die corresponding to a Fortran module. */
8558
8559static void
8560add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8561 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8562{
8563 /* Add a symbol for the namespace. */
8564
8565 add_partial_symbol (pdi, cu);
8566
8567 /* Now scan partial symbols in that module. */
8568
8569 if (pdi->has_children)
8570 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8571}
8572
8573/* Read a partial die corresponding to a subprogram or an inlined
8574 subprogram and create a partial symbol for that subprogram.
8575 When the CU language allows it, this routine also defines a partial
8576 symbol for each nested subprogram that this subprogram contains.
8577 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8578 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8579
8580 PDI may also be a lexical block, in which case we simply search
8581 recursively for subprograms defined inside that lexical block.
8582 Again, this is only performed when the CU language allows this
8583 type of definitions. */
8584
8585static void
8586add_partial_subprogram (struct partial_die_info *pdi,
8587 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8588 int set_addrmap, struct dwarf2_cu *cu)
8589{
8590 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8591 {
8592 if (pdi->has_pc_info)
8593 {
8594 if (pdi->lowpc < *lowpc)
8595 *lowpc = pdi->lowpc;
8596 if (pdi->highpc > *highpc)
8597 *highpc = pdi->highpc;
8598 if (set_addrmap)
8599 {
8600 struct objfile *objfile = cu->per_objfile->objfile;
8601 struct gdbarch *gdbarch = objfile->arch ();
8602 CORE_ADDR baseaddr;
8603 CORE_ADDR this_highpc;
8604 CORE_ADDR this_lowpc;
8605
8606 baseaddr = objfile->text_section_offset ();
8607 this_lowpc
8608 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8609 pdi->lowpc + baseaddr)
8610 - baseaddr);
8611 this_highpc
8612 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8613 pdi->highpc + baseaddr)
8614 - baseaddr);
8615 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8616 this_lowpc, this_highpc - 1,
8617 cu->per_cu->v.psymtab);
8618 }
8619 }
8620
8621 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8622 {
8623 if (!pdi->is_declaration)
8624 /* Ignore subprogram DIEs that do not have a name, they are
8625 illegal. Do not emit a complaint at this point, we will
8626 do so when we convert this psymtab into a symtab. */
8627 if (pdi->name)
8628 add_partial_symbol (pdi, cu);
8629 }
8630 }
8631
8632 if (! pdi->has_children)
8633 return;
8634
8635 if (cu->language == language_ada || cu->language == language_fortran)
8636 {
8637 pdi = pdi->die_child;
8638 while (pdi != NULL)
8639 {
8640 pdi->fixup (cu);
8641 if (pdi->tag == DW_TAG_subprogram
8642 || pdi->tag == DW_TAG_inlined_subroutine
8643 || pdi->tag == DW_TAG_lexical_block)
8644 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8645 pdi = pdi->die_sibling;
8646 }
8647 }
8648}
8649
8650/* Read a partial die corresponding to an enumeration type. */
8651
8652static void
8653add_partial_enumeration (struct partial_die_info *enum_pdi,
8654 struct dwarf2_cu *cu)
8655{
8656 struct partial_die_info *pdi;
8657
8658 if (enum_pdi->name != NULL)
8659 add_partial_symbol (enum_pdi, cu);
8660
8661 pdi = enum_pdi->die_child;
8662 while (pdi)
8663 {
8664 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8665 complaint (_("malformed enumerator DIE ignored"));
8666 else
8667 add_partial_symbol (pdi, cu);
8668 pdi = pdi->die_sibling;
8669 }
8670}
8671
8672/* Return the initial uleb128 in the die at INFO_PTR. */
8673
8674static unsigned int
8675peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8676{
8677 unsigned int bytes_read;
8678
8679 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8680}
8681
8682/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8683 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8684
8685 Return the corresponding abbrev, or NULL if the number is zero (indicating
8686 an empty DIE). In either case *BYTES_READ will be set to the length of
8687 the initial number. */
8688
8689static struct abbrev_info *
8690peek_die_abbrev (const die_reader_specs &reader,
8691 const gdb_byte *info_ptr, unsigned int *bytes_read)
8692{
8693 dwarf2_cu *cu = reader.cu;
8694 bfd *abfd = cu->per_objfile->objfile->obfd;
8695 unsigned int abbrev_number
8696 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8697
8698 if (abbrev_number == 0)
8699 return NULL;
8700
8701 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8702 if (!abbrev)
8703 {
8704 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8705 " at offset %s [in module %s]"),
8706 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8707 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8708 }
8709
8710 return abbrev;
8711}
8712
8713/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8714 Returns a pointer to the end of a series of DIEs, terminated by an empty
8715 DIE. Any children of the skipped DIEs will also be skipped. */
8716
8717static const gdb_byte *
8718skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8719{
8720 while (1)
8721 {
8722 unsigned int bytes_read;
8723 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8724
8725 if (abbrev == NULL)
8726 return info_ptr + bytes_read;
8727 else
8728 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8729 }
8730}
8731
8732/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8733 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8734 abbrev corresponding to that skipped uleb128 should be passed in
8735 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8736 children. */
8737
8738static const gdb_byte *
8739skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8740 struct abbrev_info *abbrev)
8741{
8742 unsigned int bytes_read;
8743 struct attribute attr;
8744 bfd *abfd = reader->abfd;
8745 struct dwarf2_cu *cu = reader->cu;
8746 const gdb_byte *buffer = reader->buffer;
8747 const gdb_byte *buffer_end = reader->buffer_end;
8748 unsigned int form, i;
8749
8750 for (i = 0; i < abbrev->num_attrs; i++)
8751 {
8752 /* The only abbrev we care about is DW_AT_sibling. */
8753 if (abbrev->attrs[i].name == DW_AT_sibling)
8754 {
8755 bool ignored;
8756 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8757 &ignored);
8758 if (attr.form == DW_FORM_ref_addr)
8759 complaint (_("ignoring absolute DW_AT_sibling"));
8760 else
8761 {
8762 sect_offset off = attr.get_ref_die_offset ();
8763 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8764
8765 if (sibling_ptr < info_ptr)
8766 complaint (_("DW_AT_sibling points backwards"));
8767 else if (sibling_ptr > reader->buffer_end)
8768 reader->die_section->overflow_complaint ();
8769 else
8770 return sibling_ptr;
8771 }
8772 }
8773
8774 /* If it isn't DW_AT_sibling, skip this attribute. */
8775 form = abbrev->attrs[i].form;
8776 skip_attribute:
8777 switch (form)
8778 {
8779 case DW_FORM_ref_addr:
8780 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8781 and later it is offset sized. */
8782 if (cu->header.version == 2)
8783 info_ptr += cu->header.addr_size;
8784 else
8785 info_ptr += cu->header.offset_size;
8786 break;
8787 case DW_FORM_GNU_ref_alt:
8788 info_ptr += cu->header.offset_size;
8789 break;
8790 case DW_FORM_addr:
8791 info_ptr += cu->header.addr_size;
8792 break;
8793 case DW_FORM_data1:
8794 case DW_FORM_ref1:
8795 case DW_FORM_flag:
8796 case DW_FORM_strx1:
8797 info_ptr += 1;
8798 break;
8799 case DW_FORM_flag_present:
8800 case DW_FORM_implicit_const:
8801 break;
8802 case DW_FORM_data2:
8803 case DW_FORM_ref2:
8804 case DW_FORM_strx2:
8805 info_ptr += 2;
8806 break;
8807 case DW_FORM_strx3:
8808 info_ptr += 3;
8809 break;
8810 case DW_FORM_data4:
8811 case DW_FORM_ref4:
8812 case DW_FORM_strx4:
8813 info_ptr += 4;
8814 break;
8815 case DW_FORM_data8:
8816 case DW_FORM_ref8:
8817 case DW_FORM_ref_sig8:
8818 info_ptr += 8;
8819 break;
8820 case DW_FORM_data16:
8821 info_ptr += 16;
8822 break;
8823 case DW_FORM_string:
8824 read_direct_string (abfd, info_ptr, &bytes_read);
8825 info_ptr += bytes_read;
8826 break;
8827 case DW_FORM_sec_offset:
8828 case DW_FORM_strp:
8829 case DW_FORM_GNU_strp_alt:
8830 info_ptr += cu->header.offset_size;
8831 break;
8832 case DW_FORM_exprloc:
8833 case DW_FORM_block:
8834 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8835 info_ptr += bytes_read;
8836 break;
8837 case DW_FORM_block1:
8838 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8839 break;
8840 case DW_FORM_block2:
8841 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8842 break;
8843 case DW_FORM_block4:
8844 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8845 break;
8846 case DW_FORM_addrx:
8847 case DW_FORM_strx:
8848 case DW_FORM_sdata:
8849 case DW_FORM_udata:
8850 case DW_FORM_ref_udata:
8851 case DW_FORM_GNU_addr_index:
8852 case DW_FORM_GNU_str_index:
8853 case DW_FORM_rnglistx:
8854 case DW_FORM_loclistx:
8855 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8856 break;
8857 case DW_FORM_indirect:
8858 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 /* We need to continue parsing from here, so just go back to
8861 the top. */
8862 goto skip_attribute;
8863
8864 default:
8865 error (_("Dwarf Error: Cannot handle %s "
8866 "in DWARF reader [in module %s]"),
8867 dwarf_form_name (form),
8868 bfd_get_filename (abfd));
8869 }
8870 }
8871
8872 if (abbrev->has_children)
8873 return skip_children (reader, info_ptr);
8874 else
8875 return info_ptr;
8876}
8877
8878/* Locate ORIG_PDI's sibling.
8879 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8880
8881static const gdb_byte *
8882locate_pdi_sibling (const struct die_reader_specs *reader,
8883 struct partial_die_info *orig_pdi,
8884 const gdb_byte *info_ptr)
8885{
8886 /* Do we know the sibling already? */
8887
8888 if (orig_pdi->sibling)
8889 return orig_pdi->sibling;
8890
8891 /* Are there any children to deal with? */
8892
8893 if (!orig_pdi->has_children)
8894 return info_ptr;
8895
8896 /* Skip the children the long way. */
8897
8898 return skip_children (reader, info_ptr);
8899}
8900
8901/* Expand this partial symbol table into a full symbol table. SELF is
8902 not NULL. */
8903
8904void
8905dwarf2_psymtab::read_symtab (struct objfile *objfile)
8906{
8907 struct dwarf2_per_objfile *dwarf2_per_objfile
8908 = get_dwarf2_per_objfile (objfile);
8909
8910 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8911
8912 /* If this psymtab is constructed from a debug-only objfile, the
8913 has_section_at_zero flag will not necessarily be correct. We
8914 can get the correct value for this flag by looking at the data
8915 associated with the (presumably stripped) associated objfile. */
8916 if (objfile->separate_debug_objfile_backlink)
8917 {
8918 struct dwarf2_per_objfile *dpo_backlink
8919 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8920
8921 dwarf2_per_objfile->per_bfd->has_section_at_zero
8922 = dpo_backlink->per_bfd->has_section_at_zero;
8923 }
8924
8925 expand_psymtab (objfile);
8926
8927 process_cu_includes (dwarf2_per_objfile);
8928}
8929\f
8930/* Reading in full CUs. */
8931
8932/* Add PER_CU to the queue. */
8933
8934static void
8935queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8936 enum language pretend_language)
8937{
8938 per_cu->queued = 1;
8939 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8940}
8941
8942/* If PER_CU is not yet queued, add it to the queue.
8943 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8944 dependency.
8945 The result is non-zero if PER_CU was queued, otherwise the result is zero
8946 meaning either PER_CU is already queued or it is already loaded.
8947
8948 N.B. There is an invariant here that if a CU is queued then it is loaded.
8949 The caller is required to load PER_CU if we return non-zero. */
8950
8951static int
8952maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8953 struct dwarf2_per_cu_data *per_cu,
8954 enum language pretend_language)
8955{
8956 /* We may arrive here during partial symbol reading, if we need full
8957 DIEs to process an unusual case (e.g. template arguments). Do
8958 not queue PER_CU, just tell our caller to load its DIEs. */
8959 if (per_cu->per_bfd->reading_partial_symbols)
8960 {
8961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8962 return 1;
8963 return 0;
8964 }
8965
8966 /* Mark the dependence relation so that we don't flush PER_CU
8967 too early. */
8968 if (dependent_cu != NULL)
8969 dwarf2_add_dependence (dependent_cu, per_cu);
8970
8971 /* If it's already on the queue, we have nothing to do. */
8972 if (per_cu->queued)
8973 return 0;
8974
8975 /* If the compilation unit is already loaded, just mark it as
8976 used. */
8977 if (per_cu->cu != NULL)
8978 {
8979 per_cu->cu->last_used = 0;
8980 return 0;
8981 }
8982
8983 /* Add it to the queue. */
8984 queue_comp_unit (per_cu, pretend_language);
8985
8986 return 1;
8987}
8988
8989/* Process the queue. */
8990
8991static void
8992process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8993{
8994 if (dwarf_read_debug)
8995 {
8996 fprintf_unfiltered (gdb_stdlog,
8997 "Expanding one or more symtabs of objfile %s ...\n",
8998 objfile_name (dwarf2_per_objfile->objfile));
8999 }
9000
9001 /* The queue starts out with one item, but following a DIE reference
9002 may load a new CU, adding it to the end of the queue. */
9003 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9004 {
9005 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9006
9007 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9008 /* Skip dummy CUs. */
9009 && item.per_cu->cu != NULL)
9010 {
9011 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9012 unsigned int debug_print_threshold;
9013 char buf[100];
9014
9015 if (per_cu->is_debug_types)
9016 {
9017 struct signatured_type *sig_type =
9018 (struct signatured_type *) per_cu;
9019
9020 sprintf (buf, "TU %s at offset %s",
9021 hex_string (sig_type->signature),
9022 sect_offset_str (per_cu->sect_off));
9023 /* There can be 100s of TUs.
9024 Only print them in verbose mode. */
9025 debug_print_threshold = 2;
9026 }
9027 else
9028 {
9029 sprintf (buf, "CU at offset %s",
9030 sect_offset_str (per_cu->sect_off));
9031 debug_print_threshold = 1;
9032 }
9033
9034 if (dwarf_read_debug >= debug_print_threshold)
9035 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9036
9037 if (per_cu->is_debug_types)
9038 process_full_type_unit (per_cu, item.pretend_language);
9039 else
9040 process_full_comp_unit (per_cu, item.pretend_language);
9041
9042 if (dwarf_read_debug >= debug_print_threshold)
9043 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9044 }
9045
9046 item.per_cu->queued = 0;
9047 dwarf2_per_objfile->per_bfd->queue.pop ();
9048 }
9049
9050 if (dwarf_read_debug)
9051 {
9052 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9053 objfile_name (dwarf2_per_objfile->objfile));
9054 }
9055}
9056
9057/* Read in full symbols for PST, and anything it depends on. */
9058
9059void
9060dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9061{
9062 gdb_assert (!readin_p (objfile));
9063
9064 expand_dependencies (objfile);
9065
9066 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9067 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9068 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9069}
9070
9071/* See psympriv.h. */
9072
9073bool
9074dwarf2_psymtab::readin_p (struct objfile *objfile) const
9075{
9076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9077 return per_objfile->symtab_set_p (per_cu_data);
9078}
9079
9080/* See psympriv.h. */
9081
9082compunit_symtab *
9083dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9084{
9085 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9086 return per_objfile->get_symtab (per_cu_data);
9087}
9088
9089/* Trivial hash function for die_info: the hash value of a DIE
9090 is its offset in .debug_info for this objfile. */
9091
9092static hashval_t
9093die_hash (const void *item)
9094{
9095 const struct die_info *die = (const struct die_info *) item;
9096
9097 return to_underlying (die->sect_off);
9098}
9099
9100/* Trivial comparison function for die_info structures: two DIEs
9101 are equal if they have the same offset. */
9102
9103static int
9104die_eq (const void *item_lhs, const void *item_rhs)
9105{
9106 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9107 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9108
9109 return die_lhs->sect_off == die_rhs->sect_off;
9110}
9111
9112/* Load the DIEs associated with PER_CU into memory. */
9113
9114static void
9115load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9116 dwarf2_per_objfile *per_objfile,
9117 bool skip_partial,
9118 enum language pretend_language)
9119{
9120 gdb_assert (! this_cu->is_debug_types);
9121
9122 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9123 if (reader.dummy_p)
9124 return;
9125
9126 struct dwarf2_cu *cu = reader.cu;
9127 const gdb_byte *info_ptr = reader.info_ptr;
9128
9129 gdb_assert (cu->die_hash == NULL);
9130 cu->die_hash =
9131 htab_create_alloc_ex (cu->header.length / 12,
9132 die_hash,
9133 die_eq,
9134 NULL,
9135 &cu->comp_unit_obstack,
9136 hashtab_obstack_allocate,
9137 dummy_obstack_deallocate);
9138
9139 if (reader.comp_unit_die->has_children)
9140 reader.comp_unit_die->child
9141 = read_die_and_siblings (&reader, reader.info_ptr,
9142 &info_ptr, reader.comp_unit_die);
9143 cu->dies = reader.comp_unit_die;
9144 /* comp_unit_die is not stored in die_hash, no need. */
9145
9146 /* We try not to read any attributes in this function, because not
9147 all CUs needed for references have been loaded yet, and symbol
9148 table processing isn't initialized. But we have to set the CU language,
9149 or we won't be able to build types correctly.
9150 Similarly, if we do not read the producer, we can not apply
9151 producer-specific interpretation. */
9152 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9153
9154 reader.keep ();
9155}
9156
9157/* Add a DIE to the delayed physname list. */
9158
9159static void
9160add_to_method_list (struct type *type, int fnfield_index, int index,
9161 const char *name, struct die_info *die,
9162 struct dwarf2_cu *cu)
9163{
9164 struct delayed_method_info mi;
9165 mi.type = type;
9166 mi.fnfield_index = fnfield_index;
9167 mi.index = index;
9168 mi.name = name;
9169 mi.die = die;
9170 cu->method_list.push_back (mi);
9171}
9172
9173/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9174 "const" / "volatile". If so, decrements LEN by the length of the
9175 modifier and return true. Otherwise return false. */
9176
9177template<size_t N>
9178static bool
9179check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9180{
9181 size_t mod_len = sizeof (mod) - 1;
9182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9183 {
9184 len -= mod_len;
9185 return true;
9186 }
9187 return false;
9188}
9189
9190/* Compute the physnames of any methods on the CU's method list.
9191
9192 The computation of method physnames is delayed in order to avoid the
9193 (bad) condition that one of the method's formal parameters is of an as yet
9194 incomplete type. */
9195
9196static void
9197compute_delayed_physnames (struct dwarf2_cu *cu)
9198{
9199 /* Only C++ delays computing physnames. */
9200 if (cu->method_list.empty ())
9201 return;
9202 gdb_assert (cu->language == language_cplus);
9203
9204 for (const delayed_method_info &mi : cu->method_list)
9205 {
9206 const char *physname;
9207 struct fn_fieldlist *fn_flp
9208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9209 physname = dwarf2_physname (mi.name, mi.die, cu);
9210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9211 = physname ? physname : "";
9212
9213 /* Since there's no tag to indicate whether a method is a
9214 const/volatile overload, extract that information out of the
9215 demangled name. */
9216 if (physname != NULL)
9217 {
9218 size_t len = strlen (physname);
9219
9220 while (1)
9221 {
9222 if (physname[len] == ')') /* shortcut */
9223 break;
9224 else if (check_modifier (physname, len, " const"))
9225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9226 else if (check_modifier (physname, len, " volatile"))
9227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9228 else
9229 break;
9230 }
9231 }
9232 }
9233
9234 /* The list is no longer needed. */
9235 cu->method_list.clear ();
9236}
9237
9238/* Go objects should be embedded in a DW_TAG_module DIE,
9239 and it's not clear if/how imported objects will appear.
9240 To keep Go support simple until that's worked out,
9241 go back through what we've read and create something usable.
9242 We could do this while processing each DIE, and feels kinda cleaner,
9243 but that way is more invasive.
9244 This is to, for example, allow the user to type "p var" or "b main"
9245 without having to specify the package name, and allow lookups
9246 of module.object to work in contexts that use the expression
9247 parser. */
9248
9249static void
9250fixup_go_packaging (struct dwarf2_cu *cu)
9251{
9252 gdb::unique_xmalloc_ptr<char> package_name;
9253 struct pending *list;
9254 int i;
9255
9256 for (list = *cu->get_builder ()->get_global_symbols ();
9257 list != NULL;
9258 list = list->next)
9259 {
9260 for (i = 0; i < list->nsyms; ++i)
9261 {
9262 struct symbol *sym = list->symbol[i];
9263
9264 if (sym->language () == language_go
9265 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9266 {
9267 gdb::unique_xmalloc_ptr<char> this_package_name
9268 (go_symbol_package_name (sym));
9269
9270 if (this_package_name == NULL)
9271 continue;
9272 if (package_name == NULL)
9273 package_name = std::move (this_package_name);
9274 else
9275 {
9276 struct objfile *objfile = cu->per_objfile->objfile;
9277 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9278 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9279 (symbol_symtab (sym) != NULL
9280 ? symtab_to_filename_for_display
9281 (symbol_symtab (sym))
9282 : objfile_name (objfile)),
9283 this_package_name.get (), package_name.get ());
9284 }
9285 }
9286 }
9287 }
9288
9289 if (package_name != NULL)
9290 {
9291 struct objfile *objfile = cu->per_objfile->objfile;
9292 const char *saved_package_name = objfile->intern (package_name.get ());
9293 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9294 saved_package_name);
9295 struct symbol *sym;
9296
9297 sym = new (&objfile->objfile_obstack) symbol;
9298 sym->set_language (language_go, &objfile->objfile_obstack);
9299 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9300 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9301 e.g., "main" finds the "main" module and not C's main(). */
9302 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9303 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9304 SYMBOL_TYPE (sym) = type;
9305
9306 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9307 }
9308}
9309
9310/* Allocate a fully-qualified name consisting of the two parts on the
9311 obstack. */
9312
9313static const char *
9314rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9315{
9316 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9317}
9318
9319/* A helper that allocates a variant part to attach to a Rust enum
9320 type. OBSTACK is where the results should be allocated. TYPE is
9321 the type we're processing. DISCRIMINANT_INDEX is the index of the
9322 discriminant. It must be the index of one of the fields of TYPE.
9323 DEFAULT_INDEX is the index of the default field; or -1 if there is
9324 no default. RANGES is indexed by "effective" field number (the
9325 field index, but omitting the discriminant and default fields) and
9326 must hold the discriminant values used by the variants. Note that
9327 RANGES must have a lifetime at least as long as OBSTACK -- either
9328 already allocated on it, or static. */
9329
9330static void
9331alloc_rust_variant (struct obstack *obstack, struct type *type,
9332 int discriminant_index, int default_index,
9333 gdb::array_view<discriminant_range> ranges)
9334{
9335 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9336 must be handled by the caller. */
9337 gdb_assert (discriminant_index >= 0
9338 && discriminant_index < type->num_fields ());
9339 gdb_assert (default_index == -1
9340 || (default_index >= 0 && default_index < type->num_fields ()));
9341
9342 /* We have one variant for each non-discriminant field. */
9343 int n_variants = type->num_fields () - 1;
9344
9345 variant *variants = new (obstack) variant[n_variants];
9346 int var_idx = 0;
9347 int range_idx = 0;
9348 for (int i = 0; i < type->num_fields (); ++i)
9349 {
9350 if (i == discriminant_index)
9351 continue;
9352
9353 variants[var_idx].first_field = i;
9354 variants[var_idx].last_field = i + 1;
9355
9356 /* The default field does not need a range, but other fields do.
9357 We skipped the discriminant above. */
9358 if (i != default_index)
9359 {
9360 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9361 ++range_idx;
9362 }
9363
9364 ++var_idx;
9365 }
9366
9367 gdb_assert (range_idx == ranges.size ());
9368 gdb_assert (var_idx == n_variants);
9369
9370 variant_part *part = new (obstack) variant_part;
9371 part->discriminant_index = discriminant_index;
9372 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9373 discriminant_index));
9374 part->variants = gdb::array_view<variant> (variants, n_variants);
9375
9376 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9377 gdb::array_view<variant_part> *prop_value
9378 = new (storage) gdb::array_view<variant_part> (part, 1);
9379
9380 struct dynamic_prop prop;
9381 prop.kind = PROP_VARIANT_PARTS;
9382 prop.data.variant_parts = prop_value;
9383
9384 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9385}
9386
9387/* Some versions of rustc emitted enums in an unusual way.
9388
9389 Ordinary enums were emitted as unions. The first element of each
9390 structure in the union was named "RUST$ENUM$DISR". This element
9391 held the discriminant.
9392
9393 These versions of Rust also implemented the "non-zero"
9394 optimization. When the enum had two values, and one is empty and
9395 the other holds a pointer that cannot be zero, the pointer is used
9396 as the discriminant, with a zero value meaning the empty variant.
9397 Here, the union's first member is of the form
9398 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9399 where the fieldnos are the indices of the fields that should be
9400 traversed in order to find the field (which may be several fields deep)
9401 and the variantname is the name of the variant of the case when the
9402 field is zero.
9403
9404 This function recognizes whether TYPE is of one of these forms,
9405 and, if so, smashes it to be a variant type. */
9406
9407static void
9408quirk_rust_enum (struct type *type, struct objfile *objfile)
9409{
9410 gdb_assert (type->code () == TYPE_CODE_UNION);
9411
9412 /* We don't need to deal with empty enums. */
9413 if (type->num_fields () == 0)
9414 return;
9415
9416#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9417 if (type->num_fields () == 1
9418 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9419 {
9420 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9421
9422 /* Decode the field name to find the offset of the
9423 discriminant. */
9424 ULONGEST bit_offset = 0;
9425 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9426 while (name[0] >= '0' && name[0] <= '9')
9427 {
9428 char *tail;
9429 unsigned long index = strtoul (name, &tail, 10);
9430 name = tail;
9431 if (*name != '$'
9432 || index >= field_type->num_fields ()
9433 || (TYPE_FIELD_LOC_KIND (field_type, index)
9434 != FIELD_LOC_KIND_BITPOS))
9435 {
9436 complaint (_("Could not parse Rust enum encoding string \"%s\""
9437 "[in module %s]"),
9438 TYPE_FIELD_NAME (type, 0),
9439 objfile_name (objfile));
9440 return;
9441 }
9442 ++name;
9443
9444 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9445 field_type = TYPE_FIELD_TYPE (field_type, index);
9446 }
9447
9448 /* Smash this type to be a structure type. We have to do this
9449 because the type has already been recorded. */
9450 type->set_code (TYPE_CODE_STRUCT);
9451 type->set_num_fields (3);
9452 /* Save the field we care about. */
9453 struct field saved_field = type->field (0);
9454 type->set_fields
9455 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9456
9457 /* Put the discriminant at index 0. */
9458 TYPE_FIELD_TYPE (type, 0) = field_type;
9459 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9460 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9461 SET_FIELD_BITPOS (type->field (0), bit_offset);
9462
9463 /* The order of fields doesn't really matter, so put the real
9464 field at index 1 and the data-less field at index 2. */
9465 type->field (1) = saved_field;
9466 TYPE_FIELD_NAME (type, 1)
9467 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9468 TYPE_FIELD_TYPE (type, 1)->set_name
9469 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9470 TYPE_FIELD_NAME (type, 1)));
9471
9472 const char *dataless_name
9473 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9474 name);
9475 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9476 dataless_name);
9477 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9478 /* NAME points into the original discriminant name, which
9479 already has the correct lifetime. */
9480 TYPE_FIELD_NAME (type, 2) = name;
9481 SET_FIELD_BITPOS (type->field (2), 0);
9482
9483 /* Indicate that this is a variant type. */
9484 static discriminant_range ranges[1] = { { 0, 0 } };
9485 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9486 }
9487 /* A union with a single anonymous field is probably an old-style
9488 univariant enum. */
9489 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9490 {
9491 /* Smash this type to be a structure type. We have to do this
9492 because the type has already been recorded. */
9493 type->set_code (TYPE_CODE_STRUCT);
9494
9495 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9496 const char *variant_name
9497 = rust_last_path_segment (field_type->name ());
9498 TYPE_FIELD_NAME (type, 0) = variant_name;
9499 field_type->set_name
9500 (rust_fully_qualify (&objfile->objfile_obstack,
9501 type->name (), variant_name));
9502 }
9503 else
9504 {
9505 struct type *disr_type = nullptr;
9506 for (int i = 0; i < type->num_fields (); ++i)
9507 {
9508 disr_type = TYPE_FIELD_TYPE (type, i);
9509
9510 if (disr_type->code () != TYPE_CODE_STRUCT)
9511 {
9512 /* All fields of a true enum will be structs. */
9513 return;
9514 }
9515 else if (disr_type->num_fields () == 0)
9516 {
9517 /* Could be data-less variant, so keep going. */
9518 disr_type = nullptr;
9519 }
9520 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9521 "RUST$ENUM$DISR") != 0)
9522 {
9523 /* Not a Rust enum. */
9524 return;
9525 }
9526 else
9527 {
9528 /* Found one. */
9529 break;
9530 }
9531 }
9532
9533 /* If we got here without a discriminant, then it's probably
9534 just a union. */
9535 if (disr_type == nullptr)
9536 return;
9537
9538 /* Smash this type to be a structure type. We have to do this
9539 because the type has already been recorded. */
9540 type->set_code (TYPE_CODE_STRUCT);
9541
9542 /* Make space for the discriminant field. */
9543 struct field *disr_field = &disr_type->field (0);
9544 field *new_fields
9545 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9546 * sizeof (struct field)));
9547 memcpy (new_fields + 1, type->fields (),
9548 type->num_fields () * sizeof (struct field));
9549 type->set_fields (new_fields);
9550 type->set_num_fields (type->num_fields () + 1);
9551
9552 /* Install the discriminant at index 0 in the union. */
9553 type->field (0) = *disr_field;
9554 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9555 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9556
9557 /* We need a way to find the correct discriminant given a
9558 variant name. For convenience we build a map here. */
9559 struct type *enum_type = FIELD_TYPE (*disr_field);
9560 std::unordered_map<std::string, ULONGEST> discriminant_map;
9561 for (int i = 0; i < enum_type->num_fields (); ++i)
9562 {
9563 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9564 {
9565 const char *name
9566 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9567 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9568 }
9569 }
9570
9571 int n_fields = type->num_fields ();
9572 /* We don't need a range entry for the discriminant, but we do
9573 need one for every other field, as there is no default
9574 variant. */
9575 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9576 discriminant_range,
9577 n_fields - 1);
9578 /* Skip the discriminant here. */
9579 for (int i = 1; i < n_fields; ++i)
9580 {
9581 /* Find the final word in the name of this variant's type.
9582 That name can be used to look up the correct
9583 discriminant. */
9584 const char *variant_name
9585 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9586
9587 auto iter = discriminant_map.find (variant_name);
9588 if (iter != discriminant_map.end ())
9589 {
9590 ranges[i].low = iter->second;
9591 ranges[i].high = iter->second;
9592 }
9593
9594 /* Remove the discriminant field, if it exists. */
9595 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9596 if (sub_type->num_fields () > 0)
9597 {
9598 sub_type->set_num_fields (sub_type->num_fields () - 1);
9599 sub_type->set_fields (sub_type->fields () + 1);
9600 }
9601 TYPE_FIELD_NAME (type, i) = variant_name;
9602 sub_type->set_name
9603 (rust_fully_qualify (&objfile->objfile_obstack,
9604 type->name (), variant_name));
9605 }
9606
9607 /* Indicate that this is a variant type. */
9608 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9609 gdb::array_view<discriminant_range> (ranges,
9610 n_fields - 1));
9611 }
9612}
9613
9614/* Rewrite some Rust unions to be structures with variants parts. */
9615
9616static void
9617rust_union_quirks (struct dwarf2_cu *cu)
9618{
9619 gdb_assert (cu->language == language_rust);
9620 for (type *type_ : cu->rust_unions)
9621 quirk_rust_enum (type_, cu->per_objfile->objfile);
9622 /* We don't need this any more. */
9623 cu->rust_unions.clear ();
9624}
9625
9626/* A helper function for computing the list of all symbol tables
9627 included by PER_CU. */
9628
9629static void
9630recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9631 htab_t all_children, htab_t all_type_symtabs,
9632 dwarf2_per_cu_data *per_cu,
9633 dwarf2_per_objfile *per_objfile,
9634 struct compunit_symtab *immediate_parent)
9635{
9636 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9637 if (*slot != NULL)
9638 {
9639 /* This inclusion and its children have been processed. */
9640 return;
9641 }
9642
9643 *slot = per_cu;
9644
9645 /* Only add a CU if it has a symbol table. */
9646 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9647 if (cust != NULL)
9648 {
9649 /* If this is a type unit only add its symbol table if we haven't
9650 seen it yet (type unit per_cu's can share symtabs). */
9651 if (per_cu->is_debug_types)
9652 {
9653 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9654 if (*slot == NULL)
9655 {
9656 *slot = cust;
9657 result->push_back (cust);
9658 if (cust->user == NULL)
9659 cust->user = immediate_parent;
9660 }
9661 }
9662 else
9663 {
9664 result->push_back (cust);
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
9667 }
9668 }
9669
9670 if (!per_cu->imported_symtabs_empty ())
9671 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9672 {
9673 recursively_compute_inclusions (result, all_children,
9674 all_type_symtabs, ptr, per_objfile,
9675 cust);
9676 }
9677}
9678
9679/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9680 PER_CU. */
9681
9682static void
9683compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9684 dwarf2_per_objfile *per_objfile)
9685{
9686 gdb_assert (! per_cu->is_debug_types);
9687
9688 if (!per_cu->imported_symtabs_empty ())
9689 {
9690 int len;
9691 std::vector<compunit_symtab *> result_symtabs;
9692 htab_t all_children, all_type_symtabs;
9693 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9694
9695 /* If we don't have a symtab, we can just skip this case. */
9696 if (cust == NULL)
9697 return;
9698
9699 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9700 NULL, xcalloc, xfree);
9701 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9702 NULL, xcalloc, xfree);
9703
9704 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9705 {
9706 recursively_compute_inclusions (&result_symtabs, all_children,
9707 all_type_symtabs, ptr, per_objfile,
9708 cust);
9709 }
9710
9711 /* Now we have a transitive closure of all the included symtabs. */
9712 len = result_symtabs.size ();
9713 cust->includes
9714 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9715 struct compunit_symtab *, len + 1);
9716 memcpy (cust->includes, result_symtabs.data (),
9717 len * sizeof (compunit_symtab *));
9718 cust->includes[len] = NULL;
9719
9720 htab_delete (all_children);
9721 htab_delete (all_type_symtabs);
9722 }
9723}
9724
9725/* Compute the 'includes' field for the symtabs of all the CUs we just
9726 read. */
9727
9728static void
9729process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9730{
9731 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9732 {
9733 if (! iter->is_debug_types)
9734 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9735 }
9736
9737 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9738}
9739
9740/* Generate full symbol information for PER_CU, whose DIEs have
9741 already been loaded into memory. */
9742
9743static void
9744process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9745 enum language pretend_language)
9746{
9747 struct dwarf2_cu *cu = per_cu->cu;
9748 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9749 struct objfile *objfile = dwarf2_per_objfile->objfile;
9750 struct gdbarch *gdbarch = objfile->arch ();
9751 CORE_ADDR lowpc, highpc;
9752 struct compunit_symtab *cust;
9753 CORE_ADDR baseaddr;
9754 struct block *static_block;
9755 CORE_ADDR addr;
9756
9757 baseaddr = objfile->text_section_offset ();
9758
9759 /* Clear the list here in case something was left over. */
9760 cu->method_list.clear ();
9761
9762 cu->language = pretend_language;
9763 cu->language_defn = language_def (cu->language);
9764
9765 /* Do line number decoding in read_file_scope () */
9766 process_die (cu->dies, cu);
9767
9768 /* For now fudge the Go package. */
9769 if (cu->language == language_go)
9770 fixup_go_packaging (cu);
9771
9772 /* Now that we have processed all the DIEs in the CU, all the types
9773 should be complete, and it should now be safe to compute all of the
9774 physnames. */
9775 compute_delayed_physnames (cu);
9776
9777 if (cu->language == language_rust)
9778 rust_union_quirks (cu);
9779
9780 /* Some compilers don't define a DW_AT_high_pc attribute for the
9781 compilation unit. If the DW_AT_high_pc is missing, synthesize
9782 it, by scanning the DIE's below the compilation unit. */
9783 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9784
9785 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9786 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9787
9788 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9789 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9790 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9791 addrmap to help ensure it has an accurate map of pc values belonging to
9792 this comp unit. */
9793 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9794
9795 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9796 SECT_OFF_TEXT (objfile),
9797 0);
9798
9799 if (cust != NULL)
9800 {
9801 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9802
9803 /* Set symtab language to language from DW_AT_language. If the
9804 compilation is from a C file generated by language preprocessors, do
9805 not set the language if it was already deduced by start_subfile. */
9806 if (!(cu->language == language_c
9807 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9808 COMPUNIT_FILETABS (cust)->language = cu->language;
9809
9810 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9811 produce DW_AT_location with location lists but it can be possibly
9812 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9813 there were bugs in prologue debug info, fixed later in GCC-4.5
9814 by "unwind info for epilogues" patch (which is not directly related).
9815
9816 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9817 needed, it would be wrong due to missing DW_AT_producer there.
9818
9819 Still one can confuse GDB by using non-standard GCC compilation
9820 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9821 */
9822 if (cu->has_loclist && gcc_4_minor >= 5)
9823 cust->locations_valid = 1;
9824
9825 if (gcc_4_minor >= 5)
9826 cust->epilogue_unwind_valid = 1;
9827
9828 cust->call_site_htab = cu->call_site_htab;
9829 }
9830
9831 dwarf2_per_objfile->set_symtab (per_cu, cust);
9832
9833 /* Push it for inclusion processing later. */
9834 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9835
9836 /* Not needed any more. */
9837 cu->reset_builder ();
9838}
9839
9840/* Generate full symbol information for type unit PER_CU, whose DIEs have
9841 already been loaded into memory. */
9842
9843static void
9844process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9845 enum language pretend_language)
9846{
9847 struct dwarf2_cu *cu = per_cu->cu;
9848 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9849 struct objfile *objfile = dwarf2_per_objfile->objfile;
9850 struct compunit_symtab *cust;
9851 struct signatured_type *sig_type;
9852
9853 gdb_assert (per_cu->is_debug_types);
9854 sig_type = (struct signatured_type *) per_cu;
9855
9856 /* Clear the list here in case something was left over. */
9857 cu->method_list.clear ();
9858
9859 cu->language = pretend_language;
9860 cu->language_defn = language_def (cu->language);
9861
9862 /* The symbol tables are set up in read_type_unit_scope. */
9863 process_die (cu->dies, cu);
9864
9865 /* For now fudge the Go package. */
9866 if (cu->language == language_go)
9867 fixup_go_packaging (cu);
9868
9869 /* Now that we have processed all the DIEs in the CU, all the types
9870 should be complete, and it should now be safe to compute all of the
9871 physnames. */
9872 compute_delayed_physnames (cu);
9873
9874 if (cu->language == language_rust)
9875 rust_union_quirks (cu);
9876
9877 /* TUs share symbol tables.
9878 If this is the first TU to use this symtab, complete the construction
9879 of it with end_expandable_symtab. Otherwise, complete the addition of
9880 this TU's symbols to the existing symtab. */
9881 if (sig_type->type_unit_group->compunit_symtab == NULL)
9882 {
9883 buildsym_compunit *builder = cu->get_builder ();
9884 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9885 sig_type->type_unit_group->compunit_symtab = cust;
9886
9887 if (cust != NULL)
9888 {
9889 /* Set symtab language to language from DW_AT_language. If the
9890 compilation is from a C file generated by language preprocessors,
9891 do not set the language if it was already deduced by
9892 start_subfile. */
9893 if (!(cu->language == language_c
9894 && COMPUNIT_FILETABS (cust)->language != language_c))
9895 COMPUNIT_FILETABS (cust)->language = cu->language;
9896 }
9897 }
9898 else
9899 {
9900 cu->get_builder ()->augment_type_symtab ();
9901 cust = sig_type->type_unit_group->compunit_symtab;
9902 }
9903
9904 dwarf2_per_objfile->set_symtab (per_cu, cust);
9905
9906 /* Not needed any more. */
9907 cu->reset_builder ();
9908}
9909
9910/* Process an imported unit DIE. */
9911
9912static void
9913process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9914{
9915 struct attribute *attr;
9916
9917 /* For now we don't handle imported units in type units. */
9918 if (cu->per_cu->is_debug_types)
9919 {
9920 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9921 " supported in type units [in module %s]"),
9922 objfile_name (cu->per_objfile->objfile));
9923 }
9924
9925 attr = dwarf2_attr (die, DW_AT_import, cu);
9926 if (attr != NULL)
9927 {
9928 sect_offset sect_off = attr->get_ref_die_offset ();
9929 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9930 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9931 dwarf2_per_cu_data *per_cu
9932 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9933
9934 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9935 into another compilation unit, at root level. Regard this as a hint,
9936 and ignore it. */
9937 if (die->parent && die->parent->parent == NULL
9938 && per_cu->unit_type == DW_UT_compile
9939 && per_cu->lang == language_cplus)
9940 return;
9941
9942 /* If necessary, add it to the queue and load its DIEs. */
9943 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9944 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9945
9946 cu->per_cu->imported_symtabs_push (per_cu);
9947 }
9948}
9949
9950/* RAII object that represents a process_die scope: i.e.,
9951 starts/finishes processing a DIE. */
9952class process_die_scope
9953{
9954public:
9955 process_die_scope (die_info *die, dwarf2_cu *cu)
9956 : m_die (die), m_cu (cu)
9957 {
9958 /* We should only be processing DIEs not already in process. */
9959 gdb_assert (!m_die->in_process);
9960 m_die->in_process = true;
9961 }
9962
9963 ~process_die_scope ()
9964 {
9965 m_die->in_process = false;
9966
9967 /* If we're done processing the DIE for the CU that owns the line
9968 header, we don't need the line header anymore. */
9969 if (m_cu->line_header_die_owner == m_die)
9970 {
9971 delete m_cu->line_header;
9972 m_cu->line_header = NULL;
9973 m_cu->line_header_die_owner = NULL;
9974 }
9975 }
9976
9977private:
9978 die_info *m_die;
9979 dwarf2_cu *m_cu;
9980};
9981
9982/* Process a die and its children. */
9983
9984static void
9985process_die (struct die_info *die, struct dwarf2_cu *cu)
9986{
9987 process_die_scope scope (die, cu);
9988
9989 switch (die->tag)
9990 {
9991 case DW_TAG_padding:
9992 break;
9993 case DW_TAG_compile_unit:
9994 case DW_TAG_partial_unit:
9995 read_file_scope (die, cu);
9996 break;
9997 case DW_TAG_type_unit:
9998 read_type_unit_scope (die, cu);
9999 break;
10000 case DW_TAG_subprogram:
10001 /* Nested subprograms in Fortran get a prefix. */
10002 if (cu->language == language_fortran
10003 && die->parent != NULL
10004 && die->parent->tag == DW_TAG_subprogram)
10005 cu->processing_has_namespace_info = true;
10006 /* Fall through. */
10007 case DW_TAG_inlined_subroutine:
10008 read_func_scope (die, cu);
10009 break;
10010 case DW_TAG_lexical_block:
10011 case DW_TAG_try_block:
10012 case DW_TAG_catch_block:
10013 read_lexical_block_scope (die, cu);
10014 break;
10015 case DW_TAG_call_site:
10016 case DW_TAG_GNU_call_site:
10017 read_call_site_scope (die, cu);
10018 break;
10019 case DW_TAG_class_type:
10020 case DW_TAG_interface_type:
10021 case DW_TAG_structure_type:
10022 case DW_TAG_union_type:
10023 process_structure_scope (die, cu);
10024 break;
10025 case DW_TAG_enumeration_type:
10026 process_enumeration_scope (die, cu);
10027 break;
10028
10029 /* These dies have a type, but processing them does not create
10030 a symbol or recurse to process the children. Therefore we can
10031 read them on-demand through read_type_die. */
10032 case DW_TAG_subroutine_type:
10033 case DW_TAG_set_type:
10034 case DW_TAG_array_type:
10035 case DW_TAG_pointer_type:
10036 case DW_TAG_ptr_to_member_type:
10037 case DW_TAG_reference_type:
10038 case DW_TAG_rvalue_reference_type:
10039 case DW_TAG_string_type:
10040 break;
10041
10042 case DW_TAG_base_type:
10043 case DW_TAG_subrange_type:
10044 case DW_TAG_typedef:
10045 /* Add a typedef symbol for the type definition, if it has a
10046 DW_AT_name. */
10047 new_symbol (die, read_type_die (die, cu), cu);
10048 break;
10049 case DW_TAG_common_block:
10050 read_common_block (die, cu);
10051 break;
10052 case DW_TAG_common_inclusion:
10053 break;
10054 case DW_TAG_namespace:
10055 cu->processing_has_namespace_info = true;
10056 read_namespace (die, cu);
10057 break;
10058 case DW_TAG_module:
10059 cu->processing_has_namespace_info = true;
10060 read_module (die, cu);
10061 break;
10062 case DW_TAG_imported_declaration:
10063 cu->processing_has_namespace_info = true;
10064 if (read_namespace_alias (die, cu))
10065 break;
10066 /* The declaration is not a global namespace alias. */
10067 /* Fall through. */
10068 case DW_TAG_imported_module:
10069 cu->processing_has_namespace_info = true;
10070 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10071 || cu->language != language_fortran))
10072 complaint (_("Tag '%s' has unexpected children"),
10073 dwarf_tag_name (die->tag));
10074 read_import_statement (die, cu);
10075 break;
10076
10077 case DW_TAG_imported_unit:
10078 process_imported_unit_die (die, cu);
10079 break;
10080
10081 case DW_TAG_variable:
10082 read_variable (die, cu);
10083 break;
10084
10085 default:
10086 new_symbol (die, NULL, cu);
10087 break;
10088 }
10089}
10090\f
10091/* DWARF name computation. */
10092
10093/* A helper function for dwarf2_compute_name which determines whether DIE
10094 needs to have the name of the scope prepended to the name listed in the
10095 die. */
10096
10097static int
10098die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10099{
10100 struct attribute *attr;
10101
10102 switch (die->tag)
10103 {
10104 case DW_TAG_namespace:
10105 case DW_TAG_typedef:
10106 case DW_TAG_class_type:
10107 case DW_TAG_interface_type:
10108 case DW_TAG_structure_type:
10109 case DW_TAG_union_type:
10110 case DW_TAG_enumeration_type:
10111 case DW_TAG_enumerator:
10112 case DW_TAG_subprogram:
10113 case DW_TAG_inlined_subroutine:
10114 case DW_TAG_member:
10115 case DW_TAG_imported_declaration:
10116 return 1;
10117
10118 case DW_TAG_variable:
10119 case DW_TAG_constant:
10120 /* We only need to prefix "globally" visible variables. These include
10121 any variable marked with DW_AT_external or any variable that
10122 lives in a namespace. [Variables in anonymous namespaces
10123 require prefixing, but they are not DW_AT_external.] */
10124
10125 if (dwarf2_attr (die, DW_AT_specification, cu))
10126 {
10127 struct dwarf2_cu *spec_cu = cu;
10128
10129 return die_needs_namespace (die_specification (die, &spec_cu),
10130 spec_cu);
10131 }
10132
10133 attr = dwarf2_attr (die, DW_AT_external, cu);
10134 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10135 && die->parent->tag != DW_TAG_module)
10136 return 0;
10137 /* A variable in a lexical block of some kind does not need a
10138 namespace, even though in C++ such variables may be external
10139 and have a mangled name. */
10140 if (die->parent->tag == DW_TAG_lexical_block
10141 || die->parent->tag == DW_TAG_try_block
10142 || die->parent->tag == DW_TAG_catch_block
10143 || die->parent->tag == DW_TAG_subprogram)
10144 return 0;
10145 return 1;
10146
10147 default:
10148 return 0;
10149 }
10150}
10151
10152/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10153 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10154 defined for the given DIE. */
10155
10156static struct attribute *
10157dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10158{
10159 struct attribute *attr;
10160
10161 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10162 if (attr == NULL)
10163 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10164
10165 return attr;
10166}
10167
10168/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10169 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10170 defined for the given DIE. */
10171
10172static const char *
10173dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10174{
10175 const char *linkage_name;
10176
10177 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10178 if (linkage_name == NULL)
10179 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10180
10181 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10182 See https://github.com/rust-lang/rust/issues/32925. */
10183 if (cu->language == language_rust && linkage_name != NULL
10184 && strchr (linkage_name, '{') != NULL)
10185 linkage_name = NULL;
10186
10187 return linkage_name;
10188}
10189
10190/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10191 compute the physname for the object, which include a method's:
10192 - formal parameters (C++),
10193 - receiver type (Go),
10194
10195 The term "physname" is a bit confusing.
10196 For C++, for example, it is the demangled name.
10197 For Go, for example, it's the mangled name.
10198
10199 For Ada, return the DIE's linkage name rather than the fully qualified
10200 name. PHYSNAME is ignored..
10201
10202 The result is allocated on the objfile->per_bfd's obstack and
10203 canonicalized. */
10204
10205static const char *
10206dwarf2_compute_name (const char *name,
10207 struct die_info *die, struct dwarf2_cu *cu,
10208 int physname)
10209{
10210 struct objfile *objfile = cu->per_objfile->objfile;
10211
10212 if (name == NULL)
10213 name = dwarf2_name (die, cu);
10214
10215 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10216 but otherwise compute it by typename_concat inside GDB.
10217 FIXME: Actually this is not really true, or at least not always true.
10218 It's all very confusing. compute_and_set_names doesn't try to demangle
10219 Fortran names because there is no mangling standard. So new_symbol
10220 will set the demangled name to the result of dwarf2_full_name, and it is
10221 the demangled name that GDB uses if it exists. */
10222 if (cu->language == language_ada
10223 || (cu->language == language_fortran && physname))
10224 {
10225 /* For Ada unit, we prefer the linkage name over the name, as
10226 the former contains the exported name, which the user expects
10227 to be able to reference. Ideally, we want the user to be able
10228 to reference this entity using either natural or linkage name,
10229 but we haven't started looking at this enhancement yet. */
10230 const char *linkage_name = dw2_linkage_name (die, cu);
10231
10232 if (linkage_name != NULL)
10233 return linkage_name;
10234 }
10235
10236 /* These are the only languages we know how to qualify names in. */
10237 if (name != NULL
10238 && (cu->language == language_cplus
10239 || cu->language == language_fortran || cu->language == language_d
10240 || cu->language == language_rust))
10241 {
10242 if (die_needs_namespace (die, cu))
10243 {
10244 const char *prefix;
10245 const char *canonical_name = NULL;
10246
10247 string_file buf;
10248
10249 prefix = determine_prefix (die, cu);
10250 if (*prefix != '\0')
10251 {
10252 gdb::unique_xmalloc_ptr<char> prefixed_name
10253 (typename_concat (NULL, prefix, name, physname, cu));
10254
10255 buf.puts (prefixed_name.get ());
10256 }
10257 else
10258 buf.puts (name);
10259
10260 /* Template parameters may be specified in the DIE's DW_AT_name, or
10261 as children with DW_TAG_template_type_param or
10262 DW_TAG_value_type_param. If the latter, add them to the name
10263 here. If the name already has template parameters, then
10264 skip this step; some versions of GCC emit both, and
10265 it is more efficient to use the pre-computed name.
10266
10267 Something to keep in mind about this process: it is very
10268 unlikely, or in some cases downright impossible, to produce
10269 something that will match the mangled name of a function.
10270 If the definition of the function has the same debug info,
10271 we should be able to match up with it anyway. But fallbacks
10272 using the minimal symbol, for instance to find a method
10273 implemented in a stripped copy of libstdc++, will not work.
10274 If we do not have debug info for the definition, we will have to
10275 match them up some other way.
10276
10277 When we do name matching there is a related problem with function
10278 templates; two instantiated function templates are allowed to
10279 differ only by their return types, which we do not add here. */
10280
10281 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10282 {
10283 struct attribute *attr;
10284 struct die_info *child;
10285 int first = 1;
10286
10287 die->building_fullname = 1;
10288
10289 for (child = die->child; child != NULL; child = child->sibling)
10290 {
10291 struct type *type;
10292 LONGEST value;
10293 const gdb_byte *bytes;
10294 struct dwarf2_locexpr_baton *baton;
10295 struct value *v;
10296
10297 if (child->tag != DW_TAG_template_type_param
10298 && child->tag != DW_TAG_template_value_param)
10299 continue;
10300
10301 if (first)
10302 {
10303 buf.puts ("<");
10304 first = 0;
10305 }
10306 else
10307 buf.puts (", ");
10308
10309 attr = dwarf2_attr (child, DW_AT_type, cu);
10310 if (attr == NULL)
10311 {
10312 complaint (_("template parameter missing DW_AT_type"));
10313 buf.puts ("UNKNOWN_TYPE");
10314 continue;
10315 }
10316 type = die_type (child, cu);
10317
10318 if (child->tag == DW_TAG_template_type_param)
10319 {
10320 c_print_type (type, "", &buf, -1, 0, cu->language,
10321 &type_print_raw_options);
10322 continue;
10323 }
10324
10325 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10326 if (attr == NULL)
10327 {
10328 complaint (_("template parameter missing "
10329 "DW_AT_const_value"));
10330 buf.puts ("UNKNOWN_VALUE");
10331 continue;
10332 }
10333
10334 dwarf2_const_value_attr (attr, type, name,
10335 &cu->comp_unit_obstack, cu,
10336 &value, &bytes, &baton);
10337
10338 if (TYPE_NOSIGN (type))
10339 /* GDB prints characters as NUMBER 'CHAR'. If that's
10340 changed, this can use value_print instead. */
10341 c_printchar (value, type, &buf);
10342 else
10343 {
10344 struct value_print_options opts;
10345
10346 if (baton != NULL)
10347 v = dwarf2_evaluate_loc_desc (type, NULL,
10348 baton->data,
10349 baton->size,
10350 baton->per_cu);
10351 else if (bytes != NULL)
10352 {
10353 v = allocate_value (type);
10354 memcpy (value_contents_writeable (v), bytes,
10355 TYPE_LENGTH (type));
10356 }
10357 else
10358 v = value_from_longest (type, value);
10359
10360 /* Specify decimal so that we do not depend on
10361 the radix. */
10362 get_formatted_print_options (&opts, 'd');
10363 opts.raw = 1;
10364 value_print (v, &buf, &opts);
10365 release_value (v);
10366 }
10367 }
10368
10369 die->building_fullname = 0;
10370
10371 if (!first)
10372 {
10373 /* Close the argument list, with a space if necessary
10374 (nested templates). */
10375 if (!buf.empty () && buf.string ().back () == '>')
10376 buf.puts (" >");
10377 else
10378 buf.puts (">");
10379 }
10380 }
10381
10382 /* For C++ methods, append formal parameter type
10383 information, if PHYSNAME. */
10384
10385 if (physname && die->tag == DW_TAG_subprogram
10386 && cu->language == language_cplus)
10387 {
10388 struct type *type = read_type_die (die, cu);
10389
10390 c_type_print_args (type, &buf, 1, cu->language,
10391 &type_print_raw_options);
10392
10393 if (cu->language == language_cplus)
10394 {
10395 /* Assume that an artificial first parameter is
10396 "this", but do not crash if it is not. RealView
10397 marks unnamed (and thus unused) parameters as
10398 artificial; there is no way to differentiate
10399 the two cases. */
10400 if (type->num_fields () > 0
10401 && TYPE_FIELD_ARTIFICIAL (type, 0)
10402 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10403 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10404 0))))
10405 buf.puts (" const");
10406 }
10407 }
10408
10409 const std::string &intermediate_name = buf.string ();
10410
10411 if (cu->language == language_cplus)
10412 canonical_name
10413 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10414 objfile);
10415
10416 /* If we only computed INTERMEDIATE_NAME, or if
10417 INTERMEDIATE_NAME is already canonical, then we need to
10418 intern it. */
10419 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10420 name = objfile->intern (intermediate_name);
10421 else
10422 name = canonical_name;
10423 }
10424 }
10425
10426 return name;
10427}
10428
10429/* Return the fully qualified name of DIE, based on its DW_AT_name.
10430 If scope qualifiers are appropriate they will be added. The result
10431 will be allocated on the storage_obstack, or NULL if the DIE does
10432 not have a name. NAME may either be from a previous call to
10433 dwarf2_name or NULL.
10434
10435 The output string will be canonicalized (if C++). */
10436
10437static const char *
10438dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10439{
10440 return dwarf2_compute_name (name, die, cu, 0);
10441}
10442
10443/* Construct a physname for the given DIE in CU. NAME may either be
10444 from a previous call to dwarf2_name or NULL. The result will be
10445 allocated on the objfile_objstack or NULL if the DIE does not have a
10446 name.
10447
10448 The output string will be canonicalized (if C++). */
10449
10450static const char *
10451dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10452{
10453 struct objfile *objfile = cu->per_objfile->objfile;
10454 const char *retval, *mangled = NULL, *canon = NULL;
10455 int need_copy = 1;
10456
10457 /* In this case dwarf2_compute_name is just a shortcut not building anything
10458 on its own. */
10459 if (!die_needs_namespace (die, cu))
10460 return dwarf2_compute_name (name, die, cu, 1);
10461
10462 if (cu->language != language_rust)
10463 mangled = dw2_linkage_name (die, cu);
10464
10465 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10466 has computed. */
10467 gdb::unique_xmalloc_ptr<char> demangled;
10468 if (mangled != NULL)
10469 {
10470
10471 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10472 {
10473 /* Do nothing (do not demangle the symbol name). */
10474 }
10475 else if (cu->language == language_go)
10476 {
10477 /* This is a lie, but we already lie to the caller new_symbol.
10478 new_symbol assumes we return the mangled name.
10479 This just undoes that lie until things are cleaned up. */
10480 }
10481 else
10482 {
10483 /* Use DMGL_RET_DROP for C++ template functions to suppress
10484 their return type. It is easier for GDB users to search
10485 for such functions as `name(params)' than `long name(params)'.
10486 In such case the minimal symbol names do not match the full
10487 symbol names but for template functions there is never a need
10488 to look up their definition from their declaration so
10489 the only disadvantage remains the minimal symbol variant
10490 `long name(params)' does not have the proper inferior type. */
10491 demangled.reset (gdb_demangle (mangled,
10492 (DMGL_PARAMS | DMGL_ANSI
10493 | DMGL_RET_DROP)));
10494 }
10495 if (demangled)
10496 canon = demangled.get ();
10497 else
10498 {
10499 canon = mangled;
10500 need_copy = 0;
10501 }
10502 }
10503
10504 if (canon == NULL || check_physname)
10505 {
10506 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10507
10508 if (canon != NULL && strcmp (physname, canon) != 0)
10509 {
10510 /* It may not mean a bug in GDB. The compiler could also
10511 compute DW_AT_linkage_name incorrectly. But in such case
10512 GDB would need to be bug-to-bug compatible. */
10513
10514 complaint (_("Computed physname <%s> does not match demangled <%s> "
10515 "(from linkage <%s>) - DIE at %s [in module %s]"),
10516 physname, canon, mangled, sect_offset_str (die->sect_off),
10517 objfile_name (objfile));
10518
10519 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10520 is available here - over computed PHYSNAME. It is safer
10521 against both buggy GDB and buggy compilers. */
10522
10523 retval = canon;
10524 }
10525 else
10526 {
10527 retval = physname;
10528 need_copy = 0;
10529 }
10530 }
10531 else
10532 retval = canon;
10533
10534 if (need_copy)
10535 retval = objfile->intern (retval);
10536
10537 return retval;
10538}
10539
10540/* Inspect DIE in CU for a namespace alias. If one exists, record
10541 a new symbol for it.
10542
10543 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10544
10545static int
10546read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10547{
10548 struct attribute *attr;
10549
10550 /* If the die does not have a name, this is not a namespace
10551 alias. */
10552 attr = dwarf2_attr (die, DW_AT_name, cu);
10553 if (attr != NULL)
10554 {
10555 int num;
10556 struct die_info *d = die;
10557 struct dwarf2_cu *imported_cu = cu;
10558
10559 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10560 keep inspecting DIEs until we hit the underlying import. */
10561#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10562 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10563 {
10564 attr = dwarf2_attr (d, DW_AT_import, cu);
10565 if (attr == NULL)
10566 break;
10567
10568 d = follow_die_ref (d, attr, &imported_cu);
10569 if (d->tag != DW_TAG_imported_declaration)
10570 break;
10571 }
10572
10573 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10574 {
10575 complaint (_("DIE at %s has too many recursively imported "
10576 "declarations"), sect_offset_str (d->sect_off));
10577 return 0;
10578 }
10579
10580 if (attr != NULL)
10581 {
10582 struct type *type;
10583 sect_offset sect_off = attr->get_ref_die_offset ();
10584
10585 type = get_die_type_at_offset (sect_off, cu->per_cu);
10586 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10587 {
10588 /* This declaration is a global namespace alias. Add
10589 a symbol for it whose type is the aliased namespace. */
10590 new_symbol (die, type, cu);
10591 return 1;
10592 }
10593 }
10594 }
10595
10596 return 0;
10597}
10598
10599/* Return the using directives repository (global or local?) to use in the
10600 current context for CU.
10601
10602 For Ada, imported declarations can materialize renamings, which *may* be
10603 global. However it is impossible (for now?) in DWARF to distinguish
10604 "external" imported declarations and "static" ones. As all imported
10605 declarations seem to be static in all other languages, make them all CU-wide
10606 global only in Ada. */
10607
10608static struct using_direct **
10609using_directives (struct dwarf2_cu *cu)
10610{
10611 if (cu->language == language_ada
10612 && cu->get_builder ()->outermost_context_p ())
10613 return cu->get_builder ()->get_global_using_directives ();
10614 else
10615 return cu->get_builder ()->get_local_using_directives ();
10616}
10617
10618/* Read the import statement specified by the given die and record it. */
10619
10620static void
10621read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10622{
10623 struct objfile *objfile = cu->per_objfile->objfile;
10624 struct attribute *import_attr;
10625 struct die_info *imported_die, *child_die;
10626 struct dwarf2_cu *imported_cu;
10627 const char *imported_name;
10628 const char *imported_name_prefix;
10629 const char *canonical_name;
10630 const char *import_alias;
10631 const char *imported_declaration = NULL;
10632 const char *import_prefix;
10633 std::vector<const char *> excludes;
10634
10635 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10636 if (import_attr == NULL)
10637 {
10638 complaint (_("Tag '%s' has no DW_AT_import"),
10639 dwarf_tag_name (die->tag));
10640 return;
10641 }
10642
10643 imported_cu = cu;
10644 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10645 imported_name = dwarf2_name (imported_die, imported_cu);
10646 if (imported_name == NULL)
10647 {
10648 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10649
10650 The import in the following code:
10651 namespace A
10652 {
10653 typedef int B;
10654 }
10655
10656 int main ()
10657 {
10658 using A::B;
10659 B b;
10660 return b;
10661 }
10662
10663 ...
10664 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10665 <52> DW_AT_decl_file : 1
10666 <53> DW_AT_decl_line : 6
10667 <54> DW_AT_import : <0x75>
10668 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10669 <59> DW_AT_name : B
10670 <5b> DW_AT_decl_file : 1
10671 <5c> DW_AT_decl_line : 2
10672 <5d> DW_AT_type : <0x6e>
10673 ...
10674 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10675 <76> DW_AT_byte_size : 4
10676 <77> DW_AT_encoding : 5 (signed)
10677
10678 imports the wrong die ( 0x75 instead of 0x58 ).
10679 This case will be ignored until the gcc bug is fixed. */
10680 return;
10681 }
10682
10683 /* Figure out the local name after import. */
10684 import_alias = dwarf2_name (die, cu);
10685
10686 /* Figure out where the statement is being imported to. */
10687 import_prefix = determine_prefix (die, cu);
10688
10689 /* Figure out what the scope of the imported die is and prepend it
10690 to the name of the imported die. */
10691 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10692
10693 if (imported_die->tag != DW_TAG_namespace
10694 && imported_die->tag != DW_TAG_module)
10695 {
10696 imported_declaration = imported_name;
10697 canonical_name = imported_name_prefix;
10698 }
10699 else if (strlen (imported_name_prefix) > 0)
10700 canonical_name = obconcat (&objfile->objfile_obstack,
10701 imported_name_prefix,
10702 (cu->language == language_d ? "." : "::"),
10703 imported_name, (char *) NULL);
10704 else
10705 canonical_name = imported_name;
10706
10707 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10708 for (child_die = die->child; child_die && child_die->tag;
10709 child_die = child_die->sibling)
10710 {
10711 /* DWARF-4: A Fortran use statement with a “rename list” may be
10712 represented by an imported module entry with an import attribute
10713 referring to the module and owned entries corresponding to those
10714 entities that are renamed as part of being imported. */
10715
10716 if (child_die->tag != DW_TAG_imported_declaration)
10717 {
10718 complaint (_("child DW_TAG_imported_declaration expected "
10719 "- DIE at %s [in module %s]"),
10720 sect_offset_str (child_die->sect_off),
10721 objfile_name (objfile));
10722 continue;
10723 }
10724
10725 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10726 if (import_attr == NULL)
10727 {
10728 complaint (_("Tag '%s' has no DW_AT_import"),
10729 dwarf_tag_name (child_die->tag));
10730 continue;
10731 }
10732
10733 imported_cu = cu;
10734 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10735 &imported_cu);
10736 imported_name = dwarf2_name (imported_die, imported_cu);
10737 if (imported_name == NULL)
10738 {
10739 complaint (_("child DW_TAG_imported_declaration has unknown "
10740 "imported name - DIE at %s [in module %s]"),
10741 sect_offset_str (child_die->sect_off),
10742 objfile_name (objfile));
10743 continue;
10744 }
10745
10746 excludes.push_back (imported_name);
10747
10748 process_die (child_die, cu);
10749 }
10750
10751 add_using_directive (using_directives (cu),
10752 import_prefix,
10753 canonical_name,
10754 import_alias,
10755 imported_declaration,
10756 excludes,
10757 0,
10758 &objfile->objfile_obstack);
10759}
10760
10761/* ICC<14 does not output the required DW_AT_declaration on incomplete
10762 types, but gives them a size of zero. Starting with version 14,
10763 ICC is compatible with GCC. */
10764
10765static bool
10766producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10767{
10768 if (!cu->checked_producer)
10769 check_producer (cu);
10770
10771 return cu->producer_is_icc_lt_14;
10772}
10773
10774/* ICC generates a DW_AT_type for C void functions. This was observed on
10775 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10776 which says that void functions should not have a DW_AT_type. */
10777
10778static bool
10779producer_is_icc (struct dwarf2_cu *cu)
10780{
10781 if (!cu->checked_producer)
10782 check_producer (cu);
10783
10784 return cu->producer_is_icc;
10785}
10786
10787/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10788 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10789 this, it was first present in GCC release 4.3.0. */
10790
10791static bool
10792producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10793{
10794 if (!cu->checked_producer)
10795 check_producer (cu);
10796
10797 return cu->producer_is_gcc_lt_4_3;
10798}
10799
10800static file_and_directory
10801find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10802{
10803 file_and_directory res;
10804
10805 /* Find the filename. Do not use dwarf2_name here, since the filename
10806 is not a source language identifier. */
10807 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10808 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10809
10810 if (res.comp_dir == NULL
10811 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10812 && IS_ABSOLUTE_PATH (res.name))
10813 {
10814 res.comp_dir_storage = ldirname (res.name);
10815 if (!res.comp_dir_storage.empty ())
10816 res.comp_dir = res.comp_dir_storage.c_str ();
10817 }
10818 if (res.comp_dir != NULL)
10819 {
10820 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10821 directory, get rid of it. */
10822 const char *cp = strchr (res.comp_dir, ':');
10823
10824 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10825 res.comp_dir = cp + 1;
10826 }
10827
10828 if (res.name == NULL)
10829 res.name = "<unknown>";
10830
10831 return res;
10832}
10833
10834/* Handle DW_AT_stmt_list for a compilation unit.
10835 DIE is the DW_TAG_compile_unit die for CU.
10836 COMP_DIR is the compilation directory. LOWPC is passed to
10837 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10838
10839static void
10840handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10841 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10842{
10843 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10844 struct attribute *attr;
10845 struct line_header line_header_local;
10846 hashval_t line_header_local_hash;
10847 void **slot;
10848 int decode_mapping;
10849
10850 gdb_assert (! cu->per_cu->is_debug_types);
10851
10852 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10853 if (attr == NULL)
10854 return;
10855
10856 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10857
10858 /* The line header hash table is only created if needed (it exists to
10859 prevent redundant reading of the line table for partial_units).
10860 If we're given a partial_unit, we'll need it. If we're given a
10861 compile_unit, then use the line header hash table if it's already
10862 created, but don't create one just yet. */
10863
10864 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10865 && die->tag == DW_TAG_partial_unit)
10866 {
10867 dwarf2_per_objfile->per_bfd->line_header_hash
10868 .reset (htab_create_alloc (127, line_header_hash_voidp,
10869 line_header_eq_voidp,
10870 free_line_header_voidp,
10871 xcalloc, xfree));
10872 }
10873
10874 line_header_local.sect_off = line_offset;
10875 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10876 line_header_local_hash = line_header_hash (&line_header_local);
10877 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10878 {
10879 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10880 &line_header_local,
10881 line_header_local_hash, NO_INSERT);
10882
10883 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10884 is not present in *SLOT (since if there is something in *SLOT then
10885 it will be for a partial_unit). */
10886 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10887 {
10888 gdb_assert (*slot != NULL);
10889 cu->line_header = (struct line_header *) *slot;
10890 return;
10891 }
10892 }
10893
10894 /* dwarf_decode_line_header does not yet provide sufficient information.
10895 We always have to call also dwarf_decode_lines for it. */
10896 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10897 if (lh == NULL)
10898 return;
10899
10900 cu->line_header = lh.release ();
10901 cu->line_header_die_owner = die;
10902
10903 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10904 slot = NULL;
10905 else
10906 {
10907 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10908 &line_header_local,
10909 line_header_local_hash, INSERT);
10910 gdb_assert (slot != NULL);
10911 }
10912 if (slot != NULL && *slot == NULL)
10913 {
10914 /* This newly decoded line number information unit will be owned
10915 by line_header_hash hash table. */
10916 *slot = cu->line_header;
10917 cu->line_header_die_owner = NULL;
10918 }
10919 else
10920 {
10921 /* We cannot free any current entry in (*slot) as that struct line_header
10922 may be already used by multiple CUs. Create only temporary decoded
10923 line_header for this CU - it may happen at most once for each line
10924 number information unit. And if we're not using line_header_hash
10925 then this is what we want as well. */
10926 gdb_assert (die->tag != DW_TAG_partial_unit);
10927 }
10928 decode_mapping = (die->tag != DW_TAG_partial_unit);
10929 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10930 decode_mapping);
10931
10932}
10933
10934/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10935
10936static void
10937read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10938{
10939 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10940 struct objfile *objfile = dwarf2_per_objfile->objfile;
10941 struct gdbarch *gdbarch = objfile->arch ();
10942 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10943 CORE_ADDR highpc = ((CORE_ADDR) 0);
10944 struct attribute *attr;
10945 struct die_info *child_die;
10946 CORE_ADDR baseaddr;
10947
10948 prepare_one_comp_unit (cu, die, cu->language);
10949 baseaddr = objfile->text_section_offset ();
10950
10951 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10952
10953 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10954 from finish_block. */
10955 if (lowpc == ((CORE_ADDR) -1))
10956 lowpc = highpc;
10957 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10958
10959 file_and_directory fnd = find_file_and_directory (die, cu);
10960
10961 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10962 standardised yet. As a workaround for the language detection we fall
10963 back to the DW_AT_producer string. */
10964 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10965 cu->language = language_opencl;
10966
10967 /* Similar hack for Go. */
10968 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10969 set_cu_language (DW_LANG_Go, cu);
10970
10971 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10972
10973 /* Decode line number information if present. We do this before
10974 processing child DIEs, so that the line header table is available
10975 for DW_AT_decl_file. */
10976 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10977
10978 /* Process all dies in compilation unit. */
10979 if (die->child != NULL)
10980 {
10981 child_die = die->child;
10982 while (child_die && child_die->tag)
10983 {
10984 process_die (child_die, cu);
10985 child_die = child_die->sibling;
10986 }
10987 }
10988
10989 /* Decode macro information, if present. Dwarf 2 macro information
10990 refers to information in the line number info statement program
10991 header, so we can only read it if we've read the header
10992 successfully. */
10993 attr = dwarf2_attr (die, DW_AT_macros, cu);
10994 if (attr == NULL)
10995 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10996 if (attr && cu->line_header)
10997 {
10998 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10999 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11000
11001 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11002 }
11003 else
11004 {
11005 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11006 if (attr && cu->line_header)
11007 {
11008 unsigned int macro_offset = DW_UNSND (attr);
11009
11010 dwarf_decode_macros (cu, macro_offset, 0);
11011 }
11012 }
11013}
11014
11015void
11016dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11017{
11018 struct type_unit_group *tu_group;
11019 int first_time;
11020 struct attribute *attr;
11021 unsigned int i;
11022 struct signatured_type *sig_type;
11023
11024 gdb_assert (per_cu->is_debug_types);
11025 sig_type = (struct signatured_type *) per_cu;
11026
11027 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11028
11029 /* If we're using .gdb_index (includes -readnow) then
11030 per_cu->type_unit_group may not have been set up yet. */
11031 if (sig_type->type_unit_group == NULL)
11032 sig_type->type_unit_group = get_type_unit_group (this, attr);
11033 tu_group = sig_type->type_unit_group;
11034
11035 /* If we've already processed this stmt_list there's no real need to
11036 do it again, we could fake it and just recreate the part we need
11037 (file name,index -> symtab mapping). If data shows this optimization
11038 is useful we can do it then. */
11039 first_time = tu_group->compunit_symtab == NULL;
11040
11041 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11042 debug info. */
11043 line_header_up lh;
11044 if (attr != NULL)
11045 {
11046 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11047 lh = dwarf_decode_line_header (line_offset, this);
11048 }
11049 if (lh == NULL)
11050 {
11051 if (first_time)
11052 start_symtab ("", NULL, 0);
11053 else
11054 {
11055 gdb_assert (tu_group->symtabs == NULL);
11056 gdb_assert (m_builder == nullptr);
11057 struct compunit_symtab *cust = tu_group->compunit_symtab;
11058 m_builder.reset (new struct buildsym_compunit
11059 (COMPUNIT_OBJFILE (cust), "",
11060 COMPUNIT_DIRNAME (cust),
11061 compunit_language (cust),
11062 0, cust));
11063 list_in_scope = get_builder ()->get_file_symbols ();
11064 }
11065 return;
11066 }
11067
11068 line_header = lh.release ();
11069 line_header_die_owner = die;
11070
11071 if (first_time)
11072 {
11073 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11074
11075 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11076 still initializing it, and our caller (a few levels up)
11077 process_full_type_unit still needs to know if this is the first
11078 time. */
11079
11080 tu_group->symtabs
11081 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11082 struct symtab *, line_header->file_names_size ());
11083
11084 auto &file_names = line_header->file_names ();
11085 for (i = 0; i < file_names.size (); ++i)
11086 {
11087 file_entry &fe = file_names[i];
11088 dwarf2_start_subfile (this, fe.name,
11089 fe.include_dir (line_header));
11090 buildsym_compunit *b = get_builder ();
11091 if (b->get_current_subfile ()->symtab == NULL)
11092 {
11093 /* NOTE: start_subfile will recognize when it's been
11094 passed a file it has already seen. So we can't
11095 assume there's a simple mapping from
11096 cu->line_header->file_names to subfiles, plus
11097 cu->line_header->file_names may contain dups. */
11098 b->get_current_subfile ()->symtab
11099 = allocate_symtab (cust, b->get_current_subfile ()->name);
11100 }
11101
11102 fe.symtab = b->get_current_subfile ()->symtab;
11103 tu_group->symtabs[i] = fe.symtab;
11104 }
11105 }
11106 else
11107 {
11108 gdb_assert (m_builder == nullptr);
11109 struct compunit_symtab *cust = tu_group->compunit_symtab;
11110 m_builder.reset (new struct buildsym_compunit
11111 (COMPUNIT_OBJFILE (cust), "",
11112 COMPUNIT_DIRNAME (cust),
11113 compunit_language (cust),
11114 0, cust));
11115 list_in_scope = get_builder ()->get_file_symbols ();
11116
11117 auto &file_names = line_header->file_names ();
11118 for (i = 0; i < file_names.size (); ++i)
11119 {
11120 file_entry &fe = file_names[i];
11121 fe.symtab = tu_group->symtabs[i];
11122 }
11123 }
11124
11125 /* The main symtab is allocated last. Type units don't have DW_AT_name
11126 so they don't have a "real" (so to speak) symtab anyway.
11127 There is later code that will assign the main symtab to all symbols
11128 that don't have one. We need to handle the case of a symbol with a
11129 missing symtab (DW_AT_decl_file) anyway. */
11130}
11131
11132/* Process DW_TAG_type_unit.
11133 For TUs we want to skip the first top level sibling if it's not the
11134 actual type being defined by this TU. In this case the first top
11135 level sibling is there to provide context only. */
11136
11137static void
11138read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11139{
11140 struct die_info *child_die;
11141
11142 prepare_one_comp_unit (cu, die, language_minimal);
11143
11144 /* Initialize (or reinitialize) the machinery for building symtabs.
11145 We do this before processing child DIEs, so that the line header table
11146 is available for DW_AT_decl_file. */
11147 cu->setup_type_unit_groups (die);
11148
11149 if (die->child != NULL)
11150 {
11151 child_die = die->child;
11152 while (child_die && child_die->tag)
11153 {
11154 process_die (child_die, cu);
11155 child_die = child_die->sibling;
11156 }
11157 }
11158}
11159\f
11160/* DWO/DWP files.
11161
11162 http://gcc.gnu.org/wiki/DebugFission
11163 http://gcc.gnu.org/wiki/DebugFissionDWP
11164
11165 To simplify handling of both DWO files ("object" files with the DWARF info)
11166 and DWP files (a file with the DWOs packaged up into one file), we treat
11167 DWP files as having a collection of virtual DWO files. */
11168
11169static hashval_t
11170hash_dwo_file (const void *item)
11171{
11172 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11173 hashval_t hash;
11174
11175 hash = htab_hash_string (dwo_file->dwo_name);
11176 if (dwo_file->comp_dir != NULL)
11177 hash += htab_hash_string (dwo_file->comp_dir);
11178 return hash;
11179}
11180
11181static int
11182eq_dwo_file (const void *item_lhs, const void *item_rhs)
11183{
11184 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11185 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11186
11187 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11188 return 0;
11189 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11190 return lhs->comp_dir == rhs->comp_dir;
11191 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11192}
11193
11194/* Allocate a hash table for DWO files. */
11195
11196static htab_up
11197allocate_dwo_file_hash_table ()
11198{
11199 auto delete_dwo_file = [] (void *item)
11200 {
11201 struct dwo_file *dwo_file = (struct dwo_file *) item;
11202
11203 delete dwo_file;
11204 };
11205
11206 return htab_up (htab_create_alloc (41,
11207 hash_dwo_file,
11208 eq_dwo_file,
11209 delete_dwo_file,
11210 xcalloc, xfree));
11211}
11212
11213/* Lookup DWO file DWO_NAME. */
11214
11215static void **
11216lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11217 const char *dwo_name,
11218 const char *comp_dir)
11219{
11220 struct dwo_file find_entry;
11221 void **slot;
11222
11223 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11224 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11225
11226 find_entry.dwo_name = dwo_name;
11227 find_entry.comp_dir = comp_dir;
11228 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11229 INSERT);
11230
11231 return slot;
11232}
11233
11234static hashval_t
11235hash_dwo_unit (const void *item)
11236{
11237 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11238
11239 /* This drops the top 32 bits of the id, but is ok for a hash. */
11240 return dwo_unit->signature;
11241}
11242
11243static int
11244eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11245{
11246 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11247 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11248
11249 /* The signature is assumed to be unique within the DWO file.
11250 So while object file CU dwo_id's always have the value zero,
11251 that's OK, assuming each object file DWO file has only one CU,
11252 and that's the rule for now. */
11253 return lhs->signature == rhs->signature;
11254}
11255
11256/* Allocate a hash table for DWO CUs,TUs.
11257 There is one of these tables for each of CUs,TUs for each DWO file. */
11258
11259static htab_up
11260allocate_dwo_unit_table ()
11261{
11262 /* Start out with a pretty small number.
11263 Generally DWO files contain only one CU and maybe some TUs. */
11264 return htab_up (htab_create_alloc (3,
11265 hash_dwo_unit,
11266 eq_dwo_unit,
11267 NULL, xcalloc, xfree));
11268}
11269
11270/* die_reader_func for create_dwo_cu. */
11271
11272static void
11273create_dwo_cu_reader (const struct die_reader_specs *reader,
11274 const gdb_byte *info_ptr,
11275 struct die_info *comp_unit_die,
11276 struct dwo_file *dwo_file,
11277 struct dwo_unit *dwo_unit)
11278{
11279 struct dwarf2_cu *cu = reader->cu;
11280 sect_offset sect_off = cu->per_cu->sect_off;
11281 struct dwarf2_section_info *section = cu->per_cu->section;
11282
11283 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11284 if (!signature.has_value ())
11285 {
11286 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11287 " its dwo_id [in module %s]"),
11288 sect_offset_str (sect_off), dwo_file->dwo_name);
11289 return;
11290 }
11291
11292 dwo_unit->dwo_file = dwo_file;
11293 dwo_unit->signature = *signature;
11294 dwo_unit->section = section;
11295 dwo_unit->sect_off = sect_off;
11296 dwo_unit->length = cu->per_cu->length;
11297
11298 if (dwarf_read_debug)
11299 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11300 sect_offset_str (sect_off),
11301 hex_string (dwo_unit->signature));
11302}
11303
11304/* Create the dwo_units for the CUs in a DWO_FILE.
11305 Note: This function processes DWO files only, not DWP files. */
11306
11307static void
11308create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11309 dwarf2_cu *cu, struct dwo_file &dwo_file,
11310 dwarf2_section_info &section, htab_up &cus_htab)
11311{
11312 struct objfile *objfile = dwarf2_per_objfile->objfile;
11313 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11314 const gdb_byte *info_ptr, *end_ptr;
11315
11316 section.read (objfile);
11317 info_ptr = section.buffer;
11318
11319 if (info_ptr == NULL)
11320 return;
11321
11322 if (dwarf_read_debug)
11323 {
11324 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11325 section.get_name (),
11326 section.get_file_name ());
11327 }
11328
11329 end_ptr = info_ptr + section.size;
11330 while (info_ptr < end_ptr)
11331 {
11332 struct dwarf2_per_cu_data per_cu;
11333 struct dwo_unit read_unit {};
11334 struct dwo_unit *dwo_unit;
11335 void **slot;
11336 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11337
11338 memset (&per_cu, 0, sizeof (per_cu));
11339 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11340 per_cu.per_bfd = per_bfd;
11341 per_cu.is_debug_types = 0;
11342 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11343 per_cu.section = &section;
11344
11345 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11346 if (!reader.dummy_p)
11347 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11348 &dwo_file, &read_unit);
11349 info_ptr += per_cu.length;
11350
11351 // If the unit could not be parsed, skip it.
11352 if (read_unit.dwo_file == NULL)
11353 continue;
11354
11355 if (cus_htab == NULL)
11356 cus_htab = allocate_dwo_unit_table ();
11357
11358 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11359 struct dwo_unit);
11360 *dwo_unit = read_unit;
11361 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11362 gdb_assert (slot != NULL);
11363 if (*slot != NULL)
11364 {
11365 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11366 sect_offset dup_sect_off = dup_cu->sect_off;
11367
11368 complaint (_("debug cu entry at offset %s is duplicate to"
11369 " the entry at offset %s, signature %s"),
11370 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11371 hex_string (dwo_unit->signature));
11372 }
11373 *slot = (void *)dwo_unit;
11374 }
11375}
11376
11377/* DWP file .debug_{cu,tu}_index section format:
11378 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11379
11380 DWP Version 1:
11381
11382 Both index sections have the same format, and serve to map a 64-bit
11383 signature to a set of section numbers. Each section begins with a header,
11384 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11385 indexes, and a pool of 32-bit section numbers. The index sections will be
11386 aligned at 8-byte boundaries in the file.
11387
11388 The index section header consists of:
11389
11390 V, 32 bit version number
11391 -, 32 bits unused
11392 N, 32 bit number of compilation units or type units in the index
11393 M, 32 bit number of slots in the hash table
11394
11395 Numbers are recorded using the byte order of the application binary.
11396
11397 The hash table begins at offset 16 in the section, and consists of an array
11398 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11399 order of the application binary). Unused slots in the hash table are 0.
11400 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11401
11402 The parallel table begins immediately after the hash table
11403 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11404 array of 32-bit indexes (using the byte order of the application binary),
11405 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11406 table contains a 32-bit index into the pool of section numbers. For unused
11407 hash table slots, the corresponding entry in the parallel table will be 0.
11408
11409 The pool of section numbers begins immediately following the hash table
11410 (at offset 16 + 12 * M from the beginning of the section). The pool of
11411 section numbers consists of an array of 32-bit words (using the byte order
11412 of the application binary). Each item in the array is indexed starting
11413 from 0. The hash table entry provides the index of the first section
11414 number in the set. Additional section numbers in the set follow, and the
11415 set is terminated by a 0 entry (section number 0 is not used in ELF).
11416
11417 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11418 section must be the first entry in the set, and the .debug_abbrev.dwo must
11419 be the second entry. Other members of the set may follow in any order.
11420
11421 ---
11422
11423 DWP Version 2:
11424
11425 DWP Version 2 combines all the .debug_info, etc. sections into one,
11426 and the entries in the index tables are now offsets into these sections.
11427 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11428 section.
11429
11430 Index Section Contents:
11431 Header
11432 Hash Table of Signatures dwp_hash_table.hash_table
11433 Parallel Table of Indices dwp_hash_table.unit_table
11434 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11435 Table of Section Sizes dwp_hash_table.v2.sizes
11436
11437 The index section header consists of:
11438
11439 V, 32 bit version number
11440 L, 32 bit number of columns in the table of section offsets
11441 N, 32 bit number of compilation units or type units in the index
11442 M, 32 bit number of slots in the hash table
11443
11444 Numbers are recorded using the byte order of the application binary.
11445
11446 The hash table has the same format as version 1.
11447 The parallel table of indices has the same format as version 1,
11448 except that the entries are origin-1 indices into the table of sections
11449 offsets and the table of section sizes.
11450
11451 The table of offsets begins immediately following the parallel table
11452 (at offset 16 + 12 * M from the beginning of the section). The table is
11453 a two-dimensional array of 32-bit words (using the byte order of the
11454 application binary), with L columns and N+1 rows, in row-major order.
11455 Each row in the array is indexed starting from 0. The first row provides
11456 a key to the remaining rows: each column in this row provides an identifier
11457 for a debug section, and the offsets in the same column of subsequent rows
11458 refer to that section. The section identifiers are:
11459
11460 DW_SECT_INFO 1 .debug_info.dwo
11461 DW_SECT_TYPES 2 .debug_types.dwo
11462 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11463 DW_SECT_LINE 4 .debug_line.dwo
11464 DW_SECT_LOC 5 .debug_loc.dwo
11465 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11466 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11467 DW_SECT_MACRO 8 .debug_macro.dwo
11468
11469 The offsets provided by the CU and TU index sections are the base offsets
11470 for the contributions made by each CU or TU to the corresponding section
11471 in the package file. Each CU and TU header contains an abbrev_offset
11472 field, used to find the abbreviations table for that CU or TU within the
11473 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11474 be interpreted as relative to the base offset given in the index section.
11475 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11476 should be interpreted as relative to the base offset for .debug_line.dwo,
11477 and offsets into other debug sections obtained from DWARF attributes should
11478 also be interpreted as relative to the corresponding base offset.
11479
11480 The table of sizes begins immediately following the table of offsets.
11481 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11482 with L columns and N rows, in row-major order. Each row in the array is
11483 indexed starting from 1 (row 0 is shared by the two tables).
11484
11485 ---
11486
11487 Hash table lookup is handled the same in version 1 and 2:
11488
11489 We assume that N and M will not exceed 2^32 - 1.
11490 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11491
11492 Given a 64-bit compilation unit signature or a type signature S, an entry
11493 in the hash table is located as follows:
11494
11495 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11496 the low-order k bits all set to 1.
11497
11498 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11499
11500 3) If the hash table entry at index H matches the signature, use that
11501 entry. If the hash table entry at index H is unused (all zeroes),
11502 terminate the search: the signature is not present in the table.
11503
11504 4) Let H = (H + H') modulo M. Repeat at Step 3.
11505
11506 Because M > N and H' and M are relatively prime, the search is guaranteed
11507 to stop at an unused slot or find the match. */
11508
11509/* Create a hash table to map DWO IDs to their CU/TU entry in
11510 .debug_{info,types}.dwo in DWP_FILE.
11511 Returns NULL if there isn't one.
11512 Note: This function processes DWP files only, not DWO files. */
11513
11514static struct dwp_hash_table *
11515create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11516 struct dwp_file *dwp_file, int is_debug_types)
11517{
11518 struct objfile *objfile = dwarf2_per_objfile->objfile;
11519 bfd *dbfd = dwp_file->dbfd.get ();
11520 const gdb_byte *index_ptr, *index_end;
11521 struct dwarf2_section_info *index;
11522 uint32_t version, nr_columns, nr_units, nr_slots;
11523 struct dwp_hash_table *htab;
11524
11525 if (is_debug_types)
11526 index = &dwp_file->sections.tu_index;
11527 else
11528 index = &dwp_file->sections.cu_index;
11529
11530 if (index->empty ())
11531 return NULL;
11532 index->read (objfile);
11533
11534 index_ptr = index->buffer;
11535 index_end = index_ptr + index->size;
11536
11537 version = read_4_bytes (dbfd, index_ptr);
11538 index_ptr += 4;
11539 if (version == 2)
11540 nr_columns = read_4_bytes (dbfd, index_ptr);
11541 else
11542 nr_columns = 0;
11543 index_ptr += 4;
11544 nr_units = read_4_bytes (dbfd, index_ptr);
11545 index_ptr += 4;
11546 nr_slots = read_4_bytes (dbfd, index_ptr);
11547 index_ptr += 4;
11548
11549 if (version != 1 && version != 2)
11550 {
11551 error (_("Dwarf Error: unsupported DWP file version (%s)"
11552 " [in module %s]"),
11553 pulongest (version), dwp_file->name);
11554 }
11555 if (nr_slots != (nr_slots & -nr_slots))
11556 {
11557 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11558 " is not power of 2 [in module %s]"),
11559 pulongest (nr_slots), dwp_file->name);
11560 }
11561
11562 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11563 htab->version = version;
11564 htab->nr_columns = nr_columns;
11565 htab->nr_units = nr_units;
11566 htab->nr_slots = nr_slots;
11567 htab->hash_table = index_ptr;
11568 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11569
11570 /* Exit early if the table is empty. */
11571 if (nr_slots == 0 || nr_units == 0
11572 || (version == 2 && nr_columns == 0))
11573 {
11574 /* All must be zero. */
11575 if (nr_slots != 0 || nr_units != 0
11576 || (version == 2 && nr_columns != 0))
11577 {
11578 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11579 " all zero [in modules %s]"),
11580 dwp_file->name);
11581 }
11582 return htab;
11583 }
11584
11585 if (version == 1)
11586 {
11587 htab->section_pool.v1.indices =
11588 htab->unit_table + sizeof (uint32_t) * nr_slots;
11589 /* It's harder to decide whether the section is too small in v1.
11590 V1 is deprecated anyway so we punt. */
11591 }
11592 else
11593 {
11594 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11595 int *ids = htab->section_pool.v2.section_ids;
11596 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11597 /* Reverse map for error checking. */
11598 int ids_seen[DW_SECT_MAX + 1];
11599 int i;
11600
11601 if (nr_columns < 2)
11602 {
11603 error (_("Dwarf Error: bad DWP hash table, too few columns"
11604 " in section table [in module %s]"),
11605 dwp_file->name);
11606 }
11607 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11608 {
11609 error (_("Dwarf Error: bad DWP hash table, too many columns"
11610 " in section table [in module %s]"),
11611 dwp_file->name);
11612 }
11613 memset (ids, 255, sizeof_ids);
11614 memset (ids_seen, 255, sizeof (ids_seen));
11615 for (i = 0; i < nr_columns; ++i)
11616 {
11617 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11618
11619 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11622 " in section table [in module %s]"),
11623 id, dwp_file->name);
11624 }
11625 if (ids_seen[id] != -1)
11626 {
11627 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11628 " id %d in section table [in module %s]"),
11629 id, dwp_file->name);
11630 }
11631 ids_seen[id] = i;
11632 ids[i] = id;
11633 }
11634 /* Must have exactly one info or types section. */
11635 if (((ids_seen[DW_SECT_INFO] != -1)
11636 + (ids_seen[DW_SECT_TYPES] != -1))
11637 != 1)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11640 " DWO info/types section [in module %s]"),
11641 dwp_file->name);
11642 }
11643 /* Must have an abbrev section. */
11644 if (ids_seen[DW_SECT_ABBREV] == -1)
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11647 " section [in module %s]"),
11648 dwp_file->name);
11649 }
11650 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11651 htab->section_pool.v2.sizes =
11652 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11653 * nr_units * nr_columns);
11654 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11655 * nr_units * nr_columns))
11656 > index_end)
11657 {
11658 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11659 " [in module %s]"),
11660 dwp_file->name);
11661 }
11662 }
11663
11664 return htab;
11665}
11666
11667/* Update SECTIONS with the data from SECTP.
11668
11669 This function is like the other "locate" section routines that are
11670 passed to bfd_map_over_sections, but in this context the sections to
11671 read comes from the DWP V1 hash table, not the full ELF section table.
11672
11673 The result is non-zero for success, or zero if an error was found. */
11674
11675static int
11676locate_v1_virtual_dwo_sections (asection *sectp,
11677 struct virtual_v1_dwo_sections *sections)
11678{
11679 const struct dwop_section_names *names = &dwop_section_names;
11680
11681 if (section_is_p (sectp->name, &names->abbrev_dwo))
11682 {
11683 /* There can be only one. */
11684 if (sections->abbrev.s.section != NULL)
11685 return 0;
11686 sections->abbrev.s.section = sectp;
11687 sections->abbrev.size = bfd_section_size (sectp);
11688 }
11689 else if (section_is_p (sectp->name, &names->info_dwo)
11690 || section_is_p (sectp->name, &names->types_dwo))
11691 {
11692 /* There can be only one. */
11693 if (sections->info_or_types.s.section != NULL)
11694 return 0;
11695 sections->info_or_types.s.section = sectp;
11696 sections->info_or_types.size = bfd_section_size (sectp);
11697 }
11698 else if (section_is_p (sectp->name, &names->line_dwo))
11699 {
11700 /* There can be only one. */
11701 if (sections->line.s.section != NULL)
11702 return 0;
11703 sections->line.s.section = sectp;
11704 sections->line.size = bfd_section_size (sectp);
11705 }
11706 else if (section_is_p (sectp->name, &names->loc_dwo))
11707 {
11708 /* There can be only one. */
11709 if (sections->loc.s.section != NULL)
11710 return 0;
11711 sections->loc.s.section = sectp;
11712 sections->loc.size = bfd_section_size (sectp);
11713 }
11714 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11715 {
11716 /* There can be only one. */
11717 if (sections->macinfo.s.section != NULL)
11718 return 0;
11719 sections->macinfo.s.section = sectp;
11720 sections->macinfo.size = bfd_section_size (sectp);
11721 }
11722 else if (section_is_p (sectp->name, &names->macro_dwo))
11723 {
11724 /* There can be only one. */
11725 if (sections->macro.s.section != NULL)
11726 return 0;
11727 sections->macro.s.section = sectp;
11728 sections->macro.size = bfd_section_size (sectp);
11729 }
11730 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11731 {
11732 /* There can be only one. */
11733 if (sections->str_offsets.s.section != NULL)
11734 return 0;
11735 sections->str_offsets.s.section = sectp;
11736 sections->str_offsets.size = bfd_section_size (sectp);
11737 }
11738 else
11739 {
11740 /* No other kind of section is valid. */
11741 return 0;
11742 }
11743
11744 return 1;
11745}
11746
11747/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11748 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11749 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11750 This is for DWP version 1 files. */
11751
11752static struct dwo_unit *
11753create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11754 struct dwp_file *dwp_file,
11755 uint32_t unit_index,
11756 const char *comp_dir,
11757 ULONGEST signature, int is_debug_types)
11758{
11759 const struct dwp_hash_table *dwp_htab =
11760 is_debug_types ? dwp_file->tus : dwp_file->cus;
11761 bfd *dbfd = dwp_file->dbfd.get ();
11762 const char *kind = is_debug_types ? "TU" : "CU";
11763 struct dwo_file *dwo_file;
11764 struct dwo_unit *dwo_unit;
11765 struct virtual_v1_dwo_sections sections;
11766 void **dwo_file_slot;
11767 int i;
11768
11769 gdb_assert (dwp_file->version == 1);
11770
11771 if (dwarf_read_debug)
11772 {
11773 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11774 kind,
11775 pulongest (unit_index), hex_string (signature),
11776 dwp_file->name);
11777 }
11778
11779 /* Fetch the sections of this DWO unit.
11780 Put a limit on the number of sections we look for so that bad data
11781 doesn't cause us to loop forever. */
11782
11783#define MAX_NR_V1_DWO_SECTIONS \
11784 (1 /* .debug_info or .debug_types */ \
11785 + 1 /* .debug_abbrev */ \
11786 + 1 /* .debug_line */ \
11787 + 1 /* .debug_loc */ \
11788 + 1 /* .debug_str_offsets */ \
11789 + 1 /* .debug_macro or .debug_macinfo */ \
11790 + 1 /* trailing zero */)
11791
11792 memset (&sections, 0, sizeof (sections));
11793
11794 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11795 {
11796 asection *sectp;
11797 uint32_t section_nr =
11798 read_4_bytes (dbfd,
11799 dwp_htab->section_pool.v1.indices
11800 + (unit_index + i) * sizeof (uint32_t));
11801
11802 if (section_nr == 0)
11803 break;
11804 if (section_nr >= dwp_file->num_sections)
11805 {
11806 error (_("Dwarf Error: bad DWP hash table, section number too large"
11807 " [in module %s]"),
11808 dwp_file->name);
11809 }
11810
11811 sectp = dwp_file->elf_sections[section_nr];
11812 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11815 " [in module %s]"),
11816 dwp_file->name);
11817 }
11818 }
11819
11820 if (i < 2
11821 || sections.info_or_types.empty ()
11822 || sections.abbrev.empty ())
11823 {
11824 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11825 " [in module %s]"),
11826 dwp_file->name);
11827 }
11828 if (i == MAX_NR_V1_DWO_SECTIONS)
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11831 " [in module %s]"),
11832 dwp_file->name);
11833 }
11834
11835 /* It's easier for the rest of the code if we fake a struct dwo_file and
11836 have dwo_unit "live" in that. At least for now.
11837
11838 The DWP file can be made up of a random collection of CUs and TUs.
11839 However, for each CU + set of TUs that came from the same original DWO
11840 file, we can combine them back into a virtual DWO file to save space
11841 (fewer struct dwo_file objects to allocate). Remember that for really
11842 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11843
11844 std::string virtual_dwo_name =
11845 string_printf ("virtual-dwo/%d-%d-%d-%d",
11846 sections.abbrev.get_id (),
11847 sections.line.get_id (),
11848 sections.loc.get_id (),
11849 sections.str_offsets.get_id ());
11850 /* Can we use an existing virtual DWO file? */
11851 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11852 virtual_dwo_name.c_str (),
11853 comp_dir);
11854 /* Create one if necessary. */
11855 if (*dwo_file_slot == NULL)
11856 {
11857 if (dwarf_read_debug)
11858 {
11859 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11860 virtual_dwo_name.c_str ());
11861 }
11862 dwo_file = new struct dwo_file;
11863 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11864 dwo_file->comp_dir = comp_dir;
11865 dwo_file->sections.abbrev = sections.abbrev;
11866 dwo_file->sections.line = sections.line;
11867 dwo_file->sections.loc = sections.loc;
11868 dwo_file->sections.macinfo = sections.macinfo;
11869 dwo_file->sections.macro = sections.macro;
11870 dwo_file->sections.str_offsets = sections.str_offsets;
11871 /* The "str" section is global to the entire DWP file. */
11872 dwo_file->sections.str = dwp_file->sections.str;
11873 /* The info or types section is assigned below to dwo_unit,
11874 there's no need to record it in dwo_file.
11875 Also, we can't simply record type sections in dwo_file because
11876 we record a pointer into the vector in dwo_unit. As we collect more
11877 types we'll grow the vector and eventually have to reallocate space
11878 for it, invalidating all copies of pointers into the previous
11879 contents. */
11880 *dwo_file_slot = dwo_file;
11881 }
11882 else
11883 {
11884 if (dwarf_read_debug)
11885 {
11886 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11887 virtual_dwo_name.c_str ());
11888 }
11889 dwo_file = (struct dwo_file *) *dwo_file_slot;
11890 }
11891
11892 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11893 dwo_unit->dwo_file = dwo_file;
11894 dwo_unit->signature = signature;
11895 dwo_unit->section =
11896 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11897 *dwo_unit->section = sections.info_or_types;
11898 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11899
11900 return dwo_unit;
11901}
11902
11903/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11904 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11905 piece within that section used by a TU/CU, return a virtual section
11906 of just that piece. */
11907
11908static struct dwarf2_section_info
11909create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11910 struct dwarf2_section_info *section,
11911 bfd_size_type offset, bfd_size_type size)
11912{
11913 struct dwarf2_section_info result;
11914 asection *sectp;
11915
11916 gdb_assert (section != NULL);
11917 gdb_assert (!section->is_virtual);
11918
11919 memset (&result, 0, sizeof (result));
11920 result.s.containing_section = section;
11921 result.is_virtual = true;
11922
11923 if (size == 0)
11924 return result;
11925
11926 sectp = section->get_bfd_section ();
11927
11928 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11929 bounds of the real section. This is a pretty-rare event, so just
11930 flag an error (easier) instead of a warning and trying to cope. */
11931 if (sectp == NULL
11932 || offset + size > bfd_section_size (sectp))
11933 {
11934 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11935 " in section %s [in module %s]"),
11936 sectp ? bfd_section_name (sectp) : "<unknown>",
11937 objfile_name (dwarf2_per_objfile->objfile));
11938 }
11939
11940 result.virtual_offset = offset;
11941 result.size = size;
11942 return result;
11943}
11944
11945/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11946 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11947 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11948 This is for DWP version 2 files. */
11949
11950static struct dwo_unit *
11951create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11952 struct dwp_file *dwp_file,
11953 uint32_t unit_index,
11954 const char *comp_dir,
11955 ULONGEST signature, int is_debug_types)
11956{
11957 const struct dwp_hash_table *dwp_htab =
11958 is_debug_types ? dwp_file->tus : dwp_file->cus;
11959 bfd *dbfd = dwp_file->dbfd.get ();
11960 const char *kind = is_debug_types ? "TU" : "CU";
11961 struct dwo_file *dwo_file;
11962 struct dwo_unit *dwo_unit;
11963 struct virtual_v2_dwo_sections sections;
11964 void **dwo_file_slot;
11965 int i;
11966
11967 gdb_assert (dwp_file->version == 2);
11968
11969 if (dwarf_read_debug)
11970 {
11971 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11972 kind,
11973 pulongest (unit_index), hex_string (signature),
11974 dwp_file->name);
11975 }
11976
11977 /* Fetch the section offsets of this DWO unit. */
11978
11979 memset (&sections, 0, sizeof (sections));
11980
11981 for (i = 0; i < dwp_htab->nr_columns; ++i)
11982 {
11983 uint32_t offset = read_4_bytes (dbfd,
11984 dwp_htab->section_pool.v2.offsets
11985 + (((unit_index - 1) * dwp_htab->nr_columns
11986 + i)
11987 * sizeof (uint32_t)));
11988 uint32_t size = read_4_bytes (dbfd,
11989 dwp_htab->section_pool.v2.sizes
11990 + (((unit_index - 1) * dwp_htab->nr_columns
11991 + i)
11992 * sizeof (uint32_t)));
11993
11994 switch (dwp_htab->section_pool.v2.section_ids[i])
11995 {
11996 case DW_SECT_INFO:
11997 case DW_SECT_TYPES:
11998 sections.info_or_types_offset = offset;
11999 sections.info_or_types_size = size;
12000 break;
12001 case DW_SECT_ABBREV:
12002 sections.abbrev_offset = offset;
12003 sections.abbrev_size = size;
12004 break;
12005 case DW_SECT_LINE:
12006 sections.line_offset = offset;
12007 sections.line_size = size;
12008 break;
12009 case DW_SECT_LOC:
12010 sections.loc_offset = offset;
12011 sections.loc_size = size;
12012 break;
12013 case DW_SECT_STR_OFFSETS:
12014 sections.str_offsets_offset = offset;
12015 sections.str_offsets_size = size;
12016 break;
12017 case DW_SECT_MACINFO:
12018 sections.macinfo_offset = offset;
12019 sections.macinfo_size = size;
12020 break;
12021 case DW_SECT_MACRO:
12022 sections.macro_offset = offset;
12023 sections.macro_size = size;
12024 break;
12025 }
12026 }
12027
12028 /* It's easier for the rest of the code if we fake a struct dwo_file and
12029 have dwo_unit "live" in that. At least for now.
12030
12031 The DWP file can be made up of a random collection of CUs and TUs.
12032 However, for each CU + set of TUs that came from the same original DWO
12033 file, we can combine them back into a virtual DWO file to save space
12034 (fewer struct dwo_file objects to allocate). Remember that for really
12035 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12036
12037 std::string virtual_dwo_name =
12038 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12039 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12040 (long) (sections.line_size ? sections.line_offset : 0),
12041 (long) (sections.loc_size ? sections.loc_offset : 0),
12042 (long) (sections.str_offsets_size
12043 ? sections.str_offsets_offset : 0));
12044 /* Can we use an existing virtual DWO file? */
12045 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12046 virtual_dwo_name.c_str (),
12047 comp_dir);
12048 /* Create one if necessary. */
12049 if (*dwo_file_slot == NULL)
12050 {
12051 if (dwarf_read_debug)
12052 {
12053 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12054 virtual_dwo_name.c_str ());
12055 }
12056 dwo_file = new struct dwo_file;
12057 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12058 dwo_file->comp_dir = comp_dir;
12059 dwo_file->sections.abbrev =
12060 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12061 sections.abbrev_offset, sections.abbrev_size);
12062 dwo_file->sections.line =
12063 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12064 sections.line_offset, sections.line_size);
12065 dwo_file->sections.loc =
12066 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12067 sections.loc_offset, sections.loc_size);
12068 dwo_file->sections.macinfo =
12069 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12070 sections.macinfo_offset, sections.macinfo_size);
12071 dwo_file->sections.macro =
12072 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12073 sections.macro_offset, sections.macro_size);
12074 dwo_file->sections.str_offsets =
12075 create_dwp_v2_section (dwarf2_per_objfile,
12076 &dwp_file->sections.str_offsets,
12077 sections.str_offsets_offset,
12078 sections.str_offsets_size);
12079 /* The "str" section is global to the entire DWP file. */
12080 dwo_file->sections.str = dwp_file->sections.str;
12081 /* The info or types section is assigned below to dwo_unit,
12082 there's no need to record it in dwo_file.
12083 Also, we can't simply record type sections in dwo_file because
12084 we record a pointer into the vector in dwo_unit. As we collect more
12085 types we'll grow the vector and eventually have to reallocate space
12086 for it, invalidating all copies of pointers into the previous
12087 contents. */
12088 *dwo_file_slot = dwo_file;
12089 }
12090 else
12091 {
12092 if (dwarf_read_debug)
12093 {
12094 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12095 virtual_dwo_name.c_str ());
12096 }
12097 dwo_file = (struct dwo_file *) *dwo_file_slot;
12098 }
12099
12100 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12101 dwo_unit->dwo_file = dwo_file;
12102 dwo_unit->signature = signature;
12103 dwo_unit->section =
12104 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12105 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12106 is_debug_types
12107 ? &dwp_file->sections.types
12108 : &dwp_file->sections.info,
12109 sections.info_or_types_offset,
12110 sections.info_or_types_size);
12111 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12112
12113 return dwo_unit;
12114}
12115
12116/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12117 Returns NULL if the signature isn't found. */
12118
12119static struct dwo_unit *
12120lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12121 struct dwp_file *dwp_file, const char *comp_dir,
12122 ULONGEST signature, int is_debug_types)
12123{
12124 const struct dwp_hash_table *dwp_htab =
12125 is_debug_types ? dwp_file->tus : dwp_file->cus;
12126 bfd *dbfd = dwp_file->dbfd.get ();
12127 uint32_t mask = dwp_htab->nr_slots - 1;
12128 uint32_t hash = signature & mask;
12129 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12130 unsigned int i;
12131 void **slot;
12132 struct dwo_unit find_dwo_cu;
12133
12134 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12135 find_dwo_cu.signature = signature;
12136 slot = htab_find_slot (is_debug_types
12137 ? dwp_file->loaded_tus.get ()
12138 : dwp_file->loaded_cus.get (),
12139 &find_dwo_cu, INSERT);
12140
12141 if (*slot != NULL)
12142 return (struct dwo_unit *) *slot;
12143
12144 /* Use a for loop so that we don't loop forever on bad debug info. */
12145 for (i = 0; i < dwp_htab->nr_slots; ++i)
12146 {
12147 ULONGEST signature_in_table;
12148
12149 signature_in_table =
12150 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12151 if (signature_in_table == signature)
12152 {
12153 uint32_t unit_index =
12154 read_4_bytes (dbfd,
12155 dwp_htab->unit_table + hash * sizeof (uint32_t));
12156
12157 if (dwp_file->version == 1)
12158 {
12159 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12160 dwp_file, unit_index,
12161 comp_dir, signature,
12162 is_debug_types);
12163 }
12164 else
12165 {
12166 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12167 dwp_file, unit_index,
12168 comp_dir, signature,
12169 is_debug_types);
12170 }
12171 return (struct dwo_unit *) *slot;
12172 }
12173 if (signature_in_table == 0)
12174 return NULL;
12175 hash = (hash + hash2) & mask;
12176 }
12177
12178 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12179 " [in module %s]"),
12180 dwp_file->name);
12181}
12182
12183/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12184 Open the file specified by FILE_NAME and hand it off to BFD for
12185 preliminary analysis. Return a newly initialized bfd *, which
12186 includes a canonicalized copy of FILE_NAME.
12187 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12188 SEARCH_CWD is true if the current directory is to be searched.
12189 It will be searched before debug-file-directory.
12190 If successful, the file is added to the bfd include table of the
12191 objfile's bfd (see gdb_bfd_record_inclusion).
12192 If unable to find/open the file, return NULL.
12193 NOTE: This function is derived from symfile_bfd_open. */
12194
12195static gdb_bfd_ref_ptr
12196try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12197 const char *file_name, int is_dwp, int search_cwd)
12198{
12199 int desc;
12200 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12201 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12202 to debug_file_directory. */
12203 const char *search_path;
12204 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12205
12206 gdb::unique_xmalloc_ptr<char> search_path_holder;
12207 if (search_cwd)
12208 {
12209 if (*debug_file_directory != '\0')
12210 {
12211 search_path_holder.reset (concat (".", dirname_separator_string,
12212 debug_file_directory,
12213 (char *) NULL));
12214 search_path = search_path_holder.get ();
12215 }
12216 else
12217 search_path = ".";
12218 }
12219 else
12220 search_path = debug_file_directory;
12221
12222 openp_flags flags = OPF_RETURN_REALPATH;
12223 if (is_dwp)
12224 flags |= OPF_SEARCH_IN_PATH;
12225
12226 gdb::unique_xmalloc_ptr<char> absolute_name;
12227 desc = openp (search_path, flags, file_name,
12228 O_RDONLY | O_BINARY, &absolute_name);
12229 if (desc < 0)
12230 return NULL;
12231
12232 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12233 gnutarget, desc));
12234 if (sym_bfd == NULL)
12235 return NULL;
12236 bfd_set_cacheable (sym_bfd.get (), 1);
12237
12238 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12239 return NULL;
12240
12241 /* Success. Record the bfd as having been included by the objfile's bfd.
12242 This is important because things like demangled_names_hash lives in the
12243 objfile's per_bfd space and may have references to things like symbol
12244 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12245 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12246
12247 return sym_bfd;
12248}
12249
12250/* Try to open DWO file FILE_NAME.
12251 COMP_DIR is the DW_AT_comp_dir attribute.
12252 The result is the bfd handle of the file.
12253 If there is a problem finding or opening the file, return NULL.
12254 Upon success, the canonicalized path of the file is stored in the bfd,
12255 same as symfile_bfd_open. */
12256
12257static gdb_bfd_ref_ptr
12258open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12259 const char *file_name, const char *comp_dir)
12260{
12261 if (IS_ABSOLUTE_PATH (file_name))
12262 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12263 0 /*is_dwp*/, 0 /*search_cwd*/);
12264
12265 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12266
12267 if (comp_dir != NULL)
12268 {
12269 gdb::unique_xmalloc_ptr<char> path_to_try
12270 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12271
12272 /* NOTE: If comp_dir is a relative path, this will also try the
12273 search path, which seems useful. */
12274 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12275 path_to_try.get (),
12276 0 /*is_dwp*/,
12277 1 /*search_cwd*/));
12278 if (abfd != NULL)
12279 return abfd;
12280 }
12281
12282 /* That didn't work, try debug-file-directory, which, despite its name,
12283 is a list of paths. */
12284
12285 if (*debug_file_directory == '\0')
12286 return NULL;
12287
12288 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12289 0 /*is_dwp*/, 1 /*search_cwd*/);
12290}
12291
12292/* This function is mapped across the sections and remembers the offset and
12293 size of each of the DWO debugging sections we are interested in. */
12294
12295static void
12296dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12297{
12298 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12299 const struct dwop_section_names *names = &dwop_section_names;
12300
12301 if (section_is_p (sectp->name, &names->abbrev_dwo))
12302 {
12303 dwo_sections->abbrev.s.section = sectp;
12304 dwo_sections->abbrev.size = bfd_section_size (sectp);
12305 }
12306 else if (section_is_p (sectp->name, &names->info_dwo))
12307 {
12308 dwo_sections->info.s.section = sectp;
12309 dwo_sections->info.size = bfd_section_size (sectp);
12310 }
12311 else if (section_is_p (sectp->name, &names->line_dwo))
12312 {
12313 dwo_sections->line.s.section = sectp;
12314 dwo_sections->line.size = bfd_section_size (sectp);
12315 }
12316 else if (section_is_p (sectp->name, &names->loc_dwo))
12317 {
12318 dwo_sections->loc.s.section = sectp;
12319 dwo_sections->loc.size = bfd_section_size (sectp);
12320 }
12321 else if (section_is_p (sectp->name, &names->loclists_dwo))
12322 {
12323 dwo_sections->loclists.s.section = sectp;
12324 dwo_sections->loclists.size = bfd_section_size (sectp);
12325 }
12326 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12327 {
12328 dwo_sections->macinfo.s.section = sectp;
12329 dwo_sections->macinfo.size = bfd_section_size (sectp);
12330 }
12331 else if (section_is_p (sectp->name, &names->macro_dwo))
12332 {
12333 dwo_sections->macro.s.section = sectp;
12334 dwo_sections->macro.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->str_dwo))
12337 {
12338 dwo_sections->str.s.section = sectp;
12339 dwo_sections->str.size = bfd_section_size (sectp);
12340 }
12341 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12342 {
12343 dwo_sections->str_offsets.s.section = sectp;
12344 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->types_dwo))
12347 {
12348 struct dwarf2_section_info type_section;
12349
12350 memset (&type_section, 0, sizeof (type_section));
12351 type_section.s.section = sectp;
12352 type_section.size = bfd_section_size (sectp);
12353 dwo_sections->types.push_back (type_section);
12354 }
12355}
12356
12357/* Initialize the use of the DWO file specified by DWO_NAME and referenced
12358 by PER_CU. This is for the non-DWP case.
12359 The result is NULL if DWO_NAME can't be found. */
12360
12361static struct dwo_file *
12362open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12363 const char *dwo_name, const char *comp_dir)
12364{
12365 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12366
12367 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12368 if (dbfd == NULL)
12369 {
12370 if (dwarf_read_debug)
12371 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12372 return NULL;
12373 }
12374
12375 dwo_file_up dwo_file (new struct dwo_file);
12376 dwo_file->dwo_name = dwo_name;
12377 dwo_file->comp_dir = comp_dir;
12378 dwo_file->dbfd = std::move (dbfd);
12379
12380 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12381 &dwo_file->sections);
12382
12383 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12384 dwo_file->sections.info, dwo_file->cus);
12385
12386 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12387 dwo_file->sections.types, dwo_file->tus);
12388
12389 if (dwarf_read_debug)
12390 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12391
12392 return dwo_file.release ();
12393}
12394
12395/* This function is mapped across the sections and remembers the offset and
12396 size of each of the DWP debugging sections common to version 1 and 2 that
12397 we are interested in. */
12398
12399static void
12400dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12401 void *dwp_file_ptr)
12402{
12403 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12404 const struct dwop_section_names *names = &dwop_section_names;
12405 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12406
12407 /* Record the ELF section number for later lookup: this is what the
12408 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12409 gdb_assert (elf_section_nr < dwp_file->num_sections);
12410 dwp_file->elf_sections[elf_section_nr] = sectp;
12411
12412 /* Look for specific sections that we need. */
12413 if (section_is_p (sectp->name, &names->str_dwo))
12414 {
12415 dwp_file->sections.str.s.section = sectp;
12416 dwp_file->sections.str.size = bfd_section_size (sectp);
12417 }
12418 else if (section_is_p (sectp->name, &names->cu_index))
12419 {
12420 dwp_file->sections.cu_index.s.section = sectp;
12421 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12422 }
12423 else if (section_is_p (sectp->name, &names->tu_index))
12424 {
12425 dwp_file->sections.tu_index.s.section = sectp;
12426 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12427 }
12428}
12429
12430/* This function is mapped across the sections and remembers the offset and
12431 size of each of the DWP version 2 debugging sections that we are interested
12432 in. This is split into a separate function because we don't know if we
12433 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12434
12435static void
12436dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12437{
12438 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12439 const struct dwop_section_names *names = &dwop_section_names;
12440 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12441
12442 /* Record the ELF section number for later lookup: this is what the
12443 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12444 gdb_assert (elf_section_nr < dwp_file->num_sections);
12445 dwp_file->elf_sections[elf_section_nr] = sectp;
12446
12447 /* Look for specific sections that we need. */
12448 if (section_is_p (sectp->name, &names->abbrev_dwo))
12449 {
12450 dwp_file->sections.abbrev.s.section = sectp;
12451 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12452 }
12453 else if (section_is_p (sectp->name, &names->info_dwo))
12454 {
12455 dwp_file->sections.info.s.section = sectp;
12456 dwp_file->sections.info.size = bfd_section_size (sectp);
12457 }
12458 else if (section_is_p (sectp->name, &names->line_dwo))
12459 {
12460 dwp_file->sections.line.s.section = sectp;
12461 dwp_file->sections.line.size = bfd_section_size (sectp);
12462 }
12463 else if (section_is_p (sectp->name, &names->loc_dwo))
12464 {
12465 dwp_file->sections.loc.s.section = sectp;
12466 dwp_file->sections.loc.size = bfd_section_size (sectp);
12467 }
12468 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12469 {
12470 dwp_file->sections.macinfo.s.section = sectp;
12471 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12472 }
12473 else if (section_is_p (sectp->name, &names->macro_dwo))
12474 {
12475 dwp_file->sections.macro.s.section = sectp;
12476 dwp_file->sections.macro.size = bfd_section_size (sectp);
12477 }
12478 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12479 {
12480 dwp_file->sections.str_offsets.s.section = sectp;
12481 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12482 }
12483 else if (section_is_p (sectp->name, &names->types_dwo))
12484 {
12485 dwp_file->sections.types.s.section = sectp;
12486 dwp_file->sections.types.size = bfd_section_size (sectp);
12487 }
12488}
12489
12490/* Hash function for dwp_file loaded CUs/TUs. */
12491
12492static hashval_t
12493hash_dwp_loaded_cutus (const void *item)
12494{
12495 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12496
12497 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12498 return dwo_unit->signature;
12499}
12500
12501/* Equality function for dwp_file loaded CUs/TUs. */
12502
12503static int
12504eq_dwp_loaded_cutus (const void *a, const void *b)
12505{
12506 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12507 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12508
12509 return dua->signature == dub->signature;
12510}
12511
12512/* Allocate a hash table for dwp_file loaded CUs/TUs. */
12513
12514static htab_up
12515allocate_dwp_loaded_cutus_table ()
12516{
12517 return htab_up (htab_create_alloc (3,
12518 hash_dwp_loaded_cutus,
12519 eq_dwp_loaded_cutus,
12520 NULL, xcalloc, xfree));
12521}
12522
12523/* Try to open DWP file FILE_NAME.
12524 The result is the bfd handle of the file.
12525 If there is a problem finding or opening the file, return NULL.
12526 Upon success, the canonicalized path of the file is stored in the bfd,
12527 same as symfile_bfd_open. */
12528
12529static gdb_bfd_ref_ptr
12530open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12531 const char *file_name)
12532{
12533 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12534 1 /*is_dwp*/,
12535 1 /*search_cwd*/));
12536 if (abfd != NULL)
12537 return abfd;
12538
12539 /* Work around upstream bug 15652.
12540 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12541 [Whether that's a "bug" is debatable, but it is getting in our way.]
12542 We have no real idea where the dwp file is, because gdb's realpath-ing
12543 of the executable's path may have discarded the needed info.
12544 [IWBN if the dwp file name was recorded in the executable, akin to
12545 .gnu_debuglink, but that doesn't exist yet.]
12546 Strip the directory from FILE_NAME and search again. */
12547 if (*debug_file_directory != '\0')
12548 {
12549 /* Don't implicitly search the current directory here.
12550 If the user wants to search "." to handle this case,
12551 it must be added to debug-file-directory. */
12552 return try_open_dwop_file (dwarf2_per_objfile,
12553 lbasename (file_name), 1 /*is_dwp*/,
12554 0 /*search_cwd*/);
12555 }
12556
12557 return NULL;
12558}
12559
12560/* Initialize the use of the DWP file for the current objfile.
12561 By convention the name of the DWP file is ${objfile}.dwp.
12562 The result is NULL if it can't be found. */
12563
12564static std::unique_ptr<struct dwp_file>
12565open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12566{
12567 struct objfile *objfile = dwarf2_per_objfile->objfile;
12568
12569 /* Try to find first .dwp for the binary file before any symbolic links
12570 resolving. */
12571
12572 /* If the objfile is a debug file, find the name of the real binary
12573 file and get the name of dwp file from there. */
12574 std::string dwp_name;
12575 if (objfile->separate_debug_objfile_backlink != NULL)
12576 {
12577 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12578 const char *backlink_basename = lbasename (backlink->original_name);
12579
12580 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12581 }
12582 else
12583 dwp_name = objfile->original_name;
12584
12585 dwp_name += ".dwp";
12586
12587 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12588 if (dbfd == NULL
12589 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12590 {
12591 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12592 dwp_name = objfile_name (objfile);
12593 dwp_name += ".dwp";
12594 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12595 }
12596
12597 if (dbfd == NULL)
12598 {
12599 if (dwarf_read_debug)
12600 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12601 return std::unique_ptr<dwp_file> ();
12602 }
12603
12604 const char *name = bfd_get_filename (dbfd.get ());
12605 std::unique_ptr<struct dwp_file> dwp_file
12606 (new struct dwp_file (name, std::move (dbfd)));
12607
12608 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12609 dwp_file->elf_sections =
12610 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12611 dwp_file->num_sections, asection *);
12612
12613 bfd_map_over_sections (dwp_file->dbfd.get (),
12614 dwarf2_locate_common_dwp_sections,
12615 dwp_file.get ());
12616
12617 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12618 0);
12619
12620 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12621 1);
12622
12623 /* The DWP file version is stored in the hash table. Oh well. */
12624 if (dwp_file->cus && dwp_file->tus
12625 && dwp_file->cus->version != dwp_file->tus->version)
12626 {
12627 /* Technically speaking, we should try to limp along, but this is
12628 pretty bizarre. We use pulongest here because that's the established
12629 portability solution (e.g, we cannot use %u for uint32_t). */
12630 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12631 " TU version %s [in DWP file %s]"),
12632 pulongest (dwp_file->cus->version),
12633 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12634 }
12635
12636 if (dwp_file->cus)
12637 dwp_file->version = dwp_file->cus->version;
12638 else if (dwp_file->tus)
12639 dwp_file->version = dwp_file->tus->version;
12640 else
12641 dwp_file->version = 2;
12642
12643 if (dwp_file->version == 2)
12644 bfd_map_over_sections (dwp_file->dbfd.get (),
12645 dwarf2_locate_v2_dwp_sections,
12646 dwp_file.get ());
12647
12648 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12649 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12650
12651 if (dwarf_read_debug)
12652 {
12653 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12654 fprintf_unfiltered (gdb_stdlog,
12655 " %s CUs, %s TUs\n",
12656 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12657 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12658 }
12659
12660 return dwp_file;
12661}
12662
12663/* Wrapper around open_and_init_dwp_file, only open it once. */
12664
12665static struct dwp_file *
12666get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12667{
12668 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12669 {
12670 dwarf2_per_objfile->per_bfd->dwp_file
12671 = open_and_init_dwp_file (dwarf2_per_objfile);
12672 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12673 }
12674 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12675}
12676
12677/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12678 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12679 or in the DWP file for the objfile, referenced by THIS_UNIT.
12680 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12681 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12682
12683 This is called, for example, when wanting to read a variable with a
12684 complex location. Therefore we don't want to do file i/o for every call.
12685 Therefore we don't want to look for a DWO file on every call.
12686 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12687 then we check if we've already seen DWO_NAME, and only THEN do we check
12688 for a DWO file.
12689
12690 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12691 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12692
12693static struct dwo_unit *
12694lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12695 const char *dwo_name, const char *comp_dir,
12696 ULONGEST signature, int is_debug_types)
12697{
12698 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12699 struct objfile *objfile = dwarf2_per_objfile->objfile;
12700 const char *kind = is_debug_types ? "TU" : "CU";
12701 void **dwo_file_slot;
12702 struct dwo_file *dwo_file;
12703 struct dwp_file *dwp_file;
12704
12705 /* First see if there's a DWP file.
12706 If we have a DWP file but didn't find the DWO inside it, don't
12707 look for the original DWO file. It makes gdb behave differently
12708 depending on whether one is debugging in the build tree. */
12709
12710 dwp_file = get_dwp_file (dwarf2_per_objfile);
12711 if (dwp_file != NULL)
12712 {
12713 const struct dwp_hash_table *dwp_htab =
12714 is_debug_types ? dwp_file->tus : dwp_file->cus;
12715
12716 if (dwp_htab != NULL)
12717 {
12718 struct dwo_unit *dwo_cutu =
12719 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12720 signature, is_debug_types);
12721
12722 if (dwo_cutu != NULL)
12723 {
12724 if (dwarf_read_debug)
12725 {
12726 fprintf_unfiltered (gdb_stdlog,
12727 "Virtual DWO %s %s found: @%s\n",
12728 kind, hex_string (signature),
12729 host_address_to_string (dwo_cutu));
12730 }
12731 return dwo_cutu;
12732 }
12733 }
12734 }
12735 else
12736 {
12737 /* No DWP file, look for the DWO file. */
12738
12739 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12740 dwo_name, comp_dir);
12741 if (*dwo_file_slot == NULL)
12742 {
12743 /* Read in the file and build a table of the CUs/TUs it contains. */
12744 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12745 }
12746 /* NOTE: This will be NULL if unable to open the file. */
12747 dwo_file = (struct dwo_file *) *dwo_file_slot;
12748
12749 if (dwo_file != NULL)
12750 {
12751 struct dwo_unit *dwo_cutu = NULL;
12752
12753 if (is_debug_types && dwo_file->tus)
12754 {
12755 struct dwo_unit find_dwo_cutu;
12756
12757 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12758 find_dwo_cutu.signature = signature;
12759 dwo_cutu
12760 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12761 &find_dwo_cutu);
12762 }
12763 else if (!is_debug_types && dwo_file->cus)
12764 {
12765 struct dwo_unit find_dwo_cutu;
12766
12767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12768 find_dwo_cutu.signature = signature;
12769 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12770 &find_dwo_cutu);
12771 }
12772
12773 if (dwo_cutu != NULL)
12774 {
12775 if (dwarf_read_debug)
12776 {
12777 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12778 kind, dwo_name, hex_string (signature),
12779 host_address_to_string (dwo_cutu));
12780 }
12781 return dwo_cutu;
12782 }
12783 }
12784 }
12785
12786 /* We didn't find it. This could mean a dwo_id mismatch, or
12787 someone deleted the DWO/DWP file, or the search path isn't set up
12788 correctly to find the file. */
12789
12790 if (dwarf_read_debug)
12791 {
12792 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12793 kind, dwo_name, hex_string (signature));
12794 }
12795
12796 /* This is a warning and not a complaint because it can be caused by
12797 pilot error (e.g., user accidentally deleting the DWO). */
12798 {
12799 /* Print the name of the DWP file if we looked there, helps the user
12800 better diagnose the problem. */
12801 std::string dwp_text;
12802
12803 if (dwp_file != NULL)
12804 dwp_text = string_printf (" [in DWP file %s]",
12805 lbasename (dwp_file->name));
12806
12807 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12808 " [in module %s]"),
12809 kind, dwo_name, hex_string (signature),
12810 dwp_text.c_str (),
12811 this_unit->is_debug_types ? "TU" : "CU",
12812 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12813 }
12814 return NULL;
12815}
12816
12817/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12818 See lookup_dwo_cutu_unit for details. */
12819
12820static struct dwo_unit *
12821lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12822 const char *dwo_name, const char *comp_dir,
12823 ULONGEST signature)
12824{
12825 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12826}
12827
12828/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12829 See lookup_dwo_cutu_unit for details. */
12830
12831static struct dwo_unit *
12832lookup_dwo_type_unit (struct signatured_type *this_tu,
12833 const char *dwo_name, const char *comp_dir)
12834{
12835 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12836}
12837
12838/* Traversal function for queue_and_load_all_dwo_tus. */
12839
12840static int
12841queue_and_load_dwo_tu (void **slot, void *info)
12842{
12843 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12844 dwarf2_cu *cu = (dwarf2_cu *) info;
12845 ULONGEST signature = dwo_unit->signature;
12846 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12847
12848 if (sig_type != NULL)
12849 {
12850 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12851
12852 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12853 a real dependency of PER_CU on SIG_TYPE. That is detected later
12854 while processing PER_CU. */
12855 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12856 load_full_type_unit (sig_cu, cu->per_objfile);
12857 cu->per_cu->imported_symtabs_push (sig_cu);
12858 }
12859
12860 return 1;
12861}
12862
12863/* Queue all TUs contained in the DWO of PER_CU to be read in.
12864 The DWO may have the only definition of the type, though it may not be
12865 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12866 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12867
12868static void
12869queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12870{
12871 struct dwo_unit *dwo_unit;
12872 struct dwo_file *dwo_file;
12873
12874 gdb_assert (!per_cu->is_debug_types);
12875 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12876 gdb_assert (per_cu->cu != NULL);
12877
12878 dwo_unit = per_cu->cu->dwo_unit;
12879 gdb_assert (dwo_unit != NULL);
12880
12881 dwo_file = dwo_unit->dwo_file;
12882 if (dwo_file->tus != NULL)
12883 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12884 per_cu->cu);
12885}
12886
12887/* Read in various DIEs. */
12888
12889/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12890 Inherit only the children of the DW_AT_abstract_origin DIE not being
12891 already referenced by DW_AT_abstract_origin from the children of the
12892 current DIE. */
12893
12894static void
12895inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12896{
12897 struct die_info *child_die;
12898 sect_offset *offsetp;
12899 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12900 struct die_info *origin_die;
12901 /* Iterator of the ORIGIN_DIE children. */
12902 struct die_info *origin_child_die;
12903 struct attribute *attr;
12904 struct dwarf2_cu *origin_cu;
12905 struct pending **origin_previous_list_in_scope;
12906
12907 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12908 if (!attr)
12909 return;
12910
12911 /* Note that following die references may follow to a die in a
12912 different cu. */
12913
12914 origin_cu = cu;
12915 origin_die = follow_die_ref (die, attr, &origin_cu);
12916
12917 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12918 symbols in. */
12919 origin_previous_list_in_scope = origin_cu->list_in_scope;
12920 origin_cu->list_in_scope = cu->list_in_scope;
12921
12922 if (die->tag != origin_die->tag
12923 && !(die->tag == DW_TAG_inlined_subroutine
12924 && origin_die->tag == DW_TAG_subprogram))
12925 complaint (_("DIE %s and its abstract origin %s have different tags"),
12926 sect_offset_str (die->sect_off),
12927 sect_offset_str (origin_die->sect_off));
12928
12929 std::vector<sect_offset> offsets;
12930
12931 for (child_die = die->child;
12932 child_die && child_die->tag;
12933 child_die = child_die->sibling)
12934 {
12935 struct die_info *child_origin_die;
12936 struct dwarf2_cu *child_origin_cu;
12937
12938 /* We are trying to process concrete instance entries:
12939 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12940 it's not relevant to our analysis here. i.e. detecting DIEs that are
12941 present in the abstract instance but not referenced in the concrete
12942 one. */
12943 if (child_die->tag == DW_TAG_call_site
12944 || child_die->tag == DW_TAG_GNU_call_site)
12945 continue;
12946
12947 /* For each CHILD_DIE, find the corresponding child of
12948 ORIGIN_DIE. If there is more than one layer of
12949 DW_AT_abstract_origin, follow them all; there shouldn't be,
12950 but GCC versions at least through 4.4 generate this (GCC PR
12951 40573). */
12952 child_origin_die = child_die;
12953 child_origin_cu = cu;
12954 while (1)
12955 {
12956 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12957 child_origin_cu);
12958 if (attr == NULL)
12959 break;
12960 child_origin_die = follow_die_ref (child_origin_die, attr,
12961 &child_origin_cu);
12962 }
12963
12964 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12965 counterpart may exist. */
12966 if (child_origin_die != child_die)
12967 {
12968 if (child_die->tag != child_origin_die->tag
12969 && !(child_die->tag == DW_TAG_inlined_subroutine
12970 && child_origin_die->tag == DW_TAG_subprogram))
12971 complaint (_("Child DIE %s and its abstract origin %s have "
12972 "different tags"),
12973 sect_offset_str (child_die->sect_off),
12974 sect_offset_str (child_origin_die->sect_off));
12975 if (child_origin_die->parent != origin_die)
12976 complaint (_("Child DIE %s and its abstract origin %s have "
12977 "different parents"),
12978 sect_offset_str (child_die->sect_off),
12979 sect_offset_str (child_origin_die->sect_off));
12980 else
12981 offsets.push_back (child_origin_die->sect_off);
12982 }
12983 }
12984 std::sort (offsets.begin (), offsets.end ());
12985 sect_offset *offsets_end = offsets.data () + offsets.size ();
12986 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12987 if (offsetp[-1] == *offsetp)
12988 complaint (_("Multiple children of DIE %s refer "
12989 "to DIE %s as their abstract origin"),
12990 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12991
12992 offsetp = offsets.data ();
12993 origin_child_die = origin_die->child;
12994 while (origin_child_die && origin_child_die->tag)
12995 {
12996 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12997 while (offsetp < offsets_end
12998 && *offsetp < origin_child_die->sect_off)
12999 offsetp++;
13000 if (offsetp >= offsets_end
13001 || *offsetp > origin_child_die->sect_off)
13002 {
13003 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13004 Check whether we're already processing ORIGIN_CHILD_DIE.
13005 This can happen with mutually referenced abstract_origins.
13006 PR 16581. */
13007 if (!origin_child_die->in_process)
13008 process_die (origin_child_die, origin_cu);
13009 }
13010 origin_child_die = origin_child_die->sibling;
13011 }
13012 origin_cu->list_in_scope = origin_previous_list_in_scope;
13013
13014 if (cu != origin_cu)
13015 compute_delayed_physnames (origin_cu);
13016}
13017
13018static void
13019read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13020{
13021 struct objfile *objfile = cu->per_objfile->objfile;
13022 struct gdbarch *gdbarch = objfile->arch ();
13023 struct context_stack *newobj;
13024 CORE_ADDR lowpc;
13025 CORE_ADDR highpc;
13026 struct die_info *child_die;
13027 struct attribute *attr, *call_line, *call_file;
13028 const char *name;
13029 CORE_ADDR baseaddr;
13030 struct block *block;
13031 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13032 std::vector<struct symbol *> template_args;
13033 struct template_symbol *templ_func = NULL;
13034
13035 if (inlined_func)
13036 {
13037 /* If we do not have call site information, we can't show the
13038 caller of this inlined function. That's too confusing, so
13039 only use the scope for local variables. */
13040 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13041 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13042 if (call_line == NULL || call_file == NULL)
13043 {
13044 read_lexical_block_scope (die, cu);
13045 return;
13046 }
13047 }
13048
13049 baseaddr = objfile->text_section_offset ();
13050
13051 name = dwarf2_name (die, cu);
13052
13053 /* Ignore functions with missing or empty names. These are actually
13054 illegal according to the DWARF standard. */
13055 if (name == NULL)
13056 {
13057 complaint (_("missing name for subprogram DIE at %s"),
13058 sect_offset_str (die->sect_off));
13059 return;
13060 }
13061
13062 /* Ignore functions with missing or invalid low and high pc attributes. */
13063 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13064 <= PC_BOUNDS_INVALID)
13065 {
13066 attr = dwarf2_attr (die, DW_AT_external, cu);
13067 if (!attr || !DW_UNSND (attr))
13068 complaint (_("cannot get low and high bounds "
13069 "for subprogram DIE at %s"),
13070 sect_offset_str (die->sect_off));
13071 return;
13072 }
13073
13074 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13075 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13076
13077 /* If we have any template arguments, then we must allocate a
13078 different sort of symbol. */
13079 for (child_die = die->child; child_die; child_die = child_die->sibling)
13080 {
13081 if (child_die->tag == DW_TAG_template_type_param
13082 || child_die->tag == DW_TAG_template_value_param)
13083 {
13084 templ_func = new (&objfile->objfile_obstack) template_symbol;
13085 templ_func->subclass = SYMBOL_TEMPLATE;
13086 break;
13087 }
13088 }
13089
13090 newobj = cu->get_builder ()->push_context (0, lowpc);
13091 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13092 (struct symbol *) templ_func);
13093
13094 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13095 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13096 cu->language);
13097
13098 /* If there is a location expression for DW_AT_frame_base, record
13099 it. */
13100 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13101 if (attr != nullptr)
13102 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13103
13104 /* If there is a location for the static link, record it. */
13105 newobj->static_link = NULL;
13106 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13107 if (attr != nullptr)
13108 {
13109 newobj->static_link
13110 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13111 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13112 cu->per_cu->addr_type ());
13113 }
13114
13115 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13116
13117 if (die->child != NULL)
13118 {
13119 child_die = die->child;
13120 while (child_die && child_die->tag)
13121 {
13122 if (child_die->tag == DW_TAG_template_type_param
13123 || child_die->tag == DW_TAG_template_value_param)
13124 {
13125 struct symbol *arg = new_symbol (child_die, NULL, cu);
13126
13127 if (arg != NULL)
13128 template_args.push_back (arg);
13129 }
13130 else
13131 process_die (child_die, cu);
13132 child_die = child_die->sibling;
13133 }
13134 }
13135
13136 inherit_abstract_dies (die, cu);
13137
13138 /* If we have a DW_AT_specification, we might need to import using
13139 directives from the context of the specification DIE. See the
13140 comment in determine_prefix. */
13141 if (cu->language == language_cplus
13142 && dwarf2_attr (die, DW_AT_specification, cu))
13143 {
13144 struct dwarf2_cu *spec_cu = cu;
13145 struct die_info *spec_die = die_specification (die, &spec_cu);
13146
13147 while (spec_die)
13148 {
13149 child_die = spec_die->child;
13150 while (child_die && child_die->tag)
13151 {
13152 if (child_die->tag == DW_TAG_imported_module)
13153 process_die (child_die, spec_cu);
13154 child_die = child_die->sibling;
13155 }
13156
13157 /* In some cases, GCC generates specification DIEs that
13158 themselves contain DW_AT_specification attributes. */
13159 spec_die = die_specification (spec_die, &spec_cu);
13160 }
13161 }
13162
13163 struct context_stack cstk = cu->get_builder ()->pop_context ();
13164 /* Make a block for the local symbols within. */
13165 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13166 cstk.static_link, lowpc, highpc);
13167
13168 /* For C++, set the block's scope. */
13169 if ((cu->language == language_cplus
13170 || cu->language == language_fortran
13171 || cu->language == language_d
13172 || cu->language == language_rust)
13173 && cu->processing_has_namespace_info)
13174 block_set_scope (block, determine_prefix (die, cu),
13175 &objfile->objfile_obstack);
13176
13177 /* If we have address ranges, record them. */
13178 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13179
13180 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13181
13182 /* Attach template arguments to function. */
13183 if (!template_args.empty ())
13184 {
13185 gdb_assert (templ_func != NULL);
13186
13187 templ_func->n_template_arguments = template_args.size ();
13188 templ_func->template_arguments
13189 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13190 templ_func->n_template_arguments);
13191 memcpy (templ_func->template_arguments,
13192 template_args.data (),
13193 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13194
13195 /* Make sure that the symtab is set on the new symbols. Even
13196 though they don't appear in this symtab directly, other parts
13197 of gdb assume that symbols do, and this is reasonably
13198 true. */
13199 for (symbol *sym : template_args)
13200 symbol_set_symtab (sym, symbol_symtab (templ_func));
13201 }
13202
13203 /* In C++, we can have functions nested inside functions (e.g., when
13204 a function declares a class that has methods). This means that
13205 when we finish processing a function scope, we may need to go
13206 back to building a containing block's symbol lists. */
13207 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13208 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13209
13210 /* If we've finished processing a top-level function, subsequent
13211 symbols go in the file symbol list. */
13212 if (cu->get_builder ()->outermost_context_p ())
13213 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13214}
13215
13216/* Process all the DIES contained within a lexical block scope. Start
13217 a new scope, process the dies, and then close the scope. */
13218
13219static void
13220read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13221{
13222 struct objfile *objfile = cu->per_objfile->objfile;
13223 struct gdbarch *gdbarch = objfile->arch ();
13224 CORE_ADDR lowpc, highpc;
13225 struct die_info *child_die;
13226 CORE_ADDR baseaddr;
13227
13228 baseaddr = objfile->text_section_offset ();
13229
13230 /* Ignore blocks with missing or invalid low and high pc attributes. */
13231 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13232 as multiple lexical blocks? Handling children in a sane way would
13233 be nasty. Might be easier to properly extend generic blocks to
13234 describe ranges. */
13235 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13236 {
13237 case PC_BOUNDS_NOT_PRESENT:
13238 /* DW_TAG_lexical_block has no attributes, process its children as if
13239 there was no wrapping by that DW_TAG_lexical_block.
13240 GCC does no longer produces such DWARF since GCC r224161. */
13241 for (child_die = die->child;
13242 child_die != NULL && child_die->tag;
13243 child_die = child_die->sibling)
13244 {
13245 /* We might already be processing this DIE. This can happen
13246 in an unusual circumstance -- where a subroutine A
13247 appears lexically in another subroutine B, but A actually
13248 inlines B. The recursion is broken here, rather than in
13249 inherit_abstract_dies, because it seems better to simply
13250 drop concrete children here. */
13251 if (!child_die->in_process)
13252 process_die (child_die, cu);
13253 }
13254 return;
13255 case PC_BOUNDS_INVALID:
13256 return;
13257 }
13258 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13259 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13260
13261 cu->get_builder ()->push_context (0, lowpc);
13262 if (die->child != NULL)
13263 {
13264 child_die = die->child;
13265 while (child_die && child_die->tag)
13266 {
13267 process_die (child_die, cu);
13268 child_die = child_die->sibling;
13269 }
13270 }
13271 inherit_abstract_dies (die, cu);
13272 struct context_stack cstk = cu->get_builder ()->pop_context ();
13273
13274 if (*cu->get_builder ()->get_local_symbols () != NULL
13275 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13276 {
13277 struct block *block
13278 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13279 cstk.start_addr, highpc);
13280
13281 /* Note that recording ranges after traversing children, as we
13282 do here, means that recording a parent's ranges entails
13283 walking across all its children's ranges as they appear in
13284 the address map, which is quadratic behavior.
13285
13286 It would be nicer to record the parent's ranges before
13287 traversing its children, simply overriding whatever you find
13288 there. But since we don't even decide whether to create a
13289 block until after we've traversed its children, that's hard
13290 to do. */
13291 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13292 }
13293 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13294 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13295}
13296
13297/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13298
13299static void
13300read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13301{
13302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13303 struct objfile *objfile = per_objfile->objfile;
13304 struct gdbarch *gdbarch = objfile->arch ();
13305 CORE_ADDR pc, baseaddr;
13306 struct attribute *attr;
13307 struct call_site *call_site, call_site_local;
13308 void **slot;
13309 int nparams;
13310 struct die_info *child_die;
13311
13312 baseaddr = objfile->text_section_offset ();
13313
13314 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13315 if (attr == NULL)
13316 {
13317 /* This was a pre-DWARF-5 GNU extension alias
13318 for DW_AT_call_return_pc. */
13319 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13320 }
13321 if (!attr)
13322 {
13323 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13324 "DIE %s [in module %s]"),
13325 sect_offset_str (die->sect_off), objfile_name (objfile));
13326 return;
13327 }
13328 pc = attr->value_as_address () + baseaddr;
13329 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13330
13331 if (cu->call_site_htab == NULL)
13332 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13333 NULL, &objfile->objfile_obstack,
13334 hashtab_obstack_allocate, NULL);
13335 call_site_local.pc = pc;
13336 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13337 if (*slot != NULL)
13338 {
13339 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13340 "DIE %s [in module %s]"),
13341 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13342 objfile_name (objfile));
13343 return;
13344 }
13345
13346 /* Count parameters at the caller. */
13347
13348 nparams = 0;
13349 for (child_die = die->child; child_die && child_die->tag;
13350 child_die = child_die->sibling)
13351 {
13352 if (child_die->tag != DW_TAG_call_site_parameter
13353 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13354 {
13355 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13356 "DW_TAG_call_site child DIE %s [in module %s]"),
13357 child_die->tag, sect_offset_str (child_die->sect_off),
13358 objfile_name (objfile));
13359 continue;
13360 }
13361
13362 nparams++;
13363 }
13364
13365 call_site
13366 = ((struct call_site *)
13367 obstack_alloc (&objfile->objfile_obstack,
13368 sizeof (*call_site)
13369 + (sizeof (*call_site->parameter) * (nparams - 1))));
13370 *slot = call_site;
13371 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13372 call_site->pc = pc;
13373
13374 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13375 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13376 {
13377 struct die_info *func_die;
13378
13379 /* Skip also over DW_TAG_inlined_subroutine. */
13380 for (func_die = die->parent;
13381 func_die && func_die->tag != DW_TAG_subprogram
13382 && func_die->tag != DW_TAG_subroutine_type;
13383 func_die = func_die->parent);
13384
13385 /* DW_AT_call_all_calls is a superset
13386 of DW_AT_call_all_tail_calls. */
13387 if (func_die
13388 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13389 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13390 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13391 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13392 {
13393 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13394 not complete. But keep CALL_SITE for look ups via call_site_htab,
13395 both the initial caller containing the real return address PC and
13396 the final callee containing the current PC of a chain of tail
13397 calls do not need to have the tail call list complete. But any
13398 function candidate for a virtual tail call frame searched via
13399 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13400 determined unambiguously. */
13401 }
13402 else
13403 {
13404 struct type *func_type = NULL;
13405
13406 if (func_die)
13407 func_type = get_die_type (func_die, cu);
13408 if (func_type != NULL)
13409 {
13410 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13411
13412 /* Enlist this call site to the function. */
13413 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13414 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13415 }
13416 else
13417 complaint (_("Cannot find function owning DW_TAG_call_site "
13418 "DIE %s [in module %s]"),
13419 sect_offset_str (die->sect_off), objfile_name (objfile));
13420 }
13421 }
13422
13423 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13424 if (attr == NULL)
13425 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13426 if (attr == NULL)
13427 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13428 if (attr == NULL)
13429 {
13430 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13431 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13432 }
13433 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13434 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13435 /* Keep NULL DWARF_BLOCK. */;
13436 else if (attr->form_is_block ())
13437 {
13438 struct dwarf2_locexpr_baton *dlbaton;
13439
13440 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13441 dlbaton->data = DW_BLOCK (attr)->data;
13442 dlbaton->size = DW_BLOCK (attr)->size;
13443 dlbaton->per_objfile = per_objfile;
13444 dlbaton->per_cu = cu->per_cu;
13445
13446 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13447 }
13448 else if (attr->form_is_ref ())
13449 {
13450 struct dwarf2_cu *target_cu = cu;
13451 struct die_info *target_die;
13452
13453 target_die = follow_die_ref (die, attr, &target_cu);
13454 gdb_assert (target_cu->per_objfile->objfile == objfile);
13455 if (die_is_declaration (target_die, target_cu))
13456 {
13457 const char *target_physname;
13458
13459 /* Prefer the mangled name; otherwise compute the demangled one. */
13460 target_physname = dw2_linkage_name (target_die, target_cu);
13461 if (target_physname == NULL)
13462 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13463 if (target_physname == NULL)
13464 complaint (_("DW_AT_call_target target DIE has invalid "
13465 "physname, for referencing DIE %s [in module %s]"),
13466 sect_offset_str (die->sect_off), objfile_name (objfile));
13467 else
13468 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13469 }
13470 else
13471 {
13472 CORE_ADDR lowpc;
13473
13474 /* DW_AT_entry_pc should be preferred. */
13475 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13476 <= PC_BOUNDS_INVALID)
13477 complaint (_("DW_AT_call_target target DIE has invalid "
13478 "low pc, for referencing DIE %s [in module %s]"),
13479 sect_offset_str (die->sect_off), objfile_name (objfile));
13480 else
13481 {
13482 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13483 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13484 }
13485 }
13486 }
13487 else
13488 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13489 "block nor reference, for DIE %s [in module %s]"),
13490 sect_offset_str (die->sect_off), objfile_name (objfile));
13491
13492 call_site->per_cu = cu->per_cu;
13493
13494 for (child_die = die->child;
13495 child_die && child_die->tag;
13496 child_die = child_die->sibling)
13497 {
13498 struct call_site_parameter *parameter;
13499 struct attribute *loc, *origin;
13500
13501 if (child_die->tag != DW_TAG_call_site_parameter
13502 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13503 {
13504 /* Already printed the complaint above. */
13505 continue;
13506 }
13507
13508 gdb_assert (call_site->parameter_count < nparams);
13509 parameter = &call_site->parameter[call_site->parameter_count];
13510
13511 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13512 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13513 register is contained in DW_AT_call_value. */
13514
13515 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13516 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13517 if (origin == NULL)
13518 {
13519 /* This was a pre-DWARF-5 GNU extension alias
13520 for DW_AT_call_parameter. */
13521 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13522 }
13523 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13524 {
13525 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13526
13527 sect_offset sect_off = origin->get_ref_die_offset ();
13528 if (!cu->header.offset_in_cu_p (sect_off))
13529 {
13530 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13531 binding can be done only inside one CU. Such referenced DIE
13532 therefore cannot be even moved to DW_TAG_partial_unit. */
13533 complaint (_("DW_AT_call_parameter offset is not in CU for "
13534 "DW_TAG_call_site child DIE %s [in module %s]"),
13535 sect_offset_str (child_die->sect_off),
13536 objfile_name (objfile));
13537 continue;
13538 }
13539 parameter->u.param_cu_off
13540 = (cu_offset) (sect_off - cu->header.sect_off);
13541 }
13542 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13543 {
13544 complaint (_("No DW_FORM_block* DW_AT_location for "
13545 "DW_TAG_call_site child DIE %s [in module %s]"),
13546 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13547 continue;
13548 }
13549 else
13550 {
13551 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13552 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13553 if (parameter->u.dwarf_reg != -1)
13554 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13555 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13556 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13557 &parameter->u.fb_offset))
13558 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13559 else
13560 {
13561 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13562 "for DW_FORM_block* DW_AT_location is supported for "
13563 "DW_TAG_call_site child DIE %s "
13564 "[in module %s]"),
13565 sect_offset_str (child_die->sect_off),
13566 objfile_name (objfile));
13567 continue;
13568 }
13569 }
13570
13571 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13572 if (attr == NULL)
13573 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13574 if (attr == NULL || !attr->form_is_block ())
13575 {
13576 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13577 "DW_TAG_call_site child DIE %s [in module %s]"),
13578 sect_offset_str (child_die->sect_off),
13579 objfile_name (objfile));
13580 continue;
13581 }
13582 parameter->value = DW_BLOCK (attr)->data;
13583 parameter->value_size = DW_BLOCK (attr)->size;
13584
13585 /* Parameters are not pre-cleared by memset above. */
13586 parameter->data_value = NULL;
13587 parameter->data_value_size = 0;
13588 call_site->parameter_count++;
13589
13590 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13591 if (attr == NULL)
13592 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13593 if (attr != nullptr)
13594 {
13595 if (!attr->form_is_block ())
13596 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13597 "DW_TAG_call_site child DIE %s [in module %s]"),
13598 sect_offset_str (child_die->sect_off),
13599 objfile_name (objfile));
13600 else
13601 {
13602 parameter->data_value = DW_BLOCK (attr)->data;
13603 parameter->data_value_size = DW_BLOCK (attr)->size;
13604 }
13605 }
13606 }
13607}
13608
13609/* Helper function for read_variable. If DIE represents a virtual
13610 table, then return the type of the concrete object that is
13611 associated with the virtual table. Otherwise, return NULL. */
13612
13613static struct type *
13614rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13615{
13616 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13617 if (attr == NULL)
13618 return NULL;
13619
13620 /* Find the type DIE. */
13621 struct die_info *type_die = NULL;
13622 struct dwarf2_cu *type_cu = cu;
13623
13624 if (attr->form_is_ref ())
13625 type_die = follow_die_ref (die, attr, &type_cu);
13626 if (type_die == NULL)
13627 return NULL;
13628
13629 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13630 return NULL;
13631 return die_containing_type (type_die, type_cu);
13632}
13633
13634/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13635
13636static void
13637read_variable (struct die_info *die, struct dwarf2_cu *cu)
13638{
13639 struct rust_vtable_symbol *storage = NULL;
13640
13641 if (cu->language == language_rust)
13642 {
13643 struct type *containing_type = rust_containing_type (die, cu);
13644
13645 if (containing_type != NULL)
13646 {
13647 struct objfile *objfile = cu->per_objfile->objfile;
13648
13649 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13650 storage->concrete_type = containing_type;
13651 storage->subclass = SYMBOL_RUST_VTABLE;
13652 }
13653 }
13654
13655 struct symbol *res = new_symbol (die, NULL, cu, storage);
13656 struct attribute *abstract_origin
13657 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13658 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13659 if (res == NULL && loc && abstract_origin)
13660 {
13661 /* We have a variable without a name, but with a location and an abstract
13662 origin. This may be a concrete instance of an abstract variable
13663 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13664 later. */
13665 struct dwarf2_cu *origin_cu = cu;
13666 struct die_info *origin_die
13667 = follow_die_ref (die, abstract_origin, &origin_cu);
13668 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13669 per_objfile->per_bfd->abstract_to_concrete
13670 [origin_die->sect_off].push_back (die->sect_off);
13671 }
13672}
13673
13674/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13675 reading .debug_rnglists.
13676 Callback's type should be:
13677 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13678 Return true if the attributes are present and valid, otherwise,
13679 return false. */
13680
13681template <typename Callback>
13682static bool
13683dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13684 Callback &&callback)
13685{
13686 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13687 struct objfile *objfile = dwarf2_per_objfile->objfile;
13688 bfd *obfd = objfile->obfd;
13689 /* Base address selection entry. */
13690 gdb::optional<CORE_ADDR> base;
13691 const gdb_byte *buffer;
13692 CORE_ADDR baseaddr;
13693 bool overflow = false;
13694
13695 base = cu->base_address;
13696
13697 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13698 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13699 {
13700 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13701 offset);
13702 return false;
13703 }
13704 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13705
13706 baseaddr = objfile->text_section_offset ();
13707
13708 while (1)
13709 {
13710 /* Initialize it due to a false compiler warning. */
13711 CORE_ADDR range_beginning = 0, range_end = 0;
13712 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13713 + dwarf2_per_objfile->per_bfd->rnglists.size);
13714 unsigned int bytes_read;
13715
13716 if (buffer == buf_end)
13717 {
13718 overflow = true;
13719 break;
13720 }
13721 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13722 switch (rlet)
13723 {
13724 case DW_RLE_end_of_list:
13725 break;
13726 case DW_RLE_base_address:
13727 if (buffer + cu->header.addr_size > buf_end)
13728 {
13729 overflow = true;
13730 break;
13731 }
13732 base = cu->header.read_address (obfd, buffer, &bytes_read);
13733 buffer += bytes_read;
13734 break;
13735 case DW_RLE_start_length:
13736 if (buffer + cu->header.addr_size > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 range_beginning = cu->header.read_address (obfd, buffer,
13742 &bytes_read);
13743 buffer += bytes_read;
13744 range_end = (range_beginning
13745 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13746 buffer += bytes_read;
13747 if (buffer > buf_end)
13748 {
13749 overflow = true;
13750 break;
13751 }
13752 break;
13753 case DW_RLE_offset_pair:
13754 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13755 buffer += bytes_read;
13756 if (buffer > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 if (buffer > buf_end)
13764 {
13765 overflow = true;
13766 break;
13767 }
13768 break;
13769 case DW_RLE_start_end:
13770 if (buffer + 2 * cu->header.addr_size > buf_end)
13771 {
13772 overflow = true;
13773 break;
13774 }
13775 range_beginning = cu->header.read_address (obfd, buffer,
13776 &bytes_read);
13777 buffer += bytes_read;
13778 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13779 buffer += bytes_read;
13780 break;
13781 default:
13782 complaint (_("Invalid .debug_rnglists data (no base address)"));
13783 return false;
13784 }
13785 if (rlet == DW_RLE_end_of_list || overflow)
13786 break;
13787 if (rlet == DW_RLE_base_address)
13788 continue;
13789
13790 if (!base.has_value ())
13791 {
13792 /* We have no valid base address for the ranges
13793 data. */
13794 complaint (_("Invalid .debug_rnglists data (no base address)"));
13795 return false;
13796 }
13797
13798 if (range_beginning > range_end)
13799 {
13800 /* Inverted range entries are invalid. */
13801 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13802 return false;
13803 }
13804
13805 /* Empty range entries have no effect. */
13806 if (range_beginning == range_end)
13807 continue;
13808
13809 range_beginning += *base;
13810 range_end += *base;
13811
13812 /* A not-uncommon case of bad debug info.
13813 Don't pollute the addrmap with bad data. */
13814 if (range_beginning + baseaddr == 0
13815 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13816 {
13817 complaint (_(".debug_rnglists entry has start address of zero"
13818 " [in module %s]"), objfile_name (objfile));
13819 continue;
13820 }
13821
13822 callback (range_beginning, range_end);
13823 }
13824
13825 if (overflow)
13826 {
13827 complaint (_("Offset %d is not terminated "
13828 "for DW_AT_ranges attribute"),
13829 offset);
13830 return false;
13831 }
13832
13833 return true;
13834}
13835
13836/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13837 Callback's type should be:
13838 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13839 Return 1 if the attributes are present and valid, otherwise, return 0. */
13840
13841template <typename Callback>
13842static int
13843dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13844 Callback &&callback)
13845{
13846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13847 struct objfile *objfile = per_objfile->objfile;
13848 struct comp_unit_head *cu_header = &cu->header;
13849 bfd *obfd = objfile->obfd;
13850 unsigned int addr_size = cu_header->addr_size;
13851 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13852 /* Base address selection entry. */
13853 gdb::optional<CORE_ADDR> base;
13854 unsigned int dummy;
13855 const gdb_byte *buffer;
13856 CORE_ADDR baseaddr;
13857
13858 if (cu_header->version >= 5)
13859 return dwarf2_rnglists_process (offset, cu, callback);
13860
13861 base = cu->base_address;
13862
13863 per_objfile->per_bfd->ranges.read (objfile);
13864 if (offset >= per_objfile->per_bfd->ranges.size)
13865 {
13866 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13867 offset);
13868 return 0;
13869 }
13870 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13871
13872 baseaddr = objfile->text_section_offset ();
13873
13874 while (1)
13875 {
13876 CORE_ADDR range_beginning, range_end;
13877
13878 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13879 buffer += addr_size;
13880 range_end = cu->header.read_address (obfd, buffer, &dummy);
13881 buffer += addr_size;
13882 offset += 2 * addr_size;
13883
13884 /* An end of list marker is a pair of zero addresses. */
13885 if (range_beginning == 0 && range_end == 0)
13886 /* Found the end of list entry. */
13887 break;
13888
13889 /* Each base address selection entry is a pair of 2 values.
13890 The first is the largest possible address, the second is
13891 the base address. Check for a base address here. */
13892 if ((range_beginning & mask) == mask)
13893 {
13894 /* If we found the largest possible address, then we already
13895 have the base address in range_end. */
13896 base = range_end;
13897 continue;
13898 }
13899
13900 if (!base.has_value ())
13901 {
13902 /* We have no valid base address for the ranges
13903 data. */
13904 complaint (_("Invalid .debug_ranges data (no base address)"));
13905 return 0;
13906 }
13907
13908 if (range_beginning > range_end)
13909 {
13910 /* Inverted range entries are invalid. */
13911 complaint (_("Invalid .debug_ranges data (inverted range)"));
13912 return 0;
13913 }
13914
13915 /* Empty range entries have no effect. */
13916 if (range_beginning == range_end)
13917 continue;
13918
13919 range_beginning += *base;
13920 range_end += *base;
13921
13922 /* A not-uncommon case of bad debug info.
13923 Don't pollute the addrmap with bad data. */
13924 if (range_beginning + baseaddr == 0
13925 && !per_objfile->per_bfd->has_section_at_zero)
13926 {
13927 complaint (_(".debug_ranges entry has start address of zero"
13928 " [in module %s]"), objfile_name (objfile));
13929 continue;
13930 }
13931
13932 callback (range_beginning, range_end);
13933 }
13934
13935 return 1;
13936}
13937
13938/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13939 Return 1 if the attributes are present and valid, otherwise, return 0.
13940 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13941
13942static int
13943dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13944 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13945 dwarf2_psymtab *ranges_pst)
13946{
13947 struct objfile *objfile = cu->per_objfile->objfile;
13948 struct gdbarch *gdbarch = objfile->arch ();
13949 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13950 int low_set = 0;
13951 CORE_ADDR low = 0;
13952 CORE_ADDR high = 0;
13953 int retval;
13954
13955 retval = dwarf2_ranges_process (offset, cu,
13956 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13957 {
13958 if (ranges_pst != NULL)
13959 {
13960 CORE_ADDR lowpc;
13961 CORE_ADDR highpc;
13962
13963 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13964 range_beginning + baseaddr)
13965 - baseaddr);
13966 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13967 range_end + baseaddr)
13968 - baseaddr);
13969 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13970 lowpc, highpc - 1, ranges_pst);
13971 }
13972
13973 /* FIXME: This is recording everything as a low-high
13974 segment of consecutive addresses. We should have a
13975 data structure for discontiguous block ranges
13976 instead. */
13977 if (! low_set)
13978 {
13979 low = range_beginning;
13980 high = range_end;
13981 low_set = 1;
13982 }
13983 else
13984 {
13985 if (range_beginning < low)
13986 low = range_beginning;
13987 if (range_end > high)
13988 high = range_end;
13989 }
13990 });
13991 if (!retval)
13992 return 0;
13993
13994 if (! low_set)
13995 /* If the first entry is an end-of-list marker, the range
13996 describes an empty scope, i.e. no instructions. */
13997 return 0;
13998
13999 if (low_return)
14000 *low_return = low;
14001 if (high_return)
14002 *high_return = high;
14003 return 1;
14004}
14005
14006/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14007 definition for the return value. *LOWPC and *HIGHPC are set iff
14008 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14009
14010static enum pc_bounds_kind
14011dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14012 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14013 dwarf2_psymtab *pst)
14014{
14015 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14016 struct attribute *attr;
14017 struct attribute *attr_high;
14018 CORE_ADDR low = 0;
14019 CORE_ADDR high = 0;
14020 enum pc_bounds_kind ret;
14021
14022 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14023 if (attr_high)
14024 {
14025 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14026 if (attr != nullptr)
14027 {
14028 low = attr->value_as_address ();
14029 high = attr_high->value_as_address ();
14030 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14031 high += low;
14032 }
14033 else
14034 /* Found high w/o low attribute. */
14035 return PC_BOUNDS_INVALID;
14036
14037 /* Found consecutive range of addresses. */
14038 ret = PC_BOUNDS_HIGH_LOW;
14039 }
14040 else
14041 {
14042 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14043 if (attr != NULL)
14044 {
14045 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14046 We take advantage of the fact that DW_AT_ranges does not appear
14047 in DW_TAG_compile_unit of DWO files. */
14048 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14049 unsigned int ranges_offset = (DW_UNSND (attr)
14050 + (need_ranges_base
14051 ? cu->ranges_base
14052 : 0));
14053
14054 /* Value of the DW_AT_ranges attribute is the offset in the
14055 .debug_ranges section. */
14056 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14057 return PC_BOUNDS_INVALID;
14058 /* Found discontinuous range of addresses. */
14059 ret = PC_BOUNDS_RANGES;
14060 }
14061 else
14062 return PC_BOUNDS_NOT_PRESENT;
14063 }
14064
14065 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14066 if (high <= low)
14067 return PC_BOUNDS_INVALID;
14068
14069 /* When using the GNU linker, .gnu.linkonce. sections are used to
14070 eliminate duplicate copies of functions and vtables and such.
14071 The linker will arbitrarily choose one and discard the others.
14072 The AT_*_pc values for such functions refer to local labels in
14073 these sections. If the section from that file was discarded, the
14074 labels are not in the output, so the relocs get a value of 0.
14075 If this is a discarded function, mark the pc bounds as invalid,
14076 so that GDB will ignore it. */
14077 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14078 return PC_BOUNDS_INVALID;
14079
14080 *lowpc = low;
14081 if (highpc)
14082 *highpc = high;
14083 return ret;
14084}
14085
14086/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14087 its low and high PC addresses. Do nothing if these addresses could not
14088 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14089 and HIGHPC to the high address if greater than HIGHPC. */
14090
14091static void
14092dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14094 struct dwarf2_cu *cu)
14095{
14096 CORE_ADDR low, high;
14097 struct die_info *child = die->child;
14098
14099 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14100 {
14101 *lowpc = std::min (*lowpc, low);
14102 *highpc = std::max (*highpc, high);
14103 }
14104
14105 /* If the language does not allow nested subprograms (either inside
14106 subprograms or lexical blocks), we're done. */
14107 if (cu->language != language_ada)
14108 return;
14109
14110 /* Check all the children of the given DIE. If it contains nested
14111 subprograms, then check their pc bounds. Likewise, we need to
14112 check lexical blocks as well, as they may also contain subprogram
14113 definitions. */
14114 while (child && child->tag)
14115 {
14116 if (child->tag == DW_TAG_subprogram
14117 || child->tag == DW_TAG_lexical_block)
14118 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14119 child = child->sibling;
14120 }
14121}
14122
14123/* Get the low and high pc's represented by the scope DIE, and store
14124 them in *LOWPC and *HIGHPC. If the correct values can't be
14125 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14126
14127static void
14128get_scope_pc_bounds (struct die_info *die,
14129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14130 struct dwarf2_cu *cu)
14131{
14132 CORE_ADDR best_low = (CORE_ADDR) -1;
14133 CORE_ADDR best_high = (CORE_ADDR) 0;
14134 CORE_ADDR current_low, current_high;
14135
14136 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14137 >= PC_BOUNDS_RANGES)
14138 {
14139 best_low = current_low;
14140 best_high = current_high;
14141 }
14142 else
14143 {
14144 struct die_info *child = die->child;
14145
14146 while (child && child->tag)
14147 {
14148 switch (child->tag) {
14149 case DW_TAG_subprogram:
14150 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14151 break;
14152 case DW_TAG_namespace:
14153 case DW_TAG_module:
14154 /* FIXME: carlton/2004-01-16: Should we do this for
14155 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14156 that current GCC's always emit the DIEs corresponding
14157 to definitions of methods of classes as children of a
14158 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14159 the DIEs giving the declarations, which could be
14160 anywhere). But I don't see any reason why the
14161 standards says that they have to be there. */
14162 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14163
14164 if (current_low != ((CORE_ADDR) -1))
14165 {
14166 best_low = std::min (best_low, current_low);
14167 best_high = std::max (best_high, current_high);
14168 }
14169 break;
14170 default:
14171 /* Ignore. */
14172 break;
14173 }
14174
14175 child = child->sibling;
14176 }
14177 }
14178
14179 *lowpc = best_low;
14180 *highpc = best_high;
14181}
14182
14183/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14184 in DIE. */
14185
14186static void
14187dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14188 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14189{
14190 struct objfile *objfile = cu->per_objfile->objfile;
14191 struct gdbarch *gdbarch = objfile->arch ();
14192 struct attribute *attr;
14193 struct attribute *attr_high;
14194
14195 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14196 if (attr_high)
14197 {
14198 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14199 if (attr != nullptr)
14200 {
14201 CORE_ADDR low = attr->value_as_address ();
14202 CORE_ADDR high = attr_high->value_as_address ();
14203
14204 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14205 high += low;
14206
14207 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14208 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14209 cu->get_builder ()->record_block_range (block, low, high - 1);
14210 }
14211 }
14212
14213 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14214 if (attr != nullptr)
14215 {
14216 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14217 We take advantage of the fact that DW_AT_ranges does not appear
14218 in DW_TAG_compile_unit of DWO files. */
14219 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14220
14221 /* The value of the DW_AT_ranges attribute is the offset of the
14222 address range list in the .debug_ranges section. */
14223 unsigned long offset = (DW_UNSND (attr)
14224 + (need_ranges_base ? cu->ranges_base : 0));
14225
14226 std::vector<blockrange> blockvec;
14227 dwarf2_ranges_process (offset, cu,
14228 [&] (CORE_ADDR start, CORE_ADDR end)
14229 {
14230 start += baseaddr;
14231 end += baseaddr;
14232 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14233 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14234 cu->get_builder ()->record_block_range (block, start, end - 1);
14235 blockvec.emplace_back (start, end);
14236 });
14237
14238 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14239 }
14240}
14241
14242/* Check whether the producer field indicates either of GCC < 4.6, or the
14243 Intel C/C++ compiler, and cache the result in CU. */
14244
14245static void
14246check_producer (struct dwarf2_cu *cu)
14247{
14248 int major, minor;
14249
14250 if (cu->producer == NULL)
14251 {
14252 /* For unknown compilers expect their behavior is DWARF version
14253 compliant.
14254
14255 GCC started to support .debug_types sections by -gdwarf-4 since
14256 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14257 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14258 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14259 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14260 }
14261 else if (producer_is_gcc (cu->producer, &major, &minor))
14262 {
14263 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14264 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14265 }
14266 else if (producer_is_icc (cu->producer, &major, &minor))
14267 {
14268 cu->producer_is_icc = true;
14269 cu->producer_is_icc_lt_14 = major < 14;
14270 }
14271 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14272 cu->producer_is_codewarrior = true;
14273 else
14274 {
14275 /* For other non-GCC compilers, expect their behavior is DWARF version
14276 compliant. */
14277 }
14278
14279 cu->checked_producer = true;
14280}
14281
14282/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14283 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14284 during 4.6.0 experimental. */
14285
14286static bool
14287producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14288{
14289 if (!cu->checked_producer)
14290 check_producer (cu);
14291
14292 return cu->producer_is_gxx_lt_4_6;
14293}
14294
14295
14296/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14297 with incorrect is_stmt attributes. */
14298
14299static bool
14300producer_is_codewarrior (struct dwarf2_cu *cu)
14301{
14302 if (!cu->checked_producer)
14303 check_producer (cu);
14304
14305 return cu->producer_is_codewarrior;
14306}
14307
14308/* Return the default accessibility type if it is not overridden by
14309 DW_AT_accessibility. */
14310
14311static enum dwarf_access_attribute
14312dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14313{
14314 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14315 {
14316 /* The default DWARF 2 accessibility for members is public, the default
14317 accessibility for inheritance is private. */
14318
14319 if (die->tag != DW_TAG_inheritance)
14320 return DW_ACCESS_public;
14321 else
14322 return DW_ACCESS_private;
14323 }
14324 else
14325 {
14326 /* DWARF 3+ defines the default accessibility a different way. The same
14327 rules apply now for DW_TAG_inheritance as for the members and it only
14328 depends on the container kind. */
14329
14330 if (die->parent->tag == DW_TAG_class_type)
14331 return DW_ACCESS_private;
14332 else
14333 return DW_ACCESS_public;
14334 }
14335}
14336
14337/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14338 offset. If the attribute was not found return 0, otherwise return
14339 1. If it was found but could not properly be handled, set *OFFSET
14340 to 0. */
14341
14342static int
14343handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14344 LONGEST *offset)
14345{
14346 struct attribute *attr;
14347
14348 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14349 if (attr != NULL)
14350 {
14351 *offset = 0;
14352
14353 /* Note that we do not check for a section offset first here.
14354 This is because DW_AT_data_member_location is new in DWARF 4,
14355 so if we see it, we can assume that a constant form is really
14356 a constant and not a section offset. */
14357 if (attr->form_is_constant ())
14358 *offset = attr->constant_value (0);
14359 else if (attr->form_is_section_offset ())
14360 dwarf2_complex_location_expr_complaint ();
14361 else if (attr->form_is_block ())
14362 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14363 else
14364 dwarf2_complex_location_expr_complaint ();
14365
14366 return 1;
14367 }
14368
14369 return 0;
14370}
14371
14372/* Look for DW_AT_data_member_location and store the results in FIELD. */
14373
14374static void
14375handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14376 struct field *field)
14377{
14378 struct attribute *attr;
14379
14380 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14381 if (attr != NULL)
14382 {
14383 if (attr->form_is_constant ())
14384 {
14385 LONGEST offset = attr->constant_value (0);
14386 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14387 }
14388 else if (attr->form_is_section_offset ())
14389 dwarf2_complex_location_expr_complaint ();
14390 else if (attr->form_is_block ())
14391 {
14392 bool handled;
14393 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14394 if (handled)
14395 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14396 else
14397 {
14398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14399 struct objfile *objfile = per_objfile->objfile;
14400 struct dwarf2_locexpr_baton *dlbaton
14401 = XOBNEW (&objfile->objfile_obstack,
14402 struct dwarf2_locexpr_baton);
14403 dlbaton->data = DW_BLOCK (attr)->data;
14404 dlbaton->size = DW_BLOCK (attr)->size;
14405 /* When using this baton, we want to compute the address
14406 of the field, not the value. This is why
14407 is_reference is set to false here. */
14408 dlbaton->is_reference = false;
14409 dlbaton->per_objfile = per_objfile;
14410 dlbaton->per_cu = cu->per_cu;
14411
14412 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14413 }
14414 }
14415 else
14416 dwarf2_complex_location_expr_complaint ();
14417 }
14418}
14419
14420/* Add an aggregate field to the field list. */
14421
14422static void
14423dwarf2_add_field (struct field_info *fip, struct die_info *die,
14424 struct dwarf2_cu *cu)
14425{
14426 struct objfile *objfile = cu->per_objfile->objfile;
14427 struct gdbarch *gdbarch = objfile->arch ();
14428 struct nextfield *new_field;
14429 struct attribute *attr;
14430 struct field *fp;
14431 const char *fieldname = "";
14432
14433 if (die->tag == DW_TAG_inheritance)
14434 {
14435 fip->baseclasses.emplace_back ();
14436 new_field = &fip->baseclasses.back ();
14437 }
14438 else
14439 {
14440 fip->fields.emplace_back ();
14441 new_field = &fip->fields.back ();
14442 }
14443
14444 new_field->offset = die->sect_off;
14445
14446 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14447 if (attr != nullptr)
14448 new_field->accessibility = DW_UNSND (attr);
14449 else
14450 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14451 if (new_field->accessibility != DW_ACCESS_public)
14452 fip->non_public_fields = 1;
14453
14454 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14455 if (attr != nullptr)
14456 new_field->virtuality = DW_UNSND (attr);
14457 else
14458 new_field->virtuality = DW_VIRTUALITY_none;
14459
14460 fp = &new_field->field;
14461
14462 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14463 {
14464 /* Data member other than a C++ static data member. */
14465
14466 /* Get type of field. */
14467 fp->type = die_type (die, cu);
14468
14469 SET_FIELD_BITPOS (*fp, 0);
14470
14471 /* Get bit size of field (zero if none). */
14472 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14473 if (attr != nullptr)
14474 {
14475 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14476 }
14477 else
14478 {
14479 FIELD_BITSIZE (*fp) = 0;
14480 }
14481
14482 /* Get bit offset of field. */
14483 handle_data_member_location (die, cu, fp);
14484 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14485 if (attr != nullptr)
14486 {
14487 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14488 {
14489 /* For big endian bits, the DW_AT_bit_offset gives the
14490 additional bit offset from the MSB of the containing
14491 anonymous object to the MSB of the field. We don't
14492 have to do anything special since we don't need to
14493 know the size of the anonymous object. */
14494 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14495 }
14496 else
14497 {
14498 /* For little endian bits, compute the bit offset to the
14499 MSB of the anonymous object, subtract off the number of
14500 bits from the MSB of the field to the MSB of the
14501 object, and then subtract off the number of bits of
14502 the field itself. The result is the bit offset of
14503 the LSB of the field. */
14504 int anonymous_size;
14505 int bit_offset = DW_UNSND (attr);
14506
14507 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14508 if (attr != nullptr)
14509 {
14510 /* The size of the anonymous object containing
14511 the bit field is explicit, so use the
14512 indicated size (in bytes). */
14513 anonymous_size = DW_UNSND (attr);
14514 }
14515 else
14516 {
14517 /* The size of the anonymous object containing
14518 the bit field must be inferred from the type
14519 attribute of the data member containing the
14520 bit field. */
14521 anonymous_size = TYPE_LENGTH (fp->type);
14522 }
14523 SET_FIELD_BITPOS (*fp,
14524 (FIELD_BITPOS (*fp)
14525 + anonymous_size * bits_per_byte
14526 - bit_offset - FIELD_BITSIZE (*fp)));
14527 }
14528 }
14529 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14530 if (attr != NULL)
14531 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14532 + attr->constant_value (0)));
14533
14534 /* Get name of field. */
14535 fieldname = dwarf2_name (die, cu);
14536 if (fieldname == NULL)
14537 fieldname = "";
14538
14539 /* The name is already allocated along with this objfile, so we don't
14540 need to duplicate it for the type. */
14541 fp->name = fieldname;
14542
14543 /* Change accessibility for artificial fields (e.g. virtual table
14544 pointer or virtual base class pointer) to private. */
14545 if (dwarf2_attr (die, DW_AT_artificial, cu))
14546 {
14547 FIELD_ARTIFICIAL (*fp) = 1;
14548 new_field->accessibility = DW_ACCESS_private;
14549 fip->non_public_fields = 1;
14550 }
14551 }
14552 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14553 {
14554 /* C++ static member. */
14555
14556 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14557 is a declaration, but all versions of G++ as of this writing
14558 (so through at least 3.2.1) incorrectly generate
14559 DW_TAG_variable tags. */
14560
14561 const char *physname;
14562
14563 /* Get name of field. */
14564 fieldname = dwarf2_name (die, cu);
14565 if (fieldname == NULL)
14566 return;
14567
14568 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14569 if (attr
14570 /* Only create a symbol if this is an external value.
14571 new_symbol checks this and puts the value in the global symbol
14572 table, which we want. If it is not external, new_symbol
14573 will try to put the value in cu->list_in_scope which is wrong. */
14574 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14575 {
14576 /* A static const member, not much different than an enum as far as
14577 we're concerned, except that we can support more types. */
14578 new_symbol (die, NULL, cu);
14579 }
14580
14581 /* Get physical name. */
14582 physname = dwarf2_physname (fieldname, die, cu);
14583
14584 /* The name is already allocated along with this objfile, so we don't
14585 need to duplicate it for the type. */
14586 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14587 FIELD_TYPE (*fp) = die_type (die, cu);
14588 FIELD_NAME (*fp) = fieldname;
14589 }
14590 else if (die->tag == DW_TAG_inheritance)
14591 {
14592 /* C++ base class field. */
14593 handle_data_member_location (die, cu, fp);
14594 FIELD_BITSIZE (*fp) = 0;
14595 FIELD_TYPE (*fp) = die_type (die, cu);
14596 FIELD_NAME (*fp) = fp->type->name ();
14597 }
14598 else
14599 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14600}
14601
14602/* Can the type given by DIE define another type? */
14603
14604static bool
14605type_can_define_types (const struct die_info *die)
14606{
14607 switch (die->tag)
14608 {
14609 case DW_TAG_typedef:
14610 case DW_TAG_class_type:
14611 case DW_TAG_structure_type:
14612 case DW_TAG_union_type:
14613 case DW_TAG_enumeration_type:
14614 return true;
14615
14616 default:
14617 return false;
14618 }
14619}
14620
14621/* Add a type definition defined in the scope of the FIP's class. */
14622
14623static void
14624dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14625 struct dwarf2_cu *cu)
14626{
14627 struct decl_field fp;
14628 memset (&fp, 0, sizeof (fp));
14629
14630 gdb_assert (type_can_define_types (die));
14631
14632 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14633 fp.name = dwarf2_name (die, cu);
14634 fp.type = read_type_die (die, cu);
14635
14636 /* Save accessibility. */
14637 enum dwarf_access_attribute accessibility;
14638 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14639 if (attr != NULL)
14640 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14641 else
14642 accessibility = dwarf2_default_access_attribute (die, cu);
14643 switch (accessibility)
14644 {
14645 case DW_ACCESS_public:
14646 /* The assumed value if neither private nor protected. */
14647 break;
14648 case DW_ACCESS_private:
14649 fp.is_private = 1;
14650 break;
14651 case DW_ACCESS_protected:
14652 fp.is_protected = 1;
14653 break;
14654 default:
14655 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14656 }
14657
14658 if (die->tag == DW_TAG_typedef)
14659 fip->typedef_field_list.push_back (fp);
14660 else
14661 fip->nested_types_list.push_back (fp);
14662}
14663
14664/* A convenience typedef that's used when finding the discriminant
14665 field for a variant part. */
14666typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14667 offset_map_type;
14668
14669/* Compute the discriminant range for a given variant. OBSTACK is
14670 where the results will be stored. VARIANT is the variant to
14671 process. IS_UNSIGNED indicates whether the discriminant is signed
14672 or unsigned. */
14673
14674static const gdb::array_view<discriminant_range>
14675convert_variant_range (struct obstack *obstack, const variant_field &variant,
14676 bool is_unsigned)
14677{
14678 std::vector<discriminant_range> ranges;
14679
14680 if (variant.default_branch)
14681 return {};
14682
14683 if (variant.discr_list_data == nullptr)
14684 {
14685 discriminant_range r
14686 = {variant.discriminant_value, variant.discriminant_value};
14687 ranges.push_back (r);
14688 }
14689 else
14690 {
14691 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14692 variant.discr_list_data->size);
14693 while (!data.empty ())
14694 {
14695 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14696 {
14697 complaint (_("invalid discriminant marker: %d"), data[0]);
14698 break;
14699 }
14700 bool is_range = data[0] == DW_DSC_range;
14701 data = data.slice (1);
14702
14703 ULONGEST low, high;
14704 unsigned int bytes_read;
14705
14706 if (data.empty ())
14707 {
14708 complaint (_("DW_AT_discr_list missing low value"));
14709 break;
14710 }
14711 if (is_unsigned)
14712 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14713 else
14714 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14715 &bytes_read);
14716 data = data.slice (bytes_read);
14717
14718 if (is_range)
14719 {
14720 if (data.empty ())
14721 {
14722 complaint (_("DW_AT_discr_list missing high value"));
14723 break;
14724 }
14725 if (is_unsigned)
14726 high = read_unsigned_leb128 (nullptr, data.data (),
14727 &bytes_read);
14728 else
14729 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14730 &bytes_read);
14731 data = data.slice (bytes_read);
14732 }
14733 else
14734 high = low;
14735
14736 ranges.push_back ({ low, high });
14737 }
14738 }
14739
14740 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14741 ranges.size ());
14742 std::copy (ranges.begin (), ranges.end (), result);
14743 return gdb::array_view<discriminant_range> (result, ranges.size ());
14744}
14745
14746static const gdb::array_view<variant_part> create_variant_parts
14747 (struct obstack *obstack,
14748 const offset_map_type &offset_map,
14749 struct field_info *fi,
14750 const std::vector<variant_part_builder> &variant_parts);
14751
14752/* Fill in a "struct variant" for a given variant field. RESULT is
14753 the variant to fill in. OBSTACK is where any needed allocations
14754 will be done. OFFSET_MAP holds the mapping from section offsets to
14755 fields for the type. FI describes the fields of the type we're
14756 processing. FIELD is the variant field we're converting. */
14757
14758static void
14759create_one_variant (variant &result, struct obstack *obstack,
14760 const offset_map_type &offset_map,
14761 struct field_info *fi, const variant_field &field)
14762{
14763 result.discriminants = convert_variant_range (obstack, field, false);
14764 result.first_field = field.first_field + fi->baseclasses.size ();
14765 result.last_field = field.last_field + fi->baseclasses.size ();
14766 result.parts = create_variant_parts (obstack, offset_map, fi,
14767 field.variant_parts);
14768}
14769
14770/* Fill in a "struct variant_part" for a given variant part. RESULT
14771 is the variant part to fill in. OBSTACK is where any needed
14772 allocations will be done. OFFSET_MAP holds the mapping from
14773 section offsets to fields for the type. FI describes the fields of
14774 the type we're processing. BUILDER is the variant part to be
14775 converted. */
14776
14777static void
14778create_one_variant_part (variant_part &result,
14779 struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const variant_part_builder &builder)
14783{
14784 auto iter = offset_map.find (builder.discriminant_offset);
14785 if (iter == offset_map.end ())
14786 {
14787 result.discriminant_index = -1;
14788 /* Doesn't matter. */
14789 result.is_unsigned = false;
14790 }
14791 else
14792 {
14793 result.discriminant_index = iter->second;
14794 result.is_unsigned
14795 = TYPE_UNSIGNED (FIELD_TYPE
14796 (fi->fields[result.discriminant_index].field));
14797 }
14798
14799 size_t n = builder.variants.size ();
14800 variant *output = new (obstack) variant[n];
14801 for (size_t i = 0; i < n; ++i)
14802 create_one_variant (output[i], obstack, offset_map, fi,
14803 builder.variants[i]);
14804
14805 result.variants = gdb::array_view<variant> (output, n);
14806}
14807
14808/* Create a vector of variant parts that can be attached to a type.
14809 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14810 holds the mapping from section offsets to fields for the type. FI
14811 describes the fields of the type we're processing. VARIANT_PARTS
14812 is the vector to convert. */
14813
14814static const gdb::array_view<variant_part>
14815create_variant_parts (struct obstack *obstack,
14816 const offset_map_type &offset_map,
14817 struct field_info *fi,
14818 const std::vector<variant_part_builder> &variant_parts)
14819{
14820 if (variant_parts.empty ())
14821 return {};
14822
14823 size_t n = variant_parts.size ();
14824 variant_part *result = new (obstack) variant_part[n];
14825 for (size_t i = 0; i < n; ++i)
14826 create_one_variant_part (result[i], obstack, offset_map, fi,
14827 variant_parts[i]);
14828
14829 return gdb::array_view<variant_part> (result, n);
14830}
14831
14832/* Compute the variant part vector for FIP, attaching it to TYPE when
14833 done. */
14834
14835static void
14836add_variant_property (struct field_info *fip, struct type *type,
14837 struct dwarf2_cu *cu)
14838{
14839 /* Map section offsets of fields to their field index. Note the
14840 field index here does not take the number of baseclasses into
14841 account. */
14842 offset_map_type offset_map;
14843 for (int i = 0; i < fip->fields.size (); ++i)
14844 offset_map[fip->fields[i].offset] = i;
14845
14846 struct objfile *objfile = cu->per_objfile->objfile;
14847 gdb::array_view<variant_part> parts
14848 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14849 fip->variant_parts);
14850
14851 struct dynamic_prop prop;
14852 prop.kind = PROP_VARIANT_PARTS;
14853 prop.data.variant_parts
14854 = ((gdb::array_view<variant_part> *)
14855 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14856
14857 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14858}
14859
14860/* Create the vector of fields, and attach it to the type. */
14861
14862static void
14863dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14864 struct dwarf2_cu *cu)
14865{
14866 int nfields = fip->nfields ();
14867
14868 /* Record the field count, allocate space for the array of fields,
14869 and create blank accessibility bitfields if necessary. */
14870 type->set_num_fields (nfields);
14871 type->set_fields
14872 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14873
14874 if (fip->non_public_fields && cu->language != language_ada)
14875 {
14876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14877
14878 TYPE_FIELD_PRIVATE_BITS (type) =
14879 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14880 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14881
14882 TYPE_FIELD_PROTECTED_BITS (type) =
14883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14884 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14885
14886 TYPE_FIELD_IGNORE_BITS (type) =
14887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14888 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14889 }
14890
14891 /* If the type has baseclasses, allocate and clear a bit vector for
14892 TYPE_FIELD_VIRTUAL_BITS. */
14893 if (!fip->baseclasses.empty () && cu->language != language_ada)
14894 {
14895 int num_bytes = B_BYTES (fip->baseclasses.size ());
14896 unsigned char *pointer;
14897
14898 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14899 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14900 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14901 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14902 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14903 }
14904
14905 if (!fip->variant_parts.empty ())
14906 add_variant_property (fip, type, cu);
14907
14908 /* Copy the saved-up fields into the field vector. */
14909 for (int i = 0; i < nfields; ++i)
14910 {
14911 struct nextfield &field
14912 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14913 : fip->fields[i - fip->baseclasses.size ()]);
14914
14915 type->field (i) = field.field;
14916 switch (field.accessibility)
14917 {
14918 case DW_ACCESS_private:
14919 if (cu->language != language_ada)
14920 SET_TYPE_FIELD_PRIVATE (type, i);
14921 break;
14922
14923 case DW_ACCESS_protected:
14924 if (cu->language != language_ada)
14925 SET_TYPE_FIELD_PROTECTED (type, i);
14926 break;
14927
14928 case DW_ACCESS_public:
14929 break;
14930
14931 default:
14932 /* Unknown accessibility. Complain and treat it as public. */
14933 {
14934 complaint (_("unsupported accessibility %d"),
14935 field.accessibility);
14936 }
14937 break;
14938 }
14939 if (i < fip->baseclasses.size ())
14940 {
14941 switch (field.virtuality)
14942 {
14943 case DW_VIRTUALITY_virtual:
14944 case DW_VIRTUALITY_pure_virtual:
14945 if (cu->language == language_ada)
14946 error (_("unexpected virtuality in component of Ada type"));
14947 SET_TYPE_FIELD_VIRTUAL (type, i);
14948 break;
14949 }
14950 }
14951 }
14952}
14953
14954/* Return true if this member function is a constructor, false
14955 otherwise. */
14956
14957static int
14958dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14959{
14960 const char *fieldname;
14961 const char *type_name;
14962 int len;
14963
14964 if (die->parent == NULL)
14965 return 0;
14966
14967 if (die->parent->tag != DW_TAG_structure_type
14968 && die->parent->tag != DW_TAG_union_type
14969 && die->parent->tag != DW_TAG_class_type)
14970 return 0;
14971
14972 fieldname = dwarf2_name (die, cu);
14973 type_name = dwarf2_name (die->parent, cu);
14974 if (fieldname == NULL || type_name == NULL)
14975 return 0;
14976
14977 len = strlen (fieldname);
14978 return (strncmp (fieldname, type_name, len) == 0
14979 && (type_name[len] == '\0' || type_name[len] == '<'));
14980}
14981
14982/* Check if the given VALUE is a recognized enum
14983 dwarf_defaulted_attribute constant according to DWARF5 spec,
14984 Table 7.24. */
14985
14986static bool
14987is_valid_DW_AT_defaulted (ULONGEST value)
14988{
14989 switch (value)
14990 {
14991 case DW_DEFAULTED_no:
14992 case DW_DEFAULTED_in_class:
14993 case DW_DEFAULTED_out_of_class:
14994 return true;
14995 }
14996
14997 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14998 return false;
14999}
15000
15001/* Add a member function to the proper fieldlist. */
15002
15003static void
15004dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15005 struct type *type, struct dwarf2_cu *cu)
15006{
15007 struct objfile *objfile = cu->per_objfile->objfile;
15008 struct attribute *attr;
15009 int i;
15010 struct fnfieldlist *flp = nullptr;
15011 struct fn_field *fnp;
15012 const char *fieldname;
15013 struct type *this_type;
15014 enum dwarf_access_attribute accessibility;
15015
15016 if (cu->language == language_ada)
15017 error (_("unexpected member function in Ada type"));
15018
15019 /* Get name of member function. */
15020 fieldname = dwarf2_name (die, cu);
15021 if (fieldname == NULL)
15022 return;
15023
15024 /* Look up member function name in fieldlist. */
15025 for (i = 0; i < fip->fnfieldlists.size (); i++)
15026 {
15027 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15028 {
15029 flp = &fip->fnfieldlists[i];
15030 break;
15031 }
15032 }
15033
15034 /* Create a new fnfieldlist if necessary. */
15035 if (flp == nullptr)
15036 {
15037 fip->fnfieldlists.emplace_back ();
15038 flp = &fip->fnfieldlists.back ();
15039 flp->name = fieldname;
15040 i = fip->fnfieldlists.size () - 1;
15041 }
15042
15043 /* Create a new member function field and add it to the vector of
15044 fnfieldlists. */
15045 flp->fnfields.emplace_back ();
15046 fnp = &flp->fnfields.back ();
15047
15048 /* Delay processing of the physname until later. */
15049 if (cu->language == language_cplus)
15050 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15051 die, cu);
15052 else
15053 {
15054 const char *physname = dwarf2_physname (fieldname, die, cu);
15055 fnp->physname = physname ? physname : "";
15056 }
15057
15058 fnp->type = alloc_type (objfile);
15059 this_type = read_type_die (die, cu);
15060 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15061 {
15062 int nparams = this_type->num_fields ();
15063
15064 /* TYPE is the domain of this method, and THIS_TYPE is the type
15065 of the method itself (TYPE_CODE_METHOD). */
15066 smash_to_method_type (fnp->type, type,
15067 TYPE_TARGET_TYPE (this_type),
15068 this_type->fields (),
15069 this_type->num_fields (),
15070 TYPE_VARARGS (this_type));
15071
15072 /* Handle static member functions.
15073 Dwarf2 has no clean way to discern C++ static and non-static
15074 member functions. G++ helps GDB by marking the first
15075 parameter for non-static member functions (which is the this
15076 pointer) as artificial. We obtain this information from
15077 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15078 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15079 fnp->voffset = VOFFSET_STATIC;
15080 }
15081 else
15082 complaint (_("member function type missing for '%s'"),
15083 dwarf2_full_name (fieldname, die, cu));
15084
15085 /* Get fcontext from DW_AT_containing_type if present. */
15086 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15087 fnp->fcontext = die_containing_type (die, cu);
15088
15089 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15090 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15091
15092 /* Get accessibility. */
15093 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15094 if (attr != nullptr)
15095 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15096 else
15097 accessibility = dwarf2_default_access_attribute (die, cu);
15098 switch (accessibility)
15099 {
15100 case DW_ACCESS_private:
15101 fnp->is_private = 1;
15102 break;
15103 case DW_ACCESS_protected:
15104 fnp->is_protected = 1;
15105 break;
15106 }
15107
15108 /* Check for artificial methods. */
15109 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15110 if (attr && DW_UNSND (attr) != 0)
15111 fnp->is_artificial = 1;
15112
15113 /* Check for defaulted methods. */
15114 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15115 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15116 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15117
15118 /* Check for deleted methods. */
15119 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15120 if (attr != nullptr && DW_UNSND (attr) != 0)
15121 fnp->is_deleted = 1;
15122
15123 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15124
15125 /* Get index in virtual function table if it is a virtual member
15126 function. For older versions of GCC, this is an offset in the
15127 appropriate virtual table, as specified by DW_AT_containing_type.
15128 For everyone else, it is an expression to be evaluated relative
15129 to the object address. */
15130
15131 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15132 if (attr != nullptr)
15133 {
15134 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15135 {
15136 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15137 {
15138 /* Old-style GCC. */
15139 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15140 }
15141 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15142 || (DW_BLOCK (attr)->size > 1
15143 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15144 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15145 {
15146 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15147 if ((fnp->voffset % cu->header.addr_size) != 0)
15148 dwarf2_complex_location_expr_complaint ();
15149 else
15150 fnp->voffset /= cu->header.addr_size;
15151 fnp->voffset += 2;
15152 }
15153 else
15154 dwarf2_complex_location_expr_complaint ();
15155
15156 if (!fnp->fcontext)
15157 {
15158 /* If there is no `this' field and no DW_AT_containing_type,
15159 we cannot actually find a base class context for the
15160 vtable! */
15161 if (this_type->num_fields () == 0
15162 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15163 {
15164 complaint (_("cannot determine context for virtual member "
15165 "function \"%s\" (offset %s)"),
15166 fieldname, sect_offset_str (die->sect_off));
15167 }
15168 else
15169 {
15170 fnp->fcontext
15171 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15172 }
15173 }
15174 }
15175 else if (attr->form_is_section_offset ())
15176 {
15177 dwarf2_complex_location_expr_complaint ();
15178 }
15179 else
15180 {
15181 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15182 fieldname);
15183 }
15184 }
15185 else
15186 {
15187 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15188 if (attr && DW_UNSND (attr))
15189 {
15190 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15191 complaint (_("Member function \"%s\" (offset %s) is virtual "
15192 "but the vtable offset is not specified"),
15193 fieldname, sect_offset_str (die->sect_off));
15194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15195 TYPE_CPLUS_DYNAMIC (type) = 1;
15196 }
15197 }
15198}
15199
15200/* Create the vector of member function fields, and attach it to the type. */
15201
15202static void
15203dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15204 struct dwarf2_cu *cu)
15205{
15206 if (cu->language == language_ada)
15207 error (_("unexpected member functions in Ada type"));
15208
15209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15210 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15211 TYPE_ALLOC (type,
15212 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15213
15214 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15215 {
15216 struct fnfieldlist &nf = fip->fnfieldlists[i];
15217 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15218
15219 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15220 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15221 fn_flp->fn_fields = (struct fn_field *)
15222 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15223
15224 for (int k = 0; k < nf.fnfields.size (); ++k)
15225 fn_flp->fn_fields[k] = nf.fnfields[k];
15226 }
15227
15228 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15229}
15230
15231/* Returns non-zero if NAME is the name of a vtable member in CU's
15232 language, zero otherwise. */
15233static int
15234is_vtable_name (const char *name, struct dwarf2_cu *cu)
15235{
15236 static const char vptr[] = "_vptr";
15237
15238 /* Look for the C++ form of the vtable. */
15239 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15240 return 1;
15241
15242 return 0;
15243}
15244
15245/* GCC outputs unnamed structures that are really pointers to member
15246 functions, with the ABI-specified layout. If TYPE describes
15247 such a structure, smash it into a member function type.
15248
15249 GCC shouldn't do this; it should just output pointer to member DIEs.
15250 This is GCC PR debug/28767. */
15251
15252static void
15253quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15254{
15255 struct type *pfn_type, *self_type, *new_type;
15256
15257 /* Check for a structure with no name and two children. */
15258 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15259 return;
15260
15261 /* Check for __pfn and __delta members. */
15262 if (TYPE_FIELD_NAME (type, 0) == NULL
15263 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15264 || TYPE_FIELD_NAME (type, 1) == NULL
15265 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15266 return;
15267
15268 /* Find the type of the method. */
15269 pfn_type = TYPE_FIELD_TYPE (type, 0);
15270 if (pfn_type == NULL
15271 || pfn_type->code () != TYPE_CODE_PTR
15272 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15273 return;
15274
15275 /* Look for the "this" argument. */
15276 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15277 if (pfn_type->num_fields () == 0
15278 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15279 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15280 return;
15281
15282 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15283 new_type = alloc_type (objfile);
15284 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15285 pfn_type->fields (), pfn_type->num_fields (),
15286 TYPE_VARARGS (pfn_type));
15287 smash_to_methodptr_type (type, new_type);
15288}
15289
15290/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15291 appropriate error checking and issuing complaints if there is a
15292 problem. */
15293
15294static ULONGEST
15295get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15296{
15297 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15298
15299 if (attr == nullptr)
15300 return 0;
15301
15302 if (!attr->form_is_constant ())
15303 {
15304 complaint (_("DW_AT_alignment must have constant form"
15305 " - DIE at %s [in module %s]"),
15306 sect_offset_str (die->sect_off),
15307 objfile_name (cu->per_objfile->objfile));
15308 return 0;
15309 }
15310
15311 ULONGEST align;
15312 if (attr->form == DW_FORM_sdata)
15313 {
15314 LONGEST val = DW_SND (attr);
15315 if (val < 0)
15316 {
15317 complaint (_("DW_AT_alignment value must not be negative"
15318 " - DIE at %s [in module %s]"),
15319 sect_offset_str (die->sect_off),
15320 objfile_name (cu->per_objfile->objfile));
15321 return 0;
15322 }
15323 align = val;
15324 }
15325 else
15326 align = DW_UNSND (attr);
15327
15328 if (align == 0)
15329 {
15330 complaint (_("DW_AT_alignment value must not be zero"
15331 " - DIE at %s [in module %s]"),
15332 sect_offset_str (die->sect_off),
15333 objfile_name (cu->per_objfile->objfile));
15334 return 0;
15335 }
15336 if ((align & (align - 1)) != 0)
15337 {
15338 complaint (_("DW_AT_alignment value must be a power of 2"
15339 " - DIE at %s [in module %s]"),
15340 sect_offset_str (die->sect_off),
15341 objfile_name (cu->per_objfile->objfile));
15342 return 0;
15343 }
15344
15345 return align;
15346}
15347
15348/* If the DIE has a DW_AT_alignment attribute, use its value to set
15349 the alignment for TYPE. */
15350
15351static void
15352maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15353 struct type *type)
15354{
15355 if (!set_type_align (type, get_alignment (cu, die)))
15356 complaint (_("DW_AT_alignment value too large"
15357 " - DIE at %s [in module %s]"),
15358 sect_offset_str (die->sect_off),
15359 objfile_name (cu->per_objfile->objfile));
15360}
15361
15362/* Check if the given VALUE is a valid enum dwarf_calling_convention
15363 constant for a type, according to DWARF5 spec, Table 5.5. */
15364
15365static bool
15366is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15367{
15368 switch (value)
15369 {
15370 case DW_CC_normal:
15371 case DW_CC_pass_by_reference:
15372 case DW_CC_pass_by_value:
15373 return true;
15374
15375 default:
15376 complaint (_("unrecognized DW_AT_calling_convention value "
15377 "(%s) for a type"), pulongest (value));
15378 return false;
15379 }
15380}
15381
15382/* Check if the given VALUE is a valid enum dwarf_calling_convention
15383 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15384 also according to GNU-specific values (see include/dwarf2.h). */
15385
15386static bool
15387is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15388{
15389 switch (value)
15390 {
15391 case DW_CC_normal:
15392 case DW_CC_program:
15393 case DW_CC_nocall:
15394 return true;
15395
15396 case DW_CC_GNU_renesas_sh:
15397 case DW_CC_GNU_borland_fastcall_i386:
15398 case DW_CC_GDB_IBM_OpenCL:
15399 return true;
15400
15401 default:
15402 complaint (_("unrecognized DW_AT_calling_convention value "
15403 "(%s) for a subroutine"), pulongest (value));
15404 return false;
15405 }
15406}
15407
15408/* Called when we find the DIE that starts a structure or union scope
15409 (definition) to create a type for the structure or union. Fill in
15410 the type's name and general properties; the members will not be
15411 processed until process_structure_scope. A symbol table entry for
15412 the type will also not be done until process_structure_scope (assuming
15413 the type has a name).
15414
15415 NOTE: we need to call these functions regardless of whether or not the
15416 DIE has a DW_AT_name attribute, since it might be an anonymous
15417 structure or union. This gets the type entered into our set of
15418 user defined types. */
15419
15420static struct type *
15421read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15422{
15423 struct objfile *objfile = cu->per_objfile->objfile;
15424 struct type *type;
15425 struct attribute *attr;
15426 const char *name;
15427
15428 /* If the definition of this type lives in .debug_types, read that type.
15429 Don't follow DW_AT_specification though, that will take us back up
15430 the chain and we want to go down. */
15431 attr = die->attr (DW_AT_signature);
15432 if (attr != nullptr)
15433 {
15434 type = get_DW_AT_signature_type (die, attr, cu);
15435
15436 /* The type's CU may not be the same as CU.
15437 Ensure TYPE is recorded with CU in die_type_hash. */
15438 return set_die_type (die, type, cu);
15439 }
15440
15441 type = alloc_type (objfile);
15442 INIT_CPLUS_SPECIFIC (type);
15443
15444 name = dwarf2_name (die, cu);
15445 if (name != NULL)
15446 {
15447 if (cu->language == language_cplus
15448 || cu->language == language_d
15449 || cu->language == language_rust)
15450 {
15451 const char *full_name = dwarf2_full_name (name, die, cu);
15452
15453 /* dwarf2_full_name might have already finished building the DIE's
15454 type. If so, there is no need to continue. */
15455 if (get_die_type (die, cu) != NULL)
15456 return get_die_type (die, cu);
15457
15458 type->set_name (full_name);
15459 }
15460 else
15461 {
15462 /* The name is already allocated along with this objfile, so
15463 we don't need to duplicate it for the type. */
15464 type->set_name (name);
15465 }
15466 }
15467
15468 if (die->tag == DW_TAG_structure_type)
15469 {
15470 type->set_code (TYPE_CODE_STRUCT);
15471 }
15472 else if (die->tag == DW_TAG_union_type)
15473 {
15474 type->set_code (TYPE_CODE_UNION);
15475 }
15476 else
15477 {
15478 type->set_code (TYPE_CODE_STRUCT);
15479 }
15480
15481 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15482 TYPE_DECLARED_CLASS (type) = 1;
15483
15484 /* Store the calling convention in the type if it's available in
15485 the die. Otherwise the calling convention remains set to
15486 the default value DW_CC_normal. */
15487 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15488 if (attr != nullptr
15489 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15490 {
15491 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15492 TYPE_CPLUS_CALLING_CONVENTION (type)
15493 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15494 }
15495
15496 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15497 if (attr != nullptr)
15498 {
15499 if (attr->form_is_constant ())
15500 TYPE_LENGTH (type) = DW_UNSND (attr);
15501 else
15502 {
15503 struct dynamic_prop prop;
15504 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15505 cu->per_cu->addr_type ()))
15506 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15507 TYPE_LENGTH (type) = 0;
15508 }
15509 }
15510 else
15511 {
15512 TYPE_LENGTH (type) = 0;
15513 }
15514
15515 maybe_set_alignment (cu, die, type);
15516
15517 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15518 {
15519 /* ICC<14 does not output the required DW_AT_declaration on
15520 incomplete types, but gives them a size of zero. */
15521 TYPE_STUB (type) = 1;
15522 }
15523 else
15524 TYPE_STUB_SUPPORTED (type) = 1;
15525
15526 if (die_is_declaration (die, cu))
15527 TYPE_STUB (type) = 1;
15528 else if (attr == NULL && die->child == NULL
15529 && producer_is_realview (cu->producer))
15530 /* RealView does not output the required DW_AT_declaration
15531 on incomplete types. */
15532 TYPE_STUB (type) = 1;
15533
15534 /* We need to add the type field to the die immediately so we don't
15535 infinitely recurse when dealing with pointers to the structure
15536 type within the structure itself. */
15537 set_die_type (die, type, cu);
15538
15539 /* set_die_type should be already done. */
15540 set_descriptive_type (type, die, cu);
15541
15542 return type;
15543}
15544
15545static void handle_struct_member_die
15546 (struct die_info *child_die,
15547 struct type *type,
15548 struct field_info *fi,
15549 std::vector<struct symbol *> *template_args,
15550 struct dwarf2_cu *cu);
15551
15552/* A helper for handle_struct_member_die that handles
15553 DW_TAG_variant_part. */
15554
15555static void
15556handle_variant_part (struct die_info *die, struct type *type,
15557 struct field_info *fi,
15558 std::vector<struct symbol *> *template_args,
15559 struct dwarf2_cu *cu)
15560{
15561 variant_part_builder *new_part;
15562 if (fi->current_variant_part == nullptr)
15563 {
15564 fi->variant_parts.emplace_back ();
15565 new_part = &fi->variant_parts.back ();
15566 }
15567 else if (!fi->current_variant_part->processing_variant)
15568 {
15569 complaint (_("nested DW_TAG_variant_part seen "
15570 "- DIE at %s [in module %s]"),
15571 sect_offset_str (die->sect_off),
15572 objfile_name (cu->per_objfile->objfile));
15573 return;
15574 }
15575 else
15576 {
15577 variant_field &current = fi->current_variant_part->variants.back ();
15578 current.variant_parts.emplace_back ();
15579 new_part = &current.variant_parts.back ();
15580 }
15581
15582 /* When we recurse, we want callees to add to this new variant
15583 part. */
15584 scoped_restore save_current_variant_part
15585 = make_scoped_restore (&fi->current_variant_part, new_part);
15586
15587 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15588 if (discr == NULL)
15589 {
15590 /* It's a univariant form, an extension we support. */
15591 }
15592 else if (discr->form_is_ref ())
15593 {
15594 struct dwarf2_cu *target_cu = cu;
15595 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15596
15597 new_part->discriminant_offset = target_die->sect_off;
15598 }
15599 else
15600 {
15601 complaint (_("DW_AT_discr does not have DIE reference form"
15602 " - DIE at %s [in module %s]"),
15603 sect_offset_str (die->sect_off),
15604 objfile_name (cu->per_objfile->objfile));
15605 }
15606
15607 for (die_info *child_die = die->child;
15608 child_die != NULL;
15609 child_die = child_die->sibling)
15610 handle_struct_member_die (child_die, type, fi, template_args, cu);
15611}
15612
15613/* A helper for handle_struct_member_die that handles
15614 DW_TAG_variant. */
15615
15616static void
15617handle_variant (struct die_info *die, struct type *type,
15618 struct field_info *fi,
15619 std::vector<struct symbol *> *template_args,
15620 struct dwarf2_cu *cu)
15621{
15622 if (fi->current_variant_part == nullptr)
15623 {
15624 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15625 "- DIE at %s [in module %s]"),
15626 sect_offset_str (die->sect_off),
15627 objfile_name (cu->per_objfile->objfile));
15628 return;
15629 }
15630 if (fi->current_variant_part->processing_variant)
15631 {
15632 complaint (_("nested DW_TAG_variant seen "
15633 "- DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
15635 objfile_name (cu->per_objfile->objfile));
15636 return;
15637 }
15638
15639 scoped_restore save_processing_variant
15640 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15641 true);
15642
15643 fi->current_variant_part->variants.emplace_back ();
15644 variant_field &variant = fi->current_variant_part->variants.back ();
15645 variant.first_field = fi->fields.size ();
15646
15647 /* In a variant we want to get the discriminant and also add a
15648 field for our sole member child. */
15649 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15650 if (discr == nullptr)
15651 {
15652 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15653 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15654 variant.default_branch = true;
15655 else
15656 variant.discr_list_data = DW_BLOCK (discr);
15657 }
15658 else
15659 variant.discriminant_value = DW_UNSND (discr);
15660
15661 for (die_info *variant_child = die->child;
15662 variant_child != NULL;
15663 variant_child = variant_child->sibling)
15664 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15665
15666 variant.last_field = fi->fields.size ();
15667}
15668
15669/* A helper for process_structure_scope that handles a single member
15670 DIE. */
15671
15672static void
15673handle_struct_member_die (struct die_info *child_die, struct type *type,
15674 struct field_info *fi,
15675 std::vector<struct symbol *> *template_args,
15676 struct dwarf2_cu *cu)
15677{
15678 if (child_die->tag == DW_TAG_member
15679 || child_die->tag == DW_TAG_variable)
15680 {
15681 /* NOTE: carlton/2002-11-05: A C++ static data member
15682 should be a DW_TAG_member that is a declaration, but
15683 all versions of G++ as of this writing (so through at
15684 least 3.2.1) incorrectly generate DW_TAG_variable
15685 tags for them instead. */
15686 dwarf2_add_field (fi, child_die, cu);
15687 }
15688 else if (child_die->tag == DW_TAG_subprogram)
15689 {
15690 /* Rust doesn't have member functions in the C++ sense.
15691 However, it does emit ordinary functions as children
15692 of a struct DIE. */
15693 if (cu->language == language_rust)
15694 read_func_scope (child_die, cu);
15695 else
15696 {
15697 /* C++ member function. */
15698 dwarf2_add_member_fn (fi, child_die, type, cu);
15699 }
15700 }
15701 else if (child_die->tag == DW_TAG_inheritance)
15702 {
15703 /* C++ base class field. */
15704 dwarf2_add_field (fi, child_die, cu);
15705 }
15706 else if (type_can_define_types (child_die))
15707 dwarf2_add_type_defn (fi, child_die, cu);
15708 else if (child_die->tag == DW_TAG_template_type_param
15709 || child_die->tag == DW_TAG_template_value_param)
15710 {
15711 struct symbol *arg = new_symbol (child_die, NULL, cu);
15712
15713 if (arg != NULL)
15714 template_args->push_back (arg);
15715 }
15716 else if (child_die->tag == DW_TAG_variant_part)
15717 handle_variant_part (child_die, type, fi, template_args, cu);
15718 else if (child_die->tag == DW_TAG_variant)
15719 handle_variant (child_die, type, fi, template_args, cu);
15720}
15721
15722/* Finish creating a structure or union type, including filling in
15723 its members and creating a symbol for it. */
15724
15725static void
15726process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15727{
15728 struct objfile *objfile = cu->per_objfile->objfile;
15729 struct die_info *child_die;
15730 struct type *type;
15731
15732 type = get_die_type (die, cu);
15733 if (type == NULL)
15734 type = read_structure_type (die, cu);
15735
15736 bool has_template_parameters = false;
15737 if (die->child != NULL && ! die_is_declaration (die, cu))
15738 {
15739 struct field_info fi;
15740 std::vector<struct symbol *> template_args;
15741
15742 child_die = die->child;
15743
15744 while (child_die && child_die->tag)
15745 {
15746 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15747 child_die = child_die->sibling;
15748 }
15749
15750 /* Attach template arguments to type. */
15751 if (!template_args.empty ())
15752 {
15753 has_template_parameters = true;
15754 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15755 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15756 TYPE_TEMPLATE_ARGUMENTS (type)
15757 = XOBNEWVEC (&objfile->objfile_obstack,
15758 struct symbol *,
15759 TYPE_N_TEMPLATE_ARGUMENTS (type));
15760 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15761 template_args.data (),
15762 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15763 * sizeof (struct symbol *)));
15764 }
15765
15766 /* Attach fields and member functions to the type. */
15767 if (fi.nfields () > 0)
15768 dwarf2_attach_fields_to_type (&fi, type, cu);
15769 if (!fi.fnfieldlists.empty ())
15770 {
15771 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15772
15773 /* Get the type which refers to the base class (possibly this
15774 class itself) which contains the vtable pointer for the current
15775 class from the DW_AT_containing_type attribute. This use of
15776 DW_AT_containing_type is a GNU extension. */
15777
15778 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15779 {
15780 struct type *t = die_containing_type (die, cu);
15781
15782 set_type_vptr_basetype (type, t);
15783 if (type == t)
15784 {
15785 int i;
15786
15787 /* Our own class provides vtbl ptr. */
15788 for (i = t->num_fields () - 1;
15789 i >= TYPE_N_BASECLASSES (t);
15790 --i)
15791 {
15792 const char *fieldname = TYPE_FIELD_NAME (t, i);
15793
15794 if (is_vtable_name (fieldname, cu))
15795 {
15796 set_type_vptr_fieldno (type, i);
15797 break;
15798 }
15799 }
15800
15801 /* Complain if virtual function table field not found. */
15802 if (i < TYPE_N_BASECLASSES (t))
15803 complaint (_("virtual function table pointer "
15804 "not found when defining class '%s'"),
15805 type->name () ? type->name () : "");
15806 }
15807 else
15808 {
15809 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15810 }
15811 }
15812 else if (cu->producer
15813 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15814 {
15815 /* The IBM XLC compiler does not provide direct indication
15816 of the containing type, but the vtable pointer is
15817 always named __vfp. */
15818
15819 int i;
15820
15821 for (i = type->num_fields () - 1;
15822 i >= TYPE_N_BASECLASSES (type);
15823 --i)
15824 {
15825 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15826 {
15827 set_type_vptr_fieldno (type, i);
15828 set_type_vptr_basetype (type, type);
15829 break;
15830 }
15831 }
15832 }
15833 }
15834
15835 /* Copy fi.typedef_field_list linked list elements content into the
15836 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15837 if (!fi.typedef_field_list.empty ())
15838 {
15839 int count = fi.typedef_field_list.size ();
15840
15841 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15842 TYPE_TYPEDEF_FIELD_ARRAY (type)
15843 = ((struct decl_field *)
15844 TYPE_ALLOC (type,
15845 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15846 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15847
15848 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15849 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15850 }
15851
15852 /* Copy fi.nested_types_list linked list elements content into the
15853 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15854 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15855 {
15856 int count = fi.nested_types_list.size ();
15857
15858 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15859 TYPE_NESTED_TYPES_ARRAY (type)
15860 = ((struct decl_field *)
15861 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15862 TYPE_NESTED_TYPES_COUNT (type) = count;
15863
15864 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15865 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15866 }
15867 }
15868
15869 quirk_gcc_member_function_pointer (type, objfile);
15870 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15871 cu->rust_unions.push_back (type);
15872
15873 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15874 snapshots) has been known to create a die giving a declaration
15875 for a class that has, as a child, a die giving a definition for a
15876 nested class. So we have to process our children even if the
15877 current die is a declaration. Normally, of course, a declaration
15878 won't have any children at all. */
15879
15880 child_die = die->child;
15881
15882 while (child_die != NULL && child_die->tag)
15883 {
15884 if (child_die->tag == DW_TAG_member
15885 || child_die->tag == DW_TAG_variable
15886 || child_die->tag == DW_TAG_inheritance
15887 || child_die->tag == DW_TAG_template_value_param
15888 || child_die->tag == DW_TAG_template_type_param)
15889 {
15890 /* Do nothing. */
15891 }
15892 else
15893 process_die (child_die, cu);
15894
15895 child_die = child_die->sibling;
15896 }
15897
15898 /* Do not consider external references. According to the DWARF standard,
15899 these DIEs are identified by the fact that they have no byte_size
15900 attribute, and a declaration attribute. */
15901 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15902 || !die_is_declaration (die, cu)
15903 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15904 {
15905 struct symbol *sym = new_symbol (die, type, cu);
15906
15907 if (has_template_parameters)
15908 {
15909 struct symtab *symtab;
15910 if (sym != nullptr)
15911 symtab = symbol_symtab (sym);
15912 else if (cu->line_header != nullptr)
15913 {
15914 /* Any related symtab will do. */
15915 symtab
15916 = cu->line_header->file_names ()[0].symtab;
15917 }
15918 else
15919 {
15920 symtab = nullptr;
15921 complaint (_("could not find suitable "
15922 "symtab for template parameter"
15923 " - DIE at %s [in module %s]"),
15924 sect_offset_str (die->sect_off),
15925 objfile_name (objfile));
15926 }
15927
15928 if (symtab != nullptr)
15929 {
15930 /* Make sure that the symtab is set on the new symbols.
15931 Even though they don't appear in this symtab directly,
15932 other parts of gdb assume that symbols do, and this is
15933 reasonably true. */
15934 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15935 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15936 }
15937 }
15938 }
15939}
15940
15941/* Assuming DIE is an enumeration type, and TYPE is its associated
15942 type, update TYPE using some information only available in DIE's
15943 children. In particular, the fields are computed. */
15944
15945static void
15946update_enumeration_type_from_children (struct die_info *die,
15947 struct type *type,
15948 struct dwarf2_cu *cu)
15949{
15950 struct die_info *child_die;
15951 int unsigned_enum = 1;
15952 int flag_enum = 1;
15953
15954 auto_obstack obstack;
15955 std::vector<struct field> fields;
15956
15957 for (child_die = die->child;
15958 child_die != NULL && child_die->tag;
15959 child_die = child_die->sibling)
15960 {
15961 struct attribute *attr;
15962 LONGEST value;
15963 const gdb_byte *bytes;
15964 struct dwarf2_locexpr_baton *baton;
15965 const char *name;
15966
15967 if (child_die->tag != DW_TAG_enumerator)
15968 continue;
15969
15970 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15971 if (attr == NULL)
15972 continue;
15973
15974 name = dwarf2_name (child_die, cu);
15975 if (name == NULL)
15976 name = "<anonymous enumerator>";
15977
15978 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15979 &value, &bytes, &baton);
15980 if (value < 0)
15981 {
15982 unsigned_enum = 0;
15983 flag_enum = 0;
15984 }
15985 else
15986 {
15987 if (count_one_bits_ll (value) >= 2)
15988 flag_enum = 0;
15989 }
15990
15991 fields.emplace_back ();
15992 struct field &field = fields.back ();
15993 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15994 SET_FIELD_ENUMVAL (field, value);
15995 }
15996
15997 if (!fields.empty ())
15998 {
15999 type->set_num_fields (fields.size ());
16000 type->set_fields
16001 ((struct field *)
16002 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16003 memcpy (type->fields (), fields.data (),
16004 sizeof (struct field) * fields.size ());
16005 }
16006
16007 if (unsigned_enum)
16008 TYPE_UNSIGNED (type) = 1;
16009 if (flag_enum)
16010 TYPE_FLAG_ENUM (type) = 1;
16011}
16012
16013/* Given a DW_AT_enumeration_type die, set its type. We do not
16014 complete the type's fields yet, or create any symbols. */
16015
16016static struct type *
16017read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16018{
16019 struct objfile *objfile = cu->per_objfile->objfile;
16020 struct type *type;
16021 struct attribute *attr;
16022 const char *name;
16023
16024 /* If the definition of this type lives in .debug_types, read that type.
16025 Don't follow DW_AT_specification though, that will take us back up
16026 the chain and we want to go down. */
16027 attr = die->attr (DW_AT_signature);
16028 if (attr != nullptr)
16029 {
16030 type = get_DW_AT_signature_type (die, attr, cu);
16031
16032 /* The type's CU may not be the same as CU.
16033 Ensure TYPE is recorded with CU in die_type_hash. */
16034 return set_die_type (die, type, cu);
16035 }
16036
16037 type = alloc_type (objfile);
16038
16039 type->set_code (TYPE_CODE_ENUM);
16040 name = dwarf2_full_name (NULL, die, cu);
16041 if (name != NULL)
16042 type->set_name (name);
16043
16044 attr = dwarf2_attr (die, DW_AT_type, cu);
16045 if (attr != NULL)
16046 {
16047 struct type *underlying_type = die_type (die, cu);
16048
16049 TYPE_TARGET_TYPE (type) = underlying_type;
16050 }
16051
16052 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16053 if (attr != nullptr)
16054 {
16055 TYPE_LENGTH (type) = DW_UNSND (attr);
16056 }
16057 else
16058 {
16059 TYPE_LENGTH (type) = 0;
16060 }
16061
16062 maybe_set_alignment (cu, die, type);
16063
16064 /* The enumeration DIE can be incomplete. In Ada, any type can be
16065 declared as private in the package spec, and then defined only
16066 inside the package body. Such types are known as Taft Amendment
16067 Types. When another package uses such a type, an incomplete DIE
16068 may be generated by the compiler. */
16069 if (die_is_declaration (die, cu))
16070 TYPE_STUB (type) = 1;
16071
16072 /* If this type has an underlying type that is not a stub, then we
16073 may use its attributes. We always use the "unsigned" attribute
16074 in this situation, because ordinarily we guess whether the type
16075 is unsigned -- but the guess can be wrong and the underlying type
16076 can tell us the reality. However, we defer to a local size
16077 attribute if one exists, because this lets the compiler override
16078 the underlying type if needed. */
16079 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16080 {
16081 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16082 underlying_type = check_typedef (underlying_type);
16083 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16084 if (TYPE_LENGTH (type) == 0)
16085 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16086 if (TYPE_RAW_ALIGN (type) == 0
16087 && TYPE_RAW_ALIGN (underlying_type) != 0)
16088 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16089 }
16090
16091 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16092
16093 set_die_type (die, type, cu);
16094
16095 /* Finish the creation of this type by using the enum's children.
16096 Note that, as usual, this must come after set_die_type to avoid
16097 infinite recursion when trying to compute the names of the
16098 enumerators. */
16099 update_enumeration_type_from_children (die, type, cu);
16100
16101 return type;
16102}
16103
16104/* Given a pointer to a die which begins an enumeration, process all
16105 the dies that define the members of the enumeration, and create the
16106 symbol for the enumeration type.
16107
16108 NOTE: We reverse the order of the element list. */
16109
16110static void
16111process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16112{
16113 struct type *this_type;
16114
16115 this_type = get_die_type (die, cu);
16116 if (this_type == NULL)
16117 this_type = read_enumeration_type (die, cu);
16118
16119 if (die->child != NULL)
16120 {
16121 struct die_info *child_die;
16122 const char *name;
16123
16124 child_die = die->child;
16125 while (child_die && child_die->tag)
16126 {
16127 if (child_die->tag != DW_TAG_enumerator)
16128 {
16129 process_die (child_die, cu);
16130 }
16131 else
16132 {
16133 name = dwarf2_name (child_die, cu);
16134 if (name)
16135 new_symbol (child_die, this_type, cu);
16136 }
16137
16138 child_die = child_die->sibling;
16139 }
16140 }
16141
16142 /* If we are reading an enum from a .debug_types unit, and the enum
16143 is a declaration, and the enum is not the signatured type in the
16144 unit, then we do not want to add a symbol for it. Adding a
16145 symbol would in some cases obscure the true definition of the
16146 enum, giving users an incomplete type when the definition is
16147 actually available. Note that we do not want to do this for all
16148 enums which are just declarations, because C++0x allows forward
16149 enum declarations. */
16150 if (cu->per_cu->is_debug_types
16151 && die_is_declaration (die, cu))
16152 {
16153 struct signatured_type *sig_type;
16154
16155 sig_type = (struct signatured_type *) cu->per_cu;
16156 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16157 if (sig_type->type_offset_in_section != die->sect_off)
16158 return;
16159 }
16160
16161 new_symbol (die, this_type, cu);
16162}
16163
16164/* Extract all information from a DW_TAG_array_type DIE and put it in
16165 the DIE's type field. For now, this only handles one dimensional
16166 arrays. */
16167
16168static struct type *
16169read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16170{
16171 struct objfile *objfile = cu->per_objfile->objfile;
16172 struct die_info *child_die;
16173 struct type *type;
16174 struct type *element_type, *range_type, *index_type;
16175 struct attribute *attr;
16176 const char *name;
16177 struct dynamic_prop *byte_stride_prop = NULL;
16178 unsigned int bit_stride = 0;
16179
16180 element_type = die_type (die, cu);
16181
16182 /* The die_type call above may have already set the type for this DIE. */
16183 type = get_die_type (die, cu);
16184 if (type)
16185 return type;
16186
16187 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16188 if (attr != NULL)
16189 {
16190 int stride_ok;
16191 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16192
16193 byte_stride_prop
16194 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16195 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16196 prop_type);
16197 if (!stride_ok)
16198 {
16199 complaint (_("unable to read array DW_AT_byte_stride "
16200 " - DIE at %s [in module %s]"),
16201 sect_offset_str (die->sect_off),
16202 objfile_name (cu->per_objfile->objfile));
16203 /* Ignore this attribute. We will likely not be able to print
16204 arrays of this type correctly, but there is little we can do
16205 to help if we cannot read the attribute's value. */
16206 byte_stride_prop = NULL;
16207 }
16208 }
16209
16210 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16211 if (attr != NULL)
16212 bit_stride = DW_UNSND (attr);
16213
16214 /* Irix 6.2 native cc creates array types without children for
16215 arrays with unspecified length. */
16216 if (die->child == NULL)
16217 {
16218 index_type = objfile_type (objfile)->builtin_int;
16219 range_type = create_static_range_type (NULL, index_type, 0, -1);
16220 type = create_array_type_with_stride (NULL, element_type, range_type,
16221 byte_stride_prop, bit_stride);
16222 return set_die_type (die, type, cu);
16223 }
16224
16225 std::vector<struct type *> range_types;
16226 child_die = die->child;
16227 while (child_die && child_die->tag)
16228 {
16229 if (child_die->tag == DW_TAG_subrange_type)
16230 {
16231 struct type *child_type = read_type_die (child_die, cu);
16232
16233 if (child_type != NULL)
16234 {
16235 /* The range type was succesfully read. Save it for the
16236 array type creation. */
16237 range_types.push_back (child_type);
16238 }
16239 }
16240 child_die = child_die->sibling;
16241 }
16242
16243 /* Dwarf2 dimensions are output from left to right, create the
16244 necessary array types in backwards order. */
16245
16246 type = element_type;
16247
16248 if (read_array_order (die, cu) == DW_ORD_col_major)
16249 {
16250 int i = 0;
16251
16252 while (i < range_types.size ())
16253 type = create_array_type_with_stride (NULL, type, range_types[i++],
16254 byte_stride_prop, bit_stride);
16255 }
16256 else
16257 {
16258 size_t ndim = range_types.size ();
16259 while (ndim-- > 0)
16260 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16261 byte_stride_prop, bit_stride);
16262 }
16263
16264 /* Understand Dwarf2 support for vector types (like they occur on
16265 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16266 array type. This is not part of the Dwarf2/3 standard yet, but a
16267 custom vendor extension. The main difference between a regular
16268 array and the vector variant is that vectors are passed by value
16269 to functions. */
16270 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16271 if (attr != nullptr)
16272 make_vector_type (type);
16273
16274 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16275 implementation may choose to implement triple vectors using this
16276 attribute. */
16277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16278 if (attr != nullptr)
16279 {
16280 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16281 TYPE_LENGTH (type) = DW_UNSND (attr);
16282 else
16283 complaint (_("DW_AT_byte_size for array type smaller "
16284 "than the total size of elements"));
16285 }
16286
16287 name = dwarf2_name (die, cu);
16288 if (name)
16289 type->set_name (name);
16290
16291 maybe_set_alignment (cu, die, type);
16292
16293 /* Install the type in the die. */
16294 set_die_type (die, type, cu);
16295
16296 /* set_die_type should be already done. */
16297 set_descriptive_type (type, die, cu);
16298
16299 return type;
16300}
16301
16302static enum dwarf_array_dim_ordering
16303read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16304{
16305 struct attribute *attr;
16306
16307 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16308
16309 if (attr != nullptr)
16310 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16311
16312 /* GNU F77 is a special case, as at 08/2004 array type info is the
16313 opposite order to the dwarf2 specification, but data is still
16314 laid out as per normal fortran.
16315
16316 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16317 version checking. */
16318
16319 if (cu->language == language_fortran
16320 && cu->producer && strstr (cu->producer, "GNU F77"))
16321 {
16322 return DW_ORD_row_major;
16323 }
16324
16325 switch (cu->language_defn->la_array_ordering)
16326 {
16327 case array_column_major:
16328 return DW_ORD_col_major;
16329 case array_row_major:
16330 default:
16331 return DW_ORD_row_major;
16332 };
16333}
16334
16335/* Extract all information from a DW_TAG_set_type DIE and put it in
16336 the DIE's type field. */
16337
16338static struct type *
16339read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16340{
16341 struct type *domain_type, *set_type;
16342 struct attribute *attr;
16343
16344 domain_type = die_type (die, cu);
16345
16346 /* The die_type call above may have already set the type for this DIE. */
16347 set_type = get_die_type (die, cu);
16348 if (set_type)
16349 return set_type;
16350
16351 set_type = create_set_type (NULL, domain_type);
16352
16353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16354 if (attr != nullptr)
16355 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16356
16357 maybe_set_alignment (cu, die, set_type);
16358
16359 return set_die_type (die, set_type, cu);
16360}
16361
16362/* A helper for read_common_block that creates a locexpr baton.
16363 SYM is the symbol which we are marking as computed.
16364 COMMON_DIE is the DIE for the common block.
16365 COMMON_LOC is the location expression attribute for the common
16366 block itself.
16367 MEMBER_LOC is the location expression attribute for the particular
16368 member of the common block that we are processing.
16369 CU is the CU from which the above come. */
16370
16371static void
16372mark_common_block_symbol_computed (struct symbol *sym,
16373 struct die_info *common_die,
16374 struct attribute *common_loc,
16375 struct attribute *member_loc,
16376 struct dwarf2_cu *cu)
16377{
16378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16379 struct objfile *objfile = per_objfile->objfile;
16380 struct dwarf2_locexpr_baton *baton;
16381 gdb_byte *ptr;
16382 unsigned int cu_off;
16383 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16384 LONGEST offset = 0;
16385
16386 gdb_assert (common_loc && member_loc);
16387 gdb_assert (common_loc->form_is_block ());
16388 gdb_assert (member_loc->form_is_block ()
16389 || member_loc->form_is_constant ());
16390
16391 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16392 baton->per_objfile = per_objfile;
16393 baton->per_cu = cu->per_cu;
16394 gdb_assert (baton->per_cu);
16395
16396 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16397
16398 if (member_loc->form_is_constant ())
16399 {
16400 offset = member_loc->constant_value (0);
16401 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16402 }
16403 else
16404 baton->size += DW_BLOCK (member_loc)->size;
16405
16406 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16407 baton->data = ptr;
16408
16409 *ptr++ = DW_OP_call4;
16410 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16411 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16412 ptr += 4;
16413
16414 if (member_loc->form_is_constant ())
16415 {
16416 *ptr++ = DW_OP_addr;
16417 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16418 ptr += cu->header.addr_size;
16419 }
16420 else
16421 {
16422 /* We have to copy the data here, because DW_OP_call4 will only
16423 use a DW_AT_location attribute. */
16424 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16425 ptr += DW_BLOCK (member_loc)->size;
16426 }
16427
16428 *ptr++ = DW_OP_plus;
16429 gdb_assert (ptr - baton->data == baton->size);
16430
16431 SYMBOL_LOCATION_BATON (sym) = baton;
16432 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16433}
16434
16435/* Create appropriate locally-scoped variables for all the
16436 DW_TAG_common_block entries. Also create a struct common_block
16437 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16438 is used to separate the common blocks name namespace from regular
16439 variable names. */
16440
16441static void
16442read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16443{
16444 struct attribute *attr;
16445
16446 attr = dwarf2_attr (die, DW_AT_location, cu);
16447 if (attr != nullptr)
16448 {
16449 /* Support the .debug_loc offsets. */
16450 if (attr->form_is_block ())
16451 {
16452 /* Ok. */
16453 }
16454 else if (attr->form_is_section_offset ())
16455 {
16456 dwarf2_complex_location_expr_complaint ();
16457 attr = NULL;
16458 }
16459 else
16460 {
16461 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16462 "common block member");
16463 attr = NULL;
16464 }
16465 }
16466
16467 if (die->child != NULL)
16468 {
16469 struct objfile *objfile = cu->per_objfile->objfile;
16470 struct die_info *child_die;
16471 size_t n_entries = 0, size;
16472 struct common_block *common_block;
16473 struct symbol *sym;
16474
16475 for (child_die = die->child;
16476 child_die && child_die->tag;
16477 child_die = child_die->sibling)
16478 ++n_entries;
16479
16480 size = (sizeof (struct common_block)
16481 + (n_entries - 1) * sizeof (struct symbol *));
16482 common_block
16483 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16484 size);
16485 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16486 common_block->n_entries = 0;
16487
16488 for (child_die = die->child;
16489 child_die && child_die->tag;
16490 child_die = child_die->sibling)
16491 {
16492 /* Create the symbol in the DW_TAG_common_block block in the current
16493 symbol scope. */
16494 sym = new_symbol (child_die, NULL, cu);
16495 if (sym != NULL)
16496 {
16497 struct attribute *member_loc;
16498
16499 common_block->contents[common_block->n_entries++] = sym;
16500
16501 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16502 cu);
16503 if (member_loc)
16504 {
16505 /* GDB has handled this for a long time, but it is
16506 not specified by DWARF. It seems to have been
16507 emitted by gfortran at least as recently as:
16508 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16509 complaint (_("Variable in common block has "
16510 "DW_AT_data_member_location "
16511 "- DIE at %s [in module %s]"),
16512 sect_offset_str (child_die->sect_off),
16513 objfile_name (objfile));
16514
16515 if (member_loc->form_is_section_offset ())
16516 dwarf2_complex_location_expr_complaint ();
16517 else if (member_loc->form_is_constant ()
16518 || member_loc->form_is_block ())
16519 {
16520 if (attr != nullptr)
16521 mark_common_block_symbol_computed (sym, die, attr,
16522 member_loc, cu);
16523 }
16524 else
16525 dwarf2_complex_location_expr_complaint ();
16526 }
16527 }
16528 }
16529
16530 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16531 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16532 }
16533}
16534
16535/* Create a type for a C++ namespace. */
16536
16537static struct type *
16538read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16539{
16540 struct objfile *objfile = cu->per_objfile->objfile;
16541 const char *previous_prefix, *name;
16542 int is_anonymous;
16543 struct type *type;
16544
16545 /* For extensions, reuse the type of the original namespace. */
16546 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16547 {
16548 struct die_info *ext_die;
16549 struct dwarf2_cu *ext_cu = cu;
16550
16551 ext_die = dwarf2_extension (die, &ext_cu);
16552 type = read_type_die (ext_die, ext_cu);
16553
16554 /* EXT_CU may not be the same as CU.
16555 Ensure TYPE is recorded with CU in die_type_hash. */
16556 return set_die_type (die, type, cu);
16557 }
16558
16559 name = namespace_name (die, &is_anonymous, cu);
16560
16561 /* Now build the name of the current namespace. */
16562
16563 previous_prefix = determine_prefix (die, cu);
16564 if (previous_prefix[0] != '\0')
16565 name = typename_concat (&objfile->objfile_obstack,
16566 previous_prefix, name, 0, cu);
16567
16568 /* Create the type. */
16569 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16570
16571 return set_die_type (die, type, cu);
16572}
16573
16574/* Read a namespace scope. */
16575
16576static void
16577read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16578{
16579 struct objfile *objfile = cu->per_objfile->objfile;
16580 int is_anonymous;
16581
16582 /* Add a symbol associated to this if we haven't seen the namespace
16583 before. Also, add a using directive if it's an anonymous
16584 namespace. */
16585
16586 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16587 {
16588 struct type *type;
16589
16590 type = read_type_die (die, cu);
16591 new_symbol (die, type, cu);
16592
16593 namespace_name (die, &is_anonymous, cu);
16594 if (is_anonymous)
16595 {
16596 const char *previous_prefix = determine_prefix (die, cu);
16597
16598 std::vector<const char *> excludes;
16599 add_using_directive (using_directives (cu),
16600 previous_prefix, type->name (), NULL,
16601 NULL, excludes, 0, &objfile->objfile_obstack);
16602 }
16603 }
16604
16605 if (die->child != NULL)
16606 {
16607 struct die_info *child_die = die->child;
16608
16609 while (child_die && child_die->tag)
16610 {
16611 process_die (child_die, cu);
16612 child_die = child_die->sibling;
16613 }
16614 }
16615}
16616
16617/* Read a Fortran module as type. This DIE can be only a declaration used for
16618 imported module. Still we need that type as local Fortran "use ... only"
16619 declaration imports depend on the created type in determine_prefix. */
16620
16621static struct type *
16622read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16623{
16624 struct objfile *objfile = cu->per_objfile->objfile;
16625 const char *module_name;
16626 struct type *type;
16627
16628 module_name = dwarf2_name (die, cu);
16629 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16630
16631 return set_die_type (die, type, cu);
16632}
16633
16634/* Read a Fortran module. */
16635
16636static void
16637read_module (struct die_info *die, struct dwarf2_cu *cu)
16638{
16639 struct die_info *child_die = die->child;
16640 struct type *type;
16641
16642 type = read_type_die (die, cu);
16643 new_symbol (die, type, cu);
16644
16645 while (child_die && child_die->tag)
16646 {
16647 process_die (child_die, cu);
16648 child_die = child_die->sibling;
16649 }
16650}
16651
16652/* Return the name of the namespace represented by DIE. Set
16653 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16654 namespace. */
16655
16656static const char *
16657namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16658{
16659 struct die_info *current_die;
16660 const char *name = NULL;
16661
16662 /* Loop through the extensions until we find a name. */
16663
16664 for (current_die = die;
16665 current_die != NULL;
16666 current_die = dwarf2_extension (die, &cu))
16667 {
16668 /* We don't use dwarf2_name here so that we can detect the absence
16669 of a name -> anonymous namespace. */
16670 name = dwarf2_string_attr (die, DW_AT_name, cu);
16671
16672 if (name != NULL)
16673 break;
16674 }
16675
16676 /* Is it an anonymous namespace? */
16677
16678 *is_anonymous = (name == NULL);
16679 if (*is_anonymous)
16680 name = CP_ANONYMOUS_NAMESPACE_STR;
16681
16682 return name;
16683}
16684
16685/* Extract all information from a DW_TAG_pointer_type DIE and add to
16686 the user defined type vector. */
16687
16688static struct type *
16689read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16690{
16691 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16692 struct comp_unit_head *cu_header = &cu->header;
16693 struct type *type;
16694 struct attribute *attr_byte_size;
16695 struct attribute *attr_address_class;
16696 int byte_size, addr_class;
16697 struct type *target_type;
16698
16699 target_type = die_type (die, cu);
16700
16701 /* The die_type call above may have already set the type for this DIE. */
16702 type = get_die_type (die, cu);
16703 if (type)
16704 return type;
16705
16706 type = lookup_pointer_type (target_type);
16707
16708 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16709 if (attr_byte_size)
16710 byte_size = DW_UNSND (attr_byte_size);
16711 else
16712 byte_size = cu_header->addr_size;
16713
16714 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16715 if (attr_address_class)
16716 addr_class = DW_UNSND (attr_address_class);
16717 else
16718 addr_class = DW_ADDR_none;
16719
16720 ULONGEST alignment = get_alignment (cu, die);
16721
16722 /* If the pointer size, alignment, or address class is different
16723 than the default, create a type variant marked as such and set
16724 the length accordingly. */
16725 if (TYPE_LENGTH (type) != byte_size
16726 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16727 && alignment != TYPE_RAW_ALIGN (type))
16728 || addr_class != DW_ADDR_none)
16729 {
16730 if (gdbarch_address_class_type_flags_p (gdbarch))
16731 {
16732 int type_flags;
16733
16734 type_flags = gdbarch_address_class_type_flags
16735 (gdbarch, byte_size, addr_class);
16736 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16737 == 0);
16738 type = make_type_with_address_space (type, type_flags);
16739 }
16740 else if (TYPE_LENGTH (type) != byte_size)
16741 {
16742 complaint (_("invalid pointer size %d"), byte_size);
16743 }
16744 else if (TYPE_RAW_ALIGN (type) != alignment)
16745 {
16746 complaint (_("Invalid DW_AT_alignment"
16747 " - DIE at %s [in module %s]"),
16748 sect_offset_str (die->sect_off),
16749 objfile_name (cu->per_objfile->objfile));
16750 }
16751 else
16752 {
16753 /* Should we also complain about unhandled address classes? */
16754 }
16755 }
16756
16757 TYPE_LENGTH (type) = byte_size;
16758 set_type_align (type, alignment);
16759 return set_die_type (die, type, cu);
16760}
16761
16762/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16763 the user defined type vector. */
16764
16765static struct type *
16766read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16767{
16768 struct type *type;
16769 struct type *to_type;
16770 struct type *domain;
16771
16772 to_type = die_type (die, cu);
16773 domain = die_containing_type (die, cu);
16774
16775 /* The calls above may have already set the type for this DIE. */
16776 type = get_die_type (die, cu);
16777 if (type)
16778 return type;
16779
16780 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16781 type = lookup_methodptr_type (to_type);
16782 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16783 {
16784 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16785
16786 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16787 to_type->fields (), to_type->num_fields (),
16788 TYPE_VARARGS (to_type));
16789 type = lookup_methodptr_type (new_type);
16790 }
16791 else
16792 type = lookup_memberptr_type (to_type, domain);
16793
16794 return set_die_type (die, type, cu);
16795}
16796
16797/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16798 the user defined type vector. */
16799
16800static struct type *
16801read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16802 enum type_code refcode)
16803{
16804 struct comp_unit_head *cu_header = &cu->header;
16805 struct type *type, *target_type;
16806 struct attribute *attr;
16807
16808 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16809
16810 target_type = die_type (die, cu);
16811
16812 /* The die_type call above may have already set the type for this DIE. */
16813 type = get_die_type (die, cu);
16814 if (type)
16815 return type;
16816
16817 type = lookup_reference_type (target_type, refcode);
16818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16819 if (attr != nullptr)
16820 {
16821 TYPE_LENGTH (type) = DW_UNSND (attr);
16822 }
16823 else
16824 {
16825 TYPE_LENGTH (type) = cu_header->addr_size;
16826 }
16827 maybe_set_alignment (cu, die, type);
16828 return set_die_type (die, type, cu);
16829}
16830
16831/* Add the given cv-qualifiers to the element type of the array. GCC
16832 outputs DWARF type qualifiers that apply to an array, not the
16833 element type. But GDB relies on the array element type to carry
16834 the cv-qualifiers. This mimics section 6.7.3 of the C99
16835 specification. */
16836
16837static struct type *
16838add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16839 struct type *base_type, int cnst, int voltl)
16840{
16841 struct type *el_type, *inner_array;
16842
16843 base_type = copy_type (base_type);
16844 inner_array = base_type;
16845
16846 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16847 {
16848 TYPE_TARGET_TYPE (inner_array) =
16849 copy_type (TYPE_TARGET_TYPE (inner_array));
16850 inner_array = TYPE_TARGET_TYPE (inner_array);
16851 }
16852
16853 el_type = TYPE_TARGET_TYPE (inner_array);
16854 cnst |= TYPE_CONST (el_type);
16855 voltl |= TYPE_VOLATILE (el_type);
16856 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16857
16858 return set_die_type (die, base_type, cu);
16859}
16860
16861static struct type *
16862read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16863{
16864 struct type *base_type, *cv_type;
16865
16866 base_type = die_type (die, cu);
16867
16868 /* The die_type call above may have already set the type for this DIE. */
16869 cv_type = get_die_type (die, cu);
16870 if (cv_type)
16871 return cv_type;
16872
16873 /* In case the const qualifier is applied to an array type, the element type
16874 is so qualified, not the array type (section 6.7.3 of C99). */
16875 if (base_type->code () == TYPE_CODE_ARRAY)
16876 return add_array_cv_type (die, cu, base_type, 1, 0);
16877
16878 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16879 return set_die_type (die, cv_type, cu);
16880}
16881
16882static struct type *
16883read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16884{
16885 struct type *base_type, *cv_type;
16886
16887 base_type = die_type (die, cu);
16888
16889 /* The die_type call above may have already set the type for this DIE. */
16890 cv_type = get_die_type (die, cu);
16891 if (cv_type)
16892 return cv_type;
16893
16894 /* In case the volatile qualifier is applied to an array type, the
16895 element type is so qualified, not the array type (section 6.7.3
16896 of C99). */
16897 if (base_type->code () == TYPE_CODE_ARRAY)
16898 return add_array_cv_type (die, cu, base_type, 0, 1);
16899
16900 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16901 return set_die_type (die, cv_type, cu);
16902}
16903
16904/* Handle DW_TAG_restrict_type. */
16905
16906static struct type *
16907read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16908{
16909 struct type *base_type, *cv_type;
16910
16911 base_type = die_type (die, cu);
16912
16913 /* The die_type call above may have already set the type for this DIE. */
16914 cv_type = get_die_type (die, cu);
16915 if (cv_type)
16916 return cv_type;
16917
16918 cv_type = make_restrict_type (base_type);
16919 return set_die_type (die, cv_type, cu);
16920}
16921
16922/* Handle DW_TAG_atomic_type. */
16923
16924static struct type *
16925read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16926{
16927 struct type *base_type, *cv_type;
16928
16929 base_type = die_type (die, cu);
16930
16931 /* The die_type call above may have already set the type for this DIE. */
16932 cv_type = get_die_type (die, cu);
16933 if (cv_type)
16934 return cv_type;
16935
16936 cv_type = make_atomic_type (base_type);
16937 return set_die_type (die, cv_type, cu);
16938}
16939
16940/* Extract all information from a DW_TAG_string_type DIE and add to
16941 the user defined type vector. It isn't really a user defined type,
16942 but it behaves like one, with other DIE's using an AT_user_def_type
16943 attribute to reference it. */
16944
16945static struct type *
16946read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16947{
16948 struct objfile *objfile = cu->per_objfile->objfile;
16949 struct gdbarch *gdbarch = objfile->arch ();
16950 struct type *type, *range_type, *index_type, *char_type;
16951 struct attribute *attr;
16952 struct dynamic_prop prop;
16953 bool length_is_constant = true;
16954 LONGEST length;
16955
16956 /* There are a couple of places where bit sizes might be made use of
16957 when parsing a DW_TAG_string_type, however, no producer that we know
16958 of make use of these. Handling bit sizes that are a multiple of the
16959 byte size is easy enough, but what about other bit sizes? Lets deal
16960 with that problem when we have to. Warn about these attributes being
16961 unsupported, then parse the type and ignore them like we always
16962 have. */
16963 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16964 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16965 {
16966 static bool warning_printed = false;
16967 if (!warning_printed)
16968 {
16969 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16970 "currently supported on DW_TAG_string_type."));
16971 warning_printed = true;
16972 }
16973 }
16974
16975 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16976 if (attr != nullptr && !attr->form_is_constant ())
16977 {
16978 /* The string length describes the location at which the length of
16979 the string can be found. The size of the length field can be
16980 specified with one of the attributes below. */
16981 struct type *prop_type;
16982 struct attribute *len
16983 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16984 if (len == nullptr)
16985 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16986 if (len != nullptr && len->form_is_constant ())
16987 {
16988 /* Pass 0 as the default as we know this attribute is constant
16989 and the default value will not be returned. */
16990 LONGEST sz = len->constant_value (0);
16991 prop_type = cu->per_cu->int_type (sz, true);
16992 }
16993 else
16994 {
16995 /* If the size is not specified then we assume it is the size of
16996 an address on this target. */
16997 prop_type = cu->per_cu->addr_sized_int_type (true);
16998 }
16999
17000 /* Convert the attribute into a dynamic property. */
17001 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17002 length = 1;
17003 else
17004 length_is_constant = false;
17005 }
17006 else if (attr != nullptr)
17007 {
17008 /* This DW_AT_string_length just contains the length with no
17009 indirection. There's no need to create a dynamic property in this
17010 case. Pass 0 for the default value as we know it will not be
17011 returned in this case. */
17012 length = attr->constant_value (0);
17013 }
17014 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17015 {
17016 /* We don't currently support non-constant byte sizes for strings. */
17017 length = attr->constant_value (1);
17018 }
17019 else
17020 {
17021 /* Use 1 as a fallback length if we have nothing else. */
17022 length = 1;
17023 }
17024
17025 index_type = objfile_type (objfile)->builtin_int;
17026 if (length_is_constant)
17027 range_type = create_static_range_type (NULL, index_type, 1, length);
17028 else
17029 {
17030 struct dynamic_prop low_bound;
17031
17032 low_bound.kind = PROP_CONST;
17033 low_bound.data.const_val = 1;
17034 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17035 }
17036 char_type = language_string_char_type (cu->language_defn, gdbarch);
17037 type = create_string_type (NULL, char_type, range_type);
17038
17039 return set_die_type (die, type, cu);
17040}
17041
17042/* Assuming that DIE corresponds to a function, returns nonzero
17043 if the function is prototyped. */
17044
17045static int
17046prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17047{
17048 struct attribute *attr;
17049
17050 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17051 if (attr && (DW_UNSND (attr) != 0))
17052 return 1;
17053
17054 /* The DWARF standard implies that the DW_AT_prototyped attribute
17055 is only meaningful for C, but the concept also extends to other
17056 languages that allow unprototyped functions (Eg: Objective C).
17057 For all other languages, assume that functions are always
17058 prototyped. */
17059 if (cu->language != language_c
17060 && cu->language != language_objc
17061 && cu->language != language_opencl)
17062 return 1;
17063
17064 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17065 prototyped and unprototyped functions; default to prototyped,
17066 since that is more common in modern code (and RealView warns
17067 about unprototyped functions). */
17068 if (producer_is_realview (cu->producer))
17069 return 1;
17070
17071 return 0;
17072}
17073
17074/* Handle DIES due to C code like:
17075
17076 struct foo
17077 {
17078 int (*funcp)(int a, long l);
17079 int b;
17080 };
17081
17082 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17083
17084static struct type *
17085read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17086{
17087 struct objfile *objfile = cu->per_objfile->objfile;
17088 struct type *type; /* Type that this function returns. */
17089 struct type *ftype; /* Function that returns above type. */
17090 struct attribute *attr;
17091
17092 type = die_type (die, cu);
17093
17094 /* The die_type call above may have already set the type for this DIE. */
17095 ftype = get_die_type (die, cu);
17096 if (ftype)
17097 return ftype;
17098
17099 ftype = lookup_function_type (type);
17100
17101 if (prototyped_function_p (die, cu))
17102 TYPE_PROTOTYPED (ftype) = 1;
17103
17104 /* Store the calling convention in the type if it's available in
17105 the subroutine die. Otherwise set the calling convention to
17106 the default value DW_CC_normal. */
17107 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17108 if (attr != nullptr
17109 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17110 TYPE_CALLING_CONVENTION (ftype)
17111 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17112 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17113 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17114 else
17115 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17116
17117 /* Record whether the function returns normally to its caller or not
17118 if the DWARF producer set that information. */
17119 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17120 if (attr && (DW_UNSND (attr) != 0))
17121 TYPE_NO_RETURN (ftype) = 1;
17122
17123 /* We need to add the subroutine type to the die immediately so
17124 we don't infinitely recurse when dealing with parameters
17125 declared as the same subroutine type. */
17126 set_die_type (die, ftype, cu);
17127
17128 if (die->child != NULL)
17129 {
17130 struct type *void_type = objfile_type (objfile)->builtin_void;
17131 struct die_info *child_die;
17132 int nparams, iparams;
17133
17134 /* Count the number of parameters.
17135 FIXME: GDB currently ignores vararg functions, but knows about
17136 vararg member functions. */
17137 nparams = 0;
17138 child_die = die->child;
17139 while (child_die && child_die->tag)
17140 {
17141 if (child_die->tag == DW_TAG_formal_parameter)
17142 nparams++;
17143 else if (child_die->tag == DW_TAG_unspecified_parameters)
17144 TYPE_VARARGS (ftype) = 1;
17145 child_die = child_die->sibling;
17146 }
17147
17148 /* Allocate storage for parameters and fill them in. */
17149 ftype->set_num_fields (nparams);
17150 ftype->set_fields
17151 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17152
17153 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17154 even if we error out during the parameters reading below. */
17155 for (iparams = 0; iparams < nparams; iparams++)
17156 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17157
17158 iparams = 0;
17159 child_die = die->child;
17160 while (child_die && child_die->tag)
17161 {
17162 if (child_die->tag == DW_TAG_formal_parameter)
17163 {
17164 struct type *arg_type;
17165
17166 /* DWARF version 2 has no clean way to discern C++
17167 static and non-static member functions. G++ helps
17168 GDB by marking the first parameter for non-static
17169 member functions (which is the this pointer) as
17170 artificial. We pass this information to
17171 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17172
17173 DWARF version 3 added DW_AT_object_pointer, which GCC
17174 4.5 does not yet generate. */
17175 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17176 if (attr != nullptr)
17177 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17178 else
17179 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17180 arg_type = die_type (child_die, cu);
17181
17182 /* RealView does not mark THIS as const, which the testsuite
17183 expects. GCC marks THIS as const in method definitions,
17184 but not in the class specifications (GCC PR 43053). */
17185 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17186 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17187 {
17188 int is_this = 0;
17189 struct dwarf2_cu *arg_cu = cu;
17190 const char *name = dwarf2_name (child_die, cu);
17191
17192 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17193 if (attr != nullptr)
17194 {
17195 /* If the compiler emits this, use it. */
17196 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17197 is_this = 1;
17198 }
17199 else if (name && strcmp (name, "this") == 0)
17200 /* Function definitions will have the argument names. */
17201 is_this = 1;
17202 else if (name == NULL && iparams == 0)
17203 /* Declarations may not have the names, so like
17204 elsewhere in GDB, assume an artificial first
17205 argument is "this". */
17206 is_this = 1;
17207
17208 if (is_this)
17209 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17210 arg_type, 0);
17211 }
17212
17213 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17214 iparams++;
17215 }
17216 child_die = child_die->sibling;
17217 }
17218 }
17219
17220 return ftype;
17221}
17222
17223static struct type *
17224read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17225{
17226 struct objfile *objfile = cu->per_objfile->objfile;
17227 const char *name = NULL;
17228 struct type *this_type, *target_type;
17229
17230 name = dwarf2_full_name (NULL, die, cu);
17231 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17232 TYPE_TARGET_STUB (this_type) = 1;
17233 set_die_type (die, this_type, cu);
17234 target_type = die_type (die, cu);
17235 if (target_type != this_type)
17236 TYPE_TARGET_TYPE (this_type) = target_type;
17237 else
17238 {
17239 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17240 spec and cause infinite loops in GDB. */
17241 complaint (_("Self-referential DW_TAG_typedef "
17242 "- DIE at %s [in module %s]"),
17243 sect_offset_str (die->sect_off), objfile_name (objfile));
17244 TYPE_TARGET_TYPE (this_type) = NULL;
17245 }
17246 if (name == NULL)
17247 {
17248 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17249 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17250 Handle these by just returning the target type, rather than
17251 constructing an anonymous typedef type and trying to handle this
17252 elsewhere. */
17253 set_die_type (die, target_type, cu);
17254 return target_type;
17255 }
17256 return this_type;
17257}
17258
17259/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17260 (which may be different from NAME) to the architecture back-end to allow
17261 it to guess the correct format if necessary. */
17262
17263static struct type *
17264dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17265 const char *name_hint, enum bfd_endian byte_order)
17266{
17267 struct gdbarch *gdbarch = objfile->arch ();
17268 const struct floatformat **format;
17269 struct type *type;
17270
17271 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17272 if (format)
17273 type = init_float_type (objfile, bits, name, format, byte_order);
17274 else
17275 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17276
17277 return type;
17278}
17279
17280/* Allocate an integer type of size BITS and name NAME. */
17281
17282static struct type *
17283dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17284 int bits, int unsigned_p, const char *name)
17285{
17286 struct type *type;
17287
17288 /* Versions of Intel's C Compiler generate an integer type called "void"
17289 instead of using DW_TAG_unspecified_type. This has been seen on
17290 at least versions 14, 17, and 18. */
17291 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17292 && strcmp (name, "void") == 0)
17293 type = objfile_type (objfile)->builtin_void;
17294 else
17295 type = init_integer_type (objfile, bits, unsigned_p, name);
17296
17297 return type;
17298}
17299
17300/* Initialise and return a floating point type of size BITS suitable for
17301 use as a component of a complex number. The NAME_HINT is passed through
17302 when initialising the floating point type and is the name of the complex
17303 type.
17304
17305 As DWARF doesn't currently provide an explicit name for the components
17306 of a complex number, but it can be helpful to have these components
17307 named, we try to select a suitable name based on the size of the
17308 component. */
17309static struct type *
17310dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17311 struct objfile *objfile,
17312 int bits, const char *name_hint,
17313 enum bfd_endian byte_order)
17314{
17315 gdbarch *gdbarch = objfile->arch ();
17316 struct type *tt = nullptr;
17317
17318 /* Try to find a suitable floating point builtin type of size BITS.
17319 We're going to use the name of this type as the name for the complex
17320 target type that we are about to create. */
17321 switch (cu->language)
17322 {
17323 case language_fortran:
17324 switch (bits)
17325 {
17326 case 32:
17327 tt = builtin_f_type (gdbarch)->builtin_real;
17328 break;
17329 case 64:
17330 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17331 break;
17332 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17333 case 128:
17334 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17335 break;
17336 }
17337 break;
17338 default:
17339 switch (bits)
17340 {
17341 case 32:
17342 tt = builtin_type (gdbarch)->builtin_float;
17343 break;
17344 case 64:
17345 tt = builtin_type (gdbarch)->builtin_double;
17346 break;
17347 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17348 case 128:
17349 tt = builtin_type (gdbarch)->builtin_long_double;
17350 break;
17351 }
17352 break;
17353 }
17354
17355 /* If the type we found doesn't match the size we were looking for, then
17356 pretend we didn't find a type at all, the complex target type we
17357 create will then be nameless. */
17358 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17359 tt = nullptr;
17360
17361 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17362 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17363}
17364
17365/* Find a representation of a given base type and install
17366 it in the TYPE field of the die. */
17367
17368static struct type *
17369read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17370{
17371 struct objfile *objfile = cu->per_objfile->objfile;
17372 struct type *type;
17373 struct attribute *attr;
17374 int encoding = 0, bits = 0;
17375 const char *name;
17376 gdbarch *arch;
17377
17378 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17379 if (attr != nullptr)
17380 encoding = DW_UNSND (attr);
17381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17382 if (attr != nullptr)
17383 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17384 name = dwarf2_name (die, cu);
17385 if (!name)
17386 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17387
17388 arch = objfile->arch ();
17389 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17390
17391 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17392 if (attr)
17393 {
17394 int endianity = DW_UNSND (attr);
17395
17396 switch (endianity)
17397 {
17398 case DW_END_big:
17399 byte_order = BFD_ENDIAN_BIG;
17400 break;
17401 case DW_END_little:
17402 byte_order = BFD_ENDIAN_LITTLE;
17403 break;
17404 default:
17405 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17406 break;
17407 }
17408 }
17409
17410 switch (encoding)
17411 {
17412 case DW_ATE_address:
17413 /* Turn DW_ATE_address into a void * pointer. */
17414 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17415 type = init_pointer_type (objfile, bits, name, type);
17416 break;
17417 case DW_ATE_boolean:
17418 type = init_boolean_type (objfile, bits, 1, name);
17419 break;
17420 case DW_ATE_complex_float:
17421 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17422 byte_order);
17423 if (type->code () == TYPE_CODE_ERROR)
17424 {
17425 if (name == nullptr)
17426 {
17427 struct obstack *obstack
17428 = &cu->per_objfile->objfile->objfile_obstack;
17429 name = obconcat (obstack, "_Complex ", type->name (),
17430 nullptr);
17431 }
17432 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17433 }
17434 else
17435 type = init_complex_type (name, type);
17436 break;
17437 case DW_ATE_decimal_float:
17438 type = init_decfloat_type (objfile, bits, name);
17439 break;
17440 case DW_ATE_float:
17441 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17442 break;
17443 case DW_ATE_signed:
17444 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17445 break;
17446 case DW_ATE_unsigned:
17447 if (cu->language == language_fortran
17448 && name
17449 && startswith (name, "character("))
17450 type = init_character_type (objfile, bits, 1, name);
17451 else
17452 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17453 break;
17454 case DW_ATE_signed_char:
17455 if (cu->language == language_ada || cu->language == language_m2
17456 || cu->language == language_pascal
17457 || cu->language == language_fortran)
17458 type = init_character_type (objfile, bits, 0, name);
17459 else
17460 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17461 break;
17462 case DW_ATE_unsigned_char:
17463 if (cu->language == language_ada || cu->language == language_m2
17464 || cu->language == language_pascal
17465 || cu->language == language_fortran
17466 || cu->language == language_rust)
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
17469 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17470 break;
17471 case DW_ATE_UTF:
17472 {
17473 if (bits == 16)
17474 type = builtin_type (arch)->builtin_char16;
17475 else if (bits == 32)
17476 type = builtin_type (arch)->builtin_char32;
17477 else
17478 {
17479 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17480 bits);
17481 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17482 }
17483 return set_die_type (die, type, cu);
17484 }
17485 break;
17486
17487 default:
17488 complaint (_("unsupported DW_AT_encoding: '%s'"),
17489 dwarf_type_encoding_name (encoding));
17490 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17491 break;
17492 }
17493
17494 if (name && strcmp (name, "char") == 0)
17495 TYPE_NOSIGN (type) = 1;
17496
17497 maybe_set_alignment (cu, die, type);
17498
17499 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17500
17501 return set_die_type (die, type, cu);
17502}
17503
17504/* Parse dwarf attribute if it's a block, reference or constant and put the
17505 resulting value of the attribute into struct bound_prop.
17506 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17507
17508static int
17509attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17510 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17511 struct type *default_type)
17512{
17513 struct dwarf2_property_baton *baton;
17514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17515 struct objfile *objfile = per_objfile->objfile;
17516 struct obstack *obstack = &objfile->objfile_obstack;
17517
17518 gdb_assert (default_type != NULL);
17519
17520 if (attr == NULL || prop == NULL)
17521 return 0;
17522
17523 if (attr->form_is_block ())
17524 {
17525 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17526 baton->property_type = default_type;
17527 baton->locexpr.per_cu = cu->per_cu;
17528 baton->locexpr.per_objfile = per_objfile;
17529 baton->locexpr.size = DW_BLOCK (attr)->size;
17530 baton->locexpr.data = DW_BLOCK (attr)->data;
17531 switch (attr->name)
17532 {
17533 case DW_AT_string_length:
17534 baton->locexpr.is_reference = true;
17535 break;
17536 default:
17537 baton->locexpr.is_reference = false;
17538 break;
17539 }
17540 prop->data.baton = baton;
17541 prop->kind = PROP_LOCEXPR;
17542 gdb_assert (prop->data.baton != NULL);
17543 }
17544 else if (attr->form_is_ref ())
17545 {
17546 struct dwarf2_cu *target_cu = cu;
17547 struct die_info *target_die;
17548 struct attribute *target_attr;
17549
17550 target_die = follow_die_ref (die, attr, &target_cu);
17551 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17552 if (target_attr == NULL)
17553 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17554 target_cu);
17555 if (target_attr == NULL)
17556 return 0;
17557
17558 switch (target_attr->name)
17559 {
17560 case DW_AT_location:
17561 if (target_attr->form_is_section_offset ())
17562 {
17563 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17564 baton->property_type = die_type (target_die, target_cu);
17565 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17566 prop->data.baton = baton;
17567 prop->kind = PROP_LOCLIST;
17568 gdb_assert (prop->data.baton != NULL);
17569 }
17570 else if (target_attr->form_is_block ())
17571 {
17572 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17573 baton->property_type = die_type (target_die, target_cu);
17574 baton->locexpr.per_cu = cu->per_cu;
17575 baton->locexpr.per_objfile = per_objfile;
17576 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17577 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17578 baton->locexpr.is_reference = true;
17579 prop->data.baton = baton;
17580 prop->kind = PROP_LOCEXPR;
17581 gdb_assert (prop->data.baton != NULL);
17582 }
17583 else
17584 {
17585 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17586 "dynamic property");
17587 return 0;
17588 }
17589 break;
17590 case DW_AT_data_member_location:
17591 {
17592 LONGEST offset;
17593
17594 if (!handle_data_member_location (target_die, target_cu,
17595 &offset))
17596 return 0;
17597
17598 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17599 baton->property_type = read_type_die (target_die->parent,
17600 target_cu);
17601 baton->offset_info.offset = offset;
17602 baton->offset_info.type = die_type (target_die, target_cu);
17603 prop->data.baton = baton;
17604 prop->kind = PROP_ADDR_OFFSET;
17605 break;
17606 }
17607 }
17608 }
17609 else if (attr->form_is_constant ())
17610 {
17611 prop->data.const_val = attr->constant_value (0);
17612 prop->kind = PROP_CONST;
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17617 dwarf2_name (die, cu));
17618 return 0;
17619 }
17620
17621 return 1;
17622}
17623
17624/* See read.h. */
17625
17626struct type *
17627dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17628{
17629 struct objfile *objfile = dwarf2_per_objfile->objfile;
17630 struct type *int_type;
17631
17632 /* Helper macro to examine the various builtin types. */
17633#define TRY_TYPE(F) \
17634 int_type = (unsigned_p \
17635 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17636 : objfile_type (objfile)->builtin_ ## F); \
17637 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17638 return int_type
17639
17640 TRY_TYPE (char);
17641 TRY_TYPE (short);
17642 TRY_TYPE (int);
17643 TRY_TYPE (long);
17644 TRY_TYPE (long_long);
17645
17646#undef TRY_TYPE
17647
17648 gdb_assert_not_reached ("unable to find suitable integer type");
17649}
17650
17651/* See read.h. */
17652
17653struct type *
17654dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17655{
17656 int addr_size = this->addr_size ();
17657 return int_type (addr_size, unsigned_p);
17658}
17659
17660/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17661 present (which is valid) then compute the default type based on the
17662 compilation units address size. */
17663
17664static struct type *
17665read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17666{
17667 struct type *index_type = die_type (die, cu);
17668
17669 /* Dwarf-2 specifications explicitly allows to create subrange types
17670 without specifying a base type.
17671 In that case, the base type must be set to the type of
17672 the lower bound, upper bound or count, in that order, if any of these
17673 three attributes references an object that has a type.
17674 If no base type is found, the Dwarf-2 specifications say that
17675 a signed integer type of size equal to the size of an address should
17676 be used.
17677 For the following C code: `extern char gdb_int [];'
17678 GCC produces an empty range DIE.
17679 FIXME: muller/2010-05-28: Possible references to object for low bound,
17680 high bound or count are not yet handled by this code. */
17681 if (index_type->code () == TYPE_CODE_VOID)
17682 index_type = cu->per_cu->addr_sized_int_type (false);
17683
17684 return index_type;
17685}
17686
17687/* Read the given DW_AT_subrange DIE. */
17688
17689static struct type *
17690read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17691{
17692 struct type *base_type, *orig_base_type;
17693 struct type *range_type;
17694 struct attribute *attr;
17695 struct dynamic_prop low, high;
17696 int low_default_is_valid;
17697 int high_bound_is_count = 0;
17698 const char *name;
17699 ULONGEST negative_mask;
17700
17701 orig_base_type = read_subrange_index_type (die, cu);
17702
17703 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17704 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17705 creating the range type, but we use the result of check_typedef
17706 when examining properties of the type. */
17707 base_type = check_typedef (orig_base_type);
17708
17709 /* The die_type call above may have already set the type for this DIE. */
17710 range_type = get_die_type (die, cu);
17711 if (range_type)
17712 return range_type;
17713
17714 low.kind = PROP_CONST;
17715 high.kind = PROP_CONST;
17716 high.data.const_val = 0;
17717
17718 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17719 omitting DW_AT_lower_bound. */
17720 switch (cu->language)
17721 {
17722 case language_c:
17723 case language_cplus:
17724 low.data.const_val = 0;
17725 low_default_is_valid = 1;
17726 break;
17727 case language_fortran:
17728 low.data.const_val = 1;
17729 low_default_is_valid = 1;
17730 break;
17731 case language_d:
17732 case language_objc:
17733 case language_rust:
17734 low.data.const_val = 0;
17735 low_default_is_valid = (cu->header.version >= 4);
17736 break;
17737 case language_ada:
17738 case language_m2:
17739 case language_pascal:
17740 low.data.const_val = 1;
17741 low_default_is_valid = (cu->header.version >= 4);
17742 break;
17743 default:
17744 low.data.const_val = 0;
17745 low_default_is_valid = 0;
17746 break;
17747 }
17748
17749 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17750 if (attr != nullptr)
17751 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17752 else if (!low_default_is_valid)
17753 complaint (_("Missing DW_AT_lower_bound "
17754 "- DIE at %s [in module %s]"),
17755 sect_offset_str (die->sect_off),
17756 objfile_name (cu->per_objfile->objfile));
17757
17758 struct attribute *attr_ub, *attr_count;
17759 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17760 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17761 {
17762 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17763 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17764 {
17765 /* If bounds are constant do the final calculation here. */
17766 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17767 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17768 else
17769 high_bound_is_count = 1;
17770 }
17771 else
17772 {
17773 if (attr_ub != NULL)
17774 complaint (_("Unresolved DW_AT_upper_bound "
17775 "- DIE at %s [in module %s]"),
17776 sect_offset_str (die->sect_off),
17777 objfile_name (cu->per_objfile->objfile));
17778 if (attr_count != NULL)
17779 complaint (_("Unresolved DW_AT_count "
17780 "- DIE at %s [in module %s]"),
17781 sect_offset_str (die->sect_off),
17782 objfile_name (cu->per_objfile->objfile));
17783 }
17784 }
17785
17786 LONGEST bias = 0;
17787 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17788 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17789 bias = bias_attr->constant_value (0);
17790
17791 /* Normally, the DWARF producers are expected to use a signed
17792 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17793 But this is unfortunately not always the case, as witnessed
17794 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17795 is used instead. To work around that ambiguity, we treat
17796 the bounds as signed, and thus sign-extend their values, when
17797 the base type is signed. */
17798 negative_mask =
17799 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17800 if (low.kind == PROP_CONST
17801 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17802 low.data.const_val |= negative_mask;
17803 if (high.kind == PROP_CONST
17804 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17805 high.data.const_val |= negative_mask;
17806
17807 /* Check for bit and byte strides. */
17808 struct dynamic_prop byte_stride_prop;
17809 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17810 if (attr_byte_stride != nullptr)
17811 {
17812 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17813 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17814 prop_type);
17815 }
17816
17817 struct dynamic_prop bit_stride_prop;
17818 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17819 if (attr_bit_stride != nullptr)
17820 {
17821 /* It only makes sense to have either a bit or byte stride. */
17822 if (attr_byte_stride != nullptr)
17823 {
17824 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17825 "- DIE at %s [in module %s]"),
17826 sect_offset_str (die->sect_off),
17827 objfile_name (cu->per_objfile->objfile));
17828 attr_bit_stride = nullptr;
17829 }
17830 else
17831 {
17832 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17833 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17834 prop_type);
17835 }
17836 }
17837
17838 if (attr_byte_stride != nullptr
17839 || attr_bit_stride != nullptr)
17840 {
17841 bool byte_stride_p = (attr_byte_stride != nullptr);
17842 struct dynamic_prop *stride
17843 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17844
17845 range_type
17846 = create_range_type_with_stride (NULL, orig_base_type, &low,
17847 &high, bias, stride, byte_stride_p);
17848 }
17849 else
17850 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17851
17852 if (high_bound_is_count)
17853 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17854
17855 /* Ada expects an empty array on no boundary attributes. */
17856 if (attr == NULL && cu->language != language_ada)
17857 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17858
17859 name = dwarf2_name (die, cu);
17860 if (name)
17861 range_type->set_name (name);
17862
17863 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17864 if (attr != nullptr)
17865 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17866
17867 maybe_set_alignment (cu, die, range_type);
17868
17869 set_die_type (die, range_type, cu);
17870
17871 /* set_die_type should be already done. */
17872 set_descriptive_type (range_type, die, cu);
17873
17874 return range_type;
17875}
17876
17877static struct type *
17878read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17879{
17880 struct type *type;
17881
17882 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17883 type->set_name (dwarf2_name (die, cu));
17884
17885 /* In Ada, an unspecified type is typically used when the description
17886 of the type is deferred to a different unit. When encountering
17887 such a type, we treat it as a stub, and try to resolve it later on,
17888 when needed. */
17889 if (cu->language == language_ada)
17890 TYPE_STUB (type) = 1;
17891
17892 return set_die_type (die, type, cu);
17893}
17894
17895/* Read a single die and all its descendents. Set the die's sibling
17896 field to NULL; set other fields in the die correctly, and set all
17897 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17898 location of the info_ptr after reading all of those dies. PARENT
17899 is the parent of the die in question. */
17900
17901static struct die_info *
17902read_die_and_children (const struct die_reader_specs *reader,
17903 const gdb_byte *info_ptr,
17904 const gdb_byte **new_info_ptr,
17905 struct die_info *parent)
17906{
17907 struct die_info *die;
17908 const gdb_byte *cur_ptr;
17909
17910 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17911 if (die == NULL)
17912 {
17913 *new_info_ptr = cur_ptr;
17914 return NULL;
17915 }
17916 store_in_ref_table (die, reader->cu);
17917
17918 if (die->has_children)
17919 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17920 else
17921 {
17922 die->child = NULL;
17923 *new_info_ptr = cur_ptr;
17924 }
17925
17926 die->sibling = NULL;
17927 die->parent = parent;
17928 return die;
17929}
17930
17931/* Read a die, all of its descendents, and all of its siblings; set
17932 all of the fields of all of the dies correctly. Arguments are as
17933 in read_die_and_children. */
17934
17935static struct die_info *
17936read_die_and_siblings_1 (const struct die_reader_specs *reader,
17937 const gdb_byte *info_ptr,
17938 const gdb_byte **new_info_ptr,
17939 struct die_info *parent)
17940{
17941 struct die_info *first_die, *last_sibling;
17942 const gdb_byte *cur_ptr;
17943
17944 cur_ptr = info_ptr;
17945 first_die = last_sibling = NULL;
17946
17947 while (1)
17948 {
17949 struct die_info *die
17950 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17951
17952 if (die == NULL)
17953 {
17954 *new_info_ptr = cur_ptr;
17955 return first_die;
17956 }
17957
17958 if (!first_die)
17959 first_die = die;
17960 else
17961 last_sibling->sibling = die;
17962
17963 last_sibling = die;
17964 }
17965}
17966
17967/* Read a die, all of its descendents, and all of its siblings; set
17968 all of the fields of all of the dies correctly. Arguments are as
17969 in read_die_and_children.
17970 This the main entry point for reading a DIE and all its children. */
17971
17972static struct die_info *
17973read_die_and_siblings (const struct die_reader_specs *reader,
17974 const gdb_byte *info_ptr,
17975 const gdb_byte **new_info_ptr,
17976 struct die_info *parent)
17977{
17978 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17979 new_info_ptr, parent);
17980
17981 if (dwarf_die_debug)
17982 {
17983 fprintf_unfiltered (gdb_stdlog,
17984 "Read die from %s@0x%x of %s:\n",
17985 reader->die_section->get_name (),
17986 (unsigned) (info_ptr - reader->die_section->buffer),
17987 bfd_get_filename (reader->abfd));
17988 dump_die (die, dwarf_die_debug);
17989 }
17990
17991 return die;
17992}
17993
17994/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17995 attributes.
17996 The caller is responsible for filling in the extra attributes
17997 and updating (*DIEP)->num_attrs.
17998 Set DIEP to point to a newly allocated die with its information,
17999 except for its child, sibling, and parent fields. */
18000
18001static const gdb_byte *
18002read_full_die_1 (const struct die_reader_specs *reader,
18003 struct die_info **diep, const gdb_byte *info_ptr,
18004 int num_extra_attrs)
18005{
18006 unsigned int abbrev_number, bytes_read, i;
18007 struct abbrev_info *abbrev;
18008 struct die_info *die;
18009 struct dwarf2_cu *cu = reader->cu;
18010 bfd *abfd = reader->abfd;
18011
18012 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18013 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18014 info_ptr += bytes_read;
18015 if (!abbrev_number)
18016 {
18017 *diep = NULL;
18018 return info_ptr;
18019 }
18020
18021 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18022 if (!abbrev)
18023 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18024 abbrev_number,
18025 bfd_get_filename (abfd));
18026
18027 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18028 die->sect_off = sect_off;
18029 die->tag = abbrev->tag;
18030 die->abbrev = abbrev_number;
18031 die->has_children = abbrev->has_children;
18032
18033 /* Make the result usable.
18034 The caller needs to update num_attrs after adding the extra
18035 attributes. */
18036 die->num_attrs = abbrev->num_attrs;
18037
18038 std::vector<int> indexes_that_need_reprocess;
18039 for (i = 0; i < abbrev->num_attrs; ++i)
18040 {
18041 bool need_reprocess;
18042 info_ptr =
18043 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18044 info_ptr, &need_reprocess);
18045 if (need_reprocess)
18046 indexes_that_need_reprocess.push_back (i);
18047 }
18048
18049 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18050 if (attr != nullptr)
18051 cu->str_offsets_base = DW_UNSND (attr);
18052
18053 attr = die->attr (DW_AT_loclists_base);
18054 if (attr != nullptr)
18055 cu->loclist_base = DW_UNSND (attr);
18056
18057 auto maybe_addr_base = die->addr_base ();
18058 if (maybe_addr_base.has_value ())
18059 cu->addr_base = *maybe_addr_base;
18060 for (int index : indexes_that_need_reprocess)
18061 read_attribute_reprocess (reader, &die->attrs[index]);
18062 *diep = die;
18063 return info_ptr;
18064}
18065
18066/* Read a die and all its attributes.
18067 Set DIEP to point to a newly allocated die with its information,
18068 except for its child, sibling, and parent fields. */
18069
18070static const gdb_byte *
18071read_full_die (const struct die_reader_specs *reader,
18072 struct die_info **diep, const gdb_byte *info_ptr)
18073{
18074 const gdb_byte *result;
18075
18076 result = read_full_die_1 (reader, diep, info_ptr, 0);
18077
18078 if (dwarf_die_debug)
18079 {
18080 fprintf_unfiltered (gdb_stdlog,
18081 "Read die from %s@0x%x of %s:\n",
18082 reader->die_section->get_name (),
18083 (unsigned) (info_ptr - reader->die_section->buffer),
18084 bfd_get_filename (reader->abfd));
18085 dump_die (*diep, dwarf_die_debug);
18086 }
18087
18088 return result;
18089}
18090\f
18091
18092/* Returns nonzero if TAG represents a type that we might generate a partial
18093 symbol for. */
18094
18095static int
18096is_type_tag_for_partial (int tag)
18097{
18098 switch (tag)
18099 {
18100#if 0
18101 /* Some types that would be reasonable to generate partial symbols for,
18102 that we don't at present. */
18103 case DW_TAG_array_type:
18104 case DW_TAG_file_type:
18105 case DW_TAG_ptr_to_member_type:
18106 case DW_TAG_set_type:
18107 case DW_TAG_string_type:
18108 case DW_TAG_subroutine_type:
18109#endif
18110 case DW_TAG_base_type:
18111 case DW_TAG_class_type:
18112 case DW_TAG_interface_type:
18113 case DW_TAG_enumeration_type:
18114 case DW_TAG_structure_type:
18115 case DW_TAG_subrange_type:
18116 case DW_TAG_typedef:
18117 case DW_TAG_union_type:
18118 return 1;
18119 default:
18120 return 0;
18121 }
18122}
18123
18124/* Load all DIEs that are interesting for partial symbols into memory. */
18125
18126static struct partial_die_info *
18127load_partial_dies (const struct die_reader_specs *reader,
18128 const gdb_byte *info_ptr, int building_psymtab)
18129{
18130 struct dwarf2_cu *cu = reader->cu;
18131 struct objfile *objfile = cu->per_objfile->objfile;
18132 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18133 unsigned int bytes_read;
18134 unsigned int load_all = 0;
18135 int nesting_level = 1;
18136
18137 parent_die = NULL;
18138 last_die = NULL;
18139
18140 gdb_assert (cu->per_cu != NULL);
18141 if (cu->per_cu->load_all_dies)
18142 load_all = 1;
18143
18144 cu->partial_dies
18145 = htab_create_alloc_ex (cu->header.length / 12,
18146 partial_die_hash,
18147 partial_die_eq,
18148 NULL,
18149 &cu->comp_unit_obstack,
18150 hashtab_obstack_allocate,
18151 dummy_obstack_deallocate);
18152
18153 while (1)
18154 {
18155 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18156
18157 /* A NULL abbrev means the end of a series of children. */
18158 if (abbrev == NULL)
18159 {
18160 if (--nesting_level == 0)
18161 return first_die;
18162
18163 info_ptr += bytes_read;
18164 last_die = parent_die;
18165 parent_die = parent_die->die_parent;
18166 continue;
18167 }
18168
18169 /* Check for template arguments. We never save these; if
18170 they're seen, we just mark the parent, and go on our way. */
18171 if (parent_die != NULL
18172 && cu->language == language_cplus
18173 && (abbrev->tag == DW_TAG_template_type_param
18174 || abbrev->tag == DW_TAG_template_value_param))
18175 {
18176 parent_die->has_template_arguments = 1;
18177
18178 if (!load_all)
18179 {
18180 /* We don't need a partial DIE for the template argument. */
18181 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18182 continue;
18183 }
18184 }
18185
18186 /* We only recurse into c++ subprograms looking for template arguments.
18187 Skip their other children. */
18188 if (!load_all
18189 && cu->language == language_cplus
18190 && parent_die != NULL
18191 && parent_die->tag == DW_TAG_subprogram)
18192 {
18193 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18194 continue;
18195 }
18196
18197 /* Check whether this DIE is interesting enough to save. Normally
18198 we would not be interested in members here, but there may be
18199 later variables referencing them via DW_AT_specification (for
18200 static members). */
18201 if (!load_all
18202 && !is_type_tag_for_partial (abbrev->tag)
18203 && abbrev->tag != DW_TAG_constant
18204 && abbrev->tag != DW_TAG_enumerator
18205 && abbrev->tag != DW_TAG_subprogram
18206 && abbrev->tag != DW_TAG_inlined_subroutine
18207 && abbrev->tag != DW_TAG_lexical_block
18208 && abbrev->tag != DW_TAG_variable
18209 && abbrev->tag != DW_TAG_namespace
18210 && abbrev->tag != DW_TAG_module
18211 && abbrev->tag != DW_TAG_member
18212 && abbrev->tag != DW_TAG_imported_unit
18213 && abbrev->tag != DW_TAG_imported_declaration)
18214 {
18215 /* Otherwise we skip to the next sibling, if any. */
18216 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18217 continue;
18218 }
18219
18220 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18221 abbrev);
18222
18223 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18224
18225 /* This two-pass algorithm for processing partial symbols has a
18226 high cost in cache pressure. Thus, handle some simple cases
18227 here which cover the majority of C partial symbols. DIEs
18228 which neither have specification tags in them, nor could have
18229 specification tags elsewhere pointing at them, can simply be
18230 processed and discarded.
18231
18232 This segment is also optional; scan_partial_symbols and
18233 add_partial_symbol will handle these DIEs if we chain
18234 them in normally. When compilers which do not emit large
18235 quantities of duplicate debug information are more common,
18236 this code can probably be removed. */
18237
18238 /* Any complete simple types at the top level (pretty much all
18239 of them, for a language without namespaces), can be processed
18240 directly. */
18241 if (parent_die == NULL
18242 && pdi.has_specification == 0
18243 && pdi.is_declaration == 0
18244 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18245 || pdi.tag == DW_TAG_base_type
18246 || pdi.tag == DW_TAG_subrange_type))
18247 {
18248 if (building_psymtab && pdi.name != NULL)
18249 add_psymbol_to_list (pdi.name, false,
18250 VAR_DOMAIN, LOC_TYPEDEF, -1,
18251 psymbol_placement::STATIC,
18252 0, cu->language, objfile);
18253 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18254 continue;
18255 }
18256
18257 /* The exception for DW_TAG_typedef with has_children above is
18258 a workaround of GCC PR debug/47510. In the case of this complaint
18259 type_name_or_error will error on such types later.
18260
18261 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18262 it could not find the child DIEs referenced later, this is checked
18263 above. In correct DWARF DW_TAG_typedef should have no children. */
18264
18265 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18266 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18267 "- DIE at %s [in module %s]"),
18268 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18269
18270 /* If we're at the second level, and we're an enumerator, and
18271 our parent has no specification (meaning possibly lives in a
18272 namespace elsewhere), then we can add the partial symbol now
18273 instead of queueing it. */
18274 if (pdi.tag == DW_TAG_enumerator
18275 && parent_die != NULL
18276 && parent_die->die_parent == NULL
18277 && parent_die->tag == DW_TAG_enumeration_type
18278 && parent_die->has_specification == 0)
18279 {
18280 if (pdi.name == NULL)
18281 complaint (_("malformed enumerator DIE ignored"));
18282 else if (building_psymtab)
18283 add_psymbol_to_list (pdi.name, false,
18284 VAR_DOMAIN, LOC_CONST, -1,
18285 cu->language == language_cplus
18286 ? psymbol_placement::GLOBAL
18287 : psymbol_placement::STATIC,
18288 0, cu->language, objfile);
18289
18290 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18291 continue;
18292 }
18293
18294 struct partial_die_info *part_die
18295 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18296
18297 /* We'll save this DIE so link it in. */
18298 part_die->die_parent = parent_die;
18299 part_die->die_sibling = NULL;
18300 part_die->die_child = NULL;
18301
18302 if (last_die && last_die == parent_die)
18303 last_die->die_child = part_die;
18304 else if (last_die)
18305 last_die->die_sibling = part_die;
18306
18307 last_die = part_die;
18308
18309 if (first_die == NULL)
18310 first_die = part_die;
18311
18312 /* Maybe add the DIE to the hash table. Not all DIEs that we
18313 find interesting need to be in the hash table, because we
18314 also have the parent/sibling/child chains; only those that we
18315 might refer to by offset later during partial symbol reading.
18316
18317 For now this means things that might have be the target of a
18318 DW_AT_specification, DW_AT_abstract_origin, or
18319 DW_AT_extension. DW_AT_extension will refer only to
18320 namespaces; DW_AT_abstract_origin refers to functions (and
18321 many things under the function DIE, but we do not recurse
18322 into function DIEs during partial symbol reading) and
18323 possibly variables as well; DW_AT_specification refers to
18324 declarations. Declarations ought to have the DW_AT_declaration
18325 flag. It happens that GCC forgets to put it in sometimes, but
18326 only for functions, not for types.
18327
18328 Adding more things than necessary to the hash table is harmless
18329 except for the performance cost. Adding too few will result in
18330 wasted time in find_partial_die, when we reread the compilation
18331 unit with load_all_dies set. */
18332
18333 if (load_all
18334 || abbrev->tag == DW_TAG_constant
18335 || abbrev->tag == DW_TAG_subprogram
18336 || abbrev->tag == DW_TAG_variable
18337 || abbrev->tag == DW_TAG_namespace
18338 || part_die->is_declaration)
18339 {
18340 void **slot;
18341
18342 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18343 to_underlying (part_die->sect_off),
18344 INSERT);
18345 *slot = part_die;
18346 }
18347
18348 /* For some DIEs we want to follow their children (if any). For C
18349 we have no reason to follow the children of structures; for other
18350 languages we have to, so that we can get at method physnames
18351 to infer fully qualified class names, for DW_AT_specification,
18352 and for C++ template arguments. For C++, we also look one level
18353 inside functions to find template arguments (if the name of the
18354 function does not already contain the template arguments).
18355
18356 For Ada and Fortran, we need to scan the children of subprograms
18357 and lexical blocks as well because these languages allow the
18358 definition of nested entities that could be interesting for the
18359 debugger, such as nested subprograms for instance. */
18360 if (last_die->has_children
18361 && (load_all
18362 || last_die->tag == DW_TAG_namespace
18363 || last_die->tag == DW_TAG_module
18364 || last_die->tag == DW_TAG_enumeration_type
18365 || (cu->language == language_cplus
18366 && last_die->tag == DW_TAG_subprogram
18367 && (last_die->name == NULL
18368 || strchr (last_die->name, '<') == NULL))
18369 || (cu->language != language_c
18370 && (last_die->tag == DW_TAG_class_type
18371 || last_die->tag == DW_TAG_interface_type
18372 || last_die->tag == DW_TAG_structure_type
18373 || last_die->tag == DW_TAG_union_type))
18374 || ((cu->language == language_ada
18375 || cu->language == language_fortran)
18376 && (last_die->tag == DW_TAG_subprogram
18377 || last_die->tag == DW_TAG_lexical_block))))
18378 {
18379 nesting_level++;
18380 parent_die = last_die;
18381 continue;
18382 }
18383
18384 /* Otherwise we skip to the next sibling, if any. */
18385 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18386
18387 /* Back to the top, do it again. */
18388 }
18389}
18390
18391partial_die_info::partial_die_info (sect_offset sect_off_,
18392 struct abbrev_info *abbrev)
18393 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18394{
18395}
18396
18397/* Read a minimal amount of information into the minimal die structure.
18398 INFO_PTR should point just after the initial uleb128 of a DIE. */
18399
18400const gdb_byte *
18401partial_die_info::read (const struct die_reader_specs *reader,
18402 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18403{
18404 struct dwarf2_cu *cu = reader->cu;
18405 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18406 unsigned int i;
18407 int has_low_pc_attr = 0;
18408 int has_high_pc_attr = 0;
18409 int high_pc_relative = 0;
18410
18411 for (i = 0; i < abbrev.num_attrs; ++i)
18412 {
18413 attribute attr;
18414 bool need_reprocess;
18415 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18416 info_ptr, &need_reprocess);
18417 /* String and address offsets that need to do the reprocessing have
18418 already been read at this point, so there is no need to wait until
18419 the loop terminates to do the reprocessing. */
18420 if (need_reprocess)
18421 read_attribute_reprocess (reader, &attr);
18422 /* Store the data if it is of an attribute we want to keep in a
18423 partial symbol table. */
18424 switch (attr.name)
18425 {
18426 case DW_AT_name:
18427 switch (tag)
18428 {
18429 case DW_TAG_compile_unit:
18430 case DW_TAG_partial_unit:
18431 case DW_TAG_type_unit:
18432 /* Compilation units have a DW_AT_name that is a filename, not
18433 a source language identifier. */
18434 case DW_TAG_enumeration_type:
18435 case DW_TAG_enumerator:
18436 /* These tags always have simple identifiers already; no need
18437 to canonicalize them. */
18438 name = DW_STRING (&attr);
18439 break;
18440 default:
18441 {
18442 struct objfile *objfile = dwarf2_per_objfile->objfile;
18443
18444 name
18445 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18446 }
18447 break;
18448 }
18449 break;
18450 case DW_AT_linkage_name:
18451 case DW_AT_MIPS_linkage_name:
18452 /* Note that both forms of linkage name might appear. We
18453 assume they will be the same, and we only store the last
18454 one we see. */
18455 linkage_name = attr.value_as_string ();
18456 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18457 See https://github.com/rust-lang/rust/issues/32925. */
18458 if (cu->language == language_rust && linkage_name != NULL
18459 && strchr (linkage_name, '{') != NULL)
18460 linkage_name = NULL;
18461 break;
18462 case DW_AT_low_pc:
18463 has_low_pc_attr = 1;
18464 lowpc = attr.value_as_address ();
18465 break;
18466 case DW_AT_high_pc:
18467 has_high_pc_attr = 1;
18468 highpc = attr.value_as_address ();
18469 if (cu->header.version >= 4 && attr.form_is_constant ())
18470 high_pc_relative = 1;
18471 break;
18472 case DW_AT_location:
18473 /* Support the .debug_loc offsets. */
18474 if (attr.form_is_block ())
18475 {
18476 d.locdesc = DW_BLOCK (&attr);
18477 }
18478 else if (attr.form_is_section_offset ())
18479 {
18480 dwarf2_complex_location_expr_complaint ();
18481 }
18482 else
18483 {
18484 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18485 "partial symbol information");
18486 }
18487 break;
18488 case DW_AT_external:
18489 is_external = DW_UNSND (&attr);
18490 break;
18491 case DW_AT_declaration:
18492 is_declaration = DW_UNSND (&attr);
18493 break;
18494 case DW_AT_type:
18495 has_type = 1;
18496 break;
18497 case DW_AT_abstract_origin:
18498 case DW_AT_specification:
18499 case DW_AT_extension:
18500 has_specification = 1;
18501 spec_offset = attr.get_ref_die_offset ();
18502 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18503 || cu->per_cu->is_dwz);
18504 break;
18505 case DW_AT_sibling:
18506 /* Ignore absolute siblings, they might point outside of
18507 the current compile unit. */
18508 if (attr.form == DW_FORM_ref_addr)
18509 complaint (_("ignoring absolute DW_AT_sibling"));
18510 else
18511 {
18512 const gdb_byte *buffer = reader->buffer;
18513 sect_offset off = attr.get_ref_die_offset ();
18514 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18515
18516 if (sibling_ptr < info_ptr)
18517 complaint (_("DW_AT_sibling points backwards"));
18518 else if (sibling_ptr > reader->buffer_end)
18519 reader->die_section->overflow_complaint ();
18520 else
18521 sibling = sibling_ptr;
18522 }
18523 break;
18524 case DW_AT_byte_size:
18525 has_byte_size = 1;
18526 break;
18527 case DW_AT_const_value:
18528 has_const_value = 1;
18529 break;
18530 case DW_AT_calling_convention:
18531 /* DWARF doesn't provide a way to identify a program's source-level
18532 entry point. DW_AT_calling_convention attributes are only meant
18533 to describe functions' calling conventions.
18534
18535 However, because it's a necessary piece of information in
18536 Fortran, and before DWARF 4 DW_CC_program was the only
18537 piece of debugging information whose definition refers to
18538 a 'main program' at all, several compilers marked Fortran
18539 main programs with DW_CC_program --- even when those
18540 functions use the standard calling conventions.
18541
18542 Although DWARF now specifies a way to provide this
18543 information, we support this practice for backward
18544 compatibility. */
18545 if (DW_UNSND (&attr) == DW_CC_program
18546 && cu->language == language_fortran)
18547 main_subprogram = 1;
18548 break;
18549 case DW_AT_inline:
18550 if (DW_UNSND (&attr) == DW_INL_inlined
18551 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18552 may_be_inlined = 1;
18553 break;
18554
18555 case DW_AT_import:
18556 if (tag == DW_TAG_imported_unit)
18557 {
18558 d.sect_off = attr.get_ref_die_offset ();
18559 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18560 || cu->per_cu->is_dwz);
18561 }
18562 break;
18563
18564 case DW_AT_main_subprogram:
18565 main_subprogram = DW_UNSND (&attr);
18566 break;
18567
18568 case DW_AT_ranges:
18569 {
18570 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18571 but that requires a full DIE, so instead we just
18572 reimplement it. */
18573 int need_ranges_base = tag != DW_TAG_compile_unit;
18574 unsigned int ranges_offset = (DW_UNSND (&attr)
18575 + (need_ranges_base
18576 ? cu->ranges_base
18577 : 0));
18578
18579 /* Value of the DW_AT_ranges attribute is the offset in the
18580 .debug_ranges section. */
18581 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18582 nullptr))
18583 has_pc_info = 1;
18584 }
18585 break;
18586
18587 default:
18588 break;
18589 }
18590 }
18591
18592 /* For Ada, if both the name and the linkage name appear, we prefer
18593 the latter. This lets "catch exception" work better, regardless
18594 of the order in which the name and linkage name were emitted.
18595 Really, though, this is just a workaround for the fact that gdb
18596 doesn't store both the name and the linkage name. */
18597 if (cu->language == language_ada && linkage_name != nullptr)
18598 name = linkage_name;
18599
18600 if (high_pc_relative)
18601 highpc += lowpc;
18602
18603 if (has_low_pc_attr && has_high_pc_attr)
18604 {
18605 /* When using the GNU linker, .gnu.linkonce. sections are used to
18606 eliminate duplicate copies of functions and vtables and such.
18607 The linker will arbitrarily choose one and discard the others.
18608 The AT_*_pc values for such functions refer to local labels in
18609 these sections. If the section from that file was discarded, the
18610 labels are not in the output, so the relocs get a value of 0.
18611 If this is a discarded function, mark the pc bounds as invalid,
18612 so that GDB will ignore it. */
18613 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18614 {
18615 struct objfile *objfile = dwarf2_per_objfile->objfile;
18616 struct gdbarch *gdbarch = objfile->arch ();
18617
18618 complaint (_("DW_AT_low_pc %s is zero "
18619 "for DIE at %s [in module %s]"),
18620 paddress (gdbarch, lowpc),
18621 sect_offset_str (sect_off),
18622 objfile_name (objfile));
18623 }
18624 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18625 else if (lowpc >= highpc)
18626 {
18627 struct objfile *objfile = dwarf2_per_objfile->objfile;
18628 struct gdbarch *gdbarch = objfile->arch ();
18629
18630 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18631 "for DIE at %s [in module %s]"),
18632 paddress (gdbarch, lowpc),
18633 paddress (gdbarch, highpc),
18634 sect_offset_str (sect_off),
18635 objfile_name (objfile));
18636 }
18637 else
18638 has_pc_info = 1;
18639 }
18640
18641 return info_ptr;
18642}
18643
18644/* Find a cached partial DIE at OFFSET in CU. */
18645
18646struct partial_die_info *
18647dwarf2_cu::find_partial_die (sect_offset sect_off)
18648{
18649 struct partial_die_info *lookup_die = NULL;
18650 struct partial_die_info part_die (sect_off);
18651
18652 lookup_die = ((struct partial_die_info *)
18653 htab_find_with_hash (partial_dies, &part_die,
18654 to_underlying (sect_off)));
18655
18656 return lookup_die;
18657}
18658
18659/* Find a partial DIE at OFFSET, which may or may not be in CU,
18660 except in the case of .debug_types DIEs which do not reference
18661 outside their CU (they do however referencing other types via
18662 DW_FORM_ref_sig8). */
18663
18664static const struct cu_partial_die_info
18665find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18666{
18667 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18668 struct objfile *objfile = dwarf2_per_objfile->objfile;
18669 struct dwarf2_per_cu_data *per_cu = NULL;
18670 struct partial_die_info *pd = NULL;
18671
18672 if (offset_in_dwz == cu->per_cu->is_dwz
18673 && cu->header.offset_in_cu_p (sect_off))
18674 {
18675 pd = cu->find_partial_die (sect_off);
18676 if (pd != NULL)
18677 return { cu, pd };
18678 /* We missed recording what we needed.
18679 Load all dies and try again. */
18680 per_cu = cu->per_cu;
18681 }
18682 else
18683 {
18684 /* TUs don't reference other CUs/TUs (except via type signatures). */
18685 if (cu->per_cu->is_debug_types)
18686 {
18687 error (_("Dwarf Error: Type Unit at offset %s contains"
18688 " external reference to offset %s [in module %s].\n"),
18689 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18690 bfd_get_filename (objfile->obfd));
18691 }
18692 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18693 dwarf2_per_objfile);
18694
18695 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18696 load_partial_comp_unit (per_cu, cu->per_objfile);
18697
18698 per_cu->cu->last_used = 0;
18699 pd = per_cu->cu->find_partial_die (sect_off);
18700 }
18701
18702 /* If we didn't find it, and not all dies have been loaded,
18703 load them all and try again. */
18704
18705 if (pd == NULL && per_cu->load_all_dies == 0)
18706 {
18707 per_cu->load_all_dies = 1;
18708
18709 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18710 THIS_CU->cu may already be in use. So we can't just free it and
18711 replace its DIEs with the ones we read in. Instead, we leave those
18712 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18713 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18714 set. */
18715 load_partial_comp_unit (per_cu, cu->per_objfile);
18716
18717 pd = per_cu->cu->find_partial_die (sect_off);
18718 }
18719
18720 if (pd == NULL)
18721 internal_error (__FILE__, __LINE__,
18722 _("could not find partial DIE %s "
18723 "in cache [from module %s]\n"),
18724 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18725 return { per_cu->cu, pd };
18726}
18727
18728/* See if we can figure out if the class lives in a namespace. We do
18729 this by looking for a member function; its demangled name will
18730 contain namespace info, if there is any. */
18731
18732static void
18733guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18734 struct dwarf2_cu *cu)
18735{
18736 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18737 what template types look like, because the demangler
18738 frequently doesn't give the same name as the debug info. We
18739 could fix this by only using the demangled name to get the
18740 prefix (but see comment in read_structure_type). */
18741
18742 struct partial_die_info *real_pdi;
18743 struct partial_die_info *child_pdi;
18744
18745 /* If this DIE (this DIE's specification, if any) has a parent, then
18746 we should not do this. We'll prepend the parent's fully qualified
18747 name when we create the partial symbol. */
18748
18749 real_pdi = struct_pdi;
18750 while (real_pdi->has_specification)
18751 {
18752 auto res = find_partial_die (real_pdi->spec_offset,
18753 real_pdi->spec_is_dwz, cu);
18754 real_pdi = res.pdi;
18755 cu = res.cu;
18756 }
18757
18758 if (real_pdi->die_parent != NULL)
18759 return;
18760
18761 for (child_pdi = struct_pdi->die_child;
18762 child_pdi != NULL;
18763 child_pdi = child_pdi->die_sibling)
18764 {
18765 if (child_pdi->tag == DW_TAG_subprogram
18766 && child_pdi->linkage_name != NULL)
18767 {
18768 gdb::unique_xmalloc_ptr<char> actual_class_name
18769 (language_class_name_from_physname (cu->language_defn,
18770 child_pdi->linkage_name));
18771 if (actual_class_name != NULL)
18772 {
18773 struct objfile *objfile = cu->per_objfile->objfile;
18774 struct_pdi->name = objfile->intern (actual_class_name.get ());
18775 }
18776 break;
18777 }
18778 }
18779}
18780
18781/* Return true if a DIE with TAG may have the DW_AT_const_value
18782 attribute. */
18783
18784static bool
18785can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18786{
18787 switch (tag)
18788 {
18789 case DW_TAG_constant:
18790 case DW_TAG_enumerator:
18791 case DW_TAG_formal_parameter:
18792 case DW_TAG_template_value_param:
18793 case DW_TAG_variable:
18794 return true;
18795 }
18796
18797 return false;
18798}
18799
18800void
18801partial_die_info::fixup (struct dwarf2_cu *cu)
18802{
18803 /* Once we've fixed up a die, there's no point in doing so again.
18804 This also avoids a memory leak if we were to call
18805 guess_partial_die_structure_name multiple times. */
18806 if (fixup_called)
18807 return;
18808
18809 /* If we found a reference attribute and the DIE has no name, try
18810 to find a name in the referred to DIE. */
18811
18812 if (name == NULL && has_specification)
18813 {
18814 struct partial_die_info *spec_die;
18815
18816 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18817 spec_die = res.pdi;
18818 cu = res.cu;
18819
18820 spec_die->fixup (cu);
18821
18822 if (spec_die->name)
18823 {
18824 name = spec_die->name;
18825
18826 /* Copy DW_AT_external attribute if it is set. */
18827 if (spec_die->is_external)
18828 is_external = spec_die->is_external;
18829 }
18830 }
18831
18832 if (!has_const_value && has_specification
18833 && can_have_DW_AT_const_value_p (tag))
18834 {
18835 struct partial_die_info *spec_die;
18836
18837 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18838 spec_die = res.pdi;
18839 cu = res.cu;
18840
18841 spec_die->fixup (cu);
18842
18843 if (spec_die->has_const_value)
18844 {
18845 /* Copy DW_AT_const_value attribute if it is set. */
18846 has_const_value = spec_die->has_const_value;
18847 }
18848 }
18849
18850 /* Set default names for some unnamed DIEs. */
18851
18852 if (name == NULL && tag == DW_TAG_namespace)
18853 name = CP_ANONYMOUS_NAMESPACE_STR;
18854
18855 /* If there is no parent die to provide a namespace, and there are
18856 children, see if we can determine the namespace from their linkage
18857 name. */
18858 if (cu->language == language_cplus
18859 && !cu->per_objfile->per_bfd->types.empty ()
18860 && die_parent == NULL
18861 && has_children
18862 && (tag == DW_TAG_class_type
18863 || tag == DW_TAG_structure_type
18864 || tag == DW_TAG_union_type))
18865 guess_partial_die_structure_name (this, cu);
18866
18867 /* GCC might emit a nameless struct or union that has a linkage
18868 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18869 if (name == NULL
18870 && (tag == DW_TAG_class_type
18871 || tag == DW_TAG_interface_type
18872 || tag == DW_TAG_structure_type
18873 || tag == DW_TAG_union_type)
18874 && linkage_name != NULL)
18875 {
18876 gdb::unique_xmalloc_ptr<char> demangled
18877 (gdb_demangle (linkage_name, DMGL_TYPES));
18878 if (demangled != nullptr)
18879 {
18880 const char *base;
18881
18882 /* Strip any leading namespaces/classes, keep only the base name.
18883 DW_AT_name for named DIEs does not contain the prefixes. */
18884 base = strrchr (demangled.get (), ':');
18885 if (base && base > demangled.get () && base[-1] == ':')
18886 base++;
18887 else
18888 base = demangled.get ();
18889
18890 struct objfile *objfile = cu->per_objfile->objfile;
18891 name = objfile->intern (base);
18892 }
18893 }
18894
18895 fixup_called = 1;
18896}
18897
18898/* Read the .debug_loclists header contents from the given SECTION in the
18899 HEADER. */
18900static void
18901read_loclist_header (struct loclist_header *header,
18902 struct dwarf2_section_info *section)
18903{
18904 unsigned int bytes_read;
18905 bfd *abfd = section->get_bfd_owner ();
18906 const gdb_byte *info_ptr = section->buffer;
18907 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18908 info_ptr += bytes_read;
18909 header->version = read_2_bytes (abfd, info_ptr);
18910 info_ptr += 2;
18911 header->addr_size = read_1_byte (abfd, info_ptr);
18912 info_ptr += 1;
18913 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18914 info_ptr += 1;
18915 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18916}
18917
18918/* Return the DW_AT_loclists_base value for the CU. */
18919static ULONGEST
18920lookup_loclist_base (struct dwarf2_cu *cu)
18921{
18922 /* For the .dwo unit, the loclist_base points to the first offset following
18923 the header. The header consists of the following entities-
18924 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18925 bit format)
18926 2. version (2 bytes)
18927 3. address size (1 byte)
18928 4. segment selector size (1 byte)
18929 5. offset entry count (4 bytes)
18930 These sizes are derived as per the DWARFv5 standard. */
18931 if (cu->dwo_unit != nullptr)
18932 {
18933 if (cu->header.initial_length_size == 4)
18934 return LOCLIST_HEADER_SIZE32;
18935 return LOCLIST_HEADER_SIZE64;
18936 }
18937 return cu->loclist_base;
18938}
18939
18940/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18941 array of offsets in the .debug_loclists section. */
18942static CORE_ADDR
18943read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18944{
18945 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18946 struct objfile *objfile = dwarf2_per_objfile->objfile;
18947 bfd *abfd = objfile->obfd;
18948 ULONGEST loclist_base = lookup_loclist_base (cu);
18949 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18950
18951 section->read (objfile);
18952 if (section->buffer == NULL)
18953 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18954 "section [in module %s]"), objfile_name (objfile));
18955 struct loclist_header header;
18956 read_loclist_header (&header, section);
18957 if (loclist_index >= header.offset_entry_count)
18958 complaint (_("DW_FORM_loclistx pointing outside of "
18959 ".debug_loclists offset array [in module %s]"),
18960 objfile_name (objfile));
18961 if (loclist_base + loclist_index * cu->header.offset_size
18962 >= section->size)
18963 complaint (_("DW_FORM_loclistx pointing outside of "
18964 ".debug_loclists section [in module %s]"),
18965 objfile_name (objfile));
18966 const gdb_byte *info_ptr
18967 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18968
18969 if (cu->header.offset_size == 4)
18970 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18971 else
18972 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18973}
18974
18975/* Process the attributes that had to be skipped in the first round. These
18976 attributes are the ones that need str_offsets_base or addr_base attributes.
18977 They could not have been processed in the first round, because at the time
18978 the values of str_offsets_base or addr_base may not have been known. */
18979static void
18980read_attribute_reprocess (const struct die_reader_specs *reader,
18981 struct attribute *attr)
18982{
18983 struct dwarf2_cu *cu = reader->cu;
18984 switch (attr->form)
18985 {
18986 case DW_FORM_addrx:
18987 case DW_FORM_GNU_addr_index:
18988 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18989 break;
18990 case DW_FORM_loclistx:
18991 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18992 break;
18993 case DW_FORM_strx:
18994 case DW_FORM_strx1:
18995 case DW_FORM_strx2:
18996 case DW_FORM_strx3:
18997 case DW_FORM_strx4:
18998 case DW_FORM_GNU_str_index:
18999 {
19000 unsigned int str_index = DW_UNSND (attr);
19001 if (reader->dwo_file != NULL)
19002 {
19003 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19004 DW_STRING_IS_CANONICAL (attr) = 0;
19005 }
19006 else
19007 {
19008 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19009 DW_STRING_IS_CANONICAL (attr) = 0;
19010 }
19011 break;
19012 }
19013 default:
19014 gdb_assert_not_reached (_("Unexpected DWARF form."));
19015 }
19016}
19017
19018/* Read an attribute value described by an attribute form. */
19019
19020static const gdb_byte *
19021read_attribute_value (const struct die_reader_specs *reader,
19022 struct attribute *attr, unsigned form,
19023 LONGEST implicit_const, const gdb_byte *info_ptr,
19024 bool *need_reprocess)
19025{
19026 struct dwarf2_cu *cu = reader->cu;
19027 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19028 struct objfile *objfile = dwarf2_per_objfile->objfile;
19029 bfd *abfd = reader->abfd;
19030 struct comp_unit_head *cu_header = &cu->header;
19031 unsigned int bytes_read;
19032 struct dwarf_block *blk;
19033 *need_reprocess = false;
19034
19035 attr->form = (enum dwarf_form) form;
19036 switch (form)
19037 {
19038 case DW_FORM_ref_addr:
19039 if (cu->header.version == 2)
19040 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19041 &bytes_read);
19042 else
19043 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19044 &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_GNU_ref_alt:
19048 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19049 info_ptr += bytes_read;
19050 break;
19051 case DW_FORM_addr:
19052 {
19053 struct gdbarch *gdbarch = objfile->arch ();
19054 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19055 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19056 info_ptr += bytes_read;
19057 }
19058 break;
19059 case DW_FORM_block2:
19060 blk = dwarf_alloc_block (cu);
19061 blk->size = read_2_bytes (abfd, info_ptr);
19062 info_ptr += 2;
19063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19064 info_ptr += blk->size;
19065 DW_BLOCK (attr) = blk;
19066 break;
19067 case DW_FORM_block4:
19068 blk = dwarf_alloc_block (cu);
19069 blk->size = read_4_bytes (abfd, info_ptr);
19070 info_ptr += 4;
19071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19072 info_ptr += blk->size;
19073 DW_BLOCK (attr) = blk;
19074 break;
19075 case DW_FORM_data2:
19076 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19077 info_ptr += 2;
19078 break;
19079 case DW_FORM_data4:
19080 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19081 info_ptr += 4;
19082 break;
19083 case DW_FORM_data8:
19084 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19085 info_ptr += 8;
19086 break;
19087 case DW_FORM_data16:
19088 blk = dwarf_alloc_block (cu);
19089 blk->size = 16;
19090 blk->data = read_n_bytes (abfd, info_ptr, 16);
19091 info_ptr += 16;
19092 DW_BLOCK (attr) = blk;
19093 break;
19094 case DW_FORM_sec_offset:
19095 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19096 info_ptr += bytes_read;
19097 break;
19098 case DW_FORM_loclistx:
19099 {
19100 *need_reprocess = true;
19101 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19102 info_ptr += bytes_read;
19103 }
19104 break;
19105 case DW_FORM_string:
19106 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19107 DW_STRING_IS_CANONICAL (attr) = 0;
19108 info_ptr += bytes_read;
19109 break;
19110 case DW_FORM_strp:
19111 if (!cu->per_cu->is_dwz)
19112 {
19113 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19114 abfd, info_ptr, cu_header,
19115 &bytes_read);
19116 DW_STRING_IS_CANONICAL (attr) = 0;
19117 info_ptr += bytes_read;
19118 break;
19119 }
19120 /* FALLTHROUGH */
19121 case DW_FORM_line_strp:
19122 if (!cu->per_cu->is_dwz)
19123 {
19124 DW_STRING (attr)
19125 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19126 &bytes_read);
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19129 break;
19130 }
19131 /* FALLTHROUGH */
19132 case DW_FORM_GNU_strp_alt:
19133 {
19134 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19135 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19136 &bytes_read);
19137
19138 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19139 DW_STRING_IS_CANONICAL (attr) = 0;
19140 info_ptr += bytes_read;
19141 }
19142 break;
19143 case DW_FORM_exprloc:
19144 case DW_FORM_block:
19145 blk = dwarf_alloc_block (cu);
19146 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19147 info_ptr += bytes_read;
19148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19149 info_ptr += blk->size;
19150 DW_BLOCK (attr) = blk;
19151 break;
19152 case DW_FORM_block1:
19153 blk = dwarf_alloc_block (cu);
19154 blk->size = read_1_byte (abfd, info_ptr);
19155 info_ptr += 1;
19156 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19157 info_ptr += blk->size;
19158 DW_BLOCK (attr) = blk;
19159 break;
19160 case DW_FORM_data1:
19161 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19162 info_ptr += 1;
19163 break;
19164 case DW_FORM_flag:
19165 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19166 info_ptr += 1;
19167 break;
19168 case DW_FORM_flag_present:
19169 DW_UNSND (attr) = 1;
19170 break;
19171 case DW_FORM_sdata:
19172 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 break;
19175 case DW_FORM_udata:
19176 case DW_FORM_rnglistx:
19177 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19178 info_ptr += bytes_read;
19179 break;
19180 case DW_FORM_ref1:
19181 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19182 + read_1_byte (abfd, info_ptr));
19183 info_ptr += 1;
19184 break;
19185 case DW_FORM_ref2:
19186 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19187 + read_2_bytes (abfd, info_ptr));
19188 info_ptr += 2;
19189 break;
19190 case DW_FORM_ref4:
19191 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19192 + read_4_bytes (abfd, info_ptr));
19193 info_ptr += 4;
19194 break;
19195 case DW_FORM_ref8:
19196 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19197 + read_8_bytes (abfd, info_ptr));
19198 info_ptr += 8;
19199 break;
19200 case DW_FORM_ref_sig8:
19201 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19202 info_ptr += 8;
19203 break;
19204 case DW_FORM_ref_udata:
19205 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19206 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19207 info_ptr += bytes_read;
19208 break;
19209 case DW_FORM_indirect:
19210 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19211 info_ptr += bytes_read;
19212 if (form == DW_FORM_implicit_const)
19213 {
19214 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19215 info_ptr += bytes_read;
19216 }
19217 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19218 info_ptr, need_reprocess);
19219 break;
19220 case DW_FORM_implicit_const:
19221 DW_SND (attr) = implicit_const;
19222 break;
19223 case DW_FORM_addrx:
19224 case DW_FORM_GNU_addr_index:
19225 *need_reprocess = true;
19226 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19227 info_ptr += bytes_read;
19228 break;
19229 case DW_FORM_strx:
19230 case DW_FORM_strx1:
19231 case DW_FORM_strx2:
19232 case DW_FORM_strx3:
19233 case DW_FORM_strx4:
19234 case DW_FORM_GNU_str_index:
19235 {
19236 ULONGEST str_index;
19237 if (form == DW_FORM_strx1)
19238 {
19239 str_index = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 }
19242 else if (form == DW_FORM_strx2)
19243 {
19244 str_index = read_2_bytes (abfd, info_ptr);
19245 info_ptr += 2;
19246 }
19247 else if (form == DW_FORM_strx3)
19248 {
19249 str_index = read_3_bytes (abfd, info_ptr);
19250 info_ptr += 3;
19251 }
19252 else if (form == DW_FORM_strx4)
19253 {
19254 str_index = read_4_bytes (abfd, info_ptr);
19255 info_ptr += 4;
19256 }
19257 else
19258 {
19259 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19260 info_ptr += bytes_read;
19261 }
19262 *need_reprocess = true;
19263 DW_UNSND (attr) = str_index;
19264 }
19265 break;
19266 default:
19267 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19268 dwarf_form_name (form),
19269 bfd_get_filename (abfd));
19270 }
19271
19272 /* Super hack. */
19273 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19274 attr->form = DW_FORM_GNU_ref_alt;
19275
19276 /* We have seen instances where the compiler tried to emit a byte
19277 size attribute of -1 which ended up being encoded as an unsigned
19278 0xffffffff. Although 0xffffffff is technically a valid size value,
19279 an object of this size seems pretty unlikely so we can relatively
19280 safely treat these cases as if the size attribute was invalid and
19281 treat them as zero by default. */
19282 if (attr->name == DW_AT_byte_size
19283 && form == DW_FORM_data4
19284 && DW_UNSND (attr) >= 0xffffffff)
19285 {
19286 complaint
19287 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19288 hex_string (DW_UNSND (attr)));
19289 DW_UNSND (attr) = 0;
19290 }
19291
19292 return info_ptr;
19293}
19294
19295/* Read an attribute described by an abbreviated attribute. */
19296
19297static const gdb_byte *
19298read_attribute (const struct die_reader_specs *reader,
19299 struct attribute *attr, struct attr_abbrev *abbrev,
19300 const gdb_byte *info_ptr, bool *need_reprocess)
19301{
19302 attr->name = abbrev->name;
19303 return read_attribute_value (reader, attr, abbrev->form,
19304 abbrev->implicit_const, info_ptr,
19305 need_reprocess);
19306}
19307
19308/* Return pointer to string at .debug_str offset STR_OFFSET. */
19309
19310static const char *
19311read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19312 LONGEST str_offset)
19313{
19314 return dwarf2_per_objfile->per_bfd->str.read_string
19315 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19316}
19317
19318/* Return pointer to string at .debug_str offset as read from BUF.
19319 BUF is assumed to be in a compilation unit described by CU_HEADER.
19320 Return *BYTES_READ_PTR count of bytes read from BUF. */
19321
19322static const char *
19323read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19324 const gdb_byte *buf,
19325 const struct comp_unit_head *cu_header,
19326 unsigned int *bytes_read_ptr)
19327{
19328 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19329
19330 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19331}
19332
19333/* See read.h. */
19334
19335const char *
19336dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19337 const struct comp_unit_head *cu_header,
19338 unsigned int *bytes_read_ptr)
19339{
19340 bfd *abfd = objfile->obfd;
19341 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19342
19343 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19344}
19345
19346/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19347 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19348 ADDR_SIZE is the size of addresses from the CU header. */
19349
19350static CORE_ADDR
19351read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19352 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19353 int addr_size)
19354{
19355 struct objfile *objfile = dwarf2_per_objfile->objfile;
19356 bfd *abfd = objfile->obfd;
19357 const gdb_byte *info_ptr;
19358 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19359
19360 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19361 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19362 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19363 objfile_name (objfile));
19364 if (addr_base_or_zero + addr_index * addr_size
19365 >= dwarf2_per_objfile->per_bfd->addr.size)
19366 error (_("DW_FORM_addr_index pointing outside of "
19367 ".debug_addr section [in module %s]"),
19368 objfile_name (objfile));
19369 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19370 + addr_base_or_zero + addr_index * addr_size);
19371 if (addr_size == 4)
19372 return bfd_get_32 (abfd, info_ptr);
19373 else
19374 return bfd_get_64 (abfd, info_ptr);
19375}
19376
19377/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19378
19379static CORE_ADDR
19380read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19381{
19382 return read_addr_index_1 (cu->per_objfile, addr_index,
19383 cu->addr_base, cu->header.addr_size);
19384}
19385
19386/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19387
19388static CORE_ADDR
19389read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19390 unsigned int *bytes_read)
19391{
19392 bfd *abfd = cu->per_objfile->objfile->obfd;
19393 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19394
19395 return read_addr_index (cu, addr_index);
19396}
19397
19398/* See read.h. */
19399
19400CORE_ADDR
19401dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19402{
19403 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19404 struct dwarf2_cu *cu = per_cu->cu;
19405 gdb::optional<ULONGEST> addr_base;
19406 int addr_size;
19407
19408 /* We need addr_base and addr_size.
19409 If we don't have PER_CU->cu, we have to get it.
19410 Nasty, but the alternative is storing the needed info in PER_CU,
19411 which at this point doesn't seem justified: it's not clear how frequently
19412 it would get used and it would increase the size of every PER_CU.
19413 Entry points like dwarf2_per_cu_addr_size do a similar thing
19414 so we're not in uncharted territory here.
19415 Alas we need to be a bit more complicated as addr_base is contained
19416 in the DIE.
19417
19418 We don't need to read the entire CU(/TU).
19419 We just need the header and top level die.
19420
19421 IWBN to use the aging mechanism to let us lazily later discard the CU.
19422 For now we skip this optimization. */
19423
19424 if (cu != NULL)
19425 {
19426 addr_base = cu->addr_base;
19427 addr_size = cu->header.addr_size;
19428 }
19429 else
19430 {
19431 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19432 addr_base = reader.cu->addr_base;
19433 addr_size = reader.cu->header.addr_size;
19434 }
19435
19436 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19437 addr_size);
19438}
19439
19440/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19441 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19442 DWO file. */
19443
19444static const char *
19445read_str_index (struct dwarf2_cu *cu,
19446 struct dwarf2_section_info *str_section,
19447 struct dwarf2_section_info *str_offsets_section,
19448 ULONGEST str_offsets_base, ULONGEST str_index)
19449{
19450 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19451 struct objfile *objfile = dwarf2_per_objfile->objfile;
19452 const char *objf_name = objfile_name (objfile);
19453 bfd *abfd = objfile->obfd;
19454 const gdb_byte *info_ptr;
19455 ULONGEST str_offset;
19456 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19457
19458 str_section->read (objfile);
19459 str_offsets_section->read (objfile);
19460 if (str_section->buffer == NULL)
19461 error (_("%s used without %s section"
19462 " in CU at offset %s [in module %s]"),
19463 form_name, str_section->get_name (),
19464 sect_offset_str (cu->header.sect_off), objf_name);
19465 if (str_offsets_section->buffer == NULL)
19466 error (_("%s used without %s section"
19467 " in CU at offset %s [in module %s]"),
19468 form_name, str_section->get_name (),
19469 sect_offset_str (cu->header.sect_off), objf_name);
19470 info_ptr = (str_offsets_section->buffer
19471 + str_offsets_base
19472 + str_index * cu->header.offset_size);
19473 if (cu->header.offset_size == 4)
19474 str_offset = bfd_get_32 (abfd, info_ptr);
19475 else
19476 str_offset = bfd_get_64 (abfd, info_ptr);
19477 if (str_offset >= str_section->size)
19478 error (_("Offset from %s pointing outside of"
19479 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19480 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19481 return (const char *) (str_section->buffer + str_offset);
19482}
19483
19484/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19485
19486static const char *
19487read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19488{
19489 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19490 ? reader->cu->header.addr_size : 0;
19491 return read_str_index (reader->cu,
19492 &reader->dwo_file->sections.str,
19493 &reader->dwo_file->sections.str_offsets,
19494 str_offsets_base, str_index);
19495}
19496
19497/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19498
19499static const char *
19500read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19501{
19502 struct objfile *objfile = cu->per_objfile->objfile;
19503 const char *objf_name = objfile_name (objfile);
19504 static const char form_name[] = "DW_FORM_GNU_str_index";
19505 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19506
19507 if (!cu->str_offsets_base.has_value ())
19508 error (_("%s used in Fission stub without %s"
19509 " in CU at offset 0x%lx [in module %s]"),
19510 form_name, str_offsets_attr_name,
19511 (long) cu->header.offset_size, objf_name);
19512
19513 return read_str_index (cu,
19514 &cu->per_objfile->per_bfd->str,
19515 &cu->per_objfile->per_bfd->str_offsets,
19516 *cu->str_offsets_base, str_index);
19517}
19518
19519/* Return the length of an LEB128 number in BUF. */
19520
19521static int
19522leb128_size (const gdb_byte *buf)
19523{
19524 const gdb_byte *begin = buf;
19525 gdb_byte byte;
19526
19527 while (1)
19528 {
19529 byte = *buf++;
19530 if ((byte & 128) == 0)
19531 return buf - begin;
19532 }
19533}
19534
19535static void
19536set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19537{
19538 switch (lang)
19539 {
19540 case DW_LANG_C89:
19541 case DW_LANG_C99:
19542 case DW_LANG_C11:
19543 case DW_LANG_C:
19544 case DW_LANG_UPC:
19545 cu->language = language_c;
19546 break;
19547 case DW_LANG_Java:
19548 case DW_LANG_C_plus_plus:
19549 case DW_LANG_C_plus_plus_11:
19550 case DW_LANG_C_plus_plus_14:
19551 cu->language = language_cplus;
19552 break;
19553 case DW_LANG_D:
19554 cu->language = language_d;
19555 break;
19556 case DW_LANG_Fortran77:
19557 case DW_LANG_Fortran90:
19558 case DW_LANG_Fortran95:
19559 case DW_LANG_Fortran03:
19560 case DW_LANG_Fortran08:
19561 cu->language = language_fortran;
19562 break;
19563 case DW_LANG_Go:
19564 cu->language = language_go;
19565 break;
19566 case DW_LANG_Mips_Assembler:
19567 cu->language = language_asm;
19568 break;
19569 case DW_LANG_Ada83:
19570 case DW_LANG_Ada95:
19571 cu->language = language_ada;
19572 break;
19573 case DW_LANG_Modula2:
19574 cu->language = language_m2;
19575 break;
19576 case DW_LANG_Pascal83:
19577 cu->language = language_pascal;
19578 break;
19579 case DW_LANG_ObjC:
19580 cu->language = language_objc;
19581 break;
19582 case DW_LANG_Rust:
19583 case DW_LANG_Rust_old:
19584 cu->language = language_rust;
19585 break;
19586 case DW_LANG_Cobol74:
19587 case DW_LANG_Cobol85:
19588 default:
19589 cu->language = language_minimal;
19590 break;
19591 }
19592 cu->language_defn = language_def (cu->language);
19593}
19594
19595/* Return the named attribute or NULL if not there. */
19596
19597static struct attribute *
19598dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19599{
19600 for (;;)
19601 {
19602 unsigned int i;
19603 struct attribute *spec = NULL;
19604
19605 for (i = 0; i < die->num_attrs; ++i)
19606 {
19607 if (die->attrs[i].name == name)
19608 return &die->attrs[i];
19609 if (die->attrs[i].name == DW_AT_specification
19610 || die->attrs[i].name == DW_AT_abstract_origin)
19611 spec = &die->attrs[i];
19612 }
19613
19614 if (!spec)
19615 break;
19616
19617 die = follow_die_ref (die, spec, &cu);
19618 }
19619
19620 return NULL;
19621}
19622
19623/* Return the string associated with a string-typed attribute, or NULL if it
19624 is either not found or is of an incorrect type. */
19625
19626static const char *
19627dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19628{
19629 struct attribute *attr;
19630 const char *str = NULL;
19631
19632 attr = dwarf2_attr (die, name, cu);
19633
19634 if (attr != NULL)
19635 {
19636 str = attr->value_as_string ();
19637 if (str == nullptr)
19638 complaint (_("string type expected for attribute %s for "
19639 "DIE at %s in module %s"),
19640 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19641 objfile_name (cu->per_objfile->objfile));
19642 }
19643
19644 return str;
19645}
19646
19647/* Return the dwo name or NULL if not present. If present, it is in either
19648 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19649static const char *
19650dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19651{
19652 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19653 if (dwo_name == nullptr)
19654 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19655 return dwo_name;
19656}
19657
19658/* Return non-zero iff the attribute NAME is defined for the given DIE,
19659 and holds a non-zero value. This function should only be used for
19660 DW_FORM_flag or DW_FORM_flag_present attributes. */
19661
19662static int
19663dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19664{
19665 struct attribute *attr = dwarf2_attr (die, name, cu);
19666
19667 return (attr && DW_UNSND (attr));
19668}
19669
19670static int
19671die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19672{
19673 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19674 which value is non-zero. However, we have to be careful with
19675 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19676 (via dwarf2_flag_true_p) follows this attribute. So we may
19677 end up accidently finding a declaration attribute that belongs
19678 to a different DIE referenced by the specification attribute,
19679 even though the given DIE does not have a declaration attribute. */
19680 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19681 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19682}
19683
19684/* Return the die giving the specification for DIE, if there is
19685 one. *SPEC_CU is the CU containing DIE on input, and the CU
19686 containing the return value on output. If there is no
19687 specification, but there is an abstract origin, that is
19688 returned. */
19689
19690static struct die_info *
19691die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19692{
19693 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19694 *spec_cu);
19695
19696 if (spec_attr == NULL)
19697 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19698
19699 if (spec_attr == NULL)
19700 return NULL;
19701 else
19702 return follow_die_ref (die, spec_attr, spec_cu);
19703}
19704
19705/* Stub for free_line_header to match void * callback types. */
19706
19707static void
19708free_line_header_voidp (void *arg)
19709{
19710 struct line_header *lh = (struct line_header *) arg;
19711
19712 delete lh;
19713}
19714
19715/* A convenience function to find the proper .debug_line section for a CU. */
19716
19717static struct dwarf2_section_info *
19718get_debug_line_section (struct dwarf2_cu *cu)
19719{
19720 struct dwarf2_section_info *section;
19721 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19722
19723 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19724 DWO file. */
19725 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19726 section = &cu->dwo_unit->dwo_file->sections.line;
19727 else if (cu->per_cu->is_dwz)
19728 {
19729 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19730
19731 section = &dwz->line;
19732 }
19733 else
19734 section = &dwarf2_per_objfile->per_bfd->line;
19735
19736 return section;
19737}
19738
19739/* Read the statement program header starting at OFFSET in
19740 .debug_line, or .debug_line.dwo. Return a pointer
19741 to a struct line_header, allocated using xmalloc.
19742 Returns NULL if there is a problem reading the header, e.g., if it
19743 has a version we don't understand.
19744
19745 NOTE: the strings in the include directory and file name tables of
19746 the returned object point into the dwarf line section buffer,
19747 and must not be freed. */
19748
19749static line_header_up
19750dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19751{
19752 struct dwarf2_section_info *section;
19753 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19754
19755 section = get_debug_line_section (cu);
19756 section->read (dwarf2_per_objfile->objfile);
19757 if (section->buffer == NULL)
19758 {
19759 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19760 complaint (_("missing .debug_line.dwo section"));
19761 else
19762 complaint (_("missing .debug_line section"));
19763 return 0;
19764 }
19765
19766 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19767 dwarf2_per_objfile, section,
19768 &cu->header);
19769}
19770
19771/* Subroutine of dwarf_decode_lines to simplify it.
19772 Return the file name of the psymtab for the given file_entry.
19773 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19774 If space for the result is malloc'd, *NAME_HOLDER will be set.
19775 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19776
19777static const char *
19778psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19779 const dwarf2_psymtab *pst,
19780 const char *comp_dir,
19781 gdb::unique_xmalloc_ptr<char> *name_holder)
19782{
19783 const char *include_name = fe.name;
19784 const char *include_name_to_compare = include_name;
19785 const char *pst_filename;
19786 int file_is_pst;
19787
19788 const char *dir_name = fe.include_dir (lh);
19789
19790 gdb::unique_xmalloc_ptr<char> hold_compare;
19791 if (!IS_ABSOLUTE_PATH (include_name)
19792 && (dir_name != NULL || comp_dir != NULL))
19793 {
19794 /* Avoid creating a duplicate psymtab for PST.
19795 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19796 Before we do the comparison, however, we need to account
19797 for DIR_NAME and COMP_DIR.
19798 First prepend dir_name (if non-NULL). If we still don't
19799 have an absolute path prepend comp_dir (if non-NULL).
19800 However, the directory we record in the include-file's
19801 psymtab does not contain COMP_DIR (to match the
19802 corresponding symtab(s)).
19803
19804 Example:
19805
19806 bash$ cd /tmp
19807 bash$ gcc -g ./hello.c
19808 include_name = "hello.c"
19809 dir_name = "."
19810 DW_AT_comp_dir = comp_dir = "/tmp"
19811 DW_AT_name = "./hello.c"
19812
19813 */
19814
19815 if (dir_name != NULL)
19816 {
19817 name_holder->reset (concat (dir_name, SLASH_STRING,
19818 include_name, (char *) NULL));
19819 include_name = name_holder->get ();
19820 include_name_to_compare = include_name;
19821 }
19822 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19823 {
19824 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19825 include_name, (char *) NULL));
19826 include_name_to_compare = hold_compare.get ();
19827 }
19828 }
19829
19830 pst_filename = pst->filename;
19831 gdb::unique_xmalloc_ptr<char> copied_name;
19832 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19833 {
19834 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19835 pst_filename, (char *) NULL));
19836 pst_filename = copied_name.get ();
19837 }
19838
19839 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19840
19841 if (file_is_pst)
19842 return NULL;
19843 return include_name;
19844}
19845
19846/* State machine to track the state of the line number program. */
19847
19848class lnp_state_machine
19849{
19850public:
19851 /* Initialize a machine state for the start of a line number
19852 program. */
19853 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19854 bool record_lines_p);
19855
19856 file_entry *current_file ()
19857 {
19858 /* lh->file_names is 0-based, but the file name numbers in the
19859 statement program are 1-based. */
19860 return m_line_header->file_name_at (m_file);
19861 }
19862
19863 /* Record the line in the state machine. END_SEQUENCE is true if
19864 we're processing the end of a sequence. */
19865 void record_line (bool end_sequence);
19866
19867 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19868 nop-out rest of the lines in this sequence. */
19869 void check_line_address (struct dwarf2_cu *cu,
19870 const gdb_byte *line_ptr,
19871 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19872
19873 void handle_set_discriminator (unsigned int discriminator)
19874 {
19875 m_discriminator = discriminator;
19876 m_line_has_non_zero_discriminator |= discriminator != 0;
19877 }
19878
19879 /* Handle DW_LNE_set_address. */
19880 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19881 {
19882 m_op_index = 0;
19883 address += baseaddr;
19884 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19885 }
19886
19887 /* Handle DW_LNS_advance_pc. */
19888 void handle_advance_pc (CORE_ADDR adjust);
19889
19890 /* Handle a special opcode. */
19891 void handle_special_opcode (unsigned char op_code);
19892
19893 /* Handle DW_LNS_advance_line. */
19894 void handle_advance_line (int line_delta)
19895 {
19896 advance_line (line_delta);
19897 }
19898
19899 /* Handle DW_LNS_set_file. */
19900 void handle_set_file (file_name_index file);
19901
19902 /* Handle DW_LNS_negate_stmt. */
19903 void handle_negate_stmt ()
19904 {
19905 m_is_stmt = !m_is_stmt;
19906 }
19907
19908 /* Handle DW_LNS_const_add_pc. */
19909 void handle_const_add_pc ();
19910
19911 /* Handle DW_LNS_fixed_advance_pc. */
19912 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19913 {
19914 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19915 m_op_index = 0;
19916 }
19917
19918 /* Handle DW_LNS_copy. */
19919 void handle_copy ()
19920 {
19921 record_line (false);
19922 m_discriminator = 0;
19923 }
19924
19925 /* Handle DW_LNE_end_sequence. */
19926 void handle_end_sequence ()
19927 {
19928 m_currently_recording_lines = true;
19929 }
19930
19931private:
19932 /* Advance the line by LINE_DELTA. */
19933 void advance_line (int line_delta)
19934 {
19935 m_line += line_delta;
19936
19937 if (line_delta != 0)
19938 m_line_has_non_zero_discriminator = m_discriminator != 0;
19939 }
19940
19941 struct dwarf2_cu *m_cu;
19942
19943 gdbarch *m_gdbarch;
19944
19945 /* True if we're recording lines.
19946 Otherwise we're building partial symtabs and are just interested in
19947 finding include files mentioned by the line number program. */
19948 bool m_record_lines_p;
19949
19950 /* The line number header. */
19951 line_header *m_line_header;
19952
19953 /* These are part of the standard DWARF line number state machine,
19954 and initialized according to the DWARF spec. */
19955
19956 unsigned char m_op_index = 0;
19957 /* The line table index of the current file. */
19958 file_name_index m_file = 1;
19959 unsigned int m_line = 1;
19960
19961 /* These are initialized in the constructor. */
19962
19963 CORE_ADDR m_address;
19964 bool m_is_stmt;
19965 unsigned int m_discriminator;
19966
19967 /* Additional bits of state we need to track. */
19968
19969 /* The last file that we called dwarf2_start_subfile for.
19970 This is only used for TLLs. */
19971 unsigned int m_last_file = 0;
19972 /* The last file a line number was recorded for. */
19973 struct subfile *m_last_subfile = NULL;
19974
19975 /* When true, record the lines we decode. */
19976 bool m_currently_recording_lines = false;
19977
19978 /* The last line number that was recorded, used to coalesce
19979 consecutive entries for the same line. This can happen, for
19980 example, when discriminators are present. PR 17276. */
19981 unsigned int m_last_line = 0;
19982 bool m_line_has_non_zero_discriminator = false;
19983};
19984
19985void
19986lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19987{
19988 CORE_ADDR addr_adj = (((m_op_index + adjust)
19989 / m_line_header->maximum_ops_per_instruction)
19990 * m_line_header->minimum_instruction_length);
19991 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19992 m_op_index = ((m_op_index + adjust)
19993 % m_line_header->maximum_ops_per_instruction);
19994}
19995
19996void
19997lnp_state_machine::handle_special_opcode (unsigned char op_code)
19998{
19999 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20000 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20001 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20002 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20003 / m_line_header->maximum_ops_per_instruction)
20004 * m_line_header->minimum_instruction_length);
20005 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20006 m_op_index = ((m_op_index + adj_opcode_d)
20007 % m_line_header->maximum_ops_per_instruction);
20008
20009 int line_delta = m_line_header->line_base + adj_opcode_r;
20010 advance_line (line_delta);
20011 record_line (false);
20012 m_discriminator = 0;
20013}
20014
20015void
20016lnp_state_machine::handle_set_file (file_name_index file)
20017{
20018 m_file = file;
20019
20020 const file_entry *fe = current_file ();
20021 if (fe == NULL)
20022 dwarf2_debug_line_missing_file_complaint ();
20023 else if (m_record_lines_p)
20024 {
20025 const char *dir = fe->include_dir (m_line_header);
20026
20027 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20028 m_line_has_non_zero_discriminator = m_discriminator != 0;
20029 dwarf2_start_subfile (m_cu, fe->name, dir);
20030 }
20031}
20032
20033void
20034lnp_state_machine::handle_const_add_pc ()
20035{
20036 CORE_ADDR adjust
20037 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20038
20039 CORE_ADDR addr_adj
20040 = (((m_op_index + adjust)
20041 / m_line_header->maximum_ops_per_instruction)
20042 * m_line_header->minimum_instruction_length);
20043
20044 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20045 m_op_index = ((m_op_index + adjust)
20046 % m_line_header->maximum_ops_per_instruction);
20047}
20048
20049/* Return non-zero if we should add LINE to the line number table.
20050 LINE is the line to add, LAST_LINE is the last line that was added,
20051 LAST_SUBFILE is the subfile for LAST_LINE.
20052 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20053 had a non-zero discriminator.
20054
20055 We have to be careful in the presence of discriminators.
20056 E.g., for this line:
20057
20058 for (i = 0; i < 100000; i++);
20059
20060 clang can emit four line number entries for that one line,
20061 each with a different discriminator.
20062 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20063
20064 However, we want gdb to coalesce all four entries into one.
20065 Otherwise the user could stepi into the middle of the line and
20066 gdb would get confused about whether the pc really was in the
20067 middle of the line.
20068
20069 Things are further complicated by the fact that two consecutive
20070 line number entries for the same line is a heuristic used by gcc
20071 to denote the end of the prologue. So we can't just discard duplicate
20072 entries, we have to be selective about it. The heuristic we use is
20073 that we only collapse consecutive entries for the same line if at least
20074 one of those entries has a non-zero discriminator. PR 17276.
20075
20076 Note: Addresses in the line number state machine can never go backwards
20077 within one sequence, thus this coalescing is ok. */
20078
20079static int
20080dwarf_record_line_p (struct dwarf2_cu *cu,
20081 unsigned int line, unsigned int last_line,
20082 int line_has_non_zero_discriminator,
20083 struct subfile *last_subfile)
20084{
20085 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20086 return 1;
20087 if (line != last_line)
20088 return 1;
20089 /* Same line for the same file that we've seen already.
20090 As a last check, for pr 17276, only record the line if the line
20091 has never had a non-zero discriminator. */
20092 if (!line_has_non_zero_discriminator)
20093 return 1;
20094 return 0;
20095}
20096
20097/* Use the CU's builder to record line number LINE beginning at
20098 address ADDRESS in the line table of subfile SUBFILE. */
20099
20100static void
20101dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20102 unsigned int line, CORE_ADDR address, bool is_stmt,
20103 struct dwarf2_cu *cu)
20104{
20105 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20106
20107 if (dwarf_line_debug)
20108 {
20109 fprintf_unfiltered (gdb_stdlog,
20110 "Recording line %u, file %s, address %s\n",
20111 line, lbasename (subfile->name),
20112 paddress (gdbarch, address));
20113 }
20114
20115 if (cu != nullptr)
20116 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20117}
20118
20119/* Subroutine of dwarf_decode_lines_1 to simplify it.
20120 Mark the end of a set of line number records.
20121 The arguments are the same as for dwarf_record_line_1.
20122 If SUBFILE is NULL the request is ignored. */
20123
20124static void
20125dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20126 CORE_ADDR address, struct dwarf2_cu *cu)
20127{
20128 if (subfile == NULL)
20129 return;
20130
20131 if (dwarf_line_debug)
20132 {
20133 fprintf_unfiltered (gdb_stdlog,
20134 "Finishing current line, file %s, address %s\n",
20135 lbasename (subfile->name),
20136 paddress (gdbarch, address));
20137 }
20138
20139 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20140}
20141
20142void
20143lnp_state_machine::record_line (bool end_sequence)
20144{
20145 if (dwarf_line_debug)
20146 {
20147 fprintf_unfiltered (gdb_stdlog,
20148 "Processing actual line %u: file %u,"
20149 " address %s, is_stmt %u, discrim %u%s\n",
20150 m_line, m_file,
20151 paddress (m_gdbarch, m_address),
20152 m_is_stmt, m_discriminator,
20153 (end_sequence ? "\t(end sequence)" : ""));
20154 }
20155
20156 file_entry *fe = current_file ();
20157
20158 if (fe == NULL)
20159 dwarf2_debug_line_missing_file_complaint ();
20160 /* For now we ignore lines not starting on an instruction boundary.
20161 But not when processing end_sequence for compatibility with the
20162 previous version of the code. */
20163 else if (m_op_index == 0 || end_sequence)
20164 {
20165 fe->included_p = 1;
20166 if (m_record_lines_p)
20167 {
20168 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20169 || end_sequence)
20170 {
20171 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20172 m_currently_recording_lines ? m_cu : nullptr);
20173 }
20174
20175 if (!end_sequence)
20176 {
20177 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20178
20179 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20180 m_line_has_non_zero_discriminator,
20181 m_last_subfile))
20182 {
20183 buildsym_compunit *builder = m_cu->get_builder ();
20184 dwarf_record_line_1 (m_gdbarch,
20185 builder->get_current_subfile (),
20186 m_line, m_address, is_stmt,
20187 m_currently_recording_lines ? m_cu : nullptr);
20188 }
20189 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20190 m_last_line = m_line;
20191 }
20192 }
20193 }
20194}
20195
20196lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20197 line_header *lh, bool record_lines_p)
20198{
20199 m_cu = cu;
20200 m_gdbarch = arch;
20201 m_record_lines_p = record_lines_p;
20202 m_line_header = lh;
20203
20204 m_currently_recording_lines = true;
20205
20206 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20207 was a line entry for it so that the backend has a chance to adjust it
20208 and also record it in case it needs it. This is currently used by MIPS
20209 code, cf. `mips_adjust_dwarf2_line'. */
20210 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20211 m_is_stmt = lh->default_is_stmt;
20212 m_discriminator = 0;
20213}
20214
20215void
20216lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20217 const gdb_byte *line_ptr,
20218 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20219{
20220 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20221 the pc range of the CU. However, we restrict the test to only ADDRESS
20222 values of zero to preserve GDB's previous behaviour which is to handle
20223 the specific case of a function being GC'd by the linker. */
20224
20225 if (address == 0 && address < unrelocated_lowpc)
20226 {
20227 /* This line table is for a function which has been
20228 GCd by the linker. Ignore it. PR gdb/12528 */
20229
20230 struct objfile *objfile = cu->per_objfile->objfile;
20231 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20232
20233 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20234 line_offset, objfile_name (objfile));
20235 m_currently_recording_lines = false;
20236 /* Note: m_currently_recording_lines is left as false until we see
20237 DW_LNE_end_sequence. */
20238 }
20239}
20240
20241/* Subroutine of dwarf_decode_lines to simplify it.
20242 Process the line number information in LH.
20243 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20244 program in order to set included_p for every referenced header. */
20245
20246static void
20247dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20248 const int decode_for_pst_p, CORE_ADDR lowpc)
20249{
20250 const gdb_byte *line_ptr, *extended_end;
20251 const gdb_byte *line_end;
20252 unsigned int bytes_read, extended_len;
20253 unsigned char op_code, extended_op;
20254 CORE_ADDR baseaddr;
20255 struct objfile *objfile = cu->per_objfile->objfile;
20256 bfd *abfd = objfile->obfd;
20257 struct gdbarch *gdbarch = objfile->arch ();
20258 /* True if we're recording line info (as opposed to building partial
20259 symtabs and just interested in finding include files mentioned by
20260 the line number program). */
20261 bool record_lines_p = !decode_for_pst_p;
20262
20263 baseaddr = objfile->text_section_offset ();
20264
20265 line_ptr = lh->statement_program_start;
20266 line_end = lh->statement_program_end;
20267
20268 /* Read the statement sequences until there's nothing left. */
20269 while (line_ptr < line_end)
20270 {
20271 /* The DWARF line number program state machine. Reset the state
20272 machine at the start of each sequence. */
20273 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20274 bool end_sequence = false;
20275
20276 if (record_lines_p)
20277 {
20278 /* Start a subfile for the current file of the state
20279 machine. */
20280 const file_entry *fe = state_machine.current_file ();
20281
20282 if (fe != NULL)
20283 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20284 }
20285
20286 /* Decode the table. */
20287 while (line_ptr < line_end && !end_sequence)
20288 {
20289 op_code = read_1_byte (abfd, line_ptr);
20290 line_ptr += 1;
20291
20292 if (op_code >= lh->opcode_base)
20293 {
20294 /* Special opcode. */
20295 state_machine.handle_special_opcode (op_code);
20296 }
20297 else switch (op_code)
20298 {
20299 case DW_LNS_extended_op:
20300 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20301 &bytes_read);
20302 line_ptr += bytes_read;
20303 extended_end = line_ptr + extended_len;
20304 extended_op = read_1_byte (abfd, line_ptr);
20305 line_ptr += 1;
20306 switch (extended_op)
20307 {
20308 case DW_LNE_end_sequence:
20309 state_machine.handle_end_sequence ();
20310 end_sequence = true;
20311 break;
20312 case DW_LNE_set_address:
20313 {
20314 CORE_ADDR address
20315 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20316 line_ptr += bytes_read;
20317
20318 state_machine.check_line_address (cu, line_ptr,
20319 lowpc - baseaddr, address);
20320 state_machine.handle_set_address (baseaddr, address);
20321 }
20322 break;
20323 case DW_LNE_define_file:
20324 {
20325 const char *cur_file;
20326 unsigned int mod_time, length;
20327 dir_index dindex;
20328
20329 cur_file = read_direct_string (abfd, line_ptr,
20330 &bytes_read);
20331 line_ptr += bytes_read;
20332 dindex = (dir_index)
20333 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20334 line_ptr += bytes_read;
20335 mod_time =
20336 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20337 line_ptr += bytes_read;
20338 length =
20339 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20340 line_ptr += bytes_read;
20341 lh->add_file_name (cur_file, dindex, mod_time, length);
20342 }
20343 break;
20344 case DW_LNE_set_discriminator:
20345 {
20346 /* The discriminator is not interesting to the
20347 debugger; just ignore it. We still need to
20348 check its value though:
20349 if there are consecutive entries for the same
20350 (non-prologue) line we want to coalesce them.
20351 PR 17276. */
20352 unsigned int discr
20353 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20354 line_ptr += bytes_read;
20355
20356 state_machine.handle_set_discriminator (discr);
20357 }
20358 break;
20359 default:
20360 complaint (_("mangled .debug_line section"));
20361 return;
20362 }
20363 /* Make sure that we parsed the extended op correctly. If e.g.
20364 we expected a different address size than the producer used,
20365 we may have read the wrong number of bytes. */
20366 if (line_ptr != extended_end)
20367 {
20368 complaint (_("mangled .debug_line section"));
20369 return;
20370 }
20371 break;
20372 case DW_LNS_copy:
20373 state_machine.handle_copy ();
20374 break;
20375 case DW_LNS_advance_pc:
20376 {
20377 CORE_ADDR adjust
20378 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20379 line_ptr += bytes_read;
20380
20381 state_machine.handle_advance_pc (adjust);
20382 }
20383 break;
20384 case DW_LNS_advance_line:
20385 {
20386 int line_delta
20387 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20388 line_ptr += bytes_read;
20389
20390 state_machine.handle_advance_line (line_delta);
20391 }
20392 break;
20393 case DW_LNS_set_file:
20394 {
20395 file_name_index file
20396 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20397 &bytes_read);
20398 line_ptr += bytes_read;
20399
20400 state_machine.handle_set_file (file);
20401 }
20402 break;
20403 case DW_LNS_set_column:
20404 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20405 line_ptr += bytes_read;
20406 break;
20407 case DW_LNS_negate_stmt:
20408 state_machine.handle_negate_stmt ();
20409 break;
20410 case DW_LNS_set_basic_block:
20411 break;
20412 /* Add to the address register of the state machine the
20413 address increment value corresponding to special opcode
20414 255. I.e., this value is scaled by the minimum
20415 instruction length since special opcode 255 would have
20416 scaled the increment. */
20417 case DW_LNS_const_add_pc:
20418 state_machine.handle_const_add_pc ();
20419 break;
20420 case DW_LNS_fixed_advance_pc:
20421 {
20422 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20423 line_ptr += 2;
20424
20425 state_machine.handle_fixed_advance_pc (addr_adj);
20426 }
20427 break;
20428 default:
20429 {
20430 /* Unknown standard opcode, ignore it. */
20431 int i;
20432
20433 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20434 {
20435 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20436 line_ptr += bytes_read;
20437 }
20438 }
20439 }
20440 }
20441
20442 if (!end_sequence)
20443 dwarf2_debug_line_missing_end_sequence_complaint ();
20444
20445 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20446 in which case we still finish recording the last line). */
20447 state_machine.record_line (true);
20448 }
20449}
20450
20451/* Decode the Line Number Program (LNP) for the given line_header
20452 structure and CU. The actual information extracted and the type
20453 of structures created from the LNP depends on the value of PST.
20454
20455 1. If PST is NULL, then this procedure uses the data from the program
20456 to create all necessary symbol tables, and their linetables.
20457
20458 2. If PST is not NULL, this procedure reads the program to determine
20459 the list of files included by the unit represented by PST, and
20460 builds all the associated partial symbol tables.
20461
20462 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20463 It is used for relative paths in the line table.
20464 NOTE: When processing partial symtabs (pst != NULL),
20465 comp_dir == pst->dirname.
20466
20467 NOTE: It is important that psymtabs have the same file name (via strcmp)
20468 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20469 symtab we don't use it in the name of the psymtabs we create.
20470 E.g. expand_line_sal requires this when finding psymtabs to expand.
20471 A good testcase for this is mb-inline.exp.
20472
20473 LOWPC is the lowest address in CU (or 0 if not known).
20474
20475 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20476 for its PC<->lines mapping information. Otherwise only the filename
20477 table is read in. */
20478
20479static void
20480dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20481 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20482 CORE_ADDR lowpc, int decode_mapping)
20483{
20484 struct objfile *objfile = cu->per_objfile->objfile;
20485 const int decode_for_pst_p = (pst != NULL);
20486
20487 if (decode_mapping)
20488 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20489
20490 if (decode_for_pst_p)
20491 {
20492 /* Now that we're done scanning the Line Header Program, we can
20493 create the psymtab of each included file. */
20494 for (auto &file_entry : lh->file_names ())
20495 if (file_entry.included_p == 1)
20496 {
20497 gdb::unique_xmalloc_ptr<char> name_holder;
20498 const char *include_name =
20499 psymtab_include_file_name (lh, file_entry, pst,
20500 comp_dir, &name_holder);
20501 if (include_name != NULL)
20502 dwarf2_create_include_psymtab (include_name, pst, objfile);
20503 }
20504 }
20505 else
20506 {
20507 /* Make sure a symtab is created for every file, even files
20508 which contain only variables (i.e. no code with associated
20509 line numbers). */
20510 buildsym_compunit *builder = cu->get_builder ();
20511 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20512
20513 for (auto &fe : lh->file_names ())
20514 {
20515 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20516 if (builder->get_current_subfile ()->symtab == NULL)
20517 {
20518 builder->get_current_subfile ()->symtab
20519 = allocate_symtab (cust,
20520 builder->get_current_subfile ()->name);
20521 }
20522 fe.symtab = builder->get_current_subfile ()->symtab;
20523 }
20524 }
20525}
20526
20527/* Start a subfile for DWARF. FILENAME is the name of the file and
20528 DIRNAME the name of the source directory which contains FILENAME
20529 or NULL if not known.
20530 This routine tries to keep line numbers from identical absolute and
20531 relative file names in a common subfile.
20532
20533 Using the `list' example from the GDB testsuite, which resides in
20534 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20535 of /srcdir/list0.c yields the following debugging information for list0.c:
20536
20537 DW_AT_name: /srcdir/list0.c
20538 DW_AT_comp_dir: /compdir
20539 files.files[0].name: list0.h
20540 files.files[0].dir: /srcdir
20541 files.files[1].name: list0.c
20542 files.files[1].dir: /srcdir
20543
20544 The line number information for list0.c has to end up in a single
20545 subfile, so that `break /srcdir/list0.c:1' works as expected.
20546 start_subfile will ensure that this happens provided that we pass the
20547 concatenation of files.files[1].dir and files.files[1].name as the
20548 subfile's name. */
20549
20550static void
20551dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20552 const char *dirname)
20553{
20554 gdb::unique_xmalloc_ptr<char> copy;
20555
20556 /* In order not to lose the line information directory,
20557 we concatenate it to the filename when it makes sense.
20558 Note that the Dwarf3 standard says (speaking of filenames in line
20559 information): ``The directory index is ignored for file names
20560 that represent full path names''. Thus ignoring dirname in the
20561 `else' branch below isn't an issue. */
20562
20563 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20564 {
20565 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20566 filename = copy.get ();
20567 }
20568
20569 cu->get_builder ()->start_subfile (filename);
20570}
20571
20572/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20573 buildsym_compunit constructor. */
20574
20575struct compunit_symtab *
20576dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20577 CORE_ADDR low_pc)
20578{
20579 gdb_assert (m_builder == nullptr);
20580
20581 m_builder.reset (new struct buildsym_compunit
20582 (per_cu->dwarf2_per_objfile->objfile,
20583 name, comp_dir, language, low_pc));
20584
20585 list_in_scope = get_builder ()->get_file_symbols ();
20586
20587 get_builder ()->record_debugformat ("DWARF 2");
20588 get_builder ()->record_producer (producer);
20589
20590 processing_has_namespace_info = false;
20591
20592 return get_builder ()->get_compunit_symtab ();
20593}
20594
20595static void
20596var_decode_location (struct attribute *attr, struct symbol *sym,
20597 struct dwarf2_cu *cu)
20598{
20599 struct objfile *objfile = cu->per_objfile->objfile;
20600 struct comp_unit_head *cu_header = &cu->header;
20601
20602 /* NOTE drow/2003-01-30: There used to be a comment and some special
20603 code here to turn a symbol with DW_AT_external and a
20604 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20605 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20606 with some versions of binutils) where shared libraries could have
20607 relocations against symbols in their debug information - the
20608 minimal symbol would have the right address, but the debug info
20609 would not. It's no longer necessary, because we will explicitly
20610 apply relocations when we read in the debug information now. */
20611
20612 /* A DW_AT_location attribute with no contents indicates that a
20613 variable has been optimized away. */
20614 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20615 {
20616 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20617 return;
20618 }
20619
20620 /* Handle one degenerate form of location expression specially, to
20621 preserve GDB's previous behavior when section offsets are
20622 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20623 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20624
20625 if (attr->form_is_block ()
20626 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20627 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20628 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20629 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20630 && (DW_BLOCK (attr)->size
20631 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20632 {
20633 unsigned int dummy;
20634
20635 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20636 SET_SYMBOL_VALUE_ADDRESS
20637 (sym, cu->header.read_address (objfile->obfd,
20638 DW_BLOCK (attr)->data + 1,
20639 &dummy));
20640 else
20641 SET_SYMBOL_VALUE_ADDRESS
20642 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20643 &dummy));
20644 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20645 fixup_symbol_section (sym, objfile);
20646 SET_SYMBOL_VALUE_ADDRESS
20647 (sym,
20648 SYMBOL_VALUE_ADDRESS (sym)
20649 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20650 return;
20651 }
20652
20653 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20654 expression evaluator, and use LOC_COMPUTED only when necessary
20655 (i.e. when the value of a register or memory location is
20656 referenced, or a thread-local block, etc.). Then again, it might
20657 not be worthwhile. I'm assuming that it isn't unless performance
20658 or memory numbers show me otherwise. */
20659
20660 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20661
20662 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20663 cu->has_loclist = true;
20664}
20665
20666/* Given a pointer to a DWARF information entry, figure out if we need
20667 to make a symbol table entry for it, and if so, create a new entry
20668 and return a pointer to it.
20669 If TYPE is NULL, determine symbol type from the die, otherwise
20670 used the passed type.
20671 If SPACE is not NULL, use it to hold the new symbol. If it is
20672 NULL, allocate a new symbol on the objfile's obstack. */
20673
20674static struct symbol *
20675new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20676 struct symbol *space)
20677{
20678 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20679 struct objfile *objfile = dwarf2_per_objfile->objfile;
20680 struct gdbarch *gdbarch = objfile->arch ();
20681 struct symbol *sym = NULL;
20682 const char *name;
20683 struct attribute *attr = NULL;
20684 struct attribute *attr2 = NULL;
20685 CORE_ADDR baseaddr;
20686 struct pending **list_to_add = NULL;
20687
20688 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20689
20690 baseaddr = objfile->text_section_offset ();
20691
20692 name = dwarf2_name (die, cu);
20693 if (name)
20694 {
20695 int suppress_add = 0;
20696
20697 if (space)
20698 sym = space;
20699 else
20700 sym = new (&objfile->objfile_obstack) symbol;
20701 OBJSTAT (objfile, n_syms++);
20702
20703 /* Cache this symbol's name and the name's demangled form (if any). */
20704 sym->set_language (cu->language, &objfile->objfile_obstack);
20705 /* Fortran does not have mangling standard and the mangling does differ
20706 between gfortran, iFort etc. */
20707 const char *physname
20708 = (cu->language == language_fortran
20709 ? dwarf2_full_name (name, die, cu)
20710 : dwarf2_physname (name, die, cu));
20711 const char *linkagename = dw2_linkage_name (die, cu);
20712
20713 if (linkagename == nullptr || cu->language == language_ada)
20714 sym->set_linkage_name (physname);
20715 else
20716 {
20717 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20718 sym->set_linkage_name (linkagename);
20719 }
20720
20721 /* Default assumptions.
20722 Use the passed type or decode it from the die. */
20723 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20724 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20725 if (type != NULL)
20726 SYMBOL_TYPE (sym) = type;
20727 else
20728 SYMBOL_TYPE (sym) = die_type (die, cu);
20729 attr = dwarf2_attr (die,
20730 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20731 cu);
20732 if (attr != nullptr)
20733 {
20734 SYMBOL_LINE (sym) = DW_UNSND (attr);
20735 }
20736
20737 attr = dwarf2_attr (die,
20738 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20739 cu);
20740 if (attr != nullptr)
20741 {
20742 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20743 struct file_entry *fe;
20744
20745 if (cu->line_header != NULL)
20746 fe = cu->line_header->file_name_at (file_index);
20747 else
20748 fe = NULL;
20749
20750 if (fe == NULL)
20751 complaint (_("file index out of range"));
20752 else
20753 symbol_set_symtab (sym, fe->symtab);
20754 }
20755
20756 switch (die->tag)
20757 {
20758 case DW_TAG_label:
20759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20760 if (attr != nullptr)
20761 {
20762 CORE_ADDR addr;
20763
20764 addr = attr->value_as_address ();
20765 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20766 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20767 }
20768 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20769 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20770 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20771 add_symbol_to_list (sym, cu->list_in_scope);
20772 break;
20773 case DW_TAG_subprogram:
20774 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20775 finish_block. */
20776 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20777 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20778 if ((attr2 && (DW_UNSND (attr2) != 0))
20779 || cu->language == language_ada
20780 || cu->language == language_fortran)
20781 {
20782 /* Subprograms marked external are stored as a global symbol.
20783 Ada and Fortran subprograms, whether marked external or
20784 not, are always stored as a global symbol, because we want
20785 to be able to access them globally. For instance, we want
20786 to be able to break on a nested subprogram without having
20787 to specify the context. */
20788 list_to_add = cu->get_builder ()->get_global_symbols ();
20789 }
20790 else
20791 {
20792 list_to_add = cu->list_in_scope;
20793 }
20794 break;
20795 case DW_TAG_inlined_subroutine:
20796 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20797 finish_block. */
20798 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20799 SYMBOL_INLINED (sym) = 1;
20800 list_to_add = cu->list_in_scope;
20801 break;
20802 case DW_TAG_template_value_param:
20803 suppress_add = 1;
20804 /* Fall through. */
20805 case DW_TAG_constant:
20806 case DW_TAG_variable:
20807 case DW_TAG_member:
20808 /* Compilation with minimal debug info may result in
20809 variables with missing type entries. Change the
20810 misleading `void' type to something sensible. */
20811 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20812 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20813
20814 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20815 /* In the case of DW_TAG_member, we should only be called for
20816 static const members. */
20817 if (die->tag == DW_TAG_member)
20818 {
20819 /* dwarf2_add_field uses die_is_declaration,
20820 so we do the same. */
20821 gdb_assert (die_is_declaration (die, cu));
20822 gdb_assert (attr);
20823 }
20824 if (attr != nullptr)
20825 {
20826 dwarf2_const_value (attr, sym, cu);
20827 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20828 if (!suppress_add)
20829 {
20830 if (attr2 && (DW_UNSND (attr2) != 0))
20831 list_to_add = cu->get_builder ()->get_global_symbols ();
20832 else
20833 list_to_add = cu->list_in_scope;
20834 }
20835 break;
20836 }
20837 attr = dwarf2_attr (die, DW_AT_location, cu);
20838 if (attr != nullptr)
20839 {
20840 var_decode_location (attr, sym, cu);
20841 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20842
20843 /* Fortran explicitly imports any global symbols to the local
20844 scope by DW_TAG_common_block. */
20845 if (cu->language == language_fortran && die->parent
20846 && die->parent->tag == DW_TAG_common_block)
20847 attr2 = NULL;
20848
20849 if (SYMBOL_CLASS (sym) == LOC_STATIC
20850 && SYMBOL_VALUE_ADDRESS (sym) == 0
20851 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20852 {
20853 /* When a static variable is eliminated by the linker,
20854 the corresponding debug information is not stripped
20855 out, but the variable address is set to null;
20856 do not add such variables into symbol table. */
20857 }
20858 else if (attr2 && (DW_UNSND (attr2) != 0))
20859 {
20860 if (SYMBOL_CLASS (sym) == LOC_STATIC
20861 && (objfile->flags & OBJF_MAINLINE) == 0
20862 && dwarf2_per_objfile->per_bfd->can_copy)
20863 {
20864 /* A global static variable might be subject to
20865 copy relocation. We first check for a local
20866 minsym, though, because maybe the symbol was
20867 marked hidden, in which case this would not
20868 apply. */
20869 bound_minimal_symbol found
20870 = (lookup_minimal_symbol_linkage
20871 (sym->linkage_name (), objfile));
20872 if (found.minsym != nullptr)
20873 sym->maybe_copied = 1;
20874 }
20875
20876 /* A variable with DW_AT_external is never static,
20877 but it may be block-scoped. */
20878 list_to_add
20879 = ((cu->list_in_scope
20880 == cu->get_builder ()->get_file_symbols ())
20881 ? cu->get_builder ()->get_global_symbols ()
20882 : cu->list_in_scope);
20883 }
20884 else
20885 list_to_add = cu->list_in_scope;
20886 }
20887 else
20888 {
20889 /* We do not know the address of this symbol.
20890 If it is an external symbol and we have type information
20891 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20892 The address of the variable will then be determined from
20893 the minimal symbol table whenever the variable is
20894 referenced. */
20895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20896
20897 /* Fortran explicitly imports any global symbols to the local
20898 scope by DW_TAG_common_block. */
20899 if (cu->language == language_fortran && die->parent
20900 && die->parent->tag == DW_TAG_common_block)
20901 {
20902 /* SYMBOL_CLASS doesn't matter here because
20903 read_common_block is going to reset it. */
20904 if (!suppress_add)
20905 list_to_add = cu->list_in_scope;
20906 }
20907 else if (attr2 && (DW_UNSND (attr2) != 0)
20908 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20909 {
20910 /* A variable with DW_AT_external is never static, but it
20911 may be block-scoped. */
20912 list_to_add
20913 = ((cu->list_in_scope
20914 == cu->get_builder ()->get_file_symbols ())
20915 ? cu->get_builder ()->get_global_symbols ()
20916 : cu->list_in_scope);
20917
20918 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20919 }
20920 else if (!die_is_declaration (die, cu))
20921 {
20922 /* Use the default LOC_OPTIMIZED_OUT class. */
20923 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20924 if (!suppress_add)
20925 list_to_add = cu->list_in_scope;
20926 }
20927 }
20928 break;
20929 case DW_TAG_formal_parameter:
20930 {
20931 /* If we are inside a function, mark this as an argument. If
20932 not, we might be looking at an argument to an inlined function
20933 when we do not have enough information to show inlined frames;
20934 pretend it's a local variable in that case so that the user can
20935 still see it. */
20936 struct context_stack *curr
20937 = cu->get_builder ()->get_current_context_stack ();
20938 if (curr != nullptr && curr->name != nullptr)
20939 SYMBOL_IS_ARGUMENT (sym) = 1;
20940 attr = dwarf2_attr (die, DW_AT_location, cu);
20941 if (attr != nullptr)
20942 {
20943 var_decode_location (attr, sym, cu);
20944 }
20945 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20946 if (attr != nullptr)
20947 {
20948 dwarf2_const_value (attr, sym, cu);
20949 }
20950
20951 list_to_add = cu->list_in_scope;
20952 }
20953 break;
20954 case DW_TAG_unspecified_parameters:
20955 /* From varargs functions; gdb doesn't seem to have any
20956 interest in this information, so just ignore it for now.
20957 (FIXME?) */
20958 break;
20959 case DW_TAG_template_type_param:
20960 suppress_add = 1;
20961 /* Fall through. */
20962 case DW_TAG_class_type:
20963 case DW_TAG_interface_type:
20964 case DW_TAG_structure_type:
20965 case DW_TAG_union_type:
20966 case DW_TAG_set_type:
20967 case DW_TAG_enumeration_type:
20968 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20969 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20970
20971 {
20972 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20973 really ever be static objects: otherwise, if you try
20974 to, say, break of a class's method and you're in a file
20975 which doesn't mention that class, it won't work unless
20976 the check for all static symbols in lookup_symbol_aux
20977 saves you. See the OtherFileClass tests in
20978 gdb.c++/namespace.exp. */
20979
20980 if (!suppress_add)
20981 {
20982 buildsym_compunit *builder = cu->get_builder ();
20983 list_to_add
20984 = (cu->list_in_scope == builder->get_file_symbols ()
20985 && cu->language == language_cplus
20986 ? builder->get_global_symbols ()
20987 : cu->list_in_scope);
20988
20989 /* The semantics of C++ state that "struct foo {
20990 ... }" also defines a typedef for "foo". */
20991 if (cu->language == language_cplus
20992 || cu->language == language_ada
20993 || cu->language == language_d
20994 || cu->language == language_rust)
20995 {
20996 /* The symbol's name is already allocated along
20997 with this objfile, so we don't need to
20998 duplicate it for the type. */
20999 if (SYMBOL_TYPE (sym)->name () == 0)
21000 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21001 }
21002 }
21003 }
21004 break;
21005 case DW_TAG_typedef:
21006 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21007 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21008 list_to_add = cu->list_in_scope;
21009 break;
21010 case DW_TAG_base_type:
21011 case DW_TAG_subrange_type:
21012 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21013 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21014 list_to_add = cu->list_in_scope;
21015 break;
21016 case DW_TAG_enumerator:
21017 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21018 if (attr != nullptr)
21019 {
21020 dwarf2_const_value (attr, sym, cu);
21021 }
21022 {
21023 /* NOTE: carlton/2003-11-10: See comment above in the
21024 DW_TAG_class_type, etc. block. */
21025
21026 list_to_add
21027 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21028 && cu->language == language_cplus
21029 ? cu->get_builder ()->get_global_symbols ()
21030 : cu->list_in_scope);
21031 }
21032 break;
21033 case DW_TAG_imported_declaration:
21034 case DW_TAG_namespace:
21035 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21036 list_to_add = cu->get_builder ()->get_global_symbols ();
21037 break;
21038 case DW_TAG_module:
21039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21040 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21041 list_to_add = cu->get_builder ()->get_global_symbols ();
21042 break;
21043 case DW_TAG_common_block:
21044 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21045 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21046 add_symbol_to_list (sym, cu->list_in_scope);
21047 break;
21048 default:
21049 /* Not a tag we recognize. Hopefully we aren't processing
21050 trash data, but since we must specifically ignore things
21051 we don't recognize, there is nothing else we should do at
21052 this point. */
21053 complaint (_("unsupported tag: '%s'"),
21054 dwarf_tag_name (die->tag));
21055 break;
21056 }
21057
21058 if (suppress_add)
21059 {
21060 sym->hash_next = objfile->template_symbols;
21061 objfile->template_symbols = sym;
21062 list_to_add = NULL;
21063 }
21064
21065 if (list_to_add != NULL)
21066 add_symbol_to_list (sym, list_to_add);
21067
21068 /* For the benefit of old versions of GCC, check for anonymous
21069 namespaces based on the demangled name. */
21070 if (!cu->processing_has_namespace_info
21071 && cu->language == language_cplus)
21072 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21073 }
21074 return (sym);
21075}
21076
21077/* Given an attr with a DW_FORM_dataN value in host byte order,
21078 zero-extend it as appropriate for the symbol's type. The DWARF
21079 standard (v4) is not entirely clear about the meaning of using
21080 DW_FORM_dataN for a constant with a signed type, where the type is
21081 wider than the data. The conclusion of a discussion on the DWARF
21082 list was that this is unspecified. We choose to always zero-extend
21083 because that is the interpretation long in use by GCC. */
21084
21085static gdb_byte *
21086dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21087 struct dwarf2_cu *cu, LONGEST *value, int bits)
21088{
21089 struct objfile *objfile = cu->per_objfile->objfile;
21090 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21091 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21092 LONGEST l = DW_UNSND (attr);
21093
21094 if (bits < sizeof (*value) * 8)
21095 {
21096 l &= ((LONGEST) 1 << bits) - 1;
21097 *value = l;
21098 }
21099 else if (bits == sizeof (*value) * 8)
21100 *value = l;
21101 else
21102 {
21103 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21104 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21105 return bytes;
21106 }
21107
21108 return NULL;
21109}
21110
21111/* Read a constant value from an attribute. Either set *VALUE, or if
21112 the value does not fit in *VALUE, set *BYTES - either already
21113 allocated on the objfile obstack, or newly allocated on OBSTACK,
21114 or, set *BATON, if we translated the constant to a location
21115 expression. */
21116
21117static void
21118dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21119 const char *name, struct obstack *obstack,
21120 struct dwarf2_cu *cu,
21121 LONGEST *value, const gdb_byte **bytes,
21122 struct dwarf2_locexpr_baton **baton)
21123{
21124 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21125 struct objfile *objfile = per_objfile->objfile;
21126 struct comp_unit_head *cu_header = &cu->header;
21127 struct dwarf_block *blk;
21128 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21129 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21130
21131 *value = 0;
21132 *bytes = NULL;
21133 *baton = NULL;
21134
21135 switch (attr->form)
21136 {
21137 case DW_FORM_addr:
21138 case DW_FORM_addrx:
21139 case DW_FORM_GNU_addr_index:
21140 {
21141 gdb_byte *data;
21142
21143 if (TYPE_LENGTH (type) != cu_header->addr_size)
21144 dwarf2_const_value_length_mismatch_complaint (name,
21145 cu_header->addr_size,
21146 TYPE_LENGTH (type));
21147 /* Symbols of this form are reasonably rare, so we just
21148 piggyback on the existing location code rather than writing
21149 a new implementation of symbol_computed_ops. */
21150 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21151 (*baton)->per_objfile = per_objfile;
21152 (*baton)->per_cu = cu->per_cu;
21153 gdb_assert ((*baton)->per_cu);
21154
21155 (*baton)->size = 2 + cu_header->addr_size;
21156 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21157 (*baton)->data = data;
21158
21159 data[0] = DW_OP_addr;
21160 store_unsigned_integer (&data[1], cu_header->addr_size,
21161 byte_order, DW_ADDR (attr));
21162 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21163 }
21164 break;
21165 case DW_FORM_string:
21166 case DW_FORM_strp:
21167 case DW_FORM_strx:
21168 case DW_FORM_GNU_str_index:
21169 case DW_FORM_GNU_strp_alt:
21170 /* DW_STRING is already allocated on the objfile obstack, point
21171 directly to it. */
21172 *bytes = (const gdb_byte *) DW_STRING (attr);
21173 break;
21174 case DW_FORM_block1:
21175 case DW_FORM_block2:
21176 case DW_FORM_block4:
21177 case DW_FORM_block:
21178 case DW_FORM_exprloc:
21179 case DW_FORM_data16:
21180 blk = DW_BLOCK (attr);
21181 if (TYPE_LENGTH (type) != blk->size)
21182 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21183 TYPE_LENGTH (type));
21184 *bytes = blk->data;
21185 break;
21186
21187 /* The DW_AT_const_value attributes are supposed to carry the
21188 symbol's value "represented as it would be on the target
21189 architecture." By the time we get here, it's already been
21190 converted to host endianness, so we just need to sign- or
21191 zero-extend it as appropriate. */
21192 case DW_FORM_data1:
21193 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21194 break;
21195 case DW_FORM_data2:
21196 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21197 break;
21198 case DW_FORM_data4:
21199 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21200 break;
21201 case DW_FORM_data8:
21202 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21203 break;
21204
21205 case DW_FORM_sdata:
21206 case DW_FORM_implicit_const:
21207 *value = DW_SND (attr);
21208 break;
21209
21210 case DW_FORM_udata:
21211 *value = DW_UNSND (attr);
21212 break;
21213
21214 default:
21215 complaint (_("unsupported const value attribute form: '%s'"),
21216 dwarf_form_name (attr->form));
21217 *value = 0;
21218 break;
21219 }
21220}
21221
21222
21223/* Copy constant value from an attribute to a symbol. */
21224
21225static void
21226dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21227 struct dwarf2_cu *cu)
21228{
21229 struct objfile *objfile = cu->per_objfile->objfile;
21230 LONGEST value;
21231 const gdb_byte *bytes;
21232 struct dwarf2_locexpr_baton *baton;
21233
21234 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21235 sym->print_name (),
21236 &objfile->objfile_obstack, cu,
21237 &value, &bytes, &baton);
21238
21239 if (baton != NULL)
21240 {
21241 SYMBOL_LOCATION_BATON (sym) = baton;
21242 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21243 }
21244 else if (bytes != NULL)
21245 {
21246 SYMBOL_VALUE_BYTES (sym) = bytes;
21247 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21248 }
21249 else
21250 {
21251 SYMBOL_VALUE (sym) = value;
21252 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21253 }
21254}
21255
21256/* Return the type of the die in question using its DW_AT_type attribute. */
21257
21258static struct type *
21259die_type (struct die_info *die, struct dwarf2_cu *cu)
21260{
21261 struct attribute *type_attr;
21262
21263 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21264 if (!type_attr)
21265 {
21266 struct objfile *objfile = cu->per_objfile->objfile;
21267 /* A missing DW_AT_type represents a void type. */
21268 return objfile_type (objfile)->builtin_void;
21269 }
21270
21271 return lookup_die_type (die, type_attr, cu);
21272}
21273
21274/* True iff CU's producer generates GNAT Ada auxiliary information
21275 that allows to find parallel types through that information instead
21276 of having to do expensive parallel lookups by type name. */
21277
21278static int
21279need_gnat_info (struct dwarf2_cu *cu)
21280{
21281 /* Assume that the Ada compiler was GNAT, which always produces
21282 the auxiliary information. */
21283 return (cu->language == language_ada);
21284}
21285
21286/* Return the auxiliary type of the die in question using its
21287 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21288 attribute is not present. */
21289
21290static struct type *
21291die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21292{
21293 struct attribute *type_attr;
21294
21295 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21296 if (!type_attr)
21297 return NULL;
21298
21299 return lookup_die_type (die, type_attr, cu);
21300}
21301
21302/* If DIE has a descriptive_type attribute, then set the TYPE's
21303 descriptive type accordingly. */
21304
21305static void
21306set_descriptive_type (struct type *type, struct die_info *die,
21307 struct dwarf2_cu *cu)
21308{
21309 struct type *descriptive_type = die_descriptive_type (die, cu);
21310
21311 if (descriptive_type)
21312 {
21313 ALLOCATE_GNAT_AUX_TYPE (type);
21314 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21315 }
21316}
21317
21318/* Return the containing type of the die in question using its
21319 DW_AT_containing_type attribute. */
21320
21321static struct type *
21322die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21323{
21324 struct attribute *type_attr;
21325 struct objfile *objfile = cu->per_objfile->objfile;
21326
21327 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21328 if (!type_attr)
21329 error (_("Dwarf Error: Problem turning containing type into gdb type "
21330 "[in module %s]"), objfile_name (objfile));
21331
21332 return lookup_die_type (die, type_attr, cu);
21333}
21334
21335/* Return an error marker type to use for the ill formed type in DIE/CU. */
21336
21337static struct type *
21338build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21339{
21340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21341 struct objfile *objfile = dwarf2_per_objfile->objfile;
21342 char *saved;
21343
21344 std::string message
21345 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21346 objfile_name (objfile),
21347 sect_offset_str (cu->header.sect_off),
21348 sect_offset_str (die->sect_off));
21349 saved = obstack_strdup (&objfile->objfile_obstack, message);
21350
21351 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21352}
21353
21354/* Look up the type of DIE in CU using its type attribute ATTR.
21355 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21356 DW_AT_containing_type.
21357 If there is no type substitute an error marker. */
21358
21359static struct type *
21360lookup_die_type (struct die_info *die, const struct attribute *attr,
21361 struct dwarf2_cu *cu)
21362{
21363 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21364 struct objfile *objfile = dwarf2_per_objfile->objfile;
21365 struct type *this_type;
21366
21367 gdb_assert (attr->name == DW_AT_type
21368 || attr->name == DW_AT_GNAT_descriptive_type
21369 || attr->name == DW_AT_containing_type);
21370
21371 /* First see if we have it cached. */
21372
21373 if (attr->form == DW_FORM_GNU_ref_alt)
21374 {
21375 struct dwarf2_per_cu_data *per_cu;
21376 sect_offset sect_off = attr->get_ref_die_offset ();
21377
21378 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21379 dwarf2_per_objfile);
21380 this_type = get_die_type_at_offset (sect_off, per_cu);
21381 }
21382 else if (attr->form_is_ref ())
21383 {
21384 sect_offset sect_off = attr->get_ref_die_offset ();
21385
21386 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21387 }
21388 else if (attr->form == DW_FORM_ref_sig8)
21389 {
21390 ULONGEST signature = DW_SIGNATURE (attr);
21391
21392 return get_signatured_type (die, signature, cu);
21393 }
21394 else
21395 {
21396 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21397 " at %s [in module %s]"),
21398 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21399 objfile_name (objfile));
21400 return build_error_marker_type (cu, die);
21401 }
21402
21403 /* If not cached we need to read it in. */
21404
21405 if (this_type == NULL)
21406 {
21407 struct die_info *type_die = NULL;
21408 struct dwarf2_cu *type_cu = cu;
21409
21410 if (attr->form_is_ref ())
21411 type_die = follow_die_ref (die, attr, &type_cu);
21412 if (type_die == NULL)
21413 return build_error_marker_type (cu, die);
21414 /* If we find the type now, it's probably because the type came
21415 from an inter-CU reference and the type's CU got expanded before
21416 ours. */
21417 this_type = read_type_die (type_die, type_cu);
21418 }
21419
21420 /* If we still don't have a type use an error marker. */
21421
21422 if (this_type == NULL)
21423 return build_error_marker_type (cu, die);
21424
21425 return this_type;
21426}
21427
21428/* Return the type in DIE, CU.
21429 Returns NULL for invalid types.
21430
21431 This first does a lookup in die_type_hash,
21432 and only reads the die in if necessary.
21433
21434 NOTE: This can be called when reading in partial or full symbols. */
21435
21436static struct type *
21437read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21438{
21439 struct type *this_type;
21440
21441 this_type = get_die_type (die, cu);
21442 if (this_type)
21443 return this_type;
21444
21445 return read_type_die_1 (die, cu);
21446}
21447
21448/* Read the type in DIE, CU.
21449 Returns NULL for invalid types. */
21450
21451static struct type *
21452read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21453{
21454 struct type *this_type = NULL;
21455
21456 switch (die->tag)
21457 {
21458 case DW_TAG_class_type:
21459 case DW_TAG_interface_type:
21460 case DW_TAG_structure_type:
21461 case DW_TAG_union_type:
21462 this_type = read_structure_type (die, cu);
21463 break;
21464 case DW_TAG_enumeration_type:
21465 this_type = read_enumeration_type (die, cu);
21466 break;
21467 case DW_TAG_subprogram:
21468 case DW_TAG_subroutine_type:
21469 case DW_TAG_inlined_subroutine:
21470 this_type = read_subroutine_type (die, cu);
21471 break;
21472 case DW_TAG_array_type:
21473 this_type = read_array_type (die, cu);
21474 break;
21475 case DW_TAG_set_type:
21476 this_type = read_set_type (die, cu);
21477 break;
21478 case DW_TAG_pointer_type:
21479 this_type = read_tag_pointer_type (die, cu);
21480 break;
21481 case DW_TAG_ptr_to_member_type:
21482 this_type = read_tag_ptr_to_member_type (die, cu);
21483 break;
21484 case DW_TAG_reference_type:
21485 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21486 break;
21487 case DW_TAG_rvalue_reference_type:
21488 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21489 break;
21490 case DW_TAG_const_type:
21491 this_type = read_tag_const_type (die, cu);
21492 break;
21493 case DW_TAG_volatile_type:
21494 this_type = read_tag_volatile_type (die, cu);
21495 break;
21496 case DW_TAG_restrict_type:
21497 this_type = read_tag_restrict_type (die, cu);
21498 break;
21499 case DW_TAG_string_type:
21500 this_type = read_tag_string_type (die, cu);
21501 break;
21502 case DW_TAG_typedef:
21503 this_type = read_typedef (die, cu);
21504 break;
21505 case DW_TAG_subrange_type:
21506 this_type = read_subrange_type (die, cu);
21507 break;
21508 case DW_TAG_base_type:
21509 this_type = read_base_type (die, cu);
21510 break;
21511 case DW_TAG_unspecified_type:
21512 this_type = read_unspecified_type (die, cu);
21513 break;
21514 case DW_TAG_namespace:
21515 this_type = read_namespace_type (die, cu);
21516 break;
21517 case DW_TAG_module:
21518 this_type = read_module_type (die, cu);
21519 break;
21520 case DW_TAG_atomic_type:
21521 this_type = read_tag_atomic_type (die, cu);
21522 break;
21523 default:
21524 complaint (_("unexpected tag in read_type_die: '%s'"),
21525 dwarf_tag_name (die->tag));
21526 break;
21527 }
21528
21529 return this_type;
21530}
21531
21532/* See if we can figure out if the class lives in a namespace. We do
21533 this by looking for a member function; its demangled name will
21534 contain namespace info, if there is any.
21535 Return the computed name or NULL.
21536 Space for the result is allocated on the objfile's obstack.
21537 This is the full-die version of guess_partial_die_structure_name.
21538 In this case we know DIE has no useful parent. */
21539
21540static const char *
21541guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21542{
21543 struct die_info *spec_die;
21544 struct dwarf2_cu *spec_cu;
21545 struct die_info *child;
21546 struct objfile *objfile = cu->per_objfile->objfile;
21547
21548 spec_cu = cu;
21549 spec_die = die_specification (die, &spec_cu);
21550 if (spec_die != NULL)
21551 {
21552 die = spec_die;
21553 cu = spec_cu;
21554 }
21555
21556 for (child = die->child;
21557 child != NULL;
21558 child = child->sibling)
21559 {
21560 if (child->tag == DW_TAG_subprogram)
21561 {
21562 const char *linkage_name = dw2_linkage_name (child, cu);
21563
21564 if (linkage_name != NULL)
21565 {
21566 gdb::unique_xmalloc_ptr<char> actual_name
21567 (language_class_name_from_physname (cu->language_defn,
21568 linkage_name));
21569 const char *name = NULL;
21570
21571 if (actual_name != NULL)
21572 {
21573 const char *die_name = dwarf2_name (die, cu);
21574
21575 if (die_name != NULL
21576 && strcmp (die_name, actual_name.get ()) != 0)
21577 {
21578 /* Strip off the class name from the full name.
21579 We want the prefix. */
21580 int die_name_len = strlen (die_name);
21581 int actual_name_len = strlen (actual_name.get ());
21582 const char *ptr = actual_name.get ();
21583
21584 /* Test for '::' as a sanity check. */
21585 if (actual_name_len > die_name_len + 2
21586 && ptr[actual_name_len - die_name_len - 1] == ':')
21587 name = obstack_strndup (
21588 &objfile->per_bfd->storage_obstack,
21589 ptr, actual_name_len - die_name_len - 2);
21590 }
21591 }
21592 return name;
21593 }
21594 }
21595 }
21596
21597 return NULL;
21598}
21599
21600/* GCC might emit a nameless typedef that has a linkage name. Determine the
21601 prefix part in such case. See
21602 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21603
21604static const char *
21605anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21606{
21607 struct attribute *attr;
21608 const char *base;
21609
21610 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21611 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21612 return NULL;
21613
21614 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21615 return NULL;
21616
21617 attr = dw2_linkage_name_attr (die, cu);
21618 if (attr == NULL || DW_STRING (attr) == NULL)
21619 return NULL;
21620
21621 /* dwarf2_name had to be already called. */
21622 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21623
21624 /* Strip the base name, keep any leading namespaces/classes. */
21625 base = strrchr (DW_STRING (attr), ':');
21626 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21627 return "";
21628
21629 struct objfile *objfile = cu->per_objfile->objfile;
21630 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21631 DW_STRING (attr),
21632 &base[-1] - DW_STRING (attr));
21633}
21634
21635/* Return the name of the namespace/class that DIE is defined within,
21636 or "" if we can't tell. The caller should not xfree the result.
21637
21638 For example, if we're within the method foo() in the following
21639 code:
21640
21641 namespace N {
21642 class C {
21643 void foo () {
21644 }
21645 };
21646 }
21647
21648 then determine_prefix on foo's die will return "N::C". */
21649
21650static const char *
21651determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21652{
21653 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21654 struct die_info *parent, *spec_die;
21655 struct dwarf2_cu *spec_cu;
21656 struct type *parent_type;
21657 const char *retval;
21658
21659 if (cu->language != language_cplus
21660 && cu->language != language_fortran && cu->language != language_d
21661 && cu->language != language_rust)
21662 return "";
21663
21664 retval = anonymous_struct_prefix (die, cu);
21665 if (retval)
21666 return retval;
21667
21668 /* We have to be careful in the presence of DW_AT_specification.
21669 For example, with GCC 3.4, given the code
21670
21671 namespace N {
21672 void foo() {
21673 // Definition of N::foo.
21674 }
21675 }
21676
21677 then we'll have a tree of DIEs like this:
21678
21679 1: DW_TAG_compile_unit
21680 2: DW_TAG_namespace // N
21681 3: DW_TAG_subprogram // declaration of N::foo
21682 4: DW_TAG_subprogram // definition of N::foo
21683 DW_AT_specification // refers to die #3
21684
21685 Thus, when processing die #4, we have to pretend that we're in
21686 the context of its DW_AT_specification, namely the contex of die
21687 #3. */
21688 spec_cu = cu;
21689 spec_die = die_specification (die, &spec_cu);
21690 if (spec_die == NULL)
21691 parent = die->parent;
21692 else
21693 {
21694 parent = spec_die->parent;
21695 cu = spec_cu;
21696 }
21697
21698 if (parent == NULL)
21699 return "";
21700 else if (parent->building_fullname)
21701 {
21702 const char *name;
21703 const char *parent_name;
21704
21705 /* It has been seen on RealView 2.2 built binaries,
21706 DW_TAG_template_type_param types actually _defined_ as
21707 children of the parent class:
21708
21709 enum E {};
21710 template class <class Enum> Class{};
21711 Class<enum E> class_e;
21712
21713 1: DW_TAG_class_type (Class)
21714 2: DW_TAG_enumeration_type (E)
21715 3: DW_TAG_enumerator (enum1:0)
21716 3: DW_TAG_enumerator (enum2:1)
21717 ...
21718 2: DW_TAG_template_type_param
21719 DW_AT_type DW_FORM_ref_udata (E)
21720
21721 Besides being broken debug info, it can put GDB into an
21722 infinite loop. Consider:
21723
21724 When we're building the full name for Class<E>, we'll start
21725 at Class, and go look over its template type parameters,
21726 finding E. We'll then try to build the full name of E, and
21727 reach here. We're now trying to build the full name of E,
21728 and look over the parent DIE for containing scope. In the
21729 broken case, if we followed the parent DIE of E, we'd again
21730 find Class, and once again go look at its template type
21731 arguments, etc., etc. Simply don't consider such parent die
21732 as source-level parent of this die (it can't be, the language
21733 doesn't allow it), and break the loop here. */
21734 name = dwarf2_name (die, cu);
21735 parent_name = dwarf2_name (parent, cu);
21736 complaint (_("template param type '%s' defined within parent '%s'"),
21737 name ? name : "<unknown>",
21738 parent_name ? parent_name : "<unknown>");
21739 return "";
21740 }
21741 else
21742 switch (parent->tag)
21743 {
21744 case DW_TAG_namespace:
21745 parent_type = read_type_die (parent, cu);
21746 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21747 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21748 Work around this problem here. */
21749 if (cu->language == language_cplus
21750 && strcmp (parent_type->name (), "::") == 0)
21751 return "";
21752 /* We give a name to even anonymous namespaces. */
21753 return parent_type->name ();
21754 case DW_TAG_class_type:
21755 case DW_TAG_interface_type:
21756 case DW_TAG_structure_type:
21757 case DW_TAG_union_type:
21758 case DW_TAG_module:
21759 parent_type = read_type_die (parent, cu);
21760 if (parent_type->name () != NULL)
21761 return parent_type->name ();
21762 else
21763 /* An anonymous structure is only allowed non-static data
21764 members; no typedefs, no member functions, et cetera.
21765 So it does not need a prefix. */
21766 return "";
21767 case DW_TAG_compile_unit:
21768 case DW_TAG_partial_unit:
21769 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21770 if (cu->language == language_cplus
21771 && !dwarf2_per_objfile->per_bfd->types.empty ()
21772 && die->child != NULL
21773 && (die->tag == DW_TAG_class_type
21774 || die->tag == DW_TAG_structure_type
21775 || die->tag == DW_TAG_union_type))
21776 {
21777 const char *name = guess_full_die_structure_name (die, cu);
21778 if (name != NULL)
21779 return name;
21780 }
21781 return "";
21782 case DW_TAG_subprogram:
21783 /* Nested subroutines in Fortran get a prefix with the name
21784 of the parent's subroutine. */
21785 if (cu->language == language_fortran)
21786 {
21787 if ((die->tag == DW_TAG_subprogram)
21788 && (dwarf2_name (parent, cu) != NULL))
21789 return dwarf2_name (parent, cu);
21790 }
21791 return determine_prefix (parent, cu);
21792 case DW_TAG_enumeration_type:
21793 parent_type = read_type_die (parent, cu);
21794 if (TYPE_DECLARED_CLASS (parent_type))
21795 {
21796 if (parent_type->name () != NULL)
21797 return parent_type->name ();
21798 return "";
21799 }
21800 /* Fall through. */
21801 default:
21802 return determine_prefix (parent, cu);
21803 }
21804}
21805
21806/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21807 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21808 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21809 an obconcat, otherwise allocate storage for the result. The CU argument is
21810 used to determine the language and hence, the appropriate separator. */
21811
21812#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21813
21814static char *
21815typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21816 int physname, struct dwarf2_cu *cu)
21817{
21818 const char *lead = "";
21819 const char *sep;
21820
21821 if (suffix == NULL || suffix[0] == '\0'
21822 || prefix == NULL || prefix[0] == '\0')
21823 sep = "";
21824 else if (cu->language == language_d)
21825 {
21826 /* For D, the 'main' function could be defined in any module, but it
21827 should never be prefixed. */
21828 if (strcmp (suffix, "D main") == 0)
21829 {
21830 prefix = "";
21831 sep = "";
21832 }
21833 else
21834 sep = ".";
21835 }
21836 else if (cu->language == language_fortran && physname)
21837 {
21838 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21839 DW_AT_MIPS_linkage_name is preferred and used instead. */
21840
21841 lead = "__";
21842 sep = "_MOD_";
21843 }
21844 else
21845 sep = "::";
21846
21847 if (prefix == NULL)
21848 prefix = "";
21849 if (suffix == NULL)
21850 suffix = "";
21851
21852 if (obs == NULL)
21853 {
21854 char *retval
21855 = ((char *)
21856 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21857
21858 strcpy (retval, lead);
21859 strcat (retval, prefix);
21860 strcat (retval, sep);
21861 strcat (retval, suffix);
21862 return retval;
21863 }
21864 else
21865 {
21866 /* We have an obstack. */
21867 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21868 }
21869}
21870
21871/* Get name of a die, return NULL if not found. */
21872
21873static const char *
21874dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21875 struct objfile *objfile)
21876{
21877 if (name && cu->language == language_cplus)
21878 {
21879 gdb::unique_xmalloc_ptr<char> canon_name
21880 = cp_canonicalize_string (name);
21881
21882 if (canon_name != nullptr)
21883 name = objfile->intern (canon_name.get ());
21884 }
21885
21886 return name;
21887}
21888
21889/* Get name of a die, return NULL if not found.
21890 Anonymous namespaces are converted to their magic string. */
21891
21892static const char *
21893dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21894{
21895 struct attribute *attr;
21896 struct objfile *objfile = cu->per_objfile->objfile;
21897
21898 attr = dwarf2_attr (die, DW_AT_name, cu);
21899 if ((!attr || !DW_STRING (attr))
21900 && die->tag != DW_TAG_namespace
21901 && die->tag != DW_TAG_class_type
21902 && die->tag != DW_TAG_interface_type
21903 && die->tag != DW_TAG_structure_type
21904 && die->tag != DW_TAG_union_type)
21905 return NULL;
21906
21907 switch (die->tag)
21908 {
21909 case DW_TAG_compile_unit:
21910 case DW_TAG_partial_unit:
21911 /* Compilation units have a DW_AT_name that is a filename, not
21912 a source language identifier. */
21913 case DW_TAG_enumeration_type:
21914 case DW_TAG_enumerator:
21915 /* These tags always have simple identifiers already; no need
21916 to canonicalize them. */
21917 return DW_STRING (attr);
21918
21919 case DW_TAG_namespace:
21920 if (attr != NULL && DW_STRING (attr) != NULL)
21921 return DW_STRING (attr);
21922 return CP_ANONYMOUS_NAMESPACE_STR;
21923
21924 case DW_TAG_class_type:
21925 case DW_TAG_interface_type:
21926 case DW_TAG_structure_type:
21927 case DW_TAG_union_type:
21928 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21929 structures or unions. These were of the form "._%d" in GCC 4.1,
21930 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21931 and GCC 4.4. We work around this problem by ignoring these. */
21932 if (attr && DW_STRING (attr)
21933 && (startswith (DW_STRING (attr), "._")
21934 || startswith (DW_STRING (attr), "<anonymous")))
21935 return NULL;
21936
21937 /* GCC might emit a nameless typedef that has a linkage name. See
21938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21939 if (!attr || DW_STRING (attr) == NULL)
21940 {
21941 attr = dw2_linkage_name_attr (die, cu);
21942 if (attr == NULL || DW_STRING (attr) == NULL)
21943 return NULL;
21944
21945 /* Avoid demangling DW_STRING (attr) the second time on a second
21946 call for the same DIE. */
21947 if (!DW_STRING_IS_CANONICAL (attr))
21948 {
21949 gdb::unique_xmalloc_ptr<char> demangled
21950 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21951 if (demangled == nullptr)
21952 return nullptr;
21953
21954 DW_STRING (attr) = objfile->intern (demangled.get ());
21955 DW_STRING_IS_CANONICAL (attr) = 1;
21956 }
21957
21958 /* Strip any leading namespaces/classes, keep only the base name.
21959 DW_AT_name for named DIEs does not contain the prefixes. */
21960 const char *base = strrchr (DW_STRING (attr), ':');
21961 if (base && base > DW_STRING (attr) && base[-1] == ':')
21962 return &base[1];
21963 else
21964 return DW_STRING (attr);
21965 }
21966 break;
21967
21968 default:
21969 break;
21970 }
21971
21972 if (!DW_STRING_IS_CANONICAL (attr))
21973 {
21974 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21975 objfile);
21976 DW_STRING_IS_CANONICAL (attr) = 1;
21977 }
21978 return DW_STRING (attr);
21979}
21980
21981/* Return the die that this die in an extension of, or NULL if there
21982 is none. *EXT_CU is the CU containing DIE on input, and the CU
21983 containing the return value on output. */
21984
21985static struct die_info *
21986dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21987{
21988 struct attribute *attr;
21989
21990 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21991 if (attr == NULL)
21992 return NULL;
21993
21994 return follow_die_ref (die, attr, ext_cu);
21995}
21996
21997static void
21998dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21999{
22000 unsigned int i;
22001
22002 print_spaces (indent, f);
22003 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22004 dwarf_tag_name (die->tag), die->abbrev,
22005 sect_offset_str (die->sect_off));
22006
22007 if (die->parent != NULL)
22008 {
22009 print_spaces (indent, f);
22010 fprintf_unfiltered (f, " parent at offset: %s\n",
22011 sect_offset_str (die->parent->sect_off));
22012 }
22013
22014 print_spaces (indent, f);
22015 fprintf_unfiltered (f, " has children: %s\n",
22016 dwarf_bool_name (die->child != NULL));
22017
22018 print_spaces (indent, f);
22019 fprintf_unfiltered (f, " attributes:\n");
22020
22021 for (i = 0; i < die->num_attrs; ++i)
22022 {
22023 print_spaces (indent, f);
22024 fprintf_unfiltered (f, " %s (%s) ",
22025 dwarf_attr_name (die->attrs[i].name),
22026 dwarf_form_name (die->attrs[i].form));
22027
22028 switch (die->attrs[i].form)
22029 {
22030 case DW_FORM_addr:
22031 case DW_FORM_addrx:
22032 case DW_FORM_GNU_addr_index:
22033 fprintf_unfiltered (f, "address: ");
22034 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22035 break;
22036 case DW_FORM_block2:
22037 case DW_FORM_block4:
22038 case DW_FORM_block:
22039 case DW_FORM_block1:
22040 fprintf_unfiltered (f, "block: size %s",
22041 pulongest (DW_BLOCK (&die->attrs[i])->size));
22042 break;
22043 case DW_FORM_exprloc:
22044 fprintf_unfiltered (f, "expression: size %s",
22045 pulongest (DW_BLOCK (&die->attrs[i])->size));
22046 break;
22047 case DW_FORM_data16:
22048 fprintf_unfiltered (f, "constant of 16 bytes");
22049 break;
22050 case DW_FORM_ref_addr:
22051 fprintf_unfiltered (f, "ref address: ");
22052 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22053 break;
22054 case DW_FORM_GNU_ref_alt:
22055 fprintf_unfiltered (f, "alt ref address: ");
22056 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22057 break;
22058 case DW_FORM_ref1:
22059 case DW_FORM_ref2:
22060 case DW_FORM_ref4:
22061 case DW_FORM_ref8:
22062 case DW_FORM_ref_udata:
22063 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22064 (long) (DW_UNSND (&die->attrs[i])));
22065 break;
22066 case DW_FORM_data1:
22067 case DW_FORM_data2:
22068 case DW_FORM_data4:
22069 case DW_FORM_data8:
22070 case DW_FORM_udata:
22071 case DW_FORM_sdata:
22072 fprintf_unfiltered (f, "constant: %s",
22073 pulongest (DW_UNSND (&die->attrs[i])));
22074 break;
22075 case DW_FORM_sec_offset:
22076 fprintf_unfiltered (f, "section offset: %s",
22077 pulongest (DW_UNSND (&die->attrs[i])));
22078 break;
22079 case DW_FORM_ref_sig8:
22080 fprintf_unfiltered (f, "signature: %s",
22081 hex_string (DW_SIGNATURE (&die->attrs[i])));
22082 break;
22083 case DW_FORM_string:
22084 case DW_FORM_strp:
22085 case DW_FORM_line_strp:
22086 case DW_FORM_strx:
22087 case DW_FORM_GNU_str_index:
22088 case DW_FORM_GNU_strp_alt:
22089 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22090 DW_STRING (&die->attrs[i])
22091 ? DW_STRING (&die->attrs[i]) : "",
22092 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22093 break;
22094 case DW_FORM_flag:
22095 if (DW_UNSND (&die->attrs[i]))
22096 fprintf_unfiltered (f, "flag: TRUE");
22097 else
22098 fprintf_unfiltered (f, "flag: FALSE");
22099 break;
22100 case DW_FORM_flag_present:
22101 fprintf_unfiltered (f, "flag: TRUE");
22102 break;
22103 case DW_FORM_indirect:
22104 /* The reader will have reduced the indirect form to
22105 the "base form" so this form should not occur. */
22106 fprintf_unfiltered (f,
22107 "unexpected attribute form: DW_FORM_indirect");
22108 break;
22109 case DW_FORM_implicit_const:
22110 fprintf_unfiltered (f, "constant: %s",
22111 plongest (DW_SND (&die->attrs[i])));
22112 break;
22113 default:
22114 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22115 die->attrs[i].form);
22116 break;
22117 }
22118 fprintf_unfiltered (f, "\n");
22119 }
22120}
22121
22122static void
22123dump_die_for_error (struct die_info *die)
22124{
22125 dump_die_shallow (gdb_stderr, 0, die);
22126}
22127
22128static void
22129dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22130{
22131 int indent = level * 4;
22132
22133 gdb_assert (die != NULL);
22134
22135 if (level >= max_level)
22136 return;
22137
22138 dump_die_shallow (f, indent, die);
22139
22140 if (die->child != NULL)
22141 {
22142 print_spaces (indent, f);
22143 fprintf_unfiltered (f, " Children:");
22144 if (level + 1 < max_level)
22145 {
22146 fprintf_unfiltered (f, "\n");
22147 dump_die_1 (f, level + 1, max_level, die->child);
22148 }
22149 else
22150 {
22151 fprintf_unfiltered (f,
22152 " [not printed, max nesting level reached]\n");
22153 }
22154 }
22155
22156 if (die->sibling != NULL && level > 0)
22157 {
22158 dump_die_1 (f, level, max_level, die->sibling);
22159 }
22160}
22161
22162/* This is called from the pdie macro in gdbinit.in.
22163 It's not static so gcc will keep a copy callable from gdb. */
22164
22165void
22166dump_die (struct die_info *die, int max_level)
22167{
22168 dump_die_1 (gdb_stdlog, 0, max_level, die);
22169}
22170
22171static void
22172store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22173{
22174 void **slot;
22175
22176 slot = htab_find_slot_with_hash (cu->die_hash, die,
22177 to_underlying (die->sect_off),
22178 INSERT);
22179
22180 *slot = die;
22181}
22182
22183/* Follow reference or signature attribute ATTR of SRC_DIE.
22184 On entry *REF_CU is the CU of SRC_DIE.
22185 On exit *REF_CU is the CU of the result. */
22186
22187static struct die_info *
22188follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22189 struct dwarf2_cu **ref_cu)
22190{
22191 struct die_info *die;
22192
22193 if (attr->form_is_ref ())
22194 die = follow_die_ref (src_die, attr, ref_cu);
22195 else if (attr->form == DW_FORM_ref_sig8)
22196 die = follow_die_sig (src_die, attr, ref_cu);
22197 else
22198 {
22199 dump_die_for_error (src_die);
22200 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22201 objfile_name ((*ref_cu)->per_objfile->objfile));
22202 }
22203
22204 return die;
22205}
22206
22207/* Follow reference OFFSET.
22208 On entry *REF_CU is the CU of the source die referencing OFFSET.
22209 On exit *REF_CU is the CU of the result.
22210 Returns NULL if OFFSET is invalid. */
22211
22212static struct die_info *
22213follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22214 struct dwarf2_cu **ref_cu)
22215{
22216 struct die_info temp_die;
22217 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22218 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22219
22220 gdb_assert (cu->per_cu != NULL);
22221
22222 target_cu = cu;
22223
22224 if (cu->per_cu->is_debug_types)
22225 {
22226 /* .debug_types CUs cannot reference anything outside their CU.
22227 If they need to, they have to reference a signatured type via
22228 DW_FORM_ref_sig8. */
22229 if (!cu->header.offset_in_cu_p (sect_off))
22230 return NULL;
22231 }
22232 else if (offset_in_dwz != cu->per_cu->is_dwz
22233 || !cu->header.offset_in_cu_p (sect_off))
22234 {
22235 struct dwarf2_per_cu_data *per_cu;
22236
22237 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22238 dwarf2_per_objfile);
22239
22240 /* If necessary, add it to the queue and load its DIEs. */
22241 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22242 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22243
22244 target_cu = per_cu->cu;
22245 }
22246 else if (cu->dies == NULL)
22247 {
22248 /* We're loading full DIEs during partial symbol reading. */
22249 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22250 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22251 language_minimal);
22252 }
22253
22254 *ref_cu = target_cu;
22255 temp_die.sect_off = sect_off;
22256
22257 if (target_cu != cu)
22258 target_cu->ancestor = cu;
22259
22260 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22261 &temp_die,
22262 to_underlying (sect_off));
22263}
22264
22265/* Follow reference attribute ATTR of SRC_DIE.
22266 On entry *REF_CU is the CU of SRC_DIE.
22267 On exit *REF_CU is the CU of the result. */
22268
22269static struct die_info *
22270follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22271 struct dwarf2_cu **ref_cu)
22272{
22273 sect_offset sect_off = attr->get_ref_die_offset ();
22274 struct dwarf2_cu *cu = *ref_cu;
22275 struct die_info *die;
22276
22277 die = follow_die_offset (sect_off,
22278 (attr->form == DW_FORM_GNU_ref_alt
22279 || cu->per_cu->is_dwz),
22280 ref_cu);
22281 if (!die)
22282 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22283 "at %s [in module %s]"),
22284 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22285 objfile_name (cu->per_objfile->objfile));
22286
22287 return die;
22288}
22289
22290/* See read.h. */
22291
22292struct dwarf2_locexpr_baton
22293dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22294 dwarf2_per_cu_data *per_cu,
22295 CORE_ADDR (*get_frame_pc) (void *baton),
22296 void *baton, bool resolve_abstract_p)
22297{
22298 struct dwarf2_cu *cu;
22299 struct die_info *die;
22300 struct attribute *attr;
22301 struct dwarf2_locexpr_baton retval;
22302 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22303 struct objfile *objfile = dwarf2_per_objfile->objfile;
22304
22305 if (per_cu->cu == NULL)
22306 load_cu (per_cu, dwarf2_per_objfile, false);
22307 cu = per_cu->cu;
22308 if (cu == NULL)
22309 {
22310 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22311 Instead just throw an error, not much else we can do. */
22312 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22313 sect_offset_str (sect_off), objfile_name (objfile));
22314 }
22315
22316 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22317 if (!die)
22318 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22319 sect_offset_str (sect_off), objfile_name (objfile));
22320
22321 attr = dwarf2_attr (die, DW_AT_location, cu);
22322 if (!attr && resolve_abstract_p
22323 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22324 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22325 {
22326 CORE_ADDR pc = (*get_frame_pc) (baton);
22327 CORE_ADDR baseaddr = objfile->text_section_offset ();
22328 struct gdbarch *gdbarch = objfile->arch ();
22329
22330 for (const auto &cand_off
22331 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22332 {
22333 struct dwarf2_cu *cand_cu = cu;
22334 struct die_info *cand
22335 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22336 if (!cand
22337 || !cand->parent
22338 || cand->parent->tag != DW_TAG_subprogram)
22339 continue;
22340
22341 CORE_ADDR pc_low, pc_high;
22342 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22343 if (pc_low == ((CORE_ADDR) -1))
22344 continue;
22345 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22346 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22347 if (!(pc_low <= pc && pc < pc_high))
22348 continue;
22349
22350 die = cand;
22351 attr = dwarf2_attr (die, DW_AT_location, cu);
22352 break;
22353 }
22354 }
22355
22356 if (!attr)
22357 {
22358 /* DWARF: "If there is no such attribute, then there is no effect.".
22359 DATA is ignored if SIZE is 0. */
22360
22361 retval.data = NULL;
22362 retval.size = 0;
22363 }
22364 else if (attr->form_is_section_offset ())
22365 {
22366 struct dwarf2_loclist_baton loclist_baton;
22367 CORE_ADDR pc = (*get_frame_pc) (baton);
22368 size_t size;
22369
22370 fill_in_loclist_baton (cu, &loclist_baton, attr);
22371
22372 retval.data = dwarf2_find_location_expression (&loclist_baton,
22373 &size, pc);
22374 retval.size = size;
22375 }
22376 else
22377 {
22378 if (!attr->form_is_block ())
22379 error (_("Dwarf Error: DIE at %s referenced in module %s "
22380 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22381 sect_offset_str (sect_off), objfile_name (objfile));
22382
22383 retval.data = DW_BLOCK (attr)->data;
22384 retval.size = DW_BLOCK (attr)->size;
22385 }
22386 retval.per_objfile = dwarf2_per_objfile;
22387 retval.per_cu = cu->per_cu;
22388
22389 age_cached_comp_units (dwarf2_per_objfile);
22390
22391 return retval;
22392}
22393
22394/* See read.h. */
22395
22396struct dwarf2_locexpr_baton
22397dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22398 dwarf2_per_cu_data *per_cu,
22399 CORE_ADDR (*get_frame_pc) (void *baton),
22400 void *baton)
22401{
22402 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22403
22404 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22405}
22406
22407/* Write a constant of a given type as target-ordered bytes into
22408 OBSTACK. */
22409
22410static const gdb_byte *
22411write_constant_as_bytes (struct obstack *obstack,
22412 enum bfd_endian byte_order,
22413 struct type *type,
22414 ULONGEST value,
22415 LONGEST *len)
22416{
22417 gdb_byte *result;
22418
22419 *len = TYPE_LENGTH (type);
22420 result = (gdb_byte *) obstack_alloc (obstack, *len);
22421 store_unsigned_integer (result, *len, byte_order, value);
22422
22423 return result;
22424}
22425
22426/* See read.h. */
22427
22428const gdb_byte *
22429dwarf2_fetch_constant_bytes (sect_offset sect_off,
22430 dwarf2_per_cu_data *per_cu,
22431 obstack *obstack,
22432 LONGEST *len)
22433{
22434 struct dwarf2_cu *cu;
22435 struct die_info *die;
22436 struct attribute *attr;
22437 const gdb_byte *result = NULL;
22438 struct type *type;
22439 LONGEST value;
22440 enum bfd_endian byte_order;
22441 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22442
22443 if (per_cu->cu == NULL)
22444 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22445 cu = per_cu->cu;
22446 if (cu == NULL)
22447 {
22448 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22449 Instead just throw an error, not much else we can do. */
22450 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22451 sect_offset_str (sect_off), objfile_name (objfile));
22452 }
22453
22454 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22455 if (!die)
22456 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22457 sect_offset_str (sect_off), objfile_name (objfile));
22458
22459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22460 if (attr == NULL)
22461 return NULL;
22462
22463 byte_order = (bfd_big_endian (objfile->obfd)
22464 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22465
22466 switch (attr->form)
22467 {
22468 case DW_FORM_addr:
22469 case DW_FORM_addrx:
22470 case DW_FORM_GNU_addr_index:
22471 {
22472 gdb_byte *tem;
22473
22474 *len = cu->header.addr_size;
22475 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22476 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22477 result = tem;
22478 }
22479 break;
22480 case DW_FORM_string:
22481 case DW_FORM_strp:
22482 case DW_FORM_strx:
22483 case DW_FORM_GNU_str_index:
22484 case DW_FORM_GNU_strp_alt:
22485 /* DW_STRING is already allocated on the objfile obstack, point
22486 directly to it. */
22487 result = (const gdb_byte *) DW_STRING (attr);
22488 *len = strlen (DW_STRING (attr));
22489 break;
22490 case DW_FORM_block1:
22491 case DW_FORM_block2:
22492 case DW_FORM_block4:
22493 case DW_FORM_block:
22494 case DW_FORM_exprloc:
22495 case DW_FORM_data16:
22496 result = DW_BLOCK (attr)->data;
22497 *len = DW_BLOCK (attr)->size;
22498 break;
22499
22500 /* The DW_AT_const_value attributes are supposed to carry the
22501 symbol's value "represented as it would be on the target
22502 architecture." By the time we get here, it's already been
22503 converted to host endianness, so we just need to sign- or
22504 zero-extend it as appropriate. */
22505 case DW_FORM_data1:
22506 type = die_type (die, cu);
22507 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22508 if (result == NULL)
22509 result = write_constant_as_bytes (obstack, byte_order,
22510 type, value, len);
22511 break;
22512 case DW_FORM_data2:
22513 type = die_type (die, cu);
22514 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22515 if (result == NULL)
22516 result = write_constant_as_bytes (obstack, byte_order,
22517 type, value, len);
22518 break;
22519 case DW_FORM_data4:
22520 type = die_type (die, cu);
22521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22522 if (result == NULL)
22523 result = write_constant_as_bytes (obstack, byte_order,
22524 type, value, len);
22525 break;
22526 case DW_FORM_data8:
22527 type = die_type (die, cu);
22528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22529 if (result == NULL)
22530 result = write_constant_as_bytes (obstack, byte_order,
22531 type, value, len);
22532 break;
22533
22534 case DW_FORM_sdata:
22535 case DW_FORM_implicit_const:
22536 type = die_type (die, cu);
22537 result = write_constant_as_bytes (obstack, byte_order,
22538 type, DW_SND (attr), len);
22539 break;
22540
22541 case DW_FORM_udata:
22542 type = die_type (die, cu);
22543 result = write_constant_as_bytes (obstack, byte_order,
22544 type, DW_UNSND (attr), len);
22545 break;
22546
22547 default:
22548 complaint (_("unsupported const value attribute form: '%s'"),
22549 dwarf_form_name (attr->form));
22550 break;
22551 }
22552
22553 return result;
22554}
22555
22556/* See read.h. */
22557
22558struct type *
22559dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22560 dwarf2_per_cu_data *per_cu)
22561{
22562 struct dwarf2_cu *cu;
22563 struct die_info *die;
22564
22565 if (per_cu->cu == NULL)
22566 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22567 cu = per_cu->cu;
22568 if (!cu)
22569 return NULL;
22570
22571 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22572 if (!die)
22573 return NULL;
22574
22575 return die_type (die, cu);
22576}
22577
22578/* See read.h. */
22579
22580struct type *
22581dwarf2_get_die_type (cu_offset die_offset,
22582 struct dwarf2_per_cu_data *per_cu)
22583{
22584 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22585 return get_die_type_at_offset (die_offset_sect, per_cu);
22586}
22587
22588/* Follow type unit SIG_TYPE referenced by SRC_DIE.
22589 On entry *REF_CU is the CU of SRC_DIE.
22590 On exit *REF_CU is the CU of the result.
22591 Returns NULL if the referenced DIE isn't found. */
22592
22593static struct die_info *
22594follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22595 struct dwarf2_cu **ref_cu)
22596{
22597 struct die_info temp_die;
22598 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22599 struct die_info *die;
22600
22601 /* While it might be nice to assert sig_type->type == NULL here,
22602 we can get here for DW_AT_imported_declaration where we need
22603 the DIE not the type. */
22604
22605 /* If necessary, add it to the queue and load its DIEs. */
22606
22607 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22608 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
22609
22610 sig_cu = sig_type->per_cu.cu;
22611 gdb_assert (sig_cu != NULL);
22612 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22613 temp_die.sect_off = sig_type->type_offset_in_section;
22614 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22615 to_underlying (temp_die.sect_off));
22616 if (die)
22617 {
22618 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22619
22620 /* For .gdb_index version 7 keep track of included TUs.
22621 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22622 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22623 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22624 {
22625 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22626 }
22627
22628 *ref_cu = sig_cu;
22629 if (sig_cu != cu)
22630 sig_cu->ancestor = cu;
22631
22632 return die;
22633 }
22634
22635 return NULL;
22636}
22637
22638/* Follow signatured type referenced by ATTR in SRC_DIE.
22639 On entry *REF_CU is the CU of SRC_DIE.
22640 On exit *REF_CU is the CU of the result.
22641 The result is the DIE of the type.
22642 If the referenced type cannot be found an error is thrown. */
22643
22644static struct die_info *
22645follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22646 struct dwarf2_cu **ref_cu)
22647{
22648 ULONGEST signature = DW_SIGNATURE (attr);
22649 struct signatured_type *sig_type;
22650 struct die_info *die;
22651
22652 gdb_assert (attr->form == DW_FORM_ref_sig8);
22653
22654 sig_type = lookup_signatured_type (*ref_cu, signature);
22655 /* sig_type will be NULL if the signatured type is missing from
22656 the debug info. */
22657 if (sig_type == NULL)
22658 {
22659 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22660 " from DIE at %s [in module %s]"),
22661 hex_string (signature), sect_offset_str (src_die->sect_off),
22662 objfile_name ((*ref_cu)->per_objfile->objfile));
22663 }
22664
22665 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22666 if (die == NULL)
22667 {
22668 dump_die_for_error (src_die);
22669 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22670 " from DIE at %s [in module %s]"),
22671 hex_string (signature), sect_offset_str (src_die->sect_off),
22672 objfile_name ((*ref_cu)->per_objfile->objfile));
22673 }
22674
22675 return die;
22676}
22677
22678/* Get the type specified by SIGNATURE referenced in DIE/CU,
22679 reading in and processing the type unit if necessary. */
22680
22681static struct type *
22682get_signatured_type (struct die_info *die, ULONGEST signature,
22683 struct dwarf2_cu *cu)
22684{
22685 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22686 struct signatured_type *sig_type;
22687 struct dwarf2_cu *type_cu;
22688 struct die_info *type_die;
22689 struct type *type;
22690
22691 sig_type = lookup_signatured_type (cu, signature);
22692 /* sig_type will be NULL if the signatured type is missing from
22693 the debug info. */
22694 if (sig_type == NULL)
22695 {
22696 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22697 " from DIE at %s [in module %s]"),
22698 hex_string (signature), sect_offset_str (die->sect_off),
22699 objfile_name (dwarf2_per_objfile->objfile));
22700 return build_error_marker_type (cu, die);
22701 }
22702
22703 /* If we already know the type we're done. */
22704 if (sig_type->type != NULL)
22705 return sig_type->type;
22706
22707 type_cu = cu;
22708 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22709 if (type_die != NULL)
22710 {
22711 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22712 is created. This is important, for example, because for c++ classes
22713 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22714 type = read_type_die (type_die, type_cu);
22715 if (type == NULL)
22716 {
22717 complaint (_("Dwarf Error: Cannot build signatured type %s"
22718 " referenced from DIE at %s [in module %s]"),
22719 hex_string (signature), sect_offset_str (die->sect_off),
22720 objfile_name (dwarf2_per_objfile->objfile));
22721 type = build_error_marker_type (cu, die);
22722 }
22723 }
22724 else
22725 {
22726 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22727 " from DIE at %s [in module %s]"),
22728 hex_string (signature), sect_offset_str (die->sect_off),
22729 objfile_name (dwarf2_per_objfile->objfile));
22730 type = build_error_marker_type (cu, die);
22731 }
22732 sig_type->type = type;
22733
22734 return type;
22735}
22736
22737/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22738 reading in and processing the type unit if necessary. */
22739
22740static struct type *
22741get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22742 struct dwarf2_cu *cu) /* ARI: editCase function */
22743{
22744 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22745 if (attr->form_is_ref ())
22746 {
22747 struct dwarf2_cu *type_cu = cu;
22748 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22749
22750 return read_type_die (type_die, type_cu);
22751 }
22752 else if (attr->form == DW_FORM_ref_sig8)
22753 {
22754 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22755 }
22756 else
22757 {
22758 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22759
22760 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22761 " at %s [in module %s]"),
22762 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22763 objfile_name (dwarf2_per_objfile->objfile));
22764 return build_error_marker_type (cu, die);
22765 }
22766}
22767
22768/* Load the DIEs associated with type unit PER_CU into memory. */
22769
22770static void
22771load_full_type_unit (dwarf2_per_cu_data *per_cu,
22772 dwarf2_per_objfile *per_objfile)
22773{
22774 struct signatured_type *sig_type;
22775
22776 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22777 gdb_assert (! per_cu->type_unit_group_p ());
22778
22779 /* We have the per_cu, but we need the signatured_type.
22780 Fortunately this is an easy translation. */
22781 gdb_assert (per_cu->is_debug_types);
22782 sig_type = (struct signatured_type *) per_cu;
22783
22784 gdb_assert (per_cu->cu == NULL);
22785
22786 read_signatured_type (sig_type, per_objfile);
22787
22788 gdb_assert (per_cu->cu != NULL);
22789}
22790
22791/* Read in a signatured type and build its CU and DIEs.
22792 If the type is a stub for the real type in a DWO file,
22793 read in the real type from the DWO file as well. */
22794
22795static void
22796read_signatured_type (signatured_type *sig_type,
22797 dwarf2_per_objfile *per_objfile)
22798{
22799 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22800
22801 gdb_assert (per_cu->is_debug_types);
22802 gdb_assert (per_cu->cu == NULL);
22803
22804 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22805
22806 if (!reader.dummy_p)
22807 {
22808 struct dwarf2_cu *cu = reader.cu;
22809 const gdb_byte *info_ptr = reader.info_ptr;
22810
22811 gdb_assert (cu->die_hash == NULL);
22812 cu->die_hash =
22813 htab_create_alloc_ex (cu->header.length / 12,
22814 die_hash,
22815 die_eq,
22816 NULL,
22817 &cu->comp_unit_obstack,
22818 hashtab_obstack_allocate,
22819 dummy_obstack_deallocate);
22820
22821 if (reader.comp_unit_die->has_children)
22822 reader.comp_unit_die->child
22823 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22824 reader.comp_unit_die);
22825 cu->dies = reader.comp_unit_die;
22826 /* comp_unit_die is not stored in die_hash, no need. */
22827
22828 /* We try not to read any attributes in this function, because
22829 not all CUs needed for references have been loaded yet, and
22830 symbol table processing isn't initialized. But we have to
22831 set the CU language, or we won't be able to build types
22832 correctly. Similarly, if we do not read the producer, we can
22833 not apply producer-specific interpretation. */
22834 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22835
22836 reader.keep ();
22837 }
22838
22839 sig_type->per_cu.tu_read = 1;
22840}
22841
22842/* Decode simple location descriptions.
22843 Given a pointer to a dwarf block that defines a location, compute
22844 the location and return the value. If COMPUTED is non-null, it is
22845 set to true to indicate that decoding was successful, and false
22846 otherwise. If COMPUTED is null, then this function may emit a
22847 complaint. */
22848
22849static CORE_ADDR
22850decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22851{
22852 struct objfile *objfile = cu->per_objfile->objfile;
22853 size_t i;
22854 size_t size = blk->size;
22855 const gdb_byte *data = blk->data;
22856 CORE_ADDR stack[64];
22857 int stacki;
22858 unsigned int bytes_read, unsnd;
22859 gdb_byte op;
22860
22861 if (computed != nullptr)
22862 *computed = false;
22863
22864 i = 0;
22865 stacki = 0;
22866 stack[stacki] = 0;
22867 stack[++stacki] = 0;
22868
22869 while (i < size)
22870 {
22871 op = data[i++];
22872 switch (op)
22873 {
22874 case DW_OP_lit0:
22875 case DW_OP_lit1:
22876 case DW_OP_lit2:
22877 case DW_OP_lit3:
22878 case DW_OP_lit4:
22879 case DW_OP_lit5:
22880 case DW_OP_lit6:
22881 case DW_OP_lit7:
22882 case DW_OP_lit8:
22883 case DW_OP_lit9:
22884 case DW_OP_lit10:
22885 case DW_OP_lit11:
22886 case DW_OP_lit12:
22887 case DW_OP_lit13:
22888 case DW_OP_lit14:
22889 case DW_OP_lit15:
22890 case DW_OP_lit16:
22891 case DW_OP_lit17:
22892 case DW_OP_lit18:
22893 case DW_OP_lit19:
22894 case DW_OP_lit20:
22895 case DW_OP_lit21:
22896 case DW_OP_lit22:
22897 case DW_OP_lit23:
22898 case DW_OP_lit24:
22899 case DW_OP_lit25:
22900 case DW_OP_lit26:
22901 case DW_OP_lit27:
22902 case DW_OP_lit28:
22903 case DW_OP_lit29:
22904 case DW_OP_lit30:
22905 case DW_OP_lit31:
22906 stack[++stacki] = op - DW_OP_lit0;
22907 break;
22908
22909 case DW_OP_reg0:
22910 case DW_OP_reg1:
22911 case DW_OP_reg2:
22912 case DW_OP_reg3:
22913 case DW_OP_reg4:
22914 case DW_OP_reg5:
22915 case DW_OP_reg6:
22916 case DW_OP_reg7:
22917 case DW_OP_reg8:
22918 case DW_OP_reg9:
22919 case DW_OP_reg10:
22920 case DW_OP_reg11:
22921 case DW_OP_reg12:
22922 case DW_OP_reg13:
22923 case DW_OP_reg14:
22924 case DW_OP_reg15:
22925 case DW_OP_reg16:
22926 case DW_OP_reg17:
22927 case DW_OP_reg18:
22928 case DW_OP_reg19:
22929 case DW_OP_reg20:
22930 case DW_OP_reg21:
22931 case DW_OP_reg22:
22932 case DW_OP_reg23:
22933 case DW_OP_reg24:
22934 case DW_OP_reg25:
22935 case DW_OP_reg26:
22936 case DW_OP_reg27:
22937 case DW_OP_reg28:
22938 case DW_OP_reg29:
22939 case DW_OP_reg30:
22940 case DW_OP_reg31:
22941 stack[++stacki] = op - DW_OP_reg0;
22942 if (i < size)
22943 {
22944 if (computed == nullptr)
22945 dwarf2_complex_location_expr_complaint ();
22946 else
22947 return 0;
22948 }
22949 break;
22950
22951 case DW_OP_regx:
22952 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22953 i += bytes_read;
22954 stack[++stacki] = unsnd;
22955 if (i < size)
22956 {
22957 if (computed == nullptr)
22958 dwarf2_complex_location_expr_complaint ();
22959 else
22960 return 0;
22961 }
22962 break;
22963
22964 case DW_OP_addr:
22965 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22966 &bytes_read);
22967 i += bytes_read;
22968 break;
22969
22970 case DW_OP_const1u:
22971 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22972 i += 1;
22973 break;
22974
22975 case DW_OP_const1s:
22976 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22977 i += 1;
22978 break;
22979
22980 case DW_OP_const2u:
22981 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22982 i += 2;
22983 break;
22984
22985 case DW_OP_const2s:
22986 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22987 i += 2;
22988 break;
22989
22990 case DW_OP_const4u:
22991 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22992 i += 4;
22993 break;
22994
22995 case DW_OP_const4s:
22996 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22997 i += 4;
22998 break;
22999
23000 case DW_OP_const8u:
23001 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23002 i += 8;
23003 break;
23004
23005 case DW_OP_constu:
23006 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23007 &bytes_read);
23008 i += bytes_read;
23009 break;
23010
23011 case DW_OP_consts:
23012 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23013 i += bytes_read;
23014 break;
23015
23016 case DW_OP_dup:
23017 stack[stacki + 1] = stack[stacki];
23018 stacki++;
23019 break;
23020
23021 case DW_OP_plus:
23022 stack[stacki - 1] += stack[stacki];
23023 stacki--;
23024 break;
23025
23026 case DW_OP_plus_uconst:
23027 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23028 &bytes_read);
23029 i += bytes_read;
23030 break;
23031
23032 case DW_OP_minus:
23033 stack[stacki - 1] -= stack[stacki];
23034 stacki--;
23035 break;
23036
23037 case DW_OP_deref:
23038 /* If we're not the last op, then we definitely can't encode
23039 this using GDB's address_class enum. This is valid for partial
23040 global symbols, although the variable's address will be bogus
23041 in the psymtab. */
23042 if (i < size)
23043 {
23044 if (computed == nullptr)
23045 dwarf2_complex_location_expr_complaint ();
23046 else
23047 return 0;
23048 }
23049 break;
23050
23051 case DW_OP_GNU_push_tls_address:
23052 case DW_OP_form_tls_address:
23053 /* The top of the stack has the offset from the beginning
23054 of the thread control block at which the variable is located. */
23055 /* Nothing should follow this operator, so the top of stack would
23056 be returned. */
23057 /* This is valid for partial global symbols, but the variable's
23058 address will be bogus in the psymtab. Make it always at least
23059 non-zero to not look as a variable garbage collected by linker
23060 which have DW_OP_addr 0. */
23061 if (i < size)
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
23068 stack[stacki]++;
23069 break;
23070
23071 case DW_OP_GNU_uninit:
23072 if (computed != nullptr)
23073 return 0;
23074 break;
23075
23076 case DW_OP_addrx:
23077 case DW_OP_GNU_addr_index:
23078 case DW_OP_GNU_const_index:
23079 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23080 &bytes_read);
23081 i += bytes_read;
23082 break;
23083
23084 default:
23085 if (computed == nullptr)
23086 {
23087 const char *name = get_DW_OP_name (op);
23088
23089 if (name)
23090 complaint (_("unsupported stack op: '%s'"),
23091 name);
23092 else
23093 complaint (_("unsupported stack op: '%02x'"),
23094 op);
23095 }
23096
23097 return (stack[stacki]);
23098 }
23099
23100 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23101 outside of the allocated space. Also enforce minimum>0. */
23102 if (stacki >= ARRAY_SIZE (stack) - 1)
23103 {
23104 if (computed == nullptr)
23105 complaint (_("location description stack overflow"));
23106 return 0;
23107 }
23108
23109 if (stacki <= 0)
23110 {
23111 if (computed == nullptr)
23112 complaint (_("location description stack underflow"));
23113 return 0;
23114 }
23115 }
23116
23117 if (computed != nullptr)
23118 *computed = true;
23119 return (stack[stacki]);
23120}
23121
23122/* memory allocation interface */
23123
23124static struct dwarf_block *
23125dwarf_alloc_block (struct dwarf2_cu *cu)
23126{
23127 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23128}
23129
23130static struct die_info *
23131dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23132{
23133 struct die_info *die;
23134 size_t size = sizeof (struct die_info);
23135
23136 if (num_attrs > 1)
23137 size += (num_attrs - 1) * sizeof (struct attribute);
23138
23139 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23140 memset (die, 0, sizeof (struct die_info));
23141 return (die);
23142}
23143
23144\f
23145
23146/* Macro support. */
23147
23148/* An overload of dwarf_decode_macros that finds the correct section
23149 and ensures it is read in before calling the other overload. */
23150
23151static void
23152dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23153 int section_is_gnu)
23154{
23155 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23156 struct objfile *objfile = dwarf2_per_objfile->objfile;
23157 const struct line_header *lh = cu->line_header;
23158 unsigned int offset_size = cu->header.offset_size;
23159 struct dwarf2_section_info *section;
23160 const char *section_name;
23161
23162 if (cu->dwo_unit != nullptr)
23163 {
23164 if (section_is_gnu)
23165 {
23166 section = &cu->dwo_unit->dwo_file->sections.macro;
23167 section_name = ".debug_macro.dwo";
23168 }
23169 else
23170 {
23171 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23172 section_name = ".debug_macinfo.dwo";
23173 }
23174 }
23175 else
23176 {
23177 if (section_is_gnu)
23178 {
23179 section = &dwarf2_per_objfile->per_bfd->macro;
23180 section_name = ".debug_macro";
23181 }
23182 else
23183 {
23184 section = &dwarf2_per_objfile->per_bfd->macinfo;
23185 section_name = ".debug_macinfo";
23186 }
23187 }
23188
23189 section->read (objfile);
23190 if (section->buffer == nullptr)
23191 {
23192 complaint (_("missing %s section"), section_name);
23193 return;
23194 }
23195
23196 buildsym_compunit *builder = cu->get_builder ();
23197
23198 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23199 offset_size, offset, section_is_gnu);
23200}
23201
23202/* Return the .debug_loc section to use for CU.
23203 For DWO files use .debug_loc.dwo. */
23204
23205static struct dwarf2_section_info *
23206cu_debug_loc_section (struct dwarf2_cu *cu)
23207{
23208 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23209
23210 if (cu->dwo_unit)
23211 {
23212 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23213
23214 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23215 }
23216 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23217 : &dwarf2_per_objfile->per_bfd->loc);
23218}
23219
23220/* A helper function that fills in a dwarf2_loclist_baton. */
23221
23222static void
23223fill_in_loclist_baton (struct dwarf2_cu *cu,
23224 struct dwarf2_loclist_baton *baton,
23225 const struct attribute *attr)
23226{
23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23228 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23229
23230 section->read (dwarf2_per_objfile->objfile);
23231
23232 baton->per_objfile = dwarf2_per_objfile;
23233 baton->per_cu = cu->per_cu;
23234 gdb_assert (baton->per_cu);
23235 /* We don't know how long the location list is, but make sure we
23236 don't run off the edge of the section. */
23237 baton->size = section->size - DW_UNSND (attr);
23238 baton->data = section->buffer + DW_UNSND (attr);
23239 if (cu->base_address.has_value ())
23240 baton->base_address = *cu->base_address;
23241 else
23242 baton->base_address = 0;
23243 baton->from_dwo = cu->dwo_unit != NULL;
23244}
23245
23246static void
23247dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23248 struct dwarf2_cu *cu, int is_block)
23249{
23250 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23251 struct objfile *objfile = dwarf2_per_objfile->objfile;
23252 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23253
23254 if (attr->form_is_section_offset ()
23255 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23256 the section. If so, fall through to the complaint in the
23257 other branch. */
23258 && DW_UNSND (attr) < section->get_size (objfile))
23259 {
23260 struct dwarf2_loclist_baton *baton;
23261
23262 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23263
23264 fill_in_loclist_baton (cu, baton, attr);
23265
23266 if (!cu->base_address.has_value ())
23267 complaint (_("Location list used without "
23268 "specifying the CU base address."));
23269
23270 SYMBOL_ACLASS_INDEX (sym) = (is_block
23271 ? dwarf2_loclist_block_index
23272 : dwarf2_loclist_index);
23273 SYMBOL_LOCATION_BATON (sym) = baton;
23274 }
23275 else
23276 {
23277 struct dwarf2_locexpr_baton *baton;
23278
23279 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23280 baton->per_objfile = dwarf2_per_objfile;
23281 baton->per_cu = cu->per_cu;
23282 gdb_assert (baton->per_cu);
23283
23284 if (attr->form_is_block ())
23285 {
23286 /* Note that we're just copying the block's data pointer
23287 here, not the actual data. We're still pointing into the
23288 info_buffer for SYM's objfile; right now we never release
23289 that buffer, but when we do clean up properly this may
23290 need to change. */
23291 baton->size = DW_BLOCK (attr)->size;
23292 baton->data = DW_BLOCK (attr)->data;
23293 }
23294 else
23295 {
23296 dwarf2_invalid_attrib_class_complaint ("location description",
23297 sym->natural_name ());
23298 baton->size = 0;
23299 }
23300
23301 SYMBOL_ACLASS_INDEX (sym) = (is_block
23302 ? dwarf2_locexpr_block_index
23303 : dwarf2_locexpr_index);
23304 SYMBOL_LOCATION_BATON (sym) = baton;
23305 }
23306}
23307
23308/* See read.h. */
23309
23310struct objfile *
23311dwarf2_per_cu_data::objfile () const
23312{
23313 struct objfile *objfile = dwarf2_per_objfile->objfile;
23314
23315 /* Return the master objfile, so that we can report and look up the
23316 correct file containing this variable. */
23317 if (objfile->separate_debug_objfile_backlink)
23318 objfile = objfile->separate_debug_objfile_backlink;
23319
23320 return objfile;
23321}
23322
23323/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23324 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23325 CU_HEADERP first. */
23326
23327static const struct comp_unit_head *
23328per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23329 const struct dwarf2_per_cu_data *per_cu)
23330{
23331 const gdb_byte *info_ptr;
23332
23333 if (per_cu->cu)
23334 return &per_cu->cu->header;
23335
23336 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23337
23338 memset (cu_headerp, 0, sizeof (*cu_headerp));
23339 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23340 rcuh_kind::COMPILE);
23341
23342 return cu_headerp;
23343}
23344
23345/* See read.h. */
23346
23347int
23348dwarf2_per_cu_data::addr_size () const
23349{
23350 struct comp_unit_head cu_header_local;
23351 const struct comp_unit_head *cu_headerp;
23352
23353 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23354
23355 return cu_headerp->addr_size;
23356}
23357
23358/* See read.h. */
23359
23360int
23361dwarf2_per_cu_data::offset_size () const
23362{
23363 struct comp_unit_head cu_header_local;
23364 const struct comp_unit_head *cu_headerp;
23365
23366 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23367
23368 return cu_headerp->offset_size;
23369}
23370
23371/* See read.h. */
23372
23373int
23374dwarf2_per_cu_data::ref_addr_size () const
23375{
23376 struct comp_unit_head cu_header_local;
23377 const struct comp_unit_head *cu_headerp;
23378
23379 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23380
23381 if (cu_headerp->version == 2)
23382 return cu_headerp->addr_size;
23383 else
23384 return cu_headerp->offset_size;
23385}
23386
23387/* See read.h. */
23388
23389CORE_ADDR
23390dwarf2_per_cu_data::text_offset () const
23391{
23392 struct objfile *objfile = dwarf2_per_objfile->objfile;
23393
23394 return objfile->text_section_offset ();
23395}
23396
23397/* See read.h. */
23398
23399struct type *
23400dwarf2_per_cu_data::addr_type () const
23401{
23402 struct objfile *objfile = dwarf2_per_objfile->objfile;
23403 struct type *void_type = objfile_type (objfile)->builtin_void;
23404 struct type *addr_type = lookup_pointer_type (void_type);
23405 int addr_size = this->addr_size ();
23406
23407 if (TYPE_LENGTH (addr_type) == addr_size)
23408 return addr_type;
23409
23410 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23411 return addr_type;
23412}
23413
23414/* A helper function for dwarf2_find_containing_comp_unit that returns
23415 the index of the result, and that searches a vector. It will
23416 return a result even if the offset in question does not actually
23417 occur in any CU. This is separate so that it can be unit
23418 tested. */
23419
23420static int
23421dwarf2_find_containing_comp_unit
23422 (sect_offset sect_off,
23423 unsigned int offset_in_dwz,
23424 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23425{
23426 int low, high;
23427
23428 low = 0;
23429 high = all_comp_units.size () - 1;
23430 while (high > low)
23431 {
23432 struct dwarf2_per_cu_data *mid_cu;
23433 int mid = low + (high - low) / 2;
23434
23435 mid_cu = all_comp_units[mid];
23436 if (mid_cu->is_dwz > offset_in_dwz
23437 || (mid_cu->is_dwz == offset_in_dwz
23438 && mid_cu->sect_off + mid_cu->length > sect_off))
23439 high = mid;
23440 else
23441 low = mid + 1;
23442 }
23443 gdb_assert (low == high);
23444 return low;
23445}
23446
23447/* Locate the .debug_info compilation unit from CU's objfile which contains
23448 the DIE at OFFSET. Raises an error on failure. */
23449
23450static struct dwarf2_per_cu_data *
23451dwarf2_find_containing_comp_unit (sect_offset sect_off,
23452 unsigned int offset_in_dwz,
23453 struct dwarf2_per_objfile *dwarf2_per_objfile)
23454{
23455 int low
23456 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23457 dwarf2_per_objfile->per_bfd->all_comp_units);
23458 struct dwarf2_per_cu_data *this_cu
23459 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23460
23461 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23462 {
23463 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23464 error (_("Dwarf Error: could not find partial DIE containing "
23465 "offset %s [in module %s]"),
23466 sect_offset_str (sect_off),
23467 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23468
23469 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23470 <= sect_off);
23471 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23472 }
23473 else
23474 {
23475 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23476 && sect_off >= this_cu->sect_off + this_cu->length)
23477 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23478 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23479 return this_cu;
23480 }
23481}
23482
23483#if GDB_SELF_TEST
23484
23485namespace selftests {
23486namespace find_containing_comp_unit {
23487
23488static void
23489run_test ()
23490{
23491 struct dwarf2_per_cu_data one {};
23492 struct dwarf2_per_cu_data two {};
23493 struct dwarf2_per_cu_data three {};
23494 struct dwarf2_per_cu_data four {};
23495
23496 one.length = 5;
23497 two.sect_off = sect_offset (one.length);
23498 two.length = 7;
23499
23500 three.length = 5;
23501 three.is_dwz = 1;
23502 four.sect_off = sect_offset (three.length);
23503 four.length = 7;
23504 four.is_dwz = 1;
23505
23506 std::vector<dwarf2_per_cu_data *> units;
23507 units.push_back (&one);
23508 units.push_back (&two);
23509 units.push_back (&three);
23510 units.push_back (&four);
23511
23512 int result;
23513
23514 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23515 SELF_CHECK (units[result] == &one);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23517 SELF_CHECK (units[result] == &one);
23518 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23519 SELF_CHECK (units[result] == &two);
23520
23521 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23522 SELF_CHECK (units[result] == &three);
23523 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23524 SELF_CHECK (units[result] == &three);
23525 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23526 SELF_CHECK (units[result] == &four);
23527}
23528
23529}
23530}
23531
23532#endif /* GDB_SELF_TEST */
23533
23534/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23535
23536dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23537 dwarf2_per_objfile *per_objfile)
23538 : per_cu (per_cu),
23539 per_objfile (per_objfile),
23540 mark (false),
23541 has_loclist (false),
23542 checked_producer (false),
23543 producer_is_gxx_lt_4_6 (false),
23544 producer_is_gcc_lt_4_3 (false),
23545 producer_is_icc (false),
23546 producer_is_icc_lt_14 (false),
23547 producer_is_codewarrior (false),
23548 processing_has_namespace_info (false)
23549{
23550 per_cu->cu = this;
23551}
23552
23553/* Destroy a dwarf2_cu. */
23554
23555dwarf2_cu::~dwarf2_cu ()
23556{
23557 per_cu->cu = NULL;
23558}
23559
23560/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23561
23562static void
23563prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23564 enum language pretend_language)
23565{
23566 struct attribute *attr;
23567
23568 /* Set the language we're debugging. */
23569 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23570 if (attr != nullptr)
23571 set_cu_language (DW_UNSND (attr), cu);
23572 else
23573 {
23574 cu->language = pretend_language;
23575 cu->language_defn = language_def (cu->language);
23576 }
23577
23578 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23579}
23580
23581/* Increase the age counter on each cached compilation unit, and free
23582 any that are too old. */
23583
23584static void
23585age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23586{
23587 struct dwarf2_per_cu_data *per_cu, **last_chain;
23588
23589 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23590 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23591 while (per_cu != NULL)
23592 {
23593 per_cu->cu->last_used ++;
23594 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23595 dwarf2_mark (per_cu->cu);
23596 per_cu = per_cu->cu->read_in_chain;
23597 }
23598
23599 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23600 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23601 while (per_cu != NULL)
23602 {
23603 struct dwarf2_per_cu_data *next_cu;
23604
23605 next_cu = per_cu->cu->read_in_chain;
23606
23607 if (!per_cu->cu->mark)
23608 {
23609 delete per_cu->cu;
23610 *last_chain = next_cu;
23611 }
23612 else
23613 last_chain = &per_cu->cu->read_in_chain;
23614
23615 per_cu = next_cu;
23616 }
23617}
23618
23619/* Remove a single compilation unit from the cache. */
23620
23621static void
23622free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23623{
23624 struct dwarf2_per_cu_data *per_cu, **last_chain;
23625 struct dwarf2_per_objfile *dwarf2_per_objfile
23626 = target_per_cu->dwarf2_per_objfile;
23627
23628 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23629 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23630 while (per_cu != NULL)
23631 {
23632 struct dwarf2_per_cu_data *next_cu;
23633
23634 next_cu = per_cu->cu->read_in_chain;
23635
23636 if (per_cu == target_per_cu)
23637 {
23638 delete per_cu->cu;
23639 per_cu->cu = NULL;
23640 *last_chain = next_cu;
23641 break;
23642 }
23643 else
23644 last_chain = &per_cu->cu->read_in_chain;
23645
23646 per_cu = next_cu;
23647 }
23648}
23649
23650/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23651 We store these in a hash table separate from the DIEs, and preserve them
23652 when the DIEs are flushed out of cache.
23653
23654 The CU "per_cu" pointer is needed because offset alone is not enough to
23655 uniquely identify the type. A file may have multiple .debug_types sections,
23656 or the type may come from a DWO file. Furthermore, while it's more logical
23657 to use per_cu->section+offset, with Fission the section with the data is in
23658 the DWO file but we don't know that section at the point we need it.
23659 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23660 because we can enter the lookup routine, get_die_type_at_offset, from
23661 outside this file, and thus won't necessarily have PER_CU->cu.
23662 Fortunately, PER_CU is stable for the life of the objfile. */
23663
23664struct dwarf2_per_cu_offset_and_type
23665{
23666 const struct dwarf2_per_cu_data *per_cu;
23667 sect_offset sect_off;
23668 struct type *type;
23669};
23670
23671/* Hash function for a dwarf2_per_cu_offset_and_type. */
23672
23673static hashval_t
23674per_cu_offset_and_type_hash (const void *item)
23675{
23676 const struct dwarf2_per_cu_offset_and_type *ofs
23677 = (const struct dwarf2_per_cu_offset_and_type *) item;
23678
23679 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23680}
23681
23682/* Equality function for a dwarf2_per_cu_offset_and_type. */
23683
23684static int
23685per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23686{
23687 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23688 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23689 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23690 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23691
23692 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23693 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23694}
23695
23696/* Set the type associated with DIE to TYPE. Save it in CU's hash
23697 table if necessary. For convenience, return TYPE.
23698
23699 The DIEs reading must have careful ordering to:
23700 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23701 reading current DIE.
23702 * Not trying to dereference contents of still incompletely read in types
23703 while reading in other DIEs.
23704 * Enable referencing still incompletely read in types just by a pointer to
23705 the type without accessing its fields.
23706
23707 Therefore caller should follow these rules:
23708 * Try to fetch any prerequisite types we may need to build this DIE type
23709 before building the type and calling set_die_type.
23710 * After building type call set_die_type for current DIE as soon as
23711 possible before fetching more types to complete the current type.
23712 * Make the type as complete as possible before fetching more types. */
23713
23714static struct type *
23715set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23716{
23717 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23718 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23719 struct objfile *objfile = dwarf2_per_objfile->objfile;
23720 struct attribute *attr;
23721 struct dynamic_prop prop;
23722
23723 /* For Ada types, make sure that the gnat-specific data is always
23724 initialized (if not already set). There are a few types where
23725 we should not be doing so, because the type-specific area is
23726 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23727 where the type-specific area is used to store the floatformat).
23728 But this is not a problem, because the gnat-specific information
23729 is actually not needed for these types. */
23730 if (need_gnat_info (cu)
23731 && type->code () != TYPE_CODE_FUNC
23732 && type->code () != TYPE_CODE_FLT
23733 && type->code () != TYPE_CODE_METHODPTR
23734 && type->code () != TYPE_CODE_MEMBERPTR
23735 && type->code () != TYPE_CODE_METHOD
23736 && !HAVE_GNAT_AUX_INFO (type))
23737 INIT_GNAT_SPECIFIC (type);
23738
23739 /* Read DW_AT_allocated and set in type. */
23740 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23741 if (attr != NULL && attr->form_is_block ())
23742 {
23743 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23744 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23745 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23746 }
23747 else if (attr != NULL)
23748 {
23749 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23750 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23751 sect_offset_str (die->sect_off));
23752 }
23753
23754 /* Read DW_AT_associated and set in type. */
23755 attr = dwarf2_attr (die, DW_AT_associated, cu);
23756 if (attr != NULL && attr->form_is_block ())
23757 {
23758 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23759 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23760 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23761 }
23762 else if (attr != NULL)
23763 {
23764 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23765 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23766 sect_offset_str (die->sect_off));
23767 }
23768
23769 /* Read DW_AT_data_location and set in type. */
23770 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23771 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23772 cu->per_cu->addr_type ()))
23773 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23774
23775 if (dwarf2_per_objfile->die_type_hash == NULL)
23776 dwarf2_per_objfile->die_type_hash
23777 = htab_up (htab_create_alloc (127,
23778 per_cu_offset_and_type_hash,
23779 per_cu_offset_and_type_eq,
23780 NULL, xcalloc, xfree));
23781
23782 ofs.per_cu = cu->per_cu;
23783 ofs.sect_off = die->sect_off;
23784 ofs.type = type;
23785 slot = (struct dwarf2_per_cu_offset_and_type **)
23786 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23787 if (*slot)
23788 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23789 sect_offset_str (die->sect_off));
23790 *slot = XOBNEW (&objfile->objfile_obstack,
23791 struct dwarf2_per_cu_offset_and_type);
23792 **slot = ofs;
23793 return type;
23794}
23795
23796/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23797 or return NULL if the die does not have a saved type. */
23798
23799static struct type *
23800get_die_type_at_offset (sect_offset sect_off,
23801 struct dwarf2_per_cu_data *per_cu)
23802{
23803 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23804 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23805
23806 if (dwarf2_per_objfile->die_type_hash == NULL)
23807 return NULL;
23808
23809 ofs.per_cu = per_cu;
23810 ofs.sect_off = sect_off;
23811 slot = ((struct dwarf2_per_cu_offset_and_type *)
23812 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23813 if (slot)
23814 return slot->type;
23815 else
23816 return NULL;
23817}
23818
23819/* Look up the type for DIE in CU in die_type_hash,
23820 or return NULL if DIE does not have a saved type. */
23821
23822static struct type *
23823get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23824{
23825 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23826}
23827
23828/* Add a dependence relationship from CU to REF_PER_CU. */
23829
23830static void
23831dwarf2_add_dependence (struct dwarf2_cu *cu,
23832 struct dwarf2_per_cu_data *ref_per_cu)
23833{
23834 void **slot;
23835
23836 if (cu->dependencies == NULL)
23837 cu->dependencies
23838 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23839 NULL, &cu->comp_unit_obstack,
23840 hashtab_obstack_allocate,
23841 dummy_obstack_deallocate);
23842
23843 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23844 if (*slot == NULL)
23845 *slot = ref_per_cu;
23846}
23847
23848/* Subroutine of dwarf2_mark to pass to htab_traverse.
23849 Set the mark field in every compilation unit in the
23850 cache that we must keep because we are keeping CU. */
23851
23852static int
23853dwarf2_mark_helper (void **slot, void *data)
23854{
23855 struct dwarf2_per_cu_data *per_cu;
23856
23857 per_cu = (struct dwarf2_per_cu_data *) *slot;
23858
23859 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23860 reading of the chain. As such dependencies remain valid it is not much
23861 useful to track and undo them during QUIT cleanups. */
23862 if (per_cu->cu == NULL)
23863 return 1;
23864
23865 if (per_cu->cu->mark)
23866 return 1;
23867 per_cu->cu->mark = true;
23868
23869 if (per_cu->cu->dependencies != NULL)
23870 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23871
23872 return 1;
23873}
23874
23875/* Set the mark field in CU and in every other compilation unit in the
23876 cache that we must keep because we are keeping CU. */
23877
23878static void
23879dwarf2_mark (struct dwarf2_cu *cu)
23880{
23881 if (cu->mark)
23882 return;
23883 cu->mark = true;
23884 if (cu->dependencies != NULL)
23885 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23886}
23887
23888static void
23889dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23890{
23891 while (per_cu)
23892 {
23893 per_cu->cu->mark = false;
23894 per_cu = per_cu->cu->read_in_chain;
23895 }
23896}
23897
23898/* Trivial hash function for partial_die_info: the hash value of a DIE
23899 is its offset in .debug_info for this objfile. */
23900
23901static hashval_t
23902partial_die_hash (const void *item)
23903{
23904 const struct partial_die_info *part_die
23905 = (const struct partial_die_info *) item;
23906
23907 return to_underlying (part_die->sect_off);
23908}
23909
23910/* Trivial comparison function for partial_die_info structures: two DIEs
23911 are equal if they have the same offset. */
23912
23913static int
23914partial_die_eq (const void *item_lhs, const void *item_rhs)
23915{
23916 const struct partial_die_info *part_die_lhs
23917 = (const struct partial_die_info *) item_lhs;
23918 const struct partial_die_info *part_die_rhs
23919 = (const struct partial_die_info *) item_rhs;
23920
23921 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23922}
23923
23924struct cmd_list_element *set_dwarf_cmdlist;
23925struct cmd_list_element *show_dwarf_cmdlist;
23926
23927static void
23928show_check_physname (struct ui_file *file, int from_tty,
23929 struct cmd_list_element *c, const char *value)
23930{
23931 fprintf_filtered (file,
23932 _("Whether to check \"physname\" is %s.\n"),
23933 value);
23934}
23935
23936void _initialize_dwarf2_read ();
23937void
23938_initialize_dwarf2_read ()
23939{
23940 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23941Set DWARF specific variables.\n\
23942Configure DWARF variables such as the cache size."),
23943 &set_dwarf_cmdlist, "maintenance set dwarf ",
23944 0/*allow-unknown*/, &maintenance_set_cmdlist);
23945
23946 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23947Show DWARF specific variables.\n\
23948Show DWARF variables such as the cache size."),
23949 &show_dwarf_cmdlist, "maintenance show dwarf ",
23950 0/*allow-unknown*/, &maintenance_show_cmdlist);
23951
23952 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23953 &dwarf_max_cache_age, _("\
23954Set the upper bound on the age of cached DWARF compilation units."), _("\
23955Show the upper bound on the age of cached DWARF compilation units."), _("\
23956A higher limit means that cached compilation units will be stored\n\
23957in memory longer, and more total memory will be used. Zero disables\n\
23958caching, which can slow down startup."),
23959 NULL,
23960 show_dwarf_max_cache_age,
23961 &set_dwarf_cmdlist,
23962 &show_dwarf_cmdlist);
23963
23964 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23965Set debugging of the DWARF reader."), _("\
23966Show debugging of the DWARF reader."), _("\
23967When enabled (non-zero), debugging messages are printed during DWARF\n\
23968reading and symtab expansion. A value of 1 (one) provides basic\n\
23969information. A value greater than 1 provides more verbose information."),
23970 NULL,
23971 NULL,
23972 &setdebuglist, &showdebuglist);
23973
23974 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23975Set debugging of the DWARF DIE reader."), _("\
23976Show debugging of the DWARF DIE reader."), _("\
23977When enabled (non-zero), DIEs are dumped after they are read in.\n\
23978The value is the maximum depth to print."),
23979 NULL,
23980 NULL,
23981 &setdebuglist, &showdebuglist);
23982
23983 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23984Set debugging of the dwarf line reader."), _("\
23985Show debugging of the dwarf line reader."), _("\
23986When enabled (non-zero), line number entries are dumped as they are read in.\n\
23987A value of 1 (one) provides basic information.\n\
23988A value greater than 1 provides more verbose information."),
23989 NULL,
23990 NULL,
23991 &setdebuglist, &showdebuglist);
23992
23993 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23994Set cross-checking of \"physname\" code against demangler."), _("\
23995Show cross-checking of \"physname\" code against demangler."), _("\
23996When enabled, GDB's internal \"physname\" code is checked against\n\
23997the demangler."),
23998 NULL, show_check_physname,
23999 &setdebuglist, &showdebuglist);
24000
24001 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24002 no_class, &use_deprecated_index_sections, _("\
24003Set whether to use deprecated gdb_index sections."), _("\
24004Show whether to use deprecated gdb_index sections."), _("\
24005When enabled, deprecated .gdb_index sections are used anyway.\n\
24006Normally they are ignored either because of a missing feature or\n\
24007performance issue.\n\
24008Warning: This option must be enabled before gdb reads the file."),
24009 NULL,
24010 NULL,
24011 &setlist, &showlist);
24012
24013 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24014 &dwarf2_locexpr_funcs);
24015 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24016 &dwarf2_loclist_funcs);
24017
24018 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24019 &dwarf2_block_frame_base_locexpr_funcs);
24020 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24021 &dwarf2_block_frame_base_loclist_funcs);
24022
24023#if GDB_SELF_TEST
24024 selftests::register_test ("dw2_expand_symtabs_matching",
24025 selftests::dw2_expand_symtabs_matching::run_test);
24026 selftests::register_test ("dwarf2_find_containing_comp_unit",
24027 selftests::find_containing_comp_unit::run_test);
24028#endif
24029}
This page took 0.106664 seconds and 4 git commands to generate.