Fix crash in new DWARF indexer
[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 "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
96
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
103
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
109
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
113
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
116
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
119
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
122
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
128
129 /* These are used to store the dwarf2_per_bfd objects.
130
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
133
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
136 version. */
137 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
139
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
141
142 static int dwarf2_locexpr_index;
143 static int dwarf2_loclist_index;
144 static int dwarf2_locexpr_block_index;
145 static int dwarf2_loclist_block_index;
146
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
152
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
155
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
158
159 /* This is a view into the index that converts from bytes to an
160 offset_type, and allows indexing. Unaligned bytes are specifically
161 allowed here, and handled via unpacking. */
162
163 class offset_view
164 {
165 public:
166 offset_view () = default;
167
168 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
169 : m_bytes (bytes)
170 {
171 }
172
173 /* Extract the INDEXth offset_type from the array. */
174 offset_type operator[] (size_t index) const
175 {
176 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
177 return (offset_type) extract_unsigned_integer (bytes,
178 sizeof (offset_type),
179 BFD_ENDIAN_LITTLE);
180 }
181
182 /* Return the number of offset_types in this array. */
183 size_t size () const
184 {
185 return m_bytes.size () / sizeof (offset_type);
186 }
187
188 /* Return true if this view is empty. */
189 bool empty () const
190 {
191 return m_bytes.empty ();
192 }
193
194 private:
195 /* The underlying bytes. */
196 gdb::array_view<const gdb_byte> m_bytes;
197 };
198
199 /* A description of the mapped index. The file format is described in
200 a comment by the code that writes the index. */
201 struct mapped_index final : public mapped_index_base
202 {
203 /* Index data format version. */
204 int version = 0;
205
206 /* The address table data. */
207 gdb::array_view<const gdb_byte> address_table;
208
209 /* The symbol table, implemented as a hash table. */
210 offset_view symbol_table;
211
212 /* A pointer to the constant pool. */
213 gdb::array_view<const gdb_byte> constant_pool;
214
215 /* Return the index into the constant pool of the name of the IDXth
216 symbol in the symbol table. */
217 offset_type symbol_name_index (offset_type idx) const
218 {
219 return symbol_table[2 * idx];
220 }
221
222 /* Return the index into the constant pool of the CU vector of the
223 IDXth symbol in the symbol table. */
224 offset_type symbol_vec_index (offset_type idx) const
225 {
226 return symbol_table[2 * idx + 1];
227 }
228
229 bool symbol_name_slot_invalid (offset_type idx) const override
230 {
231 return (symbol_name_index (idx) == 0
232 && symbol_vec_index (idx) == 0);
233 }
234
235 /* Convenience method to get at the name of the symbol at IDX in the
236 symbol table. */
237 const char *symbol_name_at
238 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
239 {
240 return (const char *) (this->constant_pool.data ()
241 + symbol_name_index (idx));
242 }
243
244 size_t symbol_name_count () const override
245 { return this->symbol_table.size () / 2; }
246
247 quick_symbol_functions_up make_quick_functions () const override;
248
249 bool version_check () const override
250 {
251 return version >= 8;
252 }
253 };
254
255 /* A description of the mapped .debug_names.
256 Uninitialized map has CU_COUNT 0. */
257 struct mapped_debug_names final : public mapped_index_base
258 {
259 bfd_endian dwarf5_byte_order;
260 bool dwarf5_is_dwarf64;
261 bool augmentation_is_gdb;
262 uint8_t offset_size;
263 uint32_t cu_count = 0;
264 uint32_t tu_count, bucket_count, name_count;
265 const gdb_byte *cu_table_reordered, *tu_table_reordered;
266 const uint32_t *bucket_table_reordered, *hash_table_reordered;
267 const gdb_byte *name_table_string_offs_reordered;
268 const gdb_byte *name_table_entry_offs_reordered;
269 const gdb_byte *entry_pool;
270
271 struct index_val
272 {
273 ULONGEST dwarf_tag;
274 struct attr
275 {
276 /* Attribute name DW_IDX_*. */
277 ULONGEST dw_idx;
278
279 /* Attribute form DW_FORM_*. */
280 ULONGEST form;
281
282 /* Value if FORM is DW_FORM_implicit_const. */
283 LONGEST implicit_const;
284 };
285 std::vector<attr> attr_vec;
286 };
287
288 std::unordered_map<ULONGEST, index_val> abbrev_map;
289
290 const char *namei_to_name
291 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
292
293 /* Implementation of the mapped_index_base virtual interface, for
294 the name_components cache. */
295
296 const char *symbol_name_at
297 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
298 { return namei_to_name (idx, per_objfile); }
299
300 size_t symbol_name_count () const override
301 { return this->name_count; }
302
303 quick_symbol_functions_up make_quick_functions () const override;
304 };
305
306 /* See dwarf2/read.h. */
307
308 dwarf2_per_objfile *
309 get_dwarf2_per_objfile (struct objfile *objfile)
310 {
311 return dwarf2_objfile_data_key.get (objfile);
312 }
313
314 /* Default names of the debugging sections. */
315
316 /* Note that if the debugging section has been compressed, it might
317 have a name like .zdebug_info. */
318
319 const struct dwarf2_debug_sections dwarf2_elf_names =
320 {
321 { ".debug_info", ".zdebug_info" },
322 { ".debug_abbrev", ".zdebug_abbrev" },
323 { ".debug_line", ".zdebug_line" },
324 { ".debug_loc", ".zdebug_loc" },
325 { ".debug_loclists", ".zdebug_loclists" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_str", ".zdebug_str" },
329 { ".debug_str_offsets", ".zdebug_str_offsets" },
330 { ".debug_line_str", ".zdebug_line_str" },
331 { ".debug_ranges", ".zdebug_ranges" },
332 { ".debug_rnglists", ".zdebug_rnglists" },
333 { ".debug_types", ".zdebug_types" },
334 { ".debug_addr", ".zdebug_addr" },
335 { ".debug_frame", ".zdebug_frame" },
336 { ".eh_frame", NULL },
337 { ".gdb_index", ".zgdb_index" },
338 { ".debug_names", ".zdebug_names" },
339 { ".debug_aranges", ".zdebug_aranges" },
340 23
341 };
342
343 /* List of DWO/DWP sections. */
344
345 static const struct dwop_section_names
346 {
347 struct dwarf2_section_names abbrev_dwo;
348 struct dwarf2_section_names info_dwo;
349 struct dwarf2_section_names line_dwo;
350 struct dwarf2_section_names loc_dwo;
351 struct dwarf2_section_names loclists_dwo;
352 struct dwarf2_section_names macinfo_dwo;
353 struct dwarf2_section_names macro_dwo;
354 struct dwarf2_section_names rnglists_dwo;
355 struct dwarf2_section_names str_dwo;
356 struct dwarf2_section_names str_offsets_dwo;
357 struct dwarf2_section_names types_dwo;
358 struct dwarf2_section_names cu_index;
359 struct dwarf2_section_names tu_index;
360 }
361 dwop_section_names =
362 {
363 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
364 { ".debug_info.dwo", ".zdebug_info.dwo" },
365 { ".debug_line.dwo", ".zdebug_line.dwo" },
366 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
367 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
368 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
369 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
370 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
376 };
377
378 /* local data types */
379
380 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
381 begin with a header, which contains the following information. */
382 struct loclists_rnglists_header
383 {
384 /* A 4-byte or 12-byte length containing the length of the
385 set of entries for this compilation unit, not including the
386 length field itself. */
387 unsigned int length;
388
389 /* A 2-byte version identifier. */
390 short version;
391
392 /* A 1-byte unsigned integer containing the size in bytes of an address on
393 the target system. */
394 unsigned char addr_size;
395
396 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
397 on the target system. */
398 unsigned char segment_collector_size;
399
400 /* A 4-byte count of the number of offsets that follow the header. */
401 unsigned int offset_entry_count;
402 };
403
404 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
405 This includes type_unit_group and quick_file_names. */
406
407 struct stmt_list_hash
408 {
409 /* The DWO unit this table is from or NULL if there is none. */
410 struct dwo_unit *dwo_unit;
411
412 /* Offset in .debug_line or .debug_line.dwo. */
413 sect_offset line_sect_off;
414 };
415
416 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
417 an object of this type. This contains elements of type unit groups
418 that can be shared across objfiles. The non-shareable parts are in
419 type_unit_group_unshareable. */
420
421 struct type_unit_group
422 {
423 /* The data used to construct the hash key. */
424 struct stmt_list_hash hash {};
425 };
426
427 /* These sections are what may appear in a (real or virtual) DWO file. */
428
429 struct dwo_sections
430 {
431 struct dwarf2_section_info abbrev;
432 struct dwarf2_section_info line;
433 struct dwarf2_section_info loc;
434 struct dwarf2_section_info loclists;
435 struct dwarf2_section_info macinfo;
436 struct dwarf2_section_info macro;
437 struct dwarf2_section_info rnglists;
438 struct dwarf2_section_info str;
439 struct dwarf2_section_info str_offsets;
440 /* In the case of a virtual DWO file, these two are unused. */
441 struct dwarf2_section_info info;
442 std::vector<dwarf2_section_info> types;
443 };
444
445 /* CUs/TUs in DWP/DWO files. */
446
447 struct dwo_unit
448 {
449 /* Backlink to the containing struct dwo_file. */
450 struct dwo_file *dwo_file;
451
452 /* The "id" that distinguishes this CU/TU.
453 .debug_info calls this "dwo_id", .debug_types calls this "signature".
454 Since signatures came first, we stick with it for consistency. */
455 ULONGEST signature;
456
457 /* The section this CU/TU lives in, in the DWO file. */
458 struct dwarf2_section_info *section;
459
460 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
461 sect_offset sect_off;
462 unsigned int length;
463
464 /* For types, offset in the type's DIE of the type defined by this TU. */
465 cu_offset type_offset_in_tu;
466 };
467
468 /* include/dwarf2.h defines the DWP section codes.
469 It defines a max value but it doesn't define a min value, which we
470 use for error checking, so provide one. */
471
472 enum dwp_v2_section_ids
473 {
474 DW_SECT_MIN = 1
475 };
476
477 /* Data for one DWO file.
478
479 This includes virtual DWO files (a virtual DWO file is a DWO file as it
480 appears in a DWP file). DWP files don't really have DWO files per se -
481 comdat folding of types "loses" the DWO file they came from, and from
482 a high level view DWP files appear to contain a mass of random types.
483 However, to maintain consistency with the non-DWP case we pretend DWP
484 files contain virtual DWO files, and we assign each TU with one virtual
485 DWO file (generally based on the line and abbrev section offsets -
486 a heuristic that seems to work in practice). */
487
488 struct dwo_file
489 {
490 dwo_file () = default;
491 DISABLE_COPY_AND_ASSIGN (dwo_file);
492
493 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
494 For virtual DWO files the name is constructed from the section offsets
495 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
496 from related CU+TUs. */
497 const char *dwo_name = nullptr;
498
499 /* The DW_AT_comp_dir attribute. */
500 const char *comp_dir = nullptr;
501
502 /* The bfd, when the file is open. Otherwise this is NULL.
503 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
504 gdb_bfd_ref_ptr dbfd;
505
506 /* The sections that make up this DWO file.
507 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
508 sections (for lack of a better name). */
509 struct dwo_sections sections {};
510
511 /* The CUs in the file.
512 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
513 an extension to handle LLVM's Link Time Optimization output (where
514 multiple source files may be compiled into a single object/dwo pair). */
515 htab_up cus;
516
517 /* Table of TUs in the file.
518 Each element is a struct dwo_unit. */
519 htab_up tus;
520 };
521
522 /* These sections are what may appear in a DWP file. */
523
524 struct dwp_sections
525 {
526 /* These are used by all DWP versions (1, 2 and 5). */
527 struct dwarf2_section_info str;
528 struct dwarf2_section_info cu_index;
529 struct dwarf2_section_info tu_index;
530
531 /* These are only used by DWP version 2 and version 5 files.
532 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
533 sections are referenced by section number, and are not recorded here.
534 In DWP version 2 or 5 there is at most one copy of all these sections,
535 each section being (effectively) comprised of the concatenation of all of
536 the individual sections that exist in the version 1 format.
537 To keep the code simple we treat each of these concatenated pieces as a
538 section itself (a virtual section?). */
539 struct dwarf2_section_info abbrev;
540 struct dwarf2_section_info info;
541 struct dwarf2_section_info line;
542 struct dwarf2_section_info loc;
543 struct dwarf2_section_info loclists;
544 struct dwarf2_section_info macinfo;
545 struct dwarf2_section_info macro;
546 struct dwarf2_section_info rnglists;
547 struct dwarf2_section_info str_offsets;
548 struct dwarf2_section_info types;
549 };
550
551 /* These sections are what may appear in a virtual DWO file in DWP version 1.
552 A virtual DWO file is a DWO file as it appears in a DWP file. */
553
554 struct virtual_v1_dwo_sections
555 {
556 struct dwarf2_section_info abbrev;
557 struct dwarf2_section_info line;
558 struct dwarf2_section_info loc;
559 struct dwarf2_section_info macinfo;
560 struct dwarf2_section_info macro;
561 struct dwarf2_section_info str_offsets;
562 /* Each DWP hash table entry records one CU or one TU.
563 That is recorded here, and copied to dwo_unit.section. */
564 struct dwarf2_section_info info_or_types;
565 };
566
567 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
568 In version 2, the sections of the DWO files are concatenated together
569 and stored in one section of that name. Thus each ELF section contains
570 several "virtual" sections. */
571
572 struct virtual_v2_or_v5_dwo_sections
573 {
574 bfd_size_type abbrev_offset;
575 bfd_size_type abbrev_size;
576
577 bfd_size_type line_offset;
578 bfd_size_type line_size;
579
580 bfd_size_type loc_offset;
581 bfd_size_type loc_size;
582
583 bfd_size_type loclists_offset;
584 bfd_size_type loclists_size;
585
586 bfd_size_type macinfo_offset;
587 bfd_size_type macinfo_size;
588
589 bfd_size_type macro_offset;
590 bfd_size_type macro_size;
591
592 bfd_size_type rnglists_offset;
593 bfd_size_type rnglists_size;
594
595 bfd_size_type str_offsets_offset;
596 bfd_size_type str_offsets_size;
597
598 /* Each DWP hash table entry records one CU or one TU.
599 That is recorded here, and copied to dwo_unit.section. */
600 bfd_size_type info_or_types_offset;
601 bfd_size_type info_or_types_size;
602 };
603
604 /* Contents of DWP hash tables. */
605
606 struct dwp_hash_table
607 {
608 uint32_t version, nr_columns;
609 uint32_t nr_units, nr_slots;
610 const gdb_byte *hash_table, *unit_table;
611 union
612 {
613 struct
614 {
615 const gdb_byte *indices;
616 } v1;
617 struct
618 {
619 /* This is indexed by column number and gives the id of the section
620 in that column. */
621 #define MAX_NR_V2_DWO_SECTIONS \
622 (1 /* .debug_info or .debug_types */ \
623 + 1 /* .debug_abbrev */ \
624 + 1 /* .debug_line */ \
625 + 1 /* .debug_loc */ \
626 + 1 /* .debug_str_offsets */ \
627 + 1 /* .debug_macro or .debug_macinfo */)
628 int section_ids[MAX_NR_V2_DWO_SECTIONS];
629 const gdb_byte *offsets;
630 const gdb_byte *sizes;
631 } v2;
632 struct
633 {
634 /* This is indexed by column number and gives the id of the section
635 in that column. */
636 #define MAX_NR_V5_DWO_SECTIONS \
637 (1 /* .debug_info */ \
638 + 1 /* .debug_abbrev */ \
639 + 1 /* .debug_line */ \
640 + 1 /* .debug_loclists */ \
641 + 1 /* .debug_str_offsets */ \
642 + 1 /* .debug_macro */ \
643 + 1 /* .debug_rnglists */)
644 int section_ids[MAX_NR_V5_DWO_SECTIONS];
645 const gdb_byte *offsets;
646 const gdb_byte *sizes;
647 } v5;
648 } section_pool;
649 };
650
651 /* Data for one DWP file. */
652
653 struct dwp_file
654 {
655 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
656 : name (name_),
657 dbfd (std::move (abfd))
658 {
659 }
660
661 /* Name of the file. */
662 const char *name;
663
664 /* File format version. */
665 int version = 0;
666
667 /* The bfd. */
668 gdb_bfd_ref_ptr dbfd;
669
670 /* Section info for this file. */
671 struct dwp_sections sections {};
672
673 /* Table of CUs in the file. */
674 const struct dwp_hash_table *cus = nullptr;
675
676 /* Table of TUs in the file. */
677 const struct dwp_hash_table *tus = nullptr;
678
679 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
680 htab_up loaded_cus;
681 htab_up loaded_tus;
682
683 /* Table to map ELF section numbers to their sections.
684 This is only needed for the DWP V1 file format. */
685 unsigned int num_sections = 0;
686 asection **elf_sections = nullptr;
687 };
688
689 /* Struct used to pass misc. parameters to read_die_and_children, et
690 al. which are used for both .debug_info and .debug_types dies.
691 All parameters here are unchanging for the life of the call. This
692 struct exists to abstract away the constant parameters of die reading. */
693
694 struct die_reader_specs
695 {
696 /* The bfd of die_section. */
697 bfd *abfd;
698
699 /* The CU of the DIE we are parsing. */
700 struct dwarf2_cu *cu;
701
702 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
703 struct dwo_file *dwo_file;
704
705 /* The section the die comes from.
706 This is either .debug_info or .debug_types, or the .dwo variants. */
707 struct dwarf2_section_info *die_section;
708
709 /* die_section->buffer. */
710 const gdb_byte *buffer;
711
712 /* The end of the buffer. */
713 const gdb_byte *buffer_end;
714
715 /* The abbreviation table to use when reading the DIEs. */
716 struct abbrev_table *abbrev_table;
717 };
718
719 /* A subclass of die_reader_specs that holds storage and has complex
720 constructor and destructor behavior. */
721
722 class cutu_reader : public die_reader_specs
723 {
724 public:
725
726 cutu_reader (dwarf2_per_cu_data *this_cu,
727 dwarf2_per_objfile *per_objfile,
728 struct abbrev_table *abbrev_table,
729 dwarf2_cu *existing_cu,
730 bool skip_partial,
731 abbrev_cache *cache = nullptr);
732
733 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
734 dwarf2_per_objfile *per_objfile,
735 struct dwarf2_cu *parent_cu = nullptr,
736 struct dwo_file *dwo_file = nullptr);
737
738 DISABLE_COPY_AND_ASSIGN (cutu_reader);
739
740 cutu_reader (cutu_reader &&) = default;
741
742 const gdb_byte *info_ptr = nullptr;
743 struct die_info *comp_unit_die = nullptr;
744 bool dummy_p = false;
745
746 /* Release the new CU, putting it on the chain. This cannot be done
747 for dummy CUs. */
748 void keep ();
749
750 /* Release the abbrev table, transferring ownership to the
751 caller. */
752 abbrev_table_up release_abbrev_table ()
753 {
754 return std::move (m_abbrev_table_holder);
755 }
756
757 private:
758 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
759 dwarf2_per_objfile *per_objfile,
760 dwarf2_cu *existing_cu);
761
762 struct dwarf2_per_cu_data *m_this_cu;
763 std::unique_ptr<dwarf2_cu> m_new_cu;
764
765 /* The ordinary abbreviation table. */
766 abbrev_table_up m_abbrev_table_holder;
767
768 /* The DWO abbreviation table. */
769 abbrev_table_up m_dwo_abbrev_table;
770 };
771
772 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
773 but this would require a corresponding change in unpack_field_as_long
774 and friends. */
775 static int bits_per_byte = 8;
776
777 struct variant_part_builder;
778
779 /* When reading a variant, we track a bit more information about the
780 field, and store it in an object of this type. */
781
782 struct variant_field
783 {
784 int first_field = -1;
785 int last_field = -1;
786
787 /* A variant can contain other variant parts. */
788 std::vector<variant_part_builder> variant_parts;
789
790 /* If we see a DW_TAG_variant, then this will be set if this is the
791 default branch. */
792 bool default_branch = false;
793 /* If we see a DW_AT_discr_value, then this will be the discriminant
794 value. */
795 ULONGEST discriminant_value = 0;
796 /* If we see a DW_AT_discr_list, then this is a pointer to the list
797 data. */
798 struct dwarf_block *discr_list_data = nullptr;
799 };
800
801 /* This represents a DW_TAG_variant_part. */
802
803 struct variant_part_builder
804 {
805 /* The offset of the discriminant field. */
806 sect_offset discriminant_offset {};
807
808 /* Variants that are direct children of this variant part. */
809 std::vector<variant_field> variants;
810
811 /* True if we're currently reading a variant. */
812 bool processing_variant = false;
813 };
814
815 struct nextfield
816 {
817 int accessibility = 0;
818 int virtuality = 0;
819 /* Variant parts need to find the discriminant, which is a DIE
820 reference. We track the section offset of each field to make
821 this link. */
822 sect_offset offset;
823 struct field field {};
824 };
825
826 struct fnfieldlist
827 {
828 const char *name = nullptr;
829 std::vector<struct fn_field> fnfields;
830 };
831
832 /* The routines that read and process dies for a C struct or C++ class
833 pass lists of data member fields and lists of member function fields
834 in an instance of a field_info structure, as defined below. */
835 struct field_info
836 {
837 /* List of data member and baseclasses fields. */
838 std::vector<struct nextfield> fields;
839 std::vector<struct nextfield> baseclasses;
840
841 /* Set if the accessibility of one of the fields is not public. */
842 bool non_public_fields = false;
843
844 /* Member function fieldlist array, contains name of possibly overloaded
845 member function, number of overloaded member functions and a pointer
846 to the head of the member function field chain. */
847 std::vector<struct fnfieldlist> fnfieldlists;
848
849 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
850 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
851 std::vector<struct decl_field> typedef_field_list;
852
853 /* Nested types defined by this class and the number of elements in this
854 list. */
855 std::vector<struct decl_field> nested_types_list;
856
857 /* If non-null, this is the variant part we are currently
858 reading. */
859 variant_part_builder *current_variant_part = nullptr;
860 /* This holds all the top-level variant parts attached to the type
861 we're reading. */
862 std::vector<variant_part_builder> variant_parts;
863
864 /* Return the total number of fields (including baseclasses). */
865 int nfields () const
866 {
867 return fields.size () + baseclasses.size ();
868 }
869 };
870
871 /* Loaded secondary compilation units are kept in memory until they
872 have not been referenced for the processing of this many
873 compilation units. Set this to zero to disable caching. Cache
874 sizes of up to at least twenty will improve startup time for
875 typical inter-CU-reference binaries, at an obvious memory cost. */
876 static int dwarf_max_cache_age = 5;
877 static void
878 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
879 struct cmd_list_element *c, const char *value)
880 {
881 gdb_printf (file, _("The upper bound on the age of cached "
882 "DWARF compilation units is %s.\n"),
883 value);
884 }
885 \f
886 /* local function prototypes */
887
888 static void dwarf2_find_base_address (struct die_info *die,
889 struct dwarf2_cu *cu);
890
891 class cooked_index_storage;
892 static void build_type_psymtabs_reader (cutu_reader *reader,
893 cooked_index_storage *storage);
894
895 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
896
897 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
898
899 static const gdb_byte *read_attribute (const struct die_reader_specs *,
900 struct attribute *,
901 const struct attr_abbrev *,
902 const gdb_byte *);
903
904 static void read_attribute_reprocess (const struct die_reader_specs *reader,
905 struct attribute *attr, dwarf_tag tag);
906
907 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
908
909 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
910 dwarf2_section_info *, sect_offset);
911
912 static const char *read_indirect_string
913 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
914 const struct comp_unit_head *, unsigned int *);
915
916 static const char *read_indirect_string_at_offset
917 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
918
919 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
920 const gdb_byte *,
921 unsigned int *);
922
923 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
924 ULONGEST str_index);
925
926 static const char *read_stub_str_index (struct dwarf2_cu *cu,
927 ULONGEST str_index);
928
929 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
930 struct dwarf2_cu *);
931
932 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
933 struct dwarf2_cu *cu);
934
935 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
936
937 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
938 struct dwarf2_cu *cu);
939
940 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
941
942 static struct die_info *die_specification (struct die_info *die,
943 struct dwarf2_cu **);
944
945 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
946 struct dwarf2_cu *cu);
947
948 static void dwarf_decode_lines (struct line_header *,
949 struct dwarf2_cu *,
950 CORE_ADDR, int decode_mapping);
951
952 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
953 const char *);
954
955 static struct symbol *new_symbol (struct die_info *, struct type *,
956 struct dwarf2_cu *, struct symbol * = NULL);
957
958 static void dwarf2_const_value (const struct attribute *, struct symbol *,
959 struct dwarf2_cu *);
960
961 static void dwarf2_const_value_attr (const struct attribute *attr,
962 struct type *type,
963 const char *name,
964 struct obstack *obstack,
965 struct dwarf2_cu *cu, LONGEST *value,
966 const gdb_byte **bytes,
967 struct dwarf2_locexpr_baton **baton);
968
969 static struct type *read_subrange_index_type (struct die_info *die,
970 struct dwarf2_cu *cu);
971
972 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
973
974 static int need_gnat_info (struct dwarf2_cu *);
975
976 static struct type *die_descriptive_type (struct die_info *,
977 struct dwarf2_cu *);
978
979 static void set_descriptive_type (struct type *, struct die_info *,
980 struct dwarf2_cu *);
981
982 static struct type *die_containing_type (struct die_info *,
983 struct dwarf2_cu *);
984
985 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
986 struct dwarf2_cu *);
987
988 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
989
990 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
991
992 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
993
994 static char *typename_concat (struct obstack *obs, const char *prefix,
995 const char *suffix, int physname,
996 struct dwarf2_cu *cu);
997
998 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
999
1000 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1001
1002 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1003
1004 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1005
1006 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1007
1008 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1009
1010 /* Return the .debug_loclists section to use for cu. */
1011 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1012
1013 /* Return the .debug_rnglists section to use for cu. */
1014 static struct dwarf2_section_info *cu_debug_rnglists_section
1015 (struct dwarf2_cu *cu, dwarf_tag tag);
1016
1017 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1018 values. Keep the items ordered with increasing constraints compliance. */
1019 enum pc_bounds_kind
1020 {
1021 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1022 PC_BOUNDS_NOT_PRESENT,
1023
1024 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1025 were present but they do not form a valid range of PC addresses. */
1026 PC_BOUNDS_INVALID,
1027
1028 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1029 PC_BOUNDS_RANGES,
1030
1031 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1032 PC_BOUNDS_HIGH_LOW,
1033 };
1034
1035 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1036 CORE_ADDR *, CORE_ADDR *,
1037 struct dwarf2_cu *,
1038 addrmap *,
1039 void *);
1040
1041 static void get_scope_pc_bounds (struct die_info *,
1042 CORE_ADDR *, CORE_ADDR *,
1043 struct dwarf2_cu *);
1044
1045 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1046 CORE_ADDR, struct dwarf2_cu *);
1047
1048 static void dwarf2_add_field (struct field_info *, struct die_info *,
1049 struct dwarf2_cu *);
1050
1051 static void dwarf2_attach_fields_to_type (struct field_info *,
1052 struct type *, struct dwarf2_cu *);
1053
1054 static void dwarf2_add_member_fn (struct field_info *,
1055 struct die_info *, struct type *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1059 struct type *,
1060 struct dwarf2_cu *);
1061
1062 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1065
1066 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1067
1068 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1069
1070 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1071
1072 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1073
1074 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1075
1076 static struct type *read_module_type (struct die_info *die,
1077 struct dwarf2_cu *cu);
1078
1079 static const char *namespace_name (struct die_info *die,
1080 int *is_anonymous, struct dwarf2_cu *);
1081
1082 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1083
1084 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1085 bool * = nullptr);
1086
1087 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1088 struct dwarf2_cu *);
1089
1090 static struct die_info *read_die_and_siblings_1
1091 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1092 struct die_info *);
1093
1094 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1095 const gdb_byte *info_ptr,
1096 const gdb_byte **new_info_ptr,
1097 struct die_info *parent);
1098
1099 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1100 struct die_info **, const gdb_byte *,
1101 int);
1102
1103 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1104 struct die_info **, const gdb_byte *);
1105
1106 static void process_die (struct die_info *, struct dwarf2_cu *);
1107
1108 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1109 struct objfile *);
1110
1111 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1112
1113 static const char *dwarf2_full_name (const char *name,
1114 struct die_info *die,
1115 struct dwarf2_cu *cu);
1116
1117 static const char *dwarf2_physname (const char *name, struct die_info *die,
1118 struct dwarf2_cu *cu);
1119
1120 static struct die_info *dwarf2_extension (struct die_info *die,
1121 struct dwarf2_cu **);
1122
1123 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1124
1125 static void dump_die_for_error (struct die_info *);
1126
1127 static void dump_die_1 (struct ui_file *, int level, int max_level,
1128 struct die_info *);
1129
1130 /*static*/ void dump_die (struct die_info *, int max_level);
1131
1132 static void store_in_ref_table (struct die_info *,
1133 struct dwarf2_cu *);
1134
1135 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1136 const struct attribute *,
1137 struct dwarf2_cu **);
1138
1139 static struct die_info *follow_die_ref (struct die_info *,
1140 const struct attribute *,
1141 struct dwarf2_cu **);
1142
1143 static struct die_info *follow_die_sig (struct die_info *,
1144 const struct attribute *,
1145 struct dwarf2_cu **);
1146
1147 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1148 struct dwarf2_cu *);
1149
1150 static struct type *get_DW_AT_signature_type (struct die_info *,
1151 const struct attribute *,
1152 struct dwarf2_cu *);
1153
1154 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1155 dwarf2_per_objfile *per_objfile);
1156
1157 static void read_signatured_type (signatured_type *sig_type,
1158 dwarf2_per_objfile *per_objfile);
1159
1160 static int attr_to_dynamic_prop (const struct attribute *attr,
1161 struct die_info *die, struct dwarf2_cu *cu,
1162 struct dynamic_prop *prop, struct type *type);
1163
1164 /* memory allocation interface */
1165
1166 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1167
1168 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1169
1170 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1171
1172 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1173 struct dwarf2_loclist_baton *baton,
1174 const struct attribute *attr);
1175
1176 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1177 struct symbol *sym,
1178 struct dwarf2_cu *cu,
1179 int is_block);
1180
1181 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1182 const gdb_byte *info_ptr,
1183 const struct abbrev_info *abbrev,
1184 bool do_skip_children = true);
1185
1186 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1187 (sect_offset sect_off, unsigned int offset_in_dwz,
1188 dwarf2_per_bfd *per_bfd);
1189
1190 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1191 struct die_info *comp_unit_die,
1192 enum language pretend_language);
1193
1194 static struct type *set_die_type (struct die_info *, struct type *,
1195 struct dwarf2_cu *, bool = false);
1196
1197 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1198
1199 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1200 dwarf2_per_objfile *per_objfile,
1201 dwarf2_cu *existing_cu,
1202 bool skip_partial,
1203 enum language pretend_language);
1204
1205 static void process_full_comp_unit (dwarf2_cu *cu,
1206 enum language pretend_language);
1207
1208 static void process_full_type_unit (dwarf2_cu *cu,
1209 enum language pretend_language);
1210
1211 static struct type *get_die_type_at_offset (sect_offset,
1212 dwarf2_per_cu_data *per_cu,
1213 dwarf2_per_objfile *per_objfile);
1214
1215 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1216
1217 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1218 dwarf2_per_objfile *per_objfile,
1219 enum language pretend_language);
1220
1221 static void process_queue (dwarf2_per_objfile *per_objfile);
1222
1223 /* Class, the destructor of which frees all allocated queue entries. This
1224 will only have work to do if an error was thrown while processing the
1225 dwarf. If no error was thrown then the queue entries should have all
1226 been processed, and freed, as we went along. */
1227
1228 class dwarf2_queue_guard
1229 {
1230 public:
1231 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1232 : m_per_objfile (per_objfile)
1233 {
1234 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1235
1236 m_per_objfile->per_bfd->queue.emplace ();
1237 }
1238
1239 /* Free any entries remaining on the queue. There should only be
1240 entries left if we hit an error while processing the dwarf. */
1241 ~dwarf2_queue_guard ()
1242 {
1243 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1244
1245 m_per_objfile->per_bfd->queue.reset ();
1246 }
1247
1248 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1249
1250 private:
1251 dwarf2_per_objfile *m_per_objfile;
1252 };
1253
1254 dwarf2_queue_item::~dwarf2_queue_item ()
1255 {
1256 /* Anything still marked queued is likely to be in an
1257 inconsistent state, so discard it. */
1258 if (per_cu->queued)
1259 {
1260 per_objfile->remove_cu (per_cu);
1261 per_cu->queued = 0;
1262 }
1263 }
1264
1265 /* See dwarf2/read.h. */
1266
1267 void
1268 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1269 {
1270 if (data->is_debug_types)
1271 delete static_cast<signatured_type *> (data);
1272 else
1273 delete data;
1274 }
1275
1276 static file_and_directory &find_file_and_directory
1277 (struct die_info *die, struct dwarf2_cu *cu);
1278
1279 static const char *compute_include_file_name
1280 (const struct line_header *lh,
1281 const file_entry &fe,
1282 const file_and_directory &cu_info,
1283 std::string &name_holder);
1284
1285 static htab_up allocate_signatured_type_table ();
1286
1287 static htab_up allocate_dwo_unit_table ();
1288
1289 static struct dwo_unit *lookup_dwo_unit_in_dwp
1290 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1291 const char *comp_dir, ULONGEST signature, int is_debug_types);
1292
1293 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1294
1295 static struct dwo_unit *lookup_dwo_comp_unit
1296 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1297 ULONGEST signature);
1298
1299 static struct dwo_unit *lookup_dwo_type_unit
1300 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1301
1302 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1303
1304 /* A unique pointer to a dwo_file. */
1305
1306 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1307
1308 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1309
1310 static void check_producer (struct dwarf2_cu *cu);
1311 \f
1312 /* Various complaints about symbol reading that don't abort the process. */
1313
1314 static void
1315 dwarf2_debug_line_missing_file_complaint (void)
1316 {
1317 complaint (_(".debug_line section has line data without a file"));
1318 }
1319
1320 static void
1321 dwarf2_debug_line_missing_end_sequence_complaint (void)
1322 {
1323 complaint (_(".debug_line section has line "
1324 "program sequence without an end"));
1325 }
1326
1327 static void
1328 dwarf2_complex_location_expr_complaint (void)
1329 {
1330 complaint (_("location expression too complex"));
1331 }
1332
1333 static void
1334 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1335 int arg3)
1336 {
1337 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1338 arg1, arg2, arg3);
1339 }
1340
1341 static void
1342 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1343 {
1344 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1345 arg1, arg2);
1346 }
1347
1348 /* Hash function for line_header_hash. */
1349
1350 static hashval_t
1351 line_header_hash (const struct line_header *ofs)
1352 {
1353 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1354 }
1355
1356 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1357
1358 static hashval_t
1359 line_header_hash_voidp (const void *item)
1360 {
1361 const struct line_header *ofs = (const struct line_header *) item;
1362
1363 return line_header_hash (ofs);
1364 }
1365
1366 /* Equality function for line_header_hash. */
1367
1368 static int
1369 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1370 {
1371 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1372 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1373
1374 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1375 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1376 }
1377
1378 \f
1379
1380 /* An iterator for all_comp_units that is based on index. This
1381 approach makes it possible to iterate over all_comp_units safely,
1382 when some caller in the loop may add new units. */
1383
1384 class all_comp_units_iterator
1385 {
1386 public:
1387
1388 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1389 : m_per_bfd (per_bfd),
1390 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1391 {
1392 }
1393
1394 all_comp_units_iterator &operator++ ()
1395 {
1396 ++m_index;
1397 return *this;
1398 }
1399
1400 dwarf2_per_cu_data *operator* () const
1401 {
1402 return m_per_bfd->get_cu (m_index);
1403 }
1404
1405 bool operator== (const all_comp_units_iterator &other) const
1406 {
1407 return m_index == other.m_index;
1408 }
1409
1410
1411 bool operator!= (const all_comp_units_iterator &other) const
1412 {
1413 return m_index != other.m_index;
1414 }
1415
1416 private:
1417
1418 dwarf2_per_bfd *m_per_bfd;
1419 size_t m_index;
1420 };
1421
1422 /* A range adapter for the all_comp_units_iterator. */
1423 class all_comp_units_range
1424 {
1425 public:
1426
1427 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1428 : m_per_bfd (per_bfd)
1429 {
1430 }
1431
1432 all_comp_units_iterator begin ()
1433 {
1434 return all_comp_units_iterator (m_per_bfd, true);
1435 }
1436
1437 all_comp_units_iterator end ()
1438 {
1439 return all_comp_units_iterator (m_per_bfd, false);
1440 }
1441
1442 private:
1443
1444 dwarf2_per_bfd *m_per_bfd;
1445 };
1446
1447 /* See declaration. */
1448
1449 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1450 bool can_copy_)
1451 : obfd (obfd),
1452 can_copy (can_copy_)
1453 {
1454 if (names == NULL)
1455 names = &dwarf2_elf_names;
1456
1457 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1458 locate_sections (obfd, sec, *names);
1459 }
1460
1461 dwarf2_per_bfd::~dwarf2_per_bfd ()
1462 {
1463 for (auto &per_cu : all_comp_units)
1464 {
1465 per_cu->imported_symtabs_free ();
1466 per_cu->free_cached_file_names ();
1467 }
1468
1469 /* Everything else should be on this->obstack. */
1470 }
1471
1472 /* See read.h. */
1473
1474 void
1475 dwarf2_per_objfile::remove_all_cus ()
1476 {
1477 gdb_assert (!this->per_bfd->queue.has_value ());
1478
1479 for (auto pair : m_dwarf2_cus)
1480 delete pair.second;
1481
1482 m_dwarf2_cus.clear ();
1483 }
1484
1485 /* A helper class that calls free_cached_comp_units on
1486 destruction. */
1487
1488 class free_cached_comp_units
1489 {
1490 public:
1491
1492 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1493 : m_per_objfile (per_objfile)
1494 {
1495 }
1496
1497 ~free_cached_comp_units ()
1498 {
1499 m_per_objfile->remove_all_cus ();
1500 }
1501
1502 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1503
1504 private:
1505
1506 dwarf2_per_objfile *m_per_objfile;
1507 };
1508
1509 /* See read.h. */
1510
1511 bool
1512 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1513 {
1514 if (per_cu->index < this->m_symtabs.size ())
1515 return this->m_symtabs[per_cu->index] != nullptr;
1516 return false;
1517 }
1518
1519 /* See read.h. */
1520
1521 compunit_symtab *
1522 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1523 {
1524 if (per_cu->index < this->m_symtabs.size ())
1525 return this->m_symtabs[per_cu->index];
1526 return nullptr;
1527 }
1528
1529 /* See read.h. */
1530
1531 void
1532 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1533 compunit_symtab *symtab)
1534 {
1535 if (per_cu->index >= this->m_symtabs.size ())
1536 this->m_symtabs.resize (per_cu->index + 1);
1537 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1538 this->m_symtabs[per_cu->index] = symtab;
1539 }
1540
1541 /* Try to locate the sections we need for DWARF 2 debugging
1542 information and return true if we have enough to do something.
1543 NAMES points to the dwarf2 section names, or is NULL if the standard
1544 ELF names are used. CAN_COPY is true for formats where symbol
1545 interposition is possible and so symbol values must follow copy
1546 relocation rules. */
1547
1548 int
1549 dwarf2_has_info (struct objfile *objfile,
1550 const struct dwarf2_debug_sections *names,
1551 bool can_copy)
1552 {
1553 if (objfile->flags & OBJF_READNEVER)
1554 return 0;
1555
1556 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1557
1558 if (per_objfile == NULL)
1559 {
1560 dwarf2_per_bfd *per_bfd;
1561
1562 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1563 BFD doesn't require relocations.
1564
1565 We don't share with objfiles for which -readnow was requested,
1566 because it would complicate things when loading the same BFD with
1567 -readnow and then without -readnow. */
1568 if (!gdb_bfd_requires_relocations (objfile->obfd)
1569 && (objfile->flags & OBJF_READNOW) == 0)
1570 {
1571 /* See if one has been created for this BFD yet. */
1572 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1573
1574 if (per_bfd == nullptr)
1575 {
1576 /* No, create it now. */
1577 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1578 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1579 }
1580 }
1581 else
1582 {
1583 /* No sharing possible, create one specifically for this objfile. */
1584 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1585 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1586 }
1587
1588 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1589 }
1590
1591 return (!per_objfile->per_bfd->info.is_virtual
1592 && per_objfile->per_bfd->info.s.section != NULL
1593 && !per_objfile->per_bfd->abbrev.is_virtual
1594 && per_objfile->per_bfd->abbrev.s.section != NULL);
1595 }
1596
1597 /* See declaration. */
1598
1599 void
1600 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1601 const dwarf2_debug_sections &names)
1602 {
1603 flagword aflag = bfd_section_flags (sectp);
1604
1605 if ((aflag & SEC_HAS_CONTENTS) == 0)
1606 {
1607 }
1608 else if (elf_section_data (sectp)->this_hdr.sh_size
1609 > bfd_get_file_size (abfd))
1610 {
1611 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1612 warning (_("Discarding section %s which has a section size (%s"
1613 ") larger than the file size [in module %s]"),
1614 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1615 bfd_get_filename (abfd));
1616 }
1617 else if (names.info.matches (sectp->name))
1618 {
1619 this->info.s.section = sectp;
1620 this->info.size = bfd_section_size (sectp);
1621 }
1622 else if (names.abbrev.matches (sectp->name))
1623 {
1624 this->abbrev.s.section = sectp;
1625 this->abbrev.size = bfd_section_size (sectp);
1626 }
1627 else if (names.line.matches (sectp->name))
1628 {
1629 this->line.s.section = sectp;
1630 this->line.size = bfd_section_size (sectp);
1631 }
1632 else if (names.loc.matches (sectp->name))
1633 {
1634 this->loc.s.section = sectp;
1635 this->loc.size = bfd_section_size (sectp);
1636 }
1637 else if (names.loclists.matches (sectp->name))
1638 {
1639 this->loclists.s.section = sectp;
1640 this->loclists.size = bfd_section_size (sectp);
1641 }
1642 else if (names.macinfo.matches (sectp->name))
1643 {
1644 this->macinfo.s.section = sectp;
1645 this->macinfo.size = bfd_section_size (sectp);
1646 }
1647 else if (names.macro.matches (sectp->name))
1648 {
1649 this->macro.s.section = sectp;
1650 this->macro.size = bfd_section_size (sectp);
1651 }
1652 else if (names.str.matches (sectp->name))
1653 {
1654 this->str.s.section = sectp;
1655 this->str.size = bfd_section_size (sectp);
1656 }
1657 else if (names.str_offsets.matches (sectp->name))
1658 {
1659 this->str_offsets.s.section = sectp;
1660 this->str_offsets.size = bfd_section_size (sectp);
1661 }
1662 else if (names.line_str.matches (sectp->name))
1663 {
1664 this->line_str.s.section = sectp;
1665 this->line_str.size = bfd_section_size (sectp);
1666 }
1667 else if (names.addr.matches (sectp->name))
1668 {
1669 this->addr.s.section = sectp;
1670 this->addr.size = bfd_section_size (sectp);
1671 }
1672 else if (names.frame.matches (sectp->name))
1673 {
1674 this->frame.s.section = sectp;
1675 this->frame.size = bfd_section_size (sectp);
1676 }
1677 else if (names.eh_frame.matches (sectp->name))
1678 {
1679 this->eh_frame.s.section = sectp;
1680 this->eh_frame.size = bfd_section_size (sectp);
1681 }
1682 else if (names.ranges.matches (sectp->name))
1683 {
1684 this->ranges.s.section = sectp;
1685 this->ranges.size = bfd_section_size (sectp);
1686 }
1687 else if (names.rnglists.matches (sectp->name))
1688 {
1689 this->rnglists.s.section = sectp;
1690 this->rnglists.size = bfd_section_size (sectp);
1691 }
1692 else if (names.types.matches (sectp->name))
1693 {
1694 struct dwarf2_section_info type_section;
1695
1696 memset (&type_section, 0, sizeof (type_section));
1697 type_section.s.section = sectp;
1698 type_section.size = bfd_section_size (sectp);
1699
1700 this->types.push_back (type_section);
1701 }
1702 else if (names.gdb_index.matches (sectp->name))
1703 {
1704 this->gdb_index.s.section = sectp;
1705 this->gdb_index.size = bfd_section_size (sectp);
1706 }
1707 else if (names.debug_names.matches (sectp->name))
1708 {
1709 this->debug_names.s.section = sectp;
1710 this->debug_names.size = bfd_section_size (sectp);
1711 }
1712 else if (names.debug_aranges.matches (sectp->name))
1713 {
1714 this->debug_aranges.s.section = sectp;
1715 this->debug_aranges.size = bfd_section_size (sectp);
1716 }
1717
1718 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1719 && bfd_section_vma (sectp) == 0)
1720 this->has_section_at_zero = true;
1721 }
1722
1723 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1724 SECTION_NAME. */
1725
1726 void
1727 dwarf2_get_section_info (struct objfile *objfile,
1728 enum dwarf2_section_enum sect,
1729 asection **sectp, const gdb_byte **bufp,
1730 bfd_size_type *sizep)
1731 {
1732 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1733 struct dwarf2_section_info *info;
1734
1735 /* We may see an objfile without any DWARF, in which case we just
1736 return nothing. */
1737 if (per_objfile == NULL)
1738 {
1739 *sectp = NULL;
1740 *bufp = NULL;
1741 *sizep = 0;
1742 return;
1743 }
1744 switch (sect)
1745 {
1746 case DWARF2_DEBUG_FRAME:
1747 info = &per_objfile->per_bfd->frame;
1748 break;
1749 case DWARF2_EH_FRAME:
1750 info = &per_objfile->per_bfd->eh_frame;
1751 break;
1752 default:
1753 gdb_assert_not_reached ("unexpected section");
1754 }
1755
1756 info->read (objfile);
1757
1758 *sectp = info->get_bfd_section ();
1759 *bufp = info->buffer;
1760 *sizep = info->size;
1761 }
1762
1763 /* See dwarf2/read.h. */
1764
1765 void
1766 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1767 {
1768 info.read (objfile);
1769 abbrev.read (objfile);
1770 line.read (objfile);
1771 str.read (objfile);
1772 str_offsets.read (objfile);
1773 line_str.read (objfile);
1774 ranges.read (objfile);
1775 rnglists.read (objfile);
1776 addr.read (objfile);
1777
1778 for (auto &section : types)
1779 section.read (objfile);
1780 }
1781
1782 \f
1783 /* DWARF quick_symbol_functions support. */
1784
1785 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1786 unique line tables, so we maintain a separate table of all .debug_line
1787 derived entries to support the sharing.
1788 All the quick functions need is the list of file names. We discard the
1789 line_header when we're done and don't need to record it here. */
1790 struct quick_file_names
1791 {
1792 /* The data used to construct the hash key. */
1793 struct stmt_list_hash hash;
1794
1795 /* The number of entries in file_names, real_names. */
1796 unsigned int num_file_names;
1797
1798 /* The CU directory, as given by DW_AT_comp_dir. May be
1799 nullptr. */
1800 const char *comp_dir;
1801
1802 /* The file names from the line table, after being run through
1803 file_full_name. */
1804 const char **file_names;
1805
1806 /* The file names from the line table after being run through
1807 gdb_realpath. These are computed lazily. */
1808 const char **real_names;
1809 };
1810
1811 struct dwarf2_base_index_functions : public quick_symbol_functions
1812 {
1813 bool has_symbols (struct objfile *objfile) override;
1814
1815 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1816
1817 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1818
1819 void forget_cached_source_info (struct objfile *objfile) override;
1820
1821 enum language lookup_global_symbol_language (struct objfile *objfile,
1822 const char *name,
1823 domain_enum domain,
1824 bool *symbol_found_p) override
1825 {
1826 *symbol_found_p = false;
1827 return language_unknown;
1828 }
1829
1830 void print_stats (struct objfile *objfile, bool print_bcache) override;
1831
1832 void expand_all_symtabs (struct objfile *objfile) override;
1833
1834 /* A helper function that finds the per-cu object from an "adjusted"
1835 PC -- a PC with the base text offset removed. */
1836 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
1837 CORE_ADDR adjusted_pc);
1838
1839 struct compunit_symtab *find_pc_sect_compunit_symtab
1840 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1841 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
1842 override final;
1843
1844 struct compunit_symtab *find_compunit_symtab_by_address
1845 (struct objfile *objfile, CORE_ADDR address) override
1846 {
1847 return nullptr;
1848 }
1849
1850 void map_symbol_filenames (struct objfile *objfile,
1851 gdb::function_view<symbol_filename_ftype> fun,
1852 bool need_fullname) override;
1853 };
1854
1855 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1856 It's handy in this case to have an empty implementation of the
1857 quick symbol functions, to avoid special cases in the rest of the
1858 code. */
1859
1860 struct readnow_functions : public dwarf2_base_index_functions
1861 {
1862 void dump (struct objfile *objfile) override
1863 {
1864 }
1865
1866 void expand_matching_symbols
1867 (struct objfile *,
1868 const lookup_name_info &lookup_name,
1869 domain_enum domain,
1870 int global,
1871 symbol_compare_ftype *ordered_compare) override
1872 {
1873 }
1874
1875 bool expand_symtabs_matching
1876 (struct objfile *objfile,
1877 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1878 const lookup_name_info *lookup_name,
1879 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1880 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1881 block_search_flags search_flags,
1882 domain_enum domain,
1883 enum search_domain kind) override
1884 {
1885 return true;
1886 }
1887 };
1888
1889 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1890 {
1891 void dump (struct objfile *objfile) override;
1892
1893 void expand_matching_symbols
1894 (struct objfile *,
1895 const lookup_name_info &lookup_name,
1896 domain_enum domain,
1897 int global,
1898 symbol_compare_ftype *ordered_compare) override;
1899
1900 bool expand_symtabs_matching
1901 (struct objfile *objfile,
1902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1903 const lookup_name_info *lookup_name,
1904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1906 block_search_flags search_flags,
1907 domain_enum domain,
1908 enum search_domain kind) override;
1909 };
1910
1911 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1912 {
1913 void dump (struct objfile *objfile) override;
1914
1915 void expand_matching_symbols
1916 (struct objfile *,
1917 const lookup_name_info &lookup_name,
1918 domain_enum domain,
1919 int global,
1920 symbol_compare_ftype *ordered_compare) override;
1921
1922 bool expand_symtabs_matching
1923 (struct objfile *objfile,
1924 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1925 const lookup_name_info *lookup_name,
1926 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1927 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1928 block_search_flags search_flags,
1929 domain_enum domain,
1930 enum search_domain kind) override;
1931 };
1932
1933 quick_symbol_functions_up
1934 mapped_index::make_quick_functions () const
1935 {
1936 return quick_symbol_functions_up (new dwarf2_gdb_index);
1937 }
1938
1939 quick_symbol_functions_up
1940 mapped_debug_names::make_quick_functions () const
1941 {
1942 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1943 }
1944
1945 /* Utility hash function for a stmt_list_hash. */
1946
1947 static hashval_t
1948 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1949 {
1950 hashval_t v = 0;
1951
1952 if (stmt_list_hash->dwo_unit != NULL)
1953 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1954 v += to_underlying (stmt_list_hash->line_sect_off);
1955 return v;
1956 }
1957
1958 /* Utility equality function for a stmt_list_hash. */
1959
1960 static int
1961 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1962 const struct stmt_list_hash *rhs)
1963 {
1964 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1965 return 0;
1966 if (lhs->dwo_unit != NULL
1967 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1968 return 0;
1969
1970 return lhs->line_sect_off == rhs->line_sect_off;
1971 }
1972
1973 /* Hash function for a quick_file_names. */
1974
1975 static hashval_t
1976 hash_file_name_entry (const void *e)
1977 {
1978 const struct quick_file_names *file_data
1979 = (const struct quick_file_names *) e;
1980
1981 return hash_stmt_list_entry (&file_data->hash);
1982 }
1983
1984 /* Equality function for a quick_file_names. */
1985
1986 static int
1987 eq_file_name_entry (const void *a, const void *b)
1988 {
1989 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1990 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1991
1992 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1993 }
1994
1995 /* Create a quick_file_names hash table. */
1996
1997 static htab_up
1998 create_quick_file_names_table (unsigned int nr_initial_entries)
1999 {
2000 return htab_up (htab_create_alloc (nr_initial_entries,
2001 hash_file_name_entry, eq_file_name_entry,
2002 nullptr, xcalloc, xfree));
2003 }
2004
2005 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2006 function is unrelated to symtabs, symtab would have to be created afterwards.
2007 You should call age_cached_comp_units after processing the CU. */
2008
2009 static dwarf2_cu *
2010 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2011 bool skip_partial)
2012 {
2013 if (per_cu->is_debug_types)
2014 load_full_type_unit (per_cu, per_objfile);
2015 else
2016 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2017 skip_partial, language_minimal);
2018
2019 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2020 if (cu == nullptr)
2021 return nullptr; /* Dummy CU. */
2022
2023 dwarf2_find_base_address (cu->dies, cu);
2024
2025 return cu;
2026 }
2027
2028 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2029
2030 static void
2031 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2032 dwarf2_per_objfile *per_objfile, bool skip_partial)
2033 {
2034 {
2035 /* The destructor of dwarf2_queue_guard frees any entries left on
2036 the queue. After this point we're guaranteed to leave this function
2037 with the dwarf queue empty. */
2038 dwarf2_queue_guard q_guard (per_objfile);
2039
2040 if (!per_objfile->symtab_set_p (per_cu))
2041 {
2042 queue_comp_unit (per_cu, per_objfile, language_minimal);
2043 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2044
2045 /* If we just loaded a CU from a DWO, and we're working with an index
2046 that may badly handle TUs, load all the TUs in that DWO as well.
2047 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2048 if (!per_cu->is_debug_types
2049 && cu != NULL
2050 && cu->dwo_unit != NULL
2051 && per_objfile->per_bfd->index_table != NULL
2052 && !per_objfile->per_bfd->index_table->version_check ()
2053 /* DWP files aren't supported yet. */
2054 && get_dwp_file (per_objfile) == NULL)
2055 queue_and_load_all_dwo_tus (cu);
2056 }
2057
2058 process_queue (per_objfile);
2059 }
2060
2061 /* Age the cache, releasing compilation units that have not
2062 been used recently. */
2063 per_objfile->age_comp_units ();
2064 }
2065
2066 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2067 the per-objfile for which this symtab is instantiated.
2068
2069 Returns the resulting symbol table. */
2070
2071 static struct compunit_symtab *
2072 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2073 dwarf2_per_objfile *per_objfile,
2074 bool skip_partial)
2075 {
2076 if (!per_objfile->symtab_set_p (per_cu))
2077 {
2078 free_cached_comp_units freer (per_objfile);
2079 scoped_restore decrementer = increment_reading_symtab ();
2080 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2081 process_cu_includes (per_objfile);
2082 }
2083
2084 return per_objfile->get_symtab (per_cu);
2085 }
2086
2087 /* See read.h. */
2088
2089 dwarf2_per_cu_data_up
2090 dwarf2_per_bfd::allocate_per_cu ()
2091 {
2092 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2093 result->per_bfd = this;
2094 result->index = all_comp_units.size ();
2095 return result;
2096 }
2097
2098 /* See read.h. */
2099
2100 signatured_type_up
2101 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2102 {
2103 signatured_type_up result (new signatured_type (signature));
2104 result->per_bfd = this;
2105 result->index = all_comp_units.size ();
2106 result->is_debug_types = true;
2107 tu_stats.nr_tus++;
2108 return result;
2109 }
2110
2111 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2112 obstack, and constructed with the specified field values. */
2113
2114 static dwarf2_per_cu_data_up
2115 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2116 struct dwarf2_section_info *section,
2117 int is_dwz,
2118 sect_offset sect_off, ULONGEST length)
2119 {
2120 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2121 the_cu->sect_off = sect_off;
2122 the_cu->length = length;
2123 the_cu->section = section;
2124 the_cu->is_dwz = is_dwz;
2125 return the_cu;
2126 }
2127
2128 /* A helper for create_cus_from_index that handles a given list of
2129 CUs. */
2130
2131 static void
2132 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2133 const gdb_byte *cu_list, offset_type n_elements,
2134 struct dwarf2_section_info *section,
2135 int is_dwz)
2136 {
2137 for (offset_type i = 0; i < n_elements; i += 2)
2138 {
2139 gdb_static_assert (sizeof (ULONGEST) >= 8);
2140
2141 sect_offset sect_off
2142 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2143 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2144 cu_list += 2 * 8;
2145
2146 dwarf2_per_cu_data_up per_cu
2147 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2148 length);
2149 per_bfd->all_comp_units.push_back (std::move (per_cu));
2150 }
2151 }
2152
2153 /* Read the CU list from the mapped index, and use it to create all
2154 the CU objects for PER_BFD. */
2155
2156 static void
2157 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2158 const gdb_byte *cu_list, offset_type cu_list_elements,
2159 const gdb_byte *dwz_list, offset_type dwz_elements)
2160 {
2161 gdb_assert (per_bfd->all_comp_units.empty ());
2162 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2163
2164 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2165 &per_bfd->info, 0);
2166
2167 if (dwz_elements == 0)
2168 return;
2169
2170 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2171 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2172 &dwz->info, 1);
2173 }
2174
2175 /* Create the signatured type hash table from the index. */
2176
2177 static void
2178 create_signatured_type_table_from_index
2179 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2180 const gdb_byte *bytes, offset_type elements)
2181 {
2182 htab_up sig_types_hash = allocate_signatured_type_table ();
2183
2184 for (offset_type i = 0; i < elements; i += 3)
2185 {
2186 signatured_type_up sig_type;
2187 ULONGEST signature;
2188 void **slot;
2189 cu_offset type_offset_in_tu;
2190
2191 gdb_static_assert (sizeof (ULONGEST) >= 8);
2192 sect_offset sect_off
2193 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2194 type_offset_in_tu
2195 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2196 BFD_ENDIAN_LITTLE);
2197 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2198 bytes += 3 * 8;
2199
2200 sig_type = per_bfd->allocate_signatured_type (signature);
2201 sig_type->type_offset_in_tu = type_offset_in_tu;
2202 sig_type->section = section;
2203 sig_type->sect_off = sect_off;
2204
2205 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2206 *slot = sig_type.get ();
2207
2208 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2209 }
2210
2211 per_bfd->signatured_types = std::move (sig_types_hash);
2212 }
2213
2214 /* Create the signatured type hash table from .debug_names. */
2215
2216 static void
2217 create_signatured_type_table_from_debug_names
2218 (dwarf2_per_objfile *per_objfile,
2219 const mapped_debug_names &map,
2220 struct dwarf2_section_info *section,
2221 struct dwarf2_section_info *abbrev_section)
2222 {
2223 struct objfile *objfile = per_objfile->objfile;
2224
2225 section->read (objfile);
2226 abbrev_section->read (objfile);
2227
2228 htab_up sig_types_hash = allocate_signatured_type_table ();
2229
2230 for (uint32_t i = 0; i < map.tu_count; ++i)
2231 {
2232 signatured_type_up sig_type;
2233 void **slot;
2234
2235 sect_offset sect_off
2236 = (sect_offset) (extract_unsigned_integer
2237 (map.tu_table_reordered + i * map.offset_size,
2238 map.offset_size,
2239 map.dwarf5_byte_order));
2240
2241 comp_unit_head cu_header;
2242 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2243 abbrev_section,
2244 section->buffer + to_underlying (sect_off),
2245 rcuh_kind::TYPE);
2246
2247 sig_type = per_objfile->per_bfd->allocate_signatured_type
2248 (cu_header.signature);
2249 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2250 sig_type->section = section;
2251 sig_type->sect_off = sect_off;
2252
2253 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2254 *slot = sig_type.get ();
2255
2256 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2257 }
2258
2259 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2260 }
2261
2262 /* Read the address map data from the mapped index, and use it to
2263 populate the psymtabs_addrmap. */
2264
2265 static void
2266 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2267 struct mapped_index *index)
2268 {
2269 struct objfile *objfile = per_objfile->objfile;
2270 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2271 struct gdbarch *gdbarch = objfile->arch ();
2272 const gdb_byte *iter, *end;
2273 struct addrmap *mutable_map;
2274 CORE_ADDR baseaddr;
2275
2276 auto_obstack temp_obstack;
2277
2278 mutable_map = addrmap_create_mutable (&temp_obstack);
2279
2280 iter = index->address_table.data ();
2281 end = iter + index->address_table.size ();
2282
2283 baseaddr = objfile->text_section_offset ();
2284
2285 while (iter < end)
2286 {
2287 ULONGEST hi, lo, cu_index;
2288 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2289 iter += 8;
2290 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2291 iter += 8;
2292 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2293 iter += 4;
2294
2295 if (lo > hi)
2296 {
2297 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2298 hex_string (lo), hex_string (hi));
2299 continue;
2300 }
2301
2302 if (cu_index >= per_bfd->all_comp_units.size ())
2303 {
2304 complaint (_(".gdb_index address table has invalid CU number %u"),
2305 (unsigned) cu_index);
2306 continue;
2307 }
2308
2309 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2310 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2311 addrmap_set_empty (mutable_map, lo, hi - 1,
2312 per_bfd->get_cu (cu_index));
2313 }
2314
2315 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2316 &per_bfd->obstack);
2317 }
2318
2319 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2320 to populate given addrmap. Returns true on success, false on
2321 failure. */
2322
2323 static bool
2324 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2325 struct dwarf2_section_info *section,
2326 addrmap *mutable_map)
2327 {
2328 struct objfile *objfile = per_objfile->objfile;
2329 bfd *abfd = objfile->obfd;
2330 struct gdbarch *gdbarch = objfile->arch ();
2331 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2332 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2333
2334 std::unordered_map<sect_offset,
2335 dwarf2_per_cu_data *,
2336 gdb::hash_enum<sect_offset>>
2337 debug_info_offset_to_per_cu;
2338 for (const auto &per_cu : per_bfd->all_comp_units)
2339 {
2340 /* A TU will not need aranges, and skipping them here is an easy
2341 way of ignoring .debug_types -- and possibly seeing a
2342 duplicate section offset -- entirely. The same applies to
2343 units coming from a dwz file. */
2344 if (per_cu->is_debug_types || per_cu->is_dwz)
2345 continue;
2346
2347 const auto insertpair
2348 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2349 per_cu.get ());
2350 if (!insertpair.second)
2351 {
2352 warning (_("Section .debug_aranges in %s has duplicate "
2353 "debug_info_offset %s, ignoring .debug_aranges."),
2354 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2355 return false;
2356 }
2357 }
2358
2359 section->read (objfile);
2360
2361 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2362
2363 const gdb_byte *addr = section->buffer;
2364
2365 while (addr < section->buffer + section->size)
2366 {
2367 const gdb_byte *const entry_addr = addr;
2368 unsigned int bytes_read;
2369
2370 const LONGEST entry_length = read_initial_length (abfd, addr,
2371 &bytes_read);
2372 addr += bytes_read;
2373
2374 const gdb_byte *const entry_end = addr + entry_length;
2375 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2376 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2377 if (addr + entry_length > section->buffer + section->size)
2378 {
2379 warning (_("Section .debug_aranges in %s entry at offset %s "
2380 "length %s exceeds section length %s, "
2381 "ignoring .debug_aranges."),
2382 objfile_name (objfile),
2383 plongest (entry_addr - section->buffer),
2384 plongest (bytes_read + entry_length),
2385 pulongest (section->size));
2386 return false;
2387 }
2388
2389 /* The version number. */
2390 const uint16_t version = read_2_bytes (abfd, addr);
2391 addr += 2;
2392 if (version != 2)
2393 {
2394 warning (_("Section .debug_aranges in %s entry at offset %s "
2395 "has unsupported version %d, ignoring .debug_aranges."),
2396 objfile_name (objfile),
2397 plongest (entry_addr - section->buffer), version);
2398 return false;
2399 }
2400
2401 const uint64_t debug_info_offset
2402 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2403 addr += offset_size;
2404 const auto per_cu_it
2405 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2406 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2407 {
2408 warning (_("Section .debug_aranges in %s entry at offset %s "
2409 "debug_info_offset %s does not exists, "
2410 "ignoring .debug_aranges."),
2411 objfile_name (objfile),
2412 plongest (entry_addr - section->buffer),
2413 pulongest (debug_info_offset));
2414 return false;
2415 }
2416 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2417
2418 const uint8_t address_size = *addr++;
2419 if (address_size < 1 || address_size > 8)
2420 {
2421 warning (_("Section .debug_aranges in %s entry at offset %s "
2422 "address_size %u is invalid, ignoring .debug_aranges."),
2423 objfile_name (objfile),
2424 plongest (entry_addr - section->buffer), address_size);
2425 return false;
2426 }
2427
2428 const uint8_t segment_selector_size = *addr++;
2429 if (segment_selector_size != 0)
2430 {
2431 warning (_("Section .debug_aranges in %s entry at offset %s "
2432 "segment_selector_size %u is not supported, "
2433 "ignoring .debug_aranges."),
2434 objfile_name (objfile),
2435 plongest (entry_addr - section->buffer),
2436 segment_selector_size);
2437 return false;
2438 }
2439
2440 /* Must pad to an alignment boundary that is twice the address
2441 size. It is undocumented by the DWARF standard but GCC does
2442 use it. However, not every compiler does this. We can see
2443 whether it has happened by looking at the total length of the
2444 contents of the aranges for this CU -- it if isn't a multiple
2445 of twice the address size, then we skip any leftover
2446 bytes. */
2447 addr += (entry_end - addr) % (2 * address_size);
2448
2449 while (addr < entry_end)
2450 {
2451 if (addr + 2 * address_size > entry_end)
2452 {
2453 warning (_("Section .debug_aranges in %s entry at offset %s "
2454 "address list is not properly terminated, "
2455 "ignoring .debug_aranges."),
2456 objfile_name (objfile),
2457 plongest (entry_addr - section->buffer));
2458 return false;
2459 }
2460 ULONGEST start = extract_unsigned_integer (addr, address_size,
2461 dwarf5_byte_order);
2462 addr += address_size;
2463 ULONGEST length = extract_unsigned_integer (addr, address_size,
2464 dwarf5_byte_order);
2465 addr += address_size;
2466 if (start == 0 && length == 0)
2467 {
2468 /* This can happen on some targets with --gc-sections.
2469 This pair of values is also used to mark the end of
2470 the entries for a given CU, but we ignore it and
2471 instead handle termination using the check at the top
2472 of the loop. */
2473 continue;
2474 }
2475 if (start == 0 && !per_bfd->has_section_at_zero)
2476 {
2477 /* Symbol was eliminated due to a COMDAT group. */
2478 continue;
2479 }
2480 ULONGEST end = start + length;
2481 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2482 - baseaddr);
2483 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2484 - baseaddr);
2485 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2486 }
2487
2488 per_cu->addresses_seen = true;
2489 }
2490
2491 return true;
2492 }
2493
2494 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2495 populate the psymtabs_addrmap. */
2496
2497 static void
2498 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2499 struct dwarf2_section_info *section)
2500 {
2501 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2502
2503 auto_obstack temp_obstack;
2504 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2505
2506 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2507 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2508 &per_bfd->obstack);
2509 }
2510
2511 /* A helper function that reads the .gdb_index from BUFFER and fills
2512 in MAP. FILENAME is the name of the file containing the data;
2513 it is used for error reporting. DEPRECATED_OK is true if it is
2514 ok to use deprecated sections.
2515
2516 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2517 out parameters that are filled in with information about the CU and
2518 TU lists in the section.
2519
2520 Returns true if all went well, false otherwise. */
2521
2522 static bool
2523 read_gdb_index_from_buffer (const char *filename,
2524 bool deprecated_ok,
2525 gdb::array_view<const gdb_byte> buffer,
2526 struct mapped_index *map,
2527 const gdb_byte **cu_list,
2528 offset_type *cu_list_elements,
2529 const gdb_byte **types_list,
2530 offset_type *types_list_elements)
2531 {
2532 const gdb_byte *addr = &buffer[0];
2533 offset_view metadata (buffer);
2534
2535 /* Version check. */
2536 offset_type version = metadata[0];
2537 /* Versions earlier than 3 emitted every copy of a psymbol. This
2538 causes the index to behave very poorly for certain requests. Version 3
2539 contained incomplete addrmap. So, it seems better to just ignore such
2540 indices. */
2541 if (version < 4)
2542 {
2543 static int warning_printed = 0;
2544 if (!warning_printed)
2545 {
2546 warning (_("Skipping obsolete .gdb_index section in %s."),
2547 filename);
2548 warning_printed = 1;
2549 }
2550 return 0;
2551 }
2552 /* Index version 4 uses a different hash function than index version
2553 5 and later.
2554
2555 Versions earlier than 6 did not emit psymbols for inlined
2556 functions. Using these files will cause GDB not to be able to
2557 set breakpoints on inlined functions by name, so we ignore these
2558 indices unless the user has done
2559 "set use-deprecated-index-sections on". */
2560 if (version < 6 && !deprecated_ok)
2561 {
2562 static int warning_printed = 0;
2563 if (!warning_printed)
2564 {
2565 warning (_("\
2566 Skipping deprecated .gdb_index section in %s.\n\
2567 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2568 to use the section anyway."),
2569 filename);
2570 warning_printed = 1;
2571 }
2572 return 0;
2573 }
2574 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2575 of the TU (for symbols coming from TUs),
2576 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2577 Plus gold-generated indices can have duplicate entries for global symbols,
2578 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2579 These are just performance bugs, and we can't distinguish gdb-generated
2580 indices from gold-generated ones, so issue no warning here. */
2581
2582 /* Indexes with higher version than the one supported by GDB may be no
2583 longer backward compatible. */
2584 if (version > 8)
2585 return 0;
2586
2587 map->version = version;
2588
2589 int i = 1;
2590 *cu_list = addr + metadata[i];
2591 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2592 ++i;
2593
2594 *types_list = addr + metadata[i];
2595 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2596 ++i;
2597
2598 const gdb_byte *address_table = addr + metadata[i];
2599 const gdb_byte *address_table_end = addr + metadata[i + 1];
2600 map->address_table
2601 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2602 ++i;
2603
2604 const gdb_byte *symbol_table = addr + metadata[i];
2605 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2606 map->symbol_table
2607 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2608 symbol_table_end));
2609
2610 ++i;
2611 map->constant_pool = buffer.slice (metadata[i]);
2612
2613 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2614 {
2615 /* An empty constant pool implies that all symbol table entries are
2616 empty. Make map->symbol_table.empty () == true. */
2617 map->symbol_table
2618 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2619 symbol_table));
2620 }
2621
2622 return 1;
2623 }
2624
2625 /* Callback types for dwarf2_read_gdb_index. */
2626
2627 typedef gdb::function_view
2628 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2629 get_gdb_index_contents_ftype;
2630 typedef gdb::function_view
2631 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2632 get_gdb_index_contents_dwz_ftype;
2633
2634 /* Read .gdb_index. If everything went ok, initialize the "quick"
2635 elements of all the CUs and return 1. Otherwise, return 0. */
2636
2637 static int
2638 dwarf2_read_gdb_index
2639 (dwarf2_per_objfile *per_objfile,
2640 get_gdb_index_contents_ftype get_gdb_index_contents,
2641 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2642 {
2643 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2644 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2645 struct dwz_file *dwz;
2646 struct objfile *objfile = per_objfile->objfile;
2647 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2648
2649 gdb::array_view<const gdb_byte> main_index_contents
2650 = get_gdb_index_contents (objfile, per_bfd);
2651
2652 if (main_index_contents.empty ())
2653 return 0;
2654
2655 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2656 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2657 use_deprecated_index_sections,
2658 main_index_contents, map.get (), &cu_list,
2659 &cu_list_elements, &types_list,
2660 &types_list_elements))
2661 return 0;
2662
2663 /* Don't use the index if it's empty. */
2664 if (map->symbol_table.empty ())
2665 return 0;
2666
2667 /* If there is a .dwz file, read it so we can get its CU list as
2668 well. */
2669 dwz = dwarf2_get_dwz_file (per_bfd);
2670 if (dwz != NULL)
2671 {
2672 struct mapped_index dwz_map;
2673 const gdb_byte *dwz_types_ignore;
2674 offset_type dwz_types_elements_ignore;
2675
2676 gdb::array_view<const gdb_byte> dwz_index_content
2677 = get_gdb_index_contents_dwz (objfile, dwz);
2678
2679 if (dwz_index_content.empty ())
2680 return 0;
2681
2682 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2683 1, dwz_index_content, &dwz_map,
2684 &dwz_list, &dwz_list_elements,
2685 &dwz_types_ignore,
2686 &dwz_types_elements_ignore))
2687 {
2688 warning (_("could not read '.gdb_index' section from %s; skipping"),
2689 bfd_get_filename (dwz->dwz_bfd.get ()));
2690 return 0;
2691 }
2692 }
2693
2694 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2695 dwz_list_elements);
2696
2697 if (types_list_elements)
2698 {
2699 /* We can only handle a single .debug_types when we have an
2700 index. */
2701 if (per_bfd->types.size () != 1)
2702 return 0;
2703
2704 dwarf2_section_info *section = &per_bfd->types[0];
2705
2706 create_signatured_type_table_from_index (per_bfd, section, types_list,
2707 types_list_elements);
2708 }
2709
2710 create_addrmap_from_index (per_objfile, map.get ());
2711
2712 per_bfd->index_table = std::move (map);
2713 per_bfd->quick_file_names_table =
2714 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2715
2716 return 1;
2717 }
2718
2719 /* die_reader_func for dw2_get_file_names. */
2720
2721 static void
2722 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2723 struct die_info *comp_unit_die)
2724 {
2725 struct dwarf2_cu *cu = reader->cu;
2726 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2727 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2728 struct dwarf2_per_cu_data *lh_cu;
2729 struct attribute *attr;
2730 void **slot;
2731 struct quick_file_names *qfn;
2732
2733 gdb_assert (! this_cu->is_debug_types);
2734
2735 this_cu->files_read = true;
2736 /* Our callers never want to match partial units -- instead they
2737 will match the enclosing full CU. */
2738 if (comp_unit_die->tag == DW_TAG_partial_unit)
2739 return;
2740
2741 lh_cu = this_cu;
2742 slot = NULL;
2743
2744 line_header_up lh;
2745 sect_offset line_offset {};
2746
2747 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2748 if (attr != nullptr && attr->form_is_unsigned ())
2749 {
2750 struct quick_file_names find_entry;
2751
2752 line_offset = (sect_offset) attr->as_unsigned ();
2753
2754 /* We may have already read in this line header (TU line header sharing).
2755 If we have we're done. */
2756 find_entry.hash.dwo_unit = cu->dwo_unit;
2757 find_entry.hash.line_sect_off = line_offset;
2758 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2759 &find_entry, INSERT);
2760 if (*slot != NULL)
2761 {
2762 lh_cu->file_names = (struct quick_file_names *) *slot;
2763 return;
2764 }
2765
2766 lh = dwarf_decode_line_header (line_offset, cu);
2767 }
2768
2769 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2770
2771 int offset = 0;
2772 if (!fnd.is_unknown ())
2773 ++offset;
2774 else if (lh == nullptr)
2775 return;
2776
2777 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2778 qfn->hash.dwo_unit = cu->dwo_unit;
2779 qfn->hash.line_sect_off = line_offset;
2780 /* There may not be a DW_AT_stmt_list. */
2781 if (slot != nullptr)
2782 *slot = qfn;
2783
2784 std::vector<const char *> include_names;
2785 if (lh != nullptr)
2786 {
2787 for (const auto &entry : lh->file_names ())
2788 {
2789 std::string name_holder;
2790 const char *include_name =
2791 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2792 if (include_name != nullptr)
2793 {
2794 include_name = per_objfile->objfile->intern (include_name);
2795 include_names.push_back (include_name);
2796 }
2797 }
2798 }
2799
2800 qfn->num_file_names = offset + include_names.size ();
2801 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2802 qfn->file_names =
2803 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2804 qfn->num_file_names);
2805 if (offset != 0)
2806 qfn->file_names[0] = xstrdup (fnd.get_name ());
2807
2808 if (!include_names.empty ())
2809 memcpy (&qfn->file_names[offset], include_names.data (),
2810 include_names.size () * sizeof (const char *));
2811
2812 qfn->real_names = NULL;
2813
2814 lh_cu->file_names = qfn;
2815 }
2816
2817 /* A helper for the "quick" functions which attempts to read the line
2818 table for THIS_CU. */
2819
2820 static struct quick_file_names *
2821 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2822 dwarf2_per_objfile *per_objfile)
2823 {
2824 /* This should never be called for TUs. */
2825 gdb_assert (! this_cu->is_debug_types);
2826
2827 if (this_cu->files_read)
2828 return this_cu->file_names;
2829
2830 cutu_reader reader (this_cu, per_objfile);
2831 if (!reader.dummy_p)
2832 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2833
2834 return this_cu->file_names;
2835 }
2836
2837 /* A helper for the "quick" functions which computes and caches the
2838 real path for a given file name from the line table. */
2839
2840 static const char *
2841 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2842 struct quick_file_names *qfn, int index)
2843 {
2844 if (qfn->real_names == NULL)
2845 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2846 qfn->num_file_names, const char *);
2847
2848 if (qfn->real_names[index] == NULL)
2849 {
2850 const char *dirname = nullptr;
2851
2852 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2853 dirname = qfn->comp_dir;
2854
2855 gdb::unique_xmalloc_ptr<char> fullname;
2856 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2857
2858 qfn->real_names[index] = fullname.release ();
2859 }
2860
2861 return qfn->real_names[index];
2862 }
2863
2864 struct symtab *
2865 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2866 {
2867 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2868 dwarf2_per_cu_data *dwarf_cu
2869 = per_objfile->per_bfd->all_comp_units.back ().get ();
2870 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2871
2872 if (cust == NULL)
2873 return NULL;
2874
2875 return cust->primary_filetab ();
2876 }
2877
2878 /* See read.h. */
2879
2880 void
2881 dwarf2_per_cu_data::free_cached_file_names ()
2882 {
2883 if (fnd != nullptr)
2884 fnd->forget_fullname ();
2885
2886 if (per_bfd == nullptr)
2887 return;
2888
2889 struct quick_file_names *file_data = file_names;
2890 if (file_data != nullptr && file_data->real_names != nullptr)
2891 {
2892 for (int i = 0; i < file_data->num_file_names; ++i)
2893 {
2894 xfree ((void *) file_data->real_names[i]);
2895 file_data->real_names[i] = nullptr;
2896 }
2897 }
2898 }
2899
2900 void
2901 dwarf2_base_index_functions::forget_cached_source_info
2902 (struct objfile *objfile)
2903 {
2904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2905
2906 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
2907 per_cu->free_cached_file_names ();
2908 }
2909
2910 /* Struct used to manage iterating over all CUs looking for a symbol. */
2911
2912 struct dw2_symtab_iterator
2913 {
2914 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2915 dwarf2_per_objfile *per_objfile;
2916 /* If set, only look for symbols that match that block. Valid values are
2917 GLOBAL_BLOCK and STATIC_BLOCK. */
2918 gdb::optional<block_enum> block_index;
2919 /* The kind of symbol we're looking for. */
2920 domain_enum domain;
2921 /* The list of CUs from the index entry of the symbol,
2922 or NULL if not found. */
2923 offset_view vec;
2924 /* The next element in VEC to look at. */
2925 int next;
2926 /* The number of elements in VEC, or zero if there is no match. */
2927 int length;
2928 /* Have we seen a global version of the symbol?
2929 If so we can ignore all further global instances.
2930 This is to work around gold/15646, inefficient gold-generated
2931 indices. */
2932 int global_seen;
2933 };
2934
2935 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2936
2937 static void
2938 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2939 dwarf2_per_objfile *per_objfile,
2940 gdb::optional<block_enum> block_index,
2941 domain_enum domain, offset_type namei,
2942 mapped_index &index)
2943 {
2944 iter->per_objfile = per_objfile;
2945 iter->block_index = block_index;
2946 iter->domain = domain;
2947 iter->next = 0;
2948 iter->global_seen = 0;
2949 iter->vec = {};
2950 iter->length = 0;
2951
2952 gdb_assert (!index.symbol_name_slot_invalid (namei));
2953 offset_type vec_idx = index.symbol_vec_index (namei);
2954
2955 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2956 iter->length = iter->vec[0];
2957 }
2958
2959 /* Return the next matching CU or NULL if there are no more. */
2960
2961 static struct dwarf2_per_cu_data *
2962 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2963 mapped_index &index)
2964 {
2965 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2966
2967 for ( ; iter->next < iter->length; ++iter->next)
2968 {
2969 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2970 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2971 gdb_index_symbol_kind symbol_kind =
2972 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2973 /* Only check the symbol attributes if they're present.
2974 Indices prior to version 7 don't record them,
2975 and indices >= 7 may elide them for certain symbols
2976 (gold does this). */
2977 int attrs_valid = (index.version >= 7
2978 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2979
2980 /* Don't crash on bad data. */
2981 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2982 {
2983 complaint (_(".gdb_index entry has bad CU index"
2984 " [in module %s]"), objfile_name (per_objfile->objfile));
2985 continue;
2986 }
2987
2988 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
2989
2990 /* Skip if already read in. */
2991 if (per_objfile->symtab_set_p (per_cu))
2992 continue;
2993
2994 /* Check static vs global. */
2995 if (attrs_valid)
2996 {
2997 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2998
2999 if (iter->block_index.has_value ())
3000 {
3001 bool want_static = *iter->block_index == STATIC_BLOCK;
3002
3003 if (is_static != want_static)
3004 continue;
3005 }
3006
3007 /* Work around gold/15646. */
3008 if (!is_static
3009 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3010 {
3011 if (iter->global_seen)
3012 continue;
3013
3014 iter->global_seen = 1;
3015 }
3016 }
3017
3018 /* Only check the symbol's kind if it has one. */
3019 if (attrs_valid)
3020 {
3021 switch (iter->domain)
3022 {
3023 case VAR_DOMAIN:
3024 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3026 /* Some types are also in VAR_DOMAIN. */
3027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3028 continue;
3029 break;
3030 case STRUCT_DOMAIN:
3031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3032 continue;
3033 break;
3034 case LABEL_DOMAIN:
3035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3036 continue;
3037 break;
3038 case MODULE_DOMAIN:
3039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3040 continue;
3041 break;
3042 default:
3043 break;
3044 }
3045 }
3046
3047 ++iter->next;
3048 return per_cu;
3049 }
3050
3051 return NULL;
3052 }
3053
3054 void
3055 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3056 bool print_bcache)
3057 {
3058 if (print_bcache)
3059 return;
3060
3061 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3062 int total = per_objfile->per_bfd->all_comp_units.size ();
3063 int count = 0;
3064
3065 for (int i = 0; i < total; ++i)
3066 {
3067 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3068
3069 if (!per_objfile->symtab_set_p (per_cu))
3070 ++count;
3071 }
3072 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3073 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3074 }
3075
3076 /* This dumps minimal information about the index.
3077 It is called via "mt print objfiles".
3078 One use is to verify .gdb_index has been loaded by the
3079 gdb.dwarf2/gdb-index.exp testcase. */
3080
3081 void
3082 dwarf2_gdb_index::dump (struct objfile *objfile)
3083 {
3084 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3085
3086 mapped_index *index = (static_cast<mapped_index *>
3087 (per_objfile->per_bfd->index_table.get ()));
3088 gdb_printf (".gdb_index: version %d\n", index->version);
3089 gdb_printf ("\n");
3090 }
3091
3092 void
3093 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3094 {
3095 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3096 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3097
3098 for (int i = 0; i < total_units; ++i)
3099 {
3100 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3101
3102 /* We don't want to directly expand a partial CU, because if we
3103 read it with the wrong language, then assertion failures can
3104 be triggered later on. See PR symtab/23010. So, tell
3105 dw2_instantiate_symtab to skip partial CUs -- any important
3106 partial CU will be read via DW_TAG_imported_unit anyway. */
3107 dw2_instantiate_symtab (per_cu, per_objfile, true);
3108 }
3109 }
3110
3111 static bool
3112 dw2_expand_symtabs_matching_symbol
3113 (mapped_index_base &index,
3114 const lookup_name_info &lookup_name_in,
3115 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3116 gdb::function_view<bool (offset_type)> match_callback,
3117 dwarf2_per_objfile *per_objfile);
3118
3119 static bool
3120 dw2_expand_symtabs_matching_one
3121 (dwarf2_per_cu_data *per_cu,
3122 dwarf2_per_objfile *per_objfile,
3123 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3124 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3125
3126 void
3127 dwarf2_gdb_index::expand_matching_symbols
3128 (struct objfile *objfile,
3129 const lookup_name_info &name, domain_enum domain,
3130 int global,
3131 symbol_compare_ftype *ordered_compare)
3132 {
3133 /* Used for Ada. */
3134 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3135
3136 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3137
3138 mapped_index &index
3139 = (static_cast<mapped_index &>
3140 (*per_objfile->per_bfd->index_table.get ()));
3141
3142 const char *match_name = name.ada ().lookup_name ().c_str ();
3143 auto matcher = [&] (const char *symname)
3144 {
3145 if (ordered_compare == nullptr)
3146 return true;
3147 return ordered_compare (symname, match_name) == 0;
3148 };
3149
3150 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3151 [&] (offset_type namei)
3152 {
3153 struct dw2_symtab_iterator iter;
3154 struct dwarf2_per_cu_data *per_cu;
3155
3156 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3157 index);
3158 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3159 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3160 nullptr);
3161 return true;
3162 }, per_objfile);
3163 }
3164
3165 /* Starting from a search name, return the string that finds the upper
3166 bound of all strings that start with SEARCH_NAME in a sorted name
3167 list. Returns the empty string to indicate that the upper bound is
3168 the end of the list. */
3169
3170 static std::string
3171 make_sort_after_prefix_name (const char *search_name)
3172 {
3173 /* When looking to complete "func", we find the upper bound of all
3174 symbols that start with "func" by looking for where we'd insert
3175 the closest string that would follow "func" in lexicographical
3176 order. Usually, that's "func"-with-last-character-incremented,
3177 i.e. "fund". Mind non-ASCII characters, though. Usually those
3178 will be UTF-8 multi-byte sequences, but we can't be certain.
3179 Especially mind the 0xff character, which is a valid character in
3180 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3181 rule out compilers allowing it in identifiers. Note that
3182 conveniently, strcmp/strcasecmp are specified to compare
3183 characters interpreted as unsigned char. So what we do is treat
3184 the whole string as a base 256 number composed of a sequence of
3185 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3186 to 0, and carries 1 to the following more-significant position.
3187 If the very first character in SEARCH_NAME ends up incremented
3188 and carries/overflows, then the upper bound is the end of the
3189 list. The string after the empty string is also the empty
3190 string.
3191
3192 Some examples of this operation:
3193
3194 SEARCH_NAME => "+1" RESULT
3195
3196 "abc" => "abd"
3197 "ab\xff" => "ac"
3198 "\xff" "a" "\xff" => "\xff" "b"
3199 "\xff" => ""
3200 "\xff\xff" => ""
3201 "" => ""
3202
3203 Then, with these symbols for example:
3204
3205 func
3206 func1
3207 fund
3208
3209 completing "func" looks for symbols between "func" and
3210 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3211 which finds "func" and "func1", but not "fund".
3212
3213 And with:
3214
3215 funcÿ (Latin1 'ÿ' [0xff])
3216 funcÿ1
3217 fund
3218
3219 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3220 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3221
3222 And with:
3223
3224 ÿÿ (Latin1 'ÿ' [0xff])
3225 ÿÿ1
3226
3227 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3228 the end of the list.
3229 */
3230 std::string after = search_name;
3231 while (!after.empty () && (unsigned char) after.back () == 0xff)
3232 after.pop_back ();
3233 if (!after.empty ())
3234 after.back () = (unsigned char) after.back () + 1;
3235 return after;
3236 }
3237
3238 /* See declaration. */
3239
3240 std::pair<std::vector<name_component>::const_iterator,
3241 std::vector<name_component>::const_iterator>
3242 mapped_index_base::find_name_components_bounds
3243 (const lookup_name_info &lookup_name_without_params, language lang,
3244 dwarf2_per_objfile *per_objfile) const
3245 {
3246 auto *name_cmp
3247 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3248
3249 const char *lang_name
3250 = lookup_name_without_params.language_lookup_name (lang);
3251
3252 /* Comparison function object for lower_bound that matches against a
3253 given symbol name. */
3254 auto lookup_compare_lower = [&] (const name_component &elem,
3255 const char *name)
3256 {
3257 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3258 const char *elem_name = elem_qualified + elem.name_offset;
3259 return name_cmp (elem_name, name) < 0;
3260 };
3261
3262 /* Comparison function object for upper_bound that matches against a
3263 given symbol name. */
3264 auto lookup_compare_upper = [&] (const char *name,
3265 const name_component &elem)
3266 {
3267 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3268 const char *elem_name = elem_qualified + elem.name_offset;
3269 return name_cmp (name, elem_name) < 0;
3270 };
3271
3272 auto begin = this->name_components.begin ();
3273 auto end = this->name_components.end ();
3274
3275 /* Find the lower bound. */
3276 auto lower = [&] ()
3277 {
3278 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3279 return begin;
3280 else
3281 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3282 } ();
3283
3284 /* Find the upper bound. */
3285 auto upper = [&] ()
3286 {
3287 if (lookup_name_without_params.completion_mode ())
3288 {
3289 /* In completion mode, we want UPPER to point past all
3290 symbols names that have the same prefix. I.e., with
3291 these symbols, and completing "func":
3292
3293 function << lower bound
3294 function1
3295 other_function << upper bound
3296
3297 We find the upper bound by looking for the insertion
3298 point of "func"-with-last-character-incremented,
3299 i.e. "fund". */
3300 std::string after = make_sort_after_prefix_name (lang_name);
3301 if (after.empty ())
3302 return end;
3303 return std::lower_bound (lower, end, after.c_str (),
3304 lookup_compare_lower);
3305 }
3306 else
3307 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3308 } ();
3309
3310 return {lower, upper};
3311 }
3312
3313 /* See declaration. */
3314
3315 void
3316 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3317 {
3318 if (!this->name_components.empty ())
3319 return;
3320
3321 this->name_components_casing = case_sensitivity;
3322 auto *name_cmp
3323 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3324
3325 /* The code below only knows how to break apart components of C++
3326 symbol names (and other languages that use '::' as
3327 namespace/module separator) and Ada symbol names. */
3328 auto count = this->symbol_name_count ();
3329 for (offset_type idx = 0; idx < count; idx++)
3330 {
3331 if (this->symbol_name_slot_invalid (idx))
3332 continue;
3333
3334 const char *name = this->symbol_name_at (idx, per_objfile);
3335
3336 /* Add each name component to the name component table. */
3337 unsigned int previous_len = 0;
3338
3339 if (strstr (name, "::") != nullptr)
3340 {
3341 for (unsigned int current_len = cp_find_first_component (name);
3342 name[current_len] != '\0';
3343 current_len += cp_find_first_component (name + current_len))
3344 {
3345 gdb_assert (name[current_len] == ':');
3346 this->name_components.push_back ({previous_len, idx});
3347 /* Skip the '::'. */
3348 current_len += 2;
3349 previous_len = current_len;
3350 }
3351 }
3352 else
3353 {
3354 /* Handle the Ada encoded (aka mangled) form here. */
3355 for (const char *iter = strstr (name, "__");
3356 iter != nullptr;
3357 iter = strstr (iter, "__"))
3358 {
3359 this->name_components.push_back ({previous_len, idx});
3360 iter += 2;
3361 previous_len = iter - name;
3362 }
3363 }
3364
3365 this->name_components.push_back ({previous_len, idx});
3366 }
3367
3368 /* Sort name_components elements by name. */
3369 auto name_comp_compare = [&] (const name_component &left,
3370 const name_component &right)
3371 {
3372 const char *left_qualified
3373 = this->symbol_name_at (left.idx, per_objfile);
3374 const char *right_qualified
3375 = this->symbol_name_at (right.idx, per_objfile);
3376
3377 const char *left_name = left_qualified + left.name_offset;
3378 const char *right_name = right_qualified + right.name_offset;
3379
3380 return name_cmp (left_name, right_name) < 0;
3381 };
3382
3383 std::sort (this->name_components.begin (),
3384 this->name_components.end (),
3385 name_comp_compare);
3386 }
3387
3388 /* Helper for dw2_expand_symtabs_matching that works with a
3389 mapped_index_base instead of the containing objfile. This is split
3390 to a separate function in order to be able to unit test the
3391 name_components matching using a mock mapped_index_base. For each
3392 symbol name that matches, calls MATCH_CALLBACK, passing it the
3393 symbol's index in the mapped_index_base symbol table. */
3394
3395 static bool
3396 dw2_expand_symtabs_matching_symbol
3397 (mapped_index_base &index,
3398 const lookup_name_info &lookup_name_in,
3399 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3400 gdb::function_view<bool (offset_type)> match_callback,
3401 dwarf2_per_objfile *per_objfile)
3402 {
3403 lookup_name_info lookup_name_without_params
3404 = lookup_name_in.make_ignore_params ();
3405
3406 /* Build the symbol name component sorted vector, if we haven't
3407 yet. */
3408 index.build_name_components (per_objfile);
3409
3410 /* The same symbol may appear more than once in the range though.
3411 E.g., if we're looking for symbols that complete "w", and we have
3412 a symbol named "w1::w2", we'll find the two name components for
3413 that same symbol in the range. To be sure we only call the
3414 callback once per symbol, we first collect the symbol name
3415 indexes that matched in a temporary vector and ignore
3416 duplicates. */
3417 std::vector<offset_type> matches;
3418
3419 struct name_and_matcher
3420 {
3421 symbol_name_matcher_ftype *matcher;
3422 const char *name;
3423
3424 bool operator== (const name_and_matcher &other) const
3425 {
3426 return matcher == other.matcher && strcmp (name, other.name) == 0;
3427 }
3428 };
3429
3430 /* A vector holding all the different symbol name matchers, for all
3431 languages. */
3432 std::vector<name_and_matcher> matchers;
3433
3434 for (int i = 0; i < nr_languages; i++)
3435 {
3436 enum language lang_e = (enum language) i;
3437
3438 const language_defn *lang = language_def (lang_e);
3439 symbol_name_matcher_ftype *name_matcher
3440 = lang->get_symbol_name_matcher (lookup_name_without_params);
3441
3442 name_and_matcher key {
3443 name_matcher,
3444 lookup_name_without_params.language_lookup_name (lang_e)
3445 };
3446
3447 /* Don't insert the same comparison routine more than once.
3448 Note that we do this linear walk. This is not a problem in
3449 practice because the number of supported languages is
3450 low. */
3451 if (std::find (matchers.begin (), matchers.end (), key)
3452 != matchers.end ())
3453 continue;
3454 matchers.push_back (std::move (key));
3455
3456 auto bounds
3457 = index.find_name_components_bounds (lookup_name_without_params,
3458 lang_e, per_objfile);
3459
3460 /* Now for each symbol name in range, check to see if we have a name
3461 match, and if so, call the MATCH_CALLBACK callback. */
3462
3463 for (; bounds.first != bounds.second; ++bounds.first)
3464 {
3465 const char *qualified
3466 = index.symbol_name_at (bounds.first->idx, per_objfile);
3467
3468 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3469 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3470 continue;
3471
3472 matches.push_back (bounds.first->idx);
3473 }
3474 }
3475
3476 std::sort (matches.begin (), matches.end ());
3477
3478 /* Finally call the callback, once per match. */
3479 ULONGEST prev = -1;
3480 bool result = true;
3481 for (offset_type idx : matches)
3482 {
3483 if (prev != idx)
3484 {
3485 if (!match_callback (idx))
3486 {
3487 result = false;
3488 break;
3489 }
3490 prev = idx;
3491 }
3492 }
3493
3494 /* Above we use a type wider than idx's for 'prev', since 0 and
3495 (offset_type)-1 are both possible values. */
3496 static_assert (sizeof (prev) > sizeof (offset_type), "");
3497
3498 return result;
3499 }
3500
3501 #if GDB_SELF_TEST
3502
3503 namespace selftests { namespace dw2_expand_symtabs_matching {
3504
3505 /* A mock .gdb_index/.debug_names-like name index table, enough to
3506 exercise dw2_expand_symtabs_matching_symbol, which works with the
3507 mapped_index_base interface. Builds an index from the symbol list
3508 passed as parameter to the constructor. */
3509 class mock_mapped_index : public mapped_index_base
3510 {
3511 public:
3512 mock_mapped_index (gdb::array_view<const char *> symbols)
3513 : m_symbol_table (symbols)
3514 {}
3515
3516 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3517
3518 /* Return the number of names in the symbol table. */
3519 size_t symbol_name_count () const override
3520 {
3521 return m_symbol_table.size ();
3522 }
3523
3524 /* Get the name of the symbol at IDX in the symbol table. */
3525 const char *symbol_name_at
3526 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3527 {
3528 return m_symbol_table[idx];
3529 }
3530
3531 quick_symbol_functions_up make_quick_functions () const override
3532 {
3533 return nullptr;
3534 }
3535
3536 private:
3537 gdb::array_view<const char *> m_symbol_table;
3538 };
3539
3540 /* Convenience function that converts a NULL pointer to a "<null>"
3541 string, to pass to print routines. */
3542
3543 static const char *
3544 string_or_null (const char *str)
3545 {
3546 return str != NULL ? str : "<null>";
3547 }
3548
3549 /* Check if a lookup_name_info built from
3550 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3551 index. EXPECTED_LIST is the list of expected matches, in expected
3552 matching order. If no match expected, then an empty list is
3553 specified. Returns true on success. On failure prints a warning
3554 indicating the file:line that failed, and returns false. */
3555
3556 static bool
3557 check_match (const char *file, int line,
3558 mock_mapped_index &mock_index,
3559 const char *name, symbol_name_match_type match_type,
3560 bool completion_mode,
3561 std::initializer_list<const char *> expected_list,
3562 dwarf2_per_objfile *per_objfile)
3563 {
3564 lookup_name_info lookup_name (name, match_type, completion_mode);
3565
3566 bool matched = true;
3567
3568 auto mismatch = [&] (const char *expected_str,
3569 const char *got)
3570 {
3571 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3572 "expected=\"%s\", got=\"%s\"\n"),
3573 file, line,
3574 (match_type == symbol_name_match_type::FULL
3575 ? "FULL" : "WILD"),
3576 name, string_or_null (expected_str), string_or_null (got));
3577 matched = false;
3578 };
3579
3580 auto expected_it = expected_list.begin ();
3581 auto expected_end = expected_list.end ();
3582
3583 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3584 nullptr,
3585 [&] (offset_type idx)
3586 {
3587 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3588 const char *expected_str
3589 = expected_it == expected_end ? NULL : *expected_it++;
3590
3591 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3592 mismatch (expected_str, matched_name);
3593 return true;
3594 }, per_objfile);
3595
3596 const char *expected_str
3597 = expected_it == expected_end ? NULL : *expected_it++;
3598 if (expected_str != NULL)
3599 mismatch (expected_str, NULL);
3600
3601 return matched;
3602 }
3603
3604 /* The symbols added to the mock mapped_index for testing (in
3605 canonical form). */
3606 static const char *test_symbols[] = {
3607 "function",
3608 "std::bar",
3609 "std::zfunction",
3610 "std::zfunction2",
3611 "w1::w2",
3612 "ns::foo<char*>",
3613 "ns::foo<int>",
3614 "ns::foo<long>",
3615 "ns2::tmpl<int>::foo2",
3616 "(anonymous namespace)::A::B::C",
3617
3618 /* These are used to check that the increment-last-char in the
3619 matching algorithm for completion doesn't match "t1_fund" when
3620 completing "t1_func". */
3621 "t1_func",
3622 "t1_func1",
3623 "t1_fund",
3624 "t1_fund1",
3625
3626 /* A UTF-8 name with multi-byte sequences to make sure that
3627 cp-name-parser understands this as a single identifier ("função"
3628 is "function" in PT). */
3629 u8"u8função",
3630
3631 /* \377 (0xff) is Latin1 'ÿ'. */
3632 "yfunc\377",
3633
3634 /* \377 (0xff) is Latin1 'ÿ'. */
3635 "\377",
3636 "\377\377123",
3637
3638 /* A name with all sorts of complications. Starts with "z" to make
3639 it easier for the completion tests below. */
3640 #define Z_SYM_NAME \
3641 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3642 "::tuple<(anonymous namespace)::ui*, " \
3643 "std::default_delete<(anonymous namespace)::ui>, void>"
3644
3645 Z_SYM_NAME
3646 };
3647
3648 /* Returns true if the mapped_index_base::find_name_component_bounds
3649 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3650 in completion mode. */
3651
3652 static bool
3653 check_find_bounds_finds (mapped_index_base &index,
3654 const char *search_name,
3655 gdb::array_view<const char *> expected_syms,
3656 dwarf2_per_objfile *per_objfile)
3657 {
3658 lookup_name_info lookup_name (search_name,
3659 symbol_name_match_type::FULL, true);
3660
3661 auto bounds = index.find_name_components_bounds (lookup_name,
3662 language_cplus,
3663 per_objfile);
3664
3665 size_t distance = std::distance (bounds.first, bounds.second);
3666 if (distance != expected_syms.size ())
3667 return false;
3668
3669 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3670 {
3671 auto nc_elem = bounds.first + exp_elem;
3672 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3673 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3674 return false;
3675 }
3676
3677 return true;
3678 }
3679
3680 /* Test the lower-level mapped_index::find_name_component_bounds
3681 method. */
3682
3683 static void
3684 test_mapped_index_find_name_component_bounds ()
3685 {
3686 mock_mapped_index mock_index (test_symbols);
3687
3688 mock_index.build_name_components (NULL /* per_objfile */);
3689
3690 /* Test the lower-level mapped_index::find_name_component_bounds
3691 method in completion mode. */
3692 {
3693 static const char *expected_syms[] = {
3694 "t1_func",
3695 "t1_func1",
3696 };
3697
3698 SELF_CHECK (check_find_bounds_finds
3699 (mock_index, "t1_func", expected_syms,
3700 NULL /* per_objfile */));
3701 }
3702
3703 /* Check that the increment-last-char in the name matching algorithm
3704 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3705 {
3706 static const char *expected_syms1[] = {
3707 "\377",
3708 "\377\377123",
3709 };
3710 SELF_CHECK (check_find_bounds_finds
3711 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3712
3713 static const char *expected_syms2[] = {
3714 "\377\377123",
3715 };
3716 SELF_CHECK (check_find_bounds_finds
3717 (mock_index, "\377\377", expected_syms2,
3718 NULL /* per_objfile */));
3719 }
3720 }
3721
3722 /* Test dw2_expand_symtabs_matching_symbol. */
3723
3724 static void
3725 test_dw2_expand_symtabs_matching_symbol ()
3726 {
3727 mock_mapped_index mock_index (test_symbols);
3728
3729 /* We let all tests run until the end even if some fails, for debug
3730 convenience. */
3731 bool any_mismatch = false;
3732
3733 /* Create the expected symbols list (an initializer_list). Needed
3734 because lists have commas, and we need to pass them to CHECK,
3735 which is a macro. */
3736 #define EXPECT(...) { __VA_ARGS__ }
3737
3738 /* Wrapper for check_match that passes down the current
3739 __FILE__/__LINE__. */
3740 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3741 any_mismatch |= !check_match (__FILE__, __LINE__, \
3742 mock_index, \
3743 NAME, MATCH_TYPE, COMPLETION_MODE, \
3744 EXPECTED_LIST, NULL)
3745
3746 /* Identity checks. */
3747 for (const char *sym : test_symbols)
3748 {
3749 /* Should be able to match all existing symbols. */
3750 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3751 EXPECT (sym));
3752
3753 /* Should be able to match all existing symbols with
3754 parameters. */
3755 std::string with_params = std::string (sym) + "(int)";
3756 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3757 EXPECT (sym));
3758
3759 /* Should be able to match all existing symbols with
3760 parameters and qualifiers. */
3761 with_params = std::string (sym) + " ( int ) const";
3762 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3763 EXPECT (sym));
3764
3765 /* This should really find sym, but cp-name-parser.y doesn't
3766 know about lvalue/rvalue qualifiers yet. */
3767 with_params = std::string (sym) + " ( int ) &&";
3768 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3769 {});
3770 }
3771
3772 /* Check that the name matching algorithm for completion doesn't get
3773 confused with Latin1 'ÿ' / 0xff. */
3774 {
3775 static const char str[] = "\377";
3776 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3777 EXPECT ("\377", "\377\377123"));
3778 }
3779
3780 /* Check that the increment-last-char in the matching algorithm for
3781 completion doesn't match "t1_fund" when completing "t1_func". */
3782 {
3783 static const char str[] = "t1_func";
3784 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3785 EXPECT ("t1_func", "t1_func1"));
3786 }
3787
3788 /* Check that completion mode works at each prefix of the expected
3789 symbol name. */
3790 {
3791 static const char str[] = "function(int)";
3792 size_t len = strlen (str);
3793 std::string lookup;
3794
3795 for (size_t i = 1; i < len; i++)
3796 {
3797 lookup.assign (str, i);
3798 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3799 EXPECT ("function"));
3800 }
3801 }
3802
3803 /* While "w" is a prefix of both components, the match function
3804 should still only be called once. */
3805 {
3806 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3807 EXPECT ("w1::w2"));
3808 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3809 EXPECT ("w1::w2"));
3810 }
3811
3812 /* Same, with a "complicated" symbol. */
3813 {
3814 static const char str[] = Z_SYM_NAME;
3815 size_t len = strlen (str);
3816 std::string lookup;
3817
3818 for (size_t i = 1; i < len; i++)
3819 {
3820 lookup.assign (str, i);
3821 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3822 EXPECT (Z_SYM_NAME));
3823 }
3824 }
3825
3826 /* In FULL mode, an incomplete symbol doesn't match. */
3827 {
3828 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3829 {});
3830 }
3831
3832 /* A complete symbol with parameters matches any overload, since the
3833 index has no overload info. */
3834 {
3835 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3836 EXPECT ("std::zfunction", "std::zfunction2"));
3837 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3838 EXPECT ("std::zfunction", "std::zfunction2"));
3839 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3840 EXPECT ("std::zfunction", "std::zfunction2"));
3841 }
3842
3843 /* Check that whitespace is ignored appropriately. A symbol with a
3844 template argument list. */
3845 {
3846 static const char expected[] = "ns::foo<int>";
3847 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3848 EXPECT (expected));
3849 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3850 EXPECT (expected));
3851 }
3852
3853 /* Check that whitespace is ignored appropriately. A symbol with a
3854 template argument list that includes a pointer. */
3855 {
3856 static const char expected[] = "ns::foo<char*>";
3857 /* Try both completion and non-completion modes. */
3858 static const bool completion_mode[2] = {false, true};
3859 for (size_t i = 0; i < 2; i++)
3860 {
3861 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3862 completion_mode[i], EXPECT (expected));
3863 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3864 completion_mode[i], EXPECT (expected));
3865
3866 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3867 completion_mode[i], EXPECT (expected));
3868 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3869 completion_mode[i], EXPECT (expected));
3870 }
3871 }
3872
3873 {
3874 /* Check method qualifiers are ignored. */
3875 static const char expected[] = "ns::foo<char*>";
3876 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3877 symbol_name_match_type::FULL, true, EXPECT (expected));
3878 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3879 symbol_name_match_type::FULL, true, EXPECT (expected));
3880 CHECK_MATCH ("foo < char * > ( int ) const",
3881 symbol_name_match_type::WILD, true, EXPECT (expected));
3882 CHECK_MATCH ("foo < char * > ( int ) &&",
3883 symbol_name_match_type::WILD, true, EXPECT (expected));
3884 }
3885
3886 /* Test lookup names that don't match anything. */
3887 {
3888 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3889 {});
3890
3891 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3892 {});
3893 }
3894
3895 /* Some wild matching tests, exercising "(anonymous namespace)",
3896 which should not be confused with a parameter list. */
3897 {
3898 static const char *syms[] = {
3899 "A::B::C",
3900 "B::C",
3901 "C",
3902 "A :: B :: C ( int )",
3903 "B :: C ( int )",
3904 "C ( int )",
3905 };
3906
3907 for (const char *s : syms)
3908 {
3909 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3910 EXPECT ("(anonymous namespace)::A::B::C"));
3911 }
3912 }
3913
3914 {
3915 static const char expected[] = "ns2::tmpl<int>::foo2";
3916 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3917 EXPECT (expected));
3918 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3919 EXPECT (expected));
3920 }
3921
3922 SELF_CHECK (!any_mismatch);
3923
3924 #undef EXPECT
3925 #undef CHECK_MATCH
3926 }
3927
3928 static void
3929 run_test ()
3930 {
3931 test_mapped_index_find_name_component_bounds ();
3932 test_dw2_expand_symtabs_matching_symbol ();
3933 }
3934
3935 }} // namespace selftests::dw2_expand_symtabs_matching
3936
3937 #endif /* GDB_SELF_TEST */
3938
3939 /* If FILE_MATCHER is NULL or if PER_CU has
3940 dwarf2_per_cu_quick_data::MARK set (see
3941 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3942 EXPANSION_NOTIFY on it. */
3943
3944 static bool
3945 dw2_expand_symtabs_matching_one
3946 (dwarf2_per_cu_data *per_cu,
3947 dwarf2_per_objfile *per_objfile,
3948 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3949 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3950 {
3951 if (file_matcher == NULL || per_cu->mark)
3952 {
3953 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3954
3955 compunit_symtab *symtab
3956 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3957 gdb_assert (symtab != nullptr);
3958
3959 if (expansion_notify != NULL && symtab_was_null)
3960 return expansion_notify (symtab);
3961 }
3962 return true;
3963 }
3964
3965 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3966 matched, to expand corresponding CUs that were marked. IDX is the
3967 index of the symbol name that matched. */
3968
3969 static bool
3970 dw2_expand_marked_cus
3971 (dwarf2_per_objfile *per_objfile, offset_type idx,
3972 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3973 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3974 block_search_flags search_flags,
3975 search_domain kind)
3976 {
3977 offset_type vec_len, vec_idx;
3978 bool global_seen = false;
3979 mapped_index &index
3980 = (static_cast<mapped_index &>
3981 (*per_objfile->per_bfd->index_table.get ()));
3982
3983 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
3984 vec_len = vec[0];
3985 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3986 {
3987 offset_type cu_index_and_attrs = vec[vec_idx + 1];
3988 /* This value is only valid for index versions >= 7. */
3989 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3990 gdb_index_symbol_kind symbol_kind =
3991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3992 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3993 /* Only check the symbol attributes if they're present.
3994 Indices prior to version 7 don't record them,
3995 and indices >= 7 may elide them for certain symbols
3996 (gold does this). */
3997 int attrs_valid =
3998 (index.version >= 7
3999 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4000
4001 /* Work around gold/15646. */
4002 if (attrs_valid
4003 && !is_static
4004 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4005 {
4006 if (global_seen)
4007 continue;
4008
4009 global_seen = true;
4010 }
4011
4012 /* Only check the symbol's kind if it has one. */
4013 if (attrs_valid)
4014 {
4015 if (is_static)
4016 {
4017 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4018 continue;
4019 }
4020 else
4021 {
4022 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4023 continue;
4024 }
4025
4026 switch (kind)
4027 {
4028 case VARIABLES_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4030 continue;
4031 break;
4032 case FUNCTIONS_DOMAIN:
4033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4034 continue;
4035 break;
4036 case TYPES_DOMAIN:
4037 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4038 continue;
4039 break;
4040 case MODULES_DOMAIN:
4041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4042 continue;
4043 break;
4044 default:
4045 break;
4046 }
4047 }
4048
4049 /* Don't crash on bad data. */
4050 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4051 {
4052 complaint (_(".gdb_index entry has bad CU index"
4053 " [in module %s]"), objfile_name (per_objfile->objfile));
4054 continue;
4055 }
4056
4057 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4058 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4059 expansion_notify))
4060 return false;
4061 }
4062
4063 return true;
4064 }
4065
4066 /* If FILE_MATCHER is non-NULL, set all the
4067 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4068 that match FILE_MATCHER. */
4069
4070 static void
4071 dw_expand_symtabs_matching_file_matcher
4072 (dwarf2_per_objfile *per_objfile,
4073 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4074 {
4075 if (file_matcher == NULL)
4076 return;
4077
4078 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4079 htab_eq_pointer,
4080 NULL, xcalloc, xfree));
4081 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4082 htab_eq_pointer,
4083 NULL, xcalloc, xfree));
4084
4085 /* The rule is CUs specify all the files, including those used by
4086 any TU, so there's no need to scan TUs here. */
4087
4088 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4089 {
4090 QUIT;
4091
4092 if (per_cu->is_debug_types)
4093 continue;
4094 per_cu->mark = 0;
4095
4096 /* We only need to look at symtabs not already expanded. */
4097 if (per_objfile->symtab_set_p (per_cu.get ()))
4098 continue;
4099
4100 if (per_cu->fnd != nullptr)
4101 {
4102 file_and_directory *fnd = per_cu->fnd.get ();
4103
4104 if (file_matcher (fnd->get_name (), false))
4105 {
4106 per_cu->mark = 1;
4107 continue;
4108 }
4109
4110 /* Before we invoke realpath, which can get expensive when many
4111 files are involved, do a quick comparison of the basenames. */
4112 if ((basenames_may_differ
4113 || file_matcher (lbasename (fnd->get_name ()), true))
4114 && file_matcher (fnd->get_fullname (), false))
4115 {
4116 per_cu->mark = 1;
4117 continue;
4118 }
4119 }
4120
4121 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4122 per_objfile);
4123 if (file_data == NULL)
4124 continue;
4125
4126 if (htab_find (visited_not_found.get (), file_data) != NULL)
4127 continue;
4128 else if (htab_find (visited_found.get (), file_data) != NULL)
4129 {
4130 per_cu->mark = 1;
4131 continue;
4132 }
4133
4134 for (int j = 0; j < file_data->num_file_names; ++j)
4135 {
4136 const char *this_real_name;
4137
4138 if (file_matcher (file_data->file_names[j], false))
4139 {
4140 per_cu->mark = 1;
4141 break;
4142 }
4143
4144 /* Before we invoke realpath, which can get expensive when many
4145 files are involved, do a quick comparison of the basenames. */
4146 if (!basenames_may_differ
4147 && !file_matcher (lbasename (file_data->file_names[j]),
4148 true))
4149 continue;
4150
4151 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4152 if (file_matcher (this_real_name, false))
4153 {
4154 per_cu->mark = 1;
4155 break;
4156 }
4157 }
4158
4159 void **slot = htab_find_slot (per_cu->mark
4160 ? visited_found.get ()
4161 : visited_not_found.get (),
4162 file_data, INSERT);
4163 *slot = file_data;
4164 }
4165 }
4166
4167 bool
4168 dwarf2_gdb_index::expand_symtabs_matching
4169 (struct objfile *objfile,
4170 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4171 const lookup_name_info *lookup_name,
4172 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4173 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4174 block_search_flags search_flags,
4175 domain_enum domain,
4176 enum search_domain kind)
4177 {
4178 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4179
4180 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4181
4182 /* This invariant is documented in quick-functions.h. */
4183 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4184 if (lookup_name == nullptr)
4185 {
4186 for (dwarf2_per_cu_data *per_cu
4187 : all_comp_units_range (per_objfile->per_bfd))
4188 {
4189 QUIT;
4190
4191 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4192 file_matcher,
4193 expansion_notify))
4194 return false;
4195 }
4196 return true;
4197 }
4198
4199 mapped_index &index
4200 = (static_cast<mapped_index &>
4201 (*per_objfile->per_bfd->index_table.get ()));
4202
4203 bool result
4204 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4205 symbol_matcher,
4206 [&] (offset_type idx)
4207 {
4208 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4209 expansion_notify, search_flags, kind))
4210 return false;
4211 return true;
4212 }, per_objfile);
4213
4214 return result;
4215 }
4216
4217 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4218 symtab. */
4219
4220 static struct compunit_symtab *
4221 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4222 CORE_ADDR pc)
4223 {
4224 int i;
4225
4226 if (cust->blockvector () != nullptr
4227 && blockvector_contains_pc (cust->blockvector (), pc))
4228 return cust;
4229
4230 if (cust->includes == NULL)
4231 return NULL;
4232
4233 for (i = 0; cust->includes[i]; ++i)
4234 {
4235 struct compunit_symtab *s = cust->includes[i];
4236
4237 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4238 if (s != NULL)
4239 return s;
4240 }
4241
4242 return NULL;
4243 }
4244
4245 dwarf2_per_cu_data *
4246 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
4247 CORE_ADDR adjusted_pc)
4248 {
4249 if (per_bfd->index_addrmap == nullptr)
4250 return nullptr;
4251 return (struct dwarf2_per_cu_data *) addrmap_find (per_bfd->index_addrmap,
4252 adjusted_pc);
4253 }
4254
4255 struct compunit_symtab *
4256 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4257 (struct objfile *objfile,
4258 struct bound_minimal_symbol msymbol,
4259 CORE_ADDR pc,
4260 struct obj_section *section,
4261 int warn_if_readin)
4262 {
4263 struct compunit_symtab *result;
4264
4265 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4266
4267 CORE_ADDR baseaddr = objfile->text_section_offset ();
4268 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
4269 pc - baseaddr);
4270 if (data == nullptr)
4271 return nullptr;
4272
4273 if (warn_if_readin && per_objfile->symtab_set_p (data))
4274 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4275 paddress (objfile->arch (), pc));
4276
4277 result = recursively_find_pc_sect_compunit_symtab
4278 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4279
4280 if (warn_if_readin && result == nullptr)
4281 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4282 paddress (objfile->arch (), pc));
4283
4284 return result;
4285 }
4286
4287 void
4288 dwarf2_base_index_functions::map_symbol_filenames
4289 (struct objfile *objfile,
4290 gdb::function_view<symbol_filename_ftype> fun,
4291 bool need_fullname)
4292 {
4293 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4294
4295 /* Use caches to ensure we only call FUN once for each filename. */
4296 filename_seen_cache filenames_cache;
4297 std::unordered_set<quick_file_names *> qfn_cache;
4298
4299 /* The rule is CUs specify all the files, including those used by any TU,
4300 so there's no need to scan TUs here. We can ignore file names coming
4301 from already-expanded CUs. It is possible that an expanded CU might
4302 reuse the file names data from a currently unexpanded CU, in this
4303 case we don't want to report the files from the unexpanded CU. */
4304
4305 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4306 {
4307 if (!per_cu->is_debug_types
4308 && per_objfile->symtab_set_p (per_cu.get ()))
4309 {
4310 if (per_cu->file_names != nullptr)
4311 qfn_cache.insert (per_cu->file_names);
4312 }
4313 }
4314
4315 for (dwarf2_per_cu_data *per_cu
4316 : all_comp_units_range (per_objfile->per_bfd))
4317 {
4318 /* We only need to look at symtabs not already expanded. */
4319 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4320 continue;
4321
4322 if (per_cu->fnd != nullptr)
4323 {
4324 file_and_directory *fnd = per_cu->fnd.get ();
4325
4326 const char *filename = fnd->get_name ();
4327 const char *key = filename;
4328 const char *fullname = nullptr;
4329
4330 if (need_fullname)
4331 {
4332 fullname = fnd->get_fullname ();
4333 key = fullname;
4334 }
4335
4336 if (!filenames_cache.seen (key))
4337 fun (filename, fullname);
4338 }
4339
4340 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4341 if (file_data == nullptr
4342 || qfn_cache.find (file_data) != qfn_cache.end ())
4343 continue;
4344
4345 for (int j = 0; j < file_data->num_file_names; ++j)
4346 {
4347 const char *filename = file_data->file_names[j];
4348 const char *key = filename;
4349 const char *fullname = nullptr;
4350
4351 if (need_fullname)
4352 {
4353 fullname = dw2_get_real_path (per_objfile, file_data, j);
4354 key = fullname;
4355 }
4356
4357 if (!filenames_cache.seen (key))
4358 fun (filename, fullname);
4359 }
4360 }
4361 }
4362
4363 bool
4364 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4365 {
4366 return true;
4367 }
4368
4369 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4370
4371 bool
4372 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4373 {
4374 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4375
4376 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4377 {
4378 /* Is this already expanded? */
4379 if (per_objfile->symtab_set_p (per_cu.get ()))
4380 continue;
4381
4382 /* It has not yet been expanded. */
4383 return true;
4384 }
4385
4386 return false;
4387 }
4388
4389 /* DWARF-5 debug_names reader. */
4390
4391 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4392 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4393
4394 /* A helper function that reads the .debug_names section in SECTION
4395 and fills in MAP. FILENAME is the name of the file containing the
4396 section; it is used for error reporting.
4397
4398 Returns true if all went well, false otherwise. */
4399
4400 static bool
4401 read_debug_names_from_section (struct objfile *objfile,
4402 const char *filename,
4403 struct dwarf2_section_info *section,
4404 mapped_debug_names &map)
4405 {
4406 if (section->empty ())
4407 return false;
4408
4409 /* Older elfutils strip versions could keep the section in the main
4410 executable while splitting it for the separate debug info file. */
4411 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4412 return false;
4413
4414 section->read (objfile);
4415
4416 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4417
4418 const gdb_byte *addr = section->buffer;
4419
4420 bfd *const abfd = section->get_bfd_owner ();
4421
4422 unsigned int bytes_read;
4423 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4424 addr += bytes_read;
4425
4426 map.dwarf5_is_dwarf64 = bytes_read != 4;
4427 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4428 if (bytes_read + length != section->size)
4429 {
4430 /* There may be multiple per-CU indices. */
4431 warning (_("Section .debug_names in %s length %s does not match "
4432 "section length %s, ignoring .debug_names."),
4433 filename, plongest (bytes_read + length),
4434 pulongest (section->size));
4435 return false;
4436 }
4437
4438 /* The version number. */
4439 uint16_t version = read_2_bytes (abfd, addr);
4440 addr += 2;
4441 if (version != 5)
4442 {
4443 warning (_("Section .debug_names in %s has unsupported version %d, "
4444 "ignoring .debug_names."),
4445 filename, version);
4446 return false;
4447 }
4448
4449 /* Padding. */
4450 uint16_t padding = read_2_bytes (abfd, addr);
4451 addr += 2;
4452 if (padding != 0)
4453 {
4454 warning (_("Section .debug_names in %s has unsupported padding %d, "
4455 "ignoring .debug_names."),
4456 filename, padding);
4457 return false;
4458 }
4459
4460 /* comp_unit_count - The number of CUs in the CU list. */
4461 map.cu_count = read_4_bytes (abfd, addr);
4462 addr += 4;
4463
4464 /* local_type_unit_count - The number of TUs in the local TU
4465 list. */
4466 map.tu_count = read_4_bytes (abfd, addr);
4467 addr += 4;
4468
4469 /* foreign_type_unit_count - The number of TUs in the foreign TU
4470 list. */
4471 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4472 addr += 4;
4473 if (foreign_tu_count != 0)
4474 {
4475 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4476 "ignoring .debug_names."),
4477 filename, static_cast<unsigned long> (foreign_tu_count));
4478 return false;
4479 }
4480
4481 /* bucket_count - The number of hash buckets in the hash lookup
4482 table. */
4483 map.bucket_count = read_4_bytes (abfd, addr);
4484 addr += 4;
4485
4486 /* name_count - The number of unique names in the index. */
4487 map.name_count = read_4_bytes (abfd, addr);
4488 addr += 4;
4489
4490 /* abbrev_table_size - The size in bytes of the abbreviations
4491 table. */
4492 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4493 addr += 4;
4494
4495 /* augmentation_string_size - The size in bytes of the augmentation
4496 string. This value is rounded up to a multiple of 4. */
4497 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4498 addr += 4;
4499 map.augmentation_is_gdb = ((augmentation_string_size
4500 == sizeof (dwarf5_augmentation))
4501 && memcmp (addr, dwarf5_augmentation,
4502 sizeof (dwarf5_augmentation)) == 0);
4503 augmentation_string_size += (-augmentation_string_size) & 3;
4504 addr += augmentation_string_size;
4505
4506 /* List of CUs */
4507 map.cu_table_reordered = addr;
4508 addr += map.cu_count * map.offset_size;
4509
4510 /* List of Local TUs */
4511 map.tu_table_reordered = addr;
4512 addr += map.tu_count * map.offset_size;
4513
4514 /* Hash Lookup Table */
4515 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4516 addr += map.bucket_count * 4;
4517 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4518 addr += map.name_count * 4;
4519
4520 /* Name Table */
4521 map.name_table_string_offs_reordered = addr;
4522 addr += map.name_count * map.offset_size;
4523 map.name_table_entry_offs_reordered = addr;
4524 addr += map.name_count * map.offset_size;
4525
4526 const gdb_byte *abbrev_table_start = addr;
4527 for (;;)
4528 {
4529 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4530 addr += bytes_read;
4531 if (index_num == 0)
4532 break;
4533
4534 const auto insertpair
4535 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4536 if (!insertpair.second)
4537 {
4538 warning (_("Section .debug_names in %s has duplicate index %s, "
4539 "ignoring .debug_names."),
4540 filename, pulongest (index_num));
4541 return false;
4542 }
4543 mapped_debug_names::index_val &indexval = insertpair.first->second;
4544 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4545 addr += bytes_read;
4546
4547 for (;;)
4548 {
4549 mapped_debug_names::index_val::attr attr;
4550 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4551 addr += bytes_read;
4552 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4553 addr += bytes_read;
4554 if (attr.form == DW_FORM_implicit_const)
4555 {
4556 attr.implicit_const = read_signed_leb128 (abfd, addr,
4557 &bytes_read);
4558 addr += bytes_read;
4559 }
4560 if (attr.dw_idx == 0 && attr.form == 0)
4561 break;
4562 indexval.attr_vec.push_back (std::move (attr));
4563 }
4564 }
4565 if (addr != abbrev_table_start + abbrev_table_size)
4566 {
4567 warning (_("Section .debug_names in %s has abbreviation_table "
4568 "of size %s vs. written as %u, ignoring .debug_names."),
4569 filename, plongest (addr - abbrev_table_start),
4570 abbrev_table_size);
4571 return false;
4572 }
4573 map.entry_pool = addr;
4574
4575 return true;
4576 }
4577
4578 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4579 list. */
4580
4581 static void
4582 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4583 const mapped_debug_names &map,
4584 dwarf2_section_info &section,
4585 bool is_dwz)
4586 {
4587 if (!map.augmentation_is_gdb)
4588 {
4589 for (uint32_t i = 0; i < map.cu_count; ++i)
4590 {
4591 sect_offset sect_off
4592 = (sect_offset) (extract_unsigned_integer
4593 (map.cu_table_reordered + i * map.offset_size,
4594 map.offset_size,
4595 map.dwarf5_byte_order));
4596 /* We don't know the length of the CU, because the CU list in a
4597 .debug_names index can be incomplete, so we can't use the start
4598 of the next CU as end of this CU. We create the CUs here with
4599 length 0, and in cutu_reader::cutu_reader we'll fill in the
4600 actual length. */
4601 dwarf2_per_cu_data_up per_cu
4602 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4603 sect_off, 0);
4604 per_bfd->all_comp_units.push_back (std::move (per_cu));
4605 }
4606 return;
4607 }
4608
4609 sect_offset sect_off_prev;
4610 for (uint32_t i = 0; i <= map.cu_count; ++i)
4611 {
4612 sect_offset sect_off_next;
4613 if (i < map.cu_count)
4614 {
4615 sect_off_next
4616 = (sect_offset) (extract_unsigned_integer
4617 (map.cu_table_reordered + i * map.offset_size,
4618 map.offset_size,
4619 map.dwarf5_byte_order));
4620 }
4621 else
4622 sect_off_next = (sect_offset) section.size;
4623 if (i >= 1)
4624 {
4625 const ULONGEST length = sect_off_next - sect_off_prev;
4626 dwarf2_per_cu_data_up per_cu
4627 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4628 sect_off_prev, length);
4629 per_bfd->all_comp_units.push_back (std::move (per_cu));
4630 }
4631 sect_off_prev = sect_off_next;
4632 }
4633 }
4634
4635 /* Read the CU list from the mapped index, and use it to create all
4636 the CU objects for this dwarf2_per_objfile. */
4637
4638 static void
4639 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4640 const mapped_debug_names &map,
4641 const mapped_debug_names &dwz_map)
4642 {
4643 gdb_assert (per_bfd->all_comp_units.empty ());
4644 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4645
4646 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4647 false /* is_dwz */);
4648
4649 if (dwz_map.cu_count == 0)
4650 return;
4651
4652 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4653 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4654 true /* is_dwz */);
4655 }
4656
4657 /* Read .debug_names. If everything went ok, initialize the "quick"
4658 elements of all the CUs and return true. Otherwise, return false. */
4659
4660 static bool
4661 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4662 {
4663 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4664 mapped_debug_names dwz_map;
4665 struct objfile *objfile = per_objfile->objfile;
4666 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4667
4668 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4669 &per_bfd->debug_names, *map))
4670 return false;
4671
4672 /* Don't use the index if it's empty. */
4673 if (map->name_count == 0)
4674 return false;
4675
4676 /* If there is a .dwz file, read it so we can get its CU list as
4677 well. */
4678 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4679 if (dwz != NULL)
4680 {
4681 if (!read_debug_names_from_section (objfile,
4682 bfd_get_filename (dwz->dwz_bfd.get ()),
4683 &dwz->debug_names, dwz_map))
4684 {
4685 warning (_("could not read '.debug_names' section from %s; skipping"),
4686 bfd_get_filename (dwz->dwz_bfd.get ()));
4687 return false;
4688 }
4689 }
4690
4691 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4692
4693 if (map->tu_count != 0)
4694 {
4695 /* We can only handle a single .debug_types when we have an
4696 index. */
4697 if (per_bfd->types.size () != 1)
4698 return false;
4699
4700 dwarf2_section_info *section = &per_bfd->types[0];
4701
4702 create_signatured_type_table_from_debug_names
4703 (per_objfile, *map, section, &per_bfd->abbrev);
4704 }
4705
4706 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4707
4708 per_bfd->index_table = std::move (map);
4709 per_bfd->quick_file_names_table =
4710 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4711
4712 return true;
4713 }
4714
4715 /* Type used to manage iterating over all CUs looking for a symbol for
4716 .debug_names. */
4717
4718 class dw2_debug_names_iterator
4719 {
4720 public:
4721 dw2_debug_names_iterator (const mapped_debug_names &map,
4722 block_search_flags block_index,
4723 domain_enum domain,
4724 const char *name, dwarf2_per_objfile *per_objfile)
4725 : m_map (map), m_block_index (block_index), m_domain (domain),
4726 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4727 m_per_objfile (per_objfile)
4728 {}
4729
4730 dw2_debug_names_iterator (const mapped_debug_names &map,
4731 search_domain search, uint32_t namei,
4732 dwarf2_per_objfile *per_objfile,
4733 domain_enum domain = UNDEF_DOMAIN)
4734 : m_map (map),
4735 m_domain (domain),
4736 m_search (search),
4737 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4738 m_per_objfile (per_objfile)
4739 {}
4740
4741 dw2_debug_names_iterator (const mapped_debug_names &map,
4742 block_search_flags block_index, domain_enum domain,
4743 uint32_t namei, dwarf2_per_objfile *per_objfile)
4744 : m_map (map), m_block_index (block_index), m_domain (domain),
4745 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4746 m_per_objfile (per_objfile)
4747 {}
4748
4749 /* Return the next matching CU or NULL if there are no more. */
4750 dwarf2_per_cu_data *next ();
4751
4752 private:
4753 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4754 const char *name,
4755 dwarf2_per_objfile *per_objfile);
4756 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4757 uint32_t namei,
4758 dwarf2_per_objfile *per_objfile);
4759
4760 /* The internalized form of .debug_names. */
4761 const mapped_debug_names &m_map;
4762
4763 /* Restrict the search to these blocks. */
4764 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4765 | SEARCH_STATIC_BLOCK);
4766
4767 /* The kind of symbol we're looking for. */
4768 const domain_enum m_domain = UNDEF_DOMAIN;
4769 const search_domain m_search = ALL_DOMAIN;
4770
4771 /* The list of CUs from the index entry of the symbol, or NULL if
4772 not found. */
4773 const gdb_byte *m_addr;
4774
4775 dwarf2_per_objfile *m_per_objfile;
4776 };
4777
4778 const char *
4779 mapped_debug_names::namei_to_name
4780 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4781 {
4782 const ULONGEST namei_string_offs
4783 = extract_unsigned_integer ((name_table_string_offs_reordered
4784 + namei * offset_size),
4785 offset_size,
4786 dwarf5_byte_order);
4787 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4788 }
4789
4790 /* Find a slot in .debug_names for the object named NAME. If NAME is
4791 found, return pointer to its pool data. If NAME cannot be found,
4792 return NULL. */
4793
4794 const gdb_byte *
4795 dw2_debug_names_iterator::find_vec_in_debug_names
4796 (const mapped_debug_names &map, const char *name,
4797 dwarf2_per_objfile *per_objfile)
4798 {
4799 int (*cmp) (const char *, const char *);
4800
4801 gdb::unique_xmalloc_ptr<char> without_params;
4802 if (current_language->la_language == language_cplus
4803 || current_language->la_language == language_fortran
4804 || current_language->la_language == language_d)
4805 {
4806 /* NAME is already canonical. Drop any qualifiers as
4807 .debug_names does not contain any. */
4808
4809 if (strchr (name, '(') != NULL)
4810 {
4811 without_params = cp_remove_params (name);
4812 if (without_params != NULL)
4813 name = without_params.get ();
4814 }
4815 }
4816
4817 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4818
4819 const uint32_t full_hash = dwarf5_djb_hash (name);
4820 uint32_t namei
4821 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4822 (map.bucket_table_reordered
4823 + (full_hash % map.bucket_count)), 4,
4824 map.dwarf5_byte_order);
4825 if (namei == 0)
4826 return NULL;
4827 --namei;
4828 if (namei >= map.name_count)
4829 {
4830 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4831 "[in module %s]"),
4832 namei, map.name_count,
4833 objfile_name (per_objfile->objfile));
4834 return NULL;
4835 }
4836
4837 for (;;)
4838 {
4839 const uint32_t namei_full_hash
4840 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4841 (map.hash_table_reordered + namei), 4,
4842 map.dwarf5_byte_order);
4843 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4844 return NULL;
4845
4846 if (full_hash == namei_full_hash)
4847 {
4848 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4849
4850 #if 0 /* An expensive sanity check. */
4851 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4852 {
4853 complaint (_("Wrong .debug_names hash for string at index %u "
4854 "[in module %s]"),
4855 namei, objfile_name (dwarf2_per_objfile->objfile));
4856 return NULL;
4857 }
4858 #endif
4859
4860 if (cmp (namei_string, name) == 0)
4861 {
4862 const ULONGEST namei_entry_offs
4863 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4864 + namei * map.offset_size),
4865 map.offset_size, map.dwarf5_byte_order);
4866 return map.entry_pool + namei_entry_offs;
4867 }
4868 }
4869
4870 ++namei;
4871 if (namei >= map.name_count)
4872 return NULL;
4873 }
4874 }
4875
4876 const gdb_byte *
4877 dw2_debug_names_iterator::find_vec_in_debug_names
4878 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4879 {
4880 if (namei >= map.name_count)
4881 {
4882 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4883 "[in module %s]"),
4884 namei, map.name_count,
4885 objfile_name (per_objfile->objfile));
4886 return NULL;
4887 }
4888
4889 const ULONGEST namei_entry_offs
4890 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4891 + namei * map.offset_size),
4892 map.offset_size, map.dwarf5_byte_order);
4893 return map.entry_pool + namei_entry_offs;
4894 }
4895
4896 /* See dw2_debug_names_iterator. */
4897
4898 dwarf2_per_cu_data *
4899 dw2_debug_names_iterator::next ()
4900 {
4901 if (m_addr == NULL)
4902 return NULL;
4903
4904 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4905 struct objfile *objfile = m_per_objfile->objfile;
4906 bfd *const abfd = objfile->obfd;
4907
4908 again:
4909
4910 unsigned int bytes_read;
4911 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4912 m_addr += bytes_read;
4913 if (abbrev == 0)
4914 return NULL;
4915
4916 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4917 if (indexval_it == m_map.abbrev_map.cend ())
4918 {
4919 complaint (_("Wrong .debug_names undefined abbrev code %s "
4920 "[in module %s]"),
4921 pulongest (abbrev), objfile_name (objfile));
4922 return NULL;
4923 }
4924 const mapped_debug_names::index_val &indexval = indexval_it->second;
4925 enum class symbol_linkage {
4926 unknown,
4927 static_,
4928 extern_,
4929 } symbol_linkage_ = symbol_linkage::unknown;
4930 dwarf2_per_cu_data *per_cu = NULL;
4931 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4932 {
4933 ULONGEST ull;
4934 switch (attr.form)
4935 {
4936 case DW_FORM_implicit_const:
4937 ull = attr.implicit_const;
4938 break;
4939 case DW_FORM_flag_present:
4940 ull = 1;
4941 break;
4942 case DW_FORM_udata:
4943 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4944 m_addr += bytes_read;
4945 break;
4946 case DW_FORM_ref4:
4947 ull = read_4_bytes (abfd, m_addr);
4948 m_addr += 4;
4949 break;
4950 case DW_FORM_ref8:
4951 ull = read_8_bytes (abfd, m_addr);
4952 m_addr += 8;
4953 break;
4954 case DW_FORM_ref_sig8:
4955 ull = read_8_bytes (abfd, m_addr);
4956 m_addr += 8;
4957 break;
4958 default:
4959 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4960 dwarf_form_name (attr.form),
4961 objfile_name (objfile));
4962 return NULL;
4963 }
4964 switch (attr.dw_idx)
4965 {
4966 case DW_IDX_compile_unit:
4967 /* Don't crash on bad data. */
4968 if (ull >= per_bfd->all_comp_units.size ())
4969 {
4970 complaint (_(".debug_names entry has bad CU index %s"
4971 " [in module %s]"),
4972 pulongest (ull),
4973 objfile_name (objfile));
4974 continue;
4975 }
4976 per_cu = per_bfd->get_cu (ull);
4977 break;
4978 case DW_IDX_type_unit:
4979 /* Don't crash on bad data. */
4980 if (ull >= per_bfd->tu_stats.nr_tus)
4981 {
4982 complaint (_(".debug_names entry has bad TU index %s"
4983 " [in module %s]"),
4984 pulongest (ull),
4985 objfile_name (objfile));
4986 continue;
4987 }
4988 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
4989 break;
4990 case DW_IDX_die_offset:
4991 /* In a per-CU index (as opposed to a per-module index), index
4992 entries without CU attribute implicitly refer to the single CU. */
4993 if (per_cu == NULL)
4994 per_cu = per_bfd->get_cu (0);
4995 break;
4996 case DW_IDX_GNU_internal:
4997 if (!m_map.augmentation_is_gdb)
4998 break;
4999 symbol_linkage_ = symbol_linkage::static_;
5000 break;
5001 case DW_IDX_GNU_external:
5002 if (!m_map.augmentation_is_gdb)
5003 break;
5004 symbol_linkage_ = symbol_linkage::extern_;
5005 break;
5006 }
5007 }
5008
5009 /* Skip if already read in. */
5010 if (m_per_objfile->symtab_set_p (per_cu))
5011 goto again;
5012
5013 /* Check static vs global. */
5014 if (symbol_linkage_ != symbol_linkage::unknown)
5015 {
5016 if (symbol_linkage_ == symbol_linkage::static_)
5017 {
5018 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5019 goto again;
5020 }
5021 else
5022 {
5023 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5024 goto again;
5025 }
5026 }
5027
5028 /* Match dw2_symtab_iter_next, symbol_kind
5029 and debug_names::psymbol_tag. */
5030 switch (m_domain)
5031 {
5032 case VAR_DOMAIN:
5033 switch (indexval.dwarf_tag)
5034 {
5035 case DW_TAG_variable:
5036 case DW_TAG_subprogram:
5037 /* Some types are also in VAR_DOMAIN. */
5038 case DW_TAG_typedef:
5039 case DW_TAG_structure_type:
5040 break;
5041 default:
5042 goto again;
5043 }
5044 break;
5045 case STRUCT_DOMAIN:
5046 switch (indexval.dwarf_tag)
5047 {
5048 case DW_TAG_typedef:
5049 case DW_TAG_structure_type:
5050 break;
5051 default:
5052 goto again;
5053 }
5054 break;
5055 case LABEL_DOMAIN:
5056 switch (indexval.dwarf_tag)
5057 {
5058 case 0:
5059 case DW_TAG_variable:
5060 break;
5061 default:
5062 goto again;
5063 }
5064 break;
5065 case MODULE_DOMAIN:
5066 switch (indexval.dwarf_tag)
5067 {
5068 case DW_TAG_module:
5069 break;
5070 default:
5071 goto again;
5072 }
5073 break;
5074 default:
5075 break;
5076 }
5077
5078 /* Match dw2_expand_symtabs_matching, symbol_kind and
5079 debug_names::psymbol_tag. */
5080 switch (m_search)
5081 {
5082 case VARIABLES_DOMAIN:
5083 switch (indexval.dwarf_tag)
5084 {
5085 case DW_TAG_variable:
5086 break;
5087 default:
5088 goto again;
5089 }
5090 break;
5091 case FUNCTIONS_DOMAIN:
5092 switch (indexval.dwarf_tag)
5093 {
5094 case DW_TAG_subprogram:
5095 break;
5096 default:
5097 goto again;
5098 }
5099 break;
5100 case TYPES_DOMAIN:
5101 switch (indexval.dwarf_tag)
5102 {
5103 case DW_TAG_typedef:
5104 case DW_TAG_structure_type:
5105 break;
5106 default:
5107 goto again;
5108 }
5109 break;
5110 case MODULES_DOMAIN:
5111 switch (indexval.dwarf_tag)
5112 {
5113 case DW_TAG_module:
5114 break;
5115 default:
5116 goto again;
5117 }
5118 default:
5119 break;
5120 }
5121
5122 return per_cu;
5123 }
5124
5125 /* This dumps minimal information about .debug_names. It is called
5126 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5127 uses this to verify that .debug_names has been loaded. */
5128
5129 void
5130 dwarf2_debug_names_index::dump (struct objfile *objfile)
5131 {
5132 gdb_printf (".debug_names: exists\n");
5133 }
5134
5135 void
5136 dwarf2_debug_names_index::expand_matching_symbols
5137 (struct objfile *objfile,
5138 const lookup_name_info &name, domain_enum domain,
5139 int global,
5140 symbol_compare_ftype *ordered_compare)
5141 {
5142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5143
5144 mapped_debug_names &map
5145 = (static_cast<mapped_debug_names &>
5146 (*per_objfile->per_bfd->index_table.get ()));
5147 const block_search_flags block_flags
5148 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5149
5150 const char *match_name = name.ada ().lookup_name ().c_str ();
5151 auto matcher = [&] (const char *symname)
5152 {
5153 if (ordered_compare == nullptr)
5154 return true;
5155 return ordered_compare (symname, match_name) == 0;
5156 };
5157
5158 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5159 [&] (offset_type namei)
5160 {
5161 /* The name was matched, now expand corresponding CUs that were
5162 marked. */
5163 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5164 per_objfile);
5165
5166 struct dwarf2_per_cu_data *per_cu;
5167 while ((per_cu = iter.next ()) != NULL)
5168 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5169 nullptr);
5170 return true;
5171 }, per_objfile);
5172 }
5173
5174 bool
5175 dwarf2_debug_names_index::expand_symtabs_matching
5176 (struct objfile *objfile,
5177 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5178 const lookup_name_info *lookup_name,
5179 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5180 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5181 block_search_flags search_flags,
5182 domain_enum domain,
5183 enum search_domain kind)
5184 {
5185 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5186
5187 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5188
5189 /* This invariant is documented in quick-functions.h. */
5190 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5191 if (lookup_name == nullptr)
5192 {
5193 for (dwarf2_per_cu_data *per_cu
5194 : all_comp_units_range (per_objfile->per_bfd))
5195 {
5196 QUIT;
5197
5198 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5199 file_matcher,
5200 expansion_notify))
5201 return false;
5202 }
5203 return true;
5204 }
5205
5206 mapped_debug_names &map
5207 = (static_cast<mapped_debug_names &>
5208 (*per_objfile->per_bfd->index_table.get ()));
5209
5210 bool result
5211 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5212 symbol_matcher,
5213 [&] (offset_type namei)
5214 {
5215 /* The name was matched, now expand corresponding CUs that were
5216 marked. */
5217 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5218
5219 struct dwarf2_per_cu_data *per_cu;
5220 while ((per_cu = iter.next ()) != NULL)
5221 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5222 file_matcher,
5223 expansion_notify))
5224 return false;
5225 return true;
5226 }, per_objfile);
5227
5228 return result;
5229 }
5230
5231 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5232 to either a dwarf2_per_bfd or dwz_file object. */
5233
5234 template <typename T>
5235 static gdb::array_view<const gdb_byte>
5236 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5237 {
5238 dwarf2_section_info *section = &section_owner->gdb_index;
5239
5240 if (section->empty ())
5241 return {};
5242
5243 /* Older elfutils strip versions could keep the section in the main
5244 executable while splitting it for the separate debug info file. */
5245 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5246 return {};
5247
5248 section->read (obj);
5249
5250 /* dwarf2_section_info::size is a bfd_size_type, while
5251 gdb::array_view works with size_t. On 32-bit hosts, with
5252 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5253 is 32-bit. So we need an explicit narrowing conversion here.
5254 This is fine, because it's impossible to allocate or mmap an
5255 array/buffer larger than what size_t can represent. */
5256 return gdb::make_array_view (section->buffer, section->size);
5257 }
5258
5259 /* Lookup the index cache for the contents of the index associated to
5260 DWARF2_OBJ. */
5261
5262 static gdb::array_view<const gdb_byte>
5263 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5264 {
5265 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5266 if (build_id == nullptr)
5267 return {};
5268
5269 return global_index_cache.lookup_gdb_index (build_id,
5270 &dwarf2_per_bfd->index_cache_res);
5271 }
5272
5273 /* Same as the above, but for DWZ. */
5274
5275 static gdb::array_view<const gdb_byte>
5276 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5277 {
5278 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5279 if (build_id == nullptr)
5280 return {};
5281
5282 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5283 }
5284
5285 static quick_symbol_functions_up make_cooked_index_funcs ();
5286
5287 /* See dwarf2/public.h. */
5288
5289 void
5290 dwarf2_initialize_objfile (struct objfile *objfile)
5291 {
5292 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5293 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5294
5295 dwarf_read_debug_printf ("called");
5296
5297 /* If we're about to read full symbols, don't bother with the
5298 indices. In this case we also don't care if some other debug
5299 format is making psymtabs, because they are all about to be
5300 expanded anyway. */
5301 if ((objfile->flags & OBJF_READNOW))
5302 {
5303 dwarf_read_debug_printf ("readnow requested");
5304
5305 create_all_comp_units (per_objfile);
5306 per_bfd->quick_file_names_table
5307 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5308
5309 objfile->qf.emplace_front (new readnow_functions);
5310 return;
5311 }
5312
5313 /* Was a GDB index already read when we processed an objfile sharing
5314 PER_BFD? */
5315 if (per_bfd->index_table != nullptr)
5316 {
5317 dwarf_read_debug_printf ("re-using symbols");
5318 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5319 return;
5320 }
5321
5322 if (dwarf2_read_debug_names (per_objfile))
5323 {
5324 dwarf_read_debug_printf ("found debug names");
5325 objfile->qf.push_front
5326 (per_bfd->index_table->make_quick_functions ());
5327 return;
5328 }
5329
5330 if (dwarf2_read_gdb_index (per_objfile,
5331 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5332 get_gdb_index_contents_from_section<dwz_file>))
5333 {
5334 dwarf_read_debug_printf ("found gdb index from file");
5335 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5336 return;
5337 }
5338
5339 /* ... otherwise, try to find the index in the index cache. */
5340 if (dwarf2_read_gdb_index (per_objfile,
5341 get_gdb_index_contents_from_cache,
5342 get_gdb_index_contents_from_cache_dwz))
5343 {
5344 dwarf_read_debug_printf ("found gdb index from cache");
5345 global_index_cache.hit ();
5346 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5347 return;
5348 }
5349
5350 global_index_cache.miss ();
5351 objfile->qf.push_front (make_cooked_index_funcs ());
5352 }
5353
5354 \f
5355
5356 /* Build a partial symbol table. */
5357
5358 static void
5359 dwarf2_build_psymtabs (struct objfile *objfile)
5360 {
5361 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5362
5363 if (per_objfile->per_bfd->index_table != nullptr)
5364 return;
5365
5366 try
5367 {
5368 dwarf2_build_psymtabs_hard (per_objfile);
5369
5370 /* (maybe) store an index in the cache. */
5371 global_index_cache.store (per_objfile);
5372 }
5373 catch (const gdb_exception_error &except)
5374 {
5375 exception_print (gdb_stderr, except);
5376 }
5377 }
5378
5379 /* Find the base address of the compilation unit for range lists and
5380 location lists. It will normally be specified by DW_AT_low_pc.
5381 In DWARF-3 draft 4, the base address could be overridden by
5382 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5383 compilation units with discontinuous ranges. */
5384
5385 static void
5386 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5387 {
5388 struct attribute *attr;
5389
5390 cu->base_address.reset ();
5391
5392 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5393 if (attr != nullptr)
5394 cu->base_address = attr->as_address ();
5395 else
5396 {
5397 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5398 if (attr != nullptr)
5399 cu->base_address = attr->as_address ();
5400 }
5401 }
5402
5403 /* Helper function that returns the proper abbrev section for
5404 THIS_CU. */
5405
5406 static struct dwarf2_section_info *
5407 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5408 {
5409 struct dwarf2_section_info *abbrev;
5410 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5411
5412 if (this_cu->is_dwz)
5413 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5414 else
5415 abbrev = &per_bfd->abbrev;
5416
5417 return abbrev;
5418 }
5419
5420 /* Fetch the abbreviation table offset from a comp or type unit header. */
5421
5422 static sect_offset
5423 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5424 struct dwarf2_section_info *section,
5425 sect_offset sect_off)
5426 {
5427 bfd *abfd = section->get_bfd_owner ();
5428 const gdb_byte *info_ptr;
5429 unsigned int initial_length_size, offset_size;
5430 uint16_t version;
5431
5432 section->read (per_objfile->objfile);
5433 info_ptr = section->buffer + to_underlying (sect_off);
5434 read_initial_length (abfd, info_ptr, &initial_length_size);
5435 offset_size = initial_length_size == 4 ? 4 : 8;
5436 info_ptr += initial_length_size;
5437
5438 version = read_2_bytes (abfd, info_ptr);
5439 info_ptr += 2;
5440 if (version >= 5)
5441 {
5442 /* Skip unit type and address size. */
5443 info_ptr += 2;
5444 }
5445
5446 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5447 }
5448
5449 static hashval_t
5450 hash_signatured_type (const void *item)
5451 {
5452 const struct signatured_type *sig_type
5453 = (const struct signatured_type *) item;
5454
5455 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5456 return sig_type->signature;
5457 }
5458
5459 static int
5460 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5461 {
5462 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5463 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5464
5465 return lhs->signature == rhs->signature;
5466 }
5467
5468 /* Allocate a hash table for signatured types. */
5469
5470 static htab_up
5471 allocate_signatured_type_table ()
5472 {
5473 return htab_up (htab_create_alloc (41,
5474 hash_signatured_type,
5475 eq_signatured_type,
5476 NULL, xcalloc, xfree));
5477 }
5478
5479 /* A helper for create_debug_types_hash_table. Read types from SECTION
5480 and fill them into TYPES_HTAB. It will process only type units,
5481 therefore DW_UT_type. */
5482
5483 static void
5484 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5485 struct dwo_file *dwo_file,
5486 dwarf2_section_info *section, htab_up &types_htab,
5487 rcuh_kind section_kind)
5488 {
5489 struct objfile *objfile = per_objfile->objfile;
5490 struct dwarf2_section_info *abbrev_section;
5491 bfd *abfd;
5492 const gdb_byte *info_ptr, *end_ptr;
5493
5494 abbrev_section = &dwo_file->sections.abbrev;
5495
5496 dwarf_read_debug_printf ("Reading %s for %s",
5497 section->get_name (),
5498 abbrev_section->get_file_name ());
5499
5500 section->read (objfile);
5501 info_ptr = section->buffer;
5502
5503 if (info_ptr == NULL)
5504 return;
5505
5506 /* We can't set abfd until now because the section may be empty or
5507 not present, in which case the bfd is unknown. */
5508 abfd = section->get_bfd_owner ();
5509
5510 /* We don't use cutu_reader here because we don't need to read
5511 any dies: the signature is in the header. */
5512
5513 end_ptr = info_ptr + section->size;
5514 while (info_ptr < end_ptr)
5515 {
5516 signatured_type_up sig_type;
5517 struct dwo_unit *dwo_tu;
5518 void **slot;
5519 const gdb_byte *ptr = info_ptr;
5520 struct comp_unit_head header;
5521 unsigned int length;
5522
5523 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5524
5525 /* Initialize it due to a false compiler warning. */
5526 header.signature = -1;
5527 header.type_cu_offset_in_tu = (cu_offset) -1;
5528
5529 /* We need to read the type's signature in order to build the hash
5530 table, but we don't need anything else just yet. */
5531
5532 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5533 abbrev_section, ptr, section_kind);
5534
5535 length = header.get_length ();
5536
5537 /* Skip dummy type units. */
5538 if (ptr >= info_ptr + length
5539 || peek_abbrev_code (abfd, ptr) == 0
5540 || (header.unit_type != DW_UT_type
5541 && header.unit_type != DW_UT_split_type))
5542 {
5543 info_ptr += length;
5544 continue;
5545 }
5546
5547 if (types_htab == NULL)
5548 types_htab = allocate_dwo_unit_table ();
5549
5550 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5551 dwo_tu->dwo_file = dwo_file;
5552 dwo_tu->signature = header.signature;
5553 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5554 dwo_tu->section = section;
5555 dwo_tu->sect_off = sect_off;
5556 dwo_tu->length = length;
5557
5558 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5559 gdb_assert (slot != NULL);
5560 if (*slot != NULL)
5561 complaint (_("debug type entry at offset %s is duplicate to"
5562 " the entry at offset %s, signature %s"),
5563 sect_offset_str (sect_off),
5564 sect_offset_str (dwo_tu->sect_off),
5565 hex_string (header.signature));
5566 *slot = dwo_tu;
5567
5568 dwarf_read_debug_printf_v (" offset %s, signature %s",
5569 sect_offset_str (sect_off),
5570 hex_string (header.signature));
5571
5572 info_ptr += length;
5573 }
5574 }
5575
5576 /* Create the hash table of all entries in the .debug_types
5577 (or .debug_types.dwo) section(s).
5578 DWO_FILE is a pointer to the DWO file object.
5579
5580 The result is a pointer to the hash table or NULL if there are no types.
5581
5582 Note: This function processes DWO files only, not DWP files. */
5583
5584 static void
5585 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5586 struct dwo_file *dwo_file,
5587 gdb::array_view<dwarf2_section_info> type_sections,
5588 htab_up &types_htab)
5589 {
5590 for (dwarf2_section_info &section : type_sections)
5591 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5592 rcuh_kind::TYPE);
5593 }
5594
5595 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5596 If SLOT is non-NULL, it is the entry to use in the hash table.
5597 Otherwise we find one. */
5598
5599 static struct signatured_type *
5600 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5601 {
5602 if (per_objfile->per_bfd->all_comp_units.size ()
5603 == per_objfile->per_bfd->all_comp_units.capacity ())
5604 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5605
5606 signatured_type_up sig_type_holder
5607 = per_objfile->per_bfd->allocate_signatured_type (sig);
5608 signatured_type *sig_type = sig_type_holder.get ();
5609
5610 per_objfile->per_bfd->all_comp_units.emplace_back
5611 (sig_type_holder.release ());
5612
5613 if (slot == NULL)
5614 {
5615 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5616 sig_type, INSERT);
5617 }
5618 gdb_assert (*slot == NULL);
5619 *slot = sig_type;
5620 /* The rest of sig_type must be filled in by the caller. */
5621 return sig_type;
5622 }
5623
5624 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5625 Fill in SIG_ENTRY with DWO_ENTRY. */
5626
5627 static void
5628 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5629 struct signatured_type *sig_entry,
5630 struct dwo_unit *dwo_entry)
5631 {
5632 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5633
5634 /* Make sure we're not clobbering something we don't expect to. */
5635 gdb_assert (! sig_entry->queued);
5636 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5637 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5638 gdb_assert (sig_entry->signature == dwo_entry->signature);
5639 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5640 || (to_underlying (sig_entry->type_offset_in_section)
5641 == to_underlying (dwo_entry->type_offset_in_tu)));
5642 gdb_assert (sig_entry->type_unit_group == NULL);
5643 gdb_assert (sig_entry->dwo_unit == NULL
5644 || sig_entry->dwo_unit == dwo_entry);
5645
5646 sig_entry->section = dwo_entry->section;
5647 sig_entry->sect_off = dwo_entry->sect_off;
5648 sig_entry->length = dwo_entry->length;
5649 sig_entry->reading_dwo_directly = 1;
5650 sig_entry->per_bfd = per_bfd;
5651 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5652 sig_entry->dwo_unit = dwo_entry;
5653 }
5654
5655 /* Subroutine of lookup_signatured_type.
5656 If we haven't read the TU yet, create the signatured_type data structure
5657 for a TU to be read in directly from a DWO file, bypassing the stub.
5658 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5659 using .gdb_index, then when reading a CU we want to stay in the DWO file
5660 containing that CU. Otherwise we could end up reading several other DWO
5661 files (due to comdat folding) to process the transitive closure of all the
5662 mentioned TUs, and that can be slow. The current DWO file will have every
5663 type signature that it needs.
5664 We only do this for .gdb_index because in the psymtab case we already have
5665 to read all the DWOs to build the type unit groups. */
5666
5667 static struct signatured_type *
5668 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5669 {
5670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5671 struct dwo_file *dwo_file;
5672 struct dwo_unit find_dwo_entry, *dwo_entry;
5673 void **slot;
5674
5675 gdb_assert (cu->dwo_unit);
5676
5677 /* If TU skeletons have been removed then we may not have read in any
5678 TUs yet. */
5679 if (per_objfile->per_bfd->signatured_types == NULL)
5680 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5681
5682 /* We only ever need to read in one copy of a signatured type.
5683 Use the global signatured_types array to do our own comdat-folding
5684 of types. If this is the first time we're reading this TU, and
5685 the TU has an entry in .gdb_index, replace the recorded data from
5686 .gdb_index with this TU. */
5687
5688 signatured_type find_sig_entry (sig);
5689 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5690 &find_sig_entry, INSERT);
5691 signatured_type *sig_entry = (struct signatured_type *) *slot;
5692
5693 /* We can get here with the TU already read, *or* in the process of being
5694 read. Don't reassign the global entry to point to this DWO if that's
5695 the case. Also note that if the TU is already being read, it may not
5696 have come from a DWO, the program may be a mix of Fission-compiled
5697 code and non-Fission-compiled code. */
5698
5699 /* Have we already tried to read this TU?
5700 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5701 needn't exist in the global table yet). */
5702 if (sig_entry != NULL && sig_entry->tu_read)
5703 return sig_entry;
5704
5705 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5706 dwo_unit of the TU itself. */
5707 dwo_file = cu->dwo_unit->dwo_file;
5708
5709 /* Ok, this is the first time we're reading this TU. */
5710 if (dwo_file->tus == NULL)
5711 return NULL;
5712 find_dwo_entry.signature = sig;
5713 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5714 &find_dwo_entry);
5715 if (dwo_entry == NULL)
5716 return NULL;
5717
5718 /* If the global table doesn't have an entry for this TU, add one. */
5719 if (sig_entry == NULL)
5720 sig_entry = add_type_unit (per_objfile, sig, slot);
5721
5722 if (sig_entry->dwo_unit == nullptr)
5723 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5724 sig_entry->tu_read = 1;
5725 return sig_entry;
5726 }
5727
5728 /* Subroutine of lookup_signatured_type.
5729 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5730 then try the DWP file. If the TU stub (skeleton) has been removed then
5731 it won't be in .gdb_index. */
5732
5733 static struct signatured_type *
5734 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5735 {
5736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5737 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5738 struct dwo_unit *dwo_entry;
5739 void **slot;
5740
5741 gdb_assert (cu->dwo_unit);
5742 gdb_assert (dwp_file != NULL);
5743
5744 /* If TU skeletons have been removed then we may not have read in any
5745 TUs yet. */
5746 if (per_objfile->per_bfd->signatured_types == NULL)
5747 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5748
5749 signatured_type find_sig_entry (sig);
5750 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5751 &find_sig_entry, INSERT);
5752 signatured_type *sig_entry = (struct signatured_type *) *slot;
5753
5754 /* Have we already tried to read this TU?
5755 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5756 needn't exist in the global table yet). */
5757 if (sig_entry != NULL)
5758 return sig_entry;
5759
5760 if (dwp_file->tus == NULL)
5761 return NULL;
5762 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5763 1 /* is_debug_types */);
5764 if (dwo_entry == NULL)
5765 return NULL;
5766
5767 sig_entry = add_type_unit (per_objfile, sig, slot);
5768 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5769
5770 return sig_entry;
5771 }
5772
5773 /* Lookup a signature based type for DW_FORM_ref_sig8.
5774 Returns NULL if signature SIG is not present in the table.
5775 It is up to the caller to complain about this. */
5776
5777 static struct signatured_type *
5778 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5779 {
5780 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5781
5782 if (cu->dwo_unit)
5783 {
5784 /* We're in a DWO/DWP file, and we're using .gdb_index.
5785 These cases require special processing. */
5786 if (get_dwp_file (per_objfile) == NULL)
5787 return lookup_dwo_signatured_type (cu, sig);
5788 else
5789 return lookup_dwp_signatured_type (cu, sig);
5790 }
5791 else
5792 {
5793 if (per_objfile->per_bfd->signatured_types == NULL)
5794 return NULL;
5795 signatured_type find_entry (sig);
5796 return ((struct signatured_type *)
5797 htab_find (per_objfile->per_bfd->signatured_types.get (),
5798 &find_entry));
5799 }
5800 }
5801
5802 /* Low level DIE reading support. */
5803
5804 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5805
5806 static void
5807 init_cu_die_reader (struct die_reader_specs *reader,
5808 struct dwarf2_cu *cu,
5809 struct dwarf2_section_info *section,
5810 struct dwo_file *dwo_file,
5811 struct abbrev_table *abbrev_table)
5812 {
5813 gdb_assert (section->readin && section->buffer != NULL);
5814 reader->abfd = section->get_bfd_owner ();
5815 reader->cu = cu;
5816 reader->dwo_file = dwo_file;
5817 reader->die_section = section;
5818 reader->buffer = section->buffer;
5819 reader->buffer_end = section->buffer + section->size;
5820 reader->abbrev_table = abbrev_table;
5821 }
5822
5823 /* Subroutine of cutu_reader to simplify it.
5824 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5825 There's just a lot of work to do, and cutu_reader is big enough
5826 already.
5827
5828 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5829 from it to the DIE in the DWO. If NULL we are skipping the stub.
5830 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5831 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5832 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5833 STUB_COMP_DIR may be non-NULL.
5834 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5835 are filled in with the info of the DIE from the DWO file.
5836 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5837 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5838 kept around for at least as long as *RESULT_READER.
5839
5840 The result is non-zero if a valid (non-dummy) DIE was found. */
5841
5842 static int
5843 read_cutu_die_from_dwo (dwarf2_cu *cu,
5844 struct dwo_unit *dwo_unit,
5845 struct die_info *stub_comp_unit_die,
5846 const char *stub_comp_dir,
5847 struct die_reader_specs *result_reader,
5848 const gdb_byte **result_info_ptr,
5849 struct die_info **result_comp_unit_die,
5850 abbrev_table_up *result_dwo_abbrev_table)
5851 {
5852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5853 dwarf2_per_cu_data *per_cu = cu->per_cu;
5854 struct objfile *objfile = per_objfile->objfile;
5855 bfd *abfd;
5856 const gdb_byte *begin_info_ptr, *info_ptr;
5857 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5858 int i,num_extra_attrs;
5859 struct dwarf2_section_info *dwo_abbrev_section;
5860 struct die_info *comp_unit_die;
5861
5862 /* At most one of these may be provided. */
5863 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5864
5865 /* These attributes aren't processed until later:
5866 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5867 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5868 referenced later. However, these attributes are found in the stub
5869 which we won't have later. In order to not impose this complication
5870 on the rest of the code, we read them here and copy them to the
5871 DWO CU/TU die. */
5872
5873 stmt_list = NULL;
5874 low_pc = NULL;
5875 high_pc = NULL;
5876 ranges = NULL;
5877 comp_dir = NULL;
5878
5879 if (stub_comp_unit_die != NULL)
5880 {
5881 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5882 DWO file. */
5883 if (!per_cu->is_debug_types)
5884 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5885 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5886 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5887 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5888 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5889
5890 cu->addr_base = stub_comp_unit_die->addr_base ();
5891
5892 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5893 We need the value before we can process DW_AT_ranges values from the
5894 DWO. */
5895 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5896
5897 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5898 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5899 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5900 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5901 section. */
5902 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5903 }
5904 else if (stub_comp_dir != NULL)
5905 {
5906 /* Reconstruct the comp_dir attribute to simplify the code below. */
5907 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5908 comp_dir->name = DW_AT_comp_dir;
5909 comp_dir->form = DW_FORM_string;
5910 comp_dir->set_string_noncanonical (stub_comp_dir);
5911 }
5912
5913 /* Set up for reading the DWO CU/TU. */
5914 cu->dwo_unit = dwo_unit;
5915 dwarf2_section_info *section = dwo_unit->section;
5916 section->read (objfile);
5917 abfd = section->get_bfd_owner ();
5918 begin_info_ptr = info_ptr = (section->buffer
5919 + to_underlying (dwo_unit->sect_off));
5920 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5921
5922 if (per_cu->is_debug_types)
5923 {
5924 signatured_type *sig_type = (struct signatured_type *) per_cu;
5925
5926 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5927 section, dwo_abbrev_section,
5928 info_ptr, rcuh_kind::TYPE);
5929 /* This is not an assert because it can be caused by bad debug info. */
5930 if (sig_type->signature != cu->header.signature)
5931 {
5932 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5933 " TU at offset %s [in module %s]"),
5934 hex_string (sig_type->signature),
5935 hex_string (cu->header.signature),
5936 sect_offset_str (dwo_unit->sect_off),
5937 bfd_get_filename (abfd));
5938 }
5939 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5940 /* For DWOs coming from DWP files, we don't know the CU length
5941 nor the type's offset in the TU until now. */
5942 dwo_unit->length = cu->header.get_length ();
5943 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5944
5945 /* Establish the type offset that can be used to lookup the type.
5946 For DWO files, we don't know it until now. */
5947 sig_type->type_offset_in_section
5948 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5949 }
5950 else
5951 {
5952 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5953 section, dwo_abbrev_section,
5954 info_ptr, rcuh_kind::COMPILE);
5955 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5956 /* For DWOs coming from DWP files, we don't know the CU length
5957 until now. */
5958 dwo_unit->length = cu->header.get_length ();
5959 }
5960
5961 dwo_abbrev_section->read (objfile);
5962 *result_dwo_abbrev_table
5963 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
5964 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
5965 result_dwo_abbrev_table->get ());
5966
5967 /* Read in the die, but leave space to copy over the attributes
5968 from the stub. This has the benefit of simplifying the rest of
5969 the code - all the work to maintain the illusion of a single
5970 DW_TAG_{compile,type}_unit DIE is done here. */
5971 num_extra_attrs = ((stmt_list != NULL)
5972 + (low_pc != NULL)
5973 + (high_pc != NULL)
5974 + (ranges != NULL)
5975 + (comp_dir != NULL));
5976 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5977 num_extra_attrs);
5978
5979 /* Copy over the attributes from the stub to the DIE we just read in. */
5980 comp_unit_die = *result_comp_unit_die;
5981 i = comp_unit_die->num_attrs;
5982 if (stmt_list != NULL)
5983 comp_unit_die->attrs[i++] = *stmt_list;
5984 if (low_pc != NULL)
5985 comp_unit_die->attrs[i++] = *low_pc;
5986 if (high_pc != NULL)
5987 comp_unit_die->attrs[i++] = *high_pc;
5988 if (ranges != NULL)
5989 comp_unit_die->attrs[i++] = *ranges;
5990 if (comp_dir != NULL)
5991 comp_unit_die->attrs[i++] = *comp_dir;
5992 comp_unit_die->num_attrs += num_extra_attrs;
5993
5994 if (dwarf_die_debug)
5995 {
5996 gdb_printf (gdb_stdlog,
5997 "Read die from %s@0x%x of %s:\n",
5998 section->get_name (),
5999 (unsigned) (begin_info_ptr - section->buffer),
6000 bfd_get_filename (abfd));
6001 dump_die (comp_unit_die, dwarf_die_debug);
6002 }
6003
6004 /* Skip dummy compilation units. */
6005 if (info_ptr >= begin_info_ptr + dwo_unit->length
6006 || peek_abbrev_code (abfd, info_ptr) == 0)
6007 return 0;
6008
6009 *result_info_ptr = info_ptr;
6010 return 1;
6011 }
6012
6013 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6014 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6015 signature is part of the header. */
6016 static gdb::optional<ULONGEST>
6017 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6018 {
6019 if (cu->header.version >= 5)
6020 return cu->header.signature;
6021 struct attribute *attr;
6022 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6023 if (attr == nullptr || !attr->form_is_unsigned ())
6024 return gdb::optional<ULONGEST> ();
6025 return attr->as_unsigned ();
6026 }
6027
6028 /* Subroutine of cutu_reader to simplify it.
6029 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6030 Returns NULL if the specified DWO unit cannot be found. */
6031
6032 static struct dwo_unit *
6033 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6034 {
6035 #if CXX_STD_THREAD
6036 /* We need a lock here both to handle the DWO hash table, and BFD,
6037 which is not thread-safe. */
6038 static std::mutex dwo_lock;
6039
6040 std::lock_guard<std::mutex> guard (dwo_lock);
6041 #endif
6042
6043 dwarf2_per_cu_data *per_cu = cu->per_cu;
6044 struct dwo_unit *dwo_unit;
6045 const char *comp_dir;
6046
6047 gdb_assert (cu != NULL);
6048
6049 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6050 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6051 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6052
6053 if (per_cu->is_debug_types)
6054 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6055 else
6056 {
6057 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6058
6059 if (!signature.has_value ())
6060 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6061 " [in module %s]"),
6062 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6063
6064 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6065 }
6066
6067 return dwo_unit;
6068 }
6069
6070 /* Subroutine of cutu_reader to simplify it.
6071 See it for a description of the parameters.
6072 Read a TU directly from a DWO file, bypassing the stub. */
6073
6074 void
6075 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6076 dwarf2_per_objfile *per_objfile,
6077 dwarf2_cu *existing_cu)
6078 {
6079 struct signatured_type *sig_type;
6080
6081 /* Verify we can do the following downcast, and that we have the
6082 data we need. */
6083 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6084 sig_type = (struct signatured_type *) this_cu;
6085 gdb_assert (sig_type->dwo_unit != NULL);
6086
6087 dwarf2_cu *cu;
6088
6089 if (existing_cu != nullptr)
6090 {
6091 cu = existing_cu;
6092 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6093 /* There's no need to do the rereading_dwo_cu handling that
6094 cutu_reader does since we don't read the stub. */
6095 }
6096 else
6097 {
6098 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6099 in per_objfile yet. */
6100 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6101 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6102 cu = m_new_cu.get ();
6103 }
6104
6105 /* A future optimization, if needed, would be to use an existing
6106 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6107 could share abbrev tables. */
6108
6109 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6110 NULL /* stub_comp_unit_die */,
6111 sig_type->dwo_unit->dwo_file->comp_dir,
6112 this, &info_ptr,
6113 &comp_unit_die,
6114 &m_dwo_abbrev_table) == 0)
6115 {
6116 /* Dummy die. */
6117 dummy_p = true;
6118 }
6119 }
6120
6121 /* Initialize a CU (or TU) and read its DIEs.
6122 If the CU defers to a DWO file, read the DWO file as well.
6123
6124 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6125 Otherwise the table specified in the comp unit header is read in and used.
6126 This is an optimization for when we already have the abbrev table.
6127
6128 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6129 allocated. */
6130
6131 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6132 dwarf2_per_objfile *per_objfile,
6133 struct abbrev_table *abbrev_table,
6134 dwarf2_cu *existing_cu,
6135 bool skip_partial,
6136 abbrev_cache *cache)
6137 : die_reader_specs {},
6138 m_this_cu (this_cu)
6139 {
6140 struct objfile *objfile = per_objfile->objfile;
6141 struct dwarf2_section_info *section = this_cu->section;
6142 bfd *abfd = section->get_bfd_owner ();
6143 const gdb_byte *begin_info_ptr;
6144 struct signatured_type *sig_type = NULL;
6145 struct dwarf2_section_info *abbrev_section;
6146 /* Non-zero if CU currently points to a DWO file and we need to
6147 reread it. When this happens we need to reread the skeleton die
6148 before we can reread the DWO file (this only applies to CUs, not TUs). */
6149 int rereading_dwo_cu = 0;
6150
6151 if (dwarf_die_debug)
6152 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6153 this_cu->is_debug_types ? "type" : "comp",
6154 sect_offset_str (this_cu->sect_off));
6155
6156 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6157 file (instead of going through the stub), short-circuit all of this. */
6158 if (this_cu->reading_dwo_directly)
6159 {
6160 /* Narrow down the scope of possibilities to have to understand. */
6161 gdb_assert (this_cu->is_debug_types);
6162 gdb_assert (abbrev_table == NULL);
6163 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6164 return;
6165 }
6166
6167 /* This is cheap if the section is already read in. */
6168 section->read (objfile);
6169
6170 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6171
6172 abbrev_section = get_abbrev_section_for_cu (this_cu);
6173
6174 dwarf2_cu *cu;
6175
6176 if (existing_cu != nullptr)
6177 {
6178 cu = existing_cu;
6179 /* If this CU is from a DWO file we need to start over, we need to
6180 refetch the attributes from the skeleton CU.
6181 This could be optimized by retrieving those attributes from when we
6182 were here the first time: the previous comp_unit_die was stored in
6183 comp_unit_obstack. But there's no data yet that we need this
6184 optimization. */
6185 if (cu->dwo_unit != NULL)
6186 rereading_dwo_cu = 1;
6187 }
6188 else
6189 {
6190 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6191 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6192 let us know that the CU is being scanned using the parallel
6193 indexer. This assert is avoided in this case because (1) it
6194 is irrelevant, and (2) the get_cu method is not
6195 thread-safe. */
6196 gdb_assert (cache != nullptr
6197 || per_objfile->get_cu (this_cu) == nullptr);
6198 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6199 cu = m_new_cu.get ();
6200 }
6201
6202 /* Get the header. */
6203 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6204 {
6205 /* We already have the header, there's no need to read it in again. */
6206 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6207 }
6208 else
6209 {
6210 if (this_cu->is_debug_types)
6211 {
6212 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6213 section, abbrev_section,
6214 info_ptr, rcuh_kind::TYPE);
6215
6216 /* Since per_cu is the first member of struct signatured_type,
6217 we can go from a pointer to one to a pointer to the other. */
6218 sig_type = (struct signatured_type *) this_cu;
6219 gdb_assert (sig_type->signature == cu->header.signature);
6220 gdb_assert (sig_type->type_offset_in_tu
6221 == cu->header.type_cu_offset_in_tu);
6222 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6223
6224 /* LENGTH has not been set yet for type units if we're
6225 using .gdb_index. */
6226 this_cu->length = cu->header.get_length ();
6227
6228 /* Establish the type offset that can be used to lookup the type. */
6229 sig_type->type_offset_in_section =
6230 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6231
6232 this_cu->dwarf_version = cu->header.version;
6233 }
6234 else
6235 {
6236 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6237 section, abbrev_section,
6238 info_ptr,
6239 rcuh_kind::COMPILE);
6240
6241 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6242 if (this_cu->length == 0)
6243 this_cu->length = cu->header.get_length ();
6244 else
6245 gdb_assert (this_cu->length == cu->header.get_length ());
6246 this_cu->dwarf_version = cu->header.version;
6247 }
6248 }
6249
6250 /* Skip dummy compilation units. */
6251 if (info_ptr >= begin_info_ptr + this_cu->length
6252 || peek_abbrev_code (abfd, info_ptr) == 0)
6253 {
6254 dummy_p = true;
6255 return;
6256 }
6257
6258 /* If we don't have them yet, read the abbrevs for this compilation unit.
6259 And if we need to read them now, make sure they're freed when we're
6260 done. */
6261 if (abbrev_table != NULL)
6262 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6263 else
6264 {
6265 if (cache != nullptr)
6266 abbrev_table = cache->find (abbrev_section,
6267 cu->header.abbrev_sect_off);
6268 if (abbrev_table == nullptr)
6269 {
6270 abbrev_section->read (objfile);
6271 m_abbrev_table_holder
6272 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6273 abbrev_table = m_abbrev_table_holder.get ();
6274 }
6275 }
6276
6277 /* Read the top level CU/TU die. */
6278 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6279 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6280
6281 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6282 {
6283 dummy_p = true;
6284 return;
6285 }
6286
6287 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6288 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6289 table from the DWO file and pass the ownership over to us. It will be
6290 referenced from READER, so we must make sure to free it after we're done
6291 with READER.
6292
6293 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6294 DWO CU, that this test will fail (the attribute will not be present). */
6295 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6296 if (dwo_name != nullptr)
6297 {
6298 struct dwo_unit *dwo_unit;
6299 struct die_info *dwo_comp_unit_die;
6300
6301 if (comp_unit_die->has_children)
6302 {
6303 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6304 " has children (offset %s) [in module %s]"),
6305 sect_offset_str (this_cu->sect_off),
6306 bfd_get_filename (abfd));
6307 }
6308 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6309 if (dwo_unit != NULL)
6310 {
6311 if (read_cutu_die_from_dwo (cu, dwo_unit,
6312 comp_unit_die, NULL,
6313 this, &info_ptr,
6314 &dwo_comp_unit_die,
6315 &m_dwo_abbrev_table) == 0)
6316 {
6317 /* Dummy die. */
6318 dummy_p = true;
6319 return;
6320 }
6321 comp_unit_die = dwo_comp_unit_die;
6322 }
6323 else
6324 {
6325 /* Yikes, we couldn't find the rest of the DIE, we only have
6326 the stub. A complaint has already been logged. There's
6327 not much more we can do except pass on the stub DIE to
6328 die_reader_func. We don't want to throw an error on bad
6329 debug info. */
6330 }
6331 }
6332 }
6333
6334 void
6335 cutu_reader::keep ()
6336 {
6337 /* Done, clean up. */
6338 gdb_assert (!dummy_p);
6339 if (m_new_cu != NULL)
6340 {
6341 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6342 now. */
6343 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6344 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6345 }
6346 }
6347
6348 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6349 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6350 assumed to have already done the lookup to find the DWO file).
6351
6352 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6353 THIS_CU->is_debug_types, but nothing else.
6354
6355 We fill in THIS_CU->length.
6356
6357 THIS_CU->cu is always freed when done.
6358 This is done in order to not leave THIS_CU->cu in a state where we have
6359 to care whether it refers to the "main" CU or the DWO CU.
6360
6361 When parent_cu is passed, it is used to provide a default value for
6362 str_offsets_base and addr_base from the parent. */
6363
6364 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6365 dwarf2_per_objfile *per_objfile,
6366 struct dwarf2_cu *parent_cu,
6367 struct dwo_file *dwo_file)
6368 : die_reader_specs {},
6369 m_this_cu (this_cu)
6370 {
6371 struct objfile *objfile = per_objfile->objfile;
6372 struct dwarf2_section_info *section = this_cu->section;
6373 bfd *abfd = section->get_bfd_owner ();
6374 struct dwarf2_section_info *abbrev_section;
6375 const gdb_byte *begin_info_ptr, *info_ptr;
6376
6377 if (dwarf_die_debug)
6378 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6379 this_cu->is_debug_types ? "type" : "comp",
6380 sect_offset_str (this_cu->sect_off));
6381
6382 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6383
6384 abbrev_section = (dwo_file != NULL
6385 ? &dwo_file->sections.abbrev
6386 : get_abbrev_section_for_cu (this_cu));
6387
6388 /* This is cheap if the section is already read in. */
6389 section->read (objfile);
6390
6391 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6392
6393 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6394 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6395 section, abbrev_section, info_ptr,
6396 (this_cu->is_debug_types
6397 ? rcuh_kind::TYPE
6398 : rcuh_kind::COMPILE));
6399
6400 if (parent_cu != nullptr)
6401 {
6402 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6403 m_new_cu->addr_base = parent_cu->addr_base;
6404 }
6405 this_cu->length = m_new_cu->header.get_length ();
6406
6407 /* Skip dummy compilation units. */
6408 if (info_ptr >= begin_info_ptr + this_cu->length
6409 || peek_abbrev_code (abfd, info_ptr) == 0)
6410 {
6411 dummy_p = true;
6412 return;
6413 }
6414
6415 abbrev_section->read (objfile);
6416 m_abbrev_table_holder
6417 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6418
6419 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6420 m_abbrev_table_holder.get ());
6421 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6422 }
6423
6424 \f
6425 /* Type Unit Groups.
6426
6427 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6428 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6429 so that all types coming from the same compilation (.o file) are grouped
6430 together. A future step could be to put the types in the same symtab as
6431 the CU the types ultimately came from. */
6432
6433 static hashval_t
6434 hash_type_unit_group (const void *item)
6435 {
6436 const struct type_unit_group *tu_group
6437 = (const struct type_unit_group *) item;
6438
6439 return hash_stmt_list_entry (&tu_group->hash);
6440 }
6441
6442 static int
6443 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6444 {
6445 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6446 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6447
6448 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6449 }
6450
6451 /* Allocate a hash table for type unit groups. */
6452
6453 static htab_up
6454 allocate_type_unit_groups_table ()
6455 {
6456 return htab_up (htab_create_alloc (3,
6457 hash_type_unit_group,
6458 eq_type_unit_group,
6459 htab_delete_entry<type_unit_group>,
6460 xcalloc, xfree));
6461 }
6462
6463 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6464 partial symtabs. We combine several TUs per psymtab to not let the size
6465 of any one psymtab grow too big. */
6466 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6467 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6468
6469 /* Helper routine for get_type_unit_group.
6470 Create the type_unit_group object used to hold one or more TUs. */
6471
6472 static std::unique_ptr<type_unit_group>
6473 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6474 {
6475 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6476
6477 tu_group->hash.dwo_unit = cu->dwo_unit;
6478 tu_group->hash.line_sect_off = line_offset_struct;
6479
6480 return tu_group;
6481 }
6482
6483 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6484 STMT_LIST is a DW_AT_stmt_list attribute. */
6485
6486 static struct type_unit_group *
6487 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6488 {
6489 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6490 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6491 struct type_unit_group *tu_group;
6492 void **slot;
6493 unsigned int line_offset;
6494 struct type_unit_group type_unit_group_for_lookup;
6495
6496 if (per_objfile->per_bfd->type_unit_groups == NULL)
6497 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6498
6499 /* Do we need to create a new group, or can we use an existing one? */
6500
6501 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6502 {
6503 line_offset = stmt_list->as_unsigned ();
6504 ++tu_stats->nr_symtab_sharers;
6505 }
6506 else
6507 {
6508 /* Ugh, no stmt_list. Rare, but we have to handle it.
6509 We can do various things here like create one group per TU or
6510 spread them over multiple groups to split up the expansion work.
6511 To avoid worst case scenarios (too many groups or too large groups)
6512 we, umm, group them in bunches. */
6513 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6514 | (tu_stats->nr_stmt_less_type_units
6515 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6516 ++tu_stats->nr_stmt_less_type_units;
6517 }
6518
6519 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6520 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6521 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6522 &type_unit_group_for_lookup, INSERT);
6523 if (*slot == nullptr)
6524 {
6525 sect_offset line_offset_struct = (sect_offset) line_offset;
6526 std::unique_ptr<type_unit_group> grp
6527 = create_type_unit_group (cu, line_offset_struct);
6528 *slot = grp.release ();
6529 ++tu_stats->nr_symtabs;
6530 }
6531
6532 tu_group = (struct type_unit_group *) *slot;
6533 gdb_assert (tu_group != nullptr);
6534 return tu_group;
6535 }
6536 \f
6537
6538 /* An instance of this is created when scanning DWARF to create a
6539 cooked index. */
6540
6541 class cooked_index_storage
6542 {
6543 public:
6544
6545 cooked_index_storage ()
6546 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6547 eq_cutu_reader,
6548 htab_delete_entry<cutu_reader>,
6549 xcalloc, xfree)),
6550 m_index (new cooked_index),
6551 m_addrmap_storage (),
6552 m_addrmap (addrmap_create_mutable (&m_addrmap_storage))
6553 {
6554 }
6555
6556 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6557
6558 /* Return the current abbrev cache. */
6559 abbrev_cache *get_abbrev_cache ()
6560 {
6561 return &m_abbrev_cache;
6562 }
6563
6564 /* Return the DIE reader corresponding to PER_CU. If no such reader
6565 has been registered, return NULL. */
6566 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6567 {
6568 int index = per_cu->index;
6569 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6570 &index, index);
6571 }
6572
6573 /* Preserve READER by storing it in the local hash table. */
6574 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6575 {
6576 m_abbrev_cache.add (reader->release_abbrev_table ());
6577
6578 int index = reader->cu->per_cu->index;
6579 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6580 index, INSERT);
6581 gdb_assert (*slot == nullptr);
6582 cutu_reader *result = reader.get ();
6583 *slot = reader.release ();
6584 return result;
6585 }
6586
6587 /* Add an entry to the index. The arguments describe the entry; see
6588 cooked-index.h. The new entry is returned. */
6589 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6590 cooked_index_flag flags,
6591 const char *name,
6592 const cooked_index_entry *parent_entry,
6593 dwarf2_per_cu_data *per_cu)
6594 {
6595 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6596 }
6597
6598 /* Install the current addrmap into the index being constructed,
6599 then transfer ownership of the index to the caller. */
6600 std::unique_ptr<cooked_index> release ()
6601 {
6602 m_index->install_addrmap (m_addrmap);
6603 return std::move (m_index);
6604 }
6605
6606 /* Return the mutable addrmap that is currently being created. */
6607 addrmap *get_addrmap ()
6608 {
6609 return m_addrmap;
6610 }
6611
6612 private:
6613
6614 /* Hash function for a cutu_reader. */
6615 static hashval_t hash_cutu_reader (const void *a)
6616 {
6617 const cutu_reader *reader = (const cutu_reader *) a;
6618 return reader->cu->per_cu->index;
6619 }
6620
6621 /* Equality function for cutu_reader. */
6622 static int eq_cutu_reader (const void *a, const void *b)
6623 {
6624 const cutu_reader *ra = (const cutu_reader *) a;
6625 const int *rb = (const int *) b;
6626 return ra->cu->per_cu->index == *rb;
6627 }
6628
6629 /* The abbrev cache used by this indexer. */
6630 abbrev_cache m_abbrev_cache;
6631 /* A hash table of cutu_reader objects. */
6632 htab_up m_reader_hash;
6633 /* The index that is being constructed. */
6634 std::unique_ptr<cooked_index> m_index;
6635
6636 /* Storage for the writeable addrmap. */
6637 auto_obstack m_addrmap_storage;
6638 /* A writeable addrmap being constructed by this scanner. */
6639 addrmap *m_addrmap;
6640 };
6641
6642 /* An instance of this is created to index a CU. */
6643
6644 class cooked_indexer
6645 {
6646 public:
6647
6648 cooked_indexer (cooked_index_storage *storage,
6649 dwarf2_per_cu_data *per_cu,
6650 enum language language)
6651 : m_index_storage (storage),
6652 m_per_cu (per_cu),
6653 m_language (language),
6654 m_obstack (),
6655 m_die_range_map (addrmap_create_mutable (&m_obstack))
6656 {
6657 }
6658
6659 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6660
6661 /* Index the given CU. */
6662 void make_index (cutu_reader *reader);
6663
6664 private:
6665
6666 /* A helper function to turn a section offset into an address that
6667 can be used in an addrmap. */
6668 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6669 {
6670 CORE_ADDR value = to_underlying (offset);
6671 if (is_dwz)
6672 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6673 return value;
6674 }
6675
6676 /* A helper function to scan the PC bounds of READER and record them
6677 in the storage's addrmap. */
6678 void check_bounds (cutu_reader *reader);
6679
6680 /* Ensure that the indicated CU exists. The cutu_reader for it is
6681 returned. FOR_SCANNING is true if the caller intends to scan all
6682 the DIEs in the CU; when false, this use is assumed to be to look
6683 up just a single DIE. */
6684 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6685 dwarf2_per_objfile *per_objfile,
6686 sect_offset sect_off,
6687 bool is_dwz,
6688 bool for_scanning);
6689
6690 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6691 the entry for the enclosing scope (nullptr at top level). FULLY
6692 is true when a full scan must be done -- in some languages,
6693 function scopes must be fully explored in order to find nested
6694 functions. This returns a pointer to just after the spot where
6695 reading stopped. */
6696 const gdb_byte *index_dies (cutu_reader *reader,
6697 const gdb_byte *info_ptr,
6698 const cooked_index_entry *parent_entry,
6699 bool fully);
6700
6701 /* Scan the attributes for a given DIE and update the out
6702 parameters. Returns a pointer to the byte after the DIE. */
6703 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6704 cutu_reader *reader,
6705 const gdb_byte *watermark_ptr,
6706 const gdb_byte *info_ptr,
6707 const abbrev_info *abbrev,
6708 const char **name,
6709 const char **linkage_name,
6710 cooked_index_flag *flags,
6711 sect_offset *sibling_offset,
6712 const cooked_index_entry **parent_entry,
6713 CORE_ADDR *maybe_defer,
6714 bool for_specification);
6715
6716 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6717 DW_AT_import, and then scanning the referenced CU. Returns a
6718 pointer to the byte after the DIE. */
6719 const gdb_byte *index_imported_unit (cutu_reader *reader,
6720 const gdb_byte *info_ptr,
6721 const abbrev_info *abbrev);
6722
6723 /* Recursively read DIEs, recording the section offsets in
6724 m_die_range_map and then calling index_dies. */
6725 const gdb_byte *recurse (cutu_reader *reader,
6726 const gdb_byte *info_ptr,
6727 const cooked_index_entry *parent_entry,
6728 bool fully);
6729
6730 /* The storage object, where the results are kept. */
6731 cooked_index_storage *m_index_storage;
6732 /* The CU that we are reading on behalf of. This object might be
6733 asked to index one CU but to treat the results as if they come
6734 from some including CU; in this case the including CU would be
6735 recorded here. */
6736 dwarf2_per_cu_data *m_per_cu;
6737 /* The language that we're assuming when reading. */
6738 enum language m_language;
6739
6740 /* Temporary storage. */
6741 auto_obstack m_obstack;
6742 /* An addrmap that maps from section offsets (see the form_addr
6743 method) to newly-created entries. See m_deferred_entries to
6744 understand this. */
6745 addrmap *m_die_range_map;
6746
6747 /* A single deferred entry. */
6748 struct deferred_entry
6749 {
6750 sect_offset die_offset;
6751 const char *name;
6752 CORE_ADDR spec_offset;
6753 dwarf_tag tag;
6754 cooked_index_flag flags;
6755 };
6756
6757 /* The generated DWARF can sometimes have the declaration for a
6758 method in a class (or perhaps namespace) scope, with the
6759 definition appearing outside this scope... just one of the many
6760 bad things about DWARF. In order to handle this situation, we
6761 defer certain entries until the end of scanning, at which point
6762 we'll know the containing context of all the DIEs that we might
6763 have scanned. This vector stores these deferred entries. */
6764 std::vector<deferred_entry> m_deferred_entries;
6765 };
6766
6767 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6768 Process compilation unit THIS_CU for a psymtab. */
6769
6770 static void
6771 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6772 dwarf2_per_objfile *per_objfile,
6773 cooked_index_storage *storage)
6774 {
6775 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6776 storage->get_abbrev_cache ());
6777
6778 if (reader.comp_unit_die == nullptr)
6779 return;
6780
6781 switch (reader.comp_unit_die->tag)
6782 {
6783 case DW_TAG_compile_unit:
6784 this_cu->unit_type = DW_UT_compile;
6785 break;
6786 case DW_TAG_partial_unit:
6787 this_cu->unit_type = DW_UT_partial;
6788 break;
6789 case DW_TAG_type_unit:
6790 this_cu->unit_type = DW_UT_type;
6791 break;
6792 default:
6793 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6794 dwarf_tag_name (reader.comp_unit_die->tag),
6795 sect_offset_str (reader.cu->per_cu->sect_off),
6796 objfile_name (per_objfile->objfile));
6797 }
6798
6799 if (reader.dummy_p)
6800 {
6801 /* Nothing. */
6802 }
6803 else if (this_cu->is_debug_types)
6804 build_type_psymtabs_reader (&reader, storage);
6805 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6806 {
6807 bool nope = false;
6808 if (this_cu->scanned.compare_exchange_strong (nope, true))
6809 {
6810 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6811 language_minimal);
6812 gdb_assert (storage != nullptr);
6813 cooked_indexer indexer (storage, this_cu, reader.cu->per_cu->lang);
6814 indexer.make_index (&reader);
6815 }
6816 }
6817 }
6818
6819 /* Reader function for build_type_psymtabs. */
6820
6821 static void
6822 build_type_psymtabs_reader (cutu_reader *reader,
6823 cooked_index_storage *storage)
6824 {
6825 struct dwarf2_cu *cu = reader->cu;
6826 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6827 struct die_info *type_unit_die = reader->comp_unit_die;
6828
6829 gdb_assert (per_cu->is_debug_types);
6830
6831 if (! type_unit_die->has_children)
6832 return;
6833
6834 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6835
6836 gdb_assert (storage != nullptr);
6837 cooked_indexer indexer (storage, per_cu, cu->per_cu->lang);
6838 indexer.make_index (reader);
6839 }
6840
6841 /* Struct used to sort TUs by their abbreviation table offset. */
6842
6843 struct tu_abbrev_offset
6844 {
6845 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6846 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6847 {}
6848
6849 /* This is used when sorting. */
6850 bool operator< (const tu_abbrev_offset &other) const
6851 {
6852 return abbrev_offset < other.abbrev_offset;
6853 }
6854
6855 signatured_type *sig_type;
6856 sect_offset abbrev_offset;
6857 };
6858
6859 /* Efficiently read all the type units.
6860
6861 The efficiency is because we sort TUs by the abbrev table they use and
6862 only read each abbrev table once. In one program there are 200K TUs
6863 sharing 8K abbrev tables.
6864
6865 The main purpose of this function is to support building the
6866 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6867 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6868 can collapse the search space by grouping them by stmt_list.
6869 The savings can be significant, in the same program from above the 200K TUs
6870 share 8K stmt_list tables.
6871
6872 FUNC is expected to call get_type_unit_group, which will create the
6873 struct type_unit_group if necessary and add it to
6874 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6875
6876 static void
6877 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6878 cooked_index_storage *storage)
6879 {
6880 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6881 abbrev_table_up abbrev_table;
6882 sect_offset abbrev_offset;
6883
6884 /* It's up to the caller to not call us multiple times. */
6885 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6886
6887 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
6888 return;
6889
6890 /* TUs typically share abbrev tables, and there can be way more TUs than
6891 abbrev tables. Sort by abbrev table to reduce the number of times we
6892 read each abbrev table in.
6893 Alternatives are to punt or to maintain a cache of abbrev tables.
6894 This is simpler and efficient enough for now.
6895
6896 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6897 symtab to use). Typically TUs with the same abbrev offset have the same
6898 stmt_list value too so in practice this should work well.
6899
6900 The basic algorithm here is:
6901
6902 sort TUs by abbrev table
6903 for each TU with same abbrev table:
6904 read abbrev table if first user
6905 read TU top level DIE
6906 [IWBN if DWO skeletons had DW_AT_stmt_list]
6907 call FUNC */
6908
6909 dwarf_read_debug_printf ("Building type unit groups ...");
6910
6911 /* Sort in a separate table to maintain the order of all_comp_units
6912 for .gdb_index: TU indices directly index all_type_units. */
6913 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6914 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
6915
6916 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
6917 {
6918 if (cu->is_debug_types)
6919 {
6920 auto sig_type = static_cast<signatured_type *> (cu.get ());
6921 sorted_by_abbrev.emplace_back
6922 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6923 sig_type->sect_off));
6924 }
6925 }
6926
6927 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6928
6929 abbrev_offset = (sect_offset) ~(unsigned) 0;
6930
6931 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6932 {
6933 /* Switch to the next abbrev table if necessary. */
6934 if (abbrev_table == NULL
6935 || tu.abbrev_offset != abbrev_offset)
6936 {
6937 abbrev_offset = tu.abbrev_offset;
6938 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6939 abbrev_table =
6940 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6941 ++tu_stats->nr_uniq_abbrev_tables;
6942 }
6943
6944 cutu_reader reader (tu.sig_type, per_objfile,
6945 abbrev_table.get (), nullptr, false);
6946 if (!reader.dummy_p)
6947 build_type_psymtabs_reader (&reader, storage);
6948 }
6949 }
6950
6951 /* Print collected type unit statistics. */
6952
6953 static void
6954 print_tu_stats (dwarf2_per_objfile *per_objfile)
6955 {
6956 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6957
6958 dwarf_read_debug_printf ("Type unit statistics:");
6959 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6960 dwarf_read_debug_printf (" %d uniq abbrev tables",
6961 tu_stats->nr_uniq_abbrev_tables);
6962 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6963 tu_stats->nr_symtabs);
6964 dwarf_read_debug_printf (" %d symtab sharers",
6965 tu_stats->nr_symtab_sharers);
6966 dwarf_read_debug_printf (" %d type units without a stmt_list",
6967 tu_stats->nr_stmt_less_type_units);
6968 dwarf_read_debug_printf (" %d all_type_units reallocs",
6969 tu_stats->nr_all_type_units_reallocs);
6970 }
6971
6972 struct skeleton_data
6973 {
6974 dwarf2_per_objfile *per_objfile;
6975 cooked_index_storage *storage;
6976 };
6977
6978 /* Traversal function for process_skeletonless_type_unit.
6979 Read a TU in a DWO file and build partial symbols for it. */
6980
6981 static int
6982 process_skeletonless_type_unit (void **slot, void *info)
6983 {
6984 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6985 skeleton_data *data = (skeleton_data *) info;
6986
6987 /* If this TU doesn't exist in the global table, add it and read it in. */
6988
6989 if (data->per_objfile->per_bfd->signatured_types == NULL)
6990 data->per_objfile->per_bfd->signatured_types
6991 = allocate_signatured_type_table ();
6992
6993 signatured_type find_entry (dwo_unit->signature);
6994 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
6995 &find_entry, INSERT);
6996 /* If we've already seen this type there's nothing to do. What's happening
6997 is we're doing our own version of comdat-folding here. */
6998 if (*slot != NULL)
6999 return 1;
7000
7001 /* This does the job that create_all_comp_units would have done for
7002 this TU. */
7003 signatured_type *entry
7004 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7005 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7006 *slot = entry;
7007
7008 /* This does the job that build_type_psymtabs would have done. */
7009 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7010 if (!reader.dummy_p)
7011 build_type_psymtabs_reader (&reader, data->storage);
7012
7013 return 1;
7014 }
7015
7016 /* Traversal function for process_skeletonless_type_units. */
7017
7018 static int
7019 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7020 {
7021 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7022
7023 if (dwo_file->tus != NULL)
7024 htab_traverse_noresize (dwo_file->tus.get (),
7025 process_skeletonless_type_unit, info);
7026
7027 return 1;
7028 }
7029
7030 /* Scan all TUs of DWO files, verifying we've processed them.
7031 This is needed in case a TU was emitted without its skeleton.
7032 Note: This can't be done until we know what all the DWO files are. */
7033
7034 static void
7035 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7036 cooked_index_storage *storage)
7037 {
7038 skeleton_data data { per_objfile, storage };
7039
7040 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7041 if (get_dwp_file (per_objfile) == NULL
7042 && per_objfile->per_bfd->dwo_files != NULL)
7043 {
7044 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7045 process_dwo_file_for_skeletonless_type_units,
7046 &data);
7047 }
7048 }
7049
7050 /* Build the partial symbol table by doing a quick pass through the
7051 .debug_info and .debug_abbrev sections. */
7052
7053 static void
7054 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7055 {
7056 struct objfile *objfile = per_objfile->objfile;
7057 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7058
7059 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7060 objfile_name (objfile));
7061
7062 per_bfd->map_info_sections (objfile);
7063
7064 cooked_index_storage index_storage;
7065 create_all_comp_units (per_objfile);
7066 build_type_psymtabs (per_objfile, &index_storage);
7067 std::vector<std::unique_ptr<cooked_index>> indexes;
7068
7069 per_bfd->quick_file_names_table
7070 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
7071 if (!per_bfd->debug_aranges.empty ())
7072 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7073 index_storage.get_addrmap ());
7074
7075 {
7076 /* Ensure that complaints are handled correctly. */
7077 complaint_interceptor complaint_handler;
7078
7079 using iter_type = decltype (per_bfd->all_comp_units.begin ());
7080
7081 /* Each thread returns a pair holding a cooked index, and a vector
7082 of errors that should be printed. The latter is done because
7083 GDB's I/O system is not thread-safe. run_on_main_thread could be
7084 used, but that would mean the messages are printed after the
7085 prompt, which looks weird. */
7086 using result_type = std::pair<std::unique_ptr<cooked_index>,
7087 std::vector<gdb_exception>>;
7088 std::vector<result_type> results
7089 = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
7090 per_bfd->all_comp_units.end (),
7091 [=] (iter_type iter, iter_type end)
7092 {
7093 std::vector<gdb_exception> errors;
7094 cooked_index_storage thread_storage;
7095 for (; iter != end; ++iter)
7096 {
7097 dwarf2_per_cu_data *per_cu = iter->get ();
7098 try
7099 {
7100 process_psymtab_comp_unit (per_cu, per_objfile,
7101 &thread_storage);
7102 }
7103 catch (gdb_exception &except)
7104 {
7105 errors.push_back (std::move (except));
7106 }
7107 }
7108 return result_type (thread_storage.release (), std::move (errors));
7109 });
7110
7111 /* Only show a given exception a single time. */
7112 std::unordered_set<gdb_exception> seen_exceptions;
7113 for (auto &one_result : results)
7114 {
7115 indexes.push_back (std::move (one_result.first));
7116 for (auto &one_exc : one_result.second)
7117 if (seen_exceptions.insert (one_exc).second)
7118 exception_print (gdb_stderr, one_exc);
7119 }
7120 }
7121
7122 /* This has to wait until we read the CUs, we need the list of DWOs. */
7123 process_skeletonless_type_units (per_objfile, &index_storage);
7124
7125 if (dwarf_read_debug > 0)
7126 print_tu_stats (per_objfile);
7127
7128 indexes.push_back (index_storage.release ());
7129 /* Remove any NULL entries. This might happen if parallel-for
7130 decides to throttle the number of threads that were used. */
7131 indexes.erase
7132 (std::remove_if (indexes.begin (),
7133 indexes.end (),
7134 [] (const std::unique_ptr<cooked_index> &entry)
7135 {
7136 return entry == nullptr;
7137 }),
7138 indexes.end ());
7139 indexes.shrink_to_fit ();
7140
7141 cooked_index_vector *vec = new cooked_index_vector (std::move (indexes));
7142 per_bfd->index_table.reset (vec);
7143
7144 const cooked_index_entry *main_entry = vec->get_main ();
7145 if (main_entry != nullptr)
7146 set_objfile_main_name (objfile, main_entry->name,
7147 main_entry->per_cu->lang);
7148
7149 dwarf_read_debug_printf ("Done building psymtabs of %s",
7150 objfile_name (objfile));
7151 }
7152
7153 static void
7154 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7155 struct dwarf2_section_info *section,
7156 struct dwarf2_section_info *abbrev_section,
7157 unsigned int is_dwz,
7158 htab_up &types_htab,
7159 rcuh_kind section_kind)
7160 {
7161 const gdb_byte *info_ptr;
7162 struct objfile *objfile = per_objfile->objfile;
7163
7164 dwarf_read_debug_printf ("Reading %s for %s",
7165 section->get_name (),
7166 section->get_file_name ());
7167
7168 section->read (objfile);
7169
7170 info_ptr = section->buffer;
7171
7172 while (info_ptr < section->buffer + section->size)
7173 {
7174 dwarf2_per_cu_data_up this_cu;
7175
7176 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7177
7178 comp_unit_head cu_header;
7179 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7180 abbrev_section, info_ptr,
7181 section_kind);
7182
7183 /* Save the compilation unit for later lookup. */
7184 if (cu_header.unit_type != DW_UT_type)
7185 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7186 else
7187 {
7188 if (types_htab == nullptr)
7189 types_htab = allocate_signatured_type_table ();
7190
7191 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7192 (cu_header.signature);
7193 signatured_type *sig_ptr = sig_type.get ();
7194 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7195 this_cu.reset (sig_type.release ());
7196
7197 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7198 gdb_assert (slot != nullptr);
7199 if (*slot != nullptr)
7200 complaint (_("debug type entry at offset %s is duplicate to"
7201 " the entry at offset %s, signature %s"),
7202 sect_offset_str (sect_off),
7203 sect_offset_str (sig_ptr->sect_off),
7204 hex_string (sig_ptr->signature));
7205 *slot = sig_ptr;
7206 }
7207 this_cu->sect_off = sect_off;
7208 this_cu->length = cu_header.length + cu_header.initial_length_size;
7209 this_cu->is_dwz = is_dwz;
7210 this_cu->section = section;
7211
7212 info_ptr = info_ptr + this_cu->length;
7213 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7214 }
7215 }
7216
7217 /* Create a list of all compilation units in OBJFILE.
7218 This is only done for -readnow and building partial symtabs. */
7219
7220 static void
7221 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7222 {
7223 htab_up types_htab;
7224
7225 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7226 &per_objfile->per_bfd->abbrev, 0,
7227 types_htab, rcuh_kind::COMPILE);
7228 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7229 read_comp_units_from_section (per_objfile, &section,
7230 &per_objfile->per_bfd->abbrev, 0,
7231 types_htab, rcuh_kind::TYPE);
7232
7233 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7234 if (dwz != NULL)
7235 {
7236 /* Pre-read the sections we'll need to construct an index. */
7237 struct objfile *objfile = per_objfile->objfile;
7238 dwz->abbrev.read (objfile);
7239 dwz->info.read (objfile);
7240 dwz->str.read (objfile);
7241 dwz->line.read (objfile);
7242 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7243 types_htab, rcuh_kind::COMPILE);
7244 }
7245
7246 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7247 }
7248
7249 /* Return the initial uleb128 in the die at INFO_PTR. */
7250
7251 static unsigned int
7252 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7253 {
7254 unsigned int bytes_read;
7255
7256 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7257 }
7258
7259 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7260 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7261
7262 Return the corresponding abbrev, or NULL if the number is zero (indicating
7263 an empty DIE). In either case *BYTES_READ will be set to the length of
7264 the initial number. */
7265
7266 static const struct abbrev_info *
7267 peek_die_abbrev (const die_reader_specs &reader,
7268 const gdb_byte *info_ptr, unsigned int *bytes_read)
7269 {
7270 dwarf2_cu *cu = reader.cu;
7271 bfd *abfd = reader.abfd;
7272 unsigned int abbrev_number
7273 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7274
7275 if (abbrev_number == 0)
7276 return NULL;
7277
7278 const abbrev_info *abbrev
7279 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7280 if (!abbrev)
7281 {
7282 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7283 " at offset %s [in module %s]"),
7284 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7285 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7286 }
7287
7288 return abbrev;
7289 }
7290
7291 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7292 Returns a pointer to the end of a series of DIEs, terminated by an empty
7293 DIE. Any children of the skipped DIEs will also be skipped. */
7294
7295 static const gdb_byte *
7296 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7297 {
7298 while (1)
7299 {
7300 unsigned int bytes_read;
7301 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7302 &bytes_read);
7303
7304 if (abbrev == NULL)
7305 return info_ptr + bytes_read;
7306 else
7307 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7308 }
7309 }
7310
7311 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7312 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7313 abbrev corresponding to that skipped uleb128 should be passed in
7314 ABBREV.
7315
7316 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7317 returns a pointer to this DIE's sibling, skipping any children.
7318 Otherwise, returns a pointer to the DIE's first child. */
7319
7320 static const gdb_byte *
7321 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7322 const struct abbrev_info *abbrev, bool do_skip_children)
7323 {
7324 unsigned int bytes_read;
7325 struct attribute attr;
7326 bfd *abfd = reader->abfd;
7327 struct dwarf2_cu *cu = reader->cu;
7328 const gdb_byte *buffer = reader->buffer;
7329 const gdb_byte *buffer_end = reader->buffer_end;
7330 unsigned int form, i;
7331
7332 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7333 {
7334 /* We only handle DW_FORM_ref4 here. */
7335 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7336 unsigned int offset = read_4_bytes (abfd, sibling_data);
7337 const gdb_byte *sibling_ptr
7338 = buffer + to_underlying (cu->header.sect_off) + offset;
7339 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7340 return sibling_ptr;
7341 /* Fall through to the slow way. */
7342 }
7343 else if (abbrev->size_if_constant != 0)
7344 {
7345 info_ptr += abbrev->size_if_constant;
7346 if (do_skip_children && abbrev->has_children)
7347 return skip_children (reader, info_ptr);
7348 return info_ptr;
7349 }
7350
7351 for (i = 0; i < abbrev->num_attrs; i++)
7352 {
7353 /* The only abbrev we care about is DW_AT_sibling. */
7354 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7355 {
7356 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7357 if (attr.form == DW_FORM_ref_addr)
7358 complaint (_("ignoring absolute DW_AT_sibling"));
7359 else
7360 {
7361 sect_offset off = attr.get_ref_die_offset ();
7362 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7363
7364 if (sibling_ptr < info_ptr)
7365 complaint (_("DW_AT_sibling points backwards"));
7366 else if (sibling_ptr > reader->buffer_end)
7367 reader->die_section->overflow_complaint ();
7368 else
7369 return sibling_ptr;
7370 }
7371 }
7372
7373 /* If it isn't DW_AT_sibling, skip this attribute. */
7374 form = abbrev->attrs[i].form;
7375 skip_attribute:
7376 switch (form)
7377 {
7378 case DW_FORM_ref_addr:
7379 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7380 and later it is offset sized. */
7381 if (cu->header.version == 2)
7382 info_ptr += cu->header.addr_size;
7383 else
7384 info_ptr += cu->header.offset_size;
7385 break;
7386 case DW_FORM_GNU_ref_alt:
7387 info_ptr += cu->header.offset_size;
7388 break;
7389 case DW_FORM_addr:
7390 info_ptr += cu->header.addr_size;
7391 break;
7392 case DW_FORM_data1:
7393 case DW_FORM_ref1:
7394 case DW_FORM_flag:
7395 case DW_FORM_strx1:
7396 info_ptr += 1;
7397 break;
7398 case DW_FORM_flag_present:
7399 case DW_FORM_implicit_const:
7400 break;
7401 case DW_FORM_data2:
7402 case DW_FORM_ref2:
7403 case DW_FORM_strx2:
7404 info_ptr += 2;
7405 break;
7406 case DW_FORM_strx3:
7407 info_ptr += 3;
7408 break;
7409 case DW_FORM_data4:
7410 case DW_FORM_ref4:
7411 case DW_FORM_strx4:
7412 info_ptr += 4;
7413 break;
7414 case DW_FORM_data8:
7415 case DW_FORM_ref8:
7416 case DW_FORM_ref_sig8:
7417 info_ptr += 8;
7418 break;
7419 case DW_FORM_data16:
7420 info_ptr += 16;
7421 break;
7422 case DW_FORM_string:
7423 read_direct_string (abfd, info_ptr, &bytes_read);
7424 info_ptr += bytes_read;
7425 break;
7426 case DW_FORM_sec_offset:
7427 case DW_FORM_strp:
7428 case DW_FORM_GNU_strp_alt:
7429 info_ptr += cu->header.offset_size;
7430 break;
7431 case DW_FORM_exprloc:
7432 case DW_FORM_block:
7433 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7434 info_ptr += bytes_read;
7435 break;
7436 case DW_FORM_block1:
7437 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7438 break;
7439 case DW_FORM_block2:
7440 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7441 break;
7442 case DW_FORM_block4:
7443 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7444 break;
7445 case DW_FORM_addrx:
7446 case DW_FORM_strx:
7447 case DW_FORM_sdata:
7448 case DW_FORM_udata:
7449 case DW_FORM_ref_udata:
7450 case DW_FORM_GNU_addr_index:
7451 case DW_FORM_GNU_str_index:
7452 case DW_FORM_rnglistx:
7453 case DW_FORM_loclistx:
7454 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7455 break;
7456 case DW_FORM_indirect:
7457 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7458 info_ptr += bytes_read;
7459 /* We need to continue parsing from here, so just go back to
7460 the top. */
7461 goto skip_attribute;
7462
7463 default:
7464 error (_("Dwarf Error: Cannot handle %s "
7465 "in DWARF reader [in module %s]"),
7466 dwarf_form_name (form),
7467 bfd_get_filename (abfd));
7468 }
7469 }
7470
7471 if (do_skip_children && abbrev->has_children)
7472 return skip_children (reader, info_ptr);
7473 else
7474 return info_ptr;
7475 }
7476 \f
7477 /* Reading in full CUs. */
7478
7479 /* Add PER_CU to the queue. */
7480
7481 static void
7482 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7483 dwarf2_per_objfile *per_objfile,
7484 enum language pretend_language)
7485 {
7486 per_cu->queued = 1;
7487
7488 gdb_assert (per_objfile->per_bfd->queue.has_value ());
7489 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
7490 }
7491
7492 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7493
7494 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7495 dependency.
7496
7497 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7498 DIEs, false otherwise.
7499
7500 Explanation: there is an invariant that if a CU is queued for expansion
7501 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7502 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7503 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7504 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7505 invariant is respected.
7506
7507 The caller is therefore not required to load the CU's DIEs (we return false)
7508 if:
7509
7510 - the CU is already expanded, and therefore does not get enqueued
7511 - the CU gets enqueued for expansion, but its DIEs are already loaded
7512
7513 Note that the caller should not use this function's return value as an
7514 indicator of whether the CU's DIEs are loaded right now, it should check
7515 that by calling `dwarf2_per_objfile::get_cu` instead. */
7516
7517 static int
7518 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7519 dwarf2_per_cu_data *per_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 enum language pretend_language)
7522 {
7523 /* Mark the dependence relation so that we don't flush PER_CU
7524 too early. */
7525 if (dependent_cu != NULL)
7526 dependent_cu->add_dependence (per_cu);
7527
7528 /* If it's already on the queue, we have nothing to do. */
7529 if (per_cu->queued)
7530 {
7531 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7532 loaded. */
7533 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7534
7535 /* If the CU is queued for expansion, it should not already be
7536 expanded. */
7537 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7538
7539 /* The DIEs are already loaded, the caller doesn't need to do it. */
7540 return 0;
7541 }
7542
7543 bool queued = false;
7544 if (!per_objfile->symtab_set_p (per_cu))
7545 {
7546 /* Add it to the queue. */
7547 queue_comp_unit (per_cu, per_objfile, pretend_language);
7548 queued = true;
7549 }
7550
7551 /* If the compilation unit is already loaded, just mark it as
7552 used. */
7553 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7554 if (cu != nullptr)
7555 cu->last_used = 0;
7556
7557 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7558 and the DIEs are not already loaded. */
7559 return queued && cu == nullptr;
7560 }
7561
7562 /* Process the queue. */
7563
7564 static void
7565 process_queue (dwarf2_per_objfile *per_objfile)
7566 {
7567 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7568 objfile_name (per_objfile->objfile));
7569
7570 /* The queue starts out with one item, but following a DIE reference
7571 may load a new CU, adding it to the end of the queue. */
7572 while (!per_objfile->per_bfd->queue->empty ())
7573 {
7574 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7575 dwarf2_per_cu_data *per_cu = item.per_cu;
7576
7577 if (!per_objfile->symtab_set_p (per_cu))
7578 {
7579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7580
7581 /* Skip dummy CUs. */
7582 if (cu != nullptr)
7583 {
7584 unsigned int debug_print_threshold;
7585 char buf[100];
7586
7587 if (per_cu->is_debug_types)
7588 {
7589 struct signatured_type *sig_type =
7590 (struct signatured_type *) per_cu;
7591
7592 sprintf (buf, "TU %s at offset %s",
7593 hex_string (sig_type->signature),
7594 sect_offset_str (per_cu->sect_off));
7595 /* There can be 100s of TUs.
7596 Only print them in verbose mode. */
7597 debug_print_threshold = 2;
7598 }
7599 else
7600 {
7601 sprintf (buf, "CU at offset %s",
7602 sect_offset_str (per_cu->sect_off));
7603 debug_print_threshold = 1;
7604 }
7605
7606 if (dwarf_read_debug >= debug_print_threshold)
7607 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7608
7609 if (per_cu->is_debug_types)
7610 process_full_type_unit (cu, item.pretend_language);
7611 else
7612 process_full_comp_unit (cu, item.pretend_language);
7613
7614 if (dwarf_read_debug >= debug_print_threshold)
7615 dwarf_read_debug_printf ("Done expanding %s", buf);
7616 }
7617 }
7618
7619 per_cu->queued = 0;
7620 per_objfile->per_bfd->queue->pop ();
7621 }
7622
7623 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7624 objfile_name (per_objfile->objfile));
7625 }
7626
7627 /* Trivial hash function for die_info: the hash value of a DIE
7628 is its offset in .debug_info for this objfile. */
7629
7630 static hashval_t
7631 die_hash (const void *item)
7632 {
7633 const struct die_info *die = (const struct die_info *) item;
7634
7635 return to_underlying (die->sect_off);
7636 }
7637
7638 /* Trivial comparison function for die_info structures: two DIEs
7639 are equal if they have the same offset. */
7640
7641 static int
7642 die_eq (const void *item_lhs, const void *item_rhs)
7643 {
7644 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7645 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7646
7647 return die_lhs->sect_off == die_rhs->sect_off;
7648 }
7649
7650 /* Load the DIEs associated with PER_CU into memory.
7651
7652 In some cases, the caller, while reading partial symbols, will need to load
7653 the full symbols for the CU for some reason. It will already have a
7654 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7655 rather than creating a new one. */
7656
7657 static void
7658 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7659 dwarf2_per_objfile *per_objfile,
7660 dwarf2_cu *existing_cu,
7661 bool skip_partial,
7662 enum language pretend_language)
7663 {
7664 gdb_assert (! this_cu->is_debug_types);
7665
7666 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7667 if (reader.dummy_p)
7668 return;
7669
7670 struct dwarf2_cu *cu = reader.cu;
7671 const gdb_byte *info_ptr = reader.info_ptr;
7672
7673 gdb_assert (cu->die_hash == NULL);
7674 cu->die_hash =
7675 htab_create_alloc_ex (cu->header.length / 12,
7676 die_hash,
7677 die_eq,
7678 NULL,
7679 &cu->comp_unit_obstack,
7680 hashtab_obstack_allocate,
7681 dummy_obstack_deallocate);
7682
7683 if (reader.comp_unit_die->has_children)
7684 reader.comp_unit_die->child
7685 = read_die_and_siblings (&reader, reader.info_ptr,
7686 &info_ptr, reader.comp_unit_die);
7687 cu->dies = reader.comp_unit_die;
7688 /* comp_unit_die is not stored in die_hash, no need. */
7689
7690 /* We try not to read any attributes in this function, because not
7691 all CUs needed for references have been loaded yet, and symbol
7692 table processing isn't initialized. But we have to set the CU language,
7693 or we won't be able to build types correctly.
7694 Similarly, if we do not read the producer, we can not apply
7695 producer-specific interpretation. */
7696 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7697
7698 reader.keep ();
7699 }
7700
7701 /* Add a DIE to the delayed physname list. */
7702
7703 static void
7704 add_to_method_list (struct type *type, int fnfield_index, int index,
7705 const char *name, struct die_info *die,
7706 struct dwarf2_cu *cu)
7707 {
7708 struct delayed_method_info mi;
7709 mi.type = type;
7710 mi.fnfield_index = fnfield_index;
7711 mi.index = index;
7712 mi.name = name;
7713 mi.die = die;
7714 cu->method_list.push_back (mi);
7715 }
7716
7717 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7718 "const" / "volatile". If so, decrements LEN by the length of the
7719 modifier and return true. Otherwise return false. */
7720
7721 template<size_t N>
7722 static bool
7723 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7724 {
7725 size_t mod_len = sizeof (mod) - 1;
7726 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7727 {
7728 len -= mod_len;
7729 return true;
7730 }
7731 return false;
7732 }
7733
7734 /* Compute the physnames of any methods on the CU's method list.
7735
7736 The computation of method physnames is delayed in order to avoid the
7737 (bad) condition that one of the method's formal parameters is of an as yet
7738 incomplete type. */
7739
7740 static void
7741 compute_delayed_physnames (struct dwarf2_cu *cu)
7742 {
7743 /* Only C++ delays computing physnames. */
7744 if (cu->method_list.empty ())
7745 return;
7746 gdb_assert (cu->per_cu->lang == language_cplus);
7747
7748 for (const delayed_method_info &mi : cu->method_list)
7749 {
7750 const char *physname;
7751 struct fn_fieldlist *fn_flp
7752 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7753 physname = dwarf2_physname (mi.name, mi.die, cu);
7754 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7755 = physname ? physname : "";
7756
7757 /* Since there's no tag to indicate whether a method is a
7758 const/volatile overload, extract that information out of the
7759 demangled name. */
7760 if (physname != NULL)
7761 {
7762 size_t len = strlen (physname);
7763
7764 while (1)
7765 {
7766 if (physname[len] == ')') /* shortcut */
7767 break;
7768 else if (check_modifier (physname, len, " const"))
7769 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7770 else if (check_modifier (physname, len, " volatile"))
7771 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7772 else
7773 break;
7774 }
7775 }
7776 }
7777
7778 /* The list is no longer needed. */
7779 cu->method_list.clear ();
7780 }
7781
7782 /* Go objects should be embedded in a DW_TAG_module DIE,
7783 and it's not clear if/how imported objects will appear.
7784 To keep Go support simple until that's worked out,
7785 go back through what we've read and create something usable.
7786 We could do this while processing each DIE, and feels kinda cleaner,
7787 but that way is more invasive.
7788 This is to, for example, allow the user to type "p var" or "b main"
7789 without having to specify the package name, and allow lookups
7790 of module.object to work in contexts that use the expression
7791 parser. */
7792
7793 static void
7794 fixup_go_packaging (struct dwarf2_cu *cu)
7795 {
7796 gdb::unique_xmalloc_ptr<char> package_name;
7797 struct pending *list;
7798 int i;
7799
7800 for (list = *cu->get_builder ()->get_global_symbols ();
7801 list != NULL;
7802 list = list->next)
7803 {
7804 for (i = 0; i < list->nsyms; ++i)
7805 {
7806 struct symbol *sym = list->symbol[i];
7807
7808 if (sym->language () == language_go
7809 && sym->aclass () == LOC_BLOCK)
7810 {
7811 gdb::unique_xmalloc_ptr<char> this_package_name
7812 (go_symbol_package_name (sym));
7813
7814 if (this_package_name == NULL)
7815 continue;
7816 if (package_name == NULL)
7817 package_name = std::move (this_package_name);
7818 else
7819 {
7820 struct objfile *objfile = cu->per_objfile->objfile;
7821 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7822 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7823 (sym->symtab () != NULL
7824 ? symtab_to_filename_for_display
7825 (sym->symtab ())
7826 : objfile_name (objfile)),
7827 this_package_name.get (), package_name.get ());
7828 }
7829 }
7830 }
7831 }
7832
7833 if (package_name != NULL)
7834 {
7835 struct objfile *objfile = cu->per_objfile->objfile;
7836 const char *saved_package_name = objfile->intern (package_name.get ());
7837 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7838 saved_package_name);
7839 struct symbol *sym;
7840
7841 sym = new (&objfile->objfile_obstack) symbol;
7842 sym->set_language (language_go, &objfile->objfile_obstack);
7843 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7844 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7845 e.g., "main" finds the "main" module and not C's main(). */
7846 sym->set_domain (STRUCT_DOMAIN);
7847 sym->set_aclass_index (LOC_TYPEDEF);
7848 sym->set_type (type);
7849
7850 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7851 }
7852 }
7853
7854 /* Allocate a fully-qualified name consisting of the two parts on the
7855 obstack. */
7856
7857 static const char *
7858 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7859 {
7860 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7861 }
7862
7863 /* A helper that allocates a variant part to attach to a Rust enum
7864 type. OBSTACK is where the results should be allocated. TYPE is
7865 the type we're processing. DISCRIMINANT_INDEX is the index of the
7866 discriminant. It must be the index of one of the fields of TYPE,
7867 or -1 to mean there is no discriminant (univariant enum).
7868 DEFAULT_INDEX is the index of the default field; or -1 if there is
7869 no default. RANGES is indexed by "effective" field number (the
7870 field index, but omitting the discriminant and default fields) and
7871 must hold the discriminant values used by the variants. Note that
7872 RANGES must have a lifetime at least as long as OBSTACK -- either
7873 already allocated on it, or static. */
7874
7875 static void
7876 alloc_rust_variant (struct obstack *obstack, struct type *type,
7877 int discriminant_index, int default_index,
7878 gdb::array_view<discriminant_range> ranges)
7879 {
7880 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7881 gdb_assert (discriminant_index == -1
7882 || (discriminant_index >= 0
7883 && discriminant_index < type->num_fields ()));
7884 gdb_assert (default_index == -1
7885 || (default_index >= 0 && default_index < type->num_fields ()));
7886
7887 /* We have one variant for each non-discriminant field. */
7888 int n_variants = type->num_fields ();
7889 if (discriminant_index != -1)
7890 --n_variants;
7891
7892 variant *variants = new (obstack) variant[n_variants];
7893 int var_idx = 0;
7894 int range_idx = 0;
7895 for (int i = 0; i < type->num_fields (); ++i)
7896 {
7897 if (i == discriminant_index)
7898 continue;
7899
7900 variants[var_idx].first_field = i;
7901 variants[var_idx].last_field = i + 1;
7902
7903 /* The default field does not need a range, but other fields do.
7904 We skipped the discriminant above. */
7905 if (i != default_index)
7906 {
7907 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7908 ++range_idx;
7909 }
7910
7911 ++var_idx;
7912 }
7913
7914 gdb_assert (range_idx == ranges.size ());
7915 gdb_assert (var_idx == n_variants);
7916
7917 variant_part *part = new (obstack) variant_part;
7918 part->discriminant_index = discriminant_index;
7919 /* If there is no discriminant, then whether it is signed is of no
7920 consequence. */
7921 part->is_unsigned
7922 = (discriminant_index == -1
7923 ? false
7924 : type->field (discriminant_index).type ()->is_unsigned ());
7925 part->variants = gdb::array_view<variant> (variants, n_variants);
7926
7927 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7928 gdb::array_view<variant_part> *prop_value
7929 = new (storage) gdb::array_view<variant_part> (part, 1);
7930
7931 struct dynamic_prop prop;
7932 prop.set_variant_parts (prop_value);
7933
7934 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7935 }
7936
7937 /* Some versions of rustc emitted enums in an unusual way.
7938
7939 Ordinary enums were emitted as unions. The first element of each
7940 structure in the union was named "RUST$ENUM$DISR". This element
7941 held the discriminant.
7942
7943 These versions of Rust also implemented the "non-zero"
7944 optimization. When the enum had two values, and one is empty and
7945 the other holds a pointer that cannot be zero, the pointer is used
7946 as the discriminant, with a zero value meaning the empty variant.
7947 Here, the union's first member is of the form
7948 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7949 where the fieldnos are the indices of the fields that should be
7950 traversed in order to find the field (which may be several fields deep)
7951 and the variantname is the name of the variant of the case when the
7952 field is zero.
7953
7954 This function recognizes whether TYPE is of one of these forms,
7955 and, if so, smashes it to be a variant type. */
7956
7957 static void
7958 quirk_rust_enum (struct type *type, struct objfile *objfile)
7959 {
7960 gdb_assert (type->code () == TYPE_CODE_UNION);
7961
7962 /* We don't need to deal with empty enums. */
7963 if (type->num_fields () == 0)
7964 return;
7965
7966 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7967 if (type->num_fields () == 1
7968 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
7969 {
7970 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
7971
7972 /* Decode the field name to find the offset of the
7973 discriminant. */
7974 ULONGEST bit_offset = 0;
7975 struct type *field_type = type->field (0).type ();
7976 while (name[0] >= '0' && name[0] <= '9')
7977 {
7978 char *tail;
7979 unsigned long index = strtoul (name, &tail, 10);
7980 name = tail;
7981 if (*name != '$'
7982 || index >= field_type->num_fields ()
7983 || (field_type->field (index).loc_kind ()
7984 != FIELD_LOC_KIND_BITPOS))
7985 {
7986 complaint (_("Could not parse Rust enum encoding string \"%s\""
7987 "[in module %s]"),
7988 type->field (0).name (),
7989 objfile_name (objfile));
7990 return;
7991 }
7992 ++name;
7993
7994 bit_offset += field_type->field (index).loc_bitpos ();
7995 field_type = field_type->field (index).type ();
7996 }
7997
7998 /* Smash this type to be a structure type. We have to do this
7999 because the type has already been recorded. */
8000 type->set_code (TYPE_CODE_STRUCT);
8001 type->set_num_fields (3);
8002 /* Save the field we care about. */
8003 struct field saved_field = type->field (0);
8004 type->set_fields
8005 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8006
8007 /* Put the discriminant at index 0. */
8008 type->field (0).set_type (field_type);
8009 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8010 type->field (0).set_name ("<<discriminant>>");
8011 type->field (0).set_loc_bitpos (bit_offset);
8012
8013 /* The order of fields doesn't really matter, so put the real
8014 field at index 1 and the data-less field at index 2. */
8015 type->field (1) = saved_field;
8016 type->field (1).set_name
8017 (rust_last_path_segment (type->field (1).type ()->name ()));
8018 type->field (1).type ()->set_name
8019 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8020 type->field (1).name ()));
8021
8022 const char *dataless_name
8023 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8024 name);
8025 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8026 dataless_name);
8027 type->field (2).set_type (dataless_type);
8028 /* NAME points into the original discriminant name, which
8029 already has the correct lifetime. */
8030 type->field (2).set_name (name);
8031 type->field (2).set_loc_bitpos (0);
8032
8033 /* Indicate that this is a variant type. */
8034 static discriminant_range ranges[1] = { { 0, 0 } };
8035 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8036 }
8037 /* A union with a single anonymous field is probably an old-style
8038 univariant enum. */
8039 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8040 {
8041 /* Smash this type to be a structure type. We have to do this
8042 because the type has already been recorded. */
8043 type->set_code (TYPE_CODE_STRUCT);
8044
8045 struct type *field_type = type->field (0).type ();
8046 const char *variant_name
8047 = rust_last_path_segment (field_type->name ());
8048 type->field (0).set_name (variant_name);
8049 field_type->set_name
8050 (rust_fully_qualify (&objfile->objfile_obstack,
8051 type->name (), variant_name));
8052
8053 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8054 }
8055 else
8056 {
8057 struct type *disr_type = nullptr;
8058 for (int i = 0; i < type->num_fields (); ++i)
8059 {
8060 disr_type = type->field (i).type ();
8061
8062 if (disr_type->code () != TYPE_CODE_STRUCT)
8063 {
8064 /* All fields of a true enum will be structs. */
8065 return;
8066 }
8067 else if (disr_type->num_fields () == 0)
8068 {
8069 /* Could be data-less variant, so keep going. */
8070 disr_type = nullptr;
8071 }
8072 else if (strcmp (disr_type->field (0).name (),
8073 "RUST$ENUM$DISR") != 0)
8074 {
8075 /* Not a Rust enum. */
8076 return;
8077 }
8078 else
8079 {
8080 /* Found one. */
8081 break;
8082 }
8083 }
8084
8085 /* If we got here without a discriminant, then it's probably
8086 just a union. */
8087 if (disr_type == nullptr)
8088 return;
8089
8090 /* Smash this type to be a structure type. We have to do this
8091 because the type has already been recorded. */
8092 type->set_code (TYPE_CODE_STRUCT);
8093
8094 /* Make space for the discriminant field. */
8095 struct field *disr_field = &disr_type->field (0);
8096 field *new_fields
8097 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8098 * sizeof (struct field)));
8099 memcpy (new_fields + 1, type->fields (),
8100 type->num_fields () * sizeof (struct field));
8101 type->set_fields (new_fields);
8102 type->set_num_fields (type->num_fields () + 1);
8103
8104 /* Install the discriminant at index 0 in the union. */
8105 type->field (0) = *disr_field;
8106 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8107 type->field (0).set_name ("<<discriminant>>");
8108
8109 /* We need a way to find the correct discriminant given a
8110 variant name. For convenience we build a map here. */
8111 struct type *enum_type = disr_field->type ();
8112 std::unordered_map<std::string, ULONGEST> discriminant_map;
8113 for (int i = 0; i < enum_type->num_fields (); ++i)
8114 {
8115 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8116 {
8117 const char *name
8118 = rust_last_path_segment (enum_type->field (i).name ());
8119 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8120 }
8121 }
8122
8123 int n_fields = type->num_fields ();
8124 /* We don't need a range entry for the discriminant, but we do
8125 need one for every other field, as there is no default
8126 variant. */
8127 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8128 discriminant_range,
8129 n_fields - 1);
8130 /* Skip the discriminant here. */
8131 for (int i = 1; i < n_fields; ++i)
8132 {
8133 /* Find the final word in the name of this variant's type.
8134 That name can be used to look up the correct
8135 discriminant. */
8136 const char *variant_name
8137 = rust_last_path_segment (type->field (i).type ()->name ());
8138
8139 auto iter = discriminant_map.find (variant_name);
8140 if (iter != discriminant_map.end ())
8141 {
8142 ranges[i - 1].low = iter->second;
8143 ranges[i - 1].high = iter->second;
8144 }
8145
8146 /* In Rust, each element should have the size of the
8147 enclosing enum. */
8148 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
8149
8150 /* Remove the discriminant field, if it exists. */
8151 struct type *sub_type = type->field (i).type ();
8152 if (sub_type->num_fields () > 0)
8153 {
8154 sub_type->set_num_fields (sub_type->num_fields () - 1);
8155 sub_type->set_fields (sub_type->fields () + 1);
8156 }
8157 type->field (i).set_name (variant_name);
8158 sub_type->set_name
8159 (rust_fully_qualify (&objfile->objfile_obstack,
8160 type->name (), variant_name));
8161 }
8162
8163 /* Indicate that this is a variant type. */
8164 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8165 gdb::array_view<discriminant_range> (ranges,
8166 n_fields - 1));
8167 }
8168 }
8169
8170 /* Rewrite some Rust unions to be structures with variants parts. */
8171
8172 static void
8173 rust_union_quirks (struct dwarf2_cu *cu)
8174 {
8175 gdb_assert (cu->per_cu->lang == language_rust);
8176 for (type *type_ : cu->rust_unions)
8177 quirk_rust_enum (type_, cu->per_objfile->objfile);
8178 /* We don't need this any more. */
8179 cu->rust_unions.clear ();
8180 }
8181
8182 /* See read.h. */
8183
8184 type_unit_group_unshareable *
8185 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8186 {
8187 auto iter = this->m_type_units.find (tu_group);
8188 if (iter != this->m_type_units.end ())
8189 return iter->second.get ();
8190
8191 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8192 type_unit_group_unshareable *result = uniq.get ();
8193 this->m_type_units[tu_group] = std::move (uniq);
8194 return result;
8195 }
8196
8197 struct type *
8198 dwarf2_per_objfile::get_type_for_signatured_type
8199 (signatured_type *sig_type) const
8200 {
8201 auto iter = this->m_type_map.find (sig_type);
8202 if (iter == this->m_type_map.end ())
8203 return nullptr;
8204
8205 return iter->second;
8206 }
8207
8208 void dwarf2_per_objfile::set_type_for_signatured_type
8209 (signatured_type *sig_type, struct type *type)
8210 {
8211 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8212
8213 this->m_type_map[sig_type] = type;
8214 }
8215
8216 /* A helper function for computing the list of all symbol tables
8217 included by PER_CU. */
8218
8219 static void
8220 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8221 htab_t all_children, htab_t all_type_symtabs,
8222 dwarf2_per_cu_data *per_cu,
8223 dwarf2_per_objfile *per_objfile,
8224 struct compunit_symtab *immediate_parent)
8225 {
8226 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8227 if (*slot != NULL)
8228 {
8229 /* This inclusion and its children have been processed. */
8230 return;
8231 }
8232
8233 *slot = per_cu;
8234
8235 /* Only add a CU if it has a symbol table. */
8236 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8237 if (cust != NULL)
8238 {
8239 /* If this is a type unit only add its symbol table if we haven't
8240 seen it yet (type unit per_cu's can share symtabs). */
8241 if (per_cu->is_debug_types)
8242 {
8243 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8244 if (*slot == NULL)
8245 {
8246 *slot = cust;
8247 result->push_back (cust);
8248 if (cust->user == NULL)
8249 cust->user = immediate_parent;
8250 }
8251 }
8252 else
8253 {
8254 result->push_back (cust);
8255 if (cust->user == NULL)
8256 cust->user = immediate_parent;
8257 }
8258 }
8259
8260 if (!per_cu->imported_symtabs_empty ())
8261 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8262 {
8263 recursively_compute_inclusions (result, all_children,
8264 all_type_symtabs, ptr, per_objfile,
8265 cust);
8266 }
8267 }
8268
8269 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8270 PER_CU. */
8271
8272 static void
8273 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8274 dwarf2_per_objfile *per_objfile)
8275 {
8276 gdb_assert (! per_cu->is_debug_types);
8277
8278 if (!per_cu->imported_symtabs_empty ())
8279 {
8280 int len;
8281 std::vector<compunit_symtab *> result_symtabs;
8282 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8283
8284 /* If we don't have a symtab, we can just skip this case. */
8285 if (cust == NULL)
8286 return;
8287
8288 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8289 htab_eq_pointer,
8290 NULL, xcalloc, xfree));
8291 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8292 htab_eq_pointer,
8293 NULL, xcalloc, xfree));
8294
8295 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8296 {
8297 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8298 all_type_symtabs.get (), ptr,
8299 per_objfile, cust);
8300 }
8301
8302 /* Now we have a transitive closure of all the included symtabs. */
8303 len = result_symtabs.size ();
8304 cust->includes
8305 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8306 struct compunit_symtab *, len + 1);
8307 memcpy (cust->includes, result_symtabs.data (),
8308 len * sizeof (compunit_symtab *));
8309 cust->includes[len] = NULL;
8310 }
8311 }
8312
8313 /* Compute the 'includes' field for the symtabs of all the CUs we just
8314 read. */
8315
8316 static void
8317 process_cu_includes (dwarf2_per_objfile *per_objfile)
8318 {
8319 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8320 {
8321 if (! iter->is_debug_types)
8322 compute_compunit_symtab_includes (iter, per_objfile);
8323 }
8324
8325 per_objfile->per_bfd->just_read_cus.clear ();
8326 }
8327
8328 /* Generate full symbol information for CU, whose DIEs have
8329 already been loaded into memory. */
8330
8331 static void
8332 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8333 {
8334 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8335 struct objfile *objfile = per_objfile->objfile;
8336 struct gdbarch *gdbarch = objfile->arch ();
8337 CORE_ADDR lowpc, highpc;
8338 struct compunit_symtab *cust;
8339 CORE_ADDR baseaddr;
8340 struct block *static_block;
8341 CORE_ADDR addr;
8342
8343 baseaddr = objfile->text_section_offset ();
8344
8345 /* Clear the list here in case something was left over. */
8346 cu->method_list.clear ();
8347
8348 dwarf2_find_base_address (cu->dies, cu);
8349
8350 /* Before we start reading the top-level DIE, ensure it has a valid tag
8351 type. */
8352 switch (cu->dies->tag)
8353 {
8354 case DW_TAG_compile_unit:
8355 case DW_TAG_partial_unit:
8356 case DW_TAG_type_unit:
8357 break;
8358 default:
8359 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8360 dwarf_tag_name (cu->dies->tag),
8361 sect_offset_str (cu->per_cu->sect_off),
8362 objfile_name (per_objfile->objfile));
8363 }
8364
8365 /* Do line number decoding in read_file_scope () */
8366 process_die (cu->dies, cu);
8367
8368 /* For now fudge the Go package. */
8369 if (cu->per_cu->lang == language_go)
8370 fixup_go_packaging (cu);
8371
8372 /* Now that we have processed all the DIEs in the CU, all the types
8373 should be complete, and it should now be safe to compute all of the
8374 physnames. */
8375 compute_delayed_physnames (cu);
8376
8377 if (cu->per_cu->lang == language_rust)
8378 rust_union_quirks (cu);
8379
8380 /* Some compilers don't define a DW_AT_high_pc attribute for the
8381 compilation unit. If the DW_AT_high_pc is missing, synthesize
8382 it, by scanning the DIE's below the compilation unit. */
8383 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8384
8385 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8386 static_block
8387 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8388
8389 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8390 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8391 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8392 addrmap to help ensure it has an accurate map of pc values belonging to
8393 this comp unit. */
8394 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8395
8396 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8397 (static_block, SECT_OFF_TEXT (objfile), 0);
8398
8399 if (cust != NULL)
8400 {
8401 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8402
8403 /* Set symtab language to language from DW_AT_language. If the
8404 compilation is from a C file generated by language preprocessors, do
8405 not set the language if it was already deduced by start_subfile. */
8406 if (!(cu->per_cu->lang == language_c
8407 && cust->primary_filetab ()->language () != language_unknown))
8408 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8409
8410 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8411 produce DW_AT_location with location lists but it can be possibly
8412 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8413 there were bugs in prologue debug info, fixed later in GCC-4.5
8414 by "unwind info for epilogues" patch (which is not directly related).
8415
8416 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8417 needed, it would be wrong due to missing DW_AT_producer there.
8418
8419 Still one can confuse GDB by using non-standard GCC compilation
8420 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8421 */
8422 if (cu->has_loclist && gcc_4_minor >= 5)
8423 cust->set_locations_valid (true);
8424
8425 if (gcc_4_minor >= 5)
8426 cust->set_epilogue_unwind_valid (true);
8427
8428 cust->set_call_site_htab (cu->call_site_htab);
8429 }
8430
8431 per_objfile->set_symtab (cu->per_cu, cust);
8432
8433 /* Push it for inclusion processing later. */
8434 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8435
8436 /* Not needed any more. */
8437 cu->reset_builder ();
8438 }
8439
8440 /* Generate full symbol information for type unit CU, whose DIEs have
8441 already been loaded into memory. */
8442
8443 static void
8444 process_full_type_unit (dwarf2_cu *cu,
8445 enum language pretend_language)
8446 {
8447 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8448 struct objfile *objfile = per_objfile->objfile;
8449 struct compunit_symtab *cust;
8450 struct signatured_type *sig_type;
8451
8452 gdb_assert (cu->per_cu->is_debug_types);
8453 sig_type = (struct signatured_type *) cu->per_cu;
8454
8455 /* Clear the list here in case something was left over. */
8456 cu->method_list.clear ();
8457
8458 /* The symbol tables are set up in read_type_unit_scope. */
8459 process_die (cu->dies, cu);
8460
8461 /* For now fudge the Go package. */
8462 if (cu->per_cu->lang == language_go)
8463 fixup_go_packaging (cu);
8464
8465 /* Now that we have processed all the DIEs in the CU, all the types
8466 should be complete, and it should now be safe to compute all of the
8467 physnames. */
8468 compute_delayed_physnames (cu);
8469
8470 if (cu->per_cu->lang == language_rust)
8471 rust_union_quirks (cu);
8472
8473 /* TUs share symbol tables.
8474 If this is the first TU to use this symtab, complete the construction
8475 of it with end_expandable_symtab. Otherwise, complete the addition of
8476 this TU's symbols to the existing symtab. */
8477 type_unit_group_unshareable *tug_unshare =
8478 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8479 if (tug_unshare->compunit_symtab == NULL)
8480 {
8481 buildsym_compunit *builder = cu->get_builder ();
8482 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8483 tug_unshare->compunit_symtab = cust;
8484
8485 if (cust != NULL)
8486 {
8487 /* Set symtab language to language from DW_AT_language. If the
8488 compilation is from a C file generated by language preprocessors,
8489 do not set the language if it was already deduced by
8490 start_subfile. */
8491 if (!(cu->per_cu->lang == language_c
8492 && cust->primary_filetab ()->language () != language_c))
8493 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8494 }
8495 }
8496 else
8497 {
8498 cu->get_builder ()->augment_type_symtab ();
8499 cust = tug_unshare->compunit_symtab;
8500 }
8501
8502 per_objfile->set_symtab (cu->per_cu, cust);
8503
8504 /* Not needed any more. */
8505 cu->reset_builder ();
8506 }
8507
8508 /* Process an imported unit DIE. */
8509
8510 static void
8511 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8512 {
8513 struct attribute *attr;
8514
8515 /* For now we don't handle imported units in type units. */
8516 if (cu->per_cu->is_debug_types)
8517 {
8518 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8519 " supported in type units [in module %s]"),
8520 objfile_name (cu->per_objfile->objfile));
8521 }
8522
8523 attr = dwarf2_attr (die, DW_AT_import, cu);
8524 if (attr != NULL)
8525 {
8526 sect_offset sect_off = attr->get_ref_die_offset ();
8527 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8529 dwarf2_per_cu_data *per_cu
8530 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8531 per_objfile->per_bfd);
8532
8533 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8534 into another compilation unit, at root level. Regard this as a hint,
8535 and ignore it. */
8536 if (die->parent && die->parent->parent == NULL
8537 && per_cu->unit_type == DW_UT_compile
8538 && per_cu->lang == language_cplus)
8539 return;
8540
8541 /* If necessary, add it to the queue and load its DIEs. */
8542 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8543 cu->per_cu->lang))
8544 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8545 false, cu->per_cu->lang);
8546
8547 cu->per_cu->imported_symtabs_push (per_cu);
8548 }
8549 }
8550
8551 /* RAII object that represents a process_die scope: i.e.,
8552 starts/finishes processing a DIE. */
8553 class process_die_scope
8554 {
8555 public:
8556 process_die_scope (die_info *die, dwarf2_cu *cu)
8557 : m_die (die), m_cu (cu)
8558 {
8559 /* We should only be processing DIEs not already in process. */
8560 gdb_assert (!m_die->in_process);
8561 m_die->in_process = true;
8562 }
8563
8564 ~process_die_scope ()
8565 {
8566 m_die->in_process = false;
8567
8568 /* If we're done processing the DIE for the CU that owns the line
8569 header, we don't need the line header anymore. */
8570 if (m_cu->line_header_die_owner == m_die)
8571 {
8572 delete m_cu->line_header;
8573 m_cu->line_header = NULL;
8574 m_cu->line_header_die_owner = NULL;
8575 }
8576 }
8577
8578 private:
8579 die_info *m_die;
8580 dwarf2_cu *m_cu;
8581 };
8582
8583 /* Process a die and its children. */
8584
8585 static void
8586 process_die (struct die_info *die, struct dwarf2_cu *cu)
8587 {
8588 process_die_scope scope (die, cu);
8589
8590 switch (die->tag)
8591 {
8592 case DW_TAG_padding:
8593 break;
8594 case DW_TAG_compile_unit:
8595 case DW_TAG_partial_unit:
8596 read_file_scope (die, cu);
8597 break;
8598 case DW_TAG_type_unit:
8599 read_type_unit_scope (die, cu);
8600 break;
8601 case DW_TAG_subprogram:
8602 /* Nested subprograms in Fortran get a prefix. */
8603 if (cu->per_cu->lang == language_fortran
8604 && die->parent != NULL
8605 && die->parent->tag == DW_TAG_subprogram)
8606 cu->processing_has_namespace_info = true;
8607 /* Fall through. */
8608 case DW_TAG_inlined_subroutine:
8609 read_func_scope (die, cu);
8610 break;
8611 case DW_TAG_lexical_block:
8612 case DW_TAG_try_block:
8613 case DW_TAG_catch_block:
8614 read_lexical_block_scope (die, cu);
8615 break;
8616 case DW_TAG_call_site:
8617 case DW_TAG_GNU_call_site:
8618 read_call_site_scope (die, cu);
8619 break;
8620 case DW_TAG_class_type:
8621 case DW_TAG_interface_type:
8622 case DW_TAG_structure_type:
8623 case DW_TAG_union_type:
8624 case DW_TAG_namelist:
8625 process_structure_scope (die, cu);
8626 break;
8627 case DW_TAG_enumeration_type:
8628 process_enumeration_scope (die, cu);
8629 break;
8630
8631 /* These dies have a type, but processing them does not create
8632 a symbol or recurse to process the children. Therefore we can
8633 read them on-demand through read_type_die. */
8634 case DW_TAG_subroutine_type:
8635 case DW_TAG_set_type:
8636 case DW_TAG_pointer_type:
8637 case DW_TAG_ptr_to_member_type:
8638 case DW_TAG_reference_type:
8639 case DW_TAG_rvalue_reference_type:
8640 case DW_TAG_string_type:
8641 break;
8642
8643 case DW_TAG_array_type:
8644 /* We only need to handle this case for Ada -- in other
8645 languages, it's normal for the compiler to emit a typedef
8646 instead. */
8647 if (cu->per_cu->lang != language_ada)
8648 break;
8649 /* FALLTHROUGH */
8650 case DW_TAG_base_type:
8651 case DW_TAG_subrange_type:
8652 case DW_TAG_generic_subrange:
8653 case DW_TAG_typedef:
8654 /* Add a typedef symbol for the type definition, if it has a
8655 DW_AT_name. */
8656 new_symbol (die, read_type_die (die, cu), cu);
8657 break;
8658 case DW_TAG_common_block:
8659 read_common_block (die, cu);
8660 break;
8661 case DW_TAG_common_inclusion:
8662 break;
8663 case DW_TAG_namespace:
8664 cu->processing_has_namespace_info = true;
8665 read_namespace (die, cu);
8666 break;
8667 case DW_TAG_module:
8668 cu->processing_has_namespace_info = true;
8669 read_module (die, cu);
8670 break;
8671 case DW_TAG_imported_declaration:
8672 cu->processing_has_namespace_info = true;
8673 if (read_namespace_alias (die, cu))
8674 break;
8675 /* The declaration is not a global namespace alias. */
8676 /* Fall through. */
8677 case DW_TAG_imported_module:
8678 cu->processing_has_namespace_info = true;
8679 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8680 || cu->per_cu->lang != language_fortran))
8681 complaint (_("Tag '%s' has unexpected children"),
8682 dwarf_tag_name (die->tag));
8683 read_import_statement (die, cu);
8684 break;
8685
8686 case DW_TAG_imported_unit:
8687 process_imported_unit_die (die, cu);
8688 break;
8689
8690 case DW_TAG_variable:
8691 read_variable (die, cu);
8692 break;
8693
8694 default:
8695 new_symbol (die, NULL, cu);
8696 break;
8697 }
8698 }
8699 \f
8700 /* DWARF name computation. */
8701
8702 /* A helper function for dwarf2_compute_name which determines whether DIE
8703 needs to have the name of the scope prepended to the name listed in the
8704 die. */
8705
8706 static int
8707 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8708 {
8709 struct attribute *attr;
8710
8711 switch (die->tag)
8712 {
8713 case DW_TAG_namespace:
8714 case DW_TAG_typedef:
8715 case DW_TAG_class_type:
8716 case DW_TAG_interface_type:
8717 case DW_TAG_structure_type:
8718 case DW_TAG_union_type:
8719 case DW_TAG_enumeration_type:
8720 case DW_TAG_enumerator:
8721 case DW_TAG_subprogram:
8722 case DW_TAG_inlined_subroutine:
8723 case DW_TAG_member:
8724 case DW_TAG_imported_declaration:
8725 return 1;
8726
8727 case DW_TAG_variable:
8728 case DW_TAG_constant:
8729 /* We only need to prefix "globally" visible variables. These include
8730 any variable marked with DW_AT_external or any variable that
8731 lives in a namespace. [Variables in anonymous namespaces
8732 require prefixing, but they are not DW_AT_external.] */
8733
8734 if (dwarf2_attr (die, DW_AT_specification, cu))
8735 {
8736 struct dwarf2_cu *spec_cu = cu;
8737
8738 return die_needs_namespace (die_specification (die, &spec_cu),
8739 spec_cu);
8740 }
8741
8742 attr = dwarf2_attr (die, DW_AT_external, cu);
8743 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8744 && die->parent->tag != DW_TAG_module)
8745 return 0;
8746 /* A variable in a lexical block of some kind does not need a
8747 namespace, even though in C++ such variables may be external
8748 and have a mangled name. */
8749 if (die->parent->tag == DW_TAG_lexical_block
8750 || die->parent->tag == DW_TAG_try_block
8751 || die->parent->tag == DW_TAG_catch_block
8752 || die->parent->tag == DW_TAG_subprogram)
8753 return 0;
8754 return 1;
8755
8756 default:
8757 return 0;
8758 }
8759 }
8760
8761 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8762 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8763 defined for the given DIE. */
8764
8765 static struct attribute *
8766 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8767 {
8768 struct attribute *attr;
8769
8770 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8771 if (attr == NULL)
8772 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8773
8774 return attr;
8775 }
8776
8777 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8778 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8779 defined for the given DIE. */
8780
8781 static const char *
8782 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8783 {
8784 const char *linkage_name;
8785
8786 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8787 if (linkage_name == NULL)
8788 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8789
8790 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8791 See https://github.com/rust-lang/rust/issues/32925. */
8792 if (cu->per_cu->lang == language_rust && linkage_name != NULL
8793 && strchr (linkage_name, '{') != NULL)
8794 linkage_name = NULL;
8795
8796 return linkage_name;
8797 }
8798
8799 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8800 compute the physname for the object, which include a method's:
8801 - formal parameters (C++),
8802 - receiver type (Go),
8803
8804 The term "physname" is a bit confusing.
8805 For C++, for example, it is the demangled name.
8806 For Go, for example, it's the mangled name.
8807
8808 For Ada, return the DIE's linkage name rather than the fully qualified
8809 name. PHYSNAME is ignored..
8810
8811 The result is allocated on the objfile->per_bfd's obstack and
8812 canonicalized. */
8813
8814 static const char *
8815 dwarf2_compute_name (const char *name,
8816 struct die_info *die, struct dwarf2_cu *cu,
8817 int physname)
8818 {
8819 struct objfile *objfile = cu->per_objfile->objfile;
8820
8821 if (name == NULL)
8822 name = dwarf2_name (die, cu);
8823
8824 enum language lang = cu->per_cu->lang;
8825
8826 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8827 but otherwise compute it by typename_concat inside GDB.
8828 FIXME: Actually this is not really true, or at least not always true.
8829 It's all very confusing. compute_and_set_names doesn't try to demangle
8830 Fortran names because there is no mangling standard. So new_symbol
8831 will set the demangled name to the result of dwarf2_full_name, and it is
8832 the demangled name that GDB uses if it exists. */
8833 if (lang == language_ada
8834 || (lang == language_fortran && physname))
8835 {
8836 /* For Ada unit, we prefer the linkage name over the name, as
8837 the former contains the exported name, which the user expects
8838 to be able to reference. Ideally, we want the user to be able
8839 to reference this entity using either natural or linkage name,
8840 but we haven't started looking at this enhancement yet. */
8841 const char *linkage_name = dw2_linkage_name (die, cu);
8842
8843 if (linkage_name != NULL)
8844 return linkage_name;
8845 }
8846
8847 /* These are the only languages we know how to qualify names in. */
8848 if (name != NULL
8849 && (lang == language_cplus
8850 || lang == language_fortran || lang == language_d
8851 || lang == language_rust))
8852 {
8853 if (die_needs_namespace (die, cu))
8854 {
8855 const char *prefix;
8856 const char *canonical_name = NULL;
8857
8858 string_file buf;
8859
8860 prefix = determine_prefix (die, cu);
8861 if (*prefix != '\0')
8862 {
8863 gdb::unique_xmalloc_ptr<char> prefixed_name
8864 (typename_concat (NULL, prefix, name, physname, cu));
8865
8866 buf.puts (prefixed_name.get ());
8867 }
8868 else
8869 buf.puts (name);
8870
8871 /* Template parameters may be specified in the DIE's DW_AT_name, or
8872 as children with DW_TAG_template_type_param or
8873 DW_TAG_value_type_param. If the latter, add them to the name
8874 here. If the name already has template parameters, then
8875 skip this step; some versions of GCC emit both, and
8876 it is more efficient to use the pre-computed name.
8877
8878 Something to keep in mind about this process: it is very
8879 unlikely, or in some cases downright impossible, to produce
8880 something that will match the mangled name of a function.
8881 If the definition of the function has the same debug info,
8882 we should be able to match up with it anyway. But fallbacks
8883 using the minimal symbol, for instance to find a method
8884 implemented in a stripped copy of libstdc++, will not work.
8885 If we do not have debug info for the definition, we will have to
8886 match them up some other way.
8887
8888 When we do name matching there is a related problem with function
8889 templates; two instantiated function templates are allowed to
8890 differ only by their return types, which we do not add here. */
8891
8892 if (lang == language_cplus && strchr (name, '<') == NULL)
8893 {
8894 struct attribute *attr;
8895 struct die_info *child;
8896 int first = 1;
8897
8898 die->building_fullname = 1;
8899
8900 for (child = die->child; child != NULL; child = child->sibling)
8901 {
8902 struct type *type;
8903 LONGEST value;
8904 const gdb_byte *bytes;
8905 struct dwarf2_locexpr_baton *baton;
8906 struct value *v;
8907
8908 if (child->tag != DW_TAG_template_type_param
8909 && child->tag != DW_TAG_template_value_param)
8910 continue;
8911
8912 if (first)
8913 {
8914 buf.puts ("<");
8915 first = 0;
8916 }
8917 else
8918 buf.puts (", ");
8919
8920 attr = dwarf2_attr (child, DW_AT_type, cu);
8921 if (attr == NULL)
8922 {
8923 complaint (_("template parameter missing DW_AT_type"));
8924 buf.puts ("UNKNOWN_TYPE");
8925 continue;
8926 }
8927 type = die_type (child, cu);
8928
8929 if (child->tag == DW_TAG_template_type_param)
8930 {
8931 cu->language_defn->print_type (type, "", &buf, -1, 0,
8932 &type_print_raw_options);
8933 continue;
8934 }
8935
8936 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8937 if (attr == NULL)
8938 {
8939 complaint (_("template parameter missing "
8940 "DW_AT_const_value"));
8941 buf.puts ("UNKNOWN_VALUE");
8942 continue;
8943 }
8944
8945 dwarf2_const_value_attr (attr, type, name,
8946 &cu->comp_unit_obstack, cu,
8947 &value, &bytes, &baton);
8948
8949 if (type->has_no_signedness ())
8950 /* GDB prints characters as NUMBER 'CHAR'. If that's
8951 changed, this can use value_print instead. */
8952 cu->language_defn->printchar (value, type, &buf);
8953 else
8954 {
8955 struct value_print_options opts;
8956
8957 if (baton != NULL)
8958 v = dwarf2_evaluate_loc_desc (type, NULL,
8959 baton->data,
8960 baton->size,
8961 baton->per_cu,
8962 baton->per_objfile);
8963 else if (bytes != NULL)
8964 {
8965 v = allocate_value (type);
8966 memcpy (value_contents_writeable (v).data (), bytes,
8967 TYPE_LENGTH (type));
8968 }
8969 else
8970 v = value_from_longest (type, value);
8971
8972 /* Specify decimal so that we do not depend on
8973 the radix. */
8974 get_formatted_print_options (&opts, 'd');
8975 opts.raw = 1;
8976 value_print (v, &buf, &opts);
8977 release_value (v);
8978 }
8979 }
8980
8981 die->building_fullname = 0;
8982
8983 if (!first)
8984 {
8985 /* Close the argument list, with a space if necessary
8986 (nested templates). */
8987 if (!buf.empty () && buf.string ().back () == '>')
8988 buf.puts (" >");
8989 else
8990 buf.puts (">");
8991 }
8992 }
8993
8994 /* For C++ methods, append formal parameter type
8995 information, if PHYSNAME. */
8996
8997 if (physname && die->tag == DW_TAG_subprogram
8998 && lang == language_cplus)
8999 {
9000 struct type *type = read_type_die (die, cu);
9001
9002 c_type_print_args (type, &buf, 1, lang,
9003 &type_print_raw_options);
9004
9005 if (lang == language_cplus)
9006 {
9007 /* Assume that an artificial first parameter is
9008 "this", but do not crash if it is not. RealView
9009 marks unnamed (and thus unused) parameters as
9010 artificial; there is no way to differentiate
9011 the two cases. */
9012 if (type->num_fields () > 0
9013 && TYPE_FIELD_ARTIFICIAL (type, 0)
9014 && type->field (0).type ()->code () == TYPE_CODE_PTR
9015 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9016 buf.puts (" const");
9017 }
9018 }
9019
9020 const std::string &intermediate_name = buf.string ();
9021
9022 if (lang == language_cplus)
9023 canonical_name
9024 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9025 objfile);
9026
9027 /* If we only computed INTERMEDIATE_NAME, or if
9028 INTERMEDIATE_NAME is already canonical, then we need to
9029 intern it. */
9030 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9031 name = objfile->intern (intermediate_name);
9032 else
9033 name = canonical_name;
9034 }
9035 }
9036
9037 return name;
9038 }
9039
9040 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9041 If scope qualifiers are appropriate they will be added. The result
9042 will be allocated on the storage_obstack, or NULL if the DIE does
9043 not have a name. NAME may either be from a previous call to
9044 dwarf2_name or NULL.
9045
9046 The output string will be canonicalized (if C++). */
9047
9048 static const char *
9049 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9050 {
9051 return dwarf2_compute_name (name, die, cu, 0);
9052 }
9053
9054 /* Construct a physname for the given DIE in CU. NAME may either be
9055 from a previous call to dwarf2_name or NULL. The result will be
9056 allocated on the objfile_objstack or NULL if the DIE does not have a
9057 name.
9058
9059 The output string will be canonicalized (if C++). */
9060
9061 static const char *
9062 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9063 {
9064 struct objfile *objfile = cu->per_objfile->objfile;
9065 const char *retval, *mangled = NULL, *canon = NULL;
9066 int need_copy = 1;
9067
9068 /* In this case dwarf2_compute_name is just a shortcut not building anything
9069 on its own. */
9070 if (!die_needs_namespace (die, cu))
9071 return dwarf2_compute_name (name, die, cu, 1);
9072
9073 if (cu->per_cu->lang != language_rust)
9074 mangled = dw2_linkage_name (die, cu);
9075
9076 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9077 has computed. */
9078 gdb::unique_xmalloc_ptr<char> demangled;
9079 if (mangled != NULL)
9080 {
9081 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9082 {
9083 /* Do nothing (do not demangle the symbol name). */
9084 }
9085 else
9086 {
9087 /* Use DMGL_RET_DROP for C++ template functions to suppress
9088 their return type. It is easier for GDB users to search
9089 for such functions as `name(params)' than `long name(params)'.
9090 In such case the minimal symbol names do not match the full
9091 symbol names but for template functions there is never a need
9092 to look up their definition from their declaration so
9093 the only disadvantage remains the minimal symbol variant
9094 `long name(params)' does not have the proper inferior type. */
9095 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9096 | DMGL_RET_DROP));
9097 }
9098 if (demangled)
9099 canon = demangled.get ();
9100 else
9101 {
9102 canon = mangled;
9103 need_copy = 0;
9104 }
9105 }
9106
9107 if (canon == NULL || check_physname)
9108 {
9109 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9110
9111 if (canon != NULL && strcmp (physname, canon) != 0)
9112 {
9113 /* It may not mean a bug in GDB. The compiler could also
9114 compute DW_AT_linkage_name incorrectly. But in such case
9115 GDB would need to be bug-to-bug compatible. */
9116
9117 complaint (_("Computed physname <%s> does not match demangled <%s> "
9118 "(from linkage <%s>) - DIE at %s [in module %s]"),
9119 physname, canon, mangled, sect_offset_str (die->sect_off),
9120 objfile_name (objfile));
9121
9122 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9123 is available here - over computed PHYSNAME. It is safer
9124 against both buggy GDB and buggy compilers. */
9125
9126 retval = canon;
9127 }
9128 else
9129 {
9130 retval = physname;
9131 need_copy = 0;
9132 }
9133 }
9134 else
9135 retval = canon;
9136
9137 if (need_copy)
9138 retval = objfile->intern (retval);
9139
9140 return retval;
9141 }
9142
9143 /* Inspect DIE in CU for a namespace alias. If one exists, record
9144 a new symbol for it.
9145
9146 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9147
9148 static int
9149 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9150 {
9151 struct attribute *attr;
9152
9153 /* If the die does not have a name, this is not a namespace
9154 alias. */
9155 attr = dwarf2_attr (die, DW_AT_name, cu);
9156 if (attr != NULL)
9157 {
9158 int num;
9159 struct die_info *d = die;
9160 struct dwarf2_cu *imported_cu = cu;
9161
9162 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9163 keep inspecting DIEs until we hit the underlying import. */
9164 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9165 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9166 {
9167 attr = dwarf2_attr (d, DW_AT_import, cu);
9168 if (attr == NULL)
9169 break;
9170
9171 d = follow_die_ref (d, attr, &imported_cu);
9172 if (d->tag != DW_TAG_imported_declaration)
9173 break;
9174 }
9175
9176 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9177 {
9178 complaint (_("DIE at %s has too many recursively imported "
9179 "declarations"), sect_offset_str (d->sect_off));
9180 return 0;
9181 }
9182
9183 if (attr != NULL)
9184 {
9185 struct type *type;
9186 sect_offset sect_off = attr->get_ref_die_offset ();
9187
9188 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
9189 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
9190 {
9191 /* This declaration is a global namespace alias. Add
9192 a symbol for it whose type is the aliased namespace. */
9193 new_symbol (die, type, cu);
9194 return 1;
9195 }
9196 }
9197 }
9198
9199 return 0;
9200 }
9201
9202 /* Return the using directives repository (global or local?) to use in the
9203 current context for CU.
9204
9205 For Ada, imported declarations can materialize renamings, which *may* be
9206 global. However it is impossible (for now?) in DWARF to distinguish
9207 "external" imported declarations and "static" ones. As all imported
9208 declarations seem to be static in all other languages, make them all CU-wide
9209 global only in Ada. */
9210
9211 static struct using_direct **
9212 using_directives (struct dwarf2_cu *cu)
9213 {
9214 if (cu->per_cu->lang == language_ada
9215 && cu->get_builder ()->outermost_context_p ())
9216 return cu->get_builder ()->get_global_using_directives ();
9217 else
9218 return cu->get_builder ()->get_local_using_directives ();
9219 }
9220
9221 /* Read the import statement specified by the given die and record it. */
9222
9223 static void
9224 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9225 {
9226 struct objfile *objfile = cu->per_objfile->objfile;
9227 struct attribute *import_attr;
9228 struct die_info *imported_die, *child_die;
9229 struct dwarf2_cu *imported_cu;
9230 const char *imported_name;
9231 const char *imported_name_prefix;
9232 const char *canonical_name;
9233 const char *import_alias;
9234 const char *imported_declaration = NULL;
9235 const char *import_prefix;
9236 std::vector<const char *> excludes;
9237
9238 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9239 if (import_attr == NULL)
9240 {
9241 complaint (_("Tag '%s' has no DW_AT_import"),
9242 dwarf_tag_name (die->tag));
9243 return;
9244 }
9245
9246 imported_cu = cu;
9247 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9248 imported_name = dwarf2_name (imported_die, imported_cu);
9249 if (imported_name == NULL)
9250 {
9251 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9252
9253 The import in the following code:
9254 namespace A
9255 {
9256 typedef int B;
9257 }
9258
9259 int main ()
9260 {
9261 using A::B;
9262 B b;
9263 return b;
9264 }
9265
9266 ...
9267 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9268 <52> DW_AT_decl_file : 1
9269 <53> DW_AT_decl_line : 6
9270 <54> DW_AT_import : <0x75>
9271 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9272 <59> DW_AT_name : B
9273 <5b> DW_AT_decl_file : 1
9274 <5c> DW_AT_decl_line : 2
9275 <5d> DW_AT_type : <0x6e>
9276 ...
9277 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9278 <76> DW_AT_byte_size : 4
9279 <77> DW_AT_encoding : 5 (signed)
9280
9281 imports the wrong die ( 0x75 instead of 0x58 ).
9282 This case will be ignored until the gcc bug is fixed. */
9283 return;
9284 }
9285
9286 /* Figure out the local name after import. */
9287 import_alias = dwarf2_name (die, cu);
9288
9289 /* Figure out where the statement is being imported to. */
9290 import_prefix = determine_prefix (die, cu);
9291
9292 /* Figure out what the scope of the imported die is and prepend it
9293 to the name of the imported die. */
9294 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9295
9296 if (imported_die->tag != DW_TAG_namespace
9297 && imported_die->tag != DW_TAG_module)
9298 {
9299 imported_declaration = imported_name;
9300 canonical_name = imported_name_prefix;
9301 }
9302 else if (strlen (imported_name_prefix) > 0)
9303 canonical_name = obconcat (&objfile->objfile_obstack,
9304 imported_name_prefix,
9305 (cu->per_cu->lang == language_d
9306 ? "."
9307 : "::"),
9308 imported_name, (char *) NULL);
9309 else
9310 canonical_name = imported_name;
9311
9312 if (die->tag == DW_TAG_imported_module
9313 && cu->per_cu->lang == language_fortran)
9314 for (child_die = die->child; child_die && child_die->tag;
9315 child_die = child_die->sibling)
9316 {
9317 /* DWARF-4: A Fortran use statement with a “rename list” may be
9318 represented by an imported module entry with an import attribute
9319 referring to the module and owned entries corresponding to those
9320 entities that are renamed as part of being imported. */
9321
9322 if (child_die->tag != DW_TAG_imported_declaration)
9323 {
9324 complaint (_("child DW_TAG_imported_declaration expected "
9325 "- DIE at %s [in module %s]"),
9326 sect_offset_str (child_die->sect_off),
9327 objfile_name (objfile));
9328 continue;
9329 }
9330
9331 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9332 if (import_attr == NULL)
9333 {
9334 complaint (_("Tag '%s' has no DW_AT_import"),
9335 dwarf_tag_name (child_die->tag));
9336 continue;
9337 }
9338
9339 imported_cu = cu;
9340 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9341 &imported_cu);
9342 imported_name = dwarf2_name (imported_die, imported_cu);
9343 if (imported_name == NULL)
9344 {
9345 complaint (_("child DW_TAG_imported_declaration has unknown "
9346 "imported name - DIE at %s [in module %s]"),
9347 sect_offset_str (child_die->sect_off),
9348 objfile_name (objfile));
9349 continue;
9350 }
9351
9352 excludes.push_back (imported_name);
9353
9354 process_die (child_die, cu);
9355 }
9356
9357 add_using_directive (using_directives (cu),
9358 import_prefix,
9359 canonical_name,
9360 import_alias,
9361 imported_declaration,
9362 excludes,
9363 0,
9364 &objfile->objfile_obstack);
9365 }
9366
9367 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9368 types, but gives them a size of zero. Starting with version 14,
9369 ICC is compatible with GCC. */
9370
9371 static bool
9372 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9373 {
9374 if (!cu->checked_producer)
9375 check_producer (cu);
9376
9377 return cu->producer_is_icc_lt_14;
9378 }
9379
9380 /* ICC generates a DW_AT_type for C void functions. This was observed on
9381 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9382 which says that void functions should not have a DW_AT_type. */
9383
9384 static bool
9385 producer_is_icc (struct dwarf2_cu *cu)
9386 {
9387 if (!cu->checked_producer)
9388 check_producer (cu);
9389
9390 return cu->producer_is_icc;
9391 }
9392
9393 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9394 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9395 this, it was first present in GCC release 4.3.0. */
9396
9397 static bool
9398 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9399 {
9400 if (!cu->checked_producer)
9401 check_producer (cu);
9402
9403 return cu->producer_is_gcc_lt_4_3;
9404 }
9405
9406 static file_and_directory &
9407 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9408 {
9409 if (cu->per_cu->fnd != nullptr)
9410 return *cu->per_cu->fnd;
9411
9412 /* Find the filename. Do not use dwarf2_name here, since the filename
9413 is not a source language identifier. */
9414 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9415 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9416
9417 if (res.get_comp_dir () == nullptr
9418 && producer_is_gcc_lt_4_3 (cu)
9419 && res.get_name () != nullptr
9420 && IS_ABSOLUTE_PATH (res.get_name ()))
9421 res.set_comp_dir (ldirname (res.get_name ()));
9422
9423 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9424 return *cu->per_cu->fnd;
9425 }
9426
9427 /* Handle DW_AT_stmt_list for a compilation unit.
9428 DIE is the DW_TAG_compile_unit die for CU.
9429 COMP_DIR is the compilation directory. LOWPC is passed to
9430 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9431
9432 static void
9433 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9434 CORE_ADDR lowpc) /* ARI: editCase function */
9435 {
9436 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9437 struct attribute *attr;
9438 struct line_header line_header_local;
9439 hashval_t line_header_local_hash;
9440 void **slot;
9441 int decode_mapping;
9442
9443 gdb_assert (! cu->per_cu->is_debug_types);
9444
9445 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9446 if (attr == NULL || !attr->form_is_unsigned ())
9447 return;
9448
9449 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9450
9451 /* The line header hash table is only created if needed (it exists to
9452 prevent redundant reading of the line table for partial_units).
9453 If we're given a partial_unit, we'll need it. If we're given a
9454 compile_unit, then use the line header hash table if it's already
9455 created, but don't create one just yet. */
9456
9457 if (per_objfile->line_header_hash == NULL
9458 && die->tag == DW_TAG_partial_unit)
9459 {
9460 per_objfile->line_header_hash
9461 .reset (htab_create_alloc (127, line_header_hash_voidp,
9462 line_header_eq_voidp,
9463 htab_delete_entry<line_header>,
9464 xcalloc, xfree));
9465 }
9466
9467 line_header_local.sect_off = line_offset;
9468 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9469 line_header_local_hash = line_header_hash (&line_header_local);
9470 if (per_objfile->line_header_hash != NULL)
9471 {
9472 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9473 &line_header_local,
9474 line_header_local_hash, NO_INSERT);
9475
9476 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9477 is not present in *SLOT (since if there is something in *SLOT then
9478 it will be for a partial_unit). */
9479 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9480 {
9481 gdb_assert (*slot != NULL);
9482 cu->line_header = (struct line_header *) *slot;
9483 return;
9484 }
9485 }
9486
9487 /* dwarf_decode_line_header does not yet provide sufficient information.
9488 We always have to call also dwarf_decode_lines for it. */
9489 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9490 if (lh == NULL)
9491 return;
9492
9493 cu->line_header = lh.release ();
9494 cu->line_header_die_owner = die;
9495
9496 if (per_objfile->line_header_hash == NULL)
9497 slot = NULL;
9498 else
9499 {
9500 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9501 &line_header_local,
9502 line_header_local_hash, INSERT);
9503 gdb_assert (slot != NULL);
9504 }
9505 if (slot != NULL && *slot == NULL)
9506 {
9507 /* This newly decoded line number information unit will be owned
9508 by line_header_hash hash table. */
9509 *slot = cu->line_header;
9510 cu->line_header_die_owner = NULL;
9511 }
9512 else
9513 {
9514 /* We cannot free any current entry in (*slot) as that struct line_header
9515 may be already used by multiple CUs. Create only temporary decoded
9516 line_header for this CU - it may happen at most once for each line
9517 number information unit. And if we're not using line_header_hash
9518 then this is what we want as well. */
9519 gdb_assert (die->tag != DW_TAG_partial_unit);
9520 }
9521 decode_mapping = (die->tag != DW_TAG_partial_unit);
9522 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9523 }
9524
9525 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9526
9527 static void
9528 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9529 {
9530 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9531 struct objfile *objfile = per_objfile->objfile;
9532 struct gdbarch *gdbarch = objfile->arch ();
9533 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9534 CORE_ADDR highpc = ((CORE_ADDR) 0);
9535 struct attribute *attr;
9536 struct die_info *child_die;
9537 CORE_ADDR baseaddr;
9538
9539 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
9540 baseaddr = objfile->text_section_offset ();
9541
9542 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9543
9544 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9545 from finish_block. */
9546 if (lowpc == ((CORE_ADDR) -1))
9547 lowpc = highpc;
9548 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9549
9550 file_and_directory &fnd = find_file_and_directory (die, cu);
9551
9552 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9553 lowpc);
9554
9555 gdb_assert (per_objfile->sym_cu == nullptr);
9556 scoped_restore restore_sym_cu
9557 = make_scoped_restore (&per_objfile->sym_cu, cu);
9558
9559 /* Decode line number information if present. We do this before
9560 processing child DIEs, so that the line header table is available
9561 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9562 HIGHPC are both 0x0, then there won't be any interesting code in
9563 the CU, but a check later on (in
9564 lnp_state_machine::check_line_address) will fail to properly
9565 exclude an entry that was removed via --gc-sections. */
9566 if (lowpc != highpc)
9567 handle_DW_AT_stmt_list (die, cu, lowpc);
9568
9569 /* Process all dies in compilation unit. */
9570 if (die->child != NULL)
9571 {
9572 child_die = die->child;
9573 while (child_die && child_die->tag)
9574 {
9575 process_die (child_die, cu);
9576 child_die = child_die->sibling;
9577 }
9578 }
9579 per_objfile->sym_cu = nullptr;
9580
9581 /* Decode macro information, if present. Dwarf 2 macro information
9582 refers to information in the line number info statement program
9583 header, so we can only read it if we've read the header
9584 successfully. */
9585 attr = dwarf2_attr (die, DW_AT_macros, cu);
9586 if (attr == NULL)
9587 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9588 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9589 {
9590 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9591 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9592
9593 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9594 }
9595 else
9596 {
9597 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9598 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9599 {
9600 unsigned int macro_offset = attr->as_unsigned ();
9601
9602 dwarf_decode_macros (cu, macro_offset, 0);
9603 }
9604 }
9605 }
9606
9607 void
9608 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9609 {
9610 struct type_unit_group *tu_group;
9611 int first_time;
9612 struct attribute *attr;
9613 unsigned int i;
9614 struct signatured_type *sig_type;
9615
9616 gdb_assert (per_cu->is_debug_types);
9617 sig_type = (struct signatured_type *) per_cu;
9618
9619 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9620
9621 /* If we're using .gdb_index (includes -readnow) then
9622 per_cu->type_unit_group may not have been set up yet. */
9623 if (sig_type->type_unit_group == NULL)
9624 sig_type->type_unit_group = get_type_unit_group (this, attr);
9625 tu_group = sig_type->type_unit_group;
9626
9627 /* If we've already processed this stmt_list there's no real need to
9628 do it again, we could fake it and just recreate the part we need
9629 (file name,index -> symtab mapping). If data shows this optimization
9630 is useful we can do it then. */
9631 type_unit_group_unshareable *tug_unshare
9632 = per_objfile->get_type_unit_group_unshareable (tu_group);
9633 first_time = tug_unshare->compunit_symtab == NULL;
9634
9635 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9636 debug info. */
9637 line_header_up lh;
9638 if (attr != NULL && attr->form_is_unsigned ())
9639 {
9640 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9641 lh = dwarf_decode_line_header (line_offset, this);
9642 }
9643 if (lh == NULL)
9644 {
9645 if (first_time)
9646 start_compunit_symtab ("", NULL, 0);
9647 else
9648 {
9649 gdb_assert (tug_unshare->symtabs == NULL);
9650 gdb_assert (m_builder == nullptr);
9651 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9652 m_builder.reset (new struct buildsym_compunit
9653 (cust->objfile (), "",
9654 cust->dirname (),
9655 compunit_language (cust),
9656 0, cust));
9657 list_in_scope = get_builder ()->get_file_symbols ();
9658 }
9659 return;
9660 }
9661
9662 line_header = lh.release ();
9663 line_header_die_owner = die;
9664
9665 if (first_time)
9666 {
9667 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9668
9669 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9670 still initializing it, and our caller (a few levels up)
9671 process_full_type_unit still needs to know if this is the first
9672 time. */
9673
9674 tug_unshare->symtabs
9675 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9676 struct symtab *, line_header->file_names_size ());
9677
9678 auto &file_names = line_header->file_names ();
9679 for (i = 0; i < file_names.size (); ++i)
9680 {
9681 file_entry &fe = file_names[i];
9682 dwarf2_start_subfile (this, fe.name,
9683 fe.include_dir (line_header));
9684 buildsym_compunit *b = get_builder ();
9685 if (b->get_current_subfile ()->symtab == NULL)
9686 {
9687 /* NOTE: start_subfile will recognize when it's been
9688 passed a file it has already seen. So we can't
9689 assume there's a simple mapping from
9690 cu->line_header->file_names to subfiles, plus
9691 cu->line_header->file_names may contain dups. */
9692 const char *name = b->get_current_subfile ()->name.c_str ();
9693 b->get_current_subfile ()->symtab = allocate_symtab (cust, name);
9694 }
9695
9696 fe.symtab = b->get_current_subfile ()->symtab;
9697 tug_unshare->symtabs[i] = fe.symtab;
9698 }
9699 }
9700 else
9701 {
9702 gdb_assert (m_builder == nullptr);
9703 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9704 m_builder.reset (new struct buildsym_compunit
9705 (cust->objfile (), "",
9706 cust->dirname (),
9707 compunit_language (cust),
9708 0, cust));
9709 list_in_scope = get_builder ()->get_file_symbols ();
9710
9711 auto &file_names = line_header->file_names ();
9712 for (i = 0; i < file_names.size (); ++i)
9713 {
9714 file_entry &fe = file_names[i];
9715 fe.symtab = tug_unshare->symtabs[i];
9716 }
9717 }
9718
9719 /* The main symtab is allocated last. Type units don't have DW_AT_name
9720 so they don't have a "real" (so to speak) symtab anyway.
9721 There is later code that will assign the main symtab to all symbols
9722 that don't have one. We need to handle the case of a symbol with a
9723 missing symtab (DW_AT_decl_file) anyway. */
9724 }
9725
9726 /* Process DW_TAG_type_unit.
9727 For TUs we want to skip the first top level sibling if it's not the
9728 actual type being defined by this TU. In this case the first top
9729 level sibling is there to provide context only. */
9730
9731 static void
9732 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9733 {
9734 struct die_info *child_die;
9735
9736 prepare_one_comp_unit (cu, die, language_minimal);
9737
9738 /* Initialize (or reinitialize) the machinery for building symtabs.
9739 We do this before processing child DIEs, so that the line header table
9740 is available for DW_AT_decl_file. */
9741 cu->setup_type_unit_groups (die);
9742
9743 if (die->child != NULL)
9744 {
9745 child_die = die->child;
9746 while (child_die && child_die->tag)
9747 {
9748 process_die (child_die, cu);
9749 child_die = child_die->sibling;
9750 }
9751 }
9752 }
9753 \f
9754 /* DWO/DWP files.
9755
9756 http://gcc.gnu.org/wiki/DebugFission
9757 http://gcc.gnu.org/wiki/DebugFissionDWP
9758
9759 To simplify handling of both DWO files ("object" files with the DWARF info)
9760 and DWP files (a file with the DWOs packaged up into one file), we treat
9761 DWP files as having a collection of virtual DWO files. */
9762
9763 static hashval_t
9764 hash_dwo_file (const void *item)
9765 {
9766 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9767 hashval_t hash;
9768
9769 hash = htab_hash_string (dwo_file->dwo_name);
9770 if (dwo_file->comp_dir != NULL)
9771 hash += htab_hash_string (dwo_file->comp_dir);
9772 return hash;
9773 }
9774
9775 static int
9776 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9777 {
9778 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9779 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9780
9781 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9782 return 0;
9783 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9784 return lhs->comp_dir == rhs->comp_dir;
9785 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9786 }
9787
9788 /* Allocate a hash table for DWO files. */
9789
9790 static htab_up
9791 allocate_dwo_file_hash_table ()
9792 {
9793 return htab_up (htab_create_alloc (41,
9794 hash_dwo_file,
9795 eq_dwo_file,
9796 htab_delete_entry<dwo_file>,
9797 xcalloc, xfree));
9798 }
9799
9800 /* Lookup DWO file DWO_NAME. */
9801
9802 static void **
9803 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9804 const char *dwo_name,
9805 const char *comp_dir)
9806 {
9807 struct dwo_file find_entry;
9808 void **slot;
9809
9810 if (per_objfile->per_bfd->dwo_files == NULL)
9811 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9812
9813 find_entry.dwo_name = dwo_name;
9814 find_entry.comp_dir = comp_dir;
9815 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9816 INSERT);
9817
9818 return slot;
9819 }
9820
9821 static hashval_t
9822 hash_dwo_unit (const void *item)
9823 {
9824 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9825
9826 /* This drops the top 32 bits of the id, but is ok for a hash. */
9827 return dwo_unit->signature;
9828 }
9829
9830 static int
9831 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9832 {
9833 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9834 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9835
9836 /* The signature is assumed to be unique within the DWO file.
9837 So while object file CU dwo_id's always have the value zero,
9838 that's OK, assuming each object file DWO file has only one CU,
9839 and that's the rule for now. */
9840 return lhs->signature == rhs->signature;
9841 }
9842
9843 /* Allocate a hash table for DWO CUs,TUs.
9844 There is one of these tables for each of CUs,TUs for each DWO file. */
9845
9846 static htab_up
9847 allocate_dwo_unit_table ()
9848 {
9849 /* Start out with a pretty small number.
9850 Generally DWO files contain only one CU and maybe some TUs. */
9851 return htab_up (htab_create_alloc (3,
9852 hash_dwo_unit,
9853 eq_dwo_unit,
9854 NULL, xcalloc, xfree));
9855 }
9856
9857 /* die_reader_func for create_dwo_cu. */
9858
9859 static void
9860 create_dwo_cu_reader (const struct die_reader_specs *reader,
9861 const gdb_byte *info_ptr,
9862 struct die_info *comp_unit_die,
9863 struct dwo_file *dwo_file,
9864 struct dwo_unit *dwo_unit)
9865 {
9866 struct dwarf2_cu *cu = reader->cu;
9867 sect_offset sect_off = cu->per_cu->sect_off;
9868 struct dwarf2_section_info *section = cu->per_cu->section;
9869
9870 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9871 if (!signature.has_value ())
9872 {
9873 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9874 " its dwo_id [in module %s]"),
9875 sect_offset_str (sect_off), dwo_file->dwo_name);
9876 return;
9877 }
9878
9879 dwo_unit->dwo_file = dwo_file;
9880 dwo_unit->signature = *signature;
9881 dwo_unit->section = section;
9882 dwo_unit->sect_off = sect_off;
9883 dwo_unit->length = cu->per_cu->length;
9884
9885 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9886 sect_offset_str (sect_off),
9887 hex_string (dwo_unit->signature));
9888 }
9889
9890 /* Create the dwo_units for the CUs in a DWO_FILE.
9891 Note: This function processes DWO files only, not DWP files. */
9892
9893 static void
9894 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
9895 dwarf2_cu *cu, struct dwo_file &dwo_file,
9896 dwarf2_section_info &section, htab_up &cus_htab)
9897 {
9898 struct objfile *objfile = per_objfile->objfile;
9899 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
9900 const gdb_byte *info_ptr, *end_ptr;
9901
9902 section.read (objfile);
9903 info_ptr = section.buffer;
9904
9905 if (info_ptr == NULL)
9906 return;
9907
9908 dwarf_read_debug_printf ("Reading %s for %s:",
9909 section.get_name (),
9910 section.get_file_name ());
9911
9912 end_ptr = info_ptr + section.size;
9913 while (info_ptr < end_ptr)
9914 {
9915 struct dwarf2_per_cu_data per_cu;
9916 struct dwo_unit read_unit {};
9917 struct dwo_unit *dwo_unit;
9918 void **slot;
9919 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9920
9921 per_cu.per_bfd = per_bfd;
9922 per_cu.is_debug_types = 0;
9923 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9924 per_cu.section = &section;
9925
9926 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
9927 if (!reader.dummy_p)
9928 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
9929 &dwo_file, &read_unit);
9930 info_ptr += per_cu.length;
9931
9932 // If the unit could not be parsed, skip it.
9933 if (read_unit.dwo_file == NULL)
9934 continue;
9935
9936 if (cus_htab == NULL)
9937 cus_htab = allocate_dwo_unit_table ();
9938
9939 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
9940 struct dwo_unit);
9941 *dwo_unit = read_unit;
9942 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
9943 gdb_assert (slot != NULL);
9944 if (*slot != NULL)
9945 {
9946 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9947 sect_offset dup_sect_off = dup_cu->sect_off;
9948
9949 complaint (_("debug cu entry at offset %s is duplicate to"
9950 " the entry at offset %s, signature %s"),
9951 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
9952 hex_string (dwo_unit->signature));
9953 }
9954 *slot = (void *)dwo_unit;
9955 }
9956 }
9957
9958 /* DWP file .debug_{cu,tu}_index section format:
9959 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9960 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9961
9962 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9963 officially standard DWP format was published with DWARF v5 and is called
9964 Version 5. There are no versions 3 or 4.
9965
9966 DWP Version 1:
9967
9968 Both index sections have the same format, and serve to map a 64-bit
9969 signature to a set of section numbers. Each section begins with a header,
9970 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9971 indexes, and a pool of 32-bit section numbers. The index sections will be
9972 aligned at 8-byte boundaries in the file.
9973
9974 The index section header consists of:
9975
9976 V, 32 bit version number
9977 -, 32 bits unused
9978 N, 32 bit number of compilation units or type units in the index
9979 M, 32 bit number of slots in the hash table
9980
9981 Numbers are recorded using the byte order of the application binary.
9982
9983 The hash table begins at offset 16 in the section, and consists of an array
9984 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9985 order of the application binary). Unused slots in the hash table are 0.
9986 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9987
9988 The parallel table begins immediately after the hash table
9989 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9990 array of 32-bit indexes (using the byte order of the application binary),
9991 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9992 table contains a 32-bit index into the pool of section numbers. For unused
9993 hash table slots, the corresponding entry in the parallel table will be 0.
9994
9995 The pool of section numbers begins immediately following the hash table
9996 (at offset 16 + 12 * M from the beginning of the section). The pool of
9997 section numbers consists of an array of 32-bit words (using the byte order
9998 of the application binary). Each item in the array is indexed starting
9999 from 0. The hash table entry provides the index of the first section
10000 number in the set. Additional section numbers in the set follow, and the
10001 set is terminated by a 0 entry (section number 0 is not used in ELF).
10002
10003 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10004 section must be the first entry in the set, and the .debug_abbrev.dwo must
10005 be the second entry. Other members of the set may follow in any order.
10006
10007 ---
10008
10009 DWP Versions 2 and 5:
10010
10011 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10012 and the entries in the index tables are now offsets into these sections.
10013 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10014 section.
10015
10016 Index Section Contents:
10017 Header
10018 Hash Table of Signatures dwp_hash_table.hash_table
10019 Parallel Table of Indices dwp_hash_table.unit_table
10020 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10021 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10022
10023 The index section header consists of:
10024
10025 V, 32 bit version number
10026 L, 32 bit number of columns in the table of section offsets
10027 N, 32 bit number of compilation units or type units in the index
10028 M, 32 bit number of slots in the hash table
10029
10030 Numbers are recorded using the byte order of the application binary.
10031
10032 The hash table has the same format as version 1.
10033 The parallel table of indices has the same format as version 1,
10034 except that the entries are origin-1 indices into the table of sections
10035 offsets and the table of section sizes.
10036
10037 The table of offsets begins immediately following the parallel table
10038 (at offset 16 + 12 * M from the beginning of the section). The table is
10039 a two-dimensional array of 32-bit words (using the byte order of the
10040 application binary), with L columns and N+1 rows, in row-major order.
10041 Each row in the array is indexed starting from 0. The first row provides
10042 a key to the remaining rows: each column in this row provides an identifier
10043 for a debug section, and the offsets in the same column of subsequent rows
10044 refer to that section. The section identifiers for Version 2 are:
10045
10046 DW_SECT_INFO 1 .debug_info.dwo
10047 DW_SECT_TYPES 2 .debug_types.dwo
10048 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10049 DW_SECT_LINE 4 .debug_line.dwo
10050 DW_SECT_LOC 5 .debug_loc.dwo
10051 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10052 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10053 DW_SECT_MACRO 8 .debug_macro.dwo
10054
10055 The section identifiers for Version 5 are:
10056
10057 DW_SECT_INFO_V5 1 .debug_info.dwo
10058 DW_SECT_RESERVED_V5 2 --
10059 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10060 DW_SECT_LINE_V5 4 .debug_line.dwo
10061 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10062 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10063 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10064 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10065
10066 The offsets provided by the CU and TU index sections are the base offsets
10067 for the contributions made by each CU or TU to the corresponding section
10068 in the package file. Each CU and TU header contains an abbrev_offset
10069 field, used to find the abbreviations table for that CU or TU within the
10070 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10071 be interpreted as relative to the base offset given in the index section.
10072 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10073 should be interpreted as relative to the base offset for .debug_line.dwo,
10074 and offsets into other debug sections obtained from DWARF attributes should
10075 also be interpreted as relative to the corresponding base offset.
10076
10077 The table of sizes begins immediately following the table of offsets.
10078 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10079 with L columns and N rows, in row-major order. Each row in the array is
10080 indexed starting from 1 (row 0 is shared by the two tables).
10081
10082 ---
10083
10084 Hash table lookup is handled the same in version 1 and 2:
10085
10086 We assume that N and M will not exceed 2^32 - 1.
10087 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10088
10089 Given a 64-bit compilation unit signature or a type signature S, an entry
10090 in the hash table is located as follows:
10091
10092 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10093 the low-order k bits all set to 1.
10094
10095 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10096
10097 3) If the hash table entry at index H matches the signature, use that
10098 entry. If the hash table entry at index H is unused (all zeroes),
10099 terminate the search: the signature is not present in the table.
10100
10101 4) Let H = (H + H') modulo M. Repeat at Step 3.
10102
10103 Because M > N and H' and M are relatively prime, the search is guaranteed
10104 to stop at an unused slot or find the match. */
10105
10106 /* Create a hash table to map DWO IDs to their CU/TU entry in
10107 .debug_{info,types}.dwo in DWP_FILE.
10108 Returns NULL if there isn't one.
10109 Note: This function processes DWP files only, not DWO files. */
10110
10111 static struct dwp_hash_table *
10112 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10113 struct dwp_file *dwp_file, int is_debug_types)
10114 {
10115 struct objfile *objfile = per_objfile->objfile;
10116 bfd *dbfd = dwp_file->dbfd.get ();
10117 const gdb_byte *index_ptr, *index_end;
10118 struct dwarf2_section_info *index;
10119 uint32_t version, nr_columns, nr_units, nr_slots;
10120 struct dwp_hash_table *htab;
10121
10122 if (is_debug_types)
10123 index = &dwp_file->sections.tu_index;
10124 else
10125 index = &dwp_file->sections.cu_index;
10126
10127 if (index->empty ())
10128 return NULL;
10129 index->read (objfile);
10130
10131 index_ptr = index->buffer;
10132 index_end = index_ptr + index->size;
10133
10134 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10135 For now it's safe to just read 4 bytes (particularly as it's difficult to
10136 tell if you're dealing with Version 5 before you've read the version). */
10137 version = read_4_bytes (dbfd, index_ptr);
10138 index_ptr += 4;
10139 if (version == 2 || version == 5)
10140 nr_columns = read_4_bytes (dbfd, index_ptr);
10141 else
10142 nr_columns = 0;
10143 index_ptr += 4;
10144 nr_units = read_4_bytes (dbfd, index_ptr);
10145 index_ptr += 4;
10146 nr_slots = read_4_bytes (dbfd, index_ptr);
10147 index_ptr += 4;
10148
10149 if (version != 1 && version != 2 && version != 5)
10150 {
10151 error (_("Dwarf Error: unsupported DWP file version (%s)"
10152 " [in module %s]"),
10153 pulongest (version), dwp_file->name);
10154 }
10155 if (nr_slots != (nr_slots & -nr_slots))
10156 {
10157 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10158 " is not power of 2 [in module %s]"),
10159 pulongest (nr_slots), dwp_file->name);
10160 }
10161
10162 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10163 htab->version = version;
10164 htab->nr_columns = nr_columns;
10165 htab->nr_units = nr_units;
10166 htab->nr_slots = nr_slots;
10167 htab->hash_table = index_ptr;
10168 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10169
10170 /* Exit early if the table is empty. */
10171 if (nr_slots == 0 || nr_units == 0
10172 || (version == 2 && nr_columns == 0)
10173 || (version == 5 && nr_columns == 0))
10174 {
10175 /* All must be zero. */
10176 if (nr_slots != 0 || nr_units != 0
10177 || (version == 2 && nr_columns != 0)
10178 || (version == 5 && nr_columns != 0))
10179 {
10180 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10181 " all zero [in modules %s]"),
10182 dwp_file->name);
10183 }
10184 return htab;
10185 }
10186
10187 if (version == 1)
10188 {
10189 htab->section_pool.v1.indices =
10190 htab->unit_table + sizeof (uint32_t) * nr_slots;
10191 /* It's harder to decide whether the section is too small in v1.
10192 V1 is deprecated anyway so we punt. */
10193 }
10194 else if (version == 2)
10195 {
10196 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10197 int *ids = htab->section_pool.v2.section_ids;
10198 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10199 /* Reverse map for error checking. */
10200 int ids_seen[DW_SECT_MAX + 1];
10201 int i;
10202
10203 if (nr_columns < 2)
10204 {
10205 error (_("Dwarf Error: bad DWP hash table, too few columns"
10206 " in section table [in module %s]"),
10207 dwp_file->name);
10208 }
10209 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10210 {
10211 error (_("Dwarf Error: bad DWP hash table, too many columns"
10212 " in section table [in module %s]"),
10213 dwp_file->name);
10214 }
10215 memset (ids, 255, sizeof_ids);
10216 memset (ids_seen, 255, sizeof (ids_seen));
10217 for (i = 0; i < nr_columns; ++i)
10218 {
10219 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10220
10221 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10222 {
10223 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10224 " in section table [in module %s]"),
10225 id, dwp_file->name);
10226 }
10227 if (ids_seen[id] != -1)
10228 {
10229 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10230 " id %d in section table [in module %s]"),
10231 id, dwp_file->name);
10232 }
10233 ids_seen[id] = i;
10234 ids[i] = id;
10235 }
10236 /* Must have exactly one info or types section. */
10237 if (((ids_seen[DW_SECT_INFO] != -1)
10238 + (ids_seen[DW_SECT_TYPES] != -1))
10239 != 1)
10240 {
10241 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10242 " DWO info/types section [in module %s]"),
10243 dwp_file->name);
10244 }
10245 /* Must have an abbrev section. */
10246 if (ids_seen[DW_SECT_ABBREV] == -1)
10247 {
10248 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10249 " section [in module %s]"),
10250 dwp_file->name);
10251 }
10252 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10253 htab->section_pool.v2.sizes =
10254 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10255 * nr_units * nr_columns);
10256 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10257 * nr_units * nr_columns))
10258 > index_end)
10259 {
10260 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10261 " [in module %s]"),
10262 dwp_file->name);
10263 }
10264 }
10265 else /* version == 5 */
10266 {
10267 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10268 int *ids = htab->section_pool.v5.section_ids;
10269 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10270 /* Reverse map for error checking. */
10271 int ids_seen[DW_SECT_MAX_V5 + 1];
10272
10273 if (nr_columns < 2)
10274 {
10275 error (_("Dwarf Error: bad DWP hash table, too few columns"
10276 " in section table [in module %s]"),
10277 dwp_file->name);
10278 }
10279 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10280 {
10281 error (_("Dwarf Error: bad DWP hash table, too many columns"
10282 " in section table [in module %s]"),
10283 dwp_file->name);
10284 }
10285 memset (ids, 255, sizeof_ids);
10286 memset (ids_seen, 255, sizeof (ids_seen));
10287 for (int i = 0; i < nr_columns; ++i)
10288 {
10289 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10290
10291 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10292 {
10293 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10294 " in section table [in module %s]"),
10295 id, dwp_file->name);
10296 }
10297 if (ids_seen[id] != -1)
10298 {
10299 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10300 " id %d in section table [in module %s]"),
10301 id, dwp_file->name);
10302 }
10303 ids_seen[id] = i;
10304 ids[i] = id;
10305 }
10306 /* Must have seen an info section. */
10307 if (ids_seen[DW_SECT_INFO_V5] == -1)
10308 {
10309 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10310 " DWO info/types section [in module %s]"),
10311 dwp_file->name);
10312 }
10313 /* Must have an abbrev section. */
10314 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10315 {
10316 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10317 " section [in module %s]"),
10318 dwp_file->name);
10319 }
10320 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10321 htab->section_pool.v5.sizes
10322 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10323 * nr_units * nr_columns);
10324 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10325 * nr_units * nr_columns))
10326 > index_end)
10327 {
10328 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10329 " [in module %s]"),
10330 dwp_file->name);
10331 }
10332 }
10333
10334 return htab;
10335 }
10336
10337 /* Update SECTIONS with the data from SECTP.
10338
10339 This function is like the other "locate" section routines, but in
10340 this context the sections to read comes from the DWP V1 hash table,
10341 not the full ELF section table.
10342
10343 The result is non-zero for success, or zero if an error was found. */
10344
10345 static int
10346 locate_v1_virtual_dwo_sections (asection *sectp,
10347 struct virtual_v1_dwo_sections *sections)
10348 {
10349 const struct dwop_section_names *names = &dwop_section_names;
10350
10351 if (names->abbrev_dwo.matches (sectp->name))
10352 {
10353 /* There can be only one. */
10354 if (sections->abbrev.s.section != NULL)
10355 return 0;
10356 sections->abbrev.s.section = sectp;
10357 sections->abbrev.size = bfd_section_size (sectp);
10358 }
10359 else if (names->info_dwo.matches (sectp->name)
10360 || names->types_dwo.matches (sectp->name))
10361 {
10362 /* There can be only one. */
10363 if (sections->info_or_types.s.section != NULL)
10364 return 0;
10365 sections->info_or_types.s.section = sectp;
10366 sections->info_or_types.size = bfd_section_size (sectp);
10367 }
10368 else if (names->line_dwo.matches (sectp->name))
10369 {
10370 /* There can be only one. */
10371 if (sections->line.s.section != NULL)
10372 return 0;
10373 sections->line.s.section = sectp;
10374 sections->line.size = bfd_section_size (sectp);
10375 }
10376 else if (names->loc_dwo.matches (sectp->name))
10377 {
10378 /* There can be only one. */
10379 if (sections->loc.s.section != NULL)
10380 return 0;
10381 sections->loc.s.section = sectp;
10382 sections->loc.size = bfd_section_size (sectp);
10383 }
10384 else if (names->macinfo_dwo.matches (sectp->name))
10385 {
10386 /* There can be only one. */
10387 if (sections->macinfo.s.section != NULL)
10388 return 0;
10389 sections->macinfo.s.section = sectp;
10390 sections->macinfo.size = bfd_section_size (sectp);
10391 }
10392 else if (names->macro_dwo.matches (sectp->name))
10393 {
10394 /* There can be only one. */
10395 if (sections->macro.s.section != NULL)
10396 return 0;
10397 sections->macro.s.section = sectp;
10398 sections->macro.size = bfd_section_size (sectp);
10399 }
10400 else if (names->str_offsets_dwo.matches (sectp->name))
10401 {
10402 /* There can be only one. */
10403 if (sections->str_offsets.s.section != NULL)
10404 return 0;
10405 sections->str_offsets.s.section = sectp;
10406 sections->str_offsets.size = bfd_section_size (sectp);
10407 }
10408 else
10409 {
10410 /* No other kind of section is valid. */
10411 return 0;
10412 }
10413
10414 return 1;
10415 }
10416
10417 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10418 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10419 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10420 This is for DWP version 1 files. */
10421
10422 static struct dwo_unit *
10423 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10424 struct dwp_file *dwp_file,
10425 uint32_t unit_index,
10426 const char *comp_dir,
10427 ULONGEST signature, int is_debug_types)
10428 {
10429 const struct dwp_hash_table *dwp_htab =
10430 is_debug_types ? dwp_file->tus : dwp_file->cus;
10431 bfd *dbfd = dwp_file->dbfd.get ();
10432 const char *kind = is_debug_types ? "TU" : "CU";
10433 struct dwo_file *dwo_file;
10434 struct dwo_unit *dwo_unit;
10435 struct virtual_v1_dwo_sections sections;
10436 void **dwo_file_slot;
10437 int i;
10438
10439 gdb_assert (dwp_file->version == 1);
10440
10441 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10442 kind, pulongest (unit_index), hex_string (signature),
10443 dwp_file->name);
10444
10445 /* Fetch the sections of this DWO unit.
10446 Put a limit on the number of sections we look for so that bad data
10447 doesn't cause us to loop forever. */
10448
10449 #define MAX_NR_V1_DWO_SECTIONS \
10450 (1 /* .debug_info or .debug_types */ \
10451 + 1 /* .debug_abbrev */ \
10452 + 1 /* .debug_line */ \
10453 + 1 /* .debug_loc */ \
10454 + 1 /* .debug_str_offsets */ \
10455 + 1 /* .debug_macro or .debug_macinfo */ \
10456 + 1 /* trailing zero */)
10457
10458 memset (&sections, 0, sizeof (sections));
10459
10460 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10461 {
10462 asection *sectp;
10463 uint32_t section_nr =
10464 read_4_bytes (dbfd,
10465 dwp_htab->section_pool.v1.indices
10466 + (unit_index + i) * sizeof (uint32_t));
10467
10468 if (section_nr == 0)
10469 break;
10470 if (section_nr >= dwp_file->num_sections)
10471 {
10472 error (_("Dwarf Error: bad DWP hash table, section number too large"
10473 " [in module %s]"),
10474 dwp_file->name);
10475 }
10476
10477 sectp = dwp_file->elf_sections[section_nr];
10478 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10479 {
10480 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10481 " [in module %s]"),
10482 dwp_file->name);
10483 }
10484 }
10485
10486 if (i < 2
10487 || sections.info_or_types.empty ()
10488 || sections.abbrev.empty ())
10489 {
10490 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10491 " [in module %s]"),
10492 dwp_file->name);
10493 }
10494 if (i == MAX_NR_V1_DWO_SECTIONS)
10495 {
10496 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10497 " [in module %s]"),
10498 dwp_file->name);
10499 }
10500
10501 /* It's easier for the rest of the code if we fake a struct dwo_file and
10502 have dwo_unit "live" in that. At least for now.
10503
10504 The DWP file can be made up of a random collection of CUs and TUs.
10505 However, for each CU + set of TUs that came from the same original DWO
10506 file, we can combine them back into a virtual DWO file to save space
10507 (fewer struct dwo_file objects to allocate). Remember that for really
10508 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10509
10510 std::string virtual_dwo_name =
10511 string_printf ("virtual-dwo/%d-%d-%d-%d",
10512 sections.abbrev.get_id (),
10513 sections.line.get_id (),
10514 sections.loc.get_id (),
10515 sections.str_offsets.get_id ());
10516 /* Can we use an existing virtual DWO file? */
10517 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10518 comp_dir);
10519 /* Create one if necessary. */
10520 if (*dwo_file_slot == NULL)
10521 {
10522 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10523 virtual_dwo_name.c_str ());
10524
10525 dwo_file = new struct dwo_file;
10526 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10527 dwo_file->comp_dir = comp_dir;
10528 dwo_file->sections.abbrev = sections.abbrev;
10529 dwo_file->sections.line = sections.line;
10530 dwo_file->sections.loc = sections.loc;
10531 dwo_file->sections.macinfo = sections.macinfo;
10532 dwo_file->sections.macro = sections.macro;
10533 dwo_file->sections.str_offsets = sections.str_offsets;
10534 /* The "str" section is global to the entire DWP file. */
10535 dwo_file->sections.str = dwp_file->sections.str;
10536 /* The info or types section is assigned below to dwo_unit,
10537 there's no need to record it in dwo_file.
10538 Also, we can't simply record type sections in dwo_file because
10539 we record a pointer into the vector in dwo_unit. As we collect more
10540 types we'll grow the vector and eventually have to reallocate space
10541 for it, invalidating all copies of pointers into the previous
10542 contents. */
10543 *dwo_file_slot = dwo_file;
10544 }
10545 else
10546 {
10547 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10548 virtual_dwo_name.c_str ());
10549
10550 dwo_file = (struct dwo_file *) *dwo_file_slot;
10551 }
10552
10553 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10554 dwo_unit->dwo_file = dwo_file;
10555 dwo_unit->signature = signature;
10556 dwo_unit->section =
10557 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10558 *dwo_unit->section = sections.info_or_types;
10559 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10560
10561 return dwo_unit;
10562 }
10563
10564 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10565 simplify them. Given a pointer to the containing section SECTION, and
10566 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10567 virtual section of just that piece. */
10568
10569 static struct dwarf2_section_info
10570 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10571 struct dwarf2_section_info *section,
10572 bfd_size_type offset, bfd_size_type size)
10573 {
10574 struct dwarf2_section_info result;
10575 asection *sectp;
10576
10577 gdb_assert (section != NULL);
10578 gdb_assert (!section->is_virtual);
10579
10580 memset (&result, 0, sizeof (result));
10581 result.s.containing_section = section;
10582 result.is_virtual = true;
10583
10584 if (size == 0)
10585 return result;
10586
10587 sectp = section->get_bfd_section ();
10588
10589 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10590 bounds of the real section. This is a pretty-rare event, so just
10591 flag an error (easier) instead of a warning and trying to cope. */
10592 if (sectp == NULL
10593 || offset + size > bfd_section_size (sectp))
10594 {
10595 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10596 " in section %s [in module %s]"),
10597 sectp ? bfd_section_name (sectp) : "<unknown>",
10598 objfile_name (per_objfile->objfile));
10599 }
10600
10601 result.virtual_offset = offset;
10602 result.size = size;
10603 return result;
10604 }
10605
10606 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10607 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10608 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10609 This is for DWP version 2 files. */
10610
10611 static struct dwo_unit *
10612 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10613 struct dwp_file *dwp_file,
10614 uint32_t unit_index,
10615 const char *comp_dir,
10616 ULONGEST signature, int is_debug_types)
10617 {
10618 const struct dwp_hash_table *dwp_htab =
10619 is_debug_types ? dwp_file->tus : dwp_file->cus;
10620 bfd *dbfd = dwp_file->dbfd.get ();
10621 const char *kind = is_debug_types ? "TU" : "CU";
10622 struct dwo_file *dwo_file;
10623 struct dwo_unit *dwo_unit;
10624 struct virtual_v2_or_v5_dwo_sections sections;
10625 void **dwo_file_slot;
10626 int i;
10627
10628 gdb_assert (dwp_file->version == 2);
10629
10630 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10631 kind, pulongest (unit_index), hex_string (signature),
10632 dwp_file->name);
10633
10634 /* Fetch the section offsets of this DWO unit. */
10635
10636 memset (&sections, 0, sizeof (sections));
10637
10638 for (i = 0; i < dwp_htab->nr_columns; ++i)
10639 {
10640 uint32_t offset = read_4_bytes (dbfd,
10641 dwp_htab->section_pool.v2.offsets
10642 + (((unit_index - 1) * dwp_htab->nr_columns
10643 + i)
10644 * sizeof (uint32_t)));
10645 uint32_t size = read_4_bytes (dbfd,
10646 dwp_htab->section_pool.v2.sizes
10647 + (((unit_index - 1) * dwp_htab->nr_columns
10648 + i)
10649 * sizeof (uint32_t)));
10650
10651 switch (dwp_htab->section_pool.v2.section_ids[i])
10652 {
10653 case DW_SECT_INFO:
10654 case DW_SECT_TYPES:
10655 sections.info_or_types_offset = offset;
10656 sections.info_or_types_size = size;
10657 break;
10658 case DW_SECT_ABBREV:
10659 sections.abbrev_offset = offset;
10660 sections.abbrev_size = size;
10661 break;
10662 case DW_SECT_LINE:
10663 sections.line_offset = offset;
10664 sections.line_size = size;
10665 break;
10666 case DW_SECT_LOC:
10667 sections.loc_offset = offset;
10668 sections.loc_size = size;
10669 break;
10670 case DW_SECT_STR_OFFSETS:
10671 sections.str_offsets_offset = offset;
10672 sections.str_offsets_size = size;
10673 break;
10674 case DW_SECT_MACINFO:
10675 sections.macinfo_offset = offset;
10676 sections.macinfo_size = size;
10677 break;
10678 case DW_SECT_MACRO:
10679 sections.macro_offset = offset;
10680 sections.macro_size = size;
10681 break;
10682 }
10683 }
10684
10685 /* It's easier for the rest of the code if we fake a struct dwo_file and
10686 have dwo_unit "live" in that. At least for now.
10687
10688 The DWP file can be made up of a random collection of CUs and TUs.
10689 However, for each CU + set of TUs that came from the same original DWO
10690 file, we can combine them back into a virtual DWO file to save space
10691 (fewer struct dwo_file objects to allocate). Remember that for really
10692 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10693
10694 std::string virtual_dwo_name =
10695 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10696 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10697 (long) (sections.line_size ? sections.line_offset : 0),
10698 (long) (sections.loc_size ? sections.loc_offset : 0),
10699 (long) (sections.str_offsets_size
10700 ? sections.str_offsets_offset : 0));
10701 /* Can we use an existing virtual DWO file? */
10702 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10703 comp_dir);
10704 /* Create one if necessary. */
10705 if (*dwo_file_slot == NULL)
10706 {
10707 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10708 virtual_dwo_name.c_str ());
10709
10710 dwo_file = new struct dwo_file;
10711 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10712 dwo_file->comp_dir = comp_dir;
10713 dwo_file->sections.abbrev =
10714 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10715 sections.abbrev_offset,
10716 sections.abbrev_size);
10717 dwo_file->sections.line =
10718 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10719 sections.line_offset,
10720 sections.line_size);
10721 dwo_file->sections.loc =
10722 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10723 sections.loc_offset, sections.loc_size);
10724 dwo_file->sections.macinfo =
10725 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10726 sections.macinfo_offset,
10727 sections.macinfo_size);
10728 dwo_file->sections.macro =
10729 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10730 sections.macro_offset,
10731 sections.macro_size);
10732 dwo_file->sections.str_offsets =
10733 create_dwp_v2_or_v5_section (per_objfile,
10734 &dwp_file->sections.str_offsets,
10735 sections.str_offsets_offset,
10736 sections.str_offsets_size);
10737 /* The "str" section is global to the entire DWP file. */
10738 dwo_file->sections.str = dwp_file->sections.str;
10739 /* The info or types section is assigned below to dwo_unit,
10740 there's no need to record it in dwo_file.
10741 Also, we can't simply record type sections in dwo_file because
10742 we record a pointer into the vector in dwo_unit. As we collect more
10743 types we'll grow the vector and eventually have to reallocate space
10744 for it, invalidating all copies of pointers into the previous
10745 contents. */
10746 *dwo_file_slot = dwo_file;
10747 }
10748 else
10749 {
10750 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10751 virtual_dwo_name.c_str ());
10752
10753 dwo_file = (struct dwo_file *) *dwo_file_slot;
10754 }
10755
10756 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10757 dwo_unit->dwo_file = dwo_file;
10758 dwo_unit->signature = signature;
10759 dwo_unit->section =
10760 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10761 *dwo_unit->section = create_dwp_v2_or_v5_section
10762 (per_objfile,
10763 is_debug_types
10764 ? &dwp_file->sections.types
10765 : &dwp_file->sections.info,
10766 sections.info_or_types_offset,
10767 sections.info_or_types_size);
10768 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10769
10770 return dwo_unit;
10771 }
10772
10773 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10774 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10775 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10776 This is for DWP version 5 files. */
10777
10778 static struct dwo_unit *
10779 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10780 struct dwp_file *dwp_file,
10781 uint32_t unit_index,
10782 const char *comp_dir,
10783 ULONGEST signature, int is_debug_types)
10784 {
10785 const struct dwp_hash_table *dwp_htab
10786 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10787 bfd *dbfd = dwp_file->dbfd.get ();
10788 const char *kind = is_debug_types ? "TU" : "CU";
10789 struct dwo_file *dwo_file;
10790 struct dwo_unit *dwo_unit;
10791 struct virtual_v2_or_v5_dwo_sections sections {};
10792 void **dwo_file_slot;
10793
10794 gdb_assert (dwp_file->version == 5);
10795
10796 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10797 kind, pulongest (unit_index), hex_string (signature),
10798 dwp_file->name);
10799
10800 /* Fetch the section offsets of this DWO unit. */
10801
10802 /* memset (&sections, 0, sizeof (sections)); */
10803
10804 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10805 {
10806 uint32_t offset = read_4_bytes (dbfd,
10807 dwp_htab->section_pool.v5.offsets
10808 + (((unit_index - 1)
10809 * dwp_htab->nr_columns
10810 + i)
10811 * sizeof (uint32_t)));
10812 uint32_t size = read_4_bytes (dbfd,
10813 dwp_htab->section_pool.v5.sizes
10814 + (((unit_index - 1) * dwp_htab->nr_columns
10815 + i)
10816 * sizeof (uint32_t)));
10817
10818 switch (dwp_htab->section_pool.v5.section_ids[i])
10819 {
10820 case DW_SECT_ABBREV_V5:
10821 sections.abbrev_offset = offset;
10822 sections.abbrev_size = size;
10823 break;
10824 case DW_SECT_INFO_V5:
10825 sections.info_or_types_offset = offset;
10826 sections.info_or_types_size = size;
10827 break;
10828 case DW_SECT_LINE_V5:
10829 sections.line_offset = offset;
10830 sections.line_size = size;
10831 break;
10832 case DW_SECT_LOCLISTS_V5:
10833 sections.loclists_offset = offset;
10834 sections.loclists_size = size;
10835 break;
10836 case DW_SECT_MACRO_V5:
10837 sections.macro_offset = offset;
10838 sections.macro_size = size;
10839 break;
10840 case DW_SECT_RNGLISTS_V5:
10841 sections.rnglists_offset = offset;
10842 sections.rnglists_size = size;
10843 break;
10844 case DW_SECT_STR_OFFSETS_V5:
10845 sections.str_offsets_offset = offset;
10846 sections.str_offsets_size = size;
10847 break;
10848 case DW_SECT_RESERVED_V5:
10849 default:
10850 break;
10851 }
10852 }
10853
10854 /* It's easier for the rest of the code if we fake a struct dwo_file and
10855 have dwo_unit "live" in that. At least for now.
10856
10857 The DWP file can be made up of a random collection of CUs and TUs.
10858 However, for each CU + set of TUs that came from the same original DWO
10859 file, we can combine them back into a virtual DWO file to save space
10860 (fewer struct dwo_file objects to allocate). Remember that for really
10861 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10862
10863 std::string virtual_dwo_name =
10864 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10865 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10866 (long) (sections.line_size ? sections.line_offset : 0),
10867 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10868 (long) (sections.str_offsets_size
10869 ? sections.str_offsets_offset : 0),
10870 (long) (sections.macro_size ? sections.macro_offset : 0),
10871 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10872 /* Can we use an existing virtual DWO file? */
10873 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10874 virtual_dwo_name.c_str (),
10875 comp_dir);
10876 /* Create one if necessary. */
10877 if (*dwo_file_slot == NULL)
10878 {
10879 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10880 virtual_dwo_name.c_str ());
10881
10882 dwo_file = new struct dwo_file;
10883 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10884 dwo_file->comp_dir = comp_dir;
10885 dwo_file->sections.abbrev =
10886 create_dwp_v2_or_v5_section (per_objfile,
10887 &dwp_file->sections.abbrev,
10888 sections.abbrev_offset,
10889 sections.abbrev_size);
10890 dwo_file->sections.line =
10891 create_dwp_v2_or_v5_section (per_objfile,
10892 &dwp_file->sections.line,
10893 sections.line_offset, sections.line_size);
10894 dwo_file->sections.macro =
10895 create_dwp_v2_or_v5_section (per_objfile,
10896 &dwp_file->sections.macro,
10897 sections.macro_offset,
10898 sections.macro_size);
10899 dwo_file->sections.loclists =
10900 create_dwp_v2_or_v5_section (per_objfile,
10901 &dwp_file->sections.loclists,
10902 sections.loclists_offset,
10903 sections.loclists_size);
10904 dwo_file->sections.rnglists =
10905 create_dwp_v2_or_v5_section (per_objfile,
10906 &dwp_file->sections.rnglists,
10907 sections.rnglists_offset,
10908 sections.rnglists_size);
10909 dwo_file->sections.str_offsets =
10910 create_dwp_v2_or_v5_section (per_objfile,
10911 &dwp_file->sections.str_offsets,
10912 sections.str_offsets_offset,
10913 sections.str_offsets_size);
10914 /* The "str" section is global to the entire DWP file. */
10915 dwo_file->sections.str = dwp_file->sections.str;
10916 /* The info or types section is assigned below to dwo_unit,
10917 there's no need to record it in dwo_file.
10918 Also, we can't simply record type sections in dwo_file because
10919 we record a pointer into the vector in dwo_unit. As we collect more
10920 types we'll grow the vector and eventually have to reallocate space
10921 for it, invalidating all copies of pointers into the previous
10922 contents. */
10923 *dwo_file_slot = dwo_file;
10924 }
10925 else
10926 {
10927 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10928 virtual_dwo_name.c_str ());
10929
10930 dwo_file = (struct dwo_file *) *dwo_file_slot;
10931 }
10932
10933 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10934 dwo_unit->dwo_file = dwo_file;
10935 dwo_unit->signature = signature;
10936 dwo_unit->section
10937 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10938 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
10939 &dwp_file->sections.info,
10940 sections.info_or_types_offset,
10941 sections.info_or_types_size);
10942 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10943
10944 return dwo_unit;
10945 }
10946
10947 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10948 Returns NULL if the signature isn't found. */
10949
10950 static struct dwo_unit *
10951 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
10952 struct dwp_file *dwp_file, const char *comp_dir,
10953 ULONGEST signature, int is_debug_types)
10954 {
10955 const struct dwp_hash_table *dwp_htab =
10956 is_debug_types ? dwp_file->tus : dwp_file->cus;
10957 bfd *dbfd = dwp_file->dbfd.get ();
10958 uint32_t mask = dwp_htab->nr_slots - 1;
10959 uint32_t hash = signature & mask;
10960 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10961 unsigned int i;
10962 void **slot;
10963 struct dwo_unit find_dwo_cu;
10964
10965 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10966 find_dwo_cu.signature = signature;
10967 slot = htab_find_slot (is_debug_types
10968 ? dwp_file->loaded_tus.get ()
10969 : dwp_file->loaded_cus.get (),
10970 &find_dwo_cu, INSERT);
10971
10972 if (*slot != NULL)
10973 return (struct dwo_unit *) *slot;
10974
10975 /* Use a for loop so that we don't loop forever on bad debug info. */
10976 for (i = 0; i < dwp_htab->nr_slots; ++i)
10977 {
10978 ULONGEST signature_in_table;
10979
10980 signature_in_table =
10981 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10982 if (signature_in_table == signature)
10983 {
10984 uint32_t unit_index =
10985 read_4_bytes (dbfd,
10986 dwp_htab->unit_table + hash * sizeof (uint32_t));
10987
10988 if (dwp_file->version == 1)
10989 {
10990 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
10991 unit_index, comp_dir,
10992 signature, is_debug_types);
10993 }
10994 else if (dwp_file->version == 2)
10995 {
10996 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
10997 unit_index, comp_dir,
10998 signature, is_debug_types);
10999 }
11000 else /* version == 5 */
11001 {
11002 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11003 unit_index, comp_dir,
11004 signature, is_debug_types);
11005 }
11006 return (struct dwo_unit *) *slot;
11007 }
11008 if (signature_in_table == 0)
11009 return NULL;
11010 hash = (hash + hash2) & mask;
11011 }
11012
11013 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11014 " [in module %s]"),
11015 dwp_file->name);
11016 }
11017
11018 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11019 Open the file specified by FILE_NAME and hand it off to BFD for
11020 preliminary analysis. Return a newly initialized bfd *, which
11021 includes a canonicalized copy of FILE_NAME.
11022 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11023 SEARCH_CWD is true if the current directory is to be searched.
11024 It will be searched before debug-file-directory.
11025 If successful, the file is added to the bfd include table of the
11026 objfile's bfd (see gdb_bfd_record_inclusion).
11027 If unable to find/open the file, return NULL.
11028 NOTE: This function is derived from symfile_bfd_open. */
11029
11030 static gdb_bfd_ref_ptr
11031 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11032 const char *file_name, int is_dwp, int search_cwd)
11033 {
11034 int desc;
11035 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11036 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11037 to debug_file_directory. */
11038 const char *search_path;
11039 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11040
11041 gdb::unique_xmalloc_ptr<char> search_path_holder;
11042 if (search_cwd)
11043 {
11044 if (!debug_file_directory.empty ())
11045 {
11046 search_path_holder.reset (concat (".", dirname_separator_string,
11047 debug_file_directory.c_str (),
11048 (char *) NULL));
11049 search_path = search_path_holder.get ();
11050 }
11051 else
11052 search_path = ".";
11053 }
11054 else
11055 search_path = debug_file_directory.c_str ();
11056
11057 /* Add the path for the executable binary to the list of search paths. */
11058 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11059 search_path_holder.reset (concat (objfile_dir.c_str (),
11060 dirname_separator_string,
11061 search_path, nullptr));
11062 search_path = search_path_holder.get ();
11063
11064 openp_flags flags = OPF_RETURN_REALPATH;
11065 if (is_dwp)
11066 flags |= OPF_SEARCH_IN_PATH;
11067
11068 gdb::unique_xmalloc_ptr<char> absolute_name;
11069 desc = openp (search_path, flags, file_name,
11070 O_RDONLY | O_BINARY, &absolute_name);
11071 if (desc < 0)
11072 return NULL;
11073
11074 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11075 gnutarget, desc));
11076 if (sym_bfd == NULL)
11077 return NULL;
11078 bfd_set_cacheable (sym_bfd.get (), 1);
11079
11080 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11081 return NULL;
11082
11083 /* Success. Record the bfd as having been included by the objfile's bfd.
11084 This is important because things like demangled_names_hash lives in the
11085 objfile's per_bfd space and may have references to things like symbol
11086 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11087 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
11088
11089 return sym_bfd;
11090 }
11091
11092 /* Try to open DWO file FILE_NAME.
11093 COMP_DIR is the DW_AT_comp_dir attribute.
11094 The result is the bfd handle of the file.
11095 If there is a problem finding or opening the file, return NULL.
11096 Upon success, the canonicalized path of the file is stored in the bfd,
11097 same as symfile_bfd_open. */
11098
11099 static gdb_bfd_ref_ptr
11100 open_dwo_file (dwarf2_per_objfile *per_objfile,
11101 const char *file_name, const char *comp_dir)
11102 {
11103 if (IS_ABSOLUTE_PATH (file_name))
11104 return try_open_dwop_file (per_objfile, file_name,
11105 0 /*is_dwp*/, 0 /*search_cwd*/);
11106
11107 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11108
11109 if (comp_dir != NULL)
11110 {
11111 std::string path_to_try = path_join (comp_dir, file_name);
11112
11113 /* NOTE: If comp_dir is a relative path, this will also try the
11114 search path, which seems useful. */
11115 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11116 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11117
11118 if (abfd != NULL)
11119 return abfd;
11120 }
11121
11122 /* That didn't work, try debug-file-directory, which, despite its name,
11123 is a list of paths. */
11124
11125 if (debug_file_directory.empty ())
11126 return NULL;
11127
11128 return try_open_dwop_file (per_objfile, file_name,
11129 0 /*is_dwp*/, 1 /*search_cwd*/);
11130 }
11131
11132 /* This function is mapped across the sections and remembers the offset and
11133 size of each of the DWO debugging sections we are interested in. */
11134
11135 static void
11136 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11137 asection *sectp, dwo_sections *dwo_sections)
11138 {
11139 const struct dwop_section_names *names = &dwop_section_names;
11140
11141 struct dwarf2_section_info *dw_sect = nullptr;
11142
11143 if (names->abbrev_dwo.matches (sectp->name))
11144 dw_sect = &dwo_sections->abbrev;
11145 else if (names->info_dwo.matches (sectp->name))
11146 dw_sect = &dwo_sections->info;
11147 else if (names->line_dwo.matches (sectp->name))
11148 dw_sect = &dwo_sections->line;
11149 else if (names->loc_dwo.matches (sectp->name))
11150 dw_sect = &dwo_sections->loc;
11151 else if (names->loclists_dwo.matches (sectp->name))
11152 dw_sect = &dwo_sections->loclists;
11153 else if (names->macinfo_dwo.matches (sectp->name))
11154 dw_sect = &dwo_sections->macinfo;
11155 else if (names->macro_dwo.matches (sectp->name))
11156 dw_sect = &dwo_sections->macro;
11157 else if (names->rnglists_dwo.matches (sectp->name))
11158 dw_sect = &dwo_sections->rnglists;
11159 else if (names->str_dwo.matches (sectp->name))
11160 dw_sect = &dwo_sections->str;
11161 else if (names->str_offsets_dwo.matches (sectp->name))
11162 dw_sect = &dwo_sections->str_offsets;
11163 else if (names->types_dwo.matches (sectp->name))
11164 {
11165 struct dwarf2_section_info type_section;
11166
11167 memset (&type_section, 0, sizeof (type_section));
11168 dwo_sections->types.push_back (type_section);
11169 dw_sect = &dwo_sections->types.back ();
11170 }
11171
11172 if (dw_sect != nullptr)
11173 {
11174 dw_sect->s.section = sectp;
11175 dw_sect->size = bfd_section_size (sectp);
11176 dw_sect->read (objfile);
11177 }
11178 }
11179
11180 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11181 by PER_CU. This is for the non-DWP case.
11182 The result is NULL if DWO_NAME can't be found. */
11183
11184 static struct dwo_file *
11185 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11186 const char *comp_dir)
11187 {
11188 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11189
11190 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11191 if (dbfd == NULL)
11192 {
11193 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11194
11195 return NULL;
11196 }
11197
11198 dwo_file_up dwo_file (new struct dwo_file);
11199 dwo_file->dwo_name = dwo_name;
11200 dwo_file->comp_dir = comp_dir;
11201 dwo_file->dbfd = std::move (dbfd);
11202
11203 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11204 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11205 sec, &dwo_file->sections);
11206
11207 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11208 dwo_file->cus);
11209
11210 if (cu->per_cu->dwarf_version < 5)
11211 {
11212 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11213 dwo_file->sections.types, dwo_file->tus);
11214 }
11215 else
11216 {
11217 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11218 &dwo_file->sections.info, dwo_file->tus,
11219 rcuh_kind::COMPILE);
11220 }
11221
11222 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11223
11224 return dwo_file.release ();
11225 }
11226
11227 /* This function is mapped across the sections and remembers the offset and
11228 size of each of the DWP debugging sections common to version 1 and 2 that
11229 we are interested in. */
11230
11231 static void
11232 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11233 asection *sectp, dwp_file *dwp_file)
11234 {
11235 const struct dwop_section_names *names = &dwop_section_names;
11236 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11237
11238 /* Record the ELF section number for later lookup: this is what the
11239 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11240 gdb_assert (elf_section_nr < dwp_file->num_sections);
11241 dwp_file->elf_sections[elf_section_nr] = sectp;
11242
11243 /* Look for specific sections that we need. */
11244 struct dwarf2_section_info *dw_sect = nullptr;
11245 if (names->str_dwo.matches (sectp->name))
11246 dw_sect = &dwp_file->sections.str;
11247 else if (names->cu_index.matches (sectp->name))
11248 dw_sect = &dwp_file->sections.cu_index;
11249 else if (names->tu_index.matches (sectp->name))
11250 dw_sect = &dwp_file->sections.tu_index;
11251
11252 if (dw_sect != nullptr)
11253 {
11254 dw_sect->s.section = sectp;
11255 dw_sect->size = bfd_section_size (sectp);
11256 dw_sect->read (objfile);
11257 }
11258 }
11259
11260 /* This function is mapped across the sections and remembers the offset and
11261 size of each of the DWP version 2 debugging sections that we are interested
11262 in. This is split into a separate function because we don't know if we
11263 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11264
11265 static void
11266 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11267 asection *sectp, void *dwp_file_ptr)
11268 {
11269 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11270 const struct dwop_section_names *names = &dwop_section_names;
11271 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11272
11273 /* Record the ELF section number for later lookup: this is what the
11274 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11275 gdb_assert (elf_section_nr < dwp_file->num_sections);
11276 dwp_file->elf_sections[elf_section_nr] = sectp;
11277
11278 /* Look for specific sections that we need. */
11279 struct dwarf2_section_info *dw_sect = nullptr;
11280 if (names->abbrev_dwo.matches (sectp->name))
11281 dw_sect = &dwp_file->sections.abbrev;
11282 else if (names->info_dwo.matches (sectp->name))
11283 dw_sect = &dwp_file->sections.info;
11284 else if (names->line_dwo.matches (sectp->name))
11285 dw_sect = &dwp_file->sections.line;
11286 else if (names->loc_dwo.matches (sectp->name))
11287 dw_sect = &dwp_file->sections.loc;
11288 else if (names->macinfo_dwo.matches (sectp->name))
11289 dw_sect = &dwp_file->sections.macinfo;
11290 else if (names->macro_dwo.matches (sectp->name))
11291 dw_sect = &dwp_file->sections.macro;
11292 else if (names->str_offsets_dwo.matches (sectp->name))
11293 dw_sect = &dwp_file->sections.str_offsets;
11294 else if (names->types_dwo.matches (sectp->name))
11295 dw_sect = &dwp_file->sections.types;
11296
11297 if (dw_sect != nullptr)
11298 {
11299 dw_sect->s.section = sectp;
11300 dw_sect->size = bfd_section_size (sectp);
11301 dw_sect->read (objfile);
11302 }
11303 }
11304
11305 /* This function is mapped across the sections and remembers the offset and
11306 size of each of the DWP version 5 debugging sections that we are interested
11307 in. This is split into a separate function because we don't know if we
11308 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11309
11310 static void
11311 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11312 asection *sectp, void *dwp_file_ptr)
11313 {
11314 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11315 const struct dwop_section_names *names = &dwop_section_names;
11316 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11317
11318 /* Record the ELF section number for later lookup: this is what the
11319 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11320 gdb_assert (elf_section_nr < dwp_file->num_sections);
11321 dwp_file->elf_sections[elf_section_nr] = sectp;
11322
11323 /* Look for specific sections that we need. */
11324 struct dwarf2_section_info *dw_sect = nullptr;
11325 if (names->abbrev_dwo.matches (sectp->name))
11326 dw_sect = &dwp_file->sections.abbrev;
11327 else if (names->info_dwo.matches (sectp->name))
11328 dw_sect = &dwp_file->sections.info;
11329 else if (names->line_dwo.matches (sectp->name))
11330 dw_sect = &dwp_file->sections.line;
11331 else if (names->loclists_dwo.matches (sectp->name))
11332 dw_sect = &dwp_file->sections.loclists;
11333 else if (names->macro_dwo.matches (sectp->name))
11334 dw_sect = &dwp_file->sections.macro;
11335 else if (names->rnglists_dwo.matches (sectp->name))
11336 dw_sect = &dwp_file->sections.rnglists;
11337 else if (names->str_offsets_dwo.matches (sectp->name))
11338 dw_sect = &dwp_file->sections.str_offsets;
11339
11340 if (dw_sect != nullptr)
11341 {
11342 dw_sect->s.section = sectp;
11343 dw_sect->size = bfd_section_size (sectp);
11344 dw_sect->read (objfile);
11345 }
11346 }
11347
11348 /* Hash function for dwp_file loaded CUs/TUs. */
11349
11350 static hashval_t
11351 hash_dwp_loaded_cutus (const void *item)
11352 {
11353 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11354
11355 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11356 return dwo_unit->signature;
11357 }
11358
11359 /* Equality function for dwp_file loaded CUs/TUs. */
11360
11361 static int
11362 eq_dwp_loaded_cutus (const void *a, const void *b)
11363 {
11364 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11365 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11366
11367 return dua->signature == dub->signature;
11368 }
11369
11370 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11371
11372 static htab_up
11373 allocate_dwp_loaded_cutus_table ()
11374 {
11375 return htab_up (htab_create_alloc (3,
11376 hash_dwp_loaded_cutus,
11377 eq_dwp_loaded_cutus,
11378 NULL, xcalloc, xfree));
11379 }
11380
11381 /* Try to open DWP file FILE_NAME.
11382 The result is the bfd handle of the file.
11383 If there is a problem finding or opening the file, return NULL.
11384 Upon success, the canonicalized path of the file is stored in the bfd,
11385 same as symfile_bfd_open. */
11386
11387 static gdb_bfd_ref_ptr
11388 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11389 {
11390 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11391 1 /*is_dwp*/,
11392 1 /*search_cwd*/));
11393 if (abfd != NULL)
11394 return abfd;
11395
11396 /* Work around upstream bug 15652.
11397 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11398 [Whether that's a "bug" is debatable, but it is getting in our way.]
11399 We have no real idea where the dwp file is, because gdb's realpath-ing
11400 of the executable's path may have discarded the needed info.
11401 [IWBN if the dwp file name was recorded in the executable, akin to
11402 .gnu_debuglink, but that doesn't exist yet.]
11403 Strip the directory from FILE_NAME and search again. */
11404 if (!debug_file_directory.empty ())
11405 {
11406 /* Don't implicitly search the current directory here.
11407 If the user wants to search "." to handle this case,
11408 it must be added to debug-file-directory. */
11409 return try_open_dwop_file (per_objfile, lbasename (file_name),
11410 1 /*is_dwp*/,
11411 0 /*search_cwd*/);
11412 }
11413
11414 return NULL;
11415 }
11416
11417 /* Initialize the use of the DWP file for the current objfile.
11418 By convention the name of the DWP file is ${objfile}.dwp.
11419 The result is NULL if it can't be found. */
11420
11421 static std::unique_ptr<struct dwp_file>
11422 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11423 {
11424 struct objfile *objfile = per_objfile->objfile;
11425
11426 /* Try to find first .dwp for the binary file before any symbolic links
11427 resolving. */
11428
11429 /* If the objfile is a debug file, find the name of the real binary
11430 file and get the name of dwp file from there. */
11431 std::string dwp_name;
11432 if (objfile->separate_debug_objfile_backlink != NULL)
11433 {
11434 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11435 const char *backlink_basename = lbasename (backlink->original_name);
11436
11437 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11438 }
11439 else
11440 dwp_name = objfile->original_name;
11441
11442 dwp_name += ".dwp";
11443
11444 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11445 if (dbfd == NULL
11446 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11447 {
11448 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11449 dwp_name = objfile_name (objfile);
11450 dwp_name += ".dwp";
11451 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11452 }
11453
11454 if (dbfd == NULL)
11455 {
11456 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11457
11458 return std::unique_ptr<dwp_file> ();
11459 }
11460
11461 const char *name = bfd_get_filename (dbfd.get ());
11462 std::unique_ptr<struct dwp_file> dwp_file
11463 (new struct dwp_file (name, std::move (dbfd)));
11464
11465 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11466 dwp_file->elf_sections =
11467 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11468 dwp_file->num_sections, asection *);
11469
11470 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11471 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11472 dwp_file.get ());
11473
11474 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11475
11476 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11477
11478 /* The DWP file version is stored in the hash table. Oh well. */
11479 if (dwp_file->cus && dwp_file->tus
11480 && dwp_file->cus->version != dwp_file->tus->version)
11481 {
11482 /* Technically speaking, we should try to limp along, but this is
11483 pretty bizarre. We use pulongest here because that's the established
11484 portability solution (e.g, we cannot use %u for uint32_t). */
11485 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11486 " TU version %s [in DWP file %s]"),
11487 pulongest (dwp_file->cus->version),
11488 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11489 }
11490
11491 if (dwp_file->cus)
11492 dwp_file->version = dwp_file->cus->version;
11493 else if (dwp_file->tus)
11494 dwp_file->version = dwp_file->tus->version;
11495 else
11496 dwp_file->version = 2;
11497
11498 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11499 {
11500 if (dwp_file->version == 2)
11501 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11502 dwp_file.get ());
11503 else
11504 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11505 dwp_file.get ());
11506 }
11507
11508 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11509 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11510
11511 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11512 dwarf_read_debug_printf (" %s CUs, %s TUs",
11513 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11514 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11515
11516 return dwp_file;
11517 }
11518
11519 /* Wrapper around open_and_init_dwp_file, only open it once. */
11520
11521 static struct dwp_file *
11522 get_dwp_file (dwarf2_per_objfile *per_objfile)
11523 {
11524 if (!per_objfile->per_bfd->dwp_checked)
11525 {
11526 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11527 per_objfile->per_bfd->dwp_checked = 1;
11528 }
11529 return per_objfile->per_bfd->dwp_file.get ();
11530 }
11531
11532 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11533 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11534 or in the DWP file for the objfile, referenced by THIS_UNIT.
11535 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11536 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11537
11538 This is called, for example, when wanting to read a variable with a
11539 complex location. Therefore we don't want to do file i/o for every call.
11540 Therefore we don't want to look for a DWO file on every call.
11541 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11542 then we check if we've already seen DWO_NAME, and only THEN do we check
11543 for a DWO file.
11544
11545 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11546 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11547
11548 static struct dwo_unit *
11549 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11550 ULONGEST signature, int is_debug_types)
11551 {
11552 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11553 struct objfile *objfile = per_objfile->objfile;
11554 const char *kind = is_debug_types ? "TU" : "CU";
11555 void **dwo_file_slot;
11556 struct dwo_file *dwo_file;
11557 struct dwp_file *dwp_file;
11558
11559 /* First see if there's a DWP file.
11560 If we have a DWP file but didn't find the DWO inside it, don't
11561 look for the original DWO file. It makes gdb behave differently
11562 depending on whether one is debugging in the build tree. */
11563
11564 dwp_file = get_dwp_file (per_objfile);
11565 if (dwp_file != NULL)
11566 {
11567 const struct dwp_hash_table *dwp_htab =
11568 is_debug_types ? dwp_file->tus : dwp_file->cus;
11569
11570 if (dwp_htab != NULL)
11571 {
11572 struct dwo_unit *dwo_cutu =
11573 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11574 is_debug_types);
11575
11576 if (dwo_cutu != NULL)
11577 {
11578 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11579 kind, hex_string (signature),
11580 host_address_to_string (dwo_cutu));
11581
11582 return dwo_cutu;
11583 }
11584 }
11585 }
11586 else
11587 {
11588 /* No DWP file, look for the DWO file. */
11589
11590 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11591 if (*dwo_file_slot == NULL)
11592 {
11593 /* Read in the file and build a table of the CUs/TUs it contains. */
11594 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11595 }
11596 /* NOTE: This will be NULL if unable to open the file. */
11597 dwo_file = (struct dwo_file *) *dwo_file_slot;
11598
11599 if (dwo_file != NULL)
11600 {
11601 struct dwo_unit *dwo_cutu = NULL;
11602
11603 if (is_debug_types && dwo_file->tus)
11604 {
11605 struct dwo_unit find_dwo_cutu;
11606
11607 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11608 find_dwo_cutu.signature = signature;
11609 dwo_cutu
11610 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11611 &find_dwo_cutu);
11612 }
11613 else if (!is_debug_types && dwo_file->cus)
11614 {
11615 struct dwo_unit find_dwo_cutu;
11616
11617 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11618 find_dwo_cutu.signature = signature;
11619 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11620 &find_dwo_cutu);
11621 }
11622
11623 if (dwo_cutu != NULL)
11624 {
11625 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11626 kind, dwo_name, hex_string (signature),
11627 host_address_to_string (dwo_cutu));
11628
11629 return dwo_cutu;
11630 }
11631 }
11632 }
11633
11634 /* We didn't find it. This could mean a dwo_id mismatch, or
11635 someone deleted the DWO/DWP file, or the search path isn't set up
11636 correctly to find the file. */
11637
11638 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11639 kind, dwo_name, hex_string (signature));
11640
11641 /* This is a warning and not a complaint because it can be caused by
11642 pilot error (e.g., user accidentally deleting the DWO). */
11643 {
11644 /* Print the name of the DWP file if we looked there, helps the user
11645 better diagnose the problem. */
11646 std::string dwp_text;
11647
11648 if (dwp_file != NULL)
11649 dwp_text = string_printf (" [in DWP file %s]",
11650 lbasename (dwp_file->name));
11651
11652 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11653 " [in module %s]"),
11654 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11655 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11656 }
11657 return NULL;
11658 }
11659
11660 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11661 See lookup_dwo_cutu_unit for details. */
11662
11663 static struct dwo_unit *
11664 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11665 ULONGEST signature)
11666 {
11667 gdb_assert (!cu->per_cu->is_debug_types);
11668
11669 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11670 }
11671
11672 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11673 See lookup_dwo_cutu_unit for details. */
11674
11675 static struct dwo_unit *
11676 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11677 {
11678 gdb_assert (cu->per_cu->is_debug_types);
11679
11680 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11681
11682 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11683 }
11684
11685 /* Traversal function for queue_and_load_all_dwo_tus. */
11686
11687 static int
11688 queue_and_load_dwo_tu (void **slot, void *info)
11689 {
11690 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11691 dwarf2_cu *cu = (dwarf2_cu *) info;
11692 ULONGEST signature = dwo_unit->signature;
11693 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11694
11695 if (sig_type != NULL)
11696 {
11697 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11698 a real dependency of PER_CU on SIG_TYPE. That is detected later
11699 while processing PER_CU. */
11700 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11701 cu->per_cu->lang))
11702 load_full_type_unit (sig_type, cu->per_objfile);
11703 cu->per_cu->imported_symtabs_push (sig_type);
11704 }
11705
11706 return 1;
11707 }
11708
11709 /* Queue all TUs contained in the DWO of CU to be read in.
11710 The DWO may have the only definition of the type, though it may not be
11711 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11712 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11713
11714 static void
11715 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11716 {
11717 struct dwo_unit *dwo_unit;
11718 struct dwo_file *dwo_file;
11719
11720 gdb_assert (cu != nullptr);
11721 gdb_assert (!cu->per_cu->is_debug_types);
11722 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11723
11724 dwo_unit = cu->dwo_unit;
11725 gdb_assert (dwo_unit != NULL);
11726
11727 dwo_file = dwo_unit->dwo_file;
11728 if (dwo_file->tus != NULL)
11729 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11730 }
11731
11732 /* Read in various DIEs. */
11733
11734 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11735 Inherit only the children of the DW_AT_abstract_origin DIE not being
11736 already referenced by DW_AT_abstract_origin from the children of the
11737 current DIE. */
11738
11739 static void
11740 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11741 {
11742 struct die_info *child_die;
11743 sect_offset *offsetp;
11744 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11745 struct die_info *origin_die;
11746 /* Iterator of the ORIGIN_DIE children. */
11747 struct die_info *origin_child_die;
11748 struct attribute *attr;
11749 struct dwarf2_cu *origin_cu;
11750 struct pending **origin_previous_list_in_scope;
11751
11752 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11753 if (!attr)
11754 return;
11755
11756 /* Note that following die references may follow to a die in a
11757 different cu. */
11758
11759 origin_cu = cu;
11760 origin_die = follow_die_ref (die, attr, &origin_cu);
11761
11762 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11763 symbols in. */
11764 origin_previous_list_in_scope = origin_cu->list_in_scope;
11765 origin_cu->list_in_scope = cu->list_in_scope;
11766
11767 if (die->tag != origin_die->tag
11768 && !(die->tag == DW_TAG_inlined_subroutine
11769 && origin_die->tag == DW_TAG_subprogram))
11770 complaint (_("DIE %s and its abstract origin %s have different tags"),
11771 sect_offset_str (die->sect_off),
11772 sect_offset_str (origin_die->sect_off));
11773
11774 /* Find if the concrete and abstract trees are structurally the
11775 same. This is a shallow traversal and it is not bullet-proof;
11776 the compiler can trick the debugger into believing that the trees
11777 are isomorphic, whereas they actually are not. However, the
11778 likelyhood of this happening is pretty low, and a full-fledged
11779 check would be an overkill. */
11780 bool are_isomorphic = true;
11781 die_info *concrete_child = die->child;
11782 die_info *abstract_child = origin_die->child;
11783 while (concrete_child != nullptr || abstract_child != nullptr)
11784 {
11785 if (concrete_child == nullptr
11786 || abstract_child == nullptr
11787 || concrete_child->tag != abstract_child->tag)
11788 {
11789 are_isomorphic = false;
11790 break;
11791 }
11792
11793 concrete_child = concrete_child->sibling;
11794 abstract_child = abstract_child->sibling;
11795 }
11796
11797 /* Walk the origin's children in parallel to the concrete children.
11798 This helps match an origin child in case the debug info misses
11799 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11800 origin tree may not have the same tree structure as the concrete
11801 DIE, though. */
11802 die_info *corresponding_abstract_child
11803 = are_isomorphic ? origin_die->child : nullptr;
11804
11805 std::vector<sect_offset> offsets;
11806
11807 for (child_die = die->child;
11808 child_die && child_die->tag;
11809 child_die = child_die->sibling)
11810 {
11811 struct die_info *child_origin_die;
11812 struct dwarf2_cu *child_origin_cu;
11813
11814 /* We are trying to process concrete instance entries:
11815 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11816 it's not relevant to our analysis here. i.e. detecting DIEs that are
11817 present in the abstract instance but not referenced in the concrete
11818 one. */
11819 if (child_die->tag == DW_TAG_call_site
11820 || child_die->tag == DW_TAG_GNU_call_site)
11821 {
11822 if (are_isomorphic)
11823 corresponding_abstract_child
11824 = corresponding_abstract_child->sibling;
11825 continue;
11826 }
11827
11828 /* For each CHILD_DIE, find the corresponding child of
11829 ORIGIN_DIE. If there is more than one layer of
11830 DW_AT_abstract_origin, follow them all; there shouldn't be,
11831 but GCC versions at least through 4.4 generate this (GCC PR
11832 40573). */
11833 child_origin_die = child_die;
11834 child_origin_cu = cu;
11835 while (1)
11836 {
11837 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11838 child_origin_cu);
11839 if (attr == NULL)
11840 break;
11841 child_origin_die = follow_die_ref (child_origin_die, attr,
11842 &child_origin_cu);
11843 }
11844
11845 /* If missing DW_AT_abstract_origin, try the corresponding child
11846 of the origin. Clang emits such lexical scopes. */
11847 if (child_origin_die == child_die
11848 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11849 && are_isomorphic
11850 && child_die->tag == DW_TAG_lexical_block)
11851 child_origin_die = corresponding_abstract_child;
11852
11853 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11854 counterpart may exist. */
11855 if (child_origin_die != child_die)
11856 {
11857 if (child_die->tag != child_origin_die->tag
11858 && !(child_die->tag == DW_TAG_inlined_subroutine
11859 && child_origin_die->tag == DW_TAG_subprogram))
11860 complaint (_("Child DIE %s and its abstract origin %s have "
11861 "different tags"),
11862 sect_offset_str (child_die->sect_off),
11863 sect_offset_str (child_origin_die->sect_off));
11864 if (child_origin_die->parent != origin_die)
11865 complaint (_("Child DIE %s and its abstract origin %s have "
11866 "different parents"),
11867 sect_offset_str (child_die->sect_off),
11868 sect_offset_str (child_origin_die->sect_off));
11869 else
11870 offsets.push_back (child_origin_die->sect_off);
11871 }
11872
11873 if (are_isomorphic)
11874 corresponding_abstract_child = corresponding_abstract_child->sibling;
11875 }
11876 std::sort (offsets.begin (), offsets.end ());
11877 sect_offset *offsets_end = offsets.data () + offsets.size ();
11878 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
11879 if (offsetp[-1] == *offsetp)
11880 complaint (_("Multiple children of DIE %s refer "
11881 "to DIE %s as their abstract origin"),
11882 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
11883
11884 offsetp = offsets.data ();
11885 origin_child_die = origin_die->child;
11886 while (origin_child_die && origin_child_die->tag)
11887 {
11888 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11889 while (offsetp < offsets_end
11890 && *offsetp < origin_child_die->sect_off)
11891 offsetp++;
11892 if (offsetp >= offsets_end
11893 || *offsetp > origin_child_die->sect_off)
11894 {
11895 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11896 Check whether we're already processing ORIGIN_CHILD_DIE.
11897 This can happen with mutually referenced abstract_origins.
11898 PR 16581. */
11899 if (!origin_child_die->in_process)
11900 process_die (origin_child_die, origin_cu);
11901 }
11902 origin_child_die = origin_child_die->sibling;
11903 }
11904 origin_cu->list_in_scope = origin_previous_list_in_scope;
11905
11906 if (cu != origin_cu)
11907 compute_delayed_physnames (origin_cu);
11908 }
11909
11910 static void
11911 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11912 {
11913 struct objfile *objfile = cu->per_objfile->objfile;
11914 struct gdbarch *gdbarch = objfile->arch ();
11915 struct context_stack *newobj;
11916 CORE_ADDR lowpc;
11917 CORE_ADDR highpc;
11918 struct die_info *child_die;
11919 struct attribute *attr, *call_line, *call_file;
11920 const char *name;
11921 CORE_ADDR baseaddr;
11922 struct block *block;
11923 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11924 std::vector<struct symbol *> template_args;
11925 struct template_symbol *templ_func = NULL;
11926
11927 if (inlined_func)
11928 {
11929 /* If we do not have call site information, we can't show the
11930 caller of this inlined function. That's too confusing, so
11931 only use the scope for local variables. */
11932 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11933 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11934 if (call_line == NULL || call_file == NULL)
11935 {
11936 read_lexical_block_scope (die, cu);
11937 return;
11938 }
11939 }
11940
11941 baseaddr = objfile->text_section_offset ();
11942
11943 name = dwarf2_name (die, cu);
11944 if (name == nullptr)
11945 name = dw2_linkage_name (die, cu);
11946
11947 /* Ignore functions with missing or empty names. These are actually
11948 illegal according to the DWARF standard. */
11949 if (name == NULL)
11950 {
11951 complaint (_("missing name for subprogram DIE at %s"),
11952 sect_offset_str (die->sect_off));
11953 return;
11954 }
11955
11956 /* Ignore functions with missing or invalid low and high pc attributes. */
11957 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
11958 <= PC_BOUNDS_INVALID)
11959 {
11960 attr = dwarf2_attr (die, DW_AT_external, cu);
11961 if (attr == nullptr || !attr->as_boolean ())
11962 complaint (_("cannot get low and high bounds "
11963 "for subprogram DIE at %s"),
11964 sect_offset_str (die->sect_off));
11965 return;
11966 }
11967
11968 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11969 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11970
11971 /* If we have any template arguments, then we must allocate a
11972 different sort of symbol. */
11973 for (child_die = die->child; child_die; child_die = child_die->sibling)
11974 {
11975 if (child_die->tag == DW_TAG_template_type_param
11976 || child_die->tag == DW_TAG_template_value_param)
11977 {
11978 templ_func = new (&objfile->objfile_obstack) template_symbol;
11979 templ_func->subclass = SYMBOL_TEMPLATE;
11980 break;
11981 }
11982 }
11983
11984 gdb_assert (cu->get_builder () != nullptr);
11985 newobj = cu->get_builder ()->push_context (0, lowpc);
11986 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
11987 (struct symbol *) templ_func);
11988
11989 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
11990 set_objfile_main_name (objfile, newobj->name->linkage_name (),
11991 cu->per_cu->lang);
11992
11993 /* If there is a location expression for DW_AT_frame_base, record
11994 it. */
11995 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11996 if (attr != nullptr)
11997 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11998
11999 /* If there is a location for the static link, record it. */
12000 newobj->static_link = NULL;
12001 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12002 if (attr != nullptr)
12003 {
12004 newobj->static_link
12005 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12006 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12007 cu->addr_type ());
12008 }
12009
12010 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12011
12012 if (die->child != NULL)
12013 {
12014 child_die = die->child;
12015 while (child_die && child_die->tag)
12016 {
12017 if (child_die->tag == DW_TAG_template_type_param
12018 || child_die->tag == DW_TAG_template_value_param)
12019 {
12020 struct symbol *arg = new_symbol (child_die, NULL, cu);
12021
12022 if (arg != NULL)
12023 template_args.push_back (arg);
12024 }
12025 else
12026 process_die (child_die, cu);
12027 child_die = child_die->sibling;
12028 }
12029 }
12030
12031 inherit_abstract_dies (die, cu);
12032
12033 /* If we have a DW_AT_specification, we might need to import using
12034 directives from the context of the specification DIE. See the
12035 comment in determine_prefix. */
12036 if (cu->per_cu->lang == language_cplus
12037 && dwarf2_attr (die, DW_AT_specification, cu))
12038 {
12039 struct dwarf2_cu *spec_cu = cu;
12040 struct die_info *spec_die = die_specification (die, &spec_cu);
12041
12042 while (spec_die)
12043 {
12044 child_die = spec_die->child;
12045 while (child_die && child_die->tag)
12046 {
12047 if (child_die->tag == DW_TAG_imported_module)
12048 process_die (child_die, spec_cu);
12049 child_die = child_die->sibling;
12050 }
12051
12052 /* In some cases, GCC generates specification DIEs that
12053 themselves contain DW_AT_specification attributes. */
12054 spec_die = die_specification (spec_die, &spec_cu);
12055 }
12056 }
12057
12058 struct context_stack cstk = cu->get_builder ()->pop_context ();
12059 /* Make a block for the local symbols within. */
12060 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12061 cstk.static_link, lowpc, highpc);
12062
12063 /* For C++, set the block's scope. */
12064 if ((cu->per_cu->lang == language_cplus
12065 || cu->per_cu->lang == language_fortran
12066 || cu->per_cu->lang == language_d
12067 || cu->per_cu->lang == language_rust)
12068 && cu->processing_has_namespace_info)
12069 block_set_scope (block, determine_prefix (die, cu),
12070 &objfile->objfile_obstack);
12071
12072 /* If we have address ranges, record them. */
12073 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12074
12075 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12076
12077 /* Attach template arguments to function. */
12078 if (!template_args.empty ())
12079 {
12080 gdb_assert (templ_func != NULL);
12081
12082 templ_func->n_template_arguments = template_args.size ();
12083 templ_func->template_arguments
12084 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12085 templ_func->n_template_arguments);
12086 memcpy (templ_func->template_arguments,
12087 template_args.data (),
12088 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12089
12090 /* Make sure that the symtab is set on the new symbols. Even
12091 though they don't appear in this symtab directly, other parts
12092 of gdb assume that symbols do, and this is reasonably
12093 true. */
12094 for (symbol *sym : template_args)
12095 sym->set_symtab (templ_func->symtab ());
12096 }
12097
12098 /* In C++, we can have functions nested inside functions (e.g., when
12099 a function declares a class that has methods). This means that
12100 when we finish processing a function scope, we may need to go
12101 back to building a containing block's symbol lists. */
12102 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12103 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12104
12105 /* If we've finished processing a top-level function, subsequent
12106 symbols go in the file symbol list. */
12107 if (cu->get_builder ()->outermost_context_p ())
12108 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12109 }
12110
12111 /* Process all the DIES contained within a lexical block scope. Start
12112 a new scope, process the dies, and then close the scope. */
12113
12114 static void
12115 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12116 {
12117 struct objfile *objfile = cu->per_objfile->objfile;
12118 struct gdbarch *gdbarch = objfile->arch ();
12119 CORE_ADDR lowpc, highpc;
12120 struct die_info *child_die;
12121 CORE_ADDR baseaddr;
12122
12123 baseaddr = objfile->text_section_offset ();
12124
12125 /* Ignore blocks with missing or invalid low and high pc attributes. */
12126 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12127 as multiple lexical blocks? Handling children in a sane way would
12128 be nasty. Might be easier to properly extend generic blocks to
12129 describe ranges. */
12130 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12131 {
12132 case PC_BOUNDS_NOT_PRESENT:
12133 /* DW_TAG_lexical_block has no attributes, process its children as if
12134 there was no wrapping by that DW_TAG_lexical_block.
12135 GCC does no longer produces such DWARF since GCC r224161. */
12136 for (child_die = die->child;
12137 child_die != NULL && child_die->tag;
12138 child_die = child_die->sibling)
12139 {
12140 /* We might already be processing this DIE. This can happen
12141 in an unusual circumstance -- where a subroutine A
12142 appears lexically in another subroutine B, but A actually
12143 inlines B. The recursion is broken here, rather than in
12144 inherit_abstract_dies, because it seems better to simply
12145 drop concrete children here. */
12146 if (!child_die->in_process)
12147 process_die (child_die, cu);
12148 }
12149 return;
12150 case PC_BOUNDS_INVALID:
12151 return;
12152 }
12153 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12154 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12155
12156 cu->get_builder ()->push_context (0, lowpc);
12157 if (die->child != NULL)
12158 {
12159 child_die = die->child;
12160 while (child_die && child_die->tag)
12161 {
12162 process_die (child_die, cu);
12163 child_die = child_die->sibling;
12164 }
12165 }
12166 inherit_abstract_dies (die, cu);
12167 struct context_stack cstk = cu->get_builder ()->pop_context ();
12168
12169 if (*cu->get_builder ()->get_local_symbols () != NULL
12170 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12171 {
12172 struct block *block
12173 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12174 cstk.start_addr, highpc);
12175
12176 /* Note that recording ranges after traversing children, as we
12177 do here, means that recording a parent's ranges entails
12178 walking across all its children's ranges as they appear in
12179 the address map, which is quadratic behavior.
12180
12181 It would be nicer to record the parent's ranges before
12182 traversing its children, simply overriding whatever you find
12183 there. But since we don't even decide whether to create a
12184 block until after we've traversed its children, that's hard
12185 to do. */
12186 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12187 }
12188 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12189 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12190 }
12191
12192 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12193 struct dwarf2_cu *cu,
12194 dwarf_tag tag,
12195 std::vector<CORE_ADDR> &result);
12196
12197 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12198
12199 static void
12200 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12201 {
12202 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12203 struct objfile *objfile = per_objfile->objfile;
12204 struct gdbarch *gdbarch = objfile->arch ();
12205 CORE_ADDR pc, baseaddr;
12206 struct attribute *attr;
12207 void **slot;
12208 int nparams;
12209 struct die_info *child_die;
12210
12211 baseaddr = objfile->text_section_offset ();
12212
12213 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12214 if (attr == NULL)
12215 {
12216 /* This was a pre-DWARF-5 GNU extension alias
12217 for DW_AT_call_return_pc. */
12218 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12219 }
12220 if (!attr)
12221 {
12222 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12223 "DIE %s [in module %s]"),
12224 sect_offset_str (die->sect_off), objfile_name (objfile));
12225 return;
12226 }
12227 pc = attr->as_address () + baseaddr;
12228 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12229 pc -= baseaddr;
12230
12231 if (cu->call_site_htab == NULL)
12232 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12233 call_site::eq, NULL,
12234 &objfile->objfile_obstack,
12235 hashtab_obstack_allocate, NULL);
12236 struct call_site call_site_local (pc, nullptr, nullptr);
12237 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12238 if (*slot != NULL)
12239 {
12240 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12241 "DIE %s [in module %s]"),
12242 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12243 objfile_name (objfile));
12244 return;
12245 }
12246
12247 /* Count parameters at the caller. */
12248
12249 nparams = 0;
12250 for (child_die = die->child; child_die && child_die->tag;
12251 child_die = child_die->sibling)
12252 {
12253 if (child_die->tag != DW_TAG_call_site_parameter
12254 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12255 {
12256 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12257 "DW_TAG_call_site child DIE %s [in module %s]"),
12258 child_die->tag, sect_offset_str (child_die->sect_off),
12259 objfile_name (objfile));
12260 continue;
12261 }
12262
12263 nparams++;
12264 }
12265
12266 struct call_site *call_site
12267 = new (XOBNEWVAR (&objfile->objfile_obstack,
12268 struct call_site,
12269 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12270 struct call_site (pc, cu->per_cu, per_objfile);
12271 *slot = call_site;
12272
12273 /* We never call the destructor of call_site, so we must ensure it is
12274 trivially destructible. */
12275 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12276
12277 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12278 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12279 {
12280 struct die_info *func_die;
12281
12282 /* Skip also over DW_TAG_inlined_subroutine. */
12283 for (func_die = die->parent;
12284 func_die && func_die->tag != DW_TAG_subprogram
12285 && func_die->tag != DW_TAG_subroutine_type;
12286 func_die = func_die->parent);
12287
12288 /* DW_AT_call_all_calls is a superset
12289 of DW_AT_call_all_tail_calls. */
12290 if (func_die
12291 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12292 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12293 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12294 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12295 {
12296 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12297 not complete. But keep CALL_SITE for look ups via call_site_htab,
12298 both the initial caller containing the real return address PC and
12299 the final callee containing the current PC of a chain of tail
12300 calls do not need to have the tail call list complete. But any
12301 function candidate for a virtual tail call frame searched via
12302 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12303 determined unambiguously. */
12304 }
12305 else
12306 {
12307 struct type *func_type = NULL;
12308
12309 if (func_die)
12310 func_type = get_die_type (func_die, cu);
12311 if (func_type != NULL)
12312 {
12313 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12314
12315 /* Enlist this call site to the function. */
12316 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12317 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12318 }
12319 else
12320 complaint (_("Cannot find function owning DW_TAG_call_site "
12321 "DIE %s [in module %s]"),
12322 sect_offset_str (die->sect_off), objfile_name (objfile));
12323 }
12324 }
12325
12326 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12327 if (attr == NULL)
12328 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12329 if (attr == NULL)
12330 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12331 if (attr == NULL)
12332 {
12333 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12334 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12335 }
12336
12337 call_site->target.set_loc_dwarf_block (nullptr);
12338 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12339 /* Keep NULL DWARF_BLOCK. */;
12340 else if (attr->form_is_block ())
12341 {
12342 struct dwarf2_locexpr_baton *dlbaton;
12343 struct dwarf_block *block = attr->as_block ();
12344
12345 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12346 dlbaton->data = block->data;
12347 dlbaton->size = block->size;
12348 dlbaton->per_objfile = per_objfile;
12349 dlbaton->per_cu = cu->per_cu;
12350
12351 call_site->target.set_loc_dwarf_block (dlbaton);
12352 }
12353 else if (attr->form_is_ref ())
12354 {
12355 struct dwarf2_cu *target_cu = cu;
12356 struct die_info *target_die;
12357
12358 target_die = follow_die_ref (die, attr, &target_cu);
12359 gdb_assert (target_cu->per_objfile->objfile == objfile);
12360
12361 struct attribute *ranges_attr
12362 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12363
12364 if (die_is_declaration (target_die, target_cu))
12365 {
12366 const char *target_physname;
12367
12368 /* Prefer the mangled name; otherwise compute the demangled one. */
12369 target_physname = dw2_linkage_name (target_die, target_cu);
12370 if (target_physname == NULL)
12371 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12372 if (target_physname == NULL)
12373 complaint (_("DW_AT_call_target target DIE has invalid "
12374 "physname, for referencing DIE %s [in module %s]"),
12375 sect_offset_str (die->sect_off), objfile_name (objfile));
12376 else
12377 call_site->target.set_loc_physname (target_physname);
12378 }
12379 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12380 {
12381 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12382 + target_cu->gnu_ranges_base);
12383 std::vector<CORE_ADDR> addresses;
12384 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12385 target_die->tag, addresses);
12386 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12387 addresses.size ());
12388 std::copy (addresses.begin (), addresses.end (), saved);
12389 call_site->target.set_loc_array (addresses.size (), saved);
12390 }
12391 else
12392 {
12393 CORE_ADDR lowpc;
12394
12395 /* DW_AT_entry_pc should be preferred. */
12396 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12397 nullptr, nullptr)
12398 <= PC_BOUNDS_INVALID)
12399 complaint (_("DW_AT_call_target target DIE has invalid "
12400 "low pc, for referencing DIE %s [in module %s]"),
12401 sect_offset_str (die->sect_off), objfile_name (objfile));
12402 else
12403 {
12404 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12405 - baseaddr);
12406 call_site->target.set_loc_physaddr (lowpc);
12407 }
12408 }
12409 }
12410 else
12411 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12412 "block nor reference, for DIE %s [in module %s]"),
12413 sect_offset_str (die->sect_off), objfile_name (objfile));
12414
12415 for (child_die = die->child;
12416 child_die && child_die->tag;
12417 child_die = child_die->sibling)
12418 {
12419 struct call_site_parameter *parameter;
12420 struct attribute *loc, *origin;
12421
12422 if (child_die->tag != DW_TAG_call_site_parameter
12423 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12424 {
12425 /* Already printed the complaint above. */
12426 continue;
12427 }
12428
12429 gdb_assert (call_site->parameter_count < nparams);
12430 parameter = &call_site->parameter[call_site->parameter_count];
12431
12432 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12433 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12434 register is contained in DW_AT_call_value. */
12435
12436 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12437 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12438 if (origin == NULL)
12439 {
12440 /* This was a pre-DWARF-5 GNU extension alias
12441 for DW_AT_call_parameter. */
12442 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12443 }
12444 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12445 {
12446 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12447
12448 sect_offset sect_off = origin->get_ref_die_offset ();
12449 if (!cu->header.offset_in_cu_p (sect_off))
12450 {
12451 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12452 binding can be done only inside one CU. Such referenced DIE
12453 therefore cannot be even moved to DW_TAG_partial_unit. */
12454 complaint (_("DW_AT_call_parameter offset is not in CU for "
12455 "DW_TAG_call_site child DIE %s [in module %s]"),
12456 sect_offset_str (child_die->sect_off),
12457 objfile_name (objfile));
12458 continue;
12459 }
12460 parameter->u.param_cu_off
12461 = (cu_offset) (sect_off - cu->header.sect_off);
12462 }
12463 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12464 {
12465 complaint (_("No DW_FORM_block* DW_AT_location for "
12466 "DW_TAG_call_site child DIE %s [in module %s]"),
12467 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12468 continue;
12469 }
12470 else
12471 {
12472 struct dwarf_block *block = loc->as_block ();
12473
12474 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12475 (block->data, &block->data[block->size]);
12476 if (parameter->u.dwarf_reg != -1)
12477 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12478 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12479 &block->data[block->size],
12480 &parameter->u.fb_offset))
12481 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12482 else
12483 {
12484 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12485 "for DW_FORM_block* DW_AT_location is supported for "
12486 "DW_TAG_call_site child DIE %s "
12487 "[in module %s]"),
12488 sect_offset_str (child_die->sect_off),
12489 objfile_name (objfile));
12490 continue;
12491 }
12492 }
12493
12494 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12495 if (attr == NULL)
12496 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12497 if (attr == NULL || !attr->form_is_block ())
12498 {
12499 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12500 "DW_TAG_call_site child DIE %s [in module %s]"),
12501 sect_offset_str (child_die->sect_off),
12502 objfile_name (objfile));
12503 continue;
12504 }
12505
12506 struct dwarf_block *block = attr->as_block ();
12507 parameter->value = block->data;
12508 parameter->value_size = block->size;
12509
12510 /* Parameters are not pre-cleared by memset above. */
12511 parameter->data_value = NULL;
12512 parameter->data_value_size = 0;
12513 call_site->parameter_count++;
12514
12515 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12516 if (attr == NULL)
12517 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12518 if (attr != nullptr)
12519 {
12520 if (!attr->form_is_block ())
12521 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12522 "DW_TAG_call_site child DIE %s [in module %s]"),
12523 sect_offset_str (child_die->sect_off),
12524 objfile_name (objfile));
12525 else
12526 {
12527 block = attr->as_block ();
12528 parameter->data_value = block->data;
12529 parameter->data_value_size = block->size;
12530 }
12531 }
12532 }
12533 }
12534
12535 /* Helper function for read_variable. If DIE represents a virtual
12536 table, then return the type of the concrete object that is
12537 associated with the virtual table. Otherwise, return NULL. */
12538
12539 static struct type *
12540 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12541 {
12542 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12543 if (attr == NULL)
12544 return NULL;
12545
12546 /* Find the type DIE. */
12547 struct die_info *type_die = NULL;
12548 struct dwarf2_cu *type_cu = cu;
12549
12550 if (attr->form_is_ref ())
12551 type_die = follow_die_ref (die, attr, &type_cu);
12552 if (type_die == NULL)
12553 return NULL;
12554
12555 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12556 return NULL;
12557 return die_containing_type (type_die, type_cu);
12558 }
12559
12560 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12561
12562 static void
12563 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12564 {
12565 struct rust_vtable_symbol *storage = NULL;
12566
12567 if (cu->per_cu->lang == language_rust)
12568 {
12569 struct type *containing_type = rust_containing_type (die, cu);
12570
12571 if (containing_type != NULL)
12572 {
12573 struct objfile *objfile = cu->per_objfile->objfile;
12574
12575 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12576 storage->concrete_type = containing_type;
12577 storage->subclass = SYMBOL_RUST_VTABLE;
12578 }
12579 }
12580
12581 struct symbol *res = new_symbol (die, NULL, cu, storage);
12582 struct attribute *abstract_origin
12583 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12584 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12585 if (res == NULL && loc && abstract_origin)
12586 {
12587 /* We have a variable without a name, but with a location and an abstract
12588 origin. This may be a concrete instance of an abstract variable
12589 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12590 later. */
12591 struct dwarf2_cu *origin_cu = cu;
12592 struct die_info *origin_die
12593 = follow_die_ref (die, abstract_origin, &origin_cu);
12594 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12595 per_objfile->per_bfd->abstract_to_concrete
12596 [origin_die->sect_off].push_back (die->sect_off);
12597 }
12598 }
12599
12600 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12601 reading .debug_rnglists.
12602 Callback's type should be:
12603 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12604 Return true if the attributes are present and valid, otherwise,
12605 return false. */
12606
12607 template <typename Callback>
12608 static bool
12609 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12610 dwarf_tag tag, Callback &&callback)
12611 {
12612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12613 struct objfile *objfile = per_objfile->objfile;
12614 bfd *obfd = objfile->obfd;
12615 /* Base address selection entry. */
12616 gdb::optional<CORE_ADDR> base;
12617 const gdb_byte *buffer;
12618 bool overflow = false;
12619 ULONGEST addr_index;
12620 struct dwarf2_section_info *rnglists_section;
12621
12622 base = cu->base_address;
12623 rnglists_section = cu_debug_rnglists_section (cu, tag);
12624 rnglists_section->read (objfile);
12625
12626 if (offset >= rnglists_section->size)
12627 {
12628 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12629 offset);
12630 return false;
12631 }
12632 buffer = rnglists_section->buffer + offset;
12633
12634 while (1)
12635 {
12636 /* Initialize it due to a false compiler warning. */
12637 CORE_ADDR range_beginning = 0, range_end = 0;
12638 const gdb_byte *buf_end = (rnglists_section->buffer
12639 + rnglists_section->size);
12640 unsigned int bytes_read;
12641
12642 if (buffer == buf_end)
12643 {
12644 overflow = true;
12645 break;
12646 }
12647 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12648 switch (rlet)
12649 {
12650 case DW_RLE_end_of_list:
12651 break;
12652 case DW_RLE_base_address:
12653 if (buffer + cu->header.addr_size > buf_end)
12654 {
12655 overflow = true;
12656 break;
12657 }
12658 base = cu->header.read_address (obfd, buffer, &bytes_read);
12659 buffer += bytes_read;
12660 break;
12661 case DW_RLE_base_addressx:
12662 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12663 buffer += bytes_read;
12664 base = read_addr_index (cu, addr_index);
12665 break;
12666 case DW_RLE_start_length:
12667 if (buffer + cu->header.addr_size > buf_end)
12668 {
12669 overflow = true;
12670 break;
12671 }
12672 range_beginning = cu->header.read_address (obfd, buffer,
12673 &bytes_read);
12674 buffer += bytes_read;
12675 range_end = (range_beginning
12676 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12677 buffer += bytes_read;
12678 if (buffer > buf_end)
12679 {
12680 overflow = true;
12681 break;
12682 }
12683 break;
12684 case DW_RLE_startx_length:
12685 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12686 buffer += bytes_read;
12687 range_beginning = read_addr_index (cu, addr_index);
12688 if (buffer > buf_end)
12689 {
12690 overflow = true;
12691 break;
12692 }
12693 range_end = (range_beginning
12694 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12695 buffer += bytes_read;
12696 break;
12697 case DW_RLE_offset_pair:
12698 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12699 buffer += bytes_read;
12700 if (buffer > buf_end)
12701 {
12702 overflow = true;
12703 break;
12704 }
12705 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12706 buffer += bytes_read;
12707 if (buffer > buf_end)
12708 {
12709 overflow = true;
12710 break;
12711 }
12712 break;
12713 case DW_RLE_start_end:
12714 if (buffer + 2 * cu->header.addr_size > buf_end)
12715 {
12716 overflow = true;
12717 break;
12718 }
12719 range_beginning = cu->header.read_address (obfd, buffer,
12720 &bytes_read);
12721 buffer += bytes_read;
12722 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12723 buffer += bytes_read;
12724 break;
12725 case DW_RLE_startx_endx:
12726 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12727 buffer += bytes_read;
12728 range_beginning = read_addr_index (cu, addr_index);
12729 if (buffer > buf_end)
12730 {
12731 overflow = true;
12732 break;
12733 }
12734 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12735 buffer += bytes_read;
12736 range_end = read_addr_index (cu, addr_index);
12737 break;
12738 default:
12739 complaint (_("Invalid .debug_rnglists data (no base address)"));
12740 return false;
12741 }
12742 if (rlet == DW_RLE_end_of_list || overflow)
12743 break;
12744 if (rlet == DW_RLE_base_address)
12745 continue;
12746
12747 if (range_beginning > range_end)
12748 {
12749 /* Inverted range entries are invalid. */
12750 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12751 return false;
12752 }
12753
12754 /* Empty range entries have no effect. */
12755 if (range_beginning == range_end)
12756 continue;
12757
12758 /* Only DW_RLE_offset_pair needs the base address added. */
12759 if (rlet == DW_RLE_offset_pair)
12760 {
12761 if (!base.has_value ())
12762 {
12763 /* We have no valid base address for the DW_RLE_offset_pair. */
12764 complaint (_("Invalid .debug_rnglists data (no base address for "
12765 "DW_RLE_offset_pair)"));
12766 return false;
12767 }
12768
12769 range_beginning += *base;
12770 range_end += *base;
12771 }
12772
12773 /* A not-uncommon case of bad debug info.
12774 Don't pollute the addrmap with bad data. */
12775 if (range_beginning == 0
12776 && !per_objfile->per_bfd->has_section_at_zero)
12777 {
12778 complaint (_(".debug_rnglists entry has start address of zero"
12779 " [in module %s]"), objfile_name (objfile));
12780 continue;
12781 }
12782
12783 callback (range_beginning, range_end);
12784 }
12785
12786 if (overflow)
12787 {
12788 complaint (_("Offset %d is not terminated "
12789 "for DW_AT_ranges attribute"),
12790 offset);
12791 return false;
12792 }
12793
12794 return true;
12795 }
12796
12797 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12798 Callback's type should be:
12799 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12800 Return 1 if the attributes are present and valid, otherwise, return 0. */
12801
12802 template <typename Callback>
12803 static int
12804 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12805 Callback &&callback)
12806 {
12807 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12808 struct objfile *objfile = per_objfile->objfile;
12809 struct comp_unit_head *cu_header = &cu->header;
12810 bfd *obfd = objfile->obfd;
12811 unsigned int addr_size = cu_header->addr_size;
12812 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12813 /* Base address selection entry. */
12814 gdb::optional<CORE_ADDR> base;
12815 unsigned int dummy;
12816 const gdb_byte *buffer;
12817
12818 if (cu_header->version >= 5)
12819 return dwarf2_rnglists_process (offset, cu, tag, callback);
12820
12821 base = cu->base_address;
12822
12823 per_objfile->per_bfd->ranges.read (objfile);
12824 if (offset >= per_objfile->per_bfd->ranges.size)
12825 {
12826 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12827 offset);
12828 return 0;
12829 }
12830 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12831
12832 while (1)
12833 {
12834 CORE_ADDR range_beginning, range_end;
12835
12836 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12837 buffer += addr_size;
12838 range_end = cu->header.read_address (obfd, buffer, &dummy);
12839 buffer += addr_size;
12840 offset += 2 * addr_size;
12841
12842 /* An end of list marker is a pair of zero addresses. */
12843 if (range_beginning == 0 && range_end == 0)
12844 /* Found the end of list entry. */
12845 break;
12846
12847 /* Each base address selection entry is a pair of 2 values.
12848 The first is the largest possible address, the second is
12849 the base address. Check for a base address here. */
12850 if ((range_beginning & mask) == mask)
12851 {
12852 /* If we found the largest possible address, then we already
12853 have the base address in range_end. */
12854 base = range_end;
12855 continue;
12856 }
12857
12858 if (!base.has_value ())
12859 {
12860 /* We have no valid base address for the ranges
12861 data. */
12862 complaint (_("Invalid .debug_ranges data (no base address)"));
12863 return 0;
12864 }
12865
12866 if (range_beginning > range_end)
12867 {
12868 /* Inverted range entries are invalid. */
12869 complaint (_("Invalid .debug_ranges data (inverted range)"));
12870 return 0;
12871 }
12872
12873 /* Empty range entries have no effect. */
12874 if (range_beginning == range_end)
12875 continue;
12876
12877 range_beginning += *base;
12878 range_end += *base;
12879
12880 /* A not-uncommon case of bad debug info.
12881 Don't pollute the addrmap with bad data. */
12882 if (range_beginning == 0
12883 && !per_objfile->per_bfd->has_section_at_zero)
12884 {
12885 complaint (_(".debug_ranges entry has start address of zero"
12886 " [in module %s]"), objfile_name (objfile));
12887 continue;
12888 }
12889
12890 callback (range_beginning, range_end);
12891 }
12892
12893 return 1;
12894 }
12895
12896 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12897 Return 1 if the attributes are present and valid, otherwise, return 0.
12898 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12899 ranges in MAP are set, using DATUM as the value. */
12900
12901 static int
12902 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12903 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12904 addrmap *map, void *datum, dwarf_tag tag)
12905 {
12906 struct objfile *objfile = cu->per_objfile->objfile;
12907 struct gdbarch *gdbarch = objfile->arch ();
12908 const CORE_ADDR baseaddr = objfile->text_section_offset ();
12909 int low_set = 0;
12910 CORE_ADDR low = 0;
12911 CORE_ADDR high = 0;
12912 int retval;
12913
12914 retval = dwarf2_ranges_process (offset, cu, tag,
12915 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12916 {
12917 if (map != nullptr)
12918 {
12919 CORE_ADDR lowpc;
12920 CORE_ADDR highpc;
12921
12922 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12923 range_beginning + baseaddr)
12924 - baseaddr);
12925 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12926 range_end + baseaddr)
12927 - baseaddr);
12928 addrmap_set_empty (map, lowpc, highpc - 1, datum);
12929 }
12930
12931 /* FIXME: This is recording everything as a low-high
12932 segment of consecutive addresses. We should have a
12933 data structure for discontiguous block ranges
12934 instead. */
12935 if (! low_set)
12936 {
12937 low = range_beginning;
12938 high = range_end;
12939 low_set = 1;
12940 }
12941 else
12942 {
12943 if (range_beginning < low)
12944 low = range_beginning;
12945 if (range_end > high)
12946 high = range_end;
12947 }
12948 });
12949 if (!retval)
12950 return 0;
12951
12952 if (! low_set)
12953 /* If the first entry is an end-of-list marker, the range
12954 describes an empty scope, i.e. no instructions. */
12955 return 0;
12956
12957 if (low_return)
12958 *low_return = low;
12959 if (high_return)
12960 *high_return = high;
12961 return 1;
12962 }
12963
12964 /* Process ranges and fill in a vector of the low PC values only. */
12965
12966 static void
12967 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
12968 dwarf_tag tag,
12969 std::vector<CORE_ADDR> &result)
12970 {
12971 dwarf2_ranges_process (offset, cu, tag,
12972 [&] (CORE_ADDR start, CORE_ADDR end)
12973 {
12974 result.push_back (start);
12975 });
12976 }
12977
12978 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12979 definition for the return value. *LOWPC and *HIGHPC are set iff
12980 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12981
12982 static enum pc_bounds_kind
12983 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12984 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12985 addrmap *map, void *datum)
12986 {
12987 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12988 struct attribute *attr;
12989 struct attribute *attr_high;
12990 CORE_ADDR low = 0;
12991 CORE_ADDR high = 0;
12992 enum pc_bounds_kind ret;
12993
12994 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12995 if (attr_high)
12996 {
12997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12998 if (attr != nullptr)
12999 {
13000 low = attr->as_address ();
13001 high = attr_high->as_address ();
13002 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13003 high += low;
13004 }
13005 else
13006 /* Found high w/o low attribute. */
13007 return PC_BOUNDS_INVALID;
13008
13009 /* Found consecutive range of addresses. */
13010 ret = PC_BOUNDS_HIGH_LOW;
13011 }
13012 else
13013 {
13014 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13015 if (attr != nullptr && attr->form_is_unsigned ())
13016 {
13017 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13018 on DWARF version). */
13019 ULONGEST ranges_offset = attr->as_unsigned ();
13020
13021 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13022 this value. */
13023 if (die->tag != DW_TAG_compile_unit)
13024 ranges_offset += cu->gnu_ranges_base;
13025
13026 /* Value of the DW_AT_ranges attribute is the offset in the
13027 .debug_ranges section. */
13028 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13029 map, datum, die->tag))
13030 return PC_BOUNDS_INVALID;
13031 /* Found discontinuous range of addresses. */
13032 ret = PC_BOUNDS_RANGES;
13033 }
13034 else
13035 return PC_BOUNDS_NOT_PRESENT;
13036 }
13037
13038 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13039 if (high <= low)
13040 return PC_BOUNDS_INVALID;
13041
13042 /* When using the GNU linker, .gnu.linkonce. sections are used to
13043 eliminate duplicate copies of functions and vtables and such.
13044 The linker will arbitrarily choose one and discard the others.
13045 The AT_*_pc values for such functions refer to local labels in
13046 these sections. If the section from that file was discarded, the
13047 labels are not in the output, so the relocs get a value of 0.
13048 If this is a discarded function, mark the pc bounds as invalid,
13049 so that GDB will ignore it. */
13050 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13051 return PC_BOUNDS_INVALID;
13052
13053 *lowpc = low;
13054 if (highpc)
13055 *highpc = high;
13056 return ret;
13057 }
13058
13059 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13060 its low and high PC addresses. Do nothing if these addresses could not
13061 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13062 and HIGHPC to the high address if greater than HIGHPC. */
13063
13064 static void
13065 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13067 struct dwarf2_cu *cu)
13068 {
13069 CORE_ADDR low, high;
13070 struct die_info *child = die->child;
13071
13072 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13073 >= PC_BOUNDS_RANGES)
13074 {
13075 *lowpc = std::min (*lowpc, low);
13076 *highpc = std::max (*highpc, high);
13077 }
13078
13079 /* If the language does not allow nested subprograms (either inside
13080 subprograms or lexical blocks), we're done. */
13081 if (cu->per_cu->lang != language_ada)
13082 return;
13083
13084 /* Check all the children of the given DIE. If it contains nested
13085 subprograms, then check their pc bounds. Likewise, we need to
13086 check lexical blocks as well, as they may also contain subprogram
13087 definitions. */
13088 while (child && child->tag)
13089 {
13090 if (child->tag == DW_TAG_subprogram
13091 || child->tag == DW_TAG_lexical_block)
13092 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13093 child = child->sibling;
13094 }
13095 }
13096
13097 /* Get the low and high pc's represented by the scope DIE, and store
13098 them in *LOWPC and *HIGHPC. If the correct values can't be
13099 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13100
13101 static void
13102 get_scope_pc_bounds (struct die_info *die,
13103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13104 struct dwarf2_cu *cu)
13105 {
13106 CORE_ADDR best_low = (CORE_ADDR) -1;
13107 CORE_ADDR best_high = (CORE_ADDR) 0;
13108 CORE_ADDR current_low, current_high;
13109
13110 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13111 nullptr, nullptr)
13112 >= PC_BOUNDS_RANGES)
13113 {
13114 best_low = current_low;
13115 best_high = current_high;
13116 }
13117 else
13118 {
13119 struct die_info *child = die->child;
13120
13121 while (child && child->tag)
13122 {
13123 switch (child->tag) {
13124 case DW_TAG_subprogram:
13125 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13126 break;
13127 case DW_TAG_namespace:
13128 case DW_TAG_module:
13129 /* FIXME: carlton/2004-01-16: Should we do this for
13130 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13131 that current GCC's always emit the DIEs corresponding
13132 to definitions of methods of classes as children of a
13133 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13134 the DIEs giving the declarations, which could be
13135 anywhere). But I don't see any reason why the
13136 standards says that they have to be there. */
13137 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13138
13139 if (current_low != ((CORE_ADDR) -1))
13140 {
13141 best_low = std::min (best_low, current_low);
13142 best_high = std::max (best_high, current_high);
13143 }
13144 break;
13145 default:
13146 /* Ignore. */
13147 break;
13148 }
13149
13150 child = child->sibling;
13151 }
13152 }
13153
13154 *lowpc = best_low;
13155 *highpc = best_high;
13156 }
13157
13158 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13159 in DIE. */
13160
13161 static void
13162 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13163 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13164 {
13165 struct objfile *objfile = cu->per_objfile->objfile;
13166 struct gdbarch *gdbarch = objfile->arch ();
13167 struct attribute *attr;
13168 struct attribute *attr_high;
13169
13170 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13171 if (attr_high)
13172 {
13173 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13174 if (attr != nullptr)
13175 {
13176 CORE_ADDR low = attr->as_address ();
13177 CORE_ADDR high = attr_high->as_address ();
13178
13179 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13180 high += low;
13181
13182 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13183 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13184 cu->get_builder ()->record_block_range (block, low, high - 1);
13185 }
13186 }
13187
13188 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13189 if (attr != nullptr && attr->form_is_unsigned ())
13190 {
13191 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13192 on DWARF version). */
13193 ULONGEST ranges_offset = attr->as_unsigned ();
13194
13195 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13196 this value. */
13197 if (die->tag != DW_TAG_compile_unit)
13198 ranges_offset += cu->gnu_ranges_base;
13199
13200 std::vector<blockrange> blockvec;
13201 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13202 [&] (CORE_ADDR start, CORE_ADDR end)
13203 {
13204 start += baseaddr;
13205 end += baseaddr;
13206 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13207 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13208 cu->get_builder ()->record_block_range (block, start, end - 1);
13209 blockvec.emplace_back (start, end);
13210 });
13211
13212 block->set_ranges (make_blockranges (objfile, blockvec));
13213 }
13214 }
13215
13216 /* Check whether the producer field indicates either of GCC < 4.6, or the
13217 Intel C/C++ compiler, and cache the result in CU. */
13218
13219 static void
13220 check_producer (struct dwarf2_cu *cu)
13221 {
13222 int major, minor;
13223
13224 if (cu->producer == NULL)
13225 {
13226 /* For unknown compilers expect their behavior is DWARF version
13227 compliant.
13228
13229 GCC started to support .debug_types sections by -gdwarf-4 since
13230 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13231 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13232 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13233 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13234 }
13235 else if (producer_is_gcc (cu->producer, &major, &minor))
13236 {
13237 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13238 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13239 cu->producer_is_gcc_11 = major == 11;
13240 }
13241 else if (producer_is_icc (cu->producer, &major, &minor))
13242 {
13243 cu->producer_is_icc = true;
13244 cu->producer_is_icc_lt_14 = major < 14;
13245 }
13246 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13247 cu->producer_is_codewarrior = true;
13248 else
13249 {
13250 /* For other non-GCC compilers, expect their behavior is DWARF version
13251 compliant. */
13252 }
13253
13254 cu->checked_producer = true;
13255 }
13256
13257 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13258 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13259 during 4.6.0 experimental. */
13260
13261 static bool
13262 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13263 {
13264 if (!cu->checked_producer)
13265 check_producer (cu);
13266
13267 return cu->producer_is_gxx_lt_4_6;
13268 }
13269
13270
13271 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13272 with incorrect is_stmt attributes. */
13273
13274 static bool
13275 producer_is_codewarrior (struct dwarf2_cu *cu)
13276 {
13277 if (!cu->checked_producer)
13278 check_producer (cu);
13279
13280 return cu->producer_is_codewarrior;
13281 }
13282
13283 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13284 If that attribute is not available, return the appropriate
13285 default. */
13286
13287 static enum dwarf_access_attribute
13288 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13289 {
13290 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13291 if (attr != nullptr)
13292 {
13293 LONGEST value = attr->constant_value (-1);
13294 if (value == DW_ACCESS_public
13295 || value == DW_ACCESS_protected
13296 || value == DW_ACCESS_private)
13297 return (dwarf_access_attribute) value;
13298 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13299 plongest (value));
13300 }
13301
13302 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13303 {
13304 /* The default DWARF 2 accessibility for members is public, the default
13305 accessibility for inheritance is private. */
13306
13307 if (die->tag != DW_TAG_inheritance)
13308 return DW_ACCESS_public;
13309 else
13310 return DW_ACCESS_private;
13311 }
13312 else
13313 {
13314 /* DWARF 3+ defines the default accessibility a different way. The same
13315 rules apply now for DW_TAG_inheritance as for the members and it only
13316 depends on the container kind. */
13317
13318 if (die->parent->tag == DW_TAG_class_type)
13319 return DW_ACCESS_private;
13320 else
13321 return DW_ACCESS_public;
13322 }
13323 }
13324
13325 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13326 *OFFSET to the byte offset. If the attribute was not found return
13327 0, otherwise return 1. If it was found but could not properly be
13328 handled, set *OFFSET to 0. */
13329
13330 static int
13331 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13332 LONGEST *offset)
13333 {
13334 struct attribute *attr;
13335
13336 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13337 if (attr != NULL)
13338 {
13339 *offset = 0;
13340
13341 /* Note that we do not check for a section offset first here.
13342 This is because DW_AT_data_member_location is new in DWARF 4,
13343 so if we see it, we can assume that a constant form is really
13344 a constant and not a section offset. */
13345 if (attr->form_is_constant ())
13346 *offset = attr->constant_value (0);
13347 else if (attr->form_is_section_offset ())
13348 dwarf2_complex_location_expr_complaint ();
13349 else if (attr->form_is_block ())
13350 *offset = decode_locdesc (attr->as_block (), cu);
13351 else
13352 dwarf2_complex_location_expr_complaint ();
13353
13354 return 1;
13355 }
13356 else
13357 {
13358 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13359 if (attr != nullptr)
13360 {
13361 *offset = attr->constant_value (0);
13362 return 1;
13363 }
13364 }
13365
13366 return 0;
13367 }
13368
13369 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13370 store the results in FIELD. */
13371
13372 static void
13373 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13374 struct field *field)
13375 {
13376 struct attribute *attr;
13377
13378 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13379 if (attr != NULL)
13380 {
13381 if (attr->form_is_constant ())
13382 {
13383 LONGEST offset = attr->constant_value (0);
13384
13385 /* Work around this GCC 11 bug, where it would erroneously use -1
13386 data member locations, instead of 0:
13387
13388 Negative DW_AT_data_member_location
13389 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13390 */
13391 if (offset == -1 && cu->producer_is_gcc_11)
13392 {
13393 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13394 offset = 0;
13395 }
13396
13397 field->set_loc_bitpos (offset * bits_per_byte);
13398 }
13399 else if (attr->form_is_section_offset ())
13400 dwarf2_complex_location_expr_complaint ();
13401 else if (attr->form_is_block ())
13402 {
13403 bool handled;
13404 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13405 if (handled)
13406 field->set_loc_bitpos (offset * bits_per_byte);
13407 else
13408 {
13409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13410 struct objfile *objfile = per_objfile->objfile;
13411 struct dwarf2_locexpr_baton *dlbaton
13412 = XOBNEW (&objfile->objfile_obstack,
13413 struct dwarf2_locexpr_baton);
13414 dlbaton->data = attr->as_block ()->data;
13415 dlbaton->size = attr->as_block ()->size;
13416 /* When using this baton, we want to compute the address
13417 of the field, not the value. This is why
13418 is_reference is set to false here. */
13419 dlbaton->is_reference = false;
13420 dlbaton->per_objfile = per_objfile;
13421 dlbaton->per_cu = cu->per_cu;
13422
13423 field->set_loc_dwarf_block (dlbaton);
13424 }
13425 }
13426 else
13427 dwarf2_complex_location_expr_complaint ();
13428 }
13429 else
13430 {
13431 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13432 if (attr != nullptr)
13433 field->set_loc_bitpos (attr->constant_value (0));
13434 }
13435 }
13436
13437 /* Add an aggregate field to the field list. */
13438
13439 static void
13440 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13441 struct dwarf2_cu *cu)
13442 {
13443 struct objfile *objfile = cu->per_objfile->objfile;
13444 struct gdbarch *gdbarch = objfile->arch ();
13445 struct nextfield *new_field;
13446 struct attribute *attr;
13447 struct field *fp;
13448 const char *fieldname = "";
13449
13450 if (die->tag == DW_TAG_inheritance)
13451 {
13452 fip->baseclasses.emplace_back ();
13453 new_field = &fip->baseclasses.back ();
13454 }
13455 else
13456 {
13457 fip->fields.emplace_back ();
13458 new_field = &fip->fields.back ();
13459 }
13460
13461 new_field->offset = die->sect_off;
13462
13463 new_field->accessibility = dwarf2_access_attribute (die, cu);
13464 if (new_field->accessibility != DW_ACCESS_public)
13465 fip->non_public_fields = true;
13466
13467 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13468 if (attr != nullptr)
13469 new_field->virtuality = attr->as_virtuality ();
13470 else
13471 new_field->virtuality = DW_VIRTUALITY_none;
13472
13473 fp = &new_field->field;
13474
13475 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13476 && !die_is_declaration (die, cu))
13477 {
13478 if (die->tag == DW_TAG_namelist_item)
13479 {
13480 /* Typically, DW_TAG_namelist_item are references to namelist items.
13481 If so, follow that reference. */
13482 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13483 struct die_info *item_die = nullptr;
13484 struct dwarf2_cu *item_cu = cu;
13485 if (attr1->form_is_ref ())
13486 item_die = follow_die_ref (die, attr1, &item_cu);
13487 if (item_die != nullptr)
13488 die = item_die;
13489 }
13490 /* Data member other than a C++ static data member. */
13491
13492 /* Get type of field. */
13493 fp->set_type (die_type (die, cu));
13494
13495 fp->set_loc_bitpos (0);
13496
13497 /* Get bit size of field (zero if none). */
13498 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13499 if (attr != nullptr)
13500 {
13501 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13502 }
13503 else
13504 {
13505 FIELD_BITSIZE (*fp) = 0;
13506 }
13507
13508 /* Get bit offset of field. */
13509 handle_member_location (die, cu, fp);
13510 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13511 if (attr != nullptr && attr->form_is_constant ())
13512 {
13513 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13514 {
13515 /* For big endian bits, the DW_AT_bit_offset gives the
13516 additional bit offset from the MSB of the containing
13517 anonymous object to the MSB of the field. We don't
13518 have to do anything special since we don't need to
13519 know the size of the anonymous object. */
13520 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13521 }
13522 else
13523 {
13524 /* For little endian bits, compute the bit offset to the
13525 MSB of the anonymous object, subtract off the number of
13526 bits from the MSB of the field to the MSB of the
13527 object, and then subtract off the number of bits of
13528 the field itself. The result is the bit offset of
13529 the LSB of the field. */
13530 int anonymous_size;
13531 int bit_offset = attr->constant_value (0);
13532
13533 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13534 if (attr != nullptr && attr->form_is_constant ())
13535 {
13536 /* The size of the anonymous object containing
13537 the bit field is explicit, so use the
13538 indicated size (in bytes). */
13539 anonymous_size = attr->constant_value (0);
13540 }
13541 else
13542 {
13543 /* The size of the anonymous object containing
13544 the bit field must be inferred from the type
13545 attribute of the data member containing the
13546 bit field. */
13547 anonymous_size = TYPE_LENGTH (fp->type ());
13548 }
13549 fp->set_loc_bitpos (fp->loc_bitpos ()
13550 + anonymous_size * bits_per_byte
13551 - bit_offset - FIELD_BITSIZE (*fp));
13552 }
13553 }
13554
13555 /* Get name of field. */
13556 fieldname = dwarf2_name (die, cu);
13557 if (fieldname == NULL)
13558 fieldname = "";
13559
13560 /* The name is already allocated along with this objfile, so we don't
13561 need to duplicate it for the type. */
13562 fp->set_name (fieldname);
13563
13564 /* Change accessibility for artificial fields (e.g. virtual table
13565 pointer or virtual base class pointer) to private. */
13566 if (dwarf2_attr (die, DW_AT_artificial, cu))
13567 {
13568 FIELD_ARTIFICIAL (*fp) = 1;
13569 new_field->accessibility = DW_ACCESS_private;
13570 fip->non_public_fields = true;
13571 }
13572 }
13573 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13574 {
13575 /* C++ static member. */
13576
13577 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13578 is a declaration, but all versions of G++ as of this writing
13579 (so through at least 3.2.1) incorrectly generate
13580 DW_TAG_variable tags. */
13581
13582 const char *physname;
13583
13584 /* Get name of field. */
13585 fieldname = dwarf2_name (die, cu);
13586 if (fieldname == NULL)
13587 return;
13588
13589 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13590 if (attr
13591 /* Only create a symbol if this is an external value.
13592 new_symbol checks this and puts the value in the global symbol
13593 table, which we want. If it is not external, new_symbol
13594 will try to put the value in cu->list_in_scope which is wrong. */
13595 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13596 {
13597 /* A static const member, not much different than an enum as far as
13598 we're concerned, except that we can support more types. */
13599 new_symbol (die, NULL, cu);
13600 }
13601
13602 /* Get physical name. */
13603 physname = dwarf2_physname (fieldname, die, cu);
13604
13605 /* The name is already allocated along with this objfile, so we don't
13606 need to duplicate it for the type. */
13607 fp->set_loc_physname (physname ? physname : "");
13608 fp->set_type (die_type (die, cu));
13609 fp->set_name (fieldname);
13610 }
13611 else if (die->tag == DW_TAG_inheritance)
13612 {
13613 /* C++ base class field. */
13614 handle_member_location (die, cu, fp);
13615 FIELD_BITSIZE (*fp) = 0;
13616 fp->set_type (die_type (die, cu));
13617 fp->set_name (fp->type ()->name ());
13618 }
13619 else
13620 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13621 }
13622
13623 /* Can the type given by DIE define another type? */
13624
13625 static bool
13626 type_can_define_types (const struct die_info *die)
13627 {
13628 switch (die->tag)
13629 {
13630 case DW_TAG_typedef:
13631 case DW_TAG_class_type:
13632 case DW_TAG_structure_type:
13633 case DW_TAG_union_type:
13634 case DW_TAG_enumeration_type:
13635 return true;
13636
13637 default:
13638 return false;
13639 }
13640 }
13641
13642 /* Add a type definition defined in the scope of the FIP's class. */
13643
13644 static void
13645 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13646 struct dwarf2_cu *cu)
13647 {
13648 struct decl_field fp;
13649 memset (&fp, 0, sizeof (fp));
13650
13651 gdb_assert (type_can_define_types (die));
13652
13653 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13654 fp.name = dwarf2_name (die, cu);
13655 fp.type = read_type_die (die, cu);
13656
13657 /* Save accessibility. */
13658 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13659 switch (accessibility)
13660 {
13661 case DW_ACCESS_public:
13662 /* The assumed value if neither private nor protected. */
13663 break;
13664 case DW_ACCESS_private:
13665 fp.is_private = 1;
13666 break;
13667 case DW_ACCESS_protected:
13668 fp.is_protected = 1;
13669 break;
13670 }
13671
13672 if (die->tag == DW_TAG_typedef)
13673 fip->typedef_field_list.push_back (fp);
13674 else
13675 fip->nested_types_list.push_back (fp);
13676 }
13677
13678 /* A convenience typedef that's used when finding the discriminant
13679 field for a variant part. */
13680 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13681 offset_map_type;
13682
13683 /* Compute the discriminant range for a given variant. OBSTACK is
13684 where the results will be stored. VARIANT is the variant to
13685 process. IS_UNSIGNED indicates whether the discriminant is signed
13686 or unsigned. */
13687
13688 static const gdb::array_view<discriminant_range>
13689 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13690 bool is_unsigned)
13691 {
13692 std::vector<discriminant_range> ranges;
13693
13694 if (variant.default_branch)
13695 return {};
13696
13697 if (variant.discr_list_data == nullptr)
13698 {
13699 discriminant_range r
13700 = {variant.discriminant_value, variant.discriminant_value};
13701 ranges.push_back (r);
13702 }
13703 else
13704 {
13705 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13706 variant.discr_list_data->size);
13707 while (!data.empty ())
13708 {
13709 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13710 {
13711 complaint (_("invalid discriminant marker: %d"), data[0]);
13712 break;
13713 }
13714 bool is_range = data[0] == DW_DSC_range;
13715 data = data.slice (1);
13716
13717 ULONGEST low, high;
13718 unsigned int bytes_read;
13719
13720 if (data.empty ())
13721 {
13722 complaint (_("DW_AT_discr_list missing low value"));
13723 break;
13724 }
13725 if (is_unsigned)
13726 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13727 else
13728 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13729 &bytes_read);
13730 data = data.slice (bytes_read);
13731
13732 if (is_range)
13733 {
13734 if (data.empty ())
13735 {
13736 complaint (_("DW_AT_discr_list missing high value"));
13737 break;
13738 }
13739 if (is_unsigned)
13740 high = read_unsigned_leb128 (nullptr, data.data (),
13741 &bytes_read);
13742 else
13743 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13744 &bytes_read);
13745 data = data.slice (bytes_read);
13746 }
13747 else
13748 high = low;
13749
13750 ranges.push_back ({ low, high });
13751 }
13752 }
13753
13754 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13755 ranges.size ());
13756 std::copy (ranges.begin (), ranges.end (), result);
13757 return gdb::array_view<discriminant_range> (result, ranges.size ());
13758 }
13759
13760 static const gdb::array_view<variant_part> create_variant_parts
13761 (struct obstack *obstack,
13762 const offset_map_type &offset_map,
13763 struct field_info *fi,
13764 const std::vector<variant_part_builder> &variant_parts);
13765
13766 /* Fill in a "struct variant" for a given variant field. RESULT is
13767 the variant to fill in. OBSTACK is where any needed allocations
13768 will be done. OFFSET_MAP holds the mapping from section offsets to
13769 fields for the type. FI describes the fields of the type we're
13770 processing. FIELD is the variant field we're converting. */
13771
13772 static void
13773 create_one_variant (variant &result, struct obstack *obstack,
13774 const offset_map_type &offset_map,
13775 struct field_info *fi, const variant_field &field)
13776 {
13777 result.discriminants = convert_variant_range (obstack, field, false);
13778 result.first_field = field.first_field + fi->baseclasses.size ();
13779 result.last_field = field.last_field + fi->baseclasses.size ();
13780 result.parts = create_variant_parts (obstack, offset_map, fi,
13781 field.variant_parts);
13782 }
13783
13784 /* Fill in a "struct variant_part" for a given variant part. RESULT
13785 is the variant part to fill in. OBSTACK is where any needed
13786 allocations will be done. OFFSET_MAP holds the mapping from
13787 section offsets to fields for the type. FI describes the fields of
13788 the type we're processing. BUILDER is the variant part to be
13789 converted. */
13790
13791 static void
13792 create_one_variant_part (variant_part &result,
13793 struct obstack *obstack,
13794 const offset_map_type &offset_map,
13795 struct field_info *fi,
13796 const variant_part_builder &builder)
13797 {
13798 auto iter = offset_map.find (builder.discriminant_offset);
13799 if (iter == offset_map.end ())
13800 {
13801 result.discriminant_index = -1;
13802 /* Doesn't matter. */
13803 result.is_unsigned = false;
13804 }
13805 else
13806 {
13807 result.discriminant_index = iter->second;
13808 result.is_unsigned
13809 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13810 }
13811
13812 size_t n = builder.variants.size ();
13813 variant *output = new (obstack) variant[n];
13814 for (size_t i = 0; i < n; ++i)
13815 create_one_variant (output[i], obstack, offset_map, fi,
13816 builder.variants[i]);
13817
13818 result.variants = gdb::array_view<variant> (output, n);
13819 }
13820
13821 /* Create a vector of variant parts that can be attached to a type.
13822 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13823 holds the mapping from section offsets to fields for the type. FI
13824 describes the fields of the type we're processing. VARIANT_PARTS
13825 is the vector to convert. */
13826
13827 static const gdb::array_view<variant_part>
13828 create_variant_parts (struct obstack *obstack,
13829 const offset_map_type &offset_map,
13830 struct field_info *fi,
13831 const std::vector<variant_part_builder> &variant_parts)
13832 {
13833 if (variant_parts.empty ())
13834 return {};
13835
13836 size_t n = variant_parts.size ();
13837 variant_part *result = new (obstack) variant_part[n];
13838 for (size_t i = 0; i < n; ++i)
13839 create_one_variant_part (result[i], obstack, offset_map, fi,
13840 variant_parts[i]);
13841
13842 return gdb::array_view<variant_part> (result, n);
13843 }
13844
13845 /* Compute the variant part vector for FIP, attaching it to TYPE when
13846 done. */
13847
13848 static void
13849 add_variant_property (struct field_info *fip, struct type *type,
13850 struct dwarf2_cu *cu)
13851 {
13852 /* Map section offsets of fields to their field index. Note the
13853 field index here does not take the number of baseclasses into
13854 account. */
13855 offset_map_type offset_map;
13856 for (int i = 0; i < fip->fields.size (); ++i)
13857 offset_map[fip->fields[i].offset] = i;
13858
13859 struct objfile *objfile = cu->per_objfile->objfile;
13860 gdb::array_view<const variant_part> parts
13861 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13862 fip->variant_parts);
13863
13864 struct dynamic_prop prop;
13865 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13866 obstack_copy (&objfile->objfile_obstack, &parts,
13867 sizeof (parts)));
13868
13869 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
13870 }
13871
13872 /* Create the vector of fields, and attach it to the type. */
13873
13874 static void
13875 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13876 struct dwarf2_cu *cu)
13877 {
13878 int nfields = fip->nfields ();
13879
13880 /* Record the field count, allocate space for the array of fields,
13881 and create blank accessibility bitfields if necessary. */
13882 type->set_num_fields (nfields);
13883 type->set_fields
13884 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
13885
13886 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
13887 {
13888 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13889
13890 TYPE_FIELD_PRIVATE_BITS (type) =
13891 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13892 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13893
13894 TYPE_FIELD_PROTECTED_BITS (type) =
13895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13896 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13897
13898 TYPE_FIELD_IGNORE_BITS (type) =
13899 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13900 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13901 }
13902
13903 /* If the type has baseclasses, allocate and clear a bit vector for
13904 TYPE_FIELD_VIRTUAL_BITS. */
13905 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
13906 {
13907 int num_bytes = B_BYTES (fip->baseclasses.size ());
13908 unsigned char *pointer;
13909
13910 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13911 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13912 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13913 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
13914 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
13915 }
13916
13917 if (!fip->variant_parts.empty ())
13918 add_variant_property (fip, type, cu);
13919
13920 /* Copy the saved-up fields into the field vector. */
13921 for (int i = 0; i < nfields; ++i)
13922 {
13923 struct nextfield &field
13924 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
13925 : fip->fields[i - fip->baseclasses.size ()]);
13926
13927 type->field (i) = field.field;
13928 switch (field.accessibility)
13929 {
13930 case DW_ACCESS_private:
13931 if (cu->per_cu->lang != language_ada)
13932 SET_TYPE_FIELD_PRIVATE (type, i);
13933 break;
13934
13935 case DW_ACCESS_protected:
13936 if (cu->per_cu->lang != language_ada)
13937 SET_TYPE_FIELD_PROTECTED (type, i);
13938 break;
13939
13940 case DW_ACCESS_public:
13941 break;
13942
13943 default:
13944 /* Unknown accessibility. Complain and treat it as public. */
13945 {
13946 complaint (_("unsupported accessibility %d"),
13947 field.accessibility);
13948 }
13949 break;
13950 }
13951 if (i < fip->baseclasses.size ())
13952 {
13953 switch (field.virtuality)
13954 {
13955 case DW_VIRTUALITY_virtual:
13956 case DW_VIRTUALITY_pure_virtual:
13957 if (cu->per_cu->lang == language_ada)
13958 error (_("unexpected virtuality in component of Ada type"));
13959 SET_TYPE_FIELD_VIRTUAL (type, i);
13960 break;
13961 }
13962 }
13963 }
13964 }
13965
13966 /* Return true if this member function is a constructor, false
13967 otherwise. */
13968
13969 static int
13970 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13971 {
13972 const char *fieldname;
13973 const char *type_name;
13974 int len;
13975
13976 if (die->parent == NULL)
13977 return 0;
13978
13979 if (die->parent->tag != DW_TAG_structure_type
13980 && die->parent->tag != DW_TAG_union_type
13981 && die->parent->tag != DW_TAG_class_type)
13982 return 0;
13983
13984 fieldname = dwarf2_name (die, cu);
13985 type_name = dwarf2_name (die->parent, cu);
13986 if (fieldname == NULL || type_name == NULL)
13987 return 0;
13988
13989 len = strlen (fieldname);
13990 return (strncmp (fieldname, type_name, len) == 0
13991 && (type_name[len] == '\0' || type_name[len] == '<'));
13992 }
13993
13994 /* Add a member function to the proper fieldlist. */
13995
13996 static void
13997 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13998 struct type *type, struct dwarf2_cu *cu)
13999 {
14000 struct objfile *objfile = cu->per_objfile->objfile;
14001 struct attribute *attr;
14002 int i;
14003 struct fnfieldlist *flp = nullptr;
14004 struct fn_field *fnp;
14005 const char *fieldname;
14006 struct type *this_type;
14007
14008 if (cu->per_cu->lang == language_ada)
14009 error (_("unexpected member function in Ada type"));
14010
14011 /* Get name of member function. */
14012 fieldname = dwarf2_name (die, cu);
14013 if (fieldname == NULL)
14014 return;
14015
14016 /* Look up member function name in fieldlist. */
14017 for (i = 0; i < fip->fnfieldlists.size (); i++)
14018 {
14019 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14020 {
14021 flp = &fip->fnfieldlists[i];
14022 break;
14023 }
14024 }
14025
14026 /* Create a new fnfieldlist if necessary. */
14027 if (flp == nullptr)
14028 {
14029 fip->fnfieldlists.emplace_back ();
14030 flp = &fip->fnfieldlists.back ();
14031 flp->name = fieldname;
14032 i = fip->fnfieldlists.size () - 1;
14033 }
14034
14035 /* Create a new member function field and add it to the vector of
14036 fnfieldlists. */
14037 flp->fnfields.emplace_back ();
14038 fnp = &flp->fnfields.back ();
14039
14040 /* Delay processing of the physname until later. */
14041 if (cu->per_cu->lang == language_cplus)
14042 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14043 die, cu);
14044 else
14045 {
14046 const char *physname = dwarf2_physname (fieldname, die, cu);
14047 fnp->physname = physname ? physname : "";
14048 }
14049
14050 fnp->type = alloc_type (objfile);
14051 this_type = read_type_die (die, cu);
14052 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14053 {
14054 int nparams = this_type->num_fields ();
14055
14056 /* TYPE is the domain of this method, and THIS_TYPE is the type
14057 of the method itself (TYPE_CODE_METHOD). */
14058 smash_to_method_type (fnp->type, type,
14059 TYPE_TARGET_TYPE (this_type),
14060 this_type->fields (),
14061 this_type->num_fields (),
14062 this_type->has_varargs ());
14063
14064 /* Handle static member functions.
14065 Dwarf2 has no clean way to discern C++ static and non-static
14066 member functions. G++ helps GDB by marking the first
14067 parameter for non-static member functions (which is the this
14068 pointer) as artificial. We obtain this information from
14069 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14070 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14071 fnp->voffset = VOFFSET_STATIC;
14072 }
14073 else
14074 complaint (_("member function type missing for '%s'"),
14075 dwarf2_full_name (fieldname, die, cu));
14076
14077 /* Get fcontext from DW_AT_containing_type if present. */
14078 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14079 fnp->fcontext = die_containing_type (die, cu);
14080
14081 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14082 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14083
14084 /* Get accessibility. */
14085 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14086 switch (accessibility)
14087 {
14088 case DW_ACCESS_private:
14089 fnp->is_private = 1;
14090 break;
14091 case DW_ACCESS_protected:
14092 fnp->is_protected = 1;
14093 break;
14094 }
14095
14096 /* Check for artificial methods. */
14097 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14098 if (attr && attr->as_boolean ())
14099 fnp->is_artificial = 1;
14100
14101 /* Check for defaulted methods. */
14102 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14103 if (attr != nullptr)
14104 fnp->defaulted = attr->defaulted ();
14105
14106 /* Check for deleted methods. */
14107 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14108 if (attr != nullptr && attr->as_boolean ())
14109 fnp->is_deleted = 1;
14110
14111 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14112
14113 /* Get index in virtual function table if it is a virtual member
14114 function. For older versions of GCC, this is an offset in the
14115 appropriate virtual table, as specified by DW_AT_containing_type.
14116 For everyone else, it is an expression to be evaluated relative
14117 to the object address. */
14118
14119 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14120 if (attr != nullptr)
14121 {
14122 if (attr->form_is_block () && attr->as_block ()->size > 0)
14123 {
14124 struct dwarf_block *block = attr->as_block ();
14125
14126 if (block->data[0] == DW_OP_constu)
14127 {
14128 /* Old-style GCC. */
14129 fnp->voffset = decode_locdesc (block, cu) + 2;
14130 }
14131 else if (block->data[0] == DW_OP_deref
14132 || (block->size > 1
14133 && block->data[0] == DW_OP_deref_size
14134 && block->data[1] == cu->header.addr_size))
14135 {
14136 fnp->voffset = decode_locdesc (block, cu);
14137 if ((fnp->voffset % cu->header.addr_size) != 0)
14138 dwarf2_complex_location_expr_complaint ();
14139 else
14140 fnp->voffset /= cu->header.addr_size;
14141 fnp->voffset += 2;
14142 }
14143 else
14144 dwarf2_complex_location_expr_complaint ();
14145
14146 if (!fnp->fcontext)
14147 {
14148 /* If there is no `this' field and no DW_AT_containing_type,
14149 we cannot actually find a base class context for the
14150 vtable! */
14151 if (this_type->num_fields () == 0
14152 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14153 {
14154 complaint (_("cannot determine context for virtual member "
14155 "function \"%s\" (offset %s)"),
14156 fieldname, sect_offset_str (die->sect_off));
14157 }
14158 else
14159 {
14160 fnp->fcontext
14161 = TYPE_TARGET_TYPE (this_type->field (0).type ());
14162 }
14163 }
14164 }
14165 else if (attr->form_is_section_offset ())
14166 {
14167 dwarf2_complex_location_expr_complaint ();
14168 }
14169 else
14170 {
14171 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14172 fieldname);
14173 }
14174 }
14175 else
14176 {
14177 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14178 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14179 {
14180 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14181 complaint (_("Member function \"%s\" (offset %s) is virtual "
14182 "but the vtable offset is not specified"),
14183 fieldname, sect_offset_str (die->sect_off));
14184 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14185 TYPE_CPLUS_DYNAMIC (type) = 1;
14186 }
14187 }
14188 }
14189
14190 /* Create the vector of member function fields, and attach it to the type. */
14191
14192 static void
14193 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14194 struct dwarf2_cu *cu)
14195 {
14196 if (cu->per_cu->lang == language_ada)
14197 error (_("unexpected member functions in Ada type"));
14198
14199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14200 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14201 TYPE_ALLOC (type,
14202 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14203
14204 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14205 {
14206 struct fnfieldlist &nf = fip->fnfieldlists[i];
14207 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14208
14209 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14210 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14211 fn_flp->fn_fields = (struct fn_field *)
14212 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14213
14214 for (int k = 0; k < nf.fnfields.size (); ++k)
14215 fn_flp->fn_fields[k] = nf.fnfields[k];
14216 }
14217
14218 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14219 }
14220
14221 /* Returns non-zero if NAME is the name of a vtable member in CU's
14222 language, zero otherwise. */
14223 static int
14224 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14225 {
14226 static const char vptr[] = "_vptr";
14227
14228 /* Look for the C++ form of the vtable. */
14229 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14230 return 1;
14231
14232 return 0;
14233 }
14234
14235 /* GCC outputs unnamed structures that are really pointers to member
14236 functions, with the ABI-specified layout. If TYPE describes
14237 such a structure, smash it into a member function type.
14238
14239 GCC shouldn't do this; it should just output pointer to member DIEs.
14240 This is GCC PR debug/28767. */
14241
14242 static void
14243 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14244 {
14245 struct type *pfn_type, *self_type, *new_type;
14246
14247 /* Check for a structure with no name and two children. */
14248 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14249 return;
14250
14251 /* Check for __pfn and __delta members. */
14252 if (type->field (0).name () == NULL
14253 || strcmp (type->field (0).name (), "__pfn") != 0
14254 || type->field (1).name () == NULL
14255 || strcmp (type->field (1).name (), "__delta") != 0)
14256 return;
14257
14258 /* Find the type of the method. */
14259 pfn_type = type->field (0).type ();
14260 if (pfn_type == NULL
14261 || pfn_type->code () != TYPE_CODE_PTR
14262 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
14263 return;
14264
14265 /* Look for the "this" argument. */
14266 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14267 if (pfn_type->num_fields () == 0
14268 /* || pfn_type->field (0).type () == NULL */
14269 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14270 return;
14271
14272 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
14273 new_type = alloc_type (objfile);
14274 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14275 pfn_type->fields (), pfn_type->num_fields (),
14276 pfn_type->has_varargs ());
14277 smash_to_methodptr_type (type, new_type);
14278 }
14279
14280 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14281 requires rewriting, then copy it and return the updated copy.
14282 Otherwise return nullptr. */
14283
14284 static struct type *
14285 rewrite_array_type (struct type *type)
14286 {
14287 if (type->code () != TYPE_CODE_ARRAY)
14288 return nullptr;
14289
14290 struct type *index_type = type->index_type ();
14291 range_bounds *current_bounds = index_type->bounds ();
14292
14293 /* Handle multi-dimensional arrays. */
14294 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
14295 if (new_target == nullptr)
14296 {
14297 /* Maybe we don't need to rewrite this array. */
14298 if (current_bounds->low.kind () == PROP_CONST
14299 && current_bounds->high.kind () == PROP_CONST)
14300 return nullptr;
14301 }
14302
14303 /* Either the target type was rewritten, or the bounds have to be
14304 updated. Either way we want to copy the type and update
14305 everything. */
14306 struct type *copy = copy_type (type);
14307 int nfields = copy->num_fields ();
14308 field *new_fields
14309 = ((struct field *) TYPE_ZALLOC (copy,
14310 nfields * sizeof (struct field)));
14311 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14312 copy->set_fields (new_fields);
14313 if (new_target != nullptr)
14314 TYPE_TARGET_TYPE (copy) = new_target;
14315
14316 struct type *index_copy = copy_type (index_type);
14317 range_bounds *bounds
14318 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14319 sizeof (range_bounds));
14320 *bounds = *current_bounds;
14321 bounds->low.set_const_val (1);
14322 bounds->high.set_const_val (0);
14323 index_copy->set_bounds (bounds);
14324 copy->set_index_type (index_copy);
14325
14326 return copy;
14327 }
14328
14329 /* While some versions of GCC will generate complicated DWARF for an
14330 array (see quirk_ada_thick_pointer), more recent versions were
14331 modified to emit an explicit thick pointer structure. However, in
14332 this case, the array still has DWARF expressions for its ranges,
14333 and these must be ignored. */
14334
14335 static void
14336 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14337 struct type *type)
14338 {
14339 gdb_assert (cu->per_cu->lang == language_ada);
14340
14341 /* Check for a structure with two children. */
14342 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14343 return;
14344
14345 /* Check for P_ARRAY and P_BOUNDS members. */
14346 if (type->field (0).name () == NULL
14347 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14348 || type->field (1).name () == NULL
14349 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14350 return;
14351
14352 /* Make sure we're looking at a pointer to an array. */
14353 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14354 return;
14355
14356 /* The Ada code already knows how to handle these types, so all that
14357 we need to do is turn the bounds into static bounds. However, we
14358 don't want to rewrite existing array or index types in-place,
14359 because those may be referenced in other contexts where this
14360 rewriting is undesirable. */
14361 struct type *new_ary_type
14362 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
14363 if (new_ary_type != nullptr)
14364 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14365 }
14366
14367 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14368 appropriate error checking and issuing complaints if there is a
14369 problem. */
14370
14371 static ULONGEST
14372 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14373 {
14374 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14375
14376 if (attr == nullptr)
14377 return 0;
14378
14379 if (!attr->form_is_constant ())
14380 {
14381 complaint (_("DW_AT_alignment must have constant form"
14382 " - DIE at %s [in module %s]"),
14383 sect_offset_str (die->sect_off),
14384 objfile_name (cu->per_objfile->objfile));
14385 return 0;
14386 }
14387
14388 LONGEST val = attr->constant_value (0);
14389 if (val < 0)
14390 {
14391 complaint (_("DW_AT_alignment value must not be negative"
14392 " - DIE at %s [in module %s]"),
14393 sect_offset_str (die->sect_off),
14394 objfile_name (cu->per_objfile->objfile));
14395 return 0;
14396 }
14397 ULONGEST align = val;
14398
14399 if (align == 0)
14400 {
14401 complaint (_("DW_AT_alignment value must not be zero"
14402 " - DIE at %s [in module %s]"),
14403 sect_offset_str (die->sect_off),
14404 objfile_name (cu->per_objfile->objfile));
14405 return 0;
14406 }
14407 if ((align & (align - 1)) != 0)
14408 {
14409 complaint (_("DW_AT_alignment value must be a power of 2"
14410 " - DIE at %s [in module %s]"),
14411 sect_offset_str (die->sect_off),
14412 objfile_name (cu->per_objfile->objfile));
14413 return 0;
14414 }
14415
14416 return align;
14417 }
14418
14419 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14420 the alignment for TYPE. */
14421
14422 static void
14423 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14424 struct type *type)
14425 {
14426 if (!set_type_align (type, get_alignment (cu, die)))
14427 complaint (_("DW_AT_alignment value too large"
14428 " - DIE at %s [in module %s]"),
14429 sect_offset_str (die->sect_off),
14430 objfile_name (cu->per_objfile->objfile));
14431 }
14432
14433 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14434 constant for a type, according to DWARF5 spec, Table 5.5. */
14435
14436 static bool
14437 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14438 {
14439 switch (value)
14440 {
14441 case DW_CC_normal:
14442 case DW_CC_pass_by_reference:
14443 case DW_CC_pass_by_value:
14444 return true;
14445
14446 default:
14447 complaint (_("unrecognized DW_AT_calling_convention value "
14448 "(%s) for a type"), pulongest (value));
14449 return false;
14450 }
14451 }
14452
14453 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14454 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14455 also according to GNU-specific values (see include/dwarf2.h). */
14456
14457 static bool
14458 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14459 {
14460 switch (value)
14461 {
14462 case DW_CC_normal:
14463 case DW_CC_program:
14464 case DW_CC_nocall:
14465 return true;
14466
14467 case DW_CC_GNU_renesas_sh:
14468 case DW_CC_GNU_borland_fastcall_i386:
14469 case DW_CC_GDB_IBM_OpenCL:
14470 return true;
14471
14472 default:
14473 complaint (_("unrecognized DW_AT_calling_convention value "
14474 "(%s) for a subroutine"), pulongest (value));
14475 return false;
14476 }
14477 }
14478
14479 /* Called when we find the DIE that starts a structure or union scope
14480 (definition) to create a type for the structure or union. Fill in
14481 the type's name and general properties; the members will not be
14482 processed until process_structure_scope. A symbol table entry for
14483 the type will also not be done until process_structure_scope (assuming
14484 the type has a name).
14485
14486 NOTE: we need to call these functions regardless of whether or not the
14487 DIE has a DW_AT_name attribute, since it might be an anonymous
14488 structure or union. This gets the type entered into our set of
14489 user defined types. */
14490
14491 static struct type *
14492 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14493 {
14494 struct objfile *objfile = cu->per_objfile->objfile;
14495 struct type *type;
14496 struct attribute *attr;
14497 const char *name;
14498
14499 /* If the definition of this type lives in .debug_types, read that type.
14500 Don't follow DW_AT_specification though, that will take us back up
14501 the chain and we want to go down. */
14502 attr = die->attr (DW_AT_signature);
14503 if (attr != nullptr)
14504 {
14505 type = get_DW_AT_signature_type (die, attr, cu);
14506
14507 /* The type's CU may not be the same as CU.
14508 Ensure TYPE is recorded with CU in die_type_hash. */
14509 return set_die_type (die, type, cu);
14510 }
14511
14512 type = alloc_type (objfile);
14513 INIT_CPLUS_SPECIFIC (type);
14514
14515 name = dwarf2_name (die, cu);
14516 if (name != NULL)
14517 {
14518 if (cu->per_cu->lang == language_cplus
14519 || cu->per_cu->lang == language_d
14520 || cu->per_cu->lang == language_rust)
14521 {
14522 const char *full_name = dwarf2_full_name (name, die, cu);
14523
14524 /* dwarf2_full_name might have already finished building the DIE's
14525 type. If so, there is no need to continue. */
14526 if (get_die_type (die, cu) != NULL)
14527 return get_die_type (die, cu);
14528
14529 type->set_name (full_name);
14530 }
14531 else
14532 {
14533 /* The name is already allocated along with this objfile, so
14534 we don't need to duplicate it for the type. */
14535 type->set_name (name);
14536 }
14537 }
14538
14539 if (die->tag == DW_TAG_structure_type)
14540 {
14541 type->set_code (TYPE_CODE_STRUCT);
14542 }
14543 else if (die->tag == DW_TAG_union_type)
14544 {
14545 type->set_code (TYPE_CODE_UNION);
14546 }
14547 else if (die->tag == DW_TAG_namelist)
14548 {
14549 type->set_code (TYPE_CODE_NAMELIST);
14550 }
14551 else
14552 {
14553 type->set_code (TYPE_CODE_STRUCT);
14554 }
14555
14556 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
14557 type->set_is_declared_class (true);
14558
14559 /* Store the calling convention in the type if it's available in
14560 the die. Otherwise the calling convention remains set to
14561 the default value DW_CC_normal. */
14562 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14563 if (attr != nullptr
14564 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14565 {
14566 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14567 TYPE_CPLUS_CALLING_CONVENTION (type)
14568 = (enum dwarf_calling_convention) (attr->constant_value (0));
14569 }
14570
14571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14572 if (attr != nullptr)
14573 {
14574 if (attr->form_is_constant ())
14575 TYPE_LENGTH (type) = attr->constant_value (0);
14576 else
14577 {
14578 struct dynamic_prop prop;
14579 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14580 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14581 TYPE_LENGTH (type) = 0;
14582 }
14583 }
14584 else
14585 {
14586 TYPE_LENGTH (type) = 0;
14587 }
14588
14589 maybe_set_alignment (cu, die, type);
14590
14591 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14592 {
14593 /* ICC<14 does not output the required DW_AT_declaration on
14594 incomplete types, but gives them a size of zero. */
14595 type->set_is_stub (true);
14596 }
14597 else
14598 type->set_stub_is_supported (true);
14599
14600 if (die_is_declaration (die, cu))
14601 type->set_is_stub (true);
14602 else if (attr == NULL && die->child == NULL
14603 && producer_is_realview (cu->producer))
14604 /* RealView does not output the required DW_AT_declaration
14605 on incomplete types. */
14606 type->set_is_stub (true);
14607
14608 /* We need to add the type field to the die immediately so we don't
14609 infinitely recurse when dealing with pointers to the structure
14610 type within the structure itself. */
14611 set_die_type (die, type, cu);
14612
14613 /* set_die_type should be already done. */
14614 set_descriptive_type (type, die, cu);
14615
14616 return type;
14617 }
14618
14619 static void handle_struct_member_die
14620 (struct die_info *child_die,
14621 struct type *type,
14622 struct field_info *fi,
14623 std::vector<struct symbol *> *template_args,
14624 struct dwarf2_cu *cu);
14625
14626 /* A helper for handle_struct_member_die that handles
14627 DW_TAG_variant_part. */
14628
14629 static void
14630 handle_variant_part (struct die_info *die, struct type *type,
14631 struct field_info *fi,
14632 std::vector<struct symbol *> *template_args,
14633 struct dwarf2_cu *cu)
14634 {
14635 variant_part_builder *new_part;
14636 if (fi->current_variant_part == nullptr)
14637 {
14638 fi->variant_parts.emplace_back ();
14639 new_part = &fi->variant_parts.back ();
14640 }
14641 else if (!fi->current_variant_part->processing_variant)
14642 {
14643 complaint (_("nested DW_TAG_variant_part seen "
14644 "- DIE at %s [in module %s]"),
14645 sect_offset_str (die->sect_off),
14646 objfile_name (cu->per_objfile->objfile));
14647 return;
14648 }
14649 else
14650 {
14651 variant_field &current = fi->current_variant_part->variants.back ();
14652 current.variant_parts.emplace_back ();
14653 new_part = &current.variant_parts.back ();
14654 }
14655
14656 /* When we recurse, we want callees to add to this new variant
14657 part. */
14658 scoped_restore save_current_variant_part
14659 = make_scoped_restore (&fi->current_variant_part, new_part);
14660
14661 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14662 if (discr == NULL)
14663 {
14664 /* It's a univariant form, an extension we support. */
14665 }
14666 else if (discr->form_is_ref ())
14667 {
14668 struct dwarf2_cu *target_cu = cu;
14669 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14670
14671 new_part->discriminant_offset = target_die->sect_off;
14672 }
14673 else
14674 {
14675 complaint (_("DW_AT_discr does not have DIE reference form"
14676 " - DIE at %s [in module %s]"),
14677 sect_offset_str (die->sect_off),
14678 objfile_name (cu->per_objfile->objfile));
14679 }
14680
14681 for (die_info *child_die = die->child;
14682 child_die != NULL;
14683 child_die = child_die->sibling)
14684 handle_struct_member_die (child_die, type, fi, template_args, cu);
14685 }
14686
14687 /* A helper for handle_struct_member_die that handles
14688 DW_TAG_variant. */
14689
14690 static void
14691 handle_variant (struct die_info *die, struct type *type,
14692 struct field_info *fi,
14693 std::vector<struct symbol *> *template_args,
14694 struct dwarf2_cu *cu)
14695 {
14696 if (fi->current_variant_part == nullptr)
14697 {
14698 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14699 "- DIE at %s [in module %s]"),
14700 sect_offset_str (die->sect_off),
14701 objfile_name (cu->per_objfile->objfile));
14702 return;
14703 }
14704 if (fi->current_variant_part->processing_variant)
14705 {
14706 complaint (_("nested DW_TAG_variant seen "
14707 "- DIE at %s [in module %s]"),
14708 sect_offset_str (die->sect_off),
14709 objfile_name (cu->per_objfile->objfile));
14710 return;
14711 }
14712
14713 scoped_restore save_processing_variant
14714 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14715 true);
14716
14717 fi->current_variant_part->variants.emplace_back ();
14718 variant_field &variant = fi->current_variant_part->variants.back ();
14719 variant.first_field = fi->fields.size ();
14720
14721 /* In a variant we want to get the discriminant and also add a
14722 field for our sole member child. */
14723 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14724 if (discr == nullptr || !discr->form_is_constant ())
14725 {
14726 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14727 if (discr == nullptr || discr->as_block ()->size == 0)
14728 variant.default_branch = true;
14729 else
14730 variant.discr_list_data = discr->as_block ();
14731 }
14732 else
14733 variant.discriminant_value = discr->constant_value (0);
14734
14735 for (die_info *variant_child = die->child;
14736 variant_child != NULL;
14737 variant_child = variant_child->sibling)
14738 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14739
14740 variant.last_field = fi->fields.size ();
14741 }
14742
14743 /* A helper for process_structure_scope that handles a single member
14744 DIE. */
14745
14746 static void
14747 handle_struct_member_die (struct die_info *child_die, struct type *type,
14748 struct field_info *fi,
14749 std::vector<struct symbol *> *template_args,
14750 struct dwarf2_cu *cu)
14751 {
14752 if (child_die->tag == DW_TAG_member
14753 || child_die->tag == DW_TAG_variable
14754 || child_die->tag == DW_TAG_namelist_item)
14755 {
14756 /* NOTE: carlton/2002-11-05: A C++ static data member
14757 should be a DW_TAG_member that is a declaration, but
14758 all versions of G++ as of this writing (so through at
14759 least 3.2.1) incorrectly generate DW_TAG_variable
14760 tags for them instead. */
14761 dwarf2_add_field (fi, child_die, cu);
14762 }
14763 else if (child_die->tag == DW_TAG_subprogram)
14764 {
14765 /* Rust doesn't have member functions in the C++ sense.
14766 However, it does emit ordinary functions as children
14767 of a struct DIE. */
14768 if (cu->per_cu->lang == language_rust)
14769 read_func_scope (child_die, cu);
14770 else
14771 {
14772 /* C++ member function. */
14773 dwarf2_add_member_fn (fi, child_die, type, cu);
14774 }
14775 }
14776 else if (child_die->tag == DW_TAG_inheritance)
14777 {
14778 /* C++ base class field. */
14779 dwarf2_add_field (fi, child_die, cu);
14780 }
14781 else if (type_can_define_types (child_die))
14782 dwarf2_add_type_defn (fi, child_die, cu);
14783 else if (child_die->tag == DW_TAG_template_type_param
14784 || child_die->tag == DW_TAG_template_value_param)
14785 {
14786 struct symbol *arg = new_symbol (child_die, NULL, cu);
14787
14788 if (arg != NULL)
14789 template_args->push_back (arg);
14790 }
14791 else if (child_die->tag == DW_TAG_variant_part)
14792 handle_variant_part (child_die, type, fi, template_args, cu);
14793 else if (child_die->tag == DW_TAG_variant)
14794 handle_variant (child_die, type, fi, template_args, cu);
14795 }
14796
14797 /* Finish creating a structure or union type, including filling in its
14798 members and creating a symbol for it. This function also handles Fortran
14799 namelist variables, their items or members and creating a symbol for
14800 them. */
14801
14802 static void
14803 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14804 {
14805 struct objfile *objfile = cu->per_objfile->objfile;
14806 struct die_info *child_die;
14807 struct type *type;
14808
14809 type = get_die_type (die, cu);
14810 if (type == NULL)
14811 type = read_structure_type (die, cu);
14812
14813 bool has_template_parameters = false;
14814 if (die->child != NULL && ! die_is_declaration (die, cu))
14815 {
14816 struct field_info fi;
14817 std::vector<struct symbol *> template_args;
14818
14819 child_die = die->child;
14820
14821 while (child_die && child_die->tag)
14822 {
14823 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14824 child_die = child_die->sibling;
14825 }
14826
14827 /* Attach template arguments to type. */
14828 if (!template_args.empty ())
14829 {
14830 has_template_parameters = true;
14831 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14832 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14833 TYPE_TEMPLATE_ARGUMENTS (type)
14834 = XOBNEWVEC (&objfile->objfile_obstack,
14835 struct symbol *,
14836 TYPE_N_TEMPLATE_ARGUMENTS (type));
14837 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14838 template_args.data (),
14839 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14840 * sizeof (struct symbol *)));
14841 }
14842
14843 /* Attach fields and member functions to the type. */
14844 if (fi.nfields () > 0)
14845 dwarf2_attach_fields_to_type (&fi, type, cu);
14846 if (!fi.fnfieldlists.empty ())
14847 {
14848 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14849
14850 /* Get the type which refers to the base class (possibly this
14851 class itself) which contains the vtable pointer for the current
14852 class from the DW_AT_containing_type attribute. This use of
14853 DW_AT_containing_type is a GNU extension. */
14854
14855 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14856 {
14857 struct type *t = die_containing_type (die, cu);
14858
14859 set_type_vptr_basetype (type, t);
14860 if (type == t)
14861 {
14862 int i;
14863
14864 /* Our own class provides vtbl ptr. */
14865 for (i = t->num_fields () - 1;
14866 i >= TYPE_N_BASECLASSES (t);
14867 --i)
14868 {
14869 const char *fieldname = t->field (i).name ();
14870
14871 if (is_vtable_name (fieldname, cu))
14872 {
14873 set_type_vptr_fieldno (type, i);
14874 break;
14875 }
14876 }
14877
14878 /* Complain if virtual function table field not found. */
14879 if (i < TYPE_N_BASECLASSES (t))
14880 complaint (_("virtual function table pointer "
14881 "not found when defining class '%s'"),
14882 type->name () ? type->name () : "");
14883 }
14884 else
14885 {
14886 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14887 }
14888 }
14889 else if (cu->producer
14890 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14891 {
14892 /* The IBM XLC compiler does not provide direct indication
14893 of the containing type, but the vtable pointer is
14894 always named __vfp. */
14895
14896 int i;
14897
14898 for (i = type->num_fields () - 1;
14899 i >= TYPE_N_BASECLASSES (type);
14900 --i)
14901 {
14902 if (strcmp (type->field (i).name (), "__vfp") == 0)
14903 {
14904 set_type_vptr_fieldno (type, i);
14905 set_type_vptr_basetype (type, type);
14906 break;
14907 }
14908 }
14909 }
14910 }
14911
14912 /* Copy fi.typedef_field_list linked list elements content into the
14913 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14914 if (!fi.typedef_field_list.empty ())
14915 {
14916 int count = fi.typedef_field_list.size ();
14917
14918 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14919 TYPE_TYPEDEF_FIELD_ARRAY (type)
14920 = ((struct decl_field *)
14921 TYPE_ALLOC (type,
14922 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
14923 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
14924
14925 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
14926 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
14927 }
14928
14929 /* Copy fi.nested_types_list linked list elements content into the
14930 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14931 if (!fi.nested_types_list.empty ()
14932 && cu->per_cu->lang != language_ada)
14933 {
14934 int count = fi.nested_types_list.size ();
14935
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937 TYPE_NESTED_TYPES_ARRAY (type)
14938 = ((struct decl_field *)
14939 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
14940 TYPE_NESTED_TYPES_COUNT (type) = count;
14941
14942 for (int i = 0; i < fi.nested_types_list.size (); ++i)
14943 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
14944 }
14945 }
14946
14947 quirk_gcc_member_function_pointer (type, objfile);
14948 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
14949 cu->rust_unions.push_back (type);
14950 else if (cu->per_cu->lang == language_ada)
14951 quirk_ada_thick_pointer_struct (die, cu, type);
14952
14953 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14954 snapshots) has been known to create a die giving a declaration
14955 for a class that has, as a child, a die giving a definition for a
14956 nested class. So we have to process our children even if the
14957 current die is a declaration. Normally, of course, a declaration
14958 won't have any children at all. */
14959
14960 child_die = die->child;
14961
14962 while (child_die != NULL && child_die->tag)
14963 {
14964 if (child_die->tag == DW_TAG_member
14965 || child_die->tag == DW_TAG_variable
14966 || child_die->tag == DW_TAG_inheritance
14967 || child_die->tag == DW_TAG_template_value_param
14968 || child_die->tag == DW_TAG_template_type_param)
14969 {
14970 /* Do nothing. */
14971 }
14972 else
14973 process_die (child_die, cu);
14974
14975 child_die = child_die->sibling;
14976 }
14977
14978 /* Do not consider external references. According to the DWARF standard,
14979 these DIEs are identified by the fact that they have no byte_size
14980 attribute, and a declaration attribute. */
14981 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14982 || !die_is_declaration (die, cu)
14983 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
14984 {
14985 struct symbol *sym = new_symbol (die, type, cu);
14986
14987 if (has_template_parameters)
14988 {
14989 struct symtab *symtab;
14990 if (sym != nullptr)
14991 symtab = sym->symtab ();
14992 else if (cu->line_header != nullptr)
14993 {
14994 /* Any related symtab will do. */
14995 symtab
14996 = cu->line_header->file_names ()[0].symtab;
14997 }
14998 else
14999 {
15000 symtab = nullptr;
15001 complaint (_("could not find suitable "
15002 "symtab for template parameter"
15003 " - DIE at %s [in module %s]"),
15004 sect_offset_str (die->sect_off),
15005 objfile_name (objfile));
15006 }
15007
15008 if (symtab != nullptr)
15009 {
15010 /* Make sure that the symtab is set on the new symbols.
15011 Even though they don't appear in this symtab directly,
15012 other parts of gdb assume that symbols do, and this is
15013 reasonably true. */
15014 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15015 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15016 }
15017 }
15018 }
15019 }
15020
15021 /* Assuming DIE is an enumeration type, and TYPE is its associated
15022 type, update TYPE using some information only available in DIE's
15023 children. In particular, the fields are computed. */
15024
15025 static void
15026 update_enumeration_type_from_children (struct die_info *die,
15027 struct type *type,
15028 struct dwarf2_cu *cu)
15029 {
15030 struct die_info *child_die;
15031 int unsigned_enum = 1;
15032 int flag_enum = 1;
15033
15034 auto_obstack obstack;
15035 std::vector<struct field> fields;
15036
15037 for (child_die = die->child;
15038 child_die != NULL && child_die->tag;
15039 child_die = child_die->sibling)
15040 {
15041 struct attribute *attr;
15042 LONGEST value;
15043 const gdb_byte *bytes;
15044 struct dwarf2_locexpr_baton *baton;
15045 const char *name;
15046
15047 if (child_die->tag != DW_TAG_enumerator)
15048 continue;
15049
15050 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15051 if (attr == NULL)
15052 continue;
15053
15054 name = dwarf2_name (child_die, cu);
15055 if (name == NULL)
15056 name = "<anonymous enumerator>";
15057
15058 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15059 &value, &bytes, &baton);
15060 if (value < 0)
15061 {
15062 unsigned_enum = 0;
15063 flag_enum = 0;
15064 }
15065 else
15066 {
15067 if (count_one_bits_ll (value) >= 2)
15068 flag_enum = 0;
15069 }
15070
15071 fields.emplace_back ();
15072 struct field &field = fields.back ();
15073 field.set_name (dwarf2_physname (name, child_die, cu));
15074 field.set_loc_enumval (value);
15075 }
15076
15077 if (!fields.empty ())
15078 {
15079 type->set_num_fields (fields.size ());
15080 type->set_fields
15081 ((struct field *)
15082 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15083 memcpy (type->fields (), fields.data (),
15084 sizeof (struct field) * fields.size ());
15085 }
15086
15087 if (unsigned_enum)
15088 type->set_is_unsigned (true);
15089
15090 if (flag_enum)
15091 type->set_is_flag_enum (true);
15092 }
15093
15094 /* Given a DW_AT_enumeration_type die, set its type. We do not
15095 complete the type's fields yet, or create any symbols. */
15096
15097 static struct type *
15098 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15099 {
15100 struct objfile *objfile = cu->per_objfile->objfile;
15101 struct type *type;
15102 struct attribute *attr;
15103 const char *name;
15104
15105 /* If the definition of this type lives in .debug_types, read that type.
15106 Don't follow DW_AT_specification though, that will take us back up
15107 the chain and we want to go down. */
15108 attr = die->attr (DW_AT_signature);
15109 if (attr != nullptr)
15110 {
15111 type = get_DW_AT_signature_type (die, attr, cu);
15112
15113 /* The type's CU may not be the same as CU.
15114 Ensure TYPE is recorded with CU in die_type_hash. */
15115 return set_die_type (die, type, cu);
15116 }
15117
15118 type = alloc_type (objfile);
15119
15120 type->set_code (TYPE_CODE_ENUM);
15121 name = dwarf2_full_name (NULL, die, cu);
15122 if (name != NULL)
15123 type->set_name (name);
15124
15125 attr = dwarf2_attr (die, DW_AT_type, cu);
15126 if (attr != NULL)
15127 {
15128 struct type *underlying_type = die_type (die, cu);
15129
15130 TYPE_TARGET_TYPE (type) = underlying_type;
15131 }
15132
15133 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15134 if (attr != nullptr)
15135 {
15136 TYPE_LENGTH (type) = attr->constant_value (0);
15137 }
15138 else
15139 {
15140 TYPE_LENGTH (type) = 0;
15141 }
15142
15143 maybe_set_alignment (cu, die, type);
15144
15145 /* The enumeration DIE can be incomplete. In Ada, any type can be
15146 declared as private in the package spec, and then defined only
15147 inside the package body. Such types are known as Taft Amendment
15148 Types. When another package uses such a type, an incomplete DIE
15149 may be generated by the compiler. */
15150 if (die_is_declaration (die, cu))
15151 type->set_is_stub (true);
15152
15153 /* If this type has an underlying type that is not a stub, then we
15154 may use its attributes. We always use the "unsigned" attribute
15155 in this situation, because ordinarily we guess whether the type
15156 is unsigned -- but the guess can be wrong and the underlying type
15157 can tell us the reality. However, we defer to a local size
15158 attribute if one exists, because this lets the compiler override
15159 the underlying type if needed. */
15160 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
15161 {
15162 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15163 underlying_type = check_typedef (underlying_type);
15164
15165 type->set_is_unsigned (underlying_type->is_unsigned ());
15166
15167 if (TYPE_LENGTH (type) == 0)
15168 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15169
15170 if (TYPE_RAW_ALIGN (type) == 0
15171 && TYPE_RAW_ALIGN (underlying_type) != 0)
15172 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15173 }
15174
15175 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15176
15177 set_die_type (die, type, cu);
15178
15179 /* Finish the creation of this type by using the enum's children.
15180 Note that, as usual, this must come after set_die_type to avoid
15181 infinite recursion when trying to compute the names of the
15182 enumerators. */
15183 update_enumeration_type_from_children (die, type, cu);
15184
15185 return type;
15186 }
15187
15188 /* Given a pointer to a die which begins an enumeration, process all
15189 the dies that define the members of the enumeration, and create the
15190 symbol for the enumeration type.
15191
15192 NOTE: We reverse the order of the element list. */
15193
15194 static void
15195 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15196 {
15197 struct type *this_type;
15198
15199 this_type = get_die_type (die, cu);
15200 if (this_type == NULL)
15201 this_type = read_enumeration_type (die, cu);
15202
15203 if (die->child != NULL)
15204 {
15205 struct die_info *child_die;
15206 const char *name;
15207
15208 child_die = die->child;
15209 while (child_die && child_die->tag)
15210 {
15211 if (child_die->tag != DW_TAG_enumerator)
15212 {
15213 process_die (child_die, cu);
15214 }
15215 else
15216 {
15217 name = dwarf2_name (child_die, cu);
15218 if (name)
15219 new_symbol (child_die, this_type, cu);
15220 }
15221
15222 child_die = child_die->sibling;
15223 }
15224 }
15225
15226 /* If we are reading an enum from a .debug_types unit, and the enum
15227 is a declaration, and the enum is not the signatured type in the
15228 unit, then we do not want to add a symbol for it. Adding a
15229 symbol would in some cases obscure the true definition of the
15230 enum, giving users an incomplete type when the definition is
15231 actually available. Note that we do not want to do this for all
15232 enums which are just declarations, because C++0x allows forward
15233 enum declarations. */
15234 if (cu->per_cu->is_debug_types
15235 && die_is_declaration (die, cu))
15236 {
15237 struct signatured_type *sig_type;
15238
15239 sig_type = (struct signatured_type *) cu->per_cu;
15240 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15241 if (sig_type->type_offset_in_section != die->sect_off)
15242 return;
15243 }
15244
15245 new_symbol (die, this_type, cu);
15246 }
15247
15248 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15249 expression for an index type and finds the corresponding field
15250 offset in the hidden "P_BOUNDS" structure. Returns true on success
15251 and updates *FIELD, false if it fails to recognize an
15252 expression. */
15253
15254 static bool
15255 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15256 int *bounds_offset, struct field *field,
15257 struct dwarf2_cu *cu)
15258 {
15259 struct attribute *attr = dwarf2_attr (die, name, cu);
15260 if (attr == nullptr || !attr->form_is_block ())
15261 return false;
15262
15263 const struct dwarf_block *block = attr->as_block ();
15264 const gdb_byte *start = block->data;
15265 const gdb_byte *end = block->data + block->size;
15266
15267 /* The expression to recognize generally looks like:
15268
15269 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15270 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15271
15272 However, the second "plus_uconst" may be missing:
15273
15274 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15275 DW_OP_deref_size: 4)
15276
15277 This happens when the field is at the start of the structure.
15278
15279 Also, the final deref may not be sized:
15280
15281 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15282 DW_OP_deref)
15283
15284 This happens when the size of the index type happens to be the
15285 same as the architecture's word size. This can occur with or
15286 without the second plus_uconst. */
15287
15288 if (end - start < 2)
15289 return false;
15290 if (*start++ != DW_OP_push_object_address)
15291 return false;
15292 if (*start++ != DW_OP_plus_uconst)
15293 return false;
15294
15295 uint64_t this_bound_off;
15296 start = gdb_read_uleb128 (start, end, &this_bound_off);
15297 if (start == nullptr || (int) this_bound_off != this_bound_off)
15298 return false;
15299 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15300 is consistent among all bounds. */
15301 if (*bounds_offset == -1)
15302 *bounds_offset = this_bound_off;
15303 else if (*bounds_offset != this_bound_off)
15304 return false;
15305
15306 if (start == end || *start++ != DW_OP_deref)
15307 return false;
15308
15309 int offset = 0;
15310 if (start ==end)
15311 return false;
15312 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15313 {
15314 /* This means an offset of 0. */
15315 }
15316 else if (*start++ != DW_OP_plus_uconst)
15317 return false;
15318 else
15319 {
15320 /* The size is the parameter to DW_OP_plus_uconst. */
15321 uint64_t val;
15322 start = gdb_read_uleb128 (start, end, &val);
15323 if (start == nullptr)
15324 return false;
15325 if ((int) val != val)
15326 return false;
15327 offset = val;
15328 }
15329
15330 if (start == end)
15331 return false;
15332
15333 uint64_t size;
15334 if (*start == DW_OP_deref_size)
15335 {
15336 start = gdb_read_uleb128 (start + 1, end, &size);
15337 if (start == nullptr)
15338 return false;
15339 }
15340 else if (*start == DW_OP_deref)
15341 {
15342 size = cu->header.addr_size;
15343 ++start;
15344 }
15345 else
15346 return false;
15347
15348 field->set_loc_bitpos (8 * offset);
15349 if (size != TYPE_LENGTH (field->type ()))
15350 FIELD_BITSIZE (*field) = 8 * size;
15351
15352 return true;
15353 }
15354
15355 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15356 some kinds of Ada arrays:
15357
15358 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15359 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15360 <11e0> DW_AT_data_location: 2 byte block: 97 6
15361 (DW_OP_push_object_address; DW_OP_deref)
15362 <11e3> DW_AT_type : <0x1173>
15363 <11e7> DW_AT_sibling : <0x1201>
15364 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15365 <11ec> DW_AT_type : <0x1206>
15366 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15367 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15368 DW_OP_deref_size: 4)
15369 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15370 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15371 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15372
15373 This actually represents a "thick pointer", which is a structure
15374 with two elements: one that is a pointer to the array data, and one
15375 that is a pointer to another structure; this second structure holds
15376 the array bounds.
15377
15378 This returns a new type on success, or nullptr if this didn't
15379 recognize the type. */
15380
15381 static struct type *
15382 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15383 struct type *type)
15384 {
15385 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15386 /* So far we've only seen this with block form. */
15387 if (attr == nullptr || !attr->form_is_block ())
15388 return nullptr;
15389
15390 /* Note that this will fail if the structure layout is changed by
15391 the compiler. However, we have no good way to recognize some
15392 other layout, because we don't know what expression the compiler
15393 might choose to emit should this happen. */
15394 struct dwarf_block *blk = attr->as_block ();
15395 if (blk->size != 2
15396 || blk->data[0] != DW_OP_push_object_address
15397 || blk->data[1] != DW_OP_deref)
15398 return nullptr;
15399
15400 int bounds_offset = -1;
15401 int max_align = -1;
15402 std::vector<struct field> range_fields;
15403 for (struct die_info *child_die = die->child;
15404 child_die;
15405 child_die = child_die->sibling)
15406 {
15407 if (child_die->tag == DW_TAG_subrange_type)
15408 {
15409 struct type *underlying = read_subrange_index_type (child_die, cu);
15410
15411 int this_align = type_align (underlying);
15412 if (this_align > max_align)
15413 max_align = this_align;
15414
15415 range_fields.emplace_back ();
15416 range_fields.emplace_back ();
15417
15418 struct field &lower = range_fields[range_fields.size () - 2];
15419 struct field &upper = range_fields[range_fields.size () - 1];
15420
15421 lower.set_type (underlying);
15422 FIELD_ARTIFICIAL (lower) = 1;
15423
15424 upper.set_type (underlying);
15425 FIELD_ARTIFICIAL (upper) = 1;
15426
15427 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15428 &bounds_offset, &lower, cu)
15429 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15430 &bounds_offset, &upper, cu))
15431 return nullptr;
15432 }
15433 }
15434
15435 /* This shouldn't really happen, but double-check that we found
15436 where the bounds are stored. */
15437 if (bounds_offset == -1)
15438 return nullptr;
15439
15440 struct objfile *objfile = cu->per_objfile->objfile;
15441 for (int i = 0; i < range_fields.size (); i += 2)
15442 {
15443 char name[20];
15444
15445 /* Set the name of each field in the bounds. */
15446 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15447 range_fields[i].set_name (objfile->intern (name));
15448 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15449 range_fields[i + 1].set_name (objfile->intern (name));
15450 }
15451
15452 struct type *bounds = alloc_type (objfile);
15453 bounds->set_code (TYPE_CODE_STRUCT);
15454
15455 bounds->set_num_fields (range_fields.size ());
15456 bounds->set_fields
15457 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15458 * sizeof (struct field))));
15459 memcpy (bounds->fields (), range_fields.data (),
15460 bounds->num_fields () * sizeof (struct field));
15461
15462 int last_fieldno = range_fields.size () - 1;
15463 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15464 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
15465 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
15466
15467 /* Rewrite the existing array type in place. Specifically, we
15468 remove any dynamic properties we might have read, and we replace
15469 the index types. */
15470 struct type *iter = type;
15471 for (int i = 0; i < range_fields.size (); i += 2)
15472 {
15473 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15474 iter->main_type->dyn_prop_list = nullptr;
15475 iter->set_index_type
15476 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15477 iter = TYPE_TARGET_TYPE (iter);
15478 }
15479
15480 struct type *result = alloc_type (objfile);
15481 result->set_code (TYPE_CODE_STRUCT);
15482
15483 result->set_num_fields (2);
15484 result->set_fields
15485 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15486 * sizeof (struct field))));
15487
15488 /* The names are chosen to coincide with what the compiler does with
15489 -fgnat-encodings=all, which the Ada code in gdb already
15490 understands. */
15491 result->field (0).set_name ("P_ARRAY");
15492 result->field (0).set_type (lookup_pointer_type (type));
15493
15494 result->field (1).set_name ("P_BOUNDS");
15495 result->field (1).set_type (lookup_pointer_type (bounds));
15496 result->field (1).set_loc_bitpos (8 * bounds_offset);
15497
15498 result->set_name (type->name ());
15499 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
15500 + TYPE_LENGTH (result->field (1).type ()));
15501
15502 return result;
15503 }
15504
15505 /* Extract all information from a DW_TAG_array_type DIE and put it in
15506 the DIE's type field. For now, this only handles one dimensional
15507 arrays. */
15508
15509 static struct type *
15510 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15511 {
15512 struct objfile *objfile = cu->per_objfile->objfile;
15513 struct die_info *child_die;
15514 struct type *type;
15515 struct type *element_type, *range_type, *index_type;
15516 struct attribute *attr;
15517 const char *name;
15518 struct dynamic_prop *byte_stride_prop = NULL;
15519 unsigned int bit_stride = 0;
15520
15521 element_type = die_type (die, cu);
15522
15523 /* The die_type call above may have already set the type for this DIE. */
15524 type = get_die_type (die, cu);
15525 if (type)
15526 return type;
15527
15528 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15529 if (attr != NULL)
15530 {
15531 int stride_ok;
15532 struct type *prop_type = cu->addr_sized_int_type (false);
15533
15534 byte_stride_prop
15535 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15536 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15537 prop_type);
15538 if (!stride_ok)
15539 {
15540 complaint (_("unable to read array DW_AT_byte_stride "
15541 " - DIE at %s [in module %s]"),
15542 sect_offset_str (die->sect_off),
15543 objfile_name (cu->per_objfile->objfile));
15544 /* Ignore this attribute. We will likely not be able to print
15545 arrays of this type correctly, but there is little we can do
15546 to help if we cannot read the attribute's value. */
15547 byte_stride_prop = NULL;
15548 }
15549 }
15550
15551 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15552 if (attr != NULL)
15553 bit_stride = attr->constant_value (0);
15554
15555 /* Irix 6.2 native cc creates array types without children for
15556 arrays with unspecified length. */
15557 if (die->child == NULL)
15558 {
15559 index_type = objfile_type (objfile)->builtin_int;
15560 range_type = create_static_range_type (NULL, index_type, 0, -1);
15561 type = create_array_type_with_stride (NULL, element_type, range_type,
15562 byte_stride_prop, bit_stride);
15563 return set_die_type (die, type, cu);
15564 }
15565
15566 std::vector<struct type *> range_types;
15567 child_die = die->child;
15568 while (child_die && child_die->tag)
15569 {
15570 if (child_die->tag == DW_TAG_subrange_type
15571 || child_die->tag == DW_TAG_generic_subrange)
15572 {
15573 struct type *child_type = read_type_die (child_die, cu);
15574
15575 if (child_type != NULL)
15576 {
15577 /* The range type was succesfully read. Save it for the
15578 array type creation. */
15579 range_types.push_back (child_type);
15580 }
15581 }
15582 child_die = child_die->sibling;
15583 }
15584
15585 if (range_types.empty ())
15586 {
15587 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15588 sect_offset_str (die->sect_off),
15589 objfile_name (cu->per_objfile->objfile));
15590 return NULL;
15591 }
15592
15593 /* Dwarf2 dimensions are output from left to right, create the
15594 necessary array types in backwards order. */
15595
15596 type = element_type;
15597
15598 if (read_array_order (die, cu) == DW_ORD_col_major)
15599 {
15600 int i = 0;
15601
15602 while (i < range_types.size ())
15603 {
15604 type = create_array_type_with_stride (NULL, type, range_types[i++],
15605 byte_stride_prop, bit_stride);
15606 bit_stride = 0;
15607 byte_stride_prop = nullptr;
15608 }
15609 }
15610 else
15611 {
15612 size_t ndim = range_types.size ();
15613 while (ndim-- > 0)
15614 {
15615 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15616 byte_stride_prop, bit_stride);
15617 bit_stride = 0;
15618 byte_stride_prop = nullptr;
15619 }
15620 }
15621
15622 gdb_assert (type != element_type);
15623
15624 /* Understand Dwarf2 support for vector types (like they occur on
15625 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15626 array type. This is not part of the Dwarf2/3 standard yet, but a
15627 custom vendor extension. The main difference between a regular
15628 array and the vector variant is that vectors are passed by value
15629 to functions. */
15630 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15631 if (attr != nullptr)
15632 make_vector_type (type);
15633
15634 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15635 implementation may choose to implement triple vectors using this
15636 attribute. */
15637 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15638 if (attr != nullptr && attr->form_is_unsigned ())
15639 {
15640 if (attr->as_unsigned () >= TYPE_LENGTH (type))
15641 TYPE_LENGTH (type) = attr->as_unsigned ();
15642 else
15643 complaint (_("DW_AT_byte_size for array type smaller "
15644 "than the total size of elements"));
15645 }
15646
15647 name = dwarf2_name (die, cu);
15648 if (name)
15649 type->set_name (name);
15650
15651 maybe_set_alignment (cu, die, type);
15652
15653 struct type *replacement_type = nullptr;
15654 if (cu->per_cu->lang == language_ada)
15655 {
15656 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15657 if (replacement_type != nullptr)
15658 type = replacement_type;
15659 }
15660
15661 /* Install the type in the die. */
15662 set_die_type (die, type, cu, replacement_type != nullptr);
15663
15664 /* set_die_type should be already done. */
15665 set_descriptive_type (type, die, cu);
15666
15667 return type;
15668 }
15669
15670 static enum dwarf_array_dim_ordering
15671 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15672 {
15673 struct attribute *attr;
15674
15675 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15676
15677 if (attr != nullptr)
15678 {
15679 LONGEST val = attr->constant_value (-1);
15680 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15681 return (enum dwarf_array_dim_ordering) val;
15682 }
15683
15684 /* GNU F77 is a special case, as at 08/2004 array type info is the
15685 opposite order to the dwarf2 specification, but data is still
15686 laid out as per normal fortran.
15687
15688 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15689 version checking. */
15690
15691 if (cu->per_cu->lang == language_fortran
15692 && cu->producer && strstr (cu->producer, "GNU F77"))
15693 {
15694 return DW_ORD_row_major;
15695 }
15696
15697 switch (cu->language_defn->array_ordering ())
15698 {
15699 case array_column_major:
15700 return DW_ORD_col_major;
15701 case array_row_major:
15702 default:
15703 return DW_ORD_row_major;
15704 };
15705 }
15706
15707 /* Extract all information from a DW_TAG_set_type DIE and put it in
15708 the DIE's type field. */
15709
15710 static struct type *
15711 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15712 {
15713 struct type *domain_type, *set_type;
15714 struct attribute *attr;
15715
15716 domain_type = die_type (die, cu);
15717
15718 /* The die_type call above may have already set the type for this DIE. */
15719 set_type = get_die_type (die, cu);
15720 if (set_type)
15721 return set_type;
15722
15723 set_type = create_set_type (NULL, domain_type);
15724
15725 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15726 if (attr != nullptr && attr->form_is_unsigned ())
15727 TYPE_LENGTH (set_type) = attr->as_unsigned ();
15728
15729 maybe_set_alignment (cu, die, set_type);
15730
15731 return set_die_type (die, set_type, cu);
15732 }
15733
15734 /* A helper for read_common_block that creates a locexpr baton.
15735 SYM is the symbol which we are marking as computed.
15736 COMMON_DIE is the DIE for the common block.
15737 COMMON_LOC is the location expression attribute for the common
15738 block itself.
15739 MEMBER_LOC is the location expression attribute for the particular
15740 member of the common block that we are processing.
15741 CU is the CU from which the above come. */
15742
15743 static void
15744 mark_common_block_symbol_computed (struct symbol *sym,
15745 struct die_info *common_die,
15746 struct attribute *common_loc,
15747 struct attribute *member_loc,
15748 struct dwarf2_cu *cu)
15749 {
15750 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15751 struct objfile *objfile = per_objfile->objfile;
15752 struct dwarf2_locexpr_baton *baton;
15753 gdb_byte *ptr;
15754 unsigned int cu_off;
15755 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15756 LONGEST offset = 0;
15757
15758 gdb_assert (common_loc && member_loc);
15759 gdb_assert (common_loc->form_is_block ());
15760 gdb_assert (member_loc->form_is_block ()
15761 || member_loc->form_is_constant ());
15762
15763 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15764 baton->per_objfile = per_objfile;
15765 baton->per_cu = cu->per_cu;
15766 gdb_assert (baton->per_cu);
15767
15768 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15769
15770 if (member_loc->form_is_constant ())
15771 {
15772 offset = member_loc->constant_value (0);
15773 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15774 }
15775 else
15776 baton->size += member_loc->as_block ()->size;
15777
15778 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15779 baton->data = ptr;
15780
15781 *ptr++ = DW_OP_call4;
15782 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15783 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15784 ptr += 4;
15785
15786 if (member_loc->form_is_constant ())
15787 {
15788 *ptr++ = DW_OP_addr;
15789 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15790 ptr += cu->header.addr_size;
15791 }
15792 else
15793 {
15794 /* We have to copy the data here, because DW_OP_call4 will only
15795 use a DW_AT_location attribute. */
15796 struct dwarf_block *block = member_loc->as_block ();
15797 memcpy (ptr, block->data, block->size);
15798 ptr += block->size;
15799 }
15800
15801 *ptr++ = DW_OP_plus;
15802 gdb_assert (ptr - baton->data == baton->size);
15803
15804 SYMBOL_LOCATION_BATON (sym) = baton;
15805 sym->set_aclass_index (dwarf2_locexpr_index);
15806 }
15807
15808 /* Create appropriate locally-scoped variables for all the
15809 DW_TAG_common_block entries. Also create a struct common_block
15810 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15811 is used to separate the common blocks name namespace from regular
15812 variable names. */
15813
15814 static void
15815 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15816 {
15817 struct attribute *attr;
15818
15819 attr = dwarf2_attr (die, DW_AT_location, cu);
15820 if (attr != nullptr)
15821 {
15822 /* Support the .debug_loc offsets. */
15823 if (attr->form_is_block ())
15824 {
15825 /* Ok. */
15826 }
15827 else if (attr->form_is_section_offset ())
15828 {
15829 dwarf2_complex_location_expr_complaint ();
15830 attr = NULL;
15831 }
15832 else
15833 {
15834 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15835 "common block member");
15836 attr = NULL;
15837 }
15838 }
15839
15840 if (die->child != NULL)
15841 {
15842 struct objfile *objfile = cu->per_objfile->objfile;
15843 struct die_info *child_die;
15844 size_t n_entries = 0, size;
15845 struct common_block *common_block;
15846 struct symbol *sym;
15847
15848 for (child_die = die->child;
15849 child_die && child_die->tag;
15850 child_die = child_die->sibling)
15851 ++n_entries;
15852
15853 size = (sizeof (struct common_block)
15854 + (n_entries - 1) * sizeof (struct symbol *));
15855 common_block
15856 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15857 size);
15858 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15859 common_block->n_entries = 0;
15860
15861 for (child_die = die->child;
15862 child_die && child_die->tag;
15863 child_die = child_die->sibling)
15864 {
15865 /* Create the symbol in the DW_TAG_common_block block in the current
15866 symbol scope. */
15867 sym = new_symbol (child_die, NULL, cu);
15868 if (sym != NULL)
15869 {
15870 struct attribute *member_loc;
15871
15872 common_block->contents[common_block->n_entries++] = sym;
15873
15874 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15875 cu);
15876 if (member_loc)
15877 {
15878 /* GDB has handled this for a long time, but it is
15879 not specified by DWARF. It seems to have been
15880 emitted by gfortran at least as recently as:
15881 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15882 complaint (_("Variable in common block has "
15883 "DW_AT_data_member_location "
15884 "- DIE at %s [in module %s]"),
15885 sect_offset_str (child_die->sect_off),
15886 objfile_name (objfile));
15887
15888 if (member_loc->form_is_section_offset ())
15889 dwarf2_complex_location_expr_complaint ();
15890 else if (member_loc->form_is_constant ()
15891 || member_loc->form_is_block ())
15892 {
15893 if (attr != nullptr)
15894 mark_common_block_symbol_computed (sym, die, attr,
15895 member_loc, cu);
15896 }
15897 else
15898 dwarf2_complex_location_expr_complaint ();
15899 }
15900 }
15901 }
15902
15903 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15904 sym->set_value_common_block (common_block);
15905 }
15906 }
15907
15908 /* Create a type for a C++ namespace. */
15909
15910 static struct type *
15911 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15912 {
15913 struct objfile *objfile = cu->per_objfile->objfile;
15914 const char *previous_prefix, *name;
15915 int is_anonymous;
15916 struct type *type;
15917
15918 /* For extensions, reuse the type of the original namespace. */
15919 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15920 {
15921 struct die_info *ext_die;
15922 struct dwarf2_cu *ext_cu = cu;
15923
15924 ext_die = dwarf2_extension (die, &ext_cu);
15925 type = read_type_die (ext_die, ext_cu);
15926
15927 /* EXT_CU may not be the same as CU.
15928 Ensure TYPE is recorded with CU in die_type_hash. */
15929 return set_die_type (die, type, cu);
15930 }
15931
15932 name = namespace_name (die, &is_anonymous, cu);
15933
15934 /* Now build the name of the current namespace. */
15935
15936 previous_prefix = determine_prefix (die, cu);
15937 if (previous_prefix[0] != '\0')
15938 name = typename_concat (&objfile->objfile_obstack,
15939 previous_prefix, name, 0, cu);
15940
15941 /* Create the type. */
15942 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15943
15944 return set_die_type (die, type, cu);
15945 }
15946
15947 /* Read a namespace scope. */
15948
15949 static void
15950 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15951 {
15952 struct objfile *objfile = cu->per_objfile->objfile;
15953 int is_anonymous;
15954
15955 /* Add a symbol associated to this if we haven't seen the namespace
15956 before. Also, add a using directive if it's an anonymous
15957 namespace. */
15958
15959 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15960 {
15961 struct type *type;
15962
15963 type = read_type_die (die, cu);
15964 new_symbol (die, type, cu);
15965
15966 namespace_name (die, &is_anonymous, cu);
15967 if (is_anonymous)
15968 {
15969 const char *previous_prefix = determine_prefix (die, cu);
15970
15971 std::vector<const char *> excludes;
15972 add_using_directive (using_directives (cu),
15973 previous_prefix, type->name (), NULL,
15974 NULL, excludes, 0, &objfile->objfile_obstack);
15975 }
15976 }
15977
15978 if (die->child != NULL)
15979 {
15980 struct die_info *child_die = die->child;
15981
15982 while (child_die && child_die->tag)
15983 {
15984 process_die (child_die, cu);
15985 child_die = child_die->sibling;
15986 }
15987 }
15988 }
15989
15990 /* Read a Fortran module as type. This DIE can be only a declaration used for
15991 imported module. Still we need that type as local Fortran "use ... only"
15992 declaration imports depend on the created type in determine_prefix. */
15993
15994 static struct type *
15995 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15996 {
15997 struct objfile *objfile = cu->per_objfile->objfile;
15998 const char *module_name;
15999 struct type *type;
16000
16001 module_name = dwarf2_name (die, cu);
16002 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16003
16004 return set_die_type (die, type, cu);
16005 }
16006
16007 /* Read a Fortran module. */
16008
16009 static void
16010 read_module (struct die_info *die, struct dwarf2_cu *cu)
16011 {
16012 struct die_info *child_die = die->child;
16013 struct type *type;
16014
16015 type = read_type_die (die, cu);
16016 new_symbol (die, type, cu);
16017
16018 while (child_die && child_die->tag)
16019 {
16020 process_die (child_die, cu);
16021 child_die = child_die->sibling;
16022 }
16023 }
16024
16025 /* Return the name of the namespace represented by DIE. Set
16026 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16027 namespace. */
16028
16029 static const char *
16030 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16031 {
16032 struct die_info *current_die;
16033 const char *name = NULL;
16034
16035 /* Loop through the extensions until we find a name. */
16036
16037 for (current_die = die;
16038 current_die != NULL;
16039 current_die = dwarf2_extension (die, &cu))
16040 {
16041 /* We don't use dwarf2_name here so that we can detect the absence
16042 of a name -> anonymous namespace. */
16043 name = dwarf2_string_attr (die, DW_AT_name, cu);
16044
16045 if (name != NULL)
16046 break;
16047 }
16048
16049 /* Is it an anonymous namespace? */
16050
16051 *is_anonymous = (name == NULL);
16052 if (*is_anonymous)
16053 name = CP_ANONYMOUS_NAMESPACE_STR;
16054
16055 return name;
16056 }
16057
16058 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16059 the user defined type vector. */
16060
16061 static struct type *
16062 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16063 {
16064 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16065 struct comp_unit_head *cu_header = &cu->header;
16066 struct type *type;
16067 struct attribute *attr_byte_size;
16068 struct attribute *attr_address_class;
16069 int byte_size, addr_class;
16070 struct type *target_type;
16071
16072 target_type = die_type (die, cu);
16073
16074 /* The die_type call above may have already set the type for this DIE. */
16075 type = get_die_type (die, cu);
16076 if (type)
16077 return type;
16078
16079 type = lookup_pointer_type (target_type);
16080
16081 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16082 if (attr_byte_size)
16083 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16084 else
16085 byte_size = cu_header->addr_size;
16086
16087 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16088 if (attr_address_class)
16089 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16090 else
16091 addr_class = DW_ADDR_none;
16092
16093 ULONGEST alignment = get_alignment (cu, die);
16094
16095 /* If the pointer size, alignment, or address class is different
16096 than the default, create a type variant marked as such and set
16097 the length accordingly. */
16098 if (TYPE_LENGTH (type) != byte_size
16099 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16100 && alignment != TYPE_RAW_ALIGN (type))
16101 || addr_class != DW_ADDR_none)
16102 {
16103 if (gdbarch_address_class_type_flags_p (gdbarch))
16104 {
16105 type_instance_flags type_flags
16106 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16107 addr_class);
16108 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16109 == 0);
16110 type = make_type_with_address_space (type, type_flags);
16111 }
16112 else if (TYPE_LENGTH (type) != byte_size)
16113 {
16114 complaint (_("invalid pointer size %d"), byte_size);
16115 }
16116 else if (TYPE_RAW_ALIGN (type) != alignment)
16117 {
16118 complaint (_("Invalid DW_AT_alignment"
16119 " - DIE at %s [in module %s]"),
16120 sect_offset_str (die->sect_off),
16121 objfile_name (cu->per_objfile->objfile));
16122 }
16123 else
16124 {
16125 /* Should we also complain about unhandled address classes? */
16126 }
16127 }
16128
16129 TYPE_LENGTH (type) = byte_size;
16130 set_type_align (type, alignment);
16131 return set_die_type (die, type, cu);
16132 }
16133
16134 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16135 the user defined type vector. */
16136
16137 static struct type *
16138 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16139 {
16140 struct type *type;
16141 struct type *to_type;
16142 struct type *domain;
16143
16144 to_type = die_type (die, cu);
16145 domain = die_containing_type (die, cu);
16146
16147 /* The calls above may have already set the type for this DIE. */
16148 type = get_die_type (die, cu);
16149 if (type)
16150 return type;
16151
16152 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16153 type = lookup_methodptr_type (to_type);
16154 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16155 {
16156 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16157
16158 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16159 to_type->fields (), to_type->num_fields (),
16160 to_type->has_varargs ());
16161 type = lookup_methodptr_type (new_type);
16162 }
16163 else
16164 type = lookup_memberptr_type (to_type, domain);
16165
16166 return set_die_type (die, type, cu);
16167 }
16168
16169 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16170 the user defined type vector. */
16171
16172 static struct type *
16173 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16174 enum type_code refcode)
16175 {
16176 struct comp_unit_head *cu_header = &cu->header;
16177 struct type *type, *target_type;
16178 struct attribute *attr;
16179
16180 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16181
16182 target_type = die_type (die, cu);
16183
16184 /* The die_type call above may have already set the type for this DIE. */
16185 type = get_die_type (die, cu);
16186 if (type)
16187 return type;
16188
16189 type = lookup_reference_type (target_type, refcode);
16190 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16191 if (attr != nullptr)
16192 {
16193 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
16194 }
16195 else
16196 {
16197 TYPE_LENGTH (type) = cu_header->addr_size;
16198 }
16199 maybe_set_alignment (cu, die, type);
16200 return set_die_type (die, type, cu);
16201 }
16202
16203 /* Add the given cv-qualifiers to the element type of the array. GCC
16204 outputs DWARF type qualifiers that apply to an array, not the
16205 element type. But GDB relies on the array element type to carry
16206 the cv-qualifiers. This mimics section 6.7.3 of the C99
16207 specification. */
16208
16209 static struct type *
16210 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16211 struct type *base_type, int cnst, int voltl)
16212 {
16213 struct type *el_type, *inner_array;
16214
16215 base_type = copy_type (base_type);
16216 inner_array = base_type;
16217
16218 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16219 {
16220 TYPE_TARGET_TYPE (inner_array) =
16221 copy_type (TYPE_TARGET_TYPE (inner_array));
16222 inner_array = TYPE_TARGET_TYPE (inner_array);
16223 }
16224
16225 el_type = TYPE_TARGET_TYPE (inner_array);
16226 cnst |= TYPE_CONST (el_type);
16227 voltl |= TYPE_VOLATILE (el_type);
16228 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16229
16230 return set_die_type (die, base_type, cu);
16231 }
16232
16233 static struct type *
16234 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16235 {
16236 struct type *base_type, *cv_type;
16237
16238 base_type = die_type (die, cu);
16239
16240 /* The die_type call above may have already set the type for this DIE. */
16241 cv_type = get_die_type (die, cu);
16242 if (cv_type)
16243 return cv_type;
16244
16245 /* In case the const qualifier is applied to an array type, the element type
16246 is so qualified, not the array type (section 6.7.3 of C99). */
16247 if (base_type->code () == TYPE_CODE_ARRAY)
16248 return add_array_cv_type (die, cu, base_type, 1, 0);
16249
16250 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16251 return set_die_type (die, cv_type, cu);
16252 }
16253
16254 static struct type *
16255 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16256 {
16257 struct type *base_type, *cv_type;
16258
16259 base_type = die_type (die, cu);
16260
16261 /* The die_type call above may have already set the type for this DIE. */
16262 cv_type = get_die_type (die, cu);
16263 if (cv_type)
16264 return cv_type;
16265
16266 /* In case the volatile qualifier is applied to an array type, the
16267 element type is so qualified, not the array type (section 6.7.3
16268 of C99). */
16269 if (base_type->code () == TYPE_CODE_ARRAY)
16270 return add_array_cv_type (die, cu, base_type, 0, 1);
16271
16272 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16273 return set_die_type (die, cv_type, cu);
16274 }
16275
16276 /* Handle DW_TAG_restrict_type. */
16277
16278 static struct type *
16279 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16280 {
16281 struct type *base_type, *cv_type;
16282
16283 base_type = die_type (die, cu);
16284
16285 /* The die_type call above may have already set the type for this DIE. */
16286 cv_type = get_die_type (die, cu);
16287 if (cv_type)
16288 return cv_type;
16289
16290 cv_type = make_restrict_type (base_type);
16291 return set_die_type (die, cv_type, cu);
16292 }
16293
16294 /* Handle DW_TAG_atomic_type. */
16295
16296 static struct type *
16297 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16298 {
16299 struct type *base_type, *cv_type;
16300
16301 base_type = die_type (die, cu);
16302
16303 /* The die_type call above may have already set the type for this DIE. */
16304 cv_type = get_die_type (die, cu);
16305 if (cv_type)
16306 return cv_type;
16307
16308 cv_type = make_atomic_type (base_type);
16309 return set_die_type (die, cv_type, cu);
16310 }
16311
16312 /* Extract all information from a DW_TAG_string_type DIE and add to
16313 the user defined type vector. It isn't really a user defined type,
16314 but it behaves like one, with other DIE's using an AT_user_def_type
16315 attribute to reference it. */
16316
16317 static struct type *
16318 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16319 {
16320 struct objfile *objfile = cu->per_objfile->objfile;
16321 struct gdbarch *gdbarch = objfile->arch ();
16322 struct type *type, *range_type, *index_type, *char_type;
16323 struct attribute *attr;
16324 struct dynamic_prop prop;
16325 bool length_is_constant = true;
16326 LONGEST length;
16327
16328 /* There are a couple of places where bit sizes might be made use of
16329 when parsing a DW_TAG_string_type, however, no producer that we know
16330 of make use of these. Handling bit sizes that are a multiple of the
16331 byte size is easy enough, but what about other bit sizes? Lets deal
16332 with that problem when we have to. Warn about these attributes being
16333 unsupported, then parse the type and ignore them like we always
16334 have. */
16335 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16336 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16337 {
16338 static bool warning_printed = false;
16339 if (!warning_printed)
16340 {
16341 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16342 "currently supported on DW_TAG_string_type."));
16343 warning_printed = true;
16344 }
16345 }
16346
16347 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16348 if (attr != nullptr && !attr->form_is_constant ())
16349 {
16350 /* The string length describes the location at which the length of
16351 the string can be found. The size of the length field can be
16352 specified with one of the attributes below. */
16353 struct type *prop_type;
16354 struct attribute *len
16355 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16356 if (len == nullptr)
16357 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16358 if (len != nullptr && len->form_is_constant ())
16359 {
16360 /* Pass 0 as the default as we know this attribute is constant
16361 and the default value will not be returned. */
16362 LONGEST sz = len->constant_value (0);
16363 prop_type = objfile_int_type (objfile, sz, true);
16364 }
16365 else
16366 {
16367 /* If the size is not specified then we assume it is the size of
16368 an address on this target. */
16369 prop_type = cu->addr_sized_int_type (true);
16370 }
16371
16372 /* Convert the attribute into a dynamic property. */
16373 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16374 length = 1;
16375 else
16376 length_is_constant = false;
16377 }
16378 else if (attr != nullptr)
16379 {
16380 /* This DW_AT_string_length just contains the length with no
16381 indirection. There's no need to create a dynamic property in this
16382 case. Pass 0 for the default value as we know it will not be
16383 returned in this case. */
16384 length = attr->constant_value (0);
16385 }
16386 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16387 {
16388 /* We don't currently support non-constant byte sizes for strings. */
16389 length = attr->constant_value (1);
16390 }
16391 else
16392 {
16393 /* Use 1 as a fallback length if we have nothing else. */
16394 length = 1;
16395 }
16396
16397 index_type = objfile_type (objfile)->builtin_int;
16398 if (length_is_constant)
16399 range_type = create_static_range_type (NULL, index_type, 1, length);
16400 else
16401 {
16402 struct dynamic_prop low_bound;
16403
16404 low_bound.set_const_val (1);
16405 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16406 }
16407 char_type = language_string_char_type (cu->language_defn, gdbarch);
16408 type = create_string_type (NULL, char_type, range_type);
16409
16410 return set_die_type (die, type, cu);
16411 }
16412
16413 /* Assuming that DIE corresponds to a function, returns nonzero
16414 if the function is prototyped. */
16415
16416 static int
16417 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16418 {
16419 struct attribute *attr;
16420
16421 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16422 if (attr && attr->as_boolean ())
16423 return 1;
16424
16425 /* The DWARF standard implies that the DW_AT_prototyped attribute
16426 is only meaningful for C, but the concept also extends to other
16427 languages that allow unprototyped functions (Eg: Objective C).
16428 For all other languages, assume that functions are always
16429 prototyped. */
16430 if (cu->per_cu->lang != language_c
16431 && cu->per_cu->lang != language_objc
16432 && cu->per_cu->lang != language_opencl)
16433 return 1;
16434
16435 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16436 prototyped and unprototyped functions; default to prototyped,
16437 since that is more common in modern code (and RealView warns
16438 about unprototyped functions). */
16439 if (producer_is_realview (cu->producer))
16440 return 1;
16441
16442 return 0;
16443 }
16444
16445 /* Handle DIES due to C code like:
16446
16447 struct foo
16448 {
16449 int (*funcp)(int a, long l);
16450 int b;
16451 };
16452
16453 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16454
16455 static struct type *
16456 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16457 {
16458 struct objfile *objfile = cu->per_objfile->objfile;
16459 struct type *type; /* Type that this function returns. */
16460 struct type *ftype; /* Function that returns above type. */
16461 struct attribute *attr;
16462
16463 type = die_type (die, cu);
16464
16465 /* The die_type call above may have already set the type for this DIE. */
16466 ftype = get_die_type (die, cu);
16467 if (ftype)
16468 return ftype;
16469
16470 ftype = lookup_function_type (type);
16471
16472 if (prototyped_function_p (die, cu))
16473 ftype->set_is_prototyped (true);
16474
16475 /* Store the calling convention in the type if it's available in
16476 the subroutine die. Otherwise set the calling convention to
16477 the default value DW_CC_normal. */
16478 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16479 if (attr != nullptr
16480 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16481 TYPE_CALLING_CONVENTION (ftype)
16482 = (enum dwarf_calling_convention) attr->constant_value (0);
16483 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16484 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16485 else
16486 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16487
16488 /* Record whether the function returns normally to its caller or not
16489 if the DWARF producer set that information. */
16490 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16491 if (attr && attr->as_boolean ())
16492 TYPE_NO_RETURN (ftype) = 1;
16493
16494 /* We need to add the subroutine type to the die immediately so
16495 we don't infinitely recurse when dealing with parameters
16496 declared as the same subroutine type. */
16497 set_die_type (die, ftype, cu);
16498
16499 if (die->child != NULL)
16500 {
16501 struct type *void_type = objfile_type (objfile)->builtin_void;
16502 struct die_info *child_die;
16503 int nparams, iparams;
16504
16505 /* Count the number of parameters.
16506 FIXME: GDB currently ignores vararg functions, but knows about
16507 vararg member functions. */
16508 nparams = 0;
16509 child_die = die->child;
16510 while (child_die && child_die->tag)
16511 {
16512 if (child_die->tag == DW_TAG_formal_parameter)
16513 nparams++;
16514 else if (child_die->tag == DW_TAG_unspecified_parameters)
16515 ftype->set_has_varargs (true);
16516
16517 child_die = child_die->sibling;
16518 }
16519
16520 /* Allocate storage for parameters and fill them in. */
16521 ftype->set_num_fields (nparams);
16522 ftype->set_fields
16523 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16524
16525 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16526 even if we error out during the parameters reading below. */
16527 for (iparams = 0; iparams < nparams; iparams++)
16528 ftype->field (iparams).set_type (void_type);
16529
16530 iparams = 0;
16531 child_die = die->child;
16532 while (child_die && child_die->tag)
16533 {
16534 if (child_die->tag == DW_TAG_formal_parameter)
16535 {
16536 struct type *arg_type;
16537
16538 /* DWARF version 2 has no clean way to discern C++
16539 static and non-static member functions. G++ helps
16540 GDB by marking the first parameter for non-static
16541 member functions (which is the this pointer) as
16542 artificial. We pass this information to
16543 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16544
16545 DWARF version 3 added DW_AT_object_pointer, which GCC
16546 4.5 does not yet generate. */
16547 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16548 if (attr != nullptr)
16549 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16550 else
16551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16552 arg_type = die_type (child_die, cu);
16553
16554 /* RealView does not mark THIS as const, which the testsuite
16555 expects. GCC marks THIS as const in method definitions,
16556 but not in the class specifications (GCC PR 43053). */
16557 if (cu->per_cu->lang == language_cplus
16558 && !TYPE_CONST (arg_type)
16559 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16560 {
16561 int is_this = 0;
16562 struct dwarf2_cu *arg_cu = cu;
16563 const char *name = dwarf2_name (child_die, cu);
16564
16565 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16566 if (attr != nullptr)
16567 {
16568 /* If the compiler emits this, use it. */
16569 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16570 is_this = 1;
16571 }
16572 else if (name && strcmp (name, "this") == 0)
16573 /* Function definitions will have the argument names. */
16574 is_this = 1;
16575 else if (name == NULL && iparams == 0)
16576 /* Declarations may not have the names, so like
16577 elsewhere in GDB, assume an artificial first
16578 argument is "this". */
16579 is_this = 1;
16580
16581 if (is_this)
16582 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16583 arg_type, 0);
16584 }
16585
16586 ftype->field (iparams).set_type (arg_type);
16587 iparams++;
16588 }
16589 child_die = child_die->sibling;
16590 }
16591 }
16592
16593 return ftype;
16594 }
16595
16596 static struct type *
16597 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16598 {
16599 struct objfile *objfile = cu->per_objfile->objfile;
16600 const char *name = NULL;
16601 struct type *this_type, *target_type;
16602
16603 name = dwarf2_full_name (NULL, die, cu);
16604 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16605 this_type->set_target_is_stub (true);
16606 set_die_type (die, this_type, cu);
16607 target_type = die_type (die, cu);
16608 if (target_type != this_type)
16609 TYPE_TARGET_TYPE (this_type) = target_type;
16610 else
16611 {
16612 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16613 spec and cause infinite loops in GDB. */
16614 complaint (_("Self-referential DW_TAG_typedef "
16615 "- DIE at %s [in module %s]"),
16616 sect_offset_str (die->sect_off), objfile_name (objfile));
16617 TYPE_TARGET_TYPE (this_type) = NULL;
16618 }
16619 if (name == NULL)
16620 {
16621 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16622 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16623 Handle these by just returning the target type, rather than
16624 constructing an anonymous typedef type and trying to handle this
16625 elsewhere. */
16626 set_die_type (die, target_type, cu);
16627 return target_type;
16628 }
16629 return this_type;
16630 }
16631
16632 /* Helper for get_dwarf2_rational_constant that computes the value of
16633 a given gmp_mpz given an attribute. */
16634
16635 static void
16636 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16637 {
16638 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16639 location expression that pushes an implicit value. */
16640 if (attr->form == DW_FORM_exprloc)
16641 {
16642 dwarf_block *blk = attr->as_block ();
16643 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16644 {
16645 uint64_t len;
16646 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16647 blk->data + blk->size,
16648 &len);
16649 if (ptr - blk->data + len <= blk->size)
16650 {
16651 mpz_import (value->val, len,
16652 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16653 1, 0, 0, ptr);
16654 return;
16655 }
16656 }
16657
16658 /* On failure set it to 1. */
16659 *value = gdb_mpz (1);
16660 }
16661 else if (attr->form_is_block ())
16662 {
16663 dwarf_block *blk = attr->as_block ();
16664 mpz_import (value->val, blk->size,
16665 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16666 1, 0, 0, blk->data);
16667 }
16668 else
16669 *value = gdb_mpz (attr->constant_value (1));
16670 }
16671
16672 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16673 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16674
16675 If the numerator and/or numerator attribute is missing,
16676 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16677 untouched. */
16678
16679 static void
16680 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16681 gdb_mpz *numerator, gdb_mpz *denominator)
16682 {
16683 struct attribute *num_attr, *denom_attr;
16684
16685 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16686 if (num_attr == nullptr)
16687 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16688 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16689
16690 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16691 if (denom_attr == nullptr)
16692 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16693 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16694
16695 if (num_attr == nullptr || denom_attr == nullptr)
16696 return;
16697
16698 get_mpz (cu, numerator, num_attr);
16699 get_mpz (cu, denominator, denom_attr);
16700 }
16701
16702 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16703 rational constant, rather than a signed one.
16704
16705 If the rational constant has a negative value, a complaint
16706 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16707
16708 static void
16709 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16710 struct dwarf2_cu *cu,
16711 gdb_mpz *numerator,
16712 gdb_mpz *denominator)
16713 {
16714 gdb_mpz num (1);
16715 gdb_mpz denom (1);
16716
16717 get_dwarf2_rational_constant (die, cu, &num, &denom);
16718 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16719 {
16720 mpz_neg (num.val, num.val);
16721 mpz_neg (denom.val, denom.val);
16722 }
16723 else if (mpz_sgn (num.val) == -1)
16724 {
16725 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16726 " in DIE at %s"),
16727 sect_offset_str (die->sect_off));
16728 return;
16729 }
16730 else if (mpz_sgn (denom.val) == -1)
16731 {
16732 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16733 " in DIE at %s"),
16734 sect_offset_str (die->sect_off));
16735 return;
16736 }
16737
16738 *numerator = std::move (num);
16739 *denominator = std::move (denom);
16740 }
16741
16742 /* Assuming that ENCODING is a string whose contents starting at the
16743 K'th character is "_nn" where "nn" is a decimal number, scan that
16744 number and set RESULT to the value. K is updated to point to the
16745 character immediately following the number.
16746
16747 If the string does not conform to the format described above, false
16748 is returned, and K may or may not be changed. */
16749
16750 static bool
16751 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16752 {
16753 /* The next character should be an underscore ('_') followed
16754 by a digit. */
16755 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16756 return false;
16757
16758 /* Skip the underscore. */
16759 k++;
16760 int start = k;
16761
16762 /* Determine the number of digits for our number. */
16763 while (isdigit (encoding[k]))
16764 k++;
16765 if (k == start)
16766 return false;
16767
16768 std::string copy (&encoding[start], k - start);
16769 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16770 return false;
16771
16772 return true;
16773 }
16774
16775 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16776 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16777 DENOM, update OFFSET, and return true on success. Return false on
16778 failure. */
16779
16780 static bool
16781 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16782 gdb_mpz *num, gdb_mpz *denom)
16783 {
16784 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16785 return false;
16786 return ada_get_gnat_encoded_number (encoding, offset, denom);
16787 }
16788
16789 /* Assuming DIE corresponds to a fixed point type, finish the creation
16790 of the corresponding TYPE by setting its type-specific data. CU is
16791 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16792 encodings. It is nullptr if the GNAT encoding should be
16793 ignored. */
16794
16795 static void
16796 finish_fixed_point_type (struct type *type, const char *suffix,
16797 struct die_info *die, struct dwarf2_cu *cu)
16798 {
16799 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16800 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16801
16802 /* If GNAT encodings are preferred, don't examine the
16803 attributes. */
16804 struct attribute *attr = nullptr;
16805 if (suffix == nullptr)
16806 {
16807 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16808 if (attr == nullptr)
16809 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16810 if (attr == nullptr)
16811 attr = dwarf2_attr (die, DW_AT_small, cu);
16812 }
16813
16814 /* Numerator and denominator of our fixed-point type's scaling factor.
16815 The default is a scaling factor of 1, which we use as a fallback
16816 when we are not able to decode it (problem with the debugging info,
16817 unsupported forms, bug in GDB, etc...). Using that as the default
16818 allows us to at least print the unscaled value, which might still
16819 be useful to a user. */
16820 gdb_mpz scale_num (1);
16821 gdb_mpz scale_denom (1);
16822
16823 if (attr == nullptr)
16824 {
16825 int offset = 0;
16826 if (suffix != nullptr
16827 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16828 &scale_denom)
16829 /* The number might be encoded as _nn_dd_nn_dd, where the
16830 second ratio is the 'small value. In this situation, we
16831 want the second value. */
16832 && (suffix[offset] != '_'
16833 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16834 &scale_denom)))
16835 {
16836 /* Found it. */
16837 }
16838 else
16839 {
16840 /* Scaling factor not found. Assume a scaling factor of 1,
16841 and hope for the best. At least the user will be able to
16842 see the encoded value. */
16843 scale_num = 1;
16844 scale_denom = 1;
16845 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16846 sect_offset_str (die->sect_off));
16847 }
16848 }
16849 else if (attr->name == DW_AT_binary_scale)
16850 {
16851 LONGEST scale_exp = attr->constant_value (0);
16852 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16853
16854 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16855 }
16856 else if (attr->name == DW_AT_decimal_scale)
16857 {
16858 LONGEST scale_exp = attr->constant_value (0);
16859 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16860
16861 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16862 }
16863 else if (attr->name == DW_AT_small)
16864 {
16865 struct die_info *scale_die;
16866 struct dwarf2_cu *scale_cu = cu;
16867
16868 scale_die = follow_die_ref (die, attr, &scale_cu);
16869 if (scale_die->tag == DW_TAG_constant)
16870 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
16871 &scale_num, &scale_denom);
16872 else
16873 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16874 " (DIE at %s)"),
16875 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16876 }
16877 else
16878 {
16879 complaint (_("unsupported scale attribute %s for fixed-point type"
16880 " (DIE at %s)"),
16881 dwarf_attr_name (attr->name),
16882 sect_offset_str (die->sect_off));
16883 }
16884
16885 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
16886 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
16887 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
16888 mpq_canonicalize (scaling_factor.val);
16889 }
16890
16891 /* The gnat-encoding suffix for fixed point. */
16892
16893 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16894
16895 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16896 "XF" suffix of the name. The text after this is what encodes the
16897 'small and 'delta information. Otherwise, return nullptr. */
16898
16899 static const char *
16900 gnat_encoded_fixed_point_type_info (const char *name)
16901 {
16902 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
16903 }
16904
16905 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16906 (which may be different from NAME) to the architecture back-end to allow
16907 it to guess the correct format if necessary. */
16908
16909 static struct type *
16910 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16911 const char *name_hint, enum bfd_endian byte_order)
16912 {
16913 struct gdbarch *gdbarch = objfile->arch ();
16914 const struct floatformat **format;
16915 struct type *type;
16916
16917 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16918 if (format)
16919 type = init_float_type (objfile, bits, name, format, byte_order);
16920 else
16921 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16922
16923 return type;
16924 }
16925
16926 /* Allocate an integer type of size BITS and name NAME. */
16927
16928 static struct type *
16929 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16930 int bits, int unsigned_p, const char *name)
16931 {
16932 struct type *type;
16933
16934 /* Versions of Intel's C Compiler generate an integer type called "void"
16935 instead of using DW_TAG_unspecified_type. This has been seen on
16936 at least versions 14, 17, and 18. */
16937 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16938 && strcmp (name, "void") == 0)
16939 type = objfile_type (objfile)->builtin_void;
16940 else
16941 type = init_integer_type (objfile, bits, unsigned_p, name);
16942
16943 return type;
16944 }
16945
16946 /* Return true if DIE has a DW_AT_small attribute whose value is
16947 a constant rational, where both the numerator and denominator
16948 are equal to zero.
16949
16950 CU is the DIE's Compilation Unit. */
16951
16952 static bool
16953 has_zero_over_zero_small_attribute (struct die_info *die,
16954 struct dwarf2_cu *cu)
16955 {
16956 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
16957 if (attr == nullptr)
16958 return false;
16959
16960 struct dwarf2_cu *scale_cu = cu;
16961 struct die_info *scale_die
16962 = follow_die_ref (die, attr, &scale_cu);
16963
16964 if (scale_die->tag != DW_TAG_constant)
16965 return false;
16966
16967 gdb_mpz num (1), denom (1);
16968 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
16969 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
16970 }
16971
16972 /* Initialise and return a floating point type of size BITS suitable for
16973 use as a component of a complex number. The NAME_HINT is passed through
16974 when initialising the floating point type and is the name of the complex
16975 type.
16976
16977 As DWARF doesn't currently provide an explicit name for the components
16978 of a complex number, but it can be helpful to have these components
16979 named, we try to select a suitable name based on the size of the
16980 component. */
16981 static struct type *
16982 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16983 struct objfile *objfile,
16984 int bits, const char *name_hint,
16985 enum bfd_endian byte_order)
16986 {
16987 gdbarch *gdbarch = objfile->arch ();
16988 struct type *tt = nullptr;
16989
16990 /* Try to find a suitable floating point builtin type of size BITS.
16991 We're going to use the name of this type as the name for the complex
16992 target type that we are about to create. */
16993 switch (cu->per_cu->lang)
16994 {
16995 case language_fortran:
16996 switch (bits)
16997 {
16998 case 32:
16999 tt = builtin_f_type (gdbarch)->builtin_real;
17000 break;
17001 case 64:
17002 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17003 break;
17004 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17005 case 128:
17006 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17007 break;
17008 }
17009 break;
17010 default:
17011 switch (bits)
17012 {
17013 case 32:
17014 tt = builtin_type (gdbarch)->builtin_float;
17015 break;
17016 case 64:
17017 tt = builtin_type (gdbarch)->builtin_double;
17018 break;
17019 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17020 case 128:
17021 tt = builtin_type (gdbarch)->builtin_long_double;
17022 break;
17023 }
17024 break;
17025 }
17026
17027 /* If the type we found doesn't match the size we were looking for, then
17028 pretend we didn't find a type at all, the complex target type we
17029 create will then be nameless. */
17030 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17031 tt = nullptr;
17032
17033 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17034 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17035 }
17036
17037 /* Find a representation of a given base type and install
17038 it in the TYPE field of the die. */
17039
17040 static struct type *
17041 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17042 {
17043 struct objfile *objfile = cu->per_objfile->objfile;
17044 struct type *type;
17045 struct attribute *attr;
17046 int encoding = 0, bits = 0;
17047 const char *name;
17048 gdbarch *arch;
17049
17050 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17051 if (attr != nullptr && attr->form_is_constant ())
17052 encoding = attr->constant_value (0);
17053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17054 if (attr != nullptr)
17055 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17056 name = dwarf2_name (die, cu);
17057 if (!name)
17058 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17059
17060 arch = objfile->arch ();
17061 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17062
17063 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17064 if (attr != nullptr && attr->form_is_constant ())
17065 {
17066 int endianity = attr->constant_value (0);
17067
17068 switch (endianity)
17069 {
17070 case DW_END_big:
17071 byte_order = BFD_ENDIAN_BIG;
17072 break;
17073 case DW_END_little:
17074 byte_order = BFD_ENDIAN_LITTLE;
17075 break;
17076 default:
17077 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17078 break;
17079 }
17080 }
17081
17082 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17083 && cu->per_cu->lang == language_ada
17084 && has_zero_over_zero_small_attribute (die, cu))
17085 {
17086 /* brobecker/2018-02-24: This is a fixed point type for which
17087 the scaling factor is represented as fraction whose value
17088 does not make sense (zero divided by zero), so we should
17089 normally never see these. However, there is a small category
17090 of fixed point types for which GNAT is unable to provide
17091 the scaling factor via the standard DWARF mechanisms, and
17092 for which the info is provided via the GNAT encodings instead.
17093 This is likely what this DIE is about. */
17094 encoding = (encoding == DW_ATE_signed_fixed
17095 ? DW_ATE_signed
17096 : DW_ATE_unsigned);
17097 }
17098
17099 /* With GNAT encodings, fixed-point information will be encoded in
17100 the type name. Note that this can also occur with the above
17101 zero-over-zero case, which is why this is a separate "if" rather
17102 than an "else if". */
17103 const char *gnat_encoding_suffix = nullptr;
17104 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17105 && cu->per_cu->lang == language_ada
17106 && name != nullptr)
17107 {
17108 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17109 if (gnat_encoding_suffix != nullptr)
17110 {
17111 gdb_assert (startswith (gnat_encoding_suffix,
17112 GNAT_FIXED_POINT_SUFFIX));
17113 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17114 name, gnat_encoding_suffix - name);
17115 /* Use -1 here so that SUFFIX points at the "_" after the
17116 "XF". */
17117 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17118
17119 encoding = (encoding == DW_ATE_signed
17120 ? DW_ATE_signed_fixed
17121 : DW_ATE_unsigned_fixed);
17122 }
17123 }
17124
17125 switch (encoding)
17126 {
17127 case DW_ATE_address:
17128 /* Turn DW_ATE_address into a void * pointer. */
17129 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17130 type = init_pointer_type (objfile, bits, name, type);
17131 break;
17132 case DW_ATE_boolean:
17133 type = init_boolean_type (objfile, bits, 1, name);
17134 break;
17135 case DW_ATE_complex_float:
17136 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17137 byte_order);
17138 if (type->code () == TYPE_CODE_ERROR)
17139 {
17140 if (name == nullptr)
17141 {
17142 struct obstack *obstack
17143 = &cu->per_objfile->objfile->objfile_obstack;
17144 name = obconcat (obstack, "_Complex ", type->name (),
17145 nullptr);
17146 }
17147 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17148 }
17149 else
17150 type = init_complex_type (name, type);
17151 break;
17152 case DW_ATE_decimal_float:
17153 type = init_decfloat_type (objfile, bits, name);
17154 break;
17155 case DW_ATE_float:
17156 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17157 break;
17158 case DW_ATE_signed:
17159 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17160 break;
17161 case DW_ATE_unsigned:
17162 if (cu->per_cu->lang == language_fortran
17163 && name
17164 && startswith (name, "character("))
17165 type = init_character_type (objfile, bits, 1, name);
17166 else
17167 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17168 break;
17169 case DW_ATE_signed_char:
17170 if (cu->per_cu->lang == language_ada
17171 || cu->per_cu->lang == language_m2
17172 || cu->per_cu->lang == language_pascal
17173 || cu->per_cu->lang == language_fortran)
17174 type = init_character_type (objfile, bits, 0, name);
17175 else
17176 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17177 break;
17178 case DW_ATE_unsigned_char:
17179 if (cu->per_cu->lang == language_ada
17180 || cu->per_cu->lang == language_m2
17181 || cu->per_cu->lang == language_pascal
17182 || cu->per_cu->lang == language_fortran
17183 || cu->per_cu->lang == language_rust)
17184 type = init_character_type (objfile, bits, 1, name);
17185 else
17186 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17187 break;
17188 case DW_ATE_UTF:
17189 {
17190 type = init_character_type (objfile, bits, 1, name);
17191 return set_die_type (die, type, cu);
17192 }
17193 break;
17194 case DW_ATE_signed_fixed:
17195 type = init_fixed_point_type (objfile, bits, 0, name);
17196 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17197 break;
17198 case DW_ATE_unsigned_fixed:
17199 type = init_fixed_point_type (objfile, bits, 1, name);
17200 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17201 break;
17202
17203 default:
17204 complaint (_("unsupported DW_AT_encoding: '%s'"),
17205 dwarf_type_encoding_name (encoding));
17206 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17207 break;
17208 }
17209
17210 if (type->code () == TYPE_CODE_INT
17211 && name != nullptr
17212 && strcmp (name, "char") == 0)
17213 type->set_has_no_signedness (true);
17214
17215 maybe_set_alignment (cu, die, type);
17216
17217 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17218
17219 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17220 {
17221 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17222 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
17223 {
17224 unsigned real_bit_size = attr->as_unsigned ();
17225 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17226 /* Only use the attributes if they make sense together. */
17227 if (attr == nullptr
17228 || (attr->as_unsigned () + real_bit_size
17229 <= 8 * TYPE_LENGTH (type)))
17230 {
17231 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17232 = real_bit_size;
17233 if (attr != nullptr)
17234 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17235 = attr->as_unsigned ();
17236 }
17237 }
17238 }
17239
17240 return set_die_type (die, type, cu);
17241 }
17242
17243 /* A helper function that returns the name of DIE, if it refers to a
17244 variable declaration. */
17245
17246 static const char *
17247 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17248 {
17249 if (die->tag != DW_TAG_variable)
17250 return nullptr;
17251
17252 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17253 if (attr == nullptr || !attr->as_boolean ())
17254 return nullptr;
17255
17256 attr = dwarf2_attr (die, DW_AT_name, cu);
17257 if (attr == nullptr)
17258 return nullptr;
17259 return attr->as_string ();
17260 }
17261
17262 /* Parse dwarf attribute if it's a block, reference or constant and put the
17263 resulting value of the attribute into struct bound_prop.
17264 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17265
17266 static int
17267 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17268 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17269 struct type *default_type)
17270 {
17271 struct dwarf2_property_baton *baton;
17272 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17273 struct objfile *objfile = per_objfile->objfile;
17274 struct obstack *obstack = &objfile->objfile_obstack;
17275
17276 gdb_assert (default_type != NULL);
17277
17278 if (attr == NULL || prop == NULL)
17279 return 0;
17280
17281 if (attr->form_is_block ())
17282 {
17283 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17284 baton->property_type = default_type;
17285 baton->locexpr.per_cu = cu->per_cu;
17286 baton->locexpr.per_objfile = per_objfile;
17287
17288 struct dwarf_block *block;
17289 if (attr->form == DW_FORM_data16)
17290 {
17291 size_t data_size = 16;
17292 block = XOBNEW (obstack, struct dwarf_block);
17293 block->size = (data_size
17294 + 2 /* Extra bytes for DW_OP and arg. */);
17295 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17296 data[0] = DW_OP_implicit_value;
17297 data[1] = data_size;
17298 memcpy (&data[2], attr->as_block ()->data, data_size);
17299 block->data = data;
17300 }
17301 else
17302 block = attr->as_block ();
17303
17304 baton->locexpr.size = block->size;
17305 baton->locexpr.data = block->data;
17306 switch (attr->name)
17307 {
17308 case DW_AT_string_length:
17309 baton->locexpr.is_reference = true;
17310 break;
17311 default:
17312 baton->locexpr.is_reference = false;
17313 break;
17314 }
17315
17316 prop->set_locexpr (baton);
17317 gdb_assert (prop->baton () != NULL);
17318 }
17319 else if (attr->form_is_ref ())
17320 {
17321 struct dwarf2_cu *target_cu = cu;
17322 struct die_info *target_die;
17323 struct attribute *target_attr;
17324
17325 target_die = follow_die_ref (die, attr, &target_cu);
17326 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17327 if (target_attr == NULL)
17328 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17329 target_cu);
17330 if (target_attr == nullptr)
17331 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17332 target_cu);
17333 if (target_attr == NULL)
17334 {
17335 const char *name = var_decl_name (target_die, target_cu);
17336 if (name != nullptr)
17337 {
17338 prop->set_variable_name (name);
17339 return 1;
17340 }
17341 return 0;
17342 }
17343
17344 switch (target_attr->name)
17345 {
17346 case DW_AT_location:
17347 if (target_attr->form_is_section_offset ())
17348 {
17349 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17350 baton->property_type = die_type (target_die, target_cu);
17351 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17352 prop->set_loclist (baton);
17353 gdb_assert (prop->baton () != NULL);
17354 }
17355 else if (target_attr->form_is_block ())
17356 {
17357 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17358 baton->property_type = die_type (target_die, target_cu);
17359 baton->locexpr.per_cu = cu->per_cu;
17360 baton->locexpr.per_objfile = per_objfile;
17361 struct dwarf_block *block = target_attr->as_block ();
17362 baton->locexpr.size = block->size;
17363 baton->locexpr.data = block->data;
17364 baton->locexpr.is_reference = true;
17365 prop->set_locexpr (baton);
17366 gdb_assert (prop->baton () != NULL);
17367 }
17368 else
17369 {
17370 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17371 "dynamic property");
17372 return 0;
17373 }
17374 break;
17375 case DW_AT_data_member_location:
17376 case DW_AT_data_bit_offset:
17377 {
17378 LONGEST offset;
17379
17380 if (!handle_member_location (target_die, target_cu, &offset))
17381 return 0;
17382
17383 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17384 baton->property_type = read_type_die (target_die->parent,
17385 target_cu);
17386 baton->offset_info.offset = offset;
17387 baton->offset_info.type = die_type (target_die, target_cu);
17388 prop->set_addr_offset (baton);
17389 break;
17390 }
17391 }
17392 }
17393 else if (attr->form_is_constant ())
17394 prop->set_const_val (attr->constant_value (0));
17395 else if (attr->form_is_section_offset ())
17396 {
17397 switch (attr->name)
17398 {
17399 case DW_AT_string_length:
17400 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17401 baton->property_type = default_type;
17402 fill_in_loclist_baton (cu, &baton->loclist, attr);
17403 prop->set_loclist (baton);
17404 gdb_assert (prop->baton () != NULL);
17405 break;
17406 default:
17407 goto invalid;
17408 }
17409 }
17410 else
17411 goto invalid;
17412
17413 return 1;
17414
17415 invalid:
17416 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17417 dwarf2_name (die, cu));
17418 return 0;
17419 }
17420
17421 /* See read.h. */
17422
17423 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17424 present (which is valid) then compute the default type based on the
17425 compilation units address size. */
17426
17427 static struct type *
17428 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17429 {
17430 struct type *index_type = die_type (die, cu);
17431
17432 /* Dwarf-2 specifications explicitly allows to create subrange types
17433 without specifying a base type.
17434 In that case, the base type must be set to the type of
17435 the lower bound, upper bound or count, in that order, if any of these
17436 three attributes references an object that has a type.
17437 If no base type is found, the Dwarf-2 specifications say that
17438 a signed integer type of size equal to the size of an address should
17439 be used.
17440 For the following C code: `extern char gdb_int [];'
17441 GCC produces an empty range DIE.
17442 FIXME: muller/2010-05-28: Possible references to object for low bound,
17443 high bound or count are not yet handled by this code. */
17444 if (index_type->code () == TYPE_CODE_VOID)
17445 index_type = cu->addr_sized_int_type (false);
17446
17447 return index_type;
17448 }
17449
17450 /* Read the given DW_AT_subrange DIE. */
17451
17452 static struct type *
17453 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17454 {
17455 struct type *base_type, *orig_base_type;
17456 struct type *range_type;
17457 struct attribute *attr;
17458 struct dynamic_prop low, high;
17459 int low_default_is_valid;
17460 int high_bound_is_count = 0;
17461 const char *name;
17462 ULONGEST negative_mask;
17463
17464 orig_base_type = read_subrange_index_type (die, cu);
17465
17466 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17467 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17468 creating the range type, but we use the result of check_typedef
17469 when examining properties of the type. */
17470 base_type = check_typedef (orig_base_type);
17471
17472 /* The die_type call above may have already set the type for this DIE. */
17473 range_type = get_die_type (die, cu);
17474 if (range_type)
17475 return range_type;
17476
17477 high.set_const_val (0);
17478
17479 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17480 omitting DW_AT_lower_bound. */
17481 switch (cu->per_cu->lang)
17482 {
17483 case language_c:
17484 case language_cplus:
17485 low.set_const_val (0);
17486 low_default_is_valid = 1;
17487 break;
17488 case language_fortran:
17489 low.set_const_val (1);
17490 low_default_is_valid = 1;
17491 break;
17492 case language_d:
17493 case language_objc:
17494 case language_rust:
17495 low.set_const_val (0);
17496 low_default_is_valid = (cu->header.version >= 4);
17497 break;
17498 case language_ada:
17499 case language_m2:
17500 case language_pascal:
17501 low.set_const_val (1);
17502 low_default_is_valid = (cu->header.version >= 4);
17503 break;
17504 default:
17505 low.set_const_val (0);
17506 low_default_is_valid = 0;
17507 break;
17508 }
17509
17510 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17511 if (attr != nullptr)
17512 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17513 else if (!low_default_is_valid)
17514 complaint (_("Missing DW_AT_lower_bound "
17515 "- DIE at %s [in module %s]"),
17516 sect_offset_str (die->sect_off),
17517 objfile_name (cu->per_objfile->objfile));
17518
17519 struct attribute *attr_ub, *attr_count;
17520 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17521 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17522 {
17523 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17524 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17525 {
17526 /* If bounds are constant do the final calculation here. */
17527 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17528 high.set_const_val (low.const_val () + high.const_val () - 1);
17529 else
17530 high_bound_is_count = 1;
17531 }
17532 else
17533 {
17534 if (attr_ub != NULL)
17535 complaint (_("Unresolved DW_AT_upper_bound "
17536 "- DIE at %s [in module %s]"),
17537 sect_offset_str (die->sect_off),
17538 objfile_name (cu->per_objfile->objfile));
17539 if (attr_count != NULL)
17540 complaint (_("Unresolved DW_AT_count "
17541 "- DIE at %s [in module %s]"),
17542 sect_offset_str (die->sect_off),
17543 objfile_name (cu->per_objfile->objfile));
17544 }
17545 }
17546
17547 LONGEST bias = 0;
17548 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17549 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17550 bias = bias_attr->constant_value (0);
17551
17552 /* Normally, the DWARF producers are expected to use a signed
17553 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17554 But this is unfortunately not always the case, as witnessed
17555 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17556 is used instead. To work around that ambiguity, we treat
17557 the bounds as signed, and thus sign-extend their values, when
17558 the base type is signed. */
17559 negative_mask =
17560 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17561 if (low.kind () == PROP_CONST
17562 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17563 low.set_const_val (low.const_val () | negative_mask);
17564 if (high.kind () == PROP_CONST
17565 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17566 high.set_const_val (high.const_val () | negative_mask);
17567
17568 /* Check for bit and byte strides. */
17569 struct dynamic_prop byte_stride_prop;
17570 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17571 if (attr_byte_stride != nullptr)
17572 {
17573 struct type *prop_type = cu->addr_sized_int_type (false);
17574 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17575 prop_type);
17576 }
17577
17578 struct dynamic_prop bit_stride_prop;
17579 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17580 if (attr_bit_stride != nullptr)
17581 {
17582 /* It only makes sense to have either a bit or byte stride. */
17583 if (attr_byte_stride != nullptr)
17584 {
17585 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17586 "- DIE at %s [in module %s]"),
17587 sect_offset_str (die->sect_off),
17588 objfile_name (cu->per_objfile->objfile));
17589 attr_bit_stride = nullptr;
17590 }
17591 else
17592 {
17593 struct type *prop_type = cu->addr_sized_int_type (false);
17594 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17595 prop_type);
17596 }
17597 }
17598
17599 if (attr_byte_stride != nullptr
17600 || attr_bit_stride != nullptr)
17601 {
17602 bool byte_stride_p = (attr_byte_stride != nullptr);
17603 struct dynamic_prop *stride
17604 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17605
17606 range_type
17607 = create_range_type_with_stride (NULL, orig_base_type, &low,
17608 &high, bias, stride, byte_stride_p);
17609 }
17610 else
17611 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17612
17613 if (high_bound_is_count)
17614 range_type->bounds ()->flag_upper_bound_is_count = 1;
17615
17616 /* Ada expects an empty array on no boundary attributes. */
17617 if (attr == NULL && cu->per_cu->lang != language_ada)
17618 range_type->bounds ()->high.set_undefined ();
17619
17620 name = dwarf2_name (die, cu);
17621 if (name)
17622 range_type->set_name (name);
17623
17624 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17625 if (attr != nullptr)
17626 TYPE_LENGTH (range_type) = attr->constant_value (0);
17627
17628 maybe_set_alignment (cu, die, range_type);
17629
17630 set_die_type (die, range_type, cu);
17631
17632 /* set_die_type should be already done. */
17633 set_descriptive_type (range_type, die, cu);
17634
17635 return range_type;
17636 }
17637
17638 static struct type *
17639 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17640 {
17641 struct type *type;
17642
17643 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17644 type->set_name (dwarf2_name (die, cu));
17645
17646 /* In Ada, an unspecified type is typically used when the description
17647 of the type is deferred to a different unit. When encountering
17648 such a type, we treat it as a stub, and try to resolve it later on,
17649 when needed. */
17650 if (cu->per_cu->lang == language_ada)
17651 type->set_is_stub (true);
17652
17653 return set_die_type (die, type, cu);
17654 }
17655
17656 /* Read a single die and all its descendents. Set the die's sibling
17657 field to NULL; set other fields in the die correctly, and set all
17658 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17659 location of the info_ptr after reading all of those dies. PARENT
17660 is the parent of the die in question. */
17661
17662 static struct die_info *
17663 read_die_and_children (const struct die_reader_specs *reader,
17664 const gdb_byte *info_ptr,
17665 const gdb_byte **new_info_ptr,
17666 struct die_info *parent)
17667 {
17668 struct die_info *die;
17669 const gdb_byte *cur_ptr;
17670
17671 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17672 if (die == NULL)
17673 {
17674 *new_info_ptr = cur_ptr;
17675 return NULL;
17676 }
17677 store_in_ref_table (die, reader->cu);
17678
17679 if (die->has_children)
17680 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17681 else
17682 {
17683 die->child = NULL;
17684 *new_info_ptr = cur_ptr;
17685 }
17686
17687 die->sibling = NULL;
17688 die->parent = parent;
17689 return die;
17690 }
17691
17692 /* Read a die, all of its descendents, and all of its siblings; set
17693 all of the fields of all of the dies correctly. Arguments are as
17694 in read_die_and_children. */
17695
17696 static struct die_info *
17697 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17698 const gdb_byte *info_ptr,
17699 const gdb_byte **new_info_ptr,
17700 struct die_info *parent)
17701 {
17702 struct die_info *first_die, *last_sibling;
17703 const gdb_byte *cur_ptr;
17704
17705 cur_ptr = info_ptr;
17706 first_die = last_sibling = NULL;
17707
17708 while (1)
17709 {
17710 struct die_info *die
17711 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17712
17713 if (die == NULL)
17714 {
17715 *new_info_ptr = cur_ptr;
17716 return first_die;
17717 }
17718
17719 if (!first_die)
17720 first_die = die;
17721 else
17722 last_sibling->sibling = die;
17723
17724 last_sibling = die;
17725 }
17726 }
17727
17728 /* Read a die, all of its descendents, and all of its siblings; set
17729 all of the fields of all of the dies correctly. Arguments are as
17730 in read_die_and_children.
17731 This the main entry point for reading a DIE and all its children. */
17732
17733 static struct die_info *
17734 read_die_and_siblings (const struct die_reader_specs *reader,
17735 const gdb_byte *info_ptr,
17736 const gdb_byte **new_info_ptr,
17737 struct die_info *parent)
17738 {
17739 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17740 new_info_ptr, parent);
17741
17742 if (dwarf_die_debug)
17743 {
17744 gdb_printf (gdb_stdlog,
17745 "Read die from %s@0x%x of %s:\n",
17746 reader->die_section->get_name (),
17747 (unsigned) (info_ptr - reader->die_section->buffer),
17748 bfd_get_filename (reader->abfd));
17749 dump_die (die, dwarf_die_debug);
17750 }
17751
17752 return die;
17753 }
17754
17755 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17756 attributes.
17757 The caller is responsible for filling in the extra attributes
17758 and updating (*DIEP)->num_attrs.
17759 Set DIEP to point to a newly allocated die with its information,
17760 except for its child, sibling, and parent fields. */
17761
17762 static const gdb_byte *
17763 read_full_die_1 (const struct die_reader_specs *reader,
17764 struct die_info **diep, const gdb_byte *info_ptr,
17765 int num_extra_attrs)
17766 {
17767 unsigned int abbrev_number, bytes_read, i;
17768 const struct abbrev_info *abbrev;
17769 struct die_info *die;
17770 struct dwarf2_cu *cu = reader->cu;
17771 bfd *abfd = reader->abfd;
17772
17773 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17774 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17775 info_ptr += bytes_read;
17776 if (!abbrev_number)
17777 {
17778 *diep = NULL;
17779 return info_ptr;
17780 }
17781
17782 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17783 if (!abbrev)
17784 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17785 abbrev_number,
17786 bfd_get_filename (abfd));
17787
17788 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17789 die->sect_off = sect_off;
17790 die->tag = abbrev->tag;
17791 die->abbrev = abbrev_number;
17792 die->has_children = abbrev->has_children;
17793
17794 /* Make the result usable.
17795 The caller needs to update num_attrs after adding the extra
17796 attributes. */
17797 die->num_attrs = abbrev->num_attrs;
17798
17799 bool any_need_reprocess = false;
17800 for (i = 0; i < abbrev->num_attrs; ++i)
17801 {
17802 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17803 info_ptr);
17804 if (die->attrs[i].requires_reprocessing_p ())
17805 any_need_reprocess = true;
17806 }
17807
17808 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17809 if (attr != nullptr && attr->form_is_unsigned ())
17810 cu->str_offsets_base = attr->as_unsigned ();
17811
17812 attr = die->attr (DW_AT_loclists_base);
17813 if (attr != nullptr)
17814 cu->loclist_base = attr->as_unsigned ();
17815
17816 auto maybe_addr_base = die->addr_base ();
17817 if (maybe_addr_base.has_value ())
17818 cu->addr_base = *maybe_addr_base;
17819
17820 attr = die->attr (DW_AT_rnglists_base);
17821 if (attr != nullptr)
17822 cu->rnglists_base = attr->as_unsigned ();
17823
17824 if (any_need_reprocess)
17825 {
17826 for (i = 0; i < abbrev->num_attrs; ++i)
17827 {
17828 if (die->attrs[i].requires_reprocessing_p ())
17829 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17830 }
17831 }
17832 *diep = die;
17833 return info_ptr;
17834 }
17835
17836 /* Read a die and all its attributes.
17837 Set DIEP to point to a newly allocated die with its information,
17838 except for its child, sibling, and parent fields. */
17839
17840 static const gdb_byte *
17841 read_full_die (const struct die_reader_specs *reader,
17842 struct die_info **diep, const gdb_byte *info_ptr)
17843 {
17844 const gdb_byte *result;
17845
17846 result = read_full_die_1 (reader, diep, info_ptr, 0);
17847
17848 if (dwarf_die_debug)
17849 {
17850 gdb_printf (gdb_stdlog,
17851 "Read die from %s@0x%x of %s:\n",
17852 reader->die_section->get_name (),
17853 (unsigned) (info_ptr - reader->die_section->buffer),
17854 bfd_get_filename (reader->abfd));
17855 dump_die (*diep, dwarf_die_debug);
17856 }
17857
17858 return result;
17859 }
17860 \f
17861
17862 void
17863 cooked_indexer::check_bounds (cutu_reader *reader)
17864 {
17865 if (reader->cu->per_cu->addresses_seen)
17866 return;
17867
17868 dwarf2_cu *cu = reader->cu;
17869
17870 CORE_ADDR best_lowpc = 0, best_highpc = 0;
17871 /* Possibly set the default values of LOWPC and HIGHPC from
17872 `DW_AT_ranges'. */
17873 dwarf2_find_base_address (reader->comp_unit_die, cu);
17874 enum pc_bounds_kind cu_bounds_kind
17875 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
17876 cu, m_index_storage->get_addrmap (), cu->per_cu);
17877 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
17878 {
17879 struct objfile *objfile = cu->per_objfile->objfile;
17880 CORE_ADDR baseaddr = objfile->text_section_offset ();
17881 struct gdbarch *gdbarch = objfile->arch ();
17882 CORE_ADDR low
17883 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
17884 - baseaddr);
17885 CORE_ADDR high
17886 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
17887 - baseaddr - 1);
17888 /* Store the contiguous range if it is not empty; it can be
17889 empty for CUs with no code. */
17890 addrmap_set_empty (m_index_storage->get_addrmap (), low, high,
17891 cu->per_cu);
17892
17893 cu->per_cu->addresses_seen = true;
17894 }
17895 }
17896
17897 /* Helper function that returns true if TAG can have a linkage
17898 name. */
17899
17900 static bool
17901 tag_can_have_linkage_name (enum dwarf_tag tag)
17902 {
17903 switch (tag)
17904 {
17905 /* We include types here because an anonymous C++ type might
17906 have a name for linkage purposes. */
17907 case DW_TAG_class_type:
17908 case DW_TAG_structure_type:
17909 case DW_TAG_union_type:
17910 case DW_TAG_variable:
17911 case DW_TAG_subprogram:
17912 return true;
17913
17914 default:
17915 return false;
17916 }
17917 }
17918
17919 cutu_reader *
17920 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
17921 dwarf2_per_objfile *per_objfile,
17922 sect_offset sect_off, bool is_dwz,
17923 bool for_scanning)
17924 {
17925 /* Lookups for type unit references are always in the CU, and
17926 cross-CU references will crash. */
17927 if (reader->cu->per_cu->is_dwz == is_dwz
17928 && reader->cu->header.offset_in_cu_p (sect_off))
17929 return reader;
17930
17931 dwarf2_per_cu_data *per_cu
17932 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
17933 per_objfile->per_bfd);
17934
17935 /* When scanning, we only want to visit a given CU a single time.
17936 Doing this check here avoids self-imports as well. */
17937 if (for_scanning)
17938 {
17939 bool nope = false;
17940 if (!per_cu->scanned.compare_exchange_strong (nope, true))
17941 return nullptr;
17942 }
17943 if (per_cu == m_per_cu)
17944 return reader;
17945
17946 cutu_reader *result = m_index_storage->get_reader (per_cu);
17947 if (result == nullptr)
17948 {
17949 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
17950 m_index_storage->get_abbrev_cache ());
17951
17952 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
17953 language_minimal);
17954 std::unique_ptr<cutu_reader> copy
17955 (new cutu_reader (std::move (new_reader)));
17956 result = m_index_storage->preserve (std::move (copy));
17957 }
17958
17959 if (result->dummy_p || !result->comp_unit_die->has_children)
17960 return nullptr;
17961
17962 if (for_scanning)
17963 check_bounds (result);
17964
17965 return result;
17966 }
17967
17968 const gdb_byte *
17969 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
17970 cutu_reader *reader,
17971 const gdb_byte *watermark_ptr,
17972 const gdb_byte *info_ptr,
17973 const abbrev_info *abbrev,
17974 const char **name,
17975 const char **linkage_name,
17976 cooked_index_flag *flags,
17977 sect_offset *sibling_offset,
17978 const cooked_index_entry **parent_entry,
17979 CORE_ADDR *maybe_defer,
17980 bool for_specification)
17981 {
17982 bool origin_is_dwz = false;
17983 bool is_declaration = false;
17984 sect_offset origin_offset {};
17985
17986 gdb::optional<CORE_ADDR> low_pc;
17987 gdb::optional<CORE_ADDR> high_pc;
17988 bool high_pc_relative = false;
17989
17990 for (int i = 0; i < abbrev->num_attrs; ++i)
17991 {
17992 attribute attr;
17993 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17994 if (attr.requires_reprocessing_p ())
17995 read_attribute_reprocess (reader, &attr, abbrev->tag);
17996
17997 /* Store the data if it is of an attribute we want to keep in a
17998 partial symbol table. */
17999 switch (attr.name)
18000 {
18001 case DW_AT_name:
18002 switch (abbrev->tag)
18003 {
18004 case DW_TAG_compile_unit:
18005 case DW_TAG_partial_unit:
18006 case DW_TAG_type_unit:
18007 /* Compilation units have a DW_AT_name that is a filename, not
18008 a source language identifier. */
18009 break;
18010
18011 default:
18012 if (*name == nullptr)
18013 *name = attr.as_string ();
18014 break;
18015 }
18016 break;
18017
18018 case DW_AT_linkage_name:
18019 case DW_AT_MIPS_linkage_name:
18020 /* Note that both forms of linkage name might appear. We
18021 assume they will be the same, and we only store the last
18022 one we see. */
18023 if (*linkage_name == nullptr)
18024 *linkage_name = attr.as_string ();
18025 break;
18026
18027 case DW_AT_main_subprogram:
18028 if (attr.as_boolean ())
18029 *flags |= IS_MAIN;
18030 break;
18031
18032 case DW_AT_declaration:
18033 is_declaration = attr.as_boolean ();
18034 break;
18035
18036 case DW_AT_sibling:
18037 if (sibling_offset != nullptr)
18038 *sibling_offset = attr.get_ref_die_offset ();
18039 break;
18040
18041 case DW_AT_specification:
18042 case DW_AT_abstract_origin:
18043 case DW_AT_extension:
18044 origin_offset = attr.get_ref_die_offset ();
18045 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18046 break;
18047
18048 case DW_AT_external:
18049 if (attr.as_boolean ())
18050 *flags &= ~IS_STATIC;
18051 break;
18052
18053 case DW_AT_enum_class:
18054 if (attr.as_boolean ())
18055 *flags |= IS_ENUM_CLASS;
18056 break;
18057
18058 case DW_AT_low_pc:
18059 low_pc = attr.as_address ();
18060 break;
18061
18062 case DW_AT_high_pc:
18063 high_pc = attr.as_address ();
18064 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18065 high_pc_relative = true;
18066 break;
18067
18068 case DW_AT_location:
18069 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18070 {
18071 struct dwarf_block *locdesc = attr.as_block ();
18072 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18073 if (addr != 0
18074 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18075 {
18076 low_pc = addr;
18077 /* For variables, we don't want to try decoding the
18078 type just to find the size -- for gdb's purposes
18079 we only need the address of a variable. */
18080 high_pc = addr + 1;
18081 high_pc_relative = false;
18082 }
18083 }
18084 break;
18085
18086 case DW_AT_ranges:
18087 if (!scanning_per_cu->addresses_seen)
18088 {
18089 /* Offset in the .debug_ranges or .debug_rnglist section
18090 (depending on DWARF version). */
18091 ULONGEST ranges_offset = attr.as_unsigned ();
18092
18093 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18094 want to add this value. */
18095 ranges_offset += reader->cu->gnu_ranges_base;
18096
18097 CORE_ADDR lowpc, highpc;
18098 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18099 m_index_storage->get_addrmap (),
18100 scanning_per_cu, abbrev->tag);
18101 }
18102 break;
18103 }
18104 }
18105
18106 /* We don't want to examine declarations, but if we found a
18107 declaration when handling DW_AT_specification or the like, then
18108 that is ok. Similarly, we allow an external variable without a
18109 location; those are resolved via minimal symbols. */
18110 if (is_declaration && !for_specification
18111 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0)
18112 && !((abbrev->tag == DW_TAG_class_type
18113 || abbrev->tag == DW_TAG_structure_type
18114 || abbrev->tag == DW_TAG_union_type)
18115 && abbrev->has_children))
18116 {
18117 *linkage_name = nullptr;
18118 *name = nullptr;
18119 }
18120 else if ((*name == nullptr
18121 || (*linkage_name == nullptr
18122 && tag_can_have_linkage_name (abbrev->tag))
18123 || (*parent_entry == nullptr && m_language != language_c))
18124 && origin_offset != sect_offset (0))
18125 {
18126 cutu_reader *new_reader
18127 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18128 origin_is_dwz, false);
18129 if (new_reader != nullptr)
18130 {
18131 const gdb_byte *new_info_ptr = (new_reader->buffer
18132 + to_underlying (origin_offset));
18133
18134 if (new_reader->cu == reader->cu
18135 && new_info_ptr > watermark_ptr
18136 && maybe_defer != nullptr
18137 && *parent_entry == nullptr)
18138 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18139 else if (*parent_entry == nullptr)
18140 {
18141 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18142 *parent_entry
18143 = (cooked_index_entry *) addrmap_find (m_die_range_map,
18144 lookup);
18145 }
18146
18147 unsigned int bytes_read;
18148 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18149 new_info_ptr,
18150 &bytes_read);
18151 new_info_ptr += bytes_read;
18152 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18153 new_abbrev, name, linkage_name, flags, nullptr,
18154 parent_entry, maybe_defer, true);
18155 }
18156 }
18157
18158 if (!for_specification)
18159 {
18160 if (m_language == language_ada
18161 && *linkage_name == nullptr)
18162 *linkage_name = *name;
18163
18164 if (!scanning_per_cu->addresses_seen
18165 && low_pc.has_value ()
18166 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18167 || *low_pc != 0)
18168 && high_pc.has_value ())
18169 {
18170 if (high_pc_relative)
18171 high_pc = *high_pc + *low_pc;
18172
18173 if (*high_pc > *low_pc)
18174 {
18175 struct objfile *objfile = reader->cu->per_objfile->objfile;
18176 CORE_ADDR baseaddr = objfile->text_section_offset ();
18177 struct gdbarch *gdbarch = objfile->arch ();
18178 CORE_ADDR lo
18179 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18180 - baseaddr);
18181 CORE_ADDR hi
18182 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18183 - baseaddr);
18184 addrmap_set_empty (m_index_storage->get_addrmap (), lo, hi - 1,
18185 scanning_per_cu);
18186 }
18187 }
18188
18189 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18190 *flags &= ~IS_STATIC;
18191
18192 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18193 *name = "(anonymous namespace)";
18194
18195 if (m_language == language_cplus
18196 && (abbrev->tag == DW_TAG_class_type
18197 || abbrev->tag == DW_TAG_interface_type
18198 || abbrev->tag == DW_TAG_structure_type
18199 || abbrev->tag == DW_TAG_union_type
18200 || abbrev->tag == DW_TAG_enumeration_type
18201 || abbrev->tag == DW_TAG_enumerator))
18202 *flags &= ~IS_STATIC;
18203 }
18204
18205 return info_ptr;
18206 }
18207
18208 const gdb_byte *
18209 cooked_indexer::index_imported_unit (cutu_reader *reader,
18210 const gdb_byte *info_ptr,
18211 const abbrev_info *abbrev)
18212 {
18213 sect_offset sect_off {};
18214 bool is_dwz = false;
18215
18216 for (int i = 0; i < abbrev->num_attrs; ++i)
18217 {
18218 /* Note that we never need to reprocess attributes here. */
18219 attribute attr;
18220 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18221
18222 if (attr.name == DW_AT_import)
18223 {
18224 sect_off = attr.get_ref_die_offset ();
18225 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18226 || reader->cu->per_cu->is_dwz);
18227 }
18228 }
18229
18230 /* Did not find DW_AT_import. */
18231 if (sect_off == sect_offset (0))
18232 return info_ptr;
18233
18234 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18235 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18236 is_dwz, true);
18237 if (new_reader != nullptr)
18238 {
18239 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18240
18241 reader->cu->add_dependence (new_reader->cu->per_cu);
18242 }
18243
18244 return info_ptr;
18245 }
18246
18247 const gdb_byte *
18248 cooked_indexer::recurse (cutu_reader *reader,
18249 const gdb_byte *info_ptr,
18250 const cooked_index_entry *parent_entry,
18251 bool fully)
18252 {
18253 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18254
18255 if (parent_entry != nullptr)
18256 {
18257 CORE_ADDR start = form_addr (parent_entry->die_offset,
18258 reader->cu->per_cu->is_dwz);
18259 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18260 reader->cu->per_cu->is_dwz);
18261 addrmap_set_empty (m_die_range_map, start, end, (void *) parent_entry);
18262 }
18263
18264 return info_ptr;
18265 }
18266
18267 const gdb_byte *
18268 cooked_indexer::index_dies (cutu_reader *reader,
18269 const gdb_byte *info_ptr,
18270 const cooked_index_entry *parent_entry,
18271 bool fully)
18272 {
18273 const gdb_byte *end_ptr = info_ptr + reader->cu->header.get_length ();
18274
18275 while (info_ptr < end_ptr)
18276 {
18277 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18278 unsigned int bytes_read;
18279 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18280 &bytes_read);
18281 info_ptr += bytes_read;
18282 if (abbrev == nullptr)
18283 break;
18284
18285 if (abbrev->tag == DW_TAG_imported_unit)
18286 {
18287 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18288 continue;
18289 }
18290
18291 if (!abbrev->interesting)
18292 {
18293 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18294 if (fully && abbrev->has_children)
18295 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18296 continue;
18297 }
18298
18299 const char *name = nullptr;
18300 const char *linkage_name = nullptr;
18301 CORE_ADDR defer = 0;
18302 cooked_index_flag flags = IS_STATIC;
18303 sect_offset sibling {};
18304 const cooked_index_entry *this_parent_entry = parent_entry;
18305 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18306 info_ptr, abbrev, &name, &linkage_name,
18307 &flags, &sibling, &this_parent_entry,
18308 &defer, false);
18309
18310 if (abbrev->tag == DW_TAG_namespace
18311 && m_language == language_cplus
18312 && strcmp (name, "::") == 0)
18313 {
18314 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18315 generated bogus DW_TAG_namespace DIEs with a name of "::"
18316 for the global namespace. Work around this problem
18317 here. */
18318 name = nullptr;
18319 }
18320
18321 const cooked_index_entry *this_entry = nullptr;
18322 if (name != nullptr)
18323 {
18324 if (defer != 0)
18325 m_deferred_entries.push_back ({
18326 this_die, name, defer, abbrev->tag, flags
18327 });
18328 else
18329 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18330 name, this_parent_entry,
18331 m_per_cu);
18332 }
18333
18334 if (linkage_name != nullptr)
18335 {
18336 /* We only want this to be "main" if it has a linkage name
18337 but not an ordinary name. */
18338 if (name != nullptr)
18339 flags = flags & ~IS_MAIN;
18340 /* Set the IS_LINKAGE on for everything except when functions
18341 have linkage name present but name is absent. */
18342 if (name != nullptr
18343 || (abbrev->tag != DW_TAG_subprogram
18344 && abbrev->tag != DW_TAG_inlined_subroutine
18345 && abbrev->tag != DW_TAG_entry_point))
18346 flags = flags | IS_LINKAGE;
18347 m_index_storage->add (this_die, abbrev->tag, flags,
18348 linkage_name, nullptr, m_per_cu);
18349 }
18350
18351 if (abbrev->has_children)
18352 {
18353 switch (abbrev->tag)
18354 {
18355 case DW_TAG_class_type:
18356 case DW_TAG_interface_type:
18357 case DW_TAG_structure_type:
18358 case DW_TAG_union_type:
18359 if (m_language != language_c && this_entry != nullptr)
18360 {
18361 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18362 continue;
18363 }
18364 break;
18365
18366 case DW_TAG_enumeration_type:
18367 /* We need to recurse even for an anonymous enumeration.
18368 Which scope we record as the parent scope depends on
18369 whether we're reading an "enum class". If so, we use
18370 the enum itself as the parent, yielding names like
18371 "enum_class::enumerator"; otherwise we inject the
18372 names into our own parent scope. */
18373 info_ptr = recurse (reader, info_ptr,
18374 ((flags & IS_ENUM_CLASS) == 0)
18375 ? parent_entry
18376 : this_entry,
18377 fully);
18378 continue;
18379
18380 case DW_TAG_module:
18381 if (this_entry == nullptr)
18382 break;
18383 /* FALLTHROUGH */
18384 case DW_TAG_namespace:
18385 /* We don't check THIS_ENTRY for a namespace, to handle
18386 the ancient G++ workaround pointed out above. */
18387 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18388 continue;
18389
18390 case DW_TAG_subprogram:
18391 if ((m_language == language_fortran
18392 || m_language == language_ada)
18393 && this_entry != nullptr)
18394 {
18395 info_ptr = recurse (reader, info_ptr, this_entry, true);
18396 continue;
18397 }
18398 break;
18399 }
18400
18401 if (sibling != sect_offset (0))
18402 {
18403 const gdb_byte *sibling_ptr
18404 = reader->buffer + to_underlying (sibling);
18405
18406 if (sibling_ptr < info_ptr)
18407 complaint (_("DW_AT_sibling points backwards"));
18408 else if (sibling_ptr > reader->buffer_end)
18409 reader->die_section->overflow_complaint ();
18410 else
18411 info_ptr = sibling_ptr;
18412 }
18413 else
18414 info_ptr = skip_children (reader, info_ptr);
18415 }
18416 }
18417
18418 return info_ptr;
18419 }
18420
18421 void
18422 cooked_indexer::make_index (cutu_reader *reader)
18423 {
18424 check_bounds (reader);
18425 find_file_and_directory (reader->comp_unit_die, reader->cu);
18426 if (!reader->comp_unit_die->has_children)
18427 return;
18428 index_dies (reader, reader->info_ptr, nullptr, false);
18429
18430 for (const auto &entry : m_deferred_entries)
18431 {
18432 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18433 cooked_index_entry *parent
18434 = (cooked_index_entry *) addrmap_find (m_die_range_map, key);
18435 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18436 entry.name, parent, m_per_cu);
18437 }
18438 }
18439
18440 /* An implementation of quick_symbol_functions for the cooked DWARF
18441 index. */
18442
18443 struct cooked_index_functions : public dwarf2_base_index_functions
18444 {
18445 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
18446 CORE_ADDR adjusted_pc) override;
18447
18448 struct compunit_symtab *find_compunit_symtab_by_address
18449 (struct objfile *objfile, CORE_ADDR address) override;
18450
18451 void dump (struct objfile *objfile) override
18452 {
18453 gdb_printf ("Cooked index in use\n");
18454 }
18455
18456 void expand_matching_symbols
18457 (struct objfile *,
18458 const lookup_name_info &lookup_name,
18459 domain_enum domain,
18460 int global,
18461 symbol_compare_ftype *ordered_compare) override;
18462
18463 bool expand_symtabs_matching
18464 (struct objfile *objfile,
18465 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18466 const lookup_name_info *lookup_name,
18467 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18468 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18469 block_search_flags search_flags,
18470 domain_enum domain,
18471 enum search_domain kind) override;
18472
18473 bool can_lazily_read_symbols () override
18474 {
18475 return true;
18476 }
18477
18478 void read_partial_symbols (struct objfile *objfile) override
18479 {
18480 if (dwarf2_has_info (objfile, nullptr))
18481 dwarf2_build_psymtabs (objfile);
18482 }
18483 };
18484
18485 dwarf2_per_cu_data *
18486 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
18487 CORE_ADDR adjusted_pc)
18488 {
18489 cooked_index_vector *table
18490 = (static_cast<cooked_index_vector *>
18491 (per_bfd->index_table.get ()));
18492 if (table == nullptr)
18493 return nullptr;
18494 return table->lookup (adjusted_pc);
18495 }
18496
18497 struct compunit_symtab *
18498 cooked_index_functions::find_compunit_symtab_by_address
18499 (struct objfile *objfile, CORE_ADDR address)
18500 {
18501 if (objfile->sect_index_data == -1)
18502 return nullptr;
18503
18504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18505 if (per_objfile->per_bfd->index_table == nullptr)
18506 return nullptr;
18507
18508 CORE_ADDR baseaddr = objfile->data_section_offset ();
18509 cooked_index_vector *table
18510 = (static_cast<cooked_index_vector *>
18511 (per_objfile->per_bfd->index_table.get ()));
18512 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18513 if (per_cu == nullptr)
18514 return nullptr;
18515
18516 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18517 }
18518
18519 void
18520 cooked_index_functions::expand_matching_symbols
18521 (struct objfile *objfile,
18522 const lookup_name_info &lookup_name,
18523 domain_enum domain,
18524 int global,
18525 symbol_compare_ftype *ordered_compare)
18526 {
18527 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18528 if (per_objfile->per_bfd->index_table == nullptr)
18529 return;
18530 const block_search_flags search_flags = (global
18531 ? SEARCH_GLOBAL_BLOCK
18532 : SEARCH_STATIC_BLOCK);
18533 const language_defn *lang = language_def (language_ada);
18534 symbol_name_matcher_ftype *name_match
18535 = lang->get_symbol_name_matcher (lookup_name);
18536
18537 cooked_index_vector *table
18538 = (static_cast<cooked_index_vector *>
18539 (per_objfile->per_bfd->index_table.get ()));
18540 for (const cooked_index_entry *entry : table->all_entries ())
18541 {
18542 if (entry->parent_entry != nullptr)
18543 continue;
18544
18545 if (!entry->matches (search_flags)
18546 || !entry->matches (domain))
18547 continue;
18548
18549 if (name_match (entry->canonical, lookup_name, nullptr))
18550 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18551 }
18552 }
18553
18554 bool
18555 cooked_index_functions::expand_symtabs_matching
18556 (struct objfile *objfile,
18557 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18558 const lookup_name_info *lookup_name,
18559 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18560 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18561 block_search_flags search_flags,
18562 domain_enum domain,
18563 enum search_domain kind)
18564 {
18565 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18566 if (per_objfile->per_bfd->index_table == nullptr)
18567 return true;
18568
18569 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18570
18571 /* This invariant is documented in quick-functions.h. */
18572 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18573 if (lookup_name == nullptr)
18574 {
18575 for (dwarf2_per_cu_data *per_cu
18576 : all_comp_units_range (per_objfile->per_bfd))
18577 {
18578 QUIT;
18579
18580 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18581 file_matcher,
18582 expansion_notify))
18583 return false;
18584 }
18585 return true;
18586 }
18587
18588 lookup_name_info lookup_name_without_params
18589 = lookup_name->make_ignore_params ();
18590 bool completing = lookup_name->completion_mode ();
18591
18592 /* Unique styles of language splitting. */
18593 static const enum language unique_styles[] =
18594 {
18595 /* No splitting is also a style. */
18596 language_c,
18597 /* This includes Rust. */
18598 language_cplus,
18599 /* This includes Go. */
18600 language_d,
18601 language_ada
18602 };
18603
18604 cooked_index_vector *table
18605 = (static_cast<cooked_index_vector *>
18606 (per_objfile->per_bfd->index_table.get ()));
18607 for (enum language lang : unique_styles)
18608 {
18609 std::vector<gdb::string_view> name_vec
18610 = lookup_name_without_params.split_name (lang);
18611
18612 for (const cooked_index_entry *entry : table->find (name_vec.back (),
18613 completing))
18614 {
18615 /* No need to consider symbols from expanded CUs. */
18616 if (per_objfile->symtab_set_p (entry->per_cu))
18617 continue;
18618
18619 /* If file-matching was done, we don't need to consider
18620 symbols from unmarked CUs. */
18621 if (file_matcher != nullptr && !entry->per_cu->mark)
18622 continue;
18623
18624 /* See if the symbol matches the type filter. */
18625 if (!entry->matches (search_flags)
18626 || !entry->matches (domain)
18627 || !entry->matches (kind))
18628 continue;
18629
18630 /* We've found the base name of the symbol; now walk its
18631 parentage chain, ensuring that each component
18632 matches. */
18633 bool found = true;
18634
18635 const cooked_index_entry *parent = entry->parent_entry;
18636 for (int i = name_vec.size () - 1; i > 0; --i)
18637 {
18638 /* If we ran out of entries, or if this segment doesn't
18639 match, this did not match. */
18640 if (parent == nullptr
18641 || strncmp (parent->name, name_vec[i - 1].data (),
18642 name_vec[i - 1].length ()) != 0)
18643 {
18644 found = false;
18645 break;
18646 }
18647
18648 parent = parent->parent_entry;
18649 }
18650
18651 if (!found)
18652 continue;
18653
18654 /* Might have been looking for "a::b" and found
18655 "x::a::b". */
18656 if (symbol_matcher == nullptr)
18657 {
18658 symbol_name_match_type match_type
18659 = lookup_name_without_params.match_type ();
18660 if ((match_type == symbol_name_match_type::FULL
18661 || (lang != language_ada
18662 && match_type == symbol_name_match_type::EXPRESSION))
18663 && parent != nullptr)
18664 continue;
18665 }
18666 else
18667 {
18668 auto_obstack temp_storage;
18669 const char *full_name = entry->full_name (&temp_storage);
18670 if (!symbol_matcher (full_name))
18671 continue;
18672 }
18673
18674 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18675 file_matcher,
18676 expansion_notify))
18677 return false;
18678 }
18679 }
18680
18681 return true;
18682 }
18683
18684 /* Return a new cooked_index_functions object. */
18685
18686 static quick_symbol_functions_up
18687 make_cooked_index_funcs ()
18688 {
18689 return quick_symbol_functions_up (new cooked_index_functions);
18690 }
18691
18692 quick_symbol_functions_up
18693 cooked_index_vector::make_quick_functions () const
18694 {
18695 return make_cooked_index_funcs ();
18696 }
18697
18698 \f
18699
18700 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18701 contents from the given SECTION in the HEADER.
18702
18703 HEADER_OFFSET is the offset of the header in the section. */
18704 static void
18705 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18706 struct dwarf2_section_info *section,
18707 sect_offset header_offset)
18708 {
18709 unsigned int bytes_read;
18710 bfd *abfd = section->get_bfd_owner ();
18711 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18712
18713 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18714 info_ptr += bytes_read;
18715
18716 header->version = read_2_bytes (abfd, info_ptr);
18717 info_ptr += 2;
18718
18719 header->addr_size = read_1_byte (abfd, info_ptr);
18720 info_ptr += 1;
18721
18722 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18723 info_ptr += 1;
18724
18725 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18726 }
18727
18728 /* Return the DW_AT_loclists_base value for the CU. */
18729 static ULONGEST
18730 lookup_loclist_base (struct dwarf2_cu *cu)
18731 {
18732 /* For the .dwo unit, the loclist_base points to the first offset following
18733 the header. The header consists of the following entities-
18734 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18735 bit format)
18736 2. version (2 bytes)
18737 3. address size (1 byte)
18738 4. segment selector size (1 byte)
18739 5. offset entry count (4 bytes)
18740 These sizes are derived as per the DWARFv5 standard. */
18741 if (cu->dwo_unit != nullptr)
18742 {
18743 if (cu->header.initial_length_size == 4)
18744 return LOCLIST_HEADER_SIZE32;
18745 return LOCLIST_HEADER_SIZE64;
18746 }
18747 return cu->loclist_base;
18748 }
18749
18750 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18751 array of offsets in the .debug_loclists section. */
18752
18753 static sect_offset
18754 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18755 {
18756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18757 struct objfile *objfile = per_objfile->objfile;
18758 bfd *abfd = objfile->obfd;
18759 ULONGEST loclist_header_size =
18760 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18761 : LOCLIST_HEADER_SIZE64);
18762 ULONGEST loclist_base = lookup_loclist_base (cu);
18763
18764 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18765 ULONGEST start_offset =
18766 loclist_base + loclist_index * cu->header.offset_size;
18767
18768 /* Get loclists section. */
18769 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18770
18771 /* Read the loclists section content. */
18772 section->read (objfile);
18773 if (section->buffer == NULL)
18774 error (_("DW_FORM_loclistx used without .debug_loclists "
18775 "section [in module %s]"), objfile_name (objfile));
18776
18777 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18778 so if loclist_base is smaller than the header size, we have a problem. */
18779 if (loclist_base < loclist_header_size)
18780 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18781 objfile_name (objfile));
18782
18783 /* Read the header of the loclists contribution. */
18784 struct loclists_rnglists_header header;
18785 read_loclists_rnglists_header (&header, section,
18786 (sect_offset) (loclist_base - loclist_header_size));
18787
18788 /* Verify the loclist index is valid. */
18789 if (loclist_index >= header.offset_entry_count)
18790 error (_("DW_FORM_loclistx pointing outside of "
18791 ".debug_loclists offset array [in module %s]"),
18792 objfile_name (objfile));
18793
18794 /* Validate that reading won't go beyond the end of the section. */
18795 if (start_offset + cu->header.offset_size > section->size)
18796 error (_("Reading DW_FORM_loclistx index beyond end of"
18797 ".debug_loclists section [in module %s]"),
18798 objfile_name (objfile));
18799
18800 const gdb_byte *info_ptr = section->buffer + start_offset;
18801
18802 if (cu->header.offset_size == 4)
18803 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18804 else
18805 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18806 }
18807
18808 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18809 array of offsets in the .debug_rnglists section. */
18810
18811 static sect_offset
18812 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18813 dwarf_tag tag)
18814 {
18815 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18816 struct objfile *objfile = dwarf2_per_objfile->objfile;
18817 bfd *abfd = objfile->obfd;
18818 ULONGEST rnglist_header_size =
18819 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18820 : RNGLIST_HEADER_SIZE64);
18821
18822 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18823 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18824 doesn't apply. */
18825 ULONGEST rnglist_base =
18826 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18827
18828 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18829 ULONGEST start_offset =
18830 rnglist_base + rnglist_index * cu->header.offset_size;
18831
18832 /* Get rnglists section. */
18833 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18834
18835 /* Read the rnglists section content. */
18836 section->read (objfile);
18837 if (section->buffer == nullptr)
18838 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18839 "[in module %s]"),
18840 objfile_name (objfile));
18841
18842 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18843 so if rnglist_base is smaller than the header size, we have a problem. */
18844 if (rnglist_base < rnglist_header_size)
18845 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18846 objfile_name (objfile));
18847
18848 /* Read the header of the rnglists contribution. */
18849 struct loclists_rnglists_header header;
18850 read_loclists_rnglists_header (&header, section,
18851 (sect_offset) (rnglist_base - rnglist_header_size));
18852
18853 /* Verify the rnglist index is valid. */
18854 if (rnglist_index >= header.offset_entry_count)
18855 error (_("DW_FORM_rnglistx index pointing outside of "
18856 ".debug_rnglists offset array [in module %s]"),
18857 objfile_name (objfile));
18858
18859 /* Validate that reading won't go beyond the end of the section. */
18860 if (start_offset + cu->header.offset_size > section->size)
18861 error (_("Reading DW_FORM_rnglistx index beyond end of"
18862 ".debug_rnglists section [in module %s]"),
18863 objfile_name (objfile));
18864
18865 const gdb_byte *info_ptr = section->buffer + start_offset;
18866
18867 if (cu->header.offset_size == 4)
18868 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
18869 else
18870 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
18871 }
18872
18873 /* Process the attributes that had to be skipped in the first round. These
18874 attributes are the ones that need str_offsets_base or addr_base attributes.
18875 They could not have been processed in the first round, because at the time
18876 the values of str_offsets_base or addr_base may not have been known. */
18877 static void
18878 read_attribute_reprocess (const struct die_reader_specs *reader,
18879 struct attribute *attr, dwarf_tag tag)
18880 {
18881 struct dwarf2_cu *cu = reader->cu;
18882 switch (attr->form)
18883 {
18884 case DW_FORM_addrx:
18885 case DW_FORM_GNU_addr_index:
18886 attr->set_address (read_addr_index (cu,
18887 attr->as_unsigned_reprocess ()));
18888 break;
18889 case DW_FORM_loclistx:
18890 {
18891 sect_offset loclists_sect_off
18892 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
18893
18894 attr->set_unsigned (to_underlying (loclists_sect_off));
18895 }
18896 break;
18897 case DW_FORM_rnglistx:
18898 {
18899 sect_offset rnglists_sect_off
18900 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
18901
18902 attr->set_unsigned (to_underlying (rnglists_sect_off));
18903 }
18904 break;
18905 case DW_FORM_strx:
18906 case DW_FORM_strx1:
18907 case DW_FORM_strx2:
18908 case DW_FORM_strx3:
18909 case DW_FORM_strx4:
18910 case DW_FORM_GNU_str_index:
18911 {
18912 unsigned int str_index = attr->as_unsigned_reprocess ();
18913 gdb_assert (!attr->canonical_string_p ());
18914 if (reader->dwo_file != NULL)
18915 attr->set_string_noncanonical (read_dwo_str_index (reader,
18916 str_index));
18917 else
18918 attr->set_string_noncanonical (read_stub_str_index (cu,
18919 str_index));
18920 break;
18921 }
18922 default:
18923 gdb_assert_not_reached ("Unexpected DWARF form.");
18924 }
18925 }
18926
18927 /* Read an attribute value described by an attribute form. */
18928
18929 static const gdb_byte *
18930 read_attribute_value (const struct die_reader_specs *reader,
18931 struct attribute *attr, unsigned form,
18932 LONGEST implicit_const, const gdb_byte *info_ptr)
18933 {
18934 struct dwarf2_cu *cu = reader->cu;
18935 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18936 struct objfile *objfile = per_objfile->objfile;
18937 bfd *abfd = reader->abfd;
18938 struct comp_unit_head *cu_header = &cu->header;
18939 unsigned int bytes_read;
18940 struct dwarf_block *blk;
18941
18942 attr->form = (enum dwarf_form) form;
18943 switch (form)
18944 {
18945 case DW_FORM_ref_addr:
18946 if (cu_header->version == 2)
18947 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
18948 &bytes_read));
18949 else
18950 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18951 &bytes_read));
18952 info_ptr += bytes_read;
18953 break;
18954 case DW_FORM_GNU_ref_alt:
18955 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18956 &bytes_read));
18957 info_ptr += bytes_read;
18958 break;
18959 case DW_FORM_addr:
18960 {
18961 struct gdbarch *gdbarch = objfile->arch ();
18962 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
18963 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
18964 attr->set_address (addr);
18965 info_ptr += bytes_read;
18966 }
18967 break;
18968 case DW_FORM_block2:
18969 blk = dwarf_alloc_block (cu);
18970 blk->size = read_2_bytes (abfd, info_ptr);
18971 info_ptr += 2;
18972 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18973 info_ptr += blk->size;
18974 attr->set_block (blk);
18975 break;
18976 case DW_FORM_block4:
18977 blk = dwarf_alloc_block (cu);
18978 blk->size = read_4_bytes (abfd, info_ptr);
18979 info_ptr += 4;
18980 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18981 info_ptr += blk->size;
18982 attr->set_block (blk);
18983 break;
18984 case DW_FORM_data2:
18985 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
18986 info_ptr += 2;
18987 break;
18988 case DW_FORM_data4:
18989 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
18990 info_ptr += 4;
18991 break;
18992 case DW_FORM_data8:
18993 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
18994 info_ptr += 8;
18995 break;
18996 case DW_FORM_data16:
18997 blk = dwarf_alloc_block (cu);
18998 blk->size = 16;
18999 blk->data = read_n_bytes (abfd, info_ptr, 16);
19000 info_ptr += 16;
19001 attr->set_block (blk);
19002 break;
19003 case DW_FORM_sec_offset:
19004 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19005 &bytes_read));
19006 info_ptr += bytes_read;
19007 break;
19008 case DW_FORM_loclistx:
19009 {
19010 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19011 &bytes_read));
19012 info_ptr += bytes_read;
19013 }
19014 break;
19015 case DW_FORM_string:
19016 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19017 &bytes_read));
19018 info_ptr += bytes_read;
19019 break;
19020 case DW_FORM_strp:
19021 if (!cu->per_cu->is_dwz)
19022 {
19023 attr->set_string_noncanonical
19024 (read_indirect_string (per_objfile,
19025 abfd, info_ptr, cu_header,
19026 &bytes_read));
19027 info_ptr += bytes_read;
19028 break;
19029 }
19030 /* FALLTHROUGH */
19031 case DW_FORM_line_strp:
19032 if (!cu->per_cu->is_dwz)
19033 {
19034 attr->set_string_noncanonical
19035 (per_objfile->read_line_string (info_ptr, cu_header,
19036 &bytes_read));
19037 info_ptr += bytes_read;
19038 break;
19039 }
19040 /* FALLTHROUGH */
19041 case DW_FORM_GNU_strp_alt:
19042 {
19043 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19044 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19045 &bytes_read);
19046
19047 attr->set_string_noncanonical
19048 (dwz->read_string (objfile, str_offset));
19049 info_ptr += bytes_read;
19050 }
19051 break;
19052 case DW_FORM_exprloc:
19053 case DW_FORM_block:
19054 blk = dwarf_alloc_block (cu);
19055 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19056 info_ptr += bytes_read;
19057 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19058 info_ptr += blk->size;
19059 attr->set_block (blk);
19060 break;
19061 case DW_FORM_block1:
19062 blk = dwarf_alloc_block (cu);
19063 blk->size = read_1_byte (abfd, info_ptr);
19064 info_ptr += 1;
19065 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19066 info_ptr += blk->size;
19067 attr->set_block (blk);
19068 break;
19069 case DW_FORM_data1:
19070 case DW_FORM_flag:
19071 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19072 info_ptr += 1;
19073 break;
19074 case DW_FORM_flag_present:
19075 attr->set_unsigned (1);
19076 break;
19077 case DW_FORM_sdata:
19078 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19079 info_ptr += bytes_read;
19080 break;
19081 case DW_FORM_rnglistx:
19082 {
19083 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19084 &bytes_read));
19085 info_ptr += bytes_read;
19086 }
19087 break;
19088 case DW_FORM_udata:
19089 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19090 info_ptr += bytes_read;
19091 break;
19092 case DW_FORM_ref1:
19093 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19094 + read_1_byte (abfd, info_ptr)));
19095 info_ptr += 1;
19096 break;
19097 case DW_FORM_ref2:
19098 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19099 + read_2_bytes (abfd, info_ptr)));
19100 info_ptr += 2;
19101 break;
19102 case DW_FORM_ref4:
19103 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19104 + read_4_bytes (abfd, info_ptr)));
19105 info_ptr += 4;
19106 break;
19107 case DW_FORM_ref8:
19108 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19109 + read_8_bytes (abfd, info_ptr)));
19110 info_ptr += 8;
19111 break;
19112 case DW_FORM_ref_sig8:
19113 attr->set_signature (read_8_bytes (abfd, info_ptr));
19114 info_ptr += 8;
19115 break;
19116 case DW_FORM_ref_udata:
19117 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19118 + read_unsigned_leb128 (abfd, info_ptr,
19119 &bytes_read)));
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_indirect:
19123 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19124 info_ptr += bytes_read;
19125 if (form == DW_FORM_implicit_const)
19126 {
19127 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19128 info_ptr += bytes_read;
19129 }
19130 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19131 info_ptr);
19132 break;
19133 case DW_FORM_implicit_const:
19134 attr->set_signed (implicit_const);
19135 break;
19136 case DW_FORM_addrx:
19137 case DW_FORM_GNU_addr_index:
19138 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19139 &bytes_read));
19140 info_ptr += bytes_read;
19141 break;
19142 case DW_FORM_strx:
19143 case DW_FORM_strx1:
19144 case DW_FORM_strx2:
19145 case DW_FORM_strx3:
19146 case DW_FORM_strx4:
19147 case DW_FORM_GNU_str_index:
19148 {
19149 ULONGEST str_index;
19150 if (form == DW_FORM_strx1)
19151 {
19152 str_index = read_1_byte (abfd, info_ptr);
19153 info_ptr += 1;
19154 }
19155 else if (form == DW_FORM_strx2)
19156 {
19157 str_index = read_2_bytes (abfd, info_ptr);
19158 info_ptr += 2;
19159 }
19160 else if (form == DW_FORM_strx3)
19161 {
19162 str_index = read_3_bytes (abfd, info_ptr);
19163 info_ptr += 3;
19164 }
19165 else if (form == DW_FORM_strx4)
19166 {
19167 str_index = read_4_bytes (abfd, info_ptr);
19168 info_ptr += 4;
19169 }
19170 else
19171 {
19172 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 }
19175 attr->set_unsigned_reprocess (str_index);
19176 }
19177 break;
19178 default:
19179 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19180 dwarf_form_name (form),
19181 bfd_get_filename (abfd));
19182 }
19183
19184 /* Super hack. */
19185 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19186 attr->form = DW_FORM_GNU_ref_alt;
19187
19188 /* We have seen instances where the compiler tried to emit a byte
19189 size attribute of -1 which ended up being encoded as an unsigned
19190 0xffffffff. Although 0xffffffff is technically a valid size value,
19191 an object of this size seems pretty unlikely so we can relatively
19192 safely treat these cases as if the size attribute was invalid and
19193 treat them as zero by default. */
19194 if (attr->name == DW_AT_byte_size
19195 && form == DW_FORM_data4
19196 && attr->as_unsigned () >= 0xffffffff)
19197 {
19198 complaint
19199 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19200 hex_string (attr->as_unsigned ()));
19201 attr->set_unsigned (0);
19202 }
19203
19204 return info_ptr;
19205 }
19206
19207 /* Read an attribute described by an abbreviated attribute. */
19208
19209 static const gdb_byte *
19210 read_attribute (const struct die_reader_specs *reader,
19211 struct attribute *attr, const struct attr_abbrev *abbrev,
19212 const gdb_byte *info_ptr)
19213 {
19214 attr->name = abbrev->name;
19215 attr->string_is_canonical = 0;
19216 attr->requires_reprocessing = 0;
19217 return read_attribute_value (reader, attr, abbrev->form,
19218 abbrev->implicit_const, info_ptr);
19219 }
19220
19221 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19222
19223 static const char *
19224 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19225 LONGEST str_offset)
19226 {
19227 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19228 str_offset, "DW_FORM_strp");
19229 }
19230
19231 /* Return pointer to string at .debug_str offset as read from BUF.
19232 BUF is assumed to be in a compilation unit described by CU_HEADER.
19233 Return *BYTES_READ_PTR count of bytes read from BUF. */
19234
19235 static const char *
19236 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19237 const gdb_byte *buf,
19238 const struct comp_unit_head *cu_header,
19239 unsigned int *bytes_read_ptr)
19240 {
19241 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19242
19243 return read_indirect_string_at_offset (per_objfile, str_offset);
19244 }
19245
19246 /* See read.h. */
19247
19248 const char *
19249 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19250 unsigned int offset_size)
19251 {
19252 bfd *abfd = objfile->obfd;
19253 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19254
19255 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19256 }
19257
19258 /* See read.h. */
19259
19260 const char *
19261 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19262 const struct comp_unit_head *cu_header,
19263 unsigned int *bytes_read_ptr)
19264 {
19265 bfd *abfd = objfile->obfd;
19266 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19267
19268 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19269 }
19270
19271 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19272 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19273 ADDR_SIZE is the size of addresses from the CU header. */
19274
19275 static CORE_ADDR
19276 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19277 gdb::optional<ULONGEST> addr_base, int addr_size)
19278 {
19279 struct objfile *objfile = per_objfile->objfile;
19280 bfd *abfd = objfile->obfd;
19281 const gdb_byte *info_ptr;
19282 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19283
19284 per_objfile->per_bfd->addr.read (objfile);
19285 if (per_objfile->per_bfd->addr.buffer == NULL)
19286 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19287 objfile_name (objfile));
19288 if (addr_base_or_zero + addr_index * addr_size
19289 >= per_objfile->per_bfd->addr.size)
19290 error (_("DW_FORM_addr_index pointing outside of "
19291 ".debug_addr section [in module %s]"),
19292 objfile_name (objfile));
19293 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19294 + addr_index * addr_size);
19295 if (addr_size == 4)
19296 return bfd_get_32 (abfd, info_ptr);
19297 else
19298 return bfd_get_64 (abfd, info_ptr);
19299 }
19300
19301 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19302
19303 static CORE_ADDR
19304 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19305 {
19306 return read_addr_index_1 (cu->per_objfile, addr_index,
19307 cu->addr_base, cu->header.addr_size);
19308 }
19309
19310 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19311
19312 static CORE_ADDR
19313 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19314 unsigned int *bytes_read)
19315 {
19316 bfd *abfd = cu->per_objfile->objfile->obfd;
19317 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19318
19319 return read_addr_index (cu, addr_index);
19320 }
19321
19322 /* See read.h. */
19323
19324 CORE_ADDR
19325 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19326 dwarf2_per_objfile *per_objfile,
19327 unsigned int addr_index)
19328 {
19329 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19330 gdb::optional<ULONGEST> addr_base;
19331 int addr_size;
19332
19333 /* We need addr_base and addr_size.
19334 If we don't have PER_CU->cu, we have to get it.
19335 Nasty, but the alternative is storing the needed info in PER_CU,
19336 which at this point doesn't seem justified: it's not clear how frequently
19337 it would get used and it would increase the size of every PER_CU.
19338 Entry points like dwarf2_per_cu_addr_size do a similar thing
19339 so we're not in uncharted territory here.
19340 Alas we need to be a bit more complicated as addr_base is contained
19341 in the DIE.
19342
19343 We don't need to read the entire CU(/TU).
19344 We just need the header and top level die.
19345
19346 IWBN to use the aging mechanism to let us lazily later discard the CU.
19347 For now we skip this optimization. */
19348
19349 if (cu != NULL)
19350 {
19351 addr_base = cu->addr_base;
19352 addr_size = cu->header.addr_size;
19353 }
19354 else
19355 {
19356 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19357 addr_base = reader.cu->addr_base;
19358 addr_size = reader.cu->header.addr_size;
19359 }
19360
19361 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19362 }
19363
19364 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19365 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19366 DWO file. */
19367
19368 static const char *
19369 read_str_index (struct dwarf2_cu *cu,
19370 struct dwarf2_section_info *str_section,
19371 struct dwarf2_section_info *str_offsets_section,
19372 ULONGEST str_offsets_base, ULONGEST str_index)
19373 {
19374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19375 struct objfile *objfile = per_objfile->objfile;
19376 const char *objf_name = objfile_name (objfile);
19377 bfd *abfd = objfile->obfd;
19378 const gdb_byte *info_ptr;
19379 ULONGEST str_offset;
19380 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19381
19382 str_section->read (objfile);
19383 str_offsets_section->read (objfile);
19384 if (str_section->buffer == NULL)
19385 error (_("%s used without %s section"
19386 " in CU at offset %s [in module %s]"),
19387 form_name, str_section->get_name (),
19388 sect_offset_str (cu->header.sect_off), objf_name);
19389 if (str_offsets_section->buffer == NULL)
19390 error (_("%s used without %s section"
19391 " in CU at offset %s [in module %s]"),
19392 form_name, str_section->get_name (),
19393 sect_offset_str (cu->header.sect_off), objf_name);
19394 info_ptr = (str_offsets_section->buffer
19395 + str_offsets_base
19396 + str_index * cu->header.offset_size);
19397 if (cu->header.offset_size == 4)
19398 str_offset = bfd_get_32 (abfd, info_ptr);
19399 else
19400 str_offset = bfd_get_64 (abfd, info_ptr);
19401 if (str_offset >= str_section->size)
19402 error (_("Offset from %s pointing outside of"
19403 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19404 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19405 return (const char *) (str_section->buffer + str_offset);
19406 }
19407
19408 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19409
19410 static const char *
19411 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19412 {
19413 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19414 ? reader->cu->header.addr_size : 0;
19415 return read_str_index (reader->cu,
19416 &reader->dwo_file->sections.str,
19417 &reader->dwo_file->sections.str_offsets,
19418 str_offsets_base, str_index);
19419 }
19420
19421 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19422
19423 static const char *
19424 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19425 {
19426 struct objfile *objfile = cu->per_objfile->objfile;
19427 const char *objf_name = objfile_name (objfile);
19428 static const char form_name[] = "DW_FORM_GNU_str_index";
19429 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19430
19431 if (!cu->str_offsets_base.has_value ())
19432 error (_("%s used in Fission stub without %s"
19433 " in CU at offset 0x%lx [in module %s]"),
19434 form_name, str_offsets_attr_name,
19435 (long) cu->header.offset_size, objf_name);
19436
19437 return read_str_index (cu,
19438 &cu->per_objfile->per_bfd->str,
19439 &cu->per_objfile->per_bfd->str_offsets,
19440 *cu->str_offsets_base, str_index);
19441 }
19442
19443 /* Return the length of an LEB128 number in BUF. */
19444
19445 static int
19446 leb128_size (const gdb_byte *buf)
19447 {
19448 const gdb_byte *begin = buf;
19449 gdb_byte byte;
19450
19451 while (1)
19452 {
19453 byte = *buf++;
19454 if ((byte & 128) == 0)
19455 return buf - begin;
19456 }
19457 }
19458
19459 static enum language
19460 dwarf_lang_to_enum_language (unsigned int lang)
19461 {
19462 enum language language;
19463
19464 switch (lang)
19465 {
19466 case DW_LANG_C89:
19467 case DW_LANG_C99:
19468 case DW_LANG_C11:
19469 case DW_LANG_C:
19470 case DW_LANG_UPC:
19471 language = language_c;
19472 break;
19473 case DW_LANG_Java:
19474 case DW_LANG_C_plus_plus:
19475 case DW_LANG_C_plus_plus_11:
19476 case DW_LANG_C_plus_plus_14:
19477 language = language_cplus;
19478 break;
19479 case DW_LANG_D:
19480 language = language_d;
19481 break;
19482 case DW_LANG_Fortran77:
19483 case DW_LANG_Fortran90:
19484 case DW_LANG_Fortran95:
19485 case DW_LANG_Fortran03:
19486 case DW_LANG_Fortran08:
19487 language = language_fortran;
19488 break;
19489 case DW_LANG_Go:
19490 language = language_go;
19491 break;
19492 case DW_LANG_Mips_Assembler:
19493 language = language_asm;
19494 break;
19495 case DW_LANG_Ada83:
19496 case DW_LANG_Ada95:
19497 language = language_ada;
19498 break;
19499 case DW_LANG_Modula2:
19500 language = language_m2;
19501 break;
19502 case DW_LANG_Pascal83:
19503 language = language_pascal;
19504 break;
19505 case DW_LANG_ObjC:
19506 language = language_objc;
19507 break;
19508 case DW_LANG_Rust:
19509 case DW_LANG_Rust_old:
19510 language = language_rust;
19511 break;
19512 case DW_LANG_OpenCL:
19513 language = language_opencl;
19514 break;
19515 case DW_LANG_Cobol74:
19516 case DW_LANG_Cobol85:
19517 default:
19518 language = language_minimal;
19519 break;
19520 }
19521
19522 return language;
19523 }
19524
19525 /* Return the named attribute or NULL if not there. */
19526
19527 static struct attribute *
19528 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19529 {
19530 for (;;)
19531 {
19532 unsigned int i;
19533 struct attribute *spec = NULL;
19534
19535 for (i = 0; i < die->num_attrs; ++i)
19536 {
19537 if (die->attrs[i].name == name)
19538 return &die->attrs[i];
19539 if (die->attrs[i].name == DW_AT_specification
19540 || die->attrs[i].name == DW_AT_abstract_origin)
19541 spec = &die->attrs[i];
19542 }
19543
19544 if (!spec)
19545 break;
19546
19547 die = follow_die_ref (die, spec, &cu);
19548 }
19549
19550 return NULL;
19551 }
19552
19553 /* Return the string associated with a string-typed attribute, or NULL if it
19554 is either not found or is of an incorrect type. */
19555
19556 static const char *
19557 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19558 {
19559 struct attribute *attr;
19560 const char *str = NULL;
19561
19562 attr = dwarf2_attr (die, name, cu);
19563
19564 if (attr != NULL)
19565 {
19566 str = attr->as_string ();
19567 if (str == nullptr)
19568 complaint (_("string type expected for attribute %s for "
19569 "DIE at %s in module %s"),
19570 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19571 objfile_name (cu->per_objfile->objfile));
19572 }
19573
19574 return str;
19575 }
19576
19577 /* Return the dwo name or NULL if not present. If present, it is in either
19578 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19579 static const char *
19580 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19581 {
19582 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19583 if (dwo_name == nullptr)
19584 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19585 return dwo_name;
19586 }
19587
19588 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19589 and holds a non-zero value. This function should only be used for
19590 DW_FORM_flag or DW_FORM_flag_present attributes. */
19591
19592 static int
19593 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19594 {
19595 struct attribute *attr = dwarf2_attr (die, name, cu);
19596
19597 return attr != nullptr && attr->as_boolean ();
19598 }
19599
19600 static int
19601 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19602 {
19603 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19604 which value is non-zero. However, we have to be careful with
19605 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19606 (via dwarf2_flag_true_p) follows this attribute. So we may
19607 end up accidently finding a declaration attribute that belongs
19608 to a different DIE referenced by the specification attribute,
19609 even though the given DIE does not have a declaration attribute. */
19610 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19611 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19612 }
19613
19614 /* Return the die giving the specification for DIE, if there is
19615 one. *SPEC_CU is the CU containing DIE on input, and the CU
19616 containing the return value on output. If there is no
19617 specification, but there is an abstract origin, that is
19618 returned. */
19619
19620 static struct die_info *
19621 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19622 {
19623 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19624 *spec_cu);
19625
19626 if (spec_attr == NULL)
19627 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19628
19629 if (spec_attr == NULL)
19630 return NULL;
19631 else
19632 return follow_die_ref (die, spec_attr, spec_cu);
19633 }
19634
19635 /* A convenience function to find the proper .debug_line section for a CU. */
19636
19637 static struct dwarf2_section_info *
19638 get_debug_line_section (struct dwarf2_cu *cu)
19639 {
19640 struct dwarf2_section_info *section;
19641 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19642
19643 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19644 DWO file. */
19645 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19646 section = &cu->dwo_unit->dwo_file->sections.line;
19647 else if (cu->per_cu->is_dwz)
19648 {
19649 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19650
19651 section = &dwz->line;
19652 }
19653 else
19654 section = &per_objfile->per_bfd->line;
19655
19656 return section;
19657 }
19658
19659 /* Read the statement program header starting at OFFSET in
19660 .debug_line, or .debug_line.dwo. Return a pointer
19661 to a struct line_header, allocated using xmalloc.
19662 Returns NULL if there is a problem reading the header, e.g., if it
19663 has a version we don't understand.
19664
19665 NOTE: the strings in the include directory and file name tables of
19666 the returned object point into the dwarf line section buffer,
19667 and must not be freed. */
19668
19669 static line_header_up
19670 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19671 {
19672 struct dwarf2_section_info *section;
19673 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19674
19675 section = get_debug_line_section (cu);
19676 section->read (per_objfile->objfile);
19677 if (section->buffer == NULL)
19678 {
19679 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19680 complaint (_("missing .debug_line.dwo section"));
19681 else
19682 complaint (_("missing .debug_line section"));
19683 return 0;
19684 }
19685
19686 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19687 per_objfile, section, &cu->header);
19688 }
19689
19690 /* Subroutine of dwarf_decode_lines to simplify it.
19691 Return the file name for the given file_entry.
19692 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19693 If space for the result is malloc'd, *NAME_HOLDER will be set.
19694 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19695 equivalent to CU_INFO. */
19696
19697 static const char *
19698 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19699 const file_and_directory &cu_info,
19700 std::string &name_holder)
19701 {
19702 const char *include_name = fe.name;
19703 const char *include_name_to_compare = include_name;
19704
19705 const char *dir_name = fe.include_dir (lh);
19706
19707 std::string hold_compare;
19708 if (!IS_ABSOLUTE_PATH (include_name)
19709 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19710 {
19711 /* Avoid creating a duplicate name for CU_INFO.
19712 We do this by comparing INCLUDE_NAME and CU_INFO.
19713 Before we do the comparison, however, we need to account
19714 for DIR_NAME and COMP_DIR.
19715 First prepend dir_name (if non-NULL). If we still don't
19716 have an absolute path prepend comp_dir (if non-NULL).
19717 However, the directory we record in the include-file's
19718 psymtab does not contain COMP_DIR (to match the
19719 corresponding symtab(s)).
19720
19721 Example:
19722
19723 bash$ cd /tmp
19724 bash$ gcc -g ./hello.c
19725 include_name = "hello.c"
19726 dir_name = "."
19727 DW_AT_comp_dir = comp_dir = "/tmp"
19728 DW_AT_name = "./hello.c"
19729
19730 */
19731
19732 if (dir_name != NULL)
19733 {
19734 name_holder = path_join (dir_name, include_name);
19735 include_name = name_holder.c_str ();
19736 include_name_to_compare = include_name;
19737 }
19738 if (!IS_ABSOLUTE_PATH (include_name)
19739 && cu_info.get_comp_dir () != nullptr)
19740 {
19741 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19742 include_name_to_compare = hold_compare.c_str ();
19743 }
19744 }
19745
19746 std::string copied_name;
19747 const char *cu_filename = cu_info.get_name ();
19748 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19749 {
19750 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19751 cu_filename = copied_name.c_str ();
19752 }
19753
19754 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19755 return nullptr;
19756 return include_name;
19757 }
19758
19759 /* State machine to track the state of the line number program. */
19760
19761 class lnp_state_machine
19762 {
19763 public:
19764 /* Initialize a machine state for the start of a line number
19765 program. */
19766 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19767
19768 file_entry *current_file ()
19769 {
19770 /* lh->file_names is 0-based, but the file name numbers in the
19771 statement program are 1-based. */
19772 return m_line_header->file_name_at (m_file);
19773 }
19774
19775 /* Record the line in the state machine. END_SEQUENCE is true if
19776 we're processing the end of a sequence. */
19777 void record_line (bool end_sequence);
19778
19779 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19780 nop-out rest of the lines in this sequence. */
19781 void check_line_address (struct dwarf2_cu *cu,
19782 const gdb_byte *line_ptr,
19783 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19784
19785 void handle_set_discriminator (unsigned int discriminator)
19786 {
19787 m_discriminator = discriminator;
19788 m_line_has_non_zero_discriminator |= discriminator != 0;
19789 }
19790
19791 /* Handle DW_LNE_set_address. */
19792 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19793 {
19794 m_op_index = 0;
19795 address += baseaddr;
19796 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19797 }
19798
19799 /* Handle DW_LNS_advance_pc. */
19800 void handle_advance_pc (CORE_ADDR adjust);
19801
19802 /* Handle a special opcode. */
19803 void handle_special_opcode (unsigned char op_code);
19804
19805 /* Handle DW_LNS_advance_line. */
19806 void handle_advance_line (int line_delta)
19807 {
19808 advance_line (line_delta);
19809 }
19810
19811 /* Handle DW_LNS_set_file. */
19812 void handle_set_file (file_name_index file);
19813
19814 /* Handle DW_LNS_negate_stmt. */
19815 void handle_negate_stmt ()
19816 {
19817 m_flags ^= LEF_IS_STMT;
19818 }
19819
19820 /* Handle DW_LNS_const_add_pc. */
19821 void handle_const_add_pc ();
19822
19823 /* Handle DW_LNS_fixed_advance_pc. */
19824 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19825 {
19826 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19827 m_op_index = 0;
19828 }
19829
19830 /* Handle DW_LNS_copy. */
19831 void handle_copy ()
19832 {
19833 record_line (false);
19834 m_discriminator = 0;
19835 m_flags &= ~LEF_PROLOGUE_END;
19836 }
19837
19838 /* Handle DW_LNE_end_sequence. */
19839 void handle_end_sequence ()
19840 {
19841 m_currently_recording_lines = true;
19842 }
19843
19844 /* Handle DW_LNS_set_prologue_end. */
19845 void handle_set_prologue_end ()
19846 {
19847 m_flags |= LEF_PROLOGUE_END;
19848 }
19849
19850 private:
19851 /* Advance the line by LINE_DELTA. */
19852 void advance_line (int line_delta)
19853 {
19854 m_line += line_delta;
19855
19856 if (line_delta != 0)
19857 m_line_has_non_zero_discriminator = m_discriminator != 0;
19858 }
19859
19860 struct dwarf2_cu *m_cu;
19861
19862 gdbarch *m_gdbarch;
19863
19864 /* The line number header. */
19865 line_header *m_line_header;
19866
19867 /* These are part of the standard DWARF line number state machine,
19868 and initialized according to the DWARF spec. */
19869
19870 unsigned char m_op_index = 0;
19871 /* The line table index of the current file. */
19872 file_name_index m_file = 1;
19873 unsigned int m_line = 1;
19874
19875 /* These are initialized in the constructor. */
19876
19877 CORE_ADDR m_address;
19878 linetable_entry_flags m_flags;
19879 unsigned int m_discriminator;
19880
19881 /* Additional bits of state we need to track. */
19882
19883 /* The last file that we called dwarf2_start_subfile for.
19884 This is only used for TLLs. */
19885 unsigned int m_last_file = 0;
19886 /* The last file a line number was recorded for. */
19887 struct subfile *m_last_subfile = NULL;
19888
19889 /* The address of the last line entry. */
19890 CORE_ADDR m_last_address;
19891
19892 /* Set to true when a previous line at the same address (using
19893 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19894 when a line entry at a new address (m_address different to
19895 m_last_address) is processed. */
19896 bool m_stmt_at_address = false;
19897
19898 /* When true, record the lines we decode. */
19899 bool m_currently_recording_lines = false;
19900
19901 /* The last line number that was recorded, used to coalesce
19902 consecutive entries for the same line. This can happen, for
19903 example, when discriminators are present. PR 17276. */
19904 unsigned int m_last_line = 0;
19905 bool m_line_has_non_zero_discriminator = false;
19906 };
19907
19908 void
19909 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19910 {
19911 CORE_ADDR addr_adj = (((m_op_index + adjust)
19912 / m_line_header->maximum_ops_per_instruction)
19913 * m_line_header->minimum_instruction_length);
19914 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19915 m_op_index = ((m_op_index + adjust)
19916 % m_line_header->maximum_ops_per_instruction);
19917 }
19918
19919 void
19920 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19921 {
19922 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19923 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19924 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19925 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19926 / m_line_header->maximum_ops_per_instruction)
19927 * m_line_header->minimum_instruction_length);
19928 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19929 m_op_index = ((m_op_index + adj_opcode_d)
19930 % m_line_header->maximum_ops_per_instruction);
19931
19932 int line_delta = m_line_header->line_base + adj_opcode_r;
19933 advance_line (line_delta);
19934 record_line (false);
19935 m_discriminator = 0;
19936 m_flags &= ~LEF_PROLOGUE_END;
19937 }
19938
19939 void
19940 lnp_state_machine::handle_set_file (file_name_index file)
19941 {
19942 m_file = file;
19943
19944 const file_entry *fe = current_file ();
19945 if (fe == NULL)
19946 dwarf2_debug_line_missing_file_complaint ();
19947 else
19948 {
19949 const char *dir = fe->include_dir (m_line_header);
19950
19951 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19952 m_line_has_non_zero_discriminator = m_discriminator != 0;
19953 dwarf2_start_subfile (m_cu, fe->name, dir);
19954 }
19955 }
19956
19957 void
19958 lnp_state_machine::handle_const_add_pc ()
19959 {
19960 CORE_ADDR adjust
19961 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19962
19963 CORE_ADDR addr_adj
19964 = (((m_op_index + adjust)
19965 / m_line_header->maximum_ops_per_instruction)
19966 * m_line_header->minimum_instruction_length);
19967
19968 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19969 m_op_index = ((m_op_index + adjust)
19970 % m_line_header->maximum_ops_per_instruction);
19971 }
19972
19973 /* Return non-zero if we should add LINE to the line number table.
19974 LINE is the line to add, LAST_LINE is the last line that was added,
19975 LAST_SUBFILE is the subfile for LAST_LINE.
19976 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19977 had a non-zero discriminator.
19978
19979 We have to be careful in the presence of discriminators.
19980 E.g., for this line:
19981
19982 for (i = 0; i < 100000; i++);
19983
19984 clang can emit four line number entries for that one line,
19985 each with a different discriminator.
19986 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19987
19988 However, we want gdb to coalesce all four entries into one.
19989 Otherwise the user could stepi into the middle of the line and
19990 gdb would get confused about whether the pc really was in the
19991 middle of the line.
19992
19993 Things are further complicated by the fact that two consecutive
19994 line number entries for the same line is a heuristic used by gcc
19995 to denote the end of the prologue. So we can't just discard duplicate
19996 entries, we have to be selective about it. The heuristic we use is
19997 that we only collapse consecutive entries for the same line if at least
19998 one of those entries has a non-zero discriminator. PR 17276.
19999
20000 Note: Addresses in the line number state machine can never go backwards
20001 within one sequence, thus this coalescing is ok. */
20002
20003 static int
20004 dwarf_record_line_p (struct dwarf2_cu *cu,
20005 unsigned int line, unsigned int last_line,
20006 int line_has_non_zero_discriminator,
20007 struct subfile *last_subfile)
20008 {
20009 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20010 return 1;
20011 if (line != last_line)
20012 return 1;
20013 /* Same line for the same file that we've seen already.
20014 As a last check, for pr 17276, only record the line if the line
20015 has never had a non-zero discriminator. */
20016 if (!line_has_non_zero_discriminator)
20017 return 1;
20018 return 0;
20019 }
20020
20021 /* Use the CU's builder to record line number LINE beginning at
20022 address ADDRESS in the line table of subfile SUBFILE. */
20023
20024 static void
20025 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20026 unsigned int line, CORE_ADDR address,
20027 linetable_entry_flags flags,
20028 struct dwarf2_cu *cu)
20029 {
20030 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20031
20032 if (dwarf_line_debug)
20033 {
20034 gdb_printf (gdb_stdlog,
20035 "Recording line %u, file %s, address %s\n",
20036 line, lbasename (subfile->name.c_str ()),
20037 paddress (gdbarch, address));
20038 }
20039
20040 if (cu != nullptr)
20041 cu->get_builder ()->record_line (subfile, line, addr, flags);
20042 }
20043
20044 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20045 Mark the end of a set of line number records.
20046 The arguments are the same as for dwarf_record_line_1.
20047 If SUBFILE is NULL the request is ignored. */
20048
20049 static void
20050 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20051 CORE_ADDR address, struct dwarf2_cu *cu)
20052 {
20053 if (subfile == NULL)
20054 return;
20055
20056 if (dwarf_line_debug)
20057 {
20058 gdb_printf (gdb_stdlog,
20059 "Finishing current line, file %s, address %s\n",
20060 lbasename (subfile->name.c_str ()),
20061 paddress (gdbarch, address));
20062 }
20063
20064 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20065 }
20066
20067 void
20068 lnp_state_machine::record_line (bool end_sequence)
20069 {
20070 if (dwarf_line_debug)
20071 {
20072 gdb_printf (gdb_stdlog,
20073 "Processing actual line %u: file %u,"
20074 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20075 m_line, m_file,
20076 paddress (m_gdbarch, m_address),
20077 (m_flags & LEF_IS_STMT) != 0,
20078 (m_flags & LEF_PROLOGUE_END) != 0,
20079 m_discriminator,
20080 (end_sequence ? "\t(end sequence)" : ""));
20081 }
20082
20083 file_entry *fe = current_file ();
20084
20085 if (fe == NULL)
20086 dwarf2_debug_line_missing_file_complaint ();
20087 /* For now we ignore lines not starting on an instruction boundary.
20088 But not when processing end_sequence for compatibility with the
20089 previous version of the code. */
20090 else if (m_op_index == 0 || end_sequence)
20091 {
20092 /* When we switch files we insert an end maker in the first file,
20093 switch to the second file and add a new line entry. The
20094 problem is that the end marker inserted in the first file will
20095 discard any previous line entries at the same address. If the
20096 line entries in the first file are marked as is-stmt, while
20097 the new line in the second file is non-stmt, then this means
20098 the end marker will discard is-stmt lines so we can have a
20099 non-stmt line. This means that there are less addresses at
20100 which the user can insert a breakpoint.
20101
20102 To improve this we track the last address in m_last_address,
20103 and whether we have seen an is-stmt at this address. Then
20104 when switching files, if we have seen a stmt at the current
20105 address, and we are switching to create a non-stmt line, then
20106 discard the new line. */
20107 bool file_changed
20108 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20109 bool ignore_this_line
20110 = ((file_changed && !end_sequence && m_last_address == m_address
20111 && ((m_flags & LEF_IS_STMT) == 0)
20112 && m_stmt_at_address)
20113 || (!end_sequence && m_line == 0));
20114
20115 if ((file_changed && !ignore_this_line) || end_sequence)
20116 {
20117 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20118 m_currently_recording_lines ? m_cu : nullptr);
20119 }
20120
20121 if (!end_sequence && !ignore_this_line)
20122 {
20123 linetable_entry_flags lte_flags = m_flags;
20124 if (producer_is_codewarrior (m_cu))
20125 lte_flags |= LEF_IS_STMT;
20126
20127 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20128 m_line_has_non_zero_discriminator,
20129 m_last_subfile))
20130 {
20131 buildsym_compunit *builder = m_cu->get_builder ();
20132 dwarf_record_line_1 (m_gdbarch,
20133 builder->get_current_subfile (),
20134 m_line, m_address, lte_flags,
20135 m_currently_recording_lines ? m_cu : nullptr);
20136 }
20137 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20138 m_last_line = m_line;
20139 }
20140 }
20141
20142 /* Track whether we have seen any IS_STMT true at m_address in case we
20143 have multiple line table entries all at m_address. */
20144 if (m_last_address != m_address)
20145 {
20146 m_stmt_at_address = false;
20147 m_last_address = m_address;
20148 }
20149 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20150 }
20151
20152 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20153 line_header *lh)
20154 {
20155 m_cu = cu;
20156 m_gdbarch = arch;
20157 m_line_header = lh;
20158
20159 m_currently_recording_lines = true;
20160
20161 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20162 was a line entry for it so that the backend has a chance to adjust it
20163 and also record it in case it needs it. This is currently used by MIPS
20164 code, cf. `mips_adjust_dwarf2_line'. */
20165 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20166 m_flags = 0;
20167 if (lh->default_is_stmt)
20168 m_flags |= LEF_IS_STMT;
20169 m_discriminator = 0;
20170
20171 m_last_address = m_address;
20172 m_stmt_at_address = false;
20173 }
20174
20175 void
20176 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20177 const gdb_byte *line_ptr,
20178 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20179 {
20180 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20181 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20182 located at 0x0. In this case, additionally check that if
20183 ADDRESS < UNRELOCATED_LOWPC. */
20184
20185 if ((address == 0 && address < unrelocated_lowpc)
20186 || address == (CORE_ADDR) -1)
20187 {
20188 /* This line table is for a function which has been
20189 GCd by the linker. Ignore it. PR gdb/12528 */
20190
20191 struct objfile *objfile = cu->per_objfile->objfile;
20192 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20193
20194 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20195 line_offset, objfile_name (objfile));
20196 m_currently_recording_lines = false;
20197 /* Note: m_currently_recording_lines is left as false until we see
20198 DW_LNE_end_sequence. */
20199 }
20200 }
20201
20202 /* Subroutine of dwarf_decode_lines to simplify it.
20203 Process the line number information in LH. */
20204
20205 static void
20206 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20207 CORE_ADDR lowpc)
20208 {
20209 const gdb_byte *line_ptr, *extended_end;
20210 const gdb_byte *line_end;
20211 unsigned int bytes_read, extended_len;
20212 unsigned char op_code, extended_op;
20213 CORE_ADDR baseaddr;
20214 struct objfile *objfile = cu->per_objfile->objfile;
20215 bfd *abfd = objfile->obfd;
20216 struct gdbarch *gdbarch = objfile->arch ();
20217
20218 baseaddr = objfile->text_section_offset ();
20219
20220 line_ptr = lh->statement_program_start;
20221 line_end = lh->statement_program_end;
20222
20223 /* Read the statement sequences until there's nothing left. */
20224 while (line_ptr < line_end)
20225 {
20226 /* The DWARF line number program state machine. Reset the state
20227 machine at the start of each sequence. */
20228 lnp_state_machine state_machine (cu, gdbarch, lh);
20229 bool end_sequence = false;
20230
20231 /* Start a subfile for the current file of the state
20232 machine. */
20233 const file_entry *fe = state_machine.current_file ();
20234
20235 if (fe != NULL)
20236 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20237
20238 /* Decode the table. */
20239 while (line_ptr < line_end && !end_sequence)
20240 {
20241 op_code = read_1_byte (abfd, line_ptr);
20242 line_ptr += 1;
20243
20244 if (op_code >= lh->opcode_base)
20245 {
20246 /* Special opcode. */
20247 state_machine.handle_special_opcode (op_code);
20248 }
20249 else switch (op_code)
20250 {
20251 case DW_LNS_extended_op:
20252 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20253 &bytes_read);
20254 line_ptr += bytes_read;
20255 extended_end = line_ptr + extended_len;
20256 extended_op = read_1_byte (abfd, line_ptr);
20257 line_ptr += 1;
20258 if (DW_LNE_lo_user <= extended_op
20259 && extended_op <= DW_LNE_hi_user)
20260 {
20261 /* Vendor extension, ignore. */
20262 line_ptr = extended_end;
20263 break;
20264 }
20265 switch (extended_op)
20266 {
20267 case DW_LNE_end_sequence:
20268 state_machine.handle_end_sequence ();
20269 end_sequence = true;
20270 break;
20271 case DW_LNE_set_address:
20272 {
20273 CORE_ADDR address
20274 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20275 line_ptr += bytes_read;
20276
20277 state_machine.check_line_address (cu, line_ptr,
20278 lowpc - baseaddr, address);
20279 state_machine.handle_set_address (baseaddr, address);
20280 }
20281 break;
20282 case DW_LNE_define_file:
20283 {
20284 const char *cur_file;
20285 unsigned int mod_time, length;
20286 dir_index dindex;
20287
20288 cur_file = read_direct_string (abfd, line_ptr,
20289 &bytes_read);
20290 line_ptr += bytes_read;
20291 dindex = (dir_index)
20292 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20293 line_ptr += bytes_read;
20294 mod_time =
20295 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20296 line_ptr += bytes_read;
20297 length =
20298 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20299 line_ptr += bytes_read;
20300 lh->add_file_name (cur_file, dindex, mod_time, length);
20301 }
20302 break;
20303 case DW_LNE_set_discriminator:
20304 {
20305 /* The discriminator is not interesting to the
20306 debugger; just ignore it. We still need to
20307 check its value though:
20308 if there are consecutive entries for the same
20309 (non-prologue) line we want to coalesce them.
20310 PR 17276. */
20311 unsigned int discr
20312 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20313 line_ptr += bytes_read;
20314
20315 state_machine.handle_set_discriminator (discr);
20316 }
20317 break;
20318 default:
20319 complaint (_("mangled .debug_line section"));
20320 return;
20321 }
20322 /* Make sure that we parsed the extended op correctly. If e.g.
20323 we expected a different address size than the producer used,
20324 we may have read the wrong number of bytes. */
20325 if (line_ptr != extended_end)
20326 {
20327 complaint (_("mangled .debug_line section"));
20328 return;
20329 }
20330 break;
20331 case DW_LNS_copy:
20332 state_machine.handle_copy ();
20333 break;
20334 case DW_LNS_advance_pc:
20335 {
20336 CORE_ADDR adjust
20337 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20338 line_ptr += bytes_read;
20339
20340 state_machine.handle_advance_pc (adjust);
20341 }
20342 break;
20343 case DW_LNS_advance_line:
20344 {
20345 int line_delta
20346 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20347 line_ptr += bytes_read;
20348
20349 state_machine.handle_advance_line (line_delta);
20350 }
20351 break;
20352 case DW_LNS_set_file:
20353 {
20354 file_name_index file
20355 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20356 &bytes_read);
20357 line_ptr += bytes_read;
20358
20359 state_machine.handle_set_file (file);
20360 }
20361 break;
20362 case DW_LNS_set_column:
20363 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20364 line_ptr += bytes_read;
20365 break;
20366 case DW_LNS_negate_stmt:
20367 state_machine.handle_negate_stmt ();
20368 break;
20369 case DW_LNS_set_basic_block:
20370 break;
20371 /* Add to the address register of the state machine the
20372 address increment value corresponding to special opcode
20373 255. I.e., this value is scaled by the minimum
20374 instruction length since special opcode 255 would have
20375 scaled the increment. */
20376 case DW_LNS_const_add_pc:
20377 state_machine.handle_const_add_pc ();
20378 break;
20379 case DW_LNS_fixed_advance_pc:
20380 {
20381 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20382 line_ptr += 2;
20383
20384 state_machine.handle_fixed_advance_pc (addr_adj);
20385 }
20386 break;
20387 case DW_LNS_set_prologue_end:
20388 state_machine.handle_set_prologue_end ();
20389 break;
20390 default:
20391 {
20392 /* Unknown standard opcode, ignore it. */
20393 int i;
20394
20395 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20396 {
20397 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20398 line_ptr += bytes_read;
20399 }
20400 }
20401 }
20402 }
20403
20404 if (!end_sequence)
20405 dwarf2_debug_line_missing_end_sequence_complaint ();
20406
20407 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20408 in which case we still finish recording the last line). */
20409 state_machine.record_line (true);
20410 }
20411 }
20412
20413 /* Decode the Line Number Program (LNP) for the given line_header
20414 structure and CU. The actual information extracted and the type
20415 of structures created from the LNP depends on the value of PST.
20416
20417 FND holds the CU file name and directory, if known.
20418 It is used for relative paths in the line table.
20419
20420 NOTE: It is important that psymtabs have the same file name (via
20421 strcmp) as the corresponding symtab. Since the directory is not
20422 used in the name of the symtab we don't use it in the name of the
20423 psymtabs we create. E.g. expand_line_sal requires this when
20424 finding psymtabs to expand. A good testcase for this is
20425 mb-inline.exp.
20426
20427 LOWPC is the lowest address in CU (or 0 if not known).
20428
20429 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20430 for its PC<->lines mapping information. Otherwise only the filename
20431 table is read in. */
20432
20433 static void
20434 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20435 CORE_ADDR lowpc, int decode_mapping)
20436 {
20437 if (decode_mapping)
20438 dwarf_decode_lines_1 (lh, cu, lowpc);
20439
20440 /* Make sure a symtab is created for every file, even files
20441 which contain only variables (i.e. no code with associated
20442 line numbers). */
20443 buildsym_compunit *builder = cu->get_builder ();
20444 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20445
20446 for (auto &fe : lh->file_names ())
20447 {
20448 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20449 if (builder->get_current_subfile ()->symtab == NULL)
20450 {
20451 builder->get_current_subfile ()->symtab
20452 = allocate_symtab (cust,
20453 builder->get_current_subfile ()->name.c_str ());
20454 }
20455 fe.symtab = builder->get_current_subfile ()->symtab;
20456 }
20457 }
20458
20459 /* Start a subfile for DWARF. FILENAME is the name of the file and
20460 DIRNAME the name of the source directory which contains FILENAME
20461 or NULL if not known.
20462 This routine tries to keep line numbers from identical absolute and
20463 relative file names in a common subfile.
20464
20465 Using the `list' example from the GDB testsuite, which resides in
20466 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20467 of /srcdir/list0.c yields the following debugging information for list0.c:
20468
20469 DW_AT_name: /srcdir/list0.c
20470 DW_AT_comp_dir: /compdir
20471 files.files[0].name: list0.h
20472 files.files[0].dir: /srcdir
20473 files.files[1].name: list0.c
20474 files.files[1].dir: /srcdir
20475
20476 The line number information for list0.c has to end up in a single
20477 subfile, so that `break /srcdir/list0.c:1' works as expected.
20478 start_subfile will ensure that this happens provided that we pass the
20479 concatenation of files.files[1].dir and files.files[1].name as the
20480 subfile's name. */
20481
20482 static void
20483 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20484 const char *dirname)
20485 {
20486 std::string copy;
20487
20488 /* In order not to lose the line information directory,
20489 we concatenate it to the filename when it makes sense.
20490 Note that the Dwarf3 standard says (speaking of filenames in line
20491 information): ``The directory index is ignored for file names
20492 that represent full path names''. Thus ignoring dirname in the
20493 `else' branch below isn't an issue. */
20494
20495 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20496 {
20497 copy = path_join (dirname, filename);
20498 filename = copy.c_str ();
20499 }
20500
20501 cu->get_builder ()->start_subfile (filename);
20502 }
20503
20504 static void
20505 var_decode_location (struct attribute *attr, struct symbol *sym,
20506 struct dwarf2_cu *cu)
20507 {
20508 struct objfile *objfile = cu->per_objfile->objfile;
20509 struct comp_unit_head *cu_header = &cu->header;
20510
20511 /* NOTE drow/2003-01-30: There used to be a comment and some special
20512 code here to turn a symbol with DW_AT_external and a
20513 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20514 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20515 with some versions of binutils) where shared libraries could have
20516 relocations against symbols in their debug information - the
20517 minimal symbol would have the right address, but the debug info
20518 would not. It's no longer necessary, because we will explicitly
20519 apply relocations when we read in the debug information now. */
20520
20521 /* A DW_AT_location attribute with no contents indicates that a
20522 variable has been optimized away. */
20523 if (attr->form_is_block () && attr->as_block ()->size == 0)
20524 {
20525 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20526 return;
20527 }
20528
20529 /* Handle one degenerate form of location expression specially, to
20530 preserve GDB's previous behavior when section offsets are
20531 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20532 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20533
20534 if (attr->form_is_block ())
20535 {
20536 struct dwarf_block *block = attr->as_block ();
20537
20538 if ((block->data[0] == DW_OP_addr
20539 && block->size == 1 + cu_header->addr_size)
20540 || ((block->data[0] == DW_OP_GNU_addr_index
20541 || block->data[0] == DW_OP_addrx)
20542 && (block->size
20543 == 1 + leb128_size (&block->data[1]))))
20544 {
20545 unsigned int dummy;
20546
20547 if (block->data[0] == DW_OP_addr)
20548 sym->set_value_address
20549 (cu->header.read_address (objfile->obfd, block->data + 1,
20550 &dummy));
20551 else
20552 sym->set_value_address
20553 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20554 sym->set_aclass_index (LOC_STATIC);
20555 fixup_symbol_section (sym, objfile);
20556 sym->set_value_address
20557 (sym->value_address ()
20558 + objfile->section_offsets[sym->section_index ()]);
20559 return;
20560 }
20561 }
20562
20563 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20564 expression evaluator, and use LOC_COMPUTED only when necessary
20565 (i.e. when the value of a register or memory location is
20566 referenced, or a thread-local block, etc.). Then again, it might
20567 not be worthwhile. I'm assuming that it isn't unless performance
20568 or memory numbers show me otherwise. */
20569
20570 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20571
20572 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20573 cu->has_loclist = true;
20574 }
20575
20576 /* Given a pointer to a DWARF information entry, figure out if we need
20577 to make a symbol table entry for it, and if so, create a new entry
20578 and return a pointer to it.
20579 If TYPE is NULL, determine symbol type from the die, otherwise
20580 used the passed type.
20581 If SPACE is not NULL, use it to hold the new symbol. If it is
20582 NULL, allocate a new symbol on the objfile's obstack. */
20583
20584 static struct symbol *
20585 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20586 struct symbol *space)
20587 {
20588 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20589 struct objfile *objfile = per_objfile->objfile;
20590 struct gdbarch *gdbarch = objfile->arch ();
20591 struct symbol *sym = NULL;
20592 const char *name;
20593 struct attribute *attr = NULL;
20594 struct attribute *attr2 = NULL;
20595 CORE_ADDR baseaddr;
20596 struct pending **list_to_add = NULL;
20597
20598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20599
20600 baseaddr = objfile->text_section_offset ();
20601
20602 name = dwarf2_name (die, cu);
20603 if (name == nullptr && (die->tag == DW_TAG_subprogram
20604 || die->tag == DW_TAG_inlined_subroutine
20605 || die->tag == DW_TAG_entry_point))
20606 name = dw2_linkage_name (die, cu);
20607
20608 if (name)
20609 {
20610 int suppress_add = 0;
20611
20612 if (space)
20613 sym = space;
20614 else
20615 sym = new (&objfile->objfile_obstack) symbol;
20616 OBJSTAT (objfile, n_syms++);
20617
20618 /* Cache this symbol's name and the name's demangled form (if any). */
20619 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
20620 /* Fortran does not have mangling standard and the mangling does differ
20621 between gfortran, iFort etc. */
20622 const char *physname
20623 = (cu->per_cu->lang == language_fortran
20624 ? dwarf2_full_name (name, die, cu)
20625 : dwarf2_physname (name, die, cu));
20626 const char *linkagename = dw2_linkage_name (die, cu);
20627
20628 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
20629 sym->set_linkage_name (physname);
20630 else
20631 {
20632 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20633 sym->set_linkage_name (linkagename);
20634 }
20635
20636 /* Handle DW_AT_artificial. */
20637 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20638 if (attr != nullptr)
20639 sym->set_is_artificial (attr->as_boolean ());
20640
20641 /* Default assumptions.
20642 Use the passed type or decode it from the die. */
20643 sym->set_domain (VAR_DOMAIN);
20644 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20645 if (type != NULL)
20646 sym->set_type (type);
20647 else
20648 sym->set_type (die_type (die, cu));
20649 attr = dwarf2_attr (die,
20650 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20651 cu);
20652 if (attr != nullptr)
20653 sym->set_line (attr->constant_value (0));
20654
20655 attr = dwarf2_attr (die,
20656 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20657 cu);
20658 if (attr != nullptr && attr->is_nonnegative ())
20659 {
20660 file_name_index file_index
20661 = (file_name_index) attr->as_nonnegative ();
20662 struct file_entry *fe;
20663
20664 if (cu->line_header != NULL)
20665 fe = cu->line_header->file_name_at (file_index);
20666 else
20667 fe = NULL;
20668
20669 if (fe == NULL)
20670 complaint (_("file index out of range"));
20671 else
20672 sym->set_symtab (fe->symtab);
20673 }
20674
20675 switch (die->tag)
20676 {
20677 case DW_TAG_label:
20678 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20679 if (attr != nullptr)
20680 {
20681 CORE_ADDR addr;
20682
20683 addr = attr->as_address ();
20684 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20685 sym->set_value_address (addr);
20686 sym->set_aclass_index (LOC_LABEL);
20687 }
20688 else
20689 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20690 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20691 sym->set_domain (LABEL_DOMAIN);
20692 add_symbol_to_list (sym, cu->list_in_scope);
20693 break;
20694 case DW_TAG_subprogram:
20695 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20696 finish_block. */
20697 sym->set_aclass_index (LOC_BLOCK);
20698 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20699 if ((attr2 != nullptr && attr2->as_boolean ())
20700 || cu->per_cu->lang == language_ada
20701 || cu->per_cu->lang == language_fortran)
20702 {
20703 /* Subprograms marked external are stored as a global symbol.
20704 Ada and Fortran subprograms, whether marked external or
20705 not, are always stored as a global symbol, because we want
20706 to be able to access them globally. For instance, we want
20707 to be able to break on a nested subprogram without having
20708 to specify the context. */
20709 list_to_add = cu->get_builder ()->get_global_symbols ();
20710 }
20711 else
20712 {
20713 list_to_add = cu->list_in_scope;
20714 }
20715 break;
20716 case DW_TAG_inlined_subroutine:
20717 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20718 finish_block. */
20719 sym->set_aclass_index (LOC_BLOCK);
20720 sym->set_is_inlined (1);
20721 list_to_add = cu->list_in_scope;
20722 break;
20723 case DW_TAG_template_value_param:
20724 suppress_add = 1;
20725 /* Fall through. */
20726 case DW_TAG_constant:
20727 case DW_TAG_variable:
20728 case DW_TAG_member:
20729 /* Compilation with minimal debug info may result in
20730 variables with missing type entries. Change the
20731 misleading `void' type to something sensible. */
20732 if (sym->type ()->code () == TYPE_CODE_VOID)
20733 sym->set_type (objfile_type (objfile)->builtin_int);
20734
20735 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20736 /* In the case of DW_TAG_member, we should only be called for
20737 static const members. */
20738 if (die->tag == DW_TAG_member)
20739 {
20740 /* dwarf2_add_field uses die_is_declaration,
20741 so we do the same. */
20742 gdb_assert (die_is_declaration (die, cu));
20743 gdb_assert (attr);
20744 }
20745 if (attr != nullptr)
20746 {
20747 dwarf2_const_value (attr, sym, cu);
20748 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20749 if (!suppress_add)
20750 {
20751 if (attr2 != nullptr && attr2->as_boolean ())
20752 list_to_add = cu->get_builder ()->get_global_symbols ();
20753 else
20754 list_to_add = cu->list_in_scope;
20755 }
20756 break;
20757 }
20758 attr = dwarf2_attr (die, DW_AT_location, cu);
20759 if (attr != nullptr)
20760 {
20761 var_decode_location (attr, sym, cu);
20762 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20763
20764 /* Fortran explicitly imports any global symbols to the local
20765 scope by DW_TAG_common_block. */
20766 if (cu->per_cu->lang == language_fortran && die->parent
20767 && die->parent->tag == DW_TAG_common_block)
20768 attr2 = NULL;
20769
20770 if (sym->aclass () == LOC_STATIC
20771 && sym->value_address () == 0
20772 && !per_objfile->per_bfd->has_section_at_zero)
20773 {
20774 /* When a static variable is eliminated by the linker,
20775 the corresponding debug information is not stripped
20776 out, but the variable address is set to null;
20777 do not add such variables into symbol table. */
20778 }
20779 else if (attr2 != nullptr && attr2->as_boolean ())
20780 {
20781 if (sym->aclass () == LOC_STATIC
20782 && (objfile->flags & OBJF_MAINLINE) == 0
20783 && per_objfile->per_bfd->can_copy)
20784 {
20785 /* A global static variable might be subject to
20786 copy relocation. We first check for a local
20787 minsym, though, because maybe the symbol was
20788 marked hidden, in which case this would not
20789 apply. */
20790 bound_minimal_symbol found
20791 = (lookup_minimal_symbol_linkage
20792 (sym->linkage_name (), objfile));
20793 if (found.minsym != nullptr)
20794 sym->maybe_copied = 1;
20795 }
20796
20797 /* A variable with DW_AT_external is never static,
20798 but it may be block-scoped. */
20799 list_to_add
20800 = ((cu->list_in_scope
20801 == cu->get_builder ()->get_file_symbols ())
20802 ? cu->get_builder ()->get_global_symbols ()
20803 : cu->list_in_scope);
20804 }
20805 else
20806 list_to_add = cu->list_in_scope;
20807 }
20808 else
20809 {
20810 /* We do not know the address of this symbol.
20811 If it is an external symbol and we have type information
20812 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20813 The address of the variable will then be determined from
20814 the minimal symbol table whenever the variable is
20815 referenced. */
20816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20817
20818 /* Fortran explicitly imports any global symbols to the local
20819 scope by DW_TAG_common_block. */
20820 if (cu->per_cu->lang == language_fortran && die->parent
20821 && die->parent->tag == DW_TAG_common_block)
20822 {
20823 /* SYMBOL_CLASS doesn't matter here because
20824 read_common_block is going to reset it. */
20825 if (!suppress_add)
20826 list_to_add = cu->list_in_scope;
20827 }
20828 else if (attr2 != nullptr && attr2->as_boolean ()
20829 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20830 {
20831 /* A variable with DW_AT_external is never static, but it
20832 may be block-scoped. */
20833 list_to_add
20834 = ((cu->list_in_scope
20835 == cu->get_builder ()->get_file_symbols ())
20836 ? cu->get_builder ()->get_global_symbols ()
20837 : cu->list_in_scope);
20838
20839 sym->set_aclass_index (LOC_UNRESOLVED);
20840 }
20841 else if (!die_is_declaration (die, cu))
20842 {
20843 /* Use the default LOC_OPTIMIZED_OUT class. */
20844 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
20845 if (!suppress_add)
20846 list_to_add = cu->list_in_scope;
20847 }
20848 }
20849 break;
20850 case DW_TAG_formal_parameter:
20851 {
20852 /* If we are inside a function, mark this as an argument. If
20853 not, we might be looking at an argument to an inlined function
20854 when we do not have enough information to show inlined frames;
20855 pretend it's a local variable in that case so that the user can
20856 still see it. */
20857 struct context_stack *curr
20858 = cu->get_builder ()->get_current_context_stack ();
20859 if (curr != nullptr && curr->name != nullptr)
20860 sym->set_is_argument (1);
20861 attr = dwarf2_attr (die, DW_AT_location, cu);
20862 if (attr != nullptr)
20863 {
20864 var_decode_location (attr, sym, cu);
20865 }
20866 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20867 if (attr != nullptr)
20868 {
20869 dwarf2_const_value (attr, sym, cu);
20870 }
20871
20872 list_to_add = cu->list_in_scope;
20873 }
20874 break;
20875 case DW_TAG_unspecified_parameters:
20876 /* From varargs functions; gdb doesn't seem to have any
20877 interest in this information, so just ignore it for now.
20878 (FIXME?) */
20879 break;
20880 case DW_TAG_template_type_param:
20881 suppress_add = 1;
20882 /* Fall through. */
20883 case DW_TAG_class_type:
20884 case DW_TAG_interface_type:
20885 case DW_TAG_structure_type:
20886 case DW_TAG_union_type:
20887 case DW_TAG_set_type:
20888 case DW_TAG_enumeration_type:
20889 case DW_TAG_namelist:
20890 if (die->tag == DW_TAG_namelist)
20891 {
20892 sym->set_aclass_index (LOC_STATIC);
20893 sym->set_domain (VAR_DOMAIN);
20894 }
20895 else
20896 {
20897 sym->set_aclass_index (LOC_TYPEDEF);
20898 sym->set_domain (STRUCT_DOMAIN);
20899 }
20900 {
20901 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20902 really ever be static objects: otherwise, if you try
20903 to, say, break of a class's method and you're in a file
20904 which doesn't mention that class, it won't work unless
20905 the check for all static symbols in lookup_symbol_aux
20906 saves you. See the OtherFileClass tests in
20907 gdb.c++/namespace.exp. */
20908
20909 if (!suppress_add)
20910 {
20911 buildsym_compunit *builder = cu->get_builder ();
20912 list_to_add
20913 = (cu->list_in_scope == builder->get_file_symbols ()
20914 && cu->per_cu->lang == language_cplus
20915 ? builder->get_global_symbols ()
20916 : cu->list_in_scope);
20917
20918 /* The semantics of C++ state that "struct foo {
20919 ... }" also defines a typedef for "foo". */
20920 if (cu->per_cu->lang == language_cplus
20921 || cu->per_cu->lang == language_ada
20922 || cu->per_cu->lang == language_d
20923 || cu->per_cu->lang == language_rust)
20924 {
20925 /* The symbol's name is already allocated along
20926 with this objfile, so we don't need to
20927 duplicate it for the type. */
20928 if (sym->type ()->name () == 0)
20929 sym->type ()->set_name (sym->search_name ());
20930 }
20931 }
20932 }
20933 break;
20934 case DW_TAG_typedef:
20935 sym->set_aclass_index (LOC_TYPEDEF);
20936 sym->set_domain (VAR_DOMAIN);
20937 list_to_add = cu->list_in_scope;
20938 break;
20939 case DW_TAG_array_type:
20940 case DW_TAG_base_type:
20941 case DW_TAG_subrange_type:
20942 case DW_TAG_generic_subrange:
20943 sym->set_aclass_index (LOC_TYPEDEF);
20944 sym->set_domain (VAR_DOMAIN);
20945 list_to_add = cu->list_in_scope;
20946 break;
20947 case DW_TAG_enumerator:
20948 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20949 if (attr != nullptr)
20950 {
20951 dwarf2_const_value (attr, sym, cu);
20952 }
20953 {
20954 /* NOTE: carlton/2003-11-10: See comment above in the
20955 DW_TAG_class_type, etc. block. */
20956
20957 list_to_add
20958 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20959 && cu->per_cu->lang == language_cplus
20960 ? cu->get_builder ()->get_global_symbols ()
20961 : cu->list_in_scope);
20962 }
20963 break;
20964 case DW_TAG_imported_declaration:
20965 case DW_TAG_namespace:
20966 sym->set_aclass_index (LOC_TYPEDEF);
20967 list_to_add = cu->get_builder ()->get_global_symbols ();
20968 break;
20969 case DW_TAG_module:
20970 sym->set_aclass_index (LOC_TYPEDEF);
20971 sym->set_domain (MODULE_DOMAIN);
20972 list_to_add = cu->get_builder ()->get_global_symbols ();
20973 break;
20974 case DW_TAG_common_block:
20975 sym->set_aclass_index (LOC_COMMON_BLOCK);
20976 sym->set_domain (COMMON_BLOCK_DOMAIN);
20977 add_symbol_to_list (sym, cu->list_in_scope);
20978 break;
20979 default:
20980 /* Not a tag we recognize. Hopefully we aren't processing
20981 trash data, but since we must specifically ignore things
20982 we don't recognize, there is nothing else we should do at
20983 this point. */
20984 complaint (_("unsupported tag: '%s'"),
20985 dwarf_tag_name (die->tag));
20986 break;
20987 }
20988
20989 if (suppress_add)
20990 {
20991 sym->hash_next = objfile->template_symbols;
20992 objfile->template_symbols = sym;
20993 list_to_add = NULL;
20994 }
20995
20996 if (list_to_add != NULL)
20997 add_symbol_to_list (sym, list_to_add);
20998
20999 /* For the benefit of old versions of GCC, check for anonymous
21000 namespaces based on the demangled name. */
21001 if (!cu->processing_has_namespace_info
21002 && cu->per_cu->lang == language_cplus)
21003 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21004 }
21005 return (sym);
21006 }
21007
21008 /* Given an attr with a DW_FORM_dataN value in host byte order,
21009 zero-extend it as appropriate for the symbol's type. The DWARF
21010 standard (v4) is not entirely clear about the meaning of using
21011 DW_FORM_dataN for a constant with a signed type, where the type is
21012 wider than the data. The conclusion of a discussion on the DWARF
21013 list was that this is unspecified. We choose to always zero-extend
21014 because that is the interpretation long in use by GCC. */
21015
21016 static gdb_byte *
21017 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21018 struct dwarf2_cu *cu, LONGEST *value, int bits)
21019 {
21020 struct objfile *objfile = cu->per_objfile->objfile;
21021 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21022 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21023 LONGEST l = attr->constant_value (0);
21024
21025 if (bits < sizeof (*value) * 8)
21026 {
21027 l &= ((LONGEST) 1 << bits) - 1;
21028 *value = l;
21029 }
21030 else if (bits == sizeof (*value) * 8)
21031 *value = l;
21032 else
21033 {
21034 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21035 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21036 return bytes;
21037 }
21038
21039 return NULL;
21040 }
21041
21042 /* Read a constant value from an attribute. Either set *VALUE, or if
21043 the value does not fit in *VALUE, set *BYTES - either already
21044 allocated on the objfile obstack, or newly allocated on OBSTACK,
21045 or, set *BATON, if we translated the constant to a location
21046 expression. */
21047
21048 static void
21049 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21050 const char *name, struct obstack *obstack,
21051 struct dwarf2_cu *cu,
21052 LONGEST *value, const gdb_byte **bytes,
21053 struct dwarf2_locexpr_baton **baton)
21054 {
21055 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21056 struct objfile *objfile = per_objfile->objfile;
21057 struct comp_unit_head *cu_header = &cu->header;
21058 struct dwarf_block *blk;
21059 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21060 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21061
21062 *value = 0;
21063 *bytes = NULL;
21064 *baton = NULL;
21065
21066 switch (attr->form)
21067 {
21068 case DW_FORM_addr:
21069 case DW_FORM_addrx:
21070 case DW_FORM_GNU_addr_index:
21071 {
21072 gdb_byte *data;
21073
21074 if (TYPE_LENGTH (type) != cu_header->addr_size)
21075 dwarf2_const_value_length_mismatch_complaint (name,
21076 cu_header->addr_size,
21077 TYPE_LENGTH (type));
21078 /* Symbols of this form are reasonably rare, so we just
21079 piggyback on the existing location code rather than writing
21080 a new implementation of symbol_computed_ops. */
21081 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21082 (*baton)->per_objfile = per_objfile;
21083 (*baton)->per_cu = cu->per_cu;
21084 gdb_assert ((*baton)->per_cu);
21085
21086 (*baton)->size = 2 + cu_header->addr_size;
21087 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21088 (*baton)->data = data;
21089
21090 data[0] = DW_OP_addr;
21091 store_unsigned_integer (&data[1], cu_header->addr_size,
21092 byte_order, attr->as_address ());
21093 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21094 }
21095 break;
21096 case DW_FORM_string:
21097 case DW_FORM_strp:
21098 case DW_FORM_strx:
21099 case DW_FORM_GNU_str_index:
21100 case DW_FORM_GNU_strp_alt:
21101 /* The string is already allocated on the objfile obstack, point
21102 directly to it. */
21103 *bytes = (const gdb_byte *) attr->as_string ();
21104 break;
21105 case DW_FORM_block1:
21106 case DW_FORM_block2:
21107 case DW_FORM_block4:
21108 case DW_FORM_block:
21109 case DW_FORM_exprloc:
21110 case DW_FORM_data16:
21111 blk = attr->as_block ();
21112 if (TYPE_LENGTH (type) != blk->size)
21113 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21114 TYPE_LENGTH (type));
21115 *bytes = blk->data;
21116 break;
21117
21118 /* The DW_AT_const_value attributes are supposed to carry the
21119 symbol's value "represented as it would be on the target
21120 architecture." By the time we get here, it's already been
21121 converted to host endianness, so we just need to sign- or
21122 zero-extend it as appropriate. */
21123 case DW_FORM_data1:
21124 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21125 break;
21126 case DW_FORM_data2:
21127 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21128 break;
21129 case DW_FORM_data4:
21130 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21131 break;
21132 case DW_FORM_data8:
21133 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21134 break;
21135
21136 case DW_FORM_sdata:
21137 case DW_FORM_implicit_const:
21138 *value = attr->as_signed ();
21139 break;
21140
21141 case DW_FORM_udata:
21142 *value = attr->as_unsigned ();
21143 break;
21144
21145 default:
21146 complaint (_("unsupported const value attribute form: '%s'"),
21147 dwarf_form_name (attr->form));
21148 *value = 0;
21149 break;
21150 }
21151 }
21152
21153
21154 /* Copy constant value from an attribute to a symbol. */
21155
21156 static void
21157 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21158 struct dwarf2_cu *cu)
21159 {
21160 struct objfile *objfile = cu->per_objfile->objfile;
21161 LONGEST value;
21162 const gdb_byte *bytes;
21163 struct dwarf2_locexpr_baton *baton;
21164
21165 dwarf2_const_value_attr (attr, sym->type (),
21166 sym->print_name (),
21167 &objfile->objfile_obstack, cu,
21168 &value, &bytes, &baton);
21169
21170 if (baton != NULL)
21171 {
21172 SYMBOL_LOCATION_BATON (sym) = baton;
21173 sym->set_aclass_index (dwarf2_locexpr_index);
21174 }
21175 else if (bytes != NULL)
21176 {
21177 sym->set_value_bytes (bytes);
21178 sym->set_aclass_index (LOC_CONST_BYTES);
21179 }
21180 else
21181 {
21182 sym->set_value_longest (value);
21183 sym->set_aclass_index (LOC_CONST);
21184 }
21185 }
21186
21187 /* Return the type of the die in question using its DW_AT_type attribute. */
21188
21189 static struct type *
21190 die_type (struct die_info *die, struct dwarf2_cu *cu)
21191 {
21192 struct attribute *type_attr;
21193
21194 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21195 if (!type_attr)
21196 {
21197 struct objfile *objfile = cu->per_objfile->objfile;
21198 /* A missing DW_AT_type represents a void type. */
21199 return objfile_type (objfile)->builtin_void;
21200 }
21201
21202 return lookup_die_type (die, type_attr, cu);
21203 }
21204
21205 /* True iff CU's producer generates GNAT Ada auxiliary information
21206 that allows to find parallel types through that information instead
21207 of having to do expensive parallel lookups by type name. */
21208
21209 static int
21210 need_gnat_info (struct dwarf2_cu *cu)
21211 {
21212 /* Assume that the Ada compiler was GNAT, which always produces
21213 the auxiliary information. */
21214 return (cu->per_cu->lang == language_ada);
21215 }
21216
21217 /* Return the auxiliary type of the die in question using its
21218 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21219 attribute is not present. */
21220
21221 static struct type *
21222 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21223 {
21224 struct attribute *type_attr;
21225
21226 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21227 if (!type_attr)
21228 return NULL;
21229
21230 return lookup_die_type (die, type_attr, cu);
21231 }
21232
21233 /* If DIE has a descriptive_type attribute, then set the TYPE's
21234 descriptive type accordingly. */
21235
21236 static void
21237 set_descriptive_type (struct type *type, struct die_info *die,
21238 struct dwarf2_cu *cu)
21239 {
21240 struct type *descriptive_type = die_descriptive_type (die, cu);
21241
21242 if (descriptive_type)
21243 {
21244 ALLOCATE_GNAT_AUX_TYPE (type);
21245 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21246 }
21247 }
21248
21249 /* Return the containing type of the die in question using its
21250 DW_AT_containing_type attribute. */
21251
21252 static struct type *
21253 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21254 {
21255 struct attribute *type_attr;
21256 struct objfile *objfile = cu->per_objfile->objfile;
21257
21258 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21259 if (!type_attr)
21260 error (_("Dwarf Error: Problem turning containing type into gdb type "
21261 "[in module %s]"), objfile_name (objfile));
21262
21263 return lookup_die_type (die, type_attr, cu);
21264 }
21265
21266 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21267
21268 static struct type *
21269 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21270 {
21271 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21272 struct objfile *objfile = per_objfile->objfile;
21273 char *saved;
21274
21275 std::string message
21276 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21277 objfile_name (objfile),
21278 sect_offset_str (cu->header.sect_off),
21279 sect_offset_str (die->sect_off));
21280 saved = obstack_strdup (&objfile->objfile_obstack, message);
21281
21282 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21283 }
21284
21285 /* Look up the type of DIE in CU using its type attribute ATTR.
21286 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21287 DW_AT_containing_type.
21288 If there is no type substitute an error marker. */
21289
21290 static struct type *
21291 lookup_die_type (struct die_info *die, const struct attribute *attr,
21292 struct dwarf2_cu *cu)
21293 {
21294 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21295 struct objfile *objfile = per_objfile->objfile;
21296 struct type *this_type;
21297
21298 gdb_assert (attr->name == DW_AT_type
21299 || attr->name == DW_AT_GNAT_descriptive_type
21300 || attr->name == DW_AT_containing_type);
21301
21302 /* First see if we have it cached. */
21303
21304 if (attr->form == DW_FORM_GNU_ref_alt)
21305 {
21306 struct dwarf2_per_cu_data *per_cu;
21307 sect_offset sect_off = attr->get_ref_die_offset ();
21308
21309 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21310 per_objfile->per_bfd);
21311 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21312 }
21313 else if (attr->form_is_ref ())
21314 {
21315 sect_offset sect_off = attr->get_ref_die_offset ();
21316
21317 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21318 }
21319 else if (attr->form == DW_FORM_ref_sig8)
21320 {
21321 ULONGEST signature = attr->as_signature ();
21322
21323 return get_signatured_type (die, signature, cu);
21324 }
21325 else
21326 {
21327 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21328 " at %s [in module %s]"),
21329 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21330 objfile_name (objfile));
21331 return build_error_marker_type (cu, die);
21332 }
21333
21334 /* If not cached we need to read it in. */
21335
21336 if (this_type == NULL)
21337 {
21338 struct die_info *type_die = NULL;
21339 struct dwarf2_cu *type_cu = cu;
21340
21341 if (attr->form_is_ref ())
21342 type_die = follow_die_ref (die, attr, &type_cu);
21343 if (type_die == NULL)
21344 return build_error_marker_type (cu, die);
21345 /* If we find the type now, it's probably because the type came
21346 from an inter-CU reference and the type's CU got expanded before
21347 ours. */
21348 this_type = read_type_die (type_die, type_cu);
21349 }
21350
21351 /* If we still don't have a type use an error marker. */
21352
21353 if (this_type == NULL)
21354 return build_error_marker_type (cu, die);
21355
21356 return this_type;
21357 }
21358
21359 /* Return the type in DIE, CU.
21360 Returns NULL for invalid types.
21361
21362 This first does a lookup in die_type_hash,
21363 and only reads the die in if necessary.
21364
21365 NOTE: This can be called when reading in partial or full symbols. */
21366
21367 static struct type *
21368 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21369 {
21370 struct type *this_type;
21371
21372 this_type = get_die_type (die, cu);
21373 if (this_type)
21374 return this_type;
21375
21376 return read_type_die_1 (die, cu);
21377 }
21378
21379 /* Read the type in DIE, CU.
21380 Returns NULL for invalid types. */
21381
21382 static struct type *
21383 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21384 {
21385 struct type *this_type = NULL;
21386
21387 switch (die->tag)
21388 {
21389 case DW_TAG_class_type:
21390 case DW_TAG_interface_type:
21391 case DW_TAG_structure_type:
21392 case DW_TAG_union_type:
21393 this_type = read_structure_type (die, cu);
21394 break;
21395 case DW_TAG_enumeration_type:
21396 this_type = read_enumeration_type (die, cu);
21397 break;
21398 case DW_TAG_subprogram:
21399 case DW_TAG_subroutine_type:
21400 case DW_TAG_inlined_subroutine:
21401 this_type = read_subroutine_type (die, cu);
21402 break;
21403 case DW_TAG_array_type:
21404 this_type = read_array_type (die, cu);
21405 break;
21406 case DW_TAG_set_type:
21407 this_type = read_set_type (die, cu);
21408 break;
21409 case DW_TAG_pointer_type:
21410 this_type = read_tag_pointer_type (die, cu);
21411 break;
21412 case DW_TAG_ptr_to_member_type:
21413 this_type = read_tag_ptr_to_member_type (die, cu);
21414 break;
21415 case DW_TAG_reference_type:
21416 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21417 break;
21418 case DW_TAG_rvalue_reference_type:
21419 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21420 break;
21421 case DW_TAG_const_type:
21422 this_type = read_tag_const_type (die, cu);
21423 break;
21424 case DW_TAG_volatile_type:
21425 this_type = read_tag_volatile_type (die, cu);
21426 break;
21427 case DW_TAG_restrict_type:
21428 this_type = read_tag_restrict_type (die, cu);
21429 break;
21430 case DW_TAG_string_type:
21431 this_type = read_tag_string_type (die, cu);
21432 break;
21433 case DW_TAG_typedef:
21434 this_type = read_typedef (die, cu);
21435 break;
21436 case DW_TAG_generic_subrange:
21437 case DW_TAG_subrange_type:
21438 this_type = read_subrange_type (die, cu);
21439 break;
21440 case DW_TAG_base_type:
21441 this_type = read_base_type (die, cu);
21442 break;
21443 case DW_TAG_unspecified_type:
21444 this_type = read_unspecified_type (die, cu);
21445 break;
21446 case DW_TAG_namespace:
21447 this_type = read_namespace_type (die, cu);
21448 break;
21449 case DW_TAG_module:
21450 this_type = read_module_type (die, cu);
21451 break;
21452 case DW_TAG_atomic_type:
21453 this_type = read_tag_atomic_type (die, cu);
21454 break;
21455 default:
21456 complaint (_("unexpected tag in read_type_die: '%s'"),
21457 dwarf_tag_name (die->tag));
21458 break;
21459 }
21460
21461 return this_type;
21462 }
21463
21464 /* See if we can figure out if the class lives in a namespace. We do
21465 this by looking for a member function; its demangled name will
21466 contain namespace info, if there is any.
21467 Return the computed name or NULL.
21468 Space for the result is allocated on the objfile's obstack.
21469 This is the full-die version of guess_partial_die_structure_name.
21470 In this case we know DIE has no useful parent. */
21471
21472 static const char *
21473 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21474 {
21475 struct die_info *spec_die;
21476 struct dwarf2_cu *spec_cu;
21477 struct die_info *child;
21478 struct objfile *objfile = cu->per_objfile->objfile;
21479
21480 spec_cu = cu;
21481 spec_die = die_specification (die, &spec_cu);
21482 if (spec_die != NULL)
21483 {
21484 die = spec_die;
21485 cu = spec_cu;
21486 }
21487
21488 for (child = die->child;
21489 child != NULL;
21490 child = child->sibling)
21491 {
21492 if (child->tag == DW_TAG_subprogram)
21493 {
21494 const char *linkage_name = dw2_linkage_name (child, cu);
21495
21496 if (linkage_name != NULL)
21497 {
21498 gdb::unique_xmalloc_ptr<char> actual_name
21499 (cu->language_defn->class_name_from_physname (linkage_name));
21500 const char *name = NULL;
21501
21502 if (actual_name != NULL)
21503 {
21504 const char *die_name = dwarf2_name (die, cu);
21505
21506 if (die_name != NULL
21507 && strcmp (die_name, actual_name.get ()) != 0)
21508 {
21509 /* Strip off the class name from the full name.
21510 We want the prefix. */
21511 int die_name_len = strlen (die_name);
21512 int actual_name_len = strlen (actual_name.get ());
21513 const char *ptr = actual_name.get ();
21514
21515 /* Test for '::' as a sanity check. */
21516 if (actual_name_len > die_name_len + 2
21517 && ptr[actual_name_len - die_name_len - 1] == ':')
21518 name = obstack_strndup (
21519 &objfile->per_bfd->storage_obstack,
21520 ptr, actual_name_len - die_name_len - 2);
21521 }
21522 }
21523 return name;
21524 }
21525 }
21526 }
21527
21528 return NULL;
21529 }
21530
21531 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21532 prefix part in such case. See
21533 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21534
21535 static const char *
21536 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21537 {
21538 struct attribute *attr;
21539 const char *base;
21540
21541 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21542 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21543 return NULL;
21544
21545 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21546 return NULL;
21547
21548 attr = dw2_linkage_name_attr (die, cu);
21549 const char *attr_name = attr->as_string ();
21550 if (attr == NULL || attr_name == NULL)
21551 return NULL;
21552
21553 /* dwarf2_name had to be already called. */
21554 gdb_assert (attr->canonical_string_p ());
21555
21556 /* Strip the base name, keep any leading namespaces/classes. */
21557 base = strrchr (attr_name, ':');
21558 if (base == NULL || base == attr_name || base[-1] != ':')
21559 return "";
21560
21561 struct objfile *objfile = cu->per_objfile->objfile;
21562 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21563 attr_name,
21564 &base[-1] - attr_name);
21565 }
21566
21567 /* Return the name of the namespace/class that DIE is defined within,
21568 or "" if we can't tell. The caller should not xfree the result.
21569
21570 For example, if we're within the method foo() in the following
21571 code:
21572
21573 namespace N {
21574 class C {
21575 void foo () {
21576 }
21577 };
21578 }
21579
21580 then determine_prefix on foo's die will return "N::C". */
21581
21582 static const char *
21583 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21584 {
21585 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21586 struct die_info *parent, *spec_die;
21587 struct dwarf2_cu *spec_cu;
21588 struct type *parent_type;
21589 const char *retval;
21590
21591 if (cu->per_cu->lang != language_cplus
21592 && cu->per_cu->lang != language_fortran
21593 && cu->per_cu->lang != language_d
21594 && cu->per_cu->lang != language_rust)
21595 return "";
21596
21597 retval = anonymous_struct_prefix (die, cu);
21598 if (retval)
21599 return retval;
21600
21601 /* We have to be careful in the presence of DW_AT_specification.
21602 For example, with GCC 3.4, given the code
21603
21604 namespace N {
21605 void foo() {
21606 // Definition of N::foo.
21607 }
21608 }
21609
21610 then we'll have a tree of DIEs like this:
21611
21612 1: DW_TAG_compile_unit
21613 2: DW_TAG_namespace // N
21614 3: DW_TAG_subprogram // declaration of N::foo
21615 4: DW_TAG_subprogram // definition of N::foo
21616 DW_AT_specification // refers to die #3
21617
21618 Thus, when processing die #4, we have to pretend that we're in
21619 the context of its DW_AT_specification, namely the contex of die
21620 #3. */
21621 spec_cu = cu;
21622 spec_die = die_specification (die, &spec_cu);
21623 if (spec_die == NULL)
21624 parent = die->parent;
21625 else
21626 {
21627 parent = spec_die->parent;
21628 cu = spec_cu;
21629 }
21630
21631 if (parent == NULL)
21632 return "";
21633 else if (parent->building_fullname)
21634 {
21635 const char *name;
21636 const char *parent_name;
21637
21638 /* It has been seen on RealView 2.2 built binaries,
21639 DW_TAG_template_type_param types actually _defined_ as
21640 children of the parent class:
21641
21642 enum E {};
21643 template class <class Enum> Class{};
21644 Class<enum E> class_e;
21645
21646 1: DW_TAG_class_type (Class)
21647 2: DW_TAG_enumeration_type (E)
21648 3: DW_TAG_enumerator (enum1:0)
21649 3: DW_TAG_enumerator (enum2:1)
21650 ...
21651 2: DW_TAG_template_type_param
21652 DW_AT_type DW_FORM_ref_udata (E)
21653
21654 Besides being broken debug info, it can put GDB into an
21655 infinite loop. Consider:
21656
21657 When we're building the full name for Class<E>, we'll start
21658 at Class, and go look over its template type parameters,
21659 finding E. We'll then try to build the full name of E, and
21660 reach here. We're now trying to build the full name of E,
21661 and look over the parent DIE for containing scope. In the
21662 broken case, if we followed the parent DIE of E, we'd again
21663 find Class, and once again go look at its template type
21664 arguments, etc., etc. Simply don't consider such parent die
21665 as source-level parent of this die (it can't be, the language
21666 doesn't allow it), and break the loop here. */
21667 name = dwarf2_name (die, cu);
21668 parent_name = dwarf2_name (parent, cu);
21669 complaint (_("template param type '%s' defined within parent '%s'"),
21670 name ? name : "<unknown>",
21671 parent_name ? parent_name : "<unknown>");
21672 return "";
21673 }
21674 else
21675 switch (parent->tag)
21676 {
21677 case DW_TAG_namespace:
21678 parent_type = read_type_die (parent, cu);
21679 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21680 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21681 Work around this problem here. */
21682 if (cu->per_cu->lang == language_cplus
21683 && strcmp (parent_type->name (), "::") == 0)
21684 return "";
21685 /* We give a name to even anonymous namespaces. */
21686 return parent_type->name ();
21687 case DW_TAG_class_type:
21688 case DW_TAG_interface_type:
21689 case DW_TAG_structure_type:
21690 case DW_TAG_union_type:
21691 case DW_TAG_module:
21692 parent_type = read_type_die (parent, cu);
21693 if (parent_type->name () != NULL)
21694 return parent_type->name ();
21695 else
21696 /* An anonymous structure is only allowed non-static data
21697 members; no typedefs, no member functions, et cetera.
21698 So it does not need a prefix. */
21699 return "";
21700 case DW_TAG_compile_unit:
21701 case DW_TAG_partial_unit:
21702 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21703 if (cu->per_cu->lang == language_cplus
21704 && !per_objfile->per_bfd->types.empty ()
21705 && die->child != NULL
21706 && (die->tag == DW_TAG_class_type
21707 || die->tag == DW_TAG_structure_type
21708 || die->tag == DW_TAG_union_type))
21709 {
21710 const char *name = guess_full_die_structure_name (die, cu);
21711 if (name != NULL)
21712 return name;
21713 }
21714 return "";
21715 case DW_TAG_subprogram:
21716 /* Nested subroutines in Fortran get a prefix with the name
21717 of the parent's subroutine. */
21718 if (cu->per_cu->lang == language_fortran)
21719 {
21720 if ((die->tag == DW_TAG_subprogram)
21721 && (dwarf2_name (parent, cu) != NULL))
21722 return dwarf2_name (parent, cu);
21723 }
21724 return "";
21725 case DW_TAG_enumeration_type:
21726 parent_type = read_type_die (parent, cu);
21727 if (parent_type->is_declared_class ())
21728 {
21729 if (parent_type->name () != NULL)
21730 return parent_type->name ();
21731 return "";
21732 }
21733 /* Fall through. */
21734 default:
21735 return determine_prefix (parent, cu);
21736 }
21737 }
21738
21739 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21740 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21741 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21742 an obconcat, otherwise allocate storage for the result. The CU argument is
21743 used to determine the language and hence, the appropriate separator. */
21744
21745 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21746
21747 static char *
21748 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21749 int physname, struct dwarf2_cu *cu)
21750 {
21751 const char *lead = "";
21752 const char *sep;
21753
21754 if (suffix == NULL || suffix[0] == '\0'
21755 || prefix == NULL || prefix[0] == '\0')
21756 sep = "";
21757 else if (cu->per_cu->lang == language_d)
21758 {
21759 /* For D, the 'main' function could be defined in any module, but it
21760 should never be prefixed. */
21761 if (strcmp (suffix, "D main") == 0)
21762 {
21763 prefix = "";
21764 sep = "";
21765 }
21766 else
21767 sep = ".";
21768 }
21769 else if (cu->per_cu->lang == language_fortran && physname)
21770 {
21771 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21772 DW_AT_MIPS_linkage_name is preferred and used instead. */
21773
21774 lead = "__";
21775 sep = "_MOD_";
21776 }
21777 else
21778 sep = "::";
21779
21780 if (prefix == NULL)
21781 prefix = "";
21782 if (suffix == NULL)
21783 suffix = "";
21784
21785 if (obs == NULL)
21786 {
21787 char *retval
21788 = ((char *)
21789 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21790
21791 strcpy (retval, lead);
21792 strcat (retval, prefix);
21793 strcat (retval, sep);
21794 strcat (retval, suffix);
21795 return retval;
21796 }
21797 else
21798 {
21799 /* We have an obstack. */
21800 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21801 }
21802 }
21803
21804 /* Get name of a die, return NULL if not found. */
21805
21806 static const char *
21807 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21808 struct objfile *objfile)
21809 {
21810 if (name && cu->per_cu->lang == language_cplus)
21811 {
21812 gdb::unique_xmalloc_ptr<char> canon_name
21813 = cp_canonicalize_string (name);
21814
21815 if (canon_name != nullptr)
21816 name = objfile->intern (canon_name.get ());
21817 }
21818
21819 return name;
21820 }
21821
21822 /* Get name of a die, return NULL if not found.
21823 Anonymous namespaces are converted to their magic string. */
21824
21825 static const char *
21826 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21827 {
21828 struct attribute *attr;
21829 struct objfile *objfile = cu->per_objfile->objfile;
21830
21831 attr = dwarf2_attr (die, DW_AT_name, cu);
21832 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
21833 if (attr_name == nullptr
21834 && die->tag != DW_TAG_namespace
21835 && die->tag != DW_TAG_class_type
21836 && die->tag != DW_TAG_interface_type
21837 && die->tag != DW_TAG_structure_type
21838 && die->tag != DW_TAG_namelist
21839 && die->tag != DW_TAG_union_type)
21840 return NULL;
21841
21842 switch (die->tag)
21843 {
21844 case DW_TAG_compile_unit:
21845 case DW_TAG_partial_unit:
21846 /* Compilation units have a DW_AT_name that is a filename, not
21847 a source language identifier. */
21848 case DW_TAG_enumeration_type:
21849 case DW_TAG_enumerator:
21850 /* These tags always have simple identifiers already; no need
21851 to canonicalize them. */
21852 return attr_name;
21853
21854 case DW_TAG_namespace:
21855 if (attr_name != nullptr)
21856 return attr_name;
21857 return CP_ANONYMOUS_NAMESPACE_STR;
21858
21859 case DW_TAG_class_type:
21860 case DW_TAG_interface_type:
21861 case DW_TAG_structure_type:
21862 case DW_TAG_union_type:
21863 case DW_TAG_namelist:
21864 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21865 structures or unions. These were of the form "._%d" in GCC 4.1,
21866 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21867 and GCC 4.4. We work around this problem by ignoring these. */
21868 if (attr_name != nullptr
21869 && (startswith (attr_name, "._")
21870 || startswith (attr_name, "<anonymous")))
21871 return NULL;
21872
21873 /* GCC might emit a nameless typedef that has a linkage name. See
21874 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21875 if (!attr || attr_name == NULL)
21876 {
21877 attr = dw2_linkage_name_attr (die, cu);
21878 attr_name = attr == nullptr ? nullptr : attr->as_string ();
21879 if (attr == NULL || attr_name == NULL)
21880 return NULL;
21881
21882 /* Avoid demangling attr_name the second time on a second
21883 call for the same DIE. */
21884 if (!attr->canonical_string_p ())
21885 {
21886 gdb::unique_xmalloc_ptr<char> demangled
21887 (gdb_demangle (attr_name, DMGL_TYPES));
21888 if (demangled == nullptr)
21889 return nullptr;
21890
21891 attr->set_string_canonical (objfile->intern (demangled.get ()));
21892 attr_name = attr->as_string ();
21893 }
21894
21895 /* Strip any leading namespaces/classes, keep only the
21896 base name. DW_AT_name for named DIEs does not
21897 contain the prefixes. */
21898 const char *base = strrchr (attr_name, ':');
21899 if (base && base > attr_name && base[-1] == ':')
21900 return &base[1];
21901 else
21902 return attr_name;
21903 }
21904 break;
21905
21906 default:
21907 break;
21908 }
21909
21910 if (!attr->canonical_string_p ())
21911 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
21912 objfile));
21913 return attr->as_string ();
21914 }
21915
21916 /* Return the die that this die in an extension of, or NULL if there
21917 is none. *EXT_CU is the CU containing DIE on input, and the CU
21918 containing the return value on output. */
21919
21920 static struct die_info *
21921 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21922 {
21923 struct attribute *attr;
21924
21925 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21926 if (attr == NULL)
21927 return NULL;
21928
21929 return follow_die_ref (die, attr, ext_cu);
21930 }
21931
21932 static void
21933 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21934 {
21935 unsigned int i;
21936
21937 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
21938 indent, "",
21939 dwarf_tag_name (die->tag), die->abbrev,
21940 sect_offset_str (die->sect_off));
21941
21942 if (die->parent != NULL)
21943 gdb_printf (f, "%*s parent at offset: %s\n",
21944 indent, "",
21945 sect_offset_str (die->parent->sect_off));
21946
21947 gdb_printf (f, "%*s has children: %s\n",
21948 indent, "",
21949 dwarf_bool_name (die->child != NULL));
21950
21951 gdb_printf (f, "%*s attributes:\n", indent, "");
21952
21953 for (i = 0; i < die->num_attrs; ++i)
21954 {
21955 gdb_printf (f, "%*s %s (%s) ",
21956 indent, "",
21957 dwarf_attr_name (die->attrs[i].name),
21958 dwarf_form_name (die->attrs[i].form));
21959
21960 switch (die->attrs[i].form)
21961 {
21962 case DW_FORM_addr:
21963 case DW_FORM_addrx:
21964 case DW_FORM_GNU_addr_index:
21965 gdb_printf (f, "address: ");
21966 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
21967 break;
21968 case DW_FORM_block2:
21969 case DW_FORM_block4:
21970 case DW_FORM_block:
21971 case DW_FORM_block1:
21972 gdb_printf (f, "block: size %s",
21973 pulongest (die->attrs[i].as_block ()->size));
21974 break;
21975 case DW_FORM_exprloc:
21976 gdb_printf (f, "expression: size %s",
21977 pulongest (die->attrs[i].as_block ()->size));
21978 break;
21979 case DW_FORM_data16:
21980 gdb_printf (f, "constant of 16 bytes");
21981 break;
21982 case DW_FORM_ref_addr:
21983 gdb_printf (f, "ref address: ");
21984 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
21985 break;
21986 case DW_FORM_GNU_ref_alt:
21987 gdb_printf (f, "alt ref address: ");
21988 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
21989 break;
21990 case DW_FORM_ref1:
21991 case DW_FORM_ref2:
21992 case DW_FORM_ref4:
21993 case DW_FORM_ref8:
21994 case DW_FORM_ref_udata:
21995 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
21996 (long) (die->attrs[i].as_unsigned ()));
21997 break;
21998 case DW_FORM_data1:
21999 case DW_FORM_data2:
22000 case DW_FORM_data4:
22001 case DW_FORM_data8:
22002 case DW_FORM_udata:
22003 gdb_printf (f, "constant: %s",
22004 pulongest (die->attrs[i].as_unsigned ()));
22005 break;
22006 case DW_FORM_sec_offset:
22007 gdb_printf (f, "section offset: %s",
22008 pulongest (die->attrs[i].as_unsigned ()));
22009 break;
22010 case DW_FORM_ref_sig8:
22011 gdb_printf (f, "signature: %s",
22012 hex_string (die->attrs[i].as_signature ()));
22013 break;
22014 case DW_FORM_string:
22015 case DW_FORM_strp:
22016 case DW_FORM_line_strp:
22017 case DW_FORM_strx:
22018 case DW_FORM_GNU_str_index:
22019 case DW_FORM_GNU_strp_alt:
22020 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22021 die->attrs[i].as_string ()
22022 ? die->attrs[i].as_string () : "",
22023 die->attrs[i].canonical_string_p () ? "is" : "not");
22024 break;
22025 case DW_FORM_flag:
22026 if (die->attrs[i].as_boolean ())
22027 gdb_printf (f, "flag: TRUE");
22028 else
22029 gdb_printf (f, "flag: FALSE");
22030 break;
22031 case DW_FORM_flag_present:
22032 gdb_printf (f, "flag: TRUE");
22033 break;
22034 case DW_FORM_indirect:
22035 /* The reader will have reduced the indirect form to
22036 the "base form" so this form should not occur. */
22037 gdb_printf (f,
22038 "unexpected attribute form: DW_FORM_indirect");
22039 break;
22040 case DW_FORM_sdata:
22041 case DW_FORM_implicit_const:
22042 gdb_printf (f, "constant: %s",
22043 plongest (die->attrs[i].as_signed ()));
22044 break;
22045 default:
22046 gdb_printf (f, "unsupported attribute form: %d.",
22047 die->attrs[i].form);
22048 break;
22049 }
22050 gdb_printf (f, "\n");
22051 }
22052 }
22053
22054 static void
22055 dump_die_for_error (struct die_info *die)
22056 {
22057 dump_die_shallow (gdb_stderr, 0, die);
22058 }
22059
22060 static void
22061 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22062 {
22063 int indent = level * 4;
22064
22065 gdb_assert (die != NULL);
22066
22067 if (level >= max_level)
22068 return;
22069
22070 dump_die_shallow (f, indent, die);
22071
22072 if (die->child != NULL)
22073 {
22074 gdb_printf (f, "%*s Children:", indent, "");
22075 if (level + 1 < max_level)
22076 {
22077 gdb_printf (f, "\n");
22078 dump_die_1 (f, level + 1, max_level, die->child);
22079 }
22080 else
22081 {
22082 gdb_printf (f,
22083 " [not printed, max nesting level reached]\n");
22084 }
22085 }
22086
22087 if (die->sibling != NULL && level > 0)
22088 {
22089 dump_die_1 (f, level, max_level, die->sibling);
22090 }
22091 }
22092
22093 /* This is called from the pdie macro in gdbinit.in.
22094 It's not static so gcc will keep a copy callable from gdb. */
22095
22096 void
22097 dump_die (struct die_info *die, int max_level)
22098 {
22099 dump_die_1 (gdb_stdlog, 0, max_level, die);
22100 }
22101
22102 static void
22103 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22104 {
22105 void **slot;
22106
22107 slot = htab_find_slot_with_hash (cu->die_hash, die,
22108 to_underlying (die->sect_off),
22109 INSERT);
22110
22111 *slot = die;
22112 }
22113
22114 /* Follow reference or signature attribute ATTR of SRC_DIE.
22115 On entry *REF_CU is the CU of SRC_DIE.
22116 On exit *REF_CU is the CU of the result. */
22117
22118 static struct die_info *
22119 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22120 struct dwarf2_cu **ref_cu)
22121 {
22122 struct die_info *die;
22123
22124 if (attr->form_is_ref ())
22125 die = follow_die_ref (src_die, attr, ref_cu);
22126 else if (attr->form == DW_FORM_ref_sig8)
22127 die = follow_die_sig (src_die, attr, ref_cu);
22128 else
22129 {
22130 dump_die_for_error (src_die);
22131 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22132 objfile_name ((*ref_cu)->per_objfile->objfile));
22133 }
22134
22135 return die;
22136 }
22137
22138 /* Follow reference OFFSET.
22139 On entry *REF_CU is the CU of the source die referencing OFFSET.
22140 On exit *REF_CU is the CU of the result.
22141 Returns NULL if OFFSET is invalid. */
22142
22143 static struct die_info *
22144 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22145 struct dwarf2_cu **ref_cu)
22146 {
22147 struct die_info temp_die;
22148 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22150
22151 gdb_assert (cu->per_cu != NULL);
22152
22153 target_cu = cu;
22154
22155 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22156 "source CU contains target offset: %d",
22157 sect_offset_str (cu->per_cu->sect_off),
22158 sect_offset_str (sect_off),
22159 cu->header.offset_in_cu_p (sect_off));
22160
22161 if (cu->per_cu->is_debug_types)
22162 {
22163 /* .debug_types CUs cannot reference anything outside their CU.
22164 If they need to, they have to reference a signatured type via
22165 DW_FORM_ref_sig8. */
22166 if (!cu->header.offset_in_cu_p (sect_off))
22167 return NULL;
22168 }
22169 else if (offset_in_dwz != cu->per_cu->is_dwz
22170 || !cu->header.offset_in_cu_p (sect_off))
22171 {
22172 struct dwarf2_per_cu_data *per_cu;
22173
22174 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22175 per_objfile->per_bfd);
22176
22177 dwarf_read_debug_printf_v ("target CU offset: %s, "
22178 "target CU DIEs loaded: %d",
22179 sect_offset_str (per_cu->sect_off),
22180 per_objfile->get_cu (per_cu) != nullptr);
22181
22182 /* If necessary, add it to the queue and load its DIEs.
22183
22184 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22185 it doesn't mean they are currently loaded. Since we require them
22186 to be loaded, we must check for ourselves. */
22187 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
22188 || per_objfile->get_cu (per_cu) == nullptr)
22189 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22190 false, cu->per_cu->lang);
22191
22192 target_cu = per_objfile->get_cu (per_cu);
22193 gdb_assert (target_cu != nullptr);
22194 }
22195 else if (cu->dies == NULL)
22196 {
22197 /* We're loading full DIEs during partial symbol reading. */
22198 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22199 language_minimal);
22200 }
22201
22202 *ref_cu = target_cu;
22203 temp_die.sect_off = sect_off;
22204
22205 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22206 &temp_die,
22207 to_underlying (sect_off));
22208 }
22209
22210 /* Follow reference attribute ATTR of SRC_DIE.
22211 On entry *REF_CU is the CU of SRC_DIE.
22212 On exit *REF_CU is the CU of the result. */
22213
22214 static struct die_info *
22215 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22216 struct dwarf2_cu **ref_cu)
22217 {
22218 sect_offset sect_off = attr->get_ref_die_offset ();
22219 struct dwarf2_cu *cu = *ref_cu;
22220 struct die_info *die;
22221
22222 die = follow_die_offset (sect_off,
22223 (attr->form == DW_FORM_GNU_ref_alt
22224 || cu->per_cu->is_dwz),
22225 ref_cu);
22226 if (!die)
22227 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22228 "at %s [in module %s]"),
22229 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22230 objfile_name (cu->per_objfile->objfile));
22231
22232 return die;
22233 }
22234
22235 /* See read.h. */
22236
22237 struct dwarf2_locexpr_baton
22238 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22239 dwarf2_per_cu_data *per_cu,
22240 dwarf2_per_objfile *per_objfile,
22241 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22242 bool resolve_abstract_p)
22243 {
22244 struct die_info *die;
22245 struct attribute *attr;
22246 struct dwarf2_locexpr_baton retval;
22247 struct objfile *objfile = per_objfile->objfile;
22248
22249 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22250 if (cu == nullptr)
22251 cu = load_cu (per_cu, per_objfile, false);
22252
22253 if (cu == nullptr)
22254 {
22255 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22256 Instead just throw an error, not much else we can do. */
22257 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22258 sect_offset_str (sect_off), objfile_name (objfile));
22259 }
22260
22261 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22262 if (!die)
22263 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22264 sect_offset_str (sect_off), objfile_name (objfile));
22265
22266 attr = dwarf2_attr (die, DW_AT_location, cu);
22267 if (!attr && resolve_abstract_p
22268 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22269 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22270 {
22271 CORE_ADDR pc = get_frame_pc ();
22272 CORE_ADDR baseaddr = objfile->text_section_offset ();
22273 struct gdbarch *gdbarch = objfile->arch ();
22274
22275 for (const auto &cand_off
22276 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22277 {
22278 struct dwarf2_cu *cand_cu = cu;
22279 struct die_info *cand
22280 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22281 if (!cand
22282 || !cand->parent
22283 || cand->parent->tag != DW_TAG_subprogram)
22284 continue;
22285
22286 CORE_ADDR pc_low, pc_high;
22287 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22288 if (pc_low == ((CORE_ADDR) -1))
22289 continue;
22290 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22291 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22292 if (!(pc_low <= pc && pc < pc_high))
22293 continue;
22294
22295 die = cand;
22296 attr = dwarf2_attr (die, DW_AT_location, cu);
22297 break;
22298 }
22299 }
22300
22301 if (!attr)
22302 {
22303 /* DWARF: "If there is no such attribute, then there is no effect.".
22304 DATA is ignored if SIZE is 0. */
22305
22306 retval.data = NULL;
22307 retval.size = 0;
22308 }
22309 else if (attr->form_is_section_offset ())
22310 {
22311 struct dwarf2_loclist_baton loclist_baton;
22312 CORE_ADDR pc = get_frame_pc ();
22313 size_t size;
22314
22315 fill_in_loclist_baton (cu, &loclist_baton, attr);
22316
22317 retval.data = dwarf2_find_location_expression (&loclist_baton,
22318 &size, pc);
22319 retval.size = size;
22320 }
22321 else
22322 {
22323 if (!attr->form_is_block ())
22324 error (_("Dwarf Error: DIE at %s referenced in module %s "
22325 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
22327
22328 struct dwarf_block *block = attr->as_block ();
22329 retval.data = block->data;
22330 retval.size = block->size;
22331 }
22332 retval.per_objfile = per_objfile;
22333 retval.per_cu = cu->per_cu;
22334
22335 per_objfile->age_comp_units ();
22336
22337 return retval;
22338 }
22339
22340 /* See read.h. */
22341
22342 struct dwarf2_locexpr_baton
22343 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22344 dwarf2_per_cu_data *per_cu,
22345 dwarf2_per_objfile *per_objfile,
22346 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22347 {
22348 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22349
22350 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22351 get_frame_pc);
22352 }
22353
22354 /* Write a constant of a given type as target-ordered bytes into
22355 OBSTACK. */
22356
22357 static const gdb_byte *
22358 write_constant_as_bytes (struct obstack *obstack,
22359 enum bfd_endian byte_order,
22360 struct type *type,
22361 ULONGEST value,
22362 LONGEST *len)
22363 {
22364 gdb_byte *result;
22365
22366 *len = TYPE_LENGTH (type);
22367 result = (gdb_byte *) obstack_alloc (obstack, *len);
22368 store_unsigned_integer (result, *len, byte_order, value);
22369
22370 return result;
22371 }
22372
22373 /* See read.h. */
22374
22375 const gdb_byte *
22376 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22377 dwarf2_per_cu_data *per_cu,
22378 dwarf2_per_objfile *per_objfile,
22379 obstack *obstack,
22380 LONGEST *len)
22381 {
22382 struct die_info *die;
22383 struct attribute *attr;
22384 const gdb_byte *result = NULL;
22385 struct type *type;
22386 LONGEST value;
22387 enum bfd_endian byte_order;
22388 struct objfile *objfile = per_objfile->objfile;
22389
22390 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22391 if (cu == nullptr)
22392 cu = load_cu (per_cu, per_objfile, false);
22393
22394 if (cu == nullptr)
22395 {
22396 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22397 Instead just throw an error, not much else we can do. */
22398 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22399 sect_offset_str (sect_off), objfile_name (objfile));
22400 }
22401
22402 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22403 if (!die)
22404 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22405 sect_offset_str (sect_off), objfile_name (objfile));
22406
22407 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22408 if (attr == NULL)
22409 return NULL;
22410
22411 byte_order = (bfd_big_endian (objfile->obfd)
22412 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22413
22414 switch (attr->form)
22415 {
22416 case DW_FORM_addr:
22417 case DW_FORM_addrx:
22418 case DW_FORM_GNU_addr_index:
22419 {
22420 gdb_byte *tem;
22421
22422 *len = cu->header.addr_size;
22423 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22424 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22425 result = tem;
22426 }
22427 break;
22428 case DW_FORM_string:
22429 case DW_FORM_strp:
22430 case DW_FORM_strx:
22431 case DW_FORM_GNU_str_index:
22432 case DW_FORM_GNU_strp_alt:
22433 /* The string is already allocated on the objfile obstack, point
22434 directly to it. */
22435 {
22436 const char *attr_name = attr->as_string ();
22437 result = (const gdb_byte *) attr_name;
22438 *len = strlen (attr_name);
22439 }
22440 break;
22441 case DW_FORM_block1:
22442 case DW_FORM_block2:
22443 case DW_FORM_block4:
22444 case DW_FORM_block:
22445 case DW_FORM_exprloc:
22446 case DW_FORM_data16:
22447 {
22448 struct dwarf_block *block = attr->as_block ();
22449 result = block->data;
22450 *len = block->size;
22451 }
22452 break;
22453
22454 /* The DW_AT_const_value attributes are supposed to carry the
22455 symbol's value "represented as it would be on the target
22456 architecture." By the time we get here, it's already been
22457 converted to host endianness, so we just need to sign- or
22458 zero-extend it as appropriate. */
22459 case DW_FORM_data1:
22460 type = die_type (die, cu);
22461 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22462 if (result == NULL)
22463 result = write_constant_as_bytes (obstack, byte_order,
22464 type, value, len);
22465 break;
22466 case DW_FORM_data2:
22467 type = die_type (die, cu);
22468 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22469 if (result == NULL)
22470 result = write_constant_as_bytes (obstack, byte_order,
22471 type, value, len);
22472 break;
22473 case DW_FORM_data4:
22474 type = die_type (die, cu);
22475 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22476 if (result == NULL)
22477 result = write_constant_as_bytes (obstack, byte_order,
22478 type, value, len);
22479 break;
22480 case DW_FORM_data8:
22481 type = die_type (die, cu);
22482 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22483 if (result == NULL)
22484 result = write_constant_as_bytes (obstack, byte_order,
22485 type, value, len);
22486 break;
22487
22488 case DW_FORM_sdata:
22489 case DW_FORM_implicit_const:
22490 type = die_type (die, cu);
22491 result = write_constant_as_bytes (obstack, byte_order,
22492 type, attr->as_signed (), len);
22493 break;
22494
22495 case DW_FORM_udata:
22496 type = die_type (die, cu);
22497 result = write_constant_as_bytes (obstack, byte_order,
22498 type, attr->as_unsigned (), len);
22499 break;
22500
22501 default:
22502 complaint (_("unsupported const value attribute form: '%s'"),
22503 dwarf_form_name (attr->form));
22504 break;
22505 }
22506
22507 return result;
22508 }
22509
22510 /* See read.h. */
22511
22512 struct type *
22513 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22514 dwarf2_per_cu_data *per_cu,
22515 dwarf2_per_objfile *per_objfile,
22516 const char **var_name)
22517 {
22518 struct die_info *die;
22519
22520 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22521 if (cu == nullptr)
22522 cu = load_cu (per_cu, per_objfile, false);
22523
22524 if (cu == nullptr)
22525 return nullptr;
22526
22527 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22528 if (!die)
22529 return NULL;
22530
22531 if (var_name != nullptr)
22532 *var_name = var_decl_name (die, cu);
22533 return die_type (die, cu);
22534 }
22535
22536 /* See read.h. */
22537
22538 struct type *
22539 dwarf2_get_die_type (cu_offset die_offset,
22540 dwarf2_per_cu_data *per_cu,
22541 dwarf2_per_objfile *per_objfile)
22542 {
22543 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22544 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22545 }
22546
22547 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22548 On entry *REF_CU is the CU of SRC_DIE.
22549 On exit *REF_CU is the CU of the result.
22550 Returns NULL if the referenced DIE isn't found. */
22551
22552 static struct die_info *
22553 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22554 struct dwarf2_cu **ref_cu)
22555 {
22556 struct die_info temp_die;
22557 struct dwarf2_cu *sig_cu;
22558 struct die_info *die;
22559 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22560
22561
22562 /* While it might be nice to assert sig_type->type == NULL here,
22563 we can get here for DW_AT_imported_declaration where we need
22564 the DIE not the type. */
22565
22566 /* If necessary, add it to the queue and load its DIEs.
22567
22568 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22569 it doesn't mean they are currently loaded. Since we require them
22570 to be loaded, we must check for ourselves. */
22571 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22572 language_minimal)
22573 || per_objfile->get_cu (sig_type) == nullptr)
22574 read_signatured_type (sig_type, per_objfile);
22575
22576 sig_cu = per_objfile->get_cu (sig_type);
22577 gdb_assert (sig_cu != NULL);
22578 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22579 temp_die.sect_off = sig_type->type_offset_in_section;
22580 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22581 to_underlying (temp_die.sect_off));
22582 if (die)
22583 {
22584 /* For .gdb_index version 7 keep track of included TUs.
22585 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22586 if (per_objfile->per_bfd->index_table != NULL
22587 && !per_objfile->per_bfd->index_table->version_check ())
22588 {
22589 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22590 }
22591
22592 *ref_cu = sig_cu;
22593 return die;
22594 }
22595
22596 return NULL;
22597 }
22598
22599 /* Follow signatured type referenced by ATTR in SRC_DIE.
22600 On entry *REF_CU is the CU of SRC_DIE.
22601 On exit *REF_CU is the CU of the result.
22602 The result is the DIE of the type.
22603 If the referenced type cannot be found an error is thrown. */
22604
22605 static struct die_info *
22606 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22607 struct dwarf2_cu **ref_cu)
22608 {
22609 ULONGEST signature = attr->as_signature ();
22610 struct signatured_type *sig_type;
22611 struct die_info *die;
22612
22613 gdb_assert (attr->form == DW_FORM_ref_sig8);
22614
22615 sig_type = lookup_signatured_type (*ref_cu, signature);
22616 /* sig_type will be NULL if the signatured type is missing from
22617 the debug info. */
22618 if (sig_type == NULL)
22619 {
22620 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22621 " from DIE at %s [in module %s]"),
22622 hex_string (signature), sect_offset_str (src_die->sect_off),
22623 objfile_name ((*ref_cu)->per_objfile->objfile));
22624 }
22625
22626 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22627 if (die == NULL)
22628 {
22629 dump_die_for_error (src_die);
22630 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature), sect_offset_str (src_die->sect_off),
22633 objfile_name ((*ref_cu)->per_objfile->objfile));
22634 }
22635
22636 return die;
22637 }
22638
22639 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22640 reading in and processing the type unit if necessary. */
22641
22642 static struct type *
22643 get_signatured_type (struct die_info *die, ULONGEST signature,
22644 struct dwarf2_cu *cu)
22645 {
22646 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22647 struct signatured_type *sig_type;
22648 struct dwarf2_cu *type_cu;
22649 struct die_info *type_die;
22650 struct type *type;
22651
22652 sig_type = lookup_signatured_type (cu, signature);
22653 /* sig_type will be NULL if the signatured type is missing from
22654 the debug info. */
22655 if (sig_type == NULL)
22656 {
22657 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22658 " from DIE at %s [in module %s]"),
22659 hex_string (signature), sect_offset_str (die->sect_off),
22660 objfile_name (per_objfile->objfile));
22661 return build_error_marker_type (cu, die);
22662 }
22663
22664 /* If we already know the type we're done. */
22665 type = per_objfile->get_type_for_signatured_type (sig_type);
22666 if (type != nullptr)
22667 return type;
22668
22669 type_cu = cu;
22670 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22671 if (type_die != NULL)
22672 {
22673 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22674 is created. This is important, for example, because for c++ classes
22675 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22676 type = read_type_die (type_die, type_cu);
22677 if (type == NULL)
22678 {
22679 complaint (_("Dwarf Error: Cannot build signatured type %s"
22680 " referenced from DIE at %s [in module %s]"),
22681 hex_string (signature), sect_offset_str (die->sect_off),
22682 objfile_name (per_objfile->objfile));
22683 type = build_error_marker_type (cu, die);
22684 }
22685 }
22686 else
22687 {
22688 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
22691 objfile_name (per_objfile->objfile));
22692 type = build_error_marker_type (cu, die);
22693 }
22694
22695 per_objfile->set_type_for_signatured_type (sig_type, type);
22696
22697 return type;
22698 }
22699
22700 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22701 reading in and processing the type unit if necessary. */
22702
22703 static struct type *
22704 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22705 struct dwarf2_cu *cu) /* ARI: editCase function */
22706 {
22707 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22708 if (attr->form_is_ref ())
22709 {
22710 struct dwarf2_cu *type_cu = cu;
22711 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22712
22713 return read_type_die (type_die, type_cu);
22714 }
22715 else if (attr->form == DW_FORM_ref_sig8)
22716 {
22717 return get_signatured_type (die, attr->as_signature (), cu);
22718 }
22719 else
22720 {
22721 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22722
22723 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22724 " at %s [in module %s]"),
22725 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22726 objfile_name (per_objfile->objfile));
22727 return build_error_marker_type (cu, die);
22728 }
22729 }
22730
22731 /* Load the DIEs associated with type unit PER_CU into memory. */
22732
22733 static void
22734 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22735 dwarf2_per_objfile *per_objfile)
22736 {
22737 struct signatured_type *sig_type;
22738
22739 /* We have the per_cu, but we need the signatured_type.
22740 Fortunately this is an easy translation. */
22741 gdb_assert (per_cu->is_debug_types);
22742 sig_type = (struct signatured_type *) per_cu;
22743
22744 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22745
22746 read_signatured_type (sig_type, per_objfile);
22747
22748 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22749 }
22750
22751 /* Read in a signatured type and build its CU and DIEs.
22752 If the type is a stub for the real type in a DWO file,
22753 read in the real type from the DWO file as well. */
22754
22755 static void
22756 read_signatured_type (signatured_type *sig_type,
22757 dwarf2_per_objfile *per_objfile)
22758 {
22759 gdb_assert (sig_type->is_debug_types);
22760 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
22761
22762 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
22763
22764 if (!reader.dummy_p)
22765 {
22766 struct dwarf2_cu *cu = reader.cu;
22767 const gdb_byte *info_ptr = reader.info_ptr;
22768
22769 gdb_assert (cu->die_hash == NULL);
22770 cu->die_hash =
22771 htab_create_alloc_ex (cu->header.length / 12,
22772 die_hash,
22773 die_eq,
22774 NULL,
22775 &cu->comp_unit_obstack,
22776 hashtab_obstack_allocate,
22777 dummy_obstack_deallocate);
22778
22779 if (reader.comp_unit_die->has_children)
22780 reader.comp_unit_die->child
22781 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22782 reader.comp_unit_die);
22783 cu->dies = reader.comp_unit_die;
22784 /* comp_unit_die is not stored in die_hash, no need. */
22785
22786 /* We try not to read any attributes in this function, because
22787 not all CUs needed for references have been loaded yet, and
22788 symbol table processing isn't initialized. But we have to
22789 set the CU language, or we won't be able to build types
22790 correctly. Similarly, if we do not read the producer, we can
22791 not apply producer-specific interpretation. */
22792 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22793
22794 reader.keep ();
22795 }
22796
22797 sig_type->tu_read = 1;
22798 }
22799
22800 /* Decode simple location descriptions.
22801 Given a pointer to a dwarf block that defines a location, compute
22802 the location and return the value. If COMPUTED is non-null, it is
22803 set to true to indicate that decoding was successful, and false
22804 otherwise. If COMPUTED is null, then this function may emit a
22805 complaint. */
22806
22807 static CORE_ADDR
22808 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22809 {
22810 struct objfile *objfile = cu->per_objfile->objfile;
22811 size_t i;
22812 size_t size = blk->size;
22813 const gdb_byte *data = blk->data;
22814 CORE_ADDR stack[64];
22815 int stacki;
22816 unsigned int bytes_read, unsnd;
22817 gdb_byte op;
22818
22819 if (computed != nullptr)
22820 *computed = false;
22821
22822 i = 0;
22823 stacki = 0;
22824 stack[stacki] = 0;
22825 stack[++stacki] = 0;
22826
22827 while (i < size)
22828 {
22829 op = data[i++];
22830 switch (op)
22831 {
22832 case DW_OP_lit0:
22833 case DW_OP_lit1:
22834 case DW_OP_lit2:
22835 case DW_OP_lit3:
22836 case DW_OP_lit4:
22837 case DW_OP_lit5:
22838 case DW_OP_lit6:
22839 case DW_OP_lit7:
22840 case DW_OP_lit8:
22841 case DW_OP_lit9:
22842 case DW_OP_lit10:
22843 case DW_OP_lit11:
22844 case DW_OP_lit12:
22845 case DW_OP_lit13:
22846 case DW_OP_lit14:
22847 case DW_OP_lit15:
22848 case DW_OP_lit16:
22849 case DW_OP_lit17:
22850 case DW_OP_lit18:
22851 case DW_OP_lit19:
22852 case DW_OP_lit20:
22853 case DW_OP_lit21:
22854 case DW_OP_lit22:
22855 case DW_OP_lit23:
22856 case DW_OP_lit24:
22857 case DW_OP_lit25:
22858 case DW_OP_lit26:
22859 case DW_OP_lit27:
22860 case DW_OP_lit28:
22861 case DW_OP_lit29:
22862 case DW_OP_lit30:
22863 case DW_OP_lit31:
22864 stack[++stacki] = op - DW_OP_lit0;
22865 break;
22866
22867 case DW_OP_reg0:
22868 case DW_OP_reg1:
22869 case DW_OP_reg2:
22870 case DW_OP_reg3:
22871 case DW_OP_reg4:
22872 case DW_OP_reg5:
22873 case DW_OP_reg6:
22874 case DW_OP_reg7:
22875 case DW_OP_reg8:
22876 case DW_OP_reg9:
22877 case DW_OP_reg10:
22878 case DW_OP_reg11:
22879 case DW_OP_reg12:
22880 case DW_OP_reg13:
22881 case DW_OP_reg14:
22882 case DW_OP_reg15:
22883 case DW_OP_reg16:
22884 case DW_OP_reg17:
22885 case DW_OP_reg18:
22886 case DW_OP_reg19:
22887 case DW_OP_reg20:
22888 case DW_OP_reg21:
22889 case DW_OP_reg22:
22890 case DW_OP_reg23:
22891 case DW_OP_reg24:
22892 case DW_OP_reg25:
22893 case DW_OP_reg26:
22894 case DW_OP_reg27:
22895 case DW_OP_reg28:
22896 case DW_OP_reg29:
22897 case DW_OP_reg30:
22898 case DW_OP_reg31:
22899 stack[++stacki] = op - DW_OP_reg0;
22900 if (i < size)
22901 {
22902 if (computed == nullptr)
22903 dwarf2_complex_location_expr_complaint ();
22904 else
22905 return 0;
22906 }
22907 break;
22908
22909 case DW_OP_regx:
22910 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22911 i += bytes_read;
22912 stack[++stacki] = unsnd;
22913 if (i < size)
22914 {
22915 if (computed == nullptr)
22916 dwarf2_complex_location_expr_complaint ();
22917 else
22918 return 0;
22919 }
22920 break;
22921
22922 case DW_OP_addr:
22923 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22924 &bytes_read);
22925 i += bytes_read;
22926 break;
22927
22928 case DW_OP_const1u:
22929 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22930 i += 1;
22931 break;
22932
22933 case DW_OP_const1s:
22934 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22935 i += 1;
22936 break;
22937
22938 case DW_OP_const2u:
22939 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22940 i += 2;
22941 break;
22942
22943 case DW_OP_const2s:
22944 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22945 i += 2;
22946 break;
22947
22948 case DW_OP_const4u:
22949 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22950 i += 4;
22951 break;
22952
22953 case DW_OP_const4s:
22954 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22955 i += 4;
22956 break;
22957
22958 case DW_OP_const8u:
22959 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22960 i += 8;
22961 break;
22962
22963 case DW_OP_constu:
22964 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22965 &bytes_read);
22966 i += bytes_read;
22967 break;
22968
22969 case DW_OP_consts:
22970 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22971 i += bytes_read;
22972 break;
22973
22974 case DW_OP_dup:
22975 stack[stacki + 1] = stack[stacki];
22976 stacki++;
22977 break;
22978
22979 case DW_OP_plus:
22980 stack[stacki - 1] += stack[stacki];
22981 stacki--;
22982 break;
22983
22984 case DW_OP_plus_uconst:
22985 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22986 &bytes_read);
22987 i += bytes_read;
22988 break;
22989
22990 case DW_OP_minus:
22991 stack[stacki - 1] -= stack[stacki];
22992 stacki--;
22993 break;
22994
22995 case DW_OP_deref:
22996 /* If we're not the last op, then we definitely can't encode
22997 this using GDB's address_class enum. This is valid for partial
22998 global symbols, although the variable's address will be bogus
22999 in the psymtab. */
23000 if (i < size)
23001 {
23002 if (computed == nullptr)
23003 dwarf2_complex_location_expr_complaint ();
23004 else
23005 return 0;
23006 }
23007 break;
23008
23009 case DW_OP_GNU_push_tls_address:
23010 case DW_OP_form_tls_address:
23011 /* The top of the stack has the offset from the beginning
23012 of the thread control block at which the variable is located. */
23013 /* Nothing should follow this operator, so the top of stack would
23014 be returned. */
23015 /* This is valid for partial global symbols, but the variable's
23016 address will be bogus in the psymtab. Make it always at least
23017 non-zero to not look as a variable garbage collected by linker
23018 which have DW_OP_addr 0. */
23019 if (i < size)
23020 {
23021 if (computed == nullptr)
23022 dwarf2_complex_location_expr_complaint ();
23023 else
23024 return 0;
23025 }
23026 stack[stacki]++;
23027 break;
23028
23029 case DW_OP_GNU_uninit:
23030 if (computed != nullptr)
23031 return 0;
23032 break;
23033
23034 case DW_OP_addrx:
23035 case DW_OP_GNU_addr_index:
23036 case DW_OP_GNU_const_index:
23037 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23038 &bytes_read);
23039 i += bytes_read;
23040 break;
23041
23042 default:
23043 if (computed == nullptr)
23044 {
23045 const char *name = get_DW_OP_name (op);
23046
23047 if (name)
23048 complaint (_("unsupported stack op: '%s'"),
23049 name);
23050 else
23051 complaint (_("unsupported stack op: '%02x'"),
23052 op);
23053 }
23054
23055 return (stack[stacki]);
23056 }
23057
23058 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23059 outside of the allocated space. Also enforce minimum>0. */
23060 if (stacki >= ARRAY_SIZE (stack) - 1)
23061 {
23062 if (computed == nullptr)
23063 complaint (_("location description stack overflow"));
23064 return 0;
23065 }
23066
23067 if (stacki <= 0)
23068 {
23069 if (computed == nullptr)
23070 complaint (_("location description stack underflow"));
23071 return 0;
23072 }
23073 }
23074
23075 if (computed != nullptr)
23076 *computed = true;
23077 return (stack[stacki]);
23078 }
23079
23080 /* memory allocation interface */
23081
23082 static struct dwarf_block *
23083 dwarf_alloc_block (struct dwarf2_cu *cu)
23084 {
23085 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23086 }
23087
23088 static struct die_info *
23089 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23090 {
23091 struct die_info *die;
23092 size_t size = sizeof (struct die_info);
23093
23094 if (num_attrs > 1)
23095 size += (num_attrs - 1) * sizeof (struct attribute);
23096
23097 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23098 memset (die, 0, sizeof (struct die_info));
23099 return (die);
23100 }
23101
23102 \f
23103
23104 /* Macro support. */
23105
23106 /* An overload of dwarf_decode_macros that finds the correct section
23107 and ensures it is read in before calling the other overload. */
23108
23109 static void
23110 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23111 int section_is_gnu)
23112 {
23113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23114 struct objfile *objfile = per_objfile->objfile;
23115 const struct line_header *lh = cu->line_header;
23116 unsigned int offset_size = cu->header.offset_size;
23117 struct dwarf2_section_info *section;
23118 const char *section_name;
23119
23120 if (cu->dwo_unit != nullptr)
23121 {
23122 if (section_is_gnu)
23123 {
23124 section = &cu->dwo_unit->dwo_file->sections.macro;
23125 section_name = ".debug_macro.dwo";
23126 }
23127 else
23128 {
23129 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23130 section_name = ".debug_macinfo.dwo";
23131 }
23132 }
23133 else
23134 {
23135 if (section_is_gnu)
23136 {
23137 section = &per_objfile->per_bfd->macro;
23138 section_name = ".debug_macro";
23139 }
23140 else
23141 {
23142 section = &per_objfile->per_bfd->macinfo;
23143 section_name = ".debug_macinfo";
23144 }
23145 }
23146
23147 section->read (objfile);
23148 if (section->buffer == nullptr)
23149 {
23150 complaint (_("missing %s section"), section_name);
23151 return;
23152 }
23153
23154 buildsym_compunit *builder = cu->get_builder ();
23155
23156 struct dwarf2_section_info *str_offsets_section;
23157 struct dwarf2_section_info *str_section;
23158 gdb::optional<ULONGEST> str_offsets_base;
23159
23160 if (cu->dwo_unit != nullptr)
23161 {
23162 str_offsets_section = &cu->dwo_unit->dwo_file
23163 ->sections.str_offsets;
23164 str_section = &cu->dwo_unit->dwo_file->sections.str;
23165 str_offsets_base = cu->header.addr_size;
23166 }
23167 else
23168 {
23169 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23170 str_section = &per_objfile->per_bfd->str;
23171 str_offsets_base = cu->str_offsets_base;
23172 }
23173
23174 dwarf_decode_macros (per_objfile, builder, section, lh,
23175 offset_size, offset, str_section, str_offsets_section,
23176 str_offsets_base, section_is_gnu);
23177 }
23178
23179 /* Return the .debug_loc section to use for CU.
23180 For DWO files use .debug_loc.dwo. */
23181
23182 static struct dwarf2_section_info *
23183 cu_debug_loc_section (struct dwarf2_cu *cu)
23184 {
23185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23186
23187 if (cu->dwo_unit)
23188 {
23189 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23190
23191 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23192 }
23193 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23194 : &per_objfile->per_bfd->loc);
23195 }
23196
23197 /* Return the .debug_rnglists section to use for CU. */
23198 static struct dwarf2_section_info *
23199 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23200 {
23201 if (cu->header.version < 5)
23202 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23203 cu->header.version);
23204 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23205
23206 /* Make sure we read the .debug_rnglists section from the file that
23207 contains the DW_AT_ranges attribute we are reading. Normally that
23208 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23209 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23210 program. */
23211 if (cu->dwo_unit != nullptr
23212 && tag != DW_TAG_compile_unit
23213 && tag != DW_TAG_skeleton_unit)
23214 {
23215 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23216
23217 if (sections->rnglists.size > 0)
23218 return &sections->rnglists;
23219 else
23220 error (_(".debug_rnglists section is missing from .dwo file."));
23221 }
23222 return &dwarf2_per_objfile->per_bfd->rnglists;
23223 }
23224
23225 /* A helper function that fills in a dwarf2_loclist_baton. */
23226
23227 static void
23228 fill_in_loclist_baton (struct dwarf2_cu *cu,
23229 struct dwarf2_loclist_baton *baton,
23230 const struct attribute *attr)
23231 {
23232 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23233 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23234
23235 section->read (per_objfile->objfile);
23236
23237 baton->per_objfile = per_objfile;
23238 baton->per_cu = cu->per_cu;
23239 gdb_assert (baton->per_cu);
23240 /* We don't know how long the location list is, but make sure we
23241 don't run off the edge of the section. */
23242 baton->size = section->size - attr->as_unsigned ();
23243 baton->data = section->buffer + attr->as_unsigned ();
23244 if (cu->base_address.has_value ())
23245 baton->base_address = *cu->base_address;
23246 else
23247 baton->base_address = 0;
23248 baton->from_dwo = cu->dwo_unit != NULL;
23249 }
23250
23251 static void
23252 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23253 struct dwarf2_cu *cu, int is_block)
23254 {
23255 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23256 struct objfile *objfile = per_objfile->objfile;
23257 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23258
23259 if (attr->form_is_section_offset ()
23260 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23261 the section. If so, fall through to the complaint in the
23262 other branch. */
23263 && attr->as_unsigned () < section->get_size (objfile))
23264 {
23265 struct dwarf2_loclist_baton *baton;
23266
23267 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23268
23269 fill_in_loclist_baton (cu, baton, attr);
23270
23271 if (!cu->base_address.has_value ())
23272 complaint (_("Location list used without "
23273 "specifying the CU base address."));
23274
23275 sym->set_aclass_index ((is_block
23276 ? dwarf2_loclist_block_index
23277 : dwarf2_loclist_index));
23278 SYMBOL_LOCATION_BATON (sym) = baton;
23279 }
23280 else
23281 {
23282 struct dwarf2_locexpr_baton *baton;
23283
23284 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23285 baton->per_objfile = per_objfile;
23286 baton->per_cu = cu->per_cu;
23287 gdb_assert (baton->per_cu);
23288
23289 if (attr->form_is_block ())
23290 {
23291 /* Note that we're just copying the block's data pointer
23292 here, not the actual data. We're still pointing into the
23293 info_buffer for SYM's objfile; right now we never release
23294 that buffer, but when we do clean up properly this may
23295 need to change. */
23296 struct dwarf_block *block = attr->as_block ();
23297 baton->size = block->size;
23298 baton->data = block->data;
23299 }
23300 else
23301 {
23302 dwarf2_invalid_attrib_class_complaint ("location description",
23303 sym->natural_name ());
23304 baton->size = 0;
23305 }
23306
23307 sym->set_aclass_index ((is_block
23308 ? dwarf2_locexpr_block_index
23309 : dwarf2_locexpr_index));
23310 SYMBOL_LOCATION_BATON (sym) = baton;
23311 }
23312 }
23313
23314 /* See read.h. */
23315
23316 const comp_unit_head *
23317 dwarf2_per_cu_data::get_header () const
23318 {
23319 if (!m_header_read_in)
23320 {
23321 const gdb_byte *info_ptr
23322 = this->section->buffer + to_underlying (this->sect_off);
23323
23324 memset (&m_header, 0, sizeof (m_header));
23325
23326 read_comp_unit_head (&m_header, info_ptr, this->section,
23327 rcuh_kind::COMPILE);
23328
23329 m_header_read_in = true;
23330 }
23331
23332 return &m_header;
23333 }
23334
23335 /* See read.h. */
23336
23337 int
23338 dwarf2_per_cu_data::addr_size () const
23339 {
23340 return this->get_header ()->addr_size;
23341 }
23342
23343 /* See read.h. */
23344
23345 int
23346 dwarf2_per_cu_data::offset_size () const
23347 {
23348 return this->get_header ()->offset_size;
23349 }
23350
23351 /* See read.h. */
23352
23353 int
23354 dwarf2_per_cu_data::ref_addr_size () const
23355 {
23356 const comp_unit_head *header = this->get_header ();
23357
23358 if (header->version == 2)
23359 return header->addr_size;
23360 else
23361 return header->offset_size;
23362 }
23363
23364 /* A helper function for dwarf2_find_containing_comp_unit that returns
23365 the index of the result, and that searches a vector. It will
23366 return a result even if the offset in question does not actually
23367 occur in any CU. This is separate so that it can be unit
23368 tested. */
23369
23370 static int
23371 dwarf2_find_containing_comp_unit
23372 (sect_offset sect_off,
23373 unsigned int offset_in_dwz,
23374 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
23375 {
23376 int low, high;
23377
23378 low = 0;
23379 high = all_comp_units.size () - 1;
23380 while (high > low)
23381 {
23382 struct dwarf2_per_cu_data *mid_cu;
23383 int mid = low + (high - low) / 2;
23384
23385 mid_cu = all_comp_units[mid].get ();
23386 if (mid_cu->is_dwz > offset_in_dwz
23387 || (mid_cu->is_dwz == offset_in_dwz
23388 && mid_cu->sect_off + mid_cu->length > sect_off))
23389 high = mid;
23390 else
23391 low = mid + 1;
23392 }
23393 gdb_assert (low == high);
23394 return low;
23395 }
23396
23397 /* Locate the .debug_info compilation unit from CU's objfile which contains
23398 the DIE at OFFSET. Raises an error on failure. */
23399
23400 static struct dwarf2_per_cu_data *
23401 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23402 unsigned int offset_in_dwz,
23403 dwarf2_per_bfd *per_bfd)
23404 {
23405 int low = dwarf2_find_containing_comp_unit
23406 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
23407 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
23408
23409 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23410 {
23411 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23412 error (_("Dwarf Error: could not find partial DIE containing "
23413 "offset %s [in module %s]"),
23414 sect_offset_str (sect_off),
23415 bfd_get_filename (per_bfd->obfd));
23416
23417 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
23418 <= sect_off);
23419 return per_bfd->all_comp_units[low - 1].get ();
23420 }
23421 else
23422 {
23423 if (low == per_bfd->all_comp_units.size () - 1
23424 && sect_off >= this_cu->sect_off + this_cu->length)
23425 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23426 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23427 return this_cu;
23428 }
23429 }
23430
23431 #if GDB_SELF_TEST
23432
23433 namespace selftests {
23434 namespace find_containing_comp_unit {
23435
23436 static void
23437 run_test ()
23438 {
23439 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23440 dwarf2_per_cu_data *one_ptr = one.get ();
23441 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23442 dwarf2_per_cu_data *two_ptr = two.get ();
23443 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23444 dwarf2_per_cu_data *three_ptr = three.get ();
23445 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23446 dwarf2_per_cu_data *four_ptr = four.get ();
23447
23448 one->length = 5;
23449 two->sect_off = sect_offset (one->length);
23450 two->length = 7;
23451
23452 three->length = 5;
23453 three->is_dwz = 1;
23454 four->sect_off = sect_offset (three->length);
23455 four->length = 7;
23456 four->is_dwz = 1;
23457
23458 std::vector<dwarf2_per_cu_data_up> units;
23459 units.push_back (std::move (one));
23460 units.push_back (std::move (two));
23461 units.push_back (std::move (three));
23462 units.push_back (std::move (four));
23463
23464 int result;
23465
23466 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23467 SELF_CHECK (units[result].get () == one_ptr);
23468 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23469 SELF_CHECK (units[result].get () == one_ptr);
23470 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23471 SELF_CHECK (units[result].get () == two_ptr);
23472
23473 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23474 SELF_CHECK (units[result].get () == three_ptr);
23475 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23476 SELF_CHECK (units[result].get () == three_ptr);
23477 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23478 SELF_CHECK (units[result].get () == four_ptr);
23479 }
23480
23481 }
23482 }
23483
23484 #endif /* GDB_SELF_TEST */
23485
23486 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23487
23488 static void
23489 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23490 enum language pretend_language)
23491 {
23492 struct attribute *attr;
23493
23494 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23495
23496 /* Set the language we're debugging. */
23497 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23498 if (cu->producer != nullptr
23499 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23500 {
23501 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23502 attribute is not standardised yet. As a workaround for the
23503 language detection we fall back to the DW_AT_producer
23504 string. */
23505 cu->per_cu->lang = language_opencl;
23506 }
23507 else if (cu->producer != nullptr
23508 && strstr (cu->producer, "GNU Go ") != NULL)
23509 {
23510 /* Similar hack for Go. */
23511 cu->per_cu->lang = language_go;
23512 }
23513 else if (attr != nullptr)
23514 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23515 else
23516 cu->per_cu->lang = pretend_language;
23517 cu->language_defn = language_def (cu->per_cu->lang);
23518 }
23519
23520 /* See read.h. */
23521
23522 dwarf2_cu *
23523 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23524 {
23525 auto it = m_dwarf2_cus.find (per_cu);
23526 if (it == m_dwarf2_cus.end ())
23527 return nullptr;
23528
23529 return it->second;
23530 }
23531
23532 /* See read.h. */
23533
23534 void
23535 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23536 {
23537 gdb_assert (this->get_cu (per_cu) == nullptr);
23538
23539 m_dwarf2_cus[per_cu] = cu;
23540 }
23541
23542 /* See read.h. */
23543
23544 void
23545 dwarf2_per_objfile::age_comp_units ()
23546 {
23547 dwarf_read_debug_printf_v ("running");
23548
23549 /* This is not expected to be called in the middle of CU expansion. There is
23550 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23551 loaded in memory. Calling age_comp_units while the queue is in use could
23552 make us free the DIEs for a CU that is in the queue and therefore break
23553 that invariant. */
23554 gdb_assert (!this->per_bfd->queue.has_value ());
23555
23556 /* Start by clearing all marks. */
23557 for (auto pair : m_dwarf2_cus)
23558 pair.second->clear_mark ();
23559
23560 /* Traverse all CUs, mark them and their dependencies if used recently
23561 enough. */
23562 for (auto pair : m_dwarf2_cus)
23563 {
23564 dwarf2_cu *cu = pair.second;
23565
23566 cu->last_used++;
23567 if (cu->last_used <= dwarf_max_cache_age)
23568 cu->mark ();
23569 }
23570
23571 /* Delete all CUs still not marked. */
23572 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23573 {
23574 dwarf2_cu *cu = it->second;
23575
23576 if (!cu->is_marked ())
23577 {
23578 dwarf_read_debug_printf_v ("deleting old CU %s",
23579 sect_offset_str (cu->per_cu->sect_off));
23580 delete cu;
23581 it = m_dwarf2_cus.erase (it);
23582 }
23583 else
23584 it++;
23585 }
23586 }
23587
23588 /* See read.h. */
23589
23590 void
23591 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23592 {
23593 auto it = m_dwarf2_cus.find (per_cu);
23594 if (it == m_dwarf2_cus.end ())
23595 return;
23596
23597 delete it->second;
23598
23599 m_dwarf2_cus.erase (it);
23600 }
23601
23602 dwarf2_per_objfile::~dwarf2_per_objfile ()
23603 {
23604 remove_all_cus ();
23605 }
23606
23607 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23608 We store these in a hash table separate from the DIEs, and preserve them
23609 when the DIEs are flushed out of cache.
23610
23611 The CU "per_cu" pointer is needed because offset alone is not enough to
23612 uniquely identify the type. A file may have multiple .debug_types sections,
23613 or the type may come from a DWO file. Furthermore, while it's more logical
23614 to use per_cu->section+offset, with Fission the section with the data is in
23615 the DWO file but we don't know that section at the point we need it.
23616 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23617 because we can enter the lookup routine, get_die_type_at_offset, from
23618 outside this file, and thus won't necessarily have PER_CU->cu.
23619 Fortunately, PER_CU is stable for the life of the objfile. */
23620
23621 struct dwarf2_per_cu_offset_and_type
23622 {
23623 const struct dwarf2_per_cu_data *per_cu;
23624 sect_offset sect_off;
23625 struct type *type;
23626 };
23627
23628 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23629
23630 static hashval_t
23631 per_cu_offset_and_type_hash (const void *item)
23632 {
23633 const struct dwarf2_per_cu_offset_and_type *ofs
23634 = (const struct dwarf2_per_cu_offset_and_type *) item;
23635
23636 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23637 }
23638
23639 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23640
23641 static int
23642 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23643 {
23644 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23645 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23646 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23647 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23648
23649 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23650 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23651 }
23652
23653 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23654 table if necessary. For convenience, return TYPE.
23655
23656 The DIEs reading must have careful ordering to:
23657 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23658 reading current DIE.
23659 * Not trying to dereference contents of still incompletely read in types
23660 while reading in other DIEs.
23661 * Enable referencing still incompletely read in types just by a pointer to
23662 the type without accessing its fields.
23663
23664 Therefore caller should follow these rules:
23665 * Try to fetch any prerequisite types we may need to build this DIE type
23666 before building the type and calling set_die_type.
23667 * After building type call set_die_type for current DIE as soon as
23668 possible before fetching more types to complete the current type.
23669 * Make the type as complete as possible before fetching more types. */
23670
23671 static struct type *
23672 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23673 bool skip_data_location)
23674 {
23675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23676 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23677 struct objfile *objfile = per_objfile->objfile;
23678 struct attribute *attr;
23679 struct dynamic_prop prop;
23680
23681 /* For Ada types, make sure that the gnat-specific data is always
23682 initialized (if not already set). There are a few types where
23683 we should not be doing so, because the type-specific area is
23684 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23685 where the type-specific area is used to store the floatformat).
23686 But this is not a problem, because the gnat-specific information
23687 is actually not needed for these types. */
23688 if (need_gnat_info (cu)
23689 && type->code () != TYPE_CODE_FUNC
23690 && type->code () != TYPE_CODE_FLT
23691 && type->code () != TYPE_CODE_METHODPTR
23692 && type->code () != TYPE_CODE_MEMBERPTR
23693 && type->code () != TYPE_CODE_METHOD
23694 && type->code () != TYPE_CODE_FIXED_POINT
23695 && !HAVE_GNAT_AUX_INFO (type))
23696 INIT_GNAT_SPECIFIC (type);
23697
23698 /* Read DW_AT_allocated and set in type. */
23699 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23700 if (attr != NULL)
23701 {
23702 struct type *prop_type = cu->addr_sized_int_type (false);
23703 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23704 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23705 }
23706
23707 /* Read DW_AT_associated and set in type. */
23708 attr = dwarf2_attr (die, DW_AT_associated, cu);
23709 if (attr != NULL)
23710 {
23711 struct type *prop_type = cu->addr_sized_int_type (false);
23712 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23713 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23714 }
23715
23716 /* Read DW_AT_rank and set in type. */
23717 attr = dwarf2_attr (die, DW_AT_rank, cu);
23718 if (attr != NULL)
23719 {
23720 struct type *prop_type = cu->addr_sized_int_type (false);
23721 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23722 type->add_dyn_prop (DYN_PROP_RANK, prop);
23723 }
23724
23725 /* Read DW_AT_data_location and set in type. */
23726 if (!skip_data_location)
23727 {
23728 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23729 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23730 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23731 }
23732
23733 if (per_objfile->die_type_hash == NULL)
23734 per_objfile->die_type_hash
23735 = htab_up (htab_create_alloc (127,
23736 per_cu_offset_and_type_hash,
23737 per_cu_offset_and_type_eq,
23738 NULL, xcalloc, xfree));
23739
23740 ofs.per_cu = cu->per_cu;
23741 ofs.sect_off = die->sect_off;
23742 ofs.type = type;
23743 slot = (struct dwarf2_per_cu_offset_and_type **)
23744 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23745 if (*slot)
23746 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23747 sect_offset_str (die->sect_off));
23748 *slot = XOBNEW (&objfile->objfile_obstack,
23749 struct dwarf2_per_cu_offset_and_type);
23750 **slot = ofs;
23751 return type;
23752 }
23753
23754 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23755 or return NULL if the die does not have a saved type. */
23756
23757 static struct type *
23758 get_die_type_at_offset (sect_offset sect_off,
23759 dwarf2_per_cu_data *per_cu,
23760 dwarf2_per_objfile *per_objfile)
23761 {
23762 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23763
23764 if (per_objfile->die_type_hash == NULL)
23765 return NULL;
23766
23767 ofs.per_cu = per_cu;
23768 ofs.sect_off = sect_off;
23769 slot = ((struct dwarf2_per_cu_offset_and_type *)
23770 htab_find (per_objfile->die_type_hash.get (), &ofs));
23771 if (slot)
23772 return slot->type;
23773 else
23774 return NULL;
23775 }
23776
23777 /* Look up the type for DIE in CU in die_type_hash,
23778 or return NULL if DIE does not have a saved type. */
23779
23780 static struct type *
23781 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23782 {
23783 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23784 }
23785
23786 struct cmd_list_element *set_dwarf_cmdlist;
23787 struct cmd_list_element *show_dwarf_cmdlist;
23788
23789 static void
23790 show_check_physname (struct ui_file *file, int from_tty,
23791 struct cmd_list_element *c, const char *value)
23792 {
23793 gdb_printf (file,
23794 _("Whether to check \"physname\" is %s.\n"),
23795 value);
23796 }
23797
23798 void _initialize_dwarf2_read ();
23799 void
23800 _initialize_dwarf2_read ()
23801 {
23802 add_setshow_prefix_cmd ("dwarf", class_maintenance,
23803 _("\
23804 Set DWARF specific variables.\n\
23805 Configure DWARF variables such as the cache size."),
23806 _("\
23807 Show DWARF specific variables.\n\
23808 Show DWARF variables such as the cache size."),
23809 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
23810 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
23811
23812 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23813 &dwarf_max_cache_age, _("\
23814 Set the upper bound on the age of cached DWARF compilation units."), _("\
23815 Show the upper bound on the age of cached DWARF compilation units."), _("\
23816 A higher limit means that cached compilation units will be stored\n\
23817 in memory longer, and more total memory will be used. Zero disables\n\
23818 caching, which can slow down startup."),
23819 NULL,
23820 show_dwarf_max_cache_age,
23821 &set_dwarf_cmdlist,
23822 &show_dwarf_cmdlist);
23823
23824 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23825 Set debugging of the DWARF reader."), _("\
23826 Show debugging of the DWARF reader."), _("\
23827 When enabled (non-zero), debugging messages are printed during DWARF\n\
23828 reading and symtab expansion. A value of 1 (one) provides basic\n\
23829 information. A value greater than 1 provides more verbose information."),
23830 NULL,
23831 NULL,
23832 &setdebuglist, &showdebuglist);
23833
23834 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23835 Set debugging of the DWARF DIE reader."), _("\
23836 Show debugging of the DWARF DIE reader."), _("\
23837 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23838 The value is the maximum depth to print."),
23839 NULL,
23840 NULL,
23841 &setdebuglist, &showdebuglist);
23842
23843 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23844 Set debugging of the dwarf line reader."), _("\
23845 Show debugging of the dwarf line reader."), _("\
23846 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23847 A value of 1 (one) provides basic information.\n\
23848 A value greater than 1 provides more verbose information."),
23849 NULL,
23850 NULL,
23851 &setdebuglist, &showdebuglist);
23852
23853 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23854 Set cross-checking of \"physname\" code against demangler."), _("\
23855 Show cross-checking of \"physname\" code against demangler."), _("\
23856 When enabled, GDB's internal \"physname\" code is checked against\n\
23857 the demangler."),
23858 NULL, show_check_physname,
23859 &setdebuglist, &showdebuglist);
23860
23861 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23862 no_class, &use_deprecated_index_sections, _("\
23863 Set whether to use deprecated gdb_index sections."), _("\
23864 Show whether to use deprecated gdb_index sections."), _("\
23865 When enabled, deprecated .gdb_index sections are used anyway.\n\
23866 Normally they are ignored either because of a missing feature or\n\
23867 performance issue.\n\
23868 Warning: This option must be enabled before gdb reads the file."),
23869 NULL,
23870 NULL,
23871 &setlist, &showlist);
23872
23873 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23874 &dwarf2_locexpr_funcs);
23875 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23876 &dwarf2_loclist_funcs);
23877
23878 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23879 &dwarf2_block_frame_base_locexpr_funcs);
23880 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23881 &dwarf2_block_frame_base_loclist_funcs);
23882
23883 #if GDB_SELF_TEST
23884 selftests::register_test ("dw2_expand_symtabs_matching",
23885 selftests::dw2_expand_symtabs_matching::run_test);
23886 selftests::register_test ("dwarf2_find_containing_comp_unit",
23887 selftests::find_containing_comp_unit::run_test);
23888 #endif
23889 }