Parallelize DWARF indexing
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 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-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92 #include "dwarf2/abbrev-cache.h"
93 #include "cooked-index.h"
94 #include "split-name.h"
95 #include "gdbsupport/parallel-for.h"
96 #include "gdbsupport/thread-pool.h"
97
98 /* When == 1, print basic high level tracing messages.
99 When > 1, be more verbose.
100 This is in contrast to the low level DIE reading of dwarf_die_debug. */
101 static unsigned int dwarf_read_debug = 0;
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
104
105 #define dwarf_read_debug_printf(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
110
111 #define dwarf_read_debug_printf_v(fmt, ...) \
112 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
113 ##__VA_ARGS__)
114
115 /* When non-zero, dump DIEs after they are read in. */
116 static unsigned int dwarf_die_debug = 0;
117
118 /* When non-zero, dump line number entries as they are read in. */
119 unsigned int dwarf_line_debug = 0;
120
121 /* When true, cross-check physname against demangler. */
122 static bool check_physname = false;
123
124 /* When true, do not reject deprecated .gdb_index sections. */
125 static bool use_deprecated_index_sections = false;
126
127 /* This is used to store the data that is always per objfile. */
128 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
129
130 /* These are used to store the dwarf2_per_bfd objects.
131
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
137 version. */
138 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
139 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
140
141 /* The "aclass" indices for various kinds of computed DWARF symbols. */
142
143 static int dwarf2_locexpr_index;
144 static int dwarf2_loclist_index;
145 static int dwarf2_locexpr_block_index;
146 static int dwarf2_loclist_block_index;
147
148 /* Size of .debug_loclists section header for 32-bit DWARF format. */
149 #define LOCLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_loclists section header for 64-bit DWARF format. */
152 #define LOCLIST_HEADER_SIZE64 20
153
154 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
155 #define RNGLIST_HEADER_SIZE32 12
156
157 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
158 #define RNGLIST_HEADER_SIZE64 20
159
160 /* An index into a (C++) symbol name component in a symbol name as
161 recorded in the mapped_index's symbol table. For each C++ symbol
162 in the symbol table, we record one entry for the start of each
163 component in the symbol in a table of name components, and then
164 sort the table, in order to be able to binary search symbol names,
165 ignoring leading namespaces, both completion and regular look up.
166 For example, for symbol "A::B::C", we'll have an entry that points
167 to "A::B::C", another that points to "B::C", and another for "C".
168 Note that function symbols in GDB index have no parameter
169 information, just the function/method names. You can convert a
170 name_component to a "const char *" using the
171 'mapped_index::symbol_name_at(offset_type)' method. */
172
173 struct name_component
174 {
175 /* Offset in the symbol name where the component starts. Stored as
176 a (32-bit) offset instead of a pointer to save memory and improve
177 locality on 64-bit architectures. */
178 offset_type name_offset;
179
180 /* The symbol's index in the symbol and constant pool tables of a
181 mapped_index. */
182 offset_type idx;
183 };
184
185 /* Base class containing bits shared by both .gdb_index and
186 .debug_name indexes. */
187
188 struct mapped_index_base
189 {
190 mapped_index_base () = default;
191 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
192
193 /* The name_component table (a sorted vector). See name_component's
194 description above. */
195 std::vector<name_component> name_components;
196
197 /* How NAME_COMPONENTS is sorted. */
198 enum case_sensitivity name_components_casing;
199
200 /* Return the number of names in the symbol table. */
201 virtual size_t symbol_name_count () const = 0;
202
203 /* Get the name of the symbol at IDX in the symbol table. */
204 virtual const char *symbol_name_at
205 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
206
207 /* Return whether the name at IDX in the symbol table should be
208 ignored. */
209 virtual bool symbol_name_slot_invalid (offset_type idx) const
210 {
211 return false;
212 }
213
214 /* Build the symbol name component sorted vector, if we haven't
215 yet. */
216 void build_name_components (dwarf2_per_objfile *per_objfile);
217
218 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
219 possible matches for LN_NO_PARAMS in the name component
220 vector. */
221 std::pair<std::vector<name_component>::const_iterator,
222 std::vector<name_component>::const_iterator>
223 find_name_components_bounds (const lookup_name_info &ln_no_params,
224 enum language lang,
225 dwarf2_per_objfile *per_objfile) const;
226
227 /* Prevent deleting/destroying via a base class pointer. */
228 protected:
229 ~mapped_index_base() = default;
230 };
231
232 /* This is a view into the index that converts from bytes to an
233 offset_type, and allows indexing. Unaligned bytes are specifically
234 allowed here, and handled via unpacking. */
235
236 class offset_view
237 {
238 public:
239 offset_view () = default;
240
241 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
242 : m_bytes (bytes)
243 {
244 }
245
246 /* Extract the INDEXth offset_type from the array. */
247 offset_type operator[] (size_t index) const
248 {
249 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
250 return (offset_type) extract_unsigned_integer (bytes,
251 sizeof (offset_type),
252 BFD_ENDIAN_LITTLE);
253 }
254
255 /* Return the number of offset_types in this array. */
256 size_t size () const
257 {
258 return m_bytes.size () / sizeof (offset_type);
259 }
260
261 /* Return true if this view is empty. */
262 bool empty () const
263 {
264 return m_bytes.empty ();
265 }
266
267 private:
268 /* The underlying bytes. */
269 gdb::array_view<const gdb_byte> m_bytes;
270 };
271
272 /* A description of the mapped index. The file format is described in
273 a comment by the code that writes the index. */
274 struct mapped_index final : public mapped_index_base
275 {
276 /* Index data format version. */
277 int version = 0;
278
279 /* The address table data. */
280 gdb::array_view<const gdb_byte> address_table;
281
282 /* The symbol table, implemented as a hash table. */
283 offset_view symbol_table;
284
285 /* A pointer to the constant pool. */
286 gdb::array_view<const gdb_byte> constant_pool;
287
288 /* Return the index into the constant pool of the name of the IDXth
289 symbol in the symbol table. */
290 offset_type symbol_name_index (offset_type idx) const
291 {
292 return symbol_table[2 * idx];
293 }
294
295 /* Return the index into the constant pool of the CU vector of the
296 IDXth symbol in the symbol table. */
297 offset_type symbol_vec_index (offset_type idx) const
298 {
299 return symbol_table[2 * idx + 1];
300 }
301
302 bool symbol_name_slot_invalid (offset_type idx) const override
303 {
304 return (symbol_name_index (idx) == 0
305 && symbol_vec_index (idx) == 0);
306 }
307
308 /* Convenience method to get at the name of the symbol at IDX in the
309 symbol table. */
310 const char *symbol_name_at
311 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
312 {
313 return (const char *) (this->constant_pool.data ()
314 + symbol_name_index (idx));
315 }
316
317 size_t symbol_name_count () const override
318 { return this->symbol_table.size () / 2; }
319 };
320
321 /* A description of the mapped .debug_names.
322 Uninitialized map has CU_COUNT 0. */
323 struct mapped_debug_names final : public mapped_index_base
324 {
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name
357 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
358
359 /* Implementation of the mapped_index_base virtual interface, for
360 the name_components cache. */
361
362 const char *symbol_name_at
363 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
364 { return namei_to_name (idx, per_objfile); }
365
366 size_t symbol_name_count () const override
367 { return this->name_count; }
368 };
369
370 /* See dwarf2/read.h. */
371
372 dwarf2_per_objfile *
373 get_dwarf2_per_objfile (struct objfile *objfile)
374 {
375 return dwarf2_objfile_data_key.get (objfile);
376 }
377
378 /* Default names of the debugging sections. */
379
380 /* Note that if the debugging section has been compressed, it might
381 have a name like .zdebug_info. */
382
383 const struct dwarf2_debug_sections dwarf2_elf_names =
384 {
385 { ".debug_info", ".zdebug_info" },
386 { ".debug_abbrev", ".zdebug_abbrev" },
387 { ".debug_line", ".zdebug_line" },
388 { ".debug_loc", ".zdebug_loc" },
389 { ".debug_loclists", ".zdebug_loclists" },
390 { ".debug_macinfo", ".zdebug_macinfo" },
391 { ".debug_macro", ".zdebug_macro" },
392 { ".debug_str", ".zdebug_str" },
393 { ".debug_str_offsets", ".zdebug_str_offsets" },
394 { ".debug_line_str", ".zdebug_line_str" },
395 { ".debug_ranges", ".zdebug_ranges" },
396 { ".debug_rnglists", ".zdebug_rnglists" },
397 { ".debug_types", ".zdebug_types" },
398 { ".debug_addr", ".zdebug_addr" },
399 { ".debug_frame", ".zdebug_frame" },
400 { ".eh_frame", NULL },
401 { ".gdb_index", ".zgdb_index" },
402 { ".debug_names", ".zdebug_names" },
403 { ".debug_aranges", ".zdebug_aranges" },
404 23
405 };
406
407 /* List of DWO/DWP sections. */
408
409 static const struct dwop_section_names
410 {
411 struct dwarf2_section_names abbrev_dwo;
412 struct dwarf2_section_names info_dwo;
413 struct dwarf2_section_names line_dwo;
414 struct dwarf2_section_names loc_dwo;
415 struct dwarf2_section_names loclists_dwo;
416 struct dwarf2_section_names macinfo_dwo;
417 struct dwarf2_section_names macro_dwo;
418 struct dwarf2_section_names rnglists_dwo;
419 struct dwarf2_section_names str_dwo;
420 struct dwarf2_section_names str_offsets_dwo;
421 struct dwarf2_section_names types_dwo;
422 struct dwarf2_section_names cu_index;
423 struct dwarf2_section_names tu_index;
424 }
425 dwop_section_names =
426 {
427 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
428 { ".debug_info.dwo", ".zdebug_info.dwo" },
429 { ".debug_line.dwo", ".zdebug_line.dwo" },
430 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
431 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
432 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
433 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
434 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
435 { ".debug_str.dwo", ".zdebug_str.dwo" },
436 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
437 { ".debug_types.dwo", ".zdebug_types.dwo" },
438 { ".debug_cu_index", ".zdebug_cu_index" },
439 { ".debug_tu_index", ".zdebug_tu_index" },
440 };
441
442 /* local data types */
443
444 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
445 begin with a header, which contains the following information. */
446 struct loclists_rnglists_header
447 {
448 /* A 4-byte or 12-byte length containing the length of the
449 set of entries for this compilation unit, not including the
450 length field itself. */
451 unsigned int length;
452
453 /* A 2-byte version identifier. */
454 short version;
455
456 /* A 1-byte unsigned integer containing the size in bytes of an address on
457 the target system. */
458 unsigned char addr_size;
459
460 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
461 on the target system. */
462 unsigned char segment_collector_size;
463
464 /* A 4-byte count of the number of offsets that follow the header. */
465 unsigned int offset_entry_count;
466 };
467
468 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
469 This includes type_unit_group and quick_file_names. */
470
471 struct stmt_list_hash
472 {
473 /* The DWO unit this table is from or NULL if there is none. */
474 struct dwo_unit *dwo_unit;
475
476 /* Offset in .debug_line or .debug_line.dwo. */
477 sect_offset line_sect_off;
478 };
479
480 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
481 an object of this type. This contains elements of type unit groups
482 that can be shared across objfiles. The non-shareable parts are in
483 type_unit_group_unshareable. */
484
485 struct type_unit_group : public dwarf2_per_cu_data
486 {
487 /* The TUs that share this DW_AT_stmt_list entry.
488 This is added to while parsing type units to build partial symtabs,
489 and is deleted afterwards and not used again. */
490 std::vector<signatured_type *> *tus = nullptr;
491
492 /* The data used to construct the hash key. */
493 struct stmt_list_hash hash {};
494 };
495
496 /* These sections are what may appear in a (real or virtual) DWO file. */
497
498 struct dwo_sections
499 {
500 struct dwarf2_section_info abbrev;
501 struct dwarf2_section_info line;
502 struct dwarf2_section_info loc;
503 struct dwarf2_section_info loclists;
504 struct dwarf2_section_info macinfo;
505 struct dwarf2_section_info macro;
506 struct dwarf2_section_info rnglists;
507 struct dwarf2_section_info str;
508 struct dwarf2_section_info str_offsets;
509 /* In the case of a virtual DWO file, these two are unused. */
510 struct dwarf2_section_info info;
511 std::vector<dwarf2_section_info> types;
512 };
513
514 /* CUs/TUs in DWP/DWO files. */
515
516 struct dwo_unit
517 {
518 /* Backlink to the containing struct dwo_file. */
519 struct dwo_file *dwo_file;
520
521 /* The "id" that distinguishes this CU/TU.
522 .debug_info calls this "dwo_id", .debug_types calls this "signature".
523 Since signatures came first, we stick with it for consistency. */
524 ULONGEST signature;
525
526 /* The section this CU/TU lives in, in the DWO file. */
527 struct dwarf2_section_info *section;
528
529 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
530 sect_offset sect_off;
531 unsigned int length;
532
533 /* For types, offset in the type's DIE of the type defined by this TU. */
534 cu_offset type_offset_in_tu;
535 };
536
537 /* include/dwarf2.h defines the DWP section codes.
538 It defines a max value but it doesn't define a min value, which we
539 use for error checking, so provide one. */
540
541 enum dwp_v2_section_ids
542 {
543 DW_SECT_MIN = 1
544 };
545
546 /* Data for one DWO file.
547
548 This includes virtual DWO files (a virtual DWO file is a DWO file as it
549 appears in a DWP file). DWP files don't really have DWO files per se -
550 comdat folding of types "loses" the DWO file they came from, and from
551 a high level view DWP files appear to contain a mass of random types.
552 However, to maintain consistency with the non-DWP case we pretend DWP
553 files contain virtual DWO files, and we assign each TU with one virtual
554 DWO file (generally based on the line and abbrev section offsets -
555 a heuristic that seems to work in practice). */
556
557 struct dwo_file
558 {
559 dwo_file () = default;
560 DISABLE_COPY_AND_ASSIGN (dwo_file);
561
562 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
563 For virtual DWO files the name is constructed from the section offsets
564 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
565 from related CU+TUs. */
566 const char *dwo_name = nullptr;
567
568 /* The DW_AT_comp_dir attribute. */
569 const char *comp_dir = nullptr;
570
571 /* The bfd, when the file is open. Otherwise this is NULL.
572 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
573 gdb_bfd_ref_ptr dbfd;
574
575 /* The sections that make up this DWO file.
576 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
577 sections (for lack of a better name). */
578 struct dwo_sections sections {};
579
580 /* The CUs in the file.
581 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
582 an extension to handle LLVM's Link Time Optimization output (where
583 multiple source files may be compiled into a single object/dwo pair). */
584 htab_up cus;
585
586 /* Table of TUs in the file.
587 Each element is a struct dwo_unit. */
588 htab_up tus;
589 };
590
591 /* These sections are what may appear in a DWP file. */
592
593 struct dwp_sections
594 {
595 /* These are used by all DWP versions (1, 2 and 5). */
596 struct dwarf2_section_info str;
597 struct dwarf2_section_info cu_index;
598 struct dwarf2_section_info tu_index;
599
600 /* These are only used by DWP version 2 and version 5 files.
601 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
602 sections are referenced by section number, and are not recorded here.
603 In DWP version 2 or 5 there is at most one copy of all these sections,
604 each section being (effectively) comprised of the concatenation of all of
605 the individual sections that exist in the version 1 format.
606 To keep the code simple we treat each of these concatenated pieces as a
607 section itself (a virtual section?). */
608 struct dwarf2_section_info abbrev;
609 struct dwarf2_section_info info;
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
612 struct dwarf2_section_info loclists;
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
615 struct dwarf2_section_info rnglists;
616 struct dwarf2_section_info str_offsets;
617 struct dwarf2_section_info types;
618 };
619
620 /* These sections are what may appear in a virtual DWO file in DWP version 1.
621 A virtual DWO file is a DWO file as it appears in a DWP file. */
622
623 struct virtual_v1_dwo_sections
624 {
625 struct dwarf2_section_info abbrev;
626 struct dwarf2_section_info line;
627 struct dwarf2_section_info loc;
628 struct dwarf2_section_info macinfo;
629 struct dwarf2_section_info macro;
630 struct dwarf2_section_info str_offsets;
631 /* Each DWP hash table entry records one CU or one TU.
632 That is recorded here, and copied to dwo_unit.section. */
633 struct dwarf2_section_info info_or_types;
634 };
635
636 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
637 In version 2, the sections of the DWO files are concatenated together
638 and stored in one section of that name. Thus each ELF section contains
639 several "virtual" sections. */
640
641 struct virtual_v2_or_v5_dwo_sections
642 {
643 bfd_size_type abbrev_offset;
644 bfd_size_type abbrev_size;
645
646 bfd_size_type line_offset;
647 bfd_size_type line_size;
648
649 bfd_size_type loc_offset;
650 bfd_size_type loc_size;
651
652 bfd_size_type loclists_offset;
653 bfd_size_type loclists_size;
654
655 bfd_size_type macinfo_offset;
656 bfd_size_type macinfo_size;
657
658 bfd_size_type macro_offset;
659 bfd_size_type macro_size;
660
661 bfd_size_type rnglists_offset;
662 bfd_size_type rnglists_size;
663
664 bfd_size_type str_offsets_offset;
665 bfd_size_type str_offsets_size;
666
667 /* Each DWP hash table entry records one CU or one TU.
668 That is recorded here, and copied to dwo_unit.section. */
669 bfd_size_type info_or_types_offset;
670 bfd_size_type info_or_types_size;
671 };
672
673 /* Contents of DWP hash tables. */
674
675 struct dwp_hash_table
676 {
677 uint32_t version, nr_columns;
678 uint32_t nr_units, nr_slots;
679 const gdb_byte *hash_table, *unit_table;
680 union
681 {
682 struct
683 {
684 const gdb_byte *indices;
685 } v1;
686 struct
687 {
688 /* This is indexed by column number and gives the id of the section
689 in that column. */
690 #define MAX_NR_V2_DWO_SECTIONS \
691 (1 /* .debug_info or .debug_types */ \
692 + 1 /* .debug_abbrev */ \
693 + 1 /* .debug_line */ \
694 + 1 /* .debug_loc */ \
695 + 1 /* .debug_str_offsets */ \
696 + 1 /* .debug_macro or .debug_macinfo */)
697 int section_ids[MAX_NR_V2_DWO_SECTIONS];
698 const gdb_byte *offsets;
699 const gdb_byte *sizes;
700 } v2;
701 struct
702 {
703 /* This is indexed by column number and gives the id of the section
704 in that column. */
705 #define MAX_NR_V5_DWO_SECTIONS \
706 (1 /* .debug_info */ \
707 + 1 /* .debug_abbrev */ \
708 + 1 /* .debug_line */ \
709 + 1 /* .debug_loclists */ \
710 + 1 /* .debug_str_offsets */ \
711 + 1 /* .debug_macro */ \
712 + 1 /* .debug_rnglists */)
713 int section_ids[MAX_NR_V5_DWO_SECTIONS];
714 const gdb_byte *offsets;
715 const gdb_byte *sizes;
716 } v5;
717 } section_pool;
718 };
719
720 /* Data for one DWP file. */
721
722 struct dwp_file
723 {
724 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
725 : name (name_),
726 dbfd (std::move (abfd))
727 {
728 }
729
730 /* Name of the file. */
731 const char *name;
732
733 /* File format version. */
734 int version = 0;
735
736 /* The bfd. */
737 gdb_bfd_ref_ptr dbfd;
738
739 /* Section info for this file. */
740 struct dwp_sections sections {};
741
742 /* Table of CUs in the file. */
743 const struct dwp_hash_table *cus = nullptr;
744
745 /* Table of TUs in the file. */
746 const struct dwp_hash_table *tus = nullptr;
747
748 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
749 htab_up loaded_cus;
750 htab_up loaded_tus;
751
752 /* Table to map ELF section numbers to their sections.
753 This is only needed for the DWP V1 file format. */
754 unsigned int num_sections = 0;
755 asection **elf_sections = nullptr;
756 };
757
758 /* Struct used to pass misc. parameters to read_die_and_children, et
759 al. which are used for both .debug_info and .debug_types dies.
760 All parameters here are unchanging for the life of the call. This
761 struct exists to abstract away the constant parameters of die reading. */
762
763 struct die_reader_specs
764 {
765 /* The bfd of die_section. */
766 bfd *abfd;
767
768 /* The CU of the DIE we are parsing. */
769 struct dwarf2_cu *cu;
770
771 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
772 struct dwo_file *dwo_file;
773
774 /* The section the die comes from.
775 This is either .debug_info or .debug_types, or the .dwo variants. */
776 struct dwarf2_section_info *die_section;
777
778 /* die_section->buffer. */
779 const gdb_byte *buffer;
780
781 /* The end of the buffer. */
782 const gdb_byte *buffer_end;
783
784 /* The abbreviation table to use when reading the DIEs. */
785 struct abbrev_table *abbrev_table;
786 };
787
788 /* A subclass of die_reader_specs that holds storage and has complex
789 constructor and destructor behavior. */
790
791 class cutu_reader : public die_reader_specs
792 {
793 public:
794
795 cutu_reader (dwarf2_per_cu_data *this_cu,
796 dwarf2_per_objfile *per_objfile,
797 struct abbrev_table *abbrev_table,
798 dwarf2_cu *existing_cu,
799 bool skip_partial,
800 abbrev_cache *cache = nullptr);
801
802 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
803 dwarf2_per_objfile *per_objfile,
804 struct dwarf2_cu *parent_cu = nullptr,
805 struct dwo_file *dwo_file = nullptr);
806
807 DISABLE_COPY_AND_ASSIGN (cutu_reader);
808
809 cutu_reader (cutu_reader &&) = default;
810
811 const gdb_byte *info_ptr = nullptr;
812 struct die_info *comp_unit_die = nullptr;
813 bool dummy_p = false;
814
815 /* Release the new CU, putting it on the chain. This cannot be done
816 for dummy CUs. */
817 void keep ();
818
819 /* Release the abbrev table, transferring ownership to the
820 caller. */
821 abbrev_table_up release_abbrev_table ()
822 {
823 return std::move (m_abbrev_table_holder);
824 }
825
826 private:
827 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
828 dwarf2_per_objfile *per_objfile,
829 dwarf2_cu *existing_cu);
830
831 struct dwarf2_per_cu_data *m_this_cu;
832 std::unique_ptr<dwarf2_cu> m_new_cu;
833
834 /* The ordinary abbreviation table. */
835 abbrev_table_up m_abbrev_table_holder;
836
837 /* The DWO abbreviation table. */
838 abbrev_table_up m_dwo_abbrev_table;
839 };
840
841 /* When we construct a partial symbol table entry we only
842 need this much information. */
843 struct partial_die_info : public allocate_on_obstack
844 {
845 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
846
847 /* Disable assign but still keep copy ctor, which is needed
848 load_partial_dies. */
849 partial_die_info& operator=(const partial_die_info& rhs) = delete;
850 partial_die_info (const partial_die_info &) = default;
851
852 /* Adjust the partial die before generating a symbol for it. This
853 function may set the is_external flag or change the DIE's
854 name. */
855 void fixup (struct dwarf2_cu *cu);
856
857 /* Read a minimal amount of information into the minimal die
858 structure. */
859 const gdb_byte *read (const struct die_reader_specs *reader,
860 const struct abbrev_info &abbrev,
861 const gdb_byte *info_ptr);
862
863 /* Compute the name of this partial DIE. This memoizes the
864 result, so it is safe to call multiple times. */
865 const char *name (dwarf2_cu *cu);
866
867 /* Offset of this DIE. */
868 const sect_offset sect_off;
869
870 /* DWARF-2 tag for this DIE. */
871 const ENUM_BITFIELD(dwarf_tag) tag : 16;
872
873 /* Assorted flags describing the data found in this DIE. */
874 const unsigned int has_children : 1;
875
876 unsigned int is_external : 1;
877 unsigned int is_declaration : 1;
878 unsigned int has_type : 1;
879 unsigned int has_specification : 1;
880 unsigned int has_pc_info : 1;
881 unsigned int has_range_info : 1;
882 unsigned int may_be_inlined : 1;
883
884 /* This DIE has been marked DW_AT_main_subprogram. */
885 unsigned int main_subprogram : 1;
886
887 /* Flag set if the SCOPE field of this structure has been
888 computed. */
889 unsigned int scope_set : 1;
890
891 /* Flag set if the DIE has a byte_size attribute. */
892 unsigned int has_byte_size : 1;
893
894 /* Flag set if the DIE has a DW_AT_const_value attribute. */
895 unsigned int has_const_value : 1;
896
897 /* Flag set if any of the DIE's children are template arguments. */
898 unsigned int has_template_arguments : 1;
899
900 /* Flag set if fixup has been called on this die. */
901 unsigned int fixup_called : 1;
902
903 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
904 unsigned int is_dwz : 1;
905
906 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
907 unsigned int spec_is_dwz : 1;
908
909 unsigned int canonical_name : 1;
910
911 /* The name of this DIE. Normally the value of DW_AT_name, but
912 sometimes a default name for unnamed DIEs. */
913 const char *raw_name = nullptr;
914
915 /* The linkage name, if present. */
916 const char *linkage_name = nullptr;
917
918 /* The scope to prepend to our children. This is generally
919 allocated on the comp_unit_obstack, so will disappear
920 when this compilation unit leaves the cache. */
921 const char *scope = nullptr;
922
923 /* Some data associated with the partial DIE. The tag determines
924 which field is live. */
925 union
926 {
927 /* The location description associated with this DIE, if any. */
928 struct dwarf_block *locdesc;
929 /* The offset of an import, for DW_TAG_imported_unit. */
930 sect_offset sect_off;
931 } d {};
932
933 union
934 {
935 /* If HAS_PC_INFO, the PC range associated with this DIE. */
936 struct
937 {
938 CORE_ADDR lowpc;
939 CORE_ADDR highpc;
940 };
941 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
942 ULONGEST ranges_offset;
943 };
944
945 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
946 DW_AT_sibling, if any. */
947 /* NOTE: This member isn't strictly necessary, partial_die_info::read
948 could return DW_AT_sibling values to its caller load_partial_dies. */
949 const gdb_byte *sibling = nullptr;
950
951 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
952 DW_AT_specification (or DW_AT_abstract_origin or
953 DW_AT_extension). */
954 sect_offset spec_offset {};
955
956 /* Pointers to this DIE's parent, first child, and next sibling,
957 if any. */
958 struct partial_die_info *die_parent = nullptr;
959 struct partial_die_info *die_child = nullptr;
960 struct partial_die_info *die_sibling = nullptr;
961
962 friend struct partial_die_info *
963 dwarf2_cu::find_partial_die (sect_offset sect_off);
964
965 private:
966 /* Only need to do look up in dwarf2_cu::find_partial_die. */
967 partial_die_info (sect_offset sect_off)
968 : partial_die_info (sect_off, DW_TAG_padding, 0)
969 {
970 }
971
972 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
973 int has_children_)
974 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
975 {
976 is_external = 0;
977 is_declaration = 0;
978 has_type = 0;
979 has_specification = 0;
980 has_pc_info = 0;
981 has_range_info = 0;
982 may_be_inlined = 0;
983 main_subprogram = 0;
984 scope_set = 0;
985 has_byte_size = 0;
986 has_const_value = 0;
987 has_template_arguments = 0;
988 fixup_called = 0;
989 is_dwz = 0;
990 spec_is_dwz = 0;
991 canonical_name = 0;
992 /* Don't set these using NSDMI (Non-static data member initialisation),
993 because g++-4.8 will error out. */
994 lowpc = 0;
995 highpc = 0;
996 }
997 };
998
999 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1000 but this would require a corresponding change in unpack_field_as_long
1001 and friends. */
1002 static int bits_per_byte = 8;
1003
1004 struct variant_part_builder;
1005
1006 /* When reading a variant, we track a bit more information about the
1007 field, and store it in an object of this type. */
1008
1009 struct variant_field
1010 {
1011 int first_field = -1;
1012 int last_field = -1;
1013
1014 /* A variant can contain other variant parts. */
1015 std::vector<variant_part_builder> variant_parts;
1016
1017 /* If we see a DW_TAG_variant, then this will be set if this is the
1018 default branch. */
1019 bool default_branch = false;
1020 /* If we see a DW_AT_discr_value, then this will be the discriminant
1021 value. */
1022 ULONGEST discriminant_value = 0;
1023 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1024 data. */
1025 struct dwarf_block *discr_list_data = nullptr;
1026 };
1027
1028 /* This represents a DW_TAG_variant_part. */
1029
1030 struct variant_part_builder
1031 {
1032 /* The offset of the discriminant field. */
1033 sect_offset discriminant_offset {};
1034
1035 /* Variants that are direct children of this variant part. */
1036 std::vector<variant_field> variants;
1037
1038 /* True if we're currently reading a variant. */
1039 bool processing_variant = false;
1040 };
1041
1042 struct nextfield
1043 {
1044 int accessibility = 0;
1045 int virtuality = 0;
1046 /* Variant parts need to find the discriminant, which is a DIE
1047 reference. We track the section offset of each field to make
1048 this link. */
1049 sect_offset offset;
1050 struct field field {};
1051 };
1052
1053 struct fnfieldlist
1054 {
1055 const char *name = nullptr;
1056 std::vector<struct fn_field> fnfields;
1057 };
1058
1059 /* The routines that read and process dies for a C struct or C++ class
1060 pass lists of data member fields and lists of member function fields
1061 in an instance of a field_info structure, as defined below. */
1062 struct field_info
1063 {
1064 /* List of data member and baseclasses fields. */
1065 std::vector<struct nextfield> fields;
1066 std::vector<struct nextfield> baseclasses;
1067
1068 /* Set if the accessibility of one of the fields is not public. */
1069 bool non_public_fields = false;
1070
1071 /* Member function fieldlist array, contains name of possibly overloaded
1072 member function, number of overloaded member functions and a pointer
1073 to the head of the member function field chain. */
1074 std::vector<struct fnfieldlist> fnfieldlists;
1075
1076 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1077 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1078 std::vector<struct decl_field> typedef_field_list;
1079
1080 /* Nested types defined by this class and the number of elements in this
1081 list. */
1082 std::vector<struct decl_field> nested_types_list;
1083
1084 /* If non-null, this is the variant part we are currently
1085 reading. */
1086 variant_part_builder *current_variant_part = nullptr;
1087 /* This holds all the top-level variant parts attached to the type
1088 we're reading. */
1089 std::vector<variant_part_builder> variant_parts;
1090
1091 /* Return the total number of fields (including baseclasses). */
1092 int nfields () const
1093 {
1094 return fields.size () + baseclasses.size ();
1095 }
1096 };
1097
1098 /* Loaded secondary compilation units are kept in memory until they
1099 have not been referenced for the processing of this many
1100 compilation units. Set this to zero to disable caching. Cache
1101 sizes of up to at least twenty will improve startup time for
1102 typical inter-CU-reference binaries, at an obvious memory cost. */
1103 static int dwarf_max_cache_age = 5;
1104 static void
1105 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1106 struct cmd_list_element *c, const char *value)
1107 {
1108 gdb_printf (file, _("The upper bound on the age of cached "
1109 "DWARF compilation units is %s.\n"),
1110 value);
1111 }
1112 \f
1113 /* local function prototypes */
1114
1115 static void dwarf2_find_base_address (struct die_info *die,
1116 struct dwarf2_cu *cu);
1117
1118 static dwarf2_psymtab *create_partial_symtab
1119 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1120 const char *name);
1121
1122 class cooked_index_storage;
1123 static void build_type_psymtabs_reader (cutu_reader *reader,
1124 cooked_index_storage *storage);
1125
1126 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1127
1128 static void scan_partial_symbols (struct partial_die_info *,
1129 CORE_ADDR *, CORE_ADDR *,
1130 int, struct dwarf2_cu *);
1131
1132 static void add_partial_symbol (struct partial_die_info *,
1133 struct dwarf2_cu *);
1134
1135 static void add_partial_namespace (struct partial_die_info *pdi,
1136 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1137 int set_addrmap, struct dwarf2_cu *cu);
1138
1139 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1140 CORE_ADDR *highpc, int set_addrmap,
1141 struct dwarf2_cu *cu);
1142
1143 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1144 struct dwarf2_cu *cu);
1145
1146 static void add_partial_subprogram (struct partial_die_info *pdi,
1147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1148 int need_pc, struct dwarf2_cu *cu);
1149
1150 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1151
1152 static struct partial_die_info *load_partial_dies
1153 (const struct die_reader_specs *, const gdb_byte *, int);
1154
1155 /* A pair of partial_die_info and compilation unit. */
1156 struct cu_partial_die_info
1157 {
1158 /* The compilation unit of the partial_die_info. */
1159 struct dwarf2_cu *cu;
1160 /* A partial_die_info. */
1161 struct partial_die_info *pdi;
1162
1163 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1164 : cu (cu),
1165 pdi (pdi)
1166 { /* Nothing. */ }
1167
1168 private:
1169 cu_partial_die_info () = delete;
1170 };
1171
1172 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1173 struct dwarf2_cu *);
1174
1175 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1176 struct attribute *,
1177 const struct attr_abbrev *,
1178 const gdb_byte *);
1179
1180 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1181 struct attribute *attr, dwarf_tag tag);
1182
1183 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1184
1185 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1186 dwarf2_section_info *, sect_offset);
1187
1188 static const char *read_indirect_string
1189 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1190 const struct comp_unit_head *, unsigned int *);
1191
1192 static const char *read_indirect_string_at_offset
1193 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1194
1195 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1196 const gdb_byte *,
1197 unsigned int *);
1198
1199 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1200 ULONGEST str_index);
1201
1202 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1203 ULONGEST str_index);
1204
1205 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1206 struct dwarf2_cu *);
1207
1208 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1209 struct dwarf2_cu *cu);
1210
1211 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1212
1213 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1214 struct dwarf2_cu *cu);
1215
1216 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1217
1218 static struct die_info *die_specification (struct die_info *die,
1219 struct dwarf2_cu **);
1220
1221 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1222 struct dwarf2_cu *cu);
1223
1224 static void dwarf_decode_lines (struct line_header *,
1225 const file_and_directory &,
1226 struct dwarf2_cu *, dwarf2_psymtab *,
1227 CORE_ADDR, int decode_mapping);
1228
1229 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1230 const char *);
1231
1232 static struct symbol *new_symbol (struct die_info *, struct type *,
1233 struct dwarf2_cu *, struct symbol * = NULL);
1234
1235 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1236 struct dwarf2_cu *);
1237
1238 static void dwarf2_const_value_attr (const struct attribute *attr,
1239 struct type *type,
1240 const char *name,
1241 struct obstack *obstack,
1242 struct dwarf2_cu *cu, LONGEST *value,
1243 const gdb_byte **bytes,
1244 struct dwarf2_locexpr_baton **baton);
1245
1246 static struct type *read_subrange_index_type (struct die_info *die,
1247 struct dwarf2_cu *cu);
1248
1249 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1250
1251 static int need_gnat_info (struct dwarf2_cu *);
1252
1253 static struct type *die_descriptive_type (struct die_info *,
1254 struct dwarf2_cu *);
1255
1256 static void set_descriptive_type (struct type *, struct die_info *,
1257 struct dwarf2_cu *);
1258
1259 static struct type *die_containing_type (struct die_info *,
1260 struct dwarf2_cu *);
1261
1262 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1263 struct dwarf2_cu *);
1264
1265 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1266
1267 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1268
1269 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1270
1271 static char *typename_concat (struct obstack *obs, const char *prefix,
1272 const char *suffix, int physname,
1273 struct dwarf2_cu *cu);
1274
1275 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1276
1277 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1278
1279 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1280
1281 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1282
1283 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1284
1285 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 /* Return the .debug_loclists section to use for cu. */
1288 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1289
1290 /* Return the .debug_rnglists section to use for cu. */
1291 static struct dwarf2_section_info *cu_debug_rnglists_section
1292 (struct dwarf2_cu *cu, dwarf_tag tag);
1293
1294 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1295 values. Keep the items ordered with increasing constraints compliance. */
1296 enum pc_bounds_kind
1297 {
1298 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1299 PC_BOUNDS_NOT_PRESENT,
1300
1301 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1302 were present but they do not form a valid range of PC addresses. */
1303 PC_BOUNDS_INVALID,
1304
1305 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1306 PC_BOUNDS_RANGES,
1307
1308 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1309 PC_BOUNDS_HIGH_LOW,
1310 };
1311
1312 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1313 CORE_ADDR *, CORE_ADDR *,
1314 struct dwarf2_cu *,
1315 addrmap *,
1316 void *);
1317
1318 static void get_scope_pc_bounds (struct die_info *,
1319 CORE_ADDR *, CORE_ADDR *,
1320 struct dwarf2_cu *);
1321
1322 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1323 CORE_ADDR, struct dwarf2_cu *);
1324
1325 static void dwarf2_add_field (struct field_info *, struct die_info *,
1326 struct dwarf2_cu *);
1327
1328 static void dwarf2_attach_fields_to_type (struct field_info *,
1329 struct type *, struct dwarf2_cu *);
1330
1331 static void dwarf2_add_member_fn (struct field_info *,
1332 struct die_info *, struct type *,
1333 struct dwarf2_cu *);
1334
1335 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1336 struct type *,
1337 struct dwarf2_cu *);
1338
1339 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1340
1341 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1342
1343 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1344
1345 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1346
1347 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1348
1349 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1350
1351 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1352
1353 static struct type *read_module_type (struct die_info *die,
1354 struct dwarf2_cu *cu);
1355
1356 static const char *namespace_name (struct die_info *die,
1357 int *is_anonymous, struct dwarf2_cu *);
1358
1359 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1360
1361 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1362 bool * = nullptr);
1363
1364 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1365 struct dwarf2_cu *);
1366
1367 static struct die_info *read_die_and_siblings_1
1368 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1369 struct die_info *);
1370
1371 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1372 const gdb_byte *info_ptr,
1373 const gdb_byte **new_info_ptr,
1374 struct die_info *parent);
1375
1376 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1377 struct die_info **, const gdb_byte *,
1378 int);
1379
1380 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1381 struct die_info **, const gdb_byte *);
1382
1383 static void process_die (struct die_info *, struct dwarf2_cu *);
1384
1385 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1386 struct objfile *);
1387
1388 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1389
1390 static const char *dwarf2_full_name (const char *name,
1391 struct die_info *die,
1392 struct dwarf2_cu *cu);
1393
1394 static const char *dwarf2_physname (const char *name, struct die_info *die,
1395 struct dwarf2_cu *cu);
1396
1397 static struct die_info *dwarf2_extension (struct die_info *die,
1398 struct dwarf2_cu **);
1399
1400 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1401
1402 static void dump_die_for_error (struct die_info *);
1403
1404 static void dump_die_1 (struct ui_file *, int level, int max_level,
1405 struct die_info *);
1406
1407 /*static*/ void dump_die (struct die_info *, int max_level);
1408
1409 static void store_in_ref_table (struct die_info *,
1410 struct dwarf2_cu *);
1411
1412 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1413 const struct attribute *,
1414 struct dwarf2_cu **);
1415
1416 static struct die_info *follow_die_ref (struct die_info *,
1417 const struct attribute *,
1418 struct dwarf2_cu **);
1419
1420 static struct die_info *follow_die_sig (struct die_info *,
1421 const struct attribute *,
1422 struct dwarf2_cu **);
1423
1424 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1425 struct dwarf2_cu *);
1426
1427 static struct type *get_DW_AT_signature_type (struct die_info *,
1428 const struct attribute *,
1429 struct dwarf2_cu *);
1430
1431 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1432 dwarf2_per_objfile *per_objfile);
1433
1434 static void read_signatured_type (signatured_type *sig_type,
1435 dwarf2_per_objfile *per_objfile);
1436
1437 static int attr_to_dynamic_prop (const struct attribute *attr,
1438 struct die_info *die, struct dwarf2_cu *cu,
1439 struct dynamic_prop *prop, struct type *type);
1440
1441 /* memory allocation interface */
1442
1443 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1444
1445 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1446
1447 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1448
1449 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1450 struct dwarf2_loclist_baton *baton,
1451 const struct attribute *attr);
1452
1453 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1454 struct symbol *sym,
1455 struct dwarf2_cu *cu,
1456 int is_block);
1457
1458 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1459 const gdb_byte *info_ptr,
1460 const struct abbrev_info *abbrev,
1461 bool do_skip_children = true);
1462
1463 static hashval_t partial_die_hash (const void *item);
1464
1465 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1466
1467 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1468 (sect_offset sect_off, unsigned int offset_in_dwz,
1469 dwarf2_per_bfd *per_bfd);
1470
1471 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1472 struct die_info *comp_unit_die,
1473 enum language pretend_language);
1474
1475 static struct type *set_die_type (struct die_info *, struct type *,
1476 struct dwarf2_cu *, bool = false);
1477
1478 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1479
1480 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1481 dwarf2_per_objfile *per_objfile,
1482 dwarf2_cu *existing_cu,
1483 bool skip_partial,
1484 enum language pretend_language);
1485
1486 static void process_full_comp_unit (dwarf2_cu *cu,
1487 enum language pretend_language);
1488
1489 static void process_full_type_unit (dwarf2_cu *cu,
1490 enum language pretend_language);
1491
1492 static struct type *get_die_type_at_offset (sect_offset,
1493 dwarf2_per_cu_data *per_cu,
1494 dwarf2_per_objfile *per_objfile);
1495
1496 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1497
1498 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1499 dwarf2_per_objfile *per_objfile,
1500 enum language pretend_language);
1501
1502 static void process_queue (dwarf2_per_objfile *per_objfile);
1503
1504 /* Class, the destructor of which frees all allocated queue entries. This
1505 will only have work to do if an error was thrown while processing the
1506 dwarf. If no error was thrown then the queue entries should have all
1507 been processed, and freed, as we went along. */
1508
1509 class dwarf2_queue_guard
1510 {
1511 public:
1512 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1513 : m_per_objfile (per_objfile)
1514 {
1515 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1516
1517 m_per_objfile->per_bfd->queue.emplace ();
1518 }
1519
1520 /* Free any entries remaining on the queue. There should only be
1521 entries left if we hit an error while processing the dwarf. */
1522 ~dwarf2_queue_guard ()
1523 {
1524 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1525
1526 m_per_objfile->per_bfd->queue.reset ();
1527 }
1528
1529 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1530
1531 private:
1532 dwarf2_per_objfile *m_per_objfile;
1533 };
1534
1535 dwarf2_queue_item::~dwarf2_queue_item ()
1536 {
1537 /* Anything still marked queued is likely to be in an
1538 inconsistent state, so discard it. */
1539 if (per_cu->queued)
1540 {
1541 per_objfile->remove_cu (per_cu);
1542 per_cu->queued = 0;
1543 }
1544 }
1545
1546 /* See dwarf2/read.h. */
1547
1548 void
1549 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1550 {
1551 if (data->is_debug_types)
1552 delete static_cast<signatured_type *> (data);
1553 else
1554 delete data;
1555 }
1556
1557 static file_and_directory &find_file_and_directory
1558 (struct die_info *die, struct dwarf2_cu *cu);
1559
1560 static const char *compute_include_file_name
1561 (const struct line_header *lh,
1562 const file_entry &fe,
1563 const file_and_directory &cu_info,
1564 gdb::unique_xmalloc_ptr<char> *name_holder);
1565
1566 static htab_up allocate_signatured_type_table ();
1567
1568 static htab_up allocate_dwo_unit_table ();
1569
1570 static struct dwo_unit *lookup_dwo_unit_in_dwp
1571 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1572 const char *comp_dir, ULONGEST signature, int is_debug_types);
1573
1574 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1575
1576 static struct dwo_unit *lookup_dwo_comp_unit
1577 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1578 ULONGEST signature);
1579
1580 static struct dwo_unit *lookup_dwo_type_unit
1581 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1582
1583 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1584
1585 /* A unique pointer to a dwo_file. */
1586
1587 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1588
1589 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1590
1591 static void check_producer (struct dwarf2_cu *cu);
1592 \f
1593 /* Various complaints about symbol reading that don't abort the process. */
1594
1595 static void
1596 dwarf2_debug_line_missing_file_complaint (void)
1597 {
1598 complaint (_(".debug_line section has line data without a file"));
1599 }
1600
1601 static void
1602 dwarf2_debug_line_missing_end_sequence_complaint (void)
1603 {
1604 complaint (_(".debug_line section has line "
1605 "program sequence without an end"));
1606 }
1607
1608 static void
1609 dwarf2_complex_location_expr_complaint (void)
1610 {
1611 complaint (_("location expression too complex"));
1612 }
1613
1614 static void
1615 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1616 int arg3)
1617 {
1618 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1619 arg1, arg2, arg3);
1620 }
1621
1622 static void
1623 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1624 {
1625 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1626 arg1, arg2);
1627 }
1628
1629 /* Hash function for line_header_hash. */
1630
1631 static hashval_t
1632 line_header_hash (const struct line_header *ofs)
1633 {
1634 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1635 }
1636
1637 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1638
1639 static hashval_t
1640 line_header_hash_voidp (const void *item)
1641 {
1642 const struct line_header *ofs = (const struct line_header *) item;
1643
1644 return line_header_hash (ofs);
1645 }
1646
1647 /* Equality function for line_header_hash. */
1648
1649 static int
1650 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1651 {
1652 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1653 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1654
1655 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1656 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1657 }
1658
1659 \f
1660
1661 /* An iterator for all_comp_units that is based on index. This
1662 approach makes it possible to iterate over all_comp_units safely,
1663 when some caller in the loop may add new units. */
1664
1665 class all_comp_units_iterator
1666 {
1667 public:
1668
1669 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1670 : m_per_bfd (per_bfd),
1671 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1672 {
1673 }
1674
1675 all_comp_units_iterator &operator++ ()
1676 {
1677 ++m_index;
1678 return *this;
1679 }
1680
1681 dwarf2_per_cu_data *operator* () const
1682 {
1683 return m_per_bfd->get_cu (m_index);
1684 }
1685
1686 bool operator== (const all_comp_units_iterator &other) const
1687 {
1688 return m_index == other.m_index;
1689 }
1690
1691
1692 bool operator!= (const all_comp_units_iterator &other) const
1693 {
1694 return m_index != other.m_index;
1695 }
1696
1697 private:
1698
1699 dwarf2_per_bfd *m_per_bfd;
1700 size_t m_index;
1701 };
1702
1703 /* A range adapter for the all_comp_units_iterator. */
1704 class all_comp_units_range
1705 {
1706 public:
1707
1708 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1709 : m_per_bfd (per_bfd)
1710 {
1711 }
1712
1713 all_comp_units_iterator begin ()
1714 {
1715 return all_comp_units_iterator (m_per_bfd, true);
1716 }
1717
1718 all_comp_units_iterator end ()
1719 {
1720 return all_comp_units_iterator (m_per_bfd, false);
1721 }
1722
1723 private:
1724
1725 dwarf2_per_bfd *m_per_bfd;
1726 };
1727
1728 /* See declaration. */
1729
1730 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1731 bool can_copy_)
1732 : obfd (obfd),
1733 can_copy (can_copy_)
1734 {
1735 if (names == NULL)
1736 names = &dwarf2_elf_names;
1737
1738 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1739 locate_sections (obfd, sec, *names);
1740 }
1741
1742 dwarf2_per_bfd::~dwarf2_per_bfd ()
1743 {
1744 for (auto &per_cu : all_comp_units)
1745 {
1746 per_cu->imported_symtabs_free ();
1747 per_cu->free_cached_file_names ();
1748 }
1749
1750 /* Everything else should be on this->obstack. */
1751 }
1752
1753 /* See read.h. */
1754
1755 void
1756 dwarf2_per_objfile::remove_all_cus ()
1757 {
1758 gdb_assert (!this->per_bfd->queue.has_value ());
1759
1760 for (auto pair : m_dwarf2_cus)
1761 delete pair.second;
1762
1763 m_dwarf2_cus.clear ();
1764 }
1765
1766 /* A helper class that calls free_cached_comp_units on
1767 destruction. */
1768
1769 class free_cached_comp_units
1770 {
1771 public:
1772
1773 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1774 : m_per_objfile (per_objfile)
1775 {
1776 }
1777
1778 ~free_cached_comp_units ()
1779 {
1780 m_per_objfile->remove_all_cus ();
1781 }
1782
1783 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1784
1785 private:
1786
1787 dwarf2_per_objfile *m_per_objfile;
1788 };
1789
1790 /* See read.h. */
1791
1792 bool
1793 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1794 {
1795 if (per_cu->index < this->m_symtabs.size ())
1796 return this->m_symtabs[per_cu->index] != nullptr;
1797 return false;
1798 }
1799
1800 /* See read.h. */
1801
1802 compunit_symtab *
1803 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1804 {
1805 if (per_cu->index < this->m_symtabs.size ())
1806 return this->m_symtabs[per_cu->index];
1807 return nullptr;
1808 }
1809
1810 /* See read.h. */
1811
1812 void
1813 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1814 compunit_symtab *symtab)
1815 {
1816 if (per_cu->index >= this->m_symtabs.size ())
1817 this->m_symtabs.resize (per_cu->index + 1);
1818 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1819 this->m_symtabs[per_cu->index] = symtab;
1820 }
1821
1822 /* Try to locate the sections we need for DWARF 2 debugging
1823 information and return true if we have enough to do something.
1824 NAMES points to the dwarf2 section names, or is NULL if the standard
1825 ELF names are used. CAN_COPY is true for formats where symbol
1826 interposition is possible and so symbol values must follow copy
1827 relocation rules. */
1828
1829 int
1830 dwarf2_has_info (struct objfile *objfile,
1831 const struct dwarf2_debug_sections *names,
1832 bool can_copy)
1833 {
1834 if (objfile->flags & OBJF_READNEVER)
1835 return 0;
1836
1837 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1838
1839 if (per_objfile == NULL)
1840 {
1841 dwarf2_per_bfd *per_bfd;
1842
1843 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1844 BFD doesn't require relocations.
1845
1846 We don't share with objfiles for which -readnow was requested,
1847 because it would complicate things when loading the same BFD with
1848 -readnow and then without -readnow. */
1849 if (!gdb_bfd_requires_relocations (objfile->obfd)
1850 && (objfile->flags & OBJF_READNOW) == 0)
1851 {
1852 /* See if one has been created for this BFD yet. */
1853 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1854
1855 if (per_bfd == nullptr)
1856 {
1857 /* No, create it now. */
1858 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1859 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1860 }
1861 }
1862 else
1863 {
1864 /* No sharing possible, create one specifically for this objfile. */
1865 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1866 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1867 }
1868
1869 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1870 }
1871
1872 return (!per_objfile->per_bfd->info.is_virtual
1873 && per_objfile->per_bfd->info.s.section != NULL
1874 && !per_objfile->per_bfd->abbrev.is_virtual
1875 && per_objfile->per_bfd->abbrev.s.section != NULL);
1876 }
1877
1878 /* See declaration. */
1879
1880 void
1881 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1882 const dwarf2_debug_sections &names)
1883 {
1884 flagword aflag = bfd_section_flags (sectp);
1885
1886 if ((aflag & SEC_HAS_CONTENTS) == 0)
1887 {
1888 }
1889 else if (elf_section_data (sectp)->this_hdr.sh_size
1890 > bfd_get_file_size (abfd))
1891 {
1892 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1893 warning (_("Discarding section %s which has a section size (%s"
1894 ") larger than the file size [in module %s]"),
1895 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1896 bfd_get_filename (abfd));
1897 }
1898 else if (names.info.matches (sectp->name))
1899 {
1900 this->info.s.section = sectp;
1901 this->info.size = bfd_section_size (sectp);
1902 }
1903 else if (names.abbrev.matches (sectp->name))
1904 {
1905 this->abbrev.s.section = sectp;
1906 this->abbrev.size = bfd_section_size (sectp);
1907 }
1908 else if (names.line.matches (sectp->name))
1909 {
1910 this->line.s.section = sectp;
1911 this->line.size = bfd_section_size (sectp);
1912 }
1913 else if (names.loc.matches (sectp->name))
1914 {
1915 this->loc.s.section = sectp;
1916 this->loc.size = bfd_section_size (sectp);
1917 }
1918 else if (names.loclists.matches (sectp->name))
1919 {
1920 this->loclists.s.section = sectp;
1921 this->loclists.size = bfd_section_size (sectp);
1922 }
1923 else if (names.macinfo.matches (sectp->name))
1924 {
1925 this->macinfo.s.section = sectp;
1926 this->macinfo.size = bfd_section_size (sectp);
1927 }
1928 else if (names.macro.matches (sectp->name))
1929 {
1930 this->macro.s.section = sectp;
1931 this->macro.size = bfd_section_size (sectp);
1932 }
1933 else if (names.str.matches (sectp->name))
1934 {
1935 this->str.s.section = sectp;
1936 this->str.size = bfd_section_size (sectp);
1937 }
1938 else if (names.str_offsets.matches (sectp->name))
1939 {
1940 this->str_offsets.s.section = sectp;
1941 this->str_offsets.size = bfd_section_size (sectp);
1942 }
1943 else if (names.line_str.matches (sectp->name))
1944 {
1945 this->line_str.s.section = sectp;
1946 this->line_str.size = bfd_section_size (sectp);
1947 }
1948 else if (names.addr.matches (sectp->name))
1949 {
1950 this->addr.s.section = sectp;
1951 this->addr.size = bfd_section_size (sectp);
1952 }
1953 else if (names.frame.matches (sectp->name))
1954 {
1955 this->frame.s.section = sectp;
1956 this->frame.size = bfd_section_size (sectp);
1957 }
1958 else if (names.eh_frame.matches (sectp->name))
1959 {
1960 this->eh_frame.s.section = sectp;
1961 this->eh_frame.size = bfd_section_size (sectp);
1962 }
1963 else if (names.ranges.matches (sectp->name))
1964 {
1965 this->ranges.s.section = sectp;
1966 this->ranges.size = bfd_section_size (sectp);
1967 }
1968 else if (names.rnglists.matches (sectp->name))
1969 {
1970 this->rnglists.s.section = sectp;
1971 this->rnglists.size = bfd_section_size (sectp);
1972 }
1973 else if (names.types.matches (sectp->name))
1974 {
1975 struct dwarf2_section_info type_section;
1976
1977 memset (&type_section, 0, sizeof (type_section));
1978 type_section.s.section = sectp;
1979 type_section.size = bfd_section_size (sectp);
1980
1981 this->types.push_back (type_section);
1982 }
1983 else if (names.gdb_index.matches (sectp->name))
1984 {
1985 this->gdb_index.s.section = sectp;
1986 this->gdb_index.size = bfd_section_size (sectp);
1987 }
1988 else if (names.debug_names.matches (sectp->name))
1989 {
1990 this->debug_names.s.section = sectp;
1991 this->debug_names.size = bfd_section_size (sectp);
1992 }
1993 else if (names.debug_aranges.matches (sectp->name))
1994 {
1995 this->debug_aranges.s.section = sectp;
1996 this->debug_aranges.size = bfd_section_size (sectp);
1997 }
1998
1999 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2000 && bfd_section_vma (sectp) == 0)
2001 this->has_section_at_zero = true;
2002 }
2003
2004 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2005 SECTION_NAME. */
2006
2007 void
2008 dwarf2_get_section_info (struct objfile *objfile,
2009 enum dwarf2_section_enum sect,
2010 asection **sectp, const gdb_byte **bufp,
2011 bfd_size_type *sizep)
2012 {
2013 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2014 struct dwarf2_section_info *info;
2015
2016 /* We may see an objfile without any DWARF, in which case we just
2017 return nothing. */
2018 if (per_objfile == NULL)
2019 {
2020 *sectp = NULL;
2021 *bufp = NULL;
2022 *sizep = 0;
2023 return;
2024 }
2025 switch (sect)
2026 {
2027 case DWARF2_DEBUG_FRAME:
2028 info = &per_objfile->per_bfd->frame;
2029 break;
2030 case DWARF2_EH_FRAME:
2031 info = &per_objfile->per_bfd->eh_frame;
2032 break;
2033 default:
2034 gdb_assert_not_reached ("unexpected section");
2035 }
2036
2037 info->read (objfile);
2038
2039 *sectp = info->get_bfd_section ();
2040 *bufp = info->buffer;
2041 *sizep = info->size;
2042 }
2043
2044 /* See dwarf2/read.h. */
2045
2046 void
2047 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
2048 {
2049 info.read (objfile);
2050 abbrev.read (objfile);
2051 line.read (objfile);
2052 str.read (objfile);
2053 str_offsets.read (objfile);
2054 line_str.read (objfile);
2055 ranges.read (objfile);
2056 rnglists.read (objfile);
2057 addr.read (objfile);
2058
2059 for (auto &section : types)
2060 section.read (objfile);
2061 }
2062
2063 \f
2064 /* DWARF quick_symbol_functions support. */
2065
2066 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2067 unique line tables, so we maintain a separate table of all .debug_line
2068 derived entries to support the sharing.
2069 All the quick functions need is the list of file names. We discard the
2070 line_header when we're done and don't need to record it here. */
2071 struct quick_file_names
2072 {
2073 /* The data used to construct the hash key. */
2074 struct stmt_list_hash hash;
2075
2076 /* The number of entries in file_names, real_names. */
2077 unsigned int num_file_names;
2078
2079 /* The CU directory, as given by DW_AT_comp_dir. May be
2080 nullptr. */
2081 const char *comp_dir;
2082
2083 /* The file names from the line table, after being run through
2084 file_full_name. */
2085 const char **file_names;
2086
2087 /* The file names from the line table after being run through
2088 gdb_realpath. These are computed lazily. */
2089 const char **real_names;
2090 };
2091
2092 /* When using the index (and thus not using psymtabs), each CU has an
2093 object of this type. This is used to hold information needed by
2094 the various "quick" methods. */
2095 struct dwarf2_per_cu_quick_data
2096 {
2097 /* The file table. This can be NULL if there was no file table
2098 or it's currently not read in.
2099 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2100 struct quick_file_names *file_names;
2101
2102 /* A temporary mark bit used when iterating over all CUs in
2103 expand_symtabs_matching. */
2104 unsigned int mark : 1;
2105
2106 /* True if we've tried to read the file table. There will be no
2107 point in trying to read it again next time. */
2108 bool files_read : 1;
2109 };
2110
2111 /* A subclass of psymbol_functions that arranges to read the DWARF
2112 partial symbols when needed. */
2113 struct lazy_dwarf_reader : public psymbol_functions
2114 {
2115 using psymbol_functions::psymbol_functions;
2116
2117 bool can_lazily_read_symbols () override
2118 {
2119 return true;
2120 }
2121
2122 void read_partial_symbols (struct objfile *objfile) override
2123 {
2124 if (dwarf2_has_info (objfile, nullptr))
2125 dwarf2_build_psymtabs (objfile, this);
2126 }
2127 };
2128
2129 static quick_symbol_functions_up
2130 make_lazy_dwarf_reader ()
2131 {
2132 return quick_symbol_functions_up (new lazy_dwarf_reader);
2133 }
2134
2135 struct dwarf2_base_index_functions : public quick_symbol_functions
2136 {
2137 bool has_symbols (struct objfile *objfile) override;
2138
2139 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2140
2141 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2142
2143 void forget_cached_source_info (struct objfile *objfile) override;
2144
2145 enum language lookup_global_symbol_language (struct objfile *objfile,
2146 const char *name,
2147 domain_enum domain,
2148 bool *symbol_found_p) override
2149 {
2150 *symbol_found_p = false;
2151 return language_unknown;
2152 }
2153
2154 void print_stats (struct objfile *objfile, bool print_bcache) override;
2155
2156 void expand_all_symtabs (struct objfile *objfile) override;
2157
2158 struct compunit_symtab *find_pc_sect_compunit_symtab
2159 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2160 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2161
2162 struct compunit_symtab *find_compunit_symtab_by_address
2163 (struct objfile *objfile, CORE_ADDR address) override
2164 {
2165 return nullptr;
2166 }
2167
2168 void map_symbol_filenames (struct objfile *objfile,
2169 gdb::function_view<symbol_filename_ftype> fun,
2170 bool need_fullname) override;
2171 };
2172
2173 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2174 {
2175 void dump (struct objfile *objfile) override;
2176
2177 void expand_matching_symbols
2178 (struct objfile *,
2179 const lookup_name_info &lookup_name,
2180 domain_enum domain,
2181 int global,
2182 symbol_compare_ftype *ordered_compare) override;
2183
2184 bool expand_symtabs_matching
2185 (struct objfile *objfile,
2186 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2187 const lookup_name_info *lookup_name,
2188 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2189 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2190 block_search_flags search_flags,
2191 domain_enum domain,
2192 enum search_domain kind) override;
2193 };
2194
2195 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2196 {
2197 void dump (struct objfile *objfile) override;
2198
2199 void expand_matching_symbols
2200 (struct objfile *,
2201 const lookup_name_info &lookup_name,
2202 domain_enum domain,
2203 int global,
2204 symbol_compare_ftype *ordered_compare) override;
2205
2206 bool expand_symtabs_matching
2207 (struct objfile *objfile,
2208 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2209 const lookup_name_info *lookup_name,
2210 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2211 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2212 block_search_flags search_flags,
2213 domain_enum domain,
2214 enum search_domain kind) override;
2215 };
2216
2217 static quick_symbol_functions_up
2218 make_dwarf_gdb_index ()
2219 {
2220 return quick_symbol_functions_up (new dwarf2_gdb_index);
2221 }
2222
2223 static quick_symbol_functions_up
2224 make_dwarf_debug_names ()
2225 {
2226 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2227 }
2228
2229 /* Utility hash function for a stmt_list_hash. */
2230
2231 static hashval_t
2232 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2233 {
2234 hashval_t v = 0;
2235
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += to_underlying (stmt_list_hash->line_sect_off);
2239 return v;
2240 }
2241
2242 /* Utility equality function for a stmt_list_hash. */
2243
2244 static int
2245 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2247 {
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2249 return 0;
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return 0;
2253
2254 return lhs->line_sect_off == rhs->line_sect_off;
2255 }
2256
2257 /* Hash function for a quick_file_names. */
2258
2259 static hashval_t
2260 hash_file_name_entry (const void *e)
2261 {
2262 const struct quick_file_names *file_data
2263 = (const struct quick_file_names *) e;
2264
2265 return hash_stmt_list_entry (&file_data->hash);
2266 }
2267
2268 /* Equality function for a quick_file_names. */
2269
2270 static int
2271 eq_file_name_entry (const void *a, const void *b)
2272 {
2273 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2274 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2275
2276 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2277 }
2278
2279 /* Create a quick_file_names hash table. */
2280
2281 static htab_up
2282 create_quick_file_names_table (unsigned int nr_initial_entries)
2283 {
2284 return htab_up (htab_create_alloc (nr_initial_entries,
2285 hash_file_name_entry, eq_file_name_entry,
2286 nullptr, xcalloc, xfree));
2287 }
2288
2289 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2290 function is unrelated to symtabs, symtab would have to be created afterwards.
2291 You should call age_cached_comp_units after processing the CU. */
2292
2293 static dwarf2_cu *
2294 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2295 bool skip_partial)
2296 {
2297 if (per_cu->is_debug_types)
2298 load_full_type_unit (per_cu, per_objfile);
2299 else
2300 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2301 skip_partial, language_minimal);
2302
2303 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2304 if (cu == nullptr)
2305 return nullptr; /* Dummy CU. */
2306
2307 dwarf2_find_base_address (cu->dies, cu);
2308
2309 return cu;
2310 }
2311
2312 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2313
2314 static void
2315 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2316 dwarf2_per_objfile *per_objfile, bool skip_partial)
2317 {
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
2320 if (per_cu->type_unit_group_p ())
2321 return;
2322
2323 {
2324 /* The destructor of dwarf2_queue_guard frees any entries left on
2325 the queue. After this point we're guaranteed to leave this function
2326 with the dwarf queue empty. */
2327 dwarf2_queue_guard q_guard (per_objfile);
2328
2329 if (!per_objfile->symtab_set_p (per_cu))
2330 {
2331 queue_comp_unit (per_cu, per_objfile, language_minimal);
2332 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2333
2334 /* If we just loaded a CU from a DWO, and we're working with an index
2335 that may badly handle TUs, load all the TUs in that DWO as well.
2336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2337 if (!per_cu->is_debug_types
2338 && cu != NULL
2339 && cu->dwo_unit != NULL
2340 && per_objfile->per_bfd->index_table != NULL
2341 && per_objfile->per_bfd->index_table->version <= 7
2342 /* DWP files aren't supported yet. */
2343 && get_dwp_file (per_objfile) == NULL)
2344 queue_and_load_all_dwo_tus (cu);
2345 }
2346
2347 process_queue (per_objfile);
2348 }
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
2352 per_objfile->age_comp_units ();
2353 }
2354
2355 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2356 the per-objfile for which this symtab is instantiated.
2357
2358 Returns the resulting symbol table. */
2359
2360 static struct compunit_symtab *
2361 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2362 dwarf2_per_objfile *per_objfile,
2363 bool skip_partial)
2364 {
2365 gdb_assert (per_objfile->per_bfd->using_index);
2366
2367 if (!per_objfile->symtab_set_p (per_cu))
2368 {
2369 free_cached_comp_units freer (per_objfile);
2370 scoped_restore decrementer = increment_reading_symtab ();
2371 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2372 process_cu_includes (per_objfile);
2373 }
2374
2375 return per_objfile->get_symtab (per_cu);
2376 }
2377
2378 /* See read.h. */
2379
2380 dwarf2_per_cu_data_up
2381 dwarf2_per_bfd::allocate_per_cu ()
2382 {
2383 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2384 result->per_bfd = this;
2385 result->index = all_comp_units.size ();
2386 return result;
2387 }
2388
2389 /* See read.h. */
2390
2391 signatured_type_up
2392 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2393 {
2394 signatured_type_up result (new signatured_type (signature));
2395 result->per_bfd = this;
2396 result->index = all_comp_units.size ();
2397 result->is_debug_types = true;
2398 tu_stats.nr_tus++;
2399 return result;
2400 }
2401
2402 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2403 obstack, and constructed with the specified field values. */
2404
2405 static dwarf2_per_cu_data_up
2406 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2407 struct dwarf2_section_info *section,
2408 int is_dwz,
2409 sect_offset sect_off, ULONGEST length)
2410 {
2411 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2412 the_cu->sect_off = sect_off;
2413 the_cu->length = length;
2414 the_cu->section = section;
2415 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2416 struct dwarf2_per_cu_quick_data);
2417 the_cu->is_dwz = is_dwz;
2418 return the_cu;
2419 }
2420
2421 /* A helper for create_cus_from_index that handles a given list of
2422 CUs. */
2423
2424 static void
2425 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2426 const gdb_byte *cu_list, offset_type n_elements,
2427 struct dwarf2_section_info *section,
2428 int is_dwz)
2429 {
2430 for (offset_type i = 0; i < n_elements; i += 2)
2431 {
2432 gdb_static_assert (sizeof (ULONGEST) >= 8);
2433
2434 sect_offset sect_off
2435 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2436 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2437 cu_list += 2 * 8;
2438
2439 dwarf2_per_cu_data_up per_cu
2440 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2441 length);
2442 per_bfd->all_comp_units.push_back (std::move (per_cu));
2443 }
2444 }
2445
2446 /* Read the CU list from the mapped index, and use it to create all
2447 the CU objects for PER_BFD. */
2448
2449 static void
2450 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2451 const gdb_byte *cu_list, offset_type cu_list_elements,
2452 const gdb_byte *dwz_list, offset_type dwz_elements)
2453 {
2454 gdb_assert (per_bfd->all_comp_units.empty ());
2455 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2456
2457 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2458 &per_bfd->info, 0);
2459
2460 if (dwz_elements == 0)
2461 return;
2462
2463 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2464 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2465 &dwz->info, 1);
2466 }
2467
2468 /* Create the signatured type hash table from the index. */
2469
2470 static void
2471 create_signatured_type_table_from_index
2472 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2473 const gdb_byte *bytes, offset_type elements)
2474 {
2475 htab_up sig_types_hash = allocate_signatured_type_table ();
2476
2477 for (offset_type i = 0; i < elements; i += 3)
2478 {
2479 signatured_type_up sig_type;
2480 ULONGEST signature;
2481 void **slot;
2482 cu_offset type_offset_in_tu;
2483
2484 gdb_static_assert (sizeof (ULONGEST) >= 8);
2485 sect_offset sect_off
2486 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2487 type_offset_in_tu
2488 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2489 BFD_ENDIAN_LITTLE);
2490 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2491 bytes += 3 * 8;
2492
2493 sig_type = per_bfd->allocate_signatured_type (signature);
2494 sig_type->type_offset_in_tu = type_offset_in_tu;
2495 sig_type->section = section;
2496 sig_type->sect_off = sect_off;
2497 sig_type->v.quick
2498 = OBSTACK_ZALLOC (&per_bfd->obstack,
2499 struct dwarf2_per_cu_quick_data);
2500
2501 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2502 *slot = sig_type.get ();
2503
2504 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2505 }
2506
2507 per_bfd->signatured_types = std::move (sig_types_hash);
2508 }
2509
2510 /* Create the signatured type hash table from .debug_names. */
2511
2512 static void
2513 create_signatured_type_table_from_debug_names
2514 (dwarf2_per_objfile *per_objfile,
2515 const mapped_debug_names &map,
2516 struct dwarf2_section_info *section,
2517 struct dwarf2_section_info *abbrev_section)
2518 {
2519 struct objfile *objfile = per_objfile->objfile;
2520
2521 section->read (objfile);
2522 abbrev_section->read (objfile);
2523
2524 htab_up sig_types_hash = allocate_signatured_type_table ();
2525
2526 for (uint32_t i = 0; i < map.tu_count; ++i)
2527 {
2528 signatured_type_up sig_type;
2529 void **slot;
2530
2531 sect_offset sect_off
2532 = (sect_offset) (extract_unsigned_integer
2533 (map.tu_table_reordered + i * map.offset_size,
2534 map.offset_size,
2535 map.dwarf5_byte_order));
2536
2537 comp_unit_head cu_header;
2538 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2539 abbrev_section,
2540 section->buffer + to_underlying (sect_off),
2541 rcuh_kind::TYPE);
2542
2543 sig_type = per_objfile->per_bfd->allocate_signatured_type
2544 (cu_header.signature);
2545 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2546 sig_type->section = section;
2547 sig_type->sect_off = sect_off;
2548 sig_type->v.quick
2549 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2550 struct dwarf2_per_cu_quick_data);
2551
2552 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2553 *slot = sig_type.get ();
2554
2555 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2556 }
2557
2558 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2559 }
2560
2561 /* Read the address map data from the mapped index, and use it to
2562 populate the psymtabs_addrmap. */
2563
2564 static void
2565 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2566 struct mapped_index *index)
2567 {
2568 struct objfile *objfile = per_objfile->objfile;
2569 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2570 struct gdbarch *gdbarch = objfile->arch ();
2571 const gdb_byte *iter, *end;
2572 struct addrmap *mutable_map;
2573 CORE_ADDR baseaddr;
2574
2575 auto_obstack temp_obstack;
2576
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
2581
2582 baseaddr = objfile->text_section_offset ();
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
2593
2594 if (lo > hi)
2595 {
2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2597 hex_string (lo), hex_string (hi));
2598 continue;
2599 }
2600
2601 if (cu_index >= per_bfd->all_comp_units.size ())
2602 {
2603 complaint (_(".gdb_index address table has invalid CU number %u"),
2604 (unsigned) cu_index);
2605 continue;
2606 }
2607
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2610 addrmap_set_empty (mutable_map, lo, hi - 1,
2611 per_bfd->get_cu (cu_index));
2612 }
2613
2614 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2615 &per_bfd->obstack);
2616 }
2617
2618 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2619 to populate given addrmap. Returns true on success, false on
2620 failure. */
2621
2622 static bool
2623 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2624 struct dwarf2_section_info *section,
2625 addrmap *mutable_map)
2626 {
2627 struct objfile *objfile = per_objfile->objfile;
2628 bfd *abfd = objfile->obfd;
2629 struct gdbarch *gdbarch = objfile->arch ();
2630 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2631 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
2637 for (const auto &per_cu : per_bfd->all_comp_units)
2638 {
2639 /* A TU will not need aranges, and skipping them here is an easy
2640 way of ignoring .debug_types -- and possibly seeing a
2641 duplicate section offset -- entirely. The same applies to
2642 units coming from a dwz file. */
2643 if (per_cu->is_debug_types || per_cu->is_dwz)
2644 continue;
2645
2646 const auto insertpair
2647 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2648 per_cu.get ());
2649 if (!insertpair.second)
2650 {
2651 warning (_("Section .debug_aranges in %s has duplicate "
2652 "debug_info_offset %s, ignoring .debug_aranges."),
2653 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2654 return false;
2655 }
2656 }
2657
2658 section->read (objfile);
2659
2660 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2661
2662 const gdb_byte *addr = section->buffer;
2663
2664 while (addr < section->buffer + section->size)
2665 {
2666 const gdb_byte *const entry_addr = addr;
2667 unsigned int bytes_read;
2668
2669 const LONGEST entry_length = read_initial_length (abfd, addr,
2670 &bytes_read);
2671 addr += bytes_read;
2672
2673 const gdb_byte *const entry_end = addr + entry_length;
2674 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2675 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2676 if (addr + entry_length > section->buffer + section->size)
2677 {
2678 warning (_("Section .debug_aranges in %s entry at offset %s "
2679 "length %s exceeds section length %s, "
2680 "ignoring .debug_aranges."),
2681 objfile_name (objfile),
2682 plongest (entry_addr - section->buffer),
2683 plongest (bytes_read + entry_length),
2684 pulongest (section->size));
2685 return false;
2686 }
2687
2688 /* The version number. */
2689 const uint16_t version = read_2_bytes (abfd, addr);
2690 addr += 2;
2691 if (version != 2)
2692 {
2693 warning (_("Section .debug_aranges in %s entry at offset %s "
2694 "has unsupported version %d, ignoring .debug_aranges."),
2695 objfile_name (objfile),
2696 plongest (entry_addr - section->buffer), version);
2697 return false;
2698 }
2699
2700 const uint64_t debug_info_offset
2701 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2702 addr += offset_size;
2703 const auto per_cu_it
2704 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2705 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2706 {
2707 warning (_("Section .debug_aranges in %s entry at offset %s "
2708 "debug_info_offset %s does not exists, "
2709 "ignoring .debug_aranges."),
2710 objfile_name (objfile),
2711 plongest (entry_addr - section->buffer),
2712 pulongest (debug_info_offset));
2713 return false;
2714 }
2715 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2716
2717 const uint8_t address_size = *addr++;
2718 if (address_size < 1 || address_size > 8)
2719 {
2720 warning (_("Section .debug_aranges in %s entry at offset %s "
2721 "address_size %u is invalid, ignoring .debug_aranges."),
2722 objfile_name (objfile),
2723 plongest (entry_addr - section->buffer), address_size);
2724 return false;
2725 }
2726
2727 const uint8_t segment_selector_size = *addr++;
2728 if (segment_selector_size != 0)
2729 {
2730 warning (_("Section .debug_aranges in %s entry at offset %s "
2731 "segment_selector_size %u is not supported, "
2732 "ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
2735 segment_selector_size);
2736 return false;
2737 }
2738
2739 /* Must pad to an alignment boundary that is twice the address
2740 size. It is undocumented by the DWARF standard but GCC does
2741 use it. However, not every compiler does this. We can see
2742 whether it has happened by looking at the total length of the
2743 contents of the aranges for this CU -- it if isn't a multiple
2744 of twice the address size, then we skip any leftover
2745 bytes. */
2746 addr += (entry_end - addr) % (2 * address_size);
2747
2748 for (;;)
2749 {
2750 if (addr + 2 * address_size > entry_end)
2751 {
2752 warning (_("Section .debug_aranges in %s entry at offset %s "
2753 "address list is not properly terminated, "
2754 "ignoring .debug_aranges."),
2755 objfile_name (objfile),
2756 plongest (entry_addr - section->buffer));
2757 return false;
2758 }
2759 ULONGEST start = extract_unsigned_integer (addr, address_size,
2760 dwarf5_byte_order);
2761 addr += address_size;
2762 ULONGEST length = extract_unsigned_integer (addr, address_size,
2763 dwarf5_byte_order);
2764 addr += address_size;
2765 if (start == 0 && length == 0)
2766 break;
2767 if (start == 0 && !per_bfd->has_section_at_zero)
2768 {
2769 /* Symbol was eliminated due to a COMDAT group. */
2770 continue;
2771 }
2772 ULONGEST end = start + length;
2773 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2774 - baseaddr);
2775 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2776 - baseaddr);
2777 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2778 }
2779
2780 per_cu->addresses_seen = true;
2781 }
2782
2783 return true;
2784 }
2785
2786 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2787 populate the psymtabs_addrmap. */
2788
2789 static void
2790 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2791 struct dwarf2_section_info *section)
2792 {
2793 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2794
2795 auto_obstack temp_obstack;
2796 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2797
2798 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2799 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2800 &per_bfd->obstack);
2801 }
2802
2803 /* A helper function that reads the .gdb_index from BUFFER and fills
2804 in MAP. FILENAME is the name of the file containing the data;
2805 it is used for error reporting. DEPRECATED_OK is true if it is
2806 ok to use deprecated sections.
2807
2808 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2809 out parameters that are filled in with information about the CU and
2810 TU lists in the section.
2811
2812 Returns true if all went well, false otherwise. */
2813
2814 static bool
2815 read_gdb_index_from_buffer (const char *filename,
2816 bool deprecated_ok,
2817 gdb::array_view<const gdb_byte> buffer,
2818 struct mapped_index *map,
2819 const gdb_byte **cu_list,
2820 offset_type *cu_list_elements,
2821 const gdb_byte **types_list,
2822 offset_type *types_list_elements)
2823 {
2824 const gdb_byte *addr = &buffer[0];
2825 offset_view metadata (buffer);
2826
2827 /* Version check. */
2828 offset_type version = metadata[0];
2829 /* Versions earlier than 3 emitted every copy of a psymbol. This
2830 causes the index to behave very poorly for certain requests. Version 3
2831 contained incomplete addrmap. So, it seems better to just ignore such
2832 indices. */
2833 if (version < 4)
2834 {
2835 static int warning_printed = 0;
2836 if (!warning_printed)
2837 {
2838 warning (_("Skipping obsolete .gdb_index section in %s."),
2839 filename);
2840 warning_printed = 1;
2841 }
2842 return 0;
2843 }
2844 /* Index version 4 uses a different hash function than index version
2845 5 and later.
2846
2847 Versions earlier than 6 did not emit psymbols for inlined
2848 functions. Using these files will cause GDB not to be able to
2849 set breakpoints on inlined functions by name, so we ignore these
2850 indices unless the user has done
2851 "set use-deprecated-index-sections on". */
2852 if (version < 6 && !deprecated_ok)
2853 {
2854 static int warning_printed = 0;
2855 if (!warning_printed)
2856 {
2857 warning (_("\
2858 Skipping deprecated .gdb_index section in %s.\n\
2859 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2860 to use the section anyway."),
2861 filename);
2862 warning_printed = 1;
2863 }
2864 return 0;
2865 }
2866 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2867 of the TU (for symbols coming from TUs),
2868 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2869 Plus gold-generated indices can have duplicate entries for global symbols,
2870 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2871 These are just performance bugs, and we can't distinguish gdb-generated
2872 indices from gold-generated ones, so issue no warning here. */
2873
2874 /* Indexes with higher version than the one supported by GDB may be no
2875 longer backward compatible. */
2876 if (version > 8)
2877 return 0;
2878
2879 map->version = version;
2880
2881 int i = 1;
2882 *cu_list = addr + metadata[i];
2883 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2884 ++i;
2885
2886 *types_list = addr + metadata[i];
2887 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2888 ++i;
2889
2890 const gdb_byte *address_table = addr + metadata[i];
2891 const gdb_byte *address_table_end = addr + metadata[i + 1];
2892 map->address_table
2893 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2894 ++i;
2895
2896 const gdb_byte *symbol_table = addr + metadata[i];
2897 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2898 map->symbol_table
2899 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2900 symbol_table_end));
2901
2902 ++i;
2903 map->constant_pool = buffer.slice (metadata[i]);
2904
2905 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2906 {
2907 /* An empty constant pool implies that all symbol table entries are
2908 empty. Make map->symbol_table.empty () == true. */
2909 map->symbol_table
2910 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2911 symbol_table));
2912 }
2913
2914 return 1;
2915 }
2916
2917 /* Callback types for dwarf2_read_gdb_index. */
2918
2919 typedef gdb::function_view
2920 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2921 get_gdb_index_contents_ftype;
2922 typedef gdb::function_view
2923 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2924 get_gdb_index_contents_dwz_ftype;
2925
2926 /* Read .gdb_index. If everything went ok, initialize the "quick"
2927 elements of all the CUs and return 1. Otherwise, return 0. */
2928
2929 static int
2930 dwarf2_read_gdb_index
2931 (dwarf2_per_objfile *per_objfile,
2932 get_gdb_index_contents_ftype get_gdb_index_contents,
2933 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2934 {
2935 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2936 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2937 struct dwz_file *dwz;
2938 struct objfile *objfile = per_objfile->objfile;
2939 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2940
2941 gdb::array_view<const gdb_byte> main_index_contents
2942 = get_gdb_index_contents (objfile, per_bfd);
2943
2944 if (main_index_contents.empty ())
2945 return 0;
2946
2947 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2948 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2949 use_deprecated_index_sections,
2950 main_index_contents, map.get (), &cu_list,
2951 &cu_list_elements, &types_list,
2952 &types_list_elements))
2953 return 0;
2954
2955 /* Don't use the index if it's empty. */
2956 if (map->symbol_table.empty ())
2957 return 0;
2958
2959 /* If there is a .dwz file, read it so we can get its CU list as
2960 well. */
2961 dwz = dwarf2_get_dwz_file (per_bfd);
2962 if (dwz != NULL)
2963 {
2964 struct mapped_index dwz_map;
2965 const gdb_byte *dwz_types_ignore;
2966 offset_type dwz_types_elements_ignore;
2967
2968 gdb::array_view<const gdb_byte> dwz_index_content
2969 = get_gdb_index_contents_dwz (objfile, dwz);
2970
2971 if (dwz_index_content.empty ())
2972 return 0;
2973
2974 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2975 1, dwz_index_content, &dwz_map,
2976 &dwz_list, &dwz_list_elements,
2977 &dwz_types_ignore,
2978 &dwz_types_elements_ignore))
2979 {
2980 warning (_("could not read '.gdb_index' section from %s; skipping"),
2981 bfd_get_filename (dwz->dwz_bfd.get ()));
2982 return 0;
2983 }
2984 }
2985
2986 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2987 dwz_list_elements);
2988
2989 if (types_list_elements)
2990 {
2991 /* We can only handle a single .debug_types when we have an
2992 index. */
2993 if (per_bfd->types.size () != 1)
2994 return 0;
2995
2996 dwarf2_section_info *section = &per_bfd->types[0];
2997
2998 create_signatured_type_table_from_index (per_bfd, section, types_list,
2999 types_list_elements);
3000 }
3001
3002 create_addrmap_from_index (per_objfile, map.get ());
3003
3004 per_bfd->index_table = std::move (map);
3005 per_bfd->using_index = 1;
3006 per_bfd->quick_file_names_table =
3007 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3008
3009 return 1;
3010 }
3011
3012 /* die_reader_func for dw2_get_file_names. */
3013
3014 static void
3015 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3016 struct die_info *comp_unit_die)
3017 {
3018 struct dwarf2_cu *cu = reader->cu;
3019 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3021 struct dwarf2_per_cu_data *lh_cu;
3022 struct attribute *attr;
3023 void **slot;
3024 struct quick_file_names *qfn;
3025
3026 gdb_assert (! this_cu->is_debug_types);
3027
3028 this_cu->v.quick->files_read = true;
3029 /* Our callers never want to match partial units -- instead they
3030 will match the enclosing full CU. */
3031 if (comp_unit_die->tag == DW_TAG_partial_unit)
3032 return;
3033
3034 lh_cu = this_cu;
3035 slot = NULL;
3036
3037 line_header_up lh;
3038 sect_offset line_offset {};
3039
3040 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3041 if (attr != nullptr && attr->form_is_unsigned ())
3042 {
3043 struct quick_file_names find_entry;
3044
3045 line_offset = (sect_offset) attr->as_unsigned ();
3046
3047 /* We may have already read in this line header (TU line header sharing).
3048 If we have we're done. */
3049 find_entry.hash.dwo_unit = cu->dwo_unit;
3050 find_entry.hash.line_sect_off = line_offset;
3051 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3052 &find_entry, INSERT);
3053 if (*slot != NULL)
3054 {
3055 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3056 return;
3057 }
3058
3059 lh = dwarf_decode_line_header (line_offset, cu);
3060 }
3061
3062 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3063
3064 int offset = 0;
3065 if (!fnd.is_unknown ())
3066 ++offset;
3067 else if (lh == nullptr)
3068 return;
3069
3070 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3071 qfn->hash.dwo_unit = cu->dwo_unit;
3072 qfn->hash.line_sect_off = line_offset;
3073 /* There may not be a DW_AT_stmt_list. */
3074 if (slot != nullptr)
3075 *slot = qfn;
3076
3077 std::vector<const char *> include_names;
3078 if (lh != nullptr)
3079 {
3080 for (const auto &entry : lh->file_names ())
3081 {
3082 gdb::unique_xmalloc_ptr<char> name_holder;
3083 const char *include_name =
3084 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3085 if (include_name != nullptr)
3086 {
3087 include_name = per_objfile->objfile->intern (include_name);
3088 include_names.push_back (include_name);
3089 }
3090 }
3091 }
3092
3093 qfn->num_file_names = offset + include_names.size ();
3094 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3095 qfn->file_names =
3096 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3097 qfn->num_file_names);
3098 if (offset != 0)
3099 qfn->file_names[0] = xstrdup (fnd.get_name ());
3100
3101 if (!include_names.empty ())
3102 memcpy (&qfn->file_names[offset], include_names.data (),
3103 include_names.size () * sizeof (const char *));
3104
3105 qfn->real_names = NULL;
3106
3107 lh_cu->v.quick->file_names = qfn;
3108 }
3109
3110 /* A helper for the "quick" functions which attempts to read the line
3111 table for THIS_CU. */
3112
3113 static struct quick_file_names *
3114 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3115 dwarf2_per_objfile *per_objfile)
3116 {
3117 /* This should never be called for TUs. */
3118 gdb_assert (! this_cu->is_debug_types);
3119 /* Nor type unit groups. */
3120 gdb_assert (! this_cu->type_unit_group_p ());
3121
3122 if (this_cu->v.quick->files_read)
3123 return this_cu->v.quick->file_names;
3124
3125 cutu_reader reader (this_cu, per_objfile);
3126 if (!reader.dummy_p)
3127 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3128
3129 return this_cu->v.quick->file_names;
3130 }
3131
3132 /* A helper for the "quick" functions which computes and caches the
3133 real path for a given file name from the line table. */
3134
3135 static const char *
3136 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3137 struct quick_file_names *qfn, int index)
3138 {
3139 if (qfn->real_names == NULL)
3140 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3141 qfn->num_file_names, const char *);
3142
3143 if (qfn->real_names[index] == NULL)
3144 {
3145 const char *dirname = nullptr;
3146
3147 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3148 dirname = qfn->comp_dir;
3149
3150 gdb::unique_xmalloc_ptr<char> fullname;
3151 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3152
3153 qfn->real_names[index] = fullname.release ();
3154 }
3155
3156 return qfn->real_names[index];
3157 }
3158
3159 struct symtab *
3160 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3161 {
3162 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3163 dwarf2_per_cu_data *dwarf_cu
3164 = per_objfile->per_bfd->all_comp_units.back ().get ();
3165 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3166
3167 if (cust == NULL)
3168 return NULL;
3169
3170 return cust->primary_filetab ();
3171 }
3172
3173 /* See read.h. */
3174
3175 void
3176 dwarf2_per_cu_data::free_cached_file_names ()
3177 {
3178 if (fnd != nullptr)
3179 fnd->forget_fullname ();
3180
3181 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3182 return;
3183
3184 struct quick_file_names *file_data = v.quick->file_names;
3185 if (file_data != nullptr && file_data->real_names != nullptr)
3186 {
3187 for (int i = 0; i < file_data->num_file_names; ++i)
3188 {
3189 xfree ((void *) file_data->real_names[i]);
3190 file_data->real_names[i] = nullptr;
3191 }
3192 }
3193 }
3194
3195 void
3196 dwarf2_base_index_functions::forget_cached_source_info
3197 (struct objfile *objfile)
3198 {
3199 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3200
3201 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3202 per_cu->free_cached_file_names ();
3203 }
3204
3205 /* Struct used to manage iterating over all CUs looking for a symbol. */
3206
3207 struct dw2_symtab_iterator
3208 {
3209 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3210 dwarf2_per_objfile *per_objfile;
3211 /* If set, only look for symbols that match that block. Valid values are
3212 GLOBAL_BLOCK and STATIC_BLOCK. */
3213 gdb::optional<block_enum> block_index;
3214 /* The kind of symbol we're looking for. */
3215 domain_enum domain;
3216 /* The list of CUs from the index entry of the symbol,
3217 or NULL if not found. */
3218 offset_view vec;
3219 /* The next element in VEC to look at. */
3220 int next;
3221 /* The number of elements in VEC, or zero if there is no match. */
3222 int length;
3223 /* Have we seen a global version of the symbol?
3224 If so we can ignore all further global instances.
3225 This is to work around gold/15646, inefficient gold-generated
3226 indices. */
3227 int global_seen;
3228 };
3229
3230 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3231
3232 static void
3233 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3234 dwarf2_per_objfile *per_objfile,
3235 gdb::optional<block_enum> block_index,
3236 domain_enum domain, offset_type namei)
3237 {
3238 iter->per_objfile = per_objfile;
3239 iter->block_index = block_index;
3240 iter->domain = domain;
3241 iter->next = 0;
3242 iter->global_seen = 0;
3243 iter->vec = {};
3244 iter->length = 0;
3245
3246 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3247 /* index is NULL if OBJF_READNOW. */
3248 if (index == NULL)
3249 return;
3250
3251 gdb_assert (!index->symbol_name_slot_invalid (namei));
3252 offset_type vec_idx = index->symbol_vec_index (namei);
3253
3254 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3255 iter->length = iter->vec[0];
3256 }
3257
3258 /* Return the next matching CU or NULL if there are no more. */
3259
3260 static struct dwarf2_per_cu_data *
3261 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3262 {
3263 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3264
3265 for ( ; iter->next < iter->length; ++iter->next)
3266 {
3267 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3268 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3269 gdb_index_symbol_kind symbol_kind =
3270 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3271 /* Only check the symbol attributes if they're present.
3272 Indices prior to version 7 don't record them,
3273 and indices >= 7 may elide them for certain symbols
3274 (gold does this). */
3275 int attrs_valid =
3276 (per_objfile->per_bfd->index_table->version >= 7
3277 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3278
3279 /* Don't crash on bad data. */
3280 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3281 {
3282 complaint (_(".gdb_index entry has bad CU index"
3283 " [in module %s]"), objfile_name (per_objfile->objfile));
3284 continue;
3285 }
3286
3287 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3288
3289 /* Skip if already read in. */
3290 if (per_objfile->symtab_set_p (per_cu))
3291 continue;
3292
3293 /* Check static vs global. */
3294 if (attrs_valid)
3295 {
3296 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3297
3298 if (iter->block_index.has_value ())
3299 {
3300 bool want_static = *iter->block_index == STATIC_BLOCK;
3301
3302 if (is_static != want_static)
3303 continue;
3304 }
3305
3306 /* Work around gold/15646. */
3307 if (!is_static
3308 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3309 {
3310 if (iter->global_seen)
3311 continue;
3312
3313 iter->global_seen = 1;
3314 }
3315 }
3316
3317 /* Only check the symbol's kind if it has one. */
3318 if (attrs_valid)
3319 {
3320 switch (iter->domain)
3321 {
3322 case VAR_DOMAIN:
3323 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3324 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3325 /* Some types are also in VAR_DOMAIN. */
3326 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3327 continue;
3328 break;
3329 case STRUCT_DOMAIN:
3330 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3331 continue;
3332 break;
3333 case LABEL_DOMAIN:
3334 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3335 continue;
3336 break;
3337 case MODULE_DOMAIN:
3338 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3339 continue;
3340 break;
3341 default:
3342 break;
3343 }
3344 }
3345
3346 ++iter->next;
3347 return per_cu;
3348 }
3349
3350 return NULL;
3351 }
3352
3353 void
3354 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3355 bool print_bcache)
3356 {
3357 if (print_bcache)
3358 return;
3359
3360 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3361 int total = per_objfile->per_bfd->all_comp_units.size ();
3362 int count = 0;
3363
3364 for (int i = 0; i < total; ++i)
3365 {
3366 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3367
3368 if (!per_objfile->symtab_set_p (per_cu))
3369 ++count;
3370 }
3371 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3372 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3373 }
3374
3375 /* This dumps minimal information about the index.
3376 It is called via "mt print objfiles".
3377 One use is to verify .gdb_index has been loaded by the
3378 gdb.dwarf2/gdb-index.exp testcase. */
3379
3380 void
3381 dwarf2_gdb_index::dump (struct objfile *objfile)
3382 {
3383 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3384
3385 gdb_assert (per_objfile->per_bfd->using_index);
3386 gdb_printf (".gdb_index:");
3387 if (per_objfile->per_bfd->index_table != NULL)
3388 {
3389 gdb_printf (" version %d\n",
3390 per_objfile->per_bfd->index_table->version);
3391 }
3392 else
3393 gdb_printf (" faked for \"readnow\"\n");
3394 gdb_printf ("\n");
3395 }
3396
3397 void
3398 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3399 {
3400 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3401 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3402
3403 for (int i = 0; i < total_units; ++i)
3404 {
3405 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3406
3407 /* We don't want to directly expand a partial CU, because if we
3408 read it with the wrong language, then assertion failures can
3409 be triggered later on. See PR symtab/23010. So, tell
3410 dw2_instantiate_symtab to skip partial CUs -- any important
3411 partial CU will be read via DW_TAG_imported_unit anyway. */
3412 dw2_instantiate_symtab (per_cu, per_objfile, true);
3413 }
3414 }
3415
3416 static bool
3417 dw2_expand_symtabs_matching_symbol
3418 (mapped_index_base &index,
3419 const lookup_name_info &lookup_name_in,
3420 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3421 gdb::function_view<bool (offset_type)> match_callback,
3422 dwarf2_per_objfile *per_objfile);
3423
3424 static bool
3425 dw2_expand_symtabs_matching_one
3426 (dwarf2_per_cu_data *per_cu,
3427 dwarf2_per_objfile *per_objfile,
3428 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3429 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3430
3431 void
3432 dwarf2_gdb_index::expand_matching_symbols
3433 (struct objfile *objfile,
3434 const lookup_name_info &name, domain_enum domain,
3435 int global,
3436 symbol_compare_ftype *ordered_compare)
3437 {
3438 /* Used for Ada. */
3439 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3440
3441 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3442
3443 if (per_objfile->per_bfd->index_table != nullptr)
3444 {
3445 mapped_index &index = *per_objfile->per_bfd->index_table;
3446
3447 const char *match_name = name.ada ().lookup_name ().c_str ();
3448 auto matcher = [&] (const char *symname)
3449 {
3450 if (ordered_compare == nullptr)
3451 return true;
3452 return ordered_compare (symname, match_name) == 0;
3453 };
3454
3455 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3456 [&] (offset_type namei)
3457 {
3458 struct dw2_symtab_iterator iter;
3459 struct dwarf2_per_cu_data *per_cu;
3460
3461 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3462 namei);
3463 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3464 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3465 nullptr);
3466 return true;
3467 }, per_objfile);
3468 }
3469 else
3470 {
3471 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3472 proceed assuming all symtabs have been read in. */
3473 }
3474 }
3475
3476 /* Starting from a search name, return the string that finds the upper
3477 bound of all strings that start with SEARCH_NAME in a sorted name
3478 list. Returns the empty string to indicate that the upper bound is
3479 the end of the list. */
3480
3481 static std::string
3482 make_sort_after_prefix_name (const char *search_name)
3483 {
3484 /* When looking to complete "func", we find the upper bound of all
3485 symbols that start with "func" by looking for where we'd insert
3486 the closest string that would follow "func" in lexicographical
3487 order. Usually, that's "func"-with-last-character-incremented,
3488 i.e. "fund". Mind non-ASCII characters, though. Usually those
3489 will be UTF-8 multi-byte sequences, but we can't be certain.
3490 Especially mind the 0xff character, which is a valid character in
3491 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3492 rule out compilers allowing it in identifiers. Note that
3493 conveniently, strcmp/strcasecmp are specified to compare
3494 characters interpreted as unsigned char. So what we do is treat
3495 the whole string as a base 256 number composed of a sequence of
3496 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3497 to 0, and carries 1 to the following more-significant position.
3498 If the very first character in SEARCH_NAME ends up incremented
3499 and carries/overflows, then the upper bound is the end of the
3500 list. The string after the empty string is also the empty
3501 string.
3502
3503 Some examples of this operation:
3504
3505 SEARCH_NAME => "+1" RESULT
3506
3507 "abc" => "abd"
3508 "ab\xff" => "ac"
3509 "\xff" "a" "\xff" => "\xff" "b"
3510 "\xff" => ""
3511 "\xff\xff" => ""
3512 "" => ""
3513
3514 Then, with these symbols for example:
3515
3516 func
3517 func1
3518 fund
3519
3520 completing "func" looks for symbols between "func" and
3521 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3522 which finds "func" and "func1", but not "fund".
3523
3524 And with:
3525
3526 funcÿ (Latin1 'ÿ' [0xff])
3527 funcÿ1
3528 fund
3529
3530 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3531 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3532
3533 And with:
3534
3535 ÿÿ (Latin1 'ÿ' [0xff])
3536 ÿÿ1
3537
3538 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3539 the end of the list.
3540 */
3541 std::string after = search_name;
3542 while (!after.empty () && (unsigned char) after.back () == 0xff)
3543 after.pop_back ();
3544 if (!after.empty ())
3545 after.back () = (unsigned char) after.back () + 1;
3546 return after;
3547 }
3548
3549 /* See declaration. */
3550
3551 std::pair<std::vector<name_component>::const_iterator,
3552 std::vector<name_component>::const_iterator>
3553 mapped_index_base::find_name_components_bounds
3554 (const lookup_name_info &lookup_name_without_params, language lang,
3555 dwarf2_per_objfile *per_objfile) const
3556 {
3557 auto *name_cmp
3558 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3559
3560 const char *lang_name
3561 = lookup_name_without_params.language_lookup_name (lang);
3562
3563 /* Comparison function object for lower_bound that matches against a
3564 given symbol name. */
3565 auto lookup_compare_lower = [&] (const name_component &elem,
3566 const char *name)
3567 {
3568 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3569 const char *elem_name = elem_qualified + elem.name_offset;
3570 return name_cmp (elem_name, name) < 0;
3571 };
3572
3573 /* Comparison function object for upper_bound that matches against a
3574 given symbol name. */
3575 auto lookup_compare_upper = [&] (const char *name,
3576 const name_component &elem)
3577 {
3578 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3579 const char *elem_name = elem_qualified + elem.name_offset;
3580 return name_cmp (name, elem_name) < 0;
3581 };
3582
3583 auto begin = this->name_components.begin ();
3584 auto end = this->name_components.end ();
3585
3586 /* Find the lower bound. */
3587 auto lower = [&] ()
3588 {
3589 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3590 return begin;
3591 else
3592 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3593 } ();
3594
3595 /* Find the upper bound. */
3596 auto upper = [&] ()
3597 {
3598 if (lookup_name_without_params.completion_mode ())
3599 {
3600 /* In completion mode, we want UPPER to point past all
3601 symbols names that have the same prefix. I.e., with
3602 these symbols, and completing "func":
3603
3604 function << lower bound
3605 function1
3606 other_function << upper bound
3607
3608 We find the upper bound by looking for the insertion
3609 point of "func"-with-last-character-incremented,
3610 i.e. "fund". */
3611 std::string after = make_sort_after_prefix_name (lang_name);
3612 if (after.empty ())
3613 return end;
3614 return std::lower_bound (lower, end, after.c_str (),
3615 lookup_compare_lower);
3616 }
3617 else
3618 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3619 } ();
3620
3621 return {lower, upper};
3622 }
3623
3624 /* See declaration. */
3625
3626 void
3627 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3628 {
3629 if (!this->name_components.empty ())
3630 return;
3631
3632 this->name_components_casing = case_sensitivity;
3633 auto *name_cmp
3634 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3635
3636 /* The code below only knows how to break apart components of C++
3637 symbol names (and other languages that use '::' as
3638 namespace/module separator) and Ada symbol names. */
3639 auto count = this->symbol_name_count ();
3640 for (offset_type idx = 0; idx < count; idx++)
3641 {
3642 if (this->symbol_name_slot_invalid (idx))
3643 continue;
3644
3645 const char *name = this->symbol_name_at (idx, per_objfile);
3646
3647 /* Add each name component to the name component table. */
3648 unsigned int previous_len = 0;
3649
3650 if (strstr (name, "::") != nullptr)
3651 {
3652 for (unsigned int current_len = cp_find_first_component (name);
3653 name[current_len] != '\0';
3654 current_len += cp_find_first_component (name + current_len))
3655 {
3656 gdb_assert (name[current_len] == ':');
3657 this->name_components.push_back ({previous_len, idx});
3658 /* Skip the '::'. */
3659 current_len += 2;
3660 previous_len = current_len;
3661 }
3662 }
3663 else
3664 {
3665 /* Handle the Ada encoded (aka mangled) form here. */
3666 for (const char *iter = strstr (name, "__");
3667 iter != nullptr;
3668 iter = strstr (iter, "__"))
3669 {
3670 this->name_components.push_back ({previous_len, idx});
3671 iter += 2;
3672 previous_len = iter - name;
3673 }
3674 }
3675
3676 this->name_components.push_back ({previous_len, idx});
3677 }
3678
3679 /* Sort name_components elements by name. */
3680 auto name_comp_compare = [&] (const name_component &left,
3681 const name_component &right)
3682 {
3683 const char *left_qualified
3684 = this->symbol_name_at (left.idx, per_objfile);
3685 const char *right_qualified
3686 = this->symbol_name_at (right.idx, per_objfile);
3687
3688 const char *left_name = left_qualified + left.name_offset;
3689 const char *right_name = right_qualified + right.name_offset;
3690
3691 return name_cmp (left_name, right_name) < 0;
3692 };
3693
3694 std::sort (this->name_components.begin (),
3695 this->name_components.end (),
3696 name_comp_compare);
3697 }
3698
3699 /* Helper for dw2_expand_symtabs_matching that works with a
3700 mapped_index_base instead of the containing objfile. This is split
3701 to a separate function in order to be able to unit test the
3702 name_components matching using a mock mapped_index_base. For each
3703 symbol name that matches, calls MATCH_CALLBACK, passing it the
3704 symbol's index in the mapped_index_base symbol table. */
3705
3706 static bool
3707 dw2_expand_symtabs_matching_symbol
3708 (mapped_index_base &index,
3709 const lookup_name_info &lookup_name_in,
3710 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3711 gdb::function_view<bool (offset_type)> match_callback,
3712 dwarf2_per_objfile *per_objfile)
3713 {
3714 lookup_name_info lookup_name_without_params
3715 = lookup_name_in.make_ignore_params ();
3716
3717 /* Build the symbol name component sorted vector, if we haven't
3718 yet. */
3719 index.build_name_components (per_objfile);
3720
3721 /* The same symbol may appear more than once in the range though.
3722 E.g., if we're looking for symbols that complete "w", and we have
3723 a symbol named "w1::w2", we'll find the two name components for
3724 that same symbol in the range. To be sure we only call the
3725 callback once per symbol, we first collect the symbol name
3726 indexes that matched in a temporary vector and ignore
3727 duplicates. */
3728 std::vector<offset_type> matches;
3729
3730 struct name_and_matcher
3731 {
3732 symbol_name_matcher_ftype *matcher;
3733 const char *name;
3734
3735 bool operator== (const name_and_matcher &other) const
3736 {
3737 return matcher == other.matcher && strcmp (name, other.name) == 0;
3738 }
3739 };
3740
3741 /* A vector holding all the different symbol name matchers, for all
3742 languages. */
3743 std::vector<name_and_matcher> matchers;
3744
3745 for (int i = 0; i < nr_languages; i++)
3746 {
3747 enum language lang_e = (enum language) i;
3748
3749 const language_defn *lang = language_def (lang_e);
3750 symbol_name_matcher_ftype *name_matcher
3751 = lang->get_symbol_name_matcher (lookup_name_without_params);
3752
3753 name_and_matcher key {
3754 name_matcher,
3755 lookup_name_without_params.language_lookup_name (lang_e)
3756 };
3757
3758 /* Don't insert the same comparison routine more than once.
3759 Note that we do this linear walk. This is not a problem in
3760 practice because the number of supported languages is
3761 low. */
3762 if (std::find (matchers.begin (), matchers.end (), key)
3763 != matchers.end ())
3764 continue;
3765 matchers.push_back (std::move (key));
3766
3767 auto bounds
3768 = index.find_name_components_bounds (lookup_name_without_params,
3769 lang_e, per_objfile);
3770
3771 /* Now for each symbol name in range, check to see if we have a name
3772 match, and if so, call the MATCH_CALLBACK callback. */
3773
3774 for (; bounds.first != bounds.second; ++bounds.first)
3775 {
3776 const char *qualified
3777 = index.symbol_name_at (bounds.first->idx, per_objfile);
3778
3779 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3780 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3781 continue;
3782
3783 matches.push_back (bounds.first->idx);
3784 }
3785 }
3786
3787 std::sort (matches.begin (), matches.end ());
3788
3789 /* Finally call the callback, once per match. */
3790 ULONGEST prev = -1;
3791 bool result = true;
3792 for (offset_type idx : matches)
3793 {
3794 if (prev != idx)
3795 {
3796 if (!match_callback (idx))
3797 {
3798 result = false;
3799 break;
3800 }
3801 prev = idx;
3802 }
3803 }
3804
3805 /* Above we use a type wider than idx's for 'prev', since 0 and
3806 (offset_type)-1 are both possible values. */
3807 static_assert (sizeof (prev) > sizeof (offset_type), "");
3808
3809 return result;
3810 }
3811
3812 #if GDB_SELF_TEST
3813
3814 namespace selftests { namespace dw2_expand_symtabs_matching {
3815
3816 /* A mock .gdb_index/.debug_names-like name index table, enough to
3817 exercise dw2_expand_symtabs_matching_symbol, which works with the
3818 mapped_index_base interface. Builds an index from the symbol list
3819 passed as parameter to the constructor. */
3820 class mock_mapped_index : public mapped_index_base
3821 {
3822 public:
3823 mock_mapped_index (gdb::array_view<const char *> symbols)
3824 : m_symbol_table (symbols)
3825 {}
3826
3827 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3828
3829 /* Return the number of names in the symbol table. */
3830 size_t symbol_name_count () const override
3831 {
3832 return m_symbol_table.size ();
3833 }
3834
3835 /* Get the name of the symbol at IDX in the symbol table. */
3836 const char *symbol_name_at
3837 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3838 {
3839 return m_symbol_table[idx];
3840 }
3841
3842 private:
3843 gdb::array_view<const char *> m_symbol_table;
3844 };
3845
3846 /* Convenience function that converts a NULL pointer to a "<null>"
3847 string, to pass to print routines. */
3848
3849 static const char *
3850 string_or_null (const char *str)
3851 {
3852 return str != NULL ? str : "<null>";
3853 }
3854
3855 /* Check if a lookup_name_info built from
3856 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3857 index. EXPECTED_LIST is the list of expected matches, in expected
3858 matching order. If no match expected, then an empty list is
3859 specified. Returns true on success. On failure prints a warning
3860 indicating the file:line that failed, and returns false. */
3861
3862 static bool
3863 check_match (const char *file, int line,
3864 mock_mapped_index &mock_index,
3865 const char *name, symbol_name_match_type match_type,
3866 bool completion_mode,
3867 std::initializer_list<const char *> expected_list,
3868 dwarf2_per_objfile *per_objfile)
3869 {
3870 lookup_name_info lookup_name (name, match_type, completion_mode);
3871
3872 bool matched = true;
3873
3874 auto mismatch = [&] (const char *expected_str,
3875 const char *got)
3876 {
3877 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3878 "expected=\"%s\", got=\"%s\"\n"),
3879 file, line,
3880 (match_type == symbol_name_match_type::FULL
3881 ? "FULL" : "WILD"),
3882 name, string_or_null (expected_str), string_or_null (got));
3883 matched = false;
3884 };
3885
3886 auto expected_it = expected_list.begin ();
3887 auto expected_end = expected_list.end ();
3888
3889 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3890 nullptr,
3891 [&] (offset_type idx)
3892 {
3893 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3894 const char *expected_str
3895 = expected_it == expected_end ? NULL : *expected_it++;
3896
3897 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3898 mismatch (expected_str, matched_name);
3899 return true;
3900 }, per_objfile);
3901
3902 const char *expected_str
3903 = expected_it == expected_end ? NULL : *expected_it++;
3904 if (expected_str != NULL)
3905 mismatch (expected_str, NULL);
3906
3907 return matched;
3908 }
3909
3910 /* The symbols added to the mock mapped_index for testing (in
3911 canonical form). */
3912 static const char *test_symbols[] = {
3913 "function",
3914 "std::bar",
3915 "std::zfunction",
3916 "std::zfunction2",
3917 "w1::w2",
3918 "ns::foo<char*>",
3919 "ns::foo<int>",
3920 "ns::foo<long>",
3921 "ns2::tmpl<int>::foo2",
3922 "(anonymous namespace)::A::B::C",
3923
3924 /* These are used to check that the increment-last-char in the
3925 matching algorithm for completion doesn't match "t1_fund" when
3926 completing "t1_func". */
3927 "t1_func",
3928 "t1_func1",
3929 "t1_fund",
3930 "t1_fund1",
3931
3932 /* A UTF-8 name with multi-byte sequences to make sure that
3933 cp-name-parser understands this as a single identifier ("função"
3934 is "function" in PT). */
3935 u8"u8função",
3936
3937 /* \377 (0xff) is Latin1 'ÿ'. */
3938 "yfunc\377",
3939
3940 /* \377 (0xff) is Latin1 'ÿ'. */
3941 "\377",
3942 "\377\377123",
3943
3944 /* A name with all sorts of complications. Starts with "z" to make
3945 it easier for the completion tests below. */
3946 #define Z_SYM_NAME \
3947 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3948 "::tuple<(anonymous namespace)::ui*, " \
3949 "std::default_delete<(anonymous namespace)::ui>, void>"
3950
3951 Z_SYM_NAME
3952 };
3953
3954 /* Returns true if the mapped_index_base::find_name_component_bounds
3955 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3956 in completion mode. */
3957
3958 static bool
3959 check_find_bounds_finds (mapped_index_base &index,
3960 const char *search_name,
3961 gdb::array_view<const char *> expected_syms,
3962 dwarf2_per_objfile *per_objfile)
3963 {
3964 lookup_name_info lookup_name (search_name,
3965 symbol_name_match_type::FULL, true);
3966
3967 auto bounds = index.find_name_components_bounds (lookup_name,
3968 language_cplus,
3969 per_objfile);
3970
3971 size_t distance = std::distance (bounds.first, bounds.second);
3972 if (distance != expected_syms.size ())
3973 return false;
3974
3975 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3976 {
3977 auto nc_elem = bounds.first + exp_elem;
3978 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3979 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3980 return false;
3981 }
3982
3983 return true;
3984 }
3985
3986 /* Test the lower-level mapped_index::find_name_component_bounds
3987 method. */
3988
3989 static void
3990 test_mapped_index_find_name_component_bounds ()
3991 {
3992 mock_mapped_index mock_index (test_symbols);
3993
3994 mock_index.build_name_components (NULL /* per_objfile */);
3995
3996 /* Test the lower-level mapped_index::find_name_component_bounds
3997 method in completion mode. */
3998 {
3999 static const char *expected_syms[] = {
4000 "t1_func",
4001 "t1_func1",
4002 };
4003
4004 SELF_CHECK (check_find_bounds_finds
4005 (mock_index, "t1_func", expected_syms,
4006 NULL /* per_objfile */));
4007 }
4008
4009 /* Check that the increment-last-char in the name matching algorithm
4010 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4011 {
4012 static const char *expected_syms1[] = {
4013 "\377",
4014 "\377\377123",
4015 };
4016 SELF_CHECK (check_find_bounds_finds
4017 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4018
4019 static const char *expected_syms2[] = {
4020 "\377\377123",
4021 };
4022 SELF_CHECK (check_find_bounds_finds
4023 (mock_index, "\377\377", expected_syms2,
4024 NULL /* per_objfile */));
4025 }
4026 }
4027
4028 /* Test dw2_expand_symtabs_matching_symbol. */
4029
4030 static void
4031 test_dw2_expand_symtabs_matching_symbol ()
4032 {
4033 mock_mapped_index mock_index (test_symbols);
4034
4035 /* We let all tests run until the end even if some fails, for debug
4036 convenience. */
4037 bool any_mismatch = false;
4038
4039 /* Create the expected symbols list (an initializer_list). Needed
4040 because lists have commas, and we need to pass them to CHECK,
4041 which is a macro. */
4042 #define EXPECT(...) { __VA_ARGS__ }
4043
4044 /* Wrapper for check_match that passes down the current
4045 __FILE__/__LINE__. */
4046 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4047 any_mismatch |= !check_match (__FILE__, __LINE__, \
4048 mock_index, \
4049 NAME, MATCH_TYPE, COMPLETION_MODE, \
4050 EXPECTED_LIST, NULL)
4051
4052 /* Identity checks. */
4053 for (const char *sym : test_symbols)
4054 {
4055 /* Should be able to match all existing symbols. */
4056 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4057 EXPECT (sym));
4058
4059 /* Should be able to match all existing symbols with
4060 parameters. */
4061 std::string with_params = std::string (sym) + "(int)";
4062 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4063 EXPECT (sym));
4064
4065 /* Should be able to match all existing symbols with
4066 parameters and qualifiers. */
4067 with_params = std::string (sym) + " ( int ) const";
4068 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4069 EXPECT (sym));
4070
4071 /* This should really find sym, but cp-name-parser.y doesn't
4072 know about lvalue/rvalue qualifiers yet. */
4073 with_params = std::string (sym) + " ( int ) &&";
4074 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4075 {});
4076 }
4077
4078 /* Check that the name matching algorithm for completion doesn't get
4079 confused with Latin1 'ÿ' / 0xff. */
4080 {
4081 static const char str[] = "\377";
4082 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4083 EXPECT ("\377", "\377\377123"));
4084 }
4085
4086 /* Check that the increment-last-char in the matching algorithm for
4087 completion doesn't match "t1_fund" when completing "t1_func". */
4088 {
4089 static const char str[] = "t1_func";
4090 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4091 EXPECT ("t1_func", "t1_func1"));
4092 }
4093
4094 /* Check that completion mode works at each prefix of the expected
4095 symbol name. */
4096 {
4097 static const char str[] = "function(int)";
4098 size_t len = strlen (str);
4099 std::string lookup;
4100
4101 for (size_t i = 1; i < len; i++)
4102 {
4103 lookup.assign (str, i);
4104 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4105 EXPECT ("function"));
4106 }
4107 }
4108
4109 /* While "w" is a prefix of both components, the match function
4110 should still only be called once. */
4111 {
4112 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4113 EXPECT ("w1::w2"));
4114 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4115 EXPECT ("w1::w2"));
4116 }
4117
4118 /* Same, with a "complicated" symbol. */
4119 {
4120 static const char str[] = Z_SYM_NAME;
4121 size_t len = strlen (str);
4122 std::string lookup;
4123
4124 for (size_t i = 1; i < len; i++)
4125 {
4126 lookup.assign (str, i);
4127 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4128 EXPECT (Z_SYM_NAME));
4129 }
4130 }
4131
4132 /* In FULL mode, an incomplete symbol doesn't match. */
4133 {
4134 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4135 {});
4136 }
4137
4138 /* A complete symbol with parameters matches any overload, since the
4139 index has no overload info. */
4140 {
4141 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4142 EXPECT ("std::zfunction", "std::zfunction2"));
4143 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4144 EXPECT ("std::zfunction", "std::zfunction2"));
4145 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4146 EXPECT ("std::zfunction", "std::zfunction2"));
4147 }
4148
4149 /* Check that whitespace is ignored appropriately. A symbol with a
4150 template argument list. */
4151 {
4152 static const char expected[] = "ns::foo<int>";
4153 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4154 EXPECT (expected));
4155 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4156 EXPECT (expected));
4157 }
4158
4159 /* Check that whitespace is ignored appropriately. A symbol with a
4160 template argument list that includes a pointer. */
4161 {
4162 static const char expected[] = "ns::foo<char*>";
4163 /* Try both completion and non-completion modes. */
4164 static const bool completion_mode[2] = {false, true};
4165 for (size_t i = 0; i < 2; i++)
4166 {
4167 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4168 completion_mode[i], EXPECT (expected));
4169 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4170 completion_mode[i], EXPECT (expected));
4171
4172 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4173 completion_mode[i], EXPECT (expected));
4174 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4175 completion_mode[i], EXPECT (expected));
4176 }
4177 }
4178
4179 {
4180 /* Check method qualifiers are ignored. */
4181 static const char expected[] = "ns::foo<char*>";
4182 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4183 symbol_name_match_type::FULL, true, EXPECT (expected));
4184 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4185 symbol_name_match_type::FULL, true, EXPECT (expected));
4186 CHECK_MATCH ("foo < char * > ( int ) const",
4187 symbol_name_match_type::WILD, true, EXPECT (expected));
4188 CHECK_MATCH ("foo < char * > ( int ) &&",
4189 symbol_name_match_type::WILD, true, EXPECT (expected));
4190 }
4191
4192 /* Test lookup names that don't match anything. */
4193 {
4194 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4195 {});
4196
4197 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4198 {});
4199 }
4200
4201 /* Some wild matching tests, exercising "(anonymous namespace)",
4202 which should not be confused with a parameter list. */
4203 {
4204 static const char *syms[] = {
4205 "A::B::C",
4206 "B::C",
4207 "C",
4208 "A :: B :: C ( int )",
4209 "B :: C ( int )",
4210 "C ( int )",
4211 };
4212
4213 for (const char *s : syms)
4214 {
4215 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4216 EXPECT ("(anonymous namespace)::A::B::C"));
4217 }
4218 }
4219
4220 {
4221 static const char expected[] = "ns2::tmpl<int>::foo2";
4222 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4223 EXPECT (expected));
4224 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4225 EXPECT (expected));
4226 }
4227
4228 SELF_CHECK (!any_mismatch);
4229
4230 #undef EXPECT
4231 #undef CHECK_MATCH
4232 }
4233
4234 static void
4235 run_test ()
4236 {
4237 test_mapped_index_find_name_component_bounds ();
4238 test_dw2_expand_symtabs_matching_symbol ();
4239 }
4240
4241 }} // namespace selftests::dw2_expand_symtabs_matching
4242
4243 #endif /* GDB_SELF_TEST */
4244
4245 /* If FILE_MATCHER is NULL or if PER_CU has
4246 dwarf2_per_cu_quick_data::MARK set (see
4247 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4248 EXPANSION_NOTIFY on it. */
4249
4250 static bool
4251 dw2_expand_symtabs_matching_one
4252 (dwarf2_per_cu_data *per_cu,
4253 dwarf2_per_objfile *per_objfile,
4254 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4255 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4256 {
4257 if (file_matcher == NULL || per_cu->v.quick->mark)
4258 {
4259 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4260
4261 compunit_symtab *symtab
4262 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4263 gdb_assert (symtab != nullptr);
4264
4265 if (expansion_notify != NULL && symtab_was_null)
4266 return expansion_notify (symtab);
4267 }
4268 return true;
4269 }
4270
4271 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4272 matched, to expand corresponding CUs that were marked. IDX is the
4273 index of the symbol name that matched. */
4274
4275 static bool
4276 dw2_expand_marked_cus
4277 (dwarf2_per_objfile *per_objfile, offset_type idx,
4278 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4279 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4280 block_search_flags search_flags,
4281 search_domain kind)
4282 {
4283 offset_type vec_len, vec_idx;
4284 bool global_seen = false;
4285 mapped_index &index = *per_objfile->per_bfd->index_table;
4286
4287 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4288 vec_len = vec[0];
4289 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4290 {
4291 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4292 /* This value is only valid for index versions >= 7. */
4293 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4294 gdb_index_symbol_kind symbol_kind =
4295 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4296 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4297 /* Only check the symbol attributes if they're present.
4298 Indices prior to version 7 don't record them,
4299 and indices >= 7 may elide them for certain symbols
4300 (gold does this). */
4301 int attrs_valid =
4302 (index.version >= 7
4303 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4304
4305 /* Work around gold/15646. */
4306 if (attrs_valid
4307 && !is_static
4308 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4309 {
4310 if (global_seen)
4311 continue;
4312
4313 global_seen = true;
4314 }
4315
4316 /* Only check the symbol's kind if it has one. */
4317 if (attrs_valid)
4318 {
4319 if (is_static)
4320 {
4321 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4322 continue;
4323 }
4324 else
4325 {
4326 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4327 continue;
4328 }
4329
4330 switch (kind)
4331 {
4332 case VARIABLES_DOMAIN:
4333 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4334 continue;
4335 break;
4336 case FUNCTIONS_DOMAIN:
4337 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4338 continue;
4339 break;
4340 case TYPES_DOMAIN:
4341 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4342 continue;
4343 break;
4344 case MODULES_DOMAIN:
4345 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4346 continue;
4347 break;
4348 default:
4349 break;
4350 }
4351 }
4352
4353 /* Don't crash on bad data. */
4354 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4355 {
4356 complaint (_(".gdb_index entry has bad CU index"
4357 " [in module %s]"), objfile_name (per_objfile->objfile));
4358 continue;
4359 }
4360
4361 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4362 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4363 expansion_notify))
4364 return false;
4365 }
4366
4367 return true;
4368 }
4369
4370 /* If FILE_MATCHER is non-NULL, set all the
4371 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4372 that match FILE_MATCHER. */
4373
4374 static void
4375 dw_expand_symtabs_matching_file_matcher
4376 (dwarf2_per_objfile *per_objfile,
4377 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4378 {
4379 if (file_matcher == NULL)
4380 return;
4381
4382 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4383 htab_eq_pointer,
4384 NULL, xcalloc, xfree));
4385 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4386 htab_eq_pointer,
4387 NULL, xcalloc, xfree));
4388
4389 /* The rule is CUs specify all the files, including those used by
4390 any TU, so there's no need to scan TUs here. */
4391
4392 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4393 {
4394 QUIT;
4395
4396 if (per_cu->is_debug_types)
4397 continue;
4398 per_cu->v.quick->mark = 0;
4399
4400 /* We only need to look at symtabs not already expanded. */
4401 if (per_objfile->symtab_set_p (per_cu.get ()))
4402 continue;
4403
4404 if (per_cu->fnd != nullptr)
4405 {
4406 file_and_directory *fnd = per_cu->fnd.get ();
4407
4408 if (file_matcher (fnd->get_name (), false))
4409 {
4410 per_cu->v.quick->mark = 1;
4411 continue;
4412 }
4413
4414 /* Before we invoke realpath, which can get expensive when many
4415 files are involved, do a quick comparison of the basenames. */
4416 if ((basenames_may_differ
4417 || file_matcher (lbasename (fnd->get_name ()), true))
4418 && file_matcher (fnd->get_fullname (), false))
4419 {
4420 per_cu->v.quick->mark = 1;
4421 continue;
4422 }
4423 }
4424
4425 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4426 per_objfile);
4427 if (file_data == NULL)
4428 continue;
4429
4430 if (htab_find (visited_not_found.get (), file_data) != NULL)
4431 continue;
4432 else if (htab_find (visited_found.get (), file_data) != NULL)
4433 {
4434 per_cu->v.quick->mark = 1;
4435 continue;
4436 }
4437
4438 for (int j = 0; j < file_data->num_file_names; ++j)
4439 {
4440 const char *this_real_name;
4441
4442 if (file_matcher (file_data->file_names[j], false))
4443 {
4444 per_cu->v.quick->mark = 1;
4445 break;
4446 }
4447
4448 /* Before we invoke realpath, which can get expensive when many
4449 files are involved, do a quick comparison of the basenames. */
4450 if (!basenames_may_differ
4451 && !file_matcher (lbasename (file_data->file_names[j]),
4452 true))
4453 continue;
4454
4455 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4456 if (file_matcher (this_real_name, false))
4457 {
4458 per_cu->v.quick->mark = 1;
4459 break;
4460 }
4461 }
4462
4463 void **slot = htab_find_slot (per_cu->v.quick->mark
4464 ? visited_found.get ()
4465 : visited_not_found.get (),
4466 file_data, INSERT);
4467 *slot = file_data;
4468 }
4469 }
4470
4471 bool
4472 dwarf2_gdb_index::expand_symtabs_matching
4473 (struct objfile *objfile,
4474 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4475 const lookup_name_info *lookup_name,
4476 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 block_search_flags search_flags,
4479 domain_enum domain,
4480 enum search_domain kind)
4481 {
4482 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4483
4484 /* index_table is NULL if OBJF_READNOW. */
4485 if (!per_objfile->per_bfd->index_table)
4486 return true;
4487
4488 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4489
4490 /* This invariant is documented in quick-functions.h. */
4491 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4492 if (lookup_name == nullptr)
4493 {
4494 for (dwarf2_per_cu_data *per_cu
4495 : all_comp_units_range (per_objfile->per_bfd))
4496 {
4497 QUIT;
4498
4499 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4500 file_matcher,
4501 expansion_notify))
4502 return false;
4503 }
4504 return true;
4505 }
4506
4507 mapped_index &index = *per_objfile->per_bfd->index_table;
4508
4509 bool result
4510 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4511 symbol_matcher,
4512 [&] (offset_type idx)
4513 {
4514 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4515 expansion_notify, search_flags, kind))
4516 return false;
4517 return true;
4518 }, per_objfile);
4519
4520 return result;
4521 }
4522
4523 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4524 symtab. */
4525
4526 static struct compunit_symtab *
4527 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4528 CORE_ADDR pc)
4529 {
4530 int i;
4531
4532 if (cust->blockvector () != nullptr
4533 && blockvector_contains_pc (cust->blockvector (), pc))
4534 return cust;
4535
4536 if (cust->includes == NULL)
4537 return NULL;
4538
4539 for (i = 0; cust->includes[i]; ++i)
4540 {
4541 struct compunit_symtab *s = cust->includes[i];
4542
4543 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4544 if (s != NULL)
4545 return s;
4546 }
4547
4548 return NULL;
4549 }
4550
4551 struct compunit_symtab *
4552 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4553 (struct objfile *objfile,
4554 struct bound_minimal_symbol msymbol,
4555 CORE_ADDR pc,
4556 struct obj_section *section,
4557 int warn_if_readin)
4558 {
4559 struct dwarf2_per_cu_data *data;
4560 struct compunit_symtab *result;
4561
4562 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4563 if (per_objfile->per_bfd->index_addrmap == nullptr)
4564 return NULL;
4565
4566 CORE_ADDR baseaddr = objfile->text_section_offset ();
4567 data = ((struct dwarf2_per_cu_data *)
4568 addrmap_find (per_objfile->per_bfd->index_addrmap,
4569 pc - baseaddr));
4570 if (!data)
4571 return NULL;
4572
4573 if (warn_if_readin && per_objfile->symtab_set_p (data))
4574 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4575 paddress (objfile->arch (), pc));
4576
4577 result = recursively_find_pc_sect_compunit_symtab
4578 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4579
4580 gdb_assert (result != NULL);
4581 return result;
4582 }
4583
4584 void
4585 dwarf2_base_index_functions::map_symbol_filenames
4586 (struct objfile *objfile,
4587 gdb::function_view<symbol_filename_ftype> fun,
4588 bool need_fullname)
4589 {
4590 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4591
4592 /* Use caches to ensure we only call FUN once for each filename. */
4593 filename_seen_cache filenames_cache;
4594 std::unordered_set<quick_file_names *> qfn_cache;
4595
4596 /* The rule is CUs specify all the files, including those used by any TU,
4597 so there's no need to scan TUs here. We can ignore file names coming
4598 from already-expanded CUs. It is possible that an expanded CU might
4599 reuse the file names data from a currently unexpanded CU, in this
4600 case we don't want to report the files from the unexpanded CU. */
4601
4602 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4603 {
4604 if (!per_cu->is_debug_types
4605 && per_objfile->symtab_set_p (per_cu.get ()))
4606 {
4607 if (per_cu->v.quick->file_names != nullptr)
4608 qfn_cache.insert (per_cu->v.quick->file_names);
4609 }
4610 }
4611
4612 for (dwarf2_per_cu_data *per_cu
4613 : all_comp_units_range (per_objfile->per_bfd))
4614 {
4615 /* We only need to look at symtabs not already expanded. */
4616 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4617 continue;
4618
4619 if (per_cu->fnd != nullptr)
4620 {
4621 file_and_directory *fnd = per_cu->fnd.get ();
4622
4623 const char *filename = fnd->get_name ();
4624 const char *key = filename;
4625 const char *fullname = nullptr;
4626
4627 if (need_fullname)
4628 {
4629 fullname = fnd->get_fullname ();
4630 key = fullname;
4631 }
4632
4633 if (!filenames_cache.seen (key))
4634 fun (filename, fullname);
4635 }
4636
4637 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4638 if (file_data == nullptr
4639 || qfn_cache.find (file_data) != qfn_cache.end ())
4640 continue;
4641
4642 for (int j = 0; j < file_data->num_file_names; ++j)
4643 {
4644 const char *filename = file_data->file_names[j];
4645 const char *key = filename;
4646 const char *fullname = nullptr;
4647
4648 if (need_fullname)
4649 {
4650 fullname = dw2_get_real_path (per_objfile, file_data, j);
4651 key = fullname;
4652 }
4653
4654 if (!filenames_cache.seen (key))
4655 fun (filename, fullname);
4656 }
4657 }
4658 }
4659
4660 bool
4661 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4662 {
4663 return true;
4664 }
4665
4666 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4667
4668 bool
4669 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4670 {
4671 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4672
4673 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4674 {
4675 /* Is this already expanded? */
4676 if (per_objfile->symtab_set_p (per_cu.get ()))
4677 continue;
4678
4679 /* It has not yet been expanded. */
4680 return true;
4681 }
4682
4683 return false;
4684 }
4685
4686 /* DWARF-5 debug_names reader. */
4687
4688 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4689 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4690
4691 /* A helper function that reads the .debug_names section in SECTION
4692 and fills in MAP. FILENAME is the name of the file containing the
4693 section; it is used for error reporting.
4694
4695 Returns true if all went well, false otherwise. */
4696
4697 static bool
4698 read_debug_names_from_section (struct objfile *objfile,
4699 const char *filename,
4700 struct dwarf2_section_info *section,
4701 mapped_debug_names &map)
4702 {
4703 if (section->empty ())
4704 return false;
4705
4706 /* Older elfutils strip versions could keep the section in the main
4707 executable while splitting it for the separate debug info file. */
4708 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4709 return false;
4710
4711 section->read (objfile);
4712
4713 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4714
4715 const gdb_byte *addr = section->buffer;
4716
4717 bfd *const abfd = section->get_bfd_owner ();
4718
4719 unsigned int bytes_read;
4720 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4721 addr += bytes_read;
4722
4723 map.dwarf5_is_dwarf64 = bytes_read != 4;
4724 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4725 if (bytes_read + length != section->size)
4726 {
4727 /* There may be multiple per-CU indices. */
4728 warning (_("Section .debug_names in %s length %s does not match "
4729 "section length %s, ignoring .debug_names."),
4730 filename, plongest (bytes_read + length),
4731 pulongest (section->size));
4732 return false;
4733 }
4734
4735 /* The version number. */
4736 uint16_t version = read_2_bytes (abfd, addr);
4737 addr += 2;
4738 if (version != 5)
4739 {
4740 warning (_("Section .debug_names in %s has unsupported version %d, "
4741 "ignoring .debug_names."),
4742 filename, version);
4743 return false;
4744 }
4745
4746 /* Padding. */
4747 uint16_t padding = read_2_bytes (abfd, addr);
4748 addr += 2;
4749 if (padding != 0)
4750 {
4751 warning (_("Section .debug_names in %s has unsupported padding %d, "
4752 "ignoring .debug_names."),
4753 filename, padding);
4754 return false;
4755 }
4756
4757 /* comp_unit_count - The number of CUs in the CU list. */
4758 map.cu_count = read_4_bytes (abfd, addr);
4759 addr += 4;
4760
4761 /* local_type_unit_count - The number of TUs in the local TU
4762 list. */
4763 map.tu_count = read_4_bytes (abfd, addr);
4764 addr += 4;
4765
4766 /* foreign_type_unit_count - The number of TUs in the foreign TU
4767 list. */
4768 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4769 addr += 4;
4770 if (foreign_tu_count != 0)
4771 {
4772 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4773 "ignoring .debug_names."),
4774 filename, static_cast<unsigned long> (foreign_tu_count));
4775 return false;
4776 }
4777
4778 /* bucket_count - The number of hash buckets in the hash lookup
4779 table. */
4780 map.bucket_count = read_4_bytes (abfd, addr);
4781 addr += 4;
4782
4783 /* name_count - The number of unique names in the index. */
4784 map.name_count = read_4_bytes (abfd, addr);
4785 addr += 4;
4786
4787 /* abbrev_table_size - The size in bytes of the abbreviations
4788 table. */
4789 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4790 addr += 4;
4791
4792 /* augmentation_string_size - The size in bytes of the augmentation
4793 string. This value is rounded up to a multiple of 4. */
4794 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4795 addr += 4;
4796 map.augmentation_is_gdb = ((augmentation_string_size
4797 == sizeof (dwarf5_augmentation))
4798 && memcmp (addr, dwarf5_augmentation,
4799 sizeof (dwarf5_augmentation)) == 0);
4800 augmentation_string_size += (-augmentation_string_size) & 3;
4801 addr += augmentation_string_size;
4802
4803 /* List of CUs */
4804 map.cu_table_reordered = addr;
4805 addr += map.cu_count * map.offset_size;
4806
4807 /* List of Local TUs */
4808 map.tu_table_reordered = addr;
4809 addr += map.tu_count * map.offset_size;
4810
4811 /* Hash Lookup Table */
4812 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4813 addr += map.bucket_count * 4;
4814 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4815 addr += map.name_count * 4;
4816
4817 /* Name Table */
4818 map.name_table_string_offs_reordered = addr;
4819 addr += map.name_count * map.offset_size;
4820 map.name_table_entry_offs_reordered = addr;
4821 addr += map.name_count * map.offset_size;
4822
4823 const gdb_byte *abbrev_table_start = addr;
4824 for (;;)
4825 {
4826 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4827 addr += bytes_read;
4828 if (index_num == 0)
4829 break;
4830
4831 const auto insertpair
4832 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4833 if (!insertpair.second)
4834 {
4835 warning (_("Section .debug_names in %s has duplicate index %s, "
4836 "ignoring .debug_names."),
4837 filename, pulongest (index_num));
4838 return false;
4839 }
4840 mapped_debug_names::index_val &indexval = insertpair.first->second;
4841 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4842 addr += bytes_read;
4843
4844 for (;;)
4845 {
4846 mapped_debug_names::index_val::attr attr;
4847 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4848 addr += bytes_read;
4849 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4850 addr += bytes_read;
4851 if (attr.form == DW_FORM_implicit_const)
4852 {
4853 attr.implicit_const = read_signed_leb128 (abfd, addr,
4854 &bytes_read);
4855 addr += bytes_read;
4856 }
4857 if (attr.dw_idx == 0 && attr.form == 0)
4858 break;
4859 indexval.attr_vec.push_back (std::move (attr));
4860 }
4861 }
4862 if (addr != abbrev_table_start + abbrev_table_size)
4863 {
4864 warning (_("Section .debug_names in %s has abbreviation_table "
4865 "of size %s vs. written as %u, ignoring .debug_names."),
4866 filename, plongest (addr - abbrev_table_start),
4867 abbrev_table_size);
4868 return false;
4869 }
4870 map.entry_pool = addr;
4871
4872 return true;
4873 }
4874
4875 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4876 list. */
4877
4878 static void
4879 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4880 const mapped_debug_names &map,
4881 dwarf2_section_info &section,
4882 bool is_dwz)
4883 {
4884 if (!map.augmentation_is_gdb)
4885 {
4886 for (uint32_t i = 0; i < map.cu_count; ++i)
4887 {
4888 sect_offset sect_off
4889 = (sect_offset) (extract_unsigned_integer
4890 (map.cu_table_reordered + i * map.offset_size,
4891 map.offset_size,
4892 map.dwarf5_byte_order));
4893 /* We don't know the length of the CU, because the CU list in a
4894 .debug_names index can be incomplete, so we can't use the start
4895 of the next CU as end of this CU. We create the CUs here with
4896 length 0, and in cutu_reader::cutu_reader we'll fill in the
4897 actual length. */
4898 dwarf2_per_cu_data_up per_cu
4899 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4900 sect_off, 0);
4901 per_bfd->all_comp_units.push_back (std::move (per_cu));
4902 }
4903 return;
4904 }
4905
4906 sect_offset sect_off_prev;
4907 for (uint32_t i = 0; i <= map.cu_count; ++i)
4908 {
4909 sect_offset sect_off_next;
4910 if (i < map.cu_count)
4911 {
4912 sect_off_next
4913 = (sect_offset) (extract_unsigned_integer
4914 (map.cu_table_reordered + i * map.offset_size,
4915 map.offset_size,
4916 map.dwarf5_byte_order));
4917 }
4918 else
4919 sect_off_next = (sect_offset) section.size;
4920 if (i >= 1)
4921 {
4922 const ULONGEST length = sect_off_next - sect_off_prev;
4923 dwarf2_per_cu_data_up per_cu
4924 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4925 sect_off_prev, length);
4926 per_bfd->all_comp_units.push_back (std::move (per_cu));
4927 }
4928 sect_off_prev = sect_off_next;
4929 }
4930 }
4931
4932 /* Read the CU list from the mapped index, and use it to create all
4933 the CU objects for this dwarf2_per_objfile. */
4934
4935 static void
4936 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4937 const mapped_debug_names &map,
4938 const mapped_debug_names &dwz_map)
4939 {
4940 gdb_assert (per_bfd->all_comp_units.empty ());
4941 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4942
4943 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4944 false /* is_dwz */);
4945
4946 if (dwz_map.cu_count == 0)
4947 return;
4948
4949 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4950 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4951 true /* is_dwz */);
4952 }
4953
4954 /* Read .debug_names. If everything went ok, initialize the "quick"
4955 elements of all the CUs and return true. Otherwise, return false. */
4956
4957 static bool
4958 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4959 {
4960 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4961 mapped_debug_names dwz_map;
4962 struct objfile *objfile = per_objfile->objfile;
4963 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4964
4965 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4966 &per_bfd->debug_names, *map))
4967 return false;
4968
4969 /* Don't use the index if it's empty. */
4970 if (map->name_count == 0)
4971 return false;
4972
4973 /* If there is a .dwz file, read it so we can get its CU list as
4974 well. */
4975 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4976 if (dwz != NULL)
4977 {
4978 if (!read_debug_names_from_section (objfile,
4979 bfd_get_filename (dwz->dwz_bfd.get ()),
4980 &dwz->debug_names, dwz_map))
4981 {
4982 warning (_("could not read '.debug_names' section from %s; skipping"),
4983 bfd_get_filename (dwz->dwz_bfd.get ()));
4984 return false;
4985 }
4986 }
4987
4988 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4989
4990 if (map->tu_count != 0)
4991 {
4992 /* We can only handle a single .debug_types when we have an
4993 index. */
4994 if (per_bfd->types.size () != 1)
4995 return false;
4996
4997 dwarf2_section_info *section = &per_bfd->types[0];
4998
4999 create_signatured_type_table_from_debug_names
5000 (per_objfile, *map, section, &per_bfd->abbrev);
5001 }
5002
5003 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5004
5005 per_bfd->debug_names_table = std::move (map);
5006 per_bfd->using_index = 1;
5007 per_bfd->quick_file_names_table =
5008 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5009
5010 return true;
5011 }
5012
5013 /* Type used to manage iterating over all CUs looking for a symbol for
5014 .debug_names. */
5015
5016 class dw2_debug_names_iterator
5017 {
5018 public:
5019 dw2_debug_names_iterator (const mapped_debug_names &map,
5020 block_search_flags block_index,
5021 domain_enum domain,
5022 const char *name, dwarf2_per_objfile *per_objfile)
5023 : m_map (map), m_block_index (block_index), m_domain (domain),
5024 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5025 m_per_objfile (per_objfile)
5026 {}
5027
5028 dw2_debug_names_iterator (const mapped_debug_names &map,
5029 search_domain search, uint32_t namei,
5030 dwarf2_per_objfile *per_objfile,
5031 domain_enum domain = UNDEF_DOMAIN)
5032 : m_map (map),
5033 m_domain (domain),
5034 m_search (search),
5035 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5036 m_per_objfile (per_objfile)
5037 {}
5038
5039 dw2_debug_names_iterator (const mapped_debug_names &map,
5040 block_search_flags block_index, domain_enum domain,
5041 uint32_t namei, dwarf2_per_objfile *per_objfile)
5042 : m_map (map), m_block_index (block_index), m_domain (domain),
5043 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5044 m_per_objfile (per_objfile)
5045 {}
5046
5047 /* Return the next matching CU or NULL if there are no more. */
5048 dwarf2_per_cu_data *next ();
5049
5050 private:
5051 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5052 const char *name,
5053 dwarf2_per_objfile *per_objfile);
5054 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5055 uint32_t namei,
5056 dwarf2_per_objfile *per_objfile);
5057
5058 /* The internalized form of .debug_names. */
5059 const mapped_debug_names &m_map;
5060
5061 /* Restrict the search to these blocks. */
5062 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5063 | SEARCH_STATIC_BLOCK);
5064
5065 /* The kind of symbol we're looking for. */
5066 const domain_enum m_domain = UNDEF_DOMAIN;
5067 const search_domain m_search = ALL_DOMAIN;
5068
5069 /* The list of CUs from the index entry of the symbol, or NULL if
5070 not found. */
5071 const gdb_byte *m_addr;
5072
5073 dwarf2_per_objfile *m_per_objfile;
5074 };
5075
5076 const char *
5077 mapped_debug_names::namei_to_name
5078 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5079 {
5080 const ULONGEST namei_string_offs
5081 = extract_unsigned_integer ((name_table_string_offs_reordered
5082 + namei * offset_size),
5083 offset_size,
5084 dwarf5_byte_order);
5085 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5086 }
5087
5088 /* Find a slot in .debug_names for the object named NAME. If NAME is
5089 found, return pointer to its pool data. If NAME cannot be found,
5090 return NULL. */
5091
5092 const gdb_byte *
5093 dw2_debug_names_iterator::find_vec_in_debug_names
5094 (const mapped_debug_names &map, const char *name,
5095 dwarf2_per_objfile *per_objfile)
5096 {
5097 int (*cmp) (const char *, const char *);
5098
5099 gdb::unique_xmalloc_ptr<char> without_params;
5100 if (current_language->la_language == language_cplus
5101 || current_language->la_language == language_fortran
5102 || current_language->la_language == language_d)
5103 {
5104 /* NAME is already canonical. Drop any qualifiers as
5105 .debug_names does not contain any. */
5106
5107 if (strchr (name, '(') != NULL)
5108 {
5109 without_params = cp_remove_params (name);
5110 if (without_params != NULL)
5111 name = without_params.get ();
5112 }
5113 }
5114
5115 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5116
5117 const uint32_t full_hash = dwarf5_djb_hash (name);
5118 uint32_t namei
5119 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5120 (map.bucket_table_reordered
5121 + (full_hash % map.bucket_count)), 4,
5122 map.dwarf5_byte_order);
5123 if (namei == 0)
5124 return NULL;
5125 --namei;
5126 if (namei >= map.name_count)
5127 {
5128 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5129 "[in module %s]"),
5130 namei, map.name_count,
5131 objfile_name (per_objfile->objfile));
5132 return NULL;
5133 }
5134
5135 for (;;)
5136 {
5137 const uint32_t namei_full_hash
5138 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5139 (map.hash_table_reordered + namei), 4,
5140 map.dwarf5_byte_order);
5141 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5142 return NULL;
5143
5144 if (full_hash == namei_full_hash)
5145 {
5146 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5147
5148 #if 0 /* An expensive sanity check. */
5149 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5150 {
5151 complaint (_("Wrong .debug_names hash for string at index %u "
5152 "[in module %s]"),
5153 namei, objfile_name (dwarf2_per_objfile->objfile));
5154 return NULL;
5155 }
5156 #endif
5157
5158 if (cmp (namei_string, name) == 0)
5159 {
5160 const ULONGEST namei_entry_offs
5161 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5162 + namei * map.offset_size),
5163 map.offset_size, map.dwarf5_byte_order);
5164 return map.entry_pool + namei_entry_offs;
5165 }
5166 }
5167
5168 ++namei;
5169 if (namei >= map.name_count)
5170 return NULL;
5171 }
5172 }
5173
5174 const gdb_byte *
5175 dw2_debug_names_iterator::find_vec_in_debug_names
5176 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5177 {
5178 if (namei >= map.name_count)
5179 {
5180 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5181 "[in module %s]"),
5182 namei, map.name_count,
5183 objfile_name (per_objfile->objfile));
5184 return NULL;
5185 }
5186
5187 const ULONGEST namei_entry_offs
5188 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5189 + namei * map.offset_size),
5190 map.offset_size, map.dwarf5_byte_order);
5191 return map.entry_pool + namei_entry_offs;
5192 }
5193
5194 /* See dw2_debug_names_iterator. */
5195
5196 dwarf2_per_cu_data *
5197 dw2_debug_names_iterator::next ()
5198 {
5199 if (m_addr == NULL)
5200 return NULL;
5201
5202 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5203 struct objfile *objfile = m_per_objfile->objfile;
5204 bfd *const abfd = objfile->obfd;
5205
5206 again:
5207
5208 unsigned int bytes_read;
5209 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5210 m_addr += bytes_read;
5211 if (abbrev == 0)
5212 return NULL;
5213
5214 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5215 if (indexval_it == m_map.abbrev_map.cend ())
5216 {
5217 complaint (_("Wrong .debug_names undefined abbrev code %s "
5218 "[in module %s]"),
5219 pulongest (abbrev), objfile_name (objfile));
5220 return NULL;
5221 }
5222 const mapped_debug_names::index_val &indexval = indexval_it->second;
5223 enum class symbol_linkage {
5224 unknown,
5225 static_,
5226 extern_,
5227 } symbol_linkage_ = symbol_linkage::unknown;
5228 dwarf2_per_cu_data *per_cu = NULL;
5229 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5230 {
5231 ULONGEST ull;
5232 switch (attr.form)
5233 {
5234 case DW_FORM_implicit_const:
5235 ull = attr.implicit_const;
5236 break;
5237 case DW_FORM_flag_present:
5238 ull = 1;
5239 break;
5240 case DW_FORM_udata:
5241 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5242 m_addr += bytes_read;
5243 break;
5244 case DW_FORM_ref4:
5245 ull = read_4_bytes (abfd, m_addr);
5246 m_addr += 4;
5247 break;
5248 case DW_FORM_ref8:
5249 ull = read_8_bytes (abfd, m_addr);
5250 m_addr += 8;
5251 break;
5252 case DW_FORM_ref_sig8:
5253 ull = read_8_bytes (abfd, m_addr);
5254 m_addr += 8;
5255 break;
5256 default:
5257 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5258 dwarf_form_name (attr.form),
5259 objfile_name (objfile));
5260 return NULL;
5261 }
5262 switch (attr.dw_idx)
5263 {
5264 case DW_IDX_compile_unit:
5265 /* Don't crash on bad data. */
5266 if (ull >= per_bfd->all_comp_units.size ())
5267 {
5268 complaint (_(".debug_names entry has bad CU index %s"
5269 " [in module %s]"),
5270 pulongest (ull),
5271 objfile_name (objfile));
5272 continue;
5273 }
5274 per_cu = per_bfd->get_cu (ull);
5275 break;
5276 case DW_IDX_type_unit:
5277 /* Don't crash on bad data. */
5278 if (ull >= per_bfd->tu_stats.nr_tus)
5279 {
5280 complaint (_(".debug_names entry has bad TU index %s"
5281 " [in module %s]"),
5282 pulongest (ull),
5283 objfile_name (objfile));
5284 continue;
5285 }
5286 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5287 break;
5288 case DW_IDX_die_offset:
5289 /* In a per-CU index (as opposed to a per-module index), index
5290 entries without CU attribute implicitly refer to the single CU. */
5291 if (per_cu == NULL)
5292 per_cu = per_bfd->get_cu (0);
5293 break;
5294 case DW_IDX_GNU_internal:
5295 if (!m_map.augmentation_is_gdb)
5296 break;
5297 symbol_linkage_ = symbol_linkage::static_;
5298 break;
5299 case DW_IDX_GNU_external:
5300 if (!m_map.augmentation_is_gdb)
5301 break;
5302 symbol_linkage_ = symbol_linkage::extern_;
5303 break;
5304 }
5305 }
5306
5307 /* Skip if already read in. */
5308 if (m_per_objfile->symtab_set_p (per_cu))
5309 goto again;
5310
5311 /* Check static vs global. */
5312 if (symbol_linkage_ != symbol_linkage::unknown)
5313 {
5314 if (symbol_linkage_ == symbol_linkage::static_)
5315 {
5316 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5317 goto again;
5318 }
5319 else
5320 {
5321 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5322 goto again;
5323 }
5324 }
5325
5326 /* Match dw2_symtab_iter_next, symbol_kind
5327 and debug_names::psymbol_tag. */
5328 switch (m_domain)
5329 {
5330 case VAR_DOMAIN:
5331 switch (indexval.dwarf_tag)
5332 {
5333 case DW_TAG_variable:
5334 case DW_TAG_subprogram:
5335 /* Some types are also in VAR_DOMAIN. */
5336 case DW_TAG_typedef:
5337 case DW_TAG_structure_type:
5338 break;
5339 default:
5340 goto again;
5341 }
5342 break;
5343 case STRUCT_DOMAIN:
5344 switch (indexval.dwarf_tag)
5345 {
5346 case DW_TAG_typedef:
5347 case DW_TAG_structure_type:
5348 break;
5349 default:
5350 goto again;
5351 }
5352 break;
5353 case LABEL_DOMAIN:
5354 switch (indexval.dwarf_tag)
5355 {
5356 case 0:
5357 case DW_TAG_variable:
5358 break;
5359 default:
5360 goto again;
5361 }
5362 break;
5363 case MODULE_DOMAIN:
5364 switch (indexval.dwarf_tag)
5365 {
5366 case DW_TAG_module:
5367 break;
5368 default:
5369 goto again;
5370 }
5371 break;
5372 default:
5373 break;
5374 }
5375
5376 /* Match dw2_expand_symtabs_matching, symbol_kind and
5377 debug_names::psymbol_tag. */
5378 switch (m_search)
5379 {
5380 case VARIABLES_DOMAIN:
5381 switch (indexval.dwarf_tag)
5382 {
5383 case DW_TAG_variable:
5384 break;
5385 default:
5386 goto again;
5387 }
5388 break;
5389 case FUNCTIONS_DOMAIN:
5390 switch (indexval.dwarf_tag)
5391 {
5392 case DW_TAG_subprogram:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case TYPES_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_typedef:
5402 case DW_TAG_structure_type:
5403 break;
5404 default:
5405 goto again;
5406 }
5407 break;
5408 case MODULES_DOMAIN:
5409 switch (indexval.dwarf_tag)
5410 {
5411 case DW_TAG_module:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 default:
5417 break;
5418 }
5419
5420 return per_cu;
5421 }
5422
5423 /* This dumps minimal information about .debug_names. It is called
5424 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5425 uses this to verify that .debug_names has been loaded. */
5426
5427 void
5428 dwarf2_debug_names_index::dump (struct objfile *objfile)
5429 {
5430 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5431
5432 gdb_assert (per_objfile->per_bfd->using_index);
5433 gdb_printf (".debug_names:");
5434 if (per_objfile->per_bfd->debug_names_table)
5435 gdb_printf (" exists\n");
5436 else
5437 gdb_printf (" faked for \"readnow\"\n");
5438 gdb_printf ("\n");
5439 }
5440
5441 void
5442 dwarf2_debug_names_index::expand_matching_symbols
5443 (struct objfile *objfile,
5444 const lookup_name_info &name, domain_enum domain,
5445 int global,
5446 symbol_compare_ftype *ordered_compare)
5447 {
5448 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5449
5450 /* debug_names_table is NULL if OBJF_READNOW. */
5451 if (!per_objfile->per_bfd->debug_names_table)
5452 return;
5453
5454 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5455 const block_search_flags block_flags
5456 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5457
5458 const char *match_name = name.ada ().lookup_name ().c_str ();
5459 auto matcher = [&] (const char *symname)
5460 {
5461 if (ordered_compare == nullptr)
5462 return true;
5463 return ordered_compare (symname, match_name) == 0;
5464 };
5465
5466 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5467 [&] (offset_type namei)
5468 {
5469 /* The name was matched, now expand corresponding CUs that were
5470 marked. */
5471 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5472 per_objfile);
5473
5474 struct dwarf2_per_cu_data *per_cu;
5475 while ((per_cu = iter.next ()) != NULL)
5476 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5477 nullptr);
5478 return true;
5479 }, per_objfile);
5480 }
5481
5482 bool
5483 dwarf2_debug_names_index::expand_symtabs_matching
5484 (struct objfile *objfile,
5485 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5486 const lookup_name_info *lookup_name,
5487 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5488 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5489 block_search_flags search_flags,
5490 domain_enum domain,
5491 enum search_domain kind)
5492 {
5493 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5494
5495 /* debug_names_table is NULL if OBJF_READNOW. */
5496 if (!per_objfile->per_bfd->debug_names_table)
5497 return true;
5498
5499 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5500
5501 /* This invariant is documented in quick-functions.h. */
5502 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5503 if (lookup_name == nullptr)
5504 {
5505 for (dwarf2_per_cu_data *per_cu
5506 : all_comp_units_range (per_objfile->per_bfd))
5507 {
5508 QUIT;
5509
5510 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5511 file_matcher,
5512 expansion_notify))
5513 return false;
5514 }
5515 return true;
5516 }
5517
5518 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5519
5520 bool result
5521 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5522 symbol_matcher,
5523 [&] (offset_type namei)
5524 {
5525 /* The name was matched, now expand corresponding CUs that were
5526 marked. */
5527 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5528
5529 struct dwarf2_per_cu_data *per_cu;
5530 while ((per_cu = iter.next ()) != NULL)
5531 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5532 file_matcher,
5533 expansion_notify))
5534 return false;
5535 return true;
5536 }, per_objfile);
5537
5538 return result;
5539 }
5540
5541 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5542 to either a dwarf2_per_bfd or dwz_file object. */
5543
5544 template <typename T>
5545 static gdb::array_view<const gdb_byte>
5546 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5547 {
5548 dwarf2_section_info *section = &section_owner->gdb_index;
5549
5550 if (section->empty ())
5551 return {};
5552
5553 /* Older elfutils strip versions could keep the section in the main
5554 executable while splitting it for the separate debug info file. */
5555 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5556 return {};
5557
5558 section->read (obj);
5559
5560 /* dwarf2_section_info::size is a bfd_size_type, while
5561 gdb::array_view works with size_t. On 32-bit hosts, with
5562 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5563 is 32-bit. So we need an explicit narrowing conversion here.
5564 This is fine, because it's impossible to allocate or mmap an
5565 array/buffer larger than what size_t can represent. */
5566 return gdb::make_array_view (section->buffer, section->size);
5567 }
5568
5569 /* Lookup the index cache for the contents of the index associated to
5570 DWARF2_OBJ. */
5571
5572 static gdb::array_view<const gdb_byte>
5573 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5574 {
5575 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5576 if (build_id == nullptr)
5577 return {};
5578
5579 return global_index_cache.lookup_gdb_index (build_id,
5580 &dwarf2_per_bfd->index_cache_res);
5581 }
5582
5583 /* Same as the above, but for DWZ. */
5584
5585 static gdb::array_view<const gdb_byte>
5586 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5587 {
5588 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5589 if (build_id == nullptr)
5590 return {};
5591
5592 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5593 }
5594
5595 static quick_symbol_functions_up make_cooked_index_funcs ();
5596
5597 /* See dwarf2/public.h. */
5598
5599 void
5600 dwarf2_initialize_objfile (struct objfile *objfile)
5601 {
5602 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5603 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5604
5605 dwarf_read_debug_printf ("called");
5606
5607 /* If we're about to read full symbols, don't bother with the
5608 indices. In this case we also don't care if some other debug
5609 format is making psymtabs, because they are all about to be
5610 expanded anyway. */
5611 if ((objfile->flags & OBJF_READNOW))
5612 {
5613 dwarf_read_debug_printf ("readnow requested");
5614
5615 /* When using READNOW, the using_index flag (set below) indicates that
5616 PER_BFD was already initialized, when we loaded some other objfile. */
5617 if (per_bfd->using_index)
5618 {
5619 dwarf_read_debug_printf ("using_index already set");
5620 objfile->qf.push_front (make_dwarf_gdb_index ());
5621 return;
5622 }
5623
5624 per_bfd->using_index = 1;
5625 create_all_comp_units (per_objfile);
5626 per_bfd->quick_file_names_table
5627 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5628
5629 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5630 {
5631 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5632
5633 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5634 struct dwarf2_per_cu_quick_data);
5635 }
5636
5637 /* Arrange for gdb to see the "quick" functions. However, these
5638 functions will be no-ops because we will have expanded all
5639 symtabs. */
5640 objfile->qf.push_front (make_dwarf_gdb_index ());
5641 return;
5642 }
5643
5644 /* Was a debug names index already read when we processed an objfile sharing
5645 PER_BFD? */
5646 if (per_bfd->debug_names_table != nullptr)
5647 {
5648 dwarf_read_debug_printf ("re-using shared debug names table");
5649 objfile->qf.push_front (make_dwarf_debug_names ());
5650 return;
5651 }
5652
5653 /* Was a GDB index already read when we processed an objfile sharing
5654 PER_BFD? */
5655 if (per_bfd->index_table != nullptr)
5656 {
5657 dwarf_read_debug_printf ("re-using shared index table");
5658 objfile->qf.push_front (make_dwarf_gdb_index ());
5659 return;
5660 }
5661
5662 if (per_bfd->cooked_index_table != nullptr)
5663 {
5664 dwarf_read_debug_printf ("re-using cooked index table");
5665 objfile->qf.push_front (make_cooked_index_funcs ());
5666 return;
5667 }
5668
5669 /* There might already be partial symtabs built for this BFD. This happens
5670 when loading the same binary twice with the index-cache enabled. If so,
5671 don't try to read an index. The objfile / per_objfile initialization will
5672 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5673 code path. */
5674 if (per_bfd->partial_symtabs != nullptr)
5675 {
5676 dwarf_read_debug_printf ("re-using shared partial symtabs");
5677 objfile->qf.push_front (make_lazy_dwarf_reader ());
5678 return;
5679 }
5680
5681 if (dwarf2_read_debug_names (per_objfile))
5682 {
5683 dwarf_read_debug_printf ("found debug names");
5684 objfile->qf.push_front (make_dwarf_debug_names ());
5685 return;
5686 }
5687
5688 if (dwarf2_read_gdb_index (per_objfile,
5689 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5690 get_gdb_index_contents_from_section<dwz_file>))
5691 {
5692 dwarf_read_debug_printf ("found gdb index from file");
5693 objfile->qf.push_front (make_dwarf_gdb_index ());
5694 return;
5695 }
5696
5697 /* ... otherwise, try to find the index in the index cache. */
5698 if (dwarf2_read_gdb_index (per_objfile,
5699 get_gdb_index_contents_from_cache,
5700 get_gdb_index_contents_from_cache_dwz))
5701 {
5702 dwarf_read_debug_printf ("found gdb index from cache");
5703 global_index_cache.hit ();
5704 objfile->qf.push_front (make_dwarf_gdb_index ());
5705 return;
5706 }
5707
5708 global_index_cache.miss ();
5709 objfile->qf.push_front (make_lazy_dwarf_reader ());
5710 }
5711
5712 \f
5713
5714 /* Build a partial symbol table. */
5715
5716 void
5717 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5718 {
5719 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5720 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5721
5722 if (per_bfd->partial_symtabs != nullptr)
5723 {
5724 /* Partial symbols were already read, so now we can simply
5725 attach them. */
5726 if (psf == nullptr)
5727 {
5728 psf = new psymbol_functions (per_bfd->partial_symtabs);
5729 objfile->qf.emplace_front (psf);
5730 }
5731 else
5732 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5733 return;
5734 }
5735
5736 if (psf == nullptr)
5737 {
5738 psf = new psymbol_functions;
5739 objfile->qf.emplace_front (psf);
5740 }
5741 const std::shared_ptr<psymtab_storage> &partial_symtabs
5742 = psf->get_partial_symtabs ();
5743
5744 /* Set the local reference to partial symtabs, so that we don't try
5745 to read them again if reading another objfile with the same BFD.
5746 If we can't in fact share, this won't make a difference anyway as
5747 the dwarf2_per_bfd object won't be shared. */
5748 per_bfd->partial_symtabs = partial_symtabs;
5749
5750 try
5751 {
5752 /* This isn't really ideal: all the data we allocate on the
5753 objfile's obstack is still uselessly kept around. However,
5754 freeing it seems unsafe. */
5755 psymtab_discarder psymtabs (partial_symtabs.get ());
5756 dwarf2_build_psymtabs_hard (per_objfile);
5757 psymtabs.keep ();
5758
5759 /* (maybe) store an index in the cache. */
5760 global_index_cache.store (per_objfile);
5761 }
5762 catch (const gdb_exception_error &except)
5763 {
5764 exception_print (gdb_stderr, except);
5765 }
5766 }
5767
5768 /* Find the base address of the compilation unit for range lists and
5769 location lists. It will normally be specified by DW_AT_low_pc.
5770 In DWARF-3 draft 4, the base address could be overridden by
5771 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5772 compilation units with discontinuous ranges. */
5773
5774 static void
5775 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5776 {
5777 struct attribute *attr;
5778
5779 cu->base_address.reset ();
5780
5781 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5782 if (attr != nullptr)
5783 cu->base_address = attr->as_address ();
5784 else
5785 {
5786 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5787 if (attr != nullptr)
5788 cu->base_address = attr->as_address ();
5789 }
5790 }
5791
5792 /* Helper function that returns the proper abbrev section for
5793 THIS_CU. */
5794
5795 static struct dwarf2_section_info *
5796 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5797 {
5798 struct dwarf2_section_info *abbrev;
5799 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5800
5801 if (this_cu->is_dwz)
5802 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5803 else
5804 abbrev = &per_bfd->abbrev;
5805
5806 return abbrev;
5807 }
5808
5809 /* Fetch the abbreviation table offset from a comp or type unit header. */
5810
5811 static sect_offset
5812 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5813 struct dwarf2_section_info *section,
5814 sect_offset sect_off)
5815 {
5816 bfd *abfd = section->get_bfd_owner ();
5817 const gdb_byte *info_ptr;
5818 unsigned int initial_length_size, offset_size;
5819 uint16_t version;
5820
5821 section->read (per_objfile->objfile);
5822 info_ptr = section->buffer + to_underlying (sect_off);
5823 read_initial_length (abfd, info_ptr, &initial_length_size);
5824 offset_size = initial_length_size == 4 ? 4 : 8;
5825 info_ptr += initial_length_size;
5826
5827 version = read_2_bytes (abfd, info_ptr);
5828 info_ptr += 2;
5829 if (version >= 5)
5830 {
5831 /* Skip unit type and address size. */
5832 info_ptr += 2;
5833 }
5834
5835 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5836 }
5837
5838 /* A partial symtab that is used only for include files. */
5839 struct dwarf2_include_psymtab : public partial_symtab
5840 {
5841 dwarf2_include_psymtab (const char *filename,
5842 psymtab_storage *partial_symtabs,
5843 objfile_per_bfd_storage *objfile_per_bfd)
5844 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5845 {
5846 }
5847
5848 void read_symtab (struct objfile *objfile) override
5849 {
5850 /* It's an include file, no symbols to read for it.
5851 Everything is in the includer symtab. */
5852
5853 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5854 expansion of the includer psymtab. We use the dependencies[0] field to
5855 model the includer. But if we go the regular route of calling
5856 expand_psymtab here, and having expand_psymtab call expand_dependencies
5857 to expand the includer, we'll only use expand_psymtab on the includer
5858 (making it a non-toplevel psymtab), while if we expand the includer via
5859 another path, we'll use read_symtab (making it a toplevel psymtab).
5860 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5861 psymtab, and trigger read_symtab on the includer here directly. */
5862 includer ()->read_symtab (objfile);
5863 }
5864
5865 void expand_psymtab (struct objfile *objfile) override
5866 {
5867 /* This is not called by read_symtab, and should not be called by any
5868 expand_dependencies. */
5869 gdb_assert (false);
5870 }
5871
5872 bool readin_p (struct objfile *objfile) const override
5873 {
5874 return includer ()->readin_p (objfile);
5875 }
5876
5877 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5878 {
5879 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5880 while (cust != nullptr && cust->user != nullptr)
5881 cust = cust->user;
5882 return cust;
5883 }
5884
5885 private:
5886 partial_symtab *includer () const
5887 {
5888 /* An include psymtab has exactly one dependency: the psymtab that
5889 includes it. */
5890 gdb_assert (this->number_of_dependencies == 1);
5891 return this->dependencies[0];
5892 }
5893 };
5894
5895 /* Allocate a new partial symtab for file named NAME and mark this new
5896 partial symtab as being an include of PST. */
5897
5898 static void
5899 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5900 const char *name,
5901 dwarf2_psymtab *pst,
5902 psymtab_storage *partial_symtabs,
5903 objfile_per_bfd_storage *objfile_per_bfd)
5904 {
5905 dwarf2_include_psymtab *subpst
5906 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5907
5908 if (!IS_ABSOLUTE_PATH (subpst->filename))
5909 subpst->dirname = pst->dirname;
5910
5911 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5912 subpst->dependencies[0] = pst;
5913 subpst->number_of_dependencies = 1;
5914 }
5915
5916 /* Read the Line Number Program data and extract the list of files
5917 included by the source file represented by PST. Build an include
5918 partial symtab for each of these included files. */
5919
5920 static void
5921 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5922 struct die_info *die,
5923 const file_and_directory &fnd,
5924 dwarf2_psymtab *pst)
5925 {
5926 line_header_up lh;
5927 struct attribute *attr;
5928
5929 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5930 if (attr != nullptr && attr->form_is_unsigned ())
5931 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5932 if (lh == NULL)
5933 return; /* No linetable, so no includes. */
5934
5935 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5936 that we pass in the raw text_low here; that is ok because we're
5937 only decoding the line table to make include partial symtabs, and
5938 so the addresses aren't really used. */
5939 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5940 pst->raw_text_low (), 1);
5941 }
5942
5943 static hashval_t
5944 hash_signatured_type (const void *item)
5945 {
5946 const struct signatured_type *sig_type
5947 = (const struct signatured_type *) item;
5948
5949 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5950 return sig_type->signature;
5951 }
5952
5953 static int
5954 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5955 {
5956 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5957 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5958
5959 return lhs->signature == rhs->signature;
5960 }
5961
5962 /* Allocate a hash table for signatured types. */
5963
5964 static htab_up
5965 allocate_signatured_type_table ()
5966 {
5967 return htab_up (htab_create_alloc (41,
5968 hash_signatured_type,
5969 eq_signatured_type,
5970 NULL, xcalloc, xfree));
5971 }
5972
5973 /* A helper for create_debug_types_hash_table. Read types from SECTION
5974 and fill them into TYPES_HTAB. It will process only type units,
5975 therefore DW_UT_type. */
5976
5977 static void
5978 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5979 struct dwo_file *dwo_file,
5980 dwarf2_section_info *section, htab_up &types_htab,
5981 rcuh_kind section_kind)
5982 {
5983 struct objfile *objfile = per_objfile->objfile;
5984 struct dwarf2_section_info *abbrev_section;
5985 bfd *abfd;
5986 const gdb_byte *info_ptr, *end_ptr;
5987
5988 abbrev_section = &dwo_file->sections.abbrev;
5989
5990 dwarf_read_debug_printf ("Reading %s for %s",
5991 section->get_name (),
5992 abbrev_section->get_file_name ());
5993
5994 section->read (objfile);
5995 info_ptr = section->buffer;
5996
5997 if (info_ptr == NULL)
5998 return;
5999
6000 /* We can't set abfd until now because the section may be empty or
6001 not present, in which case the bfd is unknown. */
6002 abfd = section->get_bfd_owner ();
6003
6004 /* We don't use cutu_reader here because we don't need to read
6005 any dies: the signature is in the header. */
6006
6007 end_ptr = info_ptr + section->size;
6008 while (info_ptr < end_ptr)
6009 {
6010 signatured_type_up sig_type;
6011 struct dwo_unit *dwo_tu;
6012 void **slot;
6013 const gdb_byte *ptr = info_ptr;
6014 struct comp_unit_head header;
6015 unsigned int length;
6016
6017 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6018
6019 /* Initialize it due to a false compiler warning. */
6020 header.signature = -1;
6021 header.type_cu_offset_in_tu = (cu_offset) -1;
6022
6023 /* We need to read the type's signature in order to build the hash
6024 table, but we don't need anything else just yet. */
6025
6026 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6027 abbrev_section, ptr, section_kind);
6028
6029 length = header.get_length ();
6030
6031 /* Skip dummy type units. */
6032 if (ptr >= info_ptr + length
6033 || peek_abbrev_code (abfd, ptr) == 0
6034 || (header.unit_type != DW_UT_type
6035 && header.unit_type != DW_UT_split_type))
6036 {
6037 info_ptr += length;
6038 continue;
6039 }
6040
6041 if (types_htab == NULL)
6042 types_htab = allocate_dwo_unit_table ();
6043
6044 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6045 dwo_tu->dwo_file = dwo_file;
6046 dwo_tu->signature = header.signature;
6047 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6048 dwo_tu->section = section;
6049 dwo_tu->sect_off = sect_off;
6050 dwo_tu->length = length;
6051
6052 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
6053 gdb_assert (slot != NULL);
6054 if (*slot != NULL)
6055 complaint (_("debug type entry at offset %s is duplicate to"
6056 " the entry at offset %s, signature %s"),
6057 sect_offset_str (sect_off),
6058 sect_offset_str (dwo_tu->sect_off),
6059 hex_string (header.signature));
6060 *slot = dwo_tu;
6061
6062 dwarf_read_debug_printf_v (" offset %s, signature %s",
6063 sect_offset_str (sect_off),
6064 hex_string (header.signature));
6065
6066 info_ptr += length;
6067 }
6068 }
6069
6070 /* Create the hash table of all entries in the .debug_types
6071 (or .debug_types.dwo) section(s).
6072 DWO_FILE is a pointer to the DWO file object.
6073
6074 The result is a pointer to the hash table or NULL if there are no types.
6075
6076 Note: This function processes DWO files only, not DWP files. */
6077
6078 static void
6079 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6080 struct dwo_file *dwo_file,
6081 gdb::array_view<dwarf2_section_info> type_sections,
6082 htab_up &types_htab)
6083 {
6084 for (dwarf2_section_info &section : type_sections)
6085 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6086 rcuh_kind::TYPE);
6087 }
6088
6089 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6090 If SLOT is non-NULL, it is the entry to use in the hash table.
6091 Otherwise we find one. */
6092
6093 static struct signatured_type *
6094 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6095 {
6096 if (per_objfile->per_bfd->all_comp_units.size ()
6097 == per_objfile->per_bfd->all_comp_units.capacity ())
6098 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6099
6100 signatured_type_up sig_type_holder
6101 = per_objfile->per_bfd->allocate_signatured_type (sig);
6102 signatured_type *sig_type = sig_type_holder.get ();
6103
6104 per_objfile->per_bfd->all_comp_units.emplace_back
6105 (sig_type_holder.release ());
6106 if (per_objfile->per_bfd->using_index)
6107 {
6108 sig_type->v.quick =
6109 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6110 struct dwarf2_per_cu_quick_data);
6111 }
6112
6113 if (slot == NULL)
6114 {
6115 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6116 sig_type, INSERT);
6117 }
6118 gdb_assert (*slot == NULL);
6119 *slot = sig_type;
6120 /* The rest of sig_type must be filled in by the caller. */
6121 return sig_type;
6122 }
6123
6124 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6125 Fill in SIG_ENTRY with DWO_ENTRY. */
6126
6127 static void
6128 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6129 struct signatured_type *sig_entry,
6130 struct dwo_unit *dwo_entry)
6131 {
6132 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6133
6134 /* Make sure we're not clobbering something we don't expect to. */
6135 gdb_assert (! sig_entry->queued);
6136 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6137 if (per_bfd->using_index)
6138 {
6139 gdb_assert (sig_entry->v.quick != NULL);
6140 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6141 }
6142 else
6143 gdb_assert (sig_entry->v.psymtab == NULL);
6144 gdb_assert (sig_entry->signature == dwo_entry->signature);
6145 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
6146 || (to_underlying (sig_entry->type_offset_in_section)
6147 == to_underlying (dwo_entry->type_offset_in_tu)));
6148 gdb_assert (sig_entry->type_unit_group == NULL);
6149 gdb_assert (sig_entry->dwo_unit == NULL
6150 || sig_entry->dwo_unit == dwo_entry);
6151
6152 sig_entry->section = dwo_entry->section;
6153 sig_entry->sect_off = dwo_entry->sect_off;
6154 sig_entry->length = dwo_entry->length;
6155 sig_entry->reading_dwo_directly = 1;
6156 sig_entry->per_bfd = per_bfd;
6157 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6158 sig_entry->dwo_unit = dwo_entry;
6159 }
6160
6161 /* Subroutine of lookup_signatured_type.
6162 If we haven't read the TU yet, create the signatured_type data structure
6163 for a TU to be read in directly from a DWO file, bypassing the stub.
6164 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6165 using .gdb_index, then when reading a CU we want to stay in the DWO file
6166 containing that CU. Otherwise we could end up reading several other DWO
6167 files (due to comdat folding) to process the transitive closure of all the
6168 mentioned TUs, and that can be slow. The current DWO file will have every
6169 type signature that it needs.
6170 We only do this for .gdb_index because in the psymtab case we already have
6171 to read all the DWOs to build the type unit groups. */
6172
6173 static struct signatured_type *
6174 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6175 {
6176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6177 struct dwo_file *dwo_file;
6178 struct dwo_unit find_dwo_entry, *dwo_entry;
6179 void **slot;
6180
6181 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6182
6183 /* If TU skeletons have been removed then we may not have read in any
6184 TUs yet. */
6185 if (per_objfile->per_bfd->signatured_types == NULL)
6186 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6187
6188 /* We only ever need to read in one copy of a signatured type.
6189 Use the global signatured_types array to do our own comdat-folding
6190 of types. If this is the first time we're reading this TU, and
6191 the TU has an entry in .gdb_index, replace the recorded data from
6192 .gdb_index with this TU. */
6193
6194 signatured_type find_sig_entry (sig);
6195 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6196 &find_sig_entry, INSERT);
6197 signatured_type *sig_entry = (struct signatured_type *) *slot;
6198
6199 /* We can get here with the TU already read, *or* in the process of being
6200 read. Don't reassign the global entry to point to this DWO if that's
6201 the case. Also note that if the TU is already being read, it may not
6202 have come from a DWO, the program may be a mix of Fission-compiled
6203 code and non-Fission-compiled code. */
6204
6205 /* Have we already tried to read this TU?
6206 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6207 needn't exist in the global table yet). */
6208 if (sig_entry != NULL && sig_entry->tu_read)
6209 return sig_entry;
6210
6211 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6212 dwo_unit of the TU itself. */
6213 dwo_file = cu->dwo_unit->dwo_file;
6214
6215 /* Ok, this is the first time we're reading this TU. */
6216 if (dwo_file->tus == NULL)
6217 return NULL;
6218 find_dwo_entry.signature = sig;
6219 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6220 &find_dwo_entry);
6221 if (dwo_entry == NULL)
6222 return NULL;
6223
6224 /* If the global table doesn't have an entry for this TU, add one. */
6225 if (sig_entry == NULL)
6226 sig_entry = add_type_unit (per_objfile, sig, slot);
6227
6228 if (sig_entry->dwo_unit == nullptr)
6229 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6230 sig_entry->tu_read = 1;
6231 return sig_entry;
6232 }
6233
6234 /* Subroutine of lookup_signatured_type.
6235 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6236 then try the DWP file. If the TU stub (skeleton) has been removed then
6237 it won't be in .gdb_index. */
6238
6239 static struct signatured_type *
6240 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6241 {
6242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6243 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6244 struct dwo_unit *dwo_entry;
6245 void **slot;
6246
6247 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6248 gdb_assert (dwp_file != NULL);
6249
6250 /* If TU skeletons have been removed then we may not have read in any
6251 TUs yet. */
6252 if (per_objfile->per_bfd->signatured_types == NULL)
6253 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6254
6255 signatured_type find_sig_entry (sig);
6256 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6257 &find_sig_entry, INSERT);
6258 signatured_type *sig_entry = (struct signatured_type *) *slot;
6259
6260 /* Have we already tried to read this TU?
6261 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6262 needn't exist in the global table yet). */
6263 if (sig_entry != NULL)
6264 return sig_entry;
6265
6266 if (dwp_file->tus == NULL)
6267 return NULL;
6268 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6269 1 /* is_debug_types */);
6270 if (dwo_entry == NULL)
6271 return NULL;
6272
6273 sig_entry = add_type_unit (per_objfile, sig, slot);
6274 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6275
6276 return sig_entry;
6277 }
6278
6279 /* Lookup a signature based type for DW_FORM_ref_sig8.
6280 Returns NULL if signature SIG is not present in the table.
6281 It is up to the caller to complain about this. */
6282
6283 static struct signatured_type *
6284 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6285 {
6286 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6287
6288 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6289 {
6290 /* We're in a DWO/DWP file, and we're using .gdb_index.
6291 These cases require special processing. */
6292 if (get_dwp_file (per_objfile) == NULL)
6293 return lookup_dwo_signatured_type (cu, sig);
6294 else
6295 return lookup_dwp_signatured_type (cu, sig);
6296 }
6297 else
6298 {
6299 if (per_objfile->per_bfd->signatured_types == NULL)
6300 return NULL;
6301 signatured_type find_entry (sig);
6302 return ((struct signatured_type *)
6303 htab_find (per_objfile->per_bfd->signatured_types.get (),
6304 &find_entry));
6305 }
6306 }
6307
6308 /* Low level DIE reading support. */
6309
6310 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6311
6312 static void
6313 init_cu_die_reader (struct die_reader_specs *reader,
6314 struct dwarf2_cu *cu,
6315 struct dwarf2_section_info *section,
6316 struct dwo_file *dwo_file,
6317 struct abbrev_table *abbrev_table)
6318 {
6319 gdb_assert (section->readin && section->buffer != NULL);
6320 reader->abfd = section->get_bfd_owner ();
6321 reader->cu = cu;
6322 reader->dwo_file = dwo_file;
6323 reader->die_section = section;
6324 reader->buffer = section->buffer;
6325 reader->buffer_end = section->buffer + section->size;
6326 reader->abbrev_table = abbrev_table;
6327 }
6328
6329 /* Subroutine of cutu_reader to simplify it.
6330 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6331 There's just a lot of work to do, and cutu_reader is big enough
6332 already.
6333
6334 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6335 from it to the DIE in the DWO. If NULL we are skipping the stub.
6336 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6337 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6338 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6339 STUB_COMP_DIR may be non-NULL.
6340 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6341 are filled in with the info of the DIE from the DWO file.
6342 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6343 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6344 kept around for at least as long as *RESULT_READER.
6345
6346 The result is non-zero if a valid (non-dummy) DIE was found. */
6347
6348 static int
6349 read_cutu_die_from_dwo (dwarf2_cu *cu,
6350 struct dwo_unit *dwo_unit,
6351 struct die_info *stub_comp_unit_die,
6352 const char *stub_comp_dir,
6353 struct die_reader_specs *result_reader,
6354 const gdb_byte **result_info_ptr,
6355 struct die_info **result_comp_unit_die,
6356 abbrev_table_up *result_dwo_abbrev_table)
6357 {
6358 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6359 dwarf2_per_cu_data *per_cu = cu->per_cu;
6360 struct objfile *objfile = per_objfile->objfile;
6361 bfd *abfd;
6362 const gdb_byte *begin_info_ptr, *info_ptr;
6363 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6364 int i,num_extra_attrs;
6365 struct dwarf2_section_info *dwo_abbrev_section;
6366 struct die_info *comp_unit_die;
6367
6368 /* At most one of these may be provided. */
6369 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6370
6371 /* These attributes aren't processed until later:
6372 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6373 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6374 referenced later. However, these attributes are found in the stub
6375 which we won't have later. In order to not impose this complication
6376 on the rest of the code, we read them here and copy them to the
6377 DWO CU/TU die. */
6378
6379 stmt_list = NULL;
6380 low_pc = NULL;
6381 high_pc = NULL;
6382 ranges = NULL;
6383 comp_dir = NULL;
6384
6385 if (stub_comp_unit_die != NULL)
6386 {
6387 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6388 DWO file. */
6389 if (!per_cu->is_debug_types)
6390 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6391 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6392 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6393 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6394 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6395
6396 cu->addr_base = stub_comp_unit_die->addr_base ();
6397
6398 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6399 We need the value before we can process DW_AT_ranges values from the
6400 DWO. */
6401 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6402
6403 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6404 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6405 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6406 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6407 section. */
6408 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6409 }
6410 else if (stub_comp_dir != NULL)
6411 {
6412 /* Reconstruct the comp_dir attribute to simplify the code below. */
6413 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6414 comp_dir->name = DW_AT_comp_dir;
6415 comp_dir->form = DW_FORM_string;
6416 comp_dir->set_string_noncanonical (stub_comp_dir);
6417 }
6418
6419 /* Set up for reading the DWO CU/TU. */
6420 cu->dwo_unit = dwo_unit;
6421 dwarf2_section_info *section = dwo_unit->section;
6422 section->read (objfile);
6423 abfd = section->get_bfd_owner ();
6424 begin_info_ptr = info_ptr = (section->buffer
6425 + to_underlying (dwo_unit->sect_off));
6426 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6427
6428 if (per_cu->is_debug_types)
6429 {
6430 signatured_type *sig_type = (struct signatured_type *) per_cu;
6431
6432 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6433 section, dwo_abbrev_section,
6434 info_ptr, rcuh_kind::TYPE);
6435 /* This is not an assert because it can be caused by bad debug info. */
6436 if (sig_type->signature != cu->header.signature)
6437 {
6438 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6439 " TU at offset %s [in module %s]"),
6440 hex_string (sig_type->signature),
6441 hex_string (cu->header.signature),
6442 sect_offset_str (dwo_unit->sect_off),
6443 bfd_get_filename (abfd));
6444 }
6445 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6446 /* For DWOs coming from DWP files, we don't know the CU length
6447 nor the type's offset in the TU until now. */
6448 dwo_unit->length = cu->header.get_length ();
6449 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6450
6451 /* Establish the type offset that can be used to lookup the type.
6452 For DWO files, we don't know it until now. */
6453 sig_type->type_offset_in_section
6454 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6455 }
6456 else
6457 {
6458 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6459 section, dwo_abbrev_section,
6460 info_ptr, rcuh_kind::COMPILE);
6461 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6462 /* For DWOs coming from DWP files, we don't know the CU length
6463 until now. */
6464 dwo_unit->length = cu->header.get_length ();
6465 }
6466
6467 dwo_abbrev_section->read (objfile);
6468 *result_dwo_abbrev_table
6469 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6470 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6471 result_dwo_abbrev_table->get ());
6472
6473 /* Read in the die, but leave space to copy over the attributes
6474 from the stub. This has the benefit of simplifying the rest of
6475 the code - all the work to maintain the illusion of a single
6476 DW_TAG_{compile,type}_unit DIE is done here. */
6477 num_extra_attrs = ((stmt_list != NULL)
6478 + (low_pc != NULL)
6479 + (high_pc != NULL)
6480 + (ranges != NULL)
6481 + (comp_dir != NULL));
6482 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6483 num_extra_attrs);
6484
6485 /* Copy over the attributes from the stub to the DIE we just read in. */
6486 comp_unit_die = *result_comp_unit_die;
6487 i = comp_unit_die->num_attrs;
6488 if (stmt_list != NULL)
6489 comp_unit_die->attrs[i++] = *stmt_list;
6490 if (low_pc != NULL)
6491 comp_unit_die->attrs[i++] = *low_pc;
6492 if (high_pc != NULL)
6493 comp_unit_die->attrs[i++] = *high_pc;
6494 if (ranges != NULL)
6495 comp_unit_die->attrs[i++] = *ranges;
6496 if (comp_dir != NULL)
6497 comp_unit_die->attrs[i++] = *comp_dir;
6498 comp_unit_die->num_attrs += num_extra_attrs;
6499
6500 if (dwarf_die_debug)
6501 {
6502 gdb_printf (gdb_stdlog,
6503 "Read die from %s@0x%x of %s:\n",
6504 section->get_name (),
6505 (unsigned) (begin_info_ptr - section->buffer),
6506 bfd_get_filename (abfd));
6507 dump_die (comp_unit_die, dwarf_die_debug);
6508 }
6509
6510 /* Skip dummy compilation units. */
6511 if (info_ptr >= begin_info_ptr + dwo_unit->length
6512 || peek_abbrev_code (abfd, info_ptr) == 0)
6513 return 0;
6514
6515 *result_info_ptr = info_ptr;
6516 return 1;
6517 }
6518
6519 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6520 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6521 signature is part of the header. */
6522 static gdb::optional<ULONGEST>
6523 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6524 {
6525 if (cu->header.version >= 5)
6526 return cu->header.signature;
6527 struct attribute *attr;
6528 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6529 if (attr == nullptr || !attr->form_is_unsigned ())
6530 return gdb::optional<ULONGEST> ();
6531 return attr->as_unsigned ();
6532 }
6533
6534 /* Subroutine of cutu_reader to simplify it.
6535 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6536 Returns NULL if the specified DWO unit cannot be found. */
6537
6538 static struct dwo_unit *
6539 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6540 {
6541 #if CXX_STD_THREAD
6542 /* We need a lock here both to handle the DWO hash table, and BFD,
6543 which is not thread-safe. */
6544 static std::mutex dwo_lock;
6545
6546 std::lock_guard<std::mutex> guard (dwo_lock);
6547 #endif
6548
6549 dwarf2_per_cu_data *per_cu = cu->per_cu;
6550 struct dwo_unit *dwo_unit;
6551 const char *comp_dir;
6552
6553 gdb_assert (cu != NULL);
6554
6555 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6556 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6557 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6558
6559 if (per_cu->is_debug_types)
6560 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6561 else
6562 {
6563 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6564
6565 if (!signature.has_value ())
6566 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6567 " [in module %s]"),
6568 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6569
6570 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6571 }
6572
6573 return dwo_unit;
6574 }
6575
6576 /* Subroutine of cutu_reader to simplify it.
6577 See it for a description of the parameters.
6578 Read a TU directly from a DWO file, bypassing the stub. */
6579
6580 void
6581 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6582 dwarf2_per_objfile *per_objfile,
6583 dwarf2_cu *existing_cu)
6584 {
6585 struct signatured_type *sig_type;
6586
6587 /* Verify we can do the following downcast, and that we have the
6588 data we need. */
6589 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6590 sig_type = (struct signatured_type *) this_cu;
6591 gdb_assert (sig_type->dwo_unit != NULL);
6592
6593 dwarf2_cu *cu;
6594
6595 if (existing_cu != nullptr)
6596 {
6597 cu = existing_cu;
6598 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6599 /* There's no need to do the rereading_dwo_cu handling that
6600 cutu_reader does since we don't read the stub. */
6601 }
6602 else
6603 {
6604 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6605 in per_objfile yet. */
6606 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6607 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6608 cu = m_new_cu.get ();
6609 }
6610
6611 /* A future optimization, if needed, would be to use an existing
6612 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6613 could share abbrev tables. */
6614
6615 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6616 NULL /* stub_comp_unit_die */,
6617 sig_type->dwo_unit->dwo_file->comp_dir,
6618 this, &info_ptr,
6619 &comp_unit_die,
6620 &m_dwo_abbrev_table) == 0)
6621 {
6622 /* Dummy die. */
6623 dummy_p = true;
6624 }
6625 }
6626
6627 /* Initialize a CU (or TU) and read its DIEs.
6628 If the CU defers to a DWO file, read the DWO file as well.
6629
6630 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6631 Otherwise the table specified in the comp unit header is read in and used.
6632 This is an optimization for when we already have the abbrev table.
6633
6634 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6635 allocated. */
6636
6637 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6638 dwarf2_per_objfile *per_objfile,
6639 struct abbrev_table *abbrev_table,
6640 dwarf2_cu *existing_cu,
6641 bool skip_partial,
6642 abbrev_cache *cache)
6643 : die_reader_specs {},
6644 m_this_cu (this_cu)
6645 {
6646 struct objfile *objfile = per_objfile->objfile;
6647 struct dwarf2_section_info *section = this_cu->section;
6648 bfd *abfd = section->get_bfd_owner ();
6649 const gdb_byte *begin_info_ptr;
6650 struct signatured_type *sig_type = NULL;
6651 struct dwarf2_section_info *abbrev_section;
6652 /* Non-zero if CU currently points to a DWO file and we need to
6653 reread it. When this happens we need to reread the skeleton die
6654 before we can reread the DWO file (this only applies to CUs, not TUs). */
6655 int rereading_dwo_cu = 0;
6656
6657 if (dwarf_die_debug)
6658 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6659 this_cu->is_debug_types ? "type" : "comp",
6660 sect_offset_str (this_cu->sect_off));
6661
6662 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6663 file (instead of going through the stub), short-circuit all of this. */
6664 if (this_cu->reading_dwo_directly)
6665 {
6666 /* Narrow down the scope of possibilities to have to understand. */
6667 gdb_assert (this_cu->is_debug_types);
6668 gdb_assert (abbrev_table == NULL);
6669 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6670 return;
6671 }
6672
6673 /* This is cheap if the section is already read in. */
6674 section->read (objfile);
6675
6676 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6677
6678 abbrev_section = get_abbrev_section_for_cu (this_cu);
6679
6680 dwarf2_cu *cu;
6681
6682 if (existing_cu != nullptr)
6683 {
6684 cu = existing_cu;
6685 /* If this CU is from a DWO file we need to start over, we need to
6686 refetch the attributes from the skeleton CU.
6687 This could be optimized by retrieving those attributes from when we
6688 were here the first time: the previous comp_unit_die was stored in
6689 comp_unit_obstack. But there's no data yet that we need this
6690 optimization. */
6691 if (cu->dwo_unit != NULL)
6692 rereading_dwo_cu = 1;
6693 }
6694 else
6695 {
6696 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6697 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6698 let us know that the CU is being scanned using the parallel
6699 indexer. This assert is avoided in this case because (1) it
6700 is irrelevant, and (2) the get_cu method is not
6701 thread-safe. */
6702 gdb_assert (cache != nullptr
6703 || per_objfile->get_cu (this_cu) == nullptr);
6704 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6705 cu = m_new_cu.get ();
6706 }
6707
6708 /* Get the header. */
6709 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6710 {
6711 /* We already have the header, there's no need to read it in again. */
6712 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6713 }
6714 else
6715 {
6716 if (this_cu->is_debug_types)
6717 {
6718 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6719 section, abbrev_section,
6720 info_ptr, rcuh_kind::TYPE);
6721
6722 /* Since per_cu is the first member of struct signatured_type,
6723 we can go from a pointer to one to a pointer to the other. */
6724 sig_type = (struct signatured_type *) this_cu;
6725 gdb_assert (sig_type->signature == cu->header.signature);
6726 gdb_assert (sig_type->type_offset_in_tu
6727 == cu->header.type_cu_offset_in_tu);
6728 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6729
6730 /* LENGTH has not been set yet for type units if we're
6731 using .gdb_index. */
6732 this_cu->length = cu->header.get_length ();
6733
6734 /* Establish the type offset that can be used to lookup the type. */
6735 sig_type->type_offset_in_section =
6736 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6737
6738 this_cu->dwarf_version = cu->header.version;
6739 }
6740 else
6741 {
6742 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6743 section, abbrev_section,
6744 info_ptr,
6745 rcuh_kind::COMPILE);
6746
6747 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6748 if (this_cu->length == 0)
6749 this_cu->length = cu->header.get_length ();
6750 else
6751 gdb_assert (this_cu->length == cu->header.get_length ());
6752 this_cu->dwarf_version = cu->header.version;
6753 }
6754 }
6755
6756 /* Skip dummy compilation units. */
6757 if (info_ptr >= begin_info_ptr + this_cu->length
6758 || peek_abbrev_code (abfd, info_ptr) == 0)
6759 {
6760 dummy_p = true;
6761 return;
6762 }
6763
6764 /* If we don't have them yet, read the abbrevs for this compilation unit.
6765 And if we need to read them now, make sure they're freed when we're
6766 done. */
6767 if (abbrev_table != NULL)
6768 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6769 else
6770 {
6771 if (cache != nullptr)
6772 abbrev_table = cache->find (abbrev_section,
6773 cu->header.abbrev_sect_off);
6774 if (abbrev_table == nullptr)
6775 {
6776 abbrev_section->read (objfile);
6777 m_abbrev_table_holder
6778 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6779 abbrev_table = m_abbrev_table_holder.get ();
6780 }
6781 }
6782
6783 /* Read the top level CU/TU die. */
6784 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6785 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6786
6787 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6788 {
6789 dummy_p = true;
6790 return;
6791 }
6792
6793 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6794 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6795 table from the DWO file and pass the ownership over to us. It will be
6796 referenced from READER, so we must make sure to free it after we're done
6797 with READER.
6798
6799 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6800 DWO CU, that this test will fail (the attribute will not be present). */
6801 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6802 if (dwo_name != nullptr)
6803 {
6804 struct dwo_unit *dwo_unit;
6805 struct die_info *dwo_comp_unit_die;
6806
6807 if (comp_unit_die->has_children)
6808 {
6809 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6810 " has children (offset %s) [in module %s]"),
6811 sect_offset_str (this_cu->sect_off),
6812 bfd_get_filename (abfd));
6813 }
6814 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6815 if (dwo_unit != NULL)
6816 {
6817 if (read_cutu_die_from_dwo (cu, dwo_unit,
6818 comp_unit_die, NULL,
6819 this, &info_ptr,
6820 &dwo_comp_unit_die,
6821 &m_dwo_abbrev_table) == 0)
6822 {
6823 /* Dummy die. */
6824 dummy_p = true;
6825 return;
6826 }
6827 comp_unit_die = dwo_comp_unit_die;
6828 }
6829 else
6830 {
6831 /* Yikes, we couldn't find the rest of the DIE, we only have
6832 the stub. A complaint has already been logged. There's
6833 not much more we can do except pass on the stub DIE to
6834 die_reader_func. We don't want to throw an error on bad
6835 debug info. */
6836 }
6837 }
6838 }
6839
6840 void
6841 cutu_reader::keep ()
6842 {
6843 /* Done, clean up. */
6844 gdb_assert (!dummy_p);
6845 if (m_new_cu != NULL)
6846 {
6847 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6848 now. */
6849 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6850 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6851 }
6852 }
6853
6854 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6855 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6856 assumed to have already done the lookup to find the DWO file).
6857
6858 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6859 THIS_CU->is_debug_types, but nothing else.
6860
6861 We fill in THIS_CU->length.
6862
6863 THIS_CU->cu is always freed when done.
6864 This is done in order to not leave THIS_CU->cu in a state where we have
6865 to care whether it refers to the "main" CU or the DWO CU.
6866
6867 When parent_cu is passed, it is used to provide a default value for
6868 str_offsets_base and addr_base from the parent. */
6869
6870 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6871 dwarf2_per_objfile *per_objfile,
6872 struct dwarf2_cu *parent_cu,
6873 struct dwo_file *dwo_file)
6874 : die_reader_specs {},
6875 m_this_cu (this_cu)
6876 {
6877 struct objfile *objfile = per_objfile->objfile;
6878 struct dwarf2_section_info *section = this_cu->section;
6879 bfd *abfd = section->get_bfd_owner ();
6880 struct dwarf2_section_info *abbrev_section;
6881 const gdb_byte *begin_info_ptr, *info_ptr;
6882
6883 if (dwarf_die_debug)
6884 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6885 this_cu->is_debug_types ? "type" : "comp",
6886 sect_offset_str (this_cu->sect_off));
6887
6888 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6889
6890 abbrev_section = (dwo_file != NULL
6891 ? &dwo_file->sections.abbrev
6892 : get_abbrev_section_for_cu (this_cu));
6893
6894 /* This is cheap if the section is already read in. */
6895 section->read (objfile);
6896
6897 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6898
6899 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6900 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6901 section, abbrev_section, info_ptr,
6902 (this_cu->is_debug_types
6903 ? rcuh_kind::TYPE
6904 : rcuh_kind::COMPILE));
6905
6906 if (parent_cu != nullptr)
6907 {
6908 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6909 m_new_cu->addr_base = parent_cu->addr_base;
6910 }
6911 this_cu->length = m_new_cu->header.get_length ();
6912
6913 /* Skip dummy compilation units. */
6914 if (info_ptr >= begin_info_ptr + this_cu->length
6915 || peek_abbrev_code (abfd, info_ptr) == 0)
6916 {
6917 dummy_p = true;
6918 return;
6919 }
6920
6921 abbrev_section->read (objfile);
6922 m_abbrev_table_holder
6923 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6924
6925 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6926 m_abbrev_table_holder.get ());
6927 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6928 }
6929
6930 \f
6931 /* Type Unit Groups.
6932
6933 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6934 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6935 so that all types coming from the same compilation (.o file) are grouped
6936 together. A future step could be to put the types in the same symtab as
6937 the CU the types ultimately came from. */
6938
6939 static hashval_t
6940 hash_type_unit_group (const void *item)
6941 {
6942 const struct type_unit_group *tu_group
6943 = (const struct type_unit_group *) item;
6944
6945 return hash_stmt_list_entry (&tu_group->hash);
6946 }
6947
6948 static int
6949 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6950 {
6951 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6952 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6953
6954 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6955 }
6956
6957 /* Allocate a hash table for type unit groups. */
6958
6959 static htab_up
6960 allocate_type_unit_groups_table ()
6961 {
6962 return htab_up (htab_create_alloc (3,
6963 hash_type_unit_group,
6964 eq_type_unit_group,
6965 htab_delete_entry<type_unit_group>,
6966 xcalloc, xfree));
6967 }
6968
6969 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6970 partial symtabs. We combine several TUs per psymtab to not let the size
6971 of any one psymtab grow too big. */
6972 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6973 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6974
6975 /* Helper routine for get_type_unit_group.
6976 Create the type_unit_group object used to hold one or more TUs. */
6977
6978 static std::unique_ptr<type_unit_group>
6979 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6980 {
6981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6982 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6983
6984 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6985 tu_group->per_bfd = per_bfd;
6986
6987 if (per_bfd->using_index)
6988 {
6989 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6990 struct dwarf2_per_cu_quick_data);
6991 }
6992 else
6993 {
6994 unsigned int line_offset = to_underlying (line_offset_struct);
6995 dwarf2_psymtab *pst;
6996 std::string name;
6997
6998 /* Give the symtab a useful name for debug purposes. */
6999 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7000 name = string_printf ("<type_units_%d>",
7001 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7002 else
7003 name = string_printf ("<type_units_at_0x%x>", line_offset);
7004
7005 pst = create_partial_symtab (tu_group.get (), per_objfile,
7006 name.c_str ());
7007 pst->anonymous = true;
7008 }
7009
7010 tu_group->hash.dwo_unit = cu->dwo_unit;
7011 tu_group->hash.line_sect_off = line_offset_struct;
7012
7013 return tu_group;
7014 }
7015
7016 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7017 STMT_LIST is a DW_AT_stmt_list attribute. */
7018
7019 static struct type_unit_group *
7020 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7021 {
7022 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7023 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7024 struct type_unit_group *tu_group;
7025 void **slot;
7026 unsigned int line_offset;
7027 struct type_unit_group type_unit_group_for_lookup;
7028
7029 if (per_objfile->per_bfd->type_unit_groups == NULL)
7030 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7031
7032 /* Do we need to create a new group, or can we use an existing one? */
7033
7034 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7035 {
7036 line_offset = stmt_list->as_unsigned ();
7037 ++tu_stats->nr_symtab_sharers;
7038 }
7039 else
7040 {
7041 /* Ugh, no stmt_list. Rare, but we have to handle it.
7042 We can do various things here like create one group per TU or
7043 spread them over multiple groups to split up the expansion work.
7044 To avoid worst case scenarios (too many groups or too large groups)
7045 we, umm, group them in bunches. */
7046 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7047 | (tu_stats->nr_stmt_less_type_units
7048 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7049 ++tu_stats->nr_stmt_less_type_units;
7050 }
7051
7052 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7053 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7054 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7055 &type_unit_group_for_lookup, INSERT);
7056 if (*slot == nullptr)
7057 {
7058 sect_offset line_offset_struct = (sect_offset) line_offset;
7059 std::unique_ptr<type_unit_group> grp
7060 = create_type_unit_group (cu, line_offset_struct);
7061 *slot = grp.release ();
7062 ++tu_stats->nr_symtabs;
7063 }
7064
7065 tu_group = (struct type_unit_group *) *slot;
7066 gdb_assert (tu_group != nullptr);
7067 return tu_group;
7068 }
7069 \f
7070 /* Partial symbol tables. */
7071
7072 /* Create a psymtab named NAME and assign it to PER_CU.
7073
7074 The caller must fill in the following details:
7075 dirname, textlow, texthigh. */
7076
7077 static dwarf2_psymtab *
7078 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7079 dwarf2_per_objfile *per_objfile,
7080 const char *name)
7081 {
7082 dwarf2_psymtab *pst
7083 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7084 per_objfile->objfile->per_bfd, per_cu);
7085
7086 pst->psymtabs_addrmap_supported = true;
7087
7088 /* This is the glue that links PST into GDB's symbol API. */
7089 per_cu->v.psymtab = pst;
7090
7091 return pst;
7092 }
7093 \f
7094
7095 /* An instance of this is created when scanning DWARF to create a
7096 cooked index. */
7097
7098 class cooked_index_storage
7099 {
7100 public:
7101
7102 cooked_index_storage ()
7103 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
7104 eq_cutu_reader,
7105 htab_delete_entry<cutu_reader>,
7106 xcalloc, xfree)),
7107 m_index (new cooked_index),
7108 m_addrmap_storage (),
7109 m_addrmap (addrmap_create_mutable (&m_addrmap_storage))
7110 {
7111 }
7112
7113 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
7114
7115 /* Return the current abbrev cache. */
7116 abbrev_cache *get_abbrev_cache ()
7117 {
7118 return &m_abbrev_cache;
7119 }
7120
7121 /* Return the DIE reader corresponding to PER_CU. If no such reader
7122 has been registered, return NULL. */
7123 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
7124 {
7125 int index = per_cu->index;
7126 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
7127 &index, index);
7128 }
7129
7130 /* Preserve READER by storing it in the local hash table. */
7131 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
7132 {
7133 m_abbrev_cache.add (reader->release_abbrev_table ());
7134
7135 int index = reader->cu->per_cu->index;
7136 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
7137 index, INSERT);
7138 gdb_assert (*slot == nullptr);
7139 cutu_reader *result = reader.get ();
7140 *slot = reader.release ();
7141 return result;
7142 }
7143
7144 /* Add an entry to the index. The arguments describe the entry; see
7145 cooked-index.h. The new entry is returned. */
7146 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
7147 cooked_index_flag flags,
7148 const char *name,
7149 const cooked_index_entry *parent_entry,
7150 dwarf2_per_cu_data *per_cu)
7151 {
7152 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
7153 }
7154
7155 /* Install the current addrmap into the index being constructed,
7156 then transfer ownership of the index to the caller. */
7157 std::unique_ptr<cooked_index> release ()
7158 {
7159 m_index->install_addrmap (m_addrmap);
7160 return std::move (m_index);
7161 }
7162
7163 /* Return the mutable addrmap that is currently being created. */
7164 addrmap *get_addrmap ()
7165 {
7166 return m_addrmap;
7167 }
7168
7169 private:
7170
7171 /* Hash function for a cutu_reader. */
7172 static hashval_t hash_cutu_reader (const void *a)
7173 {
7174 const cutu_reader *reader = (const cutu_reader *) a;
7175 return reader->cu->per_cu->index;
7176 }
7177
7178 /* Equality function for cutu_reader. */
7179 static int eq_cutu_reader (const void *a, const void *b)
7180 {
7181 const cutu_reader *ra = (const cutu_reader *) a;
7182 const int *rb = (const int *) b;
7183 return ra->cu->per_cu->index == *rb;
7184 }
7185
7186 /* The abbrev cache used by this indexer. */
7187 abbrev_cache m_abbrev_cache;
7188 /* A hash table of cutu_reader objects. */
7189 htab_up m_reader_hash;
7190 /* The index that is being constructed. */
7191 std::unique_ptr<cooked_index> m_index;
7192
7193 /* Storage for the writeable addrmap. */
7194 auto_obstack m_addrmap_storage;
7195 /* A writeable addrmap being constructed by this scanner. */
7196 addrmap *m_addrmap;
7197 };
7198
7199 /* An instance of this is created to index a CU. */
7200
7201 class cooked_indexer
7202 {
7203 public:
7204
7205 cooked_indexer (cooked_index_storage *storage,
7206 dwarf2_per_cu_data *per_cu,
7207 enum language language)
7208 : m_index_storage (storage),
7209 m_per_cu (per_cu),
7210 m_language (language),
7211 m_obstack (),
7212 m_die_range_map (addrmap_create_mutable (&m_obstack))
7213 {
7214 }
7215
7216 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
7217
7218 /* Index the given CU. */
7219 void make_index (cutu_reader *reader);
7220
7221 private:
7222
7223 /* A helper function to turn a section offset into an address that
7224 can be used in an addrmap. */
7225 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
7226 {
7227 CORE_ADDR value = to_underlying (offset);
7228 if (is_dwz)
7229 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
7230 return value;
7231 }
7232
7233 /* A helper function to scan the PC bounds of READER and record them
7234 in the storage's addrmap. */
7235 void check_bounds (cutu_reader *reader);
7236
7237 /* Ensure that the indicated CU exists. The cutu_reader for it is
7238 returned. FOR_SCANNING is true if the caller intends to scan all
7239 the DIEs in the CU; when false, this use is assumed to be to look
7240 up just a single DIE. */
7241 cutu_reader *ensure_cu_exists (cutu_reader *reader,
7242 dwarf2_per_objfile *per_objfile,
7243 sect_offset sect_off,
7244 bool is_dwz,
7245 bool for_scanning);
7246
7247 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
7248 the entry for the enclosing scope (nullptr at top level). FULLY
7249 is true when a full scan must be done -- in some languages,
7250 function scopes must be fully explored in order to find nested
7251 functions. This returns a pointer to just after the spot where
7252 reading stopped. */
7253 const gdb_byte *index_dies (cutu_reader *reader,
7254 const gdb_byte *info_ptr,
7255 const cooked_index_entry *parent_entry,
7256 bool fully);
7257
7258 /* Scan the attributes for a given DIE and update the out
7259 parameters. Returns a pointer to the byte after the DIE. */
7260 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
7261 cutu_reader *reader,
7262 const gdb_byte *watermark_ptr,
7263 const gdb_byte *info_ptr,
7264 const abbrev_info *abbrev,
7265 const char **name,
7266 const char **linkage_name,
7267 cooked_index_flag *flags,
7268 sect_offset *sibling_offset,
7269 const cooked_index_entry **parent_entry,
7270 CORE_ADDR *maybe_defer,
7271 bool for_specification);
7272
7273 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
7274 DW_AT_import, and then scanning the referenced CU. Returns a
7275 pointer to the byte after the DIE. */
7276 const gdb_byte *index_imported_unit (cutu_reader *reader,
7277 const gdb_byte *info_ptr,
7278 const abbrev_info *abbrev);
7279
7280 /* Recursively read DIEs, recording the section offsets in
7281 m_die_range_map and then calling index_dies. */
7282 const gdb_byte *recurse (cutu_reader *reader,
7283 const gdb_byte *info_ptr,
7284 const cooked_index_entry *parent_entry,
7285 bool fully);
7286
7287 /* The storage object, where the results are kept. */
7288 cooked_index_storage *m_index_storage;
7289 /* The CU that we are reading on behalf of. This object might be
7290 asked to index one CU but to treat the results as if they come
7291 from some including CU; in this case the including CU would be
7292 recorded here. */
7293 dwarf2_per_cu_data *m_per_cu;
7294 /* The language that we're assuming when reading. */
7295 enum language m_language;
7296
7297 /* Temporary storage. */
7298 auto_obstack m_obstack;
7299 /* An addrmap that maps from section offsets (see the form_addr
7300 method) to newly-created entries. See m_deferred_entries to
7301 understand this. */
7302 addrmap *m_die_range_map;
7303
7304 /* A single deferred entry. */
7305 struct deferred_entry
7306 {
7307 sect_offset die_offset;
7308 const char *name;
7309 CORE_ADDR spec_offset;
7310 dwarf_tag tag;
7311 cooked_index_flag flags;
7312 };
7313
7314 /* The generated DWARF can sometimes have the declaration for a
7315 method in a class (or perhaps namespace) scope, with the
7316 definition appearing outside this scope... just one of the many
7317 bad things about DWARF. In order to handle this situation, we
7318 defer certain entries until the end of scanning, at which point
7319 we'll know the containing context of all the DIEs that we might
7320 have scanned. This vector stores these deferred entries. */
7321 std::vector<deferred_entry> m_deferred_entries;
7322 };
7323
7324
7325
7326 /* DIE reader function for process_psymtab_comp_unit. */
7327
7328 static void
7329 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7330 const gdb_byte *info_ptr,
7331 struct die_info *comp_unit_die,
7332 enum language pretend_language)
7333 {
7334 struct dwarf2_cu *cu = reader->cu;
7335 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7336 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7337 struct objfile *objfile = per_objfile->objfile;
7338 struct gdbarch *gdbarch = objfile->arch ();
7339 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7340 CORE_ADDR baseaddr;
7341 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7342 dwarf2_psymtab *pst;
7343 enum pc_bounds_kind cu_bounds_kind;
7344
7345 gdb_assert (! per_cu->is_debug_types);
7346
7347 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7348
7349 /* Allocate a new partial symbol table structure. */
7350 static const char artificial[] = "<artificial>";
7351 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
7352 if (strcmp (fnd.get_name (), artificial) == 0)
7353 {
7354 gdb::unique_xmalloc_ptr<char> debug_filename
7355 (concat (artificial, "@",
7356 sect_offset_str (per_cu->sect_off),
7357 (char *) NULL));
7358 fnd.set_name (std::move (debug_filename));
7359 }
7360
7361 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7362
7363 /* This must be done before calling dwarf2_build_include_psymtabs. */
7364 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7365
7366 baseaddr = objfile->text_section_offset ();
7367
7368 dwarf2_find_base_address (comp_unit_die, cu);
7369
7370 /* Possibly set the default values of LOWPC and HIGHPC from
7371 `DW_AT_ranges'. */
7372 cu_bounds_kind
7373 = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7374 &best_highpc, cu,
7375 per_bfd->partial_symtabs->psymtabs_addrmap,
7376 pst);
7377 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7378 {
7379 CORE_ADDR low
7380 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7381 - baseaddr);
7382 CORE_ADDR high
7383 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7384 - baseaddr - 1);
7385 /* Store the contiguous range if it is not empty; it can be
7386 empty for CUs with no code. */
7387 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7388 low, high, pst);
7389 }
7390
7391 /* Check if comp unit has_children.
7392 If so, read the rest of the partial symbols from this comp unit.
7393 If not, there's no more debug_info for this comp unit. */
7394 if (comp_unit_die->has_children)
7395 {
7396 struct partial_die_info *first_die;
7397 CORE_ADDR lowpc, highpc;
7398
7399 lowpc = ((CORE_ADDR) -1);
7400 highpc = ((CORE_ADDR) 0);
7401
7402 first_die = load_partial_dies (reader, info_ptr, 1);
7403
7404 scan_partial_symbols (first_die, &lowpc, &highpc,
7405 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7406
7407 /* If we didn't find a lowpc, set it to highpc to avoid
7408 complaints from `maint check'. */
7409 if (lowpc == ((CORE_ADDR) -1))
7410 lowpc = highpc;
7411
7412 /* If the compilation unit didn't have an explicit address range,
7413 then use the information extracted from its child dies. */
7414 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7415 {
7416 best_lowpc = lowpc;
7417 best_highpc = highpc;
7418 }
7419 }
7420 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7421 best_lowpc + baseaddr)
7422 - baseaddr);
7423 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7424 best_highpc + baseaddr)
7425 - baseaddr);
7426
7427 pst->end ();
7428
7429 if (!cu->per_cu->imported_symtabs_empty ())
7430 {
7431 int i;
7432 int len = cu->per_cu->imported_symtabs_size ();
7433
7434 /* Fill in 'dependencies' here; we fill in 'users' in a
7435 post-pass. */
7436 pst->number_of_dependencies = len;
7437 pst->dependencies
7438 = per_bfd->partial_symtabs->allocate_dependencies (len);
7439 for (i = 0; i < len; ++i)
7440 {
7441 pst->dependencies[i]
7442 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7443 }
7444
7445 cu->per_cu->imported_symtabs_free ();
7446 }
7447
7448 /* Get the list of files included in the current compilation unit,
7449 and build a psymtab for each of them. */
7450 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7451
7452 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7453 ", %d global, %d static syms",
7454 per_cu->is_debug_types ? "type" : "comp",
7455 sect_offset_str (per_cu->sect_off),
7456 paddress (gdbarch, pst->text_low (objfile)),
7457 paddress (gdbarch, pst->text_high (objfile)),
7458 (int) pst->global_psymbols.size (),
7459 (int) pst->static_psymbols.size ());
7460 }
7461
7462 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7463 Process compilation unit THIS_CU for a psymtab. */
7464
7465 static void
7466 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7467 dwarf2_per_objfile *per_objfile,
7468 bool want_partial_unit,
7469 enum language pretend_language,
7470 cooked_index_storage *storage)
7471 {
7472 /* If this compilation unit was already read in, free the
7473 cached copy in order to read it in again. This is
7474 necessary because we skipped some symbols when we first
7475 read in the compilation unit (see load_partial_dies).
7476 This problem could be avoided, but the benefit is unclear. */
7477 if (!per_objfile->per_bfd->using_index)
7478 per_objfile->remove_cu (this_cu);
7479
7480 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
7481 (storage == nullptr
7482 ? nullptr
7483 : storage->get_abbrev_cache ()));
7484
7485 if (reader.comp_unit_die == nullptr)
7486 return;
7487
7488 switch (reader.comp_unit_die->tag)
7489 {
7490 case DW_TAG_compile_unit:
7491 this_cu->unit_type = DW_UT_compile;
7492 break;
7493 case DW_TAG_partial_unit:
7494 this_cu->unit_type = DW_UT_partial;
7495 break;
7496 case DW_TAG_type_unit:
7497 this_cu->unit_type = DW_UT_type;
7498 break;
7499 default:
7500 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7501 dwarf_tag_name (reader.comp_unit_die->tag),
7502 sect_offset_str (reader.cu->per_cu->sect_off),
7503 objfile_name (per_objfile->objfile));
7504 }
7505
7506 if (reader.dummy_p)
7507 {
7508 /* Nothing. */
7509 }
7510 else if (this_cu->is_debug_types)
7511 build_type_psymtabs_reader (&reader, storage);
7512 else if (want_partial_unit
7513 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7514 {
7515 if (per_objfile->per_bfd->using_index)
7516 {
7517 bool nope = false;
7518 if (this_cu->scanned.compare_exchange_strong (nope, true))
7519 {
7520 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7521 pretend_language);
7522 gdb_assert (storage != nullptr);
7523 cooked_indexer indexer (storage, this_cu,
7524 reader.cu->per_cu->lang);
7525 indexer.make_index (&reader);
7526 }
7527 }
7528 else
7529 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7530 reader.comp_unit_die,
7531 pretend_language);
7532 }
7533
7534 /* Age out any secondary CUs. */
7535 per_objfile->age_comp_units ();
7536 }
7537
7538 /* Reader function for build_type_psymtabs. */
7539
7540 static void
7541 build_type_psymtabs_reader (cutu_reader *reader,
7542 cooked_index_storage *storage)
7543 {
7544 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7545 struct dwarf2_cu *cu = reader->cu;
7546 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7547 struct signatured_type *sig_type;
7548 struct type_unit_group *tu_group;
7549 struct attribute *attr;
7550 struct partial_die_info *first_die;
7551 CORE_ADDR lowpc, highpc;
7552 dwarf2_psymtab *pst;
7553 const gdb_byte *info_ptr = reader->info_ptr;
7554 struct die_info *type_unit_die = reader->comp_unit_die;
7555
7556 gdb_assert (per_cu->is_debug_types);
7557 sig_type = (struct signatured_type *) per_cu;
7558
7559 if (! type_unit_die->has_children)
7560 return;
7561
7562 attr = type_unit_die->attr (DW_AT_stmt_list);
7563 tu_group = get_type_unit_group (cu, attr);
7564
7565 if (tu_group->tus == nullptr)
7566 tu_group->tus = new std::vector<signatured_type *>;
7567 tu_group->tus->push_back (sig_type);
7568
7569 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7570
7571 if (per_objfile->per_bfd->using_index)
7572 {
7573 gdb_assert (storage != nullptr);
7574 cooked_indexer indexer (storage, per_cu, cu->per_cu->lang);
7575 indexer.make_index (reader);
7576 }
7577 else
7578 {
7579 pst = create_partial_symtab (per_cu, per_objfile, "");
7580 pst->anonymous = true;
7581
7582 first_die = load_partial_dies (reader, info_ptr, 1);
7583
7584 lowpc = (CORE_ADDR) -1;
7585 highpc = (CORE_ADDR) 0;
7586 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7587
7588 pst->end ();
7589 }
7590 }
7591
7592 /* Struct used to sort TUs by their abbreviation table offset. */
7593
7594 struct tu_abbrev_offset
7595 {
7596 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7597 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7598 {}
7599
7600 /* This is used when sorting. */
7601 bool operator< (const tu_abbrev_offset &other) const
7602 {
7603 return abbrev_offset < other.abbrev_offset;
7604 }
7605
7606 signatured_type *sig_type;
7607 sect_offset abbrev_offset;
7608 };
7609
7610 /* Efficiently read all the type units.
7611
7612 The efficiency is because we sort TUs by the abbrev table they use and
7613 only read each abbrev table once. In one program there are 200K TUs
7614 sharing 8K abbrev tables.
7615
7616 The main purpose of this function is to support building the
7617 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7618 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7619 can collapse the search space by grouping them by stmt_list.
7620 The savings can be significant, in the same program from above the 200K TUs
7621 share 8K stmt_list tables.
7622
7623 FUNC is expected to call get_type_unit_group, which will create the
7624 struct type_unit_group if necessary and add it to
7625 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7626
7627 static void
7628 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
7629 cooked_index_storage *storage)
7630 {
7631 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7632 abbrev_table_up abbrev_table;
7633 sect_offset abbrev_offset;
7634
7635 /* It's up to the caller to not call us multiple times. */
7636 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7637
7638 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7639 return;
7640
7641 /* TUs typically share abbrev tables, and there can be way more TUs than
7642 abbrev tables. Sort by abbrev table to reduce the number of times we
7643 read each abbrev table in.
7644 Alternatives are to punt or to maintain a cache of abbrev tables.
7645 This is simpler and efficient enough for now.
7646
7647 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7648 symtab to use). Typically TUs with the same abbrev offset have the same
7649 stmt_list value too so in practice this should work well.
7650
7651 The basic algorithm here is:
7652
7653 sort TUs by abbrev table
7654 for each TU with same abbrev table:
7655 read abbrev table if first user
7656 read TU top level DIE
7657 [IWBN if DWO skeletons had DW_AT_stmt_list]
7658 call FUNC */
7659
7660 dwarf_read_debug_printf ("Building type unit groups ...");
7661
7662 /* Sort in a separate table to maintain the order of all_comp_units
7663 for .gdb_index: TU indices directly index all_type_units. */
7664 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7665 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7666
7667 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7668 {
7669 if (cu->is_debug_types)
7670 {
7671 auto sig_type = static_cast<signatured_type *> (cu.get ());
7672 sorted_by_abbrev.emplace_back
7673 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7674 sig_type->sect_off));
7675 }
7676 }
7677
7678 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7679
7680 abbrev_offset = (sect_offset) ~(unsigned) 0;
7681
7682 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7683 {
7684 /* Switch to the next abbrev table if necessary. */
7685 if (abbrev_table == NULL
7686 || tu.abbrev_offset != abbrev_offset)
7687 {
7688 abbrev_offset = tu.abbrev_offset;
7689 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7690 abbrev_table =
7691 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7692 ++tu_stats->nr_uniq_abbrev_tables;
7693 }
7694
7695 cutu_reader reader (tu.sig_type, per_objfile,
7696 abbrev_table.get (), nullptr, false);
7697 if (!reader.dummy_p)
7698 build_type_psymtabs_reader (&reader, storage);
7699 }
7700 }
7701
7702 /* Print collected type unit statistics. */
7703
7704 static void
7705 print_tu_stats (dwarf2_per_objfile *per_objfile)
7706 {
7707 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7708
7709 dwarf_read_debug_printf ("Type unit statistics:");
7710 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7711 dwarf_read_debug_printf (" %d uniq abbrev tables",
7712 tu_stats->nr_uniq_abbrev_tables);
7713 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7714 tu_stats->nr_symtabs);
7715 dwarf_read_debug_printf (" %d symtab sharers",
7716 tu_stats->nr_symtab_sharers);
7717 dwarf_read_debug_printf (" %d type units without a stmt_list",
7718 tu_stats->nr_stmt_less_type_units);
7719 dwarf_read_debug_printf (" %d all_type_units reallocs",
7720 tu_stats->nr_all_type_units_reallocs);
7721 }
7722
7723 /* Traversal function for build_type_psymtabs. */
7724
7725 static int
7726 build_type_psymtab_dependencies (void **slot, void *info)
7727 {
7728 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7729 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7730 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7731 dwarf2_psymtab *pst = tu_group->v.psymtab;
7732 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7733 int i;
7734
7735 gdb_assert (len > 0);
7736 gdb_assert (tu_group->type_unit_group_p ());
7737
7738 pst->number_of_dependencies = len;
7739 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7740 for (i = 0; i < len; ++i)
7741 {
7742 struct signatured_type *iter = tu_group->tus->at (i);
7743 gdb_assert (iter->is_debug_types);
7744 pst->dependencies[i] = iter->v.psymtab;
7745 iter->type_unit_group = tu_group;
7746 }
7747
7748 delete tu_group->tus;
7749 tu_group->tus = nullptr;
7750
7751 return 1;
7752 }
7753
7754 struct skeleton_data
7755 {
7756 dwarf2_per_objfile *per_objfile;
7757 cooked_index_storage *storage;
7758 };
7759
7760 /* Traversal function for process_skeletonless_type_unit.
7761 Read a TU in a DWO file and build partial symbols for it. */
7762
7763 static int
7764 process_skeletonless_type_unit (void **slot, void *info)
7765 {
7766 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7767 skeleton_data *data = (skeleton_data *) info;
7768
7769 /* If this TU doesn't exist in the global table, add it and read it in. */
7770
7771 if (data->per_objfile->per_bfd->signatured_types == NULL)
7772 data->per_objfile->per_bfd->signatured_types
7773 = allocate_signatured_type_table ();
7774
7775 signatured_type find_entry (dwo_unit->signature);
7776 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
7777 &find_entry, INSERT);
7778 /* If we've already seen this type there's nothing to do. What's happening
7779 is we're doing our own version of comdat-folding here. */
7780 if (*slot != NULL)
7781 return 1;
7782
7783 /* This does the job that create_all_comp_units would have done for
7784 this TU. */
7785 signatured_type *entry
7786 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7787 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7788 *slot = entry;
7789
7790 /* This does the job that build_type_psymtabs would have done. */
7791 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7792 if (!reader.dummy_p)
7793 build_type_psymtabs_reader (&reader, data->storage);
7794
7795 return 1;
7796 }
7797
7798 /* Traversal function for process_skeletonless_type_units. */
7799
7800 static int
7801 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7802 {
7803 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7804
7805 if (dwo_file->tus != NULL)
7806 htab_traverse_noresize (dwo_file->tus.get (),
7807 process_skeletonless_type_unit, info);
7808
7809 return 1;
7810 }
7811
7812 /* Scan all TUs of DWO files, verifying we've processed them.
7813 This is needed in case a TU was emitted without its skeleton.
7814 Note: This can't be done until we know what all the DWO files are. */
7815
7816 static void
7817 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7818 cooked_index_storage *storage)
7819 {
7820 skeleton_data data { per_objfile, storage };
7821
7822 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7823 if (get_dwp_file (per_objfile) == NULL
7824 && per_objfile->per_bfd->dwo_files != NULL)
7825 {
7826 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7827 process_dwo_file_for_skeletonless_type_units,
7828 &data);
7829 }
7830 }
7831
7832 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7833
7834 static void
7835 set_partial_user (dwarf2_per_objfile *per_objfile)
7836 {
7837 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7838 {
7839 dwarf2_psymtab *pst = per_cu->v.psymtab;
7840
7841 if (pst == NULL)
7842 continue;
7843
7844 for (int j = 0; j < pst->number_of_dependencies; ++j)
7845 {
7846 /* Set the 'user' field only if it is not already set. */
7847 if (pst->dependencies[j]->user == NULL)
7848 pst->dependencies[j]->user = pst;
7849 }
7850 }
7851 }
7852
7853 /* Build the partial symbol table by doing a quick pass through the
7854 .debug_info and .debug_abbrev sections. */
7855
7856 static void
7857 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7858 {
7859 struct objfile *objfile = per_objfile->objfile;
7860 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7861
7862 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7863 objfile_name (objfile));
7864
7865 scoped_restore restore_reading_psyms
7866 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7867
7868 per_bfd->map_info_sections (objfile);
7869
7870 /* Any cached compilation units will be linked by the per-objfile
7871 read_in_chain. Make sure to free them when we're done. */
7872 free_cached_comp_units freer (per_objfile);
7873
7874 /* Create a temporary address map on a temporary obstack. We later
7875 copy this to the final obstack. */
7876 auto_obstack temp_obstack;
7877
7878 scoped_restore save_psymtabs_addrmap
7879 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7880 addrmap_create_mutable (&temp_obstack));
7881
7882 cooked_index_storage index_storage;
7883 cooked_index_storage *index_storage_ptr
7884 = per_bfd->using_index ? &index_storage : nullptr;
7885 create_all_comp_units (per_objfile);
7886 build_type_psymtabs (per_objfile, index_storage_ptr);
7887 std::vector<std::unique_ptr<cooked_index>> indexes;
7888 if (per_bfd->using_index)
7889 {
7890 per_bfd->quick_file_names_table
7891 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
7892
7893 if (!per_bfd->debug_aranges.empty ())
7894 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7895 index_storage.get_addrmap ());
7896
7897 {
7898 /* Ensure that complaints are handled correctly. */
7899 complaint_interceptor complaint_handler;
7900
7901 using iter_type = typeof (per_bfd->all_comp_units.begin ());
7902
7903 /* Each thread returns a pair holding a cooked index, and a vector
7904 of errors that should be printed. The latter is done because
7905 GDB's I/O system is not thread-safe. run_on_main_thread could be
7906 used, but that would mean the messages are printed after the
7907 prompt, which looks weird. */
7908 using result_type = std::pair<std::unique_ptr<cooked_index>,
7909 std::vector<gdb_exception>>;
7910 std::vector<result_type> results
7911 = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
7912 per_bfd->all_comp_units.end (),
7913 [=] (iter_type iter, iter_type end)
7914 {
7915 std::vector<gdb_exception> errors;
7916 cooked_index_storage thread_storage;
7917 for (; iter != end; ++iter)
7918 {
7919 dwarf2_per_cu_data *per_cu = iter->get ();
7920 try
7921 {
7922 process_psymtab_comp_unit (per_cu, per_objfile,
7923 false,
7924 language_minimal,
7925 &thread_storage);
7926 }
7927 catch (gdb_exception &except)
7928 {
7929 errors.push_back (std::move (except));
7930 }
7931 }
7932 return result_type (thread_storage.release (),
7933 std::move (errors));
7934 });
7935
7936 /* Only show a given exception a single time. */
7937 std::unordered_set<gdb_exception> seen_exceptions;
7938 for (auto &one_result : results)
7939 {
7940 indexes.push_back (std::move (one_result.first));
7941 for (auto &one_exc : one_result.second)
7942 {
7943 if (seen_exceptions.insert (one_exc).second)
7944 exception_print (gdb_stderr, one_exc);
7945 }
7946 }
7947 }
7948 }
7949 else
7950 {
7951 for (const auto &per_cu : per_bfd->all_comp_units)
7952 {
7953 if (!per_bfd->using_index && per_cu->v.psymtab != NULL)
7954 /* In case a forward DW_TAG_imported_unit has read the CU
7955 already. */
7956 continue;
7957 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7958 language_minimal, nullptr);
7959 }
7960 }
7961
7962 /* This has to wait until we read the CUs, we need the list of DWOs. */
7963 process_skeletonless_type_units (per_objfile, &index_storage);
7964
7965 /* Now that all TUs have been processed we can fill in the dependencies. */
7966 if (!per_bfd->using_index && per_bfd->type_unit_groups != NULL)
7967 {
7968 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7969 build_type_psymtab_dependencies, per_objfile);
7970 }
7971
7972 if (dwarf_read_debug > 0)
7973 print_tu_stats (per_objfile);
7974
7975 if (per_bfd->using_index)
7976 {
7977 indexes.push_back (index_storage.release ());
7978 /* Remove any NULL entries. This might happen if parallel-for
7979 decides to throttle the number of threads that were used. */
7980 indexes.erase
7981 (std::remove_if (indexes.begin (),
7982 indexes.end (),
7983 [] (const std::unique_ptr<cooked_index> &entry)
7984 {
7985 return entry == nullptr;
7986 }),
7987 indexes.end ());
7988 indexes.shrink_to_fit ();
7989 per_bfd->cooked_index_table.reset
7990 (new cooked_index_vector (std::move (indexes)));
7991
7992 const cooked_index_entry *main_entry
7993 = per_bfd->cooked_index_table->get_main ();
7994 if (main_entry != nullptr)
7995 set_objfile_main_name (objfile, main_entry->name,
7996 main_entry->per_cu->lang);
7997 }
7998 else
7999 {
8000 set_partial_user (per_objfile);
8001
8002 per_bfd->partial_symtabs->psymtabs_addrmap
8003 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8004 per_bfd->partial_symtabs->obstack ());
8005 /* At this point we want to keep the address map. */
8006 save_psymtabs_addrmap.release ();
8007 }
8008 dwarf_read_debug_printf ("Done building psymtabs of %s",
8009 objfile_name (objfile));
8010 }
8011
8012 /* Load the partial DIEs for a secondary CU into memory.
8013 This is also used when rereading a primary CU with load_all_dies. */
8014
8015 static void
8016 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8017 dwarf2_per_objfile *per_objfile,
8018 dwarf2_cu *existing_cu)
8019 {
8020 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8021
8022 if (!reader.dummy_p)
8023 {
8024 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8025 language_minimal);
8026
8027 /* Check if comp unit has_children.
8028 If so, read the rest of the partial symbols from this comp unit.
8029 If not, there's no more debug_info for this comp unit. */
8030 if (reader.comp_unit_die->has_children)
8031 load_partial_dies (&reader, reader.info_ptr, 0);
8032
8033 reader.keep ();
8034 }
8035 }
8036
8037 static void
8038 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8039 struct dwarf2_section_info *section,
8040 struct dwarf2_section_info *abbrev_section,
8041 unsigned int is_dwz,
8042 htab_up &types_htab,
8043 rcuh_kind section_kind)
8044 {
8045 const gdb_byte *info_ptr;
8046 struct objfile *objfile = per_objfile->objfile;
8047
8048 dwarf_read_debug_printf ("Reading %s for %s",
8049 section->get_name (),
8050 section->get_file_name ());
8051
8052 section->read (objfile);
8053
8054 info_ptr = section->buffer;
8055
8056 while (info_ptr < section->buffer + section->size)
8057 {
8058 dwarf2_per_cu_data_up this_cu;
8059
8060 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8061
8062 comp_unit_head cu_header;
8063 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8064 abbrev_section, info_ptr,
8065 section_kind);
8066
8067 /* Save the compilation unit for later lookup. */
8068 if (cu_header.unit_type != DW_UT_type)
8069 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8070 else
8071 {
8072 if (types_htab == nullptr)
8073 types_htab = allocate_signatured_type_table ();
8074
8075 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
8076 (cu_header.signature);
8077 signatured_type *sig_ptr = sig_type.get ();
8078 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8079 this_cu.reset (sig_type.release ());
8080
8081 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
8082 gdb_assert (slot != nullptr);
8083 if (*slot != nullptr)
8084 complaint (_("debug type entry at offset %s is duplicate to"
8085 " the entry at offset %s, signature %s"),
8086 sect_offset_str (sect_off),
8087 sect_offset_str (sig_ptr->sect_off),
8088 hex_string (sig_ptr->signature));
8089 *slot = sig_ptr;
8090 }
8091 this_cu->sect_off = sect_off;
8092 this_cu->length = cu_header.length + cu_header.initial_length_size;
8093 this_cu->is_dwz = is_dwz;
8094 this_cu->section = section;
8095
8096 if (per_objfile->per_bfd->using_index)
8097 this_cu->v.quick = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
8098 struct dwarf2_per_cu_quick_data);
8099
8100 info_ptr = info_ptr + this_cu->length;
8101 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
8102 }
8103 }
8104
8105 /* Create a list of all compilation units in OBJFILE.
8106 This is only done for -readnow and building partial symtabs. */
8107
8108 static void
8109 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8110 {
8111 htab_up types_htab;
8112
8113 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8114 &per_objfile->per_bfd->abbrev, 0,
8115 types_htab, rcuh_kind::COMPILE);
8116 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
8117 read_comp_units_from_section (per_objfile, &section,
8118 &per_objfile->per_bfd->abbrev, 0,
8119 types_htab, rcuh_kind::TYPE);
8120
8121 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8122 if (dwz != NULL)
8123 {
8124 /* Pre-read the sections we'll need to construct an index. */
8125 struct objfile *objfile = per_objfile->objfile;
8126 dwz->abbrev.read (objfile);
8127 dwz->info.read (objfile);
8128 dwz->str.read (objfile);
8129 dwz->line.read (objfile);
8130 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
8131 types_htab, rcuh_kind::COMPILE);
8132 }
8133
8134 per_objfile->per_bfd->signatured_types = std::move (types_htab);
8135 }
8136
8137 /* Process all loaded DIEs for compilation unit CU, starting at
8138 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8139 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8140 DW_AT_ranges). See the comments of add_partial_subprogram on how
8141 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8142
8143 static void
8144 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8145 CORE_ADDR *highpc, int set_addrmap,
8146 struct dwarf2_cu *cu)
8147 {
8148 struct partial_die_info *pdi;
8149
8150 /* Now, march along the PDI's, descending into ones which have
8151 interesting children but skipping the children of the other ones,
8152 until we reach the end of the compilation unit. */
8153
8154 pdi = first_die;
8155
8156 while (pdi != NULL)
8157 {
8158 pdi->fixup (cu);
8159
8160 /* Anonymous namespaces or modules have no name but have interesting
8161 children, so we need to look at them. Ditto for anonymous
8162 enums. */
8163
8164 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8165 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8166 || pdi->tag == DW_TAG_imported_unit
8167 || pdi->tag == DW_TAG_inlined_subroutine)
8168 {
8169 switch (pdi->tag)
8170 {
8171 case DW_TAG_subprogram:
8172 case DW_TAG_inlined_subroutine:
8173 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8174 if (cu->per_cu->lang == language_cplus)
8175 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8176 set_addrmap, cu);
8177 break;
8178 case DW_TAG_constant:
8179 case DW_TAG_variable:
8180 case DW_TAG_typedef:
8181 case DW_TAG_union_type:
8182 if (!pdi->is_declaration
8183 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8184 {
8185 add_partial_symbol (pdi, cu);
8186 }
8187 break;
8188 case DW_TAG_class_type:
8189 case DW_TAG_interface_type:
8190 case DW_TAG_structure_type:
8191 if (!pdi->is_declaration)
8192 {
8193 add_partial_symbol (pdi, cu);
8194 }
8195 if ((cu->per_cu->lang == language_rust
8196 || cu->per_cu->lang == language_cplus)
8197 && pdi->has_children)
8198 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8199 set_addrmap, cu);
8200 break;
8201 case DW_TAG_enumeration_type:
8202 if (!pdi->is_declaration)
8203 add_partial_enumeration (pdi, cu);
8204 break;
8205 case DW_TAG_base_type:
8206 case DW_TAG_generic_subrange:
8207 case DW_TAG_subrange_type:
8208 /* File scope base type definitions are added to the partial
8209 symbol table. */
8210 add_partial_symbol (pdi, cu);
8211 break;
8212 case DW_TAG_namespace:
8213 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8214 break;
8215 case DW_TAG_module:
8216 if (!pdi->is_declaration)
8217 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8218 break;
8219 case DW_TAG_imported_unit:
8220 {
8221 struct dwarf2_per_cu_data *per_cu;
8222
8223 /* For now we don't handle imported units in type units. */
8224 if (cu->per_cu->is_debug_types)
8225 {
8226 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8227 " supported in type units [in module %s]"),
8228 objfile_name (cu->per_objfile->objfile));
8229 }
8230
8231 per_cu = dwarf2_find_containing_comp_unit
8232 (pdi->d.sect_off, pdi->is_dwz,
8233 cu->per_objfile->per_bfd);
8234
8235 /* Go read the partial unit, if needed. */
8236 if (per_cu->v.psymtab == NULL)
8237 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8238 cu->per_cu->lang, nullptr);
8239
8240 if (pdi->die_parent == nullptr
8241 && per_cu->unit_type == DW_UT_compile
8242 && per_cu->lang == language_cplus)
8243 /* Regard import as hint. See corresponding code in
8244 process_imported_unit_die. */
8245 break;
8246
8247 cu->per_cu->imported_symtabs_push (per_cu);
8248 }
8249 break;
8250 case DW_TAG_imported_declaration:
8251 add_partial_symbol (pdi, cu);
8252 break;
8253 default:
8254 break;
8255 }
8256 }
8257
8258 /* If the die has a sibling, skip to the sibling. */
8259
8260 pdi = pdi->die_sibling;
8261 }
8262 }
8263
8264 /* Functions used to compute the fully scoped name of a partial DIE.
8265
8266 Normally, this is simple. For C++, the parent DIE's fully scoped
8267 name is concatenated with "::" and the partial DIE's name.
8268 Enumerators are an exception; they use the scope of their parent
8269 enumeration type, i.e. the name of the enumeration type is not
8270 prepended to the enumerator.
8271
8272 There are two complexities. One is DW_AT_specification; in this
8273 case "parent" means the parent of the target of the specification,
8274 instead of the direct parent of the DIE. The other is compilers
8275 which do not emit DW_TAG_namespace; in this case we try to guess
8276 the fully qualified name of structure types from their members'
8277 linkage names. This must be done using the DIE's children rather
8278 than the children of any DW_AT_specification target. We only need
8279 to do this for structures at the top level, i.e. if the target of
8280 any DW_AT_specification (if any; otherwise the DIE itself) does not
8281 have a parent. */
8282
8283 /* Compute the scope prefix associated with PDI's parent, in
8284 compilation unit CU. The result will be allocated on CU's
8285 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8286 field. NULL is returned if no prefix is necessary. */
8287 static const char *
8288 partial_die_parent_scope (struct partial_die_info *pdi,
8289 struct dwarf2_cu *cu)
8290 {
8291 const char *grandparent_scope;
8292 struct partial_die_info *parent, *real_pdi;
8293
8294 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8295 then this means the parent of the specification DIE. */
8296
8297 real_pdi = pdi;
8298 while (real_pdi->has_specification)
8299 {
8300 auto res = find_partial_die (real_pdi->spec_offset,
8301 real_pdi->spec_is_dwz, cu);
8302 real_pdi = res.pdi;
8303 cu = res.cu;
8304 }
8305
8306 parent = real_pdi->die_parent;
8307 if (parent == NULL)
8308 return NULL;
8309
8310 if (parent->scope_set)
8311 return parent->scope;
8312
8313 parent->fixup (cu);
8314
8315 grandparent_scope = partial_die_parent_scope (parent, cu);
8316
8317 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8318 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8319 Work around this problem here. */
8320 if (cu->per_cu->lang == language_cplus
8321 && parent->tag == DW_TAG_namespace
8322 && strcmp (parent->name (cu), "::") == 0
8323 && grandparent_scope == NULL)
8324 {
8325 parent->scope = NULL;
8326 parent->scope_set = 1;
8327 return NULL;
8328 }
8329
8330 /* Nested subroutines in Fortran get a prefix. */
8331 if (pdi->tag == DW_TAG_enumerator)
8332 /* Enumerators should not get the name of the enumeration as a prefix. */
8333 parent->scope = grandparent_scope;
8334 else if (parent->tag == DW_TAG_namespace
8335 || parent->tag == DW_TAG_module
8336 || parent->tag == DW_TAG_structure_type
8337 || parent->tag == DW_TAG_class_type
8338 || parent->tag == DW_TAG_interface_type
8339 || parent->tag == DW_TAG_union_type
8340 || parent->tag == DW_TAG_enumeration_type
8341 || (cu->per_cu->lang == language_fortran
8342 && parent->tag == DW_TAG_subprogram
8343 && pdi->tag == DW_TAG_subprogram))
8344 {
8345 if (grandparent_scope == NULL)
8346 parent->scope = parent->name (cu);
8347 else
8348 parent->scope = typename_concat (&cu->comp_unit_obstack,
8349 grandparent_scope,
8350 parent->name (cu), 0, cu);
8351 }
8352 else
8353 {
8354 /* FIXME drow/2004-04-01: What should we be doing with
8355 function-local names? For partial symbols, we should probably be
8356 ignoring them. */
8357 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8358 dwarf_tag_name (parent->tag),
8359 sect_offset_str (pdi->sect_off));
8360 parent->scope = grandparent_scope;
8361 }
8362
8363 parent->scope_set = 1;
8364 return parent->scope;
8365 }
8366
8367 /* Return the fully scoped name associated with PDI, from compilation unit
8368 CU. The result will be allocated with malloc. */
8369
8370 static gdb::unique_xmalloc_ptr<char>
8371 partial_die_full_name (struct partial_die_info *pdi,
8372 struct dwarf2_cu *cu)
8373 {
8374 const char *parent_scope;
8375
8376 /* If this is a template instantiation, we can not work out the
8377 template arguments from partial DIEs. So, unfortunately, we have
8378 to go through the full DIEs. At least any work we do building
8379 types here will be reused if full symbols are loaded later. */
8380 if (pdi->has_template_arguments)
8381 {
8382 pdi->fixup (cu);
8383
8384 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8385 {
8386 struct die_info *die;
8387 struct attribute attr;
8388 struct dwarf2_cu *ref_cu = cu;
8389
8390 /* DW_FORM_ref_addr is using section offset. */
8391 attr.name = (enum dwarf_attribute) 0;
8392 attr.form = DW_FORM_ref_addr;
8393 attr.u.unsnd = to_underlying (pdi->sect_off);
8394 die = follow_die_ref (NULL, &attr, &ref_cu);
8395
8396 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8397 }
8398 }
8399
8400 parent_scope = partial_die_parent_scope (pdi, cu);
8401 if (parent_scope == NULL)
8402 return NULL;
8403 else
8404 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8405 pdi->name (cu),
8406 0, cu));
8407 }
8408
8409 static void
8410 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8411 {
8412 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8413 struct objfile *objfile = per_objfile->objfile;
8414 struct gdbarch *gdbarch = objfile->arch ();
8415 CORE_ADDR addr = 0;
8416 const char *actual_name = NULL;
8417 CORE_ADDR baseaddr;
8418
8419 baseaddr = objfile->text_section_offset ();
8420
8421 gdb::unique_xmalloc_ptr<char> built_actual_name
8422 = partial_die_full_name (pdi, cu);
8423 if (built_actual_name != NULL)
8424 actual_name = built_actual_name.get ();
8425
8426 if (actual_name == NULL)
8427 actual_name = pdi->name (cu);
8428
8429 partial_symbol psymbol;
8430 memset (&psymbol, 0, sizeof (psymbol));
8431 psymbol.ginfo.set_language (cu->per_cu->lang,
8432 &objfile->objfile_obstack);
8433 psymbol.ginfo.set_section_index (-1);
8434
8435 /* The code below indicates that the psymbol should be installed by
8436 setting this. */
8437 gdb::optional<psymbol_placement> where;
8438
8439 switch (pdi->tag)
8440 {
8441 case DW_TAG_inlined_subroutine:
8442 case DW_TAG_subprogram:
8443 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8444 - baseaddr);
8445 if (pdi->is_external
8446 || cu->per_cu->lang == language_ada
8447 || (cu->per_cu->lang == language_fortran
8448 && pdi->die_parent != NULL
8449 && pdi->die_parent->tag == DW_TAG_subprogram))
8450 {
8451 /* Normally, only "external" DIEs are part of the global scope.
8452 But in Ada and Fortran, we want to be able to access nested
8453 procedures globally. So all Ada and Fortran subprograms are
8454 stored in the global scope. */
8455 where = psymbol_placement::GLOBAL;
8456 }
8457 else
8458 where = psymbol_placement::STATIC;
8459
8460 psymbol.domain = VAR_DOMAIN;
8461 psymbol.aclass = LOC_BLOCK;
8462 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8463 psymbol.ginfo.set_value_address (addr);
8464
8465 if (pdi->main_subprogram && actual_name != NULL)
8466 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
8467 break;
8468 case DW_TAG_constant:
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_STATIC;
8471 where = (pdi->is_external
8472 ? psymbol_placement::GLOBAL
8473 : psymbol_placement::STATIC);
8474 break;
8475 case DW_TAG_variable:
8476 if (pdi->d.locdesc)
8477 addr = decode_locdesc (pdi->d.locdesc, cu);
8478
8479 if (pdi->d.locdesc
8480 && addr == 0
8481 && !per_objfile->per_bfd->has_section_at_zero)
8482 {
8483 /* A global or static variable may also have been stripped
8484 out by the linker if unused, in which case its address
8485 will be nullified; do not add such variables into partial
8486 symbol table then. */
8487 }
8488 else if (pdi->is_external)
8489 {
8490 /* Global Variable.
8491 Don't enter into the minimal symbol tables as there is
8492 a minimal symbol table entry from the ELF symbols already.
8493 Enter into partial symbol table if it has a location
8494 descriptor or a type.
8495 If the location descriptor is missing, new_symbol will create
8496 a LOC_UNRESOLVED symbol, the address of the variable will then
8497 be determined from the minimal symbol table whenever the variable
8498 is referenced.
8499 The address for the partial symbol table entry is not
8500 used by GDB, but it comes in handy for debugging partial symbol
8501 table building. */
8502
8503 if (pdi->d.locdesc || pdi->has_type)
8504 {
8505 psymbol.domain = VAR_DOMAIN;
8506 psymbol.aclass = LOC_STATIC;
8507 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8508 psymbol.ginfo.set_value_address (addr);
8509 where = psymbol_placement::GLOBAL;
8510 }
8511 }
8512 else
8513 {
8514 int has_loc = pdi->d.locdesc != NULL;
8515
8516 /* Static Variable. Skip symbols whose value we cannot know (those
8517 without location descriptors or constant values). */
8518 if (!has_loc && !pdi->has_const_value)
8519 return;
8520
8521 psymbol.domain = VAR_DOMAIN;
8522 psymbol.aclass = LOC_STATIC;
8523 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8524 if (has_loc)
8525 psymbol.ginfo.set_value_address (addr);
8526 where = psymbol_placement::STATIC;
8527 }
8528 break;
8529 case DW_TAG_array_type:
8530 case DW_TAG_typedef:
8531 case DW_TAG_base_type:
8532 case DW_TAG_subrange_type:
8533 case DW_TAG_generic_subrange:
8534 psymbol.domain = VAR_DOMAIN;
8535 psymbol.aclass = LOC_TYPEDEF;
8536 where = psymbol_placement::STATIC;
8537 break;
8538 case DW_TAG_imported_declaration:
8539 case DW_TAG_namespace:
8540 psymbol.domain = VAR_DOMAIN;
8541 psymbol.aclass = LOC_TYPEDEF;
8542 where = psymbol_placement::GLOBAL;
8543 break;
8544 case DW_TAG_module:
8545 /* With Fortran 77 there might be a "BLOCK DATA" module
8546 available without any name. If so, we skip the module as it
8547 doesn't bring any value. */
8548 if (actual_name != nullptr)
8549 {
8550 psymbol.domain = MODULE_DOMAIN;
8551 psymbol.aclass = LOC_TYPEDEF;
8552 where = psymbol_placement::GLOBAL;
8553 }
8554 break;
8555 case DW_TAG_class_type:
8556 case DW_TAG_interface_type:
8557 case DW_TAG_structure_type:
8558 case DW_TAG_union_type:
8559 case DW_TAG_enumeration_type:
8560 /* Skip external references. The DWARF standard says in the section
8561 about "Structure, Union, and Class Type Entries": "An incomplete
8562 structure, union or class type is represented by a structure,
8563 union or class entry that does not have a byte size attribute
8564 and that has a DW_AT_declaration attribute." */
8565 if (!pdi->has_byte_size && pdi->is_declaration)
8566 return;
8567
8568 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8569 static vs. global. */
8570 psymbol.domain = STRUCT_DOMAIN;
8571 psymbol.aclass = LOC_TYPEDEF;
8572 where = (cu->per_cu->lang == language_cplus
8573 ? psymbol_placement::GLOBAL
8574 : psymbol_placement::STATIC);
8575 break;
8576 case DW_TAG_enumerator:
8577 psymbol.domain = VAR_DOMAIN;
8578 psymbol.aclass = LOC_CONST;
8579 where = (cu->per_cu->lang == language_cplus
8580 ? psymbol_placement::GLOBAL
8581 : psymbol_placement::STATIC);
8582 break;
8583 default:
8584 break;
8585 }
8586
8587 if (where.has_value ())
8588 {
8589 if (built_actual_name != nullptr)
8590 actual_name = objfile->intern (actual_name);
8591 if (pdi->linkage_name == nullptr
8592 || cu->per_cu->lang == language_ada)
8593 psymbol.ginfo.set_linkage_name (actual_name);
8594 else
8595 {
8596 psymbol.ginfo.set_demangled_name (actual_name,
8597 &objfile->objfile_obstack);
8598 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8599 }
8600 cu->per_cu->v.psymtab->add_psymbol
8601 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8602 objfile);
8603 }
8604 }
8605
8606 /* Read a partial die corresponding to a namespace; also, add a symbol
8607 corresponding to that namespace to the symbol table. NAMESPACE is
8608 the name of the enclosing namespace. */
8609
8610 static void
8611 add_partial_namespace (struct partial_die_info *pdi,
8612 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8613 int set_addrmap, struct dwarf2_cu *cu)
8614 {
8615 /* Add a symbol for the namespace. */
8616
8617 add_partial_symbol (pdi, cu);
8618
8619 /* Now scan partial symbols in that namespace. */
8620
8621 if (pdi->has_children)
8622 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8623 }
8624
8625 /* Read a partial die corresponding to a Fortran module. */
8626
8627 static void
8628 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8629 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8630 {
8631 /* Add a symbol for the namespace. */
8632
8633 add_partial_symbol (pdi, cu);
8634
8635 /* Now scan partial symbols in that module. */
8636
8637 if (pdi->has_children)
8638 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8639 }
8640
8641 static int dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8642 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8643 addrmap *map, void *datum, dwarf_tag tag);
8644
8645 /* Read a partial die corresponding to a subprogram or an inlined
8646 subprogram and create a partial symbol for that subprogram.
8647 When the CU language allows it, this routine also defines a partial
8648 symbol for each nested subprogram that this subprogram contains.
8649 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8650 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8651
8652 PDI may also be a lexical block, in which case we simply search
8653 recursively for subprograms defined inside that lexical block.
8654 Again, this is only performed when the CU language allows this
8655 type of definitions. */
8656
8657 static void
8658 add_partial_subprogram (struct partial_die_info *pdi,
8659 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8660 int set_addrmap, struct dwarf2_cu *cu)
8661 {
8662 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8663 {
8664 if (pdi->has_pc_info)
8665 {
8666 if (pdi->lowpc < *lowpc)
8667 *lowpc = pdi->lowpc;
8668 if (pdi->highpc > *highpc)
8669 *highpc = pdi->highpc;
8670 if (set_addrmap)
8671 {
8672 struct objfile *objfile = cu->per_objfile->objfile;
8673 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8674 struct gdbarch *gdbarch = objfile->arch ();
8675 CORE_ADDR baseaddr;
8676 CORE_ADDR this_highpc;
8677 CORE_ADDR this_lowpc;
8678
8679 baseaddr = objfile->text_section_offset ();
8680 this_lowpc
8681 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8682 pdi->lowpc + baseaddr)
8683 - baseaddr);
8684 this_highpc
8685 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8686 pdi->highpc + baseaddr)
8687 - baseaddr);
8688 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8689 this_lowpc, this_highpc - 1,
8690 cu->per_cu->v.psymtab);
8691 }
8692 }
8693
8694 if (pdi->has_range_info
8695 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8696 cu,
8697 (set_addrmap
8698 ? cu->per_objfile->per_bfd->partial_symtabs->psymtabs_addrmap
8699 : nullptr),
8700 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8701 pdi->tag))
8702 {
8703 if (pdi->lowpc < *lowpc)
8704 *lowpc = pdi->lowpc;
8705 if (pdi->highpc > *highpc)
8706 *highpc = pdi->highpc;
8707 }
8708
8709 if (pdi->has_pc_info || pdi->has_range_info
8710 || (!pdi->is_external && pdi->may_be_inlined))
8711 {
8712 if (!pdi->is_declaration)
8713 /* Ignore subprogram DIEs that do not have a name, they are
8714 illegal. Do not emit a complaint at this point, we will
8715 do so when we convert this psymtab into a symtab. */
8716 if (pdi->name (cu))
8717 add_partial_symbol (pdi, cu);
8718 }
8719 }
8720
8721 if (! pdi->has_children)
8722 return;
8723
8724 if (cu->per_cu->lang == language_ada
8725 || cu->per_cu->lang == language_fortran)
8726 {
8727 pdi = pdi->die_child;
8728 while (pdi != NULL)
8729 {
8730 pdi->fixup (cu);
8731 if (pdi->tag == DW_TAG_subprogram
8732 || pdi->tag == DW_TAG_inlined_subroutine
8733 || pdi->tag == DW_TAG_lexical_block)
8734 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8735 pdi = pdi->die_sibling;
8736 }
8737 }
8738 }
8739
8740 /* Read a partial die corresponding to an enumeration type. */
8741
8742 static void
8743 add_partial_enumeration (struct partial_die_info *enum_pdi,
8744 struct dwarf2_cu *cu)
8745 {
8746 struct partial_die_info *pdi;
8747
8748 if (enum_pdi->name (cu) != NULL)
8749 add_partial_symbol (enum_pdi, cu);
8750
8751 pdi = enum_pdi->die_child;
8752 while (pdi)
8753 {
8754 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8755 complaint (_("malformed enumerator DIE ignored"));
8756 else
8757 add_partial_symbol (pdi, cu);
8758 pdi = pdi->die_sibling;
8759 }
8760 }
8761
8762 /* Return the initial uleb128 in the die at INFO_PTR. */
8763
8764 static unsigned int
8765 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8766 {
8767 unsigned int bytes_read;
8768
8769 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8770 }
8771
8772 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8773 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8774
8775 Return the corresponding abbrev, or NULL if the number is zero (indicating
8776 an empty DIE). In either case *BYTES_READ will be set to the length of
8777 the initial number. */
8778
8779 static const struct abbrev_info *
8780 peek_die_abbrev (const die_reader_specs &reader,
8781 const gdb_byte *info_ptr, unsigned int *bytes_read)
8782 {
8783 dwarf2_cu *cu = reader.cu;
8784 bfd *abfd = reader.abfd;
8785 unsigned int abbrev_number
8786 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8787
8788 if (abbrev_number == 0)
8789 return NULL;
8790
8791 const abbrev_info *abbrev
8792 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8793 if (!abbrev)
8794 {
8795 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8796 " at offset %s [in module %s]"),
8797 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8798 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8799 }
8800
8801 return abbrev;
8802 }
8803
8804 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8805 Returns a pointer to the end of a series of DIEs, terminated by an empty
8806 DIE. Any children of the skipped DIEs will also be skipped. */
8807
8808 static const gdb_byte *
8809 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8810 {
8811 while (1)
8812 {
8813 unsigned int bytes_read;
8814 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8815 &bytes_read);
8816
8817 if (abbrev == NULL)
8818 return info_ptr + bytes_read;
8819 else
8820 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8821 }
8822 }
8823
8824 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8825 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8826 abbrev corresponding to that skipped uleb128 should be passed in
8827 ABBREV.
8828
8829 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
8830 returns a pointer to this DIE's sibling, skipping any children.
8831 Otherwise, returns a pointer to the DIE's first child. */
8832
8833 static const gdb_byte *
8834 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8835 const struct abbrev_info *abbrev, bool do_skip_children)
8836 {
8837 unsigned int bytes_read;
8838 struct attribute attr;
8839 bfd *abfd = reader->abfd;
8840 struct dwarf2_cu *cu = reader->cu;
8841 const gdb_byte *buffer = reader->buffer;
8842 const gdb_byte *buffer_end = reader->buffer_end;
8843 unsigned int form, i;
8844
8845 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
8846 {
8847 /* We only handle DW_FORM_ref4 here. */
8848 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
8849 unsigned int offset = read_4_bytes (abfd, sibling_data);
8850 const gdb_byte *sibling_ptr
8851 = buffer + to_underlying (cu->header.sect_off) + offset;
8852 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
8853 return sibling_ptr;
8854 /* Fall through to the slow way. */
8855 }
8856 else if (abbrev->size_if_constant != 0)
8857 {
8858 info_ptr += abbrev->size_if_constant;
8859 if (do_skip_children && abbrev->has_children)
8860 return skip_children (reader, info_ptr);
8861 return info_ptr;
8862 }
8863
8864 for (i = 0; i < abbrev->num_attrs; i++)
8865 {
8866 /* The only abbrev we care about is DW_AT_sibling. */
8867 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
8868 {
8869 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8870 if (attr.form == DW_FORM_ref_addr)
8871 complaint (_("ignoring absolute DW_AT_sibling"));
8872 else
8873 {
8874 sect_offset off = attr.get_ref_die_offset ();
8875 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8876
8877 if (sibling_ptr < info_ptr)
8878 complaint (_("DW_AT_sibling points backwards"));
8879 else if (sibling_ptr > reader->buffer_end)
8880 reader->die_section->overflow_complaint ();
8881 else
8882 return sibling_ptr;
8883 }
8884 }
8885
8886 /* If it isn't DW_AT_sibling, skip this attribute. */
8887 form = abbrev->attrs[i].form;
8888 skip_attribute:
8889 switch (form)
8890 {
8891 case DW_FORM_ref_addr:
8892 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8893 and later it is offset sized. */
8894 if (cu->header.version == 2)
8895 info_ptr += cu->header.addr_size;
8896 else
8897 info_ptr += cu->header.offset_size;
8898 break;
8899 case DW_FORM_GNU_ref_alt:
8900 info_ptr += cu->header.offset_size;
8901 break;
8902 case DW_FORM_addr:
8903 info_ptr += cu->header.addr_size;
8904 break;
8905 case DW_FORM_data1:
8906 case DW_FORM_ref1:
8907 case DW_FORM_flag:
8908 case DW_FORM_strx1:
8909 info_ptr += 1;
8910 break;
8911 case DW_FORM_flag_present:
8912 case DW_FORM_implicit_const:
8913 break;
8914 case DW_FORM_data2:
8915 case DW_FORM_ref2:
8916 case DW_FORM_strx2:
8917 info_ptr += 2;
8918 break;
8919 case DW_FORM_strx3:
8920 info_ptr += 3;
8921 break;
8922 case DW_FORM_data4:
8923 case DW_FORM_ref4:
8924 case DW_FORM_strx4:
8925 info_ptr += 4;
8926 break;
8927 case DW_FORM_data8:
8928 case DW_FORM_ref8:
8929 case DW_FORM_ref_sig8:
8930 info_ptr += 8;
8931 break;
8932 case DW_FORM_data16:
8933 info_ptr += 16;
8934 break;
8935 case DW_FORM_string:
8936 read_direct_string (abfd, info_ptr, &bytes_read);
8937 info_ptr += bytes_read;
8938 break;
8939 case DW_FORM_sec_offset:
8940 case DW_FORM_strp:
8941 case DW_FORM_GNU_strp_alt:
8942 info_ptr += cu->header.offset_size;
8943 break;
8944 case DW_FORM_exprloc:
8945 case DW_FORM_block:
8946 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8947 info_ptr += bytes_read;
8948 break;
8949 case DW_FORM_block1:
8950 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8951 break;
8952 case DW_FORM_block2:
8953 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8954 break;
8955 case DW_FORM_block4:
8956 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8957 break;
8958 case DW_FORM_addrx:
8959 case DW_FORM_strx:
8960 case DW_FORM_sdata:
8961 case DW_FORM_udata:
8962 case DW_FORM_ref_udata:
8963 case DW_FORM_GNU_addr_index:
8964 case DW_FORM_GNU_str_index:
8965 case DW_FORM_rnglistx:
8966 case DW_FORM_loclistx:
8967 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8968 break;
8969 case DW_FORM_indirect:
8970 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8971 info_ptr += bytes_read;
8972 /* We need to continue parsing from here, so just go back to
8973 the top. */
8974 goto skip_attribute;
8975
8976 default:
8977 error (_("Dwarf Error: Cannot handle %s "
8978 "in DWARF reader [in module %s]"),
8979 dwarf_form_name (form),
8980 bfd_get_filename (abfd));
8981 }
8982 }
8983
8984 if (do_skip_children && abbrev->has_children)
8985 return skip_children (reader, info_ptr);
8986 else
8987 return info_ptr;
8988 }
8989
8990 /* Locate ORIG_PDI's sibling.
8991 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8992
8993 static const gdb_byte *
8994 locate_pdi_sibling (const struct die_reader_specs *reader,
8995 struct partial_die_info *orig_pdi,
8996 const gdb_byte *info_ptr)
8997 {
8998 /* Do we know the sibling already? */
8999
9000 if (orig_pdi->sibling)
9001 return orig_pdi->sibling;
9002
9003 /* Are there any children to deal with? */
9004
9005 if (!orig_pdi->has_children)
9006 return info_ptr;
9007
9008 /* Skip the children the long way. */
9009
9010 return skip_children (reader, info_ptr);
9011 }
9012
9013 /* Expand this partial symbol table into a full symbol table. SELF is
9014 not NULL. */
9015
9016 void
9017 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9018 {
9019 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9020
9021 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9022
9023 /* If this psymtab is constructed from a debug-only objfile, the
9024 has_section_at_zero flag will not necessarily be correct. We
9025 can get the correct value for this flag by looking at the data
9026 associated with the (presumably stripped) associated objfile. */
9027 if (objfile->separate_debug_objfile_backlink)
9028 {
9029 dwarf2_per_objfile *per_objfile_backlink
9030 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9031
9032 per_objfile->per_bfd->has_section_at_zero
9033 = per_objfile_backlink->per_bfd->has_section_at_zero;
9034 }
9035
9036 expand_psymtab (objfile);
9037
9038 process_cu_includes (per_objfile);
9039 }
9040 \f
9041 /* Reading in full CUs. */
9042
9043 /* Add PER_CU to the queue. */
9044
9045 static void
9046 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9047 dwarf2_per_objfile *per_objfile,
9048 enum language pretend_language)
9049 {
9050 per_cu->queued = 1;
9051
9052 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9053 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9054 }
9055
9056 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9057
9058 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9059 dependency.
9060
9061 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9062 DIEs, false otherwise.
9063
9064 Explanation: there is an invariant that if a CU is queued for expansion
9065 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9066 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9067 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9068 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9069 invariant is respected.
9070
9071 The caller is therefore not required to load the CU's DIEs (we return false)
9072 if:
9073
9074 - the CU is already expanded, and therefore does not get enqueued
9075 - the CU gets enqueued for expansion, but its DIEs are already loaded
9076
9077 Note that the caller should not use this function's return value as an
9078 indicator of whether the CU's DIEs are loaded right now, it should check
9079 that by calling `dwarf2_per_objfile::get_cu` instead. */
9080
9081 static int
9082 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9083 dwarf2_per_cu_data *per_cu,
9084 dwarf2_per_objfile *per_objfile,
9085 enum language pretend_language)
9086 {
9087 /* We may arrive here during partial symbol reading, if we need full
9088 DIEs to process an unusual case (e.g. template arguments). Do
9089 not queue PER_CU, just tell our caller to load its DIEs. */
9090 if (per_cu->per_bfd->reading_partial_symbols)
9091 {
9092 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9093
9094 if (cu == NULL || cu->dies == NULL)
9095 return 1;
9096 return 0;
9097 }
9098
9099 /* Mark the dependence relation so that we don't flush PER_CU
9100 too early. */
9101 if (dependent_cu != NULL)
9102 dependent_cu->add_dependence (per_cu);
9103
9104 /* If it's already on the queue, we have nothing to do. */
9105 if (per_cu->queued)
9106 {
9107 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9108 loaded. */
9109 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9110
9111 /* If the CU is queued for expansion, it should not already be
9112 expanded. */
9113 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9114
9115 /* The DIEs are already loaded, the caller doesn't need to do it. */
9116 return 0;
9117 }
9118
9119 bool queued = false;
9120 if (!per_objfile->symtab_set_p (per_cu))
9121 {
9122 /* Add it to the queue. */
9123 queue_comp_unit (per_cu, per_objfile, pretend_language);
9124 queued = true;
9125 }
9126
9127 /* If the compilation unit is already loaded, just mark it as
9128 used. */
9129 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9130 if (cu != nullptr)
9131 cu->last_used = 0;
9132
9133 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9134 and the DIEs are not already loaded. */
9135 return queued && cu == nullptr;
9136 }
9137
9138 /* Process the queue. */
9139
9140 static void
9141 process_queue (dwarf2_per_objfile *per_objfile)
9142 {
9143 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9144 objfile_name (per_objfile->objfile));
9145
9146 /* The queue starts out with one item, but following a DIE reference
9147 may load a new CU, adding it to the end of the queue. */
9148 while (!per_objfile->per_bfd->queue->empty ())
9149 {
9150 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9151 dwarf2_per_cu_data *per_cu = item.per_cu;
9152
9153 if (!per_objfile->symtab_set_p (per_cu))
9154 {
9155 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9156
9157 /* Skip dummy CUs. */
9158 if (cu != nullptr)
9159 {
9160 unsigned int debug_print_threshold;
9161 char buf[100];
9162
9163 if (per_cu->is_debug_types)
9164 {
9165 struct signatured_type *sig_type =
9166 (struct signatured_type *) per_cu;
9167
9168 sprintf (buf, "TU %s at offset %s",
9169 hex_string (sig_type->signature),
9170 sect_offset_str (per_cu->sect_off));
9171 /* There can be 100s of TUs.
9172 Only print them in verbose mode. */
9173 debug_print_threshold = 2;
9174 }
9175 else
9176 {
9177 sprintf (buf, "CU at offset %s",
9178 sect_offset_str (per_cu->sect_off));
9179 debug_print_threshold = 1;
9180 }
9181
9182 if (dwarf_read_debug >= debug_print_threshold)
9183 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9184
9185 if (per_cu->is_debug_types)
9186 process_full_type_unit (cu, item.pretend_language);
9187 else
9188 process_full_comp_unit (cu, item.pretend_language);
9189
9190 if (dwarf_read_debug >= debug_print_threshold)
9191 dwarf_read_debug_printf ("Done expanding %s", buf);
9192 }
9193 }
9194
9195 per_cu->queued = 0;
9196 per_objfile->per_bfd->queue->pop ();
9197 }
9198
9199 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9200 objfile_name (per_objfile->objfile));
9201 }
9202
9203 /* Read in full symbols for PST, and anything it depends on. */
9204
9205 void
9206 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9207 {
9208 gdb_assert (!readin_p (objfile));
9209
9210 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9211 free_cached_comp_units freer (per_objfile);
9212 expand_dependencies (objfile);
9213
9214 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9215 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9216 }
9217
9218 /* See psympriv.h. */
9219
9220 bool
9221 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9222 {
9223 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9224 return per_objfile->symtab_set_p (per_cu_data);
9225 }
9226
9227 /* See psympriv.h. */
9228
9229 compunit_symtab *
9230 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9231 {
9232 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9233 return per_objfile->get_symtab (per_cu_data);
9234 }
9235
9236 /* Trivial hash function for die_info: the hash value of a DIE
9237 is its offset in .debug_info for this objfile. */
9238
9239 static hashval_t
9240 die_hash (const void *item)
9241 {
9242 const struct die_info *die = (const struct die_info *) item;
9243
9244 return to_underlying (die->sect_off);
9245 }
9246
9247 /* Trivial comparison function for die_info structures: two DIEs
9248 are equal if they have the same offset. */
9249
9250 static int
9251 die_eq (const void *item_lhs, const void *item_rhs)
9252 {
9253 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9254 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9255
9256 return die_lhs->sect_off == die_rhs->sect_off;
9257 }
9258
9259 /* Load the DIEs associated with PER_CU into memory.
9260
9261 In some cases, the caller, while reading partial symbols, will need to load
9262 the full symbols for the CU for some reason. It will already have a
9263 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9264 rather than creating a new one. */
9265
9266 static void
9267 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9268 dwarf2_per_objfile *per_objfile,
9269 dwarf2_cu *existing_cu,
9270 bool skip_partial,
9271 enum language pretend_language)
9272 {
9273 gdb_assert (! this_cu->is_debug_types);
9274
9275 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9276 if (reader.dummy_p)
9277 return;
9278
9279 struct dwarf2_cu *cu = reader.cu;
9280 const gdb_byte *info_ptr = reader.info_ptr;
9281
9282 gdb_assert (cu->die_hash == NULL);
9283 cu->die_hash =
9284 htab_create_alloc_ex (cu->header.length / 12,
9285 die_hash,
9286 die_eq,
9287 NULL,
9288 &cu->comp_unit_obstack,
9289 hashtab_obstack_allocate,
9290 dummy_obstack_deallocate);
9291
9292 if (reader.comp_unit_die->has_children)
9293 reader.comp_unit_die->child
9294 = read_die_and_siblings (&reader, reader.info_ptr,
9295 &info_ptr, reader.comp_unit_die);
9296 cu->dies = reader.comp_unit_die;
9297 /* comp_unit_die is not stored in die_hash, no need. */
9298
9299 /* We try not to read any attributes in this function, because not
9300 all CUs needed for references have been loaded yet, and symbol
9301 table processing isn't initialized. But we have to set the CU language,
9302 or we won't be able to build types correctly.
9303 Similarly, if we do not read the producer, we can not apply
9304 producer-specific interpretation. */
9305 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9306
9307 reader.keep ();
9308 }
9309
9310 /* Add a DIE to the delayed physname list. */
9311
9312 static void
9313 add_to_method_list (struct type *type, int fnfield_index, int index,
9314 const char *name, struct die_info *die,
9315 struct dwarf2_cu *cu)
9316 {
9317 struct delayed_method_info mi;
9318 mi.type = type;
9319 mi.fnfield_index = fnfield_index;
9320 mi.index = index;
9321 mi.name = name;
9322 mi.die = die;
9323 cu->method_list.push_back (mi);
9324 }
9325
9326 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9327 "const" / "volatile". If so, decrements LEN by the length of the
9328 modifier and return true. Otherwise return false. */
9329
9330 template<size_t N>
9331 static bool
9332 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9333 {
9334 size_t mod_len = sizeof (mod) - 1;
9335 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9336 {
9337 len -= mod_len;
9338 return true;
9339 }
9340 return false;
9341 }
9342
9343 /* Compute the physnames of any methods on the CU's method list.
9344
9345 The computation of method physnames is delayed in order to avoid the
9346 (bad) condition that one of the method's formal parameters is of an as yet
9347 incomplete type. */
9348
9349 static void
9350 compute_delayed_physnames (struct dwarf2_cu *cu)
9351 {
9352 /* Only C++ delays computing physnames. */
9353 if (cu->method_list.empty ())
9354 return;
9355 gdb_assert (cu->per_cu->lang == language_cplus);
9356
9357 for (const delayed_method_info &mi : cu->method_list)
9358 {
9359 const char *physname;
9360 struct fn_fieldlist *fn_flp
9361 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9362 physname = dwarf2_physname (mi.name, mi.die, cu);
9363 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9364 = physname ? physname : "";
9365
9366 /* Since there's no tag to indicate whether a method is a
9367 const/volatile overload, extract that information out of the
9368 demangled name. */
9369 if (physname != NULL)
9370 {
9371 size_t len = strlen (physname);
9372
9373 while (1)
9374 {
9375 if (physname[len] == ')') /* shortcut */
9376 break;
9377 else if (check_modifier (physname, len, " const"))
9378 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9379 else if (check_modifier (physname, len, " volatile"))
9380 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9381 else
9382 break;
9383 }
9384 }
9385 }
9386
9387 /* The list is no longer needed. */
9388 cu->method_list.clear ();
9389 }
9390
9391 /* Go objects should be embedded in a DW_TAG_module DIE,
9392 and it's not clear if/how imported objects will appear.
9393 To keep Go support simple until that's worked out,
9394 go back through what we've read and create something usable.
9395 We could do this while processing each DIE, and feels kinda cleaner,
9396 but that way is more invasive.
9397 This is to, for example, allow the user to type "p var" or "b main"
9398 without having to specify the package name, and allow lookups
9399 of module.object to work in contexts that use the expression
9400 parser. */
9401
9402 static void
9403 fixup_go_packaging (struct dwarf2_cu *cu)
9404 {
9405 gdb::unique_xmalloc_ptr<char> package_name;
9406 struct pending *list;
9407 int i;
9408
9409 for (list = *cu->get_builder ()->get_global_symbols ();
9410 list != NULL;
9411 list = list->next)
9412 {
9413 for (i = 0; i < list->nsyms; ++i)
9414 {
9415 struct symbol *sym = list->symbol[i];
9416
9417 if (sym->language () == language_go
9418 && sym->aclass () == LOC_BLOCK)
9419 {
9420 gdb::unique_xmalloc_ptr<char> this_package_name
9421 (go_symbol_package_name (sym));
9422
9423 if (this_package_name == NULL)
9424 continue;
9425 if (package_name == NULL)
9426 package_name = std::move (this_package_name);
9427 else
9428 {
9429 struct objfile *objfile = cu->per_objfile->objfile;
9430 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9431 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9432 (symbol_symtab (sym) != NULL
9433 ? symtab_to_filename_for_display
9434 (symbol_symtab (sym))
9435 : objfile_name (objfile)),
9436 this_package_name.get (), package_name.get ());
9437 }
9438 }
9439 }
9440 }
9441
9442 if (package_name != NULL)
9443 {
9444 struct objfile *objfile = cu->per_objfile->objfile;
9445 const char *saved_package_name = objfile->intern (package_name.get ());
9446 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9447 saved_package_name);
9448 struct symbol *sym;
9449
9450 sym = new (&objfile->objfile_obstack) symbol;
9451 sym->set_language (language_go, &objfile->objfile_obstack);
9452 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9453 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9454 e.g., "main" finds the "main" module and not C's main(). */
9455 sym->set_domain (STRUCT_DOMAIN);
9456 sym->set_aclass_index (LOC_TYPEDEF);
9457 sym->set_type (type);
9458
9459 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9460 }
9461 }
9462
9463 /* Allocate a fully-qualified name consisting of the two parts on the
9464 obstack. */
9465
9466 static const char *
9467 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9468 {
9469 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9470 }
9471
9472 /* A helper that allocates a variant part to attach to a Rust enum
9473 type. OBSTACK is where the results should be allocated. TYPE is
9474 the type we're processing. DISCRIMINANT_INDEX is the index of the
9475 discriminant. It must be the index of one of the fields of TYPE,
9476 or -1 to mean there is no discriminant (univariant enum).
9477 DEFAULT_INDEX is the index of the default field; or -1 if there is
9478 no default. RANGES is indexed by "effective" field number (the
9479 field index, but omitting the discriminant and default fields) and
9480 must hold the discriminant values used by the variants. Note that
9481 RANGES must have a lifetime at least as long as OBSTACK -- either
9482 already allocated on it, or static. */
9483
9484 static void
9485 alloc_rust_variant (struct obstack *obstack, struct type *type,
9486 int discriminant_index, int default_index,
9487 gdb::array_view<discriminant_range> ranges)
9488 {
9489 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9490 gdb_assert (discriminant_index == -1
9491 || (discriminant_index >= 0
9492 && discriminant_index < type->num_fields ()));
9493 gdb_assert (default_index == -1
9494 || (default_index >= 0 && default_index < type->num_fields ()));
9495
9496 /* We have one variant for each non-discriminant field. */
9497 int n_variants = type->num_fields ();
9498 if (discriminant_index != -1)
9499 --n_variants;
9500
9501 variant *variants = new (obstack) variant[n_variants];
9502 int var_idx = 0;
9503 int range_idx = 0;
9504 for (int i = 0; i < type->num_fields (); ++i)
9505 {
9506 if (i == discriminant_index)
9507 continue;
9508
9509 variants[var_idx].first_field = i;
9510 variants[var_idx].last_field = i + 1;
9511
9512 /* The default field does not need a range, but other fields do.
9513 We skipped the discriminant above. */
9514 if (i != default_index)
9515 {
9516 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9517 ++range_idx;
9518 }
9519
9520 ++var_idx;
9521 }
9522
9523 gdb_assert (range_idx == ranges.size ());
9524 gdb_assert (var_idx == n_variants);
9525
9526 variant_part *part = new (obstack) variant_part;
9527 part->discriminant_index = discriminant_index;
9528 /* If there is no discriminant, then whether it is signed is of no
9529 consequence. */
9530 part->is_unsigned
9531 = (discriminant_index == -1
9532 ? false
9533 : type->field (discriminant_index).type ()->is_unsigned ());
9534 part->variants = gdb::array_view<variant> (variants, n_variants);
9535
9536 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9537 gdb::array_view<variant_part> *prop_value
9538 = new (storage) gdb::array_view<variant_part> (part, 1);
9539
9540 struct dynamic_prop prop;
9541 prop.set_variant_parts (prop_value);
9542
9543 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9544 }
9545
9546 /* Some versions of rustc emitted enums in an unusual way.
9547
9548 Ordinary enums were emitted as unions. The first element of each
9549 structure in the union was named "RUST$ENUM$DISR". This element
9550 held the discriminant.
9551
9552 These versions of Rust also implemented the "non-zero"
9553 optimization. When the enum had two values, and one is empty and
9554 the other holds a pointer that cannot be zero, the pointer is used
9555 as the discriminant, with a zero value meaning the empty variant.
9556 Here, the union's first member is of the form
9557 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9558 where the fieldnos are the indices of the fields that should be
9559 traversed in order to find the field (which may be several fields deep)
9560 and the variantname is the name of the variant of the case when the
9561 field is zero.
9562
9563 This function recognizes whether TYPE is of one of these forms,
9564 and, if so, smashes it to be a variant type. */
9565
9566 static void
9567 quirk_rust_enum (struct type *type, struct objfile *objfile)
9568 {
9569 gdb_assert (type->code () == TYPE_CODE_UNION);
9570
9571 /* We don't need to deal with empty enums. */
9572 if (type->num_fields () == 0)
9573 return;
9574
9575 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9576 if (type->num_fields () == 1
9577 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9578 {
9579 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9580
9581 /* Decode the field name to find the offset of the
9582 discriminant. */
9583 ULONGEST bit_offset = 0;
9584 struct type *field_type = type->field (0).type ();
9585 while (name[0] >= '0' && name[0] <= '9')
9586 {
9587 char *tail;
9588 unsigned long index = strtoul (name, &tail, 10);
9589 name = tail;
9590 if (*name != '$'
9591 || index >= field_type->num_fields ()
9592 || (field_type->field (index).loc_kind ()
9593 != FIELD_LOC_KIND_BITPOS))
9594 {
9595 complaint (_("Could not parse Rust enum encoding string \"%s\""
9596 "[in module %s]"),
9597 type->field (0).name (),
9598 objfile_name (objfile));
9599 return;
9600 }
9601 ++name;
9602
9603 bit_offset += field_type->field (index).loc_bitpos ();
9604 field_type = field_type->field (index).type ();
9605 }
9606
9607 /* Smash this type to be a structure type. We have to do this
9608 because the type has already been recorded. */
9609 type->set_code (TYPE_CODE_STRUCT);
9610 type->set_num_fields (3);
9611 /* Save the field we care about. */
9612 struct field saved_field = type->field (0);
9613 type->set_fields
9614 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9615
9616 /* Put the discriminant at index 0. */
9617 type->field (0).set_type (field_type);
9618 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9619 type->field (0).set_name ("<<discriminant>>");
9620 type->field (0).set_loc_bitpos (bit_offset);
9621
9622 /* The order of fields doesn't really matter, so put the real
9623 field at index 1 and the data-less field at index 2. */
9624 type->field (1) = saved_field;
9625 type->field (1).set_name
9626 (rust_last_path_segment (type->field (1).type ()->name ()));
9627 type->field (1).type ()->set_name
9628 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9629 type->field (1).name ()));
9630
9631 const char *dataless_name
9632 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9633 name);
9634 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9635 dataless_name);
9636 type->field (2).set_type (dataless_type);
9637 /* NAME points into the original discriminant name, which
9638 already has the correct lifetime. */
9639 type->field (2).set_name (name);
9640 type->field (2).set_loc_bitpos (0);
9641
9642 /* Indicate that this is a variant type. */
9643 static discriminant_range ranges[1] = { { 0, 0 } };
9644 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9645 }
9646 /* A union with a single anonymous field is probably an old-style
9647 univariant enum. */
9648 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9649 {
9650 /* Smash this type to be a structure type. We have to do this
9651 because the type has already been recorded. */
9652 type->set_code (TYPE_CODE_STRUCT);
9653
9654 struct type *field_type = type->field (0).type ();
9655 const char *variant_name
9656 = rust_last_path_segment (field_type->name ());
9657 type->field (0).set_name (variant_name);
9658 field_type->set_name
9659 (rust_fully_qualify (&objfile->objfile_obstack,
9660 type->name (), variant_name));
9661
9662 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9663 }
9664 else
9665 {
9666 struct type *disr_type = nullptr;
9667 for (int i = 0; i < type->num_fields (); ++i)
9668 {
9669 disr_type = type->field (i).type ();
9670
9671 if (disr_type->code () != TYPE_CODE_STRUCT)
9672 {
9673 /* All fields of a true enum will be structs. */
9674 return;
9675 }
9676 else if (disr_type->num_fields () == 0)
9677 {
9678 /* Could be data-less variant, so keep going. */
9679 disr_type = nullptr;
9680 }
9681 else if (strcmp (disr_type->field (0).name (),
9682 "RUST$ENUM$DISR") != 0)
9683 {
9684 /* Not a Rust enum. */
9685 return;
9686 }
9687 else
9688 {
9689 /* Found one. */
9690 break;
9691 }
9692 }
9693
9694 /* If we got here without a discriminant, then it's probably
9695 just a union. */
9696 if (disr_type == nullptr)
9697 return;
9698
9699 /* Smash this type to be a structure type. We have to do this
9700 because the type has already been recorded. */
9701 type->set_code (TYPE_CODE_STRUCT);
9702
9703 /* Make space for the discriminant field. */
9704 struct field *disr_field = &disr_type->field (0);
9705 field *new_fields
9706 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9707 * sizeof (struct field)));
9708 memcpy (new_fields + 1, type->fields (),
9709 type->num_fields () * sizeof (struct field));
9710 type->set_fields (new_fields);
9711 type->set_num_fields (type->num_fields () + 1);
9712
9713 /* Install the discriminant at index 0 in the union. */
9714 type->field (0) = *disr_field;
9715 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9716 type->field (0).set_name ("<<discriminant>>");
9717
9718 /* We need a way to find the correct discriminant given a
9719 variant name. For convenience we build a map here. */
9720 struct type *enum_type = disr_field->type ();
9721 std::unordered_map<std::string, ULONGEST> discriminant_map;
9722 for (int i = 0; i < enum_type->num_fields (); ++i)
9723 {
9724 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9725 {
9726 const char *name
9727 = rust_last_path_segment (enum_type->field (i).name ());
9728 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9729 }
9730 }
9731
9732 int n_fields = type->num_fields ();
9733 /* We don't need a range entry for the discriminant, but we do
9734 need one for every other field, as there is no default
9735 variant. */
9736 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9737 discriminant_range,
9738 n_fields - 1);
9739 /* Skip the discriminant here. */
9740 for (int i = 1; i < n_fields; ++i)
9741 {
9742 /* Find the final word in the name of this variant's type.
9743 That name can be used to look up the correct
9744 discriminant. */
9745 const char *variant_name
9746 = rust_last_path_segment (type->field (i).type ()->name ());
9747
9748 auto iter = discriminant_map.find (variant_name);
9749 if (iter != discriminant_map.end ())
9750 {
9751 ranges[i - 1].low = iter->second;
9752 ranges[i - 1].high = iter->second;
9753 }
9754
9755 /* In Rust, each element should have the size of the
9756 enclosing enum. */
9757 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9758
9759 /* Remove the discriminant field, if it exists. */
9760 struct type *sub_type = type->field (i).type ();
9761 if (sub_type->num_fields () > 0)
9762 {
9763 sub_type->set_num_fields (sub_type->num_fields () - 1);
9764 sub_type->set_fields (sub_type->fields () + 1);
9765 }
9766 type->field (i).set_name (variant_name);
9767 sub_type->set_name
9768 (rust_fully_qualify (&objfile->objfile_obstack,
9769 type->name (), variant_name));
9770 }
9771
9772 /* Indicate that this is a variant type. */
9773 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9774 gdb::array_view<discriminant_range> (ranges,
9775 n_fields - 1));
9776 }
9777 }
9778
9779 /* Rewrite some Rust unions to be structures with variants parts. */
9780
9781 static void
9782 rust_union_quirks (struct dwarf2_cu *cu)
9783 {
9784 gdb_assert (cu->per_cu->lang == language_rust);
9785 for (type *type_ : cu->rust_unions)
9786 quirk_rust_enum (type_, cu->per_objfile->objfile);
9787 /* We don't need this any more. */
9788 cu->rust_unions.clear ();
9789 }
9790
9791 /* See read.h. */
9792
9793 type_unit_group_unshareable *
9794 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9795 {
9796 auto iter = this->m_type_units.find (tu_group);
9797 if (iter != this->m_type_units.end ())
9798 return iter->second.get ();
9799
9800 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9801 type_unit_group_unshareable *result = uniq.get ();
9802 this->m_type_units[tu_group] = std::move (uniq);
9803 return result;
9804 }
9805
9806 struct type *
9807 dwarf2_per_objfile::get_type_for_signatured_type
9808 (signatured_type *sig_type) const
9809 {
9810 auto iter = this->m_type_map.find (sig_type);
9811 if (iter == this->m_type_map.end ())
9812 return nullptr;
9813
9814 return iter->second;
9815 }
9816
9817 void dwarf2_per_objfile::set_type_for_signatured_type
9818 (signatured_type *sig_type, struct type *type)
9819 {
9820 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9821
9822 this->m_type_map[sig_type] = type;
9823 }
9824
9825 /* A helper function for computing the list of all symbol tables
9826 included by PER_CU. */
9827
9828 static void
9829 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9830 htab_t all_children, htab_t all_type_symtabs,
9831 dwarf2_per_cu_data *per_cu,
9832 dwarf2_per_objfile *per_objfile,
9833 struct compunit_symtab *immediate_parent)
9834 {
9835 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9836 if (*slot != NULL)
9837 {
9838 /* This inclusion and its children have been processed. */
9839 return;
9840 }
9841
9842 *slot = per_cu;
9843
9844 /* Only add a CU if it has a symbol table. */
9845 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9846 if (cust != NULL)
9847 {
9848 /* If this is a type unit only add its symbol table if we haven't
9849 seen it yet (type unit per_cu's can share symtabs). */
9850 if (per_cu->is_debug_types)
9851 {
9852 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9853 if (*slot == NULL)
9854 {
9855 *slot = cust;
9856 result->push_back (cust);
9857 if (cust->user == NULL)
9858 cust->user = immediate_parent;
9859 }
9860 }
9861 else
9862 {
9863 result->push_back (cust);
9864 if (cust->user == NULL)
9865 cust->user = immediate_parent;
9866 }
9867 }
9868
9869 if (!per_cu->imported_symtabs_empty ())
9870 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9871 {
9872 recursively_compute_inclusions (result, all_children,
9873 all_type_symtabs, ptr, per_objfile,
9874 cust);
9875 }
9876 }
9877
9878 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9879 PER_CU. */
9880
9881 static void
9882 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9883 dwarf2_per_objfile *per_objfile)
9884 {
9885 gdb_assert (! per_cu->is_debug_types);
9886
9887 if (!per_cu->imported_symtabs_empty ())
9888 {
9889 int len;
9890 std::vector<compunit_symtab *> result_symtabs;
9891 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9892
9893 /* If we don't have a symtab, we can just skip this case. */
9894 if (cust == NULL)
9895 return;
9896
9897 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9898 htab_eq_pointer,
9899 NULL, xcalloc, xfree));
9900 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9901 htab_eq_pointer,
9902 NULL, xcalloc, xfree));
9903
9904 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9905 {
9906 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9907 all_type_symtabs.get (), ptr,
9908 per_objfile, cust);
9909 }
9910
9911 /* Now we have a transitive closure of all the included symtabs. */
9912 len = result_symtabs.size ();
9913 cust->includes
9914 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9915 struct compunit_symtab *, len + 1);
9916 memcpy (cust->includes, result_symtabs.data (),
9917 len * sizeof (compunit_symtab *));
9918 cust->includes[len] = NULL;
9919 }
9920 }
9921
9922 /* Compute the 'includes' field for the symtabs of all the CUs we just
9923 read. */
9924
9925 static void
9926 process_cu_includes (dwarf2_per_objfile *per_objfile)
9927 {
9928 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9929 {
9930 if (! iter->is_debug_types)
9931 compute_compunit_symtab_includes (iter, per_objfile);
9932 }
9933
9934 per_objfile->per_bfd->just_read_cus.clear ();
9935 }
9936
9937 /* Generate full symbol information for CU, whose DIEs have
9938 already been loaded into memory. */
9939
9940 static void
9941 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9942 {
9943 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9944 struct objfile *objfile = per_objfile->objfile;
9945 struct gdbarch *gdbarch = objfile->arch ();
9946 CORE_ADDR lowpc, highpc;
9947 struct compunit_symtab *cust;
9948 CORE_ADDR baseaddr;
9949 struct block *static_block;
9950 CORE_ADDR addr;
9951
9952 baseaddr = objfile->text_section_offset ();
9953
9954 /* Clear the list here in case something was left over. */
9955 cu->method_list.clear ();
9956
9957 dwarf2_find_base_address (cu->dies, cu);
9958
9959 /* Before we start reading the top-level DIE, ensure it has a valid tag
9960 type. */
9961 switch (cu->dies->tag)
9962 {
9963 case DW_TAG_compile_unit:
9964 case DW_TAG_partial_unit:
9965 case DW_TAG_type_unit:
9966 break;
9967 default:
9968 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9969 dwarf_tag_name (cu->dies->tag),
9970 sect_offset_str (cu->per_cu->sect_off),
9971 objfile_name (per_objfile->objfile));
9972 }
9973
9974 /* Do line number decoding in read_file_scope () */
9975 process_die (cu->dies, cu);
9976
9977 /* For now fudge the Go package. */
9978 if (cu->per_cu->lang == language_go)
9979 fixup_go_packaging (cu);
9980
9981 /* Now that we have processed all the DIEs in the CU, all the types
9982 should be complete, and it should now be safe to compute all of the
9983 physnames. */
9984 compute_delayed_physnames (cu);
9985
9986 if (cu->per_cu->lang == language_rust)
9987 rust_union_quirks (cu);
9988
9989 /* Some compilers don't define a DW_AT_high_pc attribute for the
9990 compilation unit. If the DW_AT_high_pc is missing, synthesize
9991 it, by scanning the DIE's below the compilation unit. */
9992 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9993
9994 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9995 static_block
9996 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
9997
9998 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9999 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10000 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10001 addrmap to help ensure it has an accurate map of pc values belonging to
10002 this comp unit. */
10003 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10004
10005 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
10006 (static_block, SECT_OFF_TEXT (objfile), 0);
10007
10008 if (cust != NULL)
10009 {
10010 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10011
10012 /* Set symtab language to language from DW_AT_language. If the
10013 compilation is from a C file generated by language preprocessors, do
10014 not set the language if it was already deduced by start_subfile. */
10015 if (!(cu->per_cu->lang == language_c
10016 && cust->primary_filetab ()->language () != language_unknown))
10017 cust->primary_filetab ()->set_language (cu->per_cu->lang);
10018
10019 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10020 produce DW_AT_location with location lists but it can be possibly
10021 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10022 there were bugs in prologue debug info, fixed later in GCC-4.5
10023 by "unwind info for epilogues" patch (which is not directly related).
10024
10025 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10026 needed, it would be wrong due to missing DW_AT_producer there.
10027
10028 Still one can confuse GDB by using non-standard GCC compilation
10029 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10030 */
10031 if (cu->has_loclist && gcc_4_minor >= 5)
10032 cust->set_locations_valid (true);
10033
10034 if (gcc_4_minor >= 5)
10035 cust->set_epilogue_unwind_valid (true);
10036
10037 cust->set_call_site_htab (cu->call_site_htab);
10038 }
10039
10040 per_objfile->set_symtab (cu->per_cu, cust);
10041
10042 /* Push it for inclusion processing later. */
10043 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10044
10045 /* Not needed any more. */
10046 cu->reset_builder ();
10047 }
10048
10049 /* Generate full symbol information for type unit CU, whose DIEs have
10050 already been loaded into memory. */
10051
10052 static void
10053 process_full_type_unit (dwarf2_cu *cu,
10054 enum language pretend_language)
10055 {
10056 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10057 struct objfile *objfile = per_objfile->objfile;
10058 struct compunit_symtab *cust;
10059 struct signatured_type *sig_type;
10060
10061 gdb_assert (cu->per_cu->is_debug_types);
10062 sig_type = (struct signatured_type *) cu->per_cu;
10063
10064 /* Clear the list here in case something was left over. */
10065 cu->method_list.clear ();
10066
10067 /* The symbol tables are set up in read_type_unit_scope. */
10068 process_die (cu->dies, cu);
10069
10070 /* For now fudge the Go package. */
10071 if (cu->per_cu->lang == language_go)
10072 fixup_go_packaging (cu);
10073
10074 /* Now that we have processed all the DIEs in the CU, all the types
10075 should be complete, and it should now be safe to compute all of the
10076 physnames. */
10077 compute_delayed_physnames (cu);
10078
10079 if (cu->per_cu->lang == language_rust)
10080 rust_union_quirks (cu);
10081
10082 /* TUs share symbol tables.
10083 If this is the first TU to use this symtab, complete the construction
10084 of it with end_expandable_symtab. Otherwise, complete the addition of
10085 this TU's symbols to the existing symtab. */
10086 type_unit_group_unshareable *tug_unshare =
10087 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10088 if (tug_unshare->compunit_symtab == NULL)
10089 {
10090 buildsym_compunit *builder = cu->get_builder ();
10091 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10092 tug_unshare->compunit_symtab = cust;
10093
10094 if (cust != NULL)
10095 {
10096 /* Set symtab language to language from DW_AT_language. If the
10097 compilation is from a C file generated by language preprocessors,
10098 do not set the language if it was already deduced by
10099 start_subfile. */
10100 if (!(cu->per_cu->lang == language_c
10101 && cust->primary_filetab ()->language () != language_c))
10102 cust->primary_filetab ()->set_language (cu->per_cu->lang);
10103 }
10104 }
10105 else
10106 {
10107 cu->get_builder ()->augment_type_symtab ();
10108 cust = tug_unshare->compunit_symtab;
10109 }
10110
10111 per_objfile->set_symtab (cu->per_cu, cust);
10112
10113 /* Not needed any more. */
10114 cu->reset_builder ();
10115 }
10116
10117 /* Process an imported unit DIE. */
10118
10119 static void
10120 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10121 {
10122 struct attribute *attr;
10123
10124 /* For now we don't handle imported units in type units. */
10125 if (cu->per_cu->is_debug_types)
10126 {
10127 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10128 " supported in type units [in module %s]"),
10129 objfile_name (cu->per_objfile->objfile));
10130 }
10131
10132 attr = dwarf2_attr (die, DW_AT_import, cu);
10133 if (attr != NULL)
10134 {
10135 sect_offset sect_off = attr->get_ref_die_offset ();
10136 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10137 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10138 dwarf2_per_cu_data *per_cu
10139 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10140 per_objfile->per_bfd);
10141
10142 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10143 into another compilation unit, at root level. Regard this as a hint,
10144 and ignore it. */
10145 if (die->parent && die->parent->parent == NULL
10146 && per_cu->unit_type == DW_UT_compile
10147 && per_cu->lang == language_cplus)
10148 return;
10149
10150 /* If necessary, add it to the queue and load its DIEs. */
10151 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
10152 cu->per_cu->lang))
10153 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10154 false, cu->per_cu->lang);
10155
10156 cu->per_cu->imported_symtabs_push (per_cu);
10157 }
10158 }
10159
10160 /* RAII object that represents a process_die scope: i.e.,
10161 starts/finishes processing a DIE. */
10162 class process_die_scope
10163 {
10164 public:
10165 process_die_scope (die_info *die, dwarf2_cu *cu)
10166 : m_die (die), m_cu (cu)
10167 {
10168 /* We should only be processing DIEs not already in process. */
10169 gdb_assert (!m_die->in_process);
10170 m_die->in_process = true;
10171 }
10172
10173 ~process_die_scope ()
10174 {
10175 m_die->in_process = false;
10176
10177 /* If we're done processing the DIE for the CU that owns the line
10178 header, we don't need the line header anymore. */
10179 if (m_cu->line_header_die_owner == m_die)
10180 {
10181 delete m_cu->line_header;
10182 m_cu->line_header = NULL;
10183 m_cu->line_header_die_owner = NULL;
10184 }
10185 }
10186
10187 private:
10188 die_info *m_die;
10189 dwarf2_cu *m_cu;
10190 };
10191
10192 /* Process a die and its children. */
10193
10194 static void
10195 process_die (struct die_info *die, struct dwarf2_cu *cu)
10196 {
10197 process_die_scope scope (die, cu);
10198
10199 switch (die->tag)
10200 {
10201 case DW_TAG_padding:
10202 break;
10203 case DW_TAG_compile_unit:
10204 case DW_TAG_partial_unit:
10205 read_file_scope (die, cu);
10206 break;
10207 case DW_TAG_type_unit:
10208 read_type_unit_scope (die, cu);
10209 break;
10210 case DW_TAG_subprogram:
10211 /* Nested subprograms in Fortran get a prefix. */
10212 if (cu->per_cu->lang == language_fortran
10213 && die->parent != NULL
10214 && die->parent->tag == DW_TAG_subprogram)
10215 cu->processing_has_namespace_info = true;
10216 /* Fall through. */
10217 case DW_TAG_inlined_subroutine:
10218 read_func_scope (die, cu);
10219 break;
10220 case DW_TAG_lexical_block:
10221 case DW_TAG_try_block:
10222 case DW_TAG_catch_block:
10223 read_lexical_block_scope (die, cu);
10224 break;
10225 case DW_TAG_call_site:
10226 case DW_TAG_GNU_call_site:
10227 read_call_site_scope (die, cu);
10228 break;
10229 case DW_TAG_class_type:
10230 case DW_TAG_interface_type:
10231 case DW_TAG_structure_type:
10232 case DW_TAG_union_type:
10233 case DW_TAG_namelist:
10234 process_structure_scope (die, cu);
10235 break;
10236 case DW_TAG_enumeration_type:
10237 process_enumeration_scope (die, cu);
10238 break;
10239
10240 /* These dies have a type, but processing them does not create
10241 a symbol or recurse to process the children. Therefore we can
10242 read them on-demand through read_type_die. */
10243 case DW_TAG_subroutine_type:
10244 case DW_TAG_set_type:
10245 case DW_TAG_pointer_type:
10246 case DW_TAG_ptr_to_member_type:
10247 case DW_TAG_reference_type:
10248 case DW_TAG_rvalue_reference_type:
10249 case DW_TAG_string_type:
10250 break;
10251
10252 case DW_TAG_array_type:
10253 /* We only need to handle this case for Ada -- in other
10254 languages, it's normal for the compiler to emit a typedef
10255 instead. */
10256 if (cu->per_cu->lang != language_ada)
10257 break;
10258 /* FALLTHROUGH */
10259 case DW_TAG_base_type:
10260 case DW_TAG_subrange_type:
10261 case DW_TAG_generic_subrange:
10262 case DW_TAG_typedef:
10263 /* Add a typedef symbol for the type definition, if it has a
10264 DW_AT_name. */
10265 new_symbol (die, read_type_die (die, cu), cu);
10266 break;
10267 case DW_TAG_common_block:
10268 read_common_block (die, cu);
10269 break;
10270 case DW_TAG_common_inclusion:
10271 break;
10272 case DW_TAG_namespace:
10273 cu->processing_has_namespace_info = true;
10274 read_namespace (die, cu);
10275 break;
10276 case DW_TAG_module:
10277 cu->processing_has_namespace_info = true;
10278 read_module (die, cu);
10279 break;
10280 case DW_TAG_imported_declaration:
10281 cu->processing_has_namespace_info = true;
10282 if (read_namespace_alias (die, cu))
10283 break;
10284 /* The declaration is not a global namespace alias. */
10285 /* Fall through. */
10286 case DW_TAG_imported_module:
10287 cu->processing_has_namespace_info = true;
10288 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10289 || cu->per_cu->lang != language_fortran))
10290 complaint (_("Tag '%s' has unexpected children"),
10291 dwarf_tag_name (die->tag));
10292 read_import_statement (die, cu);
10293 break;
10294
10295 case DW_TAG_imported_unit:
10296 process_imported_unit_die (die, cu);
10297 break;
10298
10299 case DW_TAG_variable:
10300 read_variable (die, cu);
10301 break;
10302
10303 default:
10304 new_symbol (die, NULL, cu);
10305 break;
10306 }
10307 }
10308 \f
10309 /* DWARF name computation. */
10310
10311 /* A helper function for dwarf2_compute_name which determines whether DIE
10312 needs to have the name of the scope prepended to the name listed in the
10313 die. */
10314
10315 static int
10316 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10317 {
10318 struct attribute *attr;
10319
10320 switch (die->tag)
10321 {
10322 case DW_TAG_namespace:
10323 case DW_TAG_typedef:
10324 case DW_TAG_class_type:
10325 case DW_TAG_interface_type:
10326 case DW_TAG_structure_type:
10327 case DW_TAG_union_type:
10328 case DW_TAG_enumeration_type:
10329 case DW_TAG_enumerator:
10330 case DW_TAG_subprogram:
10331 case DW_TAG_inlined_subroutine:
10332 case DW_TAG_member:
10333 case DW_TAG_imported_declaration:
10334 return 1;
10335
10336 case DW_TAG_variable:
10337 case DW_TAG_constant:
10338 /* We only need to prefix "globally" visible variables. These include
10339 any variable marked with DW_AT_external or any variable that
10340 lives in a namespace. [Variables in anonymous namespaces
10341 require prefixing, but they are not DW_AT_external.] */
10342
10343 if (dwarf2_attr (die, DW_AT_specification, cu))
10344 {
10345 struct dwarf2_cu *spec_cu = cu;
10346
10347 return die_needs_namespace (die_specification (die, &spec_cu),
10348 spec_cu);
10349 }
10350
10351 attr = dwarf2_attr (die, DW_AT_external, cu);
10352 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10353 && die->parent->tag != DW_TAG_module)
10354 return 0;
10355 /* A variable in a lexical block of some kind does not need a
10356 namespace, even though in C++ such variables may be external
10357 and have a mangled name. */
10358 if (die->parent->tag == DW_TAG_lexical_block
10359 || die->parent->tag == DW_TAG_try_block
10360 || die->parent->tag == DW_TAG_catch_block
10361 || die->parent->tag == DW_TAG_subprogram)
10362 return 0;
10363 return 1;
10364
10365 default:
10366 return 0;
10367 }
10368 }
10369
10370 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10371 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10372 defined for the given DIE. */
10373
10374 static struct attribute *
10375 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10376 {
10377 struct attribute *attr;
10378
10379 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10380 if (attr == NULL)
10381 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10382
10383 return attr;
10384 }
10385
10386 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10387 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10388 defined for the given DIE. */
10389
10390 static const char *
10391 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10392 {
10393 const char *linkage_name;
10394
10395 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10396 if (linkage_name == NULL)
10397 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10398
10399 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10400 See https://github.com/rust-lang/rust/issues/32925. */
10401 if (cu->per_cu->lang == language_rust && linkage_name != NULL
10402 && strchr (linkage_name, '{') != NULL)
10403 linkage_name = NULL;
10404
10405 return linkage_name;
10406 }
10407
10408 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10409 compute the physname for the object, which include a method's:
10410 - formal parameters (C++),
10411 - receiver type (Go),
10412
10413 The term "physname" is a bit confusing.
10414 For C++, for example, it is the demangled name.
10415 For Go, for example, it's the mangled name.
10416
10417 For Ada, return the DIE's linkage name rather than the fully qualified
10418 name. PHYSNAME is ignored..
10419
10420 The result is allocated on the objfile->per_bfd's obstack and
10421 canonicalized. */
10422
10423 static const char *
10424 dwarf2_compute_name (const char *name,
10425 struct die_info *die, struct dwarf2_cu *cu,
10426 int physname)
10427 {
10428 struct objfile *objfile = cu->per_objfile->objfile;
10429
10430 if (name == NULL)
10431 name = dwarf2_name (die, cu);
10432
10433 enum language lang = cu->per_cu->lang;
10434
10435 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10436 but otherwise compute it by typename_concat inside GDB.
10437 FIXME: Actually this is not really true, or at least not always true.
10438 It's all very confusing. compute_and_set_names doesn't try to demangle
10439 Fortran names because there is no mangling standard. So new_symbol
10440 will set the demangled name to the result of dwarf2_full_name, and it is
10441 the demangled name that GDB uses if it exists. */
10442 if (lang == language_ada
10443 || (lang == language_fortran && physname))
10444 {
10445 /* For Ada unit, we prefer the linkage name over the name, as
10446 the former contains the exported name, which the user expects
10447 to be able to reference. Ideally, we want the user to be able
10448 to reference this entity using either natural or linkage name,
10449 but we haven't started looking at this enhancement yet. */
10450 const char *linkage_name = dw2_linkage_name (die, cu);
10451
10452 if (linkage_name != NULL)
10453 return linkage_name;
10454 }
10455
10456 /* These are the only languages we know how to qualify names in. */
10457 if (name != NULL
10458 && (lang == language_cplus
10459 || lang == language_fortran || lang == language_d
10460 || lang == language_rust))
10461 {
10462 if (die_needs_namespace (die, cu))
10463 {
10464 const char *prefix;
10465 const char *canonical_name = NULL;
10466
10467 string_file buf;
10468
10469 prefix = determine_prefix (die, cu);
10470 if (*prefix != '\0')
10471 {
10472 gdb::unique_xmalloc_ptr<char> prefixed_name
10473 (typename_concat (NULL, prefix, name, physname, cu));
10474
10475 buf.puts (prefixed_name.get ());
10476 }
10477 else
10478 buf.puts (name);
10479
10480 /* Template parameters may be specified in the DIE's DW_AT_name, or
10481 as children with DW_TAG_template_type_param or
10482 DW_TAG_value_type_param. If the latter, add them to the name
10483 here. If the name already has template parameters, then
10484 skip this step; some versions of GCC emit both, and
10485 it is more efficient to use the pre-computed name.
10486
10487 Something to keep in mind about this process: it is very
10488 unlikely, or in some cases downright impossible, to produce
10489 something that will match the mangled name of a function.
10490 If the definition of the function has the same debug info,
10491 we should be able to match up with it anyway. But fallbacks
10492 using the minimal symbol, for instance to find a method
10493 implemented in a stripped copy of libstdc++, will not work.
10494 If we do not have debug info for the definition, we will have to
10495 match them up some other way.
10496
10497 When we do name matching there is a related problem with function
10498 templates; two instantiated function templates are allowed to
10499 differ only by their return types, which we do not add here. */
10500
10501 if (lang == language_cplus && strchr (name, '<') == NULL)
10502 {
10503 struct attribute *attr;
10504 struct die_info *child;
10505 int first = 1;
10506
10507 die->building_fullname = 1;
10508
10509 for (child = die->child; child != NULL; child = child->sibling)
10510 {
10511 struct type *type;
10512 LONGEST value;
10513 const gdb_byte *bytes;
10514 struct dwarf2_locexpr_baton *baton;
10515 struct value *v;
10516
10517 if (child->tag != DW_TAG_template_type_param
10518 && child->tag != DW_TAG_template_value_param)
10519 continue;
10520
10521 if (first)
10522 {
10523 buf.puts ("<");
10524 first = 0;
10525 }
10526 else
10527 buf.puts (", ");
10528
10529 attr = dwarf2_attr (child, DW_AT_type, cu);
10530 if (attr == NULL)
10531 {
10532 complaint (_("template parameter missing DW_AT_type"));
10533 buf.puts ("UNKNOWN_TYPE");
10534 continue;
10535 }
10536 type = die_type (child, cu);
10537
10538 if (child->tag == DW_TAG_template_type_param)
10539 {
10540 cu->language_defn->print_type (type, "", &buf, -1, 0,
10541 &type_print_raw_options);
10542 continue;
10543 }
10544
10545 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10546 if (attr == NULL)
10547 {
10548 complaint (_("template parameter missing "
10549 "DW_AT_const_value"));
10550 buf.puts ("UNKNOWN_VALUE");
10551 continue;
10552 }
10553
10554 dwarf2_const_value_attr (attr, type, name,
10555 &cu->comp_unit_obstack, cu,
10556 &value, &bytes, &baton);
10557
10558 if (type->has_no_signedness ())
10559 /* GDB prints characters as NUMBER 'CHAR'. If that's
10560 changed, this can use value_print instead. */
10561 cu->language_defn->printchar (value, type, &buf);
10562 else
10563 {
10564 struct value_print_options opts;
10565
10566 if (baton != NULL)
10567 v = dwarf2_evaluate_loc_desc (type, NULL,
10568 baton->data,
10569 baton->size,
10570 baton->per_cu,
10571 baton->per_objfile);
10572 else if (bytes != NULL)
10573 {
10574 v = allocate_value (type);
10575 memcpy (value_contents_writeable (v).data (), bytes,
10576 TYPE_LENGTH (type));
10577 }
10578 else
10579 v = value_from_longest (type, value);
10580
10581 /* Specify decimal so that we do not depend on
10582 the radix. */
10583 get_formatted_print_options (&opts, 'd');
10584 opts.raw = 1;
10585 value_print (v, &buf, &opts);
10586 release_value (v);
10587 }
10588 }
10589
10590 die->building_fullname = 0;
10591
10592 if (!first)
10593 {
10594 /* Close the argument list, with a space if necessary
10595 (nested templates). */
10596 if (!buf.empty () && buf.string ().back () == '>')
10597 buf.puts (" >");
10598 else
10599 buf.puts (">");
10600 }
10601 }
10602
10603 /* For C++ methods, append formal parameter type
10604 information, if PHYSNAME. */
10605
10606 if (physname && die->tag == DW_TAG_subprogram
10607 && lang == language_cplus)
10608 {
10609 struct type *type = read_type_die (die, cu);
10610
10611 c_type_print_args (type, &buf, 1, lang,
10612 &type_print_raw_options);
10613
10614 if (lang == language_cplus)
10615 {
10616 /* Assume that an artificial first parameter is
10617 "this", but do not crash if it is not. RealView
10618 marks unnamed (and thus unused) parameters as
10619 artificial; there is no way to differentiate
10620 the two cases. */
10621 if (type->num_fields () > 0
10622 && TYPE_FIELD_ARTIFICIAL (type, 0)
10623 && type->field (0).type ()->code () == TYPE_CODE_PTR
10624 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10625 buf.puts (" const");
10626 }
10627 }
10628
10629 const std::string &intermediate_name = buf.string ();
10630
10631 if (lang == language_cplus)
10632 canonical_name
10633 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10634 objfile);
10635
10636 /* If we only computed INTERMEDIATE_NAME, or if
10637 INTERMEDIATE_NAME is already canonical, then we need to
10638 intern it. */
10639 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10640 name = objfile->intern (intermediate_name);
10641 else
10642 name = canonical_name;
10643 }
10644 }
10645
10646 return name;
10647 }
10648
10649 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10650 If scope qualifiers are appropriate they will be added. The result
10651 will be allocated on the storage_obstack, or NULL if the DIE does
10652 not have a name. NAME may either be from a previous call to
10653 dwarf2_name or NULL.
10654
10655 The output string will be canonicalized (if C++). */
10656
10657 static const char *
10658 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10659 {
10660 return dwarf2_compute_name (name, die, cu, 0);
10661 }
10662
10663 /* Construct a physname for the given DIE in CU. NAME may either be
10664 from a previous call to dwarf2_name or NULL. The result will be
10665 allocated on the objfile_objstack or NULL if the DIE does not have a
10666 name.
10667
10668 The output string will be canonicalized (if C++). */
10669
10670 static const char *
10671 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10672 {
10673 struct objfile *objfile = cu->per_objfile->objfile;
10674 const char *retval, *mangled = NULL, *canon = NULL;
10675 int need_copy = 1;
10676
10677 /* In this case dwarf2_compute_name is just a shortcut not building anything
10678 on its own. */
10679 if (!die_needs_namespace (die, cu))
10680 return dwarf2_compute_name (name, die, cu, 1);
10681
10682 if (cu->per_cu->lang != language_rust)
10683 mangled = dw2_linkage_name (die, cu);
10684
10685 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10686 has computed. */
10687 gdb::unique_xmalloc_ptr<char> demangled;
10688 if (mangled != NULL)
10689 {
10690 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10691 {
10692 /* Do nothing (do not demangle the symbol name). */
10693 }
10694 else
10695 {
10696 /* Use DMGL_RET_DROP for C++ template functions to suppress
10697 their return type. It is easier for GDB users to search
10698 for such functions as `name(params)' than `long name(params)'.
10699 In such case the minimal symbol names do not match the full
10700 symbol names but for template functions there is never a need
10701 to look up their definition from their declaration so
10702 the only disadvantage remains the minimal symbol variant
10703 `long name(params)' does not have the proper inferior type. */
10704 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10705 | DMGL_RET_DROP));
10706 }
10707 if (demangled)
10708 canon = demangled.get ();
10709 else
10710 {
10711 canon = mangled;
10712 need_copy = 0;
10713 }
10714 }
10715
10716 if (canon == NULL || check_physname)
10717 {
10718 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10719
10720 if (canon != NULL && strcmp (physname, canon) != 0)
10721 {
10722 /* It may not mean a bug in GDB. The compiler could also
10723 compute DW_AT_linkage_name incorrectly. But in such case
10724 GDB would need to be bug-to-bug compatible. */
10725
10726 complaint (_("Computed physname <%s> does not match demangled <%s> "
10727 "(from linkage <%s>) - DIE at %s [in module %s]"),
10728 physname, canon, mangled, sect_offset_str (die->sect_off),
10729 objfile_name (objfile));
10730
10731 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10732 is available here - over computed PHYSNAME. It is safer
10733 against both buggy GDB and buggy compilers. */
10734
10735 retval = canon;
10736 }
10737 else
10738 {
10739 retval = physname;
10740 need_copy = 0;
10741 }
10742 }
10743 else
10744 retval = canon;
10745
10746 if (need_copy)
10747 retval = objfile->intern (retval);
10748
10749 return retval;
10750 }
10751
10752 /* Inspect DIE in CU for a namespace alias. If one exists, record
10753 a new symbol for it.
10754
10755 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10756
10757 static int
10758 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10759 {
10760 struct attribute *attr;
10761
10762 /* If the die does not have a name, this is not a namespace
10763 alias. */
10764 attr = dwarf2_attr (die, DW_AT_name, cu);
10765 if (attr != NULL)
10766 {
10767 int num;
10768 struct die_info *d = die;
10769 struct dwarf2_cu *imported_cu = cu;
10770
10771 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10772 keep inspecting DIEs until we hit the underlying import. */
10773 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10774 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10775 {
10776 attr = dwarf2_attr (d, DW_AT_import, cu);
10777 if (attr == NULL)
10778 break;
10779
10780 d = follow_die_ref (d, attr, &imported_cu);
10781 if (d->tag != DW_TAG_imported_declaration)
10782 break;
10783 }
10784
10785 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10786 {
10787 complaint (_("DIE at %s has too many recursively imported "
10788 "declarations"), sect_offset_str (d->sect_off));
10789 return 0;
10790 }
10791
10792 if (attr != NULL)
10793 {
10794 struct type *type;
10795 sect_offset sect_off = attr->get_ref_die_offset ();
10796
10797 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10798 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10799 {
10800 /* This declaration is a global namespace alias. Add
10801 a symbol for it whose type is the aliased namespace. */
10802 new_symbol (die, type, cu);
10803 return 1;
10804 }
10805 }
10806 }
10807
10808 return 0;
10809 }
10810
10811 /* Return the using directives repository (global or local?) to use in the
10812 current context for CU.
10813
10814 For Ada, imported declarations can materialize renamings, which *may* be
10815 global. However it is impossible (for now?) in DWARF to distinguish
10816 "external" imported declarations and "static" ones. As all imported
10817 declarations seem to be static in all other languages, make them all CU-wide
10818 global only in Ada. */
10819
10820 static struct using_direct **
10821 using_directives (struct dwarf2_cu *cu)
10822 {
10823 if (cu->per_cu->lang == language_ada
10824 && cu->get_builder ()->outermost_context_p ())
10825 return cu->get_builder ()->get_global_using_directives ();
10826 else
10827 return cu->get_builder ()->get_local_using_directives ();
10828 }
10829
10830 /* Read the import statement specified by the given die and record it. */
10831
10832 static void
10833 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10834 {
10835 struct objfile *objfile = cu->per_objfile->objfile;
10836 struct attribute *import_attr;
10837 struct die_info *imported_die, *child_die;
10838 struct dwarf2_cu *imported_cu;
10839 const char *imported_name;
10840 const char *imported_name_prefix;
10841 const char *canonical_name;
10842 const char *import_alias;
10843 const char *imported_declaration = NULL;
10844 const char *import_prefix;
10845 std::vector<const char *> excludes;
10846
10847 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10848 if (import_attr == NULL)
10849 {
10850 complaint (_("Tag '%s' has no DW_AT_import"),
10851 dwarf_tag_name (die->tag));
10852 return;
10853 }
10854
10855 imported_cu = cu;
10856 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10857 imported_name = dwarf2_name (imported_die, imported_cu);
10858 if (imported_name == NULL)
10859 {
10860 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10861
10862 The import in the following code:
10863 namespace A
10864 {
10865 typedef int B;
10866 }
10867
10868 int main ()
10869 {
10870 using A::B;
10871 B b;
10872 return b;
10873 }
10874
10875 ...
10876 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10877 <52> DW_AT_decl_file : 1
10878 <53> DW_AT_decl_line : 6
10879 <54> DW_AT_import : <0x75>
10880 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10881 <59> DW_AT_name : B
10882 <5b> DW_AT_decl_file : 1
10883 <5c> DW_AT_decl_line : 2
10884 <5d> DW_AT_type : <0x6e>
10885 ...
10886 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10887 <76> DW_AT_byte_size : 4
10888 <77> DW_AT_encoding : 5 (signed)
10889
10890 imports the wrong die ( 0x75 instead of 0x58 ).
10891 This case will be ignored until the gcc bug is fixed. */
10892 return;
10893 }
10894
10895 /* Figure out the local name after import. */
10896 import_alias = dwarf2_name (die, cu);
10897
10898 /* Figure out where the statement is being imported to. */
10899 import_prefix = determine_prefix (die, cu);
10900
10901 /* Figure out what the scope of the imported die is and prepend it
10902 to the name of the imported die. */
10903 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10904
10905 if (imported_die->tag != DW_TAG_namespace
10906 && imported_die->tag != DW_TAG_module)
10907 {
10908 imported_declaration = imported_name;
10909 canonical_name = imported_name_prefix;
10910 }
10911 else if (strlen (imported_name_prefix) > 0)
10912 canonical_name = obconcat (&objfile->objfile_obstack,
10913 imported_name_prefix,
10914 (cu->per_cu->lang == language_d
10915 ? "."
10916 : "::"),
10917 imported_name, (char *) NULL);
10918 else
10919 canonical_name = imported_name;
10920
10921 if (die->tag == DW_TAG_imported_module
10922 && cu->per_cu->lang == language_fortran)
10923 for (child_die = die->child; child_die && child_die->tag;
10924 child_die = child_die->sibling)
10925 {
10926 /* DWARF-4: A Fortran use statement with a “rename list” may be
10927 represented by an imported module entry with an import attribute
10928 referring to the module and owned entries corresponding to those
10929 entities that are renamed as part of being imported. */
10930
10931 if (child_die->tag != DW_TAG_imported_declaration)
10932 {
10933 complaint (_("child DW_TAG_imported_declaration expected "
10934 "- DIE at %s [in module %s]"),
10935 sect_offset_str (child_die->sect_off),
10936 objfile_name (objfile));
10937 continue;
10938 }
10939
10940 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10941 if (import_attr == NULL)
10942 {
10943 complaint (_("Tag '%s' has no DW_AT_import"),
10944 dwarf_tag_name (child_die->tag));
10945 continue;
10946 }
10947
10948 imported_cu = cu;
10949 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10950 &imported_cu);
10951 imported_name = dwarf2_name (imported_die, imported_cu);
10952 if (imported_name == NULL)
10953 {
10954 complaint (_("child DW_TAG_imported_declaration has unknown "
10955 "imported name - DIE at %s [in module %s]"),
10956 sect_offset_str (child_die->sect_off),
10957 objfile_name (objfile));
10958 continue;
10959 }
10960
10961 excludes.push_back (imported_name);
10962
10963 process_die (child_die, cu);
10964 }
10965
10966 add_using_directive (using_directives (cu),
10967 import_prefix,
10968 canonical_name,
10969 import_alias,
10970 imported_declaration,
10971 excludes,
10972 0,
10973 &objfile->objfile_obstack);
10974 }
10975
10976 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10977 types, but gives them a size of zero. Starting with version 14,
10978 ICC is compatible with GCC. */
10979
10980 static bool
10981 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10982 {
10983 if (!cu->checked_producer)
10984 check_producer (cu);
10985
10986 return cu->producer_is_icc_lt_14;
10987 }
10988
10989 /* ICC generates a DW_AT_type for C void functions. This was observed on
10990 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10991 which says that void functions should not have a DW_AT_type. */
10992
10993 static bool
10994 producer_is_icc (struct dwarf2_cu *cu)
10995 {
10996 if (!cu->checked_producer)
10997 check_producer (cu);
10998
10999 return cu->producer_is_icc;
11000 }
11001
11002 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11003 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11004 this, it was first present in GCC release 4.3.0. */
11005
11006 static bool
11007 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11008 {
11009 if (!cu->checked_producer)
11010 check_producer (cu);
11011
11012 return cu->producer_is_gcc_lt_4_3;
11013 }
11014
11015 static file_and_directory &
11016 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11017 {
11018 if (cu->per_cu->fnd != nullptr)
11019 return *cu->per_cu->fnd;
11020
11021 /* Find the filename. Do not use dwarf2_name here, since the filename
11022 is not a source language identifier. */
11023 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
11024 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
11025
11026 if (res.get_comp_dir () == nullptr
11027 && producer_is_gcc_lt_4_3 (cu)
11028 && res.get_name () != nullptr
11029 && IS_ABSOLUTE_PATH (res.get_name ()))
11030 res.set_comp_dir (ldirname (res.get_name ()));
11031
11032 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
11033 return *cu->per_cu->fnd;
11034 }
11035
11036 /* Handle DW_AT_stmt_list for a compilation unit.
11037 DIE is the DW_TAG_compile_unit die for CU.
11038 COMP_DIR is the compilation directory. LOWPC is passed to
11039 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11040
11041 static void
11042 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11043 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
11044 {
11045 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11046 struct attribute *attr;
11047 struct line_header line_header_local;
11048 hashval_t line_header_local_hash;
11049 void **slot;
11050 int decode_mapping;
11051
11052 gdb_assert (! cu->per_cu->is_debug_types);
11053
11054 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11055 if (attr == NULL || !attr->form_is_unsigned ())
11056 return;
11057
11058 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11059
11060 /* The line header hash table is only created if needed (it exists to
11061 prevent redundant reading of the line table for partial_units).
11062 If we're given a partial_unit, we'll need it. If we're given a
11063 compile_unit, then use the line header hash table if it's already
11064 created, but don't create one just yet. */
11065
11066 if (per_objfile->line_header_hash == NULL
11067 && die->tag == DW_TAG_partial_unit)
11068 {
11069 per_objfile->line_header_hash
11070 .reset (htab_create_alloc (127, line_header_hash_voidp,
11071 line_header_eq_voidp,
11072 htab_delete_entry<line_header>,
11073 xcalloc, xfree));
11074 }
11075
11076 line_header_local.sect_off = line_offset;
11077 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11078 line_header_local_hash = line_header_hash (&line_header_local);
11079 if (per_objfile->line_header_hash != NULL)
11080 {
11081 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11082 &line_header_local,
11083 line_header_local_hash, NO_INSERT);
11084
11085 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11086 is not present in *SLOT (since if there is something in *SLOT then
11087 it will be for a partial_unit). */
11088 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11089 {
11090 gdb_assert (*slot != NULL);
11091 cu->line_header = (struct line_header *) *slot;
11092 return;
11093 }
11094 }
11095
11096 /* dwarf_decode_line_header does not yet provide sufficient information.
11097 We always have to call also dwarf_decode_lines for it. */
11098 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11099 if (lh == NULL)
11100 return;
11101
11102 cu->line_header = lh.release ();
11103 cu->line_header_die_owner = die;
11104
11105 if (per_objfile->line_header_hash == NULL)
11106 slot = NULL;
11107 else
11108 {
11109 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11110 &line_header_local,
11111 line_header_local_hash, INSERT);
11112 gdb_assert (slot != NULL);
11113 }
11114 if (slot != NULL && *slot == NULL)
11115 {
11116 /* This newly decoded line number information unit will be owned
11117 by line_header_hash hash table. */
11118 *slot = cu->line_header;
11119 cu->line_header_die_owner = NULL;
11120 }
11121 else
11122 {
11123 /* We cannot free any current entry in (*slot) as that struct line_header
11124 may be already used by multiple CUs. Create only temporary decoded
11125 line_header for this CU - it may happen at most once for each line
11126 number information unit. And if we're not using line_header_hash
11127 then this is what we want as well. */
11128 gdb_assert (die->tag != DW_TAG_partial_unit);
11129 }
11130 decode_mapping = (die->tag != DW_TAG_partial_unit);
11131 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
11132 decode_mapping);
11133
11134 }
11135
11136 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11137
11138 static void
11139 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11140 {
11141 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11142 struct objfile *objfile = per_objfile->objfile;
11143 struct gdbarch *gdbarch = objfile->arch ();
11144 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11145 CORE_ADDR highpc = ((CORE_ADDR) 0);
11146 struct attribute *attr;
11147 struct die_info *child_die;
11148 CORE_ADDR baseaddr;
11149
11150 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
11151 baseaddr = objfile->text_section_offset ();
11152
11153 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11154
11155 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11156 from finish_block. */
11157 if (lowpc == ((CORE_ADDR) -1))
11158 lowpc = highpc;
11159 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11160
11161 file_and_directory &fnd = find_file_and_directory (die, cu);
11162
11163 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
11164 lowpc);
11165
11166 gdb_assert (per_objfile->sym_cu == nullptr);
11167 scoped_restore restore_sym_cu
11168 = make_scoped_restore (&per_objfile->sym_cu, cu);
11169
11170 /* Decode line number information if present. We do this before
11171 processing child DIEs, so that the line header table is available
11172 for DW_AT_decl_file. The PC check is here because, if LOWPC and
11173 HIGHPC are both 0x0, then there won't be any interesting code in
11174 the CU, but a check later on (in
11175 lnp_state_machine::check_line_address) will fail to properly
11176 exclude an entry that was removed via --gc-sections. */
11177 if (lowpc != highpc)
11178 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
11179
11180 /* Process all dies in compilation unit. */
11181 if (die->child != NULL)
11182 {
11183 child_die = die->child;
11184 while (child_die && child_die->tag)
11185 {
11186 process_die (child_die, cu);
11187 child_die = child_die->sibling;
11188 }
11189 }
11190 per_objfile->sym_cu = nullptr;
11191
11192 /* Decode macro information, if present. Dwarf 2 macro information
11193 refers to information in the line number info statement program
11194 header, so we can only read it if we've read the header
11195 successfully. */
11196 attr = dwarf2_attr (die, DW_AT_macros, cu);
11197 if (attr == NULL)
11198 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11199 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11200 {
11201 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11202 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11203
11204 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11205 }
11206 else
11207 {
11208 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11209 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11210 {
11211 unsigned int macro_offset = attr->as_unsigned ();
11212
11213 dwarf_decode_macros (cu, macro_offset, 0);
11214 }
11215 }
11216 }
11217
11218 void
11219 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11220 {
11221 struct type_unit_group *tu_group;
11222 int first_time;
11223 struct attribute *attr;
11224 unsigned int i;
11225 struct signatured_type *sig_type;
11226
11227 gdb_assert (per_cu->is_debug_types);
11228 sig_type = (struct signatured_type *) per_cu;
11229
11230 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11231
11232 /* If we're using .gdb_index (includes -readnow) then
11233 per_cu->type_unit_group may not have been set up yet. */
11234 if (sig_type->type_unit_group == NULL)
11235 sig_type->type_unit_group = get_type_unit_group (this, attr);
11236 tu_group = sig_type->type_unit_group;
11237
11238 /* If we've already processed this stmt_list there's no real need to
11239 do it again, we could fake it and just recreate the part we need
11240 (file name,index -> symtab mapping). If data shows this optimization
11241 is useful we can do it then. */
11242 type_unit_group_unshareable *tug_unshare
11243 = per_objfile->get_type_unit_group_unshareable (tu_group);
11244 first_time = tug_unshare->compunit_symtab == NULL;
11245
11246 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11247 debug info. */
11248 line_header_up lh;
11249 if (attr != NULL && attr->form_is_unsigned ())
11250 {
11251 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11252 lh = dwarf_decode_line_header (line_offset, this);
11253 }
11254 if (lh == NULL)
11255 {
11256 if (first_time)
11257 start_compunit_symtab ("", NULL, 0);
11258 else
11259 {
11260 gdb_assert (tug_unshare->symtabs == NULL);
11261 gdb_assert (m_builder == nullptr);
11262 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11263 m_builder.reset (new struct buildsym_compunit
11264 (cust->objfile (), "",
11265 cust->dirname (),
11266 compunit_language (cust),
11267 0, cust));
11268 list_in_scope = get_builder ()->get_file_symbols ();
11269 }
11270 return;
11271 }
11272
11273 line_header = lh.release ();
11274 line_header_die_owner = die;
11275
11276 if (first_time)
11277 {
11278 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
11279
11280 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11281 still initializing it, and our caller (a few levels up)
11282 process_full_type_unit still needs to know if this is the first
11283 time. */
11284
11285 tug_unshare->symtabs
11286 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
11287 struct symtab *, line_header->file_names_size ());
11288
11289 auto &file_names = line_header->file_names ();
11290 for (i = 0; i < file_names.size (); ++i)
11291 {
11292 file_entry &fe = file_names[i];
11293 dwarf2_start_subfile (this, fe.name,
11294 fe.include_dir (line_header));
11295 buildsym_compunit *b = get_builder ();
11296 if (b->get_current_subfile ()->symtab == NULL)
11297 {
11298 /* NOTE: start_subfile will recognize when it's been
11299 passed a file it has already seen. So we can't
11300 assume there's a simple mapping from
11301 cu->line_header->file_names to subfiles, plus
11302 cu->line_header->file_names may contain dups. */
11303 b->get_current_subfile ()->symtab
11304 = allocate_symtab (cust, b->get_current_subfile ()->name);
11305 }
11306
11307 fe.symtab = b->get_current_subfile ()->symtab;
11308 tug_unshare->symtabs[i] = fe.symtab;
11309 }
11310 }
11311 else
11312 {
11313 gdb_assert (m_builder == nullptr);
11314 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11315 m_builder.reset (new struct buildsym_compunit
11316 (cust->objfile (), "",
11317 cust->dirname (),
11318 compunit_language (cust),
11319 0, cust));
11320 list_in_scope = get_builder ()->get_file_symbols ();
11321
11322 auto &file_names = line_header->file_names ();
11323 for (i = 0; i < file_names.size (); ++i)
11324 {
11325 file_entry &fe = file_names[i];
11326 fe.symtab = tug_unshare->symtabs[i];
11327 }
11328 }
11329
11330 /* The main symtab is allocated last. Type units don't have DW_AT_name
11331 so they don't have a "real" (so to speak) symtab anyway.
11332 There is later code that will assign the main symtab to all symbols
11333 that don't have one. We need to handle the case of a symbol with a
11334 missing symtab (DW_AT_decl_file) anyway. */
11335 }
11336
11337 /* Process DW_TAG_type_unit.
11338 For TUs we want to skip the first top level sibling if it's not the
11339 actual type being defined by this TU. In this case the first top
11340 level sibling is there to provide context only. */
11341
11342 static void
11343 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11344 {
11345 struct die_info *child_die;
11346
11347 prepare_one_comp_unit (cu, die, language_minimal);
11348
11349 /* Initialize (or reinitialize) the machinery for building symtabs.
11350 We do this before processing child DIEs, so that the line header table
11351 is available for DW_AT_decl_file. */
11352 cu->setup_type_unit_groups (die);
11353
11354 if (die->child != NULL)
11355 {
11356 child_die = die->child;
11357 while (child_die && child_die->tag)
11358 {
11359 process_die (child_die, cu);
11360 child_die = child_die->sibling;
11361 }
11362 }
11363 }
11364 \f
11365 /* DWO/DWP files.
11366
11367 http://gcc.gnu.org/wiki/DebugFission
11368 http://gcc.gnu.org/wiki/DebugFissionDWP
11369
11370 To simplify handling of both DWO files ("object" files with the DWARF info)
11371 and DWP files (a file with the DWOs packaged up into one file), we treat
11372 DWP files as having a collection of virtual DWO files. */
11373
11374 static hashval_t
11375 hash_dwo_file (const void *item)
11376 {
11377 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11378 hashval_t hash;
11379
11380 hash = htab_hash_string (dwo_file->dwo_name);
11381 if (dwo_file->comp_dir != NULL)
11382 hash += htab_hash_string (dwo_file->comp_dir);
11383 return hash;
11384 }
11385
11386 static int
11387 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11388 {
11389 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11390 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11391
11392 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11393 return 0;
11394 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11395 return lhs->comp_dir == rhs->comp_dir;
11396 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11397 }
11398
11399 /* Allocate a hash table for DWO files. */
11400
11401 static htab_up
11402 allocate_dwo_file_hash_table ()
11403 {
11404 return htab_up (htab_create_alloc (41,
11405 hash_dwo_file,
11406 eq_dwo_file,
11407 htab_delete_entry<dwo_file>,
11408 xcalloc, xfree));
11409 }
11410
11411 /* Lookup DWO file DWO_NAME. */
11412
11413 static void **
11414 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11415 const char *dwo_name,
11416 const char *comp_dir)
11417 {
11418 struct dwo_file find_entry;
11419 void **slot;
11420
11421 if (per_objfile->per_bfd->dwo_files == NULL)
11422 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11423
11424 find_entry.dwo_name = dwo_name;
11425 find_entry.comp_dir = comp_dir;
11426 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11427 INSERT);
11428
11429 return slot;
11430 }
11431
11432 static hashval_t
11433 hash_dwo_unit (const void *item)
11434 {
11435 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11436
11437 /* This drops the top 32 bits of the id, but is ok for a hash. */
11438 return dwo_unit->signature;
11439 }
11440
11441 static int
11442 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11443 {
11444 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11445 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11446
11447 /* The signature is assumed to be unique within the DWO file.
11448 So while object file CU dwo_id's always have the value zero,
11449 that's OK, assuming each object file DWO file has only one CU,
11450 and that's the rule for now. */
11451 return lhs->signature == rhs->signature;
11452 }
11453
11454 /* Allocate a hash table for DWO CUs,TUs.
11455 There is one of these tables for each of CUs,TUs for each DWO file. */
11456
11457 static htab_up
11458 allocate_dwo_unit_table ()
11459 {
11460 /* Start out with a pretty small number.
11461 Generally DWO files contain only one CU and maybe some TUs. */
11462 return htab_up (htab_create_alloc (3,
11463 hash_dwo_unit,
11464 eq_dwo_unit,
11465 NULL, xcalloc, xfree));
11466 }
11467
11468 /* die_reader_func for create_dwo_cu. */
11469
11470 static void
11471 create_dwo_cu_reader (const struct die_reader_specs *reader,
11472 const gdb_byte *info_ptr,
11473 struct die_info *comp_unit_die,
11474 struct dwo_file *dwo_file,
11475 struct dwo_unit *dwo_unit)
11476 {
11477 struct dwarf2_cu *cu = reader->cu;
11478 sect_offset sect_off = cu->per_cu->sect_off;
11479 struct dwarf2_section_info *section = cu->per_cu->section;
11480
11481 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11482 if (!signature.has_value ())
11483 {
11484 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11485 " its dwo_id [in module %s]"),
11486 sect_offset_str (sect_off), dwo_file->dwo_name);
11487 return;
11488 }
11489
11490 dwo_unit->dwo_file = dwo_file;
11491 dwo_unit->signature = *signature;
11492 dwo_unit->section = section;
11493 dwo_unit->sect_off = sect_off;
11494 dwo_unit->length = cu->per_cu->length;
11495
11496 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11497 sect_offset_str (sect_off),
11498 hex_string (dwo_unit->signature));
11499 }
11500
11501 /* Create the dwo_units for the CUs in a DWO_FILE.
11502 Note: This function processes DWO files only, not DWP files. */
11503
11504 static void
11505 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11506 dwarf2_cu *cu, struct dwo_file &dwo_file,
11507 dwarf2_section_info &section, htab_up &cus_htab)
11508 {
11509 struct objfile *objfile = per_objfile->objfile;
11510 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11511 const gdb_byte *info_ptr, *end_ptr;
11512
11513 section.read (objfile);
11514 info_ptr = section.buffer;
11515
11516 if (info_ptr == NULL)
11517 return;
11518
11519 dwarf_read_debug_printf ("Reading %s for %s:",
11520 section.get_name (),
11521 section.get_file_name ());
11522
11523 end_ptr = info_ptr + section.size;
11524 while (info_ptr < end_ptr)
11525 {
11526 struct dwarf2_per_cu_data per_cu;
11527 struct dwo_unit read_unit {};
11528 struct dwo_unit *dwo_unit;
11529 void **slot;
11530 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11531
11532 per_cu.per_bfd = per_bfd;
11533 per_cu.is_debug_types = 0;
11534 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11535 per_cu.section = &section;
11536
11537 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11538 if (!reader.dummy_p)
11539 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11540 &dwo_file, &read_unit);
11541 info_ptr += per_cu.length;
11542
11543 // If the unit could not be parsed, skip it.
11544 if (read_unit.dwo_file == NULL)
11545 continue;
11546
11547 if (cus_htab == NULL)
11548 cus_htab = allocate_dwo_unit_table ();
11549
11550 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11551 struct dwo_unit);
11552 *dwo_unit = read_unit;
11553 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11554 gdb_assert (slot != NULL);
11555 if (*slot != NULL)
11556 {
11557 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11558 sect_offset dup_sect_off = dup_cu->sect_off;
11559
11560 complaint (_("debug cu entry at offset %s is duplicate to"
11561 " the entry at offset %s, signature %s"),
11562 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11563 hex_string (dwo_unit->signature));
11564 }
11565 *slot = (void *)dwo_unit;
11566 }
11567 }
11568
11569 /* DWP file .debug_{cu,tu}_index section format:
11570 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11571 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11572
11573 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11574 officially standard DWP format was published with DWARF v5 and is called
11575 Version 5. There are no versions 3 or 4.
11576
11577 DWP Version 1:
11578
11579 Both index sections have the same format, and serve to map a 64-bit
11580 signature to a set of section numbers. Each section begins with a header,
11581 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11582 indexes, and a pool of 32-bit section numbers. The index sections will be
11583 aligned at 8-byte boundaries in the file.
11584
11585 The index section header consists of:
11586
11587 V, 32 bit version number
11588 -, 32 bits unused
11589 N, 32 bit number of compilation units or type units in the index
11590 M, 32 bit number of slots in the hash table
11591
11592 Numbers are recorded using the byte order of the application binary.
11593
11594 The hash table begins at offset 16 in the section, and consists of an array
11595 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11596 order of the application binary). Unused slots in the hash table are 0.
11597 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11598
11599 The parallel table begins immediately after the hash table
11600 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11601 array of 32-bit indexes (using the byte order of the application binary),
11602 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11603 table contains a 32-bit index into the pool of section numbers. For unused
11604 hash table slots, the corresponding entry in the parallel table will be 0.
11605
11606 The pool of section numbers begins immediately following the hash table
11607 (at offset 16 + 12 * M from the beginning of the section). The pool of
11608 section numbers consists of an array of 32-bit words (using the byte order
11609 of the application binary). Each item in the array is indexed starting
11610 from 0. The hash table entry provides the index of the first section
11611 number in the set. Additional section numbers in the set follow, and the
11612 set is terminated by a 0 entry (section number 0 is not used in ELF).
11613
11614 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11615 section must be the first entry in the set, and the .debug_abbrev.dwo must
11616 be the second entry. Other members of the set may follow in any order.
11617
11618 ---
11619
11620 DWP Versions 2 and 5:
11621
11622 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11623 and the entries in the index tables are now offsets into these sections.
11624 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11625 section.
11626
11627 Index Section Contents:
11628 Header
11629 Hash Table of Signatures dwp_hash_table.hash_table
11630 Parallel Table of Indices dwp_hash_table.unit_table
11631 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11632 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11633
11634 The index section header consists of:
11635
11636 V, 32 bit version number
11637 L, 32 bit number of columns in the table of section offsets
11638 N, 32 bit number of compilation units or type units in the index
11639 M, 32 bit number of slots in the hash table
11640
11641 Numbers are recorded using the byte order of the application binary.
11642
11643 The hash table has the same format as version 1.
11644 The parallel table of indices has the same format as version 1,
11645 except that the entries are origin-1 indices into the table of sections
11646 offsets and the table of section sizes.
11647
11648 The table of offsets begins immediately following the parallel table
11649 (at offset 16 + 12 * M from the beginning of the section). The table is
11650 a two-dimensional array of 32-bit words (using the byte order of the
11651 application binary), with L columns and N+1 rows, in row-major order.
11652 Each row in the array is indexed starting from 0. The first row provides
11653 a key to the remaining rows: each column in this row provides an identifier
11654 for a debug section, and the offsets in the same column of subsequent rows
11655 refer to that section. The section identifiers for Version 2 are:
11656
11657 DW_SECT_INFO 1 .debug_info.dwo
11658 DW_SECT_TYPES 2 .debug_types.dwo
11659 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11660 DW_SECT_LINE 4 .debug_line.dwo
11661 DW_SECT_LOC 5 .debug_loc.dwo
11662 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11663 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11664 DW_SECT_MACRO 8 .debug_macro.dwo
11665
11666 The section identifiers for Version 5 are:
11667
11668 DW_SECT_INFO_V5 1 .debug_info.dwo
11669 DW_SECT_RESERVED_V5 2 --
11670 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11671 DW_SECT_LINE_V5 4 .debug_line.dwo
11672 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11673 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11674 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11675 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11676
11677 The offsets provided by the CU and TU index sections are the base offsets
11678 for the contributions made by each CU or TU to the corresponding section
11679 in the package file. Each CU and TU header contains an abbrev_offset
11680 field, used to find the abbreviations table for that CU or TU within the
11681 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11682 be interpreted as relative to the base offset given in the index section.
11683 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11684 should be interpreted as relative to the base offset for .debug_line.dwo,
11685 and offsets into other debug sections obtained from DWARF attributes should
11686 also be interpreted as relative to the corresponding base offset.
11687
11688 The table of sizes begins immediately following the table of offsets.
11689 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11690 with L columns and N rows, in row-major order. Each row in the array is
11691 indexed starting from 1 (row 0 is shared by the two tables).
11692
11693 ---
11694
11695 Hash table lookup is handled the same in version 1 and 2:
11696
11697 We assume that N and M will not exceed 2^32 - 1.
11698 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11699
11700 Given a 64-bit compilation unit signature or a type signature S, an entry
11701 in the hash table is located as follows:
11702
11703 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11704 the low-order k bits all set to 1.
11705
11706 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11707
11708 3) If the hash table entry at index H matches the signature, use that
11709 entry. If the hash table entry at index H is unused (all zeroes),
11710 terminate the search: the signature is not present in the table.
11711
11712 4) Let H = (H + H') modulo M. Repeat at Step 3.
11713
11714 Because M > N and H' and M are relatively prime, the search is guaranteed
11715 to stop at an unused slot or find the match. */
11716
11717 /* Create a hash table to map DWO IDs to their CU/TU entry in
11718 .debug_{info,types}.dwo in DWP_FILE.
11719 Returns NULL if there isn't one.
11720 Note: This function processes DWP files only, not DWO files. */
11721
11722 static struct dwp_hash_table *
11723 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11724 struct dwp_file *dwp_file, int is_debug_types)
11725 {
11726 struct objfile *objfile = per_objfile->objfile;
11727 bfd *dbfd = dwp_file->dbfd.get ();
11728 const gdb_byte *index_ptr, *index_end;
11729 struct dwarf2_section_info *index;
11730 uint32_t version, nr_columns, nr_units, nr_slots;
11731 struct dwp_hash_table *htab;
11732
11733 if (is_debug_types)
11734 index = &dwp_file->sections.tu_index;
11735 else
11736 index = &dwp_file->sections.cu_index;
11737
11738 if (index->empty ())
11739 return NULL;
11740 index->read (objfile);
11741
11742 index_ptr = index->buffer;
11743 index_end = index_ptr + index->size;
11744
11745 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11746 For now it's safe to just read 4 bytes (particularly as it's difficult to
11747 tell if you're dealing with Version 5 before you've read the version). */
11748 version = read_4_bytes (dbfd, index_ptr);
11749 index_ptr += 4;
11750 if (version == 2 || version == 5)
11751 nr_columns = read_4_bytes (dbfd, index_ptr);
11752 else
11753 nr_columns = 0;
11754 index_ptr += 4;
11755 nr_units = read_4_bytes (dbfd, index_ptr);
11756 index_ptr += 4;
11757 nr_slots = read_4_bytes (dbfd, index_ptr);
11758 index_ptr += 4;
11759
11760 if (version != 1 && version != 2 && version != 5)
11761 {
11762 error (_("Dwarf Error: unsupported DWP file version (%s)"
11763 " [in module %s]"),
11764 pulongest (version), dwp_file->name);
11765 }
11766 if (nr_slots != (nr_slots & -nr_slots))
11767 {
11768 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11769 " is not power of 2 [in module %s]"),
11770 pulongest (nr_slots), dwp_file->name);
11771 }
11772
11773 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11774 htab->version = version;
11775 htab->nr_columns = nr_columns;
11776 htab->nr_units = nr_units;
11777 htab->nr_slots = nr_slots;
11778 htab->hash_table = index_ptr;
11779 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11780
11781 /* Exit early if the table is empty. */
11782 if (nr_slots == 0 || nr_units == 0
11783 || (version == 2 && nr_columns == 0)
11784 || (version == 5 && nr_columns == 0))
11785 {
11786 /* All must be zero. */
11787 if (nr_slots != 0 || nr_units != 0
11788 || (version == 2 && nr_columns != 0)
11789 || (version == 5 && nr_columns != 0))
11790 {
11791 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11792 " all zero [in modules %s]"),
11793 dwp_file->name);
11794 }
11795 return htab;
11796 }
11797
11798 if (version == 1)
11799 {
11800 htab->section_pool.v1.indices =
11801 htab->unit_table + sizeof (uint32_t) * nr_slots;
11802 /* It's harder to decide whether the section is too small in v1.
11803 V1 is deprecated anyway so we punt. */
11804 }
11805 else if (version == 2)
11806 {
11807 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11808 int *ids = htab->section_pool.v2.section_ids;
11809 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11810 /* Reverse map for error checking. */
11811 int ids_seen[DW_SECT_MAX + 1];
11812 int i;
11813
11814 if (nr_columns < 2)
11815 {
11816 error (_("Dwarf Error: bad DWP hash table, too few columns"
11817 " in section table [in module %s]"),
11818 dwp_file->name);
11819 }
11820 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11821 {
11822 error (_("Dwarf Error: bad DWP hash table, too many columns"
11823 " in section table [in module %s]"),
11824 dwp_file->name);
11825 }
11826 memset (ids, 255, sizeof_ids);
11827 memset (ids_seen, 255, sizeof (ids_seen));
11828 for (i = 0; i < nr_columns; ++i)
11829 {
11830 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11831
11832 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11833 {
11834 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11835 " in section table [in module %s]"),
11836 id, dwp_file->name);
11837 }
11838 if (ids_seen[id] != -1)
11839 {
11840 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11841 " id %d in section table [in module %s]"),
11842 id, dwp_file->name);
11843 }
11844 ids_seen[id] = i;
11845 ids[i] = id;
11846 }
11847 /* Must have exactly one info or types section. */
11848 if (((ids_seen[DW_SECT_INFO] != -1)
11849 + (ids_seen[DW_SECT_TYPES] != -1))
11850 != 1)
11851 {
11852 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11853 " DWO info/types section [in module %s]"),
11854 dwp_file->name);
11855 }
11856 /* Must have an abbrev section. */
11857 if (ids_seen[DW_SECT_ABBREV] == -1)
11858 {
11859 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11860 " section [in module %s]"),
11861 dwp_file->name);
11862 }
11863 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11864 htab->section_pool.v2.sizes =
11865 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11866 * nr_units * nr_columns);
11867 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11868 * nr_units * nr_columns))
11869 > index_end)
11870 {
11871 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11872 " [in module %s]"),
11873 dwp_file->name);
11874 }
11875 }
11876 else /* version == 5 */
11877 {
11878 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11879 int *ids = htab->section_pool.v5.section_ids;
11880 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11881 /* Reverse map for error checking. */
11882 int ids_seen[DW_SECT_MAX_V5 + 1];
11883
11884 if (nr_columns < 2)
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, too few columns"
11887 " in section table [in module %s]"),
11888 dwp_file->name);
11889 }
11890 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, too many columns"
11893 " in section table [in module %s]"),
11894 dwp_file->name);
11895 }
11896 memset (ids, 255, sizeof_ids);
11897 memset (ids_seen, 255, sizeof (ids_seen));
11898 for (int i = 0; i < nr_columns; ++i)
11899 {
11900 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11901
11902 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11903 {
11904 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11905 " in section table [in module %s]"),
11906 id, dwp_file->name);
11907 }
11908 if (ids_seen[id] != -1)
11909 {
11910 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11911 " id %d in section table [in module %s]"),
11912 id, dwp_file->name);
11913 }
11914 ids_seen[id] = i;
11915 ids[i] = id;
11916 }
11917 /* Must have seen an info section. */
11918 if (ids_seen[DW_SECT_INFO_V5] == -1)
11919 {
11920 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11921 " DWO info/types section [in module %s]"),
11922 dwp_file->name);
11923 }
11924 /* Must have an abbrev section. */
11925 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11926 {
11927 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11928 " section [in module %s]"),
11929 dwp_file->name);
11930 }
11931 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11932 htab->section_pool.v5.sizes
11933 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11934 * nr_units * nr_columns);
11935 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11936 * nr_units * nr_columns))
11937 > index_end)
11938 {
11939 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11940 " [in module %s]"),
11941 dwp_file->name);
11942 }
11943 }
11944
11945 return htab;
11946 }
11947
11948 /* Update SECTIONS with the data from SECTP.
11949
11950 This function is like the other "locate" section routines, but in
11951 this context the sections to read comes from the DWP V1 hash table,
11952 not the full ELF section table.
11953
11954 The result is non-zero for success, or zero if an error was found. */
11955
11956 static int
11957 locate_v1_virtual_dwo_sections (asection *sectp,
11958 struct virtual_v1_dwo_sections *sections)
11959 {
11960 const struct dwop_section_names *names = &dwop_section_names;
11961
11962 if (names->abbrev_dwo.matches (sectp->name))
11963 {
11964 /* There can be only one. */
11965 if (sections->abbrev.s.section != NULL)
11966 return 0;
11967 sections->abbrev.s.section = sectp;
11968 sections->abbrev.size = bfd_section_size (sectp);
11969 }
11970 else if (names->info_dwo.matches (sectp->name)
11971 || names->types_dwo.matches (sectp->name))
11972 {
11973 /* There can be only one. */
11974 if (sections->info_or_types.s.section != NULL)
11975 return 0;
11976 sections->info_or_types.s.section = sectp;
11977 sections->info_or_types.size = bfd_section_size (sectp);
11978 }
11979 else if (names->line_dwo.matches (sectp->name))
11980 {
11981 /* There can be only one. */
11982 if (sections->line.s.section != NULL)
11983 return 0;
11984 sections->line.s.section = sectp;
11985 sections->line.size = bfd_section_size (sectp);
11986 }
11987 else if (names->loc_dwo.matches (sectp->name))
11988 {
11989 /* There can be only one. */
11990 if (sections->loc.s.section != NULL)
11991 return 0;
11992 sections->loc.s.section = sectp;
11993 sections->loc.size = bfd_section_size (sectp);
11994 }
11995 else if (names->macinfo_dwo.matches (sectp->name))
11996 {
11997 /* There can be only one. */
11998 if (sections->macinfo.s.section != NULL)
11999 return 0;
12000 sections->macinfo.s.section = sectp;
12001 sections->macinfo.size = bfd_section_size (sectp);
12002 }
12003 else if (names->macro_dwo.matches (sectp->name))
12004 {
12005 /* There can be only one. */
12006 if (sections->macro.s.section != NULL)
12007 return 0;
12008 sections->macro.s.section = sectp;
12009 sections->macro.size = bfd_section_size (sectp);
12010 }
12011 else if (names->str_offsets_dwo.matches (sectp->name))
12012 {
12013 /* There can be only one. */
12014 if (sections->str_offsets.s.section != NULL)
12015 return 0;
12016 sections->str_offsets.s.section = sectp;
12017 sections->str_offsets.size = bfd_section_size (sectp);
12018 }
12019 else
12020 {
12021 /* No other kind of section is valid. */
12022 return 0;
12023 }
12024
12025 return 1;
12026 }
12027
12028 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12029 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12030 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12031 This is for DWP version 1 files. */
12032
12033 static struct dwo_unit *
12034 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12035 struct dwp_file *dwp_file,
12036 uint32_t unit_index,
12037 const char *comp_dir,
12038 ULONGEST signature, int is_debug_types)
12039 {
12040 const struct dwp_hash_table *dwp_htab =
12041 is_debug_types ? dwp_file->tus : dwp_file->cus;
12042 bfd *dbfd = dwp_file->dbfd.get ();
12043 const char *kind = is_debug_types ? "TU" : "CU";
12044 struct dwo_file *dwo_file;
12045 struct dwo_unit *dwo_unit;
12046 struct virtual_v1_dwo_sections sections;
12047 void **dwo_file_slot;
12048 int i;
12049
12050 gdb_assert (dwp_file->version == 1);
12051
12052 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12053 kind, pulongest (unit_index), hex_string (signature),
12054 dwp_file->name);
12055
12056 /* Fetch the sections of this DWO unit.
12057 Put a limit on the number of sections we look for so that bad data
12058 doesn't cause us to loop forever. */
12059
12060 #define MAX_NR_V1_DWO_SECTIONS \
12061 (1 /* .debug_info or .debug_types */ \
12062 + 1 /* .debug_abbrev */ \
12063 + 1 /* .debug_line */ \
12064 + 1 /* .debug_loc */ \
12065 + 1 /* .debug_str_offsets */ \
12066 + 1 /* .debug_macro or .debug_macinfo */ \
12067 + 1 /* trailing zero */)
12068
12069 memset (&sections, 0, sizeof (sections));
12070
12071 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12072 {
12073 asection *sectp;
12074 uint32_t section_nr =
12075 read_4_bytes (dbfd,
12076 dwp_htab->section_pool.v1.indices
12077 + (unit_index + i) * sizeof (uint32_t));
12078
12079 if (section_nr == 0)
12080 break;
12081 if (section_nr >= dwp_file->num_sections)
12082 {
12083 error (_("Dwarf Error: bad DWP hash table, section number too large"
12084 " [in module %s]"),
12085 dwp_file->name);
12086 }
12087
12088 sectp = dwp_file->elf_sections[section_nr];
12089 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12090 {
12091 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12092 " [in module %s]"),
12093 dwp_file->name);
12094 }
12095 }
12096
12097 if (i < 2
12098 || sections.info_or_types.empty ()
12099 || sections.abbrev.empty ())
12100 {
12101 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12102 " [in module %s]"),
12103 dwp_file->name);
12104 }
12105 if (i == MAX_NR_V1_DWO_SECTIONS)
12106 {
12107 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12108 " [in module %s]"),
12109 dwp_file->name);
12110 }
12111
12112 /* It's easier for the rest of the code if we fake a struct dwo_file and
12113 have dwo_unit "live" in that. At least for now.
12114
12115 The DWP file can be made up of a random collection of CUs and TUs.
12116 However, for each CU + set of TUs that came from the same original DWO
12117 file, we can combine them back into a virtual DWO file to save space
12118 (fewer struct dwo_file objects to allocate). Remember that for really
12119 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12120
12121 std::string virtual_dwo_name =
12122 string_printf ("virtual-dwo/%d-%d-%d-%d",
12123 sections.abbrev.get_id (),
12124 sections.line.get_id (),
12125 sections.loc.get_id (),
12126 sections.str_offsets.get_id ());
12127 /* Can we use an existing virtual DWO file? */
12128 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12129 comp_dir);
12130 /* Create one if necessary. */
12131 if (*dwo_file_slot == NULL)
12132 {
12133 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12134 virtual_dwo_name.c_str ());
12135
12136 dwo_file = new struct dwo_file;
12137 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12138 dwo_file->comp_dir = comp_dir;
12139 dwo_file->sections.abbrev = sections.abbrev;
12140 dwo_file->sections.line = sections.line;
12141 dwo_file->sections.loc = sections.loc;
12142 dwo_file->sections.macinfo = sections.macinfo;
12143 dwo_file->sections.macro = sections.macro;
12144 dwo_file->sections.str_offsets = sections.str_offsets;
12145 /* The "str" section is global to the entire DWP file. */
12146 dwo_file->sections.str = dwp_file->sections.str;
12147 /* The info or types section is assigned below to dwo_unit,
12148 there's no need to record it in dwo_file.
12149 Also, we can't simply record type sections in dwo_file because
12150 we record a pointer into the vector in dwo_unit. As we collect more
12151 types we'll grow the vector and eventually have to reallocate space
12152 for it, invalidating all copies of pointers into the previous
12153 contents. */
12154 *dwo_file_slot = dwo_file;
12155 }
12156 else
12157 {
12158 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12159 virtual_dwo_name.c_str ());
12160
12161 dwo_file = (struct dwo_file *) *dwo_file_slot;
12162 }
12163
12164 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12165 dwo_unit->dwo_file = dwo_file;
12166 dwo_unit->signature = signature;
12167 dwo_unit->section =
12168 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12169 *dwo_unit->section = sections.info_or_types;
12170 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12171
12172 return dwo_unit;
12173 }
12174
12175 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12176 simplify them. Given a pointer to the containing section SECTION, and
12177 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12178 virtual section of just that piece. */
12179
12180 static struct dwarf2_section_info
12181 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12182 struct dwarf2_section_info *section,
12183 bfd_size_type offset, bfd_size_type size)
12184 {
12185 struct dwarf2_section_info result;
12186 asection *sectp;
12187
12188 gdb_assert (section != NULL);
12189 gdb_assert (!section->is_virtual);
12190
12191 memset (&result, 0, sizeof (result));
12192 result.s.containing_section = section;
12193 result.is_virtual = true;
12194
12195 if (size == 0)
12196 return result;
12197
12198 sectp = section->get_bfd_section ();
12199
12200 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12201 bounds of the real section. This is a pretty-rare event, so just
12202 flag an error (easier) instead of a warning and trying to cope. */
12203 if (sectp == NULL
12204 || offset + size > bfd_section_size (sectp))
12205 {
12206 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12207 " in section %s [in module %s]"),
12208 sectp ? bfd_section_name (sectp) : "<unknown>",
12209 objfile_name (per_objfile->objfile));
12210 }
12211
12212 result.virtual_offset = offset;
12213 result.size = size;
12214 return result;
12215 }
12216
12217 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12218 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12219 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12220 This is for DWP version 2 files. */
12221
12222 static struct dwo_unit *
12223 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12224 struct dwp_file *dwp_file,
12225 uint32_t unit_index,
12226 const char *comp_dir,
12227 ULONGEST signature, int is_debug_types)
12228 {
12229 const struct dwp_hash_table *dwp_htab =
12230 is_debug_types ? dwp_file->tus : dwp_file->cus;
12231 bfd *dbfd = dwp_file->dbfd.get ();
12232 const char *kind = is_debug_types ? "TU" : "CU";
12233 struct dwo_file *dwo_file;
12234 struct dwo_unit *dwo_unit;
12235 struct virtual_v2_or_v5_dwo_sections sections;
12236 void **dwo_file_slot;
12237 int i;
12238
12239 gdb_assert (dwp_file->version == 2);
12240
12241 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12242 kind, pulongest (unit_index), hex_string (signature),
12243 dwp_file->name);
12244
12245 /* Fetch the section offsets of this DWO unit. */
12246
12247 memset (&sections, 0, sizeof (sections));
12248
12249 for (i = 0; i < dwp_htab->nr_columns; ++i)
12250 {
12251 uint32_t offset = read_4_bytes (dbfd,
12252 dwp_htab->section_pool.v2.offsets
12253 + (((unit_index - 1) * dwp_htab->nr_columns
12254 + i)
12255 * sizeof (uint32_t)));
12256 uint32_t size = read_4_bytes (dbfd,
12257 dwp_htab->section_pool.v2.sizes
12258 + (((unit_index - 1) * dwp_htab->nr_columns
12259 + i)
12260 * sizeof (uint32_t)));
12261
12262 switch (dwp_htab->section_pool.v2.section_ids[i])
12263 {
12264 case DW_SECT_INFO:
12265 case DW_SECT_TYPES:
12266 sections.info_or_types_offset = offset;
12267 sections.info_or_types_size = size;
12268 break;
12269 case DW_SECT_ABBREV:
12270 sections.abbrev_offset = offset;
12271 sections.abbrev_size = size;
12272 break;
12273 case DW_SECT_LINE:
12274 sections.line_offset = offset;
12275 sections.line_size = size;
12276 break;
12277 case DW_SECT_LOC:
12278 sections.loc_offset = offset;
12279 sections.loc_size = size;
12280 break;
12281 case DW_SECT_STR_OFFSETS:
12282 sections.str_offsets_offset = offset;
12283 sections.str_offsets_size = size;
12284 break;
12285 case DW_SECT_MACINFO:
12286 sections.macinfo_offset = offset;
12287 sections.macinfo_size = size;
12288 break;
12289 case DW_SECT_MACRO:
12290 sections.macro_offset = offset;
12291 sections.macro_size = size;
12292 break;
12293 }
12294 }
12295
12296 /* It's easier for the rest of the code if we fake a struct dwo_file and
12297 have dwo_unit "live" in that. At least for now.
12298
12299 The DWP file can be made up of a random collection of CUs and TUs.
12300 However, for each CU + set of TUs that came from the same original DWO
12301 file, we can combine them back into a virtual DWO file to save space
12302 (fewer struct dwo_file objects to allocate). Remember that for really
12303 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12304
12305 std::string virtual_dwo_name =
12306 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12307 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12308 (long) (sections.line_size ? sections.line_offset : 0),
12309 (long) (sections.loc_size ? sections.loc_offset : 0),
12310 (long) (sections.str_offsets_size
12311 ? sections.str_offsets_offset : 0));
12312 /* Can we use an existing virtual DWO file? */
12313 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12314 comp_dir);
12315 /* Create one if necessary. */
12316 if (*dwo_file_slot == NULL)
12317 {
12318 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12319 virtual_dwo_name.c_str ());
12320
12321 dwo_file = new struct dwo_file;
12322 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12323 dwo_file->comp_dir = comp_dir;
12324 dwo_file->sections.abbrev =
12325 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12326 sections.abbrev_offset,
12327 sections.abbrev_size);
12328 dwo_file->sections.line =
12329 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12330 sections.line_offset,
12331 sections.line_size);
12332 dwo_file->sections.loc =
12333 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12334 sections.loc_offset, sections.loc_size);
12335 dwo_file->sections.macinfo =
12336 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12337 sections.macinfo_offset,
12338 sections.macinfo_size);
12339 dwo_file->sections.macro =
12340 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12341 sections.macro_offset,
12342 sections.macro_size);
12343 dwo_file->sections.str_offsets =
12344 create_dwp_v2_or_v5_section (per_objfile,
12345 &dwp_file->sections.str_offsets,
12346 sections.str_offsets_offset,
12347 sections.str_offsets_size);
12348 /* The "str" section is global to the entire DWP file. */
12349 dwo_file->sections.str = dwp_file->sections.str;
12350 /* The info or types section is assigned below to dwo_unit,
12351 there's no need to record it in dwo_file.
12352 Also, we can't simply record type sections in dwo_file because
12353 we record a pointer into the vector in dwo_unit. As we collect more
12354 types we'll grow the vector and eventually have to reallocate space
12355 for it, invalidating all copies of pointers into the previous
12356 contents. */
12357 *dwo_file_slot = dwo_file;
12358 }
12359 else
12360 {
12361 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12362 virtual_dwo_name.c_str ());
12363
12364 dwo_file = (struct dwo_file *) *dwo_file_slot;
12365 }
12366
12367 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12368 dwo_unit->dwo_file = dwo_file;
12369 dwo_unit->signature = signature;
12370 dwo_unit->section =
12371 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12372 *dwo_unit->section = create_dwp_v2_or_v5_section
12373 (per_objfile,
12374 is_debug_types
12375 ? &dwp_file->sections.types
12376 : &dwp_file->sections.info,
12377 sections.info_or_types_offset,
12378 sections.info_or_types_size);
12379 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12380
12381 return dwo_unit;
12382 }
12383
12384 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12385 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12386 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12387 This is for DWP version 5 files. */
12388
12389 static struct dwo_unit *
12390 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12391 struct dwp_file *dwp_file,
12392 uint32_t unit_index,
12393 const char *comp_dir,
12394 ULONGEST signature, int is_debug_types)
12395 {
12396 const struct dwp_hash_table *dwp_htab
12397 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12398 bfd *dbfd = dwp_file->dbfd.get ();
12399 const char *kind = is_debug_types ? "TU" : "CU";
12400 struct dwo_file *dwo_file;
12401 struct dwo_unit *dwo_unit;
12402 struct virtual_v2_or_v5_dwo_sections sections {};
12403 void **dwo_file_slot;
12404
12405 gdb_assert (dwp_file->version == 5);
12406
12407 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12408 kind, pulongest (unit_index), hex_string (signature),
12409 dwp_file->name);
12410
12411 /* Fetch the section offsets of this DWO unit. */
12412
12413 /* memset (&sections, 0, sizeof (sections)); */
12414
12415 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12416 {
12417 uint32_t offset = read_4_bytes (dbfd,
12418 dwp_htab->section_pool.v5.offsets
12419 + (((unit_index - 1)
12420 * dwp_htab->nr_columns
12421 + i)
12422 * sizeof (uint32_t)));
12423 uint32_t size = read_4_bytes (dbfd,
12424 dwp_htab->section_pool.v5.sizes
12425 + (((unit_index - 1) * dwp_htab->nr_columns
12426 + i)
12427 * sizeof (uint32_t)));
12428
12429 switch (dwp_htab->section_pool.v5.section_ids[i])
12430 {
12431 case DW_SECT_ABBREV_V5:
12432 sections.abbrev_offset = offset;
12433 sections.abbrev_size = size;
12434 break;
12435 case DW_SECT_INFO_V5:
12436 sections.info_or_types_offset = offset;
12437 sections.info_or_types_size = size;
12438 break;
12439 case DW_SECT_LINE_V5:
12440 sections.line_offset = offset;
12441 sections.line_size = size;
12442 break;
12443 case DW_SECT_LOCLISTS_V5:
12444 sections.loclists_offset = offset;
12445 sections.loclists_size = size;
12446 break;
12447 case DW_SECT_MACRO_V5:
12448 sections.macro_offset = offset;
12449 sections.macro_size = size;
12450 break;
12451 case DW_SECT_RNGLISTS_V5:
12452 sections.rnglists_offset = offset;
12453 sections.rnglists_size = size;
12454 break;
12455 case DW_SECT_STR_OFFSETS_V5:
12456 sections.str_offsets_offset = offset;
12457 sections.str_offsets_size = size;
12458 break;
12459 case DW_SECT_RESERVED_V5:
12460 default:
12461 break;
12462 }
12463 }
12464
12465 /* It's easier for the rest of the code if we fake a struct dwo_file and
12466 have dwo_unit "live" in that. At least for now.
12467
12468 The DWP file can be made up of a random collection of CUs and TUs.
12469 However, for each CU + set of TUs that came from the same original DWO
12470 file, we can combine them back into a virtual DWO file to save space
12471 (fewer struct dwo_file objects to allocate). Remember that for really
12472 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12473
12474 std::string virtual_dwo_name =
12475 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12476 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12477 (long) (sections.line_size ? sections.line_offset : 0),
12478 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12479 (long) (sections.str_offsets_size
12480 ? sections.str_offsets_offset : 0),
12481 (long) (sections.macro_size ? sections.macro_offset : 0),
12482 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12483 /* Can we use an existing virtual DWO file? */
12484 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12485 virtual_dwo_name.c_str (),
12486 comp_dir);
12487 /* Create one if necessary. */
12488 if (*dwo_file_slot == NULL)
12489 {
12490 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12491 virtual_dwo_name.c_str ());
12492
12493 dwo_file = new struct dwo_file;
12494 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12495 dwo_file->comp_dir = comp_dir;
12496 dwo_file->sections.abbrev =
12497 create_dwp_v2_or_v5_section (per_objfile,
12498 &dwp_file->sections.abbrev,
12499 sections.abbrev_offset,
12500 sections.abbrev_size);
12501 dwo_file->sections.line =
12502 create_dwp_v2_or_v5_section (per_objfile,
12503 &dwp_file->sections.line,
12504 sections.line_offset, sections.line_size);
12505 dwo_file->sections.macro =
12506 create_dwp_v2_or_v5_section (per_objfile,
12507 &dwp_file->sections.macro,
12508 sections.macro_offset,
12509 sections.macro_size);
12510 dwo_file->sections.loclists =
12511 create_dwp_v2_or_v5_section (per_objfile,
12512 &dwp_file->sections.loclists,
12513 sections.loclists_offset,
12514 sections.loclists_size);
12515 dwo_file->sections.rnglists =
12516 create_dwp_v2_or_v5_section (per_objfile,
12517 &dwp_file->sections.rnglists,
12518 sections.rnglists_offset,
12519 sections.rnglists_size);
12520 dwo_file->sections.str_offsets =
12521 create_dwp_v2_or_v5_section (per_objfile,
12522 &dwp_file->sections.str_offsets,
12523 sections.str_offsets_offset,
12524 sections.str_offsets_size);
12525 /* The "str" section is global to the entire DWP file. */
12526 dwo_file->sections.str = dwp_file->sections.str;
12527 /* The info or types section is assigned below to dwo_unit,
12528 there's no need to record it in dwo_file.
12529 Also, we can't simply record type sections in dwo_file because
12530 we record a pointer into the vector in dwo_unit. As we collect more
12531 types we'll grow the vector and eventually have to reallocate space
12532 for it, invalidating all copies of pointers into the previous
12533 contents. */
12534 *dwo_file_slot = dwo_file;
12535 }
12536 else
12537 {
12538 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12539 virtual_dwo_name.c_str ());
12540
12541 dwo_file = (struct dwo_file *) *dwo_file_slot;
12542 }
12543
12544 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12545 dwo_unit->dwo_file = dwo_file;
12546 dwo_unit->signature = signature;
12547 dwo_unit->section
12548 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12549 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12550 &dwp_file->sections.info,
12551 sections.info_or_types_offset,
12552 sections.info_or_types_size);
12553 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12554
12555 return dwo_unit;
12556 }
12557
12558 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12559 Returns NULL if the signature isn't found. */
12560
12561 static struct dwo_unit *
12562 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12563 struct dwp_file *dwp_file, const char *comp_dir,
12564 ULONGEST signature, int is_debug_types)
12565 {
12566 const struct dwp_hash_table *dwp_htab =
12567 is_debug_types ? dwp_file->tus : dwp_file->cus;
12568 bfd *dbfd = dwp_file->dbfd.get ();
12569 uint32_t mask = dwp_htab->nr_slots - 1;
12570 uint32_t hash = signature & mask;
12571 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12572 unsigned int i;
12573 void **slot;
12574 struct dwo_unit find_dwo_cu;
12575
12576 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12577 find_dwo_cu.signature = signature;
12578 slot = htab_find_slot (is_debug_types
12579 ? dwp_file->loaded_tus.get ()
12580 : dwp_file->loaded_cus.get (),
12581 &find_dwo_cu, INSERT);
12582
12583 if (*slot != NULL)
12584 return (struct dwo_unit *) *slot;
12585
12586 /* Use a for loop so that we don't loop forever on bad debug info. */
12587 for (i = 0; i < dwp_htab->nr_slots; ++i)
12588 {
12589 ULONGEST signature_in_table;
12590
12591 signature_in_table =
12592 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12593 if (signature_in_table == signature)
12594 {
12595 uint32_t unit_index =
12596 read_4_bytes (dbfd,
12597 dwp_htab->unit_table + hash * sizeof (uint32_t));
12598
12599 if (dwp_file->version == 1)
12600 {
12601 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12602 unit_index, comp_dir,
12603 signature, is_debug_types);
12604 }
12605 else if (dwp_file->version == 2)
12606 {
12607 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12608 unit_index, comp_dir,
12609 signature, is_debug_types);
12610 }
12611 else /* version == 5 */
12612 {
12613 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12614 unit_index, comp_dir,
12615 signature, is_debug_types);
12616 }
12617 return (struct dwo_unit *) *slot;
12618 }
12619 if (signature_in_table == 0)
12620 return NULL;
12621 hash = (hash + hash2) & mask;
12622 }
12623
12624 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12625 " [in module %s]"),
12626 dwp_file->name);
12627 }
12628
12629 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12630 Open the file specified by FILE_NAME and hand it off to BFD for
12631 preliminary analysis. Return a newly initialized bfd *, which
12632 includes a canonicalized copy of FILE_NAME.
12633 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12634 SEARCH_CWD is true if the current directory is to be searched.
12635 It will be searched before debug-file-directory.
12636 If successful, the file is added to the bfd include table of the
12637 objfile's bfd (see gdb_bfd_record_inclusion).
12638 If unable to find/open the file, return NULL.
12639 NOTE: This function is derived from symfile_bfd_open. */
12640
12641 static gdb_bfd_ref_ptr
12642 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12643 const char *file_name, int is_dwp, int search_cwd)
12644 {
12645 int desc;
12646 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12647 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12648 to debug_file_directory. */
12649 const char *search_path;
12650 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12651
12652 gdb::unique_xmalloc_ptr<char> search_path_holder;
12653 if (search_cwd)
12654 {
12655 if (!debug_file_directory.empty ())
12656 {
12657 search_path_holder.reset (concat (".", dirname_separator_string,
12658 debug_file_directory.c_str (),
12659 (char *) NULL));
12660 search_path = search_path_holder.get ();
12661 }
12662 else
12663 search_path = ".";
12664 }
12665 else
12666 search_path = debug_file_directory.c_str ();
12667
12668 /* Add the path for the executable binary to the list of search paths. */
12669 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12670 search_path_holder.reset (concat (objfile_dir.c_str (),
12671 dirname_separator_string,
12672 search_path, nullptr));
12673 search_path = search_path_holder.get ();
12674
12675 openp_flags flags = OPF_RETURN_REALPATH;
12676 if (is_dwp)
12677 flags |= OPF_SEARCH_IN_PATH;
12678
12679 gdb::unique_xmalloc_ptr<char> absolute_name;
12680 desc = openp (search_path, flags, file_name,
12681 O_RDONLY | O_BINARY, &absolute_name);
12682 if (desc < 0)
12683 return NULL;
12684
12685 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12686 gnutarget, desc));
12687 if (sym_bfd == NULL)
12688 return NULL;
12689 bfd_set_cacheable (sym_bfd.get (), 1);
12690
12691 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12692 return NULL;
12693
12694 /* Success. Record the bfd as having been included by the objfile's bfd.
12695 This is important because things like demangled_names_hash lives in the
12696 objfile's per_bfd space and may have references to things like symbol
12697 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12698 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12699
12700 return sym_bfd;
12701 }
12702
12703 /* Try to open DWO file FILE_NAME.
12704 COMP_DIR is the DW_AT_comp_dir attribute.
12705 The result is the bfd handle of the file.
12706 If there is a problem finding or opening the file, return NULL.
12707 Upon success, the canonicalized path of the file is stored in the bfd,
12708 same as symfile_bfd_open. */
12709
12710 static gdb_bfd_ref_ptr
12711 open_dwo_file (dwarf2_per_objfile *per_objfile,
12712 const char *file_name, const char *comp_dir)
12713 {
12714 if (IS_ABSOLUTE_PATH (file_name))
12715 return try_open_dwop_file (per_objfile, file_name,
12716 0 /*is_dwp*/, 0 /*search_cwd*/);
12717
12718 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12719
12720 if (comp_dir != NULL)
12721 {
12722 gdb::unique_xmalloc_ptr<char> path_to_try
12723 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12724
12725 /* NOTE: If comp_dir is a relative path, this will also try the
12726 search path, which seems useful. */
12727 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12728 0 /*is_dwp*/,
12729 1 /*search_cwd*/));
12730 if (abfd != NULL)
12731 return abfd;
12732 }
12733
12734 /* That didn't work, try debug-file-directory, which, despite its name,
12735 is a list of paths. */
12736
12737 if (debug_file_directory.empty ())
12738 return NULL;
12739
12740 return try_open_dwop_file (per_objfile, file_name,
12741 0 /*is_dwp*/, 1 /*search_cwd*/);
12742 }
12743
12744 /* This function is mapped across the sections and remembers the offset and
12745 size of each of the DWO debugging sections we are interested in. */
12746
12747 static void
12748 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
12749 asection *sectp, dwo_sections *dwo_sections)
12750 {
12751 const struct dwop_section_names *names = &dwop_section_names;
12752
12753 struct dwarf2_section_info *dw_sect = nullptr;
12754
12755 if (names->abbrev_dwo.matches (sectp->name))
12756 dw_sect = &dwo_sections->abbrev;
12757 else if (names->info_dwo.matches (sectp->name))
12758 dw_sect = &dwo_sections->info;
12759 else if (names->line_dwo.matches (sectp->name))
12760 dw_sect = &dwo_sections->line;
12761 else if (names->loc_dwo.matches (sectp->name))
12762 dw_sect = &dwo_sections->loc;
12763 else if (names->loclists_dwo.matches (sectp->name))
12764 dw_sect = &dwo_sections->loclists;
12765 else if (names->macinfo_dwo.matches (sectp->name))
12766 dw_sect = &dwo_sections->macinfo;
12767 else if (names->macro_dwo.matches (sectp->name))
12768 dw_sect = &dwo_sections->macro;
12769 else if (names->rnglists_dwo.matches (sectp->name))
12770 dw_sect = &dwo_sections->rnglists;
12771 else if (names->str_dwo.matches (sectp->name))
12772 dw_sect = &dwo_sections->str;
12773 else if (names->str_offsets_dwo.matches (sectp->name))
12774 dw_sect = &dwo_sections->str_offsets;
12775 else if (names->types_dwo.matches (sectp->name))
12776 {
12777 struct dwarf2_section_info type_section;
12778
12779 memset (&type_section, 0, sizeof (type_section));
12780 dwo_sections->types.push_back (type_section);
12781 dw_sect = &dwo_sections->types.back ();
12782 }
12783
12784 if (dw_sect != nullptr)
12785 {
12786 dw_sect->s.section = sectp;
12787 dw_sect->size = bfd_section_size (sectp);
12788 dw_sect->read (objfile);
12789 }
12790 }
12791
12792 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12793 by PER_CU. This is for the non-DWP case.
12794 The result is NULL if DWO_NAME can't be found. */
12795
12796 static struct dwo_file *
12797 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12798 const char *comp_dir)
12799 {
12800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12801
12802 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12803 if (dbfd == NULL)
12804 {
12805 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12806
12807 return NULL;
12808 }
12809
12810 dwo_file_up dwo_file (new struct dwo_file);
12811 dwo_file->dwo_name = dwo_name;
12812 dwo_file->comp_dir = comp_dir;
12813 dwo_file->dbfd = std::move (dbfd);
12814
12815 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12816 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
12817 sec, &dwo_file->sections);
12818
12819 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12820 dwo_file->cus);
12821
12822 if (cu->per_cu->dwarf_version < 5)
12823 {
12824 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12825 dwo_file->sections.types, dwo_file->tus);
12826 }
12827 else
12828 {
12829 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12830 &dwo_file->sections.info, dwo_file->tus,
12831 rcuh_kind::COMPILE);
12832 }
12833
12834 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12835
12836 return dwo_file.release ();
12837 }
12838
12839 /* This function is mapped across the sections and remembers the offset and
12840 size of each of the DWP debugging sections common to version 1 and 2 that
12841 we are interested in. */
12842
12843 static void
12844 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
12845 asection *sectp, dwp_file *dwp_file)
12846 {
12847 const struct dwop_section_names *names = &dwop_section_names;
12848 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12849
12850 /* Record the ELF section number for later lookup: this is what the
12851 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12852 gdb_assert (elf_section_nr < dwp_file->num_sections);
12853 dwp_file->elf_sections[elf_section_nr] = sectp;
12854
12855 /* Look for specific sections that we need. */
12856 struct dwarf2_section_info *dw_sect = nullptr;
12857 if (names->str_dwo.matches (sectp->name))
12858 dw_sect = &dwp_file->sections.str;
12859 else if (names->cu_index.matches (sectp->name))
12860 dw_sect = &dwp_file->sections.cu_index;
12861 else if (names->tu_index.matches (sectp->name))
12862 dw_sect = &dwp_file->sections.tu_index;
12863
12864 if (dw_sect != nullptr)
12865 {
12866 dw_sect->s.section = sectp;
12867 dw_sect->size = bfd_section_size (sectp);
12868 dw_sect->read (objfile);
12869 }
12870 }
12871
12872 /* This function is mapped across the sections and remembers the offset and
12873 size of each of the DWP version 2 debugging sections that we are interested
12874 in. This is split into a separate function because we don't know if we
12875 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12876
12877 static void
12878 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
12879 asection *sectp, void *dwp_file_ptr)
12880 {
12881 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12882 const struct dwop_section_names *names = &dwop_section_names;
12883 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12884
12885 /* Record the ELF section number for later lookup: this is what the
12886 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12887 gdb_assert (elf_section_nr < dwp_file->num_sections);
12888 dwp_file->elf_sections[elf_section_nr] = sectp;
12889
12890 /* Look for specific sections that we need. */
12891 struct dwarf2_section_info *dw_sect = nullptr;
12892 if (names->abbrev_dwo.matches (sectp->name))
12893 dw_sect = &dwp_file->sections.abbrev;
12894 else if (names->info_dwo.matches (sectp->name))
12895 dw_sect = &dwp_file->sections.info;
12896 else if (names->line_dwo.matches (sectp->name))
12897 dw_sect = &dwp_file->sections.line;
12898 else if (names->loc_dwo.matches (sectp->name))
12899 dw_sect = &dwp_file->sections.loc;
12900 else if (names->macinfo_dwo.matches (sectp->name))
12901 dw_sect = &dwp_file->sections.macinfo;
12902 else if (names->macro_dwo.matches (sectp->name))
12903 dw_sect = &dwp_file->sections.macro;
12904 else if (names->str_offsets_dwo.matches (sectp->name))
12905 dw_sect = &dwp_file->sections.str_offsets;
12906 else if (names->types_dwo.matches (sectp->name))
12907 dw_sect = &dwp_file->sections.types;
12908
12909 if (dw_sect != nullptr)
12910 {
12911 dw_sect->s.section = sectp;
12912 dw_sect->size = bfd_section_size (sectp);
12913 dw_sect->read (objfile);
12914 }
12915 }
12916
12917 /* This function is mapped across the sections and remembers the offset and
12918 size of each of the DWP version 5 debugging sections that we are interested
12919 in. This is split into a separate function because we don't know if we
12920 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12921
12922 static void
12923 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
12924 asection *sectp, void *dwp_file_ptr)
12925 {
12926 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12927 const struct dwop_section_names *names = &dwop_section_names;
12928 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12929
12930 /* Record the ELF section number for later lookup: this is what the
12931 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12932 gdb_assert (elf_section_nr < dwp_file->num_sections);
12933 dwp_file->elf_sections[elf_section_nr] = sectp;
12934
12935 /* Look for specific sections that we need. */
12936 struct dwarf2_section_info *dw_sect = nullptr;
12937 if (names->abbrev_dwo.matches (sectp->name))
12938 dw_sect = &dwp_file->sections.abbrev;
12939 else if (names->info_dwo.matches (sectp->name))
12940 dw_sect = &dwp_file->sections.info;
12941 else if (names->line_dwo.matches (sectp->name))
12942 dw_sect = &dwp_file->sections.line;
12943 else if (names->loclists_dwo.matches (sectp->name))
12944 dw_sect = &dwp_file->sections.loclists;
12945 else if (names->macro_dwo.matches (sectp->name))
12946 dw_sect = &dwp_file->sections.macro;
12947 else if (names->rnglists_dwo.matches (sectp->name))
12948 dw_sect = &dwp_file->sections.rnglists;
12949 else if (names->str_offsets_dwo.matches (sectp->name))
12950 dw_sect = &dwp_file->sections.str_offsets;
12951
12952 if (dw_sect != nullptr)
12953 {
12954 dw_sect->s.section = sectp;
12955 dw_sect->size = bfd_section_size (sectp);
12956 dw_sect->read (objfile);
12957 }
12958 }
12959
12960 /* Hash function for dwp_file loaded CUs/TUs. */
12961
12962 static hashval_t
12963 hash_dwp_loaded_cutus (const void *item)
12964 {
12965 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12966
12967 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12968 return dwo_unit->signature;
12969 }
12970
12971 /* Equality function for dwp_file loaded CUs/TUs. */
12972
12973 static int
12974 eq_dwp_loaded_cutus (const void *a, const void *b)
12975 {
12976 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12977 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12978
12979 return dua->signature == dub->signature;
12980 }
12981
12982 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12983
12984 static htab_up
12985 allocate_dwp_loaded_cutus_table ()
12986 {
12987 return htab_up (htab_create_alloc (3,
12988 hash_dwp_loaded_cutus,
12989 eq_dwp_loaded_cutus,
12990 NULL, xcalloc, xfree));
12991 }
12992
12993 /* Try to open DWP file FILE_NAME.
12994 The result is the bfd handle of the file.
12995 If there is a problem finding or opening the file, return NULL.
12996 Upon success, the canonicalized path of the file is stored in the bfd,
12997 same as symfile_bfd_open. */
12998
12999 static gdb_bfd_ref_ptr
13000 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13001 {
13002 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13003 1 /*is_dwp*/,
13004 1 /*search_cwd*/));
13005 if (abfd != NULL)
13006 return abfd;
13007
13008 /* Work around upstream bug 15652.
13009 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13010 [Whether that's a "bug" is debatable, but it is getting in our way.]
13011 We have no real idea where the dwp file is, because gdb's realpath-ing
13012 of the executable's path may have discarded the needed info.
13013 [IWBN if the dwp file name was recorded in the executable, akin to
13014 .gnu_debuglink, but that doesn't exist yet.]
13015 Strip the directory from FILE_NAME and search again. */
13016 if (!debug_file_directory.empty ())
13017 {
13018 /* Don't implicitly search the current directory here.
13019 If the user wants to search "." to handle this case,
13020 it must be added to debug-file-directory. */
13021 return try_open_dwop_file (per_objfile, lbasename (file_name),
13022 1 /*is_dwp*/,
13023 0 /*search_cwd*/);
13024 }
13025
13026 return NULL;
13027 }
13028
13029 /* Initialize the use of the DWP file for the current objfile.
13030 By convention the name of the DWP file is ${objfile}.dwp.
13031 The result is NULL if it can't be found. */
13032
13033 static std::unique_ptr<struct dwp_file>
13034 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13035 {
13036 struct objfile *objfile = per_objfile->objfile;
13037
13038 /* Try to find first .dwp for the binary file before any symbolic links
13039 resolving. */
13040
13041 /* If the objfile is a debug file, find the name of the real binary
13042 file and get the name of dwp file from there. */
13043 std::string dwp_name;
13044 if (objfile->separate_debug_objfile_backlink != NULL)
13045 {
13046 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13047 const char *backlink_basename = lbasename (backlink->original_name);
13048
13049 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13050 }
13051 else
13052 dwp_name = objfile->original_name;
13053
13054 dwp_name += ".dwp";
13055
13056 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13057 if (dbfd == NULL
13058 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13059 {
13060 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13061 dwp_name = objfile_name (objfile);
13062 dwp_name += ".dwp";
13063 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13064 }
13065
13066 if (dbfd == NULL)
13067 {
13068 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13069
13070 return std::unique_ptr<dwp_file> ();
13071 }
13072
13073 const char *name = bfd_get_filename (dbfd.get ());
13074 std::unique_ptr<struct dwp_file> dwp_file
13075 (new struct dwp_file (name, std::move (dbfd)));
13076
13077 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13078 dwp_file->elf_sections =
13079 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13080 dwp_file->num_sections, asection *);
13081
13082 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13083 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
13084 dwp_file.get ());
13085
13086 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13087
13088 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13089
13090 /* The DWP file version is stored in the hash table. Oh well. */
13091 if (dwp_file->cus && dwp_file->tus
13092 && dwp_file->cus->version != dwp_file->tus->version)
13093 {
13094 /* Technically speaking, we should try to limp along, but this is
13095 pretty bizarre. We use pulongest here because that's the established
13096 portability solution (e.g, we cannot use %u for uint32_t). */
13097 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13098 " TU version %s [in DWP file %s]"),
13099 pulongest (dwp_file->cus->version),
13100 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13101 }
13102
13103 if (dwp_file->cus)
13104 dwp_file->version = dwp_file->cus->version;
13105 else if (dwp_file->tus)
13106 dwp_file->version = dwp_file->tus->version;
13107 else
13108 dwp_file->version = 2;
13109
13110 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13111 {
13112 if (dwp_file->version == 2)
13113 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
13114 dwp_file.get ());
13115 else
13116 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
13117 dwp_file.get ());
13118 }
13119
13120 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13121 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13122
13123 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13124 dwarf_read_debug_printf (" %s CUs, %s TUs",
13125 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13126 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13127
13128 return dwp_file;
13129 }
13130
13131 /* Wrapper around open_and_init_dwp_file, only open it once. */
13132
13133 static struct dwp_file *
13134 get_dwp_file (dwarf2_per_objfile *per_objfile)
13135 {
13136 if (!per_objfile->per_bfd->dwp_checked)
13137 {
13138 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13139 per_objfile->per_bfd->dwp_checked = 1;
13140 }
13141 return per_objfile->per_bfd->dwp_file.get ();
13142 }
13143
13144 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13145 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13146 or in the DWP file for the objfile, referenced by THIS_UNIT.
13147 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13148 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13149
13150 This is called, for example, when wanting to read a variable with a
13151 complex location. Therefore we don't want to do file i/o for every call.
13152 Therefore we don't want to look for a DWO file on every call.
13153 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13154 then we check if we've already seen DWO_NAME, and only THEN do we check
13155 for a DWO file.
13156
13157 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13158 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13159
13160 static struct dwo_unit *
13161 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13162 ULONGEST signature, int is_debug_types)
13163 {
13164 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13165 struct objfile *objfile = per_objfile->objfile;
13166 const char *kind = is_debug_types ? "TU" : "CU";
13167 void **dwo_file_slot;
13168 struct dwo_file *dwo_file;
13169 struct dwp_file *dwp_file;
13170
13171 /* First see if there's a DWP file.
13172 If we have a DWP file but didn't find the DWO inside it, don't
13173 look for the original DWO file. It makes gdb behave differently
13174 depending on whether one is debugging in the build tree. */
13175
13176 dwp_file = get_dwp_file (per_objfile);
13177 if (dwp_file != NULL)
13178 {
13179 const struct dwp_hash_table *dwp_htab =
13180 is_debug_types ? dwp_file->tus : dwp_file->cus;
13181
13182 if (dwp_htab != NULL)
13183 {
13184 struct dwo_unit *dwo_cutu =
13185 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13186 is_debug_types);
13187
13188 if (dwo_cutu != NULL)
13189 {
13190 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13191 kind, hex_string (signature),
13192 host_address_to_string (dwo_cutu));
13193
13194 return dwo_cutu;
13195 }
13196 }
13197 }
13198 else
13199 {
13200 /* No DWP file, look for the DWO file. */
13201
13202 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13203 if (*dwo_file_slot == NULL)
13204 {
13205 /* Read in the file and build a table of the CUs/TUs it contains. */
13206 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13207 }
13208 /* NOTE: This will be NULL if unable to open the file. */
13209 dwo_file = (struct dwo_file *) *dwo_file_slot;
13210
13211 if (dwo_file != NULL)
13212 {
13213 struct dwo_unit *dwo_cutu = NULL;
13214
13215 if (is_debug_types && dwo_file->tus)
13216 {
13217 struct dwo_unit find_dwo_cutu;
13218
13219 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13220 find_dwo_cutu.signature = signature;
13221 dwo_cutu
13222 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13223 &find_dwo_cutu);
13224 }
13225 else if (!is_debug_types && dwo_file->cus)
13226 {
13227 struct dwo_unit find_dwo_cutu;
13228
13229 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13230 find_dwo_cutu.signature = signature;
13231 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13232 &find_dwo_cutu);
13233 }
13234
13235 if (dwo_cutu != NULL)
13236 {
13237 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13238 kind, dwo_name, hex_string (signature),
13239 host_address_to_string (dwo_cutu));
13240
13241 return dwo_cutu;
13242 }
13243 }
13244 }
13245
13246 /* We didn't find it. This could mean a dwo_id mismatch, or
13247 someone deleted the DWO/DWP file, or the search path isn't set up
13248 correctly to find the file. */
13249
13250 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13251 kind, dwo_name, hex_string (signature));
13252
13253 /* This is a warning and not a complaint because it can be caused by
13254 pilot error (e.g., user accidentally deleting the DWO). */
13255 {
13256 /* Print the name of the DWP file if we looked there, helps the user
13257 better diagnose the problem. */
13258 std::string dwp_text;
13259
13260 if (dwp_file != NULL)
13261 dwp_text = string_printf (" [in DWP file %s]",
13262 lbasename (dwp_file->name));
13263
13264 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13265 " [in module %s]"),
13266 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13267 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13268 }
13269 return NULL;
13270 }
13271
13272 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13273 See lookup_dwo_cutu_unit for details. */
13274
13275 static struct dwo_unit *
13276 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13277 ULONGEST signature)
13278 {
13279 gdb_assert (!cu->per_cu->is_debug_types);
13280
13281 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13282 }
13283
13284 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13285 See lookup_dwo_cutu_unit for details. */
13286
13287 static struct dwo_unit *
13288 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13289 {
13290 gdb_assert (cu->per_cu->is_debug_types);
13291
13292 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13293
13294 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13295 }
13296
13297 /* Traversal function for queue_and_load_all_dwo_tus. */
13298
13299 static int
13300 queue_and_load_dwo_tu (void **slot, void *info)
13301 {
13302 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13303 dwarf2_cu *cu = (dwarf2_cu *) info;
13304 ULONGEST signature = dwo_unit->signature;
13305 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13306
13307 if (sig_type != NULL)
13308 {
13309 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13310 a real dependency of PER_CU on SIG_TYPE. That is detected later
13311 while processing PER_CU. */
13312 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
13313 cu->per_cu->lang))
13314 load_full_type_unit (sig_type, cu->per_objfile);
13315 cu->per_cu->imported_symtabs_push (sig_type);
13316 }
13317
13318 return 1;
13319 }
13320
13321 /* Queue all TUs contained in the DWO of CU to be read in.
13322 The DWO may have the only definition of the type, though it may not be
13323 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13324 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13325
13326 static void
13327 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13328 {
13329 struct dwo_unit *dwo_unit;
13330 struct dwo_file *dwo_file;
13331
13332 gdb_assert (cu != nullptr);
13333 gdb_assert (!cu->per_cu->is_debug_types);
13334 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13335
13336 dwo_unit = cu->dwo_unit;
13337 gdb_assert (dwo_unit != NULL);
13338
13339 dwo_file = dwo_unit->dwo_file;
13340 if (dwo_file->tus != NULL)
13341 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13342 }
13343
13344 /* Read in various DIEs. */
13345
13346 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13347 Inherit only the children of the DW_AT_abstract_origin DIE not being
13348 already referenced by DW_AT_abstract_origin from the children of the
13349 current DIE. */
13350
13351 static void
13352 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13353 {
13354 struct die_info *child_die;
13355 sect_offset *offsetp;
13356 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13357 struct die_info *origin_die;
13358 /* Iterator of the ORIGIN_DIE children. */
13359 struct die_info *origin_child_die;
13360 struct attribute *attr;
13361 struct dwarf2_cu *origin_cu;
13362 struct pending **origin_previous_list_in_scope;
13363
13364 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13365 if (!attr)
13366 return;
13367
13368 /* Note that following die references may follow to a die in a
13369 different cu. */
13370
13371 origin_cu = cu;
13372 origin_die = follow_die_ref (die, attr, &origin_cu);
13373
13374 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13375 symbols in. */
13376 origin_previous_list_in_scope = origin_cu->list_in_scope;
13377 origin_cu->list_in_scope = cu->list_in_scope;
13378
13379 if (die->tag != origin_die->tag
13380 && !(die->tag == DW_TAG_inlined_subroutine
13381 && origin_die->tag == DW_TAG_subprogram))
13382 complaint (_("DIE %s and its abstract origin %s have different tags"),
13383 sect_offset_str (die->sect_off),
13384 sect_offset_str (origin_die->sect_off));
13385
13386 /* Find if the concrete and abstract trees are structurally the
13387 same. This is a shallow traversal and it is not bullet-proof;
13388 the compiler can trick the debugger into believing that the trees
13389 are isomorphic, whereas they actually are not. However, the
13390 likelyhood of this happening is pretty low, and a full-fledged
13391 check would be an overkill. */
13392 bool are_isomorphic = true;
13393 die_info *concrete_child = die->child;
13394 die_info *abstract_child = origin_die->child;
13395 while (concrete_child != nullptr || abstract_child != nullptr)
13396 {
13397 if (concrete_child == nullptr
13398 || abstract_child == nullptr
13399 || concrete_child->tag != abstract_child->tag)
13400 {
13401 are_isomorphic = false;
13402 break;
13403 }
13404
13405 concrete_child = concrete_child->sibling;
13406 abstract_child = abstract_child->sibling;
13407 }
13408
13409 /* Walk the origin's children in parallel to the concrete children.
13410 This helps match an origin child in case the debug info misses
13411 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13412 origin tree may not have the same tree structure as the concrete
13413 DIE, though. */
13414 die_info *corresponding_abstract_child
13415 = are_isomorphic ? origin_die->child : nullptr;
13416
13417 std::vector<sect_offset> offsets;
13418
13419 for (child_die = die->child;
13420 child_die && child_die->tag;
13421 child_die = child_die->sibling)
13422 {
13423 struct die_info *child_origin_die;
13424 struct dwarf2_cu *child_origin_cu;
13425
13426 /* We are trying to process concrete instance entries:
13427 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13428 it's not relevant to our analysis here. i.e. detecting DIEs that are
13429 present in the abstract instance but not referenced in the concrete
13430 one. */
13431 if (child_die->tag == DW_TAG_call_site
13432 || child_die->tag == DW_TAG_GNU_call_site)
13433 {
13434 if (are_isomorphic)
13435 corresponding_abstract_child
13436 = corresponding_abstract_child->sibling;
13437 continue;
13438 }
13439
13440 /* For each CHILD_DIE, find the corresponding child of
13441 ORIGIN_DIE. If there is more than one layer of
13442 DW_AT_abstract_origin, follow them all; there shouldn't be,
13443 but GCC versions at least through 4.4 generate this (GCC PR
13444 40573). */
13445 child_origin_die = child_die;
13446 child_origin_cu = cu;
13447 while (1)
13448 {
13449 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13450 child_origin_cu);
13451 if (attr == NULL)
13452 break;
13453 child_origin_die = follow_die_ref (child_origin_die, attr,
13454 &child_origin_cu);
13455 }
13456
13457 /* If missing DW_AT_abstract_origin, try the corresponding child
13458 of the origin. Clang emits such lexical scopes. */
13459 if (child_origin_die == child_die
13460 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13461 && are_isomorphic
13462 && child_die->tag == DW_TAG_lexical_block)
13463 child_origin_die = corresponding_abstract_child;
13464
13465 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13466 counterpart may exist. */
13467 if (child_origin_die != child_die)
13468 {
13469 if (child_die->tag != child_origin_die->tag
13470 && !(child_die->tag == DW_TAG_inlined_subroutine
13471 && child_origin_die->tag == DW_TAG_subprogram))
13472 complaint (_("Child DIE %s and its abstract origin %s have "
13473 "different tags"),
13474 sect_offset_str (child_die->sect_off),
13475 sect_offset_str (child_origin_die->sect_off));
13476 if (child_origin_die->parent != origin_die)
13477 complaint (_("Child DIE %s and its abstract origin %s have "
13478 "different parents"),
13479 sect_offset_str (child_die->sect_off),
13480 sect_offset_str (child_origin_die->sect_off));
13481 else
13482 offsets.push_back (child_origin_die->sect_off);
13483 }
13484
13485 if (are_isomorphic)
13486 corresponding_abstract_child = corresponding_abstract_child->sibling;
13487 }
13488 std::sort (offsets.begin (), offsets.end ());
13489 sect_offset *offsets_end = offsets.data () + offsets.size ();
13490 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13491 if (offsetp[-1] == *offsetp)
13492 complaint (_("Multiple children of DIE %s refer "
13493 "to DIE %s as their abstract origin"),
13494 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13495
13496 offsetp = offsets.data ();
13497 origin_child_die = origin_die->child;
13498 while (origin_child_die && origin_child_die->tag)
13499 {
13500 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13501 while (offsetp < offsets_end
13502 && *offsetp < origin_child_die->sect_off)
13503 offsetp++;
13504 if (offsetp >= offsets_end
13505 || *offsetp > origin_child_die->sect_off)
13506 {
13507 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13508 Check whether we're already processing ORIGIN_CHILD_DIE.
13509 This can happen with mutually referenced abstract_origins.
13510 PR 16581. */
13511 if (!origin_child_die->in_process)
13512 process_die (origin_child_die, origin_cu);
13513 }
13514 origin_child_die = origin_child_die->sibling;
13515 }
13516 origin_cu->list_in_scope = origin_previous_list_in_scope;
13517
13518 if (cu != origin_cu)
13519 compute_delayed_physnames (origin_cu);
13520 }
13521
13522 static void
13523 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13524 {
13525 struct objfile *objfile = cu->per_objfile->objfile;
13526 struct gdbarch *gdbarch = objfile->arch ();
13527 struct context_stack *newobj;
13528 CORE_ADDR lowpc;
13529 CORE_ADDR highpc;
13530 struct die_info *child_die;
13531 struct attribute *attr, *call_line, *call_file;
13532 const char *name;
13533 CORE_ADDR baseaddr;
13534 struct block *block;
13535 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13536 std::vector<struct symbol *> template_args;
13537 struct template_symbol *templ_func = NULL;
13538
13539 if (inlined_func)
13540 {
13541 /* If we do not have call site information, we can't show the
13542 caller of this inlined function. That's too confusing, so
13543 only use the scope for local variables. */
13544 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13545 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13546 if (call_line == NULL || call_file == NULL)
13547 {
13548 read_lexical_block_scope (die, cu);
13549 return;
13550 }
13551 }
13552
13553 baseaddr = objfile->text_section_offset ();
13554
13555 name = dwarf2_name (die, cu);
13556
13557 /* Ignore functions with missing or empty names. These are actually
13558 illegal according to the DWARF standard. */
13559 if (name == NULL)
13560 {
13561 complaint (_("missing name for subprogram DIE at %s"),
13562 sect_offset_str (die->sect_off));
13563 return;
13564 }
13565
13566 /* Ignore functions with missing or invalid low and high pc attributes. */
13567 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
13568 <= PC_BOUNDS_INVALID)
13569 {
13570 attr = dwarf2_attr (die, DW_AT_external, cu);
13571 if (attr == nullptr || !attr->as_boolean ())
13572 complaint (_("cannot get low and high bounds "
13573 "for subprogram DIE at %s"),
13574 sect_offset_str (die->sect_off));
13575 return;
13576 }
13577
13578 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13579 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13580
13581 /* If we have any template arguments, then we must allocate a
13582 different sort of symbol. */
13583 for (child_die = die->child; child_die; child_die = child_die->sibling)
13584 {
13585 if (child_die->tag == DW_TAG_template_type_param
13586 || child_die->tag == DW_TAG_template_value_param)
13587 {
13588 templ_func = new (&objfile->objfile_obstack) template_symbol;
13589 templ_func->subclass = SYMBOL_TEMPLATE;
13590 break;
13591 }
13592 }
13593
13594 gdb_assert (cu->get_builder () != nullptr);
13595 newobj = cu->get_builder ()->push_context (0, lowpc);
13596 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13597 (struct symbol *) templ_func);
13598
13599 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13600 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13601 cu->per_cu->lang);
13602
13603 /* If there is a location expression for DW_AT_frame_base, record
13604 it. */
13605 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13606 if (attr != nullptr)
13607 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13608
13609 /* If there is a location for the static link, record it. */
13610 newobj->static_link = NULL;
13611 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13612 if (attr != nullptr)
13613 {
13614 newobj->static_link
13615 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13616 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13617 cu->addr_type ());
13618 }
13619
13620 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13621
13622 if (die->child != NULL)
13623 {
13624 child_die = die->child;
13625 while (child_die && child_die->tag)
13626 {
13627 if (child_die->tag == DW_TAG_template_type_param
13628 || child_die->tag == DW_TAG_template_value_param)
13629 {
13630 struct symbol *arg = new_symbol (child_die, NULL, cu);
13631
13632 if (arg != NULL)
13633 template_args.push_back (arg);
13634 }
13635 else
13636 process_die (child_die, cu);
13637 child_die = child_die->sibling;
13638 }
13639 }
13640
13641 inherit_abstract_dies (die, cu);
13642
13643 /* If we have a DW_AT_specification, we might need to import using
13644 directives from the context of the specification DIE. See the
13645 comment in determine_prefix. */
13646 if (cu->per_cu->lang == language_cplus
13647 && dwarf2_attr (die, DW_AT_specification, cu))
13648 {
13649 struct dwarf2_cu *spec_cu = cu;
13650 struct die_info *spec_die = die_specification (die, &spec_cu);
13651
13652 while (spec_die)
13653 {
13654 child_die = spec_die->child;
13655 while (child_die && child_die->tag)
13656 {
13657 if (child_die->tag == DW_TAG_imported_module)
13658 process_die (child_die, spec_cu);
13659 child_die = child_die->sibling;
13660 }
13661
13662 /* In some cases, GCC generates specification DIEs that
13663 themselves contain DW_AT_specification attributes. */
13664 spec_die = die_specification (spec_die, &spec_cu);
13665 }
13666 }
13667
13668 struct context_stack cstk = cu->get_builder ()->pop_context ();
13669 /* Make a block for the local symbols within. */
13670 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13671 cstk.static_link, lowpc, highpc);
13672
13673 /* For C++, set the block's scope. */
13674 if ((cu->per_cu->lang == language_cplus
13675 || cu->per_cu->lang == language_fortran
13676 || cu->per_cu->lang == language_d
13677 || cu->per_cu->lang == language_rust)
13678 && cu->processing_has_namespace_info)
13679 block_set_scope (block, determine_prefix (die, cu),
13680 &objfile->objfile_obstack);
13681
13682 /* If we have address ranges, record them. */
13683 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13684
13685 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13686
13687 /* Attach template arguments to function. */
13688 if (!template_args.empty ())
13689 {
13690 gdb_assert (templ_func != NULL);
13691
13692 templ_func->n_template_arguments = template_args.size ();
13693 templ_func->template_arguments
13694 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13695 templ_func->n_template_arguments);
13696 memcpy (templ_func->template_arguments,
13697 template_args.data (),
13698 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13699
13700 /* Make sure that the symtab is set on the new symbols. Even
13701 though they don't appear in this symtab directly, other parts
13702 of gdb assume that symbols do, and this is reasonably
13703 true. */
13704 for (symbol *sym : template_args)
13705 symbol_set_symtab (sym, symbol_symtab (templ_func));
13706 }
13707
13708 /* In C++, we can have functions nested inside functions (e.g., when
13709 a function declares a class that has methods). This means that
13710 when we finish processing a function scope, we may need to go
13711 back to building a containing block's symbol lists. */
13712 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13713 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13714
13715 /* If we've finished processing a top-level function, subsequent
13716 symbols go in the file symbol list. */
13717 if (cu->get_builder ()->outermost_context_p ())
13718 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13719 }
13720
13721 /* Process all the DIES contained within a lexical block scope. Start
13722 a new scope, process the dies, and then close the scope. */
13723
13724 static void
13725 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13726 {
13727 struct objfile *objfile = cu->per_objfile->objfile;
13728 struct gdbarch *gdbarch = objfile->arch ();
13729 CORE_ADDR lowpc, highpc;
13730 struct die_info *child_die;
13731 CORE_ADDR baseaddr;
13732
13733 baseaddr = objfile->text_section_offset ();
13734
13735 /* Ignore blocks with missing or invalid low and high pc attributes. */
13736 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13737 as multiple lexical blocks? Handling children in a sane way would
13738 be nasty. Might be easier to properly extend generic blocks to
13739 describe ranges. */
13740 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
13741 {
13742 case PC_BOUNDS_NOT_PRESENT:
13743 /* DW_TAG_lexical_block has no attributes, process its children as if
13744 there was no wrapping by that DW_TAG_lexical_block.
13745 GCC does no longer produces such DWARF since GCC r224161. */
13746 for (child_die = die->child;
13747 child_die != NULL && child_die->tag;
13748 child_die = child_die->sibling)
13749 {
13750 /* We might already be processing this DIE. This can happen
13751 in an unusual circumstance -- where a subroutine A
13752 appears lexically in another subroutine B, but A actually
13753 inlines B. The recursion is broken here, rather than in
13754 inherit_abstract_dies, because it seems better to simply
13755 drop concrete children here. */
13756 if (!child_die->in_process)
13757 process_die (child_die, cu);
13758 }
13759 return;
13760 case PC_BOUNDS_INVALID:
13761 return;
13762 }
13763 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13764 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13765
13766 cu->get_builder ()->push_context (0, lowpc);
13767 if (die->child != NULL)
13768 {
13769 child_die = die->child;
13770 while (child_die && child_die->tag)
13771 {
13772 process_die (child_die, cu);
13773 child_die = child_die->sibling;
13774 }
13775 }
13776 inherit_abstract_dies (die, cu);
13777 struct context_stack cstk = cu->get_builder ()->pop_context ();
13778
13779 if (*cu->get_builder ()->get_local_symbols () != NULL
13780 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13781 {
13782 struct block *block
13783 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13784 cstk.start_addr, highpc);
13785
13786 /* Note that recording ranges after traversing children, as we
13787 do here, means that recording a parent's ranges entails
13788 walking across all its children's ranges as they appear in
13789 the address map, which is quadratic behavior.
13790
13791 It would be nicer to record the parent's ranges before
13792 traversing its children, simply overriding whatever you find
13793 there. But since we don't even decide whether to create a
13794 block until after we've traversed its children, that's hard
13795 to do. */
13796 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13797 }
13798 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13799 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13800 }
13801
13802 static void dwarf2_ranges_read_low_addrs (unsigned offset,
13803 struct dwarf2_cu *cu,
13804 dwarf_tag tag,
13805 std::vector<CORE_ADDR> &result);
13806
13807 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13808
13809 static void
13810 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13811 {
13812 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13813 struct objfile *objfile = per_objfile->objfile;
13814 struct gdbarch *gdbarch = objfile->arch ();
13815 CORE_ADDR pc, baseaddr;
13816 struct attribute *attr;
13817 void **slot;
13818 int nparams;
13819 struct die_info *child_die;
13820
13821 baseaddr = objfile->text_section_offset ();
13822
13823 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13824 if (attr == NULL)
13825 {
13826 /* This was a pre-DWARF-5 GNU extension alias
13827 for DW_AT_call_return_pc. */
13828 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13829 }
13830 if (!attr)
13831 {
13832 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13833 "DIE %s [in module %s]"),
13834 sect_offset_str (die->sect_off), objfile_name (objfile));
13835 return;
13836 }
13837 pc = attr->as_address () + baseaddr;
13838 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13839 pc -= baseaddr;
13840
13841 if (cu->call_site_htab == NULL)
13842 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13843 call_site::eq, NULL,
13844 &objfile->objfile_obstack,
13845 hashtab_obstack_allocate, NULL);
13846 struct call_site call_site_local (pc, nullptr, nullptr);
13847 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13848 if (*slot != NULL)
13849 {
13850 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13851 "DIE %s [in module %s]"),
13852 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13853 objfile_name (objfile));
13854 return;
13855 }
13856
13857 /* Count parameters at the caller. */
13858
13859 nparams = 0;
13860 for (child_die = die->child; child_die && child_die->tag;
13861 child_die = child_die->sibling)
13862 {
13863 if (child_die->tag != DW_TAG_call_site_parameter
13864 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13865 {
13866 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13867 "DW_TAG_call_site child DIE %s [in module %s]"),
13868 child_die->tag, sect_offset_str (child_die->sect_off),
13869 objfile_name (objfile));
13870 continue;
13871 }
13872
13873 nparams++;
13874 }
13875
13876 struct call_site *call_site
13877 = new (XOBNEWVAR (&objfile->objfile_obstack,
13878 struct call_site,
13879 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13880 struct call_site (pc, cu->per_cu, per_objfile);
13881 *slot = call_site;
13882
13883 /* We never call the destructor of call_site, so we must ensure it is
13884 trivially destructible. */
13885 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13886
13887 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13888 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13889 {
13890 struct die_info *func_die;
13891
13892 /* Skip also over DW_TAG_inlined_subroutine. */
13893 for (func_die = die->parent;
13894 func_die && func_die->tag != DW_TAG_subprogram
13895 && func_die->tag != DW_TAG_subroutine_type;
13896 func_die = func_die->parent);
13897
13898 /* DW_AT_call_all_calls is a superset
13899 of DW_AT_call_all_tail_calls. */
13900 if (func_die
13901 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13902 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13903 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13904 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13905 {
13906 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13907 not complete. But keep CALL_SITE for look ups via call_site_htab,
13908 both the initial caller containing the real return address PC and
13909 the final callee containing the current PC of a chain of tail
13910 calls do not need to have the tail call list complete. But any
13911 function candidate for a virtual tail call frame searched via
13912 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13913 determined unambiguously. */
13914 }
13915 else
13916 {
13917 struct type *func_type = NULL;
13918
13919 if (func_die)
13920 func_type = get_die_type (func_die, cu);
13921 if (func_type != NULL)
13922 {
13923 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13924
13925 /* Enlist this call site to the function. */
13926 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13927 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13928 }
13929 else
13930 complaint (_("Cannot find function owning DW_TAG_call_site "
13931 "DIE %s [in module %s]"),
13932 sect_offset_str (die->sect_off), objfile_name (objfile));
13933 }
13934 }
13935
13936 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13937 if (attr == NULL)
13938 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13939 if (attr == NULL)
13940 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13941 if (attr == NULL)
13942 {
13943 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13944 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13945 }
13946
13947 call_site->target.set_loc_dwarf_block (nullptr);
13948 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13949 /* Keep NULL DWARF_BLOCK. */;
13950 else if (attr->form_is_block ())
13951 {
13952 struct dwarf2_locexpr_baton *dlbaton;
13953 struct dwarf_block *block = attr->as_block ();
13954
13955 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13956 dlbaton->data = block->data;
13957 dlbaton->size = block->size;
13958 dlbaton->per_objfile = per_objfile;
13959 dlbaton->per_cu = cu->per_cu;
13960
13961 call_site->target.set_loc_dwarf_block (dlbaton);
13962 }
13963 else if (attr->form_is_ref ())
13964 {
13965 struct dwarf2_cu *target_cu = cu;
13966 struct die_info *target_die;
13967
13968 target_die = follow_die_ref (die, attr, &target_cu);
13969 gdb_assert (target_cu->per_objfile->objfile == objfile);
13970
13971 struct attribute *ranges_attr
13972 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
13973
13974 if (die_is_declaration (target_die, target_cu))
13975 {
13976 const char *target_physname;
13977
13978 /* Prefer the mangled name; otherwise compute the demangled one. */
13979 target_physname = dw2_linkage_name (target_die, target_cu);
13980 if (target_physname == NULL)
13981 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13982 if (target_physname == NULL)
13983 complaint (_("DW_AT_call_target target DIE has invalid "
13984 "physname, for referencing DIE %s [in module %s]"),
13985 sect_offset_str (die->sect_off), objfile_name (objfile));
13986 else
13987 call_site->target.set_loc_physname (target_physname);
13988 }
13989 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
13990 {
13991 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
13992 + target_cu->gnu_ranges_base);
13993 std::vector<CORE_ADDR> addresses;
13994 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
13995 target_die->tag, addresses);
13996 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
13997 addresses.size ());
13998 std::copy (addresses.begin (), addresses.end (), saved);
13999 call_site->target.set_loc_array (addresses.size (), saved);
14000 }
14001 else
14002 {
14003 CORE_ADDR lowpc;
14004
14005 /* DW_AT_entry_pc should be preferred. */
14006 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
14007 nullptr, nullptr)
14008 <= PC_BOUNDS_INVALID)
14009 complaint (_("DW_AT_call_target target DIE has invalid "
14010 "low pc, for referencing DIE %s [in module %s]"),
14011 sect_offset_str (die->sect_off), objfile_name (objfile));
14012 else
14013 {
14014 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
14015 - baseaddr);
14016 call_site->target.set_loc_physaddr (lowpc);
14017 }
14018 }
14019 }
14020 else
14021 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14022 "block nor reference, for DIE %s [in module %s]"),
14023 sect_offset_str (die->sect_off), objfile_name (objfile));
14024
14025 for (child_die = die->child;
14026 child_die && child_die->tag;
14027 child_die = child_die->sibling)
14028 {
14029 struct call_site_parameter *parameter;
14030 struct attribute *loc, *origin;
14031
14032 if (child_die->tag != DW_TAG_call_site_parameter
14033 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14034 {
14035 /* Already printed the complaint above. */
14036 continue;
14037 }
14038
14039 gdb_assert (call_site->parameter_count < nparams);
14040 parameter = &call_site->parameter[call_site->parameter_count];
14041
14042 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14043 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14044 register is contained in DW_AT_call_value. */
14045
14046 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14047 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14048 if (origin == NULL)
14049 {
14050 /* This was a pre-DWARF-5 GNU extension alias
14051 for DW_AT_call_parameter. */
14052 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14053 }
14054 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14055 {
14056 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14057
14058 sect_offset sect_off = origin->get_ref_die_offset ();
14059 if (!cu->header.offset_in_cu_p (sect_off))
14060 {
14061 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14062 binding can be done only inside one CU. Such referenced DIE
14063 therefore cannot be even moved to DW_TAG_partial_unit. */
14064 complaint (_("DW_AT_call_parameter offset is not in CU for "
14065 "DW_TAG_call_site child DIE %s [in module %s]"),
14066 sect_offset_str (child_die->sect_off),
14067 objfile_name (objfile));
14068 continue;
14069 }
14070 parameter->u.param_cu_off
14071 = (cu_offset) (sect_off - cu->header.sect_off);
14072 }
14073 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14074 {
14075 complaint (_("No DW_FORM_block* DW_AT_location for "
14076 "DW_TAG_call_site child DIE %s [in module %s]"),
14077 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14078 continue;
14079 }
14080 else
14081 {
14082 struct dwarf_block *block = loc->as_block ();
14083
14084 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14085 (block->data, &block->data[block->size]);
14086 if (parameter->u.dwarf_reg != -1)
14087 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14088 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14089 &block->data[block->size],
14090 &parameter->u.fb_offset))
14091 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14092 else
14093 {
14094 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14095 "for DW_FORM_block* DW_AT_location is supported for "
14096 "DW_TAG_call_site child DIE %s "
14097 "[in module %s]"),
14098 sect_offset_str (child_die->sect_off),
14099 objfile_name (objfile));
14100 continue;
14101 }
14102 }
14103
14104 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14107 if (attr == NULL || !attr->form_is_block ())
14108 {
14109 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14110 "DW_TAG_call_site child DIE %s [in module %s]"),
14111 sect_offset_str (child_die->sect_off),
14112 objfile_name (objfile));
14113 continue;
14114 }
14115
14116 struct dwarf_block *block = attr->as_block ();
14117 parameter->value = block->data;
14118 parameter->value_size = block->size;
14119
14120 /* Parameters are not pre-cleared by memset above. */
14121 parameter->data_value = NULL;
14122 parameter->data_value_size = 0;
14123 call_site->parameter_count++;
14124
14125 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14126 if (attr == NULL)
14127 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14128 if (attr != nullptr)
14129 {
14130 if (!attr->form_is_block ())
14131 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14132 "DW_TAG_call_site child DIE %s [in module %s]"),
14133 sect_offset_str (child_die->sect_off),
14134 objfile_name (objfile));
14135 else
14136 {
14137 block = attr->as_block ();
14138 parameter->data_value = block->data;
14139 parameter->data_value_size = block->size;
14140 }
14141 }
14142 }
14143 }
14144
14145 /* Helper function for read_variable. If DIE represents a virtual
14146 table, then return the type of the concrete object that is
14147 associated with the virtual table. Otherwise, return NULL. */
14148
14149 static struct type *
14150 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14151 {
14152 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14153 if (attr == NULL)
14154 return NULL;
14155
14156 /* Find the type DIE. */
14157 struct die_info *type_die = NULL;
14158 struct dwarf2_cu *type_cu = cu;
14159
14160 if (attr->form_is_ref ())
14161 type_die = follow_die_ref (die, attr, &type_cu);
14162 if (type_die == NULL)
14163 return NULL;
14164
14165 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14166 return NULL;
14167 return die_containing_type (type_die, type_cu);
14168 }
14169
14170 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14171
14172 static void
14173 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14174 {
14175 struct rust_vtable_symbol *storage = NULL;
14176
14177 if (cu->per_cu->lang == language_rust)
14178 {
14179 struct type *containing_type = rust_containing_type (die, cu);
14180
14181 if (containing_type != NULL)
14182 {
14183 struct objfile *objfile = cu->per_objfile->objfile;
14184
14185 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14186 storage->concrete_type = containing_type;
14187 storage->subclass = SYMBOL_RUST_VTABLE;
14188 }
14189 }
14190
14191 struct symbol *res = new_symbol (die, NULL, cu, storage);
14192 struct attribute *abstract_origin
14193 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14194 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14195 if (res == NULL && loc && abstract_origin)
14196 {
14197 /* We have a variable without a name, but with a location and an abstract
14198 origin. This may be a concrete instance of an abstract variable
14199 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14200 later. */
14201 struct dwarf2_cu *origin_cu = cu;
14202 struct die_info *origin_die
14203 = follow_die_ref (die, abstract_origin, &origin_cu);
14204 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14205 per_objfile->per_bfd->abstract_to_concrete
14206 [origin_die->sect_off].push_back (die->sect_off);
14207 }
14208 }
14209
14210 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14211 reading .debug_rnglists.
14212 Callback's type should be:
14213 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14214 Return true if the attributes are present and valid, otherwise,
14215 return false. */
14216
14217 template <typename Callback>
14218 static bool
14219 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14220 dwarf_tag tag, Callback &&callback)
14221 {
14222 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14223 struct objfile *objfile = per_objfile->objfile;
14224 bfd *obfd = objfile->obfd;
14225 /* Base address selection entry. */
14226 gdb::optional<CORE_ADDR> base;
14227 const gdb_byte *buffer;
14228 bool overflow = false;
14229 ULONGEST addr_index;
14230 struct dwarf2_section_info *rnglists_section;
14231
14232 base = cu->base_address;
14233 rnglists_section = cu_debug_rnglists_section (cu, tag);
14234 rnglists_section->read (objfile);
14235
14236 if (offset >= rnglists_section->size)
14237 {
14238 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14239 offset);
14240 return false;
14241 }
14242 buffer = rnglists_section->buffer + offset;
14243
14244 while (1)
14245 {
14246 /* Initialize it due to a false compiler warning. */
14247 CORE_ADDR range_beginning = 0, range_end = 0;
14248 const gdb_byte *buf_end = (rnglists_section->buffer
14249 + rnglists_section->size);
14250 unsigned int bytes_read;
14251
14252 if (buffer == buf_end)
14253 {
14254 overflow = true;
14255 break;
14256 }
14257 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14258 switch (rlet)
14259 {
14260 case DW_RLE_end_of_list:
14261 break;
14262 case DW_RLE_base_address:
14263 if (buffer + cu->header.addr_size > buf_end)
14264 {
14265 overflow = true;
14266 break;
14267 }
14268 base = cu->header.read_address (obfd, buffer, &bytes_read);
14269 buffer += bytes_read;
14270 break;
14271 case DW_RLE_base_addressx:
14272 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14273 buffer += bytes_read;
14274 base = read_addr_index (cu, addr_index);
14275 break;
14276 case DW_RLE_start_length:
14277 if (buffer + cu->header.addr_size > buf_end)
14278 {
14279 overflow = true;
14280 break;
14281 }
14282 range_beginning = cu->header.read_address (obfd, buffer,
14283 &bytes_read);
14284 buffer += bytes_read;
14285 range_end = (range_beginning
14286 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14287 buffer += bytes_read;
14288 if (buffer > buf_end)
14289 {
14290 overflow = true;
14291 break;
14292 }
14293 break;
14294 case DW_RLE_startx_length:
14295 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14296 buffer += bytes_read;
14297 range_beginning = read_addr_index (cu, addr_index);
14298 if (buffer > buf_end)
14299 {
14300 overflow = true;
14301 break;
14302 }
14303 range_end = (range_beginning
14304 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14305 buffer += bytes_read;
14306 break;
14307 case DW_RLE_offset_pair:
14308 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14309 buffer += bytes_read;
14310 if (buffer > buf_end)
14311 {
14312 overflow = true;
14313 break;
14314 }
14315 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14316 buffer += bytes_read;
14317 if (buffer > buf_end)
14318 {
14319 overflow = true;
14320 break;
14321 }
14322 break;
14323 case DW_RLE_start_end:
14324 if (buffer + 2 * cu->header.addr_size > buf_end)
14325 {
14326 overflow = true;
14327 break;
14328 }
14329 range_beginning = cu->header.read_address (obfd, buffer,
14330 &bytes_read);
14331 buffer += bytes_read;
14332 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14333 buffer += bytes_read;
14334 break;
14335 case DW_RLE_startx_endx:
14336 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14337 buffer += bytes_read;
14338 range_beginning = read_addr_index (cu, addr_index);
14339 if (buffer > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14345 buffer += bytes_read;
14346 range_end = read_addr_index (cu, addr_index);
14347 break;
14348 default:
14349 complaint (_("Invalid .debug_rnglists data (no base address)"));
14350 return false;
14351 }
14352 if (rlet == DW_RLE_end_of_list || overflow)
14353 break;
14354 if (rlet == DW_RLE_base_address)
14355 continue;
14356
14357 if (range_beginning > range_end)
14358 {
14359 /* Inverted range entries are invalid. */
14360 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14361 return false;
14362 }
14363
14364 /* Empty range entries have no effect. */
14365 if (range_beginning == range_end)
14366 continue;
14367
14368 /* Only DW_RLE_offset_pair needs the base address added. */
14369 if (rlet == DW_RLE_offset_pair)
14370 {
14371 if (!base.has_value ())
14372 {
14373 /* We have no valid base address for the DW_RLE_offset_pair. */
14374 complaint (_("Invalid .debug_rnglists data (no base address for "
14375 "DW_RLE_offset_pair)"));
14376 return false;
14377 }
14378
14379 range_beginning += *base;
14380 range_end += *base;
14381 }
14382
14383 /* A not-uncommon case of bad debug info.
14384 Don't pollute the addrmap with bad data. */
14385 if (range_beginning == 0
14386 && !per_objfile->per_bfd->has_section_at_zero)
14387 {
14388 complaint (_(".debug_rnglists entry has start address of zero"
14389 " [in module %s]"), objfile_name (objfile));
14390 continue;
14391 }
14392
14393 callback (range_beginning, range_end);
14394 }
14395
14396 if (overflow)
14397 {
14398 complaint (_("Offset %d is not terminated "
14399 "for DW_AT_ranges attribute"),
14400 offset);
14401 return false;
14402 }
14403
14404 return true;
14405 }
14406
14407 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14408 Callback's type should be:
14409 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14410 Return 1 if the attributes are present and valid, otherwise, return 0. */
14411
14412 template <typename Callback>
14413 static int
14414 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14415 Callback &&callback)
14416 {
14417 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14418 struct objfile *objfile = per_objfile->objfile;
14419 struct comp_unit_head *cu_header = &cu->header;
14420 bfd *obfd = objfile->obfd;
14421 unsigned int addr_size = cu_header->addr_size;
14422 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14423 /* Base address selection entry. */
14424 gdb::optional<CORE_ADDR> base;
14425 unsigned int dummy;
14426 const gdb_byte *buffer;
14427
14428 if (cu_header->version >= 5)
14429 return dwarf2_rnglists_process (offset, cu, tag, callback);
14430
14431 base = cu->base_address;
14432
14433 per_objfile->per_bfd->ranges.read (objfile);
14434 if (offset >= per_objfile->per_bfd->ranges.size)
14435 {
14436 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14437 offset);
14438 return 0;
14439 }
14440 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14441
14442 while (1)
14443 {
14444 CORE_ADDR range_beginning, range_end;
14445
14446 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14447 buffer += addr_size;
14448 range_end = cu->header.read_address (obfd, buffer, &dummy);
14449 buffer += addr_size;
14450 offset += 2 * addr_size;
14451
14452 /* An end of list marker is a pair of zero addresses. */
14453 if (range_beginning == 0 && range_end == 0)
14454 /* Found the end of list entry. */
14455 break;
14456
14457 /* Each base address selection entry is a pair of 2 values.
14458 The first is the largest possible address, the second is
14459 the base address. Check for a base address here. */
14460 if ((range_beginning & mask) == mask)
14461 {
14462 /* If we found the largest possible address, then we already
14463 have the base address in range_end. */
14464 base = range_end;
14465 continue;
14466 }
14467
14468 if (!base.has_value ())
14469 {
14470 /* We have no valid base address for the ranges
14471 data. */
14472 complaint (_("Invalid .debug_ranges data (no base address)"));
14473 return 0;
14474 }
14475
14476 if (range_beginning > range_end)
14477 {
14478 /* Inverted range entries are invalid. */
14479 complaint (_("Invalid .debug_ranges data (inverted range)"));
14480 return 0;
14481 }
14482
14483 /* Empty range entries have no effect. */
14484 if (range_beginning == range_end)
14485 continue;
14486
14487 range_beginning += *base;
14488 range_end += *base;
14489
14490 /* A not-uncommon case of bad debug info.
14491 Don't pollute the addrmap with bad data. */
14492 if (range_beginning == 0
14493 && !per_objfile->per_bfd->has_section_at_zero)
14494 {
14495 complaint (_(".debug_ranges entry has start address of zero"
14496 " [in module %s]"), objfile_name (objfile));
14497 continue;
14498 }
14499
14500 callback (range_beginning, range_end);
14501 }
14502
14503 return 1;
14504 }
14505
14506 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14507 Return 1 if the attributes are present and valid, otherwise, return 0.
14508 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
14509 ranges in MAP are set, using DATUM as the value. */
14510
14511 static int
14512 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14513 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14514 addrmap *map, void *datum, dwarf_tag tag)
14515 {
14516 struct objfile *objfile = cu->per_objfile->objfile;
14517 struct gdbarch *gdbarch = objfile->arch ();
14518 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14519 int low_set = 0;
14520 CORE_ADDR low = 0;
14521 CORE_ADDR high = 0;
14522 int retval;
14523
14524 retval = dwarf2_ranges_process (offset, cu, tag,
14525 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14526 {
14527 if (map != nullptr)
14528 {
14529 CORE_ADDR lowpc;
14530 CORE_ADDR highpc;
14531
14532 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14533 range_beginning + baseaddr)
14534 - baseaddr);
14535 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14536 range_end + baseaddr)
14537 - baseaddr);
14538 addrmap_set_empty (map, lowpc, highpc - 1, datum);
14539 }
14540
14541 /* FIXME: This is recording everything as a low-high
14542 segment of consecutive addresses. We should have a
14543 data structure for discontiguous block ranges
14544 instead. */
14545 if (! low_set)
14546 {
14547 low = range_beginning;
14548 high = range_end;
14549 low_set = 1;
14550 }
14551 else
14552 {
14553 if (range_beginning < low)
14554 low = range_beginning;
14555 if (range_end > high)
14556 high = range_end;
14557 }
14558 });
14559 if (!retval)
14560 return 0;
14561
14562 if (! low_set)
14563 /* If the first entry is an end-of-list marker, the range
14564 describes an empty scope, i.e. no instructions. */
14565 return 0;
14566
14567 if (low_return)
14568 *low_return = low;
14569 if (high_return)
14570 *high_return = high;
14571 return 1;
14572 }
14573
14574 /* Process ranges and fill in a vector of the low PC values only. */
14575
14576 static void
14577 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
14578 dwarf_tag tag,
14579 std::vector<CORE_ADDR> &result)
14580 {
14581 dwarf2_ranges_process (offset, cu, tag,
14582 [&] (CORE_ADDR start, CORE_ADDR end)
14583 {
14584 result.push_back (start);
14585 });
14586 }
14587
14588 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14589 definition for the return value. *LOWPC and *HIGHPC are set iff
14590 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14591
14592 static enum pc_bounds_kind
14593 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14594 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14595 addrmap *map, void *datum)
14596 {
14597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14598 struct attribute *attr;
14599 struct attribute *attr_high;
14600 CORE_ADDR low = 0;
14601 CORE_ADDR high = 0;
14602 enum pc_bounds_kind ret;
14603
14604 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14605 if (attr_high)
14606 {
14607 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14608 if (attr != nullptr)
14609 {
14610 low = attr->as_address ();
14611 high = attr_high->as_address ();
14612 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14613 high += low;
14614 }
14615 else
14616 /* Found high w/o low attribute. */
14617 return PC_BOUNDS_INVALID;
14618
14619 /* Found consecutive range of addresses. */
14620 ret = PC_BOUNDS_HIGH_LOW;
14621 }
14622 else
14623 {
14624 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14625 if (attr != nullptr && attr->form_is_unsigned ())
14626 {
14627 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14628 on DWARF version). */
14629 ULONGEST ranges_offset = attr->as_unsigned ();
14630
14631 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14632 this value. */
14633 if (die->tag != DW_TAG_compile_unit)
14634 ranges_offset += cu->gnu_ranges_base;
14635
14636 /* Value of the DW_AT_ranges attribute is the offset in the
14637 .debug_ranges section. */
14638 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
14639 map, datum, die->tag))
14640 return PC_BOUNDS_INVALID;
14641 /* Found discontinuous range of addresses. */
14642 ret = PC_BOUNDS_RANGES;
14643 }
14644 else
14645 return PC_BOUNDS_NOT_PRESENT;
14646 }
14647
14648 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14649 if (high <= low)
14650 return PC_BOUNDS_INVALID;
14651
14652 /* When using the GNU linker, .gnu.linkonce. sections are used to
14653 eliminate duplicate copies of functions and vtables and such.
14654 The linker will arbitrarily choose one and discard the others.
14655 The AT_*_pc values for such functions refer to local labels in
14656 these sections. If the section from that file was discarded, the
14657 labels are not in the output, so the relocs get a value of 0.
14658 If this is a discarded function, mark the pc bounds as invalid,
14659 so that GDB will ignore it. */
14660 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14661 return PC_BOUNDS_INVALID;
14662
14663 *lowpc = low;
14664 if (highpc)
14665 *highpc = high;
14666 return ret;
14667 }
14668
14669 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14670 its low and high PC addresses. Do nothing if these addresses could not
14671 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14672 and HIGHPC to the high address if greater than HIGHPC. */
14673
14674 static void
14675 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14676 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14677 struct dwarf2_cu *cu)
14678 {
14679 CORE_ADDR low, high;
14680 struct die_info *child = die->child;
14681
14682 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
14683 >= PC_BOUNDS_RANGES)
14684 {
14685 *lowpc = std::min (*lowpc, low);
14686 *highpc = std::max (*highpc, high);
14687 }
14688
14689 /* If the language does not allow nested subprograms (either inside
14690 subprograms or lexical blocks), we're done. */
14691 if (cu->per_cu->lang != language_ada)
14692 return;
14693
14694 /* Check all the children of the given DIE. If it contains nested
14695 subprograms, then check their pc bounds. Likewise, we need to
14696 check lexical blocks as well, as they may also contain subprogram
14697 definitions. */
14698 while (child && child->tag)
14699 {
14700 if (child->tag == DW_TAG_subprogram
14701 || child->tag == DW_TAG_lexical_block)
14702 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14703 child = child->sibling;
14704 }
14705 }
14706
14707 /* Get the low and high pc's represented by the scope DIE, and store
14708 them in *LOWPC and *HIGHPC. If the correct values can't be
14709 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14710
14711 static void
14712 get_scope_pc_bounds (struct die_info *die,
14713 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14714 struct dwarf2_cu *cu)
14715 {
14716 CORE_ADDR best_low = (CORE_ADDR) -1;
14717 CORE_ADDR best_high = (CORE_ADDR) 0;
14718 CORE_ADDR current_low, current_high;
14719
14720 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
14721 nullptr, nullptr)
14722 >= PC_BOUNDS_RANGES)
14723 {
14724 best_low = current_low;
14725 best_high = current_high;
14726 }
14727 else
14728 {
14729 struct die_info *child = die->child;
14730
14731 while (child && child->tag)
14732 {
14733 switch (child->tag) {
14734 case DW_TAG_subprogram:
14735 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14736 break;
14737 case DW_TAG_namespace:
14738 case DW_TAG_module:
14739 /* FIXME: carlton/2004-01-16: Should we do this for
14740 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14741 that current GCC's always emit the DIEs corresponding
14742 to definitions of methods of classes as children of a
14743 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14744 the DIEs giving the declarations, which could be
14745 anywhere). But I don't see any reason why the
14746 standards says that they have to be there. */
14747 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14748
14749 if (current_low != ((CORE_ADDR) -1))
14750 {
14751 best_low = std::min (best_low, current_low);
14752 best_high = std::max (best_high, current_high);
14753 }
14754 break;
14755 default:
14756 /* Ignore. */
14757 break;
14758 }
14759
14760 child = child->sibling;
14761 }
14762 }
14763
14764 *lowpc = best_low;
14765 *highpc = best_high;
14766 }
14767
14768 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14769 in DIE. */
14770
14771 static void
14772 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14773 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14774 {
14775 struct objfile *objfile = cu->per_objfile->objfile;
14776 struct gdbarch *gdbarch = objfile->arch ();
14777 struct attribute *attr;
14778 struct attribute *attr_high;
14779
14780 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14781 if (attr_high)
14782 {
14783 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14784 if (attr != nullptr)
14785 {
14786 CORE_ADDR low = attr->as_address ();
14787 CORE_ADDR high = attr_high->as_address ();
14788
14789 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14790 high += low;
14791
14792 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14793 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14794 cu->get_builder ()->record_block_range (block, low, high - 1);
14795 }
14796 }
14797
14798 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14799 if (attr != nullptr && attr->form_is_unsigned ())
14800 {
14801 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14802 on DWARF version). */
14803 ULONGEST ranges_offset = attr->as_unsigned ();
14804
14805 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14806 this value. */
14807 if (die->tag != DW_TAG_compile_unit)
14808 ranges_offset += cu->gnu_ranges_base;
14809
14810 std::vector<blockrange> blockvec;
14811 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14812 [&] (CORE_ADDR start, CORE_ADDR end)
14813 {
14814 start += baseaddr;
14815 end += baseaddr;
14816 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14817 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14818 cu->get_builder ()->record_block_range (block, start, end - 1);
14819 blockvec.emplace_back (start, end);
14820 });
14821
14822 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14823 }
14824 }
14825
14826 /* Check whether the producer field indicates either of GCC < 4.6, or the
14827 Intel C/C++ compiler, and cache the result in CU. */
14828
14829 static void
14830 check_producer (struct dwarf2_cu *cu)
14831 {
14832 int major, minor;
14833
14834 if (cu->producer == NULL)
14835 {
14836 /* For unknown compilers expect their behavior is DWARF version
14837 compliant.
14838
14839 GCC started to support .debug_types sections by -gdwarf-4 since
14840 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14841 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14842 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14843 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14844 }
14845 else if (producer_is_gcc (cu->producer, &major, &minor))
14846 {
14847 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14848 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14849 cu->producer_is_gcc_11 = major == 11;
14850 }
14851 else if (producer_is_icc (cu->producer, &major, &minor))
14852 {
14853 cu->producer_is_icc = true;
14854 cu->producer_is_icc_lt_14 = major < 14;
14855 }
14856 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14857 cu->producer_is_codewarrior = true;
14858 else
14859 {
14860 /* For other non-GCC compilers, expect their behavior is DWARF version
14861 compliant. */
14862 }
14863
14864 cu->checked_producer = true;
14865 }
14866
14867 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14868 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14869 during 4.6.0 experimental. */
14870
14871 static bool
14872 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14873 {
14874 if (!cu->checked_producer)
14875 check_producer (cu);
14876
14877 return cu->producer_is_gxx_lt_4_6;
14878 }
14879
14880
14881 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14882 with incorrect is_stmt attributes. */
14883
14884 static bool
14885 producer_is_codewarrior (struct dwarf2_cu *cu)
14886 {
14887 if (!cu->checked_producer)
14888 check_producer (cu);
14889
14890 return cu->producer_is_codewarrior;
14891 }
14892
14893 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14894 If that attribute is not available, return the appropriate
14895 default. */
14896
14897 static enum dwarf_access_attribute
14898 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14899 {
14900 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14901 if (attr != nullptr)
14902 {
14903 LONGEST value = attr->constant_value (-1);
14904 if (value == DW_ACCESS_public
14905 || value == DW_ACCESS_protected
14906 || value == DW_ACCESS_private)
14907 return (dwarf_access_attribute) value;
14908 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14909 plongest (value));
14910 }
14911
14912 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14913 {
14914 /* The default DWARF 2 accessibility for members is public, the default
14915 accessibility for inheritance is private. */
14916
14917 if (die->tag != DW_TAG_inheritance)
14918 return DW_ACCESS_public;
14919 else
14920 return DW_ACCESS_private;
14921 }
14922 else
14923 {
14924 /* DWARF 3+ defines the default accessibility a different way. The same
14925 rules apply now for DW_TAG_inheritance as for the members and it only
14926 depends on the container kind. */
14927
14928 if (die->parent->tag == DW_TAG_class_type)
14929 return DW_ACCESS_private;
14930 else
14931 return DW_ACCESS_public;
14932 }
14933 }
14934
14935 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14936 *OFFSET to the byte offset. If the attribute was not found return
14937 0, otherwise return 1. If it was found but could not properly be
14938 handled, set *OFFSET to 0. */
14939
14940 static int
14941 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14942 LONGEST *offset)
14943 {
14944 struct attribute *attr;
14945
14946 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14947 if (attr != NULL)
14948 {
14949 *offset = 0;
14950
14951 /* Note that we do not check for a section offset first here.
14952 This is because DW_AT_data_member_location is new in DWARF 4,
14953 so if we see it, we can assume that a constant form is really
14954 a constant and not a section offset. */
14955 if (attr->form_is_constant ())
14956 *offset = attr->constant_value (0);
14957 else if (attr->form_is_section_offset ())
14958 dwarf2_complex_location_expr_complaint ();
14959 else if (attr->form_is_block ())
14960 *offset = decode_locdesc (attr->as_block (), cu);
14961 else
14962 dwarf2_complex_location_expr_complaint ();
14963
14964 return 1;
14965 }
14966 else
14967 {
14968 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14969 if (attr != nullptr)
14970 {
14971 *offset = attr->constant_value (0);
14972 return 1;
14973 }
14974 }
14975
14976 return 0;
14977 }
14978
14979 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14980 store the results in FIELD. */
14981
14982 static void
14983 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14984 struct field *field)
14985 {
14986 struct attribute *attr;
14987
14988 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14989 if (attr != NULL)
14990 {
14991 if (attr->form_is_constant ())
14992 {
14993 LONGEST offset = attr->constant_value (0);
14994
14995 /* Work around this GCC 11 bug, where it would erroneously use -1
14996 data member locations, instead of 0:
14997
14998 Negative DW_AT_data_member_location
14999 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
15000 */
15001 if (offset == -1 && cu->producer_is_gcc_11)
15002 {
15003 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
15004 offset = 0;
15005 }
15006
15007 field->set_loc_bitpos (offset * bits_per_byte);
15008 }
15009 else if (attr->form_is_section_offset ())
15010 dwarf2_complex_location_expr_complaint ();
15011 else if (attr->form_is_block ())
15012 {
15013 bool handled;
15014 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15015 if (handled)
15016 field->set_loc_bitpos (offset * bits_per_byte);
15017 else
15018 {
15019 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15020 struct objfile *objfile = per_objfile->objfile;
15021 struct dwarf2_locexpr_baton *dlbaton
15022 = XOBNEW (&objfile->objfile_obstack,
15023 struct dwarf2_locexpr_baton);
15024 dlbaton->data = attr->as_block ()->data;
15025 dlbaton->size = attr->as_block ()->size;
15026 /* When using this baton, we want to compute the address
15027 of the field, not the value. This is why
15028 is_reference is set to false here. */
15029 dlbaton->is_reference = false;
15030 dlbaton->per_objfile = per_objfile;
15031 dlbaton->per_cu = cu->per_cu;
15032
15033 field->set_loc_dwarf_block (dlbaton);
15034 }
15035 }
15036 else
15037 dwarf2_complex_location_expr_complaint ();
15038 }
15039 else
15040 {
15041 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15042 if (attr != nullptr)
15043 field->set_loc_bitpos (attr->constant_value (0));
15044 }
15045 }
15046
15047 /* Add an aggregate field to the field list. */
15048
15049 static void
15050 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15051 struct dwarf2_cu *cu)
15052 {
15053 struct objfile *objfile = cu->per_objfile->objfile;
15054 struct gdbarch *gdbarch = objfile->arch ();
15055 struct nextfield *new_field;
15056 struct attribute *attr;
15057 struct field *fp;
15058 const char *fieldname = "";
15059
15060 if (die->tag == DW_TAG_inheritance)
15061 {
15062 fip->baseclasses.emplace_back ();
15063 new_field = &fip->baseclasses.back ();
15064 }
15065 else
15066 {
15067 fip->fields.emplace_back ();
15068 new_field = &fip->fields.back ();
15069 }
15070
15071 new_field->offset = die->sect_off;
15072
15073 new_field->accessibility = dwarf2_access_attribute (die, cu);
15074 if (new_field->accessibility != DW_ACCESS_public)
15075 fip->non_public_fields = true;
15076
15077 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15078 if (attr != nullptr)
15079 new_field->virtuality = attr->as_virtuality ();
15080 else
15081 new_field->virtuality = DW_VIRTUALITY_none;
15082
15083 fp = &new_field->field;
15084
15085 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
15086 && !die_is_declaration (die, cu))
15087 {
15088 if (die->tag == DW_TAG_namelist_item)
15089 {
15090 /* Typically, DW_TAG_namelist_item are references to namelist items.
15091 If so, follow that reference. */
15092 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
15093 struct die_info *item_die = nullptr;
15094 struct dwarf2_cu *item_cu = cu;
15095 if (attr1->form_is_ref ())
15096 item_die = follow_die_ref (die, attr1, &item_cu);
15097 if (item_die != nullptr)
15098 die = item_die;
15099 }
15100 /* Data member other than a C++ static data member. */
15101
15102 /* Get type of field. */
15103 fp->set_type (die_type (die, cu));
15104
15105 fp->set_loc_bitpos (0);
15106
15107 /* Get bit size of field (zero if none). */
15108 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15109 if (attr != nullptr)
15110 {
15111 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15112 }
15113 else
15114 {
15115 FIELD_BITSIZE (*fp) = 0;
15116 }
15117
15118 /* Get bit offset of field. */
15119 handle_member_location (die, cu, fp);
15120 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15121 if (attr != nullptr && attr->form_is_constant ())
15122 {
15123 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15124 {
15125 /* For big endian bits, the DW_AT_bit_offset gives the
15126 additional bit offset from the MSB of the containing
15127 anonymous object to the MSB of the field. We don't
15128 have to do anything special since we don't need to
15129 know the size of the anonymous object. */
15130 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
15131 }
15132 else
15133 {
15134 /* For little endian bits, compute the bit offset to the
15135 MSB of the anonymous object, subtract off the number of
15136 bits from the MSB of the field to the MSB of the
15137 object, and then subtract off the number of bits of
15138 the field itself. The result is the bit offset of
15139 the LSB of the field. */
15140 int anonymous_size;
15141 int bit_offset = attr->constant_value (0);
15142
15143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15144 if (attr != nullptr && attr->form_is_constant ())
15145 {
15146 /* The size of the anonymous object containing
15147 the bit field is explicit, so use the
15148 indicated size (in bytes). */
15149 anonymous_size = attr->constant_value (0);
15150 }
15151 else
15152 {
15153 /* The size of the anonymous object containing
15154 the bit field must be inferred from the type
15155 attribute of the data member containing the
15156 bit field. */
15157 anonymous_size = TYPE_LENGTH (fp->type ());
15158 }
15159 fp->set_loc_bitpos (fp->loc_bitpos ()
15160 + anonymous_size * bits_per_byte
15161 - bit_offset - FIELD_BITSIZE (*fp));
15162 }
15163 }
15164
15165 /* Get name of field. */
15166 fieldname = dwarf2_name (die, cu);
15167 if (fieldname == NULL)
15168 fieldname = "";
15169
15170 /* The name is already allocated along with this objfile, so we don't
15171 need to duplicate it for the type. */
15172 fp->set_name (fieldname);
15173
15174 /* Change accessibility for artificial fields (e.g. virtual table
15175 pointer or virtual base class pointer) to private. */
15176 if (dwarf2_attr (die, DW_AT_artificial, cu))
15177 {
15178 FIELD_ARTIFICIAL (*fp) = 1;
15179 new_field->accessibility = DW_ACCESS_private;
15180 fip->non_public_fields = true;
15181 }
15182 }
15183 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15184 {
15185 /* C++ static member. */
15186
15187 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15188 is a declaration, but all versions of G++ as of this writing
15189 (so through at least 3.2.1) incorrectly generate
15190 DW_TAG_variable tags. */
15191
15192 const char *physname;
15193
15194 /* Get name of field. */
15195 fieldname = dwarf2_name (die, cu);
15196 if (fieldname == NULL)
15197 return;
15198
15199 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15200 if (attr
15201 /* Only create a symbol if this is an external value.
15202 new_symbol checks this and puts the value in the global symbol
15203 table, which we want. If it is not external, new_symbol
15204 will try to put the value in cu->list_in_scope which is wrong. */
15205 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15206 {
15207 /* A static const member, not much different than an enum as far as
15208 we're concerned, except that we can support more types. */
15209 new_symbol (die, NULL, cu);
15210 }
15211
15212 /* Get physical name. */
15213 physname = dwarf2_physname (fieldname, die, cu);
15214
15215 /* The name is already allocated along with this objfile, so we don't
15216 need to duplicate it for the type. */
15217 fp->set_loc_physname (physname ? physname : "");
15218 fp->set_type (die_type (die, cu));
15219 fp->set_name (fieldname);
15220 }
15221 else if (die->tag == DW_TAG_inheritance)
15222 {
15223 /* C++ base class field. */
15224 handle_member_location (die, cu, fp);
15225 FIELD_BITSIZE (*fp) = 0;
15226 fp->set_type (die_type (die, cu));
15227 fp->set_name (fp->type ()->name ());
15228 }
15229 else
15230 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15231 }
15232
15233 /* Can the type given by DIE define another type? */
15234
15235 static bool
15236 type_can_define_types (const struct die_info *die)
15237 {
15238 switch (die->tag)
15239 {
15240 case DW_TAG_typedef:
15241 case DW_TAG_class_type:
15242 case DW_TAG_structure_type:
15243 case DW_TAG_union_type:
15244 case DW_TAG_enumeration_type:
15245 return true;
15246
15247 default:
15248 return false;
15249 }
15250 }
15251
15252 /* Add a type definition defined in the scope of the FIP's class. */
15253
15254 static void
15255 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15256 struct dwarf2_cu *cu)
15257 {
15258 struct decl_field fp;
15259 memset (&fp, 0, sizeof (fp));
15260
15261 gdb_assert (type_can_define_types (die));
15262
15263 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15264 fp.name = dwarf2_name (die, cu);
15265 fp.type = read_type_die (die, cu);
15266
15267 /* Save accessibility. */
15268 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15269 switch (accessibility)
15270 {
15271 case DW_ACCESS_public:
15272 /* The assumed value if neither private nor protected. */
15273 break;
15274 case DW_ACCESS_private:
15275 fp.is_private = 1;
15276 break;
15277 case DW_ACCESS_protected:
15278 fp.is_protected = 1;
15279 break;
15280 }
15281
15282 if (die->tag == DW_TAG_typedef)
15283 fip->typedef_field_list.push_back (fp);
15284 else
15285 fip->nested_types_list.push_back (fp);
15286 }
15287
15288 /* A convenience typedef that's used when finding the discriminant
15289 field for a variant part. */
15290 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15291 offset_map_type;
15292
15293 /* Compute the discriminant range for a given variant. OBSTACK is
15294 where the results will be stored. VARIANT is the variant to
15295 process. IS_UNSIGNED indicates whether the discriminant is signed
15296 or unsigned. */
15297
15298 static const gdb::array_view<discriminant_range>
15299 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15300 bool is_unsigned)
15301 {
15302 std::vector<discriminant_range> ranges;
15303
15304 if (variant.default_branch)
15305 return {};
15306
15307 if (variant.discr_list_data == nullptr)
15308 {
15309 discriminant_range r
15310 = {variant.discriminant_value, variant.discriminant_value};
15311 ranges.push_back (r);
15312 }
15313 else
15314 {
15315 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15316 variant.discr_list_data->size);
15317 while (!data.empty ())
15318 {
15319 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15320 {
15321 complaint (_("invalid discriminant marker: %d"), data[0]);
15322 break;
15323 }
15324 bool is_range = data[0] == DW_DSC_range;
15325 data = data.slice (1);
15326
15327 ULONGEST low, high;
15328 unsigned int bytes_read;
15329
15330 if (data.empty ())
15331 {
15332 complaint (_("DW_AT_discr_list missing low value"));
15333 break;
15334 }
15335 if (is_unsigned)
15336 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15337 else
15338 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15339 &bytes_read);
15340 data = data.slice (bytes_read);
15341
15342 if (is_range)
15343 {
15344 if (data.empty ())
15345 {
15346 complaint (_("DW_AT_discr_list missing high value"));
15347 break;
15348 }
15349 if (is_unsigned)
15350 high = read_unsigned_leb128 (nullptr, data.data (),
15351 &bytes_read);
15352 else
15353 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15354 &bytes_read);
15355 data = data.slice (bytes_read);
15356 }
15357 else
15358 high = low;
15359
15360 ranges.push_back ({ low, high });
15361 }
15362 }
15363
15364 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15365 ranges.size ());
15366 std::copy (ranges.begin (), ranges.end (), result);
15367 return gdb::array_view<discriminant_range> (result, ranges.size ());
15368 }
15369
15370 static const gdb::array_view<variant_part> create_variant_parts
15371 (struct obstack *obstack,
15372 const offset_map_type &offset_map,
15373 struct field_info *fi,
15374 const std::vector<variant_part_builder> &variant_parts);
15375
15376 /* Fill in a "struct variant" for a given variant field. RESULT is
15377 the variant to fill in. OBSTACK is where any needed allocations
15378 will be done. OFFSET_MAP holds the mapping from section offsets to
15379 fields for the type. FI describes the fields of the type we're
15380 processing. FIELD is the variant field we're converting. */
15381
15382 static void
15383 create_one_variant (variant &result, struct obstack *obstack,
15384 const offset_map_type &offset_map,
15385 struct field_info *fi, const variant_field &field)
15386 {
15387 result.discriminants = convert_variant_range (obstack, field, false);
15388 result.first_field = field.first_field + fi->baseclasses.size ();
15389 result.last_field = field.last_field + fi->baseclasses.size ();
15390 result.parts = create_variant_parts (obstack, offset_map, fi,
15391 field.variant_parts);
15392 }
15393
15394 /* Fill in a "struct variant_part" for a given variant part. RESULT
15395 is the variant part to fill in. OBSTACK is where any needed
15396 allocations will be done. OFFSET_MAP holds the mapping from
15397 section offsets to fields for the type. FI describes the fields of
15398 the type we're processing. BUILDER is the variant part to be
15399 converted. */
15400
15401 static void
15402 create_one_variant_part (variant_part &result,
15403 struct obstack *obstack,
15404 const offset_map_type &offset_map,
15405 struct field_info *fi,
15406 const variant_part_builder &builder)
15407 {
15408 auto iter = offset_map.find (builder.discriminant_offset);
15409 if (iter == offset_map.end ())
15410 {
15411 result.discriminant_index = -1;
15412 /* Doesn't matter. */
15413 result.is_unsigned = false;
15414 }
15415 else
15416 {
15417 result.discriminant_index = iter->second;
15418 result.is_unsigned
15419 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15420 }
15421
15422 size_t n = builder.variants.size ();
15423 variant *output = new (obstack) variant[n];
15424 for (size_t i = 0; i < n; ++i)
15425 create_one_variant (output[i], obstack, offset_map, fi,
15426 builder.variants[i]);
15427
15428 result.variants = gdb::array_view<variant> (output, n);
15429 }
15430
15431 /* Create a vector of variant parts that can be attached to a type.
15432 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15433 holds the mapping from section offsets to fields for the type. FI
15434 describes the fields of the type we're processing. VARIANT_PARTS
15435 is the vector to convert. */
15436
15437 static const gdb::array_view<variant_part>
15438 create_variant_parts (struct obstack *obstack,
15439 const offset_map_type &offset_map,
15440 struct field_info *fi,
15441 const std::vector<variant_part_builder> &variant_parts)
15442 {
15443 if (variant_parts.empty ())
15444 return {};
15445
15446 size_t n = variant_parts.size ();
15447 variant_part *result = new (obstack) variant_part[n];
15448 for (size_t i = 0; i < n; ++i)
15449 create_one_variant_part (result[i], obstack, offset_map, fi,
15450 variant_parts[i]);
15451
15452 return gdb::array_view<variant_part> (result, n);
15453 }
15454
15455 /* Compute the variant part vector for FIP, attaching it to TYPE when
15456 done. */
15457
15458 static void
15459 add_variant_property (struct field_info *fip, struct type *type,
15460 struct dwarf2_cu *cu)
15461 {
15462 /* Map section offsets of fields to their field index. Note the
15463 field index here does not take the number of baseclasses into
15464 account. */
15465 offset_map_type offset_map;
15466 for (int i = 0; i < fip->fields.size (); ++i)
15467 offset_map[fip->fields[i].offset] = i;
15468
15469 struct objfile *objfile = cu->per_objfile->objfile;
15470 gdb::array_view<const variant_part> parts
15471 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15472 fip->variant_parts);
15473
15474 struct dynamic_prop prop;
15475 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15476 obstack_copy (&objfile->objfile_obstack, &parts,
15477 sizeof (parts)));
15478
15479 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15480 }
15481
15482 /* Create the vector of fields, and attach it to the type. */
15483
15484 static void
15485 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15486 struct dwarf2_cu *cu)
15487 {
15488 int nfields = fip->nfields ();
15489
15490 /* Record the field count, allocate space for the array of fields,
15491 and create blank accessibility bitfields if necessary. */
15492 type->set_num_fields (nfields);
15493 type->set_fields
15494 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15495
15496 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
15497 {
15498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15499
15500 TYPE_FIELD_PRIVATE_BITS (type) =
15501 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15502 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15503
15504 TYPE_FIELD_PROTECTED_BITS (type) =
15505 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15506 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15507
15508 TYPE_FIELD_IGNORE_BITS (type) =
15509 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15510 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15511 }
15512
15513 /* If the type has baseclasses, allocate and clear a bit vector for
15514 TYPE_FIELD_VIRTUAL_BITS. */
15515 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
15516 {
15517 int num_bytes = B_BYTES (fip->baseclasses.size ());
15518 unsigned char *pointer;
15519
15520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15521 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15522 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15523 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15524 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15525 }
15526
15527 if (!fip->variant_parts.empty ())
15528 add_variant_property (fip, type, cu);
15529
15530 /* Copy the saved-up fields into the field vector. */
15531 for (int i = 0; i < nfields; ++i)
15532 {
15533 struct nextfield &field
15534 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15535 : fip->fields[i - fip->baseclasses.size ()]);
15536
15537 type->field (i) = field.field;
15538 switch (field.accessibility)
15539 {
15540 case DW_ACCESS_private:
15541 if (cu->per_cu->lang != language_ada)
15542 SET_TYPE_FIELD_PRIVATE (type, i);
15543 break;
15544
15545 case DW_ACCESS_protected:
15546 if (cu->per_cu->lang != language_ada)
15547 SET_TYPE_FIELD_PROTECTED (type, i);
15548 break;
15549
15550 case DW_ACCESS_public:
15551 break;
15552
15553 default:
15554 /* Unknown accessibility. Complain and treat it as public. */
15555 {
15556 complaint (_("unsupported accessibility %d"),
15557 field.accessibility);
15558 }
15559 break;
15560 }
15561 if (i < fip->baseclasses.size ())
15562 {
15563 switch (field.virtuality)
15564 {
15565 case DW_VIRTUALITY_virtual:
15566 case DW_VIRTUALITY_pure_virtual:
15567 if (cu->per_cu->lang == language_ada)
15568 error (_("unexpected virtuality in component of Ada type"));
15569 SET_TYPE_FIELD_VIRTUAL (type, i);
15570 break;
15571 }
15572 }
15573 }
15574 }
15575
15576 /* Return true if this member function is a constructor, false
15577 otherwise. */
15578
15579 static int
15580 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15581 {
15582 const char *fieldname;
15583 const char *type_name;
15584 int len;
15585
15586 if (die->parent == NULL)
15587 return 0;
15588
15589 if (die->parent->tag != DW_TAG_structure_type
15590 && die->parent->tag != DW_TAG_union_type
15591 && die->parent->tag != DW_TAG_class_type)
15592 return 0;
15593
15594 fieldname = dwarf2_name (die, cu);
15595 type_name = dwarf2_name (die->parent, cu);
15596 if (fieldname == NULL || type_name == NULL)
15597 return 0;
15598
15599 len = strlen (fieldname);
15600 return (strncmp (fieldname, type_name, len) == 0
15601 && (type_name[len] == '\0' || type_name[len] == '<'));
15602 }
15603
15604 /* Add a member function to the proper fieldlist. */
15605
15606 static void
15607 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15608 struct type *type, struct dwarf2_cu *cu)
15609 {
15610 struct objfile *objfile = cu->per_objfile->objfile;
15611 struct attribute *attr;
15612 int i;
15613 struct fnfieldlist *flp = nullptr;
15614 struct fn_field *fnp;
15615 const char *fieldname;
15616 struct type *this_type;
15617
15618 if (cu->per_cu->lang == language_ada)
15619 error (_("unexpected member function in Ada type"));
15620
15621 /* Get name of member function. */
15622 fieldname = dwarf2_name (die, cu);
15623 if (fieldname == NULL)
15624 return;
15625
15626 /* Look up member function name in fieldlist. */
15627 for (i = 0; i < fip->fnfieldlists.size (); i++)
15628 {
15629 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15630 {
15631 flp = &fip->fnfieldlists[i];
15632 break;
15633 }
15634 }
15635
15636 /* Create a new fnfieldlist if necessary. */
15637 if (flp == nullptr)
15638 {
15639 fip->fnfieldlists.emplace_back ();
15640 flp = &fip->fnfieldlists.back ();
15641 flp->name = fieldname;
15642 i = fip->fnfieldlists.size () - 1;
15643 }
15644
15645 /* Create a new member function field and add it to the vector of
15646 fnfieldlists. */
15647 flp->fnfields.emplace_back ();
15648 fnp = &flp->fnfields.back ();
15649
15650 /* Delay processing of the physname until later. */
15651 if (cu->per_cu->lang == language_cplus)
15652 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15653 die, cu);
15654 else
15655 {
15656 const char *physname = dwarf2_physname (fieldname, die, cu);
15657 fnp->physname = physname ? physname : "";
15658 }
15659
15660 fnp->type = alloc_type (objfile);
15661 this_type = read_type_die (die, cu);
15662 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15663 {
15664 int nparams = this_type->num_fields ();
15665
15666 /* TYPE is the domain of this method, and THIS_TYPE is the type
15667 of the method itself (TYPE_CODE_METHOD). */
15668 smash_to_method_type (fnp->type, type,
15669 TYPE_TARGET_TYPE (this_type),
15670 this_type->fields (),
15671 this_type->num_fields (),
15672 this_type->has_varargs ());
15673
15674 /* Handle static member functions.
15675 Dwarf2 has no clean way to discern C++ static and non-static
15676 member functions. G++ helps GDB by marking the first
15677 parameter for non-static member functions (which is the this
15678 pointer) as artificial. We obtain this information from
15679 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15680 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15681 fnp->voffset = VOFFSET_STATIC;
15682 }
15683 else
15684 complaint (_("member function type missing for '%s'"),
15685 dwarf2_full_name (fieldname, die, cu));
15686
15687 /* Get fcontext from DW_AT_containing_type if present. */
15688 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15689 fnp->fcontext = die_containing_type (die, cu);
15690
15691 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15692 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15693
15694 /* Get accessibility. */
15695 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15696 switch (accessibility)
15697 {
15698 case DW_ACCESS_private:
15699 fnp->is_private = 1;
15700 break;
15701 case DW_ACCESS_protected:
15702 fnp->is_protected = 1;
15703 break;
15704 }
15705
15706 /* Check for artificial methods. */
15707 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15708 if (attr && attr->as_boolean ())
15709 fnp->is_artificial = 1;
15710
15711 /* Check for defaulted methods. */
15712 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15713 if (attr != nullptr)
15714 fnp->defaulted = attr->defaulted ();
15715
15716 /* Check for deleted methods. */
15717 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15718 if (attr != nullptr && attr->as_boolean ())
15719 fnp->is_deleted = 1;
15720
15721 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15722
15723 /* Get index in virtual function table if it is a virtual member
15724 function. For older versions of GCC, this is an offset in the
15725 appropriate virtual table, as specified by DW_AT_containing_type.
15726 For everyone else, it is an expression to be evaluated relative
15727 to the object address. */
15728
15729 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15730 if (attr != nullptr)
15731 {
15732 if (attr->form_is_block () && attr->as_block ()->size > 0)
15733 {
15734 struct dwarf_block *block = attr->as_block ();
15735
15736 if (block->data[0] == DW_OP_constu)
15737 {
15738 /* Old-style GCC. */
15739 fnp->voffset = decode_locdesc (block, cu) + 2;
15740 }
15741 else if (block->data[0] == DW_OP_deref
15742 || (block->size > 1
15743 && block->data[0] == DW_OP_deref_size
15744 && block->data[1] == cu->header.addr_size))
15745 {
15746 fnp->voffset = decode_locdesc (block, cu);
15747 if ((fnp->voffset % cu->header.addr_size) != 0)
15748 dwarf2_complex_location_expr_complaint ();
15749 else
15750 fnp->voffset /= cu->header.addr_size;
15751 fnp->voffset += 2;
15752 }
15753 else
15754 dwarf2_complex_location_expr_complaint ();
15755
15756 if (!fnp->fcontext)
15757 {
15758 /* If there is no `this' field and no DW_AT_containing_type,
15759 we cannot actually find a base class context for the
15760 vtable! */
15761 if (this_type->num_fields () == 0
15762 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15763 {
15764 complaint (_("cannot determine context for virtual member "
15765 "function \"%s\" (offset %s)"),
15766 fieldname, sect_offset_str (die->sect_off));
15767 }
15768 else
15769 {
15770 fnp->fcontext
15771 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15772 }
15773 }
15774 }
15775 else if (attr->form_is_section_offset ())
15776 {
15777 dwarf2_complex_location_expr_complaint ();
15778 }
15779 else
15780 {
15781 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15782 fieldname);
15783 }
15784 }
15785 else
15786 {
15787 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15788 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15789 {
15790 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15791 complaint (_("Member function \"%s\" (offset %s) is virtual "
15792 "but the vtable offset is not specified"),
15793 fieldname, sect_offset_str (die->sect_off));
15794 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15795 TYPE_CPLUS_DYNAMIC (type) = 1;
15796 }
15797 }
15798 }
15799
15800 /* Create the vector of member function fields, and attach it to the type. */
15801
15802 static void
15803 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15804 struct dwarf2_cu *cu)
15805 {
15806 if (cu->per_cu->lang == language_ada)
15807 error (_("unexpected member functions in Ada type"));
15808
15809 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15810 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15811 TYPE_ALLOC (type,
15812 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15813
15814 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15815 {
15816 struct fnfieldlist &nf = fip->fnfieldlists[i];
15817 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15818
15819 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15820 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15821 fn_flp->fn_fields = (struct fn_field *)
15822 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15823
15824 for (int k = 0; k < nf.fnfields.size (); ++k)
15825 fn_flp->fn_fields[k] = nf.fnfields[k];
15826 }
15827
15828 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15829 }
15830
15831 /* Returns non-zero if NAME is the name of a vtable member in CU's
15832 language, zero otherwise. */
15833 static int
15834 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15835 {
15836 static const char vptr[] = "_vptr";
15837
15838 /* Look for the C++ form of the vtable. */
15839 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15840 return 1;
15841
15842 return 0;
15843 }
15844
15845 /* GCC outputs unnamed structures that are really pointers to member
15846 functions, with the ABI-specified layout. If TYPE describes
15847 such a structure, smash it into a member function type.
15848
15849 GCC shouldn't do this; it should just output pointer to member DIEs.
15850 This is GCC PR debug/28767. */
15851
15852 static void
15853 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15854 {
15855 struct type *pfn_type, *self_type, *new_type;
15856
15857 /* Check for a structure with no name and two children. */
15858 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15859 return;
15860
15861 /* Check for __pfn and __delta members. */
15862 if (type->field (0).name () == NULL
15863 || strcmp (type->field (0).name (), "__pfn") != 0
15864 || type->field (1).name () == NULL
15865 || strcmp (type->field (1).name (), "__delta") != 0)
15866 return;
15867
15868 /* Find the type of the method. */
15869 pfn_type = type->field (0).type ();
15870 if (pfn_type == NULL
15871 || pfn_type->code () != TYPE_CODE_PTR
15872 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15873 return;
15874
15875 /* Look for the "this" argument. */
15876 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15877 if (pfn_type->num_fields () == 0
15878 /* || pfn_type->field (0).type () == NULL */
15879 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15880 return;
15881
15882 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15883 new_type = alloc_type (objfile);
15884 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15885 pfn_type->fields (), pfn_type->num_fields (),
15886 pfn_type->has_varargs ());
15887 smash_to_methodptr_type (type, new_type);
15888 }
15889
15890 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15891 requires rewriting, then copy it and return the updated copy.
15892 Otherwise return nullptr. */
15893
15894 static struct type *
15895 rewrite_array_type (struct type *type)
15896 {
15897 if (type->code () != TYPE_CODE_ARRAY)
15898 return nullptr;
15899
15900 struct type *index_type = type->index_type ();
15901 range_bounds *current_bounds = index_type->bounds ();
15902
15903 /* Handle multi-dimensional arrays. */
15904 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15905 if (new_target == nullptr)
15906 {
15907 /* Maybe we don't need to rewrite this array. */
15908 if (current_bounds->low.kind () == PROP_CONST
15909 && current_bounds->high.kind () == PROP_CONST)
15910 return nullptr;
15911 }
15912
15913 /* Either the target type was rewritten, or the bounds have to be
15914 updated. Either way we want to copy the type and update
15915 everything. */
15916 struct type *copy = copy_type (type);
15917 int nfields = copy->num_fields ();
15918 field *new_fields
15919 = ((struct field *) TYPE_ZALLOC (copy,
15920 nfields * sizeof (struct field)));
15921 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15922 copy->set_fields (new_fields);
15923 if (new_target != nullptr)
15924 TYPE_TARGET_TYPE (copy) = new_target;
15925
15926 struct type *index_copy = copy_type (index_type);
15927 range_bounds *bounds
15928 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15929 sizeof (range_bounds));
15930 *bounds = *current_bounds;
15931 bounds->low.set_const_val (1);
15932 bounds->high.set_const_val (0);
15933 index_copy->set_bounds (bounds);
15934 copy->set_index_type (index_copy);
15935
15936 return copy;
15937 }
15938
15939 /* While some versions of GCC will generate complicated DWARF for an
15940 array (see quirk_ada_thick_pointer), more recent versions were
15941 modified to emit an explicit thick pointer structure. However, in
15942 this case, the array still has DWARF expressions for its ranges,
15943 and these must be ignored. */
15944
15945 static void
15946 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15947 struct type *type)
15948 {
15949 gdb_assert (cu->per_cu->lang == language_ada);
15950
15951 /* Check for a structure with two children. */
15952 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15953 return;
15954
15955 /* Check for P_ARRAY and P_BOUNDS members. */
15956 if (type->field (0).name () == NULL
15957 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15958 || type->field (1).name () == NULL
15959 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15960 return;
15961
15962 /* Make sure we're looking at a pointer to an array. */
15963 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15964 return;
15965
15966 /* The Ada code already knows how to handle these types, so all that
15967 we need to do is turn the bounds into static bounds. However, we
15968 don't want to rewrite existing array or index types in-place,
15969 because those may be referenced in other contexts where this
15970 rewriting is undesirable. */
15971 struct type *new_ary_type
15972 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15973 if (new_ary_type != nullptr)
15974 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15975 }
15976
15977 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15978 appropriate error checking and issuing complaints if there is a
15979 problem. */
15980
15981 static ULONGEST
15982 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15983 {
15984 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15985
15986 if (attr == nullptr)
15987 return 0;
15988
15989 if (!attr->form_is_constant ())
15990 {
15991 complaint (_("DW_AT_alignment must have constant form"
15992 " - DIE at %s [in module %s]"),
15993 sect_offset_str (die->sect_off),
15994 objfile_name (cu->per_objfile->objfile));
15995 return 0;
15996 }
15997
15998 LONGEST val = attr->constant_value (0);
15999 if (val < 0)
16000 {
16001 complaint (_("DW_AT_alignment value must not be negative"
16002 " - DIE at %s [in module %s]"),
16003 sect_offset_str (die->sect_off),
16004 objfile_name (cu->per_objfile->objfile));
16005 return 0;
16006 }
16007 ULONGEST align = val;
16008
16009 if (align == 0)
16010 {
16011 complaint (_("DW_AT_alignment value must not be zero"
16012 " - DIE at %s [in module %s]"),
16013 sect_offset_str (die->sect_off),
16014 objfile_name (cu->per_objfile->objfile));
16015 return 0;
16016 }
16017 if ((align & (align - 1)) != 0)
16018 {
16019 complaint (_("DW_AT_alignment value must be a power of 2"
16020 " - DIE at %s [in module %s]"),
16021 sect_offset_str (die->sect_off),
16022 objfile_name (cu->per_objfile->objfile));
16023 return 0;
16024 }
16025
16026 return align;
16027 }
16028
16029 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16030 the alignment for TYPE. */
16031
16032 static void
16033 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16034 struct type *type)
16035 {
16036 if (!set_type_align (type, get_alignment (cu, die)))
16037 complaint (_("DW_AT_alignment value too large"
16038 " - DIE at %s [in module %s]"),
16039 sect_offset_str (die->sect_off),
16040 objfile_name (cu->per_objfile->objfile));
16041 }
16042
16043 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16044 constant for a type, according to DWARF5 spec, Table 5.5. */
16045
16046 static bool
16047 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16048 {
16049 switch (value)
16050 {
16051 case DW_CC_normal:
16052 case DW_CC_pass_by_reference:
16053 case DW_CC_pass_by_value:
16054 return true;
16055
16056 default:
16057 complaint (_("unrecognized DW_AT_calling_convention value "
16058 "(%s) for a type"), pulongest (value));
16059 return false;
16060 }
16061 }
16062
16063 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16064 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16065 also according to GNU-specific values (see include/dwarf2.h). */
16066
16067 static bool
16068 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16069 {
16070 switch (value)
16071 {
16072 case DW_CC_normal:
16073 case DW_CC_program:
16074 case DW_CC_nocall:
16075 return true;
16076
16077 case DW_CC_GNU_renesas_sh:
16078 case DW_CC_GNU_borland_fastcall_i386:
16079 case DW_CC_GDB_IBM_OpenCL:
16080 return true;
16081
16082 default:
16083 complaint (_("unrecognized DW_AT_calling_convention value "
16084 "(%s) for a subroutine"), pulongest (value));
16085 return false;
16086 }
16087 }
16088
16089 /* Called when we find the DIE that starts a structure or union scope
16090 (definition) to create a type for the structure or union. Fill in
16091 the type's name and general properties; the members will not be
16092 processed until process_structure_scope. A symbol table entry for
16093 the type will also not be done until process_structure_scope (assuming
16094 the type has a name).
16095
16096 NOTE: we need to call these functions regardless of whether or not the
16097 DIE has a DW_AT_name attribute, since it might be an anonymous
16098 structure or union. This gets the type entered into our set of
16099 user defined types. */
16100
16101 static struct type *
16102 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16103 {
16104 struct objfile *objfile = cu->per_objfile->objfile;
16105 struct type *type;
16106 struct attribute *attr;
16107 const char *name;
16108
16109 /* If the definition of this type lives in .debug_types, read that type.
16110 Don't follow DW_AT_specification though, that will take us back up
16111 the chain and we want to go down. */
16112 attr = die->attr (DW_AT_signature);
16113 if (attr != nullptr)
16114 {
16115 type = get_DW_AT_signature_type (die, attr, cu);
16116
16117 /* The type's CU may not be the same as CU.
16118 Ensure TYPE is recorded with CU in die_type_hash. */
16119 return set_die_type (die, type, cu);
16120 }
16121
16122 type = alloc_type (objfile);
16123 INIT_CPLUS_SPECIFIC (type);
16124
16125 name = dwarf2_name (die, cu);
16126 if (name != NULL)
16127 {
16128 if (cu->per_cu->lang == language_cplus
16129 || cu->per_cu->lang == language_d
16130 || cu->per_cu->lang == language_rust)
16131 {
16132 const char *full_name = dwarf2_full_name (name, die, cu);
16133
16134 /* dwarf2_full_name might have already finished building the DIE's
16135 type. If so, there is no need to continue. */
16136 if (get_die_type (die, cu) != NULL)
16137 return get_die_type (die, cu);
16138
16139 type->set_name (full_name);
16140 }
16141 else
16142 {
16143 /* The name is already allocated along with this objfile, so
16144 we don't need to duplicate it for the type. */
16145 type->set_name (name);
16146 }
16147 }
16148
16149 if (die->tag == DW_TAG_structure_type)
16150 {
16151 type->set_code (TYPE_CODE_STRUCT);
16152 }
16153 else if (die->tag == DW_TAG_union_type)
16154 {
16155 type->set_code (TYPE_CODE_UNION);
16156 }
16157 else if (die->tag == DW_TAG_namelist)
16158 {
16159 type->set_code (TYPE_CODE_NAMELIST);
16160 }
16161 else
16162 {
16163 type->set_code (TYPE_CODE_STRUCT);
16164 }
16165
16166 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
16167 type->set_is_declared_class (true);
16168
16169 /* Store the calling convention in the type if it's available in
16170 the die. Otherwise the calling convention remains set to
16171 the default value DW_CC_normal. */
16172 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16173 if (attr != nullptr
16174 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16175 {
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_CPLUS_CALLING_CONVENTION (type)
16178 = (enum dwarf_calling_convention) (attr->constant_value (0));
16179 }
16180
16181 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16182 if (attr != nullptr)
16183 {
16184 if (attr->form_is_constant ())
16185 TYPE_LENGTH (type) = attr->constant_value (0);
16186 else
16187 {
16188 struct dynamic_prop prop;
16189 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16190 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16191 TYPE_LENGTH (type) = 0;
16192 }
16193 }
16194 else
16195 {
16196 TYPE_LENGTH (type) = 0;
16197 }
16198
16199 maybe_set_alignment (cu, die, type);
16200
16201 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16202 {
16203 /* ICC<14 does not output the required DW_AT_declaration on
16204 incomplete types, but gives them a size of zero. */
16205 type->set_is_stub (true);
16206 }
16207 else
16208 type->set_stub_is_supported (true);
16209
16210 if (die_is_declaration (die, cu))
16211 type->set_is_stub (true);
16212 else if (attr == NULL && die->child == NULL
16213 && producer_is_realview (cu->producer))
16214 /* RealView does not output the required DW_AT_declaration
16215 on incomplete types. */
16216 type->set_is_stub (true);
16217
16218 /* We need to add the type field to the die immediately so we don't
16219 infinitely recurse when dealing with pointers to the structure
16220 type within the structure itself. */
16221 set_die_type (die, type, cu);
16222
16223 /* set_die_type should be already done. */
16224 set_descriptive_type (type, die, cu);
16225
16226 return type;
16227 }
16228
16229 static void handle_struct_member_die
16230 (struct die_info *child_die,
16231 struct type *type,
16232 struct field_info *fi,
16233 std::vector<struct symbol *> *template_args,
16234 struct dwarf2_cu *cu);
16235
16236 /* A helper for handle_struct_member_die that handles
16237 DW_TAG_variant_part. */
16238
16239 static void
16240 handle_variant_part (struct die_info *die, struct type *type,
16241 struct field_info *fi,
16242 std::vector<struct symbol *> *template_args,
16243 struct dwarf2_cu *cu)
16244 {
16245 variant_part_builder *new_part;
16246 if (fi->current_variant_part == nullptr)
16247 {
16248 fi->variant_parts.emplace_back ();
16249 new_part = &fi->variant_parts.back ();
16250 }
16251 else if (!fi->current_variant_part->processing_variant)
16252 {
16253 complaint (_("nested DW_TAG_variant_part seen "
16254 "- DIE at %s [in module %s]"),
16255 sect_offset_str (die->sect_off),
16256 objfile_name (cu->per_objfile->objfile));
16257 return;
16258 }
16259 else
16260 {
16261 variant_field &current = fi->current_variant_part->variants.back ();
16262 current.variant_parts.emplace_back ();
16263 new_part = &current.variant_parts.back ();
16264 }
16265
16266 /* When we recurse, we want callees to add to this new variant
16267 part. */
16268 scoped_restore save_current_variant_part
16269 = make_scoped_restore (&fi->current_variant_part, new_part);
16270
16271 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16272 if (discr == NULL)
16273 {
16274 /* It's a univariant form, an extension we support. */
16275 }
16276 else if (discr->form_is_ref ())
16277 {
16278 struct dwarf2_cu *target_cu = cu;
16279 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16280
16281 new_part->discriminant_offset = target_die->sect_off;
16282 }
16283 else
16284 {
16285 complaint (_("DW_AT_discr does not have DIE reference form"
16286 " - DIE at %s [in module %s]"),
16287 sect_offset_str (die->sect_off),
16288 objfile_name (cu->per_objfile->objfile));
16289 }
16290
16291 for (die_info *child_die = die->child;
16292 child_die != NULL;
16293 child_die = child_die->sibling)
16294 handle_struct_member_die (child_die, type, fi, template_args, cu);
16295 }
16296
16297 /* A helper for handle_struct_member_die that handles
16298 DW_TAG_variant. */
16299
16300 static void
16301 handle_variant (struct die_info *die, struct type *type,
16302 struct field_info *fi,
16303 std::vector<struct symbol *> *template_args,
16304 struct dwarf2_cu *cu)
16305 {
16306 if (fi->current_variant_part == nullptr)
16307 {
16308 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16309 "- DIE at %s [in module %s]"),
16310 sect_offset_str (die->sect_off),
16311 objfile_name (cu->per_objfile->objfile));
16312 return;
16313 }
16314 if (fi->current_variant_part->processing_variant)
16315 {
16316 complaint (_("nested DW_TAG_variant seen "
16317 "- DIE at %s [in module %s]"),
16318 sect_offset_str (die->sect_off),
16319 objfile_name (cu->per_objfile->objfile));
16320 return;
16321 }
16322
16323 scoped_restore save_processing_variant
16324 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16325 true);
16326
16327 fi->current_variant_part->variants.emplace_back ();
16328 variant_field &variant = fi->current_variant_part->variants.back ();
16329 variant.first_field = fi->fields.size ();
16330
16331 /* In a variant we want to get the discriminant and also add a
16332 field for our sole member child. */
16333 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16334 if (discr == nullptr || !discr->form_is_constant ())
16335 {
16336 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16337 if (discr == nullptr || discr->as_block ()->size == 0)
16338 variant.default_branch = true;
16339 else
16340 variant.discr_list_data = discr->as_block ();
16341 }
16342 else
16343 variant.discriminant_value = discr->constant_value (0);
16344
16345 for (die_info *variant_child = die->child;
16346 variant_child != NULL;
16347 variant_child = variant_child->sibling)
16348 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16349
16350 variant.last_field = fi->fields.size ();
16351 }
16352
16353 /* A helper for process_structure_scope that handles a single member
16354 DIE. */
16355
16356 static void
16357 handle_struct_member_die (struct die_info *child_die, struct type *type,
16358 struct field_info *fi,
16359 std::vector<struct symbol *> *template_args,
16360 struct dwarf2_cu *cu)
16361 {
16362 if (child_die->tag == DW_TAG_member
16363 || child_die->tag == DW_TAG_variable
16364 || child_die->tag == DW_TAG_namelist_item)
16365 {
16366 /* NOTE: carlton/2002-11-05: A C++ static data member
16367 should be a DW_TAG_member that is a declaration, but
16368 all versions of G++ as of this writing (so through at
16369 least 3.2.1) incorrectly generate DW_TAG_variable
16370 tags for them instead. */
16371 dwarf2_add_field (fi, child_die, cu);
16372 }
16373 else if (child_die->tag == DW_TAG_subprogram)
16374 {
16375 /* Rust doesn't have member functions in the C++ sense.
16376 However, it does emit ordinary functions as children
16377 of a struct DIE. */
16378 if (cu->per_cu->lang == language_rust)
16379 read_func_scope (child_die, cu);
16380 else
16381 {
16382 /* C++ member function. */
16383 dwarf2_add_member_fn (fi, child_die, type, cu);
16384 }
16385 }
16386 else if (child_die->tag == DW_TAG_inheritance)
16387 {
16388 /* C++ base class field. */
16389 dwarf2_add_field (fi, child_die, cu);
16390 }
16391 else if (type_can_define_types (child_die))
16392 dwarf2_add_type_defn (fi, child_die, cu);
16393 else if (child_die->tag == DW_TAG_template_type_param
16394 || child_die->tag == DW_TAG_template_value_param)
16395 {
16396 struct symbol *arg = new_symbol (child_die, NULL, cu);
16397
16398 if (arg != NULL)
16399 template_args->push_back (arg);
16400 }
16401 else if (child_die->tag == DW_TAG_variant_part)
16402 handle_variant_part (child_die, type, fi, template_args, cu);
16403 else if (child_die->tag == DW_TAG_variant)
16404 handle_variant (child_die, type, fi, template_args, cu);
16405 }
16406
16407 /* Finish creating a structure or union type, including filling in its
16408 members and creating a symbol for it. This function also handles Fortran
16409 namelist variables, their items or members and creating a symbol for
16410 them. */
16411
16412 static void
16413 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16414 {
16415 struct objfile *objfile = cu->per_objfile->objfile;
16416 struct die_info *child_die;
16417 struct type *type;
16418
16419 type = get_die_type (die, cu);
16420 if (type == NULL)
16421 type = read_structure_type (die, cu);
16422
16423 bool has_template_parameters = false;
16424 if (die->child != NULL && ! die_is_declaration (die, cu))
16425 {
16426 struct field_info fi;
16427 std::vector<struct symbol *> template_args;
16428
16429 child_die = die->child;
16430
16431 while (child_die && child_die->tag)
16432 {
16433 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16434 child_die = child_die->sibling;
16435 }
16436
16437 /* Attach template arguments to type. */
16438 if (!template_args.empty ())
16439 {
16440 has_template_parameters = true;
16441 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16442 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16443 TYPE_TEMPLATE_ARGUMENTS (type)
16444 = XOBNEWVEC (&objfile->objfile_obstack,
16445 struct symbol *,
16446 TYPE_N_TEMPLATE_ARGUMENTS (type));
16447 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16448 template_args.data (),
16449 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16450 * sizeof (struct symbol *)));
16451 }
16452
16453 /* Attach fields and member functions to the type. */
16454 if (fi.nfields () > 0)
16455 dwarf2_attach_fields_to_type (&fi, type, cu);
16456 if (!fi.fnfieldlists.empty ())
16457 {
16458 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16459
16460 /* Get the type which refers to the base class (possibly this
16461 class itself) which contains the vtable pointer for the current
16462 class from the DW_AT_containing_type attribute. This use of
16463 DW_AT_containing_type is a GNU extension. */
16464
16465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16466 {
16467 struct type *t = die_containing_type (die, cu);
16468
16469 set_type_vptr_basetype (type, t);
16470 if (type == t)
16471 {
16472 int i;
16473
16474 /* Our own class provides vtbl ptr. */
16475 for (i = t->num_fields () - 1;
16476 i >= TYPE_N_BASECLASSES (t);
16477 --i)
16478 {
16479 const char *fieldname = t->field (i).name ();
16480
16481 if (is_vtable_name (fieldname, cu))
16482 {
16483 set_type_vptr_fieldno (type, i);
16484 break;
16485 }
16486 }
16487
16488 /* Complain if virtual function table field not found. */
16489 if (i < TYPE_N_BASECLASSES (t))
16490 complaint (_("virtual function table pointer "
16491 "not found when defining class '%s'"),
16492 type->name () ? type->name () : "");
16493 }
16494 else
16495 {
16496 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16497 }
16498 }
16499 else if (cu->producer
16500 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16501 {
16502 /* The IBM XLC compiler does not provide direct indication
16503 of the containing type, but the vtable pointer is
16504 always named __vfp. */
16505
16506 int i;
16507
16508 for (i = type->num_fields () - 1;
16509 i >= TYPE_N_BASECLASSES (type);
16510 --i)
16511 {
16512 if (strcmp (type->field (i).name (), "__vfp") == 0)
16513 {
16514 set_type_vptr_fieldno (type, i);
16515 set_type_vptr_basetype (type, type);
16516 break;
16517 }
16518 }
16519 }
16520 }
16521
16522 /* Copy fi.typedef_field_list linked list elements content into the
16523 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16524 if (!fi.typedef_field_list.empty ())
16525 {
16526 int count = fi.typedef_field_list.size ();
16527
16528 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16529 TYPE_TYPEDEF_FIELD_ARRAY (type)
16530 = ((struct decl_field *)
16531 TYPE_ALLOC (type,
16532 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16533 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16534
16535 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16536 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16537 }
16538
16539 /* Copy fi.nested_types_list linked list elements content into the
16540 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16541 if (!fi.nested_types_list.empty ()
16542 && cu->per_cu->lang != language_ada)
16543 {
16544 int count = fi.nested_types_list.size ();
16545
16546 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16547 TYPE_NESTED_TYPES_ARRAY (type)
16548 = ((struct decl_field *)
16549 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16550 TYPE_NESTED_TYPES_COUNT (type) = count;
16551
16552 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16553 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16554 }
16555 }
16556
16557 quirk_gcc_member_function_pointer (type, objfile);
16558 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16559 cu->rust_unions.push_back (type);
16560 else if (cu->per_cu->lang == language_ada)
16561 quirk_ada_thick_pointer_struct (die, cu, type);
16562
16563 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16564 snapshots) has been known to create a die giving a declaration
16565 for a class that has, as a child, a die giving a definition for a
16566 nested class. So we have to process our children even if the
16567 current die is a declaration. Normally, of course, a declaration
16568 won't have any children at all. */
16569
16570 child_die = die->child;
16571
16572 while (child_die != NULL && child_die->tag)
16573 {
16574 if (child_die->tag == DW_TAG_member
16575 || child_die->tag == DW_TAG_variable
16576 || child_die->tag == DW_TAG_inheritance
16577 || child_die->tag == DW_TAG_template_value_param
16578 || child_die->tag == DW_TAG_template_type_param)
16579 {
16580 /* Do nothing. */
16581 }
16582 else
16583 process_die (child_die, cu);
16584
16585 child_die = child_die->sibling;
16586 }
16587
16588 /* Do not consider external references. According to the DWARF standard,
16589 these DIEs are identified by the fact that they have no byte_size
16590 attribute, and a declaration attribute. */
16591 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16592 || !die_is_declaration (die, cu)
16593 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16594 {
16595 struct symbol *sym = new_symbol (die, type, cu);
16596
16597 if (has_template_parameters)
16598 {
16599 struct symtab *symtab;
16600 if (sym != nullptr)
16601 symtab = symbol_symtab (sym);
16602 else if (cu->line_header != nullptr)
16603 {
16604 /* Any related symtab will do. */
16605 symtab
16606 = cu->line_header->file_names ()[0].symtab;
16607 }
16608 else
16609 {
16610 symtab = nullptr;
16611 complaint (_("could not find suitable "
16612 "symtab for template parameter"
16613 " - DIE at %s [in module %s]"),
16614 sect_offset_str (die->sect_off),
16615 objfile_name (objfile));
16616 }
16617
16618 if (symtab != nullptr)
16619 {
16620 /* Make sure that the symtab is set on the new symbols.
16621 Even though they don't appear in this symtab directly,
16622 other parts of gdb assume that symbols do, and this is
16623 reasonably true. */
16624 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16625 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16626 }
16627 }
16628 }
16629 }
16630
16631 /* Assuming DIE is an enumeration type, and TYPE is its associated
16632 type, update TYPE using some information only available in DIE's
16633 children. In particular, the fields are computed. */
16634
16635 static void
16636 update_enumeration_type_from_children (struct die_info *die,
16637 struct type *type,
16638 struct dwarf2_cu *cu)
16639 {
16640 struct die_info *child_die;
16641 int unsigned_enum = 1;
16642 int flag_enum = 1;
16643
16644 auto_obstack obstack;
16645 std::vector<struct field> fields;
16646
16647 for (child_die = die->child;
16648 child_die != NULL && child_die->tag;
16649 child_die = child_die->sibling)
16650 {
16651 struct attribute *attr;
16652 LONGEST value;
16653 const gdb_byte *bytes;
16654 struct dwarf2_locexpr_baton *baton;
16655 const char *name;
16656
16657 if (child_die->tag != DW_TAG_enumerator)
16658 continue;
16659
16660 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16661 if (attr == NULL)
16662 continue;
16663
16664 name = dwarf2_name (child_die, cu);
16665 if (name == NULL)
16666 name = "<anonymous enumerator>";
16667
16668 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16669 &value, &bytes, &baton);
16670 if (value < 0)
16671 {
16672 unsigned_enum = 0;
16673 flag_enum = 0;
16674 }
16675 else
16676 {
16677 if (count_one_bits_ll (value) >= 2)
16678 flag_enum = 0;
16679 }
16680
16681 fields.emplace_back ();
16682 struct field &field = fields.back ();
16683 field.set_name (dwarf2_physname (name, child_die, cu));
16684 field.set_loc_enumval (value);
16685 }
16686
16687 if (!fields.empty ())
16688 {
16689 type->set_num_fields (fields.size ());
16690 type->set_fields
16691 ((struct field *)
16692 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16693 memcpy (type->fields (), fields.data (),
16694 sizeof (struct field) * fields.size ());
16695 }
16696
16697 if (unsigned_enum)
16698 type->set_is_unsigned (true);
16699
16700 if (flag_enum)
16701 type->set_is_flag_enum (true);
16702 }
16703
16704 /* Given a DW_AT_enumeration_type die, set its type. We do not
16705 complete the type's fields yet, or create any symbols. */
16706
16707 static struct type *
16708 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16709 {
16710 struct objfile *objfile = cu->per_objfile->objfile;
16711 struct type *type;
16712 struct attribute *attr;
16713 const char *name;
16714
16715 /* If the definition of this type lives in .debug_types, read that type.
16716 Don't follow DW_AT_specification though, that will take us back up
16717 the chain and we want to go down. */
16718 attr = die->attr (DW_AT_signature);
16719 if (attr != nullptr)
16720 {
16721 type = get_DW_AT_signature_type (die, attr, cu);
16722
16723 /* The type's CU may not be the same as CU.
16724 Ensure TYPE is recorded with CU in die_type_hash. */
16725 return set_die_type (die, type, cu);
16726 }
16727
16728 type = alloc_type (objfile);
16729
16730 type->set_code (TYPE_CODE_ENUM);
16731 name = dwarf2_full_name (NULL, die, cu);
16732 if (name != NULL)
16733 type->set_name (name);
16734
16735 attr = dwarf2_attr (die, DW_AT_type, cu);
16736 if (attr != NULL)
16737 {
16738 struct type *underlying_type = die_type (die, cu);
16739
16740 TYPE_TARGET_TYPE (type) = underlying_type;
16741 }
16742
16743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16744 if (attr != nullptr)
16745 {
16746 TYPE_LENGTH (type) = attr->constant_value (0);
16747 }
16748 else
16749 {
16750 TYPE_LENGTH (type) = 0;
16751 }
16752
16753 maybe_set_alignment (cu, die, type);
16754
16755 /* The enumeration DIE can be incomplete. In Ada, any type can be
16756 declared as private in the package spec, and then defined only
16757 inside the package body. Such types are known as Taft Amendment
16758 Types. When another package uses such a type, an incomplete DIE
16759 may be generated by the compiler. */
16760 if (die_is_declaration (die, cu))
16761 type->set_is_stub (true);
16762
16763 /* If this type has an underlying type that is not a stub, then we
16764 may use its attributes. We always use the "unsigned" attribute
16765 in this situation, because ordinarily we guess whether the type
16766 is unsigned -- but the guess can be wrong and the underlying type
16767 can tell us the reality. However, we defer to a local size
16768 attribute if one exists, because this lets the compiler override
16769 the underlying type if needed. */
16770 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16771 {
16772 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16773 underlying_type = check_typedef (underlying_type);
16774
16775 type->set_is_unsigned (underlying_type->is_unsigned ());
16776
16777 if (TYPE_LENGTH (type) == 0)
16778 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16779
16780 if (TYPE_RAW_ALIGN (type) == 0
16781 && TYPE_RAW_ALIGN (underlying_type) != 0)
16782 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16783 }
16784
16785 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16786
16787 set_die_type (die, type, cu);
16788
16789 /* Finish the creation of this type by using the enum's children.
16790 Note that, as usual, this must come after set_die_type to avoid
16791 infinite recursion when trying to compute the names of the
16792 enumerators. */
16793 update_enumeration_type_from_children (die, type, cu);
16794
16795 return type;
16796 }
16797
16798 /* Given a pointer to a die which begins an enumeration, process all
16799 the dies that define the members of the enumeration, and create the
16800 symbol for the enumeration type.
16801
16802 NOTE: We reverse the order of the element list. */
16803
16804 static void
16805 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16806 {
16807 struct type *this_type;
16808
16809 this_type = get_die_type (die, cu);
16810 if (this_type == NULL)
16811 this_type = read_enumeration_type (die, cu);
16812
16813 if (die->child != NULL)
16814 {
16815 struct die_info *child_die;
16816 const char *name;
16817
16818 child_die = die->child;
16819 while (child_die && child_die->tag)
16820 {
16821 if (child_die->tag != DW_TAG_enumerator)
16822 {
16823 process_die (child_die, cu);
16824 }
16825 else
16826 {
16827 name = dwarf2_name (child_die, cu);
16828 if (name)
16829 new_symbol (child_die, this_type, cu);
16830 }
16831
16832 child_die = child_die->sibling;
16833 }
16834 }
16835
16836 /* If we are reading an enum from a .debug_types unit, and the enum
16837 is a declaration, and the enum is not the signatured type in the
16838 unit, then we do not want to add a symbol for it. Adding a
16839 symbol would in some cases obscure the true definition of the
16840 enum, giving users an incomplete type when the definition is
16841 actually available. Note that we do not want to do this for all
16842 enums which are just declarations, because C++0x allows forward
16843 enum declarations. */
16844 if (cu->per_cu->is_debug_types
16845 && die_is_declaration (die, cu))
16846 {
16847 struct signatured_type *sig_type;
16848
16849 sig_type = (struct signatured_type *) cu->per_cu;
16850 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16851 if (sig_type->type_offset_in_section != die->sect_off)
16852 return;
16853 }
16854
16855 new_symbol (die, this_type, cu);
16856 }
16857
16858 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16859 expression for an index type and finds the corresponding field
16860 offset in the hidden "P_BOUNDS" structure. Returns true on success
16861 and updates *FIELD, false if it fails to recognize an
16862 expression. */
16863
16864 static bool
16865 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16866 int *bounds_offset, struct field *field,
16867 struct dwarf2_cu *cu)
16868 {
16869 struct attribute *attr = dwarf2_attr (die, name, cu);
16870 if (attr == nullptr || !attr->form_is_block ())
16871 return false;
16872
16873 const struct dwarf_block *block = attr->as_block ();
16874 const gdb_byte *start = block->data;
16875 const gdb_byte *end = block->data + block->size;
16876
16877 /* The expression to recognize generally looks like:
16878
16879 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16880 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16881
16882 However, the second "plus_uconst" may be missing:
16883
16884 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16885 DW_OP_deref_size: 4)
16886
16887 This happens when the field is at the start of the structure.
16888
16889 Also, the final deref may not be sized:
16890
16891 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16892 DW_OP_deref)
16893
16894 This happens when the size of the index type happens to be the
16895 same as the architecture's word size. This can occur with or
16896 without the second plus_uconst. */
16897
16898 if (end - start < 2)
16899 return false;
16900 if (*start++ != DW_OP_push_object_address)
16901 return false;
16902 if (*start++ != DW_OP_plus_uconst)
16903 return false;
16904
16905 uint64_t this_bound_off;
16906 start = gdb_read_uleb128 (start, end, &this_bound_off);
16907 if (start == nullptr || (int) this_bound_off != this_bound_off)
16908 return false;
16909 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16910 is consistent among all bounds. */
16911 if (*bounds_offset == -1)
16912 *bounds_offset = this_bound_off;
16913 else if (*bounds_offset != this_bound_off)
16914 return false;
16915
16916 if (start == end || *start++ != DW_OP_deref)
16917 return false;
16918
16919 int offset = 0;
16920 if (start ==end)
16921 return false;
16922 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16923 {
16924 /* This means an offset of 0. */
16925 }
16926 else if (*start++ != DW_OP_plus_uconst)
16927 return false;
16928 else
16929 {
16930 /* The size is the parameter to DW_OP_plus_uconst. */
16931 uint64_t val;
16932 start = gdb_read_uleb128 (start, end, &val);
16933 if (start == nullptr)
16934 return false;
16935 if ((int) val != val)
16936 return false;
16937 offset = val;
16938 }
16939
16940 if (start == end)
16941 return false;
16942
16943 uint64_t size;
16944 if (*start == DW_OP_deref_size)
16945 {
16946 start = gdb_read_uleb128 (start + 1, end, &size);
16947 if (start == nullptr)
16948 return false;
16949 }
16950 else if (*start == DW_OP_deref)
16951 {
16952 size = cu->header.addr_size;
16953 ++start;
16954 }
16955 else
16956 return false;
16957
16958 field->set_loc_bitpos (8 * offset);
16959 if (size != TYPE_LENGTH (field->type ()))
16960 FIELD_BITSIZE (*field) = 8 * size;
16961
16962 return true;
16963 }
16964
16965 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16966 some kinds of Ada arrays:
16967
16968 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16969 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16970 <11e0> DW_AT_data_location: 2 byte block: 97 6
16971 (DW_OP_push_object_address; DW_OP_deref)
16972 <11e3> DW_AT_type : <0x1173>
16973 <11e7> DW_AT_sibling : <0x1201>
16974 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16975 <11ec> DW_AT_type : <0x1206>
16976 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16977 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16978 DW_OP_deref_size: 4)
16979 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16980 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16981 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16982
16983 This actually represents a "thick pointer", which is a structure
16984 with two elements: one that is a pointer to the array data, and one
16985 that is a pointer to another structure; this second structure holds
16986 the array bounds.
16987
16988 This returns a new type on success, or nullptr if this didn't
16989 recognize the type. */
16990
16991 static struct type *
16992 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16993 struct type *type)
16994 {
16995 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16996 /* So far we've only seen this with block form. */
16997 if (attr == nullptr || !attr->form_is_block ())
16998 return nullptr;
16999
17000 /* Note that this will fail if the structure layout is changed by
17001 the compiler. However, we have no good way to recognize some
17002 other layout, because we don't know what expression the compiler
17003 might choose to emit should this happen. */
17004 struct dwarf_block *blk = attr->as_block ();
17005 if (blk->size != 2
17006 || blk->data[0] != DW_OP_push_object_address
17007 || blk->data[1] != DW_OP_deref)
17008 return nullptr;
17009
17010 int bounds_offset = -1;
17011 int max_align = -1;
17012 std::vector<struct field> range_fields;
17013 for (struct die_info *child_die = die->child;
17014 child_die;
17015 child_die = child_die->sibling)
17016 {
17017 if (child_die->tag == DW_TAG_subrange_type)
17018 {
17019 struct type *underlying = read_subrange_index_type (child_die, cu);
17020
17021 int this_align = type_align (underlying);
17022 if (this_align > max_align)
17023 max_align = this_align;
17024
17025 range_fields.emplace_back ();
17026 range_fields.emplace_back ();
17027
17028 struct field &lower = range_fields[range_fields.size () - 2];
17029 struct field &upper = range_fields[range_fields.size () - 1];
17030
17031 lower.set_type (underlying);
17032 FIELD_ARTIFICIAL (lower) = 1;
17033
17034 upper.set_type (underlying);
17035 FIELD_ARTIFICIAL (upper) = 1;
17036
17037 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17038 &bounds_offset, &lower, cu)
17039 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17040 &bounds_offset, &upper, cu))
17041 return nullptr;
17042 }
17043 }
17044
17045 /* This shouldn't really happen, but double-check that we found
17046 where the bounds are stored. */
17047 if (bounds_offset == -1)
17048 return nullptr;
17049
17050 struct objfile *objfile = cu->per_objfile->objfile;
17051 for (int i = 0; i < range_fields.size (); i += 2)
17052 {
17053 char name[20];
17054
17055 /* Set the name of each field in the bounds. */
17056 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17057 range_fields[i].set_name (objfile->intern (name));
17058 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17059 range_fields[i + 1].set_name (objfile->intern (name));
17060 }
17061
17062 struct type *bounds = alloc_type (objfile);
17063 bounds->set_code (TYPE_CODE_STRUCT);
17064
17065 bounds->set_num_fields (range_fields.size ());
17066 bounds->set_fields
17067 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17068 * sizeof (struct field))));
17069 memcpy (bounds->fields (), range_fields.data (),
17070 bounds->num_fields () * sizeof (struct field));
17071
17072 int last_fieldno = range_fields.size () - 1;
17073 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
17074 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17075 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17076
17077 /* Rewrite the existing array type in place. Specifically, we
17078 remove any dynamic properties we might have read, and we replace
17079 the index types. */
17080 struct type *iter = type;
17081 for (int i = 0; i < range_fields.size (); i += 2)
17082 {
17083 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17084 iter->main_type->dyn_prop_list = nullptr;
17085 iter->set_index_type
17086 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17087 iter = TYPE_TARGET_TYPE (iter);
17088 }
17089
17090 struct type *result = alloc_type (objfile);
17091 result->set_code (TYPE_CODE_STRUCT);
17092
17093 result->set_num_fields (2);
17094 result->set_fields
17095 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17096 * sizeof (struct field))));
17097
17098 /* The names are chosen to coincide with what the compiler does with
17099 -fgnat-encodings=all, which the Ada code in gdb already
17100 understands. */
17101 result->field (0).set_name ("P_ARRAY");
17102 result->field (0).set_type (lookup_pointer_type (type));
17103
17104 result->field (1).set_name ("P_BOUNDS");
17105 result->field (1).set_type (lookup_pointer_type (bounds));
17106 result->field (1).set_loc_bitpos (8 * bounds_offset);
17107
17108 result->set_name (type->name ());
17109 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17110 + TYPE_LENGTH (result->field (1).type ()));
17111
17112 return result;
17113 }
17114
17115 /* Extract all information from a DW_TAG_array_type DIE and put it in
17116 the DIE's type field. For now, this only handles one dimensional
17117 arrays. */
17118
17119 static struct type *
17120 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17121 {
17122 struct objfile *objfile = cu->per_objfile->objfile;
17123 struct die_info *child_die;
17124 struct type *type;
17125 struct type *element_type, *range_type, *index_type;
17126 struct attribute *attr;
17127 const char *name;
17128 struct dynamic_prop *byte_stride_prop = NULL;
17129 unsigned int bit_stride = 0;
17130
17131 element_type = die_type (die, cu);
17132
17133 /* The die_type call above may have already set the type for this DIE. */
17134 type = get_die_type (die, cu);
17135 if (type)
17136 return type;
17137
17138 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17139 if (attr != NULL)
17140 {
17141 int stride_ok;
17142 struct type *prop_type = cu->addr_sized_int_type (false);
17143
17144 byte_stride_prop
17145 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17146 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17147 prop_type);
17148 if (!stride_ok)
17149 {
17150 complaint (_("unable to read array DW_AT_byte_stride "
17151 " - DIE at %s [in module %s]"),
17152 sect_offset_str (die->sect_off),
17153 objfile_name (cu->per_objfile->objfile));
17154 /* Ignore this attribute. We will likely not be able to print
17155 arrays of this type correctly, but there is little we can do
17156 to help if we cannot read the attribute's value. */
17157 byte_stride_prop = NULL;
17158 }
17159 }
17160
17161 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17162 if (attr != NULL)
17163 bit_stride = attr->constant_value (0);
17164
17165 /* Irix 6.2 native cc creates array types without children for
17166 arrays with unspecified length. */
17167 if (die->child == NULL)
17168 {
17169 index_type = objfile_type (objfile)->builtin_int;
17170 range_type = create_static_range_type (NULL, index_type, 0, -1);
17171 type = create_array_type_with_stride (NULL, element_type, range_type,
17172 byte_stride_prop, bit_stride);
17173 return set_die_type (die, type, cu);
17174 }
17175
17176 std::vector<struct type *> range_types;
17177 child_die = die->child;
17178 while (child_die && child_die->tag)
17179 {
17180 if (child_die->tag == DW_TAG_subrange_type
17181 || child_die->tag == DW_TAG_generic_subrange)
17182 {
17183 struct type *child_type = read_type_die (child_die, cu);
17184
17185 if (child_type != NULL)
17186 {
17187 /* The range type was succesfully read. Save it for the
17188 array type creation. */
17189 range_types.push_back (child_type);
17190 }
17191 }
17192 child_die = child_die->sibling;
17193 }
17194
17195 if (range_types.empty ())
17196 {
17197 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17198 sect_offset_str (die->sect_off),
17199 objfile_name (cu->per_objfile->objfile));
17200 return NULL;
17201 }
17202
17203 /* Dwarf2 dimensions are output from left to right, create the
17204 necessary array types in backwards order. */
17205
17206 type = element_type;
17207
17208 if (read_array_order (die, cu) == DW_ORD_col_major)
17209 {
17210 int i = 0;
17211
17212 while (i < range_types.size ())
17213 {
17214 type = create_array_type_with_stride (NULL, type, range_types[i++],
17215 byte_stride_prop, bit_stride);
17216 bit_stride = 0;
17217 byte_stride_prop = nullptr;
17218 }
17219 }
17220 else
17221 {
17222 size_t ndim = range_types.size ();
17223 while (ndim-- > 0)
17224 {
17225 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17226 byte_stride_prop, bit_stride);
17227 bit_stride = 0;
17228 byte_stride_prop = nullptr;
17229 }
17230 }
17231
17232 gdb_assert (type != element_type);
17233
17234 /* Understand Dwarf2 support for vector types (like they occur on
17235 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17236 array type. This is not part of the Dwarf2/3 standard yet, but a
17237 custom vendor extension. The main difference between a regular
17238 array and the vector variant is that vectors are passed by value
17239 to functions. */
17240 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17241 if (attr != nullptr)
17242 make_vector_type (type);
17243
17244 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17245 implementation may choose to implement triple vectors using this
17246 attribute. */
17247 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17248 if (attr != nullptr && attr->form_is_unsigned ())
17249 {
17250 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17251 TYPE_LENGTH (type) = attr->as_unsigned ();
17252 else
17253 complaint (_("DW_AT_byte_size for array type smaller "
17254 "than the total size of elements"));
17255 }
17256
17257 name = dwarf2_name (die, cu);
17258 if (name)
17259 type->set_name (name);
17260
17261 maybe_set_alignment (cu, die, type);
17262
17263 struct type *replacement_type = nullptr;
17264 if (cu->per_cu->lang == language_ada)
17265 {
17266 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17267 if (replacement_type != nullptr)
17268 type = replacement_type;
17269 }
17270
17271 /* Install the type in the die. */
17272 set_die_type (die, type, cu, replacement_type != nullptr);
17273
17274 /* set_die_type should be already done. */
17275 set_descriptive_type (type, die, cu);
17276
17277 return type;
17278 }
17279
17280 static enum dwarf_array_dim_ordering
17281 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17282 {
17283 struct attribute *attr;
17284
17285 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17286
17287 if (attr != nullptr)
17288 {
17289 LONGEST val = attr->constant_value (-1);
17290 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17291 return (enum dwarf_array_dim_ordering) val;
17292 }
17293
17294 /* GNU F77 is a special case, as at 08/2004 array type info is the
17295 opposite order to the dwarf2 specification, but data is still
17296 laid out as per normal fortran.
17297
17298 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17299 version checking. */
17300
17301 if (cu->per_cu->lang == language_fortran
17302 && cu->producer && strstr (cu->producer, "GNU F77"))
17303 {
17304 return DW_ORD_row_major;
17305 }
17306
17307 switch (cu->language_defn->array_ordering ())
17308 {
17309 case array_column_major:
17310 return DW_ORD_col_major;
17311 case array_row_major:
17312 default:
17313 return DW_ORD_row_major;
17314 };
17315 }
17316
17317 /* Extract all information from a DW_TAG_set_type DIE and put it in
17318 the DIE's type field. */
17319
17320 static struct type *
17321 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17322 {
17323 struct type *domain_type, *set_type;
17324 struct attribute *attr;
17325
17326 domain_type = die_type (die, cu);
17327
17328 /* The die_type call above may have already set the type for this DIE. */
17329 set_type = get_die_type (die, cu);
17330 if (set_type)
17331 return set_type;
17332
17333 set_type = create_set_type (NULL, domain_type);
17334
17335 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17336 if (attr != nullptr && attr->form_is_unsigned ())
17337 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17338
17339 maybe_set_alignment (cu, die, set_type);
17340
17341 return set_die_type (die, set_type, cu);
17342 }
17343
17344 /* A helper for read_common_block that creates a locexpr baton.
17345 SYM is the symbol which we are marking as computed.
17346 COMMON_DIE is the DIE for the common block.
17347 COMMON_LOC is the location expression attribute for the common
17348 block itself.
17349 MEMBER_LOC is the location expression attribute for the particular
17350 member of the common block that we are processing.
17351 CU is the CU from which the above come. */
17352
17353 static void
17354 mark_common_block_symbol_computed (struct symbol *sym,
17355 struct die_info *common_die,
17356 struct attribute *common_loc,
17357 struct attribute *member_loc,
17358 struct dwarf2_cu *cu)
17359 {
17360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17361 struct objfile *objfile = per_objfile->objfile;
17362 struct dwarf2_locexpr_baton *baton;
17363 gdb_byte *ptr;
17364 unsigned int cu_off;
17365 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17366 LONGEST offset = 0;
17367
17368 gdb_assert (common_loc && member_loc);
17369 gdb_assert (common_loc->form_is_block ());
17370 gdb_assert (member_loc->form_is_block ()
17371 || member_loc->form_is_constant ());
17372
17373 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17374 baton->per_objfile = per_objfile;
17375 baton->per_cu = cu->per_cu;
17376 gdb_assert (baton->per_cu);
17377
17378 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17379
17380 if (member_loc->form_is_constant ())
17381 {
17382 offset = member_loc->constant_value (0);
17383 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17384 }
17385 else
17386 baton->size += member_loc->as_block ()->size;
17387
17388 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17389 baton->data = ptr;
17390
17391 *ptr++ = DW_OP_call4;
17392 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17393 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17394 ptr += 4;
17395
17396 if (member_loc->form_is_constant ())
17397 {
17398 *ptr++ = DW_OP_addr;
17399 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17400 ptr += cu->header.addr_size;
17401 }
17402 else
17403 {
17404 /* We have to copy the data here, because DW_OP_call4 will only
17405 use a DW_AT_location attribute. */
17406 struct dwarf_block *block = member_loc->as_block ();
17407 memcpy (ptr, block->data, block->size);
17408 ptr += block->size;
17409 }
17410
17411 *ptr++ = DW_OP_plus;
17412 gdb_assert (ptr - baton->data == baton->size);
17413
17414 SYMBOL_LOCATION_BATON (sym) = baton;
17415 sym->set_aclass_index (dwarf2_locexpr_index);
17416 }
17417
17418 /* Create appropriate locally-scoped variables for all the
17419 DW_TAG_common_block entries. Also create a struct common_block
17420 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17421 is used to separate the common blocks name namespace from regular
17422 variable names. */
17423
17424 static void
17425 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17426 {
17427 struct attribute *attr;
17428
17429 attr = dwarf2_attr (die, DW_AT_location, cu);
17430 if (attr != nullptr)
17431 {
17432 /* Support the .debug_loc offsets. */
17433 if (attr->form_is_block ())
17434 {
17435 /* Ok. */
17436 }
17437 else if (attr->form_is_section_offset ())
17438 {
17439 dwarf2_complex_location_expr_complaint ();
17440 attr = NULL;
17441 }
17442 else
17443 {
17444 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17445 "common block member");
17446 attr = NULL;
17447 }
17448 }
17449
17450 if (die->child != NULL)
17451 {
17452 struct objfile *objfile = cu->per_objfile->objfile;
17453 struct die_info *child_die;
17454 size_t n_entries = 0, size;
17455 struct common_block *common_block;
17456 struct symbol *sym;
17457
17458 for (child_die = die->child;
17459 child_die && child_die->tag;
17460 child_die = child_die->sibling)
17461 ++n_entries;
17462
17463 size = (sizeof (struct common_block)
17464 + (n_entries - 1) * sizeof (struct symbol *));
17465 common_block
17466 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17467 size);
17468 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17469 common_block->n_entries = 0;
17470
17471 for (child_die = die->child;
17472 child_die && child_die->tag;
17473 child_die = child_die->sibling)
17474 {
17475 /* Create the symbol in the DW_TAG_common_block block in the current
17476 symbol scope. */
17477 sym = new_symbol (child_die, NULL, cu);
17478 if (sym != NULL)
17479 {
17480 struct attribute *member_loc;
17481
17482 common_block->contents[common_block->n_entries++] = sym;
17483
17484 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17485 cu);
17486 if (member_loc)
17487 {
17488 /* GDB has handled this for a long time, but it is
17489 not specified by DWARF. It seems to have been
17490 emitted by gfortran at least as recently as:
17491 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17492 complaint (_("Variable in common block has "
17493 "DW_AT_data_member_location "
17494 "- DIE at %s [in module %s]"),
17495 sect_offset_str (child_die->sect_off),
17496 objfile_name (objfile));
17497
17498 if (member_loc->form_is_section_offset ())
17499 dwarf2_complex_location_expr_complaint ();
17500 else if (member_loc->form_is_constant ()
17501 || member_loc->form_is_block ())
17502 {
17503 if (attr != nullptr)
17504 mark_common_block_symbol_computed (sym, die, attr,
17505 member_loc, cu);
17506 }
17507 else
17508 dwarf2_complex_location_expr_complaint ();
17509 }
17510 }
17511 }
17512
17513 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17514 sym->set_value_common_block (common_block);
17515 }
17516 }
17517
17518 /* Create a type for a C++ namespace. */
17519
17520 static struct type *
17521 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17522 {
17523 struct objfile *objfile = cu->per_objfile->objfile;
17524 const char *previous_prefix, *name;
17525 int is_anonymous;
17526 struct type *type;
17527
17528 /* For extensions, reuse the type of the original namespace. */
17529 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17530 {
17531 struct die_info *ext_die;
17532 struct dwarf2_cu *ext_cu = cu;
17533
17534 ext_die = dwarf2_extension (die, &ext_cu);
17535 type = read_type_die (ext_die, ext_cu);
17536
17537 /* EXT_CU may not be the same as CU.
17538 Ensure TYPE is recorded with CU in die_type_hash. */
17539 return set_die_type (die, type, cu);
17540 }
17541
17542 name = namespace_name (die, &is_anonymous, cu);
17543
17544 /* Now build the name of the current namespace. */
17545
17546 previous_prefix = determine_prefix (die, cu);
17547 if (previous_prefix[0] != '\0')
17548 name = typename_concat (&objfile->objfile_obstack,
17549 previous_prefix, name, 0, cu);
17550
17551 /* Create the type. */
17552 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17553
17554 return set_die_type (die, type, cu);
17555 }
17556
17557 /* Read a namespace scope. */
17558
17559 static void
17560 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17561 {
17562 struct objfile *objfile = cu->per_objfile->objfile;
17563 int is_anonymous;
17564
17565 /* Add a symbol associated to this if we haven't seen the namespace
17566 before. Also, add a using directive if it's an anonymous
17567 namespace. */
17568
17569 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17570 {
17571 struct type *type;
17572
17573 type = read_type_die (die, cu);
17574 new_symbol (die, type, cu);
17575
17576 namespace_name (die, &is_anonymous, cu);
17577 if (is_anonymous)
17578 {
17579 const char *previous_prefix = determine_prefix (die, cu);
17580
17581 std::vector<const char *> excludes;
17582 add_using_directive (using_directives (cu),
17583 previous_prefix, type->name (), NULL,
17584 NULL, excludes, 0, &objfile->objfile_obstack);
17585 }
17586 }
17587
17588 if (die->child != NULL)
17589 {
17590 struct die_info *child_die = die->child;
17591
17592 while (child_die && child_die->tag)
17593 {
17594 process_die (child_die, cu);
17595 child_die = child_die->sibling;
17596 }
17597 }
17598 }
17599
17600 /* Read a Fortran module as type. This DIE can be only a declaration used for
17601 imported module. Still we need that type as local Fortran "use ... only"
17602 declaration imports depend on the created type in determine_prefix. */
17603
17604 static struct type *
17605 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17606 {
17607 struct objfile *objfile = cu->per_objfile->objfile;
17608 const char *module_name;
17609 struct type *type;
17610
17611 module_name = dwarf2_name (die, cu);
17612 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17613
17614 return set_die_type (die, type, cu);
17615 }
17616
17617 /* Read a Fortran module. */
17618
17619 static void
17620 read_module (struct die_info *die, struct dwarf2_cu *cu)
17621 {
17622 struct die_info *child_die = die->child;
17623 struct type *type;
17624
17625 type = read_type_die (die, cu);
17626 new_symbol (die, type, cu);
17627
17628 while (child_die && child_die->tag)
17629 {
17630 process_die (child_die, cu);
17631 child_die = child_die->sibling;
17632 }
17633 }
17634
17635 /* Return the name of the namespace represented by DIE. Set
17636 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17637 namespace. */
17638
17639 static const char *
17640 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17641 {
17642 struct die_info *current_die;
17643 const char *name = NULL;
17644
17645 /* Loop through the extensions until we find a name. */
17646
17647 for (current_die = die;
17648 current_die != NULL;
17649 current_die = dwarf2_extension (die, &cu))
17650 {
17651 /* We don't use dwarf2_name here so that we can detect the absence
17652 of a name -> anonymous namespace. */
17653 name = dwarf2_string_attr (die, DW_AT_name, cu);
17654
17655 if (name != NULL)
17656 break;
17657 }
17658
17659 /* Is it an anonymous namespace? */
17660
17661 *is_anonymous = (name == NULL);
17662 if (*is_anonymous)
17663 name = CP_ANONYMOUS_NAMESPACE_STR;
17664
17665 return name;
17666 }
17667
17668 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17669 the user defined type vector. */
17670
17671 static struct type *
17672 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17673 {
17674 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17675 struct comp_unit_head *cu_header = &cu->header;
17676 struct type *type;
17677 struct attribute *attr_byte_size;
17678 struct attribute *attr_address_class;
17679 int byte_size, addr_class;
17680 struct type *target_type;
17681
17682 target_type = die_type (die, cu);
17683
17684 /* The die_type call above may have already set the type for this DIE. */
17685 type = get_die_type (die, cu);
17686 if (type)
17687 return type;
17688
17689 type = lookup_pointer_type (target_type);
17690
17691 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17692 if (attr_byte_size)
17693 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17694 else
17695 byte_size = cu_header->addr_size;
17696
17697 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17698 if (attr_address_class)
17699 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17700 else
17701 addr_class = DW_ADDR_none;
17702
17703 ULONGEST alignment = get_alignment (cu, die);
17704
17705 /* If the pointer size, alignment, or address class is different
17706 than the default, create a type variant marked as such and set
17707 the length accordingly. */
17708 if (TYPE_LENGTH (type) != byte_size
17709 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17710 && alignment != TYPE_RAW_ALIGN (type))
17711 || addr_class != DW_ADDR_none)
17712 {
17713 if (gdbarch_address_class_type_flags_p (gdbarch))
17714 {
17715 type_instance_flags type_flags
17716 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17717 addr_class);
17718 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17719 == 0);
17720 type = make_type_with_address_space (type, type_flags);
17721 }
17722 else if (TYPE_LENGTH (type) != byte_size)
17723 {
17724 complaint (_("invalid pointer size %d"), byte_size);
17725 }
17726 else if (TYPE_RAW_ALIGN (type) != alignment)
17727 {
17728 complaint (_("Invalid DW_AT_alignment"
17729 " - DIE at %s [in module %s]"),
17730 sect_offset_str (die->sect_off),
17731 objfile_name (cu->per_objfile->objfile));
17732 }
17733 else
17734 {
17735 /* Should we also complain about unhandled address classes? */
17736 }
17737 }
17738
17739 TYPE_LENGTH (type) = byte_size;
17740 set_type_align (type, alignment);
17741 return set_die_type (die, type, cu);
17742 }
17743
17744 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17745 the user defined type vector. */
17746
17747 static struct type *
17748 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17749 {
17750 struct type *type;
17751 struct type *to_type;
17752 struct type *domain;
17753
17754 to_type = die_type (die, cu);
17755 domain = die_containing_type (die, cu);
17756
17757 /* The calls above may have already set the type for this DIE. */
17758 type = get_die_type (die, cu);
17759 if (type)
17760 return type;
17761
17762 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17763 type = lookup_methodptr_type (to_type);
17764 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17765 {
17766 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17767
17768 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17769 to_type->fields (), to_type->num_fields (),
17770 to_type->has_varargs ());
17771 type = lookup_methodptr_type (new_type);
17772 }
17773 else
17774 type = lookup_memberptr_type (to_type, domain);
17775
17776 return set_die_type (die, type, cu);
17777 }
17778
17779 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17780 the user defined type vector. */
17781
17782 static struct type *
17783 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17784 enum type_code refcode)
17785 {
17786 struct comp_unit_head *cu_header = &cu->header;
17787 struct type *type, *target_type;
17788 struct attribute *attr;
17789
17790 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17791
17792 target_type = die_type (die, cu);
17793
17794 /* The die_type call above may have already set the type for this DIE. */
17795 type = get_die_type (die, cu);
17796 if (type)
17797 return type;
17798
17799 type = lookup_reference_type (target_type, refcode);
17800 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17801 if (attr != nullptr)
17802 {
17803 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17804 }
17805 else
17806 {
17807 TYPE_LENGTH (type) = cu_header->addr_size;
17808 }
17809 maybe_set_alignment (cu, die, type);
17810 return set_die_type (die, type, cu);
17811 }
17812
17813 /* Add the given cv-qualifiers to the element type of the array. GCC
17814 outputs DWARF type qualifiers that apply to an array, not the
17815 element type. But GDB relies on the array element type to carry
17816 the cv-qualifiers. This mimics section 6.7.3 of the C99
17817 specification. */
17818
17819 static struct type *
17820 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17821 struct type *base_type, int cnst, int voltl)
17822 {
17823 struct type *el_type, *inner_array;
17824
17825 base_type = copy_type (base_type);
17826 inner_array = base_type;
17827
17828 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17829 {
17830 TYPE_TARGET_TYPE (inner_array) =
17831 copy_type (TYPE_TARGET_TYPE (inner_array));
17832 inner_array = TYPE_TARGET_TYPE (inner_array);
17833 }
17834
17835 el_type = TYPE_TARGET_TYPE (inner_array);
17836 cnst |= TYPE_CONST (el_type);
17837 voltl |= TYPE_VOLATILE (el_type);
17838 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17839
17840 return set_die_type (die, base_type, cu);
17841 }
17842
17843 static struct type *
17844 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17845 {
17846 struct type *base_type, *cv_type;
17847
17848 base_type = die_type (die, cu);
17849
17850 /* The die_type call above may have already set the type for this DIE. */
17851 cv_type = get_die_type (die, cu);
17852 if (cv_type)
17853 return cv_type;
17854
17855 /* In case the const qualifier is applied to an array type, the element type
17856 is so qualified, not the array type (section 6.7.3 of C99). */
17857 if (base_type->code () == TYPE_CODE_ARRAY)
17858 return add_array_cv_type (die, cu, base_type, 1, 0);
17859
17860 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17861 return set_die_type (die, cv_type, cu);
17862 }
17863
17864 static struct type *
17865 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17866 {
17867 struct type *base_type, *cv_type;
17868
17869 base_type = die_type (die, cu);
17870
17871 /* The die_type call above may have already set the type for this DIE. */
17872 cv_type = get_die_type (die, cu);
17873 if (cv_type)
17874 return cv_type;
17875
17876 /* In case the volatile qualifier is applied to an array type, the
17877 element type is so qualified, not the array type (section 6.7.3
17878 of C99). */
17879 if (base_type->code () == TYPE_CODE_ARRAY)
17880 return add_array_cv_type (die, cu, base_type, 0, 1);
17881
17882 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17883 return set_die_type (die, cv_type, cu);
17884 }
17885
17886 /* Handle DW_TAG_restrict_type. */
17887
17888 static struct type *
17889 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17890 {
17891 struct type *base_type, *cv_type;
17892
17893 base_type = die_type (die, cu);
17894
17895 /* The die_type call above may have already set the type for this DIE. */
17896 cv_type = get_die_type (die, cu);
17897 if (cv_type)
17898 return cv_type;
17899
17900 cv_type = make_restrict_type (base_type);
17901 return set_die_type (die, cv_type, cu);
17902 }
17903
17904 /* Handle DW_TAG_atomic_type. */
17905
17906 static struct type *
17907 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17908 {
17909 struct type *base_type, *cv_type;
17910
17911 base_type = die_type (die, cu);
17912
17913 /* The die_type call above may have already set the type for this DIE. */
17914 cv_type = get_die_type (die, cu);
17915 if (cv_type)
17916 return cv_type;
17917
17918 cv_type = make_atomic_type (base_type);
17919 return set_die_type (die, cv_type, cu);
17920 }
17921
17922 /* Extract all information from a DW_TAG_string_type DIE and add to
17923 the user defined type vector. It isn't really a user defined type,
17924 but it behaves like one, with other DIE's using an AT_user_def_type
17925 attribute to reference it. */
17926
17927 static struct type *
17928 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17929 {
17930 struct objfile *objfile = cu->per_objfile->objfile;
17931 struct gdbarch *gdbarch = objfile->arch ();
17932 struct type *type, *range_type, *index_type, *char_type;
17933 struct attribute *attr;
17934 struct dynamic_prop prop;
17935 bool length_is_constant = true;
17936 LONGEST length;
17937
17938 /* There are a couple of places where bit sizes might be made use of
17939 when parsing a DW_TAG_string_type, however, no producer that we know
17940 of make use of these. Handling bit sizes that are a multiple of the
17941 byte size is easy enough, but what about other bit sizes? Lets deal
17942 with that problem when we have to. Warn about these attributes being
17943 unsupported, then parse the type and ignore them like we always
17944 have. */
17945 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17946 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17947 {
17948 static bool warning_printed = false;
17949 if (!warning_printed)
17950 {
17951 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17952 "currently supported on DW_TAG_string_type."));
17953 warning_printed = true;
17954 }
17955 }
17956
17957 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17958 if (attr != nullptr && !attr->form_is_constant ())
17959 {
17960 /* The string length describes the location at which the length of
17961 the string can be found. The size of the length field can be
17962 specified with one of the attributes below. */
17963 struct type *prop_type;
17964 struct attribute *len
17965 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17966 if (len == nullptr)
17967 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17968 if (len != nullptr && len->form_is_constant ())
17969 {
17970 /* Pass 0 as the default as we know this attribute is constant
17971 and the default value will not be returned. */
17972 LONGEST sz = len->constant_value (0);
17973 prop_type = objfile_int_type (objfile, sz, true);
17974 }
17975 else
17976 {
17977 /* If the size is not specified then we assume it is the size of
17978 an address on this target. */
17979 prop_type = cu->addr_sized_int_type (true);
17980 }
17981
17982 /* Convert the attribute into a dynamic property. */
17983 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17984 length = 1;
17985 else
17986 length_is_constant = false;
17987 }
17988 else if (attr != nullptr)
17989 {
17990 /* This DW_AT_string_length just contains the length with no
17991 indirection. There's no need to create a dynamic property in this
17992 case. Pass 0 for the default value as we know it will not be
17993 returned in this case. */
17994 length = attr->constant_value (0);
17995 }
17996 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17997 {
17998 /* We don't currently support non-constant byte sizes for strings. */
17999 length = attr->constant_value (1);
18000 }
18001 else
18002 {
18003 /* Use 1 as a fallback length if we have nothing else. */
18004 length = 1;
18005 }
18006
18007 index_type = objfile_type (objfile)->builtin_int;
18008 if (length_is_constant)
18009 range_type = create_static_range_type (NULL, index_type, 1, length);
18010 else
18011 {
18012 struct dynamic_prop low_bound;
18013
18014 low_bound.set_const_val (1);
18015 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18016 }
18017 char_type = language_string_char_type (cu->language_defn, gdbarch);
18018 type = create_string_type (NULL, char_type, range_type);
18019
18020 return set_die_type (die, type, cu);
18021 }
18022
18023 /* Assuming that DIE corresponds to a function, returns nonzero
18024 if the function is prototyped. */
18025
18026 static int
18027 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18028 {
18029 struct attribute *attr;
18030
18031 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18032 if (attr && attr->as_boolean ())
18033 return 1;
18034
18035 /* The DWARF standard implies that the DW_AT_prototyped attribute
18036 is only meaningful for C, but the concept also extends to other
18037 languages that allow unprototyped functions (Eg: Objective C).
18038 For all other languages, assume that functions are always
18039 prototyped. */
18040 if (cu->per_cu->lang != language_c
18041 && cu->per_cu->lang != language_objc
18042 && cu->per_cu->lang != language_opencl)
18043 return 1;
18044
18045 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18046 prototyped and unprototyped functions; default to prototyped,
18047 since that is more common in modern code (and RealView warns
18048 about unprototyped functions). */
18049 if (producer_is_realview (cu->producer))
18050 return 1;
18051
18052 return 0;
18053 }
18054
18055 /* Handle DIES due to C code like:
18056
18057 struct foo
18058 {
18059 int (*funcp)(int a, long l);
18060 int b;
18061 };
18062
18063 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18064
18065 static struct type *
18066 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18067 {
18068 struct objfile *objfile = cu->per_objfile->objfile;
18069 struct type *type; /* Type that this function returns. */
18070 struct type *ftype; /* Function that returns above type. */
18071 struct attribute *attr;
18072
18073 type = die_type (die, cu);
18074
18075 /* The die_type call above may have already set the type for this DIE. */
18076 ftype = get_die_type (die, cu);
18077 if (ftype)
18078 return ftype;
18079
18080 ftype = lookup_function_type (type);
18081
18082 if (prototyped_function_p (die, cu))
18083 ftype->set_is_prototyped (true);
18084
18085 /* Store the calling convention in the type if it's available in
18086 the subroutine die. Otherwise set the calling convention to
18087 the default value DW_CC_normal. */
18088 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18089 if (attr != nullptr
18090 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18091 TYPE_CALLING_CONVENTION (ftype)
18092 = (enum dwarf_calling_convention) attr->constant_value (0);
18093 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18094 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18095 else
18096 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18097
18098 /* Record whether the function returns normally to its caller or not
18099 if the DWARF producer set that information. */
18100 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18101 if (attr && attr->as_boolean ())
18102 TYPE_NO_RETURN (ftype) = 1;
18103
18104 /* We need to add the subroutine type to the die immediately so
18105 we don't infinitely recurse when dealing with parameters
18106 declared as the same subroutine type. */
18107 set_die_type (die, ftype, cu);
18108
18109 if (die->child != NULL)
18110 {
18111 struct type *void_type = objfile_type (objfile)->builtin_void;
18112 struct die_info *child_die;
18113 int nparams, iparams;
18114
18115 /* Count the number of parameters.
18116 FIXME: GDB currently ignores vararg functions, but knows about
18117 vararg member functions. */
18118 nparams = 0;
18119 child_die = die->child;
18120 while (child_die && child_die->tag)
18121 {
18122 if (child_die->tag == DW_TAG_formal_parameter)
18123 nparams++;
18124 else if (child_die->tag == DW_TAG_unspecified_parameters)
18125 ftype->set_has_varargs (true);
18126
18127 child_die = child_die->sibling;
18128 }
18129
18130 /* Allocate storage for parameters and fill them in. */
18131 ftype->set_num_fields (nparams);
18132 ftype->set_fields
18133 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18134
18135 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18136 even if we error out during the parameters reading below. */
18137 for (iparams = 0; iparams < nparams; iparams++)
18138 ftype->field (iparams).set_type (void_type);
18139
18140 iparams = 0;
18141 child_die = die->child;
18142 while (child_die && child_die->tag)
18143 {
18144 if (child_die->tag == DW_TAG_formal_parameter)
18145 {
18146 struct type *arg_type;
18147
18148 /* DWARF version 2 has no clean way to discern C++
18149 static and non-static member functions. G++ helps
18150 GDB by marking the first parameter for non-static
18151 member functions (which is the this pointer) as
18152 artificial. We pass this information to
18153 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18154
18155 DWARF version 3 added DW_AT_object_pointer, which GCC
18156 4.5 does not yet generate. */
18157 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18158 if (attr != nullptr)
18159 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18160 else
18161 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18162 arg_type = die_type (child_die, cu);
18163
18164 /* RealView does not mark THIS as const, which the testsuite
18165 expects. GCC marks THIS as const in method definitions,
18166 but not in the class specifications (GCC PR 43053). */
18167 if (cu->per_cu->lang == language_cplus
18168 && !TYPE_CONST (arg_type)
18169 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18170 {
18171 int is_this = 0;
18172 struct dwarf2_cu *arg_cu = cu;
18173 const char *name = dwarf2_name (child_die, cu);
18174
18175 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18176 if (attr != nullptr)
18177 {
18178 /* If the compiler emits this, use it. */
18179 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18180 is_this = 1;
18181 }
18182 else if (name && strcmp (name, "this") == 0)
18183 /* Function definitions will have the argument names. */
18184 is_this = 1;
18185 else if (name == NULL && iparams == 0)
18186 /* Declarations may not have the names, so like
18187 elsewhere in GDB, assume an artificial first
18188 argument is "this". */
18189 is_this = 1;
18190
18191 if (is_this)
18192 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18193 arg_type, 0);
18194 }
18195
18196 ftype->field (iparams).set_type (arg_type);
18197 iparams++;
18198 }
18199 child_die = child_die->sibling;
18200 }
18201 }
18202
18203 return ftype;
18204 }
18205
18206 static struct type *
18207 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18208 {
18209 struct objfile *objfile = cu->per_objfile->objfile;
18210 const char *name = NULL;
18211 struct type *this_type, *target_type;
18212
18213 name = dwarf2_full_name (NULL, die, cu);
18214 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18215 this_type->set_target_is_stub (true);
18216 set_die_type (die, this_type, cu);
18217 target_type = die_type (die, cu);
18218 if (target_type != this_type)
18219 TYPE_TARGET_TYPE (this_type) = target_type;
18220 else
18221 {
18222 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18223 spec and cause infinite loops in GDB. */
18224 complaint (_("Self-referential DW_TAG_typedef "
18225 "- DIE at %s [in module %s]"),
18226 sect_offset_str (die->sect_off), objfile_name (objfile));
18227 TYPE_TARGET_TYPE (this_type) = NULL;
18228 }
18229 if (name == NULL)
18230 {
18231 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18232 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18233 Handle these by just returning the target type, rather than
18234 constructing an anonymous typedef type and trying to handle this
18235 elsewhere. */
18236 set_die_type (die, target_type, cu);
18237 return target_type;
18238 }
18239 return this_type;
18240 }
18241
18242 /* Helper for get_dwarf2_rational_constant that computes the value of
18243 a given gmp_mpz given an attribute. */
18244
18245 static void
18246 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18247 {
18248 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18249 location expression that pushes an implicit value. */
18250 if (attr->form == DW_FORM_exprloc)
18251 {
18252 dwarf_block *blk = attr->as_block ();
18253 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18254 {
18255 uint64_t len;
18256 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18257 blk->data + blk->size,
18258 &len);
18259 if (ptr - blk->data + len <= blk->size)
18260 {
18261 mpz_import (value->val, len,
18262 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18263 1, 0, 0, ptr);
18264 return;
18265 }
18266 }
18267
18268 /* On failure set it to 1. */
18269 *value = gdb_mpz (1);
18270 }
18271 else if (attr->form_is_block ())
18272 {
18273 dwarf_block *blk = attr->as_block ();
18274 mpz_import (value->val, blk->size,
18275 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18276 1, 0, 0, blk->data);
18277 }
18278 else
18279 *value = gdb_mpz (attr->constant_value (1));
18280 }
18281
18282 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18283 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18284
18285 If the numerator and/or numerator attribute is missing,
18286 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18287 untouched. */
18288
18289 static void
18290 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18291 gdb_mpz *numerator, gdb_mpz *denominator)
18292 {
18293 struct attribute *num_attr, *denom_attr;
18294
18295 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18296 if (num_attr == nullptr)
18297 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18298 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18299
18300 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18301 if (denom_attr == nullptr)
18302 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18303 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18304
18305 if (num_attr == nullptr || denom_attr == nullptr)
18306 return;
18307
18308 get_mpz (cu, numerator, num_attr);
18309 get_mpz (cu, denominator, denom_attr);
18310 }
18311
18312 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18313 rational constant, rather than a signed one.
18314
18315 If the rational constant has a negative value, a complaint
18316 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18317
18318 static void
18319 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18320 struct dwarf2_cu *cu,
18321 gdb_mpz *numerator,
18322 gdb_mpz *denominator)
18323 {
18324 gdb_mpz num (1);
18325 gdb_mpz denom (1);
18326
18327 get_dwarf2_rational_constant (die, cu, &num, &denom);
18328 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18329 {
18330 mpz_neg (num.val, num.val);
18331 mpz_neg (denom.val, denom.val);
18332 }
18333 else if (mpz_sgn (num.val) == -1)
18334 {
18335 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18336 " in DIE at %s"),
18337 sect_offset_str (die->sect_off));
18338 return;
18339 }
18340 else if (mpz_sgn (denom.val) == -1)
18341 {
18342 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18343 " in DIE at %s"),
18344 sect_offset_str (die->sect_off));
18345 return;
18346 }
18347
18348 *numerator = std::move (num);
18349 *denominator = std::move (denom);
18350 }
18351
18352 /* Assuming that ENCODING is a string whose contents starting at the
18353 K'th character is "_nn" where "nn" is a decimal number, scan that
18354 number and set RESULT to the value. K is updated to point to the
18355 character immediately following the number.
18356
18357 If the string does not conform to the format described above, false
18358 is returned, and K may or may not be changed. */
18359
18360 static bool
18361 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18362 {
18363 /* The next character should be an underscore ('_') followed
18364 by a digit. */
18365 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18366 return false;
18367
18368 /* Skip the underscore. */
18369 k++;
18370 int start = k;
18371
18372 /* Determine the number of digits for our number. */
18373 while (isdigit (encoding[k]))
18374 k++;
18375 if (k == start)
18376 return false;
18377
18378 std::string copy (&encoding[start], k - start);
18379 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18380 return false;
18381
18382 return true;
18383 }
18384
18385 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18386 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18387 DENOM, update OFFSET, and return true on success. Return false on
18388 failure. */
18389
18390 static bool
18391 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18392 gdb_mpz *num, gdb_mpz *denom)
18393 {
18394 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18395 return false;
18396 return ada_get_gnat_encoded_number (encoding, offset, denom);
18397 }
18398
18399 /* Assuming DIE corresponds to a fixed point type, finish the creation
18400 of the corresponding TYPE by setting its type-specific data. CU is
18401 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18402 encodings. It is nullptr if the GNAT encoding should be
18403 ignored. */
18404
18405 static void
18406 finish_fixed_point_type (struct type *type, const char *suffix,
18407 struct die_info *die, struct dwarf2_cu *cu)
18408 {
18409 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18410 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18411
18412 /* If GNAT encodings are preferred, don't examine the
18413 attributes. */
18414 struct attribute *attr = nullptr;
18415 if (suffix == nullptr)
18416 {
18417 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18418 if (attr == nullptr)
18419 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18420 if (attr == nullptr)
18421 attr = dwarf2_attr (die, DW_AT_small, cu);
18422 }
18423
18424 /* Numerator and denominator of our fixed-point type's scaling factor.
18425 The default is a scaling factor of 1, which we use as a fallback
18426 when we are not able to decode it (problem with the debugging info,
18427 unsupported forms, bug in GDB, etc...). Using that as the default
18428 allows us to at least print the unscaled value, which might still
18429 be useful to a user. */
18430 gdb_mpz scale_num (1);
18431 gdb_mpz scale_denom (1);
18432
18433 if (attr == nullptr)
18434 {
18435 int offset = 0;
18436 if (suffix != nullptr
18437 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18438 &scale_denom)
18439 /* The number might be encoded as _nn_dd_nn_dd, where the
18440 second ratio is the 'small value. In this situation, we
18441 want the second value. */
18442 && (suffix[offset] != '_'
18443 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18444 &scale_denom)))
18445 {
18446 /* Found it. */
18447 }
18448 else
18449 {
18450 /* Scaling factor not found. Assume a scaling factor of 1,
18451 and hope for the best. At least the user will be able to
18452 see the encoded value. */
18453 scale_num = 1;
18454 scale_denom = 1;
18455 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18456 sect_offset_str (die->sect_off));
18457 }
18458 }
18459 else if (attr->name == DW_AT_binary_scale)
18460 {
18461 LONGEST scale_exp = attr->constant_value (0);
18462 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18463
18464 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18465 }
18466 else if (attr->name == DW_AT_decimal_scale)
18467 {
18468 LONGEST scale_exp = attr->constant_value (0);
18469 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18470
18471 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18472 }
18473 else if (attr->name == DW_AT_small)
18474 {
18475 struct die_info *scale_die;
18476 struct dwarf2_cu *scale_cu = cu;
18477
18478 scale_die = follow_die_ref (die, attr, &scale_cu);
18479 if (scale_die->tag == DW_TAG_constant)
18480 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18481 &scale_num, &scale_denom);
18482 else
18483 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18484 " (DIE at %s)"),
18485 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18486 }
18487 else
18488 {
18489 complaint (_("unsupported scale attribute %s for fixed-point type"
18490 " (DIE at %s)"),
18491 dwarf_attr_name (attr->name),
18492 sect_offset_str (die->sect_off));
18493 }
18494
18495 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18496 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18497 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18498 mpq_canonicalize (scaling_factor.val);
18499 }
18500
18501 /* The gnat-encoding suffix for fixed point. */
18502
18503 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18504
18505 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18506 "XF" suffix of the name. The text after this is what encodes the
18507 'small and 'delta information. Otherwise, return nullptr. */
18508
18509 static const char *
18510 gnat_encoded_fixed_point_type_info (const char *name)
18511 {
18512 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18513 }
18514
18515 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18516 (which may be different from NAME) to the architecture back-end to allow
18517 it to guess the correct format if necessary. */
18518
18519 static struct type *
18520 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18521 const char *name_hint, enum bfd_endian byte_order)
18522 {
18523 struct gdbarch *gdbarch = objfile->arch ();
18524 const struct floatformat **format;
18525 struct type *type;
18526
18527 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18528 if (format)
18529 type = init_float_type (objfile, bits, name, format, byte_order);
18530 else
18531 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18532
18533 return type;
18534 }
18535
18536 /* Allocate an integer type of size BITS and name NAME. */
18537
18538 static struct type *
18539 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18540 int bits, int unsigned_p, const char *name)
18541 {
18542 struct type *type;
18543
18544 /* Versions of Intel's C Compiler generate an integer type called "void"
18545 instead of using DW_TAG_unspecified_type. This has been seen on
18546 at least versions 14, 17, and 18. */
18547 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18548 && strcmp (name, "void") == 0)
18549 type = objfile_type (objfile)->builtin_void;
18550 else
18551 type = init_integer_type (objfile, bits, unsigned_p, name);
18552
18553 return type;
18554 }
18555
18556 /* Return true if DIE has a DW_AT_small attribute whose value is
18557 a constant rational, where both the numerator and denominator
18558 are equal to zero.
18559
18560 CU is the DIE's Compilation Unit. */
18561
18562 static bool
18563 has_zero_over_zero_small_attribute (struct die_info *die,
18564 struct dwarf2_cu *cu)
18565 {
18566 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18567 if (attr == nullptr)
18568 return false;
18569
18570 struct dwarf2_cu *scale_cu = cu;
18571 struct die_info *scale_die
18572 = follow_die_ref (die, attr, &scale_cu);
18573
18574 if (scale_die->tag != DW_TAG_constant)
18575 return false;
18576
18577 gdb_mpz num (1), denom (1);
18578 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18579 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18580 }
18581
18582 /* Initialise and return a floating point type of size BITS suitable for
18583 use as a component of a complex number. The NAME_HINT is passed through
18584 when initialising the floating point type and is the name of the complex
18585 type.
18586
18587 As DWARF doesn't currently provide an explicit name for the components
18588 of a complex number, but it can be helpful to have these components
18589 named, we try to select a suitable name based on the size of the
18590 component. */
18591 static struct type *
18592 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18593 struct objfile *objfile,
18594 int bits, const char *name_hint,
18595 enum bfd_endian byte_order)
18596 {
18597 gdbarch *gdbarch = objfile->arch ();
18598 struct type *tt = nullptr;
18599
18600 /* Try to find a suitable floating point builtin type of size BITS.
18601 We're going to use the name of this type as the name for the complex
18602 target type that we are about to create. */
18603 switch (cu->per_cu->lang)
18604 {
18605 case language_fortran:
18606 switch (bits)
18607 {
18608 case 32:
18609 tt = builtin_f_type (gdbarch)->builtin_real;
18610 break;
18611 case 64:
18612 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18613 break;
18614 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18615 case 128:
18616 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18617 break;
18618 }
18619 break;
18620 default:
18621 switch (bits)
18622 {
18623 case 32:
18624 tt = builtin_type (gdbarch)->builtin_float;
18625 break;
18626 case 64:
18627 tt = builtin_type (gdbarch)->builtin_double;
18628 break;
18629 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18630 case 128:
18631 tt = builtin_type (gdbarch)->builtin_long_double;
18632 break;
18633 }
18634 break;
18635 }
18636
18637 /* If the type we found doesn't match the size we were looking for, then
18638 pretend we didn't find a type at all, the complex target type we
18639 create will then be nameless. */
18640 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18641 tt = nullptr;
18642
18643 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18644 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18645 }
18646
18647 /* Find a representation of a given base type and install
18648 it in the TYPE field of the die. */
18649
18650 static struct type *
18651 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18652 {
18653 struct objfile *objfile = cu->per_objfile->objfile;
18654 struct type *type;
18655 struct attribute *attr;
18656 int encoding = 0, bits = 0;
18657 const char *name;
18658 gdbarch *arch;
18659
18660 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18661 if (attr != nullptr && attr->form_is_constant ())
18662 encoding = attr->constant_value (0);
18663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18664 if (attr != nullptr)
18665 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18666 name = dwarf2_name (die, cu);
18667 if (!name)
18668 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18669
18670 arch = objfile->arch ();
18671 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18672
18673 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18674 if (attr != nullptr && attr->form_is_constant ())
18675 {
18676 int endianity = attr->constant_value (0);
18677
18678 switch (endianity)
18679 {
18680 case DW_END_big:
18681 byte_order = BFD_ENDIAN_BIG;
18682 break;
18683 case DW_END_little:
18684 byte_order = BFD_ENDIAN_LITTLE;
18685 break;
18686 default:
18687 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18688 break;
18689 }
18690 }
18691
18692 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18693 && cu->per_cu->lang == language_ada
18694 && has_zero_over_zero_small_attribute (die, cu))
18695 {
18696 /* brobecker/2018-02-24: This is a fixed point type for which
18697 the scaling factor is represented as fraction whose value
18698 does not make sense (zero divided by zero), so we should
18699 normally never see these. However, there is a small category
18700 of fixed point types for which GNAT is unable to provide
18701 the scaling factor via the standard DWARF mechanisms, and
18702 for which the info is provided via the GNAT encodings instead.
18703 This is likely what this DIE is about. */
18704 encoding = (encoding == DW_ATE_signed_fixed
18705 ? DW_ATE_signed
18706 : DW_ATE_unsigned);
18707 }
18708
18709 /* With GNAT encodings, fixed-point information will be encoded in
18710 the type name. Note that this can also occur with the above
18711 zero-over-zero case, which is why this is a separate "if" rather
18712 than an "else if". */
18713 const char *gnat_encoding_suffix = nullptr;
18714 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18715 && cu->per_cu->lang == language_ada
18716 && name != nullptr)
18717 {
18718 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18719 if (gnat_encoding_suffix != nullptr)
18720 {
18721 gdb_assert (startswith (gnat_encoding_suffix,
18722 GNAT_FIXED_POINT_SUFFIX));
18723 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18724 name, gnat_encoding_suffix - name);
18725 /* Use -1 here so that SUFFIX points at the "_" after the
18726 "XF". */
18727 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18728
18729 encoding = (encoding == DW_ATE_signed
18730 ? DW_ATE_signed_fixed
18731 : DW_ATE_unsigned_fixed);
18732 }
18733 }
18734
18735 switch (encoding)
18736 {
18737 case DW_ATE_address:
18738 /* Turn DW_ATE_address into a void * pointer. */
18739 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18740 type = init_pointer_type (objfile, bits, name, type);
18741 break;
18742 case DW_ATE_boolean:
18743 type = init_boolean_type (objfile, bits, 1, name);
18744 break;
18745 case DW_ATE_complex_float:
18746 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18747 byte_order);
18748 if (type->code () == TYPE_CODE_ERROR)
18749 {
18750 if (name == nullptr)
18751 {
18752 struct obstack *obstack
18753 = &cu->per_objfile->objfile->objfile_obstack;
18754 name = obconcat (obstack, "_Complex ", type->name (),
18755 nullptr);
18756 }
18757 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18758 }
18759 else
18760 type = init_complex_type (name, type);
18761 break;
18762 case DW_ATE_decimal_float:
18763 type = init_decfloat_type (objfile, bits, name);
18764 break;
18765 case DW_ATE_float:
18766 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18767 break;
18768 case DW_ATE_signed:
18769 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18770 break;
18771 case DW_ATE_unsigned:
18772 if (cu->per_cu->lang == language_fortran
18773 && name
18774 && startswith (name, "character("))
18775 type = init_character_type (objfile, bits, 1, name);
18776 else
18777 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18778 break;
18779 case DW_ATE_signed_char:
18780 if (cu->per_cu->lang == language_ada
18781 || cu->per_cu->lang == language_m2
18782 || cu->per_cu->lang == language_pascal
18783 || cu->per_cu->lang == language_fortran)
18784 type = init_character_type (objfile, bits, 0, name);
18785 else
18786 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18787 break;
18788 case DW_ATE_unsigned_char:
18789 if (cu->per_cu->lang == language_ada
18790 || cu->per_cu->lang == language_m2
18791 || cu->per_cu->lang == language_pascal
18792 || cu->per_cu->lang == language_fortran
18793 || cu->per_cu->lang == language_rust)
18794 type = init_character_type (objfile, bits, 1, name);
18795 else
18796 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18797 break;
18798 case DW_ATE_UTF:
18799 {
18800 type = init_character_type (objfile, bits, 1, name);
18801 return set_die_type (die, type, cu);
18802 }
18803 break;
18804 case DW_ATE_signed_fixed:
18805 type = init_fixed_point_type (objfile, bits, 0, name);
18806 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18807 break;
18808 case DW_ATE_unsigned_fixed:
18809 type = init_fixed_point_type (objfile, bits, 1, name);
18810 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18811 break;
18812
18813 default:
18814 complaint (_("unsupported DW_AT_encoding: '%s'"),
18815 dwarf_type_encoding_name (encoding));
18816 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18817 break;
18818 }
18819
18820 if (type->code () == TYPE_CODE_INT
18821 && name != nullptr
18822 && strcmp (name, "char") == 0)
18823 type->set_has_no_signedness (true);
18824
18825 maybe_set_alignment (cu, die, type);
18826
18827 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18828
18829 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18830 {
18831 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18832 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18833 {
18834 unsigned real_bit_size = attr->as_unsigned ();
18835 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18836 /* Only use the attributes if they make sense together. */
18837 if (attr == nullptr
18838 || (attr->as_unsigned () + real_bit_size
18839 <= 8 * TYPE_LENGTH (type)))
18840 {
18841 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18842 = real_bit_size;
18843 if (attr != nullptr)
18844 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18845 = attr->as_unsigned ();
18846 }
18847 }
18848 }
18849
18850 return set_die_type (die, type, cu);
18851 }
18852
18853 /* A helper function that returns the name of DIE, if it refers to a
18854 variable declaration. */
18855
18856 static const char *
18857 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18858 {
18859 if (die->tag != DW_TAG_variable)
18860 return nullptr;
18861
18862 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18863 if (attr == nullptr || !attr->as_boolean ())
18864 return nullptr;
18865
18866 attr = dwarf2_attr (die, DW_AT_name, cu);
18867 if (attr == nullptr)
18868 return nullptr;
18869 return attr->as_string ();
18870 }
18871
18872 /* Parse dwarf attribute if it's a block, reference or constant and put the
18873 resulting value of the attribute into struct bound_prop.
18874 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18875
18876 static int
18877 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18878 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18879 struct type *default_type)
18880 {
18881 struct dwarf2_property_baton *baton;
18882 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18883 struct objfile *objfile = per_objfile->objfile;
18884 struct obstack *obstack = &objfile->objfile_obstack;
18885
18886 gdb_assert (default_type != NULL);
18887
18888 if (attr == NULL || prop == NULL)
18889 return 0;
18890
18891 if (attr->form_is_block ())
18892 {
18893 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18894 baton->property_type = default_type;
18895 baton->locexpr.per_cu = cu->per_cu;
18896 baton->locexpr.per_objfile = per_objfile;
18897
18898 struct dwarf_block *block;
18899 if (attr->form == DW_FORM_data16)
18900 {
18901 size_t data_size = 16;
18902 block = XOBNEW (obstack, struct dwarf_block);
18903 block->size = (data_size
18904 + 2 /* Extra bytes for DW_OP and arg. */);
18905 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18906 data[0] = DW_OP_implicit_value;
18907 data[1] = data_size;
18908 memcpy (&data[2], attr->as_block ()->data, data_size);
18909 block->data = data;
18910 }
18911 else
18912 block = attr->as_block ();
18913
18914 baton->locexpr.size = block->size;
18915 baton->locexpr.data = block->data;
18916 switch (attr->name)
18917 {
18918 case DW_AT_string_length:
18919 baton->locexpr.is_reference = true;
18920 break;
18921 default:
18922 baton->locexpr.is_reference = false;
18923 break;
18924 }
18925
18926 prop->set_locexpr (baton);
18927 gdb_assert (prop->baton () != NULL);
18928 }
18929 else if (attr->form_is_ref ())
18930 {
18931 struct dwarf2_cu *target_cu = cu;
18932 struct die_info *target_die;
18933 struct attribute *target_attr;
18934
18935 target_die = follow_die_ref (die, attr, &target_cu);
18936 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18937 if (target_attr == NULL)
18938 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18939 target_cu);
18940 if (target_attr == nullptr)
18941 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18942 target_cu);
18943 if (target_attr == NULL)
18944 {
18945 const char *name = var_decl_name (target_die, target_cu);
18946 if (name != nullptr)
18947 {
18948 prop->set_variable_name (name);
18949 return 1;
18950 }
18951 return 0;
18952 }
18953
18954 switch (target_attr->name)
18955 {
18956 case DW_AT_location:
18957 if (target_attr->form_is_section_offset ())
18958 {
18959 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18960 baton->property_type = die_type (target_die, target_cu);
18961 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18962 prop->set_loclist (baton);
18963 gdb_assert (prop->baton () != NULL);
18964 }
18965 else if (target_attr->form_is_block ())
18966 {
18967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18968 baton->property_type = die_type (target_die, target_cu);
18969 baton->locexpr.per_cu = cu->per_cu;
18970 baton->locexpr.per_objfile = per_objfile;
18971 struct dwarf_block *block = target_attr->as_block ();
18972 baton->locexpr.size = block->size;
18973 baton->locexpr.data = block->data;
18974 baton->locexpr.is_reference = true;
18975 prop->set_locexpr (baton);
18976 gdb_assert (prop->baton () != NULL);
18977 }
18978 else
18979 {
18980 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18981 "dynamic property");
18982 return 0;
18983 }
18984 break;
18985 case DW_AT_data_member_location:
18986 case DW_AT_data_bit_offset:
18987 {
18988 LONGEST offset;
18989
18990 if (!handle_member_location (target_die, target_cu, &offset))
18991 return 0;
18992
18993 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18994 baton->property_type = read_type_die (target_die->parent,
18995 target_cu);
18996 baton->offset_info.offset = offset;
18997 baton->offset_info.type = die_type (target_die, target_cu);
18998 prop->set_addr_offset (baton);
18999 break;
19000 }
19001 }
19002 }
19003 else if (attr->form_is_constant ())
19004 prop->set_const_val (attr->constant_value (0));
19005 else if (attr->form_is_section_offset ())
19006 {
19007 switch (attr->name)
19008 {
19009 case DW_AT_string_length:
19010 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19011 baton->property_type = default_type;
19012 fill_in_loclist_baton (cu, &baton->loclist, attr);
19013 prop->set_loclist (baton);
19014 gdb_assert (prop->baton () != NULL);
19015 break;
19016 default:
19017 goto invalid;
19018 }
19019 }
19020 else
19021 goto invalid;
19022
19023 return 1;
19024
19025 invalid:
19026 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
19027 dwarf2_name (die, cu));
19028 return 0;
19029 }
19030
19031 /* See read.h. */
19032
19033 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19034 present (which is valid) then compute the default type based on the
19035 compilation units address size. */
19036
19037 static struct type *
19038 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19039 {
19040 struct type *index_type = die_type (die, cu);
19041
19042 /* Dwarf-2 specifications explicitly allows to create subrange types
19043 without specifying a base type.
19044 In that case, the base type must be set to the type of
19045 the lower bound, upper bound or count, in that order, if any of these
19046 three attributes references an object that has a type.
19047 If no base type is found, the Dwarf-2 specifications say that
19048 a signed integer type of size equal to the size of an address should
19049 be used.
19050 For the following C code: `extern char gdb_int [];'
19051 GCC produces an empty range DIE.
19052 FIXME: muller/2010-05-28: Possible references to object for low bound,
19053 high bound or count are not yet handled by this code. */
19054 if (index_type->code () == TYPE_CODE_VOID)
19055 index_type = cu->addr_sized_int_type (false);
19056
19057 return index_type;
19058 }
19059
19060 /* Read the given DW_AT_subrange DIE. */
19061
19062 static struct type *
19063 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19064 {
19065 struct type *base_type, *orig_base_type;
19066 struct type *range_type;
19067 struct attribute *attr;
19068 struct dynamic_prop low, high;
19069 int low_default_is_valid;
19070 int high_bound_is_count = 0;
19071 const char *name;
19072 ULONGEST negative_mask;
19073
19074 orig_base_type = read_subrange_index_type (die, cu);
19075
19076 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19077 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19078 creating the range type, but we use the result of check_typedef
19079 when examining properties of the type. */
19080 base_type = check_typedef (orig_base_type);
19081
19082 /* The die_type call above may have already set the type for this DIE. */
19083 range_type = get_die_type (die, cu);
19084 if (range_type)
19085 return range_type;
19086
19087 high.set_const_val (0);
19088
19089 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19090 omitting DW_AT_lower_bound. */
19091 switch (cu->per_cu->lang)
19092 {
19093 case language_c:
19094 case language_cplus:
19095 low.set_const_val (0);
19096 low_default_is_valid = 1;
19097 break;
19098 case language_fortran:
19099 low.set_const_val (1);
19100 low_default_is_valid = 1;
19101 break;
19102 case language_d:
19103 case language_objc:
19104 case language_rust:
19105 low.set_const_val (0);
19106 low_default_is_valid = (cu->header.version >= 4);
19107 break;
19108 case language_ada:
19109 case language_m2:
19110 case language_pascal:
19111 low.set_const_val (1);
19112 low_default_is_valid = (cu->header.version >= 4);
19113 break;
19114 default:
19115 low.set_const_val (0);
19116 low_default_is_valid = 0;
19117 break;
19118 }
19119
19120 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19121 if (attr != nullptr)
19122 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19123 else if (!low_default_is_valid)
19124 complaint (_("Missing DW_AT_lower_bound "
19125 "- DIE at %s [in module %s]"),
19126 sect_offset_str (die->sect_off),
19127 objfile_name (cu->per_objfile->objfile));
19128
19129 struct attribute *attr_ub, *attr_count;
19130 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19131 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19132 {
19133 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19134 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19135 {
19136 /* If bounds are constant do the final calculation here. */
19137 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19138 high.set_const_val (low.const_val () + high.const_val () - 1);
19139 else
19140 high_bound_is_count = 1;
19141 }
19142 else
19143 {
19144 if (attr_ub != NULL)
19145 complaint (_("Unresolved DW_AT_upper_bound "
19146 "- DIE at %s [in module %s]"),
19147 sect_offset_str (die->sect_off),
19148 objfile_name (cu->per_objfile->objfile));
19149 if (attr_count != NULL)
19150 complaint (_("Unresolved DW_AT_count "
19151 "- DIE at %s [in module %s]"),
19152 sect_offset_str (die->sect_off),
19153 objfile_name (cu->per_objfile->objfile));
19154 }
19155 }
19156
19157 LONGEST bias = 0;
19158 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19159 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19160 bias = bias_attr->constant_value (0);
19161
19162 /* Normally, the DWARF producers are expected to use a signed
19163 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19164 But this is unfortunately not always the case, as witnessed
19165 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19166 is used instead. To work around that ambiguity, we treat
19167 the bounds as signed, and thus sign-extend their values, when
19168 the base type is signed. */
19169 negative_mask =
19170 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19171 if (low.kind () == PROP_CONST
19172 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19173 low.set_const_val (low.const_val () | negative_mask);
19174 if (high.kind () == PROP_CONST
19175 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19176 high.set_const_val (high.const_val () | negative_mask);
19177
19178 /* Check for bit and byte strides. */
19179 struct dynamic_prop byte_stride_prop;
19180 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19181 if (attr_byte_stride != nullptr)
19182 {
19183 struct type *prop_type = cu->addr_sized_int_type (false);
19184 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19185 prop_type);
19186 }
19187
19188 struct dynamic_prop bit_stride_prop;
19189 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19190 if (attr_bit_stride != nullptr)
19191 {
19192 /* It only makes sense to have either a bit or byte stride. */
19193 if (attr_byte_stride != nullptr)
19194 {
19195 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19196 "- DIE at %s [in module %s]"),
19197 sect_offset_str (die->sect_off),
19198 objfile_name (cu->per_objfile->objfile));
19199 attr_bit_stride = nullptr;
19200 }
19201 else
19202 {
19203 struct type *prop_type = cu->addr_sized_int_type (false);
19204 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19205 prop_type);
19206 }
19207 }
19208
19209 if (attr_byte_stride != nullptr
19210 || attr_bit_stride != nullptr)
19211 {
19212 bool byte_stride_p = (attr_byte_stride != nullptr);
19213 struct dynamic_prop *stride
19214 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19215
19216 range_type
19217 = create_range_type_with_stride (NULL, orig_base_type, &low,
19218 &high, bias, stride, byte_stride_p);
19219 }
19220 else
19221 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19222
19223 if (high_bound_is_count)
19224 range_type->bounds ()->flag_upper_bound_is_count = 1;
19225
19226 /* Ada expects an empty array on no boundary attributes. */
19227 if (attr == NULL && cu->per_cu->lang != language_ada)
19228 range_type->bounds ()->high.set_undefined ();
19229
19230 name = dwarf2_name (die, cu);
19231 if (name)
19232 range_type->set_name (name);
19233
19234 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19235 if (attr != nullptr)
19236 TYPE_LENGTH (range_type) = attr->constant_value (0);
19237
19238 maybe_set_alignment (cu, die, range_type);
19239
19240 set_die_type (die, range_type, cu);
19241
19242 /* set_die_type should be already done. */
19243 set_descriptive_type (range_type, die, cu);
19244
19245 return range_type;
19246 }
19247
19248 static struct type *
19249 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19250 {
19251 struct type *type;
19252
19253 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19254 type->set_name (dwarf2_name (die, cu));
19255
19256 /* In Ada, an unspecified type is typically used when the description
19257 of the type is deferred to a different unit. When encountering
19258 such a type, we treat it as a stub, and try to resolve it later on,
19259 when needed. */
19260 if (cu->per_cu->lang == language_ada)
19261 type->set_is_stub (true);
19262
19263 return set_die_type (die, type, cu);
19264 }
19265
19266 /* Read a single die and all its descendents. Set the die's sibling
19267 field to NULL; set other fields in the die correctly, and set all
19268 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19269 location of the info_ptr after reading all of those dies. PARENT
19270 is the parent of the die in question. */
19271
19272 static struct die_info *
19273 read_die_and_children (const struct die_reader_specs *reader,
19274 const gdb_byte *info_ptr,
19275 const gdb_byte **new_info_ptr,
19276 struct die_info *parent)
19277 {
19278 struct die_info *die;
19279 const gdb_byte *cur_ptr;
19280
19281 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19282 if (die == NULL)
19283 {
19284 *new_info_ptr = cur_ptr;
19285 return NULL;
19286 }
19287 store_in_ref_table (die, reader->cu);
19288
19289 if (die->has_children)
19290 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19291 else
19292 {
19293 die->child = NULL;
19294 *new_info_ptr = cur_ptr;
19295 }
19296
19297 die->sibling = NULL;
19298 die->parent = parent;
19299 return die;
19300 }
19301
19302 /* Read a die, all of its descendents, and all of its siblings; set
19303 all of the fields of all of the dies correctly. Arguments are as
19304 in read_die_and_children. */
19305
19306 static struct die_info *
19307 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19308 const gdb_byte *info_ptr,
19309 const gdb_byte **new_info_ptr,
19310 struct die_info *parent)
19311 {
19312 struct die_info *first_die, *last_sibling;
19313 const gdb_byte *cur_ptr;
19314
19315 cur_ptr = info_ptr;
19316 first_die = last_sibling = NULL;
19317
19318 while (1)
19319 {
19320 struct die_info *die
19321 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19322
19323 if (die == NULL)
19324 {
19325 *new_info_ptr = cur_ptr;
19326 return first_die;
19327 }
19328
19329 if (!first_die)
19330 first_die = die;
19331 else
19332 last_sibling->sibling = die;
19333
19334 last_sibling = die;
19335 }
19336 }
19337
19338 /* Read a die, all of its descendents, and all of its siblings; set
19339 all of the fields of all of the dies correctly. Arguments are as
19340 in read_die_and_children.
19341 This the main entry point for reading a DIE and all its children. */
19342
19343 static struct die_info *
19344 read_die_and_siblings (const struct die_reader_specs *reader,
19345 const gdb_byte *info_ptr,
19346 const gdb_byte **new_info_ptr,
19347 struct die_info *parent)
19348 {
19349 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19350 new_info_ptr, parent);
19351
19352 if (dwarf_die_debug)
19353 {
19354 gdb_printf (gdb_stdlog,
19355 "Read die from %s@0x%x of %s:\n",
19356 reader->die_section->get_name (),
19357 (unsigned) (info_ptr - reader->die_section->buffer),
19358 bfd_get_filename (reader->abfd));
19359 dump_die (die, dwarf_die_debug);
19360 }
19361
19362 return die;
19363 }
19364
19365 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19366 attributes.
19367 The caller is responsible for filling in the extra attributes
19368 and updating (*DIEP)->num_attrs.
19369 Set DIEP to point to a newly allocated die with its information,
19370 except for its child, sibling, and parent fields. */
19371
19372 static const gdb_byte *
19373 read_full_die_1 (const struct die_reader_specs *reader,
19374 struct die_info **diep, const gdb_byte *info_ptr,
19375 int num_extra_attrs)
19376 {
19377 unsigned int abbrev_number, bytes_read, i;
19378 const struct abbrev_info *abbrev;
19379 struct die_info *die;
19380 struct dwarf2_cu *cu = reader->cu;
19381 bfd *abfd = reader->abfd;
19382
19383 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19384 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19385 info_ptr += bytes_read;
19386 if (!abbrev_number)
19387 {
19388 *diep = NULL;
19389 return info_ptr;
19390 }
19391
19392 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19393 if (!abbrev)
19394 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19395 abbrev_number,
19396 bfd_get_filename (abfd));
19397
19398 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19399 die->sect_off = sect_off;
19400 die->tag = abbrev->tag;
19401 die->abbrev = abbrev_number;
19402 die->has_children = abbrev->has_children;
19403
19404 /* Make the result usable.
19405 The caller needs to update num_attrs after adding the extra
19406 attributes. */
19407 die->num_attrs = abbrev->num_attrs;
19408
19409 bool any_need_reprocess = false;
19410 for (i = 0; i < abbrev->num_attrs; ++i)
19411 {
19412 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19413 info_ptr);
19414 if (die->attrs[i].requires_reprocessing_p ())
19415 any_need_reprocess = true;
19416 }
19417
19418 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19419 if (attr != nullptr && attr->form_is_unsigned ())
19420 cu->str_offsets_base = attr->as_unsigned ();
19421
19422 attr = die->attr (DW_AT_loclists_base);
19423 if (attr != nullptr)
19424 cu->loclist_base = attr->as_unsigned ();
19425
19426 auto maybe_addr_base = die->addr_base ();
19427 if (maybe_addr_base.has_value ())
19428 cu->addr_base = *maybe_addr_base;
19429
19430 attr = die->attr (DW_AT_rnglists_base);
19431 if (attr != nullptr)
19432 cu->rnglists_base = attr->as_unsigned ();
19433
19434 if (any_need_reprocess)
19435 {
19436 for (i = 0; i < abbrev->num_attrs; ++i)
19437 {
19438 if (die->attrs[i].requires_reprocessing_p ())
19439 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19440 }
19441 }
19442 *diep = die;
19443 return info_ptr;
19444 }
19445
19446 /* Read a die and all its attributes.
19447 Set DIEP to point to a newly allocated die with its information,
19448 except for its child, sibling, and parent fields. */
19449
19450 static const gdb_byte *
19451 read_full_die (const struct die_reader_specs *reader,
19452 struct die_info **diep, const gdb_byte *info_ptr)
19453 {
19454 const gdb_byte *result;
19455
19456 result = read_full_die_1 (reader, diep, info_ptr, 0);
19457
19458 if (dwarf_die_debug)
19459 {
19460 gdb_printf (gdb_stdlog,
19461 "Read die from %s@0x%x of %s:\n",
19462 reader->die_section->get_name (),
19463 (unsigned) (info_ptr - reader->die_section->buffer),
19464 bfd_get_filename (reader->abfd));
19465 dump_die (*diep, dwarf_die_debug);
19466 }
19467
19468 return result;
19469 }
19470 \f
19471
19472 void
19473 cooked_indexer::check_bounds (cutu_reader *reader)
19474 {
19475 if (reader->cu->per_cu->addresses_seen)
19476 return;
19477
19478 dwarf2_cu *cu = reader->cu;
19479
19480 CORE_ADDR best_lowpc = 0, best_highpc = 0;
19481 /* Possibly set the default values of LOWPC and HIGHPC from
19482 `DW_AT_ranges'. */
19483 dwarf2_find_base_address (reader->comp_unit_die, cu);
19484 enum pc_bounds_kind cu_bounds_kind
19485 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
19486 cu, m_index_storage->get_addrmap (), cu->per_cu);
19487 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
19488 {
19489 struct objfile *objfile = cu->per_objfile->objfile;
19490 CORE_ADDR baseaddr = objfile->text_section_offset ();
19491 struct gdbarch *gdbarch = objfile->arch ();
19492 CORE_ADDR low
19493 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
19494 - baseaddr);
19495 CORE_ADDR high
19496 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
19497 - baseaddr - 1);
19498 /* Store the contiguous range if it is not empty; it can be
19499 empty for CUs with no code. */
19500 addrmap_set_empty (m_index_storage->get_addrmap (), low, high,
19501 cu->per_cu);
19502
19503 cu->per_cu->addresses_seen = true;
19504 }
19505 }
19506
19507 /* Helper function that returns true if TAG can have a linkage
19508 name. */
19509
19510 static bool
19511 tag_can_have_linkage_name (enum dwarf_tag tag)
19512 {
19513 switch (tag)
19514 {
19515 /* We include types here because an anonymous C++ type might
19516 have a name for linkage purposes. */
19517 case DW_TAG_class_type:
19518 case DW_TAG_structure_type:
19519 case DW_TAG_union_type:
19520 case DW_TAG_variable:
19521 case DW_TAG_subprogram:
19522 return true;
19523
19524 default:
19525 return false;
19526 }
19527 }
19528
19529 cutu_reader *
19530 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
19531 dwarf2_per_objfile *per_objfile,
19532 sect_offset sect_off, bool is_dwz,
19533 bool for_scanning)
19534 {
19535 /* Lookups for type unit references are always in the CU, and
19536 cross-CU references will crash. */
19537 if (reader->cu->per_cu->is_dwz == is_dwz
19538 && reader->cu->header.offset_in_cu_p (sect_off))
19539 return reader;
19540
19541 dwarf2_per_cu_data *per_cu
19542 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
19543 per_objfile->per_bfd);
19544
19545 /* When scanning, we only want to visit a given CU a single time.
19546 Doing this check here avoids self-imports as well. */
19547 if (for_scanning)
19548 {
19549 bool nope = false;
19550 if (!per_cu->scanned.compare_exchange_strong (nope, true))
19551 return nullptr;
19552 }
19553 if (per_cu == m_per_cu)
19554 return reader;
19555
19556 cutu_reader *result = m_index_storage->get_reader (per_cu);
19557 if (result == nullptr)
19558 {
19559 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
19560 m_index_storage->get_abbrev_cache ());
19561
19562 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
19563 language_minimal);
19564 std::unique_ptr<cutu_reader> copy
19565 (new cutu_reader (std::move (new_reader)));
19566 result = m_index_storage->preserve (std::move (copy));
19567 }
19568
19569 if (result->dummy_p || !result->comp_unit_die->has_children)
19570 return nullptr;
19571
19572 if (for_scanning)
19573 check_bounds (result);
19574
19575 return result;
19576 }
19577
19578 const gdb_byte *
19579 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
19580 cutu_reader *reader,
19581 const gdb_byte *watermark_ptr,
19582 const gdb_byte *info_ptr,
19583 const abbrev_info *abbrev,
19584 const char **name,
19585 const char **linkage_name,
19586 cooked_index_flag *flags,
19587 sect_offset *sibling_offset,
19588 const cooked_index_entry **parent_entry,
19589 CORE_ADDR *maybe_defer,
19590 bool for_specification)
19591 {
19592 bool origin_is_dwz = false;
19593 bool is_declaration = false;
19594 sect_offset origin_offset {};
19595
19596 gdb::optional<CORE_ADDR> low_pc;
19597 gdb::optional<CORE_ADDR> high_pc;
19598 bool high_pc_relative = false;
19599
19600 for (int i = 0; i < abbrev->num_attrs; ++i)
19601 {
19602 attribute attr;
19603 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
19604 if (attr.requires_reprocessing_p ())
19605 read_attribute_reprocess (reader, &attr, abbrev->tag);
19606
19607 /* Store the data if it is of an attribute we want to keep in a
19608 partial symbol table. */
19609 switch (attr.name)
19610 {
19611 case DW_AT_name:
19612 switch (abbrev->tag)
19613 {
19614 case DW_TAG_compile_unit:
19615 case DW_TAG_partial_unit:
19616 case DW_TAG_type_unit:
19617 /* Compilation units have a DW_AT_name that is a filename, not
19618 a source language identifier. */
19619 break;
19620
19621 default:
19622 if (*name == nullptr)
19623 *name = attr.as_string ();
19624 break;
19625 }
19626 break;
19627
19628 case DW_AT_linkage_name:
19629 case DW_AT_MIPS_linkage_name:
19630 /* Note that both forms of linkage name might appear. We
19631 assume they will be the same, and we only store the last
19632 one we see. */
19633 if (*linkage_name == nullptr)
19634 *linkage_name = attr.as_string ();
19635 break;
19636
19637 case DW_AT_main_subprogram:
19638 if (attr.as_boolean ())
19639 *flags |= IS_MAIN;
19640 break;
19641
19642 case DW_AT_declaration:
19643 is_declaration = attr.as_boolean ();
19644 break;
19645
19646 case DW_AT_sibling:
19647 if (sibling_offset != nullptr)
19648 *sibling_offset = attr.get_ref_die_offset ();
19649 break;
19650
19651 case DW_AT_specification:
19652 case DW_AT_abstract_origin:
19653 case DW_AT_extension:
19654 origin_offset = attr.get_ref_die_offset ();
19655 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
19656 break;
19657
19658 case DW_AT_external:
19659 if (attr.as_boolean ())
19660 *flags &= ~IS_STATIC;
19661 break;
19662
19663 case DW_AT_enum_class:
19664 if (attr.as_boolean ())
19665 *flags |= IS_ENUM_CLASS;
19666 break;
19667
19668 case DW_AT_low_pc:
19669 low_pc = attr.as_address ();
19670 break;
19671
19672 case DW_AT_high_pc:
19673 high_pc = attr.as_address ();
19674 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
19675 high_pc_relative = true;
19676 break;
19677
19678 case DW_AT_location:
19679 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
19680 {
19681 struct dwarf_block *locdesc = attr.as_block ();
19682 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
19683 if (addr != 0
19684 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
19685 {
19686 low_pc = addr;
19687 /* For variables, we don't want to try decoding the
19688 type just to find the size -- for gdb's purposes
19689 we only need the address of a variable. */
19690 high_pc = addr + 1;
19691 high_pc_relative = false;
19692 }
19693 }
19694 break;
19695
19696 case DW_AT_ranges:
19697 if (!scanning_per_cu->addresses_seen)
19698 {
19699 /* Offset in the .debug_ranges or .debug_rnglist section
19700 (depending on DWARF version). */
19701 ULONGEST ranges_offset = attr.as_unsigned ();
19702
19703 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
19704 want to add this value. */
19705 ranges_offset += reader->cu->gnu_ranges_base;
19706
19707 CORE_ADDR lowpc, highpc;
19708 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
19709 m_index_storage->get_addrmap (),
19710 scanning_per_cu, abbrev->tag);
19711 }
19712 break;
19713 }
19714 }
19715
19716 /* We don't want to examine declarations, but if we found a
19717 declaration when handling DW_AT_specification or the like, then
19718 that is ok. Similarly, we allow an external variable without a
19719 location; those are resolved via minimal symbols. */
19720 if (is_declaration && !for_specification
19721 && (abbrev->tag != DW_TAG_variable
19722 || (*flags & IS_STATIC) != 0))
19723 {
19724 *linkage_name = nullptr;
19725 *name = nullptr;
19726 }
19727 else if ((*name == nullptr
19728 || (*linkage_name == nullptr
19729 && tag_can_have_linkage_name (abbrev->tag))
19730 || (*parent_entry == nullptr && m_language != language_c))
19731 && origin_offset != sect_offset (0))
19732 {
19733 cutu_reader *new_reader
19734 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
19735 origin_is_dwz, false);
19736 if (new_reader != nullptr)
19737 {
19738 const gdb_byte *new_info_ptr = (new_reader->buffer
19739 + to_underlying (origin_offset));
19740
19741 if (new_reader->cu == reader->cu
19742 && new_info_ptr > watermark_ptr
19743 && maybe_defer != nullptr
19744 && *parent_entry == nullptr)
19745 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
19746 else if (*parent_entry == nullptr)
19747 {
19748 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
19749 *parent_entry
19750 = (cooked_index_entry *) addrmap_find (m_die_range_map,
19751 lookup);
19752 }
19753
19754 unsigned int bytes_read;
19755 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
19756 new_info_ptr,
19757 &bytes_read);
19758 new_info_ptr += bytes_read;
19759 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
19760 new_abbrev, name, linkage_name, flags, nullptr,
19761 parent_entry, maybe_defer, true);
19762 }
19763 }
19764
19765 if (!for_specification)
19766 {
19767 if (m_language == language_ada
19768 && *linkage_name == nullptr)
19769 *linkage_name = *name;
19770
19771 if (!scanning_per_cu->addresses_seen
19772 && low_pc.has_value ()
19773 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
19774 || *low_pc != 0)
19775 && high_pc.has_value ())
19776 {
19777 if (high_pc_relative)
19778 high_pc = *high_pc + *low_pc;
19779
19780 if (*high_pc > *low_pc)
19781 {
19782 struct objfile *objfile = reader->cu->per_objfile->objfile;
19783 CORE_ADDR baseaddr = objfile->text_section_offset ();
19784 struct gdbarch *gdbarch = objfile->arch ();
19785 CORE_ADDR lo
19786 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
19787 - baseaddr);
19788 CORE_ADDR hi
19789 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
19790 - baseaddr);
19791 addrmap_set_empty (m_index_storage->get_addrmap (), lo, hi - 1,
19792 scanning_per_cu);
19793 }
19794 }
19795
19796 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
19797 *flags &= ~IS_STATIC;
19798
19799 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
19800 *name = "(anonymous namespace)";
19801
19802 if (m_language == language_cplus
19803 && (abbrev->tag == DW_TAG_class_type
19804 || abbrev->tag == DW_TAG_interface_type
19805 || abbrev->tag == DW_TAG_structure_type
19806 || abbrev->tag == DW_TAG_union_type
19807 || abbrev->tag == DW_TAG_enumeration_type
19808 || abbrev->tag == DW_TAG_enumerator))
19809 *flags &= ~IS_STATIC;
19810 }
19811
19812 return info_ptr;
19813 }
19814
19815 const gdb_byte *
19816 cooked_indexer::index_imported_unit (cutu_reader *reader,
19817 const gdb_byte *info_ptr,
19818 const abbrev_info *abbrev)
19819 {
19820 sect_offset sect_off {};
19821 bool is_dwz = false;
19822
19823 for (int i = 0; i < abbrev->num_attrs; ++i)
19824 {
19825 /* Note that we never need to reprocess attributes here. */
19826 attribute attr;
19827 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
19828
19829 if (attr.name == DW_AT_import)
19830 {
19831 sect_off = attr.get_ref_die_offset ();
19832 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19833 || reader->cu->per_cu->is_dwz);
19834 }
19835 }
19836
19837 /* Did not find DW_AT_import. */
19838 if (sect_off == sect_offset (0))
19839 return info_ptr;
19840
19841 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
19842 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
19843 is_dwz, true);
19844 if (new_reader != nullptr)
19845 {
19846 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
19847
19848 reader->cu->add_dependence (new_reader->cu->per_cu);
19849 }
19850
19851 return info_ptr;
19852 }
19853
19854 const gdb_byte *
19855 cooked_indexer::recurse (cutu_reader *reader,
19856 const gdb_byte *info_ptr,
19857 const cooked_index_entry *parent_entry,
19858 bool fully)
19859 {
19860 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
19861
19862 if (parent_entry != nullptr)
19863 {
19864 CORE_ADDR start = form_addr (parent_entry->die_offset,
19865 reader->cu->per_cu->is_dwz);
19866 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
19867 reader->cu->per_cu->is_dwz);
19868 addrmap_set_empty (m_die_range_map, start, end, (void *) parent_entry);
19869 }
19870
19871 return info_ptr;
19872 }
19873
19874 const gdb_byte *
19875 cooked_indexer::index_dies (cutu_reader *reader,
19876 const gdb_byte *info_ptr,
19877 const cooked_index_entry *parent_entry,
19878 bool fully)
19879 {
19880 const gdb_byte *end_ptr = info_ptr + reader->cu->header.get_length ();
19881
19882 while (info_ptr < end_ptr)
19883 {
19884 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
19885 unsigned int bytes_read;
19886 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19887 &bytes_read);
19888 info_ptr += bytes_read;
19889 if (abbrev == nullptr)
19890 break;
19891
19892 if (abbrev->tag == DW_TAG_imported_unit)
19893 {
19894 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
19895 continue;
19896 }
19897
19898 if (!abbrev->interesting)
19899 {
19900 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
19901 if (fully && abbrev->has_children)
19902 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
19903 continue;
19904 }
19905
19906 const char *name = nullptr;
19907 const char *linkage_name = nullptr;
19908 CORE_ADDR defer = 0;
19909 cooked_index_flag flags = IS_STATIC;
19910 sect_offset sibling {};
19911 const cooked_index_entry *this_parent_entry = parent_entry;
19912 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
19913 info_ptr, abbrev, &name, &linkage_name,
19914 &flags, &sibling, &this_parent_entry,
19915 &defer, false);
19916
19917 if (abbrev->tag == DW_TAG_namespace
19918 && m_language == language_cplus
19919 && strcmp (name, "::") == 0)
19920 {
19921 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
19922 generated bogus DW_TAG_namespace DIEs with a name of "::"
19923 for the global namespace. Work around this problem
19924 here. */
19925 name = nullptr;
19926 }
19927
19928 const cooked_index_entry *this_entry = nullptr;
19929 if (name != nullptr)
19930 {
19931 if (defer != 0)
19932 m_deferred_entries.push_back ({
19933 this_die, name, defer, abbrev->tag, flags
19934 });
19935 else
19936 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
19937 name, this_parent_entry,
19938 m_per_cu);
19939 }
19940
19941 if (linkage_name != nullptr)
19942 {
19943 /* We only want this to be "main" if it has a linkage name
19944 but not an ordinary name. */
19945 if (name != nullptr)
19946 flags = flags & ~IS_MAIN;
19947 m_index_storage->add (this_die, abbrev->tag, flags | IS_LINKAGE,
19948 linkage_name, nullptr, m_per_cu);
19949 }
19950
19951 if (abbrev->has_children)
19952 {
19953 switch (abbrev->tag)
19954 {
19955 case DW_TAG_class_type:
19956 case DW_TAG_interface_type:
19957 case DW_TAG_structure_type:
19958 case DW_TAG_union_type:
19959 if (m_language != language_c && this_entry != nullptr)
19960 {
19961 info_ptr = recurse (reader, info_ptr, this_entry, fully);
19962 continue;
19963 }
19964 break;
19965
19966 case DW_TAG_enumeration_type:
19967 /* We need to recurse even for an anonymous enumeration.
19968 Which scope we record as the parent scope depends on
19969 whether we're reading an "enum class". If so, we use
19970 the enum itself as the parent, yielding names like
19971 "enum_class::enumerator"; otherwise we inject the
19972 names into our own parent scope. */
19973 info_ptr = recurse (reader, info_ptr,
19974 ((flags & IS_ENUM_CLASS) == 0)
19975 ? parent_entry
19976 : this_entry,
19977 fully);
19978 continue;
19979
19980 case DW_TAG_module:
19981 if (this_entry == nullptr)
19982 break;
19983 /* FALLTHROUGH */
19984 case DW_TAG_namespace:
19985 /* We don't check THIS_ENTRY for a namespace, to handle
19986 the ancient G++ workaround pointed out above. */
19987 info_ptr = recurse (reader, info_ptr, this_entry, fully);
19988 continue;
19989
19990 case DW_TAG_subprogram:
19991 if ((m_language == language_fortran
19992 || m_language == language_ada)
19993 && this_entry != nullptr)
19994 {
19995 info_ptr = recurse (reader, info_ptr, this_entry, true);
19996 continue;
19997 }
19998 break;
19999 }
20000
20001 if (sibling != sect_offset (0))
20002 {
20003 const gdb_byte *sibling_ptr
20004 = reader->buffer + to_underlying (sibling);
20005
20006 if (sibling_ptr < info_ptr)
20007 complaint (_("DW_AT_sibling points backwards"));
20008 else if (sibling_ptr > reader->buffer_end)
20009 reader->die_section->overflow_complaint ();
20010 else
20011 info_ptr = sibling_ptr;
20012 }
20013 else
20014 info_ptr = skip_children (reader, info_ptr);
20015 }
20016 }
20017
20018 return info_ptr;
20019 }
20020
20021 void
20022 cooked_indexer::make_index (cutu_reader *reader)
20023 {
20024 check_bounds (reader);
20025 find_file_and_directory (reader->comp_unit_die, reader->cu);
20026 if (!reader->comp_unit_die->has_children)
20027 return;
20028 index_dies (reader, reader->info_ptr, nullptr, false);
20029
20030 for (const auto &entry : m_deferred_entries)
20031 {
20032 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
20033 cooked_index_entry *parent
20034 = (cooked_index_entry *) addrmap_find (m_die_range_map, key);
20035 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
20036 entry.name, parent, m_per_cu);
20037 }
20038 }
20039
20040 /* An implementation of quick_symbol_functions for the cooked DWARF
20041 index. */
20042
20043 struct cooked_index_functions : public dwarf2_base_index_functions
20044 {
20045 struct compunit_symtab *find_pc_sect_compunit_symtab
20046 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
20047 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
20048
20049 struct compunit_symtab *find_compunit_symtab_by_address
20050 (struct objfile *objfile, CORE_ADDR address) override;
20051
20052 void dump (struct objfile *objfile) override
20053 {
20054 gdb_printf ("Cooked index in use\n");
20055 }
20056
20057 void expand_matching_symbols
20058 (struct objfile *,
20059 const lookup_name_info &lookup_name,
20060 domain_enum domain,
20061 int global,
20062 symbol_compare_ftype *ordered_compare) override;
20063
20064 bool expand_symtabs_matching
20065 (struct objfile *objfile,
20066 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
20067 const lookup_name_info *lookup_name,
20068 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
20069 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
20070 block_search_flags search_flags,
20071 domain_enum domain,
20072 enum search_domain kind) override;
20073
20074 bool can_lazily_read_symbols () override
20075 {
20076 return true;
20077 }
20078
20079 void read_partial_symbols (struct objfile *objfile) override
20080 {
20081 if (dwarf2_has_info (objfile, nullptr))
20082 dwarf2_build_psymtabs (objfile);
20083 }
20084 };
20085
20086 struct compunit_symtab *
20087 cooked_index_functions::find_pc_sect_compunit_symtab
20088 (struct objfile *objfile,
20089 struct bound_minimal_symbol msymbol,
20090 CORE_ADDR pc,
20091 struct obj_section *section,
20092 int warn_if_readin)
20093 {
20094 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
20095 if (per_objfile->per_bfd->cooked_index_table == nullptr)
20096 return nullptr;
20097
20098 CORE_ADDR baseaddr = objfile->text_section_offset ();
20099 dwarf2_per_cu_data *per_cu
20100 = per_objfile->per_bfd->cooked_index_table->lookup (pc - baseaddr);
20101 if (per_cu == nullptr)
20102 return nullptr;
20103
20104 if (warn_if_readin && per_objfile->symtab_set_p (per_cu))
20105 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
20106 paddress (objfile->arch (), pc));
20107
20108 compunit_symtab *result = (recursively_find_pc_sect_compunit_symtab
20109 (dw2_instantiate_symtab (per_cu, per_objfile,
20110 false),
20111 pc));
20112 gdb_assert (result != nullptr);
20113 return result;
20114 }
20115
20116 struct compunit_symtab *
20117 cooked_index_functions::find_compunit_symtab_by_address
20118 (struct objfile *objfile, CORE_ADDR address)
20119 {
20120 if (objfile->sect_index_data == -1)
20121 return nullptr;
20122
20123 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
20124 if (per_objfile->per_bfd->cooked_index_table == nullptr)
20125 return nullptr;
20126
20127 CORE_ADDR baseaddr = objfile->data_section_offset ();
20128 dwarf2_per_cu_data *per_cu
20129 = per_objfile->per_bfd->cooked_index_table->lookup (address - baseaddr);
20130 if (per_cu == nullptr)
20131 return nullptr;
20132
20133 return dw2_instantiate_symtab (per_cu, per_objfile, false);
20134 }
20135
20136 void
20137 cooked_index_functions::expand_matching_symbols
20138 (struct objfile *objfile,
20139 const lookup_name_info &lookup_name,
20140 domain_enum domain,
20141 int global,
20142 symbol_compare_ftype *ordered_compare)
20143 {
20144 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
20145 if (per_objfile->per_bfd->cooked_index_table == nullptr)
20146 return;
20147 const block_search_flags search_flags = (global
20148 ? SEARCH_GLOBAL_BLOCK
20149 : SEARCH_STATIC_BLOCK);
20150 const language_defn *lang = language_def (language_ada);
20151 symbol_name_matcher_ftype *name_match
20152 = lang->get_symbol_name_matcher (lookup_name);
20153
20154 for (const cooked_index_entry *entry
20155 : per_objfile->per_bfd->cooked_index_table->all_entries ())
20156 {
20157 if (entry->parent_entry != nullptr)
20158 continue;
20159
20160 if (!entry->matches (search_flags)
20161 || !entry->matches (domain))
20162 continue;
20163
20164 if (name_match (entry->canonical, lookup_name, nullptr))
20165 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
20166 }
20167 }
20168
20169 bool
20170 cooked_index_functions::expand_symtabs_matching
20171 (struct objfile *objfile,
20172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
20173 const lookup_name_info *lookup_name,
20174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
20175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
20176 block_search_flags search_flags,
20177 domain_enum domain,
20178 enum search_domain kind)
20179 {
20180 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
20181 if (per_objfile->per_bfd->cooked_index_table == nullptr)
20182 return true;
20183
20184 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
20185
20186 /* This invariant is documented in quick-functions.h. */
20187 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
20188 if (lookup_name == nullptr)
20189 {
20190 for (dwarf2_per_cu_data *per_cu
20191 : all_comp_units_range (per_objfile->per_bfd))
20192 {
20193 QUIT;
20194
20195 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
20196 file_matcher,
20197 expansion_notify))
20198 return false;
20199 }
20200 return true;
20201 }
20202
20203 lookup_name_info lookup_name_without_params
20204 = lookup_name->make_ignore_params ();
20205 bool completing = lookup_name->completion_mode ();
20206
20207 /* Unique styles of language splitting. */
20208 static const enum language unique_styles[] =
20209 {
20210 /* No splitting is also a style. */
20211 language_c,
20212 /* This includes Rust. */
20213 language_cplus,
20214 /* This includes Go. */
20215 language_d,
20216 language_ada
20217 };
20218
20219 for (enum language lang : unique_styles)
20220 {
20221 std::vector<gdb::string_view> name_vec
20222 = lookup_name_without_params.split_name (lang);
20223
20224 for (const cooked_index_entry *entry
20225 : per_objfile->per_bfd->cooked_index_table->find (name_vec.back (),
20226 completing))
20227 {
20228 /* No need to consider symbols from expanded CUs. */
20229 if (per_objfile->symtab_set_p (entry->per_cu))
20230 continue;
20231
20232 /* If file-matching was done, we don't need to consider
20233 symbols from unmarked CUs. */
20234 if (file_matcher != nullptr && !entry->per_cu->v.quick->mark)
20235 continue;
20236
20237 /* See if the symbol matches the type filter. */
20238 if (!entry->matches (search_flags)
20239 || !entry->matches (domain)
20240 || !entry->matches (kind))
20241 continue;
20242
20243 /* We've found the base name of the symbol; now walk its
20244 parentage chain, ensuring that each component
20245 matches. */
20246 bool found = true;
20247
20248 const cooked_index_entry *parent = entry->parent_entry;
20249 for (int i = name_vec.size () - 1; i > 0; --i)
20250 {
20251 /* If we ran out of entries, or if this segment doesn't
20252 match, this did not match. */
20253 if (parent == nullptr
20254 || strncmp (parent->name, name_vec[i - 1].data (),
20255 name_vec[i - 1].length ()) != 0)
20256 {
20257 found = false;
20258 break;
20259 }
20260
20261 parent = parent->parent_entry;
20262 }
20263
20264 if (!found)
20265 continue;
20266
20267 /* Might have been looking for "a::b" and found
20268 "x::a::b". */
20269 if (symbol_matcher == nullptr)
20270 {
20271 symbol_name_match_type match_type
20272 = lookup_name_without_params.match_type ();
20273 if ((match_type == symbol_name_match_type::FULL
20274 || (lang != language_ada
20275 && match_type == symbol_name_match_type::EXPRESSION))
20276 && parent != nullptr)
20277 continue;
20278 }
20279 else
20280 {
20281 auto_obstack temp_storage;
20282 const char *full_name = entry->full_name (&temp_storage);
20283 if (!symbol_matcher (full_name))
20284 continue;
20285 }
20286
20287 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
20288 file_matcher,
20289 expansion_notify))
20290 return false;
20291 }
20292 }
20293
20294 return true;
20295 }
20296
20297 /* Return a new cooked_index_functions object. */
20298
20299 static quick_symbol_functions_up
20300 make_cooked_index_funcs ()
20301 {
20302 return quick_symbol_functions_up (new cooked_index_functions);
20303 }
20304
20305 \f
20306
20307 /* Returns nonzero if TAG represents a type that we might generate a partial
20308 symbol for. */
20309
20310 static int
20311 is_type_tag_for_partial (int tag, enum language lang)
20312 {
20313 switch (tag)
20314 {
20315 #if 0
20316 /* Some types that would be reasonable to generate partial symbols for,
20317 that we don't at present. Note that normally this does not
20318 matter, mainly because C compilers don't give names to these
20319 types, but instead emit DW_TAG_typedef. */
20320 case DW_TAG_file_type:
20321 case DW_TAG_ptr_to_member_type:
20322 case DW_TAG_set_type:
20323 case DW_TAG_string_type:
20324 case DW_TAG_subroutine_type:
20325 #endif
20326
20327 /* GNAT may emit an array with a name, but no typedef, so we
20328 need to make a symbol in this case. */
20329 case DW_TAG_array_type:
20330 return lang == language_ada;
20331
20332 case DW_TAG_base_type:
20333 case DW_TAG_class_type:
20334 case DW_TAG_interface_type:
20335 case DW_TAG_enumeration_type:
20336 case DW_TAG_structure_type:
20337 case DW_TAG_subrange_type:
20338 case DW_TAG_generic_subrange:
20339 case DW_TAG_typedef:
20340 case DW_TAG_union_type:
20341 return 1;
20342 default:
20343 return 0;
20344 }
20345 }
20346
20347 /* Load all DIEs that are interesting for partial symbols into memory. */
20348
20349 static struct partial_die_info *
20350 load_partial_dies (const struct die_reader_specs *reader,
20351 const gdb_byte *info_ptr, int building_psymtab)
20352 {
20353 struct dwarf2_cu *cu = reader->cu;
20354 struct objfile *objfile = cu->per_objfile->objfile;
20355 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
20356 unsigned int bytes_read;
20357 unsigned int load_all = 0;
20358 int nesting_level = 1;
20359
20360 parent_die = NULL;
20361 last_die = NULL;
20362
20363 gdb_assert (cu->per_cu != NULL);
20364 if (cu->load_all_dies)
20365 load_all = 1;
20366
20367 cu->partial_dies
20368 = htab_create_alloc_ex (cu->header.length / 12,
20369 partial_die_hash,
20370 partial_die_eq,
20371 NULL,
20372 &cu->comp_unit_obstack,
20373 hashtab_obstack_allocate,
20374 dummy_obstack_deallocate);
20375
20376 while (1)
20377 {
20378 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
20379 &bytes_read);
20380
20381 /* A NULL abbrev means the end of a series of children. */
20382 if (abbrev == NULL)
20383 {
20384 if (--nesting_level == 0)
20385 return first_die;
20386
20387 info_ptr += bytes_read;
20388 last_die = parent_die;
20389 parent_die = parent_die->die_parent;
20390 continue;
20391 }
20392
20393 /* Check for template arguments. We never save these; if
20394 they're seen, we just mark the parent, and go on our way. */
20395 if (parent_die != NULL
20396 && cu->per_cu->lang == language_cplus
20397 && (abbrev->tag == DW_TAG_template_type_param
20398 || abbrev->tag == DW_TAG_template_value_param))
20399 {
20400 parent_die->has_template_arguments = 1;
20401
20402 if (!load_all)
20403 {
20404 /* We don't need a partial DIE for the template argument. */
20405 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
20406 continue;
20407 }
20408 }
20409
20410 /* We only recurse into c++ subprograms looking for template arguments.
20411 Skip their other children. */
20412 if (!load_all
20413 && cu->per_cu->lang == language_cplus
20414 && parent_die != NULL
20415 && parent_die->tag == DW_TAG_subprogram
20416 && abbrev->tag != DW_TAG_inlined_subroutine)
20417 {
20418 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
20419 continue;
20420 }
20421
20422 /* Check whether this DIE is interesting enough to save. Normally
20423 we would not be interested in members here, but there may be
20424 later variables referencing them via DW_AT_specification (for
20425 static members). */
20426 if (!load_all
20427 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
20428 && abbrev->tag != DW_TAG_constant
20429 && abbrev->tag != DW_TAG_enumerator
20430 && abbrev->tag != DW_TAG_subprogram
20431 && abbrev->tag != DW_TAG_inlined_subroutine
20432 && abbrev->tag != DW_TAG_lexical_block
20433 && abbrev->tag != DW_TAG_variable
20434 && abbrev->tag != DW_TAG_namespace
20435 && abbrev->tag != DW_TAG_module
20436 && abbrev->tag != DW_TAG_member
20437 && abbrev->tag != DW_TAG_imported_unit
20438 && abbrev->tag != DW_TAG_imported_declaration)
20439 {
20440 /* Otherwise we skip to the next sibling, if any. */
20441 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
20442 continue;
20443 }
20444
20445 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
20446 abbrev);
20447
20448 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
20449
20450 /* This two-pass algorithm for processing partial symbols has a
20451 high cost in cache pressure. Thus, handle some simple cases
20452 here which cover the majority of C partial symbols. DIEs
20453 which neither have specification tags in them, nor could have
20454 specification tags elsewhere pointing at them, can simply be
20455 processed and discarded.
20456
20457 This segment is also optional; scan_partial_symbols and
20458 add_partial_symbol will handle these DIEs if we chain
20459 them in normally. When compilers which do not emit large
20460 quantities of duplicate debug information are more common,
20461 this code can probably be removed. */
20462
20463 /* Any complete simple types at the top level (pretty much all
20464 of them, for a language without namespaces), can be processed
20465 directly. */
20466 if (parent_die == NULL
20467 && pdi.has_specification == 0
20468 && pdi.is_declaration == 0
20469 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
20470 || pdi.tag == DW_TAG_base_type
20471 || pdi.tag == DW_TAG_array_type
20472 || pdi.tag == DW_TAG_generic_subrange
20473 || pdi.tag == DW_TAG_subrange_type))
20474 {
20475 if (building_psymtab && pdi.raw_name != NULL)
20476 add_partial_symbol (&pdi, cu);
20477
20478 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
20479 continue;
20480 }
20481
20482 /* The exception for DW_TAG_typedef with has_children above is
20483 a workaround of GCC PR debug/47510. In the case of this complaint
20484 type_name_or_error will error on such types later.
20485
20486 GDB skipped children of DW_TAG_typedef by the shortcut above and then
20487 it could not find the child DIEs referenced later, this is checked
20488 above. In correct DWARF DW_TAG_typedef should have no children. */
20489
20490 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
20491 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
20492 "- DIE at %s [in module %s]"),
20493 sect_offset_str (pdi.sect_off), objfile_name (objfile));
20494
20495 /* If we're at the second level, and we're an enumerator, and
20496 our parent has no specification (meaning possibly lives in a
20497 namespace elsewhere), then we can add the partial symbol now
20498 instead of queueing it. */
20499 if (pdi.tag == DW_TAG_enumerator
20500 && parent_die != NULL
20501 && parent_die->die_parent == NULL
20502 && parent_die->tag == DW_TAG_enumeration_type
20503 && parent_die->has_specification == 0)
20504 {
20505 if (pdi.raw_name == NULL)
20506 complaint (_("malformed enumerator DIE ignored"));
20507 else if (building_psymtab)
20508 add_partial_symbol (&pdi, cu);
20509
20510 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
20511 continue;
20512 }
20513
20514 struct partial_die_info *part_die
20515 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
20516
20517 /* We'll save this DIE so link it in. */
20518 part_die->die_parent = parent_die;
20519 part_die->die_sibling = NULL;
20520 part_die->die_child = NULL;
20521
20522 if (last_die && last_die == parent_die)
20523 last_die->die_child = part_die;
20524 else if (last_die)
20525 last_die->die_sibling = part_die;
20526
20527 last_die = part_die;
20528
20529 if (first_die == NULL)
20530 first_die = part_die;
20531
20532 /* Maybe add the DIE to the hash table. Not all DIEs that we
20533 find interesting need to be in the hash table, because we
20534 also have the parent/sibling/child chains; only those that we
20535 might refer to by offset later during partial symbol reading.
20536
20537 For now this means things that might have be the target of a
20538 DW_AT_specification, DW_AT_abstract_origin, or
20539 DW_AT_extension. DW_AT_extension will refer only to
20540 namespaces; DW_AT_abstract_origin refers to functions (and
20541 many things under the function DIE, but we do not recurse
20542 into function DIEs during partial symbol reading) and
20543 possibly variables as well; DW_AT_specification refers to
20544 declarations. Declarations ought to have the DW_AT_declaration
20545 flag. It happens that GCC forgets to put it in sometimes, but
20546 only for functions, not for types.
20547
20548 Adding more things than necessary to the hash table is harmless
20549 except for the performance cost. Adding too few will result in
20550 wasted time in find_partial_die, when we reread the compilation
20551 unit with load_all_dies set. */
20552
20553 if (load_all
20554 || abbrev->tag == DW_TAG_constant
20555 || abbrev->tag == DW_TAG_subprogram
20556 || abbrev->tag == DW_TAG_variable
20557 || abbrev->tag == DW_TAG_namespace
20558 || part_die->is_declaration)
20559 {
20560 void **slot;
20561
20562 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
20563 to_underlying (part_die->sect_off),
20564 INSERT);
20565 *slot = part_die;
20566 }
20567
20568 /* For some DIEs we want to follow their children (if any). For C
20569 we have no reason to follow the children of structures; for other
20570 languages we have to, so that we can get at method physnames
20571 to infer fully qualified class names, for DW_AT_specification,
20572 and for C++ template arguments. For C++, we also look one level
20573 inside functions to find template arguments (if the name of the
20574 function does not already contain the template arguments).
20575
20576 For Ada and Fortran, we need to scan the children of subprograms
20577 and lexical blocks as well because these languages allow the
20578 definition of nested entities that could be interesting for the
20579 debugger, such as nested subprograms for instance. */
20580 if (last_die->has_children
20581 && (load_all
20582 || last_die->tag == DW_TAG_namespace
20583 || last_die->tag == DW_TAG_module
20584 || last_die->tag == DW_TAG_enumeration_type
20585 || (cu->per_cu->lang == language_cplus
20586 && last_die->tag == DW_TAG_subprogram
20587 && (last_die->raw_name == NULL
20588 || strchr (last_die->raw_name, '<') == NULL))
20589 || (cu->per_cu->lang != language_c
20590 && (last_die->tag == DW_TAG_class_type
20591 || last_die->tag == DW_TAG_interface_type
20592 || last_die->tag == DW_TAG_structure_type
20593 || last_die->tag == DW_TAG_union_type))
20594 || ((cu->per_cu->lang == language_ada
20595 || cu->per_cu->lang == language_fortran)
20596 && (last_die->tag == DW_TAG_subprogram
20597 || last_die->tag == DW_TAG_lexical_block))))
20598 {
20599 nesting_level++;
20600 parent_die = last_die;
20601 continue;
20602 }
20603
20604 /* Otherwise we skip to the next sibling, if any. */
20605 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
20606
20607 /* Back to the top, do it again. */
20608 }
20609 }
20610
20611 partial_die_info::partial_die_info (sect_offset sect_off_,
20612 const struct abbrev_info *abbrev)
20613 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
20614 {
20615 }
20616
20617 /* See class definition. */
20618
20619 const char *
20620 partial_die_info::name (dwarf2_cu *cu)
20621 {
20622 if (!canonical_name && raw_name != nullptr)
20623 {
20624 struct objfile *objfile = cu->per_objfile->objfile;
20625 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
20626 canonical_name = 1;
20627 }
20628
20629 return raw_name;
20630 }
20631
20632 /* Read a minimal amount of information into the minimal die structure.
20633 INFO_PTR should point just after the initial uleb128 of a DIE. */
20634
20635 const gdb_byte *
20636 partial_die_info::read (const struct die_reader_specs *reader,
20637 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
20638 {
20639 struct dwarf2_cu *cu = reader->cu;
20640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20641 unsigned int i;
20642 int has_low_pc_attr = 0;
20643 int has_high_pc_attr = 0;
20644 int high_pc_relative = 0;
20645
20646 for (i = 0; i < abbrev.num_attrs; ++i)
20647 {
20648 attribute attr;
20649 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
20650 /* String and address offsets that need to do the reprocessing have
20651 already been read at this point, so there is no need to wait until
20652 the loop terminates to do the reprocessing. */
20653 if (attr.requires_reprocessing_p ())
20654 read_attribute_reprocess (reader, &attr, tag);
20655 /* Store the data if it is of an attribute we want to keep in a
20656 partial symbol table. */
20657 switch (attr.name)
20658 {
20659 case DW_AT_name:
20660 switch (tag)
20661 {
20662 case DW_TAG_compile_unit:
20663 case DW_TAG_partial_unit:
20664 case DW_TAG_type_unit:
20665 /* Compilation units have a DW_AT_name that is a filename, not
20666 a source language identifier. */
20667 case DW_TAG_enumeration_type:
20668 case DW_TAG_enumerator:
20669 /* These tags always have simple identifiers already; no need
20670 to canonicalize them. */
20671 canonical_name = 1;
20672 raw_name = attr.as_string ();
20673 break;
20674 default:
20675 canonical_name = 0;
20676 raw_name = attr.as_string ();
20677 break;
20678 }
20679 break;
20680 case DW_AT_linkage_name:
20681 case DW_AT_MIPS_linkage_name:
20682 /* Note that both forms of linkage name might appear. We
20683 assume they will be the same, and we only store the last
20684 one we see. */
20685 linkage_name = attr.as_string ();
20686 break;
20687 case DW_AT_low_pc:
20688 has_low_pc_attr = 1;
20689 lowpc = attr.as_address ();
20690 break;
20691 case DW_AT_high_pc:
20692 has_high_pc_attr = 1;
20693 highpc = attr.as_address ();
20694 if (cu->header.version >= 4 && attr.form_is_constant ())
20695 high_pc_relative = 1;
20696 break;
20697 case DW_AT_location:
20698 /* Support the .debug_loc offsets. */
20699 if (attr.form_is_block ())
20700 {
20701 d.locdesc = attr.as_block ();
20702 }
20703 else if (attr.form_is_section_offset ())
20704 {
20705 dwarf2_complex_location_expr_complaint ();
20706 }
20707 else
20708 {
20709 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
20710 "partial symbol information");
20711 }
20712 break;
20713 case DW_AT_external:
20714 is_external = attr.as_boolean ();
20715 break;
20716 case DW_AT_declaration:
20717 is_declaration = attr.as_boolean ();
20718 break;
20719 case DW_AT_type:
20720 has_type = 1;
20721 break;
20722 case DW_AT_abstract_origin:
20723 case DW_AT_specification:
20724 case DW_AT_extension:
20725 has_specification = 1;
20726 spec_offset = attr.get_ref_die_offset ();
20727 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
20728 || cu->per_cu->is_dwz);
20729 break;
20730 case DW_AT_sibling:
20731 /* Ignore absolute siblings, they might point outside of
20732 the current compile unit. */
20733 if (attr.form == DW_FORM_ref_addr)
20734 complaint (_("ignoring absolute DW_AT_sibling"));
20735 else
20736 {
20737 const gdb_byte *buffer = reader->buffer;
20738 sect_offset off = attr.get_ref_die_offset ();
20739 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
20740
20741 if (sibling_ptr < info_ptr)
20742 complaint (_("DW_AT_sibling points backwards"));
20743 else if (sibling_ptr > reader->buffer_end)
20744 reader->die_section->overflow_complaint ();
20745 else
20746 sibling = sibling_ptr;
20747 }
20748 break;
20749 case DW_AT_byte_size:
20750 has_byte_size = 1;
20751 break;
20752 case DW_AT_const_value:
20753 has_const_value = 1;
20754 break;
20755 case DW_AT_calling_convention:
20756 /* DWARF doesn't provide a way to identify a program's source-level
20757 entry point. DW_AT_calling_convention attributes are only meant
20758 to describe functions' calling conventions.
20759
20760 However, because it's a necessary piece of information in
20761 Fortran, and before DWARF 4 DW_CC_program was the only
20762 piece of debugging information whose definition refers to
20763 a 'main program' at all, several compilers marked Fortran
20764 main programs with DW_CC_program --- even when those
20765 functions use the standard calling conventions.
20766
20767 Although DWARF now specifies a way to provide this
20768 information, we support this practice for backward
20769 compatibility. */
20770 if (attr.constant_value (0) == DW_CC_program
20771 && cu->per_cu->lang == language_fortran)
20772 main_subprogram = 1;
20773 break;
20774 case DW_AT_inline:
20775 {
20776 LONGEST value = attr.constant_value (-1);
20777 if (value == DW_INL_inlined
20778 || value == DW_INL_declared_inlined)
20779 may_be_inlined = 1;
20780 }
20781 break;
20782
20783 case DW_AT_import:
20784 if (tag == DW_TAG_imported_unit)
20785 {
20786 d.sect_off = attr.get_ref_die_offset ();
20787 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
20788 || cu->per_cu->is_dwz);
20789 }
20790 break;
20791
20792 case DW_AT_main_subprogram:
20793 main_subprogram = attr.as_boolean ();
20794 break;
20795
20796 case DW_AT_ranges:
20797 {
20798 /* Offset in the .debug_ranges or .debug_rnglist section (depending
20799 on DWARF version). */
20800 ranges_offset = attr.as_unsigned ();
20801
20802 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
20803 this value. */
20804 if (tag != DW_TAG_compile_unit)
20805 ranges_offset += cu->gnu_ranges_base;
20806
20807 has_range_info = 1;
20808 }
20809 break;
20810
20811 default:
20812 break;
20813 }
20814 }
20815
20816 /* For Ada, if both the name and the linkage name appear, we prefer
20817 the latter. This lets "catch exception" work better, regardless
20818 of the order in which the name and linkage name were emitted.
20819 Really, though, this is just a workaround for the fact that gdb
20820 doesn't store both the name and the linkage name. */
20821 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
20822 raw_name = linkage_name;
20823
20824 if (high_pc_relative)
20825 highpc += lowpc;
20826
20827 if (has_low_pc_attr && has_high_pc_attr)
20828 {
20829 /* When using the GNU linker, .gnu.linkonce. sections are used to
20830 eliminate duplicate copies of functions and vtables and such.
20831 The linker will arbitrarily choose one and discard the others.
20832 The AT_*_pc values for such functions refer to local labels in
20833 these sections. If the section from that file was discarded, the
20834 labels are not in the output, so the relocs get a value of 0.
20835 If this is a discarded function, mark the pc bounds as invalid,
20836 so that GDB will ignore it. */
20837 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
20838 {
20839 struct objfile *objfile = per_objfile->objfile;
20840 struct gdbarch *gdbarch = objfile->arch ();
20841
20842 complaint (_("DW_AT_low_pc %s is zero "
20843 "for DIE at %s [in module %s]"),
20844 paddress (gdbarch, lowpc),
20845 sect_offset_str (sect_off),
20846 objfile_name (objfile));
20847 }
20848 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
20849 else if (lowpc >= highpc)
20850 {
20851 struct objfile *objfile = per_objfile->objfile;
20852 struct gdbarch *gdbarch = objfile->arch ();
20853
20854 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
20855 "for DIE at %s [in module %s]"),
20856 paddress (gdbarch, lowpc),
20857 paddress (gdbarch, highpc),
20858 sect_offset_str (sect_off),
20859 objfile_name (objfile));
20860 }
20861 else
20862 has_pc_info = 1;
20863 }
20864
20865 return info_ptr;
20866 }
20867
20868 /* Find a cached partial DIE at OFFSET in CU. */
20869
20870 struct partial_die_info *
20871 dwarf2_cu::find_partial_die (sect_offset sect_off)
20872 {
20873 struct partial_die_info *lookup_die = NULL;
20874 struct partial_die_info part_die (sect_off);
20875
20876 lookup_die = ((struct partial_die_info *)
20877 htab_find_with_hash (partial_dies, &part_die,
20878 to_underlying (sect_off)));
20879
20880 return lookup_die;
20881 }
20882
20883 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20884 except in the case of .debug_types DIEs which do not reference
20885 outside their CU (they do however referencing other types via
20886 DW_FORM_ref_sig8). */
20887
20888 static const struct cu_partial_die_info
20889 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20890 {
20891 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20892 struct objfile *objfile = per_objfile->objfile;
20893 struct partial_die_info *pd = NULL;
20894
20895 if (offset_in_dwz == cu->per_cu->is_dwz
20896 && cu->header.offset_in_cu_p (sect_off))
20897 {
20898 pd = cu->find_partial_die (sect_off);
20899 if (pd != NULL)
20900 return { cu, pd };
20901 /* We missed recording what we needed.
20902 Load all dies and try again. */
20903 }
20904 else
20905 {
20906 /* TUs don't reference other CUs/TUs (except via type signatures). */
20907 if (cu->per_cu->is_debug_types)
20908 {
20909 error (_("Dwarf Error: Type Unit at offset %s contains"
20910 " external reference to offset %s [in module %s].\n"),
20911 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20912 bfd_get_filename (objfile->obfd));
20913 }
20914 dwarf2_per_cu_data *per_cu
20915 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20916 per_objfile->per_bfd);
20917
20918 cu = per_objfile->get_cu (per_cu);
20919 if (cu == NULL || cu->partial_dies == NULL)
20920 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20921
20922 cu = per_objfile->get_cu (per_cu);
20923
20924 cu->last_used = 0;
20925 pd = cu->find_partial_die (sect_off);
20926 }
20927
20928 /* If we didn't find it, and not all dies have been loaded,
20929 load them all and try again. */
20930
20931 if (pd == NULL && cu->load_all_dies == 0)
20932 {
20933 cu->load_all_dies = 1;
20934
20935 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20936 THIS_CU->cu may already be in use. So we can't just free it and
20937 replace its DIEs with the ones we read in. Instead, we leave those
20938 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20939 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20940 set. */
20941 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20942
20943 pd = cu->find_partial_die (sect_off);
20944 }
20945
20946 if (pd == NULL)
20947 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
20948 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20949 return { cu, pd };
20950 }
20951
20952 /* See if we can figure out if the class lives in a namespace. We do
20953 this by looking for a member function; its demangled name will
20954 contain namespace info, if there is any. */
20955
20956 static void
20957 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20958 struct dwarf2_cu *cu)
20959 {
20960 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20961 what template types look like, because the demangler
20962 frequently doesn't give the same name as the debug info. We
20963 could fix this by only using the demangled name to get the
20964 prefix (but see comment in read_structure_type). */
20965
20966 struct partial_die_info *real_pdi;
20967 struct partial_die_info *child_pdi;
20968
20969 /* If this DIE (this DIE's specification, if any) has a parent, then
20970 we should not do this. We'll prepend the parent's fully qualified
20971 name when we create the partial symbol. */
20972
20973 real_pdi = struct_pdi;
20974 while (real_pdi->has_specification)
20975 {
20976 auto res = find_partial_die (real_pdi->spec_offset,
20977 real_pdi->spec_is_dwz, cu);
20978 real_pdi = res.pdi;
20979 cu = res.cu;
20980 }
20981
20982 if (real_pdi->die_parent != NULL)
20983 return;
20984
20985 for (child_pdi = struct_pdi->die_child;
20986 child_pdi != NULL;
20987 child_pdi = child_pdi->die_sibling)
20988 {
20989 if (child_pdi->tag == DW_TAG_subprogram
20990 && child_pdi->linkage_name != NULL)
20991 {
20992 gdb::unique_xmalloc_ptr<char> actual_class_name
20993 (cu->language_defn->class_name_from_physname
20994 (child_pdi->linkage_name));
20995 if (actual_class_name != NULL)
20996 {
20997 struct objfile *objfile = cu->per_objfile->objfile;
20998 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20999 struct_pdi->canonical_name = 1;
21000 }
21001 break;
21002 }
21003 }
21004 }
21005
21006 /* Return true if a DIE with TAG may have the DW_AT_const_value
21007 attribute. */
21008
21009 static bool
21010 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
21011 {
21012 switch (tag)
21013 {
21014 case DW_TAG_constant:
21015 case DW_TAG_enumerator:
21016 case DW_TAG_formal_parameter:
21017 case DW_TAG_template_value_param:
21018 case DW_TAG_variable:
21019 return true;
21020 }
21021
21022 return false;
21023 }
21024
21025 void
21026 partial_die_info::fixup (struct dwarf2_cu *cu)
21027 {
21028 /* Once we've fixed up a die, there's no point in doing so again.
21029 This also avoids a memory leak if we were to call
21030 guess_partial_die_structure_name multiple times. */
21031 if (fixup_called)
21032 return;
21033
21034 /* If we found a reference attribute and the DIE has no name, try
21035 to find a name in the referred to DIE. */
21036
21037 if (raw_name == NULL && has_specification)
21038 {
21039 struct partial_die_info *spec_die;
21040
21041 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
21042 spec_die = res.pdi;
21043 cu = res.cu;
21044
21045 spec_die->fixup (cu);
21046
21047 if (spec_die->raw_name)
21048 {
21049 raw_name = spec_die->raw_name;
21050 canonical_name = spec_die->canonical_name;
21051
21052 /* Copy DW_AT_external attribute if it is set. */
21053 if (spec_die->is_external)
21054 is_external = spec_die->is_external;
21055 }
21056 }
21057
21058 if (!has_const_value && has_specification
21059 && can_have_DW_AT_const_value_p (tag))
21060 {
21061 struct partial_die_info *spec_die;
21062
21063 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
21064 spec_die = res.pdi;
21065 cu = res.cu;
21066
21067 spec_die->fixup (cu);
21068
21069 if (spec_die->has_const_value)
21070 {
21071 /* Copy DW_AT_const_value attribute if it is set. */
21072 has_const_value = spec_die->has_const_value;
21073 }
21074 }
21075
21076 /* Set default names for some unnamed DIEs. */
21077
21078 if (raw_name == NULL && tag == DW_TAG_namespace)
21079 {
21080 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
21081 canonical_name = 1;
21082 }
21083
21084 /* If there is no parent die to provide a namespace, and there are
21085 children, see if we can determine the namespace from their linkage
21086 name. */
21087 if (cu->per_cu->lang == language_cplus
21088 && !cu->per_objfile->per_bfd->types.empty ()
21089 && die_parent == NULL
21090 && has_children
21091 && (tag == DW_TAG_class_type
21092 || tag == DW_TAG_structure_type
21093 || tag == DW_TAG_union_type))
21094 guess_partial_die_structure_name (this, cu);
21095
21096 /* GCC might emit a nameless struct or union that has a linkage
21097 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21098 if (raw_name == NULL
21099 && (tag == DW_TAG_class_type
21100 || tag == DW_TAG_interface_type
21101 || tag == DW_TAG_structure_type
21102 || tag == DW_TAG_union_type)
21103 && linkage_name != NULL)
21104 {
21105 gdb::unique_xmalloc_ptr<char> demangled
21106 (gdb_demangle (linkage_name, DMGL_TYPES));
21107 if (demangled != nullptr)
21108 {
21109 const char *base;
21110
21111 /* Strip any leading namespaces/classes, keep only the base name.
21112 DW_AT_name for named DIEs does not contain the prefixes. */
21113 base = strrchr (demangled.get (), ':');
21114 if (base && base > demangled.get () && base[-1] == ':')
21115 base++;
21116 else
21117 base = demangled.get ();
21118
21119 struct objfile *objfile = cu->per_objfile->objfile;
21120 raw_name = objfile->intern (base);
21121 canonical_name = 1;
21122 }
21123 }
21124
21125 fixup_called = 1;
21126 }
21127
21128 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
21129 contents from the given SECTION in the HEADER.
21130
21131 HEADER_OFFSET is the offset of the header in the section. */
21132 static void
21133 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
21134 struct dwarf2_section_info *section,
21135 sect_offset header_offset)
21136 {
21137 unsigned int bytes_read;
21138 bfd *abfd = section->get_bfd_owner ();
21139 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
21140
21141 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
21142 info_ptr += bytes_read;
21143
21144 header->version = read_2_bytes (abfd, info_ptr);
21145 info_ptr += 2;
21146
21147 header->addr_size = read_1_byte (abfd, info_ptr);
21148 info_ptr += 1;
21149
21150 header->segment_collector_size = read_1_byte (abfd, info_ptr);
21151 info_ptr += 1;
21152
21153 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
21154 }
21155
21156 /* Return the DW_AT_loclists_base value for the CU. */
21157 static ULONGEST
21158 lookup_loclist_base (struct dwarf2_cu *cu)
21159 {
21160 /* For the .dwo unit, the loclist_base points to the first offset following
21161 the header. The header consists of the following entities-
21162 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
21163 bit format)
21164 2. version (2 bytes)
21165 3. address size (1 byte)
21166 4. segment selector size (1 byte)
21167 5. offset entry count (4 bytes)
21168 These sizes are derived as per the DWARFv5 standard. */
21169 if (cu->dwo_unit != nullptr)
21170 {
21171 if (cu->header.initial_length_size == 4)
21172 return LOCLIST_HEADER_SIZE32;
21173 return LOCLIST_HEADER_SIZE64;
21174 }
21175 return cu->loclist_base;
21176 }
21177
21178 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
21179 array of offsets in the .debug_loclists section. */
21180
21181 static sect_offset
21182 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
21183 {
21184 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21185 struct objfile *objfile = per_objfile->objfile;
21186 bfd *abfd = objfile->obfd;
21187 ULONGEST loclist_header_size =
21188 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
21189 : LOCLIST_HEADER_SIZE64);
21190 ULONGEST loclist_base = lookup_loclist_base (cu);
21191
21192 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
21193 ULONGEST start_offset =
21194 loclist_base + loclist_index * cu->header.offset_size;
21195
21196 /* Get loclists section. */
21197 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21198
21199 /* Read the loclists section content. */
21200 section->read (objfile);
21201 if (section->buffer == NULL)
21202 error (_("DW_FORM_loclistx used without .debug_loclists "
21203 "section [in module %s]"), objfile_name (objfile));
21204
21205 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
21206 so if loclist_base is smaller than the header size, we have a problem. */
21207 if (loclist_base < loclist_header_size)
21208 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
21209 objfile_name (objfile));
21210
21211 /* Read the header of the loclists contribution. */
21212 struct loclists_rnglists_header header;
21213 read_loclists_rnglists_header (&header, section,
21214 (sect_offset) (loclist_base - loclist_header_size));
21215
21216 /* Verify the loclist index is valid. */
21217 if (loclist_index >= header.offset_entry_count)
21218 error (_("DW_FORM_loclistx pointing outside of "
21219 ".debug_loclists offset array [in module %s]"),
21220 objfile_name (objfile));
21221
21222 /* Validate that reading won't go beyond the end of the section. */
21223 if (start_offset + cu->header.offset_size > section->size)
21224 error (_("Reading DW_FORM_loclistx index beyond end of"
21225 ".debug_loclists section [in module %s]"),
21226 objfile_name (objfile));
21227
21228 const gdb_byte *info_ptr = section->buffer + start_offset;
21229
21230 if (cu->header.offset_size == 4)
21231 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
21232 else
21233 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
21234 }
21235
21236 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
21237 array of offsets in the .debug_rnglists section. */
21238
21239 static sect_offset
21240 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
21241 dwarf_tag tag)
21242 {
21243 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21244 struct objfile *objfile = dwarf2_per_objfile->objfile;
21245 bfd *abfd = objfile->obfd;
21246 ULONGEST rnglist_header_size =
21247 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
21248 : RNGLIST_HEADER_SIZE64);
21249
21250 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
21251 .debug_rnglists.dwo section. The rnglists base given in the skeleton
21252 doesn't apply. */
21253 ULONGEST rnglist_base =
21254 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
21255
21256 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
21257 ULONGEST start_offset =
21258 rnglist_base + rnglist_index * cu->header.offset_size;
21259
21260 /* Get rnglists section. */
21261 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
21262
21263 /* Read the rnglists section content. */
21264 section->read (objfile);
21265 if (section->buffer == nullptr)
21266 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
21267 "[in module %s]"),
21268 objfile_name (objfile));
21269
21270 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
21271 so if rnglist_base is smaller than the header size, we have a problem. */
21272 if (rnglist_base < rnglist_header_size)
21273 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
21274 objfile_name (objfile));
21275
21276 /* Read the header of the rnglists contribution. */
21277 struct loclists_rnglists_header header;
21278 read_loclists_rnglists_header (&header, section,
21279 (sect_offset) (rnglist_base - rnglist_header_size));
21280
21281 /* Verify the rnglist index is valid. */
21282 if (rnglist_index >= header.offset_entry_count)
21283 error (_("DW_FORM_rnglistx index pointing outside of "
21284 ".debug_rnglists offset array [in module %s]"),
21285 objfile_name (objfile));
21286
21287 /* Validate that reading won't go beyond the end of the section. */
21288 if (start_offset + cu->header.offset_size > section->size)
21289 error (_("Reading DW_FORM_rnglistx index beyond end of"
21290 ".debug_rnglists section [in module %s]"),
21291 objfile_name (objfile));
21292
21293 const gdb_byte *info_ptr = section->buffer + start_offset;
21294
21295 if (cu->header.offset_size == 4)
21296 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
21297 else
21298 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
21299 }
21300
21301 /* Process the attributes that had to be skipped in the first round. These
21302 attributes are the ones that need str_offsets_base or addr_base attributes.
21303 They could not have been processed in the first round, because at the time
21304 the values of str_offsets_base or addr_base may not have been known. */
21305 static void
21306 read_attribute_reprocess (const struct die_reader_specs *reader,
21307 struct attribute *attr, dwarf_tag tag)
21308 {
21309 struct dwarf2_cu *cu = reader->cu;
21310 switch (attr->form)
21311 {
21312 case DW_FORM_addrx:
21313 case DW_FORM_GNU_addr_index:
21314 attr->set_address (read_addr_index (cu,
21315 attr->as_unsigned_reprocess ()));
21316 break;
21317 case DW_FORM_loclistx:
21318 {
21319 sect_offset loclists_sect_off
21320 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
21321
21322 attr->set_unsigned (to_underlying (loclists_sect_off));
21323 }
21324 break;
21325 case DW_FORM_rnglistx:
21326 {
21327 sect_offset rnglists_sect_off
21328 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
21329
21330 attr->set_unsigned (to_underlying (rnglists_sect_off));
21331 }
21332 break;
21333 case DW_FORM_strx:
21334 case DW_FORM_strx1:
21335 case DW_FORM_strx2:
21336 case DW_FORM_strx3:
21337 case DW_FORM_strx4:
21338 case DW_FORM_GNU_str_index:
21339 {
21340 unsigned int str_index = attr->as_unsigned_reprocess ();
21341 gdb_assert (!attr->canonical_string_p ());
21342 if (reader->dwo_file != NULL)
21343 attr->set_string_noncanonical (read_dwo_str_index (reader,
21344 str_index));
21345 else
21346 attr->set_string_noncanonical (read_stub_str_index (cu,
21347 str_index));
21348 break;
21349 }
21350 default:
21351 gdb_assert_not_reached ("Unexpected DWARF form.");
21352 }
21353 }
21354
21355 /* Read an attribute value described by an attribute form. */
21356
21357 static const gdb_byte *
21358 read_attribute_value (const struct die_reader_specs *reader,
21359 struct attribute *attr, unsigned form,
21360 LONGEST implicit_const, const gdb_byte *info_ptr)
21361 {
21362 struct dwarf2_cu *cu = reader->cu;
21363 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21364 struct objfile *objfile = per_objfile->objfile;
21365 bfd *abfd = reader->abfd;
21366 struct comp_unit_head *cu_header = &cu->header;
21367 unsigned int bytes_read;
21368 struct dwarf_block *blk;
21369
21370 attr->form = (enum dwarf_form) form;
21371 switch (form)
21372 {
21373 case DW_FORM_ref_addr:
21374 if (cu_header->version == 2)
21375 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
21376 &bytes_read));
21377 else
21378 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
21379 &bytes_read));
21380 info_ptr += bytes_read;
21381 break;
21382 case DW_FORM_GNU_ref_alt:
21383 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
21384 &bytes_read));
21385 info_ptr += bytes_read;
21386 break;
21387 case DW_FORM_addr:
21388 {
21389 struct gdbarch *gdbarch = objfile->arch ();
21390 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
21391 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
21392 attr->set_address (addr);
21393 info_ptr += bytes_read;
21394 }
21395 break;
21396 case DW_FORM_block2:
21397 blk = dwarf_alloc_block (cu);
21398 blk->size = read_2_bytes (abfd, info_ptr);
21399 info_ptr += 2;
21400 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
21401 info_ptr += blk->size;
21402 attr->set_block (blk);
21403 break;
21404 case DW_FORM_block4:
21405 blk = dwarf_alloc_block (cu);
21406 blk->size = read_4_bytes (abfd, info_ptr);
21407 info_ptr += 4;
21408 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
21409 info_ptr += blk->size;
21410 attr->set_block (blk);
21411 break;
21412 case DW_FORM_data2:
21413 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
21414 info_ptr += 2;
21415 break;
21416 case DW_FORM_data4:
21417 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
21418 info_ptr += 4;
21419 break;
21420 case DW_FORM_data8:
21421 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
21422 info_ptr += 8;
21423 break;
21424 case DW_FORM_data16:
21425 blk = dwarf_alloc_block (cu);
21426 blk->size = 16;
21427 blk->data = read_n_bytes (abfd, info_ptr, 16);
21428 info_ptr += 16;
21429 attr->set_block (blk);
21430 break;
21431 case DW_FORM_sec_offset:
21432 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
21433 &bytes_read));
21434 info_ptr += bytes_read;
21435 break;
21436 case DW_FORM_loclistx:
21437 {
21438 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
21439 &bytes_read));
21440 info_ptr += bytes_read;
21441 }
21442 break;
21443 case DW_FORM_string:
21444 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
21445 &bytes_read));
21446 info_ptr += bytes_read;
21447 break;
21448 case DW_FORM_strp:
21449 if (!cu->per_cu->is_dwz)
21450 {
21451 attr->set_string_noncanonical
21452 (read_indirect_string (per_objfile,
21453 abfd, info_ptr, cu_header,
21454 &bytes_read));
21455 info_ptr += bytes_read;
21456 break;
21457 }
21458 /* FALLTHROUGH */
21459 case DW_FORM_line_strp:
21460 if (!cu->per_cu->is_dwz)
21461 {
21462 attr->set_string_noncanonical
21463 (per_objfile->read_line_string (info_ptr, cu_header,
21464 &bytes_read));
21465 info_ptr += bytes_read;
21466 break;
21467 }
21468 /* FALLTHROUGH */
21469 case DW_FORM_GNU_strp_alt:
21470 {
21471 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21472 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
21473 &bytes_read);
21474
21475 attr->set_string_noncanonical
21476 (dwz->read_string (objfile, str_offset));
21477 info_ptr += bytes_read;
21478 }
21479 break;
21480 case DW_FORM_exprloc:
21481 case DW_FORM_block:
21482 blk = dwarf_alloc_block (cu);
21483 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
21484 info_ptr += bytes_read;
21485 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
21486 info_ptr += blk->size;
21487 attr->set_block (blk);
21488 break;
21489 case DW_FORM_block1:
21490 blk = dwarf_alloc_block (cu);
21491 blk->size = read_1_byte (abfd, info_ptr);
21492 info_ptr += 1;
21493 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
21494 info_ptr += blk->size;
21495 attr->set_block (blk);
21496 break;
21497 case DW_FORM_data1:
21498 case DW_FORM_flag:
21499 attr->set_unsigned (read_1_byte (abfd, info_ptr));
21500 info_ptr += 1;
21501 break;
21502 case DW_FORM_flag_present:
21503 attr->set_unsigned (1);
21504 break;
21505 case DW_FORM_sdata:
21506 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
21507 info_ptr += bytes_read;
21508 break;
21509 case DW_FORM_rnglistx:
21510 {
21511 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
21512 &bytes_read));
21513 info_ptr += bytes_read;
21514 }
21515 break;
21516 case DW_FORM_udata:
21517 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
21518 info_ptr += bytes_read;
21519 break;
21520 case DW_FORM_ref1:
21521 attr->set_unsigned ((to_underlying (cu_header->sect_off)
21522 + read_1_byte (abfd, info_ptr)));
21523 info_ptr += 1;
21524 break;
21525 case DW_FORM_ref2:
21526 attr->set_unsigned ((to_underlying (cu_header->sect_off)
21527 + read_2_bytes (abfd, info_ptr)));
21528 info_ptr += 2;
21529 break;
21530 case DW_FORM_ref4:
21531 attr->set_unsigned ((to_underlying (cu_header->sect_off)
21532 + read_4_bytes (abfd, info_ptr)));
21533 info_ptr += 4;
21534 break;
21535 case DW_FORM_ref8:
21536 attr->set_unsigned ((to_underlying (cu_header->sect_off)
21537 + read_8_bytes (abfd, info_ptr)));
21538 info_ptr += 8;
21539 break;
21540 case DW_FORM_ref_sig8:
21541 attr->set_signature (read_8_bytes (abfd, info_ptr));
21542 info_ptr += 8;
21543 break;
21544 case DW_FORM_ref_udata:
21545 attr->set_unsigned ((to_underlying (cu_header->sect_off)
21546 + read_unsigned_leb128 (abfd, info_ptr,
21547 &bytes_read)));
21548 info_ptr += bytes_read;
21549 break;
21550 case DW_FORM_indirect:
21551 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
21552 info_ptr += bytes_read;
21553 if (form == DW_FORM_implicit_const)
21554 {
21555 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
21556 info_ptr += bytes_read;
21557 }
21558 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
21559 info_ptr);
21560 break;
21561 case DW_FORM_implicit_const:
21562 attr->set_signed (implicit_const);
21563 break;
21564 case DW_FORM_addrx:
21565 case DW_FORM_GNU_addr_index:
21566 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
21567 &bytes_read));
21568 info_ptr += bytes_read;
21569 break;
21570 case DW_FORM_strx:
21571 case DW_FORM_strx1:
21572 case DW_FORM_strx2:
21573 case DW_FORM_strx3:
21574 case DW_FORM_strx4:
21575 case DW_FORM_GNU_str_index:
21576 {
21577 ULONGEST str_index;
21578 if (form == DW_FORM_strx1)
21579 {
21580 str_index = read_1_byte (abfd, info_ptr);
21581 info_ptr += 1;
21582 }
21583 else if (form == DW_FORM_strx2)
21584 {
21585 str_index = read_2_bytes (abfd, info_ptr);
21586 info_ptr += 2;
21587 }
21588 else if (form == DW_FORM_strx3)
21589 {
21590 str_index = read_3_bytes (abfd, info_ptr);
21591 info_ptr += 3;
21592 }
21593 else if (form == DW_FORM_strx4)
21594 {
21595 str_index = read_4_bytes (abfd, info_ptr);
21596 info_ptr += 4;
21597 }
21598 else
21599 {
21600 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
21601 info_ptr += bytes_read;
21602 }
21603 attr->set_unsigned_reprocess (str_index);
21604 }
21605 break;
21606 default:
21607 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
21608 dwarf_form_name (form),
21609 bfd_get_filename (abfd));
21610 }
21611
21612 /* Super hack. */
21613 if (cu->per_cu->is_dwz && attr->form_is_ref ())
21614 attr->form = DW_FORM_GNU_ref_alt;
21615
21616 /* We have seen instances where the compiler tried to emit a byte
21617 size attribute of -1 which ended up being encoded as an unsigned
21618 0xffffffff. Although 0xffffffff is technically a valid size value,
21619 an object of this size seems pretty unlikely so we can relatively
21620 safely treat these cases as if the size attribute was invalid and
21621 treat them as zero by default. */
21622 if (attr->name == DW_AT_byte_size
21623 && form == DW_FORM_data4
21624 && attr->as_unsigned () >= 0xffffffff)
21625 {
21626 complaint
21627 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
21628 hex_string (attr->as_unsigned ()));
21629 attr->set_unsigned (0);
21630 }
21631
21632 return info_ptr;
21633 }
21634
21635 /* Read an attribute described by an abbreviated attribute. */
21636
21637 static const gdb_byte *
21638 read_attribute (const struct die_reader_specs *reader,
21639 struct attribute *attr, const struct attr_abbrev *abbrev,
21640 const gdb_byte *info_ptr)
21641 {
21642 attr->name = abbrev->name;
21643 attr->string_is_canonical = 0;
21644 attr->requires_reprocessing = 0;
21645 return read_attribute_value (reader, attr, abbrev->form,
21646 abbrev->implicit_const, info_ptr);
21647 }
21648
21649 /* Return pointer to string at .debug_str offset STR_OFFSET. */
21650
21651 static const char *
21652 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
21653 LONGEST str_offset)
21654 {
21655 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
21656 str_offset, "DW_FORM_strp");
21657 }
21658
21659 /* Return pointer to string at .debug_str offset as read from BUF.
21660 BUF is assumed to be in a compilation unit described by CU_HEADER.
21661 Return *BYTES_READ_PTR count of bytes read from BUF. */
21662
21663 static const char *
21664 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
21665 const gdb_byte *buf,
21666 const struct comp_unit_head *cu_header,
21667 unsigned int *bytes_read_ptr)
21668 {
21669 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
21670
21671 return read_indirect_string_at_offset (per_objfile, str_offset);
21672 }
21673
21674 /* See read.h. */
21675
21676 const char *
21677 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
21678 unsigned int offset_size)
21679 {
21680 bfd *abfd = objfile->obfd;
21681 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
21682
21683 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
21684 }
21685
21686 /* See read.h. */
21687
21688 const char *
21689 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
21690 const struct comp_unit_head *cu_header,
21691 unsigned int *bytes_read_ptr)
21692 {
21693 bfd *abfd = objfile->obfd;
21694 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
21695
21696 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
21697 }
21698
21699 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
21700 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
21701 ADDR_SIZE is the size of addresses from the CU header. */
21702
21703 static CORE_ADDR
21704 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
21705 gdb::optional<ULONGEST> addr_base, int addr_size)
21706 {
21707 struct objfile *objfile = per_objfile->objfile;
21708 bfd *abfd = objfile->obfd;
21709 const gdb_byte *info_ptr;
21710 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
21711
21712 per_objfile->per_bfd->addr.read (objfile);
21713 if (per_objfile->per_bfd->addr.buffer == NULL)
21714 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
21715 objfile_name (objfile));
21716 if (addr_base_or_zero + addr_index * addr_size
21717 >= per_objfile->per_bfd->addr.size)
21718 error (_("DW_FORM_addr_index pointing outside of "
21719 ".debug_addr section [in module %s]"),
21720 objfile_name (objfile));
21721 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
21722 + addr_index * addr_size);
21723 if (addr_size == 4)
21724 return bfd_get_32 (abfd, info_ptr);
21725 else
21726 return bfd_get_64 (abfd, info_ptr);
21727 }
21728
21729 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
21730
21731 static CORE_ADDR
21732 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
21733 {
21734 return read_addr_index_1 (cu->per_objfile, addr_index,
21735 cu->addr_base, cu->header.addr_size);
21736 }
21737
21738 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
21739
21740 static CORE_ADDR
21741 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
21742 unsigned int *bytes_read)
21743 {
21744 bfd *abfd = cu->per_objfile->objfile->obfd;
21745 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
21746
21747 return read_addr_index (cu, addr_index);
21748 }
21749
21750 /* See read.h. */
21751
21752 CORE_ADDR
21753 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
21754 dwarf2_per_objfile *per_objfile,
21755 unsigned int addr_index)
21756 {
21757 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
21758 gdb::optional<ULONGEST> addr_base;
21759 int addr_size;
21760
21761 /* We need addr_base and addr_size.
21762 If we don't have PER_CU->cu, we have to get it.
21763 Nasty, but the alternative is storing the needed info in PER_CU,
21764 which at this point doesn't seem justified: it's not clear how frequently
21765 it would get used and it would increase the size of every PER_CU.
21766 Entry points like dwarf2_per_cu_addr_size do a similar thing
21767 so we're not in uncharted territory here.
21768 Alas we need to be a bit more complicated as addr_base is contained
21769 in the DIE.
21770
21771 We don't need to read the entire CU(/TU).
21772 We just need the header and top level die.
21773
21774 IWBN to use the aging mechanism to let us lazily later discard the CU.
21775 For now we skip this optimization. */
21776
21777 if (cu != NULL)
21778 {
21779 addr_base = cu->addr_base;
21780 addr_size = cu->header.addr_size;
21781 }
21782 else
21783 {
21784 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
21785 addr_base = reader.cu->addr_base;
21786 addr_size = reader.cu->header.addr_size;
21787 }
21788
21789 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
21790 }
21791
21792 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
21793 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
21794 DWO file. */
21795
21796 static const char *
21797 read_str_index (struct dwarf2_cu *cu,
21798 struct dwarf2_section_info *str_section,
21799 struct dwarf2_section_info *str_offsets_section,
21800 ULONGEST str_offsets_base, ULONGEST str_index)
21801 {
21802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21803 struct objfile *objfile = per_objfile->objfile;
21804 const char *objf_name = objfile_name (objfile);
21805 bfd *abfd = objfile->obfd;
21806 const gdb_byte *info_ptr;
21807 ULONGEST str_offset;
21808 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
21809
21810 str_section->read (objfile);
21811 str_offsets_section->read (objfile);
21812 if (str_section->buffer == NULL)
21813 error (_("%s used without %s section"
21814 " in CU at offset %s [in module %s]"),
21815 form_name, str_section->get_name (),
21816 sect_offset_str (cu->header.sect_off), objf_name);
21817 if (str_offsets_section->buffer == NULL)
21818 error (_("%s used without %s section"
21819 " in CU at offset %s [in module %s]"),
21820 form_name, str_section->get_name (),
21821 sect_offset_str (cu->header.sect_off), objf_name);
21822 info_ptr = (str_offsets_section->buffer
21823 + str_offsets_base
21824 + str_index * cu->header.offset_size);
21825 if (cu->header.offset_size == 4)
21826 str_offset = bfd_get_32 (abfd, info_ptr);
21827 else
21828 str_offset = bfd_get_64 (abfd, info_ptr);
21829 if (str_offset >= str_section->size)
21830 error (_("Offset from %s pointing outside of"
21831 " .debug_str.dwo section in CU at offset %s [in module %s]"),
21832 form_name, sect_offset_str (cu->header.sect_off), objf_name);
21833 return (const char *) (str_section->buffer + str_offset);
21834 }
21835
21836 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
21837
21838 static const char *
21839 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
21840 {
21841 ULONGEST str_offsets_base = reader->cu->header.version >= 5
21842 ? reader->cu->header.addr_size : 0;
21843 return read_str_index (reader->cu,
21844 &reader->dwo_file->sections.str,
21845 &reader->dwo_file->sections.str_offsets,
21846 str_offsets_base, str_index);
21847 }
21848
21849 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
21850
21851 static const char *
21852 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
21853 {
21854 struct objfile *objfile = cu->per_objfile->objfile;
21855 const char *objf_name = objfile_name (objfile);
21856 static const char form_name[] = "DW_FORM_GNU_str_index";
21857 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
21858
21859 if (!cu->str_offsets_base.has_value ())
21860 error (_("%s used in Fission stub without %s"
21861 " in CU at offset 0x%lx [in module %s]"),
21862 form_name, str_offsets_attr_name,
21863 (long) cu->header.offset_size, objf_name);
21864
21865 return read_str_index (cu,
21866 &cu->per_objfile->per_bfd->str,
21867 &cu->per_objfile->per_bfd->str_offsets,
21868 *cu->str_offsets_base, str_index);
21869 }
21870
21871 /* Return the length of an LEB128 number in BUF. */
21872
21873 static int
21874 leb128_size (const gdb_byte *buf)
21875 {
21876 const gdb_byte *begin = buf;
21877 gdb_byte byte;
21878
21879 while (1)
21880 {
21881 byte = *buf++;
21882 if ((byte & 128) == 0)
21883 return buf - begin;
21884 }
21885 }
21886
21887 static enum language
21888 dwarf_lang_to_enum_language (unsigned int lang)
21889 {
21890 enum language language;
21891
21892 switch (lang)
21893 {
21894 case DW_LANG_C89:
21895 case DW_LANG_C99:
21896 case DW_LANG_C11:
21897 case DW_LANG_C:
21898 case DW_LANG_UPC:
21899 language = language_c;
21900 break;
21901 case DW_LANG_Java:
21902 case DW_LANG_C_plus_plus:
21903 case DW_LANG_C_plus_plus_11:
21904 case DW_LANG_C_plus_plus_14:
21905 language = language_cplus;
21906 break;
21907 case DW_LANG_D:
21908 language = language_d;
21909 break;
21910 case DW_LANG_Fortran77:
21911 case DW_LANG_Fortran90:
21912 case DW_LANG_Fortran95:
21913 case DW_LANG_Fortran03:
21914 case DW_LANG_Fortran08:
21915 language = language_fortran;
21916 break;
21917 case DW_LANG_Go:
21918 language = language_go;
21919 break;
21920 case DW_LANG_Mips_Assembler:
21921 language = language_asm;
21922 break;
21923 case DW_LANG_Ada83:
21924 case DW_LANG_Ada95:
21925 language = language_ada;
21926 break;
21927 case DW_LANG_Modula2:
21928 language = language_m2;
21929 break;
21930 case DW_LANG_Pascal83:
21931 language = language_pascal;
21932 break;
21933 case DW_LANG_ObjC:
21934 language = language_objc;
21935 break;
21936 case DW_LANG_Rust:
21937 case DW_LANG_Rust_old:
21938 language = language_rust;
21939 break;
21940 case DW_LANG_OpenCL:
21941 language = language_opencl;
21942 break;
21943 case DW_LANG_Cobol74:
21944 case DW_LANG_Cobol85:
21945 default:
21946 language = language_minimal;
21947 break;
21948 }
21949
21950 return language;
21951 }
21952
21953 /* Return the named attribute or NULL if not there. */
21954
21955 static struct attribute *
21956 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21957 {
21958 for (;;)
21959 {
21960 unsigned int i;
21961 struct attribute *spec = NULL;
21962
21963 for (i = 0; i < die->num_attrs; ++i)
21964 {
21965 if (die->attrs[i].name == name)
21966 return &die->attrs[i];
21967 if (die->attrs[i].name == DW_AT_specification
21968 || die->attrs[i].name == DW_AT_abstract_origin)
21969 spec = &die->attrs[i];
21970 }
21971
21972 if (!spec)
21973 break;
21974
21975 die = follow_die_ref (die, spec, &cu);
21976 }
21977
21978 return NULL;
21979 }
21980
21981 /* Return the string associated with a string-typed attribute, or NULL if it
21982 is either not found or is of an incorrect type. */
21983
21984 static const char *
21985 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21986 {
21987 struct attribute *attr;
21988 const char *str = NULL;
21989
21990 attr = dwarf2_attr (die, name, cu);
21991
21992 if (attr != NULL)
21993 {
21994 str = attr->as_string ();
21995 if (str == nullptr)
21996 complaint (_("string type expected for attribute %s for "
21997 "DIE at %s in module %s"),
21998 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21999 objfile_name (cu->per_objfile->objfile));
22000 }
22001
22002 return str;
22003 }
22004
22005 /* Return the dwo name or NULL if not present. If present, it is in either
22006 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
22007 static const char *
22008 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
22009 {
22010 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
22011 if (dwo_name == nullptr)
22012 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
22013 return dwo_name;
22014 }
22015
22016 /* Return non-zero iff the attribute NAME is defined for the given DIE,
22017 and holds a non-zero value. This function should only be used for
22018 DW_FORM_flag or DW_FORM_flag_present attributes. */
22019
22020 static int
22021 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
22022 {
22023 struct attribute *attr = dwarf2_attr (die, name, cu);
22024
22025 return attr != nullptr && attr->as_boolean ();
22026 }
22027
22028 static int
22029 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
22030 {
22031 /* A DIE is a declaration if it has a DW_AT_declaration attribute
22032 which value is non-zero. However, we have to be careful with
22033 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
22034 (via dwarf2_flag_true_p) follows this attribute. So we may
22035 end up accidently finding a declaration attribute that belongs
22036 to a different DIE referenced by the specification attribute,
22037 even though the given DIE does not have a declaration attribute. */
22038 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
22039 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
22040 }
22041
22042 /* Return the die giving the specification for DIE, if there is
22043 one. *SPEC_CU is the CU containing DIE on input, and the CU
22044 containing the return value on output. If there is no
22045 specification, but there is an abstract origin, that is
22046 returned. */
22047
22048 static struct die_info *
22049 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
22050 {
22051 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
22052 *spec_cu);
22053
22054 if (spec_attr == NULL)
22055 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
22056
22057 if (spec_attr == NULL)
22058 return NULL;
22059 else
22060 return follow_die_ref (die, spec_attr, spec_cu);
22061 }
22062
22063 /* A convenience function to find the proper .debug_line section for a CU. */
22064
22065 static struct dwarf2_section_info *
22066 get_debug_line_section (struct dwarf2_cu *cu)
22067 {
22068 struct dwarf2_section_info *section;
22069 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22070
22071 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
22072 DWO file. */
22073 if (cu->dwo_unit && cu->per_cu->is_debug_types)
22074 section = &cu->dwo_unit->dwo_file->sections.line;
22075 else if (cu->per_cu->is_dwz)
22076 {
22077 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
22078
22079 section = &dwz->line;
22080 }
22081 else
22082 section = &per_objfile->per_bfd->line;
22083
22084 return section;
22085 }
22086
22087 /* Read the statement program header starting at OFFSET in
22088 .debug_line, or .debug_line.dwo. Return a pointer
22089 to a struct line_header, allocated using xmalloc.
22090 Returns NULL if there is a problem reading the header, e.g., if it
22091 has a version we don't understand.
22092
22093 NOTE: the strings in the include directory and file name tables of
22094 the returned object point into the dwarf line section buffer,
22095 and must not be freed. */
22096
22097 static line_header_up
22098 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
22099 {
22100 struct dwarf2_section_info *section;
22101 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22102
22103 section = get_debug_line_section (cu);
22104 section->read (per_objfile->objfile);
22105 if (section->buffer == NULL)
22106 {
22107 if (cu->dwo_unit && cu->per_cu->is_debug_types)
22108 complaint (_("missing .debug_line.dwo section"));
22109 else
22110 complaint (_("missing .debug_line section"));
22111 return 0;
22112 }
22113
22114 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
22115 per_objfile, section, &cu->header);
22116 }
22117
22118 /* Subroutine of dwarf_decode_lines to simplify it.
22119 Return the file name for the given file_entry.
22120 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
22121 If space for the result is malloc'd, *NAME_HOLDER will be set.
22122 Returns NULL if FILE_INDEX should be ignored, i.e., it is
22123 equivalent to CU_INFO. */
22124
22125 static const char *
22126 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
22127 const file_and_directory &cu_info,
22128 gdb::unique_xmalloc_ptr<char> *name_holder)
22129 {
22130 const char *include_name = fe.name;
22131 const char *include_name_to_compare = include_name;
22132
22133 const char *dir_name = fe.include_dir (lh);
22134
22135 gdb::unique_xmalloc_ptr<char> hold_compare;
22136 if (!IS_ABSOLUTE_PATH (include_name)
22137 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
22138 {
22139 /* Avoid creating a duplicate name for CU_INFO.
22140 We do this by comparing INCLUDE_NAME and CU_INFO.
22141 Before we do the comparison, however, we need to account
22142 for DIR_NAME and COMP_DIR.
22143 First prepend dir_name (if non-NULL). If we still don't
22144 have an absolute path prepend comp_dir (if non-NULL).
22145 However, the directory we record in the include-file's
22146 psymtab does not contain COMP_DIR (to match the
22147 corresponding symtab(s)).
22148
22149 Example:
22150
22151 bash$ cd /tmp
22152 bash$ gcc -g ./hello.c
22153 include_name = "hello.c"
22154 dir_name = "."
22155 DW_AT_comp_dir = comp_dir = "/tmp"
22156 DW_AT_name = "./hello.c"
22157
22158 */
22159
22160 if (dir_name != NULL)
22161 {
22162 name_holder->reset (concat (dir_name, SLASH_STRING,
22163 include_name, (char *) NULL));
22164 include_name = name_holder->get ();
22165 include_name_to_compare = include_name;
22166 }
22167 if (!IS_ABSOLUTE_PATH (include_name)
22168 && cu_info.get_comp_dir () != nullptr)
22169 {
22170 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
22171 include_name, (char *) NULL));
22172 include_name_to_compare = hold_compare.get ();
22173 }
22174 }
22175
22176 gdb::unique_xmalloc_ptr<char> copied_name;
22177 const char *cu_filename = cu_info.get_name ();
22178 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
22179 {
22180 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
22181 cu_filename, (char *) NULL));
22182 cu_filename = copied_name.get ();
22183 }
22184
22185 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
22186 return nullptr;
22187 return include_name;
22188 }
22189
22190 /* State machine to track the state of the line number program. */
22191
22192 class lnp_state_machine
22193 {
22194 public:
22195 /* Initialize a machine state for the start of a line number
22196 program. */
22197 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
22198 bool record_lines_p);
22199
22200 file_entry *current_file ()
22201 {
22202 /* lh->file_names is 0-based, but the file name numbers in the
22203 statement program are 1-based. */
22204 return m_line_header->file_name_at (m_file);
22205 }
22206
22207 /* Record the line in the state machine. END_SEQUENCE is true if
22208 we're processing the end of a sequence. */
22209 void record_line (bool end_sequence);
22210
22211 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
22212 nop-out rest of the lines in this sequence. */
22213 void check_line_address (struct dwarf2_cu *cu,
22214 const gdb_byte *line_ptr,
22215 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
22216
22217 void handle_set_discriminator (unsigned int discriminator)
22218 {
22219 m_discriminator = discriminator;
22220 m_line_has_non_zero_discriminator |= discriminator != 0;
22221 }
22222
22223 /* Handle DW_LNE_set_address. */
22224 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
22225 {
22226 m_op_index = 0;
22227 address += baseaddr;
22228 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
22229 }
22230
22231 /* Handle DW_LNS_advance_pc. */
22232 void handle_advance_pc (CORE_ADDR adjust);
22233
22234 /* Handle a special opcode. */
22235 void handle_special_opcode (unsigned char op_code);
22236
22237 /* Handle DW_LNS_advance_line. */
22238 void handle_advance_line (int line_delta)
22239 {
22240 advance_line (line_delta);
22241 }
22242
22243 /* Handle DW_LNS_set_file. */
22244 void handle_set_file (file_name_index file);
22245
22246 /* Handle DW_LNS_negate_stmt. */
22247 void handle_negate_stmt ()
22248 {
22249 m_flags ^= LEF_IS_STMT;
22250 }
22251
22252 /* Handle DW_LNS_const_add_pc. */
22253 void handle_const_add_pc ();
22254
22255 /* Handle DW_LNS_fixed_advance_pc. */
22256 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
22257 {
22258 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
22259 m_op_index = 0;
22260 }
22261
22262 /* Handle DW_LNS_copy. */
22263 void handle_copy ()
22264 {
22265 record_line (false);
22266 m_discriminator = 0;
22267 m_flags &= ~LEF_PROLOGUE_END;
22268 }
22269
22270 /* Handle DW_LNE_end_sequence. */
22271 void handle_end_sequence ()
22272 {
22273 m_currently_recording_lines = true;
22274 }
22275
22276 /* Handle DW_LNS_set_prologue_end. */
22277 void handle_set_prologue_end ()
22278 {
22279 m_flags |= LEF_PROLOGUE_END;
22280 }
22281
22282 private:
22283 /* Advance the line by LINE_DELTA. */
22284 void advance_line (int line_delta)
22285 {
22286 m_line += line_delta;
22287
22288 if (line_delta != 0)
22289 m_line_has_non_zero_discriminator = m_discriminator != 0;
22290 }
22291
22292 struct dwarf2_cu *m_cu;
22293
22294 gdbarch *m_gdbarch;
22295
22296 /* True if we're recording lines.
22297 Otherwise we're building partial symtabs and are just interested in
22298 finding include files mentioned by the line number program. */
22299 bool m_record_lines_p;
22300
22301 /* The line number header. */
22302 line_header *m_line_header;
22303
22304 /* These are part of the standard DWARF line number state machine,
22305 and initialized according to the DWARF spec. */
22306
22307 unsigned char m_op_index = 0;
22308 /* The line table index of the current file. */
22309 file_name_index m_file = 1;
22310 unsigned int m_line = 1;
22311
22312 /* These are initialized in the constructor. */
22313
22314 CORE_ADDR m_address;
22315 linetable_entry_flags m_flags;
22316 unsigned int m_discriminator;
22317
22318 /* Additional bits of state we need to track. */
22319
22320 /* The last file that we called dwarf2_start_subfile for.
22321 This is only used for TLLs. */
22322 unsigned int m_last_file = 0;
22323 /* The last file a line number was recorded for. */
22324 struct subfile *m_last_subfile = NULL;
22325
22326 /* The address of the last line entry. */
22327 CORE_ADDR m_last_address;
22328
22329 /* Set to true when a previous line at the same address (using
22330 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
22331 when a line entry at a new address (m_address different to
22332 m_last_address) is processed. */
22333 bool m_stmt_at_address = false;
22334
22335 /* When true, record the lines we decode. */
22336 bool m_currently_recording_lines = false;
22337
22338 /* The last line number that was recorded, used to coalesce
22339 consecutive entries for the same line. This can happen, for
22340 example, when discriminators are present. PR 17276. */
22341 unsigned int m_last_line = 0;
22342 bool m_line_has_non_zero_discriminator = false;
22343 };
22344
22345 void
22346 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
22347 {
22348 CORE_ADDR addr_adj = (((m_op_index + adjust)
22349 / m_line_header->maximum_ops_per_instruction)
22350 * m_line_header->minimum_instruction_length);
22351 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
22352 m_op_index = ((m_op_index + adjust)
22353 % m_line_header->maximum_ops_per_instruction);
22354 }
22355
22356 void
22357 lnp_state_machine::handle_special_opcode (unsigned char op_code)
22358 {
22359 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
22360 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
22361 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
22362 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
22363 / m_line_header->maximum_ops_per_instruction)
22364 * m_line_header->minimum_instruction_length);
22365 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
22366 m_op_index = ((m_op_index + adj_opcode_d)
22367 % m_line_header->maximum_ops_per_instruction);
22368
22369 int line_delta = m_line_header->line_base + adj_opcode_r;
22370 advance_line (line_delta);
22371 record_line (false);
22372 m_discriminator = 0;
22373 m_flags &= ~LEF_PROLOGUE_END;
22374 }
22375
22376 void
22377 lnp_state_machine::handle_set_file (file_name_index file)
22378 {
22379 m_file = file;
22380
22381 const file_entry *fe = current_file ();
22382 if (fe == NULL)
22383 dwarf2_debug_line_missing_file_complaint ();
22384 else if (m_record_lines_p)
22385 {
22386 const char *dir = fe->include_dir (m_line_header);
22387
22388 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
22389 m_line_has_non_zero_discriminator = m_discriminator != 0;
22390 dwarf2_start_subfile (m_cu, fe->name, dir);
22391 }
22392 }
22393
22394 void
22395 lnp_state_machine::handle_const_add_pc ()
22396 {
22397 CORE_ADDR adjust
22398 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
22399
22400 CORE_ADDR addr_adj
22401 = (((m_op_index + adjust)
22402 / m_line_header->maximum_ops_per_instruction)
22403 * m_line_header->minimum_instruction_length);
22404
22405 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
22406 m_op_index = ((m_op_index + adjust)
22407 % m_line_header->maximum_ops_per_instruction);
22408 }
22409
22410 /* Return non-zero if we should add LINE to the line number table.
22411 LINE is the line to add, LAST_LINE is the last line that was added,
22412 LAST_SUBFILE is the subfile for LAST_LINE.
22413 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
22414 had a non-zero discriminator.
22415
22416 We have to be careful in the presence of discriminators.
22417 E.g., for this line:
22418
22419 for (i = 0; i < 100000; i++);
22420
22421 clang can emit four line number entries for that one line,
22422 each with a different discriminator.
22423 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
22424
22425 However, we want gdb to coalesce all four entries into one.
22426 Otherwise the user could stepi into the middle of the line and
22427 gdb would get confused about whether the pc really was in the
22428 middle of the line.
22429
22430 Things are further complicated by the fact that two consecutive
22431 line number entries for the same line is a heuristic used by gcc
22432 to denote the end of the prologue. So we can't just discard duplicate
22433 entries, we have to be selective about it. The heuristic we use is
22434 that we only collapse consecutive entries for the same line if at least
22435 one of those entries has a non-zero discriminator. PR 17276.
22436
22437 Note: Addresses in the line number state machine can never go backwards
22438 within one sequence, thus this coalescing is ok. */
22439
22440 static int
22441 dwarf_record_line_p (struct dwarf2_cu *cu,
22442 unsigned int line, unsigned int last_line,
22443 int line_has_non_zero_discriminator,
22444 struct subfile *last_subfile)
22445 {
22446 if (cu->get_builder ()->get_current_subfile () != last_subfile)
22447 return 1;
22448 if (line != last_line)
22449 return 1;
22450 /* Same line for the same file that we've seen already.
22451 As a last check, for pr 17276, only record the line if the line
22452 has never had a non-zero discriminator. */
22453 if (!line_has_non_zero_discriminator)
22454 return 1;
22455 return 0;
22456 }
22457
22458 /* Use the CU's builder to record line number LINE beginning at
22459 address ADDRESS in the line table of subfile SUBFILE. */
22460
22461 static void
22462 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
22463 unsigned int line, CORE_ADDR address,
22464 linetable_entry_flags flags,
22465 struct dwarf2_cu *cu)
22466 {
22467 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
22468
22469 if (dwarf_line_debug)
22470 {
22471 gdb_printf (gdb_stdlog,
22472 "Recording line %u, file %s, address %s\n",
22473 line, lbasename (subfile->name),
22474 paddress (gdbarch, address));
22475 }
22476
22477 if (cu != nullptr)
22478 cu->get_builder ()->record_line (subfile, line, addr, flags);
22479 }
22480
22481 /* Subroutine of dwarf_decode_lines_1 to simplify it.
22482 Mark the end of a set of line number records.
22483 The arguments are the same as for dwarf_record_line_1.
22484 If SUBFILE is NULL the request is ignored. */
22485
22486 static void
22487 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
22488 CORE_ADDR address, struct dwarf2_cu *cu)
22489 {
22490 if (subfile == NULL)
22491 return;
22492
22493 if (dwarf_line_debug)
22494 {
22495 gdb_printf (gdb_stdlog,
22496 "Finishing current line, file %s, address %s\n",
22497 lbasename (subfile->name),
22498 paddress (gdbarch, address));
22499 }
22500
22501 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
22502 }
22503
22504 void
22505 lnp_state_machine::record_line (bool end_sequence)
22506 {
22507 if (dwarf_line_debug)
22508 {
22509 gdb_printf (gdb_stdlog,
22510 "Processing actual line %u: file %u,"
22511 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
22512 m_line, m_file,
22513 paddress (m_gdbarch, m_address),
22514 (m_flags & LEF_IS_STMT) != 0,
22515 (m_flags & LEF_PROLOGUE_END) != 0,
22516 m_discriminator,
22517 (end_sequence ? "\t(end sequence)" : ""));
22518 }
22519
22520 file_entry *fe = current_file ();
22521
22522 if (fe == NULL)
22523 dwarf2_debug_line_missing_file_complaint ();
22524 /* For now we ignore lines not starting on an instruction boundary.
22525 But not when processing end_sequence for compatibility with the
22526 previous version of the code. */
22527 else if (m_op_index == 0 || end_sequence)
22528 {
22529 fe->included_p = true;
22530 if (m_record_lines_p)
22531 {
22532 /* When we switch files we insert an end maker in the first file,
22533 switch to the second file and add a new line entry. The
22534 problem is that the end marker inserted in the first file will
22535 discard any previous line entries at the same address. If the
22536 line entries in the first file are marked as is-stmt, while
22537 the new line in the second file is non-stmt, then this means
22538 the end marker will discard is-stmt lines so we can have a
22539 non-stmt line. This means that there are less addresses at
22540 which the user can insert a breakpoint.
22541
22542 To improve this we track the last address in m_last_address,
22543 and whether we have seen an is-stmt at this address. Then
22544 when switching files, if we have seen a stmt at the current
22545 address, and we are switching to create a non-stmt line, then
22546 discard the new line. */
22547 bool file_changed
22548 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
22549 bool ignore_this_line
22550 = ((file_changed && !end_sequence && m_last_address == m_address
22551 && ((m_flags & LEF_IS_STMT) == 0)
22552 && m_stmt_at_address)
22553 || (!end_sequence && m_line == 0));
22554
22555 if ((file_changed && !ignore_this_line) || end_sequence)
22556 {
22557 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
22558 m_currently_recording_lines ? m_cu : nullptr);
22559 }
22560
22561 if (!end_sequence && !ignore_this_line)
22562 {
22563 linetable_entry_flags lte_flags = m_flags;
22564 if (producer_is_codewarrior (m_cu))
22565 lte_flags |= LEF_IS_STMT;
22566
22567 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
22568 m_line_has_non_zero_discriminator,
22569 m_last_subfile))
22570 {
22571 buildsym_compunit *builder = m_cu->get_builder ();
22572 dwarf_record_line_1 (m_gdbarch,
22573 builder->get_current_subfile (),
22574 m_line, m_address, lte_flags,
22575 m_currently_recording_lines ? m_cu : nullptr);
22576 }
22577 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
22578 m_last_line = m_line;
22579 }
22580 }
22581 }
22582
22583 /* Track whether we have seen any IS_STMT true at m_address in case we
22584 have multiple line table entries all at m_address. */
22585 if (m_last_address != m_address)
22586 {
22587 m_stmt_at_address = false;
22588 m_last_address = m_address;
22589 }
22590 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
22591 }
22592
22593 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
22594 line_header *lh, bool record_lines_p)
22595 {
22596 m_cu = cu;
22597 m_gdbarch = arch;
22598 m_record_lines_p = record_lines_p;
22599 m_line_header = lh;
22600
22601 m_currently_recording_lines = true;
22602
22603 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
22604 was a line entry for it so that the backend has a chance to adjust it
22605 and also record it in case it needs it. This is currently used by MIPS
22606 code, cf. `mips_adjust_dwarf2_line'. */
22607 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
22608 m_flags = 0;
22609 if (lh->default_is_stmt)
22610 m_flags |= LEF_IS_STMT;
22611 m_discriminator = 0;
22612
22613 m_last_address = m_address;
22614 m_stmt_at_address = false;
22615 }
22616
22617 void
22618 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
22619 const gdb_byte *line_ptr,
22620 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
22621 {
22622 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
22623 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
22624 located at 0x0. In this case, additionally check that if
22625 ADDRESS < UNRELOCATED_LOWPC. */
22626
22627 if ((address == 0 && address < unrelocated_lowpc)
22628 || address == (CORE_ADDR) -1)
22629 {
22630 /* This line table is for a function which has been
22631 GCd by the linker. Ignore it. PR gdb/12528 */
22632
22633 struct objfile *objfile = cu->per_objfile->objfile;
22634 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
22635
22636 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
22637 line_offset, objfile_name (objfile));
22638 m_currently_recording_lines = false;
22639 /* Note: m_currently_recording_lines is left as false until we see
22640 DW_LNE_end_sequence. */
22641 }
22642 }
22643
22644 /* Subroutine of dwarf_decode_lines to simplify it.
22645 Process the line number information in LH.
22646 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
22647 program in order to set included_p for every referenced header. */
22648
22649 static void
22650 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
22651 const int decode_for_pst_p, CORE_ADDR lowpc)
22652 {
22653 const gdb_byte *line_ptr, *extended_end;
22654 const gdb_byte *line_end;
22655 unsigned int bytes_read, extended_len;
22656 unsigned char op_code, extended_op;
22657 CORE_ADDR baseaddr;
22658 struct objfile *objfile = cu->per_objfile->objfile;
22659 bfd *abfd = objfile->obfd;
22660 struct gdbarch *gdbarch = objfile->arch ();
22661 /* True if we're recording line info (as opposed to building partial
22662 symtabs and just interested in finding include files mentioned by
22663 the line number program). */
22664 bool record_lines_p = !decode_for_pst_p;
22665
22666 baseaddr = objfile->text_section_offset ();
22667
22668 line_ptr = lh->statement_program_start;
22669 line_end = lh->statement_program_end;
22670
22671 /* Read the statement sequences until there's nothing left. */
22672 while (line_ptr < line_end)
22673 {
22674 /* The DWARF line number program state machine. Reset the state
22675 machine at the start of each sequence. */
22676 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
22677 bool end_sequence = false;
22678
22679 if (record_lines_p)
22680 {
22681 /* Start a subfile for the current file of the state
22682 machine. */
22683 const file_entry *fe = state_machine.current_file ();
22684
22685 if (fe != NULL)
22686 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
22687 }
22688
22689 /* Decode the table. */
22690 while (line_ptr < line_end && !end_sequence)
22691 {
22692 op_code = read_1_byte (abfd, line_ptr);
22693 line_ptr += 1;
22694
22695 if (op_code >= lh->opcode_base)
22696 {
22697 /* Special opcode. */
22698 state_machine.handle_special_opcode (op_code);
22699 }
22700 else switch (op_code)
22701 {
22702 case DW_LNS_extended_op:
22703 extended_len = read_unsigned_leb128 (abfd, line_ptr,
22704 &bytes_read);
22705 line_ptr += bytes_read;
22706 extended_end = line_ptr + extended_len;
22707 extended_op = read_1_byte (abfd, line_ptr);
22708 line_ptr += 1;
22709 if (DW_LNE_lo_user <= extended_op
22710 && extended_op <= DW_LNE_hi_user)
22711 {
22712 /* Vendor extension, ignore. */
22713 line_ptr = extended_end;
22714 break;
22715 }
22716 switch (extended_op)
22717 {
22718 case DW_LNE_end_sequence:
22719 state_machine.handle_end_sequence ();
22720 end_sequence = true;
22721 break;
22722 case DW_LNE_set_address:
22723 {
22724 CORE_ADDR address
22725 = cu->header.read_address (abfd, line_ptr, &bytes_read);
22726 line_ptr += bytes_read;
22727
22728 state_machine.check_line_address (cu, line_ptr,
22729 lowpc - baseaddr, address);
22730 state_machine.handle_set_address (baseaddr, address);
22731 }
22732 break;
22733 case DW_LNE_define_file:
22734 {
22735 const char *cur_file;
22736 unsigned int mod_time, length;
22737 dir_index dindex;
22738
22739 cur_file = read_direct_string (abfd, line_ptr,
22740 &bytes_read);
22741 line_ptr += bytes_read;
22742 dindex = (dir_index)
22743 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22744 line_ptr += bytes_read;
22745 mod_time =
22746 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22747 line_ptr += bytes_read;
22748 length =
22749 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22750 line_ptr += bytes_read;
22751 lh->add_file_name (cur_file, dindex, mod_time, length);
22752 }
22753 break;
22754 case DW_LNE_set_discriminator:
22755 {
22756 /* The discriminator is not interesting to the
22757 debugger; just ignore it. We still need to
22758 check its value though:
22759 if there are consecutive entries for the same
22760 (non-prologue) line we want to coalesce them.
22761 PR 17276. */
22762 unsigned int discr
22763 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22764 line_ptr += bytes_read;
22765
22766 state_machine.handle_set_discriminator (discr);
22767 }
22768 break;
22769 default:
22770 complaint (_("mangled .debug_line section"));
22771 return;
22772 }
22773 /* Make sure that we parsed the extended op correctly. If e.g.
22774 we expected a different address size than the producer used,
22775 we may have read the wrong number of bytes. */
22776 if (line_ptr != extended_end)
22777 {
22778 complaint (_("mangled .debug_line section"));
22779 return;
22780 }
22781 break;
22782 case DW_LNS_copy:
22783 state_machine.handle_copy ();
22784 break;
22785 case DW_LNS_advance_pc:
22786 {
22787 CORE_ADDR adjust
22788 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22789 line_ptr += bytes_read;
22790
22791 state_machine.handle_advance_pc (adjust);
22792 }
22793 break;
22794 case DW_LNS_advance_line:
22795 {
22796 int line_delta
22797 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
22798 line_ptr += bytes_read;
22799
22800 state_machine.handle_advance_line (line_delta);
22801 }
22802 break;
22803 case DW_LNS_set_file:
22804 {
22805 file_name_index file
22806 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
22807 &bytes_read);
22808 line_ptr += bytes_read;
22809
22810 state_machine.handle_set_file (file);
22811 }
22812 break;
22813 case DW_LNS_set_column:
22814 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22815 line_ptr += bytes_read;
22816 break;
22817 case DW_LNS_negate_stmt:
22818 state_machine.handle_negate_stmt ();
22819 break;
22820 case DW_LNS_set_basic_block:
22821 break;
22822 /* Add to the address register of the state machine the
22823 address increment value corresponding to special opcode
22824 255. I.e., this value is scaled by the minimum
22825 instruction length since special opcode 255 would have
22826 scaled the increment. */
22827 case DW_LNS_const_add_pc:
22828 state_machine.handle_const_add_pc ();
22829 break;
22830 case DW_LNS_fixed_advance_pc:
22831 {
22832 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
22833 line_ptr += 2;
22834
22835 state_machine.handle_fixed_advance_pc (addr_adj);
22836 }
22837 break;
22838 case DW_LNS_set_prologue_end:
22839 state_machine.handle_set_prologue_end ();
22840 break;
22841 default:
22842 {
22843 /* Unknown standard opcode, ignore it. */
22844 int i;
22845
22846 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
22847 {
22848 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22849 line_ptr += bytes_read;
22850 }
22851 }
22852 }
22853 }
22854
22855 if (!end_sequence)
22856 dwarf2_debug_line_missing_end_sequence_complaint ();
22857
22858 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
22859 in which case we still finish recording the last line). */
22860 state_machine.record_line (true);
22861 }
22862 }
22863
22864 /* Decode the Line Number Program (LNP) for the given line_header
22865 structure and CU. The actual information extracted and the type
22866 of structures created from the LNP depends on the value of PST.
22867
22868 1. If PST is NULL, then this procedure uses the data from the program
22869 to create all necessary symbol tables, and their linetables.
22870
22871 2. If PST is not NULL, this procedure reads the program to determine
22872 the list of files included by the unit represented by PST, and
22873 builds all the associated partial symbol tables.
22874
22875 FND holds the CU file name and directory, if known.
22876 It is used for relative paths in the line table.
22877
22878 NOTE: It is important that psymtabs have the same file name (via
22879 strcmp) as the corresponding symtab. Since the directory is not
22880 used in the name of the symtab we don't use it in the name of the
22881 psymtabs we create. E.g. expand_line_sal requires this when
22882 finding psymtabs to expand. A good testcase for this is
22883 mb-inline.exp.
22884
22885 LOWPC is the lowest address in CU (or 0 if not known).
22886
22887 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22888 for its PC<->lines mapping information. Otherwise only the filename
22889 table is read in. */
22890
22891 static void
22892 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
22893 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
22894 CORE_ADDR lowpc, int decode_mapping)
22895 {
22896 struct objfile *objfile = cu->per_objfile->objfile;
22897 const int decode_for_pst_p = (pst != NULL);
22898
22899 if (decode_mapping)
22900 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
22901
22902 if (decode_for_pst_p)
22903 {
22904 /* Now that we're done scanning the Line Header Program, we can
22905 create the psymtab of each included file. */
22906 for (auto &file_entry : lh->file_names ())
22907 if (file_entry.included_p)
22908 {
22909 gdb::unique_xmalloc_ptr<char> name_holder;
22910 const char *include_name =
22911 compute_include_file_name (lh, file_entry, fnd, &name_holder);
22912 if (include_name != NULL)
22913 dwarf2_create_include_psymtab
22914 (cu->per_objfile->per_bfd, include_name, pst,
22915 cu->per_objfile->per_bfd->partial_symtabs.get (),
22916 objfile->per_bfd);
22917 }
22918 }
22919 else
22920 {
22921 /* Make sure a symtab is created for every file, even files
22922 which contain only variables (i.e. no code with associated
22923 line numbers). */
22924 buildsym_compunit *builder = cu->get_builder ();
22925 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22926
22927 for (auto &fe : lh->file_names ())
22928 {
22929 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22930 if (builder->get_current_subfile ()->symtab == NULL)
22931 {
22932 builder->get_current_subfile ()->symtab
22933 = allocate_symtab (cust,
22934 builder->get_current_subfile ()->name);
22935 }
22936 fe.symtab = builder->get_current_subfile ()->symtab;
22937 }
22938 }
22939 }
22940
22941 /* Start a subfile for DWARF. FILENAME is the name of the file and
22942 DIRNAME the name of the source directory which contains FILENAME
22943 or NULL if not known.
22944 This routine tries to keep line numbers from identical absolute and
22945 relative file names in a common subfile.
22946
22947 Using the `list' example from the GDB testsuite, which resides in
22948 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22949 of /srcdir/list0.c yields the following debugging information for list0.c:
22950
22951 DW_AT_name: /srcdir/list0.c
22952 DW_AT_comp_dir: /compdir
22953 files.files[0].name: list0.h
22954 files.files[0].dir: /srcdir
22955 files.files[1].name: list0.c
22956 files.files[1].dir: /srcdir
22957
22958 The line number information for list0.c has to end up in a single
22959 subfile, so that `break /srcdir/list0.c:1' works as expected.
22960 start_subfile will ensure that this happens provided that we pass the
22961 concatenation of files.files[1].dir and files.files[1].name as the
22962 subfile's name. */
22963
22964 static void
22965 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22966 const char *dirname)
22967 {
22968 gdb::unique_xmalloc_ptr<char> copy;
22969
22970 /* In order not to lose the line information directory,
22971 we concatenate it to the filename when it makes sense.
22972 Note that the Dwarf3 standard says (speaking of filenames in line
22973 information): ``The directory index is ignored for file names
22974 that represent full path names''. Thus ignoring dirname in the
22975 `else' branch below isn't an issue. */
22976
22977 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22978 {
22979 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22980 filename = copy.get ();
22981 }
22982
22983 cu->get_builder ()->start_subfile (filename);
22984 }
22985
22986 static void
22987 var_decode_location (struct attribute *attr, struct symbol *sym,
22988 struct dwarf2_cu *cu)
22989 {
22990 struct objfile *objfile = cu->per_objfile->objfile;
22991 struct comp_unit_head *cu_header = &cu->header;
22992
22993 /* NOTE drow/2003-01-30: There used to be a comment and some special
22994 code here to turn a symbol with DW_AT_external and a
22995 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22996 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22997 with some versions of binutils) where shared libraries could have
22998 relocations against symbols in their debug information - the
22999 minimal symbol would have the right address, but the debug info
23000 would not. It's no longer necessary, because we will explicitly
23001 apply relocations when we read in the debug information now. */
23002
23003 /* A DW_AT_location attribute with no contents indicates that a
23004 variable has been optimized away. */
23005 if (attr->form_is_block () && attr->as_block ()->size == 0)
23006 {
23007 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
23008 return;
23009 }
23010
23011 /* Handle one degenerate form of location expression specially, to
23012 preserve GDB's previous behavior when section offsets are
23013 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
23014 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
23015
23016 if (attr->form_is_block ())
23017 {
23018 struct dwarf_block *block = attr->as_block ();
23019
23020 if ((block->data[0] == DW_OP_addr
23021 && block->size == 1 + cu_header->addr_size)
23022 || ((block->data[0] == DW_OP_GNU_addr_index
23023 || block->data[0] == DW_OP_addrx)
23024 && (block->size
23025 == 1 + leb128_size (&block->data[1]))))
23026 {
23027 unsigned int dummy;
23028
23029 if (block->data[0] == DW_OP_addr)
23030 sym->set_value_address
23031 (cu->header.read_address (objfile->obfd, block->data + 1,
23032 &dummy));
23033 else
23034 sym->set_value_address
23035 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
23036 sym->set_aclass_index (LOC_STATIC);
23037 fixup_symbol_section (sym, objfile);
23038 sym->set_value_address
23039 (sym->value_address ()
23040 + objfile->section_offsets[sym->section_index ()]);
23041 return;
23042 }
23043 }
23044
23045 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
23046 expression evaluator, and use LOC_COMPUTED only when necessary
23047 (i.e. when the value of a register or memory location is
23048 referenced, or a thread-local block, etc.). Then again, it might
23049 not be worthwhile. I'm assuming that it isn't unless performance
23050 or memory numbers show me otherwise. */
23051
23052 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
23053
23054 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
23055 cu->has_loclist = true;
23056 }
23057
23058 /* Given a pointer to a DWARF information entry, figure out if we need
23059 to make a symbol table entry for it, and if so, create a new entry
23060 and return a pointer to it.
23061 If TYPE is NULL, determine symbol type from the die, otherwise
23062 used the passed type.
23063 If SPACE is not NULL, use it to hold the new symbol. If it is
23064 NULL, allocate a new symbol on the objfile's obstack. */
23065
23066 static struct symbol *
23067 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23068 struct symbol *space)
23069 {
23070 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23071 struct objfile *objfile = per_objfile->objfile;
23072 struct gdbarch *gdbarch = objfile->arch ();
23073 struct symbol *sym = NULL;
23074 const char *name;
23075 struct attribute *attr = NULL;
23076 struct attribute *attr2 = NULL;
23077 CORE_ADDR baseaddr;
23078 struct pending **list_to_add = NULL;
23079
23080 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
23081
23082 baseaddr = objfile->text_section_offset ();
23083
23084 name = dwarf2_name (die, cu);
23085 if (name)
23086 {
23087 int suppress_add = 0;
23088
23089 if (space)
23090 sym = space;
23091 else
23092 sym = new (&objfile->objfile_obstack) symbol;
23093 OBJSTAT (objfile, n_syms++);
23094
23095 /* Cache this symbol's name and the name's demangled form (if any). */
23096 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
23097 /* Fortran does not have mangling standard and the mangling does differ
23098 between gfortran, iFort etc. */
23099 const char *physname
23100 = (cu->per_cu->lang == language_fortran
23101 ? dwarf2_full_name (name, die, cu)
23102 : dwarf2_physname (name, die, cu));
23103 const char *linkagename = dw2_linkage_name (die, cu);
23104
23105 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
23106 sym->set_linkage_name (physname);
23107 else
23108 {
23109 sym->set_demangled_name (physname, &objfile->objfile_obstack);
23110 sym->set_linkage_name (linkagename);
23111 }
23112
23113 /* Handle DW_AT_artificial. */
23114 attr = dwarf2_attr (die, DW_AT_artificial, cu);
23115 if (attr != nullptr)
23116 sym->artificial = attr->as_boolean ();
23117
23118 /* Default assumptions.
23119 Use the passed type or decode it from the die. */
23120 sym->set_domain (VAR_DOMAIN);
23121 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
23122 if (type != NULL)
23123 sym->set_type (type);
23124 else
23125 sym->set_type (die_type (die, cu));
23126 attr = dwarf2_attr (die,
23127 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
23128 cu);
23129 if (attr != nullptr)
23130 sym->set_line (attr->constant_value (0));
23131
23132 attr = dwarf2_attr (die,
23133 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
23134 cu);
23135 if (attr != nullptr && attr->is_nonnegative ())
23136 {
23137 file_name_index file_index
23138 = (file_name_index) attr->as_nonnegative ();
23139 struct file_entry *fe;
23140
23141 if (cu->line_header != NULL)
23142 fe = cu->line_header->file_name_at (file_index);
23143 else
23144 fe = NULL;
23145
23146 if (fe == NULL)
23147 complaint (_("file index out of range"));
23148 else
23149 symbol_set_symtab (sym, fe->symtab);
23150 }
23151
23152 switch (die->tag)
23153 {
23154 case DW_TAG_label:
23155 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
23156 if (attr != nullptr)
23157 {
23158 CORE_ADDR addr;
23159
23160 addr = attr->as_address ();
23161 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
23162 sym->set_value_address (addr);
23163 sym->set_aclass_index (LOC_LABEL);
23164 }
23165 else
23166 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
23167 sym->set_type (objfile_type (objfile)->builtin_core_addr);
23168 sym->set_domain (LABEL_DOMAIN);
23169 add_symbol_to_list (sym, cu->list_in_scope);
23170 break;
23171 case DW_TAG_subprogram:
23172 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
23173 finish_block. */
23174 sym->set_aclass_index (LOC_BLOCK);
23175 attr2 = dwarf2_attr (die, DW_AT_external, cu);
23176 if ((attr2 != nullptr && attr2->as_boolean ())
23177 || cu->per_cu->lang == language_ada
23178 || cu->per_cu->lang == language_fortran)
23179 {
23180 /* Subprograms marked external are stored as a global symbol.
23181 Ada and Fortran subprograms, whether marked external or
23182 not, are always stored as a global symbol, because we want
23183 to be able to access them globally. For instance, we want
23184 to be able to break on a nested subprogram without having
23185 to specify the context. */
23186 list_to_add = cu->get_builder ()->get_global_symbols ();
23187 }
23188 else
23189 {
23190 list_to_add = cu->list_in_scope;
23191 }
23192 break;
23193 case DW_TAG_inlined_subroutine:
23194 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
23195 finish_block. */
23196 sym->set_aclass_index (LOC_BLOCK);
23197 sym->set_is_inlined (1);
23198 list_to_add = cu->list_in_scope;
23199 break;
23200 case DW_TAG_template_value_param:
23201 suppress_add = 1;
23202 /* Fall through. */
23203 case DW_TAG_constant:
23204 case DW_TAG_variable:
23205 case DW_TAG_member:
23206 /* Compilation with minimal debug info may result in
23207 variables with missing type entries. Change the
23208 misleading `void' type to something sensible. */
23209 if (sym->type ()->code () == TYPE_CODE_VOID)
23210 sym->set_type (objfile_type (objfile)->builtin_int);
23211
23212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23213 /* In the case of DW_TAG_member, we should only be called for
23214 static const members. */
23215 if (die->tag == DW_TAG_member)
23216 {
23217 /* dwarf2_add_field uses die_is_declaration,
23218 so we do the same. */
23219 gdb_assert (die_is_declaration (die, cu));
23220 gdb_assert (attr);
23221 }
23222 if (attr != nullptr)
23223 {
23224 dwarf2_const_value (attr, sym, cu);
23225 attr2 = dwarf2_attr (die, DW_AT_external, cu);
23226 if (!suppress_add)
23227 {
23228 if (attr2 != nullptr && attr2->as_boolean ())
23229 list_to_add = cu->get_builder ()->get_global_symbols ();
23230 else
23231 list_to_add = cu->list_in_scope;
23232 }
23233 break;
23234 }
23235 attr = dwarf2_attr (die, DW_AT_location, cu);
23236 if (attr != nullptr)
23237 {
23238 var_decode_location (attr, sym, cu);
23239 attr2 = dwarf2_attr (die, DW_AT_external, cu);
23240
23241 /* Fortran explicitly imports any global symbols to the local
23242 scope by DW_TAG_common_block. */
23243 if (cu->per_cu->lang == language_fortran && die->parent
23244 && die->parent->tag == DW_TAG_common_block)
23245 attr2 = NULL;
23246
23247 if (sym->aclass () == LOC_STATIC
23248 && sym->value_address () == 0
23249 && !per_objfile->per_bfd->has_section_at_zero)
23250 {
23251 /* When a static variable is eliminated by the linker,
23252 the corresponding debug information is not stripped
23253 out, but the variable address is set to null;
23254 do not add such variables into symbol table. */
23255 }
23256 else if (attr2 != nullptr && attr2->as_boolean ())
23257 {
23258 if (sym->aclass () == LOC_STATIC
23259 && (objfile->flags & OBJF_MAINLINE) == 0
23260 && per_objfile->per_bfd->can_copy)
23261 {
23262 /* A global static variable might be subject to
23263 copy relocation. We first check for a local
23264 minsym, though, because maybe the symbol was
23265 marked hidden, in which case this would not
23266 apply. */
23267 bound_minimal_symbol found
23268 = (lookup_minimal_symbol_linkage
23269 (sym->linkage_name (), objfile));
23270 if (found.minsym != nullptr)
23271 sym->maybe_copied = 1;
23272 }
23273
23274 /* A variable with DW_AT_external is never static,
23275 but it may be block-scoped. */
23276 list_to_add
23277 = ((cu->list_in_scope
23278 == cu->get_builder ()->get_file_symbols ())
23279 ? cu->get_builder ()->get_global_symbols ()
23280 : cu->list_in_scope);
23281 }
23282 else
23283 list_to_add = cu->list_in_scope;
23284 }
23285 else
23286 {
23287 /* We do not know the address of this symbol.
23288 If it is an external symbol and we have type information
23289 for it, enter the symbol as a LOC_UNRESOLVED symbol.
23290 The address of the variable will then be determined from
23291 the minimal symbol table whenever the variable is
23292 referenced. */
23293 attr2 = dwarf2_attr (die, DW_AT_external, cu);
23294
23295 /* Fortran explicitly imports any global symbols to the local
23296 scope by DW_TAG_common_block. */
23297 if (cu->per_cu->lang == language_fortran && die->parent
23298 && die->parent->tag == DW_TAG_common_block)
23299 {
23300 /* SYMBOL_CLASS doesn't matter here because
23301 read_common_block is going to reset it. */
23302 if (!suppress_add)
23303 list_to_add = cu->list_in_scope;
23304 }
23305 else if (attr2 != nullptr && attr2->as_boolean ()
23306 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
23307 {
23308 /* A variable with DW_AT_external is never static, but it
23309 may be block-scoped. */
23310 list_to_add
23311 = ((cu->list_in_scope
23312 == cu->get_builder ()->get_file_symbols ())
23313 ? cu->get_builder ()->get_global_symbols ()
23314 : cu->list_in_scope);
23315
23316 sym->set_aclass_index (LOC_UNRESOLVED);
23317 }
23318 else if (!die_is_declaration (die, cu))
23319 {
23320 /* Use the default LOC_OPTIMIZED_OUT class. */
23321 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
23322 if (!suppress_add)
23323 list_to_add = cu->list_in_scope;
23324 }
23325 }
23326 break;
23327 case DW_TAG_formal_parameter:
23328 {
23329 /* If we are inside a function, mark this as an argument. If
23330 not, we might be looking at an argument to an inlined function
23331 when we do not have enough information to show inlined frames;
23332 pretend it's a local variable in that case so that the user can
23333 still see it. */
23334 struct context_stack *curr
23335 = cu->get_builder ()->get_current_context_stack ();
23336 if (curr != nullptr && curr->name != nullptr)
23337 sym->set_is_argument (1);
23338 attr = dwarf2_attr (die, DW_AT_location, cu);
23339 if (attr != nullptr)
23340 {
23341 var_decode_location (attr, sym, cu);
23342 }
23343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23344 if (attr != nullptr)
23345 {
23346 dwarf2_const_value (attr, sym, cu);
23347 }
23348
23349 list_to_add = cu->list_in_scope;
23350 }
23351 break;
23352 case DW_TAG_unspecified_parameters:
23353 /* From varargs functions; gdb doesn't seem to have any
23354 interest in this information, so just ignore it for now.
23355 (FIXME?) */
23356 break;
23357 case DW_TAG_template_type_param:
23358 suppress_add = 1;
23359 /* Fall through. */
23360 case DW_TAG_class_type:
23361 case DW_TAG_interface_type:
23362 case DW_TAG_structure_type:
23363 case DW_TAG_union_type:
23364 case DW_TAG_set_type:
23365 case DW_TAG_enumeration_type:
23366 case DW_TAG_namelist:
23367 if (die->tag == DW_TAG_namelist)
23368 {
23369 sym->set_aclass_index (LOC_STATIC);
23370 sym->set_domain (VAR_DOMAIN);
23371 }
23372 else
23373 {
23374 sym->set_aclass_index (LOC_TYPEDEF);
23375 sym->set_domain (STRUCT_DOMAIN);
23376 }
23377 {
23378 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
23379 really ever be static objects: otherwise, if you try
23380 to, say, break of a class's method and you're in a file
23381 which doesn't mention that class, it won't work unless
23382 the check for all static symbols in lookup_symbol_aux
23383 saves you. See the OtherFileClass tests in
23384 gdb.c++/namespace.exp. */
23385
23386 if (!suppress_add)
23387 {
23388 buildsym_compunit *builder = cu->get_builder ();
23389 list_to_add
23390 = (cu->list_in_scope == builder->get_file_symbols ()
23391 && cu->per_cu->lang == language_cplus
23392 ? builder->get_global_symbols ()
23393 : cu->list_in_scope);
23394
23395 /* The semantics of C++ state that "struct foo {
23396 ... }" also defines a typedef for "foo". */
23397 if (cu->per_cu->lang == language_cplus
23398 || cu->per_cu->lang == language_ada
23399 || cu->per_cu->lang == language_d
23400 || cu->per_cu->lang == language_rust)
23401 {
23402 /* The symbol's name is already allocated along
23403 with this objfile, so we don't need to
23404 duplicate it for the type. */
23405 if (sym->type ()->name () == 0)
23406 sym->type ()->set_name (sym->search_name ());
23407 }
23408 }
23409 }
23410 break;
23411 case DW_TAG_typedef:
23412 sym->set_aclass_index (LOC_TYPEDEF);
23413 sym->set_domain (VAR_DOMAIN);
23414 list_to_add = cu->list_in_scope;
23415 break;
23416 case DW_TAG_array_type:
23417 case DW_TAG_base_type:
23418 case DW_TAG_subrange_type:
23419 case DW_TAG_generic_subrange:
23420 sym->set_aclass_index (LOC_TYPEDEF);
23421 sym->set_domain (VAR_DOMAIN);
23422 list_to_add = cu->list_in_scope;
23423 break;
23424 case DW_TAG_enumerator:
23425 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23426 if (attr != nullptr)
23427 {
23428 dwarf2_const_value (attr, sym, cu);
23429 }
23430 {
23431 /* NOTE: carlton/2003-11-10: See comment above in the
23432 DW_TAG_class_type, etc. block. */
23433
23434 list_to_add
23435 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
23436 && cu->per_cu->lang == language_cplus
23437 ? cu->get_builder ()->get_global_symbols ()
23438 : cu->list_in_scope);
23439 }
23440 break;
23441 case DW_TAG_imported_declaration:
23442 case DW_TAG_namespace:
23443 sym->set_aclass_index (LOC_TYPEDEF);
23444 list_to_add = cu->get_builder ()->get_global_symbols ();
23445 break;
23446 case DW_TAG_module:
23447 sym->set_aclass_index (LOC_TYPEDEF);
23448 sym->set_domain (MODULE_DOMAIN);
23449 list_to_add = cu->get_builder ()->get_global_symbols ();
23450 break;
23451 case DW_TAG_common_block:
23452 sym->set_aclass_index (LOC_COMMON_BLOCK);
23453 sym->set_domain (COMMON_BLOCK_DOMAIN);
23454 add_symbol_to_list (sym, cu->list_in_scope);
23455 break;
23456 default:
23457 /* Not a tag we recognize. Hopefully we aren't processing
23458 trash data, but since we must specifically ignore things
23459 we don't recognize, there is nothing else we should do at
23460 this point. */
23461 complaint (_("unsupported tag: '%s'"),
23462 dwarf_tag_name (die->tag));
23463 break;
23464 }
23465
23466 if (suppress_add)
23467 {
23468 sym->hash_next = objfile->template_symbols;
23469 objfile->template_symbols = sym;
23470 list_to_add = NULL;
23471 }
23472
23473 if (list_to_add != NULL)
23474 add_symbol_to_list (sym, list_to_add);
23475
23476 /* For the benefit of old versions of GCC, check for anonymous
23477 namespaces based on the demangled name. */
23478 if (!cu->processing_has_namespace_info
23479 && cu->per_cu->lang == language_cplus)
23480 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
23481 }
23482 return (sym);
23483 }
23484
23485 /* Given an attr with a DW_FORM_dataN value in host byte order,
23486 zero-extend it as appropriate for the symbol's type. The DWARF
23487 standard (v4) is not entirely clear about the meaning of using
23488 DW_FORM_dataN for a constant with a signed type, where the type is
23489 wider than the data. The conclusion of a discussion on the DWARF
23490 list was that this is unspecified. We choose to always zero-extend
23491 because that is the interpretation long in use by GCC. */
23492
23493 static gdb_byte *
23494 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
23495 struct dwarf2_cu *cu, LONGEST *value, int bits)
23496 {
23497 struct objfile *objfile = cu->per_objfile->objfile;
23498 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
23499 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
23500 LONGEST l = attr->constant_value (0);
23501
23502 if (bits < sizeof (*value) * 8)
23503 {
23504 l &= ((LONGEST) 1 << bits) - 1;
23505 *value = l;
23506 }
23507 else if (bits == sizeof (*value) * 8)
23508 *value = l;
23509 else
23510 {
23511 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
23512 store_unsigned_integer (bytes, bits / 8, byte_order, l);
23513 return bytes;
23514 }
23515
23516 return NULL;
23517 }
23518
23519 /* Read a constant value from an attribute. Either set *VALUE, or if
23520 the value does not fit in *VALUE, set *BYTES - either already
23521 allocated on the objfile obstack, or newly allocated on OBSTACK,
23522 or, set *BATON, if we translated the constant to a location
23523 expression. */
23524
23525 static void
23526 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
23527 const char *name, struct obstack *obstack,
23528 struct dwarf2_cu *cu,
23529 LONGEST *value, const gdb_byte **bytes,
23530 struct dwarf2_locexpr_baton **baton)
23531 {
23532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23533 struct objfile *objfile = per_objfile->objfile;
23534 struct comp_unit_head *cu_header = &cu->header;
23535 struct dwarf_block *blk;
23536 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
23537 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23538
23539 *value = 0;
23540 *bytes = NULL;
23541 *baton = NULL;
23542
23543 switch (attr->form)
23544 {
23545 case DW_FORM_addr:
23546 case DW_FORM_addrx:
23547 case DW_FORM_GNU_addr_index:
23548 {
23549 gdb_byte *data;
23550
23551 if (TYPE_LENGTH (type) != cu_header->addr_size)
23552 dwarf2_const_value_length_mismatch_complaint (name,
23553 cu_header->addr_size,
23554 TYPE_LENGTH (type));
23555 /* Symbols of this form are reasonably rare, so we just
23556 piggyback on the existing location code rather than writing
23557 a new implementation of symbol_computed_ops. */
23558 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
23559 (*baton)->per_objfile = per_objfile;
23560 (*baton)->per_cu = cu->per_cu;
23561 gdb_assert ((*baton)->per_cu);
23562
23563 (*baton)->size = 2 + cu_header->addr_size;
23564 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
23565 (*baton)->data = data;
23566
23567 data[0] = DW_OP_addr;
23568 store_unsigned_integer (&data[1], cu_header->addr_size,
23569 byte_order, attr->as_address ());
23570 data[cu_header->addr_size + 1] = DW_OP_stack_value;
23571 }
23572 break;
23573 case DW_FORM_string:
23574 case DW_FORM_strp:
23575 case DW_FORM_strx:
23576 case DW_FORM_GNU_str_index:
23577 case DW_FORM_GNU_strp_alt:
23578 /* The string is already allocated on the objfile obstack, point
23579 directly to it. */
23580 *bytes = (const gdb_byte *) attr->as_string ();
23581 break;
23582 case DW_FORM_block1:
23583 case DW_FORM_block2:
23584 case DW_FORM_block4:
23585 case DW_FORM_block:
23586 case DW_FORM_exprloc:
23587 case DW_FORM_data16:
23588 blk = attr->as_block ();
23589 if (TYPE_LENGTH (type) != blk->size)
23590 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
23591 TYPE_LENGTH (type));
23592 *bytes = blk->data;
23593 break;
23594
23595 /* The DW_AT_const_value attributes are supposed to carry the
23596 symbol's value "represented as it would be on the target
23597 architecture." By the time we get here, it's already been
23598 converted to host endianness, so we just need to sign- or
23599 zero-extend it as appropriate. */
23600 case DW_FORM_data1:
23601 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
23602 break;
23603 case DW_FORM_data2:
23604 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
23605 break;
23606 case DW_FORM_data4:
23607 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
23608 break;
23609 case DW_FORM_data8:
23610 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
23611 break;
23612
23613 case DW_FORM_sdata:
23614 case DW_FORM_implicit_const:
23615 *value = attr->as_signed ();
23616 break;
23617
23618 case DW_FORM_udata:
23619 *value = attr->as_unsigned ();
23620 break;
23621
23622 default:
23623 complaint (_("unsupported const value attribute form: '%s'"),
23624 dwarf_form_name (attr->form));
23625 *value = 0;
23626 break;
23627 }
23628 }
23629
23630
23631 /* Copy constant value from an attribute to a symbol. */
23632
23633 static void
23634 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
23635 struct dwarf2_cu *cu)
23636 {
23637 struct objfile *objfile = cu->per_objfile->objfile;
23638 LONGEST value;
23639 const gdb_byte *bytes;
23640 struct dwarf2_locexpr_baton *baton;
23641
23642 dwarf2_const_value_attr (attr, sym->type (),
23643 sym->print_name (),
23644 &objfile->objfile_obstack, cu,
23645 &value, &bytes, &baton);
23646
23647 if (baton != NULL)
23648 {
23649 SYMBOL_LOCATION_BATON (sym) = baton;
23650 sym->set_aclass_index (dwarf2_locexpr_index);
23651 }
23652 else if (bytes != NULL)
23653 {
23654 sym->set_value_bytes (bytes);
23655 sym->set_aclass_index (LOC_CONST_BYTES);
23656 }
23657 else
23658 {
23659 sym->set_value_longest (value);
23660 sym->set_aclass_index (LOC_CONST);
23661 }
23662 }
23663
23664 /* Return the type of the die in question using its DW_AT_type attribute. */
23665
23666 static struct type *
23667 die_type (struct die_info *die, struct dwarf2_cu *cu)
23668 {
23669 struct attribute *type_attr;
23670
23671 type_attr = dwarf2_attr (die, DW_AT_type, cu);
23672 if (!type_attr)
23673 {
23674 struct objfile *objfile = cu->per_objfile->objfile;
23675 /* A missing DW_AT_type represents a void type. */
23676 return objfile_type (objfile)->builtin_void;
23677 }
23678
23679 return lookup_die_type (die, type_attr, cu);
23680 }
23681
23682 /* True iff CU's producer generates GNAT Ada auxiliary information
23683 that allows to find parallel types through that information instead
23684 of having to do expensive parallel lookups by type name. */
23685
23686 static int
23687 need_gnat_info (struct dwarf2_cu *cu)
23688 {
23689 /* Assume that the Ada compiler was GNAT, which always produces
23690 the auxiliary information. */
23691 return (cu->per_cu->lang == language_ada);
23692 }
23693
23694 /* Return the auxiliary type of the die in question using its
23695 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
23696 attribute is not present. */
23697
23698 static struct type *
23699 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
23700 {
23701 struct attribute *type_attr;
23702
23703 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
23704 if (!type_attr)
23705 return NULL;
23706
23707 return lookup_die_type (die, type_attr, cu);
23708 }
23709
23710 /* If DIE has a descriptive_type attribute, then set the TYPE's
23711 descriptive type accordingly. */
23712
23713 static void
23714 set_descriptive_type (struct type *type, struct die_info *die,
23715 struct dwarf2_cu *cu)
23716 {
23717 struct type *descriptive_type = die_descriptive_type (die, cu);
23718
23719 if (descriptive_type)
23720 {
23721 ALLOCATE_GNAT_AUX_TYPE (type);
23722 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
23723 }
23724 }
23725
23726 /* Return the containing type of the die in question using its
23727 DW_AT_containing_type attribute. */
23728
23729 static struct type *
23730 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
23731 {
23732 struct attribute *type_attr;
23733 struct objfile *objfile = cu->per_objfile->objfile;
23734
23735 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
23736 if (!type_attr)
23737 error (_("Dwarf Error: Problem turning containing type into gdb type "
23738 "[in module %s]"), objfile_name (objfile));
23739
23740 return lookup_die_type (die, type_attr, cu);
23741 }
23742
23743 /* Return an error marker type to use for the ill formed type in DIE/CU. */
23744
23745 static struct type *
23746 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
23747 {
23748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23749 struct objfile *objfile = per_objfile->objfile;
23750 char *saved;
23751
23752 std::string message
23753 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
23754 objfile_name (objfile),
23755 sect_offset_str (cu->header.sect_off),
23756 sect_offset_str (die->sect_off));
23757 saved = obstack_strdup (&objfile->objfile_obstack, message);
23758
23759 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
23760 }
23761
23762 /* Look up the type of DIE in CU using its type attribute ATTR.
23763 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
23764 DW_AT_containing_type.
23765 If there is no type substitute an error marker. */
23766
23767 static struct type *
23768 lookup_die_type (struct die_info *die, const struct attribute *attr,
23769 struct dwarf2_cu *cu)
23770 {
23771 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23772 struct objfile *objfile = per_objfile->objfile;
23773 struct type *this_type;
23774
23775 gdb_assert (attr->name == DW_AT_type
23776 || attr->name == DW_AT_GNAT_descriptive_type
23777 || attr->name == DW_AT_containing_type);
23778
23779 /* First see if we have it cached. */
23780
23781 if (attr->form == DW_FORM_GNU_ref_alt)
23782 {
23783 struct dwarf2_per_cu_data *per_cu;
23784 sect_offset sect_off = attr->get_ref_die_offset ();
23785
23786 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
23787 per_objfile->per_bfd);
23788 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
23789 }
23790 else if (attr->form_is_ref ())
23791 {
23792 sect_offset sect_off = attr->get_ref_die_offset ();
23793
23794 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
23795 }
23796 else if (attr->form == DW_FORM_ref_sig8)
23797 {
23798 ULONGEST signature = attr->as_signature ();
23799
23800 return get_signatured_type (die, signature, cu);
23801 }
23802 else
23803 {
23804 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
23805 " at %s [in module %s]"),
23806 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
23807 objfile_name (objfile));
23808 return build_error_marker_type (cu, die);
23809 }
23810
23811 /* If not cached we need to read it in. */
23812
23813 if (this_type == NULL)
23814 {
23815 struct die_info *type_die = NULL;
23816 struct dwarf2_cu *type_cu = cu;
23817
23818 if (attr->form_is_ref ())
23819 type_die = follow_die_ref (die, attr, &type_cu);
23820 if (type_die == NULL)
23821 return build_error_marker_type (cu, die);
23822 /* If we find the type now, it's probably because the type came
23823 from an inter-CU reference and the type's CU got expanded before
23824 ours. */
23825 this_type = read_type_die (type_die, type_cu);
23826 }
23827
23828 /* If we still don't have a type use an error marker. */
23829
23830 if (this_type == NULL)
23831 return build_error_marker_type (cu, die);
23832
23833 return this_type;
23834 }
23835
23836 /* Return the type in DIE, CU.
23837 Returns NULL for invalid types.
23838
23839 This first does a lookup in die_type_hash,
23840 and only reads the die in if necessary.
23841
23842 NOTE: This can be called when reading in partial or full symbols. */
23843
23844 static struct type *
23845 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
23846 {
23847 struct type *this_type;
23848
23849 this_type = get_die_type (die, cu);
23850 if (this_type)
23851 return this_type;
23852
23853 return read_type_die_1 (die, cu);
23854 }
23855
23856 /* Read the type in DIE, CU.
23857 Returns NULL for invalid types. */
23858
23859 static struct type *
23860 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
23861 {
23862 struct type *this_type = NULL;
23863
23864 switch (die->tag)
23865 {
23866 case DW_TAG_class_type:
23867 case DW_TAG_interface_type:
23868 case DW_TAG_structure_type:
23869 case DW_TAG_union_type:
23870 this_type = read_structure_type (die, cu);
23871 break;
23872 case DW_TAG_enumeration_type:
23873 this_type = read_enumeration_type (die, cu);
23874 break;
23875 case DW_TAG_subprogram:
23876 case DW_TAG_subroutine_type:
23877 case DW_TAG_inlined_subroutine:
23878 this_type = read_subroutine_type (die, cu);
23879 break;
23880 case DW_TAG_array_type:
23881 this_type = read_array_type (die, cu);
23882 break;
23883 case DW_TAG_set_type:
23884 this_type = read_set_type (die, cu);
23885 break;
23886 case DW_TAG_pointer_type:
23887 this_type = read_tag_pointer_type (die, cu);
23888 break;
23889 case DW_TAG_ptr_to_member_type:
23890 this_type = read_tag_ptr_to_member_type (die, cu);
23891 break;
23892 case DW_TAG_reference_type:
23893 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23894 break;
23895 case DW_TAG_rvalue_reference_type:
23896 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23897 break;
23898 case DW_TAG_const_type:
23899 this_type = read_tag_const_type (die, cu);
23900 break;
23901 case DW_TAG_volatile_type:
23902 this_type = read_tag_volatile_type (die, cu);
23903 break;
23904 case DW_TAG_restrict_type:
23905 this_type = read_tag_restrict_type (die, cu);
23906 break;
23907 case DW_TAG_string_type:
23908 this_type = read_tag_string_type (die, cu);
23909 break;
23910 case DW_TAG_typedef:
23911 this_type = read_typedef (die, cu);
23912 break;
23913 case DW_TAG_generic_subrange:
23914 case DW_TAG_subrange_type:
23915 this_type = read_subrange_type (die, cu);
23916 break;
23917 case DW_TAG_base_type:
23918 this_type = read_base_type (die, cu);
23919 break;
23920 case DW_TAG_unspecified_type:
23921 this_type = read_unspecified_type (die, cu);
23922 break;
23923 case DW_TAG_namespace:
23924 this_type = read_namespace_type (die, cu);
23925 break;
23926 case DW_TAG_module:
23927 this_type = read_module_type (die, cu);
23928 break;
23929 case DW_TAG_atomic_type:
23930 this_type = read_tag_atomic_type (die, cu);
23931 break;
23932 default:
23933 complaint (_("unexpected tag in read_type_die: '%s'"),
23934 dwarf_tag_name (die->tag));
23935 break;
23936 }
23937
23938 return this_type;
23939 }
23940
23941 /* See if we can figure out if the class lives in a namespace. We do
23942 this by looking for a member function; its demangled name will
23943 contain namespace info, if there is any.
23944 Return the computed name or NULL.
23945 Space for the result is allocated on the objfile's obstack.
23946 This is the full-die version of guess_partial_die_structure_name.
23947 In this case we know DIE has no useful parent. */
23948
23949 static const char *
23950 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23951 {
23952 struct die_info *spec_die;
23953 struct dwarf2_cu *spec_cu;
23954 struct die_info *child;
23955 struct objfile *objfile = cu->per_objfile->objfile;
23956
23957 spec_cu = cu;
23958 spec_die = die_specification (die, &spec_cu);
23959 if (spec_die != NULL)
23960 {
23961 die = spec_die;
23962 cu = spec_cu;
23963 }
23964
23965 for (child = die->child;
23966 child != NULL;
23967 child = child->sibling)
23968 {
23969 if (child->tag == DW_TAG_subprogram)
23970 {
23971 const char *linkage_name = dw2_linkage_name (child, cu);
23972
23973 if (linkage_name != NULL)
23974 {
23975 gdb::unique_xmalloc_ptr<char> actual_name
23976 (cu->language_defn->class_name_from_physname (linkage_name));
23977 const char *name = NULL;
23978
23979 if (actual_name != NULL)
23980 {
23981 const char *die_name = dwarf2_name (die, cu);
23982
23983 if (die_name != NULL
23984 && strcmp (die_name, actual_name.get ()) != 0)
23985 {
23986 /* Strip off the class name from the full name.
23987 We want the prefix. */
23988 int die_name_len = strlen (die_name);
23989 int actual_name_len = strlen (actual_name.get ());
23990 const char *ptr = actual_name.get ();
23991
23992 /* Test for '::' as a sanity check. */
23993 if (actual_name_len > die_name_len + 2
23994 && ptr[actual_name_len - die_name_len - 1] == ':')
23995 name = obstack_strndup (
23996 &objfile->per_bfd->storage_obstack,
23997 ptr, actual_name_len - die_name_len - 2);
23998 }
23999 }
24000 return name;
24001 }
24002 }
24003 }
24004
24005 return NULL;
24006 }
24007
24008 /* GCC might emit a nameless typedef that has a linkage name. Determine the
24009 prefix part in such case. See
24010 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
24011
24012 static const char *
24013 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
24014 {
24015 struct attribute *attr;
24016 const char *base;
24017
24018 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
24019 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
24020 return NULL;
24021
24022 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
24023 return NULL;
24024
24025 attr = dw2_linkage_name_attr (die, cu);
24026 const char *attr_name = attr->as_string ();
24027 if (attr == NULL || attr_name == NULL)
24028 return NULL;
24029
24030 /* dwarf2_name had to be already called. */
24031 gdb_assert (attr->canonical_string_p ());
24032
24033 /* Strip the base name, keep any leading namespaces/classes. */
24034 base = strrchr (attr_name, ':');
24035 if (base == NULL || base == attr_name || base[-1] != ':')
24036 return "";
24037
24038 struct objfile *objfile = cu->per_objfile->objfile;
24039 return obstack_strndup (&objfile->per_bfd->storage_obstack,
24040 attr_name,
24041 &base[-1] - attr_name);
24042 }
24043
24044 /* Return the name of the namespace/class that DIE is defined within,
24045 or "" if we can't tell. The caller should not xfree the result.
24046
24047 For example, if we're within the method foo() in the following
24048 code:
24049
24050 namespace N {
24051 class C {
24052 void foo () {
24053 }
24054 };
24055 }
24056
24057 then determine_prefix on foo's die will return "N::C". */
24058
24059 static const char *
24060 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
24061 {
24062 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24063 struct die_info *parent, *spec_die;
24064 struct dwarf2_cu *spec_cu;
24065 struct type *parent_type;
24066 const char *retval;
24067
24068 if (cu->per_cu->lang != language_cplus
24069 && cu->per_cu->lang != language_fortran
24070 && cu->per_cu->lang != language_d
24071 && cu->per_cu->lang != language_rust)
24072 return "";
24073
24074 retval = anonymous_struct_prefix (die, cu);
24075 if (retval)
24076 return retval;
24077
24078 /* We have to be careful in the presence of DW_AT_specification.
24079 For example, with GCC 3.4, given the code
24080
24081 namespace N {
24082 void foo() {
24083 // Definition of N::foo.
24084 }
24085 }
24086
24087 then we'll have a tree of DIEs like this:
24088
24089 1: DW_TAG_compile_unit
24090 2: DW_TAG_namespace // N
24091 3: DW_TAG_subprogram // declaration of N::foo
24092 4: DW_TAG_subprogram // definition of N::foo
24093 DW_AT_specification // refers to die #3
24094
24095 Thus, when processing die #4, we have to pretend that we're in
24096 the context of its DW_AT_specification, namely the contex of die
24097 #3. */
24098 spec_cu = cu;
24099 spec_die = die_specification (die, &spec_cu);
24100 if (spec_die == NULL)
24101 parent = die->parent;
24102 else
24103 {
24104 parent = spec_die->parent;
24105 cu = spec_cu;
24106 }
24107
24108 if (parent == NULL)
24109 return "";
24110 else if (parent->building_fullname)
24111 {
24112 const char *name;
24113 const char *parent_name;
24114
24115 /* It has been seen on RealView 2.2 built binaries,
24116 DW_TAG_template_type_param types actually _defined_ as
24117 children of the parent class:
24118
24119 enum E {};
24120 template class <class Enum> Class{};
24121 Class<enum E> class_e;
24122
24123 1: DW_TAG_class_type (Class)
24124 2: DW_TAG_enumeration_type (E)
24125 3: DW_TAG_enumerator (enum1:0)
24126 3: DW_TAG_enumerator (enum2:1)
24127 ...
24128 2: DW_TAG_template_type_param
24129 DW_AT_type DW_FORM_ref_udata (E)
24130
24131 Besides being broken debug info, it can put GDB into an
24132 infinite loop. Consider:
24133
24134 When we're building the full name for Class<E>, we'll start
24135 at Class, and go look over its template type parameters,
24136 finding E. We'll then try to build the full name of E, and
24137 reach here. We're now trying to build the full name of E,
24138 and look over the parent DIE for containing scope. In the
24139 broken case, if we followed the parent DIE of E, we'd again
24140 find Class, and once again go look at its template type
24141 arguments, etc., etc. Simply don't consider such parent die
24142 as source-level parent of this die (it can't be, the language
24143 doesn't allow it), and break the loop here. */
24144 name = dwarf2_name (die, cu);
24145 parent_name = dwarf2_name (parent, cu);
24146 complaint (_("template param type '%s' defined within parent '%s'"),
24147 name ? name : "<unknown>",
24148 parent_name ? parent_name : "<unknown>");
24149 return "";
24150 }
24151 else
24152 switch (parent->tag)
24153 {
24154 case DW_TAG_namespace:
24155 parent_type = read_type_die (parent, cu);
24156 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
24157 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
24158 Work around this problem here. */
24159 if (cu->per_cu->lang == language_cplus
24160 && strcmp (parent_type->name (), "::") == 0)
24161 return "";
24162 /* We give a name to even anonymous namespaces. */
24163 return parent_type->name ();
24164 case DW_TAG_class_type:
24165 case DW_TAG_interface_type:
24166 case DW_TAG_structure_type:
24167 case DW_TAG_union_type:
24168 case DW_TAG_module:
24169 parent_type = read_type_die (parent, cu);
24170 if (parent_type->name () != NULL)
24171 return parent_type->name ();
24172 else
24173 /* An anonymous structure is only allowed non-static data
24174 members; no typedefs, no member functions, et cetera.
24175 So it does not need a prefix. */
24176 return "";
24177 case DW_TAG_compile_unit:
24178 case DW_TAG_partial_unit:
24179 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
24180 if (cu->per_cu->lang == language_cplus
24181 && !per_objfile->per_bfd->types.empty ()
24182 && die->child != NULL
24183 && (die->tag == DW_TAG_class_type
24184 || die->tag == DW_TAG_structure_type
24185 || die->tag == DW_TAG_union_type))
24186 {
24187 const char *name = guess_full_die_structure_name (die, cu);
24188 if (name != NULL)
24189 return name;
24190 }
24191 return "";
24192 case DW_TAG_subprogram:
24193 /* Nested subroutines in Fortran get a prefix with the name
24194 of the parent's subroutine. */
24195 if (cu->per_cu->lang == language_fortran)
24196 {
24197 if ((die->tag == DW_TAG_subprogram)
24198 && (dwarf2_name (parent, cu) != NULL))
24199 return dwarf2_name (parent, cu);
24200 }
24201 return "";
24202 case DW_TAG_enumeration_type:
24203 parent_type = read_type_die (parent, cu);
24204 if (parent_type->is_declared_class ())
24205 {
24206 if (parent_type->name () != NULL)
24207 return parent_type->name ();
24208 return "";
24209 }
24210 /* Fall through. */
24211 default:
24212 return determine_prefix (parent, cu);
24213 }
24214 }
24215
24216 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
24217 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
24218 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
24219 an obconcat, otherwise allocate storage for the result. The CU argument is
24220 used to determine the language and hence, the appropriate separator. */
24221
24222 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
24223
24224 static char *
24225 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
24226 int physname, struct dwarf2_cu *cu)
24227 {
24228 const char *lead = "";
24229 const char *sep;
24230
24231 if (suffix == NULL || suffix[0] == '\0'
24232 || prefix == NULL || prefix[0] == '\0')
24233 sep = "";
24234 else if (cu->per_cu->lang == language_d)
24235 {
24236 /* For D, the 'main' function could be defined in any module, but it
24237 should never be prefixed. */
24238 if (strcmp (suffix, "D main") == 0)
24239 {
24240 prefix = "";
24241 sep = "";
24242 }
24243 else
24244 sep = ".";
24245 }
24246 else if (cu->per_cu->lang == language_fortran && physname)
24247 {
24248 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
24249 DW_AT_MIPS_linkage_name is preferred and used instead. */
24250
24251 lead = "__";
24252 sep = "_MOD_";
24253 }
24254 else
24255 sep = "::";
24256
24257 if (prefix == NULL)
24258 prefix = "";
24259 if (suffix == NULL)
24260 suffix = "";
24261
24262 if (obs == NULL)
24263 {
24264 char *retval
24265 = ((char *)
24266 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
24267
24268 strcpy (retval, lead);
24269 strcat (retval, prefix);
24270 strcat (retval, sep);
24271 strcat (retval, suffix);
24272 return retval;
24273 }
24274 else
24275 {
24276 /* We have an obstack. */
24277 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
24278 }
24279 }
24280
24281 /* Get name of a die, return NULL if not found. */
24282
24283 static const char *
24284 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
24285 struct objfile *objfile)
24286 {
24287 if (name && cu->per_cu->lang == language_cplus)
24288 {
24289 gdb::unique_xmalloc_ptr<char> canon_name
24290 = cp_canonicalize_string (name);
24291
24292 if (canon_name != nullptr)
24293 name = objfile->intern (canon_name.get ());
24294 }
24295
24296 return name;
24297 }
24298
24299 /* Get name of a die, return NULL if not found.
24300 Anonymous namespaces are converted to their magic string. */
24301
24302 static const char *
24303 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
24304 {
24305 struct attribute *attr;
24306 struct objfile *objfile = cu->per_objfile->objfile;
24307
24308 attr = dwarf2_attr (die, DW_AT_name, cu);
24309 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
24310 if (attr_name == nullptr
24311 && die->tag != DW_TAG_namespace
24312 && die->tag != DW_TAG_class_type
24313 && die->tag != DW_TAG_interface_type
24314 && die->tag != DW_TAG_structure_type
24315 && die->tag != DW_TAG_namelist
24316 && die->tag != DW_TAG_union_type)
24317 return NULL;
24318
24319 switch (die->tag)
24320 {
24321 case DW_TAG_compile_unit:
24322 case DW_TAG_partial_unit:
24323 /* Compilation units have a DW_AT_name that is a filename, not
24324 a source language identifier. */
24325 case DW_TAG_enumeration_type:
24326 case DW_TAG_enumerator:
24327 /* These tags always have simple identifiers already; no need
24328 to canonicalize them. */
24329 return attr_name;
24330
24331 case DW_TAG_namespace:
24332 if (attr_name != nullptr)
24333 return attr_name;
24334 return CP_ANONYMOUS_NAMESPACE_STR;
24335
24336 case DW_TAG_class_type:
24337 case DW_TAG_interface_type:
24338 case DW_TAG_structure_type:
24339 case DW_TAG_union_type:
24340 case DW_TAG_namelist:
24341 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
24342 structures or unions. These were of the form "._%d" in GCC 4.1,
24343 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
24344 and GCC 4.4. We work around this problem by ignoring these. */
24345 if (attr_name != nullptr
24346 && (startswith (attr_name, "._")
24347 || startswith (attr_name, "<anonymous")))
24348 return NULL;
24349
24350 /* GCC might emit a nameless typedef that has a linkage name. See
24351 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
24352 if (!attr || attr_name == NULL)
24353 {
24354 attr = dw2_linkage_name_attr (die, cu);
24355 attr_name = attr == nullptr ? nullptr : attr->as_string ();
24356 if (attr == NULL || attr_name == NULL)
24357 return NULL;
24358
24359 /* Avoid demangling attr_name the second time on a second
24360 call for the same DIE. */
24361 if (!attr->canonical_string_p ())
24362 {
24363 gdb::unique_xmalloc_ptr<char> demangled
24364 (gdb_demangle (attr_name, DMGL_TYPES));
24365 if (demangled == nullptr)
24366 return nullptr;
24367
24368 attr->set_string_canonical (objfile->intern (demangled.get ()));
24369 attr_name = attr->as_string ();
24370 }
24371
24372 /* Strip any leading namespaces/classes, keep only the
24373 base name. DW_AT_name for named DIEs does not
24374 contain the prefixes. */
24375 const char *base = strrchr (attr_name, ':');
24376 if (base && base > attr_name && base[-1] == ':')
24377 return &base[1];
24378 else
24379 return attr_name;
24380 }
24381 break;
24382
24383 default:
24384 break;
24385 }
24386
24387 if (!attr->canonical_string_p ())
24388 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
24389 objfile));
24390 return attr->as_string ();
24391 }
24392
24393 /* Return the die that this die in an extension of, or NULL if there
24394 is none. *EXT_CU is the CU containing DIE on input, and the CU
24395 containing the return value on output. */
24396
24397 static struct die_info *
24398 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
24399 {
24400 struct attribute *attr;
24401
24402 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
24403 if (attr == NULL)
24404 return NULL;
24405
24406 return follow_die_ref (die, attr, ext_cu);
24407 }
24408
24409 static void
24410 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
24411 {
24412 unsigned int i;
24413
24414 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
24415 indent, "",
24416 dwarf_tag_name (die->tag), die->abbrev,
24417 sect_offset_str (die->sect_off));
24418
24419 if (die->parent != NULL)
24420 gdb_printf (f, "%*s parent at offset: %s\n",
24421 indent, "",
24422 sect_offset_str (die->parent->sect_off));
24423
24424 gdb_printf (f, "%*s has children: %s\n",
24425 indent, "",
24426 dwarf_bool_name (die->child != NULL));
24427
24428 gdb_printf (f, "%*s attributes:\n", indent, "");
24429
24430 for (i = 0; i < die->num_attrs; ++i)
24431 {
24432 gdb_printf (f, "%*s %s (%s) ",
24433 indent, "",
24434 dwarf_attr_name (die->attrs[i].name),
24435 dwarf_form_name (die->attrs[i].form));
24436
24437 switch (die->attrs[i].form)
24438 {
24439 case DW_FORM_addr:
24440 case DW_FORM_addrx:
24441 case DW_FORM_GNU_addr_index:
24442 gdb_printf (f, "address: ");
24443 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
24444 break;
24445 case DW_FORM_block2:
24446 case DW_FORM_block4:
24447 case DW_FORM_block:
24448 case DW_FORM_block1:
24449 gdb_printf (f, "block: size %s",
24450 pulongest (die->attrs[i].as_block ()->size));
24451 break;
24452 case DW_FORM_exprloc:
24453 gdb_printf (f, "expression: size %s",
24454 pulongest (die->attrs[i].as_block ()->size));
24455 break;
24456 case DW_FORM_data16:
24457 gdb_printf (f, "constant of 16 bytes");
24458 break;
24459 case DW_FORM_ref_addr:
24460 gdb_printf (f, "ref address: ");
24461 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
24462 break;
24463 case DW_FORM_GNU_ref_alt:
24464 gdb_printf (f, "alt ref address: ");
24465 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
24466 break;
24467 case DW_FORM_ref1:
24468 case DW_FORM_ref2:
24469 case DW_FORM_ref4:
24470 case DW_FORM_ref8:
24471 case DW_FORM_ref_udata:
24472 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
24473 (long) (die->attrs[i].as_unsigned ()));
24474 break;
24475 case DW_FORM_data1:
24476 case DW_FORM_data2:
24477 case DW_FORM_data4:
24478 case DW_FORM_data8:
24479 case DW_FORM_udata:
24480 gdb_printf (f, "constant: %s",
24481 pulongest (die->attrs[i].as_unsigned ()));
24482 break;
24483 case DW_FORM_sec_offset:
24484 gdb_printf (f, "section offset: %s",
24485 pulongest (die->attrs[i].as_unsigned ()));
24486 break;
24487 case DW_FORM_ref_sig8:
24488 gdb_printf (f, "signature: %s",
24489 hex_string (die->attrs[i].as_signature ()));
24490 break;
24491 case DW_FORM_string:
24492 case DW_FORM_strp:
24493 case DW_FORM_line_strp:
24494 case DW_FORM_strx:
24495 case DW_FORM_GNU_str_index:
24496 case DW_FORM_GNU_strp_alt:
24497 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
24498 die->attrs[i].as_string ()
24499 ? die->attrs[i].as_string () : "",
24500 die->attrs[i].canonical_string_p () ? "is" : "not");
24501 break;
24502 case DW_FORM_flag:
24503 if (die->attrs[i].as_boolean ())
24504 gdb_printf (f, "flag: TRUE");
24505 else
24506 gdb_printf (f, "flag: FALSE");
24507 break;
24508 case DW_FORM_flag_present:
24509 gdb_printf (f, "flag: TRUE");
24510 break;
24511 case DW_FORM_indirect:
24512 /* The reader will have reduced the indirect form to
24513 the "base form" so this form should not occur. */
24514 gdb_printf (f,
24515 "unexpected attribute form: DW_FORM_indirect");
24516 break;
24517 case DW_FORM_sdata:
24518 case DW_FORM_implicit_const:
24519 gdb_printf (f, "constant: %s",
24520 plongest (die->attrs[i].as_signed ()));
24521 break;
24522 default:
24523 gdb_printf (f, "unsupported attribute form: %d.",
24524 die->attrs[i].form);
24525 break;
24526 }
24527 gdb_printf (f, "\n");
24528 }
24529 }
24530
24531 static void
24532 dump_die_for_error (struct die_info *die)
24533 {
24534 dump_die_shallow (gdb_stderr, 0, die);
24535 }
24536
24537 static void
24538 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
24539 {
24540 int indent = level * 4;
24541
24542 gdb_assert (die != NULL);
24543
24544 if (level >= max_level)
24545 return;
24546
24547 dump_die_shallow (f, indent, die);
24548
24549 if (die->child != NULL)
24550 {
24551 gdb_printf (f, "%*s Children:", indent, "");
24552 if (level + 1 < max_level)
24553 {
24554 gdb_printf (f, "\n");
24555 dump_die_1 (f, level + 1, max_level, die->child);
24556 }
24557 else
24558 {
24559 gdb_printf (f,
24560 " [not printed, max nesting level reached]\n");
24561 }
24562 }
24563
24564 if (die->sibling != NULL && level > 0)
24565 {
24566 dump_die_1 (f, level, max_level, die->sibling);
24567 }
24568 }
24569
24570 /* This is called from the pdie macro in gdbinit.in.
24571 It's not static so gcc will keep a copy callable from gdb. */
24572
24573 void
24574 dump_die (struct die_info *die, int max_level)
24575 {
24576 dump_die_1 (gdb_stdlog, 0, max_level, die);
24577 }
24578
24579 static void
24580 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
24581 {
24582 void **slot;
24583
24584 slot = htab_find_slot_with_hash (cu->die_hash, die,
24585 to_underlying (die->sect_off),
24586 INSERT);
24587
24588 *slot = die;
24589 }
24590
24591 /* Follow reference or signature attribute ATTR of SRC_DIE.
24592 On entry *REF_CU is the CU of SRC_DIE.
24593 On exit *REF_CU is the CU of the result. */
24594
24595 static struct die_info *
24596 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
24597 struct dwarf2_cu **ref_cu)
24598 {
24599 struct die_info *die;
24600
24601 if (attr->form_is_ref ())
24602 die = follow_die_ref (src_die, attr, ref_cu);
24603 else if (attr->form == DW_FORM_ref_sig8)
24604 die = follow_die_sig (src_die, attr, ref_cu);
24605 else
24606 {
24607 dump_die_for_error (src_die);
24608 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
24609 objfile_name ((*ref_cu)->per_objfile->objfile));
24610 }
24611
24612 return die;
24613 }
24614
24615 /* Follow reference OFFSET.
24616 On entry *REF_CU is the CU of the source die referencing OFFSET.
24617 On exit *REF_CU is the CU of the result.
24618 Returns NULL if OFFSET is invalid. */
24619
24620 static struct die_info *
24621 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
24622 struct dwarf2_cu **ref_cu)
24623 {
24624 struct die_info temp_die;
24625 struct dwarf2_cu *target_cu, *cu = *ref_cu;
24626 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24627
24628 gdb_assert (cu->per_cu != NULL);
24629
24630 target_cu = cu;
24631
24632 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
24633 "source CU contains target offset: %d",
24634 sect_offset_str (cu->per_cu->sect_off),
24635 sect_offset_str (sect_off),
24636 cu->header.offset_in_cu_p (sect_off));
24637
24638 if (cu->per_cu->is_debug_types)
24639 {
24640 /* .debug_types CUs cannot reference anything outside their CU.
24641 If they need to, they have to reference a signatured type via
24642 DW_FORM_ref_sig8. */
24643 if (!cu->header.offset_in_cu_p (sect_off))
24644 return NULL;
24645 }
24646 else if (offset_in_dwz != cu->per_cu->is_dwz
24647 || !cu->header.offset_in_cu_p (sect_off))
24648 {
24649 struct dwarf2_per_cu_data *per_cu;
24650
24651 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24652 per_objfile->per_bfd);
24653
24654 dwarf_read_debug_printf_v ("target CU offset: %s, "
24655 "target CU DIEs loaded: %d",
24656 sect_offset_str (per_cu->sect_off),
24657 per_objfile->get_cu (per_cu) != nullptr);
24658
24659 /* If necessary, add it to the queue and load its DIEs.
24660
24661 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24662 it doesn't mean they are currently loaded. Since we require them
24663 to be loaded, we must check for ourselves. */
24664 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
24665 || per_objfile->get_cu (per_cu) == nullptr)
24666 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
24667 false, cu->per_cu->lang);
24668
24669 target_cu = per_objfile->get_cu (per_cu);
24670 gdb_assert (target_cu != nullptr);
24671 }
24672 else if (cu->dies == NULL)
24673 {
24674 /* We're loading full DIEs during partial symbol reading. */
24675 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
24676 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
24677 language_minimal);
24678 }
24679
24680 *ref_cu = target_cu;
24681 temp_die.sect_off = sect_off;
24682
24683 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
24684 &temp_die,
24685 to_underlying (sect_off));
24686 }
24687
24688 /* Follow reference attribute ATTR of SRC_DIE.
24689 On entry *REF_CU is the CU of SRC_DIE.
24690 On exit *REF_CU is the CU of the result. */
24691
24692 static struct die_info *
24693 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
24694 struct dwarf2_cu **ref_cu)
24695 {
24696 sect_offset sect_off = attr->get_ref_die_offset ();
24697 struct dwarf2_cu *cu = *ref_cu;
24698 struct die_info *die;
24699
24700 die = follow_die_offset (sect_off,
24701 (attr->form == DW_FORM_GNU_ref_alt
24702 || cu->per_cu->is_dwz),
24703 ref_cu);
24704 if (!die)
24705 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
24706 "at %s [in module %s]"),
24707 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
24708 objfile_name (cu->per_objfile->objfile));
24709
24710 return die;
24711 }
24712
24713 /* See read.h. */
24714
24715 struct dwarf2_locexpr_baton
24716 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
24717 dwarf2_per_cu_data *per_cu,
24718 dwarf2_per_objfile *per_objfile,
24719 gdb::function_view<CORE_ADDR ()> get_frame_pc,
24720 bool resolve_abstract_p)
24721 {
24722 struct die_info *die;
24723 struct attribute *attr;
24724 struct dwarf2_locexpr_baton retval;
24725 struct objfile *objfile = per_objfile->objfile;
24726
24727 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24728 if (cu == nullptr)
24729 cu = load_cu (per_cu, per_objfile, false);
24730
24731 if (cu == nullptr)
24732 {
24733 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24734 Instead just throw an error, not much else we can do. */
24735 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24736 sect_offset_str (sect_off), objfile_name (objfile));
24737 }
24738
24739 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24740 if (!die)
24741 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24742 sect_offset_str (sect_off), objfile_name (objfile));
24743
24744 attr = dwarf2_attr (die, DW_AT_location, cu);
24745 if (!attr && resolve_abstract_p
24746 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
24747 != per_objfile->per_bfd->abstract_to_concrete.end ()))
24748 {
24749 CORE_ADDR pc = get_frame_pc ();
24750 CORE_ADDR baseaddr = objfile->text_section_offset ();
24751 struct gdbarch *gdbarch = objfile->arch ();
24752
24753 for (const auto &cand_off
24754 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
24755 {
24756 struct dwarf2_cu *cand_cu = cu;
24757 struct die_info *cand
24758 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
24759 if (!cand
24760 || !cand->parent
24761 || cand->parent->tag != DW_TAG_subprogram)
24762 continue;
24763
24764 CORE_ADDR pc_low, pc_high;
24765 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
24766 if (pc_low == ((CORE_ADDR) -1))
24767 continue;
24768 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
24769 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
24770 if (!(pc_low <= pc && pc < pc_high))
24771 continue;
24772
24773 die = cand;
24774 attr = dwarf2_attr (die, DW_AT_location, cu);
24775 break;
24776 }
24777 }
24778
24779 if (!attr)
24780 {
24781 /* DWARF: "If there is no such attribute, then there is no effect.".
24782 DATA is ignored if SIZE is 0. */
24783
24784 retval.data = NULL;
24785 retval.size = 0;
24786 }
24787 else if (attr->form_is_section_offset ())
24788 {
24789 struct dwarf2_loclist_baton loclist_baton;
24790 CORE_ADDR pc = get_frame_pc ();
24791 size_t size;
24792
24793 fill_in_loclist_baton (cu, &loclist_baton, attr);
24794
24795 retval.data = dwarf2_find_location_expression (&loclist_baton,
24796 &size, pc);
24797 retval.size = size;
24798 }
24799 else
24800 {
24801 if (!attr->form_is_block ())
24802 error (_("Dwarf Error: DIE at %s referenced in module %s "
24803 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
24804 sect_offset_str (sect_off), objfile_name (objfile));
24805
24806 struct dwarf_block *block = attr->as_block ();
24807 retval.data = block->data;
24808 retval.size = block->size;
24809 }
24810 retval.per_objfile = per_objfile;
24811 retval.per_cu = cu->per_cu;
24812
24813 per_objfile->age_comp_units ();
24814
24815 return retval;
24816 }
24817
24818 /* See read.h. */
24819
24820 struct dwarf2_locexpr_baton
24821 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
24822 dwarf2_per_cu_data *per_cu,
24823 dwarf2_per_objfile *per_objfile,
24824 gdb::function_view<CORE_ADDR ()> get_frame_pc)
24825 {
24826 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
24827
24828 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
24829 get_frame_pc);
24830 }
24831
24832 /* Write a constant of a given type as target-ordered bytes into
24833 OBSTACK. */
24834
24835 static const gdb_byte *
24836 write_constant_as_bytes (struct obstack *obstack,
24837 enum bfd_endian byte_order,
24838 struct type *type,
24839 ULONGEST value,
24840 LONGEST *len)
24841 {
24842 gdb_byte *result;
24843
24844 *len = TYPE_LENGTH (type);
24845 result = (gdb_byte *) obstack_alloc (obstack, *len);
24846 store_unsigned_integer (result, *len, byte_order, value);
24847
24848 return result;
24849 }
24850
24851 /* See read.h. */
24852
24853 const gdb_byte *
24854 dwarf2_fetch_constant_bytes (sect_offset sect_off,
24855 dwarf2_per_cu_data *per_cu,
24856 dwarf2_per_objfile *per_objfile,
24857 obstack *obstack,
24858 LONGEST *len)
24859 {
24860 struct die_info *die;
24861 struct attribute *attr;
24862 const gdb_byte *result = NULL;
24863 struct type *type;
24864 LONGEST value;
24865 enum bfd_endian byte_order;
24866 struct objfile *objfile = per_objfile->objfile;
24867
24868 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24869 if (cu == nullptr)
24870 cu = load_cu (per_cu, per_objfile, false);
24871
24872 if (cu == nullptr)
24873 {
24874 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24875 Instead just throw an error, not much else we can do. */
24876 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24877 sect_offset_str (sect_off), objfile_name (objfile));
24878 }
24879
24880 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24881 if (!die)
24882 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24883 sect_offset_str (sect_off), objfile_name (objfile));
24884
24885 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24886 if (attr == NULL)
24887 return NULL;
24888
24889 byte_order = (bfd_big_endian (objfile->obfd)
24890 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24891
24892 switch (attr->form)
24893 {
24894 case DW_FORM_addr:
24895 case DW_FORM_addrx:
24896 case DW_FORM_GNU_addr_index:
24897 {
24898 gdb_byte *tem;
24899
24900 *len = cu->header.addr_size;
24901 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24902 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24903 result = tem;
24904 }
24905 break;
24906 case DW_FORM_string:
24907 case DW_FORM_strp:
24908 case DW_FORM_strx:
24909 case DW_FORM_GNU_str_index:
24910 case DW_FORM_GNU_strp_alt:
24911 /* The string is already allocated on the objfile obstack, point
24912 directly to it. */
24913 {
24914 const char *attr_name = attr->as_string ();
24915 result = (const gdb_byte *) attr_name;
24916 *len = strlen (attr_name);
24917 }
24918 break;
24919 case DW_FORM_block1:
24920 case DW_FORM_block2:
24921 case DW_FORM_block4:
24922 case DW_FORM_block:
24923 case DW_FORM_exprloc:
24924 case DW_FORM_data16:
24925 {
24926 struct dwarf_block *block = attr->as_block ();
24927 result = block->data;
24928 *len = block->size;
24929 }
24930 break;
24931
24932 /* The DW_AT_const_value attributes are supposed to carry the
24933 symbol's value "represented as it would be on the target
24934 architecture." By the time we get here, it's already been
24935 converted to host endianness, so we just need to sign- or
24936 zero-extend it as appropriate. */
24937 case DW_FORM_data1:
24938 type = die_type (die, cu);
24939 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24940 if (result == NULL)
24941 result = write_constant_as_bytes (obstack, byte_order,
24942 type, value, len);
24943 break;
24944 case DW_FORM_data2:
24945 type = die_type (die, cu);
24946 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24947 if (result == NULL)
24948 result = write_constant_as_bytes (obstack, byte_order,
24949 type, value, len);
24950 break;
24951 case DW_FORM_data4:
24952 type = die_type (die, cu);
24953 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24954 if (result == NULL)
24955 result = write_constant_as_bytes (obstack, byte_order,
24956 type, value, len);
24957 break;
24958 case DW_FORM_data8:
24959 type = die_type (die, cu);
24960 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24961 if (result == NULL)
24962 result = write_constant_as_bytes (obstack, byte_order,
24963 type, value, len);
24964 break;
24965
24966 case DW_FORM_sdata:
24967 case DW_FORM_implicit_const:
24968 type = die_type (die, cu);
24969 result = write_constant_as_bytes (obstack, byte_order,
24970 type, attr->as_signed (), len);
24971 break;
24972
24973 case DW_FORM_udata:
24974 type = die_type (die, cu);
24975 result = write_constant_as_bytes (obstack, byte_order,
24976 type, attr->as_unsigned (), len);
24977 break;
24978
24979 default:
24980 complaint (_("unsupported const value attribute form: '%s'"),
24981 dwarf_form_name (attr->form));
24982 break;
24983 }
24984
24985 return result;
24986 }
24987
24988 /* See read.h. */
24989
24990 struct type *
24991 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24992 dwarf2_per_cu_data *per_cu,
24993 dwarf2_per_objfile *per_objfile,
24994 const char **var_name)
24995 {
24996 struct die_info *die;
24997
24998 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24999 if (cu == nullptr)
25000 cu = load_cu (per_cu, per_objfile, false);
25001
25002 if (cu == nullptr)
25003 return nullptr;
25004
25005 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
25006 if (!die)
25007 return NULL;
25008
25009 if (var_name != nullptr)
25010 *var_name = var_decl_name (die, cu);
25011 return die_type (die, cu);
25012 }
25013
25014 /* See read.h. */
25015
25016 struct type *
25017 dwarf2_get_die_type (cu_offset die_offset,
25018 dwarf2_per_cu_data *per_cu,
25019 dwarf2_per_objfile *per_objfile)
25020 {
25021 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
25022 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
25023 }
25024
25025 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
25026 On entry *REF_CU is the CU of SRC_DIE.
25027 On exit *REF_CU is the CU of the result.
25028 Returns NULL if the referenced DIE isn't found. */
25029
25030 static struct die_info *
25031 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
25032 struct dwarf2_cu **ref_cu)
25033 {
25034 struct die_info temp_die;
25035 struct dwarf2_cu *sig_cu;
25036 struct die_info *die;
25037 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
25038
25039
25040 /* While it might be nice to assert sig_type->type == NULL here,
25041 we can get here for DW_AT_imported_declaration where we need
25042 the DIE not the type. */
25043
25044 /* If necessary, add it to the queue and load its DIEs.
25045
25046 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
25047 it doesn't mean they are currently loaded. Since we require them
25048 to be loaded, we must check for ourselves. */
25049 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
25050 language_minimal)
25051 || per_objfile->get_cu (sig_type) == nullptr)
25052 read_signatured_type (sig_type, per_objfile);
25053
25054 sig_cu = per_objfile->get_cu (sig_type);
25055 gdb_assert (sig_cu != NULL);
25056 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
25057 temp_die.sect_off = sig_type->type_offset_in_section;
25058 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
25059 to_underlying (temp_die.sect_off));
25060 if (die)
25061 {
25062 /* For .gdb_index version 7 keep track of included TUs.
25063 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
25064 if (per_objfile->per_bfd->index_table != NULL
25065 && per_objfile->per_bfd->index_table->version <= 7)
25066 {
25067 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
25068 }
25069
25070 *ref_cu = sig_cu;
25071 return die;
25072 }
25073
25074 return NULL;
25075 }
25076
25077 /* Follow signatured type referenced by ATTR in SRC_DIE.
25078 On entry *REF_CU is the CU of SRC_DIE.
25079 On exit *REF_CU is the CU of the result.
25080 The result is the DIE of the type.
25081 If the referenced type cannot be found an error is thrown. */
25082
25083 static struct die_info *
25084 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
25085 struct dwarf2_cu **ref_cu)
25086 {
25087 ULONGEST signature = attr->as_signature ();
25088 struct signatured_type *sig_type;
25089 struct die_info *die;
25090
25091 gdb_assert (attr->form == DW_FORM_ref_sig8);
25092
25093 sig_type = lookup_signatured_type (*ref_cu, signature);
25094 /* sig_type will be NULL if the signatured type is missing from
25095 the debug info. */
25096 if (sig_type == NULL)
25097 {
25098 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
25099 " from DIE at %s [in module %s]"),
25100 hex_string (signature), sect_offset_str (src_die->sect_off),
25101 objfile_name ((*ref_cu)->per_objfile->objfile));
25102 }
25103
25104 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
25105 if (die == NULL)
25106 {
25107 dump_die_for_error (src_die);
25108 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
25109 " from DIE at %s [in module %s]"),
25110 hex_string (signature), sect_offset_str (src_die->sect_off),
25111 objfile_name ((*ref_cu)->per_objfile->objfile));
25112 }
25113
25114 return die;
25115 }
25116
25117 /* Get the type specified by SIGNATURE referenced in DIE/CU,
25118 reading in and processing the type unit if necessary. */
25119
25120 static struct type *
25121 get_signatured_type (struct die_info *die, ULONGEST signature,
25122 struct dwarf2_cu *cu)
25123 {
25124 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25125 struct signatured_type *sig_type;
25126 struct dwarf2_cu *type_cu;
25127 struct die_info *type_die;
25128 struct type *type;
25129
25130 sig_type = lookup_signatured_type (cu, signature);
25131 /* sig_type will be NULL if the signatured type is missing from
25132 the debug info. */
25133 if (sig_type == NULL)
25134 {
25135 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
25136 " from DIE at %s [in module %s]"),
25137 hex_string (signature), sect_offset_str (die->sect_off),
25138 objfile_name (per_objfile->objfile));
25139 return build_error_marker_type (cu, die);
25140 }
25141
25142 /* If we already know the type we're done. */
25143 type = per_objfile->get_type_for_signatured_type (sig_type);
25144 if (type != nullptr)
25145 return type;
25146
25147 type_cu = cu;
25148 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
25149 if (type_die != NULL)
25150 {
25151 /* N.B. We need to call get_die_type to ensure only one type for this DIE
25152 is created. This is important, for example, because for c++ classes
25153 we need TYPE_NAME set which is only done by new_symbol. Blech. */
25154 type = read_type_die (type_die, type_cu);
25155 if (type == NULL)
25156 {
25157 complaint (_("Dwarf Error: Cannot build signatured type %s"
25158 " referenced from DIE at %s [in module %s]"),
25159 hex_string (signature), sect_offset_str (die->sect_off),
25160 objfile_name (per_objfile->objfile));
25161 type = build_error_marker_type (cu, die);
25162 }
25163 }
25164 else
25165 {
25166 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
25167 " from DIE at %s [in module %s]"),
25168 hex_string (signature), sect_offset_str (die->sect_off),
25169 objfile_name (per_objfile->objfile));
25170 type = build_error_marker_type (cu, die);
25171 }
25172
25173 per_objfile->set_type_for_signatured_type (sig_type, type);
25174
25175 return type;
25176 }
25177
25178 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
25179 reading in and processing the type unit if necessary. */
25180
25181 static struct type *
25182 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
25183 struct dwarf2_cu *cu) /* ARI: editCase function */
25184 {
25185 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
25186 if (attr->form_is_ref ())
25187 {
25188 struct dwarf2_cu *type_cu = cu;
25189 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
25190
25191 return read_type_die (type_die, type_cu);
25192 }
25193 else if (attr->form == DW_FORM_ref_sig8)
25194 {
25195 return get_signatured_type (die, attr->as_signature (), cu);
25196 }
25197 else
25198 {
25199 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25200
25201 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
25202 " at %s [in module %s]"),
25203 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
25204 objfile_name (per_objfile->objfile));
25205 return build_error_marker_type (cu, die);
25206 }
25207 }
25208
25209 /* Load the DIEs associated with type unit PER_CU into memory. */
25210
25211 static void
25212 load_full_type_unit (dwarf2_per_cu_data *per_cu,
25213 dwarf2_per_objfile *per_objfile)
25214 {
25215 struct signatured_type *sig_type;
25216
25217 /* Caller is responsible for ensuring type_unit_groups don't get here. */
25218 gdb_assert (! per_cu->type_unit_group_p ());
25219
25220 /* We have the per_cu, but we need the signatured_type.
25221 Fortunately this is an easy translation. */
25222 gdb_assert (per_cu->is_debug_types);
25223 sig_type = (struct signatured_type *) per_cu;
25224
25225 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
25226
25227 read_signatured_type (sig_type, per_objfile);
25228
25229 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
25230 }
25231
25232 /* Read in a signatured type and build its CU and DIEs.
25233 If the type is a stub for the real type in a DWO file,
25234 read in the real type from the DWO file as well. */
25235
25236 static void
25237 read_signatured_type (signatured_type *sig_type,
25238 dwarf2_per_objfile *per_objfile)
25239 {
25240 gdb_assert (sig_type->is_debug_types);
25241 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
25242
25243 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
25244
25245 if (!reader.dummy_p)
25246 {
25247 struct dwarf2_cu *cu = reader.cu;
25248 const gdb_byte *info_ptr = reader.info_ptr;
25249
25250 gdb_assert (cu->die_hash == NULL);
25251 cu->die_hash =
25252 htab_create_alloc_ex (cu->header.length / 12,
25253 die_hash,
25254 die_eq,
25255 NULL,
25256 &cu->comp_unit_obstack,
25257 hashtab_obstack_allocate,
25258 dummy_obstack_deallocate);
25259
25260 if (reader.comp_unit_die->has_children)
25261 reader.comp_unit_die->child
25262 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
25263 reader.comp_unit_die);
25264 cu->dies = reader.comp_unit_die;
25265 /* comp_unit_die is not stored in die_hash, no need. */
25266
25267 /* We try not to read any attributes in this function, because
25268 not all CUs needed for references have been loaded yet, and
25269 symbol table processing isn't initialized. But we have to
25270 set the CU language, or we won't be able to build types
25271 correctly. Similarly, if we do not read the producer, we can
25272 not apply producer-specific interpretation. */
25273 prepare_one_comp_unit (cu, cu->dies, language_minimal);
25274
25275 reader.keep ();
25276 }
25277
25278 sig_type->tu_read = 1;
25279 }
25280
25281 /* Decode simple location descriptions.
25282 Given a pointer to a dwarf block that defines a location, compute
25283 the location and return the value. If COMPUTED is non-null, it is
25284 set to true to indicate that decoding was successful, and false
25285 otherwise. If COMPUTED is null, then this function may emit a
25286 complaint. */
25287
25288 static CORE_ADDR
25289 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
25290 {
25291 struct objfile *objfile = cu->per_objfile->objfile;
25292 size_t i;
25293 size_t size = blk->size;
25294 const gdb_byte *data = blk->data;
25295 CORE_ADDR stack[64];
25296 int stacki;
25297 unsigned int bytes_read, unsnd;
25298 gdb_byte op;
25299
25300 if (computed != nullptr)
25301 *computed = false;
25302
25303 i = 0;
25304 stacki = 0;
25305 stack[stacki] = 0;
25306 stack[++stacki] = 0;
25307
25308 while (i < size)
25309 {
25310 op = data[i++];
25311 switch (op)
25312 {
25313 case DW_OP_lit0:
25314 case DW_OP_lit1:
25315 case DW_OP_lit2:
25316 case DW_OP_lit3:
25317 case DW_OP_lit4:
25318 case DW_OP_lit5:
25319 case DW_OP_lit6:
25320 case DW_OP_lit7:
25321 case DW_OP_lit8:
25322 case DW_OP_lit9:
25323 case DW_OP_lit10:
25324 case DW_OP_lit11:
25325 case DW_OP_lit12:
25326 case DW_OP_lit13:
25327 case DW_OP_lit14:
25328 case DW_OP_lit15:
25329 case DW_OP_lit16:
25330 case DW_OP_lit17:
25331 case DW_OP_lit18:
25332 case DW_OP_lit19:
25333 case DW_OP_lit20:
25334 case DW_OP_lit21:
25335 case DW_OP_lit22:
25336 case DW_OP_lit23:
25337 case DW_OP_lit24:
25338 case DW_OP_lit25:
25339 case DW_OP_lit26:
25340 case DW_OP_lit27:
25341 case DW_OP_lit28:
25342 case DW_OP_lit29:
25343 case DW_OP_lit30:
25344 case DW_OP_lit31:
25345 stack[++stacki] = op - DW_OP_lit0;
25346 break;
25347
25348 case DW_OP_reg0:
25349 case DW_OP_reg1:
25350 case DW_OP_reg2:
25351 case DW_OP_reg3:
25352 case DW_OP_reg4:
25353 case DW_OP_reg5:
25354 case DW_OP_reg6:
25355 case DW_OP_reg7:
25356 case DW_OP_reg8:
25357 case DW_OP_reg9:
25358 case DW_OP_reg10:
25359 case DW_OP_reg11:
25360 case DW_OP_reg12:
25361 case DW_OP_reg13:
25362 case DW_OP_reg14:
25363 case DW_OP_reg15:
25364 case DW_OP_reg16:
25365 case DW_OP_reg17:
25366 case DW_OP_reg18:
25367 case DW_OP_reg19:
25368 case DW_OP_reg20:
25369 case DW_OP_reg21:
25370 case DW_OP_reg22:
25371 case DW_OP_reg23:
25372 case DW_OP_reg24:
25373 case DW_OP_reg25:
25374 case DW_OP_reg26:
25375 case DW_OP_reg27:
25376 case DW_OP_reg28:
25377 case DW_OP_reg29:
25378 case DW_OP_reg30:
25379 case DW_OP_reg31:
25380 stack[++stacki] = op - DW_OP_reg0;
25381 if (i < size)
25382 {
25383 if (computed == nullptr)
25384 dwarf2_complex_location_expr_complaint ();
25385 else
25386 return 0;
25387 }
25388 break;
25389
25390 case DW_OP_regx:
25391 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
25392 i += bytes_read;
25393 stack[++stacki] = unsnd;
25394 if (i < size)
25395 {
25396 if (computed == nullptr)
25397 dwarf2_complex_location_expr_complaint ();
25398 else
25399 return 0;
25400 }
25401 break;
25402
25403 case DW_OP_addr:
25404 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
25405 &bytes_read);
25406 i += bytes_read;
25407 break;
25408
25409 case DW_OP_const1u:
25410 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
25411 i += 1;
25412 break;
25413
25414 case DW_OP_const1s:
25415 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
25416 i += 1;
25417 break;
25418
25419 case DW_OP_const2u:
25420 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
25421 i += 2;
25422 break;
25423
25424 case DW_OP_const2s:
25425 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
25426 i += 2;
25427 break;
25428
25429 case DW_OP_const4u:
25430 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
25431 i += 4;
25432 break;
25433
25434 case DW_OP_const4s:
25435 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
25436 i += 4;
25437 break;
25438
25439 case DW_OP_const8u:
25440 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
25441 i += 8;
25442 break;
25443
25444 case DW_OP_constu:
25445 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
25446 &bytes_read);
25447 i += bytes_read;
25448 break;
25449
25450 case DW_OP_consts:
25451 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
25452 i += bytes_read;
25453 break;
25454
25455 case DW_OP_dup:
25456 stack[stacki + 1] = stack[stacki];
25457 stacki++;
25458 break;
25459
25460 case DW_OP_plus:
25461 stack[stacki - 1] += stack[stacki];
25462 stacki--;
25463 break;
25464
25465 case DW_OP_plus_uconst:
25466 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
25467 &bytes_read);
25468 i += bytes_read;
25469 break;
25470
25471 case DW_OP_minus:
25472 stack[stacki - 1] -= stack[stacki];
25473 stacki--;
25474 break;
25475
25476 case DW_OP_deref:
25477 /* If we're not the last op, then we definitely can't encode
25478 this using GDB's address_class enum. This is valid for partial
25479 global symbols, although the variable's address will be bogus
25480 in the psymtab. */
25481 if (i < size)
25482 {
25483 if (computed == nullptr)
25484 dwarf2_complex_location_expr_complaint ();
25485 else
25486 return 0;
25487 }
25488 break;
25489
25490 case DW_OP_GNU_push_tls_address:
25491 case DW_OP_form_tls_address:
25492 /* The top of the stack has the offset from the beginning
25493 of the thread control block at which the variable is located. */
25494 /* Nothing should follow this operator, so the top of stack would
25495 be returned. */
25496 /* This is valid for partial global symbols, but the variable's
25497 address will be bogus in the psymtab. Make it always at least
25498 non-zero to not look as a variable garbage collected by linker
25499 which have DW_OP_addr 0. */
25500 if (i < size)
25501 {
25502 if (computed == nullptr)
25503 dwarf2_complex_location_expr_complaint ();
25504 else
25505 return 0;
25506 }
25507 stack[stacki]++;
25508 break;
25509
25510 case DW_OP_GNU_uninit:
25511 if (computed != nullptr)
25512 return 0;
25513 break;
25514
25515 case DW_OP_addrx:
25516 case DW_OP_GNU_addr_index:
25517 case DW_OP_GNU_const_index:
25518 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
25519 &bytes_read);
25520 i += bytes_read;
25521 break;
25522
25523 default:
25524 if (computed == nullptr)
25525 {
25526 const char *name = get_DW_OP_name (op);
25527
25528 if (name)
25529 complaint (_("unsupported stack op: '%s'"),
25530 name);
25531 else
25532 complaint (_("unsupported stack op: '%02x'"),
25533 op);
25534 }
25535
25536 return (stack[stacki]);
25537 }
25538
25539 /* Enforce maximum stack depth of SIZE-1 to avoid writing
25540 outside of the allocated space. Also enforce minimum>0. */
25541 if (stacki >= ARRAY_SIZE (stack) - 1)
25542 {
25543 if (computed == nullptr)
25544 complaint (_("location description stack overflow"));
25545 return 0;
25546 }
25547
25548 if (stacki <= 0)
25549 {
25550 if (computed == nullptr)
25551 complaint (_("location description stack underflow"));
25552 return 0;
25553 }
25554 }
25555
25556 if (computed != nullptr)
25557 *computed = true;
25558 return (stack[stacki]);
25559 }
25560
25561 /* memory allocation interface */
25562
25563 static struct dwarf_block *
25564 dwarf_alloc_block (struct dwarf2_cu *cu)
25565 {
25566 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
25567 }
25568
25569 static struct die_info *
25570 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
25571 {
25572 struct die_info *die;
25573 size_t size = sizeof (struct die_info);
25574
25575 if (num_attrs > 1)
25576 size += (num_attrs - 1) * sizeof (struct attribute);
25577
25578 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
25579 memset (die, 0, sizeof (struct die_info));
25580 return (die);
25581 }
25582
25583 \f
25584
25585 /* Macro support. */
25586
25587 /* An overload of dwarf_decode_macros that finds the correct section
25588 and ensures it is read in before calling the other overload. */
25589
25590 static void
25591 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25592 int section_is_gnu)
25593 {
25594 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25595 struct objfile *objfile = per_objfile->objfile;
25596 const struct line_header *lh = cu->line_header;
25597 unsigned int offset_size = cu->header.offset_size;
25598 struct dwarf2_section_info *section;
25599 const char *section_name;
25600
25601 if (cu->dwo_unit != nullptr)
25602 {
25603 if (section_is_gnu)
25604 {
25605 section = &cu->dwo_unit->dwo_file->sections.macro;
25606 section_name = ".debug_macro.dwo";
25607 }
25608 else
25609 {
25610 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25611 section_name = ".debug_macinfo.dwo";
25612 }
25613 }
25614 else
25615 {
25616 if (section_is_gnu)
25617 {
25618 section = &per_objfile->per_bfd->macro;
25619 section_name = ".debug_macro";
25620 }
25621 else
25622 {
25623 section = &per_objfile->per_bfd->macinfo;
25624 section_name = ".debug_macinfo";
25625 }
25626 }
25627
25628 section->read (objfile);
25629 if (section->buffer == nullptr)
25630 {
25631 complaint (_("missing %s section"), section_name);
25632 return;
25633 }
25634
25635 buildsym_compunit *builder = cu->get_builder ();
25636
25637 struct dwarf2_section_info *str_offsets_section;
25638 struct dwarf2_section_info *str_section;
25639 gdb::optional<ULONGEST> str_offsets_base;
25640
25641 if (cu->dwo_unit != nullptr)
25642 {
25643 str_offsets_section = &cu->dwo_unit->dwo_file
25644 ->sections.str_offsets;
25645 str_section = &cu->dwo_unit->dwo_file->sections.str;
25646 str_offsets_base = cu->header.addr_size;
25647 }
25648 else
25649 {
25650 str_offsets_section = &per_objfile->per_bfd->str_offsets;
25651 str_section = &per_objfile->per_bfd->str;
25652 str_offsets_base = cu->str_offsets_base;
25653 }
25654
25655 dwarf_decode_macros (per_objfile, builder, section, lh,
25656 offset_size, offset, str_section, str_offsets_section,
25657 str_offsets_base, section_is_gnu);
25658 }
25659
25660 /* Return the .debug_loc section to use for CU.
25661 For DWO files use .debug_loc.dwo. */
25662
25663 static struct dwarf2_section_info *
25664 cu_debug_loc_section (struct dwarf2_cu *cu)
25665 {
25666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25667
25668 if (cu->dwo_unit)
25669 {
25670 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25671
25672 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25673 }
25674 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
25675 : &per_objfile->per_bfd->loc);
25676 }
25677
25678 /* Return the .debug_rnglists section to use for CU. */
25679 static struct dwarf2_section_info *
25680 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
25681 {
25682 if (cu->header.version < 5)
25683 error (_(".debug_rnglists section cannot be used in DWARF %d"),
25684 cu->header.version);
25685 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
25686
25687 /* Make sure we read the .debug_rnglists section from the file that
25688 contains the DW_AT_ranges attribute we are reading. Normally that
25689 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
25690 or DW_TAG_skeleton unit, we always want to read from objfile/linked
25691 program. */
25692 if (cu->dwo_unit != nullptr
25693 && tag != DW_TAG_compile_unit
25694 && tag != DW_TAG_skeleton_unit)
25695 {
25696 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25697
25698 if (sections->rnglists.size > 0)
25699 return &sections->rnglists;
25700 else
25701 error (_(".debug_rnglists section is missing from .dwo file."));
25702 }
25703 return &dwarf2_per_objfile->per_bfd->rnglists;
25704 }
25705
25706 /* A helper function that fills in a dwarf2_loclist_baton. */
25707
25708 static void
25709 fill_in_loclist_baton (struct dwarf2_cu *cu,
25710 struct dwarf2_loclist_baton *baton,
25711 const struct attribute *attr)
25712 {
25713 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25714 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25715
25716 section->read (per_objfile->objfile);
25717
25718 baton->per_objfile = per_objfile;
25719 baton->per_cu = cu->per_cu;
25720 gdb_assert (baton->per_cu);
25721 /* We don't know how long the location list is, but make sure we
25722 don't run off the edge of the section. */
25723 baton->size = section->size - attr->as_unsigned ();
25724 baton->data = section->buffer + attr->as_unsigned ();
25725 if (cu->base_address.has_value ())
25726 baton->base_address = *cu->base_address;
25727 else
25728 baton->base_address = 0;
25729 baton->from_dwo = cu->dwo_unit != NULL;
25730 }
25731
25732 static void
25733 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25734 struct dwarf2_cu *cu, int is_block)
25735 {
25736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25737 struct objfile *objfile = per_objfile->objfile;
25738 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25739
25740 if (attr->form_is_section_offset ()
25741 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25742 the section. If so, fall through to the complaint in the
25743 other branch. */
25744 && attr->as_unsigned () < section->get_size (objfile))
25745 {
25746 struct dwarf2_loclist_baton *baton;
25747
25748 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25749
25750 fill_in_loclist_baton (cu, baton, attr);
25751
25752 if (!cu->base_address.has_value ())
25753 complaint (_("Location list used without "
25754 "specifying the CU base address."));
25755
25756 sym->set_aclass_index ((is_block
25757 ? dwarf2_loclist_block_index
25758 : dwarf2_loclist_index));
25759 SYMBOL_LOCATION_BATON (sym) = baton;
25760 }
25761 else
25762 {
25763 struct dwarf2_locexpr_baton *baton;
25764
25765 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25766 baton->per_objfile = per_objfile;
25767 baton->per_cu = cu->per_cu;
25768 gdb_assert (baton->per_cu);
25769
25770 if (attr->form_is_block ())
25771 {
25772 /* Note that we're just copying the block's data pointer
25773 here, not the actual data. We're still pointing into the
25774 info_buffer for SYM's objfile; right now we never release
25775 that buffer, but when we do clean up properly this may
25776 need to change. */
25777 struct dwarf_block *block = attr->as_block ();
25778 baton->size = block->size;
25779 baton->data = block->data;
25780 }
25781 else
25782 {
25783 dwarf2_invalid_attrib_class_complaint ("location description",
25784 sym->natural_name ());
25785 baton->size = 0;
25786 }
25787
25788 sym->set_aclass_index ((is_block
25789 ? dwarf2_locexpr_block_index
25790 : dwarf2_locexpr_index));
25791 SYMBOL_LOCATION_BATON (sym) = baton;
25792 }
25793 }
25794
25795 /* See read.h. */
25796
25797 const comp_unit_head *
25798 dwarf2_per_cu_data::get_header () const
25799 {
25800 if (!m_header_read_in)
25801 {
25802 const gdb_byte *info_ptr
25803 = this->section->buffer + to_underlying (this->sect_off);
25804
25805 memset (&m_header, 0, sizeof (m_header));
25806
25807 read_comp_unit_head (&m_header, info_ptr, this->section,
25808 rcuh_kind::COMPILE);
25809
25810 m_header_read_in = true;
25811 }
25812
25813 return &m_header;
25814 }
25815
25816 /* See read.h. */
25817
25818 int
25819 dwarf2_per_cu_data::addr_size () const
25820 {
25821 return this->get_header ()->addr_size;
25822 }
25823
25824 /* See read.h. */
25825
25826 int
25827 dwarf2_per_cu_data::offset_size () const
25828 {
25829 return this->get_header ()->offset_size;
25830 }
25831
25832 /* See read.h. */
25833
25834 int
25835 dwarf2_per_cu_data::ref_addr_size () const
25836 {
25837 const comp_unit_head *header = this->get_header ();
25838
25839 if (header->version == 2)
25840 return header->addr_size;
25841 else
25842 return header->offset_size;
25843 }
25844
25845 /* A helper function for dwarf2_find_containing_comp_unit that returns
25846 the index of the result, and that searches a vector. It will
25847 return a result even if the offset in question does not actually
25848 occur in any CU. This is separate so that it can be unit
25849 tested. */
25850
25851 static int
25852 dwarf2_find_containing_comp_unit
25853 (sect_offset sect_off,
25854 unsigned int offset_in_dwz,
25855 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
25856 {
25857 int low, high;
25858
25859 low = 0;
25860 high = all_comp_units.size () - 1;
25861 while (high > low)
25862 {
25863 struct dwarf2_per_cu_data *mid_cu;
25864 int mid = low + (high - low) / 2;
25865
25866 mid_cu = all_comp_units[mid].get ();
25867 if (mid_cu->is_dwz > offset_in_dwz
25868 || (mid_cu->is_dwz == offset_in_dwz
25869 && mid_cu->sect_off + mid_cu->length > sect_off))
25870 high = mid;
25871 else
25872 low = mid + 1;
25873 }
25874 gdb_assert (low == high);
25875 return low;
25876 }
25877
25878 /* Locate the .debug_info compilation unit from CU's objfile which contains
25879 the DIE at OFFSET. Raises an error on failure. */
25880
25881 static struct dwarf2_per_cu_data *
25882 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25883 unsigned int offset_in_dwz,
25884 dwarf2_per_bfd *per_bfd)
25885 {
25886 int low = dwarf2_find_containing_comp_unit
25887 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
25888 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
25889
25890 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25891 {
25892 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25893 error (_("Dwarf Error: could not find partial DIE containing "
25894 "offset %s [in module %s]"),
25895 sect_offset_str (sect_off),
25896 bfd_get_filename (per_bfd->obfd));
25897
25898 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
25899 <= sect_off);
25900 return per_bfd->all_comp_units[low - 1].get ();
25901 }
25902 else
25903 {
25904 if (low == per_bfd->all_comp_units.size () - 1
25905 && sect_off >= this_cu->sect_off + this_cu->length)
25906 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25907 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25908 return this_cu;
25909 }
25910 }
25911
25912 #if GDB_SELF_TEST
25913
25914 namespace selftests {
25915 namespace find_containing_comp_unit {
25916
25917 static void
25918 run_test ()
25919 {
25920 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
25921 dwarf2_per_cu_data *one_ptr = one.get ();
25922 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
25923 dwarf2_per_cu_data *two_ptr = two.get ();
25924 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
25925 dwarf2_per_cu_data *three_ptr = three.get ();
25926 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
25927 dwarf2_per_cu_data *four_ptr = four.get ();
25928
25929 one->length = 5;
25930 two->sect_off = sect_offset (one->length);
25931 two->length = 7;
25932
25933 three->length = 5;
25934 three->is_dwz = 1;
25935 four->sect_off = sect_offset (three->length);
25936 four->length = 7;
25937 four->is_dwz = 1;
25938
25939 std::vector<dwarf2_per_cu_data_up> units;
25940 units.push_back (std::move (one));
25941 units.push_back (std::move (two));
25942 units.push_back (std::move (three));
25943 units.push_back (std::move (four));
25944
25945 int result;
25946
25947 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25948 SELF_CHECK (units[result].get () == one_ptr);
25949 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25950 SELF_CHECK (units[result].get () == one_ptr);
25951 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25952 SELF_CHECK (units[result].get () == two_ptr);
25953
25954 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25955 SELF_CHECK (units[result].get () == three_ptr);
25956 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25957 SELF_CHECK (units[result].get () == three_ptr);
25958 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25959 SELF_CHECK (units[result].get () == four_ptr);
25960 }
25961
25962 }
25963 }
25964
25965 #endif /* GDB_SELF_TEST */
25966
25967 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25968
25969 static void
25970 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25971 enum language pretend_language)
25972 {
25973 struct attribute *attr;
25974
25975 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25976
25977 /* Set the language we're debugging. */
25978 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25979 if (cu->producer != nullptr
25980 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
25981 {
25982 /* The XLCL doesn't generate DW_LANG_OpenCL because this
25983 attribute is not standardised yet. As a workaround for the
25984 language detection we fall back to the DW_AT_producer
25985 string. */
25986 cu->per_cu->lang = language_opencl;
25987 }
25988 else if (cu->producer != nullptr
25989 && strstr (cu->producer, "GNU Go ") != NULL)
25990 {
25991 /* Similar hack for Go. */
25992 cu->per_cu->lang = language_go;
25993 }
25994 else if (attr != nullptr)
25995 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
25996 else
25997 cu->per_cu->lang = pretend_language;
25998 cu->language_defn = language_def (cu->per_cu->lang);
25999 }
26000
26001 /* See read.h. */
26002
26003 dwarf2_cu *
26004 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
26005 {
26006 auto it = m_dwarf2_cus.find (per_cu);
26007 if (it == m_dwarf2_cus.end ())
26008 return nullptr;
26009
26010 return it->second;
26011 }
26012
26013 /* See read.h. */
26014
26015 void
26016 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
26017 {
26018 gdb_assert (this->get_cu (per_cu) == nullptr);
26019
26020 m_dwarf2_cus[per_cu] = cu;
26021 }
26022
26023 /* See read.h. */
26024
26025 void
26026 dwarf2_per_objfile::age_comp_units ()
26027 {
26028 dwarf_read_debug_printf_v ("running");
26029
26030 /* This is not expected to be called in the middle of CU expansion. There is
26031 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
26032 loaded in memory. Calling age_comp_units while the queue is in use could
26033 make us free the DIEs for a CU that is in the queue and therefore break
26034 that invariant. */
26035 gdb_assert (!this->per_bfd->queue.has_value ());
26036
26037 /* Start by clearing all marks. */
26038 for (auto pair : m_dwarf2_cus)
26039 pair.second->clear_mark ();
26040
26041 /* Traverse all CUs, mark them and their dependencies if used recently
26042 enough. */
26043 for (auto pair : m_dwarf2_cus)
26044 {
26045 dwarf2_cu *cu = pair.second;
26046
26047 cu->last_used++;
26048 if (cu->last_used <= dwarf_max_cache_age)
26049 cu->mark ();
26050 }
26051
26052 /* Delete all CUs still not marked. */
26053 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
26054 {
26055 dwarf2_cu *cu = it->second;
26056
26057 if (!cu->is_marked ())
26058 {
26059 dwarf_read_debug_printf_v ("deleting old CU %s",
26060 sect_offset_str (cu->per_cu->sect_off));
26061 delete cu;
26062 it = m_dwarf2_cus.erase (it);
26063 }
26064 else
26065 it++;
26066 }
26067 }
26068
26069 /* See read.h. */
26070
26071 void
26072 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
26073 {
26074 auto it = m_dwarf2_cus.find (per_cu);
26075 if (it == m_dwarf2_cus.end ())
26076 return;
26077
26078 delete it->second;
26079
26080 m_dwarf2_cus.erase (it);
26081 }
26082
26083 dwarf2_per_objfile::~dwarf2_per_objfile ()
26084 {
26085 remove_all_cus ();
26086 }
26087
26088 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
26089 We store these in a hash table separate from the DIEs, and preserve them
26090 when the DIEs are flushed out of cache.
26091
26092 The CU "per_cu" pointer is needed because offset alone is not enough to
26093 uniquely identify the type. A file may have multiple .debug_types sections,
26094 or the type may come from a DWO file. Furthermore, while it's more logical
26095 to use per_cu->section+offset, with Fission the section with the data is in
26096 the DWO file but we don't know that section at the point we need it.
26097 We have to use something in dwarf2_per_cu_data (or the pointer to it)
26098 because we can enter the lookup routine, get_die_type_at_offset, from
26099 outside this file, and thus won't necessarily have PER_CU->cu.
26100 Fortunately, PER_CU is stable for the life of the objfile. */
26101
26102 struct dwarf2_per_cu_offset_and_type
26103 {
26104 const struct dwarf2_per_cu_data *per_cu;
26105 sect_offset sect_off;
26106 struct type *type;
26107 };
26108
26109 /* Hash function for a dwarf2_per_cu_offset_and_type. */
26110
26111 static hashval_t
26112 per_cu_offset_and_type_hash (const void *item)
26113 {
26114 const struct dwarf2_per_cu_offset_and_type *ofs
26115 = (const struct dwarf2_per_cu_offset_and_type *) item;
26116
26117 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
26118 }
26119
26120 /* Equality function for a dwarf2_per_cu_offset_and_type. */
26121
26122 static int
26123 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
26124 {
26125 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
26126 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
26127 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
26128 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
26129
26130 return (ofs_lhs->per_cu == ofs_rhs->per_cu
26131 && ofs_lhs->sect_off == ofs_rhs->sect_off);
26132 }
26133
26134 /* Set the type associated with DIE to TYPE. Save it in CU's hash
26135 table if necessary. For convenience, return TYPE.
26136
26137 The DIEs reading must have careful ordering to:
26138 * Not cause infinite loops trying to read in DIEs as a prerequisite for
26139 reading current DIE.
26140 * Not trying to dereference contents of still incompletely read in types
26141 while reading in other DIEs.
26142 * Enable referencing still incompletely read in types just by a pointer to
26143 the type without accessing its fields.
26144
26145 Therefore caller should follow these rules:
26146 * Try to fetch any prerequisite types we may need to build this DIE type
26147 before building the type and calling set_die_type.
26148 * After building type call set_die_type for current DIE as soon as
26149 possible before fetching more types to complete the current type.
26150 * Make the type as complete as possible before fetching more types. */
26151
26152 static struct type *
26153 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
26154 bool skip_data_location)
26155 {
26156 dwarf2_per_objfile *per_objfile = cu->per_objfile;
26157 struct dwarf2_per_cu_offset_and_type **slot, ofs;
26158 struct objfile *objfile = per_objfile->objfile;
26159 struct attribute *attr;
26160 struct dynamic_prop prop;
26161
26162 /* For Ada types, make sure that the gnat-specific data is always
26163 initialized (if not already set). There are a few types where
26164 we should not be doing so, because the type-specific area is
26165 already used to hold some other piece of info (eg: TYPE_CODE_FLT
26166 where the type-specific area is used to store the floatformat).
26167 But this is not a problem, because the gnat-specific information
26168 is actually not needed for these types. */
26169 if (need_gnat_info (cu)
26170 && type->code () != TYPE_CODE_FUNC
26171 && type->code () != TYPE_CODE_FLT
26172 && type->code () != TYPE_CODE_METHODPTR
26173 && type->code () != TYPE_CODE_MEMBERPTR
26174 && type->code () != TYPE_CODE_METHOD
26175 && type->code () != TYPE_CODE_FIXED_POINT
26176 && !HAVE_GNAT_AUX_INFO (type))
26177 INIT_GNAT_SPECIFIC (type);
26178
26179 /* Read DW_AT_allocated and set in type. */
26180 attr = dwarf2_attr (die, DW_AT_allocated, cu);
26181 if (attr != NULL)
26182 {
26183 struct type *prop_type = cu->addr_sized_int_type (false);
26184 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
26185 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
26186 }
26187
26188 /* Read DW_AT_associated and set in type. */
26189 attr = dwarf2_attr (die, DW_AT_associated, cu);
26190 if (attr != NULL)
26191 {
26192 struct type *prop_type = cu->addr_sized_int_type (false);
26193 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
26194 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
26195 }
26196
26197 /* Read DW_AT_rank and set in type. */
26198 attr = dwarf2_attr (die, DW_AT_rank, cu);
26199 if (attr != NULL)
26200 {
26201 struct type *prop_type = cu->addr_sized_int_type (false);
26202 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
26203 type->add_dyn_prop (DYN_PROP_RANK, prop);
26204 }
26205
26206 /* Read DW_AT_data_location and set in type. */
26207 if (!skip_data_location)
26208 {
26209 attr = dwarf2_attr (die, DW_AT_data_location, cu);
26210 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
26211 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
26212 }
26213
26214 if (per_objfile->die_type_hash == NULL)
26215 per_objfile->die_type_hash
26216 = htab_up (htab_create_alloc (127,
26217 per_cu_offset_and_type_hash,
26218 per_cu_offset_and_type_eq,
26219 NULL, xcalloc, xfree));
26220
26221 ofs.per_cu = cu->per_cu;
26222 ofs.sect_off = die->sect_off;
26223 ofs.type = type;
26224 slot = (struct dwarf2_per_cu_offset_and_type **)
26225 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
26226 if (*slot)
26227 complaint (_("A problem internal to GDB: DIE %s has type already set"),
26228 sect_offset_str (die->sect_off));
26229 *slot = XOBNEW (&objfile->objfile_obstack,
26230 struct dwarf2_per_cu_offset_and_type);
26231 **slot = ofs;
26232 return type;
26233 }
26234
26235 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26236 or return NULL if the die does not have a saved type. */
26237
26238 static struct type *
26239 get_die_type_at_offset (sect_offset sect_off,
26240 dwarf2_per_cu_data *per_cu,
26241 dwarf2_per_objfile *per_objfile)
26242 {
26243 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26244
26245 if (per_objfile->die_type_hash == NULL)
26246 return NULL;
26247
26248 ofs.per_cu = per_cu;
26249 ofs.sect_off = sect_off;
26250 slot = ((struct dwarf2_per_cu_offset_and_type *)
26251 htab_find (per_objfile->die_type_hash.get (), &ofs));
26252 if (slot)
26253 return slot->type;
26254 else
26255 return NULL;
26256 }
26257
26258 /* Look up the type for DIE in CU in die_type_hash,
26259 or return NULL if DIE does not have a saved type. */
26260
26261 static struct type *
26262 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26263 {
26264 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
26265 }
26266
26267 /* Trivial hash function for partial_die_info: the hash value of a DIE
26268 is its offset in .debug_info for this objfile. */
26269
26270 static hashval_t
26271 partial_die_hash (const void *item)
26272 {
26273 const struct partial_die_info *part_die
26274 = (const struct partial_die_info *) item;
26275
26276 return to_underlying (part_die->sect_off);
26277 }
26278
26279 /* Trivial comparison function for partial_die_info structures: two DIEs
26280 are equal if they have the same offset. */
26281
26282 static int
26283 partial_die_eq (const void *item_lhs, const void *item_rhs)
26284 {
26285 const struct partial_die_info *part_die_lhs
26286 = (const struct partial_die_info *) item_lhs;
26287 const struct partial_die_info *part_die_rhs
26288 = (const struct partial_die_info *) item_rhs;
26289
26290 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26291 }
26292
26293 struct cmd_list_element *set_dwarf_cmdlist;
26294 struct cmd_list_element *show_dwarf_cmdlist;
26295
26296 static void
26297 show_check_physname (struct ui_file *file, int from_tty,
26298 struct cmd_list_element *c, const char *value)
26299 {
26300 gdb_printf (file,
26301 _("Whether to check \"physname\" is %s.\n"),
26302 value);
26303 }
26304
26305 void _initialize_dwarf2_read ();
26306 void
26307 _initialize_dwarf2_read ()
26308 {
26309 add_setshow_prefix_cmd ("dwarf", class_maintenance,
26310 _("\
26311 Set DWARF specific variables.\n\
26312 Configure DWARF variables such as the cache size."),
26313 _("\
26314 Show DWARF specific variables.\n\
26315 Show DWARF variables such as the cache size."),
26316 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
26317 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
26318
26319 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26320 &dwarf_max_cache_age, _("\
26321 Set the upper bound on the age of cached DWARF compilation units."), _("\
26322 Show the upper bound on the age of cached DWARF compilation units."), _("\
26323 A higher limit means that cached compilation units will be stored\n\
26324 in memory longer, and more total memory will be used. Zero disables\n\
26325 caching, which can slow down startup."),
26326 NULL,
26327 show_dwarf_max_cache_age,
26328 &set_dwarf_cmdlist,
26329 &show_dwarf_cmdlist);
26330
26331 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26332 Set debugging of the DWARF reader."), _("\
26333 Show debugging of the DWARF reader."), _("\
26334 When enabled (non-zero), debugging messages are printed during DWARF\n\
26335 reading and symtab expansion. A value of 1 (one) provides basic\n\
26336 information. A value greater than 1 provides more verbose information."),
26337 NULL,
26338 NULL,
26339 &setdebuglist, &showdebuglist);
26340
26341 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26342 Set debugging of the DWARF DIE reader."), _("\
26343 Show debugging of the DWARF DIE reader."), _("\
26344 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26345 The value is the maximum depth to print."),
26346 NULL,
26347 NULL,
26348 &setdebuglist, &showdebuglist);
26349
26350 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26351 Set debugging of the dwarf line reader."), _("\
26352 Show debugging of the dwarf line reader."), _("\
26353 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26354 A value of 1 (one) provides basic information.\n\
26355 A value greater than 1 provides more verbose information."),
26356 NULL,
26357 NULL,
26358 &setdebuglist, &showdebuglist);
26359
26360 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26361 Set cross-checking of \"physname\" code against demangler."), _("\
26362 Show cross-checking of \"physname\" code against demangler."), _("\
26363 When enabled, GDB's internal \"physname\" code is checked against\n\
26364 the demangler."),
26365 NULL, show_check_physname,
26366 &setdebuglist, &showdebuglist);
26367
26368 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26369 no_class, &use_deprecated_index_sections, _("\
26370 Set whether to use deprecated gdb_index sections."), _("\
26371 Show whether to use deprecated gdb_index sections."), _("\
26372 When enabled, deprecated .gdb_index sections are used anyway.\n\
26373 Normally they are ignored either because of a missing feature or\n\
26374 performance issue.\n\
26375 Warning: This option must be enabled before gdb reads the file."),
26376 NULL,
26377 NULL,
26378 &setlist, &showlist);
26379
26380 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26381 &dwarf2_locexpr_funcs);
26382 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26383 &dwarf2_loclist_funcs);
26384
26385 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26386 &dwarf2_block_frame_base_locexpr_funcs);
26387 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26388 &dwarf2_block_frame_base_loclist_funcs);
26389
26390 #if GDB_SELF_TEST
26391 selftests::register_test ("dw2_expand_symtabs_matching",
26392 selftests::dw2_expand_symtabs_matching::run_test);
26393 selftests::register_test ("dwarf2_find_containing_comp_unit",
26394 selftests::find_containing_comp_unit::run_test);
26395 #endif
26396 }